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