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