2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * pci.h: PCI definitions.
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #ifndef included_vlib_pci_h
41 #define included_vlib_pci_h
43 #include <vlib/vlib.h>
44 #include <vlib/pci/pci_config.h>
47 typedef CLIB_PACKED (union
60 typedef struct vlib_pci_device_info
73 /* Vital Product Data */
81 /* First 64 bytes of configuration space. */
84 pci_config_type0_regs_t config0;
85 pci_config_type1_regs_t config1;
92 } vlib_pci_device_info_t;
94 typedef u32 vlib_pci_dev_handle_t;
96 vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_pci_addr_t * addr,
97 clib_error_t ** error);
98 vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
99 vlib_pci_addr_t *vlib_pci_get_addr (vlib_pci_dev_handle_t h);
100 uword vlib_pci_get_private_data (vlib_pci_dev_handle_t h);
101 void vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data);
104 vlib_pci_free_device_info (vlib_pci_device_info_t * di)
108 vec_free (di->product_name);
109 vec_free (di->vpd_r);
110 vec_free (di->vpd_w);
111 vec_free (di->driver_name);
117 u16 vendor_id, device_id;
120 typedef void (pci_intx_handler_function_t) (vlib_pci_dev_handle_t handle);
121 typedef void (pci_msix_handler_function_t) (vlib_pci_dev_handle_t handle,
124 typedef struct _pci_device_registration
126 /* Driver init function. */
127 clib_error_t *(*init_function) (vlib_main_t * vm,
128 vlib_pci_dev_handle_t handle);
130 /* Interrupt handler */
131 pci_intx_handler_function_t *interrupt_handler;
133 /* List of registrations */
134 struct _pci_device_registration *next_registration;
136 /* Vendor/device ids supported by this driver. */
137 pci_device_id_t supported_devices[];
138 } pci_device_registration_t;
140 /* Pool of PCI devices. */
143 vlib_main_t *vlib_main;
144 pci_device_registration_t *pci_device_registrations;
147 extern vlib_pci_main_t pci_main;
149 #define PCI_REGISTER_DEVICE(x,...) \
150 __VA_ARGS__ pci_device_registration_t x; \
151 static void __vlib_add_pci_device_registration_##x (void) \
152 __attribute__((__constructor__)) ; \
153 static void __vlib_add_pci_device_registration_##x (void) \
155 vlib_pci_main_t * pm = &pci_main; \
156 x.next_registration = pm->pci_device_registrations; \
157 pm->pci_device_registrations = &x; \
159 __VA_ARGS__ pci_device_registration_t x
161 clib_error_t *vlib_pci_bind_to_uio (vlib_pci_addr_t * addr,
162 char *uio_driver_name);
164 /* Configuration space read/write. */
165 clib_error_t *vlib_pci_read_write_config (vlib_pci_dev_handle_t handle,
166 vlib_read_or_write_t read_or_write,
168 void *data, u32 n_bytes);
171 static inline clib_error_t * \
172 vlib_pci_read_config_##t (vlib_pci_dev_handle_t h, \
173 uword address, t * data) \
175 return vlib_pci_read_write_config (h, VLIB_READ,address, data, \
186 static inline clib_error_t * \
187 vlib_pci_write_config_##t (vlib_pci_dev_handle_t h, uword address, \
190 return vlib_pci_read_write_config (h, VLIB_WRITE, \
191 address, data, sizeof (data[0])); \
200 static inline clib_error_t *
201 vlib_pci_intr_enable (vlib_pci_dev_handle_t h)
206 err = vlib_pci_read_config_u16 (h, 4, &command);
211 command &= ~PCI_COMMAND_INTX_DISABLE;
213 return vlib_pci_write_config_u16 (h, 4, &command);
216 static inline clib_error_t *
217 vlib_pci_intr_disable (vlib_pci_dev_handle_t h)
222 err = vlib_pci_read_config_u16 (h, 4, &command);
227 command |= PCI_COMMAND_INTX_DISABLE;
229 return vlib_pci_write_config_u16 (h, 4, &command);
232 static inline clib_error_t *
233 vlib_pci_bus_master_enable (vlib_pci_dev_handle_t h)
238 /* Set bus master enable (BME) */
239 err = vlib_pci_read_config_u16 (h, 4, &command);
244 if (command & PCI_COMMAND_BUS_MASTER)
247 command |= PCI_COMMAND_BUS_MASTER;
249 return vlib_pci_write_config_u16 (h, 4, &command);
252 clib_error_t *vlib_pci_device_open (vlib_pci_addr_t * addr,
253 pci_device_id_t ids[],
254 vlib_pci_dev_handle_t * handle);
255 void vlib_pci_device_close (vlib_pci_dev_handle_t h);
257 clib_error_t *vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource,
260 clib_error_t *vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h,
261 u32 resource, u8 * addr,
264 clib_error_t *vlib_pci_register_intx_handler (vlib_pci_dev_handle_t h,
265 pci_intx_handler_function_t *
267 clib_error_t *vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h,
268 u32 start, u32 count,
269 pci_msix_handler_function_t *
271 clib_error_t *vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start,
273 clib_error_t *vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start,
276 unformat_function_t unformat_vlib_pci_addr;
277 format_function_t format_vlib_pci_addr;
278 format_function_t format_vlib_pci_link_speed;
279 format_function_t format_vlib_pci_vpd;
281 #endif /* included_vlib_pci_h */
284 * fd.io coding-style-patch-verification: ON
287 * eval: (c-set-style "gnu")