Home | History | Annotate | Line # | Download | only in ata
ld_ataraid.c revision 1.30
      1  1.30      tron /*	$NetBSD: ld_ataraid.c,v 1.30 2008/09/15 11:44:50 tron 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.30      tron  *
     46  1.30      tron  * bio(4) support was written by Juan Romero Pardines <xtraeme (at) gmail.com>.
     47   1.1   thorpej  */
     48   1.8     lukem 
     49   1.8     lukem #include <sys/cdefs.h>
     50  1.30      tron __KERNEL_RCSID(0, "$NetBSD: ld_ataraid.c,v 1.30 2008/09/15 11:44:50 tron Exp $");
     51   1.1   thorpej 
     52  1.30      tron #include "bio.h"
     53   1.1   thorpej #include "rnd.h"
     54   1.1   thorpej 
     55   1.1   thorpej #include <sys/param.h>
     56   1.1   thorpej #include <sys/systm.h>
     57   1.1   thorpej #include <sys/conf.h>
     58   1.1   thorpej #include <sys/kernel.h>
     59   1.1   thorpej #include <sys/device.h>
     60   1.1   thorpej #include <sys/buf.h>
     61  1.13      yamt #include <sys/bufq.h>
     62   1.1   thorpej #include <sys/dkio.h>
     63   1.1   thorpej #include <sys/disk.h>
     64   1.1   thorpej #include <sys/disklabel.h>
     65   1.1   thorpej #include <sys/fcntl.h>
     66   1.1   thorpej #include <sys/malloc.h>
     67   1.1   thorpej #include <sys/vnode.h>
     68  1.16      elad #include <sys/kauth.h>
     69   1.1   thorpej #if NRND > 0
     70   1.1   thorpej #include <sys/rnd.h>
     71   1.1   thorpej #endif
     72  1.30      tron #if NBIO > 0
     73  1.30      tron #include <dev/ata/atavar.h>
     74  1.30      tron #include <dev/ata/atareg.h>
     75  1.30      tron #include <dev/ata/wdvar.h>
     76  1.30      tron #include <dev/biovar.h>
     77  1.30      tron #include <dev/scsipi/scsipiconf.h> /* for scsipi_strvis() */
     78  1.30      tron #endif
     79   1.1   thorpej 
     80   1.1   thorpej #include <miscfs/specfs/specdev.h>
     81   1.1   thorpej 
     82   1.1   thorpej #include <dev/ldvar.h>
     83   1.1   thorpej 
     84   1.1   thorpej #include <dev/ata/ata_raidvar.h>
     85   1.1   thorpej 
     86   1.1   thorpej struct ld_ataraid_softc {
     87   1.1   thorpej 	struct ld_softc sc_ld;
     88   1.1   thorpej 
     89   1.1   thorpej 	struct ataraid_array_info *sc_aai;
     90   1.1   thorpej 	struct vnode *sc_vnodes[ATA_RAID_MAX_DISKS];
     91   1.1   thorpej 
     92   1.1   thorpej 	void	(*sc_iodone)(struct buf *);
     93   1.1   thorpej };
     94   1.1   thorpej 
     95   1.1   thorpej static int	ld_ataraid_match(struct device *, struct cfdata *, void *);
     96   1.1   thorpej static void	ld_ataraid_attach(struct device *, struct device *, void *);
     97   1.1   thorpej 
     98   1.1   thorpej static int	ld_ataraid_dump(struct ld_softc *, void *, int, int);
     99   1.1   thorpej 
    100   1.1   thorpej static int	ld_ataraid_start_span(struct ld_softc *, struct buf *);
    101   1.1   thorpej 
    102   1.1   thorpej static int	ld_ataraid_start_raid0(struct ld_softc *, struct buf *);
    103   1.1   thorpej static void	ld_ataraid_iodone_raid0(struct buf *);
    104   1.1   thorpej 
    105  1.30      tron #if NBIO > 0
    106  1.30      tron static int	ld_ataraid_bioctl(device_t, u_long, void *);
    107  1.30      tron static int	ld_ataraid_bioinq(struct ld_ataraid_softc *, struct bioc_inq *);
    108  1.30      tron static int	ld_ataraid_biovol(struct ld_ataraid_softc *, struct bioc_vol *);
    109  1.30      tron static int	ld_ataraid_biodisk(struct ld_ataraid_softc *,
    110  1.30      tron 				   struct bioc_disk *);
    111  1.30      tron #endif
    112  1.30      tron 
    113  1.27   xtraeme CFATTACH_DECL_NEW(ld_ataraid, sizeof(struct ld_ataraid_softc),
    114   1.1   thorpej     ld_ataraid_match, ld_ataraid_attach, NULL, NULL);
    115   1.1   thorpej 
    116   1.1   thorpej static int ld_ataraid_initialized;
    117   1.1   thorpej static struct pool ld_ataraid_cbufpl;
    118   1.1   thorpej 
    119   1.1   thorpej struct cbuf {
    120   1.1   thorpej 	struct buf	cb_buf;		/* new I/O buf */
    121   1.1   thorpej 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
    122   1.1   thorpej 	struct ld_ataraid_softc *cb_sc;	/* pointer to ld softc */
    123   1.1   thorpej 	u_int		cb_comp;	/* target component */
    124   1.1   thorpej 	SIMPLEQ_ENTRY(cbuf) cb_q;	/* fifo of component buffers */
    125  1.12     enami 	struct cbuf	*cb_other;	/* other cbuf in case of mirror */
    126  1.12     enami 	int		cb_flags;
    127  1.12     enami #define	CBUF_IODONE	0x00000001	/* I/O is already successfully done */
    128   1.1   thorpej };
    129   1.1   thorpej 
    130   1.1   thorpej #define	CBUF_GET()	pool_get(&ld_ataraid_cbufpl, PR_NOWAIT);
    131   1.1   thorpej #define	CBUF_PUT(cbp)	pool_put(&ld_ataraid_cbufpl, (cbp))
    132   1.1   thorpej 
    133   1.1   thorpej static int
    134  1.27   xtraeme ld_ataraid_match(device_t parent, cfdata_t match, void *aux)
    135   1.1   thorpej {
    136   1.1   thorpej 
    137   1.1   thorpej 	return (1);
    138   1.1   thorpej }
    139   1.1   thorpej 
    140   1.1   thorpej static void
    141  1.27   xtraeme ld_ataraid_attach(device_t parent, device_t self, void *aux)
    142   1.1   thorpej {
    143  1.27   xtraeme 	struct ld_ataraid_softc *sc = device_private(self);
    144   1.1   thorpej 	struct ld_softc *ld = &sc->sc_ld;
    145   1.1   thorpej 	struct ataraid_array_info *aai = aux;
    146   1.1   thorpej 	const char *level;
    147   1.1   thorpej 	struct vnode *vp;
    148   1.1   thorpej 	char unklev[32];
    149   1.1   thorpej 	u_int i;
    150   1.1   thorpej 
    151  1.29      tron 	ld->sc_dv = self;
    152  1.29      tron 
    153   1.1   thorpej 	if (ld_ataraid_initialized == 0) {
    154   1.1   thorpej 		ld_ataraid_initialized = 1;
    155   1.1   thorpej 		pool_init(&ld_ataraid_cbufpl, sizeof(struct cbuf), 0,
    156  1.20        ad 		    0, 0, "ldcbuf", NULL, IPL_BIO);
    157   1.1   thorpej 	}
    158   1.1   thorpej 
    159   1.1   thorpej 	sc->sc_aai = aai;	/* this data persists */
    160   1.1   thorpej 
    161   1.1   thorpej 	ld->sc_maxxfer = MAXPHYS * aai->aai_width;	/* XXX */
    162   1.1   thorpej 	ld->sc_secperunit = aai->aai_capacity;
    163   1.1   thorpej 	ld->sc_secsize = 512;				/* XXX */
    164   1.1   thorpej 	ld->sc_maxqueuecnt = 128;			/* XXX */
    165   1.1   thorpej 	ld->sc_dump = ld_ataraid_dump;
    166   1.1   thorpej 
    167   1.1   thorpej 	switch (aai->aai_level) {
    168   1.1   thorpej 	case AAI_L_SPAN:
    169   1.1   thorpej 		level = "SPAN";
    170   1.1   thorpej 		ld->sc_start = ld_ataraid_start_span;
    171   1.1   thorpej 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    172   1.1   thorpej 		break;
    173   1.1   thorpej 
    174   1.1   thorpej 	case AAI_L_RAID0:
    175   1.9   thorpej 		level = "RAID-0";
    176   1.1   thorpej 		ld->sc_start = ld_ataraid_start_raid0;
    177   1.1   thorpej 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    178   1.1   thorpej 		break;
    179   1.1   thorpej 
    180   1.1   thorpej 	case AAI_L_RAID1:
    181   1.9   thorpej 		level = "RAID-1";
    182  1.12     enami 		ld->sc_start = ld_ataraid_start_raid0;
    183  1.12     enami 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    184   1.1   thorpej 		break;
    185   1.1   thorpej 
    186   1.1   thorpej 	case AAI_L_RAID0 | AAI_L_RAID1:
    187   1.9   thorpej 		level = "RAID-10";
    188  1.12     enami 		ld->sc_start = ld_ataraid_start_raid0;
    189  1.12     enami 		sc->sc_iodone = ld_ataraid_iodone_raid0;
    190   1.1   thorpej 		break;
    191   1.1   thorpej 
    192   1.1   thorpej 	default:
    193  1.11    itojun 		snprintf(unklev, sizeof(unklev), "<unknown level 0x%x>",
    194  1.11    itojun 		    aai->aai_level);
    195   1.1   thorpej 		level = unklev;
    196   1.1   thorpej 	}
    197   1.1   thorpej 
    198   1.1   thorpej 	aprint_naive(": ATA %s array\n", level);
    199   1.1   thorpej 	aprint_normal(": %s ATA %s array\n",
    200   1.1   thorpej 	    ata_raid_type_name(aai->aai_type), level);
    201   1.1   thorpej 
    202   1.1   thorpej 	if (ld->sc_start == NULL) {
    203  1.29      tron 		aprint_error_dev(ld->sc_dv, "unsupported array type\n");
    204   1.1   thorpej 		return;
    205   1.1   thorpej 	}
    206   1.1   thorpej 
    207   1.1   thorpej 	/*
    208   1.1   thorpej 	 * We get a geometry from the device; use it.
    209   1.1   thorpej 	 */
    210   1.1   thorpej 	ld->sc_nheads = aai->aai_heads;
    211   1.1   thorpej 	ld->sc_nsectors = aai->aai_sectors;
    212   1.1   thorpej 	ld->sc_ncylinders = aai->aai_cylinders;
    213   1.1   thorpej 
    214   1.1   thorpej 	/*
    215   1.1   thorpej 	 * Configure all the component disks.
    216   1.1   thorpej 	 */
    217   1.1   thorpej 	for (i = 0; i < aai->aai_ndisks; i++) {
    218   1.1   thorpej 		struct ataraid_disk_info *adi = &aai->aai_disks[i];
    219   1.1   thorpej 		int bmajor, error;
    220   1.1   thorpej 		dev_t dev;
    221   1.1   thorpej 
    222  1.26    cegger 		bmajor = devsw_name2blk(device_xname(adi->adi_dev), NULL, 0);
    223  1.15   thorpej 		dev = MAKEDISKDEV(bmajor, device_unit(adi->adi_dev), RAW_PART);
    224   1.1   thorpej 		error = bdevvp(dev, &vp);
    225   1.1   thorpej 		if (error)
    226   1.3   thorpej 			break;
    227  1.22     pooka 		error = VOP_OPEN(vp, FREAD|FWRITE, NOCRED);
    228   1.1   thorpej 		if (error) {
    229   1.1   thorpej 			vput(vp);
    230   1.1   thorpej 			/*
    231   1.1   thorpej 			 * XXX This is bogus.  We should just mark the
    232   1.1   thorpej 			 * XXX component as FAILED, and write-back new
    233   1.1   thorpej 			 * XXX config blocks.
    234   1.1   thorpej 			 */
    235   1.3   thorpej 			break;
    236   1.1   thorpej 		}
    237   1.1   thorpej 
    238   1.1   thorpej 		VOP_UNLOCK(vp, 0);
    239   1.1   thorpej 		sc->sc_vnodes[i] = vp;
    240   1.1   thorpej 	}
    241   1.3   thorpej 	if (i == aai->aai_ndisks) {
    242   1.3   thorpej 		ld->sc_flags = LDF_ENABLED;
    243   1.3   thorpej 		goto finish;
    244   1.3   thorpej 	}
    245   1.1   thorpej 
    246   1.1   thorpej 	for (i = 0; i < aai->aai_ndisks; i++) {
    247   1.1   thorpej 		vp = sc->sc_vnodes[i];
    248   1.1   thorpej 		sc->sc_vnodes[i] = NULL;
    249   1.3   thorpej 		if (vp != NULL)
    250  1.25        ad 			(void) vn_close(vp, FREAD|FWRITE, NOCRED);
    251   1.1   thorpej 	}
    252   1.3   thorpej 
    253   1.3   thorpej  finish:
    254  1.30      tron #if NBIO > 0
    255  1.30      tron 	if (bio_register(self, ld_ataraid_bioctl) != 0)
    256  1.30      tron 		panic("%s: bioctl registration failed\n",
    257  1.30      tron 		    device_xname(ld->sc_dv));
    258  1.30      tron #endif
    259   1.3   thorpej 	ldattach(ld);
    260   1.1   thorpej }
    261   1.1   thorpej 
    262   1.1   thorpej static struct cbuf *
    263   1.1   thorpej ld_ataraid_make_cbuf(struct ld_ataraid_softc *sc, struct buf *bp,
    264  1.19  christos     u_int comp, daddr_t bn, void *addr, long bcount)
    265   1.1   thorpej {
    266   1.1   thorpej 	struct cbuf *cbp;
    267   1.1   thorpej 
    268   1.1   thorpej 	cbp = CBUF_GET();
    269   1.1   thorpej 	if (cbp == NULL)
    270   1.1   thorpej 		return (NULL);
    271  1.23        ad 	buf_init(&cbp->cb_buf);
    272  1.23        ad 	cbp->cb_buf.b_flags = bp->b_flags;
    273  1.23        ad 	cbp->cb_buf.b_oflags = bp->b_oflags;
    274  1.23        ad 	cbp->cb_buf.b_cflags = bp->b_cflags;
    275   1.1   thorpej 	cbp->cb_buf.b_iodone = sc->sc_iodone;
    276   1.1   thorpej 	cbp->cb_buf.b_proc = bp->b_proc;
    277   1.1   thorpej 	cbp->cb_buf.b_vp = sc->sc_vnodes[comp];
    278  1.24        ad 	cbp->cb_buf.b_objlock = &sc->sc_vnodes[comp]->v_interlock;
    279   1.1   thorpej 	cbp->cb_buf.b_blkno = bn + sc->sc_aai->aai_offset;
    280   1.1   thorpej 	cbp->cb_buf.b_data = addr;
    281   1.1   thorpej 	cbp->cb_buf.b_bcount = bcount;
    282   1.1   thorpej 
    283   1.1   thorpej 	/* Context for iodone */
    284   1.1   thorpej 	cbp->cb_obp = bp;
    285   1.1   thorpej 	cbp->cb_sc = sc;
    286   1.1   thorpej 	cbp->cb_comp = comp;
    287  1.12     enami 	cbp->cb_other = NULL;
    288  1.12     enami 	cbp->cb_flags = 0;
    289   1.1   thorpej 
    290   1.1   thorpej 	return (cbp);
    291   1.1   thorpej }
    292   1.1   thorpej 
    293   1.1   thorpej static int
    294   1.1   thorpej ld_ataraid_start_span(struct ld_softc *ld, struct buf *bp)
    295   1.1   thorpej {
    296   1.1   thorpej 	struct ld_ataraid_softc *sc = (void *) ld;
    297   1.1   thorpej 	struct ataraid_array_info *aai = sc->sc_aai;
    298   1.1   thorpej 	struct ataraid_disk_info *adi;
    299   1.1   thorpej 	SIMPLEQ_HEAD(, cbuf) cbufq;
    300   1.1   thorpej 	struct cbuf *cbp;
    301  1.19  christos 	char *addr;
    302   1.1   thorpej 	daddr_t bn;
    303   1.1   thorpej 	long bcount, rcount;
    304   1.1   thorpej 	u_int comp;
    305   1.1   thorpej 
    306   1.1   thorpej 	/* Allocate component buffers. */
    307   1.1   thorpej 	SIMPLEQ_INIT(&cbufq);
    308   1.1   thorpej 	addr = bp->b_data;
    309   1.1   thorpej 
    310   1.1   thorpej 	/* Find the first component. */
    311   1.1   thorpej 	comp = 0;
    312   1.1   thorpej 	adi = &aai->aai_disks[comp];
    313   1.1   thorpej 	bn = bp->b_rawblkno;
    314   1.1   thorpej 	while (bn >= adi->adi_compsize) {
    315   1.1   thorpej 		bn -= adi->adi_compsize;
    316   1.1   thorpej 		adi = &aai->aai_disks[++comp];
    317   1.1   thorpej 	}
    318   1.1   thorpej 
    319   1.1   thorpej 	bp->b_resid = bp->b_bcount;
    320   1.1   thorpej 
    321   1.1   thorpej 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    322   1.1   thorpej 		rcount = bp->b_bcount;
    323   1.1   thorpej 		if ((adi->adi_compsize - bn) < btodb(rcount))
    324   1.1   thorpej 			rcount = dbtob(adi->adi_compsize - bn);
    325   1.1   thorpej 
    326   1.1   thorpej 		cbp = ld_ataraid_make_cbuf(sc, bp, comp, bn, addr, rcount);
    327   1.1   thorpej 		if (cbp == NULL) {
    328   1.1   thorpej 			/* Free the already allocated component buffers. */
    329   1.1   thorpej 			while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    330   1.1   thorpej 				SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    331  1.23        ad 				buf_destroy(&cbp->cb_buf);
    332   1.1   thorpej 				CBUF_PUT(cbp);
    333   1.1   thorpej 			}
    334   1.5   thorpej 			return (EAGAIN);
    335   1.1   thorpej 		}
    336   1.1   thorpej 
    337   1.1   thorpej 		/*
    338   1.1   thorpej 		 * For a span, we always know we advance to the next disk,
    339   1.1   thorpej 		 * and always start at offset 0 on that disk.
    340   1.1   thorpej 		 */
    341   1.1   thorpej 		adi = &aai->aai_disks[++comp];
    342   1.1   thorpej 		bn = 0;
    343   1.1   thorpej 
    344   1.1   thorpej 		SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    345   1.1   thorpej 		addr += rcount;
    346   1.1   thorpej 	}
    347   1.1   thorpej 
    348   1.1   thorpej 	/* Now fire off the requests. */
    349   1.1   thorpej 	while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    350   1.1   thorpej 		SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    351  1.24        ad 		if ((cbp->cb_buf.b_flags & B_READ) == 0) {
    352  1.24        ad 			mutex_enter(&cbp->cb_buf.b_vp->v_interlock);
    353   1.1   thorpej 			cbp->cb_buf.b_vp->v_numoutput++;
    354  1.24        ad 			mutex_exit(&cbp->cb_buf.b_vp->v_interlock);
    355  1.24        ad 		}
    356  1.10   hannken 		VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
    357   1.1   thorpej 	}
    358   1.1   thorpej 
    359   1.1   thorpej 	return (0);
    360   1.1   thorpej }
    361   1.1   thorpej 
    362   1.1   thorpej static int
    363   1.1   thorpej ld_ataraid_start_raid0(struct ld_softc *ld, struct buf *bp)
    364   1.1   thorpej {
    365   1.1   thorpej 	struct ld_ataraid_softc *sc = (void *) ld;
    366   1.1   thorpej 	struct ataraid_array_info *aai = sc->sc_aai;
    367  1.12     enami 	struct ataraid_disk_info *adi;
    368   1.1   thorpej 	SIMPLEQ_HEAD(, cbuf) cbufq;
    369  1.12     enami 	struct cbuf *cbp, *other_cbp;
    370  1.19  christos 	char *addr;
    371   1.1   thorpej 	daddr_t bn, cbn, tbn, off;
    372   1.1   thorpej 	long bcount, rcount;
    373   1.1   thorpej 	u_int comp;
    374  1.12     enami 	const int read = bp->b_flags & B_READ;
    375  1.12     enami 	const int mirror = aai->aai_level & AAI_L_RAID1;
    376  1.12     enami 	int error;
    377   1.1   thorpej 
    378   1.1   thorpej 	/* Allocate component buffers. */
    379   1.1   thorpej 	SIMPLEQ_INIT(&cbufq);
    380   1.1   thorpej 	addr = bp->b_data;
    381   1.1   thorpej 	bn = bp->b_rawblkno;
    382   1.1   thorpej 
    383   1.1   thorpej 	bp->b_resid = bp->b_bcount;
    384   1.1   thorpej 
    385   1.1   thorpej 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    386   1.1   thorpej 		tbn = bn / aai->aai_interleave;
    387   1.1   thorpej 		off = bn % aai->aai_interleave;
    388   1.1   thorpej 
    389   1.1   thorpej 		if (__predict_false(tbn == aai->aai_capacity /
    390   1.1   thorpej 					   aai->aai_interleave)) {
    391   1.1   thorpej 			/* Last stripe. */
    392   1.1   thorpej 			daddr_t sz = (aai->aai_capacity -
    393   1.1   thorpej 				      (tbn * aai->aai_interleave)) /
    394   1.1   thorpej 				     aai->aai_width;
    395   1.1   thorpej 			comp = off / sz;
    396   1.1   thorpej 			cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
    397   1.1   thorpej 			    (off % sz);
    398   1.1   thorpej 			rcount = min(bcount, dbtob(sz));
    399   1.1   thorpej 		} else {
    400   1.1   thorpej 			comp = tbn % aai->aai_width;
    401   1.1   thorpej 			cbn = ((tbn / aai->aai_width) * aai->aai_interleave) +
    402   1.1   thorpej 			    off;
    403   1.1   thorpej 			rcount = min(bcount, dbtob(aai->aai_interleave - off));
    404   1.1   thorpej 		}
    405   1.1   thorpej 
    406  1.12     enami 		/*
    407  1.12     enami 		 * See if a component is valid.
    408  1.12     enami 		 */
    409  1.12     enami try_mirror:
    410  1.12     enami 		adi = &aai->aai_disks[comp];
    411  1.12     enami 		if ((adi->adi_status & ADI_S_ONLINE) == 0) {
    412  1.12     enami 			if (mirror && comp < aai->aai_width) {
    413  1.12     enami 				comp += aai->aai_width;
    414  1.12     enami 				goto try_mirror;
    415  1.12     enami 			}
    416  1.12     enami 
    417  1.12     enami 			/*
    418  1.12     enami 			 * No component available.
    419  1.12     enami 			 */
    420  1.12     enami 			error = EIO;
    421  1.12     enami 			goto free_and_exit;
    422  1.12     enami 		}
    423  1.12     enami 
    424   1.1   thorpej 		cbp = ld_ataraid_make_cbuf(sc, bp, comp, cbn, addr, rcount);
    425   1.1   thorpej 		if (cbp == NULL) {
    426  1.12     enami resource_shortage:
    427  1.12     enami 			error = EAGAIN;
    428  1.12     enami free_and_exit:
    429   1.1   thorpej 			/* Free the already allocated component buffers. */
    430   1.1   thorpej 			while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    431   1.1   thorpej 				SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    432  1.23        ad 				buf_destroy(&cbp->cb_buf);
    433   1.1   thorpej 				CBUF_PUT(cbp);
    434   1.1   thorpej 			}
    435  1.12     enami 			return (error);
    436   1.1   thorpej 		}
    437   1.1   thorpej 		SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    438  1.12     enami 		if (mirror && !read && comp < aai->aai_width) {
    439  1.12     enami 			comp += aai->aai_width;
    440  1.12     enami 			adi = &aai->aai_disks[comp];
    441  1.12     enami 			if (adi->adi_status & ADI_S_ONLINE) {
    442  1.12     enami 				other_cbp = ld_ataraid_make_cbuf(sc, bp,
    443  1.12     enami 				    comp, cbn, addr, rcount);
    444  1.12     enami 				if (other_cbp == NULL)
    445  1.12     enami 					goto resource_shortage;
    446  1.12     enami 				SIMPLEQ_INSERT_TAIL(&cbufq, other_cbp, cb_q);
    447  1.12     enami 				other_cbp->cb_other = cbp;
    448  1.12     enami 				cbp->cb_other = other_cbp;
    449  1.12     enami 			}
    450  1.12     enami 		}
    451   1.1   thorpej 		bn += btodb(rcount);
    452   1.1   thorpej 		addr += rcount;
    453   1.1   thorpej 	}
    454   1.1   thorpej 
    455   1.1   thorpej 	/* Now fire off the requests. */
    456   1.1   thorpej 	while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    457   1.1   thorpej 		SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    458  1.24        ad 		if ((cbp->cb_buf.b_flags & B_READ) == 0) {
    459  1.24        ad 			mutex_enter(&cbp->cb_buf.b_vp->v_interlock);
    460   1.1   thorpej 			cbp->cb_buf.b_vp->v_numoutput++;
    461  1.24        ad 			mutex_exit(&cbp->cb_buf.b_vp->v_interlock);
    462  1.24        ad 		}
    463  1.10   hannken 		VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
    464   1.1   thorpej 	}
    465   1.1   thorpej 
    466   1.1   thorpej 	return (0);
    467   1.1   thorpej }
    468   1.1   thorpej 
    469   1.1   thorpej /*
    470   1.1   thorpej  * Called at interrupt time.  Mark the component as done and if all
    471   1.1   thorpej  * components are done, take an "interrupt".
    472   1.1   thorpej  */
    473   1.1   thorpej static void
    474   1.1   thorpej ld_ataraid_iodone_raid0(struct buf *vbp)
    475   1.1   thorpej {
    476  1.12     enami 	struct cbuf *cbp = (struct cbuf *) vbp, *other_cbp;
    477   1.1   thorpej 	struct buf *bp = cbp->cb_obp;
    478   1.1   thorpej 	struct ld_ataraid_softc *sc = cbp->cb_sc;
    479  1.12     enami 	struct ataraid_array_info *aai = sc->sc_aai;
    480  1.12     enami 	struct ataraid_disk_info *adi;
    481   1.1   thorpej 	long count;
    482  1.12     enami 	int s, iodone;
    483   1.1   thorpej 
    484   1.1   thorpej 	s = splbio();
    485   1.1   thorpej 
    486  1.12     enami 	iodone = cbp->cb_flags & CBUF_IODONE;
    487  1.12     enami 	other_cbp = cbp->cb_other;
    488  1.12     enami 	if (other_cbp != NULL)
    489  1.12     enami 		/* You are alone */
    490  1.12     enami 		other_cbp->cb_other = NULL;
    491  1.12     enami 
    492  1.21        ad 	if (cbp->cb_buf.b_error != 0) {
    493  1.12     enami 		/*
    494  1.12     enami 		 * Mark this component broken.
    495  1.12     enami 		 */
    496  1.12     enami 		adi = &aai->aai_disks[cbp->cb_comp];
    497  1.12     enami 		adi->adi_status &= ~ADI_S_ONLINE;
    498  1.12     enami 
    499  1.12     enami 		printf("%s: error %d on component %d (%s)\n",
    500  1.29      tron 		    device_xname(sc->sc_ld.sc_dv), bp->b_error, cbp->cb_comp,
    501  1.26    cegger 		    device_xname(adi->adi_dev));
    502  1.12     enami 
    503  1.12     enami 		/*
    504  1.12     enami 		 * If we didn't see an error yet and we are reading
    505  1.12     enami 		 * RAID1 disk, try another component.
    506  1.12     enami 		 */
    507  1.21        ad 		if (bp->b_error == 0 &&
    508  1.12     enami 		    (cbp->cb_buf.b_flags & B_READ) != 0 &&
    509  1.12     enami 		    (aai->aai_level & AAI_L_RAID1) != 0 &&
    510  1.12     enami 		    cbp->cb_comp < aai->aai_width) {
    511  1.12     enami 			cbp->cb_comp += aai->aai_width;
    512  1.12     enami 			adi = &aai->aai_disks[cbp->cb_comp];
    513  1.12     enami 			if (adi->adi_status & ADI_S_ONLINE) {
    514  1.21        ad 				cbp->cb_buf.b_error = 0;
    515  1.12     enami 				VOP_STRATEGY(cbp->cb_buf.b_vp, &cbp->cb_buf);
    516  1.12     enami 				goto out;
    517  1.12     enami 			}
    518  1.12     enami 		}
    519  1.12     enami 
    520  1.12     enami 		if (iodone || other_cbp != NULL)
    521  1.12     enami 			/*
    522  1.12     enami 			 * If I/O on other component successfully done
    523  1.12     enami 			 * or the I/O is still in progress, no need
    524  1.12     enami 			 * to tell an error to upper layer.
    525  1.12     enami 			 */
    526  1.12     enami 			;
    527  1.12     enami 		else {
    528  1.12     enami 			bp->b_error = cbp->cb_buf.b_error ?
    529  1.12     enami 			    cbp->cb_buf.b_error : EIO;
    530  1.12     enami 		}
    531   1.1   thorpej 
    532   1.1   thorpej 		/* XXX Update component config blocks. */
    533   1.1   thorpej 
    534  1.12     enami 	} else {
    535  1.12     enami 		/*
    536  1.12     enami 		 * If other I/O is still in progress, tell it that
    537  1.12     enami 		 * our I/O is successfully done.
    538  1.12     enami 		 */
    539  1.12     enami 		if (other_cbp != NULL)
    540  1.12     enami 			other_cbp->cb_flags |= CBUF_IODONE;
    541   1.1   thorpej 	}
    542   1.1   thorpej 	count = cbp->cb_buf.b_bcount;
    543  1.28   hannken 	buf_destroy(&cbp->cb_buf);
    544   1.1   thorpej 	CBUF_PUT(cbp);
    545   1.1   thorpej 
    546  1.12     enami 	if (other_cbp != NULL)
    547  1.12     enami 		goto out;
    548  1.12     enami 
    549   1.1   thorpej 	/* If all done, "interrupt". */
    550   1.1   thorpej 	bp->b_resid -= count;
    551   1.1   thorpej 	if (bp->b_resid < 0)
    552   1.1   thorpej 		panic("ld_ataraid_iodone_raid0: count");
    553   1.1   thorpej 	if (bp->b_resid == 0)
    554   1.1   thorpej 		lddone(&sc->sc_ld, bp);
    555  1.12     enami 
    556  1.12     enami out:
    557   1.1   thorpej 	splx(s);
    558   1.1   thorpej }
    559   1.1   thorpej 
    560   1.1   thorpej static int
    561  1.18  christos ld_ataraid_dump(struct ld_softc *sc, void *data,
    562  1.18  christos     int blkno, int blkcnt)
    563   1.1   thorpej {
    564   1.1   thorpej 
    565   1.1   thorpej 	return (EIO);
    566   1.1   thorpej }
    567  1.30      tron 
    568  1.30      tron #if NBIO > 0
    569  1.30      tron static int
    570  1.30      tron ld_ataraid_bioctl(device_t self, u_long cmd, void *addr)
    571  1.30      tron {
    572  1.30      tron 	struct ld_ataraid_softc *sc = device_private(self);
    573  1.30      tron 	int error = 0;
    574  1.30      tron 
    575  1.30      tron 	switch (cmd) {
    576  1.30      tron 	case BIOCINQ:
    577  1.30      tron 		error = ld_ataraid_bioinq(sc, (struct bioc_inq *)addr);
    578  1.30      tron 		break;
    579  1.30      tron 	case BIOCVOL:
    580  1.30      tron 		error = ld_ataraid_biovol(sc, (struct bioc_vol *)addr);
    581  1.30      tron 		break;
    582  1.30      tron 	case BIOCDISK:
    583  1.30      tron 		error = ld_ataraid_biodisk(sc, (struct bioc_disk *)addr);
    584  1.30      tron 		break;
    585  1.30      tron 	default:
    586  1.30      tron 		error = ENOTTY;
    587  1.30      tron 		break;
    588  1.30      tron 	}
    589  1.30      tron 
    590  1.30      tron 	return error;
    591  1.30      tron }
    592  1.30      tron 
    593  1.30      tron static int
    594  1.30      tron ld_ataraid_bioinq(struct ld_ataraid_softc *sc, struct bioc_inq *bi)
    595  1.30      tron {
    596  1.30      tron 	struct ataraid_array_info *aai = sc->sc_aai;
    597  1.30      tron 
    598  1.30      tron 	/* there's always one volume per ld device */
    599  1.30      tron 	bi->bi_novol = 1;
    600  1.30      tron 	bi->bi_nodisk = aai->aai_ndisks;
    601  1.30      tron 
    602  1.30      tron 	return 0;
    603  1.30      tron }
    604  1.30      tron 
    605  1.30      tron static int
    606  1.30      tron ld_ataraid_biovol(struct ld_ataraid_softc *sc, struct bioc_vol *bv)
    607  1.30      tron {
    608  1.30      tron 	struct ataraid_array_info *aai = sc->sc_aai;
    609  1.30      tron 	struct ld_softc *ld = &sc->sc_ld;
    610  1.30      tron 
    611  1.30      tron 	/* Fill in data for _this_ volume */
    612  1.30      tron 	bv->bv_percent = -1;
    613  1.30      tron 	bv->bv_seconds = 0;
    614  1.30      tron 
    615  1.30      tron 	switch (aai->aai_status) {
    616  1.30      tron 	case AAI_S_READY:
    617  1.30      tron 		bv->bv_status = BIOC_SVONLINE;
    618  1.30      tron 		break;
    619  1.30      tron 	case AAI_S_DEGRADED:
    620  1.30      tron 		bv->bv_status = BIOC_SVDEGRADED;
    621  1.30      tron 		break;
    622  1.30      tron 	}
    623  1.30      tron 
    624  1.30      tron 	bv->bv_size = ld->sc_secsize * ld->sc_secperunit;
    625  1.30      tron 
    626  1.30      tron 	switch (aai->aai_level) {
    627  1.30      tron 	case AAI_L_SPAN:
    628  1.30      tron 	case AAI_L_RAID0:
    629  1.30      tron 		bv->bv_stripe_size = aai->aai_interleave;
    630  1.30      tron 		bv->bv_level = 0;
    631  1.30      tron 		break;
    632  1.30      tron 	case AAI_L_RAID1:
    633  1.30      tron 		bv->bv_stripe_size = 0;
    634  1.30      tron 		bv->bv_level = 1;
    635  1.30      tron 		break;
    636  1.30      tron 	case AAI_L_RAID5:
    637  1.30      tron 		bv->bv_stripe_size = aai->aai_interleave;
    638  1.30      tron 		bv->bv_level = 5;
    639  1.30      tron 		break;
    640  1.30      tron 	}
    641  1.30      tron 
    642  1.30      tron 	bv->bv_nodisk = aai->aai_ndisks;
    643  1.30      tron 	strlcpy(bv->bv_dev, device_xname(ld->sc_dv), sizeof(bv->bv_dev));
    644  1.30      tron 	if (aai->aai_name)
    645  1.30      tron 		strlcpy(bv->bv_vendor, aai->aai_name,
    646  1.30      tron 		    sizeof(bv->bv_vendor));
    647  1.30      tron 
    648  1.30      tron 	return 0;
    649  1.30      tron }
    650  1.30      tron 
    651  1.30      tron static int
    652  1.30      tron ld_ataraid_biodisk(struct ld_ataraid_softc *sc, struct bioc_disk *bd)
    653  1.30      tron {
    654  1.30      tron 	struct ataraid_array_info *aai = sc->sc_aai;
    655  1.30      tron 	struct ataraid_disk_info *adi;
    656  1.30      tron 	struct ld_softc *ld = &sc->sc_ld;
    657  1.30      tron 	struct atabus_softc *atabus;
    658  1.30      tron 	struct wd_softc *wd;
    659  1.30      tron 	char model[81], serial[41], rev[17];
    660  1.30      tron 
    661  1.30      tron 	/* sanity check */
    662  1.30      tron 	if (bd->bd_diskid > aai->aai_ndisks)
    663  1.30      tron 		return EINVAL;
    664  1.30      tron 
    665  1.30      tron 	adi = &aai->aai_disks[bd->bd_diskid];
    666  1.30      tron 	atabus = device_private(device_parent(adi->adi_dev));
    667  1.30      tron 	wd = device_private(adi->adi_dev);
    668  1.30      tron 
    669  1.30      tron 	/* fill in data for _this_ disk */
    670  1.30      tron 	switch (adi->adi_status) {
    671  1.30      tron 	case ADI_S_ONLINE | ADI_S_ASSIGNED:
    672  1.30      tron 		bd->bd_status = BIOC_SDONLINE;
    673  1.30      tron 		break;
    674  1.30      tron 	case ADI_S_SPARE:
    675  1.30      tron 		bd->bd_status = BIOC_SDHOTSPARE;
    676  1.30      tron 		break;
    677  1.30      tron 	default:
    678  1.30      tron 		bd->bd_status = BIOC_SDOFFLINE;
    679  1.30      tron 		break;
    680  1.30      tron 	}
    681  1.30      tron 
    682  1.30      tron 	bd->bd_channel = 0;
    683  1.30      tron 	bd->bd_target = atabus->sc_chan->ch_channel;
    684  1.30      tron 	bd->bd_lun = 0;
    685  1.30      tron 	bd->bd_size = (wd->sc_capacity * ld->sc_secsize) - aai->aai_reserved;
    686  1.30      tron 
    687  1.30      tron 	strlcpy(bd->bd_procdev, device_xname(adi->adi_dev),
    688  1.30      tron 	    sizeof(bd->bd_procdev));
    689  1.30      tron 
    690  1.30      tron 	scsipi_strvis(serial, sizeof(serial), wd->sc_params.atap_serial,
    691  1.30      tron 	    sizeof(wd->sc_params.atap_serial));
    692  1.30      tron 	scsipi_strvis(model, sizeof(model), wd->sc_params.atap_model,
    693  1.30      tron 	    sizeof(wd->sc_params.atap_model));
    694  1.30      tron 	scsipi_strvis(rev, sizeof(rev), wd->sc_params.atap_revision,
    695  1.30      tron 	    sizeof(wd->sc_params.atap_revision));
    696  1.30      tron 
    697  1.30      tron 	snprintf(bd->bd_vendor, sizeof(bd->bd_vendor), "%s %s", model, rev);
    698  1.30      tron 	strlcpy(bd->bd_serial, serial, sizeof(bd->bd_serial));
    699  1.30      tron 
    700  1.30      tron 	return 0;
    701  1.30      tron }
    702  1.30      tron #endif /* NBIO > 0 */
    703