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";
75 pci_intx_handler_function_t *intx_handler;
76 pci_msix_handler_function_t *msix_handler;
82 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
83 LINUX_PCI_DEVICE_TYPE_UIO,
84 LINUX_PCI_DEVICE_TYPE_VFIO,
85 } linux_pci_device_type_t;
89 linux_pci_device_type_t type;
90 vlib_pci_dev_handle_t handle;
93 /* Resource file descriptors. */
94 linux_pci_region_t *regions;
96 /* File descriptor for config space read/write. */
100 /* Device File descriptor */
103 /* Minor device for uio device. */
106 /* Index given by clib_file_add. */
109 /* Interrupt handlers */
110 linux_pci_irq_t intx_irq;
111 linux_pci_irq_t *msix_irqs;
116 } linux_pci_device_t;
123 } linux_pci_vfio_iommu_group_t;
125 /* Pool of PCI devices. */
128 vlib_main_t *vlib_main;
129 linux_pci_device_t *linux_pci_devices;
132 int vfio_container_fd;
135 /* pool of IOMMU groups */
136 linux_pci_vfio_iommu_group_t *iommu_groups;
138 /* iommu group pool index by group id hash */
139 uword *iommu_pool_index_by_group;
143 extern linux_pci_main_t linux_pci_main;
145 static linux_pci_device_t *
146 linux_pci_get_device (vlib_pci_dev_handle_t h)
148 linux_pci_main_t *lpm = &linux_pci_main;
149 return pool_elt_at_index (lpm->linux_pci_devices, h);
153 vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
155 linux_pci_device_t *d = linux_pci_get_device (h);
156 return d->private_data;
160 vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
162 linux_pci_device_t *d = linux_pci_get_device (h);
163 d->private_data = private_data;
167 vlib_pci_get_addr (vlib_pci_dev_handle_t h)
169 linux_pci_device_t *d = linux_pci_get_device (h);
173 /* Call to allocate/initialize the pci subsystem.
174 This is not an init function so that users can explicitly enable
175 pci only when it's needed. */
176 clib_error_t *pci_bus_init (vlib_main_t * vm);
178 linux_pci_main_t linux_pci_main;
180 vlib_pci_device_info_t *
181 vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
183 linux_pci_main_t *lpm = &linux_pci_main;
185 vlib_pci_device_info_t *di;
190 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
191 memset (di, 0, sizeof (vlib_pci_device_info_t));
192 di->addr.as_u32 = addr->as_u32;
194 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
195 format_vlib_pci_addr, addr);
197 f = format (0, "%v/config%c", dev_dir_name, 0);
198 fd = open ((char *) f, O_RDWR);
200 /* Try read-only access if write fails. */
202 fd = open ((char *) f, O_RDONLY);
206 err = clib_error_return_unix (0, "open `%s'", f);
210 /* You can only read more that 64 bytes of config space as root; so we try to
211 read the full space but fall back to just the first 64 bytes. */
212 if (read (fd, &di->config_data, sizeof (di->config_data)) <
213 sizeof (di->config0))
215 err = clib_error_return_unix (0, "read `%s'", f);
221 static pci_config_header_t all_ones;
222 if (all_ones.vendor_id == 0)
223 memset (&all_ones, ~0, sizeof (all_ones));
225 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
227 err = clib_error_return (0, "invalid PCI config for `%s'", f);
233 if (di->config0.header.header_type == 0)
234 pci_config_type0_little_to_host (&di->config0);
236 pci_config_type1_little_to_host (&di->config1);
239 vec_reset_length (f);
240 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
241 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
245 clib_error_free (err);
248 vec_reset_length (f);
249 f = format (f, "%v/class%c", dev_dir_name, 0);
250 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
253 di->device_class = tmp >> 8;
255 vec_reset_length (f);
256 f = format (f, "%v/vendor%c", dev_dir_name, 0);
257 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
262 vec_reset_length (f);
263 f = format (f, "%v/device%c", dev_dir_name, 0);
264 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
269 vec_reset_length (f);
270 f = format (f, "%v/driver%c", dev_dir_name, 0);
271 di->driver_name = clib_sysfs_link_to_name ((char *) f);
273 di->iommu_group = -1;
274 if (lpm->vfio_container_fd != -1)
277 vec_reset_length (f);
278 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
279 tmpstr = clib_sysfs_link_to_name ((char *) f);
282 di->iommu_group = atoi ((char *) tmpstr);
289 vec_reset_length (f);
290 f = format (f, "%v/vpd%c", dev_dir_name, 0);
291 fd = open ((char *) f, O_RDONLY);
300 if (read (fd, &tag, 3) != 3)
303 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
306 len = (tag[2] << 8) | tag[1];
307 vec_validate (data, len);
309 if (read (fd, data, len) != len)
315 di->product_name = data;
316 else if (tag[0] == 0x90)
318 else if (tag[0] == 0x91)
329 vlib_pci_free_device_info (di);
334 vec_free (dev_dir_name);
338 clib_error_free (err);
343 directory_exists (char *path)
345 struct stat s = { 0 };
346 if (stat (path, &s) == -1)
349 return S_ISDIR (s.st_mode);
353 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
355 clib_error_t *error = 0;
356 u8 *s = 0, *driver_name = 0;
359 vlib_pci_device_info_t *di;
360 int fd, clear_driver_override = 0;
361 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
362 format_vlib_pci_addr, addr);
364 di = vlib_pci_get_device_info (addr, &error);
369 if (strncmp ("auto", uio_drv_name, 5) == 0)
371 int vfio_pci_loaded = 0;
373 if (directory_exists ("/sys/module/vfio_pci"))
376 if (di->iommu_group != -1)
378 /* device is bound to IOMMU group */
379 if (!vfio_pci_loaded)
381 error = clib_error_return (0, "Skipping PCI device %U: device "
382 "is bound to IOMMU group and "
383 "vfio-pci driver is not loaded",
384 format_vlib_pci_addr, addr);
388 uio_drv_name = "vfio-pci";
392 /* device is not bound to IOMMU group so we have multiple options */
393 if (vfio_pci_loaded &&
394 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
395 uio_drv_name = "vfio-pci";
396 else if (directory_exists ("/sys/module/uio_pci_generic"))
397 uio_drv_name = "uio_pci_generic";
398 else if (directory_exists ("/sys/module/igb_uio"))
399 uio_drv_name = "igb_uio";
402 clib_error_free (error);
403 error = clib_error_return (0, "Skipping PCI device %U: missing "
404 "kernel VFIO or UIO driver",
405 format_vlib_pci_addr, addr);
408 clib_error_free (error);
412 s = format (s, "%v/driver%c", dev_dir_name, 0);
413 driver_name = clib_sysfs_link_to_name ((char *) s);
414 vec_reset_length (s);
417 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
418 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
419 (strcmp ("igb_uio", (char *) driver_name) == 0)))
422 /* walk trough all linux interfaces and if interface belonging to
423 this device is founf check if interface is admin up */
424 dir = opendir ("/sys/class/net");
425 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
429 error = clib_error_return (0, "Skipping PCI device %U: failed to "
430 "read /sys/class/net",
431 format_vlib_pci_addr, addr);
435 fd = socket (PF_INET, SOCK_DGRAM, 0);
438 error = clib_error_return_unix (0, "socket");
442 while ((e = readdir (dir)))
445 struct ethtool_drvinfo drvinfo;
447 if (e->d_name[0] == '.') /* skip . and .. */
450 memset (&ifr, 0, sizeof ifr);
451 memset (&drvinfo, 0, sizeof drvinfo);
452 ifr.ifr_data = (char *) &drvinfo;
453 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
454 drvinfo.cmd = ETHTOOL_GDRVINFO;
455 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
457 /* Some interfaces (eg "lo") don't support this ioctl */
458 if ((errno != ENOTSUP) && (errno != ENODEV))
459 clib_unix_warning ("ioctl fetch intf %s bus info error",
464 if (strcmp ((char *) s, drvinfo.bus_info))
467 memset (&ifr, 0, sizeof (ifr));
468 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
469 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
471 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
477 if (ifr.ifr_flags & IFF_UP)
479 error = clib_error_return (0, "Skipping PCI device %U as host "
480 "interface %s is up",
481 format_vlib_pci_addr, addr, e->d_name);
488 vec_reset_length (s);
490 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
491 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
492 vec_reset_length (s);
494 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
495 if (access ((char *) s, F_OK) == 0)
497 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
498 clear_driver_override = 1;
502 vec_reset_length (s);
503 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
504 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
507 vec_reset_length (s);
509 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
510 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
511 vec_reset_length (s);
513 if (clear_driver_override)
515 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
516 clib_sysfs_write ((char *) s, "%c", 0);
517 vec_reset_length (s);
523 vec_free (dev_dir_name);
524 vec_free (driver_name);
529 static clib_error_t *
530 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
532 linux_pci_device_t *l = arg;
533 unformat_input_t input;
535 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
537 if (!unformat (&input, "uio%d", &l->uio_minor))
540 unformat_free (&input);
544 static clib_error_t *
545 vfio_set_irqs (linux_pci_device_t * p, u32 index, u32 start, u32 count,
546 u32 flags, int *efds)
548 int data_len = efds ? count * sizeof (int) : 0;
549 u8 buf[sizeof (struct vfio_irq_set) + data_len];
550 struct vfio_irq_info irq_info = { 0 };
551 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
554 irq_info.argsz = sizeof (struct vfio_irq_info);
555 irq_info.index = index;
557 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
558 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
559 "'%U'", format_vlib_pci_addr, &p->addr);
561 if (irq_info.count < start + count)
562 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
563 "'%U'", format_vlib_pci_addr, &p->addr);
568 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
569 clib_memcpy (&irq_set->data, efds, data_len);
572 flags |= VFIO_IRQ_SET_DATA_NONE;
574 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
575 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
577 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
578 irq_set->index = index;
579 irq_set->start = start;
580 irq_set->count = count;
581 irq_set->flags = flags;
583 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
584 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
585 "[index = %u, start = %u, count = %u, "
587 format_vlib_pci_addr, &p->addr,
588 index, start, count, flags);
592 static clib_error_t *
593 linux_pci_uio_read_ready (clib_file_t * uf)
595 int __attribute__ ((unused)) rv;
596 vlib_pci_dev_handle_t h = uf->private_data;
597 linux_pci_device_t *p = linux_pci_get_device (h);
598 linux_pci_irq_t *irq = &p->intx_irq;
601 rv = read (uf->file_descriptor, &icount, 4);
603 if (irq->intx_handler)
604 irq->intx_handler (h);
606 vlib_pci_intr_enable (h);
608 return /* no error */ 0;
611 static clib_error_t *
612 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
614 return vfio_set_irqs (d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
615 VFIO_IRQ_SET_ACTION_UNMASK, 0);
618 static clib_error_t *
619 linux_pci_uio_error_ready (clib_file_t * uf)
621 u32 error_index = (u32) uf->private_data;
623 return clib_error_return (0, "pci device %d: error", error_index);
626 static clib_error_t *
627 linux_pci_vfio_msix_read_ready (clib_file_t * uf)
629 int __attribute__ ((unused)) rv;
630 vlib_pci_dev_handle_t h = uf->private_data >> 16;
631 u16 line = uf->private_data & 0xffff;
632 linux_pci_device_t *p = linux_pci_get_device (h);
633 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
636 rv = read (uf->file_descriptor, &icount, sizeof (icount));
638 if (irq->msix_handler)
639 irq->msix_handler (h, line);
641 return /* no error */ 0;
644 static clib_error_t *
645 linux_pci_vfio_intx_read_ready (clib_file_t * uf)
647 int __attribute__ ((unused)) rv;
648 vlib_pci_dev_handle_t h = uf->private_data;
649 linux_pci_device_t *p = linux_pci_get_device (h);
650 linux_pci_irq_t *irq = &p->intx_irq;
653 rv = read (uf->file_descriptor, &icount, sizeof (icount));
655 if (irq->intx_handler)
656 irq->intx_handler (h);
658 linux_pci_vfio_unmask_intx (p);
660 return /* no error */ 0;
663 static clib_error_t *
664 linux_pci_vfio_error_ready (clib_file_t * uf)
666 u32 error_index = (u32) uf->private_data;
668 return clib_error_return (0, "pci device %d: error", error_index);
671 static clib_error_t *
672 add_device_uio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
673 pci_device_registration_t * r)
675 linux_pci_main_t *lpm = &linux_pci_main;
676 clib_error_t *err = 0;
679 p->addr.as_u32 = di->addr.as_u32;
681 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
683 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
684 format_vlib_pci_addr, &di->addr, 0);
686 p->config_fd = open ((char *) s, O_RDWR);
687 p->config_offset = 0;
688 vec_reset_length (s);
690 if (p->config_fd == -1)
692 err = clib_error_return_unix (0, "open '%s'", s);
696 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
697 format_vlib_pci_addr, &di->addr);
698 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
700 vec_reset_length (s);
702 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
703 p->fd = open ((char *) s, O_RDWR);
706 err = clib_error_return_unix (0, "open '%s'", s);
710 if (r && r->interrupt_handler)
711 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
713 if (r && r->init_function)
714 err = r->init_function (lpm->vlib_main, p->handle);
720 if (p->config_fd != -1)
721 close (p->config_fd);
729 vlib_pci_register_intx_handler (vlib_pci_dev_handle_t h,
730 pci_intx_handler_function_t * intx_handler)
732 linux_pci_device_t *p = linux_pci_get_device (h);
733 clib_file_t t = { 0 };
734 linux_pci_irq_t *irq = &p->intx_irq;
735 ASSERT (irq->fd == -1);
737 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
739 struct vfio_irq_info irq_info = { 0 };
740 irq_info.argsz = sizeof (struct vfio_irq_info);
741 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
742 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
743 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
744 "%U'", format_vlib_pci_addr, &p->addr);
745 if (irq_info.count != 1)
746 return clib_error_return (0, "INTx interrupt does not exist on device"
747 "'%U'", format_vlib_pci_addr, &p->addr);
749 irq->fd = eventfd (0, EFD_NONBLOCK);
751 return clib_error_return_unix (0, "eventfd");
752 t.file_descriptor = irq->fd;
753 t.read_function = linux_pci_vfio_intx_read_ready;
755 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
757 t.file_descriptor = p->fd;
758 t.read_function = linux_pci_uio_read_ready;
763 t.error_function = linux_pci_uio_error_ready;
764 t.private_data = p->handle;
765 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
766 irq->clib_file_index = clib_file_add (&file_main, &t);
767 irq->intx_handler = intx_handler;
772 vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h, u32 start, u32 count,
773 pci_msix_handler_function_t * msix_handler)
775 clib_error_t *err = 0;
776 linux_pci_device_t *p = linux_pci_get_device (h);
779 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
780 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
784 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
788 for (i = start; i < start + count; i++)
790 clib_file_t t = { 0 };
791 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
792 ASSERT (irq->fd == -1);
794 irq->fd = eventfd (0, EFD_NONBLOCK);
797 err = clib_error_return_unix (0, "eventfd");
801 t.read_function = linux_pci_vfio_msix_read_ready;
802 t.file_descriptor = irq->fd;
803 t.error_function = linux_pci_vfio_error_ready;
804 t.private_data = p->handle << 16 | i;
805 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
807 irq->clib_file_index = clib_file_add (&file_main, &t);
808 irq->msix_handler = msix_handler;
816 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
819 clib_file_del_by_index (&file_main, irq->clib_file_index);
828 vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
830 linux_pci_device_t *p = linux_pci_get_device (h);
834 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
835 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
838 for (i = start; i < start + count; i++)
840 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
844 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
845 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
849 vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
851 linux_pci_device_t *p = linux_pci_get_device (h);
854 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
855 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
858 for (i = start; i < start + count; i++)
861 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
862 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
865 static linux_pci_vfio_iommu_group_t *
866 get_vfio_iommu_group (int group)
868 linux_pci_main_t *lpm = &linux_pci_main;
871 p = hash_get (lpm->iommu_pool_index_by_group, group);
873 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
876 static clib_error_t *
877 open_vfio_iommu_group (int group)
879 linux_pci_main_t *lpm = &linux_pci_main;
880 linux_pci_vfio_iommu_group_t *g;
881 clib_error_t *err = 0;
882 struct vfio_group_status group_status;
886 g = get_vfio_iommu_group (group);
892 s = format (s, "/dev/vfio/%u%c", group, 0);
893 fd = open ((char *) s, O_RDWR);
895 return clib_error_return_unix (0, "open '%s'", s);
897 group_status.argsz = sizeof (group_status);
898 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
900 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
905 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
907 err = clib_error_return (0, "iommu group %d is not viable (not all "
908 "devices in this group bound to vfio-pci)",
913 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
915 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
920 if (lpm->vfio_iommu_mode == 0)
922 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
925 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
929 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
933 pool_get (lpm->iommu_groups, g);
936 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
944 static clib_error_t *
945 add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
946 pci_device_registration_t * r)
948 linux_pci_main_t *lpm = &linux_pci_main;
949 linux_pci_vfio_iommu_group_t *g;
950 struct vfio_device_info device_info = { 0 };
951 clib_error_t *err = 0;
954 p->addr.as_u32 = di->addr.as_u32;
955 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
957 if (di->driver_name == 0 ||
958 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
959 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
960 "vfio-pci", format_vlib_pci_addr, &di->addr,
963 if ((err = open_vfio_iommu_group (di->iommu_group)))
966 g = get_vfio_iommu_group (di->iommu_group);
968 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
969 if ((p->fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
971 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
972 format_vlib_pci_addr, &di->addr);
975 vec_reset_length (s);
977 device_info.argsz = sizeof (device_info);
978 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
980 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
981 format_vlib_pci_addr, &di->addr);
985 /* reset if device supports it */
986 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
987 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
989 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
990 format_vlib_pci_addr, &di->addr);
994 if (r && r->interrupt_handler)
996 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
997 linux_pci_vfio_unmask_intx (p);
1000 if (r && r->init_function)
1001 err = r->init_function (lpm->vlib_main, p->handle);
1009 if (p->config_fd != -1)
1010 close (p->config_fd);
1015 /* Configuration space read/write. */
1017 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
1018 vlib_read_or_write_t read_or_write,
1019 uword address, void *data, u32 n_bytes)
1021 linux_pci_device_t *p = linux_pci_get_device (h);
1024 if (read_or_write == VLIB_READ)
1025 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
1027 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
1030 return clib_error_return_unix (0, "%s",
1031 read_or_write == VLIB_READ
1032 ? "read" : "write");
1037 static clib_error_t *
1038 vlib_pci_map_region_int (vlib_pci_dev_handle_t h,
1039 u32 bar, u8 * addr, void **result)
1041 linux_pci_device_t *p = linux_pci_get_device (h);
1043 clib_error_t *error;
1044 int flags = MAP_SHARED;
1045 u64 size = 0, offset = 0;
1051 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1054 struct stat stat_buf;
1055 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1056 format_vlib_pci_addr, &p->addr, bar, 0);
1058 fd = open ((char *) file_name, O_RDWR);
1061 error = clib_error_return_unix (0, "open `%s'", file_name);
1062 vec_free (file_name);
1066 if (fstat (fd, &stat_buf) < 0)
1068 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1069 vec_free (file_name);
1074 vec_free (file_name);
1077 size = stat_buf.st_size;
1080 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1082 struct vfio_region_info reg = { 0 };
1083 reg.argsz = sizeof (struct vfio_region_info);
1085 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
1086 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1087 "'%U'", format_vlib_pci_addr,
1091 offset = reg.offset;
1096 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
1097 if (*result == (void *) -1)
1099 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1100 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1106 vec_validate_init_empty (p->regions, bar,
1107 (linux_pci_region_t) { .fd = -1});
1109 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1110 p->regions[bar].fd = fd;
1111 p->regions[bar].addr = *result;
1112 p->regions[bar].size = size;
1117 vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource, void **result)
1119 return (vlib_pci_map_region_int (h, resource, 0 /* addr */ , result));
1123 vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, u32 resource, u8 * addr,
1126 return (vlib_pci_map_region_int (h, resource, addr, result));
1130 init_device_from_registered (vlib_pci_device_info_t * di)
1132 vlib_pci_main_t *pm = &pci_main;
1133 linux_pci_main_t *lpm = &linux_pci_main;
1134 pci_device_registration_t *r;
1136 clib_error_t *err = 0;
1137 linux_pci_device_t *p;
1139 pool_get (lpm->linux_pci_devices, p);
1140 p->handle = p - lpm->linux_pci_devices;
1141 p->intx_irq.fd = -1;
1143 r = pm->pci_device_registrations;
1147 for (i = r->supported_devices; i->vendor_id != 0; i++)
1148 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1150 if (di->iommu_group != -1)
1151 err = add_device_vfio (p, di, r);
1153 err = add_device_uio (p, di, r);
1156 clib_error_report (err);
1160 r = r->next_registration;
1163 /* No driver, close the PCI config-space FD */
1164 memset (p, 0, sizeof (linux_pci_device_t));
1165 pool_put (lpm->linux_pci_devices, p);
1168 static clib_error_t *
1169 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1171 vlib_pci_addr_t addr, **addrv = arg;
1172 unformat_input_t input;
1173 clib_error_t *err = 0;
1175 unformat_init_string (&input, (char *) dev_dir_name,
1176 vec_len (dev_dir_name));
1178 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1179 unformat_vlib_pci_addr, &addr))
1180 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1182 unformat_free (&input);
1187 vec_add1 (*addrv, addr);
1192 pci_addr_cmp (void *v1, void *v2)
1194 vlib_pci_addr_t *a1 = v1;
1195 vlib_pci_addr_t *a2 = v2;
1197 if (a1->domain > a2->domain)
1199 if (a1->domain < a2->domain)
1201 if (a1->bus > a2->bus)
1203 if (a1->bus < a2->bus)
1205 if (a1->slot > a2->slot)
1207 if (a1->slot < a2->slot)
1209 if (a1->function > a2->function)
1211 if (a1->function < a2->function)
1217 vlib_pci_get_all_dev_addrs ()
1219 vlib_pci_addr_t *addrs = 0;
1221 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1222 &addrs, /* scan_dirs */ 0);
1229 vec_sort_with_function (addrs, pci_addr_cmp);
1235 linux_pci_init (vlib_main_t * vm)
1237 vlib_pci_main_t *pm = &pci_main;
1238 linux_pci_main_t *lpm = &linux_pci_main;
1239 vlib_pci_addr_t *addr = 0, *addrs;
1240 clib_error_t *error;
1245 if ((error = vlib_call_init_function (vm, unix_input_init)))
1248 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1250 fd = open ("/dev/vfio/vfio", O_RDWR);
1252 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1258 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1264 lpm->vfio_container_fd = fd;
1265 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1267 addrs = vlib_pci_get_all_dev_addrs ();
1269 vec_foreach (addr, addrs)
1271 vlib_pci_device_info_t *d;
1272 if ((d = vlib_pci_get_device_info (addr, 0)))
1274 init_device_from_registered (d);
1275 vlib_pci_free_device_info (d);
1283 VLIB_INIT_FUNCTION (linux_pci_init);
1286 * fd.io coding-style-patch-verification: ON
1289 * eval: (c-set-style "gnu")