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