FD.io VPP  v19.08.3-2-gbabecb413
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  clib_bitmap_t *bmp = 0;
212 
213  di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
214  clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
215  di->addr.as_u32 = addr->as_u32;
216 
217  u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
218  format_vlib_pci_addr, addr);
219 
220  f = format (0, "%v/config%c", dev_dir_name, 0);
221  fd = open ((char *) f, O_RDWR);
222 
223  /* Try read-only access if write fails. */
224  if (fd < 0)
225  fd = open ((char *) f, O_RDONLY);
226 
227  if (fd < 0)
228  {
229  err = clib_error_return_unix (0, "open `%s'", f);
230  goto error;
231  }
232 
233  /* You can only read more that 64 bytes of config space as root; so we try to
234  read the full space but fall back to just the first 64 bytes. */
235  if (read (fd, &di->config_data, sizeof (di->config_data)) <
236  sizeof (di->config0))
237  {
238  err = clib_error_return_unix (0, "read `%s'", f);
239  close (fd);
240  goto error;
241  }
242 
243  {
244  static pci_config_header_t all_ones;
245  if (all_ones.vendor_id == 0)
246  clib_memset (&all_ones, ~0, sizeof (all_ones));
247 
248  if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
249  {
250  err = clib_error_return (0, "invalid PCI config for `%s'", f);
251  close (fd);
252  goto error;
253  }
254  }
255 
256  if (di->config0.header.header_type == 0)
258  else
260 
261  di->numa_node = -1;
262  vec_reset_length (f);
263  f = format (f, "%v/numa_node%c", dev_dir_name, 0);
264  err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
265  if (err)
266  {
267  di->numa_node = -1;
268  clib_error_free (err);
269  }
270  if (di->numa_node == -1)
271  {
272  /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
273  it is a SMP system, set numa_node to 0. */
274  if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
275  unformat_bitmap_list, &bmp)))
276  clib_error_free (err);
277  if (clib_bitmap_count_set_bits (bmp) == 1)
278  di->numa_node = 0;
279  }
280 
281  vec_reset_length (f);
282  f = format (f, "%v/class%c", dev_dir_name, 0);
283  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
284  if (err)
285  goto error;
286  di->device_class = tmp >> 8;
287 
288  vec_reset_length (f);
289  f = format (f, "%v/vendor%c", dev_dir_name, 0);
290  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
291  if (err)
292  goto error;
293  di->vendor_id = tmp;
294 
295  vec_reset_length (f);
296  f = format (f, "%v/device%c", dev_dir_name, 0);
297  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
298  if (err)
299  goto error;
300  di->device_id = tmp;
301 
302  vec_reset_length (f);
303  f = format (f, "%v/driver%c", dev_dir_name, 0);
304  di->driver_name = clib_sysfs_link_to_name ((char *) f);
305  if (!di->driver_name)
306  di->driver_name = format (0, "<NONE>%c", 0);
307 
308  di->iommu_group = -1;
309  vec_reset_length (f);
310  f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
311  tmpstr = clib_sysfs_link_to_name ((char *) f);
312  if (tmpstr)
313  {
314  di->iommu_group = atoi ((char *) tmpstr);
315  vec_free (tmpstr);
316  }
317 
318  vec_reset_length (f);
319  f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
320  err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
321  if (err == 0)
322  {
323  if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
325  vec_free (tmpstr);
326  }
327  else
328  clib_error_free (err);
329 
330  close (fd);
331 
332  vec_reset_length (f);
333  f = format (f, "%v/vpd%c", dev_dir_name, 0);
334  fd = open ((char *) f, O_RDONLY);
335  if (fd >= 0)
336  {
337  while (1)
338  {
339  u8 tag[3];
340  u8 *data = 0;
341  uword len;
342 
343  if (read (fd, &tag, 3) != 3)
344  break;
345 
346  if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
347  break;
348 
349  len = (tag[2] << 8) | tag[1];
350  vec_validate (data, len);
351 
352  if (read (fd, data, len) != len)
353  {
354  vec_free (data);
355  break;
356  }
357  if (tag[0] == 0x82)
358  di->product_name = data;
359  else if (tag[0] == 0x90)
360  di->vpd_r = data;
361  else if (tag[0] == 0x91)
362  di->vpd_w = data;
363 
364  data = 0;
365  }
366  close (fd);
367  }
368 
369  goto done;
370 
371 error:
373  di = 0;
374 
375 done:
376  vec_free (bmp);
377  vec_free (f);
378  vec_free (dev_dir_name);
379  if (error)
380  *error = err;
381  else
382  clib_error_free (err);
383  return di;
384 }
385 
386 static int
387 directory_exists (char *path)
388 {
389  struct stat s = { 0 };
390  if (stat (path, &s) == -1)
391  return 0;
392 
393  return S_ISDIR (s.st_mode);
394 }
395 
396 clib_error_t *
397 vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
398  char *uio_drv_name)
399 {
400  clib_error_t *error = 0;
401  u8 *s = 0, *driver_name = 0;
402  DIR *dir = 0;
403  struct dirent *e;
405  int fd, clear_driver_override = 0;
406  u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
407  format_vlib_pci_addr, addr);
408 
409  di = vlib_pci_get_device_info (vm, addr, &error);
410 
411  if (error)
412  return error;
413 
414  if (strncmp ("auto", uio_drv_name, 5) == 0)
415  {
416  int vfio_pci_loaded = 0;
417 
418  if (directory_exists ("/sys/module/vfio_pci"))
419  vfio_pci_loaded = 1;
420 
421  if (di->iommu_group != -1)
422  {
423  /* device is bound to IOMMU group */
424  if (!vfio_pci_loaded)
425  {
426  error = clib_error_return (0, "Skipping PCI device %U: device "
427  "is bound to IOMMU group and "
428  "vfio-pci driver is not loaded",
429  format_vlib_pci_addr, addr);
430  goto done;
431  }
432  else
433  uio_drv_name = "vfio-pci";
434  }
435  else
436  {
437  /* device is not bound to IOMMU group so we have multiple options */
438  if (vfio_pci_loaded &&
439  (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
440  uio_drv_name = "vfio-pci";
441  else if (directory_exists ("/sys/module/uio_pci_generic"))
442  uio_drv_name = "uio_pci_generic";
443  else if (directory_exists ("/sys/module/igb_uio"))
444  uio_drv_name = "igb_uio";
445  else
446  {
447  clib_error_free (error);
448  error = clib_error_return (0, "Skipping PCI device %U: missing "
449  "kernel VFIO or UIO driver",
450  format_vlib_pci_addr, addr);
451  goto done;
452  }
453  clib_error_free (error);
454  }
455  }
456 
457  s = format (s, "%v/driver%c", dev_dir_name, 0);
458  driver_name = clib_sysfs_link_to_name ((char *) s);
459  vec_reset_length (s);
460 
461  if (driver_name &&
462  ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
463  (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
464  (strcmp ("igb_uio", (char *) driver_name) == 0)))
465  goto done;
466 
467  /* walk trough all linux interfaces and if interface belonging to
468  this device is founf check if interface is admin up */
469  dir = opendir ("/sys/class/net");
470  s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
471 
472  if (!dir)
473  {
474  error = clib_error_return (0, "Skipping PCI device %U: failed to "
475  "read /sys/class/net",
476  format_vlib_pci_addr, addr);
477  goto done;
478  }
479 
480  fd = socket (PF_INET, SOCK_DGRAM, 0);
481  if (fd < 0)
482  {
483  error = clib_error_return_unix (0, "socket");
484  goto done;
485  }
486 
487  while ((e = readdir (dir)))
488  {
489  struct ifreq ifr;
490  struct ethtool_drvinfo drvinfo;
491 
492  if (e->d_name[0] == '.') /* skip . and .. */
493  continue;
494 
495  clib_memset (&ifr, 0, sizeof ifr);
496  clib_memset (&drvinfo, 0, sizeof drvinfo);
497  ifr.ifr_data = (char *) &drvinfo;
498  clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
499 
500  drvinfo.cmd = ETHTOOL_GDRVINFO;
501  if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
502  {
503  /* Some interfaces (eg "lo") don't support this ioctl */
504  if ((errno != ENOTSUP) && (errno != ENODEV))
505  clib_unix_warning ("ioctl fetch intf %s bus info error",
506  e->d_name);
507  continue;
508  }
509 
510  if (strcmp ((char *) s, drvinfo.bus_info))
511  continue;
512 
513  clib_memset (&ifr, 0, sizeof (ifr));
514  clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
515 
516  if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
517  {
518  error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
519  e->d_name);
520  close (fd);
521  goto done;
522  }
523 
524  if (ifr.ifr_flags & IFF_UP)
525  {
526  error = clib_error_return (0, "Skipping PCI device %U as host "
527  "interface %s is up",
528  format_vlib_pci_addr, addr, e->d_name);
529  close (fd);
530  goto done;
531  }
532  }
533 
534  close (fd);
535  vec_reset_length (s);
536 
537  s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
538  clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
539  vec_reset_length (s);
540 
541  s = format (s, "%v/driver_override%c", dev_dir_name, 0);
542  if (access ((char *) s, F_OK) == 0)
543  {
544  clib_sysfs_write ((char *) s, "%s", uio_drv_name);
545  clear_driver_override = 1;
546  }
547  else
548  {
549  vec_reset_length (s);
550  s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
551  clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
552  di->device_id);
553  }
554  vec_reset_length (s);
555 
556  s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
557  clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
558  vec_reset_length (s);
559 
560  if (clear_driver_override)
561  {
562  s = format (s, "%v/driver_override%c", dev_dir_name, 0);
563  clib_sysfs_write ((char *) s, "%c", 0);
564  vec_reset_length (s);
565  }
566 
567 done:
568  closedir (dir);
569  vec_free (s);
570  vec_free (dev_dir_name);
571  vec_free (driver_name);
572  return error;
573 }
574 
575 
576 static clib_error_t *
577 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
578 {
579  linux_pci_device_t *l = arg;
580  unformat_input_t input;
581 
582  unformat_init_string (&input, (char *) file_name, vec_len (file_name));
583 
584  if (!unformat (&input, "uio%d", &l->uio_minor))
585  abort ();
586 
587  unformat_free (&input);
588  return 0;
589 }
590 
591 static clib_error_t *
593  u32 count, u32 flags, int *efds)
594 {
595  int data_len = efds ? count * sizeof (int) : 0;
596  u8 buf[sizeof (struct vfio_irq_set) + data_len];
597  struct vfio_irq_info ii = { 0 };
598  struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
599 
600 
601  ii.argsz = sizeof (struct vfio_irq_info);
602  ii.index = index;
603 
604  if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
605  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
606  "'%U'", format_vlib_pci_addr, &p->addr);
607 
608  pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
609  __func__, ii.index, ii.count,
610  ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
611  ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
612  ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
613  ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
614  ii.flags);
615 
616  if (ii.count < start + count)
617  return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
618  "'%U'", format_vlib_pci_addr, &p->addr);
619 
620 
621  if (efds)
622  {
623  flags |= VFIO_IRQ_SET_DATA_EVENTFD;
624  clib_memcpy_fast (&irq_set->data, efds, data_len);
625  }
626  else
627  flags |= VFIO_IRQ_SET_DATA_NONE;
628 
629  ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
630  (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
631 
632  irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
633  irq_set->index = index;
634  irq_set->start = start;
635  irq_set->count = count;
636  irq_set->flags = flags;
637 
638  if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
639  return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
640  "[index = %u, start = %u, count = %u, "
641  "flags = 0x%x]",
643  index, start, count, flags);
644  return 0;
645 }
646 
647 static clib_error_t *
649 {
650  vlib_main_t *vm = vlib_get_main ();
651  int __attribute__ ((unused)) rv;
654  linux_pci_irq_t *irq = &p->intx_irq;
655 
656  u32 icount;
657  rv = read (uf->file_descriptor, &icount, 4);
658 
659  if (irq->intx_handler)
660  irq->intx_handler (vm, h);
661 
662  vlib_pci_intr_enable (vm, h);
663 
664  return /* no error */ 0;
665 }
666 
667 static clib_error_t *
669 {
670  return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
671  VFIO_IRQ_SET_ACTION_UNMASK, 0);
672 }
673 
674 static clib_error_t *
676 {
677  u32 error_index = (u32) uf->private_data;
678 
679  return clib_error_return (0, "pci device %d: error", error_index);
680 }
681 
682 static clib_error_t *
684 {
685  vlib_main_t *vm = vlib_get_main ();
686  int __attribute__ ((unused)) rv;
688  u16 line = uf->private_data & 0xffff;
690  linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
691 
692  u64 icount;
693  rv = read (uf->file_descriptor, &icount, sizeof (icount));
694 
695  if (irq->msix_handler)
696  irq->msix_handler (vm, h, line);
697 
698  return /* no error */ 0;
699 }
700 
701 static clib_error_t *
703 {
704  vlib_main_t *vm = vlib_get_main ();
705  int __attribute__ ((unused)) rv;
708  linux_pci_irq_t *irq = &p->intx_irq;
709 
710  u64 icount;
711  rv = read (uf->file_descriptor, &icount, sizeof (icount));
712 
713  if (irq->intx_handler)
714  irq->intx_handler (vm, h);
715 
717 
718  return /* no error */ 0;
719 }
720 
721 static clib_error_t *
723 {
724  u32 error_index = (u32) uf->private_data;
725 
726  return clib_error_return (0, "pci device %d: error", error_index);
727 }
728 
729 static clib_error_t *
732 {
734  clib_error_t *err = 0;
735  u8 *s = 0;
736 
737  p->fd = -1;
739 
740  s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
741  format_vlib_pci_addr, &di->addr, 0);
742 
743  p->config_fd = open ((char *) s, O_RDWR);
744  p->config_offset = 0;
745  vec_reset_length (s);
746 
747  if (p->config_fd == -1)
748  {
749  err = clib_error_return_unix (0, "open '%s'", s);
750  goto error;
751  }
752 
753  s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
754  format_vlib_pci_addr, &di->addr, 0);
755  foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
756  1);
757  vec_reset_length (s);
758 
759  s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
760  p->fd = open ((char *) s, O_RDWR);
761  if (p->fd < 0)
762  {
763  err = clib_error_return_unix (0, "open '%s'", s);
764  goto error;
765  }
766 
767  if (r && r->interrupt_handler)
768  vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
769 
770  if (r && r->init_function)
771  err = r->init_function (lpm->vlib_main, p->handle);
772 
773 error:
774  vec_free (s);
775  if (err)
776  {
777  if (p->config_fd != -1)
778  close (p->config_fd);
779  if (p->fd != -1)
780  close (p->fd);
781  }
782  return err;
783 }
784 
785 clib_error_t *
787  pci_intx_handler_function_t * intx_handler)
788 {
790  clib_file_t t = { 0 };
791  linux_pci_irq_t *irq = &p->intx_irq;
792  ASSERT (irq->fd == -1);
793 
795  {
796  struct vfio_irq_info ii = { 0 };
797  ii.argsz = sizeof (struct vfio_irq_info);
798  ii.index = VFIO_PCI_INTX_IRQ_INDEX;
799  if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
800  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
801  "%U'", format_vlib_pci_addr, &p->addr);
802  pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
803  __func__, ii.index, ii.count,
804  ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
805  ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
806  ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
807  ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
808  ii.flags);
809  if (ii.count != 1)
810  return clib_error_return (0, "INTx interrupt does not exist on device"
811  "'%U'", format_vlib_pci_addr, &p->addr);
812 
813  irq->fd = eventfd (0, EFD_NONBLOCK);
814  if (irq->fd == -1)
815  return clib_error_return_unix (0, "eventfd");
816 
817  t.file_descriptor = irq->fd;
819  }
820  else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
821  {
822  t.file_descriptor = p->fd;
824  }
825  else
826  return 0;
827 
829  t.private_data = p->handle;
830  t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
832  irq->intx_handler = intx_handler;
833  return 0;
834 }
835 
836 clib_error_t *
838  u32 start, u32 count,
839  pci_msix_handler_function_t * msix_handler)
840 {
841  clib_error_t *err = 0;
843  u32 i;
844 
846  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
847  "support");
848 
849  /* *INDENT-OFF* */
850  vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
851  { .fd = -1});
852  /* *INDENT-ON* */
853 
854  for (i = start; i < start + count; i++)
855  {
856  clib_file_t t = { 0 };
858  ASSERT (irq->fd == -1);
859 
860  irq->fd = eventfd (0, EFD_NONBLOCK);
861  if (irq->fd == -1)
862  {
863  err = clib_error_return_unix (0, "eventfd");
864  goto error;
865  }
866 
868  t.file_descriptor = irq->fd;
870  t.private_data = p->handle << 16 | i;
871  t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
872  &p->addr, i);
874  irq->msix_handler = msix_handler;
875  }
876 
877  return 0;
878 
879 error:
880  while (i-- > start)
881  {
883  if (irq->fd != -1)
884  {
886  close (irq->fd);
887  irq->fd = -1;
888  }
889  }
890  return err;
891 }
892 
893 clib_error_t *
895  u16 start, u16 count)
896 {
898  int fds[count];
899  int i;
900 
902  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
903  "support");
904 
905  for (i = start; i < start + count; i++)
906  {
908  fds[i] = irq->fd;
909  }
910 
911  return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
912  VFIO_IRQ_SET_ACTION_TRIGGER, fds);
913 }
914 
915 clib_error_t *
917  u16 start, u16 count)
918 {
920  int i, fds[count];
921 
923  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
924  "support");
925 
926  for (i = start; i < start + count; i++)
927  fds[i] = -1;
928 
929  return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
930  VFIO_IRQ_SET_ACTION_TRIGGER, fds);
931 }
932 
933 static clib_error_t *
936 {
938  struct vfio_device_info device_info = { 0 };
939  struct vfio_region_info reg = { 0 };
940  clib_error_t *err = 0;
941  u8 *s = 0;
942  int is_noiommu;
943 
945 
946  if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
947  return err;
948 
949  if (is_noiommu == 0)
950  p->supports_va_dma = 1;
951 
952  device_info.argsz = sizeof (device_info);
953  if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
954  {
955  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
956  format_vlib_pci_addr, &di->addr);
957  goto error;
958  }
959 
960  reg.argsz = sizeof (struct vfio_region_info);
961  reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
962  if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
963  {
964  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
965  format_vlib_pci_addr, &di->addr);
966  goto error;
967  }
968 
969  pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
970  "flags: %s%s%s(0x%x)", __func__,
971  reg.index, reg.size, reg.offset,
972  reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
973  reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
974  reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
975  reg.flags);
976 
977  p->config_offset = reg.offset;
978  p->config_fd = p->fd;
979 
980  /* reset if device supports it */
981  if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
982  if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
983  {
984  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
985  format_vlib_pci_addr, &di->addr);
986  goto error;
987  }
988 
989  if (r && r->interrupt_handler)
990  {
991  vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
993  }
994 
995  if (p->supports_va_dma)
996  {
997  vlib_buffer_pool_t *bp;
998  /* *INDENT-OFF* */
1000  {
1001  u32 i;
1002  vlib_physmem_map_t *pm;
1003  pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1004  for (i = 0; i < pm->n_pages; i++)
1005  vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1006  }
1007  /* *INDENT-ON* */
1008  }
1009 
1010  if (r && r->init_function)
1011  err = r->init_function (lpm->vlib_main, p->handle);
1012 
1013 error:
1014  vec_free (s);
1015  if (err)
1016  {
1017  if (p->fd != -1)
1018  close (p->fd);
1019  if (p->config_fd != -1 && p->config_fd != p->fd)
1020  close (p->config_fd);
1021  p->config_fd = p->fd = -1;
1022  }
1023  return err;
1024 }
1025 
1026 /* Configuration space read/write. */
1027 clib_error_t *
1029  vlib_read_or_write_t read_or_write,
1030  uword address, void *data, u32 n_bytes)
1031 {
1033  int n;
1034 
1035  if (read_or_write == VLIB_READ)
1036  n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
1037  else
1038  n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
1039 
1040  if (n != n_bytes)
1041  return clib_error_return_unix (0, "%s",
1042  read_or_write == VLIB_READ
1043  ? "read" : "write");
1044 
1045  return 0;
1046 }
1047 
1048 static clib_error_t *
1050  u64 * size, u64 * offset)
1051 {
1053  clib_error_t *error = 0;
1054  int _fd = -1;
1055  u64 _size = 0, _offset = 0;
1056 
1057  ASSERT (bar <= 5);
1058 
1059  error = 0;
1060 
1061  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1062  {
1063  u8 *file_name;
1064  struct stat stat_buf;
1065  file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1066  format_vlib_pci_addr, &p->addr, bar, 0);
1067 
1068  _fd = open ((char *) file_name, O_RDWR);
1069  if (_fd < 0)
1070  {
1071  error = clib_error_return_unix (0, "open `%s'", file_name);
1072  vec_free (file_name);
1073  return error;
1074  }
1075 
1076  if (fstat (_fd, &stat_buf) < 0)
1077  {
1078  error = clib_error_return_unix (0, "fstat `%s'", file_name);
1079  vec_free (file_name);
1080  close (_fd);
1081  return error;
1082  }
1083 
1084  vec_free (file_name);
1085  _size = stat_buf.st_size;
1086  _offset = 0;
1087  }
1088  else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1089  {
1090  struct vfio_region_info reg = { 0 };
1091  reg.argsz = sizeof (struct vfio_region_info);
1092  reg.index = bar;
1093  if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1094  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1095  "'%U'", format_vlib_pci_addr,
1096  &p->addr);
1097  _fd = p->fd;
1098  _size = reg.size;
1099  _offset = reg.offset;
1100  pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
1101  "flags: %s%s%s(0x%x)", __func__,
1102  reg.index, reg.size, reg.offset,
1103  reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
1104  reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
1105  reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
1106  reg.flags);
1107  }
1108  else
1109  ASSERT (0);
1110 
1111  *fd = _fd;
1112  *size = _size;
1113  *offset = _offset;
1114 
1115  return error;
1116 }
1117 
1118 static clib_error_t *
1120  u32 bar, u8 * addr, void **result)
1121 {
1123  int fd = -1;
1124  clib_error_t *error;
1125  int flags = MAP_SHARED;
1126  u64 size = 0, offset = 0;
1127 
1128  pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1129 
1130  if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1131  return error;
1132 
1133  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1134  flags |= MAP_FIXED;
1135 
1136  *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
1137  if (*result == (void *) -1)
1138  {
1139  error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1140  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
1141  close (fd);
1142  return error;
1143  }
1144 
1145  /* *INDENT-OFF* */
1147  (linux_pci_region_t) { .fd = -1});
1148  /* *INDENT-ON* */
1149  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1150  p->regions[bar].fd = fd;
1151  p->regions[bar].addr = *result;
1152  p->regions[bar].size = size;
1153  return 0;
1154 }
1155 
1156 clib_error_t *
1158  void **result)
1159 {
1160  return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
1161 }
1162 
1163 clib_error_t *
1165  u32 resource, u8 * addr, void **result)
1166 {
1167  return (vlib_pci_map_region_int (vm, h, resource, addr, result));
1168 }
1169 
1170 clib_error_t *
1172 {
1174  clib_error_t *error = 0;
1175  int fd = -1;
1176  u64 size = 0, offset = 0;
1177 
1178  if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1179  return error;
1180 
1181  p->io_fd = fd;
1182  p->io_offset = offset;
1183  return error;
1184 }
1185 
1186 clib_error_t *
1188  vlib_read_or_write_t read_or_write,
1189  uword offset, void *data, u32 length)
1190 {
1192  int n = 0;
1193 
1194  if (read_or_write == VLIB_READ)
1195  n = pread (p->io_fd, data, length, p->io_offset + offset);
1196  else
1197  n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1198 
1199  if (n != length)
1200  return clib_error_return_unix (0, "%s",
1201  read_or_write == VLIB_READ
1202  ? "read" : "write");
1203  return 0;
1204 }
1205 
1206 clib_error_t *
1208 {
1210 
1211  if (!p->supports_va_dma)
1212  return 0;
1213 
1214  return vfio_map_physmem_page (vm, ptr);
1215 }
1216 
1217 int
1219 {
1221 
1222  return p->supports_va_dma != 0;
1223 }
1224 
1225 clib_error_t *
1226 vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
1227  pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1228 {
1231  linux_pci_device_t *p;
1232  clib_error_t *err = 0;
1233  pci_device_id_t *i;
1234 
1235  di = vlib_pci_get_device_info (vm, addr, &err);
1236 
1237  if (err)
1238  return err;
1239  for (i = ids; i->vendor_id != 0; i++)
1240  if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1241  break;
1242 
1243  if (i->vendor_id == 0)
1244  return clib_error_return (0, "Wrong vendor or device id");
1245 
1246  pool_get (lpm->linux_pci_devices, p);
1247  p->handle = p - lpm->linux_pci_devices;
1248  p->addr.as_u32 = di->addr.as_u32;
1249  p->intx_irq.fd = -1;
1250  p->intx_irq.clib_file_index = -1;
1251  p->numa_node = di->numa_node;
1252  /*
1253  * pci io bar read/write fd
1254  */
1255  p->io_fd = -1;
1256 
1257  pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1258  di->vendor_id, di->device_id, di->driver_name,
1259  di->iommu_group);
1260 
1261  if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
1262  err = add_device_vfio (vm, p, di, 0);
1263  else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
1264  err = add_device_uio (vm, p, di, 0);
1265  else
1266  err = clib_error_create ("device not bound to 'vfio-pci' or "
1267  "'uio_pci_generic' kernel module");
1268  if (err)
1269  goto error;
1270 
1271  *handle = p->handle;
1272 
1273 error:
1275  if (err)
1276  {
1277  pci_log_err (vm, p, "%U", format_clib_error, err);
1278  clib_memset (p, 0, sizeof (linux_pci_device_t));
1279  pool_put (lpm->linux_pci_devices, p);
1280  }
1281 
1282  return err;
1283 }
1284 
1285 void
1287 {
1290  linux_pci_irq_t *irq;
1291  linux_pci_region_t *res;
1292  clib_error_t *err = 0;
1293 
1294  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1295  {
1296  irq = &p->intx_irq;
1297  if (irq->clib_file_index != -1)
1299  close (p->config_fd);
1300  if (p->io_fd != -1)
1301  close (p->io_fd);
1302  }
1303  else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1304  {
1305  irq = &p->intx_irq;
1306  /* close INTx irqs */
1307  if (irq->fd != -1)
1308  {
1309  err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
1310  VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1311  clib_error_free (err);
1312  if (irq->clib_file_index != -1)
1314  close (irq->fd);
1315  }
1316 
1317  /* close MSI-X irqs */
1318  if (vec_len (p->msix_irqs))
1319  {
1320  err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
1321  VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1322  clib_error_free (err);
1323  /* *INDENT-OFF* */
1324  vec_foreach (irq, p->msix_irqs)
1325  {
1326  if (irq->fd == -1)
1327  continue;
1329  close (irq->fd);
1330  }
1331  /* *INDENT-ON* */
1332  vec_free (p->msix_irqs);
1333  }
1334  }
1335 
1336  /* *INDENT-OFF* */
1337  vec_foreach (res, p->regions)
1338  {
1339  if (res->size == 0)
1340  continue;
1341  munmap (res->addr, res->size);
1342  if (res->fd != -1)
1343  close (res->fd);
1344  }
1345  /* *INDENT-ON* */
1346  vec_free (p->regions);
1347 
1348  close (p->fd);
1349  clib_memset (p, 0, sizeof (linux_pci_device_t));
1350  pool_put (lpm->linux_pci_devices, p);
1351 }
1352 
1353 void
1355 {
1356  vlib_pci_main_t *pm = &pci_main;
1359  pci_device_id_t *i;
1360  clib_error_t *err = 0;
1361  linux_pci_device_t *p;
1362 
1363  pool_get (lpm->linux_pci_devices, p);
1364  p->handle = p - lpm->linux_pci_devices;
1365  p->intx_irq.fd = -1;
1366 
1367  r = pm->pci_device_registrations;
1368 
1369  while (r)
1370  {
1371  for (i = r->supported_devices; i->vendor_id != 0; i++)
1372  if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1373  {
1374  if (di->iommu_group != -1)
1375  err = add_device_vfio (vm, p, di, r);
1376  else
1377  err = add_device_uio (vm, p, di, r);
1378 
1379  if (err)
1380  clib_error_report (err);
1381  else
1382  return;
1383  }
1384  r = r->next_registration;
1385  }
1386 
1387  /* No driver, close the PCI config-space FD */
1388  clib_memset (p, 0, sizeof (linux_pci_device_t));
1389  pool_put (lpm->linux_pci_devices, p);
1390 }
1391 
1392 static clib_error_t *
1393 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1394 {
1395  vlib_pci_addr_t addr, **addrv = arg;
1396  unformat_input_t input;
1397  clib_error_t *err = 0;
1398 
1399  unformat_init_string (&input, (char *) dev_dir_name,
1400  vec_len (dev_dir_name));
1401 
1402  if (!unformat (&input, "/sys/bus/pci/devices/%U",
1403  unformat_vlib_pci_addr, &addr))
1404  err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1405 
1406  unformat_free (&input);
1407 
1408  if (err)
1409  return err;
1410 
1411  vec_add1 (*addrv, addr);
1412  return 0;
1413 }
1414 
1415 static int
1416 pci_addr_cmp (void *v1, void *v2)
1417 {
1418  vlib_pci_addr_t *a1 = v1;
1419  vlib_pci_addr_t *a2 = v2;
1420 
1421  if (a1->domain > a2->domain)
1422  return 1;
1423  if (a1->domain < a2->domain)
1424  return -1;
1425  if (a1->bus > a2->bus)
1426  return 1;
1427  if (a1->bus < a2->bus)
1428  return -1;
1429  if (a1->slot > a2->slot)
1430  return 1;
1431  if (a1->slot < a2->slot)
1432  return -1;
1433  if (a1->function > a2->function)
1434  return 1;
1435  if (a1->function < a2->function)
1436  return -1;
1437  return 0;
1438 }
1439 
1440 vlib_pci_addr_t *
1442 {
1443  vlib_pci_addr_t *addrs = 0;
1444  clib_error_t *err;
1446  &addrs, /* scan_dirs */ 0);
1447  if (err)
1448  {
1449  vec_free (addrs);
1450  return 0;
1451  }
1452 
1454 
1455  return addrs;
1456 }
1457 
1458 clib_error_t *
1460 {
1461  vlib_pci_main_t *pm = &pci_main;
1462  vlib_pci_addr_t *addr = 0, *addrs;
1463 
1464  pm->vlib_main = vm;
1465 
1466  ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1467 
1468  addrs = vlib_pci_get_all_dev_addrs ();
1469  /* *INDENT-OFF* */
1470  vec_foreach (addr, addrs)
1471  {
1473  if ((d = vlib_pci_get_device_info (vm, addr, 0)))
1474  {
1477  }
1478  }
1479  /* *INDENT-ON* */
1480 
1481  return 0;
1482 }
1483 
1484 /* *INDENT-OFF* */
1486 {
1487  .runs_after = VLIB_INITS("unix_input_init"),
1488 };
1489 /* *INDENT-ON* */
1490 
1491 /*
1492  * fd.io coding-style-patch-verification: ON
1493  *
1494  * Local Variables:
1495  * eval: (c-set-style "gnu")
1496  * End:
1497  */
static clib_error_t * linux_pci_vfio_unmask_intx(vlib_main_t *vm, linux_pci_device_t *d)
Definition: pci.c:668
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:439
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:1164
typedef address
Definition: ip_types.api:83
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
u32 flags
Definition: vhost_user.h:141
#define clib_strncmp(s1, s2, n)
Definition: string.h:863
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:1286
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:428
static int directory_exists(char *path)
Definition: pci.c:387
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:1393
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
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:1416
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:648
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:894
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
u8 data[128]
Definition: ipsec.api:251
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:236
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:722
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:1028
static clib_error_t * linux_pci_vfio_intx_read_ready(clib_file_t *uf)
Definition: pci.c:702
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:1049
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:1207
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
void di(unformat_input_t *i)
Definition: unformat.c:163
static clib_error_t * linux_pci_vfio_msix_read_ready(clib_file_t *uf)
Definition: pci.c:683
#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:1441
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:514
uword size
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:397
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
Definition: pci.c:577
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
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:451
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:837
linux_pci_main_t linux_pci_main
Definition: pci.c:199
clib_error_t * linux_pci_init(vlib_main_t *vm)
Definition: pci.c:1459
void init_device_from_registered(vlib_main_t *vm, vlib_pci_device_info_t *di)
Definition: pci.c:1354
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
u8 len
Definition: ip_types.api:90
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:978
#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU
Definition: pci.h:63
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
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:1187
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:916
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:592
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:675
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
size_t count
Definition: vapi.c:47
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:786
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
Definition: bitmap.h:462
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:1119
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:934
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:1171
clib_error_t * vlib_pci_map_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, void **result)
Definition: pci.c:1157
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:1218
struct _pci_device_registration pci_device_registration_t
uword clib_bitmap_t
Definition: bitmap.h:50
#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:730
#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:486
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:1226
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