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