dm-emc.c 8.4 KB

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