scsi_proc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/errno.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/mutex.h>
  28. #include <asm/uaccess.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_transport.h>
  33. #include "scsi_priv.h"
  34. #include "scsi_logging.h"
  35. /* 4K page size, but our output routines, use some slack for overruns */
  36. #define PROC_BLOCK_SIZE (3*1024)
  37. static struct proc_dir_entry *proc_scsi;
  38. /* Protect sht->present and sht->proc_dir */
  39. static DEFINE_MUTEX(global_host_template_mutex);
  40. static int proc_scsi_read(char *buffer, char **start, off_t offset,
  41. int length, int *eof, void *data)
  42. {
  43. struct Scsi_Host *shost = data;
  44. int n;
  45. n = shost->hostt->proc_info(shost, buffer, start, offset, length, 0);
  46. *eof = (n < length);
  47. return n;
  48. }
  49. static int proc_scsi_write_proc(struct file *file, const char __user *buf,
  50. unsigned long count, void *data)
  51. {
  52. struct Scsi_Host *shost = data;
  53. ssize_t ret = -ENOMEM;
  54. char *page;
  55. char *start;
  56. if (count > PROC_BLOCK_SIZE)
  57. return -EOVERFLOW;
  58. page = (char *)__get_free_page(GFP_KERNEL);
  59. if (page) {
  60. ret = -EFAULT;
  61. if (copy_from_user(page, buf, count))
  62. goto out;
  63. ret = shost->hostt->proc_info(shost, page, &start, 0, count, 1);
  64. }
  65. out:
  66. free_page((unsigned long)page);
  67. return ret;
  68. }
  69. void scsi_proc_hostdir_add(struct scsi_host_template *sht)
  70. {
  71. if (!sht->proc_info)
  72. return;
  73. mutex_lock(&global_host_template_mutex);
  74. if (!sht->present++) {
  75. sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi);
  76. if (!sht->proc_dir)
  77. printk(KERN_ERR "%s: proc_mkdir failed for %s\n",
  78. __FUNCTION__, sht->proc_name);
  79. else
  80. sht->proc_dir->owner = sht->module;
  81. }
  82. mutex_unlock(&global_host_template_mutex);
  83. }
  84. void scsi_proc_hostdir_rm(struct scsi_host_template *sht)
  85. {
  86. if (!sht->proc_info)
  87. return;
  88. mutex_lock(&global_host_template_mutex);
  89. if (!--sht->present && sht->proc_dir) {
  90. remove_proc_entry(sht->proc_name, proc_scsi);
  91. sht->proc_dir = NULL;
  92. }
  93. mutex_unlock(&global_host_template_mutex);
  94. }
  95. void scsi_proc_host_add(struct Scsi_Host *shost)
  96. {
  97. struct scsi_host_template *sht = shost->hostt;
  98. struct proc_dir_entry *p;
  99. char name[10];
  100. if (!sht->proc_dir)
  101. return;
  102. sprintf(name,"%d", shost->host_no);
  103. p = create_proc_read_entry(name, S_IFREG | S_IRUGO | S_IWUSR,
  104. sht->proc_dir, proc_scsi_read, shost);
  105. if (!p) {
  106. printk(KERN_ERR "%s: Failed to register host %d in"
  107. "%s\n", __FUNCTION__, shost->host_no,
  108. sht->proc_name);
  109. return;
  110. }
  111. p->write_proc = proc_scsi_write_proc;
  112. p->owner = sht->module;
  113. }
  114. void scsi_proc_host_rm(struct Scsi_Host *shost)
  115. {
  116. char name[10];
  117. if (!shost->hostt->proc_dir)
  118. return;
  119. sprintf(name,"%d", shost->host_no);
  120. remove_proc_entry(name, shost->hostt->proc_dir);
  121. }
  122. static int proc_print_scsidevice(struct device *dev, void *data)
  123. {
  124. struct scsi_device *sdev = to_scsi_device(dev);
  125. struct seq_file *s = data;
  126. int i;
  127. seq_printf(s,
  128. "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n Vendor: ",
  129. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  130. for (i = 0; i < 8; i++) {
  131. if (sdev->vendor[i] >= 0x20)
  132. seq_printf(s, "%c", sdev->vendor[i]);
  133. else
  134. seq_printf(s, " ");
  135. }
  136. seq_printf(s, " Model: ");
  137. for (i = 0; i < 16; i++) {
  138. if (sdev->model[i] >= 0x20)
  139. seq_printf(s, "%c", sdev->model[i]);
  140. else
  141. seq_printf(s, " ");
  142. }
  143. seq_printf(s, " Rev: ");
  144. for (i = 0; i < 4; i++) {
  145. if (sdev->rev[i] >= 0x20)
  146. seq_printf(s, "%c", sdev->rev[i]);
  147. else
  148. seq_printf(s, " ");
  149. }
  150. seq_printf(s, "\n");
  151. seq_printf(s, " Type: %s ",
  152. sdev->type < MAX_SCSI_DEVICE_CODE ?
  153. scsi_device_types[(int) sdev->type] : "Unknown ");
  154. seq_printf(s, " ANSI"
  155. " SCSI revision: %02x", (sdev->scsi_level - 1) ?
  156. sdev->scsi_level - 1 : 1);
  157. if (sdev->scsi_level == 2)
  158. seq_printf(s, " CCS\n");
  159. else
  160. seq_printf(s, "\n");
  161. return 0;
  162. }
  163. static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
  164. {
  165. struct Scsi_Host *shost;
  166. int error = -ENXIO;
  167. shost = scsi_host_lookup(host);
  168. if (IS_ERR(shost))
  169. return PTR_ERR(shost);
  170. if (shost->transportt->user_scan)
  171. error = shost->transportt->user_scan(shost, channel, id, lun);
  172. else
  173. error = scsi_scan_host_selected(shost, channel, id, lun, 1);
  174. scsi_host_put(shost);
  175. return error;
  176. }
  177. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  178. {
  179. struct scsi_device *sdev;
  180. struct Scsi_Host *shost;
  181. int error = -ENXIO;
  182. shost = scsi_host_lookup(host);
  183. if (IS_ERR(shost))
  184. return PTR_ERR(shost);
  185. sdev = scsi_device_lookup(shost, channel, id, lun);
  186. if (sdev) {
  187. scsi_remove_device(sdev);
  188. scsi_device_put(sdev);
  189. error = 0;
  190. }
  191. scsi_host_put(shost);
  192. return error;
  193. }
  194. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  195. size_t length, loff_t *ppos)
  196. {
  197. int host, channel, id, lun;
  198. char *buffer, *p;
  199. int err;
  200. if (!buf || length > PAGE_SIZE)
  201. return -EINVAL;
  202. buffer = (char *)__get_free_page(GFP_KERNEL);
  203. if (!buffer)
  204. return -ENOMEM;
  205. err = -EFAULT;
  206. if (copy_from_user(buffer, buf, length))
  207. goto out;
  208. err = -EINVAL;
  209. if (length < PAGE_SIZE)
  210. buffer[length] = '\0';
  211. else if (buffer[PAGE_SIZE-1])
  212. goto out;
  213. /*
  214. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  215. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  216. */
  217. if (!strncmp("scsi add-single-device", buffer, 22)) {
  218. p = buffer + 23;
  219. host = simple_strtoul(p, &p, 0);
  220. channel = simple_strtoul(p + 1, &p, 0);
  221. id = simple_strtoul(p + 1, &p, 0);
  222. lun = simple_strtoul(p + 1, &p, 0);
  223. err = scsi_add_single_device(host, channel, id, lun);
  224. if (err >= 0)
  225. err = length;
  226. /*
  227. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  228. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  229. */
  230. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  231. p = buffer + 26;
  232. host = simple_strtoul(p, &p, 0);
  233. channel = simple_strtoul(p + 1, &p, 0);
  234. id = simple_strtoul(p + 1, &p, 0);
  235. lun = simple_strtoul(p + 1, &p, 0);
  236. err = scsi_remove_single_device(host, channel, id, lun);
  237. }
  238. out:
  239. free_page((unsigned long)buffer);
  240. return err;
  241. }
  242. static int proc_scsi_show(struct seq_file *s, void *p)
  243. {
  244. seq_printf(s, "Attached devices:\n");
  245. bus_for_each_dev(&scsi_bus_type, NULL, s, proc_print_scsidevice);
  246. return 0;
  247. }
  248. static int proc_scsi_open(struct inode *inode, struct file *file)
  249. {
  250. /*
  251. * We don't really needs this for the write case but it doesn't
  252. * harm either.
  253. */
  254. return single_open(file, proc_scsi_show, NULL);
  255. }
  256. static struct file_operations proc_scsi_operations = {
  257. .open = proc_scsi_open,
  258. .read = seq_read,
  259. .write = proc_scsi_write,
  260. .llseek = seq_lseek,
  261. .release = single_release,
  262. };
  263. int __init scsi_init_procfs(void)
  264. {
  265. struct proc_dir_entry *pde;
  266. proc_scsi = proc_mkdir("scsi", NULL);
  267. if (!proc_scsi)
  268. goto err1;
  269. pde = create_proc_entry("scsi/scsi", 0, NULL);
  270. if (!pde)
  271. goto err2;
  272. pde->proc_fops = &proc_scsi_operations;
  273. return 0;
  274. err2:
  275. remove_proc_entry("scsi", NULL);
  276. err1:
  277. return -ENOMEM;
  278. }
  279. void scsi_exit_procfs(void)
  280. {
  281. remove_proc_entry("scsi/scsi", NULL);
  282. remove_proc_entry("scsi", NULL);
  283. }