FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
pci.c
Go to the documentation of this file.
1 /*
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:
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 
40 #include <vppinfra/linux/sysfs.h>
41 
42 #include <vlib/vlib.h>
43 #include <vlib/pci/pci.h>
44 #include <vlib/unix/unix.h>
45 #include <vlib/linux/vfio.h>
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <dirent.h>
51 #include <sys/ioctl.h>
52 #include <net/if.h>
53 #include <linux/ethtool.h>
54 #include <linux/sockios.h>
55 #include <linux/vfio.h>
56 #include <sys/eventfd.h>
57 
58 static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
59 static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
60 static char *sysfs_mod_vfio_noiommu =
61  "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
62 
63 #define pci_log_debug(vm, dev, f, ...) \
64  vlib_log(VLIB_LOG_LEVEL_DEBUG, pci_main.log_default, "%U: " f, \
65  format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
66 #define pci_log_err(vm, dev, f, ...) \
67  vlib_log(VLIB_LOG_LEVEL_ERR, pci_main.log_default, "%U: " f, \
68  format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
69 
70 typedef struct
71 {
72  int fd;
73  void *addr;
74  size_t size;
76 
77 typedef struct
78 {
79  int fd;
81  union
82  {
85  };
87 
88 typedef enum
89 {
94 
95 typedef struct
96 {
99  vlib_pci_addr_t addr;
101 
102  /* Resource file descriptors. */
104 
105  /* File descriptor for config space read/write. */
108 
109  /* Device File descriptor */
110  int fd;
111 
112  /* read/write file descriptor for io bar */
113  int io_fd;
115 
116  /* Minor device for uio device. */
118 
119  /* Interrupt handlers */
122 
123  /* private data */
125 
127 
129 
130 /* Pool of PCI devices. */
131 typedef struct
132 {
135 
137 
139 
140 static linux_pci_device_t *
142 {
144  return pool_elt_at_index (lpm->linux_pci_devices, h);
145 }
146 
147 uword
149 {
151  return d->private_data;
152 }
153 
154 void
156  uword private_data)
157 {
159  d->private_data = private_data;
160 }
161 
162 vlib_pci_addr_t *
164 {
166  return &d->addr;
167 }
168 
169 u32
171 {
173  return d->numa_node;
174 }
175 
176 u32
178 {
180 
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 
194 /* Call to allocate/initialize the pci subsystem.
195  This is not an init function so that users can explicitly enable
196  pci only when it's needed. */
198 
200 
202 vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
203  clib_error_t ** error)
204 {
205  clib_error_t *err;
207  u8 *f = 0;
208  u32 tmp;
209  int fd;
210  u8 *tmpstr;
211 
212  di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
213  clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
214  di->addr.as_u32 = addr->as_u32;
215 
216  u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
217  format_vlib_pci_addr, addr);
218 
219  f = format (0, "%v/config%c", dev_dir_name, 0);
220  fd = open ((char *) f, O_RDWR);
221 
222  /* Try read-only access if write fails. */
223  if (fd < 0)
224  fd = open ((char *) f, O_RDONLY);
225 
226  if (fd < 0)
227  {
228  err = clib_error_return_unix (0, "open `%s'", f);
229  goto error;
230  }
231 
232  /* You can only read more that 64 bytes of config space as root; so we try to
233  read the full space but fall back to just the first 64 bytes. */
234  if (read (fd, &di->config_data, sizeof (di->config_data)) <
235  sizeof (di->config0))
236  {
237  err = clib_error_return_unix (0, "read `%s'", f);
238  close (fd);
239  goto error;
240  }
241 
242  {
243  static pci_config_header_t all_ones;
244  if (all_ones.vendor_id == 0)
245  clib_memset (&all_ones, ~0, sizeof (all_ones));
246 
247  if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
248  {
249  err = clib_error_return (0, "invalid PCI config for `%s'", f);
250  close (fd);
251  goto error;
252  }
253  }
254 
255  if (di->config0.header.header_type == 0)
257  else
259 
260  di->numa_node = -1;
261  vec_reset_length (f);
262  f = format (f, "%v/numa_node%c", dev_dir_name, 0);
263  err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
264  if (err)
265  {
266  di->numa_node = -1;
267  clib_error_free (err);
268  }
269 
270  vec_reset_length (f);
271  f = format (f, "%v/class%c", dev_dir_name, 0);
272  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
273  if (err)
274  goto error;
275  di->device_class = tmp >> 8;
276 
277  vec_reset_length (f);
278  f = format (f, "%v/vendor%c", dev_dir_name, 0);
279  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
280  if (err)
281  goto error;
282  di->vendor_id = tmp;
283 
284  vec_reset_length (f);
285  f = format (f, "%v/device%c", dev_dir_name, 0);
286  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
287  if (err)
288  goto error;
289  di->device_id = tmp;
290 
291  vec_reset_length (f);
292  f = format (f, "%v/driver%c", dev_dir_name, 0);
293  di->driver_name = clib_sysfs_link_to_name ((char *) f);
294  if (!di->driver_name)
295  di->driver_name = format (0, "<NONE>%c", 0);
296 
297  di->iommu_group = -1;
298  vec_reset_length (f);
299  f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
300  tmpstr = clib_sysfs_link_to_name ((char *) f);
301  if (tmpstr)
302  {
303  di->iommu_group = atoi ((char *) tmpstr);
304  vec_free (tmpstr);
305  }
306 
307  vec_reset_length (f);
308  f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
309  err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
310  if (err == 0)
311  {
312  if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
314  vec_free (tmpstr);
315  }
316  else
317  clib_error_free (err);
318 
319  close (fd);
320 
321  vec_reset_length (f);
322  f = format (f, "%v/vpd%c", dev_dir_name, 0);
323  fd = open ((char *) f, O_RDONLY);
324  if (fd >= 0)
325  {
326  while (1)
327  {
328  u8 tag[3];
329  u8 *data = 0;
330  uword len;
331 
332  if (read (fd, &tag, 3) != 3)
333  break;
334 
335  if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
336  break;
337 
338  len = (tag[2] << 8) | tag[1];
339  vec_validate (data, len);
340 
341  if (read (fd, data, len) != len)
342  {
343  vec_free (data);
344  break;
345  }
346  if (tag[0] == 0x82)
347  di->product_name = data;
348  else if (tag[0] == 0x90)
349  di->vpd_r = data;
350  else if (tag[0] == 0x91)
351  di->vpd_w = data;
352 
353  data = 0;
354  }
355  close (fd);
356  }
357 
358  goto done;
359 
360 error:
362  di = 0;
363 
364 done:
365  vec_free (f);
366  vec_free (dev_dir_name);
367  if (error)
368  *error = err;
369  else
370  clib_error_free (err);
371  return di;
372 }
373 
374 static int
376 {
377  struct stat s = { 0 };
378  if (stat (path, &s) == -1)
379  return 0;
380 
381  return S_ISDIR (s.st_mode);
382 }
383 
384 clib_error_t *
385 vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
386  char *uio_drv_name)
387 {
388  clib_error_t *error = 0;
389  u8 *s = 0, *driver_name = 0;
390  DIR *dir = 0;
391  struct dirent *e;
393  int fd, clear_driver_override = 0;
394  u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
395  format_vlib_pci_addr, addr);
396 
397  di = vlib_pci_get_device_info (vm, addr, &error);
398 
399  if (error)
400  return error;
401 
402  if (strncmp ("auto", uio_drv_name, 5) == 0)
403  {
404  int vfio_pci_loaded = 0;
405 
406  if (directory_exists ("/sys/module/vfio_pci"))
407  vfio_pci_loaded = 1;
408 
409  if (di->iommu_group != -1)
410  {
411  /* device is bound to IOMMU group */
412  if (!vfio_pci_loaded)
413  {
414  error = clib_error_return (0, "Skipping PCI device %U: device "
415  "is bound to IOMMU group and "
416  "vfio-pci driver is not loaded",
417  format_vlib_pci_addr, addr);
418  goto done;
419  }
420  else
421  uio_drv_name = "vfio-pci";
422  }
423  else
424  {
425  /* device is not bound to IOMMU group so we have multiple options */
426  if (vfio_pci_loaded &&
427  (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
428  uio_drv_name = "vfio-pci";
429  else if (directory_exists ("/sys/module/uio_pci_generic"))
430  uio_drv_name = "uio_pci_generic";
431  else if (directory_exists ("/sys/module/igb_uio"))
432  uio_drv_name = "igb_uio";
433  else
434  {
435  clib_error_free (error);
436  error = clib_error_return (0, "Skipping PCI device %U: missing "
437  "kernel VFIO or UIO driver",
438  format_vlib_pci_addr, addr);
439  goto done;
440  }
441  clib_error_free (error);
442  }
443  }
444 
445  s = format (s, "%v/driver%c", dev_dir_name, 0);
446  driver_name = clib_sysfs_link_to_name ((char *) s);
447  vec_reset_length (s);
448 
449  if (driver_name &&
450  ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
451  (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
452  (strcmp ("igb_uio", (char *) driver_name) == 0)))
453  goto done;
454 
455  /* walk trough all linux interfaces and if interface belonging to
456  this device is founf check if interface is admin up */
457  dir = opendir ("/sys/class/net");
458  s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
459 
460  if (!dir)
461  {
462  error = clib_error_return (0, "Skipping PCI device %U: failed to "
463  "read /sys/class/net",
464  format_vlib_pci_addr, addr);
465  goto done;
466  }
467 
468  fd = socket (PF_INET, SOCK_DGRAM, 0);
469  if (fd < 0)
470  {
471  error = clib_error_return_unix (0, "socket");
472  goto done;
473  }
474 
475  while ((e = readdir (dir)))
476  {
477  struct ifreq ifr;
478  struct ethtool_drvinfo drvinfo;
479 
480  if (e->d_name[0] == '.') /* skip . and .. */
481  continue;
482 
483  clib_memset (&ifr, 0, sizeof ifr);
484  clib_memset (&drvinfo, 0, sizeof drvinfo);
485  ifr.ifr_data = (char *) &drvinfo;
486  clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
487 
488  drvinfo.cmd = ETHTOOL_GDRVINFO;
489  if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
490  {
491  /* Some interfaces (eg "lo") don't support this ioctl */
492  if ((errno != ENOTSUP) && (errno != ENODEV))
493  clib_unix_warning ("ioctl fetch intf %s bus info error",
494  e->d_name);
495  continue;
496  }
497 
498  if (strcmp ((char *) s, drvinfo.bus_info))
499  continue;
500 
501  clib_memset (&ifr, 0, sizeof (ifr));
502  clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
503 
504  if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
505  {
506  error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
507  e->d_name);
508  close (fd);
509  goto done;
510  }
511 
512  if (ifr.ifr_flags & IFF_UP)
513  {
514  error = clib_error_return (0, "Skipping PCI device %U as host "
515  "interface %s is up",
516  format_vlib_pci_addr, addr, e->d_name);
517  close (fd);
518  goto done;
519  }
520  }
521 
522  close (fd);
523  vec_reset_length (s);
524 
525  s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
526  clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
527  vec_reset_length (s);
528 
529  s = format (s, "%v/driver_override%c", dev_dir_name, 0);
530  if (access ((char *) s, F_OK) == 0)
531  {
532  clib_sysfs_write ((char *) s, "%s", uio_drv_name);
533  clear_driver_override = 1;
534  }
535  else
536  {
537  vec_reset_length (s);
538  s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
539  clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
540  di->device_id);
541  }
542  vec_reset_length (s);
543 
544  s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
545  clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
546  vec_reset_length (s);
547 
548  if (clear_driver_override)
549  {
550  s = format (s, "%v/driver_override%c", dev_dir_name, 0);
551  clib_sysfs_write ((char *) s, "%c", 0);
552  vec_reset_length (s);
553  }
554 
555 done:
556  closedir (dir);
557  vec_free (s);
558  vec_free (dev_dir_name);
559  vec_free (driver_name);
560  return error;
561 }
562 
563 
564 static clib_error_t *
565 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
566 {
567  linux_pci_device_t *l = arg;
568  unformat_input_t input;
569 
570  unformat_init_string (&input, (char *) file_name, vec_len (file_name));
571 
572  if (!unformat (&input, "uio%d", &l->uio_minor))
573  abort ();
574 
575  unformat_free (&input);
576  return 0;
577 }
578 
579 static clib_error_t *
581  u32 count, u32 flags, int *efds)
582 {
583  int data_len = efds ? count * sizeof (int) : 0;
584  u8 buf[sizeof (struct vfio_irq_set) + data_len];
585  struct vfio_irq_info ii = { 0 };
586  struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
587 
588 
589  ii.argsz = sizeof (struct vfio_irq_info);
590  ii.index = index;
591 
592  if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
593  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
594  "'%U'", format_vlib_pci_addr, &p->addr);
595 
596  pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
597  __func__, ii.index, ii.count,
598  ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
599  ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
600  ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
601  ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
602  ii.flags);
603 
604  if (ii.count < start + count)
605  return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
606  "'%U'", format_vlib_pci_addr, &p->addr);
607 
608 
609  if (efds)
610  {
611  flags |= VFIO_IRQ_SET_DATA_EVENTFD;
612  clib_memcpy_fast (&irq_set->data, efds, data_len);
613  }
614  else
615  flags |= VFIO_IRQ_SET_DATA_NONE;
616 
617  ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
618  (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
619 
620  irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
621  irq_set->index = index;
622  irq_set->start = start;
623  irq_set->count = count;
624  irq_set->flags = flags;
625 
626  if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
627  return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
628  "[index = %u, start = %u, count = %u, "
629  "flags = 0x%x]",
631  index, start, count, flags);
632  return 0;
633 }
634 
635 static clib_error_t *
637 {
638  vlib_main_t *vm = vlib_get_main ();
639  int __attribute__ ((unused)) rv;
642  linux_pci_irq_t *irq = &p->intx_irq;
643 
644  u32 icount;
645  rv = read (uf->file_descriptor, &icount, 4);
646 
647  if (irq->intx_handler)
648  irq->intx_handler (vm, h);
649 
650  vlib_pci_intr_enable (vm, h);
651 
652  return /* no error */ 0;
653 }
654 
655 static clib_error_t *
657 {
658  return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
659  VFIO_IRQ_SET_ACTION_UNMASK, 0);
660 }
661 
662 static clib_error_t *
664 {
665  u32 error_index = (u32) uf->private_data;
666 
667  return clib_error_return (0, "pci device %d: error", error_index);
668 }
669 
670 static clib_error_t *
672 {
673  vlib_main_t *vm = vlib_get_main ();
674  int __attribute__ ((unused)) rv;
676  u16 line = uf->private_data & 0xffff;
678  linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
679 
680  u64 icount;
681  rv = read (uf->file_descriptor, &icount, sizeof (icount));
682 
683  if (irq->msix_handler)
684  irq->msix_handler (vm, h, line);
685 
686  return /* no error */ 0;
687 }
688 
689 static clib_error_t *
691 {
692  vlib_main_t *vm = vlib_get_main ();
693  int __attribute__ ((unused)) rv;
696  linux_pci_irq_t *irq = &p->intx_irq;
697 
698  u64 icount;
699  rv = read (uf->file_descriptor, &icount, sizeof (icount));
700 
701  if (irq->intx_handler)
702  irq->intx_handler (vm, h);
703 
705 
706  return /* no error */ 0;
707 }
708 
709 static clib_error_t *
711 {
712  u32 error_index = (u32) uf->private_data;
713 
714  return clib_error_return (0, "pci device %d: error", error_index);
715 }
716 
717 static clib_error_t *
720 {
722  clib_error_t *err = 0;
723  u8 *s = 0;
724 
725  p->fd = -1;
727 
728  s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
729  format_vlib_pci_addr, &di->addr, 0);
730 
731  p->config_fd = open ((char *) s, O_RDWR);
732  p->config_offset = 0;
733  vec_reset_length (s);
734 
735  if (p->config_fd == -1)
736  {
737  err = clib_error_return_unix (0, "open '%s'", s);
738  goto error;
739  }
740 
741  s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
742  format_vlib_pci_addr, &di->addr, 0);
743  foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
744  1);
745  vec_reset_length (s);
746 
747  s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
748  p->fd = open ((char *) s, O_RDWR);
749  if (p->fd < 0)
750  {
751  err = clib_error_return_unix (0, "open '%s'", s);
752  goto error;
753  }
754 
755  if (r && r->interrupt_handler)
756  vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
757 
758  if (r && r->init_function)
759  err = r->init_function (lpm->vlib_main, p->handle);
760 
761 error:
762  vec_free (s);
763  if (err)
764  {
765  if (p->config_fd != -1)
766  close (p->config_fd);
767  if (p->fd != -1)
768  close (p->fd);
769  }
770  return err;
771 }
772 
773 clib_error_t *
775  pci_intx_handler_function_t * intx_handler)
776 {
778  clib_file_t t = { 0 };
779  linux_pci_irq_t *irq = &p->intx_irq;
780  ASSERT (irq->fd == -1);
781 
783  {
784  struct vfio_irq_info ii = { 0 };
785  ii.argsz = sizeof (struct vfio_irq_info);
786  ii.index = VFIO_PCI_INTX_IRQ_INDEX;
787  if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
788  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
789  "%U'", format_vlib_pci_addr, &p->addr);
790  pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
791  __func__, ii.index, ii.count,
792  ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
793  ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
794  ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
795  ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
796  ii.flags);
797  if (ii.count != 1)
798  return clib_error_return (0, "INTx interrupt does not exist on device"
799  "'%U'", format_vlib_pci_addr, &p->addr);
800 
801  irq->fd = eventfd (0, EFD_NONBLOCK);
802  if (irq->fd == -1)
803  return clib_error_return_unix (0, "eventfd");
804 
805  t.file_descriptor = irq->fd;
807  }
808  else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
809  {
810  t.file_descriptor = p->fd;
812  }
813  else
814  return 0;
815 
817  t.private_data = p->handle;
818  t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
820  irq->intx_handler = intx_handler;
821  return 0;
822 }
823 
824 clib_error_t *
826  u32 start, u32 count,
827  pci_msix_handler_function_t * msix_handler)
828 {
829  clib_error_t *err = 0;
831  u32 i;
832 
834  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
835  "support");
836 
837  /* *INDENT-OFF* */
838  vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
839  { .fd = -1});
840  /* *INDENT-ON* */
841 
842  for (i = start; i < start + count; i++)
843  {
844  clib_file_t t = { 0 };
846  ASSERT (irq->fd == -1);
847 
848  irq->fd = eventfd (0, EFD_NONBLOCK);
849  if (irq->fd == -1)
850  {
851  err = clib_error_return_unix (0, "eventfd");
852  goto error;
853  }
854 
856  t.file_descriptor = irq->fd;
858  t.private_data = p->handle << 16 | i;
859  t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
860  &p->addr, i);
862  irq->msix_handler = msix_handler;
863  }
864 
865  return 0;
866 
867 error:
868  while (i-- > start)
869  {
871  if (irq->fd != -1)
872  {
874  close (irq->fd);
875  irq->fd = -1;
876  }
877  }
878  return err;
879 }
880 
881 clib_error_t *
883  u16 start, u16 count)
884 {
886  int fds[count];
887  int i;
888 
890  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
891  "support");
892 
893  for (i = start; i < start + count; i++)
894  {
896  fds[i] = irq->fd;
897  }
898 
899  return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
900  VFIO_IRQ_SET_ACTION_TRIGGER, fds);
901 }
902 
903 clib_error_t *
905  u16 start, u16 count)
906 {
908  int i, fds[count];
909 
911  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
912  "support");
913 
914  for (i = start; i < start + count; i++)
915  fds[i] = -1;
916 
917  return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
918  VFIO_IRQ_SET_ACTION_TRIGGER, fds);
919 }
920 
921 static clib_error_t *
924 {
926  struct vfio_device_info device_info = { 0 };
927  struct vfio_region_info reg = { 0 };
928  clib_error_t *err = 0;
929  u8 *s = 0;
930  int is_noiommu;
931 
933 
934  if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
935  return err;
936 
937  if (is_noiommu == 0)
938  p->supports_va_dma = 1;
939 
940  device_info.argsz = sizeof (device_info);
941  if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
942  {
943  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
944  format_vlib_pci_addr, &di->addr);
945  goto error;
946  }
947 
948  reg.argsz = sizeof (struct vfio_region_info);
949  reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
950  if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
951  {
952  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
953  format_vlib_pci_addr, &di->addr);
954  goto error;
955  }
956 
957  pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
958  "flags: %s%s%s(0x%x)", __func__,
959  reg.index, reg.size, reg.offset,
960  reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
961  reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
962  reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
963  reg.flags);
964 
965  p->config_offset = reg.offset;
966  p->config_fd = p->fd;
967 
968  /* reset if device supports it */
969  if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
970  if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
971  {
972  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
973  format_vlib_pci_addr, &di->addr);
974  goto error;
975  }
976 
977  if (r && r->interrupt_handler)
978  {
979  vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
981  }
982 
983  if (p->supports_va_dma)
984  {
985  vlib_buffer_pool_t *bp;
986  /* *INDENT-OFF* */
988  {
989  u32 i;
990  vlib_physmem_map_t *pm;
992  for (i = 0; i < pm->n_pages; i++)
993  vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
994  }
995  /* *INDENT-ON* */
996  }
997 
998  if (r && r->init_function)
999  err = r->init_function (lpm->vlib_main, p->handle);
1000 
1001 error:
1002  vec_free (s);
1003  if (err)
1004  {
1005  if (p->fd != -1)
1006  close (p->fd);
1007  if (p->config_fd != -1 && p->config_fd != p->fd)
1008  close (p->config_fd);
1009  p->config_fd = p->fd = -1;
1010  }
1011  return err;
1012 }
1013 
1014 /* Configuration space read/write. */
1015 clib_error_t *
1017  vlib_read_or_write_t read_or_write,
1018  uword address, void *data, u32 n_bytes)
1019 {
1021  int n;
1022 
1023  if (read_or_write == VLIB_READ)
1024  n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
1025  else
1026  n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
1027 
1028  if (n != n_bytes)
1029  return clib_error_return_unix (0, "%s",
1030  read_or_write == VLIB_READ
1031  ? "read" : "write");
1032 
1033  return 0;
1034 }
1035 
1036 static clib_error_t *
1038  u64 * size, u64 * offset)
1039 {
1041  clib_error_t *error = 0;
1042  int _fd = -1;
1043  u64 _size = 0, _offset = 0;
1044 
1045  ASSERT (bar <= 5);
1046 
1047  error = 0;
1048 
1049  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1050  {
1051  u8 *file_name;
1052  struct stat stat_buf;
1053  file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1054  format_vlib_pci_addr, &p->addr, bar, 0);
1055 
1056  _fd = open ((char *) file_name, O_RDWR);
1057  if (_fd < 0)
1058  {
1059  error = clib_error_return_unix (0, "open `%s'", file_name);
1060  vec_free (file_name);
1061  return error;
1062  }
1063 
1064  if (fstat (_fd, &stat_buf) < 0)
1065  {
1066  error = clib_error_return_unix (0, "fstat `%s'", file_name);
1067  vec_free (file_name);
1068  close (_fd);
1069  return error;
1070  }
1071 
1072  vec_free (file_name);
1073  _size = stat_buf.st_size;
1074  _offset = 0;
1075  }
1076  else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1077  {
1078  struct vfio_region_info reg = { 0 };
1079  reg.argsz = sizeof (struct vfio_region_info);
1080  reg.index = bar;
1081  if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1082  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1083  "'%U'", format_vlib_pci_addr,
1084  &p->addr);
1085  _fd = p->fd;
1086  _size = reg.size;
1087  _offset = reg.offset;
1088  pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
1089  "flags: %s%s%s(0x%x)", __func__,
1090  reg.index, reg.size, reg.offset,
1091  reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
1092  reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
1093  reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
1094  reg.flags);
1095  }
1096  else
1097  ASSERT (0);
1098 
1099  *fd = _fd;
1100  *size = _size;
1101  *offset = _offset;
1102 
1103  return error;
1104 }
1105 
1106 static clib_error_t *
1108  u32 bar, u8 * addr, void **result)
1109 {
1111  int fd = -1;
1112  clib_error_t *error;
1113  int flags = MAP_SHARED;
1114  u64 size = 0, offset = 0;
1115 
1116  pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1117 
1118  if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1119  return error;
1120 
1121  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1122  flags |= MAP_FIXED;
1123 
1124  *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
1125  if (*result == (void *) -1)
1126  {
1127  error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1128  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1129  close (fd);
1130  return error;
1131  }
1132 
1133  /* *INDENT-OFF* */
1135  (linux_pci_region_t) { .fd = -1});
1136  /* *INDENT-ON* */
1137  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1138  p->regions[bar].fd = fd;
1139  p->regions[bar].addr = *result;
1140  p->regions[bar].size = size;
1141  return 0;
1142 }
1143 
1144 clib_error_t *
1146  void **result)
1147 {
1148  return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
1149 }
1150 
1151 clib_error_t *
1153  u32 resource, u8 * addr, void **result)
1154 {
1155  return (vlib_pci_map_region_int (vm, h, resource, addr, result));
1156 }
1157 
1158 clib_error_t *
1160 {
1162  clib_error_t *error = 0;
1163  int fd = -1;
1164  u64 size = 0, offset = 0;
1165 
1166  if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1167  return error;
1168 
1169  p->io_fd = fd;
1170  p->io_offset = offset;
1171  return error;
1172 }
1173 
1174 clib_error_t *
1176  vlib_read_or_write_t read_or_write,
1177  uword offset, void *data, u32 length)
1178 {
1180  int n = 0;
1181 
1182  if (read_or_write == VLIB_READ)
1183  n = pread (p->io_fd, data, length, p->io_offset + offset);
1184  else
1185  n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1186 
1187  if (n != length)
1188  return clib_error_return_unix (0, "%s",
1189  read_or_write == VLIB_READ
1190  ? "read" : "write");
1191  return 0;
1192 }
1193 
1194 clib_error_t *
1196 {
1198 
1199  if (!p->supports_va_dma)
1200  return 0;
1201 
1202  return vfio_map_physmem_page (vm, ptr);
1203 }
1204 
1205 int
1207 {
1209 
1210  return p->supports_va_dma != 0;
1211 }
1212 
1213 clib_error_t *
1214 vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
1215  pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1216 {
1219  linux_pci_device_t *p;
1220  clib_error_t *err = 0;
1221  pci_device_id_t *i;
1222 
1223  di = vlib_pci_get_device_info (vm, addr, &err);
1224 
1225  if (err)
1226  return err;
1227  for (i = ids; i->vendor_id != 0; i++)
1228  if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1229  break;
1230 
1231  if (i->vendor_id == 0)
1232  return clib_error_return (0, "Wrong vendor or device id");
1233 
1234  pool_get (lpm->linux_pci_devices, p);
1235  p->handle = p - lpm->linux_pci_devices;
1236  p->addr.as_u32 = di->addr.as_u32;
1237  p->intx_irq.fd = -1;
1238  p->intx_irq.clib_file_index = -1;
1239  p->numa_node = di->numa_node;
1240  /*
1241  * pci io bar read/write fd
1242  */
1243  p->io_fd = -1;
1244 
1245  pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1246  di->vendor_id, di->device_id, di->driver_name,
1247  di->iommu_group);
1248 
1249  if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
1250  err = add_device_vfio (vm, p, di, 0);
1251  else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
1252  err = add_device_uio (vm, p, di, 0);
1253  else
1254  err = clib_error_create ("device not bound to 'vfio-pci' or "
1255  "'uio_pci_generic' kernel module");
1256  if (err)
1257  goto error;
1258 
1259  *handle = p->handle;
1260 
1261 error:
1263  if (err)
1264  {
1265  pci_log_err (vm, p, "%U", format_clib_error, err);
1266  clib_memset (p, 0, sizeof (linux_pci_device_t));
1267  pool_put (lpm->linux_pci_devices, p);
1268  }
1269 
1270  return err;
1271 }
1272 
1273 void
1275 {
1278  linux_pci_irq_t *irq;
1279  linux_pci_region_t *res;
1280  clib_error_t *err = 0;
1281 
1282  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1283  {
1284  irq = &p->intx_irq;
1285  if (irq->clib_file_index != -1)
1287  close (p->config_fd);
1288  if (p->io_fd != -1)
1289  close (p->io_fd);
1290  }
1291  else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1292  {
1293  irq = &p->intx_irq;
1294  /* close INTx irqs */
1295  if (irq->fd != -1)
1296  {
1297  err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
1298  VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1299  clib_error_free (err);
1300  if (irq->clib_file_index != -1)
1302  close (irq->fd);
1303  }
1304 
1305  /* close MSI-X irqs */
1306  if (vec_len (p->msix_irqs))
1307  {
1308  err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
1309  VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1310  clib_error_free (err);
1311  /* *INDENT-OFF* */
1312  vec_foreach (irq, p->msix_irqs)
1313  {
1314  if (irq->fd == -1)
1315  continue;
1317  close (irq->fd);
1318  }
1319  /* *INDENT-ON* */
1320  vec_free (p->msix_irqs);
1321  }
1322  }
1323 
1324  /* *INDENT-OFF* */
1325  vec_foreach (res, p->regions)
1326  {
1327  if (res->size == 0)
1328  continue;
1329  munmap (res->addr, res->size);
1330  if (res->fd != -1)
1331  close (res->fd);
1332  }
1333  /* *INDENT-ON* */
1334  vec_free (p->regions);
1335 
1336  close (p->fd);
1337  clib_memset (p, 0, sizeof (linux_pci_device_t));
1338  pool_put (lpm->linux_pci_devices, p);
1339 }
1340 
1341 void
1343 {
1344  vlib_pci_main_t *pm = &pci_main;
1347  pci_device_id_t *i;
1348  clib_error_t *err = 0;
1349  linux_pci_device_t *p;
1350 
1351  pool_get (lpm->linux_pci_devices, p);
1352  p->handle = p - lpm->linux_pci_devices;
1353  p->intx_irq.fd = -1;
1354 
1355  r = pm->pci_device_registrations;
1356 
1357  while (r)
1358  {
1359  for (i = r->supported_devices; i->vendor_id != 0; i++)
1360  if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1361  {
1362  if (di->iommu_group != -1)
1363  err = add_device_vfio (vm, p, di, r);
1364  else
1365  err = add_device_uio (vm, p, di, r);
1366 
1367  if (err)
1368  clib_error_report (err);
1369  else
1370  return;
1371  }
1372  r = r->next_registration;
1373  }
1374 
1375  /* No driver, close the PCI config-space FD */
1376  clib_memset (p, 0, sizeof (linux_pci_device_t));
1377  pool_put (lpm->linux_pci_devices, p);
1378 }
1379 
1380 static clib_error_t *
1381 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1382 {
1383  vlib_pci_addr_t addr, **addrv = arg;
1384  unformat_input_t input;
1385  clib_error_t *err = 0;
1386 
1387  unformat_init_string (&input, (char *) dev_dir_name,
1388  vec_len (dev_dir_name));
1389 
1390  if (!unformat (&input, "/sys/bus/pci/devices/%U",
1391  unformat_vlib_pci_addr, &addr))
1392  err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1393 
1394  unformat_free (&input);
1395 
1396  if (err)
1397  return err;
1398 
1399  vec_add1 (*addrv, addr);
1400  return 0;
1401 }
1402 
1403 static int
1404 pci_addr_cmp (void *v1, void *v2)
1405 {
1406  vlib_pci_addr_t *a1 = v1;
1407  vlib_pci_addr_t *a2 = v2;
1408 
1409  if (a1->domain > a2->domain)
1410  return 1;
1411  if (a1->domain < a2->domain)
1412  return -1;
1413  if (a1->bus > a2->bus)
1414  return 1;
1415  if (a1->bus < a2->bus)
1416  return -1;
1417  if (a1->slot > a2->slot)
1418  return 1;
1419  if (a1->slot < a2->slot)
1420  return -1;
1421  if (a1->function > a2->function)
1422  return 1;
1423  if (a1->function < a2->function)
1424  return -1;
1425  return 0;
1426 }
1427 
1428 vlib_pci_addr_t *
1430 {
1431  vlib_pci_addr_t *addrs = 0;
1432  clib_error_t *err;
1434  &addrs, /* scan_dirs */ 0);
1435  if (err)
1436  {
1437  vec_free (addrs);
1438  return 0;
1439  }
1440 
1442 
1443  return addrs;
1444 }
1445 
1446 clib_error_t *
1448 {
1449  vlib_pci_main_t *pm = &pci_main;
1450  vlib_pci_addr_t *addr = 0, *addrs;
1451 
1452  pm->vlib_main = vm;
1453 
1454  ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1455 
1456  addrs = vlib_pci_get_all_dev_addrs ();
1457  /* *INDENT-OFF* */
1458  vec_foreach (addr, addrs)
1459  {
1461  if ((d = vlib_pci_get_device_info (vm, addr, 0)))
1462  {
1465  }
1466  }
1467  /* *INDENT-ON* */
1468 
1469  return 0;
1470 }
1471 
1472 /* *INDENT-OFF* */
1474 {
1475  .runs_after = VLIB_INITS("unix_input_init"),
1476 };
1477 /* *INDENT-ON* */
1478 
1479 /*
1480  * fd.io coding-style-patch-verification: ON
1481  *
1482  * Local Variables:
1483  * eval: (c-set-style "gnu")
1484  * End:
1485  */
static clib_error_t * linux_pci_vfio_unmask_intx(vlib_main_t *vm, linux_pci_device_t *d)
Definition: pci.c:656
clib_error_t * pci_bus_init(vlib_main_t *vm)
Definition: pci.c:252
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:440
u8 count
Definition: dhcp.api:208
clib_error_t * vlib_pci_map_region_fixed(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
Definition: pci.c:1152
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
static clib_error_t * vlib_pci_intr_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:239
#define clib_strncmp(s1, s2, n)
Definition: string.h:880
static const char * sysfs_pci_dev_path
Definition: pci.c:58
linux_pci_device_type_t type
Definition: pci.c:97
linux_pci_irq_t intx_irq
Definition: pci.c:120
unsigned long u64
Definition: types.h:89
pci_device_registration_t * pci_device_registrations
Definition: pci.h:156
void vlib_pci_device_close(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1274
vlib_read_or_write_t
Definition: defs.h:54
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
vlib_pci_main_t pci_main
Definition: pci.c:53
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 vlib_pci_get_num_msix_interrupts(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:177
u32 physmem_map_index
Definition: buffer.h:430
static int directory_exists(char *path)
Definition: pci.c:375
u32 file_descriptor
Definition: file.h:54
static clib_error_t * scan_pci_addr(void *arg, u8 *dev_dir_name, u8 *ignored)
Definition: pci.c:1381
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
static const char uio_drv_name[]
Definition: vmbus.c:39
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
Definition: pci.h:114
int i
static int pci_addr_cmp(void *v1, void *v2)
Definition: pci.c:1404
vlib_buffer_main_t * buffer_main
Definition: main.h:152
static clib_error_t * linux_pci_uio_read_ready(clib_file_t *uf)
Definition: pci.c:636
clib_error_t * vlib_pci_enable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
Definition: pci.c:882
void() pci_msix_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle, u16 line)
Definition: pci.h:132
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vl_api_fib_path_t path
Definition: mfib_types.api:34
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:87
#define pci_log_err(vm, dev, f,...)
Definition: pci.c:66
vlib_pci_addr_t addr
Definition: pci.c:99
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:237
vhost_vring_addr_t addr
Definition: vhost_user.h:147
u8 supports_va_dma
Definition: pci.c:126
clib_error_t * clib_sysfs_write(char *file_name, char *fmt,...)
Definition: sysfs.c:26
static clib_error_t * linux_pci_vfio_error_ready(clib_file_t *uf)
Definition: pci.c:710
unsigned char u8
Definition: types.h:56
void * addr
Definition: pci.c:73
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
clib_error_t * vlib_pci_read_write_config(vlib_main_t *vm, vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: pci.c:1016
static clib_error_t * linux_pci_vfio_intx_read_ready(clib_file_t *uf)
Definition: pci.c:690
static clib_error_t * vlib_pci_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 bar, int *fd, u64 *size, u64 *offset)
Definition: pci.c:1037
int fd
Definition: pci.c:79
static void pci_config_type1_little_to_host(pci_config_type1_regs_t *r)
Definition: pci_config.h:353
clib_error_t * vlib_pci_map_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h, void *ptr)
Definition: pci.c:1195
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static clib_error_t * linux_pci_vfio_msix_read_ready(clib_file_t *uf)
Definition: pci.c:671
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
Definition: pci.c:1429
clib_file_main_t file_main
Definition: main.c:63
static char * sysfs_mod_vfio_noiommu
Definition: pci.c:60
vlib_main_t * vlib_main
Definition: pci.c:133
unsigned int u32
Definition: types.h:88
linux_pci_irq_t * msix_irqs
Definition: pci.c:121
#define clib_error_create(args...)
Definition: error.h:96
clib_error_t * linux_vfio_group_get_device_fd(vlib_pci_addr_t *addr, int *fdp, int *is_noiommu)
Definition: vfio.c:177
pci_config_type1_regs_t config1
Definition: pci.h:88
vlib_pci_dev_handle_t handle
Definition: pci.c:98
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
uword private_data
Definition: pci.c:124
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_main_t *vm, vlib_pci_addr_t *addr, clib_error_t **error)
Definition: pci.c:202
u8 config_data[256]
Definition: pci.h:89
u8 * description
Definition: file.h:70
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
u32 clib_file_index
Definition: pci.c:80
static const char * sysfs_pci_drv_path
Definition: pci.c:59
void vlib_pci_set_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h, uword private_data)
Definition: pci.c:155
clib_error_t * vlib_pci_bind_to_uio(vlib_main_t *vm, vlib_pci_addr_t *addr, char *uio_drv_name)
Definition: pci.c:385
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
Definition: pci.c:565
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define clib_error_return_unix(e, args...)
Definition: error.h:102
u64 size
Definition: vhost_user.h:140
void di(unformat_input_t *i)
Definition: unformat.c:163
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:287
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:454
clib_error_t * vlib_pci_register_msix_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 start, u32 count, pci_msix_handler_function_t *msix_handler)
Definition: pci.c:825
linux_pci_main_t linux_pci_main
Definition: pci.c:199
clib_error_t * linux_pci_init(vlib_main_t *vm)
Definition: pci.c:1447
void init_device_from_registered(vlib_main_t *vm, vlib_pci_device_info_t *di)
Definition: pci.c:1342
uword vlib_pci_get_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:148
u32 vlib_pci_dev_handle_t
Definition: pci.h:97
clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
Definition: sysfs.c:50
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 len
Definition: ip_types.api:91
linux_pci_device_type_t
Definition: pci.c:88
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
Definition: pci.c:141
static void pci_config_type0_little_to_host(pci_config_type0_regs_t *r)
Definition: pci_config.h:279
u32 vlib_pci_get_numa_node(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:170
#define clib_strncpy(d, s, n)
Definition: string.h:995
#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU
Definition: pci.h:63
u32 flags
Definition: vhost_user.h:141
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
u8 * driver_name
Definition: pci.h:82
#define pci_log_debug(vm, dev, f,...)
Definition: pci.c:63
clib_error_t * vlib_pci_read_write_io(vlib_main_t *vm, vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword offset, void *data, u32 length)
Definition: pci.c:1175
void() pci_intx_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle)
Definition: pci.h:130
clib_error_t * vlib_pci_disable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
Definition: pci.c:904
pci_msix_handler_function_t * msix_handler
Definition: pci.c:84
vlib_pci_addr_t * vlib_pci_get_addr(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:163
size_t size
Definition: pci.c:74
#define ASSERT(truth)
static clib_error_t * vfio_set_irqs(vlib_main_t *vm, linux_pci_device_t *p, u32 index, u32 start, u32 count, u32 flags, int *efds)
Definition: pci.c:580
manual_print typedef address
Definition: ip_types.api:84
u8 data[128]
Definition: ipsec_types.api:87
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
static void clib_file_del_by_index(clib_file_main_t *um, uword index)
Definition: file.h:119
static clib_error_t * linux_pci_uio_error_ready(clib_file_t *uf)
Definition: pci.c:663
pci_config_type0_regs_t config0
Definition: pci.h:87
#define clib_error_report(e)
Definition: error.h:113
clib_error_t * vfio_map_physmem_page(vlib_main_t *vm, void *addr)
Definition: vfio.c:38
vlib_pci_addr_t addr
Definition: pci.h:66
clib_error_t * vlib_pci_register_intx_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, pci_intx_handler_function_t *intx_handler)
Definition: pci.c:774
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
template key/value backing page structure
Definition: bihash_doc.h:44
Definition: defs.h:56
static clib_error_t * vlib_pci_map_region_int(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 bar, u8 *addr, void **result)
Definition: pci.c:1107
static clib_error_t * add_device_vfio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
Definition: pci.c:922
clib_error_t * foreach_directory_file(char *dir_name, clib_error_t *(*f)(void *arg, u8 *path_name, u8 *file_name), void *arg, int scan_dirs)
Definition: util.c:49
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
Definition: pci.c:124
u64 uword
Definition: types.h:112
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:983
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define clib_error_free(e)
Definition: error.h:86
clib_error_t * vlib_pci_io_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource)
Definition: pci.c:1159
clib_error_t * vlib_pci_map_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, void **result)
Definition: pci.c:1145
clib_file_function_t * error_function
Definition: file.h:67
linux_pci_region_t * regions
Definition: pci.c:103
pci_intx_handler_function_t * intx_handler
Definition: pci.c:83
struct clib_bihash_value offset
template key/value backing page structure
u16 device_id
Definition: pci.h:127
int vlib_pci_supports_virtual_addr_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1206
struct _pci_device_registration pci_device_registration_t
#define vec_foreach(var, vec)
Vector iterator.
uword private_data
Definition: file.h:64
Definition: file.h:51
static clib_error_t * add_device_uio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
Definition: pci.c:718
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:487
u16 device_class
Definition: pci.h:72
linux_pci_device_t * linux_pci_devices
Definition: pci.c:134
vlib_main_t * vlib_main
Definition: pci.h:155
clib_error_t * vlib_pci_device_open(vlib_main_t *vm, vlib_pci_addr_t *addr, pci_device_id_t ids[], vlib_pci_dev_handle_t *handle)
Definition: pci.c:1214
pci_config_header_t header
Definition: pci_config.h:247
u64 config_offset
Definition: pci.c:107
#define VLIB_INITS(...)
Definition: init.h:344
u8 * product_name
Definition: pci.h:77
u16 vendor_id
Definition: pci.h:127
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
u8 * clib_sysfs_link_to_name(char *link)
Definition: sysfs.c:90
static linux_pci_device_t * linux_pci_get_device(vlib_pci_dev_handle_t h)
Definition: pci.c:141