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