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