blob: 1c8929801400f99489543ed44e486a98a77945a6 [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);
Trent Piepho92425a42008-11-30 17:10:12 -080061 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070062
Trent Piepho92425a42008-11-30 17:10:12 -080063 if (strict_strtoul(buf, 0, &val) < 0)
64 return -EINVAL;
65
66 pdev->broken_parity_status = !!val;
67
68 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070069}
70
Alan Cox4327edf2005-09-10 00:25:49 -070071static ssize_t local_cpus_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Mike Travis3be83052009-01-04 05:18:01 -080074 const struct cpumask *mask;
Alan Cox4327edf2005-09-10 00:25:49 -070075 int len;
76
Mike Travis3be83052009-01-04 05:18:01 -080077 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
78 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070079 buf[len++] = '\n';
80 buf[len] = '\0';
81 return len;
82}
83
84
85static ssize_t local_cpulist_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
87{
Mike Travis3be83052009-01-04 05:18:01 -080088 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070089 int len;
90
Mike Travis3be83052009-01-04 05:18:01 -080091 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
92 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070093 buf[len++] = '\n';
94 buf[len] = '\0';
95 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/* show resources */
99static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400100resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct pci_dev * pci_dev = to_pci_dev(dev);
103 char * str = buf;
104 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800105 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700106 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (pci_dev->subordinate)
109 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800110 else
111 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000114 struct resource *res = &pci_dev->resource[i];
115 pci_resource_to_user(pci_dev, i, res, &start, &end);
116 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
117 (unsigned long long)start,
118 (unsigned long long)end,
119 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 return (str - buf);
122}
123
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200124static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700125{
126 struct pci_dev *pci_dev = to_pci_dev(dev);
127
128 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
129 pci_dev->vendor, pci_dev->device,
130 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
131 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
132 (u8)(pci_dev->class));
133}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800134
135static ssize_t is_enabled_store(struct device *dev,
136 struct device_attribute *attr, const char *buf,
137 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200138{
139 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800140 unsigned long val;
141 ssize_t result = strict_strtoul(buf, 0, &val);
142
143 if (result < 0)
144 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200145
146 /* this can crash the machine when done on the "wrong" device */
147 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800148 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200149
Trent Piepho92425a42008-11-30 17:10:12 -0800150 if (!val) {
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800151 if (atomic_read(&pdev->enable_cnt) != 0)
152 pci_disable_device(pdev);
153 else
154 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800155 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800156 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200157
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800158 return result < 0 ? result : count;
159}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200160
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800161static ssize_t is_enabled_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
164 struct pci_dev *pdev;
165
166 pdev = to_pci_dev (dev);
167 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200168}
169
Brice Goglin81bb0e12007-01-28 10:53:40 +0100170#ifdef CONFIG_NUMA
171static ssize_t
172numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
173{
174 return sprintf (buf, "%d\n", dev->numa_node);
175}
176#endif
177
Brice Goglinfe970642006-08-31 01:55:15 -0400178static ssize_t
179msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
180{
181 struct pci_dev *pdev = to_pci_dev(dev);
182
183 if (!pdev->subordinate)
184 return 0;
185
186 return sprintf (buf, "%u\n",
187 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
188}
189
190static ssize_t
191msi_bus_store(struct device *dev, struct device_attribute *attr,
192 const char *buf, size_t count)
193{
194 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800195 unsigned long val;
196
197 if (strict_strtoul(buf, 0, &val) < 0)
198 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400199
200 /* bad things may happen if the no_msi flag is changed
201 * while some drivers are loaded */
202 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800203 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400204
Trent Piepho92425a42008-11-30 17:10:12 -0800205 /* Maybe pci devices without subordinate busses shouldn't even have this
206 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400207 if (!pdev->subordinate)
208 return count;
209
Trent Piepho92425a42008-11-30 17:10:12 -0800210 /* Is the flag going to change, or keep the value it already had? */
211 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
212 !!val) {
213 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400214
Trent Piepho92425a42008-11-30 17:10:12 -0800215 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
216 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400217 }
218
219 return count;
220}
Greg KH98885492005-05-05 11:57:25 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222struct device_attribute pci_dev_attrs[] = {
223 __ATTR_RO(resource),
224 __ATTR_RO(vendor),
225 __ATTR_RO(device),
226 __ATTR_RO(subsystem_vendor),
227 __ATTR_RO(subsystem_device),
228 __ATTR_RO(class),
229 __ATTR_RO(irq),
230 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700231 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700232 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100233#ifdef CONFIG_NUMA
234 __ATTR_RO(numa_node),
235#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200236 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700237 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
238 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400239 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 __ATTR_NULL,
241};
242
243static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800244pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
245 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
248 unsigned int size = 64;
249 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900250 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Several chips lock up trying to read undefined config space */
253 if (capable(CAP_SYS_ADMIN)) {
254 size = dev->cfg_size;
255 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
256 size = 128;
257 }
258
259 if (off > size)
260 return 0;
261 if (off + count > size) {
262 size -= off;
263 count = size;
264 } else {
265 size = count;
266 }
267
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900268 if ((off & 1) && size) {
269 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700270 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900271 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900273 size--;
274 }
275
276 if ((off & 3) && size > 2) {
277 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700278 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900279 data[off - init_off] = val & 0xff;
280 data[off - init_off + 1] = (val >> 8) & 0xff;
281 off += 2;
282 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900286 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700287 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900288 data[off - init_off] = val & 0xff;
289 data[off - init_off + 1] = (val >> 8) & 0xff;
290 data[off - init_off + 2] = (val >> 16) & 0xff;
291 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 off += 4;
293 size -= 4;
294 }
295
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900296 if (size >= 2) {
297 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700298 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900299 data[off - init_off] = val & 0xff;
300 data[off - init_off + 1] = (val >> 8) & 0xff;
301 off += 2;
302 size -= 2;
303 }
304
305 if (size > 0) {
306 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700307 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900308 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 off++;
310 --size;
311 }
312
313 return count;
314}
315
316static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800317pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
318 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
321 unsigned int size = count;
322 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900323 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (off > dev->cfg_size)
326 return 0;
327 if (off + count > dev->cfg_size) {
328 size = dev->cfg_size - off;
329 count = size;
330 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900331
332 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700333 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900335 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900337
338 if ((off & 3) && size > 2) {
339 u16 val = data[off - init_off];
340 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700341 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900342 off += 2;
343 size -= 2;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900347 u32 val = data[off - init_off];
348 val |= (u32) data[off - init_off + 1] << 8;
349 val |= (u32) data[off - init_off + 2] << 16;
350 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700351 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 off += 4;
353 size -= 4;
354 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900355
356 if (size >= 2) {
357 u16 val = data[off - init_off];
358 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700359 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900360 off += 2;
361 size -= 2;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900364 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700365 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 off++;
367 --size;
368 }
369
370 return count;
371}
372
Ben Hutchings94e61082008-03-05 16:52:39 +0000373static ssize_t
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800374read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000375 char *buf, loff_t off, size_t count)
376{
377 struct pci_dev *dev =
378 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000379
380 if (off > bin_attr->size)
381 count = 0;
382 else if (count > bin_attr->size - off)
383 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000384
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800385 return pci_read_vpd(dev, off, count, buf);
386}
Ben Hutchings94e61082008-03-05 16:52:39 +0000387
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800388static ssize_t
389write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
390 char *buf, loff_t off, size_t count)
391{
392 struct pci_dev *dev =
393 to_pci_dev(container_of(kobj, struct device, kobj));
394
395 if (off > bin_attr->size)
396 count = 0;
397 else if (count > bin_attr->size - off)
398 count = bin_attr->size - off;
399
400 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#ifdef HAVE_PCI_LEGACY
404/**
405 * pci_read_legacy_io - read byte(s) from legacy I/O port space
406 * @kobj: kobject corresponding to file to read from
407 * @buf: buffer to store results
408 * @off: offset into legacy I/O port space
409 * @count: number of bytes to read
410 *
411 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
412 * callback routine (pci_legacy_read).
413 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000414static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800415pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
416 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400419 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 kobj));
421
422 /* Only support 1, 2 or 4 byte accesses */
423 if (count != 1 && count != 2 && count != 4)
424 return -EINVAL;
425
426 return pci_legacy_read(bus, off, (u32 *)buf, count);
427}
428
429/**
430 * pci_write_legacy_io - write byte(s) to legacy I/O port space
431 * @kobj: kobject corresponding to file to read from
432 * @buf: buffer containing value to be written
433 * @off: offset into legacy I/O port space
434 * @count: number of bytes to write
435 *
436 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
437 * callback routine (pci_legacy_write).
438 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000439static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800440pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
441 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400444 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kobj));
446 /* Only support 1, 2 or 4 byte accesses */
447 if (count != 1 && count != 2 && count != 4)
448 return -EINVAL;
449
450 return pci_legacy_write(bus, off, *(u32 *)buf, count);
451}
452
453/**
454 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
455 * @kobj: kobject corresponding to device to be mapped
456 * @attr: struct bin_attribute for this file
457 * @vma: struct vm_area_struct passed to mmap
458 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000459 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * legacy memory space (first meg of bus space) into application virtual
461 * memory space.
462 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000463static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
465 struct vm_area_struct *vma)
466{
467 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400468 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 kobj));
470
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000471 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
472}
473
474/**
475 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
476 * @kobj: kobject corresponding to device to be mapped
477 * @attr: struct bin_attribute for this file
478 * @vma: struct vm_area_struct passed to mmap
479 *
480 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
481 * legacy IO space (first meg of bus space) into application virtual
482 * memory space. Returns -ENOSYS if the operation isn't supported
483 */
484static int
485pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
486 struct vm_area_struct *vma)
487{
488 struct pci_bus *bus = to_pci_bus(container_of(kobj,
489 struct device,
490 kobj));
491
492 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
493}
494
495/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300496 * pci_adjust_legacy_attr - adjustment of legacy file attributes
497 * @b: bus to create files under
498 * @mmap_type: I/O port or memory
499 *
500 * Stub implementation. Can be overridden by arch if necessary.
501 */
502void __weak
503pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
504{
505 return;
506}
507
508/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000509 * pci_create_legacy_files - create legacy I/O port and memory files
510 * @b: bus to create files under
511 *
512 * Some platforms allow access to legacy I/O port and ISA memory space on
513 * a per-bus basis. This routine creates the files and ties them into
514 * their associated read, write and mmap files from pci-sysfs.c
515 *
516 * On error unwind, but don't propogate the error to the caller
517 * as it is ok to set up the PCI bus without these files.
518 */
519void pci_create_legacy_files(struct pci_bus *b)
520{
521 int error;
522
523 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
524 GFP_ATOMIC);
525 if (!b->legacy_io)
526 goto kzalloc_err;
527
528 b->legacy_io->attr.name = "legacy_io";
529 b->legacy_io->size = 0xffff;
530 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
531 b->legacy_io->read = pci_read_legacy_io;
532 b->legacy_io->write = pci_write_legacy_io;
533 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300534 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000535 error = device_create_bin_file(&b->dev, b->legacy_io);
536 if (error)
537 goto legacy_io_err;
538
539 /* Allocated above after the legacy_io struct */
540 b->legacy_mem = b->legacy_io + 1;
541 b->legacy_mem->attr.name = "legacy_mem";
542 b->legacy_mem->size = 1024*1024;
543 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
544 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300545 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000546 error = device_create_bin_file(&b->dev, b->legacy_mem);
547 if (error)
548 goto legacy_mem_err;
549
550 return;
551
552legacy_mem_err:
553 device_remove_bin_file(&b->dev, b->legacy_io);
554legacy_io_err:
555 kfree(b->legacy_io);
556 b->legacy_io = NULL;
557kzalloc_err:
558 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
559 "and ISA memory resources to sysfs\n");
560 return;
561}
562
563void pci_remove_legacy_files(struct pci_bus *b)
564{
565 if (b->legacy_io) {
566 device_remove_bin_file(&b->dev, b->legacy_io);
567 device_remove_bin_file(&b->dev, b->legacy_mem);
568 kfree(b->legacy_io); /* both are allocated here */
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571#endif /* HAVE_PCI_LEGACY */
572
573#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700574
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700575int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700576{
577 unsigned long nr, start, size;
578
579 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
580 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800581 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700582 if (start < size && size - start >= nr)
583 return 1;
584 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
585 current->comm, start, start+nr, pci_name(pdev), resno, size);
586 return 0;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/**
590 * pci_mmap_resource - map a PCI resource into user memory space
591 * @kobj: kobject for mapping
592 * @attr: struct bin_attribute for the file being mapped
593 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700594 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
596 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598static int
599pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700600 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
603 struct device, kobj));
604 struct resource *res = (struct resource *)attr->private;
605 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700606 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000607 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000609 for (i = 0; i < PCI_ROM_RESOURCE; i++)
610 if (res == &pdev->resource[i])
611 break;
612 if (i >= PCI_ROM_RESOURCE)
613 return -ENODEV;
614
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700615 if (!pci_mmap_fits(pdev, i, vma))
616 return -EINVAL;
617
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000618 /* pci_mmap_page_range() expects the same kind of entry as coming
619 * from /proc/bus/pci/ which is a "user visible" value. If this is
620 * different from the resource itself, arch will do necessary fixup.
621 */
622 pci_resource_to_user(pdev, i, res, &start, &end);
623 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
625
Arjan van de Vene8de1482008-10-22 19:55:31 -0700626 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
627 return -EINVAL;
628
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700629 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
630}
631
632static int
633pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
634 struct vm_area_struct *vma)
635{
636 return pci_mmap_resource(kobj, attr, vma, 0);
637}
638
639static int
640pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
641 struct vm_area_struct *vma)
642{
643 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700647 * pci_remove_resource_files - cleanup resource files
648 * @dev: dev to cleanup
649 *
650 * If we created resource files for @dev, remove them from sysfs and
651 * free their resources.
652 */
653static void
654pci_remove_resource_files(struct pci_dev *pdev)
655{
656 int i;
657
658 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
659 struct bin_attribute *res_attr;
660
661 res_attr = pdev->res_attr[i];
662 if (res_attr) {
663 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
664 kfree(res_attr);
665 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700666
667 res_attr = pdev->res_attr_wc[i];
668 if (res_attr) {
669 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
670 kfree(res_attr);
671 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700672 }
673}
674
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700675static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
676{
677 /* allocate attribute structure, piggyback attribute name */
678 int name_len = write_combine ? 13 : 10;
679 struct bin_attribute *res_attr;
680 int retval;
681
682 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
683 if (res_attr) {
684 char *res_attr_name = (char *)(res_attr + 1);
685
686 if (write_combine) {
687 pdev->res_attr_wc[num] = res_attr;
688 sprintf(res_attr_name, "resource%d_wc", num);
689 res_attr->mmap = pci_mmap_resource_wc;
690 } else {
691 pdev->res_attr[num] = res_attr;
692 sprintf(res_attr_name, "resource%d", num);
693 res_attr->mmap = pci_mmap_resource_uc;
694 }
695 res_attr->attr.name = res_attr_name;
696 res_attr->attr.mode = S_IRUSR | S_IWUSR;
697 res_attr->size = pci_resource_len(pdev, num);
698 res_attr->private = &pdev->resource[num];
699 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
700 } else
701 retval = -ENOMEM;
702
703 return retval;
704}
705
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700706/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * pci_create_resource_files - create resource files in sysfs for @dev
708 * @dev: dev in question
709 *
710 * Walk the resources in @dev creating files for each resource available.
711 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700712static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700715 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* Expose the PCI resources from this device as files */
718 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* skip empty resources */
721 if (!pci_resource_len(pdev, i))
722 continue;
723
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700724 retval = pci_create_attr(pdev, i, 0);
725 /* for prefetchable resources, create a WC mappable file */
726 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
727 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500728
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700729 if (retval) {
730 pci_remove_resource_files(pdev);
731 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300737int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
738void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739#endif /* HAVE_PCI_MMAP */
740
741/**
742 * pci_write_rom - used to enable access to the PCI ROM display
743 * @kobj: kernel object handle
744 * @buf: user input
745 * @off: file offset
746 * @count: number of byte in input
747 *
748 * writing anything except 0 enables it
749 */
750static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800751pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
752 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
755
756 if ((off == 0) && (*buf == '0') && (count == 2))
757 pdev->rom_attr_enabled = 0;
758 else
759 pdev->rom_attr_enabled = 1;
760
761 return count;
762}
763
764/**
765 * pci_read_rom - read a PCI ROM
766 * @kobj: kernel object handle
767 * @buf: where to put the data we read from the ROM
768 * @off: file offset
769 * @count: number of bytes to read
770 *
771 * Put @count bytes starting at @off into @buf from the ROM in the PCI
772 * device corresponding to @kobj.
773 */
774static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800775pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
776 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
779 void __iomem *rom;
780 size_t size;
781
782 if (!pdev->rom_attr_enabled)
783 return -EINVAL;
784
785 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +1100786 if (!rom || !size)
787 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (off >= size)
790 count = 0;
791 else {
792 if (off + count > size)
793 count = size - off;
794
795 memcpy_fromio(buf, rom + off, count);
796 }
797 pci_unmap_rom(pdev, rom);
798
799 return count;
800}
801
802static struct bin_attribute pci_config_attr = {
803 .attr = {
804 .name = "config",
805 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800807 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .read = pci_read_config,
809 .write = pci_write_config,
810};
811
812static struct bin_attribute pcie_config_attr = {
813 .attr = {
814 .name = "config",
815 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800817 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .read = pci_read_config,
819 .write = pci_write_config,
820};
821
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000822int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000823{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000824 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000825}
826
Zhao, Yu280c73d2008-10-13 20:01:00 +0800827static int pci_create_capabilities_sysfs(struct pci_dev *dev)
828{
829 int retval;
830 struct bin_attribute *attr;
831
832 /* If the device has VPD, try to expose it in sysfs. */
833 if (dev->vpd) {
834 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
835 if (!attr)
836 return -ENOMEM;
837
838 attr->size = dev->vpd->len;
839 attr->attr.name = "vpd";
840 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800841 attr->read = read_vpd_attr;
842 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800843 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
844 if (retval) {
845 kfree(dev->vpd->attr);
846 return retval;
847 }
848 dev->vpd->attr = attr;
849 }
850
851 /* Active State Power Management */
852 pcie_aspm_create_sysfs_dev_files(dev);
853
854 return 0;
855}
856
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700857int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700859 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800860 int rom_size = 0;
861 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!sysfs_initialized)
864 return -EACCES;
865
Zhao, Yu557848c2008-10-13 19:18:07 +0800866 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700867 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700869 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
870 if (retval)
871 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700873 retval = pci_create_resource_files(pdev);
874 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +0800875 goto err_config_file;
876
877 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
878 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
879 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
880 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +0800883 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +0000884 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +0800885 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700886 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +1000887 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800889 attr->size = rom_size;
890 attr->attr.name = "rom";
891 attr->attr.mode = S_IRUSR;
892 attr->read = pci_read_rom;
893 attr->write = pci_write_rom;
894 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
895 if (retval) {
896 kfree(attr);
897 goto err_resource_files;
898 }
899 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +0800903 retval = pcibios_add_platform_entries(pdev);
904 if (retval)
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000905 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700906
Zhao, Yu280c73d2008-10-13 20:01:00 +0800907 /* add sysfs entries for various capabilities */
908 retval = pci_create_capabilities_sysfs(pdev);
909 if (retval)
910 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +0800911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700913
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000914err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +0800915 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +0000916 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +0800917 kfree(pdev->rom_attr);
918 pdev->rom_attr = NULL;
919 }
Michael Ellerman9890b122007-04-18 13:34:12 +1000920err_resource_files:
921 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +0000922err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +0800923 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700924 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
925 else
926 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
927err:
928 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Zhao, Yu280c73d2008-10-13 20:01:00 +0800931static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
932{
933 if (dev->vpd && dev->vpd->attr) {
934 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
935 kfree(dev->vpd->attr);
936 }
937
938 pcie_aspm_remove_sysfs_dev_files(dev);
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/**
942 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
943 * @pdev: device whose entries we should free
944 *
945 * Cleanup when @pdev is removed from sysfs.
946 */
947void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
948{
Zhao, Yu280c73d2008-10-13 20:01:00 +0800949 int rom_size = 0;
950
David Millerd67afe52006-11-10 12:27:48 -0800951 if (!sysfs_initialized)
952 return;
953
Zhao, Yu280c73d2008-10-13 20:01:00 +0800954 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800955
Zhao, Yu557848c2008-10-13 19:18:07 +0800956 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
958 else
959 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
960
961 pci_remove_resource_files(pdev);
962
Zhao, Yu280c73d2008-10-13 20:01:00 +0800963 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
964 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
965 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
966 rom_size = 0x20000;
967
968 if (rom_size && pdev->rom_attr) {
969 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
970 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972}
973
974static int __init pci_sysfs_init(void)
975{
976 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700977 int retval;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700980 for_each_pci_dev(pdev) {
981 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +0100982 if (retval) {
983 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700984 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +0100985 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 return 0;
989}
990
Jesse Barnes40ee9e92007-03-24 11:03:32 -0700991late_initcall(pci_sysfs_init);