xref: /freebsd/sys/dev/ata/ata-all.c (revision 5069714534cba67f1985e6dfe23b145178372b5f)
1 /*-
2  * Copyright (c) 1998,1999,2000,2001 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, 0x40))
280 	    return "VIA 82C686 ATA100 controller";
281 	if (ata_find_dev(dev, 0x06861106, 0))
282 	    return "VIA 82C686 ATA66 controller";
283 	return "VIA Apollo ATA controller";
284 
285     case 0x55131039:
286 	return "SiS 5591 ATA33 controller";
287 
288     case 0x06491095:
289 	return "CMD 649 ATA100 controller";
290 
291     case 0x06481095:
292 	return "CMD 648 ATA66 controller";
293 
294     case 0x06461095:
295 	return "CMD 646 ATA controller";
296 
297     case 0xc6931080:
298 	if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
299 	    return "Cypress 82C693 ATA controller";
300 	break;
301 
302     case 0x01021078:
303 	return "Cyrix 5530 ATA33 controller";
304 
305     case 0x74091022:
306 	return "AMD 756 ATA66 controller";
307 
308     case 0x02111166:
309 	return "ServerWorks ROSB4 ATA33 controller";
310 
311     case 0x4d33105a:
312 	return "Promise ATA33 controller";
313 
314     case 0x4d38105a:
315 	return "Promise ATA66 controller";
316 
317     case 0x0d30105a:
318     case 0x4d30105a:
319 	return "Promise ATA100 controller";
320 
321     case 0x00041103:
322 	switch (pci_get_revid(dev)) {
323 	case 0x00:
324 	case 0x01:
325 	    return "HighPoint HPT366 ATA66 controller";
326 	case 0x02:
327 	    return "HighPoint HPT368 ATA66 controller";
328 	case 0x03:
329 	case 0x04:
330 	    return "HighPoint HPT370 ATA100 controller";
331 	default:
332 	    return "Unknown revision HighPoint ATA controller";
333 	}
334 
335    /* unsupported but known chipsets, generic DMA only */
336     case 0x10001042:
337     case 0x10011042:
338 	return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
339 
340     case 0x06401095:
341 	return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
342 
343     /* unknown chipsets, try generic DMA if it seems possible */
344     default:
345 	if (pci_get_class(dev) == PCIC_STORAGE &&
346 	    (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
347 	    return "Generic PCI ATA controller";
348     }
349     return NULL;
350 }
351 
352 static int
353 ata_pci_probe(device_t dev)
354 {
355     const char *desc = ata_pci_match(dev);
356 
357     if (desc) {
358 	device_set_desc(dev, desc);
359 	return 0;
360     }
361     else
362 	return ENXIO;
363 }
364 
365 static int
366 ata_pci_add_child(device_t dev, int unit)
367 {
368     device_t child;
369 
370     /* check if this is located at one of the std addresses */
371     if (ATA_MASTERDEV(dev)) {
372 	if (!(child = device_add_child(dev, "ata", unit)))
373 	    return ENOMEM;
374     }
375     else {
376 	if (!(child = device_add_child(dev, "ata", 2)))
377 	    return ENOMEM;
378     }
379     return 0;
380 }
381 
382 static int
383 ata_pci_attach(device_t dev)
384 {
385     struct ata_pci_softc *sc = device_get_softc(dev);
386     u_int8_t class, subclass;
387     u_int32_t type, cmd;
388     int rid;
389 
390     /* set up vendor-specific stuff */
391     type = pci_get_devid(dev);
392     class = pci_get_class(dev);
393     subclass = pci_get_subclass(dev);
394     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
395 
396     /* is busmastering supported ? */
397     if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
398 	(PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
399 
400 	/* is there a valid port range to connect to ? */
401 	rid = 0x20;
402 	sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
403 				      0, ~0, 1, RF_ACTIVE);
404 	if (!sc->bmio)
405 	    device_printf(dev, "Busmastering DMA not configured\n");
406     }
407     else
408 	device_printf(dev, "Busmastering DMA not supported\n");
409 
410     /* do extra chipset specific setups */
411     switch (type) {
412     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
413 	pci_write_config(dev, 0x53,
414 			 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
415 	break;
416 
417     case 0x4d38105a: /* Promise 66 & 100 need their clock changed */
418     case 0x4d30105a:
419     case 0x0d30105a:
420 	outb(rman_get_start(sc->bmio) + 0x11,
421 	     inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
422 	/* FALLTHROUGH */
423 
424     case 0x4d33105a: /* Promise (all) need burst mode to be turned on */
425 	outb(rman_get_start(sc->bmio) + 0x1f,
426 	     inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
427 	break;
428 
429     case 0x00041103: /* HighPoint */
430 	switch (pci_get_revid(dev)) {
431 	case 0x00:
432 	case 0x01:
433 	    /* turn off interrupt prediction */
434 	    pci_write_config(dev, 0x51,
435 	    		     (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
436 	    break;
437 
438 	case 0x02:
439 	case 0x03:
440 	case 0x04:
441 	    /* turn off interrupt prediction */
442 	    pci_write_config(dev, 0x51,
443 	    		     (pci_read_config(dev, 0x51, 1) & ~0x02), 1);
444 	    pci_write_config(dev, 0x55,
445 	    		     (pci_read_config(dev, 0x55, 1) & ~0x02), 1);
446 	    /* turn on interrupts */
447 	    pci_write_config(dev, 0x5a,
448 	    		     (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
449 
450 	}
451 	break;
452 
453     case 0x05711106:
454     case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */
455 
456 	/* set prefetch, postwrite */
457 	pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
458 
459 	/* set fifo configuration half'n'half */
460 	pci_write_config(dev, 0x43,
461 			 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
462 
463 	/* set status register read retry */
464 	pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
465 
466 	/* set DMA read & end-of-sector fifo flush */
467 	pci_write_config(dev, 0x46,
468 			 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
469 
470 	/* set sector size */
471 	pci_write_config(dev, 0x60, DEV_BSIZE, 2);
472 	pci_write_config(dev, 0x68, DEV_BSIZE, 2);
473 
474 	/* prepare for ATA-66 on the 82C686 and rev 0x12 and newer 82C596's */
475 	if (ata_find_dev(dev, 0x06861106, 0) ||
476 	    ata_find_dev(dev, 0x05961106, 0x12)) {
477 	    pci_write_config(dev, 0x50,
478 			     pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
479 	}
480 	break;
481 
482     case 0x10001042:   /* RZ 100? known bad, no DMA */
483     case 0x10011042:
484     case 0x06401095:   /* CMD 640 known bad, no DMA */
485 	sc->bmio = 0x0;
486 	device_printf(dev, "Busmastering DMA disabled\n");
487     }
488 
489     /*
490      * the Cypress chip is a mess, it contains two ATA functions, but
491      * both channels are visible on the first one.
492      * simply ignore the second function for now, as the right
493      * solution (ignoring the second channel on the first function)
494      * doesn't work with the crappy ATA interrupt setup on the alpha.
495      */
496     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
497 	return 0;
498 
499     ata_pci_add_child(dev, 0);
500 
501     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
502 	ata_pci_add_child(dev, 1);
503 
504     return bus_generic_attach(dev);
505 }
506 
507 static int
508 ata_pci_print_child(device_t dev, device_t child)
509 {
510     struct ata_softc *scp = device_get_softc(child);
511     int retval = 0;
512 
513     retval += bus_print_child_header(dev, child);
514     retval += printf(": at 0x%x", scp->ioaddr);
515 
516     if (ATA_MASTERDEV(dev))
517 	retval += printf(" irq %d", 14 + scp->channel);
518 
519     retval += bus_print_child_footer(dev, child);
520 
521     return retval;
522 }
523 
524 static struct resource *
525 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
526 		       u_long start, u_long end, u_long count, u_int flags)
527 {
528     struct ata_pci_softc *sc = device_get_softc(dev);
529     int channel = ((struct ata_softc *)device_get_softc(child))->channel;
530     int myrid;
531 
532     if (type == SYS_RES_IOPORT) {
533 	switch (*rid) {
534 	case ATA_IOADDR_RID:
535 	    if (ATA_MASTERDEV(dev)) {
536 		myrid = 0;
537 		start = (channel == 0 ? IO_WD1 : IO_WD2);
538 		end = start + ATA_IOSIZE - 1;
539 		count = ATA_IOSIZE;
540 	    }
541 	    else
542 		myrid = 0x10 + 8 * channel;
543 	    break;
544 
545 	case ATA_ALTADDR_RID:
546 	    if (ATA_MASTERDEV(dev)) {
547 		myrid = 0;
548 		start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET;
549 		end = start + ATA_ALTIOSIZE - 1;
550 		count = ATA_ALTIOSIZE;
551 	    }
552 	    else
553 		myrid = 0x14 + 8 * channel;
554 	    break;
555 
556 	case ATA_BMADDR_RID:
557 	    /* the busmaster resource is shared between the two channels */
558 	    if (sc->bmio) {
559 		if (channel == 0) {
560 		    sc->bmio_1 = *sc->bmio;
561 		    sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
562 		    return &sc->bmio_1;
563 		} else {
564 		    sc->bmio_2 = *sc->bmio;
565 		    sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
566 		    sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
567 		    return &sc->bmio_2;
568 		}
569 	    }
570 	    return 0;
571 
572 	default:
573 	    return 0;
574 	}
575 
576 	if (ATA_MASTERDEV(dev))
577 	    /* make the parent just pass through the allocation. */
578 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
579 				      SYS_RES_IOPORT, &myrid,
580 				      start, end, count, flags);
581 	else
582 	    /* we are using the parent resource directly. */
583 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
584 				      SYS_RES_IOPORT, &myrid,
585 				      start, end, count, flags);
586     }
587 
588     if (type == SYS_RES_IRQ) {
589 	if (*rid != 0)
590 	    return 0;
591 
592 	if (ATA_MASTERDEV(dev)) {
593 #ifdef __alpha__
594 	    return alpha_platform_alloc_ide_intr(channel);
595 #else
596 	    int irq = (channel == 0 ? 14 : 15);
597 
598 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
599 				      SYS_RES_IRQ, rid,
600 				      irq, irq, 1, flags & ~RF_SHAREABLE);
601 #endif
602 	} else {
603 	    /* primary and secondary channels share the same interrupt */
604 	    sc->irqcnt++;
605 	    if (!sc->irq)
606 		sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
607 					     SYS_RES_IRQ, rid, 0, ~0, 1, flags);
608 	    return sc->irq;
609 	}
610     }
611     return 0;
612 }
613 
614 static int
615 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
616 			 struct resource *r)
617 {
618     struct ata_pci_softc *sc = device_get_softc(dev);
619     int channel = ((struct ata_softc *)device_get_softc(child))->channel;
620     int myrid = 0;
621 
622     if (type == SYS_RES_IOPORT) {
623 	switch (rid) {
624 	case ATA_IOADDR_RID:
625 	    if (ATA_MASTERDEV(dev))
626 		myrid = 0;
627 	    else
628 		myrid = 0x10 + 8 * channel;
629 	    break;
630 
631 	case ATA_ALTADDR_RID:
632 	    if (ATA_MASTERDEV(dev))
633 		myrid = 0;
634 	    else
635 		myrid = 0x14 + 8 * channel;
636 	    break;
637 
638 	case ATA_BMADDR_RID:
639 	    return 0;
640 
641 	default:
642 	    return ENOENT;
643 	}
644 
645 	if (ATA_MASTERDEV(dev))
646 	    /* make the parent just pass through the allocation. */
647 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
648 					SYS_RES_IOPORT, myrid, r);
649 	else
650 	    /* we are using the parent resource directly. */
651 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
652 					SYS_RES_IOPORT, myrid, r);
653     }
654     if (type == SYS_RES_IRQ) {
655 	if (rid != 0)
656 	    return ENOENT;
657 
658 	if (ATA_MASTERDEV(dev)) {
659 #ifdef __alpha__
660 	    return alpha_platform_release_ide_intr(channel, r);
661 #else
662 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev),
663 					child, SYS_RES_IRQ, rid, r);
664 #endif
665 	}
666 	else {
667 	    if (--sc->irqcnt)
668 		return 0;
669 
670 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev),
671 					dev, SYS_RES_IRQ, rid, r);
672 	}
673     }
674     return EINVAL;
675 }
676 
677 static int
678 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
679 		   int flags, driver_intr_t *intr, void *arg,
680 		   void **cookiep)
681 {
682     if (ATA_MASTERDEV(dev)) {
683 #ifdef __alpha__
684 	return alpha_platform_setup_ide_intr(child, irq, intr, arg, cookiep);
685 #else
686 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
687 			      flags, intr, arg, cookiep);
688 #endif
689     }
690     else
691 	return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
692 			      flags, intr, arg, cookiep);
693 }
694 
695 static int
696 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
697 		      void *cookie)
698 {
699     if (ATA_MASTERDEV(dev)) {
700 #ifdef __alpha__
701 	return alpha_platform_teardown_ide_intr(child, irq, cookie);
702 #else
703 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
704 #endif
705     }
706     else
707 	return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
708 }
709 
710 static device_method_t ata_pci_methods[] = {
711     /* device interface */
712     DEVMETHOD(device_probe,		ata_pci_probe),
713     DEVMETHOD(device_attach,		ata_pci_attach),
714     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
715     DEVMETHOD(device_suspend,		bus_generic_suspend),
716     DEVMETHOD(device_resume,		bus_generic_resume),
717 
718     /* bus methods */
719     DEVMETHOD(bus_print_child,		ata_pci_print_child),
720     DEVMETHOD(bus_alloc_resource,	ata_pci_alloc_resource),
721     DEVMETHOD(bus_release_resource,	ata_pci_release_resource),
722     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
723     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
724     DEVMETHOD(bus_setup_intr,		ata_pci_setup_intr),
725     DEVMETHOD(bus_teardown_intr,	ata_pci_teardown_intr),
726     { 0, 0 }
727 };
728 
729 static driver_t ata_pci_driver = {
730     "atapci",
731     ata_pci_methods,
732     sizeof(struct ata_pci_softc),
733 };
734 
735 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
736 
737 static int
738 ata_pcisub_probe(device_t dev)
739 {
740     struct ata_softc *scp = device_get_softc(dev);
741     device_t *list;
742     int count, i;
743 
744     /* find channel number on this controller */
745     device_get_children(device_get_parent(dev), &list, &count);
746     for (i = 0; i < count; i++) {
747 	if (list[i] == dev)
748 	    scp->channel = i;
749     }
750 
751     scp->chiptype = pci_get_devid(device_get_parent(dev));
752 
753     return ata_probe(dev);
754 }
755 
756 static device_method_t ata_pcisub_methods[] = {
757     /* device interface */
758     DEVMETHOD(device_probe,	ata_pcisub_probe),
759     DEVMETHOD(device_attach,	ata_attach),
760     DEVMETHOD(device_detach,	ata_detach),
761     DEVMETHOD(device_resume,	ata_resume),
762     { 0, 0 }
763 };
764 
765 static driver_t ata_pcisub_driver = {
766     "ata",
767     ata_pcisub_methods,
768     sizeof(struct ata_softc),
769 };
770 
771 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
772 #endif
773 
774 static int
775 ata_probe(device_t dev)
776 {
777     struct ata_softc *scp = device_get_softc(dev);
778     struct resource *io = 0;
779     struct resource *altio = 0;
780     struct resource *bmio = 0;
781     int rid;
782     u_int32_t ioaddr, altioaddr, bmaddr;
783     int mask = 0;
784     u_int8_t status0, status1;
785 
786     if (!scp || scp->flags & ATA_ATTACHED)
787 	return ENXIO;
788 
789     /* initialize the softc basics */
790     scp->active = ATA_IDLE;
791     scp->dev = dev;
792     scp->devices = 0;
793 
794     rid = ATA_IOADDR_RID;
795     io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
796     			    ATA_IOSIZE, RF_ACTIVE);
797     if (!io)
798 	goto failure;
799     ioaddr = rman_get_start(io);
800 
801     rid = ATA_ALTADDR_RID;
802     altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
803 			       ATA_ALTIOSIZE, RF_ACTIVE);
804     if (altio) {
805 	if (scp->flags & ATA_USE_16BIT || ATA_MASTERDEV(device_get_parent(dev)))
806 	    altioaddr = rman_get_start(altio);
807 	else
808 	    altioaddr = rman_get_start(altio) + 0x02;
809     }
810     else
811 	altioaddr = ioaddr + ATA_PCCARD_ALTOFFSET;
812 
813     rid = ATA_BMADDR_RID;
814     bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
815     bmaddr = bmio ? rman_get_start(bmio) : 0;
816 
817     /* store the IO resources for eventual later release */
818     scp->r_io = io;
819     scp->r_altio = altio;
820     scp->r_bmio = bmio;
821 
822     /* store the physical IO addresse for easy access */
823     scp->ioaddr = ioaddr;
824     scp->altioaddr = altioaddr;
825     scp->bmaddr = bmaddr;
826 
827     if (bootverbose)
828 	ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
829 		   scp->ioaddr, scp->altioaddr, scp->bmaddr);
830 
831     /* do we have any signs of ATA/ATAPI HW being present ? */
832     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
833     DELAY(1);
834     status0 = inb(scp->ioaddr + ATA_STATUS);
835     if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
836 	mask |= 0x01;
837     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
838     DELAY(1);
839     status1 = inb(scp->ioaddr + ATA_STATUS);
840     if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
841 	mask |= 0x02;
842 
843     if (bootverbose)
844 	ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
845 		   mask, status0, status1);
846     if (!mask)
847 	goto failure;
848 
849     ata_reset(scp, &mask);
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; /* XXX */
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 (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
1199 	return;
1200 
1201 #if NATADISK > 0
1202     /* find & call the responsible driver if anything on the ATA queue */
1203     if (TAILQ_EMPTY(&scp->ata_queue)) {
1204 	if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1205 	    ad_start((struct ad_softc *)scp->dev_softc[0]);
1206 	if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1207 	    ad_start((struct ad_softc *)scp->dev_softc[1]);
1208     }
1209     if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
1210 	TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
1211 	scp->active = ATA_ACTIVE_ATA;
1212 	scp->running = ad_request;
1213 	if (ad_transfer(ad_request) == ATA_OP_CONTINUES)
1214 	    return;
1215     }
1216 
1217 #endif
1218 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1219     /* find & call the responsible driver if anything on the ATAPI queue */
1220     if (TAILQ_EMPTY(&scp->atapi_queue)) {
1221 	if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1222 	    atapi_start((struct atapi_softc *)scp->dev_softc[0]);
1223 	if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1224 	    atapi_start((struct atapi_softc *)scp->dev_softc[1]);
1225     }
1226     if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
1227 	TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
1228 	scp->active = ATA_ACTIVE_ATAPI;
1229 	scp->running = atapi_request;
1230 	atapi_transfer(atapi_request);
1231 	return;
1232     }
1233 #endif
1234     scp->active = ATA_IDLE;
1235 }
1236 
1237 void
1238 ata_reset(struct ata_softc *scp, int *mask)
1239 {
1240     int timeout;
1241     u_int8_t a, b, ostat0, ostat1;
1242     u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY;
1243 
1244     /* get the current status of the devices */
1245     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1246     DELAY(10);
1247     ostat1 = inb(scp->ioaddr + ATA_STATUS);
1248     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1249     DELAY(10);
1250     ostat0 = inb(scp->ioaddr + ATA_STATUS);
1251 
1252     /* in some setups we dont want to test for a slave */
1253     if (scp->flags & ATA_NO_SLAVE)
1254 	*mask &= ~0x02;
1255 
1256     if (bootverbose)
1257 	ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
1258 		   *mask, ostat0, ostat1);
1259 
1260     /* reset channel */
1261     outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1262     DELAY(10000);
1263     outb(scp->altioaddr, ATA_A_IDS);
1264     DELAY(100000);
1265     inb(scp->ioaddr + ATA_ERROR);
1266     scp->devices = 0;
1267 
1268     /* wait for BUSY to go inactive */
1269     for (timeout = 0; timeout < 310000; timeout++) {
1270 	if (status0 & ATA_S_BUSY) {
1271             outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1272             DELAY(10);
1273             status0 = inb(scp->ioaddr + ATA_STATUS);
1274             if (!(status0 & ATA_S_BUSY)) {
1275                 /* check for ATAPI signature while its still there */
1276 		a = inb(scp->ioaddr + ATA_CYL_LSB);
1277 		b = inb(scp->ioaddr + ATA_CYL_MSB);
1278 		if (bootverbose)
1279 		    ata_printf(scp, ATA_MASTER,
1280 			       "ATAPI probe a=%02x b=%02x\n", a, b);
1281 		if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1282                     scp->devices |= ATA_ATAPI_MASTER;
1283             }
1284         }
1285         if (status1 & ATA_S_BUSY) {
1286             outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1287             DELAY(10);
1288             status1 = inb(scp->ioaddr + ATA_STATUS);
1289             if (!(status1 & ATA_S_BUSY)) {
1290                 /* check for ATAPI signature while its still there */
1291 		a = inb(scp->ioaddr + ATA_CYL_LSB);
1292 		b = inb(scp->ioaddr + ATA_CYL_MSB);
1293 		if (bootverbose)
1294 		    ata_printf(scp, ATA_SLAVE,
1295 			       "ATAPI probe a=%02x b=%02x\n", a, b);
1296 		if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1297                     scp->devices |= ATA_ATAPI_SLAVE;
1298             }
1299         }
1300 	if (*mask == 0x01)      /* wait for master only */
1301 	    if (!(status0 & ATA_S_BUSY))
1302 		break;
1303 	if (*mask == 0x02)      /* wait for slave only */
1304 	    if (!(status1 & ATA_S_BUSY))
1305 		break;
1306 	if (*mask == 0x03)      /* wait for both master & slave */
1307 	    if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1308 		break;
1309 	DELAY(100);
1310     }
1311     DELAY(10);
1312     outb(scp->altioaddr, ATA_A_4BIT);
1313 
1314     if (status0 & ATA_S_BUSY)
1315 	*mask &= ~0x01;
1316     if (status1 & ATA_S_BUSY)
1317 	*mask &= ~0x02;
1318     if (bootverbose)
1319 	ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1320 		   *mask, status0, status1);
1321     if (!*mask)
1322 	return;
1323 
1324     if (*mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
1325         outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1326         DELAY(10);
1327 	outb(scp->ioaddr + ATA_ERROR, 0x58);
1328 	outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
1329 	a = inb(scp->ioaddr + ATA_ERROR);
1330 	b = inb(scp->ioaddr + ATA_CYL_LSB);
1331 	if (bootverbose)
1332 	    ata_printf(scp, ATA_MASTER, "ATA probe a=%02x b=%02x\n", a, b);
1333         if (a != 0x58 && b == 0xa5)
1334             scp->devices |= ATA_ATA_MASTER;
1335     }
1336     if (*mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
1337         outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1338         DELAY(10);
1339 	outb(scp->ioaddr + ATA_ERROR, 0x58);
1340 	outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
1341 	a = inb(scp->ioaddr + ATA_ERROR);
1342 	b = inb(scp->ioaddr + ATA_CYL_LSB);
1343 	if (bootverbose)
1344 	    ata_printf(scp, ATA_SLAVE, "ATA probe a=%02x b=%02x\n", a, b);
1345         if (a != 0x58 && b == 0xa5)
1346             scp->devices |= ATA_ATA_SLAVE;
1347     }
1348     if (bootverbose)
1349 	ata_printf(scp, -1, "devices=%02x\n", scp->devices);
1350 }
1351 
1352 int
1353 ata_reinit(struct ata_softc *scp)
1354 {
1355     int mask = 0, omask;
1356 
1357     scp->active = ATA_REINITING;
1358     scp->running = NULL;
1359     if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
1360 	mask |= 0x01;
1361     if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
1362 	mask |= 0x02;
1363     if (mask) {
1364 	omask = mask;
1365         ata_printf(scp, -1, "resetting devices .. ");
1366 	ata_reset(scp, &mask);
1367 	if (omask != mask)
1368 	    printf(" device dissapeared! %d ", omask & ~mask);
1369 
1370 #if NATADISK > 0
1371 	if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1372 	    ad_reinit((struct ad_softc *)scp->dev_softc[0]);
1373 	if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1374 	    ad_reinit((struct ad_softc *)scp->dev_softc[1]);
1375 #endif
1376 #if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1377 	if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1378 	    atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
1379 	if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1380 	    atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
1381 #endif
1382 	printf("done\n");
1383     }
1384     scp->active = ATA_IDLE;
1385     ata_start(scp);
1386     return 0;
1387 }
1388 
1389 static int
1390 ata_service(struct ata_softc *scp)
1391 {
1392     /* do we have a SERVICE request from the drive ? */
1393     if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
1394 	outb(scp->bmaddr + ATA_BMSTAT_PORT, ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
1395 #if NATADISK > 0
1396 	if ((inb(scp->ioaddr + ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
1397 	    if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0])
1398 		return ad_service((struct ad_softc *)scp->dev_softc[0], 0);
1399 	}
1400 	else {
1401 	    if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1])
1402 		return ad_service((struct ad_softc *)scp->dev_softc[1], 0);
1403 	}
1404 #endif
1405     }
1406     return ATA_OP_FINISHED;
1407 }
1408 
1409 int
1410 ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
1411 {
1412     int timeout = 0;
1413     int statio = scp->ioaddr + ATA_STATUS;
1414 
1415     DELAY(1);
1416     while (timeout < 5000000) {	/* timeout 5 secs */
1417 	scp->status = inb(statio);
1418 
1419 	/* if drive fails status, reselect the drive just to be sure */
1420 	if (scp->status == 0xff) {
1421 	    ata_printf(scp, device, "no status, reselecting device\n");
1422 	    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1423 	    DELAY(1);
1424 	    scp->status = inb(statio);
1425 	}
1426 
1427 	/* are we done ? */
1428 	if (!(scp->status & ATA_S_BUSY))
1429 	    break;
1430 
1431 	if (timeout > 1000) {
1432 	    timeout += 1000;
1433 	    DELAY(1000);
1434 	}
1435 	else {
1436 	    timeout += 10;
1437 	    DELAY(10);
1438 	}
1439     }
1440     if (scp->status & ATA_S_ERROR)
1441 	scp->error = inb(scp->ioaddr + ATA_ERROR);
1442     if (timeout >= 5000000)
1443 	return -1;
1444     if (!mask)
1445 	return (scp->status & ATA_S_ERROR);
1446 
1447     /* Wait 50 msec for bits wanted. */
1448     timeout = 5000;
1449     while (timeout--) {
1450 	scp->status = inb(statio);
1451 	if ((scp->status & mask) == mask) {
1452 	    if (scp->status & ATA_S_ERROR)
1453 		scp->error = inb(scp->ioaddr + ATA_ERROR);
1454 	    return (scp->status & ATA_S_ERROR);
1455 	}
1456 	DELAY (10);
1457     }
1458     return -1;
1459 }
1460 
1461 int
1462 ata_command(struct ata_softc *scp, int device, u_int8_t command,
1463 	   u_int16_t cylinder, u_int8_t head, u_int8_t sector,
1464 	   u_int8_t count, u_int8_t feature, int flags)
1465 {
1466     int error = 0;
1467 #ifdef ATA_DEBUG
1468     ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1469 	       "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
1470 	       scp->ioaddr, command, cylinder, head, sector,
1471 	       count, feature, flags);
1472 #endif
1473 
1474     /* disable interrupt from device */
1475     if (scp->flags & ATA_QUEUED)
1476 	outb(scp->altioaddr, ATA_A_IDS | ATA_A_4BIT);
1477 
1478     /* select device */
1479     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1480 
1481     /* ready to issue command ? */
1482     if (ata_wait(scp, device, 0) < 0) {
1483 	ata_printf(scp, device,
1484 		   "timeout waiting to give command=%02x s=%02x e=%02x\n",
1485 		   command, scp->status, scp->error);
1486 	return -1;
1487     }
1488 
1489     outb(scp->ioaddr + ATA_FEATURE, feature);
1490     outb(scp->ioaddr + ATA_COUNT, count);
1491     outb(scp->ioaddr + ATA_SECTOR, sector);
1492     outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1493     outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1494     outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1495 
1496     switch (flags) {
1497     case ATA_WAIT_INTR:
1498 	scp->active = ATA_WAIT_INTR;
1499 	asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1500 	outb(scp->ioaddr + ATA_CMD, command);
1501 
1502 	/* enable interrupt */
1503 	if (scp->flags & ATA_QUEUED)
1504 	    outb(scp->altioaddr, ATA_A_4BIT);
1505 
1506 	if (await(PRIBIO, 10 * hz)) {
1507 	    ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1508 	    scp->active = ATA_IDLE;
1509 	    error = -1;
1510 	}
1511 	break;
1512 
1513     case ATA_WAIT_READY:
1514 	if (scp->active != ATA_REINITING)
1515 	    scp->active = ATA_WAIT_READY;
1516 	outb(scp->ioaddr + ATA_CMD, command);
1517 	if (ata_wait(scp, device, ATA_S_READY) < 0) {
1518 	    ata_printf(scp, device,
1519 		       "timeout waiting for command=%02x s=%02x e=%02x\n",
1520 		       command, scp->status, scp->error);
1521 	    error = -1;
1522 	}
1523 	if (scp->active != ATA_REINITING)
1524 	    scp->active = ATA_IDLE;
1525 	break;
1526 
1527     case ATA_IMMEDIATE:
1528 	outb(scp->ioaddr + ATA_CMD, command);
1529 	break;
1530 
1531     default:
1532 	ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1533 		   active2str(flags));
1534     }
1535     /* enable interrupt */
1536     if (scp->flags & ATA_QUEUED)
1537 	outb(scp->altioaddr, ATA_A_4BIT);
1538     return error;
1539 }
1540 
1541 int
1542 ata_get_lun(u_int32_t *map)
1543 {
1544     int lun = ffs(~*map) - 1;
1545 
1546     *map |= (1 << lun);
1547     return lun;
1548 }
1549 
1550 void
1551 ata_free_lun(u_int32_t *map, int lun)
1552 {
1553     *map &= ~(1 << lun);
1554 }
1555 
1556 int
1557 ata_printf(struct ata_softc *scp, int device, const char * fmt, ...)
1558 {
1559     va_list ap;
1560     int ret;
1561 
1562     if (device == -1)
1563 	ret = printf("ata%d: ", device_get_unit(scp->dev));
1564     else
1565 	ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1566 		     (device == ATA_MASTER) ? "master" : "slave");
1567     va_start(ap, fmt);
1568     ret += vprintf(fmt, ap);
1569     va_end(ap);
1570     return ret;
1571 }
1572 
1573 char *
1574 ata_mode2str(int mode)
1575 {
1576     switch (mode) {
1577     case ATA_PIO: return "BIOSPIO";
1578     case ATA_PIO0: return "PIO0";
1579     case ATA_PIO1: return "PIO1";
1580     case ATA_PIO2: return "PIO2";
1581     case ATA_PIO3: return "PIO3";
1582     case ATA_PIO4: return "PIO4";
1583     case ATA_WDMA2: return "WDMA2";
1584     case ATA_UDMA2: return "UDMA33";
1585     case ATA_UDMA4: return "UDMA66";
1586     case ATA_UDMA5: return "UDMA100";
1587     case ATA_DMA: return "BIOSDMA";
1588     default: return "???";
1589     }
1590 }
1591 
1592 int
1593 ata_pio2mode(int pio)
1594 {
1595     switch (pio) {
1596     default:
1597     case 0: return ATA_PIO0;
1598     case 1: return ATA_PIO1;
1599     case 2: return ATA_PIO2;
1600     case 3: return ATA_PIO3;
1601     case 4: return ATA_PIO4;
1602     }
1603 }
1604 
1605 int
1606 ata_pmode(struct ata_params *ap)
1607 {
1608     if (ap->atavalid & ATA_FLAG_64_70) {
1609 	if (ap->apiomodes & 2)
1610 	    return 4;
1611 	if (ap->apiomodes & 1)
1612 	    return 3;
1613     }
1614     if (ap->opiomode == 2)
1615 	return 2;
1616     if (ap->opiomode == 1)
1617 	return 1;
1618     if (ap->opiomode == 0)
1619 	return 0;
1620     return -1;
1621 }
1622 
1623 int
1624 ata_wmode(struct ata_params *ap)
1625 {
1626     if (ap->wdmamodes & 4)
1627 	return 2;
1628     if (ap->wdmamodes & 2)
1629 	return 1;
1630     if (ap->wdmamodes & 1)
1631 	return 0;
1632     return -1;
1633 }
1634 
1635 int
1636 ata_umode(struct ata_params *ap)
1637 {
1638     if (ap->atavalid & ATA_FLAG_88) {
1639 	if (ap->udmamodes & 0x20)
1640 	    return 5;
1641 	if (ap->udmamodes & 0x10)
1642 	    return 4;
1643 	if (ap->udmamodes & 0x08)
1644 	    return 3;
1645 	if (ap->udmamodes & 0x04)
1646 	    return 2;
1647 	if (ap->udmamodes & 0x02)
1648 	    return 1;
1649 	if (ap->udmamodes & 0x01)
1650 	    return 0;
1651     }
1652     return -1;
1653 }
1654 
1655 static char *
1656 active2str(int active)
1657 {
1658     static char buf[8];
1659 
1660     switch (active) {
1661     case ATA_IDLE:
1662 	return("ATA_IDLE");
1663     case ATA_IMMEDIATE:
1664 	return("ATA_IMMEDIATE");
1665     case ATA_WAIT_INTR:
1666 	return("ATA_WAIT_INTR");
1667     case ATA_WAIT_READY:
1668 	return("ATA_WAIT_READY");
1669     case ATA_ACTIVE:
1670 	return("ATA_ACTIVE");
1671     case ATA_ACTIVE_ATA:
1672 	return("ATA_ACTIVE_ATA");
1673     case ATA_ACTIVE_ATAPI:
1674 	return("ATA_ACTIVE_ATAPI");
1675     case ATA_REINITING:
1676 	return("ATA_REINITING");
1677     default:
1678 	sprintf(buf, "0x%02x", active);
1679 	return buf;
1680     }
1681 }
1682 
1683 static void
1684 bswap(int8_t *buf, int len)
1685 {
1686     u_int16_t *ptr = (u_int16_t*)(buf + len);
1687 
1688     while (--ptr >= (u_int16_t*)buf)
1689 	*ptr = ntohs(*ptr);
1690 }
1691 
1692 static void
1693 btrim(int8_t *buf, int len)
1694 {
1695     int8_t *ptr;
1696 
1697     for (ptr = buf; ptr < buf+len; ++ptr)
1698 	if (!*ptr)
1699 	    *ptr = ' ';
1700     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1701 	*ptr = 0;
1702 }
1703 
1704 static void
1705 bpack(int8_t *src, int8_t *dst, int len)
1706 {
1707     int i, j, blank;
1708 
1709     for (i = j = blank = 0 ; i < len; i++) {
1710 	if (blank && src[i] == ' ') continue;
1711 	if (blank && src[i] != ' ') {
1712 	    dst[j++] = src[i];
1713 	    blank = 0;
1714 	    continue;
1715 	}
1716 	if (src[i] == ' ') {
1717 	    blank = 1;
1718 	    if (i == 0)
1719 		continue;
1720 	}
1721 	dst[j++] = src[i];
1722     }
1723     if (j < len)
1724 	dst[j] = 0x00;
1725 }
1726 
1727 static void
1728 ata_change_mode(struct ata_softc *scp, int device, int mode)
1729 {
1730     int s = splbio();
1731 
1732     while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
1733 	tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1734 
1735     ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1736 		mode < ATA_DMA ?  -1 : ata_wmode(ATA_PARAM(scp, device)),
1737 		mode < ATA_DMA ?  -1 : ata_umode(ATA_PARAM(scp, device)));
1738     scp->active = ATA_IDLE;
1739     ata_start(scp);
1740     splx(s);
1741 }
1742 
1743 static int
1744 sysctl_hw_ata(SYSCTL_HANDLER_ARGS)
1745 {
1746     struct ata_softc *scp;
1747     int ctlr, error, i;
1748 
1749     /* readout internal state */
1750     bzero(ata_conf, sizeof(ata_conf));
1751     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1752 	if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1753 	    continue;
1754 	for (i = 0; i < 2; i++) {
1755 	    if (!scp->dev_softc[i])
1756 		strcat(ata_conf, "---,");
1757 	    else if (scp->mode[i] >= ATA_DMA)
1758 		strcat(ata_conf, "dma,");
1759 	    else
1760 		strcat(ata_conf, "pio,");
1761 	}
1762     }
1763     error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1764     if (error == 0 && req->newptr != NULL) {
1765 	char *ptr = ata_conf;
1766 
1767         /* update internal state */
1768 	i = 0;
1769         while (*ptr) {
1770 	    if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1771 		if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1772 		    scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1773 		    ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1774 	    }
1775 	    else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1776 		if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1777 		    scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1778 		    ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1779 	    }
1780 	    else if (strncmp(ptr, "---", 3))
1781 		break;
1782 	    ptr+=3;
1783 	    if (*ptr++ != ',' ||
1784 		++i > (devclass_get_maxunit(ata_devclass) << 1))
1785 		break;
1786         }
1787     }
1788     return error;
1789 }
1790 SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1791             0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1792 
1793 static void
1794 ata_init(void)
1795 {
1796     /* register boot attach to be run when interrupts are enabled */
1797     if (!(ata_delayed_attach = (struct intr_config_hook *)
1798 			       malloc(sizeof(struct intr_config_hook),
1799 				      M_TEMP, M_NOWAIT | M_ZERO))) {
1800 	printf("ata: malloc of delayed attach hook failed\n");
1801 	return;
1802     }
1803 
1804     ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1805     if (config_intrhook_establish(ata_delayed_attach) != 0) {
1806 	printf("ata: config_intrhook_establish failed\n");
1807 	free(ata_delayed_attach, M_TEMP);
1808     }
1809 }
1810 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
1811