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