scsi_proc.c 11 KB

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