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