scsi_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/proc_fs.h>
  23. #include <linux/errno.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/mutex.h>
  27. #include <linux/gfp.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 ssize_t proc_scsi_host_write(struct file *file, const char __user *buf,
  41. size_t count, loff_t *ppos)
  42. {
  43. struct Scsi_Host *shost = PDE(file_inode(file))->data;
  44. ssize_t ret = -ENOMEM;
  45. char *page;
  46. if (count > PROC_BLOCK_SIZE)
  47. return -EOVERFLOW;
  48. if (!shost->hostt->write_info)
  49. return -EINVAL;
  50. page = (char *)__get_free_page(GFP_KERNEL);
  51. if (page) {
  52. ret = -EFAULT;
  53. if (copy_from_user(page, buf, count))
  54. goto out;
  55. ret = shost->hostt->write_info(shost, page, count);
  56. }
  57. out:
  58. free_page((unsigned long)page);
  59. return ret;
  60. }
  61. static int proc_scsi_show(struct seq_file *m, void *v)
  62. {
  63. struct Scsi_Host *shost = m->private;
  64. return shost->hostt->show_info(m, shost);
  65. }
  66. static int proc_scsi_host_open(struct inode *inode, struct file *file)
  67. {
  68. return single_open(file, proc_scsi_show, PDE(inode)->data);
  69. }
  70. static const struct file_operations proc_scsi_fops = {
  71. .open = proc_scsi_host_open,
  72. .read = seq_read,
  73. .llseek = seq_lseek,
  74. .write = proc_scsi_host_write
  75. };
  76. /**
  77. * scsi_proc_hostdir_add - Create directory in /proc for a scsi host
  78. * @sht: owner of this directory
  79. *
  80. * Sets sht->proc_dir to the new directory.
  81. */
  82. void scsi_proc_hostdir_add(struct scsi_host_template *sht)
  83. {
  84. if (!sht->show_info)
  85. return;
  86. mutex_lock(&global_host_template_mutex);
  87. if (!sht->present++) {
  88. sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi);
  89. if (!sht->proc_dir)
  90. printk(KERN_ERR "%s: proc_mkdir failed for %s\n",
  91. __func__, sht->proc_name);
  92. }
  93. mutex_unlock(&global_host_template_mutex);
  94. }
  95. /**
  96. * scsi_proc_hostdir_rm - remove directory in /proc for a scsi host
  97. * @sht: owner of directory
  98. */
  99. void scsi_proc_hostdir_rm(struct scsi_host_template *sht)
  100. {
  101. if (!sht->show_info)
  102. return;
  103. mutex_lock(&global_host_template_mutex);
  104. if (!--sht->present && sht->proc_dir) {
  105. remove_proc_entry(sht->proc_name, proc_scsi);
  106. sht->proc_dir = NULL;
  107. }
  108. mutex_unlock(&global_host_template_mutex);
  109. }
  110. /**
  111. * scsi_proc_host_add - Add entry for this host to appropriate /proc dir
  112. * @shost: host to add
  113. */
  114. void scsi_proc_host_add(struct Scsi_Host *shost)
  115. {
  116. struct scsi_host_template *sht = shost->hostt;
  117. struct proc_dir_entry *p;
  118. char name[10];
  119. if (!sht->proc_dir)
  120. return;
  121. sprintf(name,"%d", shost->host_no);
  122. p = proc_create_data(name, S_IRUGO | S_IWUSR,
  123. sht->proc_dir, &proc_scsi_fops, shost);
  124. if (!p)
  125. printk(KERN_ERR "%s: Failed to register host %d in"
  126. "%s\n", __func__, shost->host_no,
  127. sht->proc_name);
  128. }
  129. /**
  130. * scsi_proc_host_rm - remove this host's entry from /proc
  131. * @shost: which host
  132. */
  133. void scsi_proc_host_rm(struct Scsi_Host *shost)
  134. {
  135. char name[10];
  136. if (!shost->hostt->proc_dir)
  137. return;
  138. sprintf(name,"%d", shost->host_no);
  139. remove_proc_entry(name, shost->hostt->proc_dir);
  140. }
  141. /**
  142. * proc_print_scsidevice - return data about this host
  143. * @dev: A scsi device
  144. * @data: &struct seq_file to output to.
  145. *
  146. * Description: prints Host, Channel, Id, Lun, Vendor, Model, Rev, Type,
  147. * and revision.
  148. */
  149. static int proc_print_scsidevice(struct device *dev, void *data)
  150. {
  151. struct scsi_device *sdev;
  152. struct seq_file *s = data;
  153. int i;
  154. if (!scsi_is_sdev_device(dev))
  155. goto out;
  156. sdev = to_scsi_device(dev);
  157. seq_printf(s,
  158. "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n Vendor: ",
  159. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  160. for (i = 0; i < 8; i++) {
  161. if (sdev->vendor[i] >= 0x20)
  162. seq_printf(s, "%c", sdev->vendor[i]);
  163. else
  164. seq_printf(s, " ");
  165. }
  166. seq_printf(s, " Model: ");
  167. for (i = 0; i < 16; i++) {
  168. if (sdev->model[i] >= 0x20)
  169. seq_printf(s, "%c", sdev->model[i]);
  170. else
  171. seq_printf(s, " ");
  172. }
  173. seq_printf(s, " Rev: ");
  174. for (i = 0; i < 4; i++) {
  175. if (sdev->rev[i] >= 0x20)
  176. seq_printf(s, "%c", sdev->rev[i]);
  177. else
  178. seq_printf(s, " ");
  179. }
  180. seq_printf(s, "\n");
  181. seq_printf(s, " Type: %s ", scsi_device_type(sdev->type));
  182. seq_printf(s, " ANSI SCSI revision: %02x",
  183. sdev->scsi_level - (sdev->scsi_level > 1));
  184. if (sdev->scsi_level == 2)
  185. seq_printf(s, " CCS\n");
  186. else
  187. seq_printf(s, "\n");
  188. out:
  189. return 0;
  190. }
  191. /**
  192. * scsi_add_single_device - Respond to user request to probe for/add device
  193. * @host: user-supplied decimal integer
  194. * @channel: user-supplied decimal integer
  195. * @id: user-supplied decimal integer
  196. * @lun: user-supplied decimal integer
  197. *
  198. * Description: called by writing "scsi add-single-device" to /proc/scsi/scsi.
  199. *
  200. * does scsi_host_lookup() and either user_scan() if that transport
  201. * type supports it, or else scsi_scan_host_selected()
  202. *
  203. * Note: this seems to be aimed exclusively at SCSI parallel busses.
  204. */
  205. static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
  206. {
  207. struct Scsi_Host *shost;
  208. int error = -ENXIO;
  209. shost = scsi_host_lookup(host);
  210. if (!shost)
  211. return error;
  212. if (shost->transportt->user_scan)
  213. error = shost->transportt->user_scan(shost, channel, id, lun);
  214. else
  215. error = scsi_scan_host_selected(shost, channel, id, lun, 1);
  216. scsi_host_put(shost);
  217. return error;
  218. }
  219. /**
  220. * scsi_remove_single_device - Respond to user request to remove a device
  221. * @host: user-supplied decimal integer
  222. * @channel: user-supplied decimal integer
  223. * @id: user-supplied decimal integer
  224. * @lun: user-supplied decimal integer
  225. *
  226. * Description: called by writing "scsi remove-single-device" to
  227. * /proc/scsi/scsi. Does a scsi_device_lookup() and scsi_remove_device()
  228. */
  229. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  230. {
  231. struct scsi_device *sdev;
  232. struct Scsi_Host *shost;
  233. int error = -ENXIO;
  234. shost = scsi_host_lookup(host);
  235. if (!shost)
  236. return error;
  237. sdev = scsi_device_lookup(shost, channel, id, lun);
  238. if (sdev) {
  239. scsi_remove_device(sdev);
  240. scsi_device_put(sdev);
  241. error = 0;
  242. }
  243. scsi_host_put(shost);
  244. return error;
  245. }
  246. /**
  247. * proc_scsi_write - handle writes to /proc/scsi/scsi
  248. * @file: not used
  249. * @buf: buffer to write
  250. * @length: length of buf, at most PAGE_SIZE
  251. * @ppos: not used
  252. *
  253. * Description: this provides a legacy mechanism to add or remove devices by
  254. * Host, Channel, ID, and Lun. To use,
  255. * "echo 'scsi add-single-device 0 1 2 3' > /proc/scsi/scsi" or
  256. * "echo 'scsi remove-single-device 0 1 2 3' > /proc/scsi/scsi" with
  257. * "0 1 2 3" replaced by the Host, Channel, Id, and Lun.
  258. *
  259. * Note: this seems to be aimed at parallel SCSI. Most modern busses (USB,
  260. * SATA, Firewire, Fibre Channel, etc) dynamically assign these values to
  261. * provide a unique identifier and nothing more.
  262. */
  263. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  264. size_t length, loff_t *ppos)
  265. {
  266. int host, channel, id, lun;
  267. char *buffer, *p;
  268. int err;
  269. if (!buf || length > PAGE_SIZE)
  270. return -EINVAL;
  271. buffer = (char *)__get_free_page(GFP_KERNEL);
  272. if (!buffer)
  273. return -ENOMEM;
  274. err = -EFAULT;
  275. if (copy_from_user(buffer, buf, length))
  276. goto out;
  277. err = -EINVAL;
  278. if (length < PAGE_SIZE)
  279. buffer[length] = '\0';
  280. else if (buffer[PAGE_SIZE-1])
  281. goto out;
  282. /*
  283. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  284. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  285. */
  286. if (!strncmp("scsi add-single-device", buffer, 22)) {
  287. p = buffer + 23;
  288. host = simple_strtoul(p, &p, 0);
  289. channel = simple_strtoul(p + 1, &p, 0);
  290. id = simple_strtoul(p + 1, &p, 0);
  291. lun = simple_strtoul(p + 1, &p, 0);
  292. err = scsi_add_single_device(host, channel, id, lun);
  293. /*
  294. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  295. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  296. */
  297. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  298. p = buffer + 26;
  299. host = simple_strtoul(p, &p, 0);
  300. channel = simple_strtoul(p + 1, &p, 0);
  301. id = simple_strtoul(p + 1, &p, 0);
  302. lun = simple_strtoul(p + 1, &p, 0);
  303. err = scsi_remove_single_device(host, channel, id, lun);
  304. }
  305. /*
  306. * convert success returns so that we return the
  307. * number of bytes consumed.
  308. */
  309. if (!err)
  310. err = length;
  311. out:
  312. free_page((unsigned long)buffer);
  313. return err;
  314. }
  315. static int always_match(struct device *dev, void *data)
  316. {
  317. return 1;
  318. }
  319. static inline struct device *next_scsi_device(struct device *start)
  320. {
  321. struct device *next = bus_find_device(&scsi_bus_type, start, NULL,
  322. always_match);
  323. put_device(start);
  324. return next;
  325. }
  326. static void *scsi_seq_start(struct seq_file *sfile, loff_t *pos)
  327. {
  328. struct device *dev = NULL;
  329. loff_t n = *pos;
  330. while ((dev = next_scsi_device(dev))) {
  331. if (!n--)
  332. break;
  333. sfile->private++;
  334. }
  335. return dev;
  336. }
  337. static void *scsi_seq_next(struct seq_file *sfile, void *v, loff_t *pos)
  338. {
  339. (*pos)++;
  340. sfile->private++;
  341. return next_scsi_device(v);
  342. }
  343. static void scsi_seq_stop(struct seq_file *sfile, void *v)
  344. {
  345. put_device(v);
  346. }
  347. static int scsi_seq_show(struct seq_file *sfile, void *dev)
  348. {
  349. if (!sfile->private)
  350. seq_puts(sfile, "Attached devices:\n");
  351. return proc_print_scsidevice(dev, sfile);
  352. }
  353. static const struct seq_operations scsi_seq_ops = {
  354. .start = scsi_seq_start,
  355. .next = scsi_seq_next,
  356. .stop = scsi_seq_stop,
  357. .show = scsi_seq_show
  358. };
  359. /**
  360. * proc_scsi_open - glue function
  361. * @inode: not used
  362. * @file: passed to single_open()
  363. *
  364. * Associates proc_scsi_show with this file
  365. */
  366. static int proc_scsi_open(struct inode *inode, struct file *file)
  367. {
  368. /*
  369. * We don't really need this for the write case but it doesn't
  370. * harm either.
  371. */
  372. return seq_open(file, &scsi_seq_ops);
  373. }
  374. static const struct file_operations proc_scsi_operations = {
  375. .owner = THIS_MODULE,
  376. .open = proc_scsi_open,
  377. .read = seq_read,
  378. .write = proc_scsi_write,
  379. .llseek = seq_lseek,
  380. .release = seq_release,
  381. };
  382. /**
  383. * scsi_init_procfs - create scsi and scsi/scsi in procfs
  384. */
  385. int __init scsi_init_procfs(void)
  386. {
  387. struct proc_dir_entry *pde;
  388. proc_scsi = proc_mkdir("scsi", NULL);
  389. if (!proc_scsi)
  390. goto err1;
  391. pde = proc_create("scsi/scsi", 0, NULL, &proc_scsi_operations);
  392. if (!pde)
  393. goto err2;
  394. return 0;
  395. err2:
  396. remove_proc_entry("scsi", NULL);
  397. err1:
  398. return -ENOMEM;
  399. }
  400. /**
  401. * scsi_exit_procfs - Remove scsi/scsi and scsi from procfs
  402. */
  403. void scsi_exit_procfs(void)
  404. {
  405. remove_proc_entry("scsi/scsi", NULL);
  406. remove_proc_entry("scsi", NULL);
  407. }