Deleted Added
sdiff udiff text old ( 56988 ) new ( 57325 )
full compact
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

--- 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 56988 2000-02-04 10:20:22Z sos $
29 */
30
31#include "ata.h"
32#include "apm.h"
33#include "isa.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include "opt_ata.h"
41#include <sys/param.h>

--- 18 unchanged lines hidden (view full) ---

60#endif
61#include <isa/isavar.h>
62#include <isa/isareg.h>
63#include <machine/clock.h>
64#ifdef __i386__
65#include <machine/smp.h>
66#include <i386/isa/intr_machdep.h>
67#endif
68#if NAPM > 0
69#include <machine/apm_bios.h>
70#endif
71#include <dev/ata/ata-all.h>
72#include <dev/ata/ata-disk.h>
73#include <dev/ata/atapi-all.h>
74
75/* misc defines */
76#if SMP == 0
77#define isa_apic_irq(x) x
78#endif
79#define IOMASK 0xfffffffc
80
81/* prototypes */
82static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
83static void ata_attach(void *);
84static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
85static void ataintr(void *);
86static int8_t *active2str(int32_t);
87static void bswap(int8_t *, int32_t);
88static void btrim(int8_t *, int32_t);
89static void bpack(int8_t *, int8_t *, int32_t);
90
91/* local vars */
92static int32_t atanlun = 2;
93static struct intr_config_hook *ata_attach_hook = NULL;
94static devclass_t ata_devclass;
95struct ata_softc *atadevices[MAXATA];
96MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
97
98#if NISA > 0
99static struct isa_pnp_id ata_ids[] = {
100 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
101 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
102 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
103 {0x0306d041, "Generic ATA"}, /* PNP0603 */
104 {0}
105};
106
107static int
108ata_isaprobe(device_t dev)
109{
110 struct resource *port;
111 int rid;
112 int32_t ctlr, res;
113 int32_t lun;
114
115 /* Check isapnp ids */
116 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
117 return ENXIO;
118
119 /* Allocate the port range */
120 rid = 0;
121 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
122 if (!port)
123 return ENOMEM;
124
125 /* check if allready in use by a PCI device */
126 for (ctlr = 0; ctlr < atanlun; ctlr++) {
127 if (atadevices[ctlr] && atadevices[ctlr]->ioaddr==rman_get_start(port)){
128 printf("ata-isa%d: already registered as ata%d\n",
129 device_get_unit(dev), ctlr);
130 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
131 return ENXIO;
132 }
133 }
134
135 lun = 0;
136 res = ata_probe(rman_get_start(port), rman_get_start(port) + ATA_ALTPORT,
137 0, dev, &lun);
138
139 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
140
141 if (res) {
142 isa_set_portsize(dev, res);
143 *(int *)device_get_softc(dev) = lun;
144 atadevices[lun]->flags |= ATA_USE_16BIT;
145 return 0;
146 }
147 return ENXIO;
148}
149
150static int
151ata_isaattach(device_t dev)
152{
153 struct resource *port;
154 struct resource *irq;
155 void *ih;
156 int rid;
157
158 /* Allocate the port range and interrupt */
159 rid = 0;
160 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
161 if (!port)
162 return (ENOMEM);
163
164 rid = 0;
165 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
166 if (!irq) {
167 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
168 return (ENOMEM);
169 }
170 return bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr,
171 atadevices[*(int *)device_get_softc(dev)], &ih);
172}
173
174static device_method_t ata_isa_methods[] = {
175 /* Device interface */
176 DEVMETHOD(device_probe, ata_isaprobe),
177 DEVMETHOD(device_attach, ata_isaattach),
178 { 0, 0 }
179};
180
181static driver_t ata_isa_driver = {
182 "ata",
183 ata_isa_methods,
184 sizeof(int),
185};
186
187DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
188#endif
189
190#if NPCI > 0
191static const char *
192ata_pcimatch(device_t dev)
193{
194 if (pci_get_class(dev) != PCIC_STORAGE)
195 return NULL;
196
197 switch (pci_get_devid(dev)) {
198 /* supported chipsets */
199 case 0x12308086:
200 return "Intel PIIX ATA controller";
201
202 case 0x70108086:
203 return "Intel PIIX3 ATA controller";
204
205 case 0x71118086:
206 case 0x71998086:
207 return "Intel PIIX4 ATA-33 controller";
208
209 case 0x24118086:
210 return "Intel ICH ATA-66 controller";
211
212 case 0x24218086:
213 return "Intel ICH0 ATA-33 controller";
214
215 case 0x522910b9:
216 return "AcerLabs Aladdin ATA-33 controller";
217
218 case 0x05711106: /* 82c586 & 82c686 */
219 if (ata_find_dev(dev, 0x05861106))
220 return "VIA 82C586 ATA-33 controller";
221 if (ata_find_dev(dev, 0x05961106))
222 return "VIA 82C596 ATA-33 controller";
223 if (ata_find_dev(dev, 0x06861106))
224 return "VIA 82C686 ATA-66 controller";
225 return "VIA Apollo ATA controller";
226
227 case 0x55131039:
228 return "SiS 5591 ATA-33 controller";
229
230 case 0x74091022:
231 return "AMD 756 ATA-66 controller";
232
233 case 0x4d33105a:
234 return "Promise ATA-33 controller";
235
236 case 0x4d38105a:
237 return "Promise ATA-66 controller";
238
239 case 0x00041103:
240 return "HighPoint HPT366 ATA-66 controller";
241
242 /* unsupported but known chipsets, generic DMA only */
243 case 0x06401095:
244 return "CMD 640 ATA controller (generic mode)";
245
246 case 0x06461095:
247 return "CMD 646 ATA controller (generic mode)";
248
249 case 0xc6931080:
250 return "Cypress 82C693 ATA controller (generic mode)";
251
252 case 0x01021078:
253 return "Cyrix 5530 ATA controller (generic mode)";
254
255 default:
256 if (pci_get_class(dev) == PCIC_STORAGE &&
257 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
258 return "Unknown PCI ATA controller (generic mode)";
259 }
260 return NULL;
261}
262
263static int
264ata_pciprobe(device_t dev)
265{
266 const char *desc = ata_pcimatch(dev);
267
268 if (desc) {
269 device_set_desc(dev, desc);
270 return 0;
271 }
272 else
273 return ENXIO;
274}
275
276static int
277ata_pciattach(device_t dev)
278{
279 int unit = device_get_unit(dev);
280 struct ata_softc *scp;
281 u_int32_t type;
282 u_int8_t class, subclass;
283 u_int32_t cmd;
284 int32_t iobase_1, iobase_2, altiobase_1, altiobase_2;
285 int32_t bmaddr_1 = 0, bmaddr_2 = 0, irq1, irq2;
286 struct resource *irq = NULL;
287 int32_t lun;
288
289 /* set up vendor-specific stuff */
290 type = pci_get_devid(dev);
291 class = pci_get_class(dev);
292 subclass = pci_get_subclass(dev);
293 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
294
295#ifdef ATA_DEBUG
296 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x if=%02x\n",
297 unit, type, class, subclass, cmd, pci_get_progif(dev));
298#endif
299
300 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
301 iobase_1 = IO_WD1;
302 altiobase_1 = iobase_1 + ATA_ALTPORT;
303 irq1 = 14;
304 }
305 else {
306 iobase_1 = pci_read_config(dev, 0x10, 4) & IOMASK;
307 altiobase_1 = pci_read_config(dev, 0x14, 4) & IOMASK;
308 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
309 /* this is needed for old non-std systems */
310 if (iobase_1 == IO_WD1 && irq1 == 0x00)
311 irq1 = 14;
312 }
313
314 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
315 iobase_2 = IO_WD2;
316 altiobase_2 = iobase_2 + ATA_ALTPORT;
317 irq2 = 15;
318 }
319 else {
320 iobase_2 = pci_read_config(dev, 0x18, 4) & IOMASK;
321 altiobase_2 = pci_read_config(dev, 0x1c, 4) & IOMASK;
322 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
323 /* this is needed for old non-std systems */
324 if (iobase_2 == IO_WD2 && irq2 == 0x00)
325 irq2 = 15;
326 }
327
328 /* is this controller busmaster DMA capable ? */
329 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
330 /* is busmastering support turned on ? */
331 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
332 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
333 /* is there a valid port range to connect to ? */
334 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK))
335 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
336 else
337 printf("ata-pci%d: Busmastering DMA not configured\n", unit);
338 }
339 else
340 printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
341 }
342 else {
343 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
344 /* Promise and HPT366 controllers support busmastering DMA */
345 bmaddr_1 = pci_read_config(dev, 0x20, 4) & IOMASK;
346 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
347 }
348 else
349 /* we dont know this controller, no busmastering DMA */
350 printf("ata-pci%d: Busmastering DMA not supported\n", unit);
351 }
352
353 /* do extra chipset specific setups */
354 switch (type) {
355 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
356 pci_write_config(dev, 0x53,
357 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
358 break;
359 case 0x4d38105a: /* Promise 66's need their clock changed */
360 outb(bmaddr_1 + 0x11, inb(bmaddr_1 + 0x11) | 0x0a);
361 /* FALLTHROUGH */
362
363 case 0x4d33105a: /* Promise's need burst mode to be turned on */
364 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
365 break;
366
367 case 0x00041103: /* HPT366 turn of fast interrupt prediction */
368 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
369 break;
370
371 case 0x05711106:
372 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */

--- 10 unchanged lines hidden (view full) ---

383 /* set DMA read & end-of-sector fifo flush */
384 pci_write_config(dev, 0x46,
385 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
386
387 /* set sector size */
388 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
389 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
390
391 /* set the chiptype to the hostchip ID, makes life easier */
392 if (ata_find_dev(dev, 0x05861106))
393 type = 0x05861106;
394 if (ata_find_dev(dev, 0x05961106))
395 type = 0x05961106;
396 if (ata_find_dev(dev, 0x06861106)) {
397 type = 0x06861106;
398 /* prepare for ATA-66 on the 82C686 */
399 pci_write_config(dev, 0x50,
400 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
401 }
402 break;
403 }
404
405 /* now probe the addresse found for "real" ATA/ATAPI hardware */
406 lun = 0;
407 if (iobase_1 && ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
408 int rid;
409 void *ih;
410
411 scp = atadevices[lun];
412 scp->chiptype = type;
413 rid = 0;
414 if (iobase_1 == IO_WD1) {
415#ifdef __alpha__
416 alpha_platform_setup_ide_intr(0, ataintr, scp);
417#else
418 bus_set_resource(dev, SYS_RES_IRQ, rid, irq1, 1);
419 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
420 RF_SHAREABLE | RF_ACTIVE)))
421 printf("ata_pciattach: Unable to alloc interrupt\n");
422#endif
423 } else {
424 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
425 RF_SHAREABLE | RF_ACTIVE)))
426 printf("ata_pciattach: Unable to alloc interrupt\n");
427 }
428 if (irq)
429 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
430 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
431 lun, iobase_1, isa_apic_irq(irq1), unit);
432 }
433 lun = 1;
434 if (iobase_2 && ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
435 int rid;
436 void *ih;
437
438 scp = atadevices[lun];
439 scp->chiptype = type;
440 if (iobase_2 == IO_WD2) {
441#ifdef __alpha__
442 alpha_platform_setup_ide_intr(1, ataintr, scp);
443#else
444 rid = 1;
445 bus_set_resource(dev, SYS_RES_IRQ, rid, irq2, 1);
446 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
447 RF_SHAREABLE | RF_ACTIVE)))
448 printf("ata_pciattach: Unable to alloc interrupt\n");
449#endif
450 } else {
451 rid = 0;
452 if (irq1 != irq2 || irq == NULL) {
453 if (!(irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
454 RF_SHAREABLE | RF_ACTIVE)))
455 printf("ata_pciattach: Unable to alloc interrupt\n");
456 }
457 }
458 if (irq)
459 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
460 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
461 lun, iobase_2, isa_apic_irq(irq2), unit);
462 }
463 return 0;
464}
465
466int32_t
467ata_find_dev(device_t dev, int32_t type)
468{
469 device_t *children, child;
470 int nchildren, i;
471
472 if (device_get_children(device_get_parent(dev), &children, &nchildren))
473 return 0;
474
475 for (i = 0; i < nchildren; i++) {
476 child = children[i];
477
478 /* check that it's on the same silicon and the device we want */
479 if (pci_get_slot(dev) == pci_get_slot(child) &&
480 pci_get_vendor(child) == (type & 0xffff) &&
481 pci_get_device(child) == ((type & 0xffff0000)>>16)) {
482 free(children, M_TEMP);
483 return 1;
484 }
485 }
486 free(children, M_TEMP);
487 return 0;
488}
489
490static device_method_t ata_pci_methods[] = {
491 /* Device interface */
492 DEVMETHOD(device_probe, ata_pciprobe),
493 DEVMETHOD(device_attach, ata_pciattach),
494 { 0, 0 }
495};
496
497static driver_t ata_pci_driver = {
498 "ata-pci",
499 ata_pci_methods,
500 sizeof(int),
501};
502
503DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0);
504#endif
505
506static int32_t
507ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
508 device_t dev, int32_t *unit)
509{
510 struct ata_softc *scp;
511 int32_t lun, mask = 0;
512 u_int8_t status0, status1;
513
514 if (atanlun > MAXATA) {
515 printf("ata: unit out of range(%d)\n", atanlun);
516 return 0;
517 }
518
519 /* check if this is located at one of the std addresses */
520 if (ioaddr == IO_WD1)
521 lun = 0;
522 else if (ioaddr == IO_WD2)
523 lun = 1;
524 else
525 lun = atanlun++;
526
527 if ((scp = atadevices[lun])) {
528 ata_printf(scp, -1, "unit already attached\n");
529 return 0;
530 }
531 scp = malloc(sizeof(struct ata_softc), M_ATA, M_NOWAIT);
532 if (scp == NULL) {
533 ata_printf(scp, -1, "failed to allocate driver storage\n");
534 return 0;
535 }
536 bzero(scp, sizeof(struct ata_softc));
537
538 scp->ioaddr = ioaddr;
539 scp->altioaddr = altioaddr;
540 scp->bmaddr = bmaddr;
541 scp->lun = lun;
542 scp->unit = *unit;
543 scp->active = ATA_IDLE;
544
545 if (bootverbose)
546 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
547 scp->ioaddr, scp->altioaddr, scp->bmaddr);
548
549 /* do we have any signs of ATA/ATAPI HW being present ? */
550 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
551 DELAY(1);
552 status0 = inb(scp->ioaddr + ATA_STATUS);
553 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
554 DELAY(1);
555 status1 = inb(scp->ioaddr + ATA_STATUS);
556 if ((status0 & 0xf8) != 0xf8)
557 mask |= 0x01;
558 if ((status1 & 0xf8) != 0xf8)
559 mask |= 0x02;
560 if (bootverbose)
561 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
562 mask, status0, status1);
563 if (!mask) {
564 free(scp, M_DEVBUF);
565 return 0;
566 }
567 ata_reset(scp, &mask);
568 if (!mask) {
569 free(scp, M_DEVBUF);
570 return 0;
571 }
572 /*
573 * OK, we have at least one device on the chain,
574 * check for ATAPI signatures, if none check if its
575 * a good old ATA device.
576 */
577 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
578 DELAY(1);
579 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
580 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
581 scp->devices |= ATA_ATAPI_MASTER;
582 }
583 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));

