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