1736759efSBjorn Helgaas // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Compaq Hot Plug Controller Driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 1995,2001 Compaq Computer Corporation
61da177e4SLinus Torvalds  * Copyright (c) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
71da177e4SLinus Torvalds  * Copyright (c) 2001 IBM Corp.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * All rights reserved.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Send feedback to <greg@kroah.com>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/types.h>
181da177e4SLinus Torvalds #include <linux/pci.h>
191da177e4SLinus Torvalds #include "shpchp.h"
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /* A few routines that create sysfs entries for the hot plug controller */
231da177e4SLinus Torvalds 
show_ctrl(struct device * dev,struct device_attribute * attr,char * buf)24e404e274SYani Ioannou static ssize_t show_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
251da177e4SLinus Torvalds {
26dbd7a788Srajesh.shah@intel.com 	struct pci_dev *pdev;
27dbd7a788Srajesh.shah@intel.com 	struct resource *res;
28dbd7a788Srajesh.shah@intel.com 	struct pci_bus *bus;
29f8cf6e51SKrzysztof Wilczyński 	size_t len = 0;
30*02992064SAndy Shevchenko 	int busnr;
311da177e4SLinus Torvalds 
32f3d2f165SGeliang Tang 	pdev = to_pci_dev(dev);
33dbd7a788Srajesh.shah@intel.com 	bus = pdev->subordinate;
341da177e4SLinus Torvalds 
35f8cf6e51SKrzysztof Wilczyński 	len += sysfs_emit_at(buf, len, "Free resources: memory\n");
36*02992064SAndy Shevchenko 	pci_bus_for_each_resource(bus, res) {
37dbd7a788Srajesh.shah@intel.com 		if (res && (res->flags & IORESOURCE_MEM) &&
38dbd7a788Srajesh.shah@intel.com 				!(res->flags & IORESOURCE_PREFETCH)) {
39f8cf6e51SKrzysztof Wilczyński 			len += sysfs_emit_at(buf, len,
40f8cf6e51SKrzysztof Wilczyński 					     "start = %8.8llx, length = %8.8llx\n",
411396a8c3SGreg Kroah-Hartman 					     (unsigned long long)res->start,
4228f65c11SJoe Perches 					     (unsigned long long)resource_size(res));
43dbd7a788Srajesh.shah@intel.com 		}
441da177e4SLinus Torvalds 	}
45f8cf6e51SKrzysztof Wilczyński 	len += sysfs_emit_at(buf, len, "Free resources: prefetchable memory\n");
46*02992064SAndy Shevchenko 	pci_bus_for_each_resource(bus, res) {
47dbd7a788Srajesh.shah@intel.com 		if (res && (res->flags & IORESOURCE_MEM) &&
48dbd7a788Srajesh.shah@intel.com 			       (res->flags & IORESOURCE_PREFETCH)) {
49f8cf6e51SKrzysztof Wilczyński 			len += sysfs_emit_at(buf, len,
50f8cf6e51SKrzysztof Wilczyński 					     "start = %8.8llx, length = %8.8llx\n",
511396a8c3SGreg Kroah-Hartman 					     (unsigned long long)res->start,
5228f65c11SJoe Perches 					     (unsigned long long)resource_size(res));
53dbd7a788Srajesh.shah@intel.com 		}
541da177e4SLinus Torvalds 	}
55f8cf6e51SKrzysztof Wilczyński 	len += sysfs_emit_at(buf, len, "Free resources: IO\n");
56*02992064SAndy Shevchenko 	pci_bus_for_each_resource(bus, res) {
57dbd7a788Srajesh.shah@intel.com 		if (res && (res->flags & IORESOURCE_IO)) {
58f8cf6e51SKrzysztof Wilczyński 			len += sysfs_emit_at(buf, len,
59f8cf6e51SKrzysztof Wilczyński 					     "start = %8.8llx, length = %8.8llx\n",
601396a8c3SGreg Kroah-Hartman 					     (unsigned long long)res->start,
6128f65c11SJoe Perches 					     (unsigned long long)resource_size(res));
62dbd7a788Srajesh.shah@intel.com 		}
631da177e4SLinus Torvalds 	}
64f8cf6e51SKrzysztof Wilczyński 	len += sysfs_emit_at(buf, len, "Free resources: bus numbers\n");
65b918c62eSYinghai Lu 	for (busnr = bus->busn_res.start; busnr <= bus->busn_res.end; busnr++) {
66dbd7a788Srajesh.shah@intel.com 		if (!pci_find_bus(pci_domain_nr(bus), busnr))
67dbd7a788Srajesh.shah@intel.com 			break;
681da177e4SLinus Torvalds 	}
69b918c62eSYinghai Lu 	if (busnr < bus->busn_res.end)
70f8cf6e51SKrzysztof Wilczyński 		len += sysfs_emit_at(buf, len,
71f8cf6e51SKrzysztof Wilczyński 				     "start = %8.8x, length = %8.8x\n",
72b918c62eSYinghai Lu 				     busnr, (int)(bus->busn_res.end - busnr));
731da177e4SLinus Torvalds 
74f8cf6e51SKrzysztof Wilczyński 	return len;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds static DEVICE_ATTR(ctrl, S_IRUGO, show_ctrl, NULL);
771da177e4SLinus Torvalds 
shpchp_create_ctrl_files(struct controller * ctrl)78d67aed63SBjorn Helgaas int shpchp_create_ctrl_files(struct controller *ctrl)
791da177e4SLinus Torvalds {
80e1b95dc6SGreg Kroah-Hartman 	return device_create_file(&ctrl->pci_dev->dev, &dev_attr_ctrl);
811da177e4SLinus Torvalds }
82c2608a11Srajesh.shah@intel.com 
shpchp_remove_ctrl_files(struct controller * ctrl)83c2608a11Srajesh.shah@intel.com void shpchp_remove_ctrl_files(struct controller *ctrl)
84c2608a11Srajesh.shah@intel.com {
85c2608a11Srajesh.shah@intel.com 	device_remove_file(&ctrl->pci_dev->dev, &dev_attr_ctrl);
86c2608a11Srajesh.shah@intel.com }
87