]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * drivers/pci/pci-sysfs.c | |
4 | * | |
5 | * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com> | |
6 | * (C) Copyright 2002-2004 IBM Corp. | |
7 | * (C) Copyright 2003 Matthew Wilcox | |
8 | * (C) Copyright 2003 Hewlett-Packard | |
9 | * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> | |
10 | * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> | |
11 | * | |
12 | * File attributes for PCI devices | |
13 | * | |
f7625980 | 14 | * Modeled after usb's driverfs.c |
1da177e4 LT |
15 | * |
16 | */ | |
17 | ||
18 | ||
1da177e4 | 19 | #include <linux/kernel.h> |
b5ff7df3 | 20 | #include <linux/sched.h> |
1da177e4 LT |
21 | #include <linux/pci.h> |
22 | #include <linux/stat.h> | |
363c75db | 23 | #include <linux/export.h> |
1da177e4 LT |
24 | #include <linux/topology.h> |
25 | #include <linux/mm.h> | |
de139a33 | 26 | #include <linux/fs.h> |
aa0ac365 | 27 | #include <linux/capability.h> |
a628e7b8 | 28 | #include <linux/security.h> |
7d715a6c | 29 | #include <linux/pci-aspm.h> |
5a0e3ad6 | 30 | #include <linux/slab.h> |
1a39b310 | 31 | #include <linux/vgaarb.h> |
448bd857 | 32 | #include <linux/pm_runtime.h> |
dfc73e7a | 33 | #include <linux/of.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 | |
56c1af46 WVK |
158 | static ssize_t max_link_speed_show(struct device *dev, |
159 | struct device_attribute *attr, char *buf) | |
160 | { | |
161 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
162 | u32 linkcap; | |
163 | int err; | |
164 | const char *speed; | |
165 | ||
166 | err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap); | |
167 | if (err) | |
168 | return -EINVAL; | |
169 | ||
170 | switch (linkcap & PCI_EXP_LNKCAP_SLS) { | |
ac924662 | 171 | case PCI_EXP_LNKCAP_SLS_16_0GB: |
172 | speed = "16 GT/s"; | |
173 | break; | |
56c1af46 WVK |
174 | case PCI_EXP_LNKCAP_SLS_8_0GB: |
175 | speed = "8 GT/s"; | |
176 | break; | |
177 | case PCI_EXP_LNKCAP_SLS_5_0GB: | |
178 | speed = "5 GT/s"; | |
179 | break; | |
180 | case PCI_EXP_LNKCAP_SLS_2_5GB: | |
181 | speed = "2.5 GT/s"; | |
182 | break; | |
183 | default: | |
184 | speed = "Unknown speed"; | |
185 | } | |
186 | ||
187 | return sprintf(buf, "%s\n", speed); | |
188 | } | |
189 | static DEVICE_ATTR_RO(max_link_speed); | |
190 | ||
191 | static ssize_t max_link_width_show(struct device *dev, | |
192 | struct device_attribute *attr, char *buf) | |
193 | { | |
194 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
195 | u32 linkcap; | |
196 | int err; | |
197 | ||
198 | err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap); | |
199 | if (err) | |
200 | return -EINVAL; | |
201 | ||
202 | return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4); | |
203 | } | |
204 | static DEVICE_ATTR_RO(max_link_width); | |
205 | ||
206 | static ssize_t current_link_speed_show(struct device *dev, | |
207 | struct device_attribute *attr, char *buf) | |
208 | { | |
209 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
210 | u16 linkstat; | |
211 | int err; | |
212 | const char *speed; | |
213 | ||
214 | err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); | |
215 | if (err) | |
216 | return -EINVAL; | |
217 | ||
218 | switch (linkstat & PCI_EXP_LNKSTA_CLS) { | |
ac924662 | 219 | case PCI_EXP_LNKSTA_CLS_16_0GB: |
220 | speed = "16 GT/s"; | |
221 | break; | |
56c1af46 WVK |
222 | case PCI_EXP_LNKSTA_CLS_8_0GB: |
223 | speed = "8 GT/s"; | |
224 | break; | |
225 | case PCI_EXP_LNKSTA_CLS_5_0GB: | |
226 | speed = "5 GT/s"; | |
227 | break; | |
228 | case PCI_EXP_LNKSTA_CLS_2_5GB: | |
229 | speed = "2.5 GT/s"; | |
230 | break; | |
231 | default: | |
232 | speed = "Unknown speed"; | |
233 | } | |
234 | ||
235 | return sprintf(buf, "%s\n", speed); | |
236 | } | |
237 | static DEVICE_ATTR_RO(current_link_speed); | |
238 | ||
239 | static ssize_t current_link_width_show(struct device *dev, | |
240 | struct device_attribute *attr, char *buf) | |
241 | { | |
242 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
243 | u16 linkstat; | |
244 | int err; | |
245 | ||
246 | err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); | |
247 | if (err) | |
248 | return -EINVAL; | |
249 | ||
250 | return sprintf(buf, "%u\n", | |
251 | (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT); | |
252 | } | |
253 | static DEVICE_ATTR_RO(current_link_width); | |
254 | ||
255 | static ssize_t secondary_bus_number_show(struct device *dev, | |
256 | struct device_attribute *attr, | |
257 | char *buf) | |
258 | { | |
259 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
260 | u8 sec_bus; | |
261 | int err; | |
262 | ||
263 | err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus); | |
264 | if (err) | |
265 | return -EINVAL; | |
266 | ||
267 | return sprintf(buf, "%u\n", sec_bus); | |
268 | } | |
269 | static DEVICE_ATTR_RO(secondary_bus_number); | |
270 | ||
271 | static ssize_t subordinate_bus_number_show(struct device *dev, | |
272 | struct device_attribute *attr, | |
273 | char *buf) | |
274 | { | |
275 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
276 | u8 sub_bus; | |
277 | int err; | |
278 | ||
279 | err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus); | |
280 | if (err) | |
281 | return -EINVAL; | |
282 | ||
283 | return sprintf(buf, "%u\n", sub_bus); | |
284 | } | |
285 | static DEVICE_ATTR_RO(subordinate_bus_number); | |
286 | ||
3c78bc61 RD |
287 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
288 | char *buf) | |
9888549e GKH |
289 | { |
290 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
291 | ||
89ec3dcf | 292 | return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", |
9888549e GKH |
293 | pci_dev->vendor, pci_dev->device, |
294 | pci_dev->subsystem_vendor, pci_dev->subsystem_device, | |
295 | (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), | |
296 | (u8)(pci_dev->class)); | |
297 | } | |
5136b2da | 298 | static DEVICE_ATTR_RO(modalias); |
bae94d02 | 299 | |
d8e7d53a | 300 | static ssize_t enable_store(struct device *dev, struct device_attribute *attr, |
3c78bc61 | 301 | const char *buf, size_t count) |
9f125d30 AV |
302 | { |
303 | struct pci_dev *pdev = to_pci_dev(dev); | |
92425a40 | 304 | unsigned long val; |
9a994e8e | 305 | ssize_t result = kstrtoul(buf, 0, &val); |
92425a40 TP |
306 | |
307 | if (result < 0) | |
308 | return result; | |
9f125d30 AV |
309 | |
310 | /* this can crash the machine when done on the "wrong" device */ | |
311 | if (!capable(CAP_SYS_ADMIN)) | |
92425a40 | 312 | return -EPERM; |
9f125d30 | 313 | |
bec1d660 CH |
314 | device_lock(dev); |
315 | if (dev->driver) | |
316 | result = -EBUSY; | |
317 | else if (val) | |
bae94d02 | 318 | result = pci_enable_device(pdev); |
bec1d660 CH |
319 | else if (pci_is_enabled(pdev)) |
320 | pci_disable_device(pdev); | |
321 | else | |
322 | result = -EIO; | |
323 | device_unlock(dev); | |
9f125d30 | 324 | |
bae94d02 IPG |
325 | return result < 0 ? result : count; |
326 | } | |
327 | ||
d8e7d53a | 328 | static ssize_t enable_show(struct device *dev, struct device_attribute *attr, |
3c78bc61 | 329 | char *buf) |
bae94d02 IPG |
330 | { |
331 | struct pci_dev *pdev; | |
9f125d30 | 332 | |
3c78bc61 RD |
333 | pdev = to_pci_dev(dev); |
334 | return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt)); | |
9f125d30 | 335 | } |
d8e7d53a | 336 | static DEVICE_ATTR_RW(enable); |
9f125d30 | 337 | |
81bb0e19 | 338 | #ifdef CONFIG_NUMA |
63692df1 PB |
339 | static ssize_t numa_node_store(struct device *dev, |
340 | struct device_attribute *attr, const char *buf, | |
341 | size_t count) | |
342 | { | |
343 | struct pci_dev *pdev = to_pci_dev(dev); | |
344 | int node, ret; | |
345 | ||
346 | if (!capable(CAP_SYS_ADMIN)) | |
347 | return -EPERM; | |
348 | ||
349 | ret = kstrtoint(buf, 0, &node); | |
350 | if (ret) | |
351 | return ret; | |
352 | ||
3dcc8d39 MK |
353 | if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) |
354 | return -EINVAL; | |
355 | ||
356 | if (node != NUMA_NO_NODE && !node_online(node)) | |
63692df1 PB |
357 | return -EINVAL; |
358 | ||
359 | add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); | |
360 | dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", | |
361 | node); | |
362 | ||
363 | dev->numa_node = node; | |
364 | return count; | |
365 | } | |
366 | ||
3c78bc61 RD |
367 | static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, |
368 | char *buf) | |
81bb0e19 | 369 | { |
3c78bc61 | 370 | return sprintf(buf, "%d\n", dev->numa_node); |
81bb0e19 | 371 | } |
63692df1 | 372 | static DEVICE_ATTR_RW(numa_node); |
81bb0e19 BG |
373 | #endif |
374 | ||
3c78bc61 RD |
375 | static ssize_t dma_mask_bits_show(struct device *dev, |
376 | struct device_attribute *attr, char *buf) | |
bb965401 YL |
377 | { |
378 | struct pci_dev *pdev = to_pci_dev(dev); | |
379 | ||
3c78bc61 | 380 | return sprintf(buf, "%d\n", fls64(pdev->dma_mask)); |
bb965401 | 381 | } |
5136b2da | 382 | static DEVICE_ATTR_RO(dma_mask_bits); |
bb965401 | 383 | |
3c78bc61 RD |
384 | static ssize_t consistent_dma_mask_bits_show(struct device *dev, |
385 | struct device_attribute *attr, | |
386 | char *buf) | |
bb965401 | 387 | { |
3c78bc61 | 388 | return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask)); |
bb965401 | 389 | } |
5136b2da | 390 | static DEVICE_ATTR_RO(consistent_dma_mask_bits); |
bb965401 | 391 | |
3c78bc61 RD |
392 | static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, |
393 | char *buf) | |
fe97064c BG |
394 | { |
395 | struct pci_dev *pdev = to_pci_dev(dev); | |
468ff15a | 396 | struct pci_bus *subordinate = pdev->subordinate; |
fe97064c | 397 | |
468ff15a YW |
398 | return sprintf(buf, "%u\n", subordinate ? |
399 | !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) | |
400 | : !pdev->no_msi); | |
fe97064c BG |
401 | } |
402 | ||
3c78bc61 RD |
403 | static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, |
404 | const char *buf, size_t count) | |
fe97064c BG |
405 | { |
406 | struct pci_dev *pdev = to_pci_dev(dev); | |
468ff15a | 407 | struct pci_bus *subordinate = pdev->subordinate; |
92425a40 TP |
408 | unsigned long val; |
409 | ||
9a994e8e | 410 | if (kstrtoul(buf, 0, &val) < 0) |
92425a40 | 411 | return -EINVAL; |
fe97064c | 412 | |
fe97064c | 413 | if (!capable(CAP_SYS_ADMIN)) |
92425a40 | 414 | return -EPERM; |
fe97064c | 415 | |
f7625980 | 416 | /* |
468ff15a YW |
417 | * "no_msi" and "bus_flags" only affect what happens when a driver |
418 | * requests MSI or MSI-X. They don't affect any drivers that have | |
419 | * already requested MSI or MSI-X. | |
f7625980 | 420 | */ |
468ff15a YW |
421 | if (!subordinate) { |
422 | pdev->no_msi = !val; | |
423 | dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n", | |
424 | val ? "allowed" : "disallowed"); | |
fe97064c | 425 | return count; |
fe97064c BG |
426 | } |
427 | ||
468ff15a YW |
428 | if (val) |
429 | subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; | |
430 | else | |
431 | subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; | |
432 | ||
433 | dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", | |
434 | val ? "allowed" : "disallowed"); | |
fe97064c BG |
435 | return count; |
436 | } | |
5136b2da | 437 | static DEVICE_ATTR_RW(msi_bus); |
9888549e | 438 | |
705b1aaa AC |
439 | static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, |
440 | size_t count) | |
441 | { | |
442 | unsigned long val; | |
443 | struct pci_bus *b = NULL; | |
444 | ||
9a994e8e | 445 | if (kstrtoul(buf, 0, &val) < 0) |
705b1aaa AC |
446 | return -EINVAL; |
447 | ||
448 | if (val) { | |
9d16947b | 449 | pci_lock_rescan_remove(); |
705b1aaa AC |
450 | while ((b = pci_find_next_bus(b)) != NULL) |
451 | pci_rescan_bus(b); | |
9d16947b | 452 | pci_unlock_rescan_remove(); |
705b1aaa AC |
453 | } |
454 | return count; | |
455 | } | |
0f49ba55 | 456 | static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store); |
705b1aaa | 457 | |
bf22c90f | 458 | static struct attribute *pci_bus_attrs[] = { |
0f49ba55 GKH |
459 | &bus_attr_rescan.attr, |
460 | NULL, | |
461 | }; | |
462 | ||
463 | static const struct attribute_group pci_bus_group = { | |
464 | .attrs = pci_bus_attrs, | |
465 | }; | |
466 | ||
467 | const struct attribute_group *pci_bus_groups[] = { | |
468 | &pci_bus_group, | |
469 | NULL, | |
705b1aaa | 470 | }; |
77c27c7b | 471 | |
3c78bc61 RD |
472 | static ssize_t dev_rescan_store(struct device *dev, |
473 | struct device_attribute *attr, const char *buf, | |
474 | size_t count) | |
738a6396 AC |
475 | { |
476 | unsigned long val; | |
477 | struct pci_dev *pdev = to_pci_dev(dev); | |
478 | ||
9a994e8e | 479 | if (kstrtoul(buf, 0, &val) < 0) |
738a6396 AC |
480 | return -EINVAL; |
481 | ||
482 | if (val) { | |
9d16947b | 483 | pci_lock_rescan_remove(); |
738a6396 | 484 | pci_rescan_bus(pdev->bus); |
9d16947b | 485 | pci_unlock_rescan_remove(); |
738a6396 AC |
486 | } |
487 | return count; | |
488 | } | |
bf22c90f SK |
489 | static struct device_attribute dev_rescan_attr = __ATTR(rescan, |
490 | (S_IWUSR|S_IWGRP), | |
491 | NULL, dev_rescan_store); | |
738a6396 | 492 | |
3c78bc61 RD |
493 | static ssize_t remove_store(struct device *dev, struct device_attribute *attr, |
494 | const char *buf, size_t count) | |
77c27c7b | 495 | { |
77c27c7b | 496 | unsigned long val; |
77c27c7b | 497 | |
9a994e8e | 498 | if (kstrtoul(buf, 0, &val) < 0) |
77c27c7b AC |
499 | return -EINVAL; |
500 | ||
bc6caf02 TH |
501 | if (val && device_remove_file_self(dev, attr)) |
502 | pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); | |
77c27c7b AC |
503 | return count; |
504 | } | |
bf22c90f SK |
505 | static struct device_attribute dev_remove_attr = __ATTR(remove, |
506 | (S_IWUSR|S_IWGRP), | |
507 | NULL, remove_store); | |
b9d320fc | 508 | |
3c78bc61 RD |
509 | static ssize_t dev_bus_rescan_store(struct device *dev, |
510 | struct device_attribute *attr, | |
511 | const char *buf, size_t count) | |
b9d320fc YL |
512 | { |
513 | unsigned long val; | |
514 | struct pci_bus *bus = to_pci_bus(dev); | |
515 | ||
9a994e8e | 516 | if (kstrtoul(buf, 0, &val) < 0) |
b9d320fc YL |
517 | return -EINVAL; |
518 | ||
519 | if (val) { | |
9d16947b | 520 | pci_lock_rescan_remove(); |
2f320521 YL |
521 | if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) |
522 | pci_rescan_bus_bridge_resize(bus->self); | |
523 | else | |
524 | pci_rescan_bus(bus); | |
9d16947b | 525 | pci_unlock_rescan_remove(); |
b9d320fc YL |
526 | } |
527 | return count; | |
528 | } | |
56039e65 | 529 | static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store); |
b9d320fc | 530 | |
fbb988be | 531 | #if defined(CONFIG_PM) && defined(CONFIG_ACPI) |
448bd857 HY |
532 | static ssize_t d3cold_allowed_store(struct device *dev, |
533 | struct device_attribute *attr, | |
534 | const char *buf, size_t count) | |
535 | { | |
536 | struct pci_dev *pdev = to_pci_dev(dev); | |
537 | unsigned long val; | |
538 | ||
9a994e8e | 539 | if (kstrtoul(buf, 0, &val) < 0) |
448bd857 HY |
540 | return -EINVAL; |
541 | ||
542 | pdev->d3cold_allowed = !!val; | |
9d26d3a8 MW |
543 | if (pdev->d3cold_allowed) |
544 | pci_d3cold_enable(pdev); | |
545 | else | |
546 | pci_d3cold_disable(pdev); | |
547 | ||
448bd857 HY |
548 | pm_runtime_resume(dev); |
549 | ||
550 | return count; | |
551 | } | |
552 | ||
553 | static ssize_t d3cold_allowed_show(struct device *dev, | |
554 | struct device_attribute *attr, char *buf) | |
555 | { | |
556 | struct pci_dev *pdev = to_pci_dev(dev); | |
3c78bc61 | 557 | return sprintf(buf, "%u\n", pdev->d3cold_allowed); |
448bd857 | 558 | } |
5136b2da | 559 | static DEVICE_ATTR_RW(d3cold_allowed); |
448bd857 HY |
560 | #endif |
561 | ||
dfc73e7a SO |
562 | #ifdef CONFIG_OF |
563 | static ssize_t devspec_show(struct device *dev, | |
564 | struct device_attribute *attr, char *buf) | |
565 | { | |
566 | struct pci_dev *pdev = to_pci_dev(dev); | |
567 | struct device_node *np = pci_device_to_OF_node(pdev); | |
568 | ||
b63773a8 | 569 | if (np == NULL) |
dfc73e7a | 570 | return 0; |
b63773a8 | 571 | return sprintf(buf, "%pOF", np); |
dfc73e7a SO |
572 | } |
573 | static DEVICE_ATTR_RO(devspec); | |
574 | #endif | |
575 | ||
1789382a DD |
576 | #ifdef CONFIG_PCI_IOV |
577 | static ssize_t sriov_totalvfs_show(struct device *dev, | |
578 | struct device_attribute *attr, | |
579 | char *buf) | |
580 | { | |
581 | struct pci_dev *pdev = to_pci_dev(dev); | |
582 | ||
bff73156 | 583 | return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev)); |
1789382a DD |
584 | } |
585 | ||
586 | ||
587 | static ssize_t sriov_numvfs_show(struct device *dev, | |
588 | struct device_attribute *attr, | |
589 | char *buf) | |
590 | { | |
591 | struct pci_dev *pdev = to_pci_dev(dev); | |
592 | ||
6b136724 | 593 | return sprintf(buf, "%u\n", pdev->sriov->num_VFs); |
1789382a DD |
594 | } |
595 | ||
596 | /* | |
faa48a50 BH |
597 | * num_vfs > 0; number of VFs to enable |
598 | * num_vfs = 0; disable all VFs | |
1789382a DD |
599 | * |
600 | * Note: SRIOV spec doesn't allow partial VF | |
faa48a50 | 601 | * disable, so it's all or none. |
1789382a DD |
602 | */ |
603 | static ssize_t sriov_numvfs_store(struct device *dev, | |
604 | struct device_attribute *attr, | |
605 | const char *buf, size_t count) | |
606 | { | |
607 | struct pci_dev *pdev = to_pci_dev(dev); | |
faa48a50 BH |
608 | int ret; |
609 | u16 num_vfs; | |
1789382a | 610 | |
faa48a50 BH |
611 | ret = kstrtou16(buf, 0, &num_vfs); |
612 | if (ret < 0) | |
613 | return ret; | |
614 | ||
615 | if (num_vfs > pci_sriov_get_totalvfs(pdev)) | |
616 | return -ERANGE; | |
617 | ||
17530e71 | 618 | device_lock(&pdev->dev); |
5b0948df | 619 | |
faa48a50 | 620 | if (num_vfs == pdev->sriov->num_VFs) |
5b0948df | 621 | goto exit; |
1789382a DD |
622 | |
623 | /* is PF driver loaded w/callback */ | |
624 | if (!pdev->driver || !pdev->driver->sriov_configure) { | |
faa48a50 | 625 | dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n"); |
5b0948df ET |
626 | ret = -ENOENT; |
627 | goto exit; | |
1789382a DD |
628 | } |
629 | ||
faa48a50 BH |
630 | if (num_vfs == 0) { |
631 | /* disable VFs */ | |
632 | ret = pdev->driver->sriov_configure(pdev, 0); | |
5b0948df | 633 | goto exit; |
1789382a DD |
634 | } |
635 | ||
faa48a50 BH |
636 | /* enable VFs */ |
637 | if (pdev->sriov->num_VFs) { | |
638 | dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n", | |
639 | pdev->sriov->num_VFs, num_vfs); | |
5b0948df ET |
640 | ret = -EBUSY; |
641 | goto exit; | |
1789382a DD |
642 | } |
643 | ||
faa48a50 BH |
644 | ret = pdev->driver->sriov_configure(pdev, num_vfs); |
645 | if (ret < 0) | |
5b0948df | 646 | goto exit; |
1789382a | 647 | |
faa48a50 BH |
648 | if (ret != num_vfs) |
649 | dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n", | |
650 | num_vfs, ret); | |
651 | ||
5b0948df | 652 | exit: |
17530e71 | 653 | device_unlock(&pdev->dev); |
5b0948df ET |
654 | |
655 | if (ret < 0) | |
656 | return ret; | |
657 | ||
faa48a50 | 658 | return count; |
1789382a DD |
659 | } |
660 | ||
7dfca152 FS |
661 | static ssize_t sriov_offset_show(struct device *dev, |
662 | struct device_attribute *attr, | |
663 | char *buf) | |
664 | { | |
665 | struct pci_dev *pdev = to_pci_dev(dev); | |
666 | ||
667 | return sprintf(buf, "%u\n", pdev->sriov->offset); | |
668 | } | |
669 | ||
670 | static ssize_t sriov_stride_show(struct device *dev, | |
671 | struct device_attribute *attr, | |
672 | char *buf) | |
673 | { | |
674 | struct pci_dev *pdev = to_pci_dev(dev); | |
675 | ||
676 | return sprintf(buf, "%u\n", pdev->sriov->stride); | |
677 | } | |
678 | ||
679 | static ssize_t sriov_vf_device_show(struct device *dev, | |
680 | struct device_attribute *attr, | |
681 | char *buf) | |
682 | { | |
683 | struct pci_dev *pdev = to_pci_dev(dev); | |
684 | ||
685 | return sprintf(buf, "%x\n", pdev->sriov->vf_device); | |
686 | } | |
687 | ||
0e7df224 BW |
688 | static ssize_t sriov_drivers_autoprobe_show(struct device *dev, |
689 | struct device_attribute *attr, | |
690 | char *buf) | |
691 | { | |
692 | struct pci_dev *pdev = to_pci_dev(dev); | |
693 | ||
694 | return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe); | |
695 | } | |
696 | ||
697 | static ssize_t sriov_drivers_autoprobe_store(struct device *dev, | |
698 | struct device_attribute *attr, | |
699 | const char *buf, size_t count) | |
700 | { | |
701 | struct pci_dev *pdev = to_pci_dev(dev); | |
702 | bool drivers_autoprobe; | |
703 | ||
704 | if (kstrtobool(buf, &drivers_autoprobe) < 0) | |
705 | return -EINVAL; | |
706 | ||
707 | pdev->sriov->drivers_autoprobe = drivers_autoprobe; | |
708 | ||
709 | return count; | |
710 | } | |
711 | ||
1789382a DD |
712 | static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs); |
713 | static struct device_attribute sriov_numvfs_attr = | |
714 | __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP), | |
715 | sriov_numvfs_show, sriov_numvfs_store); | |
7dfca152 FS |
716 | static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset); |
717 | static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride); | |
718 | static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device); | |
0e7df224 BW |
719 | static struct device_attribute sriov_drivers_autoprobe_attr = |
720 | __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP), | |
721 | sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store); | |
1789382a DD |
722 | #endif /* CONFIG_PCI_IOV */ |
723 | ||
782a985d AW |
724 | static ssize_t driver_override_store(struct device *dev, |
725 | struct device_attribute *attr, | |
726 | const char *buf, size_t count) | |
727 | { | |
728 | struct pci_dev *pdev = to_pci_dev(dev); | |
9561475d | 729 | char *driver_override, *old, *cp; |
782a985d | 730 | |
4efe874a SL |
731 | /* We need to keep extra room for a newline */ |
732 | if (count >= (PAGE_SIZE - 1)) | |
782a985d AW |
733 | return -EINVAL; |
734 | ||
735 | driver_override = kstrndup(buf, count, GFP_KERNEL); | |
736 | if (!driver_override) | |
737 | return -ENOMEM; | |
738 | ||
739 | cp = strchr(driver_override, '\n'); | |
740 | if (cp) | |
741 | *cp = '\0'; | |
742 | ||
9561475d NS |
743 | device_lock(dev); |
744 | old = pdev->driver_override; | |
782a985d AW |
745 | if (strlen(driver_override)) { |
746 | pdev->driver_override = driver_override; | |
747 | } else { | |
748 | kfree(driver_override); | |
749 | pdev->driver_override = NULL; | |
750 | } | |
9561475d | 751 | device_unlock(dev); |
782a985d AW |
752 | |
753 | kfree(old); | |
754 | ||
755 | return count; | |
756 | } | |
757 | ||
758 | static ssize_t driver_override_show(struct device *dev, | |
759 | struct device_attribute *attr, char *buf) | |
760 | { | |
761 | struct pci_dev *pdev = to_pci_dev(dev); | |
9561475d | 762 | ssize_t len; |
782a985d | 763 | |
9561475d NS |
764 | device_lock(dev); |
765 | len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override); | |
766 | device_unlock(dev); | |
767 | return len; | |
782a985d AW |
768 | } |
769 | static DEVICE_ATTR_RW(driver_override); | |
770 | ||
bf22c90f | 771 | static struct attribute *pci_dev_attrs[] = { |
5136b2da GKH |
772 | &dev_attr_resource.attr, |
773 | &dev_attr_vendor.attr, | |
774 | &dev_attr_device.attr, | |
775 | &dev_attr_subsystem_vendor.attr, | |
776 | &dev_attr_subsystem_device.attr, | |
702ed3be | 777 | &dev_attr_revision.attr, |
5136b2da GKH |
778 | &dev_attr_class.attr, |
779 | &dev_attr_irq.attr, | |
780 | &dev_attr_local_cpus.attr, | |
781 | &dev_attr_local_cpulist.attr, | |
782 | &dev_attr_modalias.attr, | |
81bb0e19 | 783 | #ifdef CONFIG_NUMA |
5136b2da | 784 | &dev_attr_numa_node.attr, |
81bb0e19 | 785 | #endif |
5136b2da GKH |
786 | &dev_attr_dma_mask_bits.attr, |
787 | &dev_attr_consistent_dma_mask_bits.attr, | |
d8e7d53a | 788 | &dev_attr_enable.attr, |
5136b2da GKH |
789 | &dev_attr_broken_parity_status.attr, |
790 | &dev_attr_msi_bus.attr, | |
fbb988be | 791 | #if defined(CONFIG_PM) && defined(CONFIG_ACPI) |
5136b2da | 792 | &dev_attr_d3cold_allowed.attr, |
dfc73e7a SO |
793 | #endif |
794 | #ifdef CONFIG_OF | |
795 | &dev_attr_devspec.attr, | |
77c27c7b | 796 | #endif |
782a985d | 797 | &dev_attr_driver_override.attr, |
5136b2da GKH |
798 | NULL, |
799 | }; | |
800 | ||
56c1af46 WVK |
801 | static struct attribute *pci_bridge_attrs[] = { |
802 | &dev_attr_subordinate_bus_number.attr, | |
803 | &dev_attr_secondary_bus_number.attr, | |
804 | NULL, | |
5136b2da GKH |
805 | }; |
806 | ||
56c1af46 WVK |
807 | static struct attribute *pcie_dev_attrs[] = { |
808 | &dev_attr_current_link_speed.attr, | |
809 | &dev_attr_current_link_width.attr, | |
810 | &dev_attr_max_link_width.attr, | |
811 | &dev_attr_max_link_speed.attr, | |
5136b2da | 812 | NULL, |
1da177e4 LT |
813 | }; |
814 | ||
56039e65 GKH |
815 | static struct attribute *pcibus_attrs[] = { |
816 | &dev_attr_rescan.attr, | |
817 | &dev_attr_cpuaffinity.attr, | |
818 | &dev_attr_cpulistaffinity.attr, | |
819 | NULL, | |
820 | }; | |
821 | ||
822 | static const struct attribute_group pcibus_group = { | |
823 | .attrs = pcibus_attrs, | |
824 | }; | |
825 | ||
826 | const struct attribute_group *pcibus_groups[] = { | |
827 | &pcibus_group, | |
828 | NULL, | |
b9d320fc YL |
829 | }; |
830 | ||
3c78bc61 RD |
831 | static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, |
832 | char *buf) | |
217f45de DA |
833 | { |
834 | struct pci_dev *pdev = to_pci_dev(dev); | |
1a39b310 MG |
835 | struct pci_dev *vga_dev = vga_default_device(); |
836 | ||
837 | if (vga_dev) | |
838 | return sprintf(buf, "%u\n", (pdev == vga_dev)); | |
217f45de DA |
839 | |
840 | return sprintf(buf, "%u\n", | |
841 | !!(pdev->resource[PCI_ROM_RESOURCE].flags & | |
842 | IORESOURCE_ROM_SHADOW)); | |
843 | } | |
bf22c90f | 844 | static struct device_attribute vga_attr = __ATTR_RO(boot_vga); |
217f45de | 845 | |
3c78bc61 RD |
846 | static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, |
847 | struct bin_attribute *bin_attr, char *buf, | |
848 | loff_t off, size_t count) | |
1da177e4 | 849 | { |
554a6037 | 850 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
1da177e4 LT |
851 | unsigned int size = 64; |
852 | loff_t init_off = off; | |
3c78bc61 | 853 | u8 *data = (u8 *) buf; |
1da177e4 LT |
854 | |
855 | /* Several chips lock up trying to read undefined config space */ | |
ab0fa82b | 856 | if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) |
1da177e4 | 857 | size = dev->cfg_size; |
3c78bc61 | 858 | else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) |
1da177e4 | 859 | size = 128; |
1da177e4 LT |
860 | |
861 | if (off > size) | |
862 | return 0; | |
863 | if (off + count > size) { | |
864 | size -= off; | |
865 | count = size; | |
866 | } else { | |
867 | size = count; | |
868 | } | |
869 | ||
3d8387ef HY |
870 | pci_config_pm_runtime_get(dev); |
871 | ||
4c0619ad SS |
872 | if ((off & 1) && size) { |
873 | u8 val; | |
e04b0ea2 | 874 | pci_user_read_config_byte(dev, off, &val); |
4c0619ad | 875 | data[off - init_off] = val; |
1da177e4 | 876 | off++; |
4c0619ad SS |
877 | size--; |
878 | } | |
879 | ||
880 | if ((off & 3) && size > 2) { | |
881 | u16 val; | |
e04b0ea2 | 882 | pci_user_read_config_word(dev, off, &val); |
4c0619ad SS |
883 | data[off - init_off] = val & 0xff; |
884 | data[off - init_off + 1] = (val >> 8) & 0xff; | |
885 | off += 2; | |
886 | size -= 2; | |
1da177e4 LT |
887 | } |
888 | ||
889 | while (size > 3) { | |
4c0619ad | 890 | u32 val; |
e04b0ea2 | 891 | pci_user_read_config_dword(dev, off, &val); |
4c0619ad SS |
892 | data[off - init_off] = val & 0xff; |
893 | data[off - init_off + 1] = (val >> 8) & 0xff; | |
894 | data[off - init_off + 2] = (val >> 16) & 0xff; | |
895 | data[off - init_off + 3] = (val >> 24) & 0xff; | |
1da177e4 LT |
896 | off += 4; |
897 | size -= 4; | |
898 | } | |
899 | ||
4c0619ad SS |
900 | if (size >= 2) { |
901 | u16 val; | |
e04b0ea2 | 902 | pci_user_read_config_word(dev, off, &val); |
4c0619ad SS |
903 | data[off - init_off] = val & 0xff; |
904 | data[off - init_off + 1] = (val >> 8) & 0xff; | |
905 | off += 2; | |
906 | size -= 2; | |
907 | } | |
908 | ||
909 | if (size > 0) { | |
910 | u8 val; | |
e04b0ea2 | 911 | pci_user_read_config_byte(dev, off, &val); |
4c0619ad | 912 | data[off - init_off] = val; |
1da177e4 LT |
913 | off++; |
914 | --size; | |
915 | } | |
916 | ||
3d8387ef HY |
917 | pci_config_pm_runtime_put(dev); |
918 | ||
1da177e4 LT |
919 | return count; |
920 | } | |
921 | ||
3c78bc61 RD |
922 | static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, |
923 | struct bin_attribute *bin_attr, char *buf, | |
924 | loff_t off, size_t count) | |
1da177e4 | 925 | { |
554a6037 | 926 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
1da177e4 LT |
927 | unsigned int size = count; |
928 | loff_t init_off = off; | |
3c78bc61 | 929 | u8 *data = (u8 *) buf; |
1da177e4 | 930 | |
cf53ecd8 MG |
931 | if (kernel_is_locked_down("Direct PCI access")) |
932 | return -EPERM; | |
933 | ||
1da177e4 LT |
934 | if (off > dev->cfg_size) |
935 | return 0; | |
936 | if (off + count > dev->cfg_size) { | |
937 | size = dev->cfg_size - off; | |
938 | count = size; | |
939 | } | |
f7625980 | 940 | |
3d8387ef HY |
941 | pci_config_pm_runtime_get(dev); |
942 | ||
4c0619ad | 943 | if ((off & 1) && size) { |
e04b0ea2 | 944 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
1da177e4 | 945 | off++; |
4c0619ad | 946 | size--; |
1da177e4 | 947 | } |
f7625980 | 948 | |
4c0619ad SS |
949 | if ((off & 3) && size > 2) { |
950 | u16 val = data[off - init_off]; | |
951 | val |= (u16) data[off - init_off + 1] << 8; | |
3c78bc61 RD |
952 | pci_user_write_config_word(dev, off, val); |
953 | off += 2; | |
954 | size -= 2; | |
955 | } | |
1da177e4 LT |
956 | |
957 | while (size > 3) { | |
4c0619ad SS |
958 | u32 val = data[off - init_off]; |
959 | val |= (u32) data[off - init_off + 1] << 8; | |
960 | val |= (u32) data[off - init_off + 2] << 16; | |
961 | val |= (u32) data[off - init_off + 3] << 24; | |
e04b0ea2 | 962 | pci_user_write_config_dword(dev, off, val); |
1da177e4 LT |
963 | off += 4; |
964 | size -= 4; | |
965 | } | |
f7625980 | 966 | |
4c0619ad SS |
967 | if (size >= 2) { |
968 | u16 val = data[off - init_off]; | |
969 | val |= (u16) data[off - init_off + 1] << 8; | |
e04b0ea2 | 970 | pci_user_write_config_word(dev, off, val); |
4c0619ad SS |
971 | off += 2; |
972 | size -= 2; | |
973 | } | |
1da177e4 | 974 | |
4c0619ad | 975 | if (size) { |
e04b0ea2 | 976 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
1da177e4 LT |
977 | off++; |
978 | --size; | |
979 | } | |
980 | ||
3d8387ef HY |
981 | pci_config_pm_runtime_put(dev); |
982 | ||
1da177e4 LT |
983 | return count; |
984 | } | |
985 | ||
3c78bc61 RD |
986 | static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj, |
987 | struct bin_attribute *bin_attr, char *buf, | |
988 | loff_t off, size_t count) | |
94e61088 | 989 | { |
554a6037 | 990 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
94e61088 | 991 | |
f52e5629 HR |
992 | if (bin_attr->size > 0) { |
993 | if (off > bin_attr->size) | |
994 | count = 0; | |
995 | else if (count > bin_attr->size - off) | |
996 | count = bin_attr->size - off; | |
997 | } | |
94e61088 | 998 | |
287d19ce | 999 | return pci_read_vpd(dev, off, count, buf); |
94e61088 BH |
1000 | } |
1001 | ||
3c78bc61 RD |
1002 | static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj, |
1003 | struct bin_attribute *bin_attr, char *buf, | |
1004 | loff_t off, size_t count) | |
94e61088 | 1005 | { |
554a6037 | 1006 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
94e61088 | 1007 | |
f52e5629 HR |
1008 | if (bin_attr->size > 0) { |
1009 | if (off > bin_attr->size) | |
1010 | count = 0; | |
1011 | else if (count > bin_attr->size - off) | |
1012 | count = bin_attr->size - off; | |
1013 | } | |
94e61088 | 1014 | |
287d19ce | 1015 | return pci_write_vpd(dev, off, count, buf); |
94e61088 BH |
1016 | } |
1017 | ||
1da177e4 LT |
1018 | #ifdef HAVE_PCI_LEGACY |
1019 | /** | |
1020 | * pci_read_legacy_io - read byte(s) from legacy I/O port space | |
2c3c8bea | 1021 | * @filp: open sysfs file |
1da177e4 | 1022 | * @kobj: kobject corresponding to file to read from |
cffb2faf | 1023 | * @bin_attr: struct bin_attribute for this file |
1da177e4 LT |
1024 | * @buf: buffer to store results |
1025 | * @off: offset into legacy I/O port space | |
1026 | * @count: number of bytes to read | |
1027 | * | |
1028 | * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific | |
1029 | * callback routine (pci_legacy_read). | |
1030 | */ | |
3c78bc61 RD |
1031 | static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj, |
1032 | struct bin_attribute *bin_attr, char *buf, | |
1033 | loff_t off, size_t count) | |
1da177e4 | 1034 | { |
554a6037 | 1035 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
1da177e4 | 1036 | |
3c78bc61 RD |
1037 | /* Only support 1, 2 or 4 byte accesses */ |
1038 | if (count != 1 && count != 2 && count != 4) | |
1039 | return -EINVAL; | |
1da177e4 | 1040 | |
3c78bc61 | 1041 | return pci_legacy_read(bus, off, (u32 *)buf, count); |
1da177e4 LT |
1042 | } |
1043 | ||
1044 | /** | |
1045 | * pci_write_legacy_io - write byte(s) to legacy I/O port space | |
2c3c8bea | 1046 | * @filp: open sysfs file |
1da177e4 | 1047 | * @kobj: kobject corresponding to file to read from |
cffb2faf | 1048 | * @bin_attr: struct bin_attribute for this file |
1da177e4 LT |
1049 | * @buf: buffer containing value to be written |
1050 | * @off: offset into legacy I/O port space | |
1051 | * @count: number of bytes to write | |
1052 | * | |
1053 | * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific | |
1054 | * callback routine (pci_legacy_write). | |
1055 | */ | |
3c78bc61 RD |
1056 | static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, |
1057 | struct bin_attribute *bin_attr, char *buf, | |
1058 | loff_t off, size_t count) | |
1da177e4 | 1059 | { |
554a6037 | 1060 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
1da177e4 | 1061 | |
3c78bc61 RD |
1062 | /* Only support 1, 2 or 4 byte accesses */ |
1063 | if (count != 1 && count != 2 && count != 4) | |
1064 | return -EINVAL; | |
1065 | ||
1066 | return pci_legacy_write(bus, off, *(u32 *)buf, count); | |
1da177e4 LT |
1067 | } |
1068 | ||
1069 | /** | |
1070 | * pci_mmap_legacy_mem - map legacy PCI memory into user memory space | |
2c3c8bea | 1071 | * @filp: open sysfs file |
1da177e4 LT |
1072 | * @kobj: kobject corresponding to device to be mapped |
1073 | * @attr: struct bin_attribute for this file | |
1074 | * @vma: struct vm_area_struct passed to mmap | |
1075 | * | |
f19aeb1f | 1076 | * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap |
1da177e4 LT |
1077 | * legacy memory space (first meg of bus space) into application virtual |
1078 | * memory space. | |
1079 | */ | |
3c78bc61 RD |
1080 | static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, |
1081 | struct bin_attribute *attr, | |
1082 | struct vm_area_struct *vma) | |
1da177e4 | 1083 | { |
554a6037 | 1084 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
1da177e4 | 1085 | |
3c78bc61 | 1086 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); |
f19aeb1f BH |
1087 | } |
1088 | ||
1089 | /** | |
1090 | * pci_mmap_legacy_io - map legacy PCI IO into user memory space | |
2c3c8bea | 1091 | * @filp: open sysfs file |
f19aeb1f BH |
1092 | * @kobj: kobject corresponding to device to be mapped |
1093 | * @attr: struct bin_attribute for this file | |
1094 | * @vma: struct vm_area_struct passed to mmap | |
1095 | * | |
1096 | * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap | |
1097 | * legacy IO space (first meg of bus space) into application virtual | |
1098 | * memory space. Returns -ENOSYS if the operation isn't supported | |
1099 | */ | |
3c78bc61 RD |
1100 | static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, |
1101 | struct bin_attribute *attr, | |
1102 | struct vm_area_struct *vma) | |
f19aeb1f | 1103 | { |
554a6037 | 1104 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
f19aeb1f | 1105 | |
3c78bc61 | 1106 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); |
f19aeb1f BH |
1107 | } |
1108 | ||
10a0ef39 IK |
1109 | /** |
1110 | * pci_adjust_legacy_attr - adjustment of legacy file attributes | |
1111 | * @b: bus to create files under | |
1112 | * @mmap_type: I/O port or memory | |
1113 | * | |
1114 | * Stub implementation. Can be overridden by arch if necessary. | |
1115 | */ | |
3c78bc61 RD |
1116 | void __weak pci_adjust_legacy_attr(struct pci_bus *b, |
1117 | enum pci_mmap_state mmap_type) | |
10a0ef39 | 1118 | { |
10a0ef39 IK |
1119 | } |
1120 | ||
f19aeb1f BH |
1121 | /** |
1122 | * pci_create_legacy_files - create legacy I/O port and memory files | |
1123 | * @b: bus to create files under | |
1124 | * | |
1125 | * Some platforms allow access to legacy I/O port and ISA memory space on | |
1126 | * a per-bus basis. This routine creates the files and ties them into | |
1127 | * their associated read, write and mmap files from pci-sysfs.c | |
1128 | * | |
25985edc | 1129 | * On error unwind, but don't propagate the error to the caller |
f19aeb1f BH |
1130 | * as it is ok to set up the PCI bus without these files. |
1131 | */ | |
1132 | void pci_create_legacy_files(struct pci_bus *b) | |
1133 | { | |
1134 | int error; | |
1135 | ||
1136 | b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2, | |
1137 | GFP_ATOMIC); | |
1138 | if (!b->legacy_io) | |
1139 | goto kzalloc_err; | |
1140 | ||
62e877b8 | 1141 | sysfs_bin_attr_init(b->legacy_io); |
f19aeb1f BH |
1142 | b->legacy_io->attr.name = "legacy_io"; |
1143 | b->legacy_io->size = 0xffff; | |
1144 | b->legacy_io->attr.mode = S_IRUSR | S_IWUSR; | |
1145 | b->legacy_io->read = pci_read_legacy_io; | |
1146 | b->legacy_io->write = pci_write_legacy_io; | |
1147 | b->legacy_io->mmap = pci_mmap_legacy_io; | |
10a0ef39 | 1148 | pci_adjust_legacy_attr(b, pci_mmap_io); |
f19aeb1f BH |
1149 | error = device_create_bin_file(&b->dev, b->legacy_io); |
1150 | if (error) | |
1151 | goto legacy_io_err; | |
1152 | ||
1153 | /* Allocated above after the legacy_io struct */ | |
1154 | b->legacy_mem = b->legacy_io + 1; | |
6757eca3 | 1155 | sysfs_bin_attr_init(b->legacy_mem); |
f19aeb1f BH |
1156 | b->legacy_mem->attr.name = "legacy_mem"; |
1157 | b->legacy_mem->size = 1024*1024; | |
1158 | b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR; | |
1159 | b->legacy_mem->mmap = pci_mmap_legacy_mem; | |
10a0ef39 | 1160 | pci_adjust_legacy_attr(b, pci_mmap_mem); |
f19aeb1f BH |
1161 | error = device_create_bin_file(&b->dev, b->legacy_mem); |
1162 | if (error) | |
1163 | goto legacy_mem_err; | |
1164 | ||
1165 | return; | |
1166 | ||
1167 | legacy_mem_err: | |
1168 | device_remove_bin_file(&b->dev, b->legacy_io); | |
1169 | legacy_io_err: | |
1170 | kfree(b->legacy_io); | |
1171 | b->legacy_io = NULL; | |
1172 | kzalloc_err: | |
227f0647 | 1173 | printk(KERN_WARNING "pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n"); |
f19aeb1f BH |
1174 | return; |
1175 | } | |
1176 | ||
1177 | void pci_remove_legacy_files(struct pci_bus *b) | |
1178 | { | |
1179 | if (b->legacy_io) { | |
1180 | device_remove_bin_file(&b->dev, b->legacy_io); | |
1181 | device_remove_bin_file(&b->dev, b->legacy_mem); | |
1182 | kfree(b->legacy_io); /* both are allocated here */ | |
1183 | } | |
1da177e4 LT |
1184 | } |
1185 | #endif /* HAVE_PCI_LEGACY */ | |
1186 | ||
f7195824 | 1187 | #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) |
b5ff7df3 | 1188 | |
3b519e4e MW |
1189 | int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma, |
1190 | enum pci_mmap_api mmap_api) | |
b5ff7df3 | 1191 | { |
6bccc7f4 DW |
1192 | unsigned long nr, start, size; |
1193 | resource_size_t pci_start = 0, pci_end; | |
b5ff7df3 | 1194 | |
3b519e4e MW |
1195 | if (pci_resource_len(pdev, resno) == 0) |
1196 | return 0; | |
64b00175 | 1197 | nr = vma_pages(vma); |
b5ff7df3 | 1198 | start = vma->vm_pgoff; |
88e7df0b | 1199 | size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1; |
6bccc7f4 DW |
1200 | if (mmap_api == PCI_MMAP_PROCFS) { |
1201 | pci_resource_to_user(pdev, resno, &pdev->resource[resno], | |
1202 | &pci_start, &pci_end); | |
1203 | pci_start >>= PAGE_SHIFT; | |
1204 | } | |
3b519e4e MW |
1205 | if (start >= pci_start && start < pci_start + size && |
1206 | start + nr <= pci_start + size) | |
b5ff7df3 | 1207 | return 1; |
b5ff7df3 LT |
1208 | return 0; |
1209 | } | |
1210 | ||
1da177e4 LT |
1211 | /** |
1212 | * pci_mmap_resource - map a PCI resource into user memory space | |
1213 | * @kobj: kobject for mapping | |
1214 | * @attr: struct bin_attribute for the file being mapped | |
1215 | * @vma: struct vm_area_struct passed into the mmap | |
45aec1ae | 1216 | * @write_combine: 1 for write_combine mapping |
1da177e4 LT |
1217 | * |
1218 | * Use the regular PCI mapping routines to map a PCI resource into userspace. | |
1da177e4 | 1219 | */ |
3c78bc61 RD |
1220 | static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, |
1221 | struct vm_area_struct *vma, int write_combine) | |
1da177e4 | 1222 | { |
554a6037 | 1223 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
dca40b18 | 1224 | int bar = (unsigned long)attr->private; |
1da177e4 | 1225 | enum pci_mmap_state mmap_type; |
dca40b18 | 1226 | struct resource *res = &pdev->resource[bar]; |
2311b1f2 | 1227 | |
cf53ecd8 MG |
1228 | if (kernel_is_locked_down("Direct PCI access")) |
1229 | return -EPERM; | |
1230 | ||
ca620723 BH |
1231 | if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start)) |
1232 | return -EINVAL; | |
1233 | ||
dca40b18 | 1234 | if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) { |
227f0647 | 1235 | 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 | 1236 | current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff, |
dca40b18 DW |
1237 | pci_name(pdev), bar, |
1238 | (u64)pci_resource_start(pdev, bar), | |
1239 | (u64)pci_resource_len(pdev, bar)); | |
b5ff7df3 | 1240 | return -EINVAL; |
3b519e4e | 1241 | } |
1da177e4 | 1242 | mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; |
f7195824 DW |
1243 | |
1244 | return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); | |
45aec1ae | 1245 | } |
1246 | ||
3c78bc61 RD |
1247 | static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, |
1248 | struct bin_attribute *attr, | |
1249 | struct vm_area_struct *vma) | |
45aec1ae | 1250 | { |
1251 | return pci_mmap_resource(kobj, attr, vma, 0); | |
1252 | } | |
1253 | ||
3c78bc61 RD |
1254 | static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, |
1255 | struct bin_attribute *attr, | |
1256 | struct vm_area_struct *vma) | |
45aec1ae | 1257 | { |
1258 | return pci_mmap_resource(kobj, attr, vma, 1); | |
1da177e4 LT |
1259 | } |
1260 | ||
3c78bc61 RD |
1261 | static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, |
1262 | struct bin_attribute *attr, char *buf, | |
1263 | loff_t off, size_t count, bool write) | |
8633328b | 1264 | { |
554a6037 | 1265 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
dca40b18 | 1266 | int bar = (unsigned long)attr->private; |
8633328b | 1267 | unsigned long port = off; |
8633328b | 1268 | |
dca40b18 | 1269 | port += pci_resource_start(pdev, bar); |
8633328b | 1270 | |
dca40b18 | 1271 | if (port > pci_resource_end(pdev, bar)) |
8633328b AW |
1272 | return 0; |
1273 | ||
dca40b18 | 1274 | if (port + count - 1 > pci_resource_end(pdev, bar)) |
8633328b AW |
1275 | return -EINVAL; |
1276 | ||
1277 | switch (count) { | |
1278 | case 1: | |
1279 | if (write) | |
1280 | outb(*(u8 *)buf, port); | |
1281 | else | |
1282 | *(u8 *)buf = inb(port); | |
1283 | return 1; | |
1284 | case 2: | |
1285 | if (write) | |
1286 | outw(*(u16 *)buf, port); | |
1287 | else | |
1288 | *(u16 *)buf = inw(port); | |
1289 | return 2; | |
1290 | case 4: | |
1291 | if (write) | |
1292 | outl(*(u32 *)buf, port); | |
1293 | else | |
1294 | *(u32 *)buf = inl(port); | |
1295 | return 4; | |
1296 | } | |
1297 | return -EINVAL; | |
1298 | } | |
1299 | ||
3c78bc61 RD |
1300 | static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj, |
1301 | struct bin_attribute *attr, char *buf, | |
1302 | loff_t off, size_t count) | |
8633328b AW |
1303 | { |
1304 | return pci_resource_io(filp, kobj, attr, buf, off, count, false); | |
1305 | } | |
1306 | ||
3c78bc61 RD |
1307 | static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, |
1308 | struct bin_attribute *attr, char *buf, | |
1309 | loff_t off, size_t count) | |
8633328b | 1310 | { |
cf53ecd8 MG |
1311 | if (kernel_is_locked_down("Direct PCI access")) |
1312 | return -EPERM; | |
1313 | ||
8633328b AW |
1314 | return pci_resource_io(filp, kobj, attr, buf, off, count, true); |
1315 | } | |
1316 | ||
b19441af GKH |
1317 | /** |
1318 | * pci_remove_resource_files - cleanup resource files | |
cffb2faf | 1319 | * @pdev: dev to cleanup |
b19441af | 1320 | * |
cffb2faf | 1321 | * If we created resource files for @pdev, remove them from sysfs and |
b19441af GKH |
1322 | * free their resources. |
1323 | */ | |
3c78bc61 | 1324 | static void pci_remove_resource_files(struct pci_dev *pdev) |
b19441af GKH |
1325 | { |
1326 | int i; | |
1327 | ||
1328 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { | |
1329 | struct bin_attribute *res_attr; | |
1330 | ||
1331 | res_attr = pdev->res_attr[i]; | |
1332 | if (res_attr) { | |
1333 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); | |
1334 | kfree(res_attr); | |
1335 | } | |
45aec1ae | 1336 | |
1337 | res_attr = pdev->res_attr_wc[i]; | |
1338 | if (res_attr) { | |
1339 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); | |
1340 | kfree(res_attr); | |
1341 | } | |
b19441af GKH |
1342 | } |
1343 | } | |
1344 | ||
45aec1ae | 1345 | static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) |
1346 | { | |
1347 | /* allocate attribute structure, piggyback attribute name */ | |
1348 | int name_len = write_combine ? 13 : 10; | |
1349 | struct bin_attribute *res_attr; | |
bd5174df | 1350 | char *res_attr_name; |
45aec1ae | 1351 | int retval; |
1352 | ||
1353 | res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); | |
bd5174df BH |
1354 | if (!res_attr) |
1355 | return -ENOMEM; | |
1356 | ||
1357 | res_attr_name = (char *)(res_attr + 1); | |
1358 | ||
1359 | sysfs_bin_attr_init(res_attr); | |
1360 | if (write_combine) { | |
1361 | pdev->res_attr_wc[num] = res_attr; | |
1362 | sprintf(res_attr_name, "resource%d_wc", num); | |
1363 | res_attr->mmap = pci_mmap_resource_wc; | |
1364 | } else { | |
1365 | pdev->res_attr[num] = res_attr; | |
1366 | sprintf(res_attr_name, "resource%d", num); | |
e854d8b2 DW |
1367 | if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { |
1368 | res_attr->read = pci_read_resource_io; | |
1369 | res_attr->write = pci_write_resource_io; | |
1370 | if (arch_can_pci_mmap_io()) | |
1371 | res_attr->mmap = pci_mmap_resource_uc; | |
1372 | } else { | |
1373 | res_attr->mmap = pci_mmap_resource_uc; | |
1374 | } | |
bd5174df BH |
1375 | } |
1376 | res_attr->attr.name = res_attr_name; | |
1377 | res_attr->attr.mode = S_IRUSR | S_IWUSR; | |
1378 | res_attr->size = pci_resource_len(pdev, num); | |
dca40b18 | 1379 | res_attr->private = (void *)(unsigned long)num; |
bd5174df BH |
1380 | retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); |
1381 | if (retval) | |
1382 | kfree(res_attr); | |
45aec1ae | 1383 | |
1384 | return retval; | |
1385 | } | |
1386 | ||
1da177e4 LT |
1387 | /** |
1388 | * pci_create_resource_files - create resource files in sysfs for @dev | |
cffb2faf | 1389 | * @pdev: dev in question |
1da177e4 | 1390 | * |
cffb2faf | 1391 | * Walk the resources in @pdev creating files for each resource available. |
1da177e4 | 1392 | */ |
b19441af | 1393 | static int pci_create_resource_files(struct pci_dev *pdev) |
1da177e4 LT |
1394 | { |
1395 | int i; | |
b19441af | 1396 | int retval; |
1da177e4 LT |
1397 | |
1398 | /* Expose the PCI resources from this device as files */ | |
1399 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { | |
1da177e4 LT |
1400 | |
1401 | /* skip empty resources */ | |
1402 | if (!pci_resource_len(pdev, i)) | |
1403 | continue; | |
1404 | ||
45aec1ae | 1405 | retval = pci_create_attr(pdev, i, 0); |
1406 | /* for prefetchable resources, create a WC mappable file */ | |
ae749c7a DW |
1407 | if (!retval && arch_can_pci_mmap_wc() && |
1408 | pdev->resource[i].flags & IORESOURCE_PREFETCH) | |
45aec1ae | 1409 | retval = pci_create_attr(pdev, i, 1); |
45aec1ae | 1410 | if (retval) { |
1411 | pci_remove_resource_files(pdev); | |
1412 | return retval; | |
1da177e4 LT |
1413 | } |
1414 | } | |
b19441af | 1415 | return 0; |
1da177e4 LT |
1416 | } |
1417 | #else /* !HAVE_PCI_MMAP */ | |
10a0ef39 IK |
1418 | int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } |
1419 | void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } | |
1da177e4 LT |
1420 | #endif /* HAVE_PCI_MMAP */ |
1421 | ||
1422 | /** | |
1423 | * pci_write_rom - used to enable access to the PCI ROM display | |
2c3c8bea | 1424 | * @filp: sysfs file |
1da177e4 | 1425 | * @kobj: kernel object handle |
cffb2faf | 1426 | * @bin_attr: struct bin_attribute for this file |
1da177e4 LT |
1427 | * @buf: user input |
1428 | * @off: file offset | |
1429 | * @count: number of byte in input | |
1430 | * | |
1431 | * writing anything except 0 enables it | |
1432 | */ | |
3c78bc61 RD |
1433 | static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, |
1434 | struct bin_attribute *bin_attr, char *buf, | |
1435 | loff_t off, size_t count) | |
1da177e4 | 1436 | { |
554a6037 | 1437 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
1da177e4 LT |
1438 | |
1439 | if ((off == 0) && (*buf == '0') && (count == 2)) | |
1440 | pdev->rom_attr_enabled = 0; | |
1441 | else | |
1442 | pdev->rom_attr_enabled = 1; | |
1443 | ||
1444 | return count; | |
1445 | } | |
1446 | ||
1447 | /** | |
1448 | * pci_read_rom - read a PCI ROM | |
2c3c8bea | 1449 | * @filp: sysfs file |
1da177e4 | 1450 | * @kobj: kernel object handle |
cffb2faf | 1451 | * @bin_attr: struct bin_attribute for this file |
1da177e4 LT |
1452 | * @buf: where to put the data we read from the ROM |
1453 | * @off: file offset | |
1454 | * @count: number of bytes to read | |
1455 | * | |
1456 | * Put @count bytes starting at @off into @buf from the ROM in the PCI | |
1457 | * device corresponding to @kobj. | |
1458 | */ | |
3c78bc61 RD |
1459 | static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, |
1460 | struct bin_attribute *bin_attr, char *buf, | |
1461 | loff_t off, size_t count) | |
1da177e4 | 1462 | { |
554a6037 | 1463 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
1da177e4 LT |
1464 | void __iomem *rom; |
1465 | size_t size; | |
1466 | ||
1467 | if (!pdev->rom_attr_enabled) | |
1468 | return -EINVAL; | |
f7625980 | 1469 | |
1da177e4 | 1470 | rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ |
97c44836 TN |
1471 | if (!rom || !size) |
1472 | return -EIO; | |
f7625980 | 1473 | |
1da177e4 LT |
1474 | if (off >= size) |
1475 | count = 0; | |
1476 | else { | |
1477 | if (off + count > size) | |
1478 | count = size - off; | |
f7625980 | 1479 | |
1da177e4 LT |
1480 | memcpy_fromio(buf, rom + off, count); |
1481 | } | |
1482 | pci_unmap_rom(pdev, rom); | |
f7625980 | 1483 | |
1da177e4 LT |
1484 | return count; |
1485 | } | |
1486 | ||
8bdc50ac | 1487 | static const struct bin_attribute pci_config_attr = { |
1da177e4 LT |
1488 | .attr = { |
1489 | .name = "config", | |
1490 | .mode = S_IRUGO | S_IWUSR, | |
1da177e4 | 1491 | }, |
557848c3 | 1492 | .size = PCI_CFG_SPACE_SIZE, |
1da177e4 LT |
1493 | .read = pci_read_config, |
1494 | .write = pci_write_config, | |
1495 | }; | |
1496 | ||
8bdc50ac | 1497 | static const struct bin_attribute pcie_config_attr = { |
1da177e4 LT |
1498 | .attr = { |
1499 | .name = "config", | |
1500 | .mode = S_IRUGO | S_IWUSR, | |
1da177e4 | 1501 | }, |
557848c3 | 1502 | .size = PCI_CFG_SPACE_EXP_SIZE, |
1da177e4 LT |
1503 | .read = pci_read_config, |
1504 | .write = pci_write_config, | |
1505 | }; | |
1506 | ||
3c78bc61 RD |
1507 | static ssize_t reset_store(struct device *dev, struct device_attribute *attr, |
1508 | const char *buf, size_t count) | |
711d5779 MT |
1509 | { |
1510 | struct pci_dev *pdev = to_pci_dev(dev); | |
1511 | unsigned long val; | |
9a994e8e | 1512 | ssize_t result = kstrtoul(buf, 0, &val); |
711d5779 MT |
1513 | |
1514 | if (result < 0) | |
1515 | return result; | |
1516 | ||
1517 | if (val != 1) | |
1518 | return -EINVAL; | |
447c5dd7 MS |
1519 | |
1520 | result = pci_reset_function(pdev); | |
1521 | if (result < 0) | |
1522 | return result; | |
1523 | ||
1524 | return count; | |
711d5779 MT |
1525 | } |
1526 | ||
1527 | static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store); | |
1528 | ||
280c73d3 ZY |
1529 | static int pci_create_capabilities_sysfs(struct pci_dev *dev) |
1530 | { | |
1531 | int retval; | |
1532 | struct bin_attribute *attr; | |
1533 | ||
1534 | /* If the device has VPD, try to expose it in sysfs. */ | |
1535 | if (dev->vpd) { | |
1536 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); | |
1537 | if (!attr) | |
1538 | return -ENOMEM; | |
1539 | ||
a07e4156 | 1540 | sysfs_bin_attr_init(attr); |
104daa71 | 1541 | attr->size = 0; |
280c73d3 ZY |
1542 | attr->attr.name = "vpd"; |
1543 | attr->attr.mode = S_IRUSR | S_IWUSR; | |
287d19ce SH |
1544 | attr->read = read_vpd_attr; |
1545 | attr->write = write_vpd_attr; | |
280c73d3 ZY |
1546 | retval = sysfs_create_bin_file(&dev->dev.kobj, attr); |
1547 | if (retval) { | |
0f12a4e2 | 1548 | kfree(attr); |
280c73d3 ZY |
1549 | return retval; |
1550 | } | |
1551 | dev->vpd->attr = attr; | |
1552 | } | |
1553 | ||
1554 | /* Active State Power Management */ | |
1555 | pcie_aspm_create_sysfs_dev_files(dev); | |
1556 | ||
711d5779 MT |
1557 | if (!pci_probe_reset_function(dev)) { |
1558 | retval = device_create_file(&dev->dev, &reset_attr); | |
1559 | if (retval) | |
1560 | goto error; | |
1561 | dev->reset_fn = 1; | |
1562 | } | |
280c73d3 | 1563 | return 0; |
711d5779 MT |
1564 | |
1565 | error: | |
1566 | pcie_aspm_remove_sysfs_dev_files(dev); | |
1567 | if (dev->vpd && dev->vpd->attr) { | |
1568 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); | |
1569 | kfree(dev->vpd->attr); | |
1570 | } | |
1571 | ||
1572 | return retval; | |
280c73d3 ZY |
1573 | } |
1574 | ||
3c78bc61 | 1575 | int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) |
1da177e4 | 1576 | { |
b19441af | 1577 | int retval; |
ac0c302a | 1578 | int rom_size; |
280c73d3 | 1579 | struct bin_attribute *attr; |
b19441af | 1580 | |
1da177e4 LT |
1581 | if (!sysfs_initialized) |
1582 | return -EACCES; | |
1583 | ||
c20aecf6 | 1584 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
b19441af | 1585 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
c20aecf6 JM |
1586 | else |
1587 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); | |
b19441af GKH |
1588 | if (retval) |
1589 | goto err; | |
1da177e4 | 1590 | |
b19441af GKH |
1591 | retval = pci_create_resource_files(pdev); |
1592 | if (retval) | |
280c73d3 ZY |
1593 | goto err_config_file; |
1594 | ||
1da177e4 | 1595 | /* If the device has a ROM, try to expose it in sysfs. */ |
ac0c302a | 1596 | rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
280c73d3 | 1597 | if (rom_size) { |
94e61088 | 1598 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
280c73d3 | 1599 | if (!attr) { |
b19441af | 1600 | retval = -ENOMEM; |
9890b12a | 1601 | goto err_resource_files; |
1da177e4 | 1602 | } |
a07e4156 | 1603 | sysfs_bin_attr_init(attr); |
280c73d3 ZY |
1604 | attr->size = rom_size; |
1605 | attr->attr.name = "rom"; | |
ff29530e | 1606 | attr->attr.mode = S_IRUSR | S_IWUSR; |
280c73d3 ZY |
1607 | attr->read = pci_read_rom; |
1608 | attr->write = pci_write_rom; | |
1609 | retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); | |
1610 | if (retval) { | |
1611 | kfree(attr); | |
1612 | goto err_resource_files; | |
1613 | } | |
1614 | pdev->rom_attr = attr; | |
1da177e4 | 1615 | } |
280c73d3 | 1616 | |
280c73d3 ZY |
1617 | /* add sysfs entries for various capabilities */ |
1618 | retval = pci_create_capabilities_sysfs(pdev); | |
1619 | if (retval) | |
625e1d59 | 1620 | goto err_rom_file; |
7d715a6c | 1621 | |
911e1c9b N |
1622 | pci_create_firmware_label_files(pdev); |
1623 | ||
1da177e4 | 1624 | return 0; |
b19441af | 1625 | |
a2cd52ca | 1626 | err_rom_file: |
9d88b93b | 1627 | if (pdev->rom_attr) { |
94e61088 | 1628 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
280c73d3 ZY |
1629 | kfree(pdev->rom_attr); |
1630 | pdev->rom_attr = NULL; | |
1631 | } | |
9890b12a ME |
1632 | err_resource_files: |
1633 | pci_remove_resource_files(pdev); | |
94e61088 | 1634 | err_config_file: |
c20aecf6 | 1635 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
b19441af | 1636 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
c20aecf6 JM |
1637 | else |
1638 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); | |
b19441af GKH |
1639 | err: |
1640 | return retval; | |
1da177e4 LT |
1641 | } |
1642 | ||
280c73d3 ZY |
1643 | static void pci_remove_capabilities_sysfs(struct pci_dev *dev) |
1644 | { | |
1645 | if (dev->vpd && dev->vpd->attr) { | |
1646 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); | |
1647 | kfree(dev->vpd->attr); | |
1648 | } | |
1649 | ||
1650 | pcie_aspm_remove_sysfs_dev_files(dev); | |
711d5779 MT |
1651 | if (dev->reset_fn) { |
1652 | device_remove_file(&dev->dev, &reset_attr); | |
1653 | dev->reset_fn = 0; | |
1654 | } | |
280c73d3 ZY |
1655 | } |
1656 | ||
1da177e4 LT |
1657 | /** |
1658 | * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files | |
1659 | * @pdev: device whose entries we should free | |
1660 | * | |
1661 | * Cleanup when @pdev is removed from sysfs. | |
1662 | */ | |
1663 | void pci_remove_sysfs_dev_files(struct pci_dev *pdev) | |
1664 | { | |
d67afe5e DM |
1665 | if (!sysfs_initialized) |
1666 | return; | |
1667 | ||
280c73d3 | 1668 | pci_remove_capabilities_sysfs(pdev); |
7d715a6c | 1669 | |
c20aecf6 | 1670 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
1da177e4 | 1671 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
c20aecf6 JM |
1672 | else |
1673 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); | |
1da177e4 LT |
1674 | |
1675 | pci_remove_resource_files(pdev); | |
1676 | ||
9d88b93b | 1677 | if (pdev->rom_attr) { |
280c73d3 ZY |
1678 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
1679 | kfree(pdev->rom_attr); | |
9d88b93b | 1680 | pdev->rom_attr = NULL; |
1da177e4 | 1681 | } |
911e1c9b N |
1682 | |
1683 | pci_remove_firmware_label_files(pdev); | |
1da177e4 LT |
1684 | } |
1685 | ||
1686 | static int __init pci_sysfs_init(void) | |
1687 | { | |
1688 | struct pci_dev *pdev = NULL; | |
b19441af GKH |
1689 | int retval; |
1690 | ||
1da177e4 | 1691 | sysfs_initialized = 1; |
b19441af GKH |
1692 | for_each_pci_dev(pdev) { |
1693 | retval = pci_create_sysfs_dev_files(pdev); | |
151fc5df JL |
1694 | if (retval) { |
1695 | pci_dev_put(pdev); | |
b19441af | 1696 | return retval; |
151fc5df | 1697 | } |
b19441af | 1698 | } |
1da177e4 LT |
1699 | |
1700 | return 0; | |
1701 | } | |
40ee9e9f | 1702 | late_initcall(pci_sysfs_init); |
4e15c46b YL |
1703 | |
1704 | static struct attribute *pci_dev_dev_attrs[] = { | |
625e1d59 | 1705 | &vga_attr.attr, |
4e15c46b YL |
1706 | NULL, |
1707 | }; | |
1708 | ||
1709 | static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, | |
3c78bc61 | 1710 | struct attribute *a, int n) |
4e15c46b | 1711 | { |
554a6037 | 1712 | struct device *dev = kobj_to_dev(kobj); |
625e1d59 YL |
1713 | struct pci_dev *pdev = to_pci_dev(dev); |
1714 | ||
1715 | if (a == &vga_attr.attr) | |
1716 | if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) | |
1717 | return 0; | |
1718 | ||
4e15c46b YL |
1719 | return a->mode; |
1720 | } | |
1721 | ||
dfab88be JL |
1722 | static struct attribute *pci_dev_hp_attrs[] = { |
1723 | &dev_remove_attr.attr, | |
1724 | &dev_rescan_attr.attr, | |
1725 | NULL, | |
1726 | }; | |
1727 | ||
1728 | static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, | |
3c78bc61 | 1729 | struct attribute *a, int n) |
dfab88be | 1730 | { |
554a6037 | 1731 | struct device *dev = kobj_to_dev(kobj); |
dfab88be JL |
1732 | struct pci_dev *pdev = to_pci_dev(dev); |
1733 | ||
1734 | if (pdev->is_virtfn) | |
1735 | return 0; | |
1736 | ||
1737 | return a->mode; | |
1738 | } | |
1739 | ||
56c1af46 WVK |
1740 | static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, |
1741 | struct attribute *a, int n) | |
1742 | { | |
1743 | struct device *dev = kobj_to_dev(kobj); | |
1744 | struct pci_dev *pdev = to_pci_dev(dev); | |
1745 | ||
1746 | if (pci_is_bridge(pdev)) | |
1747 | return a->mode; | |
1748 | ||
1749 | return 0; | |
1750 | } | |
1751 | ||
1752 | static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, | |
1753 | struct attribute *a, int n) | |
1754 | { | |
1755 | struct device *dev = kobj_to_dev(kobj); | |
1756 | struct pci_dev *pdev = to_pci_dev(dev); | |
1757 | ||
1758 | if (pci_is_pcie(pdev)) | |
1759 | return a->mode; | |
1760 | ||
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | static const struct attribute_group pci_dev_group = { | |
1765 | .attrs = pci_dev_attrs, | |
1766 | }; | |
1767 | ||
1768 | const struct attribute_group *pci_dev_groups[] = { | |
1769 | &pci_dev_group, | |
1770 | NULL, | |
1771 | }; | |
1772 | ||
1773 | static const struct attribute_group pci_bridge_group = { | |
1774 | .attrs = pci_bridge_attrs, | |
1775 | }; | |
1776 | ||
1777 | const struct attribute_group *pci_bridge_groups[] = { | |
1778 | &pci_bridge_group, | |
1779 | NULL, | |
1780 | }; | |
1781 | ||
1782 | static const struct attribute_group pcie_dev_group = { | |
1783 | .attrs = pcie_dev_attrs, | |
1784 | }; | |
1785 | ||
1786 | const struct attribute_group *pcie_dev_groups[] = { | |
1787 | &pcie_dev_group, | |
1788 | NULL, | |
1789 | }; | |
1790 | ||
e7ea9825 | 1791 | static const struct attribute_group pci_dev_hp_attr_group = { |
dfab88be JL |
1792 | .attrs = pci_dev_hp_attrs, |
1793 | .is_visible = pci_dev_hp_attrs_are_visible, | |
1794 | }; | |
1795 | ||
1789382a DD |
1796 | #ifdef CONFIG_PCI_IOV |
1797 | static struct attribute *sriov_dev_attrs[] = { | |
1798 | &sriov_totalvfs_attr.attr, | |
1799 | &sriov_numvfs_attr.attr, | |
7dfca152 FS |
1800 | &sriov_offset_attr.attr, |
1801 | &sriov_stride_attr.attr, | |
1802 | &sriov_vf_device_attr.attr, | |
0e7df224 | 1803 | &sriov_drivers_autoprobe_attr.attr, |
1789382a DD |
1804 | NULL, |
1805 | }; | |
1806 | ||
1807 | static umode_t sriov_attrs_are_visible(struct kobject *kobj, | |
3c78bc61 | 1808 | struct attribute *a, int n) |
1789382a | 1809 | { |
554a6037 | 1810 | struct device *dev = kobj_to_dev(kobj); |
1789382a DD |
1811 | |
1812 | if (!dev_is_pf(dev)) | |
1813 | return 0; | |
1814 | ||
1815 | return a->mode; | |
1816 | } | |
1817 | ||
e7ea9825 | 1818 | static const struct attribute_group sriov_dev_attr_group = { |
1789382a DD |
1819 | .attrs = sriov_dev_attrs, |
1820 | .is_visible = sriov_attrs_are_visible, | |
1821 | }; | |
1822 | #endif /* CONFIG_PCI_IOV */ | |
1823 | ||
e7ea9825 | 1824 | static const struct attribute_group pci_dev_attr_group = { |
4e15c46b YL |
1825 | .attrs = pci_dev_dev_attrs, |
1826 | .is_visible = pci_dev_attrs_are_visible, | |
1827 | }; | |
1828 | ||
e7ea9825 | 1829 | static const struct attribute_group pci_bridge_attr_group = { |
56c1af46 WVK |
1830 | .attrs = pci_bridge_attrs, |
1831 | .is_visible = pci_bridge_attrs_are_visible, | |
1832 | }; | |
1833 | ||
e7ea9825 | 1834 | static const struct attribute_group pcie_dev_attr_group = { |
56c1af46 WVK |
1835 | .attrs = pcie_dev_attrs, |
1836 | .is_visible = pcie_dev_attrs_are_visible, | |
1837 | }; | |
1838 | ||
4e15c46b YL |
1839 | static const struct attribute_group *pci_dev_attr_groups[] = { |
1840 | &pci_dev_attr_group, | |
dfab88be | 1841 | &pci_dev_hp_attr_group, |
1789382a DD |
1842 | #ifdef CONFIG_PCI_IOV |
1843 | &sriov_dev_attr_group, | |
1844 | #endif | |
56c1af46 WVK |
1845 | &pci_bridge_attr_group, |
1846 | &pcie_dev_attr_group, | |
4e15c46b YL |
1847 | NULL, |
1848 | }; | |
1849 | ||
69f2dc24 | 1850 | const struct device_type pci_dev_type = { |
4e15c46b YL |
1851 | .groups = pci_dev_attr_groups, |
1852 | }; |