hosts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. /*
  153. * Some drivers (eg aha1542) do scsi_register()/scsi_unregister()
  154. * during probing without performing a scsi_set_device() in between.
  155. * In this case dev->parent is NULL.
  156. */
  157. if (parent)
  158. put_device(parent);
  159. kfree(shost);
  160. }
  161. /**
  162. * scsi_host_alloc - register a scsi host adapter instance.
  163. * @sht: pointer to scsi host template
  164. * @privsize: extra bytes to allocate for driver
  165. *
  166. * Note:
  167. * Allocate a new Scsi_Host and perform basic initialization.
  168. * The host is not published to the scsi midlayer until scsi_add_host
  169. * is called.
  170. *
  171. * Return value:
  172. * Pointer to a new Scsi_Host
  173. **/
  174. struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
  175. {
  176. struct Scsi_Host *shost;
  177. int gfp_mask = GFP_KERNEL, rval;
  178. DECLARE_COMPLETION(complete);
  179. if (sht->unchecked_isa_dma && privsize)
  180. gfp_mask |= __GFP_DMA;
  181. /* Check to see if this host has any error handling facilities */
  182. if (!sht->eh_strategy_handler && !sht->eh_abort_handler &&
  183. !sht->eh_device_reset_handler && !sht->eh_bus_reset_handler &&
  184. !sht->eh_host_reset_handler) {
  185. printk(KERN_ERR "ERROR: SCSI host `%s' has no error handling\n"
  186. "ERROR: This is not a safe way to run your "
  187. "SCSI host\n"
  188. "ERROR: The error handling must be added to "
  189. "this driver\n", sht->proc_name);
  190. dump_stack();
  191. }
  192. shost = kmalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
  193. if (!shost)
  194. return NULL;
  195. memset(shost, 0, sizeof(struct Scsi_Host) + privsize);
  196. spin_lock_init(&shost->default_lock);
  197. scsi_assign_lock(shost, &shost->default_lock);
  198. INIT_LIST_HEAD(&shost->__devices);
  199. INIT_LIST_HEAD(&shost->__targets);
  200. INIT_LIST_HEAD(&shost->eh_cmd_q);
  201. INIT_LIST_HEAD(&shost->starved_list);
  202. init_waitqueue_head(&shost->host_wait);
  203. init_MUTEX(&shost->scan_mutex);
  204. shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */
  205. shost->dma_channel = 0xff;
  206. /* These three are default values which can be overridden */
  207. shost->max_channel = 0;
  208. shost->max_id = 8;
  209. shost->max_lun = 8;
  210. /* Give each shost a default transportt */
  211. shost->transportt = &blank_transport_template;
  212. /*
  213. * All drivers right now should be able to handle 12 byte
  214. * commands. Every so often there are requests for 16 byte
  215. * commands, but individual low-level drivers need to certify that
  216. * they actually do something sensible with such commands.
  217. */
  218. shost->max_cmd_len = 12;
  219. shost->hostt = sht;
  220. shost->this_id = sht->this_id;
  221. shost->can_queue = sht->can_queue;
  222. shost->sg_tablesize = sht->sg_tablesize;
  223. shost->cmd_per_lun = sht->cmd_per_lun;
  224. shost->unchecked_isa_dma = sht->unchecked_isa_dma;
  225. shost->use_clustering = sht->use_clustering;
  226. shost->ordered_flush = sht->ordered_flush;
  227. shost->ordered_tag = sht->ordered_tag;
  228. /*
  229. * hosts/devices that do queueing must support ordered tags
  230. */
  231. if (shost->can_queue > 1 && shost->ordered_flush) {
  232. printk(KERN_ERR "scsi: ordered flushes don't support queueing\n");
  233. shost->ordered_flush = 0;
  234. }
  235. if (sht->max_host_blocked)
  236. shost->max_host_blocked = sht->max_host_blocked;
  237. else
  238. shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
  239. /*
  240. * If the driver imposes no hard sector transfer limit, start at
  241. * machine infinity initially.
  242. */
  243. if (sht->max_sectors)
  244. shost->max_sectors = sht->max_sectors;
  245. else
  246. shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
  247. /*
  248. * assume a 4GB boundary, if not set
  249. */
  250. if (sht->dma_boundary)
  251. shost->dma_boundary = sht->dma_boundary;
  252. else
  253. shost->dma_boundary = 0xffffffff;
  254. rval = scsi_setup_command_freelist(shost);
  255. if (rval)
  256. goto fail_kfree;
  257. device_initialize(&shost->shost_gendev);
  258. snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
  259. shost->host_no);
  260. shost->shost_gendev.release = scsi_host_dev_release;
  261. class_device_initialize(&shost->shost_classdev);
  262. shost->shost_classdev.dev = &shost->shost_gendev;
  263. shost->shost_classdev.class = &shost_class;
  264. snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
  265. shost->host_no);
  266. shost->eh_notify = &complete;
  267. rval = kernel_thread(scsi_error_handler, shost, 0);
  268. if (rval < 0)
  269. goto fail_destroy_freelist;
  270. wait_for_completion(&complete);
  271. shost->eh_notify = NULL;
  272. scsi_proc_hostdir_add(shost->hostt);
  273. return shost;
  274. fail_destroy_freelist:
  275. scsi_destroy_command_freelist(shost);
  276. fail_kfree:
  277. kfree(shost);
  278. return NULL;
  279. }
  280. EXPORT_SYMBOL(scsi_host_alloc);
  281. struct Scsi_Host *scsi_register(struct scsi_host_template *sht, int privsize)
  282. {
  283. struct Scsi_Host *shost = scsi_host_alloc(sht, privsize);
  284. if (!sht->detect) {
  285. printk(KERN_WARNING "scsi_register() called on new-style "
  286. "template for driver %s\n", sht->name);
  287. dump_stack();
  288. }
  289. if (shost)
  290. list_add_tail(&shost->sht_legacy_list, &sht->legacy_hosts);
  291. return shost;
  292. }
  293. EXPORT_SYMBOL(scsi_register);
  294. void scsi_unregister(struct Scsi_Host *shost)
  295. {
  296. list_del(&shost->sht_legacy_list);
  297. scsi_host_put(shost);
  298. }
  299. EXPORT_SYMBOL(scsi_unregister);
  300. /**
  301. * scsi_host_lookup - get a reference to a Scsi_Host by host no
  302. *
  303. * @hostnum: host number to locate
  304. *
  305. * Return value:
  306. * A pointer to located Scsi_Host or NULL.
  307. **/
  308. struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
  309. {
  310. struct class *class = &shost_class;
  311. struct class_device *cdev;
  312. struct Scsi_Host *shost = ERR_PTR(-ENXIO), *p;
  313. down_read(&class->subsys.rwsem);
  314. list_for_each_entry(cdev, &class->children, node) {
  315. p = class_to_shost(cdev);
  316. if (p->host_no == hostnum) {
  317. shost = scsi_host_get(p);
  318. break;
  319. }
  320. }
  321. up_read(&class->subsys.rwsem);
  322. return shost;
  323. }
  324. EXPORT_SYMBOL(scsi_host_lookup);
  325. /**
  326. * scsi_host_get - inc a Scsi_Host ref count
  327. * @shost: Pointer to Scsi_Host to inc.
  328. **/
  329. struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
  330. {
  331. if (test_bit(SHOST_DEL, &shost->shost_state) ||
  332. !get_device(&shost->shost_gendev))
  333. return NULL;
  334. return shost;
  335. }
  336. EXPORT_SYMBOL(scsi_host_get);
  337. /**
  338. * scsi_host_put - dec a Scsi_Host ref count
  339. * @shost: Pointer to Scsi_Host to dec.
  340. **/
  341. void scsi_host_put(struct Scsi_Host *shost)
  342. {
  343. put_device(&shost->shost_gendev);
  344. }
  345. EXPORT_SYMBOL(scsi_host_put);
  346. int scsi_init_hosts(void)
  347. {
  348. return class_register(&shost_class);
  349. }
  350. void scsi_exit_hosts(void)
  351. {
  352. class_unregister(&shost_class);
  353. }
  354. int scsi_is_host_device(const struct device *dev)
  355. {
  356. return dev->release == scsi_host_dev_release;
  357. }
  358. EXPORT_SYMBOL(scsi_is_host_device);
  359. /**
  360. * scsi_queue_work - Queue work to the Scsi_Host workqueue.
  361. * @shost: Pointer to Scsi_Host.
  362. * @work: Work to queue for execution.
  363. *
  364. * Return value:
  365. * 0 on success / != 0 for error
  366. **/
  367. int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  368. {
  369. if (unlikely(!shost->work_q)) {
  370. printk(KERN_ERR
  371. "ERROR: Scsi host '%s' attempted to queue scsi-work, "
  372. "when no workqueue created.\n", shost->hostt->name);
  373. dump_stack();
  374. return -EINVAL;
  375. }
  376. return queue_work(shost->work_q, work);
  377. }
  378. EXPORT_SYMBOL_GPL(scsi_queue_work);
  379. /**
  380. * scsi_flush_work - Flush a Scsi_Host's workqueue.
  381. * @shost: Pointer to Scsi_Host.
  382. **/
  383. void scsi_flush_work(struct Scsi_Host *shost)
  384. {
  385. if (!shost->work_q) {
  386. printk(KERN_ERR
  387. "ERROR: Scsi host '%s' attempted to flush scsi-work, "
  388. "when no workqueue created.\n", shost->hostt->name);
  389. dump_stack();
  390. return;
  391. }
  392. flush_workqueue(shost->work_q);
  393. }
  394. EXPORT_SYMBOL_GPL(scsi_flush_work);