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