Home | History | Annotate | Line # | Download | only in pci
pciide_common.c revision 1.43.2.1
      1 /*	$NetBSD: pciide_common.c,v 1.43.2.1 2010/11/06 08:08:31 uebayasi 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.43.2.1 2010/11/06 08:08:31 uebayasi 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 /* tell whether the chip is enabled or not */
    226 int
    227 pciide_chipen(struct pciide_softc *sc, struct pci_attach_args *pa)
    228 {
    229 	pcireg_t csr;
    230 
    231 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    232 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    233 		    PCI_COMMAND_STATUS_REG);
    234 		aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    235 		    "device disabled (at %s)\n",
    236 		   (csr & PCI_COMMAND_IO_ENABLE) == 0 ?
    237 		   "device" : "bridge");
    238 		return 0;
    239 	}
    240 	return 1;
    241 }
    242 
    243 void
    244 pciide_mapregs_compat(struct pci_attach_args *pa, struct pciide_channel *cp, int compatchan)
    245 {
    246 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    247 	struct ata_channel *wdc_cp = &cp->ata_channel;
    248 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    249 	int i;
    250 
    251 	cp->compat = 1;
    252 
    253 	wdr->cmd_iot = pa->pa_iot;
    254 	if (bus_space_map(wdr->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
    255 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdr->cmd_baseioh) != 0) {
    256 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    257 		    "couldn't map %s channel cmd regs\n", cp->name);
    258 		goto bad;
    259 	}
    260 	wdr->cmd_ios = PCIIDE_COMPAT_CMD_SIZE;
    261 
    262 	wdr->ctl_iot = pa->pa_iot;
    263 	if (bus_space_map(wdr->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
    264 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdr->ctl_ioh) != 0) {
    265 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    266 		    "couldn't map %s channel ctl regs\n", cp->name);
    267 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
    268 		goto bad;
    269 	}
    270 	wdr->ctl_ios = PCIIDE_COMPAT_CTL_SIZE;
    271 
    272 	for (i = 0; i < WDC_NREG; i++) {
    273 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
    274 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    275 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    276 			    "couldn't subregion %s channel cmd regs\n",
    277 			    cp->name);
    278 			goto bad;
    279 		}
    280 	}
    281 	wdc_init_shadow_regs(wdc_cp);
    282 	wdr->data32iot = wdr->cmd_iot;
    283 	wdr->data32ioh = wdr->cmd_iohs[0];
    284 	return;
    285 
    286 bad:
    287 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    288 	return;
    289 }
    290 
    291 void
    292 pciide_mapregs_native(struct pci_attach_args *pa,
    293 	struct pciide_channel *cp, int (*pci_intr)(void *))
    294 {
    295 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    296 	struct ata_channel *wdc_cp = &cp->ata_channel;
    297 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
    298 	const char *intrstr;
    299 	pci_intr_handle_t intrhandle;
    300 	int i;
    301 
    302 	cp->compat = 0;
    303 
    304 	if (sc->sc_pci_ih == NULL) {
    305 		if (pci_intr_map(pa, &intrhandle) != 0) {
    306 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    307 			    "couldn't map native-PCI interrupt\n");
    308 			goto bad;
    309 		}
    310 		intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    311 		sc->sc_pci_ih = pci_intr_establish(pa->pa_pc,
    312 		    intrhandle, IPL_BIO, pci_intr, sc);
    313 		if (sc->sc_pci_ih != NULL) {
    314 			aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    315 			    "using %s for native-PCI interrupt\n",
    316 			    intrstr ? intrstr : "unknown interrupt");
    317 		} else {
    318 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    319 			    "couldn't establish native-PCI interrupt");
    320 			if (intrstr != NULL)
    321 				aprint_error(" at %s", intrstr);
    322 			aprint_error("\n");
    323 			goto bad;
    324 		}
    325 	}
    326 	cp->ih = sc->sc_pci_ih;
    327 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->ch_channel),
    328 	    PCI_MAPREG_TYPE_IO, 0,
    329 	    &wdr->cmd_iot, &wdr->cmd_baseioh, NULL, &wdr->cmd_ios) != 0) {
    330 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    331 		    "couldn't map %s channel cmd regs\n", cp->name);
    332 		goto bad;
    333 	}
    334 
    335 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->ch_channel),
    336 	    PCI_MAPREG_TYPE_IO, 0,
    337 	    &wdr->ctl_iot, &cp->ctl_baseioh, NULL, &cp->ctl_ios) != 0) {
    338 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    339 		    "couldn't map %s channel ctl regs\n", cp->name);
    340 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
    341 		goto bad;
    342 	}
    343 	/*
    344 	 * In native mode, 4 bytes of I/O space are mapped for the control
    345 	 * register, the control register is at offset 2. Pass the generic
    346 	 * code a handle for only one byte at the right offset.
    347 	 */
    348 	if (bus_space_subregion(wdr->ctl_iot, cp->ctl_baseioh, 2, 1,
    349 	    &wdr->ctl_ioh) != 0) {
    350 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    351 		    "unable to subregion %s channel ctl regs\n", cp->name);
    352 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
    353 		bus_space_unmap(wdr->cmd_iot, cp->ctl_baseioh, cp->ctl_ios);
    354 		goto bad;
    355 	}
    356 
    357 	for (i = 0; i < WDC_NREG; i++) {
    358 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
    359 		    i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
    360 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    361 			    "couldn't subregion %s channel cmd regs\n",
    362 			    cp->name);
    363 			goto bad;
    364 		}
    365 	}
    366 	wdc_init_shadow_regs(wdc_cp);
    367 	wdr->data32iot = wdr->cmd_iot;
    368 	wdr->data32ioh = wdr->cmd_iohs[0];
    369 	return;
    370 
    371 bad:
    372 	cp->ata_channel.ch_flags |= ATACH_DISABLED;
    373 	return;
    374 }
    375 
    376 #if NATA_DMA
    377 void
    378 pciide_mapreg_dma(struct pciide_softc *sc, struct pci_attach_args *pa)
    379 {
    380 	pcireg_t maptype;
    381 	bus_addr_t addr;
    382 	struct pciide_channel *pc;
    383 	int reg, chan;
    384 	bus_size_t size;
    385 
    386 	/*
    387 	 * Map DMA registers
    388 	 *
    389 	 * Note that sc_dma_ok is the right variable to test to see if
    390 	 * DMA can be done.  If the interface doesn't support DMA,
    391 	 * sc_dma_ok will never be non-zero.  If the DMA regs couldn't
    392 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    393 	 * non-zero if the interface supports DMA and the registers
    394 	 * could be mapped.
    395 	 *
    396 	 * XXX Note that despite the fact that the Bus Master IDE specs
    397 	 * XXX say that "The bus master IDE function uses 16 bytes of IO
    398 	 * XXX space," some controllers (at least the United
    399 	 * XXX Microelectronics UM8886BF) place it in memory space.
    400 	 */
    401 	maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,
    402 	    PCIIDE_REG_BUS_MASTER_DMA);
    403 
    404 	switch (maptype) {
    405 	case PCI_MAPREG_TYPE_IO:
    406 		sc->sc_dma_ok = (pci_mapreg_info(pa->pa_pc, pa->pa_tag,
    407 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO,
    408 		    &addr, NULL, NULL) == 0);
    409 		if (sc->sc_dma_ok == 0) {
    410 			aprint_verbose(
    411 			    ", but unused (couldn't query registers)");
    412 			break;
    413 		}
    414 		if ((sc->sc_pp->ide_flags & IDE_16BIT_IOSPACE)
    415 		    && addr >= 0x10000) {
    416 			sc->sc_dma_ok = 0;
    417 			aprint_verbose(
    418 			    ", but unused (registers at unsafe address "
    419 			    "%#lx)", (unsigned long)addr);
    420 			break;
    421 		}
    422 		/* FALLTHROUGH */
    423 
    424 	case PCI_MAPREG_MEM_TYPE_32BIT:
    425 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    426 		    PCIIDE_REG_BUS_MASTER_DMA, maptype, 0,
    427 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, &sc->sc_dma_ios)
    428 		    == 0);
    429 		sc->sc_dmat = pa->pa_dmat;
    430 		if (sc->sc_dma_ok == 0) {
    431 			aprint_verbose(", but unused (couldn't map registers)");
    432 		} else {
    433 			sc->sc_wdcdev.dma_arg = sc;
    434 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    435 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    436 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    437 		}
    438 
    439 		if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    440 		    PCIIDE_OPTIONS_NODMA) {
    441 			aprint_verbose(
    442 			    ", but unused (forced off by config file)");
    443 			sc->sc_dma_ok = 0;
    444 		}
    445 		break;
    446 
    447 	default:
    448 		sc->sc_dma_ok = 0;
    449 		aprint_verbose(
    450 		    ", but unsupported register maptype (0x%x)", maptype);
    451 	}
    452 
    453 	if (sc->sc_dma_ok == 0)
    454 		return;
    455 
    456 	/*
    457 	 * Set up the default handles for the DMA registers.
    458 	 * Just reserve 32 bits for each handle, unless space
    459 	 * doesn't permit it.
    460 	 */
    461 	for (chan = 0; chan < PCIIDE_NUM_CHANNELS; chan++) {
    462 		pc = &sc->pciide_channels[chan];
    463 		for (reg = 0; reg < IDEDMA_NREGS; reg++) {
    464 			size = 4;
    465 			if (size > (IDEDMA_SCH_OFFSET - reg))
    466 				size = IDEDMA_SCH_OFFSET - reg;
    467 			if (bus_space_subregion(sc->sc_dma_iot, sc->sc_dma_ioh,
    468 			    IDEDMA_SCH_OFFSET * chan + reg, size,
    469 			    &pc->dma_iohs[reg]) != 0) {
    470 				sc->sc_dma_ok = 0;
    471 				aprint_verbose(", but can't subregion offset %d "
    472 					      "size %lu", reg, (u_long)size);
    473 				return;
    474 			}
    475 		}
    476 	}
    477 }
    478 #endif	/* NATA_DMA */
    479 
    480 int
    481 pciide_compat_intr(void *arg)
    482 {
    483 	struct pciide_channel *cp = arg;
    484 
    485 #ifdef DIAGNOSTIC
    486 	/* should only be called for a compat channel */
    487 	if (cp->compat == 0)
    488 		panic("pciide compat intr called for non-compat chan %p", cp);
    489 #endif
    490 	return (wdcintr(&cp->ata_channel));
    491 }
    492 
    493 int
    494 pciide_pci_intr(void *arg)
    495 {
    496 	struct pciide_softc *sc = arg;
    497 	struct pciide_channel *cp;
    498 	struct ata_channel *wdc_cp;
    499 	int i, rv, crv;
    500 
    501 	rv = 0;
    502 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
    503 		cp = &sc->pciide_channels[i];
    504 		wdc_cp = &cp->ata_channel;
    505 
    506 		/* If a compat channel skip. */
    507 		if (cp->compat)
    508 			continue;
    509 		/* if this channel not waiting for intr, skip */
    510 		if ((wdc_cp->ch_flags & ATACH_IRQ_WAIT) == 0)
    511 			continue;
    512 
    513 		crv = wdcintr(wdc_cp);
    514 		if (crv == 0)
    515 			;		/* leave rv alone */
    516 		else if (crv == 1)
    517 			rv = 1;		/* claim the intr */
    518 		else if (rv == 0)	/* crv should be -1 in this case */
    519 			rv = crv;	/* if we've done no better, take it */
    520 	}
    521 	return (rv);
    522 }
    523 
    524 #if NATA_DMA
    525 void
    526 pciide_channel_dma_setup(struct pciide_channel *cp)
    527 {
    528 	int drive, s;
    529 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    530 	struct ata_drive_datas *drvp;
    531 
    532 	KASSERT(cp->ata_channel.ch_ndrive != 0);
    533 
    534 	for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) {
    535 		drvp = &cp->ata_channel.ch_drive[drive];
    536 		/* If no drive, skip */
    537 		if ((drvp->drive_flags & DRIVE) == 0)
    538 			continue;
    539 		/* setup DMA if needed */
    540 		if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    541 		    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    542 		    sc->sc_dma_ok == 0) {
    543 			s = splbio();
    544 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    545 			splx(s);
    546 			continue;
    547 		}
    548 		if (pciide_dma_table_setup(sc, cp->ata_channel.ch_channel,
    549 					   drive) != 0) {
    550 			/* Abort DMA setup */
    551 			s = splbio();
    552 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    553 			splx(s);
    554 			continue;
    555 		}
    556 	}
    557 }
    558 
    559 #define NIDEDMA_TABLES(sc)	\
    560 	(MAXPHYS/(min((sc)->sc_dma_maxsegsz, PAGE_SIZE)) + 1)
    561 
    562 int
    563 pciide_dma_table_setup(struct pciide_softc *sc, int channel, int drive)
    564 {
    565 	int error;
    566 	const bus_size_t dma_table_size =
    567 	    sizeof(struct idedma_table) * NIDEDMA_TABLES(sc);
    568 	struct pciide_dma_maps *dma_maps =
    569 	    &sc->pciide_channels[channel].dma_maps[drive];
    570 
    571 	/* If table was already allocated, just return */
    572 	if (dma_maps->dma_table)
    573 		return 0;
    574 
    575 	/* Allocate memory for the DMA tables and map it */
    576 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
    577 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &dma_maps->dmamap_table_seg,
    578 	    1, &dma_maps->dmamap_table_nseg, BUS_DMA_NOWAIT)) != 0) {
    579 		aprint_error(dmaerrfmt,
    580 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    581 		    "allocate", drive, error);
    582 		return error;
    583 	}
    584 	if ((error = bus_dmamem_map(sc->sc_dmat, &dma_maps->dmamap_table_seg,
    585 	    dma_maps->dmamap_table_nseg, dma_table_size,
    586 	    (void **)&dma_maps->dma_table,
    587 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    588 		aprint_error(dmaerrfmt,
    589 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    590 		    "map", drive, error);
    591 		return error;
    592 	}
    593 	ATADEBUG_PRINT(("pciide_dma_table_setup: table at %p len %lu, "
    594 	    "phy 0x%lx\n", dma_maps->dma_table, (u_long)dma_table_size,
    595 	    (unsigned long)dma_maps->dmamap_table_seg.ds_addr), DEBUG_PROBE);
    596 	/* Create and load table DMA map for this disk */
    597 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
    598 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
    599 	    &dma_maps->dmamap_table)) != 0) {
    600 		aprint_error(dmaerrfmt,
    601 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    602 		    "create", drive, error);
    603 		return error;
    604 	}
    605 	if ((error = bus_dmamap_load(sc->sc_dmat,
    606 	    dma_maps->dmamap_table,
    607 	    dma_maps->dma_table,
    608 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
    609 		aprint_error(dmaerrfmt,
    610 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    611 		    "load", drive, error);
    612 		return error;
    613 	}
    614 	ATADEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
    615 	    (unsigned long)dma_maps->dmamap_table->dm_segs[0].ds_addr),
    616 	    DEBUG_PROBE);
    617 	/* Create a xfer DMA map for this drive */
    618 	if ((error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
    619 	    NIDEDMA_TABLES(sc), sc->sc_dma_maxsegsz, sc->sc_dma_boundary,
    620 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    621 	    &dma_maps->dmamap_xfer)) != 0) {
    622 		aprint_error(dmaerrfmt,
    623 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    624 		    "create xfer", drive, error);
    625 		return error;
    626 	}
    627 	return 0;
    628 }
    629 
    630 void
    631 pciide_dma_table_teardown(struct pciide_softc *sc, int channel, int drive)
    632 {
    633 	struct pciide_channel *cp;
    634 	struct pciide_dma_maps *dma_maps;
    635 
    636 	cp = &sc->pciide_channels[channel];
    637 	dma_maps = &cp->dma_maps[drive];
    638 
    639 	if (dma_maps->dma_table == NULL)
    640 		return;
    641 
    642 	bus_dmamap_destroy(sc->sc_dmat, dma_maps->dmamap_xfer);
    643 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_table);
    644 	bus_dmamap_destroy(sc->sc_dmat, dma_maps->dmamap_table);
    645 	bus_dmamem_unmap(sc->sc_dmat, dma_maps->dma_table,
    646 	    sizeof(struct idedma_table) * NIDEDMA_TABLES(sc));
    647 	bus_dmamem_free(sc->sc_dmat, &dma_maps->dmamap_table_seg,
    648 	    dma_maps->dmamap_table_nseg);
    649 
    650 	dma_maps->dma_table = NULL;
    651 
    652 	return;
    653 }
    654 
    655 int
    656 pciide_dma_dmamap_setup(struct pciide_softc *sc, int channel, int drive, void *databuf, size_t datalen, int flags)
    657 {
    658 	int error, seg;
    659 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    660 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    661 
    662 	error = bus_dmamap_load(sc->sc_dmat,
    663 	    dma_maps->dmamap_xfer,
    664 	    databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    665 	    ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
    666 	if (error) {
    667 		aprint_error(dmaerrfmt,
    668 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    669 		    "load xfer", drive, error);
    670 		return error;
    671 	}
    672 
    673 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    674 	    dma_maps->dmamap_xfer->dm_mapsize,
    675 	    (flags & WDC_DMA_READ) ?
    676 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    677 
    678 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
    679 #ifdef DIAGNOSTIC
    680 		/* A segment must not cross a 64k boundary */
    681 		{
    682 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
    683 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
    684 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
    685 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
    686 			printf("pciide_dma: segment %d physical addr 0x%lx"
    687 			    " len 0x%lx not properly aligned\n",
    688 			    seg, phys, len);
    689 			panic("pciide_dma: buf align");
    690 		}
    691 		}
    692 #endif
    693 		dma_maps->dma_table[seg].base_addr =
    694 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_addr);
    695 		dma_maps->dma_table[seg].byte_count =
    696 		    htole32(dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
    697 		    IDEDMA_BYTE_COUNT_MASK);
    698 		ATADEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
    699 		   seg, le32toh(dma_maps->dma_table[seg].byte_count),
    700 		   le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
    701 
    702 	}
    703 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
    704 	    htole32(IDEDMA_BYTE_COUNT_EOT);
    705 
    706 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
    707 	    dma_maps->dmamap_table->dm_mapsize,
    708 	    BUS_DMASYNC_PREWRITE);
    709 
    710 #ifdef DIAGNOSTIC
    711 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
    712 		printf("pciide_dma_dmamap_setup: addr 0x%lx "
    713 		    "not properly aligned\n",
    714 		    (u_long)dma_maps->dmamap_table->dm_segs[0].ds_addr);
    715 		panic("pciide_dma_init: table align");
    716 	}
    717 #endif
    718 	/* remember flags */
    719 	dma_maps->dma_flags = flags;
    720 
    721 	return 0;
    722 }
    723 
    724 int
    725 pciide_dma_init(void *v, int channel, int drive, void *databuf, size_t datalen, int flags)
    726 {
    727 	struct pciide_softc *sc = v;
    728 	int error;
    729 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    730 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    731 
    732 	if ((error = pciide_dma_dmamap_setup(sc, channel, drive,
    733 	    databuf, datalen, flags)) != 0)
    734 		return error;
    735 	/* Maps are ready. Start DMA function */
    736 	/* Clear status bits */
    737 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    738 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
    739 	/* Write table addr */
    740 	bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0,
    741 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
    742 	/* set read/write */
    743 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    744 	    ((flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE : 0) | cp->idedma_cmd);
    745 	return 0;
    746 }
    747 
    748 void
    749 pciide_dma_start(void *v, int channel, int drive)
    750 {
    751 	struct pciide_softc *sc = v;
    752 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    753 
    754 	ATADEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
    755 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    756 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
    757 		| IDEDMA_CMD_START);
    758 }
    759 
    760 int
    761 pciide_dma_finish(void *v, int channel, int drive, int force)
    762 {
    763 	struct pciide_softc *sc = v;
    764 	u_int8_t status;
    765 	int error = 0;
    766 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    767 	struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
    768 
    769 	status = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0);
    770 	ATADEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
    771 	    DEBUG_XFERS);
    772 
    773 	if (force == WDC_DMAEND_END && (status & IDEDMA_CTL_INTR) == 0)
    774 		return WDC_DMAST_NOIRQ;
    775 
    776 	/* stop DMA channel */
    777 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
    778 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
    779 		& ~IDEDMA_CMD_START);
    780 
    781 	/* Unload the map of the data buffer */
    782 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
    783 	    dma_maps->dmamap_xfer->dm_mapsize,
    784 	    (dma_maps->dma_flags & WDC_DMA_READ) ?
    785 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    786 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
    787 
    788 	if ((status & IDEDMA_CTL_ERR) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
    789 		aprint_error("%s:%d:%d: bus-master DMA error: status=0x%x\n",
    790 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
    791 		    drive, status);
    792 		error |= WDC_DMAST_ERR;
    793 	}
    794 
    795 	if ((status & IDEDMA_CTL_INTR) == 0 && force != WDC_DMAEND_ABRT_QUIET) {
    796 		aprint_error("%s:%d:%d: bus-master DMA error: missing "
    797 		    "interrupt, status=0x%x\n",
    798 		    device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
    799 		    channel, drive, status);
    800 		error |= WDC_DMAST_NOIRQ;
    801 	}
    802 
    803 	if ((status & IDEDMA_CTL_ACT) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
    804 		/* data underrun, may be a valid condition for ATAPI */
    805 		error |= WDC_DMAST_UNDER;
    806 	}
    807 	return error;
    808 }
    809 
    810 void
    811 pciide_irqack(struct ata_channel *chp)
    812 {
    813 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
    814 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
    815 
    816 	/* clear status bits in IDE DMA registers */
    817 	bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
    818 	    bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
    819 }
    820 #endif	/* NATA_DMA */
    821 
    822 /* some common code used by several chip_map */
    823 int
    824 pciide_chansetup(struct pciide_softc *sc, int channel, pcireg_t interface)
    825 {
    826 	struct pciide_channel *cp = &sc->pciide_channels[channel];
    827 	sc->wdc_chanarray[channel] = &cp->ata_channel;
    828 	cp->name = PCIIDE_CHANNEL_NAME(channel);
    829 	cp->ata_channel.ch_channel = channel;
    830 	cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    831 	cp->ata_channel.ch_queue =
    832 	    malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
    833 	if (cp->ata_channel.ch_queue == NULL) {
    834 		aprint_error("%s %s channel: "
    835 		    "can't allocate memory for command queue",
    836 		device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cp->name);
    837 		return 0;
    838 	}
    839 	cp->ata_channel.ch_ndrive = 2;
    840 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    841 	    "%s channel %s to %s mode\n", cp->name,
    842 	    (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
    843 	    "configured" : "wired",
    844 	    (interface & PCIIDE_INTERFACE_PCI(channel)) ?
    845 	    "native-PCI" : "compatibility");
    846 	return 1;
    847 }
    848 
    849 /* some common code used by several chip channel_map */
    850 void
    851 pciide_mapchan(struct pci_attach_args *pa,
    852 	struct pciide_channel *cp,
    853 	pcireg_t interface, int (*pci_intr)(void *))
    854 {
    855 	struct ata_channel *wdc_cp = &cp->ata_channel;
    856 
    857 	if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->ch_channel))
    858 		pciide_mapregs_native(pa, cp, pci_intr);
    859 	else {
    860 		pciide_mapregs_compat(pa, cp, wdc_cp->ch_channel);
    861 		if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
    862 			pciide_map_compat_intr(pa, cp, wdc_cp->ch_channel);
    863 	}
    864 	wdcattach(wdc_cp);
    865 }
    866 
    867 /*
    868  * generic code to map the compat intr.
    869  */
    870 void
    871 pciide_map_compat_intr(struct pci_attach_args *pa, struct pciide_channel *cp, int compatchan)
    872 {
    873 	struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
    874 
    875 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    876 	cp->ih =
    877 	   pciide_machdep_compat_intr_establish(sc->sc_wdcdev.sc_atac.atac_dev,
    878 	   pa, compatchan, pciide_compat_intr, cp);
    879 	if (cp->ih == NULL) {
    880 #endif
    881 		aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    882 		    "no compatibility interrupt for use by %s "
    883 		    "channel\n", cp->name);
    884 		cp->ata_channel.ch_flags |= ATACH_DISABLED;
    885 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    886 	}
    887 #endif
    888 }
    889 
    890 void
    891 default_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
    892 {
    893 	struct pciide_channel *cp;
    894 	pcireg_t interface = PCI_INTERFACE(pa->pa_class);
    895 	pcireg_t csr;
    896 	int channel;
    897 #if NATA_DMA
    898 	int drive;
    899 	u_int8_t idedma_ctl;
    900 #endif
    901 	const char *failreason;
    902 	struct wdc_regs *wdr;
    903 
    904 	if (pciide_chipen(sc, pa) == 0)
    905 		return;
    906 
    907 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    908 #if NATA_DMA
    909 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    910 		    "bus-master DMA support present");
    911 		if (sc->sc_pp == &default_product_desc &&
    912 		    (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
    913 		    PCIIDE_OPTIONS_DMA) == 0) {
    914 			aprint_verbose(", but unused (no driver support)");
    915 			sc->sc_dma_ok = 0;
    916 		} else {
    917 			pciide_mapreg_dma(sc, pa);
    918 			if (sc->sc_dma_ok != 0)
    919 				aprint_verbose(", used without full driver "
    920 				    "support");
    921 		}
    922 #else
    923 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    924 		    "bus-master DMA support present, but unused (no driver "
    925 		    "support)");
    926 #endif	/* NATA_DMA */
    927 	} else {
    928 		aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    929 		    "hardware does not support DMA");
    930 #if NATA_DMA
    931 		sc->sc_dma_ok = 0;
    932 #endif
    933 	}
    934 	aprint_verbose("\n");
    935 #if NATA_DMA
    936 	if (sc->sc_dma_ok) {
    937 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    938 		sc->sc_wdcdev.irqack = pciide_irqack;
    939 	}
    940 #endif
    941 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    942 #if NATA_DMA
    943 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 0;
    944 #endif
    945 
    946 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
    947 	sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
    948 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    949 
    950 	wdc_allocate_regs(&sc->sc_wdcdev);
    951 
    952 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
    953 	     channel++) {
    954 		cp = &sc->pciide_channels[channel];
    955 		if (pciide_chansetup(sc, channel, interface) == 0)
    956 			continue;
    957 		wdr = CHAN_TO_WDC_REGS(&cp->ata_channel);
    958 		if (interface & PCIIDE_INTERFACE_PCI(channel))
    959 			pciide_mapregs_native(pa, cp, pciide_pci_intr);
    960 		else
    961 			pciide_mapregs_compat(pa, cp,
    962 			    cp->ata_channel.ch_channel);
    963 		if (cp->ata_channel.ch_flags & ATACH_DISABLED)
    964 			continue;
    965 		/*
    966 		 * Check to see if something appears to be there.
    967 		 */
    968 		failreason = NULL;
    969 		/*
    970 		 * In native mode, always enable the controller. It's
    971 		 * not possible to have an ISA board using the same address
    972 		 * anyway.
    973 		 */
    974 		if (interface & PCIIDE_INTERFACE_PCI(channel)) {
    975 			wdcattach(&cp->ata_channel);
    976 			continue;
    977 		}
    978 		if (!wdcprobe(&cp->ata_channel)) {
    979 			failreason = "not responding; disabled or no drives?";
    980 			goto next;
    981 		}
    982 		/*
    983 		 * Now, make sure it's actually attributable to this PCI IDE
    984 		 * channel by trying to access the channel again while the
    985 		 * PCI IDE controller's I/O space is disabled.  (If the
    986 		 * channel no longer appears to be there, it belongs to
    987 		 * this controller.)  YUCK!
    988 		 */
    989 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    990 		    PCI_COMMAND_STATUS_REG);
    991 		pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
    992 		    csr & ~PCI_COMMAND_IO_ENABLE);
    993 		if (wdcprobe(&cp->ata_channel))
    994 			failreason = "other hardware responding at addresses";
    995 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    996 		    PCI_COMMAND_STATUS_REG, csr);
    997 next:
    998 		if (failreason) {
    999 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
   1000 			    "%s channel ignored (%s)\n", cp->name, failreason);
   1001 			cp->ata_channel.ch_flags |= ATACH_DISABLED;
   1002 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
   1003 			    wdr->cmd_ios);
   1004 			bus_space_unmap(wdr->ctl_iot, wdr->ctl_ioh,
   1005 			    wdr->ctl_ios);
   1006 		} else {
   1007 			pciide_map_compat_intr(pa, cp,
   1008 			    cp->ata_channel.ch_channel);
   1009 			wdcattach(&cp->ata_channel);
   1010 		}
   1011 	}
   1012 
   1013 #if NATA_DMA
   1014 	if (sc->sc_dma_ok == 0)
   1015 		return;
   1016 
   1017 	/* Allocate DMA maps */
   1018 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
   1019 	     channel++) {
   1020 		idedma_ctl = 0;
   1021 		cp = &sc->pciide_channels[channel];
   1022 		for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) {
   1023 			/*
   1024 			 * we have not probed the drives yet, allocate
   1025 			 * ressources for all of them.
   1026 			 */
   1027 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
   1028 				/* Abort DMA setup */
   1029 				aprint_error(
   1030 				    "%s:%d:%d: can't allocate DMA maps, "
   1031 				    "using PIO transfers\n",
   1032 				    device_xname(
   1033 				      sc->sc_wdcdev.sc_atac.atac_dev),
   1034 				    channel, drive);
   1035 				sc->sc_dma_ok = 0;
   1036 				sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
   1037 				sc->sc_wdcdev.irqack = NULL;
   1038 				break;
   1039 			}
   1040 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1041 		}
   1042 		if (idedma_ctl != 0) {
   1043 			/* Add software bits in status register */
   1044 			bus_space_write_1(sc->sc_dma_iot,
   1045 			    cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
   1046 		}
   1047 	}
   1048 #endif	/* NATA_DMA */
   1049 }
   1050 
   1051 void
   1052 sata_setup_channel(struct ata_channel *chp)
   1053 {
   1054 #if NATA_DMA
   1055 	struct ata_drive_datas *drvp;
   1056 	int drive;
   1057 #if NATA_UDMA
   1058 	int s;
   1059 #endif
   1060 	u_int32_t idedma_ctl;
   1061 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
   1062 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
   1063 
   1064 	/* setup DMA if needed */
   1065 	pciide_channel_dma_setup(cp);
   1066 
   1067 	idedma_ctl = 0;
   1068 
   1069 	for (drive = 0; drive < cp->ata_channel.ch_ndrive; drive++) {
   1070 		drvp = &chp->ch_drive[drive];
   1071 		/* If no drive, skip */
   1072 		if ((drvp->drive_flags & DRIVE) == 0)
   1073 			continue;
   1074 #if NATA_UDMA
   1075 		if (drvp->drive_flags & DRIVE_UDMA) {
   1076 			/* use Ultra/DMA */
   1077 			s = splbio();
   1078 			drvp->drive_flags &= ~DRIVE_DMA;
   1079 			splx(s);
   1080 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1081 		} else
   1082 #endif	/* NATA_UDMA */
   1083 		if (drvp->drive_flags & DRIVE_DMA) {
   1084 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
   1085 		}
   1086 	}
   1087 
   1088 	/*
   1089 	 * Nothing to do to setup modes; it is meaningless in S-ATA
   1090 	 * (but many S-ATA drives still want to get the SET_FEATURE
   1091 	 * command).
   1092 	 */
   1093 	if (idedma_ctl != 0) {
   1094 		/* Add software bits in status register */
   1095 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
   1096 		    idedma_ctl);
   1097 	}
   1098 #endif	/* NATA_DMA */
   1099 }
   1100