blob: 7c18505bbfc2118c6045efbdce349e0a92113f8b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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 Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#include <vlib/vlib.h>
43#include <vlib/pci/pci.h>
44#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010045#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020051#include <sys/ioctl.h>
52#include <net/if.h>
53#include <linux/ethtool.h>
54#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020055#include <linux/vfio.h>
56#include <sys/eventfd.h>
57
58static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
59static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010060static char *sysfs_mod_vfio_noiommu =
61 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020062
Damjan Marion23227982018-10-22 13:38:57 +020063#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 Barach9b8ffd92016-07-08 08:13:45 -040070typedef struct
71{
Damjan Marion2060db82018-03-04 17:37:15 +010072 int fd;
73 void *addr;
74 size_t size;
75} linux_pci_region_t;
76
Damjan Marion599a16b2018-03-04 19:35:23 +010077typedef struct
78{
79 int fd;
80 u32 clib_file_index;
81 union
82 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010083 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010084 pci_msix_handler_function_t *msix_handler;
85 };
86} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010087
88typedef 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
95typedef struct
96{
97 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +020098 vlib_pci_dev_handle_t handle;
99 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100100 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200101
102 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100103 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200104
105 /* File descriptor for config space read/write. */
106 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100107 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200108
Damjan Marionf9a968e2018-02-25 23:26:22 +0100109 /* Device File descriptor */
110 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200111
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200112 /* read/write file descriptor for io bar */
113 int io_fd;
114 u64 io_offset;
115
Damjan Marion5a206ea2016-05-12 22:11:03 +0200116 /* Minor device for uio device. */
117 u32 uio_minor;
118
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100119 /* Interrupt handlers */
120 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100121 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200122
123 /* private data */
124 uword private_data;
125
Damjan Marion23227982018-10-22 13:38:57 +0200126 u8 supports_va_dma;
127
Damjan Marion5a206ea2016-05-12 22:11:03 +0200128} linux_pci_device_t;
129
130/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400131typedef struct
132{
133 vlib_main_t *vlib_main;
134 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200135
Damjan Marion5a206ea2016-05-12 22:11:03 +0200136} linux_pci_main_t;
137
138extern linux_pci_main_t linux_pci_main;
139
Damjan Marion599a16b2018-03-04 19:35:23 +0100140static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200141linux_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
147uword
Damjan Marion23227982018-10-22 13:38:57 +0200148vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200149{
150 linux_pci_device_t *d = linux_pci_get_device (h);
151 return d->private_data;
152}
153
154void
Damjan Marion23227982018-10-22 13:38:57 +0200155vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
156 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200157{
158 linux_pci_device_t *d = linux_pci_get_device (h);
159 d->private_data = private_data;
160}
161
162vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200163vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200164{
Damjan Marion599a16b2018-03-04 19:35:23 +0100165 linux_pci_device_t *d = linux_pci_get_device (h);
166 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200167}
168
Damjan Mariond2bfb782019-01-07 20:56:04 +0100169u32
170vlib_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 Kazmia402b832019-01-25 15:57:24 +0000176u32
177vlib_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 Marion5a206ea2016-05-12 22:11:03 +0200194/* 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 Barach9b8ffd92016-07-08 08:13:45 -0400197clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200198
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199linux_pci_main_t linux_pci_main;
200
Damjan Marioncef87f12017-10-05 15:32:41 +0200201vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200202vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
203 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200204{
Damjan Marioncef87f12017-10-05 15:32:41 +0200205 clib_error_t *err;
206 vlib_pci_device_info_t *di;
207 u8 *f = 0;
208 u32 tmp;
209 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800210 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800211 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200212
213 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400214 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200215 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 Marion44bcc202018-03-04 16:44:26 +0100235 if (read (fd, &di->config_data, sizeof (di->config_data)) <
236 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200237 {
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 Barachb7b92992018-10-17 10:38:51 -0400246 clib_memset (&all_ones, ~0, sizeof (all_ones));
Damjan Marioncef87f12017-10-05 15:32:41 +0200247
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 Wang76c61592020-01-13 17:15:13 +0800264 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200265 if (err)
266 {
267 di->numa_node = -1;
268 clib_error_free (err);
269 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800270 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 Marioncef87f12017-10-05 15:32:41 +0200280
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 Ganne0eae2bb2019-10-04 17:30:21 +0200305 if (!di->driver_name)
306 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200307
308 di->iommu_group = -1;
Yulong Pei45495482019-10-17 18:41:52 +0800309 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 Marioncef87f12017-10-05 15:32:41 +0200313 {
Yulong Pei45495482019-10-17 18:41:52 +0800314 di->iommu_group = atoi ((char *) tmpstr);
315 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200316 }
317
Yulong Pei45495482019-10-17 18:41:52 +0800318 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 Marion96504182017-12-10 23:54:46 +0100330 close (fd);
331
Damjan Marioncef87f12017-10-05 15:32:41 +0200332 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 Marion96504182017-12-10 23:54:46 +0100341 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200342
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
371error:
372 vlib_pci_free_device_info (di);
373 di = 0;
374
375done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800376 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200377 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 Marion2752b892017-12-11 15:55:56 +0100386static int
387directory_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 Mariona42cd342016-04-13 18:03:20 +0200396clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200397vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
398 char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400400 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200401 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200402 DIR *dir = 0;
403 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200404 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200405 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200406 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
407 format_vlib_pci_addr, addr);
408
Damjan Marion23227982018-10-22 13:38:57 +0200409 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200410
411 if (error)
412 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413
Damjan Marion2752b892017-12-11 15:55:56 +0100414 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 Mariona7f74572017-05-23 18:32:21 +0200457 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200458 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200459 vec_reset_length (s);
460
Damjan Mariona7f74572017-05-23 18:32:21 +0200461 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 Marion3c785e02017-05-08 18:37:54 +0200465 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200466
Damjan Mariona42cd342016-04-13 18:03:20 +0200467 /* 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 Marioncef87f12017-10-05 15:32:41 +0200470 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200471
472 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200474 error = clib_error_return (0, "Skipping PCI device %U: failed to "
475 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200476 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200477 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 }
479
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400481 if (fd < 0)
482 {
483 error = clib_error_return_unix (0, "socket");
484 goto done;
485 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200486
Dave Barach9b8ffd92016-07-08 08:13:45 -0400487 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200489 struct ifreq ifr;
490 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200493 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Dave Barachb7b92992018-10-17 10:38:51 -0400495 clib_memset (&ifr, 0, sizeof ifr);
496 clib_memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200497 ifr.ifr_data = (char *) &drvinfo;
Steven Luongddccf7b2019-01-18 22:53:18 -0800498 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
Stephen Hemminger4e8a8312019-01-16 10:23:16 -0800499
Damjan Mariona42cd342016-04-13 18:03:20 +0200500 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400501 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502 {
John Loe2821212016-07-13 18:13:02 -0400503 /* Some interfaces (eg "lo") don't support this ioctl */
504 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700505 clib_unix_warning ("ioctl fetch intf %s bus info error",
506 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400507 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400508 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200509
510 if (strcmp ((char *) s, drvinfo.bus_info))
511 continue;
512
Dave Barachb7b92992018-10-17 10:38:51 -0400513 clib_memset (&ifr, 0, sizeof (ifr));
Steven Luongddccf7b2019-01-18 22:53:18 -0800514 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
Stephen Hemminger4e8a8312019-01-16 10:23:16 -0800515
Chris Luke370e9e32016-07-07 11:01:17 -0400516 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400517 {
518 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
519 e->d_name);
520 close (fd);
521 goto done;
522 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200523
524 if (ifr.ifr_flags & IFF_UP)
525 {
Dave Barachecf98c02020-12-06 08:15:18 -0500526 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
527 "Skipping PCI device %U as host "
528 "interface %s is up", format_vlib_pci_addr, addr,
529 e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200531 goto done;
532 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 }
534
Damjan Mariona42cd342016-04-13 18:03:20 +0200535 close (fd);
536 vec_reset_length (s);
537
538 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200539 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200540 vec_reset_length (s);
541
Damjan Mariona7f74572017-05-23 18:32:21 +0200542 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
543 if (access ((char *) s, F_OK) == 0)
544 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200545 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200546 clear_driver_override = 1;
547 }
548 else
549 {
550 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200551 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
552 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
553 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200554 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200555 vec_reset_length (s);
556
Damjan Marioncef87f12017-10-05 15:32:41 +0200557 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
558 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200559 vec_reset_length (s);
560
561 if (clear_driver_override)
562 {
563 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200564 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200565 vec_reset_length (s);
566 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200567
568done:
569 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200571 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200572 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 return error;
574}
575
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
577static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400578scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 unformat_input_t input;
582
583 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
584
Dave Barach9b8ffd92016-07-08 08:13:45 -0400585 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586 abort ();
587
588 unformat_free (&input);
589 return 0;
590}
591
Dave Barach9b8ffd92016-07-08 08:13:45 -0400592static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200593vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
594 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100595{
596 int data_len = efds ? count * sizeof (int) : 0;
597 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200598 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100599 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
600
601
Damjan Marion23227982018-10-22 13:38:57 +0200602 ii.argsz = sizeof (struct vfio_irq_info);
603 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100604
Damjan Marion23227982018-10-22 13:38:57 +0200605 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100606 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
607 "'%U'", format_vlib_pci_addr, &p->addr);
608
Damjan Marion23227982018-10-22 13:38:57 +0200609 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
610 __func__, ii.index, ii.count,
611 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
612 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
613 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
614 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
615 ii.flags);
616
617 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100618 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
619 "'%U'", format_vlib_pci_addr, &p->addr);
620
621
622 if (efds)
623 {
624 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500625 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100626 }
627 else
628 flags |= VFIO_IRQ_SET_DATA_NONE;
629
630 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
631 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
632
633 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
634 irq_set->index = index;
635 irq_set->start = start;
636 irq_set->count = count;
637 irq_set->flags = flags;
638
639 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
640 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
641 "[index = %u, start = %u, count = %u, "
642 "flags = 0x%x]",
643 format_vlib_pci_addr, &p->addr,
644 index, start, count, flags);
645 return 0;
646}
647
648static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200649linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650{
Damjan Marion23227982018-10-22 13:38:57 +0200651 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200652 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200653 vlib_pci_dev_handle_t h = uf->private_data;
654 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100655 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Damjan Marion5a206ea2016-05-12 22:11:03 +0200657 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400658 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100660 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200661 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200662
Damjan Marion23227982018-10-22 13:38:57 +0200663 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664
665 return /* no error */ 0;
666}
667
Dave Barach9b8ffd92016-07-08 08:13:45 -0400668static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200669linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200670{
Damjan Marion23227982018-10-22 13:38:57 +0200671 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100672 VFIO_IRQ_SET_ACTION_UNMASK, 0);
673}
674
Damjan Marioncef87f12017-10-05 15:32:41 +0200675static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200676linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677{
678 u32 error_index = (u32) uf->private_data;
679
680 return clib_error_return (0, "pci device %d: error", error_index);
681}
682
Damjan Marioncef87f12017-10-05 15:32:41 +0200683static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100684linux_pci_vfio_msix_read_ready (clib_file_t * uf)
685{
Damjan Marion23227982018-10-22 13:38:57 +0200686 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100687 int __attribute__ ((unused)) rv;
688 vlib_pci_dev_handle_t h = uf->private_data >> 16;
689 u16 line = uf->private_data & 0xffff;
690 linux_pci_device_t *p = linux_pci_get_device (h);
691 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
692
693 u64 icount;
694 rv = read (uf->file_descriptor, &icount, sizeof (icount));
695
696 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200697 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100698
699 return /* no error */ 0;
700}
701
702static clib_error_t *
703linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704{
Damjan Marion23227982018-10-22 13:38:57 +0200705 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200706 int __attribute__ ((unused)) rv;
707 vlib_pci_dev_handle_t h = uf->private_data;
708 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100709 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
Damjan Marioncef87f12017-10-05 15:32:41 +0200711 u64 icount;
712 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100714 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200715 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
Damjan Marion23227982018-10-22 13:38:57 +0200717 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
Damjan Marioncef87f12017-10-05 15:32:41 +0200719 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720}
721
Damjan Marioncef87f12017-10-05 15:32:41 +0200722static clib_error_t *
723linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724{
Damjan Marioncef87f12017-10-05 15:32:41 +0200725 u32 error_index = (u32) uf->private_data;
726
727 return clib_error_return (0, "pci device %d: error", error_index);
728}
729
730static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200731add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
732 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200733{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100734 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200735 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200736 u8 *s = 0;
737
Damjan Marionf9a968e2018-02-25 23:26:22 +0100738 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100739 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200740
741 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
742 format_vlib_pci_addr, &di->addr, 0);
743
744 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100745 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200746 vec_reset_length (s);
747
748 if (p->config_fd == -1)
749 {
750 err = clib_error_return_unix (0, "open '%s'", s);
751 goto error;
752 }
753
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100754 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
755 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200756 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
757 1);
758 vec_reset_length (s);
759
760 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100761 p->fd = open ((char *) s, O_RDWR);
762 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200763 {
764 err = clib_error_return_unix (0, "open '%s'", s);
765 goto error;
766 }
767
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100768 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200769 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200770
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100771 if (r && r->init_function)
772 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200773
774error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200775 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200776 if (err)
777 {
Damjan Marion96504182017-12-10 23:54:46 +0100778 if (p->config_fd != -1)
779 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100780 if (p->fd != -1)
781 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200782 }
783 return err;
784}
785
Damjan Marion599a16b2018-03-04 19:35:23 +0100786clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200787vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100788 pci_intx_handler_function_t * intx_handler)
789{
790 linux_pci_device_t *p = linux_pci_get_device (h);
791 clib_file_t t = { 0 };
792 linux_pci_irq_t *irq = &p->intx_irq;
793 ASSERT (irq->fd == -1);
794
795 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
796 {
Damjan Marion23227982018-10-22 13:38:57 +0200797 struct vfio_irq_info ii = { 0 };
798 ii.argsz = sizeof (struct vfio_irq_info);
799 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
800 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100801 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
802 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200803 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
804 __func__, ii.index, ii.count,
805 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
806 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
807 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
808 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
809 ii.flags);
810 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100811 return clib_error_return (0, "INTx interrupt does not exist on device"
812 "'%U'", format_vlib_pci_addr, &p->addr);
813
814 irq->fd = eventfd (0, EFD_NONBLOCK);
815 if (irq->fd == -1)
816 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200817
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100818 t.file_descriptor = irq->fd;
819 t.read_function = linux_pci_vfio_intx_read_ready;
820 }
821 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
822 {
823 t.file_descriptor = p->fd;
824 t.read_function = linux_pci_uio_read_ready;
825 }
826 else
827 return 0;
828
829 t.error_function = linux_pci_uio_error_ready;
830 t.private_data = p->handle;
831 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
832 irq->clib_file_index = clib_file_add (&file_main, &t);
833 irq->intx_handler = intx_handler;
834 return 0;
835}
836
837clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200838vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
839 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100840 pci_msix_handler_function_t * msix_handler)
841{
842 clib_error_t *err = 0;
843 linux_pci_device_t *p = linux_pci_get_device (h);
844 u32 i;
845
846 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
847 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
848 "support");
849
850 /* *INDENT-OFF* */
851 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
852 { .fd = -1});
853 /* *INDENT-ON* */
854
855 for (i = start; i < start + count; i++)
856 {
857 clib_file_t t = { 0 };
858 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
859 ASSERT (irq->fd == -1);
860
861 irq->fd = eventfd (0, EFD_NONBLOCK);
862 if (irq->fd == -1)
863 {
864 err = clib_error_return_unix (0, "eventfd");
865 goto error;
866 }
867
868 t.read_function = linux_pci_vfio_msix_read_ready;
869 t.file_descriptor = irq->fd;
870 t.error_function = linux_pci_vfio_error_ready;
871 t.private_data = p->handle << 16 | i;
872 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
873 &p->addr, i);
874 irq->clib_file_index = clib_file_add (&file_main, &t);
875 irq->msix_handler = msix_handler;
876 }
877
878 return 0;
879
880error:
881 while (i-- > start)
882 {
883 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
884 if (irq->fd != -1)
885 {
886 clib_file_del_by_index (&file_main, irq->clib_file_index);
887 close (irq->fd);
888 irq->fd = -1;
889 }
890 }
891 return err;
892}
893
894clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200895vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
896 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100897{
898 linux_pci_device_t *p = linux_pci_get_device (h);
899 int fds[count];
900 int i;
901
902 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
903 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
904 "support");
905
906 for (i = start; i < start + count; i++)
907 {
908 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
909 fds[i] = irq->fd;
910 }
911
Damjan Marion23227982018-10-22 13:38:57 +0200912 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100913 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
914}
915
Damjan Marion9c9490c2020-10-07 20:00:39 +0200916uword
917vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
918 u16 index)
919{
920 linux_pci_device_t *p = linux_pci_get_device (h);
921 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
922 if (irq->fd == -1)
923 return ~0;
924 return irq->clib_file_index;
925}
926
Damjan Marion599a16b2018-03-04 19:35:23 +0100927clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200928vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
929 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100930{
931 linux_pci_device_t *p = linux_pci_get_device (h);
932 int i, fds[count];
933
934 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
935 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
936 "support");
937
938 for (i = start; i < start + count; i++)
939 fds[i] = -1;
940
Damjan Marion23227982018-10-22 13:38:57 +0200941 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100942 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
943}
944
Damjan Marioncef87f12017-10-05 15:32:41 +0200945static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200946add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
947 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200948{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100949 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100950 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +0100951 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200952 clib_error_t *err = 0;
953 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +0200954 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +0200955
Damjan Marion2060db82018-03-04 17:37:15 +0100956 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200957
Damjan Marion23227982018-10-22 13:38:57 +0200958 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +0200959 return err;
960
Damjan Marion23227982018-10-22 13:38:57 +0200961 if (is_noiommu == 0)
962 p->supports_va_dma = 1;
963
Damjan Marioncef87f12017-10-05 15:32:41 +0200964 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100965 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200966 {
967 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
968 format_vlib_pci_addr, &di->addr);
969 goto error;
970 }
971
Damjan Marionf313b742018-03-05 14:08:33 +0100972 reg.argsz = sizeof (struct vfio_region_info);
973 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
974 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
975 {
976 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
977 format_vlib_pci_addr, &di->addr);
978 goto error;
979 }
Damjan Marion23227982018-10-22 13:38:57 +0200980
Damjan Marion0792bb42020-05-25 19:31:42 +0200981 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +0200982
Damjan Marionf313b742018-03-05 14:08:33 +0100983 p->config_offset = reg.offset;
984 p->config_fd = p->fd;
985
Damjan Marioncef87f12017-10-05 15:32:41 +0200986 /* reset if device supports it */
987 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100988 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200989 {
990 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
991 format_vlib_pci_addr, &di->addr);
992 goto error;
993 }
994
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100995 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200996 {
Damjan Marion23227982018-10-22 13:38:57 +0200997 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
998 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +0200999 }
1000
Damjan Marion68b4da62018-09-30 18:26:20 +02001001 if (p->supports_va_dma)
1002 {
1003 vlib_buffer_pool_t *bp;
1004 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001005 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001006 {
1007 u32 i;
1008 vlib_physmem_map_t *pm;
1009 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1010 for (i = 0; i < pm->n_pages; i++)
1011 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1012 }
1013 /* *INDENT-ON* */
1014 }
1015
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001016 if (r && r->init_function)
1017 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001018
1019error:
1020 vec_free (s);
1021 if (err)
1022 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001023 if (p->fd != -1)
1024 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001025 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001026 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001027 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001028 }
1029 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030}
1031
1032/* Configuration space read/write. */
1033clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001034vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001035 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001036 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037{
Damjan Marioncef87f12017-10-05 15:32:41 +02001038 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039 int n;
1040
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001042 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001044 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045
1046 if (n != n_bytes)
1047 return clib_error_return_unix (0, "%s",
1048 read_or_write == VLIB_READ
1049 ? "read" : "write");
1050
1051 return 0;
1052}
1053
1054static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001055vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1056 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057{
Damjan Marioncef87f12017-10-05 15:32:41 +02001058 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001059 clib_error_t *error = 0;
1060 int _fd = -1;
1061 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001062
1063 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064
1065 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066
Damjan Marion2060db82018-03-04 17:37:15 +01001067 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068 {
Damjan Marion2060db82018-03-04 17:37:15 +01001069 u8 *file_name;
1070 struct stat stat_buf;
1071 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1072 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001074 _fd = open ((char *) file_name, O_RDWR);
1075 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001076 {
1077 error = clib_error_return_unix (0, "open `%s'", file_name);
1078 vec_free (file_name);
1079 return error;
1080 }
1081
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001082 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001083 {
1084 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1085 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001086 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001087 return error;
1088 }
1089
1090 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001091 _size = stat_buf.st_size;
1092 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001093 }
1094 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001096 struct vfio_region_info *r;
1097 u32 sz = sizeof (struct vfio_region_info);
1098 again:
1099 r = clib_mem_alloc (sz);
1100 clib_memset (r, 0, sz);
1101 r->argsz = sz;
1102 r->index = bar;
1103 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001104 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1105 "'%U'", format_vlib_pci_addr,
1106 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001107 if (sz != r->argsz)
1108 {
1109 sz = r->argsz;
1110 clib_mem_free (r);
1111 goto again;
1112 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001113 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001114 _size = r->size;
1115 _offset = r->offset;
1116 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, r);
1117 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 }
Damjan Marion2060db82018-03-04 17:37:15 +01001119 else
1120 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001122 *fd = _fd;
1123 *size = _size;
1124 *offset = _offset;
1125
1126 return error;
1127}
1128
1129static clib_error_t *
1130vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1131 u32 bar, u8 * addr, void **result)
1132{
1133 linux_pci_device_t *p = linux_pci_get_device (h);
1134 int fd = -1;
1135 clib_error_t *error;
1136 int flags = MAP_SHARED;
1137 u64 size = 0, offset = 0;
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001138 u16 command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001139
1140 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1141
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001142 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command)))
1143 return error;
1144
1145 if (!(command & PCI_COMMAND_MEMORY))
1146 {
1147 pci_log_debug (vm, p, "setting memory enable bit");
1148 command |= PCI_COMMAND_MEMORY;
1149 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command)))
1150 return error;
1151 }
1152
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001153 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1154 return error;
1155
1156 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1157 flags |= MAP_FIXED;
1158
Damjan Marion6bfd0762020-09-11 22:16:53 +02001159 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1160 "PCIe %U region %u", format_vlib_pci_addr,
1161 vlib_pci_get_addr (vm, h), bar);
1162 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163 {
Damjan Marion2060db82018-03-04 17:37:15 +01001164 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001165 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001167 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 }
Damjan Marion2060db82018-03-04 17:37:15 +01001169
1170 /* *INDENT-OFF* */
1171 vec_validate_init_empty (p->regions, bar,
1172 (linux_pci_region_t) { .fd = -1});
1173 /* *INDENT-ON* */
1174 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1175 p->regions[bar].fd = fd;
1176 p->regions[bar].addr = *result;
1177 p->regions[bar].size = size;
1178 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179}
1180
1181clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001182vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1183 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184{
Damjan Marion23227982018-10-22 13:38:57 +02001185 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186}
1187
1188clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001189vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1190 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191{
Damjan Marion23227982018-10-22 13:38:57 +02001192 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193}
1194
Damjan Marionf313b742018-03-05 14:08:33 +01001195clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001196vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1197{
1198 linux_pci_device_t *p = linux_pci_get_device (h);
1199 clib_error_t *error = 0;
1200 int fd = -1;
1201 u64 size = 0, offset = 0;
1202
1203 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1204 return error;
1205
1206 p->io_fd = fd;
1207 p->io_offset = offset;
1208 return error;
1209}
1210
1211clib_error_t *
1212vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1213 vlib_read_or_write_t read_or_write,
1214 uword offset, void *data, u32 length)
1215{
1216 linux_pci_device_t *p = linux_pci_get_device (h);
1217 int n = 0;
1218
1219 if (read_or_write == VLIB_READ)
1220 n = pread (p->io_fd, data, length, p->io_offset + offset);
1221 else
1222 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1223
1224 if (n != length)
1225 return clib_error_return_unix (0, "%s",
1226 read_or_write == VLIB_READ
1227 ? "read" : "write");
1228 return 0;
1229}
1230
1231clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001232vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1233{
1234 linux_pci_device_t *p = linux_pci_get_device (h);
1235
1236 if (!p->supports_va_dma)
1237 return 0;
1238
1239 return vfio_map_physmem_page (vm, ptr);
1240}
1241
1242int
1243vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1244{
1245 linux_pci_device_t *p = linux_pci_get_device (h);
1246
1247 return p->supports_va_dma != 0;
1248}
1249
1250clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001251vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001252 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1253{
1254 linux_pci_main_t *lpm = &linux_pci_main;
1255 vlib_pci_device_info_t *di;
1256 linux_pci_device_t *p;
1257 clib_error_t *err = 0;
1258 pci_device_id_t *i;
1259
Damjan Marion23227982018-10-22 13:38:57 +02001260 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001261
1262 if (err)
1263 return err;
1264 for (i = ids; i->vendor_id != 0; i++)
1265 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1266 break;
1267
1268 if (i->vendor_id == 0)
1269 return clib_error_return (0, "Wrong vendor or device id");
1270
1271 pool_get (lpm->linux_pci_devices, p);
1272 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001273 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001274 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001275 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001276 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001277 /*
1278 * pci io bar read/write fd
1279 */
1280 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001281
Damjan Marion23227982018-10-22 13:38:57 +02001282 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1283 di->vendor_id, di->device_id, di->driver_name,
1284 di->iommu_group);
1285
Steven Luongab489922019-08-21 11:35:27 -07001286 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001287 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001288 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001289 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001290 else
Damjan Marion23227982018-10-22 13:38:57 +02001291 err = clib_error_create ("device not bound to 'vfio-pci' or "
1292 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001293 if (err)
1294 goto error;
1295
1296 *handle = p->handle;
1297
1298error:
1299 vlib_pci_free_device_info (di);
1300 if (err)
1301 {
Damjan Marion23227982018-10-22 13:38:57 +02001302 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001303 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001304 pool_put (lpm->linux_pci_devices, p);
1305 }
1306
1307 return err;
1308}
1309
1310void
Damjan Marion23227982018-10-22 13:38:57 +02001311vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001312{
1313 linux_pci_main_t *lpm = &linux_pci_main;
1314 linux_pci_device_t *p = linux_pci_get_device (h);
1315 linux_pci_irq_t *irq;
1316 linux_pci_region_t *res;
1317 clib_error_t *err = 0;
1318
1319 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1320 {
1321 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001322 if (irq->clib_file_index != -1)
1323 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001324 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001325 if (p->io_fd != -1)
1326 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001327 }
1328 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1329 {
1330 irq = &p->intx_irq;
1331 /* close INTx irqs */
1332 if (irq->fd != -1)
1333 {
Damjan Marion23227982018-10-22 13:38:57 +02001334 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001335 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1336 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001337 if (irq->clib_file_index != -1)
1338 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001339 close (irq->fd);
1340 }
1341
1342 /* close MSI-X irqs */
1343 if (vec_len (p->msix_irqs))
1344 {
Damjan Marion23227982018-10-22 13:38:57 +02001345 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001346 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1347 clib_error_free (err);
1348 /* *INDENT-OFF* */
1349 vec_foreach (irq, p->msix_irqs)
1350 {
1351 if (irq->fd == -1)
1352 continue;
1353 clib_file_del_by_index (&file_main, irq->clib_file_index);
1354 close (irq->fd);
1355 }
1356 /* *INDENT-ON* */
1357 vec_free (p->msix_irqs);
1358 }
1359 }
1360
1361 /* *INDENT-OFF* */
1362 vec_foreach (res, p->regions)
1363 {
1364 if (res->size == 0)
1365 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001366 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001367 if (res->fd != -1)
1368 close (res->fd);
1369 }
1370 /* *INDENT-ON* */
1371 vec_free (p->regions);
1372
1373 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001374 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001375 pool_put (lpm->linux_pci_devices, p);
1376}
1377
Dave Barach9b8ffd92016-07-08 08:13:45 -04001378void
Damjan Marion23227982018-10-22 13:38:57 +02001379init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001380{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001381 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001382 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001383 pci_device_registration_t *r;
1384 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001385 clib_error_t *err = 0;
1386 linux_pci_device_t *p;
1387
1388 pool_get (lpm->linux_pci_devices, p);
1389 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001390 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391
Damjan Marion5a206ea2016-05-12 22:11:03 +02001392 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393
1394 while (r)
1395 {
1396 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001397 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001398 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001399 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001400 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001401 else
Damjan Marion23227982018-10-22 13:38:57 +02001402 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001403
Damjan Marioncef87f12017-10-05 15:32:41 +02001404 if (err)
1405 clib_error_report (err);
1406 else
1407 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001408 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001410 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001411
Ed Warnickecb9cada2015-12-08 15:45:58 -07001412 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001413 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001414 pool_put (lpm->linux_pci_devices, p);
1415}
1416
1417static clib_error_t *
1418scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1419{
1420 vlib_pci_addr_t addr, **addrv = arg;
1421 unformat_input_t input;
1422 clib_error_t *err = 0;
1423
1424 unformat_init_string (&input, (char *) dev_dir_name,
1425 vec_len (dev_dir_name));
1426
1427 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1428 unformat_vlib_pci_addr, &addr))
1429 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1430
1431 unformat_free (&input);
1432
1433 if (err)
1434 return err;
1435
1436 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001437 return 0;
1438}
1439
Damjan Marioncef87f12017-10-05 15:32:41 +02001440static int
1441pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442{
Damjan Marioncef87f12017-10-05 15:32:41 +02001443 vlib_pci_addr_t *a1 = v1;
1444 vlib_pci_addr_t *a2 = v2;
1445
1446 if (a1->domain > a2->domain)
1447 return 1;
1448 if (a1->domain < a2->domain)
1449 return -1;
1450 if (a1->bus > a2->bus)
1451 return 1;
1452 if (a1->bus < a2->bus)
1453 return -1;
1454 if (a1->slot > a2->slot)
1455 return 1;
1456 if (a1->slot < a2->slot)
1457 return -1;
1458 if (a1->function > a2->function)
1459 return 1;
1460 if (a1->function < a2->function)
1461 return -1;
1462 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463}
1464
Damjan Marioncef87f12017-10-05 15:32:41 +02001465vlib_pci_addr_t *
1466vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001467{
Damjan Marioncef87f12017-10-05 15:32:41 +02001468 vlib_pci_addr_t *addrs = 0;
1469 clib_error_t *err;
1470 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1471 &addrs, /* scan_dirs */ 0);
1472 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001474 vec_free (addrs);
1475 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476 }
1477
Damjan Marioncef87f12017-10-05 15:32:41 +02001478 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001479
Damjan Marioncef87f12017-10-05 15:32:41 +02001480 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001481}
1482
Dave Barach9b8ffd92016-07-08 08:13:45 -04001483clib_error_t *
1484linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001486 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001487 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488
1489 pm->vlib_main = vm;
1490
Dave Barach9b8ffd92016-07-08 08:13:45 -04001491 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001492
Damjan Marioncef87f12017-10-05 15:32:41 +02001493 addrs = vlib_pci_get_all_dev_addrs ();
1494 /* *INDENT-OFF* */
1495 vec_foreach (addr, addrs)
1496 {
1497 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001498 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001499 {
Damjan Marion23227982018-10-22 13:38:57 +02001500 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001501 vlib_pci_free_device_info (d);
1502 }
1503 }
1504 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505
Dave Barachf8d50682019-05-14 18:01:44 -04001506 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507}
1508
Dave Barachf8d50682019-05-14 18:01:44 -04001509/* *INDENT-OFF* */
1510VLIB_INIT_FUNCTION (linux_pci_init) =
1511{
1512 .runs_after = VLIB_INITS("unix_input_init"),
1513};
1514/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001515
1516/*
1517 * fd.io coding-style-patch-verification: ON
1518 *
1519 * Local Variables:
1520 * eval: (c-set-style "gnu")
1521 * End:
1522 */