blob: e6089bdb6e5bddbc2b08e09c9419d26056e543e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/proc_fs.h>
33#include <linux/workqueue.h>
34#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070035#include <linux/pci_hotplug.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040036#include <linux/smp_lock.h>
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080037#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "cpqphp.h"
39
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080040static int show_ctrl (struct controller *ctrl, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080042 char *out = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 int index;
44 struct pci_resource *res;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 out += sprintf(buf, "Free resources: memory\n");
47 index = 11;
48 res = ctrl->mem_head;
49 while (res && index--) {
50 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
51 res = res->next;
52 }
53 out += sprintf(out, "Free resources: prefetchable memory\n");
54 index = 11;
55 res = ctrl->p_mem_head;
56 while (res && index--) {
57 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
58 res = res->next;
59 }
60 out += sprintf(out, "Free resources: IO\n");
61 index = 11;
62 res = ctrl->io_head;
63 while (res && index--) {
64 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
65 res = res->next;
66 }
67 out += sprintf(out, "Free resources: bus numbers\n");
68 index = 11;
69 res = ctrl->bus_head;
70 while (res && index--) {
71 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
72 res = res->next;
73 }
74
75 return out - buf;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080078static int show_dev (struct controller *ctrl, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 char * out = buf;
81 int index;
82 struct pci_resource *res;
83 struct pci_func *new_slot;
84 struct slot *slot;
85
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080086 slot = ctrl->slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 while (slot) {
89 new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
90 if (!new_slot)
91 break;
92 out += sprintf(out, "assigned resources: memory\n");
93 index = 11;
94 res = new_slot->mem_head;
95 while (res && index--) {
96 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
97 res = res->next;
98 }
99 out += sprintf(out, "assigned resources: prefetchable memory\n");
100 index = 11;
101 res = new_slot->p_mem_head;
102 while (res && index--) {
103 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
104 res = res->next;
105 }
106 out += sprintf(out, "assigned resources: IO\n");
107 index = 11;
108 res = new_slot->io_head;
109 while (res && index--) {
110 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
111 res = res->next;
112 }
113 out += sprintf(out, "assigned resources: bus numbers\n");
114 index = 11;
115 res = new_slot->bus_head;
116 while (res && index--) {
117 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
118 res = res->next;
119 }
120 slot=slot->next;
121 }
122
123 return out - buf;
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800126static int spew_debug_info(struct controller *ctrl, char *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800128 int used;
129
130 used = size - show_ctrl(ctrl, data);
131 used = (size - used) - show_dev(ctrl, &data[used]);
132 return used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800134
135struct ctrl_dbg {
136 int size;
137 char *data;
138 struct controller *ctrl;
139};
140
141#define MAX_OUTPUT (4*PAGE_SIZE)
142
143static int open(struct inode *inode, struct file *file)
144{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700145 struct controller *ctrl = inode->i_private;
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800146 struct ctrl_dbg *dbg;
147 int retval = -ENOMEM;
148
149 lock_kernel();
150 dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
151 if (!dbg)
152 goto exit;
153 dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
154 if (!dbg->data) {
155 kfree(dbg);
156 goto exit;
157 }
158 dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
159 file->private_data = dbg;
160 retval = 0;
161exit:
162 unlock_kernel();
163 return retval;
164}
165
166static loff_t lseek(struct file *file, loff_t off, int whence)
167{
168 struct ctrl_dbg *dbg;
169 loff_t new = -1;
170
171 lock_kernel();
172 dbg = file->private_data;
173
174 switch (whence) {
175 case 0:
176 new = off;
177 break;
178 case 1:
179 new = file->f_pos + off;
180 break;
181 }
182 if (new < 0 || new > dbg->size) {
183 unlock_kernel();
184 return -EINVAL;
185 }
186 unlock_kernel();
187 return (file->f_pos = new);
188}
189
190static ssize_t read(struct file *file, char __user *buf,
191 size_t nbytes, loff_t *ppos)
192{
193 struct ctrl_dbg *dbg = file->private_data;
194 return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
195}
196
197static int release(struct inode *inode, struct file *file)
198{
199 struct ctrl_dbg *dbg = file->private_data;
200
201 kfree(dbg->data);
202 kfree(dbg);
203 return 0;
204}
205
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800206static const struct file_operations debug_ops = {
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800207 .owner = THIS_MODULE,
208 .open = open,
209 .llseek = lseek,
210 .read = read,
211 .release = release,
212};
213
214static struct dentry *root;
215
216void cpqhp_initialize_debugfs(void)
217{
218 if (!root)
219 root = debugfs_create_dir("cpqhp", NULL);
220}
221
222void cpqhp_shutdown_debugfs(void)
223{
224 debugfs_remove(root);
225}
226
227void cpqhp_create_debugfs_files(struct controller *ctrl)
228{
Kay Sievers54cc6952009-03-24 16:38:21 -0700229 ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
230 S_IRUGO, root, ctrl, &debug_ops);
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800231}
232
233void cpqhp_remove_debugfs_files(struct controller *ctrl)
234{
235 if (ctrl->dentry)
236 debugfs_remove(ctrl->dentry);
237 ctrl->dentry = NULL;
238}
239