]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/pci-sysfs.c
PCI: fix size checks for mmap() on /proc/bus/pci files
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / pci-sysfs.c
CommitLineData
1da177e4
LT
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
1da177e4 18#include <linux/kernel.h>
b5ff7df3 19#include <linux/sched.h>
1da177e4
LT
20#include <linux/pci.h>
21#include <linux/stat.h>
22#include <linux/topology.h>
23#include <linux/mm.h>
de139a33 24#include <linux/fs.h>
aa0ac365 25#include <linux/capability.h>
7d715a6c 26#include <linux/pci-aspm.h>
5a0e3ad6 27#include <linux/slab.h>
1da177e4
LT
28#include "pci.h"
29
30static int sysfs_initialized; /* = 0 */
31
32/* show configuration fields */
33#define pci_config_attr(field, format_string) \
34static ssize_t \
e404e274 35field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
36{ \
37 struct pci_dev *pdev; \
38 \
39 pdev = to_pci_dev (dev); \
40 return sprintf (buf, format_string, pdev->field); \
41}
42
43pci_config_attr(vendor, "0x%04x\n");
44pci_config_attr(device, "0x%04x\n");
45pci_config_attr(subsystem_vendor, "0x%04x\n");
46pci_config_attr(subsystem_device, "0x%04x\n");
47pci_config_attr(class, "0x%06x\n");
48pci_config_attr(irq, "%u\n");
49
bdee9d98
DT
50static ssize_t broken_parity_status_show(struct device *dev,
51 struct device_attribute *attr,
52 char *buf)
53{
54 struct pci_dev *pdev = to_pci_dev(dev);
55 return sprintf (buf, "%u\n", pdev->broken_parity_status);
56}
57
58static ssize_t broken_parity_status_store(struct device *dev,
59 struct device_attribute *attr,
60 const char *buf, size_t count)
61{
62 struct pci_dev *pdev = to_pci_dev(dev);
92425a40 63 unsigned long val;
bdee9d98 64
92425a40
TP
65 if (strict_strtoul(buf, 0, &val) < 0)
66 return -EINVAL;
67
68 pdev->broken_parity_status = !!val;
69
70 return count;
bdee9d98
DT
71}
72
4327edf6
AC
73static ssize_t local_cpus_show(struct device *dev,
74 struct device_attribute *attr, char *buf)
1da177e4 75{
3be83050 76 const struct cpumask *mask;
4327edf6
AC
77 int len;
78
e0cd5160 79#ifdef CONFIG_NUMA
6be954d1
DJ
80 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
81 cpumask_of_node(dev_to_node(dev));
e0cd5160 82#else
3be83050 83 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
e0cd5160 84#endif
3be83050 85 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
39106dcf
MT
86 buf[len++] = '\n';
87 buf[len] = '\0';
88 return len;
89}
90
91
92static ssize_t local_cpulist_show(struct device *dev,
93 struct device_attribute *attr, char *buf)
94{
3be83050 95 const struct cpumask *mask;
39106dcf
MT
96 int len;
97
e0cd5160 98#ifdef CONFIG_NUMA
6be954d1
DJ
99 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
100 cpumask_of_node(dev_to_node(dev));
e0cd5160 101#else
3be83050 102 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
e0cd5160 103#endif
3be83050 104 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
39106dcf
MT
105 buf[len++] = '\n';
106 buf[len] = '\0';
107 return len;
1da177e4
LT
108}
109
110/* show resources */
111static ssize_t
e404e274 112resource_show(struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
113{
114 struct pci_dev * pci_dev = to_pci_dev(dev);
115 char * str = buf;
116 int i;
fde09c6d 117 int max;
e31dd6e4 118 resource_size_t start, end;
1da177e4
LT
119
120 if (pci_dev->subordinate)
121 max = DEVICE_COUNT_RESOURCE;
fde09c6d
YZ
122 else
123 max = PCI_BRIDGE_RESOURCES;
1da177e4
LT
124
125 for (i = 0; i < max; i++) {
2311b1f2
ME
126 struct resource *res = &pci_dev->resource[i];
127 pci_resource_to_user(pci_dev, i, res, &start, &end);
128 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
129 (unsigned long long)start,
130 (unsigned long long)end,
131 (unsigned long long)res->flags);
1da177e4
LT
132 }
133 return (str - buf);
134}
135
87c8a443 136static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
9888549e
GKH
137{
138 struct pci_dev *pci_dev = to_pci_dev(dev);
139
140 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
141 pci_dev->vendor, pci_dev->device,
142 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
143 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
144 (u8)(pci_dev->class));
145}
bae94d02
IPG
146
147static ssize_t is_enabled_store(struct device *dev,
148 struct device_attribute *attr, const char *buf,
149 size_t count)
9f125d30
AV
150{
151 struct pci_dev *pdev = to_pci_dev(dev);
92425a40
TP
152 unsigned long val;
153 ssize_t result = strict_strtoul(buf, 0, &val);
154
155 if (result < 0)
156 return result;
9f125d30
AV
157
158 /* this can crash the machine when done on the "wrong" device */
159 if (!capable(CAP_SYS_ADMIN))
92425a40 160 return -EPERM;
9f125d30 161
92425a40 162 if (!val) {
296ccb08 163 if (pci_is_enabled(pdev))
bae94d02
IPG
164 pci_disable_device(pdev);
165 else
166 result = -EIO;
92425a40 167 } else
bae94d02 168 result = pci_enable_device(pdev);
9f125d30 169
bae94d02
IPG
170 return result < 0 ? result : count;
171}
172
173static ssize_t is_enabled_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
176 struct pci_dev *pdev;
9f125d30 177
bae94d02
IPG
178 pdev = to_pci_dev (dev);
179 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
9f125d30
AV
180}
181
81bb0e19
BG
182#ifdef CONFIG_NUMA
183static ssize_t
184numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
185{
186 return sprintf (buf, "%d\n", dev->numa_node);
187}
188#endif
189
bb965401
YL
190static ssize_t
191dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
192{
193 struct pci_dev *pdev = to_pci_dev(dev);
194
195 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
196}
197
198static ssize_t
199consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
200 char *buf)
201{
202 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
203}
204
fe97064c
BG
205static ssize_t
206msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
207{
208 struct pci_dev *pdev = to_pci_dev(dev);
209
210 if (!pdev->subordinate)
211 return 0;
212
213 return sprintf (buf, "%u\n",
214 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
215}
216
217static ssize_t
218msi_bus_store(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
220{
221 struct pci_dev *pdev = to_pci_dev(dev);
92425a40
TP
222 unsigned long val;
223
224 if (strict_strtoul(buf, 0, &val) < 0)
225 return -EINVAL;
fe97064c
BG
226
227 /* bad things may happen if the no_msi flag is changed
228 * while some drivers are loaded */
229 if (!capable(CAP_SYS_ADMIN))
92425a40 230 return -EPERM;
fe97064c 231
92425a40
TP
232 /* Maybe pci devices without subordinate busses shouldn't even have this
233 * attribute in the first place? */
fe97064c
BG
234 if (!pdev->subordinate)
235 return count;
236
92425a40
TP
237 /* Is the flag going to change, or keep the value it already had? */
238 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
239 !!val) {
240 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
fe97064c 241
92425a40
TP
242 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
243 " bad things could happen\n", val ? "" : " not");
fe97064c
BG
244 }
245
246 return count;
247}
9888549e 248
705b1aaa
AC
249#ifdef CONFIG_HOTPLUG
250static DEFINE_MUTEX(pci_remove_rescan_mutex);
251static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
252 size_t count)
253{
254 unsigned long val;
255 struct pci_bus *b = NULL;
256
257 if (strict_strtoul(buf, 0, &val) < 0)
258 return -EINVAL;
259
260 if (val) {
261 mutex_lock(&pci_remove_rescan_mutex);
262 while ((b = pci_find_next_bus(b)) != NULL)
263 pci_rescan_bus(b);
264 mutex_unlock(&pci_remove_rescan_mutex);
265 }
266 return count;
267}
268
269struct bus_attribute pci_bus_attrs[] = {
270 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
271 __ATTR_NULL
272};
77c27c7b 273
738a6396
AC
274static ssize_t
275dev_rescan_store(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
277{
278 unsigned long val;
279 struct pci_dev *pdev = to_pci_dev(dev);
280
281 if (strict_strtoul(buf, 0, &val) < 0)
282 return -EINVAL;
283
284 if (val) {
285 mutex_lock(&pci_remove_rescan_mutex);
286 pci_rescan_bus(pdev->bus);
287 mutex_unlock(&pci_remove_rescan_mutex);
288 }
289 return count;
290}
291
77c27c7b
AC
292static void remove_callback(struct device *dev)
293{
294 struct pci_dev *pdev = to_pci_dev(dev);
295
296 mutex_lock(&pci_remove_rescan_mutex);
297 pci_remove_bus_device(pdev);
298 mutex_unlock(&pci_remove_rescan_mutex);
299}
300
301static ssize_t
302remove_store(struct device *dev, struct device_attribute *dummy,
303 const char *buf, size_t count)
304{
305 int ret = 0;
306 unsigned long val;
77c27c7b
AC
307
308 if (strict_strtoul(buf, 0, &val) < 0)
309 return -EINVAL;
310
77c27c7b
AC
311 /* An attribute cannot be unregistered by one of its own methods,
312 * so we have to use this roundabout approach.
313 */
314 if (val)
315 ret = device_schedule_callback(dev, remove_callback);
316 if (ret)
317 count = ret;
318 return count;
319}
705b1aaa
AC
320#endif
321
1da177e4
LT
322struct device_attribute pci_dev_attrs[] = {
323 __ATTR_RO(resource),
324 __ATTR_RO(vendor),
325 __ATTR_RO(device),
326 __ATTR_RO(subsystem_vendor),
327 __ATTR_RO(subsystem_device),
328 __ATTR_RO(class),
329 __ATTR_RO(irq),
330 __ATTR_RO(local_cpus),
39106dcf 331 __ATTR_RO(local_cpulist),
9888549e 332 __ATTR_RO(modalias),
81bb0e19
BG
333#ifdef CONFIG_NUMA
334 __ATTR_RO(numa_node),
335#endif
bb965401
YL
336 __ATTR_RO(dma_mask_bits),
337 __ATTR_RO(consistent_dma_mask_bits),
9f125d30 338 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
bdee9d98
DT
339 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
340 broken_parity_status_show,broken_parity_status_store),
fe97064c 341 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
77c27c7b
AC
342#ifdef CONFIG_HOTPLUG
343 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
738a6396 344 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
77c27c7b 345#endif
1da177e4
LT
346 __ATTR_NULL,
347};
348
217f45de
DA
349static ssize_t
350boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
351{
352 struct pci_dev *pdev = to_pci_dev(dev);
353
354 return sprintf(buf, "%u\n",
355 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
356 IORESOURCE_ROM_SHADOW));
357}
358struct device_attribute vga_attr = __ATTR_RO(boot_vga);
359
1da177e4 360static ssize_t
2c3c8bea
CW
361pci_read_config(struct file *filp, struct kobject *kobj,
362 struct bin_attribute *bin_attr,
91a69029 363 char *buf, loff_t off, size_t count)
1da177e4
LT
364{
365 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
366 unsigned int size = 64;
367 loff_t init_off = off;
4c0619ad 368 u8 *data = (u8*) buf;
1da177e4
LT
369
370 /* Several chips lock up trying to read undefined config space */
de139a33 371 if (cap_raised(filp->f_cred->cap_effective, CAP_SYS_ADMIN)) {
1da177e4
LT
372 size = dev->cfg_size;
373 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
374 size = 128;
375 }
376
377 if (off > size)
378 return 0;
379 if (off + count > size) {
380 size -= off;
381 count = size;
382 } else {
383 size = count;
384 }
385
4c0619ad
SS
386 if ((off & 1) && size) {
387 u8 val;
e04b0ea2 388 pci_user_read_config_byte(dev, off, &val);
4c0619ad 389 data[off - init_off] = val;
1da177e4 390 off++;
4c0619ad
SS
391 size--;
392 }
393
394 if ((off & 3) && size > 2) {
395 u16 val;
e04b0ea2 396 pci_user_read_config_word(dev, off, &val);
4c0619ad
SS
397 data[off - init_off] = val & 0xff;
398 data[off - init_off + 1] = (val >> 8) & 0xff;
399 off += 2;
400 size -= 2;
1da177e4
LT
401 }
402
403 while (size > 3) {
4c0619ad 404 u32 val;
e04b0ea2 405 pci_user_read_config_dword(dev, off, &val);
4c0619ad
SS
406 data[off - init_off] = val & 0xff;
407 data[off - init_off + 1] = (val >> 8) & 0xff;
408 data[off - init_off + 2] = (val >> 16) & 0xff;
409 data[off - init_off + 3] = (val >> 24) & 0xff;
1da177e4
LT
410 off += 4;
411 size -= 4;
412 }
413
4c0619ad
SS
414 if (size >= 2) {
415 u16 val;
e04b0ea2 416 pci_user_read_config_word(dev, off, &val);
4c0619ad
SS
417 data[off - init_off] = val & 0xff;
418 data[off - init_off + 1] = (val >> 8) & 0xff;
419 off += 2;
420 size -= 2;
421 }
422
423 if (size > 0) {
424 u8 val;
e04b0ea2 425 pci_user_read_config_byte(dev, off, &val);
4c0619ad 426 data[off - init_off] = val;
1da177e4
LT
427 off++;
428 --size;
429 }
430
431 return count;
432}
433
434static ssize_t
2c3c8bea
CW
435pci_write_config(struct file* filp, struct kobject *kobj,
436 struct bin_attribute *bin_attr,
91a69029 437 char *buf, loff_t off, size_t count)
1da177e4
LT
438{
439 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
440 unsigned int size = count;
441 loff_t init_off = off;
4c0619ad 442 u8 *data = (u8*) buf;
1da177e4
LT
443
444 if (off > dev->cfg_size)
445 return 0;
446 if (off + count > dev->cfg_size) {
447 size = dev->cfg_size - off;
448 count = size;
449 }
4c0619ad
SS
450
451 if ((off & 1) && size) {
e04b0ea2 452 pci_user_write_config_byte(dev, off, data[off - init_off]);
1da177e4 453 off++;
4c0619ad 454 size--;
1da177e4 455 }
4c0619ad
SS
456
457 if ((off & 3) && size > 2) {
458 u16 val = data[off - init_off];
459 val |= (u16) data[off - init_off + 1] << 8;
e04b0ea2 460 pci_user_write_config_word(dev, off, val);
4c0619ad
SS
461 off += 2;
462 size -= 2;
463 }
1da177e4
LT
464
465 while (size > 3) {
4c0619ad
SS
466 u32 val = data[off - init_off];
467 val |= (u32) data[off - init_off + 1] << 8;
468 val |= (u32) data[off - init_off + 2] << 16;
469 val |= (u32) data[off - init_off + 3] << 24;
e04b0ea2 470 pci_user_write_config_dword(dev, off, val);
1da177e4
LT
471 off += 4;
472 size -= 4;
473 }
4c0619ad
SS
474
475 if (size >= 2) {
476 u16 val = data[off - init_off];
477 val |= (u16) data[off - init_off + 1] << 8;
e04b0ea2 478 pci_user_write_config_word(dev, off, val);
4c0619ad
SS
479 off += 2;
480 size -= 2;
481 }
1da177e4 482
4c0619ad 483 if (size) {
e04b0ea2 484 pci_user_write_config_byte(dev, off, data[off - init_off]);
1da177e4
LT
485 off++;
486 --size;
487 }
488
489 return count;
490}
491
94e61088 492static ssize_t
2c3c8bea
CW
493read_vpd_attr(struct file *filp, struct kobject *kobj,
494 struct bin_attribute *bin_attr,
287d19ce 495 char *buf, loff_t off, size_t count)
94e61088
BH
496{
497 struct pci_dev *dev =
498 to_pci_dev(container_of(kobj, struct device, kobj));
94e61088
BH
499
500 if (off > bin_attr->size)
501 count = 0;
502 else if (count > bin_attr->size - off)
503 count = bin_attr->size - off;
94e61088 504
287d19ce 505 return pci_read_vpd(dev, off, count, buf);
94e61088
BH
506}
507
508static ssize_t
2c3c8bea
CW
509write_vpd_attr(struct file *filp, struct kobject *kobj,
510 struct bin_attribute *bin_attr,
287d19ce 511 char *buf, loff_t off, size_t count)
94e61088
BH
512{
513 struct pci_dev *dev =
514 to_pci_dev(container_of(kobj, struct device, kobj));
94e61088
BH
515
516 if (off > bin_attr->size)
517 count = 0;
518 else if (count > bin_attr->size - off)
519 count = bin_attr->size - off;
94e61088 520
287d19ce 521 return pci_write_vpd(dev, off, count, buf);
94e61088
BH
522}
523
1da177e4
LT
524#ifdef HAVE_PCI_LEGACY
525/**
526 * pci_read_legacy_io - read byte(s) from legacy I/O port space
2c3c8bea 527 * @filp: open sysfs file
1da177e4 528 * @kobj: kobject corresponding to file to read from
cffb2faf 529 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
530 * @buf: buffer to store results
531 * @off: offset into legacy I/O port space
532 * @count: number of bytes to read
533 *
534 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
535 * callback routine (pci_legacy_read).
536 */
f19aeb1f 537static ssize_t
2c3c8bea
CW
538pci_read_legacy_io(struct file *filp, struct kobject *kobj,
539 struct bin_attribute *bin_attr,
91a69029 540 char *buf, loff_t off, size_t count)
1da177e4
LT
541{
542 struct pci_bus *bus = to_pci_bus(container_of(kobj,
fd7d1ced 543 struct device,
1da177e4
LT
544 kobj));
545
546 /* Only support 1, 2 or 4 byte accesses */
547 if (count != 1 && count != 2 && count != 4)
548 return -EINVAL;
549
550 return pci_legacy_read(bus, off, (u32 *)buf, count);
551}
552
553/**
554 * pci_write_legacy_io - write byte(s) to legacy I/O port space
2c3c8bea 555 * @filp: open sysfs file
1da177e4 556 * @kobj: kobject corresponding to file to read from
cffb2faf 557 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
558 * @buf: buffer containing value to be written
559 * @off: offset into legacy I/O port space
560 * @count: number of bytes to write
561 *
562 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
563 * callback routine (pci_legacy_write).
564 */
f19aeb1f 565static ssize_t
2c3c8bea
CW
566pci_write_legacy_io(struct file *filp, struct kobject *kobj,
567 struct bin_attribute *bin_attr,
91a69029 568 char *buf, loff_t off, size_t count)
1da177e4
LT
569{
570 struct pci_bus *bus = to_pci_bus(container_of(kobj,
fd7d1ced 571 struct device,
1da177e4
LT
572 kobj));
573 /* Only support 1, 2 or 4 byte accesses */
574 if (count != 1 && count != 2 && count != 4)
575 return -EINVAL;
576
577 return pci_legacy_write(bus, off, *(u32 *)buf, count);
578}
579
580/**
581 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
2c3c8bea 582 * @filp: open sysfs file
1da177e4
LT
583 * @kobj: kobject corresponding to device to be mapped
584 * @attr: struct bin_attribute for this file
585 * @vma: struct vm_area_struct passed to mmap
586 *
f19aeb1f 587 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
1da177e4
LT
588 * legacy memory space (first meg of bus space) into application virtual
589 * memory space.
590 */
f19aeb1f 591static int
2c3c8bea
CW
592pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
593 struct bin_attribute *attr,
1da177e4
LT
594 struct vm_area_struct *vma)
595{
596 struct pci_bus *bus = to_pci_bus(container_of(kobj,
fd7d1ced 597 struct device,
1da177e4
LT
598 kobj));
599
f19aeb1f
BH
600 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
601}
602
603/**
604 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
2c3c8bea 605 * @filp: open sysfs file
f19aeb1f
BH
606 * @kobj: kobject corresponding to device to be mapped
607 * @attr: struct bin_attribute for this file
608 * @vma: struct vm_area_struct passed to mmap
609 *
610 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
611 * legacy IO space (first meg of bus space) into application virtual
612 * memory space. Returns -ENOSYS if the operation isn't supported
613 */
614static int
2c3c8bea
CW
615pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
616 struct bin_attribute *attr,
f19aeb1f
BH
617 struct vm_area_struct *vma)
618{
619 struct pci_bus *bus = to_pci_bus(container_of(kobj,
620 struct device,
621 kobj));
622
623 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
624}
625
10a0ef39
IK
626/**
627 * pci_adjust_legacy_attr - adjustment of legacy file attributes
628 * @b: bus to create files under
629 * @mmap_type: I/O port or memory
630 *
631 * Stub implementation. Can be overridden by arch if necessary.
632 */
633void __weak
634pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
635{
636 return;
637}
638
f19aeb1f
BH
639/**
640 * pci_create_legacy_files - create legacy I/O port and memory files
641 * @b: bus to create files under
642 *
643 * Some platforms allow access to legacy I/O port and ISA memory space on
644 * a per-bus basis. This routine creates the files and ties them into
645 * their associated read, write and mmap files from pci-sysfs.c
646 *
647 * On error unwind, but don't propogate the error to the caller
648 * as it is ok to set up the PCI bus without these files.
649 */
650void pci_create_legacy_files(struct pci_bus *b)
651{
652 int error;
653
654 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
655 GFP_ATOMIC);
656 if (!b->legacy_io)
657 goto kzalloc_err;
658
62e877b8 659 sysfs_bin_attr_init(b->legacy_io);
f19aeb1f
BH
660 b->legacy_io->attr.name = "legacy_io";
661 b->legacy_io->size = 0xffff;
662 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
663 b->legacy_io->read = pci_read_legacy_io;
664 b->legacy_io->write = pci_write_legacy_io;
665 b->legacy_io->mmap = pci_mmap_legacy_io;
10a0ef39 666 pci_adjust_legacy_attr(b, pci_mmap_io);
f19aeb1f
BH
667 error = device_create_bin_file(&b->dev, b->legacy_io);
668 if (error)
669 goto legacy_io_err;
670
671 /* Allocated above after the legacy_io struct */
672 b->legacy_mem = b->legacy_io + 1;
6757eca3 673 sysfs_bin_attr_init(b->legacy_mem);
f19aeb1f
BH
674 b->legacy_mem->attr.name = "legacy_mem";
675 b->legacy_mem->size = 1024*1024;
676 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
677 b->legacy_mem->mmap = pci_mmap_legacy_mem;
10a0ef39 678 pci_adjust_legacy_attr(b, pci_mmap_mem);
f19aeb1f
BH
679 error = device_create_bin_file(&b->dev, b->legacy_mem);
680 if (error)
681 goto legacy_mem_err;
682
683 return;
684
685legacy_mem_err:
686 device_remove_bin_file(&b->dev, b->legacy_io);
687legacy_io_err:
688 kfree(b->legacy_io);
689 b->legacy_io = NULL;
690kzalloc_err:
691 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
692 "and ISA memory resources to sysfs\n");
693 return;
694}
695
696void pci_remove_legacy_files(struct pci_bus *b)
697{
698 if (b->legacy_io) {
699 device_remove_bin_file(&b->dev, b->legacy_io);
700 device_remove_bin_file(&b->dev, b->legacy_mem);
701 kfree(b->legacy_io); /* both are allocated here */
702 }
1da177e4
LT
703}
704#endif /* HAVE_PCI_LEGACY */
705
706#ifdef HAVE_PCI_MMAP
b5ff7df3 707
3b519e4e
MW
708int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
709 enum pci_mmap_api mmap_api)
b5ff7df3 710{
3b519e4e 711 unsigned long nr, start, size, pci_start;
b5ff7df3 712
3b519e4e
MW
713 if (pci_resource_len(pdev, resno) == 0)
714 return 0;
b5ff7df3
LT
715 nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
716 start = vma->vm_pgoff;
88e7df0b 717 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
3b519e4e
MW
718 pci_start = (mmap_api == PCI_MMAP_SYSFS) ?
719 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
720 if (start >= pci_start && start < pci_start + size &&
721 start + nr <= pci_start + size)
b5ff7df3 722 return 1;
b5ff7df3
LT
723 return 0;
724}
725
1da177e4
LT
726/**
727 * pci_mmap_resource - map a PCI resource into user memory space
728 * @kobj: kobject for mapping
729 * @attr: struct bin_attribute for the file being mapped
730 * @vma: struct vm_area_struct passed into the mmap
45aec1ae 731 * @write_combine: 1 for write_combine mapping
1da177e4
LT
732 *
733 * Use the regular PCI mapping routines to map a PCI resource into userspace.
1da177e4
LT
734 */
735static int
736pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
45aec1ae 737 struct vm_area_struct *vma, int write_combine)
1da177e4
LT
738{
739 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
740 struct device, kobj));
a3f5835a 741 struct resource *res = attr->private;
1da177e4 742 enum pci_mmap_state mmap_type;
e31dd6e4 743 resource_size_t start, end;
2311b1f2 744 int i;
1da177e4 745
2311b1f2
ME
746 for (i = 0; i < PCI_ROM_RESOURCE; i++)
747 if (res == &pdev->resource[i])
748 break;
749 if (i >= PCI_ROM_RESOURCE)
750 return -ENODEV;
751
3b519e4e
MW
752 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
753 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
754 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
755 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
756 pci_name(pdev), i,
757 pci_resource_start(pdev, i), pci_resource_len(pdev, i));
b5ff7df3 758 return -EINVAL;
3b519e4e 759 }
b5ff7df3 760
2311b1f2
ME
761 /* pci_mmap_page_range() expects the same kind of entry as coming
762 * from /proc/bus/pci/ which is a "user visible" value. If this is
763 * different from the resource itself, arch will do necessary fixup.
764 */
765 pci_resource_to_user(pdev, i, res, &start, &end);
766 vma->vm_pgoff += start >> PAGE_SHIFT;
1da177e4
LT
767 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
768
e8de1481
AV
769 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
770 return -EINVAL;
771
45aec1ae 772 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
773}
774
775static int
2c3c8bea
CW
776pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
777 struct bin_attribute *attr,
45aec1ae 778 struct vm_area_struct *vma)
779{
780 return pci_mmap_resource(kobj, attr, vma, 0);
781}
782
783static int
2c3c8bea
CW
784pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
785 struct bin_attribute *attr,
45aec1ae 786 struct vm_area_struct *vma)
787{
788 return pci_mmap_resource(kobj, attr, vma, 1);
1da177e4
LT
789}
790
8633328b
AW
791static ssize_t
792pci_resource_io(struct file *filp, struct kobject *kobj,
793 struct bin_attribute *attr, char *buf,
794 loff_t off, size_t count, bool write)
795{
796 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
797 struct device, kobj));
798 struct resource *res = attr->private;
799 unsigned long port = off;
800 int i;
801
802 for (i = 0; i < PCI_ROM_RESOURCE; i++)
803 if (res == &pdev->resource[i])
804 break;
805 if (i >= PCI_ROM_RESOURCE)
806 return -ENODEV;
807
808 port += pci_resource_start(pdev, i);
809
810 if (port > pci_resource_end(pdev, i))
811 return 0;
812
813 if (port + count - 1 > pci_resource_end(pdev, i))
814 return -EINVAL;
815
816 switch (count) {
817 case 1:
818 if (write)
819 outb(*(u8 *)buf, port);
820 else
821 *(u8 *)buf = inb(port);
822 return 1;
823 case 2:
824 if (write)
825 outw(*(u16 *)buf, port);
826 else
827 *(u16 *)buf = inw(port);
828 return 2;
829 case 4:
830 if (write)
831 outl(*(u32 *)buf, port);
832 else
833 *(u32 *)buf = inl(port);
834 return 4;
835 }
836 return -EINVAL;
837}
838
839static ssize_t
840pci_read_resource_io(struct file *filp, struct kobject *kobj,
841 struct bin_attribute *attr, char *buf,
842 loff_t off, size_t count)
843{
844 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
845}
846
847static ssize_t
848pci_write_resource_io(struct file *filp, struct kobject *kobj,
849 struct bin_attribute *attr, char *buf,
850 loff_t off, size_t count)
851{
852 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
853}
854
b19441af
GKH
855/**
856 * pci_remove_resource_files - cleanup resource files
cffb2faf 857 * @pdev: dev to cleanup
b19441af 858 *
cffb2faf 859 * If we created resource files for @pdev, remove them from sysfs and
b19441af
GKH
860 * free their resources.
861 */
862static void
863pci_remove_resource_files(struct pci_dev *pdev)
864{
865 int i;
866
867 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
868 struct bin_attribute *res_attr;
869
870 res_attr = pdev->res_attr[i];
871 if (res_attr) {
872 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
873 kfree(res_attr);
874 }
45aec1ae 875
876 res_attr = pdev->res_attr_wc[i];
877 if (res_attr) {
878 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
879 kfree(res_attr);
880 }
b19441af
GKH
881 }
882}
883
45aec1ae 884static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
885{
886 /* allocate attribute structure, piggyback attribute name */
887 int name_len = write_combine ? 13 : 10;
888 struct bin_attribute *res_attr;
889 int retval;
890
891 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
892 if (res_attr) {
893 char *res_attr_name = (char *)(res_attr + 1);
894
a07e4156 895 sysfs_bin_attr_init(res_attr);
45aec1ae 896 if (write_combine) {
897 pdev->res_attr_wc[num] = res_attr;
898 sprintf(res_attr_name, "resource%d_wc", num);
899 res_attr->mmap = pci_mmap_resource_wc;
900 } else {
901 pdev->res_attr[num] = res_attr;
902 sprintf(res_attr_name, "resource%d", num);
903 res_attr->mmap = pci_mmap_resource_uc;
904 }
8633328b
AW
905 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
906 res_attr->read = pci_read_resource_io;
907 res_attr->write = pci_write_resource_io;
908 }
45aec1ae 909 res_attr->attr.name = res_attr_name;
910 res_attr->attr.mode = S_IRUSR | S_IWUSR;
911 res_attr->size = pci_resource_len(pdev, num);
912 res_attr->private = &pdev->resource[num];
913 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
914 } else
915 retval = -ENOMEM;
916
917 return retval;
918}
919
1da177e4
LT
920/**
921 * pci_create_resource_files - create resource files in sysfs for @dev
cffb2faf 922 * @pdev: dev in question
1da177e4 923 *
cffb2faf 924 * Walk the resources in @pdev creating files for each resource available.
1da177e4 925 */
b19441af 926static int pci_create_resource_files(struct pci_dev *pdev)
1da177e4
LT
927{
928 int i;
b19441af 929 int retval;
1da177e4
LT
930
931 /* Expose the PCI resources from this device as files */
932 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1da177e4
LT
933
934 /* skip empty resources */
935 if (!pci_resource_len(pdev, i))
936 continue;
937
45aec1ae 938 retval = pci_create_attr(pdev, i, 0);
939 /* for prefetchable resources, create a WC mappable file */
940 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
941 retval = pci_create_attr(pdev, i, 1);
942
943 if (retval) {
944 pci_remove_resource_files(pdev);
945 return retval;
1da177e4
LT
946 }
947 }
b19441af 948 return 0;
1da177e4
LT
949}
950#else /* !HAVE_PCI_MMAP */
10a0ef39
IK
951int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
952void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1da177e4
LT
953#endif /* HAVE_PCI_MMAP */
954
955/**
956 * pci_write_rom - used to enable access to the PCI ROM display
2c3c8bea 957 * @filp: sysfs file
1da177e4 958 * @kobj: kernel object handle
cffb2faf 959 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
960 * @buf: user input
961 * @off: file offset
962 * @count: number of byte in input
963 *
964 * writing anything except 0 enables it
965 */
966static ssize_t
2c3c8bea
CW
967pci_write_rom(struct file *filp, struct kobject *kobj,
968 struct bin_attribute *bin_attr,
91a69029 969 char *buf, loff_t off, size_t count)
1da177e4
LT
970{
971 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
972
973 if ((off == 0) && (*buf == '0') && (count == 2))
974 pdev->rom_attr_enabled = 0;
975 else
976 pdev->rom_attr_enabled = 1;
977
978 return count;
979}
980
981/**
982 * pci_read_rom - read a PCI ROM
2c3c8bea 983 * @filp: sysfs file
1da177e4 984 * @kobj: kernel object handle
cffb2faf 985 * @bin_attr: struct bin_attribute for this file
1da177e4
LT
986 * @buf: where to put the data we read from the ROM
987 * @off: file offset
988 * @count: number of bytes to read
989 *
990 * Put @count bytes starting at @off into @buf from the ROM in the PCI
991 * device corresponding to @kobj.
992 */
993static ssize_t
2c3c8bea
CW
994pci_read_rom(struct file *filp, struct kobject *kobj,
995 struct bin_attribute *bin_attr,
91a69029 996 char *buf, loff_t off, size_t count)
1da177e4
LT
997{
998 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
999 void __iomem *rom;
1000 size_t size;
1001
1002 if (!pdev->rom_attr_enabled)
1003 return -EINVAL;
1004
1005 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
97c44836
TN
1006 if (!rom || !size)
1007 return -EIO;
1da177e4
LT
1008
1009 if (off >= size)
1010 count = 0;
1011 else {
1012 if (off + count > size)
1013 count = size - off;
1014
1015 memcpy_fromio(buf, rom + off, count);
1016 }
1017 pci_unmap_rom(pdev, rom);
1018
1019 return count;
1020}
1021
1022static struct bin_attribute pci_config_attr = {
1023 .attr = {
1024 .name = "config",
1025 .mode = S_IRUGO | S_IWUSR,
1da177e4 1026 },
557848c3 1027 .size = PCI_CFG_SPACE_SIZE,
1da177e4
LT
1028 .read = pci_read_config,
1029 .write = pci_write_config,
1030};
1031
1032static struct bin_attribute pcie_config_attr = {
1033 .attr = {
1034 .name = "config",
1035 .mode = S_IRUGO | S_IWUSR,
1da177e4 1036 },
557848c3 1037 .size = PCI_CFG_SPACE_EXP_SIZE,
1da177e4
LT
1038 .read = pci_read_config,
1039 .write = pci_write_config,
1040};
1041
a2cd52ca 1042int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)
575e3348 1043{
a2cd52ca 1044 return 0;
575e3348
ME
1045}
1046
711d5779
MT
1047static ssize_t reset_store(struct device *dev,
1048 struct device_attribute *attr, const char *buf,
1049 size_t count)
1050{
1051 struct pci_dev *pdev = to_pci_dev(dev);
1052 unsigned long val;
1053 ssize_t result = strict_strtoul(buf, 0, &val);
1054
1055 if (result < 0)
1056 return result;
1057
1058 if (val != 1)
1059 return -EINVAL;
447c5dd7
MS
1060
1061 result = pci_reset_function(pdev);
1062 if (result < 0)
1063 return result;
1064
1065 return count;
711d5779
MT
1066}
1067
1068static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1069
280c73d3
ZY
1070static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1071{
1072 int retval;
1073 struct bin_attribute *attr;
1074
1075 /* If the device has VPD, try to expose it in sysfs. */
1076 if (dev->vpd) {
1077 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1078 if (!attr)
1079 return -ENOMEM;
1080
a07e4156 1081 sysfs_bin_attr_init(attr);
280c73d3
ZY
1082 attr->size = dev->vpd->len;
1083 attr->attr.name = "vpd";
1084 attr->attr.mode = S_IRUSR | S_IWUSR;
287d19ce
SH
1085 attr->read = read_vpd_attr;
1086 attr->write = write_vpd_attr;
280c73d3
ZY
1087 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1088 if (retval) {
1089 kfree(dev->vpd->attr);
1090 return retval;
1091 }
1092 dev->vpd->attr = attr;
1093 }
1094
1095 /* Active State Power Management */
1096 pcie_aspm_create_sysfs_dev_files(dev);
1097
711d5779
MT
1098 if (!pci_probe_reset_function(dev)) {
1099 retval = device_create_file(&dev->dev, &reset_attr);
1100 if (retval)
1101 goto error;
1102 dev->reset_fn = 1;
1103 }
280c73d3 1104 return 0;
711d5779
MT
1105
1106error:
1107 pcie_aspm_remove_sysfs_dev_files(dev);
1108 if (dev->vpd && dev->vpd->attr) {
1109 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1110 kfree(dev->vpd->attr);
1111 }
1112
1113 return retval;
280c73d3
ZY
1114}
1115
b19441af 1116int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1da177e4 1117{
b19441af 1118 int retval;
280c73d3
ZY
1119 int rom_size = 0;
1120 struct bin_attribute *attr;
b19441af 1121
1da177e4
LT
1122 if (!sysfs_initialized)
1123 return -EACCES;
1124
557848c3 1125 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
b19441af 1126 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1da177e4 1127 else
b19441af
GKH
1128 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1129 if (retval)
1130 goto err;
1da177e4 1131
b19441af
GKH
1132 retval = pci_create_resource_files(pdev);
1133 if (retval)
280c73d3
ZY
1134 goto err_config_file;
1135
1136 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1137 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1138 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1139 rom_size = 0x20000;
1da177e4
LT
1140
1141 /* If the device has a ROM, try to expose it in sysfs. */
280c73d3 1142 if (rom_size) {
94e61088 1143 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
280c73d3 1144 if (!attr) {
b19441af 1145 retval = -ENOMEM;
9890b12a 1146 goto err_resource_files;
1da177e4 1147 }
a07e4156 1148 sysfs_bin_attr_init(attr);
280c73d3
ZY
1149 attr->size = rom_size;
1150 attr->attr.name = "rom";
1151 attr->attr.mode = S_IRUSR;
1152 attr->read = pci_read_rom;
1153 attr->write = pci_write_rom;
1154 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1155 if (retval) {
1156 kfree(attr);
1157 goto err_resource_files;
1158 }
1159 pdev->rom_attr = attr;
1da177e4 1160 }
280c73d3 1161
217f45de
DA
1162 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1163 retval = device_create_file(&pdev->dev, &vga_attr);
1164 if (retval)
1165 goto err_rom_file;
1166 }
1167
1da177e4 1168 /* add platform-specific attributes */
280c73d3
ZY
1169 retval = pcibios_add_platform_entries(pdev);
1170 if (retval)
217f45de 1171 goto err_vga_file;
b19441af 1172
280c73d3
ZY
1173 /* add sysfs entries for various capabilities */
1174 retval = pci_create_capabilities_sysfs(pdev);
1175 if (retval)
217f45de 1176 goto err_vga_file;
7d715a6c 1177
911e1c9b
N
1178 pci_create_firmware_label_files(pdev);
1179
1da177e4 1180 return 0;
b19441af 1181
217f45de
DA
1182err_vga_file:
1183 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1184 device_remove_file(&pdev->dev, &vga_attr);
a2cd52ca 1185err_rom_file:
280c73d3 1186 if (rom_size) {
94e61088 1187 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
280c73d3
ZY
1188 kfree(pdev->rom_attr);
1189 pdev->rom_attr = NULL;
1190 }
9890b12a
ME
1191err_resource_files:
1192 pci_remove_resource_files(pdev);
94e61088 1193err_config_file:
557848c3 1194 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
b19441af
GKH
1195 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1196 else
1197 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1198err:
1199 return retval;
1da177e4
LT
1200}
1201
280c73d3
ZY
1202static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1203{
1204 if (dev->vpd && dev->vpd->attr) {
1205 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1206 kfree(dev->vpd->attr);
1207 }
1208
1209 pcie_aspm_remove_sysfs_dev_files(dev);
711d5779
MT
1210 if (dev->reset_fn) {
1211 device_remove_file(&dev->dev, &reset_attr);
1212 dev->reset_fn = 0;
1213 }
280c73d3
ZY
1214}
1215
1da177e4
LT
1216/**
1217 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1218 * @pdev: device whose entries we should free
1219 *
1220 * Cleanup when @pdev is removed from sysfs.
1221 */
1222void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1223{
280c73d3
ZY
1224 int rom_size = 0;
1225
d67afe5e
DM
1226 if (!sysfs_initialized)
1227 return;
1228
280c73d3 1229 pci_remove_capabilities_sysfs(pdev);
7d715a6c 1230
557848c3 1231 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
1da177e4
LT
1232 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1233 else
1234 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1235
1236 pci_remove_resource_files(pdev);
1237
280c73d3
ZY
1238 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1239 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1240 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1241 rom_size = 0x20000;
1242
1243 if (rom_size && pdev->rom_attr) {
1244 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1245 kfree(pdev->rom_attr);
1da177e4 1246 }
911e1c9b
N
1247
1248 pci_remove_firmware_label_files(pdev);
1249
1da177e4
LT
1250}
1251
1252static int __init pci_sysfs_init(void)
1253{
1254 struct pci_dev *pdev = NULL;
b19441af
GKH
1255 int retval;
1256
1da177e4 1257 sysfs_initialized = 1;
b19441af
GKH
1258 for_each_pci_dev(pdev) {
1259 retval = pci_create_sysfs_dev_files(pdev);
151fc5df
JL
1260 if (retval) {
1261 pci_dev_put(pdev);
b19441af 1262 return retval;
151fc5df 1263 }
b19441af 1264 }
1da177e4
LT
1265
1266 return 0;
1267}
1268
40ee9e9f 1269late_initcall(pci_sysfs_init);