scsi_dh_hp_sw.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <linux/module.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_dbg.h>
  27. #include <scsi/scsi_eh.h>
  28. #include <scsi/scsi_dh.h>
  29. #define HP_SW_NAME "hp_sw"
  30. #define HP_SW_TIMEOUT (60 * HZ)
  31. #define HP_SW_RETRIES 3
  32. #define HP_SW_PATH_UNINITIALIZED -1
  33. #define HP_SW_PATH_ACTIVE 0
  34. #define HP_SW_PATH_PASSIVE 1
  35. struct hp_sw_dh_data {
  36. struct scsi_dh_data dh_data;
  37. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  38. int path_state;
  39. int retries;
  40. int retry_cnt;
  41. struct scsi_device *sdev;
  42. activate_complete callback_fn;
  43. void *callback_data;
  44. };
  45. static int hp_sw_start_stop(struct hp_sw_dh_data *);
  46. static inline struct hp_sw_dh_data *get_hp_sw_data(struct scsi_device *sdev)
  47. {
  48. return container_of(sdev->scsi_dh_data, struct hp_sw_dh_data, dh_data);
  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 (IS_ERR(req))
  108. return SCSI_DH_RES_TEMP_UNAVAIL;
  109. blk_rq_set_block_pc(req);
  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 (IS_ERR(req))
  225. return SCSI_DH_RES_TEMP_UNAVAIL;
  226. blk_rq_set_block_pc(req);
  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 {
  280. char *vendor;
  281. char *model;
  282. } hp_sw_dh_data_list[] = {
  283. {"COMPAQ", "MSA1000 VOLUME"},
  284. {"COMPAQ", "HSV110"},
  285. {"HP", "HSV100"},
  286. {"DEC", "HSG80"},
  287. {NULL, NULL},
  288. };
  289. static bool hp_sw_match(struct scsi_device *sdev)
  290. {
  291. int i;
  292. if (scsi_device_tpgs(sdev))
  293. return false;
  294. for (i = 0; hp_sw_dh_data_list[i].vendor; i++) {
  295. if (!strncmp(sdev->vendor, hp_sw_dh_data_list[i].vendor,
  296. strlen(hp_sw_dh_data_list[i].vendor)) &&
  297. !strncmp(sdev->model, hp_sw_dh_data_list[i].model,
  298. strlen(hp_sw_dh_data_list[i].model))) {
  299. return true;
  300. }
  301. }
  302. return false;
  303. }
  304. static struct scsi_dh_data *hp_sw_bus_attach(struct scsi_device *sdev)
  305. {
  306. struct hp_sw_dh_data *h;
  307. int ret;
  308. h = kzalloc(sizeof(*h), GFP_KERNEL);
  309. if (!h)
  310. return ERR_PTR(-ENOMEM);
  311. h->path_state = HP_SW_PATH_UNINITIALIZED;
  312. h->retries = HP_SW_RETRIES;
  313. h->sdev = sdev;
  314. ret = hp_sw_tur(sdev, h);
  315. if (ret != SCSI_DH_OK || h->path_state == HP_SW_PATH_UNINITIALIZED)
  316. goto failed;
  317. sdev_printk(KERN_INFO, sdev, "%s: attached to %s path\n",
  318. HP_SW_NAME, h->path_state == HP_SW_PATH_ACTIVE?
  319. "active":"passive");
  320. return &h->dh_data;
  321. failed:
  322. kfree(h);
  323. return ERR_PTR(-EINVAL);
  324. }
  325. static void hp_sw_bus_detach( struct scsi_device *sdev )
  326. {
  327. struct hp_sw_dh_data *h = get_hp_sw_data(sdev);
  328. kfree(h);
  329. }
  330. static struct scsi_device_handler hp_sw_dh = {
  331. .name = HP_SW_NAME,
  332. .module = THIS_MODULE,
  333. .attach = hp_sw_bus_attach,
  334. .detach = hp_sw_bus_detach,
  335. .activate = hp_sw_activate,
  336. .prep_fn = hp_sw_prep_fn,
  337. .match = hp_sw_match,
  338. };
  339. static int __init hp_sw_init(void)
  340. {
  341. return scsi_register_device_handler(&hp_sw_dh);
  342. }
  343. static void __exit hp_sw_exit(void)
  344. {
  345. scsi_unregister_device_handler(&hp_sw_dh);
  346. }
  347. module_init(hp_sw_init);
  348. module_exit(hp_sw_exit);
  349. MODULE_DESCRIPTION("HP Active/Passive driver");
  350. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu");
  351. MODULE_LICENSE("GPL");