hosts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 int scsi_host_next_hn; /* host_no for next new host */
  39. static void scsi_host_cls_release(struct class_device *class_dev)
  40. {
  41. put_device(&class_to_shost(class_dev)->shost_gendev);
  42. }
  43. static struct class shost_class = {
  44. .name = "scsi_host",
  45. .release = scsi_host_cls_release,
  46. };
  47. /**
  48. * scsi_host_set_state - Take the given host through the host
  49. * 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. mutex_unlock(&shost->scan_mutex);
  150. scsi_forget_host(shost);
  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. class_device_unregister(&shost->shost_classdev);
  158. device_del(&shost->shost_gendev);
  159. scsi_proc_hostdir_rm(shost->hostt);
  160. }
  161. EXPORT_SYMBOL(scsi_remove_host);
  162. /**
  163. * scsi_add_host - add a scsi host
  164. * @shost: scsi host pointer to add
  165. * @dev: a struct device of type scsi class
  166. *
  167. * Return value:
  168. * 0 on success / != 0 for error
  169. **/
  170. int scsi_add_host(struct Scsi_Host *shost, struct device *dev)
  171. {
  172. struct scsi_host_template *sht = shost->hostt;
  173. int error = -EINVAL;
  174. printk(KERN_INFO "scsi%d : %s\n", shost->host_no,
  175. sht->info ? sht->info(shost) : sht->name);
  176. if (!shost->can_queue) {
  177. printk(KERN_ERR "%s: can_queue = 0 no longer supported\n",
  178. sht->name);
  179. goto out;
  180. }
  181. if (!shost->shost_gendev.parent)
  182. shost->shost_gendev.parent = dev ? dev : &platform_bus;
  183. error = device_add(&shost->shost_gendev);
  184. if (error)
  185. goto out;
  186. scsi_host_set_state(shost, SHOST_RUNNING);
  187. get_device(shost->shost_gendev.parent);
  188. error = class_device_add(&shost->shost_classdev);
  189. if (error)
  190. goto out_del_gendev;
  191. get_device(&shost->shost_gendev);
  192. if (shost->transportt->host_size &&
  193. (shost->shost_data = kmalloc(shost->transportt->host_size,
  194. GFP_KERNEL)) == NULL)
  195. goto out_del_classdev;
  196. if (shost->transportt->create_work_queue) {
  197. snprintf(shost->work_q_name, KOBJ_NAME_LEN, "scsi_wq_%d",
  198. shost->host_no);
  199. shost->work_q = create_singlethread_workqueue(
  200. shost->work_q_name);
  201. if (!shost->work_q)
  202. goto out_free_shost_data;
  203. }
  204. error = scsi_sysfs_add_host(shost);
  205. if (error)
  206. goto out_destroy_host;
  207. scsi_proc_host_add(shost);
  208. return error;
  209. out_destroy_host:
  210. if (shost->work_q)
  211. destroy_workqueue(shost->work_q);
  212. out_free_shost_data:
  213. kfree(shost->shost_data);
  214. out_del_classdev:
  215. class_device_del(&shost->shost_classdev);
  216. out_del_gendev:
  217. device_del(&shost->shost_gendev);
  218. out:
  219. return error;
  220. }
  221. EXPORT_SYMBOL(scsi_add_host);
  222. static void scsi_host_dev_release(struct device *dev)
  223. {
  224. struct Scsi_Host *shost = dev_to_shost(dev);
  225. struct device *parent = dev->parent;
  226. if (shost->ehandler)
  227. kthread_stop(shost->ehandler);
  228. if (shost->work_q)
  229. destroy_workqueue(shost->work_q);
  230. scsi_destroy_command_freelist(shost);
  231. if (shost->bqt)
  232. blk_free_tags(shost->bqt);
  233. kfree(shost->shost_data);
  234. if (parent)
  235. put_device(parent);
  236. kfree(shost);
  237. }
  238. /**
  239. * scsi_host_alloc - register a scsi host adapter instance.
  240. * @sht: pointer to scsi host template
  241. * @privsize: extra bytes to allocate for driver
  242. *
  243. * Note:
  244. * Allocate a new Scsi_Host and perform basic initialization.
  245. * The host is not published to the scsi midlayer until scsi_add_host
  246. * is called.
  247. *
  248. * Return value:
  249. * Pointer to a new Scsi_Host
  250. **/
  251. struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
  252. {
  253. struct Scsi_Host *shost;
  254. gfp_t gfp_mask = GFP_KERNEL;
  255. int rval;
  256. if (sht->unchecked_isa_dma && privsize)
  257. gfp_mask |= __GFP_DMA;
  258. shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
  259. if (!shost)
  260. return NULL;
  261. spin_lock_init(&shost->default_lock);
  262. scsi_assign_lock(shost, &shost->default_lock);
  263. shost->shost_state = SHOST_CREATED;
  264. INIT_LIST_HEAD(&shost->__devices);
  265. INIT_LIST_HEAD(&shost->__targets);
  266. INIT_LIST_HEAD(&shost->eh_cmd_q);
  267. INIT_LIST_HEAD(&shost->starved_list);
  268. init_waitqueue_head(&shost->host_wait);
  269. mutex_init(&shost->scan_mutex);
  270. shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */
  271. shost->dma_channel = 0xff;
  272. /* These three are default values which can be overridden */
  273. shost->max_channel = 0;
  274. shost->max_id = 8;
  275. shost->max_lun = 8;
  276. /* Give each shost a default transportt */
  277. shost->transportt = &blank_transport_template;
  278. /*
  279. * All drivers right now should be able to handle 12 byte
  280. * commands. Every so often there are requests for 16 byte
  281. * commands, but individual low-level drivers need to certify that
  282. * they actually do something sensible with such commands.
  283. */
  284. shost->max_cmd_len = 12;
  285. shost->hostt = sht;
  286. shost->this_id = sht->this_id;
  287. shost->can_queue = sht->can_queue;
  288. shost->sg_tablesize = sht->sg_tablesize;
  289. shost->cmd_per_lun = sht->cmd_per_lun;
  290. shost->unchecked_isa_dma = sht->unchecked_isa_dma;
  291. shost->use_clustering = sht->use_clustering;
  292. shost->ordered_tag = sht->ordered_tag;
  293. if (sht->max_host_blocked)
  294. shost->max_host_blocked = sht->max_host_blocked;
  295. else
  296. shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
  297. /*
  298. * If the driver imposes no hard sector transfer limit, start at
  299. * machine infinity initially.
  300. */
  301. if (sht->max_sectors)
  302. shost->max_sectors = sht->max_sectors;
  303. else
  304. shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
  305. /*
  306. * assume a 4GB boundary, if not set
  307. */
  308. if (sht->dma_boundary)
  309. shost->dma_boundary = sht->dma_boundary;
  310. else
  311. shost->dma_boundary = 0xffffffff;
  312. rval = scsi_setup_command_freelist(shost);
  313. if (rval)
  314. goto fail_kfree;
  315. device_initialize(&shost->shost_gendev);
  316. snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
  317. shost->host_no);
  318. shost->shost_gendev.release = scsi_host_dev_release;
  319. class_device_initialize(&shost->shost_classdev);
  320. shost->shost_classdev.dev = &shost->shost_gendev;
  321. shost->shost_classdev.class = &shost_class;
  322. snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
  323. shost->host_no);
  324. shost->ehandler = kthread_run(scsi_error_handler, shost,
  325. "scsi_eh_%d", shost->host_no);
  326. if (IS_ERR(shost->ehandler)) {
  327. rval = PTR_ERR(shost->ehandler);
  328. goto fail_destroy_freelist;
  329. }
  330. scsi_proc_hostdir_add(shost->hostt);
  331. return shost;
  332. fail_destroy_freelist:
  333. scsi_destroy_command_freelist(shost);
  334. fail_kfree:
  335. kfree(shost);
  336. return NULL;
  337. }
  338. EXPORT_SYMBOL(scsi_host_alloc);
  339. struct Scsi_Host *scsi_register(struct scsi_host_template *sht, int privsize)
  340. {
  341. struct Scsi_Host *shost = scsi_host_alloc(sht, privsize);
  342. if (!sht->detect) {
  343. printk(KERN_WARNING "scsi_register() called on new-style "
  344. "template for driver %s\n", sht->name);
  345. dump_stack();
  346. }
  347. if (shost)
  348. list_add_tail(&shost->sht_legacy_list, &sht->legacy_hosts);
  349. return shost;
  350. }
  351. EXPORT_SYMBOL(scsi_register);
  352. void scsi_unregister(struct Scsi_Host *shost)
  353. {
  354. list_del(&shost->sht_legacy_list);
  355. scsi_host_put(shost);
  356. }
  357. EXPORT_SYMBOL(scsi_unregister);
  358. /**
  359. * scsi_host_lookup - get a reference to a Scsi_Host by host no
  360. *
  361. * @hostnum: host number to locate
  362. *
  363. * Return value:
  364. * A pointer to located Scsi_Host or NULL.
  365. **/
  366. struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
  367. {
  368. struct class *class = &shost_class;
  369. struct class_device *cdev;
  370. struct Scsi_Host *shost = ERR_PTR(-ENXIO), *p;
  371. down_read(&class->subsys.rwsem);
  372. list_for_each_entry(cdev, &class->children, node) {
  373. p = class_to_shost(cdev);
  374. if (p->host_no == hostnum) {
  375. shost = scsi_host_get(p);
  376. break;
  377. }
  378. }
  379. up_read(&class->subsys.rwsem);
  380. return shost;
  381. }
  382. EXPORT_SYMBOL(scsi_host_lookup);
  383. /**
  384. * scsi_host_get - inc a Scsi_Host ref count
  385. * @shost: Pointer to Scsi_Host to inc.
  386. **/
  387. struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
  388. {
  389. if ((shost->shost_state == SHOST_DEL) ||
  390. !get_device(&shost->shost_gendev))
  391. return NULL;
  392. return shost;
  393. }
  394. EXPORT_SYMBOL(scsi_host_get);
  395. /**
  396. * scsi_host_put - dec a Scsi_Host ref count
  397. * @shost: Pointer to Scsi_Host to dec.
  398. **/
  399. void scsi_host_put(struct Scsi_Host *shost)
  400. {
  401. put_device(&shost->shost_gendev);
  402. }
  403. EXPORT_SYMBOL(scsi_host_put);
  404. int scsi_init_hosts(void)
  405. {
  406. return class_register(&shost_class);
  407. }
  408. void scsi_exit_hosts(void)
  409. {
  410. class_unregister(&shost_class);
  411. }
  412. int scsi_is_host_device(const struct device *dev)
  413. {
  414. return dev->release == scsi_host_dev_release;
  415. }
  416. EXPORT_SYMBOL(scsi_is_host_device);
  417. /**
  418. * scsi_queue_work - Queue work to the Scsi_Host workqueue.
  419. * @shost: Pointer to Scsi_Host.
  420. * @work: Work to queue for execution.
  421. *
  422. * Return value:
  423. * 1 - work queued for execution
  424. * 0 - work is already queued
  425. * -EINVAL - work queue doesn't exist
  426. **/
  427. int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  428. {
  429. if (unlikely(!shost->work_q)) {
  430. printk(KERN_ERR
  431. "ERROR: Scsi host '%s' attempted to queue scsi-work, "
  432. "when no workqueue created.\n", shost->hostt->name);
  433. dump_stack();
  434. return -EINVAL;
  435. }
  436. return queue_work(shost->work_q, work);
  437. }
  438. EXPORT_SYMBOL_GPL(scsi_queue_work);
  439. /**
  440. * scsi_flush_work - Flush a Scsi_Host's workqueue.
  441. * @shost: Pointer to Scsi_Host.
  442. **/
  443. void scsi_flush_work(struct Scsi_Host *shost)
  444. {
  445. if (!shost->work_q) {
  446. printk(KERN_ERR
  447. "ERROR: Scsi host '%s' attempted to flush scsi-work, "
  448. "when no workqueue created.\n", shost->hostt->name);
  449. dump_stack();
  450. return;
  451. }
  452. flush_workqueue(shost->work_q);
  453. }
  454. EXPORT_SYMBOL_GPL(scsi_flush_work);