blob: 106be0d08f81ff7b9306d0ad364b86786349a5fe [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>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/topology.h>
24#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070025#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070026#include <linux/capability.h>
Chris Wrighta628e7b2011-02-14 17:21:49 -080027#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080028#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "pci.h"
31
32static int sysfs_initialized; /* = 0 */
33
34/* show configuration fields */
35#define pci_config_attr(field, format_string) \
36static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040037field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{ \
39 struct pci_dev *pdev; \
40 \
41 pdev = to_pci_dev (dev); \
42 return sprintf (buf, format_string, pdev->field); \
43}
44
45pci_config_attr(vendor, "0x%04x\n");
46pci_config_attr(device, "0x%04x\n");
47pci_config_attr(subsystem_vendor, "0x%04x\n");
48pci_config_attr(subsystem_device, "0x%04x\n");
49pci_config_attr(class, "0x%06x\n");
50pci_config_attr(irq, "%u\n");
51
Doug Thompsonbdee9d92006-06-14 16:59:48 -070052static ssize_t broken_parity_status_show(struct device *dev,
53 struct device_attribute *attr,
54 char *buf)
55{
56 struct pci_dev *pdev = to_pci_dev(dev);
57 return sprintf (buf, "%u\n", pdev->broken_parity_status);
58}
59
60static ssize_t broken_parity_status_store(struct device *dev,
61 struct device_attribute *attr,
62 const char *buf, size_t count)
63{
64 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080065 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070066
Trent Piepho92425a42008-11-30 17:10:12 -080067 if (strict_strtoul(buf, 0, &val) < 0)
68 return -EINVAL;
69
70 pdev->broken_parity_status = !!val;
71
72 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070073}
74
Alan Cox4327edf2005-09-10 00:25:49 -070075static ssize_t local_cpus_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Mike Travis3be83052009-01-04 05:18:01 -080078 const struct cpumask *mask;
Alan Cox4327edf2005-09-10 00:25:49 -070079 int len;
80
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020081#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053082 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
83 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020084#else
Mike Travis3be83052009-01-04 05:18:01 -080085 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020086#endif
Mike Travis3be83052009-01-04 05:18:01 -080087 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070088 buf[len++] = '\n';
89 buf[len] = '\0';
90 return len;
91}
92
93
94static ssize_t local_cpulist_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
96{
Mike Travis3be83052009-01-04 05:18:01 -080097 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070098 int len;
99
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200100#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +0530101 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
102 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200103#else
Mike Travis3be83052009-01-04 05:18:01 -0800104 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200105#endif
Mike Travis3be83052009-01-04 05:18:01 -0800106 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -0700107 buf[len++] = '\n';
108 buf[len] = '\0';
109 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700112/*
113 * PCI Bus Class Devices
114 */
115static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
116 int type,
117 struct device_attribute *attr,
118 char *buf)
119{
120 int ret;
121 const struct cpumask *cpumask;
122
123 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
124 ret = type ?
125 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
126 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
127 buf[ret++] = '\n';
128 buf[ret] = '\0';
129 return ret;
130}
131
132static inline ssize_t pci_bus_show_cpumaskaffinity(struct device *dev,
133 struct device_attribute *attr,
134 char *buf)
135{
136 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
137}
138
139static inline ssize_t pci_bus_show_cpulistaffinity(struct device *dev,
140 struct device_attribute *attr,
141 char *buf)
142{
143 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* show resources */
147static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400148resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct pci_dev * pci_dev = to_pci_dev(dev);
151 char * str = buf;
152 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800153 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700154 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (pci_dev->subordinate)
157 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800158 else
159 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000162 struct resource *res = &pci_dev->resource[i];
163 pci_resource_to_user(pci_dev, i, res, &start, &end);
164 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
165 (unsigned long long)start,
166 (unsigned long long)end,
167 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 return (str - buf);
170}
171
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200172static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700173{
174 struct pci_dev *pci_dev = to_pci_dev(dev);
175
176 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
177 pci_dev->vendor, pci_dev->device,
178 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
179 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
180 (u8)(pci_dev->class));
181}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800182
183static ssize_t is_enabled_store(struct device *dev,
184 struct device_attribute *attr, const char *buf,
185 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200186{
187 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800188 unsigned long val;
189 ssize_t result = strict_strtoul(buf, 0, &val);
190
191 if (result < 0)
192 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200193
194 /* this can crash the machine when done on the "wrong" device */
195 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800196 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200197
Trent Piepho92425a42008-11-30 17:10:12 -0800198 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900199 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800200 pci_disable_device(pdev);
201 else
202 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800203 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800204 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200205
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800206 return result < 0 ? result : count;
207}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200208
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800209static ssize_t is_enabled_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 struct pci_dev *pdev;
213
214 pdev = to_pci_dev (dev);
215 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200216}
217
Brice Goglin81bb0e12007-01-28 10:53:40 +0100218#ifdef CONFIG_NUMA
219static ssize_t
220numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
221{
222 return sprintf (buf, "%d\n", dev->numa_node);
223}
224#endif
225
Brice Goglinfe970642006-08-31 01:55:15 -0400226static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800227dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
228{
229 struct pci_dev *pdev = to_pci_dev(dev);
230
231 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
232}
233
234static ssize_t
235consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
236 char *buf)
237{
238 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
239}
240
241static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400242msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
243{
244 struct pci_dev *pdev = to_pci_dev(dev);
245
246 if (!pdev->subordinate)
247 return 0;
248
249 return sprintf (buf, "%u\n",
250 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
251}
252
253static ssize_t
254msi_bus_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
256{
257 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800258 unsigned long val;
259
260 if (strict_strtoul(buf, 0, &val) < 0)
261 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400262
263 /* bad things may happen if the no_msi flag is changed
264 * while some drivers are loaded */
265 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800266 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400267
Trent Piepho92425a42008-11-30 17:10:12 -0800268 /* Maybe pci devices without subordinate busses shouldn't even have this
269 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400270 if (!pdev->subordinate)
271 return count;
272
Trent Piepho92425a42008-11-30 17:10:12 -0800273 /* Is the flag going to change, or keep the value it already had? */
274 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
275 !!val) {
276 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400277
Trent Piepho92425a42008-11-30 17:10:12 -0800278 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
279 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400280 }
281
282 return count;
283}
Greg KH98885492005-05-05 11:57:25 -0700284
Alex Chiang705b1aa2009-03-20 14:56:31 -0600285#ifdef CONFIG_HOTPLUG
286static DEFINE_MUTEX(pci_remove_rescan_mutex);
287static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
288 size_t count)
289{
290 unsigned long val;
291 struct pci_bus *b = NULL;
292
293 if (strict_strtoul(buf, 0, &val) < 0)
294 return -EINVAL;
295
296 if (val) {
297 mutex_lock(&pci_remove_rescan_mutex);
298 while ((b = pci_find_next_bus(b)) != NULL)
299 pci_rescan_bus(b);
300 mutex_unlock(&pci_remove_rescan_mutex);
301 }
302 return count;
303}
304
305struct bus_attribute pci_bus_attrs[] = {
306 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
307 __ATTR_NULL
308};
Alex Chiang77c27c72009-03-20 14:56:36 -0600309
Alex Chiang738a6392009-03-20 14:56:41 -0600310static ssize_t
311dev_rescan_store(struct device *dev, struct device_attribute *attr,
312 const char *buf, size_t count)
313{
314 unsigned long val;
315 struct pci_dev *pdev = to_pci_dev(dev);
316
317 if (strict_strtoul(buf, 0, &val) < 0)
318 return -EINVAL;
319
320 if (val) {
321 mutex_lock(&pci_remove_rescan_mutex);
322 pci_rescan_bus(pdev->bus);
323 mutex_unlock(&pci_remove_rescan_mutex);
324 }
325 return count;
326}
327
Alex Chiang77c27c72009-03-20 14:56:36 -0600328static void remove_callback(struct device *dev)
329{
330 struct pci_dev *pdev = to_pci_dev(dev);
331
332 mutex_lock(&pci_remove_rescan_mutex);
333 pci_remove_bus_device(pdev);
334 mutex_unlock(&pci_remove_rescan_mutex);
335}
336
337static ssize_t
338remove_store(struct device *dev, struct device_attribute *dummy,
339 const char *buf, size_t count)
340{
341 int ret = 0;
342 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600343
344 if (strict_strtoul(buf, 0, &val) < 0)
345 return -EINVAL;
346
Alex Chiang77c27c72009-03-20 14:56:36 -0600347 /* An attribute cannot be unregistered by one of its own methods,
348 * so we have to use this roundabout approach.
349 */
350 if (val)
351 ret = device_schedule_callback(dev, remove_callback);
352 if (ret)
353 count = ret;
354 return count;
355}
Yinghai Lub9d320f2011-05-12 17:11:39 -0700356
357static ssize_t
358dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
359 const char *buf, size_t count)
360{
361 unsigned long val;
362 struct pci_bus *bus = to_pci_bus(dev);
363
364 if (strict_strtoul(buf, 0, &val) < 0)
365 return -EINVAL;
366
367 if (val) {
368 mutex_lock(&pci_remove_rescan_mutex);
369 pci_rescan_bus(bus);
370 mutex_unlock(&pci_remove_rescan_mutex);
371 }
372 return count;
373}
374
Alex Chiang705b1aa2009-03-20 14:56:31 -0600375#endif
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377struct device_attribute pci_dev_attrs[] = {
378 __ATTR_RO(resource),
379 __ATTR_RO(vendor),
380 __ATTR_RO(device),
381 __ATTR_RO(subsystem_vendor),
382 __ATTR_RO(subsystem_device),
383 __ATTR_RO(class),
384 __ATTR_RO(irq),
385 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700386 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700387 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100388#ifdef CONFIG_NUMA
389 __ATTR_RO(numa_node),
390#endif
Yinghai Lubb965402009-11-24 18:21:21 -0800391 __ATTR_RO(dma_mask_bits),
392 __ATTR_RO(consistent_dma_mask_bits),
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200393 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700394 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
395 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400396 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600397#ifdef CONFIG_HOTPLUG
398 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600399 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 __ATTR_NULL,
402};
403
Yinghai Lub9d320f2011-05-12 17:11:39 -0700404struct device_attribute pcibus_dev_attrs[] = {
405#ifdef CONFIG_HOTPLUG
406 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
407#endif
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700408 __ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL),
409 __ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL),
Yinghai Lub9d320f2011-05-12 17:11:39 -0700410 __ATTR_NULL,
411};
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000414boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
415{
416 struct pci_dev *pdev = to_pci_dev(dev);
417
418 return sprintf(buf, "%u\n",
419 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
420 IORESOURCE_ROM_SHADOW));
421}
422struct device_attribute vga_attr = __ATTR_RO(boot_vga);
423
424static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700425pci_read_config(struct file *filp, struct kobject *kobj,
426 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800427 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
430 unsigned int size = 64;
431 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900432 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* Several chips lock up trying to read undefined config space */
Serge E. Hallyn34867402011-03-23 16:43:17 -0700435 if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 size = dev->cfg_size;
437 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
438 size = 128;
439 }
440
441 if (off > size)
442 return 0;
443 if (off + count > size) {
444 size -= off;
445 count = size;
446 } else {
447 size = count;
448 }
449
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900450 if ((off & 1) && size) {
451 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700452 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900453 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900455 size--;
456 }
457
458 if ((off & 3) && size > 2) {
459 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700460 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900461 data[off - init_off] = val & 0xff;
462 data[off - init_off + 1] = (val >> 8) & 0xff;
463 off += 2;
464 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900468 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700469 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900470 data[off - init_off] = val & 0xff;
471 data[off - init_off + 1] = (val >> 8) & 0xff;
472 data[off - init_off + 2] = (val >> 16) & 0xff;
473 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 off += 4;
475 size -= 4;
476 }
477
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900478 if (size >= 2) {
479 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700480 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900481 data[off - init_off] = val & 0xff;
482 data[off - init_off + 1] = (val >> 8) & 0xff;
483 off += 2;
484 size -= 2;
485 }
486
487 if (size > 0) {
488 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700489 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900490 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 off++;
492 --size;
493 }
494
495 return count;
496}
497
498static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700499pci_write_config(struct file* filp, struct kobject *kobj,
500 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800501 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
504 unsigned int size = count;
505 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900506 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (off > dev->cfg_size)
509 return 0;
510 if (off + count > dev->cfg_size) {
511 size = dev->cfg_size - off;
512 count = size;
513 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900514
515 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700516 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900518 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900520
521 if ((off & 3) && size > 2) {
522 u16 val = data[off - init_off];
523 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700524 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900525 off += 2;
526 size -= 2;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900530 u32 val = data[off - init_off];
531 val |= (u32) data[off - init_off + 1] << 8;
532 val |= (u32) data[off - init_off + 2] << 16;
533 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700534 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 off += 4;
536 size -= 4;
537 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900538
539 if (size >= 2) {
540 u16 val = data[off - init_off];
541 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700542 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900543 off += 2;
544 size -= 2;
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900547 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700548 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 off++;
550 --size;
551 }
552
553 return count;
554}
555
Ben Hutchings94e61082008-03-05 16:52:39 +0000556static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700557read_vpd_attr(struct file *filp, struct kobject *kobj,
558 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000559 char *buf, loff_t off, size_t count)
560{
561 struct pci_dev *dev =
562 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000563
564 if (off > bin_attr->size)
565 count = 0;
566 else if (count > bin_attr->size - off)
567 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000568
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800569 return pci_read_vpd(dev, off, count, buf);
570}
Ben Hutchings94e61082008-03-05 16:52:39 +0000571
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800572static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700573write_vpd_attr(struct file *filp, struct kobject *kobj,
574 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800575 char *buf, loff_t off, size_t count)
576{
577 struct pci_dev *dev =
578 to_pci_dev(container_of(kobj, struct device, kobj));
579
580 if (off > bin_attr->size)
581 count = 0;
582 else if (count > bin_attr->size - off)
583 count = bin_attr->size - off;
584
585 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#ifdef HAVE_PCI_LEGACY
589/**
590 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700591 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700593 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * @buf: buffer to store results
595 * @off: offset into legacy I/O port space
596 * @count: number of bytes to read
597 *
598 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
599 * callback routine (pci_legacy_read).
600 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000601static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700602pci_read_legacy_io(struct file *filp, struct kobject *kobj,
603 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800604 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400607 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 kobj));
609
610 /* Only support 1, 2 or 4 byte accesses */
611 if (count != 1 && count != 2 && count != 4)
612 return -EINVAL;
613
614 return pci_legacy_read(bus, off, (u32 *)buf, count);
615}
616
617/**
618 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700619 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700621 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * @buf: buffer containing value to be written
623 * @off: offset into legacy I/O port space
624 * @count: number of bytes to write
625 *
626 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
627 * callback routine (pci_legacy_write).
628 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000629static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700630pci_write_legacy_io(struct file *filp, struct kobject *kobj,
631 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800632 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400635 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 kobj));
637 /* Only support 1, 2 or 4 byte accesses */
638 if (count != 1 && count != 2 && count != 4)
639 return -EINVAL;
640
641 return pci_legacy_write(bus, off, *(u32 *)buf, count);
642}
643
644/**
645 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700646 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * @kobj: kobject corresponding to device to be mapped
648 * @attr: struct bin_attribute for this file
649 * @vma: struct vm_area_struct passed to mmap
650 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000651 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * legacy memory space (first meg of bus space) into application virtual
653 * memory space.
654 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000655static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700656pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
657 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct vm_area_struct *vma)
659{
660 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400661 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 kobj));
663
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000664 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
665}
666
667/**
668 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700669 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000670 * @kobj: kobject corresponding to device to be mapped
671 * @attr: struct bin_attribute for this file
672 * @vma: struct vm_area_struct passed to mmap
673 *
674 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
675 * legacy IO space (first meg of bus space) into application virtual
676 * memory space. Returns -ENOSYS if the operation isn't supported
677 */
678static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700679pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
680 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000681 struct vm_area_struct *vma)
682{
683 struct pci_bus *bus = to_pci_bus(container_of(kobj,
684 struct device,
685 kobj));
686
687 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
688}
689
690/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300691 * pci_adjust_legacy_attr - adjustment of legacy file attributes
692 * @b: bus to create files under
693 * @mmap_type: I/O port or memory
694 *
695 * Stub implementation. Can be overridden by arch if necessary.
696 */
697void __weak
698pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
699{
700 return;
701}
702
703/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000704 * pci_create_legacy_files - create legacy I/O port and memory files
705 * @b: bus to create files under
706 *
707 * Some platforms allow access to legacy I/O port and ISA memory space on
708 * a per-bus basis. This routine creates the files and ties them into
709 * their associated read, write and mmap files from pci-sysfs.c
710 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300711 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000712 * as it is ok to set up the PCI bus without these files.
713 */
714void pci_create_legacy_files(struct pci_bus *b)
715{
716 int error;
717
718 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
719 GFP_ATOMIC);
720 if (!b->legacy_io)
721 goto kzalloc_err;
722
Stephen Rothwell62e877b2010-03-01 20:38:36 +1100723 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000724 b->legacy_io->attr.name = "legacy_io";
725 b->legacy_io->size = 0xffff;
726 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
727 b->legacy_io->read = pci_read_legacy_io;
728 b->legacy_io->write = pci_write_legacy_io;
729 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300730 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000731 error = device_create_bin_file(&b->dev, b->legacy_io);
732 if (error)
733 goto legacy_io_err;
734
735 /* Allocated above after the legacy_io struct */
736 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000737 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000738 b->legacy_mem->attr.name = "legacy_mem";
739 b->legacy_mem->size = 1024*1024;
740 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
741 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300742 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000743 error = device_create_bin_file(&b->dev, b->legacy_mem);
744 if (error)
745 goto legacy_mem_err;
746
747 return;
748
749legacy_mem_err:
750 device_remove_bin_file(&b->dev, b->legacy_io);
751legacy_io_err:
752 kfree(b->legacy_io);
753 b->legacy_io = NULL;
754kzalloc_err:
755 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
756 "and ISA memory resources to sysfs\n");
757 return;
758}
759
760void pci_remove_legacy_files(struct pci_bus *b)
761{
762 if (b->legacy_io) {
763 device_remove_bin_file(&b->dev, b->legacy_io);
764 device_remove_bin_file(&b->dev, b->legacy_mem);
765 kfree(b->legacy_io); /* both are allocated here */
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768#endif /* HAVE_PCI_LEGACY */
769
770#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700771
Martin Wilck3b519e42010-11-10 11:03:21 +0100772int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
773 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700774{
Martin Wilck3b519e42010-11-10 11:03:21 +0100775 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700776
Martin Wilck3b519e42010-11-10 11:03:21 +0100777 if (pci_resource_len(pdev, resno) == 0)
778 return 0;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700779 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
780 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800781 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800782 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100783 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
784 if (start >= pci_start && start < pci_start + size &&
785 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700786 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700787 return 0;
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/**
791 * pci_mmap_resource - map a PCI resource into user memory space
792 * @kobj: kobject for mapping
793 * @attr: struct bin_attribute for the file being mapped
794 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700795 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *
797 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
799static int
800pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700801 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
804 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400805 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700807 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000808 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000810 for (i = 0; i < PCI_ROM_RESOURCE; i++)
811 if (res == &pdev->resource[i])
812 break;
813 if (i >= PCI_ROM_RESOURCE)
814 return -ENODEV;
815
Martin Wilck3b519e42010-11-10 11:03:21 +0100816 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
817 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
818 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
819 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
820 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800821 (u64)pci_resource_start(pdev, i),
822 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700823 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100824 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700825
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000826 /* pci_mmap_page_range() expects the same kind of entry as coming
827 * from /proc/bus/pci/ which is a "user visible" value. If this is
828 * different from the resource itself, arch will do necessary fixup.
829 */
830 pci_resource_to_user(pdev, i, res, &start, &end);
831 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
833
Arjan van de Vene8de1482008-10-22 19:55:31 -0700834 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
835 return -EINVAL;
836
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700837 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
838}
839
840static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700841pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
842 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700843 struct vm_area_struct *vma)
844{
845 return pci_mmap_resource(kobj, attr, vma, 0);
846}
847
848static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700849pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
850 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700851 struct vm_area_struct *vma)
852{
853 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Alex Williamson86333282010-07-19 09:45:34 -0600856static ssize_t
857pci_resource_io(struct file *filp, struct kobject *kobj,
858 struct bin_attribute *attr, char *buf,
859 loff_t off, size_t count, bool write)
860{
861 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
862 struct device, kobj));
863 struct resource *res = attr->private;
864 unsigned long port = off;
865 int i;
866
867 for (i = 0; i < PCI_ROM_RESOURCE; i++)
868 if (res == &pdev->resource[i])
869 break;
870 if (i >= PCI_ROM_RESOURCE)
871 return -ENODEV;
872
873 port += pci_resource_start(pdev, i);
874
875 if (port > pci_resource_end(pdev, i))
876 return 0;
877
878 if (port + count - 1 > pci_resource_end(pdev, i))
879 return -EINVAL;
880
881 switch (count) {
882 case 1:
883 if (write)
884 outb(*(u8 *)buf, port);
885 else
886 *(u8 *)buf = inb(port);
887 return 1;
888 case 2:
889 if (write)
890 outw(*(u16 *)buf, port);
891 else
892 *(u16 *)buf = inw(port);
893 return 2;
894 case 4:
895 if (write)
896 outl(*(u32 *)buf, port);
897 else
898 *(u32 *)buf = inl(port);
899 return 4;
900 }
901 return -EINVAL;
902}
903
904static ssize_t
905pci_read_resource_io(struct file *filp, struct kobject *kobj,
906 struct bin_attribute *attr, char *buf,
907 loff_t off, size_t count)
908{
909 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
910}
911
912static ssize_t
913pci_write_resource_io(struct file *filp, struct kobject *kobj,
914 struct bin_attribute *attr, char *buf,
915 loff_t off, size_t count)
916{
917 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700921 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700922 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700923 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700924 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700925 * free their resources.
926 */
927static void
928pci_remove_resource_files(struct pci_dev *pdev)
929{
930 int i;
931
932 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
933 struct bin_attribute *res_attr;
934
935 res_attr = pdev->res_attr[i];
936 if (res_attr) {
937 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
938 kfree(res_attr);
939 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700940
941 res_attr = pdev->res_attr_wc[i];
942 if (res_attr) {
943 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
944 kfree(res_attr);
945 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700946 }
947}
948
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700949static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
950{
951 /* allocate attribute structure, piggyback attribute name */
952 int name_len = write_combine ? 13 : 10;
953 struct bin_attribute *res_attr;
954 int retval;
955
956 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
957 if (res_attr) {
958 char *res_attr_name = (char *)(res_attr + 1);
959
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800960 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700961 if (write_combine) {
962 pdev->res_attr_wc[num] = res_attr;
963 sprintf(res_attr_name, "resource%d_wc", num);
964 res_attr->mmap = pci_mmap_resource_wc;
965 } else {
966 pdev->res_attr[num] = res_attr;
967 sprintf(res_attr_name, "resource%d", num);
968 res_attr->mmap = pci_mmap_resource_uc;
969 }
Alex Williamson86333282010-07-19 09:45:34 -0600970 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
971 res_attr->read = pci_read_resource_io;
972 res_attr->write = pci_write_resource_io;
973 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700974 res_attr->attr.name = res_attr_name;
975 res_attr->attr.mode = S_IRUSR | S_IWUSR;
976 res_attr->size = pci_resource_len(pdev, num);
977 res_attr->private = &pdev->resource[num];
978 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
979 } else
980 retval = -ENOMEM;
981
982 return retval;
983}
984
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700985/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700987 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700989 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700991static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700994 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* Expose the PCI resources from this device as files */
997 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* skip empty resources */
1000 if (!pci_resource_len(pdev, i))
1001 continue;
1002
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -07001003 retval = pci_create_attr(pdev, i, 0);
1004 /* for prefetchable resources, create a WC mappable file */
1005 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1006 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001007
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -07001008 if (retval) {
1009 pci_remove_resource_files(pdev);
1010 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001016int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1017void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018#endif /* HAVE_PCI_MMAP */
1019
1020/**
1021 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001022 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001024 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * @buf: user input
1026 * @off: file offset
1027 * @count: number of byte in input
1028 *
1029 * writing anything except 0 enables it
1030 */
1031static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001032pci_write_rom(struct file *filp, struct kobject *kobj,
1033 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001034 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1037
1038 if ((off == 0) && (*buf == '0') && (count == 2))
1039 pdev->rom_attr_enabled = 0;
1040 else
1041 pdev->rom_attr_enabled = 1;
1042
1043 return count;
1044}
1045
1046/**
1047 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001048 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001050 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * @buf: where to put the data we read from the ROM
1052 * @off: file offset
1053 * @count: number of bytes to read
1054 *
1055 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1056 * device corresponding to @kobj.
1057 */
1058static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001059pci_read_rom(struct file *filp, struct kobject *kobj,
1060 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001061 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1064 void __iomem *rom;
1065 size_t size;
1066
1067 if (!pdev->rom_attr_enabled)
1068 return -EINVAL;
1069
1070 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001071 if (!rom || !size)
1072 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (off >= size)
1075 count = 0;
1076 else {
1077 if (off + count > size)
1078 count = size - off;
1079
1080 memcpy_fromio(buf, rom + off, count);
1081 }
1082 pci_unmap_rom(pdev, rom);
1083
1084 return count;
1085}
1086
1087static struct bin_attribute pci_config_attr = {
1088 .attr = {
1089 .name = "config",
1090 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001092 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 .read = pci_read_config,
1094 .write = pci_write_config,
1095};
1096
1097static struct bin_attribute pcie_config_attr = {
1098 .attr = {
1099 .name = "config",
1100 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001102 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 .read = pci_read_config,
1104 .write = pci_write_config,
1105};
1106
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001107int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001108{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001109 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001110}
1111
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001112static ssize_t reset_store(struct device *dev,
1113 struct device_attribute *attr, const char *buf,
1114 size_t count)
1115{
1116 struct pci_dev *pdev = to_pci_dev(dev);
1117 unsigned long val;
1118 ssize_t result = strict_strtoul(buf, 0, &val);
1119
1120 if (result < 0)
1121 return result;
1122
1123 if (val != 1)
1124 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001125
1126 result = pci_reset_function(pdev);
1127 if (result < 0)
1128 return result;
1129
1130 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001131}
1132
1133static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1134
Zhao, Yu280c73d2008-10-13 20:01:00 +08001135static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1136{
1137 int retval;
1138 struct bin_attribute *attr;
1139
1140 /* If the device has VPD, try to expose it in sysfs. */
1141 if (dev->vpd) {
1142 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1143 if (!attr)
1144 return -ENOMEM;
1145
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001146 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001147 attr->size = dev->vpd->len;
1148 attr->attr.name = "vpd";
1149 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001150 attr->read = read_vpd_attr;
1151 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001152 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1153 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001154 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001155 return retval;
1156 }
1157 dev->vpd->attr = attr;
1158 }
1159
1160 /* Active State Power Management */
1161 pcie_aspm_create_sysfs_dev_files(dev);
1162
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001163 if (!pci_probe_reset_function(dev)) {
1164 retval = device_create_file(&dev->dev, &reset_attr);
1165 if (retval)
1166 goto error;
1167 dev->reset_fn = 1;
1168 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001169 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001170
1171error:
1172 pcie_aspm_remove_sysfs_dev_files(dev);
1173 if (dev->vpd && dev->vpd->attr) {
1174 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1175 kfree(dev->vpd->attr);
1176 }
1177
1178 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001179}
1180
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001181int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001183 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001184 int rom_size = 0;
1185 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (!sysfs_initialized)
1188 return -EACCES;
1189
Zhao, Yu557848c2008-10-13 19:18:07 +08001190 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001191 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001193 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1194 if (retval)
1195 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001197 retval = pci_create_resource_files(pdev);
1198 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001199 goto err_config_file;
1200
1201 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1202 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1203 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1204 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001207 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001208 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001209 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001210 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001211 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001213 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001214 attr->size = rom_size;
1215 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001216 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001217 attr->read = pci_read_rom;
1218 attr->write = pci_write_rom;
1219 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1220 if (retval) {
1221 kfree(attr);
1222 goto err_resource_files;
1223 }
1224 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001226
Dave Airlie217f45d2009-03-04 05:57:05 +00001227 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1228 retval = device_create_file(&pdev->dev, &vga_attr);
1229 if (retval)
1230 goto err_rom_file;
1231 }
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001234 retval = pcibios_add_platform_entries(pdev);
1235 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001236 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001237
Zhao, Yu280c73d2008-10-13 20:01:00 +08001238 /* add sysfs entries for various capabilities */
1239 retval = pci_create_capabilities_sysfs(pdev);
1240 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001241 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001242
Narendra K911e1c92010-07-26 05:56:50 -05001243 pci_create_firmware_label_files(pdev);
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001246
Dave Airlie217f45d2009-03-04 05:57:05 +00001247err_vga_file:
1248 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1249 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001250err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001251 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001252 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001253 kfree(pdev->rom_attr);
1254 pdev->rom_attr = NULL;
1255 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001256err_resource_files:
1257 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001258err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001259 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001260 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1261 else
1262 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1263err:
1264 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Zhao, Yu280c73d2008-10-13 20:01:00 +08001267static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1268{
1269 if (dev->vpd && dev->vpd->attr) {
1270 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1271 kfree(dev->vpd->attr);
1272 }
1273
1274 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001275 if (dev->reset_fn) {
1276 device_remove_file(&dev->dev, &reset_attr);
1277 dev->reset_fn = 0;
1278 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/**
1282 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1283 * @pdev: device whose entries we should free
1284 *
1285 * Cleanup when @pdev is removed from sysfs.
1286 */
1287void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1288{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001289 int rom_size = 0;
1290
David Millerd67afe52006-11-10 12:27:48 -08001291 if (!sysfs_initialized)
1292 return;
1293
Zhao, Yu280c73d2008-10-13 20:01:00 +08001294 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001295
Zhao, Yu557848c2008-10-13 19:18:07 +08001296 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1298 else
1299 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1300
1301 pci_remove_resource_files(pdev);
1302
Zhao, Yu280c73d2008-10-13 20:01:00 +08001303 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1304 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1305 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1306 rom_size = 0x20000;
1307
1308 if (rom_size && pdev->rom_attr) {
1309 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1310 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Narendra K911e1c92010-07-26 05:56:50 -05001312
1313 pci_remove_firmware_label_files(pdev);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317static int __init pci_sysfs_init(void)
1318{
1319 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001320 int retval;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001323 for_each_pci_dev(pdev) {
1324 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001325 if (retval) {
1326 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001327 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001328 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 return 0;
1332}
1333
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001334late_initcall(pci_sysfs_init);