xfer->drive, chp->ch_status, chp->ch_error);
xfer->drive, chp->ch_status, WDCS_BITS, chp->ch_error);
_wdc_ata_bio_start(struct channel_softc *chp, struct wdc_xfer *xfer){	struct ata_bio *ata_bio = xfer->cmd;	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->drive];	u_int16_t cyl;	u_int8_t head, sect, cmd = 0;	int nblks;	int ata_delay;	int dma_flags = 0;	WDCDEBUG_PRINT(("_wdc_ata_bio_start %s:%d:%d\n",	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive),	    DEBUG_INTR | DEBUG_XFERS);	/* Do control operations specially. */	if (drvp->state < READY) {		/*		 * Actually, we want to be careful not to mess with the control		 * state if the device is currently busy, but we can assume		 * that we never get to this point if that's the case.		 */		/* at this point, we should only be in RECAL state */		if (drvp->state != RECAL) {			printf("%s:%d:%d: bad state %d in _wdc_ata_bio_start\n",			    chp->wdc->sc_dev.dv_xname, chp->channel,			    xfer->drive, drvp->state);			panic("_wdc_ata_bio_start: bad state");		}		xfer->c_intr = wdc_ata_ctrl_intr;		wdc_set_drive(chp, xfer->drive);		if (wdcwait(chp, WDCS_DRDY, WDCS_DRDY, ATA_DELAY) != 0)			goto timeout;		wdccommandshort(chp, xfer->drive, WDCC_RECAL);		drvp->state = RECAL_WAIT;		if ((ata_bio->flags & ATA_POLL) == 0) {			chp->ch_flags |= WDCF_IRQ_WAIT;		} else {			/* Wait for at last 400ns for status bit to be valid */			DELAY(1);			wdc_ata_ctrl_intr(chp, xfer, 0);		}		return;	}	if (xfer->c_flags & C_DMA) {		if (drvp->n_xfers <= NXFER)			drvp->n_xfers++;		dma_flags = (ata_bio->flags & ATA_READ) ?  WDC_DMA_READ : 0;		if (ata_bio->flags & ATA_LBA48)			dma_flags |= WDC_DMA_LBA48;	}	if (ata_bio->flags & ATA_SINGLE)		ata_delay = ATA_DELAY;	else		ata_delay = ATA_DELAY;again:	/*	 *	 * When starting a multi-sector transfer, or doing single-sector	 * transfers...	 */	if (xfer->c_skip == 0 || (ata_bio->flags & ATA_SINGLE) != 0) {		if (ata_bio->flags & ATA_SINGLE)			nblks = 1;		else			nblks = xfer->c_bcount / ata_bio->lp->d_secsize;		/* Check for bad sectors and adjust transfer, if necessary. */		if ((ata_bio->lp->d_flags & D_BADSECT) != 0) {			long blkdiff;			int i;			for (i = 0; (blkdiff = ata_bio->badsect[i]) != -1;			     i++) {				blkdiff -= ata_bio->blkno;				if (blkdiff < 0)					continue;				if (blkdiff == 0) {					/* Replace current block of transfer. */					ata_bio->blkno =					    ata_bio->lp->d_secperunit -					    ata_bio->lp->d_nsectors - i - 1;				}				if (blkdiff < nblks) {					/* Bad block inside transfer. */					ata_bio->flags |= ATA_SINGLE;					nblks = 1;				}				break;			}			/* Transfer is okay now. */		}		if (ata_bio->flags & ATA_LBA) {			sect = (ata_bio->blkno >> 0) & 0xff;			cyl = (ata_bio->blkno >> 8) & 0xffff;			head = (ata_bio->blkno >> 24) & 0x0f;			head |= WDSD_LBA;		} else {			int blkno = ata_bio->blkno;			sect = blkno % ata_bio->lp->d_nsectors;			sect++;    /* Sectors begin with 1, not 0. */			blkno /= ata_bio->lp->d_nsectors;			head = blkno % ata_bio->lp->d_ntracks;			blkno /= ata_bio->lp->d_ntracks;			cyl = blkno;			head |= WDSD_CHS;		}		if (xfer->c_flags & C_DMA) {			ata_bio->nblks = nblks;			ata_bio->nbytes = xfer->c_bcount;			if (ata_bio->flags & ATA_LBA48)				cmd = (ata_bio->flags & ATA_READ) ?				    WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;			else				cmd = (ata_bio->flags & ATA_READ) ?				    WDCC_READDMA : WDCC_WRITEDMA;	    		/* Init the DMA channel. */			if ((*chp->wdc->dma_init)(chp->wdc->dma_arg,			    chp->channel, xfer->drive,			    (char *)xfer->databuf + xfer->c_skip,			    ata_bio->nbytes, dma_flags) != 0) {				ata_bio->error = ERR_DMA;				ata_bio->r_error = 0;				wdc_ata_bio_done(chp, xfer);				return;			}			/* Initiate command */			wdc_set_drive(chp, xfer->drive);			if (wait_for_ready(chp, ata_delay) < 0)				goto timeout;			if (ata_bio->flags & ATA_LBA48) {				wdccommandext(chp, xfer->drive, cmd,				    (u_int64_t)ata_bio->blkno, nblks);			} else {				wdccommand(chp, xfer->drive, cmd, cyl,				    head, sect, nblks, 0);			}			/* start the DMA channel */			(*chp->wdc->dma_start)(chp->wdc->dma_arg,			    chp->channel, xfer->drive);			chp->ch_flags |= WDCF_DMA_WAIT;			/* wait for irq */			goto intr;		} /* else not DMA */		ata_bio->nblks = min(nblks, ata_bio->multi);		ata_bio->nbytes = ata_bio->nblks * ata_bio->lp->d_secsize;		if (ata_bio->nblks > 1 && (ata_bio->flags & ATA_SINGLE) == 0) {			if (ata_bio->flags & ATA_LBA48)				cmd = (ata_bio->flags & ATA_READ) ?				    WDCC_READMULTI_EXT : WDCC_WRITEMULTI_EXT;			else				cmd = (ata_bio->flags & ATA_READ) ?				    WDCC_READMULTI : WDCC_WRITEMULTI;		} else {			if (ata_bio->flags & ATA_LBA48)				cmd = (ata_bio->flags & ATA_READ) ?				    WDCC_READ_EXT : WDCC_WRITE_EXT;			else				cmd = (ata_bio->flags & ATA_READ) ?				    WDCC_READ : WDCC_WRITE;		}		/* Initiate command! */		wdc_set_drive(chp, xfer->drive);		if (wait_for_ready(chp, ata_delay) < 0)			goto timeout;		if (ata_bio->flags & ATA_LBA48) {			wdccommandext(chp, xfer->drive, cmd,			    (u_int64_t)ata_bio->blkno, nblks);		} else {			wdccommand(chp, xfer->drive, cmd, cyl,			    head, sect, nblks,			    (ata_bio->lp->d_type == DTYPE_ST506) ?			    ata_bio->lp->d_precompcyl / 4 : 0);		}	} else if (ata_bio->nblks > 1) {		/* The number of blocks in the last stretch may be smaller. */		nblks = xfer->c_bcount / ata_bio->lp->d_secsize;		if (ata_bio->nblks > nblks) {			ata_bio->nblks = nblks;			ata_bio->nbytes = xfer->c_bcount;		}	}	/* If this was a write and not using DMA, push the data. */	if ((ata_bio->flags & ATA_READ) == 0) {		if (wait_for_drq(chp, ata_delay) != 0) {			printf("%s:%d:%d: timeout waiting for DRQ, "			    "st=0x%02x, err=0x%02x\n",			    chp->wdc->sc_dev.dv_xname, chp->channel,			    xfer->drive, chp->ch_status, chp->ch_error);			if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)				ata_bio->error = TIMEOUT;			wdc_ata_bio_done(chp, xfer);			return;		}		if (wdc_ata_err(drvp, ata_bio) == WDC_ATA_ERR) {			wdc_ata_bio_done(chp, xfer);			return;		}		wdc_output_bytes(drvp, (char *)xfer->databuf + xfer->c_skip,		    ata_bio->nbytes);	}intr:	/* Wait for IRQ (either real or polled) */	if ((ata_bio->flags & ATA_POLL) == 0) {		chp->ch_flags |= WDCF_IRQ_WAIT;	} else {		/* Wait for at last 400ns for status bit to be valid */		delay(1);		if (chp->ch_flags & WDCF_DMA_WAIT) {			wdc_dmawait(chp, xfer, ATA_DELAY);			chp->ch_flags &= ~WDCF_DMA_WAIT;		}		wdc_ata_bio_intr(chp, xfer, 0);		if ((ata_bio->flags & ATA_ITSDONE) == 0)			goto again;	}	return;timeout:	printf("%s:%d:%d: not ready, st=0x%02x, err=0x%02x\n",	    chp->wdc->sc_dev.dv_xname, chp->channel, xfer->drive,	    chp->ch_status, chp->ch_error);	if (wdc_ata_err(drvp, ata_bio) != WDC_ATA_ERR)		ata_bio->error = TIMEOUT;	wdc_ata_bio_done(chp, xfer);	return;}