Home | History | Annotate | Line # | Download | only in vme
xd.c revision 1.59.8.2
      1  1.59.8.2      yamt /*	$NetBSD: xd.c,v 1.59.8.2 2006/05/24 10:58:31 yamt Exp $	*/
      2       1.1        pk 
      3       1.1        pk /*
      4       1.1        pk  *
      5       1.1        pk  * Copyright (c) 1995 Charles D. Cranor
      6       1.1        pk  * All rights reserved.
      7       1.1        pk  *
      8       1.1        pk  * Redistribution and use in source and binary forms, with or without
      9       1.1        pk  * modification, are permitted provided that the following conditions
     10       1.1        pk  * are met:
     11       1.1        pk  * 1. Redistributions of source code must retain the above copyright
     12       1.1        pk  *    notice, this list of conditions and the following disclaimer.
     13       1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     15       1.1        pk  *    documentation and/or other materials provided with the distribution.
     16       1.1        pk  * 3. All advertising materials mentioning features or use of this software
     17       1.1        pk  *    must display the following acknowledgement:
     18       1.1        pk  *      This product includes software developed by Charles D. Cranor.
     19       1.1        pk  * 4. The name of the author may not be used to endorse or promote products
     20       1.1        pk  *    derived from this software without specific prior written permission.
     21       1.1        pk  *
     22       1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23       1.1        pk  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1        pk  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1        pk  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26       1.1        pk  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27       1.1        pk  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28       1.1        pk  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29       1.1        pk  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30       1.1        pk  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31       1.1        pk  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1        pk  */
     33       1.1        pk 
     34       1.1        pk /*
     35       1.1        pk  *
     36       1.1        pk  * x d . c   x y l o g i c s   7 5 3 / 7 0 5 3   v m e / s m d   d r i v e r
     37       1.1        pk  *
     38       1.1        pk  * author: Chuck Cranor <chuck (at) ccrc.wustl.edu>
     39       1.1        pk  * started: 27-Feb-95
     40       1.1        pk  * references: [1] Xylogics Model 753 User's Manual
     41       1.1        pk  *                 part number: 166-753-001, Revision B, May 21, 1988.
     42       1.1        pk  *                 "Your Partner For Performance"
     43       1.1        pk  *             [2] other NetBSD disk device drivers
     44       1.1        pk  *
     45       1.1        pk  * Special thanks go to Scott E. Campbell of Xylogics, Inc. for taking
     46       1.1        pk  * the time to answer some of my questions about the 753/7053.
     47       1.1        pk  *
     48       1.1        pk  * note: the 753 and the 7053 are programmed the same way, but are
     49       1.1        pk  * different sizes.   the 753 is a 6U VME card, while the 7053 is a 9U
     50       1.1        pk  * VME card (found in many VME based suns).
     51       1.1        pk  */
     52      1.39     lukem 
     53      1.39     lukem #include <sys/cdefs.h>
     54  1.59.8.2      yamt __KERNEL_RCSID(0, "$NetBSD: xd.c,v 1.59.8.2 2006/05/24 10:58:31 yamt Exp $");
     55       1.1        pk 
     56       1.1        pk #undef XDC_DEBUG		/* full debug */
     57       1.1        pk #define XDC_DIAG		/* extra sanity checks */
     58       1.1        pk #if defined(DIAGNOSTIC) && !defined(XDC_DIAG)
     59       1.1        pk #define XDC_DIAG		/* link in with master DIAG option */
     60       1.1        pk #endif
     61       1.1        pk 
     62       1.1        pk #include <sys/param.h>
     63       1.1        pk #include <sys/proc.h>
     64       1.1        pk #include <sys/systm.h>
     65       1.1        pk #include <sys/kernel.h>
     66       1.1        pk #include <sys/file.h>
     67       1.1        pk #include <sys/stat.h>
     68       1.1        pk #include <sys/ioctl.h>
     69       1.1        pk #include <sys/buf.h>
     70      1.55      yamt #include <sys/bufq.h>
     71       1.1        pk #include <sys/uio.h>
     72       1.1        pk #include <sys/malloc.h>
     73       1.1        pk #include <sys/device.h>
     74       1.1        pk #include <sys/disklabel.h>
     75       1.1        pk #include <sys/disk.h>
     76       1.1        pk #include <sys/syslog.h>
     77       1.1        pk #include <sys/dkbad.h>
     78       1.1        pk #include <sys/conf.h>
     79  1.59.8.2      yamt #include <sys/kauth.h>
     80       1.1        pk 
     81       1.1        pk #include <machine/bus.h>
     82      1.15  drochner #include <machine/intr.h>
     83      1.13       mrg 
     84      1.27       chs #if defined(__sparc__) || defined(sun3)
     85      1.13       mrg #include <dev/sun/disklabel.h>
     86      1.14        pk #endif
     87       1.1        pk 
     88      1.30        pk #include <dev/vme/vmereg.h>
     89       1.1        pk #include <dev/vme/vmevar.h>
     90       1.1        pk 
     91       1.1        pk #include <dev/vme/xdreg.h>
     92       1.1        pk #include <dev/vme/xdvar.h>
     93       1.1        pk #include <dev/vme/xio.h>
     94       1.1        pk 
     95       1.1        pk #include "locators.h"
     96       1.1        pk 
     97       1.1        pk /*
     98       1.1        pk  * macros
     99       1.1        pk  */
    100       1.1        pk 
    101       1.1        pk /*
    102       1.1        pk  * XDC_TWAIT: add iorq "N" to tail of SC's wait queue
    103       1.1        pk  */
    104       1.1        pk #define XDC_TWAIT(SC, N) { \
    105       1.1        pk 	(SC)->waitq[(SC)->waitend] = (N); \
    106       1.1        pk 	(SC)->waitend = ((SC)->waitend + 1) % XDC_MAXIOPB; \
    107       1.1        pk 	(SC)->nwait++; \
    108       1.1        pk }
    109       1.1        pk 
    110       1.1        pk /*
    111       1.1        pk  * XDC_HWAIT: add iorq "N" to head of SC's wait queue
    112       1.1        pk  */
    113       1.1        pk #define XDC_HWAIT(SC, N) { \
    114       1.1        pk 	(SC)->waithead = ((SC)->waithead == 0) ? \
    115       1.1        pk 		(XDC_MAXIOPB - 1) : ((SC)->waithead - 1); \
    116       1.1        pk 	(SC)->waitq[(SC)->waithead] = (N); \
    117       1.1        pk 	(SC)->nwait++; \
    118       1.1        pk }
    119       1.1        pk 
    120       1.1        pk /*
    121       1.1        pk  * XDC_GET_WAITER: gets the first request waiting on the waitq
    122       1.1        pk  * and removes it (so it can be submitted)
    123       1.1        pk  */
    124       1.1        pk #define XDC_GET_WAITER(XDCSC, RQ) { \
    125       1.1        pk 	(RQ) = (XDCSC)->waitq[(XDCSC)->waithead]; \
    126       1.1        pk 	(XDCSC)->waithead = ((XDCSC)->waithead + 1) % XDC_MAXIOPB; \
    127       1.1        pk 	xdcsc->nwait--; \
    128       1.1        pk }
    129       1.1        pk 
    130       1.1        pk /*
    131       1.1        pk  * XDC_FREE: add iorq "N" to SC's free list
    132       1.1        pk  */
    133       1.1        pk #define XDC_FREE(SC, N) { \
    134       1.1        pk 	(SC)->freereq[(SC)->nfree++] = (N); \
    135       1.1        pk 	(SC)->reqs[N].mode = 0; \
    136       1.1        pk 	if ((SC)->nfree == 1) wakeup(&(SC)->nfree); \
    137       1.1        pk }
    138       1.1        pk 
    139       1.1        pk 
    140       1.1        pk /*
    141       1.1        pk  * XDC_RQALLOC: allocate an iorq off the free list (assume nfree > 0).
    142       1.1        pk  */
    143       1.1        pk #define XDC_RQALLOC(XDCSC) (XDCSC)->freereq[--((XDCSC)->nfree)]
    144       1.1        pk 
    145       1.1        pk /*
    146       1.1        pk  * XDC_GO: start iopb ADDR (DVMA addr in a u_long) on XDC
    147       1.1        pk  */
    148       1.1        pk #define XDC_GO(XDC, ADDR) { \
    149       1.1        pk 	(XDC)->xdc_iopbaddr0 = ((ADDR) & 0xff); \
    150       1.1        pk 	(ADDR) = ((ADDR) >> 8); \
    151       1.1        pk 	(XDC)->xdc_iopbaddr1 = ((ADDR) & 0xff); \
    152       1.1        pk 	(ADDR) = ((ADDR) >> 8); \
    153       1.1        pk 	(XDC)->xdc_iopbaddr2 = ((ADDR) & 0xff); \
    154       1.1        pk 	(ADDR) = ((ADDR) >> 8); \
    155       1.1        pk 	(XDC)->xdc_iopbaddr3 = (ADDR); \
    156       1.1        pk 	(XDC)->xdc_iopbamod = XDC_ADDRMOD; \
    157       1.1        pk 	(XDC)->xdc_csr = XDC_ADDIOPB; /* go! */ \
    158       1.1        pk }
    159       1.1        pk 
    160       1.1        pk /*
    161       1.1        pk  * XDC_WAIT: wait for XDC's csr "BITS" to come on in "TIME".
    162       1.1        pk  *   LCV is a counter.  If it goes to zero then we timed out.
    163       1.1        pk  */
    164       1.1        pk #define XDC_WAIT(XDC, LCV, TIME, BITS) { \
    165       1.1        pk 	(LCV) = (TIME); \
    166       1.1        pk 	while ((LCV) > 0) { \
    167       1.1        pk 		if ((XDC)->xdc_csr & (BITS)) break; \
    168       1.1        pk 		(LCV) = (LCV) - 1; \
    169       1.1        pk 		DELAY(1); \
    170       1.1        pk 	} \
    171       1.1        pk }
    172       1.1        pk 
    173       1.1        pk /*
    174       1.1        pk  * XDC_DONE: don't need IORQ, get error code and free (done after xdc_cmd)
    175       1.1        pk  */
    176       1.1        pk #define XDC_DONE(SC,RQ,ER) { \
    177       1.1        pk 	if ((RQ) == XD_ERR_FAIL) { \
    178       1.1        pk 		(ER) = (RQ); \
    179       1.1        pk 	} else { \
    180       1.1        pk 		if ((SC)->ndone-- == XDC_SUBWAITLIM) \
    181       1.1        pk 		wakeup(&(SC)->ndone); \
    182       1.1        pk 		(ER) = (SC)->reqs[RQ].errno; \
    183       1.1        pk 		XDC_FREE((SC), (RQ)); \
    184       1.1        pk 	} \
    185       1.1        pk }
    186       1.1        pk 
    187       1.1        pk /*
    188       1.1        pk  * XDC_ADVANCE: advance iorq's pointers by a number of sectors
    189       1.1        pk  */
    190       1.1        pk #define XDC_ADVANCE(IORQ, N) { \
    191       1.1        pk 	if (N) { \
    192       1.1        pk 		(IORQ)->sectcnt -= (N); \
    193       1.1        pk 		(IORQ)->blockno += (N); \
    194       1.1        pk 		(IORQ)->dbuf += ((N)*XDFM_BPS); \
    195       1.1        pk 	} \
    196       1.1        pk }
    197       1.1        pk 
    198       1.1        pk /*
    199       1.1        pk  * note - addresses you can sleep on:
    200       1.1        pk  *   [1] & of xd_softc's "state" (waiting for a chance to attach a drive)
    201       1.1        pk  *   [2] & of xdc_softc's "nfree" (waiting for a free iorq/iopb)
    202       1.1        pk  *   [3] & of xdc_softc's "ndone" (waiting for number of done iorq/iopb's
    203       1.1        pk  *                                 to drop below XDC_SUBWAITLIM)
    204       1.1        pk  *   [4] & an iorq (waiting for an XD_SUB_WAIT iorq to finish)
    205       1.1        pk  */
    206       1.1        pk 
    207       1.1        pk 
    208       1.1        pk /*
    209       1.1        pk  * function prototypes
    210       1.1        pk  * "xdc_*" functions are internal, all others are external interfaces
    211       1.1        pk  */
    212       1.1        pk 
    213       1.1        pk extern int pil_to_vme[];	/* from obio.c */
    214       1.1        pk 
    215       1.1        pk /* internals */
    216      1.56     perry int	xdc_cmd(struct xdc_softc *, int, int, int, int, int, char *, int);
    217      1.57   tsutsui const char *xdc_e2str(int);
    218      1.56     perry int	xdc_error(struct xdc_softc *, struct xd_iorq *,
    219      1.56     perry 		   struct xd_iopb *, int, int);
    220      1.56     perry int	xdc_ioctlcmd(struct xd_softc *, dev_t dev, struct xd_iocmd *);
    221      1.56     perry void	xdc_perror(struct xd_iorq *, struct xd_iopb *, int);
    222      1.56     perry int	xdc_piodriver(struct xdc_softc *, int, int);
    223      1.56     perry int	xdc_remove_iorq(struct xdc_softc *);
    224      1.56     perry int	xdc_reset(struct xdc_softc *, int, int, int, struct xd_softc *);
    225      1.56     perry inline void xdc_rqinit(struct xd_iorq *, struct xdc_softc *,
    226      1.56     perry 			struct xd_softc *, int, u_long, int,
    227      1.56     perry 			caddr_t, struct buf *);
    228      1.56     perry void	xdc_rqtopb(struct xd_iorq *, struct xd_iopb *, int, int);
    229      1.56     perry void	xdc_start(struct xdc_softc *, int);
    230      1.56     perry int	xdc_startbuf(struct xdc_softc *, struct xd_softc *, struct buf *);
    231      1.56     perry int	xdc_submit_iorq(struct xdc_softc *, int, int);
    232      1.56     perry void	xdc_tick(void *);
    233      1.56     perry void	xdc_xdreset(struct xdc_softc *, struct xd_softc *);
    234      1.21        pk int	xd_dmamem_alloc(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
    235      1.21        pk 			int *, bus_size_t, caddr_t *, bus_addr_t *);
    236      1.21        pk void	xd_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
    237      1.21        pk 			int, bus_size_t, caddr_t);
    238      1.21        pk 
    239       1.1        pk 
    240       1.1        pk /* machine interrupt hook */
    241      1.56     perry int	xdcintr(void *);
    242       1.1        pk 
    243       1.1        pk /* autoconf */
    244      1.56     perry int	xdcmatch(struct device *, struct cfdata *, void *);
    245      1.56     perry void	xdcattach(struct device *, struct device *, void *);
    246      1.56     perry int	xdmatch(struct device *, struct cfdata *, void *);
    247      1.56     perry void	xdattach(struct device *, struct device *, void *);
    248      1.56     perry static	int xdc_probe(void *, bus_space_tag_t, bus_space_handle_t);
    249       1.1        pk 
    250      1.56     perry static	void xddummystrat(struct buf *);
    251      1.56     perry int	xdgetdisklabel(struct xd_softc *, void *);
    252       1.1        pk 
    253       1.1        pk /* XXX - think about this more.. xd_machdep? */
    254      1.56     perry void xdc_md_setup(void);
    255       1.1        pk int	XDC_DELAY;
    256      1.18   thorpej 
    257      1.20       chs #if defined(__sparc__)
    258       1.1        pk #include <sparc/sparc/vaddrs.h>
    259       1.1        pk #include <sparc/sparc/cpuvar.h>
    260      1.18   thorpej void xdc_md_setup()
    261       1.1        pk {
    262       1.1        pk 	if (CPU_ISSUN4 && cpuinfo.cpu_type == CPUTYP_4_300)
    263       1.1        pk 		XDC_DELAY = XDC_DELAY_4_300;
    264       1.1        pk 	else
    265       1.1        pk 		XDC_DELAY = XDC_DELAY_SPARC;
    266       1.1        pk }
    267      1.27       chs #elif defined(sun3)
    268      1.18   thorpej void xdc_md_setup()
    269       1.1        pk {
    270       1.1        pk 	XDC_DELAY = XDC_DELAY_SUN3;
    271       1.1        pk }
    272      1.18   thorpej #else
    273      1.18   thorpej void xdc_md_setup()
    274      1.18   thorpej {
    275      1.18   thorpej 	XDC_DELAY = 0;
    276      1.18   thorpej }
    277       1.1        pk #endif
    278      1.18   thorpej 
    279       1.1        pk /*
    280       1.4   thorpej  * cfattach's: device driver interface to autoconfig
    281       1.1        pk  */
    282       1.1        pk 
    283      1.44   thorpej CFATTACH_DECL(xdc, sizeof(struct xdc_softc),
    284      1.45   thorpej     xdcmatch, xdcattach, NULL, NULL);
    285       1.1        pk 
    286      1.44   thorpej CFATTACH_DECL(xd, sizeof(struct xd_softc),
    287      1.45   thorpej     xdmatch, xdattach, NULL, NULL);
    288       1.1        pk 
    289       1.4   thorpej extern struct cfdriver xd_cd;
    290      1.42   gehenna 
    291      1.42   gehenna dev_type_open(xdopen);
    292      1.42   gehenna dev_type_close(xdclose);
    293      1.42   gehenna dev_type_read(xdread);
    294      1.42   gehenna dev_type_write(xdwrite);
    295      1.42   gehenna dev_type_ioctl(xdioctl);
    296      1.42   gehenna dev_type_strategy(xdstrategy);
    297      1.42   gehenna dev_type_dump(xddump);
    298      1.42   gehenna dev_type_size(xdsize);
    299      1.42   gehenna 
    300      1.42   gehenna const struct bdevsw xd_bdevsw = {
    301      1.42   gehenna 	xdopen, xdclose, xdstrategy, xdioctl, xddump, xdsize, D_DISK
    302      1.42   gehenna };
    303      1.42   gehenna 
    304      1.42   gehenna const struct cdevsw xd_cdevsw = {
    305      1.42   gehenna 	xdopen, xdclose, xdread, xdwrite, xdioctl,
    306      1.46  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    307      1.42   gehenna };
    308       1.1        pk 
    309       1.1        pk struct xdc_attach_args {	/* this is the "aux" args to xdattach */
    310       1.1        pk 	int	driveno;	/* unit number */
    311       1.1        pk 	int	fullmode;	/* submit mode */
    312       1.1        pk 	int	booting;	/* are we booting or not? */
    313       1.1        pk };
    314       1.1        pk 
    315       1.1        pk /*
    316       1.1        pk  * dkdriver
    317       1.1        pk  */
    318       1.1        pk 
    319       1.1        pk struct dkdriver xddkdriver = {xdstrategy};
    320       1.1        pk 
    321       1.1        pk /*
    322       1.1        pk  * start: disk label fix code (XXX)
    323       1.1        pk  */
    324       1.1        pk 
    325       1.1        pk static void *xd_labeldata;
    326       1.1        pk 
    327       1.1        pk static void
    328       1.1        pk xddummystrat(bp)
    329       1.1        pk 	struct buf *bp;
    330       1.1        pk {
    331       1.1        pk 	if (bp->b_bcount != XDFM_BPS)
    332       1.1        pk 		panic("xddummystrat");
    333      1.24   thorpej 	bcopy(xd_labeldata, bp->b_data, XDFM_BPS);
    334       1.1        pk 	bp->b_flags |= B_DONE;
    335       1.1        pk 	bp->b_flags &= ~B_BUSY;
    336       1.1        pk }
    337       1.1        pk 
    338       1.1        pk int
    339       1.1        pk xdgetdisklabel(xd, b)
    340       1.1        pk 	struct xd_softc *xd;
    341       1.1        pk 	void *b;
    342       1.1        pk {
    343      1.48       dsl 	const char *err;
    344      1.27       chs #if defined(__sparc__) || defined(sun3)
    345       1.1        pk 	struct sun_disklabel *sdl;
    346      1.14        pk #endif
    347       1.1        pk 
    348       1.1        pk 	/* We already have the label data in `b'; setup for dummy strategy */
    349       1.1        pk 	xd_labeldata = b;
    350       1.1        pk 
    351       1.1        pk 	/* Required parameter for readdisklabel() */
    352       1.1        pk 	xd->sc_dk.dk_label->d_secsize = XDFM_BPS;
    353       1.1        pk 
    354  1.59.8.1      yamt 	err = readdisklabel(MAKEDISKDEV(0, device_unit(&xd->sc_dev), RAW_PART),
    355       1.1        pk 			    xddummystrat,
    356       1.1        pk 			    xd->sc_dk.dk_label, xd->sc_dk.dk_cpulabel);
    357       1.1        pk 	if (err) {
    358       1.1        pk 		printf("%s: %s\n", xd->sc_dev.dv_xname, err);
    359       1.1        pk 		return(XD_ERR_FAIL);
    360       1.1        pk 	}
    361       1.1        pk 
    362      1.27       chs #if defined(__sparc__) || defined(sun3)
    363       1.1        pk 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
    364       1.1        pk 	sdl = (struct sun_disklabel *)xd->sc_dk.dk_cpulabel->cd_block;
    365      1.14        pk 	if (sdl->sl_magic == SUN_DKMAGIC) {
    366       1.1        pk 		xd->pcyl = sdl->sl_pcylinders;
    367      1.14        pk 	} else
    368      1.17  drochner #endif
    369      1.14        pk 	{
    370       1.1        pk 		printf("%s: WARNING: no `pcyl' in disk label.\n",
    371       1.1        pk 							xd->sc_dev.dv_xname);
    372       1.1        pk 		xd->pcyl = xd->sc_dk.dk_label->d_ncylinders +
    373       1.1        pk 			xd->sc_dk.dk_label->d_acylinders;
    374       1.1        pk 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
    375       1.1        pk 			xd->sc_dev.dv_xname, xd->pcyl);
    376       1.1        pk 	}
    377       1.1        pk 
    378       1.1        pk 	xd->ncyl = xd->sc_dk.dk_label->d_ncylinders;
    379       1.1        pk 	xd->acyl = xd->sc_dk.dk_label->d_acylinders;
    380       1.1        pk 	xd->nhead = xd->sc_dk.dk_label->d_ntracks;
    381       1.1        pk 	xd->nsect = xd->sc_dk.dk_label->d_nsectors;
    382       1.1        pk 	xd->sectpercyl = xd->nhead * xd->nsect;
    383       1.1        pk 	xd->sc_dk.dk_label->d_secsize = XDFM_BPS; /* not handled by
    384       1.1        pk 						  * sun->bsd */
    385       1.1        pk 	return(XD_ERR_AOK);
    386       1.1        pk }
    387       1.1        pk 
    388       1.1        pk /*
    389       1.1        pk  * end: disk label fix code (XXX)
    390       1.1        pk  */
    391       1.1        pk 
    392       1.1        pk /*
    393      1.21        pk  * Shorthand for allocating, mapping and loading a DMA buffer
    394      1.21        pk  */
    395      1.21        pk int
    396      1.21        pk xd_dmamem_alloc(tag, map, seg, nsegp, len, kvap, dmap)
    397      1.21        pk 	bus_dma_tag_t		tag;
    398      1.21        pk 	bus_dmamap_t		map;
    399      1.21        pk 	bus_dma_segment_t	*seg;
    400      1.21        pk 	int			*nsegp;
    401      1.21        pk 	bus_size_t		len;
    402      1.21        pk 	caddr_t			*kvap;
    403      1.21        pk 	bus_addr_t		*dmap;
    404      1.21        pk {
    405      1.21        pk 	int nseg;
    406      1.21        pk 	int error;
    407      1.21        pk 
    408      1.22        pk 	if ((error = bus_dmamem_alloc(tag, len, 0, 0,
    409      1.21        pk 				      seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
    410      1.21        pk 		return (error);
    411      1.21        pk 	}
    412      1.21        pk 
    413      1.36   thorpej 	if ((error = bus_dmamem_map(tag, seg, nseg,
    414      1.36   thorpej 				    len, kvap,
    415      1.36   thorpej 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    416      1.21        pk 		bus_dmamem_free(tag, seg, nseg);
    417      1.21        pk 		return (error);
    418      1.21        pk 	}
    419      1.21        pk 
    420      1.36   thorpej 	if ((error = bus_dmamap_load(tag, map,
    421      1.36   thorpej 				     *kvap, len, NULL,
    422      1.36   thorpej 				     BUS_DMA_NOWAIT)) != 0) {
    423      1.36   thorpej 		bus_dmamem_unmap(tag, *kvap, len);
    424      1.21        pk 		bus_dmamem_free(tag, seg, nseg);
    425      1.21        pk 		return (error);
    426      1.21        pk 	}
    427      1.21        pk 
    428      1.21        pk 	*dmap = map->dm_segs[0].ds_addr;
    429      1.21        pk 	*nsegp = nseg;
    430      1.21        pk 	return (0);
    431      1.21        pk }
    432      1.21        pk 
    433      1.21        pk void
    434      1.21        pk xd_dmamem_free(tag, map, seg, nseg, len, kva)
    435      1.21        pk 	bus_dma_tag_t		tag;
    436      1.21        pk 	bus_dmamap_t		map;
    437      1.21        pk 	bus_dma_segment_t	*seg;
    438      1.21        pk 	int			nseg;
    439      1.21        pk 	bus_size_t		len;
    440      1.21        pk 	caddr_t			kva;
    441      1.21        pk {
    442      1.21        pk 
    443      1.21        pk 	bus_dmamap_unload(tag, map);
    444      1.21        pk 	bus_dmamem_unmap(tag, kva, len);
    445      1.21        pk 	bus_dmamem_free(tag, seg, nseg);
    446      1.21        pk }
    447      1.21        pk 
    448      1.21        pk 
    449      1.21        pk /*
    450       1.1        pk  * a u t o c o n f i g   f u n c t i o n s
    451       1.1        pk  */
    452       1.1        pk 
    453       1.1        pk /*
    454       1.1        pk  * xdcmatch: determine if xdc is present or not.   we do a
    455       1.1        pk  * soft reset to detect the xdc.
    456       1.1        pk  */
    457       1.1        pk 
    458       1.7        pk int
    459      1.15  drochner xdc_probe(arg, tag, handle)
    460       1.7        pk 	void *arg;
    461      1.15  drochner 	bus_space_tag_t tag;
    462      1.15  drochner 	bus_space_handle_t handle;
    463       1.7        pk {
    464      1.15  drochner 	struct xdc *xdc = (void *)handle; /* XXX */
    465       1.7        pk 	int del = 0;
    466       1.7        pk 
    467       1.7        pk 	xdc->xdc_csr = XDC_RESET;
    468       1.7        pk 	XDC_WAIT(xdc, del, XDC_RESETUSEC, XDC_RESET);
    469      1.15  drochner 	return (del > 0 ? 0 : EIO);
    470       1.7        pk }
    471       1.7        pk 
    472       1.1        pk int xdcmatch(parent, cf, aux)
    473       1.1        pk 	struct device *parent;
    474       1.1        pk 	struct cfdata *cf;
    475       1.1        pk 	void *aux;
    476       1.1        pk {
    477       1.1        pk 	struct vme_attach_args	*va = aux;
    478      1.15  drochner 	vme_chipset_tag_t	ct = va->va_vct;
    479      1.15  drochner 	vme_am_t		mod;
    480      1.15  drochner 	int error;
    481      1.15  drochner 
    482      1.30        pk 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
    483      1.30        pk 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xdc), mod))
    484      1.15  drochner 		return (0);
    485      1.30        pk 
    486      1.15  drochner 	error = vme_probe(ct, va->r[0].offset, sizeof(struct xdc),
    487      1.15  drochner 			  mod, VME_D32, xdc_probe, 0);
    488      1.15  drochner 	vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xdc), mod);
    489      1.15  drochner 
    490      1.15  drochner 	return (error == 0);
    491       1.1        pk }
    492       1.1        pk 
    493       1.1        pk /*
    494       1.1        pk  * xdcattach: attach controller
    495       1.1        pk  */
    496       1.1        pk void
    497       1.1        pk xdcattach(parent, self, aux)
    498       1.1        pk 	struct device *parent, *self;
    499       1.1        pk 	void   *aux;
    500       1.1        pk 
    501       1.1        pk {
    502       1.1        pk 	struct vme_attach_args	*va = aux;
    503      1.15  drochner 	vme_chipset_tag_t	ct = va->va_vct;
    504      1.15  drochner 	bus_space_tag_t		bt;
    505       1.1        pk 	bus_space_handle_t	bh;
    506       1.1        pk 	vme_intr_handle_t	ih;
    507      1.15  drochner 	vme_am_t		mod;
    508       1.1        pk 	struct xdc_softc	*xdc = (void *) self;
    509       1.1        pk 	struct xdc_attach_args	xa;
    510       1.3        pk 	int			lcv, rqno, error;
    511       1.1        pk 	struct xd_iopb_ctrl	*ctl;
    512       1.3        pk 	bus_dma_segment_t	seg;
    513       1.3        pk 	int			rseg;
    514      1.15  drochner 	vme_mapresc_t resc;
    515      1.52       mrg 	bus_addr_t		busaddr;
    516       1.1        pk 
    517      1.18   thorpej 	xdc_md_setup();
    518       1.1        pk 
    519       1.1        pk 	/* get addressing and intr level stuff from autoconfig and load it
    520       1.1        pk 	 * into our xdc_softc. */
    521       1.1        pk 
    522      1.15  drochner 	xdc->dmatag = va->va_bdt;
    523      1.30        pk 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
    524       1.1        pk 
    525      1.30        pk 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xdc), mod))
    526      1.15  drochner 		panic("xdc: vme alloc");
    527      1.30        pk 
    528      1.15  drochner 	if (vme_space_map(ct, va->r[0].offset, sizeof(struct xdc),
    529      1.15  drochner 			  mod, VME_D32, 0, &bt, &bh, &resc) != 0)
    530       1.2        pk 		panic("xdc: vme_map");
    531       1.1        pk 
    532      1.15  drochner 	xdc->xdc = (struct xdc *) bh; /* XXX */
    533      1.15  drochner 	xdc->ipl = va->ilevel;
    534      1.15  drochner 	xdc->vector = va->ivector;
    535       1.1        pk 
    536       1.1        pk 	for (lcv = 0; lcv < XDC_MAXDEV; lcv++)
    537       1.1        pk 		xdc->sc_drives[lcv] = (struct xd_softc *) 0;
    538       1.1        pk 
    539      1.21        pk 	/*
    540      1.21        pk 	 * allocate and zero buffers
    541       1.1        pk 	 *
    542       1.1        pk 	 * note: we simplify the code by allocating the max number of iopbs and
    543       1.1        pk 	 * iorq's up front.   thus, we avoid linked lists and the costs
    544      1.21        pk 	 * associated with them in exchange for wasting a little memory.
    545      1.21        pk 	 */
    546       1.1        pk 
    547      1.21        pk 	/* Get DMA handle for misc. transfers */
    548      1.30        pk 	if ((error = vme_dmamap_create(
    549      1.30        pk 				ct,		/* VME chip tag */
    550      1.30        pk 				MAXPHYS,	/* size */
    551      1.30        pk 				VME_AM_A24,	/* address modifier */
    552      1.30        pk 				VME_D32,	/* data size */
    553      1.30        pk 				0,		/* swap */
    554      1.21        pk 				1,		/* nsegments */
    555      1.30        pk 				MAXPHYS,	/* maxsegsz */
    556      1.21        pk 				0,		/* boundary */
    557      1.21        pk 				BUS_DMA_NOWAIT,
    558      1.21        pk 				&xdc->auxmap)) != 0) {
    559      1.30        pk 
    560      1.21        pk 		printf("%s: DMA buffer map create error %d\n",
    561       1.3        pk 			xdc->sc_dev.dv_xname, error);
    562       1.3        pk 		return;
    563       1.3        pk 	}
    564       1.3        pk 
    565      1.30        pk 
    566      1.21        pk 	/* Get DMA handle for mapping iorq descriptors */
    567      1.30        pk 	if ((error = vme_dmamap_create(
    568      1.30        pk 				ct,		/* VME chip tag */
    569      1.21        pk 				XDC_MAXIOPB * sizeof(struct xd_iopb),
    570      1.30        pk 				VME_AM_A24,	/* address modifier */
    571      1.30        pk 				VME_D32,	/* data size */
    572      1.30        pk 				0,		/* swap */
    573      1.21        pk 				1,		/* nsegments */
    574       1.3        pk 				XDC_MAXIOPB * sizeof(struct xd_iopb),
    575      1.21        pk 				0,		/* boundary */
    576      1.21        pk 				BUS_DMA_NOWAIT,
    577      1.21        pk 				&xdc->iopmap)) != 0) {
    578      1.30        pk 
    579      1.21        pk 		printf("%s: DMA buffer map create error %d\n",
    580      1.21        pk 			xdc->sc_dev.dv_xname, error);
    581      1.21        pk 		return;
    582      1.21        pk 	}
    583      1.21        pk 
    584      1.21        pk 	/* Get DMA buffer for iorq descriptors */
    585      1.21        pk 	if ((error = xd_dmamem_alloc(xdc->dmatag, xdc->iopmap, &seg, &rseg,
    586      1.21        pk 				     XDC_MAXIOPB * sizeof(struct xd_iopb),
    587      1.21        pk 				     (caddr_t *)&xdc->iopbase,
    588      1.52       mrg 				     &busaddr)) != 0) {
    589      1.21        pk 		printf("%s: DMA buffer alloc error %d\n",
    590       1.3        pk 			xdc->sc_dev.dv_xname, error);
    591       1.3        pk 		return;
    592       1.3        pk 	}
    593      1.53       mrg 	xdc->dvmaiopb = (struct xd_iopb *)(u_long)BUS_ADDR_PADDR(busaddr);
    594      1.21        pk 
    595       1.1        pk 	bzero(xdc->iopbase, XDC_MAXIOPB * sizeof(struct xd_iopb));
    596       1.1        pk 
    597       1.1        pk 	xdc->reqs = (struct xd_iorq *)
    598      1.40   tsutsui 	    malloc(XDC_MAXIOPB * sizeof(struct xd_iorq),
    599      1.40   tsutsui 	    M_DEVBUF, M_NOWAIT|M_ZERO);
    600       1.1        pk 	if (xdc->reqs == NULL)
    601       1.1        pk 		panic("xdc malloc");
    602       1.1        pk 
    603       1.1        pk 	/* init free list, iorq to iopb pointers, and non-zero fields in the
    604       1.1        pk 	 * iopb which never change. */
    605       1.1        pk 
    606       1.1        pk 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
    607       1.1        pk 		xdc->reqs[lcv].iopb = &xdc->iopbase[lcv];
    608      1.12        pk 		xdc->reqs[lcv].dmaiopb = &xdc->dvmaiopb[lcv];
    609       1.1        pk 		xdc->freereq[lcv] = lcv;
    610       1.1        pk 		xdc->iopbase[lcv].fixd = 1;	/* always the same */
    611       1.1        pk 		xdc->iopbase[lcv].naddrmod = XDC_ADDRMOD; /* always the same */
    612       1.1        pk 		xdc->iopbase[lcv].intr_vec = xdc->vector; /* always the same */
    613       1.3        pk 
    614      1.30        pk 		if ((error = vme_dmamap_create(
    615      1.30        pk 				ct,		/* VME chip tag */
    616       1.3        pk 				MAXPHYS,	/* size */
    617      1.30        pk 				VME_AM_A24,	/* address modifier */
    618      1.30        pk 				VME_D32,	/* data size */
    619      1.30        pk 				0,		/* swap */
    620       1.3        pk 				1,		/* nsegments */
    621       1.3        pk 				MAXPHYS,	/* maxsegsz */
    622       1.3        pk 				0,		/* boundary */
    623       1.3        pk 				BUS_DMA_NOWAIT,
    624      1.30        pk 				&xdc->reqs[lcv].dmamap)) != 0) {
    625      1.30        pk 
    626       1.3        pk 			printf("%s: DMA buffer map create error %d\n",
    627       1.3        pk 				xdc->sc_dev.dv_xname, error);
    628       1.3        pk 			return;
    629       1.3        pk 		}
    630       1.1        pk 	}
    631       1.1        pk 	xdc->nfree = XDC_MAXIOPB;
    632       1.1        pk 	xdc->nrun = 0;
    633       1.1        pk 	xdc->waithead = xdc->waitend = xdc->nwait = 0;
    634       1.1        pk 	xdc->ndone = 0;
    635       1.1        pk 
    636       1.1        pk 	/* init queue of waiting bufs */
    637       1.1        pk 
    638      1.58      yamt 	bufq_alloc(&xdc->sc_wq, "fcfs", 0);
    639      1.19   thorpej 	callout_init(&xdc->sc_tick_ch);
    640       1.1        pk 
    641       1.1        pk 	/*
    642       1.1        pk 	 * section 7 of the manual tells us how to init the controller:
    643       1.1        pk 	 * - read controller parameters (6/0)
    644       1.1        pk 	 * - write controller parameters (5/0)
    645       1.1        pk 	 */
    646       1.1        pk 
    647       1.1        pk 	/* read controller parameters and insure we have a 753/7053 */
    648       1.1        pk 
    649       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
    650       1.1        pk 	if (rqno == XD_ERR_FAIL) {
    651       1.1        pk 		printf(": couldn't read controller params\n");
    652       1.1        pk 		return;		/* shouldn't ever happen */
    653       1.1        pk 	}
    654      1.21        pk 	ctl = (struct xd_iopb_ctrl *) &xdc->iopbase[rqno];
    655       1.1        pk 	if (ctl->ctype != XDCT_753) {
    656       1.1        pk 		if (xdc->reqs[rqno].errno)
    657       1.1        pk 			printf(": %s: ", xdc_e2str(xdc->reqs[rqno].errno));
    658       1.1        pk 		printf(": doesn't identify as a 753/7053\n");
    659       1.3        pk 		XDC_DONE(xdc, rqno, error);
    660       1.1        pk 		return;
    661       1.1        pk 	}
    662       1.1        pk 	printf(": Xylogics 753/7053, PROM=0x%x.%02x.%02x\n",
    663       1.1        pk 	    ctl->eprom_partno, ctl->eprom_lvl, ctl->eprom_rev);
    664       1.3        pk 	XDC_DONE(xdc, rqno, error);
    665       1.1        pk 
    666       1.1        pk 	/* now write controller parameters (xdc_cmd sets all params for us) */
    667       1.1        pk 
    668       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
    669       1.3        pk 	XDC_DONE(xdc, rqno, error);
    670       1.3        pk 	if (error) {
    671       1.1        pk 		printf("%s: controller config error: %s\n",
    672       1.3        pk 			xdc->sc_dev.dv_xname, xdc_e2str(error));
    673       1.1        pk 		return;
    674       1.1        pk 	}
    675       1.1        pk 
    676       1.1        pk 	/* link in interrupt with higher level software */
    677      1.32       scw 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
    678      1.15  drochner 	vme_intr_establish(ct, ih, IPL_BIO, xdcintr, xdc);
    679      1.26       cgd 	evcnt_attach_dynamic(&xdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    680      1.26       cgd 	    xdc->sc_dev.dv_xname, "intr");
    681       1.1        pk 
    682       1.1        pk 
    683       1.1        pk 	/* now we must look for disks using autoconfig */
    684       1.1        pk 	xa.fullmode = XD_SUB_POLL;
    685       1.1        pk 	xa.booting = 1;
    686       1.1        pk 
    687       1.1        pk 	for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
    688       1.1        pk 		(void) config_found(self, (void *) &xa, NULL);
    689       1.1        pk 
    690       1.1        pk 	/* start the watchdog clock */
    691      1.19   thorpej 	callout_reset(&xdc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdc);
    692       1.1        pk 
    693       1.1        pk }
    694       1.1        pk 
    695       1.1        pk /*
    696       1.1        pk  * xdmatch: probe for disk.
    697       1.1        pk  *
    698       1.1        pk  * note: we almost always say disk is present.   this allows us to
    699       1.1        pk  * spin up and configure a disk after the system is booted (we can
    700       1.1        pk  * call xdattach!).
    701       1.1        pk  */
    702       1.1        pk int
    703       1.1        pk xdmatch(parent, cf, aux)
    704       1.1        pk 	struct device *parent;
    705       1.1        pk 	struct cfdata *cf;
    706       1.1        pk 	void *aux;
    707       1.1        pk {
    708       1.1        pk 	struct xdc_attach_args *xa = aux;
    709       1.1        pk 
    710       1.1        pk 	/* looking for autoconf wildcard or exact match */
    711       1.1        pk 
    712       1.1        pk 	if (cf->cf_loc[XDCCF_DRIVE] != XDCCF_DRIVE_DEFAULT &&
    713       1.1        pk 	    cf->cf_loc[XDCCF_DRIVE] != xa->driveno)
    714       1.1        pk 		return 0;
    715       1.1        pk 
    716       1.1        pk 	return 1;
    717       1.1        pk 
    718       1.1        pk }
    719       1.1        pk 
    720       1.1        pk /*
    721       1.1        pk  * xdattach: attach a disk.   this can be called from autoconf and also
    722       1.1        pk  * from xdopen/xdstrategy.
    723       1.1        pk  */
    724       1.1        pk void
    725       1.1        pk xdattach(parent, self, aux)
    726       1.1        pk 	struct device *parent, *self;
    727       1.1        pk 	void   *aux;
    728       1.1        pk 
    729       1.1        pk {
    730       1.1        pk 	struct xd_softc *xd = (void *) self;
    731       1.1        pk 	struct xdc_softc *xdc = (void *) parent;
    732       1.1        pk 	struct xdc_attach_args *xa = aux;
    733       1.3        pk 	int     rqno, spt = 0, mb, blk, lcv, fmode, s = 0, newstate;
    734       1.1        pk 	struct xd_iopb_drive *driopb;
    735       1.1        pk 	struct dkbad *dkb;
    736       1.3        pk 	int			rseg, error;
    737       1.3        pk 	bus_dma_segment_t	seg;
    738      1.52       mrg 	bus_addr_t		busaddr;
    739       1.3        pk 	caddr_t			dmaddr;
    740       1.3        pk 	caddr_t			buf;
    741       1.1        pk 
    742       1.1        pk 	/*
    743       1.1        pk 	 * Always re-initialize the disk structure.  We want statistics
    744       1.1        pk 	 * to start with a clean slate.
    745       1.1        pk 	 */
    746       1.1        pk 	bzero(&xd->sc_dk, sizeof(xd->sc_dk));
    747       1.1        pk 	xd->sc_dk.dk_driver = &xddkdriver;
    748       1.1        pk 	xd->sc_dk.dk_name = xd->sc_dev.dv_xname;
    749       1.1        pk 
    750       1.1        pk 	/* if booting, init the xd_softc */
    751       1.1        pk 
    752       1.1        pk 	if (xa->booting) {
    753       1.1        pk 		xd->state = XD_DRIVE_UNKNOWN;	/* to start */
    754       1.1        pk 		xd->flags = 0;
    755       1.1        pk 		xd->parent = xdc;
    756       1.1        pk 	}
    757       1.1        pk 	xd->xd_drive = xa->driveno;
    758       1.1        pk 	fmode = xa->fullmode;
    759       1.1        pk 	xdc->sc_drives[xa->driveno] = xd;
    760       1.1        pk 
    761       1.1        pk 	/* if not booting, make sure we are the only process in the attach for
    762       1.1        pk 	 * this drive.   if locked out, sleep on it. */
    763       1.1        pk 
    764       1.1        pk 	if (!xa->booting) {
    765       1.1        pk 		s = splbio();
    766       1.1        pk 		while (xd->state == XD_DRIVE_ATTACHING) {
    767       1.1        pk 			if (tsleep(&xd->state, PRIBIO, "xdattach", 0)) {
    768       1.1        pk 				splx(s);
    769       1.1        pk 				return;
    770       1.1        pk 			}
    771       1.1        pk 		}
    772       1.1        pk 		printf("%s at %s",
    773       1.1        pk 			xd->sc_dev.dv_xname, xd->parent->sc_dev.dv_xname);
    774       1.1        pk 	}
    775       1.3        pk 
    776       1.1        pk 	/* we now have control */
    777       1.1        pk 	xd->state = XD_DRIVE_ATTACHING;
    778       1.1        pk 	newstate = XD_DRIVE_UNKNOWN;
    779       1.1        pk 
    780       1.3        pk 	buf = NULL;
    781      1.21        pk 	if ((error = xd_dmamem_alloc(xdc->dmatag, xdc->auxmap, &seg, &rseg,
    782      1.21        pk 				     XDFM_BPS,
    783      1.21        pk 				     (caddr_t *)&buf,
    784      1.52       mrg 				     &busaddr)) != 0) {
    785       1.3        pk 		printf("%s: DMA buffer alloc error %d\n",
    786      1.21        pk 			xdc->sc_dev.dv_xname, error);
    787      1.21        pk 		return;
    788       1.3        pk 	}
    789      1.53       mrg 	dmaddr = (caddr_t)(u_long)BUS_ADDR_PADDR(busaddr);
    790       1.3        pk 
    791       1.1        pk 	/* first try and reset the drive */
    792       1.1        pk 
    793       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_RST, 0, xd->xd_drive, 0, 0, 0, fmode);
    794       1.3        pk 	XDC_DONE(xdc, rqno, error);
    795       1.3        pk 	if (error == XD_ERR_NRDY) {
    796       1.1        pk 		printf(" drive %d: off-line\n", xa->driveno);
    797       1.1        pk 		goto done;
    798       1.1        pk 	}
    799       1.3        pk 	if (error) {
    800       1.3        pk 		printf(": ERROR 0x%02x (%s)\n", error, xdc_e2str(error));
    801       1.1        pk 		goto done;
    802       1.1        pk 	}
    803       1.1        pk 	printf(" drive %d: ready\n", xa->driveno);
    804       1.1        pk 
    805       1.1        pk 	/* now set format parameters */
    806       1.1        pk 
    807       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_FMT, xd->xd_drive, 0, 0, 0, fmode);
    808       1.3        pk 	XDC_DONE(xdc, rqno, error);
    809       1.3        pk 	if (error) {
    810       1.1        pk 		printf("%s: write format parameters failed: %s\n",
    811       1.3        pk 			xd->sc_dev.dv_xname, xdc_e2str(error));
    812       1.1        pk 		goto done;
    813       1.1        pk 	}
    814       1.1        pk 
    815       1.1        pk 	/* get drive parameters */
    816       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    817       1.1        pk 	if (rqno != XD_ERR_FAIL) {
    818      1.21        pk 		driopb = (struct xd_iopb_drive *) &xdc->iopbase[rqno];
    819       1.1        pk 		spt = driopb->sectpertrk;
    820       1.1        pk 	}
    821       1.3        pk 	XDC_DONE(xdc, rqno, error);
    822       1.3        pk 	if (error) {
    823       1.1        pk 		printf("%s: read drive parameters failed: %s\n",
    824       1.3        pk 			xd->sc_dev.dv_xname, xdc_e2str(error));
    825       1.1        pk 		goto done;
    826       1.1        pk 	}
    827       1.1        pk 
    828       1.1        pk 	/*
    829       1.1        pk 	 * now set drive parameters (to semi-bogus values) so we can read the
    830       1.1        pk 	 * disk label.
    831       1.1        pk 	 */
    832       1.1        pk 	xd->pcyl = xd->ncyl = 1;
    833       1.1        pk 	xd->acyl = 0;
    834       1.1        pk 	xd->nhead = 1;
    835       1.1        pk 	xd->nsect = 1;
    836       1.1        pk 	xd->sectpercyl = 1;
    837       1.1        pk 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
    838       1.1        pk 		xd->dkb.bt_bad[lcv].bt_cyl = xd->dkb.bt_bad[lcv].bt_trksec = 0xffff;
    839       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    840       1.3        pk 	XDC_DONE(xdc, rqno, error);
    841       1.3        pk 	if (error) {
    842       1.1        pk 		printf("%s: write drive parameters failed: %s\n",
    843       1.3        pk 			xd->sc_dev.dv_xname, xdc_e2str(error));
    844       1.1        pk 		goto done;
    845       1.1        pk 	}
    846       1.1        pk 
    847       1.1        pk 	/* read disk label */
    848       1.3        pk 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, 0, 1, dmaddr, fmode);
    849       1.3        pk 	XDC_DONE(xdc, rqno, error);
    850       1.3        pk 	if (error) {
    851       1.1        pk 		printf("%s: reading disk label failed: %s\n",
    852       1.3        pk 			xd->sc_dev.dv_xname, xdc_e2str(error));
    853       1.1        pk 		goto done;
    854       1.1        pk 	}
    855       1.1        pk 	newstate = XD_DRIVE_NOLABEL;
    856       1.1        pk 
    857       1.1        pk 	xd->hw_spt = spt;
    858       1.1        pk 	/* Attach the disk: must be before getdisklabel to malloc label */
    859       1.1        pk 	disk_attach(&xd->sc_dk);
    860       1.1        pk 
    861       1.3        pk 	if (xdgetdisklabel(xd, buf) != XD_ERR_AOK)
    862       1.1        pk 		goto done;
    863       1.1        pk 
    864       1.1        pk 	/* inform the user of what is up */
    865       1.1        pk 	printf("%s: <%s>, pcyl %d, hw_spt %d\n", xd->sc_dev.dv_xname,
    866       1.3        pk 		buf, xd->pcyl, spt);
    867       1.1        pk 	mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
    868       1.1        pk 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    869       1.1        pk 		xd->sc_dev.dv_xname, mb, xd->ncyl, xd->nhead, xd->nsect,
    870       1.1        pk 		XDFM_BPS);
    871       1.1        pk 
    872       1.1        pk 	/* now set the real drive parameters! */
    873       1.1        pk 
    874       1.1        pk 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    875       1.3        pk 	XDC_DONE(xdc, rqno, error);
    876       1.3        pk 	if (error) {
    877       1.1        pk 		printf("%s: write real drive parameters failed: %s\n",
    878       1.3        pk 			xd->sc_dev.dv_xname, xdc_e2str(error));
    879       1.1        pk 		goto done;
    880       1.1        pk 	}
    881       1.1        pk 	newstate = XD_DRIVE_ONLINE;
    882       1.1        pk 
    883       1.1        pk 	/*
    884       1.1        pk 	 * read bad144 table. this table resides on the first sector of the
    885       1.1        pk 	 * last track of the disk (i.e. second cyl of "acyl" area).
    886       1.1        pk 	 */
    887       1.1        pk 
    888       1.1        pk 	blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
    889       1.1        pk 	    (xd->nhead - 1) * xd->nsect;	/* last head */
    890       1.3        pk 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, blk, 1, dmaddr, fmode);
    891       1.3        pk 	XDC_DONE(xdc, rqno, error);
    892       1.3        pk 	if (error) {
    893       1.1        pk 		printf("%s: reading bad144 failed: %s\n",
    894       1.3        pk 			xd->sc_dev.dv_xname, xdc_e2str(error));
    895       1.1        pk 		goto done;
    896       1.1        pk 	}
    897       1.1        pk 
    898       1.1        pk 	/* check dkbad for sanity */
    899       1.3        pk 	dkb = (struct dkbad *) buf;
    900       1.1        pk 	for (lcv = 0; lcv < 126; lcv++) {
    901       1.1        pk 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
    902       1.1        pk 				dkb->bt_bad[lcv].bt_cyl == 0) &&
    903       1.1        pk 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
    904       1.1        pk 			continue;	/* blank */
    905       1.1        pk 		if (dkb->bt_bad[lcv].bt_cyl >= xd->ncyl)
    906       1.1        pk 			break;
    907       1.1        pk 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xd->nhead)
    908       1.1        pk 			break;
    909       1.1        pk 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xd->nsect)
    910       1.1        pk 			break;
    911       1.1        pk 	}
    912       1.1        pk 	if (lcv != 126) {
    913       1.1        pk 		printf("%s: warning: invalid bad144 sector!\n",
    914       1.1        pk 			xd->sc_dev.dv_xname);
    915       1.1        pk 	} else {
    916       1.3        pk 		bcopy(buf, &xd->dkb, XDFM_BPS);
    917       1.1        pk 	}
    918       1.1        pk 
    919       1.1        pk done:
    920       1.3        pk 	if (buf != NULL) {
    921      1.21        pk 		xd_dmamem_free(xdc->dmatag, xdc->auxmap,
    922      1.21        pk 				&seg, rseg, XDFM_BPS, buf);
    923       1.3        pk 	}
    924       1.3        pk 
    925       1.1        pk 	xd->state = newstate;
    926       1.1        pk 	if (!xa->booting) {
    927       1.1        pk 		wakeup(&xd->state);
    928       1.1        pk 		splx(s);
    929       1.1        pk 	}
    930       1.1        pk }
    931       1.1        pk 
    932       1.1        pk /*
    933       1.1        pk  * end of autoconfig functions
    934       1.1        pk  */
    935       1.1        pk 
    936       1.1        pk /*
    937       1.1        pk  * { b , c } d e v s w   f u n c t i o n s
    938       1.1        pk  */
    939       1.1        pk 
    940       1.1        pk /*
    941       1.1        pk  * xdclose: close device
    942       1.1        pk  */
    943       1.1        pk int
    944      1.59  christos xdclose(dev, flag, fmt, l)
    945       1.1        pk 	dev_t   dev;
    946       1.1        pk 	int     flag, fmt;
    947      1.59  christos 	struct lwp *l;
    948       1.1        pk {
    949       1.1        pk 	struct xd_softc *xd = xd_cd.cd_devs[DISKUNIT(dev)];
    950       1.1        pk 	int     part = DISKPART(dev);
    951       1.1        pk 
    952       1.1        pk 	/* clear mask bits */
    953       1.1        pk 
    954       1.1        pk 	switch (fmt) {
    955       1.1        pk 	case S_IFCHR:
    956       1.1        pk 		xd->sc_dk.dk_copenmask &= ~(1 << part);
    957       1.1        pk 		break;
    958       1.1        pk 	case S_IFBLK:
    959       1.1        pk 		xd->sc_dk.dk_bopenmask &= ~(1 << part);
    960       1.1        pk 		break;
    961       1.1        pk 	}
    962       1.1        pk 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
    963       1.1        pk 
    964       1.1        pk 	return 0;
    965       1.1        pk }
    966       1.1        pk 
    967       1.1        pk /*
    968       1.1        pk  * xddump: crash dump system
    969       1.1        pk  */
    970       1.1        pk int
    971       1.1        pk xddump(dev, blkno, va, size)
    972       1.1        pk 	dev_t dev;
    973       1.1        pk 	daddr_t blkno;
    974       1.1        pk 	caddr_t va;
    975       1.1        pk 	size_t size;
    976       1.1        pk {
    977       1.1        pk 	int     unit, part;
    978       1.1        pk 	struct xd_softc *xd;
    979       1.1        pk 
    980       1.1        pk 	unit = DISKUNIT(dev);
    981       1.1        pk 	if (unit >= xd_cd.cd_ndevs)
    982       1.1        pk 		return ENXIO;
    983       1.1        pk 	part = DISKPART(dev);
    984       1.1        pk 
    985       1.1        pk 	xd = xd_cd.cd_devs[unit];
    986       1.1        pk 
    987       1.1        pk 	printf("%s%c: crash dump not supported (yet)\n", xd->sc_dev.dv_xname,
    988       1.1        pk 	    'a' + part);
    989       1.1        pk 
    990       1.1        pk 	return ENXIO;
    991       1.1        pk 
    992       1.1        pk 	/* outline: globals: "dumplo" == sector number of partition to start
    993       1.1        pk 	 * dump at (convert to physical sector with partition table)
    994       1.1        pk 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
    995       1.1        pk 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
    996       1.1        pk 	 * physmem)
    997       1.1        pk 	 *
    998       1.1        pk 	 * dump a copy of physical memory to the dump device starting at sector
    999       1.1        pk 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
   1000       1.1        pk 	 * we go.   use polled I/O.
   1001       1.1        pk 	 *
   1002       1.1        pk 	 * XXX how to handle NON_CONTIG? */
   1003       1.1        pk 
   1004       1.1        pk }
   1005       1.1        pk 
   1006       1.1        pk /*
   1007       1.1        pk  * xdioctl: ioctls on XD drives.   based on ioctl's of other netbsd disks.
   1008       1.1        pk  */
   1009       1.1        pk int
   1010      1.59  christos xdioctl(dev, command, addr, flag, l)
   1011       1.1        pk 	dev_t   dev;
   1012       1.1        pk 	u_long  command;
   1013       1.1        pk 	caddr_t addr;
   1014       1.1        pk 	int     flag;
   1015      1.59  christos 	struct lwp *l;
   1016       1.1        pk 
   1017       1.1        pk {
   1018       1.1        pk 	struct xd_softc *xd;
   1019       1.1        pk 	struct xd_iocmd *xio;
   1020       1.1        pk 	int     error, s, unit;
   1021      1.33      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1022      1.35      fvdl 	struct disklabel newlabel;
   1023      1.33      fvdl #endif
   1024      1.35      fvdl 	struct disklabel *lp;
   1025       1.1        pk 
   1026       1.1        pk 	unit = DISKUNIT(dev);
   1027       1.1        pk 
   1028       1.1        pk 	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
   1029       1.1        pk 		return (ENXIO);
   1030       1.1        pk 
   1031       1.1        pk 	/* switch on ioctl type */
   1032       1.1        pk 
   1033       1.1        pk 	switch (command) {
   1034       1.1        pk 	case DIOCSBAD:		/* set bad144 info */
   1035       1.1        pk 		if ((flag & FWRITE) == 0)
   1036       1.1        pk 			return EBADF;
   1037       1.1        pk 		s = splbio();
   1038       1.1        pk 		bcopy(addr, &xd->dkb, sizeof(xd->dkb));
   1039       1.1        pk 		splx(s);
   1040       1.1        pk 		return 0;
   1041       1.1        pk 
   1042       1.1        pk 	case DIOCGDINFO:	/* get disk label */
   1043       1.1        pk 		bcopy(xd->sc_dk.dk_label, addr, sizeof(struct disklabel));
   1044       1.1        pk 		return 0;
   1045      1.33      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1046      1.33      fvdl 	case ODIOCGDINFO:
   1047      1.33      fvdl 		newlabel = *(xd->sc_dk.dk_label);
   1048      1.33      fvdl 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1049      1.34      fvdl 			return ENOTTY;
   1050      1.33      fvdl 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1051      1.33      fvdl 		return 0;
   1052      1.33      fvdl #endif
   1053       1.1        pk 
   1054       1.1        pk 	case DIOCGPART:	/* get partition info */
   1055       1.1        pk 		((struct partinfo *) addr)->disklab = xd->sc_dk.dk_label;
   1056       1.1        pk 		((struct partinfo *) addr)->part =
   1057       1.1        pk 		    &xd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
   1058       1.1        pk 		return 0;
   1059       1.1        pk 
   1060       1.1        pk 	case DIOCSDINFO:	/* set disk label */
   1061      1.33      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1062      1.33      fvdl 	case ODIOCSDINFO:
   1063      1.33      fvdl 		if (command == ODIOCSDINFO) {
   1064      1.33      fvdl 			memset(&newlabel, 0, sizeof newlabel);
   1065      1.33      fvdl 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1066      1.33      fvdl 			lp = &newlabel;
   1067      1.33      fvdl 		} else
   1068      1.33      fvdl #endif
   1069      1.33      fvdl 		lp = (struct disklabel *)addr;
   1070      1.33      fvdl 
   1071       1.1        pk 		if ((flag & FWRITE) == 0)
   1072       1.1        pk 			return EBADF;
   1073       1.1        pk 		error = setdisklabel(xd->sc_dk.dk_label,
   1074      1.33      fvdl 		    lp, /* xd->sc_dk.dk_openmask : */ 0,
   1075       1.1        pk 		    xd->sc_dk.dk_cpulabel);
   1076       1.1        pk 		if (error == 0) {
   1077       1.1        pk 			if (xd->state == XD_DRIVE_NOLABEL)
   1078       1.1        pk 				xd->state = XD_DRIVE_ONLINE;
   1079       1.1        pk 		}
   1080       1.1        pk 		return error;
   1081       1.1        pk 
   1082       1.1        pk 	case DIOCWLABEL:	/* change write status of disk label */
   1083       1.1        pk 		if ((flag & FWRITE) == 0)
   1084       1.1        pk 			return EBADF;
   1085       1.1        pk 		if (*(int *) addr)
   1086       1.1        pk 			xd->flags |= XD_WLABEL;
   1087       1.1        pk 		else
   1088       1.1        pk 			xd->flags &= ~XD_WLABEL;
   1089       1.1        pk 		return 0;
   1090       1.1        pk 
   1091       1.1        pk 	case DIOCWDINFO:	/* write disk label */
   1092      1.33      fvdl #ifdef __HAVE_OLD_DISKLABEL
   1093      1.33      fvdl 	case ODIOCWDINFO:
   1094      1.33      fvdl 		if (command == ODIOCWDINFO) {
   1095      1.33      fvdl 			memset(&newlabel, 0, sizeof newlabel);
   1096      1.33      fvdl 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1097      1.33      fvdl 			lp = &newlabel;
   1098      1.33      fvdl 		} else
   1099      1.33      fvdl #endif
   1100      1.33      fvdl 		lp = (struct disklabel *)addr;
   1101      1.33      fvdl 
   1102       1.1        pk 		if ((flag & FWRITE) == 0)
   1103       1.1        pk 			return EBADF;
   1104       1.1        pk 		error = setdisklabel(xd->sc_dk.dk_label,
   1105      1.33      fvdl 		    lp, /* xd->sc_dk.dk_openmask : */ 0,
   1106       1.1        pk 		    xd->sc_dk.dk_cpulabel);
   1107       1.1        pk 		if (error == 0) {
   1108       1.1        pk 			if (xd->state == XD_DRIVE_NOLABEL)
   1109       1.1        pk 				xd->state = XD_DRIVE_ONLINE;
   1110       1.1        pk 
   1111       1.1        pk 			/* Simulate opening partition 0 so write succeeds. */
   1112       1.1        pk 			xd->sc_dk.dk_openmask |= (1 << 0);
   1113      1.33      fvdl 			error = writedisklabel(MAKEDISKDEV(major(dev),
   1114      1.33      fvdl 			    DISKUNIT(dev), RAW_PART),
   1115       1.1        pk 			    xdstrategy, xd->sc_dk.dk_label,
   1116       1.1        pk 			    xd->sc_dk.dk_cpulabel);
   1117       1.1        pk 			xd->sc_dk.dk_openmask =
   1118       1.1        pk 			    xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
   1119       1.1        pk 		}
   1120       1.1        pk 		return error;
   1121       1.1        pk 
   1122       1.1        pk 	case DIOSXDCMD:
   1123       1.1        pk 		xio = (struct xd_iocmd *) addr;
   1124  1.59.8.2      yamt 		if ((error = kauth_authorize_generic(l->l_proc->p_cred,
   1125  1.59.8.2      yamt 		    KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag)) != 0)
   1126       1.1        pk 			return (error);
   1127       1.1        pk 		return (xdc_ioctlcmd(xd, dev, xio));
   1128       1.1        pk 
   1129       1.1        pk 	default:
   1130       1.1        pk 		return ENOTTY;
   1131       1.1        pk 	}
   1132       1.1        pk }
   1133       1.1        pk /*
   1134       1.1        pk  * xdopen: open drive
   1135       1.1        pk  */
   1136       1.1        pk 
   1137       1.1        pk int
   1138      1.59  christos xdopen(dev, flag, fmt, l)
   1139       1.1        pk 	dev_t   dev;
   1140       1.1        pk 	int     flag, fmt;
   1141      1.59  christos 	struct lwp *l;
   1142       1.1        pk {
   1143       1.1        pk 	int     unit, part;
   1144       1.1        pk 	struct xd_softc *xd;
   1145       1.1        pk 	struct xdc_attach_args xa;
   1146       1.1        pk 
   1147       1.1        pk 	/* first, could it be a valid target? */
   1148       1.1        pk 
   1149       1.1        pk 	unit = DISKUNIT(dev);
   1150       1.1        pk 	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
   1151       1.1        pk 		return (ENXIO);
   1152       1.1        pk 	part = DISKPART(dev);
   1153       1.1        pk 
   1154       1.1        pk 	/* do we need to attach the drive? */
   1155       1.1        pk 
   1156       1.1        pk 	if (xd->state == XD_DRIVE_UNKNOWN) {
   1157       1.1        pk 		xa.driveno = xd->xd_drive;
   1158       1.1        pk 		xa.fullmode = XD_SUB_WAIT;
   1159       1.1        pk 		xa.booting = 0;
   1160       1.1        pk 		xdattach((struct device *) xd->parent, (struct device *) xd, &xa);
   1161       1.1        pk 		if (xd->state == XD_DRIVE_UNKNOWN) {
   1162       1.1        pk 			return (EIO);
   1163       1.1        pk 		}
   1164       1.1        pk 	}
   1165       1.1        pk 	/* check for partition */
   1166       1.1        pk 
   1167       1.1        pk 	if (part != RAW_PART &&
   1168       1.1        pk 	    (part >= xd->sc_dk.dk_label->d_npartitions ||
   1169       1.1        pk 		xd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
   1170       1.1        pk 		return (ENXIO);
   1171       1.1        pk 	}
   1172       1.1        pk 	/* set open masks */
   1173       1.1        pk 
   1174       1.1        pk 	switch (fmt) {
   1175       1.1        pk 	case S_IFCHR:
   1176       1.1        pk 		xd->sc_dk.dk_copenmask |= (1 << part);
   1177       1.1        pk 		break;
   1178       1.1        pk 	case S_IFBLK:
   1179       1.1        pk 		xd->sc_dk.dk_bopenmask |= (1 << part);
   1180       1.1        pk 		break;
   1181       1.1        pk 	}
   1182       1.1        pk 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
   1183       1.1        pk 
   1184       1.1        pk 	return 0;
   1185       1.1        pk }
   1186       1.1        pk 
   1187       1.1        pk int
   1188       1.1        pk xdread(dev, uio, flags)
   1189       1.1        pk 	dev_t   dev;
   1190       1.1        pk 	struct uio *uio;
   1191       1.1        pk 	int flags;
   1192       1.1        pk {
   1193       1.1        pk 
   1194       1.1        pk 	return (physio(xdstrategy, NULL, dev, B_READ, minphys, uio));
   1195       1.1        pk }
   1196       1.1        pk 
   1197       1.1        pk int
   1198       1.1        pk xdwrite(dev, uio, flags)
   1199       1.1        pk 	dev_t   dev;
   1200       1.1        pk 	struct uio *uio;
   1201       1.1        pk 	int flags;
   1202       1.1        pk {
   1203       1.1        pk 
   1204       1.1        pk 	return (physio(xdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1205       1.1        pk }
   1206       1.1        pk 
   1207       1.1        pk 
   1208       1.1        pk /*
   1209       1.1        pk  * xdsize: return size of a partition for a dump
   1210       1.1        pk  */
   1211       1.1        pk 
   1212       1.1        pk int
   1213       1.1        pk xdsize(dev)
   1214       1.1        pk 	dev_t   dev;
   1215       1.1        pk 
   1216       1.1        pk {
   1217       1.1        pk 	struct xd_softc *xdsc;
   1218       1.1        pk 	int     unit, part, size, omask;
   1219       1.1        pk 
   1220       1.1        pk 	/* valid unit? */
   1221       1.1        pk 	unit = DISKUNIT(dev);
   1222       1.1        pk 	if (unit >= xd_cd.cd_ndevs || (xdsc = xd_cd.cd_devs[unit]) == NULL)
   1223       1.1        pk 		return (-1);
   1224       1.1        pk 
   1225       1.1        pk 	part = DISKPART(dev);
   1226       1.1        pk 	omask = xdsc->sc_dk.dk_openmask & (1 << part);
   1227       1.1        pk 
   1228       1.1        pk 	if (omask == 0 && xdopen(dev, 0, S_IFBLK, NULL) != 0)
   1229       1.1        pk 		return (-1);
   1230       1.1        pk 
   1231       1.1        pk 	/* do it */
   1232       1.1        pk 	if (xdsc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1233       1.1        pk 		size = -1;	/* only give valid size for swap partitions */
   1234       1.1        pk 	else
   1235       1.1        pk 		size = xdsc->sc_dk.dk_label->d_partitions[part].p_size *
   1236       1.1        pk 		    (xdsc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1237       1.1        pk 	if (omask == 0 && xdclose(dev, 0, S_IFBLK, NULL) != 0)
   1238       1.1        pk 		return (-1);
   1239       1.1        pk 	return (size);
   1240       1.1        pk }
   1241       1.1        pk /*
   1242       1.1        pk  * xdstrategy: buffering system interface to xd.
   1243       1.1        pk  */
   1244       1.1        pk 
   1245       1.1        pk void
   1246       1.1        pk xdstrategy(bp)
   1247       1.1        pk 	struct buf *bp;
   1248       1.1        pk 
   1249       1.1        pk {
   1250       1.1        pk 	struct xd_softc *xd;
   1251       1.1        pk 	struct xdc_softc *parent;
   1252       1.1        pk 	int     s, unit;
   1253       1.1        pk 	struct xdc_attach_args xa;
   1254       1.1        pk 
   1255       1.1        pk 	unit = DISKUNIT(bp->b_dev);
   1256       1.1        pk 
   1257       1.1        pk 	/* check for live device */
   1258       1.1        pk 
   1259       1.1        pk 	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == 0 ||
   1260       1.1        pk 	    bp->b_blkno < 0 ||
   1261       1.1        pk 	    (bp->b_bcount % xd->sc_dk.dk_label->d_secsize) != 0) {
   1262       1.1        pk 		bp->b_error = EINVAL;
   1263       1.1        pk 		goto bad;
   1264       1.1        pk 	}
   1265       1.1        pk 	/* do we need to attach the drive? */
   1266       1.1        pk 
   1267       1.1        pk 	if (xd->state == XD_DRIVE_UNKNOWN) {
   1268       1.1        pk 		xa.driveno = xd->xd_drive;
   1269       1.1        pk 		xa.fullmode = XD_SUB_WAIT;
   1270       1.1        pk 		xa.booting = 0;
   1271       1.1        pk 		xdattach((struct device *)xd->parent, (struct device *)xd, &xa);
   1272       1.1        pk 		if (xd->state == XD_DRIVE_UNKNOWN) {
   1273       1.1        pk 			bp->b_error = EIO;
   1274       1.1        pk 			goto bad;
   1275       1.1        pk 		}
   1276       1.1        pk 	}
   1277       1.1        pk 	if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
   1278       1.1        pk 		/* no I/O to unlabeled disks, unless raw partition */
   1279       1.1        pk 		bp->b_error = EIO;
   1280       1.1        pk 		goto bad;
   1281       1.1        pk 	}
   1282       1.1        pk 	/* short circuit zero length request */
   1283       1.1        pk 
   1284       1.1        pk 	if (bp->b_bcount == 0)
   1285       1.1        pk 		goto done;
   1286       1.1        pk 
   1287       1.1        pk 	/* check bounds with label (disksubr.c).  Determine the size of the
   1288       1.1        pk 	 * transfer, and make sure it is within the boundaries of the
   1289       1.1        pk 	 * partition. Adjust transfer if needed, and signal errors or early
   1290       1.1        pk 	 * completion. */
   1291       1.1        pk 
   1292      1.49   thorpej 	if (bounds_check_with_label(&xd->sc_dk, bp,
   1293       1.1        pk 		(xd->flags & XD_WLABEL) != 0) <= 0)
   1294       1.1        pk 		goto done;
   1295       1.1        pk 
   1296       1.1        pk 	/*
   1297       1.1        pk 	 * now we know we have a valid buf structure that we need to do I/O
   1298       1.1        pk 	 * on.
   1299       1.1        pk 	 *
   1300       1.1        pk 	 * note that we don't disksort because the controller has a sorting
   1301       1.1        pk 	 * algorithm built into the hardware.
   1302       1.1        pk 	 */
   1303       1.1        pk 
   1304       1.1        pk 	s = splbio();		/* protect the queues */
   1305       1.1        pk 
   1306       1.1        pk 	/* first, give jobs in front of us a chance */
   1307       1.1        pk 	parent = xd->parent;
   1308      1.58      yamt 	while (parent->nfree > 0 && BUFQ_PEEK(parent->sc_wq) != NULL)
   1309       1.1        pk 		if (xdc_startbuf(parent, NULL, NULL) != XD_ERR_AOK)
   1310       1.1        pk 			break;
   1311       1.1        pk 
   1312       1.1        pk 	/* if there are no free iorq's, then we just queue and return. the
   1313       1.1        pk 	 * buffs will get picked up later by xdcintr().
   1314       1.1        pk 	 */
   1315       1.1        pk 
   1316       1.1        pk 	if (parent->nfree == 0) {
   1317      1.58      yamt 		BUFQ_PUT(parent->sc_wq, bp);
   1318       1.1        pk 		splx(s);
   1319       1.1        pk 		return;
   1320       1.1        pk 	}
   1321       1.1        pk 
   1322       1.1        pk 	/* now we have free iopb's and we are at splbio... start 'em up */
   1323       1.1        pk 	if (xdc_startbuf(parent, xd, bp) != XD_ERR_AOK) {
   1324       1.1        pk 		return;
   1325       1.1        pk 	}
   1326       1.1        pk 
   1327       1.1        pk 	/* done! */
   1328       1.1        pk 
   1329       1.1        pk 	splx(s);
   1330       1.1        pk 	return;
   1331       1.1        pk 
   1332       1.1        pk bad:				/* tells upper layers we have an error */
   1333       1.1        pk 	bp->b_flags |= B_ERROR;
   1334       1.1        pk done:				/* tells upper layers we are done with this
   1335       1.1        pk 				 * buf */
   1336       1.1        pk 	bp->b_resid = bp->b_bcount;
   1337       1.1        pk 	biodone(bp);
   1338       1.1        pk }
   1339       1.1        pk /*
   1340       1.1        pk  * end of {b,c}devsw functions
   1341       1.1        pk  */
   1342       1.1        pk 
   1343       1.1        pk /*
   1344       1.1        pk  * i n t e r r u p t   f u n c t i o n
   1345       1.1        pk  *
   1346       1.1        pk  * xdcintr: hardware interrupt.
   1347       1.1        pk  */
   1348       1.1        pk int
   1349       1.1        pk xdcintr(v)
   1350       1.1        pk 	void   *v;
   1351       1.1        pk 
   1352       1.1        pk {
   1353       1.1        pk 	struct xdc_softc *xdcsc = v;
   1354       1.1        pk 
   1355       1.1        pk 	/* kick the event counter */
   1356       1.1        pk 
   1357       1.1        pk 	xdcsc->sc_intrcnt.ev_count++;
   1358       1.1        pk 
   1359       1.1        pk 	/* remove as many done IOPBs as possible */
   1360       1.1        pk 
   1361       1.1        pk 	xdc_remove_iorq(xdcsc);
   1362       1.1        pk 
   1363       1.1        pk 	/* start any iorq's already waiting */
   1364       1.1        pk 
   1365       1.1        pk 	xdc_start(xdcsc, XDC_MAXIOPB);
   1366       1.1        pk 
   1367       1.1        pk 	/* fill up any remaining iorq's with queue'd buffers */
   1368       1.1        pk 
   1369      1.58      yamt 	while (xdcsc->nfree > 0 && BUFQ_PEEK(xdcsc->sc_wq) != NULL)
   1370       1.1        pk 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
   1371       1.1        pk 			break;
   1372       1.1        pk 
   1373       1.1        pk 	return (1);
   1374       1.1        pk }
   1375       1.1        pk /*
   1376       1.1        pk  * end of interrupt function
   1377       1.1        pk  */
   1378       1.1        pk 
   1379       1.1        pk /*
   1380       1.1        pk  * i n t e r n a l   f u n c t i o n s
   1381       1.1        pk  */
   1382       1.1        pk 
   1383       1.1        pk /*
   1384       1.1        pk  * xdc_rqinit: fill out the fields of an I/O request
   1385       1.1        pk  */
   1386       1.1        pk 
   1387       1.1        pk inline void
   1388       1.1        pk xdc_rqinit(rq, xdc, xd, md, blk, cnt, db, bp)
   1389       1.1        pk 	struct xd_iorq *rq;
   1390       1.1        pk 	struct xdc_softc *xdc;
   1391       1.1        pk 	struct xd_softc *xd;
   1392       1.1        pk 	int     md;
   1393       1.1        pk 	u_long  blk;
   1394       1.1        pk 	int     cnt;
   1395       1.1        pk 	caddr_t db;
   1396       1.1        pk 	struct buf *bp;
   1397       1.1        pk {
   1398       1.1        pk 	rq->xdc = xdc;
   1399       1.1        pk 	rq->xd = xd;
   1400       1.1        pk 	rq->ttl = XDC_MAXTTL + 10;
   1401       1.1        pk 	rq->mode = md;
   1402       1.1        pk 	rq->tries = rq->errno = rq->lasterror = 0;
   1403       1.1        pk 	rq->blockno = blk;
   1404       1.1        pk 	rq->sectcnt = cnt;
   1405       1.3        pk 	rq->dbuf = db;
   1406       1.1        pk 	rq->buf = bp;
   1407       1.1        pk }
   1408       1.1        pk /*
   1409       1.1        pk  * xdc_rqtopb: load up an IOPB based on an iorq
   1410       1.1        pk  */
   1411       1.1        pk 
   1412       1.1        pk void
   1413       1.1        pk xdc_rqtopb(iorq, iopb, cmd, subfun)
   1414       1.1        pk 	struct xd_iorq *iorq;
   1415       1.1        pk 	struct xd_iopb *iopb;
   1416       1.1        pk 	int     cmd, subfun;
   1417       1.1        pk 
   1418       1.1        pk {
   1419       1.1        pk 	u_long  block, dp;
   1420       1.1        pk 
   1421       1.1        pk 	/* standard stuff */
   1422       1.1        pk 
   1423       1.1        pk 	iopb->errs = iopb->done = 0;
   1424       1.1        pk 	iopb->comm = cmd;
   1425       1.1        pk 	iopb->errno = iopb->status = 0;
   1426       1.1        pk 	iopb->subfun = subfun;
   1427       1.1        pk 	if (iorq->xd)
   1428       1.1        pk 		iopb->unit = iorq->xd->xd_drive;
   1429       1.1        pk 	else
   1430       1.1        pk 		iopb->unit = 0;
   1431       1.1        pk 
   1432       1.1        pk 	/* check for alternate IOPB format */
   1433       1.1        pk 
   1434       1.1        pk 	if (cmd == XDCMD_WRP) {
   1435       1.1        pk 		switch (subfun) {
   1436       1.1        pk 		case XDFUN_CTL:{
   1437       1.1        pk 			struct xd_iopb_ctrl *ctrl =
   1438       1.1        pk 				(struct xd_iopb_ctrl *) iopb;
   1439       1.1        pk 			iopb->lll = 0;
   1440       1.1        pk 			iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1441       1.1        pk 					? 0
   1442       1.1        pk 					: iorq->xdc->ipl;
   1443       1.1        pk 			ctrl->param_a = XDPA_TMOD | XDPA_DACF;
   1444       1.1        pk 			ctrl->param_b = XDPB_ROR | XDPB_TDT_3_2USEC;
   1445       1.1        pk 			ctrl->param_c = XDPC_OVS | XDPC_COP | XDPC_ASR |
   1446       1.1        pk 					XDPC_RBC | XDPC_ECC2;
   1447       1.1        pk 			ctrl->throttle = XDC_THROTTLE;
   1448       1.1        pk 			ctrl->delay = XDC_DELAY;
   1449       1.1        pk 			break;
   1450       1.1        pk 			}
   1451       1.1        pk 		case XDFUN_DRV:{
   1452       1.1        pk 			struct xd_iopb_drive *drv =
   1453       1.1        pk 				(struct xd_iopb_drive *)iopb;
   1454       1.1        pk 			/* we assume that the disk label has the right
   1455       1.1        pk 			 * info */
   1456       1.1        pk 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1457       1.1        pk 				drv->dparam_ipl = (XDC_DPARAM << 3);
   1458       1.1        pk 			else
   1459       1.1        pk 				drv->dparam_ipl = (XDC_DPARAM << 3) |
   1460       1.1        pk 						  iorq->xdc->ipl;
   1461       1.1        pk 			drv->maxsect = iorq->xd->nsect - 1;
   1462       1.1        pk 			drv->maxsector = drv->maxsect;
   1463       1.1        pk 			/* note: maxsector != maxsect only if you are
   1464       1.1        pk 			 * doing cyl sparing */
   1465       1.1        pk 			drv->headoff = 0;
   1466       1.1        pk 			drv->maxcyl = iorq->xd->pcyl - 1;
   1467       1.1        pk 			drv->maxhead = iorq->xd->nhead - 1;
   1468       1.1        pk 			break;
   1469       1.1        pk 			}
   1470       1.1        pk 		case XDFUN_FMT:{
   1471       1.1        pk 			struct xd_iopb_format *form =
   1472       1.1        pk 					(struct xd_iopb_format *) iopb;
   1473       1.1        pk 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1474       1.1        pk 				form->interleave_ipl = (XDC_INTERLEAVE << 3);
   1475       1.1        pk 			else
   1476       1.1        pk 				form->interleave_ipl = (XDC_INTERLEAVE << 3) |
   1477       1.1        pk 						       iorq->xdc->ipl;
   1478       1.1        pk 			form->field1 = XDFM_FIELD1;
   1479       1.1        pk 			form->field2 = XDFM_FIELD2;
   1480       1.1        pk 			form->field3 = XDFM_FIELD3;
   1481       1.1        pk 			form->field4 = XDFM_FIELD4;
   1482       1.1        pk 			form->bytespersec = XDFM_BPS;
   1483       1.1        pk 			form->field6 = XDFM_FIELD6;
   1484       1.1        pk 			form->field7 = XDFM_FIELD7;
   1485       1.1        pk 			break;
   1486       1.1        pk 			}
   1487       1.1        pk 		}
   1488       1.1        pk 	} else {
   1489       1.1        pk 
   1490       1.1        pk 		/* normal IOPB case (harmless to RDP command) */
   1491       1.1        pk 
   1492       1.1        pk 		iopb->lll = 0;
   1493       1.1        pk 		iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1494       1.1        pk 				? 0
   1495       1.1        pk 				: iorq->xdc->ipl;
   1496       1.1        pk 		iopb->sectcnt = iorq->sectcnt;
   1497       1.1        pk 		block = iorq->blockno;
   1498       1.1        pk 		if (iorq->xd == NULL || block == 0) {
   1499       1.1        pk 			iopb->sectno = iopb->headno = iopb->cylno = 0;
   1500       1.1        pk 		} else {
   1501       1.1        pk 			iopb->sectno = block % iorq->xd->nsect;
   1502       1.1        pk 			block = block / iorq->xd->nsect;
   1503       1.1        pk 			iopb->headno = block % iorq->xd->nhead;
   1504       1.1        pk 			block = block / iorq->xd->nhead;
   1505       1.1        pk 			iopb->cylno = block;
   1506       1.1        pk 		}
   1507       1.3        pk 		dp = (u_long) iorq->dbuf;
   1508       1.1        pk 		dp = iopb->daddr = (iorq->dbuf == NULL) ? 0 : dp;
   1509       1.1        pk 		iopb->addrmod = ((dp + (XDFM_BPS * iorq->sectcnt)) > 0x1000000)
   1510       1.1        pk 					? XDC_ADDRMOD32
   1511       1.1        pk 					: XDC_ADDRMOD;
   1512       1.1        pk 	}
   1513       1.1        pk }
   1514       1.1        pk 
   1515       1.1        pk /*
   1516       1.1        pk  * xdc_cmd: front end for POLL'd and WAIT'd commands.  Returns rqno.
   1517       1.1        pk  * If you've already got an IORQ, you can call submit directly (currently
   1518      1.37       wiz  * there is no need to do this).    NORM requests are handled separately.
   1519       1.1        pk  */
   1520       1.1        pk int
   1521       1.1        pk xdc_cmd(xdcsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
   1522       1.1        pk 	struct xdc_softc *xdcsc;
   1523       1.1        pk 	int     cmd, subfn, unit, block, scnt;
   1524       1.1        pk 	char   *dptr;
   1525       1.1        pk 	int     fullmode;
   1526       1.1        pk 
   1527       1.1        pk {
   1528       1.1        pk 	int     rqno, submode = XD_STATE(fullmode), retry;
   1529       1.1        pk 	struct xd_iorq *iorq;
   1530       1.1        pk 	struct xd_iopb *iopb;
   1531       1.1        pk 
   1532       1.1        pk 	/* get iorq/iopb */
   1533       1.1        pk 	switch (submode) {
   1534       1.1        pk 	case XD_SUB_POLL:
   1535       1.1        pk 		while (xdcsc->nfree == 0) {
   1536       1.1        pk 			if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
   1537       1.1        pk 				return (XD_ERR_FAIL);
   1538       1.1        pk 		}
   1539       1.1        pk 		break;
   1540       1.1        pk 	case XD_SUB_WAIT:
   1541       1.1        pk 		retry = 1;
   1542       1.1        pk 		while (retry) {
   1543       1.1        pk 			while (xdcsc->nfree == 0) {
   1544       1.1        pk 			    if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
   1545       1.1        pk 				return (XD_ERR_FAIL);
   1546       1.1        pk 			}
   1547       1.1        pk 			while (xdcsc->ndone > XDC_SUBWAITLIM) {
   1548       1.1        pk 			    if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
   1549       1.1        pk 				return (XD_ERR_FAIL);
   1550       1.1        pk 			}
   1551       1.1        pk 			if (xdcsc->nfree)
   1552       1.1        pk 				retry = 0;	/* got it */
   1553       1.1        pk 		}
   1554       1.1        pk 		break;
   1555       1.1        pk 	default:
   1556       1.1        pk 		return (XD_ERR_FAIL);	/* illegal */
   1557       1.1        pk 	}
   1558       1.1        pk 	if (xdcsc->nfree == 0)
   1559       1.1        pk 		panic("xdcmd nfree");
   1560       1.1        pk 	rqno = XDC_RQALLOC(xdcsc);
   1561       1.1        pk 	iorq = &xdcsc->reqs[rqno];
   1562       1.1        pk 	iopb = iorq->iopb;
   1563       1.1        pk 
   1564       1.1        pk 
   1565       1.1        pk 	/* init iorq/iopb */
   1566       1.1        pk 
   1567       1.1        pk 	xdc_rqinit(iorq, xdcsc,
   1568       1.1        pk 	    (unit == XDC_NOUNIT) ? NULL : xdcsc->sc_drives[unit],
   1569       1.1        pk 	    fullmode, block, scnt, dptr, NULL);
   1570       1.1        pk 
   1571       1.1        pk 	/* load IOPB from iorq */
   1572       1.1        pk 
   1573       1.1        pk 	xdc_rqtopb(iorq, iopb, cmd, subfn);
   1574       1.1        pk 
   1575       1.1        pk 	/* submit it for processing */
   1576       1.1        pk 
   1577       1.1        pk 	xdc_submit_iorq(xdcsc, rqno, fullmode);	/* error code will be in iorq */
   1578       1.1        pk 
   1579       1.1        pk 	return (rqno);
   1580       1.1        pk }
   1581       1.1        pk /*
   1582       1.1        pk  * xdc_startbuf
   1583       1.1        pk  * start a buffer running, assumes nfree > 0
   1584       1.1        pk  */
   1585       1.1        pk 
   1586       1.1        pk int
   1587       1.1        pk xdc_startbuf(xdcsc, xdsc, bp)
   1588       1.1        pk 	struct xdc_softc *xdcsc;
   1589       1.1        pk 	struct xd_softc *xdsc;
   1590       1.1        pk 	struct buf *bp;
   1591       1.1        pk 
   1592       1.1        pk {
   1593       1.1        pk 	int     rqno, partno;
   1594       1.1        pk 	struct xd_iorq *iorq;
   1595       1.1        pk 	struct xd_iopb *iopb;
   1596       1.1        pk 	u_long  block;
   1597       1.3        pk /*	caddr_t dbuf;*/
   1598       1.3        pk 	int error;
   1599       1.1        pk 
   1600       1.1        pk 	if (!xdcsc->nfree)
   1601       1.1        pk 		panic("xdc_startbuf free");
   1602       1.1        pk 	rqno = XDC_RQALLOC(xdcsc);
   1603       1.1        pk 	iorq = &xdcsc->reqs[rqno];
   1604       1.1        pk 	iopb = iorq->iopb;
   1605       1.1        pk 
   1606       1.1        pk 	/* get buf */
   1607       1.1        pk 
   1608       1.1        pk 	if (bp == NULL) {
   1609      1.58      yamt 		bp = BUFQ_GET(xdcsc->sc_wq);
   1610      1.18   thorpej 		if (bp == NULL)
   1611       1.1        pk 			panic("xdc_startbuf bp");
   1612       1.1        pk 		xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
   1613       1.1        pk 	}
   1614       1.1        pk 	partno = DISKPART(bp->b_dev);
   1615       1.1        pk #ifdef XDC_DEBUG
   1616       1.1        pk 	printf("xdc_startbuf: %s%c: %s block %d\n", xdsc->sc_dev.dv_xname,
   1617       1.1        pk 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1618       1.1        pk 	printf("xdc_startbuf: b_bcount %d, b_data 0x%x\n",
   1619       1.1        pk 	    bp->b_bcount, bp->b_data);
   1620       1.1        pk #endif
   1621       1.1        pk 
   1622       1.1        pk 	/*
   1623       1.1        pk 	 * load request.  we have to calculate the correct block number based
   1624       1.1        pk 	 * on partition info.
   1625       1.1        pk 	 *
   1626       1.1        pk 	 * note that iorq points to the buffer as mapped into DVMA space,
   1627       1.1        pk 	 * where as the bp->b_data points to its non-DVMA mapping.
   1628       1.1        pk 	 */
   1629       1.1        pk 
   1630       1.1        pk 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
   1631       1.1        pk 	    xdsc->sc_dk.dk_label->d_partitions[partno].p_offset);
   1632       1.1        pk 
   1633       1.3        pk 	error = bus_dmamap_load(xdcsc->dmatag, iorq->dmamap,
   1634       1.3        pk 			 bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
   1635       1.3        pk 	if (error != 0) {
   1636       1.3        pk 		printf("%s: warning: cannot load DMA map\n",
   1637       1.1        pk 			xdcsc->sc_dev.dv_xname);
   1638       1.1        pk 		XDC_FREE(xdcsc, rqno);
   1639      1.58      yamt 		BUFQ_PUT(xdcsc->sc_wq, bp);
   1640       1.1        pk 		return (XD_ERR_FAIL);	/* XXX: need some sort of
   1641       1.1        pk 					 * call-back scheme here? */
   1642       1.1        pk 	}
   1643       1.9   thorpej 	bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap, 0,
   1644       1.9   thorpej 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
   1645       1.3        pk 				? BUS_DMASYNC_PREREAD
   1646       1.3        pk 				: BUS_DMASYNC_PREWRITE);
   1647       1.1        pk 
   1648       1.1        pk 	/* init iorq and load iopb from it */
   1649       1.1        pk 	xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
   1650       1.3        pk 		   bp->b_bcount / XDFM_BPS,
   1651      1.38       eeh 		   (caddr_t)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
   1652       1.3        pk 		   bp);
   1653       1.1        pk 
   1654       1.1        pk 	xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);
   1655       1.1        pk 
   1656       1.1        pk 	/* Instrumentation. */
   1657       1.1        pk 	disk_busy(&xdsc->sc_dk);
   1658       1.1        pk 
   1659       1.1        pk 	/* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */
   1660       1.1        pk 
   1661       1.1        pk 	xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
   1662       1.1        pk 	return (XD_ERR_AOK);
   1663       1.1        pk }
   1664       1.1        pk 
   1665       1.1        pk 
   1666       1.1        pk /*
   1667       1.1        pk  * xdc_submit_iorq: submit an iorq for processing.  returns XD_ERR_AOK
   1668       1.1        pk  * if ok.  if it fail returns an error code.  type is XD_SUB_*.
   1669       1.1        pk  *
   1670       1.1        pk  * note: caller frees iorq in all cases except NORM
   1671       1.1        pk  *
   1672       1.1        pk  * return value:
   1673       1.1        pk  *   NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
   1674       1.1        pk  *   WAIT: XD_AOK (success), <error-code> (failed)
   1675       1.1        pk  *   POLL: <same as WAIT>
   1676       1.1        pk  *   NOQ : <same as NORM>
   1677       1.1        pk  *
   1678       1.1        pk  * there are three sources for i/o requests:
   1679       1.1        pk  * [1] xdstrategy: normal block I/O, using "struct buf" system.
   1680       1.1        pk  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1681       1.1        pk  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1682       1.1        pk  *                 and the process should block until they are done.
   1683       1.1        pk  *
   1684       1.1        pk  * software state is stored in the iorq structure.  each iorq has an
   1685       1.1        pk  * iopb structure.  the hardware understands the iopb structure.
   1686       1.1        pk  * every command must go through an iopb.  a 7053 can only handle
   1687       1.1        pk  * XDC_MAXIOPB (31) active iopbs at one time.  iopbs are allocated in
   1688       1.1        pk  * DVMA space at boot up time.  what happens if we run out of iopb's?
   1689       1.1        pk  * for i/o type [1], the buffers are queued at the "buff" layer and
   1690       1.1        pk  * picked up later by the interrupt routine.  for case [2] the
   1691       1.1        pk  * programmed i/o driver is called with a special flag that says
   1692       1.1        pk  * return when one iopb is free.  for case [3] the process can sleep
   1693      1.54       wiz  * on the iorq free list until some iopbs are available.
   1694       1.1        pk  */
   1695       1.1        pk 
   1696       1.1        pk 
   1697       1.1        pk int
   1698       1.1        pk xdc_submit_iorq(xdcsc, iorqno, type)
   1699       1.1        pk 	struct xdc_softc *xdcsc;
   1700       1.1        pk 	int     iorqno;
   1701       1.1        pk 	int     type;
   1702       1.1        pk 
   1703       1.1        pk {
   1704       1.1        pk 	u_long  iopbaddr;
   1705       1.1        pk 	struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
   1706       1.1        pk 
   1707       1.1        pk #ifdef XDC_DEBUG
   1708       1.1        pk 	printf("xdc_submit_iorq(%s, no=%d, type=%d)\n", xdcsc->sc_dev.dv_xname,
   1709       1.1        pk 	    iorqno, type);
   1710       1.1        pk #endif
   1711       1.1        pk 
   1712       1.1        pk 	/* first check and see if controller is busy */
   1713       1.1        pk 	if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
   1714       1.1        pk #ifdef XDC_DEBUG
   1715       1.1        pk 		printf("xdc_submit_iorq: XDC not ready (ADDING)\n");
   1716       1.1        pk #endif
   1717       1.1        pk 		if (type == XD_SUB_NOQ)
   1718       1.1        pk 			return (XD_ERR_FAIL);	/* failed */
   1719       1.1        pk 		XDC_TWAIT(xdcsc, iorqno);	/* put at end of waitq */
   1720       1.1        pk 		switch (type) {
   1721       1.1        pk 		case XD_SUB_NORM:
   1722       1.1        pk 			return XD_ERR_AOK;	/* success */
   1723       1.1        pk 		case XD_SUB_WAIT:
   1724       1.1        pk 			while (iorq->iopb->done == 0) {
   1725      1.25   thorpej 				(void) tsleep(iorq, PRIBIO, "xdciorq", 0);
   1726       1.1        pk 			}
   1727       1.1        pk 			return (iorq->errno);
   1728       1.1        pk 		case XD_SUB_POLL:
   1729       1.1        pk 			return (xdc_piodriver(xdcsc, iorqno, 0));
   1730       1.1        pk 		default:
   1731       1.1        pk 			panic("xdc_submit_iorq adding");
   1732       1.1        pk 		}
   1733       1.1        pk 	}
   1734       1.1        pk #ifdef XDC_DEBUG
   1735       1.1        pk 	{
   1736       1.1        pk 		u_char *rio = (u_char *) iorq->iopb;
   1737       1.1        pk 		int     sz = sizeof(struct xd_iopb), lcv;
   1738       1.1        pk 		printf("%s: aio #%d [",
   1739       1.1        pk 			xdcsc->sc_dev.dv_xname, iorq - xdcsc->reqs);
   1740       1.1        pk 		for (lcv = 0; lcv < sz; lcv++)
   1741       1.1        pk 			printf(" %02x", rio[lcv]);
   1742       1.1        pk 		printf("]\n");
   1743       1.1        pk 	}
   1744       1.1        pk #endif				/* XDC_DEBUG */
   1745       1.1        pk 
   1746       1.1        pk 	/* controller not busy, start command */
   1747      1.12        pk 	iopbaddr = (u_long) iorq->dmaiopb;
   1748       1.1        pk 	XDC_GO(xdcsc->xdc, iopbaddr);	/* go! */
   1749       1.1        pk 	xdcsc->nrun++;
   1750       1.1        pk 	/* command now running, wrap it up */
   1751       1.1        pk 	switch (type) {
   1752       1.1        pk 	case XD_SUB_NORM:
   1753       1.1        pk 	case XD_SUB_NOQ:
   1754       1.1        pk 		return (XD_ERR_AOK);	/* success */
   1755       1.1        pk 	case XD_SUB_WAIT:
   1756       1.1        pk 		while (iorq->iopb->done == 0) {
   1757      1.25   thorpej 			(void) tsleep(iorq, PRIBIO, "xdciorq", 0);
   1758       1.1        pk 		}
   1759       1.1        pk 		return (iorq->errno);
   1760       1.1        pk 	case XD_SUB_POLL:
   1761       1.1        pk 		return (xdc_piodriver(xdcsc, iorqno, 0));
   1762       1.1        pk 	default:
   1763       1.1        pk 		panic("xdc_submit_iorq wrap up");
   1764       1.1        pk 	}
   1765       1.1        pk 	panic("xdc_submit_iorq");
   1766       1.1        pk 	return 0;	/* not reached */
   1767       1.1        pk }
   1768       1.1        pk 
   1769       1.1        pk 
   1770       1.1        pk /*
   1771       1.1        pk  * xdc_piodriver
   1772       1.1        pk  *
   1773       1.1        pk  * programmed i/o driver.   this function takes over the computer
   1774       1.1        pk  * and drains off all i/o requests.   it returns the status of the iorq
   1775       1.1        pk  * the caller is interesting in.   if freeone is true, then it returns
   1776       1.1        pk  * when there is a free iorq.
   1777       1.1        pk  */
   1778       1.1        pk int
   1779       1.1        pk xdc_piodriver(xdcsc, iorqno, freeone)
   1780      1.11        pk 	struct	xdc_softc *xdcsc;
   1781      1.11        pk 	int	iorqno;
   1782      1.11        pk 	int	freeone;
   1783       1.1        pk 
   1784       1.1        pk {
   1785      1.11        pk 	int	nreset = 0;
   1786      1.11        pk 	int	retval = 0;
   1787      1.11        pk 	u_long	count;
   1788      1.11        pk 	struct	xdc *xdc = xdcsc->xdc;
   1789       1.1        pk #ifdef XDC_DEBUG
   1790       1.1        pk 	printf("xdc_piodriver(%s, %d, freeone=%d)\n", xdcsc->sc_dev.dv_xname,
   1791       1.1        pk 	    iorqno, freeone);
   1792       1.1        pk #endif
   1793       1.1        pk 
   1794       1.1        pk 	while (xdcsc->nwait || xdcsc->nrun) {
   1795       1.1        pk #ifdef XDC_DEBUG
   1796       1.1        pk 		printf("xdc_piodriver: wait=%d, run=%d\n",
   1797       1.1        pk 			xdcsc->nwait, xdcsc->nrun);
   1798       1.1        pk #endif
   1799       1.1        pk 		XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
   1800       1.1        pk #ifdef XDC_DEBUG
   1801       1.1        pk 		printf("xdc_piodriver: done wait with count = %d\n", count);
   1802       1.1        pk #endif
   1803       1.1        pk 		/* we expect some progress soon */
   1804       1.1        pk 		if (count == 0 && nreset >= 2) {
   1805       1.1        pk 			xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
   1806       1.1        pk #ifdef XDC_DEBUG
   1807       1.1        pk 			printf("xdc_piodriver: timeout\n");
   1808       1.1        pk #endif
   1809       1.1        pk 			return (XD_ERR_FAIL);
   1810       1.1        pk 		}
   1811       1.1        pk 		if (count == 0) {
   1812       1.1        pk 			if (xdc_reset(xdcsc, 0,
   1813       1.1        pk 				      (nreset++ == 0) ? XD_RSET_NONE : iorqno,
   1814       1.1        pk 				      XD_ERR_FAIL,
   1815       1.1        pk 				      0) == XD_ERR_FAIL)
   1816       1.1        pk 				return (XD_ERR_FAIL);	/* flushes all but POLL
   1817       1.1        pk 							 * requests, resets */
   1818       1.1        pk 			continue;
   1819       1.1        pk 		}
   1820       1.1        pk 		xdc_remove_iorq(xdcsc);	/* could resubmit request */
   1821       1.1        pk 		if (freeone) {
   1822       1.1        pk 			if (xdcsc->nrun < XDC_MAXIOPB) {
   1823       1.1        pk #ifdef XDC_DEBUG
   1824       1.1        pk 				printf("xdc_piodriver: done: one free\n");
   1825       1.1        pk #endif
   1826       1.1        pk 				return (XD_ERR_AOK);
   1827       1.1        pk 			}
   1828       1.1        pk 			continue;	/* don't xdc_start */
   1829       1.1        pk 		}
   1830       1.1        pk 		xdc_start(xdcsc, XDC_MAXIOPB);
   1831       1.1        pk 	}
   1832       1.1        pk 
   1833       1.1        pk 	/* get return value */
   1834       1.1        pk 
   1835       1.1        pk 	retval = xdcsc->reqs[iorqno].errno;
   1836       1.1        pk 
   1837       1.1        pk #ifdef XDC_DEBUG
   1838       1.1        pk 	printf("xdc_piodriver: done, retval = 0x%x (%s)\n",
   1839       1.1        pk 	    xdcsc->reqs[iorqno].errno, xdc_e2str(xdcsc->reqs[iorqno].errno));
   1840       1.1        pk #endif
   1841       1.1        pk 
   1842       1.1        pk 	/* now that we've drained everything, start up any bufs that have
   1843       1.1        pk 	 * queued */
   1844       1.1        pk 
   1845      1.58      yamt 	while (xdcsc->nfree > 0 && BUFQ_PEEK(xdcsc->sc_wq) != NULL)
   1846       1.1        pk 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
   1847       1.1        pk 			break;
   1848       1.1        pk 
   1849       1.1        pk 	return (retval);
   1850       1.1        pk }
   1851       1.1        pk 
   1852       1.1        pk /*
   1853       1.1        pk  * xdc_reset: reset one drive.   NOTE: assumes xdc was just reset.
   1854       1.1        pk  * we steal iopb[0] for this, but we put it back when we are done.
   1855       1.1        pk  */
   1856       1.1        pk void
   1857       1.1        pk xdc_xdreset(xdcsc, xdsc)
   1858       1.1        pk 	struct xdc_softc *xdcsc;
   1859       1.1        pk 	struct xd_softc *xdsc;
   1860       1.1        pk 
   1861       1.1        pk {
   1862       1.1        pk 	struct xd_iopb tmpiopb;
   1863       1.1        pk 	u_long  addr;
   1864       1.1        pk 	int     del;
   1865       1.1        pk 	bcopy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
   1866       1.1        pk 	bzero(xdcsc->iopbase, sizeof(tmpiopb));
   1867       1.1        pk 	xdcsc->iopbase->comm = XDCMD_RST;
   1868       1.1        pk 	xdcsc->iopbase->unit = xdsc->xd_drive;
   1869       1.1        pk 	addr = (u_long) xdcsc->dvmaiopb;
   1870       1.1        pk 	XDC_GO(xdcsc->xdc, addr);	/* go! */
   1871       1.1        pk 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
   1872       1.1        pk 	if (del <= 0 || xdcsc->iopbase->errs) {
   1873       1.1        pk 		printf("%s: off-line: %s\n", xdcsc->sc_dev.dv_xname,
   1874       1.1        pk 		    xdc_e2str(xdcsc->iopbase->errno));
   1875       1.1        pk 		xdcsc->xdc->xdc_csr = XDC_RESET;
   1876       1.1        pk 		XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
   1877       1.1        pk 		if (del <= 0)
   1878       1.1        pk 			panic("xdc_reset");
   1879       1.1        pk 	} else {
   1880       1.1        pk 		xdcsc->xdc->xdc_csr = XDC_CLRRIO;	/* clear RIO */
   1881       1.1        pk 	}
   1882       1.1        pk 	bcopy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
   1883       1.1        pk }
   1884       1.1        pk 
   1885       1.1        pk 
   1886       1.1        pk /*
   1887       1.1        pk  * xdc_reset: reset everything: requests are marked as errors except
   1888       1.1        pk  * a polled request (which is resubmitted)
   1889       1.1        pk  */
   1890       1.1        pk int
   1891       1.1        pk xdc_reset(xdcsc, quiet, blastmode, error, xdsc)
   1892       1.1        pk 	struct xdc_softc *xdcsc;
   1893       1.1        pk 	int     quiet, blastmode, error;
   1894       1.1        pk 	struct xd_softc *xdsc;
   1895       1.1        pk 
   1896       1.1        pk {
   1897       1.1        pk 	int     del = 0, lcv, retval = XD_ERR_AOK;
   1898       1.1        pk 	int     oldfree = xdcsc->nfree;
   1899       1.1        pk 
   1900       1.1        pk 	/* soft reset hardware */
   1901       1.1        pk 
   1902       1.1        pk 	if (!quiet)
   1903       1.1        pk 		printf("%s: soft reset\n", xdcsc->sc_dev.dv_xname);
   1904       1.1        pk 	xdcsc->xdc->xdc_csr = XDC_RESET;
   1905       1.1        pk 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
   1906       1.1        pk 	if (del <= 0) {
   1907       1.1        pk 		blastmode = XD_RSET_ALL;	/* dead, flush all requests */
   1908       1.1        pk 		retval = XD_ERR_FAIL;
   1909       1.1        pk 	}
   1910       1.1        pk 	if (xdsc)
   1911       1.1        pk 		xdc_xdreset(xdcsc, xdsc);
   1912       1.1        pk 
   1913       1.1        pk 	/* fix queues based on "blast-mode" */
   1914       1.1        pk 
   1915       1.1        pk 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   1916       1.1        pk 		register struct xd_iorq *iorq = &xdcsc->reqs[lcv];
   1917       1.1        pk 
   1918       1.1        pk 		if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
   1919       1.1        pk 		    XD_STATE(iorq->mode) != XD_SUB_WAIT &&
   1920       1.1        pk 		    XD_STATE(iorq->mode) != XD_SUB_NORM)
   1921       1.1        pk 			/* is it active? */
   1922       1.1        pk 			continue;
   1923       1.1        pk 
   1924       1.1        pk 		xdcsc->nrun--;	/* it isn't running any more */
   1925       1.1        pk 		if (blastmode == XD_RSET_ALL || blastmode != lcv) {
   1926       1.1        pk 			/* failed */
   1927       1.1        pk 			iorq->errno = error;
   1928       1.1        pk 			xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
   1929       1.1        pk 			switch (XD_STATE(xdcsc->reqs[lcv].mode)) {
   1930       1.1        pk 			case XD_SUB_NORM:
   1931       1.1        pk 			    iorq->buf->b_error = EIO;
   1932       1.1        pk 			    iorq->buf->b_flags |= B_ERROR;
   1933       1.1        pk 			    iorq->buf->b_resid =
   1934       1.1        pk 			       iorq->sectcnt * XDFM_BPS;
   1935       1.3        pk 
   1936       1.9   thorpej 			    bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap, 0,
   1937       1.9   thorpej 					    iorq->dmamap->dm_mapsize,
   1938       1.3        pk 					    (iorq->buf->b_flags & B_READ)
   1939       1.3        pk 						? BUS_DMASYNC_POSTREAD
   1940       1.3        pk 						: BUS_DMASYNC_POSTWRITE);
   1941       1.3        pk 
   1942       1.3        pk 			    bus_dmamap_unload(xdcsc->dmatag, iorq->dmamap);
   1943       1.3        pk 
   1944       1.1        pk 			    disk_unbusy(&xdcsc->reqs[lcv].xd->sc_dk,
   1945       1.1        pk 				(xdcsc->reqs[lcv].buf->b_bcount -
   1946      1.47       mrg 				xdcsc->reqs[lcv].buf->b_resid),
   1947      1.47       mrg 				(iorq->buf->b_flags & B_READ));
   1948       1.1        pk 			    biodone(iorq->buf);
   1949       1.1        pk 			    XDC_FREE(xdcsc, lcv);	/* add to free list */
   1950       1.1        pk 			    break;
   1951       1.1        pk 			case XD_SUB_WAIT:
   1952       1.1        pk 			    wakeup(iorq);
   1953       1.1        pk 			case XD_SUB_POLL:
   1954       1.1        pk 			    xdcsc->ndone++;
   1955       1.1        pk 			    iorq->mode =
   1956       1.1        pk 				XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   1957       1.1        pk 			    break;
   1958       1.1        pk 			}
   1959       1.1        pk 
   1960       1.1        pk 		} else {
   1961       1.1        pk 
   1962       1.1        pk 			/* resubmit, put at front of wait queue */
   1963       1.1        pk 			XDC_HWAIT(xdcsc, lcv);
   1964       1.1        pk 		}
   1965       1.1        pk 	}
   1966       1.1        pk 
   1967       1.1        pk 	/*
   1968       1.1        pk 	 * now, if stuff is waiting, start it.
   1969       1.1        pk 	 * since we just reset it should go
   1970       1.1        pk 	 */
   1971       1.1        pk 	xdc_start(xdcsc, XDC_MAXIOPB);
   1972       1.1        pk 
   1973       1.1        pk 	/* ok, we did it */
   1974       1.1        pk 	if (oldfree == 0 && xdcsc->nfree)
   1975       1.1        pk 		wakeup(&xdcsc->nfree);
   1976       1.1        pk 
   1977       1.1        pk #ifdef XDC_DIAG
   1978       1.1        pk 	del = xdcsc->nwait + xdcsc->nrun + xdcsc->nfree + xdcsc->ndone;
   1979       1.1        pk 	if (del != XDC_MAXIOPB)
   1980       1.1        pk 		printf("%s: diag: xdc_reset miscount (%d should be %d)!\n",
   1981       1.1        pk 		    xdcsc->sc_dev.dv_xname, del, XDC_MAXIOPB);
   1982       1.1        pk 	else
   1983       1.1        pk 		if (xdcsc->ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
   1984       1.1        pk 			printf("%s: diag: lots of done jobs (%d)\n",
   1985       1.1        pk 			    xdcsc->sc_dev.dv_xname, xdcsc->ndone);
   1986       1.1        pk #endif
   1987       1.1        pk 	printf("RESET DONE\n");
   1988       1.1        pk 	return (retval);
   1989       1.1        pk }
   1990       1.1        pk /*
   1991       1.1        pk  * xdc_start: start all waiting buffers
   1992       1.1        pk  */
   1993       1.1        pk 
   1994       1.1        pk void
   1995       1.1        pk xdc_start(xdcsc, maxio)
   1996       1.1        pk 	struct xdc_softc *xdcsc;
   1997       1.1        pk 	int     maxio;
   1998       1.1        pk 
   1999       1.1        pk {
   2000       1.1        pk 	int     rqno;
   2001       1.1        pk 	while (maxio && xdcsc->nwait &&
   2002       1.1        pk 		(xdcsc->xdc->xdc_csr & XDC_ADDING) == 0) {
   2003       1.1        pk 		XDC_GET_WAITER(xdcsc, rqno);	/* note: rqno is an "out"
   2004       1.1        pk 						 * param */
   2005       1.1        pk 		if (xdc_submit_iorq(xdcsc, rqno, XD_SUB_NOQ) != XD_ERR_AOK)
   2006       1.1        pk 			panic("xdc_start");	/* should never happen */
   2007       1.1        pk 		maxio--;
   2008       1.1        pk 	}
   2009       1.1        pk }
   2010       1.1        pk /*
   2011       1.1        pk  * xdc_remove_iorq: remove "done" IOPB's.
   2012       1.1        pk  */
   2013       1.1        pk 
   2014       1.1        pk int
   2015       1.1        pk xdc_remove_iorq(xdcsc)
   2016       1.1        pk 	struct xdc_softc *xdcsc;
   2017       1.1        pk 
   2018       1.1        pk {
   2019       1.1        pk 	int     errno, rqno, comm, errs;
   2020       1.1        pk 	struct xdc *xdc = xdcsc->xdc;
   2021       1.1        pk 	struct xd_iopb *iopb;
   2022       1.1        pk 	struct xd_iorq *iorq;
   2023       1.1        pk 	struct buf *bp;
   2024       1.1        pk 
   2025       1.1        pk 	if (xdc->xdc_csr & XDC_F_ERROR) {
   2026       1.1        pk 		/*
   2027       1.1        pk 		 * FATAL ERROR: should never happen under normal use. This
   2028       1.1        pk 		 * error is so bad, you can't even tell which IOPB is bad, so
   2029       1.1        pk 		 * we dump them all.
   2030       1.1        pk 		 */
   2031       1.1        pk 		errno = xdc->xdc_f_err;
   2032       1.1        pk 		printf("%s: fatal error 0x%02x: %s\n", xdcsc->sc_dev.dv_xname,
   2033       1.1        pk 		    errno, xdc_e2str(errno));
   2034       1.1        pk 		if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errno, 0) != XD_ERR_AOK) {
   2035       1.1        pk 			printf("%s: soft reset failed!\n",
   2036       1.1        pk 				xdcsc->sc_dev.dv_xname);
   2037       1.1        pk 			panic("xdc_remove_iorq: controller DEAD");
   2038       1.1        pk 		}
   2039       1.1        pk 		return (XD_ERR_AOK);
   2040       1.1        pk 	}
   2041       1.1        pk 
   2042       1.1        pk 	/*
   2043       1.1        pk 	 * get iopb that is done
   2044       1.1        pk 	 *
   2045       1.1        pk 	 * hmm... I used to read the address of the done IOPB off the VME
   2046       1.1        pk 	 * registers and calculate the rqno directly from that.   that worked
   2047       1.1        pk 	 * until I started putting a load on the controller.   when loaded, i
   2048       1.1        pk 	 * would get interrupts but neither the REMIOPB or F_ERROR bits would
   2049       1.1        pk 	 * be set, even after DELAY'ing a while!   later on the timeout
   2050       1.1        pk 	 * routine would detect IOPBs that were marked "running" but their
   2051       1.1        pk 	 * "done" bit was set.   rather than dealing directly with this
   2052       1.1        pk 	 * problem, it is just easier to look at all running IOPB's for the
   2053       1.1        pk 	 * done bit.
   2054       1.1        pk 	 */
   2055       1.1        pk 	if (xdc->xdc_csr & XDC_REMIOPB) {
   2056       1.1        pk 		xdc->xdc_csr = XDC_CLRRIO;
   2057       1.1        pk 	}
   2058       1.1        pk 
   2059       1.1        pk 	for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
   2060       1.1        pk 		iorq = &xdcsc->reqs[rqno];
   2061       1.1        pk 		if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
   2062       1.1        pk 			continue;	/* free, or done */
   2063       1.1        pk 		iopb = &xdcsc->iopbase[rqno];
   2064       1.1        pk 		if (iopb->done == 0)
   2065       1.1        pk 			continue;	/* not done yet */
   2066       1.1        pk 
   2067       1.1        pk #ifdef XDC_DEBUG
   2068       1.1        pk 		{
   2069       1.1        pk 			u_char *rio = (u_char *) iopb;
   2070       1.1        pk 			int     sz = sizeof(struct xd_iopb), lcv;
   2071       1.1        pk 			printf("%s: rio #%d [", xdcsc->sc_dev.dv_xname, rqno);
   2072       1.1        pk 			for (lcv = 0; lcv < sz; lcv++)
   2073       1.1        pk 				printf(" %02x", rio[lcv]);
   2074       1.1        pk 			printf("]\n");
   2075       1.1        pk 		}
   2076       1.1        pk #endif				/* XDC_DEBUG */
   2077       1.1        pk 
   2078       1.1        pk 		xdcsc->nrun--;
   2079       1.1        pk 
   2080       1.1        pk 		comm = iopb->comm;
   2081       1.1        pk 		errs = iopb->errs;
   2082       1.1        pk 
   2083       1.1        pk 		if (errs)
   2084       1.1        pk 			iorq->errno = iopb->errno;
   2085       1.1        pk 		else
   2086       1.1        pk 			iorq->errno = 0;
   2087       1.1        pk 
   2088       1.1        pk 		/* handle non-fatal errors */
   2089       1.1        pk 
   2090       1.1        pk 		if (errs &&
   2091       1.1        pk 		    xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
   2092       1.1        pk 			continue;	/* AOK: we resubmitted it */
   2093       1.1        pk 
   2094       1.1        pk 
   2095       1.1        pk 		/* this iorq is now done (hasn't been restarted or anything) */
   2096       1.1        pk 
   2097       1.1        pk 		if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
   2098       1.1        pk 			xdc_perror(iorq, iopb, 0);
   2099       1.1        pk 
   2100       1.1        pk 		/* now, if read/write check to make sure we got all the data
   2101       1.1        pk 		 * we needed. (this may not be the case if we got an error in
   2102       1.1        pk 		 * the middle of a multisector request).   */
   2103       1.1        pk 
   2104       1.1        pk 		if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
   2105       1.1        pk 		    (comm == XDCMD_RD || comm == XDCMD_WR)) {
   2106       1.1        pk 			/* we just successfully processed a bad144 sector
   2107       1.1        pk 			 * note: if we are in bad 144 mode, the pointers have
   2108       1.1        pk 			 * been advanced already (see above) and are pointing
   2109       1.1        pk 			 * at the bad144 sector.   to exit bad144 mode, we
   2110       1.1        pk 			 * must advance the pointers 1 sector and issue a new
   2111       1.1        pk 			 * request if there are still sectors left to process
   2112       1.1        pk 			 *
   2113       1.1        pk 			 */
   2114       1.1        pk 			XDC_ADVANCE(iorq, 1);	/* advance 1 sector */
   2115       1.1        pk 
   2116       1.1        pk 			/* exit b144 mode */
   2117       1.1        pk 			iorq->mode = iorq->mode & (~XD_MODE_B144);
   2118       1.1        pk 
   2119       1.1        pk 			if (iorq->sectcnt) {	/* more to go! */
   2120       1.1        pk 				iorq->lasterror = iorq->errno = iopb->errno = 0;
   2121       1.1        pk 				iopb->errs = iopb->done = 0;
   2122       1.1        pk 				iorq->tries = 0;
   2123       1.1        pk 				iopb->sectcnt = iorq->sectcnt;
   2124       1.1        pk 				iopb->cylno = iorq->blockno /
   2125       1.1        pk 						iorq->xd->sectpercyl;
   2126       1.1        pk 				iopb->headno =
   2127       1.1        pk 					(iorq->blockno / iorq->xd->nhead) %
   2128       1.1        pk 						iorq->xd->nhead;
   2129       1.1        pk 				iopb->sectno = iorq->blockno % XDFM_BPS;
   2130       1.3        pk 				iopb->daddr = (u_long) iorq->dbuf;
   2131       1.1        pk 				XDC_HWAIT(xdcsc, rqno);
   2132       1.1        pk 				xdc_start(xdcsc, 1);	/* resubmit */
   2133       1.1        pk 				continue;
   2134       1.1        pk 			}
   2135       1.1        pk 		}
   2136       1.1        pk 		/* final cleanup, totally done with this request */
   2137       1.1        pk 
   2138       1.1        pk 		switch (XD_STATE(iorq->mode)) {
   2139       1.1        pk 		case XD_SUB_NORM:
   2140       1.1        pk 			bp = iorq->buf;
   2141       1.1        pk 			if (errs) {
   2142       1.1        pk 				bp->b_error = EIO;
   2143       1.1        pk 				bp->b_flags |= B_ERROR;
   2144       1.1        pk 				bp->b_resid = iorq->sectcnt * XDFM_BPS;
   2145       1.1        pk 			} else {
   2146       1.1        pk 				bp->b_resid = 0;	/* done */
   2147       1.1        pk 			}
   2148       1.9   thorpej 			bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap, 0,
   2149       1.9   thorpej 					iorq->dmamap->dm_mapsize,
   2150       1.3        pk 					(bp->b_flags & B_READ)
   2151       1.3        pk 						? BUS_DMASYNC_POSTREAD
   2152       1.3        pk 						: BUS_DMASYNC_POSTWRITE);
   2153       1.3        pk 			bus_dmamap_unload(xdcsc->dmatag, iorq->dmamap);
   2154       1.3        pk 
   2155       1.1        pk 			disk_unbusy(&iorq->xd->sc_dk,
   2156      1.47       mrg 			    (bp->b_bcount - bp->b_resid),
   2157      1.47       mrg 			    (bp->b_flags & B_READ));
   2158       1.1        pk 			XDC_FREE(xdcsc, rqno);
   2159       1.1        pk 			biodone(bp);
   2160       1.1        pk 			break;
   2161       1.1        pk 		case XD_SUB_WAIT:
   2162       1.1        pk 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   2163       1.1        pk 			xdcsc->ndone++;
   2164       1.1        pk 			wakeup(iorq);
   2165       1.1        pk 			break;
   2166       1.1        pk 		case XD_SUB_POLL:
   2167       1.1        pk 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   2168       1.1        pk 			xdcsc->ndone++;
   2169       1.1        pk 			break;
   2170       1.1        pk 		}
   2171       1.1        pk 	}
   2172       1.1        pk 
   2173       1.1        pk 	return (XD_ERR_AOK);
   2174       1.1        pk }
   2175       1.1        pk 
   2176       1.1        pk /*
   2177       1.1        pk  * xdc_perror: print error.
   2178       1.1        pk  * - if still_trying is true: we got an error, retried and got a
   2179       1.1        pk  *   different error.  in that case lasterror is the old error,
   2180       1.1        pk  *   and errno is the new one.
   2181       1.1        pk  * - if still_trying is not true, then if we ever had an error it
   2182       1.1        pk  *   is in lasterror. also, if iorq->errno == 0, then we recovered
   2183       1.1        pk  *   from that error (otherwise iorq->errno == iorq->lasterror).
   2184       1.1        pk  */
   2185       1.1        pk void
   2186       1.1        pk xdc_perror(iorq, iopb, still_trying)
   2187       1.1        pk 	struct xd_iorq *iorq;
   2188       1.1        pk 	struct xd_iopb *iopb;
   2189       1.1        pk 	int     still_trying;
   2190       1.1        pk 
   2191       1.1        pk {
   2192       1.1        pk 
   2193       1.1        pk 	int     error = iorq->lasterror;
   2194       1.1        pk 
   2195       1.1        pk 	printf("%s", (iorq->xd) ? iorq->xd->sc_dev.dv_xname
   2196       1.1        pk 	    : iorq->xdc->sc_dev.dv_xname);
   2197       1.1        pk 	if (iorq->buf)
   2198       1.1        pk 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
   2199       1.1        pk 	if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
   2200       1.1        pk 		printf("%s %d/%d/%d: ",
   2201       1.1        pk 			(iopb->comm == XDCMD_RD) ? "read" : "write",
   2202       1.1        pk 			iopb->cylno, iopb->headno, iopb->sectno);
   2203       1.1        pk 	printf("%s", xdc_e2str(error));
   2204       1.1        pk 
   2205       1.1        pk 	if (still_trying)
   2206       1.1        pk 		printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
   2207       1.1        pk 	else
   2208       1.1        pk 		if (iorq->errno == 0)
   2209       1.1        pk 			printf(" [recovered in %d tries]", iorq->tries);
   2210       1.1        pk 
   2211       1.1        pk 	printf("\n");
   2212       1.1        pk }
   2213       1.1        pk 
   2214       1.1        pk /*
   2215       1.1        pk  * xdc_error: non-fatal error encountered... recover.
   2216       1.1        pk  * return AOK if resubmitted, return FAIL if this iopb is done
   2217       1.1        pk  */
   2218       1.1        pk int
   2219       1.1        pk xdc_error(xdcsc, iorq, iopb, rqno, comm)
   2220       1.1        pk 	struct xdc_softc *xdcsc;
   2221       1.1        pk 	struct xd_iorq *iorq;
   2222       1.1        pk 	struct xd_iopb *iopb;
   2223       1.1        pk 	int     rqno, comm;
   2224       1.1        pk 
   2225       1.1        pk {
   2226       1.1        pk 	int     errno = iorq->errno;
   2227       1.1        pk 	int     erract = errno & XD_ERA_MASK;
   2228      1.15  drochner 	int     oldmode, advance;
   2229      1.20       chs #ifdef __sparc__
   2230      1.15  drochner 	int i;
   2231      1.15  drochner #endif
   2232       1.1        pk 
   2233       1.1        pk 	if (erract == XD_ERA_RSET) {	/* some errors require a reset */
   2234       1.1        pk 		oldmode = iorq->mode;
   2235       1.1        pk 		iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
   2236       1.1        pk 		xdcsc->ndone++;
   2237       1.1        pk 		/* make xdc_start ignore us */
   2238       1.1        pk 		xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
   2239       1.1        pk 		iorq->mode = oldmode;
   2240       1.1        pk 		xdcsc->ndone--;
   2241       1.1        pk 	}
   2242       1.1        pk 	/* check for read/write to a sector in bad144 table if bad: redirect
   2243       1.1        pk 	 * request to bad144 area */
   2244       1.1        pk 
   2245       1.1        pk 	if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
   2246       1.1        pk 	    (iorq->mode & XD_MODE_B144) == 0) {
   2247       1.1        pk 		advance = iorq->sectcnt - iopb->sectcnt;
   2248       1.1        pk 		XDC_ADVANCE(iorq, advance);
   2249      1.20       chs #ifdef __sparc__
   2250       1.1        pk 		if ((i = isbad(&iorq->xd->dkb, iorq->blockno / iorq->xd->sectpercyl,
   2251       1.1        pk 			    (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
   2252       1.1        pk 			    iorq->blockno % iorq->xd->nsect)) != -1) {
   2253       1.1        pk 			iorq->mode |= XD_MODE_B144;	/* enter bad144 mode &
   2254       1.1        pk 							 * redirect */
   2255       1.1        pk 			iopb->errno = iopb->done = iopb->errs = 0;
   2256       1.1        pk 			iopb->sectcnt = 1;
   2257       1.1        pk 			iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
   2258       1.1        pk 			/* second to last acyl */
   2259       1.1        pk 			i = iorq->xd->sectpercyl - 1 - i;	/* follow bad144
   2260       1.1        pk 								 * standard */
   2261       1.1        pk 			iopb->headno = i / iorq->xd->nhead;
   2262       1.1        pk 			iopb->sectno = i % iorq->xd->nhead;
   2263       1.1        pk 			XDC_HWAIT(xdcsc, rqno);
   2264       1.1        pk 			xdc_start(xdcsc, 1);	/* resubmit */
   2265       1.1        pk 			return (XD_ERR_AOK);	/* recovered! */
   2266       1.1        pk 		}
   2267      1.15  drochner #endif
   2268       1.1        pk 	}
   2269       1.1        pk 
   2270       1.1        pk 	/*
   2271       1.1        pk 	 * it isn't a bad144 sector, must be real error! see if we can retry
   2272       1.1        pk 	 * it?
   2273       1.1        pk 	 */
   2274       1.1        pk 	if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
   2275       1.1        pk 		xdc_perror(iorq, iopb, 1);	/* inform of error state
   2276       1.1        pk 						 * change */
   2277       1.1        pk 	iorq->lasterror = errno;
   2278       1.1        pk 
   2279       1.1        pk 	if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
   2280       1.1        pk 	    && iorq->tries < XDC_MAXTRIES) {	/* retry? */
   2281       1.1        pk 		iorq->tries++;
   2282       1.1        pk 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
   2283       1.1        pk 		XDC_HWAIT(xdcsc, rqno);
   2284       1.1        pk 		xdc_start(xdcsc, 1);	/* restart */
   2285       1.1        pk 		return (XD_ERR_AOK);	/* recovered! */
   2286       1.1        pk 	}
   2287       1.1        pk 
   2288       1.1        pk 	/* failed to recover from this error */
   2289       1.1        pk 	return (XD_ERR_FAIL);
   2290       1.1        pk }
   2291       1.1        pk 
   2292       1.1        pk /*
   2293       1.1        pk  * xdc_tick: make sure xd is still alive and ticking (err, kicking).
   2294       1.1        pk  */
   2295       1.1        pk void
   2296       1.1        pk xdc_tick(arg)
   2297       1.1        pk 	void   *arg;
   2298       1.1        pk 
   2299       1.1        pk {
   2300       1.1        pk 	struct xdc_softc *xdcsc = arg;
   2301       1.1        pk 	int     lcv, s, reset = 0;
   2302       1.1        pk #ifdef XDC_DIAG
   2303      1.57   tsutsui 	int     nwait, nrun, nfree, ndone, whd = 0;
   2304       1.1        pk 	u_char  fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
   2305       1.1        pk 	s = splbio();
   2306      1.57   tsutsui 	nwait = xdcsc->nwait;
   2307      1.57   tsutsui 	nrun = xdcsc->nrun;
   2308      1.57   tsutsui 	nfree = xdcsc->nfree;
   2309      1.57   tsutsui 	ndone = xdcsc->ndone;
   2310       1.1        pk 	bcopy(xdcsc->waitq, wqc, sizeof(wqc));
   2311       1.1        pk 	bcopy(xdcsc->freereq, fqc, sizeof(fqc));
   2312       1.1        pk 	splx(s);
   2313      1.57   tsutsui 	if (nwait + nrun + nfree + ndone != XDC_MAXIOPB) {
   2314       1.1        pk 		printf("%s: diag: IOPB miscount (got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
   2315      1.57   tsutsui 		    xdcsc->sc_dev.dv_xname, nwait, nfree, nrun, ndone,
   2316      1.57   tsutsui 		    XDC_MAXIOPB);
   2317       1.1        pk 		bzero(mark, sizeof(mark));
   2318       1.1        pk 		printf("FREE: ");
   2319      1.57   tsutsui 		for (lcv = nfree; lcv > 0; lcv--) {
   2320       1.1        pk 			printf("%d ", fqc[lcv - 1]);
   2321       1.1        pk 			mark[fqc[lcv - 1]] = 1;
   2322       1.1        pk 		}
   2323       1.1        pk 		printf("\nWAIT: ");
   2324      1.57   tsutsui 		lcv = nwait;
   2325       1.1        pk 		while (lcv > 0) {
   2326       1.1        pk 			printf("%d ", wqc[whd]);
   2327       1.1        pk 			mark[wqc[whd]] = 1;
   2328       1.1        pk 			whd = (whd + 1) % XDC_MAXIOPB;
   2329       1.1        pk 			lcv--;
   2330       1.1        pk 		}
   2331       1.1        pk 		printf("\n");
   2332       1.1        pk 		for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2333       1.1        pk 			if (mark[lcv] == 0)
   2334       1.1        pk 				printf("MARK: running %d: mode %d done %d errs %d errno 0x%x ttl %d buf %p\n",
   2335       1.1        pk 				lcv, xdcsc->reqs[lcv].mode,
   2336       1.1        pk 				xdcsc->iopbase[lcv].done,
   2337       1.1        pk 				xdcsc->iopbase[lcv].errs,
   2338       1.1        pk 				xdcsc->iopbase[lcv].errno,
   2339       1.1        pk 				xdcsc->reqs[lcv].ttl, xdcsc->reqs[lcv].buf);
   2340       1.1        pk 		}
   2341       1.1        pk 	} else
   2342      1.57   tsutsui 		if (ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
   2343       1.1        pk 			printf("%s: diag: lots of done jobs (%d)\n",
   2344      1.57   tsutsui 				xdcsc->sc_dev.dv_xname, ndone);
   2345       1.1        pk 
   2346       1.1        pk #endif
   2347       1.1        pk #ifdef XDC_DEBUG
   2348       1.1        pk 	printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
   2349       1.1        pk 		xdcsc->sc_dev.dv_xname,
   2350       1.1        pk 		xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
   2351       1.1        pk 		xdcsc->ndone);
   2352       1.1        pk 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2353       1.1        pk 		if (xdcsc->reqs[lcv].mode)
   2354       1.1        pk 		  printf("running %d: mode %d done %d errs %d errno 0x%x\n",
   2355       1.1        pk 			 lcv,
   2356       1.1        pk 			 xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
   2357       1.1        pk 			 xdcsc->iopbase[lcv].errs, xdcsc->iopbase[lcv].errno);
   2358       1.1        pk 	}
   2359       1.1        pk #endif
   2360       1.1        pk 
   2361       1.1        pk 	/* reduce ttl for each request if one goes to zero, reset xdc */
   2362       1.1        pk 	s = splbio();
   2363       1.1        pk 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2364       1.1        pk 		if (xdcsc->reqs[lcv].mode == 0 ||
   2365       1.1        pk 		    XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
   2366       1.1        pk 			continue;
   2367       1.1        pk 		xdcsc->reqs[lcv].ttl--;
   2368       1.1        pk 		if (xdcsc->reqs[lcv].ttl == 0)
   2369       1.1        pk 			reset = 1;
   2370       1.1        pk 	}
   2371       1.1        pk 	if (reset) {
   2372       1.1        pk 		printf("%s: watchdog timeout\n", xdcsc->sc_dev.dv_xname);
   2373       1.1        pk 		xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
   2374       1.1        pk 	}
   2375       1.1        pk 	splx(s);
   2376       1.1        pk 
   2377       1.1        pk 	/* until next time */
   2378       1.1        pk 
   2379      1.19   thorpej 	callout_reset(&xdcsc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdcsc);
   2380       1.1        pk }
   2381       1.1        pk 
   2382       1.1        pk /*
   2383       1.1        pk  * xdc_ioctlcmd: this function provides a user level interface to the
   2384       1.1        pk  * controller via ioctl.   this allows "format" programs to be written
   2385       1.1        pk  * in user code, and is also useful for some debugging.   we return
   2386       1.1        pk  * an error code.   called at user priority.
   2387       1.1        pk  */
   2388       1.1        pk int
   2389       1.1        pk xdc_ioctlcmd(xd, dev, xio)
   2390       1.1        pk 	struct xd_softc *xd;
   2391       1.1        pk 	dev_t   dev;
   2392       1.1        pk 	struct xd_iocmd *xio;
   2393       1.1        pk 
   2394       1.1        pk {
   2395       1.3        pk 	int     s, rqno, dummy;
   2396       1.1        pk 	caddr_t dvmabuf = NULL, buf = NULL;
   2397       1.1        pk 	struct xdc_softc *xdcsc;
   2398       1.3        pk 	int			rseg, error;
   2399       1.3        pk 	bus_dma_segment_t	seg;
   2400       1.1        pk 
   2401       1.1        pk 	/* check sanity of requested command */
   2402       1.1        pk 
   2403       1.1        pk 	switch (xio->cmd) {
   2404       1.1        pk 
   2405       1.1        pk 	case XDCMD_NOP:	/* no op: everything should be zero */
   2406       1.1        pk 		if (xio->subfn || xio->dptr || xio->dlen ||
   2407       1.1        pk 		    xio->block || xio->sectcnt)
   2408       1.1        pk 			return (EINVAL);
   2409       1.1        pk 		break;
   2410       1.1        pk 
   2411       1.1        pk 	case XDCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   2412       1.1        pk 	case XDCMD_WR:
   2413       1.1        pk 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   2414       1.1        pk 		    xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
   2415       1.1        pk 			return (EINVAL);
   2416       1.1        pk 		break;
   2417       1.1        pk 
   2418       1.1        pk 	case XDCMD_SK:		/* seek: doesn't seem useful to export this */
   2419       1.1        pk 		return (EINVAL);
   2420       1.1        pk 
   2421       1.1        pk 	case XDCMD_WRP:	/* write parameters */
   2422       1.1        pk 		return (EINVAL);/* not useful, except maybe drive
   2423       1.1        pk 				 * parameters... but drive parameters should
   2424       1.1        pk 				 * go via disklabel changes */
   2425       1.1        pk 
   2426       1.1        pk 	case XDCMD_RDP:	/* read parameters */
   2427       1.1        pk 		if (xio->subfn != XDFUN_DRV ||
   2428       1.1        pk 		    xio->dlen || xio->block || xio->dptr)
   2429       1.1        pk 			return (EINVAL);	/* allow read drive params to
   2430       1.1        pk 						 * get hw_spt */
   2431       1.1        pk 		xio->sectcnt = xd->hw_spt;	/* we already know the answer */
   2432       1.1        pk 		return (0);
   2433       1.1        pk 		break;
   2434       1.1        pk 
   2435       1.1        pk 	case XDCMD_XRD:	/* extended read/write */
   2436       1.1        pk 	case XDCMD_XWR:
   2437       1.1        pk 
   2438       1.1        pk 		switch (xio->subfn) {
   2439       1.1        pk 
   2440       1.1        pk 		case XDFUN_THD:/* track headers */
   2441       1.1        pk 			if (xio->sectcnt != xd->hw_spt ||
   2442       1.1        pk 			    (xio->block % xd->nsect) != 0 ||
   2443       1.1        pk 			    xio->dlen != XD_IOCMD_HSZ * xd->hw_spt ||
   2444       1.1        pk 			    xio->dptr == NULL)
   2445       1.1        pk 				return (EINVAL);
   2446       1.1        pk 			xio->sectcnt = 0;
   2447       1.1        pk 			break;
   2448       1.1        pk 
   2449       1.1        pk 		case XDFUN_FMT:/* NOTE: also XDFUN_VFY */
   2450       1.1        pk 			if (xio->cmd == XDCMD_XRD)
   2451       1.1        pk 				return (EINVAL);	/* no XDFUN_VFY */
   2452       1.1        pk 			if (xio->sectcnt || xio->dlen ||
   2453       1.1        pk 			    (xio->block % xd->nsect) != 0 || xio->dptr)
   2454       1.1        pk 				return (EINVAL);
   2455       1.1        pk 			break;
   2456       1.1        pk 
   2457       1.1        pk 		case XDFUN_HDR:/* header, header verify, data, data ECC */
   2458       1.1        pk 			return (EINVAL);	/* not yet */
   2459       1.1        pk 
   2460       1.1        pk 		case XDFUN_DM:	/* defect map */
   2461       1.1        pk 		case XDFUN_DMX:/* defect map (alternate location) */
   2462       1.1        pk 			if (xio->sectcnt || xio->dlen != XD_IOCMD_DMSZ ||
   2463       1.1        pk 			    (xio->block % xd->nsect) != 0 || xio->dptr == NULL)
   2464       1.1        pk 				return (EINVAL);
   2465       1.1        pk 			break;
   2466       1.1        pk 
   2467       1.1        pk 		default:
   2468       1.1        pk 			return (EINVAL);
   2469       1.1        pk 		}
   2470       1.1        pk 		break;
   2471       1.1        pk 
   2472       1.1        pk 	case XDCMD_TST:	/* diagnostics */
   2473       1.1        pk 		return (EINVAL);
   2474       1.1        pk 
   2475       1.1        pk 	default:
   2476       1.1        pk 		return (EINVAL);/* ??? */
   2477       1.1        pk 	}
   2478       1.1        pk 
   2479       1.3        pk 	xdcsc = xd->parent;
   2480       1.3        pk 
   2481       1.1        pk 	/* create DVMA buffer for request if needed */
   2482       1.3        pk 	if (xio->dlen) {
   2483      1.52       mrg 		bus_addr_t busbuf;
   2484      1.52       mrg 
   2485      1.21        pk 		if ((error = xd_dmamem_alloc(xdcsc->dmatag, xdcsc->auxmap,
   2486      1.21        pk 					     &seg, &rseg,
   2487      1.21        pk 					     xio->dlen, &buf,
   2488      1.52       mrg 					     &busbuf)) != 0) {
   2489       1.3        pk 			return (error);
   2490      1.21        pk 		}
   2491      1.53       mrg 		dvmabuf = (caddr_t)(u_long)BUS_ADDR_PADDR(busbuf);
   2492       1.1        pk 
   2493       1.1        pk 		if (xio->cmd == XDCMD_WR || xio->cmd == XDCMD_XWR) {
   2494       1.3        pk 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
   2495       1.3        pk 				bus_dmamem_unmap(xdcsc->dmatag, buf, xio->dlen);
   2496       1.3        pk 				bus_dmamem_free(xdcsc->dmatag, &seg, rseg);
   2497       1.3        pk 				return (error);
   2498       1.1        pk 			}
   2499       1.1        pk 		}
   2500       1.1        pk 	}
   2501       1.3        pk 
   2502       1.1        pk 	/* do it! */
   2503       1.1        pk 
   2504       1.3        pk 	error = 0;
   2505       1.1        pk 	s = splbio();
   2506       1.1        pk 	rqno = xdc_cmd(xdcsc, xio->cmd, xio->subfn, xd->xd_drive, xio->block,
   2507       1.1        pk 	    xio->sectcnt, dvmabuf, XD_SUB_WAIT);
   2508       1.1        pk 	if (rqno == XD_ERR_FAIL) {
   2509       1.3        pk 		error = EIO;
   2510       1.1        pk 		goto done;
   2511       1.1        pk 	}
   2512       1.1        pk 	xio->errno = xdcsc->reqs[rqno].errno;
   2513       1.1        pk 	xio->tries = xdcsc->reqs[rqno].tries;
   2514       1.1        pk 	XDC_DONE(xdcsc, rqno, dummy);
   2515       1.1        pk 
   2516       1.1        pk 	if (xio->cmd == XDCMD_RD || xio->cmd == XDCMD_XRD)
   2517       1.3        pk 		error = copyout(buf, xio->dptr, xio->dlen);
   2518       1.1        pk 
   2519       1.1        pk done:
   2520       1.1        pk 	splx(s);
   2521       1.3        pk 	if (dvmabuf) {
   2522      1.21        pk 		xd_dmamem_free(xdcsc->dmatag, xdcsc->auxmap, &seg, rseg,
   2523      1.21        pk 				xio->dlen, buf);
   2524       1.3        pk 	}
   2525       1.3        pk 	return (error);
   2526       1.1        pk }
   2527       1.1        pk 
   2528       1.1        pk /*
   2529       1.1        pk  * xdc_e2str: convert error code number into an error string
   2530       1.1        pk  */
   2531      1.57   tsutsui const char *
   2532       1.1        pk xdc_e2str(no)
   2533       1.1        pk 	int     no;
   2534       1.1        pk {
   2535       1.1        pk 	switch (no) {
   2536       1.1        pk 	case XD_ERR_FAIL:
   2537       1.1        pk 		return ("Software fatal error");
   2538       1.1        pk 	case XD_ERR_AOK:
   2539       1.1        pk 		return ("Successful completion");
   2540       1.1        pk 	case XD_ERR_ICYL:
   2541       1.1        pk 		return ("Illegal cylinder address");
   2542       1.1        pk 	case XD_ERR_IHD:
   2543       1.1        pk 		return ("Illegal head address");
   2544       1.1        pk 	case XD_ERR_ISEC:
   2545       1.1        pk 		return ("Illgal sector address");
   2546       1.1        pk 	case XD_ERR_CZER:
   2547       1.1        pk 		return ("Count zero");
   2548       1.1        pk 	case XD_ERR_UIMP:
   2549       1.1        pk 		return ("Unimplemented command");
   2550       1.1        pk 	case XD_ERR_IF1:
   2551       1.1        pk 		return ("Illegal field length 1");
   2552       1.1        pk 	case XD_ERR_IF2:
   2553       1.1        pk 		return ("Illegal field length 2");
   2554       1.1        pk 	case XD_ERR_IF3:
   2555       1.1        pk 		return ("Illegal field length 3");
   2556       1.1        pk 	case XD_ERR_IF4:
   2557       1.1        pk 		return ("Illegal field length 4");
   2558       1.1        pk 	case XD_ERR_IF5:
   2559       1.1        pk 		return ("Illegal field length 5");
   2560       1.1        pk 	case XD_ERR_IF6:
   2561       1.1        pk 		return ("Illegal field length 6");
   2562       1.1        pk 	case XD_ERR_IF7:
   2563       1.1        pk 		return ("Illegal field length 7");
   2564       1.1        pk 	case XD_ERR_ISG:
   2565       1.1        pk 		return ("Illegal scatter/gather length");
   2566       1.1        pk 	case XD_ERR_ISPT:
   2567       1.1        pk 		return ("Not enough sectors per track");
   2568       1.1        pk 	case XD_ERR_ALGN:
   2569       1.1        pk 		return ("Next IOPB address alignment error");
   2570       1.1        pk 	case XD_ERR_SGAL:
   2571       1.1        pk 		return ("Scatter/gather address alignment error");
   2572       1.1        pk 	case XD_ERR_SGEC:
   2573       1.1        pk 		return ("Scatter/gather with auto-ECC");
   2574       1.1        pk 	case XD_ERR_SECC:
   2575       1.1        pk 		return ("Soft ECC corrected");
   2576       1.1        pk 	case XD_ERR_SIGN:
   2577       1.1        pk 		return ("ECC ignored");
   2578       1.1        pk 	case XD_ERR_ASEK:
   2579       1.1        pk 		return ("Auto-seek retry recovered");
   2580       1.1        pk 	case XD_ERR_RTRY:
   2581       1.1        pk 		return ("Soft retry recovered");
   2582       1.1        pk 	case XD_ERR_HECC:
   2583       1.1        pk 		return ("Hard data ECC");
   2584       1.1        pk 	case XD_ERR_NHDR:
   2585       1.1        pk 		return ("Header not found");
   2586       1.1        pk 	case XD_ERR_NRDY:
   2587       1.1        pk 		return ("Drive not ready");
   2588       1.1        pk 	case XD_ERR_TOUT:
   2589       1.1        pk 		return ("Operation timeout");
   2590       1.1        pk 	case XD_ERR_VTIM:
   2591       1.1        pk 		return ("VMEDMA timeout");
   2592       1.1        pk 	case XD_ERR_DSEQ:
   2593       1.1        pk 		return ("Disk sequencer error");
   2594       1.1        pk 	case XD_ERR_HDEC:
   2595       1.1        pk 		return ("Header ECC error");
   2596       1.1        pk 	case XD_ERR_RVFY:
   2597       1.1        pk 		return ("Read verify");
   2598       1.1        pk 	case XD_ERR_VFER:
   2599       1.1        pk 		return ("Fatail VMEDMA error");
   2600       1.1        pk 	case XD_ERR_VBUS:
   2601       1.1        pk 		return ("VMEbus error");
   2602       1.1        pk 	case XD_ERR_DFLT:
   2603       1.1        pk 		return ("Drive faulted");
   2604       1.1        pk 	case XD_ERR_HECY:
   2605       1.1        pk 		return ("Header error/cyliner");
   2606       1.1        pk 	case XD_ERR_HEHD:
   2607       1.1        pk 		return ("Header error/head");
   2608       1.1        pk 	case XD_ERR_NOCY:
   2609       1.1        pk 		return ("Drive not on-cylinder");
   2610       1.1        pk 	case XD_ERR_SEEK:
   2611       1.1        pk 		return ("Seek error");
   2612       1.1        pk 	case XD_ERR_ILSS:
   2613       1.1        pk 		return ("Illegal sector size");
   2614       1.1        pk 	case XD_ERR_SEC:
   2615       1.1        pk 		return ("Soft ECC");
   2616       1.1        pk 	case XD_ERR_WPER:
   2617       1.1        pk 		return ("Write-protect error");
   2618       1.1        pk 	case XD_ERR_IRAM:
   2619       1.1        pk 		return ("IRAM self test failure");
   2620       1.1        pk 	case XD_ERR_MT3:
   2621       1.1        pk 		return ("Maintenance test 3 failure (DSKCEL RAM)");
   2622       1.1        pk 	case XD_ERR_MT4:
   2623       1.1        pk 		return ("Maintenance test 4 failure (header shift reg)");
   2624       1.1        pk 	case XD_ERR_MT5:
   2625       1.1        pk 		return ("Maintenance test 5 failure (VMEDMA regs)");
   2626       1.1        pk 	case XD_ERR_MT6:
   2627       1.1        pk 		return ("Maintenance test 6 failure (REGCEL chip)");
   2628       1.1        pk 	case XD_ERR_MT7:
   2629       1.1        pk 		return ("Maintenance test 7 failure (buffer parity)");
   2630       1.1        pk 	case XD_ERR_MT8:
   2631       1.1        pk 		return ("Maintenance test 8 failure (disk FIFO)");
   2632       1.1        pk 	case XD_ERR_IOCK:
   2633       1.1        pk 		return ("IOPB checksum miscompare");
   2634       1.1        pk 	case XD_ERR_IODM:
   2635       1.1        pk 		return ("IOPB DMA fatal");
   2636       1.1        pk 	case XD_ERR_IOAL:
   2637       1.1        pk 		return ("IOPB address alignment error");
   2638       1.1        pk 	case XD_ERR_FIRM:
   2639       1.1        pk 		return ("Firmware error");
   2640       1.1        pk 	case XD_ERR_MMOD:
   2641       1.1        pk 		return ("Illegal maintenance mode test number");
   2642       1.1        pk 	case XD_ERR_ACFL:
   2643       1.1        pk 		return ("ACFAIL asserted");
   2644       1.1        pk 	default:
   2645       1.1        pk 		return ("Unknown error");
   2646       1.1        pk 	}
   2647       1.1        pk }
   2648