blob: 95712a375cd521954f2a1f130a7ddf73704703cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-sysfs.c
3 *
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
10 *
11 * File attributes for PCI devices
12 *
13 * Modeled after usb's driverfs.c
14 *
15 */
16
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/pci.h>
21#include <linux/stat.h>
22#include <linux/topology.h>
23#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070024#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070025#include <linux/capability.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080026#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "pci.h"
29
30static int sysfs_initialized; /* = 0 */
31
32/* show configuration fields */
33#define pci_config_attr(field, format_string) \
34static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040035field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{ \
37 struct pci_dev *pdev; \
38 \
39 pdev = to_pci_dev (dev); \
40 return sprintf (buf, format_string, pdev->field); \
41}
42
43pci_config_attr(vendor, "0x%04x\n");
44pci_config_attr(device, "0x%04x\n");
45pci_config_attr(subsystem_vendor, "0x%04x\n");
46pci_config_attr(subsystem_device, "0x%04x\n");
47pci_config_attr(class, "0x%06x\n");
48pci_config_attr(irq, "%u\n");
49
Doug Thompsonbdee9d92006-06-14 16:59:48 -070050static ssize_t broken_parity_status_show(struct device *dev,
51 struct device_attribute *attr,
52 char *buf)
53{
54 struct pci_dev *pdev = to_pci_dev(dev);
55 return sprintf (buf, "%u\n", pdev->broken_parity_status);
56}
57
58static ssize_t broken_parity_status_store(struct device *dev,
59 struct device_attribute *attr,
60 const char *buf, size_t count)
61{
62 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080063 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070064
Trent Piepho92425a42008-11-30 17:10:12 -080065 if (strict_strtoul(buf, 0, &val) < 0)
66 return -EINVAL;
67
68 pdev->broken_parity_status = !!val;
69
70 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070071}
72
Alan Cox4327edf2005-09-10 00:25:49 -070073static ssize_t local_cpus_show(struct device *dev,
74 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Mike Travis3be83052009-01-04 05:18:01 -080076 const struct cpumask *mask;
Alan Cox4327edf2005-09-10 00:25:49 -070077 int len;
78
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020079#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053080 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
81 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020082#else
Mike Travis3be83052009-01-04 05:18:01 -080083 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020084#endif
Mike Travis3be83052009-01-04 05:18:01 -080085 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070086 buf[len++] = '\n';
87 buf[len] = '\0';
88 return len;
89}
90
91
92static ssize_t local_cpulist_show(struct device *dev,
93 struct device_attribute *attr, char *buf)
94{
Mike Travis3be83052009-01-04 05:18:01 -080095 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070096 int len;
97
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020098#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053099 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
100 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200101#else
Mike Travis3be83052009-01-04 05:18:01 -0800102 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200103#endif
Mike Travis3be83052009-01-04 05:18:01 -0800104 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -0700105 buf[len++] = '\n';
106 buf[len] = '\0';
107 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/* show resources */
111static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400112resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct pci_dev * pci_dev = to_pci_dev(dev);
115 char * str = buf;
116 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800117 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700118 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (pci_dev->subordinate)
121 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800122 else
123 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000126 struct resource *res = &pci_dev->resource[i];
127 pci_resource_to_user(pci_dev, i, res, &start, &end);
128 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
129 (unsigned long long)start,
130 (unsigned long long)end,
131 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 return (str - buf);
134}
135
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200136static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700137{
138 struct pci_dev *pci_dev = to_pci_dev(dev);
139
140 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
141 pci_dev->vendor, pci_dev->device,
142 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
143 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
144 (u8)(pci_dev->class));
145}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800146
147static ssize_t is_enabled_store(struct device *dev,
148 struct device_attribute *attr, const char *buf,
149 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200150{
151 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800152 unsigned long val;
153 ssize_t result = strict_strtoul(buf, 0, &val);
154
155 if (result < 0)
156 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200157
158 /* this can crash the machine when done on the "wrong" device */
159 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800160 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200161
Trent Piepho92425a42008-11-30 17:10:12 -0800162 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900163 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800164 pci_disable_device(pdev);
165 else
166 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800167 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800168 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200169
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800170 return result < 0 ? result : count;
171}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200172
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800173static ssize_t is_enabled_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
176 struct pci_dev *pdev;
177
178 pdev = to_pci_dev (dev);
179 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200180}
181
Brice Goglin81bb0e12007-01-28 10:53:40 +0100182#ifdef CONFIG_NUMA
183static ssize_t
184numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
185{
186 return sprintf (buf, "%d\n", dev->numa_node);
187}
188#endif
189
Brice Goglinfe970642006-08-31 01:55:15 -0400190static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800191dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
192{
193 struct pci_dev *pdev = to_pci_dev(dev);
194
195 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
196}
197
198static ssize_t
199consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
200 char *buf)
201{
202 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
203}
204
205static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400206msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
207{
208 struct pci_dev *pdev = to_pci_dev(dev);
209
210 if (!pdev->subordinate)
211 return 0;
212
213 return sprintf (buf, "%u\n",
214 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
215}
216
217static ssize_t
218msi_bus_store(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
220{
221 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800222 unsigned long val;
223
224 if (strict_strtoul(buf, 0, &val) < 0)
225 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400226
227 /* bad things may happen if the no_msi flag is changed
228 * while some drivers are loaded */
229 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800230 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400231
Trent Piepho92425a42008-11-30 17:10:12 -0800232 /* Maybe pci devices without subordinate busses shouldn't even have this
233 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400234 if (!pdev->subordinate)
235 return count;
236
Trent Piepho92425a42008-11-30 17:10:12 -0800237 /* Is the flag going to change, or keep the value it already had? */
238 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
239 !!val) {
240 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400241
Trent Piepho92425a42008-11-30 17:10:12 -0800242 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
243 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400244 }
245
246 return count;
247}
Greg KH98885492005-05-05 11:57:25 -0700248
Alex Chiang705b1aa2009-03-20 14:56:31 -0600249#ifdef CONFIG_HOTPLUG
250static DEFINE_MUTEX(pci_remove_rescan_mutex);
251static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
252 size_t count)
253{
254 unsigned long val;
255 struct pci_bus *b = NULL;
256
257 if (strict_strtoul(buf, 0, &val) < 0)
258 return -EINVAL;
259
260 if (val) {
261 mutex_lock(&pci_remove_rescan_mutex);
262 while ((b = pci_find_next_bus(b)) != NULL)
263 pci_rescan_bus(b);
264 mutex_unlock(&pci_remove_rescan_mutex);
265 }
266 return count;
267}
268
269struct bus_attribute pci_bus_attrs[] = {
270 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
271 __ATTR_NULL
272};
Alex Chiang77c27c72009-03-20 14:56:36 -0600273
Alex Chiang738a6392009-03-20 14:56:41 -0600274static ssize_t
275dev_rescan_store(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
277{
278 unsigned long val;
279 struct pci_dev *pdev = to_pci_dev(dev);
280
281 if (strict_strtoul(buf, 0, &val) < 0)
282 return -EINVAL;
283
284 if (val) {
285 mutex_lock(&pci_remove_rescan_mutex);
286 pci_rescan_bus(pdev->bus);
287 mutex_unlock(&pci_remove_rescan_mutex);
288 }
289 return count;
290}
291
Alex Chiang77c27c72009-03-20 14:56:36 -0600292static void remove_callback(struct device *dev)
293{
294 struct pci_dev *pdev = to_pci_dev(dev);
295
296 mutex_lock(&pci_remove_rescan_mutex);
297 pci_remove_bus_device(pdev);
298 mutex_unlock(&pci_remove_rescan_mutex);
299}
300
301static ssize_t
302remove_store(struct device *dev, struct device_attribute *dummy,
303 const char *buf, size_t count)
304{
305 int ret = 0;
306 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600307
308 if (strict_strtoul(buf, 0, &val) < 0)
309 return -EINVAL;
310
Alex Chiang77c27c72009-03-20 14:56:36 -0600311 /* An attribute cannot be unregistered by one of its own methods,
312 * so we have to use this roundabout approach.
313 */
314 if (val)
315 ret = device_schedule_callback(dev, remove_callback);
316 if (ret)
317 count = ret;
318 return count;
319}
Alex Chiang705b1aa2009-03-20 14:56:31 -0600320#endif
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322struct device_attribute pci_dev_attrs[] = {
323 __ATTR_RO(resource),
324 __ATTR_RO(vendor),
325 __ATTR_RO(device),
326 __ATTR_RO(subsystem_vendor),
327 __ATTR_RO(subsystem_device),
328 __ATTR_RO(class),
329 __ATTR_RO(irq),
330 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700331 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700332 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100333#ifdef CONFIG_NUMA
334 __ATTR_RO(numa_node),
335#endif
Yinghai Lubb965402009-11-24 18:21:21 -0800336 __ATTR_RO(dma_mask_bits),
337 __ATTR_RO(consistent_dma_mask_bits),
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200338 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700339 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
340 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400341 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600342#ifdef CONFIG_HOTPLUG
343 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600344 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 __ATTR_NULL,
347};
348
349static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000350boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
351{
352 struct pci_dev *pdev = to_pci_dev(dev);
353
354 return sprintf(buf, "%u\n",
355 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
356 IORESOURCE_ROM_SHADOW));
357}
358struct device_attribute vga_attr = __ATTR_RO(boot_vga);
359
360static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700361pci_read_config(struct file *filp, struct kobject *kobj,
362 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800363 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
366 unsigned int size = 64;
367 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900368 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* Several chips lock up trying to read undefined config space */
Chris Wrightde139a32010-05-13 10:43:07 -0700371 if (cap_raised(filp->f_cred->cap_effective, CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 size = dev->cfg_size;
373 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
374 size = 128;
375 }
376
377 if (off > size)
378 return 0;
379 if (off + count > size) {
380 size -= off;
381 count = size;
382 } else {
383 size = count;
384 }
385
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900386 if ((off & 1) && size) {
387 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700388 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900389 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900391 size--;
392 }
393
394 if ((off & 3) && size > 2) {
395 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700396 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900397 data[off - init_off] = val & 0xff;
398 data[off - init_off + 1] = (val >> 8) & 0xff;
399 off += 2;
400 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900404 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700405 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900406 data[off - init_off] = val & 0xff;
407 data[off - init_off + 1] = (val >> 8) & 0xff;
408 data[off - init_off + 2] = (val >> 16) & 0xff;
409 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 off += 4;
411 size -= 4;
412 }
413
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900414 if (size >= 2) {
415 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700416 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900417 data[off - init_off] = val & 0xff;
418 data[off - init_off + 1] = (val >> 8) & 0xff;
419 off += 2;
420 size -= 2;
421 }
422
423 if (size > 0) {
424 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700425 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900426 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 off++;
428 --size;
429 }
430
431 return count;
432}
433
434static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700435pci_write_config(struct file* filp, struct kobject *kobj,
436 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800437 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
440 unsigned int size = count;
441 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900442 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (off > dev->cfg_size)
445 return 0;
446 if (off + count > dev->cfg_size) {
447 size = dev->cfg_size - off;
448 count = size;
449 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900450
451 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700452 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900454 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900456
457 if ((off & 3) && size > 2) {
458 u16 val = data[off - init_off];
459 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700460 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900461 off += 2;
462 size -= 2;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900466 u32 val = data[off - init_off];
467 val |= (u32) data[off - init_off + 1] << 8;
468 val |= (u32) data[off - init_off + 2] << 16;
469 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700470 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 off += 4;
472 size -= 4;
473 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900474
475 if (size >= 2) {
476 u16 val = data[off - init_off];
477 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700478 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900479 off += 2;
480 size -= 2;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900483 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700484 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 off++;
486 --size;
487 }
488
489 return count;
490}
491
Ben Hutchings94e61082008-03-05 16:52:39 +0000492static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700493read_vpd_attr(struct file *filp, struct kobject *kobj,
494 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000495 char *buf, loff_t off, size_t count)
496{
497 struct pci_dev *dev =
498 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000499
500 if (off > bin_attr->size)
501 count = 0;
502 else if (count > bin_attr->size - off)
503 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000504
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800505 return pci_read_vpd(dev, off, count, buf);
506}
Ben Hutchings94e61082008-03-05 16:52:39 +0000507
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800508static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700509write_vpd_attr(struct file *filp, struct kobject *kobj,
510 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800511 char *buf, loff_t off, size_t count)
512{
513 struct pci_dev *dev =
514 to_pci_dev(container_of(kobj, struct device, kobj));
515
516 if (off > bin_attr->size)
517 count = 0;
518 else if (count > bin_attr->size - off)
519 count = bin_attr->size - off;
520
521 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#ifdef HAVE_PCI_LEGACY
525/**
526 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700527 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700529 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * @buf: buffer to store results
531 * @off: offset into legacy I/O port space
532 * @count: number of bytes to read
533 *
534 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
535 * callback routine (pci_legacy_read).
536 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000537static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700538pci_read_legacy_io(struct file *filp, struct kobject *kobj,
539 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800540 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400543 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 kobj));
545
546 /* Only support 1, 2 or 4 byte accesses */
547 if (count != 1 && count != 2 && count != 4)
548 return -EINVAL;
549
550 return pci_legacy_read(bus, off, (u32 *)buf, count);
551}
552
553/**
554 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700555 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700557 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * @buf: buffer containing value to be written
559 * @off: offset into legacy I/O port space
560 * @count: number of bytes to write
561 *
562 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
563 * callback routine (pci_legacy_write).
564 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000565static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700566pci_write_legacy_io(struct file *filp, struct kobject *kobj,
567 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800568 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400571 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 kobj));
573 /* Only support 1, 2 or 4 byte accesses */
574 if (count != 1 && count != 2 && count != 4)
575 return -EINVAL;
576
577 return pci_legacy_write(bus, off, *(u32 *)buf, count);
578}
579
580/**
581 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700582 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * @kobj: kobject corresponding to device to be mapped
584 * @attr: struct bin_attribute for this file
585 * @vma: struct vm_area_struct passed to mmap
586 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000587 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * legacy memory space (first meg of bus space) into application virtual
589 * memory space.
590 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000591static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700592pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
593 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct vm_area_struct *vma)
595{
596 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400597 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 kobj));
599
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000600 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
601}
602
603/**
604 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700605 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000606 * @kobj: kobject corresponding to device to be mapped
607 * @attr: struct bin_attribute for this file
608 * @vma: struct vm_area_struct passed to mmap
609 *
610 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
611 * legacy IO space (first meg of bus space) into application virtual
612 * memory space. Returns -ENOSYS if the operation isn't supported
613 */
614static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700615pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
616 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000617 struct vm_area_struct *vma)
618{
619 struct pci_bus *bus = to_pci_bus(container_of(kobj,
620 struct device,
621 kobj));
622
623 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
624}
625
626/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300627 * pci_adjust_legacy_attr - adjustment of legacy file attributes
628 * @b: bus to create files under
629 * @mmap_type: I/O port or memory
630 *
631 * Stub implementation. Can be overridden by arch if necessary.
632 */
633void __weak
634pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
635{
636 return;
637}
638
639/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000640 * pci_create_legacy_files - create legacy I/O port and memory files
641 * @b: bus to create files under
642 *
643 * Some platforms allow access to legacy I/O port and ISA memory space on
644 * a per-bus basis. This routine creates the files and ties them into
645 * their associated read, write and mmap files from pci-sysfs.c
646 *
647 * On error unwind, but don't propogate the error to the caller
648 * as it is ok to set up the PCI bus without these files.
649 */
650void pci_create_legacy_files(struct pci_bus *b)
651{
652 int error;
653
654 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
655 GFP_ATOMIC);
656 if (!b->legacy_io)
657 goto kzalloc_err;
658
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100659 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000660 b->legacy_io->attr.name = "legacy_io";
661 b->legacy_io->size = 0xffff;
662 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
663 b->legacy_io->read = pci_read_legacy_io;
664 b->legacy_io->write = pci_write_legacy_io;
665 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300666 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000667 error = device_create_bin_file(&b->dev, b->legacy_io);
668 if (error)
669 goto legacy_io_err;
670
671 /* Allocated above after the legacy_io struct */
672 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000673 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000674 b->legacy_mem->attr.name = "legacy_mem";
675 b->legacy_mem->size = 1024*1024;
676 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
677 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300678 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000679 error = device_create_bin_file(&b->dev, b->legacy_mem);
680 if (error)
681 goto legacy_mem_err;
682
683 return;
684
685legacy_mem_err:
686 device_remove_bin_file(&b->dev, b->legacy_io);
687legacy_io_err:
688 kfree(b->legacy_io);
689 b->legacy_io = NULL;
690kzalloc_err:
691 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
692 "and ISA memory resources to sysfs\n");
693 return;
694}
695
696void pci_remove_legacy_files(struct pci_bus *b)
697{
698 if (b->legacy_io) {
699 device_remove_bin_file(&b->dev, b->legacy_io);
700 device_remove_bin_file(&b->dev, b->legacy_mem);
701 kfree(b->legacy_io); /* both are allocated here */
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704#endif /* HAVE_PCI_LEGACY */
705
706#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700707
Martin Wilck3b519e42010-11-10 11:03:21 +0100708int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
709 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700710{
Martin Wilck3b519e42010-11-10 11:03:21 +0100711 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700712
Martin Wilck3b519e42010-11-10 11:03:21 +0100713 if (pci_resource_len(pdev, resno) == 0)
714 return 0;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700715 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
716 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800717 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Martin Wilck3b519e42010-11-10 11:03:21 +0100718 pci_start = (mmap_api == PCI_MMAP_SYSFS) ?
719 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
720 if (start >= pci_start && start < pci_start + size &&
721 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700722 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700723 return 0;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/**
727 * pci_mmap_resource - map a PCI resource into user memory space
728 * @kobj: kobject for mapping
729 * @attr: struct bin_attribute for the file being mapped
730 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700731 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
733 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735static int
736pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700737 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
740 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400741 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700743 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000744 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000746 for (i = 0; i < PCI_ROM_RESOURCE; i++)
747 if (res == &pdev->resource[i])
748 break;
749 if (i >= PCI_ROM_RESOURCE)
750 return -ENODEV;
751
Martin Wilck3b519e42010-11-10 11:03:21 +0100752 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
753 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
754 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
755 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
756 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800757 (u64)pci_resource_start(pdev, i),
758 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700759 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100760 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700761
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000762 /* pci_mmap_page_range() expects the same kind of entry as coming
763 * from /proc/bus/pci/ which is a "user visible" value. If this is
764 * different from the resource itself, arch will do necessary fixup.
765 */
766 pci_resource_to_user(pdev, i, res, &start, &end);
767 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
769
Arjan van de Vene8de1482008-10-22 19:55:31 -0700770 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
771 return -EINVAL;
772
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700773 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
774}
775
776static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700777pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
778 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700779 struct vm_area_struct *vma)
780{
781 return pci_mmap_resource(kobj, attr, vma, 0);
782}
783
784static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700785pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
786 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700787 struct vm_area_struct *vma)
788{
789 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Alex Williamson86333282010-07-19 09:45:34 -0600792static ssize_t
793pci_resource_io(struct file *filp, struct kobject *kobj,
794 struct bin_attribute *attr, char *buf,
795 loff_t off, size_t count, bool write)
796{
797 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
798 struct device, kobj));
799 struct resource *res = attr->private;
800 unsigned long port = off;
801 int i;
802
803 for (i = 0; i < PCI_ROM_RESOURCE; i++)
804 if (res == &pdev->resource[i])
805 break;
806 if (i >= PCI_ROM_RESOURCE)
807 return -ENODEV;
808
809 port += pci_resource_start(pdev, i);
810
811 if (port > pci_resource_end(pdev, i))
812 return 0;
813
814 if (port + count - 1 > pci_resource_end(pdev, i))
815 return -EINVAL;
816
817 switch (count) {
818 case 1:
819 if (write)
820 outb(*(u8 *)buf, port);
821 else
822 *(u8 *)buf = inb(port);
823 return 1;
824 case 2:
825 if (write)
826 outw(*(u16 *)buf, port);
827 else
828 *(u16 *)buf = inw(port);
829 return 2;
830 case 4:
831 if (write)
832 outl(*(u32 *)buf, port);
833 else
834 *(u32 *)buf = inl(port);
835 return 4;
836 }
837 return -EINVAL;
838}
839
840static ssize_t
841pci_read_resource_io(struct file *filp, struct kobject *kobj,
842 struct bin_attribute *attr, char *buf,
843 loff_t off, size_t count)
844{
845 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
846}
847
848static ssize_t
849pci_write_resource_io(struct file *filp, struct kobject *kobj,
850 struct bin_attribute *attr, char *buf,
851 loff_t off, size_t count)
852{
853 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700857 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700858 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700859 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700860 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700861 * free their resources.
862 */
863static void
864pci_remove_resource_files(struct pci_dev *pdev)
865{
866 int i;
867
868 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
869 struct bin_attribute *res_attr;
870
871 res_attr = pdev->res_attr[i];
872 if (res_attr) {
873 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
874 kfree(res_attr);
875 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700876
877 res_attr = pdev->res_attr_wc[i];
878 if (res_attr) {
879 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
880 kfree(res_attr);
881 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700882 }
883}
884
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700885static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
886{
887 /* allocate attribute structure, piggyback attribute name */
888 int name_len = write_combine ? 13 : 10;
889 struct bin_attribute *res_attr;
890 int retval;
891
892 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
893 if (res_attr) {
894 char *res_attr_name = (char *)(res_attr + 1);
895
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800896 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700897 if (write_combine) {
898 pdev->res_attr_wc[num] = res_attr;
899 sprintf(res_attr_name, "resource%d_wc", num);
900 res_attr->mmap = pci_mmap_resource_wc;
901 } else {
902 pdev->res_attr[num] = res_attr;
903 sprintf(res_attr_name, "resource%d", num);
904 res_attr->mmap = pci_mmap_resource_uc;
905 }
Alex Williamson86333282010-07-19 09:45:34 -0600906 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
907 res_attr->read = pci_read_resource_io;
908 res_attr->write = pci_write_resource_io;
909 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700910 res_attr->attr.name = res_attr_name;
911 res_attr->attr.mode = S_IRUSR | S_IWUSR;
912 res_attr->size = pci_resource_len(pdev, num);
913 res_attr->private = &pdev->resource[num];
914 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
915 } else
916 retval = -ENOMEM;
917
918 return retval;
919}
920
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700921/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700923 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700925 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700927static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700930 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* Expose the PCI resources from this device as files */
933 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* skip empty resources */
936 if (!pci_resource_len(pdev, i))
937 continue;
938
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700939 retval = pci_create_attr(pdev, i, 0);
940 /* for prefetchable resources, create a WC mappable file */
941 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
942 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500943
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700944 if (retval) {
945 pci_remove_resource_files(pdev);
946 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300952int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
953void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#endif /* HAVE_PCI_MMAP */
955
956/**
957 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -0700958 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700960 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * @buf: user input
962 * @off: file offset
963 * @count: number of byte in input
964 *
965 * writing anything except 0 enables it
966 */
967static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700968pci_write_rom(struct file *filp, struct kobject *kobj,
969 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800970 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
973
974 if ((off == 0) && (*buf == '0') && (count == 2))
975 pdev->rom_attr_enabled = 0;
976 else
977 pdev->rom_attr_enabled = 1;
978
979 return count;
980}
981
982/**
983 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -0700984 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700986 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * @buf: where to put the data we read from the ROM
988 * @off: file offset
989 * @count: number of bytes to read
990 *
991 * Put @count bytes starting at @off into @buf from the ROM in the PCI
992 * device corresponding to @kobj.
993 */
994static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700995pci_read_rom(struct file *filp, struct kobject *kobj,
996 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800997 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1000 void __iomem *rom;
1001 size_t size;
1002
1003 if (!pdev->rom_attr_enabled)
1004 return -EINVAL;
1005
1006 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001007 if (!rom || !size)
1008 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (off >= size)
1011 count = 0;
1012 else {
1013 if (off + count > size)
1014 count = size - off;
1015
1016 memcpy_fromio(buf, rom + off, count);
1017 }
1018 pci_unmap_rom(pdev, rom);
1019
1020 return count;
1021}
1022
1023static struct bin_attribute pci_config_attr = {
1024 .attr = {
1025 .name = "config",
1026 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001028 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 .read = pci_read_config,
1030 .write = pci_write_config,
1031};
1032
1033static struct bin_attribute pcie_config_attr = {
1034 .attr = {
1035 .name = "config",
1036 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001038 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 .read = pci_read_config,
1040 .write = pci_write_config,
1041};
1042
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001043int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001044{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001045 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001046}
1047
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001048static ssize_t reset_store(struct device *dev,
1049 struct device_attribute *attr, const char *buf,
1050 size_t count)
1051{
1052 struct pci_dev *pdev = to_pci_dev(dev);
1053 unsigned long val;
1054 ssize_t result = strict_strtoul(buf, 0, &val);
1055
1056 if (result < 0)
1057 return result;
1058
1059 if (val != 1)
1060 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001061
1062 result = pci_reset_function(pdev);
1063 if (result < 0)
1064 return result;
1065
1066 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001067}
1068
1069static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1070
Zhao, Yu280c73d2008-10-13 20:01:00 +08001071static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1072{
1073 int retval;
1074 struct bin_attribute *attr;
1075
1076 /* If the device has VPD, try to expose it in sysfs. */
1077 if (dev->vpd) {
1078 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1079 if (!attr)
1080 return -ENOMEM;
1081
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001082 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001083 attr->size = dev->vpd->len;
1084 attr->attr.name = "vpd";
1085 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001086 attr->read = read_vpd_attr;
1087 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001088 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1089 if (retval) {
1090 kfree(dev->vpd->attr);
1091 return retval;
1092 }
1093 dev->vpd->attr = attr;
1094 }
1095
1096 /* Active State Power Management */
1097 pcie_aspm_create_sysfs_dev_files(dev);
1098
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001099 if (!pci_probe_reset_function(dev)) {
1100 retval = device_create_file(&dev->dev, &reset_attr);
1101 if (retval)
1102 goto error;
1103 dev->reset_fn = 1;
1104 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001105 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001106
1107error:
1108 pcie_aspm_remove_sysfs_dev_files(dev);
1109 if (dev->vpd && dev->vpd->attr) {
1110 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1111 kfree(dev->vpd->attr);
1112 }
1113
1114 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001115}
1116
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001117int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001119 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001120 int rom_size = 0;
1121 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!sysfs_initialized)
1124 return -EACCES;
1125
Zhao, Yu557848c2008-10-13 19:18:07 +08001126 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001127 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001129 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1130 if (retval)
1131 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001133 retval = pci_create_resource_files(pdev);
1134 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001135 goto err_config_file;
1136
1137 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1138 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1139 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1140 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001143 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001144 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001145 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001146 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001147 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001149 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001150 attr->size = rom_size;
1151 attr->attr.name = "rom";
1152 attr->attr.mode = S_IRUSR;
1153 attr->read = pci_read_rom;
1154 attr->write = pci_write_rom;
1155 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1156 if (retval) {
1157 kfree(attr);
1158 goto err_resource_files;
1159 }
1160 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001162
Dave Airlie217f45d2009-03-04 05:57:05 +00001163 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1164 retval = device_create_file(&pdev->dev, &vga_attr);
1165 if (retval)
1166 goto err_rom_file;
1167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001170 retval = pcibios_add_platform_entries(pdev);
1171 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001172 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001173
Zhao, Yu280c73d2008-10-13 20:01:00 +08001174 /* add sysfs entries for various capabilities */
1175 retval = pci_create_capabilities_sysfs(pdev);
1176 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001177 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001178
Narendra K911e1c92010-07-26 05:56:50 -05001179 pci_create_firmware_label_files(pdev);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001182
Dave Airlie217f45d2009-03-04 05:57:05 +00001183err_vga_file:
1184 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1185 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001186err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001187 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001188 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001189 kfree(pdev->rom_attr);
1190 pdev->rom_attr = NULL;
1191 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001192err_resource_files:
1193 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001194err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001195 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001196 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1197 else
1198 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1199err:
1200 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Zhao, Yu280c73d2008-10-13 20:01:00 +08001203static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1204{
1205 if (dev->vpd && dev->vpd->attr) {
1206 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1207 kfree(dev->vpd->attr);
1208 }
1209
1210 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001211 if (dev->reset_fn) {
1212 device_remove_file(&dev->dev, &reset_attr);
1213 dev->reset_fn = 0;
1214 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/**
1218 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1219 * @pdev: device whose entries we should free
1220 *
1221 * Cleanup when @pdev is removed from sysfs.
1222 */
1223void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1224{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001225 int rom_size = 0;
1226
David Millerd67afe52006-11-10 12:27:48 -08001227 if (!sysfs_initialized)
1228 return;
1229
Zhao, Yu280c73d2008-10-13 20:01:00 +08001230 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001231
Zhao, Yu557848c2008-10-13 19:18:07 +08001232 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1234 else
1235 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1236
1237 pci_remove_resource_files(pdev);
1238
Zhao, Yu280c73d2008-10-13 20:01:00 +08001239 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1240 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1241 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1242 rom_size = 0x20000;
1243
1244 if (rom_size && pdev->rom_attr) {
1245 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1246 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Narendra K911e1c92010-07-26 05:56:50 -05001248
1249 pci_remove_firmware_label_files(pdev);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253static int __init pci_sysfs_init(void)
1254{
1255 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001256 int retval;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001259 for_each_pci_dev(pdev) {
1260 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001261 if (retval) {
1262 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001263 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001264 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 return 0;
1268}
1269
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001270late_initcall(pci_sysfs_init);