scsi_proc.c 11 KB

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