blob: abf4203304e405269dc111afbe19b02d1e14ff4d [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>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070023#include <linux/capability.h>
Shaohua Li6c723d52008-01-24 10:21:57 +080024#include <linux/aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "pci.h"
26
27static int sysfs_initialized; /* = 0 */
28
29/* show configuration fields */
30#define pci_config_attr(field, format_string) \
31static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040032field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{ \
34 struct pci_dev *pdev; \
35 \
36 pdev = to_pci_dev (dev); \
37 return sprintf (buf, format_string, pdev->field); \
38}
39
40pci_config_attr(vendor, "0x%04x\n");
41pci_config_attr(device, "0x%04x\n");
42pci_config_attr(subsystem_vendor, "0x%04x\n");
43pci_config_attr(subsystem_device, "0x%04x\n");
44pci_config_attr(class, "0x%06x\n");
45pci_config_attr(irq, "%u\n");
46
Doug Thompsonbdee9d92006-06-14 16:59:48 -070047static ssize_t broken_parity_status_show(struct device *dev,
48 struct device_attribute *attr,
49 char *buf)
50{
51 struct pci_dev *pdev = to_pci_dev(dev);
52 return sprintf (buf, "%u\n", pdev->broken_parity_status);
53}
54
55static ssize_t broken_parity_status_store(struct device *dev,
56 struct device_attribute *attr,
57 const char *buf, size_t count)
58{
59 struct pci_dev *pdev = to_pci_dev(dev);
60 ssize_t consumed = -EINVAL;
61
62 if ((count > 0) && (*buf == '0' || *buf == '1')) {
63 pdev->broken_parity_status = *buf == '1' ? 1 : 0;
64 consumed = count;
65 }
66 return consumed;
67}
68
Alan Cox4327edf2005-09-10 00:25:49 -070069static ssize_t local_cpus_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Alan Cox4327edf2005-09-10 00:25:49 -070072 cpumask_t mask;
73 int len;
74
75 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
76 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 strcat(buf,"\n");
78 return 1+len;
79}
80
81/* show resources */
82static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -040083resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct pci_dev * pci_dev = to_pci_dev(dev);
86 char * str = buf;
87 int i;
88 int max = 7;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070089 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (pci_dev->subordinate)
92 max = DEVICE_COUNT_RESOURCE;
93
94 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +100095 struct resource *res = &pci_dev->resource[i];
96 pci_resource_to_user(pci_dev, i, res, &start, &end);
97 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
98 (unsigned long long)start,
99 (unsigned long long)end,
100 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 return (str - buf);
103}
104
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200105static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700106{
107 struct pci_dev *pci_dev = to_pci_dev(dev);
108
109 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
110 pci_dev->vendor, pci_dev->device,
111 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
112 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
113 (u8)(pci_dev->class));
114}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800115
116static ssize_t is_enabled_store(struct device *dev,
117 struct device_attribute *attr, const char *buf,
118 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200119{
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800120 ssize_t result = -EINVAL;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200121 struct pci_dev *pdev = to_pci_dev(dev);
122
123 /* this can crash the machine when done on the "wrong" device */
124 if (!capable(CAP_SYS_ADMIN))
125 return count;
126
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800127 if (*buf == '0') {
128 if (atomic_read(&pdev->enable_cnt) != 0)
129 pci_disable_device(pdev);
130 else
131 result = -EIO;
132 } else if (*buf == '1')
133 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200134
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800135 return result < 0 ? result : count;
136}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200137
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800138static ssize_t is_enabled_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
140{
141 struct pci_dev *pdev;
142
143 pdev = to_pci_dev (dev);
144 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200145}
146
Brice Goglin81bb0e12007-01-28 10:53:40 +0100147#ifdef CONFIG_NUMA
148static ssize_t
149numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
150{
151 return sprintf (buf, "%d\n", dev->numa_node);
152}
153#endif
154
Brice Goglinfe970642006-08-31 01:55:15 -0400155static ssize_t
156msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
157{
158 struct pci_dev *pdev = to_pci_dev(dev);
159
160 if (!pdev->subordinate)
161 return 0;
162
163 return sprintf (buf, "%u\n",
164 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
165}
166
167static ssize_t
168msi_bus_store(struct device *dev, struct device_attribute *attr,
169 const char *buf, size_t count)
170{
171 struct pci_dev *pdev = to_pci_dev(dev);
172
173 /* bad things may happen if the no_msi flag is changed
174 * while some drivers are loaded */
175 if (!capable(CAP_SYS_ADMIN))
176 return count;
177
178 if (!pdev->subordinate)
179 return count;
180
181 if (*buf == '0') {
182 pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
183 dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
184 " bad things could happen.\n");
185 }
186
187 if (*buf == '1') {
188 pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
189 dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
190 " bad things could happen.\n");
191 }
192
193 return count;
194}
Greg KH98885492005-05-05 11:57:25 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196struct device_attribute pci_dev_attrs[] = {
197 __ATTR_RO(resource),
198 __ATTR_RO(vendor),
199 __ATTR_RO(device),
200 __ATTR_RO(subsystem_vendor),
201 __ATTR_RO(subsystem_device),
202 __ATTR_RO(class),
203 __ATTR_RO(irq),
204 __ATTR_RO(local_cpus),
Greg KH98885492005-05-05 11:57:25 -0700205 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100206#ifdef CONFIG_NUMA
207 __ATTR_RO(numa_node),
208#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200209 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700210 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
211 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400212 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 __ATTR_NULL,
214};
215
216static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800217pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
218 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
221 unsigned int size = 64;
222 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900223 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* Several chips lock up trying to read undefined config space */
226 if (capable(CAP_SYS_ADMIN)) {
227 size = dev->cfg_size;
228 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
229 size = 128;
230 }
231
232 if (off > size)
233 return 0;
234 if (off + count > size) {
235 size -= off;
236 count = size;
237 } else {
238 size = count;
239 }
240
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900241 if ((off & 1) && size) {
242 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700243 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900244 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900246 size--;
247 }
248
249 if ((off & 3) && size > 2) {
250 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700251 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900252 data[off - init_off] = val & 0xff;
253 data[off - init_off + 1] = (val >> 8) & 0xff;
254 off += 2;
255 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
258 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900259 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700260 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900261 data[off - init_off] = val & 0xff;
262 data[off - init_off + 1] = (val >> 8) & 0xff;
263 data[off - init_off + 2] = (val >> 16) & 0xff;
264 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 off += 4;
266 size -= 4;
267 }
268
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900269 if (size >= 2) {
270 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700271 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900272 data[off - init_off] = val & 0xff;
273 data[off - init_off + 1] = (val >> 8) & 0xff;
274 off += 2;
275 size -= 2;
276 }
277
278 if (size > 0) {
279 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700280 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900281 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 off++;
283 --size;
284 }
285
286 return count;
287}
288
289static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800290pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
291 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
294 unsigned int size = count;
295 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900296 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (off > dev->cfg_size)
299 return 0;
300 if (off + count > dev->cfg_size) {
301 size = dev->cfg_size - off;
302 count = size;
303 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900304
305 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700306 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900308 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900310
311 if ((off & 3) && size > 2) {
312 u16 val = data[off - init_off];
313 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700314 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900315 off += 2;
316 size -= 2;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900320 u32 val = data[off - init_off];
321 val |= (u32) data[off - init_off + 1] << 8;
322 val |= (u32) data[off - init_off + 2] << 16;
323 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700324 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 off += 4;
326 size -= 4;
327 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900328
329 if (size >= 2) {
330 u16 val = data[off - init_off];
331 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700332 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900333 off += 2;
334 size -= 2;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900337 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700338 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 off++;
340 --size;
341 }
342
343 return count;
344}
345
346#ifdef HAVE_PCI_LEGACY
347/**
348 * pci_read_legacy_io - read byte(s) from legacy I/O port space
349 * @kobj: kobject corresponding to file to read from
350 * @buf: buffer to store results
351 * @off: offset into legacy I/O port space
352 * @count: number of bytes to read
353 *
354 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
355 * callback routine (pci_legacy_read).
356 */
357ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800358pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
359 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400362 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 kobj));
364
365 /* Only support 1, 2 or 4 byte accesses */
366 if (count != 1 && count != 2 && count != 4)
367 return -EINVAL;
368
369 return pci_legacy_read(bus, off, (u32 *)buf, count);
370}
371
372/**
373 * pci_write_legacy_io - write byte(s) to legacy I/O port space
374 * @kobj: kobject corresponding to file to read from
375 * @buf: buffer containing value to be written
376 * @off: offset into legacy I/O port space
377 * @count: number of bytes to write
378 *
379 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
380 * callback routine (pci_legacy_write).
381 */
382ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800383pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
384 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400387 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kobj));
389 /* Only support 1, 2 or 4 byte accesses */
390 if (count != 1 && count != 2 && count != 4)
391 return -EINVAL;
392
393 return pci_legacy_write(bus, off, *(u32 *)buf, count);
394}
395
396/**
397 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
398 * @kobj: kobject corresponding to device to be mapped
399 * @attr: struct bin_attribute for this file
400 * @vma: struct vm_area_struct passed to mmap
401 *
402 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
403 * legacy memory space (first meg of bus space) into application virtual
404 * memory space.
405 */
406int
407pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
408 struct vm_area_struct *vma)
409{
410 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400411 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 kobj));
413
414 return pci_mmap_legacy_page_range(bus, vma);
415}
416#endif /* HAVE_PCI_LEGACY */
417
418#ifdef HAVE_PCI_MMAP
419/**
420 * pci_mmap_resource - map a PCI resource into user memory space
421 * @kobj: kobject for mapping
422 * @attr: struct bin_attribute for the file being mapped
423 * @vma: struct vm_area_struct passed into the mmap
424 *
425 * Use the regular PCI mapping routines to map a PCI resource into userspace.
426 * FIXME: write combining? maybe automatic for prefetchable regions?
427 */
428static int
429pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
430 struct vm_area_struct *vma)
431{
432 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
433 struct device, kobj));
434 struct resource *res = (struct resource *)attr->private;
435 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700436 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000437 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000439 for (i = 0; i < PCI_ROM_RESOURCE; i++)
440 if (res == &pdev->resource[i])
441 break;
442 if (i >= PCI_ROM_RESOURCE)
443 return -ENODEV;
444
445 /* pci_mmap_page_range() expects the same kind of entry as coming
446 * from /proc/bus/pci/ which is a "user visible" value. If this is
447 * different from the resource itself, arch will do necessary fixup.
448 */
449 pci_resource_to_user(pdev, i, res, &start, &end);
450 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
452
453 return pci_mmap_page_range(pdev, vma, mmap_type, 0);
454}
455
456/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700457 * pci_remove_resource_files - cleanup resource files
458 * @dev: dev to cleanup
459 *
460 * If we created resource files for @dev, remove them from sysfs and
461 * free their resources.
462 */
463static void
464pci_remove_resource_files(struct pci_dev *pdev)
465{
466 int i;
467
468 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
469 struct bin_attribute *res_attr;
470
471 res_attr = pdev->res_attr[i];
472 if (res_attr) {
473 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
474 kfree(res_attr);
475 }
476 }
477}
478
479/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * pci_create_resource_files - create resource files in sysfs for @dev
481 * @dev: dev in question
482 *
483 * Walk the resources in @dev creating files for each resource available.
484 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700485static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700488 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* Expose the PCI resources from this device as files */
491 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
492 struct bin_attribute *res_attr;
493
494 /* skip empty resources */
495 if (!pci_resource_len(pdev, i))
496 continue;
497
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500498 /* allocate attribute structure, piggyback attribute name */
Pekka Enberg656da9d2005-09-22 00:48:11 -0700499 res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (res_attr) {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500501 char *res_attr_name = (char *)(res_attr + 1);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 pdev->res_attr[i] = res_attr;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500504 sprintf(res_attr_name, "resource%d", i);
505 res_attr->attr.name = res_attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 res_attr->attr.mode = S_IRUSR | S_IWUSR;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500507 res_attr->size = pci_resource_len(pdev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 res_attr->mmap = pci_mmap_resource;
509 res_attr->private = &pdev->resource[i];
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700510 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
511 if (retval) {
512 pci_remove_resource_files(pdev);
513 return retval;
514 }
515 } else {
516 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521#else /* !HAVE_PCI_MMAP */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700522static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
524#endif /* HAVE_PCI_MMAP */
525
526/**
527 * pci_write_rom - used to enable access to the PCI ROM display
528 * @kobj: kernel object handle
529 * @buf: user input
530 * @off: file offset
531 * @count: number of byte in input
532 *
533 * writing anything except 0 enables it
534 */
535static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800536pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
537 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
540
541 if ((off == 0) && (*buf == '0') && (count == 2))
542 pdev->rom_attr_enabled = 0;
543 else
544 pdev->rom_attr_enabled = 1;
545
546 return count;
547}
548
549/**
550 * pci_read_rom - read a PCI ROM
551 * @kobj: kernel object handle
552 * @buf: where to put the data we read from the ROM
553 * @off: file offset
554 * @count: number of bytes to read
555 *
556 * Put @count bytes starting at @off into @buf from the ROM in the PCI
557 * device corresponding to @kobj.
558 */
559static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800560pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
561 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
564 void __iomem *rom;
565 size_t size;
566
567 if (!pdev->rom_attr_enabled)
568 return -EINVAL;
569
570 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
571 if (!rom)
572 return 0;
573
574 if (off >= size)
575 count = 0;
576 else {
577 if (off + count > size)
578 count = size - off;
579
580 memcpy_fromio(buf, rom + off, count);
581 }
582 pci_unmap_rom(pdev, rom);
583
584 return count;
585}
586
587static struct bin_attribute pci_config_attr = {
588 .attr = {
589 .name = "config",
590 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 },
592 .size = 256,
593 .read = pci_read_config,
594 .write = pci_write_config,
595};
596
597static struct bin_attribute pcie_config_attr = {
598 .attr = {
599 .name = "config",
600 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 },
602 .size = 4096,
603 .read = pci_read_config,
604 .write = pci_write_config,
605};
606
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000607int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000608{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000609 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000610}
611
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700612int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700614 struct bin_attribute *rom_attr = NULL;
615 int retval;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!sysfs_initialized)
618 return -EACCES;
619
620 if (pdev->cfg_size < 4096)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700621 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700623 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
624 if (retval)
625 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700627 retval = pci_create_resource_files(pdev);
628 if (retval)
629 goto err_bin_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* If the device has a ROM, try to expose it in sysfs. */
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700632 if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
633 (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100634 rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (rom_attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pdev->rom_attr = rom_attr;
637 rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
638 rom_attr->attr.name = "rom";
639 rom_attr->attr.mode = S_IRUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 rom_attr->read = pci_read_rom;
641 rom_attr->write = pci_write_rom;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700642 retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
643 if (retval)
644 goto err_rom;
645 } else {
646 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +1000647 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 }
650 /* add platform-specific attributes */
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000651 if (pcibios_add_platform_entries(pdev))
652 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700653
Shaohua Li6c723d52008-01-24 10:21:57 +0800654 pcie_aspm_create_sysfs_dev_files(pdev);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700657
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000658err_rom_file:
659 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
660 sysfs_remove_bin_file(&pdev->dev.kobj, rom_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700661err_rom:
662 kfree(rom_attr);
Michael Ellerman9890b122007-04-18 13:34:12 +1000663err_resource_files:
664 pci_remove_resource_files(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700665err_bin_file:
666 if (pdev->cfg_size < 4096)
667 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
668 else
669 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
670err:
671 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/**
675 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
676 * @pdev: device whose entries we should free
677 *
678 * Cleanup when @pdev is removed from sysfs.
679 */
680void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
681{
David Millerd67afe52006-11-10 12:27:48 -0800682 if (!sysfs_initialized)
683 return;
684
Shaohua Li6c723d52008-01-24 10:21:57 +0800685 pcie_aspm_remove_sysfs_dev_files(pdev);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (pdev->cfg_size < 4096)
688 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
689 else
690 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
691
692 pci_remove_resource_files(pdev);
693
694 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
695 if (pdev->rom_attr) {
696 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
697 kfree(pdev->rom_attr);
698 }
699 }
700}
701
702static int __init pci_sysfs_init(void)
703{
704 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700705 int retval;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700708 for_each_pci_dev(pdev) {
709 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +0100710 if (retval) {
711 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700712 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +0100713 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 return 0;
717}
718
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700719late_initcall(pci_sysfs_init);