hosts.c 14 KB

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