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