patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / scsi / scsi_proc.c
1 /*
2  * linux/drivers/scsi/scsi_proc.c
3  *
4  * The functions in this file provide an interface between
5  * the PROC file system and the SCSI device drivers
6  * It is mainly used for debugging, statistics and to pass 
7  * information directly to the lowlevel driver.
8  *
9  * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de 
10  * Version: 0.99.8   last change: 95/09/13
11  * 
12  * generic command parser provided by: 
13  * Andreas Heilwagen <crashcar@informatik.uni-koblenz.de>
14  *
15  * generic_proc_info() support of xxxx_info() by:
16  * Michael A. Griffith <grif@acm.org>
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/proc_fs.h>
25 #include <linux/errno.h>
26 #include <linux/blkdev.h>
27 #include <linux/seq_file.h>
28 #include <asm/uaccess.h>
29
30 #include <scsi/scsi_host.h>
31 #include "scsi.h"
32
33 #include "scsi_priv.h"
34 #include "scsi_logging.h"
35
36
37 /* 4K page size, but our output routines, use some slack for overruns */
38 #define PROC_BLOCK_SIZE (3*1024)
39
40 static struct proc_dir_entry *proc_scsi;
41
42 /* Protect sht->present and sht->proc_dir */
43 static DECLARE_MUTEX(global_host_template_sem);
44
45 static int proc_scsi_read(char *buffer, char **start, off_t offset,
46                           int length, int *eof, void *data)
47 {
48         struct Scsi_Host *shost = data;
49         int n;
50
51         n = shost->hostt->proc_info(shost, buffer, start, offset, length, 0);
52         *eof = (n < length);
53
54         return n;
55 }
56
57 static int proc_scsi_write_proc(struct file *file, const char __user *buf,
58                            unsigned long count, void *data)
59 {
60         struct Scsi_Host *shost = data;
61         ssize_t ret = -ENOMEM;
62         char *page;
63         char *start;
64     
65         if (count > PROC_BLOCK_SIZE)
66                 return -EOVERFLOW;
67
68         page = (char *)__get_free_page(GFP_KERNEL);
69         if (page) {
70                 ret = -EFAULT;
71                 if (copy_from_user(page, buf, count))
72                         goto out;
73                 ret = shost->hostt->proc_info(shost, page, &start, 0, count, 1);
74         }
75 out:
76         free_page((unsigned long)page);
77         return ret;
78 }
79
80 void scsi_proc_hostdir_add(struct scsi_host_template *sht)
81 {
82         if (!sht->proc_info)
83                 return;
84
85         down(&global_host_template_sem);
86         if (!sht->present++) {
87                 sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi);
88                 if (!sht->proc_dir)
89                         printk(KERN_ERR "%s: proc_mkdir failed for %s\n",
90                                __FUNCTION__, sht->proc_name);
91                 else
92                         sht->proc_dir->owner = sht->module;
93         }
94         up(&global_host_template_sem);
95 }
96
97 void scsi_proc_hostdir_rm(struct scsi_host_template *sht)
98 {
99         if (!sht->proc_info)
100                 return;
101
102         down(&global_host_template_sem);
103         if (!--sht->present && sht->proc_dir) {
104                 remove_proc_entry(sht->proc_name, proc_scsi);
105                 sht->proc_dir = NULL;
106         }
107         up(&global_host_template_sem);
108 }
109
110 void scsi_proc_host_add(struct Scsi_Host *shost)
111 {
112         struct scsi_host_template *sht = shost->hostt;
113         struct proc_dir_entry *p;
114         char name[10];
115
116         if (!sht->proc_dir)
117                 return;
118
119         sprintf(name,"%d", shost->host_no);
120         p = create_proc_read_entry(name, S_IFREG | S_IRUGO | S_IWUSR,
121                         sht->proc_dir, proc_scsi_read, shost);
122         if (!p) {
123                 printk(KERN_ERR "%s: Failed to register host %d in"
124                        "%s\n", __FUNCTION__, shost->host_no,
125                        sht->proc_name);
126                 return;
127         } 
128
129         p->write_proc = proc_scsi_write_proc;
130         p->owner = sht->module;
131 }
132
133 void scsi_proc_host_rm(struct Scsi_Host *shost)
134 {
135         char name[10];
136
137         if (!shost->hostt->proc_dir)
138                 return;
139
140         sprintf(name,"%d", shost->host_no);
141         remove_proc_entry(name, shost->hostt->proc_dir);
142 }
143
144 static int proc_print_scsidevice(struct device *dev, void *data)
145 {
146         struct scsi_device *sdev = to_scsi_device(dev);
147         struct seq_file *s = data;
148         int i;
149
150         seq_printf(s,
151                 "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n  Vendor: ",
152                 sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
153         for (i = 0; i < 8; i++) {
154                 if (sdev->vendor[i] >= 0x20)
155                         seq_printf(s, "%c", sdev->vendor[i]);
156                 else
157                         seq_printf(s, " ");
158         }
159
160         seq_printf(s, " Model: ");
161         for (i = 0; i < 16; i++) {
162                 if (sdev->model[i] >= 0x20)
163                         seq_printf(s, "%c", sdev->model[i]);
164                 else
165                         seq_printf(s, " ");
166         }
167
168         seq_printf(s, " Rev: ");
169         for (i = 0; i < 4; i++) {
170                 if (sdev->rev[i] >= 0x20)
171                         seq_printf(s, "%c", sdev->rev[i]);
172                 else
173                         seq_printf(s, " ");
174         }
175
176         seq_printf(s, "\n");
177
178         seq_printf(s, "  Type:   %s ",
179                      sdev->type < MAX_SCSI_DEVICE_CODE ?
180                scsi_device_types[(int) sdev->type] : "Unknown          ");
181         seq_printf(s, "               ANSI"
182                      " SCSI revision: %02x", (sdev->scsi_level - 1) ?
183                      sdev->scsi_level - 1 : 1);
184         if (sdev->scsi_level == 2)
185                 seq_printf(s, " CCS\n");
186         else
187                 seq_printf(s, "\n");
188
189         return 0;
190 }
191
192 static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
193 {
194         struct Scsi_Host *shost;
195         int error = -ENXIO;
196
197         shost = scsi_host_lookup(host);
198         if (IS_ERR(shost))
199                 return PTR_ERR(shost);
200
201         error = scsi_scan_host_selected(shost, channel, id, lun, 1);
202         scsi_host_put(shost);
203         return error;
204 }
205
206 static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
207 {
208         struct scsi_device *sdev;
209         struct Scsi_Host *shost;
210         int error = -ENXIO;
211
212         shost = scsi_host_lookup(host);
213         if (IS_ERR(shost))
214                 return PTR_ERR(shost);
215         sdev = scsi_device_lookup(shost, channel, id, lun);
216         if (sdev) {
217                 scsi_remove_device(sdev);
218                 scsi_device_put(sdev);
219                 error = 0;
220         }
221
222         scsi_host_put(shost);
223         return error;
224 }
225
226 static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
227                                size_t length, loff_t *ppos)
228 {
229         int host, channel, id, lun;
230         char *buffer, *p;
231         int err;
232
233         if (!buf || length > PAGE_SIZE)
234                 return -EINVAL;
235
236         buffer = (char *)__get_free_page(GFP_KERNEL);
237         if (!buffer)
238                 return -ENOMEM;
239
240         err = -EFAULT;
241         if (copy_from_user(buffer, buf, length))
242                 goto out;
243
244         err = -EINVAL;
245         if (length < PAGE_SIZE)
246                 buffer[length] = '\0';
247         else if (buffer[PAGE_SIZE-1])
248                 goto out;
249
250         /*
251          * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
252          * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
253          */
254         if (!strncmp("scsi add-single-device", buffer, 22)) {
255                 p = buffer + 23;
256
257                 host = simple_strtoul(p, &p, 0);
258                 channel = simple_strtoul(p + 1, &p, 0);
259                 id = simple_strtoul(p + 1, &p, 0);
260                 lun = simple_strtoul(p + 1, &p, 0);
261
262                 err = scsi_add_single_device(host, channel, id, lun);
263                 if (err >= 0)
264                         err = length;
265
266         /*
267          * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
268          * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
269          */
270         } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
271                 p = buffer + 26;
272
273                 host = simple_strtoul(p, &p, 0);
274                 channel = simple_strtoul(p + 1, &p, 0);
275                 id = simple_strtoul(p + 1, &p, 0);
276                 lun = simple_strtoul(p + 1, &p, 0);
277
278                 err = scsi_remove_single_device(host, channel, id, lun);
279         }
280
281  out:
282         free_page((unsigned long)buffer);
283         return err;
284 }
285
286 static int proc_scsi_show(struct seq_file *s, void *p)
287 {
288         seq_printf(s, "Attached devices:\n");
289         bus_for_each_dev(&scsi_bus_type, NULL, s, proc_print_scsidevice);
290         return 0;
291 }
292
293 static int proc_scsi_open(struct inode *inode, struct file *file)
294 {
295         /*
296          * We don't really needs this for the write case but it doesn't
297          * harm either.
298          */
299         return single_open(file, proc_scsi_show, NULL);
300 }
301
302 static struct file_operations proc_scsi_operations = {
303         .open           = proc_scsi_open,
304         .read           = seq_read,
305         .write          = proc_scsi_write,
306         .llseek         = seq_lseek,
307         .release        = single_release,
308 };
309
310 int __init scsi_init_procfs(void)
311 {
312         struct proc_dir_entry *pde;
313
314         proc_scsi = proc_mkdir("scsi", 0);
315         if (!proc_scsi)
316                 goto err1;
317
318         pde = create_proc_entry("scsi/scsi", 0, NULL);
319         if (!pde)
320                 goto err2;
321         pde->proc_fops = &proc_scsi_operations;
322
323         return 0;
324
325 err2:
326         remove_proc_entry("scsi", 0);
327 err1:
328         return -ENOMEM;
329 }
330
331 void scsi_exit_procfs(void)
332 {
333         remove_proc_entry("scsi/scsi", 0);
334         remove_proc_entry("scsi", 0);
335 }