blob: d9252dd89ea9d61a43d399bd912c1cf5f155989d [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); \
45}
46
47pci_config_attr(vendor, "0x%04x\n");
48pci_config_attr(device, "0x%04x\n");
49pci_config_attr(subsystem_vendor, "0x%04x\n");
50pci_config_attr(subsystem_device, "0x%04x\n");
51pci_config_attr(class, "0x%06x\n");
52pci_config_attr(irq, "%u\n");
53
Doug Thompsonbdee9d92006-06-14 16:59:48 -070054static ssize_t broken_parity_status_show(struct device *dev,
55 struct device_attribute *attr,
56 char *buf)
57{
58 struct pci_dev *pdev = to_pci_dev(dev);
59 return sprintf (buf, "%u\n", pdev->broken_parity_status);
60}
61
62static ssize_t broken_parity_status_store(struct device *dev,
63 struct device_attribute *attr,
64 const char *buf, size_t count)
65{
66 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -080067 unsigned long val;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070068
Jingoo Han9a994e82013-06-01 16:25:25 +090069 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -080070 return -EINVAL;
71
72 pdev->broken_parity_status = !!val;
73
74 return count;
Doug Thompsonbdee9d92006-06-14 16:59:48 -070075}
76
Yijing Wangc489f5f2013-09-30 15:02:38 +080077static ssize_t pci_dev_show_local_cpu(struct device *dev,
78 int type,
79 struct device_attribute *attr,
80 char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070081{
Mike Travis3be83052009-01-04 05:18:01 -080082 const struct cpumask *mask;
Mike Travis39106dc2008-04-08 11:43:03 -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
Yijing Wangc489f5f2013-09-30 15:02:38 +080091 len = type ?
92 cpumask_scnprintf(buf, PAGE_SIZE-2, mask) :
93 cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
94
Mike Travis39106dc2008-04-08 11:43:03 -070095 buf[len++] = '\n';
96 buf[len] = '\0';
97 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Yijing Wangc489f5f2013-09-30 15:02:38 +0800100static ssize_t local_cpus_show(struct device *dev,
101 struct device_attribute *attr, char *buf)
102{
103 return pci_dev_show_local_cpu(dev, 1, attr, buf);
104}
105
106static ssize_t local_cpulist_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
108{
109 return pci_dev_show_local_cpu(dev, 0, attr, buf);
110}
111
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700112/*
113 * PCI Bus Class Devices
114 */
115static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
116 int type,
117 struct device_attribute *attr,
118 char *buf)
119{
120 int ret;
121 const struct cpumask *cpumask;
122
123 cpumask = cpumask_of_pcibus(to_pci_bus(dev));
124 ret = type ?
125 cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
126 cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
127 buf[ret++] = '\n';
128 buf[ret] = '\0';
129 return ret;
130}
131
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700132static ssize_t cpuaffinity_show(struct device *dev,
133 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700134{
135 return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
136}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700137static DEVICE_ATTR_RO(cpuaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700138
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700139static ssize_t cpulistaffinity_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700141{
142 return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
143}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700144static DEVICE_ATTR_RO(cpulistaffinity);
Yinghai Ludc2c2c92011-05-12 17:11:40 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* show resources */
147static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400148resource_show(struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct pci_dev * pci_dev = to_pci_dev(dev);
151 char * str = buf;
152 int i;
Yu Zhaofde09c62008-11-22 02:39:32 +0800153 int max;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700154 resource_size_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (pci_dev->subordinate)
157 max = DEVICE_COUNT_RESOURCE;
Yu Zhaofde09c62008-11-22 02:39:32 +0800158 else
159 max = PCI_BRIDGE_RESOURCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 for (i = 0; i < max; i++) {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000162 struct resource *res = &pci_dev->resource[i];
163 pci_resource_to_user(pci_dev, i, res, &start, &end);
164 str += sprintf(str,"0x%016llx 0x%016llx 0x%016llx\n",
165 (unsigned long long)start,
166 (unsigned long long)end,
167 (unsigned long long)res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 return (str - buf);
170}
171
Greg Kroah-Hartman87c8a442005-06-19 12:21:43 +0200172static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Greg KH98885492005-05-05 11:57:25 -0700173{
174 struct pci_dev *pci_dev = to_pci_dev(dev);
175
176 return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x\n",
177 pci_dev->vendor, pci_dev->device,
178 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
179 (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
180 (u8)(pci_dev->class));
181}
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800182
183static ssize_t is_enabled_store(struct device *dev,
184 struct device_attribute *attr, const char *buf,
185 size_t count)
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200186{
187 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800188 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +0900189 ssize_t result = kstrtoul(buf, 0, &val);
Trent Piepho92425a42008-11-30 17:10:12 -0800190
191 if (result < 0)
192 return result;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200193
194 /* this can crash the machine when done on the "wrong" device */
195 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800196 return -EPERM;
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200197
Trent Piepho92425a42008-11-30 17:10:12 -0800198 if (!val) {
Yuji Shimada296ccb02009-04-03 16:41:46 +0900199 if (pci_is_enabled(pdev))
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800200 pci_disable_device(pdev);
201 else
202 result = -EIO;
Trent Piepho92425a42008-11-30 17:10:12 -0800203 } else
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800204 result = pci_enable_device(pdev);
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200205
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800206 return result < 0 ? result : count;
207}
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200208
Inaky Perez-Gonzalezbae94d02006-11-22 12:40:31 -0800209static ssize_t is_enabled_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 struct pci_dev *pdev;
213
214 pdev = to_pci_dev (dev);
215 return sprintf (buf, "%u\n", atomic_read(&pdev->enable_cnt));
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200216}
217
Brice Goglin81bb0e12007-01-28 10:53:40 +0100218#ifdef CONFIG_NUMA
219static ssize_t
220numa_node_show(struct device *dev, struct device_attribute *attr, char *buf)
221{
222 return sprintf (buf, "%d\n", dev->numa_node);
223}
224#endif
225
Brice Goglinfe970642006-08-31 01:55:15 -0400226static ssize_t
Yinghai Lubb965402009-11-24 18:21:21 -0800227dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf)
228{
229 struct pci_dev *pdev = to_pci_dev(dev);
230
231 return sprintf (buf, "%d\n", fls64(pdev->dma_mask));
232}
233
234static ssize_t
235consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr,
236 char *buf)
237{
238 return sprintf (buf, "%d\n", fls64(dev->coherent_dma_mask));
239}
240
241static ssize_t
Brice Goglinfe970642006-08-31 01:55:15 -0400242msi_bus_show(struct device *dev, struct device_attribute *attr, char *buf)
243{
244 struct pci_dev *pdev = to_pci_dev(dev);
245
246 if (!pdev->subordinate)
247 return 0;
248
249 return sprintf (buf, "%u\n",
250 !(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI));
251}
252
253static ssize_t
254msi_bus_store(struct device *dev, struct device_attribute *attr,
255 const char *buf, size_t count)
256{
257 struct pci_dev *pdev = to_pci_dev(dev);
Trent Piepho92425a42008-11-30 17:10:12 -0800258 unsigned long val;
259
Jingoo Han9a994e82013-06-01 16:25:25 +0900260 if (kstrtoul(buf, 0, &val) < 0)
Trent Piepho92425a42008-11-30 17:10:12 -0800261 return -EINVAL;
Brice Goglinfe970642006-08-31 01:55:15 -0400262
263 /* bad things may happen if the no_msi flag is changed
264 * while some drivers are loaded */
265 if (!capable(CAP_SYS_ADMIN))
Trent Piepho92425a42008-11-30 17:10:12 -0800266 return -EPERM;
Brice Goglinfe970642006-08-31 01:55:15 -0400267
Trent Piepho92425a42008-11-30 17:10:12 -0800268 /* Maybe pci devices without subordinate busses shouldn't even have this
269 * attribute in the first place? */
Brice Goglinfe970642006-08-31 01:55:15 -0400270 if (!pdev->subordinate)
271 return count;
272
Trent Piepho92425a42008-11-30 17:10:12 -0800273 /* Is the flag going to change, or keep the value it already had? */
274 if (!(pdev->subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) ^
275 !!val) {
276 pdev->subordinate->bus_flags ^= PCI_BUS_FLAGS_NO_MSI;
Brice Goglinfe970642006-08-31 01:55:15 -0400277
Trent Piepho92425a42008-11-30 17:10:12 -0800278 dev_warn(&pdev->dev, "forced subordinate bus to%s support MSI,"
279 " bad things could happen\n", val ? "" : " not");
Brice Goglinfe970642006-08-31 01:55:15 -0400280 }
281
282 return count;
283}
Greg KH98885492005-05-05 11:57:25 -0700284
Alex Chiang705b1aa2009-03-20 14:56:31 -0600285static DEFINE_MUTEX(pci_remove_rescan_mutex);
286static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
287 size_t count)
288{
289 unsigned long val;
290 struct pci_bus *b = NULL;
291
Jingoo Han9a994e82013-06-01 16:25:25 +0900292 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang705b1aa2009-03-20 14:56:31 -0600293 return -EINVAL;
294
295 if (val) {
296 mutex_lock(&pci_remove_rescan_mutex);
297 while ((b = pci_find_next_bus(b)) != NULL)
298 pci_rescan_bus(b);
299 mutex_unlock(&pci_remove_rescan_mutex);
300 }
301 return count;
302}
303
304struct bus_attribute pci_bus_attrs[] = {
305 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store),
306 __ATTR_NULL
307};
Alex Chiang77c27c72009-03-20 14:56:36 -0600308
Alex Chiang738a6392009-03-20 14:56:41 -0600309static ssize_t
310dev_rescan_store(struct device *dev, struct device_attribute *attr,
311 const char *buf, size_t count)
312{
313 unsigned long val;
314 struct pci_dev *pdev = to_pci_dev(dev);
315
Jingoo Han9a994e82013-06-01 16:25:25 +0900316 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang738a6392009-03-20 14:56:41 -0600317 return -EINVAL;
318
319 if (val) {
320 mutex_lock(&pci_remove_rescan_mutex);
321 pci_rescan_bus(pdev->bus);
322 mutex_unlock(&pci_remove_rescan_mutex);
323 }
324 return count;
325}
Jiang Liudfab88b2013-05-31 12:21:31 +0800326struct device_attribute dev_rescan_attr = __ATTR(rescan, (S_IWUSR|S_IWGRP),
327 NULL, dev_rescan_store);
Alex Chiang738a6392009-03-20 14:56:41 -0600328
Alex Chiang77c27c72009-03-20 14:56:36 -0600329static void remove_callback(struct device *dev)
330{
331 struct pci_dev *pdev = to_pci_dev(dev);
332
333 mutex_lock(&pci_remove_rescan_mutex);
Yinghai Lu210647a2012-02-25 13:54:20 -0800334 pci_stop_and_remove_bus_device(pdev);
Alex Chiang77c27c72009-03-20 14:56:36 -0600335 mutex_unlock(&pci_remove_rescan_mutex);
336}
337
338static ssize_t
339remove_store(struct device *dev, struct device_attribute *dummy,
340 const char *buf, size_t count)
341{
342 int ret = 0;
343 unsigned long val;
Alex Chiang77c27c72009-03-20 14:56:36 -0600344
Jingoo Han9a994e82013-06-01 16:25:25 +0900345 if (kstrtoul(buf, 0, &val) < 0)
Alex Chiang77c27c72009-03-20 14:56:36 -0600346 return -EINVAL;
347
Alex Chiang77c27c72009-03-20 14:56:36 -0600348 /* An attribute cannot be unregistered by one of its own methods,
349 * so we have to use this roundabout approach.
350 */
351 if (val)
352 ret = device_schedule_callback(dev, remove_callback);
353 if (ret)
354 count = ret;
355 return count;
356}
Jiang Liudfab88b2013-05-31 12:21:31 +0800357struct device_attribute dev_remove_attr = __ATTR(remove, (S_IWUSR|S_IWGRP),
358 NULL, remove_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700359
360static ssize_t
361dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
362 const char *buf, size_t count)
363{
364 unsigned long val;
365 struct pci_bus *bus = to_pci_bus(dev);
366
Jingoo Han9a994e82013-06-01 16:25:25 +0900367 if (kstrtoul(buf, 0, &val) < 0)
Yinghai Lub9d320f2011-05-12 17:11:39 -0700368 return -EINVAL;
369
370 if (val) {
371 mutex_lock(&pci_remove_rescan_mutex);
Yinghai Lu2f320522012-01-21 02:08:22 -0800372 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
373 pci_rescan_bus_bridge_resize(bus->self);
374 else
375 pci_rescan_bus(bus);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700376 mutex_unlock(&pci_remove_rescan_mutex);
377 }
378 return count;
379}
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700380static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
Yinghai Lub9d320f2011-05-12 17:11:39 -0700381
Huang Ying448bd852012-06-23 10:23:51 +0800382#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
383static ssize_t d3cold_allowed_store(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 struct pci_dev *pdev = to_pci_dev(dev);
388 unsigned long val;
389
Jingoo Han9a994e82013-06-01 16:25:25 +0900390 if (kstrtoul(buf, 0, &val) < 0)
Huang Ying448bd852012-06-23 10:23:51 +0800391 return -EINVAL;
392
393 pdev->d3cold_allowed = !!val;
394 pm_runtime_resume(dev);
395
396 return count;
397}
398
399static ssize_t d3cold_allowed_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
402 struct pci_dev *pdev = to_pci_dev(dev);
403 return sprintf (buf, "%u\n", pdev->d3cold_allowed);
404}
405#endif
406
Donald Dutile17893822012-11-05 15:20:36 -0500407#ifdef CONFIG_PCI_IOV
408static ssize_t sriov_totalvfs_show(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
411{
412 struct pci_dev *pdev = to_pci_dev(dev);
413
Donald Dutilebff73152012-11-05 15:20:37 -0500414 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
Donald Dutile17893822012-11-05 15:20:36 -0500415}
416
417
418static ssize_t sriov_numvfs_show(struct device *dev,
419 struct device_attribute *attr,
420 char *buf)
421{
422 struct pci_dev *pdev = to_pci_dev(dev);
423
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700424 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
Donald Dutile17893822012-11-05 15:20:36 -0500425}
426
427/*
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700428 * num_vfs > 0; number of VFs to enable
429 * num_vfs = 0; disable all VFs
Donald Dutile17893822012-11-05 15:20:36 -0500430 *
431 * Note: SRIOV spec doesn't allow partial VF
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700432 * disable, so it's all or none.
Donald Dutile17893822012-11-05 15:20:36 -0500433 */
434static ssize_t sriov_numvfs_store(struct device *dev,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
437{
438 struct pci_dev *pdev = to_pci_dev(dev);
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700439 int ret;
440 u16 num_vfs;
Donald Dutile17893822012-11-05 15:20:36 -0500441
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700442 ret = kstrtou16(buf, 0, &num_vfs);
443 if (ret < 0)
444 return ret;
445
446 if (num_vfs > pci_sriov_get_totalvfs(pdev))
447 return -ERANGE;
448
449 if (num_vfs == pdev->sriov->num_VFs)
450 return count; /* no change */
Donald Dutile17893822012-11-05 15:20:36 -0500451
452 /* is PF driver loaded w/callback */
453 if (!pdev->driver || !pdev->driver->sriov_configure) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700454 dev_info(&pdev->dev, "Driver doesn't support SRIOV configuration via sysfs\n");
Donald Dutile17893822012-11-05 15:20:36 -0500455 return -ENOSYS;
456 }
457
Donald Dutile17893822012-11-05 15:20:36 -0500458 if (num_vfs == 0) {
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700459 /* disable VFs */
460 ret = pdev->driver->sriov_configure(pdev, 0);
461 if (ret < 0)
462 return ret;
463 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500464 }
465
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700466 /* enable VFs */
467 if (pdev->sriov->num_VFs) {
468 dev_warn(&pdev->dev, "%d VFs already enabled. Disable before enabling %d VFs\n",
469 pdev->sriov->num_VFs, num_vfs);
470 return -EBUSY;
471 }
Donald Dutile17893822012-11-05 15:20:36 -0500472
Bjorn Helgaasfaa48a52012-12-26 10:39:22 -0700473 ret = pdev->driver->sriov_configure(pdev, num_vfs);
474 if (ret < 0)
475 return ret;
476
477 if (ret != num_vfs)
478 dev_warn(&pdev->dev, "%d VFs requested; only %d enabled\n",
479 num_vfs, ret);
480
481 return count;
Donald Dutile17893822012-11-05 15:20:36 -0500482}
483
484static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
485static struct device_attribute sriov_numvfs_attr =
486 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
487 sriov_numvfs_show, sriov_numvfs_store);
488#endif /* CONFIG_PCI_IOV */
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490struct device_attribute pci_dev_attrs[] = {
491 __ATTR_RO(resource),
492 __ATTR_RO(vendor),
493 __ATTR_RO(device),
494 __ATTR_RO(subsystem_vendor),
495 __ATTR_RO(subsystem_device),
496 __ATTR_RO(class),
497 __ATTR_RO(irq),
498 __ATTR_RO(local_cpus),
Mike Travis39106dc2008-04-08 11:43:03 -0700499 __ATTR_RO(local_cpulist),
Greg KH98885492005-05-05 11:57:25 -0700500 __ATTR_RO(modalias),
Brice Goglin81bb0e12007-01-28 10:53:40 +0100501#ifdef CONFIG_NUMA
502 __ATTR_RO(numa_node),
503#endif
Yinghai Lubb965402009-11-24 18:21:21 -0800504 __ATTR_RO(dma_mask_bits),
505 __ATTR_RO(consistent_dma_mask_bits),
Arjan van de Ven9f125d32006-04-29 10:59:08 +0200506 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
Doug Thompsonbdee9d92006-06-14 16:59:48 -0700507 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
508 broken_parity_status_show,broken_parity_status_store),
Brice Goglinfe970642006-08-31 01:55:15 -0400509 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
Huang Ying448bd852012-06-23 10:23:51 +0800510#if defined(CONFIG_PM_RUNTIME) && defined(CONFIG_ACPI)
511 __ATTR(d3cold_allowed, 0644, d3cold_allowed_show, d3cold_allowed_store),
512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 __ATTR_NULL,
514};
515
Greg Kroah-Hartman56039e62013-07-24 15:05:17 -0700516static struct attribute *pcibus_attrs[] = {
517 &dev_attr_rescan.attr,
518 &dev_attr_cpuaffinity.attr,
519 &dev_attr_cpulistaffinity.attr,
520 NULL,
521};
522
523static const struct attribute_group pcibus_group = {
524 .attrs = pcibus_attrs,
525};
526
527const struct attribute_group *pcibus_groups[] = {
528 &pcibus_group,
529 NULL,
Yinghai Lub9d320f2011-05-12 17:11:39 -0700530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532static ssize_t
Dave Airlie217f45d2009-03-04 05:57:05 +0000533boot_vga_show(struct device *dev, struct device_attribute *attr, char *buf)
534{
535 struct pci_dev *pdev = to_pci_dev(dev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400536 struct pci_dev *vga_dev = vga_default_device();
537
538 if (vga_dev)
539 return sprintf(buf, "%u\n", (pdev == vga_dev));
Dave Airlie217f45d2009-03-04 05:57:05 +0000540
541 return sprintf(buf, "%u\n",
542 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
543 IORESOURCE_ROM_SHADOW));
544}
545struct device_attribute vga_attr = __ATTR_RO(boot_vga);
546
547static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700548pci_read_config(struct file *filp, struct kobject *kobj,
549 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800550 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
553 unsigned int size = 64;
554 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900555 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Several chips lock up trying to read undefined config space */
Eric Parisb7e724d2012-01-03 12:25:15 -0500558 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 size = dev->cfg_size;
560 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
561 size = 128;
562 }
563
564 if (off > size)
565 return 0;
566 if (off + count > size) {
567 size -= off;
568 count = size;
569 } else {
570 size = count;
571 }
572
Huang Ying3d8387e2012-08-15 09:43:03 +0800573 pci_config_pm_runtime_get(dev);
574
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900575 if ((off & 1) && size) {
576 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700577 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900578 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900580 size--;
581 }
582
583 if ((off & 3) && size > 2) {
584 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700585 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900586 data[off - init_off] = val & 0xff;
587 data[off - init_off + 1] = (val >> 8) & 0xff;
588 off += 2;
589 size -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900593 u32 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700594 pci_user_read_config_dword(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900595 data[off - init_off] = val & 0xff;
596 data[off - init_off + 1] = (val >> 8) & 0xff;
597 data[off - init_off + 2] = (val >> 16) & 0xff;
598 data[off - init_off + 3] = (val >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 off += 4;
600 size -= 4;
601 }
602
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900603 if (size >= 2) {
604 u16 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700605 pci_user_read_config_word(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900606 data[off - init_off] = val & 0xff;
607 data[off - init_off + 1] = (val >> 8) & 0xff;
608 off += 2;
609 size -= 2;
610 }
611
612 if (size > 0) {
613 u8 val;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700614 pci_user_read_config_byte(dev, off, &val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900615 data[off - init_off] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 off++;
617 --size;
618 }
619
Huang Ying3d8387e2012-08-15 09:43:03 +0800620 pci_config_pm_runtime_put(dev);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return count;
623}
624
625static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700626pci_write_config(struct file* filp, struct kobject *kobj,
627 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800628 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct pci_dev *dev = to_pci_dev(container_of(kobj,struct device,kobj));
631 unsigned int size = count;
632 loff_t init_off = off;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900633 u8 *data = (u8*) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (off > dev->cfg_size)
636 return 0;
637 if (off + count > dev->cfg_size) {
638 size = dev->cfg_size - off;
639 count = size;
640 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900641
Huang Ying3d8387e2012-08-15 09:43:03 +0800642 pci_config_pm_runtime_get(dev);
643
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900644 if ((off & 1) && size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700645 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 off++;
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900647 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900649
650 if ((off & 3) && size > 2) {
651 u16 val = data[off - init_off];
652 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700653 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900654 off += 2;
655 size -= 2;
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 while (size > 3) {
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900659 u32 val = data[off - init_off];
660 val |= (u32) data[off - init_off + 1] << 8;
661 val |= (u32) data[off - init_off + 2] << 16;
662 val |= (u32) data[off - init_off + 3] << 24;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700663 pci_user_write_config_dword(dev, off, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 off += 4;
665 size -= 4;
666 }
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900667
668 if (size >= 2) {
669 u16 val = data[off - init_off];
670 val |= (u16) data[off - init_off + 1] << 8;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700671 pci_user_write_config_word(dev, off, val);
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900672 off += 2;
673 size -= 2;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
ssant@in.ibm.com4c0619a2005-04-08 14:53:31 +0900676 if (size) {
Brian Kinge04b0ea2005-09-27 01:21:55 -0700677 pci_user_write_config_byte(dev, off, data[off - init_off]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 off++;
679 --size;
680 }
681
Huang Ying3d8387e2012-08-15 09:43:03 +0800682 pci_config_pm_runtime_put(dev);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return count;
685}
686
Ben Hutchings94e61082008-03-05 16:52:39 +0000687static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700688read_vpd_attr(struct file *filp, struct kobject *kobj,
689 struct bin_attribute *bin_attr,
Ben Hutchings94e61082008-03-05 16:52:39 +0000690 char *buf, loff_t off, size_t count)
691{
692 struct pci_dev *dev =
693 to_pci_dev(container_of(kobj, struct device, kobj));
Ben Hutchings94e61082008-03-05 16:52:39 +0000694
695 if (off > bin_attr->size)
696 count = 0;
697 else if (count > bin_attr->size - off)
698 count = bin_attr->size - off;
Ben Hutchings94e61082008-03-05 16:52:39 +0000699
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800700 return pci_read_vpd(dev, off, count, buf);
701}
Ben Hutchings94e61082008-03-05 16:52:39 +0000702
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800703static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700704write_vpd_attr(struct file *filp, struct kobject *kobj,
705 struct bin_attribute *bin_attr,
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800706 char *buf, loff_t off, size_t count)
707{
708 struct pci_dev *dev =
709 to_pci_dev(container_of(kobj, struct device, kobj));
710
711 if (off > bin_attr->size)
712 count = 0;
713 else if (count > bin_attr->size - off)
714 count = bin_attr->size - off;
715
716 return pci_write_vpd(dev, off, count, buf);
Ben Hutchings94e61082008-03-05 16:52:39 +0000717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#ifdef HAVE_PCI_LEGACY
720/**
721 * pci_read_legacy_io - read byte(s) from legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700722 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700724 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * @buf: buffer to store results
726 * @off: offset into legacy I/O port space
727 * @count: number of bytes to read
728 *
729 * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
730 * callback routine (pci_legacy_read).
731 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000732static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700733pci_read_legacy_io(struct file *filp, struct kobject *kobj,
734 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800735 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400738 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 kobj));
740
741 /* Only support 1, 2 or 4 byte accesses */
742 if (count != 1 && count != 2 && count != 4)
743 return -EINVAL;
744
745 return pci_legacy_read(bus, off, (u32 *)buf, count);
746}
747
748/**
749 * pci_write_legacy_io - write byte(s) to legacy I/O port space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700750 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * @kobj: kobject corresponding to file to read from
Randy Dunlapcffb2fa2009-04-10 15:17:50 -0700752 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * @buf: buffer containing value to be written
754 * @off: offset into legacy I/O port space
755 * @count: number of bytes to write
756 *
757 * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
758 * callback routine (pci_legacy_write).
759 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000760static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700761pci_write_legacy_io(struct file *filp, struct kobject *kobj,
762 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +0800763 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400766 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 kobj));
768 /* Only support 1, 2 or 4 byte accesses */
769 if (count != 1 && count != 2 && count != 4)
770 return -EINVAL;
771
772 return pci_legacy_write(bus, off, *(u32 *)buf, count);
773}
774
775/**
776 * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700777 * @filp: open sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * @kobj: kobject corresponding to device to be mapped
779 * @attr: struct bin_attribute for this file
780 * @vma: struct vm_area_struct passed to mmap
781 *
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000782 * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * legacy memory space (first meg of bus space) into application virtual
784 * memory space.
785 */
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000786static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700787pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
788 struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct vm_area_struct *vma)
790{
791 struct pci_bus *bus = to_pci_bus(container_of(kobj,
Greg Kroah-Hartmanfd7d1ce2007-05-22 22:47:54 -0400792 struct device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 kobj));
794
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000795 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
796}
797
798/**
799 * pci_mmap_legacy_io - map legacy PCI IO into user memory space
Chris Wright2c3c8be2010-05-12 18:28:57 -0700800 * @filp: open sysfs file
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000801 * @kobj: kobject corresponding to device to be mapped
802 * @attr: struct bin_attribute for this file
803 * @vma: struct vm_area_struct passed to mmap
804 *
805 * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
806 * legacy IO space (first meg of bus space) into application virtual
807 * memory space. Returns -ENOSYS if the operation isn't supported
808 */
809static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700810pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
811 struct bin_attribute *attr,
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000812 struct vm_area_struct *vma)
813{
814 struct pci_bus *bus = to_pci_bus(container_of(kobj,
815 struct device,
816 kobj));
817
818 return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
819}
820
821/**
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300822 * pci_adjust_legacy_attr - adjustment of legacy file attributes
823 * @b: bus to create files under
824 * @mmap_type: I/O port or memory
825 *
826 * Stub implementation. Can be overridden by arch if necessary.
827 */
828void __weak
829pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
830{
831 return;
832}
833
834/**
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000835 * pci_create_legacy_files - create legacy I/O port and memory files
836 * @b: bus to create files under
837 *
838 * Some platforms allow access to legacy I/O port and ISA memory space on
839 * a per-bus basis. This routine creates the files and ties them into
840 * their associated read, write and mmap files from pci-sysfs.c
841 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300842 * On error unwind, but don't propagate the error to the caller
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000843 * as it is ok to set up the PCI bus without these files.
844 */
845void pci_create_legacy_files(struct pci_bus *b)
846{
847 int error;
848
849 b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
850 GFP_ATOMIC);
851 if (!b->legacy_io)
852 goto kzalloc_err;
853
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100854 sysfs_bin_attr_init(b->legacy_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000855 b->legacy_io->attr.name = "legacy_io";
856 b->legacy_io->size = 0xffff;
857 b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
858 b->legacy_io->read = pci_read_legacy_io;
859 b->legacy_io->write = pci_write_legacy_io;
860 b->legacy_io->mmap = pci_mmap_legacy_io;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300861 pci_adjust_legacy_attr(b, pci_mmap_io);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000862 error = device_create_bin_file(&b->dev, b->legacy_io);
863 if (error)
864 goto legacy_io_err;
865
866 /* Allocated above after the legacy_io struct */
867 b->legacy_mem = b->legacy_io + 1;
Mel Gorman6757eca32010-03-10 22:48:34 +0000868 sysfs_bin_attr_init(b->legacy_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000869 b->legacy_mem->attr.name = "legacy_mem";
870 b->legacy_mem->size = 1024*1024;
871 b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
872 b->legacy_mem->mmap = pci_mmap_legacy_mem;
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +0300873 pci_adjust_legacy_attr(b, pci_mmap_mem);
Benjamin Herrenschmidtf19aeb12008-10-03 19:49:32 +1000874 error = device_create_bin_file(&b->dev, b->legacy_mem);
875 if (error)
876 goto legacy_mem_err;
877
878 return;
879
880legacy_mem_err:
881 device_remove_bin_file(&b->dev, b->legacy_io);
882legacy_io_err:
883 kfree(b->legacy_io);
884 b->legacy_io = NULL;
885kzalloc_err:
886 printk(KERN_WARNING "pci: warning: could not create legacy I/O port "
887 "and ISA memory resources to sysfs\n");
888 return;
889}
890
891void pci_remove_legacy_files(struct pci_bus *b)
892{
893 if (b->legacy_io) {
894 device_remove_bin_file(&b->dev, b->legacy_io);
895 device_remove_bin_file(&b->dev, b->legacy_mem);
896 kfree(b->legacy_io); /* both are allocated here */
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899#endif /* HAVE_PCI_LEGACY */
900
901#ifdef HAVE_PCI_MMAP
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700902
Martin Wilck3b519e42010-11-10 11:03:21 +0100903int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
904 enum pci_mmap_api mmap_api)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700905{
Martin Wilck3b519e42010-11-10 11:03:21 +0100906 unsigned long nr, start, size, pci_start;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700907
Martin Wilck3b519e42010-11-10 11:03:21 +0100908 if (pci_resource_len(pdev, resno) == 0)
909 return 0;
Libin64b00172013-04-15 02:48:54 +0000910 nr = vma_pages(vma);
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700911 start = vma->vm_pgoff;
Ed Swierk88e7df02008-11-03 14:41:16 -0800912 size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
Darrick J. Wong8c05cd02010-11-16 09:13:41 -0800913 pci_start = (mmap_api == PCI_MMAP_PROCFS) ?
Martin Wilck3b519e42010-11-10 11:03:21 +0100914 pci_resource_start(pdev, resno) >> PAGE_SHIFT : 0;
915 if (start >= pci_start && start < pci_start + size &&
916 start + nr <= pci_start + size)
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700917 return 1;
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700918 return 0;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/**
922 * pci_mmap_resource - map a PCI resource into user memory space
923 * @kobj: kobject for mapping
924 * @attr: struct bin_attribute for the file being mapped
925 * @vma: struct vm_area_struct passed into the mmap
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700926 * @write_combine: 1 for write_combine mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
928 * Use the regular PCI mapping routines to map a PCI resource into userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 */
930static int
931pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700932 struct vm_area_struct *vma, int write_combine)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
935 struct device, kobj));
Kulikov Vasiliya3f58352010-06-29 14:15:28 +0400936 struct resource *res = attr->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 enum pci_mmap_state mmap_type;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700938 resource_size_t start, end;
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000939 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000941 for (i = 0; i < PCI_ROM_RESOURCE; i++)
942 if (res == &pdev->resource[i])
943 break;
944 if (i >= PCI_ROM_RESOURCE)
945 return -ENODEV;
946
Martin Wilck3b519e42010-11-10 11:03:21 +0100947 if (!pci_mmap_fits(pdev, i, vma, PCI_MMAP_SYSFS)) {
948 WARN(1, "process \"%s\" tried to map 0x%08lx bytes "
949 "at page 0x%08lx on %s BAR %d (start 0x%16Lx, size 0x%16Lx)\n",
950 current->comm, vma->vm_end-vma->vm_start, vma->vm_pgoff,
951 pci_name(pdev), i,
Randy Dunlape25cd062010-11-13 08:44:33 -0800952 (u64)pci_resource_start(pdev, i),
953 (u64)pci_resource_len(pdev, i));
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700954 return -EINVAL;
Martin Wilck3b519e42010-11-10 11:03:21 +0100955 }
Linus Torvaldsb5ff7df2008-10-02 18:52:51 -0700956
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000957 /* pci_mmap_page_range() expects the same kind of entry as coming
958 * from /proc/bus/pci/ which is a "user visible" value. If this is
959 * different from the resource itself, arch will do necessary fixup.
960 */
961 pci_resource_to_user(pdev, i, res, &start, &end);
962 vma->vm_pgoff += start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
964
Arjan van de Vene8de1482008-10-22 19:55:31 -0700965 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(start))
966 return -EINVAL;
967
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700968 return pci_mmap_page_range(pdev, vma, mmap_type, write_combine);
969}
970
971static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700972pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
973 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700974 struct vm_area_struct *vma)
975{
976 return pci_mmap_resource(kobj, attr, vma, 0);
977}
978
979static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700980pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
981 struct bin_attribute *attr,
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -0700982 struct vm_area_struct *vma)
983{
984 return pci_mmap_resource(kobj, attr, vma, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Alex Williamson86333282010-07-19 09:45:34 -0600987static ssize_t
988pci_resource_io(struct file *filp, struct kobject *kobj,
989 struct bin_attribute *attr, char *buf,
990 loff_t off, size_t count, bool write)
991{
992 struct pci_dev *pdev = to_pci_dev(container_of(kobj,
993 struct device, kobj));
994 struct resource *res = attr->private;
995 unsigned long port = off;
996 int i;
997
998 for (i = 0; i < PCI_ROM_RESOURCE; i++)
999 if (res == &pdev->resource[i])
1000 break;
1001 if (i >= PCI_ROM_RESOURCE)
1002 return -ENODEV;
1003
1004 port += pci_resource_start(pdev, i);
1005
1006 if (port > pci_resource_end(pdev, i))
1007 return 0;
1008
1009 if (port + count - 1 > pci_resource_end(pdev, i))
1010 return -EINVAL;
1011
1012 switch (count) {
1013 case 1:
1014 if (write)
1015 outb(*(u8 *)buf, port);
1016 else
1017 *(u8 *)buf = inb(port);
1018 return 1;
1019 case 2:
1020 if (write)
1021 outw(*(u16 *)buf, port);
1022 else
1023 *(u16 *)buf = inw(port);
1024 return 2;
1025 case 4:
1026 if (write)
1027 outl(*(u32 *)buf, port);
1028 else
1029 *(u32 *)buf = inl(port);
1030 return 4;
1031 }
1032 return -EINVAL;
1033}
1034
1035static ssize_t
1036pci_read_resource_io(struct file *filp, struct kobject *kobj,
1037 struct bin_attribute *attr, char *buf,
1038 loff_t off, size_t count)
1039{
1040 return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1041}
1042
1043static ssize_t
1044pci_write_resource_io(struct file *filp, struct kobject *kobj,
1045 struct bin_attribute *attr, char *buf,
1046 loff_t off, size_t count)
1047{
1048 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/**
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001052 * pci_remove_resource_files - cleanup resource files
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001053 * @pdev: dev to cleanup
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001054 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001055 * If we created resource files for @pdev, remove them from sysfs and
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001056 * free their resources.
1057 */
1058static void
1059pci_remove_resource_files(struct pci_dev *pdev)
1060{
1061 int i;
1062
1063 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1064 struct bin_attribute *res_attr;
1065
1066 res_attr = pdev->res_attr[i];
1067 if (res_attr) {
1068 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1069 kfree(res_attr);
1070 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001071
1072 res_attr = pdev->res_attr_wc[i];
1073 if (res_attr) {
1074 sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1075 kfree(res_attr);
1076 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001077 }
1078}
1079
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001080static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1081{
1082 /* allocate attribute structure, piggyback attribute name */
1083 int name_len = write_combine ? 13 : 10;
1084 struct bin_attribute *res_attr;
1085 int retval;
1086
1087 res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1088 if (res_attr) {
1089 char *res_attr_name = (char *)(res_attr + 1);
1090
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001091 sysfs_bin_attr_init(res_attr);
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001092 if (write_combine) {
1093 pdev->res_attr_wc[num] = res_attr;
1094 sprintf(res_attr_name, "resource%d_wc", num);
1095 res_attr->mmap = pci_mmap_resource_wc;
1096 } else {
1097 pdev->res_attr[num] = res_attr;
1098 sprintf(res_attr_name, "resource%d", num);
1099 res_attr->mmap = pci_mmap_resource_uc;
1100 }
Alex Williamson86333282010-07-19 09:45:34 -06001101 if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1102 res_attr->read = pci_read_resource_io;
1103 res_attr->write = pci_write_resource_io;
1104 }
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001105 res_attr->attr.name = res_attr_name;
1106 res_attr->attr.mode = S_IRUSR | S_IWUSR;
1107 res_attr->size = pci_resource_len(pdev, num);
1108 res_attr->private = &pdev->resource[num];
1109 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1110 } else
1111 retval = -ENOMEM;
1112
1113 return retval;
1114}
1115
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001116/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * pci_create_resource_files - create resource files in sysfs for @dev
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001118 * @pdev: dev in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001120 * Walk the resources in @pdev creating files for each resource available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001122static int pci_create_resource_files(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 int i;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001125 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* Expose the PCI resources from this device as files */
1128 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* skip empty resources */
1131 if (!pci_resource_len(pdev, i))
1132 continue;
1133
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001134 retval = pci_create_attr(pdev, i, 0);
1135 /* for prefetchable resources, create a WC mappable file */
1136 if (!retval && pdev->resource[i].flags & IORESOURCE_PREFETCH)
1137 retval = pci_create_attr(pdev, i, 1);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -05001138
venkatesh.pallipadi@intel.com45aec1ae2008-03-18 17:00:22 -07001139 if (retval) {
1140 pci_remove_resource_files(pdev);
1141 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146#else /* !HAVE_PCI_MMAP */
Ivan Kokshaysky10a0ef32009-02-17 13:46:53 +03001147int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
1148void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149#endif /* HAVE_PCI_MMAP */
1150
1151/**
1152 * pci_write_rom - used to enable access to the PCI ROM display
Chris Wright2c3c8be2010-05-12 18:28:57 -07001153 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001155 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * @buf: user input
1157 * @off: file offset
1158 * @count: number of byte in input
1159 *
1160 * writing anything except 0 enables it
1161 */
1162static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001163pci_write_rom(struct file *filp, struct kobject *kobj,
1164 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001165 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1168
1169 if ((off == 0) && (*buf == '0') && (count == 2))
1170 pdev->rom_attr_enabled = 0;
1171 else
1172 pdev->rom_attr_enabled = 1;
1173
1174 return count;
1175}
1176
1177/**
1178 * pci_read_rom - read a PCI ROM
Chris Wright2c3c8be2010-05-12 18:28:57 -07001179 * @filp: sysfs file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * @kobj: kernel object handle
Randy Dunlapcffb2fa2009-04-10 15:17:50 -07001181 * @bin_attr: struct bin_attribute for this file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * @buf: where to put the data we read from the ROM
1183 * @off: file offset
1184 * @count: number of bytes to read
1185 *
1186 * Put @count bytes starting at @off into @buf from the ROM in the PCI
1187 * device corresponding to @kobj.
1188 */
1189static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07001190pci_read_rom(struct file *filp, struct kobject *kobj,
1191 struct bin_attribute *bin_attr,
Zhang Rui91a69022007-06-09 13:57:22 +08001192 char *buf, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct pci_dev *pdev = to_pci_dev(container_of(kobj, struct device, kobj));
1195 void __iomem *rom;
1196 size_t size;
1197
1198 if (!pdev->rom_attr_enabled)
1199 return -EINVAL;
1200
1201 rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
Timothy S. Nelson97c44832009-01-30 06:12:47 +11001202 if (!rom || !size)
1203 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 if (off >= size)
1206 count = 0;
1207 else {
1208 if (off + count > size)
1209 count = size - off;
1210
1211 memcpy_fromio(buf, rom + off, count);
1212 }
1213 pci_unmap_rom(pdev, rom);
1214
1215 return count;
1216}
1217
1218static struct bin_attribute pci_config_attr = {
1219 .attr = {
1220 .name = "config",
1221 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001223 .size = PCI_CFG_SPACE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 .read = pci_read_config,
1225 .write = pci_write_config,
1226};
1227
1228static struct bin_attribute pcie_config_attr = {
1229 .attr = {
1230 .name = "config",
1231 .mode = S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 },
Zhao, Yu557848c2008-10-13 19:18:07 +08001233 .size = PCI_CFG_SPACE_EXP_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 .read = pci_read_config,
1235 .write = pci_write_config,
1236};
1237
Bjorn Helgaasd6d88c82012-06-19 06:54:49 -06001238int __weak pcibios_add_platform_entries(struct pci_dev *dev)
Michael Ellerman575e3342007-05-08 12:03:07 +10001239{
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001240 return 0;
Michael Ellerman575e3342007-05-08 12:03:07 +10001241}
1242
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001243static ssize_t reset_store(struct device *dev,
1244 struct device_attribute *attr, const char *buf,
1245 size_t count)
1246{
1247 struct pci_dev *pdev = to_pci_dev(dev);
1248 unsigned long val;
Jingoo Han9a994e82013-06-01 16:25:25 +09001249 ssize_t result = kstrtoul(buf, 0, &val);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001250
1251 if (result < 0)
1252 return result;
1253
1254 if (val != 1)
1255 return -EINVAL;
Michal Schmidt447c5dd2010-05-11 11:44:54 +02001256
1257 result = pci_reset_function(pdev);
1258 if (result < 0)
1259 return result;
1260
1261 return count;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001262}
1263
1264static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1265
Zhao, Yu280c73d2008-10-13 20:01:00 +08001266static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1267{
1268 int retval;
1269 struct bin_attribute *attr;
1270
1271 /* If the device has VPD, try to expose it in sysfs. */
1272 if (dev->vpd) {
1273 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1274 if (!attr)
1275 return -ENOMEM;
1276
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001277 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001278 attr->size = dev->vpd->len;
1279 attr->attr.name = "vpd";
1280 attr->attr.mode = S_IRUSR | S_IWUSR;
Stephen Hemminger287d19c2008-12-18 09:17:16 -08001281 attr->read = read_vpd_attr;
1282 attr->write = write_vpd_attr;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001283 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
1284 if (retval) {
Ben Hutchings0f12a4e2011-01-13 19:47:56 +00001285 kfree(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001286 return retval;
1287 }
1288 dev->vpd->attr = attr;
1289 }
1290
1291 /* Active State Power Management */
1292 pcie_aspm_create_sysfs_dev_files(dev);
1293
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001294 if (!pci_probe_reset_function(dev)) {
1295 retval = device_create_file(&dev->dev, &reset_attr);
1296 if (retval)
1297 goto error;
1298 dev->reset_fn = 1;
1299 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001300 return 0;
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001301
1302error:
1303 pcie_aspm_remove_sysfs_dev_files(dev);
1304 if (dev->vpd && dev->vpd->attr) {
1305 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1306 kfree(dev->vpd->attr);
1307 }
1308
1309 return retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001310}
1311
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001312int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001314 int retval;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001315 int rom_size = 0;
1316 struct bin_attribute *attr;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!sysfs_initialized)
1319 return -EACCES;
1320
Zhao, Yu557848c2008-10-13 19:18:07 +08001321 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001322 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 else
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001324 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1325 if (retval)
1326 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001328 retval = pci_create_resource_files(pdev);
1329 if (retval)
Zhao, Yu280c73d2008-10-13 20:01:00 +08001330 goto err_config_file;
1331
1332 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1333 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1334 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1335 rom_size = 0x20000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* If the device has a ROM, try to expose it in sysfs. */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001338 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001339 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001340 if (!attr) {
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001341 retval = -ENOMEM;
Michael Ellerman9890b122007-04-18 13:34:12 +10001342 goto err_resource_files;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Eric W. Biedermana07e4152010-02-11 15:23:05 -08001344 sysfs_bin_attr_init(attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001345 attr->size = rom_size;
1346 attr->attr.name = "rom";
Alex Williamsonff295302011-01-05 10:26:41 -07001347 attr->attr.mode = S_IRUSR | S_IWUSR;
Zhao, Yu280c73d2008-10-13 20:01:00 +08001348 attr->read = pci_read_rom;
1349 attr->write = pci_write_rom;
1350 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1351 if (retval) {
1352 kfree(attr);
1353 goto err_resource_files;
1354 }
1355 pdev->rom_attr = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* add platform-specific attributes */
Zhao, Yu280c73d2008-10-13 20:01:00 +08001359 retval = pcibios_add_platform_entries(pdev);
1360 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001361 goto err_rom_file;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001362
Zhao, Yu280c73d2008-10-13 20:01:00 +08001363 /* add sysfs entries for various capabilities */
1364 retval = pci_create_capabilities_sysfs(pdev);
1365 if (retval)
Yinghai Lu625e1d52012-11-05 15:20:35 -05001366 goto err_rom_file;
Shaohua Li7d715a62008-02-25 09:46:41 +08001367
Narendra K911e1c92010-07-26 05:56:50 -05001368 pci_create_firmware_label_files(pdev);
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return 0;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001371
Michael Ellermana2cd52c2007-05-08 12:03:08 +10001372err_rom_file:
Zhao, Yu280c73d2008-10-13 20:01:00 +08001373 if (rom_size) {
Ben Hutchings94e61082008-03-05 16:52:39 +00001374 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
Zhao, Yu280c73d2008-10-13 20:01:00 +08001375 kfree(pdev->rom_attr);
1376 pdev->rom_attr = NULL;
1377 }
Michael Ellerman9890b122007-04-18 13:34:12 +10001378err_resource_files:
1379 pci_remove_resource_files(pdev);
Ben Hutchings94e61082008-03-05 16:52:39 +00001380err_config_file:
Zhao, Yu557848c2008-10-13 19:18:07 +08001381 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001382 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1383 else
1384 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1385err:
1386 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Zhao, Yu280c73d2008-10-13 20:01:00 +08001389static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1390{
1391 if (dev->vpd && dev->vpd->attr) {
1392 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
1393 kfree(dev->vpd->attr);
1394 }
1395
1396 pcie_aspm_remove_sysfs_dev_files(dev);
Michael S. Tsirkin711d5772009-07-27 23:37:48 +03001397 if (dev->reset_fn) {
1398 device_remove_file(&dev->dev, &reset_attr);
1399 dev->reset_fn = 0;
1400 }
Zhao, Yu280c73d2008-10-13 20:01:00 +08001401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/**
1404 * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1405 * @pdev: device whose entries we should free
1406 *
1407 * Cleanup when @pdev is removed from sysfs.
1408 */
1409void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1410{
Zhao, Yu280c73d2008-10-13 20:01:00 +08001411 int rom_size = 0;
1412
David Millerd67afe52006-11-10 12:27:48 -08001413 if (!sysfs_initialized)
1414 return;
1415
Zhao, Yu280c73d2008-10-13 20:01:00 +08001416 pci_remove_capabilities_sysfs(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +08001417
Zhao, Yu557848c2008-10-13 19:18:07 +08001418 if (pdev->cfg_size < PCI_CFG_SPACE_EXP_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1420 else
1421 sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1422
1423 pci_remove_resource_files(pdev);
1424
Zhao, Yu280c73d2008-10-13 20:01:00 +08001425 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
1426 rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1427 else if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
1428 rom_size = 0x20000;
1429
1430 if (rom_size && pdev->rom_attr) {
1431 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1432 kfree(pdev->rom_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
Narendra K911e1c92010-07-26 05:56:50 -05001434
1435 pci_remove_firmware_label_files(pdev);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439static int __init pci_sysfs_init(void)
1440{
1441 struct pci_dev *pdev = NULL;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001442 int retval;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 sysfs_initialized = 1;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001445 for_each_pci_dev(pdev) {
1446 retval = pci_create_sysfs_dev_files(pdev);
Julia Lawall151fc5d2007-11-20 08:41:16 +01001447 if (retval) {
1448 pci_dev_put(pdev);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001449 return retval;
Julia Lawall151fc5d2007-11-20 08:41:16 +01001450 }
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -07001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 return 0;
1454}
1455
Jesse Barnes40ee9e92007-03-24 11:03:32 -07001456late_initcall(pci_sysfs_init);
Yinghai Lu4e15c462012-11-05 15:20:34 -05001457
1458static struct attribute *pci_dev_dev_attrs[] = {
Yinghai Lu625e1d52012-11-05 15:20:35 -05001459 &vga_attr.attr,
Yinghai Lu4e15c462012-11-05 15:20:34 -05001460 NULL,
1461};
1462
1463static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1464 struct attribute *a, int n)
1465{
Yinghai Lu625e1d52012-11-05 15:20:35 -05001466 struct device *dev = container_of(kobj, struct device, kobj);
1467 struct pci_dev *pdev = to_pci_dev(dev);
1468
1469 if (a == &vga_attr.attr)
1470 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1471 return 0;
1472
Yinghai Lu4e15c462012-11-05 15:20:34 -05001473 return a->mode;
1474}
1475
Jiang Liudfab88b2013-05-31 12:21:31 +08001476static struct attribute *pci_dev_hp_attrs[] = {
1477 &dev_remove_attr.attr,
1478 &dev_rescan_attr.attr,
1479 NULL,
1480};
1481
1482static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1483 struct attribute *a, int n)
1484{
1485 struct device *dev = container_of(kobj, struct device, kobj);
1486 struct pci_dev *pdev = to_pci_dev(dev);
1487
1488 if (pdev->is_virtfn)
1489 return 0;
1490
1491 return a->mode;
1492}
1493
1494static struct attribute_group pci_dev_hp_attr_group = {
1495 .attrs = pci_dev_hp_attrs,
1496 .is_visible = pci_dev_hp_attrs_are_visible,
1497};
1498
Donald Dutile17893822012-11-05 15:20:36 -05001499#ifdef CONFIG_PCI_IOV
1500static struct attribute *sriov_dev_attrs[] = {
1501 &sriov_totalvfs_attr.attr,
1502 &sriov_numvfs_attr.attr,
1503 NULL,
1504};
1505
1506static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1507 struct attribute *a, int n)
1508{
1509 struct device *dev = container_of(kobj, struct device, kobj);
1510
1511 if (!dev_is_pf(dev))
1512 return 0;
1513
1514 return a->mode;
1515}
1516
1517static struct attribute_group sriov_dev_attr_group = {
1518 .attrs = sriov_dev_attrs,
1519 .is_visible = sriov_attrs_are_visible,
1520};
1521#endif /* CONFIG_PCI_IOV */
1522
Yinghai Lu4e15c462012-11-05 15:20:34 -05001523static struct attribute_group pci_dev_attr_group = {
1524 .attrs = pci_dev_dev_attrs,
1525 .is_visible = pci_dev_attrs_are_visible,
1526};
1527
1528static const struct attribute_group *pci_dev_attr_groups[] = {
1529 &pci_dev_attr_group,
Jiang Liudfab88b2013-05-31 12:21:31 +08001530 &pci_dev_hp_attr_group,
Donald Dutile17893822012-11-05 15:20:36 -05001531#ifdef CONFIG_PCI_IOV
1532 &sriov_dev_attr_group,
1533#endif
Yinghai Lu4e15c462012-11-05 15:20:34 -05001534 NULL,
1535};
1536
1537struct device_type pci_dev_type = {
1538 .groups = pci_dev_attr_groups,
1539};