scsi_dh_hp_sw.c 9.9 KB

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