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