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