hosts.c 16 KB

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