blob: 707a4b46b97acb340f86e38f3dc2322756162a40 [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) {
171 case PCI_EXP_LNKCAP_SLS_8_0GB:
172 speed = "8 GT/s";
173 break;
174 case PCI_EXP_LNKCAP_SLS_5_0GB:
175 speed = "5 GT/s";
176 break;
177 case PCI_EXP_LNKCAP_SLS_2_5GB:
178 speed = "2.5 GT/s";
179 break;
180 default:
181 speed = "Unknown speed";
182 }
183
184 return sprintf(buf, "%s\n", speed);
185}
186static DEVICE_ATTR_RO(max_link_speed);
187
188static ssize_t max_link_width_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 struct pci_dev *pci_dev = to_pci_dev(dev);
192 u32 linkcap;
193 int err;
194
195 err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap);
196 if (err)
197 return -EINVAL;
198
199 return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4);
200}
201static DEVICE_ATTR_RO(max_link_width);
202
203static ssize_t current_link_speed_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
205{
206 struct pci_dev *pci_dev = to_pci_dev(dev);
207 u16 linkstat;
208 int err;
209 const char *speed;
210
211 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
212 if (err)
213 return -EINVAL;
214
215 switch (linkstat & PCI_EXP_LNKSTA_CLS) {
216 case PCI_EXP_LNKSTA_CLS_8_0GB:
217 speed = "8 GT/s";
218 break;
219 case PCI_EXP_LNKSTA_CLS_5_0GB:
220 speed = "5 GT/s";
221 break;
222 case PCI_EXP_LNKSTA_CLS_2_5GB:
223 speed = "2.5 GT/s";
224 break;
225 default:
226 speed = "Unknown speed";
227 }
228
229 return sprintf(buf, "%s\n", speed);
230}
231static DEVICE_ATTR_RO(current_link_speed);
232
233static ssize_t current_link_width_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
236 struct pci_dev *pci_dev = to_pci_dev(dev);
237 u16 linkstat;
238 int err;
239
240 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
241 if (err)
242 return -EINVAL;
243
244 return sprintf(buf, "%u\n",
245 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
246}
247static DEVICE_ATTR_RO(current_link_width);
248
249static ssize_t secondary_bus_number_show(struct device *dev,
250 struct device_attribute *attr,
251 char *buf)
252{
253 struct pci_dev *pci_dev = to_pci_dev(dev);
254 u8 sec_bus;
255 int err;
256
257 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
258 if (err)
259 return -EINVAL;
260
261 return sprintf(buf, "%u\n", sec_bus);
262}
263static DEVICE_ATTR_RO(secondary_bus_number);
264
265static ssize_t subordinate_bus_number_show(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
268{
269 struct pci_dev *pci_dev = to_pci_dev(dev);
270 u8 sub_bus;
271 int err;
272
273 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
274 if (err)
275 return -EINVAL;
276
277 return sprintf(buf, "%u\n", sub_bus);
278}
279static DEVICE_ATTR_RO(subordinate_bus_number);
280
Stuart Hayes0077a842018-01-05 12:51:55 -0600281static ssize_t ari_enabled_show(struct device *dev,
282 struct device_attribute *attr,
283 char *buf)
284{
285 struct pci_dev *pci_dev = to_pci_dev(dev);
286
287 return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
288}
289static DEVICE_ATTR_RO(ari_enabled);
290
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400291static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700293{
294 struct pci_dev *pci_dev = to_pci_dev(dev);
295
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200296 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700297 pci_dev->vendor, pci_dev->device,
298 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
299 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
300 (u8)(pci_dev->class));
301}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700302static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800303
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700304static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400305 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200306{
307 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800308 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900309 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800310
311 if (result < 0)
312 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200313
314 /* this can crash the machine when done on the "wrong" device */
315 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800316 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200317
Trent Piepho92425a42008-11-30 17:10:12 -0800318 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900319 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800320 pci_disable_device(pdev);
321 else
322 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800323 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800324 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200325
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800326 return result < 0 ? result : count;
327}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200328
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700329static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400330 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800331{
332 struct pci_dev *pdev;
333
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400334 pdev = to_pci_dev(dev);
335 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200336}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700337static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200338
Brice Goglin81bb0e12007-01-28 10:53:40 +0100339#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400340static ssize_t numa_node_store(struct device *dev,
341 struct device_attribute *attr, const char *buf,
342 size_t count)
343{
344 struct pci_dev *pdev = to_pci_dev(dev);
345 int node, ret;
346
347 if (!capable(CAP_SYS_ADMIN))
348 return -EPERM;
349
350 ret = kstrtoint(buf, 0, &node);
351 if (ret)
352 return ret;
353
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100354 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
355 return -EINVAL;
356
357 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400358 return -EINVAL;
359
360 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
361 dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
362 node);
363
364 dev->numa_node = node;
365 return count;
366}
367
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400368static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
369 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100370{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400371 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100372}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400373static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100374#endif
375
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400376static ssize_t dma_mask_bits_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800378{
379 struct pci_dev *pdev = to_pci_dev(dev);
380
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400381 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800382}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700383static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800384
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400385static ssize_t consistent_dma_mask_bits_show(struct device *dev,
386 struct device_attribute *attr,
387 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800388{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400389 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800390}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700391static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800392
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400393static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
394 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400395{
396 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800397 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400398
Yijing Wang468ff152014-09-23 13:27:24 +0800399 return sprintf(buf, "%u\n", subordinate ?
400 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
401 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400402}
403
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400404static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
405 const char *buf, size_t count)
Brice Goglinfe970642006-08-31 01:55:15 -0400406{
407 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800408 struct pci_bus *subordinate = pdev->subordinate;
Trent Piepho92425a42008-11-30 17:10:12 -0800409 unsigned long val;
410
Jingoo Han9a994e82013-06-01 16:25:25 +0900411 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800412 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400413
Brice Goglinfe970642006-08-31 01:55:15 -0400414 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800415 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400416
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700417 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800418 * "no_msi" and "bus_flags" only affect what happens when a driver
419 * requests MSI or MSI-X. They don't affect any drivers that have
420 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700421 */
Yijing Wang468ff152014-09-23 13:27:24 +0800422 if (!subordinate) {
423 pdev->no_msi = !val;
424 dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n",
425 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400426 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400427 }
428
Yijing Wang468ff152014-09-23 13:27:24 +0800429 if (val)
430 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
431 else
432 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
433
434 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
435 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400436 return count;
437}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700438static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700439
Alex Chiang705b1aa2009-03-20 14:56:31 -0600440static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
441 size_t count)
442{
443 unsigned long val;
444 struct pci_bus *b = NULL;
445
Jingoo Han9a994e82013-06-01 16:25:25 +0900446 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600447 return -EINVAL;
448
449 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100450 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600451 while ((b = pci_find_next_bus(b)) != NULL)
452 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100453 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600454 }
455 return count;
456}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600457static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600458
Sachin Kamatbf22c902013-09-28 15:42:00 +0530459static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600460 &bus_attr_rescan.attr,
461 NULL,
462};
463
464static const struct attribute_group pci_bus_group = {
465 .attrs = pci_bus_attrs,
466};
467
468const struct attribute_group *pci_bus_groups[] = {
469 &pci_bus_group,
470 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600471};
Alex Chiang77c27c72009-03-20 14:56:36 -0600472
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400473static ssize_t dev_rescan_store(struct device *dev,
474 struct device_attribute *attr, const char *buf,
475 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600476{
477 unsigned long val;
478 struct pci_dev *pdev = to_pci_dev(dev);
479
Jingoo Han9a994e82013-06-01 16:25:25 +0900480 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600481 return -EINVAL;
482
483 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100484 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600485 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100486 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600487 }
488 return count;
489}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530490static struct device_attribute dev_rescan_attr = __ATTR(rescan,
491 (S_IWUSR|S_IWGRP),
492 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600493
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400494static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600496{
Alex Chiang77c27c72009-03-20 14:56:36 -0600497 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600498
Jingoo Han9a994e82013-06-01 16:25:25 +0900499 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600500 return -EINVAL;
501
Tejun Heobc6caf02014-02-03 14:03:02 -0500502 if (val && device_remove_file_self(dev, attr))
503 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600504 return count;
505}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530506static struct device_attribute dev_remove_attr = __ATTR(remove,
507 (S_IWUSR|S_IWGRP),
508 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700509
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400510static ssize_t dev_bus_rescan_store(struct device *dev,
511 struct device_attribute *attr,
512 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700513{
514 unsigned long val;
515 struct pci_bus *bus = to_pci_bus(dev);
516
Jingoo Han9a994e82013-06-01 16:25:25 +0900517 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700518 return -EINVAL;
519
520 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100521 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800522 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
523 pci_rescan_bus_bridge_resize(bus->self);
524 else
525 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100526 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700527 }
528 return count;
529}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700530static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700531
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100532#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800533static ssize_t d3cold_allowed_store(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
536{
537 struct pci_dev *pdev = to_pci_dev(dev);
538 unsigned long val;
539
Jingoo Han9a994e82013-06-01 16:25:25 +0900540 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800541 return -EINVAL;
542
543 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300544 if (pdev->d3cold_allowed)
545 pci_d3cold_enable(pdev);
546 else
547 pci_d3cold_disable(pdev);
548
Huang Ying448bd852012-06-23 10:23:51 +0800549 pm_runtime_resume(dev);
550
551 return count;
552}
553
554static ssize_t d3cold_allowed_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
557 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400558 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800559}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700560static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800561#endif
562
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200563#ifdef CONFIG_OF
564static ssize_t devspec_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
566{
567 struct pci_dev *pdev = to_pci_dev(dev);
568 struct device_node *np = pci_device_to_OF_node(pdev);
569
Rob Herringb63773a2017-07-18 16:43:21 -0500570 if (np == NULL)
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200571 return 0;
Rob Herringb63773a2017-07-18 16:43:21 -0500572 return sprintf(buf, "%pOF", np);
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200573}
574static DEVICE_ATTR_RO(devspec);
575#endif
576
Donald Dutile17893822012-11-05 15:20:36 -0500577#ifdef CONFIG_PCI_IOV
578static ssize_t sriov_totalvfs_show(struct device *dev,
579 struct device_attribute *attr,
580 char *buf)
581{
582 struct pci_dev *pdev = to_pci_dev(dev);
583
Donald Dutilebff73152012-11-05 15:20:37 -0500584 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500585}
586
587
588static ssize_t sriov_numvfs_show(struct device *dev,
589 struct device_attribute *attr,
590 char *buf)
591{
592 struct pci_dev *pdev = to_pci_dev(dev);
593
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700594 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500595}
596
597/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700598 * num_vfs > 0; number of VFs to enable
599 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500600 *
601 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700602 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500603 */
604static ssize_t sriov_numvfs_store(struct device *dev,
605 struct device_attribute *attr,
606 const char *buf, size_t count)
607{
608 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700609 int ret;
610 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500611
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700612 ret = kstrtou16(buf, 0, &num_vfs);
613 if (ret < 0)
614 return ret;
615
616 if (num_vfs > pci_sriov_get_totalvfs(pdev))
617 return -ERANGE;
618
Jakub Kicinski17530e72017-05-22 15:50:23 -0700619 device_lock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800620
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700621 if (num_vfs == pdev->sriov->num_VFs)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800622 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500623
624 /* is PF driver loaded w/callback */
625 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700626 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800627 ret = -ENOENT;
628 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500629 }
630
Donald Dutile17893822012-11-05 15:20:36 -0500631 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700632 /* disable VFs */
633 ret = pdev->driver->sriov_configure(pdev, 0);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800634 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500635 }
636
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700637 /* enable VFs */
638 if (pdev->sriov->num_VFs) {
639 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
640 pdev->sriov->num_VFs, num_vfs);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800641 ret = -EBUSY;
642 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700643 }
Donald Dutile17893822012-11-05 15:20:36 -0500644
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700645 ret = pdev->driver->sriov_configure(pdev, num_vfs);
646 if (ret < 0)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800647 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700648
649 if (ret != num_vfs)
650 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
651 num_vfs, ret);
652
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800653exit:
Jakub Kicinski17530e72017-05-22 15:50:23 -0700654 device_unlock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800655
656 if (ret < 0)
657 return ret;
658
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700659 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500660}
661
Filippo Sironi7dfca152017-10-09 01:09:11 +0200662static ssize_t sriov_offset_show(struct device *dev,
663 struct device_attribute *attr,
664 char *buf)
665{
666 struct pci_dev *pdev = to_pci_dev(dev);
667
668 return sprintf(buf, "%u\n", pdev->sriov->offset);
669}
670
671static ssize_t sriov_stride_show(struct device *dev,
672 struct device_attribute *attr,
673 char *buf)
674{
675 struct pci_dev *pdev = to_pci_dev(dev);
676
677 return sprintf(buf, "%u\n", pdev->sriov->stride);
678}
679
680static ssize_t sriov_vf_device_show(struct device *dev,
681 struct device_attribute *attr,
682 char *buf)
683{
684 struct pci_dev *pdev = to_pci_dev(dev);
685
686 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
687}
688
Bodong Wang0e7df222017-04-13 01:51:40 +0300689static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
690 struct device_attribute *attr,
691 char *buf)
692{
693 struct pci_dev *pdev = to_pci_dev(dev);
694
695 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
696}
697
698static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
699 struct device_attribute *attr,
700 const char *buf, size_t count)
701{
702 struct pci_dev *pdev = to_pci_dev(dev);
703 bool drivers_autoprobe;
704
705 if (kstrtobool(buf, &drivers_autoprobe) < 0)
706 return -EINVAL;
707
708 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
709
710 return count;
711}
712
Donald Dutile17893822012-11-05 15:20:36 -0500713static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
714static struct device_attribute sriov_numvfs_attr =
715 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
716 sriov_numvfs_show, sriov_numvfs_store);
Filippo Sironi7dfca152017-10-09 01:09:11 +0200717static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
718static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
719static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
Bodong Wang0e7df222017-04-13 01:51:40 +0300720static struct device_attribute sriov_drivers_autoprobe_attr =
721 __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
722 sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
Donald Dutile17893822012-11-05 15:20:36 -0500723#endif /* CONFIG_PCI_IOV */
724
Alex Williamson782a9852014-05-20 08:53:21 -0600725static ssize_t driver_override_store(struct device *dev,
726 struct device_attribute *attr,
727 const char *buf, size_t count)
728{
729 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200730 char *driver_override, *old, *cp;
Alex Williamson782a9852014-05-20 08:53:21 -0600731
Sasha Levin4efe8742015-02-04 17:38:15 -0500732 /* We need to keep extra room for a newline */
733 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600734 return -EINVAL;
735
736 driver_override = kstrndup(buf, count, GFP_KERNEL);
737 if (!driver_override)
738 return -ENOMEM;
739
740 cp = strchr(driver_override, '\n');
741 if (cp)
742 *cp = '\0';
743
Nicolai Stange95614752017-09-11 09:45:40 +0200744 device_lock(dev);
745 old = pdev->driver_override;
Alex Williamson782a9852014-05-20 08:53:21 -0600746 if (strlen(driver_override)) {
747 pdev->driver_override = driver_override;
748 } else {
749 kfree(driver_override);
750 pdev->driver_override = NULL;
751 }
Nicolai Stange95614752017-09-11 09:45:40 +0200752 device_unlock(dev);
Alex Williamson782a9852014-05-20 08:53:21 -0600753
754 kfree(old);
755
756 return count;
757}
758
759static ssize_t driver_override_show(struct device *dev,
760 struct device_attribute *attr, char *buf)
761{
762 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200763 ssize_t len;
Alex Williamson782a9852014-05-20 08:53:21 -0600764
Nicolai Stange95614752017-09-11 09:45:40 +0200765 device_lock(dev);
766 len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
767 device_unlock(dev);
768 return len;
Alex Williamson782a9852014-05-20 08:53:21 -0600769}
770static DEVICE_ATTR_RW(driver_override);
771
Sachin Kamatbf22c902013-09-28 15:42:00 +0530772static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700773 &dev_attr_resource.attr,
774 &dev_attr_vendor.attr,
775 &dev_attr_device.attr,
776 &dev_attr_subsystem_vendor.attr,
777 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600778 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700779 &dev_attr_class.attr,
780 &dev_attr_irq.attr,
781 &dev_attr_local_cpus.attr,
782 &dev_attr_local_cpulist.attr,
783 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100784#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700785 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100786#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700787 &dev_attr_dma_mask_bits.attr,
788 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700789 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700790 &dev_attr_broken_parity_status.attr,
791 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100792#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700793 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800794#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200795#ifdef CONFIG_OF
796 &dev_attr_devspec.attr,
797#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600798 &dev_attr_driver_override.attr,
Stuart Hayes0077a842018-01-05 12:51:55 -0600799 &dev_attr_ari_enabled.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700800 NULL,
801};
802
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800803static struct attribute *pci_bridge_attrs[] = {
804 &dev_attr_subordinate_bus_number.attr,
805 &dev_attr_secondary_bus_number.attr,
806 NULL,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700807};
808
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800809static struct attribute *pcie_dev_attrs[] = {
810 &dev_attr_current_link_speed.attr,
811 &dev_attr_current_link_width.attr,
812 &dev_attr_max_link_width.attr,
813 &dev_attr_max_link_speed.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700814 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815};
816
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700817static struct attribute *pcibus_attrs[] = {
818 &dev_attr_rescan.attr,
819 &dev_attr_cpuaffinity.attr,
820 &dev_attr_cpulistaffinity.attr,
821 NULL,
822};
823
824static const struct attribute_group pcibus_group = {
825 .attrs = pcibus_attrs,
826};
827
828const struct attribute_group *pcibus_groups[] = {
829 &pcibus_group,
830 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700831};
832
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400833static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
834 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000835{
836 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400837 struct pci_dev *vga_dev = vga_default_device();
838
839 if (vga_dev)
840 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000841
842 return sprintf(buf, "%u\n",
843 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
844 IORESOURCE_ROM_SHADOW));
845}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530846static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000847
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400848static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
849 struct bin_attribute *bin_attr, char *buf,
850 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Geliang Tang554a6032015-12-23 20:28:13 +0800852 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned int size = 64;
854 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400855 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700858 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400860 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (off > size)
864 return 0;
865 if (off + count > size) {
866 size -= off;
867 count = size;
868 } else {
869 size = count;
870 }
871
Huang Ying3d8387e2012-08-15 09:43:03 +0800872 pci_config_pm_runtime_get(dev);
873
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900874 if ((off & 1) && size) {
875 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700876 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900877 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900879 size--;
880 }
881
882 if ((off & 3) && size > 2) {
883 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700884 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900885 data[off - init_off] = val & 0xff;
886 data[off - init_off + 1] = (val >> 8) & 0xff;
887 off += 2;
888 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900892 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700893 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900894 data[off - init_off] = val & 0xff;
895 data[off - init_off + 1] = (val >> 8) & 0xff;
896 data[off - init_off + 2] = (val >> 16) & 0xff;
897 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 off += 4;
899 size -= 4;
900 }
901
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900902 if (size >= 2) {
903 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700904 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900905 data[off - init_off] = val & 0xff;
906 data[off - init_off + 1] = (val >> 8) & 0xff;
907 off += 2;
908 size -= 2;
909 }
910
911 if (size > 0) {
912 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700913 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900914 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 off++;
916 --size;
917 }
918
Huang Ying3d8387e2012-08-15 09:43:03 +0800919 pci_config_pm_runtime_put(dev);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return count;
922}
923
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400924static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
925 struct bin_attribute *bin_attr, char *buf,
926 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Geliang Tang554a6032015-12-23 20:28:13 +0800928 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned int size = count;
930 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400931 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (off > dev->cfg_size)
934 return 0;
935 if (off + count > dev->cfg_size) {
936 size = dev->cfg_size - off;
937 count = size;
938 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700939
Huang Ying3d8387e2012-08-15 09:43:03 +0800940 pci_config_pm_runtime_get(dev);
941
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900942 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700943 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900945 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700947
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900948 if ((off & 3) && size > 2) {
949 u16 val = data[off - init_off];
950 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400951 pci_user_write_config_word(dev, off, val);
952 off += 2;
953 size -= 2;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900957 u32 val = data[off - init_off];
958 val |= (u32) data[off - init_off + 1] << 8;
959 val |= (u32) data[off - init_off + 2] << 16;
960 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700961 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 off += 4;
963 size -= 4;
964 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700965
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900966 if (size >= 2) {
967 u16 val = data[off - init_off];
968 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700969 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900970 off += 2;
971 size -= 2;
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900974 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700975 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 off++;
977 --size;
978 }
979
Huang Ying3d8387e2012-08-15 09:43:03 +0800980 pci_config_pm_runtime_put(dev);
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return count;
983}
984
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400985static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
986 struct bin_attribute *bin_attr, char *buf,
987 loff_t off, size_t count)
Ben Hutchings94e61082008-03-05 16:52:39 +0000988{
Geliang Tang554a6032015-12-23 20:28:13 +0800989 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000990
Hannes Reineckef52e5622016-02-15 09:42:00 +0100991 if (bin_attr->size > 0) {
992 if (off > bin_attr->size)
993 count = 0;
994 else if (count > bin_attr->size - off)
995 count = bin_attr->size - off;
996 }
Ben Hutchings94e61082008-03-05 16:52:39 +0000997
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800998 return pci_read_vpd(dev, off, count, buf);
999}
Ben Hutchings94e61082008-03-05 16:52:39 +00001000
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001001static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
1002 struct bin_attribute *bin_attr, char *buf,
1003 loff_t off, size_t count)
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001004{
Geliang Tang554a6032015-12-23 20:28:13 +08001005 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001006
Hannes Reineckef52e5622016-02-15 09:42:00 +01001007 if (bin_attr->size > 0) {
1008 if (off > bin_attr->size)
1009 count = 0;
1010 else if (count > bin_attr->size - off)
1011 count = bin_attr->size - off;
1012 }
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001013
1014 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +00001015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017#ifdef HAVE_PCI_LEGACY
1018/**
1019 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001020 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001022 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * @buf: buffer to store results
1024 * @off: offset into legacy I/O port space
1025 * @count: number of bytes to read
1026 *
1027 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1028 * callback routine (pci_legacy_read).
1029 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001030static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
1031 struct bin_attribute *bin_attr, char *buf,
1032 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Geliang Tang554a6032015-12-23 20:28:13 +08001034 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001036 /* Only support 1, 2 or 4 byte accesses */
1037 if (count != 1 && count != 2 && count != 4)
1038 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001040 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/**
1044 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001045 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001047 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * @buf: buffer containing value to be written
1049 * @off: offset into legacy I/O port space
1050 * @count: number of bytes to write
1051 *
1052 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1053 * callback routine (pci_legacy_write).
1054 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001055static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1056 struct bin_attribute *bin_attr, char *buf,
1057 loff_t off, size_t count)
Linus 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 /* Only support 1, 2 or 4 byte accesses */
1062 if (count != 1 && count != 2 && count != 4)
1063 return -EINVAL;
1064
1065 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068/**
1069 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001070 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * @kobj: kobject corresponding to device to be mapped
1072 * @attr: struct bin_attribute for this file
1073 * @vma: struct vm_area_struct passed to mmap
1074 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001075 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * legacy memory space (first meg of bus space) into application virtual
1077 * memory space.
1078 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001079static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1080 struct bin_attribute *attr,
1081 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Geliang Tang554a6032015-12-23 20:28:13 +08001083 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001085 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001086}
1087
1088/**
1089 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001090 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001091 * @kobj: kobject corresponding to device to be mapped
1092 * @attr: struct bin_attribute for this file
1093 * @vma: struct vm_area_struct passed to mmap
1094 *
1095 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1096 * legacy IO space (first meg of bus space) into application virtual
1097 * memory space. Returns -ENOSYS if the operation isn't supported
1098 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001099static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1100 struct bin_attribute *attr,
1101 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001102{
Geliang Tang554a6032015-12-23 20:28:13 +08001103 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001104
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001105 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001106}
1107
1108/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001109 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1110 * @b: bus to create files under
1111 * @mmap_type: I/O port or memory
1112 *
1113 * Stub implementation. Can be overridden by arch if necessary.
1114 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001115void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1116 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001117{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001118}
1119
1120/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001121 * pci_create_legacy_files - create legacy I/O port and memory files
1122 * @b: bus to create files under
1123 *
1124 * Some platforms allow access to legacy I/O port and ISA memory space on
1125 * a per-bus basis. This routine creates the files and ties them into
1126 * their associated read, write and mmap files from pci-sysfs.c
1127 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001128 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001129 * as it is ok to set up the PCI bus without these files.
1130 */
1131void pci_create_legacy_files(struct pci_bus *b)
1132{
1133 int error;
1134
1135 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
1136 GFP_ATOMIC);
1137 if (!b->legacy_io)
1138 goto kzalloc_err;
1139
Stephen Rothwell62e877b82010-03-01 20:38:36 +11001140 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001141 b->legacy_io->attr.name = "legacy_io";
1142 b->legacy_io->size = 0xffff;
1143 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1144 b->legacy_io->read = pci_read_legacy_io;
1145 b->legacy_io->write = pci_write_legacy_io;
1146 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001147 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001148 error = device_create_bin_file(&b->dev, b->legacy_io);
1149 if (error)
1150 goto legacy_io_err;
1151
1152 /* Allocated above after the legacy_io struct */
1153 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +00001154 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001155 b->legacy_mem->attr.name = "legacy_mem";
1156 b->legacy_mem->size = 1024*1024;
1157 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1158 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001159 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001160 error = device_create_bin_file(&b->dev, b->legacy_mem);
1161 if (error)
1162 goto legacy_mem_err;
1163
1164 return;
1165
1166legacy_mem_err:
1167 device_remove_bin_file(&b->dev, b->legacy_io);
1168legacy_io_err:
1169 kfree(b->legacy_io);
1170 b->legacy_io = NULL;
1171kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001172 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 +10001173 return;
1174}
1175
1176void pci_remove_legacy_files(struct pci_bus *b)
1177{
1178 if (b->legacy_io) {
1179 device_remove_bin_file(&b->dev, b->legacy_io);
1180 device_remove_bin_file(&b->dev, b->legacy_mem);
1181 kfree(b->legacy_io); /* both are allocated here */
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184#endif /* HAVE_PCI_LEGACY */
1185
David Woodhousef7195822017-04-12 13:25:59 +01001186#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001187
Martin Wilck3b519e42010-11-10 11:03:21 +01001188int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1189 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001190{
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001191 unsigned long nr, start, size;
1192 resource_size_t pci_start = 0, pci_end;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001193
Martin Wilck3b519e42010-11-10 11:03:21 +01001194 if (pci_resource_len(pdev, resno) == 0)
1195 return 0;
Libin64b00172013-04-15 02:48:54 +00001196 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001197 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -08001198 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001199 if (mmap_api == PCI_MMAP_PROCFS) {
1200 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1201 &pci_start, &pci_end);
1202 pci_start >>= PAGE_SHIFT;
1203 }
Martin Wilck3b519e42010-11-10 11:03:21 +01001204 if (start >= pci_start && start < pci_start + size &&
1205 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001206 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001207 return 0;
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/**
1211 * pci_mmap_resource - map a PCI resource into user memory space
1212 * @kobj: kobject for mapping
1213 * @attr: struct bin_attribute for the file being mapped
1214 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001215 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 *
1217 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001219static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1220 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Geliang Tang554a6032015-12-23 20:28:13 +08001222 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001223 int bar = (unsigned long)attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 enum pci_mmap_state mmap_type;
David Woodhousedca40b12017-04-12 13:25:57 +01001225 struct resource *res = &pdev->resource[bar];
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001226
Bjorn Helgaasca620722016-04-07 17:15:14 -07001227 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1228 return -EINVAL;
1229
David Woodhousedca40b12017-04-12 13:25:57 +01001230 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001231 WARN(1, "process \"%s\" tried to map 0x%08lx bytes at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
Martin Wilck3b519e42010-11-10 11:03:21 +01001232 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
David Woodhousedca40b12017-04-12 13:25:57 +01001233 pci_name(pdev), bar,
1234 (u64)pci_resource_start(pdev, bar),
1235 (u64)pci_resource_len(pdev, bar));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001236 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +01001237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
David Woodhousef7195822017-04-12 13:25:59 +01001239
1240 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001241}
1242
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001243static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1244 struct bin_attribute *attr,
1245 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001246{
1247 return pci_mmap_resource(kobj, attr, vma, 0);
1248}
1249
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001250static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1251 struct bin_attribute *attr,
1252 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001253{
1254 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001257static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1258 struct bin_attribute *attr, char *buf,
1259 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001260{
Geliang Tang554a6032015-12-23 20:28:13 +08001261 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001262 int bar = (unsigned long)attr->private;
Alex Williamson86333282010-07-19 09:45:34 -06001263 unsigned long port = off;
Alex Williamson86333282010-07-19 09:45:34 -06001264
David Woodhousedca40b12017-04-12 13:25:57 +01001265 port += pci_resource_start(pdev, bar);
Alex Williamson86333282010-07-19 09:45:34 -06001266
David Woodhousedca40b12017-04-12 13:25:57 +01001267 if (port > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001268 return 0;
1269
David Woodhousedca40b12017-04-12 13:25:57 +01001270 if (port + count - 1 > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001271 return -EINVAL;
1272
1273 switch (count) {
1274 case 1:
1275 if (write)
1276 outb(*(u8 *)buf, port);
1277 else
1278 *(u8 *)buf = inb(port);
1279 return 1;
1280 case 2:
1281 if (write)
1282 outw(*(u16 *)buf, port);
1283 else
1284 *(u16 *)buf = inw(port);
1285 return 2;
1286 case 4:
1287 if (write)
1288 outl(*(u32 *)buf, port);
1289 else
1290 *(u32 *)buf = inl(port);
1291 return 4;
1292 }
1293 return -EINVAL;
1294}
1295
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001296static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1297 struct bin_attribute *attr, char *buf,
1298 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001299{
1300 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1301}
1302
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001303static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1304 struct bin_attribute *attr, char *buf,
1305 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001306{
1307 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001311 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001312 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001313 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001314 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001315 * free their resources.
1316 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001317static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001318{
1319 int i;
1320
1321 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1322 struct bin_attribute *res_attr;
1323
1324 res_attr = pdev->res_attr[i];
1325 if (res_attr) {
1326 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1327 kfree(res_attr);
1328 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001329
1330 res_attr = pdev->res_attr_wc[i];
1331 if (res_attr) {
1332 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1333 kfree(res_attr);
1334 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001335 }
1336}
1337
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001338static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1339{
1340 /* allocate attribute structure, piggyback attribute name */
1341 int name_len = write_combine ? 13 : 10;
1342 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001343 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001344 int retval;
1345
1346 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001347 if (!res_attr)
1348 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001349
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001350 res_attr_name = (char *)(res_attr + 1);
1351
1352 sysfs_bin_attr_init(res_attr);
1353 if (write_combine) {
1354 pdev->res_attr_wc[num] = res_attr;
1355 sprintf(res_attr_name, "resource%d_wc", num);
1356 res_attr->mmap = pci_mmap_resource_wc;
1357 } else {
1358 pdev->res_attr[num] = res_attr;
1359 sprintf(res_attr_name, "resource%d", num);
David Woodhousee854d8b2017-04-12 13:25:56 +01001360 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1361 res_attr->read = pci_read_resource_io;
1362 res_attr->write = pci_write_resource_io;
1363 if (arch_can_pci_mmap_io())
1364 res_attr->mmap = pci_mmap_resource_uc;
1365 } else {
1366 res_attr->mmap = pci_mmap_resource_uc;
1367 }
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001368 }
1369 res_attr->attr.name = res_attr_name;
1370 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1371 res_attr->size = pci_resource_len(pdev, num);
David Woodhousedca40b12017-04-12 13:25:57 +01001372 res_attr->private = (void *)(unsigned long)num;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001373 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1374 if (retval)
1375 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001376
1377 return retval;
1378}
1379
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001380/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001382 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001384 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001386static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001389 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* Expose the PCI resources from this device as files */
1392 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* skip empty resources */
1395 if (!pci_resource_len(pdev, i))
1396 continue;
1397
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001398 retval = pci_create_attr(pdev, i, 0);
1399 /* for prefetchable resources, create a WC mappable file */
David Woodhouseae749c72017-04-12 13:25:54 +01001400 if (!retval && arch_can_pci_mmap_wc() &&
1401 pdev->resource[i].flags & IORESOURCE_PREFETCH)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001402 retval = pci_create_attr(pdev, i, 1);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001403 if (retval) {
1404 pci_remove_resource_files(pdev);
1405 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001411int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1412void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413#endif /* HAVE_PCI_MMAP */
1414
1415/**
1416 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001417 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001419 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * @buf: user input
1421 * @off: file offset
1422 * @count: number of byte in input
1423 *
1424 * writing anything except 0 enables it
1425 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001426static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1427 struct bin_attribute *bin_attr, char *buf,
1428 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Geliang Tang554a6032015-12-23 20:28:13 +08001430 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if ((off == 0) && (*buf == '0') && (count == 2))
1433 pdev->rom_attr_enabled = 0;
1434 else
1435 pdev->rom_attr_enabled = 1;
1436
1437 return count;
1438}
1439
1440/**
1441 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001442 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001444 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * @buf: where to put the data we read from the ROM
1446 * @off: file offset
1447 * @count: number of bytes to read
1448 *
1449 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1450 * device corresponding to @kobj.
1451 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001452static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1453 struct bin_attribute *bin_attr, char *buf,
1454 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Geliang Tang554a6032015-12-23 20:28:13 +08001456 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 void __iomem *rom;
1458 size_t size;
1459
1460 if (!pdev->rom_attr_enabled)
1461 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001464 if (!rom || !size)
1465 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (off >= size)
1468 count = 0;
1469 else {
1470 if (off + count > size)
1471 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 memcpy_fromio(buf, rom + off, count);
1474 }
1475 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return count;
1478}
1479
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301480static const struct bin_attribute pci_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .attr = {
1482 .name = "config",
1483 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001485 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 .read = pci_read_config,
1487 .write = pci_write_config,
1488};
1489
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301490static const struct bin_attribute pcie_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .attr = {
1492 .name = "config",
1493 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001495 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .read = pci_read_config,
1497 .write = pci_write_config,
1498};
1499
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001500static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1501 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001502{
1503 struct pci_dev *pdev = to_pci_dev(dev);
1504 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001505 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001506
1507 if (result < 0)
1508 return result;
1509
1510 if (val != 1)
1511 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001512
1513 result = pci_reset_function(pdev);
1514 if (result < 0)
1515 return result;
1516
1517 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001518}
1519
1520static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1521
Zhao, Yu280c73d2008-10-13 20:01:00 +08001522static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1523{
1524 int retval;
1525 struct bin_attribute *attr;
1526
1527 /* If the device has VPD, try to expose it in sysfs. */
1528 if (dev->vpd) {
1529 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1530 if (!attr)
1531 return -ENOMEM;
1532
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001533 sysfs_bin_attr_init(attr);
Hannes Reinecke104daa72016-02-15 09:42:01 +01001534 attr->size = 0;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001535 attr->attr.name = "vpd";
1536 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001537 attr->read = read_vpd_attr;
1538 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001539 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1540 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001541 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001542 return retval;
1543 }
1544 dev->vpd->attr = attr;
1545 }
1546
1547 /* Active State Power Management */
1548 pcie_aspm_create_sysfs_dev_files(dev);
1549
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001550 if (!pci_probe_reset_function(dev)) {
1551 retval = device_create_file(&dev->dev, &reset_attr);
1552 if (retval)
1553 goto error;
1554 dev->reset_fn = 1;
1555 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001556 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001557
1558error:
1559 pcie_aspm_remove_sysfs_dev_files(dev);
1560 if (dev->vpd && dev->vpd->attr) {
1561 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1562 kfree(dev->vpd->attr);
1563 }
1564
1565 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001566}
1567
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001568int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001570 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001571 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001572 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (!sysfs_initialized)
1575 return -EACCES;
1576
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001577 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001578 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001579 else
1580 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001581 if (retval)
1582 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001584 retval = pci_create_resource_files(pdev);
1585 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001586 goto err_config_file;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001589 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001590 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001591 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001592 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001593 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001594 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001596 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001597 attr->size = rom_size;
1598 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001599 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001600 attr->read = pci_read_rom;
1601 attr->write = pci_write_rom;
1602 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1603 if (retval) {
1604 kfree(attr);
1605 goto err_resource_files;
1606 }
1607 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001609
Zhao, Yu280c73d2008-10-13 20:01:00 +08001610 /* add sysfs entries for various capabilities */
1611 retval = pci_create_capabilities_sysfs(pdev);
1612 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001613 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001614
Narendra K911e1c92010-07-26 05:56:50 -05001615 pci_create_firmware_label_files(pdev);
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001618
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001619err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001620 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001621 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001622 kfree(pdev->rom_attr);
1623 pdev->rom_attr = NULL;
1624 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001625err_resource_files:
1626 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001627err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001628 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001629 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001630 else
1631 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001632err:
1633 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Zhao, Yu280c73d2008-10-13 20:01:00 +08001636static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1637{
1638 if (dev->vpd && dev->vpd->attr) {
1639 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1640 kfree(dev->vpd->attr);
1641 }
1642
1643 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001644 if (dev->reset_fn) {
1645 device_remove_file(&dev->dev, &reset_attr);
1646 dev->reset_fn = 0;
1647 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650/**
1651 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1652 * @pdev: device whose entries we should free
1653 *
1654 * Cleanup when @pdev is removed from sysfs.
1655 */
1656void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1657{
David Millerd67afe52006-11-10 12:27:48 -08001658 if (!sysfs_initialized)
1659 return;
1660
Zhao, Yu280c73d2008-10-13 20:01:00 +08001661 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001662
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001663 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001665 else
1666 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 pci_remove_resource_files(pdev);
1669
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001670 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001671 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1672 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001673 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Narendra K911e1c92010-07-26 05:56:50 -05001675
1676 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679static int __init pci_sysfs_init(void)
1680{
1681 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001682 int retval;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001685 for_each_pci_dev(pdev) {
1686 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001687 if (retval) {
1688 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001689 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001690 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 return 0;
1694}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001695late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001696
1697static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001698 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001699 NULL,
1700};
1701
1702static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001703 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001704{
Geliang Tang554a6032015-12-23 20:28:13 +08001705 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001706 struct pci_dev *pdev = to_pci_dev(dev);
1707
1708 if (a == &vga_attr.attr)
1709 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1710 return 0;
1711
Yinghai Lu4e15c462012-11-05 15:20:34 -05001712 return a->mode;
1713}
1714
Jiang Liudfab88b2013-05-31 12:21:31 +08001715static struct attribute *pci_dev_hp_attrs[] = {
1716 &dev_remove_attr.attr,
1717 &dev_rescan_attr.attr,
1718 NULL,
1719};
1720
1721static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001722 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001723{
Geliang Tang554a6032015-12-23 20:28:13 +08001724 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001725 struct pci_dev *pdev = to_pci_dev(dev);
1726
1727 if (pdev->is_virtfn)
1728 return 0;
1729
1730 return a->mode;
1731}
1732
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001733static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1734 struct attribute *a, int n)
1735{
1736 struct device *dev = kobj_to_dev(kobj);
1737 struct pci_dev *pdev = to_pci_dev(dev);
1738
1739 if (pci_is_bridge(pdev))
1740 return a->mode;
1741
1742 return 0;
1743}
1744
1745static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1746 struct attribute *a, int n)
1747{
1748 struct device *dev = kobj_to_dev(kobj);
1749 struct pci_dev *pdev = to_pci_dev(dev);
1750
1751 if (pci_is_pcie(pdev))
1752 return a->mode;
1753
1754 return 0;
1755}
1756
1757static const struct attribute_group pci_dev_group = {
1758 .attrs = pci_dev_attrs,
1759};
1760
1761const struct attribute_group *pci_dev_groups[] = {
1762 &pci_dev_group,
1763 NULL,
1764};
1765
1766static const struct attribute_group pci_bridge_group = {
1767 .attrs = pci_bridge_attrs,
1768};
1769
1770const struct attribute_group *pci_bridge_groups[] = {
1771 &pci_bridge_group,
1772 NULL,
1773};
1774
1775static const struct attribute_group pcie_dev_group = {
1776 .attrs = pcie_dev_attrs,
1777};
1778
1779const struct attribute_group *pcie_dev_groups[] = {
1780 &pcie_dev_group,
1781 NULL,
1782};
1783
Arvind Yadave7ea9822017-07-11 14:57:07 +05301784static const struct attribute_group pci_dev_hp_attr_group = {
Jiang Liudfab88b2013-05-31 12:21:31 +08001785 .attrs = pci_dev_hp_attrs,
1786 .is_visible = pci_dev_hp_attrs_are_visible,
1787};
1788
Donald Dutile17893822012-11-05 15:20:36 -05001789#ifdef CONFIG_PCI_IOV
1790static struct attribute *sriov_dev_attrs[] = {
1791 &sriov_totalvfs_attr.attr,
1792 &sriov_numvfs_attr.attr,
Filippo Sironi7dfca152017-10-09 01:09:11 +02001793 &sriov_offset_attr.attr,
1794 &sriov_stride_attr.attr,
1795 &sriov_vf_device_attr.attr,
Bodong Wang0e7df222017-04-13 01:51:40 +03001796 &sriov_drivers_autoprobe_attr.attr,
Donald Dutile17893822012-11-05 15:20:36 -05001797 NULL,
1798};
1799
1800static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001801 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001802{
Geliang Tang554a6032015-12-23 20:28:13 +08001803 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001804
1805 if (!dev_is_pf(dev))
1806 return 0;
1807
1808 return a->mode;
1809}
1810
Arvind Yadave7ea9822017-07-11 14:57:07 +05301811static const struct attribute_group sriov_dev_attr_group = {
Donald Dutile17893822012-11-05 15:20:36 -05001812 .attrs = sriov_dev_attrs,
1813 .is_visible = sriov_attrs_are_visible,
1814};
1815#endif /* CONFIG_PCI_IOV */
1816
Arvind Yadave7ea9822017-07-11 14:57:07 +05301817static const struct attribute_group pci_dev_attr_group = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001818 .attrs = pci_dev_dev_attrs,
1819 .is_visible = pci_dev_attrs_are_visible,
1820};
1821
Arvind Yadave7ea9822017-07-11 14:57:07 +05301822static const struct attribute_group pci_bridge_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001823 .attrs = pci_bridge_attrs,
1824 .is_visible = pci_bridge_attrs_are_visible,
1825};
1826
Arvind Yadave7ea9822017-07-11 14:57:07 +05301827static const struct attribute_group pcie_dev_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001828 .attrs = pcie_dev_attrs,
1829 .is_visible = pcie_dev_attrs_are_visible,
1830};
1831
Yinghai Lu4e15c462012-11-05 15:20:34 -05001832static const struct attribute_group *pci_dev_attr_groups[] = {
1833 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001834 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001835#ifdef CONFIG_PCI_IOV
1836 &sriov_dev_attr_group,
1837#endif
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001838 &pci_bridge_attr_group,
1839 &pcie_dev_attr_group,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001840 NULL,
1841};
1842
Bhumika Goyal69f2dc22017-09-12 16:43:33 +05301843const struct device_type pci_dev_type = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001844 .groups = pci_dev_attr_groups,
1845};