hosts.c 13 KB

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