blob: 17c1f36315d191b46bf81734efa69fff344bfa84 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/proc_fs.h>
34#include <linux/workqueue.h>
35#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070036#include <linux/pci_hotplug.h>
Arnd Bergmann613655f2010-06-02 14:28:52 +020037#include <linux/mutex.h>
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080038#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "cpqphp.h"
40
Arnd Bergmann613655f2010-06-02 14:28:52 +020041static DEFINE_MUTEX(cpqphp_mutex);
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080042static int show_ctrl (struct controller *ctrl, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080044 char *out = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 int index;
46 struct pci_resource *res;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 out += sprintf(buf, "Free resources: memory\n");
49 index = 11;
50 res = ctrl->mem_head;
51 while (res && index--) {
52 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
53 res = res->next;
54 }
55 out += sprintf(out, "Free resources: prefetchable memory\n");
56 index = 11;
57 res = ctrl->p_mem_head;
58 while (res && index--) {
59 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
60 res = res->next;
61 }
62 out += sprintf(out, "Free resources: IO\n");
63 index = 11;
64 res = ctrl->io_head;
65 while (res && index--) {
66 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
67 res = res->next;
68 }
69 out += sprintf(out, "Free resources: bus numbers\n");
70 index = 11;
71 res = ctrl->bus_head;
72 while (res && index--) {
73 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
74 res = res->next;
75 }
76
77 return out - buf;
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080080static int show_dev (struct controller *ctrl, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 char * out = buf;
83 int index;
84 struct pci_resource *res;
85 struct pci_func *new_slot;
86 struct slot *slot;
87
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -080088 slot = ctrl->slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 while (slot) {
91 new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
92 if (!new_slot)
93 break;
94 out += sprintf(out, "assigned resources: memory\n");
95 index = 11;
96 res = new_slot->mem_head;
97 while (res && index--) {
98 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
99 res = res->next;
100 }
101 out += sprintf(out, "assigned resources: prefetchable memory\n");
102 index = 11;
103 res = new_slot->p_mem_head;
104 while (res && index--) {
105 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
106 res = res->next;
107 }
108 out += sprintf(out, "assigned resources: IO\n");
109 index = 11;
110 res = new_slot->io_head;
111 while (res && index--) {
112 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
113 res = res->next;
114 }
115 out += sprintf(out, "assigned resources: bus numbers\n");
116 index = 11;
117 res = new_slot->bus_head;
118 while (res && index--) {
119 out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
120 res = res->next;
121 }
122 slot=slot->next;
123 }
124
125 return out - buf;
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800128static int spew_debug_info(struct controller *ctrl, char *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800130 int used;
131
132 used = size - show_ctrl(ctrl, data);
133 used = (size - used) - show_dev(ctrl, &data[used]);
134 return used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800136
137struct ctrl_dbg {
138 int size;
139 char *data;
140 struct controller *ctrl;
141};
142
143#define MAX_OUTPUT (4*PAGE_SIZE)
144
145static int open(struct inode *inode, struct file *file)
146{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700147 struct controller *ctrl = inode->i_private;
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800148 struct ctrl_dbg *dbg;
149 int retval = -ENOMEM;
150
Arnd Bergmann613655f2010-06-02 14:28:52 +0200151 mutex_lock(&cpqphp_mutex);
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800152 dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
153 if (!dbg)
154 goto exit;
155 dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
156 if (!dbg->data) {
157 kfree(dbg);
158 goto exit;
159 }
160 dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
161 file->private_data = dbg;
162 retval = 0;
163exit:
Arnd Bergmann613655f2010-06-02 14:28:52 +0200164 mutex_unlock(&cpqphp_mutex);
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800165 return retval;
166}
167
168static loff_t lseek(struct file *file, loff_t off, int whence)
169{
Al Virod2c40f72013-06-23 12:46:49 +0400170 struct ctrl_dbg *dbg = file->private_data;
171 return fixed_size_llseek(file, off, whence, dbg->size);
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800172}
173
174static ssize_t read(struct file *file, char __user *buf,
175 size_t nbytes, loff_t *ppos)
176{
177 struct ctrl_dbg *dbg = file->private_data;
178 return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
179}
180
181static int release(struct inode *inode, struct file *file)
182{
183 struct ctrl_dbg *dbg = file->private_data;
184
185 kfree(dbg->data);
186 kfree(dbg);
187 return 0;
188}
189
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800190static const struct file_operations debug_ops = {
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800191 .owner = THIS_MODULE,
192 .open = open,
193 .llseek = lseek,
194 .read = read,
195 .release = release,
196};
197
198static struct dentry *root;
199
200void cpqhp_initialize_debugfs(void)
201{
202 if (!root)
203 root = debugfs_create_dir("cpqhp", NULL);
204}
205
206void cpqhp_shutdown_debugfs(void)
207{
208 debugfs_remove(root);
209}
210
211void cpqhp_create_debugfs_files(struct controller *ctrl)
212{
Kay Sievers54cc6952009-03-24 16:38:21 -0700213 ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
214 S_IRUGO, root, ctrl, &debug_ops);
Greg Kroah-Hartman9f3f4682005-12-14 09:37:26 -0800215}
216
217void cpqhp_remove_debugfs_files(struct controller *ctrl)
218{
219 if (ctrl->dentry)
220 debugfs_remove(ctrl->dentry);
221 ctrl->dentry = NULL;
222}
223