xref: /freebsd/sys/dev/ata/ata-all.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*-
2  * Copyright (c) 1998,1999,2000 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
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. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include "ata.h"
32 #include "isa.h"
33 #include "card.h"
34 #include "pci.h"
35 #include "atadisk.h"
36 #include "atapicd.h"
37 #include "atapifd.h"
38 #include "atapist.h"
39 #include "opt_global.h"
40 #include "opt_ata.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/disk.h>
45 #include <sys/module.h>
46 #include <sys/bus.h>
47 #include <sys/buf.h>
48 #include <sys/malloc.h>
49 #include <sys/devicestat.h>
50 #include <sys/sysctl.h>
51 #include <machine/stdarg.h>
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <machine/resource.h>
55 #include <machine/bus.h>
56 #include <sys/rman.h>
57 #if NPCI > 0
58 #include <pci/pcivar.h>
59 #include <pci/pcireg.h>
60 #endif
61 #include <isa/isavar.h>
62 #include <isa/isareg.h>
63 #include <machine/clock.h>
64 #ifdef __i386__
65 #include <machine/smp.h>
66 #include <i386/isa/intr_machdep.h>
67 #endif
68 #ifdef __alpha__
69 #include <machine/md_var.h>
70 #endif
71 #include <dev/ata/ata-all.h>
72 #include <dev/ata/ata-disk.h>
73 #include <dev/ata/atapi-all.h>
74 
75 /* misc defines */
76 #define IOMASK			0xfffffffc
77 #define ATA_IOADDR_RID		0
78 #define ATA_ALTADDR_RID		1
79 #define ATA_BMADDR_RID		2
80 
81 /* prototypes */
82 static int ata_probe(device_t);
83 static int ata_attach(device_t);
84 static int ata_detach(device_t);
85 static int ata_resume(device_t);
86 static void ata_boot_attach(void);
87 static void ata_intr(void *);
88 static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
89 static int8_t *active2str(int32_t);
90 static void bswap(int8_t *, int32_t);
91 static void btrim(int8_t *, int32_t);
92 static void bpack(int8_t *, int8_t *, int32_t);
93 
94 /* local vars */
95 static devclass_t ata_devclass;
96 static devclass_t ata_pci_devclass;
97 static struct intr_config_hook *ata_delayed_attach = NULL;
98 static char ata_conf[256];
99 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
100 
101 #if NISA > 0
102 static struct isa_pnp_id ata_ids[] = {
103     {0x0006d041,	"Generic ESDI/IDE/ATA controller"},	/* PNP0600 */
104     {0x0106d041,	"Plus Hardcard II"},			/* PNP0601 */
105     {0x0206d041,	"Plus Hardcard IIXL/EZ"},		/* PNP0602 */
106     {0x0306d041,	"Generic ATA"},				/* PNP0603 */
107     {0}
108 };
109 
110 static int
111 ata_isa_probe(device_t dev)
112 {
113     struct ata_softc *scp = device_get_softc(dev);
114     struct resource *port;
115     int rid;
116     u_long tmp;
117 
118     /* check isapnp ids */
119     if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
120 	return ENXIO;
121 
122     /* allocate the port range */
123     rid = ATA_IOADDR_RID;
124     port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 			      ATA_IOSIZE, RF_ACTIVE);
126     if (!port)
127 	return ENOMEM;
128 
129     /* alloctate the altport range */
130     if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
131 	bus_set_resource(dev, SYS_RES_IOPORT, 1,
132 			 rman_get_start(port) + ATA_ALTPORT,
133 			 ATA_ALTIOSIZE);
134     }
135     bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
136     scp->unit = device_get_unit(dev);
137     scp->flags |= ATA_USE_16BIT;
138     return ata_probe(dev);
139 }
140 
141 static device_method_t ata_isa_methods[] = {
142     /* device interface */
143     DEVMETHOD(device_probe,	ata_isa_probe),
144     DEVMETHOD(device_attach,	ata_attach),
145     DEVMETHOD(device_resume,	ata_resume),
146     { 0, 0 }
147 };
148 
149 static driver_t ata_isa_driver = {
150     "ata",
151     ata_isa_methods,
152     sizeof(struct ata_softc),
153 };
154 
155 DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
156 #endif
157 
158 #if NCARD > 0
159 static int
160 ata_pccard_probe(device_t dev)
161 {
162     struct ata_softc *scp = device_get_softc(dev);
163     struct resource *port;
164     int rid, len;
165 
166     /* allocate the port range */
167     rid = ATA_IOADDR_RID;
168     len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
169     port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE);
170     if (!port)
171 	return ENOMEM;
172 
173     /*
174      * if we got more than the default ATA_IOSIZE ports, this is likely
175      * a pccard system where the altio ports are located just after the
176      * normal io ports, so no need to allocate them.
177      */
178     if (len <= ATA_IOSIZE) {
179 	bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
180 			 rman_get_start(port) + ATA_ALTPORT, ATA_ALTIOSIZE);
181     }
182     bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
183     scp->unit = device_get_unit(dev);
184     scp->flags |= ATA_USE_16BIT;
185     return ata_probe(dev);
186 }
187 
188 static device_method_t ata_pccard_methods[] = {
189     /* device interface */
190     DEVMETHOD(device_probe,	ata_pccard_probe),
191     DEVMETHOD(device_attach,	ata_attach),
192     DEVMETHOD(device_detach,	ata_detach),
193     { 0, 0 }
194 };
195 
196 static driver_t ata_pccard_driver = {
197     "ata",
198     ata_pccard_methods,
199     sizeof(struct ata_softc),
200 };
201 
202 DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
203 #endif
204 
205 #if NPCI > 0
206 struct ata_pci_softc {
207     struct resource *bmio;
208     struct resource bmio_1;
209     struct resource bmio_2;
210     struct resource *irq;
211     int32_t irqcnt;
212 };
213 
214 static int32_t
215 ata_find_dev(device_t dev, int32_t type)
216 {
217     device_t *children, child;
218     int nchildren, i;
219 
220     if (device_get_children(device_get_parent(dev), &children, &nchildren))
221 	return 0;
222 
223     for (i = 0; i < nchildren; i++) {
224 	child = children[i];
225 
226 	/* check that it's on the same silicon and the device we want */
227 	if (pci_get_slot(dev) == pci_get_slot(child) &&
228 	    pci_get_vendor(child) == (type & 0xffff) &&
229 	    pci_get_device(child) == ((type & 0xffff0000) >> 16)) {
230 	    free(children, M_TEMP);
231 	    return 1;
232 	}
233     }
234     free(children, M_TEMP);
235     return 0;
236 }
237 
238 static const char *
239 ata_pci_match(device_t dev)
240 {
241     if (pci_get_class(dev) != PCIC_STORAGE)
242 	return NULL;
243 
244     switch (pci_get_devid(dev)) {
245     /* supported chipsets */
246     case 0x12308086:
247 	return "Intel PIIX ATA controller";
248 
249     case 0x70108086:
250 	return "Intel PIIX3 ATA controller";
251 
252     case 0x71118086:
253     case 0x71998086:
254 	return "Intel PIIX4 ATA33 controller";
255 
256     case 0x24118086:
257 	return "Intel ICH ATA66 controller";
258 
259     case 0x24218086:
260 	return "Intel ICH0 ATA33 controller";
261 
262     case 0x522910b9:
263 	return "AcerLabs Aladdin ATA33 controller";
264 
265     case 0x05711106:
266 	if (ata_find_dev(dev, 0x05861106))
267 	    return "VIA 82C586 ATA33 controller";
268 	if (ata_find_dev(dev, 0x05961106))
269 	    return "VIA 82C596 ATA33 controller";
270 	if (ata_find_dev(dev, 0x06861106))
271 	    return "VIA 82C686 ATA66 controller";
272 	return "VIA Apollo ATA controller";
273 
274     case 0x55131039:
275 	return "SiS 5591 ATA33 controller";
276 
277     case 0x06461095:
278 	return "CMD 646 ATA controller";
279 
280     case 0xc6931080:
281 	if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
282 	    return "Cypress 82C693 ATA controller";
283 	break;
284 
285     case 0x74091022:
286 	return "AMD 756 ATA66 controller";
287 
288     case 0x4d33105a:
289 	return "Promise ATA33 controller";
290 
291     case 0x4d38105a:
292 	return "Promise ATA66 controller";
293 
294     case 0x00041103:
295 	return "HighPoint HPT366 ATA66 controller";
296 
297    /* unsupported but known chipsets, generic DMA only */
298     case 0x10001042:
299     case 0x10011042:
300 	return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
301 
302     case 0x06401095:
303 	return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
304 
305     case 0x01021078:
306 	return "Cyrix 5530 ATA controller (generic mode)";
307 
308     /* unknown chipsets, try generic DMA if it seems possible */
309     default:
310 	if (pci_get_class(dev) == PCIC_STORAGE &&
311 	    (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
312 	    return "Unknown PCI ATA controller (generic mode)";
313     }
314     return NULL;
315 }
316 
317 static int
318 ata_pci_probe(device_t dev)
319 {
320     const char *desc = ata_pci_match(dev);
321 
322     if (desc) {
323 	device_set_desc(dev, desc);
324 	return 0;
325     }
326     else
327 	return ENXIO;
328 }
329 
330 static int
331 ata_pci_add_child(device_t dev, int unit)
332 {
333     device_t child;
334 
335     /* check if this is located at one of the std addresses */
336     if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
337 	if (!(child = device_add_child(dev, "ata", unit)))
338 	    return ENOMEM;
339     }
340     else {
341 	if (!(child = device_add_child(dev, "ata", 2)))
342 	    return ENOMEM;
343     }
344     device_set_ivars(child, (void *)(uintptr_t) unit);
345     return 0;
346 }
347 
348 static int
349 ata_pci_attach(device_t dev)
350 {
351     struct ata_pci_softc *sc = device_get_softc(dev);
352     u_int8_t class, subclass;
353     u_int32_t type, cmd;
354     int rid;
355 
356     /* set up vendor-specific stuff */
357     type = pci_get_devid(dev);
358     class = pci_get_class(dev);
359     subclass = pci_get_subclass(dev);
360     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
361 
362     /* is this controller busmaster DMA capable ? */
363     if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
364 	/* is busmastering support turned on ? */
365 	if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
366 	    (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
367 
368 	    /* is there a valid port range to connect to ? */
369 	    rid = 0x20;
370 	    sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
371 					  0, ~0, 1, RF_ACTIVE);
372 	    if (!sc->bmio)
373 		device_printf(dev, "Busmastering DMA not configured\n");
374 	}
375 	else
376 	    device_printf(dev, "Busmastering DMA not enabled\n");
377     }
378     else {
379     	if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
380 	    /* Promise and HPT366 controllers support busmastering DMA */
381 	    rid = 0x20;
382 	    sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
383 					  0, ~0, 1, RF_ACTIVE);
384 	}
385 	else
386 	    /* we dont know this controller, no busmastering DMA */
387 	    device_printf(dev, "Busmastering DMA not supported\n");
388     }
389 
390     /* do extra chipset specific setups */
391     switch (type) {
392     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
393 	pci_write_config(dev, 0x53,
394 			 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
395 	break;
396 
397     case 0x4d38105a: /* Promise 66's need their clock changed */
398 	outb(rman_get_start(sc->bmio) + 0x11,
399 	     inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
400 	/* FALLTHROUGH */
401 
402     case 0x4d33105a: /* Promise's need burst mode to be turned on */
403 	outb(rman_get_start(sc->bmio) + 0x1f,
404 	     inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
405 	break;
406 
407     case 0x00041103: /* HPT366 turn of fast interrupt prediction */
408 	pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
409 	break;
410 
411     case 0x05711106:
412     case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */
413 	/* set prefetch, postwrite */
414 	pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
415 
416 	/* set fifo configuration half'n'half */
417 	pci_write_config(dev, 0x43,
418 			 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
419 
420 	/* set status register read retry */
421 	pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
422 
423 	/* set DMA read & end-of-sector fifo flush */
424 	pci_write_config(dev, 0x46,
425 			 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
426 
427 	/* set sector size */
428 	pci_write_config(dev, 0x60, DEV_BSIZE, 2);
429 	pci_write_config(dev, 0x68, DEV_BSIZE, 2);
430 
431 	/* prepare for ATA-66 on the 82C686 */
432 	if (ata_find_dev(dev, 0x06861106)) {
433 	    pci_write_config(dev, 0x50,
434 			     pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
435 	}
436 	break;
437     }
438 
439     /*
440      * the Cypress chip is a mess, it contains two ATA functions, but
441      * both channels are visible on the first one.
442      * simply ignore the second function for now, as the right
443      * solution (ignoring the second channel on the first function)
444      * doesn't work with the crappy ATA interrupt setup on the alpha.
445      */
446     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
447 	return 0;
448 
449     ata_pci_add_child(dev, 0);
450 
451     if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV ||
452 	pci_read_config(dev, 0x18, 4) & IOMASK)
453 	ata_pci_add_child(dev, 1);
454 
455     return bus_generic_attach(dev);
456 }
457 
458 static int
459 ata_pci_print_child(device_t dev, device_t child)
460 {
461     struct ata_softc *scp = device_get_softc(child);
462     int unit = (uintptr_t) device_get_ivars(child);
463     int retval = 0;
464 
465     retval += bus_print_child_header(dev, child);
466     retval += printf(": at 0x%x", scp->ioaddr);
467 
468     if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
469 	retval += printf(" irq %d", 14 + unit);
470 
471     retval += bus_print_child_footer(dev, child);
472 
473     return retval;
474 }
475 
476 static struct resource *
477 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
478 		       u_long start, u_long end, u_long count, u_int flags)
479 {
480     struct ata_pci_softc *sc = device_get_softc(dev);
481     int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
482     int unit = (int)device_get_ivars(child);
483     int myrid;
484 
485     if (type == SYS_RES_IOPORT) {
486 	switch (*rid) {
487 	case ATA_IOADDR_RID:
488 	    if (masterdev) {
489 		myrid = 0;
490 		start = (unit == 0 ? IO_WD1 : IO_WD2);
491 		end = start + ATA_IOSIZE - 1;
492 		count = ATA_IOSIZE;
493 	    }
494 	    else
495 		myrid = 0x10 + 8 * unit;
496 	    break;
497 
498 	case ATA_ALTADDR_RID:
499 	    if (masterdev) {
500 		myrid = 0;
501 		start = (unit == 0 ? IO_WD1 : IO_WD2) + ATA_ALTPORT;
502 		end = start + ATA_ALTIOSIZE - 1;
503 		count = ATA_ALTIOSIZE;
504 	    }
505 	    else
506 		myrid = 0x14 + 8 * unit;
507 	    break;
508 
509 	case ATA_BMADDR_RID:
510 	    /* the busmaster resource is shared between the two channels */
511 	    if (sc->bmio) {
512 		if (unit == 0) {
513 		    sc->bmio_1 = *sc->bmio;
514 		    sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
515 		    return &sc->bmio_1;
516 		} else {
517 		    sc->bmio_2 = *sc->bmio;
518 		    sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
519 		    sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
520 		    return &sc->bmio_2;
521 		}
522 	    }
523 	    break;
524 
525 	default:
526 	    return 0;
527 	}
528 
529 	if (masterdev)
530 	    /* make the parent just pass through the allocation. */
531 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
532 				      SYS_RES_IOPORT, &myrid,
533 				      start, end, count, flags);
534 	else
535 	    /* we are using the parent resource directly. */
536 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
537 				      SYS_RES_IOPORT, &myrid,
538 				      start, end, count, flags);
539     }
540 
541     if (type == SYS_RES_IRQ) {
542 	if (*rid != 0)
543 	    return 0;
544 
545 	if (masterdev) {
546 #ifdef __i386__
547 	    int irq = (unit == 0 ? 14 : 15);
548 
549 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
550 				      SYS_RES_IRQ, rid,
551 				      irq, irq, 1, flags & ~RF_SHAREABLE);
552 #else
553 	    return alpha_platform_alloc_ide_intr(unit);
554 #endif
555 	} else {
556 	    /* primary and secondary channels share the same interrupt */
557 	    sc->irqcnt++;
558 	    if (!sc->irq)
559 		sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
560 					     SYS_RES_IRQ, rid, 0, ~0, 1, flags);
561 	    return sc->irq;
562 	}
563     }
564     return 0;
565 }
566 
567 static int
568 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
569 			 struct resource *r)
570 {
571     struct ata_pci_softc *sc = device_get_softc(dev);
572     int unit = (uintptr_t) device_get_ivars(child);
573     int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
574     int myrid = 0;
575 
576     if (type == SYS_RES_IOPORT) {
577 	switch (rid) {
578 	case ATA_IOADDR_RID:
579 	    if (masterdev)
580 		myrid = 0;
581 	    else
582 		myrid = 0x10 + 8 * unit;
583 	    break;
584 
585 	case ATA_ALTADDR_RID:
586 	    if (masterdev)
587 		myrid = 0;
588 	    else
589 		myrid = 0x14 + 8 * unit;
590 	    break;
591 
592 	case ATA_BMADDR_RID:
593 	    return 0;
594 
595 	default:
596 	    return ENOENT;
597 	}
598 
599 	if (masterdev)
600 	    /* make the parent just pass through the allocation. */
601 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
602 					SYS_RES_IOPORT, myrid, r);
603 	else
604 	    /* we are using the parent resource directly. */
605 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
606 					SYS_RES_IOPORT, myrid, r);
607     }
608     if (type == SYS_RES_IRQ) {
609 	if (rid != 0)
610 	    return ENOENT;
611 
612 	if (masterdev) {
613 #ifdef __i386__
614 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev),
615 					child, SYS_RES_IRQ, rid, r);
616 #else
617 	    return alpha_platform_release_ide_intr(unit, r);
618 #endif
619 	}
620 	else {
621 	    if (--sc->irqcnt)
622 		return 0;
623 
624 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev),
625 					dev, SYS_RES_IRQ, rid, r);
626 	}
627     }
628     return EINVAL;
629 }
630 
631 static int
632 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
633 		   int flags, driver_intr_t *intr, void *arg,
634 		   void **cookiep)
635 {
636     if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
637 #ifdef __i386__
638 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
639 			      flags, intr, arg, cookiep);
640 #else
641 	return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
642 #endif
643     }
644     else
645 	return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
646 			      flags, intr, arg, cookiep);
647 }
648 
649 static int
650 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
651 		      void *cookie)
652 {
653     if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
654 #ifdef __i386__
655 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
656 #else
657 	return alpha_platform_teardown_ide_intr(irq, cookie);
658 #endif
659     }
660     else
661 	return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
662 }
663 
664 static device_method_t ata_pci_methods[] = {
665     /* device interface */
666     DEVMETHOD(device_probe,		ata_pci_probe),
667     DEVMETHOD(device_attach,		ata_pci_attach),
668     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
669     DEVMETHOD(device_suspend,		bus_generic_suspend),
670     DEVMETHOD(device_resume,		bus_generic_resume),
671 
672     /* bus methods */
673     DEVMETHOD(bus_print_child,		ata_pci_print_child),
674     DEVMETHOD(bus_alloc_resource,	ata_pci_alloc_resource),
675     DEVMETHOD(bus_release_resource,	ata_pci_release_resource),
676     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
677     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
678     DEVMETHOD(bus_setup_intr,		ata_pci_setup_intr),
679     DEVMETHOD(bus_teardown_intr,	ata_pci_teardown_intr),
680     { 0, 0 }
681 };
682 
683 static driver_t ata_pci_driver = {
684     "atapci",
685     ata_pci_methods,
686     sizeof(struct ata_pci_softc),
687 };
688 
689 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
690 
691 static int
692 ata_pcisub_probe(device_t dev)
693 {
694     struct ata_softc *scp = device_get_softc(dev);
695 
696     /* kids of pci ata chipsets has their physical unit number in ivars */
697     scp->unit = (uintptr_t) device_get_ivars(dev);
698 
699     /* set the chiptype to the hostchip ID, makes life easier */
700     if (ata_find_dev(device_get_parent(dev), 0x05861106))
701 	scp->chiptype = 0x05861106;
702     else if (ata_find_dev(device_get_parent(dev), 0x05961106))
703 	scp->chiptype = 0x05961106;
704     else if (ata_find_dev(device_get_parent(dev), 0x06861106))
705 	scp->chiptype = 0x06861106;
706     else
707 	scp->chiptype = pci_get_devid(device_get_parent(dev));
708     return ata_probe(dev);
709 }
710 
711 static device_method_t ata_pcisub_methods[] = {
712     /* device interface */
713     DEVMETHOD(device_probe,	ata_pcisub_probe),
714     DEVMETHOD(device_attach,	ata_attach),
715     DEVMETHOD(device_detach,	ata_detach),
716     DEVMETHOD(device_resume,	ata_resume),
717     { 0, 0 }
718 };
719 
720 static driver_t ata_pcisub_driver = {
721     "ata",
722     ata_pcisub_methods,
723     sizeof(struct ata_softc),
724 };
725 
726 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
727 #endif
728 
729 static int
730 ata_probe(device_t dev)
731 {
732     struct ata_softc *scp = device_get_softc(dev);
733     struct resource *io = 0;
734     struct resource *altio = 0;
735     struct resource *bmio = 0;
736     int rid;
737     int32_t ioaddr, altioaddr, bmaddr;
738     int32_t mask = 0;
739     u_int8_t status0, status1;
740 
741     if (!scp || scp->flags & ATA_ATTACHED)
742 	return ENXIO;
743 
744     /* initialize the softc basics */
745     scp->active = ATA_IDLE;
746     scp->dev = dev;
747     scp->devices = 0;
748 
749     rid = ATA_IOADDR_RID;
750     io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
751     			    ATA_IOSIZE, RF_ACTIVE);
752     if (!io)
753 	goto failure;
754     ioaddr = rman_get_start(io);
755 
756     rid = ATA_ALTADDR_RID;
757     altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
758 			       ATA_ALTIOSIZE, RF_ACTIVE);
759     if (altio)
760 	altioaddr = rman_get_start(altio);
761     else
762 	altioaddr = ioaddr + ATA_IOSIZE;
763 
764     rid = ATA_BMADDR_RID;
765     bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
766     bmaddr = bmio ? rman_get_start(bmio) : 0;
767 
768     /* store the IO resources for eventual later release */
769     scp->r_io = io;
770     scp->r_altio = altio;
771     scp->r_bmio = bmio;
772 
773     /* store the physical IO addresse for easy access */
774     scp->ioaddr = ioaddr;
775     scp->altioaddr = altioaddr;
776     scp->bmaddr = bmaddr;
777 
778     if (bootverbose)
779 	ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
780 		   scp->ioaddr, scp->altioaddr, scp->bmaddr);
781 
782     /* do we have any signs of ATA/ATAPI HW being present ? */
783     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
784     DELAY(1);
785     status0 = inb(scp->ioaddr + ATA_STATUS);
786     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
787     DELAY(1);
788     status1 = inb(scp->ioaddr + ATA_STATUS);
789     if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
790 	mask |= 0x01;
791     if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
792 	mask |= 0x02;
793     if (bootverbose)
794 	ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
795 		   mask, status0, status1);
796     if (!mask)
797 	goto failure;
798 
799     ata_reset(scp, &mask);
800     if (!mask)
801 	goto failure;
802 
803     /*
804      * OK, we have at least one device on the chain, check for ATAPI
805      * signatures, if none check if its a good old ATA device.
806      */
807     outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
808     DELAY(1);
809     if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
810 	inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
811 	scp->devices |= ATA_ATAPI_MASTER;
812     }
813     outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
814     DELAY(1);
815     if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
816 	inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
817 	scp->devices |= ATA_ATAPI_SLAVE;
818     }
819     if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
820 	outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
821 	DELAY(1);
822 	outb(scp->ioaddr + ATA_ERROR, 0x58);
823 	outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
824 	if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
825 	    inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
826 	    scp->devices |= ATA_ATA_MASTER;
827 	}
828     }
829     if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
830 	outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
831 	DELAY(1);
832 	outb(scp->ioaddr + ATA_ERROR, 0x58);
833 	outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
834 	if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
835 	    inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
836 	    scp->devices |= ATA_ATA_SLAVE;
837 	}
838     }
839     if (bootverbose)
840 	ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
841     if (!scp->devices) {
842 	goto failure;
843     }
844     TAILQ_INIT(&scp->ata_queue);
845     TAILQ_INIT(&scp->atapi_queue);
846     return 0;
847 
848 failure:
849     if (io)
850 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
851     if (altio)
852 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio);
853     if (bmio)
854 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
855     if (bootverbose)
856 	ata_printf(scp, -1, "probe allocation failed\n");
857     return ENXIO;
858 }
859 
860 static int
861 ata_attach(device_t dev)
862 {
863     struct ata_softc *scp = device_get_softc(dev);
864     int error, rid = 0;
865 
866     if (!scp || scp->flags & ATA_ATTACHED)
867 	return ENXIO;
868 
869     scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
870 				    RF_SHAREABLE | RF_ACTIVE);
871     if (!scp->r_irq) {
872 	ata_printf(scp, -1, "unable to allocate interrupt\n");
873 	return ENXIO;
874     }
875     if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
876 				scp, &scp->ih)))
877 	return error;
878 
879     /*
880      * do not attach devices if we are in early boot, this is done later
881      * when interrupts are enabled by a hook into the boot process.
882      * otherwise attach what the probe has found in scp->devices.
883      */
884     if (!ata_delayed_attach) {
885 	if (scp->devices & ATA_ATA_SLAVE)
886 	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
887 		scp->devices &= ~ATA_ATA_SLAVE;
888 	if (scp->devices & ATA_ATAPI_SLAVE)
889 	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
890 		scp->devices &= ~ATA_ATAPI_SLAVE;
891 	if (scp->devices & ATA_ATA_MASTER)
892 	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
893 		scp->devices &= ~ATA_ATA_MASTER;
894 	if (scp->devices & ATA_ATAPI_MASTER)
895 	    if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
896 		scp->devices &= ~ATA_ATAPI_MASTER;
897 #if NATADISK > 0
898 	if (scp->devices & ATA_ATA_MASTER)
899 	    ad_attach(scp, ATA_MASTER);
900 	if (scp->devices & ATA_ATA_SLAVE)
901 	    ad_attach(scp, ATA_SLAVE);
902 #endif
903 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
904 	if (scp->devices & ATA_ATAPI_MASTER)
905 	    atapi_attach(scp, ATA_MASTER);
906 	if (scp->devices & ATA_ATAPI_SLAVE)
907 	    atapi_attach(scp, ATA_SLAVE);
908 #endif
909     }
910     scp->flags |= ATA_ATTACHED;
911     return 0;
912 }
913 
914 static int
915 ata_detach(device_t dev)
916 {
917     struct ata_softc *scp = device_get_softc(dev);
918 
919     if (!scp || !(scp->flags & ATA_ATTACHED))
920 	return ENXIO;
921 
922 #if NATADISK > 0
923     if (scp->devices & ATA_ATA_MASTER)
924 	ad_detach(scp, ATA_MASTER);
925     if (scp->devices & ATA_ATA_SLAVE)
926 	ad_detach(scp, ATA_SLAVE);
927 #endif
928 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
929     if (scp->devices & ATA_ATAPI_MASTER)
930 	atapi_detach(scp, ATA_MASTER);
931     if (scp->devices & ATA_ATAPI_SLAVE)
932 	atapi_detach(scp, ATA_SLAVE);
933 #endif
934     if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
935 	free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
936 	scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
937     }
938     if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
939 	free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
940 	scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
941     }
942     scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
943     scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
944     bus_teardown_intr(dev, scp->r_irq, scp->ih);
945     bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
946     if (scp->r_bmio)
947 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
948     if (scp->r_altio)
949 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
950     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
951     scp->flags &= ~ATA_ATTACHED;
952     return 0;
953 }
954 
955 static int
956 ata_resume(device_t dev)
957 {
958     struct ata_softc *scp = device_get_softc(dev);
959 
960     ata_reinit(scp);
961     return 0;
962 }
963 
964 static int32_t
965 ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
966 {
967     struct ata_params *ata_parm;
968     int8_t buffer[DEV_BSIZE];
969     int retry = 0;
970 
971     /* select drive */
972     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
973     DELAY(1);
974 
975     /* enable interrupt */
976     outb(scp->altioaddr, ATA_A_4BIT);
977     DELAY(1);
978 
979     /* apparently some devices needs this repeated */
980     do {
981 	if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
982 	    ata_printf(scp, device, "identify failed\n");
983 	    return -1;
984 	}
985 	if (retry++ > 4) {
986 	    ata_printf(scp, device, "identify retries exceeded\n");
987 	    return -1;
988 	}
989     } while (ata_wait(scp, device,
990 		      ((command == ATA_C_ATAPI_IDENTIFY) ?
991 			ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
992 
993     insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
994     ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
995     if (!ata_parm) {
996 	ata_printf(scp, device, "malloc for identify data failed\n");
997         return -1;
998     }
999     bcopy(buffer, ata_parm, sizeof(struct ata_params));
1000     if (command == ATA_C_ATA_IDENTIFY ||
1001 	!((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
1002           (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
1003         bswap(ata_parm->model, sizeof(ata_parm->model));
1004     btrim(ata_parm->model, sizeof(ata_parm->model));
1005     bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
1006     bswap(ata_parm->revision, sizeof(ata_parm->revision));
1007     btrim(ata_parm->revision, sizeof(ata_parm->revision));
1008     bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
1009     scp->dev_param[ATA_DEV(device)] = ata_parm;
1010     return 0;
1011 }
1012 
1013 static void
1014 ata_boot_attach(void)
1015 {
1016     struct ata_softc *scp;
1017     int32_t ctlr;
1018 
1019     /*
1020      * run through all ata devices and look for real ATA & ATAPI devices
1021      * using the hints we found in the early probe, this avoids some of
1022      * the delays probing of non-exsistent devices can cause.
1023      */
1024     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1025 	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1026 	    continue;
1027 	if (scp->devices & ATA_ATA_SLAVE)
1028 	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
1029 		scp->devices &= ~ATA_ATA_SLAVE;
1030 	if (scp->devices & ATA_ATAPI_SLAVE)
1031 	    if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
1032 		scp->devices &= ~ATA_ATAPI_SLAVE;
1033 	if (scp->devices & ATA_ATA_MASTER)
1034 	    if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
1035 		scp->devices &= ~ATA_ATA_MASTER;
1036 	if (scp->devices & ATA_ATAPI_MASTER)
1037 	    if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
1038 		scp->devices &= ~ATA_ATAPI_MASTER;
1039     }
1040 
1041 #if NATADISK > 0
1042     /* now we know whats there, do the real attach, first the ATA disks */
1043     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1044 	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1045 	    continue;
1046 	if (scp->devices & ATA_ATA_MASTER)
1047 	    ad_attach(scp, ATA_MASTER);
1048 	if (scp->devices & ATA_ATA_SLAVE)
1049 	    ad_attach(scp, ATA_SLAVE);
1050     }
1051 #endif
1052 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1053     /* then the atapi devices */
1054     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1055 	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1056 	    continue;
1057 	if (scp->devices & ATA_ATAPI_MASTER)
1058 	    atapi_attach(scp, ATA_MASTER);
1059 	if (scp->devices & ATA_ATAPI_SLAVE)
1060 	    atapi_attach(scp, ATA_SLAVE);
1061     }
1062 #endif
1063     if (ata_delayed_attach) {
1064 	config_intrhook_disestablish(ata_delayed_attach);
1065 	free(ata_delayed_attach, M_ATA);
1066 	ata_delayed_attach = NULL;
1067     }
1068 }
1069 
1070 static void
1071 ata_intr(void *data)
1072 {
1073     struct ata_softc *scp = (struct ata_softc *)data;
1074     u_int8_t dmastat;
1075 
1076     /*
1077      * since we might share the IRQ with another device, and in some
1078      * cases with our twin channel, we only want to process interrupts
1079      * that we know this channel generated.
1080      */
1081     switch (scp->chiptype) {
1082 #if NPCI > 0
1083     case 0x00041103:    /* HighPoint HPT366 */
1084 	if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1085 	    return;
1086 	outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1087 	break;
1088 
1089     case 0x4d33105a:	/* Promise 33's */
1090     case 0x4d38105a:	/* Promise 66's */
1091     {
1092 	struct ata_pci_softc *sc=device_get_softc(device_get_parent(scp->dev));
1093 
1094 	if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
1095 	      ((scp->unit) ? 0x00004000 : 0x00000400)))
1096 	    return;
1097     }
1098 	/* FALLTHROUGH */
1099 #endif
1100     default:
1101 	if (scp->flags & ATA_DMA_ACTIVE) {
1102 	    if (!(dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT)
1103 		return;
1104 	    else
1105 		outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);
1106 	}
1107     }
1108     DELAY(1);
1109 
1110     /* get status, if drive is busy it didn't interrupt so return */
1111     if ((scp->status = inb(scp->ioaddr + ATA_STATUS)) & ATA_S_BUSY)
1112 	return;
1113 
1114     /* find & call the responsible driver to process this interrupt */
1115     switch (scp->active) {
1116 #if NATADISK > 0
1117     case ATA_ACTIVE_ATA:
1118 	if (!scp->running)
1119 	    return;
1120 	if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1121 	    return;
1122 	break;
1123 #endif
1124 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1125     case ATA_ACTIVE_ATAPI:
1126 	if (!scp->running)
1127 	    return;
1128 	if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
1129 	    return;
1130 	break;
1131 #endif
1132     case ATA_WAIT_INTR:
1133 	wakeup((caddr_t)scp);
1134 	break;
1135 
1136     case ATA_WAIT_READY:
1137 	break;
1138 
1139     case ATA_REINITING:
1140 	return;
1141 
1142     default:
1143     case ATA_IDLE:
1144 #ifdef ATA_DEBUG
1145 	{
1146     	    static int32_t intr_count = 0;
1147 	    if (intr_count++ < 10)
1148 		ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n",
1149 			   intr_count, scp->status);
1150 	}
1151 #endif
1152     }
1153     scp->active = ATA_IDLE;
1154     scp->running = NULL;
1155     ata_start(scp);
1156 }
1157 
1158 void
1159 ata_start(struct ata_softc *scp)
1160 {
1161 #if NATADISK > 0
1162     struct ad_request *ad_request;
1163 #endif
1164 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1165     struct atapi_request *atapi_request;
1166 #endif
1167 
1168     if (scp->active != ATA_IDLE)
1169 	return;
1170     scp->active = ATA_ACTIVE;
1171 
1172 #if NATADISK > 0
1173     /* find & call the responsible driver if anything on the ATA queue */
1174     if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
1175 	TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
1176 	scp->active = ATA_ACTIVE_ATA;
1177 	scp->running = ad_request;
1178 	ad_transfer(ad_request);
1179 	return;
1180     }
1181 #endif
1182 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1183     /*
1184      * find & call the responsible driver if anything on the ATAPI queue.
1185      * check for device busy by polling the DSC bit, if busy, check
1186      * for requests to the other device on the channel (if any).
1187      * if the other device is an ATA disk it already had its chance above.
1188      * if no request can be served, timeout a call to ata_start.
1189      */
1190     if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
1191 	struct atapi_softc *atp = atapi_request->device;
1192 	static int32_t interval = 1;
1193 
1194 	if (atp->flags & ATAPI_F_DSC_USED) {
1195 	    outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
1196 	    DELAY(1);
1197 	    if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) {
1198 		while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) {
1199 		    if (atapi_request->device->unit != atp->unit) {
1200 			struct atapi_softc *tmp = atapi_request->device;
1201 
1202 			outb(tmp->controller->ioaddr + ATA_DRIVE,
1203 			     ATA_D_IBM | tmp->unit);
1204 			DELAY(1);
1205 			if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC)
1206 			    atapi_request = NULL;
1207 			break;
1208 		    }
1209 	        }
1210 	    }
1211 	    if (!atapi_request) {
1212 		timeout((timeout_t *)ata_start, atp->controller, interval++);
1213 		return;
1214 	    }
1215 	    else
1216 		interval = 1;
1217 	}
1218 	TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
1219 	scp->active = ATA_ACTIVE_ATAPI;
1220 	scp->running = atapi_request;
1221 	atapi_transfer(atapi_request);
1222 	return;
1223     }
1224 #endif
1225     scp->active = ATA_IDLE;
1226 }
1227 
1228 void
1229 ata_reset(struct ata_softc *scp, int32_t *mask)
1230 {
1231     int32_t timeout;
1232     u_int8_t status0 = 0, status1 = 0;
1233 
1234     /* reset channel */
1235     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1236     DELAY(1);
1237     inb(scp->ioaddr + ATA_STATUS);
1238     outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1239     DELAY(10000);
1240     outb(scp->altioaddr, ATA_A_IDS);
1241     DELAY(10000);
1242     inb(scp->ioaddr + ATA_ERROR);
1243     DELAY(3000);
1244 
1245     /* wait for BUSY to go inactive */
1246     for (timeout = 0; timeout < 310000; timeout++) {
1247 	outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1248 	DELAY(1);
1249 	status0 = inb(scp->ioaddr + ATA_STATUS);
1250 	outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1251 	DELAY(1);
1252 	status1 = inb(scp->ioaddr + ATA_STATUS);
1253 	if (*mask == 0x01)      /* wait for master only */
1254 	    if (!(status0 & ATA_S_BUSY))
1255 		break;
1256 	if (*mask == 0x02)      /* wait for slave only */
1257 	    if (!(status1 & ATA_S_BUSY))
1258 		break;
1259 	if (*mask == 0x03)      /* wait for both master & slave */
1260 	    if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1261 		break;
1262 	DELAY(100);
1263     }
1264     DELAY(1);
1265     outb(scp->altioaddr, ATA_A_4BIT);
1266     if (status0 & ATA_S_BUSY)
1267 	*mask &= ~0x01;
1268     if (status1 & ATA_S_BUSY)
1269 	*mask &= ~0x02;
1270     if (bootverbose)
1271 	ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1272 		   *mask, status0, status1);
1273 }
1274 
1275 int32_t
1276 ata_reinit(struct ata_softc *scp)
1277 {
1278     int32_t mask = 0, omask;
1279 
1280     scp->active = ATA_REINITING;
1281     scp->running = NULL;
1282     if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
1283 	mask |= 0x01;
1284     if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
1285 	mask |= 0x02;
1286     if (mask) {
1287 	omask = mask;
1288         ata_printf(scp, -1, "resetting devices .. ");
1289 	ata_reset(scp, &mask);
1290 	if (omask != mask)
1291 	    printf(" device dissapeared! %d ", omask & ~mask);
1292 
1293 #if NATADISK > 0
1294 	if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1295 	    ad_reinit((struct ad_softc *)scp->dev_softc[0]);
1296 	if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1297 	    ad_reinit((struct ad_softc *)scp->dev_softc[1]);
1298 #endif
1299 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1300 	if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1301 	    atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
1302 	if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1303 	    atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
1304 #endif
1305 	printf("done\n");
1306     }
1307     scp->active = ATA_IDLE;
1308     ata_start(scp);
1309     return 0;
1310 }
1311 
1312 int32_t
1313 ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
1314 {
1315     u_int32_t timeout = 0;
1316 
1317     DELAY(1);
1318     while (timeout < 5000000) {	/* timeout 5 secs */
1319 	scp->status = inb(scp->ioaddr + ATA_STATUS);
1320 
1321 	/* if drive fails status, reselect the drive just to be sure */
1322 	if (scp->status == 0xff) {
1323 	    ata_printf(scp, device, "no status, reselecting device\n");
1324 	    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1325 	    DELAY(1);
1326 	    scp->status = inb(scp->ioaddr + ATA_STATUS);
1327 	}
1328 
1329 	/* are we done ? */
1330 	if (!(scp->status & ATA_S_BUSY))
1331 	    break;
1332 
1333 	if (timeout > 1000) {
1334 	    timeout += 1000;
1335 	    DELAY(1000);
1336 	}
1337 	else {
1338 	    timeout += 10;
1339 	    DELAY(10);
1340 	}
1341     }
1342     if (scp->status & ATA_S_ERROR)
1343 	scp->error = inb(scp->ioaddr + ATA_ERROR);
1344     if (timeout >= 5000000)
1345 	return -1;
1346     if (!mask)
1347 	return (scp->status & ATA_S_ERROR);
1348 
1349     /* Wait 50 msec for bits wanted. */
1350     timeout = 5000;
1351     while (timeout--) {
1352 	scp->status = inb(scp->ioaddr + ATA_STATUS);
1353 	if ((scp->status & mask) == mask) {
1354 	    if (scp->status & ATA_S_ERROR)
1355 		scp->error = inb(scp->ioaddr + ATA_ERROR);
1356 	    return (scp->status & ATA_S_ERROR);
1357 	}
1358 	DELAY (10);
1359     }
1360     return -1;
1361 }
1362 
1363 int32_t
1364 ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
1365 	   u_int32_t cylinder, u_int32_t head, u_int32_t sector,
1366 	   u_int32_t count, u_int32_t feature, int32_t flags)
1367 {
1368 #ifdef ATA_DEBUG
1369     ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1370 	       "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
1371 	       scp->ioaddr, command, cylinder, head, sector, count, flags);
1372 #endif
1373 
1374     /* ready to issue command ? */
1375     if (ata_wait(scp, device, 0) < 0) {
1376 	ata_printf(scp, device,
1377 		   "timeout waiting to give command=%02x s=%02x e=%02x\n",
1378 		   command, scp->status, scp->error);
1379 	return -1;
1380     }
1381     outb(scp->ioaddr + ATA_FEATURE, feature);
1382     outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1383     outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1384     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1385     outb(scp->ioaddr + ATA_SECTOR, sector);
1386     outb(scp->ioaddr + ATA_COUNT, count);
1387 
1388     switch (flags) {
1389     case ATA_WAIT_INTR:
1390 	if (scp->active != ATA_IDLE)
1391 	    ata_printf(scp, device, "WARNING: WAIT_INTR active=%s\n",
1392 		       active2str(scp->active));
1393 	scp->active = ATA_WAIT_INTR;
1394 	asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1395 	outb(scp->ioaddr + ATA_CMD, command);
1396 	if (await(PRIBIO, 10 * hz)) {
1397 	    ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1398 	    scp->active = ATA_IDLE;
1399 	    return -1;
1400 	}
1401 	break;
1402 
1403     case ATA_WAIT_READY:
1404 	if (scp->active != ATA_IDLE && scp->active != ATA_REINITING)
1405 	    ata_printf(scp, device, "WARNING: WAIT_READY active=%s\n",
1406 		       active2str(scp->active));
1407 	if (scp->active != ATA_REINITING)
1408 	    scp->active = ATA_WAIT_READY;
1409 	outb(scp->ioaddr + ATA_CMD, command);
1410 	if (ata_wait(scp, device, ATA_S_READY) < 0) {
1411 	    ata_printf(scp, device,
1412 		       "timeout waiting for command=%02x s=%02x e=%02x\n",
1413 		       command, scp->status, scp->error);
1414 	    if (scp->active != ATA_REINITING)
1415 		scp->active = ATA_IDLE;
1416 	    return -1;
1417 	}
1418 	if (scp->active != ATA_REINITING)
1419 	    scp->active = ATA_IDLE;
1420 	break;
1421 
1422     case ATA_IMMEDIATE:
1423 	outb(scp->ioaddr + ATA_CMD, command);
1424 	break;
1425 
1426     default:
1427 	ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1428 		   active2str(flags));
1429     }
1430     return 0;
1431 }
1432 
1433 int
1434 ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1435 {
1436     va_list ap;
1437     int ret;
1438 
1439     if (device == -1)
1440 	ret = printf("ata%d: ", device_get_unit(scp->dev));
1441     else
1442 	ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1443 		     (device == ATA_MASTER) ? "master" : "slave");
1444     va_start(ap, fmt);
1445     ret += vprintf(fmt, ap);
1446     va_end(ap);
1447     return ret;
1448 }
1449 
1450 int
1451 ata_get_lun(u_int32_t *map)
1452 {
1453     int lun = ffs(~*map) - 1;
1454 
1455     *map |= (1 << lun);
1456     return lun;
1457 }
1458 
1459 void
1460 ata_free_lun(u_int32_t *map, int lun)
1461 {
1462     *map &= ~(1 << lun);
1463 }
1464 
1465 int8_t *
1466 ata_mode2str(int32_t mode)
1467 {
1468     switch (mode) {
1469     case ATA_PIO: return "BIOSPIO";
1470     case ATA_PIO0: return "PIO0";
1471     case ATA_PIO1: return "PIO1";
1472     case ATA_PIO2: return "PIO2";
1473     case ATA_PIO3: return "PIO3";
1474     case ATA_PIO4: return "PIO4";
1475     case ATA_WDMA2: return "WDMA2";
1476     case ATA_UDMA2: return "UDMA33";
1477     case ATA_UDMA4: return "UDMA66";
1478     case ATA_DMA: return "BIOSDMA";
1479     default: return "???";
1480     }
1481 }
1482 
1483 int8_t
1484 ata_pio2mode(int32_t pio)
1485 {
1486     switch (pio) {
1487     default:
1488     case 0: return ATA_PIO0;
1489     case 1: return ATA_PIO1;
1490     case 2: return ATA_PIO2;
1491     case 3: return ATA_PIO3;
1492     case 4: return ATA_PIO4;
1493     }
1494 }
1495 
1496 int
1497 ata_pmode(struct ata_params *ap)
1498 {
1499     if (ap->atavalid & ATA_FLAG_64_70) {
1500 	if (ap->apiomodes & 2)
1501 	    return 4;
1502 	if (ap->apiomodes & 1)
1503 	    return 3;
1504     }
1505     if (ap->opiomode == 2)
1506 	return 2;
1507     if (ap->opiomode == 1)
1508 	return 1;
1509     if (ap->opiomode == 0)
1510 	return 0;
1511     return -1;
1512 }
1513 
1514 int
1515 ata_wmode(struct ata_params *ap)
1516 {
1517     if (ap->wdmamodes & 4)
1518 	return 2;
1519     if (ap->wdmamodes & 2)
1520 	return 1;
1521     if (ap->wdmamodes & 1)
1522 	return 0;
1523     return -1;
1524 }
1525 
1526 int
1527 ata_umode(struct ata_params *ap)
1528 {
1529     if (ap->atavalid & ATA_FLAG_88) {
1530 	if (ap->udmamodes & 0x10)
1531 	    return (ap->cblid ? 4 : 2);
1532 	if (ap->udmamodes & 0x08)
1533 	    return (ap->cblid ? 3 : 2);
1534 	if (ap->udmamodes & 0x04)
1535 	    return 2;
1536 	if (ap->udmamodes & 0x02)
1537 	    return 1;
1538 	if (ap->udmamodes & 0x01)
1539 	    return 0;
1540     }
1541     return -1;
1542 }
1543 
1544 static int8_t *
1545 active2str(int32_t active)
1546 {
1547     static char buf[8];
1548 
1549     switch (active) {
1550     case ATA_IDLE:
1551 	return("ATA_IDLE");
1552     case ATA_IMMEDIATE:
1553 	return("ATA_IMMEDIATE");
1554     case ATA_WAIT_INTR:
1555 	return("ATA_WAIT_INTR");
1556     case ATA_WAIT_READY:
1557 	return("ATA_WAIT_READY");
1558     case ATA_ACTIVE:
1559 	return("ATA_ACTIVE");
1560     case ATA_ACTIVE_ATA:
1561 	return("ATA_ACTIVE_ATA");
1562     case ATA_ACTIVE_ATAPI:
1563 	return("ATA_ACTIVE_ATAPI");
1564     case ATA_REINITING:
1565 	return("ATA_REINITING");
1566     default:
1567 	sprintf(buf, "0x%02x", active);
1568 	return buf;
1569     }
1570 }
1571 
1572 static void
1573 bswap(int8_t *buf, int32_t len)
1574 {
1575     u_int16_t *ptr = (u_int16_t*)(buf + len);
1576 
1577     while (--ptr >= (u_int16_t*)buf)
1578 	*ptr = ntohs(*ptr);
1579 }
1580 
1581 static void
1582 btrim(int8_t *buf, int32_t len)
1583 {
1584     int8_t *ptr;
1585 
1586     for (ptr = buf; ptr < buf+len; ++ptr)
1587 	if (!*ptr)
1588 	    *ptr = ' ';
1589     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1590 	*ptr = 0;
1591 }
1592 
1593 static void
1594 bpack(int8_t *src, int8_t *dst, int32_t len)
1595 {
1596     int32_t i, j, blank;
1597 
1598     for (i = j = blank = 0 ; i < len; i++) {
1599 	if (blank && src[i] == ' ') continue;
1600 	if (blank && src[i] != ' ') {
1601 	    dst[j++] = src[i];
1602 	    blank = 0;
1603 	    continue;
1604 	}
1605 	if (src[i] == ' ') {
1606 	    blank = 1;
1607 	    if (i == 0)
1608 		continue;
1609 	}
1610 	dst[j++] = src[i];
1611     }
1612     if (j < len)
1613 	dst[j] = 0x00;
1614 }
1615 
1616 static void
1617 ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1618 {
1619     int32_t s = splbio();
1620 
1621     while (scp->active != ATA_IDLE)
1622 	tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1623     scp->active = ATA_REINITING;
1624     ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1625 		mode < ATA_DMA ?  -1 : ata_wmode(ATA_PARAM(scp, device)),
1626 		mode < ATA_DMA ?  -1 : ata_umode(ATA_PARAM(scp, device)));
1627     scp->active = ATA_IDLE;
1628     ata_start(scp);
1629     splx(s);
1630 }
1631 
1632 static int
1633 sysctl_hw_ata SYSCTL_HANDLER_ARGS
1634 {
1635     struct ata_softc *scp;
1636     int ctlr, error, i;
1637 
1638     /* readout internal state */
1639     bzero(ata_conf, sizeof(ata_conf));
1640     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1641 	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1642 	    continue;
1643 	for (i = 0; i < 2; i++) {
1644 	    if (!scp->dev_softc[i])
1645 		strcat(ata_conf, "---,");
1646 	    else if (scp->mode[i] >= ATA_DMA)
1647 		strcat(ata_conf, "dma,");
1648 	    else
1649 		strcat(ata_conf, "pio,");
1650 	}
1651     }
1652     error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1653     if (error == 0 && req->newptr != NULL) {
1654 	char *ptr = ata_conf;
1655 
1656         /* update internal state */
1657 	i = 0;
1658         while (*ptr) {
1659 	    if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1660 		if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1661 		    scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1662 		    ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1663 	    }
1664 	    else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1665 		if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1666 		    scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1667 		    ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1668 	    }
1669 	    else if (strncmp(ptr, "---", 3))
1670 		break;
1671 	    ptr+=3;
1672 	    if (*ptr++ != ',' ||
1673 		++i > (devclass_get_maxunit(ata_devclass) << 1))
1674 		break;
1675         }
1676     }
1677     return error;
1678 }
1679 SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1680             0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1681 
1682 static void
1683 ata_init(void)
1684 {
1685     /* register boot attach to be run when interrupts are enabled */
1686     if (!(ata_delayed_attach = (struct intr_config_hook *)
1687 			     malloc(sizeof(struct intr_config_hook),
1688 			     M_TEMP, M_NOWAIT))) {
1689 	printf("ata: malloc of delayed attach hook failed\n");
1690 	return;
1691     }
1692     bzero(ata_delayed_attach, sizeof(struct intr_config_hook));
1693 
1694     ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1695     if (config_intrhook_establish(ata_delayed_attach) != 0) {
1696 	printf("ata: config_intrhook_establish failed\n");
1697 	free(ata_delayed_attach, M_TEMP);
1698     }
1699 }
1700 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
1701