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 | |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 112 | s = format (s, "%v/iommu_group%c", dev_dir_name, 0); |
| 113 | if (access ((char *) s, F_OK) == 0) |
| 114 | goto done; |
| 115 | vec_reset_length (s); |
| 116 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 117 | /* walk trough all linux interfaces and if interface belonging to |
| 118 | this device is founf check if interface is admin up */ |
| 119 | dir = opendir ("/sys/class/net"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | if (!dir) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 124 | error = clib_error_return (0, "Skipping PCI device %U: failed to " |
| 125 | "read /sys/class/net", |
| 126 | format_vlib_pci_addr, &d->bus_address); |
| 127 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 130 | fd = socket (PF_INET, SOCK_DGRAM, 0); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 131 | if (fd < 0) |
| 132 | { |
| 133 | error = clib_error_return_unix (0, "socket"); |
| 134 | goto done; |
| 135 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 136 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 137 | while ((e = readdir (dir))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 139 | struct ifreq ifr; |
| 140 | struct ethtool_drvinfo drvinfo; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 142 | if (e->d_name[0] == '.') /* skip . and .. */ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 143 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | memset (&ifr, 0, sizeof ifr); |
| 146 | memset (&drvinfo, 0, sizeof drvinfo); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 147 | ifr.ifr_data = (char *) &drvinfo; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 148 | strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 149 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 150 | if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 151 | { |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 152 | /* Some interfaces (eg "lo") don't support this ioctl */ |
| 153 | if ((errno != ENOTSUP) && (errno != ENODEV)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 154 | clib_unix_warning ("ioctl fetch intf %s bus info error", |
| 155 | e->d_name); |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 156 | continue; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 157 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 158 | |
| 159 | if (strcmp ((char *) s, drvinfo.bus_info)) |
| 160 | continue; |
| 161 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 162 | memset (&ifr, 0, sizeof (ifr)); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 163 | strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1); |
| 164 | if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 165 | { |
| 166 | error = clib_error_return_unix (0, "ioctl fetch intf %s flags", |
| 167 | e->d_name); |
| 168 | close (fd); |
| 169 | goto done; |
| 170 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 171 | |
| 172 | if (ifr.ifr_flags & IFF_UP) |
| 173 | { |
| 174 | error = clib_error_return (0, "Skipping PCI device %U as host " |
| 175 | "interface %s is up", |
| 176 | format_vlib_pci_addr, &d->bus_address, |
| 177 | e->d_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 178 | close (fd); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 179 | goto done; |
| 180 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 183 | close (fd); |
| 184 | vec_reset_length (s); |
| 185 | |
| 186 | s = format (s, "%v/driver/unbind%c", dev_dir_name, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 187 | 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] | 188 | vec_reset_length (s); |
| 189 | |
| 190 | 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] | 191 | 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] | 192 | vec_reset_length (s); |
| 193 | |
| 194 | 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] | 195 | 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] | 196 | |
| 197 | done: |
| 198 | closedir (dir); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | vec_free (s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 200 | vec_free (dev_dir_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | return error; |
| 202 | } |
| 203 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | |
| 205 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 206 | scan_uio_dir (void *arg, u8 * path_name, u8 * file_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 208 | linux_pci_device_t *l = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | unformat_input_t input; |
| 210 | |
| 211 | unformat_init_string (&input, (char *) file_name, vec_len (file_name)); |
| 212 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 213 | if (!unformat (&input, "uio%d", &l->uio_minor)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | abort (); |
| 215 | |
| 216 | unformat_free (&input); |
| 217 | return 0; |
| 218 | } |
| 219 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 220 | static clib_error_t * |
| 221 | linux_pci_uio_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | vlib_pci_main_t *pm = &pci_main; |
| 224 | vlib_pci_device_t *d; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 225 | int __attribute__ ((unused)) rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 227 | u32 icount; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 228 | rv = read (uf->file_descriptor, &icount, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 230 | d = pool_elt_at_index (pm->pci_devs, uf->private_data); |
| 231 | |
| 232 | if (d->interrupt_handler) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 233 | d->interrupt_handler (d); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 234 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 235 | vlib_pci_intr_enable (d); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
| 237 | return /* no error */ 0; |
| 238 | } |
| 239 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 240 | static clib_error_t * |
| 241 | linux_pci_uio_error_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | { |
| 243 | u32 error_index = (u32) uf->private_data; |
| 244 | |
| 245 | return clib_error_return (0, "pci device %d: error", error_index); |
| 246 | } |
| 247 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 248 | static void |
| 249 | add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 251 | vlib_pci_main_t *pm = &pci_main; |
| 252 | linux_pci_main_t *lpm = &linux_pci_main; |
| 253 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 255 | pool_get (lpm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | l[0] = pdev[0]; |
| 257 | |
| 258 | l->dev_dir_name = vec_dup (l->dev_dir_name); |
| 259 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 260 | dev->os_handle = l - lpm->linux_pci_devices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | |
| 262 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name); |
| 264 | foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ |
| 265 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | vec_free (uio_dir); |
| 267 | } |
| 268 | |
| 269 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 270 | 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] | 271 | l->uio_fd = open (uio_name, O_RDWR); |
| 272 | if (l->uio_fd < 0) |
| 273 | clib_unix_error ("open `%s'", uio_name); |
| 274 | vec_free (uio_name); |
| 275 | } |
| 276 | |
| 277 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | unix_file_t template = { 0 }; |
| 279 | unix_main_t *um = &unix_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | |
| 281 | template.read_function = linux_pci_uio_read_ready; |
| 282 | template.file_descriptor = l->uio_fd; |
| 283 | template.error_function = linux_pci_uio_error_ready; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 284 | template.private_data = dev - pm->pci_devs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
| 286 | l->unix_file_index = unix_file_add (um, &template); |
| 287 | } |
| 288 | } |
| 289 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 290 | static void |
| 291 | linux_pci_device_free (linux_pci_device_t * l) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | { |
| 293 | int i; |
| 294 | for (i = 0; i < vec_len (l->resource_fds); i++) |
| 295 | if (l->resource_fds[i] > 0) |
| 296 | close (l->resource_fds[i]); |
| 297 | if (l->config_fd > 0) |
| 298 | close (l->config_fd); |
| 299 | if (l->uio_fd > 0) |
| 300 | close (l->uio_fd); |
| 301 | vec_free (l->resource_fds); |
| 302 | vec_free (l->dev_dir_name); |
| 303 | } |
| 304 | |
| 305 | /* Configuration space read/write. */ |
| 306 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 307 | vlib_pci_read_write_config (vlib_pci_device_t * dev, |
| 308 | vlib_read_or_write_t read_or_write, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 309 | uword address, void *data, u32 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 311 | linux_pci_main_t *lpm = &linux_pci_main; |
| 312 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 313 | int n; |
| 314 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 315 | p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
| 317 | if (read_or_write == VLIB_READ) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 318 | n = pread (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | else |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 320 | n = pwrite (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
| 322 | if (n != n_bytes) |
| 323 | return clib_error_return_unix (0, "%s", |
| 324 | read_or_write == VLIB_READ |
| 325 | ? "read" : "write"); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static clib_error_t * |
| 331 | os_map_pci_resource_internal (uword os_handle, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 332 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 334 | linux_pci_main_t *pm = &linux_pci_main; |
| 335 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | struct stat stat_buf; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 337 | u8 *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 339 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 340 | int flags = MAP_SHARED; |
| 341 | |
| 342 | error = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 343 | p = pool_elt_at_index (pm->linux_pci_devices, os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | |
| 345 | file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0); |
| 346 | fd = open ((char *) file_name, O_RDWR); |
| 347 | if (fd < 0) |
| 348 | { |
| 349 | error = clib_error_return_unix (0, "open `%s'", file_name); |
| 350 | goto done; |
| 351 | } |
| 352 | |
| 353 | if (fstat (fd, &stat_buf) < 0) |
| 354 | { |
| 355 | error = clib_error_return_unix (0, "fstat `%s'", file_name); |
| 356 | goto done; |
| 357 | } |
| 358 | |
| 359 | vec_validate (p->resource_fds, resource); |
| 360 | p->resource_fds[resource] = fd; |
| 361 | if (addr != 0) |
| 362 | flags |= MAP_FIXED; |
| 363 | |
| 364 | *result = mmap (addr, |
| 365 | /* size */ stat_buf.st_size, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 366 | PROT_READ | PROT_WRITE, flags, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | /* file */ fd, |
| 368 | /* offset */ 0); |
| 369 | if (*result == (void *) -1) |
| 370 | { |
| 371 | error = clib_error_return_unix (0, "mmap `%s'", file_name); |
| 372 | goto done; |
| 373 | } |
| 374 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 375 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | if (error) |
| 377 | { |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 378 | if (fd >= 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | close (fd); |
| 380 | } |
| 381 | vec_free (file_name); |
| 382 | return error; |
| 383 | } |
| 384 | |
| 385 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 386 | 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] | 387 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 388 | return (os_map_pci_resource_internal |
| 389 | (dev->os_handle, resource, 0 /* addr */ , |
| 390 | result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 394 | vlib_pci_map_resource_fixed (vlib_pci_device_t * dev, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 395 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 397 | return (os_map_pci_resource_internal |
| 398 | (dev->os_handle, resource, addr, result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 401 | void |
| 402 | vlib_pci_free_device (vlib_pci_device_t * dev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 404 | linux_pci_main_t *pm = &linux_pci_main; |
| 405 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 407 | l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | linux_pci_device_free (l); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 409 | pool_put (pm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 412 | pci_device_registration_t * __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | pci_device_next_registered (pci_device_registration_t * r) |
| 414 | { |
| 415 | uword i; |
| 416 | |
| 417 | /* Null vendor id marks end of initialized list. */ |
| 418 | for (i = 0; r->supported_devices[i].vendor_id != 0; i++) |
| 419 | ; |
| 420 | |
| 421 | return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0])); |
| 422 | } |
| 423 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | static clib_error_t * |
| 425 | init_device_from_registered (vlib_main_t * vm, |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 426 | vlib_pci_device_t * dev, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | linux_pci_device_t * pdev) |
| 428 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 429 | vlib_pci_main_t *pm = &pci_main; |
| 430 | pci_device_registration_t *r; |
| 431 | pci_device_id_t *i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 432 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 434 | r = pm->pci_device_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | |
| 436 | while (r) |
| 437 | { |
| 438 | for (i = r->supported_devices; i->vendor_id != 0; i++) |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 439 | 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] | 440 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 441 | error = vlib_pci_bind_to_uio (dev, "uio_pci_generic"); |
| 442 | if (error) |
| 443 | { |
| 444 | clib_error_report (error); |
| 445 | continue; |
| 446 | } |
| 447 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 448 | add_device (dev, pdev); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 449 | dev->interrupt_handler = r->interrupt_handler; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 450 | return r->init_function (vm, dev); |
| 451 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | r = r->next_registration; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 453 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | /* No driver, close the PCI config-space FD */ |
| 455 | close (pdev->config_fd); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static clib_error_t * |
| 460 | init_device (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 461 | vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | { |
| 463 | return init_device_from_registered (vm, dev, pdev); |
| 464 | } |
| 465 | |
| 466 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 467 | scan_device (void *arg, u8 * dev_dir_name, u8 * ignored) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 468 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 469 | vlib_main_t *vm = arg; |
| 470 | vlib_pci_main_t *pm = &pci_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 472 | u8 *f; |
| 473 | clib_error_t *error = 0; |
| 474 | vlib_pci_device_t *dev; |
| 475 | linux_pci_device_t pdev = { 0 }; |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 476 | u32 tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | |
| 478 | f = format (0, "%v/config%c", dev_dir_name, 0); |
| 479 | fd = open ((char *) f, O_RDWR); |
| 480 | |
| 481 | /* Try read-only access if write fails. */ |
| 482 | if (fd < 0) |
| 483 | fd = open ((char *) f, O_RDONLY); |
| 484 | |
| 485 | if (fd < 0) |
| 486 | { |
| 487 | error = clib_error_return_unix (0, "open `%s'", f); |
| 488 | goto done; |
| 489 | } |
| 490 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 491 | pool_get (pm->pci_devs, dev); |
| 492 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | /* You can only read more that 64 bytes of config space as root; so we try to |
| 494 | 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] | 495 | if (read (fd, &dev->config_data, sizeof (dev->config_data)) != |
| 496 | sizeof (dev->config_data) |
| 497 | && read (fd, &dev->config0, |
| 498 | sizeof (dev->config0)) != sizeof (dev->config0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 500 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | error = clib_error_return_unix (0, "read `%s'", f); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 502 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | goto done; |
| 504 | } |
| 505 | |
| 506 | { |
| 507 | static pci_config_header_t all_ones; |
| 508 | if (all_ones.vendor_id == 0) |
| 509 | memset (&all_ones, ~0, sizeof (all_ones)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 510 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 511 | if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 513 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | error = clib_error_return (0, "invalid PCI config for `%s'", f); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 515 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | goto done; |
| 517 | } |
| 518 | } |
| 519 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 520 | if (dev->config0.header.header_type == 0) |
| 521 | pci_config_type0_little_to_host (&dev->config0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | else |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 523 | pci_config_type1_little_to_host (&dev->config1); |
| 524 | |
| 525 | /* Parse bus, dev, function from directory name. */ |
| 526 | { |
| 527 | unformat_input_t input; |
| 528 | |
| 529 | unformat_init_string (&input, (char *) dev_dir_name, |
| 530 | vec_len (dev_dir_name)); |
| 531 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 532 | if (!unformat (&input, "/sys/bus/pci/devices/%U", |
| 533 | unformat_vlib_pci_addr, &dev->bus_address)) |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 534 | abort (); |
| 535 | |
| 536 | unformat_free (&input); |
| 537 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | |
| 541 | pdev.config_fd = fd; |
| 542 | pdev.dev_dir_name = dev_dir_name; |
| 543 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 544 | hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32, |
| 545 | dev - pm->pci_devs); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 546 | |
| 547 | error = init_device (vm, dev, &pdev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 549 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 550 | f = format (f, "%v/vpd%c", dev_dir_name, 0); |
| 551 | fd = open ((char *) f, O_RDONLY); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 552 | if (fd >= 0) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 553 | { |
| 554 | while (1) |
| 555 | { |
| 556 | u8 tag[3]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 557 | u8 *data = 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 558 | int len; |
| 559 | |
| 560 | if (read (fd, &tag, 3) != 3) |
| 561 | break; |
| 562 | |
| 563 | if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91) |
| 564 | break; |
| 565 | |
| 566 | len = (tag[2] << 8) | tag[1]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 567 | vec_validate (data, len); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 568 | |
| 569 | if (read (fd, data, len) != len) |
| 570 | { |
| 571 | vec_free (data); |
| 572 | break; |
| 573 | } |
| 574 | if (tag[0] == 0x82) |
| 575 | dev->product_name = data; |
| 576 | else if (tag[0] == 0x90) |
| 577 | dev->vpd_r = data; |
| 578 | else if (tag[0] == 0x91) |
| 579 | dev->vpd_w = data; |
| 580 | |
| 581 | data = 0; |
| 582 | } |
| 583 | close (fd); |
| 584 | } |
| 585 | |
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/driver%c", dev_dir_name, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 588 | dev->driver_name = vlib_sysfs_link_to_name ((char *) f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 589 | |
| 590 | dev->numa_node = -1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 591 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 592 | f = format (f, "%v/numa_node%c", dev_dir_name, 0); |
| 593 | vlib_sysfs_read ((char *) f, "%u", &dev->numa_node); |
| 594 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 595 | vec_reset_length (f); |
| 596 | f = format (f, "%v/class%c", dev_dir_name, 0); |
| 597 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 598 | dev->device_class = tmp >> 8; |
| 599 | |
| 600 | vec_reset_length (f); |
| 601 | f = format (f, "%v/vendor%c", dev_dir_name, 0); |
| 602 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 603 | dev->vendor_id = tmp; |
| 604 | |
| 605 | vec_reset_length (f); |
| 606 | f = format (f, "%v/device%c", dev_dir_name, 0); |
| 607 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 608 | dev->device_id = tmp; |
| 609 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 610 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 611 | vec_free (f); |
| 612 | return error; |
| 613 | } |
| 614 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 615 | clib_error_t * |
| 616 | linux_pci_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 618 | vlib_pci_main_t *pm = &pci_main; |
| 619 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | |
| 621 | pm->vlib_main = vm; |
| 622 | |
| 623 | if ((error = vlib_call_init_function (vm, unix_input_init))) |
| 624 | return error; |
| 625 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 626 | ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 627 | pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword)); |
| 628 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 629 | error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, |
| 630 | /* scan_dirs */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | |
| 632 | /* Complain and continue. might not be root, etc. */ |
| 633 | if (error) |
| 634 | clib_error_report (error); |
| 635 | |
| 636 | return error; |
| 637 | } |
| 638 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 639 | VLIB_INIT_FUNCTION (linux_pci_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 640 | |
| 641 | /* |
| 642 | * fd.io coding-style-patch-verification: ON |
| 643 | * |
| 644 | * Local Variables: |
| 645 | * eval: (c-set-style "gnu") |
| 646 | * End: |
| 647 | */ |