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