blob: a55e248618cd00e7de495a078c3bb59d177123f4 [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);
Yinghai Lu210647a2012-02-25 13:54:20 -0800333 pci_stop_and_remove_bus_device(pdev);
Alex Chiang77c27c72009-03-20 14:56:36 -0600334 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);
Yinghai Lu2f320522012-01-21 02:08:22 -0800369 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
370 pci_rescan_bus_bridge_resize(bus->self);
371 else
372 pci_rescan_bus(bus);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700373 mutex_unlock(&pci_remove_rescan_mutex);
374 }
375 return count;
376}
377
Alex Chiang705b1aa2009-03-20 14:56:31 -0600378#endif
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380struct device_attribute pci_dev_attrs[] = {
381 __ATTR_RO(resource),
382 __ATTR_RO(vendor),
383 __ATTR_RO(device),
384 __ATTR_RO(subsystem_vendor),
385 __ATTR_RO(subsystem_device),
386 __ATTR_RO(class),
387 __ATTR_RO(irq),
388 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700389 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700390 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100391#ifdef CONFIG_NUMA
392 __ATTR_RO(numa_node),
393#endif
Yinghai Lubb965402009-11-24 18:21:21 -0800394 __ATTR_RO(dma_mask_bits),
395 __ATTR_RO(consistent_dma_mask_bits),
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200396 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700397 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
398 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400399 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600400#ifdef CONFIG_HOTPLUG
401 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600402 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 __ATTR_NULL,
405};
406
Yinghai Lub9d320f2011-05-12 17:11:39 -0700407struct device_attribute pcibus_dev_attrs[] = {
408#ifdef CONFIG_HOTPLUG
409 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
410#endif
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700411 __ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL),
412 __ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL),
Yinghai Lub9d320f2011-05-12 17:11:39 -0700413 __ATTR_NULL,
414};
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000417boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
418{
419 struct pci_dev *pdev = to_pci_dev(dev);
420
421 return sprintf(buf, "%u\n",
422 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
423 IORESOURCE_ROM_SHADOW));
424}
425struct device_attribute vga_attr = __ATTR_RO(boot_vga);
426
427static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700428pci_read_config(struct file *filp, struct kobject *kobj,
429 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800430 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
433 unsigned int size = 64;
434 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900435 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* Several chips lock up trying to read undefined config space */
Eric Parisb7e724d2012-01-03 12:25:15 -0500438 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 size = dev->cfg_size;
440 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
441 size = 128;
442 }
443
444 if (off > size)
445 return 0;
446 if (off + count > size) {
447 size -= off;
448 count = size;
449 } else {
450 size = count;
451 }
452
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900453 if ((off & 1) && size) {
454 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700455 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900456 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900458 size--;
459 }
460
461 if ((off & 3) && size > 2) {
462 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700463 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900464 data[off - init_off] = val & 0xff;
465 data[off - init_off + 1] = (val >> 8) & 0xff;
466 off += 2;
467 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900471 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700472 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900473 data[off - init_off] = val & 0xff;
474 data[off - init_off + 1] = (val >> 8) & 0xff;
475 data[off - init_off + 2] = (val >> 16) & 0xff;
476 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 off += 4;
478 size -= 4;
479 }
480
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900481 if (size >= 2) {
482 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700483 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900484 data[off - init_off] = val & 0xff;
485 data[off - init_off + 1] = (val >> 8) & 0xff;
486 off += 2;
487 size -= 2;
488 }
489
490 if (size > 0) {
491 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700492 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900493 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 off++;
495 --size;
496 }
497
498 return count;
499}
500
501static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700502pci_write_config(struct file* filp, struct kobject *kobj,
503 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800504 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
507 unsigned int size = count;
508 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900509 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (off > dev->cfg_size)
512 return 0;
513 if (off + count > dev->cfg_size) {
514 size = dev->cfg_size - off;
515 count = size;
516 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900517
518 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700519 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900521 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900523
524 if ((off & 3) && size > 2) {
525 u16 val = data[off - init_off];
526 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700527 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900528 off += 2;
529 size -= 2;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900533 u32 val = data[off - init_off];
534 val |= (u32) data[off - init_off + 1] << 8;
535 val |= (u32) data[off - init_off + 2] << 16;
536 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700537 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 off += 4;
539 size -= 4;
540 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900541
542 if (size >= 2) {
543 u16 val = data[off - init_off];
544 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700545 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900546 off += 2;
547 size -= 2;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900550 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700551 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 off++;
553 --size;
554 }
555
556 return count;
557}
558
Ben Hutchings94e61082008-03-05 16:52:39 +0000559static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700560read_vpd_attr(struct file *filp, struct kobject *kobj,
561 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000562 char *buf, loff_t off, size_t count)
563{
564 struct pci_dev *dev =
565 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000566
567 if (off > bin_attr->size)
568 count = 0;
569 else if (count > bin_attr->size - off)
570 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000571
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800572 return pci_read_vpd(dev, off, count, buf);
573}
Ben Hutchings94e61082008-03-05 16:52:39 +0000574
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800575static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700576write_vpd_attr(struct file *filp, struct kobject *kobj,
577 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800578 char *buf, loff_t off, size_t count)
579{
580 struct pci_dev *dev =
581 to_pci_dev(container_of(kobj, struct device, kobj));
582
583 if (off > bin_attr->size)
584 count = 0;
585 else if (count > bin_attr->size - off)
586 count = bin_attr->size - off;
587
588 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#ifdef HAVE_PCI_LEGACY
592/**
593 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700594 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700596 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * @buf: buffer to store results
598 * @off: offset into legacy I/O port space
599 * @count: number of bytes to read
600 *
601 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
602 * callback routine (pci_legacy_read).
603 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000604static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700605pci_read_legacy_io(struct file *filp, struct kobject *kobj,
606 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800607 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400610 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 kobj));
612
613 /* Only support 1, 2 or 4 byte accesses */
614 if (count != 1 && count != 2 && count != 4)
615 return -EINVAL;
616
617 return pci_legacy_read(bus, off, (u32 *)buf, count);
618}
619
620/**
621 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700622 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700624 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * @buf: buffer containing value to be written
626 * @off: offset into legacy I/O port space
627 * @count: number of bytes to write
628 *
629 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
630 * callback routine (pci_legacy_write).
631 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000632static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700633pci_write_legacy_io(struct file *filp, struct kobject *kobj,
634 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800635 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400638 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 kobj));
640 /* Only support 1, 2 or 4 byte accesses */
641 if (count != 1 && count != 2 && count != 4)
642 return -EINVAL;
643
644 return pci_legacy_write(bus, off, *(u32 *)buf, count);
645}
646
647/**
648 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700649 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * @kobj: kobject corresponding to device to be mapped
651 * @attr: struct bin_attribute for this file
652 * @vma: struct vm_area_struct passed to mmap
653 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000654 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * legacy memory space (first meg of bus space) into application virtual
656 * memory space.
657 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000658static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700659pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
660 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct vm_area_struct *vma)
662{
663 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400664 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 kobj));
666
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000667 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
668}
669
670/**
671 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700672 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000673 * @kobj: kobject corresponding to device to be mapped
674 * @attr: struct bin_attribute for this file
675 * @vma: struct vm_area_struct passed to mmap
676 *
677 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
678 * legacy IO space (first meg of bus space) into application virtual
679 * memory space. Returns -ENOSYS if the operation isn't supported
680 */
681static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700682pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
683 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000684 struct vm_area_struct *vma)
685{
686 struct pci_bus *bus = to_pci_bus(container_of(kobj,
687 struct device,
688 kobj));
689
690 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
691}
692
693/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300694 * pci_adjust_legacy_attr - adjustment of legacy file attributes
695 * @b: bus to create files under
696 * @mmap_type: I/O port or memory
697 *
698 * Stub implementation. Can be overridden by arch if necessary.
699 */
700void __weak
701pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
702{
703 return;
704}
705
706/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000707 * pci_create_legacy_files - create legacy I/O port and memory files
708 * @b: bus to create files under
709 *
710 * Some platforms allow access to legacy I/O port and ISA memory space on
711 * a per-bus basis. This routine creates the files and ties them into
712 * their associated read, write and mmap files from pci-sysfs.c
713 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300714 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000715 * as it is ok to set up the PCI bus without these files.
716 */
717void pci_create_legacy_files(struct pci_bus *b)
718{
719 int error;
720
721 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
722 GFP_ATOMIC);
723 if (!b->legacy_io)
724 goto kzalloc_err;
725
Stephen Rothwell62e877b2010-03-01 20:38:36 +1100726 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000727 b->legacy_io->attr.name = "legacy_io";
728 b->legacy_io->size = 0xffff;
729 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
730 b->legacy_io->read = pci_read_legacy_io;
731 b->legacy_io->write = pci_write_legacy_io;
732 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300733 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000734 error = device_create_bin_file(&b->dev, b->legacy_io);
735 if (error)
736 goto legacy_io_err;
737
738 /* Allocated above after the legacy_io struct */
739 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000740 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000741 b->legacy_mem->attr.name = "legacy_mem";
742 b->legacy_mem->size = 1024*1024;
743 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
744 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300745 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000746 error = device_create_bin_file(&b->dev, b->legacy_mem);
747 if (error)
748 goto legacy_mem_err;
749
750 return;
751
752legacy_mem_err:
753 device_remove_bin_file(&b->dev, b->legacy_io);
754legacy_io_err:
755 kfree(b->legacy_io);
756 b->legacy_io = NULL;
757kzalloc_err:
758 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
759 "and ISA memory resources to sysfs\n");
760 return;
761}
762
763void pci_remove_legacy_files(struct pci_bus *b)
764{
765 if (b->legacy_io) {
766 device_remove_bin_file(&b->dev, b->legacy_io);
767 device_remove_bin_file(&b->dev, b->legacy_mem);
768 kfree(b->legacy_io); /* both are allocated here */
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771#endif /* HAVE_PCI_LEGACY */
772
773#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700774
Martin Wilck3b519e42010-11-10 11:03:21 +0100775int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
776 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700777{
Martin Wilck3b519e42010-11-10 11:03:21 +0100778 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700779
Martin Wilck3b519e42010-11-10 11:03:21 +0100780 if (pci_resource_len(pdev, resno) == 0)
781 return 0;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700782 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
783 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800784 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800785 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100786 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
787 if (start >= pci_start && start < pci_start + size &&
788 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700789 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700790 return 0;
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/**
794 * pci_mmap_resource - map a PCI resource into user memory space
795 * @kobj: kobject for mapping
796 * @attr: struct bin_attribute for the file being mapped
797 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700798 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
800 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
802static int
803pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700804 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
807 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400808 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700810 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000811 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000813 for (i = 0; i < PCI_ROM_RESOURCE; i++)
814 if (res == &pdev->resource[i])
815 break;
816 if (i >= PCI_ROM_RESOURCE)
817 return -ENODEV;
818
Martin Wilck3b519e42010-11-10 11:03:21 +0100819 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
820 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
821 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
822 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
823 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800824 (u64)pci_resource_start(pdev, i),
825 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700826 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100827 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700828
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000829 /* pci_mmap_page_range() expects the same kind of entry as coming
830 * from /proc/bus/pci/ which is a "user visible" value. If this is
831 * different from the resource itself, arch will do necessary fixup.
832 */
833 pci_resource_to_user(pdev, i, res, &start, &end);
834 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
836
Arjan van de Vene8de1482008-10-22 19:55:31 -0700837 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
838 return -EINVAL;
839
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700840 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
841}
842
843static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700844pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
845 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700846 struct vm_area_struct *vma)
847{
848 return pci_mmap_resource(kobj, attr, vma, 0);
849}
850
851static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700852pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
853 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700854 struct vm_area_struct *vma)
855{
856 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Alex Williamson86333282010-07-19 09:45:34 -0600859static ssize_t
860pci_resource_io(struct file *filp, struct kobject *kobj,
861 struct bin_attribute *attr, char *buf,
862 loff_t off, size_t count, bool write)
863{
864 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
865 struct device, kobj));
866 struct resource *res = attr->private;
867 unsigned long port = off;
868 int i;
869
870 for (i = 0; i < PCI_ROM_RESOURCE; i++)
871 if (res == &pdev->resource[i])
872 break;
873 if (i >= PCI_ROM_RESOURCE)
874 return -ENODEV;
875
876 port += pci_resource_start(pdev, i);
877
878 if (port > pci_resource_end(pdev, i))
879 return 0;
880
881 if (port + count - 1 > pci_resource_end(pdev, i))
882 return -EINVAL;
883
884 switch (count) {
885 case 1:
886 if (write)
887 outb(*(u8 *)buf, port);
888 else
889 *(u8 *)buf = inb(port);
890 return 1;
891 case 2:
892 if (write)
893 outw(*(u16 *)buf, port);
894 else
895 *(u16 *)buf = inw(port);
896 return 2;
897 case 4:
898 if (write)
899 outl(*(u32 *)buf, port);
900 else
901 *(u32 *)buf = inl(port);
902 return 4;
903 }
904 return -EINVAL;
905}
906
907static ssize_t
908pci_read_resource_io(struct file *filp, struct kobject *kobj,
909 struct bin_attribute *attr, char *buf,
910 loff_t off, size_t count)
911{
912 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
913}
914
915static ssize_t
916pci_write_resource_io(struct file *filp, struct kobject *kobj,
917 struct bin_attribute *attr, char *buf,
918 loff_t off, size_t count)
919{
920 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700924 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700925 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700926 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700927 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700928 * free their resources.
929 */
930static void
931pci_remove_resource_files(struct pci_dev *pdev)
932{
933 int i;
934
935 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
936 struct bin_attribute *res_attr;
937
938 res_attr = pdev->res_attr[i];
939 if (res_attr) {
940 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
941 kfree(res_attr);
942 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700943
944 res_attr = pdev->res_attr_wc[i];
945 if (res_attr) {
946 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
947 kfree(res_attr);
948 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700949 }
950}
951
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700952static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
953{
954 /* allocate attribute structure, piggyback attribute name */
955 int name_len = write_combine ? 13 : 10;
956 struct bin_attribute *res_attr;
957 int retval;
958
959 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
960 if (res_attr) {
961 char *res_attr_name = (char *)(res_attr + 1);
962
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800963 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700964 if (write_combine) {
965 pdev->res_attr_wc[num] = res_attr;
966 sprintf(res_attr_name, "resource%d_wc", num);
967 res_attr->mmap = pci_mmap_resource_wc;
968 } else {
969 pdev->res_attr[num] = res_attr;
970 sprintf(res_attr_name, "resource%d", num);
971 res_attr->mmap = pci_mmap_resource_uc;
972 }
Alex Williamson86333282010-07-19 09:45:34 -0600973 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
974 res_attr->read = pci_read_resource_io;
975 res_attr->write = pci_write_resource_io;
976 }
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -0700977 res_attr->attr.name = res_attr_name;
978 res_attr->attr.mode = S_IRUSR | S_IWUSR;
979 res_attr->size = pci_resource_len(pdev, num);
980 res_attr->private = &pdev->resource[num];
981 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
982 } else
983 retval = -ENOMEM;
984
985 return retval;
986}
987
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700988/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700990 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700992 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700994static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700997 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* Expose the PCI resources from this device as files */
1000 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* skip empty resources */
1003 if (!pci_resource_len(pdev, i))
1004 continue;
1005
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -07001006 retval = pci_create_attr(pdev, i, 0);
1007 /* for prefetchable resources, create a WC mappable file */
1008 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1009 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001010
venkatesh.pallipadi@intel.com45aec1a2008-03-18 17:00:22 -07001011 if (retval) {
1012 pci_remove_resource_files(pdev);
1013 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001019int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1020void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021#endif /* HAVE_PCI_MMAP */
1022
1023/**
1024 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001025 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001027 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * @buf: user input
1029 * @off: file offset
1030 * @count: number of byte in input
1031 *
1032 * writing anything except 0 enables it
1033 */
1034static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001035pci_write_rom(struct file *filp, struct kobject *kobj,
1036 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001037 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1040
1041 if ((off == 0) && (*buf == '0') && (count == 2))
1042 pdev->rom_attr_enabled = 0;
1043 else
1044 pdev->rom_attr_enabled = 1;
1045
1046 return count;
1047}
1048
1049/**
1050 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001051 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001053 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * @buf: where to put the data we read from the ROM
1055 * @off: file offset
1056 * @count: number of bytes to read
1057 *
1058 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1059 * device corresponding to @kobj.
1060 */
1061static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001062pci_read_rom(struct file *filp, struct kobject *kobj,
1063 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001064 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1067 void __iomem *rom;
1068 size_t size;
1069
1070 if (!pdev->rom_attr_enabled)
1071 return -EINVAL;
1072
1073 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001074 if (!rom || !size)
1075 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (off >= size)
1078 count = 0;
1079 else {
1080 if (off + count > size)
1081 count = size - off;
1082
1083 memcpy_fromio(buf, rom + off, count);
1084 }
1085 pci_unmap_rom(pdev, rom);
1086
1087 return count;
1088}
1089
1090static struct bin_attribute pci_config_attr = {
1091 .attr = {
1092 .name = "config",
1093 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001095 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 .read = pci_read_config,
1097 .write = pci_write_config,
1098};
1099
1100static struct bin_attribute pcie_config_attr = {
1101 .attr = {
1102 .name = "config",
1103 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001105 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 .read = pci_read_config,
1107 .write = pci_write_config,
1108};
1109
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001110int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001111{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001112 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001113}
1114
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001115static ssize_t reset_store(struct device *dev,
1116 struct device_attribute *attr, const char *buf,
1117 size_t count)
1118{
1119 struct pci_dev *pdev = to_pci_dev(dev);
1120 unsigned long val;
1121 ssize_t result = strict_strtoul(buf, 0, &val);
1122
1123 if (result < 0)
1124 return result;
1125
1126 if (val != 1)
1127 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001128
1129 result = pci_reset_function(pdev);
1130 if (result < 0)
1131 return result;
1132
1133 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001134}
1135
1136static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1137
Zhao, Yu280c73d2008-10-13 20:01:00 +08001138static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1139{
1140 int retval;
1141 struct bin_attribute *attr;
1142
1143 /* If the device has VPD, try to expose it in sysfs. */
1144 if (dev->vpd) {
1145 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1146 if (!attr)
1147 return -ENOMEM;
1148
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001149 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001150 attr->size = dev->vpd->len;
1151 attr->attr.name = "vpd";
1152 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001153 attr->read = read_vpd_attr;
1154 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001155 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1156 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001157 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001158 return retval;
1159 }
1160 dev->vpd->attr = attr;
1161 }
1162
1163 /* Active State Power Management */
1164 pcie_aspm_create_sysfs_dev_files(dev);
1165
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001166 if (!pci_probe_reset_function(dev)) {
1167 retval = device_create_file(&dev->dev, &reset_attr);
1168 if (retval)
1169 goto error;
1170 dev->reset_fn = 1;
1171 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001172 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001173
1174error:
1175 pcie_aspm_remove_sysfs_dev_files(dev);
1176 if (dev->vpd && dev->vpd->attr) {
1177 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1178 kfree(dev->vpd->attr);
1179 }
1180
1181 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001182}
1183
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001184int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001186 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001187 int rom_size = 0;
1188 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!sysfs_initialized)
1191 return -EACCES;
1192
Zhao, Yu557848c2008-10-13 19:18:07 +08001193 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001194 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001196 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1197 if (retval)
1198 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001200 retval = pci_create_resource_files(pdev);
1201 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001202 goto err_config_file;
1203
1204 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1205 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1206 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1207 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001210 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001211 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001212 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001213 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001214 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001216 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001217 attr->size = rom_size;
1218 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001219 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001220 attr->read = pci_read_rom;
1221 attr->write = pci_write_rom;
1222 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1223 if (retval) {
1224 kfree(attr);
1225 goto err_resource_files;
1226 }
1227 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001229
Dave Airlie217f45d2009-03-04 05:57:05 +00001230 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1231 retval = device_create_file(&pdev->dev, &vga_attr);
1232 if (retval)
1233 goto err_rom_file;
1234 }
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001237 retval = pcibios_add_platform_entries(pdev);
1238 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001239 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001240
Zhao, Yu280c73d2008-10-13 20:01:00 +08001241 /* add sysfs entries for various capabilities */
1242 retval = pci_create_capabilities_sysfs(pdev);
1243 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001244 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001245
Narendra K911e1c92010-07-26 05:56:50 -05001246 pci_create_firmware_label_files(pdev);
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001249
Dave Airlie217f45d2009-03-04 05:57:05 +00001250err_vga_file:
1251 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1252 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001253err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001254 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001255 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001256 kfree(pdev->rom_attr);
1257 pdev->rom_attr = NULL;
1258 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001259err_resource_files:
1260 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001261err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001262 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001263 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1264 else
1265 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1266err:
1267 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Zhao, Yu280c73d2008-10-13 20:01:00 +08001270static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1271{
1272 if (dev->vpd && dev->vpd->attr) {
1273 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1274 kfree(dev->vpd->attr);
1275 }
1276
1277 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001278 if (dev->reset_fn) {
1279 device_remove_file(&dev->dev, &reset_attr);
1280 dev->reset_fn = 0;
1281 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/**
1285 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1286 * @pdev: device whose entries we should free
1287 *
1288 * Cleanup when @pdev is removed from sysfs.
1289 */
1290void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1291{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001292 int rom_size = 0;
1293
David Millerd67afe52006-11-10 12:27:48 -08001294 if (!sysfs_initialized)
1295 return;
1296
Zhao, Yu280c73d2008-10-13 20:01:00 +08001297 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001298
Zhao, Yu557848c2008-10-13 19:18:07 +08001299 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1301 else
1302 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1303
1304 pci_remove_resource_files(pdev);
1305
Zhao, Yu280c73d2008-10-13 20:01:00 +08001306 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1307 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1308 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1309 rom_size = 0x20000;
1310
1311 if (rom_size && pdev->rom_attr) {
1312 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1313 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Narendra K911e1c92010-07-26 05:56:50 -05001315
1316 pci_remove_firmware_label_files(pdev);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320static int __init pci_sysfs_init(void)
1321{
1322 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001323 int retval;
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001326 for_each_pci_dev(pdev) {
1327 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001328 if (retval) {
1329 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001330 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001331 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 return 0;
1335}
1336
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001337late_initcall(pci_sysfs_init);