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 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 40 | #include <vppinfra/linux/sysfs.h> |
| 41 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #include <vlib/vlib.h> |
| 43 | #include <vlib/pci/pci.h> |
| 44 | #include <vlib/unix/unix.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
| 46 | #include <sys/types.h> |
| 47 | #include <sys/stat.h> |
| 48 | #include <fcntl.h> |
| 49 | #include <dirent.h> |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 50 | #include <sys/ioctl.h> |
| 51 | #include <net/if.h> |
| 52 | #include <linux/ethtool.h> |
| 53 | #include <linux/sockios.h> |
| 54 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 55 | typedef struct |
| 56 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 57 | /* /sys/bus/pci/devices/... directory name for this device. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | u8 *dev_dir_name; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 59 | |
| 60 | /* Resource file descriptors. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 61 | int *resource_fds; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 62 | |
| 63 | /* File descriptor for config space read/write. */ |
| 64 | int config_fd; |
| 65 | |
| 66 | /* File descriptor for /dev/uio%d */ |
| 67 | int uio_fd; |
| 68 | |
| 69 | /* Minor device for uio device. */ |
| 70 | u32 uio_minor; |
| 71 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 72 | /* Index given by clib_file_add. */ |
| 73 | u32 clib_file_index; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 74 | |
| 75 | } linux_pci_device_t; |
| 76 | |
| 77 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 78 | typedef struct |
| 79 | { |
| 80 | vlib_main_t *vlib_main; |
| 81 | linux_pci_device_t *linux_pci_devices; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 82 | } linux_pci_main_t; |
| 83 | |
| 84 | extern linux_pci_main_t linux_pci_main; |
| 85 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 86 | /* Call to allocate/initialize the pci subsystem. |
| 87 | This is not an init function so that users can explicitly enable |
| 88 | pci only when it's needed. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | clib_error_t *pci_bus_init (vlib_main_t * vm); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 90 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 91 | clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d, |
| 92 | char *uio_driver_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
| 94 | linux_pci_main_t linux_pci_main; |
| 95 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 96 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | 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] | 98 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 99 | clib_error_t *error = 0; |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 100 | u8 *s = 0, *driver_name = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 101 | DIR *dir = 0; |
| 102 | struct dirent *e; |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 103 | int fd, clear_driver_override = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 104 | u8 *dev_dir_name = format (0, "/sys/bus/pci/devices/%U", |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 105 | format_vlib_pci_addr, &d->bus_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 107 | s = format (s, "%v/driver%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 108 | driver_name = clib_sysfs_link_to_name ((char *) s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 109 | vec_reset_length (s); |
| 110 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 111 | if (driver_name && |
| 112 | ((strcmp ("vfio-pci", (char *) driver_name) == 0) || |
| 113 | (strcmp ("uio_pci_generic", (char *) driver_name) == 0) || |
| 114 | (strcmp ("igb_uio", (char *) driver_name) == 0))) |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 115 | goto done; |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 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 | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 187 | clib_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 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 190 | s = format (s, "%v/driver_override%c", dev_dir_name, 0); |
| 191 | if (access ((char *) s, F_OK) == 0) |
| 192 | { |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 193 | clib_sysfs_write ((char *) s, "%s", uio_driver_name); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 194 | clear_driver_override = 1; |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | vec_reset_length (s); |
| 199 | s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 200 | clib_sysfs_write ((char *) s, "0x%04x 0x%04x", d->vendor_id, |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 201 | d->device_id); |
| 202 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 203 | vec_reset_length (s); |
| 204 | |
| 205 | s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 206 | clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 207 | vec_reset_length (s); |
| 208 | |
| 209 | if (clear_driver_override) |
| 210 | { |
| 211 | s = format (s, "%v/driver_override%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 212 | clib_sysfs_write ((char *) s, "%c", 0); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 213 | vec_reset_length (s); |
| 214 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 215 | |
| 216 | done: |
| 217 | closedir (dir); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | vec_free (s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 219 | vec_free (dev_dir_name); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 220 | vec_free (driver_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | return error; |
| 222 | } |
| 223 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
| 225 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | scan_uio_dir (void *arg, u8 * path_name, u8 * file_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 228 | linux_pci_device_t *l = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | unformat_input_t input; |
| 230 | |
| 231 | unformat_init_string (&input, (char *) file_name, vec_len (file_name)); |
| 232 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 233 | if (!unformat (&input, "uio%d", &l->uio_minor)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | abort (); |
| 235 | |
| 236 | unformat_free (&input); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 240 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 241 | linux_pci_uio_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 243 | vlib_pci_main_t *pm = &pci_main; |
| 244 | vlib_pci_device_t *d; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 245 | int __attribute__ ((unused)) rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 247 | u32 icount; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 248 | rv = read (uf->file_descriptor, &icount, 4); |
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 | d = pool_elt_at_index (pm->pci_devs, uf->private_data); |
| 251 | |
| 252 | if (d->interrupt_handler) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 253 | d->interrupt_handler (d); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 254 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 255 | vlib_pci_intr_enable (d); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
| 257 | return /* no error */ 0; |
| 258 | } |
| 259 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 260 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 261 | linux_pci_uio_error_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | { |
| 263 | u32 error_index = (u32) uf->private_data; |
| 264 | |
| 265 | return clib_error_return (0, "pci device %d: error", error_index); |
| 266 | } |
| 267 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 268 | static void |
| 269 | add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 271 | vlib_pci_main_t *pm = &pci_main; |
| 272 | linux_pci_main_t *lpm = &linux_pci_main; |
| 273 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 275 | pool_get (lpm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | l[0] = pdev[0]; |
| 277 | |
| 278 | l->dev_dir_name = vec_dup (l->dev_dir_name); |
| 279 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 280 | dev->os_handle = l - lpm->linux_pci_devices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | |
| 282 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 283 | u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name); |
| 284 | foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ |
| 285 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | vec_free (uio_dir); |
| 287 | } |
| 288 | |
| 289 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 290 | 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] | 291 | l->uio_fd = open (uio_name, O_RDWR); |
| 292 | if (l->uio_fd < 0) |
| 293 | clib_unix_error ("open `%s'", uio_name); |
| 294 | vec_free (uio_name); |
| 295 | } |
| 296 | |
| 297 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 298 | clib_file_t template = { 0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
| 300 | template.read_function = linux_pci_uio_read_ready; |
| 301 | template.file_descriptor = l->uio_fd; |
| 302 | template.error_function = linux_pci_uio_error_ready; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 303 | template.private_data = dev - pm->pci_devs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 305 | l->clib_file_index = clib_file_add (&file_main, &template); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 309 | static void |
| 310 | linux_pci_device_free (linux_pci_device_t * l) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | { |
| 312 | int i; |
| 313 | for (i = 0; i < vec_len (l->resource_fds); i++) |
| 314 | if (l->resource_fds[i] > 0) |
| 315 | close (l->resource_fds[i]); |
| 316 | if (l->config_fd > 0) |
| 317 | close (l->config_fd); |
| 318 | if (l->uio_fd > 0) |
| 319 | close (l->uio_fd); |
| 320 | vec_free (l->resource_fds); |
| 321 | vec_free (l->dev_dir_name); |
| 322 | } |
| 323 | |
| 324 | /* Configuration space read/write. */ |
| 325 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 326 | vlib_pci_read_write_config (vlib_pci_device_t * dev, |
| 327 | vlib_read_or_write_t read_or_write, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 328 | uword address, void *data, u32 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 330 | linux_pci_main_t *lpm = &linux_pci_main; |
| 331 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | int n; |
| 333 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 334 | p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
| 336 | if (read_or_write == VLIB_READ) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 337 | n = pread (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | else |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 339 | n = pwrite (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 340 | |
| 341 | if (n != n_bytes) |
| 342 | return clib_error_return_unix (0, "%s", |
| 343 | read_or_write == VLIB_READ |
| 344 | ? "read" : "write"); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static clib_error_t * |
| 350 | os_map_pci_resource_internal (uword os_handle, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 351 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 353 | linux_pci_main_t *pm = &linux_pci_main; |
| 354 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 355 | struct stat stat_buf; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 356 | u8 *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 358 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | int flags = MAP_SHARED; |
| 360 | |
| 361 | error = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 362 | p = pool_elt_at_index (pm->linux_pci_devices, os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | |
| 364 | file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0); |
| 365 | fd = open ((char *) file_name, O_RDWR); |
| 366 | if (fd < 0) |
| 367 | { |
| 368 | error = clib_error_return_unix (0, "open `%s'", file_name); |
| 369 | goto done; |
| 370 | } |
| 371 | |
| 372 | if (fstat (fd, &stat_buf) < 0) |
| 373 | { |
| 374 | error = clib_error_return_unix (0, "fstat `%s'", file_name); |
| 375 | goto done; |
| 376 | } |
| 377 | |
| 378 | vec_validate (p->resource_fds, resource); |
| 379 | p->resource_fds[resource] = fd; |
| 380 | if (addr != 0) |
| 381 | flags |= MAP_FIXED; |
| 382 | |
| 383 | *result = mmap (addr, |
| 384 | /* size */ stat_buf.st_size, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 385 | PROT_READ | PROT_WRITE, flags, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | /* file */ fd, |
| 387 | /* offset */ 0); |
| 388 | if (*result == (void *) -1) |
| 389 | { |
| 390 | error = clib_error_return_unix (0, "mmap `%s'", file_name); |
| 391 | goto done; |
| 392 | } |
| 393 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 394 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | if (error) |
| 396 | { |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 397 | if (fd >= 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | close (fd); |
| 399 | } |
| 400 | vec_free (file_name); |
| 401 | return error; |
| 402 | } |
| 403 | |
| 404 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 405 | 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] | 406 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 407 | return (os_map_pci_resource_internal |
| 408 | (dev->os_handle, resource, 0 /* addr */ , |
| 409 | result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 413 | vlib_pci_map_resource_fixed (vlib_pci_device_t * dev, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 414 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 416 | return (os_map_pci_resource_internal |
| 417 | (dev->os_handle, resource, addr, result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 420 | void |
| 421 | vlib_pci_free_device (vlib_pci_device_t * dev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 423 | linux_pci_main_t *pm = &linux_pci_main; |
| 424 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 426 | l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | linux_pci_device_free (l); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 428 | pool_put (pm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 431 | pci_device_registration_t * __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | pci_device_next_registered (pci_device_registration_t * r) |
| 433 | { |
| 434 | uword i; |
| 435 | |
| 436 | /* Null vendor id marks end of initialized list. */ |
| 437 | for (i = 0; r->supported_devices[i].vendor_id != 0; i++) |
| 438 | ; |
| 439 | |
| 440 | return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0])); |
| 441 | } |
| 442 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | static clib_error_t * |
| 444 | init_device_from_registered (vlib_main_t * vm, |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 445 | vlib_pci_device_t * dev, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | linux_pci_device_t * pdev) |
| 447 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 448 | vlib_pci_main_t *pm = &pci_main; |
| 449 | pci_device_registration_t *r; |
| 450 | pci_device_id_t *i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 451 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 453 | r = pm->pci_device_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | |
| 455 | while (r) |
| 456 | { |
| 457 | for (i = r->supported_devices; i->vendor_id != 0; i++) |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 458 | 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] | 459 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 460 | error = vlib_pci_bind_to_uio (dev, "uio_pci_generic"); |
| 461 | if (error) |
| 462 | { |
| 463 | clib_error_report (error); |
| 464 | continue; |
| 465 | } |
| 466 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 467 | add_device (dev, pdev); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 468 | dev->interrupt_handler = r->interrupt_handler; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 469 | return r->init_function (vm, dev); |
| 470 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | r = r->next_registration; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 472 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | /* No driver, close the PCI config-space FD */ |
| 474 | close (pdev->config_fd); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static clib_error_t * |
| 479 | init_device (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 480 | vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | { |
| 482 | return init_device_from_registered (vm, dev, pdev); |
| 483 | } |
| 484 | |
| 485 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 486 | scan_device (void *arg, u8 * dev_dir_name, u8 * ignored) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 488 | vlib_main_t *vm = arg; |
| 489 | vlib_pci_main_t *pm = &pci_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 491 | u8 *f; |
| 492 | clib_error_t *error = 0; |
| 493 | vlib_pci_device_t *dev; |
| 494 | linux_pci_device_t pdev = { 0 }; |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 495 | u32 tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | |
| 497 | f = format (0, "%v/config%c", dev_dir_name, 0); |
| 498 | fd = open ((char *) f, O_RDWR); |
| 499 | |
| 500 | /* Try read-only access if write fails. */ |
| 501 | if (fd < 0) |
| 502 | fd = open ((char *) f, O_RDONLY); |
| 503 | |
| 504 | if (fd < 0) |
| 505 | { |
| 506 | error = clib_error_return_unix (0, "open `%s'", f); |
| 507 | goto done; |
| 508 | } |
| 509 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 510 | pool_get (pm->pci_devs, dev); |
| 511 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | /* You can only read more that 64 bytes of config space as root; so we try to |
| 513 | 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] | 514 | if (read (fd, &dev->config_data, sizeof (dev->config_data)) != |
| 515 | sizeof (dev->config_data) |
| 516 | && read (fd, &dev->config0, |
| 517 | sizeof (dev->config0)) != sizeof (dev->config0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 519 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | error = clib_error_return_unix (0, "read `%s'", f); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 521 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | goto done; |
| 523 | } |
| 524 | |
| 525 | { |
| 526 | static pci_config_header_t all_ones; |
| 527 | if (all_ones.vendor_id == 0) |
| 528 | memset (&all_ones, ~0, sizeof (all_ones)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 529 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 530 | if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 532 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | error = clib_error_return (0, "invalid PCI config for `%s'", f); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 534 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | goto done; |
| 536 | } |
| 537 | } |
| 538 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 539 | if (dev->config0.header.header_type == 0) |
| 540 | pci_config_type0_little_to_host (&dev->config0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | else |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 542 | pci_config_type1_little_to_host (&dev->config1); |
| 543 | |
| 544 | /* Parse bus, dev, function from directory name. */ |
| 545 | { |
| 546 | unformat_input_t input; |
| 547 | |
| 548 | unformat_init_string (&input, (char *) dev_dir_name, |
| 549 | vec_len (dev_dir_name)); |
| 550 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 551 | if (!unformat (&input, "/sys/bus/pci/devices/%U", |
| 552 | unformat_vlib_pci_addr, &dev->bus_address)) |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 553 | abort (); |
| 554 | |
| 555 | unformat_free (&input); |
| 556 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | |
| 560 | pdev.config_fd = fd; |
| 561 | pdev.dev_dir_name = dev_dir_name; |
| 562 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 563 | hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32, |
| 564 | dev - pm->pci_devs); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 565 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 566 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 567 | f = format (f, "%v/vpd%c", dev_dir_name, 0); |
| 568 | fd = open ((char *) f, O_RDONLY); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 569 | if (fd >= 0) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 570 | { |
| 571 | while (1) |
| 572 | { |
| 573 | u8 tag[3]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 574 | u8 *data = 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 575 | int len; |
| 576 | |
| 577 | if (read (fd, &tag, 3) != 3) |
| 578 | break; |
| 579 | |
| 580 | if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91) |
| 581 | break; |
| 582 | |
| 583 | len = (tag[2] << 8) | tag[1]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 584 | vec_validate (data, len); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 585 | |
| 586 | if (read (fd, data, len) != len) |
| 587 | { |
| 588 | vec_free (data); |
| 589 | break; |
| 590 | } |
| 591 | if (tag[0] == 0x82) |
| 592 | dev->product_name = data; |
| 593 | else if (tag[0] == 0x90) |
| 594 | dev->vpd_r = data; |
| 595 | else if (tag[0] == 0x91) |
| 596 | dev->vpd_w = data; |
| 597 | |
| 598 | data = 0; |
| 599 | } |
| 600 | close (fd); |
| 601 | } |
| 602 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 603 | dev->numa_node = -1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 604 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 605 | f = format (f, "%v/numa_node%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 606 | clib_sysfs_read ((char *) f, "%u", &dev->numa_node); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 607 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 608 | vec_reset_length (f); |
| 609 | f = format (f, "%v/class%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 610 | clib_sysfs_read ((char *) f, "0x%x", &tmp); |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 611 | dev->device_class = tmp >> 8; |
| 612 | |
| 613 | vec_reset_length (f); |
| 614 | f = format (f, "%v/vendor%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 615 | clib_sysfs_read ((char *) f, "0x%x", &tmp); |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 616 | dev->vendor_id = tmp; |
| 617 | |
| 618 | vec_reset_length (f); |
| 619 | f = format (f, "%v/device%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 620 | clib_sysfs_read ((char *) f, "0x%x", &tmp); |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 621 | dev->device_id = tmp; |
| 622 | |
Damjan Marion | 191a1a4 | 2017-07-10 15:38:21 +0200 | [diff] [blame] | 623 | error = init_device (vm, dev, &pdev); |
| 624 | |
| 625 | vec_reset_length (f); |
| 626 | f = format (f, "%v/driver%c", dev_dir_name, 0); |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 627 | dev->driver_name = clib_sysfs_link_to_name ((char *) f); |
Damjan Marion | 191a1a4 | 2017-07-10 15:38:21 +0200 | [diff] [blame] | 628 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 629 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | vec_free (f); |
| 631 | return error; |
| 632 | } |
| 633 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 634 | clib_error_t * |
| 635 | linux_pci_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 637 | vlib_pci_main_t *pm = &pci_main; |
| 638 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 639 | |
| 640 | pm->vlib_main = vm; |
| 641 | |
| 642 | if ((error = vlib_call_init_function (vm, unix_input_init))) |
| 643 | return error; |
| 644 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 645 | ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 646 | pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword)); |
| 647 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 648 | error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, |
| 649 | /* scan_dirs */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | |
| 651 | /* Complain and continue. might not be root, etc. */ |
| 652 | if (error) |
| 653 | clib_error_report (error); |
| 654 | |
| 655 | return error; |
| 656 | } |
| 657 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 658 | VLIB_INIT_FUNCTION (linux_pci_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * fd.io coding-style-patch-verification: ON |
| 662 | * |
| 663 | * Local Variables: |
| 664 | * eval: (c-set-style "gnu") |
| 665 | * End: |
| 666 | */ |