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