blob: 3db1c7ff5dd38e9c52b4da69fe82d85b949f3c76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-sysfs.c
3 *
4 * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2002-2004 IBM Corp.
6 * (C) Copyright 2003 Matthew Wilcox
7 * (C) Copyright 2003 Hewlett-Packard
8 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
9 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
10 *
11 * File attributes for PCI devices
12 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -070013 * Modeled after usb's driverfs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
16
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -070019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/pci.h>
21#include <linux/stat.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/topology.h>
24#include <linux/mm.h>
Chris Wrightde139a32010-05-13 10:43:07 -070025#include <linux/fs.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070026#include <linux/capability.h>
Chris Wrighta628e7b2011-02-14 17:21:49 -080027#include <linux/security.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080028#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Matthew Garrett1a39b312012-04-16 16:26:02 -040030#include <linux/vgaarb.h>
Huang Ying448bd852012-06-23 10:23:51 +080031#include <linux/pm_runtime.h>
Sebastian Ottdfc73e72014-04-17 19:46:15 +020032#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "pci.h"
34
35static int sysfs_initialized; /* = 0 */
36
37/* show configuration fields */
38#define pci_config_attr(field, format_string) \
39static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -040040field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{ \
42 struct pci_dev *pdev; \
43 \
44 pdev = to_pci_dev (dev); \
45 return sprintf (buf, format_string, pdev->field); \
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070046} \
47static DEVICE_ATTR_RO(field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49pci_config_attr(vendor, "0x%04x\n");
50pci_config_attr(device, "0x%04x\n");
51pci_config_attr(subsystem_vendor, "0x%04x\n");
52pci_config_attr(subsystem_device, "0x%04x\n");
53pci_config_attr(class, "0x%06x\n");
54pci_config_attr(irq, "%u\n");
55
Doug Thompsonbdee9d92006-06-14 16:59:48 -070056static ssize_t broken_parity_status_show(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
59{
60 struct pci_dev *pdev = to_pci_dev(dev);
61 return sprintf (buf, "%u\n", pdev->broken_parity_status);
62}
63
64static ssize_t broken_parity_status_store(struct device *dev,
65 struct device_attribute *attr,
66 const char *buf, size_t count)
67{
68 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080069 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070070
Jingoo Han9a994e82013-06-01 16:25:25 +090071 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080072 return -EINVAL;
73
74 pdev->broken_parity_status = !!val;
75
76 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070077}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -070078static DEVICE_ATTR_RW(broken_parity_status);
Doug Thompsonbdee9d92006-06-14 16:59:48 -070079
Yijing Wangc489f5f2013-09-30 15:02:38 +080080static ssize_t pci_dev_show_local_cpu(struct device *dev,
81 int type,
82 struct device_attribute *attr,
83 char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070084{
Mike Travis3be83052009-01-04 05:18:01 -080085 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -070086 int len;
87
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020088#ifdef CONFIG_NUMA
David John6be954d2010-01-04 20:28:57 +053089 mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
90 cpumask_of_node(dev_to_node(dev));
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020091#else
Mike Travis3be83052009-01-04 05:18:01 -080092 mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
Andreas Herrmanne0cd5162009-04-17 12:01:55 +020093#endif
Yijing Wangc489f5f2013-09-30 15:02:38 +080094 len = type ?
95 cpumask_scnprintf(buf, PAGE_SIZE-2, mask) :
96 cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
97
Mike Travis39106dc2008-04-08 11:43:03 -070098 buf[len++] = '\n';
99 buf[len] = '\0';
100 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Yijing Wangc489f5f2013-09-30 15:02:38 +0800103static ssize_t local_cpus_show(struct device *dev,
104 struct device_attribute *attr, char *buf)
105{
106 return pci_dev_show_local_cpu(dev, 1, attr, buf);
107}
Bjorn Helgaas33de1b82013-10-31 14:12:40 -0600108static DEVICE_ATTR_RO(local_cpus);
Yijing Wangc489f5f2013-09-30 15:02:38 +0800109
110static ssize_t local_cpulist_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
112{
113 return pci_dev_show_local_cpu(dev, 0, attr, buf);
114}
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
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700274 /*
275 * Bad things may happen if the no_msi flag is changed
276 * while drivers are loaded.
277 */
Brice Goglinfe970642006-08-31 01:55:15 -0400278 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800279 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400280
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700281 /*
282 * Maybe devices without subordinate buses shouldn't have this
283 * attribute in the first place?
284 */
Brice Goglinfe970642006-08-31 01:55:15 -0400285 if (!pdev->subordinate)
286 return count;
287
Trent Piepho92425a42008-11-30 17:10:12 -0800288 /* Is the flag going to change, or keep the value it already had? */
289 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
290 !!val) {
291 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400292
Trent Piepho92425a42008-11-30 17:10:12 -0800293 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
294 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400295 }
296
297 return count;
298}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700299static DEVICE_ATTR_RW(msi_bus);
Greg KH98885492005-05-05 11:57:25 -0700300
Alex Chiang705b1aa2009-03-20 14:56:31 -0600301static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
302 size_t count)
303{
304 unsigned long val;
305 struct pci_bus *b = NULL;
306
Jingoo Han9a994e82013-06-01 16:25:25 +0900307 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600308 return -EINVAL;
309
310 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100311 pci_lock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600312 while ((b = pci_find_next_bus(b)) != NULL)
313 pci_rescan_bus(b);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100314 pci_unlock_rescan_remove();
Alex Chiang705b1aa2009-03-20 14:56:31 -0600315 }
316 return count;
317}
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600318static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
Alex Chiang705b1aa2009-03-20 14:56:31 -0600319
Sachin Kamatbf22c902013-09-28 15:42:00 +0530320static struct attribute *pci_bus_attrs[] = {
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -0600321 &bus_attr_rescan.attr,
322 NULL,
323};
324
325static const struct attribute_group pci_bus_group = {
326 .attrs = pci_bus_attrs,
327};
328
329const struct attribute_group *pci_bus_groups[] = {
330 &pci_bus_group,
331 NULL,
Alex Chiang705b1aa2009-03-20 14:56:31 -0600332};
Alex Chiang77c27c72009-03-20 14:56:36 -0600333
Alex Chiang738a6392009-03-20 14:56:41 -0600334static ssize_t
335dev_rescan_store(struct device *dev, struct device_attribute *attr,
336 const char *buf, size_t count)
337{
338 unsigned long val;
339 struct pci_dev *pdev = to_pci_dev(dev);
340
Jingoo Han9a994e82013-06-01 16:25:25 +0900341 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600342 return -EINVAL;
343
344 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100345 pci_lock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600346 pci_rescan_bus(pdev->bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100347 pci_unlock_rescan_remove();
Alex Chiang738a6392009-03-20 14:56:41 -0600348 }
349 return count;
350}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530351static struct device_attribute dev_rescan_attr = __ATTR(rescan,
352 (S_IWUSR|S_IWGRP),
353 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600354
Alex Chiang77c27c72009-03-20 14:56:36 -0600355static ssize_t
Tejun Heobc6caf02014-02-03 14:03:02 -0500356remove_store(struct device *dev, struct device_attribute *attr,
Alex Chiang77c27c72009-03-20 14:56:36 -0600357 const char *buf, size_t count)
358{
Alex Chiang77c27c72009-03-20 14:56:36 -0600359 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600360
Jingoo Han9a994e82013-06-01 16:25:25 +0900361 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600362 return -EINVAL;
363
Tejun Heobc6caf02014-02-03 14:03:02 -0500364 if (val && device_remove_file_self(dev, attr))
365 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
Alex Chiang77c27c72009-03-20 14:56:36 -0600366 return count;
367}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530368static struct device_attribute dev_remove_attr = __ATTR(remove,
369 (S_IWUSR|S_IWGRP),
370 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700371
372static ssize_t
373dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
374 const char *buf, size_t count)
375{
376 unsigned long val;
377 struct pci_bus *bus = to_pci_bus(dev);
378
Jingoo Han9a994e82013-06-01 16:25:25 +0900379 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700380 return -EINVAL;
381
382 if (val) {
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100383 pci_lock_rescan_remove();
Yinghai Lu2f320522012-01-21 02:08:22 -0800384 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
385 pci_rescan_bus_bridge_resize(bus->self);
386 else
387 pci_rescan_bus(bus);
Rafael J. Wysocki9d169472014-01-10 15:22:18 +0100388 pci_unlock_rescan_remove();
Yinghai Lub9d320f2011-05-12 17:11:39 -0700389 }
390 return count;
391}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700392static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700393
Huang Ying448bd852012-06-23 10:23:51 +0800394#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
395static ssize_t d3cold_allowed_store(struct device *dev,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
398{
399 struct pci_dev *pdev = to_pci_dev(dev);
400 unsigned long val;
401
Jingoo Han9a994e82013-06-01 16:25:25 +0900402 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800403 return -EINVAL;
404
405 pdev->d3cold_allowed = !!val;
406 pm_runtime_resume(dev);
407
408 return count;
409}
410
411static ssize_t d3cold_allowed_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
413{
414 struct pci_dev *pdev = to_pci_dev(dev);
415 return sprintf (buf, "%u\n", pdev->d3cold_allowed);
416}
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700417static DEVICE_ATTR_RW(d3cold_allowed);
Huang Ying448bd852012-06-23 10:23:51 +0800418#endif
419
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200420#ifdef CONFIG_OF
421static ssize_t devspec_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 struct pci_dev *pdev = to_pci_dev(dev);
425 struct device_node *np = pci_device_to_OF_node(pdev);
426
427 if (np == NULL || np->full_name == NULL)
428 return 0;
429 return sprintf(buf, "%s", np->full_name);
430}
431static DEVICE_ATTR_RO(devspec);
432#endif
433
Donald Dutile17893822012-11-05 15:20:36 -0500434#ifdef CONFIG_PCI_IOV
435static ssize_t sriov_totalvfs_show(struct device *dev,
436 struct device_attribute *attr,
437 char *buf)
438{
439 struct pci_dev *pdev = to_pci_dev(dev);
440
Donald Dutilebff73152012-11-05 15:20:37 -0500441 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500442}
443
444
445static ssize_t sriov_numvfs_show(struct device *dev,
446 struct device_attribute *attr,
447 char *buf)
448{
449 struct pci_dev *pdev = to_pci_dev(dev);
450
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700451 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500452}
453
454/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700455 * num_vfs > 0; number of VFs to enable
456 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500457 *
458 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700459 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500460 */
461static ssize_t sriov_numvfs_store(struct device *dev,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
464{
465 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700466 int ret;
467 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500468
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700469 ret = kstrtou16(buf, 0, &num_vfs);
470 if (ret < 0)
471 return ret;
472
473 if (num_vfs > pci_sriov_get_totalvfs(pdev))
474 return -ERANGE;
475
476 if (num_vfs == pdev->sriov->num_VFs)
477 return count; /* no change */
Donald Dutile17893822012-11-05 15:20:36 -0500478
479 /* is PF driver loaded w/callback */
480 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700481 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Donald Dutile17893822012-11-05 15:20:36 -0500482 return -ENOSYS;
483 }
484
Donald Dutile17893822012-11-05 15:20:36 -0500485 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700486 /* disable VFs */
487 ret = pdev->driver->sriov_configure(pdev, 0);
488 if (ret < 0)
489 return ret;
490 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500491 }
492
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700493 /* enable VFs */
494 if (pdev->sriov->num_VFs) {
495 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
496 pdev->sriov->num_VFs, num_vfs);
497 return -EBUSY;
498 }
Donald Dutile17893822012-11-05 15:20:36 -0500499
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700500 ret = pdev->driver->sriov_configure(pdev, num_vfs);
501 if (ret < 0)
502 return ret;
503
504 if (ret != num_vfs)
505 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
506 num_vfs, ret);
507
508 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500509}
510
511static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
512static struct device_attribute sriov_numvfs_attr =
513 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
514 sriov_numvfs_show, sriov_numvfs_store);
515#endif /* CONFIG_PCI_IOV */
516
Sachin Kamatbf22c902013-09-28 15:42:00 +0530517static struct attribute *pci_dev_attrs[] = {
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700518 &dev_attr_resource.attr,
519 &dev_attr_vendor.attr,
520 &dev_attr_device.attr,
521 &dev_attr_subsystem_vendor.attr,
522 &dev_attr_subsystem_device.attr,
523 &dev_attr_class.attr,
524 &dev_attr_irq.attr,
525 &dev_attr_local_cpus.attr,
526 &dev_attr_local_cpulist.attr,
527 &dev_attr_modalias.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100528#ifdef CONFIG_NUMA
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700529 &dev_attr_numa_node.attr,
Brice Goglin81bb0e12007-01-28 10:53:40 +0100530#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700531 &dev_attr_dma_mask_bits.attr,
532 &dev_attr_consistent_dma_mask_bits.attr,
533 &dev_attr_enabled.attr,
534 &dev_attr_broken_parity_status.attr,
535 &dev_attr_msi_bus.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800536#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700537 &dev_attr_d3cold_allowed.attr,
Huang Ying448bd852012-06-23 10:23:51 +0800538#endif
Sebastian Ottdfc73e72014-04-17 19:46:15 +0200539#ifdef CONFIG_OF
540 &dev_attr_devspec.attr,
541#endif
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -0700542 NULL,
543};
544
545static const struct attribute_group pci_dev_group = {
546 .attrs = pci_dev_attrs,
547};
548
549const struct attribute_group *pci_dev_groups[] = {
550 &pci_dev_group,
551 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552};
553
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700554static struct attribute *pcibus_attrs[] = {
555 &dev_attr_rescan.attr,
556 &dev_attr_cpuaffinity.attr,
557 &dev_attr_cpulistaffinity.attr,
558 NULL,
559};
560
561static const struct attribute_group pcibus_group = {
562 .attrs = pcibus_attrs,
563};
564
565const struct attribute_group *pcibus_groups[] = {
566 &pcibus_group,
567 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700568};
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000571boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
572{
573 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400574 struct pci_dev *vga_dev = vga_default_device();
575
576 if (vga_dev)
577 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000578
579 return sprintf(buf, "%u\n",
580 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
581 IORESOURCE_ROM_SHADOW));
582}
Sachin Kamatbf22c902013-09-28 15:42:00 +0530583static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
Dave Airlie217f45d2009-03-04 05:57:05 +0000584
585static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700586pci_read_config(struct file *filp, struct kobject *kobj,
587 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800588 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
591 unsigned int size = 64;
592 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900593 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* Several chips lock up trying to read undefined config space */
Eric Parisb7e724d2012-01-03 12:25:15 -0500596 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 size = dev->cfg_size;
598 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
599 size = 128;
600 }
601
602 if (off > size)
603 return 0;
604 if (off + count > size) {
605 size -= off;
606 count = size;
607 } else {
608 size = count;
609 }
610
Huang Ying3d8387e2012-08-15 09:43:03 +0800611 pci_config_pm_runtime_get(dev);
612
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900613 if ((off & 1) && size) {
614 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700615 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900616 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900618 size--;
619 }
620
621 if ((off & 3) && size > 2) {
622 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700623 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900624 data[off - init_off] = val & 0xff;
625 data[off - init_off + 1] = (val >> 8) & 0xff;
626 off += 2;
627 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900631 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700632 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900633 data[off - init_off] = val & 0xff;
634 data[off - init_off + 1] = (val >> 8) & 0xff;
635 data[off - init_off + 2] = (val >> 16) & 0xff;
636 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 off += 4;
638 size -= 4;
639 }
640
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900641 if (size >= 2) {
642 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700643 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900644 data[off - init_off] = val & 0xff;
645 data[off - init_off + 1] = (val >> 8) & 0xff;
646 off += 2;
647 size -= 2;
648 }
649
650 if (size > 0) {
651 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700652 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900653 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 off++;
655 --size;
656 }
657
Huang Ying3d8387e2012-08-15 09:43:03 +0800658 pci_config_pm_runtime_put(dev);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return count;
661}
662
663static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700664pci_write_config(struct file* filp, struct kobject *kobj,
665 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800666 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
669 unsigned int size = count;
670 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900671 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (off > dev->cfg_size)
674 return 0;
675 if (off + count > dev->cfg_size) {
676 size = dev->cfg_size - off;
677 count = size;
678 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700679
Huang Ying3d8387e2012-08-15 09:43:03 +0800680 pci_config_pm_runtime_get(dev);
681
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900682 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700683 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900685 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700687
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900688 if ((off & 3) && size > 2) {
689 u16 val = data[off - init_off];
690 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700691 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900692 off += 2;
693 size -= 2;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900697 u32 val = data[off - init_off];
698 val |= (u32) data[off - init_off + 1] << 8;
699 val |= (u32) data[off - init_off + 2] << 16;
700 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700701 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 off += 4;
703 size -= 4;
704 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700705
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900706 if (size >= 2) {
707 u16 val = data[off - init_off];
708 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700709 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900710 off += 2;
711 size -= 2;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900714 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700715 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 off++;
717 --size;
718 }
719
Huang Ying3d8387e2012-08-15 09:43:03 +0800720 pci_config_pm_runtime_put(dev);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return count;
723}
724
Ben Hutchings94e61082008-03-05 16:52:39 +0000725static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700726read_vpd_attr(struct file *filp, struct kobject *kobj,
727 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000728 char *buf, loff_t off, size_t count)
729{
730 struct pci_dev *dev =
731 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000732
733 if (off > bin_attr->size)
734 count = 0;
735 else if (count > bin_attr->size - off)
736 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000737
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800738 return pci_read_vpd(dev, off, count, buf);
739}
Ben Hutchings94e61082008-03-05 16:52:39 +0000740
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800741static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700742write_vpd_attr(struct file *filp, struct kobject *kobj,
743 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800744 char *buf, loff_t off, size_t count)
745{
746 struct pci_dev *dev =
747 to_pci_dev(container_of(kobj, struct device, kobj));
748
749 if (off > bin_attr->size)
750 count = 0;
751 else if (count > bin_attr->size - off)
752 count = bin_attr->size - off;
753
754 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#ifdef HAVE_PCI_LEGACY
758/**
759 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700760 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700762 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * @buf: buffer to store results
764 * @off: offset into legacy I/O port space
765 * @count: number of bytes to read
766 *
767 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
768 * callback routine (pci_legacy_read).
769 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000770static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700771pci_read_legacy_io(struct file *filp, struct kobject *kobj,
772 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800773 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400776 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 kobj));
778
779 /* Only support 1, 2 or 4 byte accesses */
780 if (count != 1 && count != 2 && count != 4)
781 return -EINVAL;
782
783 return pci_legacy_read(bus, off, (u32 *)buf, count);
784}
785
786/**
787 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700788 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700790 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * @buf: buffer containing value to be written
792 * @off: offset into legacy I/O port space
793 * @count: number of bytes to write
794 *
795 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
796 * callback routine (pci_legacy_write).
797 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000798static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700799pci_write_legacy_io(struct file *filp, struct kobject *kobj,
800 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800801 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400804 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 kobj));
806 /* Only support 1, 2 or 4 byte accesses */
807 if (count != 1 && count != 2 && count != 4)
808 return -EINVAL;
809
810 return pci_legacy_write(bus, off, *(u32 *)buf, count);
811}
812
813/**
814 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700815 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * @kobj: kobject corresponding to device to be mapped
817 * @attr: struct bin_attribute for this file
818 * @vma: struct vm_area_struct passed to mmap
819 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000820 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * legacy memory space (first meg of bus space) into application virtual
822 * memory space.
823 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000824static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700825pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
826 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct vm_area_struct *vma)
828{
829 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400830 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 kobj));
832
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000833 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
834}
835
836/**
837 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700838 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000839 * @kobj: kobject corresponding to device to be mapped
840 * @attr: struct bin_attribute for this file
841 * @vma: struct vm_area_struct passed to mmap
842 *
843 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
844 * legacy IO space (first meg of bus space) into application virtual
845 * memory space. Returns -ENOSYS if the operation isn't supported
846 */
847static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700848pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
849 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000850 struct vm_area_struct *vma)
851{
852 struct pci_bus *bus = to_pci_bus(container_of(kobj,
853 struct device,
854 kobj));
855
856 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
857}
858
859/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300860 * pci_adjust_legacy_attr - adjustment of legacy file attributes
861 * @b: bus to create files under
862 * @mmap_type: I/O port or memory
863 *
864 * Stub implementation. Can be overridden by arch if necessary.
865 */
866void __weak
867pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
868{
869 return;
870}
871
872/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000873 * pci_create_legacy_files - create legacy I/O port and memory files
874 * @b: bus to create files under
875 *
876 * Some platforms allow access to legacy I/O port and ISA memory space on
877 * a per-bus basis. This routine creates the files and ties them into
878 * their associated read, write and mmap files from pci-sysfs.c
879 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300880 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000881 * as it is ok to set up the PCI bus without these files.
882 */
883void pci_create_legacy_files(struct pci_bus *b)
884{
885 int error;
886
887 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
888 GFP_ATOMIC);
889 if (!b->legacy_io)
890 goto kzalloc_err;
891
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100892 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000893 b->legacy_io->attr.name = "legacy_io";
894 b->legacy_io->size = 0xffff;
895 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
896 b->legacy_io->read = pci_read_legacy_io;
897 b->legacy_io->write = pci_write_legacy_io;
898 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300899 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000900 error = device_create_bin_file(&b->dev, b->legacy_io);
901 if (error)
902 goto legacy_io_err;
903
904 /* Allocated above after the legacy_io struct */
905 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000906 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000907 b->legacy_mem->attr.name = "legacy_mem";
908 b->legacy_mem->size = 1024*1024;
909 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
910 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300911 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000912 error = device_create_bin_file(&b->dev, b->legacy_mem);
913 if (error)
914 goto legacy_mem_err;
915
916 return;
917
918legacy_mem_err:
919 device_remove_bin_file(&b->dev, b->legacy_io);
920legacy_io_err:
921 kfree(b->legacy_io);
922 b->legacy_io = NULL;
923kzalloc_err:
924 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
925 "and ISA memory resources to sysfs\n");
926 return;
927}
928
929void pci_remove_legacy_files(struct pci_bus *b)
930{
931 if (b->legacy_io) {
932 device_remove_bin_file(&b->dev, b->legacy_io);
933 device_remove_bin_file(&b->dev, b->legacy_mem);
934 kfree(b->legacy_io); /* both are allocated here */
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937#endif /* HAVE_PCI_LEGACY */
938
939#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700940
Martin Wilck3b519e42010-11-10 11:03:21 +0100941int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
942 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700943{
Martin Wilck3b519e42010-11-10 11:03:21 +0100944 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700945
Martin Wilck3b519e42010-11-10 11:03:21 +0100946 if (pci_resource_len(pdev, resno) == 0)
947 return 0;
Libin64b00172013-04-15 02:48:54 +0000948 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700949 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800950 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800951 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100952 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
953 if (start >= pci_start && start < pci_start + size &&
954 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700955 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700956 return 0;
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/**
960 * pci_mmap_resource - map a PCI resource into user memory space
961 * @kobj: kobject for mapping
962 * @attr: struct bin_attribute for the file being mapped
963 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700964 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 *
966 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968static int
969pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700970 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
973 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400974 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700976 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000977 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000979 for (i = 0; i < PCI_ROM_RESOURCE; i++)
980 if (res == &pdev->resource[i])
981 break;
982 if (i >= PCI_ROM_RESOURCE)
983 return -ENODEV;
984
Martin Wilck3b519e42010-11-10 11:03:21 +0100985 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
986 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
987 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
988 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
989 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800990 (u64)pci_resource_start(pdev, i),
991 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700992 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100993 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700994
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000995 /* pci_mmap_page_range() expects the same kind of entry as coming
996 * from /proc/bus/pci/ which is a "user visible" value. If this is
997 * different from the resource itself, arch will do necessary fixup.
998 */
999 pci_resource_to_user(pdev, i, res, &start, &end);
1000 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
1002
Arjan van de Vene8de1482008-10-22 19:55:31 -07001003 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
1004 return -EINVAL;
1005
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001006 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
1007}
1008
1009static int
Chris Wright2c3c8be2010-05-12 18:28:57 -07001010pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1011 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001012 struct vm_area_struct *vma)
1013{
1014 return pci_mmap_resource(kobj, attr, vma, 0);
1015}
1016
1017static int
Chris Wright2c3c8be2010-05-12 18:28:57 -07001018pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1019 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001020 struct vm_area_struct *vma)
1021{
1022 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Alex Williamson86333282010-07-19 09:45:34 -06001025static ssize_t
1026pci_resource_io(struct file *filp, struct kobject *kobj,
1027 struct bin_attribute *attr, char *buf,
1028 loff_t off, size_t count, bool write)
1029{
1030 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
1031 struct device, kobj));
1032 struct resource *res = attr->private;
1033 unsigned long port = off;
1034 int i;
1035
1036 for (i = 0; i < PCI_ROM_RESOURCE; i++)
1037 if (res == &pdev->resource[i])
1038 break;
1039 if (i >= PCI_ROM_RESOURCE)
1040 return -ENODEV;
1041
1042 port += pci_resource_start(pdev, i);
1043
1044 if (port > pci_resource_end(pdev, i))
1045 return 0;
1046
1047 if (port + count - 1 > pci_resource_end(pdev, i))
1048 return -EINVAL;
1049
1050 switch (count) {
1051 case 1:
1052 if (write)
1053 outb(*(u8 *)buf, port);
1054 else
1055 *(u8 *)buf = inb(port);
1056 return 1;
1057 case 2:
1058 if (write)
1059 outw(*(u16 *)buf, port);
1060 else
1061 *(u16 *)buf = inw(port);
1062 return 2;
1063 case 4:
1064 if (write)
1065 outl(*(u32 *)buf, port);
1066 else
1067 *(u32 *)buf = inl(port);
1068 return 4;
1069 }
1070 return -EINVAL;
1071}
1072
1073static ssize_t
1074pci_read_resource_io(struct file *filp, struct kobject *kobj,
1075 struct bin_attribute *attr, char *buf,
1076 loff_t off, size_t count)
1077{
1078 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1079}
1080
1081static ssize_t
1082pci_write_resource_io(struct file *filp, struct kobject *kobj,
1083 struct bin_attribute *attr, char *buf,
1084 loff_t off, size_t count)
1085{
1086 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001090 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001091 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001092 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001093 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001094 * free their resources.
1095 */
1096static void
1097pci_remove_resource_files(struct pci_dev *pdev)
1098{
1099 int i;
1100
1101 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1102 struct bin_attribute *res_attr;
1103
1104 res_attr = pdev->res_attr[i];
1105 if (res_attr) {
1106 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1107 kfree(res_attr);
1108 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001109
1110 res_attr = pdev->res_attr_wc[i];
1111 if (res_attr) {
1112 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1113 kfree(res_attr);
1114 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001115 }
1116}
1117
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001118static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1119{
1120 /* allocate attribute structure, piggyback attribute name */
1121 int name_len = write_combine ? 13 : 10;
1122 struct bin_attribute *res_attr;
1123 int retval;
1124
1125 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1126 if (res_attr) {
1127 char *res_attr_name = (char *)(res_attr + 1);
1128
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001129 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001130 if (write_combine) {
1131 pdev->res_attr_wc[num] = res_attr;
1132 sprintf(res_attr_name, "resource%d_wc", num);
1133 res_attr->mmap = pci_mmap_resource_wc;
1134 } else {
1135 pdev->res_attr[num] = res_attr;
1136 sprintf(res_attr_name, "resource%d", num);
1137 res_attr->mmap = pci_mmap_resource_uc;
1138 }
Alex Williamson86333282010-07-19 09:45:34 -06001139 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1140 res_attr->read = pci_read_resource_io;
1141 res_attr->write = pci_write_resource_io;
1142 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001143 res_attr->attr.name = res_attr_name;
1144 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1145 res_attr->size = pci_resource_len(pdev, num);
1146 res_attr->private = &pdev->resource[num];
1147 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1148 } else
1149 retval = -ENOMEM;
1150
1151 return retval;
1152}
1153
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001156 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001158 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001160static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001163 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* Expose the PCI resources from this device as files */
1166 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* skip empty resources */
1169 if (!pci_resource_len(pdev, i))
1170 continue;
1171
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001172 retval = pci_create_attr(pdev, i, 0);
1173 /* for prefetchable resources, create a WC mappable file */
1174 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1175 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001176
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001177 if (retval) {
1178 pci_remove_resource_files(pdev);
1179 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001185int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1186void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187#endif /* HAVE_PCI_MMAP */
1188
1189/**
1190 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001191 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001193 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 * @buf: user input
1195 * @off: file offset
1196 * @count: number of byte in input
1197 *
1198 * writing anything except 0 enables it
1199 */
1200static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001201pci_write_rom(struct file *filp, struct kobject *kobj,
1202 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001203 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1206
1207 if ((off == 0) && (*buf == '0') && (count == 2))
1208 pdev->rom_attr_enabled = 0;
1209 else
1210 pdev->rom_attr_enabled = 1;
1211
1212 return count;
1213}
1214
1215/**
1216 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001217 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001219 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * @buf: where to put the data we read from the ROM
1221 * @off: file offset
1222 * @count: number of bytes to read
1223 *
1224 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1225 * device corresponding to @kobj.
1226 */
1227static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001228pci_read_rom(struct file *filp, struct kobject *kobj,
1229 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001230 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1233 void __iomem *rom;
1234 size_t size;
1235
1236 if (!pdev->rom_attr_enabled)
1237 return -EINVAL;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001240 if (!rom || !size)
1241 return -EIO;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (off >= size)
1244 count = 0;
1245 else {
1246 if (off + count > size)
1247 count = size - off;
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 memcpy_fromio(buf, rom + off, count);
1250 }
1251 pci_unmap_rom(pdev, rom);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return count;
1254}
1255
1256static struct bin_attribute pci_config_attr = {
1257 .attr = {
1258 .name = "config",
1259 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001261 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 .read = pci_read_config,
1263 .write = pci_write_config,
1264};
1265
1266static struct bin_attribute pcie_config_attr = {
1267 .attr = {
1268 .name = "config",
1269 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001271 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 .read = pci_read_config,
1273 .write = pci_write_config,
1274};
1275
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001276int __weak pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001277{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001278 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001279}
1280
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001281static ssize_t reset_store(struct device *dev,
1282 struct device_attribute *attr, const char *buf,
1283 size_t count)
1284{
1285 struct pci_dev *pdev = to_pci_dev(dev);
1286 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001287 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001288
1289 if (result < 0)
1290 return result;
1291
1292 if (val != 1)
1293 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001294
1295 result = pci_reset_function(pdev);
1296 if (result < 0)
1297 return result;
1298
1299 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001300}
1301
1302static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1303
Zhao, Yu280c73d2008-10-13 20:01:00 +08001304static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1305{
1306 int retval;
1307 struct bin_attribute *attr;
1308
1309 /* If the device has VPD, try to expose it in sysfs. */
1310 if (dev->vpd) {
1311 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1312 if (!attr)
1313 return -ENOMEM;
1314
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001315 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001316 attr->size = dev->vpd->len;
1317 attr->attr.name = "vpd";
1318 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001319 attr->read = read_vpd_attr;
1320 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001321 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1322 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001323 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001324 return retval;
1325 }
1326 dev->vpd->attr = attr;
1327 }
1328
1329 /* Active State Power Management */
1330 pcie_aspm_create_sysfs_dev_files(dev);
1331
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001332 if (!pci_probe_reset_function(dev)) {
1333 retval = device_create_file(&dev->dev, &reset_attr);
1334 if (retval)
1335 goto error;
1336 dev->reset_fn = 1;
1337 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001338 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001339
1340error:
1341 pcie_aspm_remove_sysfs_dev_files(dev);
1342 if (dev->vpd && dev->vpd->attr) {
1343 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1344 kfree(dev->vpd->attr);
1345 }
1346
1347 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001348}
1349
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001350int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001352 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001353 int rom_size = 0;
1354 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (!sysfs_initialized)
1357 return -EACCES;
1358
Zhao, Yu557848c2008-10-13 19:18:07 +08001359 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001360 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001362 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1363 if (retval)
1364 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001366 retval = pci_create_resource_files(pdev);
1367 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001368 goto err_config_file;
1369
1370 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1371 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1372 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1373 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001376 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001377 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001378 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001379 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001380 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001382 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001383 attr->size = rom_size;
1384 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001385 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001386 attr->read = pci_read_rom;
1387 attr->write = pci_write_rom;
1388 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1389 if (retval) {
1390 kfree(attr);
1391 goto err_resource_files;
1392 }
1393 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001397 retval = pcibios_add_platform_entries(pdev);
1398 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001399 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001400
Zhao, Yu280c73d2008-10-13 20:01:00 +08001401 /* add sysfs entries for various capabilities */
1402 retval = pci_create_capabilities_sysfs(pdev);
1403 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001404 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001405
Narendra K911e1c92010-07-26 05:56:50 -05001406 pci_create_firmware_label_files(pdev);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001409
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001410err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001411 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001412 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001413 kfree(pdev->rom_attr);
1414 pdev->rom_attr = NULL;
1415 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001416err_resource_files:
1417 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001418err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001419 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001420 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1421 else
1422 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1423err:
1424 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Zhao, Yu280c73d2008-10-13 20:01:00 +08001427static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1428{
1429 if (dev->vpd && dev->vpd->attr) {
1430 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1431 kfree(dev->vpd->attr);
1432 }
1433
1434 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001435 if (dev->reset_fn) {
1436 device_remove_file(&dev->dev, &reset_attr);
1437 dev->reset_fn = 0;
1438 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/**
1442 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1443 * @pdev: device whose entries we should free
1444 *
1445 * Cleanup when @pdev is removed from sysfs.
1446 */
1447void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1448{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001449 int rom_size = 0;
1450
David Millerd67afe52006-11-10 12:27:48 -08001451 if (!sysfs_initialized)
1452 return;
1453
Zhao, Yu280c73d2008-10-13 20:01:00 +08001454 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001455
Zhao, Yu557848c2008-10-13 19:18:07 +08001456 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1458 else
1459 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1460
1461 pci_remove_resource_files(pdev);
1462
Zhao, Yu280c73d2008-10-13 20:01:00 +08001463 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1464 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1465 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1466 rom_size = 0x20000;
1467
1468 if (rom_size && pdev->rom_attr) {
1469 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1470 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Narendra K911e1c92010-07-26 05:56:50 -05001472
1473 pci_remove_firmware_label_files(pdev);
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477static int __init pci_sysfs_init(void)
1478{
1479 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001480 int retval;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001483 for_each_pci_dev(pdev) {
1484 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001485 if (retval) {
1486 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001487 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001488 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 return 0;
1492}
1493
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001494late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001495
1496static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001497 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001498 NULL,
1499};
1500
1501static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1502 struct attribute *a, int n)
1503{
Yinghai Lu625e1d52012-11-05 15:20:35 -05001504 struct device *dev = container_of(kobj, struct device, kobj);
1505 struct pci_dev *pdev = to_pci_dev(dev);
1506
1507 if (a == &vga_attr.attr)
1508 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1509 return 0;
1510
Yinghai Lu4e15c462012-11-05 15:20:34 -05001511 return a->mode;
1512}
1513
Jiang Liudfab88b2013-05-31 12:21:31 +08001514static struct attribute *pci_dev_hp_attrs[] = {
1515 &dev_remove_attr.attr,
1516 &dev_rescan_attr.attr,
1517 NULL,
1518};
1519
1520static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1521 struct attribute *a, int n)
1522{
1523 struct device *dev = container_of(kobj, struct device, kobj);
1524 struct pci_dev *pdev = to_pci_dev(dev);
1525
1526 if (pdev->is_virtfn)
1527 return 0;
1528
1529 return a->mode;
1530}
1531
1532static struct attribute_group pci_dev_hp_attr_group = {
1533 .attrs = pci_dev_hp_attrs,
1534 .is_visible = pci_dev_hp_attrs_are_visible,
1535};
1536
Donald Dutile17893822012-11-05 15:20:36 -05001537#ifdef CONFIG_PCI_IOV
1538static struct attribute *sriov_dev_attrs[] = {
1539 &sriov_totalvfs_attr.attr,
1540 &sriov_numvfs_attr.attr,
1541 NULL,
1542};
1543
1544static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1545 struct attribute *a, int n)
1546{
1547 struct device *dev = container_of(kobj, struct device, kobj);
1548
1549 if (!dev_is_pf(dev))
1550 return 0;
1551
1552 return a->mode;
1553}
1554
1555static struct attribute_group sriov_dev_attr_group = {
1556 .attrs = sriov_dev_attrs,
1557 .is_visible = sriov_attrs_are_visible,
1558};
1559#endif /* CONFIG_PCI_IOV */
1560
Yinghai Lu4e15c462012-11-05 15:20:34 -05001561static struct attribute_group pci_dev_attr_group = {
1562 .attrs = pci_dev_dev_attrs,
1563 .is_visible = pci_dev_attrs_are_visible,
1564};
1565
1566static const struct attribute_group *pci_dev_attr_groups[] = {
1567 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001568 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001569#ifdef CONFIG_PCI_IOV
1570 &sriov_dev_attr_group,
1571#endif
Yinghai Lu4e15c462012-11-05 15:20:34 -05001572 NULL,
1573};
1574
1575struct device_type pci_dev_type = {
1576 .groups = pci_dev_attr_groups,
1577};