blob: 7dc5be545d18091c122b17977578173099816a36 [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{
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) {
Jay Fang1acfb9b2018-03-12 17:13:32 +0800171 case PCI_EXP_LNKCAP_SLS_16_0GB:
172 speed = "16 GT/s";
173 break;
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800174 case PCI_EXP_LNKCAP_SLS_8_0GB:
175 speed = "8 GT/s";
176 break;
177 case PCI_EXP_LNKCAP_SLS_5_0GB:
178 speed = "5 GT/s";
179 break;
180 case PCI_EXP_LNKCAP_SLS_2_5GB:
181 speed = "2.5 GT/s";
182 break;
183 default:
184 speed = "Unknown speed";
185 }
186
187 return sprintf(buf, "%s\n", speed);
188}
189static DEVICE_ATTR_RO(max_link_speed);
190
191static ssize_t max_link_width_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
194 struct pci_dev *pci_dev = to_pci_dev(dev);
195 u32 linkcap;
196 int err;
197
198 err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap);
199 if (err)
200 return -EINVAL;
201
202 return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4);
203}
204static DEVICE_ATTR_RO(max_link_width);
205
206static ssize_t current_link_speed_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
209 struct pci_dev *pci_dev = to_pci_dev(dev);
210 u16 linkstat;
211 int err;
212 const char *speed;
213
214 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
215 if (err)
216 return -EINVAL;
217
218 switch (linkstat & PCI_EXP_LNKSTA_CLS) {
Jay Fang1acfb9b2018-03-12 17:13:32 +0800219 case PCI_EXP_LNKSTA_CLS_16_0GB:
220 speed = "16 GT/s";
221 break;
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800222 case PCI_EXP_LNKSTA_CLS_8_0GB:
223 speed = "8 GT/s";
224 break;
225 case PCI_EXP_LNKSTA_CLS_5_0GB:
226 speed = "5 GT/s";
227 break;
228 case PCI_EXP_LNKSTA_CLS_2_5GB:
229 speed = "2.5 GT/s";
230 break;
231 default:
232 speed = "Unknown speed";
233 }
234
235 return sprintf(buf, "%s\n", speed);
236}
237static DEVICE_ATTR_RO(current_link_speed);
238
239static ssize_t current_link_width_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
242 struct pci_dev *pci_dev = to_pci_dev(dev);
243 u16 linkstat;
244 int err;
245
246 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
247 if (err)
248 return -EINVAL;
249
250 return sprintf(buf, "%u\n",
251 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
252}
253static DEVICE_ATTR_RO(current_link_width);
254
255static ssize_t secondary_bus_number_show(struct device *dev,
256 struct device_attribute *attr,
257 char *buf)
258{
259 struct pci_dev *pci_dev = to_pci_dev(dev);
260 u8 sec_bus;
261 int err;
262
263 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
264 if (err)
265 return -EINVAL;
266
267 return sprintf(buf, "%u\n", sec_bus);
268}
269static DEVICE_ATTR_RO(secondary_bus_number);
270
271static ssize_t subordinate_bus_number_show(struct device *dev,
272 struct device_attribute *attr,
273 char *buf)
274{
275 struct pci_dev *pci_dev = to_pci_dev(dev);
276 u8 sub_bus;
277 int err;
278
279 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
280 if (err)
281 return -EINVAL;
282
283 return sprintf(buf, "%u\n", sub_bus);
284}
285static DEVICE_ATTR_RO(subordinate_bus_number);
286
Stuart Hayes0077a842018-01-05 12:51:55 -0600287static ssize_t ari_enabled_show(struct device *dev,
288 struct device_attribute *attr,
289 char *buf)
290{
291 struct pci_dev *pci_dev = to_pci_dev(dev);
292
293 return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
294}
295static DEVICE_ATTR_RO(ari_enabled);
296
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400297static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
298 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700299{
300 struct pci_dev *pci_dev = to_pci_dev(dev);
301
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200302 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700303 pci_dev->vendor, pci_dev->device,
304 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
305 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
306 (u8)(pci_dev->class));
307}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700308static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800309
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700310static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400311 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200312{
313 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800314 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900315 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800316
317 if (result < 0)
318 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200319
320 /* this can crash the machine when done on the "wrong" device */
321 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800322 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200323
Trent Piepho92425a42008-11-30 17:10:12 -0800324 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900325 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800326 pci_disable_device(pdev);
327 else
328 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800329 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800330 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200331
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800332 return result < 0 ? result : count;
333}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200334
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700335static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400336 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800337{
338 struct pci_dev *pdev;
339
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400340 pdev = to_pci_dev(dev);
341 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200342}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700343static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200344
Brice Goglin81bb0e12007-01-28 10:53:40 +0100345#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400346static ssize_t numa_node_store(struct device *dev,
347 struct device_attribute *attr, const char *buf,
348 size_t count)
349{
350 struct pci_dev *pdev = to_pci_dev(dev);
351 int node, ret;
352
353 if (!capable(CAP_SYS_ADMIN))
354 return -EPERM;
355
356 ret = kstrtoint(buf, 0, &node);
357 if (ret)
358 return ret;
359
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100360 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
361 return -EINVAL;
362
363 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400364 return -EINVAL;
365
366 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600367 pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
Prarit Bhargava63692df2014-10-23 14:22:12 -0400368 node);
369
370 dev->numa_node = node;
371 return count;
372}
373
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400374static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
375 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100376{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400377 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100378}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400379static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100380#endif
381
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400382static ssize_t dma_mask_bits_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800384{
385 struct pci_dev *pdev = to_pci_dev(dev);
386
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400387 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800388}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700389static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800390
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400391static ssize_t consistent_dma_mask_bits_show(struct device *dev,
392 struct device_attribute *attr,
393 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800394{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400395 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800396}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700397static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800398
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400399static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
400 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400401{
402 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800403 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400404
Yijing Wang468ff152014-09-23 13:27:24 +0800405 return sprintf(buf, "%u\n", subordinate ?
406 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
407 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400408}
409
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400410static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
Brice Goglinfe970642006-08-31 01:55:15 -0400412{
413 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800414 struct pci_bus *subordinate = pdev->subordinate;
Trent Piepho92425a42008-11-30 17:10:12 -0800415 unsigned long val;
416
Jingoo Han9a994e82013-06-01 16:25:25 +0900417 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800418 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400419
Brice Goglinfe970642006-08-31 01:55:15 -0400420 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800421 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400422
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700423 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800424 * "no_msi" and "bus_flags" only affect what happens when a driver
425 * requests MSI or MSI-X. They don't affect any drivers that have
426 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700427 */
Yijing Wang468ff152014-09-23 13:27:24 +0800428 if (!subordinate) {
429 pdev->no_msi = !val;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600430 pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
Yijing Wang468ff152014-09-23 13:27:24 +0800431 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400432 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400433 }
434
Yijing Wang468ff152014-09-23 13:27:24 +0800435 if (val)
436 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
437 else
438 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
439
440 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
441 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400442 return count;
443}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700444static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700445
Alex Chiang705b1aa2009-03-20 14:56:31 -0600446static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
447 size_t count)
448{
449 unsigned long val;
450 struct pci_bus *b = NULL;
451
Jingoo Han9a994e82013-06-01 16:25:25 +0900452 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600453 return -EINVAL;
454
455 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100456 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600457 while ((b = pci_find_next_bus(b)) != NULL)
458 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100459 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600460 }
461 return count;
462}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600463static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600464
Sachin Kamatbf22c902013-09-28 15:42:00 +0530465static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600466 &bus_attr_rescan.attr,
467 NULL,
468};
469
470static const struct attribute_group pci_bus_group = {
471 .attrs = pci_bus_attrs,
472};
473
474const struct attribute_group *pci_bus_groups[] = {
475 &pci_bus_group,
476 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600477};
Alex Chiang77c27c72009-03-20 14:56:36 -0600478
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400479static ssize_t dev_rescan_store(struct device *dev,
480 struct device_attribute *attr, const char *buf,
481 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600482{
483 unsigned long val;
484 struct pci_dev *pdev = to_pci_dev(dev);
485
Jingoo Han9a994e82013-06-01 16:25:25 +0900486 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600487 return -EINVAL;
488
489 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100490 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600491 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100492 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600493 }
494 return count;
495}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530496static struct device_attribute dev_rescan_attr = __ATTR(rescan,
497 (S_IWUSR|S_IWGRP),
498 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600499
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400500static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600502{
Alex Chiang77c27c72009-03-20 14:56:36 -0600503 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600504
Jingoo Han9a994e82013-06-01 16:25:25 +0900505 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600506 return -EINVAL;
507
Tejun Heobc6caf02014-02-03 14:03:02 -0500508 if (val && device_remove_file_self(dev, attr))
509 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600510 return count;
511}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530512static struct device_attribute dev_remove_attr = __ATTR(remove,
513 (S_IWUSR|S_IWGRP),
514 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700515
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400516static ssize_t dev_bus_rescan_store(struct device *dev,
517 struct device_attribute *attr,
518 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700519{
520 unsigned long val;
521 struct pci_bus *bus = to_pci_bus(dev);
522
Jingoo Han9a994e82013-06-01 16:25:25 +0900523 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700524 return -EINVAL;
525
526 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100527 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800528 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
529 pci_rescan_bus_bridge_resize(bus->self);
530 else
531 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100532 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700533 }
534 return count;
535}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700536static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700537
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100538#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800539static ssize_t d3cold_allowed_store(struct device *dev,
540 struct device_attribute *attr,
541 const char *buf, size_t count)
542{
543 struct pci_dev *pdev = to_pci_dev(dev);
544 unsigned long val;
545
Jingoo Han9a994e82013-06-01 16:25:25 +0900546 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800547 return -EINVAL;
548
549 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300550 if (pdev->d3cold_allowed)
551 pci_d3cold_enable(pdev);
552 else
553 pci_d3cold_disable(pdev);
554
Huang Ying448bd852012-06-23 10:23:51 +0800555 pm_runtime_resume(dev);
556
557 return count;
558}
559
560static ssize_t d3cold_allowed_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400564 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800565}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700566static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800567#endif
568
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200569#ifdef CONFIG_OF
570static ssize_t devspec_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 struct pci_dev *pdev = to_pci_dev(dev);
574 struct device_node *np = pci_device_to_OF_node(pdev);
575
Rob Herringb63773a2017-07-18 16:43:21 -0500576 if (np == NULL)
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200577 return 0;
Rob Herringb63773a2017-07-18 16:43:21 -0500578 return sprintf(buf, "%pOF", np);
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200579}
580static DEVICE_ATTR_RO(devspec);
581#endif
582
Donald Dutile17893822012-11-05 15:20:36 -0500583#ifdef CONFIG_PCI_IOV
584static ssize_t sriov_totalvfs_show(struct device *dev,
585 struct device_attribute *attr,
586 char *buf)
587{
588 struct pci_dev *pdev = to_pci_dev(dev);
589
Donald Dutilebff73152012-11-05 15:20:37 -0500590 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500591}
592
593
594static ssize_t sriov_numvfs_show(struct device *dev,
595 struct device_attribute *attr,
596 char *buf)
597{
598 struct pci_dev *pdev = to_pci_dev(dev);
599
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700600 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500601}
602
603/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700604 * num_vfs > 0; number of VFs to enable
605 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500606 *
607 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700608 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500609 */
610static ssize_t sriov_numvfs_store(struct device *dev,
611 struct device_attribute *attr,
612 const char *buf, size_t count)
613{
614 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700615 int ret;
616 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500617
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700618 ret = kstrtou16(buf, 0, &num_vfs);
619 if (ret < 0)
620 return ret;
621
622 if (num_vfs > pci_sriov_get_totalvfs(pdev))
623 return -ERANGE;
624
Jakub Kicinski17530e72017-05-22 15:50:23 -0700625 device_lock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800626
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700627 if (num_vfs == pdev->sriov->num_VFs)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800628 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500629
630 /* is PF driver loaded w/callback */
631 if (!pdev->driver || !pdev->driver->sriov_configure) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600632 pci_info(pdev, "Driver doesn't support SRIOV configuration via sysfs\n");
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800633 ret = -ENOENT;
634 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500635 }
636
Donald Dutile17893822012-11-05 15:20:36 -0500637 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700638 /* disable VFs */
639 ret = pdev->driver->sriov_configure(pdev, 0);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800640 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500641 }
642
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700643 /* enable VFs */
644 if (pdev->sriov->num_VFs) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600645 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700646 pdev->sriov->num_VFs, num_vfs);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800647 ret = -EBUSY;
648 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700649 }
Donald Dutile17893822012-11-05 15:20:36 -0500650
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700651 ret = pdev->driver->sriov_configure(pdev, num_vfs);
652 if (ret < 0)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800653 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700654
655 if (ret != num_vfs)
Frederick Lawler7506dc72018-01-18 12:55:24 -0600656 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700657 num_vfs, ret);
658
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800659exit:
Jakub Kicinski17530e72017-05-22 15:50:23 -0700660 device_unlock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800661
662 if (ret < 0)
663 return ret;
664
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700665 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500666}
667
Filippo Sironi7dfca152017-10-09 01:09:11 +0200668static ssize_t sriov_offset_show(struct device *dev,
669 struct device_attribute *attr,
670 char *buf)
671{
672 struct pci_dev *pdev = to_pci_dev(dev);
673
674 return sprintf(buf, "%u\n", pdev->sriov->offset);
675}
676
677static ssize_t sriov_stride_show(struct device *dev,
678 struct device_attribute *attr,
679 char *buf)
680{
681 struct pci_dev *pdev = to_pci_dev(dev);
682
683 return sprintf(buf, "%u\n", pdev->sriov->stride);
684}
685
686static ssize_t sriov_vf_device_show(struct device *dev,
687 struct device_attribute *attr,
688 char *buf)
689{
690 struct pci_dev *pdev = to_pci_dev(dev);
691
692 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
693}
694
Bodong Wang0e7df222017-04-13 01:51:40 +0300695static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
696 struct device_attribute *attr,
697 char *buf)
698{
699 struct pci_dev *pdev = to_pci_dev(dev);
700
701 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
702}
703
704static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
705 struct device_attribute *attr,
706 const char *buf, size_t count)
707{
708 struct pci_dev *pdev = to_pci_dev(dev);
709 bool drivers_autoprobe;
710
711 if (kstrtobool(buf, &drivers_autoprobe) < 0)
712 return -EINVAL;
713
714 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
715
716 return count;
717}
718
Donald Dutile17893822012-11-05 15:20:36 -0500719static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
720static struct device_attribute sriov_numvfs_attr =
721 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
722 sriov_numvfs_show, sriov_numvfs_store);
Filippo Sironi7dfca152017-10-09 01:09:11 +0200723static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
724static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
725static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
Bodong Wang0e7df222017-04-13 01:51:40 +0300726static struct device_attribute sriov_drivers_autoprobe_attr =
727 __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
728 sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
Donald Dutile17893822012-11-05 15:20:36 -0500729#endif /* CONFIG_PCI_IOV */
730
Alex Williamson782a9852014-05-20 08:53:21 -0600731static ssize_t driver_override_store(struct device *dev,
732 struct device_attribute *attr,
733 const char *buf, size_t count)
734{
735 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200736 char *driver_override, *old, *cp;
Alex Williamson782a9852014-05-20 08:53:21 -0600737
Sasha Levin4efe8742015-02-04 17:38:15 -0500738 /* We need to keep extra room for a newline */
739 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600740 return -EINVAL;
741
742 driver_override = kstrndup(buf, count, GFP_KERNEL);
743 if (!driver_override)
744 return -ENOMEM;
745
746 cp = strchr(driver_override, '\n');
747 if (cp)
748 *cp = '\0';
749
Nicolai Stange95614752017-09-11 09:45:40 +0200750 device_lock(dev);
751 old = pdev->driver_override;
Alex Williamson782a9852014-05-20 08:53:21 -0600752 if (strlen(driver_override)) {
753 pdev->driver_override = driver_override;
754 } else {
755 kfree(driver_override);
756 pdev->driver_override = NULL;
757 }
Nicolai Stange95614752017-09-11 09:45:40 +0200758 device_unlock(dev);
Alex Williamson782a9852014-05-20 08:53:21 -0600759
760 kfree(old);
761
762 return count;
763}
764
765static ssize_t driver_override_show(struct device *dev,
766 struct device_attribute *attr, char *buf)
767{
768 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200769 ssize_t len;
Alex Williamson782a9852014-05-20 08:53:21 -0600770
Nicolai Stange95614752017-09-11 09:45:40 +0200771 device_lock(dev);
772 len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
773 device_unlock(dev);
774 return len;
Alex Williamson782a9852014-05-20 08:53:21 -0600775}
776static DEVICE_ATTR_RW(driver_override);
777
Sachin Kamatbf22c902013-09-28 15:42:00 +0530778static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700779 &dev_attr_resource.attr,
780 &dev_attr_vendor.attr,
781 &dev_attr_device.attr,
782 &dev_attr_subsystem_vendor.attr,
783 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600784 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700785 &dev_attr_class.attr,
786 &dev_attr_irq.attr,
787 &dev_attr_local_cpus.attr,
788 &dev_attr_local_cpulist.attr,
789 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100790#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700791 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100792#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700793 &dev_attr_dma_mask_bits.attr,
794 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700795 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700796 &dev_attr_broken_parity_status.attr,
797 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100798#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700799 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800800#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200801#ifdef CONFIG_OF
802 &dev_attr_devspec.attr,
803#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600804 &dev_attr_driver_override.attr,
Stuart Hayes0077a842018-01-05 12:51:55 -0600805 &dev_attr_ari_enabled.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700806 NULL,
807};
808
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800809static struct attribute *pci_bridge_attrs[] = {
810 &dev_attr_subordinate_bus_number.attr,
811 &dev_attr_secondary_bus_number.attr,
812 NULL,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700813};
814
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800815static struct attribute *pcie_dev_attrs[] = {
816 &dev_attr_current_link_speed.attr,
817 &dev_attr_current_link_width.attr,
818 &dev_attr_max_link_width.attr,
819 &dev_attr_max_link_speed.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700820 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821};
822
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700823static struct attribute *pcibus_attrs[] = {
824 &dev_attr_rescan.attr,
825 &dev_attr_cpuaffinity.attr,
826 &dev_attr_cpulistaffinity.attr,
827 NULL,
828};
829
830static const struct attribute_group pcibus_group = {
831 .attrs = pcibus_attrs,
832};
833
834const struct attribute_group *pcibus_groups[] = {
835 &pcibus_group,
836 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700837};
838
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400839static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
840 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000841{
842 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400843 struct pci_dev *vga_dev = vga_default_device();
844
845 if (vga_dev)
846 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000847
848 return sprintf(buf, "%u\n",
849 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
850 IORESOURCE_ROM_SHADOW));
851}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530852static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000853
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400854static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
855 struct bin_attribute *bin_attr, char *buf,
856 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Geliang Tang554a6032015-12-23 20:28:13 +0800858 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 unsigned int size = 64;
860 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400861 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700864 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400866 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (off > size)
870 return 0;
871 if (off + count > size) {
872 size -= off;
873 count = size;
874 } else {
875 size = count;
876 }
877
Huang Ying3d8387e2012-08-15 09:43:03 +0800878 pci_config_pm_runtime_get(dev);
879
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900880 if ((off & 1) && size) {
881 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700882 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900883 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900885 size--;
886 }
887
888 if ((off & 3) && size > 2) {
889 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700890 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900891 data[off - init_off] = val & 0xff;
892 data[off - init_off + 1] = (val >> 8) & 0xff;
893 off += 2;
894 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
897 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900898 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700899 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900900 data[off - init_off] = val & 0xff;
901 data[off - init_off + 1] = (val >> 8) & 0xff;
902 data[off - init_off + 2] = (val >> 16) & 0xff;
903 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 off += 4;
905 size -= 4;
906 }
907
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900908 if (size >= 2) {
909 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700910 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900911 data[off - init_off] = val & 0xff;
912 data[off - init_off + 1] = (val >> 8) & 0xff;
913 off += 2;
914 size -= 2;
915 }
916
917 if (size > 0) {
918 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700919 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900920 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 off++;
922 --size;
923 }
924
Huang Ying3d8387e2012-08-15 09:43:03 +0800925 pci_config_pm_runtime_put(dev);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return count;
928}
929
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400930static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
931 struct bin_attribute *bin_attr, char *buf,
932 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Geliang Tang554a6032015-12-23 20:28:13 +0800934 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 unsigned int size = count;
936 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400937 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (off > dev->cfg_size)
940 return 0;
941 if (off + count > dev->cfg_size) {
942 size = dev->cfg_size - off;
943 count = size;
944 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700945
Huang Ying3d8387e2012-08-15 09:43:03 +0800946 pci_config_pm_runtime_get(dev);
947
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900948 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700949 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900951 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700953
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900954 if ((off & 3) && size > 2) {
955 u16 val = data[off - init_off];
956 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400957 pci_user_write_config_word(dev, off, val);
958 off += 2;
959 size -= 2;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900963 u32 val = data[off - init_off];
964 val |= (u32) data[off - init_off + 1] << 8;
965 val |= (u32) data[off - init_off + 2] << 16;
966 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700967 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 off += 4;
969 size -= 4;
970 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700971
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900972 if (size >= 2) {
973 u16 val = data[off - init_off];
974 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700975 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900976 off += 2;
977 size -= 2;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900980 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700981 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 off++;
983 --size;
984 }
985
Huang Ying3d8387e2012-08-15 09:43:03 +0800986 pci_config_pm_runtime_put(dev);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return count;
989}
990
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400991static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
992 struct bin_attribute *bin_attr, char *buf,
993 loff_t off, size_t count)
Ben Hutchings94e61082008-03-05 16:52:39 +0000994{
Geliang Tang554a6032015-12-23 20:28:13 +0800995 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000996
Hannes Reineckef52e5622016-02-15 09:42:00 +0100997 if (bin_attr->size > 0) {
998 if (off > bin_attr->size)
999 count = 0;
1000 else if (count > bin_attr->size - off)
1001 count = bin_attr->size - off;
1002 }
Ben Hutchings94e61082008-03-05 16:52:39 +00001003
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001004 return pci_read_vpd(dev, off, count, buf);
1005}
Ben Hutchings94e61082008-03-05 16:52:39 +00001006
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001007static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
1008 struct bin_attribute *bin_attr, char *buf,
1009 loff_t off, size_t count)
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001010{
Geliang Tang554a6032015-12-23 20:28:13 +08001011 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001012
Hannes Reineckef52e5622016-02-15 09:42:00 +01001013 if (bin_attr->size > 0) {
1014 if (off > bin_attr->size)
1015 count = 0;
1016 else if (count > bin_attr->size - off)
1017 count = bin_attr->size - off;
1018 }
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001019
1020 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +00001021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023#ifdef HAVE_PCI_LEGACY
1024/**
1025 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001026 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001028 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * @buf: buffer to store results
1030 * @off: offset into legacy I/O port space
1031 * @count: number of bytes to read
1032 *
1033 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1034 * callback routine (pci_legacy_read).
1035 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001036static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
1037 struct bin_attribute *bin_attr, char *buf,
1038 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Geliang Tang554a6032015-12-23 20:28:13 +08001040 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001042 /* Only support 1, 2 or 4 byte accesses */
1043 if (count != 1 && count != 2 && count != 4)
1044 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001046 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/**
1050 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001051 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001053 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * @buf: buffer containing value to be written
1055 * @off: offset into legacy I/O port space
1056 * @count: number of bytes to write
1057 *
1058 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1059 * callback routine (pci_legacy_write).
1060 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001061static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1062 struct bin_attribute *bin_attr, char *buf,
1063 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Geliang Tang554a6032015-12-23 20:28:13 +08001065 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001067 /* Only support 1, 2 or 4 byte accesses */
1068 if (count != 1 && count != 2 && count != 4)
1069 return -EINVAL;
1070
1071 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074/**
1075 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001076 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * @kobj: kobject corresponding to device to be mapped
1078 * @attr: struct bin_attribute for this file
1079 * @vma: struct vm_area_struct passed to mmap
1080 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001081 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * legacy memory space (first meg of bus space) into application virtual
1083 * memory space.
1084 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001085static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1086 struct bin_attribute *attr,
1087 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Geliang Tang554a6032015-12-23 20:28:13 +08001089 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001091 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001092}
1093
1094/**
1095 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001096 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001097 * @kobj: kobject corresponding to device to be mapped
1098 * @attr: struct bin_attribute for this file
1099 * @vma: struct vm_area_struct passed to mmap
1100 *
1101 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1102 * legacy IO space (first meg of bus space) into application virtual
1103 * memory space. Returns -ENOSYS if the operation isn't supported
1104 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001105static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1106 struct bin_attribute *attr,
1107 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001108{
Geliang Tang554a6032015-12-23 20:28:13 +08001109 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001110
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001111 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001112}
1113
1114/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001115 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1116 * @b: bus to create files under
1117 * @mmap_type: I/O port or memory
1118 *
1119 * Stub implementation. Can be overridden by arch if necessary.
1120 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001121void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1122 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001123{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001124}
1125
1126/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001127 * pci_create_legacy_files - create legacy I/O port and memory files
1128 * @b: bus to create files under
1129 *
1130 * Some platforms allow access to legacy I/O port and ISA memory space on
1131 * a per-bus basis. This routine creates the files and ties them into
1132 * their associated read, write and mmap files from pci-sysfs.c
1133 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001134 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001135 * as it is ok to set up the PCI bus without these files.
1136 */
1137void pci_create_legacy_files(struct pci_bus *b)
1138{
1139 int error;
1140
1141 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
1142 GFP_ATOMIC);
1143 if (!b->legacy_io)
1144 goto kzalloc_err;
1145
Stephen Rothwell62e877b82010-03-01 20:38:36 +11001146 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001147 b->legacy_io->attr.name = "legacy_io";
1148 b->legacy_io->size = 0xffff;
1149 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1150 b->legacy_io->read = pci_read_legacy_io;
1151 b->legacy_io->write = pci_write_legacy_io;
1152 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001153 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001154 error = device_create_bin_file(&b->dev, b->legacy_io);
1155 if (error)
1156 goto legacy_io_err;
1157
1158 /* Allocated above after the legacy_io struct */
1159 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +00001160 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001161 b->legacy_mem->attr.name = "legacy_mem";
1162 b->legacy_mem->size = 1024*1024;
1163 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1164 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001165 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001166 error = device_create_bin_file(&b->dev, b->legacy_mem);
1167 if (error)
1168 goto legacy_mem_err;
1169
1170 return;
1171
1172legacy_mem_err:
1173 device_remove_bin_file(&b->dev, b->legacy_io);
1174legacy_io_err:
1175 kfree(b->legacy_io);
1176 b->legacy_io = NULL;
1177kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001178 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 +10001179 return;
1180}
1181
1182void pci_remove_legacy_files(struct pci_bus *b)
1183{
1184 if (b->legacy_io) {
1185 device_remove_bin_file(&b->dev, b->legacy_io);
1186 device_remove_bin_file(&b->dev, b->legacy_mem);
1187 kfree(b->legacy_io); /* both are allocated here */
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190#endif /* HAVE_PCI_LEGACY */
1191
David Woodhousef7195822017-04-12 13:25:59 +01001192#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001193
Martin Wilck3b519e42010-11-10 11:03:21 +01001194int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1195 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001196{
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001197 unsigned long nr, start, size;
1198 resource_size_t pci_start = 0, pci_end;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001199
Martin Wilck3b519e42010-11-10 11:03:21 +01001200 if (pci_resource_len(pdev, resno) == 0)
1201 return 0;
Libin64b00172013-04-15 02:48:54 +00001202 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001203 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -08001204 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001205 if (mmap_api == PCI_MMAP_PROCFS) {
1206 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1207 &pci_start, &pci_end);
1208 pci_start >>= PAGE_SHIFT;
1209 }
Martin Wilck3b519e42010-11-10 11:03:21 +01001210 if (start >= pci_start && start < pci_start + size &&
1211 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001212 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001213 return 0;
1214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/**
1217 * pci_mmap_resource - map a PCI resource into user memory space
1218 * @kobj: kobject for mapping
1219 * @attr: struct bin_attribute for the file being mapped
1220 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001221 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
1223 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001225static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1226 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Geliang Tang554a6032015-12-23 20:28:13 +08001228 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001229 int bar = (unsigned long)attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 enum pci_mmap_state mmap_type;
David Woodhousedca40b12017-04-12 13:25:57 +01001231 struct resource *res = &pdev->resource[bar];
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001232
Bjorn Helgaasca620722016-04-07 17:15:14 -07001233 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1234 return -EINVAL;
1235
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001236 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001237 return -EINVAL;
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
David Woodhousef7195822017-04-12 13:25:59 +01001240
1241 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001242}
1243
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001244static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1245 struct bin_attribute *attr,
1246 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001247{
1248 return pci_mmap_resource(kobj, attr, vma, 0);
1249}
1250
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001251static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1252 struct bin_attribute *attr,
1253 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001254{
1255 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001258static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1259 struct bin_attribute *attr, char *buf,
1260 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001261{
Geliang Tang554a6032015-12-23 20:28:13 +08001262 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001263 int bar = (unsigned long)attr->private;
Alex Williamson86333282010-07-19 09:45:34 -06001264 unsigned long port = off;
Alex Williamson86333282010-07-19 09:45:34 -06001265
David Woodhousedca40b12017-04-12 13:25:57 +01001266 port += pci_resource_start(pdev, bar);
Alex Williamson86333282010-07-19 09:45:34 -06001267
David Woodhousedca40b12017-04-12 13:25:57 +01001268 if (port > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001269 return 0;
1270
David Woodhousedca40b12017-04-12 13:25:57 +01001271 if (port + count - 1 > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001272 return -EINVAL;
1273
1274 switch (count) {
1275 case 1:
1276 if (write)
1277 outb(*(u8 *)buf, port);
1278 else
1279 *(u8 *)buf = inb(port);
1280 return 1;
1281 case 2:
1282 if (write)
1283 outw(*(u16 *)buf, port);
1284 else
1285 *(u16 *)buf = inw(port);
1286 return 2;
1287 case 4:
1288 if (write)
1289 outl(*(u32 *)buf, port);
1290 else
1291 *(u32 *)buf = inl(port);
1292 return 4;
1293 }
1294 return -EINVAL;
1295}
1296
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001297static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1298 struct bin_attribute *attr, char *buf,
1299 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001300{
1301 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1302}
1303
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001304static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1305 struct bin_attribute *attr, char *buf,
1306 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001307{
1308 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001312 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001313 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001314 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001315 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001316 * free their resources.
1317 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001318static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001319{
1320 int i;
1321
1322 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1323 struct bin_attribute *res_attr;
1324
1325 res_attr = pdev->res_attr[i];
1326 if (res_attr) {
1327 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1328 kfree(res_attr);
1329 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001330
1331 res_attr = pdev->res_attr_wc[i];
1332 if (res_attr) {
1333 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1334 kfree(res_attr);
1335 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001336 }
1337}
1338
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001339static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1340{
1341 /* allocate attribute structure, piggyback attribute name */
1342 int name_len = write_combine ? 13 : 10;
1343 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001344 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001345 int retval;
1346
1347 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001348 if (!res_attr)
1349 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001350
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001351 res_attr_name = (char *)(res_attr + 1);
1352
1353 sysfs_bin_attr_init(res_attr);
1354 if (write_combine) {
1355 pdev->res_attr_wc[num] = res_attr;
1356 sprintf(res_attr_name, "resource%d_wc", num);
1357 res_attr->mmap = pci_mmap_resource_wc;
1358 } else {
1359 pdev->res_attr[num] = res_attr;
1360 sprintf(res_attr_name, "resource%d", num);
David Woodhousee854d8b2017-04-12 13:25:56 +01001361 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1362 res_attr->read = pci_read_resource_io;
1363 res_attr->write = pci_write_resource_io;
1364 if (arch_can_pci_mmap_io())
1365 res_attr->mmap = pci_mmap_resource_uc;
1366 } else {
1367 res_attr->mmap = pci_mmap_resource_uc;
1368 }
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001369 }
1370 res_attr->attr.name = res_attr_name;
1371 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1372 res_attr->size = pci_resource_len(pdev, num);
David Woodhousedca40b12017-04-12 13:25:57 +01001373 res_attr->private = (void *)(unsigned long)num;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001374 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1375 if (retval)
1376 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001377
1378 return retval;
1379}
1380
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001381/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001383 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001385 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001387static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001390 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Expose the PCI resources from this device as files */
1393 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* skip empty resources */
1396 if (!pci_resource_len(pdev, i))
1397 continue;
1398
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001399 retval = pci_create_attr(pdev, i, 0);
1400 /* for prefetchable resources, create a WC mappable file */
David Woodhouseae749c72017-04-12 13:25:54 +01001401 if (!retval && arch_can_pci_mmap_wc() &&
1402 pdev->resource[i].flags & IORESOURCE_PREFETCH)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001403 retval = pci_create_attr(pdev, i, 1);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001404 if (retval) {
1405 pci_remove_resource_files(pdev);
1406 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001412int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1413void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#endif /* HAVE_PCI_MMAP */
1415
1416/**
1417 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001418 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001420 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * @buf: user input
1422 * @off: file offset
1423 * @count: number of byte in input
1424 *
1425 * writing anything except 0 enables it
1426 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001427static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1428 struct bin_attribute *bin_attr, char *buf,
1429 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Geliang Tang554a6032015-12-23 20:28:13 +08001431 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if ((off == 0) && (*buf == '0') && (count == 2))
1434 pdev->rom_attr_enabled = 0;
1435 else
1436 pdev->rom_attr_enabled = 1;
1437
1438 return count;
1439}
1440
1441/**
1442 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001443 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001445 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * @buf: where to put the data we read from the ROM
1447 * @off: file offset
1448 * @count: number of bytes to read
1449 *
1450 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1451 * device corresponding to @kobj.
1452 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001453static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1454 struct bin_attribute *bin_attr, char *buf,
1455 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Geliang Tang554a6032015-12-23 20:28:13 +08001457 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 void __iomem *rom;
1459 size_t size;
1460
1461 if (!pdev->rom_attr_enabled)
1462 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001465 if (!rom || !size)
1466 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (off >= size)
1469 count = 0;
1470 else {
1471 if (off + count > size)
1472 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 memcpy_fromio(buf, rom + off, count);
1475 }
1476 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return count;
1479}
1480
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301481static const struct bin_attribute pci_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 .attr = {
1483 .name = "config",
1484 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001486 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 .read = pci_read_config,
1488 .write = pci_write_config,
1489};
1490
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301491static const struct bin_attribute pcie_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .attr = {
1493 .name = "config",
1494 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001496 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .read = pci_read_config,
1498 .write = pci_write_config,
1499};
1500
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001501static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1502 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001503{
1504 struct pci_dev *pdev = to_pci_dev(dev);
1505 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001506 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001507
1508 if (result < 0)
1509 return result;
1510
1511 if (val != 1)
1512 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001513
1514 result = pci_reset_function(pdev);
1515 if (result < 0)
1516 return result;
1517
1518 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001519}
1520
1521static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1522
Zhao, Yu280c73d2008-10-13 20:01:00 +08001523static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1524{
1525 int retval;
1526 struct bin_attribute *attr;
1527
1528 /* If the device has VPD, try to expose it in sysfs. */
1529 if (dev->vpd) {
1530 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1531 if (!attr)
1532 return -ENOMEM;
1533
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001534 sysfs_bin_attr_init(attr);
Hannes Reinecke104daa72016-02-15 09:42:01 +01001535 attr->size = 0;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001536 attr->attr.name = "vpd";
1537 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001538 attr->read = read_vpd_attr;
1539 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001540 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1541 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001542 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001543 return retval;
1544 }
1545 dev->vpd->attr = attr;
1546 }
1547
1548 /* Active State Power Management */
1549 pcie_aspm_create_sysfs_dev_files(dev);
1550
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001551 if (!pci_probe_reset_function(dev)) {
1552 retval = device_create_file(&dev->dev, &reset_attr);
1553 if (retval)
1554 goto error;
1555 dev->reset_fn = 1;
1556 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001557 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001558
1559error:
1560 pcie_aspm_remove_sysfs_dev_files(dev);
1561 if (dev->vpd && dev->vpd->attr) {
1562 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1563 kfree(dev->vpd->attr);
1564 }
1565
1566 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001567}
1568
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001569int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001571 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001572 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001573 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (!sysfs_initialized)
1576 return -EACCES;
1577
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001578 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001579 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001580 else
1581 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001582 if (retval)
1583 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001585 retval = pci_create_resource_files(pdev);
1586 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001587 goto err_config_file;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001590 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001591 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001592 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001593 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001594 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001595 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001597 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001598 attr->size = rom_size;
1599 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001600 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001601 attr->read = pci_read_rom;
1602 attr->write = pci_write_rom;
1603 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1604 if (retval) {
1605 kfree(attr);
1606 goto err_resource_files;
1607 }
1608 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001610
Zhao, Yu280c73d2008-10-13 20:01:00 +08001611 /* add sysfs entries for various capabilities */
1612 retval = pci_create_capabilities_sysfs(pdev);
1613 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001614 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001615
Narendra K911e1c92010-07-26 05:56:50 -05001616 pci_create_firmware_label_files(pdev);
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001619
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001620err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001621 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001622 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001623 kfree(pdev->rom_attr);
1624 pdev->rom_attr = NULL;
1625 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001626err_resource_files:
1627 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001628err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001629 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001630 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001631 else
1632 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001633err:
1634 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Zhao, Yu280c73d2008-10-13 20:01:00 +08001637static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1638{
1639 if (dev->vpd && dev->vpd->attr) {
1640 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1641 kfree(dev->vpd->attr);
1642 }
1643
1644 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001645 if (dev->reset_fn) {
1646 device_remove_file(&dev->dev, &reset_attr);
1647 dev->reset_fn = 0;
1648 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651/**
1652 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1653 * @pdev: device whose entries we should free
1654 *
1655 * Cleanup when @pdev is removed from sysfs.
1656 */
1657void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1658{
David Millerd67afe52006-11-10 12:27:48 -08001659 if (!sysfs_initialized)
1660 return;
1661
Zhao, Yu280c73d2008-10-13 20:01:00 +08001662 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001663
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001664 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001666 else
1667 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 pci_remove_resource_files(pdev);
1670
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001671 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001672 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1673 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001674 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Narendra K911e1c92010-07-26 05:56:50 -05001676
1677 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680static int __init pci_sysfs_init(void)
1681{
1682 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001683 int retval;
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001686 for_each_pci_dev(pdev) {
1687 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001688 if (retval) {
1689 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001690 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001691 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 return 0;
1695}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001696late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001697
1698static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001699 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001700 NULL,
1701};
1702
1703static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001704 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001705{
Geliang Tang554a6032015-12-23 20:28:13 +08001706 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001707 struct pci_dev *pdev = to_pci_dev(dev);
1708
1709 if (a == &vga_attr.attr)
1710 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1711 return 0;
1712
Yinghai Lu4e15c462012-11-05 15:20:34 -05001713 return a->mode;
1714}
1715
Jiang Liudfab88b2013-05-31 12:21:31 +08001716static struct attribute *pci_dev_hp_attrs[] = {
1717 &dev_remove_attr.attr,
1718 &dev_rescan_attr.attr,
1719 NULL,
1720};
1721
1722static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001723 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001724{
Geliang Tang554a6032015-12-23 20:28:13 +08001725 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001726 struct pci_dev *pdev = to_pci_dev(dev);
1727
1728 if (pdev->is_virtfn)
1729 return 0;
1730
1731 return a->mode;
1732}
1733
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001734static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1735 struct attribute *a, int n)
1736{
1737 struct device *dev = kobj_to_dev(kobj);
1738 struct pci_dev *pdev = to_pci_dev(dev);
1739
1740 if (pci_is_bridge(pdev))
1741 return a->mode;
1742
1743 return 0;
1744}
1745
1746static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1747 struct attribute *a, int n)
1748{
1749 struct device *dev = kobj_to_dev(kobj);
1750 struct pci_dev *pdev = to_pci_dev(dev);
1751
1752 if (pci_is_pcie(pdev))
1753 return a->mode;
1754
1755 return 0;
1756}
1757
1758static const struct attribute_group pci_dev_group = {
1759 .attrs = pci_dev_attrs,
1760};
1761
1762const struct attribute_group *pci_dev_groups[] = {
1763 &pci_dev_group,
1764 NULL,
1765};
1766
1767static const struct attribute_group pci_bridge_group = {
1768 .attrs = pci_bridge_attrs,
1769};
1770
1771const struct attribute_group *pci_bridge_groups[] = {
1772 &pci_bridge_group,
1773 NULL,
1774};
1775
1776static const struct attribute_group pcie_dev_group = {
1777 .attrs = pcie_dev_attrs,
1778};
1779
1780const struct attribute_group *pcie_dev_groups[] = {
1781 &pcie_dev_group,
1782 NULL,
1783};
1784
Arvind Yadave7ea9822017-07-11 14:57:07 +05301785static const struct attribute_group pci_dev_hp_attr_group = {
Jiang Liudfab88b2013-05-31 12:21:31 +08001786 .attrs = pci_dev_hp_attrs,
1787 .is_visible = pci_dev_hp_attrs_are_visible,
1788};
1789
Donald Dutile17893822012-11-05 15:20:36 -05001790#ifdef CONFIG_PCI_IOV
1791static struct attribute *sriov_dev_attrs[] = {
1792 &sriov_totalvfs_attr.attr,
1793 &sriov_numvfs_attr.attr,
Filippo Sironi7dfca152017-10-09 01:09:11 +02001794 &sriov_offset_attr.attr,
1795 &sriov_stride_attr.attr,
1796 &sriov_vf_device_attr.attr,
Bodong Wang0e7df222017-04-13 01:51:40 +03001797 &sriov_drivers_autoprobe_attr.attr,
Donald Dutile17893822012-11-05 15:20:36 -05001798 NULL,
1799};
1800
1801static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001802 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001803{
Geliang Tang554a6032015-12-23 20:28:13 +08001804 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001805
1806 if (!dev_is_pf(dev))
1807 return 0;
1808
1809 return a->mode;
1810}
1811
Arvind Yadave7ea9822017-07-11 14:57:07 +05301812static const struct attribute_group sriov_dev_attr_group = {
Donald Dutile17893822012-11-05 15:20:36 -05001813 .attrs = sriov_dev_attrs,
1814 .is_visible = sriov_attrs_are_visible,
1815};
1816#endif /* CONFIG_PCI_IOV */
1817
Arvind Yadave7ea9822017-07-11 14:57:07 +05301818static const struct attribute_group pci_dev_attr_group = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001819 .attrs = pci_dev_dev_attrs,
1820 .is_visible = pci_dev_attrs_are_visible,
1821};
1822
Arvind Yadave7ea9822017-07-11 14:57:07 +05301823static const struct attribute_group pci_bridge_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001824 .attrs = pci_bridge_attrs,
1825 .is_visible = pci_bridge_attrs_are_visible,
1826};
1827
Arvind Yadave7ea9822017-07-11 14:57:07 +05301828static const struct attribute_group pcie_dev_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001829 .attrs = pcie_dev_attrs,
1830 .is_visible = pcie_dev_attrs_are_visible,
1831};
1832
Yinghai Lu4e15c462012-11-05 15:20:34 -05001833static const struct attribute_group *pci_dev_attr_groups[] = {
1834 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001835 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001836#ifdef CONFIG_PCI_IOV
1837 &sriov_dev_attr_group,
1838#endif
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001839 &pci_bridge_attr_group,
1840 &pcie_dev_attr_group,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001841 NULL,
1842};
1843
Bhumika Goyal69f2dc22017-09-12 16:43:33 +05301844const struct device_type pci_dev_type = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001845 .groups = pci_dev_attr_groups,
1846};