Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 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 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 46 | /* *INDENT-OFF* */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 47 | typedef CLIB_PACKED (union |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 48 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 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* */ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 59 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 60 | typedef struct vlib_pci_device_info |
| 61 | { |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 62 | u32 flags; |
| 63 | #define VLIB_PCI_DEVICE_INFO_F_NOIOMMU (1 << 0); |
| 64 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 65 | /* addr */ |
| 66 | vlib_pci_addr_t addr; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 67 | |
| 68 | /* Numa Node */ |
| 69 | int numa_node; |
| 70 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 71 | /* Device data */ |
| 72 | u16 device_class; |
| 73 | u16 vendor_id; |
| 74 | u16 device_id; |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 75 | u8 revision; |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 76 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 77 | /* Vital Product Data */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 78 | u8 *product_name; |
| 79 | u8 *vpd_r; |
| 80 | u8 *vpd_w; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 81 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 82 | /* Driver name */ |
| 83 | u8 *driver_name; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 84 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 85 | /* First 64 bytes of configuration space. */ |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 86 | vlib_pci_config_t config; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 87 | |
| 88 | /* IOMMU Group */ |
| 89 | int iommu_group; |
| 90 | |
| 91 | } vlib_pci_device_info_t; |
| 92 | |
| 93 | typedef u32 vlib_pci_dev_handle_t; |
| 94 | |
Ray Kinsella | 5714a49 | 2021-11-02 13:33:44 +0000 | [diff] [blame] | 95 | vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_main_t *vm, |
| 96 | vlib_pci_addr_t *addr, |
| 97 | clib_error_t **error); |
| 98 | clib_error_t *vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, |
| 99 | vlib_pci_addr_t *root_bus); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 100 | vlib_pci_addr_t *vlib_pci_get_all_dev_addrs (); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 101 | vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm, |
| 102 | vlib_pci_dev_handle_t h); |
Damjan Marion | d2bfb78 | 2019-01-07 20:56:04 +0100 | [diff] [blame] | 103 | u32 vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h); |
Mohsin Kazmi | a402b83 | 2019-01-25 15:57:24 +0000 | [diff] [blame] | 104 | u32 vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, |
| 105 | vlib_pci_dev_handle_t h); |
| 106 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 107 | uword vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h); |
| 108 | void vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 109 | uword private_data); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 110 | |
| 111 | static inline void |
| 112 | vlib_pci_free_device_info (vlib_pci_device_info_t * di) |
| 113 | { |
| 114 | if (!di) |
| 115 | return; |
| 116 | vec_free (di->product_name); |
| 117 | vec_free (di->vpd_r); |
| 118 | vec_free (di->vpd_w); |
| 119 | vec_free (di->driver_name); |
| 120 | clib_mem_free (di); |
| 121 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 122 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 123 | typedef struct |
| 124 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 125 | u16 vendor_id, device_id; |
| 126 | } pci_device_id_t; |
| 127 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 128 | #define PCI_DEVICE_IDS(...) \ |
| 129 | (pci_device_id_t[]) \ |
| 130 | { \ |
| 131 | __VA_ARGS__, {} \ |
| 132 | } |
| 133 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 134 | typedef void (pci_intx_handler_function_t) (vlib_main_t * vm, |
| 135 | vlib_pci_dev_handle_t handle); |
| 136 | typedef void (pci_msix_handler_function_t) (vlib_main_t * vm, |
| 137 | vlib_pci_dev_handle_t handle, |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 138 | u16 line); |
| 139 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 140 | typedef struct _pci_device_registration |
| 141 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 142 | /* Driver init function. */ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 143 | clib_error_t *(*init_function) (vlib_main_t * vm, |
| 144 | vlib_pci_dev_handle_t handle); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 145 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 146 | /* Interrupt handler */ |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 147 | pci_intx_handler_function_t *interrupt_handler; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 148 | |
| 149 | /* List of registrations */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 150 | struct _pci_device_registration *next_registration; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 151 | |
| 152 | /* Vendor/device ids supported by this driver. */ |
| 153 | pci_device_id_t supported_devices[]; |
| 154 | } pci_device_registration_t; |
| 155 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 156 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 157 | typedef struct |
| 158 | { |
| 159 | vlib_main_t *vlib_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 160 | pci_device_registration_t *pci_device_registrations; |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 161 | /* logging */ |
| 162 | vlib_log_class_t log_default; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 163 | } vlib_pci_main_t; |
| 164 | |
| 165 | extern vlib_pci_main_t pci_main; |
| 166 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 167 | #define PCI_REGISTER_DEVICE(x,...) \ |
| 168 | __VA_ARGS__ pci_device_registration_t x; \ |
| 169 | static void __vlib_add_pci_device_registration_##x (void) \ |
| 170 | __attribute__((__constructor__)) ; \ |
| 171 | static void __vlib_add_pci_device_registration_##x (void) \ |
| 172 | { \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 173 | vlib_pci_main_t * pm = &pci_main; \ |
| 174 | x.next_registration = pm->pci_device_registrations; \ |
| 175 | pm->pci_device_registrations = &x; \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 176 | } \ |
Damjan Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 177 | static void __vlib_rm_pci_device_registration_##x (void) \ |
| 178 | __attribute__((__destructor__)) ; \ |
| 179 | static void __vlib_rm_pci_device_registration_##x (void) \ |
| 180 | { \ |
| 181 | vlib_pci_main_t * pm = &pci_main; \ |
| 182 | VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \ |
| 183 | &x, next_registration); \ |
| 184 | } \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 185 | __VA_ARGS__ pci_device_registration_t x |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 186 | |
BenoƮt Ganne | 6a07348 | 2022-10-13 17:22:26 +0200 | [diff] [blame] | 187 | clib_error_t *vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr, |
| 188 | char *uio_driver_name, int force); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 189 | |
| 190 | /* Configuration space read/write. */ |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 191 | clib_error_t *vlib_pci_read_write_config (vlib_main_t * vm, |
| 192 | vlib_pci_dev_handle_t handle, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 193 | vlib_read_or_write_t read_or_write, |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 194 | uword address, void *data, |
| 195 | u32 n_bytes); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 196 | |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 197 | /* io space read/write. */ |
| 198 | clib_error_t *vlib_pci_read_write_io (vlib_main_t * vm, |
| 199 | vlib_pci_dev_handle_t handle, |
| 200 | vlib_read_or_write_t read_or_write, |
| 201 | uword address, void *data, u32 n_bytes); |
| 202 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 203 | #define _(t, x) \ |
| 204 | static inline clib_error_t *vlib_pci_read_##x##_##t ( \ |
| 205 | vlib_main_t *vm, vlib_pci_dev_handle_t h, uword address, t *data) \ |
| 206 | { \ |
| 207 | return vlib_pci_read_write_##x (vm, h, VLIB_READ, address, data, \ |
| 208 | sizeof (data[0])); \ |
| 209 | } \ |
| 210 | static inline clib_error_t *vlib_pci_write_##x##_##t ( \ |
| 211 | vlib_main_t *vm, vlib_pci_dev_handle_t h, uword address, t *data) \ |
| 212 | { \ |
| 213 | return vlib_pci_read_write_##x (vm, h, VLIB_WRITE, address, data, \ |
| 214 | sizeof (data[0])); \ |
| 215 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 216 | |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 217 | _(u32, config); |
| 218 | _(u16, config); |
| 219 | _(u8, config); |
| 220 | |
| 221 | _(u32, io); |
| 222 | _(u16, io); |
| 223 | _(u8, io); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 224 | |
| 225 | #undef _ |
| 226 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 227 | clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr, |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 228 | pci_device_id_t ids[], |
| 229 | vlib_pci_dev_handle_t * handle); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 230 | void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h); |
| 231 | clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 232 | u32 resource, void **result); |
| 233 | clib_error_t *vlib_pci_map_region_fixed (vlib_main_t * vm, |
| 234 | vlib_pci_dev_handle_t h, |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 235 | u32 resource, u8 * addr, |
| 236 | void **result); |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 237 | clib_error_t *vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 238 | u32 resource); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 239 | clib_error_t *vlib_pci_register_intx_handler (vlib_main_t * vm, |
| 240 | vlib_pci_dev_handle_t h, |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 241 | pci_intx_handler_function_t * |
| 242 | intx_handler); |
Damjan Marion | 38c6191 | 2023-10-17 16:06:26 +0000 | [diff] [blame^] | 243 | clib_error_t *vlib_pci_unregister_intx_handler (vlib_main_t *vm, |
| 244 | vlib_pci_dev_handle_t h); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 245 | clib_error_t *vlib_pci_register_msix_handler (vlib_main_t * vm, |
| 246 | vlib_pci_dev_handle_t h, |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 247 | u32 start, u32 count, |
| 248 | pci_msix_handler_function_t * |
| 249 | msix_handler); |
Damjan Marion | 38c6191 | 2023-10-17 16:06:26 +0000 | [diff] [blame^] | 250 | clib_error_t *vlib_pci_unregister_msix_handler (vlib_main_t *vm, |
| 251 | vlib_pci_dev_handle_t h, |
| 252 | u32 start, u32 count); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 253 | clib_error_t *vlib_pci_enable_msix_irq (vlib_main_t * vm, |
| 254 | vlib_pci_dev_handle_t h, u16 start, |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 255 | u16 count); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 256 | clib_error_t *vlib_pci_disable_msix_irq (vlib_main_t * vm, |
| 257 | vlib_pci_dev_handle_t h, u16 start, |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 258 | u16 count); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 259 | clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 260 | void *ptr); |
Damjan Marion | 9c9490c | 2020-10-07 20:00:39 +0200 | [diff] [blame] | 261 | uword vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 262 | u16 index); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 263 | |
| 264 | int vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, |
| 265 | vlib_pci_dev_handle_t h); |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 266 | clib_error_t *vlib_pci_intr_enable (vlib_main_t *, vlib_pci_dev_handle_t); |
| 267 | clib_error_t *vlib_pci_intr_disable (vlib_main_t *, vlib_pci_dev_handle_t); |
| 268 | clib_error_t *vlib_pci_bus_master_enable (vlib_main_t *, |
| 269 | vlib_pci_dev_handle_t); |
| 270 | clib_error_t *vlib_pci_bus_master_disable (vlib_main_t *, |
| 271 | vlib_pci_dev_handle_t); |
| 272 | clib_error_t *vlib_pci_function_level_reset (vlib_main_t *, |
| 273 | vlib_pci_dev_handle_t); |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 274 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 275 | unformat_function_t unformat_vlib_pci_addr; |
| 276 | format_function_t format_vlib_pci_addr; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 277 | format_function_t format_vlib_pci_link_speed; |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 278 | format_function_t format_vlib_pci_link_speed_cap; |
Ray Kinsella | 6efe025 | 2021-11-02 13:26:49 +0000 | [diff] [blame] | 279 | format_function_t format_vlib_pci_link_port; |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 280 | format_function_t format_vlib_pci_vpd; |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 281 | format_function_t format_vlib_pci_log; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 282 | |
| 283 | #endif /* included_vlib_pci_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * fd.io coding-style-patch-verification: ON |
| 287 | * |
| 288 | * Local Variables: |
| 289 | * eval: (c-set-style "gnu") |
| 290 | * End: |
| 291 | */ |