blob: c88485860a0ad3dfff19485a417aaf112b051f08 [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>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/pci.h>
21#include <linux/stat.h>
22#include <linux/topology.h>
23#include <linux/mm.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070024#include <linux/capability.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080025#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "pci.h"
27
28static int sysfs_initialized; /* = 0 */
29
30/* show configuration fields */
31#define pci_config_attr(field, format_string) \
32static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040033field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{ \
35 struct pci_dev *pdev; \
36 \
37 pdev = to_pci_dev (dev); \
38 return sprintf (buf, format_string, pdev->field); \
39}
40
41pci_config_attr(vendor, "0x%04x\n");
42pci_config_attr(device, "0x%04x\n");
43pci_config_attr(subsystem_vendor, "0x%04x\n");
44pci_config_attr(subsystem_device, "0x%04x\n");
45pci_config_attr(class, "0x%06x\n");
46pci_config_attr(irq, "%u\n");
47
Doug Thompsonbdee9d92006-06-14 16:59:48 -070048static ssize_t broken_parity_status_show(struct device *dev,
49 struct device_attribute *attr,
50 char *buf)
51{
52 struct pci_dev *pdev = to_pci_dev(dev);
53 return sprintf (buf, "%u\n", pdev->broken_parity_status);
54}
55
56static ssize_t broken_parity_status_store(struct device *dev,
57 struct device_attribute *attr,
58 const char *buf, size_t count)
59{
60 struct pci_dev *pdev = to_pci_dev(dev);
61 ssize_t consumed = -EINVAL;
62
63 if ((count > 0) && (*buf == '0' || *buf == '1')) {
64 pdev->broken_parity_status = *buf == '1' ? 1 : 0;
65 consumed = count;
66 }
67 return consumed;
68}
69
Alan Cox4327edf2005-09-10 00:25:49 -070070static ssize_t local_cpus_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Alan Cox4327edf2005-09-10 00:25:49 -070073 cpumask_t mask;
74 int len;
75
76 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
Rusty Russell29c01772008-12-13 21:20:25 +103077 len = cpumask_scnprintf(buf, PAGE_SIZE-2, &mask);
Mike Travis39106dc2008-04-08 11:43:03 -070078 buf[len++] = '\n';
79 buf[len] = '\0';
80 return len;
81}
82
83
84static ssize_t local_cpulist_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
87 cpumask_t mask;
88 int len;
89
90 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
Rusty Russell29c01772008-12-13 21:20:25 +103091 len = cpulist_scnprintf(buf, PAGE_SIZE-2, &mask);
Mike Travis39106dc2008-04-08 11:43:03 -070092 buf[len++] = '\n';
93 buf[len] = '\0';
94 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/* show resources */
98static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -040099resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct pci_dev * pci_dev = to_pci_dev(dev);
102 char * str = buf;
103 int i;
104 int max = 7;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700105 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (pci_dev->subordinate)
108 max = DEVICE_COUNT_RESOURCE;
109
110 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000111 struct resource *res = &pci_dev->resource[i];
112 pci_resource_to_user(pci_dev, i, res, &start, &end);
113 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
114 (unsigned long long)start,
115 (unsigned long long)end,
116 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 return (str - buf);
119}
120
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200121static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700122{
123 struct pci_dev *pci_dev = to_pci_dev(dev);
124
125 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
126 pci_dev->vendor, pci_dev->device,
127 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
128 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
129 (u8)(pci_dev->class));
130}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800131
132static ssize_t is_enabled_store(struct device *dev,
133 struct device_attribute *attr, const char *buf,
134 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200135{
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800136 ssize_t result = -EINVAL;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200137 struct pci_dev *pdev = to_pci_dev(dev);
138
139 /* this can crash the machine when done on the "wrong" device */
140 if (!capable(CAP_SYS_ADMIN))
141 return count;
142
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800143 if (*buf == '0') {
144 if (atomic_read(&pdev->enable_cnt) != 0)
145 pci_disable_device(pdev);
146 else
147 result = -EIO;
148 } else if (*buf == '1')
149 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200150
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800151 return result < 0 ? result : count;
152}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200153
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800154static ssize_t is_enabled_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
157 struct pci_dev *pdev;
158
159 pdev = to_pci_dev (dev);
160 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200161}
162
Brice Goglin81bb0e12007-01-28 10:53:40 +0100163#ifdef CONFIG_NUMA
164static ssize_t
165numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
166{
167 return sprintf (buf, "%d\n", dev->numa_node);
168}
169#endif
170
Brice Goglinfe970642006-08-31 01:55:15 -0400171static ssize_t
172msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
173{
174 struct pci_dev *pdev = to_pci_dev(dev);
175
176 if (!pdev->subordinate)
177 return 0;
178
179 return sprintf (buf, "%u\n",
180 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
181}
182
183static ssize_t
184msi_bus_store(struct device *dev, struct device_attribute *attr,
185 const char *buf, size_t count)
186{
187 struct pci_dev *pdev = to_pci_dev(dev);
188
189 /* bad things may happen if the no_msi flag is changed
190 * while some drivers are loaded */
191 if (!capable(CAP_SYS_ADMIN))
192 return count;
193
194 if (!pdev->subordinate)
195 return count;
196
197 if (*buf == '0') {
198 pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
199 dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
200 " bad things could happen.\n");
201 }
202
203 if (*buf == '1') {
204 pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
205 dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
206 " bad things could happen.\n");
207 }
208
209 return count;
210}
Greg KH98885492005-05-05 11:57:25 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212struct device_attribute pci_dev_attrs[] = {
213 __ATTR_RO(resource),
214 __ATTR_RO(vendor),
215 __ATTR_RO(device),
216 __ATTR_RO(subsystem_vendor),
217 __ATTR_RO(subsystem_device),
218 __ATTR_RO(class),
219 __ATTR_RO(irq),
220 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700221 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700222 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100223#ifdef CONFIG_NUMA
224 __ATTR_RO(numa_node),
225#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200226 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700227 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
228 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400229 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 __ATTR_NULL,
231};
232
233static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800234pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
235 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
238 unsigned int size = 64;
239 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900240 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Several chips lock up trying to read undefined config space */
243 if (capable(CAP_SYS_ADMIN)) {
244 size = dev->cfg_size;
245 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
246 size = 128;
247 }
248
249 if (off > size)
250 return 0;
251 if (off + count > size) {
252 size -= off;
253 count = size;
254 } else {
255 size = count;
256 }
257
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900258 if ((off & 1) && size) {
259 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700260 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900261 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900263 size--;
264 }
265
266 if ((off & 3) && size > 2) {
267 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700268 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900269 data[off - init_off] = val & 0xff;
270 data[off - init_off + 1] = (val >> 8) & 0xff;
271 off += 2;
272 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900276 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700277 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900278 data[off - init_off] = val & 0xff;
279 data[off - init_off + 1] = (val >> 8) & 0xff;
280 data[off - init_off + 2] = (val >> 16) & 0xff;
281 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 off += 4;
283 size -= 4;
284 }
285
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900286 if (size >= 2) {
287 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700288 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900289 data[off - init_off] = val & 0xff;
290 data[off - init_off + 1] = (val >> 8) & 0xff;
291 off += 2;
292 size -= 2;
293 }
294
295 if (size > 0) {
296 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700297 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900298 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 off++;
300 --size;
301 }
302
303 return count;
304}
305
306static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800307pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
308 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
311 unsigned int size = count;
312 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900313 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (off > dev->cfg_size)
316 return 0;
317 if (off + count > dev->cfg_size) {
318 size = dev->cfg_size - off;
319 count = size;
320 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900321
322 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700323 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900325 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900327
328 if ((off & 3) && size > 2) {
329 u16 val = data[off - init_off];
330 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700331 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900332 off += 2;
333 size -= 2;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900337 u32 val = data[off - init_off];
338 val |= (u32) data[off - init_off + 1] << 8;
339 val |= (u32) data[off - init_off + 2] << 16;
340 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700341 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 off += 4;
343 size -= 4;
344 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900345
346 if (size >= 2) {
347 u16 val = data[off - init_off];
348 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700349 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900350 off += 2;
351 size -= 2;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900354 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700355 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 off++;
357 --size;
358 }
359
360 return count;
361}
362
Ben Hutchings94e61082008-03-05 16:52:39 +0000363static ssize_t
364pci_read_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
365 char *buf, loff_t off, size_t count)
366{
367 struct pci_dev *dev =
368 to_pci_dev(container_of(kobj, struct device, kobj));
369 int end;
370 int ret;
371
372 if (off > bin_attr->size)
373 count = 0;
374 else if (count > bin_attr->size - off)
375 count = bin_attr->size - off;
376 end = off + count;
377
378 while (off < end) {
379 ret = dev->vpd->ops->read(dev, off, end - off, buf);
380 if (ret < 0)
381 return ret;
382 buf += ret;
383 off += ret;
384 }
385
386 return count;
387}
388
389static ssize_t
390pci_write_vpd(struct kobject *kobj, struct bin_attribute *bin_attr,
391 char *buf, loff_t off, size_t count)
392{
393 struct pci_dev *dev =
394 to_pci_dev(container_of(kobj, struct device, kobj));
395 int end;
396 int ret;
397
398 if (off > bin_attr->size)
399 count = 0;
400 else if (count > bin_attr->size - off)
401 count = bin_attr->size - off;
402 end = off + count;
403
404 while (off < end) {
405 ret = dev->vpd->ops->write(dev, off, end - off, buf);
406 if (ret < 0)
407 return ret;
408 buf += ret;
409 off += ret;
410 }
411
412 return count;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#ifdef HAVE_PCI_LEGACY
416/**
417 * pci_read_legacy_io - read byte(s) from legacy I/O port space
418 * @kobj: kobject corresponding to file to read from
419 * @buf: buffer to store results
420 * @off: offset into legacy I/O port space
421 * @count: number of bytes to read
422 *
423 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
424 * callback routine (pci_legacy_read).
425 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000426static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800427pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
428 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400431 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 kobj));
433
434 /* Only support 1, 2 or 4 byte accesses */
435 if (count != 1 && count != 2 && count != 4)
436 return -EINVAL;
437
438 return pci_legacy_read(bus, off, (u32 *)buf, count);
439}
440
441/**
442 * pci_write_legacy_io - write byte(s) to legacy I/O port space
443 * @kobj: kobject corresponding to file to read from
444 * @buf: buffer containing value to be written
445 * @off: offset into legacy I/O port space
446 * @count: number of bytes to write
447 *
448 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
449 * callback routine (pci_legacy_write).
450 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000451static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800452pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
453 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400456 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 kobj));
458 /* Only support 1, 2 or 4 byte accesses */
459 if (count != 1 && count != 2 && count != 4)
460 return -EINVAL;
461
462 return pci_legacy_write(bus, off, *(u32 *)buf, count);
463}
464
465/**
466 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
467 * @kobj: kobject corresponding to device to be mapped
468 * @attr: struct bin_attribute for this file
469 * @vma: struct vm_area_struct passed to mmap
470 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000471 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * legacy memory space (first meg of bus space) into application virtual
473 * memory space.
474 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000475static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
477 struct vm_area_struct *vma)
478{
479 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400480 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 kobj));
482
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000483 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
484}
485
486/**
487 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
488 * @kobj: kobject corresponding to device to be mapped
489 * @attr: struct bin_attribute for this file
490 * @vma: struct vm_area_struct passed to mmap
491 *
492 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
493 * legacy IO space (first meg of bus space) into application virtual
494 * memory space. Returns -ENOSYS if the operation isn't supported
495 */
496static int
497pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
498 struct vm_area_struct *vma)
499{
500 struct pci_bus *bus = to_pci_bus(container_of(kobj,
501 struct device,
502 kobj));
503
504 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
505}
506
507/**
508 * pci_create_legacy_files - create legacy I/O port and memory files
509 * @b: bus to create files under
510 *
511 * Some platforms allow access to legacy I/O port and ISA memory space on
512 * a per-bus basis. This routine creates the files and ties them into
513 * their associated read, write and mmap files from pci-sysfs.c
514 *
515 * On error unwind, but don't propogate the error to the caller
516 * as it is ok to set up the PCI bus without these files.
517 */
518void pci_create_legacy_files(struct pci_bus *b)
519{
520 int error;
521
522 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
523 GFP_ATOMIC);
524 if (!b->legacy_io)
525 goto kzalloc_err;
526
527 b->legacy_io->attr.name = "legacy_io";
528 b->legacy_io->size = 0xffff;
529 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
530 b->legacy_io->read = pci_read_legacy_io;
531 b->legacy_io->write = pci_write_legacy_io;
532 b->legacy_io->mmap = pci_mmap_legacy_io;
533 error = device_create_bin_file(&b->dev, b->legacy_io);
534 if (error)
535 goto legacy_io_err;
536
537 /* Allocated above after the legacy_io struct */
538 b->legacy_mem = b->legacy_io + 1;
539 b->legacy_mem->attr.name = "legacy_mem";
540 b->legacy_mem->size = 1024*1024;
541 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
542 b->legacy_mem->mmap = pci_mmap_legacy_mem;
543 error = device_create_bin_file(&b->dev, b->legacy_mem);
544 if (error)
545 goto legacy_mem_err;
546
547 return;
548
549legacy_mem_err:
550 device_remove_bin_file(&b->dev, b->legacy_io);
551legacy_io_err:
552 kfree(b->legacy_io);
553 b->legacy_io = NULL;
554kzalloc_err:
555 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
556 "and ISA memory resources to sysfs\n");
557 return;
558}
559
560void pci_remove_legacy_files(struct pci_bus *b)
561{
562 if (b->legacy_io) {
563 device_remove_bin_file(&b->dev, b->legacy_io);
564 device_remove_bin_file(&b->dev, b->legacy_mem);
565 kfree(b->legacy_io); /* both are allocated here */
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568#endif /* HAVE_PCI_LEGACY */
569
570#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700571
572static int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
573{
574 unsigned long nr, start, size;
575
576 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
577 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800578 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700579 if (start < size && size - start >= nr)
580 return 1;
581 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
582 current->comm, start, start+nr, pci_name(pdev), resno, size);
583 return 0;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/**
587 * pci_mmap_resource - map a PCI resource into user memory space
588 * @kobj: kobject for mapping
589 * @attr: struct bin_attribute for the file being mapped
590 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700591 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *
593 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595static int
596pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700597 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
600 struct device, kobj));
601 struct resource *res = (struct resource *)attr->private;
602 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700603 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000604 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000606 for (i = 0; i < PCI_ROM_RESOURCE; i++)
607 if (res == &pdev->resource[i])
608 break;
609 if (i >= PCI_ROM_RESOURCE)
610 return -ENODEV;
611
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700612 if (!pci_mmap_fits(pdev, i, vma))
613 return -EINVAL;
614
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000615 /* pci_mmap_page_range() expects the same kind of entry as coming
616 * from /proc/bus/pci/ which is a "user visible" value. If this is
617 * different from the resource itself, arch will do necessary fixup.
618 */
619 pci_resource_to_user(pdev, i, res, &start, &end);
620 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
622
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700623 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
624}
625
626static int
627pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
628 struct vm_area_struct *vma)
629{
630 return pci_mmap_resource(kobj, attr, vma, 0);
631}
632
633static int
634pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
635 struct vm_area_struct *vma)
636{
637 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700641 * pci_remove_resource_files - cleanup resource files
642 * @dev: dev to cleanup
643 *
644 * If we created resource files for @dev, remove them from sysfs and
645 * free their resources.
646 */
647static void
648pci_remove_resource_files(struct pci_dev *pdev)
649{
650 int i;
651
652 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
653 struct bin_attribute *res_attr;
654
655 res_attr = pdev->res_attr[i];
656 if (res_attr) {
657 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
658 kfree(res_attr);
659 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700660
661 res_attr = pdev->res_attr_wc[i];
662 if (res_attr) {
663 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
664 kfree(res_attr);
665 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700666 }
667}
668
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700669static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
670{
671 /* allocate attribute structure, piggyback attribute name */
672 int name_len = write_combine ? 13 : 10;
673 struct bin_attribute *res_attr;
674 int retval;
675
676 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
677 if (res_attr) {
678 char *res_attr_name = (char *)(res_attr + 1);
679
680 if (write_combine) {
681 pdev->res_attr_wc[num] = res_attr;
682 sprintf(res_attr_name, "resource%d_wc", num);
683 res_attr->mmap = pci_mmap_resource_wc;
684 } else {
685 pdev->res_attr[num] = res_attr;
686 sprintf(res_attr_name, "resource%d", num);
687 res_attr->mmap = pci_mmap_resource_uc;
688 }
689 res_attr->attr.name = res_attr_name;
690 res_attr->attr.mode = S_IRUSR | S_IWUSR;
691 res_attr->size = pci_resource_len(pdev, num);
692 res_attr->private = &pdev->resource[num];
693 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
694 } else
695 retval = -ENOMEM;
696
697 return retval;
698}
699
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700700/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * pci_create_resource_files - create resource files in sysfs for @dev
702 * @dev: dev in question
703 *
704 * Walk the resources in @dev creating files for each resource available.
705 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700706static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700709 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* Expose the PCI resources from this device as files */
712 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* skip empty resources */
715 if (!pci_resource_len(pdev, i))
716 continue;
717
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700718 retval = pci_create_attr(pdev, i, 0);
719 /* for prefetchable resources, create a WC mappable file */
720 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
721 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500722
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700723 if (retval) {
724 pci_remove_resource_files(pdev);
725 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730#else /* !HAVE_PCI_MMAP */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700731static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
733#endif /* HAVE_PCI_MMAP */
734
735/**
736 * pci_write_rom - used to enable access to the PCI ROM display
737 * @kobj: kernel object handle
738 * @buf: user input
739 * @off: file offset
740 * @count: number of byte in input
741 *
742 * writing anything except 0 enables it
743 */
744static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800745pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
746 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
749
750 if ((off == 0) && (*buf == '0') && (count == 2))
751 pdev->rom_attr_enabled = 0;
752 else
753 pdev->rom_attr_enabled = 1;
754
755 return count;
756}
757
758/**
759 * pci_read_rom - read a PCI ROM
760 * @kobj: kernel object handle
761 * @buf: where to put the data we read from the ROM
762 * @off: file offset
763 * @count: number of bytes to read
764 *
765 * Put @count bytes starting at @off into @buf from the ROM in the PCI
766 * device corresponding to @kobj.
767 */
768static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800769pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
770 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
773 void __iomem *rom;
774 size_t size;
775
776 if (!pdev->rom_attr_enabled)
777 return -EINVAL;
778
779 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
780 if (!rom)
781 return 0;
782
783 if (off >= size)
784 count = 0;
785 else {
786 if (off + count > size)
787 count = size - off;
788
789 memcpy_fromio(buf, rom + off, count);
790 }
791 pci_unmap_rom(pdev, rom);
792
793 return count;
794}
795
796static struct bin_attribute pci_config_attr = {
797 .attr = {
798 .name = "config",
799 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800801 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .read = pci_read_config,
803 .write = pci_write_config,
804};
805
806static struct bin_attribute pcie_config_attr = {
807 .attr = {
808 .name = "config",
809 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800811 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .read = pci_read_config,
813 .write = pci_write_config,
814};
815
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000816int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000817{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000818 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000819}
820
Zhao, Yu280c73d2008-10-13 20:01:00 +0800821static int pci_create_capabilities_sysfs(struct pci_dev *dev)
822{
823 int retval;
824 struct bin_attribute *attr;
825
826 /* If the device has VPD, try to expose it in sysfs. */
827 if (dev->vpd) {
828 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
829 if (!attr)
830 return -ENOMEM;
831
832 attr->size = dev->vpd->len;
833 attr->attr.name = "vpd";
834 attr->attr.mode = S_IRUSR | S_IWUSR;
835 attr->read = pci_read_vpd;
836 attr->write = pci_write_vpd;
837 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
838 if (retval) {
839 kfree(dev->vpd->attr);
840 return retval;
841 }
842 dev->vpd->attr = attr;
843 }
844
845 /* Active State Power Management */
846 pcie_aspm_create_sysfs_dev_files(dev);
847
848 return 0;
849}
850
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700851int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700853 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800854 int rom_size = 0;
855 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!sysfs_initialized)
858 return -EACCES;
859
Zhao, Yu557848c2008-10-13 19:18:07 +0800860 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700861 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700863 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
864 if (retval)
865 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700867 retval = pci_create_resource_files(pdev);
868 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +0800869 goto err_config_file;
870
871 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
872 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
873 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
874 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +0800877 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +0000878 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +0800879 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700880 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +1000881 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800883 attr->size = rom_size;
884 attr->attr.name = "rom";
885 attr->attr.mode = S_IRUSR;
886 attr->read = pci_read_rom;
887 attr->write = pci_write_rom;
888 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
889 if (retval) {
890 kfree(attr);
891 goto err_resource_files;
892 }
893 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +0800897 retval = pcibios_add_platform_entries(pdev);
898 if (retval)
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000899 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700900
Zhao, Yu280c73d2008-10-13 20:01:00 +0800901 /* add sysfs entries for various capabilities */
902 retval = pci_create_capabilities_sysfs(pdev);
903 if (retval)
904 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +0800905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700907
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000908err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +0800909 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +0000910 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +0800911 kfree(pdev->rom_attr);
912 pdev->rom_attr = NULL;
913 }
Michael Ellerman9890b122007-04-18 13:34:12 +1000914err_resource_files:
915 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +0000916err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +0800917 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700918 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
919 else
920 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
921err:
922 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Zhao, Yu280c73d2008-10-13 20:01:00 +0800925static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
926{
927 if (dev->vpd && dev->vpd->attr) {
928 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
929 kfree(dev->vpd->attr);
930 }
931
932 pcie_aspm_remove_sysfs_dev_files(dev);
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/**
936 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
937 * @pdev: device whose entries we should free
938 *
939 * Cleanup when @pdev is removed from sysfs.
940 */
941void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
942{
Zhao, Yu280c73d2008-10-13 20:01:00 +0800943 int rom_size = 0;
944
David Millerd67afe52006-11-10 12:27:48 -0800945 if (!sysfs_initialized)
946 return;
947
Zhao, Yu280c73d2008-10-13 20:01:00 +0800948 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800949
Zhao, Yu557848c2008-10-13 19:18:07 +0800950 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
952 else
953 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
954
955 pci_remove_resource_files(pdev);
956
Zhao, Yu280c73d2008-10-13 20:01:00 +0800957 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
958 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
959 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
960 rom_size = 0x20000;
961
962 if (rom_size && pdev->rom_attr) {
963 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
964 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966}
967
968static int __init pci_sysfs_init(void)
969{
970 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700971 int retval;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700974 for_each_pci_dev(pdev) {
975 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +0100976 if (retval) {
977 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700978 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +0100979 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 return 0;
983}
984
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700985late_initcall(pci_sysfs_init);