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