1 /* 2 * The PCI Library -- Configuration Access via /sys/bus/pci 3 * 4 * Copyright (c) 2003 Matthew Wilcox <[email protected]> 5 * Copyright (c) 1997--2008 Martin Mares <[email protected]> 6 * 7 * Can be freely distributed and used under the terms of the GNU GPL. 8 */ 9 10 #define _GNU_SOURCE 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <stdarg.h> 16 #include <unistd.h> 17 #include <errno.h> 18 #include <dirent.h> 19 #include <fcntl.h> 20 #include <sys/types.h> 21 22 #include "internal.h" 23 #include "pread.h" 24 25 static void 26 sysfs_config(struct pci_access *a) 27 { 28 pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree"); 29 } 30 31 static inline char * 32 sysfs_name(struct pci_access *a) 33 { 34 return pci_get_param(a, "sysfs.path"); 35 } 36 37 static int 38 sysfs_detect(struct pci_access *a) 39 { 40 if (access(sysfs_name(a), R_OK)) 41 { 42 a->debug("...cannot open %s", sysfs_name(a)); 43 return 0; 44 } 45 a->debug("...using %s", sysfs_name(a)); 46 return 1; 47 } 48 49 static void 50 sysfs_init(struct pci_access *a) 51 { 52 a->fd = -1; 53 a->fd_vpd = -1; 54 } 55 56 static void 57 sysfs_flush_cache(struct pci_access *a) 58 { 59 if (a->fd >= 0) 60 { 61 close(a->fd); 62 a->fd = -1; 63 } 64 if (a->fd_vpd >= 0) 65 { 66 close(a->fd_vpd); 67 a->fd_vpd = -1; 68 } 69 a->cached_dev = NULL; 70 } 71 72 static void 73 sysfs_cleanup(struct pci_access *a) 74 { 75 sysfs_flush_cache(a); 76 } 77 78 #define OBJNAMELEN 1024 79 static void 80 sysfs_obj_name(struct pci_dev *d, char *object, char *buf) 81 { 82 int n = snprintf(buf, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d/%s", 83 sysfs_name(d->access), d->domain, d->bus, d->dev, d->func, object); 84 if (n < 0 || n >= OBJNAMELEN) 85 d->access->error("File name too long"); 86 } 87 88 #define OBJBUFSIZE 1024 89 90 static int 91 sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory) 92 { 93 struct pci_access *a = d->access; 94 int fd, n; 95 char namebuf[OBJNAMELEN]; 96 void (*warn)(char *msg, ...) = (mandatory ? a->error : a->warning); 97 98 sysfs_obj_name(d, object, namebuf); 99 fd = open(namebuf, O_RDONLY); 100 if (fd < 0) 101 { 102 if (mandatory || errno != ENOENT) 103 warn("Cannot open %s: %s", namebuf, strerror(errno)); 104 return 0; 105 } 106 n = read(fd, buf, OBJBUFSIZE); 107 close(fd); 108 if (n < 0) 109 { 110 warn("Error reading %s: %s", namebuf, strerror(errno)); 111 return 0; 112 } 113 if (n >= OBJBUFSIZE) 114 { 115 warn("Value in %s too long", namebuf); 116 return 0; 117 } 118 buf[n] = 0; 119 return 1; 120 } 121 122 static char * 123 sysfs_deref_link(struct pci_dev *d, char *link_name) 124 { 125 char path[2*OBJNAMELEN], rel_path[OBJNAMELEN]; 126 127 sysfs_obj_name(d, link_name, path); 128 memset(rel_path, 0, sizeof(rel_path)); 129 130 if (readlink(path, rel_path, sizeof(rel_path)) < 0) 131 return NULL; 132 133 sysfs_obj_name(d, "", path); 134 strcat(path, rel_path); 135 136 return canonicalize_file_name(path); 137 } 138 139 static int 140 sysfs_get_value(struct pci_dev *d, char *object, int mandatory) 141 { 142 char buf[OBJBUFSIZE]; 143 144 if (sysfs_get_string(d, object, buf, mandatory)) 145 return strtol(buf, NULL, 0); 146 else 147 return -1; 148 } 149 150 static void 151 sysfs_get_resources(struct pci_dev *d) 152 { 153 struct pci_access *a = d->access; 154 char namebuf[OBJNAMELEN], buf[256]; 155 FILE *file; 156 int i; 157 158 sysfs_obj_name(d, "resource", namebuf); 159 file = fopen(namebuf, "r"); 160 if (!file) 161 a->error("Cannot open %s: %s", namebuf, strerror(errno)); 162 for (i = 0; i < 7; i++) 163 { 164 unsigned long long start, end, size, flags; 165 if (!fgets(buf, sizeof(buf), file)) 166 break; 167 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3) 168 a->error("Syntax error in %s", namebuf); 169 if (end > start) 170 size = end - start + 1; 171 else 172 size = 0; 173 if (i < 6) 174 { 175 d->flags[i] = flags; 176 flags &= PCI_ADDR_FLAG_MASK; 177 d->base_addr[i] = start | flags; 178 d->size[i] = size; 179 } 180 else 181 { 182 d->rom_flags = flags; 183 flags &= PCI_ADDR_FLAG_MASK; 184 d->rom_base_addr = start | flags; 185 d->rom_size = size; 186 } 187 } 188 fclose(file); 189 } 190 191 static void sysfs_scan(struct pci_access *a) 192 { 193 char dirname[1024]; 194 DIR *dir; 195 struct dirent *entry; 196 int n; 197 198 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a)); 199 if (n < 0 || n >= (int) sizeof(dirname)) 200 a->error("Directory name too long"); 201 dir = opendir(dirname); 202 if (!dir) 203 a->error("Cannot open %s", dirname); 204 while ((entry = readdir(dir))) 205 { 206 struct pci_dev *d; 207 unsigned int dom, bus, dev, func; 208 209 /* ".", ".." or a special non-device perhaps */ 210 if (entry->d_name[0] == '.') 211 continue; 212 213 d = pci_alloc_dev(a); 214 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4) 215 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name); 216 217 /* Ensure kernel provided domain that fits in a signed integer */ 218 if (dom > 0x7fffffff) 219 a->error("sysfs_scan: Invalid domain %x", dom); 220 221 d->domain = dom; 222 d->bus = bus; 223 d->dev = dev; 224 d->func = func; 225 if (!a->buscentric) 226 { 227 sysfs_get_resources(d); 228 d->irq = sysfs_get_value(d, "irq", 1); 229 /* 230 * We could read these faster from the config registers, but we want to give 231 * the kernel a chance to fix up ID's and especially classes of broken devices. 232 */ 233 d->vendor_id = sysfs_get_value(d, "vendor", 1); 234 d->device_id = sysfs_get_value(d, "device", 1); 235 d->device_class = sysfs_get_value(d, "class", 1) >> 8; 236 d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS; 237 } 238 pci_link_dev(a, d); 239 } 240 closedir(dir); 241 } 242 243 static void 244 sysfs_fill_slots(struct pci_access *a) 245 { 246 char dirname[1024]; 247 DIR *dir; 248 struct dirent *entry; 249 int n; 250 251 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a)); 252 if (n < 0 || n >= (int) sizeof(dirname)) 253 a->error("Directory name too long"); 254 dir = opendir(dirname); 255 if (!dir) 256 return; 257 258 while (entry = readdir(dir)) 259 { 260 char namebuf[OBJNAMELEN], buf[16]; 261 FILE *file; 262 unsigned int dom, bus, dev; 263 int res = 0; 264 struct pci_dev *d; 265 266 /* ".", ".." or a special non-device perhaps */ 267 if (entry->d_name[0] == '.') 268 continue; 269 270 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address"); 271 if (n < 0 || n >= OBJNAMELEN) 272 a->error("File name too long"); 273 file = fopen(namebuf, "r"); 274 /* 275 * Old versions of Linux had a fakephp which didn't have an 'address' 276 * file. There's no useful information to be gleaned from these 277 * devices, pretend they're not there. 278 */ 279 if (!file) 280 continue; 281 282 if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3) 283 { 284 /* 285 * In some cases, the slot is not tied to a specific device before 286 * a card gets inserted. This happens for example on IBM pSeries 287 * and we need not warn about it. 288 */ 289 if (res != 2) 290 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf); 291 } 292 else 293 { 294 for (d = a->devices; d; d = d->next) 295 if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot) 296 d->phy_slot = pci_set_property(d, PCI_FILL_PHYS_SLOT, entry->d_name); 297 } 298 fclose(file); 299 } 300 closedir(dir); 301 } 302 303 static int 304 sysfs_fill_info(struct pci_dev *d, int flags) 305 { 306 if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT)) 307 { 308 struct pci_dev *pd; 309 sysfs_fill_slots(d->access); 310 for (pd = d->access->devices; pd; pd = pd->next) 311 pd->known_fields |= PCI_FILL_PHYS_SLOT; 312 } 313 314 if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS)) 315 { 316 char buf[OBJBUFSIZE]; 317 if (sysfs_get_string(d, "modalias", buf, 0)) 318 d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf); 319 } 320 321 if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL)) 322 { 323 char buf[OBJBUFSIZE]; 324 if (sysfs_get_string(d, "label", buf, 0)) 325 d->label = pci_set_property(d, PCI_FILL_LABEL, buf); 326 } 327 328 if ((flags & PCI_FILL_NUMA_NODE) && !(d->known_fields & PCI_FILL_NUMA_NODE)) 329 d->numa_node = sysfs_get_value(d, "numa_node", 0); 330 331 if ((flags & PCI_FILL_DT_NODE) && !(d->known_fields & PCI_FILL_DT_NODE)) 332 pci_set_property(d, PCI_FILL_DT_NODE, sysfs_deref_link(d, "of_node")); 333 334 return pci_generic_fill_info(d, flags); 335 } 336 337 /* Intent of the sysfs_setup() caller */ 338 enum 339 { 340 SETUP_READ_CONFIG = 0, 341 SETUP_WRITE_CONFIG = 1, 342 SETUP_READ_VPD = 2 343 }; 344 345 static int 346 sysfs_setup(struct pci_dev *d, int intent) 347 { 348 struct pci_access *a = d->access; 349 char namebuf[OBJNAMELEN]; 350 351 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw)) 352 { 353 sysfs_flush_cache(a); 354 a->cached_dev = d; 355 } 356 357 if (intent == SETUP_READ_VPD) 358 { 359 if (a->fd_vpd < 0) 360 { 361 sysfs_obj_name(d, "vpd", namebuf); 362 a->fd_vpd = open(namebuf, O_RDONLY); 363 /* No warning on error; vpd may be absent or accessible only to root */ 364 } 365 return a->fd_vpd; 366 } 367 368 if (a->fd < 0) 369 { 370 sysfs_obj_name(d, "config", namebuf); 371 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG; 372 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY); 373 if (a->fd < 0) 374 a->warning("Cannot open %s", namebuf); 375 a->fd_pos = 0; 376 } 377 return a->fd; 378 } 379 380 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len) 381 { 382 int fd = sysfs_setup(d, SETUP_READ_CONFIG); 383 int res; 384 385 if (fd < 0) 386 return 0; 387 res = do_read(d, fd, buf, len, pos); 388 if (res < 0) 389 { 390 d->access->warning("sysfs_read: read failed: %s", strerror(errno)); 391 return 0; 392 } 393 else if (res != len) 394 return 0; 395 return 1; 396 } 397 398 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len) 399 { 400 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG); 401 int res; 402 403 if (fd < 0) 404 return 0; 405 res = do_write(d, fd, buf, len, pos); 406 if (res < 0) 407 { 408 d->access->warning("sysfs_write: write failed: %s", strerror(errno)); 409 return 0; 410 } 411 else if (res != len) 412 { 413 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res); 414 return 0; 415 } 416 return 1; 417 } 418 419 #ifdef PCI_HAVE_DO_READ 420 421 /* pread() is not available and do_read() only works for a single fd, so we 422 * cannot implement read_vpd properly. */ 423 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len) 424 { 425 return 0; 426 } 427 428 #else /* !PCI_HAVE_DO_READ */ 429 430 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len) 431 { 432 int fd = sysfs_setup(d, SETUP_READ_VPD); 433 int res; 434 435 if (fd < 0) 436 return 0; 437 res = pread(fd, buf, len, pos); 438 if (res < 0) 439 { 440 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno)); 441 return 0; 442 } 443 else if (res != len) 444 return 0; 445 return 1; 446 } 447 448 #endif /* PCI_HAVE_DO_READ */ 449 450 static void sysfs_cleanup_dev(struct pci_dev *d) 451 { 452 struct pci_access *a = d->access; 453 454 if (a->cached_dev == d) 455 sysfs_flush_cache(a); 456 } 457 458 struct pci_methods pm_linux_sysfs = { 459 "linux-sysfs", 460 "The sys filesystem on Linux", 461 sysfs_config, 462 sysfs_detect, 463 sysfs_init, 464 sysfs_cleanup, 465 sysfs_scan, 466 sysfs_fill_info, 467 sysfs_read, 468 sysfs_write, 469 sysfs_read_vpd, 470 NULL, /* init_dev */ 471 sysfs_cleanup_dev 472 }; 473