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