Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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.c: Linux user space PCI bus management. |
| 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 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 40 | #include <vppinfra/linux/sysfs.h> |
| 41 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #include <vlib/vlib.h> |
| 43 | #include <vlib/pci/pci.h> |
| 44 | #include <vlib/unix/unix.h> |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 45 | #include <vlib/linux/vfio.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <dirent.h> |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 51 | #include <sys/ioctl.h> |
| 52 | #include <net/if.h> |
| 53 | #include <linux/ethtool.h> |
| 54 | #include <linux/sockios.h> |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 55 | #include <linux/vfio.h> |
| 56 | #include <sys/eventfd.h> |
| 57 | |
| 58 | static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices"; |
| 59 | static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers"; |
Damjan Marion | 2752b89 | 2017-12-11 15:55:56 +0100 | [diff] [blame] | 60 | static char *sysfs_mod_vfio_noiommu = |
| 61 | "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 62 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 65 | int fd; |
| 66 | void *addr; |
| 67 | size_t size; |
| 68 | } linux_pci_region_t; |
| 69 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 70 | typedef struct |
| 71 | { |
| 72 | int fd; |
| 73 | u32 clib_file_index; |
| 74 | union |
| 75 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 76 | pci_intx_handler_function_t *intx_handler; |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 77 | pci_msix_handler_function_t *msix_handler; |
| 78 | }; |
| 79 | } linux_pci_irq_t; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 80 | |
| 81 | typedef enum |
| 82 | { |
| 83 | LINUX_PCI_DEVICE_TYPE_UNKNOWN, |
| 84 | LINUX_PCI_DEVICE_TYPE_UIO, |
| 85 | LINUX_PCI_DEVICE_TYPE_VFIO, |
| 86 | } linux_pci_device_type_t; |
| 87 | |
| 88 | typedef struct |
| 89 | { |
| 90 | linux_pci_device_type_t type; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 91 | vlib_pci_dev_handle_t handle; |
| 92 | vlib_pci_addr_t addr; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 93 | |
| 94 | /* Resource file descriptors. */ |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 95 | linux_pci_region_t *regions; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 96 | |
| 97 | /* File descriptor for config space read/write. */ |
| 98 | int config_fd; |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 99 | u64 config_offset; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 100 | |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 101 | /* Device File descriptor */ |
| 102 | int fd; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 103 | |
| 104 | /* Minor device for uio device. */ |
| 105 | u32 uio_minor; |
| 106 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 107 | /* Interrupt handlers */ |
| 108 | linux_pci_irq_t intx_irq; |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 109 | linux_pci_irq_t *msix_irqs; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 110 | |
| 111 | /* private data */ |
| 112 | uword private_data; |
| 113 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 114 | } linux_pci_device_t; |
| 115 | |
| 116 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 117 | typedef struct |
| 118 | { |
| 119 | vlib_main_t *vlib_main; |
| 120 | linux_pci_device_t *linux_pci_devices; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 121 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 122 | } linux_pci_main_t; |
| 123 | |
| 124 | extern linux_pci_main_t linux_pci_main; |
| 125 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 126 | static linux_pci_device_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 127 | linux_pci_get_device (vlib_pci_dev_handle_t h) |
| 128 | { |
| 129 | linux_pci_main_t *lpm = &linux_pci_main; |
| 130 | return pool_elt_at_index (lpm->linux_pci_devices, h); |
| 131 | } |
| 132 | |
| 133 | uword |
| 134 | vlib_pci_get_private_data (vlib_pci_dev_handle_t h) |
| 135 | { |
| 136 | linux_pci_device_t *d = linux_pci_get_device (h); |
| 137 | return d->private_data; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data) |
| 142 | { |
| 143 | linux_pci_device_t *d = linux_pci_get_device (h); |
| 144 | d->private_data = private_data; |
| 145 | } |
| 146 | |
| 147 | vlib_pci_addr_t * |
| 148 | vlib_pci_get_addr (vlib_pci_dev_handle_t h) |
| 149 | { |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 150 | linux_pci_device_t *d = linux_pci_get_device (h); |
| 151 | return &d->addr; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 154 | /* Call to allocate/initialize the pci subsystem. |
| 155 | This is not an init function so that users can explicitly enable |
| 156 | pci only when it's needed. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 157 | clib_error_t *pci_bus_init (vlib_main_t * vm); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 158 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | linux_pci_main_t linux_pci_main; |
| 160 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 161 | vlib_pci_device_info_t * |
| 162 | vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error) |
| 163 | { |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 164 | linux_vfio_main_t *lvm = &vfio_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 165 | clib_error_t *err; |
| 166 | vlib_pci_device_info_t *di; |
| 167 | u8 *f = 0; |
| 168 | u32 tmp; |
| 169 | int fd; |
| 170 | |
| 171 | di = clib_mem_alloc (sizeof (vlib_pci_device_info_t)); |
| 172 | memset (di, 0, sizeof (vlib_pci_device_info_t)); |
| 173 | di->addr.as_u32 = addr->as_u32; |
| 174 | |
| 175 | u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path, |
| 176 | format_vlib_pci_addr, addr); |
| 177 | |
| 178 | f = format (0, "%v/config%c", dev_dir_name, 0); |
| 179 | fd = open ((char *) f, O_RDWR); |
| 180 | |
| 181 | /* Try read-only access if write fails. */ |
| 182 | if (fd < 0) |
| 183 | fd = open ((char *) f, O_RDONLY); |
| 184 | |
| 185 | if (fd < 0) |
| 186 | { |
| 187 | err = clib_error_return_unix (0, "open `%s'", f); |
| 188 | goto error; |
| 189 | } |
| 190 | |
| 191 | /* You can only read more that 64 bytes of config space as root; so we try to |
| 192 | read the full space but fall back to just the first 64 bytes. */ |
Damjan Marion | 44bcc20 | 2018-03-04 16:44:26 +0100 | [diff] [blame] | 193 | if (read (fd, &di->config_data, sizeof (di->config_data)) < |
| 194 | sizeof (di->config0)) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 195 | { |
| 196 | err = clib_error_return_unix (0, "read `%s'", f); |
| 197 | close (fd); |
| 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | { |
| 202 | static pci_config_header_t all_ones; |
| 203 | if (all_ones.vendor_id == 0) |
| 204 | memset (&all_ones, ~0, sizeof (all_ones)); |
| 205 | |
| 206 | if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones))) |
| 207 | { |
| 208 | err = clib_error_return (0, "invalid PCI config for `%s'", f); |
| 209 | close (fd); |
| 210 | goto error; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (di->config0.header.header_type == 0) |
| 215 | pci_config_type0_little_to_host (&di->config0); |
| 216 | else |
| 217 | pci_config_type1_little_to_host (&di->config1); |
| 218 | |
| 219 | di->numa_node = -1; |
| 220 | vec_reset_length (f); |
| 221 | f = format (f, "%v/numa_node%c", dev_dir_name, 0); |
| 222 | err = clib_sysfs_read ((char *) f, "%u", &di->numa_node); |
| 223 | if (err) |
| 224 | { |
| 225 | di->numa_node = -1; |
| 226 | clib_error_free (err); |
| 227 | } |
| 228 | |
| 229 | vec_reset_length (f); |
| 230 | f = format (f, "%v/class%c", dev_dir_name, 0); |
| 231 | err = clib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 232 | if (err) |
| 233 | goto error; |
| 234 | di->device_class = tmp >> 8; |
| 235 | |
| 236 | vec_reset_length (f); |
| 237 | f = format (f, "%v/vendor%c", dev_dir_name, 0); |
| 238 | err = clib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 239 | if (err) |
| 240 | goto error; |
| 241 | di->vendor_id = tmp; |
| 242 | |
| 243 | vec_reset_length (f); |
| 244 | f = format (f, "%v/device%c", dev_dir_name, 0); |
| 245 | err = clib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 246 | if (err) |
| 247 | goto error; |
| 248 | di->device_id = tmp; |
| 249 | |
| 250 | vec_reset_length (f); |
| 251 | f = format (f, "%v/driver%c", dev_dir_name, 0); |
| 252 | di->driver_name = clib_sysfs_link_to_name ((char *) f); |
| 253 | |
| 254 | di->iommu_group = -1; |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 255 | if (lvm->container_fd != -1) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 256 | { |
| 257 | u8 *tmpstr; |
| 258 | vec_reset_length (f); |
| 259 | f = format (f, "%v/iommu_group%c", dev_dir_name, 0); |
| 260 | tmpstr = clib_sysfs_link_to_name ((char *) f); |
| 261 | if (tmpstr) |
| 262 | { |
| 263 | di->iommu_group = atoi ((char *) tmpstr); |
| 264 | vec_free (tmpstr); |
| 265 | } |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 266 | vec_reset_length (f); |
| 267 | f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0); |
| 268 | err = clib_sysfs_read ((char *) f, "%s", &tmpstr); |
| 269 | if (err == 0) |
| 270 | { |
| 271 | if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0) |
| 272 | di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU; |
| 273 | vec_free (tmpstr); |
| 274 | } |
| 275 | else |
| 276 | clib_error_free (err); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Damjan Marion | 9650418 | 2017-12-10 23:54:46 +0100 | [diff] [blame] | 279 | close (fd); |
| 280 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 281 | vec_reset_length (f); |
| 282 | f = format (f, "%v/vpd%c", dev_dir_name, 0); |
| 283 | fd = open ((char *) f, O_RDONLY); |
| 284 | if (fd >= 0) |
| 285 | { |
| 286 | while (1) |
| 287 | { |
| 288 | u8 tag[3]; |
| 289 | u8 *data = 0; |
Damjan Marion | 9650418 | 2017-12-10 23:54:46 +0100 | [diff] [blame] | 290 | uword len; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 291 | |
| 292 | if (read (fd, &tag, 3) != 3) |
| 293 | break; |
| 294 | |
| 295 | if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91) |
| 296 | break; |
| 297 | |
| 298 | len = (tag[2] << 8) | tag[1]; |
| 299 | vec_validate (data, len); |
| 300 | |
| 301 | if (read (fd, data, len) != len) |
| 302 | { |
| 303 | vec_free (data); |
| 304 | break; |
| 305 | } |
| 306 | if (tag[0] == 0x82) |
| 307 | di->product_name = data; |
| 308 | else if (tag[0] == 0x90) |
| 309 | di->vpd_r = data; |
| 310 | else if (tag[0] == 0x91) |
| 311 | di->vpd_w = data; |
| 312 | |
| 313 | data = 0; |
| 314 | } |
| 315 | close (fd); |
| 316 | } |
| 317 | |
| 318 | goto done; |
| 319 | |
| 320 | error: |
| 321 | vlib_pci_free_device_info (di); |
| 322 | di = 0; |
| 323 | |
| 324 | done: |
| 325 | vec_free (f); |
| 326 | vec_free (dev_dir_name); |
| 327 | if (error) |
| 328 | *error = err; |
| 329 | else |
| 330 | clib_error_free (err); |
| 331 | return di; |
| 332 | } |
| 333 | |
Damjan Marion | 2752b89 | 2017-12-11 15:55:56 +0100 | [diff] [blame] | 334 | static int |
| 335 | directory_exists (char *path) |
| 336 | { |
| 337 | struct stat s = { 0 }; |
| 338 | if (stat (path, &s) == -1) |
| 339 | return 0; |
| 340 | |
| 341 | return S_ISDIR (s.st_mode); |
| 342 | } |
| 343 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 344 | clib_error_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 345 | vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 347 | clib_error_t *error = 0; |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 348 | u8 *s = 0, *driver_name = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 349 | DIR *dir = 0; |
| 350 | struct dirent *e; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 351 | vlib_pci_device_info_t *di; |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 352 | int fd, clear_driver_override = 0; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 353 | u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path, |
| 354 | format_vlib_pci_addr, addr); |
| 355 | |
| 356 | di = vlib_pci_get_device_info (addr, &error); |
| 357 | |
| 358 | if (error) |
| 359 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | |
Damjan Marion | 2752b89 | 2017-12-11 15:55:56 +0100 | [diff] [blame] | 361 | if (strncmp ("auto", uio_drv_name, 5) == 0) |
| 362 | { |
| 363 | int vfio_pci_loaded = 0; |
| 364 | |
| 365 | if (directory_exists ("/sys/module/vfio_pci")) |
| 366 | vfio_pci_loaded = 1; |
| 367 | |
| 368 | if (di->iommu_group != -1) |
| 369 | { |
| 370 | /* device is bound to IOMMU group */ |
| 371 | if (!vfio_pci_loaded) |
| 372 | { |
| 373 | error = clib_error_return (0, "Skipping PCI device %U: device " |
| 374 | "is bound to IOMMU group and " |
| 375 | "vfio-pci driver is not loaded", |
| 376 | format_vlib_pci_addr, addr); |
| 377 | goto done; |
| 378 | } |
| 379 | else |
| 380 | uio_drv_name = "vfio-pci"; |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | /* device is not bound to IOMMU group so we have multiple options */ |
| 385 | if (vfio_pci_loaded && |
| 386 | (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0) |
| 387 | uio_drv_name = "vfio-pci"; |
| 388 | else if (directory_exists ("/sys/module/uio_pci_generic")) |
| 389 | uio_drv_name = "uio_pci_generic"; |
| 390 | else if (directory_exists ("/sys/module/igb_uio")) |
| 391 | uio_drv_name = "igb_uio"; |
| 392 | else |
| 393 | { |
| 394 | clib_error_free (error); |
| 395 | error = clib_error_return (0, "Skipping PCI device %U: missing " |
| 396 | "kernel VFIO or UIO driver", |
| 397 | format_vlib_pci_addr, addr); |
| 398 | goto done; |
| 399 | } |
| 400 | clib_error_free (error); |
| 401 | } |
| 402 | } |
| 403 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 404 | s = format (s, "%v/driver%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 405 | driver_name = clib_sysfs_link_to_name ((char *) s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 406 | vec_reset_length (s); |
| 407 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 408 | if (driver_name && |
| 409 | ((strcmp ("vfio-pci", (char *) driver_name) == 0) || |
| 410 | (strcmp ("uio_pci_generic", (char *) driver_name) == 0) || |
| 411 | (strcmp ("igb_uio", (char *) driver_name) == 0))) |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 412 | goto done; |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 413 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 414 | /* walk trough all linux interfaces and if interface belonging to |
| 415 | this device is founf check if interface is admin up */ |
| 416 | dir = opendir ("/sys/class/net"); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 417 | s = format (s, "%U%c", format_vlib_pci_addr, addr, 0); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 418 | |
| 419 | if (!dir) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 421 | error = clib_error_return (0, "Skipping PCI device %U: failed to " |
| 422 | "read /sys/class/net", |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 423 | format_vlib_pci_addr, addr); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 424 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 427 | fd = socket (PF_INET, SOCK_DGRAM, 0); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 428 | if (fd < 0) |
| 429 | { |
| 430 | error = clib_error_return_unix (0, "socket"); |
| 431 | goto done; |
| 432 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 433 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 434 | while ((e = readdir (dir))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 436 | struct ifreq ifr; |
| 437 | struct ethtool_drvinfo drvinfo; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 439 | if (e->d_name[0] == '.') /* skip . and .. */ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 440 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 442 | memset (&ifr, 0, sizeof ifr); |
| 443 | memset (&drvinfo, 0, sizeof drvinfo); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 444 | ifr.ifr_data = (char *) &drvinfo; |
Marco Varlese | 99d7a72 | 2018-06-27 09:54:44 +0200 | [diff] [blame] | 445 | strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name)); |
| 446 | ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0'; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 447 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 448 | if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 449 | { |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 450 | /* Some interfaces (eg "lo") don't support this ioctl */ |
| 451 | if ((errno != ENOTSUP) && (errno != ENODEV)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 452 | clib_unix_warning ("ioctl fetch intf %s bus info error", |
| 453 | e->d_name); |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 454 | continue; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 455 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 456 | |
| 457 | if (strcmp ((char *) s, drvinfo.bus_info)) |
| 458 | continue; |
| 459 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 460 | memset (&ifr, 0, sizeof (ifr)); |
Marco Varlese | 99d7a72 | 2018-06-27 09:54:44 +0200 | [diff] [blame] | 461 | strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name)); |
| 462 | ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0'; |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 463 | if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 464 | { |
| 465 | error = clib_error_return_unix (0, "ioctl fetch intf %s flags", |
| 466 | e->d_name); |
| 467 | close (fd); |
| 468 | goto done; |
| 469 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 470 | |
| 471 | if (ifr.ifr_flags & IFF_UP) |
| 472 | { |
| 473 | error = clib_error_return (0, "Skipping PCI device %U as host " |
| 474 | "interface %s is up", |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 475 | format_vlib_pci_addr, addr, e->d_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 476 | close (fd); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 477 | goto done; |
| 478 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 481 | close (fd); |
| 482 | vec_reset_length (s); |
| 483 | |
| 484 | s = format (s, "%v/driver/unbind%c", dev_dir_name, 0); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 485 | clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 486 | vec_reset_length (s); |
| 487 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 488 | s = format (s, "%v/driver_override%c", dev_dir_name, 0); |
| 489 | if (access ((char *) s, F_OK) == 0) |
| 490 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 491 | clib_sysfs_write ((char *) s, "%s", uio_drv_name); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 492 | clear_driver_override = 1; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | vec_reset_length (s); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 497 | s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0); |
| 498 | clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id, |
| 499 | di->device_id); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 500 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 501 | vec_reset_length (s); |
| 502 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 503 | s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0); |
| 504 | clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 505 | vec_reset_length (s); |
| 506 | |
| 507 | if (clear_driver_override) |
| 508 | { |
| 509 | s = format (s, "%v/driver_override%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 510 | clib_sysfs_write ((char *) s, "%c", 0); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 511 | vec_reset_length (s); |
| 512 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 513 | |
| 514 | done: |
| 515 | closedir (dir); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | vec_free (s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 517 | vec_free (dev_dir_name); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 518 | vec_free (driver_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | return error; |
| 520 | } |
| 521 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | |
| 523 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 524 | scan_uio_dir (void *arg, u8 * path_name, u8 * file_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 526 | linux_pci_device_t *l = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | unformat_input_t input; |
| 528 | |
| 529 | unformat_init_string (&input, (char *) file_name, vec_len (file_name)); |
| 530 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 531 | if (!unformat (&input, "uio%d", &l->uio_minor)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | abort (); |
| 533 | |
| 534 | unformat_free (&input); |
| 535 | return 0; |
| 536 | } |
| 537 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 538 | static clib_error_t * |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 539 | vfio_set_irqs (linux_pci_device_t * p, u32 index, u32 start, u32 count, |
| 540 | u32 flags, int *efds) |
| 541 | { |
| 542 | int data_len = efds ? count * sizeof (int) : 0; |
| 543 | u8 buf[sizeof (struct vfio_irq_set) + data_len]; |
| 544 | struct vfio_irq_info irq_info = { 0 }; |
| 545 | struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf; |
| 546 | |
| 547 | |
| 548 | irq_info.argsz = sizeof (struct vfio_irq_info); |
| 549 | irq_info.index = index; |
| 550 | |
| 551 | if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0) |
| 552 | return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) " |
| 553 | "'%U'", format_vlib_pci_addr, &p->addr); |
| 554 | |
| 555 | if (irq_info.count < start + count) |
| 556 | return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on " |
| 557 | "'%U'", format_vlib_pci_addr, &p->addr); |
| 558 | |
| 559 | |
| 560 | if (efds) |
| 561 | { |
| 562 | flags |= VFIO_IRQ_SET_DATA_EVENTFD; |
| 563 | clib_memcpy (&irq_set->data, efds, data_len); |
| 564 | } |
| 565 | else |
| 566 | flags |= VFIO_IRQ_SET_DATA_NONE; |
| 567 | |
| 568 | ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) != |
| 569 | (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)); |
| 570 | |
| 571 | irq_set->argsz = sizeof (struct vfio_irq_set) + data_len; |
| 572 | irq_set->index = index; |
| 573 | irq_set->start = start; |
| 574 | irq_set->count = count; |
| 575 | irq_set->flags = flags; |
| 576 | |
| 577 | if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0) |
| 578 | return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) " |
| 579 | "[index = %u, start = %u, count = %u, " |
| 580 | "flags = 0x%x]", |
| 581 | format_vlib_pci_addr, &p->addr, |
| 582 | index, start, count, flags); |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 587 | linux_pci_uio_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 588 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 589 | int __attribute__ ((unused)) rv; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 590 | vlib_pci_dev_handle_t h = uf->private_data; |
| 591 | linux_pci_device_t *p = linux_pci_get_device (h); |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 592 | linux_pci_irq_t *irq = &p->intx_irq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 593 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 594 | u32 icount; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 595 | rv = read (uf->file_descriptor, &icount, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 597 | if (irq->intx_handler) |
| 598 | irq->intx_handler (h); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 599 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 600 | vlib_pci_intr_enable (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | |
| 602 | return /* no error */ 0; |
| 603 | } |
| 604 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 605 | static clib_error_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 606 | linux_pci_vfio_unmask_intx (linux_pci_device_t * d) |
| 607 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 608 | return vfio_set_irqs (d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1, |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 609 | VFIO_IRQ_SET_ACTION_UNMASK, 0); |
| 610 | } |
| 611 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 612 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 613 | linux_pci_uio_error_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 614 | { |
| 615 | u32 error_index = (u32) uf->private_data; |
| 616 | |
| 617 | return clib_error_return (0, "pci device %d: error", error_index); |
| 618 | } |
| 619 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 620 | static clib_error_t * |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 621 | linux_pci_vfio_msix_read_ready (clib_file_t * uf) |
| 622 | { |
| 623 | int __attribute__ ((unused)) rv; |
| 624 | vlib_pci_dev_handle_t h = uf->private_data >> 16; |
| 625 | u16 line = uf->private_data & 0xffff; |
| 626 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 627 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line); |
| 628 | |
| 629 | u64 icount; |
| 630 | rv = read (uf->file_descriptor, &icount, sizeof (icount)); |
| 631 | |
| 632 | if (irq->msix_handler) |
| 633 | irq->msix_handler (h, line); |
| 634 | |
| 635 | return /* no error */ 0; |
| 636 | } |
| 637 | |
| 638 | static clib_error_t * |
| 639 | linux_pci_vfio_intx_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 641 | int __attribute__ ((unused)) rv; |
| 642 | vlib_pci_dev_handle_t h = uf->private_data; |
| 643 | linux_pci_device_t *p = linux_pci_get_device (h); |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 644 | linux_pci_irq_t *irq = &p->intx_irq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 646 | u64 icount; |
| 647 | rv = read (uf->file_descriptor, &icount, sizeof (icount)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 649 | if (irq->intx_handler) |
| 650 | irq->intx_handler (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 651 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 652 | linux_pci_vfio_unmask_intx (p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 654 | return /* no error */ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 657 | static clib_error_t * |
| 658 | linux_pci_vfio_error_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 660 | u32 error_index = (u32) uf->private_data; |
| 661 | |
| 662 | return clib_error_return (0, "pci device %d: error", error_index); |
| 663 | } |
| 664 | |
| 665 | static clib_error_t * |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 666 | add_device_uio (linux_pci_device_t * p, vlib_pci_device_info_t * di, |
| 667 | pci_device_registration_t * r) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 668 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 669 | linux_pci_main_t *lpm = &linux_pci_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 670 | clib_error_t *err = 0; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 671 | u8 *s = 0; |
| 672 | |
| 673 | p->addr.as_u32 = di->addr.as_u32; |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 674 | p->fd = -1; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 675 | p->type = LINUX_PCI_DEVICE_TYPE_UIO; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 676 | |
| 677 | s = format (s, "%s/%U/config%c", sysfs_pci_dev_path, |
| 678 | format_vlib_pci_addr, &di->addr, 0); |
| 679 | |
| 680 | p->config_fd = open ((char *) s, O_RDWR); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 681 | p->config_offset = 0; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 682 | vec_reset_length (s); |
| 683 | |
| 684 | if (p->config_fd == -1) |
| 685 | { |
| 686 | err = clib_error_return_unix (0, "open '%s'", s); |
| 687 | goto error; |
| 688 | } |
| 689 | |
| 690 | s = format (0, "%s/%U/uio", sysfs_pci_dev_path, |
| 691 | format_vlib_pci_addr, &di->addr); |
| 692 | foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */ |
| 693 | 1); |
| 694 | vec_reset_length (s); |
| 695 | |
| 696 | s = format (s, "/dev/uio%d%c", p->uio_minor, 0); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 697 | p->fd = open ((char *) s, O_RDWR); |
| 698 | if (p->fd < 0) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 699 | { |
| 700 | err = clib_error_return_unix (0, "open '%s'", s); |
| 701 | goto error; |
| 702 | } |
| 703 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 704 | if (r && r->interrupt_handler) |
| 705 | vlib_pci_register_intx_handler (p->handle, r->interrupt_handler); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 706 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 707 | if (r && r->init_function) |
| 708 | err = r->init_function (lpm->vlib_main, p->handle); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 709 | |
| 710 | error: |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 711 | vec_free (s); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 712 | if (err) |
| 713 | { |
Damjan Marion | 9650418 | 2017-12-10 23:54:46 +0100 | [diff] [blame] | 714 | if (p->config_fd != -1) |
| 715 | close (p->config_fd); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 716 | if (p->fd != -1) |
| 717 | close (p->fd); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 718 | } |
| 719 | return err; |
| 720 | } |
| 721 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 722 | clib_error_t * |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 723 | vlib_pci_register_intx_handler (vlib_pci_dev_handle_t h, |
| 724 | pci_intx_handler_function_t * intx_handler) |
| 725 | { |
| 726 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 727 | clib_file_t t = { 0 }; |
| 728 | linux_pci_irq_t *irq = &p->intx_irq; |
| 729 | ASSERT (irq->fd == -1); |
| 730 | |
| 731 | if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
| 732 | { |
| 733 | struct vfio_irq_info irq_info = { 0 }; |
| 734 | irq_info.argsz = sizeof (struct vfio_irq_info); |
| 735 | irq_info.index = VFIO_PCI_INTX_IRQ_INDEX; |
| 736 | if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0) |
| 737 | return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '" |
| 738 | "%U'", format_vlib_pci_addr, &p->addr); |
| 739 | if (irq_info.count != 1) |
| 740 | return clib_error_return (0, "INTx interrupt does not exist on device" |
| 741 | "'%U'", format_vlib_pci_addr, &p->addr); |
| 742 | |
| 743 | irq->fd = eventfd (0, EFD_NONBLOCK); |
| 744 | if (irq->fd == -1) |
| 745 | return clib_error_return_unix (0, "eventfd"); |
| 746 | t.file_descriptor = irq->fd; |
| 747 | t.read_function = linux_pci_vfio_intx_read_ready; |
| 748 | } |
| 749 | else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
| 750 | { |
| 751 | t.file_descriptor = p->fd; |
| 752 | t.read_function = linux_pci_uio_read_ready; |
| 753 | } |
| 754 | else |
| 755 | return 0; |
| 756 | |
| 757 | t.error_function = linux_pci_uio_error_ready; |
| 758 | t.private_data = p->handle; |
| 759 | t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr); |
| 760 | irq->clib_file_index = clib_file_add (&file_main, &t); |
| 761 | irq->intx_handler = intx_handler; |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | clib_error_t * |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 766 | vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h, u32 start, u32 count, |
| 767 | pci_msix_handler_function_t * msix_handler) |
| 768 | { |
| 769 | clib_error_t *err = 0; |
| 770 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 771 | u32 i; |
| 772 | |
| 773 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 774 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 775 | "support"); |
| 776 | |
| 777 | /* *INDENT-OFF* */ |
| 778 | vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t) |
| 779 | { .fd = -1}); |
| 780 | /* *INDENT-ON* */ |
| 781 | |
| 782 | for (i = start; i < start + count; i++) |
| 783 | { |
| 784 | clib_file_t t = { 0 }; |
| 785 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i); |
| 786 | ASSERT (irq->fd == -1); |
| 787 | |
| 788 | irq->fd = eventfd (0, EFD_NONBLOCK); |
| 789 | if (irq->fd == -1) |
| 790 | { |
| 791 | err = clib_error_return_unix (0, "eventfd"); |
| 792 | goto error; |
| 793 | } |
| 794 | |
| 795 | t.read_function = linux_pci_vfio_msix_read_ready; |
| 796 | t.file_descriptor = irq->fd; |
| 797 | t.error_function = linux_pci_vfio_error_ready; |
| 798 | t.private_data = p->handle << 16 | i; |
| 799 | t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr, |
| 800 | &p->addr, i); |
| 801 | irq->clib_file_index = clib_file_add (&file_main, &t); |
| 802 | irq->msix_handler = msix_handler; |
| 803 | } |
| 804 | |
| 805 | return 0; |
| 806 | |
| 807 | error: |
| 808 | while (i-- > start) |
| 809 | { |
| 810 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i); |
| 811 | if (irq->fd != -1) |
| 812 | { |
| 813 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 814 | close (irq->fd); |
| 815 | irq->fd = -1; |
| 816 | } |
| 817 | } |
| 818 | return err; |
| 819 | } |
| 820 | |
| 821 | clib_error_t * |
| 822 | vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count) |
| 823 | { |
| 824 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 825 | int fds[count]; |
| 826 | int i; |
| 827 | |
| 828 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 829 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 830 | "support"); |
| 831 | |
| 832 | for (i = start; i < start + count; i++) |
| 833 | { |
| 834 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i); |
| 835 | fds[i] = irq->fd; |
| 836 | } |
| 837 | |
| 838 | return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count, |
| 839 | VFIO_IRQ_SET_ACTION_TRIGGER, fds); |
| 840 | } |
| 841 | |
| 842 | clib_error_t * |
| 843 | vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count) |
| 844 | { |
| 845 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 846 | int i, fds[count]; |
| 847 | |
| 848 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 849 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 850 | "support"); |
| 851 | |
| 852 | for (i = start; i < start + count; i++) |
| 853 | fds[i] = -1; |
| 854 | |
| 855 | return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count, |
| 856 | VFIO_IRQ_SET_ACTION_TRIGGER, fds); |
| 857 | } |
| 858 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 859 | static clib_error_t * |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 860 | add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di, |
| 861 | pci_device_registration_t * r) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 862 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 863 | linux_pci_main_t *lpm = &linux_pci_main; |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 864 | struct vfio_device_info device_info = { 0 }; |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 865 | struct vfio_region_info reg = { 0 }; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 866 | clib_error_t *err = 0; |
| 867 | u8 *s = 0; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 868 | |
| 869 | p->addr.as_u32 = di->addr.as_u32; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 870 | p->type = LINUX_PCI_DEVICE_TYPE_VFIO; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 871 | |
| 872 | if (di->driver_name == 0 || |
| 873 | (strcmp ("vfio-pci", (char *) di->driver_name) != 0)) |
| 874 | return clib_error_return (0, "Device '%U' (iommu group %d) not bound to " |
| 875 | "vfio-pci", format_vlib_pci_addr, &di->addr, |
| 876 | di->iommu_group); |
| 877 | |
Damjan Marion | 1ba0fa4 | 2018-03-04 17:19:08 +0100 | [diff] [blame] | 878 | if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd))) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 879 | return err; |
| 880 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 881 | device_info.argsz = sizeof (device_info); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 882 | if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 883 | { |
| 884 | err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'", |
| 885 | format_vlib_pci_addr, &di->addr); |
| 886 | goto error; |
| 887 | } |
| 888 | |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 889 | reg.argsz = sizeof (struct vfio_region_info); |
| 890 | reg.index = VFIO_PCI_CONFIG_REGION_INDEX; |
| 891 | if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0) |
| 892 | { |
| 893 | err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'", |
| 894 | format_vlib_pci_addr, &di->addr); |
| 895 | goto error; |
| 896 | } |
| 897 | p->config_offset = reg.offset; |
| 898 | p->config_fd = p->fd; |
| 899 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 900 | /* reset if device supports it */ |
| 901 | if (device_info.flags & VFIO_DEVICE_FLAGS_RESET) |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 902 | if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 903 | { |
| 904 | err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'", |
| 905 | format_vlib_pci_addr, &di->addr); |
| 906 | goto error; |
| 907 | } |
| 908 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 909 | if (r && r->interrupt_handler) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 910 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 911 | vlib_pci_register_intx_handler (p->handle, r->interrupt_handler); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 912 | linux_pci_vfio_unmask_intx (p); |
| 913 | } |
| 914 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 915 | if (r && r->init_function) |
| 916 | err = r->init_function (lpm->vlib_main, p->handle); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 917 | |
| 918 | error: |
| 919 | vec_free (s); |
| 920 | if (err) |
| 921 | { |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 922 | if (p->fd != -1) |
| 923 | close (p->fd); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 924 | if (p->config_fd != -1 && p->config_fd != p->fd) |
Damjan Marion | 9650418 | 2017-12-10 23:54:46 +0100 | [diff] [blame] | 925 | close (p->config_fd); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 926 | p->config_fd = p->fd = -1; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 927 | } |
| 928 | return err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* Configuration space read/write. */ |
| 932 | clib_error_t * |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 933 | vlib_pci_read_write_config (vlib_pci_dev_handle_t h, |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 934 | vlib_read_or_write_t read_or_write, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 935 | uword address, void *data, u32 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 936 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 937 | linux_pci_device_t *p = linux_pci_get_device (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 938 | int n; |
| 939 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 940 | if (read_or_write == VLIB_READ) |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 941 | n = pread (p->config_fd, data, n_bytes, p->config_offset + address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 942 | else |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 943 | n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | |
| 945 | if (n != n_bytes) |
| 946 | return clib_error_return_unix (0, "%s", |
| 947 | read_or_write == VLIB_READ |
| 948 | ? "read" : "write"); |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static clib_error_t * |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 954 | vlib_pci_map_region_int (vlib_pci_dev_handle_t h, |
| 955 | u32 bar, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 956 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 957 | linux_pci_device_t *p = linux_pci_get_device (h); |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 958 | int fd = -1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 959 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 960 | int flags = MAP_SHARED; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 961 | u64 size = 0, offset = 0; |
| 962 | |
| 963 | ASSERT (bar <= 5); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 964 | |
| 965 | error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 966 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 967 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 968 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 969 | u8 *file_name; |
| 970 | struct stat stat_buf; |
| 971 | file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path, |
| 972 | format_vlib_pci_addr, &p->addr, bar, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 974 | fd = open ((char *) file_name, O_RDWR); |
| 975 | if (fd < 0) |
| 976 | { |
| 977 | error = clib_error_return_unix (0, "open `%s'", file_name); |
| 978 | vec_free (file_name); |
| 979 | return error; |
| 980 | } |
| 981 | |
| 982 | if (fstat (fd, &stat_buf) < 0) |
| 983 | { |
| 984 | error = clib_error_return_unix (0, "fstat `%s'", file_name); |
| 985 | vec_free (file_name); |
| 986 | close (fd); |
| 987 | return error; |
| 988 | } |
| 989 | |
| 990 | vec_free (file_name); |
| 991 | if (addr != 0) |
| 992 | flags |= MAP_FIXED; |
| 993 | size = stat_buf.st_size; |
| 994 | offset = 0; |
| 995 | } |
| 996 | else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 998 | struct vfio_region_info reg = { 0 }; |
| 999 | reg.argsz = sizeof (struct vfio_region_info); |
| 1000 | reg.index = bar; |
| 1001 | if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0) |
| 1002 | return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) " |
| 1003 | "'%U'", format_vlib_pci_addr, |
| 1004 | &p->addr); |
| 1005 | fd = p->fd; |
| 1006 | size = reg.size; |
| 1007 | offset = reg.offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1008 | } |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1009 | else |
| 1010 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1011 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1012 | *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1013 | if (*result == (void *) -1) |
| 1014 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1015 | error = clib_error_return_unix (0, "mmap `BAR%u'", bar); |
| 1016 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1017 | close (fd); |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1018 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1019 | } |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1020 | |
| 1021 | /* *INDENT-OFF* */ |
| 1022 | vec_validate_init_empty (p->regions, bar, |
| 1023 | (linux_pci_region_t) { .fd = -1}); |
| 1024 | /* *INDENT-ON* */ |
| 1025 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
| 1026 | p->regions[bar].fd = fd; |
| 1027 | p->regions[bar].addr = *result; |
| 1028 | p->regions[bar].size = size; |
| 1029 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | clib_error_t * |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1033 | vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1034 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1035 | return (vlib_pci_map_region_int (h, resource, 0 /* addr */ , result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | clib_error_t * |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1039 | vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, u32 resource, u8 * addr, |
| 1040 | void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1041 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1042 | return (vlib_pci_map_region_int (h, resource, addr, result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1045 | clib_error_t * |
| 1046 | vlib_pci_device_open (vlib_pci_addr_t * addr, |
| 1047 | pci_device_id_t ids[], vlib_pci_dev_handle_t * handle) |
| 1048 | { |
| 1049 | linux_pci_main_t *lpm = &linux_pci_main; |
| 1050 | vlib_pci_device_info_t *di; |
| 1051 | linux_pci_device_t *p; |
| 1052 | clib_error_t *err = 0; |
| 1053 | pci_device_id_t *i; |
| 1054 | |
| 1055 | di = vlib_pci_get_device_info (addr, &err); |
| 1056 | |
| 1057 | if (err) |
| 1058 | return err; |
| 1059 | for (i = ids; i->vendor_id != 0; i++) |
| 1060 | if (i->vendor_id == di->vendor_id && i->device_id == di->device_id) |
| 1061 | break; |
| 1062 | |
| 1063 | if (i->vendor_id == 0) |
| 1064 | return clib_error_return (0, "Wrong vendor or device id"); |
| 1065 | |
| 1066 | pool_get (lpm->linux_pci_devices, p); |
| 1067 | p->handle = p - lpm->linux_pci_devices; |
| 1068 | p->intx_irq.fd = -1; |
| 1069 | |
| 1070 | if (di->iommu_group != -1) |
| 1071 | err = add_device_vfio (p, di, 0); |
| 1072 | else |
| 1073 | err = add_device_uio (p, di, 0); |
| 1074 | if (err) |
| 1075 | goto error; |
| 1076 | |
| 1077 | *handle = p->handle; |
| 1078 | |
| 1079 | error: |
| 1080 | vlib_pci_free_device_info (di); |
| 1081 | if (err) |
| 1082 | { |
| 1083 | memset (p, 0, sizeof (linux_pci_device_t)); |
| 1084 | pool_put (lpm->linux_pci_devices, p); |
| 1085 | } |
| 1086 | |
| 1087 | return err; |
| 1088 | } |
| 1089 | |
| 1090 | void |
| 1091 | vlib_pci_device_close (vlib_pci_dev_handle_t h) |
| 1092 | { |
| 1093 | linux_pci_main_t *lpm = &linux_pci_main; |
| 1094 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1095 | linux_pci_irq_t *irq; |
| 1096 | linux_pci_region_t *res; |
| 1097 | clib_error_t *err = 0; |
| 1098 | |
| 1099 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
| 1100 | { |
| 1101 | irq = &p->intx_irq; |
| 1102 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 1103 | close (p->config_fd); |
| 1104 | } |
| 1105 | else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
| 1106 | { |
| 1107 | irq = &p->intx_irq; |
| 1108 | /* close INTx irqs */ |
| 1109 | if (irq->fd != -1) |
| 1110 | { |
| 1111 | err = vfio_set_irqs (p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0, |
| 1112 | VFIO_IRQ_SET_ACTION_TRIGGER, 0); |
| 1113 | clib_error_free (err); |
| 1114 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 1115 | close (irq->fd); |
| 1116 | } |
| 1117 | |
| 1118 | /* close MSI-X irqs */ |
| 1119 | if (vec_len (p->msix_irqs)) |
| 1120 | { |
| 1121 | err = vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0, |
| 1122 | VFIO_IRQ_SET_ACTION_TRIGGER, 0); |
| 1123 | clib_error_free (err); |
| 1124 | /* *INDENT-OFF* */ |
| 1125 | vec_foreach (irq, p->msix_irqs) |
| 1126 | { |
| 1127 | if (irq->fd == -1) |
| 1128 | continue; |
| 1129 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 1130 | close (irq->fd); |
| 1131 | } |
| 1132 | /* *INDENT-ON* */ |
| 1133 | vec_free (p->msix_irqs); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /* *INDENT-OFF* */ |
| 1138 | vec_foreach (res, p->regions) |
| 1139 | { |
| 1140 | if (res->size == 0) |
| 1141 | continue; |
| 1142 | munmap (res->addr, res->size); |
| 1143 | if (res->fd != -1) |
| 1144 | close (res->fd); |
| 1145 | } |
| 1146 | /* *INDENT-ON* */ |
| 1147 | vec_free (p->regions); |
| 1148 | |
| 1149 | close (p->fd); |
| 1150 | memset (p, 0, sizeof (linux_pci_device_t)); |
| 1151 | pool_put (lpm->linux_pci_devices, p); |
| 1152 | } |
| 1153 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1154 | void |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1155 | init_device_from_registered (vlib_pci_device_info_t * di) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1156 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1157 | vlib_pci_main_t *pm = &pci_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1158 | linux_pci_main_t *lpm = &linux_pci_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1159 | pci_device_registration_t *r; |
| 1160 | pci_device_id_t *i; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1161 | clib_error_t *err = 0; |
| 1162 | linux_pci_device_t *p; |
| 1163 | |
| 1164 | pool_get (lpm->linux_pci_devices, p); |
| 1165 | p->handle = p - lpm->linux_pci_devices; |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1166 | p->intx_irq.fd = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1167 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 1168 | r = pm->pci_device_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1169 | |
| 1170 | while (r) |
| 1171 | { |
| 1172 | for (i = r->supported_devices; i->vendor_id != 0; i++) |
Damjan Marion | 3a59382 | 2018-02-17 14:06:53 +0100 | [diff] [blame] | 1173 | if (i->vendor_id == di->vendor_id && i->device_id == di->device_id) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1174 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1175 | if (di->iommu_group != -1) |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1176 | err = add_device_vfio (p, di, r); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1177 | else |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1178 | err = add_device_uio (p, di, r); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 1179 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1180 | if (err) |
| 1181 | clib_error_report (err); |
| 1182 | else |
| 1183 | return; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1184 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1185 | r = r->next_registration; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1186 | } |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1187 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1188 | /* No driver, close the PCI config-space FD */ |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1189 | memset (p, 0, sizeof (linux_pci_device_t)); |
| 1190 | pool_put (lpm->linux_pci_devices, p); |
| 1191 | } |
| 1192 | |
| 1193 | static clib_error_t * |
| 1194 | scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored) |
| 1195 | { |
| 1196 | vlib_pci_addr_t addr, **addrv = arg; |
| 1197 | unformat_input_t input; |
| 1198 | clib_error_t *err = 0; |
| 1199 | |
| 1200 | unformat_init_string (&input, (char *) dev_dir_name, |
| 1201 | vec_len (dev_dir_name)); |
| 1202 | |
| 1203 | if (!unformat (&input, "/sys/bus/pci/devices/%U", |
| 1204 | unformat_vlib_pci_addr, &addr)) |
| 1205 | err = clib_error_return (0, "unformat error `%v`", dev_dir_name); |
| 1206 | |
| 1207 | unformat_free (&input); |
| 1208 | |
| 1209 | if (err) |
| 1210 | return err; |
| 1211 | |
| 1212 | vec_add1 (*addrv, addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1216 | static int |
| 1217 | pci_addr_cmp (void *v1, void *v2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1218 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1219 | vlib_pci_addr_t *a1 = v1; |
| 1220 | vlib_pci_addr_t *a2 = v2; |
| 1221 | |
| 1222 | if (a1->domain > a2->domain) |
| 1223 | return 1; |
| 1224 | if (a1->domain < a2->domain) |
| 1225 | return -1; |
| 1226 | if (a1->bus > a2->bus) |
| 1227 | return 1; |
| 1228 | if (a1->bus < a2->bus) |
| 1229 | return -1; |
| 1230 | if (a1->slot > a2->slot) |
| 1231 | return 1; |
| 1232 | if (a1->slot < a2->slot) |
| 1233 | return -1; |
| 1234 | if (a1->function > a2->function) |
| 1235 | return 1; |
| 1236 | if (a1->function < a2->function) |
| 1237 | return -1; |
| 1238 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1241 | vlib_pci_addr_t * |
| 1242 | vlib_pci_get_all_dev_addrs () |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1243 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1244 | vlib_pci_addr_t *addrs = 0; |
| 1245 | clib_error_t *err; |
| 1246 | err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr, |
| 1247 | &addrs, /* scan_dirs */ 0); |
| 1248 | if (err) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1249 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1250 | vec_free (addrs); |
| 1251 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1254 | vec_sort_with_function (addrs, pci_addr_cmp); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 1255 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1256 | return addrs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1259 | clib_error_t * |
| 1260 | linux_pci_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1261 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1262 | vlib_pci_main_t *pm = &pci_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1263 | vlib_pci_addr_t *addr = 0, *addrs; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1264 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1265 | |
| 1266 | pm->vlib_main = vm; |
| 1267 | |
| 1268 | if ((error = vlib_call_init_function (vm, unix_input_init))) |
| 1269 | return error; |
| 1270 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1271 | ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 1272 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1273 | addrs = vlib_pci_get_all_dev_addrs (); |
| 1274 | /* *INDENT-OFF* */ |
| 1275 | vec_foreach (addr, addrs) |
| 1276 | { |
| 1277 | vlib_pci_device_info_t *d; |
| 1278 | if ((d = vlib_pci_get_device_info (addr, 0))) |
| 1279 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1280 | init_device_from_registered (d); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1281 | vlib_pci_free_device_info (d); |
| 1282 | } |
| 1283 | } |
| 1284 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1285 | |
| 1286 | return error; |
| 1287 | } |
| 1288 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 1289 | VLIB_INIT_FUNCTION (linux_pci_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1290 | |
| 1291 | /* |
| 1292 | * fd.io coding-style-patch-verification: ON |
| 1293 | * |
| 1294 | * Local Variables: |
| 1295 | * eval: (c-set-style "gnu") |
| 1296 | * End: |
| 1297 | */ |