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