1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Compaq Hot Plug Controller Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1995,2001 Compaq Computer Corporation
6*4882a593Smuzhiyun * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
7*4882a593Smuzhiyun * Copyright (C) 2001 IBM Corp.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * All rights reserved.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Send feedback to <greg@kroah.com>
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/types.h>
19*4882a593Smuzhiyun #include <linux/proc_fs.h>
20*4882a593Smuzhiyun #include <linux/workqueue.h>
21*4882a593Smuzhiyun #include <linux/pci.h>
22*4882a593Smuzhiyun #include <linux/pci_hotplug.h>
23*4882a593Smuzhiyun #include <linux/mutex.h>
24*4882a593Smuzhiyun #include <linux/debugfs.h>
25*4882a593Smuzhiyun #include "cpqphp.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun static DEFINE_MUTEX(cpqphp_mutex);
show_ctrl(struct controller * ctrl,char * buf)28*4882a593Smuzhiyun static int show_ctrl(struct controller *ctrl, char *buf)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun char *out = buf;
31*4882a593Smuzhiyun int index;
32*4882a593Smuzhiyun struct pci_resource *res;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun out += sprintf(buf, "Free resources: memory\n");
35*4882a593Smuzhiyun index = 11;
36*4882a593Smuzhiyun res = ctrl->mem_head;
37*4882a593Smuzhiyun while (res && index--) {
38*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
39*4882a593Smuzhiyun res = res->next;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun out += sprintf(out, "Free resources: prefetchable memory\n");
42*4882a593Smuzhiyun index = 11;
43*4882a593Smuzhiyun res = ctrl->p_mem_head;
44*4882a593Smuzhiyun while (res && index--) {
45*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
46*4882a593Smuzhiyun res = res->next;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun out += sprintf(out, "Free resources: IO\n");
49*4882a593Smuzhiyun index = 11;
50*4882a593Smuzhiyun res = ctrl->io_head;
51*4882a593Smuzhiyun while (res && index--) {
52*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
53*4882a593Smuzhiyun res = res->next;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun out += sprintf(out, "Free resources: bus numbers\n");
56*4882a593Smuzhiyun index = 11;
57*4882a593Smuzhiyun res = ctrl->bus_head;
58*4882a593Smuzhiyun while (res && index--) {
59*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
60*4882a593Smuzhiyun res = res->next;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun return out - buf;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
show_dev(struct controller * ctrl,char * buf)66*4882a593Smuzhiyun static int show_dev(struct controller *ctrl, char *buf)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun char *out = buf;
69*4882a593Smuzhiyun int index;
70*4882a593Smuzhiyun struct pci_resource *res;
71*4882a593Smuzhiyun struct pci_func *new_slot;
72*4882a593Smuzhiyun struct slot *slot;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun slot = ctrl->slot;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun while (slot) {
77*4882a593Smuzhiyun new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
78*4882a593Smuzhiyun if (!new_slot)
79*4882a593Smuzhiyun break;
80*4882a593Smuzhiyun out += sprintf(out, "assigned resources: memory\n");
81*4882a593Smuzhiyun index = 11;
82*4882a593Smuzhiyun res = new_slot->mem_head;
83*4882a593Smuzhiyun while (res && index--) {
84*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
85*4882a593Smuzhiyun res = res->next;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun out += sprintf(out, "assigned resources: prefetchable memory\n");
88*4882a593Smuzhiyun index = 11;
89*4882a593Smuzhiyun res = new_slot->p_mem_head;
90*4882a593Smuzhiyun while (res && index--) {
91*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
92*4882a593Smuzhiyun res = res->next;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun out += sprintf(out, "assigned resources: IO\n");
95*4882a593Smuzhiyun index = 11;
96*4882a593Smuzhiyun res = new_slot->io_head;
97*4882a593Smuzhiyun while (res && index--) {
98*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
99*4882a593Smuzhiyun res = res->next;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun out += sprintf(out, "assigned resources: bus numbers\n");
102*4882a593Smuzhiyun index = 11;
103*4882a593Smuzhiyun res = new_slot->bus_head;
104*4882a593Smuzhiyun while (res && index--) {
105*4882a593Smuzhiyun out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
106*4882a593Smuzhiyun res = res->next;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun slot = slot->next;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun return out - buf;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
spew_debug_info(struct controller * ctrl,char * data,int size)114*4882a593Smuzhiyun static int spew_debug_info(struct controller *ctrl, char *data, int size)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun int used;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun used = size - show_ctrl(ctrl, data);
119*4882a593Smuzhiyun used = (size - used) - show_dev(ctrl, &data[used]);
120*4882a593Smuzhiyun return used;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun struct ctrl_dbg {
124*4882a593Smuzhiyun int size;
125*4882a593Smuzhiyun char *data;
126*4882a593Smuzhiyun struct controller *ctrl;
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun #define MAX_OUTPUT (4*PAGE_SIZE)
130*4882a593Smuzhiyun
open(struct inode * inode,struct file * file)131*4882a593Smuzhiyun static int open(struct inode *inode, struct file *file)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct controller *ctrl = inode->i_private;
134*4882a593Smuzhiyun struct ctrl_dbg *dbg;
135*4882a593Smuzhiyun int retval = -ENOMEM;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun mutex_lock(&cpqphp_mutex);
138*4882a593Smuzhiyun dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
139*4882a593Smuzhiyun if (!dbg)
140*4882a593Smuzhiyun goto exit;
141*4882a593Smuzhiyun dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
142*4882a593Smuzhiyun if (!dbg->data) {
143*4882a593Smuzhiyun kfree(dbg);
144*4882a593Smuzhiyun goto exit;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
147*4882a593Smuzhiyun file->private_data = dbg;
148*4882a593Smuzhiyun retval = 0;
149*4882a593Smuzhiyun exit:
150*4882a593Smuzhiyun mutex_unlock(&cpqphp_mutex);
151*4882a593Smuzhiyun return retval;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
lseek(struct file * file,loff_t off,int whence)154*4882a593Smuzhiyun static loff_t lseek(struct file *file, loff_t off, int whence)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun struct ctrl_dbg *dbg = file->private_data;
157*4882a593Smuzhiyun return fixed_size_llseek(file, off, whence, dbg->size);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)160*4882a593Smuzhiyun static ssize_t read(struct file *file, char __user *buf,
161*4882a593Smuzhiyun size_t nbytes, loff_t *ppos)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun struct ctrl_dbg *dbg = file->private_data;
164*4882a593Smuzhiyun return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
release(struct inode * inode,struct file * file)167*4882a593Smuzhiyun static int release(struct inode *inode, struct file *file)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun struct ctrl_dbg *dbg = file->private_data;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun kfree(dbg->data);
172*4882a593Smuzhiyun kfree(dbg);
173*4882a593Smuzhiyun return 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static const struct file_operations debug_ops = {
177*4882a593Smuzhiyun .owner = THIS_MODULE,
178*4882a593Smuzhiyun .open = open,
179*4882a593Smuzhiyun .llseek = lseek,
180*4882a593Smuzhiyun .read = read,
181*4882a593Smuzhiyun .release = release,
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun static struct dentry *root;
185*4882a593Smuzhiyun
cpqhp_initialize_debugfs(void)186*4882a593Smuzhiyun void cpqhp_initialize_debugfs(void)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun if (!root)
189*4882a593Smuzhiyun root = debugfs_create_dir("cpqhp", NULL);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
cpqhp_shutdown_debugfs(void)192*4882a593Smuzhiyun void cpqhp_shutdown_debugfs(void)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun debugfs_remove(root);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
cpqhp_create_debugfs_files(struct controller * ctrl)197*4882a593Smuzhiyun void cpqhp_create_debugfs_files(struct controller *ctrl)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
200*4882a593Smuzhiyun S_IRUGO, root, ctrl, &debug_ops);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
cpqhp_remove_debugfs_files(struct controller * ctrl)203*4882a593Smuzhiyun void cpqhp_remove_debugfs_files(struct controller *ctrl)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun debugfs_remove(ctrl->dentry);
206*4882a593Smuzhiyun ctrl->dentry = NULL;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209