blob: 71cc858b786041401031d7bfab99bb021c81b973 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
10#include "dm.h"
11#include "dm-hw-handler.h"
12#include <scsi/scsi.h>
13#include <scsi/scsi_cmnd.h>
14
Alasdair G Kergon72d94862006-06-26 00:27:35 -070015#define DM_MSG_PREFIX "multipath emc"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct emc_handler {
18 spinlock_t lock;
19
20 /* Whether we should send the short trespass command (FC-series)
21 * or the long version (default for AX/CX CLARiiON arrays). */
22 unsigned short_trespass;
23 /* Whether or not to honor SCSI reservations when initiating a
24 * switch-over. Default: Don't. */
25 unsigned hr;
26
27 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
28};
29
30#define TRESPASS_PAGE 0x22
31#define EMC_FAILOVER_TIMEOUT (60 * HZ)
32
33/* Code borrowed from dm-lsi-rdac by Mike Christie */
34
35static inline void free_bio(struct bio *bio)
36{
37 __free_page(bio->bi_io_vec[0].bv_page);
38 bio_put(bio);
39}
40
41static int emc_endio(struct bio *bio, unsigned int bytes_done, int error)
42{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080043 struct dm_path *path = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (bio->bi_size)
46 return 1;
47
48 /* We also need to look at the sense keys here whether or not to
49 * switch to the next PG etc.
50 *
51 * For now simple logic: either it works or it doesn't.
52 */
53 if (error)
54 dm_pg_init_complete(path, MP_FAIL_PATH);
55 else
56 dm_pg_init_complete(path, 0);
57
58 /* request is freed in block layer */
59 free_bio(bio);
60
61 return 0;
62}
63
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080064static struct bio *get_failover_bio(struct dm_path *path, unsigned data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct bio *bio;
67 struct page *page;
68
69 bio = bio_alloc(GFP_ATOMIC, 1);
70 if (!bio) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070071 DMERR("get_failover_bio: bio_alloc() failed.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return NULL;
73 }
74
75 bio->bi_rw |= (1 << BIO_RW);
76 bio->bi_bdev = path->dev->bdev;
77 bio->bi_sector = 0;
78 bio->bi_private = path;
79 bio->bi_end_io = emc_endio;
80
81 page = alloc_page(GFP_ATOMIC);
82 if (!page) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070083 DMERR("get_failover_bio: alloc_page() failed.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 bio_put(bio);
85 return NULL;
86 }
87
88 if (bio_add_page(bio, page, data_size, 0) != data_size) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070089 DMERR("get_failover_bio: alloc_page() failed.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 __free_page(page);
91 bio_put(bio);
92 return NULL;
93 }
94
95 return bio;
96}
97
98static struct request *get_failover_req(struct emc_handler *h,
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080099 struct bio *bio, struct dm_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct request *rq;
102 struct block_device *bdev = bio->bi_bdev;
103 struct request_queue *q = bdev_get_queue(bdev);
104
105 /* FIXME: Figure out why it fails with GFP_ATOMIC. */
106 rq = blk_get_request(q, WRITE, __GFP_WAIT);
107 if (!rq) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700108 DMERR("get_failover_req: blk_get_request failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return NULL;
110 }
111
NeilBrown66846572007-08-16 13:31:28 +0200112 blk_rq_append_bio(q, rq, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 rq->sense = h->sense;
115 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
116 rq->sense_len = 0;
117
118 memset(&rq->cmd, 0, BLK_MAX_CDB);
119
120 rq->timeout = EMC_FAILOVER_TIMEOUT;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200121 rq->cmd_type = REQ_TYPE_BLOCK_PC;
122 rq->cmd_flags |= REQ_FAILFAST | REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return rq;
125}
126
127static struct request *emc_trespass_get(struct emc_handler *h,
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800128 struct dm_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct bio *bio;
131 struct request *rq;
132 unsigned char *page22;
133 unsigned char long_trespass_pg[] = {
134 0, 0, 0, 0,
135 TRESPASS_PAGE, /* Page code */
136 0x09, /* Page length - 2 */
137 h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
138 0xff, 0xff, /* Trespass target */
139 0, 0, 0, 0, 0, 0 /* Reserved bytes / unknown */
140 };
141 unsigned char short_trespass_pg[] = {
142 0, 0, 0, 0,
143 TRESPASS_PAGE, /* Page code */
144 0x02, /* Page length - 2 */
145 h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
146 0xff, /* Trespass target */
147 };
148 unsigned data_size = h->short_trespass ? sizeof(short_trespass_pg) :
149 sizeof(long_trespass_pg);
150
151 /* get bio backing */
152 if (data_size > PAGE_SIZE)
153 /* this should never happen */
154 return NULL;
155
156 bio = get_failover_bio(path, data_size);
157 if (!bio) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700158 DMERR("emc_trespass_get: no bio");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return NULL;
160 }
161
162 page22 = (unsigned char *)bio_data(bio);
163 memset(page22, 0, data_size);
164
165 memcpy(page22, h->short_trespass ?
166 short_trespass_pg : long_trespass_pg, data_size);
167
168 /* get request for block layer packet command */
169 rq = get_failover_req(h, bio, path);
170 if (!rq) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700171 DMERR("emc_trespass_get: no rq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 free_bio(bio);
173 return NULL;
174 }
175
176 /* Prepare the command. */
177 rq->cmd[0] = MODE_SELECT;
178 rq->cmd[1] = 0x10;
179 rq->cmd[4] = data_size;
180 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
181
182 return rq;
183}
184
185static void emc_pg_init(struct hw_handler *hwh, unsigned bypassed,
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800186 struct dm_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct request *rq;
189 struct request_queue *q = bdev_get_queue(path->dev->bdev);
190
191 /*
192 * We can either blindly init the pg (then look at the sense),
193 * or we can send some commands to get the state here (then
194 * possibly send the fo cmnd), or we can also have the
195 * initial state passed into us and then get an update here.
196 */
197 if (!q) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700198 DMINFO("emc_pg_init: no queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto fail_path;
200 }
201
202 /* FIXME: The request should be pre-allocated. */
203 rq = emc_trespass_get(hwh->context, path);
204 if (!rq) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700205 DMERR("emc_pg_init: no rq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto fail_path;
207 }
208
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700209 DMINFO("emc_pg_init: sending switch-over command");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
211 return;
212
213fail_path:
214 dm_pg_init_complete(path, MP_FAIL_PATH);
215}
216
217static struct emc_handler *alloc_emc_handler(void)
218{
219 struct emc_handler *h = kmalloc(sizeof(*h), GFP_KERNEL);
220
Alasdair G Kergonf1daa402005-05-05 16:16:08 -0700221 if (h) {
222 memset(h, 0, sizeof(*h));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_lock_init(&h->lock);
Alasdair G Kergonf1daa402005-05-05 16:16:08 -0700224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return h;
227}
228
229static int emc_create(struct hw_handler *hwh, unsigned argc, char **argv)
230{
231 struct emc_handler *h;
232 unsigned hr, short_trespass;
233
234 if (argc == 0) {
235 /* No arguments: use defaults */
236 hr = 0;
237 short_trespass = 0;
238 } else if (argc != 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700239 DMWARN("incorrect number of arguments");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EINVAL;
241 } else {
242 if ((sscanf(argv[0], "%u", &short_trespass) != 1)
243 || (short_trespass > 1)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700244 DMWARN("invalid trespass mode selected");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EINVAL;
246 }
247
248 if ((sscanf(argv[1], "%u", &hr) != 1)
249 || (hr > 1)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700250 DMWARN("invalid honor reservation flag selected");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EINVAL;
252 }
253 }
254
255 h = alloc_emc_handler();
256 if (!h)
257 return -ENOMEM;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 hwh->context = h;
260
261 if ((h->short_trespass = short_trespass))
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700262 DMWARN("short trespass command will be send");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 else
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700264 DMWARN("long trespass command will be send");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if ((h->hr = hr))
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700267 DMWARN("honor reservation bit will be set");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 else
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700269 DMWARN("honor reservation bit will not be set (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 return 0;
272}
273
274static void emc_destroy(struct hw_handler *hwh)
275{
276 struct emc_handler *h = (struct emc_handler *) hwh->context;
277
278 kfree(h);
279 hwh->context = NULL;
280}
281
282static unsigned emc_error(struct hw_handler *hwh, struct bio *bio)
283{
284 /* FIXME: Patch from axboe still missing */
285#if 0
286 int sense;
287
288 if (bio->bi_error & BIO_SENSE) {
289 sense = bio->bi_error & 0xffffff; /* sense key / asc / ascq */
290
291 if (sense == 0x020403) {
292 /* LUN Not Ready - Manual Intervention Required
293 * indicates this is a passive path.
294 *
295 * FIXME: However, if this is seen and EVPD C0
296 * indicates that this is due to a NDU in
297 * progress, we should set FAIL_PATH too.
298 * This indicates we might have to do a SCSI
299 * inquiry in the end_io path. Ugh. */
300 return MP_BYPASS_PG | MP_RETRY_IO;
301 } else if (sense == 0x052501) {
302 /* An array based copy is in progress. Do not
303 * fail the path, do not bypass to another PG,
304 * do not retry. Fail the IO immediately.
305 * (Actually this is the same conclusion as in
306 * the default handler, but lets make sure.) */
307 return 0;
308 } else if (sense == 0x062900) {
309 /* Unit Attention Code. This is the first IO
310 * to the new path, so just retry. */
311 return MP_RETRY_IO;
312 }
313 }
314#endif
315
316 /* Try default handler */
317 return dm_scsi_err_handler(hwh, bio);
318}
319
320static struct hw_handler_type emc_hwh = {
321 .name = "emc",
322 .module = THIS_MODULE,
323 .create = emc_create,
324 .destroy = emc_destroy,
325 .pg_init = emc_pg_init,
326 .error = emc_error,
327};
328
329static int __init dm_emc_init(void)
330{
331 int r = dm_register_hw_handler(&emc_hwh);
332
333 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700334 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700336 DMINFO("version 0.0.3 loaded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return r;
339}
340
341static void __exit dm_emc_exit(void)
342{
343 int r = dm_unregister_hw_handler(&emc_hwh);
344
345 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700346 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349module_init(dm_emc_init);
350module_exit(dm_emc_exit);
351
352MODULE_DESCRIPTION(DM_NAME " EMC CX/AX/FC-family multipath");
353MODULE_AUTHOR("Lars Marowsky-Bree <lmb@suse.de>");
354MODULE_LICENSE("GPL");