xref: /openbmc/linux/drivers/pci/hotplug/cpqphp_sysfs.c (revision 552c69b36ebd966186573b9c7a286b390935cce1)
1*736759efSBjorn 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>
175a0e3ad6STejun Heo #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/proc_fs.h>
201da177e4SLinus Torvalds #include <linux/workqueue.h>
211da177e4SLinus Torvalds #include <linux/pci.h>
227a54f25cSGreg Kroah-Hartman #include <linux/pci_hotplug.h>
23613655faSArnd Bergmann #include <linux/mutex.h>
249f3f4681SGreg Kroah-Hartman #include <linux/debugfs.h>
251da177e4SLinus Torvalds #include "cpqphp.h"
261da177e4SLinus Torvalds 
27613655faSArnd Bergmann static DEFINE_MUTEX(cpqphp_mutex);
show_ctrl(struct controller * ctrl,char * buf)289f3f4681SGreg Kroah-Hartman static int show_ctrl(struct controller *ctrl, char *buf)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	char *out = buf;
311da177e4SLinus Torvalds 	int index;
321da177e4SLinus Torvalds 	struct pci_resource *res;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	out += sprintf(buf, "Free resources: memory\n");
351da177e4SLinus Torvalds 	index = 11;
361da177e4SLinus Torvalds 	res = ctrl->mem_head;
371da177e4SLinus Torvalds 	while (res && index--) {
381da177e4SLinus Torvalds 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
391da177e4SLinus Torvalds 		res = res->next;
401da177e4SLinus Torvalds 	}
411da177e4SLinus Torvalds 	out += sprintf(out, "Free resources: prefetchable memory\n");
421da177e4SLinus Torvalds 	index = 11;
431da177e4SLinus Torvalds 	res = ctrl->p_mem_head;
441da177e4SLinus Torvalds 	while (res && index--) {
451da177e4SLinus Torvalds 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
461da177e4SLinus Torvalds 		res = res->next;
471da177e4SLinus Torvalds 	}
481da177e4SLinus Torvalds 	out += sprintf(out, "Free resources: IO\n");
491da177e4SLinus Torvalds 	index = 11;
501da177e4SLinus Torvalds 	res = ctrl->io_head;
511da177e4SLinus Torvalds 	while (res && index--) {
521da177e4SLinus Torvalds 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
531da177e4SLinus Torvalds 		res = res->next;
541da177e4SLinus Torvalds 	}
551da177e4SLinus Torvalds 	out += sprintf(out, "Free resources: bus numbers\n");
561da177e4SLinus Torvalds 	index = 11;
571da177e4SLinus Torvalds 	res = ctrl->bus_head;
581da177e4SLinus Torvalds 	while (res && index--) {
591da177e4SLinus Torvalds 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
601da177e4SLinus Torvalds 		res = res->next;
611da177e4SLinus Torvalds 	}
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	return out - buf;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
show_dev(struct controller * ctrl,char * buf)669f3f4681SGreg Kroah-Hartman static int show_dev(struct controller *ctrl, char *buf)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	char *out = buf;
691da177e4SLinus Torvalds 	int index;
701da177e4SLinus Torvalds 	struct pci_resource *res;
711da177e4SLinus Torvalds 	struct pci_func *new_slot;
721da177e4SLinus Torvalds 	struct slot *slot;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	slot = ctrl->slot;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	while (slot) {
771da177e4SLinus Torvalds 		new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
781da177e4SLinus Torvalds 		if (!new_slot)
791da177e4SLinus Torvalds 			break;
801da177e4SLinus Torvalds 		out += sprintf(out, "assigned resources: memory\n");
811da177e4SLinus Torvalds 		index = 11;
821da177e4SLinus Torvalds 		res = new_slot->mem_head;
831da177e4SLinus Torvalds 		while (res && index--) {
841da177e4SLinus Torvalds 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
851da177e4SLinus Torvalds 			res = res->next;
861da177e4SLinus Torvalds 		}
871da177e4SLinus Torvalds 		out += sprintf(out, "assigned resources: prefetchable memory\n");
881da177e4SLinus Torvalds 		index = 11;
891da177e4SLinus Torvalds 		res = new_slot->p_mem_head;
901da177e4SLinus Torvalds 		while (res && index--) {
911da177e4SLinus Torvalds 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
921da177e4SLinus Torvalds 			res = res->next;
931da177e4SLinus Torvalds 		}
941da177e4SLinus Torvalds 		out += sprintf(out, "assigned resources: IO\n");
951da177e4SLinus Torvalds 		index = 11;
961da177e4SLinus Torvalds 		res = new_slot->io_head;
971da177e4SLinus Torvalds 		while (res && index--) {
981da177e4SLinus Torvalds 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
991da177e4SLinus Torvalds 			res = res->next;
1001da177e4SLinus Torvalds 		}
1011da177e4SLinus Torvalds 		out += sprintf(out, "assigned resources: bus numbers\n");
1021da177e4SLinus Torvalds 		index = 11;
1031da177e4SLinus Torvalds 		res = new_slot->bus_head;
1041da177e4SLinus Torvalds 		while (res && index--) {
1051da177e4SLinus Torvalds 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
1061da177e4SLinus Torvalds 			res = res->next;
1071da177e4SLinus Torvalds 		}
1081da177e4SLinus Torvalds 		slot = slot->next;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	return out - buf;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
spew_debug_info(struct controller * ctrl,char * data,int size)1149f3f4681SGreg Kroah-Hartman static int spew_debug_info(struct controller *ctrl, char *data, int size)
1151da177e4SLinus Torvalds {
1169f3f4681SGreg Kroah-Hartman 	int used;
1179f3f4681SGreg Kroah-Hartman 
1189f3f4681SGreg Kroah-Hartman 	used = size - show_ctrl(ctrl, data);
1199f3f4681SGreg Kroah-Hartman 	used = (size - used) - show_dev(ctrl, &data[used]);
1209f3f4681SGreg Kroah-Hartman 	return used;
1211da177e4SLinus Torvalds }
1229f3f4681SGreg Kroah-Hartman 
1239f3f4681SGreg Kroah-Hartman struct ctrl_dbg {
1249f3f4681SGreg Kroah-Hartman 	int size;
1259f3f4681SGreg Kroah-Hartman 	char *data;
1269f3f4681SGreg Kroah-Hartman 	struct controller *ctrl;
1279f3f4681SGreg Kroah-Hartman };
1289f3f4681SGreg Kroah-Hartman 
1299f3f4681SGreg Kroah-Hartman #define MAX_OUTPUT	(4*PAGE_SIZE)
1309f3f4681SGreg Kroah-Hartman 
open(struct inode * inode,struct file * file)1319f3f4681SGreg Kroah-Hartman static int open(struct inode *inode, struct file *file)
1329f3f4681SGreg Kroah-Hartman {
1338e18e294STheodore Ts'o 	struct controller *ctrl = inode->i_private;
1349f3f4681SGreg Kroah-Hartman 	struct ctrl_dbg *dbg;
1359f3f4681SGreg Kroah-Hartman 	int retval = -ENOMEM;
1369f3f4681SGreg Kroah-Hartman 
137613655faSArnd Bergmann 	mutex_lock(&cpqphp_mutex);
1389f3f4681SGreg Kroah-Hartman 	dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
1399f3f4681SGreg Kroah-Hartman 	if (!dbg)
1409f3f4681SGreg Kroah-Hartman 		goto exit;
1419f3f4681SGreg Kroah-Hartman 	dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
1429f3f4681SGreg Kroah-Hartman 	if (!dbg->data) {
1439f3f4681SGreg Kroah-Hartman 		kfree(dbg);
1449f3f4681SGreg Kroah-Hartman 		goto exit;
1459f3f4681SGreg Kroah-Hartman 	}
1469f3f4681SGreg Kroah-Hartman 	dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
1479f3f4681SGreg Kroah-Hartman 	file->private_data = dbg;
1489f3f4681SGreg Kroah-Hartman 	retval = 0;
1499f3f4681SGreg Kroah-Hartman exit:
150613655faSArnd Bergmann 	mutex_unlock(&cpqphp_mutex);
1519f3f4681SGreg Kroah-Hartman 	return retval;
1529f3f4681SGreg Kroah-Hartman }
1539f3f4681SGreg Kroah-Hartman 
lseek(struct file * file,loff_t off,int whence)1549f3f4681SGreg Kroah-Hartman static loff_t lseek(struct file *file, loff_t off, int whence)
1559f3f4681SGreg Kroah-Hartman {
156d2c40f78SAl Viro 	struct ctrl_dbg *dbg = file->private_data;
157d2c40f78SAl Viro 	return fixed_size_llseek(file, off, whence, dbg->size);
1589f3f4681SGreg Kroah-Hartman }
1599f3f4681SGreg Kroah-Hartman 
read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)1609f3f4681SGreg Kroah-Hartman static ssize_t read(struct file *file, char __user *buf,
1619f3f4681SGreg Kroah-Hartman 		    size_t nbytes, loff_t *ppos)
1629f3f4681SGreg Kroah-Hartman {
1639f3f4681SGreg Kroah-Hartman 	struct ctrl_dbg *dbg = file->private_data;
1649f3f4681SGreg Kroah-Hartman 	return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
1659f3f4681SGreg Kroah-Hartman }
1669f3f4681SGreg Kroah-Hartman 
release(struct inode * inode,struct file * file)1679f3f4681SGreg Kroah-Hartman static int release(struct inode *inode, struct file *file)
1689f3f4681SGreg Kroah-Hartman {
1699f3f4681SGreg Kroah-Hartman 	struct ctrl_dbg *dbg = file->private_data;
1709f3f4681SGreg Kroah-Hartman 
1719f3f4681SGreg Kroah-Hartman 	kfree(dbg->data);
1729f3f4681SGreg Kroah-Hartman 	kfree(dbg);
1739f3f4681SGreg Kroah-Hartman 	return 0;
1749f3f4681SGreg Kroah-Hartman }
1759f3f4681SGreg Kroah-Hartman 
176d54b1fdbSArjan van de Ven static const struct file_operations debug_ops = {
1779f3f4681SGreg Kroah-Hartman 	.owner = THIS_MODULE,
1789f3f4681SGreg Kroah-Hartman 	.open = open,
1799f3f4681SGreg Kroah-Hartman 	.llseek = lseek,
1809f3f4681SGreg Kroah-Hartman 	.read = read,
1819f3f4681SGreg Kroah-Hartman 	.release = release,
1829f3f4681SGreg Kroah-Hartman };
1839f3f4681SGreg Kroah-Hartman 
1849f3f4681SGreg Kroah-Hartman static struct dentry *root;
1859f3f4681SGreg Kroah-Hartman 
cpqhp_initialize_debugfs(void)1869f3f4681SGreg Kroah-Hartman void cpqhp_initialize_debugfs(void)
1879f3f4681SGreg Kroah-Hartman {
1889f3f4681SGreg Kroah-Hartman 	if (!root)
1899f3f4681SGreg Kroah-Hartman 		root = debugfs_create_dir("cpqhp", NULL);
1909f3f4681SGreg Kroah-Hartman }
1919f3f4681SGreg Kroah-Hartman 
cpqhp_shutdown_debugfs(void)1929f3f4681SGreg Kroah-Hartman void cpqhp_shutdown_debugfs(void)
1939f3f4681SGreg Kroah-Hartman {
1949f3f4681SGreg Kroah-Hartman 	debugfs_remove(root);
1959f3f4681SGreg Kroah-Hartman }
1969f3f4681SGreg Kroah-Hartman 
cpqhp_create_debugfs_files(struct controller * ctrl)1979f3f4681SGreg Kroah-Hartman void cpqhp_create_debugfs_files(struct controller *ctrl)
1989f3f4681SGreg Kroah-Hartman {
19954cc6954SKay Sievers 	ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
20054cc6954SKay Sievers 					   S_IRUGO, root, ctrl, &debug_ops);
2019f3f4681SGreg Kroah-Hartman }
2029f3f4681SGreg Kroah-Hartman 
cpqhp_remove_debugfs_files(struct controller * ctrl)2039f3f4681SGreg Kroah-Hartman void cpqhp_remove_debugfs_files(struct controller *ctrl)
2049f3f4681SGreg Kroah-Hartman {
2059f3f4681SGreg Kroah-Hartman 	debugfs_remove(ctrl->dentry);
2069f3f4681SGreg Kroah-Hartman 	ctrl->dentry = NULL;
2079f3f4681SGreg Kroah-Hartman }
2089f3f4681SGreg Kroah-Hartman 
209