blob: 0c4653c1d2cec6f743a0bd650ead5c029ef299bc [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
4 * (C) Copyright 2002-2004 IBM Corp.
5 * (C) Copyright 2003 Matthew Wilcox
6 * (C) Copyright 2003 Hewlett-Packard
7 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
8 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
9 *
10 * File attributes for PCI devices
11 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -070012 * Modeled after usb's driverfs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070017#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pci.h>
19#include <linux/stat.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040020#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/topology.h>
22#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070023#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070024#include <linux/capability.h>
Chris Wrighta628e7b2011-02-14 17:21:49 -080025#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080026#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Matthew Garrett1a39b312012-04-16 16:26:02 -040028#include <linux/vgaarb.h>
Huang Ying448bd852012-06-23 10:23:51 +080029#include <linux/pm_runtime.h>
Sebastian Ottdfc73e72014-04-17 19:46:15 +020030#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "pci.h"
32
33static int sysfs_initialized; /* = 0 */
34
35/* show configuration fields */
36#define pci_config_attr(field, format_string) \
37static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040038field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{ \
40 struct pci_dev *pdev; \
41 \
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040042 pdev = to_pci_dev(dev); \
43 return sprintf(buf, format_string, pdev->field); \
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070044} \
45static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47pci_config_attr(vendor, "0x%04x\n");
48pci_config_attr(device, "0x%04x\n");
49pci_config_attr(subsystem_vendor, "0x%04x\n");
50pci_config_attr(subsystem_device, "0x%04x\n");
Emil Velikov702ed3b2016-11-21 16:24:49 -060051pci_config_attr(revision, "0x%02x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052pci_config_attr(class, "0x%06x\n");
53pci_config_attr(irq, "%u\n");
54
Doug Thompsonbdee9d92006-06-14 16:59:48 -070055static ssize_t broken_parity_status_show(struct device *dev,
56 struct device_attribute *attr,
57 char *buf)
58{
59 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040060 return sprintf(buf, "%u\n", pdev->broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070061}
62
63static ssize_t broken_parity_status_store(struct device *dev,
64 struct device_attribute *attr,
65 const char *buf, size_t count)
66{
67 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080068 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070069
Jingoo Han9a994e82013-06-01 16:25:25 +090070 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080071 return -EINVAL;
72
73 pdev->broken_parity_status = !!val;
74
75 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070076}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070077static DEVICE_ATTR_RW(broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070078
Sudeep Holla5aaba362014-09-30 14:48:22 +010079static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040080 struct device_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070081{
Mike Travis3be83052009-01-04 05:18:01 -080082 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070083
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020084#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053085 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
86 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020087#else
Mike Travis3be83052009-01-04 05:18:01 -080088 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020089#endif
Sudeep Holla5aaba362014-09-30 14:48:22 +010090 return cpumap_print_to_pagebuf(list, buf, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Yijing Wangc489f5f2013-09-30 15:02:38 +080093static ssize_t local_cpus_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040094 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +080095{
Sudeep Holla5aaba362014-09-30 14:48:22 +010096 return pci_dev_show_local_cpu(dev, false, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +080097}
Bjorn Helgaas33de1b82013-10-31 14:12:40 -060098static DEVICE_ATTR_RO(local_cpus);
Yijing Wangc489f5f2013-09-30 15:02:38 +080099
100static ssize_t local_cpulist_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400101 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +0800102{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100103 return pci_dev_show_local_cpu(dev, true, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800104}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700105static DEVICE_ATTR_RO(local_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700107/*
108 * PCI Bus Class Devices
109 */
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700110static ssize_t cpuaffinity_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700112{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100113 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
114
115 return cpumap_print_to_pagebuf(false, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700116}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700117static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700118
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700119static ssize_t cpulistaffinity_show(struct device *dev,
120 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700121{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100122 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
123
124 return cpumap_print_to_pagebuf(true, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700125}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700126static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* show resources */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400129static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
130 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400132 struct pci_dev *pci_dev = to_pci_dev(dev);
133 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800135 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700136 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (pci_dev->subordinate)
139 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800140 else
141 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000144 struct resource *res = &pci_dev->resource[i];
145 pci_resource_to_user(pci_dev, i, res, &start, &end);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400146 str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000147 (unsigned long long)start,
148 (unsigned long long)end,
149 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 return (str - buf);
152}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700153static DEVICE_ATTR_RO(resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800155static ssize_t max_link_speed_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
157{
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500158 struct pci_dev *pdev = to_pci_dev(dev);
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800159
Tal Gilboa6cf57be2018-03-30 07:44:05 -0500160 return sprintf(buf, "%s\n", PCIE_SPEED2STR(pcie_get_speed_cap(pdev)));
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800161}
162static DEVICE_ATTR_RO(max_link_speed);
163
164static ssize_t max_link_width_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
Tal Gilboac70b65f2018-03-30 08:24:36 -0500167 struct pci_dev *pdev = to_pci_dev(dev);
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800168
Tal Gilboac70b65f2018-03-30 08:24:36 -0500169 return sprintf(buf, "%u\n", pcie_get_width_cap(pdev));
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800170}
171static DEVICE_ATTR_RO(max_link_width);
172
173static ssize_t current_link_speed_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
176 struct pci_dev *pci_dev = to_pci_dev(dev);
177 u16 linkstat;
178 int err;
179 const char *speed;
180
181 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
182 if (err)
183 return -EINVAL;
184
185 switch (linkstat & PCI_EXP_LNKSTA_CLS) {
Jay Fang1acfb9b2018-03-12 17:13:32 +0800186 case PCI_EXP_LNKSTA_CLS_16_0GB:
187 speed = "16 GT/s";
188 break;
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800189 case PCI_EXP_LNKSTA_CLS_8_0GB:
190 speed = "8 GT/s";
191 break;
192 case PCI_EXP_LNKSTA_CLS_5_0GB:
193 speed = "5 GT/s";
194 break;
195 case PCI_EXP_LNKSTA_CLS_2_5GB:
196 speed = "2.5 GT/s";
197 break;
198 default:
199 speed = "Unknown speed";
200 }
201
202 return sprintf(buf, "%s\n", speed);
203}
204static DEVICE_ATTR_RO(current_link_speed);
205
206static ssize_t current_link_width_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
213 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
214 if (err)
215 return -EINVAL;
216
217 return sprintf(buf, "%u\n",
218 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
219}
220static DEVICE_ATTR_RO(current_link_width);
221
222static ssize_t secondary_bus_number_show(struct device *dev,
223 struct device_attribute *attr,
224 char *buf)
225{
226 struct pci_dev *pci_dev = to_pci_dev(dev);
227 u8 sec_bus;
228 int err;
229
230 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
231 if (err)
232 return -EINVAL;
233
234 return sprintf(buf, "%u\n", sec_bus);
235}
236static DEVICE_ATTR_RO(secondary_bus_number);
237
238static ssize_t subordinate_bus_number_show(struct device *dev,
239 struct device_attribute *attr,
240 char *buf)
241{
242 struct pci_dev *pci_dev = to_pci_dev(dev);
243 u8 sub_bus;
244 int err;
245
246 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
247 if (err)
248 return -EINVAL;
249
250 return sprintf(buf, "%u\n", sub_bus);
251}
252static DEVICE_ATTR_RO(subordinate_bus_number);
253
Stuart Hayes0077a842018-01-05 12:51:55 -0600254static ssize_t ari_enabled_show(struct device *dev,
255 struct device_attribute *attr,
256 char *buf)
257{
258 struct pci_dev *pci_dev = to_pci_dev(dev);
259
260 return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
261}
262static DEVICE_ATTR_RO(ari_enabled);
263
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400264static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
265 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700266{
267 struct pci_dev *pci_dev = to_pci_dev(dev);
268
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200269 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700270 pci_dev->vendor, pci_dev->device,
271 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
272 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
273 (u8)(pci_dev->class));
274}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700275static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800276
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700277static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400278 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200279{
280 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800281 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900282 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800283
284 if (result < 0)
285 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200286
287 /* this can crash the machine when done on the "wrong" device */
288 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800289 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200290
Christoph Hellwig6f5cdfa2018-05-18 18:56:24 +0200291 device_lock(dev);
292 if (dev->driver)
293 result = -EBUSY;
294 else if (val)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800295 result = pci_enable_device(pdev);
Christoph Hellwig6f5cdfa2018-05-18 18:56:24 +0200296 else if (pci_is_enabled(pdev))
297 pci_disable_device(pdev);
298 else
299 result = -EIO;
300 device_unlock(dev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200301
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800302 return result < 0 ? result : count;
303}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200304
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700305static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400306 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800307{
308 struct pci_dev *pdev;
309
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400310 pdev = to_pci_dev(dev);
311 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200312}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700313static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200314
Brice Goglin81bb0e12007-01-28 10:53:40 +0100315#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400316static ssize_t numa_node_store(struct device *dev,
317 struct device_attribute *attr, const char *buf,
318 size_t count)
319{
320 struct pci_dev *pdev = to_pci_dev(dev);
321 int node, ret;
322
323 if (!capable(CAP_SYS_ADMIN))
324 return -EPERM;
325
326 ret = kstrtoint(buf, 0, &node);
327 if (ret)
328 return ret;
329
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100330 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
331 return -EINVAL;
332
333 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400334 return -EINVAL;
335
336 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600337 pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
Prarit Bhargava63692df2014-10-23 14:22:12 -0400338 node);
339
340 dev->numa_node = node;
341 return count;
342}
343
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400344static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
345 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100346{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400347 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100348}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400349static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100350#endif
351
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400352static ssize_t dma_mask_bits_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800354{
355 struct pci_dev *pdev = to_pci_dev(dev);
356
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400357 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800358}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700359static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800360
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400361static ssize_t consistent_dma_mask_bits_show(struct device *dev,
362 struct device_attribute *attr,
363 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800364{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400365 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800366}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700367static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800368
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400369static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
370 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400371{
372 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800373 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400374
Yijing Wang468ff152014-09-23 13:27:24 +0800375 return sprintf(buf, "%u\n", subordinate ?
376 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
377 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400378}
379
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400380static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
381 const char *buf, size_t count)
Brice Goglinfe970642006-08-31 01:55:15 -0400382{
383 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800384 struct pci_bus *subordinate = pdev->subordinate;
Trent Piepho92425a42008-11-30 17:10:12 -0800385 unsigned long val;
386
Jingoo Han9a994e82013-06-01 16:25:25 +0900387 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800388 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400389
Brice Goglinfe970642006-08-31 01:55:15 -0400390 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800391 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400392
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700393 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800394 * "no_msi" and "bus_flags" only affect what happens when a driver
395 * requests MSI or MSI-X. They don't affect any drivers that have
396 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700397 */
Yijing Wang468ff152014-09-23 13:27:24 +0800398 if (!subordinate) {
399 pdev->no_msi = !val;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600400 pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
Yijing Wang468ff152014-09-23 13:27:24 +0800401 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400402 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400403 }
404
Yijing Wang468ff152014-09-23 13:27:24 +0800405 if (val)
406 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
407 else
408 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
409
410 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
411 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400412 return count;
413}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700414static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700415
Alex Chiang705b1aa2009-03-20 14:56:31 -0600416static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
417 size_t count)
418{
419 unsigned long val;
420 struct pci_bus *b = NULL;
421
Jingoo Han9a994e82013-06-01 16:25:25 +0900422 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600423 return -EINVAL;
424
425 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100426 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600427 while ((b = pci_find_next_bus(b)) != NULL)
428 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100429 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600430 }
431 return count;
432}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600433static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600434
Sachin Kamatbf22c902013-09-28 15:42:00 +0530435static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600436 &bus_attr_rescan.attr,
437 NULL,
438};
439
440static const struct attribute_group pci_bus_group = {
441 .attrs = pci_bus_attrs,
442};
443
444const struct attribute_group *pci_bus_groups[] = {
445 &pci_bus_group,
446 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600447};
Alex Chiang77c27c72009-03-20 14:56:36 -0600448
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400449static ssize_t dev_rescan_store(struct device *dev,
450 struct device_attribute *attr, const char *buf,
451 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600452{
453 unsigned long val;
454 struct pci_dev *pdev = to_pci_dev(dev);
455
Jingoo Han9a994e82013-06-01 16:25:25 +0900456 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600457 return -EINVAL;
458
459 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100460 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600461 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100462 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600463 }
464 return count;
465}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530466static struct device_attribute dev_rescan_attr = __ATTR(rescan,
467 (S_IWUSR|S_IWGRP),
468 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600469
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400470static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
471 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600472{
Alex Chiang77c27c72009-03-20 14:56:36 -0600473 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600474
Jingoo Han9a994e82013-06-01 16:25:25 +0900475 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600476 return -EINVAL;
477
Tejun Heobc6caf02014-02-03 14:03:02 -0500478 if (val && device_remove_file_self(dev, attr))
479 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600480 return count;
481}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530482static struct device_attribute dev_remove_attr = __ATTR(remove,
483 (S_IWUSR|S_IWGRP),
484 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700485
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400486static ssize_t dev_bus_rescan_store(struct device *dev,
487 struct device_attribute *attr,
488 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700489{
490 unsigned long val;
491 struct pci_bus *bus = to_pci_bus(dev);
492
Jingoo Han9a994e82013-06-01 16:25:25 +0900493 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700494 return -EINVAL;
495
496 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100497 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800498 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
499 pci_rescan_bus_bridge_resize(bus->self);
500 else
501 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100502 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700503 }
504 return count;
505}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700506static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700507
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100508#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800509static ssize_t d3cold_allowed_store(struct device *dev,
510 struct device_attribute *attr,
511 const char *buf, size_t count)
512{
513 struct pci_dev *pdev = to_pci_dev(dev);
514 unsigned long val;
515
Jingoo Han9a994e82013-06-01 16:25:25 +0900516 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800517 return -EINVAL;
518
519 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300520 if (pdev->d3cold_allowed)
521 pci_d3cold_enable(pdev);
522 else
523 pci_d3cold_disable(pdev);
524
Huang Ying448bd852012-06-23 10:23:51 +0800525 pm_runtime_resume(dev);
526
527 return count;
528}
529
530static ssize_t d3cold_allowed_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400534 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800535}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700536static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800537#endif
538
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200539#ifdef CONFIG_OF
540static ssize_t devspec_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
543 struct pci_dev *pdev = to_pci_dev(dev);
544 struct device_node *np = pci_device_to_OF_node(pdev);
545
Rob Herringb63773a2017-07-18 16:43:21 -0500546 if (np == NULL)
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200547 return 0;
Rob Herringb63773a2017-07-18 16:43:21 -0500548 return sprintf(buf, "%pOF", np);
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200549}
550static DEVICE_ATTR_RO(devspec);
551#endif
552
Donald Dutile17893822012-11-05 15:20:36 -0500553#ifdef CONFIG_PCI_IOV
554static ssize_t sriov_totalvfs_show(struct device *dev,
555 struct device_attribute *attr,
556 char *buf)
557{
558 struct pci_dev *pdev = to_pci_dev(dev);
559
Donald Dutilebff73152012-11-05 15:20:37 -0500560 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500561}
562
563
564static ssize_t sriov_numvfs_show(struct device *dev,
565 struct device_attribute *attr,
566 char *buf)
567{
568 struct pci_dev *pdev = to_pci_dev(dev);
569
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700570 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500571}
572
573/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700574 * num_vfs > 0; number of VFs to enable
575 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500576 *
577 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700578 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500579 */
580static ssize_t sriov_numvfs_store(struct device *dev,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
583{
584 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700585 int ret;
586 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500587
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700588 ret = kstrtou16(buf, 0, &num_vfs);
589 if (ret < 0)
590 return ret;
591
592 if (num_vfs > pci_sriov_get_totalvfs(pdev))
593 return -ERANGE;
594
Jakub Kicinski17530e72017-05-22 15:50:23 -0700595 device_lock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800596
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700597 if (num_vfs == pdev->sriov->num_VFs)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800598 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500599
600 /* is PF driver loaded w/callback */
601 if (!pdev->driver || !pdev->driver->sriov_configure) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600602 pci_info(pdev, "Driver doesn't support SRIOV configuration via sysfs\n");
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800603 ret = -ENOENT;
604 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500605 }
606
Donald Dutile17893822012-11-05 15:20:36 -0500607 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700608 /* disable VFs */
609 ret = pdev->driver->sriov_configure(pdev, 0);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800610 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500611 }
612
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700613 /* enable VFs */
614 if (pdev->sriov->num_VFs) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600615 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700616 pdev->sriov->num_VFs, num_vfs);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800617 ret = -EBUSY;
618 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700619 }
Donald Dutile17893822012-11-05 15:20:36 -0500620
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700621 ret = pdev->driver->sriov_configure(pdev, num_vfs);
622 if (ret < 0)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800623 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700624
625 if (ret != num_vfs)
Frederick Lawler7506dc72018-01-18 12:55:24 -0600626 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700627 num_vfs, ret);
628
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800629exit:
Jakub Kicinski17530e72017-05-22 15:50:23 -0700630 device_unlock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800631
632 if (ret < 0)
633 return ret;
634
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700635 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500636}
637
Filippo Sironi7dfca152017-10-09 01:09:11 +0200638static ssize_t sriov_offset_show(struct device *dev,
639 struct device_attribute *attr,
640 char *buf)
641{
642 struct pci_dev *pdev = to_pci_dev(dev);
643
644 return sprintf(buf, "%u\n", pdev->sriov->offset);
645}
646
647static ssize_t sriov_stride_show(struct device *dev,
648 struct device_attribute *attr,
649 char *buf)
650{
651 struct pci_dev *pdev = to_pci_dev(dev);
652
653 return sprintf(buf, "%u\n", pdev->sriov->stride);
654}
655
656static ssize_t sriov_vf_device_show(struct device *dev,
657 struct device_attribute *attr,
658 char *buf)
659{
660 struct pci_dev *pdev = to_pci_dev(dev);
661
662 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
663}
664
Bodong Wang0e7df222017-04-13 01:51:40 +0300665static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
666 struct device_attribute *attr,
667 char *buf)
668{
669 struct pci_dev *pdev = to_pci_dev(dev);
670
671 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
672}
673
674static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
675 struct device_attribute *attr,
676 const char *buf, size_t count)
677{
678 struct pci_dev *pdev = to_pci_dev(dev);
679 bool drivers_autoprobe;
680
681 if (kstrtobool(buf, &drivers_autoprobe) < 0)
682 return -EINVAL;
683
684 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
685
686 return count;
687}
688
Donald Dutile17893822012-11-05 15:20:36 -0500689static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
690static struct device_attribute sriov_numvfs_attr =
691 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
692 sriov_numvfs_show, sriov_numvfs_store);
Filippo Sironi7dfca152017-10-09 01:09:11 +0200693static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
694static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
695static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
Bodong Wang0e7df222017-04-13 01:51:40 +0300696static struct device_attribute sriov_drivers_autoprobe_attr =
697 __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
698 sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
Donald Dutile17893822012-11-05 15:20:36 -0500699#endif /* CONFIG_PCI_IOV */
700
Alex Williamson782a9852014-05-20 08:53:21 -0600701static ssize_t driver_override_store(struct device *dev,
702 struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200706 char *driver_override, *old, *cp;
Alex Williamson782a9852014-05-20 08:53:21 -0600707
Sasha Levin4efe8742015-02-04 17:38:15 -0500708 /* We need to keep extra room for a newline */
709 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600710 return -EINVAL;
711
712 driver_override = kstrndup(buf, count, GFP_KERNEL);
713 if (!driver_override)
714 return -ENOMEM;
715
716 cp = strchr(driver_override, '\n');
717 if (cp)
718 *cp = '\0';
719
Nicolai Stange95614752017-09-11 09:45:40 +0200720 device_lock(dev);
721 old = pdev->driver_override;
Alex Williamson782a9852014-05-20 08:53:21 -0600722 if (strlen(driver_override)) {
723 pdev->driver_override = driver_override;
724 } else {
725 kfree(driver_override);
726 pdev->driver_override = NULL;
727 }
Nicolai Stange95614752017-09-11 09:45:40 +0200728 device_unlock(dev);
Alex Williamson782a9852014-05-20 08:53:21 -0600729
730 kfree(old);
731
732 return count;
733}
734
735static ssize_t driver_override_show(struct device *dev,
736 struct device_attribute *attr, char *buf)
737{
738 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200739 ssize_t len;
Alex Williamson782a9852014-05-20 08:53:21 -0600740
Nicolai Stange95614752017-09-11 09:45:40 +0200741 device_lock(dev);
742 len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
743 device_unlock(dev);
744 return len;
Alex Williamson782a9852014-05-20 08:53:21 -0600745}
746static DEVICE_ATTR_RW(driver_override);
747
Sachin Kamatbf22c902013-09-28 15:42:00 +0530748static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700749 &dev_attr_resource.attr,
750 &dev_attr_vendor.attr,
751 &dev_attr_device.attr,
752 &dev_attr_subsystem_vendor.attr,
753 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600754 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700755 &dev_attr_class.attr,
756 &dev_attr_irq.attr,
757 &dev_attr_local_cpus.attr,
758 &dev_attr_local_cpulist.attr,
759 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100760#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700761 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100762#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700763 &dev_attr_dma_mask_bits.attr,
764 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700765 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700766 &dev_attr_broken_parity_status.attr,
767 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100768#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700769 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800770#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200771#ifdef CONFIG_OF
772 &dev_attr_devspec.attr,
773#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600774 &dev_attr_driver_override.attr,
Stuart Hayes0077a842018-01-05 12:51:55 -0600775 &dev_attr_ari_enabled.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700776 NULL,
777};
778
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800779static struct attribute *pci_bridge_attrs[] = {
780 &dev_attr_subordinate_bus_number.attr,
781 &dev_attr_secondary_bus_number.attr,
782 NULL,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700783};
784
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800785static struct attribute *pcie_dev_attrs[] = {
786 &dev_attr_current_link_speed.attr,
787 &dev_attr_current_link_width.attr,
788 &dev_attr_max_link_width.attr,
789 &dev_attr_max_link_speed.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700790 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
792
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700793static struct attribute *pcibus_attrs[] = {
794 &dev_attr_rescan.attr,
795 &dev_attr_cpuaffinity.attr,
796 &dev_attr_cpulistaffinity.attr,
797 NULL,
798};
799
800static const struct attribute_group pcibus_group = {
801 .attrs = pcibus_attrs,
802};
803
804const struct attribute_group *pcibus_groups[] = {
805 &pcibus_group,
806 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700807};
808
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400809static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
810 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000811{
812 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400813 struct pci_dev *vga_dev = vga_default_device();
814
815 if (vga_dev)
816 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000817
818 return sprintf(buf, "%u\n",
819 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
820 IORESOURCE_ROM_SHADOW));
821}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530822static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000823
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400824static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
825 struct bin_attribute *bin_attr, char *buf,
826 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Geliang Tang554a6032015-12-23 20:28:13 +0800828 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 unsigned int size = 64;
830 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400831 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700834 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400836 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (off > size)
840 return 0;
841 if (off + count > size) {
842 size -= off;
843 count = size;
844 } else {
845 size = count;
846 }
847
Huang Ying3d8387e2012-08-15 09:43:03 +0800848 pci_config_pm_runtime_get(dev);
849
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900850 if ((off & 1) && size) {
851 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700852 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900853 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900855 size--;
856 }
857
858 if ((off & 3) && size > 2) {
859 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700860 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900861 data[off - init_off] = val & 0xff;
862 data[off - init_off + 1] = (val >> 8) & 0xff;
863 off += 2;
864 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900868 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700869 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900870 data[off - init_off] = val & 0xff;
871 data[off - init_off + 1] = (val >> 8) & 0xff;
872 data[off - init_off + 2] = (val >> 16) & 0xff;
873 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 off += 4;
875 size -= 4;
876 }
877
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900878 if (size >= 2) {
879 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700880 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900881 data[off - init_off] = val & 0xff;
882 data[off - init_off + 1] = (val >> 8) & 0xff;
883 off += 2;
884 size -= 2;
885 }
886
887 if (size > 0) {
888 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700889 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900890 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 off++;
892 --size;
893 }
894
Huang Ying3d8387e2012-08-15 09:43:03 +0800895 pci_config_pm_runtime_put(dev);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return count;
898}
899
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400900static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
901 struct bin_attribute *bin_attr, char *buf,
902 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Geliang Tang554a6032015-12-23 20:28:13 +0800904 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned int size = count;
906 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400907 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (off > dev->cfg_size)
910 return 0;
911 if (off + count > dev->cfg_size) {
912 size = dev->cfg_size - off;
913 count = size;
914 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700915
Huang Ying3d8387e2012-08-15 09:43:03 +0800916 pci_config_pm_runtime_get(dev);
917
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900918 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700919 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900921 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700923
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900924 if ((off & 3) && size > 2) {
925 u16 val = data[off - init_off];
926 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400927 pci_user_write_config_word(dev, off, val);
928 off += 2;
929 size -= 2;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900933 u32 val = data[off - init_off];
934 val |= (u32) data[off - init_off + 1] << 8;
935 val |= (u32) data[off - init_off + 2] << 16;
936 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700937 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 off += 4;
939 size -= 4;
940 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700941
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900942 if (size >= 2) {
943 u16 val = data[off - init_off];
944 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700945 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900946 off += 2;
947 size -= 2;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900950 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700951 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 off++;
953 --size;
954 }
955
Huang Ying3d8387e2012-08-15 09:43:03 +0800956 pci_config_pm_runtime_put(dev);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return count;
959}
960
961#ifdef HAVE_PCI_LEGACY
962/**
963 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700964 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700966 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * @buf: buffer to store results
968 * @off: offset into legacy I/O port space
969 * @count: number of bytes to read
970 *
971 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
972 * callback routine (pci_legacy_read).
973 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400974static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
975 struct bin_attribute *bin_attr, char *buf,
976 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Geliang Tang554a6032015-12-23 20:28:13 +0800978 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400980 /* Only support 1, 2 or 4 byte accesses */
981 if (count != 1 && count != 2 && count != 4)
982 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400984 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987/**
988 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700989 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700991 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * @buf: buffer containing value to be written
993 * @off: offset into legacy I/O port space
994 * @count: number of bytes to write
995 *
996 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
997 * callback routine (pci_legacy_write).
998 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400999static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1000 struct bin_attribute *bin_attr, char *buf,
1001 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Geliang Tang554a6032015-12-23 20:28:13 +08001003 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001005 /* Only support 1, 2 or 4 byte accesses */
1006 if (count != 1 && count != 2 && count != 4)
1007 return -EINVAL;
1008
1009 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012/**
1013 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001014 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * @kobj: kobject corresponding to device to be mapped
1016 * @attr: struct bin_attribute for this file
1017 * @vma: struct vm_area_struct passed to mmap
1018 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001019 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * legacy memory space (first meg of bus space) into application virtual
1021 * memory space.
1022 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001023static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1024 struct bin_attribute *attr,
1025 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Geliang Tang554a6032015-12-23 20:28:13 +08001027 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001029 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001030}
1031
1032/**
1033 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001034 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001035 * @kobj: kobject corresponding to device to be mapped
1036 * @attr: struct bin_attribute for this file
1037 * @vma: struct vm_area_struct passed to mmap
1038 *
1039 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1040 * legacy IO space (first meg of bus space) into application virtual
1041 * memory space. Returns -ENOSYS if the operation isn't supported
1042 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001043static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1044 struct bin_attribute *attr,
1045 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001046{
Geliang Tang554a6032015-12-23 20:28:13 +08001047 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001048
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001049 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001050}
1051
1052/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001053 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1054 * @b: bus to create files under
1055 * @mmap_type: I/O port or memory
1056 *
1057 * Stub implementation. Can be overridden by arch if necessary.
1058 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001059void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1060 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001061{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001062}
1063
1064/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001065 * pci_create_legacy_files - create legacy I/O port and memory files
1066 * @b: bus to create files under
1067 *
1068 * Some platforms allow access to legacy I/O port and ISA memory space on
1069 * a per-bus basis. This routine creates the files and ties them into
1070 * their associated read, write and mmap files from pci-sysfs.c
1071 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001072 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001073 * as it is ok to set up the PCI bus without these files.
1074 */
1075void pci_create_legacy_files(struct pci_bus *b)
1076{
1077 int error;
1078
Kees Cook6396bb22018-06-12 14:03:40 -07001079 b->legacy_io = kcalloc(2, sizeof(struct bin_attribute),
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001080 GFP_ATOMIC);
1081 if (!b->legacy_io)
1082 goto kzalloc_err;
1083
Stephen Rothwell62e877b82010-03-01 20:38:36 +11001084 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001085 b->legacy_io->attr.name = "legacy_io";
1086 b->legacy_io->size = 0xffff;
1087 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1088 b->legacy_io->read = pci_read_legacy_io;
1089 b->legacy_io->write = pci_write_legacy_io;
1090 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001091 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001092 error = device_create_bin_file(&b->dev, b->legacy_io);
1093 if (error)
1094 goto legacy_io_err;
1095
1096 /* Allocated above after the legacy_io struct */
1097 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +00001098 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001099 b->legacy_mem->attr.name = "legacy_mem";
1100 b->legacy_mem->size = 1024*1024;
1101 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1102 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001103 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001104 error = device_create_bin_file(&b->dev, b->legacy_mem);
1105 if (error)
1106 goto legacy_mem_err;
1107
1108 return;
1109
1110legacy_mem_err:
1111 device_remove_bin_file(&b->dev, b->legacy_io);
1112legacy_io_err:
1113 kfree(b->legacy_io);
1114 b->legacy_io = NULL;
1115kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001116 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 +10001117 return;
1118}
1119
1120void pci_remove_legacy_files(struct pci_bus *b)
1121{
1122 if (b->legacy_io) {
1123 device_remove_bin_file(&b->dev, b->legacy_io);
1124 device_remove_bin_file(&b->dev, b->legacy_mem);
1125 kfree(b->legacy_io); /* both are allocated here */
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128#endif /* HAVE_PCI_LEGACY */
1129
David Woodhousef7195822017-04-12 13:25:59 +01001130#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001131
Martin Wilck3b519e42010-11-10 11:03:21 +01001132int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1133 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001134{
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001135 unsigned long nr, start, size;
1136 resource_size_t pci_start = 0, pci_end;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001137
Martin Wilck3b519e42010-11-10 11:03:21 +01001138 if (pci_resource_len(pdev, resno) == 0)
1139 return 0;
Libin64b00172013-04-15 02:48:54 +00001140 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001141 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -08001142 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001143 if (mmap_api == PCI_MMAP_PROCFS) {
1144 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1145 &pci_start, &pci_end);
1146 pci_start >>= PAGE_SHIFT;
1147 }
Martin Wilck3b519e42010-11-10 11:03:21 +01001148 if (start >= pci_start && start < pci_start + size &&
1149 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001150 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001151 return 0;
1152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/**
1155 * pci_mmap_resource - map a PCI resource into user memory space
1156 * @kobj: kobject for mapping
1157 * @attr: struct bin_attribute for the file being mapped
1158 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001159 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 *
1161 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001163static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1164 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Geliang Tang554a6032015-12-23 20:28:13 +08001166 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001167 int bar = (unsigned long)attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 enum pci_mmap_state mmap_type;
David Woodhousedca40b12017-04-12 13:25:57 +01001169 struct resource *res = &pdev->resource[bar];
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001170
Bjorn Helgaasca620722016-04-07 17:15:14 -07001171 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1172 return -EINVAL;
1173
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001174 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001175 return -EINVAL;
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
David Woodhousef7195822017-04-12 13:25:59 +01001178
1179 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001180}
1181
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001182static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1183 struct bin_attribute *attr,
1184 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001185{
1186 return pci_mmap_resource(kobj, attr, vma, 0);
1187}
1188
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001189static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1190 struct bin_attribute *attr,
1191 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001192{
1193 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001196static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1197 struct bin_attribute *attr, char *buf,
1198 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001199{
Geliang Tang554a6032015-12-23 20:28:13 +08001200 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001201 int bar = (unsigned long)attr->private;
Alex Williamson86333282010-07-19 09:45:34 -06001202 unsigned long port = off;
Alex Williamson86333282010-07-19 09:45:34 -06001203
David Woodhousedca40b12017-04-12 13:25:57 +01001204 port += pci_resource_start(pdev, bar);
Alex Williamson86333282010-07-19 09:45:34 -06001205
David Woodhousedca40b12017-04-12 13:25:57 +01001206 if (port > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001207 return 0;
1208
David Woodhousedca40b12017-04-12 13:25:57 +01001209 if (port + count - 1 > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001210 return -EINVAL;
1211
1212 switch (count) {
1213 case 1:
1214 if (write)
1215 outb(*(u8 *)buf, port);
1216 else
1217 *(u8 *)buf = inb(port);
1218 return 1;
1219 case 2:
1220 if (write)
1221 outw(*(u16 *)buf, port);
1222 else
1223 *(u16 *)buf = inw(port);
1224 return 2;
1225 case 4:
1226 if (write)
1227 outl(*(u32 *)buf, port);
1228 else
1229 *(u32 *)buf = inl(port);
1230 return 4;
1231 }
1232 return -EINVAL;
1233}
1234
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001235static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1236 struct bin_attribute *attr, char *buf,
1237 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001238{
1239 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1240}
1241
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001242static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1243 struct bin_attribute *attr, char *buf,
1244 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001245{
1246 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001250 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001251 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001252 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001253 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001254 * free their resources.
1255 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001256static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001257{
1258 int i;
1259
1260 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1261 struct bin_attribute *res_attr;
1262
1263 res_attr = pdev->res_attr[i];
1264 if (res_attr) {
1265 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1266 kfree(res_attr);
1267 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001268
1269 res_attr = pdev->res_attr_wc[i];
1270 if (res_attr) {
1271 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1272 kfree(res_attr);
1273 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001274 }
1275}
1276
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001277static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1278{
1279 /* allocate attribute structure, piggyback attribute name */
1280 int name_len = write_combine ? 13 : 10;
1281 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001282 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001283 int retval;
1284
1285 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001286 if (!res_attr)
1287 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001288
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001289 res_attr_name = (char *)(res_attr + 1);
1290
1291 sysfs_bin_attr_init(res_attr);
1292 if (write_combine) {
1293 pdev->res_attr_wc[num] = res_attr;
1294 sprintf(res_attr_name, "resource%d_wc", num);
1295 res_attr->mmap = pci_mmap_resource_wc;
1296 } else {
1297 pdev->res_attr[num] = res_attr;
1298 sprintf(res_attr_name, "resource%d", num);
David Woodhousee854d8b2017-04-12 13:25:56 +01001299 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1300 res_attr->read = pci_read_resource_io;
1301 res_attr->write = pci_write_resource_io;
1302 if (arch_can_pci_mmap_io())
1303 res_attr->mmap = pci_mmap_resource_uc;
1304 } else {
1305 res_attr->mmap = pci_mmap_resource_uc;
1306 }
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001307 }
1308 res_attr->attr.name = res_attr_name;
1309 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1310 res_attr->size = pci_resource_len(pdev, num);
David Woodhousedca40b12017-04-12 13:25:57 +01001311 res_attr->private = (void *)(unsigned long)num;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001312 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1313 if (retval)
1314 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001315
1316 return retval;
1317}
1318
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001319/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001321 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001323 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001325static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001328 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Expose the PCI resources from this device as files */
1331 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* skip empty resources */
1334 if (!pci_resource_len(pdev, i))
1335 continue;
1336
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001337 retval = pci_create_attr(pdev, i, 0);
1338 /* for prefetchable resources, create a WC mappable file */
David Woodhouseae749c72017-04-12 13:25:54 +01001339 if (!retval && arch_can_pci_mmap_wc() &&
1340 pdev->resource[i].flags & IORESOURCE_PREFETCH)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001341 retval = pci_create_attr(pdev, i, 1);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001342 if (retval) {
1343 pci_remove_resource_files(pdev);
1344 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001350int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1351void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352#endif /* HAVE_PCI_MMAP */
1353
1354/**
1355 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001356 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001358 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * @buf: user input
1360 * @off: file offset
1361 * @count: number of byte in input
1362 *
1363 * writing anything except 0 enables it
1364 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001365static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1366 struct bin_attribute *bin_attr, char *buf,
1367 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Geliang Tang554a6032015-12-23 20:28:13 +08001369 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if ((off == 0) && (*buf == '0') && (count == 2))
1372 pdev->rom_attr_enabled = 0;
1373 else
1374 pdev->rom_attr_enabled = 1;
1375
1376 return count;
1377}
1378
1379/**
1380 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001381 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001383 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * @buf: where to put the data we read from the ROM
1385 * @off: file offset
1386 * @count: number of bytes to read
1387 *
1388 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1389 * device corresponding to @kobj.
1390 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001391static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1392 struct bin_attribute *bin_attr, char *buf,
1393 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Geliang Tang554a6032015-12-23 20:28:13 +08001395 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 void __iomem *rom;
1397 size_t size;
1398
1399 if (!pdev->rom_attr_enabled)
1400 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001403 if (!rom || !size)
1404 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (off >= size)
1407 count = 0;
1408 else {
1409 if (off + count > size)
1410 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 memcpy_fromio(buf, rom + off, count);
1413 }
1414 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return count;
1417}
1418
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301419static const struct bin_attribute pci_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .attr = {
1421 .name = "config",
1422 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001424 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 .read = pci_read_config,
1426 .write = pci_write_config,
1427};
1428
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301429static const struct bin_attribute pcie_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 .attr = {
1431 .name = "config",
1432 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001434 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 .read = pci_read_config,
1436 .write = pci_write_config,
1437};
1438
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001439static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1440 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001441{
1442 struct pci_dev *pdev = to_pci_dev(dev);
1443 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001444 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001445
1446 if (result < 0)
1447 return result;
1448
1449 if (val != 1)
1450 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001451
1452 result = pci_reset_function(pdev);
1453 if (result < 0)
1454 return result;
1455
1456 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001457}
1458
1459static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1460
Zhao, Yu280c73d2008-10-13 20:01:00 +08001461static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1462{
1463 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001464
Bjorn Helgaasb1c615c2018-03-19 13:06:17 -05001465 pcie_vpd_create_sysfs_dev_files(dev);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001466 pcie_aspm_create_sysfs_dev_files(dev);
1467
Bjorn Helgaas5b0764c2018-02-16 10:55:38 -06001468 if (dev->reset_fn) {
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001469 retval = device_create_file(&dev->dev, &reset_attr);
1470 if (retval)
1471 goto error;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001472 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001473 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001474
1475error:
1476 pcie_aspm_remove_sysfs_dev_files(dev);
Bjorn Helgaasb1c615c2018-03-19 13:06:17 -05001477 pcie_vpd_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001478 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001479}
1480
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001481int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001483 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001484 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001485 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (!sysfs_initialized)
1488 return -EACCES;
1489
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001490 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001491 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001492 else
1493 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001494 if (retval)
1495 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001497 retval = pci_create_resource_files(pdev);
1498 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001499 goto err_config_file;
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001502 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001503 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001504 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001505 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001506 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001507 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001509 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001510 attr->size = rom_size;
1511 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001512 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001513 attr->read = pci_read_rom;
1514 attr->write = pci_write_rom;
1515 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1516 if (retval) {
1517 kfree(attr);
1518 goto err_resource_files;
1519 }
1520 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001522
Zhao, Yu280c73d2008-10-13 20:01:00 +08001523 /* add sysfs entries for various capabilities */
1524 retval = pci_create_capabilities_sysfs(pdev);
1525 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001526 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001527
Narendra K911e1c92010-07-26 05:56:50 -05001528 pci_create_firmware_label_files(pdev);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001531
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001532err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001533 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001534 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001535 kfree(pdev->rom_attr);
1536 pdev->rom_attr = NULL;
1537 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001538err_resource_files:
1539 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001540err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001541 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001542 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001543 else
1544 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001545err:
1546 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Zhao, Yu280c73d2008-10-13 20:01:00 +08001549static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1550{
Bjorn Helgaasb1c615c2018-03-19 13:06:17 -05001551 pcie_vpd_remove_sysfs_dev_files(dev);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001552 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001553 if (dev->reset_fn) {
1554 device_remove_file(&dev->dev, &reset_attr);
1555 dev->reset_fn = 0;
1556 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/**
1560 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1561 * @pdev: device whose entries we should free
1562 *
1563 * Cleanup when @pdev is removed from sysfs.
1564 */
1565void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1566{
David Millerd67afe52006-11-10 12:27:48 -08001567 if (!sysfs_initialized)
1568 return;
1569
Zhao, Yu280c73d2008-10-13 20:01:00 +08001570 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001571
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001572 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001574 else
1575 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 pci_remove_resource_files(pdev);
1578
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001579 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001580 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1581 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001582 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Narendra K911e1c92010-07-26 05:56:50 -05001584
1585 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588static int __init pci_sysfs_init(void)
1589{
1590 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001591 int retval;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001594 for_each_pci_dev(pdev) {
1595 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001596 if (retval) {
1597 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001598 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001599 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 return 0;
1603}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001604late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001605
1606static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001607 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001608 NULL,
1609};
1610
1611static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001612 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001613{
Geliang Tang554a6032015-12-23 20:28:13 +08001614 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001615 struct pci_dev *pdev = to_pci_dev(dev);
1616
1617 if (a == &vga_attr.attr)
1618 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1619 return 0;
1620
Yinghai Lu4e15c462012-11-05 15:20:34 -05001621 return a->mode;
1622}
1623
Jiang Liudfab88b2013-05-31 12:21:31 +08001624static struct attribute *pci_dev_hp_attrs[] = {
1625 &dev_remove_attr.attr,
1626 &dev_rescan_attr.attr,
1627 NULL,
1628};
1629
1630static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001631 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001632{
Geliang Tang554a6032015-12-23 20:28:13 +08001633 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001634 struct pci_dev *pdev = to_pci_dev(dev);
1635
1636 if (pdev->is_virtfn)
1637 return 0;
1638
1639 return a->mode;
1640}
1641
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001642static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1643 struct attribute *a, int n)
1644{
1645 struct device *dev = kobj_to_dev(kobj);
1646 struct pci_dev *pdev = to_pci_dev(dev);
1647
1648 if (pci_is_bridge(pdev))
1649 return a->mode;
1650
1651 return 0;
1652}
1653
1654static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1655 struct attribute *a, int n)
1656{
1657 struct device *dev = kobj_to_dev(kobj);
1658 struct pci_dev *pdev = to_pci_dev(dev);
1659
1660 if (pci_is_pcie(pdev))
1661 return a->mode;
1662
1663 return 0;
1664}
1665
1666static const struct attribute_group pci_dev_group = {
1667 .attrs = pci_dev_attrs,
1668};
1669
1670const struct attribute_group *pci_dev_groups[] = {
1671 &pci_dev_group,
1672 NULL,
1673};
1674
1675static const struct attribute_group pci_bridge_group = {
1676 .attrs = pci_bridge_attrs,
1677};
1678
1679const struct attribute_group *pci_bridge_groups[] = {
1680 &pci_bridge_group,
1681 NULL,
1682};
1683
1684static const struct attribute_group pcie_dev_group = {
1685 .attrs = pcie_dev_attrs,
1686};
1687
1688const struct attribute_group *pcie_dev_groups[] = {
1689 &pcie_dev_group,
1690 NULL,
1691};
1692
Arvind Yadave7ea9822017-07-11 14:57:07 +05301693static const struct attribute_group pci_dev_hp_attr_group = {
Jiang Liudfab88b2013-05-31 12:21:31 +08001694 .attrs = pci_dev_hp_attrs,
1695 .is_visible = pci_dev_hp_attrs_are_visible,
1696};
1697
Donald Dutile17893822012-11-05 15:20:36 -05001698#ifdef CONFIG_PCI_IOV
1699static struct attribute *sriov_dev_attrs[] = {
1700 &sriov_totalvfs_attr.attr,
1701 &sriov_numvfs_attr.attr,
Filippo Sironi7dfca152017-10-09 01:09:11 +02001702 &sriov_offset_attr.attr,
1703 &sriov_stride_attr.attr,
1704 &sriov_vf_device_attr.attr,
Bodong Wang0e7df222017-04-13 01:51:40 +03001705 &sriov_drivers_autoprobe_attr.attr,
Donald Dutile17893822012-11-05 15:20:36 -05001706 NULL,
1707};
1708
1709static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001710 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001711{
Geliang Tang554a6032015-12-23 20:28:13 +08001712 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001713
1714 if (!dev_is_pf(dev))
1715 return 0;
1716
1717 return a->mode;
1718}
1719
Arvind Yadave7ea9822017-07-11 14:57:07 +05301720static const struct attribute_group sriov_dev_attr_group = {
Donald Dutile17893822012-11-05 15:20:36 -05001721 .attrs = sriov_dev_attrs,
1722 .is_visible = sriov_attrs_are_visible,
1723};
1724#endif /* CONFIG_PCI_IOV */
1725
Arvind Yadave7ea9822017-07-11 14:57:07 +05301726static const struct attribute_group pci_dev_attr_group = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001727 .attrs = pci_dev_dev_attrs,
1728 .is_visible = pci_dev_attrs_are_visible,
1729};
1730
Arvind Yadave7ea9822017-07-11 14:57:07 +05301731static const struct attribute_group pci_bridge_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001732 .attrs = pci_bridge_attrs,
1733 .is_visible = pci_bridge_attrs_are_visible,
1734};
1735
Arvind Yadave7ea9822017-07-11 14:57:07 +05301736static const struct attribute_group pcie_dev_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001737 .attrs = pcie_dev_attrs,
1738 .is_visible = pcie_dev_attrs_are_visible,
1739};
1740
Yinghai Lu4e15c462012-11-05 15:20:34 -05001741static const struct attribute_group *pci_dev_attr_groups[] = {
1742 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001743 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001744#ifdef CONFIG_PCI_IOV
1745 &sriov_dev_attr_group,
1746#endif
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001747 &pci_bridge_attr_group,
1748 &pcie_dev_attr_group,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001749 NULL,
1750};
1751
Bhumika Goyal69f2dc22017-09-12 16:43:33 +05301752const struct device_type pci_dev_type = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001753 .groups = pci_dev_attr_groups,
1754};