dm-emc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2004 SUSE LINUX Products GmbH. All rights reserved.
  3. * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. *
  7. * Multipath support for EMC CLARiiON AX/CX-series hardware.
  8. */
  9. #include "dm.h"
  10. #include "dm-hw-handler.h"
  11. #include <scsi/scsi.h>
  12. #include <scsi/scsi_cmnd.h>
  13. #define DM_MSG_PREFIX "multipath emc"
  14. struct emc_handler {
  15. spinlock_t lock;
  16. /* Whether we should send the short trespass command (FC-series)
  17. * or the long version (default for AX/CX CLARiiON arrays). */
  18. unsigned short_trespass;
  19. /* Whether or not to honor SCSI reservations when initiating a
  20. * switch-over. Default: Don't. */
  21. unsigned hr;
  22. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  23. };
  24. #define TRESPASS_PAGE 0x22
  25. #define EMC_FAILOVER_TIMEOUT (60 * HZ)
  26. /* Code borrowed from dm-lsi-rdac by Mike Christie */
  27. static inline void free_bio(struct bio *bio)
  28. {
  29. __free_page(bio->bi_io_vec[0].bv_page);
  30. bio_put(bio);
  31. }
  32. static void emc_endio(struct bio *bio, int error)
  33. {
  34. struct dm_path *path = bio->bi_private;
  35. /* We also need to look at the sense keys here whether or not to
  36. * switch to the next PG etc.
  37. *
  38. * For now simple logic: either it works or it doesn't.
  39. */
  40. if (error)
  41. dm_pg_init_complete(path, MP_FAIL_PATH);
  42. else
  43. dm_pg_init_complete(path, 0);
  44. /* request is freed in block layer */
  45. free_bio(bio);
  46. }
  47. static struct bio *get_failover_bio(struct dm_path *path, unsigned data_size)
  48. {
  49. struct bio *bio;
  50. struct page *page;
  51. bio = bio_alloc(GFP_ATOMIC, 1);
  52. if (!bio) {
  53. DMERR("get_failover_bio: bio_alloc() failed.");
  54. return NULL;
  55. }
  56. bio->bi_rw |= (1 << BIO_RW);
  57. bio->bi_bdev = path->dev->bdev;
  58. bio->bi_sector = 0;
  59. bio->bi_private = path;
  60. bio->bi_end_io = emc_endio;
  61. page = alloc_page(GFP_ATOMIC);
  62. if (!page) {
  63. DMERR("get_failover_bio: alloc_page() failed.");
  64. bio_put(bio);
  65. return NULL;
  66. }
  67. if (bio_add_page(bio, page, data_size, 0) != data_size) {
  68. DMERR("get_failover_bio: bio_add_page() failed.");
  69. __free_page(page);
  70. bio_put(bio);
  71. return NULL;
  72. }
  73. return bio;
  74. }
  75. static struct request *get_failover_req(struct emc_handler *h,
  76. struct bio *bio, struct dm_path *path)
  77. {
  78. struct request *rq;
  79. struct block_device *bdev = bio->bi_bdev;
  80. struct request_queue *q = bdev_get_queue(bdev);
  81. /* FIXME: Figure out why it fails with GFP_ATOMIC. */
  82. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  83. if (!rq) {
  84. DMERR("get_failover_req: blk_get_request failed");
  85. return NULL;
  86. }
  87. blk_rq_append_bio(q, rq, bio);
  88. rq->sense = h->sense;
  89. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  90. rq->sense_len = 0;
  91. memset(&rq->cmd, 0, BLK_MAX_CDB);
  92. rq->timeout = EMC_FAILOVER_TIMEOUT;
  93. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  94. rq->cmd_flags |= REQ_FAILFAST | REQ_NOMERGE;
  95. return rq;
  96. }
  97. static struct request *emc_trespass_get(struct emc_handler *h,
  98. struct dm_path *path)
  99. {
  100. struct bio *bio;
  101. struct request *rq;
  102. unsigned char *page22;
  103. unsigned char long_trespass_pg[] = {
  104. 0, 0, 0, 0,
  105. TRESPASS_PAGE, /* Page code */
  106. 0x09, /* Page length - 2 */
  107. h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
  108. 0xff, 0xff, /* Trespass target */
  109. 0, 0, 0, 0, 0, 0 /* Reserved bytes / unknown */
  110. };
  111. unsigned char short_trespass_pg[] = {
  112. 0, 0, 0, 0,
  113. TRESPASS_PAGE, /* Page code */
  114. 0x02, /* Page length - 2 */
  115. h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
  116. 0xff, /* Trespass target */
  117. };
  118. unsigned data_size = h->short_trespass ? sizeof(short_trespass_pg) :
  119. sizeof(long_trespass_pg);
  120. /* get bio backing */
  121. if (data_size > PAGE_SIZE)
  122. /* this should never happen */
  123. return NULL;
  124. bio = get_failover_bio(path, data_size);
  125. if (!bio) {
  126. DMERR("emc_trespass_get: no bio");
  127. return NULL;
  128. }
  129. page22 = (unsigned char *)bio_data(bio);
  130. memset(page22, 0, data_size);
  131. memcpy(page22, h->short_trespass ?
  132. short_trespass_pg : long_trespass_pg, data_size);
  133. /* get request for block layer packet command */
  134. rq = get_failover_req(h, bio, path);
  135. if (!rq) {
  136. DMERR("emc_trespass_get: no rq");
  137. free_bio(bio);
  138. return NULL;
  139. }
  140. /* Prepare the command. */
  141. rq->cmd[0] = MODE_SELECT;
  142. rq->cmd[1] = 0x10;
  143. rq->cmd[4] = data_size;
  144. rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
  145. return rq;
  146. }
  147. static void emc_pg_init(struct hw_handler *hwh, unsigned bypassed,
  148. struct dm_path *path)
  149. {
  150. struct request *rq;
  151. struct request_queue *q = bdev_get_queue(path->dev->bdev);
  152. /*
  153. * We can either blindly init the pg (then look at the sense),
  154. * or we can send some commands to get the state here (then
  155. * possibly send the fo cmnd), or we can also have the
  156. * initial state passed into us and then get an update here.
  157. */
  158. if (!q) {
  159. DMINFO("emc_pg_init: no queue");
  160. goto fail_path;
  161. }
  162. /* FIXME: The request should be pre-allocated. */
  163. rq = emc_trespass_get(hwh->context, path);
  164. if (!rq) {
  165. DMERR("emc_pg_init: no rq");
  166. goto fail_path;
  167. }
  168. DMINFO("emc_pg_init: sending switch-over command");
  169. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
  170. return;
  171. fail_path:
  172. dm_pg_init_complete(path, MP_FAIL_PATH);
  173. }
  174. static struct emc_handler *alloc_emc_handler(void)
  175. {
  176. struct emc_handler *h = kzalloc(sizeof(*h), GFP_KERNEL);
  177. if (h)
  178. spin_lock_init(&h->lock);
  179. return h;
  180. }
  181. static int emc_create(struct hw_handler *hwh, unsigned argc, char **argv)
  182. {
  183. struct emc_handler *h;
  184. unsigned hr, short_trespass;
  185. if (argc == 0) {
  186. /* No arguments: use defaults */
  187. hr = 0;
  188. short_trespass = 0;
  189. } else if (argc != 2) {
  190. DMWARN("incorrect number of arguments");
  191. return -EINVAL;
  192. } else {
  193. if ((sscanf(argv[0], "%u", &short_trespass) != 1)
  194. || (short_trespass > 1)) {
  195. DMWARN("invalid trespass mode selected");
  196. return -EINVAL;
  197. }
  198. if ((sscanf(argv[1], "%u", &hr) != 1)
  199. || (hr > 1)) {
  200. DMWARN("invalid honor reservation flag selected");
  201. return -EINVAL;
  202. }
  203. }
  204. h = alloc_emc_handler();
  205. if (!h)
  206. return -ENOMEM;
  207. hwh->context = h;
  208. if ((h->short_trespass = short_trespass))
  209. DMWARN("short trespass command will be send");
  210. else
  211. DMWARN("long trespass command will be send");
  212. if ((h->hr = hr))
  213. DMWARN("honor reservation bit will be set");
  214. else
  215. DMWARN("honor reservation bit will not be set (default)");
  216. return 0;
  217. }
  218. static void emc_destroy(struct hw_handler *hwh)
  219. {
  220. struct emc_handler *h = (struct emc_handler *) hwh->context;
  221. kfree(h);
  222. hwh->context = NULL;
  223. }
  224. static unsigned emc_error(struct hw_handler *hwh, struct bio *bio)
  225. {
  226. /* FIXME: Patch from axboe still missing */
  227. #if 0
  228. int sense;
  229. if (bio->bi_error & BIO_SENSE) {
  230. sense = bio->bi_error & 0xffffff; /* sense key / asc / ascq */
  231. if (sense == 0x020403) {
  232. /* LUN Not Ready - Manual Intervention Required
  233. * indicates this is a passive path.
  234. *
  235. * FIXME: However, if this is seen and EVPD C0
  236. * indicates that this is due to a NDU in
  237. * progress, we should set FAIL_PATH too.
  238. * This indicates we might have to do a SCSI
  239. * inquiry in the end_io path. Ugh. */
  240. return MP_BYPASS_PG | MP_RETRY_IO;
  241. } else if (sense == 0x052501) {
  242. /* An array based copy is in progress. Do not
  243. * fail the path, do not bypass to another PG,
  244. * do not retry. Fail the IO immediately.
  245. * (Actually this is the same conclusion as in
  246. * the default handler, but lets make sure.) */
  247. return 0;
  248. } else if (sense == 0x062900) {
  249. /* Unit Attention Code. This is the first IO
  250. * to the new path, so just retry. */
  251. return MP_RETRY_IO;
  252. }
  253. }
  254. #endif
  255. /* Try default handler */
  256. return dm_scsi_err_handler(hwh, bio);
  257. }
  258. static struct hw_handler_type emc_hwh = {
  259. .name = "emc",
  260. .module = THIS_MODULE,
  261. .create = emc_create,
  262. .destroy = emc_destroy,
  263. .pg_init = emc_pg_init,
  264. .error = emc_error,
  265. };
  266. static int __init dm_emc_init(void)
  267. {
  268. int r = dm_register_hw_handler(&emc_hwh);
  269. if (r < 0)
  270. DMERR("register failed %d", r);
  271. DMINFO("version 0.0.3 loaded");
  272. return r;
  273. }
  274. static void __exit dm_emc_exit(void)
  275. {
  276. int r = dm_unregister_hw_handler(&emc_hwh);
  277. if (r < 0)
  278. DMERR("unregister failed %d", r);
  279. }
  280. module_init(dm_emc_init);
  281. module_exit(dm_emc_exit);
  282. MODULE_DESCRIPTION(DM_NAME " EMC CX/AX/FC-family multipath");
  283. MODULE_AUTHOR("Lars Marowsky-Bree <lmb@suse.de>");
  284. MODULE_LICENSE("GPL");