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