blob: 8e075ea2743ef41b4ed08065f502527cbe804c82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-sysfs.c
3 *
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
10 *
11 * File attributes for PCI devices
12 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -070013 * Modeled after usb's driverfs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
16
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/pci.h>
21#include <linux/stat.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/topology.h>
24#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070025#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070026#include <linux/capability.h>
Chris Wrighta628e7b2011-02-14 17:21:49 -080027#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080028#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Matthew Garrett1a39b312012-04-16 16:26:02 -040030#include <linux/vgaarb.h>
Huang Ying448bd852012-06-23 10:23:51 +080031#include <linux/pm_runtime.h>
Sebastian Ottdfc73e72014-04-17 19:46:15 +020032#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "pci.h"
34
35static int sysfs_initialized; /* = 0 */
36
37/* show configuration fields */
38#define pci_config_attr(field, format_string) \
39static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040040field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{ \
42 struct pci_dev *pdev; \
43 \
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040044 pdev = to_pci_dev(dev); \
45 return sprintf(buf, format_string, pdev->field); \
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070046} \
47static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49pci_config_attr(vendor, "0x%04x\n");
50pci_config_attr(device, "0x%04x\n");
51pci_config_attr(subsystem_vendor, "0x%04x\n");
52pci_config_attr(subsystem_device, "0x%04x\n");
Emil Velikov702ed3b2016-11-21 16:24:49 -060053pci_config_attr(revision, "0x%02x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054pci_config_attr(class, "0x%06x\n");
55pci_config_attr(irq, "%u\n");
56
Doug Thompsonbdee9d92006-06-14 16:59:48 -070057static ssize_t broken_parity_status_show(struct device *dev,
58 struct device_attribute *attr,
59 char *buf)
60{
61 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040062 return sprintf(buf, "%u\n", pdev->broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070063}
64
65static ssize_t broken_parity_status_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t count)
68{
69 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080070 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070071
Jingoo Han9a994e82013-06-01 16:25:25 +090072 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080073 return -EINVAL;
74
75 pdev->broken_parity_status = !!val;
76
77 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070078}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070079static DEVICE_ATTR_RW(broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070080
Sudeep Holla5aaba362014-09-30 14:48:22 +010081static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040082 struct device_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070083{
Mike Travis3be83052009-01-04 05:18:01 -080084 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070085
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020086#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053087 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
88 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020089#else
Mike Travis3be83052009-01-04 05:18:01 -080090 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020091#endif
Sudeep Holla5aaba362014-09-30 14:48:22 +010092 return cpumap_print_to_pagebuf(list, buf, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Yijing Wangc489f5f2013-09-30 15:02:38 +080095static ssize_t local_cpus_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040096 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +080097{
Sudeep Holla5aaba362014-09-30 14:48:22 +010098 return pci_dev_show_local_cpu(dev, false, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +080099}
Bjorn Helgaas33de1b82013-10-31 14:12:40 -0600100static DEVICE_ATTR_RO(local_cpus);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800101
102static ssize_t local_cpulist_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400103 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +0800104{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100105 return pci_dev_show_local_cpu(dev, true, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800106}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700107static DEVICE_ATTR_RO(local_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700109/*
110 * PCI Bus Class Devices
111 */
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700112static ssize_t cpuaffinity_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700114{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100115 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
116
117 return cpumap_print_to_pagebuf(false, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700118}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700119static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700120
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700121static ssize_t cpulistaffinity_show(struct device *dev,
122 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700123{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100124 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
125
126 return cpumap_print_to_pagebuf(true, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700127}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700128static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* show resources */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400131static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
132 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400134 struct pci_dev *pci_dev = to_pci_dev(dev);
135 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800137 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700138 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (pci_dev->subordinate)
141 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800142 else
143 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000146 struct resource *res = &pci_dev->resource[i];
147 pci_resource_to_user(pci_dev, i, res, &start, &end);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400148 str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000149 (unsigned long long)start,
150 (unsigned long long)end,
151 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 return (str - buf);
154}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700155static DEVICE_ATTR_RO(resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800157static ssize_t max_link_speed_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
160 struct pci_dev *pci_dev = to_pci_dev(dev);
161 u32 linkcap;
162 int err;
163 const char *speed;
164
165 err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap);
166 if (err)
167 return -EINVAL;
168
169 switch (linkcap & PCI_EXP_LNKCAP_SLS) {
170 case PCI_EXP_LNKCAP_SLS_8_0GB:
171 speed = "8 GT/s";
172 break;
173 case PCI_EXP_LNKCAP_SLS_5_0GB:
174 speed = "5 GT/s";
175 break;
176 case PCI_EXP_LNKCAP_SLS_2_5GB:
177 speed = "2.5 GT/s";
178 break;
179 default:
180 speed = "Unknown speed";
181 }
182
183 return sprintf(buf, "%s\n", speed);
184}
185static DEVICE_ATTR_RO(max_link_speed);
186
187static ssize_t max_link_width_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 struct pci_dev *pci_dev = to_pci_dev(dev);
191 u32 linkcap;
192 int err;
193
194 err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap);
195 if (err)
196 return -EINVAL;
197
198 return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4);
199}
200static DEVICE_ATTR_RO(max_link_width);
201
202static ssize_t current_link_speed_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
204{
205 struct pci_dev *pci_dev = to_pci_dev(dev);
206 u16 linkstat;
207 int err;
208 const char *speed;
209
210 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
211 if (err)
212 return -EINVAL;
213
214 switch (linkstat & PCI_EXP_LNKSTA_CLS) {
215 case PCI_EXP_LNKSTA_CLS_8_0GB:
216 speed = "8 GT/s";
217 break;
218 case PCI_EXP_LNKSTA_CLS_5_0GB:
219 speed = "5 GT/s";
220 break;
221 case PCI_EXP_LNKSTA_CLS_2_5GB:
222 speed = "2.5 GT/s";
223 break;
224 default:
225 speed = "Unknown speed";
226 }
227
228 return sprintf(buf, "%s\n", speed);
229}
230static DEVICE_ATTR_RO(current_link_speed);
231
232static ssize_t current_link_width_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234{
235 struct pci_dev *pci_dev = to_pci_dev(dev);
236 u16 linkstat;
237 int err;
238
239 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
240 if (err)
241 return -EINVAL;
242
243 return sprintf(buf, "%u\n",
244 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
245}
246static DEVICE_ATTR_RO(current_link_width);
247
248static ssize_t secondary_bus_number_show(struct device *dev,
249 struct device_attribute *attr,
250 char *buf)
251{
252 struct pci_dev *pci_dev = to_pci_dev(dev);
253 u8 sec_bus;
254 int err;
255
256 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
257 if (err)
258 return -EINVAL;
259
260 return sprintf(buf, "%u\n", sec_bus);
261}
262static DEVICE_ATTR_RO(secondary_bus_number);
263
264static ssize_t subordinate_bus_number_show(struct device *dev,
265 struct device_attribute *attr,
266 char *buf)
267{
268 struct pci_dev *pci_dev = to_pci_dev(dev);
269 u8 sub_bus;
270 int err;
271
272 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
273 if (err)
274 return -EINVAL;
275
276 return sprintf(buf, "%u\n", sub_bus);
277}
278static DEVICE_ATTR_RO(subordinate_bus_number);
279
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400280static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
281 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700282{
283 struct pci_dev *pci_dev = to_pci_dev(dev);
284
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200285 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700286 pci_dev->vendor, pci_dev->device,
287 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
288 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
289 (u8)(pci_dev->class));
290}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700291static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800292
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700293static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400294 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200295{
296 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800297 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900298 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800299
300 if (result < 0)
301 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200302
303 /* this can crash the machine when done on the "wrong" device */
304 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800305 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200306
Trent Piepho92425a42008-11-30 17:10:12 -0800307 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900308 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800309 pci_disable_device(pdev);
310 else
311 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800312 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800313 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200314
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800315 return result < 0 ? result : count;
316}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200317
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700318static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400319 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800320{
321 struct pci_dev *pdev;
322
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400323 pdev = to_pci_dev(dev);
324 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200325}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700326static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200327
Brice Goglin81bb0e12007-01-28 10:53:40 +0100328#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400329static ssize_t numa_node_store(struct device *dev,
330 struct device_attribute *attr, const char *buf,
331 size_t count)
332{
333 struct pci_dev *pdev = to_pci_dev(dev);
334 int node, ret;
335
336 if (!capable(CAP_SYS_ADMIN))
337 return -EPERM;
338
339 ret = kstrtoint(buf, 0, &node);
340 if (ret)
341 return ret;
342
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100343 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
344 return -EINVAL;
345
346 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400347 return -EINVAL;
348
349 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
350 dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
351 node);
352
353 dev->numa_node = node;
354 return count;
355}
356
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400357static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
358 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100359{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400360 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100361}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400362static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100363#endif
364
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400365static ssize_t dma_mask_bits_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800367{
368 struct pci_dev *pdev = to_pci_dev(dev);
369
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400370 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800371}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700372static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800373
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400374static ssize_t consistent_dma_mask_bits_show(struct device *dev,
375 struct device_attribute *attr,
376 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800377{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400378 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800379}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700380static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800381
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400382static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
383 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400384{
385 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800386 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400387
Yijing Wang468ff152014-09-23 13:27:24 +0800388 return sprintf(buf, "%u\n", subordinate ?
389 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
390 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400391}
392
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400393static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
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;
Trent Piepho92425a42008-11-30 17:10:12 -0800398 unsigned long val;
399
Jingoo Han9a994e82013-06-01 16:25:25 +0900400 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800401 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400402
Brice Goglinfe970642006-08-31 01:55:15 -0400403 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800404 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400405
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700406 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800407 * "no_msi" and "bus_flags" only affect what happens when a driver
408 * requests MSI or MSI-X. They don't affect any drivers that have
409 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700410 */
Yijing Wang468ff152014-09-23 13:27:24 +0800411 if (!subordinate) {
412 pdev->no_msi = !val;
413 dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n",
414 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400415 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400416 }
417
Yijing Wang468ff152014-09-23 13:27:24 +0800418 if (val)
419 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
420 else
421 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
422
423 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
424 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400425 return count;
426}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700427static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700428
Alex Chiang705b1aa2009-03-20 14:56:31 -0600429static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
430 size_t count)
431{
432 unsigned long val;
433 struct pci_bus *b = NULL;
434
Jingoo Han9a994e82013-06-01 16:25:25 +0900435 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600436 return -EINVAL;
437
438 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100439 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600440 while ((b = pci_find_next_bus(b)) != NULL)
441 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100442 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600443 }
444 return count;
445}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600446static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600447
Sachin Kamatbf22c902013-09-28 15:42:00 +0530448static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600449 &bus_attr_rescan.attr,
450 NULL,
451};
452
453static const struct attribute_group pci_bus_group = {
454 .attrs = pci_bus_attrs,
455};
456
457const struct attribute_group *pci_bus_groups[] = {
458 &pci_bus_group,
459 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600460};
Alex Chiang77c27c72009-03-20 14:56:36 -0600461
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400462static ssize_t dev_rescan_store(struct device *dev,
463 struct device_attribute *attr, const char *buf,
464 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600465{
466 unsigned long val;
467 struct pci_dev *pdev = to_pci_dev(dev);
468
Jingoo Han9a994e82013-06-01 16:25:25 +0900469 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600470 return -EINVAL;
471
472 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100473 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600474 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100475 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600476 }
477 return count;
478}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530479static struct device_attribute dev_rescan_attr = __ATTR(rescan,
480 (S_IWUSR|S_IWGRP),
481 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600482
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400483static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
484 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600485{
Alex Chiang77c27c72009-03-20 14:56:36 -0600486 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600487
Jingoo Han9a994e82013-06-01 16:25:25 +0900488 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600489 return -EINVAL;
490
Tejun Heobc6caf02014-02-03 14:03:02 -0500491 if (val && device_remove_file_self(dev, attr))
492 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600493 return count;
494}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530495static struct device_attribute dev_remove_attr = __ATTR(remove,
496 (S_IWUSR|S_IWGRP),
497 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700498
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400499static ssize_t dev_bus_rescan_store(struct device *dev,
500 struct device_attribute *attr,
501 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700502{
503 unsigned long val;
504 struct pci_bus *bus = to_pci_bus(dev);
505
Jingoo Han9a994e82013-06-01 16:25:25 +0900506 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700507 return -EINVAL;
508
509 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100510 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800511 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
512 pci_rescan_bus_bridge_resize(bus->self);
513 else
514 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100515 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700516 }
517 return count;
518}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700519static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700520
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100521#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800522static ssize_t d3cold_allowed_store(struct device *dev,
523 struct device_attribute *attr,
524 const char *buf, size_t count)
525{
526 struct pci_dev *pdev = to_pci_dev(dev);
527 unsigned long val;
528
Jingoo Han9a994e82013-06-01 16:25:25 +0900529 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800530 return -EINVAL;
531
532 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300533 if (pdev->d3cold_allowed)
534 pci_d3cold_enable(pdev);
535 else
536 pci_d3cold_disable(pdev);
537
Huang Ying448bd852012-06-23 10:23:51 +0800538 pm_runtime_resume(dev);
539
540 return count;
541}
542
543static ssize_t d3cold_allowed_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
546 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400547 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800548}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700549static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800550#endif
551
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200552#ifdef CONFIG_OF
553static ssize_t devspec_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
555{
556 struct pci_dev *pdev = to_pci_dev(dev);
557 struct device_node *np = pci_device_to_OF_node(pdev);
558
Rob Herringb63773a2017-07-18 16:43:21 -0500559 if (np == NULL)
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200560 return 0;
Rob Herringb63773a2017-07-18 16:43:21 -0500561 return sprintf(buf, "%pOF", np);
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200562}
563static DEVICE_ATTR_RO(devspec);
564#endif
565
Donald Dutile17893822012-11-05 15:20:36 -0500566#ifdef CONFIG_PCI_IOV
567static ssize_t sriov_totalvfs_show(struct device *dev,
568 struct device_attribute *attr,
569 char *buf)
570{
571 struct pci_dev *pdev = to_pci_dev(dev);
572
Donald Dutilebff73152012-11-05 15:20:37 -0500573 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500574}
575
576
577static ssize_t sriov_numvfs_show(struct device *dev,
578 struct device_attribute *attr,
579 char *buf)
580{
581 struct pci_dev *pdev = to_pci_dev(dev);
582
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700583 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500584}
585
586/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700587 * num_vfs > 0; number of VFs to enable
588 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500589 *
590 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700591 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500592 */
593static ssize_t sriov_numvfs_store(struct device *dev,
594 struct device_attribute *attr,
595 const char *buf, size_t count)
596{
597 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700598 int ret;
599 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500600
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700601 ret = kstrtou16(buf, 0, &num_vfs);
602 if (ret < 0)
603 return ret;
604
605 if (num_vfs > pci_sriov_get_totalvfs(pdev))
606 return -ERANGE;
607
Jakub Kicinski17530e72017-05-22 15:50:23 -0700608 device_lock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800609
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700610 if (num_vfs == pdev->sriov->num_VFs)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800611 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500612
613 /* is PF driver loaded w/callback */
614 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700615 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800616 ret = -ENOENT;
617 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500618 }
619
Donald Dutile17893822012-11-05 15:20:36 -0500620 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700621 /* disable VFs */
622 ret = pdev->driver->sriov_configure(pdev, 0);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800623 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500624 }
625
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700626 /* enable VFs */
627 if (pdev->sriov->num_VFs) {
628 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
629 pdev->sriov->num_VFs, num_vfs);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800630 ret = -EBUSY;
631 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700632 }
Donald Dutile17893822012-11-05 15:20:36 -0500633
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700634 ret = pdev->driver->sriov_configure(pdev, num_vfs);
635 if (ret < 0)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800636 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700637
638 if (ret != num_vfs)
639 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
640 num_vfs, ret);
641
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800642exit:
Jakub Kicinski17530e72017-05-22 15:50:23 -0700643 device_unlock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800644
645 if (ret < 0)
646 return ret;
647
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700648 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500649}
650
Bodong Wang0e7df222017-04-13 01:51:40 +0300651static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
652 struct device_attribute *attr,
653 char *buf)
654{
655 struct pci_dev *pdev = to_pci_dev(dev);
656
657 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
658}
659
660static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
661 struct device_attribute *attr,
662 const char *buf, size_t count)
663{
664 struct pci_dev *pdev = to_pci_dev(dev);
665 bool drivers_autoprobe;
666
667 if (kstrtobool(buf, &drivers_autoprobe) < 0)
668 return -EINVAL;
669
670 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
671
672 return count;
673}
674
Donald Dutile17893822012-11-05 15:20:36 -0500675static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
676static struct device_attribute sriov_numvfs_attr =
677 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
678 sriov_numvfs_show, sriov_numvfs_store);
Bodong Wang0e7df222017-04-13 01:51:40 +0300679static struct device_attribute sriov_drivers_autoprobe_attr =
680 __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
681 sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
Donald Dutile17893822012-11-05 15:20:36 -0500682#endif /* CONFIG_PCI_IOV */
683
Alex Williamson782a9852014-05-20 08:53:21 -0600684static ssize_t driver_override_store(struct device *dev,
685 struct device_attribute *attr,
686 const char *buf, size_t count)
687{
688 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200689 char *driver_override, *old, *cp;
Alex Williamson782a9852014-05-20 08:53:21 -0600690
Sasha Levin4efe8742015-02-04 17:38:15 -0500691 /* We need to keep extra room for a newline */
692 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600693 return -EINVAL;
694
695 driver_override = kstrndup(buf, count, GFP_KERNEL);
696 if (!driver_override)
697 return -ENOMEM;
698
699 cp = strchr(driver_override, '\n');
700 if (cp)
701 *cp = '\0';
702
Nicolai Stange95614752017-09-11 09:45:40 +0200703 device_lock(dev);
704 old = pdev->driver_override;
Alex Williamson782a9852014-05-20 08:53:21 -0600705 if (strlen(driver_override)) {
706 pdev->driver_override = driver_override;
707 } else {
708 kfree(driver_override);
709 pdev->driver_override = NULL;
710 }
Nicolai Stange95614752017-09-11 09:45:40 +0200711 device_unlock(dev);
Alex Williamson782a9852014-05-20 08:53:21 -0600712
713 kfree(old);
714
715 return count;
716}
717
718static ssize_t driver_override_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
720{
721 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200722 ssize_t len;
Alex Williamson782a9852014-05-20 08:53:21 -0600723
Nicolai Stange95614752017-09-11 09:45:40 +0200724 device_lock(dev);
725 len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
726 device_unlock(dev);
727 return len;
Alex Williamson782a9852014-05-20 08:53:21 -0600728}
729static DEVICE_ATTR_RW(driver_override);
730
Sachin Kamatbf22c902013-09-28 15:42:00 +0530731static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700732 &dev_attr_resource.attr,
733 &dev_attr_vendor.attr,
734 &dev_attr_device.attr,
735 &dev_attr_subsystem_vendor.attr,
736 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600737 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700738 &dev_attr_class.attr,
739 &dev_attr_irq.attr,
740 &dev_attr_local_cpus.attr,
741 &dev_attr_local_cpulist.attr,
742 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100743#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700744 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100745#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700746 &dev_attr_dma_mask_bits.attr,
747 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700748 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700749 &dev_attr_broken_parity_status.attr,
750 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100751#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700752 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800753#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200754#ifdef CONFIG_OF
755 &dev_attr_devspec.attr,
756#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600757 &dev_attr_driver_override.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700758 NULL,
759};
760
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800761static struct attribute *pci_bridge_attrs[] = {
762 &dev_attr_subordinate_bus_number.attr,
763 &dev_attr_secondary_bus_number.attr,
764 NULL,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700765};
766
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800767static struct attribute *pcie_dev_attrs[] = {
768 &dev_attr_current_link_speed.attr,
769 &dev_attr_current_link_width.attr,
770 &dev_attr_max_link_width.attr,
771 &dev_attr_max_link_speed.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700772 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773};
774
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700775static struct attribute *pcibus_attrs[] = {
776 &dev_attr_rescan.attr,
777 &dev_attr_cpuaffinity.attr,
778 &dev_attr_cpulistaffinity.attr,
779 NULL,
780};
781
782static const struct attribute_group pcibus_group = {
783 .attrs = pcibus_attrs,
784};
785
786const struct attribute_group *pcibus_groups[] = {
787 &pcibus_group,
788 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700789};
790
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400791static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
792 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000793{
794 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400795 struct pci_dev *vga_dev = vga_default_device();
796
797 if (vga_dev)
798 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000799
800 return sprintf(buf, "%u\n",
801 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
802 IORESOURCE_ROM_SHADOW));
803}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530804static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000805
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400806static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
807 struct bin_attribute *bin_attr, char *buf,
808 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Geliang Tang554a6032015-12-23 20:28:13 +0800810 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 unsigned int size = 64;
812 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400813 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700816 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400818 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (off > size)
822 return 0;
823 if (off + count > size) {
824 size -= off;
825 count = size;
826 } else {
827 size = count;
828 }
829
Huang Ying3d8387e2012-08-15 09:43:03 +0800830 pci_config_pm_runtime_get(dev);
831
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900832 if ((off & 1) && size) {
833 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700834 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900835 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900837 size--;
838 }
839
840 if ((off & 3) && size > 2) {
841 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700842 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900843 data[off - init_off] = val & 0xff;
844 data[off - init_off + 1] = (val >> 8) & 0xff;
845 off += 2;
846 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900850 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700851 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900852 data[off - init_off] = val & 0xff;
853 data[off - init_off + 1] = (val >> 8) & 0xff;
854 data[off - init_off + 2] = (val >> 16) & 0xff;
855 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 off += 4;
857 size -= 4;
858 }
859
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900860 if (size >= 2) {
861 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700862 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900863 data[off - init_off] = val & 0xff;
864 data[off - init_off + 1] = (val >> 8) & 0xff;
865 off += 2;
866 size -= 2;
867 }
868
869 if (size > 0) {
870 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700871 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900872 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 off++;
874 --size;
875 }
876
Huang Ying3d8387e2012-08-15 09:43:03 +0800877 pci_config_pm_runtime_put(dev);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return count;
880}
881
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400882static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
883 struct bin_attribute *bin_attr, char *buf,
884 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Geliang Tang554a6032015-12-23 20:28:13 +0800886 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 unsigned int size = count;
888 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400889 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (off > dev->cfg_size)
892 return 0;
893 if (off + count > dev->cfg_size) {
894 size = dev->cfg_size - off;
895 count = size;
896 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700897
Huang Ying3d8387e2012-08-15 09:43:03 +0800898 pci_config_pm_runtime_get(dev);
899
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900900 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700901 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900903 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700905
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900906 if ((off & 3) && size > 2) {
907 u16 val = data[off - init_off];
908 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400909 pci_user_write_config_word(dev, off, val);
910 off += 2;
911 size -= 2;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900915 u32 val = data[off - init_off];
916 val |= (u32) data[off - init_off + 1] << 8;
917 val |= (u32) data[off - init_off + 2] << 16;
918 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700919 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 off += 4;
921 size -= 4;
922 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700923
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900924 if (size >= 2) {
925 u16 val = data[off - init_off];
926 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700927 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900928 off += 2;
929 size -= 2;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900932 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700933 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 off++;
935 --size;
936 }
937
Huang Ying3d8387e2012-08-15 09:43:03 +0800938 pci_config_pm_runtime_put(dev);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return count;
941}
942
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400943static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
944 struct bin_attribute *bin_attr, char *buf,
945 loff_t off, size_t count)
Ben Hutchings94e61082008-03-05 16:52:39 +0000946{
Geliang Tang554a6032015-12-23 20:28:13 +0800947 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000948
Hannes Reineckef52e5622016-02-15 09:42:00 +0100949 if (bin_attr->size > 0) {
950 if (off > bin_attr->size)
951 count = 0;
952 else if (count > bin_attr->size - off)
953 count = bin_attr->size - off;
954 }
Ben Hutchings94e61082008-03-05 16:52:39 +0000955
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800956 return pci_read_vpd(dev, off, count, buf);
957}
Ben Hutchings94e61082008-03-05 16:52:39 +0000958
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400959static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
960 struct bin_attribute *bin_attr, char *buf,
961 loff_t off, size_t count)
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800962{
Geliang Tang554a6032015-12-23 20:28:13 +0800963 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800964
Hannes Reineckef52e5622016-02-15 09:42:00 +0100965 if (bin_attr->size > 0) {
966 if (off > bin_attr->size)
967 count = 0;
968 else if (count > bin_attr->size - off)
969 count = bin_attr->size - off;
970 }
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800971
972 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifdef HAVE_PCI_LEGACY
976/**
977 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700978 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700980 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * @buf: buffer to store results
982 * @off: offset into legacy I/O port space
983 * @count: number of bytes to read
984 *
985 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
986 * callback routine (pci_legacy_read).
987 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400988static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
989 struct bin_attribute *bin_attr, char *buf,
990 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Geliang Tang554a6032015-12-23 20:28:13 +0800992 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400994 /* Only support 1, 2 or 4 byte accesses */
995 if (count != 1 && count != 2 && count != 4)
996 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400998 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/**
1002 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001003 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001005 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * @buf: buffer containing value to be written
1007 * @off: offset into legacy I/O port space
1008 * @count: number of bytes to write
1009 *
1010 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1011 * callback routine (pci_legacy_write).
1012 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001013static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1014 struct bin_attribute *bin_attr, char *buf,
1015 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Geliang Tang554a6032015-12-23 20:28:13 +08001017 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001019 /* Only support 1, 2 or 4 byte accesses */
1020 if (count != 1 && count != 2 && count != 4)
1021 return -EINVAL;
1022
1023 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026/**
1027 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001028 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * @kobj: kobject corresponding to device to be mapped
1030 * @attr: struct bin_attribute for this file
1031 * @vma: struct vm_area_struct passed to mmap
1032 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001033 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * legacy memory space (first meg of bus space) into application virtual
1035 * memory space.
1036 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001037static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1038 struct bin_attribute *attr,
1039 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Geliang Tang554a6032015-12-23 20:28:13 +08001041 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001043 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001044}
1045
1046/**
1047 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001048 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001049 * @kobj: kobject corresponding to device to be mapped
1050 * @attr: struct bin_attribute for this file
1051 * @vma: struct vm_area_struct passed to mmap
1052 *
1053 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1054 * legacy IO space (first meg of bus space) into application virtual
1055 * memory space. Returns -ENOSYS if the operation isn't supported
1056 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001057static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1058 struct bin_attribute *attr,
1059 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001060{
Geliang Tang554a6032015-12-23 20:28:13 +08001061 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001062
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001063 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001064}
1065
1066/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001067 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1068 * @b: bus to create files under
1069 * @mmap_type: I/O port or memory
1070 *
1071 * Stub implementation. Can be overridden by arch if necessary.
1072 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001073void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1074 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001075{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001076}
1077
1078/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001079 * pci_create_legacy_files - create legacy I/O port and memory files
1080 * @b: bus to create files under
1081 *
1082 * Some platforms allow access to legacy I/O port and ISA memory space on
1083 * a per-bus basis. This routine creates the files and ties them into
1084 * their associated read, write and mmap files from pci-sysfs.c
1085 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001086 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001087 * as it is ok to set up the PCI bus without these files.
1088 */
1089void pci_create_legacy_files(struct pci_bus *b)
1090{
1091 int error;
1092
1093 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
1094 GFP_ATOMIC);
1095 if (!b->legacy_io)
1096 goto kzalloc_err;
1097
Stephen Rothwell62e877b82010-03-01 20:38:36 +11001098 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001099 b->legacy_io->attr.name = "legacy_io";
1100 b->legacy_io->size = 0xffff;
1101 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1102 b->legacy_io->read = pci_read_legacy_io;
1103 b->legacy_io->write = pci_write_legacy_io;
1104 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001105 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001106 error = device_create_bin_file(&b->dev, b->legacy_io);
1107 if (error)
1108 goto legacy_io_err;
1109
1110 /* Allocated above after the legacy_io struct */
1111 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +00001112 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001113 b->legacy_mem->attr.name = "legacy_mem";
1114 b->legacy_mem->size = 1024*1024;
1115 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1116 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001117 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001118 error = device_create_bin_file(&b->dev, b->legacy_mem);
1119 if (error)
1120 goto legacy_mem_err;
1121
1122 return;
1123
1124legacy_mem_err:
1125 device_remove_bin_file(&b->dev, b->legacy_io);
1126legacy_io_err:
1127 kfree(b->legacy_io);
1128 b->legacy_io = NULL;
1129kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001130 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 +10001131 return;
1132}
1133
1134void pci_remove_legacy_files(struct pci_bus *b)
1135{
1136 if (b->legacy_io) {
1137 device_remove_bin_file(&b->dev, b->legacy_io);
1138 device_remove_bin_file(&b->dev, b->legacy_mem);
1139 kfree(b->legacy_io); /* both are allocated here */
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142#endif /* HAVE_PCI_LEGACY */
1143
David Woodhousef7195822017-04-12 13:25:59 +01001144#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001145
Martin Wilck3b519e42010-11-10 11:03:21 +01001146int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1147 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001148{
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001149 unsigned long nr, start, size;
1150 resource_size_t pci_start = 0, pci_end;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001151
Martin Wilck3b519e42010-11-10 11:03:21 +01001152 if (pci_resource_len(pdev, resno) == 0)
1153 return 0;
Libin64b00172013-04-15 02:48:54 +00001154 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001155 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -08001156 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001157 if (mmap_api == PCI_MMAP_PROCFS) {
1158 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1159 &pci_start, &pci_end);
1160 pci_start >>= PAGE_SHIFT;
1161 }
Martin Wilck3b519e42010-11-10 11:03:21 +01001162 if (start >= pci_start && start < pci_start + size &&
1163 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001164 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001165 return 0;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/**
1169 * pci_mmap_resource - map a PCI resource into user memory space
1170 * @kobj: kobject for mapping
1171 * @attr: struct bin_attribute for the file being mapped
1172 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001173 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 *
1175 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001177static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1178 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Geliang Tang554a6032015-12-23 20:28:13 +08001180 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001181 int bar = (unsigned long)attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 enum pci_mmap_state mmap_type;
David Woodhousedca40b12017-04-12 13:25:57 +01001183 struct resource *res = &pdev->resource[bar];
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001184
Bjorn Helgaasca620722016-04-07 17:15:14 -07001185 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1186 return -EINVAL;
1187
David Woodhousedca40b12017-04-12 13:25:57 +01001188 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001189 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 +01001190 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
David Woodhousedca40b12017-04-12 13:25:57 +01001191 pci_name(pdev), bar,
1192 (u64)pci_resource_start(pdev, bar),
1193 (u64)pci_resource_len(pdev, bar));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001194 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +01001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
David Woodhousef7195822017-04-12 13:25:59 +01001197
1198 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001199}
1200
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001201static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1202 struct bin_attribute *attr,
1203 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001204{
1205 return pci_mmap_resource(kobj, attr, vma, 0);
1206}
1207
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001208static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1209 struct bin_attribute *attr,
1210 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001211{
1212 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001215static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1216 struct bin_attribute *attr, char *buf,
1217 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001218{
Geliang Tang554a6032015-12-23 20:28:13 +08001219 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001220 int bar = (unsigned long)attr->private;
Alex Williamson86333282010-07-19 09:45:34 -06001221 unsigned long port = off;
Alex Williamson86333282010-07-19 09:45:34 -06001222
David Woodhousedca40b12017-04-12 13:25:57 +01001223 port += pci_resource_start(pdev, bar);
Alex Williamson86333282010-07-19 09:45:34 -06001224
David Woodhousedca40b12017-04-12 13:25:57 +01001225 if (port > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001226 return 0;
1227
David Woodhousedca40b12017-04-12 13:25:57 +01001228 if (port + count - 1 > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001229 return -EINVAL;
1230
1231 switch (count) {
1232 case 1:
1233 if (write)
1234 outb(*(u8 *)buf, port);
1235 else
1236 *(u8 *)buf = inb(port);
1237 return 1;
1238 case 2:
1239 if (write)
1240 outw(*(u16 *)buf, port);
1241 else
1242 *(u16 *)buf = inw(port);
1243 return 2;
1244 case 4:
1245 if (write)
1246 outl(*(u32 *)buf, port);
1247 else
1248 *(u32 *)buf = inl(port);
1249 return 4;
1250 }
1251 return -EINVAL;
1252}
1253
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001254static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1255 struct bin_attribute *attr, char *buf,
1256 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001257{
1258 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1259}
1260
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001261static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1262 struct bin_attribute *attr, char *buf,
1263 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001264{
1265 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001269 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001270 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001271 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001272 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001273 * free their resources.
1274 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001275static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001276{
1277 int i;
1278
1279 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1280 struct bin_attribute *res_attr;
1281
1282 res_attr = pdev->res_attr[i];
1283 if (res_attr) {
1284 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1285 kfree(res_attr);
1286 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001287
1288 res_attr = pdev->res_attr_wc[i];
1289 if (res_attr) {
1290 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1291 kfree(res_attr);
1292 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001293 }
1294}
1295
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001296static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1297{
1298 /* allocate attribute structure, piggyback attribute name */
1299 int name_len = write_combine ? 13 : 10;
1300 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001301 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001302 int retval;
1303
1304 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001305 if (!res_attr)
1306 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001307
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001308 res_attr_name = (char *)(res_attr + 1);
1309
1310 sysfs_bin_attr_init(res_attr);
1311 if (write_combine) {
1312 pdev->res_attr_wc[num] = res_attr;
1313 sprintf(res_attr_name, "resource%d_wc", num);
1314 res_attr->mmap = pci_mmap_resource_wc;
1315 } else {
1316 pdev->res_attr[num] = res_attr;
1317 sprintf(res_attr_name, "resource%d", num);
David Woodhousee854d8b2017-04-12 13:25:56 +01001318 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1319 res_attr->read = pci_read_resource_io;
1320 res_attr->write = pci_write_resource_io;
1321 if (arch_can_pci_mmap_io())
1322 res_attr->mmap = pci_mmap_resource_uc;
1323 } else {
1324 res_attr->mmap = pci_mmap_resource_uc;
1325 }
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001326 }
1327 res_attr->attr.name = res_attr_name;
1328 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1329 res_attr->size = pci_resource_len(pdev, num);
David Woodhousedca40b12017-04-12 13:25:57 +01001330 res_attr->private = (void *)(unsigned long)num;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001331 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1332 if (retval)
1333 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001334
1335 return retval;
1336}
1337
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001338/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001340 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001342 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001344static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001347 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* Expose the PCI resources from this device as files */
1350 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* skip empty resources */
1353 if (!pci_resource_len(pdev, i))
1354 continue;
1355
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001356 retval = pci_create_attr(pdev, i, 0);
1357 /* for prefetchable resources, create a WC mappable file */
David Woodhouseae749c72017-04-12 13:25:54 +01001358 if (!retval && arch_can_pci_mmap_wc() &&
1359 pdev->resource[i].flags & IORESOURCE_PREFETCH)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001360 retval = pci_create_attr(pdev, i, 1);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001361 if (retval) {
1362 pci_remove_resource_files(pdev);
1363 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001369int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1370void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371#endif /* HAVE_PCI_MMAP */
1372
1373/**
1374 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001375 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001377 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * @buf: user input
1379 * @off: file offset
1380 * @count: number of byte in input
1381 *
1382 * writing anything except 0 enables it
1383 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001384static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1385 struct bin_attribute *bin_attr, char *buf,
1386 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Geliang Tang554a6032015-12-23 20:28:13 +08001388 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if ((off == 0) && (*buf == '0') && (count == 2))
1391 pdev->rom_attr_enabled = 0;
1392 else
1393 pdev->rom_attr_enabled = 1;
1394
1395 return count;
1396}
1397
1398/**
1399 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001400 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001402 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * @buf: where to put the data we read from the ROM
1404 * @off: file offset
1405 * @count: number of bytes to read
1406 *
1407 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1408 * device corresponding to @kobj.
1409 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001410static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1411 struct bin_attribute *bin_attr, char *buf,
1412 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Geliang Tang554a6032015-12-23 20:28:13 +08001414 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 void __iomem *rom;
1416 size_t size;
1417
1418 if (!pdev->rom_attr_enabled)
1419 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001422 if (!rom || !size)
1423 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (off >= size)
1426 count = 0;
1427 else {
1428 if (off + count > size)
1429 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 memcpy_fromio(buf, rom + off, count);
1432 }
1433 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return count;
1436}
1437
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301438static const struct bin_attribute pci_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 .attr = {
1440 .name = "config",
1441 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001443 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 .read = pci_read_config,
1445 .write = pci_write_config,
1446};
1447
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301448static const struct bin_attribute pcie_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 .attr = {
1450 .name = "config",
1451 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001453 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 .read = pci_read_config,
1455 .write = pci_write_config,
1456};
1457
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001458static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1459 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001460{
1461 struct pci_dev *pdev = to_pci_dev(dev);
1462 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001463 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001464
1465 if (result < 0)
1466 return result;
1467
1468 if (val != 1)
1469 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001470
1471 result = pci_reset_function(pdev);
1472 if (result < 0)
1473 return result;
1474
1475 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001476}
1477
1478static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1479
Zhao, Yu280c73d2008-10-13 20:01:00 +08001480static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1481{
1482 int retval;
1483 struct bin_attribute *attr;
1484
1485 /* If the device has VPD, try to expose it in sysfs. */
1486 if (dev->vpd) {
1487 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1488 if (!attr)
1489 return -ENOMEM;
1490
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001491 sysfs_bin_attr_init(attr);
Hannes Reinecke104daa72016-02-15 09:42:01 +01001492 attr->size = 0;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001493 attr->attr.name = "vpd";
1494 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001495 attr->read = read_vpd_attr;
1496 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001497 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1498 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001499 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001500 return retval;
1501 }
1502 dev->vpd->attr = attr;
1503 }
1504
1505 /* Active State Power Management */
1506 pcie_aspm_create_sysfs_dev_files(dev);
1507
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001508 if (!pci_probe_reset_function(dev)) {
1509 retval = device_create_file(&dev->dev, &reset_attr);
1510 if (retval)
1511 goto error;
1512 dev->reset_fn = 1;
1513 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001514 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001515
1516error:
1517 pcie_aspm_remove_sysfs_dev_files(dev);
1518 if (dev->vpd && dev->vpd->attr) {
1519 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1520 kfree(dev->vpd->attr);
1521 }
1522
1523 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001524}
1525
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001526int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001528 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001529 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001530 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (!sysfs_initialized)
1533 return -EACCES;
1534
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001535 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001536 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001537 else
1538 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001539 if (retval)
1540 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001542 retval = pci_create_resource_files(pdev);
1543 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001544 goto err_config_file;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001547 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001548 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001549 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001550 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001551 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001552 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001554 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001555 attr->size = rom_size;
1556 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001557 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001558 attr->read = pci_read_rom;
1559 attr->write = pci_write_rom;
1560 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1561 if (retval) {
1562 kfree(attr);
1563 goto err_resource_files;
1564 }
1565 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001567
Zhao, Yu280c73d2008-10-13 20:01:00 +08001568 /* add sysfs entries for various capabilities */
1569 retval = pci_create_capabilities_sysfs(pdev);
1570 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001571 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001572
Narendra K911e1c92010-07-26 05:56:50 -05001573 pci_create_firmware_label_files(pdev);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001576
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001577err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001578 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001579 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001580 kfree(pdev->rom_attr);
1581 pdev->rom_attr = NULL;
1582 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001583err_resource_files:
1584 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001585err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001586 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001587 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001588 else
1589 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001590err:
1591 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Zhao, Yu280c73d2008-10-13 20:01:00 +08001594static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1595{
1596 if (dev->vpd && dev->vpd->attr) {
1597 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1598 kfree(dev->vpd->attr);
1599 }
1600
1601 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001602 if (dev->reset_fn) {
1603 device_remove_file(&dev->dev, &reset_attr);
1604 dev->reset_fn = 0;
1605 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1610 * @pdev: device whose entries we should free
1611 *
1612 * Cleanup when @pdev is removed from sysfs.
1613 */
1614void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1615{
David Millerd67afe52006-11-10 12:27:48 -08001616 if (!sysfs_initialized)
1617 return;
1618
Zhao, Yu280c73d2008-10-13 20:01:00 +08001619 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001620
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001621 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001623 else
1624 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 pci_remove_resource_files(pdev);
1627
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001628 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001629 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1630 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001631 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Narendra K911e1c92010-07-26 05:56:50 -05001633
1634 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637static int __init pci_sysfs_init(void)
1638{
1639 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001640 int retval;
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001643 for_each_pci_dev(pdev) {
1644 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001645 if (retval) {
1646 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001647 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001648 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 return 0;
1652}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001653late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001654
1655static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001656 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001657 NULL,
1658};
1659
1660static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001661 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001662{
Geliang Tang554a6032015-12-23 20:28:13 +08001663 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001664 struct pci_dev *pdev = to_pci_dev(dev);
1665
1666 if (a == &vga_attr.attr)
1667 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1668 return 0;
1669
Yinghai Lu4e15c462012-11-05 15:20:34 -05001670 return a->mode;
1671}
1672
Jiang Liudfab88b2013-05-31 12:21:31 +08001673static struct attribute *pci_dev_hp_attrs[] = {
1674 &dev_remove_attr.attr,
1675 &dev_rescan_attr.attr,
1676 NULL,
1677};
1678
1679static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001680 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001681{
Geliang Tang554a6032015-12-23 20:28:13 +08001682 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001683 struct pci_dev *pdev = to_pci_dev(dev);
1684
1685 if (pdev->is_virtfn)
1686 return 0;
1687
1688 return a->mode;
1689}
1690
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001691static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1692 struct attribute *a, int n)
1693{
1694 struct device *dev = kobj_to_dev(kobj);
1695 struct pci_dev *pdev = to_pci_dev(dev);
1696
1697 if (pci_is_bridge(pdev))
1698 return a->mode;
1699
1700 return 0;
1701}
1702
1703static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1704 struct attribute *a, int n)
1705{
1706 struct device *dev = kobj_to_dev(kobj);
1707 struct pci_dev *pdev = to_pci_dev(dev);
1708
1709 if (pci_is_pcie(pdev))
1710 return a->mode;
1711
1712 return 0;
1713}
1714
1715static const struct attribute_group pci_dev_group = {
1716 .attrs = pci_dev_attrs,
1717};
1718
1719const struct attribute_group *pci_dev_groups[] = {
1720 &pci_dev_group,
1721 NULL,
1722};
1723
1724static const struct attribute_group pci_bridge_group = {
1725 .attrs = pci_bridge_attrs,
1726};
1727
1728const struct attribute_group *pci_bridge_groups[] = {
1729 &pci_bridge_group,
1730 NULL,
1731};
1732
1733static const struct attribute_group pcie_dev_group = {
1734 .attrs = pcie_dev_attrs,
1735};
1736
1737const struct attribute_group *pcie_dev_groups[] = {
1738 &pcie_dev_group,
1739 NULL,
1740};
1741
Arvind Yadave7ea9822017-07-11 14:57:07 +05301742static const struct attribute_group pci_dev_hp_attr_group = {
Jiang Liudfab88b2013-05-31 12:21:31 +08001743 .attrs = pci_dev_hp_attrs,
1744 .is_visible = pci_dev_hp_attrs_are_visible,
1745};
1746
Donald Dutile17893822012-11-05 15:20:36 -05001747#ifdef CONFIG_PCI_IOV
1748static struct attribute *sriov_dev_attrs[] = {
1749 &sriov_totalvfs_attr.attr,
1750 &sriov_numvfs_attr.attr,
Bodong Wang0e7df222017-04-13 01:51:40 +03001751 &sriov_drivers_autoprobe_attr.attr,
Donald Dutile17893822012-11-05 15:20:36 -05001752 NULL,
1753};
1754
1755static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001756 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001757{
Geliang Tang554a6032015-12-23 20:28:13 +08001758 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001759
1760 if (!dev_is_pf(dev))
1761 return 0;
1762
1763 return a->mode;
1764}
1765
Arvind Yadave7ea9822017-07-11 14:57:07 +05301766static const struct attribute_group sriov_dev_attr_group = {
Donald Dutile17893822012-11-05 15:20:36 -05001767 .attrs = sriov_dev_attrs,
1768 .is_visible = sriov_attrs_are_visible,
1769};
1770#endif /* CONFIG_PCI_IOV */
1771
Arvind Yadave7ea9822017-07-11 14:57:07 +05301772static const struct attribute_group pci_dev_attr_group = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001773 .attrs = pci_dev_dev_attrs,
1774 .is_visible = pci_dev_attrs_are_visible,
1775};
1776
Arvind Yadave7ea9822017-07-11 14:57:07 +05301777static const struct attribute_group pci_bridge_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001778 .attrs = pci_bridge_attrs,
1779 .is_visible = pci_bridge_attrs_are_visible,
1780};
1781
Arvind Yadave7ea9822017-07-11 14:57:07 +05301782static const struct attribute_group pcie_dev_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001783 .attrs = pcie_dev_attrs,
1784 .is_visible = pcie_dev_attrs_are_visible,
1785};
1786
Yinghai Lu4e15c462012-11-05 15:20:34 -05001787static const struct attribute_group *pci_dev_attr_groups[] = {
1788 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001789 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001790#ifdef CONFIG_PCI_IOV
1791 &sriov_dev_attr_group,
1792#endif
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001793 &pci_bridge_attr_group,
1794 &pcie_dev_attr_group,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001795 NULL,
1796};
1797
1798struct device_type pci_dev_type = {
1799 .groups = pci_dev_attr_groups,
1800};