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