Home | History | Annotate | Line # | Download | only in ata
ld_ataraid.c revision 1.1
      1 /*	$NetBSD: ld_ataraid.c,v 1.1 2003/01/27 18:21:29 thorpej 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 "rnd.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/conf.h>
     52 #include <sys/kernel.h>
     53 #include <sys/device.h>
     54 #include <sys/buf.h>
     55 #include <sys/dkio.h>
     56 #include <sys/disk.h>
     57 #include <sys/disklabel.h>
     58 #include <sys/fcntl.h>
     59 #include <sys/malloc.h>
     60 #include <sys/vnode.h>
     61 #if NRND > 0
     62 #include <sys/rnd.h>
     63 #endif
     64 
     65 #include <miscfs/specfs/specdev.h>
     66 
     67 #include <dev/ldvar.h>
     68 
     69 #include <dev/ata/ata_raidvar.h>
     70 
     71 struct ld_ataraid_softc {
     72 	struct ld_softc sc_ld;
     73 
     74 	struct ataraid_array_info *sc_aai;
     75 	struct iinfo *sc_itable;
     76 	struct vnode *sc_vnodes[ATA_RAID_MAX_DISKS];
     77 
     78 	void	(*sc_iodone)(struct buf *);
     79 };
     80 
     81 static int	ld_ataraid_match(struct device *, struct cfdata *, void *);
     82 static void	ld_ataraid_attach(struct device *, struct device *, void *);
     83 
     84 static int	ld_ataraid_dump(struct ld_softc *, void *, int, int);
     85 
     86 static int	ld_ataraid_start_span(struct ld_softc *, struct buf *);
     87 
     88 static int	ld_ataraid_start_raid0(struct ld_softc *, struct buf *);
     89 static void	ld_ataraid_iodone_raid0(struct buf *);
     90 
     91 CFATTACH_DECL(ld_ataraid, sizeof(struct ld_ataraid_softc),
     92     ld_ataraid_match, ld_ataraid_attach, NULL, NULL);
     93 
     94 static int ld_ataraid_initialized;
     95 static struct pool ld_ataraid_cbufpl;
     96 
     97 struct cbuf {
     98 	struct buf	cb_buf;		/* new I/O buf */
     99 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
    100 	struct ld_ataraid_softc *cb_sc;	/* pointer to ld softc */
    101 	u_int		cb_comp;	/* target component */
    102 	SIMPLEQ_ENTRY(cbuf) cb_q;	/* fifo of component buffers */
    103 };
    104 
    105 #define	CBUF_GET()	pool_get(&ld_ataraid_cbufpl, PR_NOWAIT);
    106 #define	CBUF_PUT(cbp)	pool_put(&ld_ataraid_cbufpl, (cbp))
    107 
    108 static int
    109 ld_ataraid_match(struct device *parent, struct cfdata *match, void *aux)
    110 {
    111 
    112 	return (1);
    113 }
    114 
    115 static void
    116 ld_ataraid_attach(struct device *parent, struct device *self, void *aux)
    117 {
    118 	struct ld_ataraid_softc *sc = (void *) self;
    119 	struct ld_softc *ld = &sc->sc_ld;
    120 	struct ataraid_array_info *aai = aux;
    121 	const char *level;
    122 	struct vnode *vp;
    123 	char unklev[32];
    124 	u_int i;
    125 
    126 	if (ld_ataraid_initialized == 0) {
    127 		ld_ataraid_initialized = 1;
    128 		pool_init(&ld_ataraid_cbufpl, sizeof(struct cbuf), 0,
    129 		    0, 0, "ldcbuf", NULL);
    130 	}
    131 
    132 	sc->sc_aai = aai;	/* this data persists */
    133 
    134 	ld->sc_flags = LDF_ENABLED;
    135 	ld->sc_maxxfer = MAXPHYS * aai->aai_width;	/* XXX */
    136 	ld->sc_secperunit = aai->aai_capacity;
    137 	ld->sc_secsize = 512;				/* XXX */
    138 	ld->sc_maxqueuecnt = 128;			/* XXX */
    139 	ld->sc_dump = ld_ataraid_dump;
    140 
    141 	switch (aai->aai_level) {
    142 	case AAI_L_SPAN:
    143 		level = "SPAN";
    144 		ld->sc_start = ld_ataraid_start_span;
    145 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    146 		break;
    147 
    148 	case AAI_L_RAID0:
    149 		level = "RAID0";
    150 		ld->sc_start = ld_ataraid_start_raid0;
    151 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    152 		break;
    153 
    154 	case AAI_L_RAID1:
    155 		level = "RAID1";
    156 		break;
    157 
    158 	case AAI_L_RAID0 | AAI_L_RAID1:
    159 		level = "RAID0+1";
    160 		break;
    161 
    162 	default:
    163 		sprintf(unklev, "<unknown level 0x%x>", aai->aai_level);
    164 		level = unklev;
    165 	}
    166 
    167 	aprint_naive(": ATA %s array\n", level);
    168 	aprint_normal(": %s ATA %s array\n",
    169 	    ata_raid_type_name(aai->aai_type), level);
    170 
    171 	if (ld->sc_start == NULL) {
    172 		aprint_error("%s: unsupported array type\n",
    173 		    ld->sc_dv.dv_xname);
    174 		return;
    175 	}
    176 
    177 	/*
    178 	 * We get a geometry from the device; use it.
    179 	 */
    180 	ld->sc_nheads = aai->aai_heads;
    181 	ld->sc_nsectors = aai->aai_sectors;
    182 	ld->sc_ncylinders = aai->aai_cylinders;
    183 
    184 	/*
    185 	 * Configure all the component disks.
    186 	 */
    187 	for (i = 0; i < aai->aai_ndisks; i++) {
    188 		struct ataraid_disk_info *adi = &aai->aai_disks[i];
    189 		int bmajor, error;
    190 		dev_t dev;
    191 
    192 		bmajor = devsw_name2blk(adi->adi_dev->dv_xname, NULL, 0);
    193 		dev = MAKEDISKDEV(bmajor, adi->adi_dev->dv_unit, RAW_PART);
    194 		error = bdevvp(dev, &vp);
    195 		if (error)
    196 			goto bad;
    197 		error = VOP_OPEN(vp, FREAD|FWRITE, NOCRED, 0);
    198 		if (error) {
    199 			vput(vp);
    200 			/*
    201 			 * XXX This is bogus.  We should just mark the
    202 			 * XXX component as FAILED, and write-back new
    203 			 * XXX config blocks.
    204 			 */
    205 			goto bad;
    206 		}
    207 
    208 		VOP_UNLOCK(vp, 0);
    209 		sc->sc_vnodes[i] = vp;
    210 	}
    211 
    212 	ldattach(ld);
    213 	return;
    214  bad:
    215 	for (i = 0; i < aai->aai_ndisks; i++) {
    216 		vp = sc->sc_vnodes[i];
    217 		sc->sc_vnodes[i] = NULL;
    218 		(void) vn_close(vp, FREAD|FWRITE, NOCRED, curproc);
    219 	}
    220 }
    221 
    222 static struct cbuf *
    223 ld_ataraid_make_cbuf(struct ld_ataraid_softc *sc, struct buf *bp,
    224     u_int comp, daddr_t bn, caddr_t addr, long bcount)
    225 {
    226 	struct cbuf *cbp;
    227 
    228 	cbp = CBUF_GET();
    229 	if (cbp == NULL)
    230 		return (NULL);
    231 	cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
    232 	cbp->cb_buf.b_iodone = sc->sc_iodone;
    233 	cbp->cb_buf.b_proc = bp->b_proc;
    234 	cbp->cb_buf.b_vp = sc->sc_vnodes[comp];
    235 	cbp->cb_buf.b_dev = sc->sc_vnodes[comp]->v_rdev;
    236 	cbp->cb_buf.b_blkno = bn + sc->sc_aai->aai_offset;
    237 	cbp->cb_buf.b_data = addr;
    238 	LIST_INIT(&cbp->cb_buf.b_dep);
    239 	cbp->cb_buf.b_bcount = bcount;
    240 
    241 	/* Context for iodone */
    242 	cbp->cb_obp = bp;
    243 	cbp->cb_sc = sc;
    244 	cbp->cb_comp = comp;
    245 
    246 	return (cbp);
    247 }
    248 
    249 static int
    250 ld_ataraid_start_span(struct ld_softc *ld, struct buf *bp)
    251 {
    252 	struct ld_ataraid_softc *sc = (void *) ld;
    253 	struct ataraid_array_info *aai = sc->sc_aai;
    254 	struct ataraid_disk_info *adi;
    255 	SIMPLEQ_HEAD(, cbuf) cbufq;
    256 	struct cbuf *cbp;
    257 	caddr_t addr;
    258 	daddr_t bn;
    259 	long bcount, rcount;
    260 	u_int comp;
    261 	int s;
    262 
    263 	/* Allocate component buffers. */
    264 	SIMPLEQ_INIT(&cbufq);
    265 	addr = bp->b_data;
    266 
    267 	/* Find the first component. */
    268 	comp = 0;
    269 	adi = &aai->aai_disks[comp];
    270 	bn = bp->b_rawblkno;
    271 	while (bn >= adi->adi_compsize) {
    272 		bn -= adi->adi_compsize;
    273 		adi = &aai->aai_disks[++comp];
    274 	}
    275 
    276 	s = splbio();		/* XXX big hammer */
    277 
    278 	bp->b_resid = bp->b_bcount;
    279 
    280 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    281 		rcount = bp->b_bcount;
    282 		if ((adi->adi_compsize - bn) < btodb(rcount))
    283 			rcount = dbtob(adi->adi_compsize - bn);
    284 
    285 		cbp = ld_ataraid_make_cbuf(sc, bp, comp, bn, addr, rcount);
    286 		if (cbp == NULL) {
    287 			/* Free the already allocated component buffers. */
    288 			while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    289 				SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    290 				CBUF_PUT(cbp);
    291 			}
    292 
    293 			splx(s);
    294 
    295 			/* Notify the upper layer that we are out of memory. */
    296 			return (ENOMEM);
    297 		}
    298 
    299 		/*
    300 		 * For a span, we always know we advance to the next disk,
    301 		 * and always start at offset 0 on that disk.
    302 		 */
    303 		adi = &aai->aai_disks[++comp];
    304 		bn = 0;
    305 
    306 		SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    307 		addr += rcount;
    308 	}
    309 
    310 	/* Now fire off the requests. */
    311 	while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    312 		SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    313 		if ((cbp->cb_buf.b_flags & B_READ) == 0)
    314 			cbp->cb_buf.b_vp->v_numoutput++;
    315 		VOP_STRATEGY(&cbp->cb_buf);
    316 	}
    317 
    318 	splx(s);
    319 
    320 	return (0);
    321 }
    322 
    323 static int
    324 ld_ataraid_start_raid0(struct ld_softc *ld, struct buf *bp)
    325 {
    326 	struct ld_ataraid_softc *sc = (void *) ld;
    327 	struct ataraid_array_info *aai = sc->sc_aai;
    328 	SIMPLEQ_HEAD(, cbuf) cbufq;
    329 	struct cbuf *cbp;
    330 	caddr_t addr;
    331 	daddr_t bn, cbn, tbn, off;
    332 	long bcount, rcount;
    333 	u_int comp;
    334 	int s;
    335 
    336 	/* Allocate component buffers. */
    337 	SIMPLEQ_INIT(&cbufq);
    338 	addr = bp->b_data;
    339 	bn = bp->b_rawblkno;
    340 
    341 	s = splbio();		/* XXX big hammer */
    342 
    343 	bp->b_resid = bp->b_bcount;
    344 
    345 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    346 		tbn = bn / aai->aai_interleave;
    347 		off = bn % aai->aai_interleave;
    348 
    349 		if (__predict_false(tbn == aai->aai_capacity /
    350 					   aai->aai_interleave)) {
    351 			/* Last stripe. */
    352 			daddr_t sz = (aai->aai_capacity -
    353 				      (tbn * aai->aai_interleave)) /
    354 				     aai->aai_width;
    355 			comp = off / sz;
    356 			cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
    357 			    (off % sz);
    358 			rcount = min(bcount, dbtob(sz));
    359 		} else {
    360 			comp = tbn % aai->aai_width;
    361 			cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
    362 			    off;
    363 			rcount = min(bcount, dbtob(aai->aai_interleave - off));
    364 		}
    365 
    366 		cbp = ld_ataraid_make_cbuf(sc, bp, comp, cbn, addr, rcount);
    367 		if (cbp == NULL) {
    368 			/* Free the already allocated component buffers. */
    369 			while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    370 				SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    371 				CBUF_PUT(cbp);
    372 			}
    373 
    374 			splx(s);
    375 
    376 			/* Notify the upper layer that we are out of memory. */
    377 			return (ENOMEM);
    378 		}
    379 		SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    380 		bn += btodb(rcount);
    381 		addr += rcount;
    382 	}
    383 
    384 	/* Now fire off the requests. */
    385 	while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    386 		SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    387 		if ((cbp->cb_buf.b_flags & B_READ) == 0)
    388 			cbp->cb_buf.b_vp->v_numoutput++;
    389 		VOP_STRATEGY(&cbp->cb_buf);
    390 	}
    391 
    392 	splx(s);
    393 
    394 	return (0);
    395 }
    396 
    397 /*
    398  * Called at interrupt time.  Mark the component as done and if all
    399  * components are done, take an "interrupt".
    400  */
    401 static void
    402 ld_ataraid_iodone_raid0(struct buf *vbp)
    403 {
    404 	struct cbuf *cbp = (struct cbuf *) vbp;
    405 	struct buf *bp = cbp->cb_obp;
    406 	struct ld_ataraid_softc *sc = cbp->cb_sc;
    407 	long count;
    408 	int s;
    409 
    410 	s = splbio();
    411 
    412 	if (cbp->cb_buf.b_flags & B_ERROR) {
    413 		bp->b_flags |= B_ERROR;
    414 		bp->b_error = cbp->cb_buf.b_error ?
    415 		    cbp->cb_buf.b_error : EIO;
    416 
    417 		/* XXX Update component config blocks. */
    418 
    419 		printf("%s: error %d on component %d\n",
    420 		    sc->sc_ld.sc_dv.dv_xname, bp->b_error, cbp->cb_comp);
    421 	}
    422 	count = cbp->cb_buf.b_bcount;
    423 	CBUF_PUT(cbp);
    424 
    425 	/* If all done, "interrupt". */
    426 	bp->b_resid -= count;
    427 	if (bp->b_resid < 0)
    428 		panic("ld_ataraid_iodone_raid0: count");
    429 	if (bp->b_resid == 0)
    430 		lddone(&sc->sc_ld, bp);
    431 	splx(s);
    432 }
    433 
    434 static int
    435 ld_ataraid_dump(struct ld_softc *sc, void *data, int blkno, int blkcnt)
    436 {
    437 
    438 	return (EIO);
    439 }
    440