FD.io VPP  v16.09
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  {
48  struct
49  {
50 u16 domain; u8 bus; u8 slot: 5; u8 function:3;};
51  u32 as_u32;}) vlib_pci_addr_t;
52 
53 typedef struct vlib_pci_device
54 {
55  /* Operating system handle for this device. */
57 
58  vlib_pci_addr_t bus_address;
59 
60  /* First 64 bytes of configuration space. */
61  union
62  {
66  };
67 
68  /* Interrupt handler */
69  void (*interrupt_handler) (struct vlib_pci_device * dev);
70 
71  /* Driver name */
73 
74  /* Numa Node */
75  int numa_node;
76 
77  /* Vital Product Data */
81 
82  /* Private data */
84 
86 
87 typedef struct
88 {
89  u16 vendor_id, device_id;
91 
92 typedef struct _pci_device_registration
93 {
94  /* Driver init function. */
95  clib_error_t *(*init_function) (vlib_main_t * vm, vlib_pci_device_t * dev);
96 
97  /* Interrupt handler */
98  void (*interrupt_handler) (vlib_pci_device_t * dev);
99 
100  /* List of registrations */
101  struct _pci_device_registration *next_registration;
102 
103  /* Vendor/device ids supported by this driver. */
104  pci_device_id_t supported_devices[];
106 
107 /* Pool of PCI devices. */
108 typedef struct
109 {
115 
117 
118 #define PCI_REGISTER_DEVICE(x,...) \
119  __VA_ARGS__ pci_device_registration_t x; \
120 static void __vlib_add_pci_device_registration_##x (void) \
121  __attribute__((__constructor__)) ; \
122 static void __vlib_add_pci_device_registration_##x (void) \
123 { \
124  vlib_pci_main_t * pm = &pci_main; \
125  x.next_registration = pm->pci_device_registrations; \
126  pm->pci_device_registrations = &x; \
127 } \
128 __VA_ARGS__ pci_device_registration_t x
129 
131  char *uio_driver_name);
132 
133 /* Configuration space read/write. */
135  vlib_read_or_write_t read_or_write,
136  uword address,
137  void *data, u32 n_bytes);
138 
139 #define _(t) \
140 static inline clib_error_t * \
141 vlib_pci_read_config_##t (vlib_pci_device_t * dev, \
142  uword address, t * data) \
143 { \
144  return vlib_pci_read_write_config (dev, VLIB_READ,address, data, \
145  sizeof (data[0])); \
146 }
147 
148 _(u32);
149 _(u16);
150 _(u8);
151 
152 #undef _
153 
154 #define _(t) \
155 static inline clib_error_t * \
156 vlib_pci_write_config_##t (vlib_pci_device_t * dev, uword address, \
157  t * data) \
158 { \
159  return vlib_pci_read_write_config (dev, VLIB_WRITE, \
160  address, data, sizeof (data[0])); \
161 }
162 
163 _(u32);
164 _(u16);
165 _(u8);
166 
167 #undef _
168 
169 static inline clib_error_t *
171 {
172  u16 command;
173  clib_error_t *err;
174 
175  err = vlib_pci_read_config_u16 (dev, 4, &command);
176 
177  if (err)
178  return err;
179 
180  command &= ~PCI_COMMAND_INTX_DISABLE;
181 
182  return vlib_pci_write_config_u16 (dev, 4, &command);
183 }
184 
185 static inline clib_error_t *
187 {
188  u16 command;
189  clib_error_t *err;
190 
191  err = vlib_pci_read_config_u16 (dev, 4, &command);
192 
193  if (err)
194  return err;
195 
196  command |= PCI_COMMAND_INTX_DISABLE;
197 
198  return vlib_pci_write_config_u16 (dev, 4, &command);
199 }
200 
201 static inline clib_error_t *
203 {
204  clib_error_t *err;
205  u16 command;
206 
207  /* Set bus master enable (BME) */
208  err = vlib_pci_read_config_u16 (dev, 4, &command);
209 
210  if (err)
211  return err;
212 
213  if (!(command & PCI_COMMAND_BUS_MASTER))
214  return 0;
215 
216  command |= PCI_COMMAND_BUS_MASTER;
217 
218  return vlib_pci_write_config_u16 (dev, 4, &command);
219 }
220 
222  void **result);
223 
225  u32 resource, u8 * addr,
226  void **result);
227 
228 /* Free's device. */
230 
235 
236 #endif /* included_vlib_pci_h */
237 
238 /*
239  * fd.io coding-style-patch-verification: ON
240  *
241  * Local Variables:
242  * eval: (c-set-style "gnu")
243  * End:
244  */
u8 * vpd_w
Definition: pci.h:80
format_function_t format_vlib_pci_addr
Definition: pci.h:232
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:231
pci_config_type1_regs_t config1
Definition: pci.h:64
pci_device_registration_t * pci_device_registrations
Definition: pci.h:112
vlib_read_or_write_t
Definition: defs.h:54
vlib_pci_main_t pci_main
Definition: pci.c:53
unformat_function_t unformat_vlib_pci_addr
Definition: pci.h:231
vlib_pci_addr_t bus_address
Definition: pci.h:58
int numa_node
Definition: pci.h:75
u8 * product_name
Definition: pci.h:78
#define PCI_COMMAND_INTX_DISABLE
Definition: pci_config.h:184
uword * pci_dev_index_by_pci_addr
Definition: pci.h:113
struct _pci_device_registration pci_device_registration_t
clib_error_t * vlib_pci_map_resource(vlib_pci_device_t *dev, u32 resource, void **result)
Definition: linux_pci.c:384
vlib_pci_device_t * pci_devs
Definition: pci.h:111
void(* interrupt_handler)(struct vlib_pci_device *dev)
Definition: pci.h:69
static clib_error_t * vlib_pci_intr_enable(vlib_pci_device_t *dev)
Definition: pci.h:170
format_function_t format_vlib_pci_handle
Definition: pci.h:233
uword private_data
Definition: pci.h:83
format_function_t format_vlib_pci_link_speed
Definition: pci.h:234
u8 config_data[256]
Definition: pci.h:65
pci_config_type0_regs_t config0
Definition: pci.h:63
unsigned int u32
Definition: types.h:88
clib_error_t * vlib_pci_map_resource_fixed(vlib_pci_device_t *dev, u32 resource, u8 *addr, void **result)
Definition: linux_pci.c:392
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
void vlib_pci_free_device(vlib_pci_device_t *dev)
Definition: linux_pci.c:400
typedef CLIB_PACKED(union{struct{u16 domain;u8 bus;u8 slot:5;u8 function:3;};u32 as_u32;}) vlib_pci_addr_t
unsigned char u8
Definition: types.h:56
static clib_error_t * vlib_pci_bus_master_enable(vlib_pci_device_t *dev)
Definition: pci.h:202
struct vlib_pci_device vlib_pci_device_t
clib_error_t * vlib_pci_bind_to_uio(vlib_pci_device_t *d, char *uio_driver_name)
Definition: linux_pci.c:95
uword os_handle
Definition: pci.h:56
static clib_error_t * vlib_pci_intr_disable(vlib_pci_device_t *dev)
Definition: pci.h:186
u8 * vpd_r
Definition: pci.h:79
vlib_main_t * vlib_main
Definition: pci.h:110
u8 * driver_name
Definition: pci.h:72
clib_error_t * vlib_pci_read_write_config(vlib_pci_device_t *dev, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: linux_pci.c:305
u16 vendor_id
Definition: pci.h:89
#define PCI_COMMAND_BUS_MASTER
Definition: pci_config.h:176