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