blob: 8d9d648daeba74123a6f9cf9c74434f903292bac [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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);
Mike Travis39106dc2008-04-08 11:43:03 -070076 buf[len++] = '\n';
77 buf[len] = '\0';
78 return len;
79}
80
81
82static ssize_t local_cpulist_show(struct device *dev,
83 struct device_attribute *attr, char *buf)
84{
85 cpumask_t mask;
86 int len;
87
88 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
89 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
90 buf[len++] = '\n';
91 buf[len] = '\0';
92 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95/* show resources */
96static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -040097resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct pci_dev * pci_dev = to_pci_dev(dev);
100 char * str = buf;
101 int i;
102 int max = 7;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700103 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (pci_dev->subordinate)
106 max = DEVICE_COUNT_RESOURCE;
107
108 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000109 struct resource *res = &pci_dev->resource[i];
110 pci_resource_to_user(pci_dev, i, res, &start, &end);
111 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
112 (unsigned long long)start,
113 (unsigned long long)end,
114 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116 return (str - buf);
117}
118
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200119static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700120{
121 struct pci_dev *pci_dev = to_pci_dev(dev);
122
123 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
124 pci_dev->vendor, pci_dev->device,
125 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
126 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
127 (u8)(pci_dev->class));
128}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800129
130static ssize_t is_enabled_store(struct device *dev,
131 struct device_attribute *attr, const char *buf,
132 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200133{
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800134 ssize_t result = -EINVAL;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200135 struct pci_dev *pdev = to_pci_dev(dev);
136
137 /* this can crash the machine when done on the "wrong" device */
138 if (!capable(CAP_SYS_ADMIN))
139 return count;
140
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800141 if (*buf == '0') {
142 if (atomic_read(&pdev->enable_cnt) != 0)
143 pci_disable_device(pdev);
144 else
145 result = -EIO;
146 } else if (*buf == '1')
147 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200148
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800149 return result < 0 ? result : count;
150}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200151
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800152static ssize_t is_enabled_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
154{
155 struct pci_dev *pdev;
156
157 pdev = to_pci_dev (dev);
158 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200159}
160
Brice Goglin81bb0e12007-01-28 10:53:40 +0100161#ifdef CONFIG_NUMA
162static ssize_t
163numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
164{
165 return sprintf (buf, "%d\n", dev->numa_node);
166}
167#endif
168
Brice Goglinfe970642006-08-31 01:55:15 -0400169static ssize_t
170msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
171{
172 struct pci_dev *pdev = to_pci_dev(dev);
173
174 if (!pdev->subordinate)
175 return 0;
176
177 return sprintf (buf, "%u\n",
178 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
179}
180
181static ssize_t
182msi_bus_store(struct device *dev, struct device_attribute *attr,
183 const char *buf, size_t count)
184{
185 struct pci_dev *pdev = to_pci_dev(dev);
186
187 /* bad things may happen if the no_msi flag is changed
188 * while some drivers are loaded */
189 if (!capable(CAP_SYS_ADMIN))
190 return count;
191
192 if (!pdev->subordinate)
193 return count;
194
195 if (*buf == '0') {
196 pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
197 dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
198 " bad things could happen.\n");
199 }
200
201 if (*buf == '1') {
202 pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
203 dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
204 " bad things could happen.\n");
205 }
206
207 return count;
208}
Greg KH98885492005-05-05 11:57:25 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210struct device_attribute pci_dev_attrs[] = {
211 __ATTR_RO(resource),
212 __ATTR_RO(vendor),
213 __ATTR_RO(device),
214 __ATTR_RO(subsystem_vendor),
215 __ATTR_RO(subsystem_device),
216 __ATTR_RO(class),
217 __ATTR_RO(irq),
218 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700219 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700220 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100221#ifdef CONFIG_NUMA
222 __ATTR_RO(numa_node),
223#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200224 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700225 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
226 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400227 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 __ATTR_NULL,
229};
230
231static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800232pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
233 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
236 unsigned int size = 64;
237 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900238 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* Several chips lock up trying to read undefined config space */
241 if (capable(CAP_SYS_ADMIN)) {
242 size = dev->cfg_size;
243 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
244 size = 128;
245 }
246
247 if (off > size)
248 return 0;
249 if (off + count > size) {
250 size -= off;
251 count = size;
252 } else {
253 size = count;
254 }
255
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900256 if ((off & 1) && size) {
257 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700258 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900259 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900261 size--;
262 }
263
264 if ((off & 3) && size > 2) {
265 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700266 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900267 data[off - init_off] = val & 0xff;
268 data[off - init_off + 1] = (val >> 8) & 0xff;
269 off += 2;
270 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900274 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700275 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900276 data[off - init_off] = val & 0xff;
277 data[off - init_off + 1] = (val >> 8) & 0xff;
278 data[off - init_off + 2] = (val >> 16) & 0xff;
279 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 off += 4;
281 size -= 4;
282 }
283
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900284 if (size >= 2) {
285 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700286 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900287 data[off - init_off] = val & 0xff;
288 data[off - init_off + 1] = (val >> 8) & 0xff;
289 off += 2;
290 size -= 2;
291 }
292
293 if (size > 0) {
294 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700295 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900296 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 off++;
298 --size;
299 }
300
301 return count;
302}
303
304static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800305pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
306 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
309 unsigned int size = count;
310 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900311 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (off > dev->cfg_size)
314 return 0;
315 if (off + count > dev->cfg_size) {
316 size = dev->cfg_size - off;
317 count = size;
318 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900319
320 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700321 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900323 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900325
326 if ((off & 3) && 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
334 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900335 u32 val = data[off - init_off];
336 val |= (u32) data[off - init_off + 1] << 8;
337 val |= (u32) data[off - init_off + 2] << 16;
338 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700339 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 off += 4;
341 size -= 4;
342 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900343
344 if (size >= 2) {
345 u16 val = data[off - init_off];
346 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700347 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900348 off += 2;
349 size -= 2;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900352 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700353 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 off++;
355 --size;
356 }
357
358 return count;
359}
360
361#ifdef HAVE_PCI_LEGACY
362/**
363 * pci_read_legacy_io - read byte(s) from legacy I/O port space
364 * @kobj: kobject corresponding to file to read from
365 * @buf: buffer to store results
366 * @off: offset into legacy I/O port space
367 * @count: number of bytes to read
368 *
369 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
370 * callback routine (pci_legacy_read).
371 */
372ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800373pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
374 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400377 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 kobj));
379
380 /* Only support 1, 2 or 4 byte accesses */
381 if (count != 1 && count != 2 && count != 4)
382 return -EINVAL;
383
384 return pci_legacy_read(bus, off, (u32 *)buf, count);
385}
386
387/**
388 * pci_write_legacy_io - write byte(s) to legacy I/O port space
389 * @kobj: kobject corresponding to file to read from
390 * @buf: buffer containing value to be written
391 * @off: offset into legacy I/O port space
392 * @count: number of bytes to write
393 *
394 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
395 * callback routine (pci_legacy_write).
396 */
397ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800398pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
399 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400402 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 kobj));
404 /* Only support 1, 2 or 4 byte accesses */
405 if (count != 1 && count != 2 && count != 4)
406 return -EINVAL;
407
408 return pci_legacy_write(bus, off, *(u32 *)buf, count);
409}
410
411/**
412 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
413 * @kobj: kobject corresponding to device to be mapped
414 * @attr: struct bin_attribute for this file
415 * @vma: struct vm_area_struct passed to mmap
416 *
417 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
418 * legacy memory space (first meg of bus space) into application virtual
419 * memory space.
420 */
421int
422pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
423 struct vm_area_struct *vma)
424{
425 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400426 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kobj));
428
429 return pci_mmap_legacy_page_range(bus, vma);
430}
431#endif /* HAVE_PCI_LEGACY */
432
433#ifdef HAVE_PCI_MMAP
434/**
435 * pci_mmap_resource - map a PCI resource into user memory space
436 * @kobj: kobject for mapping
437 * @attr: struct bin_attribute for the file being mapped
438 * @vma: struct vm_area_struct passed into the mmap
439 *
440 * Use the regular PCI mapping routines to map a PCI resource into userspace.
441 * FIXME: write combining? maybe automatic for prefetchable regions?
442 */
443static int
444pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
445 struct vm_area_struct *vma)
446{
447 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
448 struct device, kobj));
449 struct resource *res = (struct resource *)attr->private;
450 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700451 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000452 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000454 for (i = 0; i < PCI_ROM_RESOURCE; i++)
455 if (res == &pdev->resource[i])
456 break;
457 if (i >= PCI_ROM_RESOURCE)
458 return -ENODEV;
459
460 /* pci_mmap_page_range() expects the same kind of entry as coming
461 * from /proc/bus/pci/ which is a "user visible" value. If this is
462 * different from the resource itself, arch will do necessary fixup.
463 */
464 pci_resource_to_user(pdev, i, res, &start, &end);
465 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
467
468 return pci_mmap_page_range(pdev, vma, mmap_type, 0);
469}
470
471/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700472 * pci_remove_resource_files - cleanup resource files
473 * @dev: dev to cleanup
474 *
475 * If we created resource files for @dev, remove them from sysfs and
476 * free their resources.
477 */
478static void
479pci_remove_resource_files(struct pci_dev *pdev)
480{
481 int i;
482
483 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
484 struct bin_attribute *res_attr;
485
486 res_attr = pdev->res_attr[i];
487 if (res_attr) {
488 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
489 kfree(res_attr);
490 }
491 }
492}
493
494/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * pci_create_resource_files - create resource files in sysfs for @dev
496 * @dev: dev in question
497 *
498 * Walk the resources in @dev creating files for each resource available.
499 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700500static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700503 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* Expose the PCI resources from this device as files */
506 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
507 struct bin_attribute *res_attr;
508
509 /* skip empty resources */
510 if (!pci_resource_len(pdev, i))
511 continue;
512
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500513 /* allocate attribute structure, piggyback attribute name */
Pekka Enberg656da9d2005-09-22 00:48:11 -0700514 res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (res_attr) {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500516 char *res_attr_name = (char *)(res_attr + 1);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 pdev->res_attr[i] = res_attr;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500519 sprintf(res_attr_name, "resource%d", i);
520 res_attr->attr.name = res_attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 res_attr->attr.mode = S_IRUSR | S_IWUSR;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500522 res_attr->size = pci_resource_len(pdev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 res_attr->mmap = pci_mmap_resource;
524 res_attr->private = &pdev->resource[i];
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700525 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
526 if (retval) {
527 pci_remove_resource_files(pdev);
528 return retval;
529 }
530 } else {
531 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536#else /* !HAVE_PCI_MMAP */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700537static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
539#endif /* HAVE_PCI_MMAP */
540
541/**
542 * pci_write_rom - used to enable access to the PCI ROM display
543 * @kobj: kernel object handle
544 * @buf: user input
545 * @off: file offset
546 * @count: number of byte in input
547 *
548 * writing anything except 0 enables it
549 */
550static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800551pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
552 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
555
556 if ((off == 0) && (*buf == '0') && (count == 2))
557 pdev->rom_attr_enabled = 0;
558 else
559 pdev->rom_attr_enabled = 1;
560
561 return count;
562}
563
564/**
565 * pci_read_rom - read a PCI ROM
566 * @kobj: kernel object handle
567 * @buf: where to put the data we read from the ROM
568 * @off: file offset
569 * @count: number of bytes to read
570 *
571 * Put @count bytes starting at @off into @buf from the ROM in the PCI
572 * device corresponding to @kobj.
573 */
574static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800575pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
576 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
579 void __iomem *rom;
580 size_t size;
581
582 if (!pdev->rom_attr_enabled)
583 return -EINVAL;
584
585 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
586 if (!rom)
587 return 0;
588
589 if (off >= size)
590 count = 0;
591 else {
592 if (off + count > size)
593 count = size - off;
594
595 memcpy_fromio(buf, rom + off, count);
596 }
597 pci_unmap_rom(pdev, rom);
598
599 return count;
600}
601
602static struct bin_attribute pci_config_attr = {
603 .attr = {
604 .name = "config",
605 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 },
607 .size = 256,
608 .read = pci_read_config,
609 .write = pci_write_config,
610};
611
612static struct bin_attribute pcie_config_attr = {
613 .attr = {
614 .name = "config",
615 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 },
617 .size = 4096,
618 .read = pci_read_config,
619 .write = pci_write_config,
620};
621
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000622int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000623{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000624 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000625}
626
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700627int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700629 struct bin_attribute *rom_attr = NULL;
630 int retval;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!sysfs_initialized)
633 return -EACCES;
634
635 if (pdev->cfg_size < 4096)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700636 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700638 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
639 if (retval)
640 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700642 retval = pci_create_resource_files(pdev);
643 if (retval)
644 goto err_bin_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* If the device has a ROM, try to expose it in sysfs. */
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700647 if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
648 (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100649 rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (rom_attr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 pdev->rom_attr = rom_attr;
652 rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
653 rom_attr->attr.name = "rom";
654 rom_attr->attr.mode = S_IRUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 rom_attr->read = pci_read_rom;
656 rom_attr->write = pci_write_rom;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700657 retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
658 if (retval)
659 goto err_rom;
660 } else {
661 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +1000662 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 }
665 /* add platform-specific attributes */
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000666 if (pcibios_add_platform_entries(pdev))
667 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700670
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000671err_rom_file:
672 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
673 sysfs_remove_bin_file(&pdev->dev.kobj, rom_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700674err_rom:
675 kfree(rom_attr);
Michael Ellerman9890b122007-04-18 13:34:12 +1000676err_resource_files:
677 pci_remove_resource_files(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700678err_bin_file:
679 if (pdev->cfg_size < 4096)
680 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
681 else
682 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
683err:
684 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687/**
688 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
689 * @pdev: device whose entries we should free
690 *
691 * Cleanup when @pdev is removed from sysfs.
692 */
693void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
694{
David Millerd67afe52006-11-10 12:27:48 -0800695 if (!sysfs_initialized)
696 return;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (pdev->cfg_size < 4096)
699 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
700 else
701 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
702
703 pci_remove_resource_files(pdev);
704
705 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
706 if (pdev->rom_attr) {
707 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
708 kfree(pdev->rom_attr);
709 }
710 }
711}
712
713static int __init pci_sysfs_init(void)
714{
715 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700716 int retval;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700719 for_each_pci_dev(pdev) {
720 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +0100721 if (retval) {
722 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700723 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +0100724 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 return 0;
728}
729
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700730late_initcall(pci_sysfs_init);