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> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/stat.h> |
| 21 | #include <linux/topology.h> |
| 22 | #include <linux/mm.h> |
Alexey Dobriyan | aa0ac36 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 23 | #include <linux/capability.h> |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 24 | #include <linux/pci-aspm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "pci.h" |
| 26 | |
| 27 | static int sysfs_initialized; /* = 0 */ |
| 28 | |
| 29 | /* show configuration fields */ |
| 30 | #define pci_config_attr(field, format_string) \ |
| 31 | static ssize_t \ |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 32 | field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { \ |
| 34 | struct pci_dev *pdev; \ |
| 35 | \ |
| 36 | pdev = to_pci_dev (dev); \ |
| 37 | return sprintf (buf, format_string, pdev->field); \ |
| 38 | } |
| 39 | |
| 40 | pci_config_attr(vendor, "0x%04x\n"); |
| 41 | pci_config_attr(device, "0x%04x\n"); |
| 42 | pci_config_attr(subsystem_vendor, "0x%04x\n"); |
| 43 | pci_config_attr(subsystem_device, "0x%04x\n"); |
| 44 | pci_config_attr(class, "0x%06x\n"); |
| 45 | pci_config_attr(irq, "%u\n"); |
| 46 | |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 47 | static ssize_t broken_parity_status_show(struct device *dev, |
| 48 | struct device_attribute *attr, |
| 49 | char *buf) |
| 50 | { |
| 51 | struct pci_dev *pdev = to_pci_dev(dev); |
| 52 | return sprintf (buf, "%u\n", pdev->broken_parity_status); |
| 53 | } |
| 54 | |
| 55 | static ssize_t broken_parity_status_store(struct device *dev, |
| 56 | struct device_attribute *attr, |
| 57 | const char *buf, size_t count) |
| 58 | { |
| 59 | struct pci_dev *pdev = to_pci_dev(dev); |
| 60 | ssize_t consumed = -EINVAL; |
| 61 | |
| 62 | if ((count > 0) && (*buf == '0' || *buf == '1')) { |
| 63 | pdev->broken_parity_status = *buf == '1' ? 1 : 0; |
| 64 | consumed = count; |
| 65 | } |
| 66 | return consumed; |
| 67 | } |
| 68 | |
Alan Cox | 4327edf | 2005-09-10 00:25:49 -0700 | [diff] [blame] | 69 | static ssize_t local_cpus_show(struct device *dev, |
| 70 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Alan Cox | 4327edf | 2005-09-10 00:25:49 -0700 | [diff] [blame] | 72 | cpumask_t mask; |
| 73 | int len; |
| 74 | |
| 75 | mask = pcibus_to_cpumask(to_pci_dev(dev)->bus); |
| 76 | len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | strcat(buf,"\n"); |
| 78 | return 1+len; |
| 79 | } |
| 80 | |
| 81 | /* show resources */ |
| 82 | static ssize_t |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 83 | resource_show(struct device * dev, struct device_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | struct pci_dev * pci_dev = to_pci_dev(dev); |
| 86 | char * str = buf; |
| 87 | int i; |
| 88 | int max = 7; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 89 | resource_size_t start, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | if (pci_dev->subordinate) |
| 92 | max = DEVICE_COUNT_RESOURCE; |
| 93 | |
| 94 | for (i = 0; i < max; i++) { |
Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 95 | struct resource *res = &pci_dev->resource[i]; |
| 96 | pci_resource_to_user(pci_dev, i, res, &start, &end); |
| 97 | str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n", |
| 98 | (unsigned long long)start, |
| 99 | (unsigned long long)end, |
| 100 | (unsigned long long)res->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | return (str - buf); |
| 103 | } |
| 104 | |
Greg Kroah-Hartman | 87c8a44 | 2005-06-19 12:21:43 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
| 107 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 108 | |
| 109 | return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n", |
| 110 | pci_dev->vendor, pci_dev->device, |
| 111 | pci_dev->subsystem_vendor, pci_dev->subsystem_device, |
| 112 | (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), |
| 113 | (u8)(pci_dev->class)); |
| 114 | } |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 115 | |
| 116 | static ssize_t is_enabled_store(struct device *dev, |
| 117 | struct device_attribute *attr, const char *buf, |
| 118 | size_t count) |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 119 | { |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 120 | ssize_t result = -EINVAL; |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 121 | struct pci_dev *pdev = to_pci_dev(dev); |
| 122 | |
| 123 | /* this can crash the machine when done on the "wrong" device */ |
| 124 | if (!capable(CAP_SYS_ADMIN)) |
| 125 | return count; |
| 126 | |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 127 | if (*buf == '0') { |
| 128 | if (atomic_read(&pdev->enable_cnt) != 0) |
| 129 | pci_disable_device(pdev); |
| 130 | else |
| 131 | result = -EIO; |
| 132 | } else if (*buf == '1') |
| 133 | result = pci_enable_device(pdev); |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 134 | |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 135 | return result < 0 ? result : count; |
| 136 | } |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 137 | |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 138 | static ssize_t is_enabled_show(struct device *dev, |
| 139 | struct device_attribute *attr, char *buf) |
| 140 | { |
| 141 | struct pci_dev *pdev; |
| 142 | |
| 143 | pdev = to_pci_dev (dev); |
| 144 | return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt)); |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 147 | #ifdef CONFIG_NUMA |
| 148 | static ssize_t |
| 149 | numa_node_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 150 | { |
| 151 | return sprintf (buf, "%d\n", dev->numa_node); |
| 152 | } |
| 153 | #endif |
| 154 | |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 155 | static ssize_t |
| 156 | msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 157 | { |
| 158 | struct pci_dev *pdev = to_pci_dev(dev); |
| 159 | |
| 160 | if (!pdev->subordinate) |
| 161 | return 0; |
| 162 | |
| 163 | return sprintf (buf, "%u\n", |
| 164 | !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)); |
| 165 | } |
| 166 | |
| 167 | static ssize_t |
| 168 | msi_bus_store(struct device *dev, struct device_attribute *attr, |
| 169 | const char *buf, size_t count) |
| 170 | { |
| 171 | struct pci_dev *pdev = to_pci_dev(dev); |
| 172 | |
| 173 | /* bad things may happen if the no_msi flag is changed |
| 174 | * while some drivers are loaded */ |
| 175 | if (!capable(CAP_SYS_ADMIN)) |
| 176 | return count; |
| 177 | |
| 178 | if (!pdev->subordinate) |
| 179 | return count; |
| 180 | |
| 181 | if (*buf == '0') { |
| 182 | pdev->subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; |
| 183 | dev_warn(&pdev->dev, "forced subordinate bus to not support MSI," |
| 184 | " bad things could happen.\n"); |
| 185 | } |
| 186 | |
| 187 | if (*buf == '1') { |
| 188 | pdev->subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; |
| 189 | dev_warn(&pdev->dev, "forced subordinate bus to support MSI," |
| 190 | " bad things could happen.\n"); |
| 191 | } |
| 192 | |
| 193 | return count; |
| 194 | } |
Greg KH | 9888549 | 2005-05-05 11:57:25 -0700 | [diff] [blame] | 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | struct device_attribute pci_dev_attrs[] = { |
| 197 | __ATTR_RO(resource), |
| 198 | __ATTR_RO(vendor), |
| 199 | __ATTR_RO(device), |
| 200 | __ATTR_RO(subsystem_vendor), |
| 201 | __ATTR_RO(subsystem_device), |
| 202 | __ATTR_RO(class), |
| 203 | __ATTR_RO(irq), |
| 204 | __ATTR_RO(local_cpus), |
Greg KH | 9888549 | 2005-05-05 11:57:25 -0700 | [diff] [blame] | 205 | __ATTR_RO(modalias), |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 206 | #ifdef CONFIG_NUMA |
| 207 | __ATTR_RO(numa_node), |
| 208 | #endif |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 209 | __ATTR(enable, 0600, is_enabled_show, is_enabled_store), |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 210 | __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR), |
| 211 | broken_parity_status_show,broken_parity_status_store), |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 212 | __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | __ATTR_NULL, |
| 214 | }; |
| 215 | |
| 216 | static ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 217 | pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 218 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
| 220 | struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj)); |
| 221 | unsigned int size = 64; |
| 222 | loff_t init_off = off; |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 223 | u8 *data = (u8*) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* Several chips lock up trying to read undefined config space */ |
| 226 | if (capable(CAP_SYS_ADMIN)) { |
| 227 | size = dev->cfg_size; |
| 228 | } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
| 229 | size = 128; |
| 230 | } |
| 231 | |
| 232 | if (off > size) |
| 233 | return 0; |
| 234 | if (off + count > size) { |
| 235 | size -= off; |
| 236 | count = size; |
| 237 | } else { |
| 238 | size = count; |
| 239 | } |
| 240 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 241 | if ((off & 1) && size) { |
| 242 | u8 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 243 | pci_user_read_config_byte(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 244 | data[off - init_off] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | off++; |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 246 | size--; |
| 247 | } |
| 248 | |
| 249 | if ((off & 3) && size > 2) { |
| 250 | u16 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 251 | pci_user_read_config_word(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 252 | data[off - init_off] = val & 0xff; |
| 253 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 254 | off += 2; |
| 255 | size -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | while (size > 3) { |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 259 | u32 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 260 | pci_user_read_config_dword(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 261 | data[off - init_off] = val & 0xff; |
| 262 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 263 | data[off - init_off + 2] = (val >> 16) & 0xff; |
| 264 | data[off - init_off + 3] = (val >> 24) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | off += 4; |
| 266 | size -= 4; |
| 267 | } |
| 268 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 269 | if (size >= 2) { |
| 270 | u16 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 271 | pci_user_read_config_word(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 272 | data[off - init_off] = val & 0xff; |
| 273 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 274 | off += 2; |
| 275 | size -= 2; |
| 276 | } |
| 277 | |
| 278 | if (size > 0) { |
| 279 | u8 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 280 | pci_user_read_config_byte(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 281 | data[off - init_off] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | off++; |
| 283 | --size; |
| 284 | } |
| 285 | |
| 286 | return count; |
| 287 | } |
| 288 | |
| 289 | static ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 290 | pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 291 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
| 293 | struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj)); |
| 294 | unsigned int size = count; |
| 295 | loff_t init_off = off; |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 296 | u8 *data = (u8*) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | if (off > dev->cfg_size) |
| 299 | return 0; |
| 300 | if (off + count > dev->cfg_size) { |
| 301 | size = dev->cfg_size - off; |
| 302 | count = size; |
| 303 | } |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 304 | |
| 305 | if ((off & 1) && size) { |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 306 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | off++; |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 308 | size--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 310 | |
| 311 | if ((off & 3) && size > 2) { |
| 312 | u16 val = data[off - init_off]; |
| 313 | val |= (u16) data[off - init_off + 1] << 8; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 314 | pci_user_write_config_word(dev, off, val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 315 | off += 2; |
| 316 | size -= 2; |
| 317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | while (size > 3) { |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 320 | u32 val = data[off - init_off]; |
| 321 | val |= (u32) data[off - init_off + 1] << 8; |
| 322 | val |= (u32) data[off - init_off + 2] << 16; |
| 323 | val |= (u32) data[off - init_off + 3] << 24; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 324 | pci_user_write_config_dword(dev, off, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | off += 4; |
| 326 | size -= 4; |
| 327 | } |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 328 | |
| 329 | if (size >= 2) { |
| 330 | u16 val = data[off - init_off]; |
| 331 | val |= (u16) data[off - init_off + 1] << 8; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 332 | pci_user_write_config_word(dev, off, val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 333 | off += 2; |
| 334 | size -= 2; |
| 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 337 | if (size) { |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 338 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | off++; |
| 340 | --size; |
| 341 | } |
| 342 | |
| 343 | return count; |
| 344 | } |
| 345 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 346 | static ssize_t |
| 347 | pci_read_vpd(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 348 | char *buf, loff_t off, size_t count) |
| 349 | { |
| 350 | struct pci_dev *dev = |
| 351 | to_pci_dev(container_of(kobj, struct device, kobj)); |
| 352 | int end; |
| 353 | int ret; |
| 354 | |
| 355 | if (off > bin_attr->size) |
| 356 | count = 0; |
| 357 | else if (count > bin_attr->size - off) |
| 358 | count = bin_attr->size - off; |
| 359 | end = off + count; |
| 360 | |
| 361 | while (off < end) { |
| 362 | ret = dev->vpd->ops->read(dev, off, end - off, buf); |
| 363 | if (ret < 0) |
| 364 | return ret; |
| 365 | buf += ret; |
| 366 | off += ret; |
| 367 | } |
| 368 | |
| 369 | return count; |
| 370 | } |
| 371 | |
| 372 | static ssize_t |
| 373 | pci_write_vpd(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 374 | char *buf, loff_t off, size_t count) |
| 375 | { |
| 376 | struct pci_dev *dev = |
| 377 | to_pci_dev(container_of(kobj, struct device, kobj)); |
| 378 | int end; |
| 379 | int ret; |
| 380 | |
| 381 | if (off > bin_attr->size) |
| 382 | count = 0; |
| 383 | else if (count > bin_attr->size - off) |
| 384 | count = bin_attr->size - off; |
| 385 | end = off + count; |
| 386 | |
| 387 | while (off < end) { |
| 388 | ret = dev->vpd->ops->write(dev, off, end - off, buf); |
| 389 | if (ret < 0) |
| 390 | return ret; |
| 391 | buf += ret; |
| 392 | off += ret; |
| 393 | } |
| 394 | |
| 395 | return count; |
| 396 | } |
| 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | #ifdef HAVE_PCI_LEGACY |
| 399 | /** |
| 400 | * pci_read_legacy_io - read byte(s) from legacy I/O port space |
| 401 | * @kobj: kobject corresponding to file to read from |
| 402 | * @buf: buffer to store results |
| 403 | * @off: offset into legacy I/O port space |
| 404 | * @count: number of bytes to read |
| 405 | * |
| 406 | * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific |
| 407 | * callback routine (pci_legacy_read). |
| 408 | */ |
| 409 | ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 410 | pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 411 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | struct pci_bus *bus = to_pci_bus(container_of(kobj, |
Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 414 | struct device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | kobj)); |
| 416 | |
| 417 | /* Only support 1, 2 or 4 byte accesses */ |
| 418 | if (count != 1 && count != 2 && count != 4) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | return pci_legacy_read(bus, off, (u32 *)buf, count); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * pci_write_legacy_io - write byte(s) to legacy I/O port space |
| 426 | * @kobj: kobject corresponding to file to read from |
| 427 | * @buf: buffer containing value to be written |
| 428 | * @off: offset into legacy I/O port space |
| 429 | * @count: number of bytes to write |
| 430 | * |
| 431 | * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific |
| 432 | * callback routine (pci_legacy_write). |
| 433 | */ |
| 434 | ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 435 | pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 436 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
| 438 | struct pci_bus *bus = to_pci_bus(container_of(kobj, |
Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 439 | struct device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | kobj)); |
| 441 | /* Only support 1, 2 or 4 byte accesses */ |
| 442 | if (count != 1 && count != 2 && count != 4) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | return pci_legacy_write(bus, off, *(u32 *)buf, count); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * pci_mmap_legacy_mem - map legacy PCI memory into user memory space |
| 450 | * @kobj: kobject corresponding to device to be mapped |
| 451 | * @attr: struct bin_attribute for this file |
| 452 | * @vma: struct vm_area_struct passed to mmap |
| 453 | * |
| 454 | * Uses an arch specific callback, pci_mmap_legacy_page_range, to mmap |
| 455 | * legacy memory space (first meg of bus space) into application virtual |
| 456 | * memory space. |
| 457 | */ |
| 458 | int |
| 459 | pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr, |
| 460 | struct vm_area_struct *vma) |
| 461 | { |
| 462 | struct pci_bus *bus = to_pci_bus(container_of(kobj, |
Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 463 | struct device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | kobj)); |
| 465 | |
| 466 | return pci_mmap_legacy_page_range(bus, vma); |
| 467 | } |
| 468 | #endif /* HAVE_PCI_LEGACY */ |
| 469 | |
| 470 | #ifdef HAVE_PCI_MMAP |
| 471 | /** |
| 472 | * pci_mmap_resource - map a PCI resource into user memory space |
| 473 | * @kobj: kobject for mapping |
| 474 | * @attr: struct bin_attribute for the file being mapped |
| 475 | * @vma: struct vm_area_struct passed into the mmap |
| 476 | * |
| 477 | * Use the regular PCI mapping routines to map a PCI resource into userspace. |
| 478 | * FIXME: write combining? maybe automatic for prefetchable regions? |
| 479 | */ |
| 480 | static int |
| 481 | pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, |
| 482 | struct vm_area_struct *vma) |
| 483 | { |
| 484 | struct pci_dev *pdev = to_pci_dev(container_of(kobj, |
| 485 | struct device, kobj)); |
| 486 | struct resource *res = (struct resource *)attr->private; |
| 487 | enum pci_mmap_state mmap_type; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 488 | resource_size_t start, end; |
Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 489 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 491 | for (i = 0; i < PCI_ROM_RESOURCE; i++) |
| 492 | if (res == &pdev->resource[i]) |
| 493 | break; |
| 494 | if (i >= PCI_ROM_RESOURCE) |
| 495 | return -ENODEV; |
| 496 | |
| 497 | /* pci_mmap_page_range() expects the same kind of entry as coming |
| 498 | * from /proc/bus/pci/ which is a "user visible" value. If this is |
| 499 | * different from the resource itself, arch will do necessary fixup. |
| 500 | */ |
| 501 | pci_resource_to_user(pdev, i, res, &start, &end); |
| 502 | vma->vm_pgoff += start >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; |
| 504 | |
| 505 | return pci_mmap_page_range(pdev, vma, mmap_type, 0); |
| 506 | } |
| 507 | |
| 508 | /** |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 509 | * pci_remove_resource_files - cleanup resource files |
| 510 | * @dev: dev to cleanup |
| 511 | * |
| 512 | * If we created resource files for @dev, remove them from sysfs and |
| 513 | * free their resources. |
| 514 | */ |
| 515 | static void |
| 516 | pci_remove_resource_files(struct pci_dev *pdev) |
| 517 | { |
| 518 | int i; |
| 519 | |
| 520 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { |
| 521 | struct bin_attribute *res_attr; |
| 522 | |
| 523 | res_attr = pdev->res_attr[i]; |
| 524 | if (res_attr) { |
| 525 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); |
| 526 | kfree(res_attr); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | * pci_create_resource_files - create resource files in sysfs for @dev |
| 533 | * @dev: dev in question |
| 534 | * |
| 535 | * Walk the resources in @dev creating files for each resource available. |
| 536 | */ |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 537 | static int pci_create_resource_files(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
| 539 | int i; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 540 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | /* Expose the PCI resources from this device as files */ |
| 543 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { |
| 544 | struct bin_attribute *res_attr; |
| 545 | |
| 546 | /* skip empty resources */ |
| 547 | if (!pci_resource_len(pdev, i)) |
| 548 | continue; |
| 549 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 550 | /* allocate attribute structure, piggyback attribute name */ |
Pekka Enberg | 656da9d | 2005-09-22 00:48:11 -0700 | [diff] [blame] | 551 | res_attr = kzalloc(sizeof(*res_attr) + 10, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (res_attr) { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 553 | char *res_attr_name = (char *)(res_attr + 1); |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | pdev->res_attr[i] = res_attr; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 556 | sprintf(res_attr_name, "resource%d", i); |
| 557 | res_attr->attr.name = res_attr_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | res_attr->attr.mode = S_IRUSR | S_IWUSR; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 559 | res_attr->size = pci_resource_len(pdev, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | res_attr->mmap = pci_mmap_resource; |
| 561 | res_attr->private = &pdev->resource[i]; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 562 | retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); |
| 563 | if (retval) { |
| 564 | pci_remove_resource_files(pdev); |
| 565 | return retval; |
| 566 | } |
| 567 | } else { |
| 568 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 571 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | #else /* !HAVE_PCI_MMAP */ |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 574 | static inline int pci_create_resource_files(struct pci_dev *dev) { return 0; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | static inline void pci_remove_resource_files(struct pci_dev *dev) { return; } |
| 576 | #endif /* HAVE_PCI_MMAP */ |
| 577 | |
| 578 | /** |
| 579 | * pci_write_rom - used to enable access to the PCI ROM display |
| 580 | * @kobj: kernel object handle |
| 581 | * @buf: user input |
| 582 | * @off: file offset |
| 583 | * @count: number of byte in input |
| 584 | * |
| 585 | * writing anything except 0 enables it |
| 586 | */ |
| 587 | static ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 588 | pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 589 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
| 591 | struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); |
| 592 | |
| 593 | if ((off == 0) && (*buf == '0') && (count == 2)) |
| 594 | pdev->rom_attr_enabled = 0; |
| 595 | else |
| 596 | pdev->rom_attr_enabled = 1; |
| 597 | |
| 598 | return count; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * pci_read_rom - read a PCI ROM |
| 603 | * @kobj: kernel object handle |
| 604 | * @buf: where to put the data we read from the ROM |
| 605 | * @off: file offset |
| 606 | * @count: number of bytes to read |
| 607 | * |
| 608 | * Put @count bytes starting at @off into @buf from the ROM in the PCI |
| 609 | * device corresponding to @kobj. |
| 610 | */ |
| 611 | static ssize_t |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 612 | pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr, |
| 613 | char *buf, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
| 615 | struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); |
| 616 | void __iomem *rom; |
| 617 | size_t size; |
| 618 | |
| 619 | if (!pdev->rom_attr_enabled) |
| 620 | return -EINVAL; |
| 621 | |
| 622 | rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ |
| 623 | if (!rom) |
| 624 | return 0; |
| 625 | |
| 626 | if (off >= size) |
| 627 | count = 0; |
| 628 | else { |
| 629 | if (off + count > size) |
| 630 | count = size - off; |
| 631 | |
| 632 | memcpy_fromio(buf, rom + off, count); |
| 633 | } |
| 634 | pci_unmap_rom(pdev, rom); |
| 635 | |
| 636 | return count; |
| 637 | } |
| 638 | |
| 639 | static struct bin_attribute pci_config_attr = { |
| 640 | .attr = { |
| 641 | .name = "config", |
| 642 | .mode = S_IRUGO | S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | }, |
| 644 | .size = 256, |
| 645 | .read = pci_read_config, |
| 646 | .write = pci_write_config, |
| 647 | }; |
| 648 | |
| 649 | static struct bin_attribute pcie_config_attr = { |
| 650 | .attr = { |
| 651 | .name = "config", |
| 652 | .mode = S_IRUGO | S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | }, |
| 654 | .size = 4096, |
| 655 | .read = pci_read_config, |
| 656 | .write = pci_write_config, |
| 657 | }; |
| 658 | |
Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 659 | int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) |
Michael Ellerman | 575e334 | 2007-05-08 12:03:07 +1000 | [diff] [blame] | 660 | { |
Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 661 | return 0; |
Michael Ellerman | 575e334 | 2007-05-08 12:03:07 +1000 | [diff] [blame] | 662 | } |
| 663 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 664 | int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 666 | struct bin_attribute *attr = NULL; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 667 | int retval; |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (!sysfs_initialized) |
| 670 | return -EACCES; |
| 671 | |
| 672 | if (pdev->cfg_size < 4096) |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 673 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | else |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 675 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
| 676 | if (retval) |
| 677 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 679 | /* If the device has VPD, try to expose it in sysfs. */ |
| 680 | if (pdev->vpd) { |
| 681 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
| 682 | if (attr) { |
| 683 | pdev->vpd->attr = attr; |
| 684 | attr->size = pdev->vpd->ops->get_size(pdev); |
| 685 | attr->attr.name = "vpd"; |
| 686 | attr->attr.mode = S_IRUGO | S_IWUSR; |
| 687 | attr->read = pci_read_vpd; |
| 688 | attr->write = pci_write_vpd; |
| 689 | retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); |
| 690 | if (retval) |
| 691 | goto err_vpd; |
| 692 | } else { |
| 693 | retval = -ENOMEM; |
| 694 | goto err_config_file; |
| 695 | } |
| 696 | } |
| 697 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 698 | retval = pci_create_resource_files(pdev); |
| 699 | if (retval) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 700 | goto err_vpd_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /* If the device has a ROM, try to expose it in sysfs. */ |
Jesse Barnes | 40ee9e9 | 2007-03-24 11:03:32 -0700 | [diff] [blame] | 703 | if (pci_resource_len(pdev, PCI_ROM_RESOURCE) || |
| 704 | (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)) { |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 705 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
| 706 | if (attr) { |
| 707 | pdev->rom_attr = attr; |
| 708 | attr->size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
| 709 | attr->attr.name = "rom"; |
| 710 | attr->attr.mode = S_IRUSR; |
| 711 | attr->read = pci_read_rom; |
| 712 | attr->write = pci_write_rom; |
| 713 | retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 714 | if (retval) |
| 715 | goto err_rom; |
| 716 | } else { |
| 717 | retval = -ENOMEM; |
Michael Ellerman | 9890b12 | 2007-04-18 13:34:12 +1000 | [diff] [blame] | 718 | goto err_resource_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | /* add platform-specific attributes */ |
Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 722 | if (pcibios_add_platform_entries(pdev)) |
| 723 | goto err_rom_file; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 724 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 725 | pcie_aspm_create_sysfs_dev_files(pdev); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | return 0; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 728 | |
Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 729 | err_rom_file: |
| 730 | if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 731 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 732 | err_rom: |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 733 | kfree(pdev->rom_attr); |
Michael Ellerman | 9890b12 | 2007-04-18 13:34:12 +1000 | [diff] [blame] | 734 | err_resource_files: |
| 735 | pci_remove_resource_files(pdev); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 736 | err_vpd_file: |
| 737 | if (pdev->vpd) { |
| 738 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->vpd->attr); |
| 739 | err_vpd: |
| 740 | kfree(pdev->vpd->attr); |
| 741 | } |
| 742 | err_config_file: |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 743 | if (pdev->cfg_size < 4096) |
| 744 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); |
| 745 | else |
| 746 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
| 747 | err: |
| 748 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /** |
| 752 | * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files |
| 753 | * @pdev: device whose entries we should free |
| 754 | * |
| 755 | * Cleanup when @pdev is removed from sysfs. |
| 756 | */ |
| 757 | void pci_remove_sysfs_dev_files(struct pci_dev *pdev) |
| 758 | { |
David Miller | d67afe5 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 759 | if (!sysfs_initialized) |
| 760 | return; |
| 761 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 762 | pcie_aspm_remove_sysfs_dev_files(pdev); |
| 763 | |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame^] | 764 | if (pdev->vpd) { |
| 765 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->vpd->attr); |
| 766 | kfree(pdev->vpd->attr); |
| 767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | if (pdev->cfg_size < 4096) |
| 769 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); |
| 770 | else |
| 771 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
| 772 | |
| 773 | pci_remove_resource_files(pdev); |
| 774 | |
| 775 | if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) { |
| 776 | if (pdev->rom_attr) { |
| 777 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
| 778 | kfree(pdev->rom_attr); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static int __init pci_sysfs_init(void) |
| 784 | { |
| 785 | struct pci_dev *pdev = NULL; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 786 | int retval; |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | sysfs_initialized = 1; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 789 | for_each_pci_dev(pdev) { |
| 790 | retval = pci_create_sysfs_dev_files(pdev); |
Julia Lawall | 151fc5d | 2007-11-20 08:41:16 +0100 | [diff] [blame] | 791 | if (retval) { |
| 792 | pci_dev_put(pdev); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 793 | return retval; |
Julia Lawall | 151fc5d | 2007-11-20 08:41:16 +0100 | [diff] [blame] | 794 | } |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 795 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
Jesse Barnes | 40ee9e9 | 2007-03-24 11:03:32 -0700 | [diff] [blame] | 800 | late_initcall(pci_sysfs_init); |