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