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