Home | History | Annotate | Line # | Download | only in dev
ccd.c revision 1.3.2.1
      1  1.3.2.1  mycroft /*	$NetBSD: ccd.c,v 1.3.2.1 1994/08/14 07:41:21 mycroft 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.1  hpeyerl struct	buf *ccdbuffer();
     76      1.1  hpeyerl char	*ccddevtostr();
     77      1.3  hpeyerl void	ccdiodone();
     78      1.1  hpeyerl 
     79      1.3  hpeyerl #define	ccdunit(x)	((minor(x) >> 3) & 0xf)	/* for consistency */
     80      1.1  hpeyerl 
     81      1.1  hpeyerl #define	getcbuf()	\
     82      1.1  hpeyerl 	((struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK))
     83      1.1  hpeyerl #define putcbuf(bp)	\
     84      1.1  hpeyerl 	free((caddr_t)(bp), M_DEVBUF)
     85      1.1  hpeyerl 
     86      1.1  hpeyerl struct ccd_softc {
     87      1.1  hpeyerl 	int		 sc_flags;		/* flags */
     88      1.1  hpeyerl 	size_t		 sc_size;		/* size of ccd */
     89      1.1  hpeyerl 	int		 sc_ileave;		/* interleave */
     90      1.1  hpeyerl 	int		 sc_nccdisks;		/* number of components */
     91      1.1  hpeyerl 	struct ccdcinfo	 sc_cinfo[NCCDISKS];	/* component info */
     92      1.1  hpeyerl 	struct ccdiinfo	 *sc_itable;		/* interleave table */
     93      1.1  hpeyerl 	int		 sc_usecnt;		/* number of requests active */
     94      1.1  hpeyerl 	int		 sc_dk;			/* disk index */
     95      1.3  hpeyerl };
     96      1.1  hpeyerl 
     97      1.1  hpeyerl /* sc_flags */
     98      1.3  hpeyerl #define	CCDF_ALIVE	0x01
     99      1.3  hpeyerl #define CCDF_INITED	0x02
    100      1.3  hpeyerl 
    101      1.3  hpeyerl struct ccd_softc *ccd_softc;
    102      1.3  hpeyerl int numccd;
    103      1.1  hpeyerl 
    104      1.3  hpeyerl /*
    105      1.3  hpeyerl  * Since this is called after auto-configuration of devices,
    106      1.3  hpeyerl  * we can handle the initialization here.
    107      1.3  hpeyerl  *
    108      1.3  hpeyerl  * XXX this will not work if you want to use a ccd as your primary
    109      1.3  hpeyerl  * swap device since swapconf() has been called before now.
    110      1.1  hpeyerl  */
    111      1.1  hpeyerl void
    112      1.3  hpeyerl ccdattach(num)
    113      1.3  hpeyerl 	int num;
    114      1.3  hpeyerl {
    115      1.3  hpeyerl 	char *mem;
    116      1.3  hpeyerl 	register u_long size;
    117      1.3  hpeyerl 	register struct ccddevice *ccd;
    118      1.3  hpeyerl 	extern int dkn;
    119      1.3  hpeyerl 
    120      1.3  hpeyerl 	if (num <= 0)
    121      1.3  hpeyerl 		return;
    122      1.3  hpeyerl 	size = num * sizeof(struct ccd_softc);
    123      1.3  hpeyerl 	mem = malloc(size, M_DEVBUF, M_NOWAIT);
    124      1.3  hpeyerl 	if (mem == NULL) {
    125      1.3  hpeyerl 		printf("WARNING: no memory for concatonated disks\n");
    126      1.3  hpeyerl 		return;
    127      1.3  hpeyerl 	}
    128      1.3  hpeyerl 	bzero(mem, size);
    129      1.3  hpeyerl 	ccd_softc = (struct ccd_softc *)mem;
    130      1.3  hpeyerl 	numccd = num;
    131      1.3  hpeyerl 	for (ccd = ccddevice; ccd->ccd_unit >= 0; ccd++) {
    132      1.3  hpeyerl 		/*
    133      1.3  hpeyerl 		 * XXX
    134      1.3  hpeyerl 		 * Assign disk index first so that init routine
    135      1.3  hpeyerl 		 * can use it (saves having the driver drag around
    136      1.3  hpeyerl 		 * the ccddevice pointer just to set up the dk_*
    137      1.3  hpeyerl 		 * info in the open routine).
    138      1.3  hpeyerl 		 */
    139      1.3  hpeyerl 		if (dkn < DK_NDRIVE)
    140      1.3  hpeyerl 			ccd->ccd_dk = dkn++;
    141      1.3  hpeyerl 		else
    142      1.3  hpeyerl 			ccd->ccd_dk = -1;
    143      1.3  hpeyerl 		if (ccdinit(ccd))
    144      1.3  hpeyerl 			printf("ccd%d configured\n", ccd->ccd_unit);
    145      1.3  hpeyerl 		else if (ccd->ccd_dk >= 0) {
    146      1.3  hpeyerl 			ccd->ccd_dk = -1;
    147      1.3  hpeyerl 			dkn--;
    148      1.3  hpeyerl 		}
    149      1.3  hpeyerl 	}
    150      1.1  hpeyerl }
    151      1.1  hpeyerl 
    152      1.1  hpeyerl ccdinit(ccd)
    153      1.1  hpeyerl 	struct ccddevice *ccd;
    154      1.1  hpeyerl {
    155      1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[ccd->ccd_unit];
    156      1.1  hpeyerl 	register struct ccdcinfo *ci;
    157      1.1  hpeyerl 	register size_t size;
    158      1.1  hpeyerl 	register int ix;
    159      1.1  hpeyerl 	size_t minsize;
    160      1.1  hpeyerl 	dev_t dev;
    161      1.3  hpeyerl 	struct bdevsw *bsw;
    162      1.3  hpeyerl 	int error;
    163      1.3  hpeyerl 	struct proc *p = curproc; /* XXX */
    164      1.1  hpeyerl 
    165      1.1  hpeyerl #ifdef DEBUG
    166      1.3  hpeyerl 	if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    167      1.1  hpeyerl 		printf("ccdinit: unit %d\n", ccd->ccd_unit);
    168      1.1  hpeyerl #endif
    169      1.1  hpeyerl 	cs->sc_dk = ccd->ccd_dk;
    170      1.1  hpeyerl 	cs->sc_size = 0;
    171      1.1  hpeyerl 	cs->sc_ileave = ccd->ccd_interleave;
    172      1.1  hpeyerl 	cs->sc_nccdisks = 0;
    173      1.1  hpeyerl 	/*
    174      1.1  hpeyerl 	 * Verify that each component piece exists and record
    175      1.1  hpeyerl 	 * relevant information about it.
    176      1.1  hpeyerl 	 */
    177      1.1  hpeyerl 	minsize = 0;
    178      1.1  hpeyerl 	for (ix = 0; ix < NCCDISKS; ix++) {
    179      1.1  hpeyerl 		if ((dev = ccd->ccd_dev[ix]) == NODEV)
    180      1.1  hpeyerl 			break;
    181      1.1  hpeyerl 		ci = &cs->sc_cinfo[ix];
    182      1.1  hpeyerl 		ci->ci_dev = dev;
    183      1.3  hpeyerl 		bsw = &bdevsw[major(dev)];
    184      1.3  hpeyerl 		/*
    185      1.3  hpeyerl 		 * Open the partition
    186      1.3  hpeyerl 		 */
    187      1.3  hpeyerl 		if (bsw->d_open &&
    188      1.3  hpeyerl 		    (error = (*bsw->d_open)(dev, 0, S_IFBLK, p))) {
    189      1.3  hpeyerl 			printf("ccd%d: component %s open failed, error = %d\n",
    190      1.3  hpeyerl 			       ccd->ccd_unit, ccddevtostr(dev), error);
    191      1.3  hpeyerl 			return(0);
    192      1.3  hpeyerl 		}
    193      1.1  hpeyerl 		/*
    194      1.1  hpeyerl 		 * Calculate size (truncated to interleave boundary
    195      1.1  hpeyerl 		 * if necessary.
    196      1.1  hpeyerl 		 */
    197      1.3  hpeyerl 		if (bsw->d_psize) {
    198      1.3  hpeyerl 			size = (size_t) (*bsw->d_psize)(dev);
    199      1.1  hpeyerl 			if ((int)size < 0)
    200      1.1  hpeyerl 				size = 0;
    201      1.1  hpeyerl 		} else
    202      1.1  hpeyerl 			size = 0;
    203      1.1  hpeyerl 		if (cs->sc_ileave > 1)
    204      1.1  hpeyerl 			size -= size % cs->sc_ileave;
    205      1.1  hpeyerl 		if (size == 0) {
    206      1.1  hpeyerl 			printf("ccd%d: not configured (component %s missing)\n",
    207      1.3  hpeyerl 			       ccd->ccd_unit, ccddevtostr(dev));
    208      1.1  hpeyerl 			return(0);
    209      1.1  hpeyerl 		}
    210      1.3  hpeyerl #ifdef COMPAT_NOLABEL
    211      1.3  hpeyerl 		/*
    212      1.3  hpeyerl 		 * XXX if this is a 'c' partition then we need to mark the
    213      1.3  hpeyerl 		 * label area writeable since there cannot be a label.
    214      1.3  hpeyerl 		 */
    215      1.3  hpeyerl 		if ((minor(dev) & 7) == 2 && bsw->d_open) {
    216      1.3  hpeyerl 			int i, flag;
    217      1.3  hpeyerl 
    218      1.3  hpeyerl 			for (i = 0; i < nchrdev; i++)
    219  1.3.2.1  mycroft 				if (cdevsw[i].d_open == bsw->d_open)
    220      1.3  hpeyerl 					break;
    221  1.3.2.1  mycroft 			if (i != nchrdev && cdevsw[i].d_ioctl) {
    222      1.3  hpeyerl 				flag = 1;
    223  1.3.2.1  mycroft 				(void)(*cdevsw[i].d_ioctl)(dev, DIOCWLABEL,
    224      1.3  hpeyerl 					(caddr_t)&flag, FWRITE, p);
    225      1.3  hpeyerl 			}
    226      1.3  hpeyerl 		}
    227      1.3  hpeyerl #endif
    228      1.1  hpeyerl 		if (minsize == 0 || size < minsize)
    229      1.1  hpeyerl 			minsize = size;
    230      1.1  hpeyerl 		ci->ci_size = size;
    231      1.1  hpeyerl 		cs->sc_size += size;
    232      1.1  hpeyerl 		cs->sc_nccdisks++;
    233      1.1  hpeyerl 	}
    234      1.1  hpeyerl 	/*
    235      1.1  hpeyerl 	 * If uniform interleave is desired set all sizes to that of
    236      1.1  hpeyerl 	 * the smallest component.
    237      1.1  hpeyerl 	 */
    238      1.3  hpeyerl 	if (ccd->ccd_flags & CCDF_UNIFORM) {
    239      1.1  hpeyerl 		for (ci = cs->sc_cinfo;
    240      1.1  hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    241      1.1  hpeyerl 			ci->ci_size = minsize;
    242      1.1  hpeyerl 		cs->sc_size = cs->sc_nccdisks * minsize;
    243      1.1  hpeyerl 	}
    244      1.1  hpeyerl 	/*
    245      1.1  hpeyerl 	 * Construct the interleave table
    246      1.1  hpeyerl 	 */
    247      1.1  hpeyerl 	if (!ccdinterleave(cs))
    248      1.1  hpeyerl 		return(0);
    249      1.1  hpeyerl 	if (ccd->ccd_dk >= 0)
    250      1.1  hpeyerl 		dk_wpms[ccd->ccd_dk] = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
    251      1.1  hpeyerl 	printf("ccd%d: %d components ", ccd->ccd_unit, cs->sc_nccdisks);
    252      1.1  hpeyerl 	for (ix = 0; ix < cs->sc_nccdisks; ix++)
    253      1.1  hpeyerl 		printf("%c%s%c",
    254      1.1  hpeyerl 		       ix == 0 ? '(' : ' ',
    255      1.1  hpeyerl 		       ccddevtostr(cs->sc_cinfo[ix].ci_dev),
    256      1.1  hpeyerl 		       ix == cs->sc_nccdisks - 1 ? ')' : ',');
    257      1.1  hpeyerl 	printf(", %d blocks ", cs->sc_size);
    258      1.1  hpeyerl 	if (cs->sc_ileave)
    259      1.1  hpeyerl 		printf("interleaved at %d blocks\n", cs->sc_ileave);
    260      1.1  hpeyerl 	else
    261      1.1  hpeyerl 		printf("concatenated\n");
    262      1.3  hpeyerl 	cs->sc_flags = CCDF_ALIVE | CCDF_INITED;
    263      1.1  hpeyerl 	return(1);
    264      1.1  hpeyerl }
    265      1.1  hpeyerl 
    266      1.1  hpeyerl /*
    267      1.1  hpeyerl  * XXX not really ccd specific.
    268      1.3  hpeyerl  * Could be called something like bdevtostr in machine/conf.c.
    269      1.1  hpeyerl  */
    270      1.1  hpeyerl char *
    271      1.1  hpeyerl ccddevtostr(dev)
    272      1.1  hpeyerl 	dev_t dev;
    273      1.1  hpeyerl {
    274      1.1  hpeyerl 	static char dbuf[5];
    275      1.1  hpeyerl 
    276      1.1  hpeyerl 	switch (major(dev)) {
    277      1.3  hpeyerl #ifdef hp300
    278      1.1  hpeyerl 	case 2:
    279      1.3  hpeyerl 		dbuf[0] = 'r'; dbuf[1] = 'd';
    280      1.1  hpeyerl 		break;
    281      1.1  hpeyerl 	case 4:
    282      1.3  hpeyerl 		dbuf[0] = 's'; dbuf[1] = 'd';
    283      1.1  hpeyerl 		break;
    284      1.1  hpeyerl 	case 5:
    285      1.3  hpeyerl 		dbuf[0] = 'c'; dbuf[1] = 'd';
    286      1.3  hpeyerl 		break;
    287      1.3  hpeyerl 	case 6:
    288      1.3  hpeyerl 		dbuf[0] = 'v'; dbuf[1] = 'n';
    289      1.1  hpeyerl 		break;
    290      1.3  hpeyerl #endif
    291      1.1  hpeyerl 	default:
    292      1.1  hpeyerl 		dbuf[0] = dbuf[1] = '?';
    293      1.1  hpeyerl 		break;
    294      1.1  hpeyerl 	}
    295      1.1  hpeyerl 	dbuf[2] = (minor(dev) >> 3) + '0';
    296      1.1  hpeyerl 	dbuf[3] = (minor(dev) & 7) + 'a';
    297      1.1  hpeyerl 	dbuf[4] = '\0';
    298      1.1  hpeyerl 	return (dbuf);
    299      1.1  hpeyerl }
    300      1.1  hpeyerl 
    301      1.1  hpeyerl ccdinterleave(cs)
    302      1.1  hpeyerl 	register struct ccd_softc *cs;
    303      1.1  hpeyerl {
    304      1.1  hpeyerl 	register struct ccdcinfo *ci, *smallci;
    305      1.1  hpeyerl 	register struct ccdiinfo *ii;
    306      1.1  hpeyerl 	register daddr_t bn, lbn;
    307      1.1  hpeyerl 	register int ix;
    308      1.1  hpeyerl 	u_long size;
    309      1.1  hpeyerl 
    310      1.1  hpeyerl #ifdef DEBUG
    311      1.3  hpeyerl 	if (ccddebug & CCDB_INIT)
    312      1.1  hpeyerl 		printf("ccdinterleave(%x): ileave %d\n", cs, cs->sc_ileave);
    313      1.1  hpeyerl #endif
    314      1.1  hpeyerl 	/*
    315      1.1  hpeyerl 	 * Allocate an interleave table.
    316      1.1  hpeyerl 	 * Chances are this is too big, but we don't care.
    317      1.1  hpeyerl 	 */
    318      1.1  hpeyerl 	size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
    319      1.1  hpeyerl 	cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF, M_WAITOK);
    320      1.1  hpeyerl 	bzero((caddr_t)cs->sc_itable, size);
    321      1.1  hpeyerl 	/*
    322      1.1  hpeyerl 	 * Trivial case: no interleave (actually interleave of disk size).
    323      1.1  hpeyerl 	 * Each table entry represent a single component in its entirety.
    324      1.1  hpeyerl 	 */
    325      1.1  hpeyerl 	if (cs->sc_ileave == 0) {
    326      1.1  hpeyerl 		bn = 0;
    327      1.1  hpeyerl 		ii = cs->sc_itable;
    328      1.1  hpeyerl 		for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    329      1.1  hpeyerl 			ii->ii_ndisk = 1;
    330      1.1  hpeyerl 			ii->ii_startblk = bn;
    331      1.1  hpeyerl 			ii->ii_startoff = 0;
    332      1.1  hpeyerl 			ii->ii_index[0] = ix;
    333      1.1  hpeyerl 			bn += cs->sc_cinfo[ix].ci_size;
    334      1.1  hpeyerl 			ii++;
    335      1.1  hpeyerl 		}
    336      1.1  hpeyerl 		ii->ii_ndisk = 0;
    337      1.1  hpeyerl #ifdef DEBUG
    338      1.3  hpeyerl 		if (ccddebug & CCDB_INIT)
    339      1.1  hpeyerl 			printiinfo(cs->sc_itable);
    340      1.1  hpeyerl #endif
    341      1.1  hpeyerl 		return(1);
    342      1.1  hpeyerl 	}
    343      1.1  hpeyerl 	/*
    344      1.1  hpeyerl 	 * The following isn't fast or pretty; it doesn't have to be.
    345      1.1  hpeyerl 	 */
    346      1.1  hpeyerl 	size = 0;
    347      1.1  hpeyerl 	bn = lbn = 0;
    348      1.1  hpeyerl 	for (ii = cs->sc_itable; ; ii++) {
    349      1.1  hpeyerl 		/*
    350      1.1  hpeyerl 		 * Locate the smallest of the remaining components
    351      1.1  hpeyerl 		 */
    352      1.1  hpeyerl 		smallci = NULL;
    353      1.1  hpeyerl 		for (ci = cs->sc_cinfo;
    354      1.1  hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    355      1.1  hpeyerl 			if (ci->ci_size > size &&
    356      1.1  hpeyerl 			    (smallci == NULL ||
    357      1.1  hpeyerl 			     ci->ci_size < smallci->ci_size))
    358      1.1  hpeyerl 				smallci = ci;
    359      1.1  hpeyerl 		/*
    360      1.1  hpeyerl 		 * Nobody left, all done
    361      1.1  hpeyerl 		 */
    362      1.1  hpeyerl 		if (smallci == NULL) {
    363      1.1  hpeyerl 			ii->ii_ndisk = 0;
    364      1.1  hpeyerl 			break;
    365      1.1  hpeyerl 		}
    366      1.1  hpeyerl 		/*
    367      1.1  hpeyerl 		 * Record starting logical block and component offset
    368      1.1  hpeyerl 		 */
    369      1.1  hpeyerl 		ii->ii_startblk = bn / cs->sc_ileave;
    370      1.1  hpeyerl 		ii->ii_startoff = lbn;
    371      1.1  hpeyerl 		/*
    372      1.1  hpeyerl 		 * Determine how many disks take part in this interleave
    373      1.1  hpeyerl 		 * and record their indices.
    374      1.1  hpeyerl 		 */
    375      1.1  hpeyerl 		ix = 0;
    376      1.1  hpeyerl 		for (ci = cs->sc_cinfo;
    377      1.1  hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    378      1.1  hpeyerl 			if (ci->ci_size >= smallci->ci_size)
    379      1.1  hpeyerl 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
    380      1.1  hpeyerl 		ii->ii_ndisk = ix;
    381      1.1  hpeyerl 		bn += ix * (smallci->ci_size - size);
    382      1.1  hpeyerl 		lbn = smallci->ci_size / cs->sc_ileave;
    383      1.1  hpeyerl 		size = smallci->ci_size;
    384      1.1  hpeyerl 	}
    385      1.1  hpeyerl #ifdef DEBUG
    386      1.3  hpeyerl 	if (ccddebug & CCDB_INIT)
    387      1.1  hpeyerl 		printiinfo(cs->sc_itable);
    388      1.1  hpeyerl #endif
    389      1.1  hpeyerl 	return(1);
    390      1.1  hpeyerl }
    391      1.1  hpeyerl 
    392      1.1  hpeyerl #ifdef DEBUG
    393      1.1  hpeyerl printiinfo(ii)
    394      1.1  hpeyerl 	struct ccdiinfo *ii;
    395      1.1  hpeyerl {
    396      1.1  hpeyerl 	register int ix, i;
    397      1.1  hpeyerl 
    398      1.1  hpeyerl 	for (ix = 0; ii->ii_ndisk; ix++, ii++) {
    399      1.1  hpeyerl 		printf(" itab[%d]: #dk %d sblk %d soff %d",
    400      1.1  hpeyerl 		       ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
    401      1.1  hpeyerl 		for (i = 0; i < ii->ii_ndisk; i++)
    402      1.1  hpeyerl 			printf(" %d", ii->ii_index[i]);
    403      1.1  hpeyerl 		printf("\n");
    404      1.1  hpeyerl 	}
    405      1.1  hpeyerl }
    406      1.1  hpeyerl #endif
    407      1.1  hpeyerl 
    408      1.1  hpeyerl ccdopen(dev, flags)
    409      1.1  hpeyerl 	dev_t dev;
    410      1.1  hpeyerl {
    411      1.1  hpeyerl 	int unit = ccdunit(dev);
    412      1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    413      1.1  hpeyerl 
    414      1.1  hpeyerl #ifdef DEBUG
    415      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    416      1.1  hpeyerl 		printf("ccdopen(%x, %x)\n", dev, flags);
    417      1.1  hpeyerl #endif
    418      1.3  hpeyerl 	if (unit >= numccd || (cs->sc_flags & CCDF_ALIVE) == 0)
    419      1.1  hpeyerl 		return(ENXIO);
    420      1.1  hpeyerl 	return(0);
    421      1.1  hpeyerl }
    422      1.1  hpeyerl 
    423      1.1  hpeyerl ccdstrategy(bp)
    424      1.1  hpeyerl 	register struct buf *bp;
    425      1.1  hpeyerl {
    426      1.1  hpeyerl 	register int unit = ccdunit(bp->b_dev);
    427      1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    428      1.1  hpeyerl 	register daddr_t bn;
    429      1.1  hpeyerl 	register int sz, s;
    430      1.1  hpeyerl 
    431      1.1  hpeyerl #ifdef DEBUG
    432      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    433      1.1  hpeyerl 		printf("ccdstrategy(%x): unit %d\n", bp, unit);
    434      1.1  hpeyerl #endif
    435      1.3  hpeyerl 	if ((cs->sc_flags & CCDF_INITED) == 0) {
    436      1.1  hpeyerl 		bp->b_error = ENXIO;
    437      1.1  hpeyerl 		bp->b_flags |= B_ERROR;
    438      1.1  hpeyerl 		goto done;
    439      1.1  hpeyerl 	}
    440      1.1  hpeyerl 	bn = bp->b_blkno;
    441      1.1  hpeyerl 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    442      1.1  hpeyerl 	if (bn < 0 || bn + sz > cs->sc_size) {
    443      1.1  hpeyerl 		sz = cs->sc_size - bn;
    444      1.1  hpeyerl 		if (sz == 0) {
    445      1.1  hpeyerl 			bp->b_resid = bp->b_bcount;
    446      1.1  hpeyerl 			goto done;
    447      1.1  hpeyerl 		}
    448      1.1  hpeyerl 		if (sz < 0) {
    449      1.1  hpeyerl 			bp->b_error = EINVAL;
    450      1.1  hpeyerl 			bp->b_flags |= B_ERROR;
    451      1.1  hpeyerl 			goto done;
    452      1.1  hpeyerl 		}
    453      1.1  hpeyerl 		bp->b_bcount = dbtob(sz);
    454      1.1  hpeyerl 	}
    455      1.1  hpeyerl 	bp->b_resid = bp->b_bcount;
    456      1.1  hpeyerl 	/*
    457      1.1  hpeyerl 	 * "Start" the unit.
    458      1.1  hpeyerl 	 */
    459      1.1  hpeyerl 	s = splbio();
    460      1.3  hpeyerl 	ccdstart(cs, bp);
    461      1.1  hpeyerl 	splx(s);
    462      1.1  hpeyerl 	return;
    463      1.1  hpeyerl done:
    464      1.1  hpeyerl 	biodone(bp);
    465      1.1  hpeyerl }
    466      1.1  hpeyerl 
    467      1.3  hpeyerl ccdstart(cs, bp)
    468      1.3  hpeyerl 	register struct ccd_softc *cs;
    469      1.3  hpeyerl 	register struct buf *bp;
    470      1.1  hpeyerl {
    471      1.1  hpeyerl 	register long bcount, rcount;
    472      1.1  hpeyerl 	struct buf *cbp;
    473      1.1  hpeyerl 	caddr_t addr;
    474      1.1  hpeyerl 	daddr_t bn;
    475      1.1  hpeyerl 
    476      1.1  hpeyerl #ifdef DEBUG
    477      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    478      1.3  hpeyerl 		printf("ccdstart(%x, %x)\n", cs, bp);
    479      1.1  hpeyerl #endif
    480      1.1  hpeyerl 	/*
    481      1.1  hpeyerl 	 * Instumentation (not real meaningful)
    482      1.1  hpeyerl 	 */
    483      1.1  hpeyerl 	cs->sc_usecnt++;
    484      1.1  hpeyerl 	if (cs->sc_dk >= 0) {
    485      1.1  hpeyerl 		dk_busy |= 1 << cs->sc_dk;
    486      1.1  hpeyerl 		dk_xfer[cs->sc_dk]++;
    487      1.1  hpeyerl 		dk_wds[cs->sc_dk] += bp->b_bcount >> 6;
    488      1.1  hpeyerl 	}
    489      1.1  hpeyerl 	/*
    490      1.1  hpeyerl 	 * Allocate component buffers and fire off the requests
    491      1.1  hpeyerl 	 */
    492      1.1  hpeyerl 	bn = bp->b_blkno;
    493      1.3  hpeyerl 	addr = bp->b_data;
    494      1.1  hpeyerl 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    495      1.1  hpeyerl 		cbp = ccdbuffer(cs, bp, bn, addr, bcount);
    496      1.1  hpeyerl 		rcount = cbp->b_bcount;
    497      1.1  hpeyerl 		(*bdevsw[major(cbp->b_dev)].d_strategy)(cbp);
    498      1.1  hpeyerl 		bn += btodb(rcount);
    499      1.1  hpeyerl 		addr += rcount;
    500      1.1  hpeyerl 	}
    501      1.1  hpeyerl }
    502      1.1  hpeyerl 
    503      1.1  hpeyerl /*
    504      1.1  hpeyerl  * Build a component buffer header.
    505      1.1  hpeyerl  */
    506      1.1  hpeyerl struct buf *
    507      1.1  hpeyerl ccdbuffer(cs, bp, bn, addr, bcount)
    508      1.1  hpeyerl 	register struct ccd_softc *cs;
    509      1.1  hpeyerl 	struct buf *bp;
    510      1.1  hpeyerl 	daddr_t bn;
    511      1.1  hpeyerl 	caddr_t addr;
    512      1.1  hpeyerl 	long bcount;
    513      1.1  hpeyerl {
    514      1.1  hpeyerl 	register struct ccdcinfo *ci;
    515      1.1  hpeyerl 	register struct buf *cbp;
    516      1.1  hpeyerl 	register daddr_t cbn, cboff;
    517      1.1  hpeyerl 
    518      1.1  hpeyerl #ifdef DEBUG
    519      1.3  hpeyerl 	if (ccddebug & CCDB_IO)
    520      1.1  hpeyerl 		printf("ccdbuffer(%x, %x, %d, %x, %d)\n",
    521      1.1  hpeyerl 		       cs, bp, bn, addr, bcount);
    522      1.1  hpeyerl #endif
    523      1.1  hpeyerl 	/*
    524      1.1  hpeyerl 	 * Determine which component bn falls in.
    525      1.1  hpeyerl 	 */
    526      1.1  hpeyerl 	cbn = bn;
    527      1.1  hpeyerl 	cboff = 0;
    528      1.1  hpeyerl 	/*
    529      1.1  hpeyerl 	 * Serially concatenated
    530      1.1  hpeyerl 	 */
    531      1.1  hpeyerl 	if (cs->sc_ileave == 0) {
    532      1.1  hpeyerl 		register daddr_t sblk;
    533      1.1  hpeyerl 
    534      1.1  hpeyerl 		sblk = 0;
    535      1.1  hpeyerl 		for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
    536      1.1  hpeyerl 			sblk += ci->ci_size;
    537      1.1  hpeyerl 		cbn -= sblk;
    538      1.1  hpeyerl 	}
    539      1.1  hpeyerl 	/*
    540      1.1  hpeyerl 	 * Interleaved
    541      1.1  hpeyerl 	 */
    542      1.1  hpeyerl 	else {
    543      1.1  hpeyerl 		register struct ccdiinfo *ii;
    544      1.1  hpeyerl 		int ccdisk, off;
    545      1.1  hpeyerl 
    546      1.1  hpeyerl 		cboff = cbn % cs->sc_ileave;
    547      1.1  hpeyerl 		cbn /= cs->sc_ileave;
    548      1.1  hpeyerl 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
    549      1.1  hpeyerl 			if (ii->ii_startblk > cbn)
    550      1.1  hpeyerl 				break;
    551      1.1  hpeyerl 		ii--;
    552      1.1  hpeyerl 		off = cbn - ii->ii_startblk;
    553      1.1  hpeyerl 		if (ii->ii_ndisk == 1) {
    554      1.1  hpeyerl 			ccdisk = ii->ii_index[0];
    555      1.1  hpeyerl 			cbn = ii->ii_startoff + off;
    556      1.1  hpeyerl 		} else {
    557      1.1  hpeyerl 			ccdisk = ii->ii_index[off % ii->ii_ndisk];
    558      1.1  hpeyerl 			cbn = ii->ii_startoff + off / ii->ii_ndisk;
    559      1.1  hpeyerl 		}
    560      1.1  hpeyerl 		cbn *= cs->sc_ileave;
    561      1.1  hpeyerl 		ci = &cs->sc_cinfo[ccdisk];
    562      1.1  hpeyerl 	}
    563      1.1  hpeyerl 	/*
    564      1.1  hpeyerl 	 * Fill in the component buf structure.
    565      1.1  hpeyerl 	 */
    566      1.1  hpeyerl 	cbp = getcbuf();
    567      1.1  hpeyerl 	cbp->b_flags = bp->b_flags | B_CALL;
    568      1.1  hpeyerl 	cbp->b_iodone = ccdiodone;
    569      1.1  hpeyerl 	cbp->b_proc = bp->b_proc;
    570      1.1  hpeyerl 	cbp->b_dev = ci->ci_dev;
    571      1.1  hpeyerl 	cbp->b_blkno = cbn + cboff;
    572      1.3  hpeyerl 	cbp->b_data = addr;
    573      1.1  hpeyerl 	cbp->b_vp = 0;
    574      1.1  hpeyerl 	if (cs->sc_ileave == 0)
    575      1.1  hpeyerl 		cbp->b_bcount = dbtob(ci->ci_size - cbn);
    576      1.1  hpeyerl 	else
    577      1.1  hpeyerl 		cbp->b_bcount = dbtob(cs->sc_ileave - cboff);
    578      1.1  hpeyerl 	if (cbp->b_bcount > bcount)
    579      1.1  hpeyerl 		cbp->b_bcount = bcount;
    580      1.1  hpeyerl 	/*
    581      1.3  hpeyerl 	 * XXX context for ccdiodone
    582      1.1  hpeyerl 	 */
    583      1.1  hpeyerl 	cbp->b_saveaddr = (caddr_t)bp;
    584      1.1  hpeyerl 	cbp->b_pfcent = ((cs - ccd_softc) << 16) | (ci - cs->sc_cinfo);
    585      1.1  hpeyerl #ifdef DEBUG
    586      1.3  hpeyerl 	if (ccddebug & CCDB_IO)
    587      1.1  hpeyerl 		printf(" dev %x(u%d): cbp %x bn %d addr %x bcnt %d\n",
    588      1.1  hpeyerl 		       ci->ci_dev, ci-cs->sc_cinfo, cbp, cbp->b_blkno,
    589      1.3  hpeyerl 		       cbp->b_data, cbp->b_bcount);
    590      1.1  hpeyerl #endif
    591      1.1  hpeyerl 	return(cbp);
    592      1.1  hpeyerl }
    593      1.1  hpeyerl 
    594      1.3  hpeyerl ccdintr(cs, bp)
    595      1.3  hpeyerl 	register struct ccd_softc *cs;
    596      1.3  hpeyerl 	register struct buf *bp;
    597      1.1  hpeyerl {
    598      1.1  hpeyerl 
    599      1.1  hpeyerl #ifdef DEBUG
    600      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    601      1.3  hpeyerl 		printf("ccdintr(%x, %x)\n", cs, bp);
    602      1.1  hpeyerl #endif
    603      1.1  hpeyerl 	/*
    604      1.1  hpeyerl 	 * Request is done for better or worse, wakeup the top half.
    605      1.1  hpeyerl 	 */
    606      1.1  hpeyerl 	if (--cs->sc_usecnt == 0 && cs->sc_dk >= 0)
    607      1.1  hpeyerl 		dk_busy &= ~(1 << cs->sc_dk);
    608      1.1  hpeyerl 	if (bp->b_flags & B_ERROR)
    609      1.1  hpeyerl 		bp->b_resid = bp->b_bcount;
    610      1.1  hpeyerl 	biodone(bp);
    611      1.1  hpeyerl }
    612      1.1  hpeyerl 
    613      1.1  hpeyerl /*
    614      1.1  hpeyerl  * Called by biodone at interrupt time.
    615      1.1  hpeyerl  * Mark the component as done and if all components are done,
    616      1.1  hpeyerl  * take a ccd interrupt.
    617      1.1  hpeyerl  */
    618      1.3  hpeyerl void
    619      1.1  hpeyerl ccdiodone(cbp)
    620      1.1  hpeyerl 	register struct buf *cbp;
    621      1.1  hpeyerl {
    622      1.1  hpeyerl 	register struct buf *bp = (struct buf *)cbp->b_saveaddr;/* XXX */
    623      1.1  hpeyerl 	register int unit = (cbp->b_pfcent >> 16) & 0xFFFF;	/* XXX */
    624      1.1  hpeyerl 	int count, s;
    625      1.1  hpeyerl 
    626      1.1  hpeyerl 	s = splbio();
    627      1.1  hpeyerl #ifdef DEBUG
    628      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    629      1.1  hpeyerl 		printf("ccdiodone(%x)\n", cbp);
    630      1.3  hpeyerl 	if (ccddebug & CCDB_IO) {
    631      1.1  hpeyerl 		printf("ccdiodone: bp %x bcount %d resid %d\n",
    632      1.1  hpeyerl 		       bp, bp->b_bcount, bp->b_resid);
    633      1.1  hpeyerl 		printf(" dev %x(u%d), cbp %x bn %d addr %x bcnt %d\n",
    634      1.1  hpeyerl 		       cbp->b_dev, cbp->b_pfcent & 0xFFFF, cbp,
    635      1.3  hpeyerl 		       cbp->b_blkno, cbp->b_data, cbp->b_bcount);
    636      1.1  hpeyerl 	}
    637      1.1  hpeyerl #endif
    638      1.1  hpeyerl 
    639      1.1  hpeyerl 	if (cbp->b_flags & B_ERROR) {
    640      1.1  hpeyerl 		bp->b_flags |= B_ERROR;
    641      1.1  hpeyerl 		bp->b_error = biowait(cbp);
    642      1.1  hpeyerl #ifdef DEBUG
    643      1.1  hpeyerl 		printf("ccd%d: error %d on component %d\n",
    644      1.1  hpeyerl 		       unit, bp->b_error, cbp->b_pfcent & 0xFFFF);
    645      1.1  hpeyerl #endif
    646      1.1  hpeyerl 	}
    647      1.1  hpeyerl 	count = cbp->b_bcount;
    648      1.1  hpeyerl 	putcbuf(cbp);
    649      1.1  hpeyerl 
    650      1.1  hpeyerl 	/*
    651      1.1  hpeyerl 	 * If all done, "interrupt".
    652      1.1  hpeyerl 	 */
    653      1.1  hpeyerl 	bp->b_resid -= count;
    654      1.1  hpeyerl 	if (bp->b_resid < 0)
    655      1.1  hpeyerl 		panic("ccdiodone: count");
    656      1.3  hpeyerl 	if (bp->b_resid == 0)
    657      1.3  hpeyerl 		ccdintr(&ccd_softc[unit], bp);
    658      1.1  hpeyerl 	splx(s);
    659      1.1  hpeyerl }
    660      1.1  hpeyerl 
    661      1.3  hpeyerl ccdread(dev, uio)
    662      1.3  hpeyerl 	dev_t dev;
    663      1.3  hpeyerl 	struct uio *uio;
    664      1.3  hpeyerl {
    665      1.3  hpeyerl 	register int unit = ccdunit(dev);
    666      1.3  hpeyerl 
    667      1.3  hpeyerl #ifdef DEBUG
    668      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    669      1.3  hpeyerl 		printf("ccdread(%x, %x)\n", dev, uio);
    670      1.3  hpeyerl #endif
    671      1.3  hpeyerl 	return(physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
    672      1.3  hpeyerl }
    673      1.3  hpeyerl 
    674      1.3  hpeyerl ccdwrite(dev, uio)
    675      1.3  hpeyerl 	dev_t dev;
    676      1.3  hpeyerl 	struct uio *uio;
    677      1.3  hpeyerl {
    678      1.3  hpeyerl 	register int unit = ccdunit(dev);
    679      1.3  hpeyerl 
    680      1.3  hpeyerl #ifdef DEBUG
    681      1.3  hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    682      1.3  hpeyerl 		printf("ccdwrite(%x, %x)\n", dev, uio);
    683      1.3  hpeyerl #endif
    684      1.3  hpeyerl 	return(physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
    685      1.3  hpeyerl }
    686      1.3  hpeyerl 
    687      1.3  hpeyerl ccdioctl(dev, cmd, data, flag)
    688      1.1  hpeyerl 	dev_t dev;
    689      1.1  hpeyerl 	int cmd;
    690      1.1  hpeyerl 	caddr_t data;
    691      1.1  hpeyerl 	int flag;
    692      1.1  hpeyerl {
    693      1.1  hpeyerl 	return(EINVAL);
    694      1.1  hpeyerl }
    695      1.1  hpeyerl 
    696      1.1  hpeyerl ccdsize(dev)
    697      1.1  hpeyerl 	dev_t dev;
    698      1.1  hpeyerl {
    699      1.1  hpeyerl 	int unit = ccdunit(dev);
    700      1.1  hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    701      1.1  hpeyerl 
    702      1.3  hpeyerl 	if (unit >= numccd || (cs->sc_flags & CCDF_INITED) == 0)
    703      1.1  hpeyerl 		return(-1);
    704      1.1  hpeyerl 	return(cs->sc_size);
    705      1.1  hpeyerl }
    706      1.1  hpeyerl 
    707      1.1  hpeyerl ccddump(dev)
    708      1.1  hpeyerl {
    709      1.1  hpeyerl 	return(ENXIO);
    710      1.1  hpeyerl }
    711      1.1  hpeyerl #endif
    712