Home | History | Annotate | Line # | Download | only in ata
ld_ataraid.c revision 1.16
      1 /*	$NetBSD: ld_ataraid.c,v 1.16 2006/05/14 21:42:26 elad 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.16 2006/05/14 21:42:26 elad 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, struct cfdata *match, void *aux)
    117 {
    118 
    119 	return (1);
    120 }
    121 
    122 static void
    123 ld_ataraid_attach(struct device *parent, struct device *self, void *aux)
    124 {
    125 	struct ld_ataraid_softc *sc = (void *) self;
    126 	struct ld_softc *ld = &sc->sc_ld;
    127 	struct ataraid_array_info *aai = aux;
    128 	const char *level;
    129 	struct vnode *vp;
    130 	char unklev[32];
    131 	u_int i;
    132 
    133 	if (ld_ataraid_initialized == 0) {
    134 		ld_ataraid_initialized = 1;
    135 		pool_init(&ld_ataraid_cbufpl, sizeof(struct cbuf), 0,
    136 		    0, 0, "ldcbuf", NULL);
    137 	}
    138 
    139 	sc->sc_aai = aai;	/* this data persists */
    140 
    141 	ld->sc_maxxfer = MAXPHYS * aai->aai_width;	/* XXX */
    142 	ld->sc_secperunit = aai->aai_capacity;
    143 	ld->sc_secsize = 512;				/* XXX */
    144 	ld->sc_maxqueuecnt = 128;			/* XXX */
    145 	ld->sc_dump = ld_ataraid_dump;
    146 
    147 	switch (aai->aai_level) {
    148 	case AAI_L_SPAN:
    149 		level = "SPAN";
    150 		ld->sc_start = ld_ataraid_start_span;
    151 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    152 		break;
    153 
    154 	case AAI_L_RAID0:
    155 		level = "RAID-0";
    156 		ld->sc_start = ld_ataraid_start_raid0;
    157 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    158 		break;
    159 
    160 	case AAI_L_RAID1:
    161 		level = "RAID-1";
    162 		ld->sc_start = ld_ataraid_start_raid0;
    163 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    164 		break;
    165 
    166 	case AAI_L_RAID0 | AAI_L_RAID1:
    167 		level = "RAID-10";
    168 		ld->sc_start = ld_ataraid_start_raid0;
    169 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    170 		break;
    171 
    172 	default:
    173 		snprintf(unklev, sizeof(unklev), "<unknown level 0x%x>",
    174 		    aai->aai_level);
    175 		level = unklev;
    176 	}
    177 
    178 	aprint_naive(": ATA %s array\n", level);
    179 	aprint_normal(": %s ATA %s array\n",
    180 	    ata_raid_type_name(aai->aai_type), level);
    181 
    182 	if (ld->sc_start == NULL) {
    183 		aprint_error("%s: unsupported array type\n",
    184 		    ld->sc_dv.dv_xname);
    185 		return;
    186 	}
    187 
    188 	/*
    189 	 * We get a geometry from the device; use it.
    190 	 */
    191 	ld->sc_nheads = aai->aai_heads;
    192 	ld->sc_nsectors = aai->aai_sectors;
    193 	ld->sc_ncylinders = aai->aai_cylinders;
    194 
    195 	/*
    196 	 * Configure all the component disks.
    197 	 */
    198 	for (i = 0; i < aai->aai_ndisks; i++) {
    199 		struct ataraid_disk_info *adi = &aai->aai_disks[i];
    200 		int bmajor, error;
    201 		dev_t dev;
    202 
    203 		bmajor = devsw_name2blk(adi->adi_dev->dv_xname, NULL, 0);
    204 		dev = MAKEDISKDEV(bmajor, device_unit(adi->adi_dev), RAW_PART);
    205 		error = bdevvp(dev, &vp);
    206 		if (error)
    207 			break;
    208 		error = VOP_OPEN(vp, FREAD|FWRITE, NOCRED, 0);
    209 		if (error) {
    210 			vput(vp);
    211 			/*
    212 			 * XXX This is bogus.  We should just mark the
    213 			 * XXX component as FAILED, and write-back new
    214 			 * XXX config blocks.
    215 			 */
    216 			break;
    217 		}
    218 
    219 		VOP_UNLOCK(vp, 0);
    220 		sc->sc_vnodes[i] = vp;
    221 	}
    222 	if (i == aai->aai_ndisks) {
    223 		ld->sc_flags = LDF_ENABLED;
    224 		goto finish;
    225 	}
    226 
    227 	for (i = 0; i < aai->aai_ndisks; i++) {
    228 		vp = sc->sc_vnodes[i];
    229 		sc->sc_vnodes[i] = NULL;
    230 		if (vp != NULL)
    231 			(void) vn_close(vp, FREAD|FWRITE, NOCRED, curlwp);
    232 	}
    233 
    234  finish:
    235 	ldattach(ld);
    236 }
    237 
    238 static struct cbuf *
    239 ld_ataraid_make_cbuf(struct ld_ataraid_softc *sc, struct buf *bp,
    240     u_int comp, daddr_t bn, caddr_t addr, long bcount)
    241 {
    242 	struct cbuf *cbp;
    243 
    244 	cbp = CBUF_GET();
    245 	if (cbp == NULL)
    246 		return (NULL);
    247 	BUF_INIT(&cbp->cb_buf);
    248 	cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
    249 	cbp->cb_buf.b_iodone = sc->sc_iodone;
    250 	cbp->cb_buf.b_proc = bp->b_proc;
    251 	cbp->cb_buf.b_vp = sc->sc_vnodes[comp];
    252 	cbp->cb_buf.b_blkno = bn + sc->sc_aai->aai_offset;
    253 	cbp->cb_buf.b_data = addr;
    254 	cbp->cb_buf.b_bcount = bcount;
    255 
    256 	/* Context for iodone */
    257 	cbp->cb_obp = bp;
    258 	cbp->cb_sc = sc;
    259 	cbp->cb_comp = comp;
    260 	cbp->cb_other = NULL;
    261 	cbp->cb_flags = 0;
    262 
    263 	return (cbp);
    264 }
    265 
    266 static int
    267 ld_ataraid_start_span(struct ld_softc *ld, struct buf *bp)
    268 {
    269 	struct ld_ataraid_softc *sc = (void *) ld;
    270 	struct ataraid_array_info *aai = sc->sc_aai;
    271 	struct ataraid_disk_info *adi;
    272 	SIMPLEQ_HEAD(, cbuf) cbufq;
    273 	struct cbuf *cbp;
    274 	caddr_t addr;
    275 	daddr_t bn;
    276 	long bcount, rcount;
    277 	u_int comp;
    278 
    279 	/* Allocate component buffers. */
    280 	SIMPLEQ_INIT(&cbufq);
    281 	addr = bp->b_data;
    282 
    283 	/* Find the first component. */
    284 	comp = 0;
    285 	adi = &aai->aai_disks[comp];
    286 	bn = bp->b_rawblkno;
    287 	while (bn >= adi->adi_compsize) {
    288 		bn -= adi->adi_compsize;
    289 		adi = &aai->aai_disks[++comp];
    290 	}
    291 
    292 	bp->b_resid = bp->b_bcount;
    293 
    294 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    295 		rcount = bp->b_bcount;
    296 		if ((adi->adi_compsize - bn) < btodb(rcount))
    297 			rcount = dbtob(adi->adi_compsize - bn);
    298 
    299 		cbp = ld_ataraid_make_cbuf(sc, bp, comp, bn, addr, rcount);
    300 		if (cbp == NULL) {
    301 			/* Free the already allocated component buffers. */
    302 			while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    303 				SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    304 				CBUF_PUT(cbp);
    305 			}
    306 			return (EAGAIN);
    307 		}
    308 
    309 		/*
    310 		 * For a span, we always know we advance to the next disk,
    311 		 * and always start at offset 0 on that disk.
    312 		 */
    313 		adi = &aai->aai_disks[++comp];
    314 		bn = 0;
    315 
    316 		SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    317 		addr += rcount;
    318 	}
    319 
    320 	/* Now fire off the requests. */
    321 	while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    322 		SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    323 		if ((cbp->cb_buf.b_flags & B_READ) == 0)
    324 			cbp->cb_buf.b_vp->v_numoutput++;
    325 		VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
    326 	}
    327 
    328 	return (0);
    329 }
    330 
    331 static int
    332 ld_ataraid_start_raid0(struct ld_softc *ld, struct buf *bp)
    333 {
    334 	struct ld_ataraid_softc *sc = (void *) ld;
    335 	struct ataraid_array_info *aai = sc->sc_aai;
    336 	struct ataraid_disk_info *adi;
    337 	SIMPLEQ_HEAD(, cbuf) cbufq;
    338 	struct cbuf *cbp, *other_cbp;
    339 	caddr_t addr;
    340 	daddr_t bn, cbn, tbn, off;
    341 	long bcount, rcount;
    342 	u_int comp;
    343 	const int read = bp->b_flags & B_READ;
    344 	const int mirror = aai->aai_level & AAI_L_RAID1;
    345 	int error;
    346 
    347 	/* Allocate component buffers. */
    348 	SIMPLEQ_INIT(&cbufq);
    349 	addr = bp->b_data;
    350 	bn = bp->b_rawblkno;
    351 
    352 	bp->b_resid = bp->b_bcount;
    353 
    354 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    355 		tbn = bn / aai->aai_interleave;
    356 		off = bn % aai->aai_interleave;
    357 
    358 		if (__predict_false(tbn == aai->aai_capacity /
    359 					   aai->aai_interleave)) {
    360 			/* Last stripe. */
    361 			daddr_t sz = (aai->aai_capacity -
    362 				      (tbn * aai->aai_interleave)) /
    363 				     aai->aai_width;
    364 			comp = off / sz;
    365 			cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
    366 			    (off % sz);
    367 			rcount = min(bcount, dbtob(sz));
    368 		} else {
    369 			comp = tbn % aai->aai_width;
    370 			cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
    371 			    off;
    372 			rcount = min(bcount, dbtob(aai->aai_interleave - off));
    373 		}
    374 
    375 		/*
    376 		 * See if a component is valid.
    377 		 */
    378 try_mirror:
    379 		adi = &aai->aai_disks[comp];
    380 		if ((adi->adi_status & ADI_S_ONLINE) == 0) {
    381 			if (mirror && comp < aai->aai_width) {
    382 				comp += aai->aai_width;
    383 				goto try_mirror;
    384 			}
    385 
    386 			/*
    387 			 * No component available.
    388 			 */
    389 			error = EIO;
    390 			goto free_and_exit;
    391 		}
    392 
    393 		cbp = ld_ataraid_make_cbuf(sc, bp, comp, cbn, addr, rcount);
    394 		if (cbp == NULL) {
    395 resource_shortage:
    396 			error = EAGAIN;
    397 free_and_exit:
    398 			/* Free the already allocated component buffers. */
    399 			while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    400 				SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    401 				CBUF_PUT(cbp);
    402 			}
    403 			return (error);
    404 		}
    405 		SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    406 		if (mirror && !read && comp < aai->aai_width) {
    407 			comp += aai->aai_width;
    408 			adi = &aai->aai_disks[comp];
    409 			if (adi->adi_status & ADI_S_ONLINE) {
    410 				other_cbp = ld_ataraid_make_cbuf(sc, bp,
    411 				    comp, cbn, addr, rcount);
    412 				if (other_cbp == NULL)
    413 					goto resource_shortage;
    414 				SIMPLEQ_INSERT_TAIL(&cbufq, other_cbp, cb_q);
    415 				other_cbp->cb_other = cbp;
    416 				cbp->cb_other = other_cbp;
    417 			}
    418 		}
    419 		bn += btodb(rcount);
    420 		addr += rcount;
    421 	}
    422 
    423 	/* Now fire off the requests. */
    424 	while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    425 		SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    426 		if ((cbp->cb_buf.b_flags & B_READ) == 0)
    427 			cbp->cb_buf.b_vp->v_numoutput++;
    428 		VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
    429 	}
    430 
    431 	return (0);
    432 }
    433 
    434 /*
    435  * Called at interrupt time.  Mark the component as done and if all
    436  * components are done, take an "interrupt".
    437  */
    438 static void
    439 ld_ataraid_iodone_raid0(struct buf *vbp)
    440 {
    441 	struct cbuf *cbp = (struct cbuf *) vbp, *other_cbp;
    442 	struct buf *bp = cbp->cb_obp;
    443 	struct ld_ataraid_softc *sc = cbp->cb_sc;
    444 	struct ataraid_array_info *aai = sc->sc_aai;
    445 	struct ataraid_disk_info *adi;
    446 	long count;
    447 	int s, iodone;
    448 
    449 	s = splbio();
    450 
    451 	iodone = cbp->cb_flags & CBUF_IODONE;
    452 	other_cbp = cbp->cb_other;
    453 	if (other_cbp != NULL)
    454 		/* You are alone */
    455 		other_cbp->cb_other = NULL;
    456 
    457 	if (cbp->cb_buf.b_flags & B_ERROR) {
    458 		/*
    459 		 * Mark this component broken.
    460 		 */
    461 		adi = &aai->aai_disks[cbp->cb_comp];
    462 		adi->adi_status &= ~ADI_S_ONLINE;
    463 
    464 		printf("%s: error %d on component %d (%s)\n",
    465 		    sc->sc_ld.sc_dv.dv_xname, bp->b_error, cbp->cb_comp,
    466 		    adi->adi_dev->dv_xname);
    467 
    468 		/*
    469 		 * If we didn't see an error yet and we are reading
    470 		 * RAID1 disk, try another component.
    471 		 */
    472 		if ((bp->b_flags & B_ERROR) == 0 &&
    473 		    (cbp->cb_buf.b_flags & B_READ) != 0 &&
    474 		    (aai->aai_level & AAI_L_RAID1) != 0 &&
    475 		    cbp->cb_comp < aai->aai_width) {
    476 			cbp->cb_comp += aai->aai_width;
    477 			adi = &aai->aai_disks[cbp->cb_comp];
    478 			if (adi->adi_status & ADI_S_ONLINE) {
    479 				cbp->cb_buf.b_flags &= ~B_ERROR;
    480 				VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
    481 				goto out;
    482 			}
    483 		}
    484 
    485 		if (iodone || other_cbp != NULL)
    486 			/*
    487 			 * If I/O on other component successfully done
    488 			 * or the I/O is still in progress, no need
    489 			 * to tell an error to upper layer.
    490 			 */
    491 			;
    492 		else {
    493 			bp->b_flags |= B_ERROR;
    494 			bp->b_error = cbp->cb_buf.b_error ?
    495 			    cbp->cb_buf.b_error : EIO;
    496 		}
    497 
    498 		/* XXX Update component config blocks. */
    499 
    500 	} else {
    501 		/*
    502 		 * If other I/O is still in progress, tell it that
    503 		 * our I/O is successfully done.
    504 		 */
    505 		if (other_cbp != NULL)
    506 			other_cbp->cb_flags |= CBUF_IODONE;
    507 	}
    508 	count = cbp->cb_buf.b_bcount;
    509 	CBUF_PUT(cbp);
    510 
    511 	if (other_cbp != NULL)
    512 		goto out;
    513 
    514 	/* If all done, "interrupt". */
    515 	bp->b_resid -= count;
    516 	if (bp->b_resid < 0)
    517 		panic("ld_ataraid_iodone_raid0: count");
    518 	if (bp->b_resid == 0)
    519 		lddone(&sc->sc_ld, bp);
    520 
    521 out:
    522 	splx(s);
    523 }
    524 
    525 static int
    526 ld_ataraid_dump(struct ld_softc *sc, void *data, int blkno, int blkcnt)
    527 {
    528 
    529 	return (EIO);
    530 }
    531