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