blob: 066628776e1be62ec4fdfb2692bd15dbee7ab761 [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
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400157static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
158 char *buf)
Greg KH98885492005-05-05 11:57:25 -0700159{
160 struct pci_dev *pci_dev = to_pci_dev(dev);
161
Ricardo Ribalda Delgado89ec3dc2014-08-27 14:57:57 +0200162 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
Greg KH98885492005-05-05 11:57:25 -0700163 pci_dev->vendor, pci_dev->device,
164 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
165 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
166 (u8)(pci_dev->class));
167}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700168static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800169
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700170static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400171 const char *buf, size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200172{
173 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800174 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900175 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800176
177 if (result < 0)
178 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200179
180 /* this can crash the machine when done on the "wrong" device */
181 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800182 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200183
Trent Piepho92425a42008-11-30 17:10:12 -0800184 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900185 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800186 pci_disable_device(pdev);
187 else
188 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800189 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800190 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200191
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800192 return result < 0 ? result : count;
193}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200194
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700195static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400196 char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800197{
198 struct pci_dev *pdev;
199
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400200 pdev = to_pci_dev(dev);
201 return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200202}
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700203static DEVICE_ATTR_RW(enable);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200204
Brice Goglin81bb0e12007-01-28 10:53:40 +0100205#ifdef CONFIG_NUMA
Prarit Bhargava63692df2014-10-23 14:22:12 -0400206static ssize_t numa_node_store(struct device *dev,
207 struct device_attribute *attr, const char *buf,
208 size_t count)
209{
210 struct pci_dev *pdev = to_pci_dev(dev);
211 int node, ret;
212
213 if (!capable(CAP_SYS_ADMIN))
214 return -EPERM;
215
216 ret = kstrtoint(buf, 0, &node);
217 if (ret)
218 return ret;
219
Mathias Krause3dcc8d32015-11-09 20:00:27 +0100220 if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
221 return -EINVAL;
222
223 if (node != NUMA_NO_NODE && !node_online(node))
Prarit Bhargava63692df2014-10-23 14:22:12 -0400224 return -EINVAL;
225
226 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
227 dev_alert(&pdev->dev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
228 node);
229
230 dev->numa_node = node;
231 return count;
232}
233
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400234static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
235 char *buf)
Brice Goglin81bb0e12007-01-28 10:53:40 +0100236{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400237 return sprintf(buf, "%d\n", dev->numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100238}
Prarit Bhargava63692df2014-10-23 14:22:12 -0400239static DEVICE_ATTR_RW(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100240#endif
241
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400242static ssize_t dma_mask_bits_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800244{
245 struct pci_dev *pdev = to_pci_dev(dev);
246
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400247 return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800248}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700249static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800250
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400251static ssize_t consistent_dma_mask_bits_show(struct device *dev,
252 struct device_attribute *attr,
253 char *buf)
Yinghai Lubb965402009-11-24 18:21:21 -0800254{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400255 return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
Yinghai Lubb965402009-11-24 18:21:21 -0800256}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700257static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800258
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400259static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
260 char *buf)
Brice Goglinfe970642006-08-31 01:55:15 -0400261{
262 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800263 struct pci_bus *subordinate = pdev->subordinate;
Brice Goglinfe970642006-08-31 01:55:15 -0400264
Yijing Wang468ff152014-09-23 13:27:24 +0800265 return sprintf(buf, "%u\n", subordinate ?
266 !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
267 : !pdev->no_msi);
Brice Goglinfe970642006-08-31 01:55:15 -0400268}
269
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400270static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
Brice Goglinfe970642006-08-31 01:55:15 -0400272{
273 struct pci_dev *pdev = to_pci_dev(dev);
Yijing Wang468ff152014-09-23 13:27:24 +0800274 struct pci_bus *subordinate = pdev->subordinate;
Trent Piepho92425a42008-11-30 17:10:12 -0800275 unsigned long val;
276
Jingoo Han9a994e82013-06-01 16:25:25 +0900277 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800278 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400279
Brice Goglinfe970642006-08-31 01:55:15 -0400280 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800281 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400282
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700283 /*
Yijing Wang468ff152014-09-23 13:27:24 +0800284 * "no_msi" and "bus_flags" only affect what happens when a driver
285 * requests MSI or MSI-X. They don't affect any drivers that have
286 * already requested MSI or MSI-X.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700287 */
Yijing Wang468ff152014-09-23 13:27:24 +0800288 if (!subordinate) {
289 pdev->no_msi = !val;
290 dev_info(&pdev->dev, "MSI/MSI-X %s for future drivers\n",
291 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400292 return count;
Brice Goglinfe970642006-08-31 01:55:15 -0400293 }
294
Yijing Wang468ff152014-09-23 13:27:24 +0800295 if (val)
296 subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
297 else
298 subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
299
300 dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
301 val ? "allowed" : "disallowed");
Brice Goglinfe970642006-08-31 01:55:15 -0400302 return count;
303}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700304static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700305
Alex Chiang705b1aa2009-03-20 14:56:31 -0600306static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
307 size_t count)
308{
309 unsigned long val;
310 struct pci_bus *b = NULL;
311
Jingoo Han9a994e82013-06-01 16:25:25 +0900312 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600313 return -EINVAL;
314
315 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100316 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600317 while ((b = pci_find_next_bus(b)) != NULL)
318 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100319 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600320 }
321 return count;
322}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600323static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600324
Sachin Kamatbf22c902013-09-28 15:42:00 +0530325static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600326 &bus_attr_rescan.attr,
327 NULL,
328};
329
330static const struct attribute_group pci_bus_group = {
331 .attrs = pci_bus_attrs,
332};
333
334const struct attribute_group *pci_bus_groups[] = {
335 &pci_bus_group,
336 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600337};
Alex Chiang77c27c72009-03-20 14:56:36 -0600338
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400339static ssize_t dev_rescan_store(struct device *dev,
340 struct device_attribute *attr, const char *buf,
341 size_t count)
Alex Chiang738a6392009-03-20 14:56:41 -0600342{
343 unsigned long val;
344 struct pci_dev *pdev = to_pci_dev(dev);
345
Jingoo Han9a994e82013-06-01 16:25:25 +0900346 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600347 return -EINVAL;
348
349 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100350 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600351 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100352 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600353 }
354 return count;
355}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530356static struct device_attribute dev_rescan_attr = __ATTR(rescan,
357 (S_IWUSR|S_IWGRP),
358 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600359
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400360static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t count)
Alex Chiang77c27c72009-03-20 14:56:36 -0600362{
Alex Chiang77c27c72009-03-20 14:56:36 -0600363 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600364
Jingoo Han9a994e82013-06-01 16:25:25 +0900365 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600366 return -EINVAL;
367
Tejun Heobc6caf02014-02-03 14:03:02 -0500368 if (val && device_remove_file_self(dev, attr))
369 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600370 return count;
371}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530372static struct device_attribute dev_remove_attr = __ATTR(remove,
373 (S_IWUSR|S_IWGRP),
374 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700375
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400376static ssize_t dev_bus_rescan_store(struct device *dev,
377 struct device_attribute *attr,
378 const char *buf, size_t count)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700379{
380 unsigned long val;
381 struct pci_bus *bus = to_pci_bus(dev);
382
Jingoo Han9a994e82013-06-01 16:25:25 +0900383 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700384 return -EINVAL;
385
386 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100387 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800388 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
389 pci_rescan_bus_bridge_resize(bus->self);
390 else
391 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100392 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700393 }
394 return count;
395}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700396static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700397
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100398#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Huang Ying448bd852012-06-23 10:23:51 +0800399static ssize_t d3cold_allowed_store(struct device *dev,
400 struct device_attribute *attr,
401 const char *buf, size_t count)
402{
403 struct pci_dev *pdev = to_pci_dev(dev);
404 unsigned long val;
405
Jingoo Han9a994e82013-06-01 16:25:25 +0900406 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800407 return -EINVAL;
408
409 pdev->d3cold_allowed = !!val;
Mika Westerberg9d26d3a2016-06-02 11:17:12 +0300410 if (pdev->d3cold_allowed)
411 pci_d3cold_enable(pdev);
412 else
413 pci_d3cold_disable(pdev);
414
Huang Ying448bd852012-06-23 10:23:51 +0800415 pm_runtime_resume(dev);
416
417 return count;
418}
419
420static ssize_t d3cold_allowed_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 struct pci_dev *pdev = to_pci_dev(dev);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400424 return sprintf(buf, "%u\n", pdev->d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800425}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700426static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800427#endif
428
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200429#ifdef CONFIG_OF
430static ssize_t devspec_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
433 struct pci_dev *pdev = to_pci_dev(dev);
434 struct device_node *np = pci_device_to_OF_node(pdev);
435
436 if (np == NULL || np->full_name == NULL)
437 return 0;
438 return sprintf(buf, "%s", np->full_name);
439}
440static DEVICE_ATTR_RO(devspec);
441#endif
442
Donald Dutile17893822012-11-05 15:20:36 -0500443#ifdef CONFIG_PCI_IOV
444static ssize_t sriov_totalvfs_show(struct device *dev,
445 struct device_attribute *attr,
446 char *buf)
447{
448 struct pci_dev *pdev = to_pci_dev(dev);
449
Donald Dutilebff73152012-11-05 15:20:37 -0500450 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500451}
452
453
454static ssize_t sriov_numvfs_show(struct device *dev,
455 struct device_attribute *attr,
456 char *buf)
457{
458 struct pci_dev *pdev = to_pci_dev(dev);
459
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700460 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500461}
462
463/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700464 * num_vfs > 0; number of VFs to enable
465 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500466 *
467 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700468 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500469 */
470static ssize_t sriov_numvfs_store(struct device *dev,
471 struct device_attribute *attr,
472 const char *buf, size_t count)
473{
474 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700475 int ret;
476 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500477
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700478 ret = kstrtou16(buf, 0, &num_vfs);
479 if (ret < 0)
480 return ret;
481
482 if (num_vfs > pci_sriov_get_totalvfs(pdev))
483 return -ERANGE;
484
485 if (num_vfs == pdev->sriov->num_VFs)
486 return count; /* no change */
Donald Dutile17893822012-11-05 15:20:36 -0500487
488 /* is PF driver loaded w/callback */
489 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700490 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Donald Dutile17893822012-11-05 15:20:36 -0500491 return -ENOSYS;
492 }
493
Donald Dutile17893822012-11-05 15:20:36 -0500494 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700495 /* disable VFs */
496 ret = pdev->driver->sriov_configure(pdev, 0);
497 if (ret < 0)
498 return ret;
499 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500500 }
501
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700502 /* enable VFs */
503 if (pdev->sriov->num_VFs) {
504 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
505 pdev->sriov->num_VFs, num_vfs);
506 return -EBUSY;
507 }
Donald Dutile17893822012-11-05 15:20:36 -0500508
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700509 ret = pdev->driver->sriov_configure(pdev, num_vfs);
510 if (ret < 0)
511 return ret;
512
513 if (ret != num_vfs)
514 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
515 num_vfs, ret);
516
517 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500518}
519
520static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
521static struct device_attribute sriov_numvfs_attr =
522 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
523 sriov_numvfs_show, sriov_numvfs_store);
524#endif /* CONFIG_PCI_IOV */
525
Alex Williamson782a9852014-05-20 08:53:21 -0600526static ssize_t driver_override_store(struct device *dev,
527 struct device_attribute *attr,
528 const char *buf, size_t count)
529{
530 struct pci_dev *pdev = to_pci_dev(dev);
531 char *driver_override, *old = pdev->driver_override, *cp;
532
Sasha Levin4efe8742015-02-04 17:38:15 -0500533 /* We need to keep extra room for a newline */
534 if (count >= (PAGE_SIZE - 1))
Alex Williamson782a9852014-05-20 08:53:21 -0600535 return -EINVAL;
536
537 driver_override = kstrndup(buf, count, GFP_KERNEL);
538 if (!driver_override)
539 return -ENOMEM;
540
541 cp = strchr(driver_override, '\n');
542 if (cp)
543 *cp = '\0';
544
545 if (strlen(driver_override)) {
546 pdev->driver_override = driver_override;
547 } else {
548 kfree(driver_override);
549 pdev->driver_override = NULL;
550 }
551
552 kfree(old);
553
554 return count;
555}
556
557static ssize_t driver_override_show(struct device *dev,
558 struct device_attribute *attr, char *buf)
559{
560 struct pci_dev *pdev = to_pci_dev(dev);
561
Sasha Levin4efe8742015-02-04 17:38:15 -0500562 return snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
Alex Williamson782a9852014-05-20 08:53:21 -0600563}
564static DEVICE_ATTR_RW(driver_override);
565
Sachin Kamatbf22c902013-09-28 15:42:00 +0530566static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700567 &dev_attr_resource.attr,
568 &dev_attr_vendor.attr,
569 &dev_attr_device.attr,
570 &dev_attr_subsystem_vendor.attr,
571 &dev_attr_subsystem_device.attr,
Emil Velikov702ed3b2016-11-21 16:24:49 -0600572 &dev_attr_revision.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700573 &dev_attr_class.attr,
574 &dev_attr_irq.attr,
575 &dev_attr_local_cpus.attr,
576 &dev_attr_local_cpulist.attr,
577 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100578#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700579 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100580#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700581 &dev_attr_dma_mask_bits.attr,
582 &dev_attr_consistent_dma_mask_bits.attr,
Greg Kroah-Hartmand8e7d532014-10-30 09:30:28 -0700583 &dev_attr_enable.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700584 &dev_attr_broken_parity_status.attr,
585 &dev_attr_msi_bus.attr,
Rafael J. Wysockifbb988b2014-11-27 23:16:57 +0100586#if defined(CONFIG_PM) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700587 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800588#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200589#ifdef CONFIG_OF
590 &dev_attr_devspec.attr,
591#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600592 &dev_attr_driver_override.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700593 NULL,
594};
595
596static const struct attribute_group pci_dev_group = {
597 .attrs = pci_dev_attrs,
598};
599
600const struct attribute_group *pci_dev_groups[] = {
601 &pci_dev_group,
602 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700605static struct attribute *pcibus_attrs[] = {
606 &dev_attr_rescan.attr,
607 &dev_attr_cpuaffinity.attr,
608 &dev_attr_cpulistaffinity.attr,
609 NULL,
610};
611
612static const struct attribute_group pcibus_group = {
613 .attrs = pcibus_attrs,
614};
615
616const struct attribute_group *pcibus_groups[] = {
617 &pcibus_group,
618 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700619};
620
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400621static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
622 char *buf)
Dave Airlie217f45d2009-03-04 05:57:05 +0000623{
624 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400625 struct pci_dev *vga_dev = vga_default_device();
626
627 if (vga_dev)
628 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000629
630 return sprintf(buf, "%u\n",
631 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
632 IORESOURCE_ROM_SHADOW));
633}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530634static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000635
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400636static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
637 struct bin_attribute *bin_attr, char *buf,
638 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Geliang Tang554a6032015-12-23 20:28:13 +0800640 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 unsigned int size = 64;
642 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400643 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* Several chips lock up trying to read undefined config space */
Linus Torvaldsab0fa822016-04-14 12:00:21 -0700646 if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 size = dev->cfg_size;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400648 else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 size = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (off > size)
652 return 0;
653 if (off + count > size) {
654 size -= off;
655 count = size;
656 } else {
657 size = count;
658 }
659
Huang Ying3d8387e2012-08-15 09:43:03 +0800660 pci_config_pm_runtime_get(dev);
661
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900662 if ((off & 1) && size) {
663 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700664 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900665 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900667 size--;
668 }
669
670 if ((off & 3) && size > 2) {
671 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700672 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900673 data[off - init_off] = val & 0xff;
674 data[off - init_off + 1] = (val >> 8) & 0xff;
675 off += 2;
676 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900680 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700681 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900682 data[off - init_off] = val & 0xff;
683 data[off - init_off + 1] = (val >> 8) & 0xff;
684 data[off - init_off + 2] = (val >> 16) & 0xff;
685 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 off += 4;
687 size -= 4;
688 }
689
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900690 if (size >= 2) {
691 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700692 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900693 data[off - init_off] = val & 0xff;
694 data[off - init_off + 1] = (val >> 8) & 0xff;
695 off += 2;
696 size -= 2;
697 }
698
699 if (size > 0) {
700 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700701 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900702 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 off++;
704 --size;
705 }
706
Huang Ying3d8387e2012-08-15 09:43:03 +0800707 pci_config_pm_runtime_put(dev);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return count;
710}
711
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400712static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
713 struct bin_attribute *bin_attr, char *buf,
714 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Geliang Tang554a6032015-12-23 20:28:13 +0800716 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned int size = count;
718 loff_t init_off = off;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400719 u8 *data = (u8 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (off > dev->cfg_size)
722 return 0;
723 if (off + count > dev->cfg_size) {
724 size = dev->cfg_size - off;
725 count = size;
726 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700727
Huang Ying3d8387e2012-08-15 09:43:03 +0800728 pci_config_pm_runtime_get(dev);
729
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900730 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700731 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900733 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700735
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900736 if ((off & 3) && size > 2) {
737 u16 val = data[off - init_off];
738 val |= (u16) data[off - init_off + 1] << 8;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400739 pci_user_write_config_word(dev, off, val);
740 off += 2;
741 size -= 2;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900745 u32 val = data[off - init_off];
746 val |= (u32) data[off - init_off + 1] << 8;
747 val |= (u32) data[off - init_off + 2] << 16;
748 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700749 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 off += 4;
751 size -= 4;
752 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700753
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900754 if (size >= 2) {
755 u16 val = data[off - init_off];
756 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700757 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900758 off += 2;
759 size -= 2;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900762 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700763 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 off++;
765 --size;
766 }
767
Huang Ying3d8387e2012-08-15 09:43:03 +0800768 pci_config_pm_runtime_put(dev);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return count;
771}
772
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400773static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
774 struct bin_attribute *bin_attr, char *buf,
775 loff_t off, size_t count)
Ben Hutchings94e61082008-03-05 16:52:39 +0000776{
Geliang Tang554a6032015-12-23 20:28:13 +0800777 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000778
Hannes Reineckef52e5622016-02-15 09:42:00 +0100779 if (bin_attr->size > 0) {
780 if (off > bin_attr->size)
781 count = 0;
782 else if (count > bin_attr->size - off)
783 count = bin_attr->size - off;
784 }
Ben Hutchings94e61082008-03-05 16:52:39 +0000785
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800786 return pci_read_vpd(dev, off, count, buf);
787}
Ben Hutchings94e61082008-03-05 16:52:39 +0000788
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400789static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
790 struct bin_attribute *bin_attr, char *buf,
791 loff_t off, size_t count)
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800792{
Geliang Tang554a6032015-12-23 20:28:13 +0800793 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800794
Hannes Reineckef52e5622016-02-15 09:42:00 +0100795 if (bin_attr->size > 0) {
796 if (off > bin_attr->size)
797 count = 0;
798 else if (count > bin_attr->size - off)
799 count = bin_attr->size - off;
800 }
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800801
802 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#ifdef HAVE_PCI_LEGACY
806/**
807 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700808 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700810 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * @buf: buffer to store results
812 * @off: offset into legacy I/O port space
813 * @count: number of bytes to read
814 *
815 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
816 * callback routine (pci_legacy_read).
817 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400818static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
819 struct bin_attribute *bin_attr, char *buf,
820 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Geliang Tang554a6032015-12-23 20:28:13 +0800822 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400824 /* Only support 1, 2 or 4 byte accesses */
825 if (count != 1 && count != 2 && count != 4)
826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400828 return pci_legacy_read(bus, off, (u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831/**
832 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700833 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700835 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * @buf: buffer containing value to be written
837 * @off: offset into legacy I/O port space
838 * @count: number of bytes to write
839 *
840 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
841 * callback routine (pci_legacy_write).
842 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400843static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
844 struct bin_attribute *bin_attr, char *buf,
845 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Geliang Tang554a6032015-12-23 20:28:13 +0800847 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400849 /* Only support 1, 2 or 4 byte accesses */
850 if (count != 1 && count != 2 && count != 4)
851 return -EINVAL;
852
853 return pci_legacy_write(bus, off, *(u32 *)buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856/**
857 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700858 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * @kobj: kobject corresponding to device to be mapped
860 * @attr: struct bin_attribute for this file
861 * @vma: struct vm_area_struct passed to mmap
862 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000863 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * legacy memory space (first meg of bus space) into application virtual
865 * memory space.
866 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400867static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
868 struct bin_attribute *attr,
869 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Geliang Tang554a6032015-12-23 20:28:13 +0800871 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400873 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000874}
875
876/**
877 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700878 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000879 * @kobj: kobject corresponding to device to be mapped
880 * @attr: struct bin_attribute for this file
881 * @vma: struct vm_area_struct passed to mmap
882 *
883 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
884 * legacy IO space (first meg of bus space) into application virtual
885 * memory space. Returns -ENOSYS if the operation isn't supported
886 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400887static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
888 struct bin_attribute *attr,
889 struct vm_area_struct *vma)
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000890{
Geliang Tang554a6032015-12-23 20:28:13 +0800891 struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000892
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400893 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000894}
895
896/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300897 * pci_adjust_legacy_attr - adjustment of legacy file attributes
898 * @b: bus to create files under
899 * @mmap_type: I/O port or memory
900 *
901 * Stub implementation. Can be overridden by arch if necessary.
902 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400903void __weak pci_adjust_legacy_attr(struct pci_bus *b,
904 enum pci_mmap_state mmap_type)
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300905{
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300906}
907
908/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000909 * pci_create_legacy_files - create legacy I/O port and memory files
910 * @b: bus to create files under
911 *
912 * Some platforms allow access to legacy I/O port and ISA memory space on
913 * a per-bus basis. This routine creates the files and ties them into
914 * their associated read, write and mmap files from pci-sysfs.c
915 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300916 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000917 * as it is ok to set up the PCI bus without these files.
918 */
919void pci_create_legacy_files(struct pci_bus *b)
920{
921 int error;
922
923 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
924 GFP_ATOMIC);
925 if (!b->legacy_io)
926 goto kzalloc_err;
927
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100928 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000929 b->legacy_io->attr.name = "legacy_io";
930 b->legacy_io->size = 0xffff;
931 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
932 b->legacy_io->read = pci_read_legacy_io;
933 b->legacy_io->write = pci_write_legacy_io;
934 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300935 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000936 error = device_create_bin_file(&b->dev, b->legacy_io);
937 if (error)
938 goto legacy_io_err;
939
940 /* Allocated above after the legacy_io struct */
941 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000942 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000943 b->legacy_mem->attr.name = "legacy_mem";
944 b->legacy_mem->size = 1024*1024;
945 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
946 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300947 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000948 error = device_create_bin_file(&b->dev, b->legacy_mem);
949 if (error)
950 goto legacy_mem_err;
951
952 return;
953
954legacy_mem_err:
955 device_remove_bin_file(&b->dev, b->legacy_io);
956legacy_io_err:
957 kfree(b->legacy_io);
958 b->legacy_io = NULL;
959kzalloc_err:
Ryan Desfosses227f0642014-04-18 20:13:50 -0400960 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 +1000961 return;
962}
963
964void pci_remove_legacy_files(struct pci_bus *b)
965{
966 if (b->legacy_io) {
967 device_remove_bin_file(&b->dev, b->legacy_io);
968 device_remove_bin_file(&b->dev, b->legacy_mem);
969 kfree(b->legacy_io); /* both are allocated here */
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972#endif /* HAVE_PCI_LEGACY */
973
974#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700975
Martin Wilck3b519e42010-11-10 11:03:21 +0100976int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
977 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700978{
Martin Wilck3b519e42010-11-10 11:03:21 +0100979 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700980
Martin Wilck3b519e42010-11-10 11:03:21 +0100981 if (pci_resource_len(pdev, resno) == 0)
982 return 0;
Libin64b00172013-04-15 02:48:54 +0000983 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700984 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800985 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800986 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100987 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
988 if (start >= pci_start && start < pci_start + size &&
989 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700990 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700991 return 0;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/**
995 * pci_mmap_resource - map a PCI resource into user memory space
996 * @kobj: kobject for mapping
997 * @attr: struct bin_attribute for the file being mapped
998 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700999 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
1001 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001003static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1004 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Geliang Tang554a6032015-12-23 20:28:13 +08001006 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +04001007 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -07001009 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001010 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001012 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1013 if (res == &pdev->resource[i])
1014 break;
1015 if (i >= PCI_ROM_RESOURCE)
1016 return -ENODEV;
1017
Bjorn Helgaasca620722016-04-07 17:15:14 -07001018 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1019 return -EINVAL;
1020
Martin Wilck3b519e42010-11-10 11:03:21 +01001021 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -04001022 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 +01001023 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
1024 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -08001025 (u64)pci_resource_start(pdev, i),
1026 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001027 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +01001028 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001029
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001030 /* pci_mmap_page_range() expects the same kind of entry as coming
1031 * from /proc/bus/pci/ which is a "user visible" value. If this is
1032 * different from the resource itself, arch will do necessary fixup.
1033 */
1034 pci_resource_to_user(pdev, i, res, &start, &end);
1035 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001037 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
1038}
1039
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001040static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1041 struct bin_attribute *attr,
1042 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001043{
1044 return pci_mmap_resource(kobj, attr, vma, 0);
1045}
1046
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001047static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1048 struct bin_attribute *attr,
1049 struct vm_area_struct *vma)
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001050{
1051 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001054static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1055 struct bin_attribute *attr, char *buf,
1056 loff_t off, size_t count, bool write)
Alex Williamson86333282010-07-19 09:45:34 -06001057{
Geliang Tang554a6032015-12-23 20:28:13 +08001058 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Alex Williamson86333282010-07-19 09:45:34 -06001059 struct resource *res = attr->private;
1060 unsigned long port = off;
1061 int i;
1062
1063 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1064 if (res == &pdev->resource[i])
1065 break;
1066 if (i >= PCI_ROM_RESOURCE)
1067 return -ENODEV;
1068
1069 port += pci_resource_start(pdev, i);
1070
1071 if (port > pci_resource_end(pdev, i))
1072 return 0;
1073
1074 if (port + count - 1 > pci_resource_end(pdev, i))
1075 return -EINVAL;
1076
1077 switch (count) {
1078 case 1:
1079 if (write)
1080 outb(*(u8 *)buf, port);
1081 else
1082 *(u8 *)buf = inb(port);
1083 return 1;
1084 case 2:
1085 if (write)
1086 outw(*(u16 *)buf, port);
1087 else
1088 *(u16 *)buf = inw(port);
1089 return 2;
1090 case 4:
1091 if (write)
1092 outl(*(u32 *)buf, port);
1093 else
1094 *(u32 *)buf = inl(port);
1095 return 4;
1096 }
1097 return -EINVAL;
1098}
1099
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001100static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1101 struct bin_attribute *attr, char *buf,
1102 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001103{
1104 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1105}
1106
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001107static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1108 struct bin_attribute *attr, char *buf,
1109 loff_t off, size_t count)
Alex Williamson86333282010-07-19 09:45:34 -06001110{
1111 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001115 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001116 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001117 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001118 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001119 * free their resources.
1120 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001121static void pci_remove_resource_files(struct pci_dev *pdev)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001122{
1123 int i;
1124
1125 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1126 struct bin_attribute *res_attr;
1127
1128 res_attr = pdev->res_attr[i];
1129 if (res_attr) {
1130 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1131 kfree(res_attr);
1132 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001133
1134 res_attr = pdev->res_attr_wc[i];
1135 if (res_attr) {
1136 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1137 kfree(res_attr);
1138 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001139 }
1140}
1141
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001142static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1143{
1144 /* allocate attribute structure, piggyback attribute name */
1145 int name_len = write_combine ? 13 : 10;
1146 struct bin_attribute *res_attr;
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001147 char *res_attr_name;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001148 int retval;
1149
1150 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001151 if (!res_attr)
1152 return -ENOMEM;
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001153
Bjorn Helgaasbd5174d2016-03-10 14:36:34 -06001154 res_attr_name = (char *)(res_attr + 1);
1155
1156 sysfs_bin_attr_init(res_attr);
1157 if (write_combine) {
1158 pdev->res_attr_wc[num] = res_attr;
1159 sprintf(res_attr_name, "resource%d_wc", num);
1160 res_attr->mmap = pci_mmap_resource_wc;
1161 } else {
1162 pdev->res_attr[num] = res_attr;
1163 sprintf(res_attr_name, "resource%d", num);
1164 res_attr->mmap = pci_mmap_resource_uc;
1165 }
1166 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1167 res_attr->read = pci_read_resource_io;
1168 res_attr->write = pci_write_resource_io;
1169 }
1170 res_attr->attr.name = res_attr_name;
1171 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1172 res_attr->size = pci_resource_len(pdev, num);
1173 res_attr->private = &pdev->resource[num];
1174 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1175 if (retval)
1176 kfree(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001177
1178 return retval;
1179}
1180
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001181/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001183 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001185 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001187static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001190 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* Expose the PCI resources from this device as files */
1193 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 /* skip empty resources */
1196 if (!pci_resource_len(pdev, i))
1197 continue;
1198
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001199 retval = pci_create_attr(pdev, i, 0);
1200 /* for prefetchable resources, create a WC mappable file */
1201 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1202 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001203
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001204 if (retval) {
1205 pci_remove_resource_files(pdev);
1206 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001212int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1213void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214#endif /* HAVE_PCI_MMAP */
1215
1216/**
1217 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001218 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001220 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * @buf: user input
1222 * @off: file offset
1223 * @count: number of byte in input
1224 *
1225 * writing anything except 0 enables it
1226 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001227static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1228 struct bin_attribute *bin_attr, char *buf,
1229 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Geliang Tang554a6032015-12-23 20:28:13 +08001231 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if ((off == 0) && (*buf == '0') && (count == 2))
1234 pdev->rom_attr_enabled = 0;
1235 else
1236 pdev->rom_attr_enabled = 1;
1237
1238 return count;
1239}
1240
1241/**
1242 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001243 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001245 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * @buf: where to put the data we read from the ROM
1247 * @off: file offset
1248 * @count: number of bytes to read
1249 *
1250 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1251 * device corresponding to @kobj.
1252 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001253static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1254 struct bin_attribute *bin_attr, char *buf,
1255 loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Geliang Tang554a6032015-12-23 20:28:13 +08001257 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 void __iomem *rom;
1259 size_t size;
1260
1261 if (!pdev->rom_attr_enabled)
1262 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001265 if (!rom || !size)
1266 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (off >= size)
1269 count = 0;
1270 else {
1271 if (off + count > size)
1272 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 memcpy_fromio(buf, rom + off, count);
1275 }
1276 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return count;
1279}
1280
1281static struct bin_attribute pci_config_attr = {
1282 .attr = {
1283 .name = "config",
1284 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001286 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 .read = pci_read_config,
1288 .write = pci_write_config,
1289};
1290
1291static struct bin_attribute pcie_config_attr = {
1292 .attr = {
1293 .name = "config",
1294 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001296 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 .read = pci_read_config,
1298 .write = pci_write_config,
1299};
1300
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001301static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1302 const char *buf, size_t count)
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001303{
1304 struct pci_dev *pdev = to_pci_dev(dev);
1305 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001306 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001307
1308 if (result < 0)
1309 return result;
1310
1311 if (val != 1)
1312 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001313
1314 result = pci_reset_function(pdev);
1315 if (result < 0)
1316 return result;
1317
1318 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001319}
1320
1321static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1322
Zhao, Yu280c73d2008-10-13 20:01:00 +08001323static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1324{
1325 int retval;
1326 struct bin_attribute *attr;
1327
1328 /* If the device has VPD, try to expose it in sysfs. */
1329 if (dev->vpd) {
1330 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1331 if (!attr)
1332 return -ENOMEM;
1333
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001334 sysfs_bin_attr_init(attr);
Hannes Reinecke104daa72016-02-15 09:42:01 +01001335 attr->size = 0;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001336 attr->attr.name = "vpd";
1337 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001338 attr->read = read_vpd_attr;
1339 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001340 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1341 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001342 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001343 return retval;
1344 }
1345 dev->vpd->attr = attr;
1346 }
1347
1348 /* Active State Power Management */
1349 pcie_aspm_create_sysfs_dev_files(dev);
1350
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001351 if (!pci_probe_reset_function(dev)) {
1352 retval = device_create_file(&dev->dev, &reset_attr);
1353 if (retval)
1354 goto error;
1355 dev->reset_fn = 1;
1356 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001357 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001358
1359error:
1360 pcie_aspm_remove_sysfs_dev_files(dev);
1361 if (dev->vpd && dev->vpd->attr) {
1362 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1363 kfree(dev->vpd->attr);
1364 }
1365
1366 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001367}
1368
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001369int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001371 int retval;
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001372 int rom_size;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001373 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!sysfs_initialized)
1376 return -EACCES;
1377
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001378 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001379 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001380 else
1381 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001382 if (retval)
1383 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001385 retval = pci_create_resource_files(pdev);
1386 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001387 goto err_config_file;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* If the device has a ROM, try to expose it in sysfs. */
Bjorn Helgaasac0c3022016-03-12 05:48:08 -06001390 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001391 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001392 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001393 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001394 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001395 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001397 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001398 attr->size = rom_size;
1399 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001400 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001401 attr->read = pci_read_rom;
1402 attr->write = pci_write_rom;
1403 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1404 if (retval) {
1405 kfree(attr);
1406 goto err_resource_files;
1407 }
1408 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001410
Zhao, Yu280c73d2008-10-13 20:01:00 +08001411 /* add sysfs entries for various capabilities */
1412 retval = pci_create_capabilities_sysfs(pdev);
1413 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001414 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001415
Narendra K911e1c92010-07-26 05:56:50 -05001416 pci_create_firmware_label_files(pdev);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001419
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001420err_rom_file:
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001421 if (pdev->rom_attr) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001422 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001423 kfree(pdev->rom_attr);
1424 pdev->rom_attr = NULL;
1425 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001426err_resource_files:
1427 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001428err_config_file:
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001429 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001430 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001431 else
1432 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001433err:
1434 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Zhao, Yu280c73d2008-10-13 20:01:00 +08001437static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1438{
1439 if (dev->vpd && dev->vpd->attr) {
1440 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1441 kfree(dev->vpd->attr);
1442 }
1443
1444 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001445 if (dev->reset_fn) {
1446 device_remove_file(&dev->dev, &reset_attr);
1447 dev->reset_fn = 0;
1448 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/**
1452 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1453 * @pdev: device whose entries we should free
1454 *
1455 * Cleanup when @pdev is removed from sysfs.
1456 */
1457void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1458{
David Millerd67afe52006-11-10 12:27:48 -08001459 if (!sysfs_initialized)
1460 return;
1461
Zhao, Yu280c73d2008-10-13 20:01:00 +08001462 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001463
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001464 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
Jason S. McMullanc20aecf2015-09-30 15:35:05 +09001466 else
1467 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 pci_remove_resource_files(pdev);
1470
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001471 if (pdev->rom_attr) {
Zhao, Yu280c73d2008-10-13 20:01:00 +08001472 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1473 kfree(pdev->rom_attr);
Bjorn Helgaas9d88b932016-03-01 09:37:10 -06001474 pdev->rom_attr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Narendra K911e1c92010-07-26 05:56:50 -05001476
1477 pci_remove_firmware_label_files(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static int __init pci_sysfs_init(void)
1481{
1482 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001483 int retval;
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001486 for_each_pci_dev(pdev) {
1487 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001488 if (retval) {
1489 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001490 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001491 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 return 0;
1495}
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001496late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001497
1498static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001499 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001500 NULL,
1501};
1502
1503static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001504 struct attribute *a, int n)
Yinghai Lu4e15c462012-11-05 15:20:34 -05001505{
Geliang Tang554a6032015-12-23 20:28:13 +08001506 struct device *dev = kobj_to_dev(kobj);
Yinghai Lu625e1d52012-11-05 15:20:35 -05001507 struct pci_dev *pdev = to_pci_dev(dev);
1508
1509 if (a == &vga_attr.attr)
1510 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1511 return 0;
1512
Yinghai Lu4e15c462012-11-05 15:20:34 -05001513 return a->mode;
1514}
1515
Jiang Liudfab88b2013-05-31 12:21:31 +08001516static struct attribute *pci_dev_hp_attrs[] = {
1517 &dev_remove_attr.attr,
1518 &dev_rescan_attr.attr,
1519 NULL,
1520};
1521
1522static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001523 struct attribute *a, int n)
Jiang Liudfab88b2013-05-31 12:21:31 +08001524{
Geliang Tang554a6032015-12-23 20:28:13 +08001525 struct device *dev = kobj_to_dev(kobj);
Jiang Liudfab88b2013-05-31 12:21:31 +08001526 struct pci_dev *pdev = to_pci_dev(dev);
1527
1528 if (pdev->is_virtfn)
1529 return 0;
1530
1531 return a->mode;
1532}
1533
1534static struct attribute_group pci_dev_hp_attr_group = {
1535 .attrs = pci_dev_hp_attrs,
1536 .is_visible = pci_dev_hp_attrs_are_visible,
1537};
1538
Donald Dutile17893822012-11-05 15:20:36 -05001539#ifdef CONFIG_PCI_IOV
1540static struct attribute *sriov_dev_attrs[] = {
1541 &sriov_totalvfs_attr.attr,
1542 &sriov_numvfs_attr.attr,
1543 NULL,
1544};
1545
1546static umode_t sriov_attrs_are_visible(struct kobject *kobj,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001547 struct attribute *a, int n)
Donald Dutile17893822012-11-05 15:20:36 -05001548{
Geliang Tang554a6032015-12-23 20:28:13 +08001549 struct device *dev = kobj_to_dev(kobj);
Donald Dutile17893822012-11-05 15:20:36 -05001550
1551 if (!dev_is_pf(dev))
1552 return 0;
1553
1554 return a->mode;
1555}
1556
1557static struct attribute_group sriov_dev_attr_group = {
1558 .attrs = sriov_dev_attrs,
1559 .is_visible = sriov_attrs_are_visible,
1560};
1561#endif /* CONFIG_PCI_IOV */
1562
Yinghai Lu4e15c462012-11-05 15:20:34 -05001563static struct attribute_group pci_dev_attr_group = {
1564 .attrs = pci_dev_dev_attrs,
1565 .is_visible = pci_dev_attrs_are_visible,
1566};
1567
1568static const struct attribute_group *pci_dev_attr_groups[] = {
1569 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001570 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001571#ifdef CONFIG_PCI_IOV
1572 &sriov_dev_attr_group,
1573#endif
Yinghai Lu4e15c462012-11-05 15:20:34 -05001574 NULL,
1575};
1576
1577struct device_type pci_dev_type = {
1578 .groups = pci_dev_attr_groups,
1579};