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