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