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