xref: /linux/drivers/pci/hotplug/cpqphp_sysfs.c (revision cc4589ebfae6f8dbb5cf880a0a67eedab3416492)
1 /*
2  * Compaq Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM Corp.
7  *
8  * All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or (at
13  * your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18  * NON INFRINGEMENT.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Send feedback to <greg@kroah.com>
26  *
27  */
28 
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/proc_fs.h>
34 #include <linux/workqueue.h>
35 #include <linux/pci.h>
36 #include <linux/pci_hotplug.h>
37 #include <linux/smp_lock.h>
38 #include <linux/debugfs.h>
39 #include "cpqphp.h"
40 
41 static int show_ctrl (struct controller *ctrl, char *buf)
42 {
43 	char *out = buf;
44 	int index;
45 	struct pci_resource *res;
46 
47 	out += sprintf(buf, "Free resources: memory\n");
48 	index = 11;
49 	res = ctrl->mem_head;
50 	while (res && index--) {
51 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
52 		res = res->next;
53 	}
54 	out += sprintf(out, "Free resources: prefetchable memory\n");
55 	index = 11;
56 	res = ctrl->p_mem_head;
57 	while (res && index--) {
58 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
59 		res = res->next;
60 	}
61 	out += sprintf(out, "Free resources: IO\n");
62 	index = 11;
63 	res = ctrl->io_head;
64 	while (res && index--) {
65 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
66 		res = res->next;
67 	}
68 	out += sprintf(out, "Free resources: bus numbers\n");
69 	index = 11;
70 	res = ctrl->bus_head;
71 	while (res && index--) {
72 		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
73 		res = res->next;
74 	}
75 
76 	return out - buf;
77 }
78 
79 static int show_dev (struct controller *ctrl, char *buf)
80 {
81 	char * out = buf;
82 	int index;
83 	struct pci_resource *res;
84 	struct pci_func *new_slot;
85 	struct slot *slot;
86 
87 	slot = ctrl->slot;
88 
89 	while (slot) {
90 		new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
91 		if (!new_slot)
92 			break;
93 		out += sprintf(out, "assigned resources: memory\n");
94 		index = 11;
95 		res = new_slot->mem_head;
96 		while (res && index--) {
97 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
98 			res = res->next;
99 		}
100 		out += sprintf(out, "assigned resources: prefetchable memory\n");
101 		index = 11;
102 		res = new_slot->p_mem_head;
103 		while (res && index--) {
104 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
105 			res = res->next;
106 		}
107 		out += sprintf(out, "assigned resources: IO\n");
108 		index = 11;
109 		res = new_slot->io_head;
110 		while (res && index--) {
111 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
112 			res = res->next;
113 		}
114 		out += sprintf(out, "assigned resources: bus numbers\n");
115 		index = 11;
116 		res = new_slot->bus_head;
117 		while (res && index--) {
118 			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
119 			res = res->next;
120 		}
121 		slot=slot->next;
122 	}
123 
124 	return out - buf;
125 }
126 
127 static int spew_debug_info(struct controller *ctrl, char *data, int size)
128 {
129 	int used;
130 
131 	used = size - show_ctrl(ctrl, data);
132 	used = (size - used) - show_dev(ctrl, &data[used]);
133 	return used;
134 }
135 
136 struct ctrl_dbg {
137 	int size;
138 	char *data;
139 	struct controller *ctrl;
140 };
141 
142 #define MAX_OUTPUT	(4*PAGE_SIZE)
143 
144 static int open(struct inode *inode, struct file *file)
145 {
146 	struct controller *ctrl = inode->i_private;
147 	struct ctrl_dbg *dbg;
148 	int retval = -ENOMEM;
149 
150 	lock_kernel();
151 	dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
152 	if (!dbg)
153 		goto exit;
154 	dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
155 	if (!dbg->data) {
156 		kfree(dbg);
157 		goto exit;
158 	}
159 	dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
160 	file->private_data = dbg;
161 	retval = 0;
162 exit:
163 	unlock_kernel();
164 	return retval;
165 }
166 
167 static loff_t lseek(struct file *file, loff_t off, int whence)
168 {
169 	struct ctrl_dbg *dbg;
170 	loff_t new = -1;
171 
172 	lock_kernel();
173 	dbg = file->private_data;
174 
175 	switch (whence) {
176 	case 0:
177 		new = off;
178 		break;
179 	case 1:
180 		new = file->f_pos + off;
181 		break;
182 	}
183 	if (new < 0 || new > dbg->size) {
184 		unlock_kernel();
185 		return -EINVAL;
186 	}
187 	unlock_kernel();
188 	return (file->f_pos = new);
189 }
190 
191 static ssize_t read(struct file *file, char __user *buf,
192 		    size_t nbytes, loff_t *ppos)
193 {
194 	struct ctrl_dbg *dbg = file->private_data;
195 	return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
196 }
197 
198 static int release(struct inode *inode, struct file *file)
199 {
200 	struct ctrl_dbg *dbg = file->private_data;
201 
202 	kfree(dbg->data);
203 	kfree(dbg);
204 	return 0;
205 }
206 
207 static const struct file_operations debug_ops = {
208 	.owner = THIS_MODULE,
209 	.open = open,
210 	.llseek = lseek,
211 	.read = read,
212 	.release = release,
213 };
214 
215 static struct dentry *root;
216 
217 void cpqhp_initialize_debugfs(void)
218 {
219 	if (!root)
220 		root = debugfs_create_dir("cpqhp", NULL);
221 }
222 
223 void cpqhp_shutdown_debugfs(void)
224 {
225 	debugfs_remove(root);
226 }
227 
228 void cpqhp_create_debugfs_files(struct controller *ctrl)
229 {
230 	ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
231 					   S_IRUGO, root, ctrl, &debug_ops);
232 }
233 
234 void cpqhp_remove_debugfs_files(struct controller *ctrl)
235 {
236 	if (ctrl->dentry)
237 		debugfs_remove(ctrl->dentry);
238 	ctrl->dentry = NULL;
239 }
240 
241