2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * pci.c: Linux user space PCI bus management.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vppinfra/linux/sysfs.h>
42 #include <vlib/vlib.h>
43 #include <vlib/pci/pci.h>
44 #include <vlib/unix/unix.h>
46 #include <sys/types.h>
50 #include <sys/ioctl.h>
52 #include <linux/ethtool.h>
53 #include <linux/sockios.h>
54 #include <linux/vfio.h>
55 #include <sys/eventfd.h>
57 static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
58 static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
62 vlib_pci_dev_handle_t handle;
65 /* Resource file descriptors. */
68 /* File descriptor for config space read/write. */
71 /* File descriptor for /dev/uio%d */
74 /* Minor device for uio device. */
80 /* Index given by clib_file_add. */
83 /* Interrupt handler */
84 void (*interrupt_handler) (vlib_pci_dev_handle_t h);
96 } linux_pci_vfio_iommu_group_t;
98 /* Pool of PCI devices. */
101 vlib_main_t *vlib_main;
102 linux_pci_device_t *linux_pci_devices;
105 int vfio_container_fd;
108 /* pool of IOMMU groups */
109 linux_pci_vfio_iommu_group_t *iommu_groups;
111 /* iommu group pool index by group id hash */
112 uword *iommu_pool_index_by_group;
116 extern linux_pci_main_t linux_pci_main;
119 linux_pci_get_device (vlib_pci_dev_handle_t h)
121 linux_pci_main_t *lpm = &linux_pci_main;
122 return pool_elt_at_index (lpm->linux_pci_devices, h);
126 vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
128 linux_pci_device_t *d = linux_pci_get_device (h);
129 return d->private_data;
133 vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
135 linux_pci_device_t *d = linux_pci_get_device (h);
136 d->private_data = private_data;
140 vlib_pci_get_addr (vlib_pci_dev_handle_t h)
142 linux_pci_device_t *lpm = linux_pci_get_device (h);
146 /* Call to allocate/initialize the pci subsystem.
147 This is not an init function so that users can explicitly enable
148 pci only when it's needed. */
149 clib_error_t *pci_bus_init (vlib_main_t * vm);
151 linux_pci_main_t linux_pci_main;
153 vlib_pci_device_info_t *
154 vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
156 linux_pci_main_t *lpm = &linux_pci_main;
158 vlib_pci_device_info_t *di;
163 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
164 memset (di, 0, sizeof (vlib_pci_device_info_t));
165 di->addr.as_u32 = addr->as_u32;
167 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
168 format_vlib_pci_addr, addr);
170 f = format (0, "%v/config%c", dev_dir_name, 0);
171 fd = open ((char *) f, O_RDWR);
173 /* Try read-only access if write fails. */
175 fd = open ((char *) f, O_RDONLY);
179 err = clib_error_return_unix (0, "open `%s'", f);
183 /* You can only read more that 64 bytes of config space as root; so we try to
184 read the full space but fall back to just the first 64 bytes. */
185 if (read (fd, &di->config_data, sizeof (di->config_data)) !=
186 sizeof (di->config_data)
187 && read (fd, &di->config0,
188 sizeof (di->config0)) != sizeof (di->config0))
190 err = clib_error_return_unix (0, "read `%s'", f);
196 static pci_config_header_t all_ones;
197 if (all_ones.vendor_id == 0)
198 memset (&all_ones, ~0, sizeof (all_ones));
200 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
202 err = clib_error_return (0, "invalid PCI config for `%s'", f);
208 if (di->config0.header.header_type == 0)
209 pci_config_type0_little_to_host (&di->config0);
211 pci_config_type1_little_to_host (&di->config1);
214 vec_reset_length (f);
215 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
216 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
220 clib_error_free (err);
223 vec_reset_length (f);
224 f = format (f, "%v/class%c", dev_dir_name, 0);
225 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
228 di->device_class = tmp >> 8;
230 vec_reset_length (f);
231 f = format (f, "%v/vendor%c", dev_dir_name, 0);
232 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
237 vec_reset_length (f);
238 f = format (f, "%v/device%c", dev_dir_name, 0);
239 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
244 vec_reset_length (f);
245 f = format (f, "%v/driver%c", dev_dir_name, 0);
246 di->driver_name = clib_sysfs_link_to_name ((char *) f);
248 di->iommu_group = -1;
249 if (lpm->vfio_container_fd != -1)
252 vec_reset_length (f);
253 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
254 tmpstr = clib_sysfs_link_to_name ((char *) f);
257 di->iommu_group = atoi ((char *) tmpstr);
262 vec_reset_length (f);
263 f = format (f, "%v/vpd%c", dev_dir_name, 0);
264 fd = open ((char *) f, O_RDONLY);
273 if (read (fd, &tag, 3) != 3)
276 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
279 len = (tag[2] << 8) | tag[1];
280 vec_validate (data, len);
282 if (read (fd, data, len) != len)
288 di->product_name = data;
289 else if (tag[0] == 0x90)
291 else if (tag[0] == 0x91)
302 vlib_pci_free_device_info (di);
307 vec_free (dev_dir_name);
311 clib_error_free (err);
316 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
318 clib_error_t *error = 0;
319 u8 *s = 0, *driver_name = 0;
322 vlib_pci_device_info_t *di;
323 int fd, clear_driver_override = 0;
324 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
325 format_vlib_pci_addr, addr);
327 di = vlib_pci_get_device_info (addr, &error);
332 s = format (s, "%v/driver%c", dev_dir_name, 0);
333 driver_name = clib_sysfs_link_to_name ((char *) s);
334 vec_reset_length (s);
337 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
338 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
339 (strcmp ("igb_uio", (char *) driver_name) == 0)))
342 /* walk trough all linux interfaces and if interface belonging to
343 this device is founf check if interface is admin up */
344 dir = opendir ("/sys/class/net");
345 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
349 error = clib_error_return (0, "Skipping PCI device %U: failed to "
350 "read /sys/class/net",
351 format_vlib_pci_addr, addr);
355 fd = socket (PF_INET, SOCK_DGRAM, 0);
358 error = clib_error_return_unix (0, "socket");
362 while ((e = readdir (dir)))
365 struct ethtool_drvinfo drvinfo;
367 if (e->d_name[0] == '.') /* skip . and .. */
370 memset (&ifr, 0, sizeof ifr);
371 memset (&drvinfo, 0, sizeof drvinfo);
372 ifr.ifr_data = (char *) &drvinfo;
373 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
374 drvinfo.cmd = ETHTOOL_GDRVINFO;
375 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
377 /* Some interfaces (eg "lo") don't support this ioctl */
378 if ((errno != ENOTSUP) && (errno != ENODEV))
379 clib_unix_warning ("ioctl fetch intf %s bus info error",
384 if (strcmp ((char *) s, drvinfo.bus_info))
387 memset (&ifr, 0, sizeof (ifr));
388 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
389 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
391 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
397 if (ifr.ifr_flags & IFF_UP)
399 error = clib_error_return (0, "Skipping PCI device %U as host "
400 "interface %s is up",
401 format_vlib_pci_addr, addr, e->d_name);
408 vec_reset_length (s);
410 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
411 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
412 vec_reset_length (s);
414 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
415 if (access ((char *) s, F_OK) == 0)
417 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
418 clear_driver_override = 1;
422 vec_reset_length (s);
423 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
424 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
427 vec_reset_length (s);
429 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
430 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
431 vec_reset_length (s);
433 if (clear_driver_override)
435 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
436 clib_sysfs_write ((char *) s, "%c", 0);
437 vec_reset_length (s);
443 vec_free (dev_dir_name);
444 vec_free (driver_name);
449 static clib_error_t *
450 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
452 linux_pci_device_t *l = arg;
453 unformat_input_t input;
455 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
457 if (!unformat (&input, "uio%d", &l->uio_minor))
460 unformat_free (&input);
464 static clib_error_t *
465 linux_pci_uio_read_ready (clib_file_t * uf)
467 int __attribute__ ((unused)) rv;
468 vlib_pci_dev_handle_t h = uf->private_data;
469 linux_pci_device_t *p = linux_pci_get_device (h);
472 rv = read (uf->file_descriptor, &icount, 4);
474 if (p->interrupt_handler)
475 p->interrupt_handler (h);
477 vlib_pci_intr_enable (h);
479 return /* no error */ 0;
482 static clib_error_t *
483 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
485 clib_error_t *err = 0;
486 struct vfio_irq_set i = {
487 .argsz = sizeof (struct vfio_irq_set),
490 .index = VFIO_PCI_INTX_IRQ_INDEX,
491 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
494 if (ioctl (d->vfio_device_fd, VFIO_DEVICE_SET_IRQS, &i) < 0)
496 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
497 format_vlib_pci_addr, &d->addr);
502 static clib_error_t *
503 linux_pci_uio_error_ready (clib_file_t * uf)
505 u32 error_index = (u32) uf->private_data;
507 return clib_error_return (0, "pci device %d: error", error_index);
510 static clib_error_t *
511 linux_pci_vfio_read_ready (clib_file_t * uf)
513 int __attribute__ ((unused)) rv;
514 vlib_pci_dev_handle_t h = uf->private_data;
515 linux_pci_device_t *p = linux_pci_get_device (h);
518 rv = read (uf->file_descriptor, &icount, sizeof (icount));
520 if (p->interrupt_handler)
521 p->interrupt_handler (h);
523 linux_pci_vfio_unmask_intx (p);
525 return /* no error */ 0;
528 static clib_error_t *
529 linux_pci_vfio_error_ready (clib_file_t * uf)
531 u32 error_index = (u32) uf->private_data;
533 return clib_error_return (0, "pci device %d: error", error_index);
536 static clib_error_t *
537 add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
538 vlib_pci_device_info_t * di, pci_device_registration_t * r)
540 clib_error_t *err = 0;
541 clib_file_t t = { 0 };
544 p->addr.as_u32 = di->addr.as_u32;
547 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
548 format_vlib_pci_addr, &di->addr, 0);
550 p->config_fd = open ((char *) s, O_RDWR);
551 vec_reset_length (s);
553 if (p->config_fd == -1)
555 err = clib_error_return_unix (0, "open '%s'", s);
559 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
560 format_vlib_pci_addr, &di->addr);
561 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
563 vec_reset_length (s);
565 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
566 p->uio_fd = open ((char *) s, O_RDWR);
569 err = clib_error_return_unix (0, "open '%s'", s);
573 t.read_function = linux_pci_uio_read_ready;
574 t.file_descriptor = p->uio_fd;
575 t.error_function = linux_pci_uio_error_ready;
576 t.private_data = p->handle;
578 p->clib_file_index = clib_file_add (&file_main, &t);
579 p->interrupt_handler = r->interrupt_handler;
580 err = r->init_function (vm, p->handle);
586 close (p->config_fd);
592 static linux_pci_vfio_iommu_group_t *
593 get_vfio_iommu_group (int group)
595 linux_pci_main_t *lpm = &linux_pci_main;
598 p = hash_get (lpm->iommu_pool_index_by_group, group);
600 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
603 static clib_error_t *
604 open_vfio_iommu_group (int group)
606 linux_pci_main_t *lpm = &linux_pci_main;
607 linux_pci_vfio_iommu_group_t *g;
608 clib_error_t *err = 0;
609 struct vfio_group_status group_status;
613 g = get_vfio_iommu_group (group);
619 s = format (s, "/dev/vfio/%u%c", group, 0);
620 fd = open ((char *) s, O_RDWR);
622 clib_error_return_unix (0, "open '%s'", s);
624 group_status.argsz = sizeof (group_status);
625 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
627 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
632 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
634 err = clib_error_return (0, "iommu group %d is not viable (not all "
635 "devices in this group bound to vfio-pci)",
640 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
642 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
647 if (lpm->vfio_iommu_mode == 0)
649 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
652 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
656 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
660 pool_get (lpm->iommu_groups, g);
663 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
671 static clib_error_t *
672 add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
673 vlib_pci_device_info_t * di, pci_device_registration_t * r)
675 linux_pci_vfio_iommu_group_t *g;
676 struct vfio_device_info device_info;
677 struct vfio_irq_info irq_info = { 0 };
678 clib_error_t *err = 0;
682 p->addr.as_u32 = di->addr.as_u32;
684 if (di->driver_name == 0 ||
685 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
686 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
687 "vfio-pci", format_vlib_pci_addr, &di->addr,
690 if ((err = open_vfio_iommu_group (di->iommu_group)))
693 g = get_vfio_iommu_group (di->iommu_group);
695 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
696 if ((dfd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
698 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
699 format_vlib_pci_addr, &di->addr);
702 vec_reset_length (s);
703 p->vfio_device_fd = dfd;
705 device_info.argsz = sizeof (device_info);
706 if (ioctl (dfd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
708 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
709 format_vlib_pci_addr, &di->addr);
713 irq_info.argsz = sizeof (struct vfio_irq_info);
714 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
715 if (ioctl (dfd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
717 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
718 "'%U'", format_vlib_pci_addr, &di->addr);
722 /* reset if device supports it */
723 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
724 if (ioctl (dfd, VFIO_DEVICE_RESET) < 0)
726 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
727 format_vlib_pci_addr, &di->addr);
731 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
733 u8 buf[sizeof (struct vfio_irq_set) + sizeof (int)] = { 0 };
734 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
735 clib_file_t t = { 0 };
736 int efd = eventfd (0, EFD_NONBLOCK);
738 irq_set->argsz = sizeof (buf);
740 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
742 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
743 clib_memcpy (&irq_set->data, &efd, sizeof (int));
745 if (ioctl (dfd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
747 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
748 format_vlib_pci_addr, &di->addr);
752 t.read_function = linux_pci_vfio_read_ready;
753 t.file_descriptor = efd;
754 t.error_function = linux_pci_vfio_error_ready;
755 t.private_data = p->handle;
756 p->clib_file_index = clib_file_add (&file_main, &t);
758 /* unmask the interrupt */
759 linux_pci_vfio_unmask_intx (p);
762 p->interrupt_handler = r->interrupt_handler;
764 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
765 format_vlib_pci_addr, &di->addr, 0);
767 p->config_fd = open ((char *) s, O_RDWR);
768 vec_reset_length (s);
770 if (p->config_fd == -1)
772 err = clib_error_return_unix (0, "open '%s'", s);
776 err = r->init_function (vm, p->handle);
783 close (p->config_fd);
788 /* Configuration space read/write. */
790 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
791 vlib_read_or_write_t read_or_write,
792 uword address, void *data, u32 n_bytes)
794 linux_pci_device_t *p = linux_pci_get_device (h);
797 if (read_or_write == VLIB_READ)
798 n = pread (p->config_fd, data, n_bytes, address);
800 n = pwrite (p->config_fd, data, n_bytes, address);
803 return clib_error_return_unix (0, "%s",
804 read_or_write == VLIB_READ
810 static clib_error_t *
811 vlib_pci_map_resource_int (vlib_pci_dev_handle_t h,
812 u32 resource, u8 * addr, void **result)
814 linux_pci_device_t *p = linux_pci_get_device (h);
815 struct stat stat_buf;
819 int flags = MAP_SHARED;
823 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
824 format_vlib_pci_addr, &p->addr, resource, 0);
826 fd = open ((char *) file_name, O_RDWR);
829 error = clib_error_return_unix (0, "open `%s'", file_name);
833 if (fstat (fd, &stat_buf) < 0)
835 error = clib_error_return_unix (0, "fstat `%s'", file_name);
839 vec_validate (p->resource_fds, resource);
840 p->resource_fds[resource] = fd;
844 *result = mmap (addr,
845 /* size */ stat_buf.st_size,
846 PROT_READ | PROT_WRITE, flags,
849 if (*result == (void *) -1)
851 error = clib_error_return_unix (0, "mmap `%s'", file_name);
861 vec_free (file_name);
866 vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource, void **result)
868 return (vlib_pci_map_resource_int (h, resource, 0 /* addr */ , result));
872 vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
873 u32 resource, u8 * addr, void **result)
875 return (vlib_pci_map_resource_int (h, resource, addr, result));
879 init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
881 vlib_pci_main_t *pm = &pci_main;
882 linux_pci_main_t *lpm = &linux_pci_main;
883 pci_device_registration_t *r;
885 clib_error_t *err = 0;
886 linux_pci_device_t *p;
888 pool_get (lpm->linux_pci_devices, p);
889 p->handle = p - lpm->linux_pci_devices;
891 r = pm->pci_device_registrations;
895 for (i = r->supported_devices; i->vendor_id != 0; i++)
896 if (i->vendor_id == di->config0.header.vendor_id &&
897 i->device_id == di->config0.header.device_id)
899 if (di->iommu_group != -1)
900 err = add_device_vfio (vm, p, di, r);
902 err = add_device_uio (vm, p, di, r);
905 clib_error_report (err);
909 r = r->next_registration;
912 /* No driver, close the PCI config-space FD */
913 memset (p, 0, sizeof (linux_pci_device_t));
914 pool_put (lpm->linux_pci_devices, p);
917 static clib_error_t *
918 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
920 vlib_pci_addr_t addr, **addrv = arg;
921 unformat_input_t input;
922 clib_error_t *err = 0;
924 unformat_init_string (&input, (char *) dev_dir_name,
925 vec_len (dev_dir_name));
927 if (!unformat (&input, "/sys/bus/pci/devices/%U",
928 unformat_vlib_pci_addr, &addr))
929 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
931 unformat_free (&input);
936 vec_add1 (*addrv, addr);
941 pci_addr_cmp (void *v1, void *v2)
943 vlib_pci_addr_t *a1 = v1;
944 vlib_pci_addr_t *a2 = v2;
946 if (a1->domain > a2->domain)
948 if (a1->domain < a2->domain)
950 if (a1->bus > a2->bus)
952 if (a1->bus < a2->bus)
954 if (a1->slot > a2->slot)
956 if (a1->slot < a2->slot)
958 if (a1->function > a2->function)
960 if (a1->function < a2->function)
966 vlib_pci_get_all_dev_addrs ()
968 vlib_pci_addr_t *addrs = 0;
970 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
971 &addrs, /* scan_dirs */ 0);
978 vec_sort_with_function (addrs, pci_addr_cmp);
984 linux_pci_init (vlib_main_t * vm)
986 vlib_pci_main_t *pm = &pci_main;
987 linux_pci_main_t *lpm = &linux_pci_main;
988 vlib_pci_addr_t *addr = 0, *addrs;
994 if ((error = vlib_call_init_function (vm, unix_input_init)))
997 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
999 fd = open ("/dev/vfio/vfio", O_RDWR);
1001 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1007 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1013 lpm->vfio_container_fd = fd;
1014 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1016 addrs = vlib_pci_get_all_dev_addrs ();
1018 vec_foreach (addr, addrs)
1020 vlib_pci_device_info_t *d;
1021 if ((d = vlib_pci_get_device_info (addr, 0)))
1023 init_device_from_registered (vm, d);
1024 vlib_pci_free_device_info (d);
1032 VLIB_INIT_FUNCTION (linux_pci_init);
1035 * fd.io coding-style-patch-verification: ON
1038 * eval: (c-set-style "gnu")