hosts.c 14 KB

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