FD.io VPP  v18.01.2-1-g9b554f3
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 {
62  /* addr */
63  vlib_pci_addr_t addr;
64 
65  /* Numa Node */
66  int numa_node;
67 
68  /* Device data */
72 
73  /* Vital Product Data */
77 
78  /* Driver name */
80 
81  /* First 64 bytes of configuration space. */
82  union
83  {
87  };
88 
89  /* IOMMU Group */
91 
93 
95 
96 vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_pci_addr_t * addr,
97  clib_error_t ** error);
98 vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
99 vlib_pci_addr_t *vlib_pci_get_addr (vlib_pci_dev_handle_t h);
100 uword vlib_pci_get_private_data (vlib_pci_dev_handle_t h);
101 void vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data);
102 
103 static inline void
105 {
106  if (!di)
107  return;
108  vec_free (di->product_name);
109  vec_free (di->vpd_r);
110  vec_free (di->vpd_w);
111  vec_free (di->driver_name);
112  clib_mem_free (di);
113 }
114 
115 typedef struct
116 {
119 
120 typedef struct _pci_device_registration
121 {
122  /* Driver init function. */
123  clib_error_t *(*init_function) (vlib_main_t * vm,
124  vlib_pci_dev_handle_t handle);
125 
126  /* Interrupt handler */
127  void (*interrupt_handler) (vlib_pci_dev_handle_t handle);
128 
129  /* List of registrations */
130  struct _pci_device_registration *next_registration;
131 
132  /* Vendor/device ids supported by this driver. */
133  pci_device_id_t supported_devices[];
135 
136 /* Pool of PCI devices. */
137 typedef struct
138 {
142 
144 
145 #define PCI_REGISTER_DEVICE(x,...) \
146  __VA_ARGS__ pci_device_registration_t x; \
147 static void __vlib_add_pci_device_registration_##x (void) \
148  __attribute__((__constructor__)) ; \
149 static void __vlib_add_pci_device_registration_##x (void) \
150 { \
151  vlib_pci_main_t * pm = &pci_main; \
152  x.next_registration = pm->pci_device_registrations; \
153  pm->pci_device_registrations = &x; \
154 } \
155 __VA_ARGS__ pci_device_registration_t x
156 
157 clib_error_t *vlib_pci_bind_to_uio (vlib_pci_addr_t * addr,
158  char *uio_driver_name);
159 
160 /* Configuration space read/write. */
161 clib_error_t *vlib_pci_read_write_config (vlib_pci_dev_handle_t handle,
162  vlib_read_or_write_t read_or_write,
163  uword address,
164  void *data, u32 n_bytes);
165 
166 #define _(t) \
167 static inline clib_error_t * \
168 vlib_pci_read_config_##t (vlib_pci_dev_handle_t h, \
169  uword address, t * data) \
170 { \
171  return vlib_pci_read_write_config (h, VLIB_READ,address, data, \
172  sizeof (data[0])); \
173 }
174 
175 _(u32);
176 _(u16);
177 _(u8);
178 
179 #undef _
180 
181 #define _(t) \
182 static inline clib_error_t * \
183 vlib_pci_write_config_##t (vlib_pci_dev_handle_t h, uword address, \
184  t * data) \
185 { \
186  return vlib_pci_read_write_config (h, VLIB_WRITE, \
187  address, data, sizeof (data[0])); \
188 }
189 
190 _(u32);
191 _(u16);
192 _(u8);
193 
194 #undef _
195 
196 static inline clib_error_t *
197 vlib_pci_intr_enable (vlib_pci_dev_handle_t h)
198 {
199  u16 command;
200  clib_error_t *err;
201 
202  err = vlib_pci_read_config_u16 (h, 4, &command);
203 
204  if (err)
205  return err;
206 
207  command &= ~PCI_COMMAND_INTX_DISABLE;
208 
209  return vlib_pci_write_config_u16 (h, 4, &command);
210 }
211 
212 static inline clib_error_t *
213 vlib_pci_intr_disable (vlib_pci_dev_handle_t h)
214 {
215  u16 command;
216  clib_error_t *err;
217 
218  err = vlib_pci_read_config_u16 (h, 4, &command);
219 
220  if (err)
221  return err;
222 
223  command |= PCI_COMMAND_INTX_DISABLE;
224 
225  return vlib_pci_write_config_u16 (h, 4, &command);
226 }
227 
228 static inline clib_error_t *
229 vlib_pci_bus_master_enable (vlib_pci_dev_handle_t h)
230 {
231  clib_error_t *err;
232  u16 command;
233 
234  /* Set bus master enable (BME) */
235  err = vlib_pci_read_config_u16 (h, 4, &command);
236 
237  if (err)
238  return err;
239 
240  if (command & PCI_COMMAND_BUS_MASTER)
241  return 0;
242 
243  command |= PCI_COMMAND_BUS_MASTER;
244 
245  return vlib_pci_write_config_u16 (h, 4, &command);
246 }
247 
248 clib_error_t *vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource,
249  void **result);
250 
251 clib_error_t *vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
252  u32 resource, u8 * addr,
253  void **result);
254 
259 
260 #endif /* included_vlib_pci_h */
261 
262 /*
263  * fd.io coding-style-patch-verification: ON
264  *
265  * Local Variables:
266  * eval: (c-set-style "gnu")
267  * End:
268  */
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:231
format_function_t format_vlib_pci_addr
Definition: pci.h:256
pci_device_registration_t * pci_device_registrations
Definition: pci.h:140
vlib_read_or_write_t
Definition: defs.h:54
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
Definition: pci.c:1027
vlib_pci_main_t pci_main
Definition: pci.c:53
unformat_function_t unformat_vlib_pci_addr
Definition: pci.h:255
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
#define PCI_COMMAND_INTX_DISABLE
Definition: pci_config.h:191
clib_error_t * vlib_pci_read_write_config(vlib_pci_dev_handle_t handle, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: pci.c:851
void di(unformat_input_t *i)
Definition: unformat.c:163
struct _pci_device_registration pci_device_registration_t
clib_error_t * vlib_pci_bind_to_uio(vlib_pci_addr_t *addr, char *uio_driver_name)
Definition: pci.c:330
static clib_error_t * vlib_pci_intr_disable(vlib_pci_dev_handle_t h)
Definition: pci.h:213
pci_config_type1_regs_t config1
Definition: pci.h:85
u8 config_data[256]
Definition: pci.h:86
u32 vlib_pci_dev_handle_t
Definition: pci.h:94
static clib_error_t * vlib_pci_bus_master_enable(vlib_pci_dev_handle_t h)
Definition: pci.h:229
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_pci_addr_t *addr, clib_error_t **error)
Definition: pci.c:156
vlib_main_t * vm
Definition: buffer.c:283
format_function_t format_vlib_pci_link_speed
Definition: pci.h:257
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
u8 * driver_name
Definition: pci.h:79
uword vlib_pci_get_private_data(vlib_pci_dev_handle_t h)
Definition: pci.c:128
unsigned int u32
Definition: types.h:88
pci_config_type0_regs_t config0
Definition: pci.h:84
static void clib_mem_free(void *p)
Definition: mem.h:179
vlib_pci_addr_t addr
Definition: pci.h:63
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
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 * vlib_pci_map_resource_fixed(vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
Definition: pci.c:933
unsigned char u8
Definition: types.h:56
clib_error_t * vlib_pci_map_resource(vlib_pci_dev_handle_t h, u32 resource, void **result)
Definition: pci.c:927
void vlib_pci_set_private_data(vlib_pci_dev_handle_t h, uword private_data)
Definition: pci.c:135
static clib_error_t * vlib_pci_intr_enable(vlib_pci_dev_handle_t h)
Definition: pci.h:197
vlib_pci_addr_t * vlib_pci_get_addr(vlib_pci_dev_handle_t h)
Definition: pci.c:142
format_function_t format_vlib_pci_vpd
Definition: pci.h:258
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
Definition: pci.h:104
u16 device_class
Definition: pci.h:69
vlib_main_t * vlib_main
Definition: pci.h:139
u8 * product_name
Definition: pci.h:74
u16 vendor_id
Definition: pci.h:117
struct vlib_pci_device_info vlib_pci_device_info_t
#define PCI_COMMAND_BUS_MASTER
Definition: pci_config.h:183