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