scsi_proc.c 12 KB

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