blob: faa4ab554d68acac61f8e34036a34cdd4e5613bf [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "pci.h"
33
34static int sysfs_initialized; /* = 0 */
35
36/* show configuration fields */
37#define pci_config_attr(field, format_string) \
38static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040039field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{ \
41 struct pci_dev *pdev; \
42 \
43 pdev = to_pci_dev (dev); \
44 return sprintf (buf, format_string, pdev->field); \
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070045} \
46static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48pci_config_attr(vendor, "0x%04x\n");
49pci_config_attr(device, "0x%04x\n");
50pci_config_attr(subsystem_vendor, "0x%04x\n");
51pci_config_attr(subsystem_device, "0x%04x\n");
52pci_config_attr(class, "0x%06x\n");
53pci_config_attr(irq, "%u\n");
54
Doug Thompsonbdee9d92006-06-14 16:59:48 -070055static ssize_t broken_parity_status_show(struct device *dev,
56 struct device_attribute *attr,
57 char *buf)
58{
59 struct pci_dev *pdev = to_pci_dev(dev);
60 return sprintf (buf, "%u\n", pdev->broken_parity_status);
61}
62
63static ssize_t broken_parity_status_store(struct device *dev,
64 struct device_attribute *attr,
65 const char *buf, size_t count)
66{
67 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080068 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070069
Jingoo Han9a994e82013-06-01 16:25:25 +090070 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080071 return -EINVAL;
72
73 pdev->broken_parity_status = !!val;
74
75 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070076}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070077static DEVICE_ATTR_RW(broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070078
Yijing Wangc489f5f2013-09-30 15:02:38 +080079static ssize_t pci_dev_show_local_cpu(struct device *dev,
80 int type,
81 struct device_attribute *attr,
82 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 int len;
86
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
Yijing Wangc489f5f2013-09-30 15:02:38 +080093 len = type ?
94 cpumask_scnprintf(buf, PAGE_SIZE-2, mask) :
95 cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
96
Mike Travis39106dc2008-04-08 11:43:03 -070097 buf[len++] = '\n';
98 buf[len] = '\0';
99 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Yijing Wangc489f5f2013-09-30 15:02:38 +0800102static ssize_t local_cpus_show(struct device *dev,
103 struct device_attribute *attr, char *buf)
104{
105 return pci_dev_show_local_cpu(dev, 1, attr, buf);
106}
Bjorn Helgaas33de1b82013-10-31 14:12:40 -0600107static DEVICE_ATTR_RO(local_cpus);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800108
109static ssize_t local_cpulist_show(struct device *dev,
110 struct device_attribute *attr, char *buf)
111{
112 return pci_dev_show_local_cpu(dev, 0, attr, buf);
113}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700114static DEVICE_ATTR_RO(local_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700116/*
117 * PCI Bus Class Devices
118 */
119static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
120 int type,
121 struct device_attribute *attr,
122 char *buf)
123{
124 int ret;
125 const struct cpumask *cpumask;
126
127 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
128 ret = type ?
129 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
130 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
131 buf[ret++] = '\n';
132 buf[ret] = '\0';
133 return ret;
134}
135
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700136static ssize_t cpuaffinity_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700138{
139 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
140}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700141static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700142
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700143static ssize_t cpulistaffinity_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700145{
146 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
147}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700148static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* show resources */
151static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400152resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct pci_dev * pci_dev = to_pci_dev(dev);
155 char * str = buf;
156 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800157 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700158 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (pci_dev->subordinate)
161 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800162 else
163 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000166 struct resource *res = &pci_dev->resource[i];
167 pci_resource_to_user(pci_dev, i, res, &start, &end);
168 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
169 (unsigned long long)start,
170 (unsigned long long)end,
171 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 return (str - buf);
174}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700175static DEVICE_ATTR_RO(resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200177static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700178{
179 struct pci_dev *pci_dev = to_pci_dev(dev);
180
181 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
182 pci_dev->vendor, pci_dev->device,
183 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
184 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
185 (u8)(pci_dev->class));
186}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700187static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800188
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700189static ssize_t enabled_store(struct device *dev,
190 struct device_attribute *attr, const char *buf,
191 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200192{
193 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800194 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900195 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800196
197 if (result < 0)
198 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200199
200 /* this can crash the machine when done on the "wrong" device */
201 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800202 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200203
Trent Piepho92425a42008-11-30 17:10:12 -0800204 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900205 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800206 pci_disable_device(pdev);
207 else
208 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800209 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800210 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200211
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800212 return result < 0 ? result : count;
213}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200214
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700215static ssize_t enabled_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800217{
218 struct pci_dev *pdev;
219
220 pdev = to_pci_dev (dev);
221 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200222}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700223static DEVICE_ATTR_RW(enabled);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200224
Brice Goglin81bb0e12007-01-28 10:53:40 +0100225#ifdef CONFIG_NUMA
226static ssize_t
227numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
228{
229 return sprintf (buf, "%d\n", dev->numa_node);
230}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700231static DEVICE_ATTR_RO(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100232#endif
233
Brice Goglinfe970642006-08-31 01:55:15 -0400234static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800235dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
236{
237 struct pci_dev *pdev = to_pci_dev(dev);
238
239 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
240}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700241static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800242
243static ssize_t
244consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
245 char *buf)
246{
247 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
248}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700249static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800250
251static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400252msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
253{
254 struct pci_dev *pdev = to_pci_dev(dev);
255
256 if (!pdev->subordinate)
257 return 0;
258
259 return sprintf (buf, "%u\n",
260 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
261}
262
263static ssize_t
264msi_bus_store(struct device *dev, struct device_attribute *attr,
265 const char *buf, size_t count)
266{
267 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800268 unsigned long val;
269
Jingoo Han9a994e82013-06-01 16:25:25 +0900270 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800271 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400272
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700273 /*
274 * Bad things may happen if the no_msi flag is changed
275 * while drivers are loaded.
276 */
Brice Goglinfe970642006-08-31 01:55:15 -0400277 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800278 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400279
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700280 /*
281 * Maybe devices without subordinate buses shouldn't have this
282 * attribute in the first place?
283 */
Brice Goglinfe970642006-08-31 01:55:15 -0400284 if (!pdev->subordinate)
285 return count;
286
Trent Piepho92425a42008-11-30 17:10:12 -0800287 /* Is the flag going to change, or keep the value it already had? */
288 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
289 !!val) {
290 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400291
Trent Piepho92425a42008-11-30 17:10:12 -0800292 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
293 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400294 }
295
296 return count;
297}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700298static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700299
Alex Chiang705b1aa2009-03-20 14:56:31 -0600300static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
301 size_t count)
302{
303 unsigned long val;
304 struct pci_bus *b = NULL;
305
Jingoo Han9a994e82013-06-01 16:25:25 +0900306 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600307 return -EINVAL;
308
309 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100310 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600311 while ((b = pci_find_next_bus(b)) != NULL)
312 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100313 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600314 }
315 return count;
316}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600317static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600318
Sachin Kamatbf22c902013-09-28 15:42:00 +0530319static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600320 &bus_attr_rescan.attr,
321 NULL,
322};
323
324static const struct attribute_group pci_bus_group = {
325 .attrs = pci_bus_attrs,
326};
327
328const struct attribute_group *pci_bus_groups[] = {
329 &pci_bus_group,
330 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600331};
Alex Chiang77c27c72009-03-20 14:56:36 -0600332
Alex Chiang738a6392009-03-20 14:56:41 -0600333static ssize_t
334dev_rescan_store(struct device *dev, struct device_attribute *attr,
335 const char *buf, size_t count)
336{
337 unsigned long val;
338 struct pci_dev *pdev = to_pci_dev(dev);
339
Jingoo Han9a994e82013-06-01 16:25:25 +0900340 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600341 return -EINVAL;
342
343 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100344 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600345 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100346 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600347 }
348 return count;
349}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530350static struct device_attribute dev_rescan_attr = __ATTR(rescan,
351 (S_IWUSR|S_IWGRP),
352 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600353
Alex Chiang77c27c72009-03-20 14:56:36 -0600354static ssize_t
Tejun Heobc6caf02014-02-03 14:03:02 -0500355remove_store(struct device *dev, struct device_attribute *attr,
Alex Chiang77c27c72009-03-20 14:56:36 -0600356 const char *buf, size_t count)
357{
Alex Chiang77c27c72009-03-20 14:56:36 -0600358 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600359
Jingoo Han9a994e82013-06-01 16:25:25 +0900360 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600361 return -EINVAL;
362
Tejun Heobc6caf02014-02-03 14:03:02 -0500363 if (val && device_remove_file_self(dev, attr))
364 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600365 return count;
366}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530367static struct device_attribute dev_remove_attr = __ATTR(remove,
368 (S_IWUSR|S_IWGRP),
369 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700370
371static ssize_t
372dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
373 const char *buf, size_t count)
374{
375 unsigned long val;
376 struct pci_bus *bus = to_pci_bus(dev);
377
Jingoo Han9a994e82013-06-01 16:25:25 +0900378 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700379 return -EINVAL;
380
381 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100382 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800383 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
384 pci_rescan_bus_bridge_resize(bus->self);
385 else
386 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100387 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700388 }
389 return count;
390}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700391static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700392
Huang Ying448bd852012-06-23 10:23:51 +0800393#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
394static ssize_t d3cold_allowed_store(struct device *dev,
395 struct device_attribute *attr,
396 const char *buf, size_t count)
397{
398 struct pci_dev *pdev = to_pci_dev(dev);
399 unsigned long val;
400
Jingoo Han9a994e82013-06-01 16:25:25 +0900401 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800402 return -EINVAL;
403
404 pdev->d3cold_allowed = !!val;
405 pm_runtime_resume(dev);
406
407 return count;
408}
409
410static ssize_t d3cold_allowed_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
413 struct pci_dev *pdev = to_pci_dev(dev);
414 return sprintf (buf, "%u\n", pdev->d3cold_allowed);
415}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700416static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800417#endif
418
Donald Dutile17893822012-11-05 15:20:36 -0500419#ifdef CONFIG_PCI_IOV
420static ssize_t sriov_totalvfs_show(struct device *dev,
421 struct device_attribute *attr,
422 char *buf)
423{
424 struct pci_dev *pdev = to_pci_dev(dev);
425
Donald Dutilebff73152012-11-05 15:20:37 -0500426 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500427}
428
429
430static ssize_t sriov_numvfs_show(struct device *dev,
431 struct device_attribute *attr,
432 char *buf)
433{
434 struct pci_dev *pdev = to_pci_dev(dev);
435
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700436 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500437}
438
439/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700440 * num_vfs > 0; number of VFs to enable
441 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500442 *
443 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700444 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500445 */
446static ssize_t sriov_numvfs_store(struct device *dev,
447 struct device_attribute *attr,
448 const char *buf, size_t count)
449{
450 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700451 int ret;
452 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500453
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700454 ret = kstrtou16(buf, 0, &num_vfs);
455 if (ret < 0)
456 return ret;
457
458 if (num_vfs > pci_sriov_get_totalvfs(pdev))
459 return -ERANGE;
460
461 if (num_vfs == pdev->sriov->num_VFs)
462 return count; /* no change */
Donald Dutile17893822012-11-05 15:20:36 -0500463
464 /* is PF driver loaded w/callback */
465 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700466 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Donald Dutile17893822012-11-05 15:20:36 -0500467 return -ENOSYS;
468 }
469
Donald Dutile17893822012-11-05 15:20:36 -0500470 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700471 /* disable VFs */
472 ret = pdev->driver->sriov_configure(pdev, 0);
473 if (ret < 0)
474 return ret;
475 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500476 }
477
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700478 /* enable VFs */
479 if (pdev->sriov->num_VFs) {
480 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
481 pdev->sriov->num_VFs, num_vfs);
482 return -EBUSY;
483 }
Donald Dutile17893822012-11-05 15:20:36 -0500484
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700485 ret = pdev->driver->sriov_configure(pdev, num_vfs);
486 if (ret < 0)
487 return ret;
488
489 if (ret != num_vfs)
490 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
491 num_vfs, ret);
492
493 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500494}
495
496static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
497static struct device_attribute sriov_numvfs_attr =
498 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
499 sriov_numvfs_show, sriov_numvfs_store);
500#endif /* CONFIG_PCI_IOV */
501
Alex Williamson782a9852014-05-20 08:53:21 -0600502static ssize_t driver_override_store(struct device *dev,
503 struct device_attribute *attr,
504 const char *buf, size_t count)
505{
506 struct pci_dev *pdev = to_pci_dev(dev);
507 char *driver_override, *old = pdev->driver_override, *cp;
508
509 if (count > PATH_MAX)
510 return -EINVAL;
511
512 driver_override = kstrndup(buf, count, GFP_KERNEL);
513 if (!driver_override)
514 return -ENOMEM;
515
516 cp = strchr(driver_override, '\n');
517 if (cp)
518 *cp = '\0';
519
520 if (strlen(driver_override)) {
521 pdev->driver_override = driver_override;
522 } else {
523 kfree(driver_override);
524 pdev->driver_override = NULL;
525 }
526
527 kfree(old);
528
529 return count;
530}
531
532static ssize_t driver_override_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
534{
535 struct pci_dev *pdev = to_pci_dev(dev);
536
537 return sprintf(buf, "%s\n", pdev->driver_override);
538}
539static DEVICE_ATTR_RW(driver_override);
540
Sachin Kamatbf22c902013-09-28 15:42:00 +0530541static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700542 &dev_attr_resource.attr,
543 &dev_attr_vendor.attr,
544 &dev_attr_device.attr,
545 &dev_attr_subsystem_vendor.attr,
546 &dev_attr_subsystem_device.attr,
547 &dev_attr_class.attr,
548 &dev_attr_irq.attr,
549 &dev_attr_local_cpus.attr,
550 &dev_attr_local_cpulist.attr,
551 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100552#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700553 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100554#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700555 &dev_attr_dma_mask_bits.attr,
556 &dev_attr_consistent_dma_mask_bits.attr,
557 &dev_attr_enabled.attr,
558 &dev_attr_broken_parity_status.attr,
559 &dev_attr_msi_bus.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800560#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700561 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800562#endif
Alex Williamson782a9852014-05-20 08:53:21 -0600563 &dev_attr_driver_override.attr,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700564 NULL,
565};
566
567static const struct attribute_group pci_dev_group = {
568 .attrs = pci_dev_attrs,
569};
570
571const struct attribute_group *pci_dev_groups[] = {
572 &pci_dev_group,
573 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574};
575
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700576static struct attribute *pcibus_attrs[] = {
577 &dev_attr_rescan.attr,
578 &dev_attr_cpuaffinity.attr,
579 &dev_attr_cpulistaffinity.attr,
580 NULL,
581};
582
583static const struct attribute_group pcibus_group = {
584 .attrs = pcibus_attrs,
585};
586
587const struct attribute_group *pcibus_groups[] = {
588 &pcibus_group,
589 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700590};
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000593boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
594{
595 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400596 struct pci_dev *vga_dev = vga_default_device();
597
598 if (vga_dev)
599 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000600
601 return sprintf(buf, "%u\n",
602 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
603 IORESOURCE_ROM_SHADOW));
604}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530605static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000606
607static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700608pci_read_config(struct file *filp, struct kobject *kobj,
609 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800610 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
613 unsigned int size = 64;
614 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900615 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* Several chips lock up trying to read undefined config space */
Eric Parisb7e724d2012-01-03 12:25:15 -0500618 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 size = dev->cfg_size;
620 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
621 size = 128;
622 }
623
624 if (off > size)
625 return 0;
626 if (off + count > size) {
627 size -= off;
628 count = size;
629 } else {
630 size = count;
631 }
632
Huang Ying3d8387e2012-08-15 09:43:03 +0800633 pci_config_pm_runtime_get(dev);
634
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900635 if ((off & 1) && size) {
636 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700637 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900638 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900640 size--;
641 }
642
643 if ((off & 3) && size > 2) {
644 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700645 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900646 data[off - init_off] = val & 0xff;
647 data[off - init_off + 1] = (val >> 8) & 0xff;
648 off += 2;
649 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900653 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700654 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900655 data[off - init_off] = val & 0xff;
656 data[off - init_off + 1] = (val >> 8) & 0xff;
657 data[off - init_off + 2] = (val >> 16) & 0xff;
658 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 off += 4;
660 size -= 4;
661 }
662
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900663 if (size >= 2) {
664 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700665 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900666 data[off - init_off] = val & 0xff;
667 data[off - init_off + 1] = (val >> 8) & 0xff;
668 off += 2;
669 size -= 2;
670 }
671
672 if (size > 0) {
673 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700674 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900675 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 off++;
677 --size;
678 }
679
Huang Ying3d8387e2012-08-15 09:43:03 +0800680 pci_config_pm_runtime_put(dev);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return count;
683}
684
685static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700686pci_write_config(struct file* filp, struct kobject *kobj,
687 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800688 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
691 unsigned int size = count;
692 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900693 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (off > dev->cfg_size)
696 return 0;
697 if (off + count > dev->cfg_size) {
698 size = dev->cfg_size - off;
699 count = size;
700 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700701
Huang Ying3d8387e2012-08-15 09:43:03 +0800702 pci_config_pm_runtime_get(dev);
703
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900704 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700705 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900707 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700709
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900710 if ((off & 3) && size > 2) {
711 u16 val = data[off - init_off];
712 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700713 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900714 off += 2;
715 size -= 2;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900719 u32 val = data[off - init_off];
720 val |= (u32) data[off - init_off + 1] << 8;
721 val |= (u32) data[off - init_off + 2] << 16;
722 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700723 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 off += 4;
725 size -= 4;
726 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700727
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900728 if (size >= 2) {
729 u16 val = data[off - init_off];
730 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700731 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900732 off += 2;
733 size -= 2;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900736 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700737 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 off++;
739 --size;
740 }
741
Huang Ying3d8387e2012-08-15 09:43:03 +0800742 pci_config_pm_runtime_put(dev);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return count;
745}
746
Ben Hutchings94e61082008-03-05 16:52:39 +0000747static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700748read_vpd_attr(struct file *filp, struct kobject *kobj,
749 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000750 char *buf, loff_t off, size_t count)
751{
752 struct pci_dev *dev =
753 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000754
755 if (off > bin_attr->size)
756 count = 0;
757 else if (count > bin_attr->size - off)
758 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000759
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800760 return pci_read_vpd(dev, off, count, buf);
761}
Ben Hutchings94e61082008-03-05 16:52:39 +0000762
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800763static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700764write_vpd_attr(struct file *filp, struct kobject *kobj,
765 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800766 char *buf, loff_t off, size_t count)
767{
768 struct pci_dev *dev =
769 to_pci_dev(container_of(kobj, struct device, kobj));
770
771 if (off > bin_attr->size)
772 count = 0;
773 else if (count > bin_attr->size - off)
774 count = bin_attr->size - off;
775
776 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779#ifdef HAVE_PCI_LEGACY
780/**
781 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700782 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700784 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * @buf: buffer to store results
786 * @off: offset into legacy I/O port space
787 * @count: number of bytes to read
788 *
789 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
790 * callback routine (pci_legacy_read).
791 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000792static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700793pci_read_legacy_io(struct file *filp, struct kobject *kobj,
794 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800795 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400798 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 kobj));
800
801 /* Only support 1, 2 or 4 byte accesses */
802 if (count != 1 && count != 2 && count != 4)
803 return -EINVAL;
804
805 return pci_legacy_read(bus, off, (u32 *)buf, count);
806}
807
808/**
809 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700810 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700812 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * @buf: buffer containing value to be written
814 * @off: offset into legacy I/O port space
815 * @count: number of bytes to write
816 *
817 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
818 * callback routine (pci_legacy_write).
819 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000820static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700821pci_write_legacy_io(struct file *filp, struct kobject *kobj,
822 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800823 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400826 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 kobj));
828 /* Only support 1, 2 or 4 byte accesses */
829 if (count != 1 && count != 2 && count != 4)
830 return -EINVAL;
831
832 return pci_legacy_write(bus, off, *(u32 *)buf, count);
833}
834
835/**
836 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700837 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * @kobj: kobject corresponding to device to be mapped
839 * @attr: struct bin_attribute for this file
840 * @vma: struct vm_area_struct passed to mmap
841 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000842 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * legacy memory space (first meg of bus space) into application virtual
844 * memory space.
845 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000846static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700847pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
848 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct vm_area_struct *vma)
850{
851 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400852 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 kobj));
854
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000855 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
856}
857
858/**
859 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700860 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000861 * @kobj: kobject corresponding to device to be mapped
862 * @attr: struct bin_attribute for this file
863 * @vma: struct vm_area_struct passed to mmap
864 *
865 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
866 * legacy IO space (first meg of bus space) into application virtual
867 * memory space. Returns -ENOSYS if the operation isn't supported
868 */
869static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700870pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
871 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000872 struct vm_area_struct *vma)
873{
874 struct pci_bus *bus = to_pci_bus(container_of(kobj,
875 struct device,
876 kobj));
877
878 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
879}
880
881/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300882 * pci_adjust_legacy_attr - adjustment of legacy file attributes
883 * @b: bus to create files under
884 * @mmap_type: I/O port or memory
885 *
886 * Stub implementation. Can be overridden by arch if necessary.
887 */
888void __weak
889pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
890{
891 return;
892}
893
894/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000895 * pci_create_legacy_files - create legacy I/O port and memory files
896 * @b: bus to create files under
897 *
898 * Some platforms allow access to legacy I/O port and ISA memory space on
899 * a per-bus basis. This routine creates the files and ties them into
900 * their associated read, write and mmap files from pci-sysfs.c
901 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300902 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000903 * as it is ok to set up the PCI bus without these files.
904 */
905void pci_create_legacy_files(struct pci_bus *b)
906{
907 int error;
908
909 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
910 GFP_ATOMIC);
911 if (!b->legacy_io)
912 goto kzalloc_err;
913
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100914 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000915 b->legacy_io->attr.name = "legacy_io";
916 b->legacy_io->size = 0xffff;
917 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
918 b->legacy_io->read = pci_read_legacy_io;
919 b->legacy_io->write = pci_write_legacy_io;
920 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300921 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000922 error = device_create_bin_file(&b->dev, b->legacy_io);
923 if (error)
924 goto legacy_io_err;
925
926 /* Allocated above after the legacy_io struct */
927 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000928 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000929 b->legacy_mem->attr.name = "legacy_mem";
930 b->legacy_mem->size = 1024*1024;
931 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
932 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300933 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000934 error = device_create_bin_file(&b->dev, b->legacy_mem);
935 if (error)
936 goto legacy_mem_err;
937
938 return;
939
940legacy_mem_err:
941 device_remove_bin_file(&b->dev, b->legacy_io);
942legacy_io_err:
943 kfree(b->legacy_io);
944 b->legacy_io = NULL;
945kzalloc_err:
946 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
947 "and ISA memory resources to sysfs\n");
948 return;
949}
950
951void pci_remove_legacy_files(struct pci_bus *b)
952{
953 if (b->legacy_io) {
954 device_remove_bin_file(&b->dev, b->legacy_io);
955 device_remove_bin_file(&b->dev, b->legacy_mem);
956 kfree(b->legacy_io); /* both are allocated here */
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959#endif /* HAVE_PCI_LEGACY */
960
961#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700962
Martin Wilck3b519e42010-11-10 11:03:21 +0100963int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
964 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700965{
Martin Wilck3b519e42010-11-10 11:03:21 +0100966 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700967
Martin Wilck3b519e42010-11-10 11:03:21 +0100968 if (pci_resource_len(pdev, resno) == 0)
969 return 0;
Libin64b00172013-04-15 02:48:54 +0000970 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700971 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800972 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800973 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100974 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
975 if (start >= pci_start && start < pci_start + size &&
976 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700977 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700978 return 0;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/**
982 * pci_mmap_resource - map a PCI resource into user memory space
983 * @kobj: kobject for mapping
984 * @attr: struct bin_attribute for the file being mapped
985 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700986 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 *
988 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
990static int
991pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700992 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
995 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400996 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700998 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000999 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001001 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1002 if (res == &pdev->resource[i])
1003 break;
1004 if (i >= PCI_ROM_RESOURCE)
1005 return -ENODEV;
1006
Martin Wilck3b519e42010-11-10 11:03:21 +01001007 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
1008 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
1009 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
1010 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
1011 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -08001012 (u64)pci_resource_start(pdev, i),
1013 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001014 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +01001015 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -07001016
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001017 /* pci_mmap_page_range() expects the same kind of entry as coming
1018 * from /proc/bus/pci/ which is a "user visible" value. If this is
1019 * different from the resource itself, arch will do necessary fixup.
1020 */
1021 pci_resource_to_user(pdev, i, res, &start, &end);
1022 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
1024
Arjan van de Vene8de1482008-10-22 19:55:31 -07001025 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
1026 return -EINVAL;
1027
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001028 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
1029}
1030
1031static int
Chris Wright2c3c8be2010-05-12 18:28:57 -07001032pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1033 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001034 struct vm_area_struct *vma)
1035{
1036 return pci_mmap_resource(kobj, attr, vma, 0);
1037}
1038
1039static int
Chris Wright2c3c8be2010-05-12 18:28:57 -07001040pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1041 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001042 struct vm_area_struct *vma)
1043{
1044 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Alex Williamson86333282010-07-19 09:45:34 -06001047static ssize_t
1048pci_resource_io(struct file *filp, struct kobject *kobj,
1049 struct bin_attribute *attr, char *buf,
1050 loff_t off, size_t count, bool write)
1051{
1052 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
1053 struct device, kobj));
1054 struct resource *res = attr->private;
1055 unsigned long port = off;
1056 int i;
1057
1058 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1059 if (res == &pdev->resource[i])
1060 break;
1061 if (i >= PCI_ROM_RESOURCE)
1062 return -ENODEV;
1063
1064 port += pci_resource_start(pdev, i);
1065
1066 if (port > pci_resource_end(pdev, i))
1067 return 0;
1068
1069 if (port + count - 1 > pci_resource_end(pdev, i))
1070 return -EINVAL;
1071
1072 switch (count) {
1073 case 1:
1074 if (write)
1075 outb(*(u8 *)buf, port);
1076 else
1077 *(u8 *)buf = inb(port);
1078 return 1;
1079 case 2:
1080 if (write)
1081 outw(*(u16 *)buf, port);
1082 else
1083 *(u16 *)buf = inw(port);
1084 return 2;
1085 case 4:
1086 if (write)
1087 outl(*(u32 *)buf, port);
1088 else
1089 *(u32 *)buf = inl(port);
1090 return 4;
1091 }
1092 return -EINVAL;
1093}
1094
1095static ssize_t
1096pci_read_resource_io(struct file *filp, struct kobject *kobj,
1097 struct bin_attribute *attr, char *buf,
1098 loff_t off, size_t count)
1099{
1100 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1101}
1102
1103static ssize_t
1104pci_write_resource_io(struct file *filp, struct kobject *kobj,
1105 struct bin_attribute *attr, char *buf,
1106 loff_t off, size_t count)
1107{
1108 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001112 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001113 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001114 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001115 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001116 * free their resources.
1117 */
1118static void
1119pci_remove_resource_files(struct pci_dev *pdev)
1120{
1121 int i;
1122
1123 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1124 struct bin_attribute *res_attr;
1125
1126 res_attr = pdev->res_attr[i];
1127 if (res_attr) {
1128 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1129 kfree(res_attr);
1130 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001131
1132 res_attr = pdev->res_attr_wc[i];
1133 if (res_attr) {
1134 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1135 kfree(res_attr);
1136 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001137 }
1138}
1139
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001140static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1141{
1142 /* allocate attribute structure, piggyback attribute name */
1143 int name_len = write_combine ? 13 : 10;
1144 struct bin_attribute *res_attr;
1145 int retval;
1146
1147 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1148 if (res_attr) {
1149 char *res_attr_name = (char *)(res_attr + 1);
1150
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001151 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001152 if (write_combine) {
1153 pdev->res_attr_wc[num] = res_attr;
1154 sprintf(res_attr_name, "resource%d_wc", num);
1155 res_attr->mmap = pci_mmap_resource_wc;
1156 } else {
1157 pdev->res_attr[num] = res_attr;
1158 sprintf(res_attr_name, "resource%d", num);
1159 res_attr->mmap = pci_mmap_resource_uc;
1160 }
Alex Williamson86333282010-07-19 09:45:34 -06001161 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1162 res_attr->read = pci_read_resource_io;
1163 res_attr->write = pci_write_resource_io;
1164 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001165 res_attr->attr.name = res_attr_name;
1166 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1167 res_attr->size = pci_resource_len(pdev, num);
1168 res_attr->private = &pdev->resource[num];
1169 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1170 } else
1171 retval = -ENOMEM;
1172
1173 return retval;
1174}
1175
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001176/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001178 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001180 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001182static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001185 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /* Expose the PCI resources from this device as files */
1188 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* skip empty resources */
1191 if (!pci_resource_len(pdev, i))
1192 continue;
1193
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001194 retval = pci_create_attr(pdev, i, 0);
1195 /* for prefetchable resources, create a WC mappable file */
1196 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1197 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001198
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001199 if (retval) {
1200 pci_remove_resource_files(pdev);
1201 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001207int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1208void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209#endif /* HAVE_PCI_MMAP */
1210
1211/**
1212 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001213 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001215 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * @buf: user input
1217 * @off: file offset
1218 * @count: number of byte in input
1219 *
1220 * writing anything except 0 enables it
1221 */
1222static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001223pci_write_rom(struct file *filp, struct kobject *kobj,
1224 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001225 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1228
1229 if ((off == 0) && (*buf == '0') && (count == 2))
1230 pdev->rom_attr_enabled = 0;
1231 else
1232 pdev->rom_attr_enabled = 1;
1233
1234 return count;
1235}
1236
1237/**
1238 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001239 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001241 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * @buf: where to put the data we read from the ROM
1243 * @off: file offset
1244 * @count: number of bytes to read
1245 *
1246 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1247 * device corresponding to @kobj.
1248 */
1249static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001250pci_read_rom(struct file *filp, struct kobject *kobj,
1251 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001252 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1255 void __iomem *rom;
1256 size_t size;
1257
1258 if (!pdev->rom_attr_enabled)
1259 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001262 if (!rom || !size)
1263 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (off >= size)
1266 count = 0;
1267 else {
1268 if (off + count > size)
1269 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 memcpy_fromio(buf, rom + off, count);
1272 }
1273 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return count;
1276}
1277
1278static struct bin_attribute pci_config_attr = {
1279 .attr = {
1280 .name = "config",
1281 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001283 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 .read = pci_read_config,
1285 .write = pci_write_config,
1286};
1287
1288static struct bin_attribute pcie_config_attr = {
1289 .attr = {
1290 .name = "config",
1291 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001293 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .read = pci_read_config,
1295 .write = pci_write_config,
1296};
1297
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001298int __weak pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001299{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001300 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001301}
1302
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001303static ssize_t reset_store(struct device *dev,
1304 struct device_attribute *attr, const char *buf,
1305 size_t count)
1306{
1307 struct pci_dev *pdev = to_pci_dev(dev);
1308 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001309 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001310
1311 if (result < 0)
1312 return result;
1313
1314 if (val != 1)
1315 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001316
1317 result = pci_reset_function(pdev);
1318 if (result < 0)
1319 return result;
1320
1321 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001322}
1323
1324static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1325
Zhao, Yu280c73d2008-10-13 20:01:00 +08001326static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1327{
1328 int retval;
1329 struct bin_attribute *attr;
1330
1331 /* If the device has VPD, try to expose it in sysfs. */
1332 if (dev->vpd) {
1333 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1334 if (!attr)
1335 return -ENOMEM;
1336
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001337 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001338 attr->size = dev->vpd->len;
1339 attr->attr.name = "vpd";
1340 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001341 attr->read = read_vpd_attr;
1342 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001343 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1344 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001345 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001346 return retval;
1347 }
1348 dev->vpd->attr = attr;
1349 }
1350
1351 /* Active State Power Management */
1352 pcie_aspm_create_sysfs_dev_files(dev);
1353
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001354 if (!pci_probe_reset_function(dev)) {
1355 retval = device_create_file(&dev->dev, &reset_attr);
1356 if (retval)
1357 goto error;
1358 dev->reset_fn = 1;
1359 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001360 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001361
1362error:
1363 pcie_aspm_remove_sysfs_dev_files(dev);
1364 if (dev->vpd && dev->vpd->attr) {
1365 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1366 kfree(dev->vpd->attr);
1367 }
1368
1369 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001370}
1371
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001372int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001374 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001375 int rom_size = 0;
1376 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!sysfs_initialized)
1379 return -EACCES;
1380
Zhao, Yu557848c2008-10-13 19:18:07 +08001381 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001382 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001384 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1385 if (retval)
1386 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001388 retval = pci_create_resource_files(pdev);
1389 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001390 goto err_config_file;
1391
1392 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1393 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1394 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1395 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001398 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001399 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001400 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001401 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001402 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001404 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001405 attr->size = rom_size;
1406 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001407 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001408 attr->read = pci_read_rom;
1409 attr->write = pci_write_rom;
1410 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1411 if (retval) {
1412 kfree(attr);
1413 goto err_resource_files;
1414 }
1415 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001419 retval = pcibios_add_platform_entries(pdev);
1420 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001421 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001422
Zhao, Yu280c73d2008-10-13 20:01:00 +08001423 /* add sysfs entries for various capabilities */
1424 retval = pci_create_capabilities_sysfs(pdev);
1425 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001426 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001427
Narendra K911e1c92010-07-26 05:56:50 -05001428 pci_create_firmware_label_files(pdev);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001431
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001432err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001433 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001434 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001435 kfree(pdev->rom_attr);
1436 pdev->rom_attr = NULL;
1437 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001438err_resource_files:
1439 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001440err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001441 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001442 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1443 else
1444 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1445err:
1446 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Zhao, Yu280c73d2008-10-13 20:01:00 +08001449static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1450{
1451 if (dev->vpd && dev->vpd->attr) {
1452 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1453 kfree(dev->vpd->attr);
1454 }
1455
1456 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001457 if (dev->reset_fn) {
1458 device_remove_file(&dev->dev, &reset_attr);
1459 dev->reset_fn = 0;
1460 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/**
1464 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1465 * @pdev: device whose entries we should free
1466 *
1467 * Cleanup when @pdev is removed from sysfs.
1468 */
1469void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1470{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001471 int rom_size = 0;
1472
David Millerd67afe52006-11-10 12:27:48 -08001473 if (!sysfs_initialized)
1474 return;
1475
Zhao, Yu280c73d2008-10-13 20:01:00 +08001476 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001477
Zhao, Yu557848c2008-10-13 19:18:07 +08001478 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1480 else
1481 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1482
1483 pci_remove_resource_files(pdev);
1484
Zhao, Yu280c73d2008-10-13 20:01:00 +08001485 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1486 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1487 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1488 rom_size = 0x20000;
1489
1490 if (rom_size && pdev->rom_attr) {
1491 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1492 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Narendra K911e1c92010-07-26 05:56:50 -05001494
1495 pci_remove_firmware_label_files(pdev);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499static int __init pci_sysfs_init(void)
1500{
1501 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001502 int retval;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001505 for_each_pci_dev(pdev) {
1506 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001507 if (retval) {
1508 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001509 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001510 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 return 0;
1514}
1515
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001516late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001517
1518static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001519 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001520 NULL,
1521};
1522
1523static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1524 struct attribute *a, int n)
1525{
Yinghai Lu625e1d52012-11-05 15:20:35 -05001526 struct device *dev = container_of(kobj, struct device, kobj);
1527 struct pci_dev *pdev = to_pci_dev(dev);
1528
1529 if (a == &vga_attr.attr)
1530 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1531 return 0;
1532
Yinghai Lu4e15c462012-11-05 15:20:34 -05001533 return a->mode;
1534}
1535
Jiang Liudfab88b2013-05-31 12:21:31 +08001536static struct attribute *pci_dev_hp_attrs[] = {
1537 &dev_remove_attr.attr,
1538 &dev_rescan_attr.attr,
1539 NULL,
1540};
1541
1542static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1543 struct attribute *a, int n)
1544{
1545 struct device *dev = container_of(kobj, struct device, kobj);
1546 struct pci_dev *pdev = to_pci_dev(dev);
1547
1548 if (pdev->is_virtfn)
1549 return 0;
1550
1551 return a->mode;
1552}
1553
1554static struct attribute_group pci_dev_hp_attr_group = {
1555 .attrs = pci_dev_hp_attrs,
1556 .is_visible = pci_dev_hp_attrs_are_visible,
1557};
1558
Donald Dutile17893822012-11-05 15:20:36 -05001559#ifdef CONFIG_PCI_IOV
1560static struct attribute *sriov_dev_attrs[] = {
1561 &sriov_totalvfs_attr.attr,
1562 &sriov_numvfs_attr.attr,
1563 NULL,
1564};
1565
1566static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1567 struct attribute *a, int n)
1568{
1569 struct device *dev = container_of(kobj, struct device, kobj);
1570
1571 if (!dev_is_pf(dev))
1572 return 0;
1573
1574 return a->mode;
1575}
1576
1577static struct attribute_group sriov_dev_attr_group = {
1578 .attrs = sriov_dev_attrs,
1579 .is_visible = sriov_attrs_are_visible,
1580};
1581#endif /* CONFIG_PCI_IOV */
1582
Yinghai Lu4e15c462012-11-05 15:20:34 -05001583static struct attribute_group pci_dev_attr_group = {
1584 .attrs = pci_dev_dev_attrs,
1585 .is_visible = pci_dev_attrs_are_visible,
1586};
1587
1588static const struct attribute_group *pci_dev_attr_groups[] = {
1589 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001590 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001591#ifdef CONFIG_PCI_IOV
1592 &sriov_dev_attr_group,
1593#endif
Yinghai Lu4e15c462012-11-05 15:20:34 -05001594 NULL,
1595};
1596
1597struct device_type pci_dev_type = {
1598 .groups = pci_dev_attr_groups,
1599};