GNU Linux-libre 5.4.241-gnu1
[releases.git] / drivers / pci / pci-sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
4  * (C) Copyright 2002-2004 IBM Corp.
5  * (C) Copyright 2003 Matthew Wilcox
6  * (C) Copyright 2003 Hewlett-Packard
7  * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
8  * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
9  *
10  * File attributes for PCI devices
11  *
12  * Modeled after usb's driverfs.c
13  */
14
15
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/pci.h>
19 #include <linux/stat.h>
20 #include <linux/export.h>
21 #include <linux/topology.h>
22 #include <linux/mm.h>
23 #include <linux/fs.h>
24 #include <linux/capability.h>
25 #include <linux/security.h>
26 #include <linux/slab.h>
27 #include <linux/vgaarb.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/of.h>
30 #include "pci.h"
31
32 static int sysfs_initialized;   /* = 0 */
33
34 /* show configuration fields */
35 #define pci_config_attr(field, format_string)                           \
36 static ssize_t                                                          \
37 field##_show(struct device *dev, struct device_attribute *attr, char *buf)                              \
38 {                                                                       \
39         struct pci_dev *pdev;                                           \
40                                                                         \
41         pdev = to_pci_dev(dev);                                         \
42         return sprintf(buf, format_string, pdev->field);                \
43 }                                                                       \
44 static DEVICE_ATTR_RO(field)
45
46 pci_config_attr(vendor, "0x%04x\n");
47 pci_config_attr(device, "0x%04x\n");
48 pci_config_attr(subsystem_vendor, "0x%04x\n");
49 pci_config_attr(subsystem_device, "0x%04x\n");
50 pci_config_attr(revision, "0x%02x\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 (kstrtoul(buf, 0, &val) < 0)
70                 return -EINVAL;
71
72         pdev->broken_parity_status = !!val;
73
74         return count;
75 }
76 static DEVICE_ATTR_RW(broken_parity_status);
77
78 static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
79                                       struct device_attribute *attr, char *buf)
80 {
81         const struct cpumask *mask;
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         return cpumap_print_to_pagebuf(list, buf, mask);
90 }
91
92 static ssize_t local_cpus_show(struct device *dev,
93                                struct device_attribute *attr, char *buf)
94 {
95         return pci_dev_show_local_cpu(dev, false, attr, buf);
96 }
97 static DEVICE_ATTR_RO(local_cpus);
98
99 static ssize_t local_cpulist_show(struct device *dev,
100                                   struct device_attribute *attr, char *buf)
101 {
102         return pci_dev_show_local_cpu(dev, true, attr, buf);
103 }
104 static DEVICE_ATTR_RO(local_cpulist);
105
106 /*
107  * PCI Bus Class Devices
108  */
109 static ssize_t cpuaffinity_show(struct device *dev,
110                                 struct device_attribute *attr, char *buf)
111 {
112         const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
113
114         return cpumap_print_to_pagebuf(false, buf, cpumask);
115 }
116 static DEVICE_ATTR_RO(cpuaffinity);
117
118 static ssize_t cpulistaffinity_show(struct device *dev,
119                                     struct device_attribute *attr, char *buf)
120 {
121         const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
122
123         return cpumap_print_to_pagebuf(true, buf, cpumask);
124 }
125 static DEVICE_ATTR_RO(cpulistaffinity);
126
127 /* show resources */
128 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
129                              char *buf)
130 {
131         struct pci_dev *pci_dev = to_pci_dev(dev);
132         char *str = buf;
133         int i;
134         int max;
135         resource_size_t start, end;
136
137         if (pci_dev->subordinate)
138                 max = DEVICE_COUNT_RESOURCE;
139         else
140                 max = PCI_BRIDGE_RESOURCES;
141
142         for (i = 0; i < max; i++) {
143                 struct resource *res =  &pci_dev->resource[i];
144                 pci_resource_to_user(pci_dev, i, res, &start, &end);
145                 str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
146                                (unsigned long long)start,
147                                (unsigned long long)end,
148                                (unsigned long long)res->flags);
149         }
150         return (str - buf);
151 }
152 static DEVICE_ATTR_RO(resource);
153
154 static ssize_t max_link_speed_show(struct device *dev,
155                                    struct device_attribute *attr, char *buf)
156 {
157         struct pci_dev *pdev = to_pci_dev(dev);
158
159         return sprintf(buf, "%s\n", PCIE_SPEED2STR(pcie_get_speed_cap(pdev)));
160 }
161 static DEVICE_ATTR_RO(max_link_speed);
162
163 static ssize_t max_link_width_show(struct device *dev,
164                                    struct device_attribute *attr, char *buf)
165 {
166         struct pci_dev *pdev = to_pci_dev(dev);
167
168         return sprintf(buf, "%u\n", pcie_get_width_cap(pdev));
169 }
170 static DEVICE_ATTR_RO(max_link_width);
171
172 static ssize_t current_link_speed_show(struct device *dev,
173                                        struct device_attribute *attr, char *buf)
174 {
175         struct pci_dev *pci_dev = to_pci_dev(dev);
176         u16 linkstat;
177         int err;
178         const char *speed;
179
180         err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
181         if (err)
182                 return -EINVAL;
183
184         switch (linkstat & PCI_EXP_LNKSTA_CLS) {
185         case PCI_EXP_LNKSTA_CLS_32_0GB:
186                 speed = "32 GT/s";
187                 break;
188         case PCI_EXP_LNKSTA_CLS_16_0GB:
189                 speed = "16 GT/s";
190                 break;
191         case PCI_EXP_LNKSTA_CLS_8_0GB:
192                 speed = "8 GT/s";
193                 break;
194         case PCI_EXP_LNKSTA_CLS_5_0GB:
195                 speed = "5 GT/s";
196                 break;
197         case PCI_EXP_LNKSTA_CLS_2_5GB:
198                 speed = "2.5 GT/s";
199                 break;
200         default:
201                 speed = "Unknown speed";
202         }
203
204         return sprintf(buf, "%s\n", speed);
205 }
206 static DEVICE_ATTR_RO(current_link_speed);
207
208 static ssize_t current_link_width_show(struct device *dev,
209                                        struct device_attribute *attr, char *buf)
210 {
211         struct pci_dev *pci_dev = to_pci_dev(dev);
212         u16 linkstat;
213         int err;
214
215         err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
216         if (err)
217                 return -EINVAL;
218
219         return sprintf(buf, "%u\n",
220                 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
221 }
222 static DEVICE_ATTR_RO(current_link_width);
223
224 static ssize_t secondary_bus_number_show(struct device *dev,
225                                          struct device_attribute *attr,
226                                          char *buf)
227 {
228         struct pci_dev *pci_dev = to_pci_dev(dev);
229         u8 sec_bus;
230         int err;
231
232         err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
233         if (err)
234                 return -EINVAL;
235
236         return sprintf(buf, "%u\n", sec_bus);
237 }
238 static DEVICE_ATTR_RO(secondary_bus_number);
239
240 static ssize_t subordinate_bus_number_show(struct device *dev,
241                                            struct device_attribute *attr,
242                                            char *buf)
243 {
244         struct pci_dev *pci_dev = to_pci_dev(dev);
245         u8 sub_bus;
246         int err;
247
248         err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
249         if (err)
250                 return -EINVAL;
251
252         return sprintf(buf, "%u\n", sub_bus);
253 }
254 static DEVICE_ATTR_RO(subordinate_bus_number);
255
256 static ssize_t ari_enabled_show(struct device *dev,
257                                 struct device_attribute *attr,
258                                 char *buf)
259 {
260         struct pci_dev *pci_dev = to_pci_dev(dev);
261
262         return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
263 }
264 static DEVICE_ATTR_RO(ari_enabled);
265
266 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
267                              char *buf)
268 {
269         struct pci_dev *pci_dev = to_pci_dev(dev);
270
271         return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
272                        pci_dev->vendor, pci_dev->device,
273                        pci_dev->subsystem_vendor, pci_dev->subsystem_device,
274                        (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
275                        (u8)(pci_dev->class));
276 }
277 static DEVICE_ATTR_RO(modalias);
278
279 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
280                              const char *buf, size_t count)
281 {
282         struct pci_dev *pdev = to_pci_dev(dev);
283         unsigned long val;
284         ssize_t result = kstrtoul(buf, 0, &val);
285
286         if (result < 0)
287                 return result;
288
289         /* this can crash the machine when done on the "wrong" device */
290         if (!capable(CAP_SYS_ADMIN))
291                 return -EPERM;
292
293         device_lock(dev);
294         if (dev->driver)
295                 result = -EBUSY;
296         else if (val)
297                 result = pci_enable_device(pdev);
298         else if (pci_is_enabled(pdev))
299                 pci_disable_device(pdev);
300         else
301                 result = -EIO;
302         device_unlock(dev);
303
304         return result < 0 ? result : count;
305 }
306
307 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
308                             char *buf)
309 {
310         struct pci_dev *pdev;
311
312         pdev = to_pci_dev(dev);
313         return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
314 }
315 static DEVICE_ATTR_RW(enable);
316
317 #ifdef CONFIG_NUMA
318 static ssize_t numa_node_store(struct device *dev,
319                                struct device_attribute *attr, const char *buf,
320                                size_t count)
321 {
322         struct pci_dev *pdev = to_pci_dev(dev);
323         int node, ret;
324
325         if (!capable(CAP_SYS_ADMIN))
326                 return -EPERM;
327
328         ret = kstrtoint(buf, 0, &node);
329         if (ret)
330                 return ret;
331
332         if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
333                 return -EINVAL;
334
335         if (node != NUMA_NO_NODE && !node_online(node))
336                 return -EINVAL;
337
338         add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
339         pci_alert(pdev, FW_BUG "Overriding NUMA node to %d.  Contact your vendor for updates.",
340                   node);
341
342         dev->numa_node = node;
343         return count;
344 }
345
346 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
347                               char *buf)
348 {
349         return sprintf(buf, "%d\n", dev->numa_node);
350 }
351 static DEVICE_ATTR_RW(numa_node);
352 #endif
353
354 static ssize_t dma_mask_bits_show(struct device *dev,
355                                   struct device_attribute *attr, char *buf)
356 {
357         struct pci_dev *pdev = to_pci_dev(dev);
358
359         return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
360 }
361 static DEVICE_ATTR_RO(dma_mask_bits);
362
363 static ssize_t consistent_dma_mask_bits_show(struct device *dev,
364                                              struct device_attribute *attr,
365                                              char *buf)
366 {
367         return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
368 }
369 static DEVICE_ATTR_RO(consistent_dma_mask_bits);
370
371 static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
372                             char *buf)
373 {
374         struct pci_dev *pdev = to_pci_dev(dev);
375         struct pci_bus *subordinate = pdev->subordinate;
376
377         return sprintf(buf, "%u\n", subordinate ?
378                        !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
379                            : !pdev->no_msi);
380 }
381
382 static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
383                              const char *buf, size_t count)
384 {
385         struct pci_dev *pdev = to_pci_dev(dev);
386         struct pci_bus *subordinate = pdev->subordinate;
387         unsigned long val;
388
389         if (kstrtoul(buf, 0, &val) < 0)
390                 return -EINVAL;
391
392         if (!capable(CAP_SYS_ADMIN))
393                 return -EPERM;
394
395         /*
396          * "no_msi" and "bus_flags" only affect what happens when a driver
397          * requests MSI or MSI-X.  They don't affect any drivers that have
398          * already requested MSI or MSI-X.
399          */
400         if (!subordinate) {
401                 pdev->no_msi = !val;
402                 pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
403                          val ? "allowed" : "disallowed");
404                 return count;
405         }
406
407         if (val)
408                 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
409         else
410                 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
411
412         dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
413                  val ? "allowed" : "disallowed");
414         return count;
415 }
416 static DEVICE_ATTR_RW(msi_bus);
417
418 static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count)
419 {
420         unsigned long val;
421         struct pci_bus *b = NULL;
422
423         if (kstrtoul(buf, 0, &val) < 0)
424                 return -EINVAL;
425
426         if (val) {
427                 pci_lock_rescan_remove();
428                 while ((b = pci_find_next_bus(b)) != NULL)
429                         pci_rescan_bus(b);
430                 pci_unlock_rescan_remove();
431         }
432         return count;
433 }
434 static BUS_ATTR_WO(rescan);
435
436 static struct attribute *pci_bus_attrs[] = {
437         &bus_attr_rescan.attr,
438         NULL,
439 };
440
441 static const struct attribute_group pci_bus_group = {
442         .attrs = pci_bus_attrs,
443 };
444
445 const struct attribute_group *pci_bus_groups[] = {
446         &pci_bus_group,
447         NULL,
448 };
449
450 static ssize_t dev_rescan_store(struct device *dev,
451                                 struct device_attribute *attr, const char *buf,
452                                 size_t count)
453 {
454         unsigned long val;
455         struct pci_dev *pdev = to_pci_dev(dev);
456
457         if (kstrtoul(buf, 0, &val) < 0)
458                 return -EINVAL;
459
460         if (val) {
461                 pci_lock_rescan_remove();
462                 pci_rescan_bus(pdev->bus);
463                 pci_unlock_rescan_remove();
464         }
465         return count;
466 }
467 static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL,
468                                                             dev_rescan_store);
469
470 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
471                             const char *buf, size_t count)
472 {
473         unsigned long val;
474
475         if (kstrtoul(buf, 0, &val) < 0)
476                 return -EINVAL;
477
478         if (val && device_remove_file_self(dev, attr))
479                 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
480         return count;
481 }
482 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL,
483                                   remove_store);
484
485 static ssize_t bus_rescan_store(struct device *dev,
486                                 struct device_attribute *attr,
487                                 const char *buf, size_t count)
488 {
489         unsigned long val;
490         struct pci_bus *bus = to_pci_bus(dev);
491
492         if (kstrtoul(buf, 0, &val) < 0)
493                 return -EINVAL;
494
495         if (val) {
496                 pci_lock_rescan_remove();
497                 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
498                         pci_rescan_bus_bridge_resize(bus->self);
499                 else
500                         pci_rescan_bus(bus);
501                 pci_unlock_rescan_remove();
502         }
503         return count;
504 }
505 static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL,
506                                                             bus_rescan_store);
507
508 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
509 static ssize_t d3cold_allowed_store(struct device *dev,
510                                     struct device_attribute *attr,
511                                     const char *buf, size_t count)
512 {
513         struct pci_dev *pdev = to_pci_dev(dev);
514         unsigned long val;
515
516         if (kstrtoul(buf, 0, &val) < 0)
517                 return -EINVAL;
518
519         pdev->d3cold_allowed = !!val;
520         if (pdev->d3cold_allowed)
521                 pci_d3cold_enable(pdev);
522         else
523                 pci_d3cold_disable(pdev);
524
525         pm_runtime_resume(dev);
526
527         return count;
528 }
529
530 static ssize_t d3cold_allowed_show(struct device *dev,
531                                    struct device_attribute *attr, char *buf)
532 {
533         struct pci_dev *pdev = to_pci_dev(dev);
534         return sprintf(buf, "%u\n", pdev->d3cold_allowed);
535 }
536 static DEVICE_ATTR_RW(d3cold_allowed);
537 #endif
538
539 #ifdef CONFIG_OF
540 static ssize_t devspec_show(struct device *dev,
541                             struct device_attribute *attr, char *buf)
542 {
543         struct pci_dev *pdev = to_pci_dev(dev);
544         struct device_node *np = pci_device_to_OF_node(pdev);
545
546         if (np == NULL)
547                 return 0;
548         return sprintf(buf, "%pOF", np);
549 }
550 static DEVICE_ATTR_RO(devspec);
551 #endif
552
553 static ssize_t driver_override_store(struct device *dev,
554                                      struct device_attribute *attr,
555                                      const char *buf, size_t count)
556 {
557         struct pci_dev *pdev = to_pci_dev(dev);
558         char *driver_override, *old, *cp;
559
560         /* We need to keep extra room for a newline */
561         if (count >= (PAGE_SIZE - 1))
562                 return -EINVAL;
563
564         driver_override = kstrndup(buf, count, GFP_KERNEL);
565         if (!driver_override)
566                 return -ENOMEM;
567
568         cp = strchr(driver_override, '\n');
569         if (cp)
570                 *cp = '\0';
571
572         device_lock(dev);
573         old = pdev->driver_override;
574         if (strlen(driver_override)) {
575                 pdev->driver_override = driver_override;
576         } else {
577                 kfree(driver_override);
578                 pdev->driver_override = NULL;
579         }
580         device_unlock(dev);
581
582         kfree(old);
583
584         return count;
585 }
586
587 static ssize_t driver_override_show(struct device *dev,
588                                     struct device_attribute *attr, char *buf)
589 {
590         struct pci_dev *pdev = to_pci_dev(dev);
591         ssize_t len;
592
593         device_lock(dev);
594         len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
595         device_unlock(dev);
596         return len;
597 }
598 static DEVICE_ATTR_RW(driver_override);
599
600 static struct attribute *pci_dev_attrs[] = {
601         &dev_attr_resource.attr,
602         &dev_attr_vendor.attr,
603         &dev_attr_device.attr,
604         &dev_attr_subsystem_vendor.attr,
605         &dev_attr_subsystem_device.attr,
606         &dev_attr_revision.attr,
607         &dev_attr_class.attr,
608         &dev_attr_irq.attr,
609         &dev_attr_local_cpus.attr,
610         &dev_attr_local_cpulist.attr,
611         &dev_attr_modalias.attr,
612 #ifdef CONFIG_NUMA
613         &dev_attr_numa_node.attr,
614 #endif
615         &dev_attr_dma_mask_bits.attr,
616         &dev_attr_consistent_dma_mask_bits.attr,
617         &dev_attr_enable.attr,
618         &dev_attr_broken_parity_status.attr,
619         &dev_attr_msi_bus.attr,
620 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
621         &dev_attr_d3cold_allowed.attr,
622 #endif
623 #ifdef CONFIG_OF
624         &dev_attr_devspec.attr,
625 #endif
626         &dev_attr_driver_override.attr,
627         &dev_attr_ari_enabled.attr,
628         NULL,
629 };
630
631 static struct attribute *pci_bridge_attrs[] = {
632         &dev_attr_subordinate_bus_number.attr,
633         &dev_attr_secondary_bus_number.attr,
634         NULL,
635 };
636
637 static struct attribute *pcie_dev_attrs[] = {
638         &dev_attr_current_link_speed.attr,
639         &dev_attr_current_link_width.attr,
640         &dev_attr_max_link_width.attr,
641         &dev_attr_max_link_speed.attr,
642         NULL,
643 };
644
645 static struct attribute *pcibus_attrs[] = {
646         &dev_attr_bus_rescan.attr,
647         &dev_attr_cpuaffinity.attr,
648         &dev_attr_cpulistaffinity.attr,
649         NULL,
650 };
651
652 static const struct attribute_group pcibus_group = {
653         .attrs = pcibus_attrs,
654 };
655
656 const struct attribute_group *pcibus_groups[] = {
657         &pcibus_group,
658         NULL,
659 };
660
661 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
662                              char *buf)
663 {
664         struct pci_dev *pdev = to_pci_dev(dev);
665         struct pci_dev *vga_dev = vga_default_device();
666
667         if (vga_dev)
668                 return sprintf(buf, "%u\n", (pdev == vga_dev));
669
670         return sprintf(buf, "%u\n",
671                 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
672                    IORESOURCE_ROM_SHADOW));
673 }
674 static DEVICE_ATTR_RO(boot_vga);
675
676 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
677                                struct bin_attribute *bin_attr, char *buf,
678                                loff_t off, size_t count)
679 {
680         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
681         unsigned int size = 64;
682         loff_t init_off = off;
683         u8 *data = (u8 *) buf;
684
685         /* Several chips lock up trying to read undefined config space */
686         if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
687                 size = dev->cfg_size;
688         else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
689                 size = 128;
690
691         if (off > size)
692                 return 0;
693         if (off + count > size) {
694                 size -= off;
695                 count = size;
696         } else {
697                 size = count;
698         }
699
700         pci_config_pm_runtime_get(dev);
701
702         if ((off & 1) && size) {
703                 u8 val;
704                 pci_user_read_config_byte(dev, off, &val);
705                 data[off - init_off] = val;
706                 off++;
707                 size--;
708         }
709
710         if ((off & 3) && size > 2) {
711                 u16 val;
712                 pci_user_read_config_word(dev, off, &val);
713                 data[off - init_off] = val & 0xff;
714                 data[off - init_off + 1] = (val >> 8) & 0xff;
715                 off += 2;
716                 size -= 2;
717         }
718
719         while (size > 3) {
720                 u32 val;
721                 pci_user_read_config_dword(dev, off, &val);
722                 data[off - init_off] = val & 0xff;
723                 data[off - init_off + 1] = (val >> 8) & 0xff;
724                 data[off - init_off + 2] = (val >> 16) & 0xff;
725                 data[off - init_off + 3] = (val >> 24) & 0xff;
726                 off += 4;
727                 size -= 4;
728         }
729
730         if (size >= 2) {
731                 u16 val;
732                 pci_user_read_config_word(dev, off, &val);
733                 data[off - init_off] = val & 0xff;
734                 data[off - init_off + 1] = (val >> 8) & 0xff;
735                 off += 2;
736                 size -= 2;
737         }
738
739         if (size > 0) {
740                 u8 val;
741                 pci_user_read_config_byte(dev, off, &val);
742                 data[off - init_off] = val;
743                 off++;
744                 --size;
745         }
746
747         pci_config_pm_runtime_put(dev);
748
749         return count;
750 }
751
752 static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
753                                 struct bin_attribute *bin_attr, char *buf,
754                                 loff_t off, size_t count)
755 {
756         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
757         unsigned int size = count;
758         loff_t init_off = off;
759         u8 *data = (u8 *) buf;
760         int ret;
761
762         ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
763         if (ret)
764                 return ret;
765
766         if (off > dev->cfg_size)
767                 return 0;
768         if (off + count > dev->cfg_size) {
769                 size = dev->cfg_size - off;
770                 count = size;
771         }
772
773         pci_config_pm_runtime_get(dev);
774
775         if ((off & 1) && size) {
776                 pci_user_write_config_byte(dev, off, data[off - init_off]);
777                 off++;
778                 size--;
779         }
780
781         if ((off & 3) && size > 2) {
782                 u16 val = data[off - init_off];
783                 val |= (u16) data[off - init_off + 1] << 8;
784                 pci_user_write_config_word(dev, off, val);
785                 off += 2;
786                 size -= 2;
787         }
788
789         while (size > 3) {
790                 u32 val = data[off - init_off];
791                 val |= (u32) data[off - init_off + 1] << 8;
792                 val |= (u32) data[off - init_off + 2] << 16;
793                 val |= (u32) data[off - init_off + 3] << 24;
794                 pci_user_write_config_dword(dev, off, val);
795                 off += 4;
796                 size -= 4;
797         }
798
799         if (size >= 2) {
800                 u16 val = data[off - init_off];
801                 val |= (u16) data[off - init_off + 1] << 8;
802                 pci_user_write_config_word(dev, off, val);
803                 off += 2;
804                 size -= 2;
805         }
806
807         if (size) {
808                 pci_user_write_config_byte(dev, off, data[off - init_off]);
809                 off++;
810                 --size;
811         }
812
813         pci_config_pm_runtime_put(dev);
814
815         return count;
816 }
817
818 #ifdef HAVE_PCI_LEGACY
819 /**
820  * pci_read_legacy_io - read byte(s) from legacy I/O port space
821  * @filp: open sysfs file
822  * @kobj: kobject corresponding to file to read from
823  * @bin_attr: struct bin_attribute for this file
824  * @buf: buffer to store results
825  * @off: offset into legacy I/O port space
826  * @count: number of bytes to read
827  *
828  * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
829  * callback routine (pci_legacy_read).
830  */
831 static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
832                                   struct bin_attribute *bin_attr, char *buf,
833                                   loff_t off, size_t count)
834 {
835         struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
836
837         /* Only support 1, 2 or 4 byte accesses */
838         if (count != 1 && count != 2 && count != 4)
839                 return -EINVAL;
840
841         return pci_legacy_read(bus, off, (u32 *)buf, count);
842 }
843
844 /**
845  * pci_write_legacy_io - write byte(s) to legacy I/O port space
846  * @filp: open sysfs file
847  * @kobj: kobject corresponding to file to read from
848  * @bin_attr: struct bin_attribute for this file
849  * @buf: buffer containing value to be written
850  * @off: offset into legacy I/O port space
851  * @count: number of bytes to write
852  *
853  * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
854  * callback routine (pci_legacy_write).
855  */
856 static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
857                                    struct bin_attribute *bin_attr, char *buf,
858                                    loff_t off, size_t count)
859 {
860         struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
861
862         /* Only support 1, 2 or 4 byte accesses */
863         if (count != 1 && count != 2 && count != 4)
864                 return -EINVAL;
865
866         return pci_legacy_write(bus, off, *(u32 *)buf, count);
867 }
868
869 /**
870  * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
871  * @filp: open sysfs file
872  * @kobj: kobject corresponding to device to be mapped
873  * @attr: struct bin_attribute for this file
874  * @vma: struct vm_area_struct passed to mmap
875  *
876  * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
877  * legacy memory space (first meg of bus space) into application virtual
878  * memory space.
879  */
880 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
881                                struct bin_attribute *attr,
882                                struct vm_area_struct *vma)
883 {
884         struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
885
886         return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
887 }
888
889 /**
890  * pci_mmap_legacy_io - map legacy PCI IO into user memory space
891  * @filp: open sysfs file
892  * @kobj: kobject corresponding to device to be mapped
893  * @attr: struct bin_attribute for this file
894  * @vma: struct vm_area_struct passed to mmap
895  *
896  * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
897  * legacy IO space (first meg of bus space) into application virtual
898  * memory space. Returns -ENOSYS if the operation isn't supported
899  */
900 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
901                               struct bin_attribute *attr,
902                               struct vm_area_struct *vma)
903 {
904         struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
905
906         return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
907 }
908
909 /**
910  * pci_adjust_legacy_attr - adjustment of legacy file attributes
911  * @b: bus to create files under
912  * @mmap_type: I/O port or memory
913  *
914  * Stub implementation. Can be overridden by arch if necessary.
915  */
916 void __weak pci_adjust_legacy_attr(struct pci_bus *b,
917                                    enum pci_mmap_state mmap_type)
918 {
919 }
920
921 /**
922  * pci_create_legacy_files - create legacy I/O port and memory files
923  * @b: bus to create files under
924  *
925  * Some platforms allow access to legacy I/O port and ISA memory space on
926  * a per-bus basis.  This routine creates the files and ties them into
927  * their associated read, write and mmap files from pci-sysfs.c
928  *
929  * On error unwind, but don't propagate the error to the caller
930  * as it is ok to set up the PCI bus without these files.
931  */
932 void pci_create_legacy_files(struct pci_bus *b)
933 {
934         int error;
935
936         b->legacy_io = kcalloc(2, sizeof(struct bin_attribute),
937                                GFP_ATOMIC);
938         if (!b->legacy_io)
939                 goto kzalloc_err;
940
941         sysfs_bin_attr_init(b->legacy_io);
942         b->legacy_io->attr.name = "legacy_io";
943         b->legacy_io->size = 0xffff;
944         b->legacy_io->attr.mode = 0600;
945         b->legacy_io->read = pci_read_legacy_io;
946         b->legacy_io->write = pci_write_legacy_io;
947         b->legacy_io->mmap = pci_mmap_legacy_io;
948         pci_adjust_legacy_attr(b, pci_mmap_io);
949         error = device_create_bin_file(&b->dev, b->legacy_io);
950         if (error)
951                 goto legacy_io_err;
952
953         /* Allocated above after the legacy_io struct */
954         b->legacy_mem = b->legacy_io + 1;
955         sysfs_bin_attr_init(b->legacy_mem);
956         b->legacy_mem->attr.name = "legacy_mem";
957         b->legacy_mem->size = 1024*1024;
958         b->legacy_mem->attr.mode = 0600;
959         b->legacy_mem->mmap = pci_mmap_legacy_mem;
960         pci_adjust_legacy_attr(b, pci_mmap_mem);
961         error = device_create_bin_file(&b->dev, b->legacy_mem);
962         if (error)
963                 goto legacy_mem_err;
964
965         return;
966
967 legacy_mem_err:
968         device_remove_bin_file(&b->dev, b->legacy_io);
969 legacy_io_err:
970         kfree(b->legacy_io);
971         b->legacy_io = NULL;
972 kzalloc_err:
973         dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n");
974 }
975
976 void pci_remove_legacy_files(struct pci_bus *b)
977 {
978         if (b->legacy_io) {
979                 device_remove_bin_file(&b->dev, b->legacy_io);
980                 device_remove_bin_file(&b->dev, b->legacy_mem);
981                 kfree(b->legacy_io); /* both are allocated here */
982         }
983 }
984 #endif /* HAVE_PCI_LEGACY */
985
986 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
987
988 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
989                   enum pci_mmap_api mmap_api)
990 {
991         unsigned long nr, start, size;
992         resource_size_t pci_start = 0, pci_end;
993
994         if (pci_resource_len(pdev, resno) == 0)
995                 return 0;
996         nr = vma_pages(vma);
997         start = vma->vm_pgoff;
998         size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
999         if (mmap_api == PCI_MMAP_PROCFS) {
1000                 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1001                                      &pci_start, &pci_end);
1002                 pci_start >>= PAGE_SHIFT;
1003         }
1004         if (start >= pci_start && start < pci_start + size &&
1005                         start + nr <= pci_start + size)
1006                 return 1;
1007         return 0;
1008 }
1009
1010 /**
1011  * pci_mmap_resource - map a PCI resource into user memory space
1012  * @kobj: kobject for mapping
1013  * @attr: struct bin_attribute for the file being mapped
1014  * @vma: struct vm_area_struct passed into the mmap
1015  * @write_combine: 1 for write_combine mapping
1016  *
1017  * Use the regular PCI mapping routines to map a PCI resource into userspace.
1018  */
1019 static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1020                              struct vm_area_struct *vma, int write_combine)
1021 {
1022         struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1023         int bar = (unsigned long)attr->private;
1024         enum pci_mmap_state mmap_type;
1025         struct resource *res = &pdev->resource[bar];
1026         int ret;
1027
1028         ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1029         if (ret)
1030                 return ret;
1031
1032         if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1033                 return -EINVAL;
1034
1035         if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
1036                 return -EINVAL;
1037
1038         mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
1039
1040         return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
1041 }
1042
1043 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1044                                 struct bin_attribute *attr,
1045                                 struct vm_area_struct *vma)
1046 {
1047         return pci_mmap_resource(kobj, attr, vma, 0);
1048 }
1049
1050 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1051                                 struct bin_attribute *attr,
1052                                 struct vm_area_struct *vma)
1053 {
1054         return pci_mmap_resource(kobj, attr, vma, 1);
1055 }
1056
1057 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1058                                struct bin_attribute *attr, char *buf,
1059                                loff_t off, size_t count, bool write)
1060 {
1061         struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1062         int bar = (unsigned long)attr->private;
1063         unsigned long port = off;
1064
1065         port += pci_resource_start(pdev, bar);
1066
1067         if (port > pci_resource_end(pdev, bar))
1068                 return 0;
1069
1070         if (port + count - 1 > pci_resource_end(pdev, bar))
1071                 return -EINVAL;
1072
1073         switch (count) {
1074         case 1:
1075                 if (write)
1076                         outb(*(u8 *)buf, port);
1077                 else
1078                         *(u8 *)buf = inb(port);
1079                 return 1;
1080         case 2:
1081                 if (write)
1082                         outw(*(u16 *)buf, port);
1083                 else
1084                         *(u16 *)buf = inw(port);
1085                 return 2;
1086         case 4:
1087                 if (write)
1088                         outl(*(u32 *)buf, port);
1089                 else
1090                         *(u32 *)buf = inl(port);
1091                 return 4;
1092         }
1093         return -EINVAL;
1094 }
1095
1096 static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1097                                     struct bin_attribute *attr, char *buf,
1098                                     loff_t off, size_t count)
1099 {
1100         return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1101 }
1102
1103 static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1104                                      struct bin_attribute *attr, char *buf,
1105                                      loff_t off, size_t count)
1106 {
1107         int ret;
1108
1109         ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1110         if (ret)
1111                 return ret;
1112
1113         return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1114 }
1115
1116 /**
1117  * pci_remove_resource_files - cleanup resource files
1118  * @pdev: dev to cleanup
1119  *
1120  * If we created resource files for @pdev, remove them from sysfs and
1121  * free their resources.
1122  */
1123 static void pci_remove_resource_files(struct pci_dev *pdev)
1124 {
1125         int i;
1126
1127         for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1128                 struct bin_attribute *res_attr;
1129
1130                 res_attr = pdev->res_attr[i];
1131                 if (res_attr) {
1132                         sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1133                         kfree(res_attr);
1134                 }
1135
1136                 res_attr = pdev->res_attr_wc[i];
1137                 if (res_attr) {
1138                         sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1139                         kfree(res_attr);
1140                 }
1141         }
1142 }
1143
1144 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1145 {
1146         /* allocate attribute structure, piggyback attribute name */
1147         int name_len = write_combine ? 13 : 10;
1148         struct bin_attribute *res_attr;
1149         char *res_attr_name;
1150         int retval;
1151
1152         res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1153         if (!res_attr)
1154                 return -ENOMEM;
1155
1156         res_attr_name = (char *)(res_attr + 1);
1157
1158         sysfs_bin_attr_init(res_attr);
1159         if (write_combine) {
1160                 sprintf(res_attr_name, "resource%d_wc", num);
1161                 res_attr->mmap = pci_mmap_resource_wc;
1162         } else {
1163                 sprintf(res_attr_name, "resource%d", num);
1164                 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1165                         res_attr->read = pci_read_resource_io;
1166                         res_attr->write = pci_write_resource_io;
1167                         if (arch_can_pci_mmap_io())
1168                                 res_attr->mmap = pci_mmap_resource_uc;
1169                 } else {
1170                         res_attr->mmap = pci_mmap_resource_uc;
1171                 }
1172         }
1173         res_attr->attr.name = res_attr_name;
1174         res_attr->attr.mode = 0600;
1175         res_attr->size = pci_resource_len(pdev, num);
1176         res_attr->private = (void *)(unsigned long)num;
1177         retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1178         if (retval) {
1179                 kfree(res_attr);
1180                 return retval;
1181         }
1182
1183         if (write_combine)
1184                 pdev->res_attr_wc[num] = res_attr;
1185         else
1186                 pdev->res_attr[num] = res_attr;
1187
1188         return 0;
1189 }
1190
1191 /**
1192  * pci_create_resource_files - create resource files in sysfs for @dev
1193  * @pdev: dev in question
1194  *
1195  * Walk the resources in @pdev creating files for each resource available.
1196  */
1197 static int pci_create_resource_files(struct pci_dev *pdev)
1198 {
1199         int i;
1200         int retval;
1201
1202         /* Expose the PCI resources from this device as files */
1203         for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1204
1205                 /* skip empty resources */
1206                 if (!pci_resource_len(pdev, i))
1207                         continue;
1208
1209                 retval = pci_create_attr(pdev, i, 0);
1210                 /* for prefetchable resources, create a WC mappable file */
1211                 if (!retval && arch_can_pci_mmap_wc() &&
1212                     pdev->resource[i].flags & IORESOURCE_PREFETCH)
1213                         retval = pci_create_attr(pdev, i, 1);
1214                 if (retval) {
1215                         pci_remove_resource_files(pdev);
1216                         return retval;
1217                 }
1218         }
1219         return 0;
1220 }
1221 #else /* !HAVE_PCI_MMAP */
1222 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1223 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1224 #endif /* HAVE_PCI_MMAP */
1225
1226 /**
1227  * pci_write_rom - used to enable access to the PCI ROM display
1228  * @filp: sysfs file
1229  * @kobj: kernel object handle
1230  * @bin_attr: struct bin_attribute for this file
1231  * @buf: user input
1232  * @off: file offset
1233  * @count: number of byte in input
1234  *
1235  * writing anything except 0 enables it
1236  */
1237 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1238                              struct bin_attribute *bin_attr, char *buf,
1239                              loff_t off, size_t count)
1240 {
1241         struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1242
1243         if ((off ==  0) && (*buf == '0') && (count == 2))
1244                 pdev->rom_attr_enabled = 0;
1245         else
1246                 pdev->rom_attr_enabled = 1;
1247
1248         return count;
1249 }
1250
1251 /**
1252  * pci_read_rom - read a PCI ROM
1253  * @filp: sysfs file
1254  * @kobj: kernel object handle
1255  * @bin_attr: struct bin_attribute for this file
1256  * @buf: where to put the data we read from the ROM
1257  * @off: file offset
1258  * @count: number of bytes to read
1259  *
1260  * Put @count bytes starting at @off into @buf from the ROM in the PCI
1261  * device corresponding to @kobj.
1262  */
1263 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1264                             struct bin_attribute *bin_attr, char *buf,
1265                             loff_t off, size_t count)
1266 {
1267         struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1268         void __iomem *rom;
1269         size_t size;
1270
1271         if (!pdev->rom_attr_enabled)
1272                 return -EINVAL;
1273
1274         rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
1275         if (!rom || !size)
1276                 return -EIO;
1277
1278         if (off >= size)
1279                 count = 0;
1280         else {
1281                 if (off + count > size)
1282                         count = size - off;
1283
1284                 memcpy_fromio(buf, rom + off, count);
1285         }
1286         pci_unmap_rom(pdev, rom);
1287
1288         return count;
1289 }
1290
1291 static const struct bin_attribute pci_config_attr = {
1292         .attr = {
1293                 .name = "config",
1294                 .mode = 0644,
1295         },
1296         .size = PCI_CFG_SPACE_SIZE,
1297         .read = pci_read_config,
1298         .write = pci_write_config,
1299 };
1300
1301 static const struct bin_attribute pcie_config_attr = {
1302         .attr = {
1303                 .name = "config",
1304                 .mode = 0644,
1305         },
1306         .size = PCI_CFG_SPACE_EXP_SIZE,
1307         .read = pci_read_config,
1308         .write = pci_write_config,
1309 };
1310
1311 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1312                            const char *buf, size_t count)
1313 {
1314         struct pci_dev *pdev = to_pci_dev(dev);
1315         unsigned long val;
1316         ssize_t result = kstrtoul(buf, 0, &val);
1317
1318         if (result < 0)
1319                 return result;
1320
1321         if (val != 1)
1322                 return -EINVAL;
1323
1324         pm_runtime_get_sync(dev);
1325         result = pci_reset_function(pdev);
1326         pm_runtime_put(dev);
1327         if (result < 0)
1328                 return result;
1329
1330         return count;
1331 }
1332
1333 static DEVICE_ATTR(reset, 0200, NULL, reset_store);
1334
1335 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1336 {
1337         int retval;
1338
1339         pcie_vpd_create_sysfs_dev_files(dev);
1340         pcie_aspm_create_sysfs_dev_files(dev);
1341
1342         if (dev->reset_fn) {
1343                 retval = device_create_file(&dev->dev, &dev_attr_reset);
1344                 if (retval)
1345                         goto error;
1346         }
1347         return 0;
1348
1349 error:
1350         pcie_aspm_remove_sysfs_dev_files(dev);
1351         pcie_vpd_remove_sysfs_dev_files(dev);
1352         return retval;
1353 }
1354
1355 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
1356 {
1357         int retval;
1358         int rom_size;
1359         struct bin_attribute *attr;
1360
1361         if (!sysfs_initialized)
1362                 return -EACCES;
1363
1364         if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1365                 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1366         else
1367                 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1368         if (retval)
1369                 goto err;
1370
1371         retval = pci_create_resource_files(pdev);
1372         if (retval)
1373                 goto err_config_file;
1374
1375         /* If the device has a ROM, try to expose it in sysfs. */
1376         rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1377         if (rom_size) {
1378                 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1379                 if (!attr) {
1380                         retval = -ENOMEM;
1381                         goto err_resource_files;
1382                 }
1383                 sysfs_bin_attr_init(attr);
1384                 attr->size = rom_size;
1385                 attr->attr.name = "rom";
1386                 attr->attr.mode = 0600;
1387                 attr->read = pci_read_rom;
1388                 attr->write = pci_write_rom;
1389                 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1390                 if (retval) {
1391                         kfree(attr);
1392                         goto err_resource_files;
1393                 }
1394                 pdev->rom_attr = attr;
1395         }
1396
1397         /* add sysfs entries for various capabilities */
1398         retval = pci_create_capabilities_sysfs(pdev);
1399         if (retval)
1400                 goto err_rom_file;
1401
1402         pci_create_firmware_label_files(pdev);
1403
1404         return 0;
1405
1406 err_rom_file:
1407         if (pdev->rom_attr) {
1408                 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1409                 kfree(pdev->rom_attr);
1410                 pdev->rom_attr = NULL;
1411         }
1412 err_resource_files:
1413         pci_remove_resource_files(pdev);
1414 err_config_file:
1415         if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1416                 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1417         else
1418                 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1419 err:
1420         return retval;
1421 }
1422
1423 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1424 {
1425         pcie_vpd_remove_sysfs_dev_files(dev);
1426         pcie_aspm_remove_sysfs_dev_files(dev);
1427         if (dev->reset_fn) {
1428                 device_remove_file(&dev->dev, &dev_attr_reset);
1429                 dev->reset_fn = 0;
1430         }
1431 }
1432
1433 /**
1434  * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1435  * @pdev: device whose entries we should free
1436  *
1437  * Cleanup when @pdev is removed from sysfs.
1438  */
1439 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1440 {
1441         if (!sysfs_initialized)
1442                 return;
1443
1444         pci_remove_capabilities_sysfs(pdev);
1445
1446         if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1447                 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1448         else
1449                 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1450
1451         pci_remove_resource_files(pdev);
1452
1453         if (pdev->rom_attr) {
1454                 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1455                 kfree(pdev->rom_attr);
1456                 pdev->rom_attr = NULL;
1457         }
1458
1459         pci_remove_firmware_label_files(pdev);
1460 }
1461
1462 static int __init pci_sysfs_init(void)
1463 {
1464         struct pci_dev *pdev = NULL;
1465         int retval;
1466
1467         sysfs_initialized = 1;
1468         for_each_pci_dev(pdev) {
1469                 retval = pci_create_sysfs_dev_files(pdev);
1470                 if (retval) {
1471                         pci_dev_put(pdev);
1472                         return retval;
1473                 }
1474         }
1475
1476         return 0;
1477 }
1478 late_initcall(pci_sysfs_init);
1479
1480 static struct attribute *pci_dev_dev_attrs[] = {
1481         &dev_attr_boot_vga.attr,
1482         NULL,
1483 };
1484
1485 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1486                                          struct attribute *a, int n)
1487 {
1488         struct device *dev = kobj_to_dev(kobj);
1489         struct pci_dev *pdev = to_pci_dev(dev);
1490
1491         if (a == &dev_attr_boot_vga.attr)
1492                 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1493                         return 0;
1494
1495         return a->mode;
1496 }
1497
1498 static struct attribute *pci_dev_hp_attrs[] = {
1499         &dev_attr_remove.attr,
1500         &dev_attr_dev_rescan.attr,
1501         NULL,
1502 };
1503
1504 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1505                                             struct attribute *a, int n)
1506 {
1507         struct device *dev = kobj_to_dev(kobj);
1508         struct pci_dev *pdev = to_pci_dev(dev);
1509
1510         if (pdev->is_virtfn)
1511                 return 0;
1512
1513         return a->mode;
1514 }
1515
1516 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1517                                             struct attribute *a, int n)
1518 {
1519         struct device *dev = kobj_to_dev(kobj);
1520         struct pci_dev *pdev = to_pci_dev(dev);
1521
1522         if (pci_is_bridge(pdev))
1523                 return a->mode;
1524
1525         return 0;
1526 }
1527
1528 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1529                                           struct attribute *a, int n)
1530 {
1531         struct device *dev = kobj_to_dev(kobj);
1532         struct pci_dev *pdev = to_pci_dev(dev);
1533
1534         if (pci_is_pcie(pdev))
1535                 return a->mode;
1536
1537         return 0;
1538 }
1539
1540 static const struct attribute_group pci_dev_group = {
1541         .attrs = pci_dev_attrs,
1542 };
1543
1544 const struct attribute_group *pci_dev_groups[] = {
1545         &pci_dev_group,
1546         NULL,
1547 };
1548
1549 static const struct attribute_group pci_bridge_group = {
1550         .attrs = pci_bridge_attrs,
1551 };
1552
1553 const struct attribute_group *pci_bridge_groups[] = {
1554         &pci_bridge_group,
1555         NULL,
1556 };
1557
1558 static const struct attribute_group pcie_dev_group = {
1559         .attrs = pcie_dev_attrs,
1560 };
1561
1562 const struct attribute_group *pcie_dev_groups[] = {
1563         &pcie_dev_group,
1564         NULL,
1565 };
1566
1567 static const struct attribute_group pci_dev_hp_attr_group = {
1568         .attrs = pci_dev_hp_attrs,
1569         .is_visible = pci_dev_hp_attrs_are_visible,
1570 };
1571
1572 static const struct attribute_group pci_dev_attr_group = {
1573         .attrs = pci_dev_dev_attrs,
1574         .is_visible = pci_dev_attrs_are_visible,
1575 };
1576
1577 static const struct attribute_group pci_bridge_attr_group = {
1578         .attrs = pci_bridge_attrs,
1579         .is_visible = pci_bridge_attrs_are_visible,
1580 };
1581
1582 static const struct attribute_group pcie_dev_attr_group = {
1583         .attrs = pcie_dev_attrs,
1584         .is_visible = pcie_dev_attrs_are_visible,
1585 };
1586
1587 static const struct attribute_group *pci_dev_attr_groups[] = {
1588         &pci_dev_attr_group,
1589         &pci_dev_hp_attr_group,
1590 #ifdef CONFIG_PCI_IOV
1591         &sriov_dev_attr_group,
1592 #endif
1593         &pci_bridge_attr_group,
1594         &pcie_dev_attr_group,
1595 #ifdef CONFIG_PCIEAER
1596         &aer_stats_attr_group,
1597 #endif
1598         NULL,
1599 };
1600
1601 const struct device_type pci_dev_type = {
1602         .groups = pci_dev_attr_groups,
1603 };