dm-emc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. rq->timeout = EMC_FAILOVER_TIMEOUT;
  92. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  93. rq->cmd_flags |= REQ_FAILFAST | REQ_NOMERGE;
  94. return rq;
  95. }
  96. static struct request *emc_trespass_get(struct emc_handler *h,
  97. struct dm_path *path)
  98. {
  99. struct bio *bio;
  100. struct request *rq;
  101. unsigned char *page22;
  102. unsigned char long_trespass_pg[] = {
  103. 0, 0, 0, 0,
  104. TRESPASS_PAGE, /* Page code */
  105. 0x09, /* Page length - 2 */
  106. h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
  107. 0xff, 0xff, /* Trespass target */
  108. 0, 0, 0, 0, 0, 0 /* Reserved bytes / unknown */
  109. };
  110. unsigned char short_trespass_pg[] = {
  111. 0, 0, 0, 0,
  112. TRESPASS_PAGE, /* Page code */
  113. 0x02, /* Page length - 2 */
  114. h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
  115. 0xff, /* Trespass target */
  116. };
  117. unsigned data_size = h->short_trespass ? sizeof(short_trespass_pg) :
  118. sizeof(long_trespass_pg);
  119. /* get bio backing */
  120. if (data_size > PAGE_SIZE)
  121. /* this should never happen */
  122. return NULL;
  123. bio = get_failover_bio(path, data_size);
  124. if (!bio) {
  125. DMERR("emc_trespass_get: no bio");
  126. return NULL;
  127. }
  128. page22 = (unsigned char *)bio_data(bio);
  129. memset(page22, 0, data_size);
  130. memcpy(page22, h->short_trespass ?
  131. short_trespass_pg : long_trespass_pg, data_size);
  132. /* get request for block layer packet command */
  133. rq = get_failover_req(h, bio, path);
  134. if (!rq) {
  135. DMERR("emc_trespass_get: no rq");
  136. free_bio(bio);
  137. return NULL;
  138. }
  139. /* Prepare the command. */
  140. rq->cmd[0] = MODE_SELECT;
  141. rq->cmd[1] = 0x10;
  142. rq->cmd[4] = data_size;
  143. rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
  144. return rq;
  145. }
  146. static void emc_pg_init(struct hw_handler *hwh, unsigned bypassed,
  147. struct dm_path *path)
  148. {
  149. struct request *rq;
  150. struct request_queue *q = bdev_get_queue(path->dev->bdev);
  151. /*
  152. * We can either blindly init the pg (then look at the sense),
  153. * or we can send some commands to get the state here (then
  154. * possibly send the fo cmnd), or we can also have the
  155. * initial state passed into us and then get an update here.
  156. */
  157. if (!q) {
  158. DMINFO("emc_pg_init: no queue");
  159. goto fail_path;
  160. }
  161. /* FIXME: The request should be pre-allocated. */
  162. rq = emc_trespass_get(hwh->context, path);
  163. if (!rq) {
  164. DMERR("emc_pg_init: no rq");
  165. goto fail_path;
  166. }
  167. DMINFO("emc_pg_init: sending switch-over command");
  168. elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
  169. return;
  170. fail_path:
  171. dm_pg_init_complete(path, MP_FAIL_PATH);
  172. }
  173. static struct emc_handler *alloc_emc_handler(void)
  174. {
  175. struct emc_handler *h = kzalloc(sizeof(*h), GFP_KERNEL);
  176. if (h)
  177. spin_lock_init(&h->lock);
  178. return h;
  179. }
  180. static int emc_create(struct hw_handler *hwh, unsigned argc, char **argv)
  181. {
  182. struct emc_handler *h;
  183. unsigned hr, short_trespass;
  184. if (argc == 0) {
  185. /* No arguments: use defaults */
  186. hr = 0;
  187. short_trespass = 0;
  188. } else if (argc != 2) {
  189. DMWARN("incorrect number of arguments");
  190. return -EINVAL;
  191. } else {
  192. if ((sscanf(argv[0], "%u", &short_trespass) != 1)
  193. || (short_trespass > 1)) {
  194. DMWARN("invalid trespass mode selected");
  195. return -EINVAL;
  196. }
  197. if ((sscanf(argv[1], "%u", &hr) != 1)
  198. || (hr > 1)) {
  199. DMWARN("invalid honor reservation flag selected");
  200. return -EINVAL;
  201. }
  202. }
  203. h = alloc_emc_handler();
  204. if (!h)
  205. return -ENOMEM;
  206. hwh->context = h;
  207. if ((h->short_trespass = short_trespass))
  208. DMWARN("short trespass command will be send");
  209. else
  210. DMWARN("long trespass command will be send");
  211. if ((h->hr = hr))
  212. DMWARN("honor reservation bit will be set");
  213. else
  214. DMWARN("honor reservation bit will not be set (default)");
  215. return 0;
  216. }
  217. static void emc_destroy(struct hw_handler *hwh)
  218. {
  219. struct emc_handler *h = (struct emc_handler *) hwh->context;
  220. kfree(h);
  221. hwh->context = NULL;
  222. }
  223. static unsigned emc_error(struct hw_handler *hwh, struct bio *bio)
  224. {
  225. /* FIXME: Patch from axboe still missing */
  226. #if 0
  227. int sense;
  228. if (bio->bi_error & BIO_SENSE) {
  229. sense = bio->bi_error & 0xffffff; /* sense key / asc / ascq */
  230. if (sense == 0x020403) {
  231. /* LUN Not Ready - Manual Intervention Required
  232. * indicates this is a passive path.
  233. *
  234. * FIXME: However, if this is seen and EVPD C0
  235. * indicates that this is due to a NDU in
  236. * progress, we should set FAIL_PATH too.
  237. * This indicates we might have to do a SCSI
  238. * inquiry in the end_io path. Ugh. */
  239. return MP_BYPASS_PG | MP_RETRY_IO;
  240. } else if (sense == 0x052501) {
  241. /* An array based copy is in progress. Do not
  242. * fail the path, do not bypass to another PG,
  243. * do not retry. Fail the IO immediately.
  244. * (Actually this is the same conclusion as in
  245. * the default handler, but lets make sure.) */
  246. return 0;
  247. } else if (sense == 0x062900) {
  248. /* Unit Attention Code. This is the first IO
  249. * to the new path, so just retry. */
  250. return MP_RETRY_IO;
  251. }
  252. }
  253. #endif
  254. /* Try default handler */
  255. return dm_scsi_err_handler(hwh, bio);
  256. }
  257. static struct hw_handler_type emc_hwh = {
  258. .name = "emc",
  259. .module = THIS_MODULE,
  260. .create = emc_create,
  261. .destroy = emc_destroy,
  262. .pg_init = emc_pg_init,
  263. .error = emc_error,
  264. };
  265. static int __init dm_emc_init(void)
  266. {
  267. int r = dm_register_hw_handler(&emc_hwh);
  268. if (r < 0)
  269. DMERR("register failed %d", r);
  270. DMINFO("version 0.0.3 loaded");
  271. return r;
  272. }
  273. static void __exit dm_emc_exit(void)
  274. {
  275. int r = dm_unregister_hw_handler(&emc_hwh);
  276. if (r < 0)
  277. DMERR("unregister failed %d", r);
  278. }
  279. module_init(dm_emc_init);
  280. module_exit(dm_emc_exit);
  281. MODULE_DESCRIPTION(DM_NAME " EMC CX/AX/FC-family multipath");
  282. MODULE_AUTHOR("Lars Marowsky-Bree <lmb@suse.de>");
  283. MODULE_LICENSE("GPL");