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 | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 651 | struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf; |
| 652 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 653 | if (efds) |
| 654 | { |
Damjan Marion | 7444fd2 | 2023-11-06 00:07:15 +0000 | [diff] [blame] | 655 | int *data = (int *) irq_set->data; |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 656 | flags |= VFIO_IRQ_SET_DATA_EVENTFD; |
Damjan Marion | 7444fd2 | 2023-11-06 00:07:15 +0000 | [diff] [blame] | 657 | for (u32 i = 0; i < count; i++) |
| 658 | data[i] = efds[i]; |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 659 | } |
| 660 | else |
| 661 | flags |= VFIO_IRQ_SET_DATA_NONE; |
| 662 | |
| 663 | ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) != |
| 664 | (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)); |
| 665 | |
| 666 | irq_set->argsz = sizeof (struct vfio_irq_set) + data_len; |
| 667 | irq_set->index = index; |
| 668 | irq_set->start = start; |
| 669 | irq_set->count = count; |
| 670 | irq_set->flags = flags; |
| 671 | |
| 672 | if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0) |
Damjan Marion | 7444fd2 | 2023-11-06 00:07:15 +0000 | [diff] [blame] | 673 | return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS)\n%U", |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 674 | format_vlib_pci_addr, &p->addr, |
Damjan Marion | 7444fd2 | 2023-11-06 00:07:15 +0000 | [diff] [blame] | 675 | format_vfio_irq_set, irq_set); |
| 676 | |
| 677 | log_debug (p, "%s:\n%U", __func__, format_vfio_irq_set, irq_set); |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 682 | linux_pci_uio_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 683 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 684 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 685 | int __attribute__ ((unused)) rv; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 686 | vlib_pci_dev_handle_t h = uf->private_data; |
| 687 | linux_pci_device_t *p = linux_pci_get_device (h); |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 688 | linux_pci_irq_t *irq = &p->intx_irq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 690 | u32 icount; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 691 | rv = read (uf->file_descriptor, &icount, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 693 | if (irq->intx_handler) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 694 | irq->intx_handler (vm, h); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 695 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 696 | vlib_pci_intr_enable (vm, h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 697 | |
| 698 | return /* no error */ 0; |
| 699 | } |
| 700 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 701 | static clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 702 | 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] | 703 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 704 | 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] | 705 | VFIO_IRQ_SET_ACTION_UNMASK, 0); |
| 706 | } |
| 707 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 708 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 709 | linux_pci_uio_error_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | { |
| 711 | u32 error_index = (u32) uf->private_data; |
| 712 | |
| 713 | return clib_error_return (0, "pci device %d: error", error_index); |
| 714 | } |
| 715 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 716 | static clib_error_t * |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 717 | linux_pci_vfio_msix_read_ready (clib_file_t * uf) |
| 718 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 719 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 720 | int __attribute__ ((unused)) rv; |
| 721 | vlib_pci_dev_handle_t h = uf->private_data >> 16; |
| 722 | u16 line = uf->private_data & 0xffff; |
| 723 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 724 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line); |
| 725 | |
| 726 | u64 icount; |
| 727 | rv = read (uf->file_descriptor, &icount, sizeof (icount)); |
| 728 | |
| 729 | if (irq->msix_handler) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 730 | irq->msix_handler (vm, h, line); |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 731 | |
| 732 | return /* no error */ 0; |
| 733 | } |
| 734 | |
| 735 | static clib_error_t * |
| 736 | linux_pci_vfio_intx_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 738 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 739 | int __attribute__ ((unused)) rv; |
| 740 | vlib_pci_dev_handle_t h = uf->private_data; |
| 741 | linux_pci_device_t *p = linux_pci_get_device (h); |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 742 | linux_pci_irq_t *irq = &p->intx_irq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 743 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 744 | u64 icount; |
| 745 | rv = read (uf->file_descriptor, &icount, sizeof (icount)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 747 | if (irq->intx_handler) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 748 | irq->intx_handler (vm, h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 749 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 750 | linux_pci_vfio_unmask_intx (vm, p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 751 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 752 | return /* no error */ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 755 | static clib_error_t * |
| 756 | linux_pci_vfio_error_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 758 | u32 error_index = (u32) uf->private_data; |
| 759 | |
| 760 | return clib_error_return (0, "pci device %d: error", error_index); |
| 761 | } |
| 762 | |
| 763 | static clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 764 | add_device_uio (vlib_main_t * vm, linux_pci_device_t * p, |
| 765 | vlib_pci_device_info_t * di, pci_device_registration_t * r) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 766 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 767 | linux_pci_main_t *lpm = &linux_pci_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 768 | clib_error_t *err = 0; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 769 | u8 *s = 0; |
| 770 | |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 771 | p->fd = -1; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 772 | p->type = LINUX_PCI_DEVICE_TYPE_UIO; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 773 | |
| 774 | s = format (s, "%s/%U/config%c", sysfs_pci_dev_path, |
| 775 | format_vlib_pci_addr, &di->addr, 0); |
| 776 | |
| 777 | p->config_fd = open ((char *) s, O_RDWR); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 778 | p->config_offset = 0; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 779 | vec_reset_length (s); |
| 780 | |
| 781 | if (p->config_fd == -1) |
| 782 | { |
| 783 | err = clib_error_return_unix (0, "open '%s'", s); |
| 784 | goto error; |
| 785 | } |
| 786 | |
Benoît Ganne | 0b91bd6 | 2019-11-20 09:07:50 +0100 | [diff] [blame] | 787 | s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path, |
| 788 | format_vlib_pci_addr, &di->addr, 0); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 789 | foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */ |
| 790 | 1); |
| 791 | vec_reset_length (s); |
| 792 | |
| 793 | s = format (s, "/dev/uio%d%c", p->uio_minor, 0); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 794 | p->fd = open ((char *) s, O_RDWR); |
| 795 | if (p->fd < 0) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 796 | { |
| 797 | err = clib_error_return_unix (0, "open '%s'", s); |
| 798 | goto error; |
| 799 | } |
| 800 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 801 | if (r && r->interrupt_handler) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 802 | vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 803 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 804 | if (r && r->init_function) |
| 805 | err = r->init_function (lpm->vlib_main, p->handle); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 806 | |
| 807 | error: |
Damjan Marion | 20ba164 | 2018-03-26 15:35:33 +0200 | [diff] [blame] | 808 | vec_free (s); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 809 | if (err) |
| 810 | { |
Damjan Marion | 9650418 | 2017-12-10 23:54:46 +0100 | [diff] [blame] | 811 | if (p->config_fd != -1) |
| 812 | close (p->config_fd); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 813 | if (p->fd != -1) |
| 814 | close (p->fd); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 815 | } |
| 816 | return err; |
| 817 | } |
| 818 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 819 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 820 | 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] | 821 | pci_intx_handler_function_t * intx_handler) |
| 822 | { |
| 823 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 824 | clib_file_t t = { 0 }; |
| 825 | linux_pci_irq_t *irq = &p->intx_irq; |
| 826 | ASSERT (irq->fd == -1); |
| 827 | |
| 828 | if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
| 829 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 830 | struct vfio_irq_info ii = { 0 }; |
| 831 | ii.argsz = sizeof (struct vfio_irq_info); |
| 832 | ii.index = VFIO_PCI_INTX_IRQ_INDEX; |
| 833 | if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0) |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 834 | return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '" |
| 835 | "%U'", format_vlib_pci_addr, &p->addr); |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 836 | log_debug ( |
| 837 | p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index, |
| 838 | ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "", |
| 839 | ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "", |
| 840 | ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "", |
| 841 | ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 842 | if (ii.count != 1) |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 843 | return clib_error_return (0, "INTx interrupt does not exist on device" |
| 844 | "'%U'", format_vlib_pci_addr, &p->addr); |
| 845 | |
| 846 | irq->fd = eventfd (0, EFD_NONBLOCK); |
| 847 | if (irq->fd == -1) |
| 848 | return clib_error_return_unix (0, "eventfd"); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 849 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 850 | t.file_descriptor = irq->fd; |
| 851 | t.read_function = linux_pci_vfio_intx_read_ready; |
| 852 | } |
| 853 | else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
| 854 | { |
| 855 | t.file_descriptor = p->fd; |
| 856 | t.read_function = linux_pci_uio_read_ready; |
| 857 | } |
| 858 | else |
| 859 | return 0; |
| 860 | |
| 861 | t.error_function = linux_pci_uio_error_ready; |
| 862 | t.private_data = p->handle; |
| 863 | t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr); |
| 864 | irq->clib_file_index = clib_file_add (&file_main, &t); |
| 865 | irq->intx_handler = intx_handler; |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | clib_error_t * |
Damjan Marion | 38c6191 | 2023-10-17 16:06:26 +0000 | [diff] [blame] | 870 | vlib_pci_unregister_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h) |
| 871 | { |
| 872 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 873 | linux_pci_irq_t *irq = &p->intx_irq; |
| 874 | |
| 875 | if (irq->intx_handler == 0) |
| 876 | return 0; |
| 877 | |
| 878 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 879 | if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
| 880 | { |
| 881 | close (irq->fd); |
| 882 | irq->fd = -1; |
| 883 | } |
| 884 | |
| 885 | irq->intx_handler = 0; |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 891 | vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 892 | u32 start, u32 count, |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 893 | pci_msix_handler_function_t * msix_handler) |
| 894 | { |
| 895 | clib_error_t *err = 0; |
| 896 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 897 | u32 i; |
| 898 | |
| 899 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 900 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 901 | "support"); |
| 902 | |
| 903 | /* *INDENT-OFF* */ |
| 904 | vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t) |
| 905 | { .fd = -1}); |
| 906 | /* *INDENT-ON* */ |
| 907 | |
| 908 | for (i = start; i < start + count; i++) |
| 909 | { |
| 910 | clib_file_t t = { 0 }; |
| 911 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i); |
| 912 | ASSERT (irq->fd == -1); |
| 913 | |
| 914 | irq->fd = eventfd (0, EFD_NONBLOCK); |
| 915 | if (irq->fd == -1) |
| 916 | { |
| 917 | err = clib_error_return_unix (0, "eventfd"); |
| 918 | goto error; |
| 919 | } |
| 920 | |
| 921 | t.read_function = linux_pci_vfio_msix_read_ready; |
| 922 | t.file_descriptor = irq->fd; |
| 923 | t.error_function = linux_pci_vfio_error_ready; |
| 924 | t.private_data = p->handle << 16 | i; |
| 925 | t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr, |
| 926 | &p->addr, i); |
| 927 | irq->clib_file_index = clib_file_add (&file_main, &t); |
| 928 | irq->msix_handler = msix_handler; |
| 929 | } |
| 930 | |
| 931 | return 0; |
| 932 | |
| 933 | error: |
| 934 | while (i-- > start) |
| 935 | { |
| 936 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i); |
| 937 | if (irq->fd != -1) |
| 938 | { |
| 939 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 940 | close (irq->fd); |
| 941 | irq->fd = -1; |
| 942 | } |
| 943 | } |
| 944 | return err; |
| 945 | } |
| 946 | |
| 947 | clib_error_t * |
Damjan Marion | 38c6191 | 2023-10-17 16:06:26 +0000 | [diff] [blame] | 948 | vlib_pci_unregister_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h, |
| 949 | u32 start, u32 count) |
| 950 | { |
| 951 | clib_error_t *err = 0; |
| 952 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 953 | u32 i; |
| 954 | |
| 955 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 956 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 957 | "support"); |
| 958 | |
| 959 | for (i = start; i < start + count; i++) |
| 960 | { |
| 961 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i); |
| 962 | |
| 963 | if (irq->fd != -1) |
| 964 | { |
| 965 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 966 | close (irq->fd); |
| 967 | irq->fd = -1; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | return err; |
| 972 | } |
| 973 | |
| 974 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 975 | vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 976 | u16 start, u16 count) |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 977 | { |
| 978 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 979 | int fds[count]; |
| 980 | int i; |
| 981 | |
| 982 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 983 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 984 | "support"); |
| 985 | |
Damjan Marion | 60529a8 | 2023-11-06 00:06:26 +0000 | [diff] [blame] | 986 | for (i = 0; i < count; i++) |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 987 | { |
Damjan Marion | 60529a8 | 2023-11-06 00:06:26 +0000 | [diff] [blame] | 988 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, start + i); |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 989 | fds[i] = irq->fd; |
| 990 | } |
| 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 | 9c9490c | 2020-10-07 20:00:39 +0200 | [diff] [blame] | 996 | uword |
| 997 | vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 998 | u16 index) |
| 999 | { |
| 1000 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1001 | linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index); |
| 1002 | if (irq->fd == -1) |
| 1003 | return ~0; |
| 1004 | return irq->clib_file_index; |
| 1005 | } |
| 1006 | |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 1007 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1008 | vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 1009 | u16 start, u16 count) |
Damjan Marion | 599a16b | 2018-03-04 19:35:23 +0100 | [diff] [blame] | 1010 | { |
| 1011 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1012 | int i, fds[count]; |
| 1013 | |
| 1014 | if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO) |
| 1015 | return clib_error_return (0, "vfio driver is needed for MSI-X interrupt " |
| 1016 | "support"); |
| 1017 | |
| 1018 | for (i = start; i < start + count; i++) |
| 1019 | fds[i] = -1; |
| 1020 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1021 | 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] | 1022 | VFIO_IRQ_SET_ACTION_TRIGGER, fds); |
| 1023 | } |
| 1024 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1025 | static clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1026 | add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p, |
| 1027 | vlib_pci_device_info_t * di, pci_device_registration_t * r) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1028 | { |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1029 | linux_pci_main_t *lpm = &linux_pci_main; |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 1030 | struct vfio_device_info device_info = { 0 }; |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1031 | struct vfio_region_info reg = { 0 }; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1032 | clib_error_t *err = 0; |
| 1033 | u8 *s = 0; |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1034 | int is_noiommu; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1035 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1036 | p->type = LINUX_PCI_DEVICE_TYPE_VFIO; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1037 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1038 | 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] | 1039 | return err; |
| 1040 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1041 | if (is_noiommu == 0) |
| 1042 | p->supports_va_dma = 1; |
| 1043 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1044 | device_info.argsz = sizeof (device_info); |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 1045 | if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1046 | { |
| 1047 | err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'", |
| 1048 | format_vlib_pci_addr, &di->addr); |
| 1049 | goto error; |
| 1050 | } |
| 1051 | |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1052 | reg.argsz = sizeof (struct vfio_region_info); |
| 1053 | reg.index = VFIO_PCI_CONFIG_REGION_INDEX; |
| 1054 | if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0) |
| 1055 | { |
| 1056 | err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'", |
| 1057 | format_vlib_pci_addr, &di->addr); |
| 1058 | goto error; |
| 1059 | } |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1060 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1061 | log_debug (p, "%s %U", __func__, format_vfio_region_info, ®); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1062 | |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1063 | p->config_offset = reg.offset; |
| 1064 | p->config_fd = p->fd; |
| 1065 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1066 | /* reset if device supports it */ |
| 1067 | if (device_info.flags & VFIO_DEVICE_FLAGS_RESET) |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 1068 | if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1069 | { |
| 1070 | err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'", |
| 1071 | format_vlib_pci_addr, &di->addr); |
| 1072 | goto error; |
| 1073 | } |
| 1074 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1075 | if (r && r->interrupt_handler) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1076 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1077 | vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler); |
| 1078 | linux_pci_vfio_unmask_intx (vm, p); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1081 | if (p->supports_va_dma) |
| 1082 | { |
| 1083 | vlib_buffer_pool_t *bp; |
| 1084 | /* *INDENT-OFF* */ |
Damjan Marion | d50e347 | 2019-01-20 00:03:56 +0100 | [diff] [blame] | 1085 | vec_foreach (bp, vm->buffer_main->buffer_pools) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1086 | { |
| 1087 | u32 i; |
| 1088 | vlib_physmem_map_t *pm; |
| 1089 | pm = vlib_physmem_get_map (vm, bp->physmem_map_index); |
| 1090 | for (i = 0; i < pm->n_pages; i++) |
| 1091 | vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size)); |
| 1092 | } |
| 1093 | /* *INDENT-ON* */ |
| 1094 | } |
| 1095 | |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1096 | if (r && r->init_function) |
| 1097 | err = r->init_function (lpm->vlib_main, p->handle); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1098 | |
| 1099 | error: |
| 1100 | vec_free (s); |
| 1101 | if (err) |
| 1102 | { |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 1103 | if (p->fd != -1) |
| 1104 | close (p->fd); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 1105 | if (p->config_fd != -1 && p->config_fd != p->fd) |
Damjan Marion | 9650418 | 2017-12-10 23:54:46 +0100 | [diff] [blame] | 1106 | close (p->config_fd); |
Chris Luke | 30684ac | 2018-03-29 12:56:58 -0700 | [diff] [blame] | 1107 | p->config_fd = p->fd = -1; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1108 | } |
| 1109 | return err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* Configuration space read/write. */ |
| 1113 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1114 | 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] | 1115 | vlib_read_or_write_t read_or_write, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1116 | uword address, void *data, u32 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1117 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1118 | linux_pci_device_t *p = linux_pci_get_device (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | int n; |
| 1120 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1121 | if (read_or_write == VLIB_READ) |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 1122 | n = pread (p->config_fd, data, n_bytes, p->config_offset + address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1123 | else |
Damjan Marion | f9a968e | 2018-02-25 23:26:22 +0100 | [diff] [blame] | 1124 | n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (n != n_bytes) |
| 1127 | return clib_error_return_unix (0, "%s", |
| 1128 | read_or_write == VLIB_READ |
| 1129 | ? "read" : "write"); |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | static clib_error_t * |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1135 | vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd, |
| 1136 | u64 * size, u64 * offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1137 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1138 | linux_pci_device_t *p = linux_pci_get_device (h); |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1139 | clib_error_t *error = 0; |
| 1140 | int _fd = -1; |
| 1141 | u64 _size = 0, _offset = 0; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1142 | |
| 1143 | ASSERT (bar <= 5); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1144 | |
| 1145 | error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1146 | |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1147 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1148 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1149 | u8 *file_name; |
| 1150 | struct stat stat_buf; |
| 1151 | file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path, |
| 1152 | format_vlib_pci_addr, &p->addr, bar, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1153 | |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1154 | _fd = open ((char *) file_name, O_RDWR); |
| 1155 | if (_fd < 0) |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1156 | { |
| 1157 | error = clib_error_return_unix (0, "open `%s'", file_name); |
| 1158 | vec_free (file_name); |
| 1159 | return error; |
| 1160 | } |
| 1161 | |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1162 | if (fstat (_fd, &stat_buf) < 0) |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1163 | { |
| 1164 | error = clib_error_return_unix (0, "fstat `%s'", file_name); |
| 1165 | vec_free (file_name); |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1166 | close (_fd); |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1167 | return error; |
| 1168 | } |
| 1169 | |
| 1170 | vec_free (file_name); |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1171 | _size = stat_buf.st_size; |
| 1172 | _offset = 0; |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1173 | } |
| 1174 | else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1175 | { |
Damjan Marion | 0792bb4 | 2020-05-25 19:31:42 +0200 | [diff] [blame] | 1176 | struct vfio_region_info *r; |
| 1177 | u32 sz = sizeof (struct vfio_region_info); |
| 1178 | again: |
| 1179 | r = clib_mem_alloc (sz); |
| 1180 | clib_memset (r, 0, sz); |
| 1181 | r->argsz = sz; |
| 1182 | r->index = bar; |
| 1183 | if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0) |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1184 | return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) " |
| 1185 | "'%U'", format_vlib_pci_addr, |
| 1186 | &p->addr); |
Damjan Marion | 0792bb4 | 2020-05-25 19:31:42 +0200 | [diff] [blame] | 1187 | if (sz != r->argsz) |
| 1188 | { |
| 1189 | sz = r->argsz; |
| 1190 | clib_mem_free (r); |
| 1191 | goto again; |
| 1192 | } |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1193 | _fd = p->fd; |
Damjan Marion | 0792bb4 | 2020-05-25 19:31:42 +0200 | [diff] [blame] | 1194 | _size = r->size; |
| 1195 | _offset = r->offset; |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1196 | log_debug (p, "%s %U", __func__, format_vfio_region_info, r); |
Damjan Marion | 0792bb4 | 2020-05-25 19:31:42 +0200 | [diff] [blame] | 1197 | clib_mem_free (r); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1198 | } |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1199 | else |
| 1200 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1201 | |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1202 | *fd = _fd; |
| 1203 | *size = _size; |
| 1204 | *offset = _offset; |
| 1205 | |
| 1206 | return error; |
| 1207 | } |
| 1208 | |
| 1209 | static clib_error_t * |
| 1210 | vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 1211 | u32 bar, u8 * addr, void **result) |
| 1212 | { |
| 1213 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1214 | int fd = -1; |
| 1215 | clib_error_t *error; |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1216 | u64 size = 0, offset = 0; |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1217 | vlib_pci_config_reg_command_t command; |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1218 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1219 | log_debug (p, "map region %u to va %p", bar, addr); |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1220 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1221 | 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] | 1222 | return error; |
| 1223 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1224 | if (!(command.mem_space)) |
Mohammed Hawari | 70fc36f | 2020-10-21 16:41:30 +0200 | [diff] [blame] | 1225 | { |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1226 | log_debug (p, "setting memory enable bit"); |
| 1227 | command.mem_space = 1; |
| 1228 | 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] | 1229 | return error; |
| 1230 | } |
| 1231 | |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1232 | if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset))) |
| 1233 | return error; |
| 1234 | |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 1235 | *result = clib_mem_vm_map_shared (addr, size, fd, offset, |
| 1236 | "PCIe %U region %u", format_vlib_pci_addr, |
| 1237 | vlib_pci_get_addr (vm, h), bar); |
| 1238 | if (*result == CLIB_MEM_VM_MAP_FAILED) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1239 | { |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1240 | error = clib_error_return_unix (0, "mmap `BAR%u'", bar); |
Dave Barach | c72950e | 2020-05-04 14:47:45 -0400 | [diff] [blame] | 1241 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1242 | close (fd); |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1243 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1244 | } |
Damjan Marion | 2060db8 | 2018-03-04 17:37:15 +0100 | [diff] [blame] | 1245 | |
| 1246 | /* *INDENT-OFF* */ |
| 1247 | vec_validate_init_empty (p->regions, bar, |
| 1248 | (linux_pci_region_t) { .fd = -1}); |
| 1249 | /* *INDENT-ON* */ |
| 1250 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
| 1251 | p->regions[bar].fd = fd; |
| 1252 | p->regions[bar].addr = *result; |
| 1253 | p->regions[bar].size = size; |
| 1254 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1258 | vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource, |
| 1259 | void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1260 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1261 | return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1265 | vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 1266 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1267 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1268 | return (vlib_pci_map_region_int (vm, h, resource, addr, result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1271 | clib_error_t * |
Mohsin Kazmi | 95276ca | 2018-09-28 11:45:01 +0200 | [diff] [blame] | 1272 | vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource) |
| 1273 | { |
| 1274 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1275 | clib_error_t *error = 0; |
| 1276 | int fd = -1; |
| 1277 | u64 size = 0, offset = 0; |
| 1278 | |
| 1279 | if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset))) |
| 1280 | return error; |
| 1281 | |
| 1282 | p->io_fd = fd; |
| 1283 | p->io_offset = offset; |
| 1284 | return error; |
| 1285 | } |
| 1286 | |
| 1287 | clib_error_t * |
| 1288 | vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 1289 | vlib_read_or_write_t read_or_write, |
| 1290 | uword offset, void *data, u32 length) |
| 1291 | { |
| 1292 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1293 | int n = 0; |
| 1294 | |
| 1295 | if (read_or_write == VLIB_READ) |
| 1296 | n = pread (p->io_fd, data, length, p->io_offset + offset); |
| 1297 | else |
| 1298 | n = pwrite (p->io_fd, data, length, p->io_offset + offset); |
| 1299 | |
| 1300 | if (n != length) |
| 1301 | return clib_error_return_unix (0, "%s", |
| 1302 | read_or_write == VLIB_READ |
| 1303 | ? "read" : "write"); |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | clib_error_t * |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1308 | vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr) |
| 1309 | { |
| 1310 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1311 | |
| 1312 | if (!p->supports_va_dma) |
| 1313 | return 0; |
| 1314 | |
| 1315 | return vfio_map_physmem_page (vm, ptr); |
| 1316 | } |
| 1317 | |
| 1318 | int |
| 1319 | vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h) |
| 1320 | { |
| 1321 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1322 | |
| 1323 | return p->supports_va_dma != 0; |
| 1324 | } |
| 1325 | |
| 1326 | clib_error_t * |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1327 | 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] | 1328 | pci_device_id_t ids[], vlib_pci_dev_handle_t * handle) |
| 1329 | { |
| 1330 | linux_pci_main_t *lpm = &linux_pci_main; |
| 1331 | vlib_pci_device_info_t *di; |
| 1332 | linux_pci_device_t *p; |
| 1333 | clib_error_t *err = 0; |
| 1334 | pci_device_id_t *i; |
| 1335 | |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1336 | di = vlib_pci_get_device_info (vm, addr, &err); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1337 | |
| 1338 | if (err) |
| 1339 | return err; |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1340 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1341 | if (ids) |
| 1342 | { |
| 1343 | for (i = ids; i->vendor_id != 0; i++) |
| 1344 | if (i->vendor_id == di->vendor_id && i->device_id == di->device_id) |
| 1345 | break; |
| 1346 | |
| 1347 | if (i->vendor_id == 0) |
| 1348 | { |
| 1349 | vlib_pci_free_device_info (di); |
| 1350 | return clib_error_return (0, "Wrong vendor or device id"); |
| 1351 | } |
| 1352 | } |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1353 | |
| 1354 | pool_get (lpm->linux_pci_devices, p); |
| 1355 | p->handle = p - lpm->linux_pci_devices; |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1356 | p->addr.as_u32 = di->addr.as_u32; |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1357 | p->intx_irq.fd = -1; |
Mohsin Kazmi | 692f9b1 | 2019-04-02 11:09:49 +0000 | [diff] [blame] | 1358 | p->intx_irq.clib_file_index = -1; |
Damjan Marion | d2bfb78 | 2019-01-07 20:56:04 +0100 | [diff] [blame] | 1359 | p->numa_node = di->numa_node; |
Mohsin Kazmi | 3d3b955 | 2018-10-24 14:05:34 +0200 | [diff] [blame] | 1360 | /* |
| 1361 | * pci io bar read/write fd |
| 1362 | */ |
| 1363 | p->io_fd = -1; |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1364 | |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1365 | log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d", |
| 1366 | di->vendor_id, di->device_id, di->driver_name, di->iommu_group); |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1367 | |
Steven Luong | ab48992 | 2019-08-21 11:35:27 -0700 | [diff] [blame] | 1368 | if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1369 | err = add_device_vfio (vm, p, di, 0); |
Steven Luong | ab48992 | 2019-08-21 11:35:27 -0700 | [diff] [blame] | 1370 | 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] | 1371 | err = add_device_uio (vm, p, di, 0); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1372 | else |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1373 | err = clib_error_create ("device not bound to 'vfio-pci' or " |
| 1374 | "'uio_pci_generic' kernel module"); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1375 | if (err) |
| 1376 | goto error; |
| 1377 | |
| 1378 | *handle = p->handle; |
| 1379 | |
| 1380 | error: |
| 1381 | vlib_pci_free_device_info (di); |
| 1382 | if (err) |
| 1383 | { |
Damjan Marion | 00ea98a | 2023-07-28 13:19:49 +0200 | [diff] [blame] | 1384 | log_err (p, "%U", format_clib_error, err); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1385 | clib_memset (p, 0, sizeof (linux_pci_device_t)); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1386 | pool_put (lpm->linux_pci_devices, p); |
| 1387 | } |
| 1388 | |
| 1389 | return err; |
| 1390 | } |
| 1391 | |
| 1392 | void |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1393 | 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] | 1394 | { |
| 1395 | linux_pci_main_t *lpm = &linux_pci_main; |
| 1396 | linux_pci_device_t *p = linux_pci_get_device (h); |
| 1397 | linux_pci_irq_t *irq; |
| 1398 | linux_pci_region_t *res; |
| 1399 | clib_error_t *err = 0; |
| 1400 | |
| 1401 | if (p->type == LINUX_PCI_DEVICE_TYPE_UIO) |
| 1402 | { |
| 1403 | irq = &p->intx_irq; |
Mohsin Kazmi | 692f9b1 | 2019-04-02 11:09:49 +0000 | [diff] [blame] | 1404 | if (irq->clib_file_index != -1) |
| 1405 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1406 | close (p->config_fd); |
Mohsin Kazmi | 3d3b955 | 2018-10-24 14:05:34 +0200 | [diff] [blame] | 1407 | if (p->io_fd != -1) |
| 1408 | close (p->io_fd); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1409 | } |
| 1410 | else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO) |
| 1411 | { |
| 1412 | irq = &p->intx_irq; |
| 1413 | /* close INTx irqs */ |
| 1414 | if (irq->fd != -1) |
| 1415 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1416 | 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] | 1417 | VFIO_IRQ_SET_ACTION_TRIGGER, 0); |
| 1418 | clib_error_free (err); |
Mohsin Kazmi | 692f9b1 | 2019-04-02 11:09:49 +0000 | [diff] [blame] | 1419 | if (irq->clib_file_index != -1) |
| 1420 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1421 | close (irq->fd); |
| 1422 | } |
| 1423 | |
| 1424 | /* close MSI-X irqs */ |
| 1425 | if (vec_len (p->msix_irqs)) |
| 1426 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1427 | 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] | 1428 | VFIO_IRQ_SET_ACTION_TRIGGER, 0); |
| 1429 | clib_error_free (err); |
| 1430 | /* *INDENT-OFF* */ |
| 1431 | vec_foreach (irq, p->msix_irqs) |
| 1432 | { |
| 1433 | if (irq->fd == -1) |
| 1434 | continue; |
| 1435 | clib_file_del_by_index (&file_main, irq->clib_file_index); |
| 1436 | close (irq->fd); |
| 1437 | } |
| 1438 | /* *INDENT-ON* */ |
| 1439 | vec_free (p->msix_irqs); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | /* *INDENT-OFF* */ |
| 1444 | vec_foreach (res, p->regions) |
| 1445 | { |
| 1446 | if (res->size == 0) |
| 1447 | continue; |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 1448 | clib_mem_vm_unmap (res->addr); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1449 | if (res->fd != -1) |
| 1450 | close (res->fd); |
| 1451 | } |
| 1452 | /* *INDENT-ON* */ |
| 1453 | vec_free (p->regions); |
| 1454 | |
| 1455 | close (p->fd); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1456 | clib_memset (p, 0, sizeof (linux_pci_device_t)); |
Damjan Marion | f313b74 | 2018-03-05 14:08:33 +0100 | [diff] [blame] | 1457 | pool_put (lpm->linux_pci_devices, p); |
| 1458 | } |
| 1459 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1460 | void |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1461 | 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] | 1462 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1463 | vlib_pci_main_t *pm = &pci_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1464 | linux_pci_main_t *lpm = &linux_pci_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1465 | pci_device_registration_t *r; |
| 1466 | pci_device_id_t *i; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1467 | clib_error_t *err = 0; |
| 1468 | linux_pci_device_t *p; |
| 1469 | |
| 1470 | pool_get (lpm->linux_pci_devices, p); |
| 1471 | p->handle = p - lpm->linux_pci_devices; |
Damjan Marion | d5ded2d | 2018-03-05 10:18:50 +0100 | [diff] [blame] | 1472 | p->intx_irq.fd = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1473 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 1474 | r = pm->pci_device_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1475 | |
| 1476 | while (r) |
| 1477 | { |
| 1478 | for (i = r->supported_devices; i->vendor_id != 0; i++) |
Damjan Marion | 3a59382 | 2018-02-17 14:06:53 +0100 | [diff] [blame] | 1479 | 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] | 1480 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1481 | if (di->iommu_group != -1) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1482 | err = add_device_vfio (vm, p, di, r); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1483 | else |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1484 | err = add_device_uio (vm, p, di, r); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 1485 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1486 | if (err) |
| 1487 | clib_error_report (err); |
| 1488 | else |
| 1489 | return; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1490 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1491 | r = r->next_registration; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1492 | } |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1493 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1494 | /* No driver, close the PCI config-space FD */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1495 | clib_memset (p, 0, sizeof (linux_pci_device_t)); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1496 | pool_put (lpm->linux_pci_devices, p); |
| 1497 | } |
| 1498 | |
| 1499 | static clib_error_t * |
| 1500 | scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored) |
| 1501 | { |
| 1502 | vlib_pci_addr_t addr, **addrv = arg; |
| 1503 | unformat_input_t input; |
| 1504 | clib_error_t *err = 0; |
| 1505 | |
| 1506 | unformat_init_string (&input, (char *) dev_dir_name, |
| 1507 | vec_len (dev_dir_name)); |
| 1508 | |
| 1509 | if (!unformat (&input, "/sys/bus/pci/devices/%U", |
| 1510 | unformat_vlib_pci_addr, &addr)) |
| 1511 | err = clib_error_return (0, "unformat error `%v`", dev_dir_name); |
| 1512 | |
| 1513 | unformat_free (&input); |
| 1514 | |
| 1515 | if (err) |
| 1516 | return err; |
| 1517 | |
| 1518 | vec_add1 (*addrv, addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1522 | static int |
| 1523 | pci_addr_cmp (void *v1, void *v2) |
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 *a1 = v1; |
| 1526 | vlib_pci_addr_t *a2 = v2; |
| 1527 | |
| 1528 | if (a1->domain > a2->domain) |
| 1529 | return 1; |
| 1530 | if (a1->domain < a2->domain) |
| 1531 | return -1; |
| 1532 | if (a1->bus > a2->bus) |
| 1533 | return 1; |
| 1534 | if (a1->bus < a2->bus) |
| 1535 | return -1; |
| 1536 | if (a1->slot > a2->slot) |
| 1537 | return 1; |
| 1538 | if (a1->slot < a2->slot) |
| 1539 | return -1; |
| 1540 | if (a1->function > a2->function) |
| 1541 | return 1; |
| 1542 | if (a1->function < a2->function) |
| 1543 | return -1; |
| 1544 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1547 | vlib_pci_addr_t * |
| 1548 | vlib_pci_get_all_dev_addrs () |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1549 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1550 | vlib_pci_addr_t *addrs = 0; |
| 1551 | clib_error_t *err; |
| 1552 | err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr, |
| 1553 | &addrs, /* scan_dirs */ 0); |
| 1554 | if (err) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1555 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1556 | vec_free (addrs); |
| 1557 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1560 | vec_sort_with_function (addrs, pci_addr_cmp); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 1561 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1562 | return addrs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1565 | clib_error_t * |
| 1566 | linux_pci_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1567 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1568 | vlib_pci_main_t *pm = &pci_main; |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1569 | vlib_pci_addr_t *addr = 0, *addrs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1570 | |
| 1571 | pm->vlib_main = vm; |
| 1572 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1573 | ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 1574 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1575 | addrs = vlib_pci_get_all_dev_addrs (); |
| 1576 | /* *INDENT-OFF* */ |
| 1577 | vec_foreach (addr, addrs) |
| 1578 | { |
| 1579 | vlib_pci_device_info_t *d; |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1580 | if ((d = vlib_pci_get_device_info (vm, addr, 0))) |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1581 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 1582 | init_device_from_registered (vm, d); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 1583 | vlib_pci_free_device_info (d); |
| 1584 | } |
| 1585 | } |
| 1586 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1587 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 1588 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 1591 | /* *INDENT-OFF* */ |
| 1592 | VLIB_INIT_FUNCTION (linux_pci_init) = |
| 1593 | { |
| 1594 | .runs_after = VLIB_INITS("unix_input_init"), |
| 1595 | }; |
| 1596 | /* *INDENT-ON* */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1597 | |
| 1598 | /* |
| 1599 | * fd.io coding-style-patch-verification: ON |
| 1600 | * |
| 1601 | * Local Variables: |
| 1602 | * eval: (c-set-style "gnu") |
| 1603 | * End: |
| 1604 | */ |