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