scsi_proc.c 7.7 KB

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