scsi_proc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 ", scsi_device_type(sdev->type));
  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. if (shost->transportt->user_scan)
  169. error = shost->transportt->user_scan(shost, channel, id, lun);
  170. else
  171. error = scsi_scan_host_selected(shost, channel, id, lun, 1);
  172. scsi_host_put(shost);
  173. return error;
  174. }
  175. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  176. {
  177. struct scsi_device *sdev;
  178. struct Scsi_Host *shost;
  179. int error = -ENXIO;
  180. shost = scsi_host_lookup(host);
  181. if (IS_ERR(shost))
  182. return PTR_ERR(shost);
  183. sdev = scsi_device_lookup(shost, channel, id, lun);
  184. if (sdev) {
  185. scsi_remove_device(sdev);
  186. scsi_device_put(sdev);
  187. error = 0;
  188. }
  189. scsi_host_put(shost);
  190. return error;
  191. }
  192. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  193. size_t length, loff_t *ppos)
  194. {
  195. int host, channel, id, lun;
  196. char *buffer, *p;
  197. int err;
  198. if (!buf || length > PAGE_SIZE)
  199. return -EINVAL;
  200. buffer = (char *)__get_free_page(GFP_KERNEL);
  201. if (!buffer)
  202. return -ENOMEM;
  203. err = -EFAULT;
  204. if (copy_from_user(buffer, buf, length))
  205. goto out;
  206. err = -EINVAL;
  207. if (length < PAGE_SIZE)
  208. buffer[length] = '\0';
  209. else if (buffer[PAGE_SIZE-1])
  210. goto out;
  211. /*
  212. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  213. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  214. */
  215. if (!strncmp("scsi add-single-device", buffer, 22)) {
  216. p = buffer + 23;
  217. host = simple_strtoul(p, &p, 0);
  218. channel = simple_strtoul(p + 1, &p, 0);
  219. id = simple_strtoul(p + 1, &p, 0);
  220. lun = simple_strtoul(p + 1, &p, 0);
  221. err = scsi_add_single_device(host, channel, id, lun);
  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. /*
  235. * convert success returns so that we return the
  236. * number of bytes consumed.
  237. */
  238. if (!err)
  239. err = length;
  240. out:
  241. free_page((unsigned long)buffer);
  242. return err;
  243. }
  244. static int proc_scsi_show(struct seq_file *s, void *p)
  245. {
  246. seq_printf(s, "Attached devices:\n");
  247. bus_for_each_dev(&scsi_bus_type, NULL, s, proc_print_scsidevice);
  248. return 0;
  249. }
  250. static int proc_scsi_open(struct inode *inode, struct file *file)
  251. {
  252. /*
  253. * We don't really needs this for the write case but it doesn't
  254. * harm either.
  255. */
  256. return single_open(file, proc_scsi_show, NULL);
  257. }
  258. static struct file_operations proc_scsi_operations = {
  259. .open = proc_scsi_open,
  260. .read = seq_read,
  261. .write = proc_scsi_write,
  262. .llseek = seq_lseek,
  263. .release = single_release,
  264. };
  265. int __init scsi_init_procfs(void)
  266. {
  267. struct proc_dir_entry *pde;
  268. proc_scsi = proc_mkdir("scsi", NULL);
  269. if (!proc_scsi)
  270. goto err1;
  271. pde = create_proc_entry("scsi/scsi", 0, NULL);
  272. if (!pde)
  273. goto err2;
  274. pde->proc_fops = &proc_scsi_operations;
  275. return 0;
  276. err2:
  277. remove_proc_entry("scsi", NULL);
  278. err1:
  279. return -ENOMEM;
  280. }
  281. void scsi_exit_procfs(void)
  282. {
  283. remove_proc_entry("scsi/scsi", NULL);
  284. remove_proc_entry("scsi", NULL);
  285. }