Home | History | Annotate | Line # | Download | only in pci
pciide.c revision 1.6.2.7
      1 /*	$NetBSD: pciide.c,v 1.6.2.7 1998/06/11 09:27:24 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * PCI IDE controller driver.
     35  *
     36  * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
     37  * sys/dev/pci/ppb.c, revision 1.16).
     38  *
     39  * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
     40  * "Programming Interface for Bus Master IDE Controller, Revision 1.0
     41  * 5/16/94" from the PCI SIG.
     42  *
     43  * XXX Does not yet support DMA (but does map the Bus Master DMA regs).
     44  *
     45  * XXX Does not support serializing the two channels for broken (at least
     46  * XXX according to linux and freebsd) controllers, e.g. CMD PCI0640.
     47  */
     48 
     49 #define WDCDEBUG
     50 
     51 #define DEBUG_DMA   0x01
     52 #define DEBUG_XFERS  0x02
     53 #define DEBUG_FUNCS  0x08
     54 #define DEBUG_PROBE  0x10
     55 #ifdef WDCDEBUG
     56 int wdcdebug_pciide_mask = DEBUG_PROBE;
     57 #define WDCDEBUG_PRINT(args, level) \
     58 	if (wdcdebug_pciide_mask & (level)) printf args
     59 #else
     60 #define WDCDEBUG_PRINT(args, level)
     61 #endif
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/device.h>
     65 #include <sys/malloc.h>
     66 
     67 #include <vm/vm.h>
     68 #include <vm/vm_param.h>
     69 #include <vm/vm_kern.h>
     70 
     71 #include <dev/pci/pcireg.h>
     72 #include <dev/pci/pcivar.h>
     73 #include <dev/pci/pcidevs.h>
     74 #include <dev/pci/pciidereg.h>
     75 #include <dev/pci/pciidevar.h>
     76 #include <dev/pci/pciide_piix_reg.h>
     77 #include <dev/ata/atavar.h>
     78 #include <dev/ic/wdcreg.h>
     79 #include <dev/ic/wdcvar.h>
     80 
     81 struct pciide_softc {
     82 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
     83 
     84 	void			*sc_pci_ih;	/* PCI interrupt handle */
     85 	int			sc_dma_ok;	/* bus-master DMA info */
     86 	bus_space_tag_t		sc_dma_iot;
     87 	bus_space_handle_t	sc_dma_ioh;
     88 	bus_dma_tag_t		sc_dmat;
     89 	/* Chip description */
     90 	const struct pciide_product_desc *sc_pp;
     91 	/* common definitions */
     92 	struct channel_softc wdc_channels[PCIIDE_NUM_CHANNELS];
     93 	/* internal bookkeeping */
     94 	struct pciide_channel {			/* per-channel data */
     95 		int		hw_ok;		/* hardware mapped & OK? */
     96 		int		compat;		/* is it compat? */
     97 		void		*ih;		/* compat or pci handle */
     98 		/* DMA tables and DMA map for xfer, for each drive */
     99 		struct pciide_dma_maps {
    100 			bus_dmamap_t    dmamap_table;
    101 			struct idedma_table *dma_table;
    102 			bus_dmamap_t    dmamap_xfer;
    103 		} dma_maps[2];
    104 	} pciide_channels[PCIIDE_NUM_CHANNELS];
    105 };
    106 
    107 void default_setup_cap __P((struct pciide_softc*));
    108 void default_setup_chip __P((struct pciide_softc*,
    109 				pci_chipset_tag_t, pcitag_t));
    110 void piix_setup_cap __P((struct pciide_softc*));
    111 void piix_setup_chip __P((struct pciide_softc*,
    112 				pci_chipset_tag_t, pcitag_t));
    113 void piix3_4_setup_chip __P((struct pciide_softc*,
    114 				pci_chipset_tag_t, pcitag_t));
    115 
    116 static u_int32_t piix_setup_idetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    117 static u_int32_t piix_setup_idetim_drvs __P((struct ata_drive_datas*));
    118 static u_int32_t piix_setup_sidetim_timings __P((u_int8_t, u_int8_t, u_int8_t));
    119 
    120 int  pciide_dma_table_setup __P((struct pciide_softc*, int, int));
    121 int  pciide_dma_init __P((void*, int, int, void *, size_t, int));
    122 void pciide_dma_start __P((void*, int, int, int));
    123 int  pciide_dma_finish __P((void*, int, int, int));
    124 
    125 struct pciide_product_desc {
    126     u_int32_t ide_product;
    127     int ide_flags;
    128     const char *ide_name;
    129     /* init controller's capabilities for drives probe */
    130     void (*setup_cap) __P((struct pciide_softc*));
    131     /* init controller after drives probe */
    132     void (*setup_chip) __P((struct pciide_softc*, pci_chipset_tag_t, pcitag_t));
    133 };
    134 
    135 /* Flags for ide_flags */
    136 #define NO_PCI_INTR       0x01 /* don't try to map the native PCI intr */
    137 #define ONE_QUEUE         0x02 /* device need serialised access */
    138 
    139 /* Default product description for devices not known from this controller */
    140 const struct pciide_product_desc default_product_desc = {
    141     0,
    142     0,
    143     "Generic PCI IDE controller",
    144     default_setup_cap,
    145     default_setup_chip
    146 };
    147 
    148 
    149 const struct pciide_product_desc pciide_intel_products[] =  {
    150     { PCI_PRODUCT_INTEL_82092AA,
    151       0,
    152       "Intel 82092AA IDE controller",
    153       default_setup_cap,
    154       default_setup_chip
    155     },
    156     { PCI_PRODUCT_INTEL_82371FB_IDE,
    157       0,
    158       "Intel 82371FB IDE controller (PIIX)",
    159       piix_setup_cap,
    160       piix_setup_chip
    161     },
    162     { PCI_PRODUCT_INTEL_82371SB_IDE,
    163       0,
    164       "Intel 82371SB IDE Interface (PIIX3)",
    165       piix_setup_cap,
    166       piix3_4_setup_chip
    167     },
    168     { PCI_PRODUCT_INTEL_82371AB_IDE,
    169       0,
    170       "Intel 82371AB IDE controller (PIIX4)",
    171       piix_setup_cap,
    172       piix3_4_setup_chip
    173     },
    174     { 0,
    175       0,
    176       NULL,
    177     }
    178 };
    179 const struct pciide_product_desc pciide_cmd_products[] =  {
    180     { PCI_PRODUCT_CMDTECH_640,
    181       NO_PCI_INTR | ONE_QUEUE,
    182       "CMD Technology PCI0640",
    183       default_setup_cap,
    184       default_setup_chip
    185     },
    186     { 0,
    187       0,
    188       NULL,
    189     }
    190 };
    191 
    192 struct pciide_vendor_desc {
    193     u_int32_t ide_vendor;
    194     const struct pciide_product_desc *ide_products;
    195 };
    196 
    197 const struct pciide_vendor_desc pciide_vendors[] = {
    198     { PCI_VENDOR_INTEL, pciide_intel_products },
    199     { PCI_VENDOR_CMDTECH, pciide_cmd_products },
    200     { 0, NULL }
    201 };
    202 
    203 
    204 #define	PCIIDE_CHANNEL_NAME(chan)	((chan) == 0 ? "primary" : "secondary")
    205 
    206 int	pciide_match __P((struct device *, struct cfdata *, void *));
    207 void	pciide_attach __P((struct device *, struct device *, void *));
    208 
    209 struct cfattach pciide_ca = {
    210 	sizeof(struct pciide_softc), pciide_match, pciide_attach
    211 };
    212 
    213 int	pciide_map_channel_compat __P((struct pciide_softc *,
    214 	    struct pci_attach_args *, int));
    215 const char *pciide_compat_channel_probe __P((struct pciide_softc *,
    216 	    struct pci_attach_args *, int));
    217 int	pciide_map_channel_native __P((struct pciide_softc *,
    218 	    struct pci_attach_args *, int));
    219 int	pciide_print __P((void *, const char *pnp));
    220 int	pciide_compat_intr __P((void *));
    221 int	pciide_pci_intr __P((void *));
    222 const struct pciide_product_desc* pciide_lookup_product __P((u_int32_t));
    223 
    224 const struct pciide_product_desc*
    225 pciide_lookup_product(id)
    226     u_int32_t id;
    227 {
    228     const struct pciide_product_desc *pp;
    229     const struct pciide_vendor_desc *vp;
    230 
    231     for (vp = pciide_vendors; vp->ide_products != NULL; vp++)
    232 	if (PCI_VENDOR(id) == vp->ide_vendor)
    233 	    break;
    234 
    235     if ((pp = vp->ide_products) == NULL)
    236 	return NULL;
    237 
    238     for (; pp->ide_name != NULL; pp++)
    239 	if (PCI_PRODUCT(id) == pp->ide_product)
    240 	    break;
    241 
    242     if (pp->ide_name == NULL)
    243 	return NULL;
    244     return pp;
    245 }
    246 
    247 int
    248 pciide_match(parent, match, aux)
    249 	struct device *parent;
    250 	struct cfdata *match;
    251 	void *aux;
    252 {
    253 	struct pci_attach_args *pa = aux;
    254 
    255 	/*
    256 	 * Check the ID register to see that it's a PCI IDE controller.
    257 	 * If it is, we assume that we can deal with it; it _should_
    258 	 * work in a standardized way...
    259 	 */
    260 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
    261 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    262 		return (1);
    263 	}
    264 
    265 	return (0);
    266 }
    267 
    268 void
    269 pciide_attach(parent, self, aux)
    270 	struct device *parent, *self;
    271 	void *aux;
    272 {
    273 	struct pci_attach_args *pa = aux;
    274 	pci_chipset_tag_t pc = pa->pa_pc;
    275 	pcitag_t tag = pa->pa_tag;
    276 	struct pciide_softc *sc = (struct pciide_softc *)self;
    277 	struct pciide_channel *cp;
    278 	pcireg_t class, interface, csr;
    279 	pci_intr_handle_t intrhandle;
    280 	const char *intrstr;
    281 	char devinfo[256];
    282 	int i;
    283 
    284         sc->sc_pp = pciide_lookup_product(pa->pa_id);
    285 	if (sc->sc_pp == NULL) {
    286 		sc->sc_pp = &default_product_desc;
    287 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    288 		printf(": %s (rev. 0x%02x)\n", devinfo,
    289 		    PCI_REVISION(pa->pa_class));
    290 	} else {
    291 		printf(": %s\n", sc->sc_pp->ide_name);
    292 	}
    293 
    294 	if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
    295 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    296 		printf("%s: device disabled (at %s)\n",
    297 		    sc->sc_wdcdev.sc_dev.dv_xname,
    298 		    (csr & PCI_COMMAND_IO_ENABLE) == 0 ? "device" : "bridge");
    299 		return;
    300 	}
    301 
    302 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    303 	interface = PCI_INTERFACE(class);
    304 
    305 	/*
    306 	 * Set up PCI interrupt.
    307 	 *
    308 	 * If mapping fails, that's (probably) because there's no pin
    309 	 * set to intr, which is (probably) because it's a compat-only
    310 	 * device (or hard-wired in compatibility-only mode).  Native-PCI
    311 	 * channels will complain later if the interrupt was needed.
    312 	 *
    313 	 * If establishment fails, that's (probably) some other problem.
    314 	 */
    315 	if ((sc->sc_pp->ide_flags & NO_PCI_INTR) == 0) {
    316 	    if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    317 	        pa->pa_intrline, &intrhandle) == 0) {
    318 		    intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    319 		    sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle,
    320 		        IPL_BIO, pciide_pci_intr, sc);
    321 
    322 		    if (sc->sc_pci_ih != NULL) {
    323 			printf("%s: using %s for native-PCI interrupt\n",
    324 			    sc->sc_wdcdev.sc_dev.dv_xname,
    325 			    intrstr ? intrstr : "unknown interrupt");
    326 		    } else {
    327 			printf("%s: couldn't establish native-PCI interrupt",
    328 			    sc->sc_wdcdev.sc_dev.dv_xname);
    329 			if (intrstr != NULL)
    330 			    printf(" at %s", intrstr);
    331 			printf("\n");
    332 		    }
    333 	    }
    334 	}
    335 
    336 	/*
    337 	 * Map DMA registers, if DMA is supported.
    338 	 *
    339 	 * Note that sc_dma_ok is the right variable to test to see if
    340 	 * DMA can * be done.  If the interface doesn't support DMA,
    341 	 * sc_dma_ok * will never be non-zero.  If the DMA regs couldn't
    342 	 * be mapped, it'll be zero.  I.e., sc_dma_ok will only be
    343 	 * non-zero if the interface supports DMA and the registers
    344 	 * could be mapped.
    345 	 *
    346 	 * XXX Note that despite the fact that the Bus Master IDE specs
    347 	 * XXX say that "The bus master IDE functoin uses 16 bytes of IO
    348 	 * XXX space," some controllers (at least the United
    349 	 * XXX Microelectronics UM8886BF) place it in memory space.
    350 	 * XXX eventually, we should probably read the register and check
    351 	 * XXX which type it is.  Either that or 'quirk' certain devices.
    352 	 */
    353 	if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
    354 		sc->sc_dma_ok = (pci_mapreg_map(pa,
    355 		    PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO, 0,
    356 		    &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, NULL) == 0);
    357 		sc->sc_dmat = pa->pa_dmat;
    358 		printf("%s: bus-master DMA support present",
    359 		    sc->sc_wdcdev.sc_dev.dv_xname);
    360 		if (sc->sc_dma_ok == 0) {
    361 			printf(", but unused (couldn't map registers)");
    362 		} else if (sc->sc_pp == 0) {
    363 			printf(", but unused (no driver support)");
    364 		} else {
    365 			sc->sc_wdcdev.dma_arg = sc;
    366 			sc->sc_wdcdev.dma_init = pciide_dma_init;
    367 			sc->sc_wdcdev.dma_start = pciide_dma_start;
    368 			sc->sc_wdcdev.dma_finish = pciide_dma_finish;
    369 		}
    370 		printf("\n");
    371 	}
    372 	if (sc->sc_pp == NULL)
    373 		default_setup_cap(sc);
    374 	else
    375 		sc->sc_pp->setup_cap(sc);
    376 	sc->sc_wdcdev.channels = sc->wdc_channels;
    377 	sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
    378 
    379 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    380 		cp = &sc->pciide_channels[i];
    381 
    382 		sc->wdc_channels[i].channel = i;
    383 		sc->wdc_channels[i].wdc = &sc->sc_wdcdev;
    384 		if (i > 0 && (sc->sc_pp->ide_flags & ONE_QUEUE)) {
    385 		    sc->wdc_channels[i].ch_queue =
    386 			sc->wdc_channels[0].ch_queue;
    387 		} else {
    388 		    sc->wdc_channels[i].ch_queue =
    389 		        malloc(sizeof(struct channel_queue), M_DEVBUF,
    390 			M_NOWAIT);
    391 		}
    392 		if (sc->wdc_channels[i].ch_queue == NULL) {
    393 		    printf("%s %s channel: "
    394 			"can't allocate memory for command queue",
    395 			sc->sc_wdcdev.sc_dev.dv_xname,
    396 			PCIIDE_CHANNEL_NAME(i));
    397 			continue;
    398 		}
    399 		printf("%s: %s channel %s to %s mode\n",
    400 		    sc->sc_wdcdev.sc_dev.dv_xname,
    401 		    PCIIDE_CHANNEL_NAME(i),
    402 		    (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
    403 		      "configured" : "wired",
    404 		    (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
    405 		      "compatibility");
    406 
    407 		if (interface & PCIIDE_INTERFACE_PCI(i))
    408 			cp->hw_ok = pciide_map_channel_native(sc, pa, i);
    409 		else
    410 			cp->hw_ok = pciide_map_channel_compat(sc, pa, i);
    411 		if (!cp->hw_ok)
    412 			continue;
    413 		/* Now call common attach routine */
    414 		wdcattach(&sc->wdc_channels[i]);
    415 	}
    416 	if (sc->sc_pp == NULL)
    417 		default_setup_chip(sc, pc, tag);
    418 	else
    419 		sc->sc_pp->setup_chip(sc, pc, tag);
    420 	WDCDEBUG_PRINT(("pciide: command/status register=%x\n",
    421 	    pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
    422 }
    423 
    424 int
    425 pciide_map_channel_compat(sc, pa, chan)
    426 	struct pciide_softc *sc;
    427 	struct pci_attach_args *pa;
    428 	int chan;
    429 {
    430 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    431 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    432 	const char *probe_fail_reason;
    433 	int rv = 1;
    434 
    435 	cp->compat = 1;
    436 
    437 	wdc_cp->cmd_iot = pa->pa_iot;
    438 	if (bus_space_map(wdc_cp->cmd_iot, PCIIDE_COMPAT_CMD_BASE(chan),
    439 	    PCIIDE_COMPAT_CMD_SIZE, 0, &wdc_cp->cmd_ioh) != 0) {
    440 		printf("%s: couldn't map %s channel cmd regs\n",
    441 		    sc->sc_wdcdev.sc_dev.dv_xname,
    442 		    PCIIDE_CHANNEL_NAME(chan));
    443 		rv = 0;
    444 	}
    445 
    446 	wdc_cp->ctl_iot = pa->pa_iot;
    447 	if (bus_space_map(wdc_cp->ctl_iot, PCIIDE_COMPAT_CTL_BASE(chan),
    448 	    PCIIDE_COMPAT_CTL_SIZE, 0, &wdc_cp->ctl_ioh) != 0) {
    449 		printf("%s: couldn't map %s channel ctl regs\n",
    450 		    sc->sc_wdcdev.sc_dev.dv_xname,
    451 		    PCIIDE_CHANNEL_NAME(chan));
    452 		rv = 0;
    453 	}
    454 
    455 	/*
    456 	 * If we weren't able to map the device successfully,
    457 	 * we just give up now.  Something else has already
    458 	 * occupied those ports, indicating that the device has
    459 	 * (probably) been completely disabled (by some nonstandard
    460 	 * mechanism).
    461 	 *
    462 	 * XXX If we successfully map some ports, but not others,
    463 	 * XXX it might make sense to unmap the ones that we mapped.
    464 	 */
    465 	if (rv == 0)
    466 		goto out;
    467 
    468 	/*
    469 	 * If we were able to map the device successfully, try to
    470 	 * make sure that there's a wdc there and that it's
    471 	 * attributable to us.
    472 	 *
    473 	 * If there's not, then we assume that there's the device
    474 	 * has been disabled and that other devices are free to use
    475 	 * its ports.
    476 	 */
    477 	probe_fail_reason = pciide_compat_channel_probe(sc, pa, chan);
    478 	if (probe_fail_reason != NULL) {
    479 		printf("%s: %s channel ignored (%s)\n",
    480 		    sc->sc_wdcdev.sc_dev.dv_xname,
    481 		    PCIIDE_CHANNEL_NAME(chan), probe_fail_reason);
    482 		rv = 0;
    483 
    484 		bus_space_unmap(wdc_cp->cmd_iot, wdc_cp->cmd_ioh,
    485 		    PCIIDE_COMPAT_CMD_SIZE);
    486 		bus_space_unmap(wdc_cp->ctl_iot, wdc_cp->ctl_ioh,
    487 		    PCIIDE_COMPAT_CTL_SIZE);
    488 
    489 		goto out;
    490 	}
    491 
    492 	/*
    493 	 * If we're here, we were able to map the device successfully
    494 	 * and it really looks like there's a controller there.
    495 	 *
    496 	 * Unless those conditions are true, we don't map the
    497 	 * compatibility interrupt.  The spec indicates that if a
    498 	 * channel is configured for compatibility mode and the PCI
    499 	 * device's I/O space is enabled, the channel will be enabled.
    500 	 * Hoewver, some devices seem to be able to disable invididual
    501 	 * compatibility channels (via non-standard mechanisms).  If
    502 	 * the channel is disabled, the interrupt line can (probably)
    503 	 * be used by other devices (and may be assigned to other
    504 	 * devices by the BIOS).  If we mapped the interrupt we might
    505 	 * conflict with another interrupt assignment.
    506 	 */
    507 	cp->ih = pciide_machdep_compat_intr_establish(&sc->sc_wdcdev.sc_dev,
    508 	    pa, chan, pciide_compat_intr, wdc_cp);
    509 	if (cp->ih == NULL) {
    510 		printf("%s: no compatibility interrupt for use by %s channel\n",
    511 		    sc->sc_wdcdev.sc_dev.dv_xname,
    512 		    PCIIDE_CHANNEL_NAME(chan));
    513 		rv = 0;
    514 	}
    515 
    516 out:
    517 	return (rv);
    518 }
    519 
    520 const char *
    521 pciide_compat_channel_probe(sc, pa, chan)
    522 	struct pciide_softc *sc;
    523 	struct pci_attach_args *pa;
    524 {
    525 	pcireg_t csr;
    526 	const char *failreason = NULL;
    527 
    528 	/*
    529 	 * Check to see if something appears to be there.
    530 	 */
    531 	if (!wdcprobe(&sc->wdc_channels[chan])) {
    532 		failreason = "not responding; disabled or no drives?";
    533 		goto out;
    534 	}
    535 
    536 	/*
    537 	 * Now, make sure it's actually attributable to this PCI IDE
    538 	 * channel by trying to access the channel again while the
    539 	 * PCI IDE controller's I/O space is disabled.  (If the
    540 	 * channel no longer appears to be there, it belongs to
    541 	 * this controller.)  YUCK!
    542 	 */
    543 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    544 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    545 	    csr & ~PCI_COMMAND_IO_ENABLE);
    546 	if (wdcprobe(&sc->wdc_channels[chan]))
    547 		failreason = "other hardware responding at addresses";
    548 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    549 
    550 out:
    551 	return (failreason);
    552 }
    553 
    554 int
    555 pciide_map_channel_native(sc, pa, chan)
    556 	struct pciide_softc *sc;
    557 	struct pci_attach_args *pa;
    558 	int chan;
    559 {
    560 	struct pciide_channel *cp = &sc->pciide_channels[chan];
    561 	struct channel_softc *wdc_cp = &sc->wdc_channels[chan];
    562 	int rv = 1;
    563 
    564 	cp->compat = 0;
    565 
    566 	if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(chan), PCI_MAPREG_TYPE_IO,
    567 	    0, &wdc_cp->cmd_iot, &wdc_cp->cmd_ioh, NULL, NULL) != 0) {
    568 		printf("%s: couldn't map %s channel cmd regs\n",
    569 		    sc->sc_wdcdev.sc_dev.dv_xname,
    570 		    PCIIDE_CHANNEL_NAME(chan));
    571 		rv = 0;
    572 	}
    573 
    574 	if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(chan), PCI_MAPREG_TYPE_IO,
    575 	    0, &wdc_cp->ctl_iot, &wdc_cp->ctl_ioh, NULL, NULL) != 0) {
    576 		printf("%s: couldn't map %s channel ctl regs\n",
    577 		    sc->sc_wdcdev.sc_dev.dv_xname,
    578 		    PCIIDE_CHANNEL_NAME(chan));
    579 		rv = 0;
    580 	}
    581 
    582 	if ((cp->ih = sc->sc_pci_ih) == NULL) {
    583 		printf("%s: no native-PCI interrupt for use by %s channel\n",
    584 		    sc->sc_wdcdev.sc_dev.dv_xname,
    585 		    PCIIDE_CHANNEL_NAME(chan));
    586 		rv = 0;
    587 	}
    588 
    589 	return (rv);
    590 }
    591 
    592 int
    593 pciide_compat_intr(arg)
    594 	void *arg;
    595 {
    596 	struct channel_softc *wdc_cp = arg;
    597 
    598 #ifdef DIAGNOSTIC
    599 	struct pciide_softc *sc = (struct pciide_softc*)wdc_cp->wdc;
    600 	struct pciide_channel *cp = &sc->pciide_channels[wdc_cp->channel];
    601 	/* should only be called for a compat channel */
    602 	if (cp->compat == 0)
    603 		panic("pciide compat intr called for non-compat chan %p\n", cp);
    604 #endif
    605 	return (wdcintr(wdc_cp));
    606 }
    607 
    608 int
    609 pciide_pci_intr(arg)
    610 	void *arg;
    611 {
    612 	struct pciide_softc *sc = arg;
    613 	struct pciide_channel *cp;
    614 	struct channel_softc *wdc_cp;
    615 	int i, rv, crv;
    616 
    617 	rv = 0;
    618 	for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
    619 		cp = &sc->pciide_channels[i];
    620 		wdc_cp = &sc->wdc_channels[i];
    621 
    622 		/* If a compat channel skip. */
    623 		if (cp->compat)
    624 			continue;
    625 		/* if this channel not waiting for intr, skip */
    626 		if ((wdc_cp->ch_flags & WDCF_IRQ_WAIT) == 0)
    627 			continue;
    628 
    629 		crv = wdcintr(wdc_cp);
    630 		if (crv == 0)
    631 			;		/* leave rv alone */
    632 		else if (crv == 1)
    633 			rv = 1;		/* claim the intr */
    634 		else if (rv == 0)	/* crv should be -1 in this case */
    635 			rv = crv;	/* if we've done no better, take it */
    636 	}
    637 	return (rv);
    638 }
    639 
    640 void
    641 default_setup_cap(sc)
    642 	struct pciide_softc *sc;
    643 {
    644 	if (sc->sc_dma_ok)
    645 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    646 	sc->sc_wdcdev.pio_mode = 0;
    647 	sc->sc_wdcdev.dma_mode = 0;
    648 }
    649 
    650 void
    651 default_setup_chip(sc, pc, tag)
    652 	struct pciide_softc *sc;
    653 	pci_chipset_tag_t pc;
    654 	pcitag_t tag;
    655 {
    656 	int channel, drive, idedma_ctl;
    657 	struct channel_softc *chp;
    658 	struct ata_drive_datas *drvp;
    659 
    660 	if (sc->sc_dma_ok == 0)
    661 		return; /* nothing to do */
    662 
    663 	/* Allocate DMA maps */
    664 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    665 		idedma_ctl = 0;
    666 		chp = &sc->wdc_channels[channel];
    667 		for (drive = 0; drive < 2; drive++) {
    668 			drvp = &chp->ch_drive[drive];
    669 			/* If no drive, skip */
    670 			if ((drvp->drive_flags & DRIVE) == 0)
    671 				continue;
    672 			if (pciide_dma_table_setup(sc, channel, drive) != 0) {
    673 				/* Abort DMA setup */
    674 				printf("%s:%d:%d: can't allocate DMA maps, "
    675 				    "using PIO transferts\n",
    676 				    sc->sc_wdcdev.sc_dev.dv_xname,
    677 				    channel, drive);
    678 				drvp->drive_flags &= ~DRIVE_DMA;
    679 			}
    680 			printf("%s:%d:%d: using DMA mode %d\n",
    681 			    sc->sc_wdcdev.sc_dev.dv_xname,
    682 			    channel, drive,
    683 			    drvp->DMA_mode);
    684 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    685 		}
    686 		if (idedma_ctl != 0) {
    687 			/* Add software bits in status register */
    688 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    689 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    690 			    idedma_ctl);
    691 		}
    692 	}
    693 
    694 }
    695 
    696 void
    697 piix_setup_cap(sc)
    698 	struct pciide_softc *sc;
    699 {
    700 #if 0
    701 	if (sc->sc_pp->ide_product == PCI_PRODUCT_INTEL_82371AB_IDE)
    702 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
    703 #endif
    704 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32 | WDC_CAPABILITY_PIO |
    705 	    WDC_CAPABILITY_DMA;
    706 	sc->sc_wdcdev.pio_mode = 4;
    707 	sc->sc_wdcdev.dma_mode = 2;
    708 }
    709 
    710 void
    711 piix_setup_chip(sc, pc, tag)
    712 	struct pciide_softc *sc;
    713 	pci_chipset_tag_t pc;
    714 	pcitag_t tag;
    715 {
    716 	struct channel_softc *chp;
    717 	u_int8_t mode[2];
    718 	u_int8_t channel, drive;
    719 	u_int32_t idetim, sidetim, idedma_ctl;
    720 	struct ata_drive_datas *drvp;
    721 
    722 	idetim = sidetim = 0;
    723 
    724 	WDCDEBUG_PRINT(("piix_setup_chip: old idetim=0x%x, sidetim=0x%x\n",
    725 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    726 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    727 
    728 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    729 		chp = &sc->wdc_channels[channel];
    730 		drvp = chp->ch_drive;
    731 		idedma_ctl = 0;
    732 		/* Enable IDE registers decode */
    733 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    734 		    channel);
    735 
    736 		/* setup DMA if needed */
    737 		for (drive = 0; drive < 2; drive++) {
    738 			if (drvp[drive].drive_flags & DRIVE_DMA &&
    739 			    pciide_dma_table_setup(sc, channel, drive) != 0) {
    740 				drvp[drive].drive_flags &= ~DRIVE_DMA;
    741 			}
    742 		}
    743 
    744 		/*
    745 		 * Here we have to mess up with drives mode: PIIX can't have
    746 		 * different timings for master and slave drives.
    747 		 * We need to find the best combination.
    748 		 */
    749 
    750 		/* If both drives supports DMA, takes the lower mode */
    751 		if ((drvp[0].drive_flags & DRIVE_DMA) &&
    752 		    (drvp[1].drive_flags & DRIVE_DMA)) {
    753 			mode[0] = mode[1] =
    754 			    min(drvp[0].DMA_mode, drvp[1].DMA_mode);
    755 			    drvp[0].DMA_mode = mode[0];
    756 			goto ok;
    757 		}
    758 		/*
    759 		 * If only one drive supports DMA, use its mode, and
    760 		 * put the other one in PIO mode 0 if mode not compatible
    761 		 */
    762 		if (drvp[0].drive_flags & DRIVE_DMA) {
    763 			mode[0] = drvp[0].DMA_mode;
    764 			mode[1] = drvp[1].PIO_mode;
    765 			if (piix_isp_pio[mode[1]] != piix_isp_dma[mode[0]] ||
    766 			    piix_rtc_pio[mode[1]] != piix_rtc_dma[mode[0]])
    767 				mode[1] = 0;
    768 			goto ok;
    769 		}
    770 		if (drvp[1].drive_flags & DRIVE_DMA) {
    771 			mode[1] = drvp[1].DMA_mode;
    772 			mode[0] = drvp[0].PIO_mode;
    773 			if (piix_isp_pio[mode[0]] != piix_isp_dma[mode[1]] ||
    774 			    piix_rtc_pio[mode[0]] != piix_rtc_dma[mode[1]])
    775 				mode[0] = 0;
    776 			goto ok;
    777 		}
    778 		/*
    779 		 * If both drives are not DMA, takes the lower mode, unless
    780 		 * one of them is PIO mode < 2
    781 		 */
    782 		if (drvp[0].PIO_mode < 2) {
    783 			mode[0] = 0;
    784 			mode[1] = drvp[1].PIO_mode;
    785 		} else if (drvp[1].PIO_mode < 2) {
    786 			mode[1] = 0;
    787 			mode[0] = drvp[0].PIO_mode;
    788 		} else {
    789 			mode[0] = mode[1] =
    790 			    min(drvp[1].PIO_mode, drvp[0].PIO_mode);
    791 		}
    792 ok:		/* The modes are setup */
    793 		for (drive = 0; drive < 2; drive++) {
    794 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    795 				drvp[drive].DMA_mode = mode[drive];
    796 				idetim |= piix_setup_idetim_timings(
    797 				    mode[drive], 1, channel);
    798 				goto end;
    799 			} else
    800 				drvp[drive].PIO_mode = mode[drive];
    801 		}
    802 		/* If we are there, none of the drives are DMA */
    803 		if (mode[0] >= 2)
    804 			idetim |= piix_setup_idetim_timings(
    805 			    mode[0], 0, channel);
    806 		else
    807 			idetim |= piix_setup_idetim_timings(
    808 			    mode[1], 0, channel);
    809 end:		/*
    810 		 * timing mode is now set up in the controller. Enable
    811 		 * it per-drive
    812 		 */
    813 		for (drive = 0; drive < 2; drive++) {
    814 			/* If no drive, skip */
    815 			if ((drvp[drive].drive_flags & DRIVE) == 0)
    816 				continue;
    817 			idetim |= piix_setup_idetim_drvs(&drvp[drive]);
    818 			printf("%s:%d:%d: using PIO mode %d",
    819 			    sc->sc_wdcdev.sc_dev.dv_xname,
    820 			    channel, drive, drvp[drive].PIO_mode);
    821 			if (drvp[drive].drive_flags & DRIVE_DMA) {
    822 				idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    823 				printf(", DMA mode %d", drvp[drive].DMA_mode);
    824 			}
    825 			printf("\n");
    826 		}
    827 		if (idedma_ctl != 0) {
    828 			/* Add software bits in status register */
    829 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    830 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    831 			    idedma_ctl);
    832 		}
    833 	}
    834 	WDCDEBUG_PRINT(("piix_setup_chip: idetim=0x%x, sidetim=0x%x\n",
    835 	    idetim, sidetim), DEBUG_PROBE);
    836 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    837 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    838 }
    839 
    840 void
    841 piix3_4_setup_chip(sc, pc, tag)
    842 	struct pciide_softc *sc;
    843 	pci_chipset_tag_t pc;
    844 	pcitag_t tag;
    845 {
    846 	int channel, drive;
    847 	struct channel_softc *chp;
    848 	struct ata_drive_datas *drvp;
    849 	u_int32_t idetim, sidetim, udmareg, idedma_ctl;
    850 
    851 	idetim = sidetim = udmareg = 0;
    852 
    853 	WDCDEBUG_PRINT(("piix3_4_setup_chip: old idetim=0x%x, sidetim=0x%x",
    854 	    pci_conf_read(pc, tag, PIIX_IDETIM),
    855 	    pci_conf_read(pc, tag, PIIX_SIDETIM)), DEBUG_PROBE);
    856 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
    857 		WDCDEBUG_PRINT((", udamreg 0x%x",
    858 		    pci_conf_read(pc, tag, PIIX_UDMAREG)),
    859 		    DEBUG_PROBE);
    860 	}
    861 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    862 
    863 	for (channel = 0; channel < PCIIDE_NUM_CHANNELS; channel++) {
    864 		chp = &sc->wdc_channels[channel];
    865 		idedma_ctl = 0;
    866 		/* Enable IDE registers decode */
    867 		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE,
    868 		    channel);
    869 		for (drive = 0; drive < 2; drive++) {
    870 			drvp = &chp->ch_drive[drive];
    871 			/* If no drive, skip */
    872 			if ((drvp->drive_flags & DRIVE) == 0)
    873 				continue;
    874 			/* add timing values, setup DMA if needed */
    875 			if (((drvp->drive_flags & DRIVE_DMA) == 0 &&
    876 			    (drvp->drive_flags & DRIVE_UDMA) == 0) ||
    877 			    sc->sc_dma_ok == 0)
    878 				goto pio;
    879 			if (pciide_dma_table_setup(sc, channel, drive) != 0)
    880 			    goto pio; /* Abort DMA setup */
    881 			if ((chp->wdc->cap & WDC_CAPABILITY_UDMA) &&
    882 			    (drvp->drive_flags & DRIVE_UDMA)) {
    883 				/* use Ultra/DMA */
    884 				drvp->drive_flags &= ~DRIVE_DMA;
    885 				udmareg |= PIIX_UDMACTL_DRV_EN(
    886 				    channel, drive);
    887 				udmareg |= PIIX_UDMATIM_SET(
    888 				    piix4_sct_udma[drvp->UDMA_mode],
    889 				    channel, drive);
    890 			} else {
    891 				/* use Multiword DMA */
    892 				drvp->drive_flags &= ~DRIVE_UDMA;
    893 				if (drive == 0) {
    894 					idetim |= piix_setup_idetim_timings(
    895 					    drvp->DMA_mode, 1, channel);
    896 				} else {
    897 					sidetim |= piix_setup_sidetim_timings(
    898 						drvp->DMA_mode, 1, channel);
    899 					idetim =PIIX_IDETIM_SET(idetim,
    900 					    PIIX_IDETIM_SITRE, channel);
    901 				}
    902 			}
    903 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
    904 			goto end;
    905 
    906 pio:			/* use PIO mode */
    907 			drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    908 			if (drive == 0) {
    909 				idetim |= piix_setup_idetim_timings(
    910 				    drvp->PIO_mode, 0, channel);
    911 			} else {
    912 				sidetim |= piix_setup_sidetim_timings(
    913 					drvp->PIO_mode, 0, channel);
    914 				idetim =PIIX_IDETIM_SET(idetim,
    915 				    PIIX_IDETIM_SITRE, channel);
    916 			}
    917 end:			idetim |= piix_setup_idetim_drvs(&drvp[drive]);
    918 			printf("%s:%d:%d: using PIO mode %d",
    919 			    sc->sc_wdcdev.sc_dev.dv_xname,
    920 			    channel, drive, drvp[drive].PIO_mode);
    921 			if (drvp[drive].drive_flags & DRIVE_DMA)
    922 			    printf(", DMA mode %d", drvp[drive].DMA_mode);
    923 			if (drvp[drive].drive_flags & DRIVE_UDMA)
    924 			    printf(", UDMA mode %d", drvp->UDMA_mode);
    925 			printf("\n");
    926 		}
    927 		if (idedma_ctl != 0) {
    928 			/* Add software bits in status register */
    929 			bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
    930 			    IDEDMA_CTL + (IDEDMA_SCH_OFFSET * channel),
    931 			    idedma_ctl);
    932 		}
    933 	}
    934 
    935 	WDCDEBUG_PRINT(("piix3_4_setup_chip: idetim=0x%x, sidetim=0x%x",
    936 	    idetim, sidetim), DEBUG_PROBE);
    937 	if (sc->sc_wdcdev.cap & WDC_CAPABILITY_UDMA) {
    938 		WDCDEBUG_PRINT((", udmareg=0x%x", udmareg), DEBUG_PROBE);
    939 		pci_conf_write(pc, tag, PIIX_UDMAREG, udmareg);
    940 	}
    941 	WDCDEBUG_PRINT(("\n"), DEBUG_PROBE);
    942 	pci_conf_write(pc, tag, PIIX_IDETIM, idetim);
    943 	pci_conf_write(pc, tag, PIIX_SIDETIM, sidetim);
    944 }
    945 
    946 /* setup ISP and RTC fields, based on mode */
    947 static u_int32_t
    948 piix_setup_idetim_timings(mode, dma, channel)
    949 	u_int8_t mode;
    950 	u_int8_t dma;
    951 	u_int8_t channel;
    952 {
    953 
    954 	if (dma)
    955 		return PIIX_IDETIM_SET(0,
    956 		    PIIX_IDETIM_ISP_SET(piix_isp_dma[mode]) |
    957 		    PIIX_IDETIM_RTC_SET(piix_rtc_dma[mode]),
    958 		    channel);
    959 	else
    960 		return PIIX_IDETIM_SET(0,
    961 		    PIIX_IDETIM_ISP_SET(piix_isp_pio[mode]) |
    962 		    PIIX_IDETIM_RTC_SET(piix_rtc_pio[mode]),
    963 		    channel);
    964 }
    965 
    966 /* setup DTE, PPE, IE and TIME field based on PIO mode */
    967 static u_int32_t
    968 piix_setup_idetim_drvs(drvp)
    969 	struct ata_drive_datas *drvp;
    970 {
    971 	u_int32_t ret = 0;
    972 	struct channel_softc *chp = drvp->chnl_softc;
    973 	u_int8_t channel = chp->channel;
    974 	u_int8_t drive = drvp->drive;
    975 
    976 	/*
    977 	 * If drive is using UDMA, timings setups are independant
    978 	 * So just check DMA and PIO here.
    979 	 */
    980 	if (drvp->drive_flags & DRIVE_DMA) {
    981 		/* if mode = DMA mode 0, use compatible timings */
    982 		if ((drvp->drive_flags & DRIVE_DMA) &&
    983 		    drvp->DMA_mode == 0) {
    984 			drvp->PIO_mode = 0;
    985 			return ret;
    986 		}
    987 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
    988 		/*
    989 		 * PIO and DMA timings are the same, use fast timings for PIO
    990 		 * too, else use compat timings.
    991 		 */
    992 		if ((piix_isp_pio[drvp->PIO_mode] !=
    993 		    piix_isp_dma[drvp->DMA_mode]) ||
    994 		    (piix_rtc_pio[drvp->PIO_mode] !=
    995 		    piix_rtc_dma[drvp->DMA_mode]))
    996 			drvp->PIO_mode = 0;
    997 		/* if PIO mode <= 2, use compat timings for PIO */
    998 		if (drvp->PIO_mode <= 2) {
    999 			ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_DTE(drive),
   1000 			    channel);
   1001 			return ret;
   1002 		}
   1003 	}
   1004 
   1005 	/*
   1006 	 * Now setup PIO modes. If mode < 2, use compat timings.
   1007 	 * Else enable fast timings. Enable IORDY and prefetch/post
   1008 	 * if PIO mode >= 3.
   1009 	 */
   1010 
   1011 	if (drvp->PIO_mode < 2)
   1012 		return ret;
   1013 
   1014 	ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_TIME(drive), channel);
   1015 	if (drvp->PIO_mode >= 3) {
   1016 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_IE(drive), channel);
   1017 		ret = PIIX_IDETIM_SET(ret, PIIX_IDETIM_PPE(drive), channel);
   1018 	}
   1019 	return ret;
   1020 }
   1021 
   1022 /* setup values in SIDETIM registers, based on mode */
   1023 static u_int32_t
   1024 piix_setup_sidetim_timings(mode, dma, channel)
   1025 	u_int8_t mode;
   1026 	u_int8_t dma;
   1027 	u_int8_t channel;
   1028 {
   1029 	if (dma)
   1030 		return PIIX_SIDETIM_ISP_SET(piix_isp_dma[mode], channel) |
   1031 		    PIIX_SIDETIM_RTC_SET(piix_rtc_dma[mode], channel);
   1032 	else
   1033 		return PIIX_SIDETIM_ISP_SET(piix_isp_pio[mode], channel) |
   1034 		    PIIX_SIDETIM_RTC_SET(piix_rtc_pio[mode], channel);
   1035 }
   1036 
   1037 
   1038 
   1039 int
   1040 pciide_dma_table_setup(sc, channel, drive)
   1041 	struct pciide_softc *sc;
   1042 	int channel, drive;
   1043 {
   1044 	bus_dma_segment_t seg;
   1045 	int error, rseg;
   1046 	const bus_size_t dma_table_size =
   1047 	    sizeof(struct idedma_table) * NIDEDMA_TABLES;
   1048 	struct pciide_dma_maps *dma_maps =
   1049 	    &sc->pciide_channels[channel].dma_maps[drive];
   1050 
   1051 	/* Allocate memory for the DMA tables and map it */
   1052 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
   1053 	    IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &seg, 1, &rseg,
   1054 	    BUS_DMA_NOWAIT)) != 0) {
   1055 		printf("%s:%d: unable to allocate table DMA for"
   1056 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1057 		    channel, drive, error);
   1058 		return error;
   1059 	}
   1060 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1061 	    dma_table_size,
   1062 	    (caddr_t *)&dma_maps->dma_table,
   1063 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1064 		printf("%s:%d: unable to map table DMA for"
   1065 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1066 		    channel, drive, error);
   1067 		return error;
   1068 	}
   1069 	WDCDEBUG_PRINT(("pciide_dma_table_setup: table at %p len %ld, "
   1070 	    "phy 0x%lx\n", dma_maps->dma_table, dma_table_size,
   1071 	    seg.ds_addr), DEBUG_PROBE);
   1072 
   1073 	/* Create and load table DMA map for this disk */
   1074 	if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
   1075 	    1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
   1076 	    &dma_maps->dmamap_table)) != 0) {
   1077 		printf("%s:%d: unable to create table DMA map for"
   1078 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1079 		    channel, drive, error);
   1080 		return error;
   1081 	}
   1082 	if ((error = bus_dmamap_load(sc->sc_dmat,
   1083 	    dma_maps->dmamap_table,
   1084 	    dma_maps->dma_table,
   1085 	    dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
   1086 		printf("%s:%d: unable to load table DMA map for"
   1087 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1088 		    channel, drive, error);
   1089 		return error;
   1090 	}
   1091 	WDCDEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
   1092 	    dma_maps->dmamap_table->dm_segs[0].ds_addr), DEBUG_PROBE);
   1093 	/* Create a xfer DMA map for this drive */
   1094 	if ((error = bus_dmamap_create(sc->sc_dmat, IDEDMA_BYTE_COUNT_MAX,
   1095 	    NIDEDMA_TABLES, IDEDMA_BYTE_COUNT_MAX, IDEDMA_BYTE_COUNT_ALIGN,
   1096 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1097 	    &dma_maps->dmamap_xfer)) != 0) {
   1098 		printf("%s:%d: unable to create xfer DMA map for"
   1099 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1100 		    channel, drive, error);
   1101 		return error;
   1102 	}
   1103 	return 0;
   1104 }
   1105 
   1106 int
   1107 pciide_dma_init(v, channel, drive, databuf, datalen, read)
   1108 	void *v;
   1109 	int channel, drive;
   1110 	void *databuf;
   1111 	size_t datalen;
   1112 	int read;
   1113 {
   1114 	struct pciide_softc *sc = v;
   1115 	int error, seg;
   1116 	struct pciide_dma_maps *dma_maps =
   1117 	    &sc->pciide_channels[channel].dma_maps[drive];
   1118 
   1119 	error = bus_dmamap_load(sc->sc_dmat,
   1120 	    dma_maps->dmamap_xfer,
   1121 	    databuf, datalen, NULL, BUS_DMA_NOWAIT);
   1122 	if (error) {
   1123 		printf("%s:%d: unable to load xfer DMA map for"
   1124 		    "drive %d, error=%d\n", sc->sc_wdcdev.sc_dev.dv_xname,
   1125 		    channel, drive, error);
   1126 		return error;
   1127 	}
   1128 
   1129 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1130 	    dma_maps->dmamap_xfer->dm_mapsize,
   1131 	    (read) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1132 
   1133 	WDCDEBUG_PRINT(("pciide_dma_init: %d segs for %p len %d (phy 0x%x)\n",
   1134 	    dma_maps->dmamap_xfer->dm_nsegs, databuf, datalen,
   1135 	    vtophys(databuf)), DEBUG_DMA|DEBUG_XFERS);
   1136 	for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
   1137 #ifdef DIAGNOSTIC
   1138 		/* A segment must not cross a 64k boundary */
   1139 		{
   1140 		u_long phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1141 		u_long len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
   1142 		if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
   1143 		    ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
   1144 			printf("pciide_dma: segment %d physical addr 0x%lx"
   1145 			    " len 0x%lx not properly aligned\n",
   1146 			    seg, phys, len);
   1147 			panic("pciide_dma: buf align");
   1148 		}
   1149 		}
   1150 #endif
   1151 		dma_maps->dma_table[seg].base_addr =
   1152 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
   1153 		dma_maps->dma_table[seg].byte_count =
   1154 		    dma_maps->dmamap_xfer->dm_segs[seg].ds_len &
   1155 		    IDEDMA_BYTE_COUNT_MASK;
   1156 		WDCDEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
   1157 		   seg, dma_maps->dma_table[seg].byte_count,
   1158 		   dma_maps->dma_table[seg].base_addr), DEBUG_DMA);
   1159 
   1160 	}
   1161 	dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
   1162 		IDEDMA_BYTE_COUNT_EOT;
   1163 
   1164 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
   1165 	    dma_maps->dmamap_table->dm_mapsize,
   1166 	    BUS_DMASYNC_PREWRITE);
   1167 
   1168 	/* Maps are ready. Start DMA function */
   1169 #ifdef DIAGNOSTIC
   1170 	if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
   1171 		printf("pciide_dma_init: addr 0x%lx not properly aligned\n",
   1172 		    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1173 		panic("pciide_dma_init: table align");
   1174 	}
   1175 #endif
   1176 
   1177 	WDCDEBUG_PRINT(("phy addr of table at %p = 0x%lx len %ld (%d segs, "
   1178 	    "phys 0x%x)\n",
   1179 	    dma_maps->dma_table,
   1180 	    dma_maps->dmamap_table->dm_segs[0].ds_addr,
   1181 	    dma_maps->dmamap_table->dm_segs[0].ds_len,
   1182 	    dma_maps->dmamap_table->dm_nsegs,
   1183 	    vtophys(dma_maps->dma_table)), DEBUG_DMA);
   1184 	/* Clear status bits */
   1185 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1186 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1187 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1188 		IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel));
   1189 	/* Write table addr */
   1190 	bus_space_write_4(sc->sc_dma_iot, sc->sc_dma_ioh,
   1191 	    IDEDMA_TBL + IDEDMA_SCH_OFFSET * channel,
   1192 	    dma_maps->dmamap_table->dm_segs[0].ds_addr);
   1193 	/* set read/write */
   1194 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1195 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1196 	    (read) ? IDEDMA_CMD_WRITE: 0);
   1197 	return 0;
   1198 }
   1199 
   1200 void
   1201 pciide_dma_start(v, channel, drive, read)
   1202 	void *v;
   1203 	int channel, drive;
   1204 {
   1205 	struct pciide_softc *sc = v;
   1206 
   1207 	WDCDEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
   1208 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1209 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1210 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1211 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) | IDEDMA_CMD_START);
   1212 }
   1213 
   1214 int
   1215 pciide_dma_finish(v, channel, drive, read)
   1216 	void *v;
   1217 	int channel, drive;
   1218 	int read;
   1219 {
   1220 	struct pciide_softc *sc = v;
   1221 	u_int8_t status;
   1222 	struct pciide_dma_maps *dma_maps =
   1223 	    &sc->pciide_channels[channel].dma_maps[drive];
   1224 
   1225 	/* Unload the map of the data buffer */
   1226 	bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
   1227 	    dma_maps->dmamap_xfer->dm_mapsize,
   1228 	    (read) ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1229 	bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
   1230 
   1231 	status = bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1232 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel);
   1233 	WDCDEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
   1234 	    DEBUG_XFERS);
   1235 
   1236 	/* stop DMA channel */
   1237 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1238 	    IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel,
   1239 	    bus_space_read_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1240 		IDEDMA_CMD + IDEDMA_SCH_OFFSET * channel) & ~IDEDMA_CMD_START);
   1241 
   1242 	/* Clear status bits */
   1243 	bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
   1244 	    IDEDMA_CTL + IDEDMA_SCH_OFFSET * channel,
   1245 	    status);
   1246 
   1247 	if ((status & (IDEDMA_CTL_INTR | IDEDMA_CTL_ERR | IDEDMA_CTL_ACT)) !=
   1248 	    IDEDMA_CTL_INTR) {
   1249 		printf("%s:%d:%d: Bus-Master DMA error: status=0x%x\n",
   1250 		    sc->sc_wdcdev.sc_dev.dv_xname, channel, drive, status);
   1251 		return 1;
   1252 	}
   1253 	return 0;
   1254 }
   1255