Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-emc.c
blob7006586645943b81d6b21bd7281b88380c62201a
1 /*
2 * Copyright (C) 2004 SUSE LINUX Products GmbH. All rights reserved.
3 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
7 * Multipath support for EMC CLARiiON AX/CX-series hardware.
8 */
10 #include "dm.h"
11 #include "dm-hw-handler.h"
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_cmnd.h>
15 struct emc_handler {
16 spinlock_t lock;
18 /* Whether we should send the short trespass command (FC-series)
19 * or the long version (default for AX/CX CLARiiON arrays). */
20 unsigned short_trespass;
21 /* Whether or not to honor SCSI reservations when initiating a
22 * switch-over. Default: Don't. */
23 unsigned hr;
25 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
28 #define TRESPASS_PAGE 0x22
29 #define EMC_FAILOVER_TIMEOUT (60 * HZ)
31 /* Code borrowed from dm-lsi-rdac by Mike Christie */
33 static inline void free_bio(struct bio *bio)
35 __free_page(bio->bi_io_vec[0].bv_page);
36 bio_put(bio);
39 static int emc_endio(struct bio *bio, unsigned int bytes_done, int error)
41 struct path *path = bio->bi_private;
43 if (bio->bi_size)
44 return 1;
46 /* We also need to look at the sense keys here whether or not to
47 * switch to the next PG etc.
49 * For now simple logic: either it works or it doesn't.
51 if (error)
52 dm_pg_init_complete(path, MP_FAIL_PATH);
53 else
54 dm_pg_init_complete(path, 0);
56 /* request is freed in block layer */
57 free_bio(bio);
59 return 0;
62 static struct bio *get_failover_bio(struct path *path, unsigned data_size)
64 struct bio *bio;
65 struct page *page;
67 bio = bio_alloc(GFP_ATOMIC, 1);
68 if (!bio) {
69 DMERR("dm-emc: get_failover_bio: bio_alloc() failed.");
70 return NULL;
73 bio->bi_rw |= (1 << BIO_RW);
74 bio->bi_bdev = path->dev->bdev;
75 bio->bi_sector = 0;
76 bio->bi_private = path;
77 bio->bi_end_io = emc_endio;
79 page = alloc_page(GFP_ATOMIC);
80 if (!page) {
81 DMERR("dm-emc: get_failover_bio: alloc_page() failed.");
82 bio_put(bio);
83 return NULL;
86 if (bio_add_page(bio, page, data_size, 0) != data_size) {
87 DMERR("dm-emc: get_failover_bio: alloc_page() failed.");
88 __free_page(page);
89 bio_put(bio);
90 return NULL;
93 return bio;
96 static struct request *get_failover_req(struct emc_handler *h,
97 struct bio *bio, struct path *path)
99 struct request *rq;
100 struct block_device *bdev = bio->bi_bdev;
101 struct request_queue *q = bdev_get_queue(bdev);
103 /* FIXME: Figure out why it fails with GFP_ATOMIC. */
104 rq = blk_get_request(q, WRITE, __GFP_WAIT);
105 if (!rq) {
106 DMERR("dm-emc: get_failover_req: blk_get_request failed");
107 return NULL;
110 rq->bio = rq->biotail = bio;
111 blk_rq_bio_prep(q, rq, bio);
113 rq->rq_disk = bdev->bd_contains->bd_disk;
115 /* bio backed don't set data */
116 rq->buffer = rq->data = NULL;
117 /* rq data_len used for pc cmd's request_bufflen */
118 rq->data_len = bio->bi_size;
120 rq->sense = h->sense;
121 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
122 rq->sense_len = 0;
124 memset(&rq->cmd, 0, BLK_MAX_CDB);
126 rq->timeout = EMC_FAILOVER_TIMEOUT;
127 rq->flags |= (REQ_BLOCK_PC | REQ_FAILFAST | REQ_NOMERGE);
129 return rq;
132 static struct request *emc_trespass_get(struct emc_handler *h,
133 struct path *path)
135 struct bio *bio;
136 struct request *rq;
137 unsigned char *page22;
138 unsigned char long_trespass_pg[] = {
139 0, 0, 0, 0,
140 TRESPASS_PAGE, /* Page code */
141 0x09, /* Page length - 2 */
142 h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
143 0xff, 0xff, /* Trespass target */
144 0, 0, 0, 0, 0, 0 /* Reserved bytes / unknown */
146 unsigned char short_trespass_pg[] = {
147 0, 0, 0, 0,
148 TRESPASS_PAGE, /* Page code */
149 0x02, /* Page length - 2 */
150 h->hr ? 0x01 : 0x81, /* Trespass code + Honor reservation bit */
151 0xff, /* Trespass target */
153 unsigned data_size = h->short_trespass ? sizeof(short_trespass_pg) :
154 sizeof(long_trespass_pg);
156 /* get bio backing */
157 if (data_size > PAGE_SIZE)
158 /* this should never happen */
159 return NULL;
161 bio = get_failover_bio(path, data_size);
162 if (!bio) {
163 DMERR("dm-emc: emc_trespass_get: no bio");
164 return NULL;
167 page22 = (unsigned char *)bio_data(bio);
168 memset(page22, 0, data_size);
170 memcpy(page22, h->short_trespass ?
171 short_trespass_pg : long_trespass_pg, data_size);
173 /* get request for block layer packet command */
174 rq = get_failover_req(h, bio, path);
175 if (!rq) {
176 DMERR("dm-emc: emc_trespass_get: no rq");
177 free_bio(bio);
178 return NULL;
181 /* Prepare the command. */
182 rq->cmd[0] = MODE_SELECT;
183 rq->cmd[1] = 0x10;
184 rq->cmd[4] = data_size;
185 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
187 return rq;
190 static void emc_pg_init(struct hw_handler *hwh, unsigned bypassed,
191 struct path *path)
193 struct request *rq;
194 struct request_queue *q = bdev_get_queue(path->dev->bdev);
197 * We can either blindly init the pg (then look at the sense),
198 * or we can send some commands to get the state here (then
199 * possibly send the fo cmnd), or we can also have the
200 * initial state passed into us and then get an update here.
202 if (!q) {
203 DMINFO("dm-emc: emc_pg_init: no queue");
204 goto fail_path;
207 /* FIXME: The request should be pre-allocated. */
208 rq = emc_trespass_get(hwh->context, path);
209 if (!rq) {
210 DMERR("dm-emc: emc_pg_init: no rq");
211 goto fail_path;
214 DMINFO("dm-emc: emc_pg_init: sending switch-over command");
215 elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
216 return;
218 fail_path:
219 dm_pg_init_complete(path, MP_FAIL_PATH);
222 static struct emc_handler *alloc_emc_handler(void)
224 struct emc_handler *h = kmalloc(sizeof(*h), GFP_KERNEL);
226 if (h)
227 spin_lock_init(&h->lock);
229 return h;
232 static int emc_create(struct hw_handler *hwh, unsigned argc, char **argv)
234 struct emc_handler *h;
235 unsigned hr, short_trespass;
237 if (argc == 0) {
238 /* No arguments: use defaults */
239 hr = 0;
240 short_trespass = 0;
241 } else if (argc != 2) {
242 DMWARN("dm-emc hwhandler: incorrect number of arguments");
243 return -EINVAL;
244 } else {
245 if ((sscanf(argv[0], "%u", &short_trespass) != 1)
246 || (short_trespass > 1)) {
247 DMWARN("dm-emc: invalid trespass mode selected");
248 return -EINVAL;
251 if ((sscanf(argv[1], "%u", &hr) != 1)
252 || (hr > 1)) {
253 DMWARN("dm-emc: invalid honor reservation flag selected");
254 return -EINVAL;
258 h = alloc_emc_handler();
259 if (!h)
260 return -ENOMEM;
262 memset(h, 0, sizeof(*h));
264 hwh->context = h;
266 if ((h->short_trespass = short_trespass))
267 DMWARN("dm-emc: short trespass command will be send");
268 else
269 DMWARN("dm-emc: long trespass command will be send");
271 if ((h->hr = hr))
272 DMWARN("dm-emc: honor reservation bit will be set");
273 else
274 DMWARN("dm-emc: honor reservation bit will not be set (default)");
276 return 0;
279 static void emc_destroy(struct hw_handler *hwh)
281 struct emc_handler *h = (struct emc_handler *) hwh->context;
283 kfree(h);
284 hwh->context = NULL;
287 static unsigned emc_error(struct hw_handler *hwh, struct bio *bio)
289 /* FIXME: Patch from axboe still missing */
290 #if 0
291 int sense;
293 if (bio->bi_error & BIO_SENSE) {
294 sense = bio->bi_error & 0xffffff; /* sense key / asc / ascq */
296 if (sense == 0x020403) {
297 /* LUN Not Ready - Manual Intervention Required
298 * indicates this is a passive path.
300 * FIXME: However, if this is seen and EVPD C0
301 * indicates that this is due to a NDU in
302 * progress, we should set FAIL_PATH too.
303 * This indicates we might have to do a SCSI
304 * inquiry in the end_io path. Ugh. */
305 return MP_BYPASS_PG | MP_RETRY_IO;
306 } else if (sense == 0x052501) {
307 /* An array based copy is in progress. Do not
308 * fail the path, do not bypass to another PG,
309 * do not retry. Fail the IO immediately.
310 * (Actually this is the same conclusion as in
311 * the default handler, but lets make sure.) */
312 return 0;
313 } else if (sense == 0x062900) {
314 /* Unit Attention Code. This is the first IO
315 * to the new path, so just retry. */
316 return MP_RETRY_IO;
319 #endif
321 /* Try default handler */
322 return dm_scsi_err_handler(hwh, bio);
325 static struct hw_handler_type emc_hwh = {
326 .name = "emc",
327 .module = THIS_MODULE,
328 .create = emc_create,
329 .destroy = emc_destroy,
330 .pg_init = emc_pg_init,
331 .error = emc_error,
334 static int __init dm_emc_init(void)
336 int r = dm_register_hw_handler(&emc_hwh);
338 if (r < 0)
339 DMERR("emc: register failed %d", r);
341 DMINFO("dm-emc version 0.0.3 loaded");
343 return r;
346 static void __exit dm_emc_exit(void)
348 int r = dm_unregister_hw_handler(&emc_hwh);
350 if (r < 0)
351 DMERR("emc: unregister failed %d", r);
354 module_init(dm_emc_init);
355 module_exit(dm_emc_exit);
357 MODULE_DESCRIPTION(DM_NAME " EMC CX/AX/FC-family multipath");
358 MODULE_AUTHOR("Lars Marowsky-Bree <lmb@suse.de>");
359 MODULE_LICENSE("GPL");