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