blob: 0fa707e2a0f8c9e54150c9f41cd62d02b82f7782 [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
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020077#ifdef CONFIG_NUMA
78 mask = cpumask_of_node(dev_to_node(dev));
79#else
Mike Travis3be83052009-01-04 05:18:01 -080080 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020081#endif
Mike Travis3be83052009-01-04 05:18:01 -080082 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070083 buf[len++] = '\n';
84 buf[len] = '\0';
85 return len;
86}
87
88
89static ssize_t local_cpulist_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
91{
Mike Travis3be83052009-01-04 05:18:01 -080092 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070093 int len;
94
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020095#ifdef CONFIG_NUMA
96 mask = cpumask_of_node(dev_to_node(dev));
97#else
Mike Travis3be83052009-01-04 05:18:01 -080098 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020099#endif
Mike Travis3be83052009-01-04 05:18:01 -0800100 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -0700101 buf[len++] = '\n';
102 buf[len] = '\0';
103 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106/* show resources */
107static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400108resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct pci_dev * pci_dev = to_pci_dev(dev);
111 char * str = buf;
112 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800113 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700114 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (pci_dev->subordinate)
117 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800118 else
119 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000122 struct resource *res = &pci_dev->resource[i];
123 pci_resource_to_user(pci_dev, i, res, &start, &end);
124 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
125 (unsigned long long)start,
126 (unsigned long long)end,
127 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 return (str - buf);
130}
131
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200132static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700133{
134 struct pci_dev *pci_dev = to_pci_dev(dev);
135
136 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
137 pci_dev->vendor, pci_dev->device,
138 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
139 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
140 (u8)(pci_dev->class));
141}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800142
143static ssize_t is_enabled_store(struct device *dev,
144 struct device_attribute *attr, const char *buf,
145 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200146{
147 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800148 unsigned long val;
149 ssize_t result = strict_strtoul(buf, 0, &val);
150
151 if (result < 0)
152 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200153
154 /* this can crash the machine when done on the "wrong" device */
155 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800156 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200157
Trent Piepho92425a42008-11-30 17:10:12 -0800158 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900159 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800160 pci_disable_device(pdev);
161 else
162 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800163 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800164 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200165
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800166 return result < 0 ? result : count;
167}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200168
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800169static ssize_t is_enabled_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
172 struct pci_dev *pdev;
173
174 pdev = to_pci_dev (dev);
175 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200176}
177
Brice Goglin81bb0e12007-01-28 10:53:40 +0100178#ifdef CONFIG_NUMA
179static ssize_t
180numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
181{
182 return sprintf (buf, "%d\n", dev->numa_node);
183}
184#endif
185
Brice Goglinfe970642006-08-31 01:55:15 -0400186static ssize_t
187msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
188{
189 struct pci_dev *pdev = to_pci_dev(dev);
190
191 if (!pdev->subordinate)
192 return 0;
193
194 return sprintf (buf, "%u\n",
195 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
196}
197
198static ssize_t
199msi_bus_store(struct device *dev, struct device_attribute *attr,
200 const char *buf, size_t count)
201{
202 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800203 unsigned long val;
204
205 if (strict_strtoul(buf, 0, &val) < 0)
206 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400207
208 /* bad things may happen if the no_msi flag is changed
209 * while some drivers are loaded */
210 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800211 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400212
Trent Piepho92425a42008-11-30 17:10:12 -0800213 /* Maybe pci devices without subordinate busses shouldn't even have this
214 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400215 if (!pdev->subordinate)
216 return count;
217
Trent Piepho92425a42008-11-30 17:10:12 -0800218 /* Is the flag going to change, or keep the value it already had? */
219 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
220 !!val) {
221 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400222
Trent Piepho92425a42008-11-30 17:10:12 -0800223 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
224 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400225 }
226
227 return count;
228}
Greg KH98885492005-05-05 11:57:25 -0700229
Alex Chiang705b1aa2009-03-20 14:56:31 -0600230#ifdef CONFIG_HOTPLUG
231static DEFINE_MUTEX(pci_remove_rescan_mutex);
232static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
233 size_t count)
234{
235 unsigned long val;
236 struct pci_bus *b = NULL;
237
238 if (strict_strtoul(buf, 0, &val) < 0)
239 return -EINVAL;
240
241 if (val) {
242 mutex_lock(&pci_remove_rescan_mutex);
243 while ((b = pci_find_next_bus(b)) != NULL)
244 pci_rescan_bus(b);
245 mutex_unlock(&pci_remove_rescan_mutex);
246 }
247 return count;
248}
249
250struct bus_attribute pci_bus_attrs[] = {
251 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
252 __ATTR_NULL
253};
Alex Chiang77c27c72009-03-20 14:56:36 -0600254
Alex Chiang738a6392009-03-20 14:56:41 -0600255static ssize_t
256dev_rescan_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
258{
259 unsigned long val;
260 struct pci_dev *pdev = to_pci_dev(dev);
261
262 if (strict_strtoul(buf, 0, &val) < 0)
263 return -EINVAL;
264
265 if (val) {
266 mutex_lock(&pci_remove_rescan_mutex);
267 pci_rescan_bus(pdev->bus);
268 mutex_unlock(&pci_remove_rescan_mutex);
269 }
270 return count;
271}
272
Alex Chiang77c27c72009-03-20 14:56:36 -0600273static void remove_callback(struct device *dev)
274{
275 struct pci_dev *pdev = to_pci_dev(dev);
276
277 mutex_lock(&pci_remove_rescan_mutex);
278 pci_remove_bus_device(pdev);
279 mutex_unlock(&pci_remove_rescan_mutex);
280}
281
282static ssize_t
283remove_store(struct device *dev, struct device_attribute *dummy,
284 const char *buf, size_t count)
285{
286 int ret = 0;
287 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600288
289 if (strict_strtoul(buf, 0, &val) < 0)
290 return -EINVAL;
291
Alex Chiang77c27c72009-03-20 14:56:36 -0600292 /* An attribute cannot be unregistered by one of its own methods,
293 * so we have to use this roundabout approach.
294 */
295 if (val)
296 ret = device_schedule_callback(dev, remove_callback);
297 if (ret)
298 count = ret;
299 return count;
300}
Alex Chiang705b1aa2009-03-20 14:56:31 -0600301#endif
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303struct device_attribute pci_dev_attrs[] = {
304 __ATTR_RO(resource),
305 __ATTR_RO(vendor),
306 __ATTR_RO(device),
307 __ATTR_RO(subsystem_vendor),
308 __ATTR_RO(subsystem_device),
309 __ATTR_RO(class),
310 __ATTR_RO(irq),
311 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700312 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700313 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100314#ifdef CONFIG_NUMA
315 __ATTR_RO(numa_node),
316#endif
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200317 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700318 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
319 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400320 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600321#ifdef CONFIG_HOTPLUG
322 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600323 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 __ATTR_NULL,
326};
327
328static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000329boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
330{
331 struct pci_dev *pdev = to_pci_dev(dev);
332
333 return sprintf(buf, "%u\n",
334 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
335 IORESOURCE_ROM_SHADOW));
336}
337struct device_attribute vga_attr = __ATTR_RO(boot_vga);
338
339static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800340pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
341 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
344 unsigned int size = 64;
345 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900346 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Several chips lock up trying to read undefined config space */
349 if (capable(CAP_SYS_ADMIN)) {
350 size = dev->cfg_size;
351 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
352 size = 128;
353 }
354
355 if (off > size)
356 return 0;
357 if (off + count > size) {
358 size -= off;
359 count = size;
360 } else {
361 size = count;
362 }
363
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900364 if ((off & 1) && size) {
365 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700366 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900367 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900369 size--;
370 }
371
372 if ((off & 3) && size > 2) {
373 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700374 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900375 data[off - init_off] = val & 0xff;
376 data[off - init_off + 1] = (val >> 8) & 0xff;
377 off += 2;
378 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900382 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700383 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900384 data[off - init_off] = val & 0xff;
385 data[off - init_off + 1] = (val >> 8) & 0xff;
386 data[off - init_off + 2] = (val >> 16) & 0xff;
387 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 off += 4;
389 size -= 4;
390 }
391
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900392 if (size >= 2) {
393 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700394 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900395 data[off - init_off] = val & 0xff;
396 data[off - init_off + 1] = (val >> 8) & 0xff;
397 off += 2;
398 size -= 2;
399 }
400
401 if (size > 0) {
402 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700403 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900404 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 off++;
406 --size;
407 }
408
409 return count;
410}
411
412static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800413pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
414 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
417 unsigned int size = count;
418 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900419 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (off > dev->cfg_size)
422 return 0;
423 if (off + count > dev->cfg_size) {
424 size = dev->cfg_size - off;
425 count = size;
426 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900427
428 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700429 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900431 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900433
434 if ((off & 3) && size > 2) {
435 u16 val = data[off - init_off];
436 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700437 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900438 off += 2;
439 size -= 2;
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900443 u32 val = data[off - init_off];
444 val |= (u32) data[off - init_off + 1] << 8;
445 val |= (u32) data[off - init_off + 2] << 16;
446 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700447 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 off += 4;
449 size -= 4;
450 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900451
452 if (size >= 2) {
453 u16 val = data[off - init_off];
454 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700455 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900456 off += 2;
457 size -= 2;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900460 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700461 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 off++;
463 --size;
464 }
465
466 return count;
467}
468
Ben Hutchings94e61082008-03-05 16:52:39 +0000469static ssize_t
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800470read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000471 char *buf, loff_t off, size_t count)
472{
473 struct pci_dev *dev =
474 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000475
476 if (off > bin_attr->size)
477 count = 0;
478 else if (count > bin_attr->size - off)
479 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000480
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800481 return pci_read_vpd(dev, off, count, buf);
482}
Ben Hutchings94e61082008-03-05 16:52:39 +0000483
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800484static ssize_t
485write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
486 char *buf, loff_t off, size_t count)
487{
488 struct pci_dev *dev =
489 to_pci_dev(container_of(kobj, struct device, kobj));
490
491 if (off > bin_attr->size)
492 count = 0;
493 else if (count > bin_attr->size - off)
494 count = bin_attr->size - off;
495
496 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499#ifdef HAVE_PCI_LEGACY
500/**
501 * pci_read_legacy_io - read byte(s) from legacy I/O port space
502 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700503 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * @buf: buffer to store results
505 * @off: offset into legacy I/O port space
506 * @count: number of bytes to read
507 *
508 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
509 * callback routine (pci_legacy_read).
510 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000511static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800512pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
513 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400516 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 kobj));
518
519 /* Only support 1, 2 or 4 byte accesses */
520 if (count != 1 && count != 2 && count != 4)
521 return -EINVAL;
522
523 return pci_legacy_read(bus, off, (u32 *)buf, count);
524}
525
526/**
527 * pci_write_legacy_io - write byte(s) to legacy I/O port space
528 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700529 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * @buf: buffer containing value to be written
531 * @off: offset into legacy I/O port space
532 * @count: number of bytes to write
533 *
534 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
535 * callback routine (pci_legacy_write).
536 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000537static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800538pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
539 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400542 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 kobj));
544 /* Only support 1, 2 or 4 byte accesses */
545 if (count != 1 && count != 2 && count != 4)
546 return -EINVAL;
547
548 return pci_legacy_write(bus, off, *(u32 *)buf, count);
549}
550
551/**
552 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
553 * @kobj: kobject corresponding to device to be mapped
554 * @attr: struct bin_attribute for this file
555 * @vma: struct vm_area_struct passed to mmap
556 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000557 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * legacy memory space (first meg of bus space) into application virtual
559 * memory space.
560 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000561static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
563 struct vm_area_struct *vma)
564{
565 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400566 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 kobj));
568
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000569 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
570}
571
572/**
573 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
574 * @kobj: kobject corresponding to device to be mapped
575 * @attr: struct bin_attribute for this file
576 * @vma: struct vm_area_struct passed to mmap
577 *
578 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
579 * legacy IO space (first meg of bus space) into application virtual
580 * memory space. Returns -ENOSYS if the operation isn't supported
581 */
582static int
583pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
584 struct vm_area_struct *vma)
585{
586 struct pci_bus *bus = to_pci_bus(container_of(kobj,
587 struct device,
588 kobj));
589
590 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
591}
592
593/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300594 * pci_adjust_legacy_attr - adjustment of legacy file attributes
595 * @b: bus to create files under
596 * @mmap_type: I/O port or memory
597 *
598 * Stub implementation. Can be overridden by arch if necessary.
599 */
600void __weak
601pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
602{
603 return;
604}
605
606/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000607 * pci_create_legacy_files - create legacy I/O port and memory files
608 * @b: bus to create files under
609 *
610 * Some platforms allow access to legacy I/O port and ISA memory space on
611 * a per-bus basis. This routine creates the files and ties them into
612 * their associated read, write and mmap files from pci-sysfs.c
613 *
614 * On error unwind, but don't propogate the error to the caller
615 * as it is ok to set up the PCI bus without these files.
616 */
617void pci_create_legacy_files(struct pci_bus *b)
618{
619 int error;
620
621 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
622 GFP_ATOMIC);
623 if (!b->legacy_io)
624 goto kzalloc_err;
625
626 b->legacy_io->attr.name = "legacy_io";
627 b->legacy_io->size = 0xffff;
628 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
629 b->legacy_io->read = pci_read_legacy_io;
630 b->legacy_io->write = pci_write_legacy_io;
631 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300632 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000633 error = device_create_bin_file(&b->dev, b->legacy_io);
634 if (error)
635 goto legacy_io_err;
636
637 /* Allocated above after the legacy_io struct */
638 b->legacy_mem = b->legacy_io + 1;
639 b->legacy_mem->attr.name = "legacy_mem";
640 b->legacy_mem->size = 1024*1024;
641 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
642 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300643 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000644 error = device_create_bin_file(&b->dev, b->legacy_mem);
645 if (error)
646 goto legacy_mem_err;
647
648 return;
649
650legacy_mem_err:
651 device_remove_bin_file(&b->dev, b->legacy_io);
652legacy_io_err:
653 kfree(b->legacy_io);
654 b->legacy_io = NULL;
655kzalloc_err:
656 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
657 "and ISA memory resources to sysfs\n");
658 return;
659}
660
661void pci_remove_legacy_files(struct pci_bus *b)
662{
663 if (b->legacy_io) {
664 device_remove_bin_file(&b->dev, b->legacy_io);
665 device_remove_bin_file(&b->dev, b->legacy_mem);
666 kfree(b->legacy_io); /* both are allocated here */
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669#endif /* HAVE_PCI_LEGACY */
670
671#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700672
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700673int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700674{
675 unsigned long nr, start, size;
676
677 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
678 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800679 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700680 if (start < size && size - start >= nr)
681 return 1;
682 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
683 current->comm, start, start+nr, pci_name(pdev), resno, size);
684 return 0;
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/**
688 * pci_mmap_resource - map a PCI resource into user memory space
689 * @kobj: kobject for mapping
690 * @attr: struct bin_attribute for the file being mapped
691 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700692 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *
694 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
696static int
697pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700698 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
701 struct device, kobj));
702 struct resource *res = (struct resource *)attr->private;
703 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700704 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000705 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000707 for (i = 0; i < PCI_ROM_RESOURCE; i++)
708 if (res == &pdev->resource[i])
709 break;
710 if (i >= PCI_ROM_RESOURCE)
711 return -ENODEV;
712
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700713 if (!pci_mmap_fits(pdev, i, vma))
714 return -EINVAL;
715
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000716 /* pci_mmap_page_range() expects the same kind of entry as coming
717 * from /proc/bus/pci/ which is a "user visible" value. If this is
718 * different from the resource itself, arch will do necessary fixup.
719 */
720 pci_resource_to_user(pdev, i, res, &start, &end);
721 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
723
Arjan van de Vene8de1482008-10-22 19:55:31 -0700724 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
725 return -EINVAL;
726
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700727 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
728}
729
730static int
731pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
732 struct vm_area_struct *vma)
733{
734 return pci_mmap_resource(kobj, attr, vma, 0);
735}
736
737static int
738pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
739 struct vm_area_struct *vma)
740{
741 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700745 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700746 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700747 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700748 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700749 * free their resources.
750 */
751static void
752pci_remove_resource_files(struct pci_dev *pdev)
753{
754 int i;
755
756 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
757 struct bin_attribute *res_attr;
758
759 res_attr = pdev->res_attr[i];
760 if (res_attr) {
761 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
762 kfree(res_attr);
763 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700764
765 res_attr = pdev->res_attr_wc[i];
766 if (res_attr) {
767 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
768 kfree(res_attr);
769 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700770 }
771}
772
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700773static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
774{
775 /* allocate attribute structure, piggyback attribute name */
776 int name_len = write_combine ? 13 : 10;
777 struct bin_attribute *res_attr;
778 int retval;
779
780 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
781 if (res_attr) {
782 char *res_attr_name = (char *)(res_attr + 1);
783
784 if (write_combine) {
785 pdev->res_attr_wc[num] = res_attr;
786 sprintf(res_attr_name, "resource%d_wc", num);
787 res_attr->mmap = pci_mmap_resource_wc;
788 } else {
789 pdev->res_attr[num] = res_attr;
790 sprintf(res_attr_name, "resource%d", num);
791 res_attr->mmap = pci_mmap_resource_uc;
792 }
793 res_attr->attr.name = res_attr_name;
794 res_attr->attr.mode = S_IRUSR | S_IWUSR;
795 res_attr->size = pci_resource_len(pdev, num);
796 res_attr->private = &pdev->resource[num];
797 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
798 } else
799 retval = -ENOMEM;
800
801 return retval;
802}
803
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700804/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700806 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700808 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700810static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700813 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Expose the PCI resources from this device as files */
816 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* skip empty resources */
819 if (!pci_resource_len(pdev, i))
820 continue;
821
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700822 retval = pci_create_attr(pdev, i, 0);
823 /* for prefetchable resources, create a WC mappable file */
824 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
825 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500826
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700827 if (retval) {
828 pci_remove_resource_files(pdev);
829 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300835int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
836void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837#endif /* HAVE_PCI_MMAP */
838
839/**
840 * pci_write_rom - used to enable access to the PCI ROM display
841 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700842 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * @buf: user input
844 * @off: file offset
845 * @count: number of byte in input
846 *
847 * writing anything except 0 enables it
848 */
849static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800850pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
851 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
854
855 if ((off == 0) && (*buf == '0') && (count == 2))
856 pdev->rom_attr_enabled = 0;
857 else
858 pdev->rom_attr_enabled = 1;
859
860 return count;
861}
862
863/**
864 * pci_read_rom - read a PCI ROM
865 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700866 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 * @buf: where to put the data we read from the ROM
868 * @off: file offset
869 * @count: number of bytes to read
870 *
871 * Put @count bytes starting at @off into @buf from the ROM in the PCI
872 * device corresponding to @kobj.
873 */
874static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800875pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
876 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
879 void __iomem *rom;
880 size_t size;
881
882 if (!pdev->rom_attr_enabled)
883 return -EINVAL;
884
885 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +1100886 if (!rom || !size)
887 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (off >= size)
890 count = 0;
891 else {
892 if (off + count > size)
893 count = size - off;
894
895 memcpy_fromio(buf, rom + off, count);
896 }
897 pci_unmap_rom(pdev, rom);
898
899 return count;
900}
901
902static struct bin_attribute pci_config_attr = {
903 .attr = {
904 .name = "config",
905 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800907 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .read = pci_read_config,
909 .write = pci_write_config,
910};
911
912static struct bin_attribute pcie_config_attr = {
913 .attr = {
914 .name = "config",
915 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800917 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .read = pci_read_config,
919 .write = pci_write_config,
920};
921
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000922int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000923{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000924 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000925}
926
Michael S. Tsirkin711d5772009-07-27 23:37:48 +0300927static ssize_t reset_store(struct device *dev,
928 struct device_attribute *attr, const char *buf,
929 size_t count)
930{
931 struct pci_dev *pdev = to_pci_dev(dev);
932 unsigned long val;
933 ssize_t result = strict_strtoul(buf, 0, &val);
934
935 if (result < 0)
936 return result;
937
938 if (val != 1)
939 return -EINVAL;
940 return pci_reset_function(pdev);
941}
942
943static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
944
Zhao, Yu280c73d2008-10-13 20:01:00 +0800945static int pci_create_capabilities_sysfs(struct pci_dev *dev)
946{
947 int retval;
948 struct bin_attribute *attr;
949
950 /* If the device has VPD, try to expose it in sysfs. */
951 if (dev->vpd) {
952 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
953 if (!attr)
954 return -ENOMEM;
955
956 attr->size = dev->vpd->len;
957 attr->attr.name = "vpd";
958 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800959 attr->read = read_vpd_attr;
960 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800961 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
962 if (retval) {
963 kfree(dev->vpd->attr);
964 return retval;
965 }
966 dev->vpd->attr = attr;
967 }
968
969 /* Active State Power Management */
970 pcie_aspm_create_sysfs_dev_files(dev);
971
Michael S. Tsirkin711d5772009-07-27 23:37:48 +0300972 if (!pci_probe_reset_function(dev)) {
973 retval = device_create_file(&dev->dev, &reset_attr);
974 if (retval)
975 goto error;
976 dev->reset_fn = 1;
977 }
Zhao, Yu280c73d2008-10-13 20:01:00 +0800978 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +0300979
980error:
981 pcie_aspm_remove_sysfs_dev_files(dev);
982 if (dev->vpd && dev->vpd->attr) {
983 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
984 kfree(dev->vpd->attr);
985 }
986
987 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800988}
989
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700990int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700992 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800993 int rom_size = 0;
994 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!sysfs_initialized)
997 return -EACCES;
998
Zhao, Yu557848c2008-10-13 19:18:07 +0800999 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001000 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001002 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1003 if (retval)
1004 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001006 retval = pci_create_resource_files(pdev);
1007 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001008 goto err_config_file;
1009
1010 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1011 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1012 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1013 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001016 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001017 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001018 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001019 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001020 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001022 attr->size = rom_size;
1023 attr->attr.name = "rom";
1024 attr->attr.mode = S_IRUSR;
1025 attr->read = pci_read_rom;
1026 attr->write = pci_write_rom;
1027 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1028 if (retval) {
1029 kfree(attr);
1030 goto err_resource_files;
1031 }
1032 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001034
Dave Airlie217f45d2009-03-04 05:57:05 +00001035 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1036 retval = device_create_file(&pdev->dev, &vga_attr);
1037 if (retval)
1038 goto err_rom_file;
1039 }
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001042 retval = pcibios_add_platform_entries(pdev);
1043 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001044 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001045
Zhao, Yu280c73d2008-10-13 20:01:00 +08001046 /* add sysfs entries for various capabilities */
1047 retval = pci_create_capabilities_sysfs(pdev);
1048 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001049 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001052
Dave Airlie217f45d2009-03-04 05:57:05 +00001053err_vga_file:
1054 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1055 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001056err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001057 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001058 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001059 kfree(pdev->rom_attr);
1060 pdev->rom_attr = NULL;
1061 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001062err_resource_files:
1063 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001064err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001065 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001066 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1067 else
1068 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1069err:
1070 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Zhao, Yu280c73d2008-10-13 20:01:00 +08001073static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1074{
1075 if (dev->vpd && dev->vpd->attr) {
1076 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1077 kfree(dev->vpd->attr);
1078 }
1079
1080 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001081 if (dev->reset_fn) {
1082 device_remove_file(&dev->dev, &reset_attr);
1083 dev->reset_fn = 0;
1084 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/**
1088 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1089 * @pdev: device whose entries we should free
1090 *
1091 * Cleanup when @pdev is removed from sysfs.
1092 */
1093void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1094{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001095 int rom_size = 0;
1096
David Millerd67afe52006-11-10 12:27:48 -08001097 if (!sysfs_initialized)
1098 return;
1099
Zhao, Yu280c73d2008-10-13 20:01:00 +08001100 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001101
Zhao, Yu557848c2008-10-13 19:18:07 +08001102 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1104 else
1105 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1106
1107 pci_remove_resource_files(pdev);
1108
Zhao, Yu280c73d2008-10-13 20:01:00 +08001109 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1110 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1111 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1112 rom_size = 0x20000;
1113
1114 if (rom_size && pdev->rom_attr) {
1115 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1116 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118}
1119
1120static int __init pci_sysfs_init(void)
1121{
1122 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001123 int retval;
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001126 for_each_pci_dev(pdev) {
1127 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001128 if (retval) {
1129 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001130 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001131 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 return 0;
1135}
1136
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001137late_initcall(pci_sysfs_init);