ata-all.c revision 49471
1/*-
2 * Copyright (c) 1998,1999 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 *  $Id: ata-all.c,v 1.15 1999/06/25 09:02:56 sos Exp $
29 */
30
31#include "ata.h"
32#if NATA > 0
33#include "isa.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "opt_global.h"
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/interrupt.h>
41#include <sys/conf.h>
42#include <sys/module.h>
43#include <sys/bus.h>
44#include <sys/buf.h>
45#include <sys/malloc.h>
46#include <sys/devicestat.h>
47#include <vm/vm.h>
48#include <vm/pmap.h>
49#include <machine/resource.h>
50#include <machine/bus.h>
51#include <sys/rman.h>
52#include <machine/clock.h>
53#ifdef __i386__
54#include <machine/smp.h>
55#include <i386/isa/intr_machdep.h>
56#endif
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 <dev/ata/ata-all.h>
64#include <dev/ata/ata-disk.h>
65#include <dev/ata/atapi-all.h>
66
67/* misc defines */
68#define UNIT(dev) (dev>>3 & 0x1f)   		/* assume 8 minor # per unit */
69#define MIN(a,b) ((a)>(b)?(b):(a))
70#if SMP == 0
71#define isa_apic_irq(x)	x
72#endif
73
74/* prototypes */
75#if NPCI > 0
76static void promise_intr(void *);
77#endif
78static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
79static void ataintr(void *);
80
81static int32_t atanlun = 0;
82struct ata_softc *atadevices[MAXATA];
83static devclass_t ata_devclass;
84
85#if NISA > 0
86
87static int
88ata_isaprobe(device_t dev)
89{
90    struct resource *port;
91    int rid;
92    int32_t ctlr, res;
93    int32_t lun;
94
95    /* allocate the port range */
96    rid = 0;
97    port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
98    if (!port)
99	return (ENOMEM);
100
101    /* check if allready in use by a PCI device */
102    for (ctlr = 0; ctlr < atanlun; ctlr++) {
103	if (atadevices[ctlr]->ioaddr == rman_get_start(port)) {
104	    printf("ata-isa%d: already registered as ata%d\n",
105		   device_get_unit(dev), ctlr);
106	    bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
107	    return ENXIO;
108	}
109    }
110
111    lun = 0;
112    res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT,
113		    0, dev, &lun);
114
115    bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
116
117    if (res) {
118	isa_set_portsize(dev, res);
119	*(int *)device_get_softc(dev) = lun;
120	return 0;
121    }
122
123    return ENXIO;
124}
125
126static int
127ata_isaattach(device_t dev)
128{
129    struct resource *port;
130    struct resource *irq;
131    void *ih;
132    int rid;
133
134    /* Allocate the port range and interrupt */
135    rid = 0;
136    port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
137    if (!port)
138	return (ENOMEM);
139
140    rid = 0;
141    irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
142    if (!irq) {
143	bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
144	return (ENOMEM);
145    }
146    return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr,
147			  atadevices[*(int *)device_get_softc(dev)], &ih);
148}
149
150static device_method_t ata_isa_methods[] = {
151    /* Device interface */
152    DEVMETHOD(device_probe,	ata_isaprobe),
153    DEVMETHOD(device_attach,	ata_isaattach),
154    { 0, 0 }
155};
156
157static driver_t ata_isa_driver = {
158    "ata",
159    ata_isa_methods,
160    sizeof(int),
161};
162
163DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
164
165#endif
166
167#if NPCI > 0
168
169static const char *
170ata_pcimatch(device_t dev)
171{
172    u_int32_t data;
173
174    data = pci_read_config(dev, PCI_CLASS_REG, 4);
175    if (pci_get_class(dev) == PCIC_STORAGE &&
176	(pci_get_subclass(dev) == PCIS_STORAGE_IDE ||
177	 pci_get_subclass(dev) == PCIS_STORAGE_RAID ||
178	 pci_get_subclass(dev) == PCIS_STORAGE_OTHER)) {
179	switch (pci_get_devid(dev)) {
180	case 0x12308086:
181	    return "Intel PIIX IDE controller";
182	case 0x70108086:
183	    return "Intel PIIX3 IDE controller";
184	case 0x71118086:
185	    return "Intel PIIX4 IDE controller";
186	case 0x4d33105a:
187	    return "Promise Ultra/33 IDE controller";
188	case 0x4d38105a:
189	    return "Promise Ultra/66 IDE controller";
190	case 0x522910b9:
191	    return "AcerLabs Aladdin IDE controller";
192#if 0
193	case 0x05711106:
194	    return "VIA Apollo IDE controller";
195	case 0x06401095:
196	    return "CMD 640 IDE controller";
197	case 0x06461095:
198	    return "CMD 646 IDE controller";
199	case 0xc6931080:
200	    return "Cypress 82C693 IDE controller";
201	case 0x01021078:
202	    return "Cyrix 5530 IDE controller";
203#endif
204	default:
205	    return "Unknown PCI IDE controller (using generic mode)";
206	}
207    }
208    return NULL;
209}
210
211static int
212ata_pciprobe(device_t dev)
213{
214    const char *desc = ata_pcimatch(dev);
215    if (desc) {
216	device_set_desc(dev, desc);
217	return 0;
218    }
219    else
220	return ENXIO;
221}
222
223static int
224ata_pciattach(device_t dev)
225{
226    int unit = device_get_unit(dev);
227    struct ata_softc *scp;
228    u_int32_t type;
229    u_int8_t class, subclass;
230    u_int32_t cmd;
231    int32_t iobase_1, iobase_2, altiobase_1, altiobase_2;
232    int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2;
233    int32_t lun;
234
235    /* set up vendor-specific stuff */
236    type = pci_get_devid(dev);
237    class = pci_get_class(dev);
238    subclass = pci_get_subclass(dev);
239    cmd = pci_read_config(dev, PCIR_COMMAND, 4);
240
241#ifdef ATA_DEBUG
242    printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x\n",
243	   unit, type, class, subclass, cmd);
244#endif
245
246    /* if this is a Promise controller handle it specially */
247    if (type == 0x4d33105a || type == 0x4d38105a) {
248	iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
249	altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
250	iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
251	altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
252	irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
253    	bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
254	bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
255	outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
256	printf("ata-pci%d: Busmastering DMA supported\n", unit);
257    }
258    /* everybody else seems to do it this way */
259    else {
260	if ((unit == 0) &&
261	    (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) {
262		iobase_1 = IO_WD1;
263		altiobase_1 = iobase_1 + ATA_ALTPORT;
264		irq1 = 14;
265	}
266	else {
267		iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
268		altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
269		irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
270	}
271	if ((unit == 0) &&
272	    (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) {
273		iobase_2 = IO_WD2;
274		altiobase_2 = iobase_2 + ATA_ALTPORT;
275		irq2 = 15;
276	}
277	else {
278		iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
279		altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
280		irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
281	}
282
283        /* is this controller busmaster capable ? */
284        if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
285	    /* is busmastering support turned on ? */
286	    if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
287	        /* is there a valid port range to connect to ? */
288    	        if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
289		    bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
290		    printf("ata-pci%d: Busmastering DMA supported\n", unit);
291    	        }
292    	        else
293		    printf("ata-pci%d: Busmastering DMA not configured\n",unit);
294	    }
295	    else
296	        printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
297        }
298        else
299	    printf("ata-pci%d: Busmastering DMA not supported\n", unit);
300    }
301
302    /* now probe the addresse found for "real" ATA/ATAPI hardware */
303    lun = 0;
304    if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
305	scp = atadevices[lun];
306	if (iobase_1 == IO_WD1)
307#ifdef __i386__
308	    inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
309		        &bio_imask, INTR_EXCL);
310#endif
311#ifdef __alpha__
312	    alpha_platform_setup_ide_intr(0, ataintr, scp);
313#endif
314	else {
315	    struct resource *irq;
316	    int rid = 0;
317	    void *ih;
318
319	    irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
320				     RF_SHAREABLE | RF_ACTIVE);
321	    if (!irq)
322		printf("ata_pciattach: Unable to alloc interrupt\n");
323
324    	    if (type == 0x4d33105a || type == 0x4d38105a)
325		bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih);
326	    else
327		bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
328	}
329	printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
330	       lun, iobase_1, isa_apic_irq(irq1), unit);
331    }
332    lun = 1;
333    if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
334	scp = atadevices[lun];
335	if (iobase_2 == IO_WD2)
336#ifdef __i386__
337	    inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
338		        &bio_imask, INTR_EXCL);
339#endif
340#ifdef __alpha__
341	    alpha_platform_setup_ide_intr(1, ataintr, scp);
342#endif
343	else {
344	    struct resource *irq;
345	    int rid = 0;
346	    void *ih;
347
348    	    if (type != 0x4d33105a && type != 0x4d38105a) {
349	        irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
350					 RF_SHAREABLE | RF_ACTIVE);
351	        if (!irq)
352		    printf("ata_pciattach: Unable to alloc interrupt\n");
353
354		bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
355	    }
356	}
357	printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
358	       lun, iobase_2, isa_apic_irq(irq2), unit);
359    }
360    return 0;
361}
362
363static device_method_t ata_pci_methods[] = {
364    /* Device interface */
365    DEVMETHOD(device_probe,	ata_pciprobe),
366    DEVMETHOD(device_attach,	ata_pciattach),
367    { 0, 0 }
368};
369
370static driver_t ata_pci_driver = {
371    "ata-pci",
372    ata_pci_methods,
373    sizeof(int),
374};
375
376DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0);
377
378static void
379promise_intr(void *data)
380{
381    struct ata_softc *scp = (struct ata_softc *)data;
382    int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c);
383
384    if (channel & 0x00000400)
385	ataintr(data);
386
387    if (channel & 0x00004000)
388	ataintr(atadevices[scp->lun + 1]);
389}
390#endif
391
392static int32_t
393ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
394	  device_t dev, int32_t *unit)
395{
396    struct ata_softc *scp = atadevices[atanlun];
397    int32_t mask = 0;
398    int32_t timeout;
399    int32_t lun = atanlun;
400    u_int8_t status0, status1;
401
402#ifdef ATA_STATIC_ID
403    atanlun++;
404#endif
405    if (lun > MAXATA) {
406	printf("ata: unit out of range(%d)\n", lun);
407	return 0;
408    }
409    if (scp) {
410	printf("ata%d: unit already attached\n", lun);
411	return 0;
412    }
413    scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT);
414    if (scp == NULL) {
415	printf("ata%d: failed to allocate driver storage\n", lun);
416	return 0;
417    }
418    bzero(scp, sizeof(struct ata_softc));
419
420    scp->unit = *unit;
421    scp->lun = lun;
422    scp->ioaddr = ioaddr;
423    scp->altioaddr = altioaddr;
424    scp->active = ATA_IDLE;
425
426#ifdef ATA_DEBUG
427    printf("ata%d: iobase=0x%04x altiobase=0x%04x\n",
428	   scp->lun, scp->ioaddr, scp->altioaddr);
429#endif
430
431    /* do we have any signs of ATA/ATAPI HW being present ? */
432    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
433    DELAY(1);
434    status0 = inb(scp->ioaddr + ATA_STATUS);
435    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
436    DELAY(1);
437    status1 = inb(scp->ioaddr + ATA_STATUS);
438    if ((status0 & 0xf8) != 0xf8)
439        mask |= 0x01;
440    if ((status1 & 0xf8) != 0xf8)
441        mask |= 0x02;
442#ifdef ATA_DEBUG
443    printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
444	   scp->lun, mask, status0, status1);
445#endif
446    if (!mask) {
447	free(scp, M_DEVBUF);
448        return 0;
449    }
450    /* assert reset for devices and wait for completition */
451    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
452    DELAY(1);
453    outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
454    DELAY(1000);
455    outb(scp->altioaddr, ATA_A_IDS);
456    DELAY(1000);
457    inb(scp->ioaddr + ATA_ERROR);
458    DELAY(1);
459    outb(scp->altioaddr, ATA_A_4BIT);
460    DELAY(1);
461
462    /* wait for BUSY to go inactive */
463    for (timeout = 0; timeout < 30000*10; timeout++) {
464        outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
465        DELAY(1);
466        status0 = inb(scp->ioaddr + ATA_STATUS);
467        outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
468        DELAY(1);
469        status1 = inb(scp->ioaddr + ATA_STATUS);
470        if (mask == 0x01)      /* wait for master only */
471            if (!(status0 & ATA_S_BSY))
472            	break;
473        if (mask == 0x02)      /* wait for slave only */
474            if (!(status1 & ATA_S_BSY))
475            	break;
476        if (mask == 0x03)      /* wait for both master & slave */
477            if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY))
478            	break;
479        DELAY(100);
480    }
481    if (status0 & ATA_S_BSY)
482        mask &= ~0x01;
483    if (status1 & ATA_S_BSY)
484        mask &= ~0x02;
485#ifdef ATA_DEBUG
486    printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
487	   scp->lun, mask, status0, status1);
488#endif
489    if (!mask) {
490	free(scp, M_DEVBUF);
491        return 0;
492    }
493    /*
494     * OK, we have at least one device on the chain,
495     * check for ATAPI signatures, if none check if its
496     * a good old ATA device.
497     */
498
499    outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
500    DELAY(1);
501    if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
502	inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
503	scp->devices |= ATA_ATAPI_MASTER;
504    }
505    outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
506    DELAY(1);
507    if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
508	inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
509	scp->devices |= ATA_ATAPI_SLAVE;
510    }
511    if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
512    	outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
513        DELAY(1);
514        outb(scp->ioaddr + ATA_ERROR, 0x58);
515        outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
516        if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
517	    inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
518	    scp->devices |= ATA_ATA_MASTER;
519        }
520    }
521    if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
522    	outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
523        DELAY(1);
524        outb(scp->ioaddr + ATA_ERROR, 0x58);
525        outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
526        if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
527            inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
528	    scp->devices |= ATA_ATA_SLAVE;
529        }
530    }
531#ifdef ATA_DEBUG
532    printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices);
533#endif
534    if (!scp->devices) {
535	free(scp, M_DEVBUF);
536	return 0;
537    }
538    TAILQ_INIT(&scp->ata_queue);
539    TAILQ_INIT(&scp->atapi_queue);
540    *unit = scp->lun;
541    scp->dev = dev;
542    if (bmaddr)
543    	scp->bmaddr = bmaddr;
544    atadevices[scp->lun] = scp;
545#ifndef ATA_STATIC_ID
546    atanlun++;
547#endif
548    return ATA_IOSIZE;
549}
550
551static void
552ataintr(void *data)
553{
554    struct ata_softc *scp;
555    struct atapi_request *atapi_request;
556    struct ad_request *ad_request;
557    u_int8_t status;
558    static int32_t intr_count = 0;
559
560    scp = (struct ata_softc *)data;
561
562    /* find & call the responsible driver to process this interrupt */
563    switch (scp->active) {
564#if NATADISK > 0
565    case ATA_ACTIVE_ATA:
566    	if ((ad_request = TAILQ_FIRST(&scp->ata_queue)))
567            if (ad_interrupt(ad_request) == ATA_OP_CONTINUES)
568		return;
569	break;
570#endif
571    case ATA_ACTIVE_ATAPI:
572        if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue)))
573	    if (atapi_interrupt(atapi_request) == ATA_OP_CONTINUES)
574		return;
575	break;
576
577    case ATA_WAIT_INTR:
578	wakeup((caddr_t)scp);
579	break;
580
581    case ATA_IGNORE_INTR:
582	break;
583
584    default:
585    case ATA_IDLE:
586        status = inb(scp->ioaddr + ATA_STATUS);
587	if (intr_count++ < 10)
588	    printf("ata%d: unwanted interrupt %d status = %02x\n",
589		   scp->lun, intr_count, status);
590	return;
591    }
592    scp->active = ATA_IDLE;
593    ata_start(scp);
594}
595
596void
597ata_start(struct ata_softc *scp)
598{
599    struct ad_request *ad_request;
600    struct atapi_request *atapi_request;
601
602#ifdef ATA_DEBUG
603    printf("ata_start: entered\n");
604#endif
605    if (scp->active != ATA_IDLE) {
606	printf("ata: unwanted ata_start\n");
607	return;
608    }
609
610#if NATADISK > 0
611    /* find & call the responsible driver if anything on ATA queue */
612    if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
613	scp->active = ATA_ACTIVE_ATA;
614        ad_transfer(ad_request);
615#ifdef ATA_DEBUG
616        printf("ata_start: started ata, leaving\n");
617#endif
618	return;
619    }
620#endif
621
622    /* find & call the responsible driver if anything on ATAPI queue */
623    if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
624    	scp->active = ATA_ACTIVE_ATAPI;
625	atapi_transfer(atapi_request);
626#ifdef ATA_DEBUG
627        printf("ata_start: started atapi, leaving\n");
628#endif
629	return;
630    }
631}
632
633int32_t
634ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
635{
636    u_int8_t status;
637    u_int32_t timeout = 0;
638
639    while (timeout++ <= 500000) {	/* timeout 5 secs */
640	status = inb(scp->ioaddr + ATA_STATUS);
641
642	/* if drive fails status, reselect the drive just to be sure */
643	if (status == 0xff) {
644       	    printf("ata%d: %s: no status, reselecting device\n",
645		   scp->lun, device?"slave":"master");
646    	    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
647	    DELAY(1);
648	    status = inb(scp->ioaddr + ATA_STATUS);
649	}
650	if (status == 0xff)
651	    return -1;
652	scp->status = status;
653	if (!(status & ATA_S_BSY)) {
654	    if (status & ATA_S_ERROR)
655		scp->error = inb(scp->ioaddr + ATA_ERROR);
656	    if ((status & mask) == mask)
657		return (status & ATA_S_ERROR);
658    	}
659	if (timeout > 1000)
660	    DELAY(1000);
661	else
662	    DELAY(10);
663    }
664    return -1;
665}
666
667int32_t
668ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
669	   u_int32_t cylinder, u_int32_t head, u_int32_t sector,
670	   u_int32_t count, u_int32_t feature, int32_t flags)
671{
672#ifdef ATA_DEBUG
673printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, c=%d, h=%d, s=%d, count=%d, flags=%02x\n", scp->lun, scp->ioaddr, device, command, cylinder, head, sector, count, flags);
674#endif
675
676    /* ready to issue command ? */
677    if (ata_wait(scp, device, 0) < 0) {
678       	printf("ata%d: %s: timeout waiting to give command s=%02x e=%02x\n",
679	       scp->lun, device?"slave":"master", scp->status, scp->error);
680    }
681    outb(scp->ioaddr + ATA_FEATURE, feature);
682    outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
683    outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
684    outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
685    outb(scp->ioaddr + ATA_SECTOR, sector);
686    outb(scp->ioaddr + ATA_COUNT, count);
687
688    if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE)
689	printf("DANGER active=%d\n", scp->active);
690
691    switch (flags) {
692    case ATA_WAIT_INTR:
693        scp->active = ATA_WAIT_INTR;
694        outb(scp->ioaddr + ATA_CMD, command);
695	if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) {
696	    printf("ata_command: timeout waiting for interrupt\n");
697	    scp->active = ATA_IDLE;
698	    return -1;
699	}
700	break;
701
702    case ATA_IGNORE_INTR:
703        scp->active = ATA_IGNORE_INTR;
704        outb(scp->ioaddr + ATA_CMD, command);
705	break;
706
707    case ATA_IMMEDIATE:
708    default:
709        outb(scp->ioaddr + ATA_CMD, command);
710	break;
711    }
712#ifdef ATA_DEBUG
713printf("ata_command: leaving\n");
714#endif
715    return 0;
716}
717
718void
719bswap(int8_t *buf, int32_t len)
720{
721    u_int16_t *p = (u_int16_t*)(buf + len);
722
723    while (--p >= (u_int16_t*)buf)
724        *p = ntohs(*p);
725}
726
727void
728btrim(int8_t *buf, int32_t len)
729{
730    int8_t *p;
731
732    for (p = buf; p < buf+len; ++p)
733        if (!*p)
734            *p = ' ';
735    for (p = buf + len - 1; p >= buf && *p == ' '; --p)
736        *p = 0;
737}
738
739void
740bpack(int8_t *src, int8_t *dst, int32_t len)
741{
742    int32_t i, j, blank;
743
744    for (i = j = blank = 0 ; i < len-1; i++) {
745	if (blank && src[i] == ' ') continue;
746	if (blank && src[i] != ' ') {
747	    dst[j++] = src[i];
748	    blank = 0;
749	    continue;
750	}
751	if (src[i] == ' ')
752	    blank = 1;
753	dst[j++] = src[i];
754    }
755    dst[j] = 0x00;
756}
757#endif /* NATA > 0 */
758