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