scsi_dh_hp_sw.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Basic HP/COMPAQ MSA 1000 support. This is only needed if your HW cannot be
  3. * upgraded.
  4. *
  5. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  6. * Copyright (C) 2006 Mike Christie
  7. * Copyright (C) 2008 Hannes Reinecke <hare@suse.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, write to
  21. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_dbg.h>
  27. #include <scsi/scsi_eh.h>
  28. #include <scsi/scsi_dh.h>
  29. #define HP_SW_NAME "hp_sw"
  30. #define HP_SW_TIMEOUT (60 * HZ)
  31. #define HP_SW_RETRIES 3
  32. #define HP_SW_PATH_UNINITIALIZED -1
  33. #define HP_SW_PATH_ACTIVE 0
  34. #define HP_SW_PATH_PASSIVE 1
  35. struct hp_sw_dh_data {
  36. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  37. int path_state;
  38. int retries;
  39. int retry_cnt;
  40. struct scsi_device *sdev;
  41. activate_complete callback_fn;
  42. void *callback_data;
  43. };
  44. static int hp_sw_start_stop(struct hp_sw_dh_data *);
  45. /*
  46. * tur_done - Handle TEST UNIT READY return status
  47. * @sdev: sdev the command has been sent to
  48. * @errors: blk error code
  49. *
  50. * Returns SCSI_DH_DEV_OFFLINED if the sdev is on the passive path
  51. */
  52. static int tur_done(struct scsi_device *sdev, unsigned char *sense)
  53. {
  54. struct scsi_sense_hdr sshdr;
  55. int ret;
  56. ret = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
  57. if (!ret) {
  58. sdev_printk(KERN_WARNING, sdev,
  59. "%s: sending tur failed, no sense available\n",
  60. HP_SW_NAME);
  61. ret = SCSI_DH_IO;
  62. goto done;
  63. }
  64. switch (sshdr.sense_key) {
  65. case UNIT_ATTENTION:
  66. ret = SCSI_DH_IMM_RETRY;
  67. break;
  68. case NOT_READY:
  69. if ((sshdr.asc == 0x04) && (sshdr.ascq == 2)) {
  70. /*
  71. * LUN not ready - Initialization command required
  72. *
  73. * This is the passive path
  74. */
  75. ret = SCSI_DH_DEV_OFFLINED;
  76. break;
  77. }
  78. /* Fallthrough */
  79. default:
  80. sdev_printk(KERN_WARNING, sdev,
  81. "%s: sending tur failed, sense %x/%x/%x\n",
  82. HP_SW_NAME, sshdr.sense_key, sshdr.asc,
  83. sshdr.ascq);
  84. break;
  85. }
  86. done:
  87. return ret;
  88. }
  89. /*
  90. * hp_sw_tur - Send TEST UNIT READY
  91. * @sdev: sdev command should be sent to
  92. *
  93. * Use the TEST UNIT READY command to determine
  94. * the path state.
  95. */
  96. static int hp_sw_tur(struct scsi_device *sdev, struct hp_sw_dh_data *h)
  97. {
  98. struct request *req;
  99. int ret;
  100. retry:
  101. req = blk_get_request(sdev->request_queue, WRITE, GFP_NOIO);
  102. if (IS_ERR(req))
  103. return SCSI_DH_RES_TEMP_UNAVAIL;
  104. blk_rq_set_block_pc(req);
  105. req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  106. REQ_FAILFAST_DRIVER;
  107. req->cmd_len = COMMAND_SIZE(TEST_UNIT_READY);
  108. req->cmd[0] = TEST_UNIT_READY;
  109. req->timeout = HP_SW_TIMEOUT;
  110. req->sense = h->sense;
  111. memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
  112. req->sense_len = 0;
  113. ret = blk_execute_rq(req->q, NULL, req, 1);
  114. if (ret == -EIO) {
  115. if (req->sense_len > 0) {
  116. ret = tur_done(sdev, h->sense);
  117. } else {
  118. sdev_printk(KERN_WARNING, sdev,
  119. "%s: sending tur failed with %x\n",
  120. HP_SW_NAME, req->errors);
  121. ret = SCSI_DH_IO;
  122. }
  123. } else {
  124. h->path_state = HP_SW_PATH_ACTIVE;
  125. ret = SCSI_DH_OK;
  126. }
  127. if (ret == SCSI_DH_IMM_RETRY) {
  128. blk_put_request(req);
  129. goto retry;
  130. }
  131. if (ret == SCSI_DH_DEV_OFFLINED) {
  132. h->path_state = HP_SW_PATH_PASSIVE;
  133. ret = SCSI_DH_OK;
  134. }
  135. blk_put_request(req);
  136. return ret;
  137. }
  138. /*
  139. * start_done - Handle START STOP UNIT return status
  140. * @sdev: sdev the command has been sent to
  141. * @errors: blk error code
  142. */
  143. static int start_done(struct scsi_device *sdev, unsigned char *sense)
  144. {
  145. struct scsi_sense_hdr sshdr;
  146. int rc;
  147. rc = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
  148. if (!rc) {
  149. sdev_printk(KERN_WARNING, sdev,
  150. "%s: sending start_stop_unit failed, "
  151. "no sense available\n",
  152. HP_SW_NAME);
  153. return SCSI_DH_IO;
  154. }
  155. switch (sshdr.sense_key) {
  156. case NOT_READY:
  157. if ((sshdr.asc == 0x04) && (sshdr.ascq == 3)) {
  158. /*
  159. * LUN not ready - manual intervention required
  160. *
  161. * Switch-over in progress, retry.
  162. */
  163. rc = SCSI_DH_RETRY;
  164. break;
  165. }
  166. /* fall through */
  167. default:
  168. sdev_printk(KERN_WARNING, sdev,
  169. "%s: sending start_stop_unit failed, sense %x/%x/%x\n",
  170. HP_SW_NAME, sshdr.sense_key, sshdr.asc,
  171. sshdr.ascq);
  172. rc = SCSI_DH_IO;
  173. }
  174. return rc;
  175. }
  176. static void start_stop_endio(struct request *req, int error)
  177. {
  178. struct hp_sw_dh_data *h = req->end_io_data;
  179. unsigned err = SCSI_DH_OK;
  180. if (error || host_byte(req->errors) != DID_OK ||
  181. msg_byte(req->errors) != COMMAND_COMPLETE) {
  182. sdev_printk(KERN_WARNING, h->sdev,
  183. "%s: sending start_stop_unit failed with %x\n",
  184. HP_SW_NAME, req->errors);
  185. err = SCSI_DH_IO;
  186. goto done;
  187. }
  188. if (req->sense_len > 0) {
  189. err = start_done(h->sdev, h->sense);
  190. if (err == SCSI_DH_RETRY) {
  191. err = SCSI_DH_IO;
  192. if (--h->retry_cnt) {
  193. blk_put_request(req);
  194. err = hp_sw_start_stop(h);
  195. if (err == SCSI_DH_OK)
  196. return;
  197. }
  198. }
  199. }
  200. done:
  201. req->end_io_data = NULL;
  202. __blk_put_request(req->q, req);
  203. if (h->callback_fn) {
  204. h->callback_fn(h->callback_data, err);
  205. h->callback_fn = h->callback_data = NULL;
  206. }
  207. return;
  208. }
  209. /*
  210. * hp_sw_start_stop - Send START STOP UNIT command
  211. * @sdev: sdev command should be sent to
  212. *
  213. * Sending START STOP UNIT activates the SP.
  214. */
  215. static int hp_sw_start_stop(struct hp_sw_dh_data *h)
  216. {
  217. struct request *req;
  218. req = blk_get_request(h->sdev->request_queue, WRITE, GFP_ATOMIC);
  219. if (IS_ERR(req))
  220. return SCSI_DH_RES_TEMP_UNAVAIL;
  221. blk_rq_set_block_pc(req);
  222. req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  223. REQ_FAILFAST_DRIVER;
  224. req->cmd_len = COMMAND_SIZE(START_STOP);
  225. req->cmd[0] = START_STOP;
  226. req->cmd[4] = 1; /* Start spin cycle */
  227. req->timeout = HP_SW_TIMEOUT;
  228. req->sense = h->sense;
  229. memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
  230. req->sense_len = 0;
  231. req->end_io_data = h;
  232. blk_execute_rq_nowait(req->q, NULL, req, 1, start_stop_endio);
  233. return SCSI_DH_OK;
  234. }
  235. static int hp_sw_prep_fn(struct scsi_device *sdev, struct request *req)
  236. {
  237. struct hp_sw_dh_data *h = sdev->handler_data;
  238. int ret = BLKPREP_OK;
  239. if (h->path_state != HP_SW_PATH_ACTIVE) {
  240. ret = BLKPREP_KILL;
  241. req->cmd_flags |= REQ_QUIET;
  242. }
  243. return ret;
  244. }
  245. /*
  246. * hp_sw_activate - Activate a path
  247. * @sdev: sdev on the path to be activated
  248. *
  249. * The HP Active/Passive firmware is pretty simple;
  250. * the passive path reports NOT READY with sense codes
  251. * 0x04/0x02; a START STOP UNIT command will then
  252. * activate the passive path (and deactivate the
  253. * previously active one).
  254. */
  255. static int hp_sw_activate(struct scsi_device *sdev,
  256. activate_complete fn, void *data)
  257. {
  258. int ret = SCSI_DH_OK;
  259. struct hp_sw_dh_data *h = sdev->handler_data;
  260. ret = hp_sw_tur(sdev, h);
  261. if (ret == SCSI_DH_OK && h->path_state == HP_SW_PATH_PASSIVE) {
  262. h->retry_cnt = h->retries;
  263. h->callback_fn = fn;
  264. h->callback_data = data;
  265. ret = hp_sw_start_stop(h);
  266. if (ret == SCSI_DH_OK)
  267. return 0;
  268. h->callback_fn = h->callback_data = NULL;
  269. }
  270. if (fn)
  271. fn(data, ret);
  272. return 0;
  273. }
  274. static int hp_sw_bus_attach(struct scsi_device *sdev)
  275. {
  276. struct hp_sw_dh_data *h;
  277. int ret;
  278. h = kzalloc(sizeof(*h), GFP_KERNEL);
  279. if (!h)
  280. return -ENOMEM;
  281. h->path_state = HP_SW_PATH_UNINITIALIZED;
  282. h->retries = HP_SW_RETRIES;
  283. h->sdev = sdev;
  284. ret = hp_sw_tur(sdev, h);
  285. if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
  286. goto failed;
  287. sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
  288. HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
  289. "active":"passive");
  290. sdev->handler_data = h;
  291. return 0;
  292. failed:
  293. kfree(h);
  294. return -EINVAL;
  295. }
  296. static void hp_sw_bus_detach( struct scsi_device *sdev )
  297. {
  298. kfree(sdev->handler_data);
  299. sdev->handler_data = NULL;
  300. }
  301. static struct scsi_device_handler hp_sw_dh = {
  302. .name = HP_SW_NAME,
  303. .module = THIS_MODULE,
  304. .attach = hp_sw_bus_attach,
  305. .detach = hp_sw_bus_detach,
  306. .activate = hp_sw_activate,
  307. .prep_fn = hp_sw_prep_fn,
  308. };
  309. static int __init hp_sw_init(void)
  310. {
  311. return scsi_register_device_handler(&hp_sw_dh);
  312. }
  313. static void __exit hp_sw_exit(void)
  314. {
  315. scsi_unregister_device_handler(&hp_sw_dh);
  316. }
  317. module_init(hp_sw_init);
  318. module_exit(hp_sw_exit);
  319. MODULE_DESCRIPTION("HP Active/Passive driver");
  320. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu");
  321. MODULE_LICENSE("GPL");