blob: 1c70cc7bb8093e55ecde1889f0eb46b05ba522fb [file] [log] [blame]
Damjan Mariona42cd342016-04-13 18:03:20 +02001/*
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
Damjan Marioncef87f12017-10-05 15:32:41 +020046/* *INDENT-OFF* */
Dave Barach9b8ffd92016-07-08 08:13:45 -040047typedef CLIB_PACKED (union
Dave Barach9b8ffd92016-07-08 08:13:45 -040048{
Damjan Marioncef87f12017-10-05 15:32:41 +020049 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* */
Damjan Mariona42cd342016-04-13 18:03:20 +020059
Damjan Marioncef87f12017-10-05 15:32:41 +020060typedef struct vlib_pci_device_info
61{
Damjan Marion20ba1642018-03-26 15:35:33 +020062 u32 flags;
63#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU (1 << 0);
64
Damjan Marioncef87f12017-10-05 15:32:41 +020065 /* addr */
66 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +020067
68 /* Numa Node */
69 int numa_node;
70
Damjan Marionb1b1a142016-08-23 00:53:22 +020071 /* Device data */
72 u16 device_class;
73 u16 vendor_id;
74 u16 device_id;
75
Damjan Marion5a206ea2016-05-12 22:11:03 +020076 /* Vital Product Data */
Dave Barach9b8ffd92016-07-08 08:13:45 -040077 u8 *product_name;
78 u8 *vpd_r;
79 u8 *vpd_w;
Damjan Marion5a206ea2016-05-12 22:11:03 +020080
Damjan Marioncef87f12017-10-05 15:32:41 +020081 /* Driver name */
82 u8 *driver_name;
Damjan Marion5a206ea2016-05-12 22:11:03 +020083
Damjan Marioncef87f12017-10-05 15:32:41 +020084 /* First 64 bytes of configuration space. */
85 union
86 {
87 pci_config_type0_regs_t config0;
88 pci_config_type1_regs_t config1;
89 u8 config_data[256];
90 };
91
92 /* IOMMU Group */
93 int iommu_group;
94
95} vlib_pci_device_info_t;
96
97typedef u32 vlib_pci_dev_handle_t;
98
Damjan Marion23227982018-10-22 13:38:57 +020099vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_main_t * vm,
100 vlib_pci_addr_t * addr,
Damjan Marioncef87f12017-10-05 15:32:41 +0200101 clib_error_t ** error);
102vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
Damjan Marion23227982018-10-22 13:38:57 +0200103vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm,
104 vlib_pci_dev_handle_t h);
105uword vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h);
106void vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
107 uword private_data);
Damjan Marioncef87f12017-10-05 15:32:41 +0200108
109static inline void
110vlib_pci_free_device_info (vlib_pci_device_info_t * di)
111{
112 if (!di)
113 return;
114 vec_free (di->product_name);
115 vec_free (di->vpd_r);
116 vec_free (di->vpd_w);
117 vec_free (di->driver_name);
118 clib_mem_free (di);
119}
Damjan Mariona42cd342016-04-13 18:03:20 +0200120
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121typedef struct
122{
Damjan Mariona42cd342016-04-13 18:03:20 +0200123 u16 vendor_id, device_id;
124} pci_device_id_t;
125
Damjan Marion23227982018-10-22 13:38:57 +0200126typedef void (pci_intx_handler_function_t) (vlib_main_t * vm,
127 vlib_pci_dev_handle_t handle);
128typedef void (pci_msix_handler_function_t) (vlib_main_t * vm,
129 vlib_pci_dev_handle_t handle,
Damjan Marion599a16b2018-03-04 19:35:23 +0100130 u16 line);
131
Dave Barach9b8ffd92016-07-08 08:13:45 -0400132typedef struct _pci_device_registration
133{
Damjan Mariona42cd342016-04-13 18:03:20 +0200134 /* Driver init function. */
Damjan Marioncef87f12017-10-05 15:32:41 +0200135 clib_error_t *(*init_function) (vlib_main_t * vm,
136 vlib_pci_dev_handle_t handle);
Damjan Mariona42cd342016-04-13 18:03:20 +0200137
Damjan Marion5a206ea2016-05-12 22:11:03 +0200138 /* Interrupt handler */
Damjan Marion599a16b2018-03-04 19:35:23 +0100139 pci_intx_handler_function_t *interrupt_handler;
Damjan Mariona42cd342016-04-13 18:03:20 +0200140
141 /* List of registrations */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400142 struct _pci_device_registration *next_registration;
Damjan Mariona42cd342016-04-13 18:03:20 +0200143
144 /* Vendor/device ids supported by this driver. */
145 pci_device_id_t supported_devices[];
146} pci_device_registration_t;
147
Damjan Marion5a206ea2016-05-12 22:11:03 +0200148/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149typedef struct
150{
151 vlib_main_t *vlib_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 pci_device_registration_t *pci_device_registrations;
Damjan Marion23227982018-10-22 13:38:57 +0200153 /* logging */
154 vlib_log_class_t log_default;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200155} vlib_pci_main_t;
156
157extern vlib_pci_main_t pci_main;
158
Damjan Mariona42cd342016-04-13 18:03:20 +0200159#define PCI_REGISTER_DEVICE(x,...) \
160 __VA_ARGS__ pci_device_registration_t x; \
161static void __vlib_add_pci_device_registration_##x (void) \
162 __attribute__((__constructor__)) ; \
163static void __vlib_add_pci_device_registration_##x (void) \
164{ \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200165 vlib_pci_main_t * pm = &pci_main; \
166 x.next_registration = pm->pci_device_registrations; \
167 pm->pci_device_registrations = &x; \
Damjan Mariona42cd342016-04-13 18:03:20 +0200168} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200169static void __vlib_rm_pci_device_registration_##x (void) \
170 __attribute__((__destructor__)) ; \
171static void __vlib_rm_pci_device_registration_##x (void) \
172{ \
173 vlib_pci_main_t * pm = &pci_main; \
174 VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
175 &x, next_registration); \
176} \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200177__VA_ARGS__ pci_device_registration_t x
Damjan Mariona42cd342016-04-13 18:03:20 +0200178
Damjan Marion23227982018-10-22 13:38:57 +0200179clib_error_t *vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180 char *uio_driver_name);
Damjan Mariona42cd342016-04-13 18:03:20 +0200181
182/* Configuration space read/write. */
Damjan Marion23227982018-10-22 13:38:57 +0200183clib_error_t *vlib_pci_read_write_config (vlib_main_t * vm,
184 vlib_pci_dev_handle_t handle,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400185 vlib_read_or_write_t read_or_write,
Damjan Marion23227982018-10-22 13:38:57 +0200186 uword address, void *data,
187 u32 n_bytes);
Damjan Mariona42cd342016-04-13 18:03:20 +0200188
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200189/* io space read/write. */
190clib_error_t *vlib_pci_read_write_io (vlib_main_t * vm,
191 vlib_pci_dev_handle_t handle,
192 vlib_read_or_write_t read_or_write,
193 uword address, void *data, u32 n_bytes);
194
195
196#define _(t, x) \
Damjan Mariona42cd342016-04-13 18:03:20 +0200197static inline clib_error_t * \
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200198vlib_pci_read_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h, \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200199 uword address, t * data) \
Damjan Mariona42cd342016-04-13 18:03:20 +0200200{ \
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200201 return vlib_pci_read_write_##x (vm, h, VLIB_READ,address, data, \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200202 sizeof (data[0])); \
Damjan Mariona42cd342016-04-13 18:03:20 +0200203}
204
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200205_(u32, config);
206_(u16, config);
207_(u8, config);
208
209_(u32, io);
210_(u16, io);
211_(u8, io);
Damjan Mariona42cd342016-04-13 18:03:20 +0200212
213#undef _
214
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200215#define _(t, x) \
Damjan Mariona42cd342016-04-13 18:03:20 +0200216static inline clib_error_t * \
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200217vlib_pci_write_##x##_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h, \
Damjan Marion23227982018-10-22 13:38:57 +0200218 uword address, t * data) \
Damjan Mariona42cd342016-04-13 18:03:20 +0200219{ \
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200220 return vlib_pci_read_write_##x (vm, h, VLIB_WRITE, \
Damjan Mariona42cd342016-04-13 18:03:20 +0200221 address, data, sizeof (data[0])); \
222}
223
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200224_(u32, config);
225_(u16, config);
226_(u8, config);
227
228_(u32, io);
229_(u16, io);
230_(u8, io);
Damjan Mariona42cd342016-04-13 18:03:20 +0200231
232#undef _
233
Damjan Marion5a206ea2016-05-12 22:11:03 +0200234static inline clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200235vlib_pci_intr_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200236{
237 u16 command;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400238 clib_error_t *err;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200239
Damjan Marion23227982018-10-22 13:38:57 +0200240 err = vlib_pci_read_config_u16 (vm, h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200241
242 if (err)
243 return err;
244
245 command &= ~PCI_COMMAND_INTX_DISABLE;
246
Damjan Marion23227982018-10-22 13:38:57 +0200247 return vlib_pci_write_config_u16 (vm, h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200248}
249
250static inline clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200251vlib_pci_intr_disable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200252{
253 u16 command;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254 clib_error_t *err;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200255
Damjan Marion23227982018-10-22 13:38:57 +0200256 err = vlib_pci_read_config_u16 (vm, h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200257
258 if (err)
259 return err;
260
261 command |= PCI_COMMAND_INTX_DISABLE;
262
Damjan Marion23227982018-10-22 13:38:57 +0200263 return vlib_pci_write_config_u16 (vm, h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200264}
265
266static inline clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200267vlib_pci_bus_master_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200268{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 clib_error_t *err;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200270 u16 command;
271
272 /* Set bus master enable (BME) */
Damjan Marion23227982018-10-22 13:38:57 +0200273 err = vlib_pci_read_config_u16 (vm, h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200274
275 if (err)
276 return err;
277
Damjan Marion191a1a42017-07-10 15:38:21 +0200278 if (command & PCI_COMMAND_BUS_MASTER)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200279 return 0;
280
281 command |= PCI_COMMAND_BUS_MASTER;
282
Damjan Marion23227982018-10-22 13:38:57 +0200283 return vlib_pci_write_config_u16 (vm, h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200284}
Damjan Mariona42cd342016-04-13 18:03:20 +0200285
Damjan Marion23227982018-10-22 13:38:57 +0200286clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +0100287 pci_device_id_t ids[],
288 vlib_pci_dev_handle_t * handle);
Damjan Marion23227982018-10-22 13:38:57 +0200289void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h);
290clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
291 u32 resource, void **result);
292clib_error_t *vlib_pci_map_region_fixed (vlib_main_t * vm,
293 vlib_pci_dev_handle_t h,
Damjan Marion2060db82018-03-04 17:37:15 +0100294 u32 resource, u8 * addr,
295 void **result);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200296clib_error_t *vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
297 u32 resource);
Damjan Marion23227982018-10-22 13:38:57 +0200298clib_error_t *vlib_pci_register_intx_handler (vlib_main_t * vm,
299 vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100300 pci_intx_handler_function_t *
301 intx_handler);
Damjan Marion23227982018-10-22 13:38:57 +0200302clib_error_t *vlib_pci_register_msix_handler (vlib_main_t * vm,
303 vlib_pci_dev_handle_t h,
Damjan Marion599a16b2018-03-04 19:35:23 +0100304 u32 start, u32 count,
305 pci_msix_handler_function_t *
306 msix_handler);
Damjan Marion23227982018-10-22 13:38:57 +0200307clib_error_t *vlib_pci_enable_msix_irq (vlib_main_t * vm,
308 vlib_pci_dev_handle_t h, u16 start,
Damjan Marion599a16b2018-03-04 19:35:23 +0100309 u16 count);
Damjan Marion23227982018-10-22 13:38:57 +0200310clib_error_t *vlib_pci_disable_msix_irq (vlib_main_t * vm,
311 vlib_pci_dev_handle_t h, u16 start,
Damjan Marion599a16b2018-03-04 19:35:23 +0100312 u16 count);
Damjan Marion68b4da62018-09-30 18:26:20 +0200313clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h,
314 void *ptr);
315
316int vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm,
317 vlib_pci_dev_handle_t h);
Damjan Marion599a16b2018-03-04 19:35:23 +0100318
Damjan Marion5a206ea2016-05-12 22:11:03 +0200319unformat_function_t unformat_vlib_pci_addr;
320format_function_t format_vlib_pci_addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200321format_function_t format_vlib_pci_link_speed;
Damjan Marion7d221612016-12-26 11:39:42 +0100322format_function_t format_vlib_pci_vpd;
Damjan Mariona42cd342016-04-13 18:03:20 +0200323
324#endif /* included_vlib_pci_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400325
326/*
327 * fd.io coding-style-patch-verification: ON
328 *
329 * Local Variables:
330 * eval: (c-set-style "gnu")
331 * End:
332 */