1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
4  * (C) Copyright 2002-2004 IBM Corp.
5  * (C) Copyright 2003 Matthew Wilcox
6  * (C) Copyright 2003 Hewlett-Packard
7  * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
8  * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
9  *
10  * File attributes for PCI devices
11  *
12  * Modeled after usb's driverfs.c
13  */
14 
15 
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/pci.h>
19 #include <linux/stat.h>
20 #include <linux/export.h>
21 #include <linux/topology.h>
22 #include <linux/mm.h>
23 #include <linux/fs.h>
24 #include <linux/capability.h>
25 #include <linux/security.h>
26 #include <linux/slab.h>
27 #include <linux/vgaarb.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/of.h>
30 #include "pci.h"
31 
32 static int sysfs_initialized;	/* = 0 */
33 
34 /* show configuration fields */
35 #define pci_config_attr(field, format_string)				\
36 static ssize_t								\
37 field##_show(struct device *dev, struct device_attribute *attr, char *buf)				\
38 {									\
39 	struct pci_dev *pdev;						\
40 									\
41 	pdev = to_pci_dev(dev);						\
42 	return sprintf(buf, format_string, pdev->field);		\
43 }									\
44 static DEVICE_ATTR_RO(field)
45 
46 pci_config_attr(vendor, "0x%04x\n");
47 pci_config_attr(device, "0x%04x\n");
48 pci_config_attr(subsystem_vendor, "0x%04x\n");
49 pci_config_attr(subsystem_device, "0x%04x\n");
50 pci_config_attr(revision, "0x%02x\n");
51 pci_config_attr(class, "0x%06x\n");
52 pci_config_attr(irq, "%u\n");
53 
broken_parity_status_show(struct device * dev,struct device_attribute * attr,char * buf)54 static 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 
broken_parity_status_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)62 static 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);
67 	unsigned long val;
68 
69 	if (kstrtoul(buf, 0, &val) < 0)
70 		return -EINVAL;
71 
72 	pdev->broken_parity_status = !!val;
73 
74 	return count;
75 }
76 static DEVICE_ATTR_RW(broken_parity_status);
77 
pci_dev_show_local_cpu(struct device * dev,bool list,struct device_attribute * attr,char * buf)78 static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
79 				      struct device_attribute *attr, char *buf)
80 {
81 	const struct cpumask *mask;
82 
83 #ifdef CONFIG_NUMA
84 	mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
85 					  cpumask_of_node(dev_to_node(dev));
86 #else
87 	mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
88 #endif
89 	return cpumap_print_to_pagebuf(list, buf, mask);
90 }
91 
local_cpus_show(struct device * dev,struct device_attribute * attr,char * buf)92 static ssize_t local_cpus_show(struct device *dev,
93 			       struct device_attribute *attr, char *buf)
94 {
95 	return pci_dev_show_local_cpu(dev, false, attr, buf);
96 }
97 static DEVICE_ATTR_RO(local_cpus);
98 
local_cpulist_show(struct device * dev,struct device_attribute * attr,char * buf)99 static ssize_t local_cpulist_show(struct device *dev,
100 				  struct device_attribute *attr, char *buf)
101 {
102 	return pci_dev_show_local_cpu(dev, true, attr, buf);
103 }
104 static DEVICE_ATTR_RO(local_cpulist);
105 
106 /*
107  * PCI Bus Class Devices
108  */
cpuaffinity_show(struct device * dev,struct device_attribute * attr,char * buf)109 static ssize_t cpuaffinity_show(struct device *dev,
110 				struct device_attribute *attr, char *buf)
111 {
112 	const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
113 
114 	return cpumap_print_to_pagebuf(false, buf, cpumask);
115 }
116 static DEVICE_ATTR_RO(cpuaffinity);
117 
cpulistaffinity_show(struct device * dev,struct device_attribute * attr,char * buf)118 static ssize_t cpulistaffinity_show(struct device *dev,
119 				    struct device_attribute *attr, char *buf)
120 {
121 	const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
122 
123 	return cpumap_print_to_pagebuf(true, buf, cpumask);
124 }
125 static DEVICE_ATTR_RO(cpulistaffinity);
126 
127 /* show resources */
resource_show(struct device * dev,struct device_attribute * attr,char * buf)128 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
129 			     char *buf)
130 {
131 	struct pci_dev *pci_dev = to_pci_dev(dev);
132 	char *str = buf;
133 	int i;
134 	int max;
135 	resource_size_t start, end;
136 
137 	if (pci_dev->subordinate)
138 		max = DEVICE_COUNT_RESOURCE;
139 	else
140 		max = PCI_BRIDGE_RESOURCES;
141 
142 	for (i = 0; i < max; i++) {
143 		struct resource *res =  &pci_dev->resource[i];
144 		pci_resource_to_user(pci_dev, i, res, &start, &end);
145 		str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
146 			       (unsigned long long)start,
147 			       (unsigned long long)end,
148 			       (unsigned long long)res->flags);
149 	}
150 	return (str - buf);
151 }
152 static DEVICE_ATTR_RO(resource);
153 
max_link_speed_show(struct device * dev,struct device_attribute * attr,char * buf)154 static ssize_t max_link_speed_show(struct device *dev,
155 				   struct device_attribute *attr, char *buf)
156 {
157 	struct pci_dev *pdev = to_pci_dev(dev);
158 
159 	return sprintf(buf, "%s\n", PCIE_SPEED2STR(pcie_get_speed_cap(pdev)));
160 }
161 static DEVICE_ATTR_RO(max_link_speed);
162 
max_link_width_show(struct device * dev,struct device_attribute * attr,char * buf)163 static ssize_t max_link_width_show(struct device *dev,
164 				   struct device_attribute *attr, char *buf)
165 {
166 	struct pci_dev *pdev = to_pci_dev(dev);
167 
168 	return sprintf(buf, "%u\n", pcie_get_width_cap(pdev));
169 }
170 static DEVICE_ATTR_RO(max_link_width);
171 
current_link_speed_show(struct device * dev,struct device_attribute * attr,char * buf)172 static ssize_t current_link_speed_show(struct device *dev,
173 				       struct device_attribute *attr, char *buf)
174 {
175 	struct pci_dev *pci_dev = to_pci_dev(dev);
176 	u16 linkstat;
177 	int err;
178 	const char *speed;
179 
180 	err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
181 	if (err)
182 		return -EINVAL;
183 
184 	switch (linkstat & PCI_EXP_LNKSTA_CLS) {
185 	case PCI_EXP_LNKSTA_CLS_32_0GB:
186 		speed = "32 GT/s";
187 		break;
188 	case PCI_EXP_LNKSTA_CLS_16_0GB:
189 		speed = "16 GT/s";
190 		break;
191 	case PCI_EXP_LNKSTA_CLS_8_0GB:
192 		speed = "8 GT/s";
193 		break;
194 	case PCI_EXP_LNKSTA_CLS_5_0GB:
195 		speed = "5 GT/s";
196 		break;
197 	case PCI_EXP_LNKSTA_CLS_2_5GB:
198 		speed = "2.5 GT/s";
199 		break;
200 	default:
201 		speed = "Unknown speed";
202 	}
203 
204 	return sprintf(buf, "%s\n", speed);
205 }
206 static DEVICE_ATTR_RO(current_link_speed);
207 
current_link_width_show(struct device * dev,struct device_attribute * attr,char * buf)208 static ssize_t current_link_width_show(struct device *dev,
209 				       struct device_attribute *attr, char *buf)
210 {
211 	struct pci_dev *pci_dev = to_pci_dev(dev);
212 	u16 linkstat;
213 	int err;
214 
215 	err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
216 	if (err)
217 		return -EINVAL;
218 
219 	return sprintf(buf, "%u\n",
220 		(linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
221 }
222 static DEVICE_ATTR_RO(current_link_width);
223 
secondary_bus_number_show(struct device * dev,struct device_attribute * attr,char * buf)224 static ssize_t secondary_bus_number_show(struct device *dev,
225 					 struct device_attribute *attr,
226 					 char *buf)
227 {
228 	struct pci_dev *pci_dev = to_pci_dev(dev);
229 	u8 sec_bus;
230 	int err;
231 
232 	err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
233 	if (err)
234 		return -EINVAL;
235 
236 	return sprintf(buf, "%u\n", sec_bus);
237 }
238 static DEVICE_ATTR_RO(secondary_bus_number);
239 
subordinate_bus_number_show(struct device * dev,struct device_attribute * attr,char * buf)240 static ssize_t subordinate_bus_number_show(struct device *dev,
241 					   struct device_attribute *attr,
242 					   char *buf)
243 {
244 	struct pci_dev *pci_dev = to_pci_dev(dev);
245 	u8 sub_bus;
246 	int err;
247 
248 	err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
249 	if (err)
250 		return -EINVAL;
251 
252 	return sprintf(buf, "%u\n", sub_bus);
253 }
254 static DEVICE_ATTR_RO(subordinate_bus_number);
255 
ari_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)256 static ssize_t ari_enabled_show(struct device *dev,
257 				struct device_attribute *attr,
258 				char *buf)
259 {
260 	struct pci_dev *pci_dev = to_pci_dev(dev);
261 
262 	return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
263 }
264 static DEVICE_ATTR_RO(ari_enabled);
265 
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)266 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
267 			     char *buf)
268 {
269 	struct pci_dev *pci_dev = to_pci_dev(dev);
270 
271 	return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
272 		       pci_dev->vendor, pci_dev->device,
273 		       pci_dev->subsystem_vendor, pci_dev->subsystem_device,
274 		       (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
275 		       (u8)(pci_dev->class));
276 }
277 static DEVICE_ATTR_RO(modalias);
278 
enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)279 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
280 			     const char *buf, size_t count)
281 {
282 	struct pci_dev *pdev = to_pci_dev(dev);
283 	unsigned long val;
284 	ssize_t result = kstrtoul(buf, 0, &val);
285 
286 	if (result < 0)
287 		return result;
288 
289 	/* this can crash the machine when done on the "wrong" device */
290 	if (!capable(CAP_SYS_ADMIN))
291 		return -EPERM;
292 
293 	device_lock(dev);
294 	if (dev->driver)
295 		result = -EBUSY;
296 	else if (val)
297 		result = pci_enable_device(pdev);
298 	else if (pci_is_enabled(pdev))
299 		pci_disable_device(pdev);
300 	else
301 		result = -EIO;
302 	device_unlock(dev);
303 
304 	return result < 0 ? result : count;
305 }
306 
enable_show(struct device * dev,struct device_attribute * attr,char * buf)307 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
308 			    char *buf)
309 {
310 	struct pci_dev *pdev;
311 
312 	pdev = to_pci_dev(dev);
313 	return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt));
314 }
315 static DEVICE_ATTR_RW(enable);
316 
317 #ifdef CONFIG_NUMA
numa_node_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)318 static ssize_t numa_node_store(struct device *dev,
319 			       struct device_attribute *attr, const char *buf,
320 			       size_t count)
321 {
322 	struct pci_dev *pdev = to_pci_dev(dev);
323 	int node, ret;
324 
325 	if (!capable(CAP_SYS_ADMIN))
326 		return -EPERM;
327 
328 	ret = kstrtoint(buf, 0, &node);
329 	if (ret)
330 		return ret;
331 
332 	if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
333 		return -EINVAL;
334 
335 	if (node != NUMA_NO_NODE && !node_online(node))
336 		return -EINVAL;
337 
338 	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
339 	pci_alert(pdev, FW_BUG "Overriding NUMA node to %d.  Contact your vendor for updates.",
340 		  node);
341 
342 	dev->numa_node = node;
343 	return count;
344 }
345 
numa_node_show(struct device * dev,struct device_attribute * attr,char * buf)346 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
347 			      char *buf)
348 {
349 	return sprintf(buf, "%d\n", dev->numa_node);
350 }
351 static DEVICE_ATTR_RW(numa_node);
352 #endif
353 
dma_mask_bits_show(struct device * dev,struct device_attribute * attr,char * buf)354 static ssize_t dma_mask_bits_show(struct device *dev,
355 				  struct device_attribute *attr, char *buf)
356 {
357 	struct pci_dev *pdev = to_pci_dev(dev);
358 
359 	return sprintf(buf, "%d\n", fls64(pdev->dma_mask));
360 }
361 static DEVICE_ATTR_RO(dma_mask_bits);
362 
consistent_dma_mask_bits_show(struct device * dev,struct device_attribute * attr,char * buf)363 static ssize_t consistent_dma_mask_bits_show(struct device *dev,
364 					     struct device_attribute *attr,
365 					     char *buf)
366 {
367 	return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask));
368 }
369 static DEVICE_ATTR_RO(consistent_dma_mask_bits);
370 
msi_bus_show(struct device * dev,struct device_attribute * attr,char * buf)371 static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
372 			    char *buf)
373 {
374 	struct pci_dev *pdev = to_pci_dev(dev);
375 	struct pci_bus *subordinate = pdev->subordinate;
376 
377 	return sprintf(buf, "%u\n", subordinate ?
378 		       !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
379 			   : !pdev->no_msi);
380 }
381 
msi_bus_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)382 static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
383 			     const char *buf, size_t count)
384 {
385 	struct pci_dev *pdev = to_pci_dev(dev);
386 	struct pci_bus *subordinate = pdev->subordinate;
387 	unsigned long val;
388 
389 	if (kstrtoul(buf, 0, &val) < 0)
390 		return -EINVAL;
391 
392 	if (!capable(CAP_SYS_ADMIN))
393 		return -EPERM;
394 
395 	/*
396 	 * "no_msi" and "bus_flags" only affect what happens when a driver
397 	 * requests MSI or MSI-X.  They don't affect any drivers that have
398 	 * already requested MSI or MSI-X.
399 	 */
400 	if (!subordinate) {
401 		pdev->no_msi = !val;
402 		pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
403 			 val ? "allowed" : "disallowed");
404 		return count;
405 	}
406 
407 	if (val)
408 		subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
409 	else
410 		subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
411 
412 	dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
413 		 val ? "allowed" : "disallowed");
414 	return count;
415 }
416 static DEVICE_ATTR_RW(msi_bus);
417 
bus_rescan_store(struct bus_type * bus,const char * buf,size_t count)418 static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
419 				size_t count)
420 {
421 	unsigned long val;
422 	struct pci_bus *b = NULL;
423 
424 	if (kstrtoul(buf, 0, &val) < 0)
425 		return -EINVAL;
426 
427 	if (val) {
428 		pci_lock_rescan_remove();
429 		while ((b = pci_find_next_bus(b)) != NULL)
430 			pci_rescan_bus(b);
431 		pci_unlock_rescan_remove();
432 	}
433 	return count;
434 }
435 static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
436 
437 static struct attribute *pci_bus_attrs[] = {
438 	&bus_attr_rescan.attr,
439 	NULL,
440 };
441 
442 static const struct attribute_group pci_bus_group = {
443 	.attrs = pci_bus_attrs,
444 };
445 
446 const struct attribute_group *pci_bus_groups[] = {
447 	&pci_bus_group,
448 	NULL,
449 };
450 
dev_rescan_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)451 static ssize_t dev_rescan_store(struct device *dev,
452 				struct device_attribute *attr, const char *buf,
453 				size_t count)
454 {
455 	unsigned long val;
456 	struct pci_dev *pdev = to_pci_dev(dev);
457 
458 	if (kstrtoul(buf, 0, &val) < 0)
459 		return -EINVAL;
460 
461 	if (val) {
462 		pci_lock_rescan_remove();
463 		pci_rescan_bus(pdev->bus);
464 		pci_unlock_rescan_remove();
465 	}
466 	return count;
467 }
468 static struct device_attribute dev_rescan_attr = __ATTR(rescan,
469 							(S_IWUSR|S_IWGRP),
470 							NULL, dev_rescan_store);
471 
remove_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)472 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
473 			    const char *buf, size_t count)
474 {
475 	unsigned long val;
476 
477 	if (kstrtoul(buf, 0, &val) < 0)
478 		return -EINVAL;
479 
480 	if (val && device_remove_file_self(dev, attr))
481 		pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
482 	return count;
483 }
484 static struct device_attribute dev_remove_attr = __ATTR_IGNORE_LOCKDEP(remove,
485 							(S_IWUSR|S_IWGRP),
486 							NULL, remove_store);
487 
dev_bus_rescan_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)488 static ssize_t dev_bus_rescan_store(struct device *dev,
489 				    struct device_attribute *attr,
490 				    const char *buf, size_t count)
491 {
492 	unsigned long val;
493 	struct pci_bus *bus = to_pci_bus(dev);
494 
495 	if (kstrtoul(buf, 0, &val) < 0)
496 		return -EINVAL;
497 
498 	if (val) {
499 		pci_lock_rescan_remove();
500 		if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
501 			pci_rescan_bus_bridge_resize(bus->self);
502 		else
503 			pci_rescan_bus(bus);
504 		pci_unlock_rescan_remove();
505 	}
506 	return count;
507 }
508 static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
509 
510 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
d3cold_allowed_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)511 static ssize_t d3cold_allowed_store(struct device *dev,
512 				    struct device_attribute *attr,
513 				    const char *buf, size_t count)
514 {
515 	struct pci_dev *pdev = to_pci_dev(dev);
516 	unsigned long val;
517 
518 	if (kstrtoul(buf, 0, &val) < 0)
519 		return -EINVAL;
520 
521 	pdev->d3cold_allowed = !!val;
522 	if (pdev->d3cold_allowed)
523 		pci_d3cold_enable(pdev);
524 	else
525 		pci_d3cold_disable(pdev);
526 
527 	pm_runtime_resume(dev);
528 
529 	return count;
530 }
531 
d3cold_allowed_show(struct device * dev,struct device_attribute * attr,char * buf)532 static ssize_t d3cold_allowed_show(struct device *dev,
533 				   struct device_attribute *attr, char *buf)
534 {
535 	struct pci_dev *pdev = to_pci_dev(dev);
536 	return sprintf(buf, "%u\n", pdev->d3cold_allowed);
537 }
538 static DEVICE_ATTR_RW(d3cold_allowed);
539 #endif
540 
541 #ifdef CONFIG_OF
devspec_show(struct device * dev,struct device_attribute * attr,char * buf)542 static ssize_t devspec_show(struct device *dev,
543 			    struct device_attribute *attr, char *buf)
544 {
545 	struct pci_dev *pdev = to_pci_dev(dev);
546 	struct device_node *np = pci_device_to_OF_node(pdev);
547 
548 	if (np == NULL)
549 		return 0;
550 	return sprintf(buf, "%pOF", np);
551 }
552 static DEVICE_ATTR_RO(devspec);
553 #endif
554 
555 #ifdef CONFIG_PCI_IOV
sriov_totalvfs_show(struct device * dev,struct device_attribute * attr,char * buf)556 static ssize_t sriov_totalvfs_show(struct device *dev,
557 				   struct device_attribute *attr,
558 				   char *buf)
559 {
560 	struct pci_dev *pdev = to_pci_dev(dev);
561 
562 	return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
563 }
564 
565 
sriov_numvfs_show(struct device * dev,struct device_attribute * attr,char * buf)566 static ssize_t sriov_numvfs_show(struct device *dev,
567 				 struct device_attribute *attr,
568 				 char *buf)
569 {
570 	struct pci_dev *pdev = to_pci_dev(dev);
571 
572 	return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
573 }
574 
575 /*
576  * num_vfs > 0; number of VFs to enable
577  * num_vfs = 0; disable all VFs
578  *
579  * Note: SRIOV spec doesn't allow partial VF
580  *       disable, so it's all or none.
581  */
sriov_numvfs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)582 static ssize_t sriov_numvfs_store(struct device *dev,
583 				  struct device_attribute *attr,
584 				  const char *buf, size_t count)
585 {
586 	struct pci_dev *pdev = to_pci_dev(dev);
587 	int ret;
588 	u16 num_vfs;
589 
590 	ret = kstrtou16(buf, 0, &num_vfs);
591 	if (ret < 0)
592 		return ret;
593 
594 	if (num_vfs > pci_sriov_get_totalvfs(pdev))
595 		return -ERANGE;
596 
597 	device_lock(&pdev->dev);
598 
599 	if (num_vfs == pdev->sriov->num_VFs)
600 		goto exit;
601 
602 	/* is PF driver loaded w/callback */
603 	if (!pdev->driver || !pdev->driver->sriov_configure) {
604 		pci_info(pdev, "Driver doesn't support SRIOV configuration via sysfs\n");
605 		ret = -ENOENT;
606 		goto exit;
607 	}
608 
609 	if (num_vfs == 0) {
610 		/* disable VFs */
611 		ret = pdev->driver->sriov_configure(pdev, 0);
612 		goto exit;
613 	}
614 
615 	/* enable VFs */
616 	if (pdev->sriov->num_VFs) {
617 		pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
618 			 pdev->sriov->num_VFs, num_vfs);
619 		ret = -EBUSY;
620 		goto exit;
621 	}
622 
623 	ret = pdev->driver->sriov_configure(pdev, num_vfs);
624 	if (ret < 0)
625 		goto exit;
626 
627 	if (ret != num_vfs)
628 		pci_warn(pdev, "%d VFs requested; only %d enabled\n",
629 			 num_vfs, ret);
630 
631 exit:
632 	device_unlock(&pdev->dev);
633 
634 	if (ret < 0)
635 		return ret;
636 
637 	return count;
638 }
639 
sriov_offset_show(struct device * dev,struct device_attribute * attr,char * buf)640 static ssize_t sriov_offset_show(struct device *dev,
641 				 struct device_attribute *attr,
642 				 char *buf)
643 {
644 	struct pci_dev *pdev = to_pci_dev(dev);
645 
646 	return sprintf(buf, "%u\n", pdev->sriov->offset);
647 }
648 
sriov_stride_show(struct device * dev,struct device_attribute * attr,char * buf)649 static ssize_t sriov_stride_show(struct device *dev,
650 				 struct device_attribute *attr,
651 				 char *buf)
652 {
653 	struct pci_dev *pdev = to_pci_dev(dev);
654 
655 	return sprintf(buf, "%u\n", pdev->sriov->stride);
656 }
657 
sriov_vf_device_show(struct device * dev,struct device_attribute * attr,char * buf)658 static ssize_t sriov_vf_device_show(struct device *dev,
659 				    struct device_attribute *attr,
660 				    char *buf)
661 {
662 	struct pci_dev *pdev = to_pci_dev(dev);
663 
664 	return sprintf(buf, "%x\n", pdev->sriov->vf_device);
665 }
666 
sriov_drivers_autoprobe_show(struct device * dev,struct device_attribute * attr,char * buf)667 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
668 					    struct device_attribute *attr,
669 					    char *buf)
670 {
671 	struct pci_dev *pdev = to_pci_dev(dev);
672 
673 	return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
674 }
675 
sriov_drivers_autoprobe_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)676 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
677 					     struct device_attribute *attr,
678 					     const char *buf, size_t count)
679 {
680 	struct pci_dev *pdev = to_pci_dev(dev);
681 	bool drivers_autoprobe;
682 
683 	if (kstrtobool(buf, &drivers_autoprobe) < 0)
684 		return -EINVAL;
685 
686 	pdev->sriov->drivers_autoprobe = drivers_autoprobe;
687 
688 	return count;
689 }
690 
691 static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
692 static struct device_attribute sriov_numvfs_attr =
693 		__ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
694 		       sriov_numvfs_show, sriov_numvfs_store);
695 static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
696 static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
697 static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
698 static struct device_attribute sriov_drivers_autoprobe_attr =
699 		__ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
700 		       sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
701 #endif /* CONFIG_PCI_IOV */
702 
driver_override_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)703 static ssize_t driver_override_store(struct device *dev,
704 				     struct device_attribute *attr,
705 				     const char *buf, size_t count)
706 {
707 	struct pci_dev *pdev = to_pci_dev(dev);
708 	char *driver_override, *old, *cp;
709 
710 	/* We need to keep extra room for a newline */
711 	if (count >= (PAGE_SIZE - 1))
712 		return -EINVAL;
713 
714 	driver_override = kstrndup(buf, count, GFP_KERNEL);
715 	if (!driver_override)
716 		return -ENOMEM;
717 
718 	cp = strchr(driver_override, '\n');
719 	if (cp)
720 		*cp = '\0';
721 
722 	device_lock(dev);
723 	old = pdev->driver_override;
724 	if (strlen(driver_override)) {
725 		pdev->driver_override = driver_override;
726 	} else {
727 		kfree(driver_override);
728 		pdev->driver_override = NULL;
729 	}
730 	device_unlock(dev);
731 
732 	kfree(old);
733 
734 	return count;
735 }
736 
driver_override_show(struct device * dev,struct device_attribute * attr,char * buf)737 static ssize_t driver_override_show(struct device *dev,
738 				    struct device_attribute *attr, char *buf)
739 {
740 	struct pci_dev *pdev = to_pci_dev(dev);
741 	ssize_t len;
742 
743 	device_lock(dev);
744 	len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
745 	device_unlock(dev);
746 	return len;
747 }
748 static DEVICE_ATTR_RW(driver_override);
749 
750 static struct attribute *pci_dev_attrs[] = {
751 	&dev_attr_resource.attr,
752 	&dev_attr_vendor.attr,
753 	&dev_attr_device.attr,
754 	&dev_attr_subsystem_vendor.attr,
755 	&dev_attr_subsystem_device.attr,
756 	&dev_attr_revision.attr,
757 	&dev_attr_class.attr,
758 	&dev_attr_irq.attr,
759 	&dev_attr_local_cpus.attr,
760 	&dev_attr_local_cpulist.attr,
761 	&dev_attr_modalias.attr,
762 #ifdef CONFIG_NUMA
763 	&dev_attr_numa_node.attr,
764 #endif
765 	&dev_attr_dma_mask_bits.attr,
766 	&dev_attr_consistent_dma_mask_bits.attr,
767 	&dev_attr_enable.attr,
768 	&dev_attr_broken_parity_status.attr,
769 	&dev_attr_msi_bus.attr,
770 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
771 	&dev_attr_d3cold_allowed.attr,
772 #endif
773 #ifdef CONFIG_OF
774 	&dev_attr_devspec.attr,
775 #endif
776 	&dev_attr_driver_override.attr,
777 	&dev_attr_ari_enabled.attr,
778 	NULL,
779 };
780 
781 static struct attribute *pci_bridge_attrs[] = {
782 	&dev_attr_subordinate_bus_number.attr,
783 	&dev_attr_secondary_bus_number.attr,
784 	NULL,
785 };
786 
787 static struct attribute *pcie_dev_attrs[] = {
788 	&dev_attr_current_link_speed.attr,
789 	&dev_attr_current_link_width.attr,
790 	&dev_attr_max_link_width.attr,
791 	&dev_attr_max_link_speed.attr,
792 	NULL,
793 };
794 
795 static struct attribute *pcibus_attrs[] = {
796 	&dev_attr_rescan.attr,
797 	&dev_attr_cpuaffinity.attr,
798 	&dev_attr_cpulistaffinity.attr,
799 	NULL,
800 };
801 
802 static const struct attribute_group pcibus_group = {
803 	.attrs = pcibus_attrs,
804 };
805 
806 const struct attribute_group *pcibus_groups[] = {
807 	&pcibus_group,
808 	NULL,
809 };
810 
boot_vga_show(struct device * dev,struct device_attribute * attr,char * buf)811 static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
812 			     char *buf)
813 {
814 	struct pci_dev *pdev = to_pci_dev(dev);
815 	struct pci_dev *vga_dev = vga_default_device();
816 
817 	if (vga_dev)
818 		return sprintf(buf, "%u\n", (pdev == vga_dev));
819 
820 	return sprintf(buf, "%u\n",
821 		!!(pdev->resource[PCI_ROM_RESOURCE].flags &
822 		   IORESOURCE_ROM_SHADOW));
823 }
824 static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
825 
pci_read_config(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)826 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
827 			       struct bin_attribute *bin_attr, char *buf,
828 			       loff_t off, size_t count)
829 {
830 	struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
831 	unsigned int size = 64;
832 	loff_t init_off = off;
833 	u8 *data = (u8 *) buf;
834 
835 	/* Several chips lock up trying to read undefined config space */
836 	if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
837 		size = dev->cfg_size;
838 	else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
839 		size = 128;
840 
841 	if (off > size)
842 		return 0;
843 	if (off + count > size) {
844 		size -= off;
845 		count = size;
846 	} else {
847 		size = count;
848 	}
849 
850 	pci_config_pm_runtime_get(dev);
851 
852 	if ((off & 1) && size) {
853 		u8 val;
854 		pci_user_read_config_byte(dev, off, &val);
855 		data[off - init_off] = val;
856 		off++;
857 		size--;
858 	}
859 
860 	if ((off & 3) && size > 2) {
861 		u16 val;
862 		pci_user_read_config_word(dev, off, &val);
863 		data[off - init_off] = val & 0xff;
864 		data[off - init_off + 1] = (val >> 8) & 0xff;
865 		off += 2;
866 		size -= 2;
867 	}
868 
869 	while (size > 3) {
870 		u32 val;
871 		pci_user_read_config_dword(dev, off, &val);
872 		data[off - init_off] = val & 0xff;
873 		data[off - init_off + 1] = (val >> 8) & 0xff;
874 		data[off - init_off + 2] = (val >> 16) & 0xff;
875 		data[off - init_off + 3] = (val >> 24) & 0xff;
876 		off += 4;
877 		size -= 4;
878 	}
879 
880 	if (size >= 2) {
881 		u16 val;
882 		pci_user_read_config_word(dev, off, &val);
883 		data[off - init_off] = val & 0xff;
884 		data[off - init_off + 1] = (val >> 8) & 0xff;
885 		off += 2;
886 		size -= 2;
887 	}
888 
889 	if (size > 0) {
890 		u8 val;
891 		pci_user_read_config_byte(dev, off, &val);
892 		data[off - init_off] = val;
893 		off++;
894 		--size;
895 	}
896 
897 	pci_config_pm_runtime_put(dev);
898 
899 	return count;
900 }
901 
pci_write_config(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)902 static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
903 				struct bin_attribute *bin_attr, char *buf,
904 				loff_t off, size_t count)
905 {
906 	struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
907 	unsigned int size = count;
908 	loff_t init_off = off;
909 	u8 *data = (u8 *) buf;
910 
911 	if (off > dev->cfg_size)
912 		return 0;
913 	if (off + count > dev->cfg_size) {
914 		size = dev->cfg_size - off;
915 		count = size;
916 	}
917 
918 	pci_config_pm_runtime_get(dev);
919 
920 	if ((off & 1) && size) {
921 		pci_user_write_config_byte(dev, off, data[off - init_off]);
922 		off++;
923 		size--;
924 	}
925 
926 	if ((off & 3) && size > 2) {
927 		u16 val = data[off - init_off];
928 		val |= (u16) data[off - init_off + 1] << 8;
929 		pci_user_write_config_word(dev, off, val);
930 		off += 2;
931 		size -= 2;
932 	}
933 
934 	while (size > 3) {
935 		u32 val = data[off - init_off];
936 		val |= (u32) data[off - init_off + 1] << 8;
937 		val |= (u32) data[off - init_off + 2] << 16;
938 		val |= (u32) data[off - init_off + 3] << 24;
939 		pci_user_write_config_dword(dev, off, val);
940 		off += 4;
941 		size -= 4;
942 	}
943 
944 	if (size >= 2) {
945 		u16 val = data[off - init_off];
946 		val |= (u16) data[off - init_off + 1] << 8;
947 		pci_user_write_config_word(dev, off, val);
948 		off += 2;
949 		size -= 2;
950 	}
951 
952 	if (size) {
953 		pci_user_write_config_byte(dev, off, data[off - init_off]);
954 		off++;
955 		--size;
956 	}
957 
958 	pci_config_pm_runtime_put(dev);
959 
960 	return count;
961 }
962 
963 #ifdef HAVE_PCI_LEGACY
964 /**
965  * pci_read_legacy_io - read byte(s) from legacy I/O port space
966  * @filp: open sysfs file
967  * @kobj: kobject corresponding to file to read from
968  * @bin_attr: struct bin_attribute for this file
969  * @buf: buffer to store results
970  * @off: offset into legacy I/O port space
971  * @count: number of bytes to read
972  *
973  * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
974  * callback routine (pci_legacy_read).
975  */
pci_read_legacy_io(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)976 static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
977 				  struct bin_attribute *bin_attr, char *buf,
978 				  loff_t off, size_t count)
979 {
980 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
981 
982 	/* Only support 1, 2 or 4 byte accesses */
983 	if (count != 1 && count != 2 && count != 4)
984 		return -EINVAL;
985 
986 	return pci_legacy_read(bus, off, (u32 *)buf, count);
987 }
988 
989 /**
990  * pci_write_legacy_io - write byte(s) to legacy I/O port space
991  * @filp: open sysfs file
992  * @kobj: kobject corresponding to file to read from
993  * @bin_attr: struct bin_attribute for this file
994  * @buf: buffer containing value to be written
995  * @off: offset into legacy I/O port space
996  * @count: number of bytes to write
997  *
998  * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
999  * callback routine (pci_legacy_write).
1000  */
pci_write_legacy_io(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)1001 static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
1002 				   struct bin_attribute *bin_attr, char *buf,
1003 				   loff_t off, size_t count)
1004 {
1005 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
1006 
1007 	/* Only support 1, 2 or 4 byte accesses */
1008 	if (count != 1 && count != 2 && count != 4)
1009 		return -EINVAL;
1010 
1011 	return pci_legacy_write(bus, off, *(u32 *)buf, count);
1012 }
1013 
1014 /**
1015  * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
1016  * @filp: open sysfs file
1017  * @kobj: kobject corresponding to device to be mapped
1018  * @attr: struct bin_attribute for this file
1019  * @vma: struct vm_area_struct passed to mmap
1020  *
1021  * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
1022  * legacy memory space (first meg of bus space) into application virtual
1023  * memory space.
1024  */
pci_mmap_legacy_mem(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,struct vm_area_struct * vma)1025 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
1026 			       struct bin_attribute *attr,
1027 			       struct vm_area_struct *vma)
1028 {
1029 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
1030 
1031 	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
1032 }
1033 
1034 /**
1035  * pci_mmap_legacy_io - map legacy PCI IO into user memory space
1036  * @filp: open sysfs file
1037  * @kobj: kobject corresponding to device to be mapped
1038  * @attr: struct bin_attribute for this file
1039  * @vma: struct vm_area_struct passed to mmap
1040  *
1041  * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
1042  * legacy IO space (first meg of bus space) into application virtual
1043  * memory space. Returns -ENOSYS if the operation isn't supported
1044  */
pci_mmap_legacy_io(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,struct vm_area_struct * vma)1045 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
1046 			      struct bin_attribute *attr,
1047 			      struct vm_area_struct *vma)
1048 {
1049 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
1050 
1051 	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
1052 }
1053 
1054 /**
1055  * pci_adjust_legacy_attr - adjustment of legacy file attributes
1056  * @b: bus to create files under
1057  * @mmap_type: I/O port or memory
1058  *
1059  * Stub implementation. Can be overridden by arch if necessary.
1060  */
pci_adjust_legacy_attr(struct pci_bus * b,enum pci_mmap_state mmap_type)1061 void __weak pci_adjust_legacy_attr(struct pci_bus *b,
1062 				   enum pci_mmap_state mmap_type)
1063 {
1064 }
1065 
1066 /**
1067  * pci_create_legacy_files - create legacy I/O port and memory files
1068  * @b: bus to create files under
1069  *
1070  * Some platforms allow access to legacy I/O port and ISA memory space on
1071  * a per-bus basis.  This routine creates the files and ties them into
1072  * their associated read, write and mmap files from pci-sysfs.c
1073  *
1074  * On error unwind, but don't propagate the error to the caller
1075  * as it is ok to set up the PCI bus without these files.
1076  */
pci_create_legacy_files(struct pci_bus * b)1077 void pci_create_legacy_files(struct pci_bus *b)
1078 {
1079 	int error;
1080 
1081 	b->legacy_io = kcalloc(2, sizeof(struct bin_attribute),
1082 			       GFP_ATOMIC);
1083 	if (!b->legacy_io)
1084 		goto kzalloc_err;
1085 
1086 	sysfs_bin_attr_init(b->legacy_io);
1087 	b->legacy_io->attr.name = "legacy_io";
1088 	b->legacy_io->size = 0xffff;
1089 	b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
1090 	b->legacy_io->read = pci_read_legacy_io;
1091 	b->legacy_io->write = pci_write_legacy_io;
1092 	b->legacy_io->mmap = pci_mmap_legacy_io;
1093 	pci_adjust_legacy_attr(b, pci_mmap_io);
1094 	error = device_create_bin_file(&b->dev, b->legacy_io);
1095 	if (error)
1096 		goto legacy_io_err;
1097 
1098 	/* Allocated above after the legacy_io struct */
1099 	b->legacy_mem = b->legacy_io + 1;
1100 	sysfs_bin_attr_init(b->legacy_mem);
1101 	b->legacy_mem->attr.name = "legacy_mem";
1102 	b->legacy_mem->size = 1024*1024;
1103 	b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
1104 	b->legacy_mem->mmap = pci_mmap_legacy_mem;
1105 	pci_adjust_legacy_attr(b, pci_mmap_mem);
1106 	error = device_create_bin_file(&b->dev, b->legacy_mem);
1107 	if (error)
1108 		goto legacy_mem_err;
1109 
1110 	return;
1111 
1112 legacy_mem_err:
1113 	device_remove_bin_file(&b->dev, b->legacy_io);
1114 legacy_io_err:
1115 	kfree(b->legacy_io);
1116 	b->legacy_io = NULL;
1117 kzalloc_err:
1118 	printk(KERN_WARNING "pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n");
1119 	return;
1120 }
1121 
pci_remove_legacy_files(struct pci_bus * b)1122 void pci_remove_legacy_files(struct pci_bus *b)
1123 {
1124 	if (b->legacy_io) {
1125 		device_remove_bin_file(&b->dev, b->legacy_io);
1126 		device_remove_bin_file(&b->dev, b->legacy_mem);
1127 		kfree(b->legacy_io); /* both are allocated here */
1128 	}
1129 }
1130 #endif /* HAVE_PCI_LEGACY */
1131 
1132 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
1133 
pci_mmap_fits(struct pci_dev * pdev,int resno,struct vm_area_struct * vma,enum pci_mmap_api mmap_api)1134 int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1135 		  enum pci_mmap_api mmap_api)
1136 {
1137 	unsigned long nr, start, size;
1138 	resource_size_t pci_start = 0, pci_end;
1139 
1140 	if (pci_resource_len(pdev, resno) == 0)
1141 		return 0;
1142 	nr = vma_pages(vma);
1143 	start = vma->vm_pgoff;
1144 	size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
1145 	if (mmap_api == PCI_MMAP_PROCFS) {
1146 		pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1147 				     &pci_start, &pci_end);
1148 		pci_start >>= PAGE_SHIFT;
1149 	}
1150 	if (start >= pci_start && start < pci_start + size &&
1151 			start + nr <= pci_start + size)
1152 		return 1;
1153 	return 0;
1154 }
1155 
1156 /**
1157  * pci_mmap_resource - map a PCI resource into user memory space
1158  * @kobj: kobject for mapping
1159  * @attr: struct bin_attribute for the file being mapped
1160  * @vma: struct vm_area_struct passed into the mmap
1161  * @write_combine: 1 for write_combine mapping
1162  *
1163  * Use the regular PCI mapping routines to map a PCI resource into userspace.
1164  */
pci_mmap_resource(struct kobject * kobj,struct bin_attribute * attr,struct vm_area_struct * vma,int write_combine)1165 static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
1166 			     struct vm_area_struct *vma, int write_combine)
1167 {
1168 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1169 	int bar = (unsigned long)attr->private;
1170 	enum pci_mmap_state mmap_type;
1171 	struct resource *res = &pdev->resource[bar];
1172 
1173 	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
1174 		return -EINVAL;
1175 
1176 	if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
1177 		return -EINVAL;
1178 
1179 	mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
1180 
1181 	return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
1182 }
1183 
pci_mmap_resource_uc(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,struct vm_area_struct * vma)1184 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
1185 				struct bin_attribute *attr,
1186 				struct vm_area_struct *vma)
1187 {
1188 	return pci_mmap_resource(kobj, attr, vma, 0);
1189 }
1190 
pci_mmap_resource_wc(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,struct vm_area_struct * vma)1191 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
1192 				struct bin_attribute *attr,
1193 				struct vm_area_struct *vma)
1194 {
1195 	return pci_mmap_resource(kobj, attr, vma, 1);
1196 }
1197 
pci_resource_io(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count,bool write)1198 static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
1199 			       struct bin_attribute *attr, char *buf,
1200 			       loff_t off, size_t count, bool write)
1201 {
1202 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1203 	int bar = (unsigned long)attr->private;
1204 	unsigned long port = off;
1205 
1206 	port += pci_resource_start(pdev, bar);
1207 
1208 	if (port > pci_resource_end(pdev, bar))
1209 		return 0;
1210 
1211 	if (port + count - 1 > pci_resource_end(pdev, bar))
1212 		return -EINVAL;
1213 
1214 	switch (count) {
1215 	case 1:
1216 		if (write)
1217 			outb(*(u8 *)buf, port);
1218 		else
1219 			*(u8 *)buf = inb(port);
1220 		return 1;
1221 	case 2:
1222 		if (write)
1223 			outw(*(u16 *)buf, port);
1224 		else
1225 			*(u16 *)buf = inw(port);
1226 		return 2;
1227 	case 4:
1228 		if (write)
1229 			outl(*(u32 *)buf, port);
1230 		else
1231 			*(u32 *)buf = inl(port);
1232 		return 4;
1233 	}
1234 	return -EINVAL;
1235 }
1236 
pci_read_resource_io(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)1237 static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
1238 				    struct bin_attribute *attr, char *buf,
1239 				    loff_t off, size_t count)
1240 {
1241 	return pci_resource_io(filp, kobj, attr, buf, off, count, false);
1242 }
1243 
pci_write_resource_io(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)1244 static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
1245 				     struct bin_attribute *attr, char *buf,
1246 				     loff_t off, size_t count)
1247 {
1248 	return pci_resource_io(filp, kobj, attr, buf, off, count, true);
1249 }
1250 
1251 /**
1252  * pci_remove_resource_files - cleanup resource files
1253  * @pdev: dev to cleanup
1254  *
1255  * If we created resource files for @pdev, remove them from sysfs and
1256  * free their resources.
1257  */
pci_remove_resource_files(struct pci_dev * pdev)1258 static void pci_remove_resource_files(struct pci_dev *pdev)
1259 {
1260 	int i;
1261 
1262 	for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1263 		struct bin_attribute *res_attr;
1264 
1265 		res_attr = pdev->res_attr[i];
1266 		if (res_attr) {
1267 			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1268 			kfree(res_attr);
1269 		}
1270 
1271 		res_attr = pdev->res_attr_wc[i];
1272 		if (res_attr) {
1273 			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
1274 			kfree(res_attr);
1275 		}
1276 	}
1277 }
1278 
pci_create_attr(struct pci_dev * pdev,int num,int write_combine)1279 static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
1280 {
1281 	/* allocate attribute structure, piggyback attribute name */
1282 	int name_len = write_combine ? 13 : 10;
1283 	struct bin_attribute *res_attr;
1284 	char *res_attr_name;
1285 	int retval;
1286 
1287 	res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
1288 	if (!res_attr)
1289 		return -ENOMEM;
1290 
1291 	res_attr_name = (char *)(res_attr + 1);
1292 
1293 	sysfs_bin_attr_init(res_attr);
1294 	if (write_combine) {
1295 		sprintf(res_attr_name, "resource%d_wc", num);
1296 		res_attr->mmap = pci_mmap_resource_wc;
1297 	} else {
1298 		sprintf(res_attr_name, "resource%d", num);
1299 		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
1300 			res_attr->read = pci_read_resource_io;
1301 			res_attr->write = pci_write_resource_io;
1302 			if (arch_can_pci_mmap_io())
1303 				res_attr->mmap = pci_mmap_resource_uc;
1304 		} else {
1305 			res_attr->mmap = pci_mmap_resource_uc;
1306 		}
1307 	}
1308 	res_attr->attr.name = res_attr_name;
1309 	res_attr->attr.mode = S_IRUSR | S_IWUSR;
1310 	res_attr->size = pci_resource_len(pdev, num);
1311 	res_attr->private = (void *)(unsigned long)num;
1312 	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
1313 	if (retval) {
1314 		kfree(res_attr);
1315 		return retval;
1316 	}
1317 
1318 	if (write_combine)
1319 		pdev->res_attr_wc[num] = res_attr;
1320 	else
1321 		pdev->res_attr[num] = res_attr;
1322 
1323 	return 0;
1324 }
1325 
1326 /**
1327  * pci_create_resource_files - create resource files in sysfs for @dev
1328  * @pdev: dev in question
1329  *
1330  * Walk the resources in @pdev creating files for each resource available.
1331  */
pci_create_resource_files(struct pci_dev * pdev)1332 static int pci_create_resource_files(struct pci_dev *pdev)
1333 {
1334 	int i;
1335 	int retval;
1336 
1337 	/* Expose the PCI resources from this device as files */
1338 	for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1339 
1340 		/* skip empty resources */
1341 		if (!pci_resource_len(pdev, i))
1342 			continue;
1343 
1344 		retval = pci_create_attr(pdev, i, 0);
1345 		/* for prefetchable resources, create a WC mappable file */
1346 		if (!retval && arch_can_pci_mmap_wc() &&
1347 		    pdev->resource[i].flags & IORESOURCE_PREFETCH)
1348 			retval = pci_create_attr(pdev, i, 1);
1349 		if (retval) {
1350 			pci_remove_resource_files(pdev);
1351 			return retval;
1352 		}
1353 	}
1354 	return 0;
1355 }
1356 #else /* !HAVE_PCI_MMAP */
pci_create_resource_files(struct pci_dev * dev)1357 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
pci_remove_resource_files(struct pci_dev * dev)1358 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1359 #endif /* HAVE_PCI_MMAP */
1360 
1361 /**
1362  * pci_write_rom - used to enable access to the PCI ROM display
1363  * @filp: sysfs file
1364  * @kobj: kernel object handle
1365  * @bin_attr: struct bin_attribute for this file
1366  * @buf: user input
1367  * @off: file offset
1368  * @count: number of byte in input
1369  *
1370  * writing anything except 0 enables it
1371  */
pci_write_rom(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)1372 static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
1373 			     struct bin_attribute *bin_attr, char *buf,
1374 			     loff_t off, size_t count)
1375 {
1376 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1377 
1378 	if ((off ==  0) && (*buf == '0') && (count == 2))
1379 		pdev->rom_attr_enabled = 0;
1380 	else
1381 		pdev->rom_attr_enabled = 1;
1382 
1383 	return count;
1384 }
1385 
1386 /**
1387  * pci_read_rom - read a PCI ROM
1388  * @filp: sysfs file
1389  * @kobj: kernel object handle
1390  * @bin_attr: struct bin_attribute for this file
1391  * @buf: where to put the data we read from the ROM
1392  * @off: file offset
1393  * @count: number of bytes to read
1394  *
1395  * Put @count bytes starting at @off into @buf from the ROM in the PCI
1396  * device corresponding to @kobj.
1397  */
pci_read_rom(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)1398 static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
1399 			    struct bin_attribute *bin_attr, char *buf,
1400 			    loff_t off, size_t count)
1401 {
1402 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
1403 	void __iomem *rom;
1404 	size_t size;
1405 
1406 	if (!pdev->rom_attr_enabled)
1407 		return -EINVAL;
1408 
1409 	rom = pci_map_rom(pdev, &size);	/* size starts out as PCI window size */
1410 	if (!rom || !size)
1411 		return -EIO;
1412 
1413 	if (off >= size)
1414 		count = 0;
1415 	else {
1416 		if (off + count > size)
1417 			count = size - off;
1418 
1419 		memcpy_fromio(buf, rom + off, count);
1420 	}
1421 	pci_unmap_rom(pdev, rom);
1422 
1423 	return count;
1424 }
1425 
1426 static const struct bin_attribute pci_config_attr = {
1427 	.attr =	{
1428 		.name = "config",
1429 		.mode = S_IRUGO | S_IWUSR,
1430 	},
1431 	.size = PCI_CFG_SPACE_SIZE,
1432 	.read = pci_read_config,
1433 	.write = pci_write_config,
1434 };
1435 
1436 static const struct bin_attribute pcie_config_attr = {
1437 	.attr =	{
1438 		.name = "config",
1439 		.mode = S_IRUGO | S_IWUSR,
1440 	},
1441 	.size = PCI_CFG_SPACE_EXP_SIZE,
1442 	.read = pci_read_config,
1443 	.write = pci_write_config,
1444 };
1445 
reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1446 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
1447 			   const char *buf, size_t count)
1448 {
1449 	struct pci_dev *pdev = to_pci_dev(dev);
1450 	unsigned long val;
1451 	ssize_t result = kstrtoul(buf, 0, &val);
1452 
1453 	if (result < 0)
1454 		return result;
1455 
1456 	if (val != 1)
1457 		return -EINVAL;
1458 
1459 	pm_runtime_get_sync(dev);
1460 	result = pci_reset_function(pdev);
1461 	pm_runtime_put(dev);
1462 	if (result < 0)
1463 		return result;
1464 
1465 	return count;
1466 }
1467 
1468 static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1469 
pci_create_capabilities_sysfs(struct pci_dev * dev)1470 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
1471 {
1472 	int retval;
1473 
1474 	pcie_vpd_create_sysfs_dev_files(dev);
1475 	pcie_aspm_create_sysfs_dev_files(dev);
1476 
1477 	if (dev->reset_fn) {
1478 		retval = device_create_file(&dev->dev, &reset_attr);
1479 		if (retval)
1480 			goto error;
1481 	}
1482 	return 0;
1483 
1484 error:
1485 	pcie_aspm_remove_sysfs_dev_files(dev);
1486 	pcie_vpd_remove_sysfs_dev_files(dev);
1487 	return retval;
1488 }
1489 
pci_create_sysfs_dev_files(struct pci_dev * pdev)1490 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
1491 {
1492 	int retval;
1493 	int rom_size;
1494 	struct bin_attribute *attr;
1495 
1496 	if (!sysfs_initialized)
1497 		return -EACCES;
1498 
1499 	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1500 		retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1501 	else
1502 		retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr);
1503 	if (retval)
1504 		goto err;
1505 
1506 	retval = pci_create_resource_files(pdev);
1507 	if (retval)
1508 		goto err_config_file;
1509 
1510 	/* If the device has a ROM, try to expose it in sysfs. */
1511 	rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
1512 	if (rom_size) {
1513 		attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
1514 		if (!attr) {
1515 			retval = -ENOMEM;
1516 			goto err_resource_files;
1517 		}
1518 		sysfs_bin_attr_init(attr);
1519 		attr->size = rom_size;
1520 		attr->attr.name = "rom";
1521 		attr->attr.mode = S_IRUSR | S_IWUSR;
1522 		attr->read = pci_read_rom;
1523 		attr->write = pci_write_rom;
1524 		retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
1525 		if (retval) {
1526 			kfree(attr);
1527 			goto err_resource_files;
1528 		}
1529 		pdev->rom_attr = attr;
1530 	}
1531 
1532 	/* add sysfs entries for various capabilities */
1533 	retval = pci_create_capabilities_sysfs(pdev);
1534 	if (retval)
1535 		goto err_rom_file;
1536 
1537 	pci_create_firmware_label_files(pdev);
1538 
1539 	return 0;
1540 
1541 err_rom_file:
1542 	if (pdev->rom_attr) {
1543 		sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1544 		kfree(pdev->rom_attr);
1545 		pdev->rom_attr = NULL;
1546 	}
1547 err_resource_files:
1548 	pci_remove_resource_files(pdev);
1549 err_config_file:
1550 	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1551 		sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1552 	else
1553 		sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1554 err:
1555 	return retval;
1556 }
1557 
pci_remove_capabilities_sysfs(struct pci_dev * dev)1558 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
1559 {
1560 	pcie_vpd_remove_sysfs_dev_files(dev);
1561 	pcie_aspm_remove_sysfs_dev_files(dev);
1562 	if (dev->reset_fn) {
1563 		device_remove_file(&dev->dev, &reset_attr);
1564 		dev->reset_fn = 0;
1565 	}
1566 }
1567 
1568 /**
1569  * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
1570  * @pdev: device whose entries we should free
1571  *
1572  * Cleanup when @pdev is removed from sysfs.
1573  */
pci_remove_sysfs_dev_files(struct pci_dev * pdev)1574 void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
1575 {
1576 	if (!sysfs_initialized)
1577 		return;
1578 
1579 	pci_remove_capabilities_sysfs(pdev);
1580 
1581 	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
1582 		sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr);
1583 	else
1584 		sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr);
1585 
1586 	pci_remove_resource_files(pdev);
1587 
1588 	if (pdev->rom_attr) {
1589 		sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
1590 		kfree(pdev->rom_attr);
1591 		pdev->rom_attr = NULL;
1592 	}
1593 
1594 	pci_remove_firmware_label_files(pdev);
1595 }
1596 
pci_sysfs_init(void)1597 static int __init pci_sysfs_init(void)
1598 {
1599 	struct pci_dev *pdev = NULL;
1600 	int retval;
1601 
1602 	sysfs_initialized = 1;
1603 	for_each_pci_dev(pdev) {
1604 		retval = pci_create_sysfs_dev_files(pdev);
1605 		if (retval) {
1606 			pci_dev_put(pdev);
1607 			return retval;
1608 		}
1609 	}
1610 
1611 	return 0;
1612 }
1613 late_initcall(pci_sysfs_init);
1614 
1615 static struct attribute *pci_dev_dev_attrs[] = {
1616 	&vga_attr.attr,
1617 	NULL,
1618 };
1619 
pci_dev_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)1620 static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1621 					 struct attribute *a, int n)
1622 {
1623 	struct device *dev = kobj_to_dev(kobj);
1624 	struct pci_dev *pdev = to_pci_dev(dev);
1625 
1626 	if (a == &vga_attr.attr)
1627 		if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1628 			return 0;
1629 
1630 	return a->mode;
1631 }
1632 
1633 static struct attribute *pci_dev_hp_attrs[] = {
1634 	&dev_remove_attr.attr,
1635 	&dev_rescan_attr.attr,
1636 	NULL,
1637 };
1638 
pci_dev_hp_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)1639 static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
1640 					    struct attribute *a, int n)
1641 {
1642 	struct device *dev = kobj_to_dev(kobj);
1643 	struct pci_dev *pdev = to_pci_dev(dev);
1644 
1645 	if (pdev->is_virtfn)
1646 		return 0;
1647 
1648 	return a->mode;
1649 }
1650 
pci_bridge_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)1651 static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
1652 					    struct attribute *a, int n)
1653 {
1654 	struct device *dev = kobj_to_dev(kobj);
1655 	struct pci_dev *pdev = to_pci_dev(dev);
1656 
1657 	if (pci_is_bridge(pdev))
1658 		return a->mode;
1659 
1660 	return 0;
1661 }
1662 
pcie_dev_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)1663 static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
1664 					  struct attribute *a, int n)
1665 {
1666 	struct device *dev = kobj_to_dev(kobj);
1667 	struct pci_dev *pdev = to_pci_dev(dev);
1668 
1669 	if (pci_is_pcie(pdev))
1670 		return a->mode;
1671 
1672 	return 0;
1673 }
1674 
1675 static const struct attribute_group pci_dev_group = {
1676 	.attrs = pci_dev_attrs,
1677 };
1678 
1679 const struct attribute_group *pci_dev_groups[] = {
1680 	&pci_dev_group,
1681 	NULL,
1682 };
1683 
1684 static const struct attribute_group pci_bridge_group = {
1685 	.attrs = pci_bridge_attrs,
1686 };
1687 
1688 const struct attribute_group *pci_bridge_groups[] = {
1689 	&pci_bridge_group,
1690 	NULL,
1691 };
1692 
1693 static const struct attribute_group pcie_dev_group = {
1694 	.attrs = pcie_dev_attrs,
1695 };
1696 
1697 const struct attribute_group *pcie_dev_groups[] = {
1698 	&pcie_dev_group,
1699 	NULL,
1700 };
1701 
1702 static const struct attribute_group pci_dev_hp_attr_group = {
1703 	.attrs = pci_dev_hp_attrs,
1704 	.is_visible = pci_dev_hp_attrs_are_visible,
1705 };
1706 
1707 #ifdef CONFIG_PCI_IOV
1708 static struct attribute *sriov_dev_attrs[] = {
1709 	&sriov_totalvfs_attr.attr,
1710 	&sriov_numvfs_attr.attr,
1711 	&sriov_offset_attr.attr,
1712 	&sriov_stride_attr.attr,
1713 	&sriov_vf_device_attr.attr,
1714 	&sriov_drivers_autoprobe_attr.attr,
1715 	NULL,
1716 };
1717 
sriov_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)1718 static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1719 				       struct attribute *a, int n)
1720 {
1721 	struct device *dev = kobj_to_dev(kobj);
1722 
1723 	if (!dev_is_pf(dev))
1724 		return 0;
1725 
1726 	return a->mode;
1727 }
1728 
1729 static const struct attribute_group sriov_dev_attr_group = {
1730 	.attrs = sriov_dev_attrs,
1731 	.is_visible = sriov_attrs_are_visible,
1732 };
1733 #endif /* CONFIG_PCI_IOV */
1734 
1735 static const struct attribute_group pci_dev_attr_group = {
1736 	.attrs = pci_dev_dev_attrs,
1737 	.is_visible = pci_dev_attrs_are_visible,
1738 };
1739 
1740 static const struct attribute_group pci_bridge_attr_group = {
1741 	.attrs = pci_bridge_attrs,
1742 	.is_visible = pci_bridge_attrs_are_visible,
1743 };
1744 
1745 static const struct attribute_group pcie_dev_attr_group = {
1746 	.attrs = pcie_dev_attrs,
1747 	.is_visible = pcie_dev_attrs_are_visible,
1748 };
1749 
1750 static const struct attribute_group *pci_dev_attr_groups[] = {
1751 	&pci_dev_attr_group,
1752 	&pci_dev_hp_attr_group,
1753 #ifdef CONFIG_PCI_IOV
1754 	&sriov_dev_attr_group,
1755 #endif
1756 	&pci_bridge_attr_group,
1757 	&pcie_dev_attr_group,
1758 #ifdef CONFIG_PCIEAER
1759 	&aer_stats_attr_group,
1760 #endif
1761 	NULL,
1762 };
1763 
1764 const struct device_type pci_dev_type = {
1765 	.groups = pci_dev_attr_groups,
1766 };
1767