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
--- 11 unchanged lines hidden (view full) ---
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 51520 1999-09-21 19:50:40Z sos $
29 */
30
31#include "ata.h"
32#include "apm.h"
33
34#if NATA > 0
35
36#include "isa.h"
37#include "pci.h"
38#include "atadisk.h"
39#include "atapicd.h"
40#include "atapifd.h"
41#include "atapist.h"
42#include "opt_global.h"
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/interrupt.h>
47#include <sys/conf.h>
48#include <sys/disk.h>
49#include <sys/module.h>
50#include <sys/bus.h>
51#include <sys/buf.h>
52#include <sys/malloc.h>
53#include <sys/devicestat.h>
54#include <vm/vm.h>
55#include <vm/pmap.h>
56#include <machine/resource.h>
57#include <machine/bus.h>
58#include <sys/rman.h>
59#if NPCI > 0
60#include <pci/pcivar.h>
61#include <pci/pcireg.h>
62#endif
63#include <isa/isavar.h>
64#include <isa/isareg.h>
65#include <machine/clock.h>
66#ifdef __i386__
67#include <machine/smp.h>
68#include <i386/isa/intr_machdep.h>
69#endif
70#if NAPM > 0
71#include <machine/apm_bios.h>
72#endif
73#include <dev/ata/ata-all.h>
74#include <dev/ata/ata-disk.h>
75#include <dev/ata/atapi-all.h>
76
77/* misc defines */
78#if SMP == 0
79#define isa_apic_irq(x) x
80#endif
81
82/* prototypes */
83#if NPCI > 0
84static void promise_intr(void *);
85#endif
86static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
87static void ataintr(void *);
--- 45 unchanged lines hidden (view full) ---
133
134 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
135
136 if (res) {
137 isa_set_portsize(dev, res);
138 *(int *)device_get_softc(dev) = lun;
139 return 0;
140 }
141 return ENXIO;
142}
143
144static int
145ata_isaattach(device_t dev)
146{
147 struct resource *port;
148 struct resource *irq;
--- 25 unchanged lines hidden (view full) ---
174
175static driver_t ata_isa_driver = {
176 "ata",
177 ata_isa_methods,
178 sizeof(int),
179};
180
181DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
182#endif
183
184#if NPCI > 0
185static const char *
186ata_pcimatch(device_t dev)
187{
188 u_int32_t data;
189
190 data = pci_read_config(dev, PCI_CLASS_REG, 4);
191 if (pci_get_class(dev) == PCIC_STORAGE &&
192 (pci_get_subclass(dev) == PCIS_STORAGE_IDE ||
--- 7 unchanged lines hidden (view full) ---
200 case 0x71118086:
201 return "Intel PIIX4 IDE controller";
202 case 0x4d33105a:
203 return "Promise Ultra/33 IDE controller";
204 case 0x4d38105a:
205 return "Promise Ultra/66 IDE controller";
206 case 0x522910b9:
207 return "AcerLabs Aladdin IDE controller";
208 case 0x05711106: /* 82c586 */
209 case 0x05961106: /* 82c596 */
210 return "VIA Apollo IDE controller (generic mode)";
211 case 0x06401095:
212 return "CMD 640 IDE controller (generic mode)";
213 case 0x06461095:
214 return "CMD 646 IDE controller (generic mode)";
215 case 0xc6931080:
216 return "Cypress 82C693 IDE controller (generic mode)";
217 case 0x01021078:
218 return "Cyrix 5530 IDE controller (generic mode)";
219 default:
220 return "Unknown PCI IDE controller (generic mode)";
221 }
222 }
223 return NULL;
224}
225
226static int
227ata_pciprobe(device_t dev)
228{
--- 31 unchanged lines hidden (view full) ---
260
261 /* if this is a Promise controller handle it specially */
262 if (type == 0x4d33105a || type == 0x4d38105a) {
263 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
264 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
265 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
266 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
267 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
268 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
269 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
270 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
271 printf("ata-pci%d: Busmastering DMA supported\n", unit);
272 }
273 /* everybody else seems to do it this way */
274 else {
275 if ((unit == 0) &&
276 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) {
--- 13 unchanged lines hidden (view full) ---
290 irq2 = 15;
291 }
292 else {
293 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
294 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
295 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
296 }
297
298 /* is this controller busmaster capable ? */
299 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
300 /* is busmastering support turned on ? */
301 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
302 /* is there a valid port range to connect to ? */
303 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
304 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
305 printf("ata-pci%d: Busmastering DMA supported\n", unit);
306 }
307 else
308 printf("ata-pci%d: Busmastering DMA not configured\n",unit);
309 }
310 else
311 printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
312 }
313 else
314 printf("ata-pci%d: Busmastering DMA not supported\n", unit);
315 }
316
317 /* now probe the addresse found for "real" ATA/ATAPI hardware */
318 lun = 0;
319 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
320 scp = atadevices[lun];
321 if (iobase_1 == IO_WD1)
322#ifdef __i386__
323 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
324 &bio_imask, INTR_EXCL);
325#endif
326#ifdef __alpha__
327 alpha_platform_setup_ide_intr(0, ataintr, scp);
328#endif
329 else {
330 struct resource *irq;
331 int rid = 0;
332 void *ih;
333
334 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
335 RF_SHAREABLE | RF_ACTIVE);
336 if (!irq)
337 printf("ata_pciattach: Unable to alloc interrupt\n");
338
339 if (type == 0x4d33105a || type == 0x4d38105a)
340 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih);
341 else
342 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
343 }
344 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
345 lun, iobase_1, isa_apic_irq(irq1), unit);
346 }
347 lun = 1;
348 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
349 scp = atadevices[lun];
350 if (iobase_2 == IO_WD2)
351#ifdef __i386__
352 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
353 &bio_imask, INTR_EXCL);
354#endif
355#ifdef __alpha__
356 alpha_platform_setup_ide_intr(1, ataintr, scp);
357#endif
358 else {
359 struct resource *irq;
360 int rid = 0;
361 void *ih;
362
363 if (type != 0x4d33105a && type != 0x4d38105a) {
364 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
365 RF_SHAREABLE | RF_ACTIVE);
366 if (!irq)
367 printf("ata_pciattach: Unable to alloc interrupt\n");
368
369 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
370 }
371 }
372 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
373 lun, iobase_2, isa_apic_irq(irq2), unit);
374 }
--- 30 unchanged lines hidden (view full) ---
405#endif
406
407static int32_t
408ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
409 device_t dev, int32_t *unit)
410{
411 struct ata_softc *scp = atadevices[atanlun];
412 int32_t mask = 0;
413 int32_t lun = atanlun;
414 u_int8_t status0, status1;
415
416#ifdef ATA_STATIC_ID
417 atanlun++;
418#endif
419 if (lun > MAXATA) {
420 printf("ata: unit out of range(%d)\n", lun);
--- 11 unchanged lines hidden (view full) ---
432 bzero(scp, sizeof(struct ata_softc));
433
434 scp->unit = *unit;
435 scp->lun = lun;
436 scp->ioaddr = ioaddr;
437 scp->altioaddr = altioaddr;
438 scp->active = ATA_IDLE;
439
440 if (bootverbose)
441 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n",
442 scp->lun, scp->ioaddr, scp->altioaddr);
443
444
445 /* do we have any signs of ATA/ATAPI HW being present ? */
446 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
447 DELAY(1);
448 status0 = inb(scp->ioaddr + ATA_STATUS);
449 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
450 DELAY(1);
451 status1 = inb(scp->ioaddr + ATA_STATUS);
452 if ((status0 & 0xf8) != 0xf8)
453 mask |= 0x01;
454 if ((status1 & 0xf8) != 0xf8)
455 mask |= 0x02;
456 if (bootverbose)
457 printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
458 scp->lun, mask, status0, status1);
459 if (!mask) {
460 free(scp, M_DEVBUF);
461 return 0;
462 }
463 ata_reset(scp, &mask);
464 if (!mask) {
465 free(scp, M_DEVBUF);
466 return 0;
467 }
468 /*
469 * OK, we have at least one device on the chain,
470 * check for ATAPI signatures, if none check if its
471 * a good old ATA device.
472 */
473 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
474 DELAY(1);
475 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
476 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
477 scp->devices |= ATA_ATAPI_MASTER;
478 }
479 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
480 DELAY(1);
481 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
482 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
483 scp->devices |= ATA_ATAPI_SLAVE;
484 }
485 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
486 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
487 DELAY(1);
488 outb(scp->ioaddr + ATA_ERROR, 0x58);
489 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
490 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
491 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
492 scp->devices |= ATA_ATA_MASTER;
493 }
494 }
495 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
496 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
497 DELAY(1);
498 outb(scp->ioaddr + ATA_ERROR, 0x58);
499 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
500 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
501 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
502 scp->devices |= ATA_ATA_SLAVE;
503 }
504 }
505 if (bootverbose)
506 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices);
507 if (!scp->devices) {
508 free(scp, M_DEVBUF);
509 return 0;
510 }
511 TAILQ_INIT(&scp->ata_queue);
512 TAILQ_INIT(&scp->atapi_queue);
513 *unit = scp->lun;
514 scp->dev = dev;
515 if (bmaddr)
516 scp->bmaddr = bmaddr;
517 atadevices[scp->lun] = scp;
518#ifndef ATA_STATIC_ID
519 atanlun++;
520#endif
521#if NAPM > 0
522 scp->resume_hook.ah_fun = (void *)ata_reinit;
523 scp->resume_hook.ah_arg = scp;
524 scp->resume_hook.ah_name = "ATA driver";
525 scp->resume_hook.ah_order = APM_MID_ORDER;
526 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
527#endif
528 return ATA_IOSIZE;
529}
530
531static void
532ataintr(void *data)
533{
534 struct ata_softc *scp =(struct ata_softc *)data;
535
536 scp->status = inb(scp->ioaddr + ATA_STATUS);
537
538 /* find & call the responsible driver to process this interrupt */
539 switch (scp->active) {
540#if NATADISK > 0
541 case ATA_ACTIVE_ATA:
542 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
543 return;
544 break;
545#endif
546#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
547 case ATA_ACTIVE_ATAPI:
548 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
549 return;
550 break;
551#endif
552 case ATA_WAIT_INTR:
553 wakeup((caddr_t)scp);
554 break;
555
556 case ATA_IGNORE_INTR:
557 break;
558
559 default:
560 case ATA_IDLE:
561#ifdef ATA_DEBUG
562 {
563 static int32_t intr_count = 0;
564 if (intr_count++ < 10)
565 printf("ata%d: unwanted interrupt %d status = %02x\n",
566 scp->lun, intr_count, scp->status);
567 }
568#endif
569 return;
570 }
571 scp->active = ATA_IDLE;
572 scp->running = NULL;
573 ata_start(scp);
574}
575
576void
577ata_start(struct ata_softc *scp)
578{
579 struct ad_request *ad_request;
580 struct atapi_request *atapi_request;
581
582#ifdef ATA_DEBUG
583 printf("ata_start: entered\n");
584#endif
585 if (scp->active != ATA_IDLE)
586 return;
587
588#if NATADISK > 0
589 /* find & call the responsible driver if anything on ATA queue */
590 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
591 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
592 scp->active = ATA_ACTIVE_ATA;
593 scp->running = ad_request;
594 ad_transfer(ad_request);
595#ifdef ATA_DEBUG
596 printf("ata_start: started ata, leaving\n");
597#endif
598 return;
599 }
600#endif
601#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
602 /*
603 * find & call the responsible driver if anything on ATAPI queue.
604 * check for device busy by polling the DSC bit, if busy, check
605 * for requests to the other device on the channel (if any).
606 * if no request can be served, timeout a call to ata_start to
607 * try again in a moment. the timeout should probably scale
608 * so we dont use too much time polling for slow devices.
609 */
610 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
611 struct atapi_softc *atp = atapi_request->device;
612
613 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
614 DELAY(1);
615 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) {
616 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) {
617 if (atapi_request->device->unit != atp->unit) {
618 struct atapi_softc *tmpatp = atapi_request->device;
619
620 outb(tmpatp->controller->ioaddr + ATA_DRIVE,
621 ATA_D_IBM | tmpatp->unit);
622 DELAY(1);
623 if (!(inb(tmpatp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC))
624 atapi_request = NULL;
625 break;
626 }
627 }
628 }
629 if (!atapi_request) {
630 timeout((timeout_t*)ata_start, scp, 1);
631 return;
632 }
633 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
634 scp->active = ATA_ACTIVE_ATAPI;
635 scp->running = atapi_request;
636 atapi_transfer(atapi_request);
637#ifdef ATA_DEBUG
638 printf("ata_start: started atapi, leaving\n");
639#endif
640 return;
641 }
642#endif
643}
644
645void
646ata_reset(struct ata_softc *scp, int32_t *mask)
647{
648 int32_t timeout;
649 int8_t status0, status1;
650
651 /* reset channel */
652 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
653 DELAY(1);
654 inb(scp->ioaddr + ATA_STATUS);
655 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
656 DELAY(10000);
657 outb(scp->altioaddr, ATA_A_IDS);
658 DELAY(10000);
659 inb(scp->ioaddr + ATA_ERROR);
660 DELAY(1);
661 outb(scp->altioaddr, ATA_A_4BIT);
662 DELAY(1);
663
664 /* wait for BUSY to go inactive */
665 for (timeout = 0; timeout < 300000; timeout++) {
666 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
667 DELAY(1);
668 status0 = inb(scp->ioaddr + ATA_STATUS);
669 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
670 DELAY(1);
671 status1 = inb(scp->ioaddr + ATA_STATUS);
672 if (*mask == 0x01) /* wait for master only */
673 if (!(status0 & ATA_S_BSY))
674 break;
675 if (*mask == 0x02) /* wait for slave only */
676 if (!(status1 & ATA_S_BSY))
677 break;
678 if (*mask == 0x03) /* wait for both master & slave */
679 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY))
680 break;
681 DELAY(100);
682 }
683 if (status0 & ATA_S_BSY)
684 *mask &= ~0x01;
685 if (status1 & ATA_S_BSY)
686 *mask &= ~0x02;
687 if (bootverbose)
688 printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
689 scp->lun, *mask, status0, status1);
690}
691
692int32_t
693ata_reinit(struct ata_softc *scp)
694{
695 int32_t mask = 0;
696
697 printf("ata%d: resetting devices .. ", scp->lun);
698 scp->active = ATA_IDLE;
699 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
700 mask |= 0x01;
701 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
702 mask |= 0x02;
703 ata_reset(scp, &mask);
704
705#if NATADISK > 0
706 if (scp->devices & (ATA_ATA_MASTER))
707 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
708 if (scp->devices & (ATA_ATA_SLAVE))
709 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
710#endif
711#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
712 if (scp->devices & (ATA_ATAPI_MASTER))
713 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
714 if (scp->devices & (ATA_ATAPI_SLAVE))
715 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
716#endif
717 printf("done\n");
718 ata_start(scp);
719 return 0;
720}
721
722int32_t
723ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
724{
725 u_int8_t status;
726 u_int32_t timeout = 0;
727
728 while (timeout <= 5000000) { /* timeout 5 secs */
729 status = inb(scp->ioaddr + ATA_STATUS);
730
731 /* if drive fails status, reselect the drive just to be sure */
732 if (status == 0xff) {
733 printf("ata%d: %s: no status, reselecting device\n",
734 scp->lun, device?"slave":"master");
735 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
736 DELAY(1);
737 status = inb(scp->ioaddr + ATA_STATUS);
738 }
739 if (status == 0xff)
740 return -1;
741 scp->status = status;
742 if (!(status & ATA_S_BSY)) {
743 if (status & ATA_S_ERROR)
744 scp->error = inb(scp->ioaddr + ATA_ERROR);
745 if ((status & mask) == mask)
746 return (status & ATA_S_ERROR);
747 }
748 if (timeout > 1000) {
749 timeout += 1000;
750 DELAY(1000);
751 }
752 else {
753 timeout += 10;
754 DELAY(10);
755 }
756 }
757 return -1;
758}
759
760int32_t
761ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
762 u_int32_t cylinder, u_int32_t head, u_int32_t sector,
763 u_int32_t count, u_int32_t feature, int32_t flags)
764{
765#ifdef ATA_DEBUG
766 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, "
767 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
768 scp->lun, scp->ioaddr, device, command,
769 cylinder, head, sector, count, flags);
770#endif
771
772 /* ready to issue command ? */
773 if (ata_wait(scp, device, 0) < 0) {
774 printf("ata%d-%s: timeout waiting to give command s=%02x e=%02x\n",
775 scp->lun, device?"slave":"master", scp->status, scp->error);
776 }
777 outb(scp->ioaddr + ATA_FEATURE, feature);
778 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
779 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
780 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
781 outb(scp->ioaddr + ATA_SECTOR, sector);
782 outb(scp->ioaddr + ATA_COUNT, count);
783
784 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE)
785 printf("DANGER active=%d\n", scp->active);
786
787 switch (flags) {
788 case ATA_WAIT_INTR:
789 scp->active = ATA_WAIT_INTR;
790 outb(scp->ioaddr + ATA_CMD, command);
791 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) {
792 printf("ata_command: timeout waiting for interrupt\n");
793 scp->active = ATA_IDLE;
794 return -1;
795 }
796 break;
797
798 case ATA_IGNORE_INTR:
799 scp->active = ATA_IGNORE_INTR;
800 outb(scp->ioaddr + ATA_CMD, command);
801 break;
802
803 case ATA_IMMEDIATE:
804 default:
805 outb(scp->ioaddr + ATA_CMD, command);
806 break;
807 }
808#ifdef ATA_DEBUG
809 printf("ata_command: leaving\n");
810#endif
811 return 0;
812}
813
814int8_t *
815ata_mode2str(int32_t mode)
816{
817 switch (mode) {
818 case ATA_MODE_PIO:
819 return "PIO";
820 case ATA_MODE_DMA:
821 return "DMA";
822 case ATA_MODE_UDMA33:
823 return "UDMA33";
824 case ATA_MODE_UDMA66:
825 return "UDMA66";
826 default:
827 return "???";
828 }
829}
830
831void
832bswap(int8_t *buf, int32_t len)
833{
834 u_int16_t *p = (u_int16_t*)(buf + len);
835
836 while (--p >= (u_int16_t*)buf)
837 *p = ntohs(*p);
838}
839
840void
841btrim(int8_t *buf, int32_t len)
842{
843 int8_t *p;
844
845 for (p = buf; p < buf+len; ++p)
846 if (!*p)
847 *p = ' ';
848 for (p = buf + len - 1; p >= buf && *p == ' '; --p)
849 *p = 0;
850}
851
852void
853bpack(int8_t *src, int8_t *dst, int32_t len)
854{
855 int32_t i, j, blank;
856
857 for (i = j = blank = 0 ; i < len-1; i++) {
858 if (blank && src[i] == ' ') continue;
859 if (blank && src[i] != ' ') {
860 dst[j++] = src[i];
861 blank = 0;
862 continue;
863 }
864 if (src[i] == ' ') {
865 blank = 1;
866 if (i == 0)
867 continue;
868 }
869 dst[j++] = src[i];
870 }
871 dst[j] = 0x00;
872}
873#endif /* NATA > 0 */
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
--- 11 unchanged lines hidden (view full) ---
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 51520 1999-09-21 19:50:40Z sos $
29 */
30
31#include "ata.h"
32#include "apm.h"
33
34#if NATA > 0
35
36#include "isa.h"
37#include "pci.h"
38#include "atadisk.h"
39#include "atapicd.h"
40#include "atapifd.h"
41#include "atapist.h"
42#include "opt_global.h"
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/interrupt.h>
47#include <sys/conf.h>
48#include <sys/disk.h>
49#include <sys/module.h>
50#include <sys/bus.h>
51#include <sys/buf.h>
52#include <sys/malloc.h>
53#include <sys/devicestat.h>
54#include <vm/vm.h>
55#include <vm/pmap.h>
56#include <machine/resource.h>
57#include <machine/bus.h>
58#include <sys/rman.h>
59#if NPCI > 0
60#include <pci/pcivar.h>
61#include <pci/pcireg.h>
62#endif
63#include <isa/isavar.h>
64#include <isa/isareg.h>
65#include <machine/clock.h>
66#ifdef __i386__
67#include <machine/smp.h>
68#include <i386/isa/intr_machdep.h>
69#endif
70#if NAPM > 0
71#include <machine/apm_bios.h>
72#endif
73#include <dev/ata/ata-all.h>
74#include <dev/ata/ata-disk.h>
75#include <dev/ata/atapi-all.h>
76
77/* misc defines */
78#if SMP == 0
79#define isa_apic_irq(x) x
80#endif
81
82/* prototypes */
83#if NPCI > 0
84static void promise_intr(void *);
85#endif
86static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
87static void ataintr(void *);
--- 45 unchanged lines hidden (view full) ---
133
134 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
135
136 if (res) {
137 isa_set_portsize(dev, res);
138 *(int *)device_get_softc(dev) = lun;
139 return 0;
140 }
141 return ENXIO;
142}
143
144static int
145ata_isaattach(device_t dev)
146{
147 struct resource *port;
148 struct resource *irq;
--- 25 unchanged lines hidden (view full) ---
174
175static driver_t ata_isa_driver = {
176 "ata",
177 ata_isa_methods,
178 sizeof(int),
179};
180
181DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
182#endif
183
184#if NPCI > 0
185static const char *
186ata_pcimatch(device_t dev)
187{
188 u_int32_t data;
189
190 data = pci_read_config(dev, PCI_CLASS_REG, 4);
191 if (pci_get_class(dev) == PCIC_STORAGE &&
192 (pci_get_subclass(dev) == PCIS_STORAGE_IDE ||
--- 7 unchanged lines hidden (view full) ---
200 case 0x71118086:
201 return "Intel PIIX4 IDE controller";
202 case 0x4d33105a:
203 return "Promise Ultra/33 IDE controller";
204 case 0x4d38105a:
205 return "Promise Ultra/66 IDE controller";
206 case 0x522910b9:
207 return "AcerLabs Aladdin IDE controller";
208 case 0x05711106: /* 82c586 */
209 case 0x05961106: /* 82c596 */
210 return "VIA Apollo IDE controller (generic mode)";
211 case 0x06401095:
212 return "CMD 640 IDE controller (generic mode)";
213 case 0x06461095:
214 return "CMD 646 IDE controller (generic mode)";
215 case 0xc6931080:
216 return "Cypress 82C693 IDE controller (generic mode)";
217 case 0x01021078:
218 return "Cyrix 5530 IDE controller (generic mode)";
219 default:
220 return "Unknown PCI IDE controller (generic mode)";
221 }
222 }
223 return NULL;
224}
225
226static int
227ata_pciprobe(device_t dev)
228{
--- 31 unchanged lines hidden (view full) ---
260
261 /* if this is a Promise controller handle it specially */
262 if (type == 0x4d33105a || type == 0x4d38105a) {
263 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
264 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
265 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
266 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
267 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
268 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
269 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
270 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
271 printf("ata-pci%d: Busmastering DMA supported\n", unit);
272 }
273 /* everybody else seems to do it this way */
274 else {
275 if ((unit == 0) &&
276 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) {
--- 13 unchanged lines hidden (view full) ---
290 irq2 = 15;
291 }
292 else {
293 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
294 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
295 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
296 }
297
298 /* is this controller busmaster capable ? */
299 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
300 /* is busmastering support turned on ? */
301 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
302 /* is there a valid port range to connect to ? */
303 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
304 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
305 printf("ata-pci%d: Busmastering DMA supported\n", unit);
306 }
307 else
308 printf("ata-pci%d: Busmastering DMA not configured\n",unit);
309 }
310 else
311 printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
312 }
313 else
314 printf("ata-pci%d: Busmastering DMA not supported\n", unit);
315 }
316
317 /* now probe the addresse found for "real" ATA/ATAPI hardware */
318 lun = 0;
319 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
320 scp = atadevices[lun];
321 if (iobase_1 == IO_WD1)
322#ifdef __i386__
323 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
324 &bio_imask, INTR_EXCL);
325#endif
326#ifdef __alpha__
327 alpha_platform_setup_ide_intr(0, ataintr, scp);
328#endif
329 else {
330 struct resource *irq;
331 int rid = 0;
332 void *ih;
333
334 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
335 RF_SHAREABLE | RF_ACTIVE);
336 if (!irq)
337 printf("ata_pciattach: Unable to alloc interrupt\n");
338
339 if (type == 0x4d33105a || type == 0x4d38105a)
340 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih);
341 else
342 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
343 }
344 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
345 lun, iobase_1, isa_apic_irq(irq1), unit);
346 }
347 lun = 1;
348 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
349 scp = atadevices[lun];
350 if (iobase_2 == IO_WD2)
351#ifdef __i386__
352 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
353 &bio_imask, INTR_EXCL);
354#endif
355#ifdef __alpha__
356 alpha_platform_setup_ide_intr(1, ataintr, scp);
357#endif
358 else {
359 struct resource *irq;
360 int rid = 0;
361 void *ih;
362
363 if (type != 0x4d33105a && type != 0x4d38105a) {
364 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
365 RF_SHAREABLE | RF_ACTIVE);
366 if (!irq)
367 printf("ata_pciattach: Unable to alloc interrupt\n");
368
369 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
370 }
371 }
372 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
373 lun, iobase_2, isa_apic_irq(irq2), unit);
374 }
--- 30 unchanged lines hidden (view full) ---
405#endif
406
407static int32_t
408ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
409 device_t dev, int32_t *unit)
410{
411 struct ata_softc *scp = atadevices[atanlun];
412 int32_t mask = 0;
413 int32_t lun = atanlun;
414 u_int8_t status0, status1;
415
416#ifdef ATA_STATIC_ID
417 atanlun++;
418#endif
419 if (lun > MAXATA) {
420 printf("ata: unit out of range(%d)\n", lun);
--- 11 unchanged lines hidden (view full) ---
432 bzero(scp, sizeof(struct ata_softc));
433
434 scp->unit = *unit;
435 scp->lun = lun;
436 scp->ioaddr = ioaddr;
437 scp->altioaddr = altioaddr;
438 scp->active = ATA_IDLE;
439
440 if (bootverbose)
441 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n",
442 scp->lun, scp->ioaddr, scp->altioaddr);
443
444
445 /* do we have any signs of ATA/ATAPI HW being present ? */
446 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
447 DELAY(1);
448 status0 = inb(scp->ioaddr + ATA_STATUS);
449 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
450 DELAY(1);
451 status1 = inb(scp->ioaddr + ATA_STATUS);
452 if ((status0 & 0xf8) != 0xf8)
453 mask |= 0x01;
454 if ((status1 & 0xf8) != 0xf8)
455 mask |= 0x02;
456 if (bootverbose)
457 printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
458 scp->lun, mask, status0, status1);
459 if (!mask) {
460 free(scp, M_DEVBUF);
461 return 0;
462 }
463 ata_reset(scp, &mask);
464 if (!mask) {
465 free(scp, M_DEVBUF);
466 return 0;
467 }
468 /*
469 * OK, we have at least one device on the chain,
470 * check for ATAPI signatures, if none check if its
471 * a good old ATA device.
472 */
473 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
474 DELAY(1);
475 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
476 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
477 scp->devices |= ATA_ATAPI_MASTER;
478 }
479 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
480 DELAY(1);
481 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
482 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
483 scp->devices |= ATA_ATAPI_SLAVE;
484 }
485 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
486 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
487 DELAY(1);
488 outb(scp->ioaddr + ATA_ERROR, 0x58);
489 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
490 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
491 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
492 scp->devices |= ATA_ATA_MASTER;
493 }
494 }
495 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
496 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
497 DELAY(1);
498 outb(scp->ioaddr + ATA_ERROR, 0x58);
499 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
500 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
501 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
502 scp->devices |= ATA_ATA_SLAVE;
503 }
504 }
505 if (bootverbose)
506 printf("ata%d: devices = 0x%x\n", scp->lun, scp->devices);
507 if (!scp->devices) {
508 free(scp, M_DEVBUF);
509 return 0;
510 }
511 TAILQ_INIT(&scp->ata_queue);
512 TAILQ_INIT(&scp->atapi_queue);
513 *unit = scp->lun;
514 scp->dev = dev;
515 if (bmaddr)
516 scp->bmaddr = bmaddr;
517 atadevices[scp->lun] = scp;
518#ifndef ATA_STATIC_ID
519 atanlun++;
520#endif
521#if NAPM > 0
522 scp->resume_hook.ah_fun = (void *)ata_reinit;
523 scp->resume_hook.ah_arg = scp;
524 scp->resume_hook.ah_name = "ATA driver";
525 scp->resume_hook.ah_order = APM_MID_ORDER;
526 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
527#endif
528 return ATA_IOSIZE;
529}
530
531static void
532ataintr(void *data)
533{
534 struct ata_softc *scp =(struct ata_softc *)data;
535
536 scp->status = inb(scp->ioaddr + ATA_STATUS);
537
538 /* find & call the responsible driver to process this interrupt */
539 switch (scp->active) {
540#if NATADISK > 0
541 case ATA_ACTIVE_ATA:
542 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
543 return;
544 break;
545#endif
546#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
547 case ATA_ACTIVE_ATAPI:
548 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
549 return;
550 break;
551#endif
552 case ATA_WAIT_INTR:
553 wakeup((caddr_t)scp);
554 break;
555
556 case ATA_IGNORE_INTR:
557 break;
558
559 default:
560 case ATA_IDLE:
561#ifdef ATA_DEBUG
562 {
563 static int32_t intr_count = 0;
564 if (intr_count++ < 10)
565 printf("ata%d: unwanted interrupt %d status = %02x\n",
566 scp->lun, intr_count, scp->status);
567 }
568#endif
569 return;
570 }
571 scp->active = ATA_IDLE;
572 scp->running = NULL;
573 ata_start(scp);
574}
575
576void
577ata_start(struct ata_softc *scp)
578{
579 struct ad_request *ad_request;
580 struct atapi_request *atapi_request;
581
582#ifdef ATA_DEBUG
583 printf("ata_start: entered\n");
584#endif
585 if (scp->active != ATA_IDLE)
586 return;
587
588#if NATADISK > 0
589 /* find & call the responsible driver if anything on ATA queue */
590 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
591 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
592 scp->active = ATA_ACTIVE_ATA;
593 scp->running = ad_request;
594 ad_transfer(ad_request);
595#ifdef ATA_DEBUG
596 printf("ata_start: started ata, leaving\n");
597#endif
598 return;
599 }
600#endif
601#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
602 /*
603 * find & call the responsible driver if anything on ATAPI queue.
604 * check for device busy by polling the DSC bit, if busy, check
605 * for requests to the other device on the channel (if any).
606 * if no request can be served, timeout a call to ata_start to
607 * try again in a moment. the timeout should probably scale
608 * so we dont use too much time polling for slow devices.
609 */
610 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
611 struct atapi_softc *atp = atapi_request->device;
612
613 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
614 DELAY(1);
615 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) {
616 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) {
617 if (atapi_request->device->unit != atp->unit) {
618 struct atapi_softc *tmpatp = atapi_request->device;
619
620 outb(tmpatp->controller->ioaddr + ATA_DRIVE,
621 ATA_D_IBM | tmpatp->unit);
622 DELAY(1);
623 if (!(inb(tmpatp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC))
624 atapi_request = NULL;
625 break;
626 }
627 }
628 }
629 if (!atapi_request) {
630 timeout((timeout_t*)ata_start, scp, 1);
631 return;
632 }
633 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
634 scp->active = ATA_ACTIVE_ATAPI;
635 scp->running = atapi_request;
636 atapi_transfer(atapi_request);
637#ifdef ATA_DEBUG
638 printf("ata_start: started atapi, leaving\n");
639#endif
640 return;
641 }
642#endif
643}
644
645void
646ata_reset(struct ata_softc *scp, int32_t *mask)
647{
648 int32_t timeout;
649 int8_t status0, status1;
650
651 /* reset channel */
652 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
653 DELAY(1);
654 inb(scp->ioaddr + ATA_STATUS);
655 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
656 DELAY(10000);
657 outb(scp->altioaddr, ATA_A_IDS);
658 DELAY(10000);
659 inb(scp->ioaddr + ATA_ERROR);
660 DELAY(1);
661 outb(scp->altioaddr, ATA_A_4BIT);
662 DELAY(1);
663
664 /* wait for BUSY to go inactive */
665 for (timeout = 0; timeout < 300000; timeout++) {
666 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
667 DELAY(1);
668 status0 = inb(scp->ioaddr + ATA_STATUS);
669 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
670 DELAY(1);
671 status1 = inb(scp->ioaddr + ATA_STATUS);
672 if (*mask == 0x01) /* wait for master only */
673 if (!(status0 & ATA_S_BSY))
674 break;
675 if (*mask == 0x02) /* wait for slave only */
676 if (!(status1 & ATA_S_BSY))
677 break;
678 if (*mask == 0x03) /* wait for both master & slave */
679 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY))
680 break;
681 DELAY(100);
682 }
683 if (status0 & ATA_S_BSY)
684 *mask &= ~0x01;
685 if (status1 & ATA_S_BSY)
686 *mask &= ~0x02;
687 if (bootverbose)
688 printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
689 scp->lun, *mask, status0, status1);
690}
691
692int32_t
693ata_reinit(struct ata_softc *scp)
694{
695 int32_t mask = 0;
696
697 printf("ata%d: resetting devices .. ", scp->lun);
698 scp->active = ATA_IDLE;
699 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
700 mask |= 0x01;
701 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
702 mask |= 0x02;
703 ata_reset(scp, &mask);
704
705#if NATADISK > 0
706 if (scp->devices & (ATA_ATA_MASTER))
707 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
708 if (scp->devices & (ATA_ATA_SLAVE))
709 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
710#endif
711#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
712 if (scp->devices & (ATA_ATAPI_MASTER))
713 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
714 if (scp->devices & (ATA_ATAPI_SLAVE))
715 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
716#endif
717 printf("done\n");
718 ata_start(scp);
719 return 0;
720}
721
722int32_t
723ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
724{
725 u_int8_t status;
726 u_int32_t timeout = 0;
727
728 while (timeout <= 5000000) { /* timeout 5 secs */
729 status = inb(scp->ioaddr + ATA_STATUS);
730
731 /* if drive fails status, reselect the drive just to be sure */
732 if (status == 0xff) {
733 printf("ata%d: %s: no status, reselecting device\n",
734 scp->lun, device?"slave":"master");
735 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
736 DELAY(1);
737 status = inb(scp->ioaddr + ATA_STATUS);
738 }
739 if (status == 0xff)
740 return -1;
741 scp->status = status;
742 if (!(status & ATA_S_BSY)) {
743 if (status & ATA_S_ERROR)
744 scp->error = inb(scp->ioaddr + ATA_ERROR);
745 if ((status & mask) == mask)
746 return (status & ATA_S_ERROR);
747 }
748 if (timeout > 1000) {
749 timeout += 1000;
750 DELAY(1000);
751 }
752 else {
753 timeout += 10;
754 DELAY(10);
755 }
756 }
757 return -1;
758}
759
760int32_t
761ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
762 u_int32_t cylinder, u_int32_t head, u_int32_t sector,
763 u_int32_t count, u_int32_t feature, int32_t flags)
764{
765#ifdef ATA_DEBUG
766 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, "
767 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
768 scp->lun, scp->ioaddr, device, command,
769 cylinder, head, sector, count, flags);
770#endif
771
772 /* ready to issue command ? */
773 if (ata_wait(scp, device, 0) < 0) {
774 printf("ata%d-%s: timeout waiting to give command s=%02x e=%02x\n",
775 scp->lun, device?"slave":"master", scp->status, scp->error);
776 }
777 outb(scp->ioaddr + ATA_FEATURE, feature);
778 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
779 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
780 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
781 outb(scp->ioaddr + ATA_SECTOR, sector);
782 outb(scp->ioaddr + ATA_COUNT, count);
783
784 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE)
785 printf("DANGER active=%d\n", scp->active);
786
787 switch (flags) {
788 case ATA_WAIT_INTR:
789 scp->active = ATA_WAIT_INTR;
790 outb(scp->ioaddr + ATA_CMD, command);
791 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) {
792 printf("ata_command: timeout waiting for interrupt\n");
793 scp->active = ATA_IDLE;
794 return -1;
795 }
796 break;
797
798 case ATA_IGNORE_INTR:
799 scp->active = ATA_IGNORE_INTR;
800 outb(scp->ioaddr + ATA_CMD, command);
801 break;
802
803 case ATA_IMMEDIATE:
804 default:
805 outb(scp->ioaddr + ATA_CMD, command);
806 break;
807 }
808#ifdef ATA_DEBUG
809 printf("ata_command: leaving\n");
810#endif
811 return 0;
812}
813
814int8_t *
815ata_mode2str(int32_t mode)
816{
817 switch (mode) {
818 case ATA_MODE_PIO:
819 return "PIO";
820 case ATA_MODE_DMA:
821 return "DMA";
822 case ATA_MODE_UDMA33:
823 return "UDMA33";
824 case ATA_MODE_UDMA66:
825 return "UDMA66";
826 default:
827 return "???";
828 }
829}
830
831void
832bswap(int8_t *buf, int32_t len)
833{
834 u_int16_t *p = (u_int16_t*)(buf + len);
835
836 while (--p >= (u_int16_t*)buf)
837 *p = ntohs(*p);
838}
839
840void
841btrim(int8_t *buf, int32_t len)
842{
843 int8_t *p;
844
845 for (p = buf; p < buf+len; ++p)
846 if (!*p)
847 *p = ' ';
848 for (p = buf + len - 1; p >= buf && *p == ' '; --p)
849 *p = 0;
850}
851
852void
853bpack(int8_t *src, int8_t *dst, int32_t len)
854{
855 int32_t i, j, blank;
856
857 for (i = j = blank = 0 ; i < len-1; i++) {
858 if (blank && src[i] == ' ') continue;
859 if (blank && src[i] != ' ') {
860 dst[j++] = src[i];
861 blank = 0;
862 continue;
863 }
864 if (src[i] == ' ') {
865 blank = 1;
866 if (i == 0)
867 continue;
868 }
869 dst[j++] = src[i];
870 }
871 dst[j] = 0x00;
872}
873#endif /* NATA > 0 */