scsi_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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,
  218. SCSI_SCAN_MANUAL);
  219. scsi_host_put(shost);
  220. return error;
  221. }
  222. /**
  223. * scsi_remove_single_device - Respond to user request to remove a device
  224. * @host: user-supplied decimal integer
  225. * @channel: user-supplied decimal integer
  226. * @id: user-supplied decimal integer
  227. * @lun: user-supplied decimal integer
  228. *
  229. * Description: called by writing "scsi remove-single-device" to
  230. * /proc/scsi/scsi. Does a scsi_device_lookup() and scsi_remove_device()
  231. */
  232. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  233. {
  234. struct scsi_device *sdev;
  235. struct Scsi_Host *shost;
  236. int error = -ENXIO;
  237. shost = scsi_host_lookup(host);
  238. if (!shost)
  239. return error;
  240. sdev = scsi_device_lookup(shost, channel, id, lun);
  241. if (sdev) {
  242. scsi_remove_device(sdev);
  243. scsi_device_put(sdev);
  244. error = 0;
  245. }
  246. scsi_host_put(shost);
  247. return error;
  248. }
  249. /**
  250. * proc_scsi_write - handle writes to /proc/scsi/scsi
  251. * @file: not used
  252. * @buf: buffer to write
  253. * @length: length of buf, at most PAGE_SIZE
  254. * @ppos: not used
  255. *
  256. * Description: this provides a legacy mechanism to add or remove devices by
  257. * Host, Channel, ID, and Lun. To use,
  258. * "echo 'scsi add-single-device 0 1 2 3' > /proc/scsi/scsi" or
  259. * "echo 'scsi remove-single-device 0 1 2 3' > /proc/scsi/scsi" with
  260. * "0 1 2 3" replaced by the Host, Channel, Id, and Lun.
  261. *
  262. * Note: this seems to be aimed at parallel SCSI. Most modern busses (USB,
  263. * SATA, Firewire, Fibre Channel, etc) dynamically assign these values to
  264. * provide a unique identifier and nothing more.
  265. */
  266. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  267. size_t length, loff_t *ppos)
  268. {
  269. int host, channel, id, lun;
  270. char *buffer, *p;
  271. int err;
  272. if (!buf || length > PAGE_SIZE)
  273. return -EINVAL;
  274. buffer = (char *)__get_free_page(GFP_KERNEL);
  275. if (!buffer)
  276. return -ENOMEM;
  277. err = -EFAULT;
  278. if (copy_from_user(buffer, buf, length))
  279. goto out;
  280. err = -EINVAL;
  281. if (length < PAGE_SIZE)
  282. buffer[length] = '\0';
  283. else if (buffer[PAGE_SIZE-1])
  284. goto out;
  285. /*
  286. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  287. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  288. */
  289. if (!strncmp("scsi add-single-device", buffer, 22)) {
  290. p = buffer + 23;
  291. host = simple_strtoul(p, &p, 0);
  292. channel = simple_strtoul(p + 1, &p, 0);
  293. id = simple_strtoul(p + 1, &p, 0);
  294. lun = simple_strtoul(p + 1, &p, 0);
  295. err = scsi_add_single_device(host, channel, id, lun);
  296. /*
  297. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  298. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  299. */
  300. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  301. p = buffer + 26;
  302. host = simple_strtoul(p, &p, 0);
  303. channel = simple_strtoul(p + 1, &p, 0);
  304. id = simple_strtoul(p + 1, &p, 0);
  305. lun = simple_strtoul(p + 1, &p, 0);
  306. err = scsi_remove_single_device(host, channel, id, lun);
  307. }
  308. /*
  309. * convert success returns so that we return the
  310. * number of bytes consumed.
  311. */
  312. if (!err)
  313. err = length;
  314. out:
  315. free_page((unsigned long)buffer);
  316. return err;
  317. }
  318. static int always_match(struct device *dev, void *data)
  319. {
  320. return 1;
  321. }
  322. static inline struct device *next_scsi_device(struct device *start)
  323. {
  324. struct device *next = bus_find_device(&scsi_bus_type, start, NULL,
  325. always_match);
  326. put_device(start);
  327. return next;
  328. }
  329. static void *scsi_seq_start(struct seq_file *sfile, loff_t *pos)
  330. {
  331. struct device *dev = NULL;
  332. loff_t n = *pos;
  333. while ((dev = next_scsi_device(dev))) {
  334. if (!n--)
  335. break;
  336. sfile->private++;
  337. }
  338. return dev;
  339. }
  340. static void *scsi_seq_next(struct seq_file *sfile, void *v, loff_t *pos)
  341. {
  342. (*pos)++;
  343. sfile->private++;
  344. return next_scsi_device(v);
  345. }
  346. static void scsi_seq_stop(struct seq_file *sfile, void *v)
  347. {
  348. put_device(v);
  349. }
  350. static int scsi_seq_show(struct seq_file *sfile, void *dev)
  351. {
  352. if (!sfile->private)
  353. seq_puts(sfile, "Attached devices:\n");
  354. return proc_print_scsidevice(dev, sfile);
  355. }
  356. static const struct seq_operations scsi_seq_ops = {
  357. .start = scsi_seq_start,
  358. .next = scsi_seq_next,
  359. .stop = scsi_seq_stop,
  360. .show = scsi_seq_show
  361. };
  362. /**
  363. * proc_scsi_open - glue function
  364. * @inode: not used
  365. * @file: passed to single_open()
  366. *
  367. * Associates proc_scsi_show with this file
  368. */
  369. static int proc_scsi_open(struct inode *inode, struct file *file)
  370. {
  371. /*
  372. * We don't really need this for the write case but it doesn't
  373. * harm either.
  374. */
  375. return seq_open(file, &scsi_seq_ops);
  376. }
  377. static const struct file_operations proc_scsi_operations = {
  378. .owner = THIS_MODULE,
  379. .open = proc_scsi_open,
  380. .read = seq_read,
  381. .write = proc_scsi_write,
  382. .llseek = seq_lseek,
  383. .release = seq_release,
  384. };
  385. /**
  386. * scsi_init_procfs - create scsi and scsi/scsi in procfs
  387. */
  388. int __init scsi_init_procfs(void)
  389. {
  390. struct proc_dir_entry *pde;
  391. proc_scsi = proc_mkdir("scsi", NULL);
  392. if (!proc_scsi)
  393. goto err1;
  394. pde = proc_create("scsi/scsi", 0, NULL, &proc_scsi_operations);
  395. if (!pde)
  396. goto err2;
  397. return 0;
  398. err2:
  399. remove_proc_entry("scsi", NULL);
  400. err1:
  401. return -ENOMEM;
  402. }
  403. /**
  404. * scsi_exit_procfs - Remove scsi/scsi and scsi from procfs
  405. */
  406. void scsi_exit_procfs(void)
  407. {
  408. remove_proc_entry("scsi/scsi", NULL);
  409. remove_proc_entry("scsi", NULL);
  410. }