ata-all.c revision 56278
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 56278 2000-01-19 07:25:46Z sos $
29 */
30
31#include "ata.h"
32#include "apm.h"
33#include "isa.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 <vm/vm.h>
51#include <vm/pmap.h>
52#include <machine/resource.h>
53#include <machine/bus.h>
54#include <sys/rman.h>
55#if NPCI > 0
56#include <pci/pcivar.h>
57#include <pci/pcireg.h>
58#endif
59#include <isa/isavar.h>
60#include <isa/isareg.h>
61#include <machine/clock.h>
62#ifdef __i386__
63#include <machine/smp.h>
64#include <i386/isa/intr_machdep.h>
65#endif
66#if NAPM > 0
67#include <machine/apm_bios.h>
68#endif
69#include <dev/ata/ata-all.h>
70#include <dev/ata/ata-disk.h>
71#include <dev/ata/atapi-all.h>
72
73/* misc defines */
74#if SMP == 0
75#define isa_apic_irq(x) x
76#endif
77#define IOMASK	0xfffffffc
78
79/* prototypes */
80static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
81static void ata_attach(void *);
82static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
83static void ataintr(void *);
84static int8_t *active2str(int32_t);
85static void bswap(int8_t *, int32_t);
86static void btrim(int8_t *, int32_t);
87static void bpack(int8_t *, int8_t *, int32_t);
88
89/* local vars */
90static int32_t atanlun = 2;
91static struct intr_config_hook *ata_attach_hook = NULL;
92static devclass_t ata_devclass;
93struct ata_softc *atadevices[MAXATA];
94MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
95
96#if NISA > 0
97static struct isa_pnp_id ata_ids[] = {
98    {0x0006d041,	"Generic ESDI/IDE/ATA controller"},	/* PNP0600 */
99    {0x0106d041,	"Plus Hardcard II"},			/* PNP0601 */
100    {0x0206d041,	"Plus Hardcard IIXL/EZ"},		/* PNP0602 */
101    {0x0306d041,	"Generic ATA"},				/* PNP0603 */
102    {0}
103};
104
105static int
106ata_isaprobe(device_t dev)
107{
108    struct resource *port;
109    int rid;
110    int32_t ctlr, res;
111    int32_t lun;
112
113    /* Check isapnp ids */
114    if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
115	return (ENXIO);
116
117    /* Allocate the port range */
118    rid = 0;
119    port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
120    if (!port)
121	return (ENOMEM);
122
123    /* check if allready in use by a PCI device */
124    for (ctlr = 0; ctlr < atanlun; ctlr++) {
125	if (atadevices[ctlr] && atadevices[ctlr]->ioaddr==rman_get_start(port)){
126	    printf("ata-isa%d: already registered as ata%d\n",
127		   device_get_unit(dev), ctlr);
128	    bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
129	    return ENXIO;
130	}
131    }
132
133    lun = 0;
134    res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT,
135		    0, dev, &lun);
136
137    bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
138
139    if (res) {
140	isa_set_portsize(dev, res);
141	*(int *)device_get_softc(dev) = lun;
142        atadevices[lun]->flags |= ATA_USE_16BIT;
143	return 0;
144    }
145    return ENXIO;
146}
147
148static int
149ata_isaattach(device_t dev)
150{
151    struct resource *port;
152    struct resource *irq;
153    void *ih;
154    int rid;
155
156    /* Allocate the port range and interrupt */
157    rid = 0;
158    port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
159    if (!port)
160	return (ENOMEM);
161
162    rid = 0;
163    irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
164    if (!irq) {
165	bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
166	return (ENOMEM);
167    }
168    return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr,
169			  atadevices[*(int *)device_get_softc(dev)], &ih);
170}
171
172static device_method_t ata_isa_methods[] = {
173    /* Device interface */
174    DEVMETHOD(device_probe,	ata_isaprobe),
175    DEVMETHOD(device_attach,	ata_isaattach),
176    { 0, 0 }
177};
178
179static driver_t ata_isa_driver = {
180    "ata",
181    ata_isa_methods,
182    sizeof(int),
183};
184
185DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
186#endif
187
188#if NPCI > 0
189static const char *
190ata_pcimatch(device_t dev)
191{
192    if (pci_get_class(dev) != PCIC_STORAGE)
193	return NULL;
194
195    switch (pci_get_devid(dev)) {
196    /* supported chipsets */
197    case 0x12308086:
198	return "Intel PIIX ATA controller";
199
200    case 0x70108086:
201	return "Intel PIIX3 ATA controller";
202
203    case 0x71118086:
204    case 0x71998086:
205	return "Intel PIIX4 ATA-33 controller";
206
207    case 0x24118086:
208	return "Intel ICH ATA-66 controller";
209
210    case 0x24218086:
211	return "Intel ICH0 ATA-33 controller";
212
213    case 0x522910b9:
214	return "AcerLabs Aladdin ATA-33 controller";
215
216    case 0x05711106: /* 82c586 & 82c686 */
217	if (ata_find_dev(dev, 0x05861106))
218	    return "VIA 82C586 ATA-33 controller";
219	if (ata_find_dev(dev, 0x05961106))
220	    return "VIA 82C596 ATA-33 controller";
221	if (ata_find_dev(dev, 0x06861106))
222	    return "VIA 82C686 ATA-66 controller";
223	return "VIA Apollo ATA controller";
224
225    case 0x55131039:
226	return "SiS 5591 ATA-33 controller";
227
228    case 0x74091022:
229	return "AMD 756 ATA-66 controller";
230
231    case 0x4d33105a:
232	return "Promise ATA-33 controller";
233
234    case 0x4d38105a:
235	return "Promise ATA-66 controller";
236
237    case 0x00041103:
238	return "HighPoint HPT366 ATA-66 controller";
239
240   /* unsupported but known chipsets, generic DMA only */
241    case 0x06401095:
242	return "CMD 640 ATA controller (generic mode)";
243
244    case 0x06461095:
245	return "CMD 646 ATA controller (generic mode)";
246
247    case 0xc6931080:
248	return "Cypress 82C693 ATA controller (generic mode)";
249
250    case 0x01021078:
251	return "Cyrix 5530 ATA controller (generic mode)";
252
253    default:
254	if (pci_get_class(dev) == PCIC_STORAGE &&
255	    (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
256	    return "Unknown PCI ATA controller (generic mode)";
257    }
258    return NULL;
259}
260
261static int
262ata_pciprobe(device_t dev)
263{
264    const char *desc = ata_pcimatch(dev);
265
266    if (desc) {
267	device_set_desc(dev, desc);
268	return 0;
269    }
270    else
271	return ENXIO;
272}
273
274static int
275ata_pciattach(device_t dev)
276{
277    int unit = device_get_unit(dev);
278    struct ata_softc *scp;
279    u_int32_t type;
280    u_int8_t class, subclass;
281    u_int32_t cmd;
282    int32_t iobase_1, iobase_2, altiobase_1, altiobase_2;
283    int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2;
284    struct resource *irq = NULL;
285    int32_t lun;
286
287    /* set up vendor-specific stuff */
288    type = pci_get_devid(dev);
289    class = pci_get_class(dev);
290    subclass = pci_get_subclass(dev);
291    cmd = pci_read_config(dev, PCIR_COMMAND, 4);
292
293#ifdef ATA_DEBUG
294    printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n",
295	   unit, type, class, subclass, cmd, pci_get_progif(dev));
296#endif
297
298    if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
299	iobase_1 = IO_WD1;
300	altiobase_1 = iobase_1 + ATA_ALTPORT;
301	irq1 = 14;
302    }
303    else {
304	iobase_1 = pci_read_config(dev, 0x10, 4) & IOMASK;
305	altiobase_1 = pci_read_config(dev, 0x14, 4) & IOMASK;
306	irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
307    }
308
309    if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
310	iobase_2 = IO_WD2;
311	altiobase_2 = iobase_2 + ATA_ALTPORT;
312	irq2 = 15;
313    }
314    else {
315	iobase_2 = pci_read_config(dev, 0x18, 4) & IOMASK;
316	altiobase_2 = pci_read_config(dev, 0x1c, 4) & IOMASK;
317	irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
318    }
319
320    /* is this controller busmaster DMA capable ? */
321    if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
322	/* is busmastering support turned on ? */
323	if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
324	    /* is there a valid port range to connect to ? */
325	    if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK)) {
326		bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
327		printf("ata-pci%d: Busmastering DMA supported\n", unit);
328	    }
329	    else
330		printf("ata-pci%d: Busmastering DMA not configured\n", unit);
331	}
332	else
333	    printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
334    }
335    else {
336    	if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
337	    /* Promise and HPT366 controllers support busmastering DMA */
338	    bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK;
339	    bmaddr_2 = (pci_read_config(dev, 0x20, 4) & IOMASK)+ATA_BM_OFFSET1;
340	    printf("ata-pci%d: Busmastering DMA supported\n", unit);
341	}
342	else {
343	    /* we dont know this controller, no busmastering DMA */
344	    printf("ata-pci%d: Busmastering DMA not supported\n", unit);
345	}
346    }
347
348    /* do extra chipset specific setups */
349    switch (type) {
350    case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
351	pci_write_config(dev, 0x53,
352			 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
353	break;
354
355    case 0x4d33105a:
356    case 0x4d38105a: /* Promise's need burst mode to be turned on */
357	outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
358	break;
359
360    case 0x05711106:
361    case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */
362	/* set prefetch, postwrite */
363	pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
364
365	/* set fifo configuration half'n'half */
366	pci_write_config(dev, 0x43,
367			 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
368
369	/* set status register read retry */
370	pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
371
372	/* set DMA read & end-of-sector fifo flush */
373	pci_write_config(dev, 0x46,
374			 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
375
376	/* set sector size */
377	pci_write_config(dev, 0x60, DEV_BSIZE, 2);
378	pci_write_config(dev, 0x68, DEV_BSIZE, 2);
379
380	/* prepare for ATA-66 on the 82C686 */
381	if (ata_find_dev(dev, 0x06861106))
382	    pci_write_config(dev, 0x50,
383			     pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
384	break;
385
386    case 0x00041103: /* HighPoint HPT366 controller */
387	printf("hpt366: cache_line_size=0x%02x latency_timer=0x%02x min_grant=0x%02x max_latency=0x%02x\n",
388	pci_read_config(dev, 0x0c, 1), pci_read_config(dev, 0x0d, 1),
389	pci_read_config(dev, 0x3e, 1), pci_read_config(dev, 0x3f, 1));
390
391    }
392
393    /* now probe the addresse found for "real" ATA/ATAPI hardware */
394    lun = 0;
395    if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
396	scp = atadevices[lun];
397	scp->chiptype = type;
398	if (iobase_1 == IO_WD1)
399#ifdef __i386__
400	    inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
401			&bio_imask, INTR_EXCL);
402#endif
403#ifdef __alpha__
404	    alpha_platform_setup_ide_intr(0, ataintr, scp);
405#endif
406	else {
407	    int rid = 0;
408	    void *ih;
409
410	    if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
411					   RF_SHAREABLE | RF_ACTIVE)))
412		printf("ata_pciattach: Unable to alloc interrupt\n");
413	    bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
414	}
415	printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
416	       lun, iobase_1, isa_apic_irq(irq1), unit);
417    }
418    lun = 1;
419    if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
420	scp = atadevices[lun];
421	scp->chiptype = type;
422	if (iobase_2 == IO_WD2)
423#ifdef __i386__
424	    inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
425			&bio_imask, INTR_EXCL);
426#endif
427#ifdef __alpha__
428	    alpha_platform_setup_ide_intr(1, ataintr, scp);
429#endif
430	else {
431	    int rid = 0;
432	    void *ih;
433
434	    if (irq1 != irq2 || irq == NULL) {
435	  	if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
436					       RF_SHAREABLE | RF_ACTIVE)))
437		    printf("ata_pciattach: Unable to alloc interrupt\n");
438	    }
439	    bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
440	}
441	printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
442	       lun, iobase_2, isa_apic_irq(irq2), unit);
443    }
444    return 0;
445}
446
447int32_t
448ata_find_dev(device_t dev, int32_t type)
449{
450    device_t *children, child;
451    int nchildren, i;
452
453    if (device_get_children(device_get_parent(dev), &children, &nchildren))
454	return 0;
455
456    for (i = 0; i < nchildren; i++) {
457	child = children[i];
458
459	/* check that it's on the same silicon and the device we want */
460	if (pci_get_slot(dev) == pci_get_slot(child) &&
461	    pci_get_vendor(child) == (type & 0xffff) &&
462	    pci_get_device(child) == ((type & 0xffff0000)>>16)) {
463	    free(children, M_TEMP);
464	    return 1;
465	}
466    }
467    free(children, M_TEMP);
468    return 0;
469}
470
471static device_method_t ata_pci_methods[] = {
472    /* Device interface */
473    DEVMETHOD(device_probe,	ata_pciprobe),
474    DEVMETHOD(device_attach,	ata_pciattach),
475    { 0, 0 }
476};
477
478static driver_t ata_pci_driver = {
479    "ata-pci",
480    ata_pci_methods,
481    sizeof(int),
482};
483
484DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0);
485#endif
486
487static int32_t
488ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
489	  device_t dev, int32_t *unit)
490{
491    struct ata_softc *scp;
492    int32_t lun, mask = 0;
493    u_int8_t status0, status1;
494
495    if (atanlun > MAXATA) {
496	printf("ata: unit out of range(%d)\n", atanlun);
497	return 0;
498    }
499
500    /* check if this is located at one of the std addresses */
501    if (ioaddr == IO_WD1)
502	lun = 0;
503    else if (ioaddr == IO_WD2)
504	lun = 1;
505    else
506	lun = atanlun++;
507
508    if ((scp = atadevices[lun])) {
509	printf("ata%d: unit already attached\n", lun);
510	return 0;
511    }
512    scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT);
513    if (scp == NULL) {
514	printf("ata%d: failed to allocate driver storage\n", lun);
515	return 0;
516    }
517    bzero(scp, sizeof(struct ata_softc));
518
519    scp->ioaddr = ioaddr;
520    scp->altioaddr = altioaddr;
521    scp->bmaddr = bmaddr;
522    scp->lun = lun;
523    scp->unit = *unit;
524    scp->active = ATA_IDLE;
525
526    if (bootverbose)
527	printf("ata%d: iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
528	       scp->lun, scp->ioaddr, scp->altioaddr, scp->bmaddr);
529
530    /* do we have any signs of ATA/ATAPI HW being present ? */
531    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
532    DELAY(1);
533    status0 = inb(scp->ioaddr + ATA_STATUS);
534    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
535    DELAY(1);
536    status1 = inb(scp->ioaddr + ATA_STATUS);
537    if ((status0 & 0xf8) != 0xf8)
538	mask |= 0x01;
539    if ((status1 & 0xf8) != 0xf8)
540	mask |= 0x02;
541    if (bootverbose)
542	printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
543	       scp->lun, mask, status0, status1);
544    if (!mask) {
545	free(scp, M_DEVBUF);
546	return 0;
547    }
548    ata_reset(scp, &mask);
549    if (!mask) {
550	free(scp, M_DEVBUF);
551	return 0;
552    }
553    /*
554     * OK, we have at least one device on the chain,
555     * check for ATAPI signatures, if none check if its
556     * a good old ATA device.
557     */
558    outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
559    DELAY(1);
560    if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
561	inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
562	scp->devices |= ATA_ATAPI_MASTER;
563    }
564    outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
565    DELAY(1);
566    if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
567	inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
568	scp->devices |= ATA_ATAPI_SLAVE;
569    }
570    if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
571	outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
572	DELAY(1);
573	outb(scp->ioaddr + ATA_ERROR, 0x58);
574	outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
575	if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
576	    inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
577	    scp->devices |= ATA_ATA_MASTER;
578	}
579    }
580    if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
581	outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
582	DELAY(1);
583	outb(scp->ioaddr + ATA_ERROR, 0x58);
584	outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
585	if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
586	    inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
587	    scp->devices |= ATA_ATA_SLAVE;
588	}
589    }
590    if (bootverbose)
591	printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices);
592    if (!scp->devices) {
593	free(scp, M_DEVBUF);
594	return 0;
595    }
596    TAILQ_INIT(&scp->ata_queue);
597    TAILQ_INIT(&scp->atapi_queue);
598    *unit = scp->lun;
599    scp->dev = dev;
600    atadevices[scp->lun] = scp;
601
602    /* register callback for when interrupts are enabled */
603    if (!ata_attach_hook) {
604	if (!(ata_attach_hook = (struct intr_config_hook *)
605				malloc(sizeof(struct intr_config_hook),
606				M_TEMP, M_NOWAIT))) {
607            printf("ata: ERROR malloc attach_hook failed\n");
608            return 0;
609	}
610	bzero(ata_attach_hook, sizeof(struct intr_config_hook));
611
612	ata_attach_hook->ich_func = ata_attach;
613	if (config_intrhook_establish(ata_attach_hook) != 0) {
614            printf("ata: config_intrhook_establish failed\n");
615            free(ata_attach_hook, M_TEMP);
616	}
617    }
618#if NAPM > 0
619    scp->resume_hook.ah_fun = (void *)ata_reinit;
620    scp->resume_hook.ah_arg = scp;
621    scp->resume_hook.ah_name = "ATA driver";
622    scp->resume_hook.ah_order = APM_MID_ORDER;
623    apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
624#endif
625    return ATA_IOSIZE;
626}
627
628void
629ata_attach(void *dummy)
630{
631    int32_t ctlr;
632
633    /*
634     * run through atadevices[] and look for real ATA & ATAPI devices
635     * using the hints we found in the early probe to avoid probing
636     * of non-exsistent devices and thereby long delays
637     */
638    for (ctlr=0; ctlr<MAXATA; ctlr++) {
639	if (!atadevices[ctlr]) continue;
640
641	if (atadevices[ctlr]->devices & ATA_ATA_SLAVE)
642	    if (ata_getparam(atadevices[ctlr], ATA_SLAVE, ATA_C_ATA_IDENTIFY))
643		atadevices[ctlr]->devices &= ~ATA_ATA_SLAVE;
644	if (atadevices[ctlr]->devices & ATA_ATAPI_SLAVE)
645	    if (ata_getparam(atadevices[ctlr], ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
646		atadevices[ctlr]->devices &= ~ATA_ATAPI_SLAVE;
647	if (atadevices[ctlr]->devices & ATA_ATA_MASTER)
648	    if (ata_getparam(atadevices[ctlr], ATA_MASTER, ATA_C_ATA_IDENTIFY))
649		atadevices[ctlr]->devices &= ~ATA_ATA_MASTER;
650	if (atadevices[ctlr]->devices & ATA_ATAPI_MASTER)
651	    if (ata_getparam(atadevices[ctlr], ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
652		atadevices[ctlr]->devices &= ~ATA_ATAPI_MASTER;
653    }
654
655#if NATADISK > 0
656    /* now we know whats there, do the real attach, first the ATA disks */
657    for (ctlr=0; ctlr<MAXATA; ctlr++) {
658	if (!atadevices[ctlr]) continue;
659	if (atadevices[ctlr]->devices & ATA_ATA_MASTER)
660	    ad_attach(atadevices[ctlr], ATA_MASTER);
661	if (atadevices[ctlr]->devices & ATA_ATA_SLAVE)
662	    ad_attach(atadevices[ctlr], ATA_SLAVE);
663    }
664#endif
665#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
666    /* then the atapi devices */
667    for (ctlr=0; ctlr<MAXATA; ctlr++) {
668	if (!atadevices[ctlr]) continue;
669	if (atadevices[ctlr]->devices & ATA_ATAPI_MASTER)
670	    atapi_attach(atadevices[ctlr], ATA_MASTER);
671	if (atadevices[ctlr]->devices & ATA_ATAPI_SLAVE)
672	    atapi_attach(atadevices[ctlr], ATA_SLAVE);
673    }
674#endif
675    if (ata_attach_hook) {
676	config_intrhook_disestablish(ata_attach_hook);
677	free(ata_attach_hook, M_ATA);
678	ata_attach_hook = NULL;
679    }
680}
681
682static int32_t
683ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
684{
685    struct ata_params *ata_parm;
686    int8_t buffer[DEV_BSIZE];
687
688    /* select drive */
689    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
690    DELAY(1);
691    if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
692	printf("ata%d-%s: identify failed\n",
693	       scp->lun, (device == ATA_MASTER) ? "master" : "slave ");
694	return -1;
695    }
696    if (ata_wait(scp, device, ATA_S_READY|ATA_S_DSC|ATA_S_DRQ)) {
697	printf("ata%d-%s: drive wont come ready after identify\n",
698	       scp->lun, (device == ATA_MASTER) ? "master" : "slave ");
699        return -1;
700    }
701    insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
702    ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
703    if (!ata_parm) {
704	printf("ata%d-%s: malloc for ata_param failed\n",
705	       scp->lun, (device == ATA_MASTER) ? "master" : "slave ");
706        return -1;
707    }
708    bcopy(buffer, ata_parm, sizeof(struct ata_params));
709    if (command == ATA_C_ATA_IDENTIFY ||
710	!((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
711          (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
712        bswap(ata_parm->model, sizeof(ata_parm->model));
713    btrim(ata_parm->model, sizeof(ata_parm->model));
714    bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
715    bswap(ata_parm->revision, sizeof(ata_parm->revision));
716    btrim(ata_parm->revision, sizeof(ata_parm->revision));
717    bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
718    scp->dev_param[(device == ATA_MASTER) ? 0 : 1] = ata_parm;
719    return 0;
720}
721
722static void
723ataintr(void *data)
724{
725    struct ata_softc *scp = (struct ata_softc *)data;
726
727#if NPCI > 0
728    /* check if this interrupt is for us (shared PCI interrupts) */
729    switch (scp->chiptype) {
730    case 0x00041103:    /* HighPoint HPT366 controller */
731	if (scp->active == ATA_IDLE)
732	    return;
733	if (!(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT))
734	    return;
735	break;
736
737    case 0x4d33105a:	/* Promise 33's */
738    case 0x4d38105a:	/* Promise 66's */
739	if (!(inl((pci_read_config(scp->dev, 0x20, 4) & IOMASK) + 0x1c) &
740	      ((scp->unit) ? 0x00004000 : 0x00000400)))
741	    return;
742	break;
743
744    default:
745	if ((scp->flags & ATA_DMA_ACTIVE) &&
746	    !(ata_dmastatus(scp) & ATA_BMSTAT_INTERRUPT))
747	    return;
748    }
749#endif
750    if (((scp->status = inb(scp->ioaddr + ATA_STATUS))&ATA_S_BUSY)==ATA_S_BUSY)
751	return;
752
753    /* find & call the responsible driver to process this interrupt */
754    switch (scp->active) {
755#if NATADISK > 0
756    case ATA_ACTIVE_ATA:
757	if (!scp->running)
758	    return;
759	if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
760	    return;
761	break;
762#endif
763#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
764    case ATA_ACTIVE_ATAPI:
765	if (!scp->running)
766	    return;
767	if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
768	    return;
769	break;
770#endif
771    case ATA_WAIT_INTR:
772	wakeup((caddr_t)scp);
773	break;
774
775    case ATA_WAIT_READY:
776	break;
777
778    case ATA_REINITING:
779	return;
780
781    default:
782    case ATA_IDLE:
783#ifdef ATA_DEBUG
784	{
785    	    static int32_t intr_count = 0;
786	    if (intr_count++ < 10)
787		printf("ata%d: unwanted interrupt %d status = %02x\n",
788		       scp->lun, intr_count, scp->status);
789	}
790#endif
791	/* return; SOS XXX */
792    }
793    scp->active = ATA_IDLE;
794    scp->running = NULL;
795    ata_start(scp);
796}
797
798void
799ata_start(struct ata_softc *scp)
800{
801    struct ad_request *ad_request;
802    struct atapi_request *atapi_request;
803
804    if (scp->active != ATA_IDLE)
805	return;
806
807#if NATADISK > 0
808    /* find & call the responsible driver if anything on the ATA queue */
809    if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
810	TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
811	scp->active = ATA_ACTIVE_ATA;
812	scp->running = ad_request;
813	ad_transfer(ad_request);
814	return;
815    }
816#endif
817#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
818    /*
819     * find & call the responsible driver if anything on the ATAPI queue.
820     * check for device busy by polling the DSC bit, if busy, check
821     * for requests to the other device on the channel (if any).
822     * if the other device is an ATA disk it already had its chance above.
823     * if no request can be served, timeout a call to ata_start.
824     */
825    if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
826	struct atapi_softc *atp = atapi_request->device;
827	static int32_t interval = 1;
828
829	if (atp->flags & ATAPI_F_DSC_USED) {
830	    outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
831	    DELAY(1);
832	    if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) {
833		while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) {
834		    if (atapi_request->device->unit != atp->unit) {
835			struct atapi_softc *tmp = atapi_request->device;
836
837			outb(tmp->controller->ioaddr + ATA_DRIVE,
838			     ATA_D_IBM | tmp->unit);
839			DELAY(1);
840			if (!inb(tmp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC)
841			    atapi_request = NULL;
842			break;
843		    }
844	        }
845	    }
846	    if (!atapi_request) {
847		timeout((timeout_t *)ata_start, atp->controller, interval++);
848		return;
849	    }
850	    else
851		interval = 1;
852	}
853	TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
854	scp->active = ATA_ACTIVE_ATAPI;
855	scp->running = atapi_request;
856	atapi_transfer(atapi_request);
857	return;
858    }
859#endif
860}
861
862void
863ata_reset(struct ata_softc *scp, int32_t *mask)
864{
865    int32_t timeout;
866    int8_t status0, status1;
867
868    /* reset channel */
869    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
870    DELAY(1);
871    inb(scp->ioaddr + ATA_STATUS);
872    outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
873    DELAY(10000);
874    outb(scp->altioaddr, ATA_A_IDS);
875    DELAY(10000);
876    inb(scp->ioaddr + ATA_ERROR);
877    DELAY(3000);
878
879    /* wait for BUSY to go inactive */
880    for (timeout = 0; timeout < 310000; timeout++) {
881	outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
882	DELAY(1);
883	status0 = inb(scp->ioaddr + ATA_STATUS);
884	outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
885	DELAY(1);
886	status1 = inb(scp->ioaddr + ATA_STATUS);
887	if (*mask == 0x01)      /* wait for master only */
888	    if (!(status0 & ATA_S_BUSY))
889		break;
890	if (*mask == 0x02)      /* wait for slave only */
891	    if (!(status1 & ATA_S_BUSY))
892		break;
893	if (*mask == 0x03)      /* wait for both master & slave */
894	    if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
895		break;
896	DELAY(100);
897    }
898    DELAY(1);
899    outb(scp->altioaddr, ATA_A_4BIT);
900    if (status0 & ATA_S_BUSY)
901	*mask &= ~0x01;
902    if (status1 & ATA_S_BUSY)
903	*mask &= ~0x02;
904    if (bootverbose)
905	printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
906	       scp->lun, *mask, status0, status1);
907}
908
909int32_t
910ata_reinit(struct ata_softc *scp)
911{
912    int32_t mask = 0, omask;
913
914    scp->active = ATA_REINITING;
915    scp->running = NULL;
916    printf("ata%d: resetting devices .. ", scp->lun);
917    if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
918	mask |= 0x01;
919    if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
920	mask |= 0x02;
921    omask = mask;
922    ata_reset(scp, &mask);
923    if (omask != mask)
924	printf(" device dissapeared! %d ", omask & ~mask);
925
926#if NATADISK > 0
927    if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
928	ad_reinit((struct ad_softc *)scp->dev_softc[0]);
929    if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
930	ad_reinit((struct ad_softc *)scp->dev_softc[1]);
931#endif
932#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
933    if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
934	atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
935    if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
936	atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
937#endif
938    printf("done\n");
939    scp->active = ATA_IDLE;
940    ata_start(scp);
941    return 0;
942}
943
944int32_t
945ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
946{
947    u_int32_t timeout = 0;
948
949    DELAY(1);
950    while (timeout < 5000000) {	/* timeout 5 secs */
951	scp->status = inb(scp->ioaddr + ATA_STATUS);
952
953	/* if drive fails status, reselect the drive just to be sure */
954	if (scp->status == 0xff) {
955	    printf("ata%d-%s: no status, reselecting device\n",
956		   scp->lun, device?"slave":"master");
957	    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
958	    DELAY(1);
959	    scp->status = inb(scp->ioaddr + ATA_STATUS);
960	}
961
962	/* are we done ? */
963	if (!(scp->status & ATA_S_BUSY))
964	    break;
965
966	if (timeout > 1000) {
967	    timeout += 1000;
968	    DELAY(1000);
969	}
970	else {
971	    timeout += 10;
972	    DELAY(10);
973	}
974    }
975    if (scp->status & ATA_S_ERROR)
976	scp->error = inb(scp->ioaddr + ATA_ERROR);
977    if (timeout >= 5000000)
978	return -1;
979    if (!mask)
980	return (scp->status & ATA_S_ERROR);
981
982    /* Wait 50 msec for bits wanted. */
983    timeout = 5000;
984    while (timeout--) {
985	scp->status = inb(scp->ioaddr + ATA_STATUS);
986	if ((scp->status & mask) == mask) {
987	    if (scp->status & ATA_S_ERROR)
988		scp->error = inb(scp->ioaddr + ATA_ERROR);
989	    return (scp->status & ATA_S_ERROR);
990	}
991	DELAY (10);
992    }
993    return -1;
994}
995
996int32_t
997ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
998	   u_int32_t cylinder, u_int32_t head, u_int32_t sector,
999	   u_int32_t count, u_int32_t feature, int32_t flags)
1000{
1001#ifdef ATA_DEBUG
1002    printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, "
1003	   "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
1004	   scp->lun, scp->ioaddr, device, command,
1005	   cylinder, head, sector, count, flags);
1006#endif
1007
1008    /* ready to issue command ? */
1009    if (ata_wait(scp, device, 0) < 0) {
1010	printf("ata%d-%s: timeout waiting to give command=%02x s=%02x e=%02x\n",
1011	       scp->lun, device ? "slave" : "master", command,
1012	       scp->status, scp->error);
1013	return -1;
1014    }
1015    outb(scp->ioaddr + ATA_FEATURE, feature);
1016    outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1017    outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1018    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1019    outb(scp->ioaddr + ATA_SECTOR, sector);
1020    outb(scp->ioaddr + ATA_COUNT, count);
1021
1022    switch (flags) {
1023    case ATA_WAIT_INTR:
1024	if (scp->active != ATA_IDLE)
1025	    printf("WARNING: WAIT_INTR active=%s\n", active2str(scp->active));
1026	scp->active = ATA_WAIT_INTR;
1027	asleep((caddr_t)scp, PRIBIO, "atacmd", 500);
1028	outb(scp->ioaddr + ATA_CMD, command);
1029	if (await(PRIBIO, 500)) {
1030	    printf("ata_command: timeout waiting for interrupt\n");
1031	    scp->active = ATA_IDLE;
1032	    return -1;
1033	}
1034	break;
1035
1036    case ATA_WAIT_READY:
1037	if (scp->active != ATA_IDLE && scp->active != ATA_REINITING)
1038	    printf("WARNING: WAIT_READY active=%s\n", active2str(scp->active));
1039	scp->active = ATA_WAIT_READY;
1040	outb(scp->ioaddr + ATA_CMD, command);
1041	if (ata_wait(scp, device, ATA_S_READY) < 0) {
1042	    printf("ata%d-%s: timeout waiting for command=%02x s=%02x e=%02x\n",
1043		   scp->lun, device ? "slave" : "master", command,
1044		   scp->status, scp->error);
1045	    scp->active = ATA_IDLE;
1046	    return -1;
1047	}
1048	scp->active = ATA_IDLE;
1049	break;
1050
1051    case ATA_IMMEDIATE:
1052	outb(scp->ioaddr + ATA_CMD, command);
1053	break;
1054
1055    default:
1056	printf("DANGER: illegal interrupt flag=%s\n", active2str(flags));
1057    }
1058    return 0;
1059}
1060
1061int8_t *
1062ata_mode2str(int32_t mode)
1063{
1064    switch (mode) {
1065    case ATA_PIO0: return "PIO0";
1066    case ATA_PIO1: return "PIO1";
1067    case ATA_PIO2: return "PIO2";
1068    case ATA_PIO3: return "PIO3";
1069    case ATA_PIO4: return "PIO4";
1070    case ATA_WDMA2: return "WDMA2";
1071    case ATA_UDMA2: return "UDMA33";
1072    case ATA_UDMA4: return "UDMA66";
1073    default: return "???";
1074    }
1075}
1076
1077int8_t
1078ata_pio2mode(int32_t pio)
1079{
1080    switch (pio) {
1081    default:
1082    case 0: return ATA_PIO0;
1083    case 1: return ATA_PIO1;
1084    case 2: return ATA_PIO2;
1085    case 3: return ATA_PIO3;
1086    case 4: return ATA_PIO4;
1087    }
1088}
1089
1090static int8_t *
1091active2str(int32_t active)
1092{
1093    static char buf[8];
1094
1095    switch (active) {
1096    case ATA_IDLE:
1097	return("ATA_IDLE");
1098    case ATA_WAIT_INTR:
1099	return("ATA_WAIT_INTR");
1100    case ATA_ACTIVE_ATA:
1101	return("ATA_ACTIVE_ATA");
1102    case ATA_ACTIVE_ATAPI:
1103	return("ATA_ACTIVE_ATAPI");
1104    case ATA_REINITING:
1105	return("ATA_REINITING");
1106    default:
1107	sprintf(buf, "0x%02x", active);
1108	return buf;
1109    }
1110}
1111
1112int32_t
1113ata_pmode(struct ata_params *ap)
1114{
1115    if (ap->atavalid & ATA_FLAG_64_70) {
1116	if (ap->apiomodes & 2) return 4;
1117	if (ap->apiomodes & 1) return 3;
1118    }
1119    return -1;
1120}
1121
1122int32_t
1123ata_wmode(struct ata_params *ap)
1124{
1125    if (ap->atavalid & ATA_FLAG_64_70) {
1126	if (ap->wdmamodes & 4) return 2;
1127	if (ap->wdmamodes & 2) return 1;
1128	if (ap->wdmamodes & 1) return 0;
1129    }
1130    return -1;
1131}
1132
1133int32_t
1134ata_umode(struct ata_params *ap)
1135{
1136    if (ap->atavalid & ATA_FLAG_88) {
1137	if (ap->udmamodes & 0x10) return (ap->cblid ? 4 : 2);
1138	if (ap->udmamodes & 0x08) return (ap->cblid ? 3 : 2);
1139	if (ap->udmamodes & 0x04) return 2;
1140	if (ap->udmamodes & 0x02) return 1;
1141	if (ap->udmamodes & 0x01) return 0;
1142    }
1143    return -1;
1144}
1145
1146static void
1147bswap(int8_t *buf, int32_t len)
1148{
1149    u_int16_t *p = (u_int16_t*)(buf + len);
1150
1151    while (--p >= (u_int16_t*)buf)
1152	*p = ntohs(*p);
1153}
1154
1155static void
1156btrim(int8_t *buf, int32_t len)
1157{
1158    int8_t *p;
1159
1160    for (p = buf; p < buf+len; ++p)
1161	if (!*p)
1162	    *p = ' ';
1163    for (p = buf + len - 1; p >= buf && *p == ' '; --p)
1164	*p = 0;
1165}
1166
1167static void
1168bpack(int8_t *src, int8_t *dst, int32_t len)
1169{
1170    int32_t i, j, blank;
1171
1172    for (i = j = blank = 0 ; i < len-1; i++) {
1173	if (blank && src[i] == ' ') continue;
1174	if (blank && src[i] != ' ') {
1175	    dst[j++] = src[i];
1176	    blank = 0;
1177	    continue;
1178	}
1179	if (src[i] == ' ') {
1180	    blank = 1;
1181	    if (i == 0)
1182		continue;
1183	}
1184	dst[j++] = src[i];
1185    }
1186    dst[j] = 0x00;
1187}
1188