scsi_dh_hp_sw.c 9.9 KB

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