blob: c690abc0e5b80e138f600a0321750993eb66e58a [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>
Chris Wrighta628e7b2011-02-14 17:21:49 -080026#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080027#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "pci.h"
30
31static int sysfs_initialized; /* = 0 */
32
33/* show configuration fields */
34#define pci_config_attr(field, format_string) \
35static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040036field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{ \
38 struct pci_dev *pdev; \
39 \
40 pdev = to_pci_dev (dev); \
41 return sprintf (buf, format_string, pdev->field); \
42}
43
44pci_config_attr(vendor, "0x%04x\n");
45pci_config_attr(device, "0x%04x\n");
46pci_config_attr(subsystem_vendor, "0x%04x\n");
47pci_config_attr(subsystem_device, "0x%04x\n");
48pci_config_attr(class, "0x%06x\n");
49pci_config_attr(irq, "%u\n");
50
Doug Thompsonbdee9d92006-06-14 16:59:48 -070051static ssize_t broken_parity_status_show(struct device *dev,
52 struct device_attribute *attr,
53 char *buf)
54{
55 struct pci_dev *pdev = to_pci_dev(dev);
56 return sprintf (buf, "%u\n", pdev->broken_parity_status);
57}
58
59static ssize_t broken_parity_status_store(struct device *dev,
60 struct device_attribute *attr,
61 const char *buf, size_t count)
62{
63 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080064 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070065
Trent Piepho92425a42008-11-30 17:10:12 -080066 if (strict_strtoul(buf, 0, &val) < 0)
67 return -EINVAL;
68
69 pdev->broken_parity_status = !!val;
70
71 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070072}
73
Alan Cox4327edf2005-09-10 00:25:49 -070074static ssize_t local_cpus_show(struct device *dev,
75 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Mike Travis3be83052009-01-04 05:18:01 -080077 const struct cpumask *mask;
Alan Cox4327edf2005-09-10 00:25:49 -070078 int len;
79
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020080#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053081 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
82 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020083#else
Mike Travis3be83052009-01-04 05:18:01 -080084 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020085#endif
Mike Travis3be83052009-01-04 05:18:01 -080086 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070087 buf[len++] = '\n';
88 buf[len] = '\0';
89 return len;
90}
91
92
93static ssize_t local_cpulist_show(struct device *dev,
94 struct device_attribute *attr, char *buf)
95{
Mike Travis3be83052009-01-04 05:18:01 -080096 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070097 int len;
98
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020099#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +0530100 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
101 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200102#else
Mike Travis3be83052009-01-04 05:18:01 -0800103 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200104#endif
Mike Travis3be83052009-01-04 05:18:01 -0800105 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -0700106 buf[len++] = '\n';
107 buf[len] = '\0';
108 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111/* show resources */
112static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400113resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct pci_dev * pci_dev = to_pci_dev(dev);
116 char * str = buf;
117 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800118 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700119 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (pci_dev->subordinate)
122 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800123 else
124 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000127 struct resource *res = &pci_dev->resource[i];
128 pci_resource_to_user(pci_dev, i, res, &start, &end);
129 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
130 (unsigned long long)start,
131 (unsigned long long)end,
132 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 return (str - buf);
135}
136
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200137static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700138{
139 struct pci_dev *pci_dev = to_pci_dev(dev);
140
141 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
142 pci_dev->vendor, pci_dev->device,
143 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
144 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
145 (u8)(pci_dev->class));
146}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800147
148static ssize_t is_enabled_store(struct device *dev,
149 struct device_attribute *attr, const char *buf,
150 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200151{
152 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800153 unsigned long val;
154 ssize_t result = strict_strtoul(buf, 0, &val);
155
156 if (result < 0)
157 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200158
159 /* this can crash the machine when done on the "wrong" device */
160 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800161 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200162
Trent Piepho92425a42008-11-30 17:10:12 -0800163 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900164 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800165 pci_disable_device(pdev);
166 else
167 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800168 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800169 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200170
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800171 return result < 0 ? result : count;
172}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200173
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800174static ssize_t is_enabled_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
176{
177 struct pci_dev *pdev;
178
179 pdev = to_pci_dev (dev);
180 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200181}
182
Brice Goglin81bb0e12007-01-28 10:53:40 +0100183#ifdef CONFIG_NUMA
184static ssize_t
185numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
186{
187 return sprintf (buf, "%d\n", dev->numa_node);
188}
189#endif
190
Brice Goglinfe970642006-08-31 01:55:15 -0400191static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800192dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
193{
194 struct pci_dev *pdev = to_pci_dev(dev);
195
196 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
197}
198
199static ssize_t
200consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
201 char *buf)
202{
203 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
204}
205
206static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400207msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
208{
209 struct pci_dev *pdev = to_pci_dev(dev);
210
211 if (!pdev->subordinate)
212 return 0;
213
214 return sprintf (buf, "%u\n",
215 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
216}
217
218static ssize_t
219msi_bus_store(struct device *dev, struct device_attribute *attr,
220 const char *buf, size_t count)
221{
222 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800223 unsigned long val;
224
225 if (strict_strtoul(buf, 0, &val) < 0)
226 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400227
228 /* bad things may happen if the no_msi flag is changed
229 * while some drivers are loaded */
230 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800231 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400232
Trent Piepho92425a42008-11-30 17:10:12 -0800233 /* Maybe pci devices without subordinate busses shouldn't even have this
234 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400235 if (!pdev->subordinate)
236 return count;
237
Trent Piepho92425a42008-11-30 17:10:12 -0800238 /* Is the flag going to change, or keep the value it already had? */
239 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
240 !!val) {
241 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400242
Trent Piepho92425a42008-11-30 17:10:12 -0800243 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
244 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400245 }
246
247 return count;
248}
Greg KH98885492005-05-05 11:57:25 -0700249
Alex Chiang705b1aa2009-03-20 14:56:31 -0600250#ifdef CONFIG_HOTPLUG
251static DEFINE_MUTEX(pci_remove_rescan_mutex);
252static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
253 size_t count)
254{
255 unsigned long val;
256 struct pci_bus *b = NULL;
257
258 if (strict_strtoul(buf, 0, &val) < 0)
259 return -EINVAL;
260
261 if (val) {
262 mutex_lock(&pci_remove_rescan_mutex);
263 while ((b = pci_find_next_bus(b)) != NULL)
264 pci_rescan_bus(b);
265 mutex_unlock(&pci_remove_rescan_mutex);
266 }
267 return count;
268}
269
270struct bus_attribute pci_bus_attrs[] = {
271 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
272 __ATTR_NULL
273};
Alex Chiang77c27c72009-03-20 14:56:36 -0600274
Alex Chiang738a6392009-03-20 14:56:41 -0600275static ssize_t
276dev_rescan_store(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
278{
279 unsigned long val;
280 struct pci_dev *pdev = to_pci_dev(dev);
281
282 if (strict_strtoul(buf, 0, &val) < 0)
283 return -EINVAL;
284
285 if (val) {
286 mutex_lock(&pci_remove_rescan_mutex);
287 pci_rescan_bus(pdev->bus);
288 mutex_unlock(&pci_remove_rescan_mutex);
289 }
290 return count;
291}
292
Alex Chiang77c27c72009-03-20 14:56:36 -0600293static void remove_callback(struct device *dev)
294{
295 struct pci_dev *pdev = to_pci_dev(dev);
296
297 mutex_lock(&pci_remove_rescan_mutex);
298 pci_remove_bus_device(pdev);
299 mutex_unlock(&pci_remove_rescan_mutex);
300}
301
302static ssize_t
303remove_store(struct device *dev, struct device_attribute *dummy,
304 const char *buf, size_t count)
305{
306 int ret = 0;
307 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600308
309 if (strict_strtoul(buf, 0, &val) < 0)
310 return -EINVAL;
311
Alex Chiang77c27c72009-03-20 14:56:36 -0600312 /* An attribute cannot be unregistered by one of its own methods,
313 * so we have to use this roundabout approach.
314 */
315 if (val)
316 ret = device_schedule_callback(dev, remove_callback);
317 if (ret)
318 count = ret;
319 return count;
320}
Yinghai Lub9d320f2011-05-12 17:11:39 -0700321
322static ssize_t
323dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
324 const char *buf, size_t count)
325{
326 unsigned long val;
327 struct pci_bus *bus = to_pci_bus(dev);
328
329 if (strict_strtoul(buf, 0, &val) < 0)
330 return -EINVAL;
331
332 if (val) {
333 mutex_lock(&pci_remove_rescan_mutex);
334 pci_rescan_bus(bus);
335 mutex_unlock(&pci_remove_rescan_mutex);
336 }
337 return count;
338}
339
Alex Chiang705b1aa2009-03-20 14:56:31 -0600340#endif
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342struct device_attribute pci_dev_attrs[] = {
343 __ATTR_RO(resource),
344 __ATTR_RO(vendor),
345 __ATTR_RO(device),
346 __ATTR_RO(subsystem_vendor),
347 __ATTR_RO(subsystem_device),
348 __ATTR_RO(class),
349 __ATTR_RO(irq),
350 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700351 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700352 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100353#ifdef CONFIG_NUMA
354 __ATTR_RO(numa_node),
355#endif
Yinghai Lubb965402009-11-24 18:21:21 -0800356 __ATTR_RO(dma_mask_bits),
357 __ATTR_RO(consistent_dma_mask_bits),
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200358 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700359 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
360 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400361 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600362#ifdef CONFIG_HOTPLUG
363 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
Alex Chiang738a6392009-03-20 14:56:41 -0600364 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
Alex Chiang77c27c72009-03-20 14:56:36 -0600365#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 __ATTR_NULL,
367};
368
Yinghai Lub9d320f2011-05-12 17:11:39 -0700369struct device_attribute pcibus_dev_attrs[] = {
370#ifdef CONFIG_HOTPLUG
371 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store),
372#endif
373 __ATTR_NULL,
374};
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000377boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
378{
379 struct pci_dev *pdev = to_pci_dev(dev);
380
381 return sprintf(buf, "%u\n",
382 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
383 IORESOURCE_ROM_SHADOW));
384}
385struct device_attribute vga_attr = __ATTR_RO(boot_vga);
386
387static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700388pci_read_config(struct file *filp, struct kobject *kobj,
389 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800390 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
393 unsigned int size = 64;
394 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900395 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* Several chips lock up trying to read undefined config space */
Serge E. Hallyn34867402011-03-23 16:43:17 -0700398 if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 size = dev->cfg_size;
400 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
401 size = 128;
402 }
403
404 if (off > size)
405 return 0;
406 if (off + count > size) {
407 size -= off;
408 count = size;
409 } else {
410 size = count;
411 }
412
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900413 if ((off & 1) && size) {
414 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700415 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900416 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900418 size--;
419 }
420
421 if ((off & 3) && size > 2) {
422 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700423 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900424 data[off - init_off] = val & 0xff;
425 data[off - init_off + 1] = (val >> 8) & 0xff;
426 off += 2;
427 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900431 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700432 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900433 data[off - init_off] = val & 0xff;
434 data[off - init_off + 1] = (val >> 8) & 0xff;
435 data[off - init_off + 2] = (val >> 16) & 0xff;
436 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 off += 4;
438 size -= 4;
439 }
440
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900441 if (size >= 2) {
442 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700443 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900444 data[off - init_off] = val & 0xff;
445 data[off - init_off + 1] = (val >> 8) & 0xff;
446 off += 2;
447 size -= 2;
448 }
449
450 if (size > 0) {
451 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700452 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900453 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 off++;
455 --size;
456 }
457
458 return count;
459}
460
461static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700462pci_write_config(struct file* filp, struct kobject *kobj,
463 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800464 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
467 unsigned int size = count;
468 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900469 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (off > dev->cfg_size)
472 return 0;
473 if (off + count > dev->cfg_size) {
474 size = dev->cfg_size - off;
475 count = size;
476 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900477
478 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700479 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900481 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900483
484 if ((off & 3) && size > 2) {
485 u16 val = data[off - init_off];
486 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700487 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900488 off += 2;
489 size -= 2;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900493 u32 val = data[off - init_off];
494 val |= (u32) data[off - init_off + 1] << 8;
495 val |= (u32) data[off - init_off + 2] << 16;
496 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700497 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 off += 4;
499 size -= 4;
500 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900501
502 if (size >= 2) {
503 u16 val = data[off - init_off];
504 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700505 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900506 off += 2;
507 size -= 2;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900510 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700511 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 off++;
513 --size;
514 }
515
516 return count;
517}
518
Ben Hutchings94e61082008-03-05 16:52:39 +0000519static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700520read_vpd_attr(struct file *filp, struct kobject *kobj,
521 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000522 char *buf, loff_t off, size_t count)
523{
524 struct pci_dev *dev =
525 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000526
527 if (off > bin_attr->size)
528 count = 0;
529 else if (count > bin_attr->size - off)
530 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000531
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800532 return pci_read_vpd(dev, off, count, buf);
533}
Ben Hutchings94e61082008-03-05 16:52:39 +0000534
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800535static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700536write_vpd_attr(struct file *filp, struct kobject *kobj,
537 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800538 char *buf, loff_t off, size_t count)
539{
540 struct pci_dev *dev =
541 to_pci_dev(container_of(kobj, struct device, kobj));
542
543 if (off > bin_attr->size)
544 count = 0;
545 else if (count > bin_attr->size - off)
546 count = bin_attr->size - off;
547
548 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#ifdef HAVE_PCI_LEGACY
552/**
553 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700554 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700556 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * @buf: buffer to store results
558 * @off: offset into legacy I/O port space
559 * @count: number of bytes to read
560 *
561 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
562 * callback routine (pci_legacy_read).
563 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000564static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700565pci_read_legacy_io(struct file *filp, struct kobject *kobj,
566 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800567 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400570 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 kobj));
572
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_read(bus, off, (u32 *)buf, count);
578}
579
580/**
581 * pci_write_legacy_io - write byte(s) to legacy I/O port 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 file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700584 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * @buf: buffer containing value to be written
586 * @off: offset into legacy I/O port space
587 * @count: number of bytes to write
588 *
589 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
590 * callback routine (pci_legacy_write).
591 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000592static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700593pci_write_legacy_io(struct file *filp, struct kobject *kobj,
594 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800595 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400598 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kobj));
600 /* Only support 1, 2 or 4 byte accesses */
601 if (count != 1 && count != 2 && count != 4)
602 return -EINVAL;
603
604 return pci_legacy_write(bus, off, *(u32 *)buf, count);
605}
606
607/**
608 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700609 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * @kobj: kobject corresponding to device to be mapped
611 * @attr: struct bin_attribute for this file
612 * @vma: struct vm_area_struct passed to mmap
613 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000614 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * legacy memory space (first meg of bus space) into application virtual
616 * memory space.
617 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000618static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700619pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
620 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct vm_area_struct *vma)
622{
623 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400624 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 kobj));
626
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000627 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
628}
629
630/**
631 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700632 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000633 * @kobj: kobject corresponding to device to be mapped
634 * @attr: struct bin_attribute for this file
635 * @vma: struct vm_area_struct passed to mmap
636 *
637 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
638 * legacy IO space (first meg of bus space) into application virtual
639 * memory space. Returns -ENOSYS if the operation isn't supported
640 */
641static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700642pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
643 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000644 struct vm_area_struct *vma)
645{
646 struct pci_bus *bus = to_pci_bus(container_of(kobj,
647 struct device,
648 kobj));
649
650 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
651}
652
653/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300654 * pci_adjust_legacy_attr - adjustment of legacy file attributes
655 * @b: bus to create files under
656 * @mmap_type: I/O port or memory
657 *
658 * Stub implementation. Can be overridden by arch if necessary.
659 */
660void __weak
661pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
662{
663 return;
664}
665
666/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000667 * pci_create_legacy_files - create legacy I/O port and memory files
668 * @b: bus to create files under
669 *
670 * Some platforms allow access to legacy I/O port and ISA memory space on
671 * a per-bus basis. This routine creates the files and ties them into
672 * their associated read, write and mmap files from pci-sysfs.c
673 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300674 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000675 * as it is ok to set up the PCI bus without these files.
676 */
677void pci_create_legacy_files(struct pci_bus *b)
678{
679 int error;
680
681 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
682 GFP_ATOMIC);
683 if (!b->legacy_io)
684 goto kzalloc_err;
685
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100686 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000687 b->legacy_io->attr.name = "legacy_io";
688 b->legacy_io->size = 0xffff;
689 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
690 b->legacy_io->read = pci_read_legacy_io;
691 b->legacy_io->write = pci_write_legacy_io;
692 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300693 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000694 error = device_create_bin_file(&b->dev, b->legacy_io);
695 if (error)
696 goto legacy_io_err;
697
698 /* Allocated above after the legacy_io struct */
699 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000700 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000701 b->legacy_mem->attr.name = "legacy_mem";
702 b->legacy_mem->size = 1024*1024;
703 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
704 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300705 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000706 error = device_create_bin_file(&b->dev, b->legacy_mem);
707 if (error)
708 goto legacy_mem_err;
709
710 return;
711
712legacy_mem_err:
713 device_remove_bin_file(&b->dev, b->legacy_io);
714legacy_io_err:
715 kfree(b->legacy_io);
716 b->legacy_io = NULL;
717kzalloc_err:
718 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
719 "and ISA memory resources to sysfs\n");
720 return;
721}
722
723void pci_remove_legacy_files(struct pci_bus *b)
724{
725 if (b->legacy_io) {
726 device_remove_bin_file(&b->dev, b->legacy_io);
727 device_remove_bin_file(&b->dev, b->legacy_mem);
728 kfree(b->legacy_io); /* both are allocated here */
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731#endif /* HAVE_PCI_LEGACY */
732
733#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700734
Martin Wilck3b519e42010-11-10 11:03:21 +0100735int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
736 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700737{
Martin Wilck3b519e42010-11-10 11:03:21 +0100738 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700739
Martin Wilck3b519e42010-11-10 11:03:21 +0100740 if (pci_resource_len(pdev, resno) == 0)
741 return 0;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700742 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
743 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800744 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800745 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100746 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
747 if (start >= pci_start && start < pci_start + size &&
748 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700749 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700750 return 0;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/**
754 * pci_mmap_resource - map a PCI resource into user memory space
755 * @kobj: kobject for mapping
756 * @attr: struct bin_attribute for the file being mapped
757 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700758 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
760 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
762static int
763pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700764 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
767 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400768 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700770 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000773 for (i = 0; i < PCI_ROM_RESOURCE; i++)
774 if (res == &pdev->resource[i])
775 break;
776 if (i >= PCI_ROM_RESOURCE)
777 return -ENODEV;
778
Martin Wilck3b519e42010-11-10 11:03:21 +0100779 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
780 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
781 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
782 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
783 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800784 (u64)pci_resource_start(pdev, i),
785 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700786 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100787 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700788
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000789 /* pci_mmap_page_range() expects the same kind of entry as coming
790 * from /proc/bus/pci/ which is a "user visible" value. If this is
791 * different from the resource itself, arch will do necessary fixup.
792 */
793 pci_resource_to_user(pdev, i, res, &start, &end);
794 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
796
Arjan van de Vene8de1482008-10-22 19:55:31 -0700797 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
798 return -EINVAL;
799
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700800 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
801}
802
803static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700804pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
805 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700806 struct vm_area_struct *vma)
807{
808 return pci_mmap_resource(kobj, attr, vma, 0);
809}
810
811static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700812pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
813 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700814 struct vm_area_struct *vma)
815{
816 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Alex Williamson86333282010-07-19 09:45:34 -0600819static ssize_t
820pci_resource_io(struct file *filp, struct kobject *kobj,
821 struct bin_attribute *attr, char *buf,
822 loff_t off, size_t count, bool write)
823{
824 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
825 struct device, kobj));
826 struct resource *res = attr->private;
827 unsigned long port = off;
828 int i;
829
830 for (i = 0; i < PCI_ROM_RESOURCE; i++)
831 if (res == &pdev->resource[i])
832 break;
833 if (i >= PCI_ROM_RESOURCE)
834 return -ENODEV;
835
836 port += pci_resource_start(pdev, i);
837
838 if (port > pci_resource_end(pdev, i))
839 return 0;
840
841 if (port + count - 1 > pci_resource_end(pdev, i))
842 return -EINVAL;
843
844 switch (count) {
845 case 1:
846 if (write)
847 outb(*(u8 *)buf, port);
848 else
849 *(u8 *)buf = inb(port);
850 return 1;
851 case 2:
852 if (write)
853 outw(*(u16 *)buf, port);
854 else
855 *(u16 *)buf = inw(port);
856 return 2;
857 case 4:
858 if (write)
859 outl(*(u32 *)buf, port);
860 else
861 *(u32 *)buf = inl(port);
862 return 4;
863 }
864 return -EINVAL;
865}
866
867static ssize_t
868pci_read_resource_io(struct file *filp, struct kobject *kobj,
869 struct bin_attribute *attr, char *buf,
870 loff_t off, size_t count)
871{
872 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
873}
874
875static ssize_t
876pci_write_resource_io(struct file *filp, struct kobject *kobj,
877 struct bin_attribute *attr, char *buf,
878 loff_t off, size_t count)
879{
880 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700884 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700885 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700886 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700887 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700888 * free their resources.
889 */
890static void
891pci_remove_resource_files(struct pci_dev *pdev)
892{
893 int i;
894
895 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
896 struct bin_attribute *res_attr;
897
898 res_attr = pdev->res_attr[i];
899 if (res_attr) {
900 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
901 kfree(res_attr);
902 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700903
904 res_attr = pdev->res_attr_wc[i];
905 if (res_attr) {
906 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
907 kfree(res_attr);
908 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700909 }
910}
911
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700912static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
913{
914 /* allocate attribute structure, piggyback attribute name */
915 int name_len = write_combine ? 13 : 10;
916 struct bin_attribute *res_attr;
917 int retval;
918
919 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
920 if (res_attr) {
921 char *res_attr_name = (char *)(res_attr + 1);
922
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800923 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700924 if (write_combine) {
925 pdev->res_attr_wc[num] = res_attr;
926 sprintf(res_attr_name, "resource%d_wc", num);
927 res_attr->mmap = pci_mmap_resource_wc;
928 } else {
929 pdev->res_attr[num] = res_attr;
930 sprintf(res_attr_name, "resource%d", num);
931 res_attr->mmap = pci_mmap_resource_uc;
932 }
Alex Williamson86333282010-07-19 09:45:34 -0600933 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
934 res_attr->read = pci_read_resource_io;
935 res_attr->write = pci_write_resource_io;
936 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700937 res_attr->attr.name = res_attr_name;
938 res_attr->attr.mode = S_IRUSR | S_IWUSR;
939 res_attr->size = pci_resource_len(pdev, num);
940 res_attr->private = &pdev->resource[num];
941 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
942 } else
943 retval = -ENOMEM;
944
945 return retval;
946}
947
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700948/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700950 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700952 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700954static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700957 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* Expose the PCI resources from this device as files */
960 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /* skip empty resources */
963 if (!pci_resource_len(pdev, i))
964 continue;
965
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700966 retval = pci_create_attr(pdev, i, 0);
967 /* for prefetchable resources, create a WC mappable file */
968 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
969 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500970
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700971 if (retval) {
972 pci_remove_resource_files(pdev);
973 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300979int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
980void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#endif /* HAVE_PCI_MMAP */
982
983/**
984 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -0700985 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700987 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * @buf: user input
989 * @off: file offset
990 * @count: number of byte in input
991 *
992 * writing anything except 0 enables it
993 */
994static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700995pci_write_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
1001 if ((off == 0) && (*buf == '0') && (count == 2))
1002 pdev->rom_attr_enabled = 0;
1003 else
1004 pdev->rom_attr_enabled = 1;
1005
1006 return count;
1007}
1008
1009/**
1010 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001011 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001013 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * @buf: where to put the data we read from the ROM
1015 * @off: file offset
1016 * @count: number of bytes to read
1017 *
1018 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1019 * device corresponding to @kobj.
1020 */
1021static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001022pci_read_rom(struct file *filp, struct kobject *kobj,
1023 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001024 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1027 void __iomem *rom;
1028 size_t size;
1029
1030 if (!pdev->rom_attr_enabled)
1031 return -EINVAL;
1032
1033 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001034 if (!rom || !size)
1035 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (off >= size)
1038 count = 0;
1039 else {
1040 if (off + count > size)
1041 count = size - off;
1042
1043 memcpy_fromio(buf, rom + off, count);
1044 }
1045 pci_unmap_rom(pdev, rom);
1046
1047 return count;
1048}
1049
1050static struct bin_attribute pci_config_attr = {
1051 .attr = {
1052 .name = "config",
1053 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001055 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .read = pci_read_config,
1057 .write = pci_write_config,
1058};
1059
1060static struct bin_attribute pcie_config_attr = {
1061 .attr = {
1062 .name = "config",
1063 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001065 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 .read = pci_read_config,
1067 .write = pci_write_config,
1068};
1069
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001070int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001071{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001072 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001073}
1074
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001075static ssize_t reset_store(struct device *dev,
1076 struct device_attribute *attr, const char *buf,
1077 size_t count)
1078{
1079 struct pci_dev *pdev = to_pci_dev(dev);
1080 unsigned long val;
1081 ssize_t result = strict_strtoul(buf, 0, &val);
1082
1083 if (result < 0)
1084 return result;
1085
1086 if (val != 1)
1087 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001088
1089 result = pci_reset_function(pdev);
1090 if (result < 0)
1091 return result;
1092
1093 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001094}
1095
1096static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1097
Zhao, Yu280c73d2008-10-13 20:01:00 +08001098static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1099{
1100 int retval;
1101 struct bin_attribute *attr;
1102
1103 /* If the device has VPD, try to expose it in sysfs. */
1104 if (dev->vpd) {
1105 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1106 if (!attr)
1107 return -ENOMEM;
1108
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001109 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001110 attr->size = dev->vpd->len;
1111 attr->attr.name = "vpd";
1112 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001113 attr->read = read_vpd_attr;
1114 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001115 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1116 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001117 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001118 return retval;
1119 }
1120 dev->vpd->attr = attr;
1121 }
1122
1123 /* Active State Power Management */
1124 pcie_aspm_create_sysfs_dev_files(dev);
1125
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001126 if (!pci_probe_reset_function(dev)) {
1127 retval = device_create_file(&dev->dev, &reset_attr);
1128 if (retval)
1129 goto error;
1130 dev->reset_fn = 1;
1131 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001132 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001133
1134error:
1135 pcie_aspm_remove_sysfs_dev_files(dev);
1136 if (dev->vpd && dev->vpd->attr) {
1137 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1138 kfree(dev->vpd->attr);
1139 }
1140
1141 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001142}
1143
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001144int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001146 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001147 int rom_size = 0;
1148 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (!sysfs_initialized)
1151 return -EACCES;
1152
Zhao, Yu557848c2008-10-13 19:18:07 +08001153 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001154 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001156 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1157 if (retval)
1158 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001160 retval = pci_create_resource_files(pdev);
1161 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001162 goto err_config_file;
1163
1164 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1165 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1166 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1167 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001170 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001171 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001172 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001173 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001174 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001176 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001177 attr->size = rom_size;
1178 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001179 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001180 attr->read = pci_read_rom;
1181 attr->write = pci_write_rom;
1182 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1183 if (retval) {
1184 kfree(attr);
1185 goto err_resource_files;
1186 }
1187 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001189
Dave Airlie217f45d2009-03-04 05:57:05 +00001190 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1191 retval = device_create_file(&pdev->dev, &vga_attr);
1192 if (retval)
1193 goto err_rom_file;
1194 }
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001197 retval = pcibios_add_platform_entries(pdev);
1198 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001199 goto err_vga_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001200
Zhao, Yu280c73d2008-10-13 20:01:00 +08001201 /* add sysfs entries for various capabilities */
1202 retval = pci_create_capabilities_sysfs(pdev);
1203 if (retval)
Dave Airlie217f45d2009-03-04 05:57:05 +00001204 goto err_vga_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001205
Narendra K911e1c92010-07-26 05:56:50 -05001206 pci_create_firmware_label_files(pdev);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001209
Dave Airlie217f45d2009-03-04 05:57:05 +00001210err_vga_file:
1211 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1212 device_remove_file(&pdev->dev, &vga_attr);
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001213err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001214 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001215 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001216 kfree(pdev->rom_attr);
1217 pdev->rom_attr = NULL;
1218 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001219err_resource_files:
1220 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001221err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001222 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001223 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1224 else
1225 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1226err:
1227 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Zhao, Yu280c73d2008-10-13 20:01:00 +08001230static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1231{
1232 if (dev->vpd && dev->vpd->attr) {
1233 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1234 kfree(dev->vpd->attr);
1235 }
1236
1237 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001238 if (dev->reset_fn) {
1239 device_remove_file(&dev->dev, &reset_attr);
1240 dev->reset_fn = 0;
1241 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244/**
1245 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1246 * @pdev: device whose entries we should free
1247 *
1248 * Cleanup when @pdev is removed from sysfs.
1249 */
1250void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1251{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001252 int rom_size = 0;
1253
David Millerd67afe52006-11-10 12:27:48 -08001254 if (!sysfs_initialized)
1255 return;
1256
Zhao, Yu280c73d2008-10-13 20:01:00 +08001257 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001258
Zhao, Yu557848c2008-10-13 19:18:07 +08001259 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1261 else
1262 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1263
1264 pci_remove_resource_files(pdev);
1265
Zhao, Yu280c73d2008-10-13 20:01:00 +08001266 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1267 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1268 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1269 rom_size = 0x20000;
1270
1271 if (rom_size && pdev->rom_attr) {
1272 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1273 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Narendra K911e1c92010-07-26 05:56:50 -05001275
1276 pci_remove_firmware_label_files(pdev);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static int __init pci_sysfs_init(void)
1281{
1282 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001283 int retval;
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001286 for_each_pci_dev(pdev) {
1287 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001288 if (retval) {
1289 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001290 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001291 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 return 0;
1295}
1296
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001297late_initcall(pci_sysfs_init);