scsi_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. __func__, 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", __func__, 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;
  167. struct seq_file *s = data;
  168. int i;
  169. if (!scsi_is_sdev_device(dev))
  170. goto out;
  171. sdev = to_scsi_device(dev);
  172. seq_printf(s,
  173. "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n Vendor: ",
  174. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  175. for (i = 0; i < 8; i++) {
  176. if (sdev->vendor[i] >= 0x20)
  177. seq_printf(s, "%c", sdev->vendor[i]);
  178. else
  179. seq_printf(s, " ");
  180. }
  181. seq_printf(s, " Model: ");
  182. for (i = 0; i < 16; i++) {
  183. if (sdev->model[i] >= 0x20)
  184. seq_printf(s, "%c", sdev->model[i]);
  185. else
  186. seq_printf(s, " ");
  187. }
  188. seq_printf(s, " Rev: ");
  189. for (i = 0; i < 4; i++) {
  190. if (sdev->rev[i] >= 0x20)
  191. seq_printf(s, "%c", sdev->rev[i]);
  192. else
  193. seq_printf(s, " ");
  194. }
  195. seq_printf(s, "\n");
  196. seq_printf(s, " Type: %s ", scsi_device_type(sdev->type));
  197. seq_printf(s, " ANSI SCSI revision: %02x",
  198. sdev->scsi_level - (sdev->scsi_level > 1));
  199. if (sdev->scsi_level == 2)
  200. seq_printf(s, " CCS\n");
  201. else
  202. seq_printf(s, "\n");
  203. out:
  204. return 0;
  205. }
  206. /**
  207. * scsi_add_single_device - Respond to user request to probe for/add device
  208. * @host: user-supplied decimal integer
  209. * @channel: user-supplied decimal integer
  210. * @id: user-supplied decimal integer
  211. * @lun: user-supplied decimal integer
  212. *
  213. * Description: called by writing "scsi add-single-device" to /proc/scsi/scsi.
  214. *
  215. * does scsi_host_lookup() and either user_scan() if that transport
  216. * type supports it, or else scsi_scan_host_selected()
  217. *
  218. * Note: this seems to be aimed exclusively at SCSI parallel busses.
  219. */
  220. static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
  221. {
  222. struct Scsi_Host *shost;
  223. int error = -ENXIO;
  224. shost = scsi_host_lookup(host);
  225. if (!shost)
  226. return error;
  227. if (shost->transportt->user_scan)
  228. error = shost->transportt->user_scan(shost, channel, id, lun);
  229. else
  230. error = scsi_scan_host_selected(shost, channel, id, lun, 1);
  231. scsi_host_put(shost);
  232. return error;
  233. }
  234. /**
  235. * scsi_remove_single_device - Respond to user request to remove a device
  236. * @host: user-supplied decimal integer
  237. * @channel: user-supplied decimal integer
  238. * @id: user-supplied decimal integer
  239. * @lun: user-supplied decimal integer
  240. *
  241. * Description: called by writing "scsi remove-single-device" to
  242. * /proc/scsi/scsi. Does a scsi_device_lookup() and scsi_remove_device()
  243. */
  244. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  245. {
  246. struct scsi_device *sdev;
  247. struct Scsi_Host *shost;
  248. int error = -ENXIO;
  249. shost = scsi_host_lookup(host);
  250. if (!shost)
  251. return error;
  252. sdev = scsi_device_lookup(shost, channel, id, lun);
  253. if (sdev) {
  254. scsi_remove_device(sdev);
  255. scsi_device_put(sdev);
  256. error = 0;
  257. }
  258. scsi_host_put(shost);
  259. return error;
  260. }
  261. /**
  262. * proc_scsi_write - handle writes to /proc/scsi/scsi
  263. * @file: not used
  264. * @buf: buffer to write
  265. * @length: length of buf, at most PAGE_SIZE
  266. * @ppos: not used
  267. *
  268. * Description: this provides a legacy mechanism to add or remove devices by
  269. * Host, Channel, ID, and Lun. To use,
  270. * "echo 'scsi add-single-device 0 1 2 3' > /proc/scsi/scsi" or
  271. * "echo 'scsi remove-single-device 0 1 2 3' > /proc/scsi/scsi" with
  272. * "0 1 2 3" replaced by the Host, Channel, Id, and Lun.
  273. *
  274. * Note: this seems to be aimed at parallel SCSI. Most modern busses (USB,
  275. * SATA, Firewire, Fibre Channel, etc) dynamically assign these values to
  276. * provide a unique identifier and nothing more.
  277. */
  278. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  279. size_t length, loff_t *ppos)
  280. {
  281. int host, channel, id, lun;
  282. char *buffer, *p;
  283. int err;
  284. if (!buf || length > PAGE_SIZE)
  285. return -EINVAL;
  286. buffer = (char *)__get_free_page(GFP_KERNEL);
  287. if (!buffer)
  288. return -ENOMEM;
  289. err = -EFAULT;
  290. if (copy_from_user(buffer, buf, length))
  291. goto out;
  292. err = -EINVAL;
  293. if (length < PAGE_SIZE)
  294. buffer[length] = '\0';
  295. else if (buffer[PAGE_SIZE-1])
  296. goto out;
  297. /*
  298. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  299. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  300. */
  301. if (!strncmp("scsi add-single-device", buffer, 22)) {
  302. p = buffer + 23;
  303. host = simple_strtoul(p, &p, 0);
  304. channel = simple_strtoul(p + 1, &p, 0);
  305. id = simple_strtoul(p + 1, &p, 0);
  306. lun = simple_strtoul(p + 1, &p, 0);
  307. err = scsi_add_single_device(host, channel, id, lun);
  308. /*
  309. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  310. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  311. */
  312. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  313. p = buffer + 26;
  314. host = simple_strtoul(p, &p, 0);
  315. channel = simple_strtoul(p + 1, &p, 0);
  316. id = simple_strtoul(p + 1, &p, 0);
  317. lun = simple_strtoul(p + 1, &p, 0);
  318. err = scsi_remove_single_device(host, channel, id, lun);
  319. }
  320. /*
  321. * convert success returns so that we return the
  322. * number of bytes consumed.
  323. */
  324. if (!err)
  325. err = length;
  326. out:
  327. free_page((unsigned long)buffer);
  328. return err;
  329. }
  330. /**
  331. * proc_scsi_show - show contents of /proc/scsi/scsi (attached devices)
  332. * @s: output goes here
  333. * @p: not used
  334. */
  335. static int proc_scsi_show(struct seq_file *s, void *p)
  336. {
  337. seq_printf(s, "Attached devices:\n");
  338. bus_for_each_dev(&scsi_bus_type, NULL, s, proc_print_scsidevice);
  339. return 0;
  340. }
  341. /**
  342. * proc_scsi_open - glue function
  343. * @inode: not used
  344. * @file: passed to single_open()
  345. *
  346. * Associates proc_scsi_show with this file
  347. */
  348. static int proc_scsi_open(struct inode *inode, struct file *file)
  349. {
  350. /*
  351. * We don't really need this for the write case but it doesn't
  352. * harm either.
  353. */
  354. return single_open(file, proc_scsi_show, NULL);
  355. }
  356. static const struct file_operations proc_scsi_operations = {
  357. .owner = THIS_MODULE,
  358. .open = proc_scsi_open,
  359. .read = seq_read,
  360. .write = proc_scsi_write,
  361. .llseek = seq_lseek,
  362. .release = single_release,
  363. };
  364. /**
  365. * scsi_init_procfs - create scsi and scsi/scsi in procfs
  366. */
  367. int __init scsi_init_procfs(void)
  368. {
  369. struct proc_dir_entry *pde;
  370. proc_scsi = proc_mkdir("scsi", NULL);
  371. if (!proc_scsi)
  372. goto err1;
  373. pde = proc_create("scsi/scsi", 0, NULL, &proc_scsi_operations);
  374. if (!pde)
  375. goto err2;
  376. return 0;
  377. err2:
  378. remove_proc_entry("scsi", NULL);
  379. err1:
  380. return -ENOMEM;
  381. }
  382. /**
  383. * scsi_exit_procfs - Remove scsi/scsi and scsi from procfs
  384. */
  385. void scsi_exit_procfs(void)
  386. {
  387. remove_proc_entry("scsi/scsi", NULL);
  388. remove_proc_entry("scsi", NULL);
  389. }