scsi_dh_hp_sw.c 9.2 KB

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