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