scsi_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/slab.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/errno.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/mutex.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. __FUNCTION__, sht->proc_name);
  101. else
  102. sht->proc_dir->owner = sht->module;
  103. }
  104. mutex_unlock(&global_host_template_mutex);
  105. }
  106. /**
  107. * scsi_proc_hostdir_rm - remove directory in /proc for a scsi host
  108. * @sht: owner of directory
  109. */
  110. void scsi_proc_hostdir_rm(struct scsi_host_template *sht)
  111. {
  112. if (!sht->proc_info)
  113. return;
  114. mutex_lock(&global_host_template_mutex);
  115. if (!--sht->present && sht->proc_dir) {
  116. remove_proc_entry(sht->proc_name, proc_scsi);
  117. sht->proc_dir = NULL;
  118. }
  119. mutex_unlock(&global_host_template_mutex);
  120. }
  121. /**
  122. * scsi_proc_host_add - Add entry for this host to appropriate /proc dir
  123. * @shost: host to add
  124. */
  125. void scsi_proc_host_add(struct Scsi_Host *shost)
  126. {
  127. struct scsi_host_template *sht = shost->hostt;
  128. struct proc_dir_entry *p;
  129. char name[10];
  130. if (!sht->proc_dir)
  131. return;
  132. sprintf(name,"%d", shost->host_no);
  133. p = create_proc_read_entry(name, S_IFREG | S_IRUGO | S_IWUSR,
  134. sht->proc_dir, proc_scsi_read, shost);
  135. if (!p) {
  136. printk(KERN_ERR "%s: Failed to register host %d in"
  137. "%s\n", __FUNCTION__, shost->host_no,
  138. sht->proc_name);
  139. return;
  140. }
  141. p->write_proc = proc_scsi_write_proc;
  142. p->owner = sht->module;
  143. }
  144. /**
  145. * scsi_proc_host_rm - remove this host's entry from /proc
  146. * @shost: which host
  147. */
  148. void scsi_proc_host_rm(struct Scsi_Host *shost)
  149. {
  150. char name[10];
  151. if (!shost->hostt->proc_dir)
  152. return;
  153. sprintf(name,"%d", shost->host_no);
  154. remove_proc_entry(name, shost->hostt->proc_dir);
  155. }
  156. /**
  157. * proc_print_scsidevice - return data about this host
  158. * @dev: A scsi device
  159. * @data: &struct seq_file to output to.
  160. *
  161. * Description: prints Host, Channel, Id, Lun, Vendor, Model, Rev, Type,
  162. * and revision.
  163. */
  164. static int proc_print_scsidevice(struct device *dev, void *data)
  165. {
  166. struct scsi_device *sdev = to_scsi_device(dev);
  167. struct seq_file *s = data;
  168. int i;
  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. return 0;
  201. }
  202. /**
  203. * scsi_add_single_device - Respond to user request to probe for/add device
  204. * @host: user-supplied decimal integer
  205. * @channel: user-supplied decimal integer
  206. * @id: user-supplied decimal integer
  207. * @lun: user-supplied decimal integer
  208. *
  209. * Description: called by writing "scsi add-single-device" to /proc/scsi/scsi.
  210. *
  211. * does scsi_host_lookup() and either user_scan() if that transport
  212. * type supports it, or else scsi_scan_host_selected()
  213. *
  214. * Note: this seems to be aimed exclusively at SCSI parallel busses.
  215. */
  216. static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
  217. {
  218. struct Scsi_Host *shost;
  219. int error = -ENXIO;
  220. shost = scsi_host_lookup(host);
  221. if (IS_ERR(shost))
  222. return PTR_ERR(shost);
  223. if (shost->transportt->user_scan)
  224. error = shost->transportt->user_scan(shost, channel, id, lun);
  225. else
  226. error = scsi_scan_host_selected(shost, channel, id, lun, 1);
  227. scsi_host_put(shost);
  228. return error;
  229. }
  230. /**
  231. * scsi_remove_single_device - Respond to user request to remove a device
  232. * @host: user-supplied decimal integer
  233. * @channel: user-supplied decimal integer
  234. * @id: user-supplied decimal integer
  235. * @lun: user-supplied decimal integer
  236. *
  237. * Description: called by writing "scsi remove-single-device" to
  238. * /proc/scsi/scsi. Does a scsi_device_lookup() and scsi_remove_device()
  239. */
  240. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  241. {
  242. struct scsi_device *sdev;
  243. struct Scsi_Host *shost;
  244. int error = -ENXIO;
  245. shost = scsi_host_lookup(host);
  246. if (IS_ERR(shost))
  247. return PTR_ERR(shost);
  248. sdev = scsi_device_lookup(shost, channel, id, lun);
  249. if (sdev) {
  250. scsi_remove_device(sdev);
  251. scsi_device_put(sdev);
  252. error = 0;
  253. }
  254. scsi_host_put(shost);
  255. return error;
  256. }
  257. /**
  258. * proc_scsi_write - handle writes to /proc/scsi/scsi
  259. * @file: not used
  260. * @buf: buffer to write
  261. * @length: length of buf, at most PAGE_SIZE
  262. * @ppos: not used
  263. *
  264. * Description: this provides a legacy mechanism to add or remove devices by
  265. * Host, Channel, ID, and Lun. To use,
  266. * "echo 'scsi add-single-device 0 1 2 3' > /proc/scsi/scsi" or
  267. * "echo 'scsi remove-single-device 0 1 2 3' > /proc/scsi/scsi" with
  268. * "0 1 2 3" replaced by the Host, Channel, Id, and Lun.
  269. *
  270. * Note: this seems to be aimed at parallel SCSI. Most modern busses (USB,
  271. * SATA, Firewire, Fibre Channel, etc) dynamically assign these values to
  272. * provide a unique identifier and nothing more.
  273. */
  274. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  275. size_t length, loff_t *ppos)
  276. {
  277. int host, channel, id, lun;
  278. char *buffer, *p;
  279. int err;
  280. if (!buf || length > PAGE_SIZE)
  281. return -EINVAL;
  282. buffer = (char *)__get_free_page(GFP_KERNEL);
  283. if (!buffer)
  284. return -ENOMEM;
  285. err = -EFAULT;
  286. if (copy_from_user(buffer, buf, length))
  287. goto out;
  288. err = -EINVAL;
  289. if (length < PAGE_SIZE)
  290. buffer[length] = '\0';
  291. else if (buffer[PAGE_SIZE-1])
  292. goto out;
  293. /*
  294. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  295. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  296. */
  297. if (!strncmp("scsi add-single-device", buffer, 22)) {
  298. p = buffer + 23;
  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_add_single_device(host, channel, id, lun);
  304. /*
  305. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  306. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  307. */
  308. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  309. p = buffer + 26;
  310. host = simple_strtoul(p, &p, 0);
  311. channel = simple_strtoul(p + 1, &p, 0);
  312. id = simple_strtoul(p + 1, &p, 0);
  313. lun = simple_strtoul(p + 1, &p, 0);
  314. err = scsi_remove_single_device(host, channel, id, lun);
  315. }
  316. /*
  317. * convert success returns so that we return the
  318. * number of bytes consumed.
  319. */
  320. if (!err)
  321. err = length;
  322. out:
  323. free_page((unsigned long)buffer);
  324. return err;
  325. }
  326. /**
  327. * proc_scsi_show - show contents of /proc/scsi/scsi (attached devices)
  328. * @s: output goes here
  329. * @p: not used
  330. */
  331. static int proc_scsi_show(struct seq_file *s, void *p)
  332. {
  333. seq_printf(s, "Attached devices:\n");
  334. bus_for_each_dev(&scsi_bus_type, NULL, s, proc_print_scsidevice);
  335. return 0;
  336. }
  337. /**
  338. * proc_scsi_open - glue function
  339. * @inode: not used
  340. * @file: passed to single_open()
  341. *
  342. * Associates proc_scsi_show with this file
  343. */
  344. static int proc_scsi_open(struct inode *inode, struct file *file)
  345. {
  346. /*
  347. * We don't really need this for the write case but it doesn't
  348. * harm either.
  349. */
  350. return single_open(file, proc_scsi_show, NULL);
  351. }
  352. static const struct file_operations proc_scsi_operations = {
  353. .open = proc_scsi_open,
  354. .read = seq_read,
  355. .write = proc_scsi_write,
  356. .llseek = seq_lseek,
  357. .release = single_release,
  358. };
  359. /**
  360. * scsi_init_procfs - create scsi and scsi/scsi in procfs
  361. */
  362. int __init scsi_init_procfs(void)
  363. {
  364. struct proc_dir_entry *pde;
  365. proc_scsi = proc_mkdir("scsi", NULL);
  366. if (!proc_scsi)
  367. goto err1;
  368. pde = create_proc_entry("scsi/scsi", 0, NULL);
  369. if (!pde)
  370. goto err2;
  371. pde->proc_fops = &proc_scsi_operations;
  372. return 0;
  373. err2:
  374. remove_proc_entry("scsi", NULL);
  375. err1:
  376. return -ENOMEM;
  377. }
  378. /**
  379. * scsi_exit_procfs - Remove scsi/scsi and scsi from procfs
  380. */
  381. void scsi_exit_procfs(void)
  382. {
  383. remove_proc_entry("scsi/scsi", NULL);
  384. remove_proc_entry("scsi", NULL);
  385. }