Home | History | Annotate | Line # | Download | only in dev
ccd.c revision 1.6
      1  1.6      cgd /*	$NetBSD: ccd.c,v 1.6 1995/03/02 06:38:11 cgd Exp $      */
      2  1.2      cgd 
      3  1.1  hpeyerl /*
      4  1.1  hpeyerl  * Copyright (c) 1988 University of Utah.
      5  1.3  hpeyerl  * Copyright (c) 1990, 1993
      6  1.3  hpeyerl  *	The Regents of the University of California.  All rights reserved.
      7  1.1  hpeyerl  *
      8  1.1  hpeyerl  * This code is derived from software contributed to Berkeley by
      9  1.1  hpeyerl  * the Systems Programming Group of the University of Utah Computer
     10  1.1  hpeyerl  * Science Department.
     11  1.1  hpeyerl  *
     12  1.1  hpeyerl  * Redistribution and use in source and binary forms, with or without
     13  1.1  hpeyerl  * modification, are permitted provided that the following conditions
     14  1.1  hpeyerl  * are met:
     15  1.1  hpeyerl  * 1. Redistributions of source code must retain the above copyright
     16  1.1  hpeyerl  *    notice, this list of conditions and the following disclaimer.
     17  1.1  hpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1  hpeyerl  *    notice, this list of conditions and the following disclaimer in the
     19  1.1  hpeyerl  *    documentation and/or other materials provided with the distribution.
     20  1.1  hpeyerl  * 3. All advertising materials mentioning features or use of this software
     21  1.1  hpeyerl  *    must display the following acknowledgement:
     22  1.1  hpeyerl  *	This product includes software developed by the University of
     23  1.1  hpeyerl  *	California, Berkeley and its contributors.
     24  1.1  hpeyerl  * 4. Neither the name of the University nor the names of its contributors
     25  1.1  hpeyerl  *    may be used to endorse or promote products derived from this software
     26  1.1  hpeyerl  *    without specific prior written permission.
     27  1.1  hpeyerl  *
     28  1.1  hpeyerl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1  hpeyerl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1  hpeyerl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1  hpeyerl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1  hpeyerl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1  hpeyerl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1  hpeyerl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1  hpeyerl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1  hpeyerl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1  hpeyerl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1  hpeyerl  * SUCH DAMAGE.
     39  1.1  hpeyerl  *
     40  1.2      cgd  * from: Utah $Hdr: cd.c 1.6 90/11/28$
     41  1.2      cgd  *
     42  1.3  hpeyerl  *	@(#)cd.c	8.2 (Berkeley) 11/16/93
     43  1.1  hpeyerl  */
     44  1.1  hpeyerl 
     45  1.1  hpeyerl /*
     46  1.1  hpeyerl  * "Concatenated" disk driver.
     47  1.1  hpeyerl  */
     48  1.1  hpeyerl #include "ccd.h"
     49  1.1  hpeyerl #if NCCD > 0
     50  1.1  hpeyerl 
     51  1.1  hpeyerl #include <sys/param.h>
     52  1.1  hpeyerl #include <sys/systm.h>
     53  1.3  hpeyerl #include <sys/proc.h>
     54  1.1  hpeyerl #include <sys/errno.h>
     55  1.1  hpeyerl #include <sys/dkstat.h>
     56  1.1  hpeyerl #include <sys/buf.h>
     57  1.1  hpeyerl #include <sys/malloc.h>
     58  1.1  hpeyerl #include <sys/conf.h>
     59  1.3  hpeyerl #include <sys/stat.h>
     60  1.3  hpeyerl #ifdef COMPAT_NOLABEL
     61  1.3  hpeyerl #include <sys/ioctl.h>
     62  1.3  hpeyerl #include <sys/disklabel.h>
     63  1.3  hpeyerl #include <sys/fcntl.h>
     64  1.3  hpeyerl #endif
     65  1.1  hpeyerl 
     66  1.1  hpeyerl #include <dev/ccdvar.h>
     67  1.1  hpeyerl 
     68  1.1  hpeyerl #ifdef DEBUG
     69  1.1  hpeyerl int ccddebug = 0x00;
     70  1.3  hpeyerl #define CCDB_FOLLOW	0x01
     71  1.3  hpeyerl #define CCDB_INIT	0x02
     72  1.3  hpeyerl #define CCDB_IO		0x04
     73  1.1  hpeyerl #endif
     74  1.1  hpeyerl 
     75  1.6      cgd #define	ccdunit(x)	DISKUNIT(x)
     76  1.6      cgd 
     77  1.6      cgd struct ccdbuf {
     78  1.6      cgd 	struct buf	cb_buf;		/* new I/O buf */
     79  1.6      cgd 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
     80  1.6      cgd 	int		cb_unit;	/* target unit */
     81  1.6      cgd 	int		cb_comp;	/* target component */
     82  1.6      cgd };
     83  1.6      cgd 
     84  1.6      cgd #define	getccdbuf()	\
     85  1.6      cgd 	((struct ccdbuf *)malloc(sizeof(struct ccdbuf), M_DEVBUF, M_WAITOK))
     86  1.6      cgd #define putccdbuf(cbp)	\
     87  1.6      cgd 	free((caddr_t)(cbp), M_DEVBUF)
     88  1.1  hpeyerl 
     89  1.1  hpeyerl struct ccd_softc {
     90  1.1  hpeyerl 	int		 sc_flags;		/* flags */
     91  1.1  hpeyerl 	size_t		 sc_size;		/* size of ccd */
     92  1.1  hpeyerl 	int		 sc_ileave;		/* interleave */
     93  1.1  hpeyerl 	int		 sc_nccdisks;		/* number of components */
     94  1.1  hpeyerl 	struct ccdcinfo	 sc_cinfo[NCCDISKS];	/* component info */
     95  1.1  hpeyerl 	struct ccdiinfo	 *sc_itable;		/* interleave table */
     96  1.1  hpeyerl 	int		 sc_usecnt;		/* number of requests active */
     97  1.1  hpeyerl 	int		 sc_dk;			/* disk index */
     98  1.3  hpeyerl };
     99  1.1  hpeyerl 
    100  1.6      cgd struct ccdbuf	*ccdbuffer __P((struct ccd_softc *cs, struct buf *bp,
    101  1.6      cgd 		    daddr_t bn, caddr_t addr, long bcount));
    102  1.6      cgd char		*ccddevtostr __P((dev_t));
    103  1.6      cgd void		ccdiodone __P((struct ccdbuf *cbp));
    104  1.6      cgd 
    105  1.1  hpeyerl /* sc_flags */
    106  1.3  hpeyerl #define	CCDF_ALIVE	0x01
    107  1.3  hpeyerl #define CCDF_INITED	0x02
    108  1.3  hpeyerl 
    109  1.3  hpeyerl struct ccd_softc *ccd_softc;
    110  1.3  hpeyerl int numccd;
    111  1.1  hpeyerl 
    112  1.3  hpeyerl /*
    113  1.3  hpeyerl  * Since this is called after auto-configuration of devices,
    114  1.3  hpeyerl  * we can handle the initialization here.
    115  1.3  hpeyerl  *
    116  1.3  hpeyerl  * XXX this will not work if you want to use a ccd as your primary
    117  1.3  hpeyerl  * swap device since swapconf() has been called before now.
    118  1.1  hpeyerl  */
    119  1.1  hpeyerl void
    120  1.3  hpeyerl ccdattach(num)
    121  1.3  hpeyerl 	int num;
    122  1.3  hpeyerl {
    123  1.3  hpeyerl 	char *mem;
    124  1.3  hpeyerl 	register u_long size;
    125  1.3  hpeyerl 	register struct ccddevice *ccd;
    126  1.3  hpeyerl 	extern int dkn;
    127  1.3  hpeyerl 
    128  1.3  hpeyerl 	if (num <= 0)
    129  1.3  hpeyerl 		return;
    130  1.3  hpeyerl 	size = num * sizeof(struct ccd_softc);
    131  1.3  hpeyerl 	mem = malloc(size, M_DEVBUF, M_NOWAIT);
    132  1.3  hpeyerl 	if (mem == NULL) {
    133  1.3  hpeyerl 		printf("WARNING: no memory for concatonated disks\n");
    134  1.3  hpeyerl 		return;
    135  1.3  hpeyerl 	}
    136  1.3  hpeyerl 	bzero(mem, size);
    137  1.3  hpeyerl 	ccd_softc = (struct ccd_softc *)mem;
    138  1.3  hpeyerl 	numccd = num;
    139  1.3  hpeyerl 	for (ccd = ccddevice; ccd->ccd_unit >= 0; ccd++) {
    140  1.3  hpeyerl 		/*
    141  1.3  hpeyerl 		 * XXX
    142  1.3  hpeyerl 		 * Assign disk index first so that init routine
    143  1.3  hpeyerl 		 * can use it (saves having the driver drag around
    144  1.3  hpeyerl 		 * the ccddevice pointer just to set up the dk_*
    145  1.3  hpeyerl 		 * info in the open routine).
    146  1.3  hpeyerl 		 */
    147  1.3  hpeyerl 		if (dkn < DK_NDRIVE)
    148  1.3  hpeyerl 			ccd->ccd_dk = dkn++;
    149  1.3  hpeyerl 		else
    150  1.3  hpeyerl 			ccd->ccd_dk = -1;
    151  1.3  hpeyerl 		if (ccdinit(ccd))
    152  1.3  hpeyerl 			printf("ccd%d configured\n", ccd->ccd_unit);
    153  1.3  hpeyerl 		else if (ccd->ccd_dk >= 0) {
    154  1.3  hpeyerl 			ccd->ccd_dk = -1;
    155  1.3  hpeyerl 			dkn--;
    156  1.3  hpeyerl 		}
    157  1.3  hpeyerl 	}
    158  1.1  hpeyerl }
    159  1.1  hpeyerl 
    160  1.1  hpeyerl ccdinit(ccd)
    161  1.1  hpeyerl 	struct ccddevice *ccd;
    162  1.1  hpeyerl {
    163  1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[ccd->ccd_unit];
    164  1.1  hpeyerl 	register struct ccdcinfo *ci;
    165  1.1  hpeyerl 	register size_t size;
    166  1.1  hpeyerl 	register int ix;
    167  1.1  hpeyerl 	size_t minsize;
    168  1.1  hpeyerl 	dev_t dev;
    169  1.3  hpeyerl 	struct bdevsw *bsw;
    170  1.3  hpeyerl 	int error;
    171  1.3  hpeyerl 	struct proc *p = curproc; /* XXX */
    172  1.1  hpeyerl 
    173  1.1  hpeyerl #ifdef DEBUG
    174  1.3  hpeyerl 	if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    175  1.1  hpeyerl 		printf("ccdinit: unit %d\n", ccd->ccd_unit);
    176  1.1  hpeyerl #endif
    177  1.1  hpeyerl 	cs->sc_dk = ccd->ccd_dk;
    178  1.1  hpeyerl 	cs->sc_size = 0;
    179  1.1  hpeyerl 	cs->sc_ileave = ccd->ccd_interleave;
    180  1.1  hpeyerl 	cs->sc_nccdisks = 0;
    181  1.1  hpeyerl 	/*
    182  1.1  hpeyerl 	 * Verify that each component piece exists and record
    183  1.1  hpeyerl 	 * relevant information about it.
    184  1.1  hpeyerl 	 */
    185  1.1  hpeyerl 	minsize = 0;
    186  1.1  hpeyerl 	for (ix = 0; ix < NCCDISKS; ix++) {
    187  1.1  hpeyerl 		if ((dev = ccd->ccd_dev[ix]) == NODEV)
    188  1.1  hpeyerl 			break;
    189  1.1  hpeyerl 		ci = &cs->sc_cinfo[ix];
    190  1.1  hpeyerl 		ci->ci_dev = dev;
    191  1.3  hpeyerl 		bsw = &bdevsw[major(dev)];
    192  1.3  hpeyerl 		/*
    193  1.3  hpeyerl 		 * Open the partition
    194  1.3  hpeyerl 		 */
    195  1.3  hpeyerl 		if (bsw->d_open &&
    196  1.3  hpeyerl 		    (error = (*bsw->d_open)(dev, 0, S_IFBLK, p))) {
    197  1.3  hpeyerl 			printf("ccd%d: component %s open failed, error = %d\n",
    198  1.3  hpeyerl 			       ccd->ccd_unit, ccddevtostr(dev), error);
    199  1.3  hpeyerl 			return(0);
    200  1.3  hpeyerl 		}
    201  1.1  hpeyerl 		/*
    202  1.1  hpeyerl 		 * Calculate size (truncated to interleave boundary
    203  1.1  hpeyerl 		 * if necessary.
    204  1.1  hpeyerl 		 */
    205  1.3  hpeyerl 		if (bsw->d_psize) {
    206  1.3  hpeyerl 			size = (size_t) (*bsw->d_psize)(dev);
    207  1.1  hpeyerl 			if ((int)size < 0)
    208  1.1  hpeyerl 				size = 0;
    209  1.1  hpeyerl 		} else
    210  1.1  hpeyerl 			size = 0;
    211  1.1  hpeyerl 		if (cs->sc_ileave > 1)
    212  1.1  hpeyerl 			size -= size % cs->sc_ileave;
    213  1.1  hpeyerl 		if (size == 0) {
    214  1.1  hpeyerl 			printf("ccd%d: not configured (component %s missing)\n",
    215  1.3  hpeyerl 			       ccd->ccd_unit, ccddevtostr(dev));
    216  1.1  hpeyerl 			return(0);
    217  1.1  hpeyerl 		}
    218  1.3  hpeyerl #ifdef COMPAT_NOLABEL
    219  1.3  hpeyerl 		/*
    220  1.3  hpeyerl 		 * XXX if this is a 'c' partition then we need to mark the
    221  1.3  hpeyerl 		 * label area writeable since there cannot be a label.
    222  1.3  hpeyerl 		 */
    223  1.3  hpeyerl 		if ((minor(dev) & 7) == 2 && bsw->d_open) {
    224  1.3  hpeyerl 			int i, flag;
    225  1.3  hpeyerl 
    226  1.3  hpeyerl 			for (i = 0; i < nchrdev; i++)
    227  1.4  mycroft 				if (cdevsw[i].d_open == bsw->d_open)
    228  1.3  hpeyerl 					break;
    229  1.4  mycroft 			if (i != nchrdev && cdevsw[i].d_ioctl) {
    230  1.3  hpeyerl 				flag = 1;
    231  1.4  mycroft 				(void)(*cdevsw[i].d_ioctl)(dev, DIOCWLABEL,
    232  1.3  hpeyerl 					(caddr_t)&flag, FWRITE, p);
    233  1.3  hpeyerl 			}
    234  1.3  hpeyerl 		}
    235  1.3  hpeyerl #endif
    236  1.1  hpeyerl 		if (minsize == 0 || size < minsize)
    237  1.1  hpeyerl 			minsize = size;
    238  1.1  hpeyerl 		ci->ci_size = size;
    239  1.1  hpeyerl 		cs->sc_size += size;
    240  1.1  hpeyerl 		cs->sc_nccdisks++;
    241  1.1  hpeyerl 	}
    242  1.1  hpeyerl 	/*
    243  1.1  hpeyerl 	 * If uniform interleave is desired set all sizes to that of
    244  1.1  hpeyerl 	 * the smallest component.
    245  1.1  hpeyerl 	 */
    246  1.3  hpeyerl 	if (ccd->ccd_flags & CCDF_UNIFORM) {
    247  1.1  hpeyerl 		for (ci = cs->sc_cinfo;
    248  1.1  hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    249  1.1  hpeyerl 			ci->ci_size = minsize;
    250  1.1  hpeyerl 		cs->sc_size = cs->sc_nccdisks * minsize;
    251  1.1  hpeyerl 	}
    252  1.1  hpeyerl 	/*
    253  1.1  hpeyerl 	 * Construct the interleave table
    254  1.1  hpeyerl 	 */
    255  1.1  hpeyerl 	if (!ccdinterleave(cs))
    256  1.1  hpeyerl 		return(0);
    257  1.1  hpeyerl 	if (ccd->ccd_dk >= 0)
    258  1.1  hpeyerl 		dk_wpms[ccd->ccd_dk] = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
    259  1.1  hpeyerl 	printf("ccd%d: %d components ", ccd->ccd_unit, cs->sc_nccdisks);
    260  1.1  hpeyerl 	for (ix = 0; ix < cs->sc_nccdisks; ix++)
    261  1.1  hpeyerl 		printf("%c%s%c",
    262  1.1  hpeyerl 		       ix == 0 ? '(' : ' ',
    263  1.1  hpeyerl 		       ccddevtostr(cs->sc_cinfo[ix].ci_dev),
    264  1.1  hpeyerl 		       ix == cs->sc_nccdisks - 1 ? ')' : ',');
    265  1.1  hpeyerl 	printf(", %d blocks ", cs->sc_size);
    266  1.1  hpeyerl 	if (cs->sc_ileave)
    267  1.1  hpeyerl 		printf("interleaved at %d blocks\n", cs->sc_ileave);
    268  1.1  hpeyerl 	else
    269  1.1  hpeyerl 		printf("concatenated\n");
    270  1.3  hpeyerl 	cs->sc_flags = CCDF_ALIVE | CCDF_INITED;
    271  1.1  hpeyerl 	return(1);
    272  1.1  hpeyerl }
    273  1.1  hpeyerl 
    274  1.1  hpeyerl /*
    275  1.1  hpeyerl  * XXX not really ccd specific.
    276  1.3  hpeyerl  * Could be called something like bdevtostr in machine/conf.c.
    277  1.1  hpeyerl  */
    278  1.1  hpeyerl char *
    279  1.1  hpeyerl ccddevtostr(dev)
    280  1.1  hpeyerl 	dev_t dev;
    281  1.1  hpeyerl {
    282  1.1  hpeyerl 	static char dbuf[5];
    283  1.1  hpeyerl 
    284  1.1  hpeyerl 	switch (major(dev)) {
    285  1.3  hpeyerl #ifdef hp300
    286  1.1  hpeyerl 	case 2:
    287  1.3  hpeyerl 		dbuf[0] = 'r'; dbuf[1] = 'd';
    288  1.1  hpeyerl 		break;
    289  1.1  hpeyerl 	case 4:
    290  1.3  hpeyerl 		dbuf[0] = 's'; dbuf[1] = 'd';
    291  1.1  hpeyerl 		break;
    292  1.1  hpeyerl 	case 5:
    293  1.3  hpeyerl 		dbuf[0] = 'c'; dbuf[1] = 'd';
    294  1.3  hpeyerl 		break;
    295  1.3  hpeyerl 	case 6:
    296  1.3  hpeyerl 		dbuf[0] = 'v'; dbuf[1] = 'n';
    297  1.1  hpeyerl 		break;
    298  1.3  hpeyerl #endif
    299  1.1  hpeyerl 	default:
    300  1.1  hpeyerl 		dbuf[0] = dbuf[1] = '?';
    301  1.1  hpeyerl 		break;
    302  1.1  hpeyerl 	}
    303  1.1  hpeyerl 	dbuf[2] = (minor(dev) >> 3) + '0';
    304  1.1  hpeyerl 	dbuf[3] = (minor(dev) & 7) + 'a';
    305  1.1  hpeyerl 	dbuf[4] = '\0';
    306  1.1  hpeyerl 	return (dbuf);
    307  1.1  hpeyerl }
    308  1.1  hpeyerl 
    309  1.1  hpeyerl ccdinterleave(cs)
    310  1.1  hpeyerl 	register struct ccd_softc *cs;
    311  1.1  hpeyerl {
    312  1.1  hpeyerl 	register struct ccdcinfo *ci, *smallci;
    313  1.1  hpeyerl 	register struct ccdiinfo *ii;
    314  1.1  hpeyerl 	register daddr_t bn, lbn;
    315  1.1  hpeyerl 	register int ix;
    316  1.1  hpeyerl 	u_long size;
    317  1.1  hpeyerl 
    318  1.1  hpeyerl #ifdef DEBUG
    319  1.3  hpeyerl 	if (ccddebug & CCDB_INIT)
    320  1.1  hpeyerl 		printf("ccdinterleave(%x): ileave %d\n", cs, cs->sc_ileave);
    321  1.1  hpeyerl #endif
    322  1.1  hpeyerl 	/*
    323  1.1  hpeyerl 	 * Allocate an interleave table.
    324  1.1  hpeyerl 	 * Chances are this is too big, but we don't care.
    325  1.1  hpeyerl 	 */
    326  1.1  hpeyerl 	size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
    327  1.1  hpeyerl 	cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF, M_WAITOK);
    328  1.1  hpeyerl 	bzero((caddr_t)cs->sc_itable, size);
    329  1.1  hpeyerl 	/*
    330  1.1  hpeyerl 	 * Trivial case: no interleave (actually interleave of disk size).
    331  1.1  hpeyerl 	 * Each table entry represent a single component in its entirety.
    332  1.1  hpeyerl 	 */
    333  1.1  hpeyerl 	if (cs->sc_ileave == 0) {
    334  1.1  hpeyerl 		bn = 0;
    335  1.1  hpeyerl 		ii = cs->sc_itable;
    336  1.1  hpeyerl 		for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    337  1.1  hpeyerl 			ii->ii_ndisk = 1;
    338  1.1  hpeyerl 			ii->ii_startblk = bn;
    339  1.1  hpeyerl 			ii->ii_startoff = 0;
    340  1.1  hpeyerl 			ii->ii_index[0] = ix;
    341  1.1  hpeyerl 			bn += cs->sc_cinfo[ix].ci_size;
    342  1.1  hpeyerl 			ii++;
    343  1.1  hpeyerl 		}
    344  1.1  hpeyerl 		ii->ii_ndisk = 0;
    345  1.1  hpeyerl #ifdef DEBUG
    346  1.3  hpeyerl 		if (ccddebug & CCDB_INIT)
    347  1.1  hpeyerl 			printiinfo(cs->sc_itable);
    348  1.1  hpeyerl #endif
    349  1.1  hpeyerl 		return(1);
    350  1.1  hpeyerl 	}
    351  1.1  hpeyerl 	/*
    352  1.1  hpeyerl 	 * The following isn't fast or pretty; it doesn't have to be.
    353  1.1  hpeyerl 	 */
    354  1.1  hpeyerl 	size = 0;
    355  1.1  hpeyerl 	bn = lbn = 0;
    356  1.1  hpeyerl 	for (ii = cs->sc_itable; ; ii++) {
    357  1.1  hpeyerl 		/*
    358  1.1  hpeyerl 		 * Locate the smallest of the remaining components
    359  1.1  hpeyerl 		 */
    360  1.1  hpeyerl 		smallci = NULL;
    361  1.1  hpeyerl 		for (ci = cs->sc_cinfo;
    362  1.1  hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    363  1.1  hpeyerl 			if (ci->ci_size > size &&
    364  1.1  hpeyerl 			    (smallci == NULL ||
    365  1.1  hpeyerl 			     ci->ci_size < smallci->ci_size))
    366  1.1  hpeyerl 				smallci = ci;
    367  1.1  hpeyerl 		/*
    368  1.1  hpeyerl 		 * Nobody left, all done
    369  1.1  hpeyerl 		 */
    370  1.1  hpeyerl 		if (smallci == NULL) {
    371  1.1  hpeyerl 			ii->ii_ndisk = 0;
    372  1.1  hpeyerl 			break;
    373  1.1  hpeyerl 		}
    374  1.1  hpeyerl 		/*
    375  1.1  hpeyerl 		 * Record starting logical block and component offset
    376  1.1  hpeyerl 		 */
    377  1.1  hpeyerl 		ii->ii_startblk = bn / cs->sc_ileave;
    378  1.1  hpeyerl 		ii->ii_startoff = lbn;
    379  1.1  hpeyerl 		/*
    380  1.1  hpeyerl 		 * Determine how many disks take part in this interleave
    381  1.1  hpeyerl 		 * and record their indices.
    382  1.1  hpeyerl 		 */
    383  1.1  hpeyerl 		ix = 0;
    384  1.1  hpeyerl 		for (ci = cs->sc_cinfo;
    385  1.1  hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    386  1.1  hpeyerl 			if (ci->ci_size >= smallci->ci_size)
    387  1.1  hpeyerl 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
    388  1.1  hpeyerl 		ii->ii_ndisk = ix;
    389  1.1  hpeyerl 		bn += ix * (smallci->ci_size - size);
    390  1.1  hpeyerl 		lbn = smallci->ci_size / cs->sc_ileave;
    391  1.1  hpeyerl 		size = smallci->ci_size;
    392  1.1  hpeyerl 	}
    393  1.1  hpeyerl #ifdef DEBUG
    394  1.3  hpeyerl 	if (ccddebug & CCDB_INIT)
    395  1.1  hpeyerl 		printiinfo(cs->sc_itable);
    396  1.1  hpeyerl #endif
    397  1.1  hpeyerl 	return(1);
    398  1.1  hpeyerl }
    399  1.1  hpeyerl 
    400  1.1  hpeyerl #ifdef DEBUG
    401  1.1  hpeyerl printiinfo(ii)
    402  1.1  hpeyerl 	struct ccdiinfo *ii;
    403  1.1  hpeyerl {
    404  1.1  hpeyerl 	register int ix, i;
    405  1.1  hpeyerl 
    406  1.1  hpeyerl 	for (ix = 0; ii->ii_ndisk; ix++, ii++) {
    407  1.1  hpeyerl 		printf(" itab[%d]: #dk %d sblk %d soff %d",
    408  1.1  hpeyerl 		       ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
    409  1.1  hpeyerl 		for (i = 0; i < ii->ii_ndisk; i++)
    410  1.1  hpeyerl 			printf(" %d", ii->ii_index[i]);
    411  1.1  hpeyerl 		printf("\n");
    412  1.1  hpeyerl 	}
    413  1.1  hpeyerl }
    414  1.1  hpeyerl #endif
    415  1.1  hpeyerl 
    416  1.1  hpeyerl ccdopen(dev, flags)
    417  1.1  hpeyerl 	dev_t dev;
    418  1.1  hpeyerl {
    419  1.1  hpeyerl 	int unit = ccdunit(dev);
    420  1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    421  1.1  hpeyerl 
    422  1.1  hpeyerl #ifdef DEBUG
    423  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    424  1.1  hpeyerl 		printf("ccdopen(%x, %x)\n", dev, flags);
    425  1.1  hpeyerl #endif
    426  1.3  hpeyerl 	if (unit >= numccd || (cs->sc_flags & CCDF_ALIVE) == 0)
    427  1.1  hpeyerl 		return(ENXIO);
    428  1.1  hpeyerl 	return(0);
    429  1.1  hpeyerl }
    430  1.1  hpeyerl 
    431  1.1  hpeyerl ccdstrategy(bp)
    432  1.1  hpeyerl 	register struct buf *bp;
    433  1.1  hpeyerl {
    434  1.1  hpeyerl 	register int unit = ccdunit(bp->b_dev);
    435  1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    436  1.1  hpeyerl 	register daddr_t bn;
    437  1.1  hpeyerl 	register int sz, s;
    438  1.1  hpeyerl 
    439  1.1  hpeyerl #ifdef DEBUG
    440  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    441  1.1  hpeyerl 		printf("ccdstrategy(%x): unit %d\n", bp, unit);
    442  1.1  hpeyerl #endif
    443  1.3  hpeyerl 	if ((cs->sc_flags & CCDF_INITED) == 0) {
    444  1.1  hpeyerl 		bp->b_error = ENXIO;
    445  1.1  hpeyerl 		bp->b_flags |= B_ERROR;
    446  1.1  hpeyerl 		goto done;
    447  1.1  hpeyerl 	}
    448  1.1  hpeyerl 	bn = bp->b_blkno;
    449  1.1  hpeyerl 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    450  1.1  hpeyerl 	if (bn < 0 || bn + sz > cs->sc_size) {
    451  1.1  hpeyerl 		sz = cs->sc_size - bn;
    452  1.1  hpeyerl 		if (sz == 0) {
    453  1.1  hpeyerl 			bp->b_resid = bp->b_bcount;
    454  1.1  hpeyerl 			goto done;
    455  1.1  hpeyerl 		}
    456  1.1  hpeyerl 		if (sz < 0) {
    457  1.1  hpeyerl 			bp->b_error = EINVAL;
    458  1.1  hpeyerl 			bp->b_flags |= B_ERROR;
    459  1.1  hpeyerl 			goto done;
    460  1.1  hpeyerl 		}
    461  1.1  hpeyerl 		bp->b_bcount = dbtob(sz);
    462  1.1  hpeyerl 	}
    463  1.1  hpeyerl 	bp->b_resid = bp->b_bcount;
    464  1.1  hpeyerl 	/*
    465  1.1  hpeyerl 	 * "Start" the unit.
    466  1.1  hpeyerl 	 */
    467  1.1  hpeyerl 	s = splbio();
    468  1.3  hpeyerl 	ccdstart(cs, bp);
    469  1.1  hpeyerl 	splx(s);
    470  1.1  hpeyerl 	return;
    471  1.1  hpeyerl done:
    472  1.1  hpeyerl 	biodone(bp);
    473  1.1  hpeyerl }
    474  1.1  hpeyerl 
    475  1.3  hpeyerl ccdstart(cs, bp)
    476  1.3  hpeyerl 	register struct ccd_softc *cs;
    477  1.3  hpeyerl 	register struct buf *bp;
    478  1.1  hpeyerl {
    479  1.1  hpeyerl 	register long bcount, rcount;
    480  1.6      cgd 	struct ccdbuf *cbp;
    481  1.1  hpeyerl 	caddr_t addr;
    482  1.1  hpeyerl 	daddr_t bn;
    483  1.1  hpeyerl 
    484  1.1  hpeyerl #ifdef DEBUG
    485  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    486  1.3  hpeyerl 		printf("ccdstart(%x, %x)\n", cs, bp);
    487  1.1  hpeyerl #endif
    488  1.1  hpeyerl 	/*
    489  1.1  hpeyerl 	 * Instumentation (not real meaningful)
    490  1.1  hpeyerl 	 */
    491  1.1  hpeyerl 	cs->sc_usecnt++;
    492  1.1  hpeyerl 	if (cs->sc_dk >= 0) {
    493  1.1  hpeyerl 		dk_busy |= 1 << cs->sc_dk;
    494  1.1  hpeyerl 		dk_xfer[cs->sc_dk]++;
    495  1.1  hpeyerl 		dk_wds[cs->sc_dk] += bp->b_bcount >> 6;
    496  1.1  hpeyerl 	}
    497  1.1  hpeyerl 	/*
    498  1.1  hpeyerl 	 * Allocate component buffers and fire off the requests
    499  1.1  hpeyerl 	 */
    500  1.1  hpeyerl 	bn = bp->b_blkno;
    501  1.3  hpeyerl 	addr = bp->b_data;
    502  1.1  hpeyerl 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    503  1.1  hpeyerl 		cbp = ccdbuffer(cs, bp, bn, addr, bcount);
    504  1.6      cgd 		rcount = cbp->cb_buf.b_bcount;
    505  1.6      cgd 		(*bdevsw[major(cbp->cb_buf.b_dev)].d_strategy)(&cbp->cb_buf);
    506  1.1  hpeyerl 		bn += btodb(rcount);
    507  1.1  hpeyerl 		addr += rcount;
    508  1.1  hpeyerl 	}
    509  1.1  hpeyerl }
    510  1.1  hpeyerl 
    511  1.1  hpeyerl /*
    512  1.1  hpeyerl  * Build a component buffer header.
    513  1.1  hpeyerl  */
    514  1.6      cgd struct ccdbuf *
    515  1.1  hpeyerl ccdbuffer(cs, bp, bn, addr, bcount)
    516  1.1  hpeyerl 	register struct ccd_softc *cs;
    517  1.1  hpeyerl 	struct buf *bp;
    518  1.1  hpeyerl 	daddr_t bn;
    519  1.1  hpeyerl 	caddr_t addr;
    520  1.1  hpeyerl 	long bcount;
    521  1.1  hpeyerl {
    522  1.1  hpeyerl 	register struct ccdcinfo *ci;
    523  1.6      cgd 	register struct ccdbuf *cbp;
    524  1.1  hpeyerl 	register daddr_t cbn, cboff;
    525  1.1  hpeyerl 
    526  1.1  hpeyerl #ifdef DEBUG
    527  1.3  hpeyerl 	if (ccddebug & CCDB_IO)
    528  1.1  hpeyerl 		printf("ccdbuffer(%x, %x, %d, %x, %d)\n",
    529  1.1  hpeyerl 		       cs, bp, bn, addr, bcount);
    530  1.1  hpeyerl #endif
    531  1.1  hpeyerl 	/*
    532  1.1  hpeyerl 	 * Determine which component bn falls in.
    533  1.1  hpeyerl 	 */
    534  1.1  hpeyerl 	cbn = bn;
    535  1.1  hpeyerl 	cboff = 0;
    536  1.1  hpeyerl 	/*
    537  1.1  hpeyerl 	 * Serially concatenated
    538  1.1  hpeyerl 	 */
    539  1.1  hpeyerl 	if (cs->sc_ileave == 0) {
    540  1.1  hpeyerl 		register daddr_t sblk;
    541  1.1  hpeyerl 
    542  1.1  hpeyerl 		sblk = 0;
    543  1.1  hpeyerl 		for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
    544  1.1  hpeyerl 			sblk += ci->ci_size;
    545  1.1  hpeyerl 		cbn -= sblk;
    546  1.1  hpeyerl 	}
    547  1.1  hpeyerl 	/*
    548  1.1  hpeyerl 	 * Interleaved
    549  1.1  hpeyerl 	 */
    550  1.1  hpeyerl 	else {
    551  1.1  hpeyerl 		register struct ccdiinfo *ii;
    552  1.1  hpeyerl 		int ccdisk, off;
    553  1.1  hpeyerl 
    554  1.1  hpeyerl 		cboff = cbn % cs->sc_ileave;
    555  1.1  hpeyerl 		cbn /= cs->sc_ileave;
    556  1.1  hpeyerl 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
    557  1.1  hpeyerl 			if (ii->ii_startblk > cbn)
    558  1.1  hpeyerl 				break;
    559  1.1  hpeyerl 		ii--;
    560  1.1  hpeyerl 		off = cbn - ii->ii_startblk;
    561  1.1  hpeyerl 		if (ii->ii_ndisk == 1) {
    562  1.1  hpeyerl 			ccdisk = ii->ii_index[0];
    563  1.1  hpeyerl 			cbn = ii->ii_startoff + off;
    564  1.1  hpeyerl 		} else {
    565  1.1  hpeyerl 			ccdisk = ii->ii_index[off % ii->ii_ndisk];
    566  1.1  hpeyerl 			cbn = ii->ii_startoff + off / ii->ii_ndisk;
    567  1.1  hpeyerl 		}
    568  1.1  hpeyerl 		cbn *= cs->sc_ileave;
    569  1.1  hpeyerl 		ci = &cs->sc_cinfo[ccdisk];
    570  1.1  hpeyerl 	}
    571  1.1  hpeyerl 	/*
    572  1.1  hpeyerl 	 * Fill in the component buf structure.
    573  1.1  hpeyerl 	 */
    574  1.6      cgd 	cbp = getccdbuf();
    575  1.6      cgd 	cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
    576  1.6      cgd 	cbp->cb_buf.b_iodone = (void (*)())ccdiodone;
    577  1.6      cgd 	cbp->cb_buf.b_proc = bp->b_proc;
    578  1.6      cgd 	cbp->cb_buf.b_dev = ci->ci_dev;
    579  1.6      cgd 	cbp->cb_buf.b_blkno = cbn + cboff;
    580  1.6      cgd 	cbp->cb_buf.b_data = addr;
    581  1.6      cgd 	cbp->cb_buf.b_vp = 0;
    582  1.1  hpeyerl 	if (cs->sc_ileave == 0)
    583  1.6      cgd 		cbp->cb_buf.b_bcount = dbtob(ci->ci_size - cbn);
    584  1.1  hpeyerl 	else
    585  1.6      cgd 		cbp->cb_buf.b_bcount = dbtob(cs->sc_ileave - cboff);
    586  1.6      cgd 	if (cbp->cb_buf.b_bcount > bcount)
    587  1.6      cgd 		cbp->cb_buf.b_bcount = bcount;
    588  1.6      cgd 
    589  1.1  hpeyerl 	/*
    590  1.6      cgd 	 * context for ccdiodone
    591  1.1  hpeyerl 	 */
    592  1.6      cgd 	cbp->cb_obp = bp;
    593  1.6      cgd 	cbp->cb_unit = cs - ccd_softc;
    594  1.6      cgd 	cbp->cb_comp = ci - cs->sc_cinfo;
    595  1.6      cgd 
    596  1.1  hpeyerl #ifdef DEBUG
    597  1.3  hpeyerl 	if (ccddebug & CCDB_IO)
    598  1.1  hpeyerl 		printf(" dev %x(u%d): cbp %x bn %d addr %x bcnt %d\n",
    599  1.6      cgd 		       ci->ci_dev, ci-cs->sc_cinfo, cbp, cbp->cb_buf.b_blkno,
    600  1.6      cgd 		       cbp->cb_buf.b_data, cbp->cb_buf.b_bcount);
    601  1.1  hpeyerl #endif
    602  1.6      cgd 	return (cbp);
    603  1.1  hpeyerl }
    604  1.1  hpeyerl 
    605  1.3  hpeyerl ccdintr(cs, bp)
    606  1.3  hpeyerl 	register struct ccd_softc *cs;
    607  1.3  hpeyerl 	register struct buf *bp;
    608  1.1  hpeyerl {
    609  1.1  hpeyerl 
    610  1.1  hpeyerl #ifdef DEBUG
    611  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    612  1.3  hpeyerl 		printf("ccdintr(%x, %x)\n", cs, bp);
    613  1.1  hpeyerl #endif
    614  1.1  hpeyerl 	/*
    615  1.1  hpeyerl 	 * Request is done for better or worse, wakeup the top half.
    616  1.1  hpeyerl 	 */
    617  1.1  hpeyerl 	if (--cs->sc_usecnt == 0 && cs->sc_dk >= 0)
    618  1.1  hpeyerl 		dk_busy &= ~(1 << cs->sc_dk);
    619  1.1  hpeyerl 	if (bp->b_flags & B_ERROR)
    620  1.1  hpeyerl 		bp->b_resid = bp->b_bcount;
    621  1.1  hpeyerl 	biodone(bp);
    622  1.1  hpeyerl }
    623  1.1  hpeyerl 
    624  1.1  hpeyerl /*
    625  1.1  hpeyerl  * Called by biodone at interrupt time.
    626  1.1  hpeyerl  * Mark the component as done and if all components are done,
    627  1.1  hpeyerl  * take a ccd interrupt.
    628  1.1  hpeyerl  */
    629  1.3  hpeyerl void
    630  1.1  hpeyerl ccdiodone(cbp)
    631  1.6      cgd 	register struct ccdbuf *cbp;
    632  1.1  hpeyerl {
    633  1.6      cgd 	register struct buf *bp = cbp->cb_obp;
    634  1.6      cgd 	register int unit = cbp->cb_unit;
    635  1.1  hpeyerl 	int count, s;
    636  1.1  hpeyerl 
    637  1.1  hpeyerl 	s = splbio();
    638  1.1  hpeyerl #ifdef DEBUG
    639  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    640  1.1  hpeyerl 		printf("ccdiodone(%x)\n", cbp);
    641  1.3  hpeyerl 	if (ccddebug & CCDB_IO) {
    642  1.1  hpeyerl 		printf("ccdiodone: bp %x bcount %d resid %d\n",
    643  1.1  hpeyerl 		       bp, bp->b_bcount, bp->b_resid);
    644  1.1  hpeyerl 		printf(" dev %x(u%d), cbp %x bn %d addr %x bcnt %d\n",
    645  1.6      cgd 		       cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
    646  1.6      cgd 		       cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    647  1.6      cgd 		       cbp->cb_buf.b_bcount);
    648  1.1  hpeyerl 	}
    649  1.1  hpeyerl #endif
    650  1.1  hpeyerl 
    651  1.6      cgd 	if (cbp->cb_buf.b_flags & B_ERROR) {
    652  1.1  hpeyerl 		bp->b_flags |= B_ERROR;
    653  1.6      cgd 		bp->b_error = cbp->cb_buf.b_error ? cbp->cb_buf.b_error : EIO;
    654  1.1  hpeyerl #ifdef DEBUG
    655  1.1  hpeyerl 		printf("ccd%d: error %d on component %d\n",
    656  1.6      cgd 		       unit, bp->b_error, cbp->cb_comp);
    657  1.1  hpeyerl #endif
    658  1.1  hpeyerl 	}
    659  1.6      cgd 	count = cbp->cb_buf.b_bcount;
    660  1.6      cgd 	putccdbuf(cbp);
    661  1.1  hpeyerl 
    662  1.1  hpeyerl 	/*
    663  1.1  hpeyerl 	 * If all done, "interrupt".
    664  1.1  hpeyerl 	 */
    665  1.1  hpeyerl 	bp->b_resid -= count;
    666  1.1  hpeyerl 	if (bp->b_resid < 0)
    667  1.1  hpeyerl 		panic("ccdiodone: count");
    668  1.3  hpeyerl 	if (bp->b_resid == 0)
    669  1.3  hpeyerl 		ccdintr(&ccd_softc[unit], bp);
    670  1.1  hpeyerl 	splx(s);
    671  1.1  hpeyerl }
    672  1.1  hpeyerl 
    673  1.3  hpeyerl ccdread(dev, uio)
    674  1.3  hpeyerl 	dev_t dev;
    675  1.3  hpeyerl 	struct uio *uio;
    676  1.3  hpeyerl {
    677  1.3  hpeyerl 	register int unit = ccdunit(dev);
    678  1.3  hpeyerl 
    679  1.3  hpeyerl #ifdef DEBUG
    680  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    681  1.3  hpeyerl 		printf("ccdread(%x, %x)\n", dev, uio);
    682  1.3  hpeyerl #endif
    683  1.3  hpeyerl 	return(physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
    684  1.3  hpeyerl }
    685  1.3  hpeyerl 
    686  1.3  hpeyerl ccdwrite(dev, uio)
    687  1.3  hpeyerl 	dev_t dev;
    688  1.3  hpeyerl 	struct uio *uio;
    689  1.3  hpeyerl {
    690  1.3  hpeyerl 	register int unit = ccdunit(dev);
    691  1.3  hpeyerl 
    692  1.3  hpeyerl #ifdef DEBUG
    693  1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    694  1.3  hpeyerl 		printf("ccdwrite(%x, %x)\n", dev, uio);
    695  1.3  hpeyerl #endif
    696  1.3  hpeyerl 	return(physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
    697  1.3  hpeyerl }
    698  1.3  hpeyerl 
    699  1.3  hpeyerl ccdioctl(dev, cmd, data, flag)
    700  1.1  hpeyerl 	dev_t dev;
    701  1.5      cgd 	u_long cmd;
    702  1.1  hpeyerl 	caddr_t data;
    703  1.1  hpeyerl 	int flag;
    704  1.1  hpeyerl {
    705  1.1  hpeyerl 	return(EINVAL);
    706  1.1  hpeyerl }
    707  1.1  hpeyerl 
    708  1.1  hpeyerl ccdsize(dev)
    709  1.1  hpeyerl 	dev_t dev;
    710  1.1  hpeyerl {
    711  1.1  hpeyerl 	int unit = ccdunit(dev);
    712  1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    713  1.1  hpeyerl 
    714  1.3  hpeyerl 	if (unit >= numccd || (cs->sc_flags & CCDF_INITED) == 0)
    715  1.1  hpeyerl 		return(-1);
    716  1.1  hpeyerl 	return(cs->sc_size);
    717  1.1  hpeyerl }
    718  1.1  hpeyerl 
    719  1.1  hpeyerl ccddump(dev)
    720  1.1  hpeyerl {
    721  1.1  hpeyerl 	return(ENXIO);
    722  1.1  hpeyerl }
    723  1.1  hpeyerl #endif
    724