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