blob: a7eb1b46a5a849b1c18f6d40fa15030b68a53d95 [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) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900151 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800152 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
Alex Chiang705b1aa2009-03-20 14:56:31 -0600222#ifdef CONFIG_HOTPLUG
223static DEFINE_MUTEX(pci_remove_rescan_mutex);
224static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
225 size_t count)
226{
227 unsigned long val;
228 struct pci_bus *b = NULL;
229
230 if (strict_strtoul(buf, 0, &val) < 0)
231 return -EINVAL;
232
233 if (val) {
234 mutex_lock(&pci_remove_rescan_mutex);
235 while ((b = pci_find_next_bus(b)) != NULL)
236 pci_rescan_bus(b);
237 mutex_unlock(&pci_remove_rescan_mutex);
238 }
239 return count;
240}
241
242struct bus_attribute pci_bus_attrs[] = {
243 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
244 __ATTR_NULL
245};
Alex Chiang77c27c72009-03-20 14:56:36 -0600246
Alex Chiang738a6392009-03-20 14:56:41 -0600247static ssize_t
248dev_rescan_store(struct device *dev, struct device_attribute *attr,
249 const char *buf, size_t count)
250{
251 unsigned long val;
252 struct pci_dev *pdev = to_pci_dev(dev);
253
254 if (strict_strtoul(buf, 0, &val) < 0)
255 return -EINVAL;
256
257 if (val) {
258 mutex_lock(&pci_remove_rescan_mutex);
259 pci_rescan_bus(pdev->bus);
260 mutex_unlock(&pci_remove_rescan_mutex);
261 }
262 return count;
263}
264
Alex Chiang77c27c72009-03-20 14:56:36 -0600265static void remove_callback(struct device *dev)
266{
267 struct pci_dev *pdev = to_pci_dev(dev);
268
269 mutex_lock(&pci_remove_rescan_mutex);
270 pci_remove_bus_device(pdev);
271 mutex_unlock(&pci_remove_rescan_mutex);
272}
273
274static ssize_t
275remove_store(struct device *dev, struct device_attribute *dummy,
276 const char *buf, size_t count)
277{
278 int ret = 0;
279 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600280
281 if (strict_strtoul(buf, 0, &val) < 0)
282 return -EINVAL;
283
Alex Chiang77c27c72009-03-20 14:56:36 -0600284 /* An attribute cannot be unregistered by one of its own methods,
285 * so we have to use this roundabout approach.
286 */
287 if (val)
288 ret = device_schedule_callback(dev, remove_callback);
289 if (ret)
290 count = ret;
291 return count;
292}
Alex Chiang705b1aa2009-03-20 14:56:31 -0600293#endif
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295struct device_attribute pci_dev_attrs[] = {
296 __ATTR_RO(resource),
297 __ATTR_RO(vendor),
298 __ATTR_RO(device),
299 __ATTR_RO(subsystem_vendor),
300 __ATTR_RO(subsystem_device),
301 __ATTR_RO(class),
302 __ATTR_RO(irq),
303 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700304 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700305 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100306#ifdef CONFIG_NUMA
307 __ATTR_RO(numa_node),
308#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200309 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700310 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
311 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400312 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600313#ifdef CONFIG_HOTPLUG
314 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600315 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 __ATTR_NULL,
318};
319
320static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000321boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
322{
323 struct pci_dev *pdev = to_pci_dev(dev);
324
325 return sprintf(buf, "%u\n",
326 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
327 IORESOURCE_ROM_SHADOW));
328}
329struct device_attribute vga_attr = __ATTR_RO(boot_vga);
330
331static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800332pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
333 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
336 unsigned int size = 64;
337 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900338 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Several chips lock up trying to read undefined config space */
341 if (capable(CAP_SYS_ADMIN)) {
342 size = dev->cfg_size;
343 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
344 size = 128;
345 }
346
347 if (off > size)
348 return 0;
349 if (off + count > size) {
350 size -= off;
351 count = size;
352 } else {
353 size = count;
354 }
355
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900356 if ((off & 1) && size) {
357 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700358 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900359 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900361 size--;
362 }
363
364 if ((off & 3) && size > 2) {
365 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700366 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900367 data[off - init_off] = val & 0xff;
368 data[off - init_off + 1] = (val >> 8) & 0xff;
369 off += 2;
370 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900374 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700375 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900376 data[off - init_off] = val & 0xff;
377 data[off - init_off + 1] = (val >> 8) & 0xff;
378 data[off - init_off + 2] = (val >> 16) & 0xff;
379 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 off += 4;
381 size -= 4;
382 }
383
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900384 if (size >= 2) {
385 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700386 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900387 data[off - init_off] = val & 0xff;
388 data[off - init_off + 1] = (val >> 8) & 0xff;
389 off += 2;
390 size -= 2;
391 }
392
393 if (size > 0) {
394 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700395 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900396 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 off++;
398 --size;
399 }
400
401 return count;
402}
403
404static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800405pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
406 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
409 unsigned int size = count;
410 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900411 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (off > dev->cfg_size)
414 return 0;
415 if (off + count > dev->cfg_size) {
416 size = dev->cfg_size - off;
417 count = size;
418 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900419
420 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700421 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900423 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900425
426 if ((off & 3) && size > 2) {
427 u16 val = data[off - init_off];
428 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700429 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900430 off += 2;
431 size -= 2;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900435 u32 val = data[off - init_off];
436 val |= (u32) data[off - init_off + 1] << 8;
437 val |= (u32) data[off - init_off + 2] << 16;
438 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700439 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 off += 4;
441 size -= 4;
442 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900443
444 if (size >= 2) {
445 u16 val = data[off - init_off];
446 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700447 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900448 off += 2;
449 size -= 2;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900452 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700453 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 off++;
455 --size;
456 }
457
458 return count;
459}
460
Ben Hutchings94e61082008-03-05 16:52:39 +0000461static ssize_t
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800462read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000463 char *buf, loff_t off, size_t count)
464{
465 struct pci_dev *dev =
466 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000467
468 if (off > bin_attr->size)
469 count = 0;
470 else if (count > bin_attr->size - off)
471 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000472
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800473 return pci_read_vpd(dev, off, count, buf);
474}
Ben Hutchings94e61082008-03-05 16:52:39 +0000475
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800476static ssize_t
477write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
478 char *buf, loff_t off, size_t count)
479{
480 struct pci_dev *dev =
481 to_pci_dev(container_of(kobj, struct device, kobj));
482
483 if (off > bin_attr->size)
484 count = 0;
485 else if (count > bin_attr->size - off)
486 count = bin_attr->size - off;
487
488 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#ifdef HAVE_PCI_LEGACY
492/**
493 * pci_read_legacy_io - read byte(s) from legacy I/O port space
494 * @kobj: kobject corresponding to file to read from
495 * @buf: buffer to store results
496 * @off: offset into legacy I/O port space
497 * @count: number of bytes to read
498 *
499 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
500 * callback routine (pci_legacy_read).
501 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000502static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800503pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
504 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400507 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 kobj));
509
510 /* Only support 1, 2 or 4 byte accesses */
511 if (count != 1 && count != 2 && count != 4)
512 return -EINVAL;
513
514 return pci_legacy_read(bus, off, (u32 *)buf, count);
515}
516
517/**
518 * pci_write_legacy_io - write byte(s) to legacy I/O port space
519 * @kobj: kobject corresponding to file to read from
520 * @buf: buffer containing value to be written
521 * @off: offset into legacy I/O port space
522 * @count: number of bytes to write
523 *
524 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
525 * callback routine (pci_legacy_write).
526 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000527static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800528pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
529 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400532 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 kobj));
534 /* Only support 1, 2 or 4 byte accesses */
535 if (count != 1 && count != 2 && count != 4)
536 return -EINVAL;
537
538 return pci_legacy_write(bus, off, *(u32 *)buf, count);
539}
540
541/**
542 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
543 * @kobj: kobject corresponding to device to be mapped
544 * @attr: struct bin_attribute for this file
545 * @vma: struct vm_area_struct passed to mmap
546 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000547 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * legacy memory space (first meg of bus space) into application virtual
549 * memory space.
550 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000551static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
553 struct vm_area_struct *vma)
554{
555 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400556 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 kobj));
558
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000559 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
560}
561
562/**
563 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
564 * @kobj: kobject corresponding to device to be mapped
565 * @attr: struct bin_attribute for this file
566 * @vma: struct vm_area_struct passed to mmap
567 *
568 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
569 * legacy IO space (first meg of bus space) into application virtual
570 * memory space. Returns -ENOSYS if the operation isn't supported
571 */
572static int
573pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
574 struct vm_area_struct *vma)
575{
576 struct pci_bus *bus = to_pci_bus(container_of(kobj,
577 struct device,
578 kobj));
579
580 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
581}
582
583/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300584 * pci_adjust_legacy_attr - adjustment of legacy file attributes
585 * @b: bus to create files under
586 * @mmap_type: I/O port or memory
587 *
588 * Stub implementation. Can be overridden by arch if necessary.
589 */
590void __weak
591pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
592{
593 return;
594}
595
596/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000597 * pci_create_legacy_files - create legacy I/O port and memory files
598 * @b: bus to create files under
599 *
600 * Some platforms allow access to legacy I/O port and ISA memory space on
601 * a per-bus basis. This routine creates the files and ties them into
602 * their associated read, write and mmap files from pci-sysfs.c
603 *
604 * On error unwind, but don't propogate the error to the caller
605 * as it is ok to set up the PCI bus without these files.
606 */
607void pci_create_legacy_files(struct pci_bus *b)
608{
609 int error;
610
611 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
612 GFP_ATOMIC);
613 if (!b->legacy_io)
614 goto kzalloc_err;
615
616 b->legacy_io->attr.name = "legacy_io";
617 b->legacy_io->size = 0xffff;
618 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
619 b->legacy_io->read = pci_read_legacy_io;
620 b->legacy_io->write = pci_write_legacy_io;
621 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300622 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000623 error = device_create_bin_file(&b->dev, b->legacy_io);
624 if (error)
625 goto legacy_io_err;
626
627 /* Allocated above after the legacy_io struct */
628 b->legacy_mem = b->legacy_io + 1;
629 b->legacy_mem->attr.name = "legacy_mem";
630 b->legacy_mem->size = 1024*1024;
631 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
632 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300633 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000634 error = device_create_bin_file(&b->dev, b->legacy_mem);
635 if (error)
636 goto legacy_mem_err;
637
638 return;
639
640legacy_mem_err:
641 device_remove_bin_file(&b->dev, b->legacy_io);
642legacy_io_err:
643 kfree(b->legacy_io);
644 b->legacy_io = NULL;
645kzalloc_err:
646 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
647 "and ISA memory resources to sysfs\n");
648 return;
649}
650
651void pci_remove_legacy_files(struct pci_bus *b)
652{
653 if (b->legacy_io) {
654 device_remove_bin_file(&b->dev, b->legacy_io);
655 device_remove_bin_file(&b->dev, b->legacy_mem);
656 kfree(b->legacy_io); /* both are allocated here */
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659#endif /* HAVE_PCI_LEGACY */
660
661#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700662
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700663int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700664{
665 unsigned long nr, start, size;
666
667 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
668 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800669 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700670 if (start < size && size - start >= nr)
671 return 1;
672 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
673 current->comm, start, start+nr, pci_name(pdev), resno, size);
674 return 0;
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/**
678 * pci_mmap_resource - map a PCI resource into user memory space
679 * @kobj: kobject for mapping
680 * @attr: struct bin_attribute for the file being mapped
681 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700682 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
684 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
686static int
687pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700688 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
691 struct device, kobj));
692 struct resource *res = (struct resource *)attr->private;
693 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700694 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000695 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000697 for (i = 0; i < PCI_ROM_RESOURCE; i++)
698 if (res == &pdev->resource[i])
699 break;
700 if (i >= PCI_ROM_RESOURCE)
701 return -ENODEV;
702
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700703 if (!pci_mmap_fits(pdev, i, vma))
704 return -EINVAL;
705
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000706 /* pci_mmap_page_range() expects the same kind of entry as coming
707 * from /proc/bus/pci/ which is a "user visible" value. If this is
708 * different from the resource itself, arch will do necessary fixup.
709 */
710 pci_resource_to_user(pdev, i, res, &start, &end);
711 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
713
Arjan van de Vene8de1482008-10-22 19:55:31 -0700714 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
715 return -EINVAL;
716
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700717 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
718}
719
720static int
721pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
722 struct vm_area_struct *vma)
723{
724 return pci_mmap_resource(kobj, attr, vma, 0);
725}
726
727static int
728pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
729 struct vm_area_struct *vma)
730{
731 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700735 * pci_remove_resource_files - cleanup resource files
736 * @dev: dev to cleanup
737 *
738 * If we created resource files for @dev, remove them from sysfs and
739 * free their resources.
740 */
741static void
742pci_remove_resource_files(struct pci_dev *pdev)
743{
744 int i;
745
746 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
747 struct bin_attribute *res_attr;
748
749 res_attr = pdev->res_attr[i];
750 if (res_attr) {
751 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
752 kfree(res_attr);
753 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700754
755 res_attr = pdev->res_attr_wc[i];
756 if (res_attr) {
757 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
758 kfree(res_attr);
759 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700760 }
761}
762
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700763static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
764{
765 /* allocate attribute structure, piggyback attribute name */
766 int name_len = write_combine ? 13 : 10;
767 struct bin_attribute *res_attr;
768 int retval;
769
770 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
771 if (res_attr) {
772 char *res_attr_name = (char *)(res_attr + 1);
773
774 if (write_combine) {
775 pdev->res_attr_wc[num] = res_attr;
776 sprintf(res_attr_name, "resource%d_wc", num);
777 res_attr->mmap = pci_mmap_resource_wc;
778 } else {
779 pdev->res_attr[num] = res_attr;
780 sprintf(res_attr_name, "resource%d", num);
781 res_attr->mmap = pci_mmap_resource_uc;
782 }
783 res_attr->attr.name = res_attr_name;
784 res_attr->attr.mode = S_IRUSR | S_IWUSR;
785 res_attr->size = pci_resource_len(pdev, num);
786 res_attr->private = &pdev->resource[num];
787 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
788 } else
789 retval = -ENOMEM;
790
791 return retval;
792}
793
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700794/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 * pci_create_resource_files - create resource files in sysfs for @dev
796 * @dev: dev in question
797 *
798 * Walk the resources in @dev creating files for each resource available.
799 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700800static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700803 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* Expose the PCI resources from this device as files */
806 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* skip empty resources */
809 if (!pci_resource_len(pdev, i))
810 continue;
811
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700812 retval = pci_create_attr(pdev, i, 0);
813 /* for prefetchable resources, create a WC mappable file */
814 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
815 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500816
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700817 if (retval) {
818 pci_remove_resource_files(pdev);
819 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300825int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
826void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif /* HAVE_PCI_MMAP */
828
829/**
830 * pci_write_rom - used to enable access to the PCI ROM display
831 * @kobj: kernel object handle
832 * @buf: user input
833 * @off: file offset
834 * @count: number of byte in input
835 *
836 * writing anything except 0 enables it
837 */
838static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800839pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
840 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
843
844 if ((off == 0) && (*buf == '0') && (count == 2))
845 pdev->rom_attr_enabled = 0;
846 else
847 pdev->rom_attr_enabled = 1;
848
849 return count;
850}
851
852/**
853 * pci_read_rom - read a PCI ROM
854 * @kobj: kernel object handle
855 * @buf: where to put the data we read from the ROM
856 * @off: file offset
857 * @count: number of bytes to read
858 *
859 * Put @count bytes starting at @off into @buf from the ROM in the PCI
860 * device corresponding to @kobj.
861 */
862static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800863pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
864 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
867 void __iomem *rom;
868 size_t size;
869
870 if (!pdev->rom_attr_enabled)
871 return -EINVAL;
872
873 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +1100874 if (!rom || !size)
875 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (off >= size)
878 count = 0;
879 else {
880 if (off + count > size)
881 count = size - off;
882
883 memcpy_fromio(buf, rom + off, count);
884 }
885 pci_unmap_rom(pdev, rom);
886
887 return count;
888}
889
890static struct bin_attribute pci_config_attr = {
891 .attr = {
892 .name = "config",
893 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800895 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 .read = pci_read_config,
897 .write = pci_write_config,
898};
899
900static struct bin_attribute pcie_config_attr = {
901 .attr = {
902 .name = "config",
903 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800905 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .read = pci_read_config,
907 .write = pci_write_config,
908};
909
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000910int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000911{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000912 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000913}
914
Zhao, Yu280c73d2008-10-13 20:01:00 +0800915static int pci_create_capabilities_sysfs(struct pci_dev *dev)
916{
917 int retval;
918 struct bin_attribute *attr;
919
920 /* If the device has VPD, try to expose it in sysfs. */
921 if (dev->vpd) {
922 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
923 if (!attr)
924 return -ENOMEM;
925
926 attr->size = dev->vpd->len;
927 attr->attr.name = "vpd";
928 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800929 attr->read = read_vpd_attr;
930 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800931 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
932 if (retval) {
933 kfree(dev->vpd->attr);
934 return retval;
935 }
936 dev->vpd->attr = attr;
937 }
938
939 /* Active State Power Management */
940 pcie_aspm_create_sysfs_dev_files(dev);
941
942 return 0;
943}
944
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700945int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700947 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800948 int rom_size = 0;
949 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!sysfs_initialized)
952 return -EACCES;
953
Zhao, Yu557848c2008-10-13 19:18:07 +0800954 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700955 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700957 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
958 if (retval)
959 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700961 retval = pci_create_resource_files(pdev);
962 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +0800963 goto err_config_file;
964
965 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
966 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
967 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
968 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +0800971 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +0000972 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +0800973 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700974 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +1000975 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800977 attr->size = rom_size;
978 attr->attr.name = "rom";
979 attr->attr.mode = S_IRUSR;
980 attr->read = pci_read_rom;
981 attr->write = pci_write_rom;
982 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
983 if (retval) {
984 kfree(attr);
985 goto err_resource_files;
986 }
987 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800989
Dave Airlie217f45d2009-03-04 05:57:05 +0000990 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
991 retval = device_create_file(&pdev->dev, &vga_attr);
992 if (retval)
993 goto err_rom_file;
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +0800997 retval = pcibios_add_platform_entries(pdev);
998 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +0000999 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001000
Zhao, Yu280c73d2008-10-13 20:01:00 +08001001 /* add sysfs entries for various capabilities */
1002 retval = pci_create_capabilities_sysfs(pdev);
1003 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001004 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001007
Dave Airlie217f45d2009-03-04 05:57:05 +00001008err_vga_file:
1009 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1010 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001011err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001012 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001013 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001014 kfree(pdev->rom_attr);
1015 pdev->rom_attr = NULL;
1016 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001017err_resource_files:
1018 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001019err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001020 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001021 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1022 else
1023 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1024err:
1025 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Zhao, Yu280c73d2008-10-13 20:01:00 +08001028static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1029{
1030 if (dev->vpd && dev->vpd->attr) {
1031 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1032 kfree(dev->vpd->attr);
1033 }
1034
1035 pcie_aspm_remove_sysfs_dev_files(dev);
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/**
1039 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1040 * @pdev: device whose entries we should free
1041 *
1042 * Cleanup when @pdev is removed from sysfs.
1043 */
1044void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1045{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001046 int rom_size = 0;
1047
David Millerd67afe52006-11-10 12:27:48 -08001048 if (!sysfs_initialized)
1049 return;
1050
Zhao, Yu280c73d2008-10-13 20:01:00 +08001051 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001052
Zhao, Yu557848c2008-10-13 19:18:07 +08001053 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1055 else
1056 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1057
1058 pci_remove_resource_files(pdev);
1059
Zhao, Yu280c73d2008-10-13 20:01:00 +08001060 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1061 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1062 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1063 rom_size = 0x20000;
1064
1065 if (rom_size && pdev->rom_attr) {
1066 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1067 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
1071static int __init pci_sysfs_init(void)
1072{
1073 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001074 int retval;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001077 for_each_pci_dev(pdev) {
1078 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001079 if (retval) {
1080 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001081 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001082 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 return 0;
1086}
1087
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001088late_initcall(pci_sysfs_init);