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