]> mj.ucw.cz Git - pciutils.git/blob - lib/sysfs.c
164d6da89dbec182649eb002f5647833c93c13d8
[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       flags &= PCI_ADDR_FLAG_MASK;
157       if (i < 6)
158         {
159           d->base_addr[i] = start | flags;
160           d->size[i] = size;
161         }
162       else
163         {
164           d->rom_base_addr = start | flags;
165           d->rom_size = size;
166         }
167     }
168   fclose(file);
169 }
170
171 static void sysfs_scan(struct pci_access *a)
172 {
173   char dirname[1024];
174   DIR *dir;
175   struct dirent *entry;
176   int n;
177
178   n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
179   if (n < 0 || n >= (int) sizeof(dirname))
180     a->error("Directory name too long");
181   dir = opendir(dirname);
182   if (!dir)
183     a->error("Cannot open %s", dirname);
184   while ((entry = readdir(dir)))
185     {
186       struct pci_dev *d;
187       unsigned int dom, bus, dev, func;
188
189       /* ".", ".." or a special non-device perhaps */
190       if (entry->d_name[0] == '.')
191         continue;
192
193       d = pci_alloc_dev(a);
194       if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
195         a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
196
197       /* Ensure kernel provided domain that fits in a signed integer */
198       if (dom > 0x7fffffff)
199         a->error("sysfs_scan: invalid domain:%x", dom);
200
201       /*
202        * The domain value is truncated to 16 bits and stored in the pci_dev
203        * structure's legacy 16-bit domain offset for compatibility with
204        * applications compiled with libpci pre-32 bit domains. Such
205        * applications may not work as expected if they are on a machine
206        * utilizing PCI domain numbers requiring more than 16 bits.
207        */
208       d->domain_16 = dom;
209       d->domain = dom;
210       d->bus = bus;
211       d->dev = dev;
212       d->func = func;
213       if (!a->buscentric)
214         {
215           sysfs_get_resources(d);
216           d->irq = sysfs_get_value(d, "irq", 1);
217           /*
218            *  We could read these faster from the config registers, but we want to give
219            *  the kernel a chance to fix up ID's and especially classes of broken devices.
220            */
221           d->vendor_id = sysfs_get_value(d, "vendor", 1);
222           d->device_id = sysfs_get_value(d, "device", 1);
223           d->device_class = sysfs_get_value(d, "class", 1) >> 8;
224           d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES;
225         }
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_strdup(a, entry->d_name);
285         }
286       fclose(file);
287     }
288   closedir(dir);
289 }
290
291 static int
292 sysfs_fill_info(struct pci_dev *d, int flags)
293 {
294   if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT))
295     {
296       struct pci_dev *pd;
297       sysfs_fill_slots(d->access);
298       for (pd = d->access->devices; pd; pd = pd->next)
299         pd->known_fields |= PCI_FILL_PHYS_SLOT;
300     }
301
302   if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS))
303     {
304       char buf[OBJBUFSIZE];
305       if (sysfs_get_string(d, "modalias", buf, 0))
306         d->module_alias = pci_strdup(d->access, buf);
307     }
308
309   if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL))
310     {
311       char buf[OBJBUFSIZE];
312       if (sysfs_get_string(d, "label", buf, 0))
313         d->label = pci_strdup(d->access, buf);
314     }
315
316   if ((flags & PCI_FILL_NUMA_NODE) && !(d->known_fields & PCI_FILL_NUMA_NODE))
317     d->numa_node = sysfs_get_value(d, "numa_node", 0);
318
319   return pci_generic_fill_info(d, flags);
320 }
321
322 /* Intent of the sysfs_setup() caller */
323 enum
324   {
325     SETUP_READ_CONFIG = 0,
326     SETUP_WRITE_CONFIG = 1,
327     SETUP_READ_VPD = 2
328   };
329
330 static int
331 sysfs_setup(struct pci_dev *d, int intent)
332 {
333   struct pci_access *a = d->access;
334   char namebuf[OBJNAMELEN];
335
336   if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
337     {
338       sysfs_flush_cache(a);
339       a->cached_dev = d;
340     }
341
342   if (intent == SETUP_READ_VPD)
343     {
344       if (a->fd_vpd < 0)
345         {
346           sysfs_obj_name(d, "vpd", namebuf);
347           a->fd_vpd = open(namebuf, O_RDONLY);
348           /* No warning on error; vpd may be absent or accessible only to root */
349         }
350       return a->fd_vpd;
351     }
352
353   if (a->fd < 0)
354     {
355       sysfs_obj_name(d, "config", namebuf);
356       a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
357       a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
358       if (a->fd < 0)
359         a->warning("Cannot open %s", namebuf);
360       a->fd_pos = 0;
361     }
362   return a->fd;
363 }
364
365 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
366 {
367   int fd = sysfs_setup(d, SETUP_READ_CONFIG);
368   int res;
369
370   if (fd < 0)
371     return 0;
372   res = do_read(d, fd, buf, len, pos);
373   if (res < 0)
374     {
375       d->access->warning("sysfs_read: read failed: %s", strerror(errno));
376       return 0;
377     }
378   else if (res != len)
379     return 0;
380   return 1;
381 }
382
383 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
384 {
385   int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
386   int res;
387
388   if (fd < 0)
389     return 0;
390   res = do_write(d, fd, buf, len, pos);
391   if (res < 0)
392     {
393       d->access->warning("sysfs_write: write failed: %s", strerror(errno));
394       return 0;
395     }
396   else if (res != len)
397     {
398       d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
399       return 0;
400     }
401   return 1;
402 }
403
404 #ifdef PCI_HAVE_DO_READ
405
406 /* pread() is not available and do_read() only works for a single fd, so we
407  * cannot implement read_vpd properly. */
408 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
409 {
410   return 0;
411 }
412
413 #else /* !PCI_HAVE_DO_READ */
414
415 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
416 {
417   int fd = sysfs_setup(d, SETUP_READ_VPD);
418   int res;
419
420   if (fd < 0)
421     return 0;
422   res = pread(fd, buf, len, pos);
423   if (res < 0)
424     {
425       d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
426       return 0;
427     }
428   else if (res != len)
429     return 0;
430   return 1;
431 }
432
433 #endif /* PCI_HAVE_DO_READ */
434
435 static void sysfs_cleanup_dev(struct pci_dev *d)
436 {
437   struct pci_access *a = d->access;
438
439   if (a->cached_dev == d)
440     sysfs_flush_cache(a);
441 }
442
443 struct pci_methods pm_linux_sysfs = {
444   "linux-sysfs",
445   "The sys filesystem on Linux",
446   sysfs_config,
447   sysfs_detect,
448   sysfs_init,
449   sysfs_cleanup,
450   sysfs_scan,
451   sysfs_fill_info,
452   sysfs_read,
453   sysfs_write,
454   sysfs_read_vpd,
455   NULL,                                 /* init_dev */
456   sysfs_cleanup_dev
457 };