hosts.c 12 KB

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