vlib: retrieve the root bus of a given pci device
[vpp.git] / src / vlib / pci / pci.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  * pci.h: PCI definitions.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39
40 #ifndef included_vlib_pci_h
41 #define included_vlib_pci_h
42
43 #include <vlib/vlib.h>
44 #include <vlib/pci/pci_config.h>
45
46 /* *INDENT-OFF* */
47 typedef CLIB_PACKED (union
48 {
49   struct
50     {
51       u16 domain;
52       u8 bus;
53       u8 slot: 5;
54       u8 function:3;
55     };
56   u32 as_u32;
57 }) vlib_pci_addr_t;
58 /* *INDENT-ON* */
59
60 typedef struct vlib_pci_device_info
61 {
62   u32 flags;
63 #define VLIB_PCI_DEVICE_INFO_F_NOIOMMU          (1 << 0);
64
65   /* addr */
66   vlib_pci_addr_t addr;
67
68   /* Numa Node */
69   int numa_node;
70
71   /* Device data */
72   u16 device_class;
73   u16 vendor_id;
74   u16 device_id;
75
76   /* Vital Product Data */
77   u8 *product_name;
78   u8 *vpd_r;
79   u8 *vpd_w;
80
81   /* Driver name */
82   u8 *driver_name;
83
84   /* First 64 bytes of configuration space. */
85   union
86   {
87     pci_config_type0_regs_t config0;
88     pci_config_type1_regs_t config1;
89     u8 config_data[256];
90   };
91
92   /* IOMMU Group */
93   int iommu_group;
94
95 } vlib_pci_device_info_t;
96
97 typedef u32 vlib_pci_dev_handle_t;
98
99 vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_main_t *vm,
100                                                   vlib_pci_addr_t *addr,
101                                                   clib_error_t **error);
102 clib_error_t *vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr,
103                                             vlib_pci_addr_t *root_bus);
104 vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
105 vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm,
106                                     vlib_pci_dev_handle_t h);
107 u32 vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h);
108 u32 vlib_pci_get_num_msix_interrupts (vlib_main_t * vm,
109                                       vlib_pci_dev_handle_t h);
110
111 uword vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h);
112 void vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
113                                 uword private_data);
114
115 static inline void
116 vlib_pci_free_device_info (vlib_pci_device_info_t * di)
117 {
118   if (!di)
119     return;
120   vec_free (di->product_name);
121   vec_free (di->vpd_r);
122   vec_free (di->vpd_w);
123   vec_free (di->driver_name);
124   clib_mem_free (di);
125 }
126
127 typedef struct
128 {
129   u16 vendor_id, device_id;
130 } pci_device_id_t;
131
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,
136                                             u16 line);
137
138 typedef struct _pci_device_registration
139 {
140   /* Driver init function. */
141   clib_error_t *(*init_function) (vlib_main_t * vm,
142                                   vlib_pci_dev_handle_t handle);
143
144   /* Interrupt handler */
145   pci_intx_handler_function_t *interrupt_handler;
146
147   /* List of registrations */
148   struct _pci_device_registration *next_registration;
149
150   /* Vendor/device ids supported by this driver. */
151   pci_device_id_t supported_devices[];
152 } pci_device_registration_t;
153
154 /* Pool of PCI devices. */
155 typedef struct
156 {
157   vlib_main_t *vlib_main;
158   pci_device_registration_t *pci_device_registrations;
159   /* logging */
160   vlib_log_class_t log_default;
161 } vlib_pci_main_t;
162
163 extern vlib_pci_main_t pci_main;
164
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)       \
170 {                                                               \
171     vlib_pci_main_t * pm = &pci_main;                           \
172     x.next_registration = pm->pci_device_registrations;         \
173     pm->pci_device_registrations = &x;                          \
174 }                                                               \
175 static void __vlib_rm_pci_device_registration_##x (void)        \
176     __attribute__((__destructor__)) ;                           \
177 static void __vlib_rm_pci_device_registration_##x (void)        \
178 {                                                               \
179     vlib_pci_main_t * pm = &pci_main;                           \
180     VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
181                                   &x, next_registration);       \
182 }                                                               \
183 __VA_ARGS__ pci_device_registration_t x
184
185 clib_error_t *vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
186                                     char *uio_driver_name);
187
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,
193                                           u32 n_bytes);
194
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);
200
201
202 #define _(t, x)                                                         \
203 static inline clib_error_t *                                            \
204 vlib_pci_read_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h,      \
205                           uword address, t * data)                      \
206 {                                                                       \
207   return vlib_pci_read_write_##x (vm, h, VLIB_READ,address, data,       \
208                                      sizeof (data[0]));                 \
209 }
210
211 _(u32, config);
212 _(u16, config);
213 _(u8, config);
214
215 _(u32, io);
216 _(u16, io);
217 _(u8, io);
218
219 #undef _
220
221 #define _(t, x)                                                         \
222 static inline clib_error_t *                                            \
223 vlib_pci_write_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h,     \
224                            uword address, t * data)                     \
225 {                                                                       \
226   return vlib_pci_read_write_##x (vm, h, VLIB_WRITE,                    \
227                                    address, data, sizeof (data[0]));    \
228 }
229
230 _(u32, config);
231 _(u16, config);
232 _(u8, config);
233
234 _(u32, io);
235 _(u16, io);
236 _(u8, io);
237
238 #undef _
239
240 static inline clib_error_t *
241 vlib_pci_intr_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
242 {
243   u16 command;
244   clib_error_t *err;
245
246   err = vlib_pci_read_config_u16 (vm, h, 4, &command);
247
248   if (err)
249     return err;
250
251   command &= ~PCI_COMMAND_INTX_DISABLE;
252
253   return vlib_pci_write_config_u16 (vm, h, 4, &command);
254 }
255
256 static inline clib_error_t *
257 vlib_pci_intr_disable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
258 {
259   u16 command;
260   clib_error_t *err;
261
262   err = vlib_pci_read_config_u16 (vm, h, 4, &command);
263
264   if (err)
265     return err;
266
267   command |= PCI_COMMAND_INTX_DISABLE;
268
269   return vlib_pci_write_config_u16 (vm, h, 4, &command);
270 }
271
272 static inline clib_error_t *
273 vlib_pci_bus_master_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
274 {
275   clib_error_t *err;
276   u16 command;
277
278   /* Set bus master enable (BME) */
279   err = vlib_pci_read_config_u16 (vm, h, 4, &command);
280
281   if (err)
282     return err;
283
284   if (command & PCI_COMMAND_BUS_MASTER)
285     return 0;
286
287   command |= PCI_COMMAND_BUS_MASTER;
288
289   return vlib_pci_write_config_u16 (vm, h, 4, &command);
290 }
291
292 clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
293                                     pci_device_id_t ids[],
294                                     vlib_pci_dev_handle_t * handle);
295 void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h);
296 clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
297                                    u32 resource, void **result);
298 clib_error_t *vlib_pci_map_region_fixed (vlib_main_t * vm,
299                                          vlib_pci_dev_handle_t h,
300                                          u32 resource, u8 * addr,
301                                          void **result);
302 clib_error_t *vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
303                                   u32 resource);
304 clib_error_t *vlib_pci_register_intx_handler (vlib_main_t * vm,
305                                               vlib_pci_dev_handle_t h,
306                                               pci_intx_handler_function_t *
307                                               intx_handler);
308 clib_error_t *vlib_pci_register_msix_handler (vlib_main_t * vm,
309                                               vlib_pci_dev_handle_t h,
310                                               u32 start, u32 count,
311                                               pci_msix_handler_function_t *
312                                               msix_handler);
313 clib_error_t *vlib_pci_enable_msix_irq (vlib_main_t * vm,
314                                         vlib_pci_dev_handle_t h, u16 start,
315                                         u16 count);
316 clib_error_t *vlib_pci_disable_msix_irq (vlib_main_t * vm,
317                                          vlib_pci_dev_handle_t h, u16 start,
318                                          u16 count);
319 clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h,
320                                 void *ptr);
321 uword vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
322                                     u16 index);
323
324 int vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm,
325                                         vlib_pci_dev_handle_t h);
326
327 unformat_function_t unformat_vlib_pci_addr;
328 format_function_t format_vlib_pci_addr;
329 format_function_t format_vlib_pci_link_speed;
330 format_function_t format_vlib_pci_link_port;
331 format_function_t format_vlib_pci_vpd;
332
333 #endif /* included_vlib_pci_h */
334
335 /*
336  * fd.io coding-style-patch-verification: ON
337  *
338  * Local Variables:
339  * eval: (c-set-style "gnu")
340  * End:
341  */