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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 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; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 52 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | typedef struct vlib_pci_device |
| 54 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 55 | /* Operating system handle for this device. */ |
| 56 | uword os_handle; |
| 57 | |
| 58 | vlib_pci_addr_t bus_address; |
| 59 | |
| 60 | /* First 64 bytes of configuration space. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 61 | union |
| 62 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 63 | pci_config_type0_regs_t config0; |
| 64 | pci_config_type1_regs_t config1; |
| 65 | u8 config_data[256]; |
| 66 | }; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 67 | |
| 68 | /* Interrupt handler */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 69 | void (*interrupt_handler) (struct vlib_pci_device * dev); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 70 | |
| 71 | /* Driver name */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 72 | u8 *driver_name; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 73 | |
| 74 | /* Numa Node */ |
| 75 | int numa_node; |
| 76 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 77 | /* Device data */ |
| 78 | u16 device_class; |
| 79 | u16 vendor_id; |
| 80 | u16 device_id; |
| 81 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 82 | /* Vital Product Data */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 83 | u8 *product_name; |
| 84 | u8 *vpd_r; |
| 85 | u8 *vpd_w; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 86 | |
| 87 | /* Private data */ |
| 88 | uword private_data; |
| 89 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 90 | } vlib_pci_device_t; |
| 91 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 92 | typedef struct |
| 93 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 94 | u16 vendor_id, device_id; |
| 95 | } pci_device_id_t; |
| 96 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | typedef struct _pci_device_registration |
| 98 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 99 | /* Driver init function. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 100 | clib_error_t *(*init_function) (vlib_main_t * vm, vlib_pci_device_t * dev); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 101 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 102 | /* Interrupt handler */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 103 | void (*interrupt_handler) (vlib_pci_device_t * dev); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 104 | |
| 105 | /* List of registrations */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 106 | struct _pci_device_registration *next_registration; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 107 | |
| 108 | /* Vendor/device ids supported by this driver. */ |
| 109 | pci_device_id_t supported_devices[]; |
| 110 | } pci_device_registration_t; |
| 111 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 112 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 113 | typedef struct |
| 114 | { |
| 115 | vlib_main_t *vlib_main; |
| 116 | vlib_pci_device_t *pci_devs; |
| 117 | pci_device_registration_t *pci_device_registrations; |
| 118 | uword *pci_dev_index_by_pci_addr; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 119 | } vlib_pci_main_t; |
| 120 | |
| 121 | extern vlib_pci_main_t pci_main; |
| 122 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 123 | #define PCI_REGISTER_DEVICE(x,...) \ |
| 124 | __VA_ARGS__ pci_device_registration_t x; \ |
| 125 | static void __vlib_add_pci_device_registration_##x (void) \ |
| 126 | __attribute__((__constructor__)) ; \ |
| 127 | static void __vlib_add_pci_device_registration_##x (void) \ |
| 128 | { \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 129 | vlib_pci_main_t * pm = &pci_main; \ |
| 130 | x.next_registration = pm->pci_device_registrations; \ |
| 131 | pm->pci_device_registrations = &x; \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 132 | } \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 133 | __VA_ARGS__ pci_device_registration_t x |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 134 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 135 | clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d, |
| 136 | char *uio_driver_name); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 137 | |
| 138 | /* Configuration space read/write. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 139 | clib_error_t *vlib_pci_read_write_config (vlib_pci_device_t * dev, |
| 140 | vlib_read_or_write_t read_or_write, |
| 141 | uword address, |
| 142 | void *data, u32 n_bytes); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 143 | |
| 144 | #define _(t) \ |
| 145 | static inline clib_error_t * \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 146 | vlib_pci_read_config_##t (vlib_pci_device_t * dev, \ |
| 147 | uword address, t * data) \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 148 | { \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 149 | return vlib_pci_read_write_config (dev, VLIB_READ,address, data, \ |
| 150 | sizeof (data[0])); \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 153 | _(u32); |
| 154 | _(u16); |
| 155 | _(u8); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 156 | |
| 157 | #undef _ |
| 158 | |
| 159 | #define _(t) \ |
| 160 | static inline clib_error_t * \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 161 | vlib_pci_write_config_##t (vlib_pci_device_t * dev, uword address, \ |
| 162 | t * data) \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 163 | { \ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 164 | return vlib_pci_read_write_config (dev, VLIB_WRITE, \ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 165 | address, data, sizeof (data[0])); \ |
| 166 | } |
| 167 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 168 | _(u32); |
| 169 | _(u16); |
| 170 | _(u8); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 171 | |
| 172 | #undef _ |
| 173 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 174 | static inline clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 175 | vlib_pci_intr_enable (vlib_pci_device_t * dev) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 176 | { |
| 177 | u16 command; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 178 | clib_error_t *err; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 179 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 180 | err = vlib_pci_read_config_u16 (dev, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 181 | |
| 182 | if (err) |
| 183 | return err; |
| 184 | |
| 185 | command &= ~PCI_COMMAND_INTX_DISABLE; |
| 186 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 187 | return vlib_pci_write_config_u16 (dev, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static inline clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 191 | vlib_pci_intr_disable (vlib_pci_device_t * dev) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 192 | { |
| 193 | u16 command; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 194 | clib_error_t *err; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 195 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 196 | err = vlib_pci_read_config_u16 (dev, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 197 | |
| 198 | if (err) |
| 199 | return err; |
| 200 | |
| 201 | command |= PCI_COMMAND_INTX_DISABLE; |
| 202 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 203 | return vlib_pci_write_config_u16 (dev, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static inline clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 207 | vlib_pci_bus_master_enable (vlib_pci_device_t * dev) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 208 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 209 | clib_error_t *err; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 210 | u16 command; |
| 211 | |
| 212 | /* Set bus master enable (BME) */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 213 | err = vlib_pci_read_config_u16 (dev, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 214 | |
| 215 | if (err) |
| 216 | return err; |
| 217 | |
Damjan Marion | 191a1a4 | 2017-07-10 15:38:21 +0200 | [diff] [blame] | 218 | if (command & PCI_COMMAND_BUS_MASTER) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | |
| 221 | command |= PCI_COMMAND_BUS_MASTER; |
| 222 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | return vlib_pci_write_config_u16 (dev, 4, &command); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 224 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 225 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | clib_error_t *vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, |
| 227 | void **result); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 228 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 229 | clib_error_t *vlib_pci_map_resource_fixed (vlib_pci_device_t * dev, |
| 230 | u32 resource, u8 * addr, |
| 231 | void **result); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 232 | |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 233 | vlib_pci_device_t *vlib_get_pci_device (vlib_pci_addr_t * addr); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 234 | /* Free's device. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 235 | void vlib_pci_free_device (vlib_pci_device_t * dev); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 236 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 237 | unformat_function_t unformat_vlib_pci_addr; |
| 238 | format_function_t format_vlib_pci_addr; |
| 239 | format_function_t format_vlib_pci_handle; |
| 240 | format_function_t format_vlib_pci_link_speed; |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 241 | format_function_t format_vlib_pci_vpd; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 242 | |
| 243 | #endif /* included_vlib_pci_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * fd.io coding-style-patch-verification: ON |
| 247 | * |
| 248 | * Local Variables: |
| 249 | * eval: (c-set-style "gnu") |
| 250 | * End: |
| 251 | */ |