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