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