ld_ataraid.c revision 1.26 1 /* $NetBSD: ld_ataraid.c,v 1.26 2008/04/05 22:04:36 cegger Exp $ */
2
3 /*
4 * Copyright (c) 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Support for ATA RAID logical disks.
40 *
41 * Note that all the RAID happens in software here; the ATA RAID
42 * controllers we're dealing with (Promise, etc.) only support
43 * configuration data on the component disks, with the BIOS supporting
44 * booting from the RAID volumes.
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ld_ataraid.c,v 1.26 2008/04/05 22:04:36 cegger Exp $");
49
50 #include "rnd.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/conf.h>
55 #include <sys/kernel.h>
56 #include <sys/device.h>
57 #include <sys/buf.h>
58 #include <sys/bufq.h>
59 #include <sys/dkio.h>
60 #include <sys/disk.h>
61 #include <sys/disklabel.h>
62 #include <sys/fcntl.h>
63 #include <sys/malloc.h>
64 #include <sys/vnode.h>
65 #include <sys/kauth.h>
66 #if NRND > 0
67 #include <sys/rnd.h>
68 #endif
69
70 #include <miscfs/specfs/specdev.h>
71
72 #include <dev/ldvar.h>
73
74 #include <dev/ata/ata_raidvar.h>
75
76 struct ld_ataraid_softc {
77 struct ld_softc sc_ld;
78
79 struct ataraid_array_info *sc_aai;
80 struct vnode *sc_vnodes[ATA_RAID_MAX_DISKS];
81
82 void (*sc_iodone)(struct buf *);
83 };
84
85 static int ld_ataraid_match(struct device *, struct cfdata *, void *);
86 static void ld_ataraid_attach(struct device *, struct device *, void *);
87
88 static int ld_ataraid_dump(struct ld_softc *, void *, int, int);
89
90 static int ld_ataraid_start_span(struct ld_softc *, struct buf *);
91
92 static int ld_ataraid_start_raid0(struct ld_softc *, struct buf *);
93 static void ld_ataraid_iodone_raid0(struct buf *);
94
95 CFATTACH_DECL(ld_ataraid, sizeof(struct ld_ataraid_softc),
96 ld_ataraid_match, ld_ataraid_attach, NULL, NULL);
97
98 static int ld_ataraid_initialized;
99 static struct pool ld_ataraid_cbufpl;
100
101 struct cbuf {
102 struct buf cb_buf; /* new I/O buf */
103 struct buf *cb_obp; /* ptr. to original I/O buf */
104 struct ld_ataraid_softc *cb_sc; /* pointer to ld softc */
105 u_int cb_comp; /* target component */
106 SIMPLEQ_ENTRY(cbuf) cb_q; /* fifo of component buffers */
107 struct cbuf *cb_other; /* other cbuf in case of mirror */
108 int cb_flags;
109 #define CBUF_IODONE 0x00000001 /* I/O is already successfully done */
110 };
111
112 #define CBUF_GET() pool_get(&ld_ataraid_cbufpl, PR_NOWAIT);
113 #define CBUF_PUT(cbp) pool_put(&ld_ataraid_cbufpl, (cbp))
114
115 static int
116 ld_ataraid_match(struct device *parent,
117 struct cfdata *match, void *aux)
118 {
119
120 return (1);
121 }
122
123 static void
124 ld_ataraid_attach(struct device *parent, struct device *self,
125 void *aux)
126 {
127 struct ld_ataraid_softc *sc = (void *) self;
128 struct ld_softc *ld = &sc->sc_ld;
129 struct ataraid_array_info *aai = aux;
130 const char *level;
131 struct vnode *vp;
132 char unklev[32];
133 u_int i;
134
135 if (ld_ataraid_initialized == 0) {
136 ld_ataraid_initialized = 1;
137 pool_init(&ld_ataraid_cbufpl, sizeof(struct cbuf), 0,
138 0, 0, "ldcbuf", NULL, IPL_BIO);
139 }
140
141 sc->sc_aai = aai; /* this data persists */
142
143 ld->sc_maxxfer = MAXPHYS * aai->aai_width; /* XXX */
144 ld->sc_secperunit = aai->aai_capacity;
145 ld->sc_secsize = 512; /* XXX */
146 ld->sc_maxqueuecnt = 128; /* XXX */
147 ld->sc_dump = ld_ataraid_dump;
148
149 switch (aai->aai_level) {
150 case AAI_L_SPAN:
151 level = "SPAN";
152 ld->sc_start = ld_ataraid_start_span;
153 sc->sc_iodone = ld_ataraid_iodone_raid0;
154 break;
155
156 case AAI_L_RAID0:
157 level = "RAID-0";
158 ld->sc_start = ld_ataraid_start_raid0;
159 sc->sc_iodone = ld_ataraid_iodone_raid0;
160 break;
161
162 case AAI_L_RAID1:
163 level = "RAID-1";
164 ld->sc_start = ld_ataraid_start_raid0;
165 sc->sc_iodone = ld_ataraid_iodone_raid0;
166 break;
167
168 case AAI_L_RAID0 | AAI_L_RAID1:
169 level = "RAID-10";
170 ld->sc_start = ld_ataraid_start_raid0;
171 sc->sc_iodone = ld_ataraid_iodone_raid0;
172 break;
173
174 default:
175 snprintf(unklev, sizeof(unklev), "<unknown level 0x%x>",
176 aai->aai_level);
177 level = unklev;
178 }
179
180 aprint_naive(": ATA %s array\n", level);
181 aprint_normal(": %s ATA %s array\n",
182 ata_raid_type_name(aai->aai_type), level);
183
184 if (ld->sc_start == NULL) {
185 aprint_error_dev(&ld->sc_dv, "unsupported array type\n");
186 return;
187 }
188
189 /*
190 * We get a geometry from the device; use it.
191 */
192 ld->sc_nheads = aai->aai_heads;
193 ld->sc_nsectors = aai->aai_sectors;
194 ld->sc_ncylinders = aai->aai_cylinders;
195
196 /*
197 * Configure all the component disks.
198 */
199 for (i = 0; i < aai->aai_ndisks; i++) {
200 struct ataraid_disk_info *adi = &aai->aai_disks[i];
201 int bmajor, error;
202 dev_t dev;
203
204 bmajor = devsw_name2blk(device_xname(adi->adi_dev), NULL, 0);
205 dev = MAKEDISKDEV(bmajor, device_unit(adi->adi_dev), RAW_PART);
206 error = bdevvp(dev, &vp);
207 if (error)
208 break;
209 error = VOP_OPEN(vp, FREAD|FWRITE, NOCRED);
210 if (error) {
211 vput(vp);
212 /*
213 * XXX This is bogus. We should just mark the
214 * XXX component as FAILED, and write-back new
215 * XXX config blocks.
216 */
217 break;
218 }
219
220 VOP_UNLOCK(vp, 0);
221 sc->sc_vnodes[i] = vp;
222 }
223 if (i == aai->aai_ndisks) {
224 ld->sc_flags = LDF_ENABLED;
225 goto finish;
226 }
227
228 for (i = 0; i < aai->aai_ndisks; i++) {
229 vp = sc->sc_vnodes[i];
230 sc->sc_vnodes[i] = NULL;
231 if (vp != NULL)
232 (void) vn_close(vp, FREAD|FWRITE, NOCRED);
233 }
234
235 finish:
236 ldattach(ld);
237 }
238
239 static struct cbuf *
240 ld_ataraid_make_cbuf(struct ld_ataraid_softc *sc, struct buf *bp,
241 u_int comp, daddr_t bn, void *addr, long bcount)
242 {
243 struct cbuf *cbp;
244
245 cbp = CBUF_GET();
246 if (cbp == NULL)
247 return (NULL);
248 buf_init(&cbp->cb_buf);
249 cbp->cb_buf.b_flags = bp->b_flags;
250 cbp->cb_buf.b_oflags = bp->b_oflags;
251 cbp->cb_buf.b_cflags = bp->b_cflags;
252 cbp->cb_buf.b_iodone = sc->sc_iodone;
253 cbp->cb_buf.b_proc = bp->b_proc;
254 cbp->cb_buf.b_vp = sc->sc_vnodes[comp];
255 cbp->cb_buf.b_objlock = &sc->sc_vnodes[comp]->v_interlock;
256 cbp->cb_buf.b_blkno = bn + sc->sc_aai->aai_offset;
257 cbp->cb_buf.b_data = addr;
258 cbp->cb_buf.b_bcount = bcount;
259
260 /* Context for iodone */
261 cbp->cb_obp = bp;
262 cbp->cb_sc = sc;
263 cbp->cb_comp = comp;
264 cbp->cb_other = NULL;
265 cbp->cb_flags = 0;
266
267 return (cbp);
268 }
269
270 static int
271 ld_ataraid_start_span(struct ld_softc *ld, struct buf *bp)
272 {
273 struct ld_ataraid_softc *sc = (void *) ld;
274 struct ataraid_array_info *aai = sc->sc_aai;
275 struct ataraid_disk_info *adi;
276 SIMPLEQ_HEAD(, cbuf) cbufq;
277 struct cbuf *cbp;
278 char *addr;
279 daddr_t bn;
280 long bcount, rcount;
281 u_int comp;
282
283 /* Allocate component buffers. */
284 SIMPLEQ_INIT(&cbufq);
285 addr = bp->b_data;
286
287 /* Find the first component. */
288 comp = 0;
289 adi = &aai->aai_disks[comp];
290 bn = bp->b_rawblkno;
291 while (bn >= adi->adi_compsize) {
292 bn -= adi->adi_compsize;
293 adi = &aai->aai_disks[++comp];
294 }
295
296 bp->b_resid = bp->b_bcount;
297
298 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
299 rcount = bp->b_bcount;
300 if ((adi->adi_compsize - bn) < btodb(rcount))
301 rcount = dbtob(adi->adi_compsize - bn);
302
303 cbp = ld_ataraid_make_cbuf(sc, bp, comp, bn, addr, rcount);
304 if (cbp == NULL) {
305 /* Free the already allocated component buffers. */
306 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
307 SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
308 buf_destroy(&cbp->cb_buf);
309 CBUF_PUT(cbp);
310 }
311 return (EAGAIN);
312 }
313
314 /*
315 * For a span, we always know we advance to the next disk,
316 * and always start at offset 0 on that disk.
317 */
318 adi = &aai->aai_disks[++comp];
319 bn = 0;
320
321 SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
322 addr += rcount;
323 }
324
325 /* Now fire off the requests. */
326 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
327 SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
328 if ((cbp->cb_buf.b_flags & B_READ) == 0) {
329 mutex_enter(&cbp->cb_buf.b_vp->v_interlock);
330 cbp->cb_buf.b_vp->v_numoutput++;
331 mutex_exit(&cbp->cb_buf.b_vp->v_interlock);
332 }
333 VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
334 }
335
336 return (0);
337 }
338
339 static int
340 ld_ataraid_start_raid0(struct ld_softc *ld, struct buf *bp)
341 {
342 struct ld_ataraid_softc *sc = (void *) ld;
343 struct ataraid_array_info *aai = sc->sc_aai;
344 struct ataraid_disk_info *adi;
345 SIMPLEQ_HEAD(, cbuf) cbufq;
346 struct cbuf *cbp, *other_cbp;
347 char *addr;
348 daddr_t bn, cbn, tbn, off;
349 long bcount, rcount;
350 u_int comp;
351 const int read = bp->b_flags & B_READ;
352 const int mirror = aai->aai_level & AAI_L_RAID1;
353 int error;
354
355 /* Allocate component buffers. */
356 SIMPLEQ_INIT(&cbufq);
357 addr = bp->b_data;
358 bn = bp->b_rawblkno;
359
360 bp->b_resid = bp->b_bcount;
361
362 for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
363 tbn = bn / aai->aai_interleave;
364 off = bn % aai->aai_interleave;
365
366 if (__predict_false(tbn == aai->aai_capacity /
367 aai->aai_interleave)) {
368 /* Last stripe. */
369 daddr_t sz = (aai->aai_capacity -
370 (tbn * aai->aai_interleave)) /
371 aai->aai_width;
372 comp = off / sz;
373 cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
374 (off % sz);
375 rcount = min(bcount, dbtob(sz));
376 } else {
377 comp = tbn % aai->aai_width;
378 cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
379 off;
380 rcount = min(bcount, dbtob(aai->aai_interleave - off));
381 }
382
383 /*
384 * See if a component is valid.
385 */
386 try_mirror:
387 adi = &aai->aai_disks[comp];
388 if ((adi->adi_status & ADI_S_ONLINE) == 0) {
389 if (mirror && comp < aai->aai_width) {
390 comp += aai->aai_width;
391 goto try_mirror;
392 }
393
394 /*
395 * No component available.
396 */
397 error = EIO;
398 goto free_and_exit;
399 }
400
401 cbp = ld_ataraid_make_cbuf(sc, bp, comp, cbn, addr, rcount);
402 if (cbp == NULL) {
403 resource_shortage:
404 error = EAGAIN;
405 free_and_exit:
406 /* Free the already allocated component buffers. */
407 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
408 SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
409 buf_destroy(&cbp->cb_buf);
410 CBUF_PUT(cbp);
411 }
412 return (error);
413 }
414 SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
415 if (mirror && !read && comp < aai->aai_width) {
416 comp += aai->aai_width;
417 adi = &aai->aai_disks[comp];
418 if (adi->adi_status & ADI_S_ONLINE) {
419 other_cbp = ld_ataraid_make_cbuf(sc, bp,
420 comp, cbn, addr, rcount);
421 if (other_cbp == NULL)
422 goto resource_shortage;
423 SIMPLEQ_INSERT_TAIL(&cbufq, other_cbp, cb_q);
424 other_cbp->cb_other = cbp;
425 cbp->cb_other = other_cbp;
426 }
427 }
428 bn += btodb(rcount);
429 addr += rcount;
430 }
431
432 /* Now fire off the requests. */
433 while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
434 SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
435 if ((cbp->cb_buf.b_flags & B_READ) == 0) {
436 mutex_enter(&cbp->cb_buf.b_vp->v_interlock);
437 cbp->cb_buf.b_vp->v_numoutput++;
438 mutex_exit(&cbp->cb_buf.b_vp->v_interlock);
439 }
440 VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
441 }
442
443 return (0);
444 }
445
446 /*
447 * Called at interrupt time. Mark the component as done and if all
448 * components are done, take an "interrupt".
449 */
450 static void
451 ld_ataraid_iodone_raid0(struct buf *vbp)
452 {
453 struct cbuf *cbp = (struct cbuf *) vbp, *other_cbp;
454 struct buf *bp = cbp->cb_obp;
455 struct ld_ataraid_softc *sc = cbp->cb_sc;
456 struct ataraid_array_info *aai = sc->sc_aai;
457 struct ataraid_disk_info *adi;
458 long count;
459 int s, iodone;
460
461 s = splbio();
462
463 iodone = cbp->cb_flags & CBUF_IODONE;
464 other_cbp = cbp->cb_other;
465 if (other_cbp != NULL)
466 /* You are alone */
467 other_cbp->cb_other = NULL;
468
469 if (cbp->cb_buf.b_error != 0) {
470 /*
471 * Mark this component broken.
472 */
473 adi = &aai->aai_disks[cbp->cb_comp];
474 adi->adi_status &= ~ADI_S_ONLINE;
475
476 printf("%s: error %d on component %d (%s)\n",
477 device_xname(&sc->sc_ld.sc_dv), bp->b_error, cbp->cb_comp,
478 device_xname(adi->adi_dev));
479
480 /*
481 * If we didn't see an error yet and we are reading
482 * RAID1 disk, try another component.
483 */
484 if (bp->b_error == 0 &&
485 (cbp->cb_buf.b_flags & B_READ) != 0 &&
486 (aai->aai_level & AAI_L_RAID1) != 0 &&
487 cbp->cb_comp < aai->aai_width) {
488 cbp->cb_comp += aai->aai_width;
489 adi = &aai->aai_disks[cbp->cb_comp];
490 if (adi->adi_status & ADI_S_ONLINE) {
491 cbp->cb_buf.b_error = 0;
492 VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
493 goto out;
494 }
495 }
496
497 if (iodone || other_cbp != NULL)
498 /*
499 * If I/O on other component successfully done
500 * or the I/O is still in progress, no need
501 * to tell an error to upper layer.
502 */
503 ;
504 else {
505 bp->b_error = cbp->cb_buf.b_error ?
506 cbp->cb_buf.b_error : EIO;
507 }
508
509 /* XXX Update component config blocks. */
510
511 } else {
512 /*
513 * If other I/O is still in progress, tell it that
514 * our I/O is successfully done.
515 */
516 if (other_cbp != NULL)
517 other_cbp->cb_flags |= CBUF_IODONE;
518 }
519 count = cbp->cb_buf.b_bcount;
520 CBUF_PUT(cbp);
521
522 if (other_cbp != NULL)
523 goto out;
524
525 /* If all done, "interrupt". */
526 bp->b_resid -= count;
527 if (bp->b_resid < 0)
528 panic("ld_ataraid_iodone_raid0: count");
529 if (bp->b_resid == 0)
530 lddone(&sc->sc_ld, bp);
531
532 out:
533 splx(s);
534 }
535
536 static int
537 ld_ataraid_dump(struct ld_softc *sc, void *data,
538 int blkno, int blkcnt)
539 {
540
541 return (EIO);
542 }
543