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