]> mj.ucw.cz Git - pciutils.git/blob - lib/sysfs.c
877310c584352c6b8bb3f355054bea056cf09333
[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 int
123 sysfs_get_value(struct pci_dev *d, char *object, int mandatory)
124 {
125   char buf[OBJBUFSIZE];
126
127   if (sysfs_get_string(d, object, buf, mandatory))
128     return strtol(buf, NULL, 0);
129   else
130     return -1;
131 }
132
133 static void
134 sysfs_get_resources(struct pci_dev *d)
135 {
136   struct pci_access *a = d->access;
137   char namebuf[OBJNAMELEN], buf[256];
138   FILE *file;
139   int i;
140
141   sysfs_obj_name(d, "resource", namebuf);
142   file = fopen(namebuf, "r");
143   if (!file)
144     a->error("Cannot open %s: %s", namebuf, strerror(errno));
145   for (i = 0; i < 7; i++)
146     {
147       unsigned long long start, end, size, flags;
148       if (!fgets(buf, sizeof(buf), file))
149         break;
150       if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
151         a->error("Syntax error in %s", namebuf);
152       if (end > start)
153         size = end - start + 1;
154       else
155         size = 0;
156       if (i < 6)
157         {
158           d->flags[i] = flags;
159           flags &= PCI_ADDR_FLAG_MASK;
160           d->base_addr[i] = start | flags;
161           d->size[i] = size;
162         }
163       else
164         {
165           d->rom_flags = flags;
166           flags &= PCI_ADDR_FLAG_MASK;
167           d->rom_base_addr = start | flags;
168           d->rom_size = size;
169         }
170     }
171   fclose(file);
172 }
173
174 static void sysfs_scan(struct pci_access *a)
175 {
176   char dirname[1024];
177   DIR *dir;
178   struct dirent *entry;
179   int n;
180
181   n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
182   if (n < 0 || n >= (int) sizeof(dirname))
183     a->error("Directory name too long");
184   dir = opendir(dirname);
185   if (!dir)
186     a->error("Cannot open %s", dirname);
187   while ((entry = readdir(dir)))
188     {
189       struct pci_dev *d;
190       unsigned int dom, bus, dev, func;
191
192       /* ".", ".." or a special non-device perhaps */
193       if (entry->d_name[0] == '.')
194         continue;
195
196       d = pci_alloc_dev(a);
197       if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
198         a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
199
200       /* Ensure kernel provided domain that fits in a signed integer */
201       if (dom > 0x7fffffff)
202         a->error("sysfs_scan: invalid domain:%x", dom);
203
204       /*
205        * The domain value is truncated to 16 bits and stored in the pci_dev
206        * structure's legacy 16-bit domain offset for compatibility with
207        * applications compiled with libpci pre-32 bit domains. Such
208        * applications may not work as expected if they are on a machine
209        * utilizing PCI domain numbers requiring more than 16 bits.
210        */
211       d->domain_16 = dom;
212       d->domain = dom;
213       d->bus = bus;
214       d->dev = dev;
215       d->func = func;
216       if (!a->buscentric)
217         {
218           sysfs_get_resources(d);
219           d->irq = sysfs_get_value(d, "irq", 1);
220           /*
221            *  We could read these faster from the config registers, but we want to give
222            *  the kernel a chance to fix up ID's and especially classes of broken devices.
223            */
224           d->vendor_id = sysfs_get_value(d, "vendor", 1);
225           d->device_id = sysfs_get_value(d, "device", 1);
226           d->device_class = sysfs_get_value(d, "class", 1) >> 8;
227           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;
228         }
229       pci_link_dev(a, d);
230     }
231   closedir(dir);
232 }
233
234 static void
235 sysfs_fill_slots(struct pci_access *a)
236 {
237   char dirname[1024];
238   DIR *dir;
239   struct dirent *entry;
240   int n;
241
242   n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
243   if (n < 0 || n >= (int) sizeof(dirname))
244     a->error("Directory name too long");
245   dir = opendir(dirname);
246   if (!dir)
247     return;
248
249   while (entry = readdir(dir))
250     {
251       char namebuf[OBJNAMELEN], buf[16];
252       FILE *file;
253       unsigned int dom, bus, dev;
254       int res = 0;
255       struct pci_dev *d;
256
257       /* ".", ".." or a special non-device perhaps */
258       if (entry->d_name[0] == '.')
259         continue;
260
261       n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
262       if (n < 0 || n >= OBJNAMELEN)
263         a->error("File name too long");
264       file = fopen(namebuf, "r");
265       /*
266        * Old versions of Linux had a fakephp which didn't have an 'address'
267        * file.  There's no useful information to be gleaned from these
268        * devices, pretend they're not there.
269        */
270       if (!file)
271         continue;
272
273       if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
274         {
275           /*
276            * In some cases, the slot is not tied to a specific device before
277            * a card gets inserted. This happens for example on IBM pSeries
278            * and we need not warn about it.
279            */
280           if (res != 2)
281             a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
282         }
283       else
284         {
285           for (d = a->devices; d; d = d->next)
286             if (dom == (unsigned)d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
287               d->phy_slot = pci_strdup(a, entry->d_name);
288         }
289       fclose(file);
290     }
291   closedir(dir);
292 }
293
294 static int
295 sysfs_fill_info(struct pci_dev *d, int flags)
296 {
297   if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT))
298     {
299       struct pci_dev *pd;
300       sysfs_fill_slots(d->access);
301       for (pd = d->access->devices; pd; pd = pd->next)
302         pd->known_fields |= PCI_FILL_PHYS_SLOT;
303     }
304
305   if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS))
306     {
307       char buf[OBJBUFSIZE];
308       if (sysfs_get_string(d, "modalias", buf, 0))
309         d->module_alias = pci_strdup(d->access, buf);
310     }
311
312   if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL))
313     {
314       char buf[OBJBUFSIZE];
315       if (sysfs_get_string(d, "label", buf, 0))
316         d->label = pci_strdup(d->access, buf);
317     }
318
319   if ((flags & PCI_FILL_NUMA_NODE) && !(d->known_fields & PCI_FILL_NUMA_NODE))
320     d->numa_node = sysfs_get_value(d, "numa_node", 0);
321
322   return pci_generic_fill_info(d, flags);
323 }
324
325 /* Intent of the sysfs_setup() caller */
326 enum
327   {
328     SETUP_READ_CONFIG = 0,
329     SETUP_WRITE_CONFIG = 1,
330     SETUP_READ_VPD = 2
331   };
332
333 static int
334 sysfs_setup(struct pci_dev *d, int intent)
335 {
336   struct pci_access *a = d->access;
337   char namebuf[OBJNAMELEN];
338
339   if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
340     {
341       sysfs_flush_cache(a);
342       a->cached_dev = d;
343     }
344
345   if (intent == SETUP_READ_VPD)
346     {
347       if (a->fd_vpd < 0)
348         {
349           sysfs_obj_name(d, "vpd", namebuf);
350           a->fd_vpd = open(namebuf, O_RDONLY);
351           /* No warning on error; vpd may be absent or accessible only to root */
352         }
353       return a->fd_vpd;
354     }
355
356   if (a->fd < 0)
357     {
358       sysfs_obj_name(d, "config", namebuf);
359       a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
360       a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
361       if (a->fd < 0)
362         a->warning("Cannot open %s", namebuf);
363       a->fd_pos = 0;
364     }
365   return a->fd;
366 }
367
368 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
369 {
370   int fd = sysfs_setup(d, SETUP_READ_CONFIG);
371   int res;
372
373   if (fd < 0)
374     return 0;
375   res = do_read(d, fd, buf, len, pos);
376   if (res < 0)
377     {
378       d->access->warning("sysfs_read: read failed: %s", strerror(errno));
379       return 0;
380     }
381   else if (res != len)
382     return 0;
383   return 1;
384 }
385
386 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
387 {
388   int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
389   int res;
390
391   if (fd < 0)
392     return 0;
393   res = do_write(d, fd, buf, len, pos);
394   if (res < 0)
395     {
396       d->access->warning("sysfs_write: write failed: %s", strerror(errno));
397       return 0;
398     }
399   else if (res != len)
400     {
401       d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
402       return 0;
403     }
404   return 1;
405 }
406
407 #ifdef PCI_HAVE_DO_READ
408
409 /* pread() is not available and do_read() only works for a single fd, so we
410  * cannot implement read_vpd properly. */
411 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
412 {
413   return 0;
414 }
415
416 #else /* !PCI_HAVE_DO_READ */
417
418 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
419 {
420   int fd = sysfs_setup(d, SETUP_READ_VPD);
421   int res;
422
423   if (fd < 0)
424     return 0;
425   res = pread(fd, buf, len, pos);
426   if (res < 0)
427     {
428       d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
429       return 0;
430     }
431   else if (res != len)
432     return 0;
433   return 1;
434 }
435
436 #endif /* PCI_HAVE_DO_READ */
437
438 static void sysfs_cleanup_dev(struct pci_dev *d)
439 {
440   struct pci_access *a = d->access;
441
442   if (a->cached_dev == d)
443     sysfs_flush_cache(a);
444 }
445
446 struct pci_methods pm_linux_sysfs = {
447   "linux-sysfs",
448   "The sys filesystem on Linux",
449   sysfs_config,
450   sysfs_detect,
451   sysfs_init,
452   sysfs_cleanup,
453   sysfs_scan,
454   sysfs_fill_info,
455   sysfs_read,
456   sysfs_write,
457   sysfs_read_vpd,
458   NULL,                                 /* init_dev */
459   sysfs_cleanup_dev
460 };