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