Home | History | Annotate | Line # | Download | only in dev
xy.c revision 1.35.6.2
      1  1.35.6.2  nathanw /*	$NetBSD: xy.c,v 1.35.6.2 2002/08/01 02:43:55 nathanw Exp $	*/
      2  1.35.6.2  nathanw 
      3  1.35.6.2  nathanw /*
      4  1.35.6.2  nathanw  *
      5  1.35.6.2  nathanw  * Copyright (c) 1995 Charles D. Cranor
      6  1.35.6.2  nathanw  * All rights reserved.
      7  1.35.6.2  nathanw  *
      8  1.35.6.2  nathanw  * Redistribution and use in source and binary forms, with or without
      9  1.35.6.2  nathanw  * modification, are permitted provided that the following conditions
     10  1.35.6.2  nathanw  * are met:
     11  1.35.6.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     12  1.35.6.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     13  1.35.6.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.35.6.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     15  1.35.6.2  nathanw  *    documentation and/or other materials provided with the distribution.
     16  1.35.6.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     17  1.35.6.2  nathanw  *    must display the following acknowledgement:
     18  1.35.6.2  nathanw  *      This product includes software developed by Charles D. Cranor.
     19  1.35.6.2  nathanw  * 4. The name of the author may not be used to endorse or promote products
     20  1.35.6.2  nathanw  *    derived from this software without specific prior written permission.
     21  1.35.6.2  nathanw  *
     22  1.35.6.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.35.6.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.35.6.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.35.6.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.35.6.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.35.6.2  nathanw  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.35.6.2  nathanw  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.35.6.2  nathanw  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.35.6.2  nathanw  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.35.6.2  nathanw  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.35.6.2  nathanw  */
     33  1.35.6.2  nathanw 
     34  1.35.6.2  nathanw /*
     35  1.35.6.2  nathanw  *
     36  1.35.6.2  nathanw  * x y . c   x y l o g i c s   4 5 0 / 4 5 1   s m d   d r i v e r
     37  1.35.6.2  nathanw  *
     38  1.35.6.2  nathanw  * author: Chuck Cranor <chuck (at) ccrc.wustl.edu>
     39  1.35.6.2  nathanw  * id: &Id: xy.c,v 1.1 1995/09/25 20:35:14 chuck Exp &
     40  1.35.6.2  nathanw  * started: 14-Sep-95
     41  1.35.6.2  nathanw  * references: [1] Xylogics Model 753 User's Manual
     42  1.35.6.2  nathanw  *                 part number: 166-753-001, Revision B, May 21, 1988.
     43  1.35.6.2  nathanw  *                 "Your Partner For Performance"
     44  1.35.6.2  nathanw  *             [2] other NetBSD disk device drivers
     45  1.35.6.2  nathanw  *	       [3] Xylogics Model 450 User's Manual
     46  1.35.6.2  nathanw  *		   part number: 166-017-001, Revision B, 1983.
     47  1.35.6.2  nathanw  *	       [4] Addendum to Xylogics Model 450 Disk Controller User's
     48  1.35.6.2  nathanw  *			Manual, Jan. 1985.
     49  1.35.6.2  nathanw  *	       [5] The 451 Controller, Rev. B3, September 2, 1986.
     50  1.35.6.2  nathanw  *	       [6] David Jones <dej (at) achilles.net>'s unfinished 450/451 driver
     51  1.35.6.2  nathanw  *
     52  1.35.6.2  nathanw  */
     53  1.35.6.2  nathanw 
     54  1.35.6.2  nathanw #undef XYC_DEBUG		/* full debug */
     55  1.35.6.2  nathanw #undef XYC_DIAG			/* extra sanity checks */
     56  1.35.6.2  nathanw #if defined(DIAGNOSTIC) && !defined(XYC_DIAG)
     57  1.35.6.2  nathanw #define XYC_DIAG		/* link in with master DIAG option */
     58  1.35.6.2  nathanw #endif
     59  1.35.6.2  nathanw 
     60  1.35.6.2  nathanw #include <sys/param.h>
     61  1.35.6.2  nathanw #include <sys/proc.h>
     62  1.35.6.2  nathanw #include <sys/systm.h>
     63  1.35.6.2  nathanw #include <sys/kernel.h>
     64  1.35.6.2  nathanw #include <sys/file.h>
     65  1.35.6.2  nathanw #include <sys/stat.h>
     66  1.35.6.2  nathanw #include <sys/ioctl.h>
     67  1.35.6.2  nathanw #include <sys/buf.h>
     68  1.35.6.2  nathanw #include <sys/uio.h>
     69  1.35.6.2  nathanw #include <sys/malloc.h>
     70  1.35.6.2  nathanw #include <sys/device.h>
     71  1.35.6.2  nathanw #include <sys/disklabel.h>
     72  1.35.6.2  nathanw #include <sys/disk.h>
     73  1.35.6.2  nathanw #include <sys/syslog.h>
     74  1.35.6.2  nathanw #include <sys/dkbad.h>
     75  1.35.6.2  nathanw #include <sys/conf.h>
     76  1.35.6.2  nathanw 
     77  1.35.6.2  nathanw #include <uvm/uvm_extern.h>
     78  1.35.6.2  nathanw 
     79  1.35.6.2  nathanw #include <dev/sun/disklabel.h>
     80  1.35.6.2  nathanw 
     81  1.35.6.2  nathanw #include <machine/autoconf.h>
     82  1.35.6.2  nathanw #include <machine/dvma.h>
     83  1.35.6.2  nathanw 
     84  1.35.6.2  nathanw #include <sun3/dev/xyreg.h>
     85  1.35.6.2  nathanw #include <sun3/dev/xyvar.h>
     86  1.35.6.2  nathanw #include <sun3/dev/xio.h>
     87  1.35.6.2  nathanw 
     88  1.35.6.2  nathanw #include "locators.h"
     89  1.35.6.2  nathanw 
     90  1.35.6.2  nathanw /*
     91  1.35.6.2  nathanw  * Print a complaint when no xy children were specified
     92  1.35.6.2  nathanw  * in the config file.  Better than a link error...
     93  1.35.6.2  nathanw  *
     94  1.35.6.2  nathanw  * XXX: Some folks say this driver should be split in two,
     95  1.35.6.2  nathanw  * but that seems pointless with ONLY one type of child.
     96  1.35.6.2  nathanw  */
     97  1.35.6.2  nathanw #include "xy.h"
     98  1.35.6.2  nathanw #if NXY == 0
     99  1.35.6.2  nathanw #error "xyc but no xy?"
    100  1.35.6.2  nathanw #endif
    101  1.35.6.2  nathanw 
    102  1.35.6.2  nathanw /*
    103  1.35.6.2  nathanw  * macros
    104  1.35.6.2  nathanw  */
    105  1.35.6.2  nathanw 
    106  1.35.6.2  nathanw /*
    107  1.35.6.2  nathanw  * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
    108  1.35.6.2  nathanw  */
    109  1.35.6.2  nathanw #define XYC_GO(XYC, ADDR) { \
    110  1.35.6.2  nathanw 	(XYC)->xyc_addr_lo = ((ADDR) & 0xff); \
    111  1.35.6.2  nathanw 	(ADDR) = ((ADDR) >> 8); \
    112  1.35.6.2  nathanw 	(XYC)->xyc_addr_hi = ((ADDR) & 0xff); \
    113  1.35.6.2  nathanw 	(ADDR) = ((ADDR) >> 8); \
    114  1.35.6.2  nathanw 	(XYC)->xyc_reloc_lo = ((ADDR) & 0xff); \
    115  1.35.6.2  nathanw 	(ADDR) = ((ADDR) >> 8); \
    116  1.35.6.2  nathanw 	(XYC)->xyc_reloc_hi = (ADDR); \
    117  1.35.6.2  nathanw 	(XYC)->xyc_csr = XYC_GBSY; /* go! */ \
    118  1.35.6.2  nathanw }
    119  1.35.6.2  nathanw 
    120  1.35.6.2  nathanw /*
    121  1.35.6.2  nathanw  * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
    122  1.35.6.2  nathanw  */
    123  1.35.6.2  nathanw 
    124  1.35.6.2  nathanw #define XYC_DONE(SC,ER) { \
    125  1.35.6.2  nathanw 	if ((ER) == XY_ERR_AOK) { \
    126  1.35.6.2  nathanw 		(ER) = (SC)->ciorq->errno; \
    127  1.35.6.2  nathanw 		(SC)->ciorq->mode = XY_SUB_FREE; \
    128  1.35.6.2  nathanw 		wakeup((SC)->ciorq); \
    129  1.35.6.2  nathanw 	} \
    130  1.35.6.2  nathanw 	}
    131  1.35.6.2  nathanw 
    132  1.35.6.2  nathanw /*
    133  1.35.6.2  nathanw  * XYC_ADVANCE: advance iorq's pointers by a number of sectors
    134  1.35.6.2  nathanw  */
    135  1.35.6.2  nathanw 
    136  1.35.6.2  nathanw #define XYC_ADVANCE(IORQ, N) { \
    137  1.35.6.2  nathanw 	if (N) { \
    138  1.35.6.2  nathanw 		(IORQ)->sectcnt -= (N); \
    139  1.35.6.2  nathanw 		(IORQ)->blockno += (N); \
    140  1.35.6.2  nathanw 		(IORQ)->dbuf += ((N)*XYFM_BPS); \
    141  1.35.6.2  nathanw 	} \
    142  1.35.6.2  nathanw }
    143  1.35.6.2  nathanw 
    144  1.35.6.2  nathanw /*
    145  1.35.6.2  nathanw  * note - addresses you can sleep on:
    146  1.35.6.2  nathanw  *   [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
    147  1.35.6.2  nathanw  *   [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
    148  1.35.6.2  nathanw  */
    149  1.35.6.2  nathanw 
    150  1.35.6.2  nathanw 
    151  1.35.6.2  nathanw /*
    152  1.35.6.2  nathanw  * function prototypes
    153  1.35.6.2  nathanw  * "xyc_*" functions are internal, all others are external interfaces
    154  1.35.6.2  nathanw  */
    155  1.35.6.2  nathanw 
    156  1.35.6.2  nathanw /* internals */
    157  1.35.6.2  nathanw struct xy_iopb *xyc_chain __P((struct xyc_softc *, struct xy_iorq *));
    158  1.35.6.2  nathanw int	xyc_cmd __P((struct xyc_softc *, int, int, int, int, int, char *, int));
    159  1.35.6.2  nathanw char   *xyc_e2str __P((int));
    160  1.35.6.2  nathanw int	xyc_entoact __P((int));
    161  1.35.6.2  nathanw int	xyc_error __P((struct xyc_softc *, struct xy_iorq *,
    162  1.35.6.2  nathanw 		   struct xy_iopb *, int));
    163  1.35.6.2  nathanw int	xyc_ioctlcmd __P((struct xy_softc *, dev_t dev, struct xd_iocmd *));
    164  1.35.6.2  nathanw void	xyc_perror __P((struct xy_iorq *, struct xy_iopb *, int));
    165  1.35.6.2  nathanw int	xyc_piodriver __P((struct xyc_softc *, struct xy_iorq *));
    166  1.35.6.2  nathanw int	xyc_remove_iorq __P((struct xyc_softc *));
    167  1.35.6.2  nathanw int	xyc_reset __P((struct xyc_softc *, int, struct xy_iorq *, int,
    168  1.35.6.2  nathanw 			struct xy_softc *));
    169  1.35.6.2  nathanw inline void xyc_rqinit __P((struct xy_iorq *, struct xyc_softc *,
    170  1.35.6.2  nathanw 			    struct xy_softc *, int, u_long, int,
    171  1.35.6.2  nathanw 			    caddr_t, struct buf *));
    172  1.35.6.2  nathanw void	xyc_rqtopb __P((struct xy_iorq *, struct xy_iopb *, int, int));
    173  1.35.6.2  nathanw void	xyc_start __P((struct xyc_softc *, struct xy_iorq *));
    174  1.35.6.2  nathanw int	xyc_startbuf __P((struct xyc_softc *, struct xy_softc *, struct buf *));
    175  1.35.6.2  nathanw int	xyc_submit_iorq __P((struct xyc_softc *, struct xy_iorq *, int));
    176  1.35.6.2  nathanw void	xyc_tick __P((void *));
    177  1.35.6.2  nathanw int	xyc_unbusy __P((struct xyc *, int));
    178  1.35.6.2  nathanw void	xyc_xyreset __P((struct xyc_softc *, struct xy_softc *));
    179  1.35.6.2  nathanw 
    180  1.35.6.2  nathanw /* machine interrupt hook */
    181  1.35.6.2  nathanw int	xycintr __P((void *));
    182  1.35.6.2  nathanw 
    183  1.35.6.2  nathanw /* bdevsw, cdevsw */
    184  1.35.6.2  nathanw bdev_decl(xy);
    185  1.35.6.2  nathanw cdev_decl(xy);
    186  1.35.6.2  nathanw 
    187  1.35.6.2  nathanw /* autoconf */
    188  1.35.6.2  nathanw static int	xycmatch __P((struct device *, struct cfdata *, void *));
    189  1.35.6.2  nathanw static void	xycattach __P((struct device *, struct device *, void *));
    190  1.35.6.2  nathanw static int  xyc_print __P((void *, const char *name));
    191  1.35.6.2  nathanw 
    192  1.35.6.2  nathanw static int	xymatch __P((struct device *, struct cfdata *, void *));
    193  1.35.6.2  nathanw static void	xyattach __P((struct device *, struct device *, void *));
    194  1.35.6.2  nathanw static void xy_init __P((struct xy_softc *));
    195  1.35.6.2  nathanw 
    196  1.35.6.2  nathanw static	void xydummystrat __P((struct buf *));
    197  1.35.6.2  nathanw int	xygetdisklabel __P((struct xy_softc *, void *));
    198  1.35.6.2  nathanw 
    199  1.35.6.2  nathanw /*
    200  1.35.6.2  nathanw  * cfattach's: device driver interface to autoconfig
    201  1.35.6.2  nathanw  */
    202  1.35.6.2  nathanw 
    203  1.35.6.2  nathanw struct cfattach xyc_ca = {
    204  1.35.6.2  nathanw 	sizeof(struct xyc_softc), xycmatch, xycattach
    205  1.35.6.2  nathanw };
    206  1.35.6.2  nathanw 
    207  1.35.6.2  nathanw struct cfattach xy_ca = {
    208  1.35.6.2  nathanw 	sizeof(struct xy_softc), xymatch, xyattach
    209  1.35.6.2  nathanw };
    210  1.35.6.2  nathanw 
    211  1.35.6.2  nathanw extern struct cfdriver xy_cd;
    212  1.35.6.2  nathanw 
    213  1.35.6.2  nathanw struct xyc_attach_args {	/* this is the "aux" args to xyattach */
    214  1.35.6.2  nathanw 	int	driveno;	/* unit number */
    215  1.35.6.2  nathanw };
    216  1.35.6.2  nathanw 
    217  1.35.6.2  nathanw /*
    218  1.35.6.2  nathanw  * dkdriver
    219  1.35.6.2  nathanw  */
    220  1.35.6.2  nathanw 
    221  1.35.6.2  nathanw struct dkdriver xydkdriver = { xystrategy };
    222  1.35.6.2  nathanw 
    223  1.35.6.2  nathanw /*
    224  1.35.6.2  nathanw  * start: disk label fix code (XXX)
    225  1.35.6.2  nathanw  */
    226  1.35.6.2  nathanw 
    227  1.35.6.2  nathanw static void *xy_labeldata;
    228  1.35.6.2  nathanw 
    229  1.35.6.2  nathanw static void
    230  1.35.6.2  nathanw xydummystrat(bp)
    231  1.35.6.2  nathanw 	struct buf *bp;
    232  1.35.6.2  nathanw {
    233  1.35.6.2  nathanw 	if (bp->b_bcount != XYFM_BPS)
    234  1.35.6.2  nathanw 		panic("xydummystrat");
    235  1.35.6.2  nathanw 	memcpy(bp->b_data, xy_labeldata, XYFM_BPS);
    236  1.35.6.2  nathanw 	bp->b_flags |= B_DONE;
    237  1.35.6.2  nathanw 	bp->b_flags &= ~B_BUSY;
    238  1.35.6.2  nathanw }
    239  1.35.6.2  nathanw 
    240  1.35.6.2  nathanw int
    241  1.35.6.2  nathanw xygetdisklabel(xy, b)
    242  1.35.6.2  nathanw 	struct xy_softc *xy;
    243  1.35.6.2  nathanw 	void *b;
    244  1.35.6.2  nathanw {
    245  1.35.6.2  nathanw 	char *err;
    246  1.35.6.2  nathanw 	struct sun_disklabel *sdl;
    247  1.35.6.2  nathanw 
    248  1.35.6.2  nathanw 	/* We already have the label data in `b'; setup for dummy strategy */
    249  1.35.6.2  nathanw 	xy_labeldata = b;
    250  1.35.6.2  nathanw 
    251  1.35.6.2  nathanw 	/* Required parameter for readdisklabel() */
    252  1.35.6.2  nathanw 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
    253  1.35.6.2  nathanw 
    254  1.35.6.2  nathanw 	err = readdisklabel(MAKEDISKDEV(0, xy->sc_dev.dv_unit, RAW_PART),
    255  1.35.6.2  nathanw 					xydummystrat,
    256  1.35.6.2  nathanw 				xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
    257  1.35.6.2  nathanw 	if (err) {
    258  1.35.6.2  nathanw 		printf("%s: %s\n", xy->sc_dev.dv_xname, err);
    259  1.35.6.2  nathanw 		return(XY_ERR_FAIL);
    260  1.35.6.2  nathanw 	}
    261  1.35.6.2  nathanw 
    262  1.35.6.2  nathanw 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
    263  1.35.6.2  nathanw 	sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
    264  1.35.6.2  nathanw 	if (sdl->sl_magic == SUN_DKMAGIC)
    265  1.35.6.2  nathanw 		xy->pcyl = sdl->sl_pcyl;
    266  1.35.6.2  nathanw 	else {
    267  1.35.6.2  nathanw 		printf("%s: WARNING: no `pcyl' in disk label.\n",
    268  1.35.6.2  nathanw 			   xy->sc_dev.dv_xname);
    269  1.35.6.2  nathanw 		xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
    270  1.35.6.2  nathanw 			xy->sc_dk.dk_label->d_acylinders;
    271  1.35.6.2  nathanw 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
    272  1.35.6.2  nathanw 		xy->sc_dev.dv_xname, xy->pcyl);
    273  1.35.6.2  nathanw 	}
    274  1.35.6.2  nathanw 
    275  1.35.6.2  nathanw 	xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
    276  1.35.6.2  nathanw 	xy->acyl = xy->sc_dk.dk_label->d_acylinders;
    277  1.35.6.2  nathanw 	xy->nhead = xy->sc_dk.dk_label->d_ntracks;
    278  1.35.6.2  nathanw 	xy->nsect = xy->sc_dk.dk_label->d_nsectors;
    279  1.35.6.2  nathanw 	xy->sectpercyl = xy->nhead * xy->nsect;
    280  1.35.6.2  nathanw 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
    281  1.35.6.2  nathanw                                           	  * sun->bsd */
    282  1.35.6.2  nathanw 	return(XY_ERR_AOK);
    283  1.35.6.2  nathanw }
    284  1.35.6.2  nathanw 
    285  1.35.6.2  nathanw /*
    286  1.35.6.2  nathanw  * end: disk label fix code (XXX)
    287  1.35.6.2  nathanw  */
    288  1.35.6.2  nathanw 
    289  1.35.6.2  nathanw /*
    290  1.35.6.2  nathanw  * a u t o c o n f i g   f u n c t i o n s
    291  1.35.6.2  nathanw  */
    292  1.35.6.2  nathanw 
    293  1.35.6.2  nathanw /*
    294  1.35.6.2  nathanw  * xycmatch: determine if xyc is present or not.   we do a
    295  1.35.6.2  nathanw  * soft reset to detect the xyc.
    296  1.35.6.2  nathanw  */
    297  1.35.6.2  nathanw static int
    298  1.35.6.2  nathanw xycmatch(parent, cf, aux)
    299  1.35.6.2  nathanw 	struct device *parent;
    300  1.35.6.2  nathanw 	struct cfdata *cf;
    301  1.35.6.2  nathanw 	void *aux;
    302  1.35.6.2  nathanw {
    303  1.35.6.2  nathanw 	struct confargs *ca = aux;
    304  1.35.6.2  nathanw 
    305  1.35.6.2  nathanw 	/* No default VME address. */
    306  1.35.6.2  nathanw 	if (ca->ca_paddr == -1)
    307  1.35.6.2  nathanw 		return (0);
    308  1.35.6.2  nathanw 
    309  1.35.6.2  nathanw 	/* Make sure something is there... */
    310  1.35.6.2  nathanw 	if (bus_peek(ca->ca_bustype, ca->ca_paddr + 5, 1) == -1)
    311  1.35.6.2  nathanw 		return (0);
    312  1.35.6.2  nathanw 
    313  1.35.6.2  nathanw 	/* Default interrupt priority. */
    314  1.35.6.2  nathanw 	if (ca->ca_intpri == -1)
    315  1.35.6.2  nathanw 		ca->ca_intpri = 2;
    316  1.35.6.2  nathanw 
    317  1.35.6.2  nathanw 	return (1);
    318  1.35.6.2  nathanw }
    319  1.35.6.2  nathanw 
    320  1.35.6.2  nathanw /*
    321  1.35.6.2  nathanw  * xycattach: attach controller
    322  1.35.6.2  nathanw  */
    323  1.35.6.2  nathanw static void
    324  1.35.6.2  nathanw xycattach(parent, self, aux)
    325  1.35.6.2  nathanw 	struct device *parent, *self;
    326  1.35.6.2  nathanw 	void   *aux;
    327  1.35.6.2  nathanw {
    328  1.35.6.2  nathanw 	struct xyc_softc *xyc = (void *) self;
    329  1.35.6.2  nathanw 	struct confargs *ca = aux;
    330  1.35.6.2  nathanw 	struct xyc_attach_args xa;
    331  1.35.6.2  nathanw 	int     lcv, err, res, pbsz;
    332  1.35.6.2  nathanw 	void	*tmp, *tmp2;
    333  1.35.6.2  nathanw 	u_long	ultmp;
    334  1.35.6.2  nathanw 
    335  1.35.6.2  nathanw 	/* get addressing and intr level stuff from autoconfig and load it
    336  1.35.6.2  nathanw 	 * into our xyc_softc. */
    337  1.35.6.2  nathanw 
    338  1.35.6.2  nathanw 	xyc->xyc = (struct xyc *)
    339  1.35.6.2  nathanw 		bus_mapin(ca->ca_bustype, ca->ca_paddr, sizeof(struct xyc));
    340  1.35.6.2  nathanw 	xyc->bustype = ca->ca_bustype;
    341  1.35.6.2  nathanw 	xyc->ipl     = ca->ca_intpri;
    342  1.35.6.2  nathanw 	xyc->vector  = ca->ca_intvec;
    343  1.35.6.2  nathanw 	xyc->no_ols = 0; /* XXX should be from config */
    344  1.35.6.2  nathanw 
    345  1.35.6.2  nathanw 	for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
    346  1.35.6.2  nathanw 		xyc->sc_drives[lcv] = (struct xy_softc *) 0;
    347  1.35.6.2  nathanw 
    348  1.35.6.2  nathanw 	/*
    349  1.35.6.2  nathanw 	 * allocate and zero buffers
    350  1.35.6.2  nathanw 	 * check boundaries of the KVA's ... all IOPBs must reside in
    351  1.35.6.2  nathanw  	 * the same 64K region.
    352  1.35.6.2  nathanw 	 */
    353  1.35.6.2  nathanw 
    354  1.35.6.2  nathanw 	pbsz = XYC_MAXIOPB * sizeof(struct xy_iopb);
    355  1.35.6.2  nathanw 	tmp = tmp2 = (struct xy_iopb *) dvma_malloc(pbsz);	/* KVA */
    356  1.35.6.2  nathanw 	ultmp = (u_long) tmp;
    357  1.35.6.2  nathanw 	if ((ultmp & 0xffff0000) != ((ultmp + pbsz) & 0xffff0000)) {
    358  1.35.6.2  nathanw 		tmp = (struct xy_iopb *) dvma_malloc(pbsz); /* retry! */
    359  1.35.6.2  nathanw 		dvma_free(tmp2, pbsz);
    360  1.35.6.2  nathanw 		ultmp = (u_long) tmp;
    361  1.35.6.2  nathanw 		if ((ultmp & 0xffff0000) != ((ultmp + pbsz) & 0xffff0000)) {
    362  1.35.6.2  nathanw 			printf("%s: can't alloc IOPB mem in 64K\n",
    363  1.35.6.2  nathanw 				xyc->sc_dev.dv_xname);
    364  1.35.6.2  nathanw 			return;
    365  1.35.6.2  nathanw 		}
    366  1.35.6.2  nathanw 	}
    367  1.35.6.2  nathanw 	memset(tmp, 0, pbsz);
    368  1.35.6.2  nathanw 	xyc->iopbase = tmp;
    369  1.35.6.2  nathanw 	xyc->dvmaiopb = (struct xy_iopb *)
    370  1.35.6.2  nathanw 		dvma_kvtopa(xyc->iopbase, xyc->bustype);
    371  1.35.6.2  nathanw 	xyc->reqs = (struct xy_iorq *)
    372  1.35.6.2  nathanw 	    malloc(XYC_MAXIOPB * sizeof(struct xy_iorq), M_DEVBUF, M_NOWAIT);
    373  1.35.6.2  nathanw 	if (xyc->reqs == NULL)
    374  1.35.6.2  nathanw 		panic("xyc malloc");
    375  1.35.6.2  nathanw 	memset(xyc->reqs, 0, XYC_MAXIOPB * sizeof(struct xy_iorq));
    376  1.35.6.2  nathanw 
    377  1.35.6.2  nathanw 	/*
    378  1.35.6.2  nathanw 	 * init iorq to iopb pointers, and non-zero fields in the
    379  1.35.6.2  nathanw 	 * iopb which never change.
    380  1.35.6.2  nathanw 	 */
    381  1.35.6.2  nathanw 
    382  1.35.6.2  nathanw 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
    383  1.35.6.2  nathanw 		xyc->xy_chain[lcv] = NULL;
    384  1.35.6.2  nathanw 		xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
    385  1.35.6.2  nathanw 		xyc->iopbase[lcv].asr = 1;	/* always the same */
    386  1.35.6.2  nathanw 		xyc->iopbase[lcv].eef = 1;	/* always the same */
    387  1.35.6.2  nathanw 		xyc->iopbase[lcv].ecm = XY_ECM;	/* always the same */
    388  1.35.6.2  nathanw 		xyc->iopbase[lcv].aud = 1;	/* always the same */
    389  1.35.6.2  nathanw 		xyc->iopbase[lcv].relo = 1;	/* always the same */
    390  1.35.6.2  nathanw 		xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
    391  1.35.6.2  nathanw 	}
    392  1.35.6.2  nathanw 	xyc->ciorq = &xyc->reqs[XYC_CTLIOPB];    /* short hand name */
    393  1.35.6.2  nathanw 	xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
    394  1.35.6.2  nathanw 	xyc->xy_hand = 0;
    395  1.35.6.2  nathanw 
    396  1.35.6.2  nathanw 	/* read controller parameters and insure we have a 450/451 */
    397  1.35.6.2  nathanw 
    398  1.35.6.2  nathanw 	err = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
    399  1.35.6.2  nathanw 	res = xyc->ciopb->ctyp;
    400  1.35.6.2  nathanw 	XYC_DONE(xyc, err);
    401  1.35.6.2  nathanw 	if (res != XYCT_450) {
    402  1.35.6.2  nathanw 		if (err)
    403  1.35.6.2  nathanw 			printf(": %s: ", xyc_e2str(err));
    404  1.35.6.2  nathanw 		printf(": doesn't identify as a 450/451\n");
    405  1.35.6.2  nathanw 		return;
    406  1.35.6.2  nathanw 	}
    407  1.35.6.2  nathanw 	printf(": Xylogics 450/451");
    408  1.35.6.2  nathanw 	if (xyc->no_ols)
    409  1.35.6.2  nathanw 		printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
    410  1.35.6.2  nathanw 	printf("\n");
    411  1.35.6.2  nathanw 	if (err) {
    412  1.35.6.2  nathanw 		printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
    413  1.35.6.2  nathanw 				xyc_e2str(err));
    414  1.35.6.2  nathanw 		return;
    415  1.35.6.2  nathanw 	}
    416  1.35.6.2  nathanw 	if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
    417  1.35.6.2  nathanw 		printf("%s: 24 bit addressing turned off\n",
    418  1.35.6.2  nathanw 			   xyc->sc_dev.dv_xname);
    419  1.35.6.2  nathanw 		printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
    420  1.35.6.2  nathanw 		printf("to enable 24 bit mode and this driver\n");
    421  1.35.6.2  nathanw 		return;
    422  1.35.6.2  nathanw 	}
    423  1.35.6.2  nathanw 
    424  1.35.6.2  nathanw 	/* link in interrupt with higher level software */
    425  1.35.6.2  nathanw 	isr_add_vectored(xycintr, (void *)xyc,
    426  1.35.6.2  nathanw 	                 ca->ca_intpri, ca->ca_intvec);
    427  1.35.6.2  nathanw 	evcnt_attach_dynamic(&xyc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    428  1.35.6.2  nathanw 	    xyc->sc_dev.dv_xname, "intr");
    429  1.35.6.2  nathanw 
    430  1.35.6.2  nathanw 	callout_init(&xyc->sc_tick_ch);
    431  1.35.6.2  nathanw 
    432  1.35.6.2  nathanw 	/* now we must look for disks using autoconfig */
    433  1.35.6.2  nathanw 	for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
    434  1.35.6.2  nathanw 		(void) config_found(self, (void *) &xa, xyc_print);
    435  1.35.6.2  nathanw 
    436  1.35.6.2  nathanw 	/* start the watchdog clock */
    437  1.35.6.2  nathanw 	callout_reset(&xyc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xyc);
    438  1.35.6.2  nathanw }
    439  1.35.6.2  nathanw 
    440  1.35.6.2  nathanw static int
    441  1.35.6.2  nathanw xyc_print(aux, name)
    442  1.35.6.2  nathanw 	void *aux;
    443  1.35.6.2  nathanw 	const char *name;
    444  1.35.6.2  nathanw {
    445  1.35.6.2  nathanw 	struct xyc_attach_args *xa = aux;
    446  1.35.6.2  nathanw 
    447  1.35.6.2  nathanw 	if (name != NULL)
    448  1.35.6.2  nathanw 		printf("%s: ", name);
    449  1.35.6.2  nathanw 
    450  1.35.6.2  nathanw 	if (xa->driveno != -1)
    451  1.35.6.2  nathanw 		printf(" drive %d", xa->driveno);
    452  1.35.6.2  nathanw 
    453  1.35.6.2  nathanw 	return UNCONF;
    454  1.35.6.2  nathanw }
    455  1.35.6.2  nathanw 
    456  1.35.6.2  nathanw /*
    457  1.35.6.2  nathanw  * xymatch: probe for disk.
    458  1.35.6.2  nathanw  *
    459  1.35.6.2  nathanw  * note: we almost always say disk is present.   this allows us to
    460  1.35.6.2  nathanw  * spin up and configure a disk after the system is booted (we can
    461  1.35.6.2  nathanw  * call xyattach!).  Also, wire down the relationship between the
    462  1.35.6.2  nathanw  * xy* and xyc* devices, to simplify boot device identification.
    463  1.35.6.2  nathanw  */
    464  1.35.6.2  nathanw static int
    465  1.35.6.2  nathanw xymatch(parent, cf, aux)
    466  1.35.6.2  nathanw 	struct device *parent;
    467  1.35.6.2  nathanw 	struct cfdata *cf;
    468  1.35.6.2  nathanw 	void *aux;
    469  1.35.6.2  nathanw {
    470  1.35.6.2  nathanw 	struct xyc_attach_args *xa = aux;
    471  1.35.6.2  nathanw 	int xy_unit;
    472  1.35.6.2  nathanw 
    473  1.35.6.2  nathanw 	/* Match only on the "wired-down" controller+disk. */
    474  1.35.6.2  nathanw 	xy_unit = parent->dv_unit * 2 + xa->driveno;
    475  1.35.6.2  nathanw 	if (cf->cf_unit != xy_unit)
    476  1.35.6.2  nathanw 		return (0);
    477  1.35.6.2  nathanw 
    478  1.35.6.2  nathanw 	return (1);
    479  1.35.6.2  nathanw }
    480  1.35.6.2  nathanw 
    481  1.35.6.2  nathanw /*
    482  1.35.6.2  nathanw  * xyattach: attach a disk.
    483  1.35.6.2  nathanw  */
    484  1.35.6.2  nathanw static void
    485  1.35.6.2  nathanw xyattach(parent, self, aux)
    486  1.35.6.2  nathanw 	struct device *parent, *self;
    487  1.35.6.2  nathanw 	void   *aux;
    488  1.35.6.2  nathanw {
    489  1.35.6.2  nathanw 	struct xy_softc *xy = (void *) self;
    490  1.35.6.2  nathanw 	struct xyc_softc *xyc = (void *) parent;
    491  1.35.6.2  nathanw 	struct xyc_attach_args *xa = aux;
    492  1.35.6.2  nathanw 
    493  1.35.6.2  nathanw 	printf("\n");
    494  1.35.6.2  nathanw 
    495  1.35.6.2  nathanw 	/*
    496  1.35.6.2  nathanw 	 * Always re-initialize the disk structure.  We want statistics
    497  1.35.6.2  nathanw 	 * to start with a clean slate.
    498  1.35.6.2  nathanw 	 */
    499  1.35.6.2  nathanw 	memset(&xy->sc_dk, 0, sizeof(xy->sc_dk));
    500  1.35.6.2  nathanw 	xy->sc_dk.dk_driver = &xydkdriver;
    501  1.35.6.2  nathanw 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
    502  1.35.6.2  nathanw 
    503  1.35.6.2  nathanw 	xy->state = XY_DRIVE_UNKNOWN;	/* to start */
    504  1.35.6.2  nathanw 	xy->flags = 0;
    505  1.35.6.2  nathanw 	xy->parent = xyc;
    506  1.35.6.2  nathanw 
    507  1.35.6.2  nathanw 	/* init queue of waiting bufs */
    508  1.35.6.2  nathanw 	bufq_alloc(&xy->xyq, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
    509  1.35.6.2  nathanw 	xy->xyrq = &xyc->reqs[xa->driveno];
    510  1.35.6.2  nathanw 
    511  1.35.6.2  nathanw 	xy->xy_drive = xa->driveno;
    512  1.35.6.2  nathanw 	xyc->sc_drives[xa->driveno] = xy;
    513  1.35.6.2  nathanw 
    514  1.35.6.2  nathanw 	/* Do init work common to attach and open. */
    515  1.35.6.2  nathanw 	xy_init(xy);
    516  1.35.6.2  nathanw }
    517  1.35.6.2  nathanw 
    518  1.35.6.2  nathanw /*
    519  1.35.6.2  nathanw  * end of autoconfig functions
    520  1.35.6.2  nathanw  */
    521  1.35.6.2  nathanw 
    522  1.35.6.2  nathanw /*
    523  1.35.6.2  nathanw  * Initialize a disk.  This can be called from both autoconf and
    524  1.35.6.2  nathanw  * also from xyopen/xystrategy.
    525  1.35.6.2  nathanw  */
    526  1.35.6.2  nathanw static void
    527  1.35.6.2  nathanw xy_init(xy)
    528  1.35.6.2  nathanw 	struct xy_softc *xy;
    529  1.35.6.2  nathanw {
    530  1.35.6.2  nathanw 	struct xyc_softc *xyc;
    531  1.35.6.2  nathanw 	struct dkbad *dkb;
    532  1.35.6.2  nathanw 	void *dvmabuf;
    533  1.35.6.2  nathanw 	int err, spt, mb, blk, lcv, fullmode, newstate;
    534  1.35.6.2  nathanw 
    535  1.35.6.2  nathanw 	xyc = xy->parent;
    536  1.35.6.2  nathanw 	xy->state = XY_DRIVE_ATTACHING;
    537  1.35.6.2  nathanw 	newstate = XY_DRIVE_UNKNOWN;
    538  1.35.6.2  nathanw 	fullmode = (cold) ? XY_SUB_POLL : XY_SUB_WAIT;
    539  1.35.6.2  nathanw 	dvmabuf  = dvma_malloc(XYFM_BPS);
    540  1.35.6.2  nathanw 
    541  1.35.6.2  nathanw 	/* first try and reset the drive */
    542  1.35.6.2  nathanw 
    543  1.35.6.2  nathanw 	err = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fullmode);
    544  1.35.6.2  nathanw 	XYC_DONE(xyc, err);
    545  1.35.6.2  nathanw 	if (err == XY_ERR_DNRY) {
    546  1.35.6.2  nathanw 		printf("%s: drive %d: off-line\n",
    547  1.35.6.2  nathanw 			   xy->sc_dev.dv_xname, xy->xy_drive);
    548  1.35.6.2  nathanw 		goto done;
    549  1.35.6.2  nathanw 	}
    550  1.35.6.2  nathanw 	if (err) {
    551  1.35.6.2  nathanw 		printf("%s: ERROR 0x%02x (%s)\n",
    552  1.35.6.2  nathanw 			   xy->sc_dev.dv_xname, err, xyc_e2str(err));
    553  1.35.6.2  nathanw 		goto done;
    554  1.35.6.2  nathanw 	}
    555  1.35.6.2  nathanw 	printf("%s: drive %d ready",
    556  1.35.6.2  nathanw 		   xy->sc_dev.dv_xname, xy->xy_drive);
    557  1.35.6.2  nathanw 
    558  1.35.6.2  nathanw 	/*
    559  1.35.6.2  nathanw 	 * now set drive parameters (to semi-bogus values) so we can read the
    560  1.35.6.2  nathanw 	 * disk label.
    561  1.35.6.2  nathanw 	 */
    562  1.35.6.2  nathanw 	xy->pcyl = xy->ncyl = 1;
    563  1.35.6.2  nathanw 	xy->acyl = 0;
    564  1.35.6.2  nathanw 	xy->nhead = 1;
    565  1.35.6.2  nathanw 	xy->nsect = 1;
    566  1.35.6.2  nathanw 	xy->sectpercyl = 1;
    567  1.35.6.2  nathanw 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
    568  1.35.6.2  nathanw 		xy->dkb.bt_bad[lcv].bt_cyl =
    569  1.35.6.2  nathanw 			xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
    570  1.35.6.2  nathanw 
    571  1.35.6.2  nathanw 	/* read disk label */
    572  1.35.6.2  nathanw 	for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
    573  1.35.6.2  nathanw 						xy->drive_type++) {
    574  1.35.6.2  nathanw 		err = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
    575  1.35.6.2  nathanw 						dvmabuf, fullmode);
    576  1.35.6.2  nathanw 		XYC_DONE(xyc, err);
    577  1.35.6.2  nathanw 		if (err == XY_ERR_AOK) break;
    578  1.35.6.2  nathanw 	}
    579  1.35.6.2  nathanw 
    580  1.35.6.2  nathanw 	if (err != XY_ERR_AOK) {
    581  1.35.6.2  nathanw 		printf("%s: reading disk label failed: %s\n",
    582  1.35.6.2  nathanw 			xy->sc_dev.dv_xname, xyc_e2str(err));
    583  1.35.6.2  nathanw 		goto done;
    584  1.35.6.2  nathanw 	}
    585  1.35.6.2  nathanw 	printf("%s: drive type %d\n",
    586  1.35.6.2  nathanw 		   xy->sc_dev.dv_xname, xy->drive_type);
    587  1.35.6.2  nathanw 
    588  1.35.6.2  nathanw 	newstate = XY_DRIVE_NOLABEL;
    589  1.35.6.2  nathanw 
    590  1.35.6.2  nathanw 	xy->hw_spt = spt = 0; /* XXX needed ? */
    591  1.35.6.2  nathanw 	/* Attach the disk: must be before getdisklabel to malloc label */
    592  1.35.6.2  nathanw 	disk_attach(&xy->sc_dk);
    593  1.35.6.2  nathanw 
    594  1.35.6.2  nathanw 	if (xygetdisklabel(xy, dvmabuf) != XY_ERR_AOK)
    595  1.35.6.2  nathanw 		goto done;
    596  1.35.6.2  nathanw 
    597  1.35.6.2  nathanw 	/* inform the user of what is up */
    598  1.35.6.2  nathanw 	printf("%s: <%s>, pcyl %d\n",
    599  1.35.6.2  nathanw 		   xy->sc_dev.dv_xname,
    600  1.35.6.2  nathanw 		   (char *)dvmabuf, xy->pcyl);
    601  1.35.6.2  nathanw 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
    602  1.35.6.2  nathanw 	printf("%s: %dMB, %d cyl, %d head, %d sec\n",
    603  1.35.6.2  nathanw 		xy->sc_dev.dv_xname, mb,
    604  1.35.6.2  nathanw 		xy->ncyl, xy->nhead, xy->nsect);
    605  1.35.6.2  nathanw 
    606  1.35.6.2  nathanw 	/*
    607  1.35.6.2  nathanw 	 * 450/451 stupidity: the drive type is encoded into the format
    608  1.35.6.2  nathanw 	 * of the disk.   the drive type in the IOPB must match the drive
    609  1.35.6.2  nathanw 	 * type in the format, or you will not be able to do I/O to the
    610  1.35.6.2  nathanw 	 * disk (you get header not found errors).  if you have two drives
    611  1.35.6.2  nathanw 	 * of different sizes that have the same drive type in their
    612  1.35.6.2  nathanw 	 * formatting then you are out of luck.
    613  1.35.6.2  nathanw 	 *
    614  1.35.6.2  nathanw 	 * this problem was corrected in the 753/7053.
    615  1.35.6.2  nathanw 	 */
    616  1.35.6.2  nathanw 
    617  1.35.6.2  nathanw 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
    618  1.35.6.2  nathanw 		struct xy_softc *oxy;
    619  1.35.6.2  nathanw 
    620  1.35.6.2  nathanw 		oxy = xyc->sc_drives[lcv];
    621  1.35.6.2  nathanw 		if (oxy == NULL || oxy == xy) continue;
    622  1.35.6.2  nathanw 		if (oxy->drive_type != xy->drive_type) continue;
    623  1.35.6.2  nathanw 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
    624  1.35.6.2  nathanw 			xy->nhead != oxy->nhead) {
    625  1.35.6.2  nathanw 			printf("%s: %s and %s must be the same size!\n",
    626  1.35.6.2  nathanw 				xyc->sc_dev.dv_xname,
    627  1.35.6.2  nathanw 				xy ->sc_dev.dv_xname,
    628  1.35.6.2  nathanw 				oxy->sc_dev.dv_xname);
    629  1.35.6.2  nathanw 			panic("xy drive size mismatch");
    630  1.35.6.2  nathanw 		}
    631  1.35.6.2  nathanw 	}
    632  1.35.6.2  nathanw 
    633  1.35.6.2  nathanw 
    634  1.35.6.2  nathanw 	/* now set the real drive parameters! */
    635  1.35.6.2  nathanw 	blk = (xy->nsect - 1) +
    636  1.35.6.2  nathanw 		((xy->nhead - 1) * xy->nsect) +
    637  1.35.6.2  nathanw 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
    638  1.35.6.2  nathanw 	err = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fullmode);
    639  1.35.6.2  nathanw 	XYC_DONE(xyc, err);
    640  1.35.6.2  nathanw 	if (err) {
    641  1.35.6.2  nathanw 		printf("%s: write drive size failed: %s\n",
    642  1.35.6.2  nathanw 			xy->sc_dev.dv_xname, xyc_e2str(err));
    643  1.35.6.2  nathanw 		goto done;
    644  1.35.6.2  nathanw 	}
    645  1.35.6.2  nathanw 	newstate = XY_DRIVE_ONLINE;
    646  1.35.6.2  nathanw 
    647  1.35.6.2  nathanw 	/*
    648  1.35.6.2  nathanw 	 * read bad144 table. this table resides on the first sector of the
    649  1.35.6.2  nathanw 	 * last track of the disk (i.e. second cyl of "acyl" area).
    650  1.35.6.2  nathanw 	 */
    651  1.35.6.2  nathanw 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
    652  1.35.6.2  nathanw 								/* last cyl */
    653  1.35.6.2  nathanw 	    (xy->nhead - 1) * xy->nsect;	/* last head */
    654  1.35.6.2  nathanw 	err = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
    655  1.35.6.2  nathanw 						dvmabuf, fullmode);
    656  1.35.6.2  nathanw 	XYC_DONE(xyc, err);
    657  1.35.6.2  nathanw 	if (err) {
    658  1.35.6.2  nathanw 		printf("%s: reading bad144 failed: %s\n",
    659  1.35.6.2  nathanw 			xy->sc_dev.dv_xname, xyc_e2str(err));
    660  1.35.6.2  nathanw 		goto done;
    661  1.35.6.2  nathanw 	}
    662  1.35.6.2  nathanw 
    663  1.35.6.2  nathanw 	/* check dkbad for sanity */
    664  1.35.6.2  nathanw 	dkb = (struct dkbad *) dvmabuf;
    665  1.35.6.2  nathanw 	for (lcv = 0; lcv < 126; lcv++) {
    666  1.35.6.2  nathanw 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
    667  1.35.6.2  nathanw 				dkb->bt_bad[lcv].bt_cyl == 0) &&
    668  1.35.6.2  nathanw 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
    669  1.35.6.2  nathanw 			continue;	/* blank */
    670  1.35.6.2  nathanw 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
    671  1.35.6.2  nathanw 			break;
    672  1.35.6.2  nathanw 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
    673  1.35.6.2  nathanw 			break;
    674  1.35.6.2  nathanw 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
    675  1.35.6.2  nathanw 			break;
    676  1.35.6.2  nathanw 	}
    677  1.35.6.2  nathanw 	if (lcv != 126) {
    678  1.35.6.2  nathanw 		printf("%s: warning: invalid bad144 sector!\n",
    679  1.35.6.2  nathanw 			xy->sc_dev.dv_xname);
    680  1.35.6.2  nathanw 	} else {
    681  1.35.6.2  nathanw 		memcpy(&xy->dkb, dvmabuf, XYFM_BPS);
    682  1.35.6.2  nathanw 	}
    683  1.35.6.2  nathanw 
    684  1.35.6.2  nathanw done:
    685  1.35.6.2  nathanw 	xy->state = newstate;
    686  1.35.6.2  nathanw 	dvma_free(dvmabuf, XYFM_BPS);
    687  1.35.6.2  nathanw }
    688  1.35.6.2  nathanw 
    689  1.35.6.2  nathanw /*
    690  1.35.6.2  nathanw  * { b , c } d e v s w   f u n c t i o n s
    691  1.35.6.2  nathanw  */
    692  1.35.6.2  nathanw 
    693  1.35.6.2  nathanw /*
    694  1.35.6.2  nathanw  * xyclose: close device
    695  1.35.6.2  nathanw  */
    696  1.35.6.2  nathanw int
    697  1.35.6.2  nathanw xyclose(dev, flag, fmt, p)
    698  1.35.6.2  nathanw 	dev_t   dev;
    699  1.35.6.2  nathanw 	int     flag, fmt;
    700  1.35.6.2  nathanw 	struct proc *p;
    701  1.35.6.2  nathanw {
    702  1.35.6.2  nathanw 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
    703  1.35.6.2  nathanw 	int     part = DISKPART(dev);
    704  1.35.6.2  nathanw 
    705  1.35.6.2  nathanw 	/* clear mask bits */
    706  1.35.6.2  nathanw 
    707  1.35.6.2  nathanw 	switch (fmt) {
    708  1.35.6.2  nathanw 	case S_IFCHR:
    709  1.35.6.2  nathanw 		xy->sc_dk.dk_copenmask &= ~(1 << part);
    710  1.35.6.2  nathanw 		break;
    711  1.35.6.2  nathanw 	case S_IFBLK:
    712  1.35.6.2  nathanw 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
    713  1.35.6.2  nathanw 		break;
    714  1.35.6.2  nathanw 	}
    715  1.35.6.2  nathanw 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    716  1.35.6.2  nathanw 
    717  1.35.6.2  nathanw 	return 0;
    718  1.35.6.2  nathanw }
    719  1.35.6.2  nathanw 
    720  1.35.6.2  nathanw /*
    721  1.35.6.2  nathanw  * xydump: crash dump system
    722  1.35.6.2  nathanw  */
    723  1.35.6.2  nathanw int
    724  1.35.6.2  nathanw xydump(dev, blkno, va, sz)
    725  1.35.6.2  nathanw 	dev_t dev;
    726  1.35.6.2  nathanw 	daddr_t blkno;
    727  1.35.6.2  nathanw 	caddr_t va;
    728  1.35.6.2  nathanw 	size_t sz;
    729  1.35.6.2  nathanw {
    730  1.35.6.2  nathanw 	int     unit, part;
    731  1.35.6.2  nathanw 	struct xy_softc *xy;
    732  1.35.6.2  nathanw 
    733  1.35.6.2  nathanw 	unit = DISKUNIT(dev);
    734  1.35.6.2  nathanw 	if (unit >= xy_cd.cd_ndevs)
    735  1.35.6.2  nathanw 		return ENXIO;
    736  1.35.6.2  nathanw 	part = DISKPART(dev);
    737  1.35.6.2  nathanw 
    738  1.35.6.2  nathanw 	xy = xy_cd.cd_devs[unit];
    739  1.35.6.2  nathanw 
    740  1.35.6.2  nathanw 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
    741  1.35.6.2  nathanw 	    'a' + part);
    742  1.35.6.2  nathanw 
    743  1.35.6.2  nathanw 	return ENXIO;
    744  1.35.6.2  nathanw 
    745  1.35.6.2  nathanw 	/* outline: globals: "dumplo" == sector number of partition to start
    746  1.35.6.2  nathanw 	 * dump at (convert to physical sector with partition table)
    747  1.35.6.2  nathanw 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
    748  1.35.6.2  nathanw 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
    749  1.35.6.2  nathanw 	 * physmem)
    750  1.35.6.2  nathanw 	 *
    751  1.35.6.2  nathanw 	 * dump a copy of physical memory to the dump device starting at sector
    752  1.35.6.2  nathanw 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
    753  1.35.6.2  nathanw 	 * we go.   use polled I/O.
    754  1.35.6.2  nathanw 	 *
    755  1.35.6.2  nathanw 	 * XXX how to handle NON_CONTIG?
    756  1.35.6.2  nathanw 	 */
    757  1.35.6.2  nathanw }
    758  1.35.6.2  nathanw 
    759  1.35.6.2  nathanw /*
    760  1.35.6.2  nathanw  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
    761  1.35.6.2  nathanw  */
    762  1.35.6.2  nathanw int
    763  1.35.6.2  nathanw xyioctl(dev, command, addr, flag, p)
    764  1.35.6.2  nathanw 	dev_t   dev;
    765  1.35.6.2  nathanw 	u_long  command;
    766  1.35.6.2  nathanw 	caddr_t addr;
    767  1.35.6.2  nathanw 	int     flag;
    768  1.35.6.2  nathanw 	struct proc *p;
    769  1.35.6.2  nathanw 
    770  1.35.6.2  nathanw {
    771  1.35.6.2  nathanw 	struct xy_softc *xy;
    772  1.35.6.2  nathanw 	struct xd_iocmd *xio;
    773  1.35.6.2  nathanw 	int     error, s, unit;
    774  1.35.6.2  nathanw 
    775  1.35.6.2  nathanw 	unit = DISKUNIT(dev);
    776  1.35.6.2  nathanw 
    777  1.35.6.2  nathanw 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
    778  1.35.6.2  nathanw 		return (ENXIO);
    779  1.35.6.2  nathanw 
    780  1.35.6.2  nathanw 	/* switch on ioctl type */
    781  1.35.6.2  nathanw 
    782  1.35.6.2  nathanw 	switch (command) {
    783  1.35.6.2  nathanw 	case DIOCSBAD:		/* set bad144 info */
    784  1.35.6.2  nathanw 		if ((flag & FWRITE) == 0)
    785  1.35.6.2  nathanw 			return EBADF;
    786  1.35.6.2  nathanw 		s = splbio();
    787  1.35.6.2  nathanw 		memcpy(&xy->dkb, addr, sizeof(xy->dkb));
    788  1.35.6.2  nathanw 		splx(s);
    789  1.35.6.2  nathanw 		return 0;
    790  1.35.6.2  nathanw 
    791  1.35.6.2  nathanw 	case DIOCGDINFO:	/* get disk label */
    792  1.35.6.2  nathanw 		memcpy(addr, xy->sc_dk.dk_label, sizeof(struct disklabel));
    793  1.35.6.2  nathanw 		return 0;
    794  1.35.6.2  nathanw 
    795  1.35.6.2  nathanw 	case DIOCGPART:	/* get partition info */
    796  1.35.6.2  nathanw 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
    797  1.35.6.2  nathanw 		((struct partinfo *) addr)->part =
    798  1.35.6.2  nathanw 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    799  1.35.6.2  nathanw 		return 0;
    800  1.35.6.2  nathanw 
    801  1.35.6.2  nathanw 	case DIOCSDINFO:	/* set disk label */
    802  1.35.6.2  nathanw 		if ((flag & FWRITE) == 0)
    803  1.35.6.2  nathanw 			return EBADF;
    804  1.35.6.2  nathanw 		error = setdisklabel(xy->sc_dk.dk_label,
    805  1.35.6.2  nathanw 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
    806  1.35.6.2  nathanw 		    xy->sc_dk.dk_cpulabel);
    807  1.35.6.2  nathanw 		if (error == 0) {
    808  1.35.6.2  nathanw 			if (xy->state == XY_DRIVE_NOLABEL)
    809  1.35.6.2  nathanw 				xy->state = XY_DRIVE_ONLINE;
    810  1.35.6.2  nathanw 		}
    811  1.35.6.2  nathanw 		return error;
    812  1.35.6.2  nathanw 
    813  1.35.6.2  nathanw 	case DIOCWLABEL:	/* change write status of disk label */
    814  1.35.6.2  nathanw 		if ((flag & FWRITE) == 0)
    815  1.35.6.2  nathanw 			return EBADF;
    816  1.35.6.2  nathanw 		if (*(int *) addr)
    817  1.35.6.2  nathanw 			xy->flags |= XY_WLABEL;
    818  1.35.6.2  nathanw 		else
    819  1.35.6.2  nathanw 			xy->flags &= ~XY_WLABEL;
    820  1.35.6.2  nathanw 		return 0;
    821  1.35.6.2  nathanw 
    822  1.35.6.2  nathanw 	case DIOCWDINFO:	/* write disk label */
    823  1.35.6.2  nathanw 		if ((flag & FWRITE) == 0)
    824  1.35.6.2  nathanw 			return EBADF;
    825  1.35.6.2  nathanw 		error = setdisklabel(xy->sc_dk.dk_label,
    826  1.35.6.2  nathanw 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
    827  1.35.6.2  nathanw 		    xy->sc_dk.dk_cpulabel);
    828  1.35.6.2  nathanw 		if (error == 0) {
    829  1.35.6.2  nathanw 			if (xy->state == XY_DRIVE_NOLABEL)
    830  1.35.6.2  nathanw 				xy->state = XY_DRIVE_ONLINE;
    831  1.35.6.2  nathanw 
    832  1.35.6.2  nathanw 			/* Simulate opening partition 0 so write succeeds. */
    833  1.35.6.2  nathanw 			xy->sc_dk.dk_openmask |= (1 << 0);
    834  1.35.6.2  nathanw 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    835  1.35.6.2  nathanw 			    xystrategy, xy->sc_dk.dk_label,
    836  1.35.6.2  nathanw 			    xy->sc_dk.dk_cpulabel);
    837  1.35.6.2  nathanw 			xy->sc_dk.dk_openmask =
    838  1.35.6.2  nathanw 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    839  1.35.6.2  nathanw 		}
    840  1.35.6.2  nathanw 		return error;
    841  1.35.6.2  nathanw 
    842  1.35.6.2  nathanw 	case DIOSXDCMD:
    843  1.35.6.2  nathanw 		xio = (struct xd_iocmd *) addr;
    844  1.35.6.2  nathanw 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    845  1.35.6.2  nathanw 			return (error);
    846  1.35.6.2  nathanw 		return (xyc_ioctlcmd(xy, dev, xio));
    847  1.35.6.2  nathanw 
    848  1.35.6.2  nathanw 	default:
    849  1.35.6.2  nathanw 		return ENOTTY;
    850  1.35.6.2  nathanw 	}
    851  1.35.6.2  nathanw }
    852  1.35.6.2  nathanw 
    853  1.35.6.2  nathanw /*
    854  1.35.6.2  nathanw  * xyopen: open drive
    855  1.35.6.2  nathanw  */
    856  1.35.6.2  nathanw int
    857  1.35.6.2  nathanw xyopen(dev, flag, fmt, p)
    858  1.35.6.2  nathanw 	dev_t   dev;
    859  1.35.6.2  nathanw 	int     flag, fmt;
    860  1.35.6.2  nathanw 	struct proc *p;
    861  1.35.6.2  nathanw {
    862  1.35.6.2  nathanw 	int err, unit, part, s;
    863  1.35.6.2  nathanw 	struct xy_softc *xy;
    864  1.35.6.2  nathanw 
    865  1.35.6.2  nathanw 	/* first, could it be a valid target? */
    866  1.35.6.2  nathanw 	unit = DISKUNIT(dev);
    867  1.35.6.2  nathanw 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
    868  1.35.6.2  nathanw 		return (ENXIO);
    869  1.35.6.2  nathanw 	part = DISKPART(dev);
    870  1.35.6.2  nathanw 	err = 0;
    871  1.35.6.2  nathanw 
    872  1.35.6.2  nathanw 	/*
    873  1.35.6.2  nathanw 	 * If some other processing is doing init, sleep.
    874  1.35.6.2  nathanw 	 */
    875  1.35.6.2  nathanw 	s = splbio();
    876  1.35.6.2  nathanw 	while (xy->state == XY_DRIVE_ATTACHING) {
    877  1.35.6.2  nathanw 		if (tsleep(&xy->state, PRIBIO, "xyopen", 0)) {
    878  1.35.6.2  nathanw 			err = EINTR;
    879  1.35.6.2  nathanw 			goto done;
    880  1.35.6.2  nathanw 		}
    881  1.35.6.2  nathanw 	}
    882  1.35.6.2  nathanw 	/* Do we need to init the drive? */
    883  1.35.6.2  nathanw 	if (xy->state == XY_DRIVE_UNKNOWN) {
    884  1.35.6.2  nathanw 		xy_init(xy);
    885  1.35.6.2  nathanw 		wakeup(&xy->state);
    886  1.35.6.2  nathanw 	}
    887  1.35.6.2  nathanw 	/* Was the init successful? */
    888  1.35.6.2  nathanw 	if (xy->state == XY_DRIVE_UNKNOWN) {
    889  1.35.6.2  nathanw 		err = EIO;
    890  1.35.6.2  nathanw 		goto done;
    891  1.35.6.2  nathanw 	}
    892  1.35.6.2  nathanw 
    893  1.35.6.2  nathanw 	/* check for partition */
    894  1.35.6.2  nathanw 	if (part != RAW_PART &&
    895  1.35.6.2  nathanw 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
    896  1.35.6.2  nathanw 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    897  1.35.6.2  nathanw 		err = ENXIO;
    898  1.35.6.2  nathanw 		goto done;
    899  1.35.6.2  nathanw 	}
    900  1.35.6.2  nathanw 
    901  1.35.6.2  nathanw 	/* set open masks */
    902  1.35.6.2  nathanw 	switch (fmt) {
    903  1.35.6.2  nathanw 	case S_IFCHR:
    904  1.35.6.2  nathanw 		xy->sc_dk.dk_copenmask |= (1 << part);
    905  1.35.6.2  nathanw 		break;
    906  1.35.6.2  nathanw 	case S_IFBLK:
    907  1.35.6.2  nathanw 		xy->sc_dk.dk_bopenmask |= (1 << part);
    908  1.35.6.2  nathanw 		break;
    909  1.35.6.2  nathanw 	}
    910  1.35.6.2  nathanw 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    911  1.35.6.2  nathanw 
    912  1.35.6.2  nathanw done:
    913  1.35.6.2  nathanw 	splx(s);
    914  1.35.6.2  nathanw 	return (err);
    915  1.35.6.2  nathanw }
    916  1.35.6.2  nathanw 
    917  1.35.6.2  nathanw int
    918  1.35.6.2  nathanw xyread(dev, uio, flags)
    919  1.35.6.2  nathanw 	dev_t   dev;
    920  1.35.6.2  nathanw 	struct uio *uio;
    921  1.35.6.2  nathanw 	int flags;
    922  1.35.6.2  nathanw {
    923  1.35.6.2  nathanw 
    924  1.35.6.2  nathanw 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
    925  1.35.6.2  nathanw }
    926  1.35.6.2  nathanw 
    927  1.35.6.2  nathanw int
    928  1.35.6.2  nathanw xywrite(dev, uio, flags)
    929  1.35.6.2  nathanw 	dev_t   dev;
    930  1.35.6.2  nathanw 	struct uio *uio;
    931  1.35.6.2  nathanw 	int flags;
    932  1.35.6.2  nathanw {
    933  1.35.6.2  nathanw 
    934  1.35.6.2  nathanw 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
    935  1.35.6.2  nathanw }
    936  1.35.6.2  nathanw 
    937  1.35.6.2  nathanw 
    938  1.35.6.2  nathanw /*
    939  1.35.6.2  nathanw  * xysize: return size of a partition for a dump
    940  1.35.6.2  nathanw  */
    941  1.35.6.2  nathanw 
    942  1.35.6.2  nathanw int
    943  1.35.6.2  nathanw xysize(dev)
    944  1.35.6.2  nathanw 	dev_t   dev;
    945  1.35.6.2  nathanw 
    946  1.35.6.2  nathanw {
    947  1.35.6.2  nathanw 	struct xy_softc *xysc;
    948  1.35.6.2  nathanw 	int     unit, part, size, omask;
    949  1.35.6.2  nathanw 
    950  1.35.6.2  nathanw 	/* valid unit? */
    951  1.35.6.2  nathanw 	unit = DISKUNIT(dev);
    952  1.35.6.2  nathanw 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
    953  1.35.6.2  nathanw 		return (-1);
    954  1.35.6.2  nathanw 
    955  1.35.6.2  nathanw 	part = DISKPART(dev);
    956  1.35.6.2  nathanw 	omask = xysc->sc_dk.dk_openmask & (1 << part);
    957  1.35.6.2  nathanw 
    958  1.35.6.2  nathanw 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
    959  1.35.6.2  nathanw 		return (-1);
    960  1.35.6.2  nathanw 
    961  1.35.6.2  nathanw 	/* do it */
    962  1.35.6.2  nathanw 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    963  1.35.6.2  nathanw 		size = -1;	/* only give valid size for swap partitions */
    964  1.35.6.2  nathanw 	else
    965  1.35.6.2  nathanw 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
    966  1.35.6.2  nathanw 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    967  1.35.6.2  nathanw 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
    968  1.35.6.2  nathanw 		return (-1);
    969  1.35.6.2  nathanw 	return (size);
    970  1.35.6.2  nathanw }
    971  1.35.6.2  nathanw 
    972  1.35.6.2  nathanw /*
    973  1.35.6.2  nathanw  * xystrategy: buffering system interface to xy.
    974  1.35.6.2  nathanw  */
    975  1.35.6.2  nathanw void
    976  1.35.6.2  nathanw xystrategy(bp)
    977  1.35.6.2  nathanw 	struct buf *bp;
    978  1.35.6.2  nathanw 
    979  1.35.6.2  nathanw {
    980  1.35.6.2  nathanw 	struct xy_softc *xy;
    981  1.35.6.2  nathanw 	int     s, unit;
    982  1.35.6.2  nathanw 	struct disklabel *lp;
    983  1.35.6.2  nathanw 	daddr_t blkno;
    984  1.35.6.2  nathanw 
    985  1.35.6.2  nathanw 	unit = DISKUNIT(bp->b_dev);
    986  1.35.6.2  nathanw 
    987  1.35.6.2  nathanw 	/* check for live device */
    988  1.35.6.2  nathanw 
    989  1.35.6.2  nathanw 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
    990  1.35.6.2  nathanw 	    bp->b_blkno < 0 ||
    991  1.35.6.2  nathanw 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
    992  1.35.6.2  nathanw 		bp->b_error = EINVAL;
    993  1.35.6.2  nathanw 		goto bad;
    994  1.35.6.2  nathanw 	}
    995  1.35.6.2  nathanw 
    996  1.35.6.2  nathanw 	/* There should always be an open first. */
    997  1.35.6.2  nathanw 	if (xy->state == XY_DRIVE_UNKNOWN) {
    998  1.35.6.2  nathanw 		bp->b_error = EIO;
    999  1.35.6.2  nathanw 		goto bad;
   1000  1.35.6.2  nathanw 	}
   1001  1.35.6.2  nathanw 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
   1002  1.35.6.2  nathanw 		/* no I/O to unlabeled disks, unless raw partition */
   1003  1.35.6.2  nathanw 		bp->b_error = EIO;
   1004  1.35.6.2  nathanw 		goto bad;
   1005  1.35.6.2  nathanw 	}
   1006  1.35.6.2  nathanw 	/* short circuit zero length request */
   1007  1.35.6.2  nathanw 
   1008  1.35.6.2  nathanw 	if (bp->b_bcount == 0)
   1009  1.35.6.2  nathanw 		goto done;
   1010  1.35.6.2  nathanw 
   1011  1.35.6.2  nathanw 	/* check bounds with label (disksubr.c).  Determine the size of the
   1012  1.35.6.2  nathanw 	 * transfer, and make sure it is within the boundaries of the
   1013  1.35.6.2  nathanw 	 * partition. Adjust transfer if needed, and signal errors or early
   1014  1.35.6.2  nathanw 	 * completion. */
   1015  1.35.6.2  nathanw 
   1016  1.35.6.2  nathanw 	lp = xy->sc_dk.dk_label;
   1017  1.35.6.2  nathanw 
   1018  1.35.6.2  nathanw 	if (bounds_check_with_label(bp, lp,
   1019  1.35.6.2  nathanw 		(xy->flags & XY_WLABEL) != 0) <= 0)
   1020  1.35.6.2  nathanw 		goto done;
   1021  1.35.6.2  nathanw 
   1022  1.35.6.2  nathanw 	/*
   1023  1.35.6.2  nathanw 	 * Now convert the block number to absolute and put it in
   1024  1.35.6.2  nathanw 	 * terms of the device's logical block size.
   1025  1.35.6.2  nathanw 	 */
   1026  1.35.6.2  nathanw 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
   1027  1.35.6.2  nathanw 	if (DISKPART(bp->b_dev) != RAW_PART)
   1028  1.35.6.2  nathanw 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
   1029  1.35.6.2  nathanw 
   1030  1.35.6.2  nathanw 	bp->b_rawblkno = blkno;
   1031  1.35.6.2  nathanw 
   1032  1.35.6.2  nathanw 	/*
   1033  1.35.6.2  nathanw 	 * now we know we have a valid buf structure that we need to do I/O
   1034  1.35.6.2  nathanw 	 * on.
   1035  1.35.6.2  nathanw 	 */
   1036  1.35.6.2  nathanw 
   1037  1.35.6.2  nathanw 	s = splbio();		/* protect the queues */
   1038  1.35.6.2  nathanw 
   1039  1.35.6.2  nathanw 	BUFQ_PUT(&xy->xyq, bp);	 /* XXX disksort_cylinder */
   1040  1.35.6.2  nathanw 
   1041  1.35.6.2  nathanw 	/* start 'em up */
   1042  1.35.6.2  nathanw 
   1043  1.35.6.2  nathanw 	xyc_start(xy->parent, NULL);
   1044  1.35.6.2  nathanw 
   1045  1.35.6.2  nathanw 	/* done! */
   1046  1.35.6.2  nathanw 
   1047  1.35.6.2  nathanw 	splx(s);
   1048  1.35.6.2  nathanw 	return;
   1049  1.35.6.2  nathanw 
   1050  1.35.6.2  nathanw bad:				/* tells upper layers we have an error */
   1051  1.35.6.2  nathanw 	bp->b_flags |= B_ERROR;
   1052  1.35.6.2  nathanw done:				/* tells upper layers we are done with this
   1053  1.35.6.2  nathanw 				 * buf */
   1054  1.35.6.2  nathanw 	bp->b_resid = bp->b_bcount;
   1055  1.35.6.2  nathanw 	biodone(bp);
   1056  1.35.6.2  nathanw }
   1057  1.35.6.2  nathanw /*
   1058  1.35.6.2  nathanw  * end of {b,c}devsw functions
   1059  1.35.6.2  nathanw  */
   1060  1.35.6.2  nathanw 
   1061  1.35.6.2  nathanw /*
   1062  1.35.6.2  nathanw  * i n t e r r u p t   f u n c t i o n
   1063  1.35.6.2  nathanw  *
   1064  1.35.6.2  nathanw  * xycintr: hardware interrupt.
   1065  1.35.6.2  nathanw  */
   1066  1.35.6.2  nathanw int
   1067  1.35.6.2  nathanw xycintr(v)
   1068  1.35.6.2  nathanw 	void   *v;
   1069  1.35.6.2  nathanw 
   1070  1.35.6.2  nathanw {
   1071  1.35.6.2  nathanw 	struct xyc_softc *xycsc = v;
   1072  1.35.6.2  nathanw 
   1073  1.35.6.2  nathanw 	/* kick the event counter */
   1074  1.35.6.2  nathanw 	xycsc->sc_intrcnt.ev_count++;
   1075  1.35.6.2  nathanw 
   1076  1.35.6.2  nathanw 	/* remove as many done IOPBs as possible */
   1077  1.35.6.2  nathanw 	xyc_remove_iorq(xycsc);
   1078  1.35.6.2  nathanw 
   1079  1.35.6.2  nathanw 	/* start any iorq's already waiting */
   1080  1.35.6.2  nathanw 	xyc_start(xycsc, NULL);
   1081  1.35.6.2  nathanw 
   1082  1.35.6.2  nathanw 	return (1);
   1083  1.35.6.2  nathanw }
   1084  1.35.6.2  nathanw /*
   1085  1.35.6.2  nathanw  * end of interrupt function
   1086  1.35.6.2  nathanw  */
   1087  1.35.6.2  nathanw 
   1088  1.35.6.2  nathanw /*
   1089  1.35.6.2  nathanw  * i n t e r n a l   f u n c t i o n s
   1090  1.35.6.2  nathanw  */
   1091  1.35.6.2  nathanw 
   1092  1.35.6.2  nathanw /*
   1093  1.35.6.2  nathanw  * xyc_rqinit: fill out the fields of an I/O request
   1094  1.35.6.2  nathanw  */
   1095  1.35.6.2  nathanw 
   1096  1.35.6.2  nathanw inline void
   1097  1.35.6.2  nathanw xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
   1098  1.35.6.2  nathanw 	struct xy_iorq *rq;
   1099  1.35.6.2  nathanw 	struct xyc_softc *xyc;
   1100  1.35.6.2  nathanw 	struct xy_softc *xy;
   1101  1.35.6.2  nathanw 	int     md;
   1102  1.35.6.2  nathanw 	u_long  blk;
   1103  1.35.6.2  nathanw 	int     cnt;
   1104  1.35.6.2  nathanw 	caddr_t db;
   1105  1.35.6.2  nathanw 	struct buf *bp;
   1106  1.35.6.2  nathanw {
   1107  1.35.6.2  nathanw 	rq->xyc = xyc;
   1108  1.35.6.2  nathanw 	rq->xy = xy;
   1109  1.35.6.2  nathanw 	rq->ttl = XYC_MAXTTL + 10;
   1110  1.35.6.2  nathanw 	rq->mode = md;
   1111  1.35.6.2  nathanw 	rq->tries = rq->errno = rq->lasterror = 0;
   1112  1.35.6.2  nathanw 	rq->blockno = blk;
   1113  1.35.6.2  nathanw 	rq->sectcnt = cnt;
   1114  1.35.6.2  nathanw 	rq->dbuf = rq->dbufbase = db;
   1115  1.35.6.2  nathanw 	rq->buf = bp;
   1116  1.35.6.2  nathanw }
   1117  1.35.6.2  nathanw 
   1118  1.35.6.2  nathanw /*
   1119  1.35.6.2  nathanw  * xyc_rqtopb: load up an IOPB based on an iorq
   1120  1.35.6.2  nathanw  */
   1121  1.35.6.2  nathanw 
   1122  1.35.6.2  nathanw void
   1123  1.35.6.2  nathanw xyc_rqtopb(iorq, iopb, cmd, subfun)
   1124  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1125  1.35.6.2  nathanw 	struct xy_iopb *iopb;
   1126  1.35.6.2  nathanw 	int     cmd, subfun;
   1127  1.35.6.2  nathanw 
   1128  1.35.6.2  nathanw {
   1129  1.35.6.2  nathanw 	u_long  block, dp;
   1130  1.35.6.2  nathanw 
   1131  1.35.6.2  nathanw 	/* normal IOPB case, standard stuff */
   1132  1.35.6.2  nathanw 
   1133  1.35.6.2  nathanw 	/* chain bit handled later */
   1134  1.35.6.2  nathanw 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
   1135  1.35.6.2  nathanw 	iopb->com = cmd;
   1136  1.35.6.2  nathanw 	iopb->errno = 0;
   1137  1.35.6.2  nathanw 	iopb->errs = 0;
   1138  1.35.6.2  nathanw 	iopb->done = 0;
   1139  1.35.6.2  nathanw 	if (iorq->xy) {
   1140  1.35.6.2  nathanw 		iopb->unit = iorq->xy->xy_drive;
   1141  1.35.6.2  nathanw 		iopb->dt = iorq->xy->drive_type;
   1142  1.35.6.2  nathanw 	} else {
   1143  1.35.6.2  nathanw 		iopb->unit = 0;
   1144  1.35.6.2  nathanw 		iopb->dt = 0;
   1145  1.35.6.2  nathanw 	}
   1146  1.35.6.2  nathanw 	block = iorq->blockno;
   1147  1.35.6.2  nathanw 	if (iorq->xy == NULL || block == 0) {
   1148  1.35.6.2  nathanw 		iopb->sect = iopb->head = iopb->cyl = 0;
   1149  1.35.6.2  nathanw 	} else {
   1150  1.35.6.2  nathanw 		iopb->sect = block % iorq->xy->nsect;
   1151  1.35.6.2  nathanw 		block = block / iorq->xy->nsect;
   1152  1.35.6.2  nathanw 		iopb->head = block % iorq->xy->nhead;
   1153  1.35.6.2  nathanw 		block = block / iorq->xy->nhead;
   1154  1.35.6.2  nathanw 		iopb->cyl = block;
   1155  1.35.6.2  nathanw 	}
   1156  1.35.6.2  nathanw 	iopb->scnt = iorq->sectcnt;
   1157  1.35.6.2  nathanw 	if (iorq->dbuf == NULL) {
   1158  1.35.6.2  nathanw 		iopb->dataa = 0;
   1159  1.35.6.2  nathanw 		iopb->datar = 0;
   1160  1.35.6.2  nathanw 	} else {
   1161  1.35.6.2  nathanw 		dp = dvma_kvtopa(iorq->dbuf, iorq->xyc->bustype);
   1162  1.35.6.2  nathanw 		iopb->dataa = (dp & 0xffff);
   1163  1.35.6.2  nathanw 		iopb->datar = ((dp & 0xff0000) >> 16);
   1164  1.35.6.2  nathanw 	}
   1165  1.35.6.2  nathanw 	iopb->subfn = subfun;
   1166  1.35.6.2  nathanw }
   1167  1.35.6.2  nathanw 
   1168  1.35.6.2  nathanw 
   1169  1.35.6.2  nathanw /*
   1170  1.35.6.2  nathanw  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
   1171  1.35.6.2  nathanw  */
   1172  1.35.6.2  nathanw 
   1173  1.35.6.2  nathanw int
   1174  1.35.6.2  nathanw xyc_unbusy(xyc, del)
   1175  1.35.6.2  nathanw 
   1176  1.35.6.2  nathanw struct xyc *xyc;
   1177  1.35.6.2  nathanw int del;
   1178  1.35.6.2  nathanw 
   1179  1.35.6.2  nathanw {
   1180  1.35.6.2  nathanw 	while (del-- > 0) {
   1181  1.35.6.2  nathanw 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
   1182  1.35.6.2  nathanw 			break;
   1183  1.35.6.2  nathanw 		DELAY(1);
   1184  1.35.6.2  nathanw 	}
   1185  1.35.6.2  nathanw 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
   1186  1.35.6.2  nathanw }
   1187  1.35.6.2  nathanw 
   1188  1.35.6.2  nathanw /*
   1189  1.35.6.2  nathanw  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
   1190  1.35.6.2  nathanw  * note that NORM requests are handled separately.
   1191  1.35.6.2  nathanw  */
   1192  1.35.6.2  nathanw int
   1193  1.35.6.2  nathanw xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
   1194  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1195  1.35.6.2  nathanw 	int     cmd, subfn, unit, block, scnt;
   1196  1.35.6.2  nathanw 	char   *dptr;
   1197  1.35.6.2  nathanw 	int     fullmode;
   1198  1.35.6.2  nathanw {
   1199  1.35.6.2  nathanw 	struct xy_iorq *iorq = xycsc->ciorq;
   1200  1.35.6.2  nathanw 	struct xy_iopb *iopb = xycsc->ciopb;
   1201  1.35.6.2  nathanw 	int submode = XY_STATE(fullmode);
   1202  1.35.6.2  nathanw 
   1203  1.35.6.2  nathanw 	/*
   1204  1.35.6.2  nathanw 	 * is someone else using the control iopq wait for it if we can
   1205  1.35.6.2  nathanw 	 */
   1206  1.35.6.2  nathanw start:
   1207  1.35.6.2  nathanw 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1208  1.35.6.2  nathanw 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
   1209  1.35.6.2  nathanw                                 return(XY_ERR_FAIL);
   1210  1.35.6.2  nathanw 		goto start;
   1211  1.35.6.2  nathanw 	}
   1212  1.35.6.2  nathanw 
   1213  1.35.6.2  nathanw 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1214  1.35.6.2  nathanw 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
   1215  1.35.6.2  nathanw 		iorq->mode = XY_SUB_FREE;
   1216  1.35.6.2  nathanw 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
   1217  1.35.6.2  nathanw 	}
   1218  1.35.6.2  nathanw 
   1219  1.35.6.2  nathanw 	/* init iorq/iopb */
   1220  1.35.6.2  nathanw 
   1221  1.35.6.2  nathanw 	xyc_rqinit(iorq, xycsc,
   1222  1.35.6.2  nathanw 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
   1223  1.35.6.2  nathanw 	    fullmode, block, scnt, dptr, NULL);
   1224  1.35.6.2  nathanw 
   1225  1.35.6.2  nathanw 	/* load IOPB from iorq */
   1226  1.35.6.2  nathanw 
   1227  1.35.6.2  nathanw 	xyc_rqtopb(iorq, iopb, cmd, subfn);
   1228  1.35.6.2  nathanw 
   1229  1.35.6.2  nathanw 	/* submit it for processing */
   1230  1.35.6.2  nathanw 
   1231  1.35.6.2  nathanw 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
   1232  1.35.6.2  nathanw 
   1233  1.35.6.2  nathanw 	return(XY_ERR_AOK);
   1234  1.35.6.2  nathanw }
   1235  1.35.6.2  nathanw 
   1236  1.35.6.2  nathanw /*
   1237  1.35.6.2  nathanw  * xyc_startbuf
   1238  1.35.6.2  nathanw  * start a buffer for running
   1239  1.35.6.2  nathanw  */
   1240  1.35.6.2  nathanw 
   1241  1.35.6.2  nathanw int
   1242  1.35.6.2  nathanw xyc_startbuf(xycsc, xysc, bp)
   1243  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1244  1.35.6.2  nathanw 	struct xy_softc *xysc;
   1245  1.35.6.2  nathanw 	struct buf *bp;
   1246  1.35.6.2  nathanw 
   1247  1.35.6.2  nathanw {
   1248  1.35.6.2  nathanw 	int     partno;
   1249  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1250  1.35.6.2  nathanw 	struct xy_iopb *iopb;
   1251  1.35.6.2  nathanw 	u_long  block;
   1252  1.35.6.2  nathanw 	caddr_t dbuf;
   1253  1.35.6.2  nathanw 
   1254  1.35.6.2  nathanw 	iorq = xysc->xyrq;
   1255  1.35.6.2  nathanw 	iopb = iorq->iopb;
   1256  1.35.6.2  nathanw 
   1257  1.35.6.2  nathanw 	/* get buf */
   1258  1.35.6.2  nathanw 
   1259  1.35.6.2  nathanw 	if (bp == NULL)
   1260  1.35.6.2  nathanw 		panic("xyc_startbuf null buf");
   1261  1.35.6.2  nathanw 
   1262  1.35.6.2  nathanw 	partno = DISKPART(bp->b_dev);
   1263  1.35.6.2  nathanw #ifdef XYC_DEBUG
   1264  1.35.6.2  nathanw 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
   1265  1.35.6.2  nathanw 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1266  1.35.6.2  nathanw 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
   1267  1.35.6.2  nathanw 	    bp->b_bcount, bp->b_data);
   1268  1.35.6.2  nathanw #endif
   1269  1.35.6.2  nathanw 
   1270  1.35.6.2  nathanw 	/*
   1271  1.35.6.2  nathanw 	 * load request.
   1272  1.35.6.2  nathanw 	 *
   1273  1.35.6.2  nathanw 	 * also, note that there are two kinds of buf structures, those with
   1274  1.35.6.2  nathanw 	 * B_PHYS set and those without B_PHYS.   if B_PHYS is set, then it is
   1275  1.35.6.2  nathanw 	 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
   1276  1.35.6.2  nathanw 	 * buffer which has already been mapped into DVMA space. (Not on sun3)
   1277  1.35.6.2  nathanw 	 * However, if B_PHYS is not set, then the buffer is a normal system
   1278  1.35.6.2  nathanw 	 * buffer which does *not* live in DVMA space.  In that case we call
   1279  1.35.6.2  nathanw 	 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
   1280  1.35.6.2  nathanw 	 *
   1281  1.35.6.2  nathanw 	 * in cases where we do a dvma_mapin, note that iorq points to the buffer
   1282  1.35.6.2  nathanw 	 * as mapped into DVMA space, where as the bp->b_data points to its
   1283  1.35.6.2  nathanw 	 * non-DVMA mapping.
   1284  1.35.6.2  nathanw 	 *
   1285  1.35.6.2  nathanw 	 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
   1286  1.35.6.2  nathanw 	 * into dvma space, only that it was remapped into the kernel.
   1287  1.35.6.2  nathanw 	 * We ALWAYS have to remap the kernel buf into DVMA space.
   1288  1.35.6.2  nathanw 	 * (It is done inexpensively, using whole segments!)
   1289  1.35.6.2  nathanw 	 */
   1290  1.35.6.2  nathanw 
   1291  1.35.6.2  nathanw 	block = bp->b_rawblkno;
   1292  1.35.6.2  nathanw 
   1293  1.35.6.2  nathanw 	dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
   1294  1.35.6.2  nathanw 	if (dbuf == NULL) {	/* out of DVMA space */
   1295  1.35.6.2  nathanw 		printf("%s: warning: out of DVMA space\n",
   1296  1.35.6.2  nathanw 			   xycsc->sc_dev.dv_xname);
   1297  1.35.6.2  nathanw 		return (XY_ERR_FAIL);	/* XXX: need some sort of
   1298  1.35.6.2  nathanw 		                         * call-back scheme here? */
   1299  1.35.6.2  nathanw 	}
   1300  1.35.6.2  nathanw 
   1301  1.35.6.2  nathanw 	/* init iorq and load iopb from it */
   1302  1.35.6.2  nathanw 
   1303  1.35.6.2  nathanw 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
   1304  1.35.6.2  nathanw 	    bp->b_bcount / XYFM_BPS, dbuf, bp);
   1305  1.35.6.2  nathanw 
   1306  1.35.6.2  nathanw 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
   1307  1.35.6.2  nathanw 
   1308  1.35.6.2  nathanw 	/* Instrumentation. */
   1309  1.35.6.2  nathanw 	disk_busy(&xysc->sc_dk);
   1310  1.35.6.2  nathanw 
   1311  1.35.6.2  nathanw 	return (XY_ERR_AOK);
   1312  1.35.6.2  nathanw }
   1313  1.35.6.2  nathanw 
   1314  1.35.6.2  nathanw 
   1315  1.35.6.2  nathanw /*
   1316  1.35.6.2  nathanw  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
   1317  1.35.6.2  nathanw  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
   1318  1.35.6.2  nathanw  *
   1319  1.35.6.2  nathanw  * note: caller frees iorq in all cases except NORM
   1320  1.35.6.2  nathanw  *
   1321  1.35.6.2  nathanw  * return value:
   1322  1.35.6.2  nathanw  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
   1323  1.35.6.2  nathanw  *   WAIT: XY_AOK (success), <error-code> (failed)
   1324  1.35.6.2  nathanw  *   POLL: <same as WAIT>
   1325  1.35.6.2  nathanw  *   NOQ : <same as NORM>
   1326  1.35.6.2  nathanw  *
   1327  1.35.6.2  nathanw  * there are three sources for i/o requests:
   1328  1.35.6.2  nathanw  * [1] xystrategy: normal block I/O, using "struct buf" system.
   1329  1.35.6.2  nathanw  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1330  1.35.6.2  nathanw  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1331  1.35.6.2  nathanw  *                 and the process should block until they are done.
   1332  1.35.6.2  nathanw  *
   1333  1.35.6.2  nathanw  * software state is stored in the iorq structure.  each iorq has an
   1334  1.35.6.2  nathanw  * iopb structure.  the hardware understands the iopb structure.
   1335  1.35.6.2  nathanw  * every command must go through an iopb.  a 450 handles one iopb at a
   1336  1.35.6.2  nathanw  * time, where as a 451 can take them in chains.  [the 450 claims it
   1337  1.35.6.2  nathanw  * can handle chains, but is appears to be buggy...]   iopb are allocated
   1338  1.35.6.2  nathanw  * in DVMA space at boot up time.  each disk gets one iopb, and the
   1339  1.35.6.2  nathanw  * controller gets one (for POLL and WAIT commands).  what happens if
   1340  1.35.6.2  nathanw  * the iopb is busy?  for i/o type [1], the buffers are queued at the
   1341  1.35.6.2  nathanw  * "buff" layer and * picked up later by the interrupt routine.  for case
   1342  1.35.6.2  nathanw  * [2] we can only be blocked if there is a WAIT type I/O request being
   1343  1.35.6.2  nathanw  * run.   since this can only happen when we are crashing, we wait a sec
   1344  1.35.6.2  nathanw  * and then steal the IOPB.  for case [3] the process can sleep
   1345  1.35.6.2  nathanw  * on the iorq free list until some iopbs are avaliable.
   1346  1.35.6.2  nathanw  */
   1347  1.35.6.2  nathanw 
   1348  1.35.6.2  nathanw 
   1349  1.35.6.2  nathanw int
   1350  1.35.6.2  nathanw xyc_submit_iorq(xycsc, iorq, type)
   1351  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1352  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1353  1.35.6.2  nathanw 	int     type;
   1354  1.35.6.2  nathanw 
   1355  1.35.6.2  nathanw {
   1356  1.35.6.2  nathanw 	struct xy_iopb *iopb;
   1357  1.35.6.2  nathanw 	u_long  iopbaddr;
   1358  1.35.6.2  nathanw 
   1359  1.35.6.2  nathanw #ifdef XYC_DEBUG
   1360  1.35.6.2  nathanw 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
   1361  1.35.6.2  nathanw 		xycsc->sc_dev.dv_xname, iorq, type);
   1362  1.35.6.2  nathanw #endif
   1363  1.35.6.2  nathanw 
   1364  1.35.6.2  nathanw 	/* first check and see if controller is busy */
   1365  1.35.6.2  nathanw 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
   1366  1.35.6.2  nathanw #ifdef XYC_DEBUG
   1367  1.35.6.2  nathanw 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
   1368  1.35.6.2  nathanw #endif
   1369  1.35.6.2  nathanw 		if (type == XY_SUB_NOQ)
   1370  1.35.6.2  nathanw 			return (XY_ERR_FAIL);	/* failed */
   1371  1.35.6.2  nathanw 		switch (type) {
   1372  1.35.6.2  nathanw 		case XY_SUB_NORM:
   1373  1.35.6.2  nathanw 			return XY_ERR_AOK;	/* success */
   1374  1.35.6.2  nathanw 		case XY_SUB_WAIT:
   1375  1.35.6.2  nathanw 			while (iorq->iopb->done == 0) {
   1376  1.35.6.2  nathanw 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
   1377  1.35.6.2  nathanw 			}
   1378  1.35.6.2  nathanw 			return (iorq->errno);
   1379  1.35.6.2  nathanw 		case XY_SUB_POLL:		/* steal controller */
   1380  1.35.6.2  nathanw 			iopbaddr = xycsc->xyc->xyc_rsetup; /* RESET */
   1381  1.35.6.2  nathanw 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
   1382  1.35.6.2  nathanw 				panic("xyc_submit_iorq: stuck xyc");
   1383  1.35.6.2  nathanw 			printf("%s: stole controller\n",
   1384  1.35.6.2  nathanw 				xycsc->sc_dev.dv_xname);
   1385  1.35.6.2  nathanw 			break;
   1386  1.35.6.2  nathanw 		default:
   1387  1.35.6.2  nathanw 			panic("xyc_submit_iorq adding");
   1388  1.35.6.2  nathanw 		}
   1389  1.35.6.2  nathanw 	}
   1390  1.35.6.2  nathanw 
   1391  1.35.6.2  nathanw 	iopb = xyc_chain(xycsc, iorq);	 /* build chain */
   1392  1.35.6.2  nathanw 	if (iopb == NULL) { /* nothing doing? */
   1393  1.35.6.2  nathanw 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
   1394  1.35.6.2  nathanw 			return(XY_ERR_AOK);
   1395  1.35.6.2  nathanw 		panic("xyc_submit_iorq: xyc_chain failed!\n");
   1396  1.35.6.2  nathanw 	}
   1397  1.35.6.2  nathanw 	iopbaddr = dvma_kvtopa(iopb, xycsc->bustype);
   1398  1.35.6.2  nathanw 
   1399  1.35.6.2  nathanw 	XYC_GO(xycsc->xyc, iopbaddr);
   1400  1.35.6.2  nathanw 
   1401  1.35.6.2  nathanw 	/* command now running, wrap it up */
   1402  1.35.6.2  nathanw 	switch (type) {
   1403  1.35.6.2  nathanw 	case XY_SUB_NORM:
   1404  1.35.6.2  nathanw 	case XY_SUB_NOQ:
   1405  1.35.6.2  nathanw 		return (XY_ERR_AOK);	/* success */
   1406  1.35.6.2  nathanw 	case XY_SUB_WAIT:
   1407  1.35.6.2  nathanw 		while (iorq->iopb->done == 0) {
   1408  1.35.6.2  nathanw 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
   1409  1.35.6.2  nathanw 		}
   1410  1.35.6.2  nathanw 		return (iorq->errno);
   1411  1.35.6.2  nathanw 	case XY_SUB_POLL:
   1412  1.35.6.2  nathanw 		return (xyc_piodriver(xycsc, iorq));
   1413  1.35.6.2  nathanw 	default:
   1414  1.35.6.2  nathanw 		panic("xyc_submit_iorq wrap up");
   1415  1.35.6.2  nathanw 	}
   1416  1.35.6.2  nathanw 	panic("xyc_submit_iorq");
   1417  1.35.6.2  nathanw 	return 0;	/* not reached */
   1418  1.35.6.2  nathanw }
   1419  1.35.6.2  nathanw 
   1420  1.35.6.2  nathanw 
   1421  1.35.6.2  nathanw /*
   1422  1.35.6.2  nathanw  * xyc_chain: build a chain.  return dvma address of first element in
   1423  1.35.6.2  nathanw  * the chain.   iorq != NULL: means we only want that item on the chain.
   1424  1.35.6.2  nathanw  */
   1425  1.35.6.2  nathanw 
   1426  1.35.6.2  nathanw struct xy_iopb *
   1427  1.35.6.2  nathanw xyc_chain(xycsc, iorq)
   1428  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1429  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1430  1.35.6.2  nathanw {
   1431  1.35.6.2  nathanw 	int togo, chain, hand;
   1432  1.35.6.2  nathanw 	struct xy_iopb *iopb, *prev_iopb;
   1433  1.35.6.2  nathanw 	memset(xycsc->xy_chain, 0, sizeof(xycsc->xy_chain));
   1434  1.35.6.2  nathanw 
   1435  1.35.6.2  nathanw 	/*
   1436  1.35.6.2  nathanw 	 * promote control IOPB to the top
   1437  1.35.6.2  nathanw 	 */
   1438  1.35.6.2  nathanw 	if (iorq == NULL) {
   1439  1.35.6.2  nathanw 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
   1440  1.35.6.2  nathanw 			 XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
   1441  1.35.6.2  nathanw 			xycsc->iopbase[XYC_CTLIOPB].done == 0)
   1442  1.35.6.2  nathanw 		  iorq = &xycsc->reqs[XYC_CTLIOPB];
   1443  1.35.6.2  nathanw 	}
   1444  1.35.6.2  nathanw 	/*
   1445  1.35.6.2  nathanw 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
   1446  1.35.6.2  nathanw 	 * we let these take priority and do them first.
   1447  1.35.6.2  nathanw 	 */
   1448  1.35.6.2  nathanw 	if (iorq) {
   1449  1.35.6.2  nathanw 		xycsc->xy_chain[0] = iorq;
   1450  1.35.6.2  nathanw 		iorq->iopb->chen = 0;
   1451  1.35.6.2  nathanw 		return(iorq->iopb);
   1452  1.35.6.2  nathanw 	}
   1453  1.35.6.2  nathanw 
   1454  1.35.6.2  nathanw 	/*
   1455  1.35.6.2  nathanw 	 * NORM case: do round robin and maybe chain (if allowed and possible)
   1456  1.35.6.2  nathanw 	 */
   1457  1.35.6.2  nathanw 
   1458  1.35.6.2  nathanw 	chain = 0;
   1459  1.35.6.2  nathanw 	hand = xycsc->xy_hand;
   1460  1.35.6.2  nathanw 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
   1461  1.35.6.2  nathanw 
   1462  1.35.6.2  nathanw 	for (togo = XYC_MAXIOPB ;
   1463  1.35.6.2  nathanw 		 togo > 0 ;
   1464  1.35.6.2  nathanw 		 togo--, hand = (hand + 1) % XYC_MAXIOPB)
   1465  1.35.6.2  nathanw 	{
   1466  1.35.6.2  nathanw 
   1467  1.35.6.2  nathanw 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
   1468  1.35.6.2  nathanw 			xycsc->iopbase[hand].done)
   1469  1.35.6.2  nathanw 			continue;   /* not ready-for-i/o */
   1470  1.35.6.2  nathanw 
   1471  1.35.6.2  nathanw 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
   1472  1.35.6.2  nathanw 		iopb = xycsc->xy_chain[chain]->iopb;
   1473  1.35.6.2  nathanw 		iopb->chen = 0;
   1474  1.35.6.2  nathanw 		if (chain != 0) {   /* adding a link to a chain? */
   1475  1.35.6.2  nathanw 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
   1476  1.35.6.2  nathanw 			prev_iopb->chen = 1;
   1477  1.35.6.2  nathanw 			prev_iopb->nxtiopb = 0xffff &
   1478  1.35.6.2  nathanw 			  dvma_kvtopa(iopb, xycsc->bustype);
   1479  1.35.6.2  nathanw 		} else {            /* head of chain */
   1480  1.35.6.2  nathanw 			iorq = xycsc->xy_chain[chain];
   1481  1.35.6.2  nathanw 		}
   1482  1.35.6.2  nathanw 		chain++;
   1483  1.35.6.2  nathanw 		if (xycsc->no_ols) break;   /* quit if chaining dis-allowed */
   1484  1.35.6.2  nathanw 	}
   1485  1.35.6.2  nathanw 	return(iorq ? iorq->iopb : NULL);
   1486  1.35.6.2  nathanw }
   1487  1.35.6.2  nathanw 
   1488  1.35.6.2  nathanw /*
   1489  1.35.6.2  nathanw  * xyc_piodriver
   1490  1.35.6.2  nathanw  *
   1491  1.35.6.2  nathanw  * programmed i/o driver.   this function takes over the computer
   1492  1.35.6.2  nathanw  * and drains off the polled i/o request.   it returns the status of the iorq
   1493  1.35.6.2  nathanw  * the caller is interesting in.
   1494  1.35.6.2  nathanw  */
   1495  1.35.6.2  nathanw int
   1496  1.35.6.2  nathanw xyc_piodriver(xycsc, iorq)
   1497  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1498  1.35.6.2  nathanw 	struct xy_iorq  *iorq;
   1499  1.35.6.2  nathanw 
   1500  1.35.6.2  nathanw {
   1501  1.35.6.2  nathanw 	int     nreset = 0;
   1502  1.35.6.2  nathanw 	int     retval = 0;
   1503  1.35.6.2  nathanw 	u_long  res;
   1504  1.35.6.2  nathanw 
   1505  1.35.6.2  nathanw #ifdef XYC_DEBUG
   1506  1.35.6.2  nathanw 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
   1507  1.35.6.2  nathanw #endif
   1508  1.35.6.2  nathanw 
   1509  1.35.6.2  nathanw 	while (iorq->iopb->done == 0) {
   1510  1.35.6.2  nathanw 
   1511  1.35.6.2  nathanw 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
   1512  1.35.6.2  nathanw 
   1513  1.35.6.2  nathanw 		/* we expect some progress soon */
   1514  1.35.6.2  nathanw 		if (res == XY_ERR_FAIL && nreset >= 2) {
   1515  1.35.6.2  nathanw 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
   1516  1.35.6.2  nathanw #ifdef XYC_DEBUG
   1517  1.35.6.2  nathanw 			printf("xyc_piodriver: timeout\n");
   1518  1.35.6.2  nathanw #endif
   1519  1.35.6.2  nathanw 			return (XY_ERR_FAIL);
   1520  1.35.6.2  nathanw 		}
   1521  1.35.6.2  nathanw 		if (res == XY_ERR_FAIL) {
   1522  1.35.6.2  nathanw 			if (xyc_reset(xycsc, 0,
   1523  1.35.6.2  nathanw 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
   1524  1.35.6.2  nathanw 				      XY_ERR_FAIL,
   1525  1.35.6.2  nathanw 				      0) == XY_ERR_FAIL)
   1526  1.35.6.2  nathanw 				return (XY_ERR_FAIL);	/* flushes all but POLL
   1527  1.35.6.2  nathanw 							 * requests, resets */
   1528  1.35.6.2  nathanw 			continue;
   1529  1.35.6.2  nathanw 		}
   1530  1.35.6.2  nathanw 
   1531  1.35.6.2  nathanw 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
   1532  1.35.6.2  nathanw 
   1533  1.35.6.2  nathanw 		if (iorq->iopb->done == 0)
   1534  1.35.6.2  nathanw 			xyc_start(xycsc, iorq);
   1535  1.35.6.2  nathanw 	}
   1536  1.35.6.2  nathanw 
   1537  1.35.6.2  nathanw 	/* get return value */
   1538  1.35.6.2  nathanw 
   1539  1.35.6.2  nathanw 	retval = iorq->errno;
   1540  1.35.6.2  nathanw 
   1541  1.35.6.2  nathanw #ifdef XYC_DEBUG
   1542  1.35.6.2  nathanw 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
   1543  1.35.6.2  nathanw 	    iorq->errno, xyc_e2str(iorq->errno));
   1544  1.35.6.2  nathanw #endif
   1545  1.35.6.2  nathanw 
   1546  1.35.6.2  nathanw 	/* start up any bufs that have queued */
   1547  1.35.6.2  nathanw 
   1548  1.35.6.2  nathanw 	xyc_start(xycsc, NULL);
   1549  1.35.6.2  nathanw 
   1550  1.35.6.2  nathanw 	return (retval);
   1551  1.35.6.2  nathanw }
   1552  1.35.6.2  nathanw 
   1553  1.35.6.2  nathanw /*
   1554  1.35.6.2  nathanw  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
   1555  1.35.6.2  nathanw  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
   1556  1.35.6.2  nathanw  */
   1557  1.35.6.2  nathanw void
   1558  1.35.6.2  nathanw xyc_xyreset(xycsc, xysc)
   1559  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1560  1.35.6.2  nathanw 	struct xy_softc *xysc;
   1561  1.35.6.2  nathanw 
   1562  1.35.6.2  nathanw {
   1563  1.35.6.2  nathanw 	struct xy_iopb tmpiopb;
   1564  1.35.6.2  nathanw 	u_long  addr;
   1565  1.35.6.2  nathanw 	int     del;
   1566  1.35.6.2  nathanw 	memcpy(&tmpiopb, xycsc->ciopb, sizeof(tmpiopb));
   1567  1.35.6.2  nathanw 	xycsc->ciopb->chen = xycsc->ciopb->done = xycsc->ciopb->errs = 0;
   1568  1.35.6.2  nathanw 	xycsc->ciopb->ien = 0;
   1569  1.35.6.2  nathanw 	xycsc->ciopb->com = XYCMD_RST;
   1570  1.35.6.2  nathanw 	xycsc->ciopb->unit = xysc->xy_drive;
   1571  1.35.6.2  nathanw 	addr = dvma_kvtopa(xycsc->ciopb, xycsc->bustype);
   1572  1.35.6.2  nathanw 
   1573  1.35.6.2  nathanw 	XYC_GO(xycsc->xyc, addr);
   1574  1.35.6.2  nathanw 
   1575  1.35.6.2  nathanw 	del = XYC_RESETUSEC;
   1576  1.35.6.2  nathanw 	while (del > 0) {
   1577  1.35.6.2  nathanw 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0) break;
   1578  1.35.6.2  nathanw 		DELAY(1);
   1579  1.35.6.2  nathanw 		del--;
   1580  1.35.6.2  nathanw 	}
   1581  1.35.6.2  nathanw 
   1582  1.35.6.2  nathanw 	if (del <= 0 || xycsc->ciopb->errs) {
   1583  1.35.6.2  nathanw 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
   1584  1.35.6.2  nathanw 		    xyc_e2str(xycsc->ciopb->errno));
   1585  1.35.6.2  nathanw 		del = xycsc->xyc->xyc_rsetup;
   1586  1.35.6.2  nathanw 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
   1587  1.35.6.2  nathanw 			panic("xyc_reset");
   1588  1.35.6.2  nathanw 	} else {
   1589  1.35.6.2  nathanw 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
   1590  1.35.6.2  nathanw 	}
   1591  1.35.6.2  nathanw 	memcpy(xycsc->ciopb, &tmpiopb, sizeof(tmpiopb));
   1592  1.35.6.2  nathanw }
   1593  1.35.6.2  nathanw 
   1594  1.35.6.2  nathanw 
   1595  1.35.6.2  nathanw /*
   1596  1.35.6.2  nathanw  * xyc_reset: reset everything: requests are marked as errors except
   1597  1.35.6.2  nathanw  * a polled request (which is resubmitted)
   1598  1.35.6.2  nathanw  */
   1599  1.35.6.2  nathanw int
   1600  1.35.6.2  nathanw xyc_reset(xycsc, quiet, blastmode, error, xysc)
   1601  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1602  1.35.6.2  nathanw 	int     quiet, error;
   1603  1.35.6.2  nathanw 	struct xy_iorq *blastmode;
   1604  1.35.6.2  nathanw 	struct xy_softc *xysc;
   1605  1.35.6.2  nathanw 
   1606  1.35.6.2  nathanw {
   1607  1.35.6.2  nathanw 	int     del = 0, lcv, retval = XY_ERR_AOK;
   1608  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1609  1.35.6.2  nathanw 
   1610  1.35.6.2  nathanw 	/* soft reset hardware */
   1611  1.35.6.2  nathanw 
   1612  1.35.6.2  nathanw 	if (!quiet)
   1613  1.35.6.2  nathanw 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
   1614  1.35.6.2  nathanw 	del = xycsc->xyc->xyc_rsetup;
   1615  1.35.6.2  nathanw 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
   1616  1.35.6.2  nathanw 	if (del == XY_ERR_FAIL) {
   1617  1.35.6.2  nathanw 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
   1618  1.35.6.2  nathanw 		retval = XY_ERR_FAIL;
   1619  1.35.6.2  nathanw 	}
   1620  1.35.6.2  nathanw 	if (xysc)
   1621  1.35.6.2  nathanw 		xyc_xyreset(xycsc, xysc);
   1622  1.35.6.2  nathanw 
   1623  1.35.6.2  nathanw 	/* fix queues based on "blast-mode" */
   1624  1.35.6.2  nathanw 
   1625  1.35.6.2  nathanw 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1626  1.35.6.2  nathanw 		iorq = &xycsc->reqs[lcv];
   1627  1.35.6.2  nathanw 
   1628  1.35.6.2  nathanw 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
   1629  1.35.6.2  nathanw 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
   1630  1.35.6.2  nathanw 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
   1631  1.35.6.2  nathanw 			/* is it active? */
   1632  1.35.6.2  nathanw 			continue;
   1633  1.35.6.2  nathanw 
   1634  1.35.6.2  nathanw 		if (blastmode == XY_RSET_ALL ||
   1635  1.35.6.2  nathanw 				blastmode != iorq) {
   1636  1.35.6.2  nathanw 			/* failed */
   1637  1.35.6.2  nathanw 			iorq->errno = error;
   1638  1.35.6.2  nathanw 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
   1639  1.35.6.2  nathanw 			switch (XY_STATE(iorq->mode)) {
   1640  1.35.6.2  nathanw 			case XY_SUB_NORM:
   1641  1.35.6.2  nathanw 			    iorq->buf->b_error = EIO;
   1642  1.35.6.2  nathanw 			    iorq->buf->b_flags |= B_ERROR;
   1643  1.35.6.2  nathanw 			    iorq->buf->b_resid =
   1644  1.35.6.2  nathanw 			       iorq->sectcnt * XYFM_BPS;
   1645  1.35.6.2  nathanw 				/* Sun3: map/unmap regardless of B_PHYS */
   1646  1.35.6.2  nathanw 				dvma_mapout(iorq->dbufbase,
   1647  1.35.6.2  nathanw 				            iorq->buf->b_bcount);
   1648  1.35.6.2  nathanw 			    (void)BUFQ_GET(&iorq->xy->xyq);
   1649  1.35.6.2  nathanw 			    disk_unbusy(&iorq->xy->sc_dk,
   1650  1.35.6.2  nathanw 					        (iorq->buf->b_bcount -
   1651  1.35.6.2  nathanw 					         iorq->buf->b_resid));
   1652  1.35.6.2  nathanw 			    biodone(iorq->buf);
   1653  1.35.6.2  nathanw 			    iorq->mode = XY_SUB_FREE;
   1654  1.35.6.2  nathanw 			    break;
   1655  1.35.6.2  nathanw 			case XY_SUB_WAIT:
   1656  1.35.6.2  nathanw 			    wakeup(iorq);
   1657  1.35.6.2  nathanw 			case XY_SUB_POLL:
   1658  1.35.6.2  nathanw 			    iorq->mode =
   1659  1.35.6.2  nathanw 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1660  1.35.6.2  nathanw 			    break;
   1661  1.35.6.2  nathanw 			}
   1662  1.35.6.2  nathanw 
   1663  1.35.6.2  nathanw 		} else {
   1664  1.35.6.2  nathanw 
   1665  1.35.6.2  nathanw 			/* resubmit, no need to do anything here */
   1666  1.35.6.2  nathanw 		}
   1667  1.35.6.2  nathanw 	}
   1668  1.35.6.2  nathanw 
   1669  1.35.6.2  nathanw 	/*
   1670  1.35.6.2  nathanw 	 * now, if stuff is waiting, start it.
   1671  1.35.6.2  nathanw 	 * since we just reset it should go
   1672  1.35.6.2  nathanw 	 */
   1673  1.35.6.2  nathanw 	xyc_start(xycsc, NULL);
   1674  1.35.6.2  nathanw 
   1675  1.35.6.2  nathanw 	return (retval);
   1676  1.35.6.2  nathanw }
   1677  1.35.6.2  nathanw 
   1678  1.35.6.2  nathanw /*
   1679  1.35.6.2  nathanw  * xyc_start: start waiting buffers
   1680  1.35.6.2  nathanw  */
   1681  1.35.6.2  nathanw 
   1682  1.35.6.2  nathanw void
   1683  1.35.6.2  nathanw xyc_start(xycsc, iorq)
   1684  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1685  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1686  1.35.6.2  nathanw 
   1687  1.35.6.2  nathanw {
   1688  1.35.6.2  nathanw 	int lcv;
   1689  1.35.6.2  nathanw 	struct xy_softc *xy;
   1690  1.35.6.2  nathanw 
   1691  1.35.6.2  nathanw 	if (iorq == NULL) {
   1692  1.35.6.2  nathanw 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
   1693  1.35.6.2  nathanw 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
   1694  1.35.6.2  nathanw 			if (BUFQ_PEEK(&xy->xyq) == NULL) continue;
   1695  1.35.6.2  nathanw 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
   1696  1.35.6.2  nathanw 			xyc_startbuf(xycsc, xy, BUFQ_PEEK(&xy->xyq));
   1697  1.35.6.2  nathanw 		}
   1698  1.35.6.2  nathanw 	}
   1699  1.35.6.2  nathanw 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
   1700  1.35.6.2  nathanw }
   1701  1.35.6.2  nathanw 
   1702  1.35.6.2  nathanw /*
   1703  1.35.6.2  nathanw  * xyc_remove_iorq: remove "done" IOPB's.
   1704  1.35.6.2  nathanw  */
   1705  1.35.6.2  nathanw 
   1706  1.35.6.2  nathanw int
   1707  1.35.6.2  nathanw xyc_remove_iorq(xycsc)
   1708  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1709  1.35.6.2  nathanw 
   1710  1.35.6.2  nathanw {
   1711  1.35.6.2  nathanw 	int     errno, rq, comm, errs;
   1712  1.35.6.2  nathanw 	struct xyc *xyc = xycsc->xyc;
   1713  1.35.6.2  nathanw 	u_long  addr;
   1714  1.35.6.2  nathanw 	struct xy_iopb *iopb;
   1715  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1716  1.35.6.2  nathanw 	struct buf *bp;
   1717  1.35.6.2  nathanw 
   1718  1.35.6.2  nathanw 	if (xyc->xyc_csr & XYC_DERR) {
   1719  1.35.6.2  nathanw 		/*
   1720  1.35.6.2  nathanw 		 * DOUBLE ERROR: should never happen under normal use. This
   1721  1.35.6.2  nathanw 		 * error is so bad, you can't even tell which IOPB is bad, so
   1722  1.35.6.2  nathanw 		 * we dump them all.
   1723  1.35.6.2  nathanw 		 */
   1724  1.35.6.2  nathanw 		errno = XY_ERR_DERR;
   1725  1.35.6.2  nathanw 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
   1726  1.35.6.2  nathanw 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
   1727  1.35.6.2  nathanw 			printf("%s: soft reset failed!\n",
   1728  1.35.6.2  nathanw 				xycsc->sc_dev.dv_xname);
   1729  1.35.6.2  nathanw 			panic("xyc_remove_iorq: controller DEAD");
   1730  1.35.6.2  nathanw 		}
   1731  1.35.6.2  nathanw 		return (XY_ERR_AOK);
   1732  1.35.6.2  nathanw 	}
   1733  1.35.6.2  nathanw 
   1734  1.35.6.2  nathanw 	/*
   1735  1.35.6.2  nathanw 	 * get iopb that is done, loop down the chain
   1736  1.35.6.2  nathanw 	 */
   1737  1.35.6.2  nathanw 
   1738  1.35.6.2  nathanw 	if (xyc->xyc_csr & XYC_ERR) {
   1739  1.35.6.2  nathanw 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
   1740  1.35.6.2  nathanw 	}
   1741  1.35.6.2  nathanw 	if (xyc->xyc_csr & XYC_IPND) {
   1742  1.35.6.2  nathanw 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
   1743  1.35.6.2  nathanw 	}
   1744  1.35.6.2  nathanw 
   1745  1.35.6.2  nathanw 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
   1746  1.35.6.2  nathanw 		iorq = xycsc->xy_chain[rq];
   1747  1.35.6.2  nathanw 		if (iorq == NULL) break; /* done ! */
   1748  1.35.6.2  nathanw 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
   1749  1.35.6.2  nathanw 			continue;	/* free, or done */
   1750  1.35.6.2  nathanw 		iopb = iorq->iopb;
   1751  1.35.6.2  nathanw 		if (iopb->done == 0)
   1752  1.35.6.2  nathanw 			continue;	/* not done yet */
   1753  1.35.6.2  nathanw 
   1754  1.35.6.2  nathanw 		comm = iopb->com;
   1755  1.35.6.2  nathanw 		errs = iopb->errs;
   1756  1.35.6.2  nathanw 
   1757  1.35.6.2  nathanw 		if (errs)
   1758  1.35.6.2  nathanw 			iorq->errno = iopb->errno;
   1759  1.35.6.2  nathanw 		else
   1760  1.35.6.2  nathanw 			iorq->errno = 0;
   1761  1.35.6.2  nathanw 
   1762  1.35.6.2  nathanw 		/* handle non-fatal errors */
   1763  1.35.6.2  nathanw 
   1764  1.35.6.2  nathanw 		if (errs &&
   1765  1.35.6.2  nathanw 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
   1766  1.35.6.2  nathanw 			continue;	/* AOK: we resubmitted it */
   1767  1.35.6.2  nathanw 
   1768  1.35.6.2  nathanw 
   1769  1.35.6.2  nathanw 		/* this iorq is now done (hasn't been restarted or anything) */
   1770  1.35.6.2  nathanw 
   1771  1.35.6.2  nathanw 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1772  1.35.6.2  nathanw 			xyc_perror(iorq, iopb, 0);
   1773  1.35.6.2  nathanw 
   1774  1.35.6.2  nathanw 		/* now, if read/write check to make sure we got all the data
   1775  1.35.6.2  nathanw 		 * we needed. (this may not be the case if we got an error in
   1776  1.35.6.2  nathanw 		 * the middle of a multisector request).   */
   1777  1.35.6.2  nathanw 
   1778  1.35.6.2  nathanw 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
   1779  1.35.6.2  nathanw 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
   1780  1.35.6.2  nathanw 			/* we just successfully processed a bad144 sector
   1781  1.35.6.2  nathanw 			 * note: if we are in bad 144 mode, the pointers have
   1782  1.35.6.2  nathanw 			 * been advanced already (see above) and are pointing
   1783  1.35.6.2  nathanw 			 * at the bad144 sector.   to exit bad144 mode, we
   1784  1.35.6.2  nathanw 			 * must advance the pointers 1 sector and issue a new
   1785  1.35.6.2  nathanw 			 * request if there are still sectors left to process
   1786  1.35.6.2  nathanw 			 *
   1787  1.35.6.2  nathanw 			 */
   1788  1.35.6.2  nathanw 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
   1789  1.35.6.2  nathanw 
   1790  1.35.6.2  nathanw 			/* exit b144 mode */
   1791  1.35.6.2  nathanw 			iorq->mode = iorq->mode & (~XY_MODE_B144);
   1792  1.35.6.2  nathanw 
   1793  1.35.6.2  nathanw 			if (iorq->sectcnt) {	/* more to go! */
   1794  1.35.6.2  nathanw 				iorq->lasterror = iorq->errno = iopb->errno = 0;
   1795  1.35.6.2  nathanw 				iopb->errs = iopb->done = 0;
   1796  1.35.6.2  nathanw 				iorq->tries = 0;
   1797  1.35.6.2  nathanw 				iopb->scnt = iorq->sectcnt;
   1798  1.35.6.2  nathanw 				iopb->cyl = iorq->blockno /
   1799  1.35.6.2  nathanw 						iorq->xy->sectpercyl;
   1800  1.35.6.2  nathanw 				iopb->head =
   1801  1.35.6.2  nathanw 					(iorq->blockno / iorq->xy->nhead) %
   1802  1.35.6.2  nathanw 						iorq->xy->nhead;
   1803  1.35.6.2  nathanw 				iopb->sect = iorq->blockno % XYFM_BPS;
   1804  1.35.6.2  nathanw 				addr = dvma_kvtopa(iorq->dbuf, xycsc->bustype);
   1805  1.35.6.2  nathanw 				iopb->dataa = (addr & 0xffff);
   1806  1.35.6.2  nathanw 				iopb->datar = ((addr & 0xff0000) >> 16);
   1807  1.35.6.2  nathanw 				/* will resubit at end */
   1808  1.35.6.2  nathanw 				continue;
   1809  1.35.6.2  nathanw 			}
   1810  1.35.6.2  nathanw 		}
   1811  1.35.6.2  nathanw 		/* final cleanup, totally done with this request */
   1812  1.35.6.2  nathanw 
   1813  1.35.6.2  nathanw 		switch (XY_STATE(iorq->mode)) {
   1814  1.35.6.2  nathanw 		case XY_SUB_NORM:
   1815  1.35.6.2  nathanw 			bp = iorq->buf;
   1816  1.35.6.2  nathanw 			if (errs) {
   1817  1.35.6.2  nathanw 				bp->b_error = EIO;
   1818  1.35.6.2  nathanw 				bp->b_flags |= B_ERROR;
   1819  1.35.6.2  nathanw 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
   1820  1.35.6.2  nathanw 			} else {
   1821  1.35.6.2  nathanw 				bp->b_resid = 0;	/* done */
   1822  1.35.6.2  nathanw 			}
   1823  1.35.6.2  nathanw 			/* Sun3: map/unmap regardless of B_PHYS */
   1824  1.35.6.2  nathanw 			dvma_mapout(iorq->dbufbase,
   1825  1.35.6.2  nathanw 					    iorq->buf->b_bcount);
   1826  1.35.6.2  nathanw 			(void)BUFQ_GET(&iorq->xy->xyq);
   1827  1.35.6.2  nathanw 			disk_unbusy(&iorq->xy->sc_dk,
   1828  1.35.6.2  nathanw 			    (bp->b_bcount - bp->b_resid));
   1829  1.35.6.2  nathanw 			iorq->mode = XY_SUB_FREE;
   1830  1.35.6.2  nathanw 			biodone(bp);
   1831  1.35.6.2  nathanw 			break;
   1832  1.35.6.2  nathanw 		case XY_SUB_WAIT:
   1833  1.35.6.2  nathanw 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1834  1.35.6.2  nathanw 			wakeup(iorq);
   1835  1.35.6.2  nathanw 			break;
   1836  1.35.6.2  nathanw 		case XY_SUB_POLL:
   1837  1.35.6.2  nathanw 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1838  1.35.6.2  nathanw 			break;
   1839  1.35.6.2  nathanw 		}
   1840  1.35.6.2  nathanw 	}
   1841  1.35.6.2  nathanw 
   1842  1.35.6.2  nathanw 	return (XY_ERR_AOK);
   1843  1.35.6.2  nathanw }
   1844  1.35.6.2  nathanw 
   1845  1.35.6.2  nathanw /*
   1846  1.35.6.2  nathanw  * xyc_perror: print error.
   1847  1.35.6.2  nathanw  * - if still_trying is true: we got an error, retried and got a
   1848  1.35.6.2  nathanw  *   different error.  in that case lasterror is the old error,
   1849  1.35.6.2  nathanw  *   and errno is the new one.
   1850  1.35.6.2  nathanw  * - if still_trying is not true, then if we ever had an error it
   1851  1.35.6.2  nathanw  *   is in lasterror. also, if iorq->errno == 0, then we recovered
   1852  1.35.6.2  nathanw  *   from that error (otherwise iorq->errno == iorq->lasterror).
   1853  1.35.6.2  nathanw  */
   1854  1.35.6.2  nathanw void
   1855  1.35.6.2  nathanw xyc_perror(iorq, iopb, still_trying)
   1856  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1857  1.35.6.2  nathanw 	struct xy_iopb *iopb;
   1858  1.35.6.2  nathanw 	int     still_trying;
   1859  1.35.6.2  nathanw 
   1860  1.35.6.2  nathanw {
   1861  1.35.6.2  nathanw 
   1862  1.35.6.2  nathanw 	int     error = iorq->lasterror;
   1863  1.35.6.2  nathanw 
   1864  1.35.6.2  nathanw 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
   1865  1.35.6.2  nathanw 	    : iorq->xyc->sc_dev.dv_xname);
   1866  1.35.6.2  nathanw 	if (iorq->buf)
   1867  1.35.6.2  nathanw 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
   1868  1.35.6.2  nathanw 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
   1869  1.35.6.2  nathanw 		printf("%s %d/%d/%d: ",
   1870  1.35.6.2  nathanw 			(iopb->com == XYCMD_RD) ? "read" : "write",
   1871  1.35.6.2  nathanw 			iopb->cyl, iopb->head, iopb->sect);
   1872  1.35.6.2  nathanw 	printf("%s", xyc_e2str(error));
   1873  1.35.6.2  nathanw 
   1874  1.35.6.2  nathanw 	if (still_trying)
   1875  1.35.6.2  nathanw 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
   1876  1.35.6.2  nathanw 	else
   1877  1.35.6.2  nathanw 		if (iorq->errno == 0)
   1878  1.35.6.2  nathanw 			printf(" [recovered in %d tries]", iorq->tries);
   1879  1.35.6.2  nathanw 
   1880  1.35.6.2  nathanw 	printf("\n");
   1881  1.35.6.2  nathanw }
   1882  1.35.6.2  nathanw 
   1883  1.35.6.2  nathanw /*
   1884  1.35.6.2  nathanw  * xyc_error: non-fatal error encountered... recover.
   1885  1.35.6.2  nathanw  * return AOK if resubmitted, return FAIL if this iopb is done
   1886  1.35.6.2  nathanw  */
   1887  1.35.6.2  nathanw int
   1888  1.35.6.2  nathanw xyc_error(xycsc, iorq, iopb, comm)
   1889  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   1890  1.35.6.2  nathanw 	struct xy_iorq *iorq;
   1891  1.35.6.2  nathanw 	struct xy_iopb *iopb;
   1892  1.35.6.2  nathanw 	int     comm;
   1893  1.35.6.2  nathanw 
   1894  1.35.6.2  nathanw {
   1895  1.35.6.2  nathanw 	int     errno = iorq->errno;
   1896  1.35.6.2  nathanw 	int     erract = xyc_entoact(errno);
   1897  1.35.6.2  nathanw 	int     oldmode, advance, i;
   1898  1.35.6.2  nathanw 
   1899  1.35.6.2  nathanw 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
   1900  1.35.6.2  nathanw 		oldmode = iorq->mode;
   1901  1.35.6.2  nathanw 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
   1902  1.35.6.2  nathanw 		/* make xyc_start ignore us */
   1903  1.35.6.2  nathanw 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
   1904  1.35.6.2  nathanw 		iorq->mode = oldmode;
   1905  1.35.6.2  nathanw 	}
   1906  1.35.6.2  nathanw 	/* check for read/write to a sector in bad144 table if bad: redirect
   1907  1.35.6.2  nathanw 	 * request to bad144 area */
   1908  1.35.6.2  nathanw 
   1909  1.35.6.2  nathanw 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
   1910  1.35.6.2  nathanw 	    (iorq->mode & XY_MODE_B144) == 0) {
   1911  1.35.6.2  nathanw 		advance = iorq->sectcnt - iopb->scnt;
   1912  1.35.6.2  nathanw 		XYC_ADVANCE(iorq, advance);
   1913  1.35.6.2  nathanw 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
   1914  1.35.6.2  nathanw 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
   1915  1.35.6.2  nathanw 			    iorq->blockno % iorq->xy->nsect)) != -1) {
   1916  1.35.6.2  nathanw 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
   1917  1.35.6.2  nathanw 							 * redirect */
   1918  1.35.6.2  nathanw 			iopb->errno = iopb->done = iopb->errs = 0;
   1919  1.35.6.2  nathanw 			iopb->scnt = 1;
   1920  1.35.6.2  nathanw 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
   1921  1.35.6.2  nathanw 			/* second to last acyl */
   1922  1.35.6.2  nathanw 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
   1923  1.35.6.2  nathanw 								 * standard */
   1924  1.35.6.2  nathanw 			iopb->head = i / iorq->xy->nhead;
   1925  1.35.6.2  nathanw 			iopb->sect = i % iorq->xy->nhead;
   1926  1.35.6.2  nathanw 			/* will resubmit when we come out of remove_iorq */
   1927  1.35.6.2  nathanw 			return (XY_ERR_AOK);	/* recovered! */
   1928  1.35.6.2  nathanw 		}
   1929  1.35.6.2  nathanw 	}
   1930  1.35.6.2  nathanw 
   1931  1.35.6.2  nathanw 	/*
   1932  1.35.6.2  nathanw 	 * it isn't a bad144 sector, must be real error! see if we can retry
   1933  1.35.6.2  nathanw 	 * it?
   1934  1.35.6.2  nathanw 	 */
   1935  1.35.6.2  nathanw 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1936  1.35.6.2  nathanw 		xyc_perror(iorq, iopb, 1);	/* inform of error state
   1937  1.35.6.2  nathanw 						 * change */
   1938  1.35.6.2  nathanw 	iorq->lasterror = errno;
   1939  1.35.6.2  nathanw 
   1940  1.35.6.2  nathanw 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
   1941  1.35.6.2  nathanw 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
   1942  1.35.6.2  nathanw 		iorq->tries++;
   1943  1.35.6.2  nathanw 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
   1944  1.35.6.2  nathanw 		/* will resubmit at end of remove_iorq */
   1945  1.35.6.2  nathanw 		return (XY_ERR_AOK);	/* recovered! */
   1946  1.35.6.2  nathanw 	}
   1947  1.35.6.2  nathanw 
   1948  1.35.6.2  nathanw 	/* failed to recover from this error */
   1949  1.35.6.2  nathanw 	return (XY_ERR_FAIL);
   1950  1.35.6.2  nathanw }
   1951  1.35.6.2  nathanw 
   1952  1.35.6.2  nathanw /*
   1953  1.35.6.2  nathanw  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
   1954  1.35.6.2  nathanw  */
   1955  1.35.6.2  nathanw void
   1956  1.35.6.2  nathanw xyc_tick(arg)
   1957  1.35.6.2  nathanw 	void   *arg;
   1958  1.35.6.2  nathanw 
   1959  1.35.6.2  nathanw {
   1960  1.35.6.2  nathanw 	struct xyc_softc *xycsc = arg;
   1961  1.35.6.2  nathanw 	int     lcv, s, reset = 0;
   1962  1.35.6.2  nathanw 
   1963  1.35.6.2  nathanw 	/* reduce ttl for each request if one goes to zero, reset xyc */
   1964  1.35.6.2  nathanw 	s = splbio();
   1965  1.35.6.2  nathanw 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1966  1.35.6.2  nathanw 		if (xycsc->reqs[lcv].mode == 0 ||
   1967  1.35.6.2  nathanw 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
   1968  1.35.6.2  nathanw 			continue;
   1969  1.35.6.2  nathanw 		xycsc->reqs[lcv].ttl--;
   1970  1.35.6.2  nathanw 		if (xycsc->reqs[lcv].ttl == 0)
   1971  1.35.6.2  nathanw 			reset = 1;
   1972  1.35.6.2  nathanw 	}
   1973  1.35.6.2  nathanw 	if (reset) {
   1974  1.35.6.2  nathanw 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
   1975  1.35.6.2  nathanw 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
   1976  1.35.6.2  nathanw 	}
   1977  1.35.6.2  nathanw 	splx(s);
   1978  1.35.6.2  nathanw 
   1979  1.35.6.2  nathanw 	/* until next time */
   1980  1.35.6.2  nathanw 
   1981  1.35.6.2  nathanw 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
   1982  1.35.6.2  nathanw }
   1983  1.35.6.2  nathanw 
   1984  1.35.6.2  nathanw /*
   1985  1.35.6.2  nathanw  * xyc_ioctlcmd: this function provides a user level interface to the
   1986  1.35.6.2  nathanw  * controller via ioctl.   this allows "format" programs to be written
   1987  1.35.6.2  nathanw  * in user code, and is also useful for some debugging.   we return
   1988  1.35.6.2  nathanw  * an error code.   called at user priority.
   1989  1.35.6.2  nathanw  *
   1990  1.35.6.2  nathanw  * XXX missing a few commands (see the 7053 driver for ideas)
   1991  1.35.6.2  nathanw  */
   1992  1.35.6.2  nathanw int
   1993  1.35.6.2  nathanw xyc_ioctlcmd(xy, dev, xio)
   1994  1.35.6.2  nathanw 	struct xy_softc *xy;
   1995  1.35.6.2  nathanw 	dev_t   dev;
   1996  1.35.6.2  nathanw 	struct xd_iocmd *xio;
   1997  1.35.6.2  nathanw 
   1998  1.35.6.2  nathanw {
   1999  1.35.6.2  nathanw 	int     s, err, rqno;
   2000  1.35.6.2  nathanw 	void * dvmabuf = NULL;
   2001  1.35.6.2  nathanw 	struct xyc_softc *xycsc;
   2002  1.35.6.2  nathanw 
   2003  1.35.6.2  nathanw 	/* check sanity of requested command */
   2004  1.35.6.2  nathanw 
   2005  1.35.6.2  nathanw 	switch (xio->cmd) {
   2006  1.35.6.2  nathanw 
   2007  1.35.6.2  nathanw 	case XYCMD_NOP:	/* no op: everything should be zero */
   2008  1.35.6.2  nathanw 		if (xio->subfn || xio->dptr || xio->dlen ||
   2009  1.35.6.2  nathanw 		    xio->block || xio->sectcnt)
   2010  1.35.6.2  nathanw 			return (EINVAL);
   2011  1.35.6.2  nathanw 		break;
   2012  1.35.6.2  nathanw 
   2013  1.35.6.2  nathanw 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   2014  1.35.6.2  nathanw 	case XYCMD_WR:
   2015  1.35.6.2  nathanw 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   2016  1.35.6.2  nathanw 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
   2017  1.35.6.2  nathanw 			return (EINVAL);
   2018  1.35.6.2  nathanw 		break;
   2019  1.35.6.2  nathanw 
   2020  1.35.6.2  nathanw 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
   2021  1.35.6.2  nathanw 		return (EINVAL);
   2022  1.35.6.2  nathanw 
   2023  1.35.6.2  nathanw 		break;
   2024  1.35.6.2  nathanw 
   2025  1.35.6.2  nathanw 	default:
   2026  1.35.6.2  nathanw 		return (EINVAL);/* ??? */
   2027  1.35.6.2  nathanw 	}
   2028  1.35.6.2  nathanw 
   2029  1.35.6.2  nathanw 	/* create DVMA buffer for request if needed */
   2030  1.35.6.2  nathanw 
   2031  1.35.6.2  nathanw 	if (xio->dlen) {
   2032  1.35.6.2  nathanw 		dvmabuf = dvma_malloc(xio->dlen);
   2033  1.35.6.2  nathanw 		if (xio->cmd == XYCMD_WR) {
   2034  1.35.6.2  nathanw 			err = copyin(xio->dptr, dvmabuf, xio->dlen);
   2035  1.35.6.2  nathanw 			if (err) {
   2036  1.35.6.2  nathanw 				dvma_free(dvmabuf, xio->dlen);
   2037  1.35.6.2  nathanw 				return (err);
   2038  1.35.6.2  nathanw 			}
   2039  1.35.6.2  nathanw 		}
   2040  1.35.6.2  nathanw 	}
   2041  1.35.6.2  nathanw 	/* do it! */
   2042  1.35.6.2  nathanw 
   2043  1.35.6.2  nathanw 	err = 0;
   2044  1.35.6.2  nathanw 	xycsc = xy->parent;
   2045  1.35.6.2  nathanw 	s = splbio();
   2046  1.35.6.2  nathanw 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
   2047  1.35.6.2  nathanw 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
   2048  1.35.6.2  nathanw 	if (rqno == XY_ERR_FAIL) {
   2049  1.35.6.2  nathanw 		err = EIO;
   2050  1.35.6.2  nathanw 		goto done;
   2051  1.35.6.2  nathanw 	}
   2052  1.35.6.2  nathanw 	xio->errno = xycsc->ciorq->errno;
   2053  1.35.6.2  nathanw 	xio->tries = xycsc->ciorq->tries;
   2054  1.35.6.2  nathanw 	XYC_DONE(xycsc, err);
   2055  1.35.6.2  nathanw 
   2056  1.35.6.2  nathanw 	if (xio->cmd == XYCMD_RD)
   2057  1.35.6.2  nathanw 		err = copyout(dvmabuf, xio->dptr, xio->dlen);
   2058  1.35.6.2  nathanw 
   2059  1.35.6.2  nathanw done:
   2060  1.35.6.2  nathanw 	splx(s);
   2061  1.35.6.2  nathanw 	if (dvmabuf)
   2062  1.35.6.2  nathanw 		dvma_free(dvmabuf, xio->dlen);
   2063  1.35.6.2  nathanw 	return (err);
   2064  1.35.6.2  nathanw }
   2065  1.35.6.2  nathanw 
   2066  1.35.6.2  nathanw /*
   2067  1.35.6.2  nathanw  * xyc_e2str: convert error code number into an error string
   2068  1.35.6.2  nathanw  */
   2069  1.35.6.2  nathanw char *
   2070  1.35.6.2  nathanw xyc_e2str(no)
   2071  1.35.6.2  nathanw 	int     no;
   2072  1.35.6.2  nathanw {
   2073  1.35.6.2  nathanw 	switch (no) {
   2074  1.35.6.2  nathanw 	case XY_ERR_FAIL:
   2075  1.35.6.2  nathanw 		return ("Software fatal error");
   2076  1.35.6.2  nathanw 	case XY_ERR_DERR:
   2077  1.35.6.2  nathanw 		return ("DOUBLE ERROR");
   2078  1.35.6.2  nathanw 	case XY_ERR_AOK:
   2079  1.35.6.2  nathanw 		return ("Successful completion");
   2080  1.35.6.2  nathanw 	case XY_ERR_IPEN:
   2081  1.35.6.2  nathanw 		return("Interrupt pending");
   2082  1.35.6.2  nathanw 	case XY_ERR_BCFL:
   2083  1.35.6.2  nathanw 		return("Busy conflict");
   2084  1.35.6.2  nathanw 	case XY_ERR_TIMO:
   2085  1.35.6.2  nathanw 		return("Operation timeout");
   2086  1.35.6.2  nathanw 	case XY_ERR_NHDR:
   2087  1.35.6.2  nathanw 		return("Header not found");
   2088  1.35.6.2  nathanw 	case XY_ERR_HARD:
   2089  1.35.6.2  nathanw 		return("Hard ECC error");
   2090  1.35.6.2  nathanw 	case XY_ERR_ICYL:
   2091  1.35.6.2  nathanw 		return("Illegal cylinder address");
   2092  1.35.6.2  nathanw 	case XY_ERR_ISEC:
   2093  1.35.6.2  nathanw 		return("Illegal sector address");
   2094  1.35.6.2  nathanw 	case XY_ERR_SMAL:
   2095  1.35.6.2  nathanw 		return("Last sector too small");
   2096  1.35.6.2  nathanw 	case XY_ERR_SACK:
   2097  1.35.6.2  nathanw 		return("Slave ACK error (non-existent memory)");
   2098  1.35.6.2  nathanw 	case XY_ERR_CHER:
   2099  1.35.6.2  nathanw 		return("Cylinder and head/header error");
   2100  1.35.6.2  nathanw 	case XY_ERR_SRTR:
   2101  1.35.6.2  nathanw 		return("Auto-seek retry successful");
   2102  1.35.6.2  nathanw 	case XY_ERR_WPRO:
   2103  1.35.6.2  nathanw 		return("Write-protect error");
   2104  1.35.6.2  nathanw 	case XY_ERR_UIMP:
   2105  1.35.6.2  nathanw 		return("Unimplemented command");
   2106  1.35.6.2  nathanw 	case XY_ERR_DNRY:
   2107  1.35.6.2  nathanw 		return("Drive not ready");
   2108  1.35.6.2  nathanw 	case XY_ERR_SZER:
   2109  1.35.6.2  nathanw 		return("Sector count zero");
   2110  1.35.6.2  nathanw 	case XY_ERR_DFLT:
   2111  1.35.6.2  nathanw 		return("Drive faulted");
   2112  1.35.6.2  nathanw 	case XY_ERR_ISSZ:
   2113  1.35.6.2  nathanw 		return("Illegal sector size");
   2114  1.35.6.2  nathanw 	case XY_ERR_SLTA:
   2115  1.35.6.2  nathanw 		return("Self test A");
   2116  1.35.6.2  nathanw 	case XY_ERR_SLTB:
   2117  1.35.6.2  nathanw 		return("Self test B");
   2118  1.35.6.2  nathanw 	case XY_ERR_SLTC:
   2119  1.35.6.2  nathanw 		return("Self test C");
   2120  1.35.6.2  nathanw 	case XY_ERR_SOFT:
   2121  1.35.6.2  nathanw 		return("Soft ECC error");
   2122  1.35.6.2  nathanw 	case XY_ERR_SFOK:
   2123  1.35.6.2  nathanw 		return("Soft ECC error recovered");
   2124  1.35.6.2  nathanw 	case XY_ERR_IHED:
   2125  1.35.6.2  nathanw 		return("Illegal head");
   2126  1.35.6.2  nathanw 	case XY_ERR_DSEQ:
   2127  1.35.6.2  nathanw 		return("Disk sequencer error");
   2128  1.35.6.2  nathanw 	case XY_ERR_SEEK:
   2129  1.35.6.2  nathanw 		return("Seek error");
   2130  1.35.6.2  nathanw 	default:
   2131  1.35.6.2  nathanw 		return ("Unknown error");
   2132  1.35.6.2  nathanw 	}
   2133  1.35.6.2  nathanw }
   2134  1.35.6.2  nathanw 
   2135  1.35.6.2  nathanw int
   2136  1.35.6.2  nathanw xyc_entoact(errno)
   2137  1.35.6.2  nathanw 
   2138  1.35.6.2  nathanw int errno;
   2139  1.35.6.2  nathanw 
   2140  1.35.6.2  nathanw {
   2141  1.35.6.2  nathanw   switch (errno) {
   2142  1.35.6.2  nathanw     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
   2143  1.35.6.2  nathanw     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
   2144  1.35.6.2  nathanw     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
   2145  1.35.6.2  nathanw     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
   2146  1.35.6.2  nathanw     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
   2147  1.35.6.2  nathanw 
   2148  1.35.6.2  nathanw 	return(XY_ERA_PROG); /* program error ! */
   2149  1.35.6.2  nathanw 
   2150  1.35.6.2  nathanw     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
   2151  1.35.6.2  nathanw     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
   2152  1.35.6.2  nathanw     case XY_ERR_SOFT:
   2153  1.35.6.2  nathanw 
   2154  1.35.6.2  nathanw 	return(XY_ERA_HARD); /* hard error, retry */
   2155  1.35.6.2  nathanw 
   2156  1.35.6.2  nathanw     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
   2157  1.35.6.2  nathanw 
   2158  1.35.6.2  nathanw 	return(XY_ERA_RSET); /* hard error reset */
   2159  1.35.6.2  nathanw 
   2160  1.35.6.2  nathanw     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
   2161  1.35.6.2  nathanw 
   2162  1.35.6.2  nathanw 	return(XY_ERA_SOFT); /* an FYI error */
   2163  1.35.6.2  nathanw 
   2164  1.35.6.2  nathanw     case XY_ERR_WPRO:
   2165  1.35.6.2  nathanw 
   2166  1.35.6.2  nathanw 	return(XY_ERA_WPRO); /* write protect */
   2167  1.35.6.2  nathanw   }
   2168  1.35.6.2  nathanw 
   2169  1.35.6.2  nathanw   return(XY_ERA_PROG); /* ??? */
   2170  1.35.6.2  nathanw }
   2171