Home | History | Annotate | Line # | Download | only in pci
pciide_common.c revision 1.32
      1 /*	$NetBSD: pciide_common.c,v 1.32 2006/10/12 01:31:33 christos Exp $	*/
      2 
      3 
      4 /*
      5  * Copyright (c) 1999, 2000, 2001, 2003 Manuel Bouyer.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Manuel Bouyer.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 
     36 /*
     37  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *      This product includes software developed by Christopher G. Demetriou
     50  *	for the NetBSD Project.
     51  * 4. The name of the author may not be used to endorse or promote products
     52  *    derived from this software without specific prior written permission
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * PCI IDE controller driver.
     68  *
     69  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     70  * sys/dev/pci/ppb.c, revision 1.16).
     71  *
     72  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     73  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     74  * 5/16/94" from the PCI SIG.
     75  *
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: pciide_common.c,v 1.32 2006/10/12 01:31:33 christos Exp $");
     80 
     81 #include <sys/param.h>
     82 #include <sys/malloc.h>
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 #include <dev/pci/pcireg.h>
     87 #include <dev/pci/pcivar.h>
     88 #include <dev/pci/pcidevs.h>
     89 #include <dev/pci/pciidereg.h>
     90 #include <dev/pci/pciidevar.h>
     91 
     92 #include <dev/ic/wdcreg.h>
     93 
     94 #ifdef ATADEBUG
     95 int atadebug_pciide_mask = 0;
     96 #endif
     97 
     98 static const char dmaerrfmt[] =
     99     "%s:%d: unable to %s table DMA map for drive %d, error=%d\n";
    100 
    101 /* Default product description for devices not known from this controller */
    102 const struct pciide_product_desc default_product_desc = {
    103 	0,
    104 	0,
    105 	"Generic PCI IDE controller",
    106 	default_chip_map,
    107 };
    108 
    109 const struct pciide_product_desc *
    110 pciide_lookup_product(id, pp)
    111 	pcireg_t id;
    112 	const struct pciide_product_desc *pp;
    113 {
    114 	for (; pp->chip_map != NULL; pp++)
    115 		if (PCI_PRODUCT(id) == pp->ide_product)
    116 			break;
    117 
    118 	if (pp->chip_map == NULL)
    119 		return NULL;
    120 	return pp;
    121 }
    122 
    123 void
    124 pciide_common_attach(sc, pa, pp)
    125 	struct pciide_softc *sc;
    126 	struct pci_attach_args *pa;
    127 	const struct pciide_product_desc *pp;
    128 {
    129 	pci_chipset_tag_t pc = pa->pa_pc;
    130 	pcitag_t tag = pa->pa_tag;
    131 	pcireg_t csr;
    132 	char devinfo[256];
    133 	const char *displaydev;
    134 
    135 	aprint_naive(": disk controller\n");
    136 	aprint_normal("\n");
    137 
    138 	sc->sc_pci_id = pa->pa_id;
    139 	if (pp == NULL) {
    140 		/* should only happen for generic pciide devices */
    141 		sc->sc_pp = &default_product_desc;
    142 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    143 		displaydev = devinfo;
    144 	} else {
    145 		sc->sc_pp = pp;
    146 		displaydev = sc->sc_pp->ide_name;
    147 	}
    148 
    149 	/* if displaydev == NULL, printf is done in chip-specific map */
    150 	if (displaydev)
    151 		aprint_normal("%s: %s (rev. 0x%02x)\n",
    152 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, displaydev,
    153 		    PCI_REVISION(pa->pa_class));
    154 
    155 	sc->sc_pc = pa->pa_pc;
    156 	sc->sc_tag = pa->pa_tag;
    157 
    158 	/* Set up DMA defaults; these might be adjusted by chip_map. */
    159 	sc->sc_dma_maxsegsz = IDEDMA_BYTE_COUNT_MAX;
    160 	sc->sc_dma_boundary = IDEDMA_BYTE_COUNT_ALIGN;
    161 
    162 #ifdef ATADEBUG
    163 	if (atadebug_pciide_mask & DEBUG_PROBE)
    164 		pci_conf_print(sc->sc_pc, sc->sc_tag, NULL);
    165 #endif
    166 	sc->sc_pp->chip_map(sc, pa);
    167 
    168 	if (sc->sc_dma_ok) {
    169 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    170 		csr |= PCI_COMMAND_MASTER_ENABLE;
    171 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    172 	}
    173 	ATADEBUG_PRINT(("pciide: command/status register=%x\n",
    174 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    175 }
    176 
    177 /* tell whether the chip is enabled or not */
    178 int
    179 pciide_chipen(sc, pa)
    180 	struct pciide_softc *sc;
    181 	struct pci_attach_args *pa;
    182 {
    183 	pcireg_t csr;
    184 
    185 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    186 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    187 		    PCI_COMMAND_STATUS_REG);
    188 		aprint_normal("%s: device disabled (at %s)\n",
    189 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    190 		   (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    191 		   "device" : "bridge");
    192 		return 0;
    193 	}
    194 	return 1;
    195 }
    196 
    197 void
    198 pciide_mapregs_compat(pa, cp, compatchan, cmdsizep, ctlsizep)
    199 	struct pci_attach_args *pa;
    200 	struct pciide_channel *cp;
    201 	int compatchan;
    202 	bus_size_t *cmdsizep, *ctlsizep;
    203 {
    204 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    205 	struct ata_channel *wdc_cp = &cp->ata_channel;
    206 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    207 	int i;
    208 
    209 	cp->compat = 1;
    210 	*cmdsizep = PCIIDE_COMPAT_CMD_SIZE;
    211 	*ctlsizep = PCIIDE_COMPAT_CTL_SIZE;
    212 
    213 	wdr->cmd_iot = pa->pa_iot;
    214 	if (bus_space_map(wdr->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    215 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdr->cmd_baseioh) != 0) {
    216 		aprint_error("%s: couldn't map %s channel cmd regs\n",
    217 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    218 		goto bad;
    219 	}
    220 
    221 	wdr->ctl_iot = pa->pa_iot;
    222 	if (bus_space_map(wdr->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    223 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdr->ctl_ioh) != 0) {
    224 		aprint_error("%s: couldn't map %s channel ctl regs\n",
    225 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    226 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
    227 		    PCIIDE_COMPAT_CMD_SIZE);
    228 		goto bad;
    229 	}
    230 
    231 	for (i = 0; i < WDC_NREG; i++) {
    232 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
    233 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    234 			aprint_error("%s: couldn't subregion %s channel "
    235 				     "cmd regs\n",
    236 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    237 			goto bad;
    238 		}
    239 	}
    240 	wdc_init_shadow_regs(wdc_cp);
    241 	wdr->data32iot = wdr->cmd_iot;
    242 	wdr->data32ioh = wdr->cmd_iohs[0];
    243 	return;
    244 
    245 bad:
    246 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    247 	return;
    248 }
    249 
    250 void
    251 pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr)
    252 	struct pci_attach_args * pa;
    253 	struct pciide_channel *cp;
    254 	bus_size_t *cmdsizep, *ctlsizep;
    255 	int (*pci_intr)(void *);
    256 {
    257 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    258 	struct ata_channel *wdc_cp = &cp->ata_channel;
    259 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    260 	const char *intrstr;
    261 	pci_intr_handle_t intrhandle;
    262 	int i;
    263 
    264 	cp->compat = 0;
    265 
    266 	if (sc->sc_pci_ih == NULL) {
    267 		if (pci_intr_map(pa, &intrhandle) != 0) {
    268 			aprint_error("%s: couldn't map native-PCI interrupt\n",
    269 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    270 			goto bad;
    271 		}
    272 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    273 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    274 		    intrhandle, IPL_BIO, pci_intr, sc);
    275 		if (sc->sc_pci_ih != NULL) {
    276 			aprint_normal("%s: using %s for native-PCI interrupt\n",
    277 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    278 			    intrstr ? intrstr : "unknown interrupt");
    279 		} else {
    280 			aprint_error(
    281 			    "%s: couldn't establish native-PCI interrupt",
    282 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    283 			if (intrstr != NULL)
    284 				aprint_normal(" at %s", intrstr);
    285 			aprint_normal("\n");
    286 			goto bad;
    287 		}
    288 	}
    289 	cp->ih = sc->sc_pci_ih;
    290 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->ch_channel),
    291 	    PCI_MAPREG_TYPE_IO, 0,
    292 	    &wdr->cmd_iot, &wdr->cmd_baseioh, NULL, cmdsizep) != 0) {
    293 		aprint_error("%s: couldn't map %s channel cmd regs\n",
    294 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    295 		goto bad;
    296 	}
    297 
    298 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->ch_channel),
    299 	    PCI_MAPREG_TYPE_IO, 0,
    300 	    &wdr->ctl_iot, &cp->ctl_baseioh, NULL, ctlsizep) != 0) {
    301 		aprint_error("%s: couldn't map %s channel ctl regs\n",
    302 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    303 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
    304 		    *cmdsizep);
    305 		goto bad;
    306 	}
    307 	/*
    308 	 * In native mode, 4 bytes of I/O space are mapped for the control
    309 	 * register, the control register is at offset 2. Pass the generic
    310 	 * code a handle for only one byte at the right offset.
    311 	 */
    312 	if (bus_space_subregion(wdr->ctl_iot, cp->ctl_baseioh, 2, 1,
    313 	    &wdr->ctl_ioh) != 0) {
    314 		aprint_error("%s: unable to subregion %s channel ctl regs\n",
    315 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    316 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
    317 		     *cmdsizep);
    318 		bus_space_unmap(wdr->cmd_iot, cp->ctl_baseioh, *ctlsizep);
    319 		goto bad;
    320 	}
    321 
    322 	for (i = 0; i < WDC_NREG; i++) {
    323 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
    324 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    325 			aprint_error("%s: couldn't subregion %s channel "
    326 				     "cmd regs\n",
    327 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    328 			goto bad;
    329 		}
    330 	}
    331 	wdc_init_shadow_regs(wdc_cp);
    332 	wdr->data32iot = wdr->cmd_iot;
    333 	wdr->data32ioh = wdr->cmd_iohs[0];
    334 	return;
    335 
    336 bad:
    337 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    338 	return;
    339 }
    340 
    341 void
    342 pciide_mapreg_dma(sc, pa)
    343 	struct pciide_softc *sc;
    344 	struct pci_attach_args *pa;
    345 {
    346 	pcireg_t maptype;
    347 	bus_addr_t addr;
    348 	struct pciide_channel *pc;
    349 	int reg, chan;
    350 	bus_size_t size;
    351 
    352 	/*
    353 	 * Map DMA registers
    354 	 *
    355 	 * Note that sc_dma_ok is the right variable to test to see if
    356 	 * DMA can be done.  If the interface doesn't support DMA,
    357 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    358 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    359 	 * non-zero if the interface supports DMA and the registers
    360 	 * could be mapped.
    361 	 *
    362 	 * XXX Note that despite the fact that the Bus Master IDE specs
    363 	 * XXX say that "The bus master IDE function uses 16 bytes of IO
    364 	 * XXX space," some controllers (at least the United
    365 	 * XXX Microelectronics UM8886BF) place it in memory space.
    366 	 */
    367 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,
    368 	    PCIIDE_REG_BUS_MASTER_DMA);
    369 
    370 	switch (maptype) {
    371 	case PCI_MAPREG_TYPE_IO:
    372 		sc->sc_dma_ok = (pci_mapreg_info(pa->pa_pc, pa->pa_tag,
    373 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO,
    374 		    &addr, NULL, NULL) == 0);
    375 		if (sc->sc_dma_ok == 0) {
    376 			aprint_normal(
    377 			    ", but unused (couldn't query registers)");
    378 			break;
    379 		}
    380 		if ((sc->sc_pp->ide_flags & IDE_16BIT_IOSPACE)
    381 		    && addr >= 0x10000) {
    382 			sc->sc_dma_ok = 0;
    383 			aprint_normal(
    384 			    ", but unused (registers at unsafe address "
    385 			    "%#lx)", (unsigned long)addr);
    386 			break;
    387 		}
    388 		/* FALLTHROUGH */
    389 
    390 	case PCI_MAPREG_MEM_TYPE_32BIT:
    391 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    392 		    PCIIDE_REG_BUS_MASTER_DMA, maptype, 0,
    393 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    394 		sc->sc_dmat = pa->pa_dmat;
    395 		if (sc->sc_dma_ok == 0) {
    396 			aprint_normal(", but unused (couldn't map registers)");
    397 		} else {
    398 			sc->sc_wdcdev.dma_arg = sc;
    399 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    400 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    401 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    402 		}
    403 
    404 		if (device_cfdata(&sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    405 		    PCIIDE_OPTIONS_NODMA) {
    406 			aprint_normal(
    407 			    ", but unused (forced off by config file)");
    408 			sc->sc_dma_ok = 0;
    409 		}
    410 		break;
    411 
    412 	default:
    413 		sc->sc_dma_ok = 0;
    414 		aprint_normal(
    415 		    ", but unsupported register maptype (0x%x)", maptype);
    416 	}
    417 
    418 	if (sc->sc_dma_ok == 0)
    419 		return;
    420 
    421 	/*
    422 	 * Set up the default handles for the DMA registers.
    423 	 * Just reserve 32 bits for each handle, unless space
    424 	 * doesn't permit it.
    425 	 */
    426 	for (chan = 0; chan < PCIIDE_NUM_CHANNELS; chan++) {
    427 		pc = &sc->pciide_channels[chan];
    428 		for (reg = 0; reg < IDEDMA_NREGS; reg++) {
    429 			size = 4;
    430 			if (size > (IDEDMA_SCH_OFFSET - reg))
    431 				size = IDEDMA_SCH_OFFSET - reg;
    432 			if (bus_space_subregion(sc->sc_dma_iot, sc->sc_dma_ioh,
    433 			    IDEDMA_SCH_OFFSET * chan + reg, size,
    434 			    &pc->dma_iohs[reg]) != 0) {
    435 				sc->sc_dma_ok = 0;
    436 				aprint_normal(", but can't subregion offset %d "
    437 					      "size %lu", reg, (u_long)size);
    438 				return;
    439 			}
    440 		}
    441 	}
    442 }
    443 
    444 int
    445 pciide_compat_intr(arg)
    446 	void *arg;
    447 {
    448 	struct pciide_channel *cp = arg;
    449 
    450 #ifdef DIAGNOSTIC
    451 	/* should only be called for a compat channel */
    452 	if (cp->compat == 0)
    453 		panic("pciide compat intr called for non-compat chan %p", cp);
    454 #endif
    455 	return (wdcintr(&cp->ata_channel));
    456 }
    457 
    458 int
    459 pciide_pci_intr(arg)
    460 	void *arg;
    461 {
    462 	struct pciide_softc *sc = arg;
    463 	struct pciide_channel *cp;
    464 	struct ata_channel *wdc_cp;
    465 	int i, rv, crv;
    466 
    467 	rv = 0;
    468 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    469 		cp = &sc->pciide_channels[i];
    470 		wdc_cp = &cp->ata_channel;
    471 
    472 		/* If a compat channel skip. */
    473 		if (cp->compat)
    474 			continue;
    475 		/* if this channel not waiting for intr, skip */
    476 		if ((wdc_cp->ch_flags & ATACH_IRQ_WAIT) == 0)
    477 			continue;
    478 
    479 		crv = wdcintr(wdc_cp);
    480 		if (crv == 0)
    481 			;		/* leave rv alone */
    482 		else if (crv == 1)
    483 			rv = 1;		/* claim the intr */
    484 		else if (rv == 0)	/* crv should be -1 in this case */
    485 			rv = crv;	/* if we've done no better, take it */
    486 	}
    487 	return (rv);
    488 }
    489 
    490 void
    491 pciide_channel_dma_setup(cp)
    492 	struct pciide_channel *cp;
    493 {
    494 	int drive, s;
    495 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    496 	struct ata_drive_datas *drvp;
    497 
    498 	KASSERT(cp->ata_channel.ch_ndrive != 0);
    499 
    500 	for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) {
    501 		drvp = &cp->ata_channel.ch_drive[drive];
    502 		/* If no drive, skip */
    503 		if ((drvp->drive_flags & DRIVE) == 0)
    504 			continue;
    505 		/* setup DMA if needed */
    506 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    507 		    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    508 		    sc->sc_dma_ok == 0) {
    509 			s = splbio();
    510 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    511 			splx(s);
    512 			continue;
    513 		}
    514 		if (pciide_dma_table_setup(sc, cp->ata_channel.ch_channel,
    515 					   drive) != 0) {
    516 			/* Abort DMA setup */
    517 			s = splbio();
    518 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    519 			splx(s);
    520 			continue;
    521 		}
    522 	}
    523 }
    524 
    525 #define NIDEDMA_TABLES(sc)	\
    526 	(MAXPHYS/(min((sc)->sc_dma_maxsegsz, PAGE_SIZE)) + 1)
    527 
    528 int
    529 pciide_dma_table_setup(sc, channel, drive)
    530 	struct pciide_softc *sc;
    531 	int channel, drive;
    532 {
    533 	bus_dma_segment_t seg;
    534 	int error, rseg;
    535 	const bus_size_t dma_table_size =
    536 	    sizeof(struct idedma_table) * NIDEDMA_TABLES(sc);
    537 	struct pciide_dma_maps *dma_maps =
    538 	    &sc->pciide_channels[channel].dma_maps[drive];
    539 
    540 	/* If table was already allocated, just return */
    541 	if (dma_maps->dma_table)
    542 		return 0;
    543 
    544 	/* Allocate memory for the DMA tables and map it */
    545 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    546 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
    547 	    BUS_DMA_NOWAIT)) != 0) {
    548 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    549 		    channel, "allocate", drive, error);
    550 		return error;
    551 	}
    552 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    553 	    dma_table_size,
    554 	    (caddr_t *)&dma_maps->dma_table,
    555 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    556 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    557 		    channel, "map", drive, error);
    558 		return error;
    559 	}
    560 	ATADEBUG_PRINT(("pciide_dma_table_setup: table at %p len %lu, "
    561 	    "phy 0x%lx\n", dma_maps->dma_table, (u_long)dma_table_size,
    562 	    (unsigned long)seg.ds_addr), DEBUG_PROBE);
    563 	/* Create and load table DMA map for this disk */
    564 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    565 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    566 	    &dma_maps->dmamap_table)) != 0) {
    567 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    568 		    channel, "create", drive, error);
    569 		return error;
    570 	}
    571 	if ((error = bus_dmamap_load(sc->sc_dmat,
    572 	    dma_maps->dmamap_table,
    573 	    dma_maps->dma_table,
    574 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    575 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    576 		    channel, "load", drive, error);
    577 		return error;
    578 	}
    579 	ATADEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    580 	    (unsigned long)dma_maps->dmamap_table->dm_segs[0].ds_addr),
    581 	    DEBUG_PROBE);
    582 	/* Create a xfer DMA map for this drive */
    583 	if ((error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
    584 	    NIDEDMA_TABLES(sc), sc->sc_dma_maxsegsz, sc->sc_dma_boundary,
    585 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    586 	    &dma_maps->dmamap_xfer)) != 0) {
    587 		aprint_error(dmaerrfmt, sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    588 		    channel, "create xfer", drive, error);
    589 		return error;
    590 	}
    591 	return 0;
    592 }
    593 
    594 int
    595 pciide_dma_dmamap_setup(sc, channel, drive, databuf, datalen, flags)
    596 	struct pciide_softc *sc;
    597 	int channel, drive;
    598 	void *databuf;
    599 	size_t datalen;
    600 	int flags;
    601 {
    602 	int error, seg;
    603 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    604 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    605 
    606 	error = bus_dmamap_load(sc->sc_dmat,
    607 	    dma_maps->dmamap_xfer,
    608 	    databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    609 	    ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
    610 	if (error) {
    611 		printf(dmaerrfmt, sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    612 		    channel, "load xfer", drive, error);
    613 		return error;
    614 	}
    615 
    616 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    617 	    dma_maps->dmamap_xfer->dm_mapsize,
    618 	    (flags & WDC_DMA_READ) ?
    619 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    620 
    621 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    622 #ifdef DIAGNOSTIC
    623 		/* A segment must not cross a 64k boundary */
    624 		{
    625 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    626 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    627 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    628 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    629 			printf("pciide_dma: segment %d physical addr 0x%lx"
    630 			    " len 0x%lx not properly aligned\n",
    631 			    seg, phys, len);
    632 			panic("pciide_dma: buf align");
    633 		}
    634 		}
    635 #endif
    636 		dma_maps->dma_table[seg].base_addr =
    637 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
    638 		dma_maps->dma_table[seg].byte_count =
    639 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
    640 		    IDEDMA_BYTE_COUNT_MASK);
    641 		ATADEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    642 		   seg, le32toh(dma_maps->dma_table[seg].byte_count),
    643 		   le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
    644 
    645 	}
    646 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    647 	    htole32(IDEDMA_BYTE_COUNT_EOT);
    648 
    649 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    650 	    dma_maps->dmamap_table->dm_mapsize,
    651 	    BUS_DMASYNC_PREWRITE);
    652 
    653 #ifdef DIAGNOSTIC
    654 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    655 		printf("pciide_dma_dmamap_setup: addr 0x%lx "
    656 		    "not properly aligned\n",
    657 		    (u_long)dma_maps->dmamap_table->dm_segs[0].ds_addr);
    658 		panic("pciide_dma_init: table align");
    659 	}
    660 #endif
    661 	/* remember flags */
    662 	dma_maps->dma_flags = flags;
    663 
    664 	return 0;
    665 }
    666 
    667 int
    668 pciide_dma_init(v, channel, drive, databuf, datalen, flags)
    669 	void *v;
    670 	int channel, drive;
    671 	void *databuf;
    672 	size_t datalen;
    673 	int flags;
    674 {
    675 	struct pciide_softc *sc = v;
    676 	int error;
    677 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    678 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    679 
    680 	if ((error = pciide_dma_dmamap_setup(sc, channel, drive,
    681 	    databuf, datalen, flags)) != 0)
    682 		return error;
    683 	/* Maps are ready. Start DMA function */
    684 	/* Clear status bits */
    685 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    686 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
    687 	/* Write table addr */
    688 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0,
    689 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    690 	/* set read/write */
    691 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    692 	    ((flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE : 0) | cp->idedma_cmd);
    693 	return 0;
    694 }
    695 
    696 void
    697 pciide_dma_start(void *v, int channel, int drive __unused)
    698 {
    699 	struct pciide_softc *sc = v;
    700 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    701 
    702 	ATADEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    703 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    704 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
    705 		| IDEDMA_CMD_START);
    706 }
    707 
    708 int
    709 pciide_dma_finish(v, channel, drive, force)
    710 	void *v;
    711 	int channel, drive;
    712 	int force;
    713 {
    714 	struct pciide_softc *sc = v;
    715 	u_int8_t status;
    716 	int error = 0;
    717 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    718 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    719 
    720 	status = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0);
    721 	ATADEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    722 	    DEBUG_XFERS);
    723 
    724 	if (force == WDC_DMAEND_END && (status & IDEDMA_CTL_INTR) == 0)
    725 		return WDC_DMAST_NOIRQ;
    726 
    727 	/* stop DMA channel */
    728 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    729 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
    730 		& ~IDEDMA_CMD_START);
    731 
    732 	/* Unload the map of the data buffer */
    733 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    734 	    dma_maps->dmamap_xfer->dm_mapsize,
    735 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    736 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    737 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    738 
    739 	if ((status & IDEDMA_CTL_ERR) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
    740 		printf("%s:%d:%d: bus-master DMA error: status=0x%x\n",
    741 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, channel, drive,
    742 		    status);
    743 		error |= WDC_DMAST_ERR;
    744 	}
    745 
    746 	if ((status & IDEDMA_CTL_INTR) == 0 && force != WDC_DMAEND_ABRT_QUIET) {
    747 		printf("%s:%d:%d: bus-master DMA error: missing interrupt, "
    748 		    "status=0x%x\n", sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    749 		    channel, drive, status);
    750 		error |= WDC_DMAST_NOIRQ;
    751 	}
    752 
    753 	if ((status & IDEDMA_CTL_ACT) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
    754 		/* data underrun, may be a valid condition for ATAPI */
    755 		error |= WDC_DMAST_UNDER;
    756 	}
    757 	return error;
    758 }
    759 
    760 void
    761 pciide_irqack(chp)
    762 	struct ata_channel *chp;
    763 {
    764 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    765 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    766 
    767 	/* clear status bits in IDE DMA registers */
    768 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    769 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
    770 }
    771 
    772 /* some common code used by several chip_map */
    773 int
    774 pciide_chansetup(sc, channel, interface)
    775 	struct pciide_softc *sc;
    776 	int channel;
    777 	pcireg_t interface;
    778 {
    779 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    780 	sc->wdc_chanarray[channel] = &cp->ata_channel;
    781 	cp->name = PCIIDE_CHANNEL_NAME(channel);
    782 	cp->ata_channel.ch_channel = channel;
    783 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    784 	cp->ata_channel.ch_queue =
    785 	    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    786 	if (cp->ata_channel.ch_queue == NULL) {
    787 		aprint_error("%s %s channel: "
    788 		    "can't allocate memory for command queue",
    789 		sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
    790 		return 0;
    791 	}
    792 	cp->ata_channel.ch_ndrive = 2;
    793 	aprint_normal("%s: %s channel %s to %s mode\n",
    794 	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name,
    795 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
    796 	    "configured" : "wired",
    797 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
    798 	    "native-PCI" : "compatibility");
    799 	return 1;
    800 }
    801 
    802 /* some common code used by several chip channel_map */
    803 void
    804 pciide_mapchan(pa, cp, interface, cmdsizep, ctlsizep, pci_intr)
    805 	struct pci_attach_args *pa;
    806 	struct pciide_channel *cp;
    807 	pcireg_t interface;
    808 	bus_size_t *cmdsizep, *ctlsizep;
    809 	int (*pci_intr)(void *);
    810 {
    811 	struct ata_channel *wdc_cp = &cp->ata_channel;
    812 
    813 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->ch_channel))
    814 		pciide_mapregs_native(pa, cp, cmdsizep, ctlsizep, pci_intr);
    815 	else {
    816 		pciide_mapregs_compat(pa, cp, wdc_cp->ch_channel, cmdsizep,
    817 		    ctlsizep);
    818 		if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
    819 			pciide_map_compat_intr(pa, cp, wdc_cp->ch_channel);
    820 	}
    821 	wdcattach(wdc_cp);
    822 }
    823 
    824 /*
    825  * generic code to map the compat intr.
    826  */
    827 void
    828 pciide_map_compat_intr(pa, cp, compatchan)
    829 	struct pci_attach_args *pa;
    830 	struct pciide_channel *cp;
    831 	int compatchan;
    832 {
    833 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    834 
    835 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    836 	cp->ih =
    837 	   pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_atac.atac_dev,
    838 	   pa, compatchan, pciide_compat_intr, cp);
    839 	if (cp->ih == NULL) {
    840 #endif
    841 		aprint_error("%s: no compatibility interrupt for use by %s "
    842 		    "channel\n", sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    843 		    cp->name);
    844 		cp->ata_channel.ch_flags |= ATACH_DISABLED;
    845 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    846 	}
    847 #endif
    848 }
    849 
    850 void
    851 default_chip_map(sc, pa)
    852 	struct pciide_softc *sc;
    853 	struct pci_attach_args *pa;
    854 {
    855 	struct pciide_channel *cp;
    856 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    857 	pcireg_t csr;
    858 	int channel, drive;
    859 	u_int8_t idedma_ctl;
    860 	bus_size_t cmdsize, ctlsize;
    861 	const char *failreason;
    862 	struct wdc_regs *wdr;
    863 
    864 	if (pciide_chipen(sc, pa) == 0)
    865 		return;
    866 
    867 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    868 		aprint_normal("%s: bus-master DMA support present",
    869 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    870 		if (sc->sc_pp == &default_product_desc &&
    871 		    (device_cfdata(&sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    872 		    PCIIDE_OPTIONS_DMA) == 0) {
    873 			aprint_normal(", but unused (no driver support)");
    874 			sc->sc_dma_ok = 0;
    875 		} else {
    876 			pciide_mapreg_dma(sc, pa);
    877 			if (sc->sc_dma_ok != 0)
    878 				aprint_normal(", used without full driver "
    879 				    "support");
    880 		}
    881 	} else {
    882 		aprint_normal("%s: hardware does not support DMA",
    883 		    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
    884 		sc->sc_dma_ok = 0;
    885 	}
    886 	aprint_normal("\n");
    887 	if (sc->sc_dma_ok) {
    888 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    889 		sc->sc_wdcdev.irqack = pciide_irqack;
    890 	}
    891 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    892 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 0;
    893 
    894 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    895 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    896 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    897 
    898 	wdc_allocate_regs(&sc->sc_wdcdev);
    899 
    900 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    901 	     channel++) {
    902 		cp = &sc->pciide_channels[channel];
    903 		if (pciide_chansetup(sc, channel, interface) == 0)
    904 			continue;
    905 		wdr = CHAN_TO_WDC_REGS(&cp->ata_channel);
    906 		if (interface & PCIIDE_INTERFACE_PCI(channel))
    907 			pciide_mapregs_native(pa, cp, &cmdsize, &ctlsize,
    908 			    pciide_pci_intr);
    909 		else
    910 			pciide_mapregs_compat(pa, cp,
    911 			    cp->ata_channel.ch_channel, &cmdsize, &ctlsize);
    912 		if (cp->ata_channel.ch_flags & ATACH_DISABLED)
    913 			continue;
    914 		/*
    915 		 * Check to see if something appears to be there.
    916 		 */
    917 		failreason = NULL;
    918 		/*
    919 		 * In native mode, always enable the controller. It's
    920 		 * not possible to have an ISA board using the same address
    921 		 * anyway.
    922 		 */
    923 		if (interface & PCIIDE_INTERFACE_PCI(channel)) {
    924 			wdcattach(&cp->ata_channel);
    925 			continue;
    926 		}
    927 		if (!wdcprobe(&cp->ata_channel)) {
    928 			failreason = "not responding; disabled or no drives?";
    929 			goto next;
    930 		}
    931 		/*
    932 		 * Now, make sure it's actually attributable to this PCI IDE
    933 		 * channel by trying to access the channel again while the
    934 		 * PCI IDE controller's I/O space is disabled.  (If the
    935 		 * channel no longer appears to be there, it belongs to
    936 		 * this controller.)  YUCK!
    937 		 */
    938 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    939 		    PCI_COMMAND_STATUS_REG);
    940 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
    941 		    csr & ~PCI_COMMAND_IO_ENABLE);
    942 		if (wdcprobe(&cp->ata_channel))
    943 			failreason = "other hardware responding at addresses";
    944 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    945 		    PCI_COMMAND_STATUS_REG, csr);
    946 next:
    947 		if (failreason) {
    948 			aprint_error("%s: %s channel ignored (%s)\n",
    949 			    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name,
    950 			    failreason);
    951 			cp->ata_channel.ch_flags |= ATACH_DISABLED;
    952 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
    953 			    cmdsize);
    954 			bus_space_unmap(wdr->ctl_iot, wdr->ctl_ioh, ctlsize);
    955 		} else {
    956 			pciide_map_compat_intr(pa, cp,
    957 			    cp->ata_channel.ch_channel);
    958 			wdcattach(&cp->ata_channel);
    959 		}
    960 	}
    961 
    962 	if (sc->sc_dma_ok == 0)
    963 		return;
    964 
    965 	/* Allocate DMA maps */
    966 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    967 	     channel++) {
    968 		idedma_ctl = 0;
    969 		cp = &sc->pciide_channels[channel];
    970 		for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) {
    971 			/*
    972 			 * we have not probed the drives yet, allocate
    973 			 * ressources for all of them.
    974 			 */
    975 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    976 				/* Abort DMA setup */
    977 				aprint_error(
    978 				    "%s:%d:%d: can't allocate DMA maps, "
    979 				    "using PIO transfers\n",
    980 				    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
    981 				    channel, drive);
    982 				sc->sc_dma_ok = 0;
    983 				sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
    984 				sc->sc_wdcdev.irqack = NULL;
    985 				break;
    986 			}
    987 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    988 		}
    989 		if (idedma_ctl != 0) {
    990 			/* Add software bits in status register */
    991 			bus_space_write_1(sc->sc_dma_iot,
    992 			    cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
    993 		}
    994 	}
    995 }
    996 
    997 void
    998 sata_setup_channel(chp)
    999 	struct ata_channel *chp;
   1000 {
   1001 	struct ata_drive_datas *drvp;
   1002 	int drive, s;
   1003 	u_int32_t idedma_ctl;
   1004 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
   1005 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
   1006 
   1007 	/* setup DMA if needed */
   1008 	pciide_channel_dma_setup(cp);
   1009 
   1010 	idedma_ctl = 0;
   1011 
   1012 	for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) {
   1013 		drvp = &chp->ch_drive[drive];
   1014 		/* If no drive, skip */
   1015 		if ((drvp->drive_flags & DRIVE) == 0)
   1016 			continue;
   1017 		if (drvp->drive_flags & DRIVE_UDMA) {
   1018 			/* use Ultra/DMA */
   1019 			s = splbio();
   1020 			drvp->drive_flags &= ~DRIVE_DMA;
   1021 			splx(s);
   1022 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1023 		} else if (drvp->drive_flags & DRIVE_DMA) {
   1024 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1025 		}
   1026 	}
   1027 
   1028 	/*
   1029 	 * Nothing to do to setup modes; it is meaningless in S-ATA
   1030 	 * (but many S-ATA drives still want to get the SET_FEATURE
   1031 	 * command).
   1032 	 */
   1033 	if (idedma_ctl != 0) {
   1034 		/* Add software bits in status register */
   1035 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
   1036 		    idedma_ctl);
   1037 	}
   1038 }
   1039