blob: ccc0e28c6678e2aa9d56f1783ef08b830ca97ebd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drivers/pci/pci-sysfs.c
4 *
5 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
6 * (C) Copyright 2002-2004 IBM Corp.
7 * (C) Copyright 2003 Matthew Wilcox
8 * (C) Copyright 2003 Hewlett-Packard
9 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
10 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
11 *
12 * File attributes for PCI devices
13 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -070014 * Modeled after usb's driverfs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 */
17
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/pci.h>
22#include <linux/stat.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/topology.h>
25#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070026#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070027#include <linux/capability.h>
Chris Wrighta628e7b2011-02-14 17:21:49 -080028#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080029#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Matthew Garrett1a39b312012-04-16 16:26:02 -040031#include <linux/vgaarb.h>
Huang Ying448bd852012-06-23 10:23:51 +080032#include <linux/pm_runtime.h>
Sebastian Ottdfc73e72014-04-17 19:46:15 +020033#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "pci.h"
35
36static int sysfs_initialized; /* = 0 */
37
38/* show configuration fields */
39#define pci_config_attr(field, format_string) \
40static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040041field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{ \
43 struct pci_dev *pdev; \
44 \
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040045 pdev = to_pci_dev(dev); \
46 return sprintf(buf, format_string, pdev->field); \
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070047} \
48static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50pci_config_attr(vendor, "0x%04x\n");
51pci_config_attr(device, "0x%04x\n");
52pci_config_attr(subsystem_vendor, "0x%04x\n");
53pci_config_attr(subsystem_device, "0x%04x\n");
Emil Velikov702ed3b2016-11-21 16:24:49 -060054pci_config_attr(revision, "0x%02x\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055pci_config_attr(class, "0x%06x\n");
56pci_config_attr(irq, "%u\n");
57
Doug Thompsonbdee9d92006-06-14 16:59:48 -070058static ssize_t broken_parity_status_show(struct device *dev,
59 struct device_attribute *attr,
60 char *buf)
61{
62 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040063 return sprintf(buf, "%u\n", pdev->broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070064}
65
66static ssize_t broken_parity_status_store(struct device *dev,
67 struct device_attribute *attr,
68 const char *buf, size_t count)
69{
70 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080071 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070072
Jingoo Han9a994e82013-06-01 16:25:25 +090073 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080074 return -EINVAL;
75
76 pdev->broken_parity_status = !!val;
77
78 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070079}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070080static DEVICE_ATTR_RW(broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070081
Sudeep Holla5aaba362014-09-30 14:48:22 +010082static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040083 struct device_attribute *attr, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070084{
Mike Travis3be83052009-01-04 05:18:01 -080085 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070086
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020087#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053088 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
89 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020090#else
Mike Travis3be83052009-01-04 05:18:01 -080091 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020092#endif
Sudeep Holla5aaba362014-09-30 14:48:22 +010093 return cpumap_print_to_pagebuf(list, buf, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Yijing Wangc489f5f2013-09-30 15:02:38 +080096static ssize_t local_cpus_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040097 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +080098{
Sudeep Holla5aaba362014-09-30 14:48:22 +010099 return pci_dev_show_local_cpu(dev, false, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800100}
Bjorn Helgaas33de1b82013-10-31 14:12:40 -0600101static DEVICE_ATTR_RO(local_cpus);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800102
103static ssize_t local_cpulist_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400104 struct device_attribute *attr, char *buf)
Yijing Wangc489f5f2013-09-30 15:02:38 +0800105{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100106 return pci_dev_show_local_cpu(dev, true, attr, buf);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800107}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700108static DEVICE_ATTR_RO(local_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700110/*
111 * PCI Bus Class Devices
112 */
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700113static ssize_t cpuaffinity_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700115{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100116 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
117
118 return cpumap_print_to_pagebuf(false, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700119}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700120static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700121
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700122static ssize_t cpulistaffinity_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700124{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100125 const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
126
127 return cpumap_print_to_pagebuf(true, buf, cpumask);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700128}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700129static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* show resources */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400132static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
133 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400135 struct pci_dev *pci_dev = to_pci_dev(dev);
136 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800138 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700139 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (pci_dev->subordinate)
142 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800143 else
144 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000147 struct resource *res = &pci_dev->resource[i];
148 pci_resource_to_user(pci_dev, i, res, &start, &end);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400149 str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000150 (unsigned long long)start,
151 (unsigned long long)end,
152 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 return (str - buf);
155}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700156static DEVICE_ATTR_RO(resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800158static ssize_t max_link_speed_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
160{
161 struct pci_dev *pci_dev = to_pci_dev(dev);
162 u32 linkcap;
163 int err;
164 const char *speed;
165
166 err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap);
167 if (err)
168 return -EINVAL;
169
170 switch (linkcap & PCI_EXP_LNKCAP_SLS) {
171 case PCI_EXP_LNKCAP_SLS_8_0GB:
172 speed = "8 GT/s";
173 break;
174 case PCI_EXP_LNKCAP_SLS_5_0GB:
175 speed = "5 GT/s";
176 break;
177 case PCI_EXP_LNKCAP_SLS_2_5GB:
178 speed = "2.5 GT/s";
179 break;
180 default:
181 speed = "Unknown speed";
182 }
183
184 return sprintf(buf, "%s\n", speed);
185}
186static DEVICE_ATTR_RO(max_link_speed);
187
188static ssize_t max_link_width_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 struct pci_dev *pci_dev = to_pci_dev(dev);
192 u32 linkcap;
193 int err;
194
195 err = pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &linkcap);
196 if (err)
197 return -EINVAL;
198
199 return sprintf(buf, "%u\n", (linkcap & PCI_EXP_LNKCAP_MLW) >> 4);
200}
201static DEVICE_ATTR_RO(max_link_width);
202
203static ssize_t current_link_speed_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
205{
206 struct pci_dev *pci_dev = to_pci_dev(dev);
207 u16 linkstat;
208 int err;
209 const char *speed;
210
211 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
212 if (err)
213 return -EINVAL;
214
215 switch (linkstat & PCI_EXP_LNKSTA_CLS) {
216 case PCI_EXP_LNKSTA_CLS_8_0GB:
217 speed = "8 GT/s";
218 break;
219 case PCI_EXP_LNKSTA_CLS_5_0GB:
220 speed = "5 GT/s";
221 break;
222 case PCI_EXP_LNKSTA_CLS_2_5GB:
223 speed = "2.5 GT/s";
224 break;
225 default:
226 speed = "Unknown speed";
227 }
228
229 return sprintf(buf, "%s\n", speed);
230}
231static DEVICE_ATTR_RO(current_link_speed);
232
233static ssize_t current_link_width_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
236 struct pci_dev *pci_dev = to_pci_dev(dev);
237 u16 linkstat;
238 int err;
239
240 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
241 if (err)
242 return -EINVAL;
243
244 return sprintf(buf, "%u\n",
245 (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
246}
247static DEVICE_ATTR_RO(current_link_width);
248
249static ssize_t secondary_bus_number_show(struct device *dev,
250 struct device_attribute *attr,
251 char *buf)
252{
253 struct pci_dev *pci_dev = to_pci_dev(dev);
254 u8 sec_bus;
255 int err;
256
257 err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
258 if (err)
259 return -EINVAL;
260
261 return sprintf(buf, "%u\n", sec_bus);
262}
263static DEVICE_ATTR_RO(secondary_bus_number);
264
265static ssize_t subordinate_bus_number_show(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
268{
269 struct pci_dev *pci_dev = to_pci_dev(dev);
270 u8 sub_bus;
271 int err;
272
273 err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
274 if (err)
275 return -EINVAL;
276
277 return sprintf(buf, "%u\n", sub_bus);
278}
279static DEVICE_ATTR_RO(subordinate_bus_number);
280
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400281static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
282 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700283{
284 struct pci_dev *pci_dev = to_pci_dev(dev);
285
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200286 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700287 pci_dev->vendor, pci_dev->device,
288 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
289 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
290 (u8)(pci_dev->class));
291}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700292static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800293
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700294static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400295 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200296{
297 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800298 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900299 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800300
301 if (result < 0)
302 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200303
304 /* this can crash the machine when done on the "wrong" device */
305 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800306 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200307
Trent Piepho92425a42008-11-30 17:10:12 -0800308 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900309 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800310 pci_disable_device(pdev);
311 else
312 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800313 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800314 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200315
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800316 return result < 0 ? result : count;
317}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200318
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700319static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400320 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800321{
322 struct pci_dev *pdev;
323
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400324 pdev = to_pci_dev(dev);
325 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200326}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700327static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200328
Brice Goglin81bb0e12007-01-28 10:53:40 +0100329#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400330static ssize_t numa_node_store(struct device *dev,
331 struct device_attribute *attr, const char *buf,
332 size_t count)
333{
334 struct pci_dev *pdev = to_pci_dev(dev);
335 int node, ret;
336
337 if (!capable(CAP_SYS_ADMIN))
338 return -EPERM;
339
340 ret = kstrtoint(buf, 0, &node);
341 if (ret)
342 return ret;
343
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100344 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
345 return -EINVAL;
346
347 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400348 return -EINVAL;
349
350 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
351 dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
352 node);
353
354 dev->numa_node = node;
355 return count;
356}
357
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400358static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
359 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100360{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400361 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100362}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400363static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100364#endif
365
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400366static ssize_t dma_mask_bits_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800368{
369 struct pci_dev *pdev = to_pci_dev(dev);
370
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400371 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800372}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700373static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800374
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400375static ssize_t consistent_dma_mask_bits_show(struct device *dev,
376 struct device_attribute *attr,
377 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800378{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400379 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800380}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700381static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800382
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400383static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
384 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400385{
386 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800387 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400388
Yijing Wang468ff152014-09-23 13:27:24 +0800389 return sprintf(buf, "%u\n", subordinate ?
390 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
391 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400392}
393
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400394static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
395 const char *buf, size_t count)
Brice Goglinfe970642006-08-31 01:55:15 -0400396{
397 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800398 struct pci_bus *subordinate = pdev->subordinate;
Trent Piepho92425a42008-11-30 17:10:12 -0800399 unsigned long val;
400
Jingoo Han9a994e82013-06-01 16:25:25 +0900401 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800402 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400403
Brice Goglinfe970642006-08-31 01:55:15 -0400404 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800405 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400406
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700407 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800408 * "no_msi" and "bus_flags" only affect what happens when a driver
409 * requests MSI or MSI-X. They don't affect any drivers that have
410 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700411 */
Yijing Wang468ff152014-09-23 13:27:24 +0800412 if (!subordinate) {
413 pdev->no_msi = !val;
414 dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n",
415 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400416 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400417 }
418
Yijing Wang468ff152014-09-23 13:27:24 +0800419 if (val)
420 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
421 else
422 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
423
424 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
425 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400426 return count;
427}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700428static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700429
Alex Chiang705b1aa2009-03-20 14:56:31 -0600430static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
431 size_t count)
432{
433 unsigned long val;
434 struct pci_bus *b = NULL;
435
Jingoo Han9a994e82013-06-01 16:25:25 +0900436 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600437 return -EINVAL;
438
439 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100440 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600441 while ((b = pci_find_next_bus(b)) != NULL)
442 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100443 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600444 }
445 return count;
446}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600447static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600448
Sachin Kamatbf22c902013-09-28 15:42:00 +0530449static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600450 &bus_attr_rescan.attr,
451 NULL,
452};
453
454static const struct attribute_group pci_bus_group = {
455 .attrs = pci_bus_attrs,
456};
457
458const struct attribute_group *pci_bus_groups[] = {
459 &pci_bus_group,
460 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600461};
Alex Chiang77c27c72009-03-20 14:56:36 -0600462
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400463static ssize_t dev_rescan_store(struct device *dev,
464 struct device_attribute *attr, const char *buf,
465 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600466{
467 unsigned long val;
468 struct pci_dev *pdev = to_pci_dev(dev);
469
Jingoo Han9a994e82013-06-01 16:25:25 +0900470 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600471 return -EINVAL;
472
473 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100474 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600475 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100476 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600477 }
478 return count;
479}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530480static struct device_attribute dev_rescan_attr = __ATTR(rescan,
481 (S_IWUSR|S_IWGRP),
482 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600483
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400484static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
485 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600486{
Alex Chiang77c27c72009-03-20 14:56:36 -0600487 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600488
Jingoo Han9a994e82013-06-01 16:25:25 +0900489 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600490 return -EINVAL;
491
Tejun Heobc6caf02014-02-03 14:03:02 -0500492 if (val && device_remove_file_self(dev, attr))
493 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600494 return count;
495}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530496static struct device_attribute dev_remove_attr = __ATTR(remove,
497 (S_IWUSR|S_IWGRP),
498 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700499
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400500static ssize_t dev_bus_rescan_store(struct device *dev,
501 struct device_attribute *attr,
502 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700503{
504 unsigned long val;
505 struct pci_bus *bus = to_pci_bus(dev);
506
Jingoo Han9a994e82013-06-01 16:25:25 +0900507 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700508 return -EINVAL;
509
510 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100511 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800512 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
513 pci_rescan_bus_bridge_resize(bus->self);
514 else
515 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100516 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700517 }
518 return count;
519}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700520static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700521
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100522#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800523static ssize_t d3cold_allowed_store(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
526{
527 struct pci_dev *pdev = to_pci_dev(dev);
528 unsigned long val;
529
Jingoo Han9a994e82013-06-01 16:25:25 +0900530 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800531 return -EINVAL;
532
533 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300534 if (pdev->d3cold_allowed)
535 pci_d3cold_enable(pdev);
536 else
537 pci_d3cold_disable(pdev);
538
Huang Ying448bd852012-06-23 10:23:51 +0800539 pm_runtime_resume(dev);
540
541 return count;
542}
543
544static ssize_t d3cold_allowed_show(struct device *dev,
545 struct device_attribute *attr, char *buf)
546{
547 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400548 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800549}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700550static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800551#endif
552
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200553#ifdef CONFIG_OF
554static ssize_t devspec_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
557 struct pci_dev *pdev = to_pci_dev(dev);
558 struct device_node *np = pci_device_to_OF_node(pdev);
559
Rob Herringb63773a2017-07-18 16:43:21 -0500560 if (np == NULL)
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200561 return 0;
Rob Herringb63773a2017-07-18 16:43:21 -0500562 return sprintf(buf, "%pOF", np);
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200563}
564static DEVICE_ATTR_RO(devspec);
565#endif
566
Donald Dutile17893822012-11-05 15:20:36 -0500567#ifdef CONFIG_PCI_IOV
568static ssize_t sriov_totalvfs_show(struct device *dev,
569 struct device_attribute *attr,
570 char *buf)
571{
572 struct pci_dev *pdev = to_pci_dev(dev);
573
Donald Dutilebff73152012-11-05 15:20:37 -0500574 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500575}
576
577
578static ssize_t sriov_numvfs_show(struct device *dev,
579 struct device_attribute *attr,
580 char *buf)
581{
582 struct pci_dev *pdev = to_pci_dev(dev);
583
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700584 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500585}
586
587/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700588 * num_vfs > 0; number of VFs to enable
589 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500590 *
591 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700592 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500593 */
594static ssize_t sriov_numvfs_store(struct device *dev,
595 struct device_attribute *attr,
596 const char *buf, size_t count)
597{
598 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700599 int ret;
600 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500601
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700602 ret = kstrtou16(buf, 0, &num_vfs);
603 if (ret < 0)
604 return ret;
605
606 if (num_vfs > pci_sriov_get_totalvfs(pdev))
607 return -ERANGE;
608
Jakub Kicinski17530e72017-05-22 15:50:23 -0700609 device_lock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800610
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700611 if (num_vfs == pdev->sriov->num_VFs)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800612 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500613
614 /* is PF driver loaded w/callback */
615 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700616 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800617 ret = -ENOENT;
618 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500619 }
620
Donald Dutile17893822012-11-05 15:20:36 -0500621 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700622 /* disable VFs */
623 ret = pdev->driver->sriov_configure(pdev, 0);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800624 goto exit;
Donald Dutile17893822012-11-05 15:20:36 -0500625 }
626
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700627 /* enable VFs */
628 if (pdev->sriov->num_VFs) {
629 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
630 pdev->sriov->num_VFs, num_vfs);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800631 ret = -EBUSY;
632 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700633 }
Donald Dutile17893822012-11-05 15:20:36 -0500634
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700635 ret = pdev->driver->sriov_configure(pdev, num_vfs);
636 if (ret < 0)
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800637 goto exit;
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700638
639 if (ret != num_vfs)
640 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
641 num_vfs, ret);
642
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800643exit:
Jakub Kicinski17530e72017-05-22 15:50:23 -0700644 device_unlock(&pdev->dev);
Emil Tantilov5b0948d2017-01-06 13:59:08 -0800645
646 if (ret < 0)
647 return ret;
648
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700649 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500650}
651
Filippo Sironi7dfca152017-10-09 01:09:11 +0200652static ssize_t sriov_offset_show(struct device *dev,
653 struct device_attribute *attr,
654 char *buf)
655{
656 struct pci_dev *pdev = to_pci_dev(dev);
657
658 return sprintf(buf, "%u\n", pdev->sriov->offset);
659}
660
661static ssize_t sriov_stride_show(struct device *dev,
662 struct device_attribute *attr,
663 char *buf)
664{
665 struct pci_dev *pdev = to_pci_dev(dev);
666
667 return sprintf(buf, "%u\n", pdev->sriov->stride);
668}
669
670static ssize_t sriov_vf_device_show(struct device *dev,
671 struct device_attribute *attr,
672 char *buf)
673{
674 struct pci_dev *pdev = to_pci_dev(dev);
675
676 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
677}
678
Bodong Wang0e7df222017-04-13 01:51:40 +0300679static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
680 struct device_attribute *attr,
681 char *buf)
682{
683 struct pci_dev *pdev = to_pci_dev(dev);
684
685 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
686}
687
688static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
689 struct device_attribute *attr,
690 const char *buf, size_t count)
691{
692 struct pci_dev *pdev = to_pci_dev(dev);
693 bool drivers_autoprobe;
694
695 if (kstrtobool(buf, &drivers_autoprobe) < 0)
696 return -EINVAL;
697
698 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
699
700 return count;
701}
702
Donald Dutile17893822012-11-05 15:20:36 -0500703static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
704static struct device_attribute sriov_numvfs_attr =
705 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
706 sriov_numvfs_show, sriov_numvfs_store);
Filippo Sironi7dfca152017-10-09 01:09:11 +0200707static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
708static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
709static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
Bodong Wang0e7df222017-04-13 01:51:40 +0300710static struct device_attribute sriov_drivers_autoprobe_attr =
711 __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
712 sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
Donald Dutile17893822012-11-05 15:20:36 -0500713#endif /* CONFIG_PCI_IOV */
714
Alex Williamson782a9852014-05-20 08:53:21 -0600715static ssize_t driver_override_store(struct device *dev,
716 struct device_attribute *attr,
717 const char *buf, size_t count)
718{
719 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200720 char *driver_override, *old, *cp;
Alex Williamson782a9852014-05-20 08:53:21 -0600721
Sasha Levin4efe8742015-02-04 17:38:15 -0500722 /* We need to keep extra room for a newline */
723 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600724 return -EINVAL;
725
726 driver_override = kstrndup(buf, count, GFP_KERNEL);
727 if (!driver_override)
728 return -ENOMEM;
729
730 cp = strchr(driver_override, '\n');
731 if (cp)
732 *cp = '\0';
733
Nicolai Stange95614752017-09-11 09:45:40 +0200734 device_lock(dev);
735 old = pdev->driver_override;
Alex Williamson782a9852014-05-20 08:53:21 -0600736 if (strlen(driver_override)) {
737 pdev->driver_override = driver_override;
738 } else {
739 kfree(driver_override);
740 pdev->driver_override = NULL;
741 }
Nicolai Stange95614752017-09-11 09:45:40 +0200742 device_unlock(dev);
Alex Williamson782a9852014-05-20 08:53:21 -0600743
744 kfree(old);
745
746 return count;
747}
748
749static ssize_t driver_override_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
751{
752 struct pci_dev *pdev = to_pci_dev(dev);
Nicolai Stange95614752017-09-11 09:45:40 +0200753 ssize_t len;
Alex Williamson782a9852014-05-20 08:53:21 -0600754
Nicolai Stange95614752017-09-11 09:45:40 +0200755 device_lock(dev);
756 len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
757 device_unlock(dev);
758 return len;
Alex Williamson782a9852014-05-20 08:53:21 -0600759}
760static DEVICE_ATTR_RW(driver_override);
761
Sachin Kamatbf22c902013-09-28 15:42:00 +0530762static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700763 &dev_attr_resource.attr,
764 &dev_attr_vendor.attr,
765 &dev_attr_device.attr,
766 &dev_attr_subsystem_vendor.attr,
767 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600768 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700769 &dev_attr_class.attr,
770 &dev_attr_irq.attr,
771 &dev_attr_local_cpus.attr,
772 &dev_attr_local_cpulist.attr,
773 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100774#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700775 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100776#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700777 &dev_attr_dma_mask_bits.attr,
778 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700779 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700780 &dev_attr_broken_parity_status.attr,
781 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100782#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700783 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800784#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200785#ifdef CONFIG_OF
786 &dev_attr_devspec.attr,
787#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600788 &dev_attr_driver_override.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700789 NULL,
790};
791
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800792static struct attribute *pci_bridge_attrs[] = {
793 &dev_attr_subordinate_bus_number.attr,
794 &dev_attr_secondary_bus_number.attr,
795 NULL,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700796};
797
Wong Vee Khee56c1af42017-06-01 17:43:06 +0800798static struct attribute *pcie_dev_attrs[] = {
799 &dev_attr_current_link_speed.attr,
800 &dev_attr_current_link_width.attr,
801 &dev_attr_max_link_width.attr,
802 &dev_attr_max_link_speed.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700803 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700806static struct attribute *pcibus_attrs[] = {
807 &dev_attr_rescan.attr,
808 &dev_attr_cpuaffinity.attr,
809 &dev_attr_cpulistaffinity.attr,
810 NULL,
811};
812
813static const struct attribute_group pcibus_group = {
814 .attrs = pcibus_attrs,
815};
816
817const struct attribute_group *pcibus_groups[] = {
818 &pcibus_group,
819 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700820};
821
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400822static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
823 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000824{
825 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400826 struct pci_dev *vga_dev = vga_default_device();
827
828 if (vga_dev)
829 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000830
831 return sprintf(buf, "%u\n",
832 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
833 IORESOURCE_ROM_SHADOW));
834}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530835static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000836
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400837static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
838 struct bin_attribute *bin_attr, char *buf,
839 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Geliang Tang554a6032015-12-23 20:28:13 +0800841 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned int size = 64;
843 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400844 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700847 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400849 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (off > size)
853 return 0;
854 if (off + count > size) {
855 size -= off;
856 count = size;
857 } else {
858 size = count;
859 }
860
Huang Ying3d8387e2012-08-15 09:43:03 +0800861 pci_config_pm_runtime_get(dev);
862
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900863 if ((off & 1) && size) {
864 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700865 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900866 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900868 size--;
869 }
870
871 if ((off & 3) && size > 2) {
872 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700873 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900874 data[off - init_off] = val & 0xff;
875 data[off - init_off + 1] = (val >> 8) & 0xff;
876 off += 2;
877 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900881 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700882 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900883 data[off - init_off] = val & 0xff;
884 data[off - init_off + 1] = (val >> 8) & 0xff;
885 data[off - init_off + 2] = (val >> 16) & 0xff;
886 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 off += 4;
888 size -= 4;
889 }
890
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900891 if (size >= 2) {
892 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700893 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900894 data[off - init_off] = val & 0xff;
895 data[off - init_off + 1] = (val >> 8) & 0xff;
896 off += 2;
897 size -= 2;
898 }
899
900 if (size > 0) {
901 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700902 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900903 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 off++;
905 --size;
906 }
907
Huang Ying3d8387e2012-08-15 09:43:03 +0800908 pci_config_pm_runtime_put(dev);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return count;
911}
912
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400913static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
914 struct bin_attribute *bin_attr, char *buf,
915 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Geliang Tang554a6032015-12-23 20:28:13 +0800917 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 unsigned int size = count;
919 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400920 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (off > dev->cfg_size)
923 return 0;
924 if (off + count > dev->cfg_size) {
925 size = dev->cfg_size - off;
926 count = size;
927 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700928
Huang Ying3d8387e2012-08-15 09:43:03 +0800929 pci_config_pm_runtime_get(dev);
930
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900931 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700932 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900934 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700936
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900937 if ((off & 3) && size > 2) {
938 u16 val = data[off - init_off];
939 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400940 pci_user_write_config_word(dev, off, val);
941 off += 2;
942 size -= 2;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900946 u32 val = data[off - init_off];
947 val |= (u32) data[off - init_off + 1] << 8;
948 val |= (u32) data[off - init_off + 2] << 16;
949 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700950 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 off += 4;
952 size -= 4;
953 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700954
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900955 if (size >= 2) {
956 u16 val = data[off - init_off];
957 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700958 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900959 off += 2;
960 size -= 2;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900963 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700964 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 off++;
966 --size;
967 }
968
Huang Ying3d8387e2012-08-15 09:43:03 +0800969 pci_config_pm_runtime_put(dev);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return count;
972}
973
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400974static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
975 struct bin_attribute *bin_attr, char *buf,
976 loff_t off, size_t count)
Ben Hutchings94e61082008-03-05 16:52:39 +0000977{
Geliang Tang554a6032015-12-23 20:28:13 +0800978 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000979
Hannes Reineckef52e5622016-02-15 09:42:00 +0100980 if (bin_attr->size > 0) {
981 if (off > bin_attr->size)
982 count = 0;
983 else if (count > bin_attr->size - off)
984 count = bin_attr->size - off;
985 }
Ben Hutchings94e61082008-03-05 16:52:39 +0000986
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800987 return pci_read_vpd(dev, off, count, buf);
988}
Ben Hutchings94e61082008-03-05 16:52:39 +0000989
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400990static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
991 struct bin_attribute *bin_attr, char *buf,
992 loff_t off, size_t count)
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800993{
Geliang Tang554a6032015-12-23 20:28:13 +0800994 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800995
Hannes Reineckef52e5622016-02-15 09:42:00 +0100996 if (bin_attr->size > 0) {
997 if (off > bin_attr->size)
998 count = 0;
999 else if (count > bin_attr->size - off)
1000 count = bin_attr->size - off;
1001 }
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001002
1003 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +00001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#ifdef HAVE_PCI_LEGACY
1007/**
1008 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001009 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001011 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * @buf: buffer to store results
1013 * @off: offset into legacy I/O port space
1014 * @count: number of bytes to read
1015 *
1016 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1017 * callback routine (pci_legacy_read).
1018 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001019static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
1020 struct bin_attribute *bin_attr, char *buf,
1021 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Geliang Tang554a6032015-12-23 20:28:13 +08001023 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001025 /* Only support 1, 2 or 4 byte accesses */
1026 if (count != 1 && count != 2 && count != 4)
1027 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001029 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/**
1033 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001034 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001036 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * @buf: buffer containing value to be written
1038 * @off: offset into legacy I/O port space
1039 * @count: number of bytes to write
1040 *
1041 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
1042 * callback routine (pci_legacy_write).
1043 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001044static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1045 struct bin_attribute *bin_attr, char *buf,
1046 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Geliang Tang554a6032015-12-23 20:28:13 +08001048 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001050 /* Only support 1, 2 or 4 byte accesses */
1051 if (count != 1 && count != 2 && count != 4)
1052 return -EINVAL;
1053
1054 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057/**
1058 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001059 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 * @kobj: kobject corresponding to device to be mapped
1061 * @attr: struct bin_attribute for this file
1062 * @vma: struct vm_area_struct passed to mmap
1063 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001064 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * legacy memory space (first meg of bus space) into application virtual
1066 * memory space.
1067 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001068static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1069 struct bin_attribute *attr,
1070 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Geliang Tang554a6032015-12-23 20:28:13 +08001072 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001074 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001075}
1076
1077/**
1078 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -07001079 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001080 * @kobj: kobject corresponding to device to be mapped
1081 * @attr: struct bin_attribute for this file
1082 * @vma: struct vm_area_struct passed to mmap
1083 *
1084 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1085 * legacy IO space (first meg of bus space) into application virtual
1086 * memory space. Returns -ENOSYS if the operation isn't supported
1087 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001088static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1089 struct bin_attribute *attr,
1090 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001091{
Geliang Tang554a6032015-12-23 20:28:13 +08001092 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001093
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001094 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001095}
1096
1097/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001098 * pci_adjust_legacy_attr - adjustment of legacy file attributes
1099 * @b: bus to create files under
1100 * @mmap_type: I/O port or memory
1101 *
1102 * Stub implementation. Can be overridden by arch if necessary.
1103 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001104void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1105 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001106{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001107}
1108
1109/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001110 * pci_create_legacy_files - create legacy I/O port and memory files
1111 * @b: bus to create files under
1112 *
1113 * Some platforms allow access to legacy I/O port and ISA memory space on
1114 * a per-bus basis. This routine creates the files and ties them into
1115 * their associated read, write and mmap files from pci-sysfs.c
1116 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001117 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001118 * as it is ok to set up the PCI bus without these files.
1119 */
1120void pci_create_legacy_files(struct pci_bus *b)
1121{
1122 int error;
1123
1124 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
1125 GFP_ATOMIC);
1126 if (!b->legacy_io)
1127 goto kzalloc_err;
1128
Stephen Rothwell62e877b82010-03-01 20:38:36 +11001129 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001130 b->legacy_io->attr.name = "legacy_io";
1131 b->legacy_io->size = 0xffff;
1132 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1133 b->legacy_io->read = pci_read_legacy_io;
1134 b->legacy_io->write = pci_write_legacy_io;
1135 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001136 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001137 error = device_create_bin_file(&b->dev, b->legacy_io);
1138 if (error)
1139 goto legacy_io_err;
1140
1141 /* Allocated above after the legacy_io struct */
1142 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +00001143 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001144 b->legacy_mem->attr.name = "legacy_mem";
1145 b->legacy_mem->size = 1024*1024;
1146 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1147 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001148 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +10001149 error = device_create_bin_file(&b->dev, b->legacy_mem);
1150 if (error)
1151 goto legacy_mem_err;
1152
1153 return;
1154
1155legacy_mem_err:
1156 device_remove_bin_file(&b->dev, b->legacy_io);
1157legacy_io_err:
1158 kfree(b->legacy_io);
1159 b->legacy_io = NULL;
1160kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -04001161 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 +10001162 return;
1163}
1164
1165void pci_remove_legacy_files(struct pci_bus *b)
1166{
1167 if (b->legacy_io) {
1168 device_remove_bin_file(&b->dev, b->legacy_io);
1169 device_remove_bin_file(&b->dev, b->legacy_mem);
1170 kfree(b->legacy_io); /* both are allocated here */
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173#endif /* HAVE_PCI_LEGACY */
1174
David Woodhousef7195822017-04-12 13:25:59 +01001175#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001176
Martin Wilck3b519e42010-11-10 11:03:21 +01001177int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1178 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001179{
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001180 unsigned long nr, start, size;
1181 resource_size_t pci_start = 0, pci_end;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001182
Martin Wilck3b519e42010-11-10 11:03:21 +01001183 if (pci_resource_len(pdev, resno) == 0)
1184 return 0;
Libin64b00172013-04-15 02:48:54 +00001185 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001186 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -08001187 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
David Woodhouse6bccc7f2017-04-12 13:25:50 +01001188 if (mmap_api == PCI_MMAP_PROCFS) {
1189 pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1190 &pci_start, &pci_end);
1191 pci_start >>= PAGE_SHIFT;
1192 }
Martin Wilck3b519e42010-11-10 11:03:21 +01001193 if (start >= pci_start && start < pci_start + size &&
1194 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001195 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001196 return 0;
1197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199/**
1200 * pci_mmap_resource - map a PCI resource into user memory space
1201 * @kobj: kobject for mapping
1202 * @attr: struct bin_attribute for the file being mapped
1203 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001204 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 *
1206 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001208static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1209 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Geliang Tang554a6032015-12-23 20:28:13 +08001211 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001212 int bar = (unsigned long)attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 enum pci_mmap_state mmap_type;
David Woodhousedca40b12017-04-12 13:25:57 +01001214 struct resource *res = &pdev->resource[bar];
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001215
Bjorn Helgaasca620722016-04-07 17:15:14 -07001216 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1217 return -EINVAL;
1218
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001219 if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001220 return -EINVAL;
Bjorn Helgaas7a094902017-11-29 11:47:29 -06001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
David Woodhousef7195822017-04-12 13:25:59 +01001223
1224 return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001225}
1226
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001227static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1228 struct bin_attribute *attr,
1229 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001230{
1231 return pci_mmap_resource(kobj, attr, vma, 0);
1232}
1233
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001234static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1235 struct bin_attribute *attr,
1236 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001237{
1238 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001241static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1242 struct bin_attribute *attr, char *buf,
1243 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001244{
Geliang Tang554a6032015-12-23 20:28:13 +08001245 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
David Woodhousedca40b12017-04-12 13:25:57 +01001246 int bar = (unsigned long)attr->private;
Alex Williamson86333282010-07-19 09:45:34 -06001247 unsigned long port = off;
Alex Williamson86333282010-07-19 09:45:34 -06001248
David Woodhousedca40b12017-04-12 13:25:57 +01001249 port += pci_resource_start(pdev, bar);
Alex Williamson86333282010-07-19 09:45:34 -06001250
David Woodhousedca40b12017-04-12 13:25:57 +01001251 if (port > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001252 return 0;
1253
David Woodhousedca40b12017-04-12 13:25:57 +01001254 if (port + count - 1 > pci_resource_end(pdev, bar))
Alex Williamson86333282010-07-19 09:45:34 -06001255 return -EINVAL;
1256
1257 switch (count) {
1258 case 1:
1259 if (write)
1260 outb(*(u8 *)buf, port);
1261 else
1262 *(u8 *)buf = inb(port);
1263 return 1;
1264 case 2:
1265 if (write)
1266 outw(*(u16 *)buf, port);
1267 else
1268 *(u16 *)buf = inw(port);
1269 return 2;
1270 case 4:
1271 if (write)
1272 outl(*(u32 *)buf, port);
1273 else
1274 *(u32 *)buf = inl(port);
1275 return 4;
1276 }
1277 return -EINVAL;
1278}
1279
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001280static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1281 struct bin_attribute *attr, char *buf,
1282 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001283{
1284 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1285}
1286
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001287static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1288 struct bin_attribute *attr, char *buf,
1289 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001290{
1291 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001295 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001296 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001297 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001298 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001299 * free their resources.
1300 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001301static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001302{
1303 int i;
1304
1305 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1306 struct bin_attribute *res_attr;
1307
1308 res_attr = pdev->res_attr[i];
1309 if (res_attr) {
1310 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1311 kfree(res_attr);
1312 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001313
1314 res_attr = pdev->res_attr_wc[i];
1315 if (res_attr) {
1316 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1317 kfree(res_attr);
1318 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001319 }
1320}
1321
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001322static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1323{
1324 /* allocate attribute structure, piggyback attribute name */
1325 int name_len = write_combine ? 13 : 10;
1326 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001327 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001328 int retval;
1329
1330 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001331 if (!res_attr)
1332 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001333
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001334 res_attr_name = (char *)(res_attr + 1);
1335
1336 sysfs_bin_attr_init(res_attr);
1337 if (write_combine) {
1338 pdev->res_attr_wc[num] = res_attr;
1339 sprintf(res_attr_name, "resource%d_wc", num);
1340 res_attr->mmap = pci_mmap_resource_wc;
1341 } else {
1342 pdev->res_attr[num] = res_attr;
1343 sprintf(res_attr_name, "resource%d", num);
David Woodhousee854d8b2017-04-12 13:25:56 +01001344 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1345 res_attr->read = pci_read_resource_io;
1346 res_attr->write = pci_write_resource_io;
1347 if (arch_can_pci_mmap_io())
1348 res_attr->mmap = pci_mmap_resource_uc;
1349 } else {
1350 res_attr->mmap = pci_mmap_resource_uc;
1351 }
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001352 }
1353 res_attr->attr.name = res_attr_name;
1354 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1355 res_attr->size = pci_resource_len(pdev, num);
David Woodhousedca40b12017-04-12 13:25:57 +01001356 res_attr->private = (void *)(unsigned long)num;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001357 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1358 if (retval)
1359 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001360
1361 return retval;
1362}
1363
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001364/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001366 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001368 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001370static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001373 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* Expose the PCI resources from this device as files */
1376 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /* skip empty resources */
1379 if (!pci_resource_len(pdev, i))
1380 continue;
1381
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001382 retval = pci_create_attr(pdev, i, 0);
1383 /* for prefetchable resources, create a WC mappable file */
David Woodhouseae749c72017-04-12 13:25:54 +01001384 if (!retval && arch_can_pci_mmap_wc() &&
1385 pdev->resource[i].flags & IORESOURCE_PREFETCH)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001386 retval = pci_create_attr(pdev, i, 1);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001387 if (retval) {
1388 pci_remove_resource_files(pdev);
1389 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001395int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1396void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397#endif /* HAVE_PCI_MMAP */
1398
1399/**
1400 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001401 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001403 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * @buf: user input
1405 * @off: file offset
1406 * @count: number of byte in input
1407 *
1408 * writing anything except 0 enables it
1409 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001410static ssize_t pci_write_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
1416 if ((off == 0) && (*buf == '0') && (count == 2))
1417 pdev->rom_attr_enabled = 0;
1418 else
1419 pdev->rom_attr_enabled = 1;
1420
1421 return count;
1422}
1423
1424/**
1425 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001426 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001428 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * @buf: where to put the data we read from the ROM
1430 * @off: file offset
1431 * @count: number of bytes to read
1432 *
1433 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1434 * device corresponding to @kobj.
1435 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001436static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1437 struct bin_attribute *bin_attr, char *buf,
1438 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Geliang Tang554a6032015-12-23 20:28:13 +08001440 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 void __iomem *rom;
1442 size_t size;
1443
1444 if (!pdev->rom_attr_enabled)
1445 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001448 if (!rom || !size)
1449 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (off >= size)
1452 count = 0;
1453 else {
1454 if (off + count > size)
1455 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 memcpy_fromio(buf, rom + off, count);
1458 }
1459 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return count;
1462}
1463
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301464static const struct bin_attribute pci_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 .attr = {
1466 .name = "config",
1467 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001469 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .read = pci_read_config,
1471 .write = pci_write_config,
1472};
1473
Bhumika Goyal8bdc50a2017-08-02 20:57:27 +05301474static const struct bin_attribute pcie_config_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 .attr = {
1476 .name = "config",
1477 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001479 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 .read = pci_read_config,
1481 .write = pci_write_config,
1482};
1483
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001484static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1485 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001486{
1487 struct pci_dev *pdev = to_pci_dev(dev);
1488 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001489 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001490
1491 if (result < 0)
1492 return result;
1493
1494 if (val != 1)
1495 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001496
1497 result = pci_reset_function(pdev);
1498 if (result < 0)
1499 return result;
1500
1501 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001502}
1503
1504static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1505
Zhao, Yu280c73d2008-10-13 20:01:00 +08001506static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1507{
1508 int retval;
1509 struct bin_attribute *attr;
1510
1511 /* If the device has VPD, try to expose it in sysfs. */
1512 if (dev->vpd) {
1513 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1514 if (!attr)
1515 return -ENOMEM;
1516
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001517 sysfs_bin_attr_init(attr);
Hannes Reinecke104daa72016-02-15 09:42:01 +01001518 attr->size = 0;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001519 attr->attr.name = "vpd";
1520 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001521 attr->read = read_vpd_attr;
1522 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001523 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1524 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001525 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001526 return retval;
1527 }
1528 dev->vpd->attr = attr;
1529 }
1530
1531 /* Active State Power Management */
1532 pcie_aspm_create_sysfs_dev_files(dev);
1533
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001534 if (!pci_probe_reset_function(dev)) {
1535 retval = device_create_file(&dev->dev, &reset_attr);
1536 if (retval)
1537 goto error;
1538 dev->reset_fn = 1;
1539 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001540 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001541
1542error:
1543 pcie_aspm_remove_sysfs_dev_files(dev);
1544 if (dev->vpd && dev->vpd->attr) {
1545 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1546 kfree(dev->vpd->attr);
1547 }
1548
1549 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001550}
1551
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001552int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001554 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001555 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001556 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (!sysfs_initialized)
1559 return -EACCES;
1560
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001561 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001562 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001563 else
1564 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001565 if (retval)
1566 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001568 retval = pci_create_resource_files(pdev);
1569 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001570 goto err_config_file;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001573 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001574 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001575 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001576 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001577 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001578 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001580 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001581 attr->size = rom_size;
1582 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001583 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001584 attr->read = pci_read_rom;
1585 attr->write = pci_write_rom;
1586 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1587 if (retval) {
1588 kfree(attr);
1589 goto err_resource_files;
1590 }
1591 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001593
Zhao, Yu280c73d2008-10-13 20:01:00 +08001594 /* add sysfs entries for various capabilities */
1595 retval = pci_create_capabilities_sysfs(pdev);
1596 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001597 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001598
Narendra K911e1c92010-07-26 05:56:50 -05001599 pci_create_firmware_label_files(pdev);
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001602
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001603err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001604 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001605 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001606 kfree(pdev->rom_attr);
1607 pdev->rom_attr = NULL;
1608 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001609err_resource_files:
1610 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001611err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001612 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001613 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001614 else
1615 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001616err:
1617 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Zhao, Yu280c73d2008-10-13 20:01:00 +08001620static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1621{
1622 if (dev->vpd && dev->vpd->attr) {
1623 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1624 kfree(dev->vpd->attr);
1625 }
1626
1627 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001628 if (dev->reset_fn) {
1629 device_remove_file(&dev->dev, &reset_attr);
1630 dev->reset_fn = 0;
1631 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/**
1635 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1636 * @pdev: device whose entries we should free
1637 *
1638 * Cleanup when @pdev is removed from sysfs.
1639 */
1640void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1641{
David Millerd67afe52006-11-10 12:27:48 -08001642 if (!sysfs_initialized)
1643 return;
1644
Zhao, Yu280c73d2008-10-13 20:01:00 +08001645 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001646
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001647 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001649 else
1650 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 pci_remove_resource_files(pdev);
1653
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001654 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001655 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1656 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001657 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Narendra K911e1c92010-07-26 05:56:50 -05001659
1660 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663static int __init pci_sysfs_init(void)
1664{
1665 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001666 int retval;
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001669 for_each_pci_dev(pdev) {
1670 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001671 if (retval) {
1672 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001673 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001674 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 return 0;
1678}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001679late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001680
1681static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001682 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001683 NULL,
1684};
1685
1686static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001687 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001688{
Geliang Tang554a6032015-12-23 20:28:13 +08001689 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001690 struct pci_dev *pdev = to_pci_dev(dev);
1691
1692 if (a == &vga_attr.attr)
1693 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1694 return 0;
1695
Yinghai Lu4e15c462012-11-05 15:20:34 -05001696 return a->mode;
1697}
1698
Jiang Liudfab88b2013-05-31 12:21:31 +08001699static struct attribute *pci_dev_hp_attrs[] = {
1700 &dev_remove_attr.attr,
1701 &dev_rescan_attr.attr,
1702 NULL,
1703};
1704
1705static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001706 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001707{
Geliang Tang554a6032015-12-23 20:28:13 +08001708 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001709 struct pci_dev *pdev = to_pci_dev(dev);
1710
1711 if (pdev->is_virtfn)
1712 return 0;
1713
1714 return a->mode;
1715}
1716
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001717static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1718 struct attribute *a, int n)
1719{
1720 struct device *dev = kobj_to_dev(kobj);
1721 struct pci_dev *pdev = to_pci_dev(dev);
1722
1723 if (pci_is_bridge(pdev))
1724 return a->mode;
1725
1726 return 0;
1727}
1728
1729static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1730 struct attribute *a, int n)
1731{
1732 struct device *dev = kobj_to_dev(kobj);
1733 struct pci_dev *pdev = to_pci_dev(dev);
1734
1735 if (pci_is_pcie(pdev))
1736 return a->mode;
1737
1738 return 0;
1739}
1740
1741static const struct attribute_group pci_dev_group = {
1742 .attrs = pci_dev_attrs,
1743};
1744
1745const struct attribute_group *pci_dev_groups[] = {
1746 &pci_dev_group,
1747 NULL,
1748};
1749
1750static const struct attribute_group pci_bridge_group = {
1751 .attrs = pci_bridge_attrs,
1752};
1753
1754const struct attribute_group *pci_bridge_groups[] = {
1755 &pci_bridge_group,
1756 NULL,
1757};
1758
1759static const struct attribute_group pcie_dev_group = {
1760 .attrs = pcie_dev_attrs,
1761};
1762
1763const struct attribute_group *pcie_dev_groups[] = {
1764 &pcie_dev_group,
1765 NULL,
1766};
1767
Arvind Yadave7ea9822017-07-11 14:57:07 +05301768static const struct attribute_group pci_dev_hp_attr_group = {
Jiang Liudfab88b2013-05-31 12:21:31 +08001769 .attrs = pci_dev_hp_attrs,
1770 .is_visible = pci_dev_hp_attrs_are_visible,
1771};
1772
Donald Dutile17893822012-11-05 15:20:36 -05001773#ifdef CONFIG_PCI_IOV
1774static struct attribute *sriov_dev_attrs[] = {
1775 &sriov_totalvfs_attr.attr,
1776 &sriov_numvfs_attr.attr,
Filippo Sironi7dfca152017-10-09 01:09:11 +02001777 &sriov_offset_attr.attr,
1778 &sriov_stride_attr.attr,
1779 &sriov_vf_device_attr.attr,
Bodong Wang0e7df222017-04-13 01:51:40 +03001780 &sriov_drivers_autoprobe_attr.attr,
Donald Dutile17893822012-11-05 15:20:36 -05001781 NULL,
1782};
1783
1784static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001785 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001786{
Geliang Tang554a6032015-12-23 20:28:13 +08001787 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001788
1789 if (!dev_is_pf(dev))
1790 return 0;
1791
1792 return a->mode;
1793}
1794
Arvind Yadave7ea9822017-07-11 14:57:07 +05301795static const struct attribute_group sriov_dev_attr_group = {
Donald Dutile17893822012-11-05 15:20:36 -05001796 .attrs = sriov_dev_attrs,
1797 .is_visible = sriov_attrs_are_visible,
1798};
1799#endif /* CONFIG_PCI_IOV */
1800
Arvind Yadave7ea9822017-07-11 14:57:07 +05301801static const struct attribute_group pci_dev_attr_group = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001802 .attrs = pci_dev_dev_attrs,
1803 .is_visible = pci_dev_attrs_are_visible,
1804};
1805
Arvind Yadave7ea9822017-07-11 14:57:07 +05301806static const struct attribute_group pci_bridge_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001807 .attrs = pci_bridge_attrs,
1808 .is_visible = pci_bridge_attrs_are_visible,
1809};
1810
Arvind Yadave7ea9822017-07-11 14:57:07 +05301811static const struct attribute_group pcie_dev_attr_group = {
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001812 .attrs = pcie_dev_attrs,
1813 .is_visible = pcie_dev_attrs_are_visible,
1814};
1815
Yinghai Lu4e15c462012-11-05 15:20:34 -05001816static const struct attribute_group *pci_dev_attr_groups[] = {
1817 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001818 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001819#ifdef CONFIG_PCI_IOV
1820 &sriov_dev_attr_group,
1821#endif
Wong Vee Khee56c1af42017-06-01 17:43:06 +08001822 &pci_bridge_attr_group,
1823 &pcie_dev_attr_group,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001824 NULL,
1825};
1826
Bhumika Goyal69f2dc22017-09-12 16:43:33 +05301827const struct device_type pci_dev_type = {
Yinghai Lu4e15c462012-11-05 15:20:34 -05001828 .groups = pci_dev_attr_groups,
1829};