blob: b99f54f2a62b7bf5a5ceb0638f0926cd9fef0411 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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
Damjan Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#include <vlib/vlib.h>
43#include <vlib/pci/pci.h>
44#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010045#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020051#include <sys/ioctl.h>
52#include <net/if.h>
53#include <linux/ethtool.h>
54#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020055#include <linux/vfio.h>
56#include <sys/eventfd.h>
57
58static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
59static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010060static char *sysfs_mod_vfio_noiommu =
61 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020062
Damjan Marion23227982018-10-22 13:38:57 +020063#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
Dave Barach9b8ffd92016-07-08 08:13:45 -040070typedef struct
71{
Damjan Marion2060db82018-03-04 17:37:15 +010072 int fd;
73 void *addr;
74 size_t size;
75} linux_pci_region_t;
76
Damjan Marion599a16b2018-03-04 19:35:23 +010077typedef struct
78{
79 int fd;
80 u32 clib_file_index;
81 union
82 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010083 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010084 pci_msix_handler_function_t *msix_handler;
85 };
86} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010087
88typedef enum
89{
90 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
91 LINUX_PCI_DEVICE_TYPE_UIO,
92 LINUX_PCI_DEVICE_TYPE_VFIO,
93} linux_pci_device_type_t;
94
95typedef struct
96{
97 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +020098 vlib_pci_dev_handle_t handle;
99 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100100 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200101
102 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100103 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200104
105 /* File descriptor for config space read/write. */
106 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100107 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200108
Damjan Marionf9a968e2018-02-25 23:26:22 +0100109 /* Device File descriptor */
110 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200111
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200112 /* read/write file descriptor for io bar */
113 int io_fd;
114 u64 io_offset;
115
Damjan Marion5a206ea2016-05-12 22:11:03 +0200116 /* Minor device for uio device. */
117 u32 uio_minor;
118
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100119 /* Interrupt handlers */
120 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100121 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200122
123 /* private data */
124 uword private_data;
125
Damjan Marion23227982018-10-22 13:38:57 +0200126 u8 supports_va_dma;
127
Damjan Marion5a206ea2016-05-12 22:11:03 +0200128} linux_pci_device_t;
129
130/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400131typedef struct
132{
133 vlib_main_t *vlib_main;
134 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200135
Damjan Marion5a206ea2016-05-12 22:11:03 +0200136} linux_pci_main_t;
137
138extern linux_pci_main_t linux_pci_main;
139
Damjan Marion599a16b2018-03-04 19:35:23 +0100140static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200141linux_pci_get_device (vlib_pci_dev_handle_t h)
142{
143 linux_pci_main_t *lpm = &linux_pci_main;
144 return pool_elt_at_index (lpm->linux_pci_devices, h);
145}
146
147uword
Damjan Marion23227982018-10-22 13:38:57 +0200148vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200149{
150 linux_pci_device_t *d = linux_pci_get_device (h);
151 return d->private_data;
152}
153
154void
Damjan Marion23227982018-10-22 13:38:57 +0200155vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
156 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200157{
158 linux_pci_device_t *d = linux_pci_get_device (h);
159 d->private_data = private_data;
160}
161
162vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200163vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200164{
Damjan Marion599a16b2018-03-04 19:35:23 +0100165 linux_pci_device_t *d = linux_pci_get_device (h);
166 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200167}
168
Damjan Mariond2bfb782019-01-07 20:56:04 +0100169u32
170vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
171{
172 linux_pci_device_t *d = linux_pci_get_device (h);
173 return d->numa_node;
174}
175
Damjan Marion5a206ea2016-05-12 22:11:03 +0200176/* Call to allocate/initialize the pci subsystem.
177 This is not an init function so that users can explicitly enable
178 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400179clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181linux_pci_main_t linux_pci_main;
182
Damjan Marioncef87f12017-10-05 15:32:41 +0200183vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200184vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
185 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200186{
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100187 linux_vfio_main_t *lvm = &vfio_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200188 clib_error_t *err;
189 vlib_pci_device_info_t *di;
190 u8 *f = 0;
191 u32 tmp;
192 int fd;
193
194 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400195 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200196 di->addr.as_u32 = addr->as_u32;
197
198 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
199 format_vlib_pci_addr, addr);
200
201 f = format (0, "%v/config%c", dev_dir_name, 0);
202 fd = open ((char *) f, O_RDWR);
203
204 /* Try read-only access if write fails. */
205 if (fd < 0)
206 fd = open ((char *) f, O_RDONLY);
207
208 if (fd < 0)
209 {
210 err = clib_error_return_unix (0, "open `%s'", f);
211 goto error;
212 }
213
214 /* You can only read more that 64 bytes of config space as root; so we try to
215 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100216 if (read (fd, &di->config_data, sizeof (di->config_data)) <
217 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200218 {
219 err = clib_error_return_unix (0, "read `%s'", f);
220 close (fd);
221 goto error;
222 }
223
224 {
225 static pci_config_header_t all_ones;
226 if (all_ones.vendor_id == 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400227 clib_memset (&all_ones, ~0, sizeof (all_ones));
Damjan Marioncef87f12017-10-05 15:32:41 +0200228
229 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
230 {
231 err = clib_error_return (0, "invalid PCI config for `%s'", f);
232 close (fd);
233 goto error;
234 }
235 }
236
237 if (di->config0.header.header_type == 0)
238 pci_config_type0_little_to_host (&di->config0);
239 else
240 pci_config_type1_little_to_host (&di->config1);
241
242 di->numa_node = -1;
243 vec_reset_length (f);
244 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
245 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
246 if (err)
247 {
248 di->numa_node = -1;
249 clib_error_free (err);
250 }
251
252 vec_reset_length (f);
253 f = format (f, "%v/class%c", dev_dir_name, 0);
254 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
255 if (err)
256 goto error;
257 di->device_class = tmp >> 8;
258
259 vec_reset_length (f);
260 f = format (f, "%v/vendor%c", dev_dir_name, 0);
261 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
262 if (err)
263 goto error;
264 di->vendor_id = tmp;
265
266 vec_reset_length (f);
267 f = format (f, "%v/device%c", dev_dir_name, 0);
268 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
269 if (err)
270 goto error;
271 di->device_id = tmp;
272
273 vec_reset_length (f);
274 f = format (f, "%v/driver%c", dev_dir_name, 0);
275 di->driver_name = clib_sysfs_link_to_name ((char *) f);
276
277 di->iommu_group = -1;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100278 if (lvm->container_fd != -1)
Damjan Marioncef87f12017-10-05 15:32:41 +0200279 {
280 u8 *tmpstr;
281 vec_reset_length (f);
282 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
283 tmpstr = clib_sysfs_link_to_name ((char *) f);
284 if (tmpstr)
285 {
286 di->iommu_group = atoi ((char *) tmpstr);
287 vec_free (tmpstr);
288 }
Damjan Marion20ba1642018-03-26 15:35:33 +0200289 vec_reset_length (f);
290 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
291 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
292 if (err == 0)
293 {
294 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
295 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
296 vec_free (tmpstr);
297 }
298 else
299 clib_error_free (err);
Damjan Marioncef87f12017-10-05 15:32:41 +0200300 }
301
Damjan Marion96504182017-12-10 23:54:46 +0100302 close (fd);
303
Damjan Marioncef87f12017-10-05 15:32:41 +0200304 vec_reset_length (f);
305 f = format (f, "%v/vpd%c", dev_dir_name, 0);
306 fd = open ((char *) f, O_RDONLY);
307 if (fd >= 0)
308 {
309 while (1)
310 {
311 u8 tag[3];
312 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100313 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200314
315 if (read (fd, &tag, 3) != 3)
316 break;
317
318 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
319 break;
320
321 len = (tag[2] << 8) | tag[1];
322 vec_validate (data, len);
323
324 if (read (fd, data, len) != len)
325 {
326 vec_free (data);
327 break;
328 }
329 if (tag[0] == 0x82)
330 di->product_name = data;
331 else if (tag[0] == 0x90)
332 di->vpd_r = data;
333 else if (tag[0] == 0x91)
334 di->vpd_w = data;
335
336 data = 0;
337 }
338 close (fd);
339 }
340
341 goto done;
342
343error:
344 vlib_pci_free_device_info (di);
345 di = 0;
346
347done:
348 vec_free (f);
349 vec_free (dev_dir_name);
350 if (error)
351 *error = err;
352 else
353 clib_error_free (err);
354 return di;
355}
356
Damjan Marion2752b892017-12-11 15:55:56 +0100357static int
358directory_exists (char *path)
359{
360 struct stat s = { 0 };
361 if (stat (path, &s) == -1)
362 return 0;
363
364 return S_ISDIR (s.st_mode);
365}
366
Damjan Mariona42cd342016-04-13 18:03:20 +0200367clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200368vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
369 char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200372 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200373 DIR *dir = 0;
374 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200375 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200376 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200377 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
378 format_vlib_pci_addr, addr);
379
Damjan Marion23227982018-10-22 13:38:57 +0200380 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200381
382 if (error)
383 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384
Damjan Marion2752b892017-12-11 15:55:56 +0100385 if (strncmp ("auto", uio_drv_name, 5) == 0)
386 {
387 int vfio_pci_loaded = 0;
388
389 if (directory_exists ("/sys/module/vfio_pci"))
390 vfio_pci_loaded = 1;
391
392 if (di->iommu_group != -1)
393 {
394 /* device is bound to IOMMU group */
395 if (!vfio_pci_loaded)
396 {
397 error = clib_error_return (0, "Skipping PCI device %U: device "
398 "is bound to IOMMU group and "
399 "vfio-pci driver is not loaded",
400 format_vlib_pci_addr, addr);
401 goto done;
402 }
403 else
404 uio_drv_name = "vfio-pci";
405 }
406 else
407 {
408 /* device is not bound to IOMMU group so we have multiple options */
409 if (vfio_pci_loaded &&
410 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
411 uio_drv_name = "vfio-pci";
412 else if (directory_exists ("/sys/module/uio_pci_generic"))
413 uio_drv_name = "uio_pci_generic";
414 else if (directory_exists ("/sys/module/igb_uio"))
415 uio_drv_name = "igb_uio";
416 else
417 {
418 clib_error_free (error);
419 error = clib_error_return (0, "Skipping PCI device %U: missing "
420 "kernel VFIO or UIO driver",
421 format_vlib_pci_addr, addr);
422 goto done;
423 }
424 clib_error_free (error);
425 }
426 }
427
Damjan Mariona7f74572017-05-23 18:32:21 +0200428 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200429 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200430 vec_reset_length (s);
431
Damjan Mariona7f74572017-05-23 18:32:21 +0200432 if (driver_name &&
433 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
434 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
435 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200436 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200437
Damjan Mariona42cd342016-04-13 18:03:20 +0200438 /* walk trough all linux interfaces and if interface belonging to
439 this device is founf check if interface is admin up */
440 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200441 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200442
443 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200445 error = clib_error_return (0, "Skipping PCI device %U: failed to "
446 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200447 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200448 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 }
450
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400452 if (fd < 0)
453 {
454 error = clib_error_return_unix (0, "socket");
455 goto done;
456 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200457
Dave Barach9b8ffd92016-07-08 08:13:45 -0400458 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200460 struct ifreq ifr;
461 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Dave Barach9b8ffd92016-07-08 08:13:45 -0400463 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200464 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Dave Barachb7b92992018-10-17 10:38:51 -0400466 clib_memset (&ifr, 0, sizeof ifr);
467 clib_memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200468 ifr.ifr_data = (char *) &drvinfo;
Marco Varlese99d7a722018-06-27 09:54:44 +0200469 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
470 ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0';
Damjan Mariona42cd342016-04-13 18:03:20 +0200471 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400472 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473 {
John Loe2821212016-07-13 18:13:02 -0400474 /* Some interfaces (eg "lo") don't support this ioctl */
475 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700476 clib_unix_warning ("ioctl fetch intf %s bus info error",
477 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400478 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400479 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200480
481 if (strcmp ((char *) s, drvinfo.bus_info))
482 continue;
483
Dave Barachb7b92992018-10-17 10:38:51 -0400484 clib_memset (&ifr, 0, sizeof (ifr));
Marco Varlese99d7a722018-06-27 09:54:44 +0200485 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
486 ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0';
Chris Luke370e9e32016-07-07 11:01:17 -0400487 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400488 {
489 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
490 e->d_name);
491 close (fd);
492 goto done;
493 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200494
495 if (ifr.ifr_flags & IFF_UP)
496 {
497 error = clib_error_return (0, "Skipping PCI device %U as host "
498 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200499 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400500 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200501 goto done;
502 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 }
504
Damjan Mariona42cd342016-04-13 18:03:20 +0200505 close (fd);
506 vec_reset_length (s);
507
508 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200509 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200510 vec_reset_length (s);
511
Damjan Mariona7f74572017-05-23 18:32:21 +0200512 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
513 if (access ((char *) s, F_OK) == 0)
514 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200515 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200516 clear_driver_override = 1;
517 }
518 else
519 {
520 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200521 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
522 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
523 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200524 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200525 vec_reset_length (s);
526
Damjan Marioncef87f12017-10-05 15:32:41 +0200527 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
528 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200529 vec_reset_length (s);
530
531 if (clear_driver_override)
532 {
533 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200534 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200535 vec_reset_length (s);
536 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200537
538done:
539 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200541 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200542 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 return error;
544}
545
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
547static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400548scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 unformat_input_t input;
552
553 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
554
Dave Barach9b8ffd92016-07-08 08:13:45 -0400555 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 abort ();
557
558 unformat_free (&input);
559 return 0;
560}
561
Dave Barach9b8ffd92016-07-08 08:13:45 -0400562static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200563vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
564 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100565{
566 int data_len = efds ? count * sizeof (int) : 0;
567 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200568 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100569 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
570
571
Damjan Marion23227982018-10-22 13:38:57 +0200572 ii.argsz = sizeof (struct vfio_irq_info);
573 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100574
Damjan Marion23227982018-10-22 13:38:57 +0200575 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100576 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
577 "'%U'", format_vlib_pci_addr, &p->addr);
578
Damjan Marion23227982018-10-22 13:38:57 +0200579 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
580 __func__, ii.index, ii.count,
581 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
582 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
583 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
584 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
585 ii.flags);
586
587 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100588 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
589 "'%U'", format_vlib_pci_addr, &p->addr);
590
591
592 if (efds)
593 {
594 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500595 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100596 }
597 else
598 flags |= VFIO_IRQ_SET_DATA_NONE;
599
600 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
601 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
602
603 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
604 irq_set->index = index;
605 irq_set->start = start;
606 irq_set->count = count;
607 irq_set->flags = flags;
608
609 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
610 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
611 "[index = %u, start = %u, count = %u, "
612 "flags = 0x%x]",
613 format_vlib_pci_addr, &p->addr,
614 index, start, count, flags);
615 return 0;
616}
617
618static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200619linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620{
Damjan Marion23227982018-10-22 13:38:57 +0200621 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200622 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200623 vlib_pci_dev_handle_t h = uf->private_data;
624 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100625 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Damjan Marion5a206ea2016-05-12 22:11:03 +0200627 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400628 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100630 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200631 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200632
Damjan Marion23227982018-10-22 13:38:57 +0200633 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
635 return /* no error */ 0;
636}
637
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200639linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200640{
Damjan Marion23227982018-10-22 13:38:57 +0200641 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100642 VFIO_IRQ_SET_ACTION_UNMASK, 0);
643}
644
Damjan Marioncef87f12017-10-05 15:32:41 +0200645static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200646linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647{
648 u32 error_index = (u32) uf->private_data;
649
650 return clib_error_return (0, "pci device %d: error", error_index);
651}
652
Damjan Marioncef87f12017-10-05 15:32:41 +0200653static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100654linux_pci_vfio_msix_read_ready (clib_file_t * uf)
655{
Damjan Marion23227982018-10-22 13:38:57 +0200656 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100657 int __attribute__ ((unused)) rv;
658 vlib_pci_dev_handle_t h = uf->private_data >> 16;
659 u16 line = uf->private_data & 0xffff;
660 linux_pci_device_t *p = linux_pci_get_device (h);
661 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
662
663 u64 icount;
664 rv = read (uf->file_descriptor, &icount, sizeof (icount));
665
666 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200667 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100668
669 return /* no error */ 0;
670}
671
672static clib_error_t *
673linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674{
Damjan Marion23227982018-10-22 13:38:57 +0200675 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200676 int __attribute__ ((unused)) rv;
677 vlib_pci_dev_handle_t h = uf->private_data;
678 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100679 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Damjan Marioncef87f12017-10-05 15:32:41 +0200681 u64 icount;
682 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100684 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200685 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686
Damjan Marion23227982018-10-22 13:38:57 +0200687 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688
Damjan Marioncef87f12017-10-05 15:32:41 +0200689 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690}
691
Damjan Marioncef87f12017-10-05 15:32:41 +0200692static clib_error_t *
693linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694{
Damjan Marioncef87f12017-10-05 15:32:41 +0200695 u32 error_index = (u32) uf->private_data;
696
697 return clib_error_return (0, "pci device %d: error", error_index);
698}
699
700static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200701add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
702 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200703{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100704 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200705 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200706 u8 *s = 0;
707
Damjan Marionf9a968e2018-02-25 23:26:22 +0100708 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100709 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200710
711 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
712 format_vlib_pci_addr, &di->addr, 0);
713
714 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100715 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200716 vec_reset_length (s);
717
718 if (p->config_fd == -1)
719 {
720 err = clib_error_return_unix (0, "open '%s'", s);
721 goto error;
722 }
723
724 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
725 format_vlib_pci_addr, &di->addr);
726 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
727 1);
728 vec_reset_length (s);
729
730 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100731 p->fd = open ((char *) s, O_RDWR);
732 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200733 {
734 err = clib_error_return_unix (0, "open '%s'", s);
735 goto error;
736 }
737
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100738 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200739 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200740
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100741 if (r && r->init_function)
742 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200743
744error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200745 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200746 if (err)
747 {
Damjan Marion96504182017-12-10 23:54:46 +0100748 if (p->config_fd != -1)
749 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100750 if (p->fd != -1)
751 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200752 }
753 return err;
754}
755
Damjan Marion599a16b2018-03-04 19:35:23 +0100756clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200757vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100758 pci_intx_handler_function_t * intx_handler)
759{
760 linux_pci_device_t *p = linux_pci_get_device (h);
761 clib_file_t t = { 0 };
762 linux_pci_irq_t *irq = &p->intx_irq;
763 ASSERT (irq->fd == -1);
764
765 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
766 {
Damjan Marion23227982018-10-22 13:38:57 +0200767 struct vfio_irq_info ii = { 0 };
768 ii.argsz = sizeof (struct vfio_irq_info);
769 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
770 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100771 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
772 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200773 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
774 __func__, ii.index, ii.count,
775 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
776 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
777 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
778 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
779 ii.flags);
780 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100781 return clib_error_return (0, "INTx interrupt does not exist on device"
782 "'%U'", format_vlib_pci_addr, &p->addr);
783
784 irq->fd = eventfd (0, EFD_NONBLOCK);
785 if (irq->fd == -1)
786 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200787
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100788 t.file_descriptor = irq->fd;
789 t.read_function = linux_pci_vfio_intx_read_ready;
790 }
791 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
792 {
793 t.file_descriptor = p->fd;
794 t.read_function = linux_pci_uio_read_ready;
795 }
796 else
797 return 0;
798
799 t.error_function = linux_pci_uio_error_ready;
800 t.private_data = p->handle;
801 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
802 irq->clib_file_index = clib_file_add (&file_main, &t);
803 irq->intx_handler = intx_handler;
804 return 0;
805}
806
807clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200808vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
809 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100810 pci_msix_handler_function_t * msix_handler)
811{
812 clib_error_t *err = 0;
813 linux_pci_device_t *p = linux_pci_get_device (h);
814 u32 i;
815
816 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
817 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
818 "support");
819
820 /* *INDENT-OFF* */
821 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
822 { .fd = -1});
823 /* *INDENT-ON* */
824
825 for (i = start; i < start + count; i++)
826 {
827 clib_file_t t = { 0 };
828 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
829 ASSERT (irq->fd == -1);
830
831 irq->fd = eventfd (0, EFD_NONBLOCK);
832 if (irq->fd == -1)
833 {
834 err = clib_error_return_unix (0, "eventfd");
835 goto error;
836 }
837
838 t.read_function = linux_pci_vfio_msix_read_ready;
839 t.file_descriptor = irq->fd;
840 t.error_function = linux_pci_vfio_error_ready;
841 t.private_data = p->handle << 16 | i;
842 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
843 &p->addr, i);
844 irq->clib_file_index = clib_file_add (&file_main, &t);
845 irq->msix_handler = msix_handler;
846 }
847
848 return 0;
849
850error:
851 while (i-- > start)
852 {
853 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
854 if (irq->fd != -1)
855 {
856 clib_file_del_by_index (&file_main, irq->clib_file_index);
857 close (irq->fd);
858 irq->fd = -1;
859 }
860 }
861 return err;
862}
863
864clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200865vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
866 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100867{
868 linux_pci_device_t *p = linux_pci_get_device (h);
869 int fds[count];
870 int i;
871
872 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
873 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
874 "support");
875
876 for (i = start; i < start + count; i++)
877 {
878 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
879 fds[i] = irq->fd;
880 }
881
Damjan Marion23227982018-10-22 13:38:57 +0200882 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100883 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
884}
885
886clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200887vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
888 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100889{
890 linux_pci_device_t *p = linux_pci_get_device (h);
891 int i, fds[count];
892
893 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
894 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
895 "support");
896
897 for (i = start; i < start + count; i++)
898 fds[i] = -1;
899
Damjan Marion23227982018-10-22 13:38:57 +0200900 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100901 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
902}
903
Damjan Marioncef87f12017-10-05 15:32:41 +0200904static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200905add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
906 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200907{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100908 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100909 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +0100910 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200911 clib_error_t *err = 0;
912 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +0200913 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +0200914
Damjan Marion2060db82018-03-04 17:37:15 +0100915 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200916
Damjan Marion23227982018-10-22 13:38:57 +0200917 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +0200918 return err;
919
Damjan Marion23227982018-10-22 13:38:57 +0200920 if (is_noiommu == 0)
921 p->supports_va_dma = 1;
922
Damjan Marioncef87f12017-10-05 15:32:41 +0200923 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100924 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200925 {
926 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
927 format_vlib_pci_addr, &di->addr);
928 goto error;
929 }
930
Damjan Marionf313b742018-03-05 14:08:33 +0100931 reg.argsz = sizeof (struct vfio_region_info);
932 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
933 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
934 {
935 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
936 format_vlib_pci_addr, &di->addr);
937 goto error;
938 }
Damjan Marion23227982018-10-22 13:38:57 +0200939
940 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
941 "flags: %s%s%s(0x%x)", __func__,
942 reg.index, reg.size, reg.offset,
943 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
944 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
945 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
946 reg.flags);
947
Damjan Marionf313b742018-03-05 14:08:33 +0100948 p->config_offset = reg.offset;
949 p->config_fd = p->fd;
950
Damjan Marioncef87f12017-10-05 15:32:41 +0200951 /* reset if device supports it */
952 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100953 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200954 {
955 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
956 format_vlib_pci_addr, &di->addr);
957 goto error;
958 }
959
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100960 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200961 {
Damjan Marion23227982018-10-22 13:38:57 +0200962 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
963 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +0200964 }
965
Damjan Marion68b4da62018-09-30 18:26:20 +0200966 if (p->supports_va_dma)
967 {
968 vlib_buffer_pool_t *bp;
969 /* *INDENT-OFF* */
970 vec_foreach (bp, buffer_main.buffer_pools)
971 {
972 u32 i;
973 vlib_physmem_map_t *pm;
974 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
975 for (i = 0; i < pm->n_pages; i++)
976 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
977 }
978 /* *INDENT-ON* */
979 }
980
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100981 if (r && r->init_function)
982 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200983
984error:
985 vec_free (s);
986 if (err)
987 {
Damjan Marionf9a968e2018-02-25 23:26:22 +0100988 if (p->fd != -1)
989 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700990 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +0100991 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700992 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +0200993 }
994 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995}
996
997/* Configuration space read/write. */
998clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200999vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001000 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001001 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002{
Damjan Marioncef87f12017-10-05 15:32:41 +02001003 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 int n;
1005
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001007 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001009 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010
1011 if (n != n_bytes)
1012 return clib_error_return_unix (0, "%s",
1013 read_or_write == VLIB_READ
1014 ? "read" : "write");
1015
1016 return 0;
1017}
1018
1019static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001020vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1021 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022{
Damjan Marioncef87f12017-10-05 15:32:41 +02001023 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001024 clib_error_t *error = 0;
1025 int _fd = -1;
1026 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001027
1028 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
1030 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031
Damjan Marion2060db82018-03-04 17:37:15 +01001032 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 {
Damjan Marion2060db82018-03-04 17:37:15 +01001034 u8 *file_name;
1035 struct stat stat_buf;
1036 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1037 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001039 _fd = open ((char *) file_name, O_RDWR);
1040 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001041 {
1042 error = clib_error_return_unix (0, "open `%s'", file_name);
1043 vec_free (file_name);
1044 return error;
1045 }
1046
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001047 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001048 {
1049 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1050 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001051 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001052 return error;
1053 }
1054
1055 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001056 _size = stat_buf.st_size;
1057 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001058 }
1059 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060 {
Damjan Marion2060db82018-03-04 17:37:15 +01001061 struct vfio_region_info reg = { 0 };
1062 reg.argsz = sizeof (struct vfio_region_info);
1063 reg.index = bar;
1064 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1065 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1066 "'%U'", format_vlib_pci_addr,
1067 &p->addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001068 _fd = p->fd;
1069 _size = reg.size;
1070 _offset = reg.offset;
Damjan Marion23227982018-10-22 13:38:57 +02001071 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
1072 "flags: %s%s%s(0x%x)", __func__,
1073 reg.index, reg.size, reg.offset,
1074 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
1075 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
1076 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
1077 reg.flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078 }
Damjan Marion2060db82018-03-04 17:37:15 +01001079 else
1080 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001082 *fd = _fd;
1083 *size = _size;
1084 *offset = _offset;
1085
1086 return error;
1087}
1088
1089static clib_error_t *
1090vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1091 u32 bar, u8 * addr, void **result)
1092{
1093 linux_pci_device_t *p = linux_pci_get_device (h);
1094 int fd = -1;
1095 clib_error_t *error;
1096 int flags = MAP_SHARED;
1097 u64 size = 0, offset = 0;
1098
1099 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1100
1101 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1102 return error;
1103
1104 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1105 flags |= MAP_FIXED;
1106
Damjan Marion2060db82018-03-04 17:37:15 +01001107 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108 if (*result == (void *) -1)
1109 {
Damjan Marion2060db82018-03-04 17:37:15 +01001110 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1111 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001113 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 }
Damjan Marion2060db82018-03-04 17:37:15 +01001115
1116 /* *INDENT-OFF* */
1117 vec_validate_init_empty (p->regions, bar,
1118 (linux_pci_region_t) { .fd = -1});
1119 /* *INDENT-ON* */
1120 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1121 p->regions[bar].fd = fd;
1122 p->regions[bar].addr = *result;
1123 p->regions[bar].size = size;
1124 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125}
1126
1127clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001128vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1129 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130{
Damjan Marion23227982018-10-22 13:38:57 +02001131 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132}
1133
1134clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001135vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1136 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137{
Damjan Marion23227982018-10-22 13:38:57 +02001138 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139}
1140
Damjan Marionf313b742018-03-05 14:08:33 +01001141clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001142vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1143{
1144 linux_pci_device_t *p = linux_pci_get_device (h);
1145 clib_error_t *error = 0;
1146 int fd = -1;
1147 u64 size = 0, offset = 0;
1148
1149 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1150 return error;
1151
1152 p->io_fd = fd;
1153 p->io_offset = offset;
1154 return error;
1155}
1156
1157clib_error_t *
1158vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1159 vlib_read_or_write_t read_or_write,
1160 uword offset, void *data, u32 length)
1161{
1162 linux_pci_device_t *p = linux_pci_get_device (h);
1163 int n = 0;
1164
1165 if (read_or_write == VLIB_READ)
1166 n = pread (p->io_fd, data, length, p->io_offset + offset);
1167 else
1168 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1169
1170 if (n != length)
1171 return clib_error_return_unix (0, "%s",
1172 read_or_write == VLIB_READ
1173 ? "read" : "write");
1174 return 0;
1175}
1176
1177clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001178vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1179{
1180 linux_pci_device_t *p = linux_pci_get_device (h);
1181
1182 if (!p->supports_va_dma)
1183 return 0;
1184
1185 return vfio_map_physmem_page (vm, ptr);
1186}
1187
1188int
1189vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1190{
1191 linux_pci_device_t *p = linux_pci_get_device (h);
1192
1193 return p->supports_va_dma != 0;
1194}
1195
1196clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001197vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001198 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1199{
1200 linux_pci_main_t *lpm = &linux_pci_main;
1201 vlib_pci_device_info_t *di;
1202 linux_pci_device_t *p;
1203 clib_error_t *err = 0;
1204 pci_device_id_t *i;
1205
Damjan Marion23227982018-10-22 13:38:57 +02001206 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001207
1208 if (err)
1209 return err;
1210 for (i = ids; i->vendor_id != 0; i++)
1211 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1212 break;
1213
1214 if (i->vendor_id == 0)
1215 return clib_error_return (0, "Wrong vendor or device id");
1216
1217 pool_get (lpm->linux_pci_devices, p);
1218 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001219 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001220 p->intx_irq.fd = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001221 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001222 /*
1223 * pci io bar read/write fd
1224 */
1225 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001226
Damjan Marion23227982018-10-22 13:38:57 +02001227 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1228 di->vendor_id, di->device_id, di->driver_name,
1229 di->iommu_group);
1230
1231 if (strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
1232 err = add_device_vfio (vm, p, di, 0);
1233 else if (strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
1234 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001235 else
Damjan Marion23227982018-10-22 13:38:57 +02001236 err = clib_error_create ("device not bound to 'vfio-pci' or "
1237 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001238 if (err)
1239 goto error;
1240
1241 *handle = p->handle;
1242
1243error:
1244 vlib_pci_free_device_info (di);
1245 if (err)
1246 {
Damjan Marion23227982018-10-22 13:38:57 +02001247 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001248 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001249 pool_put (lpm->linux_pci_devices, p);
1250 }
1251
1252 return err;
1253}
1254
1255void
Damjan Marion23227982018-10-22 13:38:57 +02001256vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001257{
1258 linux_pci_main_t *lpm = &linux_pci_main;
1259 linux_pci_device_t *p = linux_pci_get_device (h);
1260 linux_pci_irq_t *irq;
1261 linux_pci_region_t *res;
1262 clib_error_t *err = 0;
1263
1264 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1265 {
1266 irq = &p->intx_irq;
1267 clib_file_del_by_index (&file_main, irq->clib_file_index);
1268 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001269 if (p->io_fd != -1)
1270 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001271 }
1272 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1273 {
1274 irq = &p->intx_irq;
1275 /* close INTx irqs */
1276 if (irq->fd != -1)
1277 {
Damjan Marion23227982018-10-22 13:38:57 +02001278 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001279 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1280 clib_error_free (err);
1281 clib_file_del_by_index (&file_main, irq->clib_file_index);
1282 close (irq->fd);
1283 }
1284
1285 /* close MSI-X irqs */
1286 if (vec_len (p->msix_irqs))
1287 {
Damjan Marion23227982018-10-22 13:38:57 +02001288 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001289 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1290 clib_error_free (err);
1291 /* *INDENT-OFF* */
1292 vec_foreach (irq, p->msix_irqs)
1293 {
1294 if (irq->fd == -1)
1295 continue;
1296 clib_file_del_by_index (&file_main, irq->clib_file_index);
1297 close (irq->fd);
1298 }
1299 /* *INDENT-ON* */
1300 vec_free (p->msix_irqs);
1301 }
1302 }
1303
1304 /* *INDENT-OFF* */
1305 vec_foreach (res, p->regions)
1306 {
1307 if (res->size == 0)
1308 continue;
1309 munmap (res->addr, res->size);
1310 if (res->fd != -1)
1311 close (res->fd);
1312 }
1313 /* *INDENT-ON* */
1314 vec_free (p->regions);
1315
1316 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001317 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001318 pool_put (lpm->linux_pci_devices, p);
1319}
1320
Dave Barach9b8ffd92016-07-08 08:13:45 -04001321void
Damjan Marion23227982018-10-22 13:38:57 +02001322init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001324 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001325 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001326 pci_device_registration_t *r;
1327 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001328 clib_error_t *err = 0;
1329 linux_pci_device_t *p;
1330
1331 pool_get (lpm->linux_pci_devices, p);
1332 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001333 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001334
Damjan Marion5a206ea2016-05-12 22:11:03 +02001335 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336
1337 while (r)
1338 {
1339 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001340 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001341 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001342 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001343 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001344 else
Damjan Marion23227982018-10-22 13:38:57 +02001345 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001346
Damjan Marioncef87f12017-10-05 15:32:41 +02001347 if (err)
1348 clib_error_report (err);
1349 else
1350 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001351 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001352 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001353 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001354
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001356 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001357 pool_put (lpm->linux_pci_devices, p);
1358}
1359
1360static clib_error_t *
1361scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1362{
1363 vlib_pci_addr_t addr, **addrv = arg;
1364 unformat_input_t input;
1365 clib_error_t *err = 0;
1366
1367 unformat_init_string (&input, (char *) dev_dir_name,
1368 vec_len (dev_dir_name));
1369
1370 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1371 unformat_vlib_pci_addr, &addr))
1372 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1373
1374 unformat_free (&input);
1375
1376 if (err)
1377 return err;
1378
1379 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001380 return 0;
1381}
1382
Damjan Marioncef87f12017-10-05 15:32:41 +02001383static int
1384pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385{
Damjan Marioncef87f12017-10-05 15:32:41 +02001386 vlib_pci_addr_t *a1 = v1;
1387 vlib_pci_addr_t *a2 = v2;
1388
1389 if (a1->domain > a2->domain)
1390 return 1;
1391 if (a1->domain < a2->domain)
1392 return -1;
1393 if (a1->bus > a2->bus)
1394 return 1;
1395 if (a1->bus < a2->bus)
1396 return -1;
1397 if (a1->slot > a2->slot)
1398 return 1;
1399 if (a1->slot < a2->slot)
1400 return -1;
1401 if (a1->function > a2->function)
1402 return 1;
1403 if (a1->function < a2->function)
1404 return -1;
1405 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001406}
1407
Damjan Marioncef87f12017-10-05 15:32:41 +02001408vlib_pci_addr_t *
1409vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410{
Damjan Marioncef87f12017-10-05 15:32:41 +02001411 vlib_pci_addr_t *addrs = 0;
1412 clib_error_t *err;
1413 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1414 &addrs, /* scan_dirs */ 0);
1415 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001417 vec_free (addrs);
1418 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001419 }
1420
Damjan Marioncef87f12017-10-05 15:32:41 +02001421 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001422
Damjan Marioncef87f12017-10-05 15:32:41 +02001423 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424}
1425
Dave Barach9b8ffd92016-07-08 08:13:45 -04001426clib_error_t *
1427linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001429 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001430 vlib_pci_addr_t *addr = 0, *addrs;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001431 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
1433 pm->vlib_main = vm;
1434
1435 if ((error = vlib_call_init_function (vm, unix_input_init)))
1436 return error;
1437
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001439
Damjan Marioncef87f12017-10-05 15:32:41 +02001440 addrs = vlib_pci_get_all_dev_addrs ();
1441 /* *INDENT-OFF* */
1442 vec_foreach (addr, addrs)
1443 {
1444 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001445 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001446 {
Damjan Marion23227982018-10-22 13:38:57 +02001447 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001448 vlib_pci_free_device_info (d);
1449 }
1450 }
1451 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452
1453 return error;
1454}
1455
Damjan Marion5a206ea2016-05-12 22:11:03 +02001456VLIB_INIT_FUNCTION (linux_pci_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001457
1458/*
1459 * fd.io coding-style-patch-verification: ON
1460 *
1461 * Local Variables:
1462 * eval: (c-set-style "gnu")
1463 * End:
1464 */