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