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