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 | * |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 13 | * Modeled after usb's driverfs.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/topology.h> |
| 24 | #include <linux/mm.h> |
Chris Wright | de139a3 | 2010-05-13 10:43:07 -0700 | [diff] [blame] | 25 | #include <linux/fs.h> |
Alexey Dobriyan | aa0ac36 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 26 | #include <linux/capability.h> |
Chris Wright | a628e7b | 2011-02-14 17:21:49 -0800 | [diff] [blame] | 27 | #include <linux/security.h> |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 28 | #include <linux/pci-aspm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Matthew Garrett | 1a39b31 | 2012-04-16 16:26:02 -0400 | [diff] [blame] | 30 | #include <linux/vgaarb.h> |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 31 | #include <linux/pm_runtime.h> |
Sebastian Ott | dfc73e7 | 2014-04-17 19:46:15 +0200 | [diff] [blame] | 32 | #include <linux/of.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "pci.h" |
| 34 | |
| 35 | static int sysfs_initialized; /* = 0 */ |
| 36 | |
| 37 | /* show configuration fields */ |
| 38 | #define pci_config_attr(field, format_string) \ |
| 39 | static ssize_t \ |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 40 | field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { \ |
| 42 | struct pci_dev *pdev; \ |
| 43 | \ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 44 | pdev = to_pci_dev(dev); \ |
| 45 | return sprintf(buf, format_string, pdev->field); \ |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 46 | } \ |
| 47 | static DEVICE_ATTR_RO(field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | pci_config_attr(vendor, "0x%04x\n"); |
| 50 | pci_config_attr(device, "0x%04x\n"); |
| 51 | pci_config_attr(subsystem_vendor, "0x%04x\n"); |
| 52 | pci_config_attr(subsystem_device, "0x%04x\n"); |
Emil Velikov | 702ed3b | 2016-11-21 16:24:49 -0600 | [diff] [blame] | 53 | pci_config_attr(revision, "0x%02x\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | pci_config_attr(class, "0x%06x\n"); |
| 55 | pci_config_attr(irq, "%u\n"); |
| 56 | |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 57 | static ssize_t broken_parity_status_show(struct device *dev, |
| 58 | struct device_attribute *attr, |
| 59 | char *buf) |
| 60 | { |
| 61 | struct pci_dev *pdev = to_pci_dev(dev); |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 62 | return sprintf(buf, "%u\n", pdev->broken_parity_status); |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static ssize_t broken_parity_status_store(struct device *dev, |
| 66 | struct device_attribute *attr, |
| 67 | const char *buf, size_t count) |
| 68 | { |
| 69 | struct pci_dev *pdev = to_pci_dev(dev); |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 70 | unsigned long val; |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 71 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 72 | if (kstrtoul(buf, 0, &val) < 0) |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 73 | return -EINVAL; |
| 74 | |
| 75 | pdev->broken_parity_status = !!val; |
| 76 | |
| 77 | return count; |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 78 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 79 | static DEVICE_ATTR_RW(broken_parity_status); |
Doug Thompson | bdee9d9 | 2006-06-14 16:59:48 -0700 | [diff] [blame] | 80 | |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 81 | static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 82 | struct device_attribute *attr, char *buf) |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 83 | { |
Mike Travis | 3be8305 | 2009-01-04 05:18:01 -0800 | [diff] [blame] | 84 | const struct cpumask *mask; |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 85 | |
Andreas Herrmann | e0cd516 | 2009-04-17 12:01:55 +0200 | [diff] [blame] | 86 | #ifdef CONFIG_NUMA |
David John | 6be954d | 2010-01-04 20:28:57 +0530 | [diff] [blame] | 87 | mask = (dev_to_node(dev) == -1) ? cpu_online_mask : |
| 88 | cpumask_of_node(dev_to_node(dev)); |
Andreas Herrmann | e0cd516 | 2009-04-17 12:01:55 +0200 | [diff] [blame] | 89 | #else |
Mike Travis | 3be8305 | 2009-01-04 05:18:01 -0800 | [diff] [blame] | 90 | mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); |
Andreas Herrmann | e0cd516 | 2009-04-17 12:01:55 +0200 | [diff] [blame] | 91 | #endif |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 92 | return cpumap_print_to_pagebuf(list, buf, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Yijing Wang | c489f5f | 2013-09-30 15:02:38 +0800 | [diff] [blame] | 95 | static ssize_t local_cpus_show(struct device *dev, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 96 | struct device_attribute *attr, char *buf) |
Yijing Wang | c489f5f | 2013-09-30 15:02:38 +0800 | [diff] [blame] | 97 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 98 | return pci_dev_show_local_cpu(dev, false, attr, buf); |
Yijing Wang | c489f5f | 2013-09-30 15:02:38 +0800 | [diff] [blame] | 99 | } |
Bjorn Helgaas | 33de1b8 | 2013-10-31 14:12:40 -0600 | [diff] [blame] | 100 | static DEVICE_ATTR_RO(local_cpus); |
Yijing Wang | c489f5f | 2013-09-30 15:02:38 +0800 | [diff] [blame] | 101 | |
| 102 | static ssize_t local_cpulist_show(struct device *dev, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 103 | struct device_attribute *attr, char *buf) |
Yijing Wang | c489f5f | 2013-09-30 15:02:38 +0800 | [diff] [blame] | 104 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 105 | return pci_dev_show_local_cpu(dev, true, attr, buf); |
Yijing Wang | c489f5f | 2013-09-30 15:02:38 +0800 | [diff] [blame] | 106 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 107 | static DEVICE_ATTR_RO(local_cpulist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 109 | /* |
| 110 | * PCI Bus Class Devices |
| 111 | */ |
Greg Kroah-Hartman | 56039e6 | 2013-07-24 15:05:17 -0700 | [diff] [blame] | 112 | static ssize_t cpuaffinity_show(struct device *dev, |
| 113 | struct device_attribute *attr, char *buf) |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 114 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 115 | const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); |
| 116 | |
| 117 | return cpumap_print_to_pagebuf(false, buf, cpumask); |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 118 | } |
Greg Kroah-Hartman | 56039e6 | 2013-07-24 15:05:17 -0700 | [diff] [blame] | 119 | static DEVICE_ATTR_RO(cpuaffinity); |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 120 | |
Greg Kroah-Hartman | 56039e6 | 2013-07-24 15:05:17 -0700 | [diff] [blame] | 121 | static ssize_t cpulistaffinity_show(struct device *dev, |
| 122 | struct device_attribute *attr, char *buf) |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 123 | { |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 124 | const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); |
| 125 | |
| 126 | return cpumap_print_to_pagebuf(true, buf, cpumask); |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 127 | } |
Greg Kroah-Hartman | 56039e6 | 2013-07-24 15:05:17 -0700 | [diff] [blame] | 128 | static DEVICE_ATTR_RO(cpulistaffinity); |
Yinghai Lu | dc2c2c9 | 2011-05-12 17:11:40 -0700 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* show resources */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 131 | static ssize_t resource_show(struct device *dev, struct device_attribute *attr, |
| 132 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 134 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 135 | char *str = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int i; |
Yu Zhao | fde09c6 | 2008-11-22 02:39:32 +0800 | [diff] [blame] | 137 | int max; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 138 | resource_size_t start, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | if (pci_dev->subordinate) |
| 141 | max = DEVICE_COUNT_RESOURCE; |
Yu Zhao | fde09c6 | 2008-11-22 02:39:32 +0800 | [diff] [blame] | 142 | else |
| 143 | max = PCI_BRIDGE_RESOURCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | for (i = 0; i < max; i++) { |
Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 146 | struct resource *res = &pci_dev->resource[i]; |
| 147 | pci_resource_to_user(pci_dev, i, res, &start, &end); |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 148 | str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n", |
Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 149 | (unsigned long long)start, |
| 150 | (unsigned long long)end, |
| 151 | (unsigned long long)res->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | return (str - buf); |
| 154 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 155 | static DEVICE_ATTR_RO(resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Wong Vee Khee | 56c1af4 | 2017-06-01 17:43:06 +0800 | [diff] [blame] | 157 | static ssize_t max_link_speed_show(struct device *dev, |
| 158 | struct device_attribute *attr, char *buf) |
| 159 | { |
| 160 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 161 | u32 linkcap; |
| 162 | int err; |
| 163 | const char *speed; |
| 164 | |
| 165 | err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap); |
| 166 | if (err) |
| 167 | return -EINVAL; |
| 168 | |
| 169 | switch (linkcap & PCI_EXP_LNKCAP_SLS) { |
| 170 | case PCI_EXP_LNKCAP_SLS_8_0GB: |
| 171 | speed = "8 GT/s"; |
| 172 | break; |
| 173 | case PCI_EXP_LNKCAP_SLS_5_0GB: |
| 174 | speed = "5 GT/s"; |
| 175 | break; |
| 176 | case PCI_EXP_LNKCAP_SLS_2_5GB: |
| 177 | speed = "2.5 GT/s"; |
| 178 | break; |
| 179 | default: |
| 180 | speed = "Unknown speed"; |
| 181 | } |
| 182 | |
| 183 | return sprintf(buf, "%s\n", speed); |
| 184 | } |
| 185 | static DEVICE_ATTR_RO(max_link_speed); |
| 186 | |
| 187 | static ssize_t max_link_width_show(struct device *dev, |
| 188 | struct device_attribute *attr, char *buf) |
| 189 | { |
| 190 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 191 | u32 linkcap; |
| 192 | int err; |
| 193 | |
| 194 | err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap); |
| 195 | if (err) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4); |
| 199 | } |
| 200 | static DEVICE_ATTR_RO(max_link_width); |
| 201 | |
| 202 | static ssize_t current_link_speed_show(struct device *dev, |
| 203 | struct device_attribute *attr, char *buf) |
| 204 | { |
| 205 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 206 | u16 linkstat; |
| 207 | int err; |
| 208 | const char *speed; |
| 209 | |
| 210 | err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); |
| 211 | if (err) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | switch (linkstat & PCI_EXP_LNKSTA_CLS) { |
| 215 | case PCI_EXP_LNKSTA_CLS_8_0GB: |
| 216 | speed = "8 GT/s"; |
| 217 | break; |
| 218 | case PCI_EXP_LNKSTA_CLS_5_0GB: |
| 219 | speed = "5 GT/s"; |
| 220 | break; |
| 221 | case PCI_EXP_LNKSTA_CLS_2_5GB: |
| 222 | speed = "2.5 GT/s"; |
| 223 | break; |
| 224 | default: |
| 225 | speed = "Unknown speed"; |
| 226 | } |
| 227 | |
| 228 | return sprintf(buf, "%s\n", speed); |
| 229 | } |
| 230 | static DEVICE_ATTR_RO(current_link_speed); |
| 231 | |
| 232 | static ssize_t current_link_width_show(struct device *dev, |
| 233 | struct device_attribute *attr, char *buf) |
| 234 | { |
| 235 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 236 | u16 linkstat; |
| 237 | int err; |
| 238 | |
| 239 | err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); |
| 240 | if (err) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | return sprintf(buf, "%u\n", |
| 244 | (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT); |
| 245 | } |
| 246 | static DEVICE_ATTR_RO(current_link_width); |
| 247 | |
| 248 | static ssize_t secondary_bus_number_show(struct device *dev, |
| 249 | struct device_attribute *attr, |
| 250 | char *buf) |
| 251 | { |
| 252 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 253 | u8 sec_bus; |
| 254 | int err; |
| 255 | |
| 256 | err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus); |
| 257 | if (err) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | return sprintf(buf, "%u\n", sec_bus); |
| 261 | } |
| 262 | static DEVICE_ATTR_RO(secondary_bus_number); |
| 263 | |
| 264 | static ssize_t subordinate_bus_number_show(struct device *dev, |
| 265 | struct device_attribute *attr, |
| 266 | char *buf) |
| 267 | { |
| 268 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 269 | u8 sub_bus; |
| 270 | int err; |
| 271 | |
| 272 | err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus); |
| 273 | if (err) |
| 274 | return -EINVAL; |
| 275 | |
| 276 | return sprintf(buf, "%u\n", sub_bus); |
| 277 | } |
| 278 | static DEVICE_ATTR_RO(subordinate_bus_number); |
| 279 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 280 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 281 | char *buf) |
Greg KH | 9888549 | 2005-05-05 11:57:25 -0700 | [diff] [blame] | 282 | { |
| 283 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 284 | |
Ricardo Ribalda Delgado | 89ec3dc | 2014-08-27 14:57:57 +0200 | [diff] [blame] | 285 | return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", |
Greg KH | 9888549 | 2005-05-05 11:57:25 -0700 | [diff] [blame] | 286 | pci_dev->vendor, pci_dev->device, |
| 287 | pci_dev->subsystem_vendor, pci_dev->subsystem_device, |
| 288 | (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), |
| 289 | (u8)(pci_dev->class)); |
| 290 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 291 | static DEVICE_ATTR_RO(modalias); |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 292 | |
Greg Kroah-Hartman | d8e7d53 | 2014-10-30 09:30:28 -0700 | [diff] [blame] | 293 | static ssize_t enable_store(struct device *dev, struct device_attribute *attr, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 294 | const char *buf, size_t count) |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 295 | { |
| 296 | struct pci_dev *pdev = to_pci_dev(dev); |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 297 | unsigned long val; |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 298 | ssize_t result = kstrtoul(buf, 0, &val); |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 299 | |
| 300 | if (result < 0) |
| 301 | return result; |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 302 | |
| 303 | /* this can crash the machine when done on the "wrong" device */ |
| 304 | if (!capable(CAP_SYS_ADMIN)) |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 305 | return -EPERM; |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 306 | |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 307 | if (!val) { |
Yuji Shimada | 296ccb0 | 2009-04-03 16:41:46 +0900 | [diff] [blame] | 308 | if (pci_is_enabled(pdev)) |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 309 | pci_disable_device(pdev); |
| 310 | else |
| 311 | result = -EIO; |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 312 | } else |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 313 | result = pci_enable_device(pdev); |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 314 | |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 315 | return result < 0 ? result : count; |
| 316 | } |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 317 | |
Greg Kroah-Hartman | d8e7d53 | 2014-10-30 09:30:28 -0700 | [diff] [blame] | 318 | static ssize_t enable_show(struct device *dev, struct device_attribute *attr, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 319 | char *buf) |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 320 | { |
| 321 | struct pci_dev *pdev; |
| 322 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 323 | pdev = to_pci_dev(dev); |
| 324 | return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt)); |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 325 | } |
Greg Kroah-Hartman | d8e7d53 | 2014-10-30 09:30:28 -0700 | [diff] [blame] | 326 | static DEVICE_ATTR_RW(enable); |
Arjan van de Ven | 9f125d3 | 2006-04-29 10:59:08 +0200 | [diff] [blame] | 327 | |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 328 | #ifdef CONFIG_NUMA |
Prarit Bhargava | 63692df | 2014-10-23 14:22:12 -0400 | [diff] [blame] | 329 | static ssize_t numa_node_store(struct device *dev, |
| 330 | struct device_attribute *attr, const char *buf, |
| 331 | size_t count) |
| 332 | { |
| 333 | struct pci_dev *pdev = to_pci_dev(dev); |
| 334 | int node, ret; |
| 335 | |
| 336 | if (!capable(CAP_SYS_ADMIN)) |
| 337 | return -EPERM; |
| 338 | |
| 339 | ret = kstrtoint(buf, 0, &node); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
Mathias Krause | 3dcc8d3 | 2015-11-09 20:00:27 +0100 | [diff] [blame] | 343 | if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | if (node != NUMA_NO_NODE && !node_online(node)) |
Prarit Bhargava | 63692df | 2014-10-23 14:22:12 -0400 | [diff] [blame] | 347 | return -EINVAL; |
| 348 | |
| 349 | add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); |
| 350 | dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", |
| 351 | node); |
| 352 | |
| 353 | dev->numa_node = node; |
| 354 | return count; |
| 355 | } |
| 356 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 357 | static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, |
| 358 | char *buf) |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 359 | { |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 360 | return sprintf(buf, "%d\n", dev->numa_node); |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 361 | } |
Prarit Bhargava | 63692df | 2014-10-23 14:22:12 -0400 | [diff] [blame] | 362 | static DEVICE_ATTR_RW(numa_node); |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 363 | #endif |
| 364 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 365 | static ssize_t dma_mask_bits_show(struct device *dev, |
| 366 | struct device_attribute *attr, char *buf) |
Yinghai Lu | bb96540 | 2009-11-24 18:21:21 -0800 | [diff] [blame] | 367 | { |
| 368 | struct pci_dev *pdev = to_pci_dev(dev); |
| 369 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 370 | return sprintf(buf, "%d\n", fls64(pdev->dma_mask)); |
Yinghai Lu | bb96540 | 2009-11-24 18:21:21 -0800 | [diff] [blame] | 371 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 372 | static DEVICE_ATTR_RO(dma_mask_bits); |
Yinghai Lu | bb96540 | 2009-11-24 18:21:21 -0800 | [diff] [blame] | 373 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 374 | static ssize_t consistent_dma_mask_bits_show(struct device *dev, |
| 375 | struct device_attribute *attr, |
| 376 | char *buf) |
Yinghai Lu | bb96540 | 2009-11-24 18:21:21 -0800 | [diff] [blame] | 377 | { |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 378 | return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask)); |
Yinghai Lu | bb96540 | 2009-11-24 18:21:21 -0800 | [diff] [blame] | 379 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 380 | static DEVICE_ATTR_RO(consistent_dma_mask_bits); |
Yinghai Lu | bb96540 | 2009-11-24 18:21:21 -0800 | [diff] [blame] | 381 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 382 | static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, |
| 383 | char *buf) |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 384 | { |
| 385 | struct pci_dev *pdev = to_pci_dev(dev); |
Yijing Wang | 468ff15 | 2014-09-23 13:27:24 +0800 | [diff] [blame] | 386 | struct pci_bus *subordinate = pdev->subordinate; |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 387 | |
Yijing Wang | 468ff15 | 2014-09-23 13:27:24 +0800 | [diff] [blame] | 388 | return sprintf(buf, "%u\n", subordinate ? |
| 389 | !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
| 390 | : !pdev->no_msi); |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 393 | static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, |
| 394 | const char *buf, size_t count) |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 395 | { |
| 396 | struct pci_dev *pdev = to_pci_dev(dev); |
Yijing Wang | 468ff15 | 2014-09-23 13:27:24 +0800 | [diff] [blame] | 397 | struct pci_bus *subordinate = pdev->subordinate; |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 398 | unsigned long val; |
| 399 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 400 | if (kstrtoul(buf, 0, &val) < 0) |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 401 | return -EINVAL; |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 402 | |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 403 | if (!capable(CAP_SYS_ADMIN)) |
Trent Piepho | 92425a4 | 2008-11-30 17:10:12 -0800 | [diff] [blame] | 404 | return -EPERM; |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 405 | |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 406 | /* |
Yijing Wang | 468ff15 | 2014-09-23 13:27:24 +0800 | [diff] [blame] | 407 | * "no_msi" and "bus_flags" only affect what happens when a driver |
| 408 | * requests MSI or MSI-X. They don't affect any drivers that have |
| 409 | * already requested MSI or MSI-X. |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 410 | */ |
Yijing Wang | 468ff15 | 2014-09-23 13:27:24 +0800 | [diff] [blame] | 411 | if (!subordinate) { |
| 412 | pdev->no_msi = !val; |
| 413 | dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n", |
| 414 | val ? "allowed" : "disallowed"); |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 415 | return count; |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Yijing Wang | 468ff15 | 2014-09-23 13:27:24 +0800 | [diff] [blame] | 418 | if (val) |
| 419 | subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; |
| 420 | else |
| 421 | subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; |
| 422 | |
| 423 | dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", |
| 424 | val ? "allowed" : "disallowed"); |
Brice Goglin | fe97064 | 2006-08-31 01:55:15 -0400 | [diff] [blame] | 425 | return count; |
| 426 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 427 | static DEVICE_ATTR_RW(msi_bus); |
Greg KH | 9888549 | 2005-05-05 11:57:25 -0700 | [diff] [blame] | 428 | |
Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 429 | static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, |
| 430 | size_t count) |
| 431 | { |
| 432 | unsigned long val; |
| 433 | struct pci_bus *b = NULL; |
| 434 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 435 | if (kstrtoul(buf, 0, &val) < 0) |
Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 436 | return -EINVAL; |
| 437 | |
| 438 | if (val) { |
Rafael J. Wysocki | 9d16947 | 2014-01-10 15:22:18 +0100 | [diff] [blame] | 439 | pci_lock_rescan_remove(); |
Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 440 | while ((b = pci_find_next_bus(b)) != NULL) |
| 441 | pci_rescan_bus(b); |
Rafael J. Wysocki | 9d16947 | 2014-01-10 15:22:18 +0100 | [diff] [blame] | 442 | pci_unlock_rescan_remove(); |
Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 443 | } |
| 444 | return count; |
| 445 | } |
Greg Kroah-Hartman | 0f49ba5 | 2013-10-07 14:51:02 -0600 | [diff] [blame] | 446 | static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store); |
Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 447 | |
Sachin Kamat | bf22c90 | 2013-09-28 15:42:00 +0530 | [diff] [blame] | 448 | static struct attribute *pci_bus_attrs[] = { |
Greg Kroah-Hartman | 0f49ba5 | 2013-10-07 14:51:02 -0600 | [diff] [blame] | 449 | &bus_attr_rescan.attr, |
| 450 | NULL, |
| 451 | }; |
| 452 | |
| 453 | static const struct attribute_group pci_bus_group = { |
| 454 | .attrs = pci_bus_attrs, |
| 455 | }; |
| 456 | |
| 457 | const struct attribute_group *pci_bus_groups[] = { |
| 458 | &pci_bus_group, |
| 459 | NULL, |
Alex Chiang | 705b1aa | 2009-03-20 14:56:31 -0600 | [diff] [blame] | 460 | }; |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 461 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 462 | static ssize_t dev_rescan_store(struct device *dev, |
| 463 | struct device_attribute *attr, const char *buf, |
| 464 | size_t count) |
Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 465 | { |
| 466 | unsigned long val; |
| 467 | struct pci_dev *pdev = to_pci_dev(dev); |
| 468 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 469 | if (kstrtoul(buf, 0, &val) < 0) |
Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | |
| 472 | if (val) { |
Rafael J. Wysocki | 9d16947 | 2014-01-10 15:22:18 +0100 | [diff] [blame] | 473 | pci_lock_rescan_remove(); |
Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 474 | pci_rescan_bus(pdev->bus); |
Rafael J. Wysocki | 9d16947 | 2014-01-10 15:22:18 +0100 | [diff] [blame] | 475 | pci_unlock_rescan_remove(); |
Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 476 | } |
| 477 | return count; |
| 478 | } |
Sachin Kamat | bf22c90 | 2013-09-28 15:42:00 +0530 | [diff] [blame] | 479 | static struct device_attribute dev_rescan_attr = __ATTR(rescan, |
| 480 | (S_IWUSR|S_IWGRP), |
| 481 | NULL, dev_rescan_store); |
Alex Chiang | 738a639 | 2009-03-20 14:56:41 -0600 | [diff] [blame] | 482 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 483 | static ssize_t remove_store(struct device *dev, struct device_attribute *attr, |
| 484 | const char *buf, size_t count) |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 485 | { |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 486 | unsigned long val; |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 487 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 488 | if (kstrtoul(buf, 0, &val) < 0) |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 489 | return -EINVAL; |
| 490 | |
Tejun Heo | bc6caf0 | 2014-02-03 14:03:02 -0500 | [diff] [blame] | 491 | if (val && device_remove_file_self(dev, attr)) |
| 492 | pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); |
Alex Chiang | 77c27c7 | 2009-03-20 14:56:36 -0600 | [diff] [blame] | 493 | return count; |
| 494 | } |
Sachin Kamat | bf22c90 | 2013-09-28 15:42:00 +0530 | [diff] [blame] | 495 | static struct device_attribute dev_remove_attr = __ATTR(remove, |
| 496 | (S_IWUSR|S_IWGRP), |
| 497 | NULL, remove_store); |
Yinghai Lu | b9d320f | 2011-05-12 17:11:39 -0700 | [diff] [blame] | 498 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 499 | static ssize_t dev_bus_rescan_store(struct device *dev, |
| 500 | struct device_attribute *attr, |
| 501 | const char *buf, size_t count) |
Yinghai Lu | b9d320f | 2011-05-12 17:11:39 -0700 | [diff] [blame] | 502 | { |
| 503 | unsigned long val; |
| 504 | struct pci_bus *bus = to_pci_bus(dev); |
| 505 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 506 | if (kstrtoul(buf, 0, &val) < 0) |
Yinghai Lu | b9d320f | 2011-05-12 17:11:39 -0700 | [diff] [blame] | 507 | return -EINVAL; |
| 508 | |
| 509 | if (val) { |
Rafael J. Wysocki | 9d16947 | 2014-01-10 15:22:18 +0100 | [diff] [blame] | 510 | pci_lock_rescan_remove(); |
Yinghai Lu | 2f32052 | 2012-01-21 02:08:22 -0800 | [diff] [blame] | 511 | if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) |
| 512 | pci_rescan_bus_bridge_resize(bus->self); |
| 513 | else |
| 514 | pci_rescan_bus(bus); |
Rafael J. Wysocki | 9d16947 | 2014-01-10 15:22:18 +0100 | [diff] [blame] | 515 | pci_unlock_rescan_remove(); |
Yinghai Lu | b9d320f | 2011-05-12 17:11:39 -0700 | [diff] [blame] | 516 | } |
| 517 | return count; |
| 518 | } |
Greg Kroah-Hartman | 56039e6 | 2013-07-24 15:05:17 -0700 | [diff] [blame] | 519 | static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store); |
Yinghai Lu | b9d320f | 2011-05-12 17:11:39 -0700 | [diff] [blame] | 520 | |
Rafael J. Wysocki | fbb988b | 2014-11-27 23:16:57 +0100 | [diff] [blame] | 521 | #if defined(CONFIG_PM) && defined(CONFIG_ACPI) |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 522 | static ssize_t d3cold_allowed_store(struct device *dev, |
| 523 | struct device_attribute *attr, |
| 524 | const char *buf, size_t count) |
| 525 | { |
| 526 | struct pci_dev *pdev = to_pci_dev(dev); |
| 527 | unsigned long val; |
| 528 | |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 529 | if (kstrtoul(buf, 0, &val) < 0) |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | |
| 532 | pdev->d3cold_allowed = !!val; |
Mika Westerberg | 9d26d3a | 2016-06-02 11:17:12 +0300 | [diff] [blame] | 533 | if (pdev->d3cold_allowed) |
| 534 | pci_d3cold_enable(pdev); |
| 535 | else |
| 536 | pci_d3cold_disable(pdev); |
| 537 | |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 538 | pm_runtime_resume(dev); |
| 539 | |
| 540 | return count; |
| 541 | } |
| 542 | |
| 543 | static ssize_t d3cold_allowed_show(struct device *dev, |
| 544 | struct device_attribute *attr, char *buf) |
| 545 | { |
| 546 | struct pci_dev *pdev = to_pci_dev(dev); |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 547 | return sprintf(buf, "%u\n", pdev->d3cold_allowed); |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 548 | } |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 549 | static DEVICE_ATTR_RW(d3cold_allowed); |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 550 | #endif |
| 551 | |
Sebastian Ott | dfc73e7 | 2014-04-17 19:46:15 +0200 | [diff] [blame] | 552 | #ifdef CONFIG_OF |
| 553 | static ssize_t devspec_show(struct device *dev, |
| 554 | struct device_attribute *attr, char *buf) |
| 555 | { |
| 556 | struct pci_dev *pdev = to_pci_dev(dev); |
| 557 | struct device_node *np = pci_device_to_OF_node(pdev); |
| 558 | |
| 559 | if (np == NULL || np->full_name == NULL) |
| 560 | return 0; |
| 561 | return sprintf(buf, "%s", np->full_name); |
| 562 | } |
| 563 | static DEVICE_ATTR_RO(devspec); |
| 564 | #endif |
| 565 | |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 566 | #ifdef CONFIG_PCI_IOV |
| 567 | static ssize_t sriov_totalvfs_show(struct device *dev, |
| 568 | struct device_attribute *attr, |
| 569 | char *buf) |
| 570 | { |
| 571 | struct pci_dev *pdev = to_pci_dev(dev); |
| 572 | |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 573 | return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev)); |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
| 577 | static ssize_t sriov_numvfs_show(struct device *dev, |
| 578 | struct device_attribute *attr, |
| 579 | char *buf) |
| 580 | { |
| 581 | struct pci_dev *pdev = to_pci_dev(dev); |
| 582 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 583 | return sprintf(buf, "%u\n", pdev->sriov->num_VFs); |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 587 | * num_vfs > 0; number of VFs to enable |
| 588 | * num_vfs = 0; disable all VFs |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 589 | * |
| 590 | * Note: SRIOV spec doesn't allow partial VF |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 591 | * disable, so it's all or none. |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 592 | */ |
| 593 | static ssize_t sriov_numvfs_store(struct device *dev, |
| 594 | struct device_attribute *attr, |
| 595 | const char *buf, size_t count) |
| 596 | { |
| 597 | struct pci_dev *pdev = to_pci_dev(dev); |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 598 | int ret; |
| 599 | u16 num_vfs; |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 600 | |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 601 | ret = kstrtou16(buf, 0, &num_vfs); |
| 602 | if (ret < 0) |
| 603 | return ret; |
| 604 | |
| 605 | if (num_vfs > pci_sriov_get_totalvfs(pdev)) |
| 606 | return -ERANGE; |
| 607 | |
Jakub Kicinski | 17530e7 | 2017-05-22 15:50:23 -0700 | [diff] [blame] | 608 | device_lock(&pdev->dev); |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 609 | |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 610 | if (num_vfs == pdev->sriov->num_VFs) |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 611 | goto exit; |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 612 | |
| 613 | /* is PF driver loaded w/callback */ |
| 614 | if (!pdev->driver || !pdev->driver->sriov_configure) { |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 615 | dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n"); |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 616 | ret = -ENOENT; |
| 617 | goto exit; |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 618 | } |
| 619 | |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 620 | if (num_vfs == 0) { |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 621 | /* disable VFs */ |
| 622 | ret = pdev->driver->sriov_configure(pdev, 0); |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 623 | goto exit; |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 624 | } |
| 625 | |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 626 | /* enable VFs */ |
| 627 | if (pdev->sriov->num_VFs) { |
| 628 | dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n", |
| 629 | pdev->sriov->num_VFs, num_vfs); |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 630 | ret = -EBUSY; |
| 631 | goto exit; |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 632 | } |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 633 | |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 634 | ret = pdev->driver->sriov_configure(pdev, num_vfs); |
| 635 | if (ret < 0) |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 636 | goto exit; |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 637 | |
| 638 | if (ret != num_vfs) |
| 639 | dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n", |
| 640 | num_vfs, ret); |
| 641 | |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 642 | exit: |
Jakub Kicinski | 17530e7 | 2017-05-22 15:50:23 -0700 | [diff] [blame] | 643 | device_unlock(&pdev->dev); |
Emil Tantilov | 5b0948d | 2017-01-06 13:59:08 -0800 | [diff] [blame] | 644 | |
| 645 | if (ret < 0) |
| 646 | return ret; |
| 647 | |
Bjorn Helgaas | faa48a5 | 2012-12-26 10:39:22 -0700 | [diff] [blame] | 648 | return count; |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 649 | } |
| 650 | |
Bodong Wang | 0e7df22 | 2017-04-13 01:51:40 +0300 | [diff] [blame] | 651 | static ssize_t sriov_drivers_autoprobe_show(struct device *dev, |
| 652 | struct device_attribute *attr, |
| 653 | char *buf) |
| 654 | { |
| 655 | struct pci_dev *pdev = to_pci_dev(dev); |
| 656 | |
| 657 | return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe); |
| 658 | } |
| 659 | |
| 660 | static ssize_t sriov_drivers_autoprobe_store(struct device *dev, |
| 661 | struct device_attribute *attr, |
| 662 | const char *buf, size_t count) |
| 663 | { |
| 664 | struct pci_dev *pdev = to_pci_dev(dev); |
| 665 | bool drivers_autoprobe; |
| 666 | |
| 667 | if (kstrtobool(buf, &drivers_autoprobe) < 0) |
| 668 | return -EINVAL; |
| 669 | |
| 670 | pdev->sriov->drivers_autoprobe = drivers_autoprobe; |
| 671 | |
| 672 | return count; |
| 673 | } |
| 674 | |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 675 | static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs); |
| 676 | static struct device_attribute sriov_numvfs_attr = |
| 677 | __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP), |
| 678 | sriov_numvfs_show, sriov_numvfs_store); |
Bodong Wang | 0e7df22 | 2017-04-13 01:51:40 +0300 | [diff] [blame] | 679 | static struct device_attribute sriov_drivers_autoprobe_attr = |
| 680 | __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP), |
| 681 | sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store); |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 682 | #endif /* CONFIG_PCI_IOV */ |
| 683 | |
Alex Williamson | 782a985 | 2014-05-20 08:53:21 -0600 | [diff] [blame] | 684 | static ssize_t driver_override_store(struct device *dev, |
| 685 | struct device_attribute *attr, |
| 686 | const char *buf, size_t count) |
| 687 | { |
| 688 | struct pci_dev *pdev = to_pci_dev(dev); |
| 689 | char *driver_override, *old = pdev->driver_override, *cp; |
| 690 | |
Sasha Levin | 4efe874 | 2015-02-04 17:38:15 -0500 | [diff] [blame] | 691 | /* We need to keep extra room for a newline */ |
| 692 | if (count >= (PAGE_SIZE - 1)) |
Alex Williamson | 782a985 | 2014-05-20 08:53:21 -0600 | [diff] [blame] | 693 | return -EINVAL; |
| 694 | |
| 695 | driver_override = kstrndup(buf, count, GFP_KERNEL); |
| 696 | if (!driver_override) |
| 697 | return -ENOMEM; |
| 698 | |
| 699 | cp = strchr(driver_override, '\n'); |
| 700 | if (cp) |
| 701 | *cp = '\0'; |
| 702 | |
| 703 | if (strlen(driver_override)) { |
| 704 | pdev->driver_override = driver_override; |
| 705 | } else { |
| 706 | kfree(driver_override); |
| 707 | pdev->driver_override = NULL; |
| 708 | } |
| 709 | |
| 710 | kfree(old); |
| 711 | |
| 712 | return count; |
| 713 | } |
| 714 | |
| 715 | static ssize_t driver_override_show(struct device *dev, |
| 716 | struct device_attribute *attr, char *buf) |
| 717 | { |
| 718 | struct pci_dev *pdev = to_pci_dev(dev); |
| 719 | |
Sasha Levin | 4efe874 | 2015-02-04 17:38:15 -0500 | [diff] [blame] | 720 | return snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override); |
Alex Williamson | 782a985 | 2014-05-20 08:53:21 -0600 | [diff] [blame] | 721 | } |
| 722 | static DEVICE_ATTR_RW(driver_override); |
| 723 | |
Sachin Kamat | bf22c90 | 2013-09-28 15:42:00 +0530 | [diff] [blame] | 724 | static struct attribute *pci_dev_attrs[] = { |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 725 | &dev_attr_resource.attr, |
| 726 | &dev_attr_vendor.attr, |
| 727 | &dev_attr_device.attr, |
| 728 | &dev_attr_subsystem_vendor.attr, |
| 729 | &dev_attr_subsystem_device.attr, |
Emil Velikov | 702ed3b | 2016-11-21 16:24:49 -0600 | [diff] [blame] | 730 | &dev_attr_revision.attr, |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 731 | &dev_attr_class.attr, |
| 732 | &dev_attr_irq.attr, |
| 733 | &dev_attr_local_cpus.attr, |
| 734 | &dev_attr_local_cpulist.attr, |
| 735 | &dev_attr_modalias.attr, |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 736 | #ifdef CONFIG_NUMA |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 737 | &dev_attr_numa_node.attr, |
Brice Goglin | 81bb0e1 | 2007-01-28 10:53:40 +0100 | [diff] [blame] | 738 | #endif |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 739 | &dev_attr_dma_mask_bits.attr, |
| 740 | &dev_attr_consistent_dma_mask_bits.attr, |
Greg Kroah-Hartman | d8e7d53 | 2014-10-30 09:30:28 -0700 | [diff] [blame] | 741 | &dev_attr_enable.attr, |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 742 | &dev_attr_broken_parity_status.attr, |
| 743 | &dev_attr_msi_bus.attr, |
Rafael J. Wysocki | fbb988b | 2014-11-27 23:16:57 +0100 | [diff] [blame] | 744 | #if defined(CONFIG_PM) && defined(CONFIG_ACPI) |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 745 | &dev_attr_d3cold_allowed.attr, |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 746 | #endif |
Sebastian Ott | dfc73e7 | 2014-04-17 19:46:15 +0200 | [diff] [blame] | 747 | #ifdef CONFIG_OF |
| 748 | &dev_attr_devspec.attr, |
| 749 | #endif |
Alex Williamson | 782a985 | 2014-05-20 08:53:21 -0600 | [diff] [blame] | 750 | &dev_attr_driver_override.attr, |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 751 | NULL, |
| 752 | }; |
| 753 | |
Wong Vee Khee | 56c1af4 | 2017-06-01 17:43:06 +0800 | [diff] [blame] | 754 | static struct attribute *pci_bridge_attrs[] = { |
| 755 | &dev_attr_subordinate_bus_number.attr, |
| 756 | &dev_attr_secondary_bus_number.attr, |
| 757 | NULL, |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 758 | }; |
| 759 | |
Wong Vee Khee | 56c1af4 | 2017-06-01 17:43:06 +0800 | [diff] [blame] | 760 | static struct attribute *pcie_dev_attrs[] = { |
| 761 | &dev_attr_current_link_speed.attr, |
| 762 | &dev_attr_current_link_width.attr, |
| 763 | &dev_attr_max_link_width.attr, |
| 764 | &dev_attr_max_link_speed.attr, |
Greg Kroah-Hartman | 5136b2d | 2013-10-06 23:55:40 -0700 | [diff] [blame] | 765 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | }; |
| 767 | |
Greg Kroah-Hartman | 56039e6 | 2013-07-24 15:05:17 -0700 | [diff] [blame] | 768 | static struct attribute *pcibus_attrs[] = { |
| 769 | &dev_attr_rescan.attr, |
| 770 | &dev_attr_cpuaffinity.attr, |
| 771 | &dev_attr_cpulistaffinity.attr, |
| 772 | NULL, |
| 773 | }; |
| 774 | |
| 775 | static const struct attribute_group pcibus_group = { |
| 776 | .attrs = pcibus_attrs, |
| 777 | }; |
| 778 | |
| 779 | const struct attribute_group *pcibus_groups[] = { |
| 780 | &pcibus_group, |
| 781 | NULL, |
Yinghai Lu | b9d320f | 2011-05-12 17:11:39 -0700 | [diff] [blame] | 782 | }; |
| 783 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 784 | static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, |
| 785 | char *buf) |
Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 786 | { |
| 787 | struct pci_dev *pdev = to_pci_dev(dev); |
Matthew Garrett | 1a39b31 | 2012-04-16 16:26:02 -0400 | [diff] [blame] | 788 | struct pci_dev *vga_dev = vga_default_device(); |
| 789 | |
| 790 | if (vga_dev) |
| 791 | return sprintf(buf, "%u\n", (pdev == vga_dev)); |
Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 792 | |
| 793 | return sprintf(buf, "%u\n", |
| 794 | !!(pdev->resource[PCI_ROM_RESOURCE].flags & |
| 795 | IORESOURCE_ROM_SHADOW)); |
| 796 | } |
Sachin Kamat | bf22c90 | 2013-09-28 15:42:00 +0530 | [diff] [blame] | 797 | static struct device_attribute vga_attr = __ATTR_RO(boot_vga); |
Dave Airlie | 217f45d | 2009-03-04 05:57:05 +0000 | [diff] [blame] | 798 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 799 | static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, |
| 800 | struct bin_attribute *bin_attr, char *buf, |
| 801 | loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 803 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | unsigned int size = 64; |
| 805 | loff_t init_off = off; |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 806 | u8 *data = (u8 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | /* Several chips lock up trying to read undefined config space */ |
Linus Torvalds | ab0fa82 | 2016-04-14 12:00:21 -0700 | [diff] [blame] | 809 | if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | size = dev->cfg_size; |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 811 | else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | size = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
| 814 | if (off > size) |
| 815 | return 0; |
| 816 | if (off + count > size) { |
| 817 | size -= off; |
| 818 | count = size; |
| 819 | } else { |
| 820 | size = count; |
| 821 | } |
| 822 | |
Huang Ying | 3d8387e | 2012-08-15 09:43:03 +0800 | [diff] [blame] | 823 | pci_config_pm_runtime_get(dev); |
| 824 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 825 | if ((off & 1) && size) { |
| 826 | u8 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 827 | pci_user_read_config_byte(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 828 | data[off - init_off] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | off++; |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 830 | size--; |
| 831 | } |
| 832 | |
| 833 | if ((off & 3) && size > 2) { |
| 834 | u16 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 835 | pci_user_read_config_word(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 836 | data[off - init_off] = val & 0xff; |
| 837 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 838 | off += 2; |
| 839 | size -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | while (size > 3) { |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 843 | u32 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 844 | pci_user_read_config_dword(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 845 | data[off - init_off] = val & 0xff; |
| 846 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 847 | data[off - init_off + 2] = (val >> 16) & 0xff; |
| 848 | data[off - init_off + 3] = (val >> 24) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | off += 4; |
| 850 | size -= 4; |
| 851 | } |
| 852 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 853 | if (size >= 2) { |
| 854 | u16 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 855 | pci_user_read_config_word(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 856 | data[off - init_off] = val & 0xff; |
| 857 | data[off - init_off + 1] = (val >> 8) & 0xff; |
| 858 | off += 2; |
| 859 | size -= 2; |
| 860 | } |
| 861 | |
| 862 | if (size > 0) { |
| 863 | u8 val; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 864 | pci_user_read_config_byte(dev, off, &val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 865 | data[off - init_off] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | off++; |
| 867 | --size; |
| 868 | } |
| 869 | |
Huang Ying | 3d8387e | 2012-08-15 09:43:03 +0800 | [diff] [blame] | 870 | pci_config_pm_runtime_put(dev); |
| 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | return count; |
| 873 | } |
| 874 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 875 | static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, |
| 876 | struct bin_attribute *bin_attr, char *buf, |
| 877 | loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 879 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | unsigned int size = count; |
| 881 | loff_t init_off = off; |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 882 | u8 *data = (u8 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | if (off > dev->cfg_size) |
| 885 | return 0; |
| 886 | if (off + count > dev->cfg_size) { |
| 887 | size = dev->cfg_size - off; |
| 888 | count = size; |
| 889 | } |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 890 | |
Huang Ying | 3d8387e | 2012-08-15 09:43:03 +0800 | [diff] [blame] | 891 | pci_config_pm_runtime_get(dev); |
| 892 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 893 | if ((off & 1) && size) { |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 894 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | off++; |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 896 | size--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 898 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 899 | if ((off & 3) && size > 2) { |
| 900 | u16 val = data[off - init_off]; |
| 901 | val |= (u16) data[off - init_off + 1] << 8; |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 902 | pci_user_write_config_word(dev, off, val); |
| 903 | off += 2; |
| 904 | size -= 2; |
| 905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | while (size > 3) { |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 908 | u32 val = data[off - init_off]; |
| 909 | val |= (u32) data[off - init_off + 1] << 8; |
| 910 | val |= (u32) data[off - init_off + 2] << 16; |
| 911 | val |= (u32) data[off - init_off + 3] << 24; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 912 | pci_user_write_config_dword(dev, off, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | off += 4; |
| 914 | size -= 4; |
| 915 | } |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 916 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 917 | if (size >= 2) { |
| 918 | u16 val = data[off - init_off]; |
| 919 | val |= (u16) data[off - init_off + 1] << 8; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 920 | pci_user_write_config_word(dev, off, val); |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 921 | off += 2; |
| 922 | size -= 2; |
| 923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
ssant@in.ibm.com | 4c0619a | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 925 | if (size) { |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 926 | pci_user_write_config_byte(dev, off, data[off - init_off]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | off++; |
| 928 | --size; |
| 929 | } |
| 930 | |
Huang Ying | 3d8387e | 2012-08-15 09:43:03 +0800 | [diff] [blame] | 931 | pci_config_pm_runtime_put(dev); |
| 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return count; |
| 934 | } |
| 935 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 936 | static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj, |
| 937 | struct bin_attribute *bin_attr, char *buf, |
| 938 | loff_t off, size_t count) |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 939 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 940 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 941 | |
Hannes Reinecke | f52e562 | 2016-02-15 09:42:00 +0100 | [diff] [blame] | 942 | if (bin_attr->size > 0) { |
| 943 | if (off > bin_attr->size) |
| 944 | count = 0; |
| 945 | else if (count > bin_attr->size - off) |
| 946 | count = bin_attr->size - off; |
| 947 | } |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 948 | |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 949 | return pci_read_vpd(dev, off, count, buf); |
| 950 | } |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 951 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 952 | static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj, |
| 953 | struct bin_attribute *bin_attr, char *buf, |
| 954 | loff_t off, size_t count) |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 955 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 956 | struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 957 | |
Hannes Reinecke | f52e562 | 2016-02-15 09:42:00 +0100 | [diff] [blame] | 958 | if (bin_attr->size > 0) { |
| 959 | if (off > bin_attr->size) |
| 960 | count = 0; |
| 961 | else if (count > bin_attr->size - off) |
| 962 | count = bin_attr->size - off; |
| 963 | } |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 964 | |
| 965 | return pci_write_vpd(dev, off, count, buf); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | #ifdef HAVE_PCI_LEGACY |
| 969 | /** |
| 970 | * 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] | 971 | * @filp: open sysfs file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | * @kobj: kobject corresponding to file to read from |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 973 | * @bin_attr: struct bin_attribute for this file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | * @buf: buffer to store results |
| 975 | * @off: offset into legacy I/O port space |
| 976 | * @count: number of bytes to read |
| 977 | * |
| 978 | * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific |
| 979 | * callback routine (pci_legacy_read). |
| 980 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 981 | static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj, |
| 982 | struct bin_attribute *bin_attr, char *buf, |
| 983 | loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 985 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 987 | /* Only support 1, 2 or 4 byte accesses */ |
| 988 | if (count != 1 && count != 2 && count != 4) |
| 989 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 991 | return pci_legacy_read(bus, off, (u32 *)buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /** |
| 995 | * 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] | 996 | * @filp: open sysfs file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | * @kobj: kobject corresponding to file to read from |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 998 | * @bin_attr: struct bin_attribute for this file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | * @buf: buffer containing value to be written |
| 1000 | * @off: offset into legacy I/O port space |
| 1001 | * @count: number of bytes to write |
| 1002 | * |
| 1003 | * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific |
| 1004 | * callback routine (pci_legacy_write). |
| 1005 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1006 | static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, |
| 1007 | struct bin_attribute *bin_attr, char *buf, |
| 1008 | loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1010 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1012 | /* Only support 1, 2 or 4 byte accesses */ |
| 1013 | if (count != 1 && count != 2 && count != 4) |
| 1014 | return -EINVAL; |
| 1015 | |
| 1016 | return pci_legacy_write(bus, off, *(u32 *)buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * pci_mmap_legacy_mem - map legacy PCI memory into user memory space |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1021 | * @filp: open sysfs file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | * @kobj: kobject corresponding to device to be mapped |
| 1023 | * @attr: struct bin_attribute for this file |
| 1024 | * @vma: struct vm_area_struct passed to mmap |
| 1025 | * |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1026 | * 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] | 1027 | * legacy memory space (first meg of bus space) into application virtual |
| 1028 | * memory space. |
| 1029 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1030 | static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, |
| 1031 | struct bin_attribute *attr, |
| 1032 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1034 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1036 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /** |
| 1040 | * pci_mmap_legacy_io - map legacy PCI IO into user memory space |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1041 | * @filp: open sysfs file |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1042 | * @kobj: kobject corresponding to device to be mapped |
| 1043 | * @attr: struct bin_attribute for this file |
| 1044 | * @vma: struct vm_area_struct passed to mmap |
| 1045 | * |
| 1046 | * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap |
| 1047 | * legacy IO space (first meg of bus space) into application virtual |
| 1048 | * memory space. Returns -ENOSYS if the operation isn't supported |
| 1049 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1050 | static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, |
| 1051 | struct bin_attribute *attr, |
| 1052 | struct vm_area_struct *vma) |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1053 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1054 | struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1055 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1056 | return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /** |
Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 1060 | * pci_adjust_legacy_attr - adjustment of legacy file attributes |
| 1061 | * @b: bus to create files under |
| 1062 | * @mmap_type: I/O port or memory |
| 1063 | * |
| 1064 | * Stub implementation. Can be overridden by arch if necessary. |
| 1065 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1066 | void __weak pci_adjust_legacy_attr(struct pci_bus *b, |
| 1067 | enum pci_mmap_state mmap_type) |
Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 1068 | { |
Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /** |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1072 | * pci_create_legacy_files - create legacy I/O port and memory files |
| 1073 | * @b: bus to create files under |
| 1074 | * |
| 1075 | * Some platforms allow access to legacy I/O port and ISA memory space on |
| 1076 | * a per-bus basis. This routine creates the files and ties them into |
| 1077 | * their associated read, write and mmap files from pci-sysfs.c |
| 1078 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1079 | * On error unwind, but don't propagate the error to the caller |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1080 | * as it is ok to set up the PCI bus without these files. |
| 1081 | */ |
| 1082 | void pci_create_legacy_files(struct pci_bus *b) |
| 1083 | { |
| 1084 | int error; |
| 1085 | |
| 1086 | b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2, |
| 1087 | GFP_ATOMIC); |
| 1088 | if (!b->legacy_io) |
| 1089 | goto kzalloc_err; |
| 1090 | |
Stephen Rothwell | 62e877b8 | 2010-03-01 20:38:36 +1100 | [diff] [blame] | 1091 | sysfs_bin_attr_init(b->legacy_io); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1092 | b->legacy_io->attr.name = "legacy_io"; |
| 1093 | b->legacy_io->size = 0xffff; |
| 1094 | b->legacy_io->attr.mode = S_IRUSR | S_IWUSR; |
| 1095 | b->legacy_io->read = pci_read_legacy_io; |
| 1096 | b->legacy_io->write = pci_write_legacy_io; |
| 1097 | b->legacy_io->mmap = pci_mmap_legacy_io; |
Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 1098 | pci_adjust_legacy_attr(b, pci_mmap_io); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1099 | error = device_create_bin_file(&b->dev, b->legacy_io); |
| 1100 | if (error) |
| 1101 | goto legacy_io_err; |
| 1102 | |
| 1103 | /* Allocated above after the legacy_io struct */ |
| 1104 | b->legacy_mem = b->legacy_io + 1; |
Mel Gorman | 6757eca3 | 2010-03-10 22:48:34 +0000 | [diff] [blame] | 1105 | sysfs_bin_attr_init(b->legacy_mem); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1106 | b->legacy_mem->attr.name = "legacy_mem"; |
| 1107 | b->legacy_mem->size = 1024*1024; |
| 1108 | b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR; |
| 1109 | b->legacy_mem->mmap = pci_mmap_legacy_mem; |
Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 1110 | pci_adjust_legacy_attr(b, pci_mmap_mem); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1111 | error = device_create_bin_file(&b->dev, b->legacy_mem); |
| 1112 | if (error) |
| 1113 | goto legacy_mem_err; |
| 1114 | |
| 1115 | return; |
| 1116 | |
| 1117 | legacy_mem_err: |
| 1118 | device_remove_bin_file(&b->dev, b->legacy_io); |
| 1119 | legacy_io_err: |
| 1120 | kfree(b->legacy_io); |
| 1121 | b->legacy_io = NULL; |
| 1122 | kzalloc_err: |
Ryan Desfosses | 227f064 | 2014-04-18 20:13:50 -0400 | [diff] [blame] | 1123 | printk(KERN_WARNING "pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n"); |
Benjamin Herrenschmidt | f19aeb1 | 2008-10-03 19:49:32 +1000 | [diff] [blame] | 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | void pci_remove_legacy_files(struct pci_bus *b) |
| 1128 | { |
| 1129 | if (b->legacy_io) { |
| 1130 | device_remove_bin_file(&b->dev, b->legacy_io); |
| 1131 | device_remove_bin_file(&b->dev, b->legacy_mem); |
| 1132 | kfree(b->legacy_io); /* both are allocated here */ |
| 1133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | #endif /* HAVE_PCI_LEGACY */ |
| 1136 | |
David Woodhouse | f719582 | 2017-04-12 13:25:59 +0100 | [diff] [blame] | 1137 | #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1138 | |
Martin Wilck | 3b519e4 | 2010-11-10 11:03:21 +0100 | [diff] [blame] | 1139 | int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma, |
| 1140 | enum pci_mmap_api mmap_api) |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1141 | { |
David Woodhouse | 6bccc7f | 2017-04-12 13:25:50 +0100 | [diff] [blame] | 1142 | unsigned long nr, start, size; |
| 1143 | resource_size_t pci_start = 0, pci_end; |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1144 | |
Martin Wilck | 3b519e4 | 2010-11-10 11:03:21 +0100 | [diff] [blame] | 1145 | if (pci_resource_len(pdev, resno) == 0) |
| 1146 | return 0; |
Libin | 64b0017 | 2013-04-15 02:48:54 +0000 | [diff] [blame] | 1147 | nr = vma_pages(vma); |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1148 | start = vma->vm_pgoff; |
Ed Swierk | 88e7df0 | 2008-11-03 14:41:16 -0800 | [diff] [blame] | 1149 | size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1; |
David Woodhouse | 6bccc7f | 2017-04-12 13:25:50 +0100 | [diff] [blame] | 1150 | if (mmap_api == PCI_MMAP_PROCFS) { |
| 1151 | pci_resource_to_user(pdev, resno, &pdev->resource[resno], |
| 1152 | &pci_start, &pci_end); |
| 1153 | pci_start >>= PAGE_SHIFT; |
| 1154 | } |
Martin Wilck | 3b519e4 | 2010-11-10 11:03:21 +0100 | [diff] [blame] | 1155 | if (start >= pci_start && start < pci_start + size && |
| 1156 | start + nr <= pci_start + size) |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1157 | return 1; |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | /** |
| 1162 | * pci_mmap_resource - map a PCI resource into user memory space |
| 1163 | * @kobj: kobject for mapping |
| 1164 | * @attr: struct bin_attribute for the file being mapped |
| 1165 | * @vma: struct vm_area_struct passed into the mmap |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1166 | * @write_combine: 1 for write_combine mapping |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | * |
| 1168 | * 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] | 1169 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1170 | static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, |
| 1171 | struct vm_area_struct *vma, int write_combine) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1173 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1174 | int bar = (unsigned long)attr->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | enum pci_mmap_state mmap_type; |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1176 | struct resource *res = &pdev->resource[bar]; |
Michael Ellerman | 2311b1f | 2005-05-13 17:44:10 +1000 | [diff] [blame] | 1177 | |
Bjorn Helgaas | ca62072 | 2016-04-07 17:15:14 -0700 | [diff] [blame] | 1178 | if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start)) |
| 1179 | return -EINVAL; |
| 1180 | |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1181 | if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) { |
Ryan Desfosses | 227f064 | 2014-04-18 20:13:50 -0400 | [diff] [blame] | 1182 | WARN(1, "process \"%s\" tried to map 0x%08lx bytes at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n", |
Martin Wilck | 3b519e4 | 2010-11-10 11:03:21 +0100 | [diff] [blame] | 1183 | current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff, |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1184 | pci_name(pdev), bar, |
| 1185 | (u64)pci_resource_start(pdev, bar), |
| 1186 | (u64)pci_resource_len(pdev, bar)); |
Linus Torvalds | b5ff7df | 2008-10-02 18:52:51 -0700 | [diff] [blame] | 1187 | return -EINVAL; |
Martin Wilck | 3b519e4 | 2010-11-10 11:03:21 +0100 | [diff] [blame] | 1188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; |
David Woodhouse | f719582 | 2017-04-12 13:25:59 +0100 | [diff] [blame] | 1190 | |
| 1191 | return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1194 | static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, |
| 1195 | struct bin_attribute *attr, |
| 1196 | struct vm_area_struct *vma) |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1197 | { |
| 1198 | return pci_mmap_resource(kobj, attr, vma, 0); |
| 1199 | } |
| 1200 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1201 | static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, |
| 1202 | struct bin_attribute *attr, |
| 1203 | struct vm_area_struct *vma) |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1204 | { |
| 1205 | return pci_mmap_resource(kobj, attr, vma, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1208 | static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, |
| 1209 | struct bin_attribute *attr, char *buf, |
| 1210 | loff_t off, size_t count, bool write) |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1211 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1212 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1213 | int bar = (unsigned long)attr->private; |
| 1214 | struct resource *res; |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1215 | unsigned long port = off; |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1216 | |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1217 | res = &pdev->resource[bar]; |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1218 | |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1219 | port += pci_resource_start(pdev, bar); |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1220 | |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1221 | if (port > pci_resource_end(pdev, bar)) |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1222 | return 0; |
| 1223 | |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1224 | if (port + count - 1 > pci_resource_end(pdev, bar)) |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1225 | return -EINVAL; |
| 1226 | |
| 1227 | switch (count) { |
| 1228 | case 1: |
| 1229 | if (write) |
| 1230 | outb(*(u8 *)buf, port); |
| 1231 | else |
| 1232 | *(u8 *)buf = inb(port); |
| 1233 | return 1; |
| 1234 | case 2: |
| 1235 | if (write) |
| 1236 | outw(*(u16 *)buf, port); |
| 1237 | else |
| 1238 | *(u16 *)buf = inw(port); |
| 1239 | return 2; |
| 1240 | case 4: |
| 1241 | if (write) |
| 1242 | outl(*(u32 *)buf, port); |
| 1243 | else |
| 1244 | *(u32 *)buf = inl(port); |
| 1245 | return 4; |
| 1246 | } |
| 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1250 | static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj, |
| 1251 | struct bin_attribute *attr, char *buf, |
| 1252 | loff_t off, size_t count) |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1253 | { |
| 1254 | return pci_resource_io(filp, kobj, attr, buf, off, count, false); |
| 1255 | } |
| 1256 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1257 | static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, |
| 1258 | struct bin_attribute *attr, char *buf, |
| 1259 | loff_t off, size_t count) |
Alex Williamson | 8633328 | 2010-07-19 09:45:34 -0600 | [diff] [blame] | 1260 | { |
| 1261 | return pci_resource_io(filp, kobj, attr, buf, off, count, true); |
| 1262 | } |
| 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | /** |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1265 | * pci_remove_resource_files - cleanup resource files |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 1266 | * @pdev: dev to cleanup |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1267 | * |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 1268 | * 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] | 1269 | * free their resources. |
| 1270 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1271 | static void pci_remove_resource_files(struct pci_dev *pdev) |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1272 | { |
| 1273 | int i; |
| 1274 | |
| 1275 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { |
| 1276 | struct bin_attribute *res_attr; |
| 1277 | |
| 1278 | res_attr = pdev->res_attr[i]; |
| 1279 | if (res_attr) { |
| 1280 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); |
| 1281 | kfree(res_attr); |
| 1282 | } |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1283 | |
| 1284 | res_attr = pdev->res_attr_wc[i]; |
| 1285 | if (res_attr) { |
| 1286 | sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); |
| 1287 | kfree(res_attr); |
| 1288 | } |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1292 | static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) |
| 1293 | { |
| 1294 | /* allocate attribute structure, piggyback attribute name */ |
| 1295 | int name_len = write_combine ? 13 : 10; |
| 1296 | struct bin_attribute *res_attr; |
Bjorn Helgaas | bd5174d | 2016-03-10 14:36:34 -0600 | [diff] [blame] | 1297 | char *res_attr_name; |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1298 | int retval; |
| 1299 | |
| 1300 | res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); |
Bjorn Helgaas | bd5174d | 2016-03-10 14:36:34 -0600 | [diff] [blame] | 1301 | if (!res_attr) |
| 1302 | return -ENOMEM; |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1303 | |
Bjorn Helgaas | bd5174d | 2016-03-10 14:36:34 -0600 | [diff] [blame] | 1304 | res_attr_name = (char *)(res_attr + 1); |
| 1305 | |
| 1306 | sysfs_bin_attr_init(res_attr); |
| 1307 | if (write_combine) { |
| 1308 | pdev->res_attr_wc[num] = res_attr; |
| 1309 | sprintf(res_attr_name, "resource%d_wc", num); |
| 1310 | res_attr->mmap = pci_mmap_resource_wc; |
| 1311 | } else { |
| 1312 | pdev->res_attr[num] = res_attr; |
| 1313 | sprintf(res_attr_name, "resource%d", num); |
David Woodhouse | e854d8b | 2017-04-12 13:25:56 +0100 | [diff] [blame] | 1314 | if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { |
| 1315 | res_attr->read = pci_read_resource_io; |
| 1316 | res_attr->write = pci_write_resource_io; |
| 1317 | if (arch_can_pci_mmap_io()) |
| 1318 | res_attr->mmap = pci_mmap_resource_uc; |
| 1319 | } else { |
| 1320 | res_attr->mmap = pci_mmap_resource_uc; |
| 1321 | } |
Bjorn Helgaas | bd5174d | 2016-03-10 14:36:34 -0600 | [diff] [blame] | 1322 | } |
| 1323 | res_attr->attr.name = res_attr_name; |
| 1324 | res_attr->attr.mode = S_IRUSR | S_IWUSR; |
| 1325 | res_attr->size = pci_resource_len(pdev, num); |
David Woodhouse | dca40b1 | 2017-04-12 13:25:57 +0100 | [diff] [blame] | 1326 | res_attr->private = (void *)(unsigned long)num; |
Bjorn Helgaas | bd5174d | 2016-03-10 14:36:34 -0600 | [diff] [blame] | 1327 | retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); |
| 1328 | if (retval) |
| 1329 | kfree(res_attr); |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1330 | |
| 1331 | return retval; |
| 1332 | } |
| 1333 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1334 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | * pci_create_resource_files - create resource files in sysfs for @dev |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 1336 | * @pdev: dev in question |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | * |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 1338 | * Walk the resources in @pdev creating files for each resource available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | */ |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1340 | static int pci_create_resource_files(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | { |
| 1342 | int i; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1343 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
| 1345 | /* Expose the PCI resources from this device as files */ |
| 1346 | for (i = 0; i < PCI_ROM_RESOURCE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | /* skip empty resources */ |
| 1349 | if (!pci_resource_len(pdev, i)) |
| 1350 | continue; |
| 1351 | |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1352 | retval = pci_create_attr(pdev, i, 0); |
| 1353 | /* for prefetchable resources, create a WC mappable file */ |
David Woodhouse | ae749c7 | 2017-04-12 13:25:54 +0100 | [diff] [blame] | 1354 | if (!retval && arch_can_pci_mmap_wc() && |
| 1355 | pdev->resource[i].flags & IORESOURCE_PREFETCH) |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1356 | retval = pci_create_attr(pdev, i, 1); |
venkatesh.pallipadi@intel.com | 45aec1ae | 2008-03-18 17:00:22 -0700 | [diff] [blame] | 1357 | if (retval) { |
| 1358 | pci_remove_resource_files(pdev); |
| 1359 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1362 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | #else /* !HAVE_PCI_MMAP */ |
Ivan Kokshaysky | 10a0ef3 | 2009-02-17 13:46:53 +0300 | [diff] [blame] | 1365 | int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } |
| 1366 | void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | #endif /* HAVE_PCI_MMAP */ |
| 1368 | |
| 1369 | /** |
| 1370 | * pci_write_rom - used to enable access to the PCI ROM display |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1371 | * @filp: sysfs file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | * @kobj: kernel object handle |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 1373 | * @bin_attr: struct bin_attribute for this file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | * @buf: user input |
| 1375 | * @off: file offset |
| 1376 | * @count: number of byte in input |
| 1377 | * |
| 1378 | * writing anything except 0 enables it |
| 1379 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1380 | static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, |
| 1381 | struct bin_attribute *bin_attr, char *buf, |
| 1382 | loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1384 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
| 1386 | if ((off == 0) && (*buf == '0') && (count == 2)) |
| 1387 | pdev->rom_attr_enabled = 0; |
| 1388 | else |
| 1389 | pdev->rom_attr_enabled = 1; |
| 1390 | |
| 1391 | return count; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * pci_read_rom - read a PCI ROM |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 1396 | * @filp: sysfs file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | * @kobj: kernel object handle |
Randy Dunlap | cffb2fa | 2009-04-10 15:17:50 -0700 | [diff] [blame] | 1398 | * @bin_attr: struct bin_attribute for this file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | * @buf: where to put the data we read from the ROM |
| 1400 | * @off: file offset |
| 1401 | * @count: number of bytes to read |
| 1402 | * |
| 1403 | * Put @count bytes starting at @off into @buf from the ROM in the PCI |
| 1404 | * device corresponding to @kobj. |
| 1405 | */ |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1406 | static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, |
| 1407 | struct bin_attribute *bin_attr, char *buf, |
| 1408 | loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1410 | struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | void __iomem *rom; |
| 1412 | size_t size; |
| 1413 | |
| 1414 | if (!pdev->rom_attr_enabled) |
| 1415 | return -EINVAL; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 1416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | 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] | 1418 | if (!rom || !size) |
| 1419 | return -EIO; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | if (off >= size) |
| 1422 | count = 0; |
| 1423 | else { |
| 1424 | if (off + count > size) |
| 1425 | count = size - off; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | memcpy_fromio(buf, rom + off, count); |
| 1428 | } |
| 1429 | pci_unmap_rom(pdev, rom); |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 1430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | return count; |
| 1432 | } |
| 1433 | |
| 1434 | static struct bin_attribute pci_config_attr = { |
| 1435 | .attr = { |
| 1436 | .name = "config", |
| 1437 | .mode = S_IRUGO | S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | }, |
Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 1439 | .size = PCI_CFG_SPACE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | .read = pci_read_config, |
| 1441 | .write = pci_write_config, |
| 1442 | }; |
| 1443 | |
| 1444 | static struct bin_attribute pcie_config_attr = { |
| 1445 | .attr = { |
| 1446 | .name = "config", |
| 1447 | .mode = S_IRUGO | S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | }, |
Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 1449 | .size = PCI_CFG_SPACE_EXP_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | .read = pci_read_config, |
| 1451 | .write = pci_write_config, |
| 1452 | }; |
| 1453 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1454 | static ssize_t reset_store(struct device *dev, struct device_attribute *attr, |
| 1455 | const char *buf, size_t count) |
Michael S. Tsirkin | 711d577 | 2009-07-27 23:37:48 +0300 | [diff] [blame] | 1456 | { |
| 1457 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1458 | unsigned long val; |
Jingoo Han | 9a994e8 | 2013-06-01 16:25:25 +0900 | [diff] [blame] | 1459 | ssize_t result = kstrtoul(buf, 0, &val); |
Michael S. Tsirkin | 711d577 | 2009-07-27 23:37:48 +0300 | [diff] [blame] | 1460 | |
| 1461 | if (result < 0) |
| 1462 | return result; |
| 1463 | |
| 1464 | if (val != 1) |
| 1465 | return -EINVAL; |
Michal Schmidt | 447c5dd | 2010-05-11 11:44:54 +0200 | [diff] [blame] | 1466 | |
| 1467 | result = pci_reset_function(pdev); |
| 1468 | if (result < 0) |
| 1469 | return result; |
| 1470 | |
| 1471 | return count; |
Michael S. Tsirkin | 711d577 | 2009-07-27 23:37:48 +0300 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store); |
| 1475 | |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1476 | static int pci_create_capabilities_sysfs(struct pci_dev *dev) |
| 1477 | { |
| 1478 | int retval; |
| 1479 | struct bin_attribute *attr; |
| 1480 | |
| 1481 | /* If the device has VPD, try to expose it in sysfs. */ |
| 1482 | if (dev->vpd) { |
| 1483 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
| 1484 | if (!attr) |
| 1485 | return -ENOMEM; |
| 1486 | |
Eric W. Biederman | a07e415 | 2010-02-11 15:23:05 -0800 | [diff] [blame] | 1487 | sysfs_bin_attr_init(attr); |
Hannes Reinecke | 104daa7 | 2016-02-15 09:42:01 +0100 | [diff] [blame] | 1488 | attr->size = 0; |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1489 | attr->attr.name = "vpd"; |
| 1490 | attr->attr.mode = S_IRUSR | S_IWUSR; |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 1491 | attr->read = read_vpd_attr; |
| 1492 | attr->write = write_vpd_attr; |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1493 | retval = sysfs_create_bin_file(&dev->dev.kobj, attr); |
| 1494 | if (retval) { |
Ben Hutchings | 0f12a4e | 2011-01-13 19:47:56 +0000 | [diff] [blame] | 1495 | kfree(attr); |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1496 | return retval; |
| 1497 | } |
| 1498 | dev->vpd->attr = attr; |
| 1499 | } |
| 1500 | |
| 1501 | /* Active State Power Management */ |
| 1502 | pcie_aspm_create_sysfs_dev_files(dev); |
| 1503 | |
Michael S. Tsirkin | 711d577 | 2009-07-27 23:37:48 +0300 | [diff] [blame] | 1504 | if (!pci_probe_reset_function(dev)) { |
| 1505 | retval = device_create_file(&dev->dev, &reset_attr); |
| 1506 | if (retval) |
| 1507 | goto error; |
| 1508 | dev->reset_fn = 1; |
| 1509 | } |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1510 | return 0; |
Michael S. Tsirkin | 711d577 | 2009-07-27 23:37:48 +0300 | [diff] [blame] | 1511 | |
| 1512 | error: |
| 1513 | pcie_aspm_remove_sysfs_dev_files(dev); |
| 1514 | if (dev->vpd && dev->vpd->attr) { |
| 1515 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); |
| 1516 | kfree(dev->vpd->attr); |
| 1517 | } |
| 1518 | |
| 1519 | return retval; |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1520 | } |
| 1521 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1522 | int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | { |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1524 | int retval; |
Bjorn Helgaas | ac0c302 | 2016-03-12 05:48:08 -0600 | [diff] [blame] | 1525 | int rom_size; |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1526 | struct bin_attribute *attr; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | if (!sysfs_initialized) |
| 1529 | return -EACCES; |
| 1530 | |
Jason S. McMullan | c20aecf | 2015-09-30 15:35:05 +0900 | [diff] [blame] | 1531 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1532 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
Jason S. McMullan | c20aecf | 2015-09-30 15:35:05 +0900 | [diff] [blame] | 1533 | else |
| 1534 | retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1535 | if (retval) |
| 1536 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1538 | retval = pci_create_resource_files(pdev); |
| 1539 | if (retval) |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1540 | goto err_config_file; |
| 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | /* If the device has a ROM, try to expose it in sysfs. */ |
Bjorn Helgaas | ac0c302 | 2016-03-12 05:48:08 -0600 | [diff] [blame] | 1543 | rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1544 | if (rom_size) { |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 1545 | attr = kzalloc(sizeof(*attr), GFP_ATOMIC); |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1546 | if (!attr) { |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1547 | retval = -ENOMEM; |
Michael Ellerman | 9890b12 | 2007-04-18 13:34:12 +1000 | [diff] [blame] | 1548 | goto err_resource_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | } |
Eric W. Biederman | a07e415 | 2010-02-11 15:23:05 -0800 | [diff] [blame] | 1550 | sysfs_bin_attr_init(attr); |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1551 | attr->size = rom_size; |
| 1552 | attr->attr.name = "rom"; |
Alex Williamson | ff29530 | 2011-01-05 10:26:41 -0700 | [diff] [blame] | 1553 | attr->attr.mode = S_IRUSR | S_IWUSR; |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1554 | attr->read = pci_read_rom; |
| 1555 | attr->write = pci_write_rom; |
| 1556 | retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); |
| 1557 | if (retval) { |
| 1558 | kfree(attr); |
| 1559 | goto err_resource_files; |
| 1560 | } |
| 1561 | pdev->rom_attr = attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1563 | |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1564 | /* add sysfs entries for various capabilities */ |
| 1565 | retval = pci_create_capabilities_sysfs(pdev); |
| 1566 | if (retval) |
Yinghai Lu | 625e1d5 | 2012-11-05 15:20:35 -0500 | [diff] [blame] | 1567 | goto err_rom_file; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 1568 | |
Narendra K | 911e1c9 | 2010-07-26 05:56:50 -0500 | [diff] [blame] | 1569 | pci_create_firmware_label_files(pdev); |
| 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | return 0; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1572 | |
Michael Ellerman | a2cd52c | 2007-05-08 12:03:08 +1000 | [diff] [blame] | 1573 | err_rom_file: |
Bjorn Helgaas | 9d88b93 | 2016-03-01 09:37:10 -0600 | [diff] [blame] | 1574 | if (pdev->rom_attr) { |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 1575 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1576 | kfree(pdev->rom_attr); |
| 1577 | pdev->rom_attr = NULL; |
| 1578 | } |
Michael Ellerman | 9890b12 | 2007-04-18 13:34:12 +1000 | [diff] [blame] | 1579 | err_resource_files: |
| 1580 | pci_remove_resource_files(pdev); |
Ben Hutchings | 94e6108 | 2008-03-05 16:52:39 +0000 | [diff] [blame] | 1581 | err_config_file: |
Jason S. McMullan | c20aecf | 2015-09-30 15:35:05 +0900 | [diff] [blame] | 1582 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1583 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
Jason S. McMullan | c20aecf | 2015-09-30 15:35:05 +0900 | [diff] [blame] | 1584 | else |
| 1585 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1586 | err: |
| 1587 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1590 | static void pci_remove_capabilities_sysfs(struct pci_dev *dev) |
| 1591 | { |
| 1592 | if (dev->vpd && dev->vpd->attr) { |
| 1593 | sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); |
| 1594 | kfree(dev->vpd->attr); |
| 1595 | } |
| 1596 | |
| 1597 | pcie_aspm_remove_sysfs_dev_files(dev); |
Michael S. Tsirkin | 711d577 | 2009-07-27 23:37:48 +0300 | [diff] [blame] | 1598 | if (dev->reset_fn) { |
| 1599 | device_remove_file(&dev->dev, &reset_attr); |
| 1600 | dev->reset_fn = 0; |
| 1601 | } |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | /** |
| 1605 | * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files |
| 1606 | * @pdev: device whose entries we should free |
| 1607 | * |
| 1608 | * Cleanup when @pdev is removed from sysfs. |
| 1609 | */ |
| 1610 | void pci_remove_sysfs_dev_files(struct pci_dev *pdev) |
| 1611 | { |
David Miller | d67afe5 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1612 | if (!sysfs_initialized) |
| 1613 | return; |
| 1614 | |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1615 | pci_remove_capabilities_sysfs(pdev); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 1616 | |
Jason S. McMullan | c20aecf | 2015-09-30 15:35:05 +0900 | [diff] [blame] | 1617 | if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); |
Jason S. McMullan | c20aecf | 2015-09-30 15:35:05 +0900 | [diff] [blame] | 1619 | else |
| 1620 | sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
| 1622 | pci_remove_resource_files(pdev); |
| 1623 | |
Bjorn Helgaas | 9d88b93 | 2016-03-01 09:37:10 -0600 | [diff] [blame] | 1624 | if (pdev->rom_attr) { |
Zhao, Yu | 280c73d | 2008-10-13 20:01:00 +0800 | [diff] [blame] | 1625 | sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); |
| 1626 | kfree(pdev->rom_attr); |
Bjorn Helgaas | 9d88b93 | 2016-03-01 09:37:10 -0600 | [diff] [blame] | 1627 | pdev->rom_attr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | } |
Narendra K | 911e1c9 | 2010-07-26 05:56:50 -0500 | [diff] [blame] | 1629 | |
| 1630 | pci_remove_firmware_label_files(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | static int __init pci_sysfs_init(void) |
| 1634 | { |
| 1635 | struct pci_dev *pdev = NULL; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1636 | int retval; |
| 1637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | sysfs_initialized = 1; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1639 | for_each_pci_dev(pdev) { |
| 1640 | retval = pci_create_sysfs_dev_files(pdev); |
Julia Lawall | 151fc5d | 2007-11-20 08:41:16 +0100 | [diff] [blame] | 1641 | if (retval) { |
| 1642 | pci_dev_put(pdev); |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1643 | return retval; |
Julia Lawall | 151fc5d | 2007-11-20 08:41:16 +0100 | [diff] [blame] | 1644 | } |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 1645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
| 1647 | return 0; |
| 1648 | } |
Jesse Barnes | 40ee9e9 | 2007-03-24 11:03:32 -0700 | [diff] [blame] | 1649 | late_initcall(pci_sysfs_init); |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1650 | |
| 1651 | static struct attribute *pci_dev_dev_attrs[] = { |
Yinghai Lu | 625e1d5 | 2012-11-05 15:20:35 -0500 | [diff] [blame] | 1652 | &vga_attr.attr, |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1653 | NULL, |
| 1654 | }; |
| 1655 | |
| 1656 | static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1657 | struct attribute *a, int n) |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1658 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1659 | struct device *dev = kobj_to_dev(kobj); |
Yinghai Lu | 625e1d5 | 2012-11-05 15:20:35 -0500 | [diff] [blame] | 1660 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1661 | |
| 1662 | if (a == &vga_attr.attr) |
| 1663 | if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) |
| 1664 | return 0; |
| 1665 | |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1666 | return a->mode; |
| 1667 | } |
| 1668 | |
Jiang Liu | dfab88b | 2013-05-31 12:21:31 +0800 | [diff] [blame] | 1669 | static struct attribute *pci_dev_hp_attrs[] = { |
| 1670 | &dev_remove_attr.attr, |
| 1671 | &dev_rescan_attr.attr, |
| 1672 | NULL, |
| 1673 | }; |
| 1674 | |
| 1675 | static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1676 | struct attribute *a, int n) |
Jiang Liu | dfab88b | 2013-05-31 12:21:31 +0800 | [diff] [blame] | 1677 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1678 | struct device *dev = kobj_to_dev(kobj); |
Jiang Liu | dfab88b | 2013-05-31 12:21:31 +0800 | [diff] [blame] | 1679 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1680 | |
| 1681 | if (pdev->is_virtfn) |
| 1682 | return 0; |
| 1683 | |
| 1684 | return a->mode; |
| 1685 | } |
| 1686 | |
Wong Vee Khee | 56c1af4 | 2017-06-01 17:43:06 +0800 | [diff] [blame] | 1687 | static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, |
| 1688 | struct attribute *a, int n) |
| 1689 | { |
| 1690 | struct device *dev = kobj_to_dev(kobj); |
| 1691 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1692 | |
| 1693 | if (pci_is_bridge(pdev)) |
| 1694 | return a->mode; |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, |
| 1700 | struct attribute *a, int n) |
| 1701 | { |
| 1702 | struct device *dev = kobj_to_dev(kobj); |
| 1703 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1704 | |
| 1705 | if (pci_is_pcie(pdev)) |
| 1706 | return a->mode; |
| 1707 | |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static const struct attribute_group pci_dev_group = { |
| 1712 | .attrs = pci_dev_attrs, |
| 1713 | }; |
| 1714 | |
| 1715 | const struct attribute_group *pci_dev_groups[] = { |
| 1716 | &pci_dev_group, |
| 1717 | NULL, |
| 1718 | }; |
| 1719 | |
| 1720 | static const struct attribute_group pci_bridge_group = { |
| 1721 | .attrs = pci_bridge_attrs, |
| 1722 | }; |
| 1723 | |
| 1724 | const struct attribute_group *pci_bridge_groups[] = { |
| 1725 | &pci_bridge_group, |
| 1726 | NULL, |
| 1727 | }; |
| 1728 | |
| 1729 | static const struct attribute_group pcie_dev_group = { |
| 1730 | .attrs = pcie_dev_attrs, |
| 1731 | }; |
| 1732 | |
| 1733 | const struct attribute_group *pcie_dev_groups[] = { |
| 1734 | &pcie_dev_group, |
| 1735 | NULL, |
| 1736 | }; |
| 1737 | |
Jiang Liu | dfab88b | 2013-05-31 12:21:31 +0800 | [diff] [blame] | 1738 | static struct attribute_group pci_dev_hp_attr_group = { |
| 1739 | .attrs = pci_dev_hp_attrs, |
| 1740 | .is_visible = pci_dev_hp_attrs_are_visible, |
| 1741 | }; |
| 1742 | |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 1743 | #ifdef CONFIG_PCI_IOV |
| 1744 | static struct attribute *sriov_dev_attrs[] = { |
| 1745 | &sriov_totalvfs_attr.attr, |
| 1746 | &sriov_numvfs_attr.attr, |
Bodong Wang | 0e7df22 | 2017-04-13 01:51:40 +0300 | [diff] [blame] | 1747 | &sriov_drivers_autoprobe_attr.attr, |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 1748 | NULL, |
| 1749 | }; |
| 1750 | |
| 1751 | static umode_t sriov_attrs_are_visible(struct kobject *kobj, |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 1752 | struct attribute *a, int n) |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 1753 | { |
Geliang Tang | 554a603 | 2015-12-23 20:28:13 +0800 | [diff] [blame] | 1754 | struct device *dev = kobj_to_dev(kobj); |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 1755 | |
| 1756 | if (!dev_is_pf(dev)) |
| 1757 | return 0; |
| 1758 | |
| 1759 | return a->mode; |
| 1760 | } |
| 1761 | |
| 1762 | static struct attribute_group sriov_dev_attr_group = { |
| 1763 | .attrs = sriov_dev_attrs, |
| 1764 | .is_visible = sriov_attrs_are_visible, |
| 1765 | }; |
| 1766 | #endif /* CONFIG_PCI_IOV */ |
| 1767 | |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1768 | static struct attribute_group pci_dev_attr_group = { |
| 1769 | .attrs = pci_dev_dev_attrs, |
| 1770 | .is_visible = pci_dev_attrs_are_visible, |
| 1771 | }; |
| 1772 | |
Wong Vee Khee | 56c1af4 | 2017-06-01 17:43:06 +0800 | [diff] [blame] | 1773 | static struct attribute_group pci_bridge_attr_group = { |
| 1774 | .attrs = pci_bridge_attrs, |
| 1775 | .is_visible = pci_bridge_attrs_are_visible, |
| 1776 | }; |
| 1777 | |
| 1778 | static struct attribute_group pcie_dev_attr_group = { |
| 1779 | .attrs = pcie_dev_attrs, |
| 1780 | .is_visible = pcie_dev_attrs_are_visible, |
| 1781 | }; |
| 1782 | |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1783 | static const struct attribute_group *pci_dev_attr_groups[] = { |
| 1784 | &pci_dev_attr_group, |
Jiang Liu | dfab88b | 2013-05-31 12:21:31 +0800 | [diff] [blame] | 1785 | &pci_dev_hp_attr_group, |
Donald Dutile | 1789382 | 2012-11-05 15:20:36 -0500 | [diff] [blame] | 1786 | #ifdef CONFIG_PCI_IOV |
| 1787 | &sriov_dev_attr_group, |
| 1788 | #endif |
Wong Vee Khee | 56c1af4 | 2017-06-01 17:43:06 +0800 | [diff] [blame] | 1789 | &pci_bridge_attr_group, |
| 1790 | &pcie_dev_attr_group, |
Yinghai Lu | 4e15c46 | 2012-11-05 15:20:34 -0500 | [diff] [blame] | 1791 | NULL, |
| 1792 | }; |
| 1793 | |
| 1794 | struct device_type pci_dev_type = { |
| 1795 | .groups = pci_dev_attr_groups, |
| 1796 | }; |