FD.io VPP  v21.06-3-gbb25fbf28
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,
108 
111  uword private_data);
112 
113 static inline void
115 {
116  if (!di)
117  return;
118  vec_free (di->product_name);
119  vec_free (di->vpd_r);
120  vec_free (di->vpd_w);
121  vec_free (di->driver_name);
122  clib_mem_free (di);
123 }
124 
125 typedef struct
126 {
127  u16 vendor_id, device_id;
129 
131  vlib_pci_dev_handle_t handle);
133  vlib_pci_dev_handle_t handle,
134  u16 line);
135 
136 typedef struct _pci_device_registration
137 {
138  /* Driver init function. */
139  clib_error_t *(*init_function) (vlib_main_t * vm,
140  vlib_pci_dev_handle_t handle);
141 
142  /* Interrupt handler */
143  pci_intx_handler_function_t *interrupt_handler;
144 
145  /* List of registrations */
146  struct _pci_device_registration *next_registration;
147 
148  /* Vendor/device ids supported by this driver. */
149  pci_device_id_t supported_devices[];
151 
152 /* Pool of PCI devices. */
153 typedef struct
154 {
157  /* logging */
160 
162 
163 #define PCI_REGISTER_DEVICE(x,...) \
164  __VA_ARGS__ pci_device_registration_t x; \
165 static void __vlib_add_pci_device_registration_##x (void) \
166  __attribute__((__constructor__)) ; \
167 static void __vlib_add_pci_device_registration_##x (void) \
168 { \
169  vlib_pci_main_t * pm = &pci_main; \
170  x.next_registration = pm->pci_device_registrations; \
171  pm->pci_device_registrations = &x; \
172 } \
173 static void __vlib_rm_pci_device_registration_##x (void) \
174  __attribute__((__destructor__)) ; \
175 static void __vlib_rm_pci_device_registration_##x (void) \
176 { \
177  vlib_pci_main_t * pm = &pci_main; \
178  VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
179  &x, next_registration); \
180 } \
181 __VA_ARGS__ pci_device_registration_t x
182 
183 clib_error_t *vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
184  char *uio_driver_name);
185 
186 /* Configuration space read/write. */
188  vlib_pci_dev_handle_t handle,
189  vlib_read_or_write_t read_or_write,
190  uword address, void *data,
191  u32 n_bytes);
192 
193 /* io space read/write. */
195  vlib_pci_dev_handle_t handle,
196  vlib_read_or_write_t read_or_write,
197  uword address, void *data, u32 n_bytes);
198 
199 
200 #define _(t, x) \
201 static inline clib_error_t * \
202 vlib_pci_read_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h, \
203  uword address, t * data) \
204 { \
205  return vlib_pci_read_write_##x (vm, h, VLIB_READ,address, data, \
206  sizeof (data[0])); \
207 }
208 
209 _(u32, config);
210 _(u16, config);
211 _(u8, config);
212 
213 _(u32, io);
214 _(u16, io);
215 _(u8, io);
216 
217 #undef _
218 
219 #define _(t, x) \
220 static inline clib_error_t * \
221 vlib_pci_write_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h, \
222  uword address, t * data) \
223 { \
224  return vlib_pci_read_write_##x (vm, h, VLIB_WRITE, \
225  address, data, sizeof (data[0])); \
226 }
227 
228 _(u32, config);
229 _(u16, config);
230 _(u8, config);
231 
232 _(u32, io);
233 _(u16, io);
234 _(u8, io);
235 
236 #undef _
237 
238 static inline clib_error_t *
240 {
241  u16 command;
242  clib_error_t *err;
243 
244  err = vlib_pci_read_config_u16 (vm, h, 4, &command);
245 
246  if (err)
247  return err;
248 
249  command &= ~PCI_COMMAND_INTX_DISABLE;
250 
251  return vlib_pci_write_config_u16 (vm, h, 4, &command);
252 }
253 
254 static inline clib_error_t *
256 {
257  u16 command;
258  clib_error_t *err;
259 
260  err = vlib_pci_read_config_u16 (vm, h, 4, &command);
261 
262  if (err)
263  return err;
264 
265  command |= PCI_COMMAND_INTX_DISABLE;
266 
267  return vlib_pci_write_config_u16 (vm, h, 4, &command);
268 }
269 
270 static inline clib_error_t *
272 {
273  clib_error_t *err;
274  u16 command;
275 
276  /* Set bus master enable (BME) */
277  err = vlib_pci_read_config_u16 (vm, h, 4, &command);
278 
279  if (err)
280  return err;
281 
282  if (command & PCI_COMMAND_BUS_MASTER)
283  return 0;
284 
285  command |= PCI_COMMAND_BUS_MASTER;
286 
287  return vlib_pci_write_config_u16 (vm, h, 4, &command);
288 }
289 
290 clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
291  pci_device_id_t ids[],
292  vlib_pci_dev_handle_t * handle);
295  u32 resource, void **result);
298  u32 resource, u8 * addr,
299  void **result);
301  u32 resource);
305  intx_handler);
308  u32 start, u32 count,
310  msix_handler);
312  vlib_pci_dev_handle_t h, u16 start,
313  u16 count);
315  vlib_pci_dev_handle_t h, u16 start,
316  u16 count);
318  void *ptr);
320  u16 index);
321 
324 
329 
330 #endif /* included_vlib_pci_h */
331 
332 /*
333  * fd.io coding-style-patch-verification: ON
334  *
335  * Local Variables:
336  * eval: (c-set-style "gnu")
337  * End:
338  */
vlib.h
vlib_pci_device_info::numa_node
int numa_node
Definition: pci.h:69
vlib_pci_register_msix_handler
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:838
vlib_pci_device_info::config1
pci_config_type1_regs_t config1
Definition: pci.h:88
vlib_pci_device_open
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:1251
pci_config_type1_regs_t
Definition: pci_config.h:296
vlib_pci_device_info::flags
u32 flags
Definition: pci.h:62
vlib_pci_get_numa_node
u32 vlib_pci_get_numa_node(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:170
vlib_log_class_t
u32 vlib_log_class_t
Definition: vlib.h:52
vlib_pci_get_private_data
uword vlib_pci_get_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:148
format_vlib_pci_vpd
format_function_t format_vlib_pci_vpd
Definition: pci.h:328
clib_mem_free
static void clib_mem_free(void *p)
Definition: mem.h:311
vlib_pci_main_t::pci_device_registrations
pci_device_registration_t * pci_device_registrations
Definition: pci.h:156
vlib_pci_main_t::log_default
vlib_log_class_t log_default
Definition: pci.h:158
vlib_pci_supports_virtual_addr_dma
int vlib_pci_supports_virtual_addr_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1243
vlib_pci_read_write_config
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:1034
vlib_pci_map_dma
clib_error_t * vlib_pci_map_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h, void *ptr)
Definition: pci.c:1232
vlib_pci_dev_handle_t
u32 vlib_pci_dev_handle_t
Definition: pci.h:97
unformat_vlib_pci_addr
unformat_function_t unformat_vlib_pci_addr
Definition: pci.h:325
vlib_pci_get_addr
vlib_pci_addr_t * vlib_pci_get_addr(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:163
u16
unsigned short u16
Definition: types.h:57
vlib_pci_get_msix_file_index
uword vlib_pci_get_msix_file_index(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 index)
Definition: pci.c:917
vlib_pci_device_info::vpd_r
u8 * vpd_r
Definition: pci.h:78
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vlib_pci_disable_msix_irq
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:928
vlib_pci_get_num_msix_interrupts
u32 vlib_pci_get_num_msix_interrupts(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:177
domain
description no domain
Definition: map.api:415
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
vlib_pci_get_device_info
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:202
pci_device_id_t::vendor_id
u16 vendor_id
Definition: pci.h:127
h
h
Definition: flowhash_template.h:372
error
Definition: cJSON.c:88
pci_device_registration_t
struct _pci_device_registration pci_device_registration_t
vlib_pci_intr_enable
static clib_error_t * vlib_pci_intr_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:239
vlib_pci_bind_to_uio
clib_error_t * vlib_pci_bind_to_uio(vlib_main_t *vm, vlib_pci_addr_t *addr, char *uio_driver_name)
Definition: pci.c:397
vlib_pci_map_region_fixed
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:1189
vlib_pci_device_info
Definition: pci.h:60
vlib_pci_bus_master_enable
static clib_error_t * vlib_pci_bus_master_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:271
pci_config_type0_regs_t
Definition: pci_config.h:245
pci_intx_handler_function_t
void() pci_intx_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle)
Definition: pci.h:130
count
u8 count
Definition: dhcp.api:208
slot
u8 slot
Definition: pci_types.api:22
CLIB_PACKED
typedef CLIB_PACKED(struct { u8 class;u8 cmd;}) virtio_net_ctrl_hdr_t
vlib_pci_set_private_data
void vlib_pci_set_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h, uword private_data)
Definition: pci.c:155
uword
u64 uword
Definition: types.h:112
pci_config.h
vlib_read_or_write_t
vlib_read_or_write_t
Definition: defs.h:62
pci_main
vlib_pci_main_t pci_main
Definition: pci.c:53
address
manual_print typedef address
Definition: ip_types.api:96
vlib_pci_map_region
clib_error_t * vlib_pci_map_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, void **result)
Definition: pci.c:1182
vlib_pci_enable_msix_irq
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:895
PCI_COMMAND_BUS_MASTER
#define PCI_COMMAND_BUS_MASTER
Definition: pci_config.h:183
data
u8 data[128]
Definition: ipsec_types.api:92
format_function_t
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
vlib_pci_device_close
void vlib_pci_device_close(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1311
PCI_COMMAND_INTX_DISABLE
#define PCI_COMMAND_INTX_DISABLE
Definition: pci_config.h:191
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
index
u32 index
Definition: flow_types.api:221
format_vlib_pci_addr
format_function_t format_vlib_pci_addr
Definition: pci.h:326
vlib_pci_device_info::iommu_group
int iommu_group
Definition: pci.h:93
vlib_pci_device_info::vendor_id
u16 vendor_id
Definition: pci.h:73
u32
unsigned int u32
Definition: types.h:88
vlib_pci_device_info_t
struct vlib_pci_device_info vlib_pci_device_info_t
vlib_pci_main_t::vlib_main
vlib_main_t * vlib_main
Definition: pci.h:155
n_bytes
u32 n_bytes
Definition: interface_output.c:401
unformat_function_t
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:225
vlib_pci_device_info::config0
pci_config_type0_regs_t config0
Definition: pci.h:87
pci_msix_handler_function_t
void() pci_msix_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle, u16 line)
Definition: pci.h:132
vlib_pci_device_info::vpd_w
u8 * vpd_w
Definition: pci.h:79
vlib_main_t
Definition: main.h:102
vlib_pci_device_info::addr
vlib_pci_addr_t addr
Definition: pci.h:66
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
format_vlib_pci_link_speed
format_function_t format_vlib_pci_link_speed
Definition: pci.h:327
vlib_pci_main_t
Definition: pci.h:153
vlib_pci_device_info::device_id
u16 device_id
Definition: pci.h:74
vlib_pci_intr_disable
static clib_error_t * vlib_pci_intr_disable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:255
vlib_pci_io_region
clib_error_t * vlib_pci_io_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource)
Definition: pci.c:1196
vlib_pci_register_intx_handler
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:787
vlib_pci_free_device_info
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
Definition: pci.h:114
vlib_pci_get_all_dev_addrs
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
Definition: pci.c:1466
vlib_pci_device_info::product_name
u8 * product_name
Definition: pci.h:77
di
void di(unformat_input_t *i)
Definition: unformat.c:163
vlib_pci_device_info::device_class
u16 device_class
Definition: pci.h:72
bus
u8 bus
Definition: pci_types.api:21
vlib_pci_read_write_io
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:1212
pci_device_id_t
Definition: pci.h:125
vlib_pci_device_info::config_data
u8 config_data[256]
Definition: pci.h:89
vlib_pci_device_info::driver_name
u8 * driver_name
Definition: pci.h:82