hosts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/string.h>
  27. #include <linux/mm.h>
  28. #include <linux/init.h>
  29. #include <linux/completion.h>
  30. #include <linux/transport_class.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport.h>
  34. #include "scsi_priv.h"
  35. #include "scsi_logging.h"
  36. static int scsi_host_next_hn; /* host_no for next new host */
  37. static void scsi_host_cls_release(struct class_device *class_dev)
  38. {
  39. put_device(&class_to_shost(class_dev)->shost_gendev);
  40. }
  41. static struct class shost_class = {
  42. .name = "scsi_host",
  43. .release = scsi_host_cls_release,
  44. };
  45. /**
  46. * scsi_host_cancel - cancel outstanding IO to this host
  47. * @shost: pointer to struct Scsi_Host
  48. * recovery: recovery requested to run.
  49. **/
  50. static void scsi_host_cancel(struct Scsi_Host *shost, int recovery)
  51. {
  52. struct scsi_device *sdev;
  53. set_bit(SHOST_CANCEL, &shost->shost_state);
  54. shost_for_each_device(sdev, shost) {
  55. scsi_device_cancel(sdev, recovery);
  56. }
  57. wait_event(shost->host_wait, (!test_bit(SHOST_RECOVERY,
  58. &shost->shost_state)));
  59. }
  60. /**
  61. * scsi_remove_host - remove a scsi host
  62. * @shost: a pointer to a scsi host to remove
  63. **/
  64. void scsi_remove_host(struct Scsi_Host *shost)
  65. {
  66. scsi_forget_host(shost);
  67. scsi_host_cancel(shost, 0);
  68. scsi_proc_host_rm(shost);
  69. set_bit(SHOST_DEL, &shost->shost_state);
  70. transport_unregister_device(&shost->shost_gendev);
  71. class_device_unregister(&shost->shost_classdev);
  72. device_del(&shost->shost_gendev);
  73. }
  74. EXPORT_SYMBOL(scsi_remove_host);
  75. /**
  76. * scsi_add_host - add a scsi host
  77. * @shost: scsi host pointer to add
  78. * @dev: a struct device of type scsi class
  79. *
  80. * Return value:
  81. * 0 on success / != 0 for error
  82. **/
  83. int scsi_add_host(struct Scsi_Host *shost, struct device *dev)
  84. {
  85. struct scsi_host_template *sht = shost->hostt;
  86. int error = -EINVAL;
  87. printk(KERN_INFO "scsi%d : %s\n", shost->host_no,
  88. sht->info ? sht->info(shost) : sht->name);
  89. if (!shost->can_queue) {
  90. printk(KERN_ERR "%s: can_queue = 0 no longer supported\n",
  91. sht->name);
  92. goto out;
  93. }
  94. if (!shost->shost_gendev.parent)
  95. shost->shost_gendev.parent = dev ? dev : &platform_bus;
  96. error = device_add(&shost->shost_gendev);
  97. if (error)
  98. goto out;
  99. set_bit(SHOST_ADD, &shost->shost_state);
  100. get_device(shost->shost_gendev.parent);
  101. error = class_device_add(&shost->shost_classdev);
  102. if (error)
  103. goto out_del_gendev;
  104. get_device(&shost->shost_gendev);
  105. if (shost->transportt->host_size &&
  106. (shost->shost_data = kmalloc(shost->transportt->host_size,
  107. GFP_KERNEL)) == NULL)
  108. goto out_del_classdev;
  109. if (shost->transportt->create_work_queue) {
  110. snprintf(shost->work_q_name, KOBJ_NAME_LEN, "scsi_wq_%d",
  111. shost->host_no);
  112. shost->work_q = create_singlethread_workqueue(
  113. shost->work_q_name);
  114. if (!shost->work_q)
  115. goto out_free_shost_data;
  116. }
  117. error = scsi_sysfs_add_host(shost);
  118. if (error)
  119. goto out_destroy_host;
  120. scsi_proc_host_add(shost);
  121. return error;
  122. out_destroy_host:
  123. if (shost->work_q)
  124. destroy_workqueue(shost->work_q);
  125. out_free_shost_data:
  126. kfree(shost->shost_data);
  127. out_del_classdev:
  128. class_device_del(&shost->shost_classdev);
  129. out_del_gendev:
  130. device_del(&shost->shost_gendev);
  131. out:
  132. return error;
  133. }
  134. EXPORT_SYMBOL(scsi_add_host);
  135. static void scsi_host_dev_release(struct device *dev)
  136. {
  137. struct Scsi_Host *shost = dev_to_shost(dev);
  138. struct device *parent = dev->parent;
  139. if (shost->ehandler) {
  140. DECLARE_COMPLETION(sem);
  141. shost->eh_notify = &sem;
  142. shost->eh_kill = 1;
  143. up(shost->eh_wait);
  144. wait_for_completion(&sem);
  145. shost->eh_notify = NULL;
  146. }
  147. if (shost->work_q)
  148. destroy_workqueue(shost->work_q);
  149. scsi_proc_hostdir_rm(shost->hostt);
  150. scsi_destroy_command_freelist(shost);
  151. kfree(shost->shost_data);
  152. if (parent)
  153. put_device(parent);
  154. kfree(shost);
  155. }
  156. /**
  157. * scsi_host_alloc - register a scsi host adapter instance.
  158. * @sht: pointer to scsi host template
  159. * @privsize: extra bytes to allocate for driver
  160. *
  161. * Note:
  162. * Allocate a new Scsi_Host and perform basic initialization.
  163. * The host is not published to the scsi midlayer until scsi_add_host
  164. * is called.
  165. *
  166. * Return value:
  167. * Pointer to a new Scsi_Host
  168. **/
  169. struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
  170. {
  171. struct Scsi_Host *shost;
  172. int gfp_mask = GFP_KERNEL, rval;
  173. DECLARE_COMPLETION(complete);
  174. if (sht->unchecked_isa_dma && privsize)
  175. gfp_mask |= __GFP_DMA;
  176. /* Check to see if this host has any error handling facilities */
  177. if (!sht->eh_strategy_handler && !sht->eh_abort_handler &&
  178. !sht->eh_device_reset_handler && !sht->eh_bus_reset_handler &&
  179. !sht->eh_host_reset_handler) {
  180. printk(KERN_ERR "ERROR: SCSI host `%s' has no error handling\n"
  181. "ERROR: This is not a safe way to run your "
  182. "SCSI host\n"
  183. "ERROR: The error handling must be added to "
  184. "this driver\n", sht->proc_name);
  185. dump_stack();
  186. }
  187. shost = kmalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
  188. if (!shost)
  189. return NULL;
  190. memset(shost, 0, sizeof(struct Scsi_Host) + privsize);
  191. spin_lock_init(&shost->default_lock);
  192. scsi_assign_lock(shost, &shost->default_lock);
  193. INIT_LIST_HEAD(&shost->__devices);
  194. INIT_LIST_HEAD(&shost->__targets);
  195. INIT_LIST_HEAD(&shost->eh_cmd_q);
  196. INIT_LIST_HEAD(&shost->starved_list);
  197. init_waitqueue_head(&shost->host_wait);
  198. init_MUTEX(&shost->scan_mutex);
  199. shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */
  200. shost->dma_channel = 0xff;
  201. /* These three are default values which can be overridden */
  202. shost->max_channel = 0;
  203. shost->max_id = 8;
  204. shost->max_lun = 8;
  205. /* Give each shost a default transportt */
  206. shost->transportt = &blank_transport_template;
  207. /*
  208. * All drivers right now should be able to handle 12 byte
  209. * commands. Every so often there are requests for 16 byte
  210. * commands, but individual low-level drivers need to certify that
  211. * they actually do something sensible with such commands.
  212. */
  213. shost->max_cmd_len = 12;
  214. shost->hostt = sht;
  215. shost->this_id = sht->this_id;
  216. shost->can_queue = sht->can_queue;
  217. shost->sg_tablesize = sht->sg_tablesize;
  218. shost->cmd_per_lun = sht->cmd_per_lun;
  219. shost->unchecked_isa_dma = sht->unchecked_isa_dma;
  220. shost->use_clustering = sht->use_clustering;
  221. shost->ordered_flush = sht->ordered_flush;
  222. shost->ordered_tag = sht->ordered_tag;
  223. /*
  224. * hosts/devices that do queueing must support ordered tags
  225. */
  226. if (shost->can_queue > 1 && shost->ordered_flush) {
  227. printk(KERN_ERR "scsi: ordered flushes don't support queueing\n");
  228. shost->ordered_flush = 0;
  229. }
  230. if (sht->max_host_blocked)
  231. shost->max_host_blocked = sht->max_host_blocked;
  232. else
  233. shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
  234. /*
  235. * If the driver imposes no hard sector transfer limit, start at
  236. * machine infinity initially.
  237. */
  238. if (sht->max_sectors)
  239. shost->max_sectors = sht->max_sectors;
  240. else
  241. shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
  242. /*
  243. * assume a 4GB boundary, if not set
  244. */
  245. if (sht->dma_boundary)
  246. shost->dma_boundary = sht->dma_boundary;
  247. else
  248. shost->dma_boundary = 0xffffffff;
  249. rval = scsi_setup_command_freelist(shost);
  250. if (rval)
  251. goto fail_kfree;
  252. device_initialize(&shost->shost_gendev);
  253. snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
  254. shost->host_no);
  255. shost->shost_gendev.release = scsi_host_dev_release;
  256. class_device_initialize(&shost->shost_classdev);
  257. shost->shost_classdev.dev = &shost->shost_gendev;
  258. shost->shost_classdev.class = &shost_class;
  259. snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
  260. shost->host_no);
  261. shost->eh_notify = &complete;
  262. rval = kernel_thread(scsi_error_handler, shost, 0);
  263. if (rval < 0)
  264. goto fail_destroy_freelist;
  265. wait_for_completion(&complete);
  266. shost->eh_notify = NULL;
  267. scsi_proc_hostdir_add(shost->hostt);
  268. return shost;
  269. fail_destroy_freelist:
  270. scsi_destroy_command_freelist(shost);
  271. fail_kfree:
  272. kfree(shost);
  273. return NULL;
  274. }
  275. EXPORT_SYMBOL(scsi_host_alloc);
  276. struct Scsi_Host *scsi_register(struct scsi_host_template *sht, int privsize)
  277. {
  278. struct Scsi_Host *shost = scsi_host_alloc(sht, privsize);
  279. if (!sht->detect) {
  280. printk(KERN_WARNING "scsi_register() called on new-style "
  281. "template for driver %s\n", sht->name);
  282. dump_stack();
  283. }
  284. if (shost)
  285. list_add_tail(&shost->sht_legacy_list, &sht->legacy_hosts);
  286. return shost;
  287. }
  288. EXPORT_SYMBOL(scsi_register);
  289. void scsi_unregister(struct Scsi_Host *shost)
  290. {
  291. list_del(&shost->sht_legacy_list);
  292. scsi_host_put(shost);
  293. }
  294. EXPORT_SYMBOL(scsi_unregister);
  295. /**
  296. * scsi_host_lookup - get a reference to a Scsi_Host by host no
  297. *
  298. * @hostnum: host number to locate
  299. *
  300. * Return value:
  301. * A pointer to located Scsi_Host or NULL.
  302. **/
  303. struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
  304. {
  305. struct class *class = &shost_class;
  306. struct class_device *cdev;
  307. struct Scsi_Host *shost = ERR_PTR(-ENXIO), *p;
  308. down_read(&class->subsys.rwsem);
  309. list_for_each_entry(cdev, &class->children, node) {
  310. p = class_to_shost(cdev);
  311. if (p->host_no == hostnum) {
  312. shost = scsi_host_get(p);
  313. break;
  314. }
  315. }
  316. up_read(&class->subsys.rwsem);
  317. return shost;
  318. }
  319. EXPORT_SYMBOL(scsi_host_lookup);
  320. /**
  321. * scsi_host_get - inc a Scsi_Host ref count
  322. * @shost: Pointer to Scsi_Host to inc.
  323. **/
  324. struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
  325. {
  326. if (test_bit(SHOST_DEL, &shost->shost_state) ||
  327. !get_device(&shost->shost_gendev))
  328. return NULL;
  329. return shost;
  330. }
  331. EXPORT_SYMBOL(scsi_host_get);
  332. /**
  333. * scsi_host_put - dec a Scsi_Host ref count
  334. * @shost: Pointer to Scsi_Host to dec.
  335. **/
  336. void scsi_host_put(struct Scsi_Host *shost)
  337. {
  338. put_device(&shost->shost_gendev);
  339. }
  340. EXPORT_SYMBOL(scsi_host_put);
  341. int scsi_init_hosts(void)
  342. {
  343. return class_register(&shost_class);
  344. }
  345. void scsi_exit_hosts(void)
  346. {
  347. class_unregister(&shost_class);
  348. }
  349. int scsi_is_host_device(const struct device *dev)
  350. {
  351. return dev->release == scsi_host_dev_release;
  352. }
  353. EXPORT_SYMBOL(scsi_is_host_device);
  354. /**
  355. * scsi_queue_work - Queue work to the Scsi_Host workqueue.
  356. * @shost: Pointer to Scsi_Host.
  357. * @work: Work to queue for execution.
  358. *
  359. * Return value:
  360. * 0 on success / != 0 for error
  361. **/
  362. int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  363. {
  364. if (unlikely(!shost->work_q)) {
  365. printk(KERN_ERR
  366. "ERROR: Scsi host '%s' attempted to queue scsi-work, "
  367. "when no workqueue created.\n", shost->hostt->name);
  368. dump_stack();
  369. return -EINVAL;
  370. }
  371. return queue_work(shost->work_q, work);
  372. }
  373. EXPORT_SYMBOL_GPL(scsi_queue_work);
  374. /**
  375. * scsi_flush_work - Flush a Scsi_Host's workqueue.
  376. * @shost: Pointer to Scsi_Host.
  377. **/
  378. void scsi_flush_work(struct Scsi_Host *shost)
  379. {
  380. if (!shost->work_q) {
  381. printk(KERN_ERR
  382. "ERROR: Scsi host '%s' attempted to flush scsi-work, "
  383. "when no workqueue created.\n", shost->hostt->name);
  384. dump_stack();
  385. return;
  386. }
  387. flush_workqueue(shost->work_q);
  388. }
  389. EXPORT_SYMBOL_GPL(scsi_flush_work);