blob: 63d0952684fbc069294f48abb29217e3e3636ddd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Helgaasf7625982013-11-14 11:28:18 -070014 * Modeled after usb's driverfs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 */
17
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/pci.h>
22#include <linux/stat.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/topology.h>
25#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070026#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070027#include <linux/capability.h>
Chris Wrighta628e7b2011-02-14 17:21:49 -080028#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080029#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Matthew Garrett1a39b312012-04-16 16:26:02 -040031#include <linux/vgaarb.h>
Huang Ying448bd852012-06-23 10:23:51 +080032#include <linux/pm_runtime.h>
Sebastian Ottdfc73e72014-04-17 19:46:15 +020033#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "pci.h"
35
36static int sysfs_initialized; /* = 0 */
37
38/* show configuration fields */
39#define pci_config_attr(field, format_string) \
40static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040041field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{ \
43 struct pci_dev *pdev; \
44 \
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040045 pdev = to_pci_dev(dev); \
46 return sprintf(buf, format_string, pdev->field); \
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070047} \
48static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50pci_config_attr(vendor, "0x%04x\n");
51pci_config_attr(device, "0x%04x\n");
52pci_config_attr(subsystem_vendor, "0x%04x\n");
53pci_config_attr(subsystem_device, "0x%04x\n");
Emil Velikov702ed3b2016-11-21 16:24:49 -060054pci_config_attr(revision, "0x%02x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055pci_config_attr(class, "0x%06x\n");
56pci_config_attr(irq, "%u\n");
57
Doug Thompsonbdee9d92006-06-14 16:59:48 -070058static 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 Desfosses3c78bc62014-04-18 20:13:49 -040063 return sprintf(buf, "%u\n", pdev->broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070064}
65
66static 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 Piepho92425a42008-11-30 17:10:12 -080071 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070072
Jingoo Han9a994e82013-06-01 16:25:25 +090073 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080074 return -EINVAL;
75
76 pdev->broken_parity_status = !!val;
77
78 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070079}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070080static DEVICE_ATTR_RW(broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070081
Sudeep Holla5aaba362014-09-30 14:48:22 +010082static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040083 struct device_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070084{
Mike Travis3be83052009-01-04 05:18:01 -080085 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070086
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020087#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053088 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
89 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020090#else
Mike Travis3be83052009-01-04 05:18:01 -080091 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020092#endif
Sudeep Holla5aaba362014-09-30 14:48:22 +010093 return cpumap_print_to_pagebuf(list, buf, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Yijing Wangc489f5f2013-09-30 15:02:38 +080096static ssize_t local_cpus_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040097 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +080098{
Sudeep Holla5aaba362014-09-30 14:48:22 +010099 return pci_dev_show_local_cpu(dev, false, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800100}
Bjorn Helgaas33de1b82013-10-31 14:12:40 -0600101static DEVICE_ATTR_RO(local_cpus);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800102
103static ssize_t local_cpulist_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400104 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +0800105{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100106 return pci_dev_show_local_cpu(dev, true, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800107}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700108static DEVICE_ATTR_RO(local_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700110/*
111 * PCI Bus Class Devices
112 */
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700113static ssize_t cpuaffinity_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700115{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100116 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
117
118 return cpumap_print_to_pagebuf(false, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700119}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700120static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700121
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700122static ssize_t cpulistaffinity_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700124{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100125 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
126
127 return cpumap_print_to_pagebuf(true, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700128}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700129static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* show resources */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400132static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
133 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400135 struct pci_dev *pci_dev = to_pci_dev(dev);
136 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800138 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700139 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (pci_dev->subordinate)
142 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800143 else
144 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000147 struct resource *res = &pci_dev->resource[i];
148 pci_resource_to_user(pci_dev, i, res, &start, &end);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400149 str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000150 (unsigned long long)start,
151 (unsigned long long)end,
152 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 return (str - buf);
155}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700156static DEVICE_ATTR_RO(resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800158static ssize_t max_link_speed_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
160{
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500161 struct pci_dev *pdev = to_pci_dev(dev);
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800162
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500163 return sprintf(buf, "%s\n", PCIE_SPEED2STR(pcie_get_speed_cap(pdev)));
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800164}
165static DEVICE_ATTR_RO(max_link_speed);
166
167static ssize_t max_link_width_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
Tal Gilboac70b65f2018-03-30 08:24:36 -0500170 struct pci_dev *pdev = to_pci_dev(dev);
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800171
Tal Gilboac70b65f2018-03-30 08:24:36 -0500172 return sprintf(buf, "%u\n", pcie_get_width_cap(pdev));
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800173}
174static DEVICE_ATTR_RO(max_link_width);
175
176static ssize_t current_link_speed_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
179 struct pci_dev *pci_dev = to_pci_dev(dev);
180 u16 linkstat;
181 int err;
182 const char *speed;
183
184 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
185 if (err)
186 return -EINVAL;
187
188 switch (linkstat & PCI_EXP_LNKSTA_CLS) {
Jay Fang1acfb9b2018-03-12 17:13:32 +0800189 case PCI_EXP_LNKSTA_CLS_16_0GB:
190 speed = "16 GT/s";
191 break;
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800192 case PCI_EXP_LNKSTA_CLS_8_0GB:
193 speed = "8 GT/s";
194 break;
195 case PCI_EXP_LNKSTA_CLS_5_0GB:
196 speed = "5 GT/s";
197 break;
198 case PCI_EXP_LNKSTA_CLS_2_5GB:
199 speed = "2.5 GT/s";
200 break;
201 default:
202 speed = "Unknown speed";
203 }
204
205 return sprintf(buf, "%s\n", speed);
206}
207static DEVICE_ATTR_RO(current_link_speed);
208
209static ssize_t current_link_width_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 struct pci_dev *pci_dev = to_pci_dev(dev);
213 u16 linkstat;
214 int err;
215
216 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
217 if (err)
218 return -EINVAL;
219
220 return sprintf(buf, "%u\n",
221 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
222}
223static DEVICE_ATTR_RO(current_link_width);
224
225static ssize_t secondary_bus_number_show(struct device *dev,
226 struct device_attribute *attr,
227 char *buf)
228{
229 struct pci_dev *pci_dev = to_pci_dev(dev);
230 u8 sec_bus;
231 int err;
232
233 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
234 if (err)
235 return -EINVAL;
236
237 return sprintf(buf, "%u\n", sec_bus);
238}
239static DEVICE_ATTR_RO(secondary_bus_number);
240
241static ssize_t subordinate_bus_number_show(struct device *dev,
242 struct device_attribute *attr,
243 char *buf)
244{
245 struct pci_dev *pci_dev = to_pci_dev(dev);
246 u8 sub_bus;
247 int err;
248
249 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
250 if (err)
251 return -EINVAL;
252
253 return sprintf(buf, "%u\n", sub_bus);
254}
255static DEVICE_ATTR_RO(subordinate_bus_number);
256
Stuart Hayes0077a842018-01-05 12:51:55 -0600257static ssize_t ari_enabled_show(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
260{
261 struct pci_dev *pci_dev = to_pci_dev(dev);
262
263 return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
264}
265static DEVICE_ATTR_RO(ari_enabled);
266
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400267static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
268 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700269{
270 struct pci_dev *pci_dev = to_pci_dev(dev);
271
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200272 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700273 pci_dev->vendor, pci_dev->device,
274 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
275 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
276 (u8)(pci_dev->class));
277}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700278static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800279
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700280static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400281 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200282{
283 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800284 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900285 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800286
287 if (result < 0)
288 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200289
290 /* this can crash the machine when done on the "wrong" device */
291 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800292 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200293
Trent Piepho92425a42008-11-30 17:10:12 -0800294 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900295 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800296 pci_disable_device(pdev);
297 else
298 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800299 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800300 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200301
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800302 return result < 0 ? result : count;
303}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200304
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700305static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400306 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800307{
308 struct pci_dev *pdev;
309
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400310 pdev = to_pci_dev(dev);
311 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200312}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700313static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200314
Brice Goglin81bb0e12007-01-28 10:53:40 +0100315#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400316static ssize_t numa_node_store(struct device *dev,
317 struct device_attribute *attr, const char *buf,
318 size_t count)
319{
320 struct pci_dev *pdev = to_pci_dev(dev);
321 int node, ret;
322
323 if (!capable(CAP_SYS_ADMIN))
324 return -EPERM;
325
326 ret = kstrtoint(buf, 0, &node);
327 if (ret)
328 return ret;
329
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100330 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
331 return -EINVAL;
332
333 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400334 return -EINVAL;
335
336 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600337 pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
Prarit Bhargava63692df2014-10-23 14:22:12 -0400338 node);
339
340 dev->numa_node = node;
341 return count;
342}
343
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400344static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
345 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100346{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400347 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100348}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400349static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100350#endif
351
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400352static ssize_t dma_mask_bits_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800354{
355 struct pci_dev *pdev = to_pci_dev(dev);
356
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400357 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800358}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700359static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800360
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400361static ssize_t consistent_dma_mask_bits_show(struct device *dev,
362 struct device_attribute *attr,
363 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800364{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400365 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800366}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700367static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800368
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400369static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
370 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400371{
372 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800373 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400374
Yijing Wang468ff152014-09-23 13:27:24 +0800375 return sprintf(buf, "%u\n", subordinate ?
376 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
377 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400378}
379
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400380static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
381 const char *buf, size_t count)
Brice Goglinfe970642006-08-31 01:55:15 -0400382{
383 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800384 struct pci_bus *subordinate = pdev->subordinate;
Trent Piepho92425a42008-11-30 17:10:12 -0800385 unsigned long val;
386
Jingoo Han9a994e82013-06-01 16:25:25 +0900387 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800388 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400389
Brice Goglinfe970642006-08-31 01:55:15 -0400390 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800391 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400392
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700393 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800394 * "no_msi" and "bus_flags" only affect what happens when a driver
395 * requests MSI or MSI-X. They don't affect any drivers that have
396 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700397 */
Yijing Wang468ff152014-09-23 13:27:24 +0800398 if (!subordinate) {
399 pdev->no_msi = !val;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600400 pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
Yijing Wang468ff152014-09-23 13:27:24 +0800401 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400402 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400403 }
404
Yijing Wang468ff152014-09-23 13:27:24 +0800405 if (val)
406 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
407 else
408 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
409
410 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
411 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400412 return count;
413}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700414static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700415
Alex Chiang705b1aa2009-03-20 14:56:31 -0600416static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
417 size_t count)
418{
419 unsigned long val;
420 struct pci_bus *b = NULL;
421
Jingoo Han9a994e82013-06-01 16:25:25 +0900422 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600423 return -EINVAL;
424
425 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100426 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600427 while ((b = pci_find_next_bus(b)) != NULL)
428 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100429 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600430 }
431 return count;
432}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600433static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600434
Sachin Kamatbf22c902013-09-28 15:42:00 +0530435static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600436 &bus_attr_rescan.attr,
437 NULL,
438};
439
440static const struct attribute_group pci_bus_group = {
441 .attrs = pci_bus_attrs,
442};
443
444const struct attribute_group *pci_bus_groups[] = {
445 &pci_bus_group,
446 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600447};
Alex Chiang77c27c72009-03-20 14:56:36 -0600448
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400449static ssize_t dev_rescan_store(struct device *dev,
450 struct device_attribute *attr, const char *buf,
451 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600452{
453 unsigned long val;
454 struct pci_dev *pdev = to_pci_dev(dev);
455
Jingoo Han9a994e82013-06-01 16:25:25 +0900456 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600457 return -EINVAL;
458
459 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100460 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600461 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100462 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600463 }
464 return count;
465}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530466static struct device_attribute dev_rescan_attr = __ATTR(rescan,
467 (S_IWUSR|S_IWGRP),
468 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600469
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400470static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
471 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600472{
Alex Chiang77c27c72009-03-20 14:56:36 -0600473 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600474
Jingoo Han9a994e82013-06-01 16:25:25 +0900475 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600476 return -EINVAL;
477
Tejun Heobc6caf02014-02-03 14:03:02 -0500478 if (val && device_remove_file_self(dev, attr))
479 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600480 return count;
481}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530482static struct device_attribute dev_remove_attr = __ATTR(remove,
483 (S_IWUSR|S_IWGRP),
484 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700485
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400486static ssize_t dev_bus_rescan_store(struct device *dev,
487 struct device_attribute *attr,
488 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700489{
490 unsigned long val;
491 struct pci_bus *bus = to_pci_bus(dev);
492
Jingoo Han9a994e82013-06-01 16:25:25 +0900493 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700494 return -EINVAL;
495
496 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100497 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800498 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
499 pci_rescan_bus_bridge_resize(bus->self);
500 else
501 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100502 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700503 }
504 return count;
505}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700506static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700507
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100508#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800509static ssize_t d3cold_allowed_store(struct device *dev,
510 struct device_attribute *attr,
511 const char *buf, size_t count)
512{
513 struct pci_dev *pdev = to_pci_dev(dev);
514 unsigned long val;
515
Jingoo Han9a994e82013-06-01 16:25:25 +0900516 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800517 return -EINVAL;
518
519 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300520 if (pdev->d3cold_allowed)
521 pci_d3cold_enable(pdev);
522 else
523 pci_d3cold_disable(pdev);
524
Huang Ying448bd852012-06-23 10:23:51 +0800525 pm_runtime_resume(dev);
526
527 return count;
528}
529
530static ssize_t d3cold_allowed_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400534 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800535}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700536static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800537#endif
538
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200539#ifdef CONFIG_OF
540static ssize_t devspec_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
543 struct pci_dev *pdev = to_pci_dev(dev);
544 struct device_node *np = pci_device_to_OF_node(pdev);
545
Rob Herringb63773a2017-07-18 16:43:21 -0500546 if (np == NULL)
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200547 return 0;
Rob Herringb63773a2017-07-18 16:43:21 -0500548 return sprintf(buf, "%pOF", np);
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200549}
550static DEVICE_ATTR_RO(devspec);
551#endif
552
Donald Dutile17893822012-11-05 15:20:36 -0500553#ifdef CONFIG_PCI_IOV
554static ssize_t sriov_totalvfs_show(struct device *dev,
555 struct device_attribute *attr,
556 char *buf)
557{
558 struct pci_dev *pdev = to_pci_dev(dev);
559
Donald Dutilebff73152012-11-05 15:20:37 -0500560 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500561}
562
563
564static ssize_t sriov_numvfs_show(struct device *dev,
565 struct device_attribute *attr,
566 char *buf)
567{
568 struct pci_dev *pdev = to_pci_dev(dev);
569
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700570 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500571}
572
573/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700574 * num_vfs > 0; number of VFs to enable
575 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500576 *
577 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700578 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500579 */
580static ssize_t sriov_numvfs_store(struct device *dev,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
583{
584 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700585 int ret;
586 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500587
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700588 ret = kstrtou16(buf, 0, &num_vfs);
589 if (ret < 0)
590 return ret;
591
592 if (num_vfs > pci_sriov_get_totalvfs(pdev))
593 return -ERANGE;
594
Jakub Kicinski17530e72017-05-22 15:50:23 -0700595 device_lock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800596
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700597 if (num_vfs == pdev->sriov->num_VFs)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800598 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500599
600 /* is PF driver loaded w/callback */
601 if (!pdev->driver || !pdev->driver->sriov_configure) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600602 pci_info(pdev, "Driver doesn't support SRIOV configuration via sysfs\n");
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800603 ret = -ENOENT;
604 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500605 }
606
Donald Dutile17893822012-11-05 15:20:36 -0500607 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700608 /* disable VFs */
609 ret = pdev->driver->sriov_configure(pdev, 0);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800610 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500611 }
612
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700613 /* enable VFs */
614 if (pdev->sriov->num_VFs) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600615 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700616 pdev->sriov->num_VFs, num_vfs);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800617 ret = -EBUSY;
618 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700619 }
Donald Dutile17893822012-11-05 15:20:36 -0500620
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700621 ret = pdev->driver->sriov_configure(pdev, num_vfs);
622 if (ret < 0)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800623 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700624
625 if (ret != num_vfs)
Frederick Lawler7506dc72018-01-18 12:55:24 -0600626 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700627 num_vfs, ret);
628
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800629exit:
Jakub Kicinski17530e72017-05-22 15:50:23 -0700630 device_unlock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800631
632 if (ret < 0)
633 return ret;
634
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700635 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500636}
637
Filippo Sironi7dfca152017-10-09 01:09:11 +0200638static ssize_t sriov_offset_show(struct device *dev,
639 struct device_attribute *attr,
640 char *buf)
641{
642 struct pci_dev *pdev = to_pci_dev(dev);
643
644 return sprintf(buf, "%u\n", pdev->sriov->offset);
645}
646
647static ssize_t sriov_stride_show(struct device *dev,
648 struct device_attribute *attr,
649 char *buf)
650{
651 struct pci_dev *pdev = to_pci_dev(dev);
652
653 return sprintf(buf, "%u\n", pdev->sriov->stride);
654}
655
656static ssize_t sriov_vf_device_show(struct device *dev,
657 struct device_attribute *attr,
658 char *buf)
659{
660 struct pci_dev *pdev = to_pci_dev(dev);
661
662 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
663}
664
Bodong Wang0e7df222017-04-13 01:51:40 +0300665static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
666 struct device_attribute *attr,
667 char *buf)
668{
669 struct pci_dev *pdev = to_pci_dev(dev);
670
671 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
672}
673
674static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
675 struct device_attribute *attr,
676 const char *buf, size_t count)
677{
678 struct pci_dev *pdev = to_pci_dev(dev);
679 bool drivers_autoprobe;
680
681 if (kstrtobool(buf, &drivers_autoprobe) < 0)
682 return -EINVAL;
683
684 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
685
686 return count;
687}
688
Donald Dutile17893822012-11-05 15:20:36 -0500689static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
690static struct device_attribute sriov_numvfs_attr =
691 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
692 sriov_numvfs_show, sriov_numvfs_store);
Filippo Sironi7dfca152017-10-09 01:09:11 +0200693static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
694static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
695static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
Bodong Wang0e7df222017-04-13 01:51:40 +0300696static struct device_attribute sriov_drivers_autoprobe_attr =
697 __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
698 sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
Donald Dutile17893822012-11-05 15:20:36 -0500699#endif /* CONFIG_PCI_IOV */
700
Alex Williamson782a9852014-05-20 08:53:21 -0600701static ssize_t driver_override_store(struct device *dev,
702 struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200706 char *driver_override, *old, *cp;
Alex Williamson782a9852014-05-20 08:53:21 -0600707
Sasha Levin4efe8742015-02-04 17:38:15 -0500708 /* We need to keep extra room for a newline */
709 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600710 return -EINVAL;
711
712 driver_override = kstrndup(buf, count, GFP_KERNEL);
713 if (!driver_override)
714 return -ENOMEM;
715
716 cp = strchr(driver_override, '\n');
717 if (cp)
718 *cp = '\0';
719
Nicolai Stange95614752017-09-11 09:45:40 +0200720 device_lock(dev);
721 old = pdev->driver_override;
Alex Williamson782a9852014-05-20 08:53:21 -0600722 if (strlen(driver_override)) {
723 pdev->driver_override = driver_override;
724 } else {
725 kfree(driver_override);
726 pdev->driver_override = NULL;
727 }
Nicolai Stange95614752017-09-11 09:45:40 +0200728 device_unlock(dev);
Alex Williamson782a9852014-05-20 08:53:21 -0600729
730 kfree(old);
731
732 return count;
733}
734
735static ssize_t driver_override_show(struct device *dev,
736 struct device_attribute *attr, char *buf)
737{
738 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200739 ssize_t len;
Alex Williamson782a9852014-05-20 08:53:21 -0600740
Nicolai Stange95614752017-09-11 09:45:40 +0200741 device_lock(dev);
742 len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
743 device_unlock(dev);
744 return len;
Alex Williamson782a9852014-05-20 08:53:21 -0600745}
746static DEVICE_ATTR_RW(driver_override);
747
Sachin Kamatbf22c902013-09-28 15:42:00 +0530748static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700749 &dev_attr_resource.attr,
750 &dev_attr_vendor.attr,
751 &dev_attr_device.attr,
752 &dev_attr_subsystem_vendor.attr,
753 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600754 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700755 &dev_attr_class.attr,
756 &dev_attr_irq.attr,
757 &dev_attr_local_cpus.attr,
758 &dev_attr_local_cpulist.attr,
759 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100760#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700761 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100762#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700763 &dev_attr_dma_mask_bits.attr,
764 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700765 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700766 &dev_attr_broken_parity_status.attr,
767 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100768#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700769 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800770#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200771#ifdef CONFIG_OF
772 &dev_attr_devspec.attr,
773#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600774 &dev_attr_driver_override.attr,
Stuart Hayes0077a842018-01-05 12:51:55 -0600775 &dev_attr_ari_enabled.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700776 NULL,
777};
778
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800779static struct attribute *pci_bridge_attrs[] = {
780 &dev_attr_subordinate_bus_number.attr,
781 &dev_attr_secondary_bus_number.attr,
782 NULL,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700783};
784
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800785static struct attribute *pcie_dev_attrs[] = {
786 &dev_attr_current_link_speed.attr,
787 &dev_attr_current_link_width.attr,
788 &dev_attr_max_link_width.attr,
789 &dev_attr_max_link_speed.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700790 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
792
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700793static struct attribute *pcibus_attrs[] = {
794 &dev_attr_rescan.attr,
795 &dev_attr_cpuaffinity.attr,
796 &dev_attr_cpulistaffinity.attr,
797 NULL,
798};
799
800static const struct attribute_group pcibus_group = {
801 .attrs = pcibus_attrs,
802};
803
804const struct attribute_group *pcibus_groups[] = {
805 &pcibus_group,
806 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700807};
808
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400809static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
810 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000811{
812 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400813 struct pci_dev *vga_dev = vga_default_device();
814
815 if (vga_dev)
816 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000817
818 return sprintf(buf, "%u\n",
819 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
820 IORESOURCE_ROM_SHADOW));
821}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530822static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000823
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400824static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
825 struct bin_attribute *bin_attr, char *buf,
826 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Geliang Tang554a6032015-12-23 20:28:13 +0800828 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 unsigned int size = 64;
830 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400831 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700834 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400836 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (off > size)
840 return 0;
841 if (off + count > size) {
842 size -= off;
843 count = size;
844 } else {
845 size = count;
846 }
847
Huang Ying3d8387e2012-08-15 09:43:03 +0800848 pci_config_pm_runtime_get(dev);
849
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900850 if ((off & 1) && size) {
851 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700852 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900853 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900855 size--;
856 }
857
858 if ((off & 3) && size > 2) {
859 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700860 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900861 data[off - init_off] = val & 0xff;
862 data[off - init_off + 1] = (val >> 8) & 0xff;
863 off += 2;
864 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900868 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700869 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900870 data[off - init_off] = val & 0xff;
871 data[off - init_off + 1] = (val >> 8) & 0xff;
872 data[off - init_off + 2] = (val >> 16) & 0xff;
873 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 off += 4;
875 size -= 4;
876 }
877
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900878 if (size >= 2) {
879 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700880 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900881 data[off - init_off] = val & 0xff;
882 data[off - init_off + 1] = (val >> 8) & 0xff;
883 off += 2;
884 size -= 2;
885 }
886
887 if (size > 0) {
888 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700889 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900890 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 off++;
892 --size;
893 }
894
Huang Ying3d8387e2012-08-15 09:43:03 +0800895 pci_config_pm_runtime_put(dev);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return count;
898}
899
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400900static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
901 struct bin_attribute *bin_attr, char *buf,
902 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Geliang Tang554a6032015-12-23 20:28:13 +0800904 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned int size = count;
906 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400907 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (off > dev->cfg_size)
910 return 0;
911 if (off + count > dev->cfg_size) {
912 size = dev->cfg_size - off;
913 count = size;
914 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700915
Huang Ying3d8387e2012-08-15 09:43:03 +0800916 pci_config_pm_runtime_get(dev);
917
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900918 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700919 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900921 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700923
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900924 if ((off & 3) && size > 2) {
925 u16 val = data[off - init_off];
926 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400927 pci_user_write_config_word(dev, off, val);
928 off += 2;
929 size -= 2;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900933 u32 val = data[off - init_off];
934 val |= (u32) data[off - init_off + 1] << 8;
935 val |= (u32) data[off - init_off + 2] << 16;
936 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700937 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 off += 4;
939 size -= 4;
940 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700941
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900942 if (size >= 2) {
943 u16 val = data[off - init_off];
944 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700945 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900946 off += 2;
947 size -= 2;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900950 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700951 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 off++;
953 --size;
954 }
955
Huang Ying3d8387e2012-08-15 09:43:03 +0800956 pci_config_pm_runtime_put(dev);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return count;
959}
960
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400961static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
962 struct bin_attribute *bin_attr, char *buf,
963 loff_t off, size_t count)
Ben Hutchings94e61082008-03-05 16:52:39 +0000964{
Geliang Tang554a6032015-12-23 20:28:13 +0800965 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000966
Hannes Reineckef52e5622016-02-15 09:42:00 +0100967 if (bin_attr->size > 0) {
968 if (off > bin_attr->size)
969 count = 0;
970 else if (count > bin_attr->size - off)
971 count = bin_attr->size - off;
972 }
Ben Hutchings94e61082008-03-05 16:52:39 +0000973
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800974 return pci_read_vpd(dev, off, count, buf);
975}
Ben Hutchings94e61082008-03-05 16:52:39 +0000976
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400977static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
978 struct bin_attribute *bin_attr, char *buf,
979 loff_t off, size_t count)
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800980{
Geliang Tang554a6032015-12-23 20:28:13 +0800981 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800982
Hannes Reineckef52e5622016-02-15 09:42:00 +0100983 if (bin_attr->size > 0) {
984 if (off > bin_attr->size)
985 count = 0;
986 else if (count > bin_attr->size - off)
987 count = bin_attr->size - off;
988 }
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800989
990 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#ifdef HAVE_PCI_LEGACY
994/**
995 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700996 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700998 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * @buf: buffer to store results
1000 * @off: offset into legacy I/O port space
1001 * @count: number of bytes to read
1002 *
1003 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1004 * callback routine (pci_legacy_read).
1005 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001006static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
1007 struct bin_attribute *bin_attr, char *buf,
1008 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Geliang Tang554a6032015-12-23 20:28:13 +08001010 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001012 /* Only support 1, 2 or 4 byte accesses */
1013 if (count != 1 && count != 2 && count != 4)
1014 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001016 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/**
1020 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001021 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001023 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * @buf: buffer containing value to be written
1025 * @off: offset into legacy I/O port space
1026 * @count: number of bytes to write
1027 *
1028 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1029 * callback routine (pci_legacy_write).
1030 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001031static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1032 struct bin_attribute *bin_attr, char *buf,
1033 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Geliang Tang554a6032015-12-23 20:28:13 +08001035 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001037 /* Only support 1, 2 or 4 byte accesses */
1038 if (count != 1 && count != 2 && count != 4)
1039 return -EINVAL;
1040
1041 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/**
1045 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001046 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * @kobj: kobject corresponding to device to be mapped
1048 * @attr: struct bin_attribute for this file
1049 * @vma: struct vm_area_struct passed to mmap
1050 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001051 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * legacy memory space (first meg of bus space) into application virtual
1053 * memory space.
1054 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001055static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1056 struct bin_attribute *attr,
1057 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Geliang Tang554a6032015-12-23 20:28:13 +08001059 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001061 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001062}
1063
1064/**
1065 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001066 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001067 * @kobj: kobject corresponding to device to be mapped
1068 * @attr: struct bin_attribute for this file
1069 * @vma: struct vm_area_struct passed to mmap
1070 *
1071 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1072 * legacy IO space (first meg of bus space) into application virtual
1073 * memory space. Returns -ENOSYS if the operation isn't supported
1074 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001075static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1076 struct bin_attribute *attr,
1077 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001078{
Geliang Tang554a6032015-12-23 20:28:13 +08001079 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001080
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001081 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001082}
1083
1084/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001085 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1086 * @b: bus to create files under
1087 * @mmap_type: I/O port or memory
1088 *
1089 * Stub implementation. Can be overridden by arch if necessary.
1090 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001091void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1092 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001093{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001094}
1095
1096/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001097 * pci_create_legacy_files - create legacy I/O port and memory files
1098 * @b: bus to create files under
1099 *
1100 * Some platforms allow access to legacy I/O port and ISA memory space on
1101 * a per-bus basis. This routine creates the files and ties them into
1102 * their associated read, write and mmap files from pci-sysfs.c
1103 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001104 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001105 * as it is ok to set up the PCI bus without these files.
1106 */
1107void pci_create_legacy_files(struct pci_bus *b)
1108{
1109 int error;
1110
1111 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
1112 GFP_ATOMIC);
1113 if (!b->legacy_io)
1114 goto kzalloc_err;
1115
Stephen Rothwell62e877b82010-03-01 20:38:36 +11001116 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001117 b->legacy_io->attr.name = "legacy_io";
1118 b->legacy_io->size = 0xffff;
1119 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1120 b->legacy_io->read = pci_read_legacy_io;
1121 b->legacy_io->write = pci_write_legacy_io;
1122 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001123 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001124 error = device_create_bin_file(&b->dev, b->legacy_io);
1125 if (error)
1126 goto legacy_io_err;
1127
1128 /* Allocated above after the legacy_io struct */
1129 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +00001130 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001131 b->legacy_mem->attr.name = "legacy_mem";
1132 b->legacy_mem->size = 1024*1024;
1133 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1134 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001135 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001136 error = device_create_bin_file(&b->dev, b->legacy_mem);
1137 if (error)
1138 goto legacy_mem_err;
1139
1140 return;
1141
1142legacy_mem_err:
1143 device_remove_bin_file(&b->dev, b->legacy_io);
1144legacy_io_err:
1145 kfree(b->legacy_io);
1146 b->legacy_io = NULL;
1147kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001148 printk(KERN_WARNING "pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n");
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001149 return;
1150}
1151
1152void pci_remove_legacy_files(struct pci_bus *b)
1153{
1154 if (b->legacy_io) {
1155 device_remove_bin_file(&b->dev, b->legacy_io);
1156 device_remove_bin_file(&b->dev, b->legacy_mem);
1157 kfree(b->legacy_io); /* both are allocated here */
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160#endif /* HAVE_PCI_LEGACY */
1161
David Woodhousef7195822017-04-12 13:25:59 +01001162#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001163
Martin Wilck3b519e42010-11-10 11:03:21 +01001164int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1165 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001166{
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001167 unsigned long nr, start, size;
1168 resource_size_t pci_start = 0, pci_end;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001169
Martin Wilck3b519e42010-11-10 11:03:21 +01001170 if (pci_resource_len(pdev, resno) == 0)
1171 return 0;
Libin64b00172013-04-15 02:48:54 +00001172 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001173 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -08001174 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001175 if (mmap_api == PCI_MMAP_PROCFS) {
1176 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1177 &pci_start, &pci_end);
1178 pci_start >>= PAGE_SHIFT;
1179 }
Martin Wilck3b519e42010-11-10 11:03:21 +01001180 if (start >= pci_start && start < pci_start + size &&
1181 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001182 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001183 return 0;
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/**
1187 * pci_mmap_resource - map a PCI resource into user memory space
1188 * @kobj: kobject for mapping
1189 * @attr: struct bin_attribute for the file being mapped
1190 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001191 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
1193 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001195static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1196 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Geliang Tang554a6032015-12-23 20:28:13 +08001198 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001199 int bar = (unsigned long)attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 enum pci_mmap_state mmap_type;
David Woodhousedca40b12017-04-12 13:25:57 +01001201 struct resource *res = &pdev->resource[bar];
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001202
Bjorn Helgaasca620722016-04-07 17:15:14 -07001203 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1204 return -EINVAL;
1205
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001206 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001207 return -EINVAL;
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
David Woodhousef7195822017-04-12 13:25:59 +01001210
1211 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001212}
1213
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001214static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1215 struct bin_attribute *attr,
1216 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001217{
1218 return pci_mmap_resource(kobj, attr, vma, 0);
1219}
1220
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001221static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1222 struct bin_attribute *attr,
1223 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001224{
1225 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001228static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1229 struct bin_attribute *attr, char *buf,
1230 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001231{
Geliang Tang554a6032015-12-23 20:28:13 +08001232 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001233 int bar = (unsigned long)attr->private;
Alex Williamson86333282010-07-19 09:45:34 -06001234 unsigned long port = off;
Alex Williamson86333282010-07-19 09:45:34 -06001235
David Woodhousedca40b12017-04-12 13:25:57 +01001236 port += pci_resource_start(pdev, bar);
Alex Williamson86333282010-07-19 09:45:34 -06001237
David Woodhousedca40b12017-04-12 13:25:57 +01001238 if (port > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001239 return 0;
1240
David Woodhousedca40b12017-04-12 13:25:57 +01001241 if (port + count - 1 > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001242 return -EINVAL;
1243
1244 switch (count) {
1245 case 1:
1246 if (write)
1247 outb(*(u8 *)buf, port);
1248 else
1249 *(u8 *)buf = inb(port);
1250 return 1;
1251 case 2:
1252 if (write)
1253 outw(*(u16 *)buf, port);
1254 else
1255 *(u16 *)buf = inw(port);
1256 return 2;
1257 case 4:
1258 if (write)
1259 outl(*(u32 *)buf, port);
1260 else
1261 *(u32 *)buf = inl(port);
1262 return 4;
1263 }
1264 return -EINVAL;
1265}
1266
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001267static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1268 struct bin_attribute *attr, char *buf,
1269 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001270{
1271 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1272}
1273
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001274static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1275 struct bin_attribute *attr, char *buf,
1276 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001277{
1278 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001282 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001283 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001284 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001285 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001286 * free their resources.
1287 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001288static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001289{
1290 int i;
1291
1292 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1293 struct bin_attribute *res_attr;
1294
1295 res_attr = pdev->res_attr[i];
1296 if (res_attr) {
1297 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1298 kfree(res_attr);
1299 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001300
1301 res_attr = pdev->res_attr_wc[i];
1302 if (res_attr) {
1303 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1304 kfree(res_attr);
1305 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001306 }
1307}
1308
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001309static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1310{
1311 /* allocate attribute structure, piggyback attribute name */
1312 int name_len = write_combine ? 13 : 10;
1313 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001314 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001315 int retval;
1316
1317 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001318 if (!res_attr)
1319 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001320
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001321 res_attr_name = (char *)(res_attr + 1);
1322
1323 sysfs_bin_attr_init(res_attr);
1324 if (write_combine) {
1325 pdev->res_attr_wc[num] = res_attr;
1326 sprintf(res_attr_name, "resource%d_wc", num);
1327 res_attr->mmap = pci_mmap_resource_wc;
1328 } else {
1329 pdev->res_attr[num] = res_attr;
1330 sprintf(res_attr_name, "resource%d", num);
David Woodhousee854d8b2017-04-12 13:25:56 +01001331 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1332 res_attr->read = pci_read_resource_io;
1333 res_attr->write = pci_write_resource_io;
1334 if (arch_can_pci_mmap_io())
1335 res_attr->mmap = pci_mmap_resource_uc;
1336 } else {
1337 res_attr->mmap = pci_mmap_resource_uc;
1338 }
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001339 }
1340 res_attr->attr.name = res_attr_name;
1341 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1342 res_attr->size = pci_resource_len(pdev, num);
David Woodhousedca40b12017-04-12 13:25:57 +01001343 res_attr->private = (void *)(unsigned long)num;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001344 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1345 if (retval)
1346 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001347
1348 return retval;
1349}
1350
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001351/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001353 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001355 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001357static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001360 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* Expose the PCI resources from this device as files */
1363 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /* skip empty resources */
1366 if (!pci_resource_len(pdev, i))
1367 continue;
1368
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001369 retval = pci_create_attr(pdev, i, 0);
1370 /* for prefetchable resources, create a WC mappable file */
David Woodhouseae749c72017-04-12 13:25:54 +01001371 if (!retval && arch_can_pci_mmap_wc() &&
1372 pdev->resource[i].flags & IORESOURCE_PREFETCH)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001373 retval = pci_create_attr(pdev, i, 1);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001374 if (retval) {
1375 pci_remove_resource_files(pdev);
1376 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001382int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1383void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif /* HAVE_PCI_MMAP */
1385
1386/**
1387 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001388 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001390 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * @buf: user input
1392 * @off: file offset
1393 * @count: number of byte in input
1394 *
1395 * writing anything except 0 enables it
1396 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001397static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1398 struct bin_attribute *bin_attr, char *buf,
1399 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Geliang Tang554a6032015-12-23 20:28:13 +08001401 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if ((off == 0) && (*buf == '0') && (count == 2))
1404 pdev->rom_attr_enabled = 0;
1405 else
1406 pdev->rom_attr_enabled = 1;
1407
1408 return count;
1409}
1410
1411/**
1412 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001413 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001415 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * @buf: where to put the data we read from the ROM
1417 * @off: file offset
1418 * @count: number of bytes to read
1419 *
1420 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1421 * device corresponding to @kobj.
1422 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001423static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1424 struct bin_attribute *bin_attr, char *buf,
1425 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Geliang Tang554a6032015-12-23 20:28:13 +08001427 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 void __iomem *rom;
1429 size_t size;
1430
1431 if (!pdev->rom_attr_enabled)
1432 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001435 if (!rom || !size)
1436 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (off >= size)
1439 count = 0;
1440 else {
1441 if (off + count > size)
1442 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 memcpy_fromio(buf, rom + off, count);
1445 }
1446 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return count;
1449}
1450
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301451static const struct bin_attribute pci_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 .attr = {
1453 .name = "config",
1454 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001456 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 .read = pci_read_config,
1458 .write = pci_write_config,
1459};
1460
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301461static const struct bin_attribute pcie_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .attr = {
1463 .name = "config",
1464 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001466 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 .read = pci_read_config,
1468 .write = pci_write_config,
1469};
1470
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001471static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1472 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001473{
1474 struct pci_dev *pdev = to_pci_dev(dev);
1475 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001476 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001477
1478 if (result < 0)
1479 return result;
1480
1481 if (val != 1)
1482 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001483
1484 result = pci_reset_function(pdev);
1485 if (result < 0)
1486 return result;
1487
1488 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001489}
1490
1491static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1492
Zhao, Yu280c73d2008-10-13 20:01:00 +08001493static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1494{
1495 int retval;
1496 struct bin_attribute *attr;
1497
1498 /* If the device has VPD, try to expose it in sysfs. */
1499 if (dev->vpd) {
1500 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1501 if (!attr)
1502 return -ENOMEM;
1503
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001504 sysfs_bin_attr_init(attr);
Hannes Reinecke104daa72016-02-15 09:42:01 +01001505 attr->size = 0;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001506 attr->attr.name = "vpd";
1507 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001508 attr->read = read_vpd_attr;
1509 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001510 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1511 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001512 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001513 return retval;
1514 }
1515 dev->vpd->attr = attr;
1516 }
1517
1518 /* Active State Power Management */
1519 pcie_aspm_create_sysfs_dev_files(dev);
1520
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001521 if (!pci_probe_reset_function(dev)) {
1522 retval = device_create_file(&dev->dev, &reset_attr);
1523 if (retval)
1524 goto error;
1525 dev->reset_fn = 1;
1526 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001527 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001528
1529error:
1530 pcie_aspm_remove_sysfs_dev_files(dev);
1531 if (dev->vpd && dev->vpd->attr) {
1532 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1533 kfree(dev->vpd->attr);
1534 }
1535
1536 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001537}
1538
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001539int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001541 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001542 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001543 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (!sysfs_initialized)
1546 return -EACCES;
1547
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001548 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001549 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001550 else
1551 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001552 if (retval)
1553 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001555 retval = pci_create_resource_files(pdev);
1556 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001557 goto err_config_file;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001560 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001561 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001562 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001563 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001564 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001565 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001567 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001568 attr->size = rom_size;
1569 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001570 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001571 attr->read = pci_read_rom;
1572 attr->write = pci_write_rom;
1573 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1574 if (retval) {
1575 kfree(attr);
1576 goto err_resource_files;
1577 }
1578 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001580
Zhao, Yu280c73d2008-10-13 20:01:00 +08001581 /* add sysfs entries for various capabilities */
1582 retval = pci_create_capabilities_sysfs(pdev);
1583 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001584 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001585
Narendra K911e1c92010-07-26 05:56:50 -05001586 pci_create_firmware_label_files(pdev);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001589
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001590err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001591 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001592 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001593 kfree(pdev->rom_attr);
1594 pdev->rom_attr = NULL;
1595 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001596err_resource_files:
1597 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001598err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001599 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001600 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001601 else
1602 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001603err:
1604 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Zhao, Yu280c73d2008-10-13 20:01:00 +08001607static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1608{
1609 if (dev->vpd && dev->vpd->attr) {
1610 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1611 kfree(dev->vpd->attr);
1612 }
1613
1614 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001615 if (dev->reset_fn) {
1616 device_remove_file(&dev->dev, &reset_attr);
1617 dev->reset_fn = 0;
1618 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/**
1622 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1623 * @pdev: device whose entries we should free
1624 *
1625 * Cleanup when @pdev is removed from sysfs.
1626 */
1627void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1628{
David Millerd67afe52006-11-10 12:27:48 -08001629 if (!sysfs_initialized)
1630 return;
1631
Zhao, Yu280c73d2008-10-13 20:01:00 +08001632 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001633
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001634 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001636 else
1637 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 pci_remove_resource_files(pdev);
1640
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001641 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001642 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1643 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001644 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Narendra K911e1c92010-07-26 05:56:50 -05001646
1647 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650static int __init pci_sysfs_init(void)
1651{
1652 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001653 int retval;
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001656 for_each_pci_dev(pdev) {
1657 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001658 if (retval) {
1659 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001660 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001661 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 return 0;
1665}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001666late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001667
1668static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001669 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001670 NULL,
1671};
1672
1673static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001674 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001675{
Geliang Tang554a6032015-12-23 20:28:13 +08001676 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001677 struct pci_dev *pdev = to_pci_dev(dev);
1678
1679 if (a == &vga_attr.attr)
1680 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1681 return 0;
1682
Yinghai Lu4e15c462012-11-05 15:20:34 -05001683 return a->mode;
1684}
1685
Jiang Liudfab88b2013-05-31 12:21:31 +08001686static struct attribute *pci_dev_hp_attrs[] = {
1687 &dev_remove_attr.attr,
1688 &dev_rescan_attr.attr,
1689 NULL,
1690};
1691
1692static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001693 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001694{
Geliang Tang554a6032015-12-23 20:28:13 +08001695 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001696 struct pci_dev *pdev = to_pci_dev(dev);
1697
1698 if (pdev->is_virtfn)
1699 return 0;
1700
1701 return a->mode;
1702}
1703
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001704static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1705 struct attribute *a, int n)
1706{
1707 struct device *dev = kobj_to_dev(kobj);
1708 struct pci_dev *pdev = to_pci_dev(dev);
1709
1710 if (pci_is_bridge(pdev))
1711 return a->mode;
1712
1713 return 0;
1714}
1715
1716static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1717 struct attribute *a, int n)
1718{
1719 struct device *dev = kobj_to_dev(kobj);
1720 struct pci_dev *pdev = to_pci_dev(dev);
1721
1722 if (pci_is_pcie(pdev))
1723 return a->mode;
1724
1725 return 0;
1726}
1727
1728static const struct attribute_group pci_dev_group = {
1729 .attrs = pci_dev_attrs,
1730};
1731
1732const struct attribute_group *pci_dev_groups[] = {
1733 &pci_dev_group,
1734 NULL,
1735};
1736
1737static const struct attribute_group pci_bridge_group = {
1738 .attrs = pci_bridge_attrs,
1739};
1740
1741const struct attribute_group *pci_bridge_groups[] = {
1742 &pci_bridge_group,
1743 NULL,
1744};
1745
1746static const struct attribute_group pcie_dev_group = {
1747 .attrs = pcie_dev_attrs,
1748};
1749
1750const struct attribute_group *pcie_dev_groups[] = {
1751 &pcie_dev_group,
1752 NULL,
1753};
1754
Arvind Yadave7ea9822017-07-11 14:57:07 +05301755static const struct attribute_group pci_dev_hp_attr_group = {
Jiang Liudfab88b2013-05-31 12:21:31 +08001756 .attrs = pci_dev_hp_attrs,
1757 .is_visible = pci_dev_hp_attrs_are_visible,
1758};
1759
Donald Dutile17893822012-11-05 15:20:36 -05001760#ifdef CONFIG_PCI_IOV
1761static struct attribute *sriov_dev_attrs[] = {
1762 &sriov_totalvfs_attr.attr,
1763 &sriov_numvfs_attr.attr,
Filippo Sironi7dfca152017-10-09 01:09:11 +02001764 &sriov_offset_attr.attr,
1765 &sriov_stride_attr.attr,
1766 &sriov_vf_device_attr.attr,
Bodong Wang0e7df222017-04-13 01:51:40 +03001767 &sriov_drivers_autoprobe_attr.attr,
Donald Dutile17893822012-11-05 15:20:36 -05001768 NULL,
1769};
1770
1771static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001772 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001773{
Geliang Tang554a6032015-12-23 20:28:13 +08001774 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001775
1776 if (!dev_is_pf(dev))
1777 return 0;
1778
1779 return a->mode;
1780}
1781
Arvind Yadave7ea9822017-07-11 14:57:07 +05301782static const struct attribute_group sriov_dev_attr_group = {
Donald Dutile17893822012-11-05 15:20:36 -05001783 .attrs = sriov_dev_attrs,
1784 .is_visible = sriov_attrs_are_visible,
1785};
1786#endif /* CONFIG_PCI_IOV */
1787
Arvind Yadave7ea9822017-07-11 14:57:07 +05301788static const struct attribute_group pci_dev_attr_group = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001789 .attrs = pci_dev_dev_attrs,
1790 .is_visible = pci_dev_attrs_are_visible,
1791};
1792
Arvind Yadave7ea9822017-07-11 14:57:07 +05301793static const struct attribute_group pci_bridge_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001794 .attrs = pci_bridge_attrs,
1795 .is_visible = pci_bridge_attrs_are_visible,
1796};
1797
Arvind Yadave7ea9822017-07-11 14:57:07 +05301798static const struct attribute_group pcie_dev_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001799 .attrs = pcie_dev_attrs,
1800 .is_visible = pcie_dev_attrs_are_visible,
1801};
1802
Yinghai Lu4e15c462012-11-05 15:20:34 -05001803static const struct attribute_group *pci_dev_attr_groups[] = {
1804 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001805 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001806#ifdef CONFIG_PCI_IOV
1807 &sriov_dev_attr_group,
1808#endif
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001809 &pci_bridge_attr_group,
1810 &pcie_dev_attr_group,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001811 NULL,
1812};
1813
Bhumika Goyal69f2dc22017-09-12 16:43:33 +05301814const struct device_type pci_dev_type = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001815 .groups = pci_dev_attr_groups,
1816};