hosts.c 12 KB

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