hosts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hosts.c Copyright (C) 1992 Drew Eckhardt
  4. * Copyright (C) 1993, 1994, 1995 Eric Youngdale
  5. * Copyright (C) 2002-2003 Christoph Hellwig
  6. *
  7. * mid to lowlevel SCSI driver interface
  8. * Initial versions: Drew Eckhardt
  9. * Subsequent revisions: Eric Youngdale
  10. *
  11. * <drew@colorado.edu>
  12. *
  13. * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
  14. * Added QLOGIC QLA1280 SCSI controller kernel host support.
  15. * August 4, 1999 Fred Lewis, Intel DuPont
  16. *
  17. * Updated to reflect the new initialization scheme for the higher
  18. * level of scsi drivers (sd/sr/st)
  19. * September 17, 2000 Torben Mathiasen <tmm@image.dk>
  20. *
  21. * Restructured scsi_host lists and associated functions.
  22. * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
  23. */
  24. #include <linux/module.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/kthread.h>
  29. #include <linux/string.h>
  30. #include <linux/mm.h>
  31. #include <linux/init.h>
  32. #include <linux/completion.h>
  33. #include <linux/transport_class.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/idr.h>
  37. #include <scsi/scsi_device.h>
  38. #include <scsi/scsi_host.h>
  39. #include <scsi/scsi_transport.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include "scsi_priv.h"
  42. #include "scsi_logging.h"
  43. static int shost_eh_deadline = -1;
  44. module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
  45. MODULE_PARM_DESC(eh_deadline,
  46. "SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
  47. static DEFINE_IDA(host_index_ida);
  48. static void scsi_host_cls_release(struct device *dev)
  49. {
  50. put_device(&class_to_shost(dev)->shost_gendev);
  51. }
  52. static struct class shost_class = {
  53. .name = "scsi_host",
  54. .dev_release = scsi_host_cls_release,
  55. .dev_groups = scsi_shost_groups,
  56. };
  57. /**
  58. * scsi_host_set_state - Take the given host through the host state model.
  59. * @shost: scsi host to change the state of.
  60. * @state: state to change to.
  61. *
  62. * Returns zero if unsuccessful or an error if the requested
  63. * transition is illegal.
  64. **/
  65. int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
  66. {
  67. enum scsi_host_state oldstate = shost->shost_state;
  68. if (state == oldstate)
  69. return 0;
  70. switch (state) {
  71. case SHOST_CREATED:
  72. /* There are no legal states that come back to
  73. * created. This is the manually initialised start
  74. * state */
  75. goto illegal;
  76. case SHOST_RUNNING:
  77. switch (oldstate) {
  78. case SHOST_CREATED:
  79. case SHOST_RECOVERY:
  80. break;
  81. default:
  82. goto illegal;
  83. }
  84. break;
  85. case SHOST_RECOVERY:
  86. switch (oldstate) {
  87. case SHOST_RUNNING:
  88. break;
  89. default:
  90. goto illegal;
  91. }
  92. break;
  93. case SHOST_CANCEL:
  94. switch (oldstate) {
  95. case SHOST_CREATED:
  96. case SHOST_RUNNING:
  97. case SHOST_CANCEL_RECOVERY:
  98. break;
  99. default:
  100. goto illegal;
  101. }
  102. break;
  103. case SHOST_DEL:
  104. switch (oldstate) {
  105. case SHOST_CANCEL:
  106. case SHOST_DEL_RECOVERY:
  107. break;
  108. default:
  109. goto illegal;
  110. }
  111. break;
  112. case SHOST_CANCEL_RECOVERY:
  113. switch (oldstate) {
  114. case SHOST_CANCEL:
  115. case SHOST_RECOVERY:
  116. break;
  117. default:
  118. goto illegal;
  119. }
  120. break;
  121. case SHOST_DEL_RECOVERY:
  122. switch (oldstate) {
  123. case SHOST_CANCEL_RECOVERY:
  124. break;
  125. default:
  126. goto illegal;
  127. }
  128. break;
  129. }
  130. shost->shost_state = state;
  131. return 0;
  132. illegal:
  133. SCSI_LOG_ERROR_RECOVERY(1,
  134. shost_printk(KERN_ERR, shost,
  135. "Illegal host state transition"
  136. "%s->%s\n",
  137. scsi_host_state_name(oldstate),
  138. scsi_host_state_name(state)));
  139. return -EINVAL;
  140. }
  141. /**
  142. * scsi_remove_host - remove a scsi host
  143. * @shost: a pointer to a scsi host to remove
  144. **/
  145. void scsi_remove_host(struct Scsi_Host *shost)
  146. {
  147. unsigned long flags;
  148. mutex_lock(&shost->scan_mutex);
  149. spin_lock_irqsave(shost->host_lock, flags);
  150. if (scsi_host_set_state(shost, SHOST_CANCEL))
  151. if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
  152. spin_unlock_irqrestore(shost->host_lock, flags);
  153. mutex_unlock(&shost->scan_mutex);
  154. return;
  155. }
  156. spin_unlock_irqrestore(shost->host_lock, flags);
  157. scsi_autopm_get_host(shost);
  158. flush_workqueue(shost->tmf_work_q);
  159. scsi_forget_host(shost);
  160. mutex_unlock(&shost->scan_mutex);
  161. scsi_proc_host_rm(shost);
  162. scsi_proc_hostdir_rm(shost->hostt);
  163. /*
  164. * New SCSI devices cannot be attached anymore because of the SCSI host
  165. * state so drop the tag set refcnt. Wait until the tag set refcnt drops
  166. * to zero because .exit_cmd_priv implementations may need the host
  167. * pointer.
  168. */
  169. kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
  170. wait_for_completion(&shost->tagset_freed);
  171. spin_lock_irqsave(shost->host_lock, flags);
  172. if (scsi_host_set_state(shost, SHOST_DEL))
  173. BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
  174. spin_unlock_irqrestore(shost->host_lock, flags);
  175. transport_unregister_device(&shost->shost_gendev);
  176. device_unregister(&shost->shost_dev);
  177. device_del(&shost->shost_gendev);
  178. }
  179. EXPORT_SYMBOL(scsi_remove_host);
  180. /**
  181. * scsi_add_host_with_dma - add a scsi host with dma device
  182. * @shost: scsi host pointer to add
  183. * @dev: a struct device of type scsi class
  184. * @dma_dev: dma device for the host
  185. *
  186. * Note: You rarely need to worry about this unless you're in a
  187. * virtualised host environments, so use the simpler scsi_add_host()
  188. * function instead.
  189. *
  190. * Return value:
  191. * 0 on success / != 0 for error
  192. **/
  193. int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
  194. struct device *dma_dev)
  195. {
  196. const struct scsi_host_template *sht = shost->hostt;
  197. int error = -EINVAL;
  198. shost_printk(KERN_INFO, shost, "%s\n",
  199. sht->info ? sht->info(shost) : sht->name);
  200. if (!shost->can_queue) {
  201. shost_printk(KERN_ERR, shost,
  202. "can_queue = 0 no longer supported\n");
  203. goto fail;
  204. }
  205. if (shost->nr_reserved_cmds && !sht->queue_reserved_command) {
  206. shost_printk(KERN_ERR, shost,
  207. "nr_reserved_cmds set but no method to queue\n");
  208. goto fail;
  209. }
  210. /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
  211. shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
  212. shost->can_queue);
  213. error = scsi_init_sense_cache(shost);
  214. if (error)
  215. goto fail;
  216. if (!shost->shost_gendev.parent)
  217. shost->shost_gendev.parent = dev ? dev : &platform_bus;
  218. if (!dma_dev)
  219. dma_dev = shost->shost_gendev.parent;
  220. shost->dma_dev = dma_dev;
  221. if (dma_dev->dma_mask) {
  222. shost->max_sectors = min_t(unsigned int, shost->max_sectors,
  223. dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT);
  224. }
  225. error = scsi_mq_setup_tags(shost);
  226. if (error)
  227. goto fail;
  228. kref_init(&shost->tagset_refcnt);
  229. init_completion(&shost->tagset_freed);
  230. /*
  231. * Increase usage count temporarily here so that calling
  232. * scsi_autopm_put_host() will trigger runtime idle if there is
  233. * nothing else preventing suspending the device.
  234. */
  235. pm_runtime_get_noresume(&shost->shost_gendev);
  236. pm_runtime_set_active(&shost->shost_gendev);
  237. pm_runtime_enable(&shost->shost_gendev);
  238. device_enable_async_suspend(&shost->shost_gendev);
  239. error = device_add(&shost->shost_gendev);
  240. if (error)
  241. goto out_disable_runtime_pm;
  242. scsi_host_set_state(shost, SHOST_RUNNING);
  243. get_device(shost->shost_gendev.parent);
  244. device_enable_async_suspend(&shost->shost_dev);
  245. get_device(&shost->shost_gendev);
  246. error = device_add(&shost->shost_dev);
  247. if (error)
  248. goto out_del_gendev;
  249. if (shost->transportt->host_size) {
  250. shost->shost_data = kzalloc(shost->transportt->host_size,
  251. GFP_KERNEL);
  252. if (shost->shost_data == NULL) {
  253. error = -ENOMEM;
  254. goto out_del_dev;
  255. }
  256. }
  257. if (shost->transportt->create_work_queue) {
  258. shost->work_q = alloc_workqueue(
  259. "scsi_wq_%d",
  260. WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  261. shost->host_no);
  262. if (!shost->work_q) {
  263. error = -EINVAL;
  264. goto out_del_dev;
  265. }
  266. }
  267. error = scsi_sysfs_add_host(shost);
  268. if (error)
  269. goto out_del_dev;
  270. if (shost->nr_reserved_cmds) {
  271. shost->pseudo_sdev = scsi_get_pseudo_sdev(shost);
  272. if (!shost->pseudo_sdev) {
  273. error = -ENOMEM;
  274. goto out_del_dev;
  275. }
  276. }
  277. scsi_proc_host_add(shost);
  278. scsi_autopm_put_host(shost);
  279. return error;
  280. /*
  281. * Any host allocation in this function will be freed in
  282. * scsi_host_dev_release().
  283. */
  284. out_del_dev:
  285. device_del(&shost->shost_dev);
  286. out_del_gendev:
  287. /*
  288. * Host state is SHOST_RUNNING so we have to explicitly release
  289. * ->shost_dev.
  290. */
  291. put_device(&shost->shost_dev);
  292. device_del(&shost->shost_gendev);
  293. out_disable_runtime_pm:
  294. device_disable_async_suspend(&shost->shost_gendev);
  295. pm_runtime_disable(&shost->shost_gendev);
  296. pm_runtime_set_suspended(&shost->shost_gendev);
  297. pm_runtime_put_noidle(&shost->shost_gendev);
  298. kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
  299. fail:
  300. return error;
  301. }
  302. EXPORT_SYMBOL(scsi_add_host_with_dma);
  303. static void scsi_host_dev_release(struct device *dev)
  304. {
  305. struct Scsi_Host *shost = dev_to_shost(dev);
  306. struct device *parent = dev->parent;
  307. /* Wait for functions invoked through call_rcu(&scmd->rcu, ...) */
  308. rcu_barrier();
  309. if (shost->tmf_work_q)
  310. destroy_workqueue(shost->tmf_work_q);
  311. if (shost->ehandler)
  312. kthread_stop(shost->ehandler);
  313. if (shost->work_q)
  314. destroy_workqueue(shost->work_q);
  315. if (shost->shost_state == SHOST_CREATED) {
  316. /*
  317. * Free the shost_dev device name and remove the proc host dir
  318. * here if scsi_host_{alloc,put}() have been called but neither
  319. * scsi_host_add() nor scsi_remove_host() has been called.
  320. * This avoids that the memory allocated for the shost_dev
  321. * name as well as the proc dir structure are leaked.
  322. */
  323. scsi_proc_hostdir_rm(shost->hostt);
  324. kfree(dev_name(&shost->shost_dev));
  325. }
  326. kfree(shost->shost_data);
  327. ida_free(&host_index_ida, shost->host_no);
  328. if (shost->shost_state != SHOST_CREATED)
  329. put_device(parent);
  330. kfree(shost);
  331. }
  332. static const struct device_type scsi_host_type = {
  333. .name = "scsi_host",
  334. .release = scsi_host_dev_release,
  335. };
  336. /**
  337. * scsi_host_alloc - register a scsi host adapter instance.
  338. * @sht: pointer to scsi host template
  339. * @privsize: extra bytes to allocate for driver
  340. *
  341. * Note:
  342. * Allocate a new Scsi_Host and perform basic initialization.
  343. * The host is not published to the scsi midlayer until scsi_add_host
  344. * is called.
  345. *
  346. * Return value:
  347. * Pointer to a new Scsi_Host
  348. **/
  349. struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int privsize)
  350. {
  351. struct Scsi_Host *shost;
  352. int index;
  353. shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL);
  354. if (!shost)
  355. return NULL;
  356. shost->host_lock = &shost->default_lock;
  357. spin_lock_init(shost->host_lock);
  358. shost->shost_state = SHOST_CREATED;
  359. INIT_LIST_HEAD(&shost->__devices);
  360. INIT_LIST_HEAD(&shost->__targets);
  361. INIT_LIST_HEAD(&shost->eh_abort_list);
  362. INIT_LIST_HEAD(&shost->eh_cmd_q);
  363. INIT_LIST_HEAD(&shost->starved_list);
  364. init_waitqueue_head(&shost->host_wait);
  365. mutex_init(&shost->scan_mutex);
  366. index = ida_alloc(&host_index_ida, GFP_KERNEL);
  367. if (index < 0) {
  368. kfree(shost);
  369. return NULL;
  370. }
  371. shost->host_no = index;
  372. shost->dma_channel = 0xff;
  373. /* These three are default values which can be overridden */
  374. shost->max_channel = 0;
  375. shost->max_id = 8;
  376. shost->max_lun = 8;
  377. /* Give each shost a default transportt */
  378. shost->transportt = &blank_transport_template;
  379. /*
  380. * All drivers right now should be able to handle 12 byte
  381. * commands. Every so often there are requests for 16 byte
  382. * commands, but individual low-level drivers need to certify that
  383. * they actually do something sensible with such commands.
  384. */
  385. shost->max_cmd_len = 12;
  386. shost->hostt = sht;
  387. shost->this_id = sht->this_id;
  388. shost->can_queue = sht->can_queue;
  389. shost->nr_reserved_cmds = sht->nr_reserved_cmds;
  390. shost->sg_tablesize = sht->sg_tablesize;
  391. shost->sg_prot_tablesize = sht->sg_prot_tablesize;
  392. shost->cmd_per_lun = sht->cmd_per_lun;
  393. shost->no_write_same = sht->no_write_same;
  394. shost->host_tagset = sht->host_tagset;
  395. shost->queuecommand_may_block = sht->queuecommand_may_block;
  396. if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
  397. shost->eh_deadline = -1;
  398. else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
  399. shost_printk(KERN_WARNING, shost,
  400. "eh_deadline %u too large, setting to %u\n",
  401. shost_eh_deadline, INT_MAX / HZ);
  402. shost->eh_deadline = INT_MAX;
  403. } else
  404. shost->eh_deadline = shost_eh_deadline * HZ;
  405. if (sht->supported_mode == MODE_UNKNOWN)
  406. /* means we didn't set it ... default to INITIATOR */
  407. shost->active_mode = MODE_INITIATOR;
  408. else
  409. shost->active_mode = sht->supported_mode;
  410. if (sht->max_host_blocked)
  411. shost->max_host_blocked = sht->max_host_blocked;
  412. else
  413. shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
  414. /*
  415. * If the driver imposes no hard sector transfer limit, start at
  416. * machine infinity initially.
  417. */
  418. if (sht->max_sectors)
  419. shost->max_sectors = sht->max_sectors;
  420. else
  421. shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
  422. shost->virt_boundary_mask = sht->virt_boundary_mask;
  423. if (shost->virt_boundary_mask) {
  424. WARN_ON_ONCE(sht->max_segment_size &&
  425. sht->max_segment_size != UINT_MAX);
  426. shost->max_segment_size = UINT_MAX;
  427. } else {
  428. if (sht->max_segment_size)
  429. shost->max_segment_size = sht->max_segment_size;
  430. else
  431. shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
  432. }
  433. /* 32-byte (dword) is a common minimum for HBAs. */
  434. if (sht->dma_alignment)
  435. shost->dma_alignment = sht->dma_alignment;
  436. else
  437. shost->dma_alignment = 3;
  438. /*
  439. * assume a 4GB boundary, if not set
  440. */
  441. if (sht->dma_boundary)
  442. shost->dma_boundary = sht->dma_boundary;
  443. else
  444. shost->dma_boundary = 0xffffffff;
  445. device_initialize(&shost->shost_gendev);
  446. dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
  447. shost->shost_gendev.bus = &scsi_bus_type;
  448. shost->shost_gendev.type = &scsi_host_type;
  449. scsi_enable_async_suspend(&shost->shost_gendev);
  450. device_initialize(&shost->shost_dev);
  451. shost->shost_dev.parent = &shost->shost_gendev;
  452. shost->shost_dev.class = &shost_class;
  453. dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
  454. shost->shost_dev.groups = sht->shost_groups;
  455. shost->ehandler = kthread_run(scsi_error_handler, shost,
  456. "scsi_eh_%d", shost->host_no);
  457. if (IS_ERR(shost->ehandler)) {
  458. shost_printk(KERN_WARNING, shost,
  459. "error handler thread failed to spawn, error = %ld\n",
  460. PTR_ERR(shost->ehandler));
  461. shost->ehandler = NULL;
  462. goto fail;
  463. }
  464. shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
  465. WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS,
  466. 1, shost->host_no);
  467. if (!shost->tmf_work_q) {
  468. shost_printk(KERN_WARNING, shost,
  469. "failed to create tmf workq\n");
  470. goto fail;
  471. }
  472. if (scsi_proc_hostdir_add(shost->hostt) < 0)
  473. goto fail;
  474. return shost;
  475. fail:
  476. /*
  477. * Host state is still SHOST_CREATED and that is enough to release
  478. * ->shost_gendev. scsi_host_dev_release() will free
  479. * dev_name(&shost->shost_dev).
  480. */
  481. put_device(&shost->shost_gendev);
  482. return NULL;
  483. }
  484. EXPORT_SYMBOL(scsi_host_alloc);
  485. static int __scsi_host_match(struct device *dev, const void *data)
  486. {
  487. struct Scsi_Host *p;
  488. const unsigned int *hostnum = data;
  489. p = class_to_shost(dev);
  490. return p->host_no == *hostnum;
  491. }
  492. /**
  493. * scsi_host_lookup - get a reference to a Scsi_Host by host no
  494. * @hostnum: host number to locate
  495. *
  496. * Return value:
  497. * A pointer to located Scsi_Host or NULL.
  498. *
  499. * The caller must do a scsi_host_put() to drop the reference
  500. * that scsi_host_get() took. The put_device() below dropped
  501. * the reference from class_find_device().
  502. **/
  503. struct Scsi_Host *scsi_host_lookup(unsigned int hostnum)
  504. {
  505. struct device *cdev;
  506. struct Scsi_Host *shost = NULL;
  507. cdev = class_find_device(&shost_class, NULL, &hostnum,
  508. __scsi_host_match);
  509. if (cdev) {
  510. shost = scsi_host_get(class_to_shost(cdev));
  511. put_device(cdev);
  512. }
  513. return shost;
  514. }
  515. EXPORT_SYMBOL(scsi_host_lookup);
  516. /**
  517. * scsi_host_get - inc a Scsi_Host ref count
  518. * @shost: Pointer to Scsi_Host to inc.
  519. **/
  520. struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
  521. {
  522. if ((shost->shost_state == SHOST_DEL) ||
  523. !get_device(&shost->shost_gendev))
  524. return NULL;
  525. return shost;
  526. }
  527. EXPORT_SYMBOL(scsi_host_get);
  528. static bool scsi_host_check_in_flight(struct request *rq, void *data)
  529. {
  530. int *count = data;
  531. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  532. if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
  533. (*count)++;
  534. return true;
  535. }
  536. /**
  537. * scsi_host_busy - Return the count of in-flight commands
  538. * @shost: Pointer to Scsi_Host
  539. **/
  540. int scsi_host_busy(struct Scsi_Host *shost)
  541. {
  542. int cnt = 0;
  543. blk_mq_tagset_busy_iter(&shost->tag_set,
  544. scsi_host_check_in_flight, &cnt);
  545. return cnt;
  546. }
  547. EXPORT_SYMBOL(scsi_host_busy);
  548. /**
  549. * scsi_host_put - dec a Scsi_Host ref count
  550. * @shost: Pointer to Scsi_Host to dec.
  551. **/
  552. void scsi_host_put(struct Scsi_Host *shost)
  553. {
  554. put_device(&shost->shost_gendev);
  555. }
  556. EXPORT_SYMBOL(scsi_host_put);
  557. int scsi_init_hosts(void)
  558. {
  559. return class_register(&shost_class);
  560. }
  561. void scsi_exit_hosts(void)
  562. {
  563. class_unregister(&shost_class);
  564. ida_destroy(&host_index_ida);
  565. }
  566. int scsi_is_host_device(const struct device *dev)
  567. {
  568. return dev->type == &scsi_host_type;
  569. }
  570. EXPORT_SYMBOL(scsi_is_host_device);
  571. /**
  572. * scsi_queue_work - Queue work to the Scsi_Host workqueue.
  573. * @shost: Pointer to Scsi_Host.
  574. * @work: Work to queue for execution.
  575. *
  576. * Return value:
  577. * 1 - work queued for execution
  578. * 0 - work is already queued
  579. * -EINVAL - work queue doesn't exist
  580. **/
  581. int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  582. {
  583. if (unlikely(!shost->work_q)) {
  584. shost_printk(KERN_ERR, shost,
  585. "ERROR: Scsi host '%s' attempted to queue scsi-work, "
  586. "when no workqueue created.\n", shost->hostt->name);
  587. dump_stack();
  588. return -EINVAL;
  589. }
  590. return queue_work(shost->work_q, work);
  591. }
  592. EXPORT_SYMBOL_GPL(scsi_queue_work);
  593. /**
  594. * scsi_flush_work - Flush a Scsi_Host's workqueue.
  595. * @shost: Pointer to Scsi_Host.
  596. **/
  597. void scsi_flush_work(struct Scsi_Host *shost)
  598. {
  599. if (!shost->work_q) {
  600. shost_printk(KERN_ERR, shost,
  601. "ERROR: Scsi host '%s' attempted to flush scsi-work, "
  602. "when no workqueue created.\n", shost->hostt->name);
  603. dump_stack();
  604. return;
  605. }
  606. flush_workqueue(shost->work_q);
  607. }
  608. EXPORT_SYMBOL_GPL(scsi_flush_work);
  609. static bool complete_all_cmds_iter(struct request *rq, void *data)
  610. {
  611. struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
  612. enum scsi_host_status status = *(enum scsi_host_status *)data;
  613. scsi_dma_unmap(scmd);
  614. scmd->result = 0;
  615. set_host_byte(scmd, status);
  616. scsi_done(scmd);
  617. return true;
  618. }
  619. /**
  620. * scsi_host_complete_all_commands - Terminate all running commands
  621. * @shost: Scsi Host on which commands should be terminated
  622. * @status: Status to be set for the terminated commands
  623. *
  624. * There is no protection against modification of the number
  625. * of outstanding commands. It is the responsibility of the
  626. * caller to ensure that concurrent I/O submission and/or
  627. * completion is stopped when calling this function.
  628. */
  629. void scsi_host_complete_all_commands(struct Scsi_Host *shost,
  630. enum scsi_host_status status)
  631. {
  632. blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter,
  633. &status);
  634. }
  635. EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands);
  636. struct scsi_host_busy_iter_data {
  637. bool (*fn)(struct scsi_cmnd *, void *);
  638. void *priv;
  639. };
  640. static bool __scsi_host_busy_iter_fn(struct request *req, void *priv)
  641. {
  642. struct scsi_host_busy_iter_data *iter_data = priv;
  643. struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req);
  644. return iter_data->fn(sc, iter_data->priv);
  645. }
  646. /**
  647. * scsi_host_busy_iter - Iterate over all busy commands
  648. * @shost: Pointer to Scsi_Host.
  649. * @fn: Function to call on each busy command
  650. * @priv: Data pointer passed to @fn
  651. *
  652. * If locking against concurrent command completions is required
  653. * ithas to be provided by the caller
  654. **/
  655. void scsi_host_busy_iter(struct Scsi_Host *shost,
  656. bool (*fn)(struct scsi_cmnd *, void *),
  657. void *priv)
  658. {
  659. struct scsi_host_busy_iter_data iter_data = {
  660. .fn = fn,
  661. .priv = priv,
  662. };
  663. blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn,
  664. &iter_data);
  665. }
  666. EXPORT_SYMBOL_GPL(scsi_host_busy_iter);