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