blob: d8cbf7a41632d5f2693e490a91eab63c1a0ad8e7 [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
Mohsin Kazmia402b832019-01-25 15:57:24 +0000176u32
177vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
178{
179 linux_pci_device_t *d = linux_pci_get_device (h);
180
181 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
182 {
183 struct vfio_irq_info ii = { 0 };
184
185 ii.argsz = sizeof (struct vfio_irq_info);
186 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
187 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
188 return 0;
189 return ii.count;
190 }
191 return 0;
192}
193
Damjan Marion5a206ea2016-05-12 22:11:03 +0200194/* Call to allocate/initialize the pci subsystem.
195 This is not an init function so that users can explicitly enable
196 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200198
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199linux_pci_main_t linux_pci_main;
200
Damjan Marioncef87f12017-10-05 15:32:41 +0200201vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200202vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
203 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200204{
Damjan Marioncef87f12017-10-05 15:32:41 +0200205 clib_error_t *err;
206 vlib_pci_device_info_t *di;
207 u8 *f = 0;
208 u32 tmp;
209 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800210 u8 *tmpstr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200211
212 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400213 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200214 di->addr.as_u32 = addr->as_u32;
215
216 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
217 format_vlib_pci_addr, addr);
218
219 f = format (0, "%v/config%c", dev_dir_name, 0);
220 fd = open ((char *) f, O_RDWR);
221
222 /* Try read-only access if write fails. */
223 if (fd < 0)
224 fd = open ((char *) f, O_RDONLY);
225
226 if (fd < 0)
227 {
228 err = clib_error_return_unix (0, "open `%s'", f);
229 goto error;
230 }
231
232 /* You can only read more that 64 bytes of config space as root; so we try to
233 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100234 if (read (fd, &di->config_data, sizeof (di->config_data)) <
235 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200236 {
237 err = clib_error_return_unix (0, "read `%s'", f);
238 close (fd);
239 goto error;
240 }
241
242 {
243 static pci_config_header_t all_ones;
244 if (all_ones.vendor_id == 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400245 clib_memset (&all_ones, ~0, sizeof (all_ones));
Damjan Marioncef87f12017-10-05 15:32:41 +0200246
247 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
248 {
249 err = clib_error_return (0, "invalid PCI config for `%s'", f);
250 close (fd);
251 goto error;
252 }
253 }
254
255 if (di->config0.header.header_type == 0)
256 pci_config_type0_little_to_host (&di->config0);
257 else
258 pci_config_type1_little_to_host (&di->config1);
259
260 di->numa_node = -1;
261 vec_reset_length (f);
262 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
263 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
264 if (err)
265 {
266 di->numa_node = -1;
267 clib_error_free (err);
268 }
269
270 vec_reset_length (f);
271 f = format (f, "%v/class%c", dev_dir_name, 0);
272 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
273 if (err)
274 goto error;
275 di->device_class = tmp >> 8;
276
277 vec_reset_length (f);
278 f = format (f, "%v/vendor%c", dev_dir_name, 0);
279 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
280 if (err)
281 goto error;
282 di->vendor_id = tmp;
283
284 vec_reset_length (f);
285 f = format (f, "%v/device%c", dev_dir_name, 0);
286 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
287 if (err)
288 goto error;
289 di->device_id = tmp;
290
291 vec_reset_length (f);
292 f = format (f, "%v/driver%c", dev_dir_name, 0);
293 di->driver_name = clib_sysfs_link_to_name ((char *) f);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200294 if (!di->driver_name)
295 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200296
297 di->iommu_group = -1;
Yulong Pei45495482019-10-17 18:41:52 +0800298 vec_reset_length (f);
299 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
300 tmpstr = clib_sysfs_link_to_name ((char *) f);
301 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200302 {
Yulong Pei45495482019-10-17 18:41:52 +0800303 di->iommu_group = atoi ((char *) tmpstr);
304 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200305 }
306
Yulong Pei45495482019-10-17 18:41:52 +0800307 vec_reset_length (f);
308 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
309 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
310 if (err == 0)
311 {
312 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
313 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
314 vec_free (tmpstr);
315 }
316 else
317 clib_error_free (err);
318
Damjan Marion96504182017-12-10 23:54:46 +0100319 close (fd);
320
Damjan Marioncef87f12017-10-05 15:32:41 +0200321 vec_reset_length (f);
322 f = format (f, "%v/vpd%c", dev_dir_name, 0);
323 fd = open ((char *) f, O_RDONLY);
324 if (fd >= 0)
325 {
326 while (1)
327 {
328 u8 tag[3];
329 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100330 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200331
332 if (read (fd, &tag, 3) != 3)
333 break;
334
335 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
336 break;
337
338 len = (tag[2] << 8) | tag[1];
339 vec_validate (data, len);
340
341 if (read (fd, data, len) != len)
342 {
343 vec_free (data);
344 break;
345 }
346 if (tag[0] == 0x82)
347 di->product_name = data;
348 else if (tag[0] == 0x90)
349 di->vpd_r = data;
350 else if (tag[0] == 0x91)
351 di->vpd_w = data;
352
353 data = 0;
354 }
355 close (fd);
356 }
357
358 goto done;
359
360error:
361 vlib_pci_free_device_info (di);
362 di = 0;
363
364done:
365 vec_free (f);
366 vec_free (dev_dir_name);
367 if (error)
368 *error = err;
369 else
370 clib_error_free (err);
371 return di;
372}
373
Damjan Marion2752b892017-12-11 15:55:56 +0100374static int
375directory_exists (char *path)
376{
377 struct stat s = { 0 };
378 if (stat (path, &s) == -1)
379 return 0;
380
381 return S_ISDIR (s.st_mode);
382}
383
Damjan Mariona42cd342016-04-13 18:03:20 +0200384clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200385vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
386 char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200389 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200390 DIR *dir = 0;
391 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200392 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200393 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200394 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
395 format_vlib_pci_addr, addr);
396
Damjan Marion23227982018-10-22 13:38:57 +0200397 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200398
399 if (error)
400 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
Damjan Marion2752b892017-12-11 15:55:56 +0100402 if (strncmp ("auto", uio_drv_name, 5) == 0)
403 {
404 int vfio_pci_loaded = 0;
405
406 if (directory_exists ("/sys/module/vfio_pci"))
407 vfio_pci_loaded = 1;
408
409 if (di->iommu_group != -1)
410 {
411 /* device is bound to IOMMU group */
412 if (!vfio_pci_loaded)
413 {
414 error = clib_error_return (0, "Skipping PCI device %U: device "
415 "is bound to IOMMU group and "
416 "vfio-pci driver is not loaded",
417 format_vlib_pci_addr, addr);
418 goto done;
419 }
420 else
421 uio_drv_name = "vfio-pci";
422 }
423 else
424 {
425 /* device is not bound to IOMMU group so we have multiple options */
426 if (vfio_pci_loaded &&
427 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
428 uio_drv_name = "vfio-pci";
429 else if (directory_exists ("/sys/module/uio_pci_generic"))
430 uio_drv_name = "uio_pci_generic";
431 else if (directory_exists ("/sys/module/igb_uio"))
432 uio_drv_name = "igb_uio";
433 else
434 {
435 clib_error_free (error);
436 error = clib_error_return (0, "Skipping PCI device %U: missing "
437 "kernel VFIO or UIO driver",
438 format_vlib_pci_addr, addr);
439 goto done;
440 }
441 clib_error_free (error);
442 }
443 }
444
Damjan Mariona7f74572017-05-23 18:32:21 +0200445 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200446 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200447 vec_reset_length (s);
448
Damjan Mariona7f74572017-05-23 18:32:21 +0200449 if (driver_name &&
450 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
451 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
452 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200453 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200454
Damjan Mariona42cd342016-04-13 18:03:20 +0200455 /* walk trough all linux interfaces and if interface belonging to
456 this device is founf check if interface is admin up */
457 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200458 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200459
460 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200462 error = clib_error_return (0, "Skipping PCI device %U: failed to "
463 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200464 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200465 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 }
467
Dave Barach9b8ffd92016-07-08 08:13:45 -0400468 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400469 if (fd < 0)
470 {
471 error = clib_error_return_unix (0, "socket");
472 goto done;
473 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200474
Dave Barach9b8ffd92016-07-08 08:13:45 -0400475 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200477 struct ifreq ifr;
478 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200481 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482
Dave Barachb7b92992018-10-17 10:38:51 -0400483 clib_memset (&ifr, 0, sizeof ifr);
484 clib_memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200485 ifr.ifr_data = (char *) &drvinfo;
Steven Luongddccf7b2019-01-18 22:53:18 -0800486 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
Stephen Hemminger4e8a8312019-01-16 10:23:16 -0800487
Damjan Mariona42cd342016-04-13 18:03:20 +0200488 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400489 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400490 {
John Loe2821212016-07-13 18:13:02 -0400491 /* Some interfaces (eg "lo") don't support this ioctl */
492 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700493 clib_unix_warning ("ioctl fetch intf %s bus info error",
494 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400495 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400496 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200497
498 if (strcmp ((char *) s, drvinfo.bus_info))
499 continue;
500
Dave Barachb7b92992018-10-17 10:38:51 -0400501 clib_memset (&ifr, 0, sizeof (ifr));
Steven Luongddccf7b2019-01-18 22:53:18 -0800502 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
Stephen Hemminger4e8a8312019-01-16 10:23:16 -0800503
Chris Luke370e9e32016-07-07 11:01:17 -0400504 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400505 {
506 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
507 e->d_name);
508 close (fd);
509 goto done;
510 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200511
512 if (ifr.ifr_flags & IFF_UP)
513 {
514 error = clib_error_return (0, "Skipping PCI device %U as host "
515 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200516 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400517 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200518 goto done;
519 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 }
521
Damjan Mariona42cd342016-04-13 18:03:20 +0200522 close (fd);
523 vec_reset_length (s);
524
525 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200526 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200527 vec_reset_length (s);
528
Damjan Mariona7f74572017-05-23 18:32:21 +0200529 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
530 if (access ((char *) s, F_OK) == 0)
531 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200532 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200533 clear_driver_override = 1;
534 }
535 else
536 {
537 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200538 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
539 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
540 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200541 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200542 vec_reset_length (s);
543
Damjan Marioncef87f12017-10-05 15:32:41 +0200544 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
545 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200546 vec_reset_length (s);
547
548 if (clear_driver_override)
549 {
550 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200551 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200552 vec_reset_length (s);
553 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200554
555done:
556 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200558 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200559 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 return error;
561}
562
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563
564static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400565scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400567 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 unformat_input_t input;
569
570 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
571
Dave Barach9b8ffd92016-07-08 08:13:45 -0400572 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 abort ();
574
575 unformat_free (&input);
576 return 0;
577}
578
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200580vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
581 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100582{
583 int data_len = efds ? count * sizeof (int) : 0;
584 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200585 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100586 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
587
588
Damjan Marion23227982018-10-22 13:38:57 +0200589 ii.argsz = sizeof (struct vfio_irq_info);
590 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100591
Damjan Marion23227982018-10-22 13:38:57 +0200592 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100593 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
594 "'%U'", format_vlib_pci_addr, &p->addr);
595
Damjan Marion23227982018-10-22 13:38:57 +0200596 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
597 __func__, ii.index, ii.count,
598 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
599 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
600 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
601 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
602 ii.flags);
603
604 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100605 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
606 "'%U'", format_vlib_pci_addr, &p->addr);
607
608
609 if (efds)
610 {
611 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500612 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100613 }
614 else
615 flags |= VFIO_IRQ_SET_DATA_NONE;
616
617 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
618 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
619
620 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
621 irq_set->index = index;
622 irq_set->start = start;
623 irq_set->count = count;
624 irq_set->flags = flags;
625
626 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
627 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
628 "[index = %u, start = %u, count = %u, "
629 "flags = 0x%x]",
630 format_vlib_pci_addr, &p->addr,
631 index, start, count, flags);
632 return 0;
633}
634
635static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200636linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637{
Damjan Marion23227982018-10-22 13:38:57 +0200638 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200639 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200640 vlib_pci_dev_handle_t h = uf->private_data;
641 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100642 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Damjan Marion5a206ea2016-05-12 22:11:03 +0200644 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100647 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200648 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200649
Damjan Marion23227982018-10-22 13:38:57 +0200650 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
652 return /* no error */ 0;
653}
654
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200656linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200657{
Damjan Marion23227982018-10-22 13:38:57 +0200658 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100659 VFIO_IRQ_SET_ACTION_UNMASK, 0);
660}
661
Damjan Marioncef87f12017-10-05 15:32:41 +0200662static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200663linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664{
665 u32 error_index = (u32) uf->private_data;
666
667 return clib_error_return (0, "pci device %d: error", error_index);
668}
669
Damjan Marioncef87f12017-10-05 15:32:41 +0200670static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100671linux_pci_vfio_msix_read_ready (clib_file_t * uf)
672{
Damjan Marion23227982018-10-22 13:38:57 +0200673 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100674 int __attribute__ ((unused)) rv;
675 vlib_pci_dev_handle_t h = uf->private_data >> 16;
676 u16 line = uf->private_data & 0xffff;
677 linux_pci_device_t *p = linux_pci_get_device (h);
678 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
679
680 u64 icount;
681 rv = read (uf->file_descriptor, &icount, sizeof (icount));
682
683 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200684 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100685
686 return /* no error */ 0;
687}
688
689static clib_error_t *
690linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691{
Damjan Marion23227982018-10-22 13:38:57 +0200692 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200693 int __attribute__ ((unused)) rv;
694 vlib_pci_dev_handle_t h = uf->private_data;
695 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100696 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697
Damjan Marioncef87f12017-10-05 15:32:41 +0200698 u64 icount;
699 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100701 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200702 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703
Damjan Marion23227982018-10-22 13:38:57 +0200704 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Damjan Marioncef87f12017-10-05 15:32:41 +0200706 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707}
708
Damjan Marioncef87f12017-10-05 15:32:41 +0200709static clib_error_t *
710linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711{
Damjan Marioncef87f12017-10-05 15:32:41 +0200712 u32 error_index = (u32) uf->private_data;
713
714 return clib_error_return (0, "pci device %d: error", error_index);
715}
716
717static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200718add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
719 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200720{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100721 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200722 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200723 u8 *s = 0;
724
Damjan Marionf9a968e2018-02-25 23:26:22 +0100725 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100726 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200727
728 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
729 format_vlib_pci_addr, &di->addr, 0);
730
731 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100732 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200733 vec_reset_length (s);
734
735 if (p->config_fd == -1)
736 {
737 err = clib_error_return_unix (0, "open '%s'", s);
738 goto error;
739 }
740
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100741 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
742 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200743 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
744 1);
745 vec_reset_length (s);
746
747 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100748 p->fd = open ((char *) s, O_RDWR);
749 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200750 {
751 err = clib_error_return_unix (0, "open '%s'", s);
752 goto error;
753 }
754
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100755 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200756 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200757
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100758 if (r && r->init_function)
759 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200760
761error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200762 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200763 if (err)
764 {
Damjan Marion96504182017-12-10 23:54:46 +0100765 if (p->config_fd != -1)
766 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100767 if (p->fd != -1)
768 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200769 }
770 return err;
771}
772
Damjan Marion599a16b2018-03-04 19:35:23 +0100773clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200774vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100775 pci_intx_handler_function_t * intx_handler)
776{
777 linux_pci_device_t *p = linux_pci_get_device (h);
778 clib_file_t t = { 0 };
779 linux_pci_irq_t *irq = &p->intx_irq;
780 ASSERT (irq->fd == -1);
781
782 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
783 {
Damjan Marion23227982018-10-22 13:38:57 +0200784 struct vfio_irq_info ii = { 0 };
785 ii.argsz = sizeof (struct vfio_irq_info);
786 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
787 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100788 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
789 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200790 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
791 __func__, ii.index, ii.count,
792 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
793 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
794 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
795 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
796 ii.flags);
797 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100798 return clib_error_return (0, "INTx interrupt does not exist on device"
799 "'%U'", format_vlib_pci_addr, &p->addr);
800
801 irq->fd = eventfd (0, EFD_NONBLOCK);
802 if (irq->fd == -1)
803 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200804
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100805 t.file_descriptor = irq->fd;
806 t.read_function = linux_pci_vfio_intx_read_ready;
807 }
808 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
809 {
810 t.file_descriptor = p->fd;
811 t.read_function = linux_pci_uio_read_ready;
812 }
813 else
814 return 0;
815
816 t.error_function = linux_pci_uio_error_ready;
817 t.private_data = p->handle;
818 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
819 irq->clib_file_index = clib_file_add (&file_main, &t);
820 irq->intx_handler = intx_handler;
821 return 0;
822}
823
824clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200825vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
826 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100827 pci_msix_handler_function_t * msix_handler)
828{
829 clib_error_t *err = 0;
830 linux_pci_device_t *p = linux_pci_get_device (h);
831 u32 i;
832
833 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
834 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
835 "support");
836
837 /* *INDENT-OFF* */
838 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
839 { .fd = -1});
840 /* *INDENT-ON* */
841
842 for (i = start; i < start + count; i++)
843 {
844 clib_file_t t = { 0 };
845 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
846 ASSERT (irq->fd == -1);
847
848 irq->fd = eventfd (0, EFD_NONBLOCK);
849 if (irq->fd == -1)
850 {
851 err = clib_error_return_unix (0, "eventfd");
852 goto error;
853 }
854
855 t.read_function = linux_pci_vfio_msix_read_ready;
856 t.file_descriptor = irq->fd;
857 t.error_function = linux_pci_vfio_error_ready;
858 t.private_data = p->handle << 16 | i;
859 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
860 &p->addr, i);
861 irq->clib_file_index = clib_file_add (&file_main, &t);
862 irq->msix_handler = msix_handler;
863 }
864
865 return 0;
866
867error:
868 while (i-- > start)
869 {
870 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
871 if (irq->fd != -1)
872 {
873 clib_file_del_by_index (&file_main, irq->clib_file_index);
874 close (irq->fd);
875 irq->fd = -1;
876 }
877 }
878 return err;
879}
880
881clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200882vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
883 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100884{
885 linux_pci_device_t *p = linux_pci_get_device (h);
886 int fds[count];
887 int i;
888
889 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
890 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
891 "support");
892
893 for (i = start; i < start + count; i++)
894 {
895 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
896 fds[i] = irq->fd;
897 }
898
Damjan Marion23227982018-10-22 13:38:57 +0200899 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100900 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
901}
902
903clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200904vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
905 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100906{
907 linux_pci_device_t *p = linux_pci_get_device (h);
908 int i, fds[count];
909
910 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
911 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
912 "support");
913
914 for (i = start; i < start + count; i++)
915 fds[i] = -1;
916
Damjan Marion23227982018-10-22 13:38:57 +0200917 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100918 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
919}
920
Damjan Marioncef87f12017-10-05 15:32:41 +0200921static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200922add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
923 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200924{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100925 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100926 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +0100927 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200928 clib_error_t *err = 0;
929 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +0200930 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +0200931
Damjan Marion2060db82018-03-04 17:37:15 +0100932 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200933
Damjan Marion23227982018-10-22 13:38:57 +0200934 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +0200935 return err;
936
Damjan Marion23227982018-10-22 13:38:57 +0200937 if (is_noiommu == 0)
938 p->supports_va_dma = 1;
939
Damjan Marioncef87f12017-10-05 15:32:41 +0200940 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100941 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200942 {
943 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
944 format_vlib_pci_addr, &di->addr);
945 goto error;
946 }
947
Damjan Marionf313b742018-03-05 14:08:33 +0100948 reg.argsz = sizeof (struct vfio_region_info);
949 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
950 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
951 {
952 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
953 format_vlib_pci_addr, &di->addr);
954 goto error;
955 }
Damjan Marion23227982018-10-22 13:38:57 +0200956
957 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
958 "flags: %s%s%s(0x%x)", __func__,
959 reg.index, reg.size, reg.offset,
960 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
961 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
962 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
963 reg.flags);
964
Damjan Marionf313b742018-03-05 14:08:33 +0100965 p->config_offset = reg.offset;
966 p->config_fd = p->fd;
967
Damjan Marioncef87f12017-10-05 15:32:41 +0200968 /* reset if device supports it */
969 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100970 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200971 {
972 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
973 format_vlib_pci_addr, &di->addr);
974 goto error;
975 }
976
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100977 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200978 {
Damjan Marion23227982018-10-22 13:38:57 +0200979 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
980 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +0200981 }
982
Damjan Marion68b4da62018-09-30 18:26:20 +0200983 if (p->supports_va_dma)
984 {
985 vlib_buffer_pool_t *bp;
986 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +0100987 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +0200988 {
989 u32 i;
990 vlib_physmem_map_t *pm;
991 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
992 for (i = 0; i < pm->n_pages; i++)
993 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
994 }
995 /* *INDENT-ON* */
996 }
997
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100998 if (r && r->init_function)
999 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001000
1001error:
1002 vec_free (s);
1003 if (err)
1004 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001005 if (p->fd != -1)
1006 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001007 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001008 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001009 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001010 }
1011 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012}
1013
1014/* Configuration space read/write. */
1015clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001016vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001017 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001018 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019{
Damjan Marioncef87f12017-10-05 15:32:41 +02001020 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 int n;
1022
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001024 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001026 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027
1028 if (n != n_bytes)
1029 return clib_error_return_unix (0, "%s",
1030 read_or_write == VLIB_READ
1031 ? "read" : "write");
1032
1033 return 0;
1034}
1035
1036static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001037vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1038 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039{
Damjan Marioncef87f12017-10-05 15:32:41 +02001040 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001041 clib_error_t *error = 0;
1042 int _fd = -1;
1043 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001044
1045 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
1047 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
Damjan Marion2060db82018-03-04 17:37:15 +01001049 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050 {
Damjan Marion2060db82018-03-04 17:37:15 +01001051 u8 *file_name;
1052 struct stat stat_buf;
1053 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1054 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001056 _fd = open ((char *) file_name, O_RDWR);
1057 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001058 {
1059 error = clib_error_return_unix (0, "open `%s'", file_name);
1060 vec_free (file_name);
1061 return error;
1062 }
1063
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001064 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001065 {
1066 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1067 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001068 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001069 return error;
1070 }
1071
1072 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001073 _size = stat_buf.st_size;
1074 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001075 }
1076 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077 {
Damjan Marion2060db82018-03-04 17:37:15 +01001078 struct vfio_region_info reg = { 0 };
1079 reg.argsz = sizeof (struct vfio_region_info);
1080 reg.index = bar;
1081 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1082 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1083 "'%U'", format_vlib_pci_addr,
1084 &p->addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001085 _fd = p->fd;
1086 _size = reg.size;
1087 _offset = reg.offset;
Damjan Marion23227982018-10-22 13:38:57 +02001088 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
1089 "flags: %s%s%s(0x%x)", __func__,
1090 reg.index, reg.size, reg.offset,
1091 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
1092 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
1093 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
1094 reg.flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 }
Damjan Marion2060db82018-03-04 17:37:15 +01001096 else
1097 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001099 *fd = _fd;
1100 *size = _size;
1101 *offset = _offset;
1102
1103 return error;
1104}
1105
1106static clib_error_t *
1107vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1108 u32 bar, u8 * addr, void **result)
1109{
1110 linux_pci_device_t *p = linux_pci_get_device (h);
1111 int fd = -1;
1112 clib_error_t *error;
1113 int flags = MAP_SHARED;
1114 u64 size = 0, offset = 0;
1115
1116 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1117
1118 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1119 return error;
1120
1121 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1122 flags |= MAP_FIXED;
1123
Damjan Marion2060db82018-03-04 17:37:15 +01001124 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125 if (*result == (void *) -1)
1126 {
Damjan Marion2060db82018-03-04 17:37:15 +01001127 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1128 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001130 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 }
Damjan Marion2060db82018-03-04 17:37:15 +01001132
1133 /* *INDENT-OFF* */
1134 vec_validate_init_empty (p->regions, bar,
1135 (linux_pci_region_t) { .fd = -1});
1136 /* *INDENT-ON* */
1137 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1138 p->regions[bar].fd = fd;
1139 p->regions[bar].addr = *result;
1140 p->regions[bar].size = size;
1141 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142}
1143
1144clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001145vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1146 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147{
Damjan Marion23227982018-10-22 13:38:57 +02001148 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149}
1150
1151clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001152vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1153 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154{
Damjan Marion23227982018-10-22 13:38:57 +02001155 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156}
1157
Damjan Marionf313b742018-03-05 14:08:33 +01001158clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001159vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1160{
1161 linux_pci_device_t *p = linux_pci_get_device (h);
1162 clib_error_t *error = 0;
1163 int fd = -1;
1164 u64 size = 0, offset = 0;
1165
1166 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1167 return error;
1168
1169 p->io_fd = fd;
1170 p->io_offset = offset;
1171 return error;
1172}
1173
1174clib_error_t *
1175vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1176 vlib_read_or_write_t read_or_write,
1177 uword offset, void *data, u32 length)
1178{
1179 linux_pci_device_t *p = linux_pci_get_device (h);
1180 int n = 0;
1181
1182 if (read_or_write == VLIB_READ)
1183 n = pread (p->io_fd, data, length, p->io_offset + offset);
1184 else
1185 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1186
1187 if (n != length)
1188 return clib_error_return_unix (0, "%s",
1189 read_or_write == VLIB_READ
1190 ? "read" : "write");
1191 return 0;
1192}
1193
1194clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001195vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1196{
1197 linux_pci_device_t *p = linux_pci_get_device (h);
1198
1199 if (!p->supports_va_dma)
1200 return 0;
1201
1202 return vfio_map_physmem_page (vm, ptr);
1203}
1204
1205int
1206vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1207{
1208 linux_pci_device_t *p = linux_pci_get_device (h);
1209
1210 return p->supports_va_dma != 0;
1211}
1212
1213clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001214vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001215 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1216{
1217 linux_pci_main_t *lpm = &linux_pci_main;
1218 vlib_pci_device_info_t *di;
1219 linux_pci_device_t *p;
1220 clib_error_t *err = 0;
1221 pci_device_id_t *i;
1222
Damjan Marion23227982018-10-22 13:38:57 +02001223 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001224
1225 if (err)
1226 return err;
1227 for (i = ids; i->vendor_id != 0; i++)
1228 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1229 break;
1230
1231 if (i->vendor_id == 0)
1232 return clib_error_return (0, "Wrong vendor or device id");
1233
1234 pool_get (lpm->linux_pci_devices, p);
1235 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001236 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001237 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001238 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001239 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001240 /*
1241 * pci io bar read/write fd
1242 */
1243 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001244
Damjan Marion23227982018-10-22 13:38:57 +02001245 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1246 di->vendor_id, di->device_id, di->driver_name,
1247 di->iommu_group);
1248
Steven Luongab489922019-08-21 11:35:27 -07001249 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001250 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001251 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001252 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001253 else
Damjan Marion23227982018-10-22 13:38:57 +02001254 err = clib_error_create ("device not bound to 'vfio-pci' or "
1255 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001256 if (err)
1257 goto error;
1258
1259 *handle = p->handle;
1260
1261error:
1262 vlib_pci_free_device_info (di);
1263 if (err)
1264 {
Damjan Marion23227982018-10-22 13:38:57 +02001265 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001266 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001267 pool_put (lpm->linux_pci_devices, p);
1268 }
1269
1270 return err;
1271}
1272
1273void
Damjan Marion23227982018-10-22 13:38:57 +02001274vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001275{
1276 linux_pci_main_t *lpm = &linux_pci_main;
1277 linux_pci_device_t *p = linux_pci_get_device (h);
1278 linux_pci_irq_t *irq;
1279 linux_pci_region_t *res;
1280 clib_error_t *err = 0;
1281
1282 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1283 {
1284 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001285 if (irq->clib_file_index != -1)
1286 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001287 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001288 if (p->io_fd != -1)
1289 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001290 }
1291 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1292 {
1293 irq = &p->intx_irq;
1294 /* close INTx irqs */
1295 if (irq->fd != -1)
1296 {
Damjan Marion23227982018-10-22 13:38:57 +02001297 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001298 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1299 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001300 if (irq->clib_file_index != -1)
1301 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001302 close (irq->fd);
1303 }
1304
1305 /* close MSI-X irqs */
1306 if (vec_len (p->msix_irqs))
1307 {
Damjan Marion23227982018-10-22 13:38:57 +02001308 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001309 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1310 clib_error_free (err);
1311 /* *INDENT-OFF* */
1312 vec_foreach (irq, p->msix_irqs)
1313 {
1314 if (irq->fd == -1)
1315 continue;
1316 clib_file_del_by_index (&file_main, irq->clib_file_index);
1317 close (irq->fd);
1318 }
1319 /* *INDENT-ON* */
1320 vec_free (p->msix_irqs);
1321 }
1322 }
1323
1324 /* *INDENT-OFF* */
1325 vec_foreach (res, p->regions)
1326 {
1327 if (res->size == 0)
1328 continue;
1329 munmap (res->addr, res->size);
1330 if (res->fd != -1)
1331 close (res->fd);
1332 }
1333 /* *INDENT-ON* */
1334 vec_free (p->regions);
1335
1336 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001337 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001338 pool_put (lpm->linux_pci_devices, p);
1339}
1340
Dave Barach9b8ffd92016-07-08 08:13:45 -04001341void
Damjan Marion23227982018-10-22 13:38:57 +02001342init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001344 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001345 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001346 pci_device_registration_t *r;
1347 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001348 clib_error_t *err = 0;
1349 linux_pci_device_t *p;
1350
1351 pool_get (lpm->linux_pci_devices, p);
1352 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001353 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354
Damjan Marion5a206ea2016-05-12 22:11:03 +02001355 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001356
1357 while (r)
1358 {
1359 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001360 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001361 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001362 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001363 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001364 else
Damjan Marion23227982018-10-22 13:38:57 +02001365 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001366
Damjan Marioncef87f12017-10-05 15:32:41 +02001367 if (err)
1368 clib_error_report (err);
1369 else
1370 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001371 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001373 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001374
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001376 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001377 pool_put (lpm->linux_pci_devices, p);
1378}
1379
1380static clib_error_t *
1381scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1382{
1383 vlib_pci_addr_t addr, **addrv = arg;
1384 unformat_input_t input;
1385 clib_error_t *err = 0;
1386
1387 unformat_init_string (&input, (char *) dev_dir_name,
1388 vec_len (dev_dir_name));
1389
1390 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1391 unformat_vlib_pci_addr, &addr))
1392 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1393
1394 unformat_free (&input);
1395
1396 if (err)
1397 return err;
1398
1399 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400 return 0;
1401}
1402
Damjan Marioncef87f12017-10-05 15:32:41 +02001403static int
1404pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405{
Damjan Marioncef87f12017-10-05 15:32:41 +02001406 vlib_pci_addr_t *a1 = v1;
1407 vlib_pci_addr_t *a2 = v2;
1408
1409 if (a1->domain > a2->domain)
1410 return 1;
1411 if (a1->domain < a2->domain)
1412 return -1;
1413 if (a1->bus > a2->bus)
1414 return 1;
1415 if (a1->bus < a2->bus)
1416 return -1;
1417 if (a1->slot > a2->slot)
1418 return 1;
1419 if (a1->slot < a2->slot)
1420 return -1;
1421 if (a1->function > a2->function)
1422 return 1;
1423 if (a1->function < a2->function)
1424 return -1;
1425 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001426}
1427
Damjan Marioncef87f12017-10-05 15:32:41 +02001428vlib_pci_addr_t *
1429vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430{
Damjan Marioncef87f12017-10-05 15:32:41 +02001431 vlib_pci_addr_t *addrs = 0;
1432 clib_error_t *err;
1433 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1434 &addrs, /* scan_dirs */ 0);
1435 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001437 vec_free (addrs);
1438 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 }
1440
Damjan Marioncef87f12017-10-05 15:32:41 +02001441 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001442
Damjan Marioncef87f12017-10-05 15:32:41 +02001443 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444}
1445
Dave Barach9b8ffd92016-07-08 08:13:45 -04001446clib_error_t *
1447linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001449 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001450 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451
1452 pm->vlib_main = vm;
1453
Dave Barach9b8ffd92016-07-08 08:13:45 -04001454 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001455
Damjan Marioncef87f12017-10-05 15:32:41 +02001456 addrs = vlib_pci_get_all_dev_addrs ();
1457 /* *INDENT-OFF* */
1458 vec_foreach (addr, addrs)
1459 {
1460 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001461 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001462 {
Damjan Marion23227982018-10-22 13:38:57 +02001463 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001464 vlib_pci_free_device_info (d);
1465 }
1466 }
1467 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468
Dave Barachf8d50682019-05-14 18:01:44 -04001469 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001470}
1471
Dave Barachf8d50682019-05-14 18:01:44 -04001472/* *INDENT-OFF* */
1473VLIB_INIT_FUNCTION (linux_pci_init) =
1474{
1475 .runs_after = VLIB_INITS("unix_input_init"),
1476};
1477/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001478
1479/*
1480 * fd.io coding-style-patch-verification: ON
1481 *
1482 * Local Variables:
1483 * eval: (c-set-style "gnu")
1484 * End:
1485 */