blob: becfa80f37ab838a4add425367c4abb2eb3f78c7 [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
Dave Barach9b8ffd92016-07-08 08:13:45 -040046typedef CLIB_PACKED (union
Dave Barach9b8ffd92016-07-08 08:13:45 -040047{
Damjan Marioncef87f12017-10-05 15:32:41 +020048 struct
49 {
50 u16 domain;
51 u8 bus;
52 u8 slot: 5;
53 u8 function:3;
54 };
55 u32 as_u32;
56}) vlib_pci_addr_t;
Damjan Mariona42cd342016-04-13 18:03:20 +020057
Damjan Marioncef87f12017-10-05 15:32:41 +020058typedef struct vlib_pci_device_info
59{
Damjan Marion20ba1642018-03-26 15:35:33 +020060 u32 flags;
61#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU (1 << 0);
62
Damjan Marioncef87f12017-10-05 15:32:41 +020063 /* addr */
64 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +020065
66 /* Numa Node */
67 int numa_node;
68
Damjan Marionb1b1a142016-08-23 00:53:22 +020069 /* Device data */
70 u16 device_class;
71 u16 vendor_id;
72 u16 device_id;
Damjan Marion00ea98a2023-07-28 13:19:49 +020073 u8 revision;
Damjan Marionb1b1a142016-08-23 00:53:22 +020074
Damjan Marion5a206ea2016-05-12 22:11:03 +020075 /* Vital Product Data */
Dave Barach9b8ffd92016-07-08 08:13:45 -040076 u8 *product_name;
77 u8 *vpd_r;
78 u8 *vpd_w;
Damjan Marion5a206ea2016-05-12 22:11:03 +020079
Damjan Marioncef87f12017-10-05 15:32:41 +020080 /* Driver name */
81 u8 *driver_name;
Damjan Marion5a206ea2016-05-12 22:11:03 +020082
Damjan Marioncef87f12017-10-05 15:32:41 +020083 /* First 64 bytes of configuration space. */
Damjan Marion00ea98a2023-07-28 13:19:49 +020084 vlib_pci_config_t config;
Damjan Marioncef87f12017-10-05 15:32:41 +020085
86 /* IOMMU Group */
87 int iommu_group;
88
89} vlib_pci_device_info_t;
90
91typedef u32 vlib_pci_dev_handle_t;
92
Ray Kinsella5714a492021-11-02 13:33:44 +000093vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_main_t *vm,
94 vlib_pci_addr_t *addr,
95 clib_error_t **error);
96clib_error_t *vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr,
97 vlib_pci_addr_t *root_bus);
Damjan Marioncef87f12017-10-05 15:32:41 +020098vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
Damjan Marion23227982018-10-22 13:38:57 +020099vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm,
100 vlib_pci_dev_handle_t h);
Damjan Mariond2bfb782019-01-07 20:56:04 +0100101u32 vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h);
Mohsin Kazmia402b832019-01-25 15:57:24 +0000102u32 vlib_pci_get_num_msix_interrupts (vlib_main_t * vm,
103 vlib_pci_dev_handle_t h);
104
Damjan Marion23227982018-10-22 13:38:57 +0200105uword 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 Marion00ea98a2023-07-28 13:19:49 +0200126#define PCI_DEVICE_IDS(...) \
127 (pci_device_id_t[]) \
128 { \
129 __VA_ARGS__, {} \
130 }
131
Damjan Marion23227982018-10-22 13:38:57 +0200132typedef void (pci_intx_handler_function_t) (vlib_main_t * vm,
133 vlib_pci_dev_handle_t handle);
134typedef void (pci_msix_handler_function_t) (vlib_main_t * vm,
135 vlib_pci_dev_handle_t handle,
Damjan Marion599a16b2018-03-04 19:35:23 +0100136 u16 line);
137
Dave Barach9b8ffd92016-07-08 08:13:45 -0400138typedef struct _pci_device_registration
139{
Damjan Mariona42cd342016-04-13 18:03:20 +0200140 /* Driver init function. */
Damjan Marioncef87f12017-10-05 15:32:41 +0200141 clib_error_t *(*init_function) (vlib_main_t * vm,
142 vlib_pci_dev_handle_t handle);
Damjan Mariona42cd342016-04-13 18:03:20 +0200143
Damjan Marion5a206ea2016-05-12 22:11:03 +0200144 /* Interrupt handler */
Damjan Marion599a16b2018-03-04 19:35:23 +0100145 pci_intx_handler_function_t *interrupt_handler;
Damjan Mariona42cd342016-04-13 18:03:20 +0200146
147 /* List of registrations */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148 struct _pci_device_registration *next_registration;
Damjan Mariona42cd342016-04-13 18:03:20 +0200149
150 /* Vendor/device ids supported by this driver. */
151 pci_device_id_t supported_devices[];
152} pci_device_registration_t;
153
Damjan Marion5a206ea2016-05-12 22:11:03 +0200154/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400155typedef struct
156{
157 vlib_main_t *vlib_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 pci_device_registration_t *pci_device_registrations;
Damjan Marion23227982018-10-22 13:38:57 +0200159 /* logging */
160 vlib_log_class_t log_default;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200161} vlib_pci_main_t;
162
163extern vlib_pci_main_t pci_main;
164
Damjan Mariona42cd342016-04-13 18:03:20 +0200165#define PCI_REGISTER_DEVICE(x,...) \
166 __VA_ARGS__ pci_device_registration_t x; \
167static void __vlib_add_pci_device_registration_##x (void) \
168 __attribute__((__constructor__)) ; \
169static void __vlib_add_pci_device_registration_##x (void) \
170{ \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200171 vlib_pci_main_t * pm = &pci_main; \
172 x.next_registration = pm->pci_device_registrations; \
173 pm->pci_device_registrations = &x; \
Damjan Mariona42cd342016-04-13 18:03:20 +0200174} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200175static void __vlib_rm_pci_device_registration_##x (void) \
176 __attribute__((__destructor__)) ; \
177static void __vlib_rm_pci_device_registration_##x (void) \
178{ \
179 vlib_pci_main_t * pm = &pci_main; \
180 VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
181 &x, next_registration); \
182} \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200183__VA_ARGS__ pci_device_registration_t x
Damjan Mariona42cd342016-04-13 18:03:20 +0200184
BenoƮt Ganne6a073482022-10-13 17:22:26 +0200185clib_error_t *vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
186 char *uio_driver_name, int force);
Damjan Mariona42cd342016-04-13 18:03:20 +0200187
188/* Configuration space read/write. */
Damjan Marion23227982018-10-22 13:38:57 +0200189clib_error_t *vlib_pci_read_write_config (vlib_main_t * vm,
190 vlib_pci_dev_handle_t handle,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 vlib_read_or_write_t read_or_write,
Damjan Marion23227982018-10-22 13:38:57 +0200192 uword address, void *data,
193 u32 n_bytes);
Damjan Mariona42cd342016-04-13 18:03:20 +0200194
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200195/* io space read/write. */
196clib_error_t *vlib_pci_read_write_io (vlib_main_t * vm,
197 vlib_pci_dev_handle_t handle,
198 vlib_read_or_write_t read_or_write,
199 uword address, void *data, u32 n_bytes);
200
Damjan Marion00ea98a2023-07-28 13:19:49 +0200201#define _(t, x) \
202 static inline clib_error_t *vlib_pci_read_##x##_##t ( \
203 vlib_main_t *vm, vlib_pci_dev_handle_t h, uword address, t *data) \
204 { \
205 return vlib_pci_read_write_##x (vm, h, VLIB_READ, address, data, \
206 sizeof (data[0])); \
207 } \
208 static inline clib_error_t *vlib_pci_write_##x##_##t ( \
209 vlib_main_t *vm, vlib_pci_dev_handle_t h, uword address, t *data) \
210 { \
211 return vlib_pci_read_write_##x (vm, h, VLIB_WRITE, address, data, \
212 sizeof (data[0])); \
213 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200214
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200215_(u32, config);
216_(u16, config);
217_(u8, config);
218
219_(u32, io);
220_(u16, io);
221_(u8, io);
Damjan Mariona42cd342016-04-13 18:03:20 +0200222
223#undef _
224
Damjan Marion23227982018-10-22 13:38:57 +0200225clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +0100226 pci_device_id_t ids[],
227 vlib_pci_dev_handle_t * handle);
Damjan Marion23227982018-10-22 13:38:57 +0200228void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h);
229clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
230 u32 resource, void **result);
231clib_error_t *vlib_pci_map_region_fixed (vlib_main_t * vm,
232 vlib_pci_dev_handle_t h,
Damjan Marion2060db82018-03-04 17:37:15 +0100233 u32 resource, u8 * addr,
234 void **result);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200235clib_error_t *vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
236 u32 resource);
Damjan Marion23227982018-10-22 13:38:57 +0200237clib_error_t *vlib_pci_register_intx_handler (vlib_main_t * vm,
238 vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100239 pci_intx_handler_function_t *
240 intx_handler);
Damjan Marion38c61912023-10-17 16:06:26 +0000241clib_error_t *vlib_pci_unregister_intx_handler (vlib_main_t *vm,
242 vlib_pci_dev_handle_t h);
Damjan Marion23227982018-10-22 13:38:57 +0200243clib_error_t *vlib_pci_register_msix_handler (vlib_main_t * vm,
244 vlib_pci_dev_handle_t h,
Damjan Marion599a16b2018-03-04 19:35:23 +0100245 u32 start, u32 count,
246 pci_msix_handler_function_t *
247 msix_handler);
Damjan Marion38c61912023-10-17 16:06:26 +0000248clib_error_t *vlib_pci_unregister_msix_handler (vlib_main_t *vm,
249 vlib_pci_dev_handle_t h,
250 u32 start, u32 count);
Damjan Marion23227982018-10-22 13:38:57 +0200251clib_error_t *vlib_pci_enable_msix_irq (vlib_main_t * vm,
252 vlib_pci_dev_handle_t h, u16 start,
Damjan Marion599a16b2018-03-04 19:35:23 +0100253 u16 count);
Damjan Marion23227982018-10-22 13:38:57 +0200254clib_error_t *vlib_pci_disable_msix_irq (vlib_main_t * vm,
255 vlib_pci_dev_handle_t h, u16 start,
Damjan Marion599a16b2018-03-04 19:35:23 +0100256 u16 count);
Damjan Marion68b4da62018-09-30 18:26:20 +0200257clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h,
258 void *ptr);
Damjan Marion9c9490c2020-10-07 20:00:39 +0200259uword vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
260 u16 index);
Damjan Marion68b4da62018-09-30 18:26:20 +0200261
262int vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm,
263 vlib_pci_dev_handle_t h);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200264clib_error_t *vlib_pci_intr_enable (vlib_main_t *, vlib_pci_dev_handle_t);
265clib_error_t *vlib_pci_intr_disable (vlib_main_t *, vlib_pci_dev_handle_t);
266clib_error_t *vlib_pci_bus_master_enable (vlib_main_t *,
267 vlib_pci_dev_handle_t);
268clib_error_t *vlib_pci_bus_master_disable (vlib_main_t *,
269 vlib_pci_dev_handle_t);
270clib_error_t *vlib_pci_function_level_reset (vlib_main_t *,
271 vlib_pci_dev_handle_t);
Damjan Marion599a16b2018-03-04 19:35:23 +0100272
Damjan Marion5a206ea2016-05-12 22:11:03 +0200273unformat_function_t unformat_vlib_pci_addr;
274format_function_t format_vlib_pci_addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200275format_function_t format_vlib_pci_link_speed;
Damjan Marion00ea98a2023-07-28 13:19:49 +0200276format_function_t format_vlib_pci_link_speed_cap;
Ray Kinsella6efe0252021-11-02 13:26:49 +0000277format_function_t format_vlib_pci_link_port;
Damjan Marion7d221612016-12-26 11:39:42 +0100278format_function_t format_vlib_pci_vpd;
Damjan Marion00ea98a2023-07-28 13:19:49 +0200279format_function_t format_vlib_pci_log;
Damjan Mariona42cd342016-04-13 18:03:20 +0200280
281#endif /* included_vlib_pci_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400282
283/*
284 * fd.io coding-style-patch-verification: ON
285 *
286 * Local Variables:
287 * eval: (c-set-style "gnu")
288 * End:
289 */