Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / drivers / md / dm-emc.c
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
10 #include "dm.h"
11 #include "dm-hw-handler.h"
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_cmnd.h>
14
15 #define DM_MSG_PREFIX "multipath emc"
16
17 struct 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
35 static inline void free_bio(struct bio *bio)
36 {
37         __free_page(bio->bi_io_vec[0].bv_page);
38         bio_put(bio);
39 }
40
41 static int emc_endio(struct bio *bio, unsigned int bytes_done, int error)
42 {
43         struct path *path = bio->bi_private;
44
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
64 static struct bio *get_failover_bio(struct path *path, unsigned data_size)
65 {
66         struct bio *bio;
67         struct page *page;
68
69         bio = bio_alloc(GFP_ATOMIC, 1);
70         if (!bio) {
71                 DMERR("get_failover_bio: bio_alloc() failed.");
72                 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) {
83                 DMERR("get_failover_bio: alloc_page() failed.");
84                 bio_put(bio);
85                 return NULL;
86         }
87
88         if (bio_add_page(bio, page, data_size, 0) != data_size) {
89                 DMERR("get_failover_bio: alloc_page() failed.");
90                 __free_page(page);
91                 bio_put(bio);
92                 return NULL;
93         }
94
95         return bio;
96 }
97
98 static struct request *get_failover_req(struct emc_handler *h,
99                                         struct bio *bio, struct path *path)
100 {
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) {
108                 DMERR("get_failover_req: blk_get_request failed");
109                 return NULL;
110         }
111
112         rq->bio = rq->biotail = bio;
113         blk_rq_bio_prep(q, rq, bio);
114
115         rq->rq_disk = bdev->bd_contains->bd_disk;
116
117         /* bio backed don't set data */
118         rq->buffer = rq->data = NULL;
119         /* rq data_len used for pc cmd's request_bufflen */
120         rq->data_len = bio->bi_size;
121
122         rq->sense = h->sense;
123         memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
124         rq->sense_len = 0;
125
126         memset(&rq->cmd, 0, BLK_MAX_CDB);
127
128         rq->timeout = EMC_FAILOVER_TIMEOUT;
129         rq->cmd_type = REQ_TYPE_BLOCK_PC;
130         rq->cmd_flags |= REQ_FAILFAST | REQ_NOMERGE;
131
132         return rq;
133 }
134
135 static struct request *emc_trespass_get(struct emc_handler *h,
136                                         struct path *path)
137 {
138         struct bio *bio;
139         struct request *rq;
140         unsigned char *page22;
141         unsigned char long_trespass_pg[] = {
142                 0, 0, 0, 0,
143                 TRESPASS_PAGE,        /* Page code */
144                 0x09,                 /* Page length - 2 */
145                 h->hr ? 0x01 : 0x81,  /* Trespass code + Honor reservation bit */
146                 0xff, 0xff,           /* Trespass target */
147                 0, 0, 0, 0, 0, 0      /* Reserved bytes / unknown */
148                 };
149         unsigned char short_trespass_pg[] = {
150                 0, 0, 0, 0,
151                 TRESPASS_PAGE,        /* Page code */
152                 0x02,                 /* Page length - 2 */
153                 h->hr ? 0x01 : 0x81,  /* Trespass code + Honor reservation bit */
154                 0xff,                 /* Trespass target */
155                 };
156         unsigned data_size = h->short_trespass ? sizeof(short_trespass_pg) :
157                                 sizeof(long_trespass_pg);
158
159         /* get bio backing */
160         if (data_size > PAGE_SIZE)
161                 /* this should never happen */
162                 return NULL;
163
164         bio = get_failover_bio(path, data_size);
165         if (!bio) {
166                 DMERR("emc_trespass_get: no bio");
167                 return NULL;
168         }
169
170         page22 = (unsigned char *)bio_data(bio);
171         memset(page22, 0, data_size);
172
173         memcpy(page22, h->short_trespass ?
174                 short_trespass_pg : long_trespass_pg, data_size);
175
176         /* get request for block layer packet command */
177         rq = get_failover_req(h, bio, path);
178         if (!rq) {
179                 DMERR("emc_trespass_get: no rq");
180                 free_bio(bio);
181                 return NULL;
182         }
183
184         /* Prepare the command. */
185         rq->cmd[0] = MODE_SELECT;
186         rq->cmd[1] = 0x10;
187         rq->cmd[4] = data_size;
188         rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
189
190         return rq;
191 }
192
193 static void emc_pg_init(struct hw_handler *hwh, unsigned bypassed,
194                         struct path *path)
195 {
196         struct request *rq;
197         struct request_queue *q = bdev_get_queue(path->dev->bdev);
198
199         /*
200          * We can either blindly init the pg (then look at the sense),
201          * or we can send some commands to get the state here (then
202          * possibly send the fo cmnd), or we can also have the
203          * initial state passed into us and then get an update here.
204          */
205         if (!q) {
206                 DMINFO("emc_pg_init: no queue");
207                 goto fail_path;
208         }
209
210         /* FIXME: The request should be pre-allocated. */
211         rq = emc_trespass_get(hwh->context, path);
212         if (!rq) {
213                 DMERR("emc_pg_init: no rq");
214                 goto fail_path;
215         }
216
217         DMINFO("emc_pg_init: sending switch-over command");
218         elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
219         return;
220
221 fail_path:
222         dm_pg_init_complete(path, MP_FAIL_PATH);
223 }
224
225 static struct emc_handler *alloc_emc_handler(void)
226 {
227         struct emc_handler *h = kmalloc(sizeof(*h), GFP_KERNEL);
228
229         if (h) {
230                 memset(h, 0, sizeof(*h));
231                 spin_lock_init(&h->lock);
232         }
233
234         return h;
235 }
236
237 static int emc_create(struct hw_handler *hwh, unsigned argc, char **argv)
238 {
239         struct emc_handler *h;
240         unsigned hr, short_trespass;
241
242         if (argc == 0) {
243                 /* No arguments: use defaults */
244                 hr = 0;
245                 short_trespass = 0;
246         } else if (argc != 2) {
247                 DMWARN("incorrect number of arguments");
248                 return -EINVAL;
249         } else {
250                 if ((sscanf(argv[0], "%u", &short_trespass) != 1)
251                         || (short_trespass > 1)) {
252                         DMWARN("invalid trespass mode selected");
253                         return -EINVAL;
254                 }
255
256                 if ((sscanf(argv[1], "%u", &hr) != 1)
257                         || (hr > 1)) {
258                         DMWARN("invalid honor reservation flag selected");
259                         return -EINVAL;
260                 }
261         }
262
263         h = alloc_emc_handler();
264         if (!h)
265                 return -ENOMEM;
266
267         hwh->context = h;
268
269         if ((h->short_trespass = short_trespass))
270                 DMWARN("short trespass command will be send");
271         else
272                 DMWARN("long trespass command will be send");
273
274         if ((h->hr = hr))
275                 DMWARN("honor reservation bit will be set");
276         else
277                 DMWARN("honor reservation bit will not be set (default)");
278
279         return 0;
280 }
281
282 static void emc_destroy(struct hw_handler *hwh)
283 {
284         struct emc_handler *h = (struct emc_handler *) hwh->context;
285
286         kfree(h);
287         hwh->context = NULL;
288 }
289
290 static unsigned emc_error(struct hw_handler *hwh, struct bio *bio)
291 {
292         /* FIXME: Patch from axboe still missing */
293 #if 0
294         int sense;
295
296         if (bio->bi_error & BIO_SENSE) {
297                 sense = bio->bi_error & 0xffffff; /* sense key / asc / ascq */
298
299                 if (sense == 0x020403) {
300                         /* LUN Not Ready - Manual Intervention Required
301                          * indicates this is a passive path.
302                          *
303                          * FIXME: However, if this is seen and EVPD C0
304                          * indicates that this is due to a NDU in
305                          * progress, we should set FAIL_PATH too.
306                          * This indicates we might have to do a SCSI
307                          * inquiry in the end_io path. Ugh. */
308                         return MP_BYPASS_PG | MP_RETRY_IO;
309                 } else if (sense == 0x052501) {
310                         /* An array based copy is in progress. Do not
311                          * fail the path, do not bypass to another PG,
312                          * do not retry. Fail the IO immediately.
313                          * (Actually this is the same conclusion as in
314                          * the default handler, but lets make sure.) */
315                         return 0;
316                 } else if (sense == 0x062900) {
317                         /* Unit Attention Code. This is the first IO
318                          * to the new path, so just retry. */
319                         return MP_RETRY_IO;
320                 }
321         }
322 #endif
323
324         /* Try default handler */
325         return dm_scsi_err_handler(hwh, bio);
326 }
327
328 static struct hw_handler_type emc_hwh = {
329         .name = "emc",
330         .module = THIS_MODULE,
331         .create = emc_create,
332         .destroy = emc_destroy,
333         .pg_init = emc_pg_init,
334         .error = emc_error,
335 };
336
337 static int __init dm_emc_init(void)
338 {
339         int r = dm_register_hw_handler(&emc_hwh);
340
341         if (r < 0)
342                 DMERR("register failed %d", r);
343
344         DMINFO("version 0.0.3 loaded");
345
346         return r;
347 }
348
349 static void __exit dm_emc_exit(void)
350 {
351         int r = dm_unregister_hw_handler(&emc_hwh);
352
353         if (r < 0)
354                 DMERR("unregister failed %d", r);
355 }
356
357 module_init(dm_emc_init);
358 module_exit(dm_emc_exit);
359
360 MODULE_DESCRIPTION(DM_NAME " EMC CX/AX/FC-family multipath");
361 MODULE_AUTHOR("Lars Marowsky-Bree <lmb@suse.de>");
362 MODULE_LICENSE("GPL");