cpumask: use new cpus_scnprintf function
[linux-2.6/mini2440.git] / drivers / pci / pci-sysfs.c
blob8d9d648daeba74123a6f9cf9c74434f903292bac
1 /*
2 * drivers/pci/pci-sysfs.c
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>
11 * File attributes for PCI devices
13 * Modeled after usb's driverfs.c
18 #include <linux/kernel.h>
19 #include <linux/pci.h>
20 #include <linux/stat.h>
21 #include <linux/topology.h>
22 #include <linux/mm.h>
23 #include <linux/capability.h>
24 #include "pci.h"
26 static int sysfs_initialized; /* = 0 */
28 /* show configuration fields */
29 #define pci_config_attr(field, format_string) \
30 static ssize_t \
31 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
32 { \
33 struct pci_dev *pdev; \
35 pdev = to_pci_dev (dev); \
36 return sprintf (buf, format_string, pdev->field); \
39 pci_config_attr(vendor, "0x%04x\n");
40 pci_config_attr(device, "0x%04x\n");
41 pci_config_attr(subsystem_vendor, "0x%04x\n");
42 pci_config_attr(subsystem_device, "0x%04x\n");
43 pci_config_attr(class, "0x%06x\n");
44 pci_config_attr(irq, "%u\n");
46 static ssize_t broken_parity_status_show(struct device *dev,
47 struct device_attribute *attr,
48 char *buf)
50 struct pci_dev *pdev = to_pci_dev(dev);
51 return sprintf (buf, "%u\n", pdev->broken_parity_status);
54 static ssize_t broken_parity_status_store(struct device *dev,
55 struct device_attribute *attr,
56 const char *buf, size_t count)
58 struct pci_dev *pdev = to_pci_dev(dev);
59 ssize_t consumed = -EINVAL;
61 if ((count > 0) && (*buf == '0' || *buf == '1')) {
62 pdev->broken_parity_status = *buf == '1' ? 1 : 0;
63 consumed = count;
65 return consumed;
68 static ssize_t local_cpus_show(struct device *dev,
69 struct device_attribute *attr, char *buf)
71 cpumask_t mask;
72 int len;
74 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
75 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
76 buf[len++] = '\n';
77 buf[len] = '\0';
78 return len;
82 static ssize_t local_cpulist_show(struct device *dev,
83 struct device_attribute *attr, char *buf)
85 cpumask_t mask;
86 int len;
88 mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
89 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
90 buf[len++] = '\n';
91 buf[len] = '\0';
92 return len;
95 /* show resources */
96 static ssize_t
97 resource_show(struct device * dev, struct device_attribute *attr, char * buf)
99 struct pci_dev * pci_dev = to_pci_dev(dev);
100 char * str = buf;
101 int i;
102 int max = 7;
103 resource_size_t start, end;
105 if (pci_dev->subordinate)
106 max = DEVICE_COUNT_RESOURCE;
108 for (i = 0; i < max; i++) {
109 struct resource *res = &pci_dev->resource[i];
110 pci_resource_to_user(pci_dev, i, res, &start, &end);
111 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
112 (unsigned long long)start,
113 (unsigned long long)end,
114 (unsigned long long)res->flags);
116 return (str - buf);
119 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
121 struct pci_dev *pci_dev = to_pci_dev(dev);
123 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
124 pci_dev->vendor, pci_dev->device,
125 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
126 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
127 (u8)(pci_dev->class));
130 static ssize_t is_enabled_store(struct device *dev,
131 struct device_attribute *attr, const char *buf,
132 size_t count)
134 ssize_t result = -EINVAL;
135 struct pci_dev *pdev = to_pci_dev(dev);
137 /* this can crash the machine when done on the "wrong" device */
138 if (!capable(CAP_SYS_ADMIN))
139 return count;
141 if (*buf == '0') {
142 if (atomic_read(&pdev->enable_cnt) != 0)
143 pci_disable_device(pdev);
144 else
145 result = -EIO;
146 } else if (*buf == '1')
147 result = pci_enable_device(pdev);
149 return result < 0 ? result : count;
152 static ssize_t is_enabled_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct pci_dev *pdev;
157 pdev = to_pci_dev (dev);
158 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
161 #ifdef CONFIG_NUMA
162 static ssize_t
163 numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
165 return sprintf (buf, "%d\n", dev->numa_node);
167 #endif
169 static ssize_t
170 msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
172 struct pci_dev *pdev = to_pci_dev(dev);
174 if (!pdev->subordinate)
175 return 0;
177 return sprintf (buf, "%u\n",
178 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
181 static ssize_t
182 msi_bus_store(struct device *dev, struct device_attribute *attr,
183 const char *buf, size_t count)
185 struct pci_dev *pdev = to_pci_dev(dev);
187 /* bad things may happen if the no_msi flag is changed
188 * while some drivers are loaded */
189 if (!capable(CAP_SYS_ADMIN))
190 return count;
192 if (!pdev->subordinate)
193 return count;
195 if (*buf == '0') {
196 pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
197 dev_warn(&pdev->dev, "forced subordinate bus to not support MSI,"
198 " bad things could happen.\n");
201 if (*buf == '1') {
202 pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
203 dev_warn(&pdev->dev, "forced subordinate bus to support MSI,"
204 " bad things could happen.\n");
207 return count;
210 struct device_attribute pci_dev_attrs[] = {
211 __ATTR_RO(resource),
212 __ATTR_RO(vendor),
213 __ATTR_RO(device),
214 __ATTR_RO(subsystem_vendor),
215 __ATTR_RO(subsystem_device),
216 __ATTR_RO(class),
217 __ATTR_RO(irq),
218 __ATTR_RO(local_cpus),
219 __ATTR_RO(local_cpulist),
220 __ATTR_RO(modalias),
221 #ifdef CONFIG_NUMA
222 __ATTR_RO(numa_node),
223 #endif
224 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
225 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
226 broken_parity_status_show,broken_parity_status_store),
227 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
228 __ATTR_NULL,
231 static ssize_t
232 pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr,
233 char *buf, loff_t off, size_t count)
235 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
236 unsigned int size = 64;
237 loff_t init_off = off;
238 u8 *data = (u8*) buf;
240 /* Several chips lock up trying to read undefined config space */
241 if (capable(CAP_SYS_ADMIN)) {
242 size = dev->cfg_size;
243 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
244 size = 128;
247 if (off > size)
248 return 0;
249 if (off + count > size) {
250 size -= off;
251 count = size;
252 } else {
253 size = count;
256 if ((off & 1) && size) {
257 u8 val;
258 pci_user_read_config_byte(dev, off, &val);
259 data[off - init_off] = val;
260 off++;
261 size--;
264 if ((off & 3) && size > 2) {
265 u16 val;
266 pci_user_read_config_word(dev, off, &val);
267 data[off - init_off] = val & 0xff;
268 data[off - init_off + 1] = (val >> 8) & 0xff;
269 off += 2;
270 size -= 2;
273 while (size > 3) {
274 u32 val;
275 pci_user_read_config_dword(dev, off, &val);
276 data[off - init_off] = val & 0xff;
277 data[off - init_off + 1] = (val >> 8) & 0xff;
278 data[off - init_off + 2] = (val >> 16) & 0xff;
279 data[off - init_off + 3] = (val >> 24) & 0xff;
280 off += 4;
281 size -= 4;
284 if (size >= 2) {
285 u16 val;
286 pci_user_read_config_word(dev, off, &val);
287 data[off - init_off] = val & 0xff;
288 data[off - init_off + 1] = (val >> 8) & 0xff;
289 off += 2;
290 size -= 2;
293 if (size > 0) {
294 u8 val;
295 pci_user_read_config_byte(dev, off, &val);
296 data[off - init_off] = val;
297 off++;
298 --size;
301 return count;
304 static ssize_t
305 pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr,
306 char *buf, loff_t off, size_t count)
308 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
309 unsigned int size = count;
310 loff_t init_off = off;
311 u8 *data = (u8*) buf;
313 if (off > dev->cfg_size)
314 return 0;
315 if (off + count > dev->cfg_size) {
316 size = dev->cfg_size - off;
317 count = size;
320 if ((off & 1) && size) {
321 pci_user_write_config_byte(dev, off, data[off - init_off]);
322 off++;
323 size--;
326 if ((off & 3) && size > 2) {
327 u16 val = data[off - init_off];
328 val |= (u16) data[off - init_off + 1] << 8;
329 pci_user_write_config_word(dev, off, val);
330 off += 2;
331 size -= 2;
334 while (size > 3) {
335 u32 val = data[off - init_off];
336 val |= (u32) data[off - init_off + 1] << 8;
337 val |= (u32) data[off - init_off + 2] << 16;
338 val |= (u32) data[off - init_off + 3] << 24;
339 pci_user_write_config_dword(dev, off, val);
340 off += 4;
341 size -= 4;
344 if (size >= 2) {
345 u16 val = data[off - init_off];
346 val |= (u16) data[off - init_off + 1] << 8;
347 pci_user_write_config_word(dev, off, val);
348 off += 2;
349 size -= 2;
352 if (size) {
353 pci_user_write_config_byte(dev, off, data[off - init_off]);
354 off++;
355 --size;
358 return count;
361 #ifdef HAVE_PCI_LEGACY
363 * pci_read_legacy_io - read byte(s) from legacy I/O port space
364 * @kobj: kobject corresponding to file to read from
365 * @buf: buffer to store results
366 * @off: offset into legacy I/O port space
367 * @count: number of bytes to read
369 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
370 * callback routine (pci_legacy_read).
372 ssize_t
373 pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
374 char *buf, loff_t off, size_t count)
376 struct pci_bus *bus = to_pci_bus(container_of(kobj,
377 struct device,
378 kobj));
380 /* Only support 1, 2 or 4 byte accesses */
381 if (count != 1 && count != 2 && count != 4)
382 return -EINVAL;
384 return pci_legacy_read(bus, off, (u32 *)buf, count);
388 * pci_write_legacy_io - write byte(s) to legacy I/O port space
389 * @kobj: kobject corresponding to file to read from
390 * @buf: buffer containing value to be written
391 * @off: offset into legacy I/O port space
392 * @count: number of bytes to write
394 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
395 * callback routine (pci_legacy_write).
397 ssize_t
398 pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr,
399 char *buf, loff_t off, size_t count)
401 struct pci_bus *bus = to_pci_bus(container_of(kobj,
402 struct device,
403 kobj));
404 /* Only support 1, 2 or 4 byte accesses */
405 if (count != 1 && count != 2 && count != 4)
406 return -EINVAL;
408 return pci_legacy_write(bus, off, *(u32 *)buf, count);
412 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
413 * @kobj: kobject corresponding to device to be mapped
414 * @attr: struct bin_attribute for this file
415 * @vma: struct vm_area_struct passed to mmap
417 * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap
418 * legacy memory space (first meg of bus space) into application virtual
419 * memory space.
422 pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr,
423 struct vm_area_struct *vma)
425 struct pci_bus *bus = to_pci_bus(container_of(kobj,
426 struct device,
427 kobj));
429 return pci_mmap_legacy_page_range(bus, vma);
431 #endif /* HAVE_PCI_LEGACY */
433 #ifdef HAVE_PCI_MMAP
435 * pci_mmap_resource - map a PCI resource into user memory space
436 * @kobj: kobject for mapping
437 * @attr: struct bin_attribute for the file being mapped
438 * @vma: struct vm_area_struct passed into the mmap
440 * Use the regular PCI mapping routines to map a PCI resource into userspace.
441 * FIXME: write combining? maybe automatic for prefetchable regions?
443 static int
444 pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
445 struct vm_area_struct *vma)
447 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
448 struct device, kobj));
449 struct resource *res = (struct resource *)attr->private;
450 enum pci_mmap_state mmap_type;
451 resource_size_t start, end;
452 int i;
454 for (i = 0; i < PCI_ROM_RESOURCE; i++)
455 if (res == &pdev->resource[i])
456 break;
457 if (i >= PCI_ROM_RESOURCE)
458 return -ENODEV;
460 /* pci_mmap_page_range() expects the same kind of entry as coming
461 * from /proc/bus/pci/ which is a "user visible" value. If this is
462 * different from the resource itself, arch will do necessary fixup.
464 pci_resource_to_user(pdev, i, res, &start, &end);
465 vma->vm_pgoff += start >> PAGE_SHIFT;
466 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
468 return pci_mmap_page_range(pdev, vma, mmap_type, 0);
472 * pci_remove_resource_files - cleanup resource files
473 * @dev: dev to cleanup
475 * If we created resource files for @dev, remove them from sysfs and
476 * free their resources.
478 static void
479 pci_remove_resource_files(struct pci_dev *pdev)
481 int i;
483 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
484 struct bin_attribute *res_attr;
486 res_attr = pdev->res_attr[i];
487 if (res_attr) {
488 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
489 kfree(res_attr);
495 * pci_create_resource_files - create resource files in sysfs for @dev
496 * @dev: dev in question
498 * Walk the resources in @dev creating files for each resource available.
500 static int pci_create_resource_files(struct pci_dev *pdev)
502 int i;
503 int retval;
505 /* Expose the PCI resources from this device as files */
506 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
507 struct bin_attribute *res_attr;
509 /* skip empty resources */
510 if (!pci_resource_len(pdev, i))
511 continue;
513 /* allocate attribute structure, piggyback attribute name */
514 res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC);
515 if (res_attr) {
516 char *res_attr_name = (char *)(res_attr + 1);
518 pdev->res_attr[i] = res_attr;
519 sprintf(res_attr_name, "resource%d", i);
520 res_attr->attr.name = res_attr_name;
521 res_attr->attr.mode = S_IRUSR | S_IWUSR;
522 res_attr->size = pci_resource_len(pdev, i);
523 res_attr->mmap = pci_mmap_resource;
524 res_attr->private = &pdev->resource[i];
525 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
526 if (retval) {
527 pci_remove_resource_files(pdev);
528 return retval;
530 } else {
531 return -ENOMEM;
534 return 0;
536 #else /* !HAVE_PCI_MMAP */
537 static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; }
538 static inline void pci_remove_resource_files(struct pci_dev *dev) { return; }
539 #endif /* HAVE_PCI_MMAP */
542 * pci_write_rom - used to enable access to the PCI ROM display
543 * @kobj: kernel object handle
544 * @buf: user input
545 * @off: file offset
546 * @count: number of byte in input
548 * writing anything except 0 enables it
550 static ssize_t
551 pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
552 char *buf, loff_t off, size_t count)
554 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
556 if ((off == 0) && (*buf == '0') && (count == 2))
557 pdev->rom_attr_enabled = 0;
558 else
559 pdev->rom_attr_enabled = 1;
561 return count;
565 * pci_read_rom - read a PCI ROM
566 * @kobj: kernel object handle
567 * @buf: where to put the data we read from the ROM
568 * @off: file offset
569 * @count: number of bytes to read
571 * Put @count bytes starting at @off into @buf from the ROM in the PCI
572 * device corresponding to @kobj.
574 static ssize_t
575 pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr,
576 char *buf, loff_t off, size_t count)
578 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
579 void __iomem *rom;
580 size_t size;
582 if (!pdev->rom_attr_enabled)
583 return -EINVAL;
585 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
586 if (!rom)
587 return 0;
589 if (off >= size)
590 count = 0;
591 else {
592 if (off + count > size)
593 count = size - off;
595 memcpy_fromio(buf, rom + off, count);
597 pci_unmap_rom(pdev, rom);
599 return count;
602 static struct bin_attribute pci_config_attr = {
603 .attr = {
604 .name = "config",
605 .mode = S_IRUGO | S_IWUSR,
607 .size = 256,
608 .read = pci_read_config,
609 .write = pci_write_config,
612 static struct bin_attribute pcie_config_attr = {
613 .attr = {
614 .name = "config",
615 .mode = S_IRUGO | S_IWUSR,
617 .size = 4096,
618 .read = pci_read_config,
619 .write = pci_write_config,
622 int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
624 return 0;
627 int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
629 struct bin_attribute *rom_attr = NULL;
630 int retval;
632 if (!sysfs_initialized)
633 return -EACCES;
635 if (pdev->cfg_size < 4096)
636 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
637 else
638 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
639 if (retval)
640 goto err;
642 retval = pci_create_resource_files(pdev);
643 if (retval)
644 goto err_bin_file;
646 /* If the device has a ROM, try to expose it in sysfs. */
647 if (pci_resource_len(pdev, PCI_ROM_RESOURCE) ||
648 (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) {
649 rom_attr = kzalloc(sizeof(*rom_attr), GFP_ATOMIC);
650 if (rom_attr) {
651 pdev->rom_attr = rom_attr;
652 rom_attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
653 rom_attr->attr.name = "rom";
654 rom_attr->attr.mode = S_IRUSR;
655 rom_attr->read = pci_read_rom;
656 rom_attr->write = pci_write_rom;
657 retval = sysfs_create_bin_file(&pdev->dev.kobj, rom_attr);
658 if (retval)
659 goto err_rom;
660 } else {
661 retval = -ENOMEM;
662 goto err_resource_files;
665 /* add platform-specific attributes */
666 if (pcibios_add_platform_entries(pdev))
667 goto err_rom_file;
669 return 0;
671 err_rom_file:
672 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
673 sysfs_remove_bin_file(&pdev->dev.kobj, rom_attr);
674 err_rom:
675 kfree(rom_attr);
676 err_resource_files:
677 pci_remove_resource_files(pdev);
678 err_bin_file:
679 if (pdev->cfg_size < 4096)
680 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
681 else
682 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
683 err:
684 return retval;
688 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
689 * @pdev: device whose entries we should free
691 * Cleanup when @pdev is removed from sysfs.
693 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
695 if (!sysfs_initialized)
696 return;
698 if (pdev->cfg_size < 4096)
699 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
700 else
701 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
703 pci_remove_resource_files(pdev);
705 if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) {
706 if (pdev->rom_attr) {
707 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
708 kfree(pdev->rom_attr);
713 static int __init pci_sysfs_init(void)
715 struct pci_dev *pdev = NULL;
716 int retval;
718 sysfs_initialized = 1;
719 for_each_pci_dev(pdev) {
720 retval = pci_create_sysfs_dev_files(pdev);
721 if (retval) {
722 pci_dev_put(pdev);
723 return retval;
727 return 0;
730 late_initcall(pci_sysfs_init);