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