2 * linux/drivers/scsi/scsi_proc.c
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.
9 * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de
10 * Version: 0.99.8 last change: 95/09/13
12 * generic command parser provided by:
13 * Andreas Heilwagen <crashcar@informatik.uni-koblenz.de>
15 * generic_proc_info() support of xxxx_info() by:
16 * Michael A. Griffith <grif@acm.org>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/string.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 <linux/mutex.h>
29 #include <asm/uaccess.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport.h>
36 #include "scsi_priv.h"
37 #include "scsi_logging.h"
40 /* 4K page size, but our output routines, use some slack for overruns */
41 #define PROC_BLOCK_SIZE (3*1024)
43 static struct proc_dir_entry
*proc_scsi
;
45 /* Protect sht->present and sht->proc_dir */
46 static DEFINE_MUTEX(global_host_template_mutex
);
48 static int proc_scsi_read(char *buffer
, char **start
, off_t offset
,
49 int length
, int *eof
, void *data
)
51 struct Scsi_Host
*shost
= data
;
54 n
= shost
->hostt
->proc_info(shost
, buffer
, start
, offset
, length
, 0);
60 static int proc_scsi_write_proc(struct file
*file
, const char __user
*buf
,
61 unsigned long count
, void *data
)
63 struct Scsi_Host
*shost
= data
;
64 ssize_t ret
= -ENOMEM
;
68 if (count
> PROC_BLOCK_SIZE
)
71 page
= (char *)__get_free_page(GFP_KERNEL
);
74 if (copy_from_user(page
, buf
, count
))
76 ret
= shost
->hostt
->proc_info(shost
, page
, &start
, 0, count
, 1);
79 free_page((unsigned long)page
);
83 void scsi_proc_hostdir_add(struct scsi_host_template
*sht
)
88 mutex_lock(&global_host_template_mutex
);
89 if (!sht
->present
++) {
90 sht
->proc_dir
= proc_mkdir(sht
->proc_name
, proc_scsi
);
92 printk(KERN_ERR
"%s: proc_mkdir failed for %s\n",
93 __FUNCTION__
, sht
->proc_name
);
95 sht
->proc_dir
->owner
= sht
->module
;
97 mutex_unlock(&global_host_template_mutex
);
100 void scsi_proc_hostdir_rm(struct scsi_host_template
*sht
)
105 mutex_lock(&global_host_template_mutex
);
106 if (!--sht
->present
&& sht
->proc_dir
) {
107 remove_proc_entry(sht
->proc_name
, proc_scsi
);
108 sht
->proc_dir
= NULL
;
110 mutex_unlock(&global_host_template_mutex
);
113 void scsi_proc_host_add(struct Scsi_Host
*shost
)
115 struct scsi_host_template
*sht
= shost
->hostt
;
116 struct proc_dir_entry
*p
;
122 sprintf(name
,"%d", shost
->host_no
);
123 p
= create_proc_read_entry(name
, S_IFREG
| S_IRUGO
| S_IWUSR
,
124 sht
->proc_dir
, proc_scsi_read
, shost
);
126 printk(KERN_ERR
"%s: Failed to register host %d in"
127 "%s\n", __FUNCTION__
, shost
->host_no
,
132 p
->write_proc
= proc_scsi_write_proc
;
133 p
->owner
= sht
->module
;
136 void scsi_proc_host_rm(struct Scsi_Host
*shost
)
140 if (!shost
->hostt
->proc_dir
)
143 sprintf(name
,"%d", shost
->host_no
);
144 remove_proc_entry(name
, shost
->hostt
->proc_dir
);
147 static int proc_print_scsidevice(struct device
*dev
, void *data
)
149 struct scsi_device
*sdev
= to_scsi_device(dev
);
150 struct seq_file
*s
= data
;
154 "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n Vendor: ",
155 sdev
->host
->host_no
, sdev
->channel
, sdev
->id
, sdev
->lun
);
156 for (i
= 0; i
< 8; i
++) {
157 if (sdev
->vendor
[i
] >= 0x20)
158 seq_printf(s
, "%c", sdev
->vendor
[i
]);
163 seq_printf(s
, " Model: ");
164 for (i
= 0; i
< 16; i
++) {
165 if (sdev
->model
[i
] >= 0x20)
166 seq_printf(s
, "%c", sdev
->model
[i
]);
171 seq_printf(s
, " Rev: ");
172 for (i
= 0; i
< 4; i
++) {
173 if (sdev
->rev
[i
] >= 0x20)
174 seq_printf(s
, "%c", sdev
->rev
[i
]);
181 seq_printf(s
, " Type: %s ",
182 sdev
->type
< MAX_SCSI_DEVICE_CODE
?
183 scsi_device_types
[(int) sdev
->type
] : "Unknown ");
184 seq_printf(s
, " ANSI"
185 " SCSI revision: %02x", (sdev
->scsi_level
- 1) ?
186 sdev
->scsi_level
- 1 : 1);
187 if (sdev
->scsi_level
== 2)
188 seq_printf(s
, " CCS\n");
195 static int scsi_add_single_device(uint host
, uint channel
, uint id
, uint lun
)
197 struct Scsi_Host
*shost
;
200 shost
= scsi_host_lookup(host
);
202 return PTR_ERR(shost
);
204 if (shost
->transportt
->user_scan
)
205 error
= shost
->transportt
->user_scan(shost
, channel
, id
, lun
);
207 error
= scsi_scan_host_selected(shost
, channel
, id
, lun
, 1);
208 scsi_host_put(shost
);
212 static int scsi_remove_single_device(uint host
, uint channel
, uint id
, uint lun
)
214 struct scsi_device
*sdev
;
215 struct Scsi_Host
*shost
;
218 shost
= scsi_host_lookup(host
);
220 return PTR_ERR(shost
);
221 sdev
= scsi_device_lookup(shost
, channel
, id
, lun
);
223 scsi_remove_device(sdev
);
224 scsi_device_put(sdev
);
228 scsi_host_put(shost
);
232 static ssize_t
proc_scsi_write(struct file
*file
, const char __user
*buf
,
233 size_t length
, loff_t
*ppos
)
235 int host
, channel
, id
, lun
;
239 if (!buf
|| length
> PAGE_SIZE
)
242 buffer
= (char *)__get_free_page(GFP_KERNEL
);
247 if (copy_from_user(buffer
, buf
, length
))
251 if (length
< PAGE_SIZE
)
252 buffer
[length
] = '\0';
253 else if (buffer
[PAGE_SIZE
-1])
257 * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
258 * with "0 1 2 3" replaced by your "Host Channel Id Lun".
260 if (!strncmp("scsi add-single-device", buffer
, 22)) {
263 host
= simple_strtoul(p
, &p
, 0);
264 channel
= simple_strtoul(p
+ 1, &p
, 0);
265 id
= simple_strtoul(p
+ 1, &p
, 0);
266 lun
= simple_strtoul(p
+ 1, &p
, 0);
268 err
= scsi_add_single_device(host
, channel
, id
, lun
);
273 * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
274 * with "0 1 2 3" replaced by your "Host Channel Id Lun".
276 } else if (!strncmp("scsi remove-single-device", buffer
, 25)) {
279 host
= simple_strtoul(p
, &p
, 0);
280 channel
= simple_strtoul(p
+ 1, &p
, 0);
281 id
= simple_strtoul(p
+ 1, &p
, 0);
282 lun
= simple_strtoul(p
+ 1, &p
, 0);
284 err
= scsi_remove_single_device(host
, channel
, id
, lun
);
288 free_page((unsigned long)buffer
);
292 static int proc_scsi_show(struct seq_file
*s
, void *p
)
294 seq_printf(s
, "Attached devices:\n");
295 bus_for_each_dev(&scsi_bus_type
, NULL
, s
, proc_print_scsidevice
);
299 static int proc_scsi_open(struct inode
*inode
, struct file
*file
)
302 * We don't really needs this for the write case but it doesn't
305 return single_open(file
, proc_scsi_show
, NULL
);
308 static struct file_operations proc_scsi_operations
= {
309 .open
= proc_scsi_open
,
311 .write
= proc_scsi_write
,
313 .release
= single_release
,
316 int __init
scsi_init_procfs(void)
318 struct proc_dir_entry
*pde
;
320 proc_scsi
= proc_mkdir("scsi", NULL
);
324 pde
= create_proc_entry("scsi/scsi", 0, NULL
);
327 pde
->proc_fops
= &proc_scsi_operations
;
332 remove_proc_entry("scsi", NULL
);
337 void scsi_exit_procfs(void)
339 remove_proc_entry("scsi/scsi", NULL
);
340 remove_proc_entry("scsi", NULL
);