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