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