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