dm-emc.c 8.3 KB

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