Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * pci.c: Linux user space PCI bus management. |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vlib/pci/pci.h> |
| 42 | #include <vlib/unix/unix.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <dirent.h> |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 48 | #include <sys/ioctl.h> |
| 49 | #include <net/if.h> |
| 50 | #include <linux/ethtool.h> |
| 51 | #include <linux/sockios.h> |
| 52 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 55 | /* /sys/bus/pci/devices/... directory name for this device. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 56 | u8 *dev_dir_name; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 57 | |
| 58 | /* Resource file descriptors. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 59 | int *resource_fds; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 60 | |
| 61 | /* File descriptor for config space read/write. */ |
| 62 | int config_fd; |
| 63 | |
| 64 | /* File descriptor for /dev/uio%d */ |
| 65 | int uio_fd; |
| 66 | |
| 67 | /* Minor device for uio device. */ |
| 68 | u32 uio_minor; |
| 69 | |
| 70 | /* Index given by unix_file_add. */ |
| 71 | u32 unix_file_index; |
| 72 | |
| 73 | } linux_pci_device_t; |
| 74 | |
| 75 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 76 | typedef struct |
| 77 | { |
| 78 | vlib_main_t *vlib_main; |
| 79 | linux_pci_device_t *linux_pci_devices; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 80 | } linux_pci_main_t; |
| 81 | |
| 82 | extern linux_pci_main_t linux_pci_main; |
| 83 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 84 | /* Call to allocate/initialize the pci subsystem. |
| 85 | This is not an init function so that users can explicitly enable |
| 86 | pci only when it's needed. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 87 | clib_error_t *pci_bus_init (vlib_main_t * vm); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 88 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d, |
| 90 | char *uio_driver_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
| 92 | linux_pci_main_t linux_pci_main; |
| 93 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 94 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 95 | vlib_pci_bind_to_uio (vlib_pci_device_t * d, char *uio_driver_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | clib_error_t *error = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 98 | u8 *s = 0; |
| 99 | DIR *dir = 0; |
| 100 | struct dirent *e; |
| 101 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 102 | u8 *dev_dir_name = format (0, "/sys/bus/pci/devices/%U", |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 103 | format_vlib_pci_addr, &d->bus_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 105 | /* if uio sub-directory exists, we are fine, device is |
| 106 | already bound to UIO driver */ |
| 107 | s = format (s, "%v/uio%c", dev_dir_name, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 108 | if (access ((char *) s, F_OK) == 0) |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 109 | goto done; |
| 110 | vec_reset_length (s); |
| 111 | |
| 112 | /* walk trough all linux interfaces and if interface belonging to |
| 113 | this device is founf check if interface is admin up */ |
| 114 | dir = opendir ("/sys/class/net"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 115 | s = format (s, "%U%c", format_vlib_pci_addr, &d->bus_address, 0); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 116 | |
| 117 | if (!dir) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 119 | error = clib_error_return (0, "Skipping PCI device %U: failed to " |
| 120 | "read /sys/class/net", |
| 121 | format_vlib_pci_addr, &d->bus_address); |
| 122 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 125 | fd = socket (PF_INET, SOCK_DGRAM, 0); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 126 | if (fd < 0) |
| 127 | { |
| 128 | error = clib_error_return_unix (0, "socket"); |
| 129 | goto done; |
| 130 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 131 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 132 | while ((e = readdir (dir))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 134 | struct ifreq ifr; |
| 135 | struct ethtool_drvinfo drvinfo; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 137 | if (e->d_name[0] == '.') /* skip . and .. */ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 138 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 140 | memset (&ifr, 0, sizeof ifr); |
| 141 | memset (&drvinfo, 0, sizeof drvinfo); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 142 | ifr.ifr_data = (char *) &drvinfo; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 143 | strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 144 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 145 | if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 146 | { |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 147 | /* Some interfaces (eg "lo") don't support this ioctl */ |
| 148 | if ((errno != ENOTSUP) && (errno != ENODEV)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 149 | clib_unix_warning ("ioctl fetch intf %s bus info error", |
| 150 | e->d_name); |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 151 | continue; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 152 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 153 | |
| 154 | if (strcmp ((char *) s, drvinfo.bus_info)) |
| 155 | continue; |
| 156 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 157 | memset (&ifr, 0, sizeof (ifr)); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 158 | strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1); |
| 159 | if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 160 | { |
| 161 | error = clib_error_return_unix (0, "ioctl fetch intf %s flags", |
| 162 | e->d_name); |
| 163 | close (fd); |
| 164 | goto done; |
| 165 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 166 | |
| 167 | if (ifr.ifr_flags & IFF_UP) |
| 168 | { |
| 169 | error = clib_error_return (0, "Skipping PCI device %U as host " |
| 170 | "interface %s is up", |
| 171 | format_vlib_pci_addr, &d->bus_address, |
| 172 | e->d_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 173 | close (fd); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 174 | goto done; |
| 175 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 178 | close (fd); |
| 179 | vec_reset_length (s); |
| 180 | |
| 181 | s = format (s, "%v/driver/unbind%c", dev_dir_name, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 182 | vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 183 | vec_reset_length (s); |
| 184 | |
| 185 | s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0); |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 186 | vlib_sysfs_write ((char *) s, "0x%04x 0x%04x", d->vendor_id, d->device_id); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 187 | vec_reset_length (s); |
| 188 | |
| 189 | s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 190 | vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 191 | |
| 192 | done: |
| 193 | closedir (dir); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | vec_free (s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 195 | vec_free (dev_dir_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | return error; |
| 197 | } |
| 198 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | |
| 200 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 201 | scan_uio_dir (void *arg, u8 * path_name, u8 * file_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 203 | linux_pci_device_t *l = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | unformat_input_t input; |
| 205 | |
| 206 | unformat_init_string (&input, (char *) file_name, vec_len (file_name)); |
| 207 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 208 | if (!unformat (&input, "uio%d", &l->uio_minor)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | abort (); |
| 210 | |
| 211 | unformat_free (&input); |
| 212 | return 0; |
| 213 | } |
| 214 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 215 | static clib_error_t * |
| 216 | linux_pci_uio_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 218 | vlib_pci_main_t *pm = &pci_main; |
| 219 | vlib_pci_device_t *d; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 220 | int __attribute__ ((unused)) rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 222 | u32 icount; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | rv = read (uf->file_descriptor, &icount, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 225 | d = pool_elt_at_index (pm->pci_devs, uf->private_data); |
| 226 | |
| 227 | if (d->interrupt_handler) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 228 | d->interrupt_handler (d); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 229 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 230 | vlib_pci_intr_enable (d); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
| 232 | return /* no error */ 0; |
| 233 | } |
| 234 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 235 | static clib_error_t * |
| 236 | linux_pci_uio_error_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | { |
| 238 | u32 error_index = (u32) uf->private_data; |
| 239 | |
| 240 | return clib_error_return (0, "pci device %d: error", error_index); |
| 241 | } |
| 242 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 243 | static void |
| 244 | add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 246 | vlib_pci_main_t *pm = &pci_main; |
| 247 | linux_pci_main_t *lpm = &linux_pci_main; |
| 248 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 250 | pool_get (lpm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | l[0] = pdev[0]; |
| 252 | |
| 253 | l->dev_dir_name = vec_dup (l->dev_dir_name); |
| 254 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 255 | dev->os_handle = l - lpm->linux_pci_devices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
| 257 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 258 | u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name); |
| 259 | foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ |
| 260 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | vec_free (uio_dir); |
| 262 | } |
| 263 | |
| 264 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 265 | char *uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | l->uio_fd = open (uio_name, O_RDWR); |
| 267 | if (l->uio_fd < 0) |
| 268 | clib_unix_error ("open `%s'", uio_name); |
| 269 | vec_free (uio_name); |
| 270 | } |
| 271 | |
| 272 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 273 | unix_file_t template = { 0 }; |
| 274 | unix_main_t *um = &unix_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | |
| 276 | template.read_function = linux_pci_uio_read_ready; |
| 277 | template.file_descriptor = l->uio_fd; |
| 278 | template.error_function = linux_pci_uio_error_ready; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 279 | template.private_data = dev - pm->pci_devs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | |
| 281 | l->unix_file_index = unix_file_add (um, &template); |
| 282 | } |
| 283 | } |
| 284 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 285 | static void |
| 286 | linux_pci_device_free (linux_pci_device_t * l) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | { |
| 288 | int i; |
| 289 | for (i = 0; i < vec_len (l->resource_fds); i++) |
| 290 | if (l->resource_fds[i] > 0) |
| 291 | close (l->resource_fds[i]); |
| 292 | if (l->config_fd > 0) |
| 293 | close (l->config_fd); |
| 294 | if (l->uio_fd > 0) |
| 295 | close (l->uio_fd); |
| 296 | vec_free (l->resource_fds); |
| 297 | vec_free (l->dev_dir_name); |
| 298 | } |
| 299 | |
| 300 | /* Configuration space read/write. */ |
| 301 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 302 | vlib_pci_read_write_config (vlib_pci_device_t * dev, |
| 303 | vlib_read_or_write_t read_or_write, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 304 | uword address, void *data, u32 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 306 | linux_pci_main_t *lpm = &linux_pci_main; |
| 307 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | int n; |
| 309 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 310 | p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | |
| 312 | if (read_or_write == VLIB_READ) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 313 | n = pread (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | else |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 315 | n = pwrite (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
| 317 | if (n != n_bytes) |
| 318 | return clib_error_return_unix (0, "%s", |
| 319 | read_or_write == VLIB_READ |
| 320 | ? "read" : "write"); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static clib_error_t * |
| 326 | os_map_pci_resource_internal (uword os_handle, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 327 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 329 | linux_pci_main_t *pm = &linux_pci_main; |
| 330 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | struct stat stat_buf; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 332 | u8 *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 334 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | int flags = MAP_SHARED; |
| 336 | |
| 337 | error = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 338 | p = pool_elt_at_index (pm->linux_pci_devices, os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
| 340 | file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0); |
| 341 | fd = open ((char *) file_name, O_RDWR); |
| 342 | if (fd < 0) |
| 343 | { |
| 344 | error = clib_error_return_unix (0, "open `%s'", file_name); |
| 345 | goto done; |
| 346 | } |
| 347 | |
| 348 | if (fstat (fd, &stat_buf) < 0) |
| 349 | { |
| 350 | error = clib_error_return_unix (0, "fstat `%s'", file_name); |
| 351 | goto done; |
| 352 | } |
| 353 | |
| 354 | vec_validate (p->resource_fds, resource); |
| 355 | p->resource_fds[resource] = fd; |
| 356 | if (addr != 0) |
| 357 | flags |= MAP_FIXED; |
| 358 | |
| 359 | *result = mmap (addr, |
| 360 | /* size */ stat_buf.st_size, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 361 | PROT_READ | PROT_WRITE, flags, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | /* file */ fd, |
| 363 | /* offset */ 0); |
| 364 | if (*result == (void *) -1) |
| 365 | { |
| 366 | error = clib_error_return_unix (0, "mmap `%s'", file_name); |
| 367 | goto done; |
| 368 | } |
| 369 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 370 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | if (error) |
| 372 | { |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 373 | if (fd >= 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 374 | close (fd); |
| 375 | } |
| 376 | vec_free (file_name); |
| 377 | return error; |
| 378 | } |
| 379 | |
| 380 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 381 | vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 383 | return (os_map_pci_resource_internal |
| 384 | (dev->os_handle, resource, 0 /* addr */ , |
| 385 | result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 389 | vlib_pci_map_resource_fixed (vlib_pci_device_t * dev, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 390 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 392 | return (os_map_pci_resource_internal |
| 393 | (dev->os_handle, resource, addr, result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 396 | void |
| 397 | vlib_pci_free_device (vlib_pci_device_t * dev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 399 | linux_pci_main_t *pm = &linux_pci_main; |
| 400 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 402 | l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | linux_pci_device_free (l); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 404 | pool_put (pm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 407 | pci_device_registration_t * __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | pci_device_next_registered (pci_device_registration_t * r) |
| 409 | { |
| 410 | uword i; |
| 411 | |
| 412 | /* Null vendor id marks end of initialized list. */ |
| 413 | for (i = 0; r->supported_devices[i].vendor_id != 0; i++) |
| 414 | ; |
| 415 | |
| 416 | return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0])); |
| 417 | } |
| 418 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 419 | static clib_error_t * |
| 420 | init_device_from_registered (vlib_main_t * vm, |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 421 | vlib_pci_device_t * dev, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | linux_pci_device_t * pdev) |
| 423 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 424 | vlib_pci_main_t *pm = &pci_main; |
| 425 | pci_device_registration_t *r; |
| 426 | pci_device_id_t *i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 427 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 429 | r = pm->pci_device_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | |
| 431 | while (r) |
| 432 | { |
| 433 | for (i = r->supported_devices; i->vendor_id != 0; i++) |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 434 | if (i->vendor_id == dev->vendor_id && i->device_id == dev->device_id) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 435 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 436 | error = vlib_pci_bind_to_uio (dev, "uio_pci_generic"); |
| 437 | if (error) |
| 438 | { |
| 439 | clib_error_report (error); |
| 440 | continue; |
| 441 | } |
| 442 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 443 | add_device (dev, pdev); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 444 | dev->interrupt_handler = r->interrupt_handler; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 445 | return r->init_function (vm, dev); |
| 446 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | r = r->next_registration; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 448 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | /* No driver, close the PCI config-space FD */ |
| 450 | close (pdev->config_fd); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static clib_error_t * |
| 455 | init_device (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 456 | vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | { |
| 458 | return init_device_from_registered (vm, dev, pdev); |
| 459 | } |
| 460 | |
| 461 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 462 | scan_device (void *arg, u8 * dev_dir_name, u8 * ignored) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 464 | vlib_main_t *vm = arg; |
| 465 | vlib_pci_main_t *pm = &pci_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 467 | u8 *f; |
| 468 | clib_error_t *error = 0; |
| 469 | vlib_pci_device_t *dev; |
| 470 | linux_pci_device_t pdev = { 0 }; |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 471 | u32 tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
| 473 | f = format (0, "%v/config%c", dev_dir_name, 0); |
| 474 | fd = open ((char *) f, O_RDWR); |
| 475 | |
| 476 | /* Try read-only access if write fails. */ |
| 477 | if (fd < 0) |
| 478 | fd = open ((char *) f, O_RDONLY); |
| 479 | |
| 480 | if (fd < 0) |
| 481 | { |
| 482 | error = clib_error_return_unix (0, "open `%s'", f); |
| 483 | goto done; |
| 484 | } |
| 485 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 486 | pool_get (pm->pci_devs, dev); |
| 487 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | /* You can only read more that 64 bytes of config space as root; so we try to |
| 489 | read the full space but fall back to just the first 64 bytes. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 490 | if (read (fd, &dev->config_data, sizeof (dev->config_data)) != |
| 491 | sizeof (dev->config_data) |
| 492 | && read (fd, &dev->config0, |
| 493 | sizeof (dev->config0)) != sizeof (dev->config0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 495 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | error = clib_error_return_unix (0, "read `%s'", f); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 497 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | goto done; |
| 499 | } |
| 500 | |
| 501 | { |
| 502 | static pci_config_header_t all_ones; |
| 503 | if (all_ones.vendor_id == 0) |
| 504 | memset (&all_ones, ~0, sizeof (all_ones)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 505 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 506 | if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 508 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | error = clib_error_return (0, "invalid PCI config for `%s'", f); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 510 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | goto done; |
| 512 | } |
| 513 | } |
| 514 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 515 | if (dev->config0.header.header_type == 0) |
| 516 | pci_config_type0_little_to_host (&dev->config0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | else |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 518 | pci_config_type1_little_to_host (&dev->config1); |
| 519 | |
| 520 | /* Parse bus, dev, function from directory name. */ |
| 521 | { |
| 522 | unformat_input_t input; |
| 523 | |
| 524 | unformat_init_string (&input, (char *) dev_dir_name, |
| 525 | vec_len (dev_dir_name)); |
| 526 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 527 | if (!unformat (&input, "/sys/bus/pci/devices/%U", |
| 528 | unformat_vlib_pci_addr, &dev->bus_address)) |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 529 | abort (); |
| 530 | |
| 531 | unformat_free (&input); |
| 532 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | |
| 536 | pdev.config_fd = fd; |
| 537 | pdev.dev_dir_name = dev_dir_name; |
| 538 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 539 | hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32, |
| 540 | dev - pm->pci_devs); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 541 | |
| 542 | error = init_device (vm, dev, &pdev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 544 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 545 | f = format (f, "%v/vpd%c", dev_dir_name, 0); |
| 546 | fd = open ((char *) f, O_RDONLY); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 547 | if (fd >= 0) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 548 | { |
| 549 | while (1) |
| 550 | { |
| 551 | u8 tag[3]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 552 | u8 *data = 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 553 | int len; |
| 554 | |
| 555 | if (read (fd, &tag, 3) != 3) |
| 556 | break; |
| 557 | |
| 558 | if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91) |
| 559 | break; |
| 560 | |
| 561 | len = (tag[2] << 8) | tag[1]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 562 | vec_validate (data, len); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 563 | |
| 564 | if (read (fd, data, len) != len) |
| 565 | { |
| 566 | vec_free (data); |
| 567 | break; |
| 568 | } |
| 569 | if (tag[0] == 0x82) |
| 570 | dev->product_name = data; |
| 571 | else if (tag[0] == 0x90) |
| 572 | dev->vpd_r = data; |
| 573 | else if (tag[0] == 0x91) |
| 574 | dev->vpd_w = data; |
| 575 | |
| 576 | data = 0; |
| 577 | } |
| 578 | close (fd); |
| 579 | } |
| 580 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 581 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 582 | f = format (f, "%v/driver%c", dev_dir_name, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 583 | dev->driver_name = vlib_sysfs_link_to_name ((char *) f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 584 | |
| 585 | dev->numa_node = -1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 586 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 587 | f = format (f, "%v/numa_node%c", dev_dir_name, 0); |
| 588 | vlib_sysfs_read ((char *) f, "%u", &dev->numa_node); |
| 589 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 590 | vec_reset_length (f); |
| 591 | f = format (f, "%v/class%c", dev_dir_name, 0); |
| 592 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 593 | dev->device_class = tmp >> 8; |
| 594 | |
| 595 | vec_reset_length (f); |
| 596 | f = format (f, "%v/vendor%c", dev_dir_name, 0); |
| 597 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 598 | dev->vendor_id = tmp; |
| 599 | |
| 600 | vec_reset_length (f); |
| 601 | f = format (f, "%v/device%c", dev_dir_name, 0); |
| 602 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 603 | dev->device_id = tmp; |
| 604 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 605 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | vec_free (f); |
| 607 | return error; |
| 608 | } |
| 609 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 610 | clib_error_t * |
| 611 | linux_pci_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 613 | vlib_pci_main_t *pm = &pci_main; |
| 614 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | |
| 616 | pm->vlib_main = vm; |
| 617 | |
| 618 | if ((error = vlib_call_init_function (vm, unix_input_init))) |
| 619 | return error; |
| 620 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 621 | ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 622 | pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword)); |
| 623 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 624 | error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, |
| 625 | /* scan_dirs */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 626 | |
| 627 | /* Complain and continue. might not be root, etc. */ |
| 628 | if (error) |
| 629 | clib_error_report (error); |
| 630 | |
| 631 | return error; |
| 632 | } |
| 633 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 634 | VLIB_INIT_FUNCTION (linux_pci_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 635 | |
| 636 | /* |
| 637 | * fd.io coding-style-patch-verification: ON |
| 638 | * |
| 639 | * Local Variables: |
| 640 | * eval: (c-set-style "gnu") |
| 641 | * End: |
| 642 | */ |