dm-emc.c 8.3 KB

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