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