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