Home | History | Annotate | Line # | Download | only in ic
wdc.c revision 1.10
      1  1.10   mikel /*	$NetBSD: wdc.c,v 1.10 1998/01/07 08:47:54 mikel Exp $ */
      2   1.2  bouyer 
      3   1.2  bouyer /*
      4   1.2  bouyer  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      5   1.2  bouyer  *
      6   1.2  bouyer  * DMA and multi-sector PIO handling are derived from code contributed by
      7   1.2  bouyer  * Onno van der Linden.
      8   1.2  bouyer  *
      9   1.2  bouyer  * Atapi support added by Manuel Bouyer.
     10   1.2  bouyer  *
     11   1.2  bouyer  * Redistribution and use in source and binary forms, with or without
     12   1.2  bouyer  * modification, are permitted provided that the following conditions
     13   1.2  bouyer  * are met:
     14   1.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     15   1.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     16   1.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     18   1.2  bouyer  *    documentation and/or other materials provided with the distribution.
     19   1.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     20   1.2  bouyer  *    must display the following acknowledgement:
     21   1.2  bouyer  *	This product includes software developed by Charles M. Hannum.
     22   1.2  bouyer  * 4. The name of the author may not be used to endorse or promote products
     23   1.2  bouyer  *    derived from this software without specific prior written permission.
     24   1.2  bouyer  *
     25   1.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26   1.2  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27   1.2  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28   1.2  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29   1.2  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30   1.2  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31   1.2  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32   1.2  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33   1.2  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     34   1.2  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.2  bouyer  */
     36   1.2  bouyer 
     37   1.2  bouyer #include <sys/param.h>
     38   1.2  bouyer #include <sys/systm.h>
     39   1.2  bouyer #include <sys/kernel.h>
     40   1.2  bouyer #include <sys/conf.h>
     41   1.2  bouyer #include <sys/file.h>
     42   1.2  bouyer #include <sys/stat.h>
     43   1.2  bouyer #include <sys/ioctl.h>
     44   1.2  bouyer #include <sys/buf.h>
     45   1.2  bouyer #include <sys/uio.h>
     46   1.2  bouyer #include <sys/malloc.h>
     47   1.2  bouyer #include <sys/device.h>
     48   1.2  bouyer #include <sys/disklabel.h>
     49   1.2  bouyer #include <sys/disk.h>
     50   1.2  bouyer #include <sys/syslog.h>
     51   1.2  bouyer #include <sys/proc.h>
     52   1.2  bouyer 
     53   1.2  bouyer #include <vm/vm.h>
     54   1.2  bouyer 
     55   1.2  bouyer #include <machine/cpu.h>
     56   1.2  bouyer #include <machine/intr.h>
     57   1.2  bouyer #include <machine/bus.h>
     58   1.2  bouyer #include <machine/pio.h>
     59   1.2  bouyer 
     60   1.2  bouyer #include <dev/isa/isavar.h>
     61   1.2  bouyer #include <dev/isa/isadmavar.h>
     62   1.2  bouyer #include <dev/isa/wdreg.h>
     63   1.2  bouyer #include <dev/isa/wdlink.h>
     64   1.2  bouyer #include "atapibus.h"
     65   1.2  bouyer #include "wd.h"
     66   1.2  bouyer 
     67   1.2  bouyer #if NATAPIBUS > 0
     68   1.2  bouyer #include <dev/scsipi/scsipi_all.h>
     69   1.2  bouyer #include <dev/scsipi/atapiconf.h>
     70   1.2  bouyer #endif
     71   1.2  bouyer 
     72   1.2  bouyer #define	WAITTIME	(10 * hz)	/* time to wait for a completion */
     73   1.2  bouyer 	/* this is a lot for hard drives, but not for cdroms */
     74   1.2  bouyer #define RECOVERYTIME hz/2
     75   1.2  bouyer #define WDCDELAY	100
     76   1.2  bouyer #define WDCNDELAY	100000	/* delay = 100us; so 10s for a controller state change */
     77   1.2  bouyer #if 0
     78   1.2  bouyer /* If you enable this, it will report any delays more than 100us * N long. */
     79   1.2  bouyer #define WDCNDELAY_DEBUG	50
     80   1.2  bouyer #endif
     81   1.2  bouyer 
     82   1.2  bouyer #define	WDIORETRIES	5		/* number of retries before giving up */
     83   1.2  bouyer 
     84   1.2  bouyer #define	WDPART(dev)			DISKPART(dev)
     85   1.2  bouyer 
     86   1.2  bouyer LIST_HEAD(xfer_free_list, wdc_xfer) xfer_free_list;
     87   1.2  bouyer 
     88   1.2  bouyer int	wdcprobe	__P((struct device *, void *, void *));
     89   1.2  bouyer void	wdcattach	__P((struct device *, struct device *, void *));
     90   1.2  bouyer int	wdcintr		__P((void *));
     91   1.2  bouyer 
     92   1.2  bouyer struct cfattach wdc_ca = {
     93   1.2  bouyer 	sizeof(struct wdc_softc), wdcprobe, wdcattach
     94   1.2  bouyer };
     95   1.2  bouyer 
     96   1.2  bouyer struct cfdriver wdc_cd = {
     97   1.2  bouyer 	NULL, "wdc", DV_DULL
     98   1.2  bouyer };
     99   1.2  bouyer 
    100   1.2  bouyer void	wdcstart	__P((struct wdc_softc *));
    101   1.2  bouyer int		wdcreset	__P((struct wdc_softc *, int));
    102   1.2  bouyer #define VERBOSE 1
    103   1.2  bouyer #define SILENT 0
    104   1.2  bouyer void	wdcrestart	__P((void *arg));
    105   1.2  bouyer void	wdcunwedge	__P((struct wdc_softc *));
    106   1.2  bouyer void	wdctimeout	__P((void *arg));
    107   1.2  bouyer int		wdccontrol	__P((struct wdc_softc*, struct wd_link *));
    108   1.2  bouyer void	wdc_free_xfer	__P((struct wdc_xfer *));
    109   1.2  bouyer void	wdcerror	__P((struct wdc_softc*, char *));
    110   1.2  bouyer void	wdcbit_bucket	__P(( struct wdc_softc *, int));
    111   1.2  bouyer #if NWD > 0
    112   1.2  bouyer int		wdprint			__P((void *, const char *));
    113   1.2  bouyer int		wdsetctlr		__P((struct wd_link *));
    114   1.2  bouyer int		wdc_ata_intr	__P((struct wdc_softc *,struct wdc_xfer *));
    115   1.2  bouyer void	wdc_ata_start	__P((struct wdc_softc *,struct wdc_xfer *));
    116   1.2  bouyer void	wdc_ata_done	__P((struct wdc_softc *, struct wdc_xfer *));
    117   1.2  bouyer #endif /* NWD > 0 */
    118   1.2  bouyer #if NATAPIBUS > 0
    119   1.2  bouyer void	wdc_atapi_minphys __P((struct buf *bp));
    120   1.2  bouyer void	wdc_atapi_start	__P((struct wdc_softc *,struct wdc_xfer *));
    121   1.2  bouyer int		wdc_atapi_intr	__P((struct wdc_softc *, struct wdc_xfer *));
    122   1.2  bouyer void	wdc_atapi_done	__P((struct wdc_softc *, struct wdc_xfer *));
    123   1.2  bouyer int		wdc_atapi_send_command_packet __P((struct scsipi_xfer *sc_xfer));
    124   1.2  bouyer #define MAX_SIZE MAXPHYS /* XXX */
    125   1.2  bouyer #endif
    126   1.2  bouyer 
    127   1.8  bouyer #ifdef ATAPI_DEBUG2
    128   1.2  bouyer static int wdc_nxfer;
    129   1.2  bouyer #endif
    130   1.2  bouyer 
    131   1.2  bouyer #ifdef WDDEBUG
    132   1.2  bouyer #define WDDEBUG_PRINT(args)	printf args
    133   1.2  bouyer #else
    134   1.2  bouyer #define WDDEBUG_PRINT(args)
    135   1.2  bouyer #endif
    136   1.2  bouyer 
    137   1.2  bouyer #if NATAPIBUS > 0
    138   1.2  bouyer static struct scsipi_adapter wdc_switch  = {
    139   1.2  bouyer 	wdc_atapi_send_command_packet,
    140   1.2  bouyer 	wdc_atapi_minphys,
    141   1.2  bouyer 	0,
    142   1.2  bouyer 	0
    143   1.2  bouyer };
    144   1.2  bouyer #endif
    145   1.2  bouyer 
    146   1.2  bouyer int
    147   1.2  bouyer wdcprobe(parent, match, aux)
    148   1.2  bouyer 	struct device *parent;
    149   1.2  bouyer 	void *match, *aux;
    150   1.2  bouyer {
    151   1.2  bouyer 	struct wdc_softc *wdc = match;
    152   1.2  bouyer 	struct isa_attach_args *ia = aux;
    153   1.2  bouyer 	int iobase;
    154   1.2  bouyer 
    155   1.2  bouyer 	wdc->sc_iobase = iobase = ia->ia_iobase;
    156   1.2  bouyer 
    157   1.7  bouyer 	if (wdcreset(wdc, SILENT) != 0) {
    158   1.2  bouyer 		/*
    159   1.7  bouyer 		 * if the reset failed, there is no master. test for ATAPI signature
    160   1.7  bouyer 		 * on the slave device. If no ATAPI slave, wait 5s and retry a reset.
    161   1.2  bouyer 		 */
    162   1.2  bouyer 		outb(iobase+wd_sdh, WDSD_IBM | 0x10); /* slave */
    163   1.7  bouyer 		if (inb(iobase + wd_cyl_lo) == 0x14 &&
    164   1.7  bouyer 			inb(iobase + wd_cyl_hi) == 0xeb) {
    165   1.7  bouyer 			wdc->sc_flags |= WDCF_ONESLAVE;
    166   1.7  bouyer 			goto drivefound;
    167   1.7  bouyer 		} else {
    168   1.2  bouyer 			delay(500000);
    169   1.2  bouyer 			if (wdcreset(wdc, SILENT) != 0)
    170   1.2  bouyer 				return 0;
    171   1.2  bouyer 		}
    172   1.7  bouyer 	}
    173   1.9    fvdl 	delay(1000);
    174   1.7  bouyer 	/* reset succeeded. Test registers */
    175   1.7  bouyer 	if (inb(iobase + wd_cyl_lo) == 0x14 &&
    176   1.7  bouyer 		inb(iobase + wd_cyl_hi) == 0xeb)
    177   1.7  bouyer 		goto drivefound;
    178   1.7  bouyer 	/* not ATAPI. Test registers */
    179   1.7  bouyer 	outb(iobase+wd_error, 0x58);    /* Error register not writable, */
    180   1.7  bouyer 	outb(iobase+wd_cyl_lo, 0xa5);   /* but all of cyllo are. */
    181   1.7  bouyer 	if (inb(iobase+wd_error) != 0x58 && inb(iobase+wd_cyl_lo) == 0xa5)
    182   1.7  bouyer 		goto drivefound;
    183   1.7  bouyer 	/* No master. Test atapi signature on slave */
    184   1.7  bouyer 	outb(iobase+wd_sdh, WDSD_IBM | 0x10);
    185   1.7  bouyer 	if (inb(iobase + wd_cyl_lo) == 0x14 &&
    186   1.7  bouyer 		inb(iobase + wd_cyl_hi) == 0xeb) {
    187   1.2  bouyer 		wdc->sc_flags |= WDCF_ONESLAVE;
    188   1.7  bouyer 		goto drivefound;
    189   1.2  bouyer 	}
    190   1.7  bouyer 	return 0;
    191   1.7  bouyer drivefound:
    192   1.2  bouyer 	/* Select drive 0 or ATAPI slave device */
    193   1.2  bouyer 	if (wdc->sc_flags & WDCF_ONESLAVE)
    194   1.2  bouyer 		outb(iobase+wd_sdh, WDSD_IBM | 0x10);
    195   1.2  bouyer 	else
    196   1.2  bouyer 		outb(iobase+wd_sdh, WDSD_IBM);
    197   1.2  bouyer 
    198   1.2  bouyer 	/* Wait for controller to become ready. */
    199   1.2  bouyer 	if (wait_for_unbusy(wdc) < 0)
    200   1.2  bouyer 		return 0;
    201   1.2  bouyer 
    202   1.2  bouyer 	/* Start drive diagnostics. */
    203   1.2  bouyer 	outb(iobase+wd_command, WDCC_DIAGNOSE);
    204   1.2  bouyer 
    205   1.2  bouyer 	/* Wait for command to complete. */
    206   1.2  bouyer 	if (wait_for_unbusy(wdc) < 0)
    207   1.2  bouyer 		return 0;
    208   1.2  bouyer 
    209   1.2  bouyer 	ia->ia_iosize = 8;
    210   1.2  bouyer 	ia->ia_msize = 0;
    211   1.2  bouyer 	return 1;
    212   1.2  bouyer }
    213   1.2  bouyer 
    214   1.2  bouyer void
    215   1.2  bouyer wdcattach(parent, self, aux)
    216   1.2  bouyer 	struct device *parent, *self;
    217   1.2  bouyer 	void *aux;
    218   1.2  bouyer {
    219   1.2  bouyer 	struct wdc_softc *wdc = (void *)self;
    220   1.2  bouyer 	struct isa_attach_args *ia = aux;
    221   1.2  bouyer #if NWD > 0
    222   1.2  bouyer 	int drive;
    223   1.2  bouyer #endif
    224   1.2  bouyer 
    225   1.2  bouyer 	TAILQ_INIT(&wdc->sc_xfer);
    226   1.2  bouyer 	wdc->sc_drq = ia->ia_drq;
    227   1.2  bouyer 
    228   1.2  bouyer 	printf("\n");
    229   1.2  bouyer 	if (wdc->sc_drq != -1) {
    230   1.2  bouyer 		if (isa_dmamap_create(parent, wdc->sc_drq, MAXPHYS,
    231   1.2  bouyer 			BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    232   1.2  bouyer 				printf("%s: can't create map for drq %d\n",
    233   1.2  bouyer 					wdc->sc_dev.dv_xname, wdc->sc_drq);
    234   1.2  bouyer 				wdc->sc_drq = -1;
    235   1.2  bouyer 		}
    236   1.2  bouyer 	}
    237   1.2  bouyer 
    238   1.2  bouyer 	wdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    239   1.2  bouyer 	    IPL_BIO, wdcintr, wdc);
    240   1.2  bouyer 
    241   1.8  bouyer #ifdef ATAPI_DEBUG2
    242   1.2  bouyer 	wdc_nxfer = 0;
    243   1.2  bouyer #endif
    244   1.2  bouyer 
    245   1.2  bouyer #if NATAPIBUS > 0
    246   1.2  bouyer 	/*
    247   1.2  bouyer 	 * Attach an ATAPI bus, if configured.
    248   1.2  bouyer 	 */
    249   1.2  bouyer 	wdc->ab_link = malloc(sizeof(struct scsipi_link), M_DEVBUF, M_NOWAIT);
    250   1.2  bouyer 	if (wdc->ab_link == NULL) {
    251   1.2  bouyer 		printf("%s: can't allocate ATAPI link\n", self->dv_xname);
    252   1.2  bouyer 		return;
    253   1.2  bouyer 	}
    254   1.2  bouyer 	bzero(wdc->ab_link,sizeof(struct scsipi_link));
    255   1.2  bouyer 	wdc->ab_link->type = BUS_ATAPI;
    256   1.2  bouyer 	wdc->ab_link->openings = 1;
    257   1.2  bouyer 	wdc->ab_link->scsipi_atapi.type = ATAPI;
    258   1.2  bouyer 	wdc->ab_link->scsipi_atapi.channel = 0;
    259   1.2  bouyer 	wdc->ab_link->adapter_softc = (caddr_t)wdc;
    260   1.2  bouyer 	wdc->ab_link->adapter = &wdc_switch;
    261   1.2  bouyer 	(void)config_found(self, (void *)wdc->ab_link, NULL);
    262   1.2  bouyer #endif /* NATAPIBUS > 0 */
    263   1.2  bouyer #if NWD > 0
    264   1.2  bouyer 	/*
    265   1.2  bouyer 	 * Attach standard IDE/ESDI/etc. disks to the controller.
    266   1.2  bouyer 	 */
    267   1.2  bouyer 	for (drive = 0; drive < 2; drive++) {
    268   1.4  bouyer 		/* if a disk is already present, skip */
    269   1.4  bouyer 		if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
    270   1.4  bouyer 			continue;
    271   1.4  bouyer 		}
    272   1.2  bouyer 		/* controller active while autoconf */
    273   1.2  bouyer 		wdc->sc_flags |= WDCF_ACTIVE;
    274   1.2  bouyer 
    275   1.2  bouyer 		if (wdccommandshort(wdc, drive, WDCC_RECAL) != 0 ||
    276   1.2  bouyer             	    wait_for_ready(wdc) != 0) {
    277   1.2  bouyer 			wdc->d_link[drive] = NULL;
    278   1.2  bouyer 			wdc->sc_flags &= ~WDCF_ACTIVE;
    279   1.2  bouyer 		} else {
    280   1.2  bouyer 			wdc->sc_flags &= ~WDCF_ACTIVE;
    281   1.2  bouyer 			wdc->d_link[drive] = malloc(sizeof(struct wd_link),
    282   1.2  bouyer 			    M_DEVBUF, M_NOWAIT);
    283   1.2  bouyer 			if (wdc->d_link[drive] == NULL) {
    284   1.2  bouyer 				printf("%s: can't allocate link for drive %d\n",
    285   1.2  bouyer 				    self->dv_xname, drive);
    286   1.2  bouyer 				continue;
    287   1.2  bouyer 			}
    288   1.2  bouyer 			bzero(wdc->d_link[drive],sizeof(struct wd_link));
    289   1.2  bouyer 			wdc->d_link[drive]->type = ATA;
    290   1.2  bouyer 			wdc->d_link[drive]->wdc_softc =(caddr_t) wdc;
    291   1.2  bouyer 			wdc->d_link[drive]->drive = drive;
    292   1.2  bouyer 			if (wdc->sc_drq != DRQUNK)
    293   1.2  bouyer 				wdc->d_link[drive]->sc_mode = WDM_DMA;
    294   1.2  bouyer 			else
    295   1.2  bouyer 				wdc->d_link[drive]->sc_mode = 0;
    296   1.2  bouyer 
    297   1.4  bouyer 			wdc->sc_drives_mask |= (1 << drive);
    298   1.2  bouyer 			(void)config_found(self, (void *)wdc->d_link[drive],
    299   1.2  bouyer 			    wdprint);
    300   1.2  bouyer 		}
    301   1.2  bouyer 	}
    302   1.2  bouyer #endif /* NWD > 0 */
    303  1.10   mikel 	/* explicitly select an existing drive, to avoid spurious interrupts */
    304   1.7  bouyer 	if (wdc->sc_flags & WDCF_ONESLAVE)
    305   1.7  bouyer 		outb(wdc->sc_iobase+wd_sdh, WDSD_IBM | 0x10); /* slave */
    306   1.7  bouyer 	else
    307   1.7  bouyer 		outb(wdc->sc_iobase+wd_sdh, WDSD_IBM); /* master */
    308   1.8  bouyer 	/*
    309   1.8  bouyer 	 * Reset controller. The probe, with some combinations of ATA/ATAPI
    310  1.10   mikel 	 * devices keep it in a mostly working, but strange state (with busy
    311   1.8  bouyer 	 * led on)
    312   1.8  bouyer 	 */
    313   1.8  bouyer 	wdcreset(wdc, VERBOSE);
    314   1.2  bouyer }
    315   1.2  bouyer 
    316   1.2  bouyer /*
    317   1.2  bouyer  * Start I/O on a controller.  This does the calculation, and starts a read or
    318   1.2  bouyer  * write operation.  Called to from wdstart() to start a transfer, from
    319   1.2  bouyer  * wdcintr() to continue a multi-sector transfer or start the next transfer, or
    320   1.2  bouyer  * wdcrestart() after recovering from an error.
    321   1.2  bouyer  */
    322   1.2  bouyer void
    323   1.2  bouyer wdcstart(wdc)
    324   1.2  bouyer 	struct wdc_softc *wdc;
    325   1.2  bouyer {
    326   1.2  bouyer 	struct wdc_xfer *xfer;
    327   1.2  bouyer 
    328   1.2  bouyer 	if ((wdc->sc_flags & WDCF_ACTIVE) != 0 ) {
    329   1.2  bouyer 		WDDEBUG_PRINT(("wdcstart: already active\n"));
    330   1.2  bouyer 		return; /* controller aleady active */
    331   1.2  bouyer 	}
    332   1.2  bouyer #ifdef DIAGNOSTIC
    333   1.2  bouyer 	if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0)
    334   1.2  bouyer 		panic("wdcstart: controller waiting for irq\n");
    335   1.2  bouyer #endif
    336   1.2  bouyer 	/* is there a xfer ? */
    337   1.2  bouyer 	xfer = wdc->sc_xfer.tqh_first;
    338   1.2  bouyer 	if (xfer == NULL) {
    339   1.2  bouyer #ifdef ATAPI_DEBUG2
    340   1.2  bouyer 		printf("wdcstart: null xfer\n");
    341   1.2  bouyer #endif
    342   1.2  bouyer 		/*
    343   1.2  bouyer 		 * XXX
    344   1.2  bouyer 		 * This is a kluge.  See comments in wd_get_parms().
    345   1.2  bouyer 		 */
    346   1.2  bouyer 		if ((wdc->sc_flags & WDCF_WANTED) != 0) {
    347   1.8  bouyer #ifdef ATAPI_DEBUG2
    348   1.2  bouyer 			printf("WDCF_WANTED\n");
    349   1.2  bouyer #endif
    350   1.2  bouyer 			wdc->sc_flags &= ~WDCF_WANTED;
    351   1.2  bouyer 			wakeup(wdc);
    352   1.2  bouyer 		}
    353   1.2  bouyer 		return;
    354   1.2  bouyer 	}
    355   1.2  bouyer 	wdc->sc_flags |= WDCF_ACTIVE;
    356   1.2  bouyer #ifdef ATAPI_DEBUG2
    357   1.2  bouyer 		printf("wdcstart: drive %d\n", (int)xfer->d_link->drive);
    358   1.2  bouyer #endif
    359   1.2  bouyer     outb(wdc->sc_iobase+wd_sdh, WDSD_IBM | xfer->d_link->drive << 4);
    360   1.2  bouyer #if NATAPIBUS > 0 && NWD > 0
    361   1.2  bouyer 	if (xfer->c_flags & C_ATAPI) {
    362   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
    363   1.2  bouyer 		printf("wdcstart: atapi\n");
    364   1.2  bouyer #endif
    365   1.2  bouyer 		wdc_atapi_start(wdc,xfer);
    366   1.2  bouyer 	} else
    367   1.2  bouyer 		wdc_ata_start(wdc,xfer);
    368   1.2  bouyer #else /* NATAPIBUS > 0 && NWD > 0 */
    369   1.2  bouyer #if NATAPIBUS > 0
    370   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
    371   1.2  bouyer 	printf("wdcstart: atapi\n");
    372   1.2  bouyer #endif
    373   1.2  bouyer 	wdc_atapi_start(wdc,xfer);
    374   1.2  bouyer #endif /* NATAPIBUS > */
    375   1.2  bouyer #if NWD > 0
    376   1.2  bouyer 	wdc_ata_start(wdc,xfer);
    377   1.2  bouyer #endif /* NWD > 0 */
    378   1.2  bouyer #endif /* NATAPIBUS > 0 && NWD > 0 */
    379   1.2  bouyer }
    380   1.2  bouyer 
    381   1.2  bouyer #if NWD > 0
    382   1.2  bouyer int
    383   1.2  bouyer wdprint(aux, wdc)
    384   1.2  bouyer 	void *aux;
    385   1.2  bouyer 	const char *wdc;
    386   1.2  bouyer {
    387   1.2  bouyer 	struct wd_link *d_link = aux;
    388   1.2  bouyer 
    389   1.2  bouyer 	if (!wdc)
    390   1.2  bouyer 		printf(" drive %d", d_link->drive);
    391   1.2  bouyer 	return QUIET;
    392   1.2  bouyer }
    393   1.2  bouyer 
    394   1.2  bouyer void
    395   1.2  bouyer wdc_ata_start(wdc, xfer)
    396   1.2  bouyer 	struct wdc_softc *wdc;
    397   1.2  bouyer 	struct wdc_xfer *xfer;
    398   1.2  bouyer {
    399   1.2  bouyer 	struct wd_link *d_link;
    400   1.2  bouyer 	struct buf *bp = xfer->c_bp;
    401   1.2  bouyer 	int nblks;
    402   1.2  bouyer 
    403   1.2  bouyer 	d_link=xfer->d_link;
    404   1.2  bouyer 
    405   1.2  bouyer 	if (wdc->sc_errors >= WDIORETRIES) {
    406   1.2  bouyer 		wderror(d_link, bp, "wdc_ata_start hard error");
    407   1.2  bouyer 		xfer->c_flags |= C_ERROR;
    408   1.2  bouyer 		wdc_ata_done(wdc, xfer);
    409   1.2  bouyer 		return;
    410   1.2  bouyer 	}
    411   1.2  bouyer 
    412   1.2  bouyer 	/* Do control operations specially. */
    413   1.2  bouyer 	if (d_link->sc_state < READY) {
    414   1.2  bouyer 		/*
    415   1.2  bouyer 		 * Actually, we want to be careful not to mess with the control
    416   1.2  bouyer 		 * state if the device is currently busy, but we can assume
    417   1.2  bouyer 		 * that we never get to this point if that's the case.
    418   1.2  bouyer 		 */
    419   1.2  bouyer 		if (wdccontrol(wdc, d_link) == 0) {
    420   1.2  bouyer 			/* The drive is busy.  Wait. */
    421   1.2  bouyer 			return;
    422   1.2  bouyer 		}
    423   1.2  bouyer 	}
    424   1.2  bouyer 
    425   1.2  bouyer 	/*
    426   1.2  bouyer 	 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
    427   1.2  bouyer 	 * encountered.  If we are in multi-sector mode, then we switch to
    428   1.2  bouyer 	 * single-sector mode and retry the operation from the start.
    429   1.2  bouyer 	 */
    430   1.2  bouyer 	if (wdc->sc_flags & WDCF_ERROR) {
    431   1.2  bouyer 		wdc->sc_flags &= ~WDCF_ERROR;
    432   1.2  bouyer 		if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
    433   1.2  bouyer 			wdc->sc_flags |= WDCF_SINGLE;
    434   1.2  bouyer 			xfer->c_skip = 0;
    435   1.2  bouyer 		}
    436   1.2  bouyer 	}
    437   1.2  bouyer 
    438   1.2  bouyer 
    439   1.2  bouyer 	/* When starting a transfer... */
    440   1.2  bouyer 	if (xfer->c_skip == 0) {
    441   1.2  bouyer 		daddr_t blkno;
    442   1.2  bouyer 
    443   1.2  bouyer 		WDDEBUG_PRINT(("\n%s: wdc_ata_start %s %d@%d; map ",
    444   1.2  bouyer 		    wdc->sc_dev.dv_xname,
    445   1.2  bouyer 		    (xfer->c_flags & B_READ) ? "read" : "write",
    446   1.2  bouyer 		    xfer->c_bcount, xfer->c_blkno));
    447   1.2  bouyer 
    448   1.2  bouyer 		blkno = xfer->c_blkno+xfer->c_p_offset;
    449   1.2  bouyer 		xfer->c_blkno = blkno / (d_link->sc_lp->d_secsize / DEV_BSIZE);
    450   1.2  bouyer 	} else {
    451   1.2  bouyer 		WDDEBUG_PRINT((" %d)%x", xfer->c_skip,
    452   1.2  bouyer 		    inb(wdc->sc_iobase + wd_altsts)));
    453   1.2  bouyer 	}
    454   1.2  bouyer 
    455   1.2  bouyer 	/*
    456   1.2  bouyer 	 * When starting a multi-sector transfer, or doing single-sector
    457   1.2  bouyer 	 * transfers...
    458   1.2  bouyer 	 */
    459   1.2  bouyer 	if (xfer->c_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0 ||
    460   1.2  bouyer 	    d_link->sc_mode == WDM_DMA) {
    461   1.2  bouyer 		daddr_t blkno = xfer->c_blkno;
    462   1.2  bouyer 		long cylin, head, sector;
    463   1.2  bouyer 		int command;
    464   1.2  bouyer 
    465   1.2  bouyer 		if ((wdc->sc_flags & WDCF_SINGLE) != 0)
    466   1.2  bouyer 			nblks = 1;
    467   1.2  bouyer 		else if (d_link->sc_mode != WDM_DMA)
    468   1.2  bouyer 			nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
    469   1.2  bouyer 		else
    470   1.2  bouyer 			nblks =
    471   1.2  bouyer 			    min(xfer->c_bcount / d_link->sc_lp->d_secsize, 8);
    472   1.2  bouyer 
    473   1.2  bouyer 		/* Check for bad sectors and adjust transfer, if necessary. */
    474   1.2  bouyer 		if ((d_link->sc_lp->d_flags & D_BADSECT) != 0
    475   1.2  bouyer #ifdef B_FORMAT
    476   1.2  bouyer 		    && (bp->b_flags & B_FORMAT) == 0
    477   1.2  bouyer #endif
    478   1.2  bouyer 		    ) {
    479   1.2  bouyer 			long blkdiff;
    480   1.2  bouyer 			int i;
    481   1.2  bouyer 
    482   1.2  bouyer 			for (i = 0;
    483   1.2  bouyer 			    (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
    484   1.2  bouyer 				blkdiff -= blkno;
    485   1.2  bouyer 				if (blkdiff < 0)
    486   1.2  bouyer 					continue;
    487   1.2  bouyer 				if (blkdiff == 0) {
    488   1.2  bouyer 					/* Replace current block of transfer. */
    489   1.2  bouyer 					blkno =
    490   1.2  bouyer 					    d_link->sc_lp->d_secperunit -
    491   1.2  bouyer 					    d_link->sc_lp->d_nsectors - i - 1;
    492   1.2  bouyer 				}
    493   1.2  bouyer 				if (blkdiff < nblks) {
    494   1.2  bouyer 					/* Bad block inside transfer. */
    495   1.2  bouyer 					wdc->sc_flags |= WDCF_SINGLE;
    496   1.2  bouyer 					nblks = 1;
    497   1.2  bouyer 				}
    498   1.2  bouyer 				break;
    499   1.2  bouyer 			}
    500  1.10   mikel 			/* Transfer is okay now. */
    501   1.2  bouyer 		}
    502   1.2  bouyer 
    503   1.2  bouyer 		if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
    504   1.2  bouyer 			sector = (blkno >> 0) & 0xff;
    505   1.2  bouyer 			cylin = (blkno >> 8) & 0xffff;
    506   1.2  bouyer 			head = (blkno >> 24) & 0xf;
    507   1.2  bouyer 			head |= WDSD_LBA;
    508   1.2  bouyer 		} else {
    509   1.2  bouyer 			sector = blkno % d_link->sc_lp->d_nsectors;
    510   1.2  bouyer 			sector++;	/* Sectors begin with 1, not 0. */
    511   1.2  bouyer 			blkno /= d_link->sc_lp->d_nsectors;
    512   1.2  bouyer 			head = blkno % d_link->sc_lp->d_ntracks;
    513   1.2  bouyer 			blkno /= d_link->sc_lp->d_ntracks;
    514   1.2  bouyer 			cylin = blkno;
    515   1.2  bouyer 			head |= WDSD_CHS;
    516   1.2  bouyer 		}
    517   1.2  bouyer 
    518   1.2  bouyer 		if (d_link->sc_mode == WDM_PIOSINGLE ||
    519   1.2  bouyer 		    (wdc->sc_flags & WDCF_SINGLE) != 0)
    520   1.2  bouyer 			xfer->c_nblks = 1;
    521   1.2  bouyer 		else if (d_link->sc_mode == WDM_PIOMULTI)
    522   1.2  bouyer 			xfer->c_nblks = min(nblks, d_link->sc_multiple);
    523   1.2  bouyer 		else
    524   1.2  bouyer 			xfer->c_nblks = nblks;
    525   1.2  bouyer 		xfer->c_nbytes = xfer->c_nblks * d_link->sc_lp->d_secsize;
    526   1.2  bouyer 
    527   1.2  bouyer #ifdef B_FORMAT
    528   1.2  bouyer 		if (bp->b_flags & B_FORMAT) {
    529   1.2  bouyer 			sector = d_link->sc_lp->d_gap3;
    530   1.2  bouyer 			nblks = d_link->sc_lp->d_nsectors;
    531   1.2  bouyer 			command = WDCC_FORMAT;
    532   1.2  bouyer 		} else
    533   1.2  bouyer #endif
    534   1.2  bouyer 		switch (d_link->sc_mode) {
    535   1.2  bouyer 		case WDM_DMA:
    536   1.2  bouyer 			command = (xfer->c_flags & B_READ) ?
    537   1.2  bouyer 			    WDCC_READDMA : WDCC_WRITEDMA;
    538   1.2  bouyer 			/* Start the DMA channel. */
    539   1.2  bouyer 			isa_dmastart(wdc->sc_dev.dv_parent, wdc->sc_drq,
    540   1.2  bouyer 				xfer->databuf + xfer->c_skip, xfer->c_nbytes,
    541   1.2  bouyer 				NULL,
    542   1.2  bouyer 				xfer->c_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE,
    543   1.2  bouyer 				BUS_DMA_NOWAIT);
    544   1.2  bouyer 			break;
    545   1.2  bouyer 
    546   1.2  bouyer 		case WDM_PIOMULTI:
    547   1.2  bouyer 			command = (xfer->c_flags & B_READ) ?
    548   1.2  bouyer 			    WDCC_READMULTI : WDCC_WRITEMULTI;
    549   1.2  bouyer 			break;
    550   1.2  bouyer 
    551   1.2  bouyer 		case WDM_PIOSINGLE:
    552   1.2  bouyer 			command = (xfer->c_flags & B_READ) ?
    553   1.2  bouyer 			    WDCC_READ : WDCC_WRITE;
    554   1.2  bouyer 			break;
    555   1.2  bouyer 
    556   1.2  bouyer 		default:
    557   1.2  bouyer #ifdef DIAGNOSTIC
    558   1.2  bouyer 			panic("bad wd mode");
    559   1.2  bouyer #endif
    560   1.2  bouyer 			return;
    561   1.2  bouyer 		}
    562   1.2  bouyer 
    563   1.2  bouyer 		/* Initiate command! */
    564   1.2  bouyer 		if (wdccommand(wdc, d_link, command, d_link->drive,
    565   1.2  bouyer 		    cylin, head, sector, nblks) != 0) {
    566   1.2  bouyer 			wderror(d_link, NULL,
    567   1.2  bouyer 			    "wdc_ata_start: timeout waiting for unbusy");
    568   1.2  bouyer 			wdcunwedge(wdc);
    569   1.2  bouyer 			return;
    570   1.2  bouyer 		}
    571   1.2  bouyer 
    572   1.2  bouyer 		WDDEBUG_PRINT(("sector %d cylin %d head %d addr %x sts %x\n",
    573   1.2  bouyer 		    sector, cylin, head, xfer->databuf,
    574   1.2  bouyer 		    inb(wdc->sc_iobase + wd_altsts)));
    575   1.2  bouyer 
    576   1.2  bouyer 	} else if (xfer->c_nblks > 1) {
    577   1.2  bouyer 		/* The number of blocks in the last stretch may be smaller. */
    578   1.2  bouyer 		nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
    579   1.2  bouyer 		if (xfer->c_nblks > nblks) {
    580   1.2  bouyer 			xfer->c_nblks = nblks;
    581   1.2  bouyer 			xfer->c_nbytes = xfer->c_bcount;
    582   1.2  bouyer 		}
    583   1.2  bouyer 	}
    584   1.2  bouyer 
    585   1.2  bouyer 	/* If this was a write and not using DMA, push the data. */
    586   1.2  bouyer 	if (d_link->sc_mode != WDM_DMA &&
    587   1.2  bouyer 	    (xfer->c_flags & (B_READ|B_WRITE)) == B_WRITE) {
    588   1.2  bouyer 		if (wait_for_drq(wdc) < 0) {
    589   1.2  bouyer 			wderror(d_link, NULL,
    590   1.2  bouyer 			    "wdc_ata_start: timeout waiting for drq");
    591   1.2  bouyer 			wdcunwedge(wdc);
    592   1.2  bouyer 			return;
    593   1.2  bouyer 		}
    594   1.2  bouyer 
    595   1.2  bouyer 		/* Push out data. */
    596   1.2  bouyer 		if ((d_link->sc_flags & WDF_32BIT) == 0)
    597   1.2  bouyer 			outsw(wdc->sc_iobase + wd_data,
    598   1.2  bouyer 			    xfer->databuf + xfer->c_skip,
    599   1.2  bouyer 			    xfer->c_nbytes >> 1);
    600   1.2  bouyer 		else
    601   1.2  bouyer 			outsl(wdc->sc_iobase + wd_data,
    602   1.2  bouyer 			    xfer->databuf + xfer->c_skip,
    603   1.2  bouyer 			    xfer->c_nbytes >> 2);
    604   1.2  bouyer 	}
    605   1.2  bouyer 
    606   1.2  bouyer 	wdc->sc_flags |= WDCF_IRQ_WAIT;
    607   1.2  bouyer 	WDDEBUG_PRINT(("wdc_ata_start: timeout "));
    608   1.2  bouyer 	timeout(wdctimeout, wdc, WAITTIME);
    609   1.2  bouyer 	WDDEBUG_PRINT(("done\n"));
    610   1.2  bouyer }
    611   1.2  bouyer 
    612   1.2  bouyer int
    613   1.2  bouyer wdc_ata_intr(wdc,xfer)
    614   1.2  bouyer 	struct wdc_softc *wdc;
    615   1.2  bouyer 	struct wdc_xfer *xfer;
    616   1.2  bouyer {
    617   1.2  bouyer 	struct wd_link *d_link;
    618   1.2  bouyer 
    619   1.2  bouyer 	d_link = xfer->d_link;
    620   1.2  bouyer 
    621   1.2  bouyer 	if (wait_for_unbusy(wdc) < 0) {
    622   1.2  bouyer 		wdcerror(wdc, "wdcintr: timeout waiting for unbusy");
    623   1.5  bouyer 		return 0;
    624   1.2  bouyer 	}
    625   1.2  bouyer 
    626   1.8  bouyer 	wdc->sc_flags &= ~WDCF_IRQ_WAIT;
    627   1.2  bouyer 	untimeout(wdctimeout, wdc);
    628   1.2  bouyer 
    629   1.2  bouyer 	/* Is it not a transfer, but a control operation? */
    630   1.2  bouyer 	if (d_link->sc_state < READY) {
    631   1.2  bouyer 		if (wdccontrol(wdc, d_link) == 0) {
    632   1.2  bouyer 			/* The drive is busy.  Wait. */
    633   1.2  bouyer 			return 1;
    634   1.2  bouyer 		}
    635   1.2  bouyer 		WDDEBUG_PRINT(("wdc_ata_start from wdc_ata_intr(open) flags 0x%x\n",
    636   1.2  bouyer 		    dc->sc_flags));
    637   1.2  bouyer 		wdc_ata_start(wdc,xfer);
    638   1.2  bouyer 		return 1;
    639   1.2  bouyer 	}
    640   1.2  bouyer 
    641   1.2  bouyer 	/* Turn off the DMA channel. */
    642   1.2  bouyer 	if (d_link->sc_mode == WDM_DMA)
    643   1.2  bouyer 		isa_dmadone(wdc->sc_dev.dv_parent, wdc->sc_drq);
    644   1.2  bouyer 
    645   1.2  bouyer 	/* Have we an error? */
    646   1.2  bouyer 	if (wdc->sc_status & WDCS_ERR) {
    647   1.2  bouyer #ifdef WDDEBUG
    648   1.2  bouyer 		wderror(d_link, NULL, "wdc_ata_start");
    649   1.2  bouyer #endif
    650   1.2  bouyer 		if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
    651   1.2  bouyer 			wdc->sc_flags |= WDCF_ERROR;
    652   1.2  bouyer 			goto restart;
    653   1.2  bouyer 		}
    654   1.2  bouyer 
    655   1.2  bouyer #ifdef B_FORMAT
    656   1.2  bouyer 		if (bp->b_flags & B_FORMAT)
    657   1.2  bouyer 			goto bad;
    658   1.2  bouyer #endif
    659   1.2  bouyer 
    660   1.2  bouyer 		if (++wdc->sc_errors < WDIORETRIES) {
    661   1.2  bouyer 			if (wdc->sc_errors == (WDIORETRIES + 1) / 2) {
    662   1.2  bouyer #if 0
    663   1.2  bouyer 				wderror(wd, NULL, "wedgie");
    664   1.2  bouyer #endif
    665   1.2  bouyer 				wdcunwedge(wdc);
    666   1.2  bouyer 				return 1;
    667   1.2  bouyer 			}
    668   1.2  bouyer 			goto restart;
    669   1.2  bouyer 		}
    670   1.2  bouyer 		wderror(d_link, xfer->c_bp, "wdc_ata_intr hard error");
    671   1.2  bouyer 
    672   1.2  bouyer #ifdef B_FORMAT
    673   1.2  bouyer 	bad:
    674   1.2  bouyer #endif
    675   1.2  bouyer 		xfer->c_flags |= C_ERROR;
    676   1.2  bouyer 		goto done;
    677   1.2  bouyer 	}
    678   1.2  bouyer 
    679   1.2  bouyer 	/* If this was a read and not using DMA, fetch the data. */
    680   1.2  bouyer 	if (d_link->sc_mode != WDM_DMA &&
    681   1.2  bouyer 	    (xfer->c_flags & (B_READ|B_WRITE)) == B_READ) {
    682   1.2  bouyer 		if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
    683   1.2  bouyer 		    != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
    684   1.2  bouyer 			wderror(d_link, NULL, "wdcintr: read intr before drq");
    685   1.2  bouyer 			wdcunwedge(wdc);
    686   1.2  bouyer 			return 1;
    687   1.2  bouyer 		}
    688   1.2  bouyer 
    689   1.2  bouyer 		/* Pull in data. */
    690   1.2  bouyer 		if ((d_link->sc_flags & WDF_32BIT) == 0)
    691   1.2  bouyer 			insw(wdc->sc_iobase+wd_data,
    692   1.2  bouyer 			    xfer->databuf + xfer->c_skip, xfer->c_nbytes >> 1);
    693   1.2  bouyer 		else
    694   1.2  bouyer 			insl(wdc->sc_iobase+wd_data,
    695   1.2  bouyer 			    xfer->databuf + xfer->c_skip, xfer->c_nbytes >> 2);
    696   1.2  bouyer 	}
    697   1.2  bouyer 
    698   1.2  bouyer 	/* If we encountered any abnormalities, flag it as a soft error. */
    699   1.2  bouyer 	if (wdc->sc_errors > 0 ||
    700   1.2  bouyer 	    (wdc->sc_status & WDCS_CORR) != 0) {
    701   1.2  bouyer 		wderror(d_link, xfer->c_bp, "soft error (corrected)");
    702   1.2  bouyer 		wdc->sc_errors = 0;
    703   1.2  bouyer 	}
    704   1.2  bouyer 
    705   1.2  bouyer 	/* Adjust pointers for the next block, if any. */
    706   1.2  bouyer 	xfer->c_blkno += xfer->c_nblks;
    707   1.2  bouyer 	xfer->c_skip += xfer->c_nbytes;
    708   1.2  bouyer 	xfer->c_bcount -= xfer->c_nbytes;
    709   1.2  bouyer 
    710   1.2  bouyer 	/* See if this transfer is complete. */
    711   1.2  bouyer 	if (xfer->c_bcount > 0)
    712   1.2  bouyer 		goto restart;
    713   1.2  bouyer 
    714   1.2  bouyer done:
    715   1.2  bouyer 	/* Done with this transfer, with or without error. */
    716   1.2  bouyer 	wdc_ata_done(wdc, xfer);
    717   1.5  bouyer 	return 1;
    718   1.2  bouyer 
    719   1.2  bouyer restart:
    720   1.2  bouyer 	/* Start the next operation */
    721   1.2  bouyer 	WDDEBUG_PRINT(("wdc_ata_start from wdcintr flags 0x%x\n",
    722   1.2  bouyer 	    wdc->sc_flags));
    723   1.2  bouyer 	wdc_ata_start(wdc, xfer);
    724   1.2  bouyer 
    725   1.2  bouyer 	return 1;
    726   1.2  bouyer }
    727   1.2  bouyer 
    728   1.2  bouyer void
    729   1.2  bouyer wdc_ata_done(wdc, xfer)
    730   1.2  bouyer 	struct wdc_softc *wdc;
    731   1.2  bouyer 	struct wdc_xfer *xfer;
    732   1.2  bouyer {
    733   1.2  bouyer 	struct buf *bp = xfer->c_bp;
    734   1.2  bouyer 	struct wd_link *d_link = xfer->d_link;
    735   1.2  bouyer 	int s;
    736   1.2  bouyer 
    737   1.2  bouyer 	WDDEBUG_PRINT(("wdc_ata_done\n"));
    738   1.2  bouyer 
    739   1.2  bouyer 	/* remove this command from xfer queue */
    740   1.2  bouyer 	s = splbio();
    741   1.2  bouyer 	TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
    742   1.2  bouyer 	wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
    743   1.2  bouyer 	wdc->sc_errors = 0;
    744   1.2  bouyer 	if (bp) {
    745   1.2  bouyer 		if (xfer->c_flags & C_ERROR) {
    746   1.2  bouyer 			bp->b_flags |= B_ERROR;
    747   1.2  bouyer 			bp->b_error = EIO;
    748   1.2  bouyer 		}
    749   1.2  bouyer 		bp->b_resid = xfer->c_bcount;
    750   1.2  bouyer 		wddone(d_link, bp);
    751   1.2  bouyer 		biodone(bp);
    752   1.2  bouyer 	} else {
    753   1.2  bouyer 		wakeup(xfer->databuf);
    754   1.2  bouyer 	}
    755   1.2  bouyer 	xfer->c_skip = 0;
    756   1.2  bouyer 	wdc_free_xfer(xfer);
    757   1.2  bouyer 	d_link->openings++;
    758   1.2  bouyer 	wdstart((void*)d_link->wd_softc);
    759   1.2  bouyer 	WDDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
    760   1.2  bouyer 	    wdc->sc_flags));
    761   1.2  bouyer 	wdcstart(wdc);
    762   1.2  bouyer 	splx(s);
    763   1.2  bouyer }
    764   1.2  bouyer 
    765   1.2  bouyer /*
    766   1.2  bouyer  * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
    767   1.2  bouyer  */
    768   1.2  bouyer int
    769   1.2  bouyer wdc_get_parms(wdc, d_link)
    770   1.2  bouyer 	struct wdc_softc * wdc;
    771   1.2  bouyer 	struct wd_link *d_link;
    772   1.2  bouyer {
    773   1.2  bouyer 	int i;
    774   1.2  bouyer 	char tb[DEV_BSIZE];
    775   1.2  bouyer 	int s, error;
    776   1.2  bouyer 
    777   1.2  bouyer 	/*
    778   1.2  bouyer 	 * XXX
    779   1.2  bouyer 	 * The locking done here, and the length of time this may keep the rest
    780   1.2  bouyer 	 * of the system suspended, is a kluge.  This should be rewritten to
    781   1.2  bouyer 	 * set up a transfer and queue it through wdstart(), but it's called
    782   1.2  bouyer 	 * infrequently enough that this isn't a pressing matter.
    783   1.2  bouyer 	 */
    784   1.2  bouyer 
    785   1.2  bouyer 	s = splbio();
    786   1.2  bouyer 
    787   1.2  bouyer 	while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
    788   1.2  bouyer 		wdc->sc_flags |= WDCF_WANTED;
    789   1.2  bouyer 		if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
    790   1.2  bouyer 			splx(s);
    791   1.2  bouyer 			return error;
    792   1.2  bouyer 		}
    793   1.2  bouyer 	}
    794   1.2  bouyer 
    795   1.2  bouyer 	wdc->sc_flags |= WDCF_ACTIVE;
    796   1.2  bouyer 
    797   1.2  bouyer 	if (wdccommandshort(wdc, d_link->drive, WDCC_IDENTIFY) != 0 ||
    798   1.2  bouyer 	    wait_for_drq(wdc) != 0) {
    799   1.2  bouyer 		/*
    800   1.2  bouyer 		 * We `know' there's a drive here; just assume it's old.
    801   1.2  bouyer 		 * This geometry is only used to read the MBR and print a
    802   1.2  bouyer 		 * (false) attach message.
    803   1.2  bouyer 		 */
    804   1.2  bouyer 		strncpy(d_link->sc_lp->d_typename, "ST506",
    805   1.2  bouyer 		    sizeof d_link->sc_lp->d_typename);
    806   1.2  bouyer 		d_link->sc_lp->d_type = DTYPE_ST506;
    807   1.2  bouyer 
    808   1.2  bouyer 		strncpy(d_link->sc_params.wdp_model, "unknown",
    809   1.2  bouyer 		    sizeof d_link->sc_params.wdp_model);
    810   1.2  bouyer 		d_link->sc_params.wdp_config = WD_CFG_FIXED;
    811   1.2  bouyer 		d_link->sc_params.wdp_cylinders = 1024;
    812   1.2  bouyer 		d_link->sc_params.wdp_heads = 8;
    813   1.2  bouyer 		d_link->sc_params.wdp_sectors = 17;
    814   1.2  bouyer 		d_link->sc_params.wdp_maxmulti = 0;
    815   1.2  bouyer 		d_link->sc_params.wdp_usedmovsd = 0;
    816   1.2  bouyer 		d_link->sc_params.wdp_capabilities = 0;
    817   1.2  bouyer 	} else {
    818   1.2  bouyer 		strncpy(d_link->sc_lp->d_typename, "ESDI/IDE",
    819   1.2  bouyer 		    sizeof d_link->sc_lp->d_typename);
    820   1.2  bouyer 		d_link->sc_lp->d_type = DTYPE_ESDI;
    821   1.2  bouyer 
    822   1.2  bouyer 		/* Read in parameter block. */
    823   1.2  bouyer 		insw(wdc->sc_iobase + wd_data, tb, sizeof(tb) / sizeof(short));
    824   1.2  bouyer 		bcopy(tb, &d_link->sc_params, sizeof(struct wdparams));
    825   1.2  bouyer 
    826   1.2  bouyer 		/* Shuffle string byte order. */
    827   1.2  bouyer 		for (i = 0; i < sizeof(d_link->sc_params.wdp_model); i += 2) {
    828   1.2  bouyer 			u_short *p;
    829   1.2  bouyer 			p = (u_short *)(d_link->sc_params.wdp_model + i);
    830   1.2  bouyer 			*p = ntohs(*p);
    831   1.2  bouyer 		}
    832   1.2  bouyer 	}
    833   1.2  bouyer 
    834   1.2  bouyer 	/* Clear any leftover interrupt. */
    835   1.2  bouyer 	(void) inb(wdc->sc_iobase + wd_status);
    836   1.2  bouyer 
    837   1.2  bouyer 	/* Restart the queue. */
    838   1.2  bouyer 	WDDEBUG_PRINT(("wdcstart from wdc_get_parms flags 0x%x\n",
    839   1.2  bouyer 	    wdc->sc_flags));
    840   1.2  bouyer 	wdc->sc_flags &= ~WDCF_ACTIVE;
    841   1.2  bouyer 	wdcstart(wdc);
    842   1.2  bouyer 
    843   1.2  bouyer 	splx(s);
    844   1.2  bouyer 	return 0;
    845   1.2  bouyer }
    846   1.2  bouyer 
    847   1.2  bouyer /*
    848   1.2  bouyer  * Implement operations needed before read/write.
    849   1.2  bouyer  * Returns 0 if operation still in progress, 1 if completed.
    850   1.2  bouyer  */
    851   1.2  bouyer int
    852   1.2  bouyer wdccontrol(wdc, d_link)
    853   1.2  bouyer 	struct wdc_softc *wdc;
    854   1.2  bouyer 	struct wd_link *d_link;
    855   1.2  bouyer {
    856   1.2  bouyer 	WDDEBUG_PRINT(("wdccontrol\n"));
    857   1.2  bouyer 
    858   1.2  bouyer 	switch (d_link->sc_state) {
    859   1.2  bouyer 	case RECAL:	/* Set SDH, step rate, do recal. */
    860   1.2  bouyer 		if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0) {
    861   1.2  bouyer 			wderror(d_link, NULL, "wdccontrol: recal failed (1)");
    862   1.2  bouyer 			goto bad;
    863   1.2  bouyer 		}
    864   1.2  bouyer 		d_link->sc_state = RECAL_WAIT;
    865   1.2  bouyer 		break;
    866   1.2  bouyer 
    867   1.2  bouyer 	case RECAL_WAIT:
    868   1.2  bouyer 		if (wdc->sc_status & WDCS_ERR) {
    869   1.2  bouyer 			wderror(d_link, NULL, "wdccontrol: recal failed (2)");
    870   1.2  bouyer 			goto bad;
    871   1.2  bouyer 		}
    872   1.2  bouyer 		/* fall through */
    873   1.2  bouyer 
    874   1.2  bouyer 	case GEOMETRY:
    875   1.2  bouyer 		if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
    876   1.2  bouyer 			goto multimode;
    877   1.2  bouyer 		if (wdsetctlr(d_link) != 0) {
    878   1.2  bouyer 			/* Already printed a message. */
    879   1.2  bouyer 			goto bad;
    880   1.2  bouyer 		}
    881   1.2  bouyer 		d_link->sc_state = GEOMETRY_WAIT;
    882   1.2  bouyer 		break;
    883   1.2  bouyer 
    884   1.2  bouyer 	case GEOMETRY_WAIT:
    885   1.2  bouyer 		if (wdc->sc_status & WDCS_ERR) {
    886   1.2  bouyer 			wderror(d_link, NULL, "wdccontrol: geometry failed");
    887   1.2  bouyer 			goto bad;
    888   1.2  bouyer 		}
    889   1.2  bouyer 		/* fall through */
    890   1.2  bouyer 
    891   1.2  bouyer 	case MULTIMODE:
    892   1.2  bouyer 	multimode:
    893   1.2  bouyer 		if (d_link->sc_mode != WDM_PIOMULTI)
    894   1.2  bouyer 			goto ready;
    895   1.2  bouyer 		outb(wdc->sc_iobase + wd_seccnt, d_link->sc_multiple);
    896   1.2  bouyer 		if (wdccommandshort(wdc, d_link->drive,
    897   1.2  bouyer 		    WDCC_SETMULTI) != 0) {
    898   1.2  bouyer 			wderror(d_link, NULL,
    899   1.2  bouyer 			    "wdccontrol: setmulti failed (1)");
    900   1.2  bouyer 			goto bad;
    901   1.2  bouyer 		}
    902   1.2  bouyer 		d_link->sc_state = MULTIMODE_WAIT;
    903   1.2  bouyer 		break;
    904   1.2  bouyer 
    905   1.2  bouyer 	case MULTIMODE_WAIT:
    906   1.2  bouyer 		if (wdc->sc_status & WDCS_ERR) {
    907   1.2  bouyer 			wderror(d_link, NULL,
    908   1.2  bouyer 			    "wdccontrol: setmulti failed (2)");
    909   1.2  bouyer 			goto bad;
    910   1.2  bouyer 		}
    911   1.2  bouyer 		/* fall through */
    912   1.2  bouyer 
    913   1.2  bouyer 	case READY:
    914   1.2  bouyer 	ready:
    915   1.2  bouyer 		wdc->sc_errors = 0;
    916   1.2  bouyer 		d_link->sc_state = READY;
    917   1.2  bouyer 		/*
    918   1.2  bouyer 		 * The rest of the initialization can be done by normal means.
    919   1.2  bouyer 		 */
    920   1.2  bouyer 		return 1;
    921   1.2  bouyer 
    922   1.2  bouyer 	bad:
    923   1.2  bouyer 		wdcunwedge(wdc);
    924   1.2  bouyer 		return 0;
    925   1.2  bouyer 	}
    926   1.2  bouyer 
    927   1.2  bouyer 	wdc->sc_flags |= WDCF_IRQ_WAIT;
    928   1.2  bouyer 	timeout(wdctimeout, wdc, WAITTIME);
    929   1.2  bouyer 	return 0;
    930   1.2  bouyer }
    931   1.2  bouyer 
    932   1.2  bouyer #endif /* NWD > 0 */
    933   1.2  bouyer 
    934   1.2  bouyer 
    935   1.2  bouyer /*
    936   1.2  bouyer  * Interrupt routine for the controller.  Acknowledge the interrupt, check for
    937   1.2  bouyer  * errors on the current operation, mark it done if necessary, and start the
    938   1.2  bouyer  * next request.  Also check for a partially done transfer, and continue with
    939   1.2  bouyer  * the next chunk if so.
    940   1.2  bouyer  */
    941   1.2  bouyer int
    942   1.2  bouyer wdcintr(arg)
    943   1.2  bouyer 	void *arg;
    944   1.2  bouyer {
    945   1.2  bouyer 	struct wdc_softc *wdc = arg;
    946   1.2  bouyer 	struct wdc_xfer *xfer;
    947   1.2  bouyer 
    948   1.2  bouyer 	if ((wdc->sc_flags & WDCF_IRQ_WAIT) == 0) {
    949   1.2  bouyer 		/* Clear the pending interrupt and abort. */
    950   1.2  bouyer 		u_char s = inb(wdc->sc_iobase+wd_status);
    951   1.2  bouyer 
    952   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
    953   1.2  bouyer 		u_char e = inb(wdc->sc_iobase+wd_error);
    954   1.2  bouyer 		u_char i = inb(wdc->sc_iobase+wd_seccnt);
    955   1.2  bouyer 		printf("wdcintr: inactive controller, "
    956   1.2  bouyer 		    "punting st=%02x er=%02x irr=%02x\n", s, e, i);
    957   1.2  bouyer #else
    958   1.2  bouyer 		inb(wdc->sc_iobase+wd_error);
    959   1.2  bouyer 		inb(wdc->sc_iobase+wd_seccnt);
    960   1.2  bouyer #endif
    961   1.2  bouyer 
    962   1.2  bouyer 		if (s & WDCS_DRQ) {
    963   1.2  bouyer 			int len = inb (wdc->sc_iobase + wd_cyl_lo) +
    964   1.2  bouyer 			    256 * inb (wdc->sc_iobase + wd_cyl_hi);
    965   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
    966   1.2  bouyer 			printf ("wdcintr: clearing up %d bytes\n", len);
    967   1.2  bouyer #endif
    968   1.2  bouyer 			wdcbit_bucket (wdc, len);
    969   1.2  bouyer 		}
    970   1.2  bouyer 		return 0;
    971   1.2  bouyer 	}
    972   1.2  bouyer 
    973   1.2  bouyer 	WDDEBUG_PRINT(("wdcintr\n"));
    974   1.2  bouyer 
    975   1.2  bouyer 	xfer = wdc->sc_xfer.tqh_first;
    976   1.2  bouyer #if NATAPIBUS > 0 && NWD > 0
    977   1.2  bouyer 	if (xfer->c_flags & C_ATAPI) {
    978   1.5  bouyer 		return wdc_atapi_intr(wdc,xfer);
    979   1.2  bouyer 	} else
    980   1.2  bouyer 		return wdc_ata_intr(wdc,xfer);
    981   1.2  bouyer #else /* NATAPIBUS > 0  && NWD > 0 */
    982   1.2  bouyer #if NATAPIBUS > 0
    983   1.5  bouyer 	return wdc_atapi_intr(wdc,xfer);
    984   1.2  bouyer #endif /* NATAPIBUS > 0 */
    985   1.2  bouyer #if NWD > 0
    986   1.2  bouyer 	return wdc_ata_intr(wdc,xfer);
    987   1.2  bouyer #endif /* NWD > 0 */
    988   1.2  bouyer #endif /* NATAPIBUS > 0  && NWD > 0 */
    989   1.2  bouyer }
    990   1.2  bouyer 
    991   1.2  bouyer int
    992   1.2  bouyer wdcreset(wdc, verb)
    993   1.2  bouyer 	struct wdc_softc *wdc;
    994   1.2  bouyer 	int verb;
    995   1.2  bouyer {
    996   1.2  bouyer 	int iobase = wdc->sc_iobase;
    997   1.2  bouyer 
    998   1.2  bouyer 	/* Reset the device. */
    999   1.2  bouyer 	outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1000   1.2  bouyer 	delay(1000);
   1001   1.2  bouyer 	outb(iobase+wd_ctlr, WDCTL_IDS);
   1002   1.2  bouyer 	delay(1000);
   1003   1.2  bouyer 	(void) inb(iobase+wd_error);
   1004   1.2  bouyer 	outb(iobase+wd_ctlr, WDCTL_4BIT);
   1005   1.2  bouyer 
   1006   1.2  bouyer 	if (wait_for_unbusy(wdc) < 0) {
   1007   1.2  bouyer 		if (verb)
   1008   1.2  bouyer 			printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
   1009   1.2  bouyer 		return 1;
   1010   1.2  bouyer 	}
   1011   1.2  bouyer 
   1012   1.2  bouyer 	return 0;
   1013   1.2  bouyer }
   1014   1.2  bouyer 
   1015   1.2  bouyer void
   1016   1.2  bouyer wdcrestart(arg)
   1017   1.2  bouyer 	void *arg;
   1018   1.2  bouyer {
   1019   1.2  bouyer 	struct wdc_softc *wdc = arg;
   1020   1.2  bouyer 	int s;
   1021   1.2  bouyer 
   1022   1.2  bouyer 	s = splbio();
   1023   1.2  bouyer 	wdcstart(wdc);
   1024   1.2  bouyer 	splx(s);
   1025   1.2  bouyer }
   1026   1.2  bouyer 
   1027   1.2  bouyer /*
   1028   1.2  bouyer  * Unwedge the controller after an unexpected error.  We do this by resetting
   1029   1.2  bouyer  * it, marking all drives for recalibration, and stalling the queue for a short
   1030   1.2  bouyer  * period to give the reset time to finish.
   1031   1.2  bouyer  * NOTE: We use a timeout here, so this routine must not be called during
   1032   1.2  bouyer  * autoconfig or dump.
   1033   1.2  bouyer  */
   1034   1.2  bouyer void
   1035   1.2  bouyer wdcunwedge(wdc)
   1036   1.2  bouyer 	struct wdc_softc *wdc;
   1037   1.2  bouyer {
   1038   1.2  bouyer 	int unit;
   1039   1.2  bouyer 
   1040   1.2  bouyer #ifdef ATAPI_DEBUG
   1041   1.2  bouyer 	printf("wdcunwedge\n");
   1042   1.2  bouyer #endif
   1043   1.2  bouyer 
   1044   1.2  bouyer 	untimeout(wdctimeout, wdc);
   1045   1.2  bouyer 	wdc->sc_flags &= ~WDCF_IRQ_WAIT;
   1046   1.2  bouyer 	(void) wdcreset(wdc, VERBOSE);
   1047   1.2  bouyer 
   1048   1.2  bouyer 	/* Schedule recalibrate for all drives on this controller. */
   1049   1.2  bouyer 	for (unit = 0; unit < 2; unit++) {
   1050   1.8  bouyer 		if (!wdc->d_link[unit])
   1051   1.8  bouyer 			wdccommandshort(wdc, unit, ATAPI_SOFT_RESET);
   1052   1.8  bouyer 		else if (wdc->d_link[unit]->sc_state > RECAL)
   1053   1.2  bouyer 			wdc->d_link[unit]->sc_state = RECAL;
   1054   1.2  bouyer 	}
   1055   1.2  bouyer 
   1056   1.2  bouyer 	wdc->sc_flags |= WDCF_ERROR;
   1057   1.2  bouyer 	++wdc->sc_errors;
   1058   1.2  bouyer 
   1059   1.2  bouyer 	/* Wake up in a little bit and restart the operation. */
   1060   1.2  bouyer 	WDDEBUG_PRINT(("wdcrestart from wdcunwedge\n"));
   1061   1.2  bouyer 	wdc->sc_flags &= ~WDCF_ACTIVE;
   1062   1.2  bouyer 	timeout(wdcrestart, wdc, RECOVERYTIME);
   1063   1.2  bouyer }
   1064   1.2  bouyer 
   1065   1.2  bouyer int
   1066   1.2  bouyer wdcwait(wdc, mask)
   1067   1.2  bouyer 	struct wdc_softc *wdc;
   1068   1.2  bouyer 	int mask;
   1069   1.2  bouyer {
   1070   1.2  bouyer 	int iobase = wdc->sc_iobase;
   1071   1.2  bouyer 	int timeout = 0;
   1072   1.2  bouyer 	u_char status;
   1073   1.2  bouyer #ifdef WDCNDELAY_DEBUG
   1074   1.2  bouyer 	extern int cold;
   1075   1.2  bouyer #endif
   1076   1.2  bouyer 
   1077   1.2  bouyer 	WDDEBUG_PRINT(("wdcwait iobase %x\n", iobase));
   1078   1.2  bouyer 
   1079   1.2  bouyer 	for (;;) {
   1080   1.2  bouyer 		wdc->sc_status = status = inb(iobase+wd_status);
   1081   1.2  bouyer 		/*
   1082   1.2  bouyer 		 * XXX
   1083   1.2  bouyer 		 * If a single slave ATAPI device is attached, it may
   1084   1.2  bouyer 		 * have released the bus. Select it and try again.
   1085   1.2  bouyer 		 */
   1086   1.2  bouyer 		if (status == 0xff && wdc->sc_flags & WDCF_ONESLAVE) {
   1087   1.2  bouyer 			outb(iobase+wd_sdh, WDSD_IBM | 0x10);
   1088   1.2  bouyer 			wdc->sc_status = status = inb(iobase+wd_status);
   1089   1.2  bouyer 		}
   1090   1.2  bouyer 		if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
   1091   1.2  bouyer 			break;
   1092   1.2  bouyer 		if (++timeout > WDCNDELAY) {
   1093   1.3  bouyer #ifdef ATAPI_DEBUG
   1094   1.2  bouyer 			printf("wdcwait: timeout, status %x error %x\n", status, inb(iobase+wd_error));
   1095   1.2  bouyer #endif
   1096   1.2  bouyer 			return -1;
   1097   1.2  bouyer 		}
   1098   1.2  bouyer 		delay(WDCDELAY);
   1099   1.2  bouyer 	}
   1100   1.2  bouyer 	if (status & WDCS_ERR) {
   1101   1.2  bouyer 		wdc->sc_error = inb(iobase+wd_error);
   1102   1.2  bouyer 		return WDCS_ERR;
   1103   1.2  bouyer 	}
   1104   1.2  bouyer #ifdef WDCNDELAY_DEBUG
   1105   1.2  bouyer 	/* After autoconfig, there should be no long delays. */
   1106   1.2  bouyer 	if (!cold && timeout > WDCNDELAY_DEBUG) {
   1107   1.2  bouyer 		struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
   1108   1.2  bouyer 		if (xfer == NULL)
   1109   1.2  bouyer 			printf("%s: warning: busy-wait took %dus\n",
   1110   1.2  bouyer 	    		wdc->sc_dev.dv_xname, WDCDELAY * timeout);
   1111   1.2  bouyer 		else
   1112   1.2  bouyer 			printf("%s(%s): warning: busy-wait took %dus\n",
   1113   1.2  bouyer 				wdc->sc_dev.dv_xname,
   1114   1.2  bouyer 			    ((struct device*)xfer->d_link->wd_softc)->dv_xname,
   1115   1.2  bouyer 				WDCDELAY * timeout);
   1116   1.2  bouyer 	}
   1117   1.2  bouyer #endif
   1118   1.2  bouyer 	return 0;
   1119   1.2  bouyer }
   1120   1.2  bouyer 
   1121   1.2  bouyer void
   1122   1.2  bouyer wdctimeout(arg)
   1123   1.2  bouyer 	void *arg;
   1124   1.2  bouyer {
   1125   1.2  bouyer 	struct wdc_softc *wdc = (struct wdc_softc *)arg;
   1126   1.8  bouyer 	struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
   1127   1.2  bouyer 	int s;
   1128   1.2  bouyer 
   1129   1.2  bouyer 	WDDEBUG_PRINT(("wdctimeout\n"));
   1130   1.2  bouyer 
   1131   1.2  bouyer 	s = splbio();
   1132   1.2  bouyer 	if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0) {
   1133   1.8  bouyer 		wdcerror(wdc, "lost interrupt");
   1134   1.8  bouyer 		printf("\ttype: %s\n", (xfer->c_flags & C_ATAPI) ? "atapi":"ata");
   1135   1.8  bouyer 		printf("\tc_bcount: %d\n", xfer->c_bcount);
   1136   1.8  bouyer 		printf("\tc_skip: %d\n", xfer->c_skip);
   1137   1.8  bouyer 		wdcintr(wdc);
   1138   1.2  bouyer 		wdc->sc_flags &= ~WDCF_IRQ_WAIT;
   1139   1.2  bouyer 		wdcunwedge(wdc);
   1140   1.2  bouyer 	} else
   1141   1.2  bouyer 		wdcerror(wdc, "missing untimeout");
   1142   1.2  bouyer 	splx(s);
   1143   1.2  bouyer }
   1144   1.2  bouyer 
   1145   1.2  bouyer /*
   1146   1.2  bouyer  * Wait for the drive to become ready and send a command.
   1147   1.2  bouyer  * Return -1 if busy for too long or 0 otherwise.
   1148   1.2  bouyer  * Assumes interrupts are blocked.
   1149   1.2  bouyer  */
   1150   1.2  bouyer int
   1151   1.2  bouyer wdccommand(wdc, d_link, command, drive, cylin, head, sector, count)
   1152   1.2  bouyer 		struct wdc_softc *wdc;
   1153   1.2  bouyer         struct wd_link *d_link;
   1154   1.2  bouyer         int command;
   1155   1.2  bouyer         int drive, cylin, head, sector, count;
   1156   1.2  bouyer {
   1157   1.2  bouyer         int iobase = wdc->sc_iobase;
   1158   1.2  bouyer         int stat;
   1159   1.2  bouyer 
   1160   1.2  bouyer 	WDDEBUG_PRINT(("wdccommand drive %d\n", drive));
   1161   1.2  bouyer 
   1162   1.2  bouyer #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
   1163   1.2  bouyer 	if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
   1164   1.2  bouyer 		printf("wdccommand: controler not active (drive %d)\n", drive);
   1165   1.2  bouyer #endif
   1166   1.2  bouyer 
   1167   1.2  bouyer         /* Select drive, head, and addressing mode. */
   1168   1.2  bouyer         outb(iobase+wd_sdh, WDSD_IBM | (drive << 4) | head);
   1169   1.2  bouyer 
   1170   1.2  bouyer         /* Wait for it to become ready to accept a command. */
   1171   1.2  bouyer         if (command == WDCC_IDP || d_link->type == ATAPI)
   1172   1.2  bouyer                 stat = wait_for_unbusy(wdc);
   1173   1.2  bouyer         else
   1174   1.2  bouyer                 stat = wdcwait(wdc, WDCS_DRDY);
   1175   1.2  bouyer 
   1176   1.2  bouyer         if (stat < 0) {
   1177   1.2  bouyer #ifdef ATAPI_DEBUG
   1178   1.2  bouyer 		printf("wdcommand: xfer failed (wait_for_unbusy) status %d\n",
   1179   1.2  bouyer 		    stat);
   1180   1.2  bouyer #endif
   1181   1.2  bouyer                 return -1;
   1182   1.2  bouyer 	}
   1183   1.2  bouyer 
   1184   1.2  bouyer         /* Load parameters. */
   1185   1.2  bouyer         if (d_link->type == ATA && d_link->sc_lp->d_type == DTYPE_ST506)
   1186   1.2  bouyer                 outb(iobase + wd_precomp, d_link->sc_lp->d_precompcyl / 4);
   1187   1.2  bouyer         else
   1188   1.2  bouyer                 outb(iobase + wd_features, 0);
   1189   1.2  bouyer         outb(iobase + wd_cyl_lo, cylin);
   1190   1.2  bouyer         outb(iobase + wd_cyl_hi, cylin >> 8);
   1191   1.2  bouyer         outb(iobase + wd_sector, sector);
   1192   1.2  bouyer         outb(iobase + wd_seccnt, count);
   1193   1.2  bouyer 
   1194   1.2  bouyer         /* Send command. */
   1195   1.2  bouyer         outb(iobase + wd_command, command);
   1196   1.2  bouyer 
   1197   1.2  bouyer         return 0;
   1198   1.2  bouyer }
   1199   1.2  bouyer 
   1200   1.2  bouyer /*
   1201   1.2  bouyer  * Simplified version of wdccommand().
   1202   1.2  bouyer  */
   1203   1.2  bouyer int
   1204   1.2  bouyer wdccommandshort(wdc, drive, command)
   1205   1.2  bouyer 	struct wdc_softc *wdc;
   1206   1.2  bouyer         int drive;
   1207   1.2  bouyer         int command;
   1208   1.2  bouyer {
   1209   1.2  bouyer 	int iobase = wdc->sc_iobase;
   1210   1.2  bouyer 
   1211   1.2  bouyer 	WDDEBUG_PRINT(("wdccommandshort\n"));
   1212   1.2  bouyer 
   1213   1.2  bouyer #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
   1214   1.2  bouyer 	if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
   1215  1.10   mikel 		printf("wdccommandshort: controller not active (drive %d)\n",
   1216   1.2  bouyer 		    drive);
   1217   1.2  bouyer #endif
   1218   1.2  bouyer 
   1219   1.2  bouyer         /* Select drive. */
   1220   1.2  bouyer         outb(iobase + wd_sdh, WDSD_IBM | (drive << 4));
   1221   1.2  bouyer 
   1222   1.2  bouyer         if (wdcwait(wdc, WDCS_DRDY) < 0)
   1223   1.2  bouyer                 return -1;
   1224   1.2  bouyer 
   1225   1.2  bouyer         outb(iobase + wd_command, command);
   1226   1.2  bouyer 
   1227   1.2  bouyer 	return 0;
   1228   1.2  bouyer }
   1229   1.2  bouyer 
   1230   1.2  bouyer void
   1231   1.2  bouyer wdc_exec_xfer(wdc, d_link, xfer)
   1232   1.2  bouyer 	struct wdc_softc *wdc;
   1233   1.2  bouyer 	struct wd_link *d_link;
   1234   1.2  bouyer 	struct wdc_xfer *xfer;
   1235   1.2  bouyer {
   1236   1.2  bouyer 	int s;
   1237   1.2  bouyer 
   1238   1.2  bouyer 	WDDEBUG_PRINT(("wdc_exec_xfer\n"));
   1239   1.2  bouyer 
   1240   1.2  bouyer 	s = splbio();
   1241   1.2  bouyer 
   1242   1.2  bouyer 	/* insert at the end of command list */
   1243   1.2  bouyer 	TAILQ_INSERT_TAIL(&wdc->sc_xfer,xfer , c_xferchain)
   1244   1.2  bouyer 	WDDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
   1245   1.2  bouyer 	    wdc->sc_flags));
   1246   1.2  bouyer 	wdcstart(wdc);
   1247   1.2  bouyer 	xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
   1248   1.2  bouyer 	splx(s);
   1249   1.2  bouyer }
   1250   1.2  bouyer 
   1251   1.2  bouyer struct wdc_xfer *
   1252   1.2  bouyer wdc_get_xfer(flags)
   1253   1.2  bouyer 	int flags;
   1254   1.2  bouyer {
   1255   1.2  bouyer 	struct wdc_xfer *xfer;
   1256   1.2  bouyer 	int s;
   1257   1.2  bouyer 
   1258   1.2  bouyer 	s = splbio();
   1259   1.2  bouyer 	if ((xfer = xfer_free_list.lh_first) != NULL) {
   1260   1.2  bouyer 		LIST_REMOVE(xfer, free_list);
   1261   1.2  bouyer 		splx(s);
   1262   1.2  bouyer #ifdef DIAGNOSTIC
   1263   1.2  bouyer 		if ((xfer->c_flags & C_INUSE) != 0)
   1264   1.2  bouyer 			panic("wdc_get_xfer: xfer already in use\n");
   1265   1.2  bouyer #endif
   1266   1.2  bouyer 	} else {
   1267   1.2  bouyer 		splx(s);
   1268   1.8  bouyer #ifdef ATAPI_DEBUG2
   1269   1.2  bouyer 		printf("wdc:making xfer %d\n",wdc_nxfer);
   1270   1.2  bouyer #endif
   1271   1.2  bouyer 		xfer = malloc(sizeof(*xfer), M_DEVBUF,
   1272   1.2  bouyer 		    ((flags & IDE_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
   1273   1.2  bouyer 		if (xfer == NULL)
   1274   1.2  bouyer 			return 0;
   1275   1.2  bouyer 
   1276   1.2  bouyer #ifdef DIAGNOSTIC
   1277   1.2  bouyer 		xfer->c_flags &= ~C_INUSE;
   1278   1.2  bouyer #endif
   1279   1.8  bouyer #ifdef ATAPI_DEBUG2
   1280   1.2  bouyer 		wdc_nxfer++;
   1281   1.2  bouyer #endif
   1282   1.2  bouyer 	}
   1283   1.2  bouyer #ifdef DIAGNOSTIC
   1284   1.2  bouyer 	if ((xfer->c_flags & C_INUSE) != 0)
   1285   1.2  bouyer 		panic("wdc_get_xfer: xfer already in use\n");
   1286   1.2  bouyer #endif
   1287   1.2  bouyer 	bzero(xfer,sizeof(struct wdc_xfer));
   1288   1.2  bouyer 	xfer->c_flags = C_INUSE;
   1289   1.2  bouyer 	return xfer;
   1290   1.2  bouyer }
   1291   1.2  bouyer 
   1292   1.2  bouyer void
   1293   1.2  bouyer wdc_free_xfer(xfer)
   1294   1.2  bouyer 	struct wdc_xfer *xfer;
   1295   1.2  bouyer {
   1296   1.2  bouyer 	int s;
   1297   1.2  bouyer 
   1298   1.2  bouyer 	s = splbio();
   1299   1.2  bouyer 	xfer->c_flags &= ~C_INUSE;
   1300   1.2  bouyer 	LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
   1301   1.2  bouyer 	splx(s);
   1302   1.2  bouyer }
   1303   1.2  bouyer 
   1304   1.2  bouyer void
   1305   1.2  bouyer wdcerror(wdc, msg)
   1306   1.2  bouyer 	struct wdc_softc *wdc;
   1307   1.2  bouyer 	char *msg;
   1308   1.2  bouyer {
   1309   1.2  bouyer 	struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
   1310   1.2  bouyer 	if (xfer == NULL)
   1311   1.2  bouyer 		printf("%s: %s\n", wdc->sc_dev.dv_xname, msg);
   1312   1.2  bouyer 	else
   1313   1.2  bouyer 		printf("%s(%d): %s\n", wdc->sc_dev.dv_xname,
   1314   1.2  bouyer 		    xfer->d_link->drive, msg);
   1315   1.2  bouyer }
   1316   1.2  bouyer 
   1317   1.2  bouyer /*
   1318   1.2  bouyer  * the bit bucket
   1319   1.2  bouyer  */
   1320   1.2  bouyer void
   1321   1.2  bouyer wdcbit_bucket(wdc, size)
   1322   1.2  bouyer 	struct wdc_softc *wdc;
   1323   1.2  bouyer 	int size;
   1324   1.2  bouyer {
   1325   1.2  bouyer 	int iobase = wdc->sc_iobase;
   1326   1.2  bouyer 	int i;
   1327   1.2  bouyer 
   1328   1.2  bouyer 	for (i = 0 ; i < size / 2 ; i++) {
   1329   1.2  bouyer 		short null;
   1330   1.2  bouyer 		(void)insw(iobase + wd_data, &null, 1);
   1331   1.2  bouyer 	}
   1332   1.2  bouyer 
   1333   1.2  bouyer 	if (size % 2)
   1334   1.2  bouyer 		(void)inb(iobase + wd_data);
   1335   1.2  bouyer }
   1336   1.2  bouyer 
   1337   1.2  bouyer 
   1338   1.2  bouyer #if NATAPIBUS > 0
   1339   1.2  bouyer 
   1340   1.2  bouyer void
   1341   1.2  bouyer wdc_atapi_minphys (struct buf *bp)
   1342   1.2  bouyer {
   1343   1.2  bouyer     if(bp->b_bcount > MAX_SIZE)
   1344   1.2  bouyer 		bp->b_bcount = MAX_SIZE;
   1345   1.2  bouyer 	minphys(bp);
   1346   1.2  bouyer }
   1347   1.2  bouyer 
   1348   1.2  bouyer 
   1349   1.2  bouyer void
   1350   1.2  bouyer wdc_atapi_start(wdc, xfer)
   1351   1.2  bouyer 	struct wdc_softc *wdc;
   1352   1.2  bouyer 	struct wdc_xfer *xfer;
   1353   1.2  bouyer {
   1354   1.2  bouyer 	struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
   1355   1.2  bouyer 
   1356   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1357   1.2  bouyer 	printf("wdc_atapi_start, acp flags %x \n",sc_xfer->flags);
   1358   1.2  bouyer #endif
   1359   1.2  bouyer 	if (wdc->sc_errors >= WDIORETRIES) {
   1360   1.2  bouyer 		if ((wdc->sc_status & WDCS_ERR) == 0) {
   1361   1.2  bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP; /* XXX do we know more ? */
   1362   1.2  bouyer 		} else {
   1363   1.2  bouyer 			sc_xfer->error = XS_SENSE;
   1364   1.2  bouyer 			sc_xfer->sense.atapi_sense = inb (wdc->sc_iobase + wd_error);
   1365   1.2  bouyer 		}
   1366   1.2  bouyer 		wdc_atapi_done(wdc, xfer);
   1367   1.2  bouyer 		return;
   1368   1.2  bouyer 	}
   1369   1.2  bouyer 	if (wait_for_unbusy(wdc) != 0) {
   1370   1.2  bouyer 		if ((wdc->sc_status & WDCS_ERR) == 0) {
   1371   1.2  bouyer 			printf("wdc_atapi_start: not ready, st = %02x\n",
   1372   1.2  bouyer 			    wdc->sc_status);
   1373   1.2  bouyer 			sc_xfer->error = XS_SELTIMEOUT;
   1374   1.2  bouyer 		}
   1375   1.2  bouyer #if 0 /* don't get the sense yet, as this may be just UNIT ATTENTION */
   1376   1.2  bouyer 		else {
   1377   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1378   1.2  bouyer 			printf("wdc_atapi_start: sense %02x\n", wdc->sc_error);
   1379   1.2  bouyer #endif
   1380   1.2  bouyer 			sc_xfer->error = XS_SENSE;
   1381   1.2  bouyer 			sc_xfer->sense.atapi_sense = wdc->sc_error;
   1382   1.2  bouyer 		}
   1383   1.2  bouyer 		wdc_atapi_done(wdc, xfer);
   1384   1.2  bouyer 		return;
   1385   1.2  bouyer #endif
   1386   1.2  bouyer 	}
   1387   1.2  bouyer 
   1388   1.2  bouyer 	if (wdccommand(wdc, (struct wd_link*)xfer->d_link, ATAPI_PACKET_COMMAND,
   1389   1.2  bouyer 	    sc_xfer->sc_link->scsipi_atapi.drive, sc_xfer->datalen,
   1390   1.2  bouyer 		0, 0, 0) != 0) {
   1391  1.10   mikel 		printf("wdc_atapi_start: can't send atapi packet command\n");
   1392   1.2  bouyer 		sc_xfer->error = XS_DRIVER_STUFFUP;
   1393   1.2  bouyer 		wdc_atapi_done(wdc, xfer);
   1394   1.2  bouyer 		return;
   1395   1.2  bouyer 	}
   1396   1.2  bouyer 	if ((sc_xfer->sc_link->scsipi_atapi.cap  & 0x0300) != ACAP_DRQ_INTR) {
   1397   1.2  bouyer 		int i, phase;
   1398   1.2  bouyer 		for (i=20000; i>0; --i) {
   1399   1.2  bouyer 			phase = (inb(wdc->sc_iobase + wd_ireason) &
   1400   1.2  bouyer 			    (WDCI_CMD | WDCI_IN)) |
   1401   1.2  bouyer 			    (inb(wdc->sc_iobase + wd_status) & WDCS_DRQ);
   1402   1.2  bouyer 			if (phase == PHASE_CMDOUT)
   1403   1.2  bouyer 				break;
   1404   1.2  bouyer 			delay(10);
   1405   1.2  bouyer 		}
   1406   1.2  bouyer 		if (phase != PHASE_CMDOUT ) {
   1407  1.10   mikel 			printf("wdc_atapi_start: timeout waiting PHASE_CMDOUT");
   1408   1.2  bouyer 			sc_xfer->error = XS_SELTIMEOUT;
   1409   1.2  bouyer 			wdc_atapi_done(wdc, xfer);
   1410   1.2  bouyer 			return;
   1411   1.2  bouyer 		}
   1412   1.2  bouyer 		outsw(wdc->sc_iobase + wd_data, sc_xfer->cmd,
   1413   1.2  bouyer 		    sc_xfer->cmdlen / sizeof(short));
   1414   1.2  bouyer 	}
   1415   1.2  bouyer 	wdc->sc_flags |= WDCF_IRQ_WAIT;
   1416   1.2  bouyer 
   1417   1.2  bouyer #ifdef ATAPI_DEBUG2
   1418   1.2  bouyer 	printf("wdc_atapi_start: timeout\n");
   1419   1.2  bouyer #endif
   1420   1.2  bouyer 	timeout(wdctimeout, wdc, WAITTIME);
   1421   1.2  bouyer 	return;
   1422   1.2  bouyer }
   1423   1.2  bouyer 
   1424   1.2  bouyer 
   1425   1.2  bouyer int
   1426   1.2  bouyer wdc_atapi_get_params(ab_link, drive, id)
   1427   1.2  bouyer 	struct scsipi_link *ab_link;
   1428   1.2  bouyer 	u_int8_t drive;
   1429   1.2  bouyer 	struct atapi_identify *id;
   1430   1.2  bouyer {
   1431   1.2  bouyer 	struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
   1432   1.2  bouyer 	int status, len, excess = 0;
   1433   1.2  bouyer 	int s, error;
   1434   1.2  bouyer 
   1435   1.4  bouyer 	/* if a disk is already present, skip */
   1436   1.4  bouyer 	if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
   1437   1.2  bouyer #ifdef ATAPI_DEBUG_PROBE
   1438   1.4  bouyer 		printf("wdc_atapi_get_params: drive %d present\n", drive);
   1439   1.2  bouyer #endif
   1440   1.2  bouyer 		return 0;
   1441   1.2  bouyer 	}
   1442   1.2  bouyer 
   1443   1.2  bouyer 	/*
   1444   1.2  bouyer 	 * If there is only one ATAPI slave on the bus,don't probe
   1445   1.2  bouyer 	 * drive 0 (master)
   1446   1.2  bouyer 	 */
   1447   1.2  bouyer 
   1448   1.2  bouyer 	if (wdc->sc_flags & WDCF_ONESLAVE && drive != 1)
   1449   1.2  bouyer 		return 0;
   1450   1.2  bouyer 
   1451   1.2  bouyer #ifdef ATAPI_DEBUG_PROBE
   1452   1.2  bouyer 	printf("wdc_atapi_get_params: probing drive %d\n", drive);
   1453   1.2  bouyer #endif
   1454   1.2  bouyer 
   1455   1.2  bouyer 	/*
   1456   1.2  bouyer 	 * XXX
   1457   1.2  bouyer 	 * The locking done here, and the length of time this may keep the rest
   1458   1.2  bouyer 	 * of the system suspended, is a kluge.  This should be rewritten to
   1459   1.2  bouyer 	 * set up a transfer and queue it through wdstart(), but it's called
   1460   1.2  bouyer 	 * infrequently enough that this isn't a pressing matter.
   1461   1.2  bouyer 	 */
   1462   1.2  bouyer 
   1463   1.2  bouyer 	s = splbio();
   1464   1.2  bouyer 
   1465   1.2  bouyer 	while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
   1466   1.2  bouyer 		wdc->sc_flags |= WDCF_WANTED;
   1467   1.2  bouyer 		if ((error = tsleep(wdc, PRIBIO | PCATCH, "atprm", 0)) != 0) {
   1468   1.2  bouyer 			splx(s);
   1469   1.2  bouyer 			return error;
   1470   1.2  bouyer 		}
   1471   1.2  bouyer 	}
   1472   1.2  bouyer 
   1473   1.2  bouyer 	wdc->sc_flags |= WDCF_ACTIVE;
   1474   1.2  bouyer 	error = 1;
   1475   1.2  bouyer 	(void)wdcreset(wdc, VERBOSE);
   1476   1.2  bouyer 	if ((status = wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
   1477   1.2  bouyer 	    ATAPI_SOFT_RESET, drive, 0, 0, 0, 0)) != 0) {
   1478   1.2  bouyer #ifdef ATAPI_DEBUG
   1479   1.2  bouyer 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET"
   1480   1.2  bouyer 		    "failed for drive %d: status %d error %d\n",
   1481   1.2  bouyer 		    drive, status, wdc->sc_error);
   1482   1.2  bouyer #endif
   1483   1.2  bouyer 		error = 0;
   1484   1.2  bouyer 		goto end;
   1485   1.2  bouyer 	}
   1486   1.2  bouyer 	if ((status = wait_for_unbusy(wdc)) != 0) {
   1487   1.2  bouyer #ifdef ATAPI_DEBUG
   1488   1.2  bouyer 	printf("wdc_atapi_get_params: wait_for_unbusy failed "
   1489   1.2  bouyer 	    "for drive %d: status %d error %d\n",
   1490   1.2  bouyer 	    drive, status, wdc->sc_error);
   1491   1.2  bouyer #endif
   1492   1.2  bouyer 		error = 0;
   1493   1.2  bouyer 		goto end;
   1494   1.2  bouyer 	}
   1495   1.2  bouyer 
   1496   1.2  bouyer 	if (wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
   1497   1.2  bouyer 		ATAPI_IDENTIFY_DEVICE, drive, sizeof(struct atapi_identify),
   1498   1.2  bouyer 		0, 0, 0) != 0 ||
   1499   1.2  bouyer 	    atapi_ready(wdc) != 0) {
   1500   1.2  bouyer #ifdef ATAPI_DEBUG_PROBE
   1501   1.2  bouyer 		printf("ATAPI_IDENTIFY_DEVICE failed for drive %d\n", drive);
   1502   1.2  bouyer #endif
   1503   1.2  bouyer 		error = 0;
   1504   1.2  bouyer 		goto end;
   1505   1.2  bouyer 	}
   1506   1.2  bouyer 	len = inb(wdc->sc_iobase + wd_cyl_lo) + 256 *
   1507   1.2  bouyer 	    inb(wdc->sc_iobase + wd_cyl_hi);
   1508   1.2  bouyer 	if (len != sizeof(struct atapi_identify)) {
   1509   1.2  bouyer 		printf("Warning drive %d returned %d/%d of "
   1510   1.2  bouyer 		    "indentify device data\n", drive, len,
   1511   1.2  bouyer 		    sizeof(struct atapi_identify));
   1512   1.2  bouyer 		excess = len - sizeof(struct atapi_identify);
   1513   1.2  bouyer 		if (excess < 0)
   1514   1.2  bouyer 			excess = 0;
   1515   1.2  bouyer 	}
   1516   1.2  bouyer 	insw(wdc->sc_iobase + wd_data, id,
   1517   1.2  bouyer 	    sizeof(struct atapi_identify)/sizeof(short));
   1518   1.2  bouyer 	wdcbit_bucket(wdc, excess);
   1519   1.4  bouyer 	wdc->sc_drives_mask |= (1 << drive);
   1520   1.2  bouyer 
   1521   1.2  bouyer  end:	/* Restart the queue. */
   1522   1.2  bouyer 	WDDEBUG_PRINT(("wdcstart from wdc_atapi_get_parms flags 0x%x\n",
   1523   1.2  bouyer 	    wdc->sc_flags));
   1524   1.2  bouyer 	wdc->sc_flags &= ~WDCF_ACTIVE;
   1525   1.2  bouyer 	wdcstart(wdc);
   1526   1.2  bouyer 	splx(s);
   1527   1.2  bouyer 	return error;
   1528   1.2  bouyer }
   1529   1.2  bouyer 
   1530   1.2  bouyer int
   1531   1.2  bouyer wdc_atapi_send_command_packet(sc_xfer)
   1532   1.2  bouyer 	struct scsipi_xfer *sc_xfer;
   1533   1.2  bouyer {
   1534   1.2  bouyer 	struct scsipi_link *sc_link = sc_xfer->sc_link;
   1535   1.2  bouyer 	struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
   1536   1.2  bouyer 	struct wdc_xfer *xfer;
   1537   1.2  bouyer 	int flags = sc_xfer->flags;
   1538   1.8  bouyer 
   1539   1.2  bouyer 	if (flags & SCSI_POLL) {   /* should use the queue and wdc_atapi_start */
   1540   1.2  bouyer 		struct wdc_xfer xfer_s;
   1541   1.2  bouyer 		int i, s;
   1542   1.2  bouyer 
   1543   1.2  bouyer 		s = splbio();
   1544   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1545   1.2  bouyer 		printf("wdc_atapi_send_cmd: "
   1546   1.2  bouyer 		    "flags 0x%x drive %d cmdlen %d datalen %d",
   1547   1.2  bouyer 		    sc_xfer->flags, sc_link->scsipi_atapi.drive, sc_xfer->cmdlen,
   1548   1.2  bouyer 			sc_xfer->datalen);
   1549   1.2  bouyer #endif
   1550   1.2  bouyer 		xfer = &xfer_s;
   1551   1.2  bouyer 		bzero(xfer, sizeof(xfer_s));
   1552   1.2  bouyer 		xfer->c_flags = C_INUSE|C_ATAPI|flags;
   1553   1.2  bouyer 		xfer->d_link = (struct wd_link *)(&sc_link->scsipi_atapi);
   1554   1.2  bouyer 		xfer->c_bp = sc_xfer->bp;
   1555   1.2  bouyer 		xfer->atapi_cmd = sc_xfer;
   1556   1.2  bouyer 		xfer->c_blkno = 0;
   1557   1.2  bouyer 		xfer->databuf = sc_xfer->data;
   1558   1.2  bouyer 		xfer->c_bcount = sc_xfer->datalen;
   1559   1.2  bouyer 		if (wait_for_unbusy (wdc) != 0)  {
   1560   1.2  bouyer 			if ((wdc->sc_status & WDCS_ERR) == 0) {
   1561   1.2  bouyer 				printf("wdc_atapi_send_command: not ready, "
   1562   1.2  bouyer 				    "st = %02x\n", wdc->sc_status);
   1563   1.2  bouyer 				sc_xfer->error = XS_SELTIMEOUT;
   1564   1.2  bouyer 			} else {
   1565   1.2  bouyer 				sc_xfer->error = XS_SENSE;
   1566   1.2  bouyer 				sc_xfer->sense.atapi_sense = wdc->sc_error;
   1567   1.2  bouyer 			}
   1568   1.2  bouyer 			splx(s);
   1569   1.2  bouyer 			return COMPLETE;
   1570   1.2  bouyer 		}
   1571   1.2  bouyer 
   1572   1.2  bouyer 		if (wdccommand(wdc, (struct wd_link*)(&sc_link->scsipi_atapi),
   1573   1.2  bouyer 		    ATAPI_PACKET_COMMAND, sc_link->scsipi_atapi.drive, sc_xfer->datalen,
   1574   1.2  bouyer 		    0, 0, 0) != 0) {
   1575  1.10   mikel 			printf("can't send atapi packet command\n");
   1576   1.2  bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1577   1.2  bouyer 			splx(s);
   1578   1.2  bouyer 			return COMPLETE;
   1579   1.2  bouyer 		}
   1580   1.2  bouyer 
   1581   1.2  bouyer 		/* Wait for cmd i/o phase. */
   1582   1.2  bouyer 		for (i = 20000; i > 0; --i) {
   1583   1.2  bouyer 			int phase;
   1584   1.2  bouyer 			phase = (inb(wdc->sc_iobase + wd_ireason) &
   1585   1.2  bouyer 			    (WDCI_CMD | WDCI_IN)) |
   1586   1.2  bouyer 			    (inb(wdc->sc_iobase + wd_status) & WDCS_DRQ);
   1587   1.2  bouyer 			if (phase == PHASE_CMDOUT)
   1588   1.2  bouyer 				break;
   1589   1.2  bouyer 			delay(10);
   1590   1.2  bouyer 		}
   1591   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1592   1.2  bouyer 		printf("Wait for cmd i/o phase: i = %d\n", i);
   1593   1.2  bouyer #endif
   1594   1.2  bouyer 
   1595   1.2  bouyer 		outsw(wdc->sc_iobase + wd_data, sc_xfer->cmd,
   1596   1.2  bouyer 		    sc_xfer->cmdlen/ sizeof (short));
   1597   1.2  bouyer 
   1598   1.2  bouyer 		/* Wait for data i/o phase. */
   1599   1.2  bouyer 		for ( i= 20000; i > 0; --i) {
   1600   1.2  bouyer 			int phase;
   1601   1.2  bouyer 			phase = (inb(wdc->sc_iobase + wd_ireason) &
   1602   1.2  bouyer 			    (WDCI_CMD | WDCI_IN)) |
   1603   1.2  bouyer 			    (inb(wdc->sc_iobase + wd_status) & WDCS_DRQ);
   1604   1.2  bouyer 			if (phase != PHASE_CMDOUT)
   1605   1.2  bouyer 				break;
   1606   1.2  bouyer 			delay(10);
   1607   1.2  bouyer 		}
   1608   1.2  bouyer 
   1609   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1610   1.2  bouyer 		printf("Wait for data i/o phase: i = %d\n", i);
   1611   1.2  bouyer #endif
   1612   1.8  bouyer 		wdc->sc_flags |= WDCF_IRQ_WAIT;
   1613   1.5  bouyer 		while ((sc_xfer->flags & ITSDONE) == 0) {
   1614   1.5  bouyer 			wdc_atapi_intr(wdc, xfer);
   1615   1.2  bouyer 			for (i = 2000; i > 0; --i)
   1616   1.2  bouyer 				if ((inb(wdc->sc_iobase + wd_status)
   1617   1.2  bouyer 				    & WDCS_DRQ) == 0)
   1618   1.2  bouyer 					break;
   1619   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1620   1.2  bouyer 			printf("wdc_atapi_intr: i = %d\n", i);
   1621   1.2  bouyer #endif
   1622   1.2  bouyer 		}
   1623   1.2  bouyer 		wdc->sc_flags &= ~(WDCF_IRQ_WAIT | WDCF_SINGLE | WDCF_ERROR);
   1624   1.2  bouyer 		wdc->sc_errors = 0;
   1625   1.2  bouyer 		xfer->c_skip = 0;
   1626   1.2  bouyer 		splx(s);
   1627   1.2  bouyer 		return COMPLETE;
   1628   1.2  bouyer 	} else {	/* POLLED */
   1629   1.2  bouyer 		xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? IDE_NOSLEEP : 0);
   1630   1.2  bouyer 		if (xfer == NULL) {
   1631   1.2  bouyer 			return TRY_AGAIN_LATER;
   1632   1.2  bouyer 		}
   1633   1.2  bouyer 		xfer->c_flags |= C_ATAPI|sc_xfer->flags;
   1634   1.2  bouyer 		xfer->d_link = (struct wd_link*)(&sc_link->scsipi_atapi);
   1635   1.2  bouyer 		xfer->c_bp = sc_xfer->bp;
   1636   1.2  bouyer 		xfer->atapi_cmd = sc_xfer;
   1637   1.2  bouyer 		xfer->c_blkno = 0;
   1638   1.2  bouyer 		xfer->databuf = sc_xfer->data;
   1639   1.2  bouyer 		xfer->c_bcount = sc_xfer->datalen;
   1640   1.2  bouyer 		wdc_exec_xfer(wdc, xfer->d_link, xfer);
   1641   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1642   1.2  bouyer 		printf("wdc_atapi_send_command_packet: wdc_exec_xfer, flags 0x%x\n",
   1643   1.2  bouyer 			sc_xfer->flags);
   1644   1.2  bouyer #endif
   1645   1.2  bouyer 		return (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
   1646   1.2  bouyer 	}
   1647   1.2  bouyer }
   1648   1.2  bouyer 
   1649   1.2  bouyer int
   1650   1.2  bouyer wdc_atapi_intr(wdc, xfer)
   1651   1.2  bouyer 	struct wdc_softc *wdc;
   1652   1.2  bouyer 	struct wdc_xfer *xfer;
   1653   1.2  bouyer {
   1654   1.2  bouyer 	struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
   1655   1.2  bouyer 	int len, phase, i, retries=0;
   1656   1.2  bouyer 	int err, st, ire;
   1657   1.2  bouyer 
   1658   1.2  bouyer #ifdef ATAPI_DEBUG2
   1659   1.2  bouyer 	printf("wdc_atapi_intr: %s\n", wdc->sc_dev.dv_xname);
   1660   1.2  bouyer #endif
   1661   1.2  bouyer 
   1662   1.2  bouyer 	if (wait_for_unbusy(wdc) < 0) {
   1663   1.2  bouyer 		if ((wdc->sc_status & WDCS_ERR) == 0) {
   1664   1.2  bouyer 			printf("wdc_atapi_intr: controller busy\n");
   1665   1.5  bouyer 			return 0;
   1666   1.2  bouyer 		} else {
   1667   1.2  bouyer 			sc_xfer->error = XS_SENSE;
   1668   1.2  bouyer 			sc_xfer->sense.atapi_sense = wdc->sc_error;
   1669   1.2  bouyer 		}
   1670   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1671   1.2  bouyer 		printf("wdc_atapi_intr: wdc_atapi_done(), error %d\n",
   1672   1.2  bouyer 			sc_xfer->error);
   1673   1.2  bouyer #endif
   1674   1.2  bouyer 		wdc_atapi_done(wdc, xfer);
   1675   1.2  bouyer 		return 0;
   1676   1.2  bouyer 	}
   1677   1.2  bouyer 
   1678   1.2  bouyer 
   1679   1.2  bouyer again:
   1680   1.2  bouyer 	len = inb(wdc->sc_iobase + wd_cyl_lo) +
   1681   1.2  bouyer 	    256 * inb(wdc->sc_iobase + wd_cyl_hi);
   1682   1.2  bouyer 
   1683   1.2  bouyer 	st = inb(wdc->sc_iobase + wd_status);
   1684   1.2  bouyer 	err = inb(wdc->sc_iobase + wd_error);
   1685   1.2  bouyer 	ire = inb(wdc->sc_iobase + wd_ireason);
   1686   1.2  bouyer 
   1687   1.2  bouyer 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (st & WDCS_DRQ);
   1688   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1689   1.2  bouyer 	printf("wdc_atapi_intr: len %d st %d err %d ire %d :",
   1690   1.2  bouyer 	    len, st, err, ire);
   1691   1.2  bouyer #endif
   1692   1.2  bouyer 	switch (phase) {
   1693   1.2  bouyer 	case PHASE_CMDOUT:
   1694   1.2  bouyer 		/* send packet command */
   1695   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1696   1.2  bouyer 		printf("PHASE_CMDOUT\n");
   1697   1.2  bouyer #endif
   1698   1.2  bouyer 
   1699   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1700   1.2  bouyer 		{
   1701   1.2  bouyer 			int i;
   1702   1.2  bouyer 			char *c = (char *)sc_xfer->cmd;
   1703   1.2  bouyer 			printf("wdc_atapi_intr: cmd ");
   1704   1.2  bouyer 			for (i = 0; i < sc_xfer->cmdlen; i++)
   1705   1.2  bouyer 				printf("%x ", c[i]);
   1706   1.2  bouyer 			printf("\n");
   1707   1.2  bouyer 		}
   1708   1.2  bouyer #endif
   1709   1.2  bouyer 
   1710   1.2  bouyer 		outsw(wdc->sc_iobase + wd_data, sc_xfer->cmd,
   1711   1.2  bouyer 		    sc_xfer->cmdlen/ sizeof (short));
   1712   1.2  bouyer 		return 1;
   1713   1.2  bouyer 
   1714   1.2  bouyer 	case PHASE_DATAOUT:
   1715   1.2  bouyer 		/* write data */
   1716   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1717   1.2  bouyer 		printf("PHASE_DATAOUT\n");
   1718   1.2  bouyer #endif
   1719   1.2  bouyer 		if ((sc_xfer->flags & SCSI_DATA_OUT) == 0) {
   1720   1.2  bouyer 			printf("wdc_atapi_intr: bad data phase\n");
   1721   1.2  bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1722   1.5  bouyer 			return 0;
   1723   1.2  bouyer 		}
   1724   1.2  bouyer 		if (xfer->c_bcount < len) {
   1725   1.2  bouyer 			printf("wdc_atapi_intr: warning: write only "
   1726   1.2  bouyer 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
   1727   1.2  bouyer 			outsw(wdc->sc_iobase + wd_data,
   1728   1.2  bouyer 			    xfer->databuf + xfer->c_skip,
   1729   1.2  bouyer 			    xfer->c_bcount / sizeof(short));
   1730   1.2  bouyer 			for (i = xfer->c_bcount; i < len; i += sizeof(short))
   1731   1.2  bouyer 				outw(wdc->sc_iobase + wd_data, 0);
   1732   1.8  bouyer 			xfer->c_skip += xfer->c_bcount;
   1733   1.2  bouyer 			xfer->c_bcount = 0;
   1734   1.2  bouyer 		} else {
   1735   1.2  bouyer 			outsw(wdc->sc_iobase + wd_data,
   1736   1.2  bouyer 			    xfer->databuf + xfer->c_skip, len / sizeof(short));
   1737   1.2  bouyer 			xfer->c_skip += len;
   1738   1.2  bouyer 			xfer->c_bcount -= len;
   1739   1.2  bouyer 		}
   1740   1.8  bouyer 		return 1;
   1741   1.2  bouyer 
   1742   1.2  bouyer 	case PHASE_DATAIN:
   1743   1.2  bouyer 		/* Read data */
   1744   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1745   1.2  bouyer 		printf("PHASE_DATAIN\n");
   1746   1.2  bouyer #endif
   1747   1.2  bouyer 		if ((sc_xfer->flags & SCSI_DATA_IN) == 0) {
   1748   1.2  bouyer 			printf("wdc_atapi_intr: bad data phase\n");
   1749   1.2  bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1750   1.5  bouyer 			return 0;
   1751   1.2  bouyer 		}
   1752   1.2  bouyer 		if (xfer->c_bcount < len) {
   1753   1.2  bouyer 			printf("wdc_atapi_intr: warning: reading only "
   1754   1.2  bouyer 			    "%d of %d bytes\n", xfer->c_bcount, len);
   1755   1.2  bouyer 			insw(wdc->sc_iobase + wd_data,
   1756   1.2  bouyer 			    xfer->databuf + xfer->c_skip,
   1757   1.2  bouyer 			    xfer->c_bcount / sizeof(short));
   1758   1.2  bouyer 			wdcbit_bucket(wdc, len - xfer->c_bcount);
   1759   1.8  bouyer 			xfer->c_skip += xfer->c_bcount;
   1760   1.2  bouyer 			xfer->c_bcount = 0;
   1761   1.2  bouyer 		} else {
   1762   1.2  bouyer 			insw(wdc->sc_iobase + wd_data,
   1763   1.2  bouyer 			    xfer->databuf + xfer->c_skip, len / sizeof(short));
   1764   1.2  bouyer 			xfer->c_skip += len;
   1765   1.2  bouyer 			xfer->c_bcount -=len;
   1766   1.2  bouyer 		}
   1767   1.8  bouyer 		return 1;
   1768   1.2  bouyer 
   1769   1.2  bouyer 	case PHASE_ABORTED:
   1770   1.2  bouyer 	case PHASE_COMPLETED:
   1771   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1772   1.2  bouyer 		printf("PHASE_COMPLETED\n");
   1773   1.2  bouyer #endif
   1774   1.2  bouyer 		if (st & WDCS_ERR) {
   1775   1.2  bouyer 			sc_xfer->error = XS_SENSE;
   1776   1.2  bouyer 			sc_xfer->sense.atapi_sense = inb (wdc->sc_iobase + wd_error);
   1777   1.2  bouyer 		}
   1778   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1779   1.2  bouyer 		if (xfer->c_bcount != 0) {
   1780   1.2  bouyer 			printf("wdc_atapi_intr warning: bcount value "
   1781   1.2  bouyer 			    "is %d after io\n", xfer->c_bcount);
   1782   1.2  bouyer 		}
   1783   1.2  bouyer #endif
   1784   1.2  bouyer 		break;
   1785   1.2  bouyer 
   1786   1.2  bouyer 	default:
   1787   1.2  bouyer         if (++retries<500) {
   1788   1.2  bouyer             DELAY(100);
   1789   1.2  bouyer             goto again;
   1790   1.2  bouyer         }
   1791   1.2  bouyer 		printf("wdc_atapi_intr: unknown phase %d\n", phase);
   1792   1.2  bouyer 		if (st & WDCS_ERR) {
   1793   1.2  bouyer 			sc_xfer->error = XS_SENSE;
   1794   1.2  bouyer 			sc_xfer->sense.atapi_sense = inb (wdc->sc_iobase + wd_error);
   1795   1.2  bouyer 		} else {
   1796   1.2  bouyer 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1797   1.2  bouyer 		}
   1798   1.2  bouyer 	}
   1799   1.2  bouyer 
   1800   1.2  bouyer #ifdef ATAPI_DEBUG_WDC
   1801   1.2  bouyer 		printf("wdc_atapi_intr: wdc_atapi_done() (end), error %d\n",
   1802   1.2  bouyer 			sc_xfer->error);
   1803   1.2  bouyer #endif
   1804   1.2  bouyer 	wdc_atapi_done(wdc, xfer);
   1805   1.5  bouyer 	return (1);
   1806   1.2  bouyer }
   1807   1.2  bouyer 
   1808   1.2  bouyer 
   1809   1.2  bouyer void
   1810   1.2  bouyer wdc_atapi_done(wdc, xfer)
   1811   1.2  bouyer 	struct wdc_softc *wdc;
   1812   1.2  bouyer 	struct wdc_xfer *xfer;
   1813   1.2  bouyer {
   1814   1.2  bouyer 	struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
   1815   1.2  bouyer 	int s;
   1816   1.2  bouyer 	int need_done =  xfer->c_flags & C_NEEDDONE;
   1817   1.2  bouyer 
   1818   1.2  bouyer #ifdef ATAPI_DEBUG
   1819   1.2  bouyer 	printf("wdc_atapi_done: flags 0x%x\n", (u_int)xfer->c_flags);
   1820   1.2  bouyer #endif
   1821   1.2  bouyer 	sc_xfer->resid = xfer->c_bcount;
   1822   1.8  bouyer 	wdc->sc_flags &= ~WDCF_IRQ_WAIT;
   1823   1.2  bouyer 
   1824   1.2  bouyer 	/* remove this command from xfer queue */
   1825   1.2  bouyer 	wdc->sc_errors = 0;
   1826   1.2  bouyer 	xfer->c_skip = 0;
   1827   1.2  bouyer 	if ((xfer->c_flags & SCSI_POLL) == 0) {
   1828   1.2  bouyer 		s = splbio();
   1829   1.2  bouyer 		untimeout(wdctimeout, wdc);
   1830   1.2  bouyer 		TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
   1831   1.2  bouyer 		wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
   1832   1.2  bouyer 		wdc_free_xfer(xfer);
   1833   1.2  bouyer 		sc_xfer->flags |= ITSDONE;
   1834   1.2  bouyer 		if (need_done) {
   1835   1.2  bouyer #ifdef ATAPI_DEBUG
   1836   1.2  bouyer 		printf("wdc_atapi_done: scsipi_done\n");
   1837   1.2  bouyer #endif
   1838   1.2  bouyer 			scsipi_done(sc_xfer);
   1839   1.2  bouyer 		}
   1840   1.2  bouyer #ifdef WDDEBUG
   1841   1.2  bouyer 		printf("wdcstart from wdc_atapi_intr, flags 0x%x\n",
   1842   1.2  bouyer 		    wdc->sc_flags);
   1843   1.2  bouyer #endif
   1844   1.2  bouyer 		wdcstart(wdc);
   1845   1.2  bouyer 	    splx(s);
   1846   1.8  bouyer 	} else {
   1847   1.2  bouyer 		wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
   1848   1.8  bouyer 		sc_xfer->flags |= ITSDONE;
   1849   1.8  bouyer 	}
   1850   1.2  bouyer }
   1851   1.2  bouyer 
   1852   1.2  bouyer #endif /* NATAPIBUS > 0 */
   1853