| 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) { | 
| Yuji Shimada | 296ccb0 | 2009-04-03 16:41:46 +0900 | [diff] [blame] | 151 | if (pci_is_enabled(pdev)) | 
| Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 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 |  | 
| Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 222 | #ifdef CONFIG_HOTPLUG | 
|  | 223 | static DEFINE_MUTEX(pci_remove_rescan_mutex); | 
|  | 224 | static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, | 
|  | 225 | size_t count) | 
|  | 226 | { | 
|  | 227 | unsigned long val; | 
|  | 228 | struct pci_bus *b = NULL; | 
|  | 229 |  | 
|  | 230 | if (strict_strtoul(buf, 0, &val) < 0) | 
|  | 231 | return -EINVAL; | 
|  | 232 |  | 
|  | 233 | if (val) { | 
|  | 234 | mutex_lock(&pci_remove_rescan_mutex); | 
|  | 235 | while ((b = pci_find_next_bus(b)) != NULL) | 
|  | 236 | pci_rescan_bus(b); | 
|  | 237 | mutex_unlock(&pci_remove_rescan_mutex); | 
|  | 238 | } | 
|  | 239 | return count; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | struct bus_attribute pci_bus_attrs[] = { | 
|  | 243 | __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store), | 
|  | 244 | __ATTR_NULL | 
|  | 245 | }; | 
| Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 246 |  | 
| Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 247 | static ssize_t | 
|  | 248 | dev_rescan_store(struct device *dev, struct device_attribute *attr, | 
|  | 249 | const char *buf, size_t count) | 
|  | 250 | { | 
|  | 251 | unsigned long val; | 
|  | 252 | struct pci_dev *pdev = to_pci_dev(dev); | 
|  | 253 |  | 
|  | 254 | if (strict_strtoul(buf, 0, &val) < 0) | 
|  | 255 | return -EINVAL; | 
|  | 256 |  | 
|  | 257 | if (val) { | 
|  | 258 | mutex_lock(&pci_remove_rescan_mutex); | 
|  | 259 | pci_rescan_bus(pdev->bus); | 
|  | 260 | mutex_unlock(&pci_remove_rescan_mutex); | 
|  | 261 | } | 
|  | 262 | return count; | 
|  | 263 | } | 
|  | 264 |  | 
| Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 265 | static void remove_callback(struct device *dev) | 
|  | 266 | { | 
|  | 267 | struct pci_dev *pdev = to_pci_dev(dev); | 
|  | 268 |  | 
|  | 269 | mutex_lock(&pci_remove_rescan_mutex); | 
|  | 270 | pci_remove_bus_device(pdev); | 
|  | 271 | mutex_unlock(&pci_remove_rescan_mutex); | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | static ssize_t | 
|  | 275 | remove_store(struct device *dev, struct device_attribute *dummy, | 
|  | 276 | const char *buf, size_t count) | 
|  | 277 | { | 
|  | 278 | int ret = 0; | 
|  | 279 | unsigned long val; | 
| Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 280 |  | 
|  | 281 | if (strict_strtoul(buf, 0, &val) < 0) | 
|  | 282 | return -EINVAL; | 
|  | 283 |  | 
| Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 284 | /* An attribute cannot be unregistered by one of its own methods, | 
|  | 285 | * so we have to use this roundabout approach. | 
|  | 286 | */ | 
|  | 287 | if (val) | 
|  | 288 | ret = device_schedule_callback(dev, remove_callback); | 
|  | 289 | if (ret) | 
|  | 290 | count = ret; | 
|  | 291 | return count; | 
|  | 292 | } | 
| Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 293 | #endif | 
|  | 294 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | struct device_attribute pci_dev_attrs[] = { | 
|  | 296 | __ATTR_RO(resource), | 
|  | 297 | __ATTR_RO(vendor), | 
|  | 298 | __ATTR_RO(device), | 
|  | 299 | __ATTR_RO(subsystem_vendor), | 
|  | 300 | __ATTR_RO(subsystem_device), | 
|  | 301 | __ATTR_RO(class), | 
|  | 302 | __ATTR_RO(irq), | 
|  | 303 | __ATTR_RO(local_cpus), | 
| Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 304 | __ATTR_RO(local_cpulist), | 
| Greg KH | 9888549 | 2005-05-05 11:57:25 -0700 | [diff] [blame] | 305 | __ATTR_RO(modalias), | 
| Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 306 | #ifdef CONFIG_NUMA | 
|  | 307 | __ATTR_RO(numa_node), | 
|  | 308 | #endif | 
| Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 309 | __ATTR(enable, 0600, is_enabled_show, is_enabled_store), | 
| Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 310 | __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR), | 
|  | 311 | broken_parity_status_show,broken_parity_status_store), | 
| Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 312 | __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store), | 
| Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 313 | #ifdef CONFIG_HOTPLUG | 
|  | 314 | __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store), | 
| Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 315 | __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store), | 
| Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 316 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | __ATTR_NULL, | 
|  | 318 | }; | 
|  | 319 |  | 
|  | 320 | static ssize_t | 
| Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 321 | boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf) | 
|  | 322 | { | 
|  | 323 | struct pci_dev *pdev = to_pci_dev(dev); | 
|  | 324 |  | 
|  | 325 | return sprintf(buf, "%u\n", | 
|  | 326 | !!(pdev->resource[PCI_ROM_RESOURCE].flags & | 
|  | 327 | IORESOURCE_ROM_SHADOW)); | 
|  | 328 | } | 
|  | 329 | struct device_attribute vga_attr = __ATTR_RO(boot_vga); | 
|  | 330 |  | 
|  | 331 | static ssize_t | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 332 | pci_read_config(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 333 | char *buf, loff_t off, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { | 
|  | 335 | struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj)); | 
|  | 336 | unsigned int size = 64; | 
|  | 337 | loff_t init_off = off; | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 338 | u8 *data = (u8*) buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 |  | 
|  | 340 | /* Several chips lock up trying to read undefined config space */ | 
|  | 341 | if (capable(CAP_SYS_ADMIN)) { | 
|  | 342 | size = dev->cfg_size; | 
|  | 343 | } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { | 
|  | 344 | size = 128; | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | if (off > size) | 
|  | 348 | return 0; | 
|  | 349 | if (off + count > size) { | 
|  | 350 | size -= off; | 
|  | 351 | count = size; | 
|  | 352 | } else { | 
|  | 353 | size = count; | 
|  | 354 | } | 
|  | 355 |  | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 356 | if ((off & 1) && size) { | 
|  | 357 | u8 val; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 358 | pci_user_read_config_byte(dev, off, &val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 359 | data[off - init_off] = val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | off++; | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 361 | size--; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | if ((off & 3) && size > 2) { | 
|  | 365 | u16 val; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 366 | pci_user_read_config_word(dev, off, &val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 367 | data[off - init_off] = val & 0xff; | 
|  | 368 | data[off - init_off + 1] = (val >> 8) & 0xff; | 
|  | 369 | off += 2; | 
|  | 370 | size -= 2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } | 
|  | 372 |  | 
|  | 373 | while (size > 3) { | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 374 | u32 val; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 375 | pci_user_read_config_dword(dev, off, &val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 376 | data[off - init_off] = val & 0xff; | 
|  | 377 | data[off - init_off + 1] = (val >> 8) & 0xff; | 
|  | 378 | data[off - init_off + 2] = (val >> 16) & 0xff; | 
|  | 379 | data[off - init_off + 3] = (val >> 24) & 0xff; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | off += 4; | 
|  | 381 | size -= 4; | 
|  | 382 | } | 
|  | 383 |  | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 384 | if (size >= 2) { | 
|  | 385 | u16 val; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 386 | pci_user_read_config_word(dev, off, &val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 387 | data[off - init_off] = val & 0xff; | 
|  | 388 | data[off - init_off + 1] = (val >> 8) & 0xff; | 
|  | 389 | off += 2; | 
|  | 390 | size -= 2; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | if (size > 0) { | 
|  | 394 | u8 val; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 395 | pci_user_read_config_byte(dev, off, &val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 396 | data[off - init_off] = val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | off++; | 
|  | 398 | --size; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | return count; | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | static ssize_t | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 405 | pci_write_config(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 406 | char *buf, loff_t off, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { | 
|  | 408 | struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj)); | 
|  | 409 | unsigned int size = count; | 
|  | 410 | loff_t init_off = off; | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 411 | u8 *data = (u8*) buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 |  | 
|  | 413 | if (off > dev->cfg_size) | 
|  | 414 | return 0; | 
|  | 415 | if (off + count > dev->cfg_size) { | 
|  | 416 | size = dev->cfg_size - off; | 
|  | 417 | count = size; | 
|  | 418 | } | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 419 |  | 
|  | 420 | if ((off & 1) && size) { | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 421 | pci_user_write_config_byte(dev, off, data[off - init_off]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | off++; | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 423 | size--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 425 |  | 
|  | 426 | if ((off & 3) && size > 2) { | 
|  | 427 | u16 val = data[off - init_off]; | 
|  | 428 | val |= (u16) data[off - init_off + 1] << 8; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 429 | pci_user_write_config_word(dev, off, val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 430 | off += 2; | 
|  | 431 | size -= 2; | 
|  | 432 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 |  | 
|  | 434 | while (size > 3) { | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 435 | u32 val = data[off - init_off]; | 
|  | 436 | val |= (u32) data[off - init_off + 1] << 8; | 
|  | 437 | val |= (u32) data[off - init_off + 2] << 16; | 
|  | 438 | val |= (u32) data[off - init_off + 3] << 24; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 439 | pci_user_write_config_dword(dev, off, val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | off += 4; | 
|  | 441 | size -= 4; | 
|  | 442 | } | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 443 |  | 
|  | 444 | if (size >= 2) { | 
|  | 445 | u16 val = data[off - init_off]; | 
|  | 446 | val |= (u16) data[off - init_off + 1] << 8; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 447 | pci_user_write_config_word(dev, off, val); | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 448 | off += 2; | 
|  | 449 | size -= 2; | 
|  | 450 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 |  | 
| ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 452 | if (size) { | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 453 | pci_user_write_config_byte(dev, off, data[off - init_off]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | off++; | 
|  | 455 | --size; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | return count; | 
|  | 459 | } | 
|  | 460 |  | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 461 | static ssize_t | 
| Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 462 | read_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr, | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 463 | char *buf, loff_t off, size_t count) | 
|  | 464 | { | 
|  | 465 | struct pci_dev *dev = | 
|  | 466 | to_pci_dev(container_of(kobj, struct device, kobj)); | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 467 |  | 
|  | 468 | if (off > bin_attr->size) | 
|  | 469 | count = 0; | 
|  | 470 | else if (count > bin_attr->size - off) | 
|  | 471 | count = bin_attr->size - off; | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 472 |  | 
| Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 473 | return pci_read_vpd(dev, off, count, buf); | 
|  | 474 | } | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 475 |  | 
| Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 476 | static ssize_t | 
|  | 477 | write_vpd_attr(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 478 | char *buf, loff_t off, size_t count) | 
|  | 479 | { | 
|  | 480 | struct pci_dev *dev = | 
|  | 481 | to_pci_dev(container_of(kobj, struct device, kobj)); | 
|  | 482 |  | 
|  | 483 | if (off > bin_attr->size) | 
|  | 484 | count = 0; | 
|  | 485 | else if (count > bin_attr->size - off) | 
|  | 486 | count = bin_attr->size - off; | 
|  | 487 |  | 
|  | 488 | return pci_write_vpd(dev, off, count, buf); | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 489 | } | 
|  | 490 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | #ifdef HAVE_PCI_LEGACY | 
|  | 492 | /** | 
|  | 493 | * pci_read_legacy_io - read byte(s) from legacy I/O port space | 
|  | 494 | * @kobj: kobject corresponding to file to read from | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 495 | * @bin_attr: struct bin_attribute for this file | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | * @buf: buffer to store results | 
|  | 497 | * @off: offset into legacy I/O port space | 
|  | 498 | * @count: number of bytes to read | 
|  | 499 | * | 
|  | 500 | * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific | 
|  | 501 | * callback routine (pci_legacy_read). | 
|  | 502 | */ | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 503 | static ssize_t | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 504 | pci_read_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 505 | char *buf, loff_t off, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { | 
|  | 507 | struct pci_bus *bus = to_pci_bus(container_of(kobj, | 
| Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 508 | struct device, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | kobj)); | 
|  | 510 |  | 
|  | 511 | /* Only support 1, 2 or 4 byte accesses */ | 
|  | 512 | if (count != 1 && count != 2 && count != 4) | 
|  | 513 | return -EINVAL; | 
|  | 514 |  | 
|  | 515 | return pci_legacy_read(bus, off, (u32 *)buf, count); | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | /** | 
|  | 519 | * pci_write_legacy_io - write byte(s) to legacy I/O port space | 
|  | 520 | * @kobj: kobject corresponding to file to read from | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 521 | * @bin_attr: struct bin_attribute for this file | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | * @buf: buffer containing value to be written | 
|  | 523 | * @off: offset into legacy I/O port space | 
|  | 524 | * @count: number of bytes to write | 
|  | 525 | * | 
|  | 526 | * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific | 
|  | 527 | * callback routine (pci_legacy_write). | 
|  | 528 | */ | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 529 | static ssize_t | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 530 | pci_write_legacy_io(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 531 | char *buf, loff_t off, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { | 
|  | 533 | struct pci_bus *bus = to_pci_bus(container_of(kobj, | 
| Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 534 | struct device, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | kobj)); | 
|  | 536 | /* Only support 1, 2 or 4 byte accesses */ | 
|  | 537 | if (count != 1 && count != 2 && count != 4) | 
|  | 538 | return -EINVAL; | 
|  | 539 |  | 
|  | 540 | return pci_legacy_write(bus, off, *(u32 *)buf, count); | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | /** | 
|  | 544 | * pci_mmap_legacy_mem - map legacy PCI memory into user memory space | 
|  | 545 | * @kobj: kobject corresponding to device to be mapped | 
|  | 546 | * @attr: struct bin_attribute for this file | 
|  | 547 | * @vma: struct vm_area_struct passed to mmap | 
|  | 548 | * | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 549 | * 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] | 550 | * legacy memory space (first meg of bus space) into application virtual | 
|  | 551 | * memory space. | 
|  | 552 | */ | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 553 | static int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | pci_mmap_legacy_mem(struct kobject *kobj, struct bin_attribute *attr, | 
|  | 555 | struct vm_area_struct *vma) | 
|  | 556 | { | 
|  | 557 | struct pci_bus *bus = to_pci_bus(container_of(kobj, | 
| Greg Kroah-Hartman | fd7d1ce | 2007-05-22 22:47:54 -0400 | [diff] [blame] | 558 | struct device, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | kobj)); | 
|  | 560 |  | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 561 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); | 
|  | 562 | } | 
|  | 563 |  | 
|  | 564 | /** | 
|  | 565 | * pci_mmap_legacy_io - map legacy PCI IO into user memory space | 
|  | 566 | * @kobj: kobject corresponding to device to be mapped | 
|  | 567 | * @attr: struct bin_attribute for this file | 
|  | 568 | * @vma: struct vm_area_struct passed to mmap | 
|  | 569 | * | 
|  | 570 | * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap | 
|  | 571 | * legacy IO space (first meg of bus space) into application virtual | 
|  | 572 | * memory space. Returns -ENOSYS if the operation isn't supported | 
|  | 573 | */ | 
|  | 574 | static int | 
|  | 575 | pci_mmap_legacy_io(struct kobject *kobj, struct bin_attribute *attr, | 
|  | 576 | struct vm_area_struct *vma) | 
|  | 577 | { | 
|  | 578 | struct pci_bus *bus = to_pci_bus(container_of(kobj, | 
|  | 579 | struct device, | 
|  | 580 | kobj)); | 
|  | 581 |  | 
|  | 582 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | /** | 
| Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 586 | * pci_adjust_legacy_attr - adjustment of legacy file attributes | 
|  | 587 | * @b: bus to create files under | 
|  | 588 | * @mmap_type: I/O port or memory | 
|  | 589 | * | 
|  | 590 | * Stub implementation. Can be overridden by arch if necessary. | 
|  | 591 | */ | 
|  | 592 | void __weak | 
|  | 593 | pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type) | 
|  | 594 | { | 
|  | 595 | return; | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | /** | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 599 | * pci_create_legacy_files - create legacy I/O port and memory files | 
|  | 600 | * @b: bus to create files under | 
|  | 601 | * | 
|  | 602 | * Some platforms allow access to legacy I/O port and ISA memory space on | 
|  | 603 | * a per-bus basis.  This routine creates the files and ties them into | 
|  | 604 | * their associated read, write and mmap files from pci-sysfs.c | 
|  | 605 | * | 
|  | 606 | * On error unwind, but don't propogate the error to the caller | 
|  | 607 | * as it is ok to set up the PCI bus without these files. | 
|  | 608 | */ | 
|  | 609 | void pci_create_legacy_files(struct pci_bus *b) | 
|  | 610 | { | 
|  | 611 | int error; | 
|  | 612 |  | 
|  | 613 | b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2, | 
|  | 614 | GFP_ATOMIC); | 
|  | 615 | if (!b->legacy_io) | 
|  | 616 | goto kzalloc_err; | 
|  | 617 |  | 
|  | 618 | b->legacy_io->attr.name = "legacy_io"; | 
|  | 619 | b->legacy_io->size = 0xffff; | 
|  | 620 | b->legacy_io->attr.mode = S_IRUSR | S_IWUSR; | 
|  | 621 | b->legacy_io->read = pci_read_legacy_io; | 
|  | 622 | b->legacy_io->write = pci_write_legacy_io; | 
|  | 623 | b->legacy_io->mmap = pci_mmap_legacy_io; | 
| Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 624 | pci_adjust_legacy_attr(b, pci_mmap_io); | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 625 | error = device_create_bin_file(&b->dev, b->legacy_io); | 
|  | 626 | if (error) | 
|  | 627 | goto legacy_io_err; | 
|  | 628 |  | 
|  | 629 | /* Allocated above after the legacy_io struct */ | 
|  | 630 | b->legacy_mem = b->legacy_io + 1; | 
|  | 631 | b->legacy_mem->attr.name = "legacy_mem"; | 
|  | 632 | b->legacy_mem->size = 1024*1024; | 
|  | 633 | b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR; | 
|  | 634 | b->legacy_mem->mmap = pci_mmap_legacy_mem; | 
| Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 635 | pci_adjust_legacy_attr(b, pci_mmap_mem); | 
| Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 636 | error = device_create_bin_file(&b->dev, b->legacy_mem); | 
|  | 637 | if (error) | 
|  | 638 | goto legacy_mem_err; | 
|  | 639 |  | 
|  | 640 | return; | 
|  | 641 |  | 
|  | 642 | legacy_mem_err: | 
|  | 643 | device_remove_bin_file(&b->dev, b->legacy_io); | 
|  | 644 | legacy_io_err: | 
|  | 645 | kfree(b->legacy_io); | 
|  | 646 | b->legacy_io = NULL; | 
|  | 647 | kzalloc_err: | 
|  | 648 | printk(KERN_WARNING "pci: warning: could not create legacy I/O port " | 
|  | 649 | "and ISA memory resources to sysfs\n"); | 
|  | 650 | return; | 
|  | 651 | } | 
|  | 652 |  | 
|  | 653 | void pci_remove_legacy_files(struct pci_bus *b) | 
|  | 654 | { | 
|  | 655 | if (b->legacy_io) { | 
|  | 656 | device_remove_bin_file(&b->dev, b->legacy_io); | 
|  | 657 | device_remove_bin_file(&b->dev, b->legacy_mem); | 
|  | 658 | kfree(b->legacy_io); /* both are allocated here */ | 
|  | 659 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } | 
|  | 661 | #endif /* HAVE_PCI_LEGACY */ | 
|  | 662 |  | 
|  | 663 | #ifdef HAVE_PCI_MMAP | 
| Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 664 |  | 
| Jesse Barnes | 9eff02e | 2008-10-24 10:32:33 -0700 | [diff] [blame] | 665 | 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] | 666 | { | 
|  | 667 | unsigned long nr, start, size; | 
|  | 668 |  | 
|  | 669 | nr = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; | 
|  | 670 | start = vma->vm_pgoff; | 
| Ed Swierk | 88e7df0 | 2008-11-03 14:41:16 -0800 | [diff] [blame] | 671 | size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1; | 
| Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 672 | if (start < size && size - start >= nr) | 
|  | 673 | return 1; | 
|  | 674 | WARN(1, "process \"%s\" tried to map 0x%08lx-0x%08lx on %s BAR %d (size 0x%08lx)\n", | 
|  | 675 | current->comm, start, start+nr, pci_name(pdev), resno, size); | 
|  | 676 | return 0; | 
|  | 677 | } | 
|  | 678 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /** | 
|  | 680 | * pci_mmap_resource - map a PCI resource into user memory space | 
|  | 681 | * @kobj: kobject for mapping | 
|  | 682 | * @attr: struct bin_attribute for the file being mapped | 
|  | 683 | * @vma: struct vm_area_struct passed into the mmap | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 684 | * @write_combine: 1 for write_combine mapping | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | * | 
|  | 686 | * 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] | 687 | */ | 
|  | 688 | static int | 
|  | 689 | pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 690 | struct vm_area_struct *vma, int write_combine) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { | 
|  | 692 | struct pci_dev *pdev = to_pci_dev(container_of(kobj, | 
|  | 693 | struct device, kobj)); | 
|  | 694 | struct resource *res = (struct resource *)attr->private; | 
|  | 695 | enum pci_mmap_state mmap_type; | 
| Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 696 | resource_size_t start, end; | 
| Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 697 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 |  | 
| Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 699 | for (i = 0; i < PCI_ROM_RESOURCE; i++) | 
|  | 700 | if (res == &pdev->resource[i]) | 
|  | 701 | break; | 
|  | 702 | if (i >= PCI_ROM_RESOURCE) | 
|  | 703 | return -ENODEV; | 
|  | 704 |  | 
| Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 705 | if (!pci_mmap_fits(pdev, i, vma)) | 
|  | 706 | return -EINVAL; | 
|  | 707 |  | 
| Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 708 | /* pci_mmap_page_range() expects the same kind of entry as coming | 
|  | 709 | * from /proc/bus/pci/ which is a "user visible" value. If this is | 
|  | 710 | * different from the resource itself, arch will do necessary fixup. | 
|  | 711 | */ | 
|  | 712 | pci_resource_to_user(pdev, i, res, &start, &end); | 
|  | 713 | vma->vm_pgoff += start >> PAGE_SHIFT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; | 
|  | 715 |  | 
| Arjan van de Ven | e8de148 | 2008-10-22 19:55:31 -0700 | [diff] [blame] | 716 | if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start)) | 
|  | 717 | return -EINVAL; | 
|  | 718 |  | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 719 | return pci_mmap_page_range(pdev, vma, mmap_type, write_combine); | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | static int | 
|  | 723 | pci_mmap_resource_uc(struct kobject *kobj, struct bin_attribute *attr, | 
|  | 724 | struct vm_area_struct *vma) | 
|  | 725 | { | 
|  | 726 | return pci_mmap_resource(kobj, attr, vma, 0); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | static int | 
|  | 730 | pci_mmap_resource_wc(struct kobject *kobj, struct bin_attribute *attr, | 
|  | 731 | struct vm_area_struct *vma) | 
|  | 732 | { | 
|  | 733 | return pci_mmap_resource(kobj, attr, vma, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } | 
|  | 735 |  | 
|  | 736 | /** | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 737 | * pci_remove_resource_files - cleanup resource files | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 738 | * @pdev: dev to cleanup | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 739 | * | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 740 | * If we created resource files for @pdev, remove them from sysfs and | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 741 | * free their resources. | 
|  | 742 | */ | 
|  | 743 | static void | 
|  | 744 | pci_remove_resource_files(struct pci_dev *pdev) | 
|  | 745 | { | 
|  | 746 | int i; | 
|  | 747 |  | 
|  | 748 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { | 
|  | 749 | struct bin_attribute *res_attr; | 
|  | 750 |  | 
|  | 751 | res_attr = pdev->res_attr[i]; | 
|  | 752 | if (res_attr) { | 
|  | 753 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); | 
|  | 754 | kfree(res_attr); | 
|  | 755 | } | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 756 |  | 
|  | 757 | res_attr = pdev->res_attr_wc[i]; | 
|  | 758 | if (res_attr) { | 
|  | 759 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); | 
|  | 760 | kfree(res_attr); | 
|  | 761 | } | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 762 | } | 
|  | 763 | } | 
|  | 764 |  | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 765 | static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) | 
|  | 766 | { | 
|  | 767 | /* allocate attribute structure, piggyback attribute name */ | 
|  | 768 | int name_len = write_combine ? 13 : 10; | 
|  | 769 | struct bin_attribute *res_attr; | 
|  | 770 | int retval; | 
|  | 771 |  | 
|  | 772 | res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); | 
|  | 773 | if (res_attr) { | 
|  | 774 | char *res_attr_name = (char *)(res_attr + 1); | 
|  | 775 |  | 
|  | 776 | if (write_combine) { | 
|  | 777 | pdev->res_attr_wc[num] = res_attr; | 
|  | 778 | sprintf(res_attr_name, "resource%d_wc", num); | 
|  | 779 | res_attr->mmap = pci_mmap_resource_wc; | 
|  | 780 | } else { | 
|  | 781 | pdev->res_attr[num] = res_attr; | 
|  | 782 | sprintf(res_attr_name, "resource%d", num); | 
|  | 783 | res_attr->mmap = pci_mmap_resource_uc; | 
|  | 784 | } | 
|  | 785 | res_attr->attr.name = res_attr_name; | 
|  | 786 | res_attr->attr.mode = S_IRUSR | S_IWUSR; | 
|  | 787 | res_attr->size = pci_resource_len(pdev, num); | 
|  | 788 | res_attr->private = &pdev->resource[num]; | 
|  | 789 | retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); | 
|  | 790 | } else | 
|  | 791 | retval = -ENOMEM; | 
|  | 792 |  | 
|  | 793 | return retval; | 
|  | 794 | } | 
|  | 795 |  | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 796 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | * pci_create_resource_files - create resource files in sysfs for @dev | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 798 | * @pdev: dev in question | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | * | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 800 | * Walk the resources in @pdev creating files for each resource available. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | */ | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 802 | static int pci_create_resource_files(struct pci_dev *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { | 
|  | 804 | int i; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 805 | int retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 |  | 
|  | 807 | /* Expose the PCI resources from this device as files */ | 
|  | 808 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 |  | 
|  | 810 | /* skip empty resources */ | 
|  | 811 | if (!pci_resource_len(pdev, i)) | 
|  | 812 | continue; | 
|  | 813 |  | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 814 | retval = pci_create_attr(pdev, i, 0); | 
|  | 815 | /* for prefetchable resources, create a WC mappable file */ | 
|  | 816 | if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH) | 
|  | 817 | retval = pci_create_attr(pdev, i, 1); | 
| Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 818 |  | 
| venkatesh.pallipadi@intel.com | 45aec1a | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 819 | if (retval) { | 
|  | 820 | pci_remove_resource_files(pdev); | 
|  | 821 | return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } | 
|  | 823 | } | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 824 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } | 
|  | 826 | #else /* !HAVE_PCI_MMAP */ | 
| Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 827 | int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } | 
|  | 828 | void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | #endif /* HAVE_PCI_MMAP */ | 
|  | 830 |  | 
|  | 831 | /** | 
|  | 832 | * pci_write_rom - used to enable access to the PCI ROM display | 
|  | 833 | * @kobj: kernel object handle | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 834 | * @bin_attr: struct bin_attribute for this file | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | * @buf: user input | 
|  | 836 | * @off: file offset | 
|  | 837 | * @count: number of byte in input | 
|  | 838 | * | 
|  | 839 | * writing anything except 0 enables it | 
|  | 840 | */ | 
|  | 841 | static ssize_t | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 842 | pci_write_rom(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 843 | char *buf, loff_t off, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { | 
|  | 845 | struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); | 
|  | 846 |  | 
|  | 847 | if ((off ==  0) && (*buf == '0') && (count == 2)) | 
|  | 848 | pdev->rom_attr_enabled = 0; | 
|  | 849 | else | 
|  | 850 | pdev->rom_attr_enabled = 1; | 
|  | 851 |  | 
|  | 852 | return count; | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | /** | 
|  | 856 | * pci_read_rom - read a PCI ROM | 
|  | 857 | * @kobj: kernel object handle | 
| Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 858 | * @bin_attr: struct bin_attribute for this file | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | * @buf: where to put the data we read from the ROM | 
|  | 860 | * @off: file offset | 
|  | 861 | * @count: number of bytes to read | 
|  | 862 | * | 
|  | 863 | * Put @count bytes starting at @off into @buf from the ROM in the PCI | 
|  | 864 | * device corresponding to @kobj. | 
|  | 865 | */ | 
|  | 866 | static ssize_t | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 867 | pci_read_rom(struct kobject *kobj, struct bin_attribute *bin_attr, | 
|  | 868 | char *buf, loff_t off, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { | 
|  | 870 | struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj)); | 
|  | 871 | void __iomem *rom; | 
|  | 872 | size_t size; | 
|  | 873 |  | 
|  | 874 | if (!pdev->rom_attr_enabled) | 
|  | 875 | return -EINVAL; | 
|  | 876 |  | 
|  | 877 | 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] | 878 | if (!rom || !size) | 
|  | 879 | return -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 |  | 
|  | 881 | if (off >= size) | 
|  | 882 | count = 0; | 
|  | 883 | else { | 
|  | 884 | if (off + count > size) | 
|  | 885 | count = size - off; | 
|  | 886 |  | 
|  | 887 | memcpy_fromio(buf, rom + off, count); | 
|  | 888 | } | 
|  | 889 | pci_unmap_rom(pdev, rom); | 
|  | 890 |  | 
|  | 891 | return count; | 
|  | 892 | } | 
|  | 893 |  | 
|  | 894 | static struct bin_attribute pci_config_attr = { | 
|  | 895 | .attr =	{ | 
|  | 896 | .name = "config", | 
|  | 897 | .mode = S_IRUGO | S_IWUSR, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | }, | 
| Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 899 | .size = PCI_CFG_SPACE_SIZE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | .read = pci_read_config, | 
|  | 901 | .write = pci_write_config, | 
|  | 902 | }; | 
|  | 903 |  | 
|  | 904 | static struct bin_attribute pcie_config_attr = { | 
|  | 905 | .attr =	{ | 
|  | 906 | .name = "config", | 
|  | 907 | .mode = S_IRUGO | S_IWUSR, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | }, | 
| Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 909 | .size = PCI_CFG_SPACE_EXP_SIZE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | .read = pci_read_config, | 
|  | 911 | .write = pci_write_config, | 
|  | 912 | }; | 
|  | 913 |  | 
| Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 914 | int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) | 
| Michael Ellerman | 575e334 | 2007-05-08 12:03:07 +1000 | [diff] [blame] | 915 | { | 
| Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 916 | return 0; | 
| Michael Ellerman | 575e334 | 2007-05-08 12:03:07 +1000 | [diff] [blame] | 917 | } | 
|  | 918 |  | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 919 | static int pci_create_capabilities_sysfs(struct pci_dev *dev) | 
|  | 920 | { | 
|  | 921 | int retval; | 
|  | 922 | struct bin_attribute *attr; | 
|  | 923 |  | 
|  | 924 | /* If the device has VPD, try to expose it in sysfs. */ | 
|  | 925 | if (dev->vpd) { | 
|  | 926 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); | 
|  | 927 | if (!attr) | 
|  | 928 | return -ENOMEM; | 
|  | 929 |  | 
|  | 930 | attr->size = dev->vpd->len; | 
|  | 931 | attr->attr.name = "vpd"; | 
|  | 932 | attr->attr.mode = S_IRUSR | S_IWUSR; | 
| Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 933 | attr->read = read_vpd_attr; | 
|  | 934 | attr->write = write_vpd_attr; | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 935 | retval = sysfs_create_bin_file(&dev->dev.kobj, attr); | 
|  | 936 | if (retval) { | 
|  | 937 | kfree(dev->vpd->attr); | 
|  | 938 | return retval; | 
|  | 939 | } | 
|  | 940 | dev->vpd->attr = attr; | 
|  | 941 | } | 
|  | 942 |  | 
|  | 943 | /* Active State Power Management */ | 
|  | 944 | pcie_aspm_create_sysfs_dev_files(dev); | 
|  | 945 |  | 
|  | 946 | return 0; | 
|  | 947 | } | 
|  | 948 |  | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 949 | int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 951 | int retval; | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 952 | int rom_size = 0; | 
|  | 953 | struct bin_attribute *attr; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 954 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (!sysfs_initialized) | 
|  | 956 | return -EACCES; | 
|  | 957 |  | 
| Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 958 | if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE) | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 959 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | else | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 961 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); | 
|  | 962 | if (retval) | 
|  | 963 | goto err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 |  | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 965 | retval = pci_create_resource_files(pdev); | 
|  | 966 | if (retval) | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 967 | goto err_config_file; | 
|  | 968 |  | 
|  | 969 | if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) | 
|  | 970 | rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); | 
|  | 971 | else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW) | 
|  | 972 | rom_size = 0x20000; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 |  | 
|  | 974 | /* If the device has a ROM, try to expose it in sysfs. */ | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 975 | if (rom_size) { | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 976 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 977 | if (!attr) { | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 978 | retval = -ENOMEM; | 
| Michael Ellerman | 9890b12 | 2007-04-18 13:34:12 +1000 | [diff] [blame] | 979 | goto err_resource_files; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 981 | attr->size = rom_size; | 
|  | 982 | attr->attr.name = "rom"; | 
|  | 983 | attr->attr.mode = S_IRUSR; | 
|  | 984 | attr->read = pci_read_rom; | 
|  | 985 | attr->write = pci_write_rom; | 
|  | 986 | retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); | 
|  | 987 | if (retval) { | 
|  | 988 | kfree(attr); | 
|  | 989 | goto err_resource_files; | 
|  | 990 | } | 
|  | 991 | pdev->rom_attr = attr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 993 |  | 
| Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 994 | if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { | 
|  | 995 | retval = device_create_file(&pdev->dev, &vga_attr); | 
|  | 996 | if (retval) | 
|  | 997 | goto err_rom_file; | 
|  | 998 | } | 
|  | 999 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | /* add platform-specific attributes */ | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1001 | retval = pcibios_add_platform_entries(pdev); | 
|  | 1002 | if (retval) | 
| Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 1003 | goto err_vga_file; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1004 |  | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1005 | /* add sysfs entries for various capabilities */ | 
|  | 1006 | retval = pci_create_capabilities_sysfs(pdev); | 
|  | 1007 | if (retval) | 
| Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 1008 | goto err_vga_file; | 
| Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 1009 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | return 0; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1011 |  | 
| Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 1012 | err_vga_file: | 
|  | 1013 | if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) | 
|  | 1014 | device_remove_file(&pdev->dev, &vga_attr); | 
| Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 1015 | err_rom_file: | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1016 | if (rom_size) { | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 1017 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1018 | kfree(pdev->rom_attr); | 
|  | 1019 | pdev->rom_attr = NULL; | 
|  | 1020 | } | 
| Michael Ellerman | 9890b12 | 2007-04-18 13:34:12 +1000 | [diff] [blame] | 1021 | err_resource_files: | 
|  | 1022 | pci_remove_resource_files(pdev); | 
| Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 1023 | err_config_file: | 
| Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 1024 | if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE) | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1025 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); | 
|  | 1026 | else | 
|  | 1027 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); | 
|  | 1028 | err: | 
|  | 1029 | return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } | 
|  | 1031 |  | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1032 | static void pci_remove_capabilities_sysfs(struct pci_dev *dev) | 
|  | 1033 | { | 
|  | 1034 | if (dev->vpd && dev->vpd->attr) { | 
|  | 1035 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); | 
|  | 1036 | kfree(dev->vpd->attr); | 
|  | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | pcie_aspm_remove_sysfs_dev_files(dev); | 
|  | 1040 | } | 
|  | 1041 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | /** | 
|  | 1043 | * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files | 
|  | 1044 | * @pdev: device whose entries we should free | 
|  | 1045 | * | 
|  | 1046 | * Cleanup when @pdev is removed from sysfs. | 
|  | 1047 | */ | 
|  | 1048 | void pci_remove_sysfs_dev_files(struct pci_dev *pdev) | 
|  | 1049 | { | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1050 | int rom_size = 0; | 
|  | 1051 |  | 
| David Miller | d67afe5 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1052 | if (!sysfs_initialized) | 
|  | 1053 | return; | 
|  | 1054 |  | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1055 | pci_remove_capabilities_sysfs(pdev); | 
| Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 1056 |  | 
| Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 1057 | if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); | 
|  | 1059 | else | 
|  | 1060 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); | 
|  | 1061 |  | 
|  | 1062 | pci_remove_resource_files(pdev); | 
|  | 1063 |  | 
| Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1064 | if (pci_resource_len(pdev, PCI_ROM_RESOURCE)) | 
|  | 1065 | rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); | 
|  | 1066 | else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW) | 
|  | 1067 | rom_size = 0x20000; | 
|  | 1068 |  | 
|  | 1069 | if (rom_size && pdev->rom_attr) { | 
|  | 1070 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); | 
|  | 1071 | kfree(pdev->rom_attr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } | 
|  | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | static int __init pci_sysfs_init(void) | 
|  | 1076 | { | 
|  | 1077 | struct pci_dev *pdev = NULL; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1078 | int retval; | 
|  | 1079 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | sysfs_initialized = 1; | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1081 | for_each_pci_dev(pdev) { | 
|  | 1082 | retval = pci_create_sysfs_dev_files(pdev); | 
| Julia Lawall | 151fc5d | 2007-11-20 08:41:16 +0100 | [diff] [blame] | 1083 | if (retval) { | 
|  | 1084 | pci_dev_put(pdev); | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1085 | return retval; | 
| Julia Lawall | 151fc5d | 2007-11-20 08:41:16 +0100 | [diff] [blame] | 1086 | } | 
| Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1087 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 |  | 
|  | 1089 | return 0; | 
|  | 1090 | } | 
|  | 1091 |  | 
| Jesse Barnes | 40ee9e9 | 2007-03-24 11:03:32 -0700 | [diff] [blame] | 1092 | late_initcall(pci_sysfs_init); |