FD.io VPP  v16.06
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 typedef CLIB_PACKED (union {
47  struct {
48  u16 domain;
49  u8 bus;
50  u8 slot:5;
51  u8 function:3;
52  };
53  u32 as_u32;
54 }) vlib_pci_addr_t;
55 
56 typedef struct {
57  /* Operating system handle for this device. */
59 
60  vlib_pci_addr_t bus_address;
61 
62  /* First 64 bytes of configuration space. */
63  union {
66  u8 config_data[256];
67  };
69 
70 typedef struct {
71  u16 vendor_id, device_id;
73 
74 typedef struct _pci_device_registration {
75  /* Driver init function. */
76  clib_error_t * (* init_function) (vlib_main_t * vm, vlib_pci_device_t * dev);
77 
78  char const *kernel_driver;
79  u8 kernel_driver_running;
80 
81  /* List of registrations */
82  struct _pci_device_registration * next_registration;
83 
84  /* Vendor/device ids supported by this driver. */
85  pci_device_id_t supported_devices[];
87 
88 #define PCI_REGISTER_DEVICE(x,...) \
89  __VA_ARGS__ pci_device_registration_t x; \
90 static void __vlib_add_pci_device_registration_##x (void) \
91  __attribute__((__constructor__)) ; \
92 static void __vlib_add_pci_device_registration_##x (void) \
93 { \
94  linux_pci_main_t * lpm = vlib_unix_get_main(); \
95  x.next_registration = lpm->pci_device_registrations; \
96  lpm->pci_device_registrations = &x; \
97 } \
98 __VA_ARGS__ pci_device_registration_t x
99 
100 
101 /* Configuration space read/write. */
102 clib_error_t *
104  vlib_read_or_write_t read_or_write,
105  uword address,
106  void * data,
107  u32 n_bytes);
108 
109 #define _(t) \
110 static inline clib_error_t * \
111 os_read_pci_config_##t (uword os_handle, uword address, t * data) \
112 { \
113  return os_read_write_pci_config (os_handle, VLIB_READ, \
114  address, data, sizeof (data[0])); \
115 }
116 
117 _ (u32);
118 _ (u16);
119 _ (u8);
120 
121 #undef _
122 
123 #define _(t) \
124 static inline clib_error_t * \
125 os_write_pci_config_##t (uword os_handle, uword address, t * data) \
126 { \
127  return os_read_write_pci_config (os_handle, VLIB_WRITE, \
128  address, data, sizeof (data[0])); \
129 }
130 
131 _ (u32);
132 _ (u16);
133 _ (u8);
134 
135 #undef _
136 
137 clib_error_t *
138 os_map_pci_resource (uword os_handle, u32 resource, void ** result);
139 
140 clib_error_t *
141 os_map_pci_resource_fixed (uword os_handle, u32 resource, u8 * addr,
142  void ** result);
143 
144 /* Free's device. */
145 void os_free_pci_device (uword os_handle);
146 
147 void os_add_pci_disable_interrupts_reg (uword os_handle, u32 resource, u32 reg_offset, u32 reg_value);
148 
150 
151 static inline uword
152 unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
153 {
154  vlib_pci_addr_t * addr = va_arg (* args, vlib_pci_addr_t *);
155  u32 x[4];
156 
157  if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
158  return 0;
159 
160  addr->domain = x[0];
161  addr->bus = x[1];
162  addr->slot = x[2];
163  addr->function = x[3];
164 
165  return 1;
166 }
167 
168 static inline u8 *
169 format_vlib_pci_addr (u8 * s, va_list * va)
170 {
171  vlib_pci_addr_t * addr = va_arg (* va, vlib_pci_addr_t *);
172  return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
173  addr->slot, addr->function);
174 }
175 
176 #endif /* included_vlib_pci_h */
format_function_t format_os_pci_handle
Definition: pci.h:149
void os_add_pci_disable_interrupts_reg(uword os_handle, u32 resource, u32 reg_offset, u32 reg_value)
Definition: pci.c:206
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
static u8 * format_vlib_pci_addr(u8 *s, va_list *va)
Definition: pci.h:169
vlib_read_or_write_t
Definition: defs.h:53
clib_error_t * os_map_pci_resource_fixed(uword os_handle, u32 resource, u8 *addr, void **result)
Definition: pci.c:394
vlib_pci_addr_t bus_address
Definition: pci.h:60
typedef CLIB_PACKED(union{struct{u16 domain;u8 bus;u8 slot:5;u8 function:3;};u32 as_u32;}) vlib_pci_addr_t
clib_error_t * os_read_write_pci_config(uword os_handle, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: pci.c:298
void os_free_pci_device(uword os_handle)
Definition: pci.c:402
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
pci_config_type1_regs_t config1
Definition: pci.h:65
static uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
Definition: pci.h:152
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u64 uword
Definition: types.h:112
pci_config_type0_regs_t config0
Definition: pci.h:64
uword os_handle
Definition: pci.h:58
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
struct _pci_device_registration pci_device_registration_t
vhost_vring_addr_t addr
Definition: vhost-user.h:78
struct _unformat_input_t unformat_input_t
clib_error_t * os_map_pci_resource(uword os_handle, u32 resource, void **result)
Definition: pci.c:385
u16 vendor_id
Definition: pci.h:71