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