scsi_proc.c 13 KB

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