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";
59 static char *sysfs_mod_vfio_noiommu =
60 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
64 vlib_pci_dev_handle_t handle;
67 /* Resource file descriptors. */
70 /* File descriptor for config space read/write. */
74 /* Device File descriptor */
77 /* 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->config0))
188 err = clib_error_return_unix (0, "read `%s'", f);
194 static pci_config_header_t all_ones;
195 if (all_ones.vendor_id == 0)
196 memset (&all_ones, ~0, sizeof (all_ones));
198 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
200 err = clib_error_return (0, "invalid PCI config for `%s'", f);
206 if (di->config0.header.header_type == 0)
207 pci_config_type0_little_to_host (&di->config0);
209 pci_config_type1_little_to_host (&di->config1);
212 vec_reset_length (f);
213 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
214 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
218 clib_error_free (err);
221 vec_reset_length (f);
222 f = format (f, "%v/class%c", dev_dir_name, 0);
223 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
226 di->device_class = tmp >> 8;
228 vec_reset_length (f);
229 f = format (f, "%v/vendor%c", dev_dir_name, 0);
230 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
235 vec_reset_length (f);
236 f = format (f, "%v/device%c", dev_dir_name, 0);
237 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
242 vec_reset_length (f);
243 f = format (f, "%v/driver%c", dev_dir_name, 0);
244 di->driver_name = clib_sysfs_link_to_name ((char *) f);
246 di->iommu_group = -1;
247 if (lpm->vfio_container_fd != -1)
250 vec_reset_length (f);
251 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
252 tmpstr = clib_sysfs_link_to_name ((char *) f);
255 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 directory_exists (char *path)
318 struct stat s = { 0 };
319 if (stat (path, &s) == -1)
322 return S_ISDIR (s.st_mode);
326 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
328 clib_error_t *error = 0;
329 u8 *s = 0, *driver_name = 0;
332 vlib_pci_device_info_t *di;
333 int fd, clear_driver_override = 0;
334 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
335 format_vlib_pci_addr, addr);
337 di = vlib_pci_get_device_info (addr, &error);
342 if (strncmp ("auto", uio_drv_name, 5) == 0)
344 int vfio_pci_loaded = 0;
346 if (directory_exists ("/sys/module/vfio_pci"))
349 if (di->iommu_group != -1)
351 /* device is bound to IOMMU group */
352 if (!vfio_pci_loaded)
354 error = clib_error_return (0, "Skipping PCI device %U: device "
355 "is bound to IOMMU group and "
356 "vfio-pci driver is not loaded",
357 format_vlib_pci_addr, addr);
361 uio_drv_name = "vfio-pci";
365 /* device is not bound to IOMMU group so we have multiple options */
366 if (vfio_pci_loaded &&
367 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
368 uio_drv_name = "vfio-pci";
369 else if (directory_exists ("/sys/module/uio_pci_generic"))
370 uio_drv_name = "uio_pci_generic";
371 else if (directory_exists ("/sys/module/igb_uio"))
372 uio_drv_name = "igb_uio";
375 clib_error_free (error);
376 error = clib_error_return (0, "Skipping PCI device %U: missing "
377 "kernel VFIO or UIO driver",
378 format_vlib_pci_addr, addr);
381 clib_error_free (error);
385 s = format (s, "%v/driver%c", dev_dir_name, 0);
386 driver_name = clib_sysfs_link_to_name ((char *) s);
387 vec_reset_length (s);
390 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
391 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
392 (strcmp ("igb_uio", (char *) driver_name) == 0)))
395 /* walk trough all linux interfaces and if interface belonging to
396 this device is founf check if interface is admin up */
397 dir = opendir ("/sys/class/net");
398 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
402 error = clib_error_return (0, "Skipping PCI device %U: failed to "
403 "read /sys/class/net",
404 format_vlib_pci_addr, addr);
408 fd = socket (PF_INET, SOCK_DGRAM, 0);
411 error = clib_error_return_unix (0, "socket");
415 while ((e = readdir (dir)))
418 struct ethtool_drvinfo drvinfo;
420 if (e->d_name[0] == '.') /* skip . and .. */
423 memset (&ifr, 0, sizeof ifr);
424 memset (&drvinfo, 0, sizeof drvinfo);
425 ifr.ifr_data = (char *) &drvinfo;
426 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
427 drvinfo.cmd = ETHTOOL_GDRVINFO;
428 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
430 /* Some interfaces (eg "lo") don't support this ioctl */
431 if ((errno != ENOTSUP) && (errno != ENODEV))
432 clib_unix_warning ("ioctl fetch intf %s bus info error",
437 if (strcmp ((char *) s, drvinfo.bus_info))
440 memset (&ifr, 0, sizeof (ifr));
441 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
442 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
444 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
450 if (ifr.ifr_flags & IFF_UP)
452 error = clib_error_return (0, "Skipping PCI device %U as host "
453 "interface %s is up",
454 format_vlib_pci_addr, addr, e->d_name);
461 vec_reset_length (s);
463 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
464 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
465 vec_reset_length (s);
467 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
468 if (access ((char *) s, F_OK) == 0)
470 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
471 clear_driver_override = 1;
475 vec_reset_length (s);
476 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
477 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
480 vec_reset_length (s);
482 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
483 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
484 vec_reset_length (s);
486 if (clear_driver_override)
488 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
489 clib_sysfs_write ((char *) s, "%c", 0);
490 vec_reset_length (s);
496 vec_free (dev_dir_name);
497 vec_free (driver_name);
502 static clib_error_t *
503 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
505 linux_pci_device_t *l = arg;
506 unformat_input_t input;
508 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
510 if (!unformat (&input, "uio%d", &l->uio_minor))
513 unformat_free (&input);
517 static clib_error_t *
518 linux_pci_uio_read_ready (clib_file_t * uf)
520 int __attribute__ ((unused)) rv;
521 vlib_pci_dev_handle_t h = uf->private_data;
522 linux_pci_device_t *p = linux_pci_get_device (h);
525 rv = read (uf->file_descriptor, &icount, 4);
527 if (p->interrupt_handler)
528 p->interrupt_handler (h);
530 vlib_pci_intr_enable (h);
532 return /* no error */ 0;
535 static clib_error_t *
536 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
538 clib_error_t *err = 0;
539 struct vfio_irq_set i = {
540 .argsz = sizeof (struct vfio_irq_set),
543 .index = VFIO_PCI_INTX_IRQ_INDEX,
544 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
547 if (ioctl (d->fd, VFIO_DEVICE_SET_IRQS, &i) < 0)
549 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
550 format_vlib_pci_addr, &d->addr);
555 static clib_error_t *
556 linux_pci_uio_error_ready (clib_file_t * uf)
558 u32 error_index = (u32) uf->private_data;
560 return clib_error_return (0, "pci device %d: error", error_index);
563 static clib_error_t *
564 linux_pci_vfio_read_ready (clib_file_t * uf)
566 int __attribute__ ((unused)) rv;
567 vlib_pci_dev_handle_t h = uf->private_data;
568 linux_pci_device_t *p = linux_pci_get_device (h);
571 rv = read (uf->file_descriptor, &icount, sizeof (icount));
573 if (p->interrupt_handler)
574 p->interrupt_handler (h);
576 linux_pci_vfio_unmask_intx (p);
578 return /* no error */ 0;
581 static clib_error_t *
582 linux_pci_vfio_error_ready (clib_file_t * uf)
584 u32 error_index = (u32) uf->private_data;
586 return clib_error_return (0, "pci device %d: error", error_index);
589 static clib_error_t *
590 add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
591 vlib_pci_device_info_t * di, pci_device_registration_t * r)
593 clib_error_t *err = 0;
594 clib_file_t t = { 0 };
597 p->addr.as_u32 = di->addr.as_u32;
600 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
601 format_vlib_pci_addr, &di->addr, 0);
603 p->config_fd = open ((char *) s, O_RDWR);
604 p->config_offset = 0;
605 vec_reset_length (s);
607 if (p->config_fd == -1)
609 err = clib_error_return_unix (0, "open '%s'", s);
613 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
614 format_vlib_pci_addr, &di->addr);
615 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
617 vec_reset_length (s);
619 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
620 p->fd = open ((char *) s, O_RDWR);
623 err = clib_error_return_unix (0, "open '%s'", s);
627 t.read_function = linux_pci_uio_read_ready;
628 t.file_descriptor = p->fd;
629 t.error_function = linux_pci_uio_error_ready;
630 t.private_data = p->handle;
632 p->clib_file_index = clib_file_add (&file_main, &t);
633 p->interrupt_handler = r->interrupt_handler;
634 err = r->init_function (vm, p->handle);
640 if (p->config_fd != -1)
641 close (p->config_fd);
648 static linux_pci_vfio_iommu_group_t *
649 get_vfio_iommu_group (int group)
651 linux_pci_main_t *lpm = &linux_pci_main;
654 p = hash_get (lpm->iommu_pool_index_by_group, group);
656 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
659 static clib_error_t *
660 open_vfio_iommu_group (int group)
662 linux_pci_main_t *lpm = &linux_pci_main;
663 linux_pci_vfio_iommu_group_t *g;
664 clib_error_t *err = 0;
665 struct vfio_group_status group_status;
669 g = get_vfio_iommu_group (group);
675 s = format (s, "/dev/vfio/%u%c", group, 0);
676 fd = open ((char *) s, O_RDWR);
678 return clib_error_return_unix (0, "open '%s'", s);
680 group_status.argsz = sizeof (group_status);
681 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
683 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
688 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
690 err = clib_error_return (0, "iommu group %d is not viable (not all "
691 "devices in this group bound to vfio-pci)",
696 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
698 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
703 if (lpm->vfio_iommu_mode == 0)
705 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
708 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
712 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
716 pool_get (lpm->iommu_groups, g);
719 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
727 static clib_error_t *
728 add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
729 vlib_pci_device_info_t * di, pci_device_registration_t * r)
731 linux_pci_vfio_iommu_group_t *g;
732 struct vfio_device_info device_info = { 0 };
733 struct vfio_region_info reg = { 0 };
734 struct vfio_irq_info irq_info = { 0 };
735 clib_error_t *err = 0;
738 p->addr.as_u32 = di->addr.as_u32;
740 if (di->driver_name == 0 ||
741 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
742 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
743 "vfio-pci", format_vlib_pci_addr, &di->addr,
746 if ((err = open_vfio_iommu_group (di->iommu_group)))
749 g = get_vfio_iommu_group (di->iommu_group);
751 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
752 if ((p->fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
754 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
755 format_vlib_pci_addr, &di->addr);
758 vec_reset_length (s);
760 device_info.argsz = sizeof (device_info);
761 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
763 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
764 format_vlib_pci_addr, &di->addr);
768 irq_info.argsz = sizeof (struct vfio_irq_info);
769 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
770 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
772 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
773 "'%U'", format_vlib_pci_addr, &di->addr);
777 /* reset if device supports it */
778 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
779 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
781 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
782 format_vlib_pci_addr, &di->addr);
786 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
788 u8 buf[sizeof (struct vfio_irq_set) + sizeof (int)] = { 0 };
789 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
790 clib_file_t t = { 0 };
791 int efd = eventfd (0, EFD_NONBLOCK);
793 irq_set->argsz = sizeof (buf);
795 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
797 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
798 clib_memcpy (&irq_set->data, &efd, sizeof (int));
800 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
802 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
803 format_vlib_pci_addr, &di->addr);
807 t.read_function = linux_pci_vfio_read_ready;
808 t.file_descriptor = efd;
809 t.error_function = linux_pci_vfio_error_ready;
810 t.private_data = p->handle;
811 p->clib_file_index = clib_file_add (&file_main, &t);
813 /* unmask the interrupt */
814 linux_pci_vfio_unmask_intx (p);
817 p->interrupt_handler = r->interrupt_handler;
819 reg.argsz = sizeof (struct vfio_region_info);
820 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
821 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
823 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_REGION_INFO) "
824 "'%U'", format_vlib_pci_addr, &di->addr);
827 p->config_offset = reg.offset;
828 p->config_fd = p->fd;
830 err = r->init_function (vm, p->handle);
838 if (p->config_fd != -1)
839 close (p->config_fd);
844 /* Configuration space read/write. */
846 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
847 vlib_read_or_write_t read_or_write,
848 uword address, void *data, u32 n_bytes)
850 linux_pci_device_t *p = linux_pci_get_device (h);
853 if (read_or_write == VLIB_READ)
854 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
856 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
859 return clib_error_return_unix (0, "%s",
860 read_or_write == VLIB_READ
866 static clib_error_t *
867 vlib_pci_map_resource_int (vlib_pci_dev_handle_t h,
868 u32 resource, u8 * addr, void **result)
870 linux_pci_device_t *p = linux_pci_get_device (h);
871 struct stat stat_buf;
875 int flags = MAP_SHARED;
879 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
880 format_vlib_pci_addr, &p->addr, resource, 0);
882 fd = open ((char *) file_name, O_RDWR);
885 error = clib_error_return_unix (0, "open `%s'", file_name);
889 if (fstat (fd, &stat_buf) < 0)
891 error = clib_error_return_unix (0, "fstat `%s'", file_name);
895 vec_validate (p->resource_fds, resource);
896 p->resource_fds[resource] = fd;
900 *result = mmap (addr,
901 /* size */ stat_buf.st_size,
902 PROT_READ | PROT_WRITE, flags,
905 if (*result == (void *) -1)
907 error = clib_error_return_unix (0, "mmap `%s'", file_name);
917 vec_free (file_name);
922 vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource, void **result)
924 return (vlib_pci_map_resource_int (h, resource, 0 /* addr */ , result));
928 vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
929 u32 resource, u8 * addr, void **result)
931 return (vlib_pci_map_resource_int (h, resource, addr, result));
935 init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
937 vlib_pci_main_t *pm = &pci_main;
938 linux_pci_main_t *lpm = &linux_pci_main;
939 pci_device_registration_t *r;
941 clib_error_t *err = 0;
942 linux_pci_device_t *p;
944 pool_get (lpm->linux_pci_devices, p);
945 p->handle = p - lpm->linux_pci_devices;
947 r = pm->pci_device_registrations;
951 for (i = r->supported_devices; i->vendor_id != 0; i++)
952 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
954 if (di->iommu_group != -1)
955 err = add_device_vfio (vm, p, di, r);
957 err = add_device_uio (vm, p, di, r);
960 clib_error_report (err);
964 r = r->next_registration;
967 /* No driver, close the PCI config-space FD */
968 memset (p, 0, sizeof (linux_pci_device_t));
969 pool_put (lpm->linux_pci_devices, p);
972 static clib_error_t *
973 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
975 vlib_pci_addr_t addr, **addrv = arg;
976 unformat_input_t input;
977 clib_error_t *err = 0;
979 unformat_init_string (&input, (char *) dev_dir_name,
980 vec_len (dev_dir_name));
982 if (!unformat (&input, "/sys/bus/pci/devices/%U",
983 unformat_vlib_pci_addr, &addr))
984 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
986 unformat_free (&input);
991 vec_add1 (*addrv, addr);
996 pci_addr_cmp (void *v1, void *v2)
998 vlib_pci_addr_t *a1 = v1;
999 vlib_pci_addr_t *a2 = v2;
1001 if (a1->domain > a2->domain)
1003 if (a1->domain < a2->domain)
1005 if (a1->bus > a2->bus)
1007 if (a1->bus < a2->bus)
1009 if (a1->slot > a2->slot)
1011 if (a1->slot < a2->slot)
1013 if (a1->function > a2->function)
1015 if (a1->function < a2->function)
1021 vlib_pci_get_all_dev_addrs ()
1023 vlib_pci_addr_t *addrs = 0;
1025 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1026 &addrs, /* scan_dirs */ 0);
1033 vec_sort_with_function (addrs, pci_addr_cmp);
1039 linux_pci_init (vlib_main_t * vm)
1041 vlib_pci_main_t *pm = &pci_main;
1042 linux_pci_main_t *lpm = &linux_pci_main;
1043 vlib_pci_addr_t *addr = 0, *addrs;
1044 clib_error_t *error;
1049 if ((error = vlib_call_init_function (vm, unix_input_init)))
1052 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1054 fd = open ("/dev/vfio/vfio", O_RDWR);
1056 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1062 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1068 lpm->vfio_container_fd = fd;
1069 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1071 addrs = vlib_pci_get_all_dev_addrs ();
1073 vec_foreach (addr, addrs)
1075 vlib_pci_device_info_t *d;
1076 if ((d = vlib_pci_get_device_info (addr, 0)))
1078 init_device_from_registered (vm, d);
1079 vlib_pci_free_device_info (d);
1087 VLIB_INIT_FUNCTION (linux_pci_init);
1090 * fd.io coding-style-patch-verification: ON
1093 * eval: (c-set-style "gnu")