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