Home | History | Annotate | Line # | Download | only in mca
edc_mca.c revision 1.9.4.1
      1  1.9.4.1   thorpej /*	$NetBSD: edc_mca.c,v 1.9.4.1 2002/01/10 19:55:58 thorpej Exp $	*/
      2      1.1  jdolecek 
      3      1.1  jdolecek /*
      4      1.1  jdolecek  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5      1.1  jdolecek  *
      6      1.1  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      7      1.1  jdolecek  * by Jaromir Dolecek.
      8      1.1  jdolecek  *
      9      1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     10      1.1  jdolecek  * modification, are permitted provided that the following conditions
     11      1.1  jdolecek  * are met:
     12      1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     13      1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     14      1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     17      1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     18      1.1  jdolecek  *    must display the following acknowledgement:
     19      1.1  jdolecek  *        This product includes software developed by the NetBSD
     20      1.1  jdolecek  *        Foundation, Inc. and its contributors.
     21      1.1  jdolecek  * 4. The name of the author may not be used to endorse or promote products
     22      1.1  jdolecek  *    derived from this software without specific prior written permission.
     23      1.1  jdolecek  *
     24      1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25      1.1  jdolecek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26      1.1  jdolecek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27      1.1  jdolecek  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28      1.1  jdolecek  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29      1.1  jdolecek  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30      1.1  jdolecek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31      1.1  jdolecek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32      1.1  jdolecek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33      1.1  jdolecek  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  jdolecek  */
     35      1.1  jdolecek 
     36      1.1  jdolecek /*
     37      1.1  jdolecek  * Driver for MCA ESDI controllers and disks conforming to IBM DASD
     38      1.1  jdolecek  * spec.
     39      1.1  jdolecek  *
     40      1.1  jdolecek  * The driver was written with DASD Storage Interface Specification
     41      1.1  jdolecek  * for MCA rev. 2.2 in hands, thanks to Scott Telford <st (at) epcc.ed.ac.uk>.
     42      1.1  jdolecek  *
     43      1.1  jdolecek  * TODO:
     44      1.1  jdolecek  * - improve error recovery
     45  1.9.4.1   thorpej  *   Issue soft reset on error or timeout?
     46  1.9.4.1   thorpej  * - test with > 1 disk (this is supported by some controllers)
     47      1.1  jdolecek  * - test with > 1 ESDI controller in machine; shared interrupts
     48      1.1  jdolecek  *   necessary for this to work should be supported - edc_intr() specifically
     49      1.1  jdolecek  *   checks if the interrupt is for this controller
     50      1.1  jdolecek  */
     51      1.1  jdolecek 
     52  1.9.4.1   thorpej #include <sys/cdefs.h>
     53  1.9.4.1   thorpej __KERNEL_RCSID(0, "$NetBSD: edc_mca.c,v 1.9.4.1 2002/01/10 19:55:58 thorpej Exp $");
     54  1.9.4.1   thorpej 
     55      1.1  jdolecek #include "rnd.h"
     56      1.1  jdolecek 
     57      1.1  jdolecek #include <sys/param.h>
     58      1.1  jdolecek #include <sys/systm.h>
     59      1.1  jdolecek #include <sys/errno.h>
     60      1.1  jdolecek #include <sys/device.h>
     61      1.1  jdolecek #include <sys/malloc.h>
     62      1.1  jdolecek #include <sys/endian.h>
     63      1.1  jdolecek #include <sys/disklabel.h>
     64      1.1  jdolecek #include <sys/disk.h>
     65      1.1  jdolecek #include <sys/syslog.h>
     66      1.1  jdolecek #include <sys/proc.h>
     67      1.1  jdolecek #include <sys/vnode.h>
     68      1.1  jdolecek #include <sys/kernel.h>
     69  1.9.4.1   thorpej #include <sys/kthread.h>
     70      1.1  jdolecek #if NRND > 0
     71      1.1  jdolecek #include <sys/rnd.h>
     72      1.1  jdolecek #endif
     73      1.1  jdolecek 
     74      1.1  jdolecek #include <machine/bus.h>
     75      1.1  jdolecek #include <machine/intr.h>
     76      1.1  jdolecek 
     77      1.1  jdolecek #include <dev/mca/mcareg.h>
     78      1.1  jdolecek #include <dev/mca/mcavar.h>
     79      1.1  jdolecek #include <dev/mca/mcadevs.h>
     80      1.1  jdolecek 
     81      1.1  jdolecek #include <dev/mca/edcreg.h>
     82      1.1  jdolecek #include <dev/mca/edvar.h>
     83      1.1  jdolecek #include <dev/mca/edcvar.h>
     84      1.1  jdolecek 
     85      1.9  jdolecek #define EDC_ATTN_MAXTRIES	10000	/* How many times check for unbusy */
     86  1.9.4.1   thorpej #define EDC_MAX_CMD_RES_LEN	8
     87      1.9  jdolecek 
     88      1.1  jdolecek struct edc_mca_softc {
     89      1.1  jdolecek 	struct device sc_dev;
     90      1.1  jdolecek 
     91      1.1  jdolecek 	bus_space_tag_t	sc_iot;
     92      1.1  jdolecek 	bus_space_handle_t sc_ioh;
     93      1.1  jdolecek 
     94  1.9.4.1   thorpej 	/* DMA related stuff */
     95      1.1  jdolecek 	bus_dma_tag_t sc_dmat;		/* DMA tag as passed by parent */
     96  1.9.4.1   thorpej 	bus_dmamap_t  sc_dmamap_xfer;	/* transfer dma map */
     97      1.1  jdolecek 
     98      1.1  jdolecek 	void	*sc_ih;				/* interrupt handle */
     99      1.1  jdolecek 
    100      1.1  jdolecek 	int	sc_flags;
    101      1.1  jdolecek #define	DASD_QUIET	0x01		/* don't dump cmd error info */
    102  1.9.4.1   thorpej 
    103      1.9  jdolecek #define DASD_MAXDEVS	8
    104      1.1  jdolecek 	struct ed_softc *sc_ed[DASD_MAXDEVS];
    105  1.9.4.1   thorpej 	int sc_maxdevs;			/* max number of disks attached to this
    106  1.9.4.1   thorpej 					 * controller */
    107  1.9.4.1   thorpej 
    108  1.9.4.1   thorpej 	/* I/O results variables */
    109  1.9.4.1   thorpej 	volatile int sc_error;
    110  1.9.4.1   thorpej 	volatile int sc_resblk;		/* residual block count */
    111      1.1  jdolecek };
    112      1.1  jdolecek 
    113      1.1  jdolecek int	edc_mca_probe	__P((struct device *, struct cfdata *, void *));
    114      1.1  jdolecek void	edc_mca_attach	__P((struct device *, struct device *, void *));
    115      1.1  jdolecek 
    116      1.1  jdolecek struct cfattach edc_mca_ca = {
    117      1.1  jdolecek 	sizeof(struct edc_mca_softc), edc_mca_probe, edc_mca_attach
    118      1.1  jdolecek };
    119      1.1  jdolecek 
    120      1.1  jdolecek static int	edc_intr __P((void *));
    121  1.9.4.1   thorpej static void	edc_dump_status_block __P((struct edc_mca_softc *,
    122  1.9.4.1   thorpej 		    u_int16_t *, int));
    123      1.1  jdolecek static int	edc_do_attn __P((struct edc_mca_softc *, int, int, int));
    124  1.9.4.1   thorpej static int	edc_cmd_wait __P((struct edc_mca_softc *, int, int));
    125  1.9.4.1   thorpej static void	edcworker __P((void *));
    126  1.9.4.1   thorpej static void	edc_spawn_worker __P((void *));
    127      1.1  jdolecek 
    128      1.1  jdolecek int
    129      1.1  jdolecek edc_mca_probe(parent, match, aux)
    130      1.1  jdolecek 	struct device *parent;
    131      1.1  jdolecek 	struct cfdata *match;
    132      1.1  jdolecek 	void *aux;
    133      1.1  jdolecek {
    134      1.1  jdolecek 	struct mca_attach_args *ma = aux;
    135      1.1  jdolecek 
    136      1.1  jdolecek 	switch (ma->ma_id) {
    137      1.1  jdolecek 	case MCA_PRODUCT_IBM_ESDIC:
    138      1.1  jdolecek 	case MCA_PRODUCT_IBM_ESDIC_IG:
    139      1.1  jdolecek 		return (1);
    140      1.1  jdolecek 	default:
    141      1.1  jdolecek 		return (0);
    142      1.1  jdolecek 	}
    143      1.1  jdolecek }
    144      1.1  jdolecek 
    145      1.1  jdolecek void
    146      1.1  jdolecek edc_mca_attach(parent, self, aux)
    147      1.1  jdolecek 	struct device *parent, *self;
    148      1.1  jdolecek 	void *aux;
    149      1.1  jdolecek {
    150      1.1  jdolecek 	struct edc_mca_softc *sc = (void *) self;
    151      1.1  jdolecek 	struct mca_attach_args *ma = aux;
    152  1.9.4.1   thorpej 	struct ed_attach_args eda;
    153      1.1  jdolecek 	int pos2, pos3, pos4;
    154      1.1  jdolecek 	int irq, drq, iobase;
    155      1.1  jdolecek 	const char *typestr;
    156  1.9.4.1   thorpej 	int devno, error;
    157      1.1  jdolecek 
    158      1.1  jdolecek 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
    159      1.1  jdolecek 	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
    160      1.1  jdolecek 	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
    161      1.1  jdolecek 
    162      1.1  jdolecek 	/*
    163      1.1  jdolecek 	 * POS register 2: (adf pos0)
    164      1.1  jdolecek 	 *
    165      1.1  jdolecek 	 * 7 6 5 4 3 2 1 0
    166      1.1  jdolecek 	 *   \ \____/  \ \__ enable: 0=adapter disabled, 1=adapter enabled
    167      1.1  jdolecek 	 *    \     \   \___ Primary/Alternate Port Adresses:
    168      1.1  jdolecek 	 *     \     \		0=0x3510-3517 1=0x3518-0x351f
    169      1.1  jdolecek 	 *      \     \_____ DMA Arbitration Level: 0101=5 0110=6 0111=7
    170      1.1  jdolecek 	 *       \              0000=0 0001=1 0011=3 0100=4
    171      1.1  jdolecek 	 *        \_________ Fairness On/Off: 1=On 0=Off
    172      1.1  jdolecek 	 *
    173      1.1  jdolecek 	 * POS register 3: (adf pos1)
    174      1.1  jdolecek 	 *
    175      1.1  jdolecek 	 * 7 6 5 4 3 2 1 0
    176      1.1  jdolecek 	 * 0 0 \_/
    177      1.1  jdolecek 	 *       \__________ DMA Burst Pacing Interval: 10=24ms 11=31ms
    178      1.1  jdolecek 	 *                     01=16ms 00=Burst Disabled
    179      1.1  jdolecek 	 *
    180      1.1  jdolecek 	 * POS register 4: (adf pos2)
    181      1.1  jdolecek 	 *
    182      1.1  jdolecek 	 * 7 6 5 4 3 2 1 0
    183      1.1  jdolecek 	 *           \_/ \__ DMA Pacing Control: 1=Disabled 0=Enabled
    184      1.1  jdolecek 	 *             \____ Time to Release: 1X=6ms 01=3ms 00=Immediate
    185      1.1  jdolecek 	 *
    186      1.1  jdolecek 	 * IRQ is fixed to 14 (0x0e).
    187      1.1  jdolecek 	 */
    188      1.1  jdolecek 
    189      1.1  jdolecek 	switch (ma->ma_id) {
    190      1.1  jdolecek 	case MCA_PRODUCT_IBM_ESDIC:
    191      1.1  jdolecek 		typestr = "IBM ESDI Fixed Disk Controller";
    192      1.1  jdolecek 		break;
    193      1.1  jdolecek 	case MCA_PRODUCT_IBM_ESDIC_IG:
    194      1.1  jdolecek 		typestr = "IBM Integ. ESDI Fixed Disk & Controller";
    195      1.1  jdolecek 		break;
    196      1.1  jdolecek 	default:
    197  1.9.4.1   thorpej 		/* never reached */ ;
    198      1.1  jdolecek 	}
    199      1.1  jdolecek 
    200      1.1  jdolecek 	irq = ESDIC_IRQ;
    201      1.1  jdolecek 	iobase = (pos2 & IO_IS_ALT) ? ESDIC_IOALT : ESDIC_IOPRM;
    202      1.1  jdolecek 	drq = (pos2 & DRQ_MASK) >> 2;
    203      1.1  jdolecek 
    204      1.7  jdolecek 	printf(" slot %d irq %d drq %d: %s\n", ma->ma_slot+1,
    205      1.7  jdolecek 		irq, drq, typestr);
    206      1.6  jdolecek 
    207      1.1  jdolecek #ifdef DIAGNOSTIC
    208      1.1  jdolecek 	/*
    209      1.1  jdolecek 	 * It's not strictly necessary to check this, machine configuration
    210      1.1  jdolecek 	 * utility uses only valid adresses.
    211      1.1  jdolecek 	 */
    212      1.1  jdolecek 	if (drq == 2 || drq >= 8) {
    213      1.1  jdolecek 		printf("%s: invalid DMA Arbitration Level %d\n",
    214      1.1  jdolecek 			sc->sc_dev.dv_xname, drq);
    215      1.1  jdolecek 		return;
    216      1.1  jdolecek 	}
    217      1.1  jdolecek #endif
    218      1.1  jdolecek 
    219      1.7  jdolecek 	printf("%s: Fairness %s, Release %s, ",
    220      1.1  jdolecek 		sc->sc_dev.dv_xname,
    221      1.1  jdolecek 		(pos2 & FAIRNESS_ENABLE) ? "On" : "Off",
    222      1.1  jdolecek 		(pos4 & RELEASE_1) ? "6ms"
    223      1.7  jdolecek 				: ((pos4 & RELEASE_2) ? "3ms" : "Immediate")
    224      1.7  jdolecek 		);
    225      1.1  jdolecek 	if ((pos4 & PACING_CTRL_DISABLE) == 0) {
    226      1.1  jdolecek 		static const char * const pacint[] =
    227      1.1  jdolecek 			{ "disabled", "16ms", "24ms", "31ms"};
    228      1.1  jdolecek 		printf("DMA burst pacing interval %s\n",
    229      1.1  jdolecek 			pacint[(pos3 & PACING_INT_MASK) >> 4]);
    230      1.1  jdolecek 	} else
    231      1.1  jdolecek 		printf("DMA pacing control disabled\n");
    232      1.1  jdolecek 
    233      1.1  jdolecek 	sc->sc_iot = ma->ma_iot;
    234      1.1  jdolecek 
    235      1.1  jdolecek 	if (bus_space_map(sc->sc_iot, iobase,
    236      1.1  jdolecek 	    ESDIC_REG_NPORTS, 0, &sc->sc_ioh)) {
    237      1.1  jdolecek 		printf("%s: couldn't map registers\n",
    238      1.1  jdolecek 		    sc->sc_dev.dv_xname);
    239      1.1  jdolecek 		return;
    240      1.1  jdolecek 	}
    241      1.1  jdolecek 
    242      1.1  jdolecek 	sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_BIO, edc_intr, sc);
    243      1.1  jdolecek 	if (sc->sc_ih == NULL) {
    244      1.1  jdolecek 		printf("%s: couldn't establish interrupt handler\n",
    245      1.1  jdolecek 			sc->sc_dev.dv_xname);
    246      1.1  jdolecek 		return;
    247      1.1  jdolecek 	}
    248      1.1  jdolecek 
    249  1.9.4.1   thorpej 	/* Create a MCA DMA map, used for data transfer */
    250  1.9.4.1   thorpej 	sc->sc_dmat = ma->ma_dmat;
    251  1.9.4.1   thorpej 	if ((error = mca_dmamap_create(sc->sc_dmat, MAXPHYS,
    252  1.9.4.1   thorpej 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW | MCABUS_DMA_16BIT,
    253  1.9.4.1   thorpej 	    &sc->sc_dmamap_xfer, drq)) != 0){
    254  1.9.4.1   thorpej 		printf("%s: couldn't create DMA map - error %d\n",
    255  1.9.4.1   thorpej 			sc->sc_dev.dv_xname, error);
    256  1.9.4.1   thorpej 		return;
    257  1.9.4.1   thorpej 	}
    258  1.9.4.1   thorpej 
    259      1.1  jdolecek 	/*
    260      1.1  jdolecek 	 * Integrated ESDI controller supports only one disk, other
    261      1.1  jdolecek 	 * controllers support two disks.
    262      1.1  jdolecek 	 */
    263      1.1  jdolecek 	if (ma->ma_id == MCA_PRODUCT_IBM_ESDIC_IG)
    264  1.9.4.1   thorpej 		sc->sc_maxdevs = 1;
    265      1.1  jdolecek 	else
    266  1.9.4.1   thorpej 		sc->sc_maxdevs = 2;
    267      1.1  jdolecek 
    268      1.9  jdolecek 	/*
    269      1.9  jdolecek 	 * Reset controller and attach individual disks. ed attach routine
    270      1.9  jdolecek 	 * uses polling so that this works with interrupts disabled.
    271      1.9  jdolecek 	 */
    272      1.1  jdolecek 
    273      1.1  jdolecek 	/* Do a reset to ensure sane state after warm boot. */
    274      1.1  jdolecek 	if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_BUSY) {
    275      1.1  jdolecek 		/* hard reset */
    276      1.1  jdolecek 		printf("%s: controller busy, performing hardware reset ...\n",
    277      1.1  jdolecek 			sc->sc_dev.dv_xname);
    278      1.1  jdolecek 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR,
    279      1.1  jdolecek 			BCR_INT_ENABLE|BCR_RESET);
    280      1.1  jdolecek 	} else {
    281      1.1  jdolecek 		/* "SOFT" reset */
    282      1.1  jdolecek 		edc_do_attn(sc, ATN_RESET_ATTACHMENT, DASD_DEVNO_CONTROLLER,0);
    283      1.1  jdolecek 	}
    284      1.1  jdolecek 
    285      1.9  jdolecek 	/*
    286      1.9  jdolecek 	 * Since interrupts are disabled ATM, it's necessary
    287      1.9  jdolecek 	 * to detect the interrupt request and call edc_intr()
    288      1.9  jdolecek 	 * explicitly. See also edc_run_cmd().
    289      1.9  jdolecek 	 */
    290      1.9  jdolecek 	while(bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_BUSY) {
    291      1.9  jdolecek 		if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_INTR)
    292      1.9  jdolecek 			edc_intr(sc);
    293      1.9  jdolecek 
    294      1.9  jdolecek 		delay(100);
    295      1.9  jdolecek 	}
    296      1.1  jdolecek 
    297  1.9.4.1   thorpej 	/* be quiet during probes */
    298      1.1  jdolecek 	sc->sc_flags |= DASD_QUIET;
    299      1.1  jdolecek 
    300      1.1  jdolecek 	/* check for attached disks */
    301  1.9.4.1   thorpej 	for(devno=0; devno < sc->sc_maxdevs; devno++) {
    302  1.9.4.1   thorpej 		eda.edc_drive = devno;
    303  1.9.4.1   thorpej 		sc->sc_ed[devno] =
    304  1.9.4.1   thorpej 			(void *) config_found_sm(self, &eda, NULL, NULL);
    305  1.9.4.1   thorpej 
    306  1.9.4.1   thorpej 		/* If initialization did not succeed, NULL the pointer. */
    307  1.9.4.1   thorpej 		if (sc->sc_ed[devno]
    308  1.9.4.1   thorpej 		    && (sc->sc_ed[devno]->sc_flags & EDF_INIT) == 0)
    309  1.9.4.1   thorpej 			sc->sc_ed[devno] = NULL;
    310      1.1  jdolecek 	}
    311      1.1  jdolecek 
    312      1.1  jdolecek 	/* enable full error dumps again */
    313      1.1  jdolecek 	sc->sc_flags &= ~DASD_QUIET;
    314      1.1  jdolecek 
    315      1.9  jdolecek 	/*
    316      1.9  jdolecek 	 * Check if there are any disks attached. If not, disestablish
    317      1.9  jdolecek 	 * the interrupt.
    318      1.9  jdolecek 	 */
    319  1.9.4.1   thorpej 	for(devno=0; devno < sc->sc_maxdevs; devno++) {
    320  1.9.4.1   thorpej 		if (sc->sc_ed[devno])
    321      1.9  jdolecek 			break;
    322      1.9  jdolecek 	}
    323  1.9.4.1   thorpej 
    324  1.9.4.1   thorpej 	if (devno == sc->sc_maxdevs) {
    325      1.9  jdolecek 		printf("%s: disabling controller (no drives attached)\n",
    326      1.9  jdolecek 			sc->sc_dev.dv_xname);
    327      1.9  jdolecek 		mca_intr_disestablish(ma->ma_mc, sc->sc_ih);
    328  1.9.4.1   thorpej 		return;
    329      1.9  jdolecek 	}
    330  1.9.4.1   thorpej 
    331  1.9.4.1   thorpej 	/*
    332  1.9.4.1   thorpej 	 * Run the worker thread.
    333  1.9.4.1   thorpej 	 */
    334  1.9.4.1   thorpej 	config_pending_incr();
    335  1.9.4.1   thorpej 	kthread_create(edc_spawn_worker, (void *) sc);
    336      1.1  jdolecek }
    337      1.1  jdolecek 
    338      1.1  jdolecek void
    339  1.9.4.1   thorpej edc_add_disk(sc, ed)
    340      1.1  jdolecek 	struct edc_mca_softc *sc;
    341      1.1  jdolecek 	struct ed_softc *ed;
    342      1.1  jdolecek {
    343  1.9.4.1   thorpej 	sc->sc_ed[ed->sc_devno] = ed;
    344      1.1  jdolecek }
    345      1.1  jdolecek 
    346      1.1  jdolecek static int
    347      1.1  jdolecek edc_intr(arg)
    348      1.1  jdolecek 	void *arg;
    349      1.1  jdolecek {
    350      1.1  jdolecek 	struct edc_mca_softc *sc = arg;
    351      1.1  jdolecek 	u_int8_t isr, intr_id;
    352      1.1  jdolecek 	u_int16_t sifr;
    353  1.9.4.1   thorpej 	int cmd=-1, devno, error=0;
    354  1.9.4.1   thorpej 	u_int16_t status_block[EDC_MAX_CMD_RES_LEN]; /* CMD status block */
    355      1.1  jdolecek 
    356      1.1  jdolecek 	/*
    357      1.1  jdolecek 	 * Check if the interrupt was for us.
    358      1.1  jdolecek 	 */
    359      1.1  jdolecek 	if ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_INTR) == 0)
    360      1.1  jdolecek 		return (0);
    361      1.1  jdolecek 
    362      1.1  jdolecek 	/*
    363      1.1  jdolecek 	 * Read ISR to find out interrupt type. This also clears the interrupt
    364      1.1  jdolecek 	 * condition and BSR_INTR flag. Accordings to docs interrupt ID of 0, 2
    365      1.1  jdolecek 	 * and 4 are reserved and not used.
    366      1.1  jdolecek 	 */
    367      1.1  jdolecek 	isr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ISR);
    368      1.1  jdolecek 	intr_id = isr & ISR_INTR_ID_MASK;
    369      1.1  jdolecek 
    370      1.1  jdolecek #ifdef DEBUG
    371      1.1  jdolecek 	if (intr_id == 0 || intr_id == 2 || intr_id == 4) {
    372      1.1  jdolecek 		printf("%s: bogus interrupt id %d\n", sc->sc_dev.dv_xname,
    373      1.1  jdolecek 			(int) intr_id);
    374      1.1  jdolecek 		return (0);
    375      1.1  jdolecek 	}
    376      1.1  jdolecek #endif
    377      1.1  jdolecek 
    378      1.1  jdolecek 	/* Get number of device whose intr this was */
    379      1.1  jdolecek 	devno = (isr & 0xe0) >> 5;
    380      1.1  jdolecek 
    381      1.1  jdolecek 	/*
    382      1.1  jdolecek 	 * Get Status block. Higher byte always says how long the status
    383      1.1  jdolecek 	 * block is, rest is device number and command code.
    384      1.1  jdolecek 	 * Check the status block length against our supported maximum length
    385      1.1  jdolecek 	 * and fetch the data.
    386      1.1  jdolecek 	 */
    387      1.9  jdolecek 	if (bus_space_read_1(sc->sc_iot, sc->sc_ioh,BSR) & BSR_SIFR_FULL) {
    388      1.1  jdolecek 		size_t len;
    389      1.1  jdolecek 		int i;
    390      1.1  jdolecek 
    391      1.1  jdolecek 		sifr = le16toh(bus_space_read_2(sc->sc_iot, sc->sc_ioh, SIFR));
    392      1.1  jdolecek 		len = (sifr & 0xff00) >> 8;
    393      1.9  jdolecek #ifdef DEBUG
    394  1.9.4.1   thorpej 		if (len > EDC_MAX_CMD_RES_LEN)
    395      1.9  jdolecek 			panic("%s: maximum Status Length exceeded: %d > %d",
    396      1.1  jdolecek 				sc->sc_dev.dv_xname,
    397  1.9.4.1   thorpej 				len, EDC_MAX_CMD_RES_LEN);
    398      1.9  jdolecek #endif
    399      1.1  jdolecek 
    400      1.1  jdolecek 		/* Get command code */
    401      1.1  jdolecek 		cmd = sifr & SIFR_CMD_MASK;
    402      1.1  jdolecek 
    403      1.1  jdolecek 		/* Read whole status block */
    404  1.9.4.1   thorpej 		memset(status_block, 0, sizeof(status_block)); /* zero first */
    405  1.9.4.1   thorpej 		status_block[0] = sifr;
    406      1.1  jdolecek 		for(i=1; i < len; i++) {
    407      1.1  jdolecek 			while((bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR)
    408      1.1  jdolecek 				& BSR_SIFR_FULL) == 0)
    409      1.1  jdolecek 				delay(1);
    410      1.1  jdolecek 
    411  1.9.4.1   thorpej 			status_block[i] = le16toh(
    412      1.1  jdolecek 				bus_space_read_2(sc->sc_iot, sc->sc_ioh, SIFR));
    413      1.1  jdolecek 		}
    414      1.1  jdolecek 	}
    415      1.1  jdolecek 
    416      1.1  jdolecek 	switch (intr_id) {
    417      1.1  jdolecek 	case ISR_DATA_TRANSFER_RDY:
    418      1.1  jdolecek 		/*
    419  1.9.4.1   thorpej 		 * Ready to do DMA. The DMA controller has already been
    420  1.9.4.1   thorpej 		 * setup, now just kick disk controller to do the transfer.
    421      1.1  jdolecek 		 */
    422  1.9.4.1   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR,
    423  1.9.4.1   thorpej 			BCR_INT_ENABLE|BCR_DMA_ENABLE);
    424      1.1  jdolecek 		break;
    425      1.1  jdolecek 	case ISR_COMPLETED:
    426      1.1  jdolecek 	case ISR_COMPLETED_WITH_ECC:
    427      1.1  jdolecek 	case ISR_COMPLETED_RETRIES:
    428      1.1  jdolecek 	case ISR_COMPLETED_WARNING:
    429  1.9.4.1   thorpej 		error = 0;
    430  1.9.4.1   thorpej 
    431  1.9.4.1   thorpej 		/*
    432  1.9.4.1   thorpej 		 * Copy device config data if appropriate. sc->sc_ed[]
    433  1.9.4.1   thorpej 		 * entry might be NULL during probe.
    434  1.9.4.1   thorpej 		 */
    435  1.9.4.1   thorpej 		if (cmd == CMD_GET_DEV_CONF && sc->sc_ed[devno]) {
    436  1.9.4.1   thorpej 			memcpy(sc->sc_ed[devno]->sense_data, status_block,
    437  1.9.4.1   thorpej 				sizeof(sc->sc_ed[devno]->sense_data));
    438  1.9.4.1   thorpej 		}
    439  1.9.4.1   thorpej 
    440      1.1  jdolecek 		break;
    441      1.1  jdolecek 	case ISR_RESET_COMPLETED:
    442      1.1  jdolecek 	case ISR_ABORT_COMPLETED:
    443      1.1  jdolecek 		/* nothing to do */
    444      1.1  jdolecek 		break;
    445      1.1  jdolecek 	default:
    446      1.1  jdolecek 		if ((sc->sc_flags & DASD_QUIET) == 0)
    447  1.9.4.1   thorpej 			edc_dump_status_block(sc, status_block, intr_id);
    448      1.1  jdolecek 
    449  1.9.4.1   thorpej 		error = EIO;
    450      1.1  jdolecek 		break;
    451      1.1  jdolecek 	}
    452      1.1  jdolecek 
    453      1.1  jdolecek 	/*
    454      1.1  jdolecek 	 * Unless the interrupt is for Data Transfer Ready or
    455      1.1  jdolecek 	 * Attention Error, finish by assertion EOI. This makes
    456      1.1  jdolecek 	 * attachment aware the interrupt is processed and system
    457      1.1  jdolecek 	 * is ready to accept another one.
    458      1.1  jdolecek 	 */
    459      1.1  jdolecek 	if (intr_id != ISR_DATA_TRANSFER_RDY && intr_id != ISR_ATTN_ERROR)
    460      1.1  jdolecek 		edc_do_attn(sc, ATN_END_INT, devno, intr_id);
    461      1.1  jdolecek 
    462      1.1  jdolecek 	/* If Read or Write Data, wakeup worker thread to finish it */
    463      1.1  jdolecek 	if (intr_id != ISR_DATA_TRANSFER_RDY
    464      1.1  jdolecek 	    && (cmd == CMD_READ_DATA || cmd == CMD_WRITE_DATA)) {
    465  1.9.4.1   thorpej 		if ((sc->sc_error = error) == 0)
    466  1.9.4.1   thorpej 			sc->sc_resblk = status_block[SB_RESBLKCNT_IDX];
    467  1.9.4.1   thorpej 		wakeup_one(sc);
    468      1.1  jdolecek 	}
    469      1.1  jdolecek 
    470      1.1  jdolecek 	return (1);
    471      1.1  jdolecek }
    472      1.1  jdolecek 
    473      1.1  jdolecek /*
    474      1.1  jdolecek  * This follows the exact order for Attention Request as
    475      1.1  jdolecek  * written in DASD Storage Interface Specification MC (Rev 2.2).
    476      1.1  jdolecek  */
    477      1.1  jdolecek static int
    478      1.1  jdolecek edc_do_attn(sc, attn_type, devno, intr_id)
    479      1.1  jdolecek 	struct edc_mca_softc *sc;
    480      1.1  jdolecek 	int attn_type, devno, intr_id;
    481      1.1  jdolecek {
    482      1.1  jdolecek 	int tries;
    483      1.1  jdolecek 
    484      1.1  jdolecek 	/* 1. Disable interrupts in BCR. */
    485      1.1  jdolecek 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR, 0);
    486      1.1  jdolecek 
    487      1.9  jdolecek 	/*
    488      1.9  jdolecek 	 * 2. Assure NOT BUSY and NO INTERRUPT PENDING, unless acknowledging
    489      1.9  jdolecek 	 *    a RESET COMPLETED interrupt.
    490      1.9  jdolecek 	 */
    491      1.1  jdolecek 	if (intr_id != ISR_RESET_COMPLETED) {
    492      1.9  jdolecek 		for(tries=1; tries < EDC_ATTN_MAXTRIES; tries++) {
    493      1.1  jdolecek 			if ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR)
    494      1.9  jdolecek 			     & BSR_BUSY) == 0) {
    495      1.9  jdolecek #ifdef DEBUG
    496      1.9  jdolecek 				if ((bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    497      1.9  jdolecek 					BSR) & BSR_INT_PENDING) && intr_id)
    498      1.9  jdolecek 					panic("foobar");
    499      1.9  jdolecek #endif
    500      1.1  jdolecek 				break;
    501      1.9  jdolecek 			}
    502      1.1  jdolecek 		}
    503      1.1  jdolecek 
    504      1.9  jdolecek 		if (tries == EDC_ATTN_MAXTRIES) {
    505      1.1  jdolecek 			printf("%s: edc_do_attn: timeout waiting for attachment to become available\n",
    506      1.9  jdolecek 					sc->sc_ed[devno]->sc_dev.dv_xname);
    507      1.1  jdolecek 			return (EAGAIN);
    508      1.1  jdolecek 		}
    509      1.1  jdolecek 	}
    510      1.1  jdolecek 
    511      1.1  jdolecek 	/*
    512      1.1  jdolecek 	 * 3. Write proper DEVICE NUMBER and Attention number to ATN.
    513      1.1  jdolecek 	 */
    514      1.1  jdolecek 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ATN,
    515      1.1  jdolecek 		attn_type | (devno << 5));
    516      1.1  jdolecek 
    517      1.1  jdolecek 	/*
    518      1.1  jdolecek 	 * 4. Enable interrupts via BCR.
    519      1.1  jdolecek 	 */
    520      1.1  jdolecek 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR, BCR_INT_ENABLE);
    521      1.1  jdolecek 
    522      1.1  jdolecek 	return (0);
    523      1.1  jdolecek }
    524      1.1  jdolecek 
    525      1.1  jdolecek /*
    526      1.1  jdolecek  * Wait until command is processed, timeout after 'secs' seconds.
    527      1.1  jdolecek  * We use mono_time, since we don't need actual RTC, just time
    528      1.1  jdolecek  * interval.
    529      1.1  jdolecek  */
    530      1.9  jdolecek static int
    531  1.9.4.1   thorpej edc_cmd_wait(sc, secs, poll)
    532      1.1  jdolecek 	struct edc_mca_softc *sc;
    533  1.9.4.1   thorpej 	int secs, poll;
    534      1.1  jdolecek {
    535      1.9  jdolecek 	int val, delayed;
    536      1.1  jdolecek 
    537  1.9.4.1   thorpej 	if (!poll) {
    538  1.9.4.1   thorpej 		int error;
    539  1.9.4.1   thorpej 
    540  1.9.4.1   thorpej 		/* Not polling, can sleep. Sleep until we are awakened,
    541  1.9.4.1   thorpej 		 * but maximum secs seconds.
    542  1.9.4.1   thorpej 		 */
    543  1.9.4.1   thorpej 		error = tsleep(sc, PRIBIO, "edcwcmd", secs * hz);
    544  1.9.4.1   thorpej 		if (error)
    545  1.9.4.1   thorpej 			goto err;
    546  1.9.4.1   thorpej 		return (0);
    547  1.9.4.1   thorpej 	}
    548  1.9.4.1   thorpej 
    549  1.9.4.1   thorpej 	/* Poll the controller until command finishes */
    550      1.9  jdolecek 	delayed = 0;
    551      1.9  jdolecek 	do {
    552      1.9  jdolecek 		val = bus_space_read_1(sc->sc_iot,sc->sc_ioh, BSR);
    553      1.9  jdolecek 		if ((val & BSR_CMD_INPROGRESS) == 0)
    554      1.1  jdolecek 			break;
    555      1.1  jdolecek 
    556  1.9.4.1   thorpej 		if (val & BSR_INTR)
    557      1.9  jdolecek 			break;
    558      1.9  jdolecek 
    559      1.9  jdolecek 		delay(1);
    560      1.9  jdolecek 
    561      1.9  jdolecek 		/*
    562      1.9  jdolecek 		 * This is not as accurate as checking mono_time, but
    563      1.9  jdolecek 		 * it works with hardclock interrupts disabled too.
    564      1.9  jdolecek 		 */
    565      1.9  jdolecek 		delayed++;
    566      1.9  jdolecek 		if (delayed == 1000000) {
    567      1.9  jdolecek 			delayed = 0;
    568      1.9  jdolecek 			secs--;
    569      1.9  jdolecek 		}
    570  1.9.4.1   thorpej 	} while(secs > 0);
    571      1.1  jdolecek 
    572      1.9  jdolecek 	if (secs == 0 &&
    573      1.1  jdolecek 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_CMD_INPROGRESS){
    574  1.9.4.1   thorpej     err:
    575  1.9.4.1   thorpej 		printf("%s: timed out waiting for cmd to finish\n",
    576  1.9.4.1   thorpej 			sc->sc_dev.dv_xname);
    577      1.1  jdolecek 		return (EAGAIN);
    578      1.1  jdolecek 	}
    579      1.1  jdolecek 
    580      1.1  jdolecek 	return (0);
    581      1.1  jdolecek }
    582      1.1  jdolecek 
    583      1.1  jdolecek int
    584  1.9.4.1   thorpej edc_run_cmd(sc, cmd, devno, cmd_args, cmd_len, poll)
    585      1.1  jdolecek 	struct edc_mca_softc *sc;
    586      1.1  jdolecek 	int cmd;
    587      1.1  jdolecek 	int devno;
    588      1.1  jdolecek 	u_int16_t cmd_args[];
    589  1.9.4.1   thorpej 	int cmd_len, poll;
    590      1.1  jdolecek {
    591      1.9  jdolecek 	int i, error, tries;
    592      1.1  jdolecek 	u_int16_t cmd0;
    593      1.1  jdolecek 
    594  1.9.4.1   thorpej 	if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_BUSY) {
    595  1.9.4.1   thorpej 		printf("%s: device busy?\n", sc->sc_dev.dv_xname);
    596  1.9.4.1   thorpej 		return (EAGAIN);
    597      1.1  jdolecek 	}
    598      1.1  jdolecek 
    599      1.1  jdolecek 	/* Do Attention Request for Command Request. */
    600      1.1  jdolecek 	if ((error = edc_do_attn(sc, ATN_CMD_REQ, devno, 0)))
    601      1.1  jdolecek 		return (error);
    602      1.1  jdolecek 
    603      1.1  jdolecek 	/*
    604      1.1  jdolecek 	 * Construct the command. The bits are like this:
    605      1.1  jdolecek 	 *
    606      1.1  jdolecek 	 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
    607      1.1  jdolecek 	 *  \_/   0  0       1 0 \__/   \_____/
    608      1.1  jdolecek 	 *    \    \__________/     \         \_ Command Code (see CMD_*)
    609      1.1  jdolecek 	 *     \              \      \__ Device: 0 common, 7 controller
    610      1.1  jdolecek 	 *      \              \__ Options: reserved, bit 10=cache bypass bit
    611      1.1  jdolecek 	 *       \_ Type: 00=2B, 01=4B, 10 and 11 reserved
    612      1.1  jdolecek 	 *
    613      1.1  jdolecek 	 * We always use device 0 or 1, so difference is made only by Command
    614      1.1  jdolecek 	 * Code, Command Options and command length.
    615      1.1  jdolecek 	 */
    616      1.1  jdolecek 	cmd0 = ((cmd_len == 4) ? (CIFR_LONG_CMD) : 0)
    617      1.1  jdolecek 		| (devno <<  5)
    618      1.1  jdolecek 		| (cmd_args[0] << 8) | cmd;
    619      1.1  jdolecek 	cmd_args[0] = cmd0;
    620      1.1  jdolecek 
    621      1.1  jdolecek 	/*
    622      1.1  jdolecek 	 * Write word of CMD to the CIFR. This sets "Command
    623      1.1  jdolecek 	 * Interface Register Full (CMD IN)" in BSR. Once the attachment
    624  1.9.4.1   thorpej 	 * detects it, it reads the word and clears CMD IN. This all should
    625  1.9.4.1   thorpej 	 * be quite fast, so don't bother with sleeps for !poll case.
    626      1.1  jdolecek 	 */
    627      1.1  jdolecek 	for(i=0; i < cmd_len; i++) {
    628      1.1  jdolecek 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, CIFR,
    629      1.1  jdolecek 			htole16(cmd_args[i]));
    630      1.1  jdolecek 
    631      1.9  jdolecek 		/*
    632      1.9  jdolecek 		 * Wait until CMD IN is cleared. The 1ms delay for polling
    633      1.9  jdolecek 		 * case is necessary, otherwise e.g. system dump gets stuck
    634      1.9  jdolecek 		 * soon. Quirky hw ?
    635      1.9  jdolecek 		 */
    636      1.9  jdolecek 		tries = 0;
    637  1.9.4.1   thorpej 		for(; (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR)
    638  1.9.4.1   thorpej 		    & BSR_CIFR_FULL) && tries < 1000 ; tries++)
    639      1.9  jdolecek 			delay(poll ? 1000 : 1);
    640      1.1  jdolecek 
    641  1.9.4.1   thorpej 		if (tries == 10000) {
    642  1.9.4.1   thorpej 			printf("%s: device too slow to accept command %d\n",
    643  1.9.4.1   thorpej 				sc->sc_dev.dv_xname, cmd);
    644  1.9.4.1   thorpej 			return (EAGAIN);
    645  1.9.4.1   thorpej 		}
    646      1.1  jdolecek 	}
    647      1.1  jdolecek 
    648      1.1  jdolecek 	/* Wait for command to complete, but maximum 15 seconds. */
    649  1.9.4.1   thorpej 	if ((error = edc_cmd_wait(sc, 15, poll)))
    650  1.9.4.1   thorpej 		return (error);
    651      1.1  jdolecek 
    652      1.9  jdolecek 	/* If polling, call edc_intr() explicitly */
    653      1.9  jdolecek 	if (poll) {
    654      1.9  jdolecek 		edc_intr(sc);
    655      1.9  jdolecek 
    656      1.9  jdolecek 		/*
    657      1.9  jdolecek 		 * If got attention id DATA TRANSFER READY, wait for
    658      1.9  jdolecek 		 * the transfer to finish.
    659      1.9  jdolecek 		 */
    660  1.9.4.1   thorpej 		if ((cmd == CMD_READ_DATA || cmd == CMD_WRITE_DATA)
    661  1.9.4.1   thorpej 		    && sc->sc_error == 0) {
    662  1.9.4.1   thorpej 			if ((error = edc_cmd_wait(sc, 15, poll)))
    663  1.9.4.1   thorpej 				return (error);
    664      1.9  jdolecek 			edc_intr(sc);
    665      1.9  jdolecek 		}
    666      1.9  jdolecek 
    667  1.9.4.1   thorpej 		if ((error = edc_cmd_wait(sc, 15, poll)))
    668  1.9.4.1   thorpej 			return (error);
    669      1.1  jdolecek 	}
    670      1.1  jdolecek 
    671  1.9.4.1   thorpej 	return (sc->sc_error);
    672      1.1  jdolecek }
    673      1.1  jdolecek 
    674  1.9.4.1   thorpej #ifdef EDC_DEBUG
    675      1.1  jdolecek static const char * const edc_commands[] = {
    676      1.1  jdolecek 	"Invalid Command",
    677      1.1  jdolecek 	"Read Data",
    678      1.1  jdolecek 	"Write Data",
    679      1.1  jdolecek 	"Read Verify",
    680      1.1  jdolecek 	"Write with Verify",
    681      1.1  jdolecek 	"Seek",
    682      1.1  jdolecek 	"Park Head",
    683      1.1  jdolecek 	"Get Command Complete Status",
    684      1.1  jdolecek 	"Get Device Status",
    685      1.1  jdolecek 	"Get Device Configuration",
    686      1.1  jdolecek 	"Get POS Information",
    687      1.1  jdolecek 	"Translate RBA",
    688      1.1  jdolecek 	"Write Attachment Buffer",
    689      1.1  jdolecek 	"Read Attachment Buffer",
    690      1.1  jdolecek 	"Run Diagnostic Test",
    691      1.1  jdolecek 	"Get Diagnostic Status Block",
    692      1.1  jdolecek 	"Get MFG Header",
    693      1.1  jdolecek 	"Format Unit",
    694      1.1  jdolecek 	"Format Prepare",
    695      1.1  jdolecek 	"Set MAX RBA",
    696      1.1  jdolecek 	"Set Power Saving Mode",
    697      1.1  jdolecek 	"Power Conservation Command",
    698      1.1  jdolecek };
    699      1.1  jdolecek 
    700      1.1  jdolecek static const char * const edc_cmd_status[256] = {
    701      1.1  jdolecek 	"Reserved",
    702      1.1  jdolecek 	"Command completed successfully",
    703      1.1  jdolecek 	"Reserved",
    704      1.1  jdolecek 	"Command completed successfully with ECC applied",
    705      1.1  jdolecek 	"Reserved",
    706      1.1  jdolecek 	"Command completed successfully with retries",
    707      1.1  jdolecek 	"Format Command partially completed",	/* Status available */
    708      1.1  jdolecek 	"Command completed successfully with ECC and retries",
    709      1.1  jdolecek 	"Command completed with Warning", 	/* Command Error is available */
    710      1.1  jdolecek 	"Aborted",
    711      1.1  jdolecek 	"Reset completed",
    712      1.1  jdolecek 	"Data Transfer Ready",		/* No Status Block available */
    713      1.1  jdolecek 	"Command terminated with failure",	/* Device Error is available */
    714      1.1  jdolecek 	"DMA Error",			/* Retry entire command as recovery */
    715      1.1  jdolecek 	"Command Block Error",
    716      1.1  jdolecek 	"Attention Error (Illegal Attention Code)",
    717      1.1  jdolecek 	/* 0x14 - 0xff reserved */
    718      1.1  jdolecek };
    719      1.1  jdolecek 
    720      1.1  jdolecek static const char * const edc_cmd_error[256] = {
    721      1.1  jdolecek 	"No Error",
    722      1.1  jdolecek 	"Invalid parameter in the command block",
    723      1.1  jdolecek 	"Reserved",
    724      1.1  jdolecek 	"Command not supported",
    725      1.1  jdolecek 	"Command Aborted per request",
    726      1.1  jdolecek 	"Reserved",
    727      1.1  jdolecek 	"Command rejected",	/* Attachment diagnostic failure */
    728      1.1  jdolecek 	"Format Rejected",	/* Prepare Format command is required */
    729      1.1  jdolecek 	"Format Error (Primary Map is not readable)",
    730      1.1  jdolecek 	"Format Error (Secondary map is not readable)",
    731      1.1  jdolecek 	"Format Error (Diagnostic Failure)",
    732      1.1  jdolecek 	"Format Warning (Secondary Map Overflow)",
    733      1.1  jdolecek 	"Reserved"
    734      1.1  jdolecek 	"Format Error (Host Checksum Error)",
    735      1.1  jdolecek 	"Reserved",
    736      1.1  jdolecek 	"Format Warning (Push table overflow)",
    737      1.1  jdolecek 	"Format Warning (More pushes than allowed)",
    738      1.1  jdolecek 	"Reserved",
    739      1.1  jdolecek 	"Format Warning (Error during verifying)",
    740      1.1  jdolecek 	"Invalid device number for the command",
    741      1.1  jdolecek 	/* 0x14-0xff reserved */
    742      1.1  jdolecek };
    743      1.1  jdolecek 
    744      1.1  jdolecek static const char * const edc_dev_errors[] = {
    745      1.1  jdolecek 	"No Error",
    746      1.1  jdolecek 	"Seek Fault",	/* Device report */
    747      1.1  jdolecek 	"Interface Fault (Parity, Attn, or Cmd Complete Error)",
    748      1.1  jdolecek 	"Block not found (ID not found)",
    749      1.1  jdolecek 	"Block not found (AM not found)",
    750      1.1  jdolecek 	"Data ECC Error (hard error)",
    751      1.1  jdolecek 	"ID CRC Error",
    752      1.1  jdolecek 	"RBA Out of Range",
    753      1.1  jdolecek 	"Reserved",
    754      1.1  jdolecek 	"Defective Block",
    755      1.1  jdolecek 	"Reserved",
    756      1.1  jdolecek 	"Selection Error",
    757      1.1  jdolecek 	"Reserved",
    758      1.1  jdolecek 	"Write Fault",
    759      1.1  jdolecek 	"No index or sector pulse",
    760      1.1  jdolecek 	"Device Not Ready",
    761      1.1  jdolecek 	"Seek Error",	/* Attachment report */
    762      1.1  jdolecek 	"Bad Format",
    763      1.1  jdolecek 	"Volume Overflow",
    764      1.1  jdolecek 	"No Data AM Found",
    765      1.8    simonb 	"Block not found (No ID AM or ID CRC error occurred)",
    766      1.1  jdolecek 	"Reserved",
    767      1.1  jdolecek 	"Reserved",
    768      1.1  jdolecek 	"No ID found on track (ID search)",
    769      1.1  jdolecek 	/* 0x19 - 0xff reserved */
    770      1.1  jdolecek };
    771  1.9.4.1   thorpej #endif /* EDC_DEBUG */
    772      1.1  jdolecek 
    773      1.1  jdolecek static void
    774  1.9.4.1   thorpej edc_dump_status_block(sc, status_block, intr_id)
    775      1.1  jdolecek 	struct edc_mca_softc *sc;
    776  1.9.4.1   thorpej 	u_int16_t *status_block;
    777  1.9.4.1   thorpej 	int intr_id;
    778      1.1  jdolecek {
    779  1.9.4.1   thorpej #ifdef EDC_DEBUG
    780      1.1  jdolecek 	printf("%s: Command: %s, Status: %s\n",
    781  1.9.4.1   thorpej 		sc->sc_dev.dv_xname,
    782  1.9.4.1   thorpej 		edc_commands[status_block[0] & 0x1f],
    783  1.9.4.1   thorpej 		edc_cmd_status[SB_GET_CMD_STATUS(status_block)]
    784      1.1  jdolecek 		);
    785  1.9.4.1   thorpej #else
    786  1.9.4.1   thorpej 	printf("%s: Command: %d, Status: %d\n",
    787  1.9.4.1   thorpej 		sc->sc_dev.dv_xname,
    788  1.9.4.1   thorpej 		status_block[0] & 0x1f,
    789  1.9.4.1   thorpej 		SB_GET_CMD_STATUS(status_block));
    790  1.9.4.1   thorpej #endif
    791      1.3  jdolecek 	printf("%s: # left blocks: %u, last processed RBA: %u\n",
    792  1.9.4.1   thorpej 		sc->sc_dev.dv_xname,
    793  1.9.4.1   thorpej 		status_block[SB_RESBLKCNT_IDX],
    794  1.9.4.1   thorpej 		(status_block[5] << 16) | status_block[4]);
    795      1.1  jdolecek 
    796      1.4  jdolecek 	if (intr_id == ISR_COMPLETED_WARNING) {
    797  1.9.4.1   thorpej #ifdef EDC_DEBUG
    798      1.1  jdolecek 		printf("%s: Command Error Code: %s\n",
    799  1.9.4.1   thorpej 			sc->sc_dev.dv_xname,
    800  1.9.4.1   thorpej 			edc_cmd_error[status_block[1] & 0xff]);
    801  1.9.4.1   thorpej #else
    802  1.9.4.1   thorpej 		printf("%s: Command Error Code: %d\n",
    803  1.9.4.1   thorpej 			sc->sc_dev.dv_xname,
    804  1.9.4.1   thorpej 			status_block[1] & 0xff);
    805  1.9.4.1   thorpej #endif
    806      1.1  jdolecek 	}
    807      1.1  jdolecek 
    808      1.4  jdolecek 	if (intr_id == ISR_CMD_FAILED) {
    809  1.9.4.1   thorpej #ifdef EDC_DEBUG
    810      1.4  jdolecek 		char buf[100];
    811      1.4  jdolecek 
    812      1.4  jdolecek 		printf("%s: Device Error Code: %s\n",
    813  1.9.4.1   thorpej 			sc->sc_dev.dv_xname,
    814  1.9.4.1   thorpej 			edc_dev_errors[status_block[2] & 0xff]);
    815  1.9.4.1   thorpej 		bitmask_snprintf((status_block[2] & 0xff00) >> 8,
    816      1.4  jdolecek 			"\20"
    817      1.4  jdolecek 			"\01SeekOrCmdComplete"
    818      1.4  jdolecek 			"\02Track0Flag"
    819      1.4  jdolecek 			"\03WriteFault"
    820      1.4  jdolecek 			"\04Selected"
    821      1.4  jdolecek 			"\05Ready"
    822      1.4  jdolecek 			"\06Reserved0"
    823      1.4  jdolecek 			"\07STANDBY"
    824      1.4  jdolecek 			"\010Reserved0",
    825      1.4  jdolecek 			buf, sizeof(buf));
    826      1.4  jdolecek 		printf("%s: Device Status: %s\n",
    827  1.9.4.1   thorpej 			sc->sc_dev.dv_xname, buf);
    828  1.9.4.1   thorpej #else
    829  1.9.4.1   thorpej 		printf("%s: Device Error Code: %d, Device Status: %d\n",
    830  1.9.4.1   thorpej 			sc->sc_dev.dv_xname,
    831  1.9.4.1   thorpej 			status_block[2] & 0xff,
    832  1.9.4.1   thorpej 			(status_block[2] & 0xff00) >> 8);
    833  1.9.4.1   thorpej #endif
    834  1.9.4.1   thorpej 	}
    835  1.9.4.1   thorpej }
    836  1.9.4.1   thorpej 
    837  1.9.4.1   thorpej static void
    838  1.9.4.1   thorpej edc_spawn_worker(arg)
    839  1.9.4.1   thorpej 	void *arg;
    840  1.9.4.1   thorpej {
    841  1.9.4.1   thorpej 	struct edc_mca_softc *sc = (struct edc_mca_softc *) arg;
    842  1.9.4.1   thorpej 	int error;
    843  1.9.4.1   thorpej 	struct proc *wrk;
    844  1.9.4.1   thorpej 
    845  1.9.4.1   thorpej 	/* Now, everything is ready, start a kthread */
    846  1.9.4.1   thorpej 	if ((error = kthread_create1(edcworker, sc, &wrk,
    847  1.9.4.1   thorpej 			"%s", sc->sc_dev.dv_xname))) {
    848  1.9.4.1   thorpej 		printf("%s: cannot spawn worker thread: errno=%d\n",
    849  1.9.4.1   thorpej 			sc->sc_dev.dv_xname, error);
    850  1.9.4.1   thorpej 		panic("edc_spawn_worker");
    851      1.1  jdolecek 	}
    852  1.9.4.1   thorpej }
    853  1.9.4.1   thorpej 
    854  1.9.4.1   thorpej /*
    855  1.9.4.1   thorpej  * Main worker thread function.
    856  1.9.4.1   thorpej  */
    857  1.9.4.1   thorpej void
    858  1.9.4.1   thorpej edcworker(arg)
    859  1.9.4.1   thorpej 	void *arg;
    860  1.9.4.1   thorpej {
    861  1.9.4.1   thorpej 	struct edc_mca_softc *sc = (struct edc_mca_softc *) arg;
    862  1.9.4.1   thorpej 	struct ed_softc *ed;
    863  1.9.4.1   thorpej 	struct buf *bp;
    864  1.9.4.1   thorpej 	int s, i, error;
    865  1.9.4.1   thorpej 
    866  1.9.4.1   thorpej 	config_pending_decr();
    867  1.9.4.1   thorpej 
    868  1.9.4.1   thorpej 	s = splbio();
    869  1.9.4.1   thorpej 
    870  1.9.4.1   thorpej 	for(;;) {
    871  1.9.4.1   thorpej 		/* Wait until awakened */
    872  1.9.4.1   thorpej 		(void) tsleep(sc, PRIBIO, "edcidle", 0);
    873  1.9.4.1   thorpej 
    874  1.9.4.1   thorpej 		for(i=0; i<sc->sc_maxdevs; ) {
    875  1.9.4.1   thorpej 			if ((ed = sc->sc_ed[i]) == NULL) {
    876  1.9.4.1   thorpej 				i++;
    877  1.9.4.1   thorpej 				continue;
    878  1.9.4.1   thorpej 			}
    879  1.9.4.1   thorpej 
    880  1.9.4.1   thorpej 			/* Is there a buf for us ? */
    881  1.9.4.1   thorpej 			simple_lock(&ed->sc_q_lock);
    882  1.9.4.1   thorpej 			if ((bp = BUFQ_FIRST(&ed->sc_q)) == NULL) {
    883  1.9.4.1   thorpej 				simple_unlock(&ed->sc_q_lock);
    884  1.9.4.1   thorpej 				i++;
    885  1.9.4.1   thorpej 				continue;
    886  1.9.4.1   thorpej 			}
    887  1.9.4.1   thorpej 			BUFQ_REMOVE(&ed->sc_q, bp);
    888  1.9.4.1   thorpej 			simple_unlock(&ed->sc_q_lock);
    889  1.9.4.1   thorpej 
    890  1.9.4.1   thorpej 			/* Instrumentation. */
    891  1.9.4.1   thorpej 			disk_busy(&ed->sc_dk);
    892  1.9.4.1   thorpej 
    893  1.9.4.1   thorpej 			error = edc_bio(sc, ed, bp->b_data, bp->b_bcount,
    894  1.9.4.1   thorpej 				bp->b_rawblkno, (bp->b_flags & B_READ), 0);
    895  1.9.4.1   thorpej 
    896  1.9.4.1   thorpej 			if (error) {
    897  1.9.4.1   thorpej 				bp->b_error = error;
    898  1.9.4.1   thorpej 				bp->b_flags |= B_ERROR;
    899  1.9.4.1   thorpej 			} else {
    900  1.9.4.1   thorpej 				/* Set resid, most commonly to zero. */
    901  1.9.4.1   thorpej 				bp->b_resid = sc->sc_resblk * DEV_BSIZE;
    902  1.9.4.1   thorpej 			}
    903  1.9.4.1   thorpej 
    904  1.9.4.1   thorpej 			disk_unbusy(&ed->sc_dk, (bp->b_bcount - bp->b_resid));
    905  1.9.4.1   thorpej #if NRND > 0
    906  1.9.4.1   thorpej 			rnd_add_uint32(&ed->rnd_source, bp->b_blkno);
    907  1.9.4.1   thorpej #endif
    908  1.9.4.1   thorpej 			biodone(bp);
    909  1.9.4.1   thorpej 		}
    910  1.9.4.1   thorpej 	}
    911  1.9.4.1   thorpej 
    912  1.9.4.1   thorpej 	splx(s);
    913  1.9.4.1   thorpej }
    914  1.9.4.1   thorpej 
    915  1.9.4.1   thorpej int
    916  1.9.4.1   thorpej edc_bio(struct edc_mca_softc *sc, struct ed_softc *ed, void *data,
    917  1.9.4.1   thorpej 	size_t bcount, daddr_t rawblkno, int isread, int poll)
    918  1.9.4.1   thorpej {
    919  1.9.4.1   thorpej 	u_int16_t cmd_args[4];
    920  1.9.4.1   thorpej 	int error=0, fl;
    921  1.9.4.1   thorpej 	u_int16_t track;
    922  1.9.4.1   thorpej 	u_int16_t cyl;
    923  1.9.4.1   thorpej 	u_int8_t head;
    924  1.9.4.1   thorpej 	u_int8_t sector;
    925  1.9.4.1   thorpej 
    926  1.9.4.1   thorpej 	mca_disk_busy();
    927  1.9.4.1   thorpej 
    928  1.9.4.1   thorpej 	/* set WAIT and R/W flag appropriately for the DMA transfer */
    929  1.9.4.1   thorpej 	fl = ((poll) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK)
    930  1.9.4.1   thorpej 		| ((isread) ? BUS_DMA_READ : BUS_DMA_WRITE);
    931  1.9.4.1   thorpej 
    932  1.9.4.1   thorpej 	/* Load the buffer for DMA transfer. */
    933  1.9.4.1   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_xfer, data,
    934  1.9.4.1   thorpej 	    bcount, NULL, BUS_DMA_STREAMING|fl))) {
    935  1.9.4.1   thorpej 		printf("%s: ed_bio: unable to load DMA buffer - error %d\n",
    936  1.9.4.1   thorpej 			ed->sc_dev.dv_xname, error);
    937  1.9.4.1   thorpej 		goto out;
    938  1.9.4.1   thorpej 	}
    939  1.9.4.1   thorpej 
    940  1.9.4.1   thorpej 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_xfer, 0,
    941  1.9.4.1   thorpej 		bcount, (isread) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    942  1.9.4.1   thorpej 
    943  1.9.4.1   thorpej 	track = rawblkno / ed->sectors;
    944  1.9.4.1   thorpej 	head = track % ed->heads;
    945  1.9.4.1   thorpej 	cyl = track / ed->heads;
    946  1.9.4.1   thorpej 	sector = rawblkno % ed->sectors;
    947  1.9.4.1   thorpej 
    948  1.9.4.1   thorpej 	/* Read or Write Data command */
    949  1.9.4.1   thorpej 	cmd_args[0] = 2;	/* Options 0000010 */
    950  1.9.4.1   thorpej 	cmd_args[1] = bcount / DEV_BSIZE;
    951  1.9.4.1   thorpej 	cmd_args[2] = ((cyl & 0x1f) << 11) | (head << 5) | sector;
    952  1.9.4.1   thorpej 	cmd_args[3] = ((cyl & 0x3E0) >> 5);
    953  1.9.4.1   thorpej 	error = edc_run_cmd(sc,
    954  1.9.4.1   thorpej 			(isread) ? CMD_READ_DATA : CMD_WRITE_DATA,
    955  1.9.4.1   thorpej 			ed->sc_devno, cmd_args, 4, poll);
    956  1.9.4.1   thorpej 
    957  1.9.4.1   thorpej 	/* Sync the DMA memory */
    958  1.9.4.1   thorpej 	if (!error)  {
    959  1.9.4.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_xfer, 0, bcount,
    960  1.9.4.1   thorpej 			(isread)? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    961  1.9.4.1   thorpej 	}
    962  1.9.4.1   thorpej 
    963  1.9.4.1   thorpej 	/* We are done, unload buffer from DMA map */
    964  1.9.4.1   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_xfer);
    965  1.9.4.1   thorpej 
    966  1.9.4.1   thorpej     out:
    967  1.9.4.1   thorpej 	mca_disk_unbusy();
    968  1.9.4.1   thorpej 
    969  1.9.4.1   thorpej 	return (error);
    970      1.1  jdolecek }
    971