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