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