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