scsi_dh_hp_sw.c 10 KB

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