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