hosts.c 16 KB

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