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