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