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