scsi_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/drivers/scsi/scsi_proc.c
  4. *
  5. * The functions in this file provide an interface between
  6. * the PROC file system and the SCSI device drivers
  7. * It is mainly used for debugging, statistics and to pass
  8. * information directly to the lowlevel driver.
  9. *
  10. * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de
  11. * Version: 0.99.8 last change: 95/09/13
  12. *
  13. * generic command parser provided by:
  14. * Andreas Heilwagen <crashcar@informatik.uni-koblenz.de>
  15. *
  16. * generic_proc_info() support of xxxx_info() by:
  17. * Michael A. Griffith <grif@acm.org>
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.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 <linux/gfp.h>
  29. #include <linux/uaccess.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport.h>
  34. #include "scsi_priv.h"
  35. #include "scsi_logging.h"
  36. /* 4K page size, but our output routines, use some slack for overruns */
  37. #define PROC_BLOCK_SIZE (3*1024)
  38. static struct proc_dir_entry *proc_scsi;
  39. /* Protect sht->present and sht->proc_dir */
  40. static DEFINE_MUTEX(global_host_template_mutex);
  41. static ssize_t proc_scsi_host_write(struct file *file, const char __user *buf,
  42. size_t count, loff_t *ppos)
  43. {
  44. struct Scsi_Host *shost = PDE_DATA(file_inode(file));
  45. ssize_t ret = -ENOMEM;
  46. char *page;
  47. if (count > PROC_BLOCK_SIZE)
  48. return -EOVERFLOW;
  49. if (!shost->hostt->write_info)
  50. return -EINVAL;
  51. page = (char *)__get_free_page(GFP_KERNEL);
  52. if (page) {
  53. ret = -EFAULT;
  54. if (copy_from_user(page, buf, count))
  55. goto out;
  56. ret = shost->hostt->write_info(shost, page, count);
  57. }
  58. out:
  59. free_page((unsigned long)page);
  60. return ret;
  61. }
  62. static int proc_scsi_show(struct seq_file *m, void *v)
  63. {
  64. struct Scsi_Host *shost = m->private;
  65. return shost->hostt->show_info(m, shost);
  66. }
  67. static int proc_scsi_host_open(struct inode *inode, struct file *file)
  68. {
  69. return single_open_size(file, proc_scsi_show, PDE_DATA(inode),
  70. 4 * PAGE_SIZE);
  71. }
  72. static const struct proc_ops proc_scsi_ops = {
  73. .proc_open = proc_scsi_host_open,
  74. .proc_release = single_release,
  75. .proc_read = seq_read,
  76. .proc_lseek = seq_lseek,
  77. .proc_write = proc_scsi_host_write
  78. };
  79. /**
  80. * scsi_proc_hostdir_add - Create directory in /proc for a scsi host
  81. * @sht: owner of this directory
  82. *
  83. * Sets sht->proc_dir to the new directory.
  84. */
  85. void scsi_proc_hostdir_add(struct scsi_host_template *sht)
  86. {
  87. if (!sht->show_info)
  88. return;
  89. mutex_lock(&global_host_template_mutex);
  90. if (!sht->present++) {
  91. sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi);
  92. if (!sht->proc_dir)
  93. printk(KERN_ERR "%s: proc_mkdir failed for %s\n",
  94. __func__, sht->proc_name);
  95. }
  96. mutex_unlock(&global_host_template_mutex);
  97. }
  98. /**
  99. * scsi_proc_hostdir_rm - remove directory in /proc for a scsi host
  100. * @sht: owner of directory
  101. */
  102. void scsi_proc_hostdir_rm(struct scsi_host_template *sht)
  103. {
  104. if (!sht->show_info)
  105. return;
  106. mutex_lock(&global_host_template_mutex);
  107. if (!--sht->present && sht->proc_dir) {
  108. remove_proc_entry(sht->proc_name, proc_scsi);
  109. sht->proc_dir = NULL;
  110. }
  111. mutex_unlock(&global_host_template_mutex);
  112. }
  113. /**
  114. * scsi_proc_host_add - Add entry for this host to appropriate /proc dir
  115. * @shost: host to add
  116. */
  117. void scsi_proc_host_add(struct Scsi_Host *shost)
  118. {
  119. struct scsi_host_template *sht = shost->hostt;
  120. struct proc_dir_entry *p;
  121. char name[10];
  122. if (!sht->proc_dir)
  123. return;
  124. sprintf(name,"%d", shost->host_no);
  125. p = proc_create_data(name, S_IRUGO | S_IWUSR,
  126. sht->proc_dir, &proc_scsi_ops, shost);
  127. if (!p)
  128. printk(KERN_ERR "%s: Failed to register host %d in"
  129. "%s\n", __func__, shost->host_no,
  130. sht->proc_name);
  131. }
  132. /**
  133. * scsi_proc_host_rm - remove this host's entry from /proc
  134. * @shost: which host
  135. */
  136. void scsi_proc_host_rm(struct Scsi_Host *shost)
  137. {
  138. char name[10];
  139. if (!shost->hostt->proc_dir)
  140. return;
  141. sprintf(name,"%d", shost->host_no);
  142. remove_proc_entry(name, shost->hostt->proc_dir);
  143. }
  144. /**
  145. * proc_print_scsidevice - return data about this host
  146. * @dev: A scsi device
  147. * @data: &struct seq_file to output to.
  148. *
  149. * Description: prints Host, Channel, Id, Lun, Vendor, Model, Rev, Type,
  150. * and revision.
  151. */
  152. static int proc_print_scsidevice(struct device *dev, void *data)
  153. {
  154. struct scsi_device *sdev;
  155. struct seq_file *s = data;
  156. int i;
  157. if (!scsi_is_sdev_device(dev))
  158. goto out;
  159. sdev = to_scsi_device(dev);
  160. seq_printf(s,
  161. "Host: scsi%d Channel: %02d Id: %02d Lun: %02llu\n Vendor: ",
  162. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  163. for (i = 0; i < 8; i++) {
  164. if (sdev->vendor[i] >= 0x20)
  165. seq_putc(s, sdev->vendor[i]);
  166. else
  167. seq_putc(s, ' ');
  168. }
  169. seq_puts(s, " Model: ");
  170. for (i = 0; i < 16; i++) {
  171. if (sdev->model[i] >= 0x20)
  172. seq_putc(s, sdev->model[i]);
  173. else
  174. seq_putc(s, ' ');
  175. }
  176. seq_puts(s, " Rev: ");
  177. for (i = 0; i < 4; i++) {
  178. if (sdev->rev[i] >= 0x20)
  179. seq_putc(s, sdev->rev[i]);
  180. else
  181. seq_putc(s, ' ');
  182. }
  183. seq_putc(s, '\n');
  184. seq_printf(s, " Type: %s ", scsi_device_type(sdev->type));
  185. seq_printf(s, " ANSI SCSI revision: %02x",
  186. sdev->scsi_level - (sdev->scsi_level > 1));
  187. if (sdev->scsi_level == 2)
  188. seq_puts(s, " CCS\n");
  189. else
  190. seq_putc(s, '\n');
  191. out:
  192. return 0;
  193. }
  194. /**
  195. * scsi_add_single_device - Respond to user request to probe for/add device
  196. * @host: user-supplied decimal integer
  197. * @channel: user-supplied decimal integer
  198. * @id: user-supplied decimal integer
  199. * @lun: user-supplied decimal integer
  200. *
  201. * Description: called by writing "scsi add-single-device" to /proc/scsi/scsi.
  202. *
  203. * does scsi_host_lookup() and either user_scan() if that transport
  204. * type supports it, or else scsi_scan_host_selected()
  205. *
  206. * Note: this seems to be aimed exclusively at SCSI parallel busses.
  207. */
  208. static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
  209. {
  210. struct Scsi_Host *shost;
  211. int error = -ENXIO;
  212. shost = scsi_host_lookup(host);
  213. if (!shost)
  214. return error;
  215. if (shost->transportt->user_scan)
  216. error = shost->transportt->user_scan(shost, channel, id, lun);
  217. else
  218. error = scsi_scan_host_selected(shost, channel, id, lun,
  219. SCSI_SCAN_MANUAL);
  220. scsi_host_put(shost);
  221. return error;
  222. }
  223. /**
  224. * scsi_remove_single_device - Respond to user request to remove a device
  225. * @host: user-supplied decimal integer
  226. * @channel: user-supplied decimal integer
  227. * @id: user-supplied decimal integer
  228. * @lun: user-supplied decimal integer
  229. *
  230. * Description: called by writing "scsi remove-single-device" to
  231. * /proc/scsi/scsi. Does a scsi_device_lookup() and scsi_remove_device()
  232. */
  233. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  234. {
  235. struct scsi_device *sdev;
  236. struct Scsi_Host *shost;
  237. int error = -ENXIO;
  238. shost = scsi_host_lookup(host);
  239. if (!shost)
  240. return error;
  241. sdev = scsi_device_lookup(shost, channel, id, lun);
  242. if (sdev) {
  243. scsi_remove_device(sdev);
  244. scsi_device_put(sdev);
  245. error = 0;
  246. }
  247. scsi_host_put(shost);
  248. return error;
  249. }
  250. /**
  251. * proc_scsi_write - handle writes to /proc/scsi/scsi
  252. * @file: not used
  253. * @buf: buffer to write
  254. * @length: length of buf, at most PAGE_SIZE
  255. * @ppos: not used
  256. *
  257. * Description: this provides a legacy mechanism to add or remove devices by
  258. * Host, Channel, ID, and Lun. To use,
  259. * "echo 'scsi add-single-device 0 1 2 3' > /proc/scsi/scsi" or
  260. * "echo 'scsi remove-single-device 0 1 2 3' > /proc/scsi/scsi" with
  261. * "0 1 2 3" replaced by the Host, Channel, Id, and Lun.
  262. *
  263. * Note: this seems to be aimed at parallel SCSI. Most modern busses (USB,
  264. * SATA, Firewire, Fibre Channel, etc) dynamically assign these values to
  265. * provide a unique identifier and nothing more.
  266. */
  267. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  268. size_t length, loff_t *ppos)
  269. {
  270. int host, channel, id, lun;
  271. char *buffer, *p;
  272. int err;
  273. if (!buf || length > PAGE_SIZE)
  274. return -EINVAL;
  275. buffer = (char *)__get_free_page(GFP_KERNEL);
  276. if (!buffer)
  277. return -ENOMEM;
  278. err = -EFAULT;
  279. if (copy_from_user(buffer, buf, length))
  280. goto out;
  281. err = -EINVAL;
  282. if (length < PAGE_SIZE)
  283. buffer[length] = '\0';
  284. else if (buffer[PAGE_SIZE-1])
  285. goto out;
  286. /*
  287. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  288. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  289. */
  290. if (!strncmp("scsi add-single-device", buffer, 22)) {
  291. p = buffer + 23;
  292. host = simple_strtoul(p, &p, 0);
  293. channel = simple_strtoul(p + 1, &p, 0);
  294. id = simple_strtoul(p + 1, &p, 0);
  295. lun = simple_strtoul(p + 1, &p, 0);
  296. err = scsi_add_single_device(host, channel, id, lun);
  297. /*
  298. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  299. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  300. */
  301. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  302. p = buffer + 26;
  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_remove_single_device(host, channel, id, lun);
  308. }
  309. /*
  310. * convert success returns so that we return the
  311. * number of bytes consumed.
  312. */
  313. if (!err)
  314. err = length;
  315. out:
  316. free_page((unsigned long)buffer);
  317. return err;
  318. }
  319. static inline struct device *next_scsi_device(struct device *start)
  320. {
  321. struct device *next = bus_find_next_device(&scsi_bus_type, start);
  322. put_device(start);
  323. return next;
  324. }
  325. static void *scsi_seq_start(struct seq_file *sfile, loff_t *pos)
  326. {
  327. struct device *dev = NULL;
  328. loff_t n = *pos;
  329. while ((dev = next_scsi_device(dev))) {
  330. if (!n--)
  331. break;
  332. sfile->private++;
  333. }
  334. return dev;
  335. }
  336. static void *scsi_seq_next(struct seq_file *sfile, void *v, loff_t *pos)
  337. {
  338. (*pos)++;
  339. sfile->private++;
  340. return next_scsi_device(v);
  341. }
  342. static void scsi_seq_stop(struct seq_file *sfile, void *v)
  343. {
  344. put_device(v);
  345. }
  346. static int scsi_seq_show(struct seq_file *sfile, void *dev)
  347. {
  348. if (!sfile->private)
  349. seq_puts(sfile, "Attached devices:\n");
  350. return proc_print_scsidevice(dev, sfile);
  351. }
  352. static const struct seq_operations scsi_seq_ops = {
  353. .start = scsi_seq_start,
  354. .next = scsi_seq_next,
  355. .stop = scsi_seq_stop,
  356. .show = scsi_seq_show
  357. };
  358. /**
  359. * proc_scsi_open - glue function
  360. * @inode: not used
  361. * @file: passed to single_open()
  362. *
  363. * Associates proc_scsi_show with this file
  364. */
  365. static int proc_scsi_open(struct inode *inode, struct file *file)
  366. {
  367. /*
  368. * We don't really need this for the write case but it doesn't
  369. * harm either.
  370. */
  371. return seq_open(file, &scsi_seq_ops);
  372. }
  373. static const struct proc_ops scsi_scsi_proc_ops = {
  374. .proc_open = proc_scsi_open,
  375. .proc_read = seq_read,
  376. .proc_write = proc_scsi_write,
  377. .proc_lseek = seq_lseek,
  378. .proc_release = seq_release,
  379. };
  380. /**
  381. * scsi_init_procfs - create scsi and scsi/scsi in procfs
  382. */
  383. int __init scsi_init_procfs(void)
  384. {
  385. struct proc_dir_entry *pde;
  386. proc_scsi = proc_mkdir("scsi", NULL);
  387. if (!proc_scsi)
  388. goto err1;
  389. pde = proc_create("scsi/scsi", 0, NULL, &scsi_scsi_proc_ops);
  390. if (!pde)
  391. goto err2;
  392. return 0;
  393. err2:
  394. remove_proc_entry("scsi", NULL);
  395. err1:
  396. return -ENOMEM;
  397. }
  398. /**
  399. * scsi_exit_procfs - Remove scsi/scsi and scsi from procfs
  400. */
  401. void scsi_exit_procfs(void)
  402. {
  403. remove_proc_entry("scsi/scsi", NULL);
  404. remove_proc_entry("scsi", NULL);
  405. }