]> mj.ucw.cz Git - pciutils.git/blob - lib/sysfs.c
65f58bfd7ed6bf2d799eacd9473d4aa5acd475b6
[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       d->domain = dom;
200       d->bus = bus;
201       d->dev = dev;
202       d->func = func;
203       if (!a->buscentric)
204         {
205           sysfs_get_resources(d);
206           d->irq = sysfs_get_value(d, "irq", 1);
207           /*
208            *  We could read these faster from the config registers, but we want to give
209            *  the kernel a chance to fix up ID's and especially classes of broken devices.
210            */
211           d->vendor_id = sysfs_get_value(d, "vendor", 1);
212           d->device_id = sysfs_get_value(d, "device", 1);
213           d->device_class = sysfs_get_value(d, "class", 1) >> 8;
214           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;
215         }
216       pci_link_dev(a, d);
217     }
218   closedir(dir);
219 }
220
221 static void
222 sysfs_fill_slots(struct pci_access *a)
223 {
224   char dirname[1024];
225   DIR *dir;
226   struct dirent *entry;
227   int n;
228
229   n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
230   if (n < 0 || n >= (int) sizeof(dirname))
231     a->error("Directory name too long");
232   dir = opendir(dirname);
233   if (!dir)
234     return;
235
236   while (entry = readdir(dir))
237     {
238       char namebuf[OBJNAMELEN], buf[16];
239       FILE *file;
240       unsigned int dom, bus, dev;
241       int res = 0;
242       struct pci_dev *d;
243
244       /* ".", ".." or a special non-device perhaps */
245       if (entry->d_name[0] == '.')
246         continue;
247
248       n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
249       if (n < 0 || n >= OBJNAMELEN)
250         a->error("File name too long");
251       file = fopen(namebuf, "r");
252       /*
253        * Old versions of Linux had a fakephp which didn't have an 'address'
254        * file.  There's no useful information to be gleaned from these
255        * devices, pretend they're not there.
256        */
257       if (!file)
258         continue;
259
260       if (!fgets(buf, sizeof(buf), file) || (res = sscanf(buf, "%x:%x:%x", &dom, &bus, &dev)) < 3)
261         {
262           /*
263            * In some cases, the slot is not tied to a specific device before
264            * a card gets inserted. This happens for example on IBM pSeries
265            * and we need not warn about it.
266            */
267           if (res != 2)
268             a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
269         }
270       else
271         {
272           for (d = a->devices; d; d = d->next)
273             if (dom == d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
274               d->phy_slot = pci_strdup(a, entry->d_name);
275         }
276       fclose(file);
277     }
278   closedir(dir);
279 }
280
281 static int
282 sysfs_fill_info(struct pci_dev *d, int flags)
283 {
284   if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT))
285     {
286       struct pci_dev *pd;
287       sysfs_fill_slots(d->access);
288       for (pd = d->access->devices; pd; pd = pd->next)
289         pd->known_fields |= PCI_FILL_PHYS_SLOT;
290     }
291
292   if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS))
293     {
294       char buf[OBJBUFSIZE];
295       if (sysfs_get_string(d, "modalias", buf, 0))
296         d->module_alias = pci_strdup(d->access, buf);
297     }
298
299   if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL))
300     {
301       char buf[OBJBUFSIZE];
302       if (sysfs_get_string(d, "label", buf, 0))
303         d->label = pci_strdup(d->access, buf);
304     }
305
306   if ((flags & PCI_FILL_NUMA_NODE) && !(d->known_fields & PCI_FILL_NUMA_NODE))
307     d->numa_node = sysfs_get_value(d, "numa_node", 0);
308
309   return pci_generic_fill_info(d, flags);
310 }
311
312 /* Intent of the sysfs_setup() caller */
313 enum
314   {
315     SETUP_READ_CONFIG = 0,
316     SETUP_WRITE_CONFIG = 1,
317     SETUP_READ_VPD = 2
318   };
319
320 static int
321 sysfs_setup(struct pci_dev *d, int intent)
322 {
323   struct pci_access *a = d->access;
324   char namebuf[OBJNAMELEN];
325
326   if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
327     {
328       sysfs_flush_cache(a);
329       a->cached_dev = d;
330     }
331
332   if (intent == SETUP_READ_VPD)
333     {
334       if (a->fd_vpd < 0)
335         {
336           sysfs_obj_name(d, "vpd", namebuf);
337           a->fd_vpd = open(namebuf, O_RDONLY);
338           /* No warning on error; vpd may be absent or accessible only to root */
339         }
340       return a->fd_vpd;
341     }
342
343   if (a->fd < 0)
344     {
345       sysfs_obj_name(d, "config", namebuf);
346       a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
347       a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
348       if (a->fd < 0)
349         a->warning("Cannot open %s", namebuf);
350       a->fd_pos = 0;
351     }
352   return a->fd;
353 }
354
355 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
356 {
357   int fd = sysfs_setup(d, SETUP_READ_CONFIG);
358   int res;
359
360   if (fd < 0)
361     return 0;
362   res = do_read(d, fd, buf, len, pos);
363   if (res < 0)
364     {
365       d->access->warning("sysfs_read: read failed: %s", strerror(errno));
366       return 0;
367     }
368   else if (res != len)
369     return 0;
370   return 1;
371 }
372
373 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
374 {
375   int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
376   int res;
377
378   if (fd < 0)
379     return 0;
380   res = do_write(d, fd, buf, len, pos);
381   if (res < 0)
382     {
383       d->access->warning("sysfs_write: write failed: %s", strerror(errno));
384       return 0;
385     }
386   else if (res != len)
387     {
388       d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
389       return 0;
390     }
391   return 1;
392 }
393
394 #ifdef PCI_HAVE_DO_READ
395
396 /* pread() is not available and do_read() only works for a single fd, so we
397  * cannot implement read_vpd properly. */
398 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
399 {
400   return 0;
401 }
402
403 #else /* !PCI_HAVE_DO_READ */
404
405 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
406 {
407   int fd = sysfs_setup(d, SETUP_READ_VPD);
408   int res;
409
410   if (fd < 0)
411     return 0;
412   res = pread(fd, buf, len, pos);
413   if (res < 0)
414     {
415       d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
416       return 0;
417     }
418   else if (res != len)
419     return 0;
420   return 1;
421 }
422
423 #endif /* PCI_HAVE_DO_READ */
424
425 static void sysfs_cleanup_dev(struct pci_dev *d)
426 {
427   struct pci_access *a = d->access;
428
429   if (a->cached_dev == d)
430     sysfs_flush_cache(a);
431 }
432
433 struct pci_methods pm_linux_sysfs = {
434   "linux-sysfs",
435   "The sys filesystem on Linux",
436   sysfs_config,
437   sysfs_detect,
438   sysfs_init,
439   sysfs_cleanup,
440   sysfs_scan,
441   sysfs_fill_info,
442   sysfs_read,
443   sysfs_write,
444   sysfs_read_vpd,
445   NULL,                                 /* init_dev */
446   sysfs_cleanup_dev
447 };