--- 20 unchanged lines hidden (view full) ---

604 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
605 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
606 scp->devices |= ATA_ATA_SLAVE;
607 }
608 }
609 if (bootverbose)
610 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
611 if (!scp->devices) {
612 free(scp, M_DEVBUF);
613 return 0;
614 }
615 TAILQ_INIT(&scp->ata_queue);
616 TAILQ_INIT(&scp->atapi_queue);
617 *unit = scp->lun;
618 scp->dev = dev;
619 atadevices[scp->lun] = scp;
620
621 /* register callback for when interrupts are enabled */
622 if (!ata_attach_hook) {
623 if (!(ata_attach_hook = (struct intr_config_hook *)
624 malloc(sizeof(struct intr_config_hook),
625 M_TEMP, M_NOWAIT))) {
626 ata_printf(scp, -1, "ERROR malloc attach_hook failed\n");
627 return 0;
628 }
629 bzero(ata_attach_hook, sizeof(struct intr_config_hook));
630
631 ata_attach_hook->ich_func = ata_attach;
632 if (config_intrhook_establish(ata_attach_hook) != 0) {
633 ata_printf(scp, -1, "config_intrhook_establish failed\n");
634 free(ata_attach_hook, M_TEMP);
635 }
636 }
637#if NAPM > 0
638 scp->resume_hook.ah_fun = (void *)ata_reinit;
639 scp->resume_hook.ah_arg = scp;
640 scp->resume_hook.ah_name = "ATA driver";
641 scp->resume_hook.ah_order = APM_MID_ORDER;
642 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
643#endif
644 return ATA_IOSIZE;
645}
646
647void
648ata_attach(void *dummy)
649{
650 int32_t ctlr;
651
652 /*
653 * run through atadevices[] and look for real ATA & ATAPI devices
654 * using the hints we found in the early probe to avoid probing
655 * of non-exsistent devices and thereby long delays
656 */
657 for (ctlr=0; ctlr<MAXATA; ctlr++) {
658 if (!atadevices[ctlr]) continue;
659 if (atadevices[ctlr]->devices & ATA_ATA_SLAVE)
660 if (ata_getparam(atadevices[ctlr], ATA_SLAVE, ATA_C_ATA_IDENTIFY))
661 atadevices[ctlr]->devices &= ~ATA_ATA_SLAVE;
662 if (atadevices[ctlr]->devices & ATA_ATAPI_SLAVE)
663 if (ata_getparam(atadevices[ctlr], ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
664 atadevices[ctlr]->devices &= ~ATA_ATAPI_SLAVE;
665 if (atadevices[ctlr]->devices & ATA_ATA_MASTER)
666 if (ata_getparam(atadevices[ctlr], ATA_MASTER, ATA_C_ATA_IDENTIFY))
667 atadevices[ctlr]->devices &= ~ATA_ATA_MASTER;
668 if (atadevices[ctlr]->devices & ATA_ATAPI_MASTER)
669 if (ata_getparam(atadevices[ctlr], ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
670 atadevices[ctlr]->devices &= ~ATA_ATAPI_MASTER;
671 }
672
673#if NATADISK > 0
674 /* now we know whats there, do the real attach, first the ATA disks */
675 for (ctlr=0; ctlr<MAXATA; ctlr++) {
676 if (!atadevices[ctlr]) continue;
677 if (atadevices[ctlr]->devices & ATA_ATA_MASTER)
678 ad_attach(atadevices[ctlr], ATA_MASTER);
679 if (atadevices[ctlr]->devices & ATA_ATA_SLAVE)
680 ad_attach(atadevices[ctlr], ATA_SLAVE);
681 }
682#endif
683#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
684 /* then the atapi devices */
685 for (ctlr=0; ctlr<MAXATA; ctlr++) {
686 if (!atadevices[ctlr]) continue;
687 if (atadevices[ctlr]->devices & ATA_ATAPI_MASTER)
688 atapi_attach(atadevices[ctlr], ATA_MASTER);
689 if (atadevices[ctlr]->devices & ATA_ATAPI_SLAVE)
690 atapi_attach(atadevices[ctlr], ATA_SLAVE);
691 }
692#endif
693 if (ata_attach_hook) {
694 config_intrhook_disestablish(ata_attach_hook);
695 free(ata_attach_hook, M_ATA);
696 ata_attach_hook = NULL;
697 }
698}
699
700static int32_t
701ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
702{
703 struct ata_params *ata_parm;
704 int8_t buffer[DEV_BSIZE];
705 int retry = 0;
706
707 /* select drive */
708 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
709 DELAY(1);
710
711 /* enable interrupts */
712 outb(scp->altioaddr, ATA_A_4BIT);
713 DELAY(1);
714
715 /* apparently some devices needs this repeated */
716 do {
717 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
718 ata_printf(scp, device, "identify failed\n");
719 return -1;
720 }
721 if (retry++ > 4) {
722 ata_printf(scp, device, "drive wont come ready after identify\n");
723 return -1;
724 }
725 } while (ata_wait(scp, device,
726 ((command == ATA_C_ATAPI_IDENTIFY) ?
727 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
728
729 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
730 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
731 if (!ata_parm) {
732 ata_printf(scp, device, "malloc for ata_param failed\n");
733 return -1;
734 }
735 bcopy(buffer, ata_parm, sizeof(struct ata_params));
736 if (command == ATA_C_ATA_IDENTIFY ||
737 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
738 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
739 bswap(ata_parm->model, sizeof(ata_parm->model));
740 btrim(ata_parm->model, sizeof(ata_parm->model));
741 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
742 bswap(ata_parm->revision, sizeof(ata_parm->revision));
743 btrim(ata_parm->revision, sizeof(ata_parm->revision));
744 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
745 scp->dev_param[ATA_DEV(device)] = ata_parm;
746 return 0;
747}
748
749static void
750ataintr(void *data)
751{
752 struct ata_softc *scp = (struct ata_softc *)data;
753 u_int8_t dmastat;
754
755 /*
756 * since we might share the IRQ with another device, and in some
757 * case with our twin channel, we only want to process interrupts
758 * that we know this channel generated.
759 */
760 switch (scp->chiptype) {
761#if NPCI > 0
762 case 0x00041103: /* HighPoint HPT366 */
763 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
764 return;
765 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
766 break;
767
768 case 0x4d33105a: /* Promise 33's */
769 case 0x4d38105a: /* Promise 66's */
770 if (!(inl((pci_read_config(scp->dev, 0x20, 4) & IOMASK) + 0x1c) &
771 ((scp->unit) ? 0x00004000 : 0x00000400)))
772 return;
773 /* FALLTHROUGH */
774#endif
775 default:
776 if (scp->flags & ATA_DMA_ACTIVE) {
777 if (!(dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT)
778 return;
779 else
780 outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);

--- 118 unchanged lines hidden (view full) ---

899#endif
900 scp->active = ATA_IDLE;
901}
902
903void
904ata_reset(struct ata_softc *scp, int32_t *mask)
905{
906 int32_t timeout;
907 int8_t status0, status1;
908
909 /* reset channel */
910 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
911 DELAY(1);
912 inb(scp->ioaddr + ATA_STATUS);
913 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
914 DELAY(10000);
915 outb(scp->altioaddr, ATA_A_IDS);

--- 33 unchanged lines hidden (view full) ---

949
950int32_t
951ata_reinit(struct ata_softc *scp)
952{
953 int32_t mask = 0, omask;
954
955 scp->active = ATA_REINITING;
956 scp->running = NULL;
957 ata_printf(scp, -1, "resetting devices .. ");
958 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
959 mask |= 0x01;
960 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
961 mask |= 0x02;
962 omask = mask;
963 ata_reset(scp, &mask);
964 if (omask != mask)
965 printf(" device dissapeared! %d ", omask & ~mask);
966
967#if NATADISK > 0
968 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
969 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
970 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
971 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
972#endif
973#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
974 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
975 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
976 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
977 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
978#endif
979 printf("done\n");
980 scp->active = ATA_IDLE;
981 ata_start(scp);
982 return 0;
983}
984
985int32_t
986ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
987{

--- 110 unchanged lines hidden (view full) ---

1098
1099 default:
1100 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1101 active2str(flags));
1102 }
1103 return 0;
1104}
1105
1106int8_t *
1107ata_mode2str(int32_t mode)
1108{
1109 switch (mode) {
1110 case ATA_PIO: return "BIOSPIO";
1111 case ATA_PIO0: return "PIO0";
1112 case ATA_PIO1: return "PIO1";
1113 case ATA_PIO2: return "PIO2";

--- 15 unchanged lines hidden (view full) ---

1129 case 0: return ATA_PIO0;
1130 case 1: return ATA_PIO1;
1131 case 2: return ATA_PIO2;
1132 case 3: return ATA_PIO3;
1133 case 4: return ATA_PIO4;
1134 }
1135}
1136
1137static int8_t *
1138active2str(int32_t active)
1139{
1140 static char buf[8];
1141
1142 switch (active) {
1143 case ATA_IDLE:
1144 return("ATA_IDLE");

--- 12 unchanged lines hidden (view full) ---

1157 case ATA_REINITING:
1158 return("ATA_REINITING");
1159 default:
1160 sprintf(buf, "0x%02x", active);
1161 return buf;
1162 }
1163}
1164
1165int32_t
1166ata_pmode(struct ata_params *ap)
1167{
1168 if (ap->atavalid & ATA_FLAG_64_70) {
1169 if (ap->apiomodes & 2) return 4;
1170 if (ap->apiomodes & 1) return 3;
1171 }
1172 if (ap->opiomode == 2) return 2;
1173 if (ap->opiomode == 1) return 1;
1174 if (ap->opiomode == 0) return 0;
1175 return -1;
1176}
1177
1178int32_t
1179ata_wmode(struct ata_params *ap)
1180{
1181 if (ap->wdmamodes & 4) return 2;
1182 if (ap->wdmamodes & 2) return 1;
1183 if (ap->wdmamodes & 1) return 0;
1184 return -1;
1185}
1186
1187int32_t
1188ata_umode(struct ata_params *ap)
1189{
1190 if (ap->atavalid & ATA_FLAG_88) {
1191 if (ap->udmamodes & 0x10) return (ap->cblid ? 4 : 2);
1192 if (ap->udmamodes & 0x08) return (ap->cblid ? 3 : 2);
1193 if (ap->udmamodes & 0x04) return 2;
1194 if (ap->udmamodes & 0x02) return 1;
1195 if (ap->udmamodes & 0x01) return 0;
1196 }
1197 return -1;
1198}
1199
1200static void
1201bswap(int8_t *buf, int32_t len)
1202{
1203 u_int16_t *p = (u_int16_t*)(buf + len);
1204
1205 while (--p >= (u_int16_t*)buf)
1206 *p = ntohs(*p);
1207}
1208
1209static void
1210btrim(int8_t *buf, int32_t len)
1211{
1212 int8_t *p;
1213
1214 for (p = buf; p < buf+len; ++p)
1215 if (!*p)
1216 *p = ' ';
1217 for (p = buf + len - 1; p >= buf && *p == ' '; --p)
1218 *p = 0;
1219}
1220
1221static void
1222bpack(int8_t *src, int8_t *dst, int32_t len)
1223{
1224 int32_t i, j, blank;
1225
1226 for (i = j = blank = 0 ; i < len; i++) {

--- 9 unchanged lines hidden (view full) ---

1236 continue;
1237 }
1238 dst[j++] = src[i];
1239 }
1240 if (j < len)
1241 dst[j] = 0x00;
1242}
1243
1244int32_t
1245ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1246{
1247 va_list ap;
1248 int ret;
1249
1250 if (device == -1)
1251 ret = printf("ata%d: ", scp->lun);
1252 else
1253 ret = printf("ata%d-%s: ", scp->lun,
1254 (device == ATA_MASTER) ? "master" : "slave");
1255 va_start(ap, fmt);
1256 ret += vprintf(fmt, ap);
1257 va_end(ap);
1258 return ret;
1259}
1260
1261static char ata_conf[1024];
1262
1263static void
1264ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1265{
1266 int32_t s = splbio();
1267
1268 while (scp->active != ATA_IDLE)
1269 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1270 scp->active = ATA_REINITING;
1271 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1272 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1273 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1274 scp->active = ATA_IDLE;
1275 ata_start(scp);
1276 splx(s);
1277}
1278
1279static int
1280sysctl_hw_ata SYSCTL_HANDLER_ARGS
1281{
1282 int error, i;
1283
1284 /* readout internal state */
1285 bzero(ata_conf, sizeof(ata_conf));
1286 for (i = 0; i < (atanlun << 1); i++) {
1287 if (!atadevices[i >> 1] || !atadevices[ i >> 1]->dev_softc[i & 1])
1288 strcat(ata_conf, "---,");
1289 else if (atadevices[i >> 1]->mode[i & 1] >= ATA_DMA)
1290 strcat(ata_conf, "dma,");
1291 else
1292 strcat(ata_conf, "pio,");
1293 }
1294 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1295 if (error == 0 && req->newptr != NULL) {
1296 char *ptr = ata_conf;
1297
1298 /* update internal state */
1299 i = 0;
1300 while (*ptr) {
1301 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1302 if (atadevices[i >> 1] &&
1303 atadevices[i >> 1]->dev_softc[i & 1] &&
1304 atadevices[i >>1 ]->mode[i & 1] >= ATA_DMA)
1305 ata_change_mode(atadevices[i >> 1],
1306 (i & 1) ? ATA_SLAVE : ATA_MASTER, ATA_PIO);
1307 }
1308 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1309 if (atadevices[i >> 1] &&
1310 atadevices[i >> 1]->dev_softc[i & 1] &&
1311 atadevices[i >> 1]->mode[i & 1] < ATA_DMA)
1312 ata_change_mode(atadevices[i >> 1],
1313 (i & 1) ? ATA_SLAVE : ATA_MASTER, ATA_DMA);
1314 }
1315 else if (strncmp(ptr, "---", 3))
1316 break;
1317 ptr+=3;
1318 if (*ptr++ != ',' || ++i > (atanlun << 1))
1319 break;
1320 }
1321 }
1322 return error;
1323}
1324
1325SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1326 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");