blob: 69fca9e6098396198f2cf175a9e31478ea5c4333 [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 Marion5a206ea2016-05-12 22:11:03 +0200100
101 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100102 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200103
104 /* File descriptor for config space read/write. */
105 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100106 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200107
Damjan Marionf9a968e2018-02-25 23:26:22 +0100108 /* Device File descriptor */
109 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200110
111 /* Minor device for uio device. */
112 u32 uio_minor;
113
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100114 /* Interrupt handlers */
115 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100116 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200117
118 /* private data */
119 uword private_data;
120
Damjan Marion23227982018-10-22 13:38:57 +0200121 u8 supports_va_dma;
122
Damjan Marion5a206ea2016-05-12 22:11:03 +0200123} linux_pci_device_t;
124
125/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400126typedef struct
127{
128 vlib_main_t *vlib_main;
129 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200130
Damjan Marion5a206ea2016-05-12 22:11:03 +0200131} linux_pci_main_t;
132
133extern linux_pci_main_t linux_pci_main;
134
Damjan Marion599a16b2018-03-04 19:35:23 +0100135static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200136linux_pci_get_device (vlib_pci_dev_handle_t h)
137{
138 linux_pci_main_t *lpm = &linux_pci_main;
139 return pool_elt_at_index (lpm->linux_pci_devices, h);
140}
141
142uword
Damjan Marion23227982018-10-22 13:38:57 +0200143vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200144{
145 linux_pci_device_t *d = linux_pci_get_device (h);
146 return d->private_data;
147}
148
149void
Damjan Marion23227982018-10-22 13:38:57 +0200150vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
151 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200152{
153 linux_pci_device_t *d = linux_pci_get_device (h);
154 d->private_data = private_data;
155}
156
157vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200158vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200159{
Damjan Marion599a16b2018-03-04 19:35:23 +0100160 linux_pci_device_t *d = linux_pci_get_device (h);
161 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200162}
163
Damjan Marion5a206ea2016-05-12 22:11:03 +0200164/* Call to allocate/initialize the pci subsystem.
165 This is not an init function so that users can explicitly enable
166 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200168
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169linux_pci_main_t linux_pci_main;
170
Damjan Marioncef87f12017-10-05 15:32:41 +0200171vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200172vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
173 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200174{
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100175 linux_vfio_main_t *lvm = &vfio_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200176 clib_error_t *err;
177 vlib_pci_device_info_t *di;
178 u8 *f = 0;
179 u32 tmp;
180 int fd;
181
182 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
183 memset (di, 0, sizeof (vlib_pci_device_info_t));
184 di->addr.as_u32 = addr->as_u32;
185
186 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
187 format_vlib_pci_addr, addr);
188
189 f = format (0, "%v/config%c", dev_dir_name, 0);
190 fd = open ((char *) f, O_RDWR);
191
192 /* Try read-only access if write fails. */
193 if (fd < 0)
194 fd = open ((char *) f, O_RDONLY);
195
196 if (fd < 0)
197 {
198 err = clib_error_return_unix (0, "open `%s'", f);
199 goto error;
200 }
201
202 /* You can only read more that 64 bytes of config space as root; so we try to
203 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100204 if (read (fd, &di->config_data, sizeof (di->config_data)) <
205 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200206 {
207 err = clib_error_return_unix (0, "read `%s'", f);
208 close (fd);
209 goto error;
210 }
211
212 {
213 static pci_config_header_t all_ones;
214 if (all_ones.vendor_id == 0)
215 memset (&all_ones, ~0, sizeof (all_ones));
216
217 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
218 {
219 err = clib_error_return (0, "invalid PCI config for `%s'", f);
220 close (fd);
221 goto error;
222 }
223 }
224
225 if (di->config0.header.header_type == 0)
226 pci_config_type0_little_to_host (&di->config0);
227 else
228 pci_config_type1_little_to_host (&di->config1);
229
230 di->numa_node = -1;
231 vec_reset_length (f);
232 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
233 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
234 if (err)
235 {
236 di->numa_node = -1;
237 clib_error_free (err);
238 }
239
240 vec_reset_length (f);
241 f = format (f, "%v/class%c", dev_dir_name, 0);
242 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
243 if (err)
244 goto error;
245 di->device_class = tmp >> 8;
246
247 vec_reset_length (f);
248 f = format (f, "%v/vendor%c", dev_dir_name, 0);
249 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
250 if (err)
251 goto error;
252 di->vendor_id = tmp;
253
254 vec_reset_length (f);
255 f = format (f, "%v/device%c", dev_dir_name, 0);
256 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
257 if (err)
258 goto error;
259 di->device_id = tmp;
260
261 vec_reset_length (f);
262 f = format (f, "%v/driver%c", dev_dir_name, 0);
263 di->driver_name = clib_sysfs_link_to_name ((char *) f);
264
265 di->iommu_group = -1;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100266 if (lvm->container_fd != -1)
Damjan Marioncef87f12017-10-05 15:32:41 +0200267 {
268 u8 *tmpstr;
269 vec_reset_length (f);
270 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
271 tmpstr = clib_sysfs_link_to_name ((char *) f);
272 if (tmpstr)
273 {
274 di->iommu_group = atoi ((char *) tmpstr);
275 vec_free (tmpstr);
276 }
Damjan Marion20ba1642018-03-26 15:35:33 +0200277 vec_reset_length (f);
278 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
279 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
280 if (err == 0)
281 {
282 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
283 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
284 vec_free (tmpstr);
285 }
286 else
287 clib_error_free (err);
Damjan Marioncef87f12017-10-05 15:32:41 +0200288 }
289
Damjan Marion96504182017-12-10 23:54:46 +0100290 close (fd);
291
Damjan Marioncef87f12017-10-05 15:32:41 +0200292 vec_reset_length (f);
293 f = format (f, "%v/vpd%c", dev_dir_name, 0);
294 fd = open ((char *) f, O_RDONLY);
295 if (fd >= 0)
296 {
297 while (1)
298 {
299 u8 tag[3];
300 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100301 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200302
303 if (read (fd, &tag, 3) != 3)
304 break;
305
306 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
307 break;
308
309 len = (tag[2] << 8) | tag[1];
310 vec_validate (data, len);
311
312 if (read (fd, data, len) != len)
313 {
314 vec_free (data);
315 break;
316 }
317 if (tag[0] == 0x82)
318 di->product_name = data;
319 else if (tag[0] == 0x90)
320 di->vpd_r = data;
321 else if (tag[0] == 0x91)
322 di->vpd_w = data;
323
324 data = 0;
325 }
326 close (fd);
327 }
328
329 goto done;
330
331error:
332 vlib_pci_free_device_info (di);
333 di = 0;
334
335done:
336 vec_free (f);
337 vec_free (dev_dir_name);
338 if (error)
339 *error = err;
340 else
341 clib_error_free (err);
342 return di;
343}
344
Damjan Marion2752b892017-12-11 15:55:56 +0100345static int
346directory_exists (char *path)
347{
348 struct stat s = { 0 };
349 if (stat (path, &s) == -1)
350 return 0;
351
352 return S_ISDIR (s.st_mode);
353}
354
Damjan Mariona42cd342016-04-13 18:03:20 +0200355clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200356vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
357 char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400359 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200360 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200361 DIR *dir = 0;
362 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200363 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200364 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200365 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
366 format_vlib_pci_addr, addr);
367
Damjan Marion23227982018-10-22 13:38:57 +0200368 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200369
370 if (error)
371 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372
Damjan Marion2752b892017-12-11 15:55:56 +0100373 if (strncmp ("auto", uio_drv_name, 5) == 0)
374 {
375 int vfio_pci_loaded = 0;
376
377 if (directory_exists ("/sys/module/vfio_pci"))
378 vfio_pci_loaded = 1;
379
380 if (di->iommu_group != -1)
381 {
382 /* device is bound to IOMMU group */
383 if (!vfio_pci_loaded)
384 {
385 error = clib_error_return (0, "Skipping PCI device %U: device "
386 "is bound to IOMMU group and "
387 "vfio-pci driver is not loaded",
388 format_vlib_pci_addr, addr);
389 goto done;
390 }
391 else
392 uio_drv_name = "vfio-pci";
393 }
394 else
395 {
396 /* device is not bound to IOMMU group so we have multiple options */
397 if (vfio_pci_loaded &&
398 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
399 uio_drv_name = "vfio-pci";
400 else if (directory_exists ("/sys/module/uio_pci_generic"))
401 uio_drv_name = "uio_pci_generic";
402 else if (directory_exists ("/sys/module/igb_uio"))
403 uio_drv_name = "igb_uio";
404 else
405 {
406 clib_error_free (error);
407 error = clib_error_return (0, "Skipping PCI device %U: missing "
408 "kernel VFIO or UIO driver",
409 format_vlib_pci_addr, addr);
410 goto done;
411 }
412 clib_error_free (error);
413 }
414 }
415
Damjan Mariona7f74572017-05-23 18:32:21 +0200416 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200417 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200418 vec_reset_length (s);
419
Damjan Mariona7f74572017-05-23 18:32:21 +0200420 if (driver_name &&
421 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
422 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
423 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200424 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200425
Damjan Mariona42cd342016-04-13 18:03:20 +0200426 /* walk trough all linux interfaces and if interface belonging to
427 this device is founf check if interface is admin up */
428 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200429 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200430
431 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200433 error = clib_error_return (0, "Skipping PCI device %U: failed to "
434 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200435 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200436 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 }
438
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400440 if (fd < 0)
441 {
442 error = clib_error_return_unix (0, "socket");
443 goto done;
444 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200445
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200448 struct ifreq ifr;
449 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200452 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 memset (&ifr, 0, sizeof ifr);
455 memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200456 ifr.ifr_data = (char *) &drvinfo;
Marco Varlese99d7a722018-06-27 09:54:44 +0200457 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
458 ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0';
Damjan Mariona42cd342016-04-13 18:03:20 +0200459 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400460 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461 {
John Loe2821212016-07-13 18:13:02 -0400462 /* Some interfaces (eg "lo") don't support this ioctl */
463 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700464 clib_unix_warning ("ioctl fetch intf %s bus info error",
465 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400466 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400467 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200468
469 if (strcmp ((char *) s, drvinfo.bus_info))
470 continue;
471
Dave Barach9b8ffd92016-07-08 08:13:45 -0400472 memset (&ifr, 0, sizeof (ifr));
Marco Varlese99d7a722018-06-27 09:54:44 +0200473 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
474 ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0';
Chris Luke370e9e32016-07-07 11:01:17 -0400475 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 {
477 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
478 e->d_name);
479 close (fd);
480 goto done;
481 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200482
483 if (ifr.ifr_flags & IFF_UP)
484 {
485 error = clib_error_return (0, "Skipping PCI device %U as host "
486 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200487 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400488 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200489 goto done;
490 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 }
492
Damjan Mariona42cd342016-04-13 18:03:20 +0200493 close (fd);
494 vec_reset_length (s);
495
496 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200497 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200498 vec_reset_length (s);
499
Damjan Mariona7f74572017-05-23 18:32:21 +0200500 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
501 if (access ((char *) s, F_OK) == 0)
502 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200503 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200504 clear_driver_override = 1;
505 }
506 else
507 {
508 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200509 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
510 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
511 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200512 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200513 vec_reset_length (s);
514
Damjan Marioncef87f12017-10-05 15:32:41 +0200515 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
516 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200517 vec_reset_length (s);
518
519 if (clear_driver_override)
520 {
521 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200522 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200523 vec_reset_length (s);
524 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200525
526done:
527 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200529 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200530 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 return error;
532}
533
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
535static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400538 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 unformat_input_t input;
540
541 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
542
Dave Barach9b8ffd92016-07-08 08:13:45 -0400543 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 abort ();
545
546 unformat_free (&input);
547 return 0;
548}
549
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200551vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
552 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100553{
554 int data_len = efds ? count * sizeof (int) : 0;
555 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200556 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100557 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
558
559
Damjan Marion23227982018-10-22 13:38:57 +0200560 ii.argsz = sizeof (struct vfio_irq_info);
561 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100562
Damjan Marion23227982018-10-22 13:38:57 +0200563 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100564 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
565 "'%U'", format_vlib_pci_addr, &p->addr);
566
Damjan Marion23227982018-10-22 13:38:57 +0200567 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
568 __func__, ii.index, ii.count,
569 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
570 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
571 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
572 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
573 ii.flags);
574
575 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100576 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
577 "'%U'", format_vlib_pci_addr, &p->addr);
578
579
580 if (efds)
581 {
582 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
583 clib_memcpy (&irq_set->data, efds, data_len);
584 }
585 else
586 flags |= VFIO_IRQ_SET_DATA_NONE;
587
588 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
589 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
590
591 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
592 irq_set->index = index;
593 irq_set->start = start;
594 irq_set->count = count;
595 irq_set->flags = flags;
596
597 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
598 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
599 "[index = %u, start = %u, count = %u, "
600 "flags = 0x%x]",
601 format_vlib_pci_addr, &p->addr,
602 index, start, count, flags);
603 return 0;
604}
605
606static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200607linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608{
Damjan Marion23227982018-10-22 13:38:57 +0200609 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200610 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200611 vlib_pci_dev_handle_t h = uf->private_data;
612 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100613 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Damjan Marion5a206ea2016-05-12 22:11:03 +0200615 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400616 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100618 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200619 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200620
Damjan Marion23227982018-10-22 13:38:57 +0200621 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622
623 return /* no error */ 0;
624}
625
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200627linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200628{
Damjan Marion23227982018-10-22 13:38:57 +0200629 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100630 VFIO_IRQ_SET_ACTION_UNMASK, 0);
631}
632
Damjan Marioncef87f12017-10-05 15:32:41 +0200633static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200634linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635{
636 u32 error_index = (u32) uf->private_data;
637
638 return clib_error_return (0, "pci device %d: error", error_index);
639}
640
Damjan Marioncef87f12017-10-05 15:32:41 +0200641static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100642linux_pci_vfio_msix_read_ready (clib_file_t * uf)
643{
Damjan Marion23227982018-10-22 13:38:57 +0200644 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100645 int __attribute__ ((unused)) rv;
646 vlib_pci_dev_handle_t h = uf->private_data >> 16;
647 u16 line = uf->private_data & 0xffff;
648 linux_pci_device_t *p = linux_pci_get_device (h);
649 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
650
651 u64 icount;
652 rv = read (uf->file_descriptor, &icount, sizeof (icount));
653
654 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200655 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100656
657 return /* no error */ 0;
658}
659
660static clib_error_t *
661linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662{
Damjan Marion23227982018-10-22 13:38:57 +0200663 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200664 int __attribute__ ((unused)) rv;
665 vlib_pci_dev_handle_t h = uf->private_data;
666 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100667 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Damjan Marioncef87f12017-10-05 15:32:41 +0200669 u64 icount;
670 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100672 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200673 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
Damjan Marion23227982018-10-22 13:38:57 +0200675 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Damjan Marioncef87f12017-10-05 15:32:41 +0200677 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678}
679
Damjan Marioncef87f12017-10-05 15:32:41 +0200680static clib_error_t *
681linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682{
Damjan Marioncef87f12017-10-05 15:32:41 +0200683 u32 error_index = (u32) uf->private_data;
684
685 return clib_error_return (0, "pci device %d: error", error_index);
686}
687
688static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200689add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
690 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200691{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100692 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200693 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200694 u8 *s = 0;
695
Damjan Marionf9a968e2018-02-25 23:26:22 +0100696 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100697 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200698
699 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
700 format_vlib_pci_addr, &di->addr, 0);
701
702 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100703 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200704 vec_reset_length (s);
705
706 if (p->config_fd == -1)
707 {
708 err = clib_error_return_unix (0, "open '%s'", s);
709 goto error;
710 }
711
712 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
713 format_vlib_pci_addr, &di->addr);
714 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
715 1);
716 vec_reset_length (s);
717
718 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100719 p->fd = open ((char *) s, O_RDWR);
720 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200721 {
722 err = clib_error_return_unix (0, "open '%s'", s);
723 goto error;
724 }
725
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100726 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200727 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200728
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100729 if (r && r->init_function)
730 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200731
732error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200733 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200734 if (err)
735 {
Damjan Marion96504182017-12-10 23:54:46 +0100736 if (p->config_fd != -1)
737 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100738 if (p->fd != -1)
739 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200740 }
741 return err;
742}
743
Damjan Marion599a16b2018-03-04 19:35:23 +0100744clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200745vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100746 pci_intx_handler_function_t * intx_handler)
747{
748 linux_pci_device_t *p = linux_pci_get_device (h);
749 clib_file_t t = { 0 };
750 linux_pci_irq_t *irq = &p->intx_irq;
751 ASSERT (irq->fd == -1);
752
753 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
754 {
Damjan Marion23227982018-10-22 13:38:57 +0200755 struct vfio_irq_info ii = { 0 };
756 ii.argsz = sizeof (struct vfio_irq_info);
757 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
758 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100759 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
760 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200761 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
762 __func__, ii.index, ii.count,
763 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
764 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
765 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
766 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
767 ii.flags);
768 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100769 return clib_error_return (0, "INTx interrupt does not exist on device"
770 "'%U'", format_vlib_pci_addr, &p->addr);
771
772 irq->fd = eventfd (0, EFD_NONBLOCK);
773 if (irq->fd == -1)
774 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200775
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100776 t.file_descriptor = irq->fd;
777 t.read_function = linux_pci_vfio_intx_read_ready;
778 }
779 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
780 {
781 t.file_descriptor = p->fd;
782 t.read_function = linux_pci_uio_read_ready;
783 }
784 else
785 return 0;
786
787 t.error_function = linux_pci_uio_error_ready;
788 t.private_data = p->handle;
789 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
790 irq->clib_file_index = clib_file_add (&file_main, &t);
791 irq->intx_handler = intx_handler;
792 return 0;
793}
794
795clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200796vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
797 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100798 pci_msix_handler_function_t * msix_handler)
799{
800 clib_error_t *err = 0;
801 linux_pci_device_t *p = linux_pci_get_device (h);
802 u32 i;
803
804 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
805 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
806 "support");
807
808 /* *INDENT-OFF* */
809 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
810 { .fd = -1});
811 /* *INDENT-ON* */
812
813 for (i = start; i < start + count; i++)
814 {
815 clib_file_t t = { 0 };
816 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
817 ASSERT (irq->fd == -1);
818
819 irq->fd = eventfd (0, EFD_NONBLOCK);
820 if (irq->fd == -1)
821 {
822 err = clib_error_return_unix (0, "eventfd");
823 goto error;
824 }
825
826 t.read_function = linux_pci_vfio_msix_read_ready;
827 t.file_descriptor = irq->fd;
828 t.error_function = linux_pci_vfio_error_ready;
829 t.private_data = p->handle << 16 | i;
830 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
831 &p->addr, i);
832 irq->clib_file_index = clib_file_add (&file_main, &t);
833 irq->msix_handler = msix_handler;
834 }
835
836 return 0;
837
838error:
839 while (i-- > start)
840 {
841 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
842 if (irq->fd != -1)
843 {
844 clib_file_del_by_index (&file_main, irq->clib_file_index);
845 close (irq->fd);
846 irq->fd = -1;
847 }
848 }
849 return err;
850}
851
852clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200853vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
854 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100855{
856 linux_pci_device_t *p = linux_pci_get_device (h);
857 int fds[count];
858 int i;
859
860 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
861 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
862 "support");
863
864 for (i = start; i < start + count; i++)
865 {
866 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
867 fds[i] = irq->fd;
868 }
869
Damjan Marion23227982018-10-22 13:38:57 +0200870 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100871 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
872}
873
874clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200875vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
876 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100877{
878 linux_pci_device_t *p = linux_pci_get_device (h);
879 int i, fds[count];
880
881 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
882 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
883 "support");
884
885 for (i = start; i < start + count; i++)
886 fds[i] = -1;
887
Damjan Marion23227982018-10-22 13:38:57 +0200888 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100889 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
890}
891
Damjan Marioncef87f12017-10-05 15:32:41 +0200892static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200893add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
894 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200895{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100896 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100897 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +0100898 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200899 clib_error_t *err = 0;
900 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +0200901 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +0200902
Damjan Marion2060db82018-03-04 17:37:15 +0100903 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200904
Damjan Marion23227982018-10-22 13:38:57 +0200905 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +0200906 return err;
907
Damjan Marion23227982018-10-22 13:38:57 +0200908 if (is_noiommu == 0)
909 p->supports_va_dma = 1;
910
Damjan Marioncef87f12017-10-05 15:32:41 +0200911 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100912 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200913 {
914 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
915 format_vlib_pci_addr, &di->addr);
916 goto error;
917 }
918
Damjan Marionf313b742018-03-05 14:08:33 +0100919 reg.argsz = sizeof (struct vfio_region_info);
920 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
921 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
922 {
923 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
924 format_vlib_pci_addr, &di->addr);
925 goto error;
926 }
Damjan Marion23227982018-10-22 13:38:57 +0200927
928 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
929 "flags: %s%s%s(0x%x)", __func__,
930 reg.index, reg.size, reg.offset,
931 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
932 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
933 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
934 reg.flags);
935
Damjan Marionf313b742018-03-05 14:08:33 +0100936 p->config_offset = reg.offset;
937 p->config_fd = p->fd;
938
Damjan Marioncef87f12017-10-05 15:32:41 +0200939 /* reset if device supports it */
940 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100941 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200942 {
943 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
944 format_vlib_pci_addr, &di->addr);
945 goto error;
946 }
947
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100948 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200949 {
Damjan Marion23227982018-10-22 13:38:57 +0200950 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
951 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +0200952 }
953
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100954 if (r && r->init_function)
955 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200956
957error:
958 vec_free (s);
959 if (err)
960 {
Damjan Marionf9a968e2018-02-25 23:26:22 +0100961 if (p->fd != -1)
962 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700963 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +0100964 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700965 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +0200966 }
967 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968}
969
970/* Configuration space read/write. */
971clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200972vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +0200973 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400974 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975{
Damjan Marioncef87f12017-10-05 15:32:41 +0200976 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977 int n;
978
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100980 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981 else
Damjan Marionf9a968e2018-02-25 23:26:22 +0100982 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
984 if (n != n_bytes)
985 return clib_error_return_unix (0, "%s",
986 read_or_write == VLIB_READ
987 ? "read" : "write");
988
989 return 0;
990}
991
992static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200993vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion2060db82018-03-04 17:37:15 +0100994 u32 bar, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995{
Damjan Marioncef87f12017-10-05 15:32:41 +0200996 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Marion2060db82018-03-04 17:37:15 +0100997 int fd = -1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400998 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999 int flags = MAP_SHARED;
Damjan Marion2060db82018-03-04 17:37:15 +01001000 u64 size = 0, offset = 0;
1001
1002 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003
1004 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005
Damjan Marion23227982018-10-22 13:38:57 +02001006 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1007
Damjan Marion2060db82018-03-04 17:37:15 +01001008 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009 {
Damjan Marion2060db82018-03-04 17:37:15 +01001010 u8 *file_name;
1011 struct stat stat_buf;
1012 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1013 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014
Damjan Marion2060db82018-03-04 17:37:15 +01001015 fd = open ((char *) file_name, O_RDWR);
1016 if (fd < 0)
1017 {
1018 error = clib_error_return_unix (0, "open `%s'", file_name);
1019 vec_free (file_name);
1020 return error;
1021 }
1022
1023 if (fstat (fd, &stat_buf) < 0)
1024 {
1025 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1026 vec_free (file_name);
1027 close (fd);
1028 return error;
1029 }
1030
1031 vec_free (file_name);
1032 if (addr != 0)
1033 flags |= MAP_FIXED;
1034 size = stat_buf.st_size;
1035 offset = 0;
1036 }
1037 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038 {
Damjan Marion2060db82018-03-04 17:37:15 +01001039 struct vfio_region_info reg = { 0 };
1040 reg.argsz = sizeof (struct vfio_region_info);
1041 reg.index = bar;
1042 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1043 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1044 "'%U'", format_vlib_pci_addr,
1045 &p->addr);
1046 fd = p->fd;
1047 size = reg.size;
1048 offset = reg.offset;
Damjan Marion23227982018-10-22 13:38:57 +02001049 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
1050 "flags: %s%s%s(0x%x)", __func__,
1051 reg.index, reg.size, reg.offset,
1052 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
1053 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
1054 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
1055 reg.flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056 }
Damjan Marion2060db82018-03-04 17:37:15 +01001057 else
1058 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059
Damjan Marion2060db82018-03-04 17:37:15 +01001060 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061 if (*result == (void *) -1)
1062 {
Damjan Marion2060db82018-03-04 17:37:15 +01001063 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1064 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001066 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067 }
Damjan Marion2060db82018-03-04 17:37:15 +01001068
1069 /* *INDENT-OFF* */
1070 vec_validate_init_empty (p->regions, bar,
1071 (linux_pci_region_t) { .fd = -1});
1072 /* *INDENT-ON* */
1073 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1074 p->regions[bar].fd = fd;
1075 p->regions[bar].addr = *result;
1076 p->regions[bar].size = size;
1077 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078}
1079
1080clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001081vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1082 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083{
Damjan Marion23227982018-10-22 13:38:57 +02001084 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085}
1086
1087clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001088vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1089 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090{
Damjan Marion23227982018-10-22 13:38:57 +02001091 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092}
1093
Damjan Marionf313b742018-03-05 14:08:33 +01001094clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001095vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001096 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1097{
1098 linux_pci_main_t *lpm = &linux_pci_main;
1099 vlib_pci_device_info_t *di;
1100 linux_pci_device_t *p;
1101 clib_error_t *err = 0;
1102 pci_device_id_t *i;
1103
Damjan Marion23227982018-10-22 13:38:57 +02001104 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001105
1106 if (err)
1107 return err;
1108 for (i = ids; i->vendor_id != 0; i++)
1109 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1110 break;
1111
1112 if (i->vendor_id == 0)
1113 return clib_error_return (0, "Wrong vendor or device id");
1114
1115 pool_get (lpm->linux_pci_devices, p);
1116 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001117 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001118 p->intx_irq.fd = -1;
1119
Damjan Marion23227982018-10-22 13:38:57 +02001120 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1121 di->vendor_id, di->device_id, di->driver_name,
1122 di->iommu_group);
1123
1124 if (strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
1125 err = add_device_vfio (vm, p, di, 0);
1126 else if (strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
1127 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001128 else
Damjan Marion23227982018-10-22 13:38:57 +02001129 err = clib_error_create ("device not bound to 'vfio-pci' or "
1130 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001131 if (err)
1132 goto error;
1133
1134 *handle = p->handle;
1135
1136error:
1137 vlib_pci_free_device_info (di);
1138 if (err)
1139 {
Damjan Marion23227982018-10-22 13:38:57 +02001140 pci_log_err (vm, p, "%U", format_clib_error, err);
Damjan Marionf313b742018-03-05 14:08:33 +01001141 memset (p, 0, sizeof (linux_pci_device_t));
1142 pool_put (lpm->linux_pci_devices, p);
1143 }
1144
1145 return err;
1146}
1147
1148void
Damjan Marion23227982018-10-22 13:38:57 +02001149vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001150{
1151 linux_pci_main_t *lpm = &linux_pci_main;
1152 linux_pci_device_t *p = linux_pci_get_device (h);
1153 linux_pci_irq_t *irq;
1154 linux_pci_region_t *res;
1155 clib_error_t *err = 0;
1156
1157 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1158 {
1159 irq = &p->intx_irq;
1160 clib_file_del_by_index (&file_main, irq->clib_file_index);
1161 close (p->config_fd);
1162 }
1163 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1164 {
1165 irq = &p->intx_irq;
1166 /* close INTx irqs */
1167 if (irq->fd != -1)
1168 {
Damjan Marion23227982018-10-22 13:38:57 +02001169 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001170 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1171 clib_error_free (err);
1172 clib_file_del_by_index (&file_main, irq->clib_file_index);
1173 close (irq->fd);
1174 }
1175
1176 /* close MSI-X irqs */
1177 if (vec_len (p->msix_irqs))
1178 {
Damjan Marion23227982018-10-22 13:38:57 +02001179 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001180 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1181 clib_error_free (err);
1182 /* *INDENT-OFF* */
1183 vec_foreach (irq, p->msix_irqs)
1184 {
1185 if (irq->fd == -1)
1186 continue;
1187 clib_file_del_by_index (&file_main, irq->clib_file_index);
1188 close (irq->fd);
1189 }
1190 /* *INDENT-ON* */
1191 vec_free (p->msix_irqs);
1192 }
1193 }
1194
1195 /* *INDENT-OFF* */
1196 vec_foreach (res, p->regions)
1197 {
1198 if (res->size == 0)
1199 continue;
1200 munmap (res->addr, res->size);
1201 if (res->fd != -1)
1202 close (res->fd);
1203 }
1204 /* *INDENT-ON* */
1205 vec_free (p->regions);
1206
1207 close (p->fd);
1208 memset (p, 0, sizeof (linux_pci_device_t));
1209 pool_put (lpm->linux_pci_devices, p);
1210}
1211
Dave Barach9b8ffd92016-07-08 08:13:45 -04001212void
Damjan Marion23227982018-10-22 13:38:57 +02001213init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001215 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001216 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001217 pci_device_registration_t *r;
1218 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001219 clib_error_t *err = 0;
1220 linux_pci_device_t *p;
1221
1222 pool_get (lpm->linux_pci_devices, p);
1223 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001224 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
Damjan Marion5a206ea2016-05-12 22:11:03 +02001226 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227
1228 while (r)
1229 {
1230 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001231 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001232 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001233 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001234 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001235 else
Damjan Marion23227982018-10-22 13:38:57 +02001236 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001237
Damjan Marioncef87f12017-10-05 15:32:41 +02001238 if (err)
1239 clib_error_report (err);
1240 else
1241 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001242 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001244 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001245
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246 /* No driver, close the PCI config-space FD */
Damjan Marioncef87f12017-10-05 15:32:41 +02001247 memset (p, 0, sizeof (linux_pci_device_t));
1248 pool_put (lpm->linux_pci_devices, p);
1249}
1250
1251static clib_error_t *
1252scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1253{
1254 vlib_pci_addr_t addr, **addrv = arg;
1255 unformat_input_t input;
1256 clib_error_t *err = 0;
1257
1258 unformat_init_string (&input, (char *) dev_dir_name,
1259 vec_len (dev_dir_name));
1260
1261 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1262 unformat_vlib_pci_addr, &addr))
1263 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1264
1265 unformat_free (&input);
1266
1267 if (err)
1268 return err;
1269
1270 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001271 return 0;
1272}
1273
Damjan Marioncef87f12017-10-05 15:32:41 +02001274static int
1275pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276{
Damjan Marioncef87f12017-10-05 15:32:41 +02001277 vlib_pci_addr_t *a1 = v1;
1278 vlib_pci_addr_t *a2 = v2;
1279
1280 if (a1->domain > a2->domain)
1281 return 1;
1282 if (a1->domain < a2->domain)
1283 return -1;
1284 if (a1->bus > a2->bus)
1285 return 1;
1286 if (a1->bus < a2->bus)
1287 return -1;
1288 if (a1->slot > a2->slot)
1289 return 1;
1290 if (a1->slot < a2->slot)
1291 return -1;
1292 if (a1->function > a2->function)
1293 return 1;
1294 if (a1->function < a2->function)
1295 return -1;
1296 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297}
1298
Damjan Marioncef87f12017-10-05 15:32:41 +02001299vlib_pci_addr_t *
1300vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301{
Damjan Marioncef87f12017-10-05 15:32:41 +02001302 vlib_pci_addr_t *addrs = 0;
1303 clib_error_t *err;
1304 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1305 &addrs, /* scan_dirs */ 0);
1306 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001308 vec_free (addrs);
1309 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310 }
1311
Damjan Marioncef87f12017-10-05 15:32:41 +02001312 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001313
Damjan Marioncef87f12017-10-05 15:32:41 +02001314 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315}
1316
Dave Barach9b8ffd92016-07-08 08:13:45 -04001317clib_error_t *
1318linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001320 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001321 vlib_pci_addr_t *addr = 0, *addrs;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001322 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323
1324 pm->vlib_main = vm;
1325
1326 if ((error = vlib_call_init_function (vm, unix_input_init)))
1327 return error;
1328
Dave Barach9b8ffd92016-07-08 08:13:45 -04001329 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001330
Damjan Marioncef87f12017-10-05 15:32:41 +02001331 addrs = vlib_pci_get_all_dev_addrs ();
1332 /* *INDENT-OFF* */
1333 vec_foreach (addr, addrs)
1334 {
1335 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001336 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001337 {
Damjan Marion23227982018-10-22 13:38:57 +02001338 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001339 vlib_pci_free_device_info (d);
1340 }
1341 }
1342 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343
1344 return error;
1345}
1346
Damjan Marion5a206ea2016-05-12 22:11:03 +02001347VLIB_INIT_FUNCTION (linux_pci_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001348
1349/*
1350 * fd.io coding-style-patch-verification: ON
1351 *
1352 * Local Variables:
1353 * eval: (c-set-style "gnu")
1354 * End:
1355 */