scsi_dh_hp_sw.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <scsi/scsi.h>
  24. #include <scsi/scsi_dbg.h>
  25. #include <scsi/scsi_eh.h>
  26. #include <scsi/scsi_dh.h>
  27. #define HP_SW_NAME "hp_sw"
  28. #define HP_SW_TIMEOUT (60 * HZ)
  29. #define HP_SW_RETRIES 3
  30. #define HP_SW_PATH_UNINITIALIZED -1
  31. #define HP_SW_PATH_ACTIVE 0
  32. #define HP_SW_PATH_PASSIVE 1
  33. struct hp_sw_dh_data {
  34. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  35. int path_state;
  36. int retries;
  37. };
  38. static inline struct hp_sw_dh_data *get_hp_sw_data(struct scsi_device *sdev)
  39. {
  40. struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
  41. BUG_ON(scsi_dh_data == NULL);
  42. return ((struct hp_sw_dh_data *) scsi_dh_data->buf);
  43. }
  44. /*
  45. * tur_done - Handle TEST UNIT READY return status
  46. * @sdev: sdev the command has been sent to
  47. * @errors: blk error code
  48. *
  49. * Returns SCSI_DH_DEV_OFFLINED if the sdev is on the passive path
  50. */
  51. static int tur_done(struct scsi_device *sdev, unsigned char *sense)
  52. {
  53. struct scsi_sense_hdr sshdr;
  54. int ret;
  55. ret = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
  56. if (!ret) {
  57. sdev_printk(KERN_WARNING, sdev,
  58. "%s: sending tur failed, no sense available\n",
  59. HP_SW_NAME);
  60. ret = SCSI_DH_IO;
  61. goto done;
  62. }
  63. switch (sshdr.sense_key) {
  64. case UNIT_ATTENTION:
  65. ret = SCSI_DH_IMM_RETRY;
  66. break;
  67. case NOT_READY:
  68. if ((sshdr.asc == 0x04) && (sshdr.ascq == 2)) {
  69. /*
  70. * LUN not ready - Initialization command required
  71. *
  72. * This is the passive path
  73. */
  74. ret = SCSI_DH_DEV_OFFLINED;
  75. break;
  76. }
  77. /* Fallthrough */
  78. default:
  79. sdev_printk(KERN_WARNING, sdev,
  80. "%s: sending tur failed, sense %x/%x/%x\n",
  81. HP_SW_NAME, sshdr.sense_key, sshdr.asc,
  82. sshdr.ascq);
  83. break;
  84. }
  85. done:
  86. return ret;
  87. }
  88. /*
  89. * hp_sw_tur - Send TEST UNIT READY
  90. * @sdev: sdev command should be sent to
  91. *
  92. * Use the TEST UNIT READY command to determine
  93. * the path state.
  94. */
  95. static int hp_sw_tur(struct scsi_device *sdev, struct hp_sw_dh_data *h)
  96. {
  97. struct request *req;
  98. int ret;
  99. retry:
  100. req = blk_get_request(sdev->request_queue, WRITE, GFP_NOIO);
  101. if (!req)
  102. return SCSI_DH_RES_TEMP_UNAVAIL;
  103. req->cmd_type = REQ_TYPE_BLOCK_PC;
  104. req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  105. REQ_FAILFAST_DRIVER;
  106. req->cmd_len = COMMAND_SIZE(TEST_UNIT_READY);
  107. req->cmd[0] = TEST_UNIT_READY;
  108. req->timeout = HP_SW_TIMEOUT;
  109. req->sense = h->sense;
  110. memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
  111. req->sense_len = 0;
  112. ret = blk_execute_rq(req->q, NULL, req, 1);
  113. if (ret == -EIO) {
  114. if (req->sense_len > 0) {
  115. ret = tur_done(sdev, h->sense);
  116. } else {
  117. sdev_printk(KERN_WARNING, sdev,
  118. "%s: sending tur failed with %x\n",
  119. HP_SW_NAME, req->errors);
  120. ret = SCSI_DH_IO;
  121. }
  122. } else {
  123. h->path_state = HP_SW_PATH_ACTIVE;
  124. ret = SCSI_DH_OK;
  125. }
  126. if (ret == SCSI_DH_IMM_RETRY) {
  127. blk_put_request(req);
  128. goto retry;
  129. }
  130. if (ret == SCSI_DH_DEV_OFFLINED) {
  131. h->path_state = HP_SW_PATH_PASSIVE;
  132. ret = SCSI_DH_OK;
  133. }
  134. blk_put_request(req);
  135. return ret;
  136. }
  137. /*
  138. * start_done - Handle START STOP UNIT return status
  139. * @sdev: sdev the command has been sent to
  140. * @errors: blk error code
  141. */
  142. static int start_done(struct scsi_device *sdev, unsigned char *sense)
  143. {
  144. struct scsi_sense_hdr sshdr;
  145. int rc;
  146. rc = scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr);
  147. if (!rc) {
  148. sdev_printk(KERN_WARNING, sdev,
  149. "%s: sending start_stop_unit failed, "
  150. "no sense available\n",
  151. HP_SW_NAME);
  152. return SCSI_DH_IO;
  153. }
  154. switch (sshdr.sense_key) {
  155. case NOT_READY:
  156. if ((sshdr.asc == 0x04) && (sshdr.ascq == 3)) {
  157. /*
  158. * LUN not ready - manual intervention required
  159. *
  160. * Switch-over in progress, retry.
  161. */
  162. rc = SCSI_DH_RETRY;
  163. break;
  164. }
  165. /* fall through */
  166. default:
  167. sdev_printk(KERN_WARNING, sdev,
  168. "%s: sending start_stop_unit failed, sense %x/%x/%x\n",
  169. HP_SW_NAME, sshdr.sense_key, sshdr.asc,
  170. sshdr.ascq);
  171. rc = SCSI_DH_IO;
  172. }
  173. return rc;
  174. }
  175. /*
  176. * hp_sw_start_stop - Send START STOP UNIT command
  177. * @sdev: sdev command should be sent to
  178. *
  179. * Sending START STOP UNIT activates the SP.
  180. */
  181. static int hp_sw_start_stop(struct scsi_device *sdev, struct hp_sw_dh_data *h)
  182. {
  183. struct request *req;
  184. int ret, retry;
  185. retry:
  186. req = blk_get_request(sdev->request_queue, WRITE, GFP_NOIO);
  187. if (!req)
  188. return SCSI_DH_RES_TEMP_UNAVAIL;
  189. req->cmd_type = REQ_TYPE_BLOCK_PC;
  190. req->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  191. REQ_FAILFAST_DRIVER;
  192. req->cmd_len = COMMAND_SIZE(START_STOP);
  193. req->cmd[0] = START_STOP;
  194. req->cmd[4] = 1; /* Start spin cycle */
  195. req->timeout = HP_SW_TIMEOUT;
  196. req->sense = h->sense;
  197. memset(req->sense, 0, SCSI_SENSE_BUFFERSIZE);
  198. req->sense_len = 0;
  199. retry = h->retries;
  200. ret = blk_execute_rq(req->q, NULL, req, 1);
  201. if (ret == -EIO) {
  202. if (req->sense_len > 0) {
  203. ret = start_done(sdev, h->sense);
  204. } else {
  205. sdev_printk(KERN_WARNING, sdev,
  206. "%s: sending start_stop_unit failed with %x\n",
  207. HP_SW_NAME, req->errors);
  208. ret = SCSI_DH_IO;
  209. }
  210. } else
  211. ret = SCSI_DH_OK;
  212. if (ret == SCSI_DH_RETRY) {
  213. if (--retry) {
  214. blk_put_request(req);
  215. goto retry;
  216. }
  217. ret = SCSI_DH_IO;
  218. }
  219. blk_put_request(req);
  220. return ret;
  221. }
  222. static int hp_sw_prep_fn(struct scsi_device *sdev, struct request *req)
  223. {
  224. struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
  225. int ret = BLKPREP_OK;
  226. if (h->path_state != HP_SW_PATH_ACTIVE) {
  227. ret = BLKPREP_KILL;
  228. req->cmd_flags |= REQ_QUIET;
  229. }
  230. return ret;
  231. }
  232. /*
  233. * hp_sw_activate - Activate a path
  234. * @sdev: sdev on the path to be activated
  235. *
  236. * The HP Active/Passive firmware is pretty simple;
  237. * the passive path reports NOT READY with sense codes
  238. * 0x04/0x02; a START STOP UNIT command will then
  239. * activate the passive path (and deactivate the
  240. * previously active one).
  241. */
  242. static int hp_sw_activate(struct scsi_device *sdev)
  243. {
  244. int ret = SCSI_DH_OK;
  245. struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
  246. ret = hp_sw_tur(sdev, h);
  247. if (ret == SCSI_DH_OK && h->path_state == HP_SW_PATH_PASSIVE) {
  248. ret = hp_sw_start_stop(sdev, h);
  249. if (ret == SCSI_DH_OK)
  250. sdev_printk(KERN_INFO, sdev,
  251. "%s: activated path\n",
  252. HP_SW_NAME);
  253. }
  254. return ret;
  255. }
  256. static const struct scsi_dh_devlist hp_sw_dh_data_list[] = {
  257. {"COMPAQ", "MSA1000 VOLUME"},
  258. {"COMPAQ", "HSV110"},
  259. {"HP", "HSV100"},
  260. {"DEC", "HSG80"},
  261. {NULL, NULL},
  262. };
  263. static int hp_sw_bus_attach(struct scsi_device *sdev);
  264. static void hp_sw_bus_detach(struct scsi_device *sdev);
  265. static struct scsi_device_handler hp_sw_dh = {
  266. .name = HP_SW_NAME,
  267. .module = THIS_MODULE,
  268. .devlist = hp_sw_dh_data_list,
  269. .attach = hp_sw_bus_attach,
  270. .detach = hp_sw_bus_detach,
  271. .activate = hp_sw_activate,
  272. .prep_fn = hp_sw_prep_fn,
  273. };
  274. static int hp_sw_bus_attach(struct scsi_device *sdev)
  275. {
  276. struct scsi_dh_data *scsi_dh_data;
  277. struct hp_sw_dh_data *h;
  278. unsigned long flags;
  279. int ret;
  280. scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
  281. + sizeof(struct hp_sw_dh_data) , GFP_KERNEL);
  282. if (!scsi_dh_data) {
  283. sdev_printk(KERN_ERR, sdev, "%s: Attach Failed\n",
  284. HP_SW_NAME);
  285. return 0;
  286. }
  287. scsi_dh_data->scsi_dh = &hp_sw_dh;
  288. h = (struct hp_sw_dh_data *) scsi_dh_data->buf;
  289. h->path_state = HP_SW_PATH_UNINITIALIZED;
  290. h->retries = HP_SW_RETRIES;
  291. ret = hp_sw_tur(sdev, h);
  292. if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
  293. goto failed;
  294. if (!try_module_get(THIS_MODULE))
  295. goto failed;
  296. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  297. sdev->scsi_dh_data = scsi_dh_data;
  298. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  299. sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
  300. HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
  301. "active":"passive");
  302. return 0;
  303. failed:
  304. kfree(scsi_dh_data);
  305. sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
  306. HP_SW_NAME);
  307. return -EINVAL;
  308. }
  309. static void hp_sw_bus_detach( struct scsi_device *sdev )
  310. {
  311. struct scsi_dh_data *scsi_dh_data;
  312. unsigned long flags;
  313. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  314. scsi_dh_data = sdev->scsi_dh_data;
  315. sdev->scsi_dh_data = NULL;
  316. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  317. module_put(THIS_MODULE);
  318. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", HP_SW_NAME);
  319. kfree(scsi_dh_data);
  320. }
  321. static int __init hp_sw_init(void)
  322. {
  323. return scsi_register_device_handler(&hp_sw_dh);
  324. }
  325. static void __exit hp_sw_exit(void)
  326. {
  327. scsi_unregister_device_handler(&hp_sw_dh);
  328. }
  329. module_init(hp_sw_init);
  330. module_exit(hp_sw_exit);
  331. MODULE_DESCRIPTION("HP Active/Passive driver");
  332. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu");
  333. MODULE_LICENSE("GPL");