scsi_dh_hp_sw.c 9.1 KB

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