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