scsi_proc.c 7.6 KB

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