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 | { |
| 62 | /* addr */ |
| 63 | vlib_pci_addr_t addr; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 64 | |
| 65 | /* Numa Node */ |
| 66 | int numa_node; |
| 67 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 68 | /* Device data */ |
| 69 | u16 device_class; |
| 70 | u16 vendor_id; |
| 71 | u16 device_id; |
| 72 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 73 | /* Vital Product Data */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 74 | u8 *product_name; |
| 75 | u8 *vpd_r; |
| 76 | u8 *vpd_w; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 77 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 78 | /* Driver name */ |
| 79 | u8 *driver_name; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 80 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 81 | /* First 64 bytes of configuration space. */ |
| 82 | union |
| 83 | { |
| 84 | pci_config_type0_regs_t config0; |
| 85 | pci_config_type1_regs_t config1; |
| 86 | u8 config_data[256]; |
| 87 | }; |
| 88 | |
| 89 | /* IOMMU Group */ |
| 90 | int iommu_group; |
| 91 | |
| 92 | } vlib_pci_device_info_t; |
| 93 | |
| 94 | typedef u32 vlib_pci_dev_handle_t; |
| 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 |
| 104 | vlib_pci_free_device_info (vlib_pci_device_info_t * di) |
| 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 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 114 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 115 | typedef struct |
| 116 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 117 | u16 vendor_id, device_id; |
| 118 | } pci_device_id_t; |
| 119 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 120 | typedef void (pci_intx_handler_function_t) (vlib_pci_dev_handle_t handle); |
| 121 | typedef void (pci_msix_handler_function_t) (vlib_pci_dev_handle_t handle, |
| 122 | u16 line); |
| 123 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 124 | typedef struct _pci_device_registration |
| 125 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 126 | /* Driver init function. */ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 127 | clib_error_t *(*init_function) (vlib_main_t * vm, |
| 128 | vlib_pci_dev_handle_t handle); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 129 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 130 | /* Interrupt handler */ |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 131 | pci_intx_handler_function_t *interrupt_handler; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 132 | |
| 133 | /* List of registrations */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 134 | struct _pci_device_registration *next_registration; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 135 | |
| 136 | /* Vendor/device ids supported by this driver. */ |
| 137 | pci_device_id_t supported_devices[]; |
| 138 | } pci_device_registration_t; |
| 139 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 140 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 141 | typedef struct |
| 142 | { |
| 143 | vlib_main_t *vlib_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 144 | pci_device_registration_t *pci_device_registrations; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 145 | } vlib_pci_main_t; |
| 146 | |
| 147 | extern vlib_pci_main_t pci_main; |
| 148 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 149 | #define PCI_REGISTER_DEVICE(x,...) \ |
| 150 | __VA_ARGS__ pci_device_registration_t x; \ |
| 151 | static void __vlib_add_pci_device_registration_##x (void) \ |
| 152 | __attribute__((__constructor__)) ; \ |
| 153 | static void __vlib_add_pci_device_registration_##x (void) \ |
| 154 | { \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 155 | vlib_pci_main_t * pm = &pci_main; \ |
| 156 | x.next_registration = pm->pci_device_registrations; \ |
| 157 | pm->pci_device_registrations = &x; \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 158 | } \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 159 | __VA_ARGS__ pci_device_registration_t x |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 160 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 161 | clib_error_t *vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 162 | char *uio_driver_name); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 163 | |
| 164 | /* Configuration space read/write. */ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 165 | clib_error_t *vlib_pci_read_write_config (vlib_pci_dev_handle_t handle, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 166 | vlib_read_or_write_t read_or_write, |
| 167 | uword address, |
| 168 | void *data, u32 n_bytes); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 169 | |
| 170 | #define _(t) \ |
| 171 | static inline clib_error_t * \ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 172 | vlib_pci_read_config_##t (vlib_pci_dev_handle_t h, \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 173 | uword address, t * data) \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 174 | { \ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 175 | return vlib_pci_read_write_config (h, VLIB_READ,address, data, \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 176 | sizeof (data[0])); \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 179 | _(u32); |
| 180 | _(u16); |
| 181 | _(u8); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 182 | |
| 183 | #undef _ |
| 184 | |
| 185 | #define _(t) \ |
| 186 | static inline clib_error_t * \ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 187 | vlib_pci_write_config_##t (vlib_pci_dev_handle_t h, uword address, \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 188 | t * data) \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 189 | { \ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 190 | return vlib_pci_read_write_config (h, VLIB_WRITE, \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 191 | address, data, sizeof (data[0])); \ |
| 192 | } |
| 193 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 194 | _(u32); |
| 195 | _(u16); |
| 196 | _(u8); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 197 | |
| 198 | #undef _ |
| 199 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 200 | static inline clib_error_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 201 | vlib_pci_intr_enable (vlib_pci_dev_handle_t h) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 202 | { |
| 203 | u16 command; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 204 | clib_error_t *err; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 205 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 206 | err = vlib_pci_read_config_u16 (h, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 207 | |
| 208 | if (err) |
| 209 | return err; |
| 210 | |
| 211 | command &= ~PCI_COMMAND_INTX_DISABLE; |
| 212 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 213 | return vlib_pci_write_config_u16 (h, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static inline clib_error_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 217 | vlib_pci_intr_disable (vlib_pci_dev_handle_t h) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 218 | { |
| 219 | u16 command; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 220 | clib_error_t *err; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 221 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 222 | err = vlib_pci_read_config_u16 (h, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 223 | |
| 224 | if (err) |
| 225 | return err; |
| 226 | |
| 227 | command |= PCI_COMMAND_INTX_DISABLE; |
| 228 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 229 | return vlib_pci_write_config_u16 (h, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static inline clib_error_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 233 | vlib_pci_bus_master_enable (vlib_pci_dev_handle_t h) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 234 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 235 | clib_error_t *err; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 236 | u16 command; |
| 237 | |
| 238 | /* Set bus master enable (BME) */ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 239 | err = vlib_pci_read_config_u16 (h, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 240 | |
| 241 | if (err) |
| 242 | return err; |
| 243 | |
Damjan Marion | 191a1a4 | 2017-07-10 15:38:21 +0200 | [diff] [blame] | 244 | if (command & PCI_COMMAND_BUS_MASTER) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 245 | return 0; |
| 246 | |
| 247 | command |= PCI_COMMAND_BUS_MASTER; |
| 248 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 249 | return vlib_pci_write_config_u16 (h, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 250 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 251 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 252 | clib_error_t *vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource, |
| 253 | void **result); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 254 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 255 | clib_error_t *vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, |
| 256 | u32 resource, u8 * addr, |
| 257 | void **result); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 258 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 259 | clib_error_t *vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h, |
| 260 | u32 start, u32 count, |
| 261 | pci_msix_handler_function_t * |
| 262 | msix_handler); |
| 263 | clib_error_t *vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start, |
| 264 | u16 count); |
| 265 | clib_error_t *vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start, |
| 266 | u16 count); |
| 267 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 268 | unformat_function_t unformat_vlib_pci_addr; |
| 269 | format_function_t format_vlib_pci_addr; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 270 | format_function_t format_vlib_pci_link_speed; |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 271 | format_function_t format_vlib_pci_vpd; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 272 | |
| 273 | #endif /* included_vlib_pci_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * fd.io coding-style-patch-verification: ON |
| 277 | * |
| 278 | * Local Variables: |
| 279 | * eval: (c-set-style "gnu") |
| 280 | * End: |
| 281 | */ |