scsi_proc.c 7.5 KB

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