Merge branch 'tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.git] / drivers / pci / pci-sysfs.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4 18#include <linux/kernel.h>
b5ff7df3 19#include <linux/sched.h>
1da177e4
LT
20#include <linux/pci.h>
21#include <linux/stat.h>
22#include <linux/topology.h>
23#include <linux/mm.h>
aa0ac365 24#include <linux/capability.h>
7d715a6c 25#include <linux/pci-aspm.h>
1da177e4
LT
26#include "pci.h"
27
28static int sysfs_initialized; /* = 0 */
29
30/* show configuration fields */
31#define pci_config_attr(field, format_string) \
32static ssize_t \
e404e274 33field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
34{ \
35 struct pci_dev *pdev; \
36 \
37 pdev = to_pci_dev (dev); \
38 return sprintf (buf, format_string, pdev->field); \
39}
40
41pci_config_attr(vendor, "0x%04x\n");
42pci_config_attr(device, "0x%04x\n");
43pci_config_attr(subsystem_vendor, "0x%04x\n");
44pci_config_attr(subsystem_device, "0x%04x\n");
45pci_config_attr(class, "0x%06x\n");
46pci_config_attr(irq, "%u\n");
47
bdee9d98
DT
48static ssize_t broken_parity_status_show(struct device *dev,
49 struct device_attribute *attr,
50 char *buf)
51{
52 struct pci_dev *pdev = to_pci_dev(dev);
53 return sprintf (buf, "%u\n", pdev->broken_parity_status);
54}
55
56static ssize_t broken_parity_status_store(struct device *dev,
57 struct device_attribute *attr,
58 const char *buf, size_t count)
59{
60 struct pci_dev *pdev = to_pci_dev(dev);
92425a40 61 unsigned long val;
bdee9d98 62
92425a40
TP
63 if (strict_strtoul(buf, 0, &val) < 0)
64 return -EINVAL;
65
66 pdev->broken_parity_status = !!val;
67
68 return count;
bdee9d98
DT
69}
70
4327edf6
AC
71static ssize_t local_cpus_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
1da177e4 73{
3be83050 74 const struct cpumask *mask;
4327edf6
AC
75 int len;
76
e0cd5160 77#ifdef CONFIG_NUMA
6be954d1
DJ
78 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
79 cpumask_of_node(dev_to_node(dev));
e0cd5160 80#else
3be83050 81 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
e0cd5160 82#endif
3be83050 83 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
39106dcf
MT
84 buf[len++] = '\n';
85 buf[len] = '\0';
86 return len;
87}
88
89
90static ssize_t local_cpulist_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
92{
3be83050 93 const struct cpumask *mask;
39106dcf
MT
94 int len;
95
e0cd5160 96#ifdef CONFIG_NUMA
6be954d1
DJ
97 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
98 cpumask_of_node(dev_to_node(dev));
e0cd5160 99#else
3be83050 100 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
e0cd5160 101#endif
3be83050 102 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
39106dcf
MT
103 buf[len++] = '\n';
104 buf[len] = '\0';
105 return len;
1da177e4
LT
106}
107
108/* show resources */
109static ssize_t
e404e274 110resource_show(struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
111{
112 struct pci_dev * pci_dev = to_pci_dev(dev);
113 char * str = buf;
114 int i;
fde09c6d 115 int max;
e31dd6e4 116 resource_size_t start, end;
1da177e4
LT
117
118 if (pci_dev->subordinate)
119 max = DEVICE_COUNT_RESOURCE;
fde09c6d
YZ
120 else
121 max = PCI_BRIDGE_RESOURCES;
1da177e4
LT
122
123 for (i = 0; i < max; i++) {
2311b1f2
ME
124 struct resource *res = &pci_dev->resource[i];
125 pci_resource_to_user(pci_dev, i, res, &start, &end);
126 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
127 (unsigned long long)start,
128 (unsigned long long)end,
129 (unsigned long long)res->flags);
1da177e4
LT
130 }
131 return (str - buf);
132}
133
87c8a443 134static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
9888549e
GK
135{
136 struct pci_dev *pci_dev = to_pci_dev(dev);
137
138 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
139 pci_dev->vendor, pci_dev->device,
140 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
141 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
142 (u8)(pci_dev->class));
143}
bae94d02
IPG
144
145static ssize_t is_enabled_store(struct device *dev,
146 struct device_attribute *attr, const char *buf,
147 size_t count)
9f125d30
AV
148{
149 struct pci_dev *pdev = to_pci_dev(dev);
92425a40
TP
150 unsigned long val;
151 ssize_t result = strict_strtoul(buf, 0, &val);
152
153 if (result < 0)
154 return result;
9f125d30
AV
155
156 /* this can crash the machine when done on the "wrong" device */
157 if (!capable(CAP_SYS_ADMIN))
92425a40 158 return -EPERM;
9f125d30 159
92425a40 160 if (!val) {
296ccb08 161 if (pci_is_enabled(pdev))
bae94d02
IPG
162 pci_disable_device(pdev);
163 else
164 result = -EIO;
92425a40 165 } else
bae94d02 166 result = pci_enable_device(pdev);
9f125d30 167
bae94d02
IPG
168 return result < 0 ? result : count;
169}
170
171static ssize_t is_enabled_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
174 struct pci_dev *pdev;
9f125d30 175
bae94d02
IPG
176 pdev = to_pci_dev (dev);
177 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
9f125d30
AV
178}
179
81bb0e19
BG
180#ifdef CONFIG_NUMA
181static ssize_t
182numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
183{
184 return sprintf (buf, "%d\n", dev->numa_node);
185}
186#endif
187
bb965401
YL
188static ssize_t
189dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
190{
191 struct pci_dev *pdev = to_pci_dev(dev);
192
193 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
194}
195
196static ssize_t
197consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
198 char *buf)
199{
200 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
201}
202
fe97064c
BG
203static ssize_t
204msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
205{
206 struct pci_dev *pdev = to_pci_dev(dev);
207
208 if (!pdev->subordinate)
209 return 0;
210
211 return sprintf (buf, "%u\n",
212 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
213}
214
215static ssize_t
216msi_bus_store(struct device *dev, struct device_attribute *attr,
217 const char *buf, size_t count)
218{
219 struct pci_dev *pdev = to_pci_dev(dev);
92425a40
TP
220 unsigned long val;
221
222 if (strict_strtoul(buf, 0, &val) < 0)
223 return -EINVAL;
fe97064c
BG
224
225 /* bad things may happen if the no_msi flag is changed
226 * while some drivers are loaded */
227 if (!capable(CAP_SYS_ADMIN))
92425a40 228 return -EPERM;
fe97064c 229
92425a40
TP
230 /* Maybe pci devices without subordinate busses shouldn't even have this
231 * attribute in the first place? */
fe97064c
BG
232 if (!pdev->subordinate)
233 return count;
234
92425a40
TP
235 /* Is the flag going to change, or keep the value it already had? */
236 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
237 !!val) {
238 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
fe97064c 239
92425a40
TP
240 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
241 " bad things could happen\n", val ? "" : " not");
fe97064c
BG
242 }
243
244 return count;
245}
9888549e 246
705b1aaa
AC
247#ifdef CONFIG_HOTPLUG
248static DEFINE_MUTEX(pci_remove_rescan_mutex);
249static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
250 size_t count)
251{
252 unsigned long val;
253 struct pci_bus *b = NULL;
254
255 if (strict_strtoul(buf, 0, &val) < 0)
256 return -EINVAL;
257
258 if (val) {
259 mutex_lock(&pci_remove_rescan_mutex);
260 while ((b = pci_find_next_bus(b)) != NULL)
261 pci_rescan_bus(b);
262 mutex_unlock(&pci_remove_rescan_mutex);
263 }
264 return count;
265}
266
267struct bus_attribute pci_bus_attrs[] = {
268 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
269 __ATTR_NULL
270};
77c27c7b 271
738a6396
AC
272static ssize_t
273dev_rescan_store(struct device *dev, struct device_attribute *attr,
274 const char *buf, size_t count)
275{
276 unsigned long val;
277 struct pci_dev *pdev = to_pci_dev(dev);
278
279 if (strict_strtoul(buf, 0, &val) < 0)
280 return -EINVAL;
281
282 if (val) {
283 mutex_lock(&pci_remove_rescan_mutex);
284 pci_rescan_bus(pdev->bus);
285 mutex_unlock(&pci_remove_rescan_mutex);
286 }
287 return count;
288}
289
77c27c7b
AC
290static void remove_callback(struct device *dev)
291{
292 struct pci_dev *pdev = to_pci_dev(dev);
293
294 mutex_lock(&pci_remove_rescan_mutex);
295 pci_remove_bus_device(pdev);
296 mutex_unlock(&pci_remove_rescan_mutex);
297}
298
299static ssize_t
300remove_store(struct device *dev, struct device_attribute *dummy,
301 const char *buf, size_t count)
302{
303 int ret = 0;
304 unsigned long val;
77c27c7b
AC
305
306 if (strict_strtoul(buf, 0, &val) < 0)
307 return -EINVAL;
308
77c27c7b
AC
309 /* An attribute cannot be unregistered by one of its own methods,
310 * so we have to use this roundabout approach.
311 */
312 if (val)
313 ret = device_schedule_callback(dev, remove_callback);
314 if (ret)
315 count = ret;
316 return count;
317}
705b1aaa
AC
318#endif
319
1da177e4
LT
320struct device_attribute pci_dev_attrs[] = {
321 __ATTR_RO(resource),
322 __ATTR_RO(vendor),
323 __ATTR_RO(device),
324 __ATTR_RO(subsystem_vendor),
325 __ATTR_RO(subsystem_device),
326 __ATTR_RO(class),
327 __ATTR_RO(irq),
328 __ATTR_RO(local_cpus),
39106dcf 329 __ATTR_RO(local_cpulist),
9888549e 330 __ATTR_RO(modalias),
81bb0e19
BG
331#ifdef CONFIG_NUMA
332 __ATTR_RO(numa_node),
333#endif
bb965401
YL
334 __ATTR_RO(dma_mask_bits),
335 __ATTR_RO(consistent_dma_mask_bits),
9f125d30 336 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
bdee9d98
DT
337 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
338 broken_parity_status_show,broken_parity_status_store),
fe97064c 339 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
77c27c7b
AC
340#ifdef CONFIG_HOTPLUG
341 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
738a6396 342 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
77c27c7b 343#endif
1da177e4
LT
344 __ATTR_NULL,
345};
346
217f45de
DA
347static ssize_t
348boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
349{
350 struct pci_dev *pdev = to_pci_dev(dev);
351
352 return sprintf(buf, "%u\n",
353 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
354 IORESOURCE_ROM_SHADOW));
355}
356struct device_attribute vga_attr = __ATTR_RO(boot_vga);
357
1da177e4 358static ssize_t
91a69029
ZR
359pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
360 char *buf, loff_t off, size_t count)
1da177e4
LT
361{
362 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
363 unsigned int size = 64;
364 loff_t init_off = off;
4c0619ad 365 u8 *data = (u8*) buf;
1da177e4
LT
366
367 /* Several chips lock up trying to read undefined config space */
368 if (capable(CAP_SYS_ADMIN)) {
369 size = dev->cfg_size;
370 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
371 size = 128;
372 }
373
374 if (off > size)
375 return 0;
376 if (off + count > size) {
377 size -= off;
378 count = size;
379 } else {
380 size = count;
381 }
382
4c0619ad 383 if ((off & 1) && size) {
384 u8 val;
e04b0ea2 385 pci_user_read_config_byte(dev, off, &val);
4c0619ad 386 data[off - init_off] = val;
1da177e4 387 off++;
4c0619ad 388 size--;
389 }
390
391 if ((off & 3) && size > 2) {
392 u16 val;
e04b0ea2 393 pci_user_read_config_word(dev, off, &val);
4c0619ad 394 data[off - init_off] = val & 0xff;
395 data[off - init_off + 1] = (val >> 8) & 0xff;
396 off += 2;
397 size -= 2;
1da177e4
LT
398 }
399
400 while (size > 3) {
4c0619ad 401 u32 val;
e04b0ea2 402 pci_user_read_config_dword(dev, off, &val);
4c0619ad 403 data[off - init_off] = val & 0xff;
404 data[off - init_off + 1] = (val >> 8) & 0xff;
405 data[off - init_off + 2] = (val >> 16) & 0xff;
406 data[off - init_off + 3] = (val >> 24) & 0xff;
1da177e4
LT
407 off += 4;
408 size -= 4;
409 }
410
4c0619ad 411 if (size >= 2) {
412 u16 val;
e04b0ea2 413 pci_user_read_config_word(dev, off, &val);
4c0619ad 414 data[off - init_off] = val & 0xff;
415 data[off - init_off + 1] = (val >> 8) & 0xff;
416 off += 2;
417 size -= 2;
418 }
419
420 if (size > 0) {
421 u8 val;
e04b0ea2 422 pci_user_read_config_byte(dev, off, &val);
4c0619ad 423 data[off - init_off] = val;
1da177e4
LT
424 off++;
425 --size;
426 }
427
428 return count;
429}
430
431static ssize_t
91a69029
ZR
432pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
433 char *buf, loff_t off, size_t count)
1da177e4
LT
434{
435 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
436 unsigned int size = count;
437 loff_t init_off = off;
4c0619ad 438 u8 *data = (u8*) buf;
1da177e4
LT
439
440 if (off > dev->cfg_size)
441 return 0;
442 if (off + count > dev->cfg_size) {
443 size = dev->cfg_size - off;
444 count = size;
445 }
4c0619ad 446
447 if ((off & 1) && size) {
e04b0ea2 448 pci_user_write_config_byte(dev, off, data[off - init_off]);
1da177e4 449 off++;
4c0619ad 450 size--;
1da177e4 451 }
4c0619ad 452
453 if ((off & 3) && size > 2) {
454 u16 val = data[off - init_off];
455 val |= (u16) data[off - init_off + 1] << 8;
e04b0ea2 456 pci_user_write_config_word(dev, off, val);
4c0619ad 457 off += 2;
458 size -= 2;
459 }
1da177e4
LT
460
461 while (size > 3) {
4c0619ad 462 u32 val = data[off - init_off];
463 val |= (u32) data[off - init_off + 1] << 8;
464 val |= (u32) data[off - init_off + 2] << 16;
465 val |= (u32) data[off - init_off + 3] << 24;
e04b0ea2 466 pci_user_write_config_dword(dev, off, val);
1da177e4
LT
467 off += 4;
468 size -= 4;
469 }
4c0619ad 470
471 if (size >= 2) {
472 u16 val = data[off - init_off];
473 val |= (u16) data[off - init_off + 1] << 8;
e04b0ea2 474 pci_user_write_config_word(dev, off, val);
4c0619ad 475 off += 2;
476 size -= 2;
477 }
1da177e4 478
4c0619ad 479 if (size) {
e04b0ea2 480 pci_user_write_config_byte(dev, off, data[off - init_off]);
1da177e4
LT
481 off++;
482 --size;
483 }
484
485 return count;
486}
487
94e61088 488static ssize_t
287d19ce
SH
489read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
490 char *buf, loff_t off, size_t count)
94e61088
BH
491{
492 struct pci_dev *dev =
493 to_pci_dev(container_of(kobj, struct device, kobj));
94e61088
BH
494
495 if (off > bin_attr->size)
496 count = 0;
497 else if (count > bin_attr->size - off)
498 count = bin_attr->size - off;
94e61088 499
287d19ce 500 return pci_read_vpd(dev, off, count, buf);
94e61088
BH
501}
502
503static ssize_t
287d19ce
SH
504write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr,
505 char *buf, loff_t off, size_t count)
94e61088
BH
506{
507 struct pci_dev *dev =
508 to_pci_dev(container_of(kobj, struct device, kobj));
94e61088
BH
509
510 if (off > bin_attr->size)
511 count = 0;
512 else if (count > bin_attr->size - off)
513 count = bin_attr->size - off;
94e61088 514
287d19ce 515 return pci_write_vpd(dev, off, count, buf);
94e61088
BH
516}
517
1da177e4
LT
518#ifdef HAVE_PCI_LEGACY
519/**
520 * pci_read_legacy_io - read byte(s) from legacy I/O port space
521 * @kobj: kobject corresponding to file to read from
cffb2faf 522 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
523 * @buf: buffer to store results
524 * @off: offset into legacy I/O port space
525 * @count: number of bytes to read
526 *
527 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
528 * callback routine (pci_legacy_read).
529 */
f19aeb1f 530static ssize_t
91a69029
ZR
531pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
532 char *buf, loff_t off, size_t count)
1da177e4
LT
533{
534 struct pci_bus *bus = to_pci_bus(container_of(kobj,
fd7d1ced 535 struct device,
1da177e4
LT
536 kobj));
537
538 /* Only support 1, 2 or 4 byte accesses */
539 if (count != 1 && count != 2 && count != 4)
540 return -EINVAL;
541
542 return pci_legacy_read(bus, off, (u32 *)buf, count);
543}
544
545/**
546 * pci_write_legacy_io - write byte(s) to legacy I/O port space
547 * @kobj: kobject corresponding to file to read from
cffb2faf 548 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
549 * @buf: buffer containing value to be written
550 * @off: offset into legacy I/O port space
551 * @count: number of bytes to write
552 *
553 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
554 * callback routine (pci_legacy_write).
555 */
f19aeb1f 556static ssize_t
91a69029
ZR
557pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
558 char *buf, loff_t off, size_t count)
1da177e4
LT
559{
560 struct pci_bus *bus = to_pci_bus(container_of(kobj,
fd7d1ced 561 struct device,
1da177e4
LT
562 kobj));
563 /* Only support 1, 2 or 4 byte accesses */
564 if (count != 1 && count != 2 && count != 4)
565 return -EINVAL;
566
567 return pci_legacy_write(bus, off, *(u32 *)buf, count);
568}
569
570/**
571 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
572 * @kobj: kobject corresponding to device to be mapped
573 * @attr: struct bin_attribute for this file
574 * @vma: struct vm_area_struct passed to mmap
575 *
f19aeb1f 576 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
1da177e4
LT
577 * legacy memory space (first meg of bus space) into application virtual
578 * memory space.
579 */
f19aeb1f 580static int
1da177e4
LT
581pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
582 struct vm_area_struct *vma)
583{
584 struct pci_bus *bus = to_pci_bus(container_of(kobj,
fd7d1ced 585 struct device,
1da177e4
LT
586 kobj));
587
f19aeb1f
BH
588 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
589}
590
591/**
592 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
593 * @kobj: kobject corresponding to device to be mapped
594 * @attr: struct bin_attribute for this file
595 * @vma: struct vm_area_struct passed to mmap
596 *
597 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
598 * legacy IO space (first meg of bus space) into application virtual
599 * memory space. Returns -ENOSYS if the operation isn't supported
600 */
601static int
602pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr,
603 struct vm_area_struct *vma)
604{
605 struct pci_bus *bus = to_pci_bus(container_of(kobj,
606 struct device,
607 kobj));
608
609 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
610}
611
10a0ef39
IK
612/**
613 * pci_adjust_legacy_attr - adjustment of legacy file attributes
614 * @b: bus to create files under
615 * @mmap_type: I/O port or memory
616 *
617 * Stub implementation. Can be overridden by arch if necessary.
618 */
619void __weak
620pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
621{
622 return;
623}
624
f19aeb1f
BH
625/**
626 * pci_create_legacy_files - create legacy I/O port and memory files
627 * @b: bus to create files under
628 *
629 * Some platforms allow access to legacy I/O port and ISA memory space on
630 * a per-bus basis. This routine creates the files and ties them into
631 * their associated read, write and mmap files from pci-sysfs.c
632 *
633 * On error unwind, but don't propogate the error to the caller
634 * as it is ok to set up the PCI bus without these files.
635 */
636void pci_create_legacy_files(struct pci_bus *b)
637{
638 int error;
639
640 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
641 GFP_ATOMIC);
642 if (!b->legacy_io)
643 goto kzalloc_err;
644
645 b->legacy_io->attr.name = "legacy_io";
646 b->legacy_io->size = 0xffff;
647 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
648 b->legacy_io->read = pci_read_legacy_io;
649 b->legacy_io->write = pci_write_legacy_io;
650 b->legacy_io->mmap = pci_mmap_legacy_io;
10a0ef39 651 pci_adjust_legacy_attr(b, pci_mmap_io);
f19aeb1f
BH
652 error = device_create_bin_file(&b->dev, b->legacy_io);
653 if (error)
654 goto legacy_io_err;
655
656 /* Allocated above after the legacy_io struct */
657 b->legacy_mem = b->legacy_io + 1;
658 b->legacy_mem->attr.name = "legacy_mem";
659 b->legacy_mem->size = 1024*1024;
660 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
661 b->legacy_mem->mmap = pci_mmap_legacy_mem;
10a0ef39 662 pci_adjust_legacy_attr(b, pci_mmap_mem);
f19aeb1f
BH
663 error = device_create_bin_file(&b->dev, b->legacy_mem);
664 if (error)
665 goto legacy_mem_err;
666
667 return;
668
669legacy_mem_err:
670 device_remove_bin_file(&b->dev, b->legacy_io);
671legacy_io_err:
672 kfree(b->legacy_io);
673 b->legacy_io = NULL;
674kzalloc_err:
675 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
676 "and ISA memory resources to sysfs\n");
677 return;
678}
679
680void pci_remove_legacy_files(struct pci_bus *b)
681{
682 if (b->legacy_io) {
683 device_remove_bin_file(&b->dev, b->legacy_io);
684 device_remove_bin_file(&b->dev, b->legacy_mem);
685 kfree(b->legacy_io); /* both are allocated here */
686 }
1da177e4
LT
687}
688#endif /* HAVE_PCI_LEGACY */
689
690#ifdef HAVE_PCI_MMAP
b5ff7df3 691
9eff02e2 692int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma)
b5ff7df3
LT
693{
694 unsigned long nr, start, size;
695
696 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
697 start = vma->vm_pgoff;
88e7df0b 698 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
b5ff7df3
LT
699 if (start < size && size - start >= nr)
700 return 1;
701 WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n",
702 current->comm, start, start+nr, pci_name(pdev), resno, size);
703 return 0;
704}
705
1da177e4
LT
706/**
707 * pci_mmap_resource - map a PCI resource into user memory space
708 * @kobj: kobject for mapping
709 * @attr: struct bin_attribute for the file being mapped
710 * @vma: struct vm_area_struct passed into the mmap
45aec1ae 711 * @write_combine: 1 for write_combine mapping
1da177e4
LT
712 *
713 * Use the regular PCI mapping routines to map a PCI resource into userspace.
1da177e4
LT
714 */
715static int
716pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
45aec1ae 717 struct vm_area_struct *vma, int write_combine)
1da177e4
LT
718{
719 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
720 struct device, kobj));
721 struct resource *res = (struct resource *)attr->private;
722 enum pci_mmap_state mmap_type;
e31dd6e4 723 resource_size_t start, end;
2311b1f2 724 int i;
1da177e4 725
2311b1f2
ME
726 for (i = 0; i < PCI_ROM_RESOURCE; i++)
727 if (res == &pdev->resource[i])
728 break;
729 if (i >= PCI_ROM_RESOURCE)
730 return -ENODEV;
731
b5ff7df3
LT
732 if (!pci_mmap_fits(pdev, i, vma))
733 return -EINVAL;
734
2311b1f2
ME
735 /* pci_mmap_page_range() expects the same kind of entry as coming
736 * from /proc/bus/pci/ which is a "user visible" value. If this is
737 * different from the resource itself, arch will do necessary fixup.
738 */
739 pci_resource_to_user(pdev, i, res, &start, &end);
740 vma->vm_pgoff += start >> PAGE_SHIFT;
1da177e4
LT
741 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
742
e8de1481
AV
743 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
744 return -EINVAL;
745
45aec1ae 746 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
747}
748
749static int
750pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr,
751 struct vm_area_struct *vma)
752{
753 return pci_mmap_resource(kobj, attr, vma, 0);
754}
755
756static int
757pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr,
758 struct vm_area_struct *vma)
759{
760 return pci_mmap_resource(kobj, attr, vma, 1);
1da177e4
LT
761}
762
b19441af
GKH
763/**
764 * pci_remove_resource_files - cleanup resource files
cffb2faf 765 * @pdev: dev to cleanup
b19441af 766 *
cffb2faf 767 * If we created resource files for @pdev, remove them from sysfs and
b19441af
GKH
768 * free their resources.
769 */
770static void
771pci_remove_resource_files(struct pci_dev *pdev)
772{
773 int i;
774
775 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
776 struct bin_attribute *res_attr;
777
778 res_attr = pdev->res_attr[i];
779 if (res_attr) {
780 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
781 kfree(res_attr);
782 }
45aec1ae 783
784 res_attr = pdev->res_attr_wc[i];
785 if (res_attr) {
786 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
787 kfree(res_attr);
788 }
b19441af
GKH
789 }
790}
791
45aec1ae 792static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
793{
794 /* allocate attribute structure, piggyback attribute name */
795 int name_len = write_combine ? 13 : 10;
796 struct bin_attribute *res_attr;
797 int retval;
798
799 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
800 if (res_attr) {
801 char *res_attr_name = (char *)(res_attr + 1);
802
803 if (write_combine) {
804 pdev->res_attr_wc[num] = res_attr;
805 sprintf(res_attr_name, "resource%d_wc", num);
806 res_attr->mmap = pci_mmap_resource_wc;
807 } else {
808 pdev->res_attr[num] = res_attr;
809 sprintf(res_attr_name, "resource%d", num);
810 res_attr->mmap = pci_mmap_resource_uc;
811 }
812 res_attr->attr.name = res_attr_name;
813 res_attr->attr.mode = S_IRUSR | S_IWUSR;
814 res_attr->size = pci_resource_len(pdev, num);
815 res_attr->private = &pdev->resource[num];
816 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
817 } else
818 retval = -ENOMEM;
819
820 return retval;
821}
822
1da177e4
LT
823/**
824 * pci_create_resource_files - create resource files in sysfs for @dev
cffb2faf 825 * @pdev: dev in question
1da177e4 826 *
cffb2faf 827 * Walk the resources in @pdev creating files for each resource available.
1da177e4 828 */
b19441af 829static int pci_create_resource_files(struct pci_dev *pdev)
1da177e4
LT
830{
831 int i;
b19441af 832 int retval;
1da177e4
LT
833
834 /* Expose the PCI resources from this device as files */
835 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1da177e4
LT
836
837 /* skip empty resources */
838 if (!pci_resource_len(pdev, i))
839 continue;
840
45aec1ae 841 retval = pci_create_attr(pdev, i, 0);
842 /* for prefetchable resources, create a WC mappable file */
843 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
844 retval = pci_create_attr(pdev, i, 1);
845
846 if (retval) {
847 pci_remove_resource_files(pdev);
848 return retval;
1da177e4
LT
849 }
850 }
b19441af 851 return 0;
1da177e4
LT
852}
853#else /* !HAVE_PCI_MMAP */
10a0ef39
IK
854int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
855void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1da177e4
LT
856#endif /* HAVE_PCI_MMAP */
857
858/**
859 * pci_write_rom - used to enable access to the PCI ROM display
860 * @kobj: kernel object handle
cffb2faf 861 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
862 * @buf: user input
863 * @off: file offset
864 * @count: number of byte in input
865 *
866 * writing anything except 0 enables it
867 */
868static ssize_t
91a69029
ZR
869pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
870 char *buf, loff_t off, size_t count)
1da177e4
LT
871{
872 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
873
874 if ((off == 0) && (*buf == '0') && (count == 2))
875 pdev->rom_attr_enabled = 0;
876 else
877 pdev->rom_attr_enabled = 1;
878
879 return count;
880}
881
882/**
883 * pci_read_rom - read a PCI ROM
884 * @kobj: kernel object handle
cffb2faf 885 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
886 * @buf: where to put the data we read from the ROM
887 * @off: file offset
888 * @count: number of bytes to read
889 *
890 * Put @count bytes starting at @off into @buf from the ROM in the PCI
891 * device corresponding to @kobj.
892 */
893static ssize_t
91a69029
ZR
894pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
895 char *buf, loff_t off, size_t count)
1da177e4
LT
896{
897 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
898 void __iomem *rom;
899 size_t size;
900
901 if (!pdev->rom_attr_enabled)
902 return -EINVAL;
903
904 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
97c44836
TN
905 if (!rom || !size)
906 return -EIO;
1da177e4
LT
907
908 if (off >= size)
909 count = 0;
910 else {
911 if (off + count > size)
912 count = size - off;
913
914 memcpy_fromio(buf, rom + off, count);
915 }
916 pci_unmap_rom(pdev, rom);
917
918 return count;
919}
920
921static struct bin_attribute pci_config_attr = {
922 .attr = {
923 .name = "config",
924 .mode = S_IRUGO | S_IWUSR,
1da177e4 925 },
557848c3 926 .size = PCI_CFG_SPACE_SIZE,
1da177e4
LT
927 .read = pci_read_config,
928 .write = pci_write_config,
929};
930
931static struct bin_attribute pcie_config_attr = {
932 .attr = {
933 .name = "config",
934 .mode = S_IRUGO | S_IWUSR,
1da177e4 935 },
557848c3 936 .size = PCI_CFG_SPACE_EXP_SIZE,
1da177e4
LT
937 .read = pci_read_config,
938 .write = pci_write_config,
939};
940
a2cd52ca 941int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
575e3348 942{
a2cd52ca 943 return 0;
575e3348
ME
944}
945
711d5779
MT
946static ssize_t reset_store(struct device *dev,
947 struct device_attribute *attr, const char *buf,
948 size_t count)
949{
950 struct pci_dev *pdev = to_pci_dev(dev);
951 unsigned long val;
952 ssize_t result = strict_strtoul(buf, 0, &val);
953
954 if (result < 0)
955 return result;
956
957 if (val != 1)
958 return -EINVAL;
959 return pci_reset_function(pdev);
960}
961
962static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
963
280c73d3
ZY
964static int pci_create_capabilities_sysfs(struct pci_dev *dev)
965{
966 int retval;
967 struct bin_attribute *attr;
968
969 /* If the device has VPD, try to expose it in sysfs. */
970 if (dev->vpd) {
971 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
972 if (!attr)
973 return -ENOMEM;
974
975 attr->size = dev->vpd->len;
976 attr->attr.name = "vpd";
977 attr->attr.mode = S_IRUSR | S_IWUSR;
287d19ce
SH
978 attr->read = read_vpd_attr;
979 attr->write = write_vpd_attr;
280c73d3
ZY
980 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
981 if (retval) {
982 kfree(dev->vpd->attr);
983 return retval;
984 }
985 dev->vpd->attr = attr;
986 }
987
988 /* Active State Power Management */
989 pcie_aspm_create_sysfs_dev_files(dev);
990
711d5779
MT
991 if (!pci_probe_reset_function(dev)) {
992 retval = device_create_file(&dev->dev, &reset_attr);
993 if (retval)
994 goto error;
995 dev->reset_fn = 1;
996 }
280c73d3 997 return 0;
711d5779
MT
998
999error:
1000 pcie_aspm_remove_sysfs_dev_files(dev);
1001 if (dev->vpd && dev->vpd->attr) {
1002 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1003 kfree(dev->vpd->attr);
1004 }
1005
1006 return retval;
280c73d3
ZY
1007}
1008
b19441af 1009int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1da177e4 1010{
b19441af 1011 int retval;
280c73d3
ZY
1012 int rom_size = 0;
1013 struct bin_attribute *attr;
b19441af 1014
1da177e4
LT
1015 if (!sysfs_initialized)
1016 return -EACCES;
1017
557848c3 1018 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
b19441af 1019 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1da177e4 1020 else
b19441af
GKH
1021 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1022 if (retval)
1023 goto err;
1da177e4 1024
b19441af
GKH
1025 retval = pci_create_resource_files(pdev);
1026 if (retval)
280c73d3
ZY
1027 goto err_config_file;
1028
1029 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1030 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1031 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1032 rom_size = 0x20000;
1da177e4
LT
1033
1034 /* If the device has a ROM, try to expose it in sysfs. */
280c73d3 1035 if (rom_size) {
94e61088 1036 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
280c73d3 1037 if (!attr) {
b19441af 1038 retval = -ENOMEM;
9890b12a 1039 goto err_resource_files;
1da177e4 1040 }
280c73d3
ZY
1041 attr->size = rom_size;
1042 attr->attr.name = "rom";
1043 attr->attr.mode = S_IRUSR;
1044 attr->read = pci_read_rom;
1045 attr->write = pci_write_rom;
1046 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1047 if (retval) {
1048 kfree(attr);
1049 goto err_resource_files;
1050 }
1051 pdev->rom_attr = attr;
1da177e4 1052 }
280c73d3 1053
217f45de
DA
1054 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1055 retval = device_create_file(&pdev->dev, &vga_attr);
1056 if (retval)
1057 goto err_rom_file;
1058 }
1059
1da177e4 1060 /* add platform-specific attributes */
280c73d3
ZY
1061 retval = pcibios_add_platform_entries(pdev);
1062 if (retval)
217f45de 1063 goto err_vga_file;
b19441af 1064
280c73d3
ZY
1065 /* add sysfs entries for various capabilities */
1066 retval = pci_create_capabilities_sysfs(pdev);
1067 if (retval)
217f45de 1068 goto err_vga_file;
7d715a6c 1069
1da177e4 1070 return 0;
b19441af 1071
217f45de
DA
1072err_vga_file:
1073 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1074 device_remove_file(&pdev->dev, &vga_attr);
a2cd52ca 1075err_rom_file:
280c73d3 1076 if (rom_size) {
94e61088 1077 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
280c73d3
ZY
1078 kfree(pdev->rom_attr);
1079 pdev->rom_attr = NULL;
1080 }
9890b12a
ME
1081err_resource_files:
1082 pci_remove_resource_files(pdev);
94e61088 1083err_config_file:
557848c3 1084 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
b19441af
GKH
1085 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1086 else
1087 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1088err:
1089 return retval;
1da177e4
LT
1090}
1091
280c73d3
ZY
1092static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1093{
1094 if (dev->vpd && dev->vpd->attr) {
1095 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1096 kfree(dev->vpd->attr);
1097 }
1098
1099 pcie_aspm_remove_sysfs_dev_files(dev);
711d5779
MT
1100 if (dev->reset_fn) {
1101 device_remove_file(&dev->dev, &reset_attr);
1102 dev->reset_fn = 0;
1103 }
280c73d3
ZY
1104}
1105
1da177e4
LT
1106/**
1107 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1108 * @pdev: device whose entries we should free
1109 *
1110 * Cleanup when @pdev is removed from sysfs.
1111 */
1112void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1113{
280c73d3
ZY
1114 int rom_size = 0;
1115
d67afe5e
DM
1116 if (!sysfs_initialized)
1117 return;
1118
280c73d3 1119 pci_remove_capabilities_sysfs(pdev);
7d715a6c 1120
557848c3 1121 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1da177e4
LT
1122 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1123 else
1124 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1125
1126 pci_remove_resource_files(pdev);
1127
280c73d3
ZY
1128 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1129 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1130 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1131 rom_size = 0x20000;
1132
1133 if (rom_size && pdev->rom_attr) {
1134 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1135 kfree(pdev->rom_attr);
1da177e4
LT
1136 }
1137}
1138
1139static int __init pci_sysfs_init(void)
1140{
1141 struct pci_dev *pdev = NULL;
b19441af
GKH
1142 int retval;
1143
1da177e4 1144 sysfs_initialized = 1;
b19441af
GKH
1145 for_each_pci_dev(pdev) {
1146 retval = pci_create_sysfs_dev_files(pdev);
151fc5df
JL
1147 if (retval) {
1148 pci_dev_put(pdev);
b19441af 1149 return retval;
151fc5df 1150 }
b19441af 1151 }
1da177e4
LT
1152
1153 return 0;
1154}
1155
40ee9e9f 1156late_initcall(pci_sysfs_init);