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