blob: 82cc45867eaad40d03fe8982e0cd7f4d736bd939 [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 *
13 * Modeled after usb's driverfs.c
14 *
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
Alan Cox4327edf2005-09-10 00:25:49 -070079static ssize_t local_cpus_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Mike Travis3be83052009-01-04 05:18:01 -080082 const struct cpumask *mask;
Alan Cox4327edf2005-09-10 00:25:49 -070083 int len;
84
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020085#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053086 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
87 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020088#else
Mike Travis3be83052009-01-04 05:18:01 -080089 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020090#endif
Mike Travis3be83052009-01-04 05:18:01 -080091 len = cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070092 buf[len++] = '\n';
93 buf[len] = '\0';
94 return len;
95}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070096static DEVICE_ATTR_RO(local_cpus);
Mike Travis39106dc2008-04-08 11:43:03 -070097
98static ssize_t local_cpulist_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
100{
Mike Travis3be83052009-01-04 05:18:01 -0800101 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -0700102 int len;
103
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200104#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +0530105 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
106 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200107#else
Mike Travis3be83052009-01-04 05:18:01 -0800108 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +0200109#endif
Mike Travis3be83052009-01-04 05:18:01 -0800110 len = cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -0700111 buf[len++] = '\n';
112 buf[len] = '\0';
113 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700115static DEVICE_ATTR_RO(local_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700117/*
118 * PCI Bus Class Devices
119 */
120static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
121 int type,
122 struct device_attribute *attr,
123 char *buf)
124{
125 int ret;
126 const struct cpumask *cpumask;
127
128 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
129 ret = type ?
130 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
131 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
132 buf[ret++] = '\n';
133 buf[ret] = '\0';
134 return ret;
135}
136
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700137static ssize_t cpuaffinity_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700139{
140 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
141}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700142static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700143
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700144static ssize_t cpulistaffinity_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700146{
147 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
148}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700149static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* show resources */
152static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400153resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct pci_dev * pci_dev = to_pci_dev(dev);
156 char * str = buf;
157 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800158 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700159 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (pci_dev->subordinate)
162 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800163 else
164 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000167 struct resource *res = &pci_dev->resource[i];
168 pci_resource_to_user(pci_dev, i, res, &start, &end);
169 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
170 (unsigned long long)start,
171 (unsigned long long)end,
172 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 return (str - buf);
175}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700176static DEVICE_ATTR_RO(resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200178static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700179{
180 struct pci_dev *pci_dev = to_pci_dev(dev);
181
182 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
183 pci_dev->vendor, pci_dev->device,
184 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
185 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
186 (u8)(pci_dev->class));
187}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700188static DEVICE_ATTR_RO(modalias);
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800189
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700190static ssize_t enabled_store(struct device *dev,
191 struct device_attribute *attr, const char *buf,
192 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200193{
194 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800195 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900196 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800197
198 if (result < 0)
199 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200200
201 /* this can crash the machine when done on the "wrong" device */
202 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800203 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200204
Trent Piepho92425a42008-11-30 17:10:12 -0800205 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900206 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800207 pci_disable_device(pdev);
208 else
209 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800210 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800211 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200212
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800213 return result < 0 ? result : count;
214}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200215
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700216static ssize_t enabled_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800218{
219 struct pci_dev *pdev;
220
221 pdev = to_pci_dev (dev);
222 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200223}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700224static DEVICE_ATTR_RW(enabled);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200225
Brice Goglin81bb0e12007-01-28 10:53:40 +0100226#ifdef CONFIG_NUMA
227static ssize_t
228numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
229{
230 return sprintf (buf, "%d\n", dev->numa_node);
231}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700232static DEVICE_ATTR_RO(numa_node);
Brice Goglin81bb0e12007-01-28 10:53:40 +0100233#endif
234
Brice Goglinfe970642006-08-31 01:55:15 -0400235static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800236dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
237{
238 struct pci_dev *pdev = to_pci_dev(dev);
239
240 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
241}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700242static DEVICE_ATTR_RO(dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800243
244static ssize_t
245consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
246 char *buf)
247{
248 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
249}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700250static DEVICE_ATTR_RO(consistent_dma_mask_bits);
Yinghai Lubb965402009-11-24 18:21:21 -0800251
252static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400253msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
254{
255 struct pci_dev *pdev = to_pci_dev(dev);
256
257 if (!pdev->subordinate)
258 return 0;
259
260 return sprintf (buf, "%u\n",
261 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
262}
263
264static ssize_t
265msi_bus_store(struct device *dev, struct device_attribute *attr,
266 const char *buf, size_t count)
267{
268 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800269 unsigned long val;
270
Jingoo Han9a994e82013-06-01 16:25:25 +0900271 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800272 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400273
274 /* bad things may happen if the no_msi flag is changed
275 * while some drivers are loaded */
276 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800277 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400278
Trent Piepho92425a42008-11-30 17:10:12 -0800279 /* Maybe pci devices without subordinate busses shouldn't even have this
280 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400281 if (!pdev->subordinate)
282 return count;
283
Trent Piepho92425a42008-11-30 17:10:12 -0800284 /* Is the flag going to change, or keep the value it already had? */
285 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
286 !!val) {
287 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400288
Trent Piepho92425a42008-11-30 17:10:12 -0800289 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
290 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400291 }
292
293 return count;
294}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700295static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700296
Alex Chiang705b1aa2009-03-20 14:56:31 -0600297static DEFINE_MUTEX(pci_remove_rescan_mutex);
298static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
299 size_t count)
300{
301 unsigned long val;
302 struct pci_bus *b = NULL;
303
Jingoo Han9a994e82013-06-01 16:25:25 +0900304 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600305 return -EINVAL;
306
307 if (val) {
308 mutex_lock(&pci_remove_rescan_mutex);
309 while ((b = pci_find_next_bus(b)) != NULL)
310 pci_rescan_bus(b);
311 mutex_unlock(&pci_remove_rescan_mutex);
312 }
313 return count;
314}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600315static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600316
Sachin Kamatbf22c902013-09-28 15:42:00 +0530317static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600318 &bus_attr_rescan.attr,
319 NULL,
320};
321
322static const struct attribute_group pci_bus_group = {
323 .attrs = pci_bus_attrs,
324};
325
326const struct attribute_group *pci_bus_groups[] = {
327 &pci_bus_group,
328 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600329};
Alex Chiang77c27c72009-03-20 14:56:36 -0600330
Alex Chiang738a6392009-03-20 14:56:41 -0600331static ssize_t
332dev_rescan_store(struct device *dev, struct device_attribute *attr,
333 const char *buf, size_t count)
334{
335 unsigned long val;
336 struct pci_dev *pdev = to_pci_dev(dev);
337
Jingoo Han9a994e82013-06-01 16:25:25 +0900338 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600339 return -EINVAL;
340
341 if (val) {
342 mutex_lock(&pci_remove_rescan_mutex);
343 pci_rescan_bus(pdev->bus);
344 mutex_unlock(&pci_remove_rescan_mutex);
345 }
346 return count;
347}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530348static struct device_attribute dev_rescan_attr = __ATTR(rescan,
349 (S_IWUSR|S_IWGRP),
350 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600351
Alex Chiang77c27c72009-03-20 14:56:36 -0600352static void remove_callback(struct device *dev)
353{
354 struct pci_dev *pdev = to_pci_dev(dev);
355
356 mutex_lock(&pci_remove_rescan_mutex);
Yinghai Lu210647a2012-02-25 13:54:20 -0800357 pci_stop_and_remove_bus_device(pdev);
Alex Chiang77c27c72009-03-20 14:56:36 -0600358 mutex_unlock(&pci_remove_rescan_mutex);
359}
360
361static ssize_t
362remove_store(struct device *dev, struct device_attribute *dummy,
363 const char *buf, size_t count)
364{
365 int ret = 0;
366 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600367
Jingoo Han9a994e82013-06-01 16:25:25 +0900368 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600369 return -EINVAL;
370
Alex Chiang77c27c72009-03-20 14:56:36 -0600371 /* An attribute cannot be unregistered by one of its own methods,
372 * so we have to use this roundabout approach.
373 */
374 if (val)
375 ret = device_schedule_callback(dev, remove_callback);
376 if (ret)
377 count = ret;
378 return count;
379}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530380static struct device_attribute dev_remove_attr = __ATTR(remove,
381 (S_IWUSR|S_IWGRP),
382 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700383
384static ssize_t
385dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
387{
388 unsigned long val;
389 struct pci_bus *bus = to_pci_bus(dev);
390
Jingoo Han9a994e82013-06-01 16:25:25 +0900391 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700392 return -EINVAL;
393
394 if (val) {
395 mutex_lock(&pci_remove_rescan_mutex);
Yinghai Lu2f320522012-01-21 02:08:22 -0800396 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
397 pci_rescan_bus_bridge_resize(bus->self);
398 else
399 pci_rescan_bus(bus);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700400 mutex_unlock(&pci_remove_rescan_mutex);
401 }
402 return count;
403}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700404static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700405
Huang Ying448bd852012-06-23 10:23:51 +0800406#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
407static ssize_t d3cold_allowed_store(struct device *dev,
408 struct device_attribute *attr,
409 const char *buf, size_t count)
410{
411 struct pci_dev *pdev = to_pci_dev(dev);
412 unsigned long val;
413
Jingoo Han9a994e82013-06-01 16:25:25 +0900414 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800415 return -EINVAL;
416
417 pdev->d3cold_allowed = !!val;
418 pm_runtime_resume(dev);
419
420 return count;
421}
422
423static ssize_t d3cold_allowed_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425{
426 struct pci_dev *pdev = to_pci_dev(dev);
427 return sprintf (buf, "%u\n", pdev->d3cold_allowed);
428}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700429static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800430#endif
431
Donald Dutile17893822012-11-05 15:20:36 -0500432#ifdef CONFIG_PCI_IOV
433static ssize_t sriov_totalvfs_show(struct device *dev,
434 struct device_attribute *attr,
435 char *buf)
436{
437 struct pci_dev *pdev = to_pci_dev(dev);
438
Donald Dutilebff73152012-11-05 15:20:37 -0500439 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500440}
441
442
443static ssize_t sriov_numvfs_show(struct device *dev,
444 struct device_attribute *attr,
445 char *buf)
446{
447 struct pci_dev *pdev = to_pci_dev(dev);
448
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700449 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500450}
451
452/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700453 * num_vfs > 0; number of VFs to enable
454 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500455 *
456 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700457 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500458 */
459static ssize_t sriov_numvfs_store(struct device *dev,
460 struct device_attribute *attr,
461 const char *buf, size_t count)
462{
463 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700464 int ret;
465 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500466
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700467 ret = kstrtou16(buf, 0, &num_vfs);
468 if (ret < 0)
469 return ret;
470
471 if (num_vfs > pci_sriov_get_totalvfs(pdev))
472 return -ERANGE;
473
474 if (num_vfs == pdev->sriov->num_VFs)
475 return count; /* no change */
Donald Dutile17893822012-11-05 15:20:36 -0500476
477 /* is PF driver loaded w/callback */
478 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700479 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Donald Dutile17893822012-11-05 15:20:36 -0500480 return -ENOSYS;
481 }
482
Donald Dutile17893822012-11-05 15:20:36 -0500483 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700484 /* disable VFs */
485 ret = pdev->driver->sriov_configure(pdev, 0);
486 if (ret < 0)
487 return ret;
488 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500489 }
490
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700491 /* enable VFs */
492 if (pdev->sriov->num_VFs) {
493 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
494 pdev->sriov->num_VFs, num_vfs);
495 return -EBUSY;
496 }
Donald Dutile17893822012-11-05 15:20:36 -0500497
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700498 ret = pdev->driver->sriov_configure(pdev, num_vfs);
499 if (ret < 0)
500 return ret;
501
502 if (ret != num_vfs)
503 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
504 num_vfs, ret);
505
506 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500507}
508
509static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
510static struct device_attribute sriov_numvfs_attr =
511 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
512 sriov_numvfs_show, sriov_numvfs_store);
513#endif /* CONFIG_PCI_IOV */
514
Sachin Kamatbf22c902013-09-28 15:42:00 +0530515static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700516 &dev_attr_resource.attr,
517 &dev_attr_vendor.attr,
518 &dev_attr_device.attr,
519 &dev_attr_subsystem_vendor.attr,
520 &dev_attr_subsystem_device.attr,
521 &dev_attr_class.attr,
522 &dev_attr_irq.attr,
523 &dev_attr_local_cpus.attr,
524 &dev_attr_local_cpulist.attr,
525 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100526#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700527 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100528#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700529 &dev_attr_dma_mask_bits.attr,
530 &dev_attr_consistent_dma_mask_bits.attr,
531 &dev_attr_enabled.attr,
532 &dev_attr_broken_parity_status.attr,
533 &dev_attr_msi_bus.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800534#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700535 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800536#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700537 NULL,
538};
539
540static const struct attribute_group pci_dev_group = {
541 .attrs = pci_dev_attrs,
542};
543
544const struct attribute_group *pci_dev_groups[] = {
545 &pci_dev_group,
546 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547};
548
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700549static struct attribute *pcibus_attrs[] = {
550 &dev_attr_rescan.attr,
551 &dev_attr_cpuaffinity.attr,
552 &dev_attr_cpulistaffinity.attr,
553 NULL,
554};
555
556static const struct attribute_group pcibus_group = {
557 .attrs = pcibus_attrs,
558};
559
560const struct attribute_group *pcibus_groups[] = {
561 &pcibus_group,
562 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700563};
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000566boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
567{
568 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400569 struct pci_dev *vga_dev = vga_default_device();
570
571 if (vga_dev)
572 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000573
574 return sprintf(buf, "%u\n",
575 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
576 IORESOURCE_ROM_SHADOW));
577}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530578static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000579
580static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700581pci_read_config(struct file *filp, struct kobject *kobj,
582 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800583 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
586 unsigned int size = 64;
587 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900588 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Several chips lock up trying to read undefined config space */
Eric Parisb7e724d2012-01-03 12:25:15 -0500591 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 size = dev->cfg_size;
593 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
594 size = 128;
595 }
596
597 if (off > size)
598 return 0;
599 if (off + count > size) {
600 size -= off;
601 count = size;
602 } else {
603 size = count;
604 }
605
Huang Ying3d8387e2012-08-15 09:43:03 +0800606 pci_config_pm_runtime_get(dev);
607
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900608 if ((off & 1) && size) {
609 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700610 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900611 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900613 size--;
614 }
615
616 if ((off & 3) && size > 2) {
617 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700618 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900619 data[off - init_off] = val & 0xff;
620 data[off - init_off + 1] = (val >> 8) & 0xff;
621 off += 2;
622 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900626 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700627 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900628 data[off - init_off] = val & 0xff;
629 data[off - init_off + 1] = (val >> 8) & 0xff;
630 data[off - init_off + 2] = (val >> 16) & 0xff;
631 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 off += 4;
633 size -= 4;
634 }
635
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900636 if (size >= 2) {
637 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700638 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900639 data[off - init_off] = val & 0xff;
640 data[off - init_off + 1] = (val >> 8) & 0xff;
641 off += 2;
642 size -= 2;
643 }
644
645 if (size > 0) {
646 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700647 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900648 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 off++;
650 --size;
651 }
652
Huang Ying3d8387e2012-08-15 09:43:03 +0800653 pci_config_pm_runtime_put(dev);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return count;
656}
657
658static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700659pci_write_config(struct file* filp, struct kobject *kobj,
660 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800661 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
664 unsigned int size = count;
665 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900666 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if (off > dev->cfg_size)
669 return 0;
670 if (off + count > dev->cfg_size) {
671 size = dev->cfg_size - off;
672 count = size;
673 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900674
Huang Ying3d8387e2012-08-15 09:43:03 +0800675 pci_config_pm_runtime_get(dev);
676
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900677 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700678 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900680 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900682
683 if ((off & 3) && size > 2) {
684 u16 val = data[off - init_off];
685 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700686 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900687 off += 2;
688 size -= 2;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900692 u32 val = data[off - init_off];
693 val |= (u32) data[off - init_off + 1] << 8;
694 val |= (u32) data[off - init_off + 2] << 16;
695 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700696 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 off += 4;
698 size -= 4;
699 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900700
701 if (size >= 2) {
702 u16 val = data[off - init_off];
703 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700704 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900705 off += 2;
706 size -= 2;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900709 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700710 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 off++;
712 --size;
713 }
714
Huang Ying3d8387e2012-08-15 09:43:03 +0800715 pci_config_pm_runtime_put(dev);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return count;
718}
719
Ben Hutchings94e61082008-03-05 16:52:39 +0000720static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700721read_vpd_attr(struct file *filp, struct kobject *kobj,
722 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000723 char *buf, loff_t off, size_t count)
724{
725 struct pci_dev *dev =
726 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000727
728 if (off > bin_attr->size)
729 count = 0;
730 else if (count > bin_attr->size - off)
731 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000732
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800733 return pci_read_vpd(dev, off, count, buf);
734}
Ben Hutchings94e61082008-03-05 16:52:39 +0000735
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800736static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700737write_vpd_attr(struct file *filp, struct kobject *kobj,
738 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800739 char *buf, loff_t off, size_t count)
740{
741 struct pci_dev *dev =
742 to_pci_dev(container_of(kobj, struct device, kobj));
743
744 if (off > bin_attr->size)
745 count = 0;
746 else if (count > bin_attr->size - off)
747 count = bin_attr->size - off;
748
749 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#ifdef HAVE_PCI_LEGACY
753/**
754 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700755 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700757 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * @buf: buffer to store results
759 * @off: offset into legacy I/O port space
760 * @count: number of bytes to read
761 *
762 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
763 * callback routine (pci_legacy_read).
764 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000765static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700766pci_read_legacy_io(struct file *filp, struct kobject *kobj,
767 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800768 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400771 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 kobj));
773
774 /* Only support 1, 2 or 4 byte accesses */
775 if (count != 1 && count != 2 && count != 4)
776 return -EINVAL;
777
778 return pci_legacy_read(bus, off, (u32 *)buf, count);
779}
780
781/**
782 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700783 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700785 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * @buf: buffer containing value to be written
787 * @off: offset into legacy I/O port space
788 * @count: number of bytes to write
789 *
790 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
791 * callback routine (pci_legacy_write).
792 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000793static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700794pci_write_legacy_io(struct file *filp, struct kobject *kobj,
795 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800796 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400799 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 kobj));
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_write(bus, off, *(u32 *)buf, count);
806}
807
808/**
809 * pci_mmap_legacy_mem - map legacy PCI memory into user memory 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 device to be mapped
812 * @attr: struct bin_attribute for this file
813 * @vma: struct vm_area_struct passed to mmap
814 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000815 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * legacy memory space (first meg of bus space) into application virtual
817 * memory space.
818 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000819static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700820pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
821 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct vm_area_struct *vma)
823{
824 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400825 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 kobj));
827
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000828 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
829}
830
831/**
832 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700833 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000834 * @kobj: kobject corresponding to device to be mapped
835 * @attr: struct bin_attribute for this file
836 * @vma: struct vm_area_struct passed to mmap
837 *
838 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
839 * legacy IO space (first meg of bus space) into application virtual
840 * memory space. Returns -ENOSYS if the operation isn't supported
841 */
842static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700843pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
844 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000845 struct vm_area_struct *vma)
846{
847 struct pci_bus *bus = to_pci_bus(container_of(kobj,
848 struct device,
849 kobj));
850
851 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
852}
853
854/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300855 * pci_adjust_legacy_attr - adjustment of legacy file attributes
856 * @b: bus to create files under
857 * @mmap_type: I/O port or memory
858 *
859 * Stub implementation. Can be overridden by arch if necessary.
860 */
861void __weak
862pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
863{
864 return;
865}
866
867/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000868 * pci_create_legacy_files - create legacy I/O port and memory files
869 * @b: bus to create files under
870 *
871 * Some platforms allow access to legacy I/O port and ISA memory space on
872 * a per-bus basis. This routine creates the files and ties them into
873 * their associated read, write and mmap files from pci-sysfs.c
874 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300875 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000876 * as it is ok to set up the PCI bus without these files.
877 */
878void pci_create_legacy_files(struct pci_bus *b)
879{
880 int error;
881
882 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
883 GFP_ATOMIC);
884 if (!b->legacy_io)
885 goto kzalloc_err;
886
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100887 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000888 b->legacy_io->attr.name = "legacy_io";
889 b->legacy_io->size = 0xffff;
890 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
891 b->legacy_io->read = pci_read_legacy_io;
892 b->legacy_io->write = pci_write_legacy_io;
893 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300894 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000895 error = device_create_bin_file(&b->dev, b->legacy_io);
896 if (error)
897 goto legacy_io_err;
898
899 /* Allocated above after the legacy_io struct */
900 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000901 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000902 b->legacy_mem->attr.name = "legacy_mem";
903 b->legacy_mem->size = 1024*1024;
904 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
905 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300906 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000907 error = device_create_bin_file(&b->dev, b->legacy_mem);
908 if (error)
909 goto legacy_mem_err;
910
911 return;
912
913legacy_mem_err:
914 device_remove_bin_file(&b->dev, b->legacy_io);
915legacy_io_err:
916 kfree(b->legacy_io);
917 b->legacy_io = NULL;
918kzalloc_err:
919 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
920 "and ISA memory resources to sysfs\n");
921 return;
922}
923
924void pci_remove_legacy_files(struct pci_bus *b)
925{
926 if (b->legacy_io) {
927 device_remove_bin_file(&b->dev, b->legacy_io);
928 device_remove_bin_file(&b->dev, b->legacy_mem);
929 kfree(b->legacy_io); /* both are allocated here */
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932#endif /* HAVE_PCI_LEGACY */
933
934#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700935
Martin Wilck3b519e42010-11-10 11:03:21 +0100936int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
937 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700938{
Martin Wilck3b519e42010-11-10 11:03:21 +0100939 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700940
Martin Wilck3b519e42010-11-10 11:03:21 +0100941 if (pci_resource_len(pdev, resno) == 0)
942 return 0;
Libin64b00172013-04-15 02:48:54 +0000943 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700944 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800945 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800946 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100947 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
948 if (start >= pci_start && start < pci_start + size &&
949 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700950 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700951 return 0;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/**
955 * pci_mmap_resource - map a PCI resource into user memory space
956 * @kobj: kobject for mapping
957 * @attr: struct bin_attribute for the file being mapped
958 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700959 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *
961 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
963static int
964pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700965 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
968 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400969 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700971 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000972 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000974 for (i = 0; i < PCI_ROM_RESOURCE; i++)
975 if (res == &pdev->resource[i])
976 break;
977 if (i >= PCI_ROM_RESOURCE)
978 return -ENODEV;
979
Martin Wilck3b519e42010-11-10 11:03:21 +0100980 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
981 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
982 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
983 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
984 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800985 (u64)pci_resource_start(pdev, i),
986 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700987 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100988 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700989
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000990 /* pci_mmap_page_range() expects the same kind of entry as coming
991 * from /proc/bus/pci/ which is a "user visible" value. If this is
992 * different from the resource itself, arch will do necessary fixup.
993 */
994 pci_resource_to_user(pdev, i, res, &start, &end);
995 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
997
Arjan van de Vene8de1482008-10-22 19:55:31 -0700998 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
999 return -EINVAL;
1000
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001001 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
1002}
1003
1004static int
Chris Wright2c3c8be2010-05-12 18:28:57 -07001005pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1006 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001007 struct vm_area_struct *vma)
1008{
1009 return pci_mmap_resource(kobj, attr, vma, 0);
1010}
1011
1012static int
Chris Wright2c3c8be2010-05-12 18:28:57 -07001013pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1014 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001015 struct vm_area_struct *vma)
1016{
1017 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Alex Williamson86333282010-07-19 09:45:34 -06001020static ssize_t
1021pci_resource_io(struct file *filp, struct kobject *kobj,
1022 struct bin_attribute *attr, char *buf,
1023 loff_t off, size_t count, bool write)
1024{
1025 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
1026 struct device, kobj));
1027 struct resource *res = attr->private;
1028 unsigned long port = off;
1029 int i;
1030
1031 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1032 if (res == &pdev->resource[i])
1033 break;
1034 if (i >= PCI_ROM_RESOURCE)
1035 return -ENODEV;
1036
1037 port += pci_resource_start(pdev, i);
1038
1039 if (port > pci_resource_end(pdev, i))
1040 return 0;
1041
1042 if (port + count - 1 > pci_resource_end(pdev, i))
1043 return -EINVAL;
1044
1045 switch (count) {
1046 case 1:
1047 if (write)
1048 outb(*(u8 *)buf, port);
1049 else
1050 *(u8 *)buf = inb(port);
1051 return 1;
1052 case 2:
1053 if (write)
1054 outw(*(u16 *)buf, port);
1055 else
1056 *(u16 *)buf = inw(port);
1057 return 2;
1058 case 4:
1059 if (write)
1060 outl(*(u32 *)buf, port);
1061 else
1062 *(u32 *)buf = inl(port);
1063 return 4;
1064 }
1065 return -EINVAL;
1066}
1067
1068static ssize_t
1069pci_read_resource_io(struct file *filp, struct kobject *kobj,
1070 struct bin_attribute *attr, char *buf,
1071 loff_t off, size_t count)
1072{
1073 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1074}
1075
1076static ssize_t
1077pci_write_resource_io(struct file *filp, struct kobject *kobj,
1078 struct bin_attribute *attr, char *buf,
1079 loff_t off, size_t count)
1080{
1081 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001085 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001086 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001087 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001088 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001089 * free their resources.
1090 */
1091static void
1092pci_remove_resource_files(struct pci_dev *pdev)
1093{
1094 int i;
1095
1096 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1097 struct bin_attribute *res_attr;
1098
1099 res_attr = pdev->res_attr[i];
1100 if (res_attr) {
1101 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1102 kfree(res_attr);
1103 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001104
1105 res_attr = pdev->res_attr_wc[i];
1106 if (res_attr) {
1107 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1108 kfree(res_attr);
1109 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001110 }
1111}
1112
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001113static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1114{
1115 /* allocate attribute structure, piggyback attribute name */
1116 int name_len = write_combine ? 13 : 10;
1117 struct bin_attribute *res_attr;
1118 int retval;
1119
1120 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1121 if (res_attr) {
1122 char *res_attr_name = (char *)(res_attr + 1);
1123
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001124 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001125 if (write_combine) {
1126 pdev->res_attr_wc[num] = res_attr;
1127 sprintf(res_attr_name, "resource%d_wc", num);
1128 res_attr->mmap = pci_mmap_resource_wc;
1129 } else {
1130 pdev->res_attr[num] = res_attr;
1131 sprintf(res_attr_name, "resource%d", num);
1132 res_attr->mmap = pci_mmap_resource_uc;
1133 }
Alex Williamson86333282010-07-19 09:45:34 -06001134 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1135 res_attr->read = pci_read_resource_io;
1136 res_attr->write = pci_write_resource_io;
1137 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001138 res_attr->attr.name = res_attr_name;
1139 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1140 res_attr->size = pci_resource_len(pdev, num);
1141 res_attr->private = &pdev->resource[num];
1142 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1143 } else
1144 retval = -ENOMEM;
1145
1146 return retval;
1147}
1148
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001149/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001151 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001153 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001155static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001158 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /* Expose the PCI resources from this device as files */
1161 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /* skip empty resources */
1164 if (!pci_resource_len(pdev, i))
1165 continue;
1166
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001167 retval = pci_create_attr(pdev, i, 0);
1168 /* for prefetchable resources, create a WC mappable file */
1169 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1170 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001171
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001172 if (retval) {
1173 pci_remove_resource_files(pdev);
1174 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001180int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1181void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#endif /* HAVE_PCI_MMAP */
1183
1184/**
1185 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001186 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001188 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * @buf: user input
1190 * @off: file offset
1191 * @count: number of byte in input
1192 *
1193 * writing anything except 0 enables it
1194 */
1195static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001196pci_write_rom(struct file *filp, struct kobject *kobj,
1197 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001198 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1201
1202 if ((off == 0) && (*buf == '0') && (count == 2))
1203 pdev->rom_attr_enabled = 0;
1204 else
1205 pdev->rom_attr_enabled = 1;
1206
1207 return count;
1208}
1209
1210/**
1211 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001212 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001214 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * @buf: where to put the data we read from the ROM
1216 * @off: file offset
1217 * @count: number of bytes to read
1218 *
1219 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1220 * device corresponding to @kobj.
1221 */
1222static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001223pci_read_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 void __iomem *rom;
1229 size_t size;
1230
1231 if (!pdev->rom_attr_enabled)
1232 return -EINVAL;
1233
1234 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001235 if (!rom || !size)
1236 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (off >= size)
1239 count = 0;
1240 else {
1241 if (off + count > size)
1242 count = size - off;
1243
1244 memcpy_fromio(buf, rom + off, count);
1245 }
1246 pci_unmap_rom(pdev, rom);
1247
1248 return count;
1249}
1250
1251static struct bin_attribute pci_config_attr = {
1252 .attr = {
1253 .name = "config",
1254 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001256 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 .read = pci_read_config,
1258 .write = pci_write_config,
1259};
1260
1261static struct bin_attribute pcie_config_attr = {
1262 .attr = {
1263 .name = "config",
1264 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001266 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 .read = pci_read_config,
1268 .write = pci_write_config,
1269};
1270
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001271int __weak pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001272{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001273 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001274}
1275
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001276static ssize_t reset_store(struct device *dev,
1277 struct device_attribute *attr, const char *buf,
1278 size_t count)
1279{
1280 struct pci_dev *pdev = to_pci_dev(dev);
1281 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001282 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001283
1284 if (result < 0)
1285 return result;
1286
1287 if (val != 1)
1288 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001289
1290 result = pci_reset_function(pdev);
1291 if (result < 0)
1292 return result;
1293
1294 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001295}
1296
1297static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1298
Zhao, Yu280c73d2008-10-13 20:01:00 +08001299static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1300{
1301 int retval;
1302 struct bin_attribute *attr;
1303
1304 /* If the device has VPD, try to expose it in sysfs. */
1305 if (dev->vpd) {
1306 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1307 if (!attr)
1308 return -ENOMEM;
1309
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001310 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001311 attr->size = dev->vpd->len;
1312 attr->attr.name = "vpd";
1313 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001314 attr->read = read_vpd_attr;
1315 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001316 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1317 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001318 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001319 return retval;
1320 }
1321 dev->vpd->attr = attr;
1322 }
1323
1324 /* Active State Power Management */
1325 pcie_aspm_create_sysfs_dev_files(dev);
1326
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001327 if (!pci_probe_reset_function(dev)) {
1328 retval = device_create_file(&dev->dev, &reset_attr);
1329 if (retval)
1330 goto error;
1331 dev->reset_fn = 1;
1332 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001333 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001334
1335error:
1336 pcie_aspm_remove_sysfs_dev_files(dev);
1337 if (dev->vpd && dev->vpd->attr) {
1338 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1339 kfree(dev->vpd->attr);
1340 }
1341
1342 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001343}
1344
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001345int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001347 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001348 int rom_size = 0;
1349 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!sysfs_initialized)
1352 return -EACCES;
1353
Zhao, Yu557848c2008-10-13 19:18:07 +08001354 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001355 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001357 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1358 if (retval)
1359 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001361 retval = pci_create_resource_files(pdev);
1362 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001363 goto err_config_file;
1364
1365 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1366 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1367 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1368 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001371 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001372 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001373 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001374 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001375 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001377 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001378 attr->size = rom_size;
1379 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001380 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001381 attr->read = pci_read_rom;
1382 attr->write = pci_write_rom;
1383 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1384 if (retval) {
1385 kfree(attr);
1386 goto err_resource_files;
1387 }
1388 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001392 retval = pcibios_add_platform_entries(pdev);
1393 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001394 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001395
Zhao, Yu280c73d2008-10-13 20:01:00 +08001396 /* add sysfs entries for various capabilities */
1397 retval = pci_create_capabilities_sysfs(pdev);
1398 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001399 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001400
Narendra K911e1c92010-07-26 05:56:50 -05001401 pci_create_firmware_label_files(pdev);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001404
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001405err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001406 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001407 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001408 kfree(pdev->rom_attr);
1409 pdev->rom_attr = NULL;
1410 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001411err_resource_files:
1412 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001413err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001414 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001415 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1416 else
1417 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1418err:
1419 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Zhao, Yu280c73d2008-10-13 20:01:00 +08001422static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1423{
1424 if (dev->vpd && dev->vpd->attr) {
1425 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1426 kfree(dev->vpd->attr);
1427 }
1428
1429 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001430 if (dev->reset_fn) {
1431 device_remove_file(&dev->dev, &reset_attr);
1432 dev->reset_fn = 0;
1433 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/**
1437 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1438 * @pdev: device whose entries we should free
1439 *
1440 * Cleanup when @pdev is removed from sysfs.
1441 */
1442void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1443{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001444 int rom_size = 0;
1445
David Millerd67afe52006-11-10 12:27:48 -08001446 if (!sysfs_initialized)
1447 return;
1448
Zhao, Yu280c73d2008-10-13 20:01:00 +08001449 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001450
Zhao, Yu557848c2008-10-13 19:18:07 +08001451 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1453 else
1454 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1455
1456 pci_remove_resource_files(pdev);
1457
Zhao, Yu280c73d2008-10-13 20:01:00 +08001458 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1459 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1460 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1461 rom_size = 0x20000;
1462
1463 if (rom_size && pdev->rom_attr) {
1464 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1465 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Narendra K911e1c92010-07-26 05:56:50 -05001467
1468 pci_remove_firmware_label_files(pdev);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472static int __init pci_sysfs_init(void)
1473{
1474 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001475 int retval;
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001478 for_each_pci_dev(pdev) {
1479 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001480 if (retval) {
1481 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001482 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001483 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 return 0;
1487}
1488
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001489late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001490
1491static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001492 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001493 NULL,
1494};
1495
1496static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1497 struct attribute *a, int n)
1498{
Yinghai Lu625e1d52012-11-05 15:20:35 -05001499 struct device *dev = container_of(kobj, struct device, kobj);
1500 struct pci_dev *pdev = to_pci_dev(dev);
1501
1502 if (a == &vga_attr.attr)
1503 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1504 return 0;
1505
Yinghai Lu4e15c462012-11-05 15:20:34 -05001506 return a->mode;
1507}
1508
Jiang Liudfab88b2013-05-31 12:21:31 +08001509static struct attribute *pci_dev_hp_attrs[] = {
1510 &dev_remove_attr.attr,
1511 &dev_rescan_attr.attr,
1512 NULL,
1513};
1514
1515static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1516 struct attribute *a, int n)
1517{
1518 struct device *dev = container_of(kobj, struct device, kobj);
1519 struct pci_dev *pdev = to_pci_dev(dev);
1520
1521 if (pdev->is_virtfn)
1522 return 0;
1523
1524 return a->mode;
1525}
1526
1527static struct attribute_group pci_dev_hp_attr_group = {
1528 .attrs = pci_dev_hp_attrs,
1529 .is_visible = pci_dev_hp_attrs_are_visible,
1530};
1531
Donald Dutile17893822012-11-05 15:20:36 -05001532#ifdef CONFIG_PCI_IOV
1533static struct attribute *sriov_dev_attrs[] = {
1534 &sriov_totalvfs_attr.attr,
1535 &sriov_numvfs_attr.attr,
1536 NULL,
1537};
1538
1539static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1540 struct attribute *a, int n)
1541{
1542 struct device *dev = container_of(kobj, struct device, kobj);
1543
1544 if (!dev_is_pf(dev))
1545 return 0;
1546
1547 return a->mode;
1548}
1549
1550static struct attribute_group sriov_dev_attr_group = {
1551 .attrs = sriov_dev_attrs,
1552 .is_visible = sriov_attrs_are_visible,
1553};
1554#endif /* CONFIG_PCI_IOV */
1555
Yinghai Lu4e15c462012-11-05 15:20:34 -05001556static struct attribute_group pci_dev_attr_group = {
1557 .attrs = pci_dev_dev_attrs,
1558 .is_visible = pci_dev_attrs_are_visible,
1559};
1560
1561static const struct attribute_group *pci_dev_attr_groups[] = {
1562 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001563 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001564#ifdef CONFIG_PCI_IOV
1565 &sriov_dev_attr_group,
1566#endif
Yinghai Lu4e15c462012-11-05 15:20:34 -05001567 NULL,
1568};
1569
1570struct device_type pci_dev_type = {
1571 .groups = pci_dev_attr_groups,
1572};