blob: d448f8df8613086e401d2b674107a28056fd64a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-sysfs.c
3 *
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
10 *
11 * File attributes for PCI devices
12 *
13 * Modeled after usb's driverfs.c
14 *
15 */
16
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/pci.h>
20#include <linux/stat.h>
21#include <linux/topology.h>
22#include <linux/mm.h>
23
24#include "pci.h"
25
26static int sysfs_initialized; /* = 0 */
27
28/* show configuration fields */
29#define pci_config_attr(field, format_string) \
30static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040031field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{ \
33 struct pci_dev *pdev; \
34 \
35 pdev = to_pci_dev (dev); \
36 return sprintf (buf, format_string, pdev->field); \
37}
38
39pci_config_attr(vendor, "0x%04x\n");
40pci_config_attr(device, "0x%04x\n");
41pci_config_attr(subsystem_vendor, "0x%04x\n");
42pci_config_attr(subsystem_device, "0x%04x\n");
43pci_config_attr(class, "0x%06x\n");
44pci_config_attr(irq, "%u\n");
45
Doug Thompsonbdee9d92006-06-14 16:59:48 -070046static ssize_t broken_parity_status_show(struct device *dev,
47 struct device_attribute *attr,
48 char *buf)
49{
50 struct pci_dev *pdev = to_pci_dev(dev);
51 return sprintf (buf, "%u\n", pdev->broken_parity_status);
52}
53
54static ssize_t broken_parity_status_store(struct device *dev,
55 struct device_attribute *attr,
56 const char *buf, size_t count)
57{
58 struct pci_dev *pdev = to_pci_dev(dev);
59 ssize_t consumed = -EINVAL;
60
61 if ((count > 0) && (*buf == '0' || *buf == '1')) {
62 pdev->broken_parity_status = *buf == '1' ? 1 : 0;
63 consumed = count;
64 }
65 return consumed;
66}
67
Alan Cox4327edf2005-09-10 00:25:49 -070068static ssize_t local_cpus_show(struct device *dev,
69 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Alan Cox4327edf2005-09-10 00:25:49 -070071 cpumask_t mask;
72 int len;
73
74 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
75 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 strcat(buf,"\n");
77 return 1+len;
78}
79
80/* show resources */
81static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -040082resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct pci_dev * pci_dev = to_pci_dev(dev);
85 char * str = buf;
86 int i;
87 int max = 7;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070088 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (pci_dev->subordinate)
91 max = DEVICE_COUNT_RESOURCE;
92
93 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +100094 struct resource *res = &pci_dev->resource[i];
95 pci_resource_to_user(pci_dev, i, res, &start, &end);
96 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
97 (unsigned long long)start,
98 (unsigned long long)end,
99 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 return (str - buf);
102}
103
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200104static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700105{
106 struct pci_dev *pci_dev = to_pci_dev(dev);
107
108 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
109 pci_dev->vendor, pci_dev->device,
110 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
111 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
112 (u8)(pci_dev->class));
113}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800114
115static ssize_t is_enabled_store(struct device *dev,
116 struct device_attribute *attr, const char *buf,
117 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200118{
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800119 ssize_t result = -EINVAL;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200120 struct pci_dev *pdev = to_pci_dev(dev);
121
122 /* this can crash the machine when done on the "wrong" device */
123 if (!capable(CAP_SYS_ADMIN))
124 return count;
125
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800126 if (*buf == '0') {
127 if (atomic_read(&pdev->enable_cnt) != 0)
128 pci_disable_device(pdev);
129 else
130 result = -EIO;
131 } else if (*buf == '1')
132 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200133
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800134 return result < 0 ? result : count;
135}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200136
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800137static ssize_t is_enabled_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 struct pci_dev *pdev;
141
142 pdev = to_pci_dev (dev);
143 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200144}
145
Brice Goglin81bb0e12007-01-28 10:53:40 +0100146#ifdef CONFIG_NUMA
147static ssize_t
148numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
149{
150 return sprintf (buf, "%d\n", dev->numa_node);
151}
152#endif
153
Brice Goglinfe970642006-08-31 01:55:15 -0400154static ssize_t
155msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
156{
157 struct pci_dev *pdev = to_pci_dev(dev);
158
159 if (!pdev->subordinate)
160 return 0;
161
162 return sprintf (buf, "%u\n",
163 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
164}
165
166static ssize_t
167msi_bus_store(struct device *dev, struct device_attribute *attr,
168 const char *buf, size_t count)
169{
170 struct pci_dev *pdev = to_pci_dev(dev);
171
172 /* bad things may happen if the no_msi flag is changed
173 * while some drivers are loaded */
174 if (!capable(CAP_SYS_ADMIN))
175 return count;
176
177 if (!pdev->subordinate)
178 return count;
179
180 if (*buf == '0') {
181 pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
182 dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
183 " bad things could happen.\n");
184 }
185
186 if (*buf == '1') {
187 pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
188 dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
189 " bad things could happen.\n");
190 }
191
192 return count;
193}
Greg KH98885492005-05-05 11:57:25 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195struct device_attribute pci_dev_attrs[] = {
196 __ATTR_RO(resource),
197 __ATTR_RO(vendor),
198 __ATTR_RO(device),
199 __ATTR_RO(subsystem_vendor),
200 __ATTR_RO(subsystem_device),
201 __ATTR_RO(class),
202 __ATTR_RO(irq),
203 __ATTR_RO(local_cpus),
Greg KH98885492005-05-05 11:57:25 -0700204 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100205#ifdef CONFIG_NUMA
206 __ATTR_RO(numa_node),
207#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200208 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700209 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
210 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400211 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 __ATTR_NULL,
213};
214
215static ssize_t
216pci_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
217{
218 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
219 unsigned int size = 64;
220 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900221 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* Several chips lock up trying to read undefined config space */
224 if (capable(CAP_SYS_ADMIN)) {
225 size = dev->cfg_size;
226 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
227 size = 128;
228 }
229
230 if (off > size)
231 return 0;
232 if (off + count > size) {
233 size -= off;
234 count = size;
235 } else {
236 size = count;
237 }
238
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900239 if ((off & 1) && size) {
240 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700241 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900242 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900244 size--;
245 }
246
247 if ((off & 3) && size > 2) {
248 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700249 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900250 data[off - init_off] = val & 0xff;
251 data[off - init_off + 1] = (val >> 8) & 0xff;
252 off += 2;
253 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
256 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900257 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700258 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900259 data[off - init_off] = val & 0xff;
260 data[off - init_off + 1] = (val >> 8) & 0xff;
261 data[off - init_off + 2] = (val >> 16) & 0xff;
262 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 off += 4;
264 size -= 4;
265 }
266
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900267 if (size >= 2) {
268 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700269 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900270 data[off - init_off] = val & 0xff;
271 data[off - init_off + 1] = (val >> 8) & 0xff;
272 off += 2;
273 size -= 2;
274 }
275
276 if (size > 0) {
277 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700278 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900279 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 off++;
281 --size;
282 }
283
284 return count;
285}
286
287static ssize_t
288pci_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
289{
290 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
291 unsigned int size = count;
292 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900293 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (off > dev->cfg_size)
296 return 0;
297 if (off + count > dev->cfg_size) {
298 size = dev->cfg_size - off;
299 count = size;
300 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900301
302 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700303 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900305 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900307
308 if ((off & 3) && size > 2) {
309 u16 val = data[off - init_off];
310 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700311 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900312 off += 2;
313 size -= 2;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900317 u32 val = data[off - init_off];
318 val |= (u32) data[off - init_off + 1] << 8;
319 val |= (u32) data[off - init_off + 2] << 16;
320 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700321 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 off += 4;
323 size -= 4;
324 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900325
326 if (size >= 2) {
327 u16 val = data[off - init_off];
328 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700329 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900330 off += 2;
331 size -= 2;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900334 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700335 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 off++;
337 --size;
338 }
339
340 return count;
341}
342
343#ifdef HAVE_PCI_LEGACY
344/**
345 * pci_read_legacy_io - read byte(s) from legacy I/O port space
346 * @kobj: kobject corresponding to file to read from
347 * @buf: buffer to store results
348 * @off: offset into legacy I/O port space
349 * @count: number of bytes to read
350 *
351 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
352 * callback routine (pci_legacy_read).
353 */
354ssize_t
355pci_read_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
356{
357 struct pci_bus *bus = to_pci_bus(container_of(kobj,
358 struct class_device,
359 kobj));
360
361 /* Only support 1, 2 or 4 byte accesses */
362 if (count != 1 && count != 2 && count != 4)
363 return -EINVAL;
364
365 return pci_legacy_read(bus, off, (u32 *)buf, count);
366}
367
368/**
369 * pci_write_legacy_io - write byte(s) to legacy I/O port space
370 * @kobj: kobject corresponding to file to read from
371 * @buf: buffer containing value to be written
372 * @off: offset into legacy I/O port space
373 * @count: number of bytes to write
374 *
375 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
376 * callback routine (pci_legacy_write).
377 */
378ssize_t
379pci_write_legacy_io(struct kobject *kobj, char *buf, loff_t off, size_t count)
380{
381 struct pci_bus *bus = to_pci_bus(container_of(kobj,
382 struct class_device,
383 kobj));
384 /* Only support 1, 2 or 4 byte accesses */
385 if (count != 1 && count != 2 && count != 4)
386 return -EINVAL;
387
388 return pci_legacy_write(bus, off, *(u32 *)buf, count);
389}
390
391/**
392 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
393 * @kobj: kobject corresponding to device to be mapped
394 * @attr: struct bin_attribute for this file
395 * @vma: struct vm_area_struct passed to mmap
396 *
397 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
398 * legacy memory space (first meg of bus space) into application virtual
399 * memory space.
400 */
401int
402pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
403 struct vm_area_struct *vma)
404{
405 struct pci_bus *bus = to_pci_bus(container_of(kobj,
406 struct class_device,
407 kobj));
408
409 return pci_mmap_legacy_page_range(bus, vma);
410}
411#endif /* HAVE_PCI_LEGACY */
412
413#ifdef HAVE_PCI_MMAP
414/**
415 * pci_mmap_resource - map a PCI resource into user memory space
416 * @kobj: kobject for mapping
417 * @attr: struct bin_attribute for the file being mapped
418 * @vma: struct vm_area_struct passed into the mmap
419 *
420 * Use the regular PCI mapping routines to map a PCI resource into userspace.
421 * FIXME: write combining? maybe automatic for prefetchable regions?
422 */
423static int
424pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
425 struct vm_area_struct *vma)
426{
427 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
428 struct device, kobj));
429 struct resource *res = (struct resource *)attr->private;
430 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700431 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000432 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000434 for (i = 0; i < PCI_ROM_RESOURCE; i++)
435 if (res == &pdev->resource[i])
436 break;
437 if (i >= PCI_ROM_RESOURCE)
438 return -ENODEV;
439
440 /* pci_mmap_page_range() expects the same kind of entry as coming
441 * from /proc/bus/pci/ which is a "user visible" value. If this is
442 * different from the resource itself, arch will do necessary fixup.
443 */
444 pci_resource_to_user(pdev, i, res, &start, &end);
445 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
447
448 return pci_mmap_page_range(pdev, vma, mmap_type, 0);
449}
450
451/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700452 * pci_remove_resource_files - cleanup resource files
453 * @dev: dev to cleanup
454 *
455 * If we created resource files for @dev, remove them from sysfs and
456 * free their resources.
457 */
458static void
459pci_remove_resource_files(struct pci_dev *pdev)
460{
461 int i;
462
463 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
464 struct bin_attribute *res_attr;
465
466 res_attr = pdev->res_attr[i];
467 if (res_attr) {
468 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
469 kfree(res_attr);
470 }
471 }
472}
473
474/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * pci_create_resource_files - create resource files in sysfs for @dev
476 * @dev: dev in question
477 *
478 * Walk the resources in @dev creating files for each resource available.
479 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700480static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700483 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Expose the PCI resources from this device as files */
486 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
487 struct bin_attribute *res_attr;
488
489 /* skip empty resources */
490 if (!pci_resource_len(pdev, i))
491 continue;
492
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500493 /* allocate attribute structure, piggyback attribute name */
Pekka Enberg656da9d2005-09-22 00:48:11 -0700494 res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (res_attr) {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500496 char *res_attr_name = (char *)(res_attr + 1);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 pdev->res_attr[i] = res_attr;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500499 sprintf(res_attr_name, "resource%d", i);
500 res_attr->attr.name = res_attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 res_attr->attr.mode = S_IRUSR | S_IWUSR;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500502 res_attr->size = pci_resource_len(pdev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 res_attr->mmap = pci_mmap_resource;
504 res_attr->private = &pdev->resource[i];
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700505 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
506 if (retval) {
507 pci_remove_resource_files(pdev);
508 return retval;
509 }
510 } else {
511 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516#else /* !HAVE_PCI_MMAP */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700517static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
519#endif /* HAVE_PCI_MMAP */
520
521/**
522 * pci_write_rom - used to enable access to the PCI ROM display
523 * @kobj: kernel object handle
524 * @buf: user input
525 * @off: file offset
526 * @count: number of byte in input
527 *
528 * writing anything except 0 enables it
529 */
530static ssize_t
531pci_write_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
532{
533 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
534
535 if ((off == 0) && (*buf == '0') && (count == 2))
536 pdev->rom_attr_enabled = 0;
537 else
538 pdev->rom_attr_enabled = 1;
539
540 return count;
541}
542
543/**
544 * pci_read_rom - read a PCI ROM
545 * @kobj: kernel object handle
546 * @buf: where to put the data we read from the ROM
547 * @off: file offset
548 * @count: number of bytes to read
549 *
550 * Put @count bytes starting at @off into @buf from the ROM in the PCI
551 * device corresponding to @kobj.
552 */
553static ssize_t
554pci_read_rom(struct kobject *kobj, char *buf, loff_t off, size_t count)
555{
556 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
557 void __iomem *rom;
558 size_t size;
559
560 if (!pdev->rom_attr_enabled)
561 return -EINVAL;
562
563 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
564 if (!rom)
565 return 0;
566
567 if (off >= size)
568 count = 0;
569 else {
570 if (off + count > size)
571 count = size - off;
572
573 memcpy_fromio(buf, rom + off, count);
574 }
575 pci_unmap_rom(pdev, rom);
576
577 return count;
578}
579
580static struct bin_attribute pci_config_attr = {
581 .attr = {
582 .name = "config",
583 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 },
585 .size = 256,
586 .read = pci_read_config,
587 .write = pci_write_config,
588};
589
590static struct bin_attribute pcie_config_attr = {
591 .attr = {
592 .name = "config",
593 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 },
595 .size = 4096,
596 .read = pci_read_config,
597 .write = pci_write_config,
598};
599
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700600int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700602 struct bin_attribute *rom_attr = NULL;
603 int retval;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!sysfs_initialized)
606 return -EACCES;
607
608 if (pdev->cfg_size < 4096)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700609 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700611 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
612 if (retval)
613 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700615 retval = pci_create_resource_files(pdev);
616 if (retval)
617 goto err_bin_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* If the device has a ROM, try to expose it in sysfs. */
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700620 if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
621 (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100622 rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (rom_attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 pdev->rom_attr = rom_attr;
625 rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
626 rom_attr->attr.name = "rom";
627 rom_attr->attr.mode = S_IRUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 rom_attr->read = pci_read_rom;
629 rom_attr->write = pci_write_rom;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700630 retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
631 if (retval)
632 goto err_rom;
633 } else {
634 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +1000635 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 }
638 /* add platform-specific attributes */
639 pcibios_add_platform_entries(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700642
643err_rom:
644 kfree(rom_attr);
Michael Ellerman9890b122007-04-18 13:34:12 +1000645err_resource_files:
646 pci_remove_resource_files(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700647err_bin_file:
648 if (pdev->cfg_size < 4096)
649 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
650 else
651 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
652err:
653 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/**
657 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
658 * @pdev: device whose entries we should free
659 *
660 * Cleanup when @pdev is removed from sysfs.
661 */
662void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
663{
David Millerd67afe52006-11-10 12:27:48 -0800664 if (!sysfs_initialized)
665 return;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (pdev->cfg_size < 4096)
668 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
669 else
670 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
671
672 pci_remove_resource_files(pdev);
673
674 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
675 if (pdev->rom_attr) {
676 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
677 kfree(pdev->rom_attr);
678 }
679 }
680}
681
682static int __init pci_sysfs_init(void)
683{
684 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700685 int retval;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700688 for_each_pci_dev(pdev) {
689 retval = pci_create_sysfs_dev_files(pdev);
690 if (retval)
691 return retval;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 return 0;
695}
696
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700697late_initcall(pci_sysfs_init);