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