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>
46 typedef CLIB_PACKED (union
58 typedef struct vlib_pci_device_info
61 #define VLIB_PCI_DEVICE_INFO_F_NOIOMMU (1 << 0);
75 /* Vital Product Data */
83 /* First 64 bytes of configuration space. */
84 vlib_pci_config_t config;
89 } vlib_pci_device_info_t;
91 typedef u32 vlib_pci_dev_handle_t;
93 vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_main_t *vm,
94 vlib_pci_addr_t *addr,
95 clib_error_t **error);
96 clib_error_t *vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr,
97 vlib_pci_addr_t *root_bus);
98 vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
99 vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm,
100 vlib_pci_dev_handle_t h);
101 u32 vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h);
102 u32 vlib_pci_get_num_msix_interrupts (vlib_main_t * vm,
103 vlib_pci_dev_handle_t h);
105 uword vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h);
106 void vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
110 vlib_pci_free_device_info (vlib_pci_device_info_t * di)
114 vec_free (di->product_name);
115 vec_free (di->vpd_r);
116 vec_free (di->vpd_w);
117 vec_free (di->driver_name);
123 u16 vendor_id, device_id;
126 #define PCI_DEVICE_IDS(...) \
127 (pci_device_id_t[]) \
132 typedef void (pci_intx_handler_function_t) (vlib_main_t * vm,
133 vlib_pci_dev_handle_t handle);
134 typedef void (pci_msix_handler_function_t) (vlib_main_t * vm,
135 vlib_pci_dev_handle_t handle,
138 typedef struct _pci_device_registration
140 /* Driver init function. */
141 clib_error_t *(*init_function) (vlib_main_t * vm,
142 vlib_pci_dev_handle_t handle);
144 /* Interrupt handler */
145 pci_intx_handler_function_t *interrupt_handler;
147 /* List of registrations */
148 struct _pci_device_registration *next_registration;
150 /* Vendor/device ids supported by this driver. */
151 pci_device_id_t supported_devices[];
152 } pci_device_registration_t;
154 /* Pool of PCI devices. */
157 vlib_main_t *vlib_main;
158 pci_device_registration_t *pci_device_registrations;
160 vlib_log_class_t log_default;
163 extern vlib_pci_main_t pci_main;
165 #define PCI_REGISTER_DEVICE(x,...) \
166 __VA_ARGS__ pci_device_registration_t x; \
167 static void __vlib_add_pci_device_registration_##x (void) \
168 __attribute__((__constructor__)) ; \
169 static void __vlib_add_pci_device_registration_##x (void) \
171 vlib_pci_main_t * pm = &pci_main; \
172 x.next_registration = pm->pci_device_registrations; \
173 pm->pci_device_registrations = &x; \
175 static void __vlib_rm_pci_device_registration_##x (void) \
176 __attribute__((__destructor__)) ; \
177 static void __vlib_rm_pci_device_registration_##x (void) \
179 vlib_pci_main_t * pm = &pci_main; \
180 VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
181 &x, next_registration); \
183 __VA_ARGS__ pci_device_registration_t x
185 clib_error_t *vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
186 char *uio_driver_name, int force);
188 /* Configuration space read/write. */
189 clib_error_t *vlib_pci_read_write_config (vlib_main_t * vm,
190 vlib_pci_dev_handle_t handle,
191 vlib_read_or_write_t read_or_write,
192 uword address, void *data,
195 /* io space read/write. */
196 clib_error_t *vlib_pci_read_write_io (vlib_main_t * vm,
197 vlib_pci_dev_handle_t handle,
198 vlib_read_or_write_t read_or_write,
199 uword address, void *data, u32 n_bytes);
202 static inline clib_error_t *vlib_pci_read_##x##_##t ( \
203 vlib_main_t *vm, vlib_pci_dev_handle_t h, uword address, t *data) \
205 return vlib_pci_read_write_##x (vm, h, VLIB_READ, address, data, \
208 static inline clib_error_t *vlib_pci_write_##x##_##t ( \
209 vlib_main_t *vm, vlib_pci_dev_handle_t h, uword address, t *data) \
211 return vlib_pci_read_write_##x (vm, h, VLIB_WRITE, address, data, \
225 clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
226 pci_device_id_t ids[],
227 vlib_pci_dev_handle_t * handle);
228 void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h);
229 clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
230 u32 resource, void **result);
231 clib_error_t *vlib_pci_map_region_fixed (vlib_main_t * vm,
232 vlib_pci_dev_handle_t h,
233 u32 resource, u8 * addr,
235 clib_error_t *vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
237 clib_error_t *vlib_pci_register_intx_handler (vlib_main_t * vm,
238 vlib_pci_dev_handle_t h,
239 pci_intx_handler_function_t *
241 clib_error_t *vlib_pci_unregister_intx_handler (vlib_main_t *vm,
242 vlib_pci_dev_handle_t h);
243 clib_error_t *vlib_pci_register_msix_handler (vlib_main_t * vm,
244 vlib_pci_dev_handle_t h,
245 u32 start, u32 count,
246 pci_msix_handler_function_t *
248 clib_error_t *vlib_pci_unregister_msix_handler (vlib_main_t *vm,
249 vlib_pci_dev_handle_t h,
250 u32 start, u32 count);
251 clib_error_t *vlib_pci_enable_msix_irq (vlib_main_t * vm,
252 vlib_pci_dev_handle_t h, u16 start,
254 clib_error_t *vlib_pci_disable_msix_irq (vlib_main_t * vm,
255 vlib_pci_dev_handle_t h, u16 start,
257 clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h,
259 uword vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
262 int vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm,
263 vlib_pci_dev_handle_t h);
264 clib_error_t *vlib_pci_intr_enable (vlib_main_t *, vlib_pci_dev_handle_t);
265 clib_error_t *vlib_pci_intr_disable (vlib_main_t *, vlib_pci_dev_handle_t);
266 clib_error_t *vlib_pci_bus_master_enable (vlib_main_t *,
267 vlib_pci_dev_handle_t);
268 clib_error_t *vlib_pci_bus_master_disable (vlib_main_t *,
269 vlib_pci_dev_handle_t);
270 clib_error_t *vlib_pci_function_level_reset (vlib_main_t *,
271 vlib_pci_dev_handle_t);
273 unformat_function_t unformat_vlib_pci_addr;
274 format_function_t format_vlib_pci_addr;
275 format_function_t format_vlib_pci_link_speed;
276 format_function_t format_vlib_pci_link_speed_cap;
277 format_function_t format_vlib_pci_link_port;
278 format_function_t format_vlib_pci_vpd;
279 format_function_t format_vlib_pci_log;
281 #endif /* included_vlib_pci_h */
284 * fd.io coding-style-patch-verification: ON
287 * eval: (c-set-style "gnu")