Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/uaccess.h> |
| 30 | |
| 31 | #include <scsi/scsi.h> |
| 32 | #include <scsi/scsi_device.h> |
| 33 | #include <scsi/scsi_host.h> |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 34 | #include <scsi/scsi_transport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include "scsi_priv.h" |
| 37 | #include "scsi_logging.h" |
| 38 | |
| 39 | |
| 40 | /* 4K page size, but our output routines, use some slack for overruns */ |
| 41 | #define PROC_BLOCK_SIZE (3*1024) |
| 42 | |
| 43 | static struct proc_dir_entry *proc_scsi; |
| 44 | |
| 45 | /* Protect sht->present and sht->proc_dir */ |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 46 | static DEFINE_MUTEX(global_host_template_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | static int proc_scsi_read(char *buffer, char **start, off_t offset, |
| 49 | int length, int *eof, void *data) |
| 50 | { |
| 51 | struct Scsi_Host *shost = data; |
| 52 | int n; |
| 53 | |
| 54 | n = shost->hostt->proc_info(shost, buffer, start, offset, length, 0); |
| 55 | *eof = (n < length); |
| 56 | |
| 57 | return n; |
| 58 | } |
| 59 | |
| 60 | static int proc_scsi_write_proc(struct file *file, const char __user *buf, |
| 61 | unsigned long count, void *data) |
| 62 | { |
| 63 | struct Scsi_Host *shost = data; |
| 64 | ssize_t ret = -ENOMEM; |
| 65 | char *page; |
| 66 | char *start; |
| 67 | |
| 68 | if (count > PROC_BLOCK_SIZE) |
| 69 | return -EOVERFLOW; |
| 70 | |
| 71 | page = (char *)__get_free_page(GFP_KERNEL); |
| 72 | if (page) { |
| 73 | ret = -EFAULT; |
| 74 | if (copy_from_user(page, buf, count)) |
| 75 | goto out; |
| 76 | ret = shost->hostt->proc_info(shost, page, &start, 0, count, 1); |
| 77 | } |
| 78 | out: |
| 79 | free_page((unsigned long)page); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | void scsi_proc_hostdir_add(struct scsi_host_template *sht) |
| 84 | { |
| 85 | if (!sht->proc_info) |
| 86 | return; |
| 87 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 88 | mutex_lock(&global_host_template_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (!sht->present++) { |
| 90 | sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi); |
| 91 | if (!sht->proc_dir) |
| 92 | printk(KERN_ERR "%s: proc_mkdir failed for %s\n", |
| 93 | __FUNCTION__, sht->proc_name); |
| 94 | else |
| 95 | sht->proc_dir->owner = sht->module; |
| 96 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 97 | mutex_unlock(&global_host_template_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void scsi_proc_hostdir_rm(struct scsi_host_template *sht) |
| 101 | { |
| 102 | if (!sht->proc_info) |
| 103 | return; |
| 104 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 105 | mutex_lock(&global_host_template_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (!--sht->present && sht->proc_dir) { |
| 107 | remove_proc_entry(sht->proc_name, proc_scsi); |
| 108 | sht->proc_dir = NULL; |
| 109 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 110 | mutex_unlock(&global_host_template_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void scsi_proc_host_add(struct Scsi_Host *shost) |
| 114 | { |
| 115 | struct scsi_host_template *sht = shost->hostt; |
| 116 | struct proc_dir_entry *p; |
| 117 | char name[10]; |
| 118 | |
| 119 | if (!sht->proc_dir) |
| 120 | return; |
| 121 | |
| 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); |
| 125 | if (!p) { |
| 126 | printk(KERN_ERR "%s: Failed to register host %d in" |
| 127 | "%s\n", __FUNCTION__, shost->host_no, |
| 128 | sht->proc_name); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | p->write_proc = proc_scsi_write_proc; |
| 133 | p->owner = sht->module; |
| 134 | } |
| 135 | |
| 136 | void scsi_proc_host_rm(struct Scsi_Host *shost) |
| 137 | { |
| 138 | char name[10]; |
| 139 | |
| 140 | if (!shost->hostt->proc_dir) |
| 141 | return; |
| 142 | |
| 143 | sprintf(name,"%d", shost->host_no); |
| 144 | remove_proc_entry(name, shost->hostt->proc_dir); |
| 145 | } |
| 146 | |
| 147 | static int proc_print_scsidevice(struct device *dev, void *data) |
| 148 | { |
| 149 | struct scsi_device *sdev = to_scsi_device(dev); |
| 150 | struct seq_file *s = data; |
| 151 | int i; |
| 152 | |
| 153 | seq_printf(s, |
| 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]); |
| 159 | else |
| 160 | seq_printf(s, " "); |
| 161 | } |
| 162 | |
| 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]); |
| 167 | else |
| 168 | seq_printf(s, " "); |
| 169 | } |
| 170 | |
| 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]); |
| 175 | else |
| 176 | seq_printf(s, " "); |
| 177 | } |
| 178 | |
| 179 | seq_printf(s, "\n"); |
| 180 | |
Matthew Wilcox | 4ff3671 | 2006-07-04 12:15:20 -0600 | [diff] [blame] | 181 | seq_printf(s, " Type: %s ", scsi_device_type(sdev->type)); |
Alan Stern | 74feb53 | 2007-01-08 11:07:41 -0500 | [diff] [blame] | 182 | seq_printf(s, " ANSI SCSI revision: %02x", |
| 183 | sdev->scsi_level - (sdev->scsi_level > 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 201 | if (shost->transportt->user_scan) |
| 202 | error = shost->transportt->user_scan(shost, channel, id, lun); |
| 203 | else |
| 204 | error = scsi_scan_host_selected(shost, channel, id, lun, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | scsi_host_put(shost); |
| 206 | return error; |
| 207 | } |
| 208 | |
| 209 | static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun) |
| 210 | { |
| 211 | struct scsi_device *sdev; |
| 212 | struct Scsi_Host *shost; |
| 213 | int error = -ENXIO; |
| 214 | |
| 215 | shost = scsi_host_lookup(host); |
| 216 | if (IS_ERR(shost)) |
| 217 | return PTR_ERR(shost); |
| 218 | sdev = scsi_device_lookup(shost, channel, id, lun); |
| 219 | if (sdev) { |
| 220 | scsi_remove_device(sdev); |
| 221 | scsi_device_put(sdev); |
| 222 | error = 0; |
| 223 | } |
| 224 | |
| 225 | scsi_host_put(shost); |
| 226 | return error; |
| 227 | } |
| 228 | |
| 229 | static ssize_t proc_scsi_write(struct file *file, const char __user *buf, |
| 230 | size_t length, loff_t *ppos) |
| 231 | { |
| 232 | int host, channel, id, lun; |
| 233 | char *buffer, *p; |
| 234 | int err; |
| 235 | |
| 236 | if (!buf || length > PAGE_SIZE) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | buffer = (char *)__get_free_page(GFP_KERNEL); |
| 240 | if (!buffer) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | err = -EFAULT; |
| 244 | if (copy_from_user(buffer, buf, length)) |
| 245 | goto out; |
| 246 | |
| 247 | err = -EINVAL; |
| 248 | if (length < PAGE_SIZE) |
| 249 | buffer[length] = '\0'; |
| 250 | else if (buffer[PAGE_SIZE-1]) |
| 251 | goto out; |
| 252 | |
| 253 | /* |
| 254 | * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi |
| 255 | * with "0 1 2 3" replaced by your "Host Channel Id Lun". |
| 256 | */ |
| 257 | if (!strncmp("scsi add-single-device", buffer, 22)) { |
| 258 | p = buffer + 23; |
| 259 | |
| 260 | host = simple_strtoul(p, &p, 0); |
| 261 | channel = simple_strtoul(p + 1, &p, 0); |
| 262 | id = simple_strtoul(p + 1, &p, 0); |
| 263 | lun = simple_strtoul(p + 1, &p, 0); |
| 264 | |
| 265 | err = scsi_add_single_device(host, channel, id, lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi |
| 269 | * with "0 1 2 3" replaced by your "Host Channel Id Lun". |
| 270 | */ |
| 271 | } else if (!strncmp("scsi remove-single-device", buffer, 25)) { |
| 272 | p = buffer + 26; |
| 273 | |
| 274 | host = simple_strtoul(p, &p, 0); |
| 275 | channel = simple_strtoul(p + 1, &p, 0); |
| 276 | id = simple_strtoul(p + 1, &p, 0); |
| 277 | lun = simple_strtoul(p + 1, &p, 0); |
| 278 | |
| 279 | err = scsi_remove_single_device(host, channel, id, lun); |
| 280 | } |
| 281 | |
James Bottomley | 2ca48a1 | 2006-04-27 14:07:49 -0500 | [diff] [blame] | 282 | /* |
| 283 | * convert success returns so that we return the |
| 284 | * number of bytes consumed. |
| 285 | */ |
| 286 | if (!err) |
| 287 | err = length; |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | out: |
| 290 | free_page((unsigned long)buffer); |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | static int proc_scsi_show(struct seq_file *s, void *p) |
| 295 | { |
| 296 | seq_printf(s, "Attached devices:\n"); |
| 297 | bus_for_each_dev(&scsi_bus_type, NULL, s, proc_print_scsidevice); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int proc_scsi_open(struct inode *inode, struct file *file) |
| 302 | { |
| 303 | /* |
| 304 | * We don't really needs this for the write case but it doesn't |
| 305 | * harm either. |
| 306 | */ |
| 307 | return single_open(file, proc_scsi_show, NULL); |
| 308 | } |
| 309 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 310 | static const struct file_operations proc_scsi_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | .open = proc_scsi_open, |
| 312 | .read = seq_read, |
| 313 | .write = proc_scsi_write, |
| 314 | .llseek = seq_lseek, |
| 315 | .release = single_release, |
| 316 | }; |
| 317 | |
| 318 | int __init scsi_init_procfs(void) |
| 319 | { |
| 320 | struct proc_dir_entry *pde; |
| 321 | |
| 322 | proc_scsi = proc_mkdir("scsi", NULL); |
| 323 | if (!proc_scsi) |
| 324 | goto err1; |
| 325 | |
| 326 | pde = create_proc_entry("scsi/scsi", 0, NULL); |
| 327 | if (!pde) |
| 328 | goto err2; |
| 329 | pde->proc_fops = &proc_scsi_operations; |
| 330 | |
| 331 | return 0; |
| 332 | |
| 333 | err2: |
| 334 | remove_proc_entry("scsi", NULL); |
| 335 | err1: |
| 336 | return -ENOMEM; |
| 337 | } |
| 338 | |
| 339 | void scsi_exit_procfs(void) |
| 340 | { |
| 341 | remove_proc_entry("scsi/scsi", NULL); |
| 342 | remove_proc_entry("scsi", NULL); |
| 343 | } |