]> mj.ucw.cz Git - pciutils.git/blob - lib/sysfs.c
fe3f0e5ece534369d833ba73ece7861b901fea07
[pciutils.git] / lib / sysfs.c
1 /*
2  *      The PCI Library -- Configuration Access via /sys/bus/pci
3  *
4  *      Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5  *      Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
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   // Returns a pointer to malloc'ed memory
137   return realpath(path, NULL);
138 }
139
140 static int
141 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
142 {
143   char buf[OBJBUFSIZE];
144
145   if (sysfs_get_string(d, object, buf, mandatory))
146     return strtol(buf, NULL, 0);
147   else
148     return -1;
149 }
150
151 static void
152 sysfs_get_resources(struct pci_dev *d)
153 {
154   struct pci_access *a = d->access;
155   char namebuf[OBJNAMELEN], buf[256];
156   struct { pciaddr_t flags, base_addr, size; } lines[10];
157   int have_bar_bases, have_rom_base, have_bridge_bases;
158   FILE *file;
159   int i;
160
161   have_bar_bases = have_rom_base = have_bridge_bases = 0;
162   sysfs_obj_name(d, "resource", namebuf);
163   file = fopen(namebuf, "r");
164   if (!file)
165     a->error("Cannot open %s: %s", namebuf, strerror(errno));
166   for (i = 0; i < 7+6+4+1; i++)
167     {
168       unsigned long long start, end, size, flags;
169       if (!fgets(buf, sizeof(buf), file))
170         break;
171       if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
172         a->error("Syntax error in %s", namebuf);
173       if (end > start)
174         size = end - start + 1;
175       else
176         size = 0;
177       if (i < 6)
178         {
179           d->flags[i] = flags;
180           flags &= PCI_ADDR_FLAG_MASK;
181           d->base_addr[i] = start | flags;
182           d->size[i] = size;
183           have_bar_bases = 1;
184         }
185       else if (i == 6)
186         {
187           d->rom_flags = flags;
188           flags &= PCI_ADDR_FLAG_MASK;
189           d->rom_base_addr = start | flags;
190           d->rom_size = size;
191           have_rom_base = 1;
192         }
193       else if (i < 7+6+4)
194         {
195           /*
196            * If kernel was compiled without CONFIG_PCI_IOV option then after
197            * the ROM line for configured bridge device (that which had set
198            * subordinary bus number to non-zero value) are four additional lines
199            * which describe resources behind bridge. For PCI-to-PCI bridges they
200            * are: IO, MEM, PREFMEM and empty. For CardBus bridges they are: IO0,
201            * IO1, MEM0 and MEM1. For unconfigured bridges and other devices
202            * there is no additional line after the ROM line. If kernel was
203            * compiled with CONFIG_PCI_IOV option then after the ROM line and
204            * before the first bridge resource line are six additional lines
205            * which describe IOV resources. Read all remaining lines in resource
206            * file and based on the number of remaining lines (0, 4, 6, 10) parse
207            * resources behind bridge.
208            */
209           lines[i-7].flags = flags;
210           lines[i-7].base_addr = start;
211           lines[i-7].size = size;
212         }
213     }
214   if (i == 7+4 || i == 7+6+4)
215     {
216       int offset = (i == 7+6+4) ? 6 : 0;
217       for (i = 0; i < 4; i++)
218         {
219           d->bridge_flags[i] = lines[offset+i].flags;
220           d->bridge_base_addr[i] = lines[offset+i].base_addr;
221           d->bridge_size[i] = lines[offset+i].size;
222         }
223       have_bridge_bases = 1;
224     }
225   fclose(file);
226   if (!have_bar_bases)
227     clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS);
228   if (!have_rom_base)
229     clear_fill(d, PCI_FILL_ROM_BASE);
230   if (!have_bridge_bases)
231     clear_fill(d, PCI_FILL_BRIDGE_BASES);
232 }
233
234 static void sysfs_scan(struct pci_access *a)
235 {
236   char dirname[1024];
237   DIR *dir;
238   struct dirent *entry;
239   int n;
240
241   n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
242   if (n < 0 || n >= (int) sizeof(dirname))
243     a->error("Directory name too long");
244   dir = opendir(dirname);
245   if (!dir)
246     a->error("Cannot open %s", dirname);
247   while ((entry = readdir(dir)))
248     {
249       struct pci_dev *d;
250       unsigned int dom, bus, dev, func;
251
252       /* ".", ".." or a special non-device perhaps */
253       if (entry->d_name[0] == '.')
254         continue;
255
256       d = pci_alloc_dev(a);
257       if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
258         a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
259
260       /* Ensure kernel provided domain that fits in a signed integer */
261       if (dom > 0x7fffffff)
262         a->error("sysfs_scan: Invalid domain %x", dom);
263
264       d->domain = dom;
265       d->bus = bus;
266       d->dev = dev;
267       d->func = func;
268       pci_link_dev(a, d);
269     }
270   closedir(dir);
271 }
272
273 static void
274 sysfs_fill_slots(struct pci_access *a)
275 {
276   char dirname[1024];
277   DIR *dir;
278   struct dirent *entry;
279   int n;
280
281   n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
282   if (n < 0 || n >= (int) sizeof(dirname))
283     a->error("Directory name too long");
284   dir = opendir(dirname);
285   if (!dir)
286     return;
287
288   while (entry = readdir(dir))
289     {
290       char namebuf[OBJNAMELEN], buf[16];
291       FILE *file;
292       unsigned int dom, bus, dev;
293       int res = 0;
294       struct pci_dev *d;
295
296       /* ".", ".." or a special non-device perhaps */
297       if (entry->d_name[0] == '.')
298         continue;
299
300       n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
301       if (n < 0 || n >= OBJNAMELEN)
302         a->error("File name too long");
303       file = fopen(namebuf, "r");
304       /*
305        * Old versions of Linux had a fakephp which didn't have an 'address'
306        * file.  There's no useful information to be gleaned from these
307        * devices, pretend they're not there.
308        */
309       if (!file)
310         continue;
311
312       if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
313         {
314           /*
315            * In some cases, the slot is not tied to a specific device before
316            * a card gets inserted. This happens for example on IBM pSeries
317            * and we need not warn about it.
318            */
319           if (res != 2)
320             a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
321         }
322       else
323         {
324           for (d = a->devices; d; d = d->next)
325             if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
326               d->phy_slot = pci_set_property(d, PCI_FILL_PHYS_SLOT, entry->d_name);
327         }
328       fclose(file);
329     }
330   closedir(dir);
331 }
332
333 static void
334 sysfs_fill_info(struct pci_dev *d, unsigned int flags)
335 {
336   if (!d->access->buscentric)
337     {
338       /*
339        *  These fields can be read from the config registers, but we want to show
340        *  the kernel's view, which has regions and IRQs remapped and other fields
341        *  (most importantly classes) possibly fixed if the device is known broken.
342        */
343       if (want_fill(d, flags, PCI_FILL_IDENT))
344         {
345           d->vendor_id = sysfs_get_value(d, "vendor", 1);
346           d->device_id = sysfs_get_value(d, "device", 1);
347         }
348       if (want_fill(d, flags, PCI_FILL_CLASS))
349         d->device_class = sysfs_get_value(d, "class", 1) >> 8;
350       if (want_fill(d, flags, PCI_FILL_IRQ))
351           d->irq = sysfs_get_value(d, "irq", 1);
352       if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_IO_FLAGS | PCI_FILL_BRIDGE_BASES))
353           sysfs_get_resources(d);
354     }
355
356   if (want_fill(d, flags, PCI_FILL_PHYS_SLOT))
357     {
358       struct pci_dev *pd;
359       sysfs_fill_slots(d->access);
360       for (pd = d->access->devices; pd; pd = pd->next)
361         pd->known_fields |= PCI_FILL_PHYS_SLOT;
362     }
363
364   if (want_fill(d, flags, PCI_FILL_MODULE_ALIAS))
365     {
366       char buf[OBJBUFSIZE];
367       if (sysfs_get_string(d, "modalias", buf, 0))
368         d->module_alias = pci_set_property(d, PCI_FILL_MODULE_ALIAS, buf);
369     }
370
371   if (want_fill(d, flags, PCI_FILL_LABEL))
372     {
373       char buf[OBJBUFSIZE];
374       if (sysfs_get_string(d, "label", buf, 0))
375         d->label = pci_set_property(d, PCI_FILL_LABEL, buf);
376     }
377
378   if (want_fill(d, flags, PCI_FILL_NUMA_NODE))
379     d->numa_node = sysfs_get_value(d, "numa_node", 0);
380
381   if (want_fill(d, flags, PCI_FILL_IOMMU_GROUP))
382     {
383       char *group_link = sysfs_deref_link(d, "iommu_group");
384       if (group_link)
385         {
386           pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
387           free(group_link);
388         }
389     }
390
391   if (want_fill(d, flags, PCI_FILL_DT_NODE))
392     {
393       char *node = sysfs_deref_link(d, "of_node");
394       if (node)
395         {
396           pci_set_property(d, PCI_FILL_DT_NODE, node);
397           free(node);
398         }
399     }
400
401   pci_generic_fill_info(d, flags);
402 }
403
404 /* Intent of the sysfs_setup() caller */
405 enum
406   {
407     SETUP_READ_CONFIG = 0,
408     SETUP_WRITE_CONFIG = 1,
409     SETUP_READ_VPD = 2
410   };
411
412 static int
413 sysfs_setup(struct pci_dev *d, int intent)
414 {
415   struct pci_access *a = d->access;
416   char namebuf[OBJNAMELEN];
417
418   if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
419     {
420       sysfs_flush_cache(a);
421       a->cached_dev = d;
422     }
423
424   if (intent == SETUP_READ_VPD)
425     {
426       if (a->fd_vpd < 0)
427         {
428           sysfs_obj_name(d, "vpd", namebuf);
429           a->fd_vpd = open(namebuf, O_RDONLY);
430           /* No warning on error; vpd may be absent or accessible only to root */
431         }
432       return a->fd_vpd;
433     }
434
435   if (a->fd < 0)
436     {
437       sysfs_obj_name(d, "config", namebuf);
438       a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
439       a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
440       if (a->fd < 0)
441         a->warning("Cannot open %s", namebuf);
442       a->fd_pos = 0;
443     }
444   return a->fd;
445 }
446
447 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
448 {
449   int fd = sysfs_setup(d, SETUP_READ_CONFIG);
450   int res;
451
452   if (fd < 0)
453     return 0;
454   res = do_read(d, fd, buf, len, pos);
455   if (res < 0)
456     {
457       d->access->warning("sysfs_read: read failed: %s", strerror(errno));
458       return 0;
459     }
460   else if (res != len)
461     return 0;
462   return 1;
463 }
464
465 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
466 {
467   int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
468   int res;
469
470   if (fd < 0)
471     return 0;
472   res = do_write(d, fd, buf, len, pos);
473   if (res < 0)
474     {
475       d->access->warning("sysfs_write: write failed: %s", strerror(errno));
476       return 0;
477     }
478   else if (res != len)
479     {
480       d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
481       return 0;
482     }
483   return 1;
484 }
485
486 #ifdef PCI_HAVE_DO_READ
487
488 /* pread() is not available and do_read() only works for a single fd, so we
489  * cannot implement read_vpd properly. */
490 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
491 {
492   return 0;
493 }
494
495 #else /* !PCI_HAVE_DO_READ */
496
497 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
498 {
499   int fd = sysfs_setup(d, SETUP_READ_VPD);
500   int res;
501
502   if (fd < 0)
503     return 0;
504   res = pread(fd, buf, len, pos);
505   if (res < 0)
506     {
507       d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
508       return 0;
509     }
510   else if (res != len)
511     return 0;
512   return 1;
513 }
514
515 #endif /* PCI_HAVE_DO_READ */
516
517 static void sysfs_cleanup_dev(struct pci_dev *d)
518 {
519   struct pci_access *a = d->access;
520
521   if (a->cached_dev == d)
522     sysfs_flush_cache(a);
523 }
524
525 struct pci_methods pm_linux_sysfs = {
526   "linux-sysfs",
527   "The sys filesystem on Linux",
528   sysfs_config,
529   sysfs_detect,
530   sysfs_init,
531   sysfs_cleanup,
532   sysfs_scan,
533   sysfs_fill_info,
534   sysfs_read,
535   sysfs_write,
536   sysfs_read_vpd,
537   NULL,                                 /* init_dev */
538   sysfs_cleanup_dev
539 };