FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
pci.h
Go to the documentation of this file.
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 {
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 */
75 
76  /* Vital Product Data */
80 
81  /* Driver name */
83 
84  /* First 64 bytes of configuration space. */
85  union
86  {
90  };
91 
92  /* IOMMU Group */
94 
96 
98 
100  vlib_pci_addr_t * addr,
101  clib_error_t ** error);
102 vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
103 vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm,
104  vlib_pci_dev_handle_t h);
105 u32 vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h);
106 uword vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h);
107 void vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
108  uword private_data);
109 
110 static inline void
112 {
113  if (!di)
114  return;
115  vec_free (di->product_name);
116  vec_free (di->vpd_r);
117  vec_free (di->vpd_w);
118  vec_free (di->driver_name);
119  clib_mem_free (di);
120 }
121 
122 typedef struct
123 {
126 
128  vlib_pci_dev_handle_t handle);
130  vlib_pci_dev_handle_t handle,
131  u16 line);
132 
133 typedef struct _pci_device_registration
134 {
135  /* Driver init function. */
136  clib_error_t *(*init_function) (vlib_main_t * vm,
137  vlib_pci_dev_handle_t handle);
138 
139  /* Interrupt handler */
140  pci_intx_handler_function_t *interrupt_handler;
141 
142  /* List of registrations */
143  struct _pci_device_registration *next_registration;
144 
145  /* Vendor/device ids supported by this driver. */
146  pci_device_id_t supported_devices[];
148 
149 /* Pool of PCI devices. */
150 typedef struct
151 {
154  /* logging */
157 
159 
160 #define PCI_REGISTER_DEVICE(x,...) \
161  __VA_ARGS__ pci_device_registration_t x; \
162 static void __vlib_add_pci_device_registration_##x (void) \
163  __attribute__((__constructor__)) ; \
164 static void __vlib_add_pci_device_registration_##x (void) \
165 { \
166  vlib_pci_main_t * pm = &pci_main; \
167  x.next_registration = pm->pci_device_registrations; \
168  pm->pci_device_registrations = &x; \
169 } \
170 static void __vlib_rm_pci_device_registration_##x (void) \
171  __attribute__((__destructor__)) ; \
172 static void __vlib_rm_pci_device_registration_##x (void) \
173 { \
174  vlib_pci_main_t * pm = &pci_main; \
175  VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
176  &x, next_registration); \
177 } \
178 __VA_ARGS__ pci_device_registration_t x
179 
180 clib_error_t *vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
181  char *uio_driver_name);
182 
183 /* Configuration space read/write. */
185  vlib_pci_dev_handle_t handle,
186  vlib_read_or_write_t read_or_write,
187  uword address, void *data,
188  u32 n_bytes);
189 
190 /* io space read/write. */
192  vlib_pci_dev_handle_t handle,
193  vlib_read_or_write_t read_or_write,
194  uword address, void *data, u32 n_bytes);
195 
196 
197 #define _(t, x) \
198 static inline clib_error_t * \
199 vlib_pci_read_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h, \
200  uword address, t * data) \
201 { \
202  return vlib_pci_read_write_##x (vm, h, VLIB_READ,address, data, \
203  sizeof (data[0])); \
204 }
205 
206 _(u32, config);
207 _(u16, config);
208 _(u8, config);
209 
210 _(u32, io);
211 _(u16, io);
212 _(u8, io);
213 
214 #undef _
215 
216 #define _(t, x) \
217 static inline clib_error_t * \
218 vlib_pci_write_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h, \
219  uword address, t * data) \
220 { \
221  return vlib_pci_read_write_##x (vm, h, VLIB_WRITE, \
222  address, data, sizeof (data[0])); \
223 }
224 
225 _(u32, config);
226 _(u16, config);
227 _(u8, config);
228 
229 _(u32, io);
230 _(u16, io);
231 _(u8, io);
232 
233 #undef _
234 
235 static inline clib_error_t *
236 vlib_pci_intr_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
237 {
238  u16 command;
239  clib_error_t *err;
240 
241  err = vlib_pci_read_config_u16 (vm, h, 4, &command);
242 
243  if (err)
244  return err;
245 
246  command &= ~PCI_COMMAND_INTX_DISABLE;
247 
248  return vlib_pci_write_config_u16 (vm, h, 4, &command);
249 }
250 
251 static inline clib_error_t *
252 vlib_pci_intr_disable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
253 {
254  u16 command;
255  clib_error_t *err;
256 
257  err = vlib_pci_read_config_u16 (vm, h, 4, &command);
258 
259  if (err)
260  return err;
261 
262  command |= PCI_COMMAND_INTX_DISABLE;
263 
264  return vlib_pci_write_config_u16 (vm, h, 4, &command);
265 }
266 
267 static inline clib_error_t *
268 vlib_pci_bus_master_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
269 {
270  clib_error_t *err;
271  u16 command;
272 
273  /* Set bus master enable (BME) */
274  err = vlib_pci_read_config_u16 (vm, h, 4, &command);
275 
276  if (err)
277  return err;
278 
279  if (command & PCI_COMMAND_BUS_MASTER)
280  return 0;
281 
282  command |= PCI_COMMAND_BUS_MASTER;
283 
284  return vlib_pci_write_config_u16 (vm, h, 4, &command);
285 }
286 
287 clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
288  pci_device_id_t ids[],
289  vlib_pci_dev_handle_t * handle);
290 void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h);
291 clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
292  u32 resource, void **result);
294  vlib_pci_dev_handle_t h,
295  u32 resource, u8 * addr,
296  void **result);
297 clib_error_t *vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
298  u32 resource);
300  vlib_pci_dev_handle_t h,
302  intx_handler);
304  vlib_pci_dev_handle_t h,
305  u32 start, u32 count,
307  msix_handler);
309  vlib_pci_dev_handle_t h, u16 start,
310  u16 count);
312  vlib_pci_dev_handle_t h, u16 start,
313  u16 count);
314 clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h,
315  void *ptr);
316 
318  vlib_pci_dev_handle_t h);
319 
324 
325 #endif /* included_vlib_pci_h */
326 
327 /*
328  * fd.io coding-style-patch-verification: ON
329  *
330  * Local Variables:
331  * eval: (c-set-style "gnu")
332  * End:
333  */
format_function_t format_vlib_pci_addr
Definition: pci.h:321
typedef address
Definition: ip_types.api:30
u32 vlib_log_class_t
Definition: log.h:21
int vlib_pci_supports_virtual_addr_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1189
static clib_error_t * vlib_pci_bus_master_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:268
pci_device_registration_t * pci_device_registrations
Definition: pci.h:153
vlib_read_or_write_t
Definition: defs.h:54
clib_error_t * vlib_pci_read_write_io(vlib_main_t *vm, vlib_pci_dev_handle_t handle, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: pci.c:1158
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
Definition: pci.c:1409
static clib_error_t * vlib_pci_intr_disable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:252
vlib_pci_main_t pci_main
Definition: pci.c:53
unformat_function_t unformat_vlib_pci_addr
Definition: pci.h:320
unsigned char u8
Definition: types.h:56
vlib_pci_addr_t * vlib_pci_get_addr(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:163
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
clib_error_t * vlib_pci_map_region_fixed(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
Definition: pci.c:1135
#define PCI_COMMAND_INTX_DISABLE
Definition: pci_config.h:191
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_main_t *vm, vlib_pci_addr_t *addr, clib_error_t **error)
Definition: pci.c:184
void di(unformat_input_t *i)
Definition: unformat.c:163
struct _pci_device_registration pci_device_registration_t
unsigned int u32
Definition: types.h:88
pci_config_type1_regs_t config1
Definition: pci.h:88
clib_error_t * vlib_pci_map_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h, void *ptr)
Definition: pci.c:1178
u8 config_data[256]
Definition: pci.h:89
unsigned short u16
Definition: types.h:57
u32 vlib_pci_dev_handle_t
Definition: pci.h:97
void() pci_intx_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle)
Definition: pci.h:127
clib_error_t * vlib_pci_io_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource)
Definition: pci.c:1142
clib_error_t * vlib_pci_read_write_config(vlib_main_t *vm, vlib_pci_dev_handle_t handle, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: pci.c:999
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:232
void vlib_pci_device_close(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1256
vlib_main_t * vm
Definition: buffer.c:301
format_function_t format_vlib_pci_link_speed
Definition: pci.h:322
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
clib_error_t * vlib_pci_register_msix_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 start, u32 count, pci_msix_handler_function_t *msix_handler)
Definition: pci.c:808
u8 * driver_name
Definition: pci.h:82
uword vlib_pci_get_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:148
clib_error_t * vlib_pci_map_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, void **result)
Definition: pci.c:1128
pci_config_type0_regs_t config0
Definition: pci.h:87
static void clib_mem_free(void *p)
Definition: mem.h:205
vlib_pci_addr_t addr
Definition: pci.h:66
void() pci_msix_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle, u16 line)
Definition: pci.h:129
clib_error_t * vlib_pci_disable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
Definition: pci.c:887
clib_error_t * vlib_pci_register_intx_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, pci_intx_handler_function_t *intx_handler)
Definition: pci.c:757
clib_error_t * vlib_pci_enable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
Definition: pci.c:865
u64 uword
Definition: types.h:112
typedef CLIB_PACKED(union { struct { u16 domain;u8 bus;u8 slot:5;u8 function:3;};u32 as_u32;}) vlib_pci_addr_t
u32 vlib_pci_get_numa_node(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:170
clib_error_t * vlib_pci_device_open(vlib_main_t *vm, vlib_pci_addr_t *addr, pci_device_id_t ids[], vlib_pci_dev_handle_t *handle)
Definition: pci.c:1197
format_function_t format_vlib_pci_vpd
Definition: pci.h:323
void vlib_pci_set_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h, uword private_data)
Definition: pci.c:155
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
Definition: pci.h:111
static clib_error_t * vlib_pci_intr_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:236
vlib_log_class_t log_default
Definition: pci.h:155
clib_error_t * vlib_pci_bind_to_uio(vlib_main_t *vm, vlib_pci_addr_t *addr, char *uio_driver_name)
Definition: pci.c:368
u16 device_class
Definition: pci.h:72
vlib_main_t * vlib_main
Definition: pci.h:152
u8 * product_name
Definition: pci.h:77
u16 vendor_id
Definition: pci.h:124
struct vlib_pci_device_info vlib_pci_device_info_t
#define PCI_COMMAND_BUS_MASTER
Definition: pci_config.h:183