blob: fad93983bfed09a2780822b8a8c36bad18a32828 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "pci.h"
28
29static int sysfs_initialized; /* = 0 */
30
31/* show configuration fields */
32#define pci_config_attr(field, format_string) \
33static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040034field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{ \
36 struct pci_dev *pdev; \
37 \
38 pdev = to_pci_dev (dev); \
39 return sprintf (buf, format_string, pdev->field); \
40}
41
42pci_config_attr(vendor, "0x%04x\n");
43pci_config_attr(device, "0x%04x\n");
44pci_config_attr(subsystem_vendor, "0x%04x\n");
45pci_config_attr(subsystem_device, "0x%04x\n");
46pci_config_attr(class, "0x%06x\n");
47pci_config_attr(irq, "%u\n");
48
Doug Thompsonbdee9d92006-06-14 16:59:48 -070049static ssize_t broken_parity_status_show(struct device *dev,
50 struct device_attribute *attr,
51 char *buf)
52{
53 struct pci_dev *pdev = to_pci_dev(dev);
54 return sprintf (buf, "%u\n", pdev->broken_parity_status);
55}
56
57static ssize_t broken_parity_status_store(struct device *dev,
58 struct device_attribute *attr,
59 const char *buf, size_t count)
60{
61 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080062 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070063
Trent Piepho92425a42008-11-30 17:10:12 -080064 if (strict_strtoul(buf, 0, &val) < 0)
65 return -EINVAL;
66
67 pdev->broken_parity_status = !!val;
68
69 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070070}
71
Alan Cox4327edf2005-09-10 00:25:49 -070072static ssize_t local_cpus_show(struct device *dev,
73 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Mike Travis3be83052009-01-04 05:18:01 -080075 const struct cpumask *mask;
Alan Cox4327edf2005-09-10 00:25:49 -070076 int len;
77
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020078#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053079 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
80 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020081#else
Mike Travis3be83052009-01-04 05:18:01 -080082 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020083#endif
Mike Travis3be83052009-01-04 05:18:01 -080084 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070085 buf[len++] = '\n';
86 buf[len] = '\0';
87 return len;
88}
89
90
91static ssize_t local_cpulist_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
Mike Travis3be83052009-01-04 05:18:01 -080094 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070095 int len;
96
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020097#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053098 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
99 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200100#else
Mike Travis3be83052009-01-04 05:18:01 -0800101 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200102#endif
Mike Travis3be83052009-01-04 05:18:01 -0800103 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -0700104 buf[len++] = '\n';
105 buf[len] = '\0';
106 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/* show resources */
110static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400111resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct pci_dev * pci_dev = to_pci_dev(dev);
114 char * str = buf;
115 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800116 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700117 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (pci_dev->subordinate)
120 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800121 else
122 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000125 struct resource *res = &pci_dev->resource[i];
126 pci_resource_to_user(pci_dev, i, res, &start, &end);
127 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
128 (unsigned long long)start,
129 (unsigned long long)end,
130 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 return (str - buf);
133}
134
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200135static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700136{
137 struct pci_dev *pci_dev = to_pci_dev(dev);
138
139 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
140 pci_dev->vendor, pci_dev->device,
141 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
142 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
143 (u8)(pci_dev->class));
144}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800145
146static ssize_t is_enabled_store(struct device *dev,
147 struct device_attribute *attr, const char *buf,
148 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200149{
150 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800151 unsigned long val;
152 ssize_t result = strict_strtoul(buf, 0, &val);
153
154 if (result < 0)
155 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200156
157 /* this can crash the machine when done on the "wrong" device */
158 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800159 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200160
Trent Piepho92425a42008-11-30 17:10:12 -0800161 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900162 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800163 pci_disable_device(pdev);
164 else
165 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800166 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800167 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200168
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800169 return result < 0 ? result : count;
170}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200171
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800172static ssize_t is_enabled_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 struct pci_dev *pdev;
176
177 pdev = to_pci_dev (dev);
178 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200179}
180
Brice Goglin81bb0e12007-01-28 10:53:40 +0100181#ifdef CONFIG_NUMA
182static ssize_t
183numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
184{
185 return sprintf (buf, "%d\n", dev->numa_node);
186}
187#endif
188
Brice Goglinfe970642006-08-31 01:55:15 -0400189static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800190dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
191{
192 struct pci_dev *pdev = to_pci_dev(dev);
193
194 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
195}
196
197static ssize_t
198consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
199 char *buf)
200{
201 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
202}
203
204static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400205msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
206{
207 struct pci_dev *pdev = to_pci_dev(dev);
208
209 if (!pdev->subordinate)
210 return 0;
211
212 return sprintf (buf, "%u\n",
213 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
214}
215
216static ssize_t
217msi_bus_store(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
219{
220 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800221 unsigned long val;
222
223 if (strict_strtoul(buf, 0, &val) < 0)
224 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400225
226 /* bad things may happen if the no_msi flag is changed
227 * while some drivers are loaded */
228 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800229 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400230
Trent Piepho92425a42008-11-30 17:10:12 -0800231 /* Maybe pci devices without subordinate busses shouldn't even have this
232 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400233 if (!pdev->subordinate)
234 return count;
235
Trent Piepho92425a42008-11-30 17:10:12 -0800236 /* Is the flag going to change, or keep the value it already had? */
237 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
238 !!val) {
239 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400240
Trent Piepho92425a42008-11-30 17:10:12 -0800241 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
242 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400243 }
244
245 return count;
246}
Greg KH98885492005-05-05 11:57:25 -0700247
Alex Chiang705b1aa2009-03-20 14:56:31 -0600248#ifdef CONFIG_HOTPLUG
249static DEFINE_MUTEX(pci_remove_rescan_mutex);
250static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
251 size_t count)
252{
253 unsigned long val;
254 struct pci_bus *b = NULL;
255
256 if (strict_strtoul(buf, 0, &val) < 0)
257 return -EINVAL;
258
259 if (val) {
260 mutex_lock(&pci_remove_rescan_mutex);
261 while ((b = pci_find_next_bus(b)) != NULL)
262 pci_rescan_bus(b);
263 mutex_unlock(&pci_remove_rescan_mutex);
264 }
265 return count;
266}
267
268struct bus_attribute pci_bus_attrs[] = {
269 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
270 __ATTR_NULL
271};
Alex Chiang77c27c72009-03-20 14:56:36 -0600272
Alex Chiang738a6392009-03-20 14:56:41 -0600273static ssize_t
274dev_rescan_store(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t count)
276{
277 unsigned long val;
278 struct pci_dev *pdev = to_pci_dev(dev);
279
280 if (strict_strtoul(buf, 0, &val) < 0)
281 return -EINVAL;
282
283 if (val) {
284 mutex_lock(&pci_remove_rescan_mutex);
285 pci_rescan_bus(pdev->bus);
286 mutex_unlock(&pci_remove_rescan_mutex);
287 }
288 return count;
289}
290
Alex Chiang77c27c72009-03-20 14:56:36 -0600291static void remove_callback(struct device *dev)
292{
293 struct pci_dev *pdev = to_pci_dev(dev);
294
295 mutex_lock(&pci_remove_rescan_mutex);
296 pci_remove_bus_device(pdev);
297 mutex_unlock(&pci_remove_rescan_mutex);
298}
299
300static ssize_t
301remove_store(struct device *dev, struct device_attribute *dummy,
302 const char *buf, size_t count)
303{
304 int ret = 0;
305 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600306
307 if (strict_strtoul(buf, 0, &val) < 0)
308 return -EINVAL;
309
Alex Chiang77c27c72009-03-20 14:56:36 -0600310 /* An attribute cannot be unregistered by one of its own methods,
311 * so we have to use this roundabout approach.
312 */
313 if (val)
314 ret = device_schedule_callback(dev, remove_callback);
315 if (ret)
316 count = ret;
317 return count;
318}
Alex Chiang705b1aa2009-03-20 14:56:31 -0600319#endif
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321struct device_attribute pci_dev_attrs[] = {
322 __ATTR_RO(resource),
323 __ATTR_RO(vendor),
324 __ATTR_RO(device),
325 __ATTR_RO(subsystem_vendor),
326 __ATTR_RO(subsystem_device),
327 __ATTR_RO(class),
328 __ATTR_RO(irq),
329 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700330 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700331 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100332#ifdef CONFIG_NUMA
333 __ATTR_RO(numa_node),
334#endif
Yinghai Lubb965402009-11-24 18:21:21 -0800335 __ATTR_RO(dma_mask_bits),
336 __ATTR_RO(consistent_dma_mask_bits),
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200337 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700338 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
339 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400340 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600341#ifdef CONFIG_HOTPLUG
342 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600343 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 __ATTR_NULL,
346};
347
348static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000349boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
350{
351 struct pci_dev *pdev = to_pci_dev(dev);
352
353 return sprintf(buf, "%u\n",
354 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
355 IORESOURCE_ROM_SHADOW));
356}
357struct device_attribute vga_attr = __ATTR_RO(boot_vga);
358
359static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800360pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
361 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
364 unsigned int size = 64;
365 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900366 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* Several chips lock up trying to read undefined config space */
369 if (capable(CAP_SYS_ADMIN)) {
370 size = dev->cfg_size;
371 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
372 size = 128;
373 }
374
375 if (off > size)
376 return 0;
377 if (off + count > size) {
378 size -= off;
379 count = size;
380 } else {
381 size = count;
382 }
383
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900384 if ((off & 1) && size) {
385 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700386 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900387 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900389 size--;
390 }
391
392 if ((off & 3) && 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900402 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700403 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900404 data[off - init_off] = val & 0xff;
405 data[off - init_off + 1] = (val >> 8) & 0xff;
406 data[off - init_off + 2] = (val >> 16) & 0xff;
407 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 off += 4;
409 size -= 4;
410 }
411
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900412 if (size >= 2) {
413 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700414 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900415 data[off - init_off] = val & 0xff;
416 data[off - init_off + 1] = (val >> 8) & 0xff;
417 off += 2;
418 size -= 2;
419 }
420
421 if (size > 0) {
422 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700423 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900424 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 off++;
426 --size;
427 }
428
429 return count;
430}
431
432static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800433pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
434 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
437 unsigned int size = count;
438 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900439 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (off > dev->cfg_size)
442 return 0;
443 if (off + count > dev->cfg_size) {
444 size = dev->cfg_size - off;
445 count = size;
446 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900447
448 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700449 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900451 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900453
454 if ((off & 3) && size > 2) {
455 u16 val = data[off - init_off];
456 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700457 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900458 off += 2;
459 size -= 2;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900463 u32 val = data[off - init_off];
464 val |= (u32) data[off - init_off + 1] << 8;
465 val |= (u32) data[off - init_off + 2] << 16;
466 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700467 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 off += 4;
469 size -= 4;
470 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900471
472 if (size >= 2) {
473 u16 val = data[off - init_off];
474 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700475 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900476 off += 2;
477 size -= 2;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900480 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700481 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 off++;
483 --size;
484 }
485
486 return count;
487}
488
Ben Hutchings94e61082008-03-05 16:52:39 +0000489static ssize_t
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800490read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000491 char *buf, loff_t off, size_t count)
492{
493 struct pci_dev *dev =
494 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000495
496 if (off > bin_attr->size)
497 count = 0;
498 else if (count > bin_attr->size - off)
499 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000500
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800501 return pci_read_vpd(dev, off, count, buf);
502}
Ben Hutchings94e61082008-03-05 16:52:39 +0000503
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800504static ssize_t
505write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
506 char *buf, loff_t off, size_t count)
507{
508 struct pci_dev *dev =
509 to_pci_dev(container_of(kobj, struct device, kobj));
510
511 if (off > bin_attr->size)
512 count = 0;
513 else if (count > bin_attr->size - off)
514 count = bin_attr->size - off;
515
516 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#ifdef HAVE_PCI_LEGACY
520/**
521 * pci_read_legacy_io - read byte(s) from legacy I/O port space
522 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700523 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * @buf: buffer to store results
525 * @off: offset into legacy I/O port space
526 * @count: number of bytes to read
527 *
528 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
529 * callback routine (pci_legacy_read).
530 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000531static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800532pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
533 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400536 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 kobj));
538
539 /* Only support 1, 2 or 4 byte accesses */
540 if (count != 1 && count != 2 && count != 4)
541 return -EINVAL;
542
543 return pci_legacy_read(bus, off, (u32 *)buf, count);
544}
545
546/**
547 * pci_write_legacy_io - write byte(s) to legacy I/O port space
548 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700549 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * @buf: buffer containing value to be written
551 * @off: offset into legacy I/O port space
552 * @count: number of bytes to write
553 *
554 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
555 * callback routine (pci_legacy_write).
556 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000557static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800558pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
559 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400562 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 kobj));
564 /* Only support 1, 2 or 4 byte accesses */
565 if (count != 1 && count != 2 && count != 4)
566 return -EINVAL;
567
568 return pci_legacy_write(bus, off, *(u32 *)buf, count);
569}
570
571/**
572 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
573 * @kobj: kobject corresponding to device to be mapped
574 * @attr: struct bin_attribute for this file
575 * @vma: struct vm_area_struct passed to mmap
576 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000577 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * legacy memory space (first meg of bus space) into application virtual
579 * memory space.
580 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000581static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
583 struct vm_area_struct *vma)
584{
585 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400586 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 kobj));
588
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000589 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
590}
591
592/**
593 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
594 * @kobj: kobject corresponding to device to be mapped
595 * @attr: struct bin_attribute for this file
596 * @vma: struct vm_area_struct passed to mmap
597 *
598 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
599 * legacy IO space (first meg of bus space) into application virtual
600 * memory space. Returns -ENOSYS if the operation isn't supported
601 */
602static int
603pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
604 struct vm_area_struct *vma)
605{
606 struct pci_bus *bus = to_pci_bus(container_of(kobj,
607 struct device,
608 kobj));
609
610 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
611}
612
613/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300614 * pci_adjust_legacy_attr - adjustment of legacy file attributes
615 * @b: bus to create files under
616 * @mmap_type: I/O port or memory
617 *
618 * Stub implementation. Can be overridden by arch if necessary.
619 */
620void __weak
621pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
622{
623 return;
624}
625
626/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000627 * pci_create_legacy_files - create legacy I/O port and memory files
628 * @b: bus to create files under
629 *
630 * Some platforms allow access to legacy I/O port and ISA memory space on
631 * a per-bus basis. This routine creates the files and ties them into
632 * their associated read, write and mmap files from pci-sysfs.c
633 *
634 * On error unwind, but don't propogate the error to the caller
635 * as it is ok to set up the PCI bus without these files.
636 */
637void pci_create_legacy_files(struct pci_bus *b)
638{
639 int error;
640
641 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
642 GFP_ATOMIC);
643 if (!b->legacy_io)
644 goto kzalloc_err;
645
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100646 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000647 b->legacy_io->attr.name = "legacy_io";
648 b->legacy_io->size = 0xffff;
649 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
650 b->legacy_io->read = pci_read_legacy_io;
651 b->legacy_io->write = pci_write_legacy_io;
652 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300653 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000654 error = device_create_bin_file(&b->dev, b->legacy_io);
655 if (error)
656 goto legacy_io_err;
657
658 /* Allocated above after the legacy_io struct */
659 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000660 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000661 b->legacy_mem->attr.name = "legacy_mem";
662 b->legacy_mem->size = 1024*1024;
663 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
664 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300665 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000666 error = device_create_bin_file(&b->dev, b->legacy_mem);
667 if (error)
668 goto legacy_mem_err;
669
670 return;
671
672legacy_mem_err:
673 device_remove_bin_file(&b->dev, b->legacy_io);
674legacy_io_err:
675 kfree(b->legacy_io);
676 b->legacy_io = NULL;
677kzalloc_err:
678 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
679 "and ISA memory resources to sysfs\n");
680 return;
681}
682
683void pci_remove_legacy_files(struct pci_bus *b)
684{
685 if (b->legacy_io) {
686 device_remove_bin_file(&b->dev, b->legacy_io);
687 device_remove_bin_file(&b->dev, b->legacy_mem);
688 kfree(b->legacy_io); /* both are allocated here */
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691#endif /* HAVE_PCI_LEGACY */
692
693#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700694
Jesse Barnes9eff02e2008-10-24 10:32:33 -0700695int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700696{
697 unsigned long nr, start, size;
698
699 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
700 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800701 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700702 if (start < size && size - start >= nr)
703 return 1;
704 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
705 current->comm, start, start+nr, pci_name(pdev), resno, size);
706 return 0;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/**
710 * pci_mmap_resource - map a PCI resource into user memory space
711 * @kobj: kobject for mapping
712 * @attr: struct bin_attribute for the file being mapped
713 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700714 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 *
716 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
718static int
719pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700720 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
723 struct device, kobj));
724 struct resource *res = (struct resource *)attr->private;
725 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700726 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000727 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000729 for (i = 0; i < PCI_ROM_RESOURCE; i++)
730 if (res == &pdev->resource[i])
731 break;
732 if (i >= PCI_ROM_RESOURCE)
733 return -ENODEV;
734
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700735 if (!pci_mmap_fits(pdev, i, vma))
736 return -EINVAL;
737
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000738 /* pci_mmap_page_range() expects the same kind of entry as coming
739 * from /proc/bus/pci/ which is a "user visible" value. If this is
740 * different from the resource itself, arch will do necessary fixup.
741 */
742 pci_resource_to_user(pdev, i, res, &start, &end);
743 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
745
Arjan van de Vene8de1482008-10-22 19:55:31 -0700746 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
747 return -EINVAL;
748
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700749 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
750}
751
752static int
753pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
754 struct vm_area_struct *vma)
755{
756 return pci_mmap_resource(kobj, attr, vma, 0);
757}
758
759static int
760pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
761 struct vm_area_struct *vma)
762{
763 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700767 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700768 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700769 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700770 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700771 * free their resources.
772 */
773static void
774pci_remove_resource_files(struct pci_dev *pdev)
775{
776 int i;
777
778 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
779 struct bin_attribute *res_attr;
780
781 res_attr = pdev->res_attr[i];
782 if (res_attr) {
783 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
784 kfree(res_attr);
785 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700786
787 res_attr = pdev->res_attr_wc[i];
788 if (res_attr) {
789 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
790 kfree(res_attr);
791 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700792 }
793}
794
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700795static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
796{
797 /* allocate attribute structure, piggyback attribute name */
798 int name_len = write_combine ? 13 : 10;
799 struct bin_attribute *res_attr;
800 int retval;
801
802 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
803 if (res_attr) {
804 char *res_attr_name = (char *)(res_attr + 1);
805
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800806 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700807 if (write_combine) {
808 pdev->res_attr_wc[num] = res_attr;
809 sprintf(res_attr_name, "resource%d_wc", num);
810 res_attr->mmap = pci_mmap_resource_wc;
811 } else {
812 pdev->res_attr[num] = res_attr;
813 sprintf(res_attr_name, "resource%d", num);
814 res_attr->mmap = pci_mmap_resource_uc;
815 }
816 res_attr->attr.name = res_attr_name;
817 res_attr->attr.mode = S_IRUSR | S_IWUSR;
818 res_attr->size = pci_resource_len(pdev, num);
819 res_attr->private = &pdev->resource[num];
820 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
821 } else
822 retval = -ENOMEM;
823
824 return retval;
825}
826
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700827/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700829 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700831 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700833static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700836 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /* Expose the PCI resources from this device as files */
839 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* skip empty resources */
842 if (!pci_resource_len(pdev, i))
843 continue;
844
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700845 retval = pci_create_attr(pdev, i, 0);
846 /* for prefetchable resources, create a WC mappable file */
847 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
848 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500849
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700850 if (retval) {
851 pci_remove_resource_files(pdev);
852 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300858int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
859void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#endif /* HAVE_PCI_MMAP */
861
862/**
863 * pci_write_rom - used to enable access to the PCI ROM display
864 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700865 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * @buf: user input
867 * @off: file offset
868 * @count: number of byte in input
869 *
870 * writing anything except 0 enables it
871 */
872static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800873pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
874 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
877
878 if ((off == 0) && (*buf == '0') && (count == 2))
879 pdev->rom_attr_enabled = 0;
880 else
881 pdev->rom_attr_enabled = 1;
882
883 return count;
884}
885
886/**
887 * pci_read_rom - read a PCI ROM
888 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700889 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * @buf: where to put the data we read from the ROM
891 * @off: file offset
892 * @count: number of bytes to read
893 *
894 * Put @count bytes starting at @off into @buf from the ROM in the PCI
895 * device corresponding to @kobj.
896 */
897static ssize_t
Zhang Rui91a69022007-06-09 13:57:22 +0800898pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
899 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
902 void __iomem *rom;
903 size_t size;
904
905 if (!pdev->rom_attr_enabled)
906 return -EINVAL;
907
908 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +1100909 if (!rom || !size)
910 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (off >= size)
913 count = 0;
914 else {
915 if (off + count > size)
916 count = size - off;
917
918 memcpy_fromio(buf, rom + off, count);
919 }
920 pci_unmap_rom(pdev, rom);
921
922 return count;
923}
924
925static struct bin_attribute pci_config_attr = {
926 .attr = {
927 .name = "config",
928 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800930 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 .read = pci_read_config,
932 .write = pci_write_config,
933};
934
935static struct bin_attribute pcie_config_attr = {
936 .attr = {
937 .name = "config",
938 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 },
Zhao, Yu557848c2008-10-13 19:18:07 +0800940 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 .read = pci_read_config,
942 .write = pci_write_config,
943};
944
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000945int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +1000946{
Michael Ellermana2cd52c2007-05-08 12:03:08 +1000947 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +1000948}
949
Michael S. Tsirkin711d5772009-07-27 23:37:48 +0300950static ssize_t reset_store(struct device *dev,
951 struct device_attribute *attr, const char *buf,
952 size_t count)
953{
954 struct pci_dev *pdev = to_pci_dev(dev);
955 unsigned long val;
956 ssize_t result = strict_strtoul(buf, 0, &val);
957
958 if (result < 0)
959 return result;
960
961 if (val != 1)
962 return -EINVAL;
963 return pci_reset_function(pdev);
964}
965
966static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
967
Zhao, Yu280c73d2008-10-13 20:01:00 +0800968static int pci_create_capabilities_sysfs(struct pci_dev *dev)
969{
970 int retval;
971 struct bin_attribute *attr;
972
973 /* If the device has VPD, try to expose it in sysfs. */
974 if (dev->vpd) {
975 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
976 if (!attr)
977 return -ENOMEM;
978
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800979 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +0800980 attr->size = dev->vpd->len;
981 attr->attr.name = "vpd";
982 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800983 attr->read = read_vpd_attr;
984 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +0800985 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
986 if (retval) {
987 kfree(dev->vpd->attr);
988 return retval;
989 }
990 dev->vpd->attr = attr;
991 }
992
993 /* Active State Power Management */
994 pcie_aspm_create_sysfs_dev_files(dev);
995
Michael S. Tsirkin711d5772009-07-27 23:37:48 +0300996 if (!pci_probe_reset_function(dev)) {
997 retval = device_create_file(&dev->dev, &reset_attr);
998 if (retval)
999 goto error;
1000 dev->reset_fn = 1;
1001 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001002 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001003
1004error:
1005 pcie_aspm_remove_sysfs_dev_files(dev);
1006 if (dev->vpd && dev->vpd->attr) {
1007 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1008 kfree(dev->vpd->attr);
1009 }
1010
1011 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001012}
1013
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001014int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001016 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001017 int rom_size = 0;
1018 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!sysfs_initialized)
1021 return -EACCES;
1022
Zhao, Yu557848c2008-10-13 19:18:07 +08001023 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001024 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001026 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1027 if (retval)
1028 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001030 retval = pci_create_resource_files(pdev);
1031 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001032 goto err_config_file;
1033
1034 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1035 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1036 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1037 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001040 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001041 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001042 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001043 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001044 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001046 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001047 attr->size = rom_size;
1048 attr->attr.name = "rom";
1049 attr->attr.mode = S_IRUSR;
1050 attr->read = pci_read_rom;
1051 attr->write = pci_write_rom;
1052 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1053 if (retval) {
1054 kfree(attr);
1055 goto err_resource_files;
1056 }
1057 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001059
Dave Airlie217f45d2009-03-04 05:57:05 +00001060 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1061 retval = device_create_file(&pdev->dev, &vga_attr);
1062 if (retval)
1063 goto err_rom_file;
1064 }
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001067 retval = pcibios_add_platform_entries(pdev);
1068 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001069 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001070
Zhao, Yu280c73d2008-10-13 20:01:00 +08001071 /* add sysfs entries for various capabilities */
1072 retval = pci_create_capabilities_sysfs(pdev);
1073 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001074 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001077
Dave Airlie217f45d2009-03-04 05:57:05 +00001078err_vga_file:
1079 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1080 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001081err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001082 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001083 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001084 kfree(pdev->rom_attr);
1085 pdev->rom_attr = NULL;
1086 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001087err_resource_files:
1088 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001089err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001090 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001091 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1092 else
1093 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1094err:
1095 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Zhao, Yu280c73d2008-10-13 20:01:00 +08001098static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1099{
1100 if (dev->vpd && dev->vpd->attr) {
1101 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1102 kfree(dev->vpd->attr);
1103 }
1104
1105 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001106 if (dev->reset_fn) {
1107 device_remove_file(&dev->dev, &reset_attr);
1108 dev->reset_fn = 0;
1109 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/**
1113 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1114 * @pdev: device whose entries we should free
1115 *
1116 * Cleanup when @pdev is removed from sysfs.
1117 */
1118void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1119{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001120 int rom_size = 0;
1121
David Millerd67afe52006-11-10 12:27:48 -08001122 if (!sysfs_initialized)
1123 return;
1124
Zhao, Yu280c73d2008-10-13 20:01:00 +08001125 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001126
Zhao, Yu557848c2008-10-13 19:18:07 +08001127 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1129 else
1130 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1131
1132 pci_remove_resource_files(pdev);
1133
Zhao, Yu280c73d2008-10-13 20:01:00 +08001134 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1135 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1136 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1137 rom_size = 0x20000;
1138
1139 if (rom_size && pdev->rom_attr) {
1140 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1141 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143}
1144
1145static int __init pci_sysfs_init(void)
1146{
1147 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001148 int retval;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001151 for_each_pci_dev(pdev) {
1152 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001153 if (retval) {
1154 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001155 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001156 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 return 0;
1160}
1161
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001162late_initcall(pci_sysfs_init);