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