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 57325 2000-02-18 20:57:33Z sos $
29 */
30
31#include "ata.h"
32#include "isa.h"
33#include "card.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#ifdef __alpha__
69#include <machine/md_var.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#define IOMASK 0xfffffffc
77#define ATA_IOADDR_RID 0
78#define ATA_ALTIOADDR_RID 1
79#define ATA_BMADDR_RID 2
80
81/* prototypes */
82static int ata_probe(device_t);
83static int ata_attach(device_t);
84static int ata_detach(device_t);
85static int ata_resume(device_t);
86static void ata_boot_attach(void);
87static void ata_intr(void *);
88static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
89static int8_t *active2str(int32_t);
90static void bswap(int8_t *, int32_t);
91static void btrim(int8_t *, int32_t);
92static void bpack(int8_t *, int8_t *, int32_t);
93
94/* local vars */
95static devclass_t ata_devclass;
96static devclass_t ata_pci_devclass;
97static struct intr_config_hook *ata_delayed_attach = NULL;
98static char ata_conf[256];
99MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
100
101#if NISA > 0
102static struct isa_pnp_id ata_ids[] = {
103 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
104 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
105 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
106 {0x0306d041, "Generic ATA"}, /* PNP0603 */
107 {0}
108};
109
110static int
111ata_isa_probe(device_t dev)
112{
113 struct ata_softc *scp = device_get_softc(dev);
114 struct resource *port;
115 int rid;
116 u_long tmp;
117
118 /* check isapnp ids */
119 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
120 return ENXIO;
121
122 /* allocate the port range */
123 rid = ATA_IOADDR_RID;
124 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
126 if (!port)
127 return ENOMEM;
128
129 /* alloctate the altport range */
130 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
131 bus_set_resource(dev, SYS_RES_IOPORT, 1,
132 rman_get_start(port) + ATA_ALTPORT,
133 ATA_ALTIOSIZE);
134 }
135 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
136 scp->unit = device_get_unit(dev);
137 scp->flags |= ATA_USE_16BIT;
138 return ata_probe(dev);
139}
140
141static device_method_t ata_isa_methods[] = {
142 /* device interface */
143 DEVMETHOD(device_probe, ata_isa_probe),
144 DEVMETHOD(device_attach, ata_attach),
145 DEVMETHOD(device_resume, ata_resume),
146 { 0, 0 }
147};
148
149static driver_t ata_isa_driver = {
150 "ata",
151 ata_isa_methods,
152 sizeof(struct ata_softc),
153};
154
155DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
156#endif
157
158#if NCARD > 0
159static int
160ata_pccard_probe(device_t dev)
161{
162 struct ata_softc *scp = device_get_softc(dev);
163 struct resource *port;
164 int rid;
165 u_long tmp;
166
167 /* allocate the port range */
168 rid = ATA_IOADDR_RID;
169 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
170 ATA_IOSIZE, RF_ACTIVE);
171 if (!port)
172 return ENOMEM;
173
174 /* alloctate the altport range */
175 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
176 bus_set_resource(dev, SYS_RES_IOPORT, 1,
177 rman_get_start(port) + ATA_ALTPORT_PCCARD,
178 ATA_ALTIOSIZE);
179 }
180 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
181 scp->unit = device_get_unit(dev);
182 scp->flags |= ATA_USE_16BIT;
183 return ata_probe(dev);
184}
185
186static device_method_t ata_pccard_methods[] = {
187 /* device interface */
188 DEVMETHOD(device_probe, ata_pccard_probe),
189 DEVMETHOD(device_attach, ata_attach),
190 DEVMETHOD(device_detach, ata_detach),
191 DEVMETHOD(device_resume, ata_resume),
192 { 0, 0 }
193};
194
195static driver_t ata_pccard_driver = {
196 "ata",
197 ata_pccard_methods,
198 sizeof(struct ata_softc),
199};
200
201DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
202#endif
203
204#if NPCI > 0
205struct ata_pci_softc {
206 struct resource *bmio;
207 struct resource bmio_1;
208 struct resource bmio_2;
209 struct resource *irq;
210 int32_t irqcnt;
211};
212
213static int32_t
214ata_find_dev(device_t dev, int32_t type)
215{
216 device_t *children, child;
217 int nchildren, i;
218
219 if (device_get_children(device_get_parent(dev), &children, &nchildren))
220 return 0;
221
222 for (i = 0; i < nchildren; i++) {
223 child = children[i];
224
225 /* check that it's on the same silicon and the device we want */
226 if (pci_get_slot(dev) == pci_get_slot(child) &&
227 pci_get_vendor(child) == (type & 0xffff) &&
228 pci_get_device(child) == ((type & 0xffff0000) >> 16)) {
229 free(children, M_TEMP);
230 return 1;
231 }
232 }
233 free(children, M_TEMP);
234 return 0;
235}
236
237static const char *
238ata_pci_match(device_t dev)
239{
240 if (pci_get_class(dev) != PCIC_STORAGE)
241 return NULL;
242
243 switch (pci_get_devid(dev)) {
244 /* supported chipsets */
245 case 0x12308086:
246 return "Intel PIIX ATA controller";
247
248 case 0x70108086:
249 return "Intel PIIX3 ATA controller";
250
251 case 0x71118086:
252 case 0x71998086:
253 return "Intel PIIX4 ATA33 controller";
254
255 case 0x24118086:
256 return "Intel ICH ATA66 controller";
257
258 case 0x24218086:
259 return "Intel ICH0 ATA33 controller";
260
261 case 0x522910b9:
262 return "AcerLabs Aladdin ATA33 controller";
263
264 case 0x05711106:
265 if (ata_find_dev(dev, 0x05861106))
266 return "VIA 82C586 ATA33 controller";
267 if (ata_find_dev(dev, 0x05961106))
268 return "VIA 82C596 ATA33 controller";
269 if (ata_find_dev(dev, 0x06861106))
270 return "VIA 82C686 ATA66 controller";
271 return "VIA Apollo ATA controller";
272
273 case 0x55131039:
274 return "SiS 5591 ATA33 controller";
275
276 case 0x06461095:
277 return "CMD 646 ATA controller";
278
279 case 0x74091022:
280 return "AMD 756 ATA66 controller";
281
282 case 0x4d33105a:
283 return "Promise ATA33 controller";
284
285 case 0x4d38105a:
286 return "Promise ATA66 controller";
287
288 case 0x00041103:
289 return "HighPoint HPT366 ATA66 controller";
290
291 /* unsupported but known chipsets, generic DMA only */
292 case 0x10001042:
293 case 0x10011042:
294 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
295
296 case 0x06401095:
297 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
298
299 case 0xc6931080:
300 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
301 return "Cypress 82C693 ATA controller (generic mode)";
302 break;
303
304 case 0x01021078:
305 return "Cyrix 5530 ATA controller (generic mode)";
306
307 /* unknown chipsets, try generic DMA if it seems possible */
308 default:
309 if (pci_get_class(dev) == PCIC_STORAGE &&
310 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
311 return "Unknown PCI ATA controller (generic mode)";
312 }
313 return NULL;
314}
315
316static int
317ata_pci_probe(device_t dev)
318{
319 const char *desc = ata_pci_match(dev);
320
321 if (desc) {
322 device_set_desc(dev, desc);
323 return 0;
324 }
325 else
326 return ENXIO;
327}
328
329static int
330ata_pci_add_child(device_t dev, int unit)
331{
332 device_t child;
333 int lun;
334
335 /* check if this is located at one of the std addresses */
336 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
337 lun = unit;
338 else
339 lun = -1;
340
341 if (!(child = device_add_child(dev, "ata", lun)))
342 return ENOMEM;
343
344 device_set_ivars(child, (void *)(uintptr_t) unit);
345 return 0;
346}
347
348static int
349ata_pci_attach(device_t dev)
350{
351 struct ata_pci_softc *sc = device_get_softc(dev);
352 u_int8_t class, subclass;
353 u_int32_t type, cmd;
354 int rid;
355
356 /* set up vendor-specific stuff */
357 type = pci_get_devid(dev);
358 class = pci_get_class(dev);
359 subclass = pci_get_subclass(dev);
360 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
361
362 /* is this controller busmaster DMA capable ? */
363 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
364 /* is busmastering support turned on ? */
365 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
366 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
367
368 /* is there a valid port range to connect to ? */
369 rid = 0x20;
370 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
371 0, ~0, 1, RF_ACTIVE);
372 if (!sc->bmio)
373 device_printf(dev, "Busmastering DMA not configured\n");
374 }
375 else
376 device_printf(dev, "Busmastering DMA not enabled\n");
377 }
378 else {
379 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
380 /* Promise and HPT366 controllers support busmastering DMA */
381 rid = 0x20;
382 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
383 0, ~0, 1, RF_ACTIVE);
384 }
385 else
386 /* we dont know this controller, no busmastering DMA */
387 device_printf(dev, "Busmastering DMA not supported\n");
388 }
389
390 /* do extra chipset specific setups */
391 switch (type) {
392 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
393 pci_write_config(dev, 0x53,
394 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
395 break;
396
397 case 0x4d38105a: /* Promise 66's need their clock changed */
398 outb(rman_get_start(sc->bmio) + 0x11,
399 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
400 /* FALLTHROUGH */
401
402 case 0x4d33105a: /* Promise's need burst mode to be turned on */
403 outb(rman_get_start(sc->bmio) + 0x1f,
404 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
405 break;
406
407 case 0x00041103: /* HPT366 turn of fast interrupt prediction */
408 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
409 break;
410
411 case 0x05711106:
412 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */

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

423 /* set DMA read & end-of-sector fifo flush */
424 pci_write_config(dev, 0x46,
425 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
426
427 /* set sector size */
428 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
429 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
430
431 /* prepare for ATA-66 on the 82C686 */
432 if (ata_find_dev(dev, 0x06861106)) {
433 pci_write_config(dev, 0x50,
434 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
435 }
436 break;
437 }
438
439 ata_pci_add_child(dev, 0);
440
441 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV ||
442 pci_read_config(dev, 0x18, 4) & IOMASK)
443 ata_pci_add_child(dev, 1);
444
445 return bus_generic_attach(dev);
446}
447
448static int
449ata_pci_print_child(device_t dev, device_t child)
450{
451 struct ata_softc *scp = device_get_softc(child);
452 int unit = (uintptr_t) device_get_ivars(child);
453 int retval = 0;
454
455 retval += bus_print_child_header(dev, child);
456 retval += printf(": at 0x%x", scp->ioaddr);
457
458 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
459 retval += printf(" irq %d", 14 + unit);
460
461 retval += bus_print_child_footer(dev, child);
462
463 return retval;
464}
465
466static struct resource *
467ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
468 u_long start, u_long end, u_long count, u_int flags)
469{
470 struct ata_pci_softc *sc = device_get_softc(dev);
471 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
472 int unit = (int)device_get_ivars(child);
473 int myrid;
474
475 if (type == SYS_RES_IOPORT) {
476 switch (*rid) {
477 case ATA_IOADDR_RID:
478 if (masterdev) {
479 myrid = 0;
480 start = (unit == 0 ? IO_WD1 : IO_WD2);
481 end = start + ATA_IOSIZE - 1;
482 count = ATA_IOSIZE;
483 }
484 else
485 myrid = 0x10 + 8 * unit;
486 break;
487
488 case ATA_ALTIOADDR_RID:
489 if (masterdev) {
490 myrid = 0;
491 start = (unit == 0 ? IO_WD1 : IO_WD2) + ATA_ALTPORT;
492 end = start + ATA_ALTIOSIZE - 1;
493 count = ATA_ALTIOSIZE;
494 }
495 else
496 myrid = 0x14 + 8 * unit;
497 break;
498
499 case ATA_BMADDR_RID:
500 /* the busmaster resource is shared between the two channels */
501 if (sc->bmio) {
502 if (unit == 0) {
503 sc->bmio_1 = *sc->bmio;
504 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
505 return &sc->bmio_1;
506 } else {
507 sc->bmio_2 = *sc->bmio;
508 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
509 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
510 return &sc->bmio_2;
511 }
512 }
513 break;
514
515 default:
516 return 0;
517 }
518
519 if (masterdev)
520 /* make the parent just pass through the allocation. */
521 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
522 SYS_RES_IOPORT, &myrid,
523 start, end, count, flags);
524 else
525 /* we are using the parent resource directly. */
526 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
527 SYS_RES_IOPORT, &myrid,
528 start, end, count, flags);
529 }
530
531 if (type == SYS_RES_IRQ) {
532 if (*rid != 0)
533 return 0;
534
535 if (masterdev) {
536#ifdef __i386__
537 int irq = (unit == 0 ? 14 : 15);
538
539 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
540 SYS_RES_IRQ, rid,
541 irq, irq, 1, flags & ~RF_SHAREABLE);
542#else
543 return alpha_platform_alloc_ide_intr(unit);
544#endif
545 } else {
546 /* primary and secondary channels share the same interrupt */
547 sc->irqcnt++;
548 if (!sc->irq)
549 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
550 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
551 return sc->irq;
552 }
553 }
554 return 0;
555}
556
557static int
558ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
559 struct resource *r)
560{
561 struct ata_pci_softc *sc = device_get_softc(dev);
562 int unit = (uintptr_t) device_get_ivars(child);
563 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
564 int myrid = 0;
565
566 if (type == SYS_RES_IOPORT) {
567 switch (rid) {
568 case ATA_IOADDR_RID:
569 if (masterdev)
570 myrid = 0;
571 else
572 myrid = 0x10 + 8 * unit;
573 break;
574
575 case ATA_ALTIOADDR_RID:
576 if (masterdev)
577 myrid = 0;
578 else
579 myrid = 0x14 + 8 * unit;
580 break;
581
582 case ATA_BMADDR_RID:
583 return 0;
584
585 default:
586 return ENOENT;
587 }
588
589 if (masterdev)
590 /* make the parent just pass through the allocation. */
591 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
592 SYS_RES_IOPORT, myrid, r);
593 else
594 /* we are using the parent resource directly. */
595 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
596 SYS_RES_IOPORT, myrid, r);
597 }
598 if (type == SYS_RES_IRQ) {
599 if (rid != 0)
600 return ENOENT;
601
602 if (masterdev) {
603#ifdef __i386__
604 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
605 child, SYS_RES_IRQ, rid, r);
606#else
607 return alpha_platform_release_ide_intr(unit, r);
608#endif
609 }
610 else {
611 if (--sc->irqcnt)
612 return 0;
613
614 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
615 dev, SYS_RES_IRQ, rid, r);
616 }
617 }
618 return EINVAL;
619}
620
621static int
622ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
623 int flags, driver_intr_t *intr, void *arg,
624 void **cookiep)
625{
626 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
627#ifdef __i386__
628 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
629 flags, intr, arg, cookiep);
630#else
631 return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
632#endif
633 }
634 else
635 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
636 flags, intr, arg, cookiep);
637}
638
639static int
640ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
641 void *cookie)
642{
643 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
644#ifdef __i386__
645 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
646#else
647 return alpha_platform_teardown_ide_intr(irq, cookie);
648#endif
649 }
650 else
651 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
652}
653
654static device_method_t ata_pci_methods[] = {
655 /* device interface */
656 DEVMETHOD(device_probe, ata_pci_probe),
657 DEVMETHOD(device_attach, ata_pci_attach),
658 DEVMETHOD(device_shutdown, bus_generic_shutdown),
659 DEVMETHOD(device_suspend, bus_generic_suspend),
660 DEVMETHOD(device_resume, bus_generic_resume),
661
662 /* bus methods */
663 DEVMETHOD(bus_print_child, ata_pci_print_child),
664 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
665 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
666 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
667 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
668 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
669 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
670 { 0, 0 }
671};
672
673static driver_t ata_pci_driver = {
674 "atapci",
675 ata_pci_methods,
676 sizeof(struct ata_pci_softc),
677};
678
679DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_devclass, 0, 0);
680
681static int
682ata_pcisub_probe(device_t dev)
683{
684 struct ata_softc *scp = device_get_softc(dev);
685
686 /* kids of pci ata chipsets has their physical unit number in ivars */
687 scp->unit = (uintptr_t) device_get_ivars(dev);
688
689 /* set the chiptype to the hostchip ID, makes life easier */
690 if (ata_find_dev(device_get_parent(dev), 0x05861106))
691 scp->chiptype = 0x05861106;
692 else if (ata_find_dev(device_get_parent(dev), 0x05961106))
693 scp->chiptype = 0x05961106;
694 else if (ata_find_dev(device_get_parent(dev), 0x06861106))
695 scp->chiptype = 0x06861106;
696 else
697 scp->chiptype = pci_get_devid(device_get_parent(dev));
698 return ata_probe(dev);
699}
700
701static device_method_t ata_pcisub_methods[] = {
702 /* device interface */
703 DEVMETHOD(device_probe, ata_pcisub_probe),
704 DEVMETHOD(device_attach, ata_attach),
705 DEVMETHOD(device_detach, ata_detach),
706 DEVMETHOD(device_resume, ata_resume),
707 { 0, 0 }
708};
709
710static driver_t ata_pcisub_driver = {
711 "ata",
712 ata_pcisub_methods,
713 sizeof(struct ata_softc),
714};
715
716DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_pci_devclass, 0, 0);
717#endif
718
719static int
720ata_probe(device_t dev)
721{
722 struct ata_softc *scp = device_get_softc(dev);
723 struct resource *io = 0;
724 struct resource *altio = 0;
725 struct resource *bmio = 0;
726 int rid;
727 int32_t ioaddr, altioaddr, bmaddr;
728 int32_t mask = 0;
729 u_int8_t status0, status1;
730
731 if (!scp || scp->flags & ATA_ATTACHED)
732 return ENXIO;
733
734 /* initialize the softc basics */
735 scp->active = ATA_IDLE;
736 scp->dev = dev;
737 scp->devices = 0;
738
739 rid = ATA_IOADDR_RID;
740 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
741 if (!io)
742 goto failure;
743 ioaddr = rman_get_start(io);
744
745 rid = ATA_ALTIOADDR_RID;
746 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
747 if (!altio)
748 goto failure;
749 altioaddr = rman_get_start(altio);
750
751 rid = ATA_BMADDR_RID;
752 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
753 bmaddr = bmio ? rman_get_start(bmio) : 0;
754
755 /* store the IO resources for eventual later release */
756 scp->r_io = io;
757 scp->r_altio = altio;
758 scp->r_bmio = bmio;
759
760 /* store the physical IO addresse for easy access */
761 scp->ioaddr = ioaddr;
762 scp->altioaddr = altioaddr;
763 scp->bmaddr = bmaddr;
764
765 if (bootverbose)
766 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
767 scp->ioaddr, scp->altioaddr, scp->bmaddr);
768
769 /* do we have any signs of ATA/ATAPI HW being present ? */
770 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
771 DELAY(1);
772 status0 = inb(scp->ioaddr + ATA_STATUS);
773 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
774 DELAY(1);
775 status1 = inb(scp->ioaddr + ATA_STATUS);
776 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
777 mask |= 0x01;
778 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
779 mask |= 0x02;
780 if (bootverbose)
781 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
782 mask, status0, status1);
783 if (!mask)
784 goto failure;
785
786 ata_reset(scp, &mask);
787 if (!mask)
788 goto failure;
789
790 /*
791 * OK, we have at least one device on the chain, check for ATAPI
792 * signatures, if none check if its a good old ATA device.
793 */
794 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
795 DELAY(1);
796 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
797 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
798 scp->devices |= ATA_ATAPI_MASTER;
799 }
800 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));

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

821 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
822 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
823 scp->devices |= ATA_ATA_SLAVE;
824 }
825 }
826 if (bootverbose)
827 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
828 if (!scp->devices) {
829 goto failure;
830 }
831 TAILQ_INIT(&scp->ata_queue);
832 TAILQ_INIT(&scp->atapi_queue);
833 return 0;
834
835failure:
836 if (io)
837 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
838 if (altio)
839 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTIOADDR_RID, altio);
840 if (bmio)
841 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
842 if (bootverbose)
843 ata_printf(scp, -1, "probe allocation failed\n");
844 return ENXIO;
845}
846
847static int
848ata_attach(device_t dev)
849{
850 struct ata_softc *scp = device_get_softc(dev);
851 int rid = 0;
852 void *ih;
853
854 if (!scp || scp->flags & ATA_ATTACHED)
855 return ENXIO;
856
857 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
858 RF_SHAREABLE | RF_ACTIVE);
859 if (!scp->r_irq) {
860 ata_printf(scp, -1, "unable to allocate interrupt\n");
861 return ENXIO;
862 }
863 bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr, scp, &ih);
864
865 /*
866 * do not attach devices if we are in early boot, this is done later
867 * when interrupts are enabled by a hook into the boot process.
868 * otherwise attach what the probe has found in scp->devices.
869 */
870 if (!ata_delayed_attach) {
871 if (scp->devices & ATA_ATA_SLAVE)
872 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
873 scp->devices &= ~ATA_ATA_SLAVE;
874 if (scp->devices & ATA_ATAPI_SLAVE)
875 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
876 scp->devices &= ~ATA_ATAPI_SLAVE;
877 if (scp->devices & ATA_ATA_MASTER)
878 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
879 scp->devices &= ~ATA_ATA_MASTER;
880 if (scp->devices & ATA_ATAPI_MASTER)
881 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
882 scp->devices &= ~ATA_ATAPI_MASTER;
883#if NATADISK > 0
884 if (scp->devices & ATA_ATA_MASTER)
885 ad_attach(scp, ATA_MASTER);
886 if (scp->devices & ATA_ATA_SLAVE)
887 ad_attach(scp, ATA_SLAVE);
888#endif
889#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
890 if (scp->devices & ATA_ATAPI_MASTER)
891 atapi_attach(scp, ATA_MASTER);
892 if (scp->devices & ATA_ATAPI_SLAVE)
893 atapi_attach(scp, ATA_SLAVE);
894#endif
895 }
896 scp->flags |= ATA_ATTACHED;
897 return 0;
898}
899
900static int
901ata_detach(device_t dev)
902{
903 struct ata_softc *scp = device_get_softc(dev);
904
905 if (!scp || !(scp->flags & ATA_ATTACHED))
906 return ENXIO;
907
908#if NATADISK > 0
909 if (scp->devices & ATA_ATA_MASTER)
910 ad_detach(scp, ATA_MASTER);
911 if (scp->devices & ATA_ATA_SLAVE)
912 ad_detach(scp, ATA_SLAVE);
913#endif
914#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
915 if (scp->devices & ATA_ATAPI_MASTER)
916 atapi_detach(scp, ATA_MASTER);
917 if (scp->devices & ATA_ATAPI_SLAVE)
918 atapi_detach(scp, ATA_SLAVE);
919#endif
920 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
921 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
922 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
923 }
924 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
925 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
926 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
927 }
928 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
929 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
930 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
931 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
932 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTIOADDR_RID, scp->r_altio);
933 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
934 scp->flags &= ~ATA_ATTACHED;
935 return 0;
936}
937
938static int
939ata_resume(device_t dev)
940{
941 struct ata_softc *scp = device_get_softc(dev);
942
943 ata_reinit(scp);
944 return 0;
945}
946
947static int32_t
948ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
949{
950 struct ata_params *ata_parm;
951 int8_t buffer[DEV_BSIZE];
952 int retry = 0;
953
954 /* select drive */
955 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
956 DELAY(1);
957
958 /* enable interrupt */
959 outb(scp->altioaddr, ATA_A_4BIT);
960 DELAY(1);
961
962 /* apparently some devices needs this repeated */
963 do {
964 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
965 ata_printf(scp, device, "identify failed\n");
966 return -1;
967 }
968 if (retry++ > 4) {
969 ata_printf(scp, device, "identify retries exceeded\n");
970 return -1;
971 }
972 } while (ata_wait(scp, device,
973 ((command == ATA_C_ATAPI_IDENTIFY) ?
974 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
975
976 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
977 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
978 if (!ata_parm) {
979 ata_printf(scp, device, "malloc for identify data failed\n");
980 return -1;
981 }
982 bcopy(buffer, ata_parm, sizeof(struct ata_params));
983 if (command == ATA_C_ATA_IDENTIFY ||
984 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
985 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
986 bswap(ata_parm->model, sizeof(ata_parm->model));
987 btrim(ata_parm->model, sizeof(ata_parm->model));
988 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
989 bswap(ata_parm->revision, sizeof(ata_parm->revision));
990 btrim(ata_parm->revision, sizeof(ata_parm->revision));
991 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
992 scp->dev_param[ATA_DEV(device)] = ata_parm;
993 return 0;
994}
995
996static void
997ata_boot_attach(void)
998{
999 struct ata_softc *scp;
1000 int32_t ctlr;
1001
1002 /*
1003 * run through all ata devices and look for real ATA & ATAPI devices
1004 * using the hints we found in the early probe, this avoids some of
1005 * the delays probing of non-exsistent devices can cause.
1006 */
1007 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1008 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1009 continue;
1010 if (scp->devices & ATA_ATA_SLAVE)
1011 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
1012 scp->devices &= ~ATA_ATA_SLAVE;
1013 if (scp->devices & ATA_ATAPI_SLAVE)
1014 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
1015 scp->devices &= ~ATA_ATAPI_SLAVE;
1016 if (scp->devices & ATA_ATA_MASTER)
1017 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
1018 scp->devices &= ~ATA_ATA_MASTER;
1019 if (scp->devices & ATA_ATAPI_MASTER)
1020 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
1021 scp->devices &= ~ATA_ATAPI_MASTER;
1022 }
1023
1024#if NATADISK > 0
1025 /* now we know whats there, do the real attach, first the ATA disks */
1026 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1027 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1028 continue;
1029 if (scp->devices & ATA_ATA_MASTER)
1030 ad_attach(scp, ATA_MASTER);
1031 if (scp->devices & ATA_ATA_SLAVE)
1032 ad_attach(scp, ATA_SLAVE);
1033 }
1034#endif
1035#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1036 /* then the atapi devices */
1037 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1038 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1039 continue;
1040 if (scp->devices & ATA_ATAPI_MASTER)
1041 atapi_attach(scp, ATA_MASTER);
1042 if (scp->devices & ATA_ATAPI_SLAVE)
1043 atapi_attach(scp, ATA_SLAVE);
1044 }
1045#endif
1046 if (ata_delayed_attach) {
1047 config_intrhook_disestablish(ata_delayed_attach);
1048 free(ata_delayed_attach, M_ATA);
1049 ata_delayed_attach = NULL;
1050 }
1051}
1052
1053static void
1054ata_intr(void *data)
1055{
1056 struct ata_softc *scp = (struct ata_softc *)data;
1057 u_int8_t dmastat;
1058
1059 /*
1060 * since we might share the IRQ with another device, and in some
1061 * cases with our twin channel, we only want to process interrupts
1062 * that we know this channel generated.
1063 */
1064 switch (scp->chiptype) {
1065#if NPCI > 0
1066 case 0x00041103: /* HighPoint HPT366 */
1067 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1068 return;
1069 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1070 break;
1071
1072 case 0x4d33105a: /* Promise 33's */
1073 case 0x4d38105a: /* Promise 66's */
1074 {
1075 struct ata_pci_softc *sc=device_get_softc(device_get_parent(scp->dev));
1076
1077 if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
1078 ((scp->unit) ? 0x00004000 : 0x00000400)))
1079 return;
1080 }
1081 /* FALLTHROUGH */
1082#endif
1083 default:
1084 if (scp->flags & ATA_DMA_ACTIVE) {
1085 if (!(dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT)
1086 return;
1087 else
1088 outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);

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

1207#endif
1208 scp->active = ATA_IDLE;
1209}
1210
1211void
1212ata_reset(struct ata_softc *scp, int32_t *mask)
1213{
1214 int32_t timeout;
1215 u_int8_t status0 = 0, status1 = 0;
1216
1217 /* reset channel */
1218 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1219 DELAY(1);
1220 inb(scp->ioaddr + ATA_STATUS);
1221 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1222 DELAY(10000);
1223 outb(scp->altioaddr, ATA_A_IDS);

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

1257
1258int32_t
1259ata_reinit(struct ata_softc *scp)
1260{
1261 int32_t mask = 0, omask;
1262
1263 scp->active = ATA_REINITING;
1264 scp->running = NULL;
1265 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
1266 mask |= 0x01;
1267 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
1268 mask |= 0x02;
1269 if (mask) {
1270 omask = mask;
1271 ata_printf(scp, -1, "resetting devices .. ");
1272 ata_reset(scp, &mask);
1273 if (omask != mask)
1274 printf(" device dissapeared! %d ", omask & ~mask);
1275
1276#if NATADISK > 0
1277 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1278 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
1279 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1280 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
1281#endif
1282#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1283 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1284 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
1285 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1286 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
1287#endif
1288 printf("done\n");
1289 }
1290 scp->active = ATA_IDLE;
1291 ata_start(scp);
1292 return 0;
1293}
1294
1295int32_t
1296ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
1297{

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

1408
1409 default:
1410 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1411 active2str(flags));
1412 }
1413 return 0;
1414}
1415
1416int
1417ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1418{
1419 va_list ap;
1420 int ret;
1421
1422 if (device == -1)
1423 ret = printf("ata%d: ", device_get_unit(scp->dev));
1424 else
1425 ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1426 (device == ATA_MASTER) ? "master" : "slave");
1427 va_start(ap, fmt);
1428 ret += vprintf(fmt, ap);
1429 va_end(ap);
1430 return ret;
1431}
1432
1433int
1434ata_get_lun(u_int32_t *map)
1435{
1436 int lun = ffs(~*map) - 1;
1437
1438 *map |= (1 << lun);
1439 return lun;
1440}
1441
1442void
1443ata_free_lun(u_int32_t *map, int lun)
1444{
1445 *map &= ~(1 << lun);
1446}
1447
1448int8_t *
1449ata_mode2str(int32_t mode)
1450{
1451 switch (mode) {
1452 case ATA_PIO: return "BIOSPIO";
1453 case ATA_PIO0: return "PIO0";
1454 case ATA_PIO1: return "PIO1";
1455 case ATA_PIO2: return "PIO2";

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

1471 case 0: return ATA_PIO0;
1472 case 1: return ATA_PIO1;
1473 case 2: return ATA_PIO2;
1474 case 3: return ATA_PIO3;
1475 case 4: return ATA_PIO4;
1476 }
1477}
1478
1479int
1480ata_pmode(struct ata_params *ap)
1481{
1482 if (ap->atavalid & ATA_FLAG_64_70) {
1483 if (ap->apiomodes & 2)
1484 return 4;
1485 if (ap->apiomodes & 1)
1486 return 3;
1487 }
1488 if (ap->opiomode == 2)
1489 return 2;
1490 if (ap->opiomode == 1)
1491 return 1;
1492 if (ap->opiomode == 0)
1493 return 0;
1494 return -1;
1495}
1496
1497int
1498ata_wmode(struct ata_params *ap)
1499{
1500 if (ap->wdmamodes & 4)
1501 return 2;
1502 if (ap->wdmamodes & 2)
1503 return 1;
1504 if (ap->wdmamodes & 1)
1505 return 0;
1506 return -1;
1507}
1508
1509int
1510ata_umode(struct ata_params *ap)
1511{
1512 if (ap->atavalid & ATA_FLAG_88) {
1513 if (ap->udmamodes & 0x10)
1514 return (ap->cblid ? 4 : 2);
1515 if (ap->udmamodes & 0x08)
1516 return (ap->cblid ? 3 : 2);
1517 if (ap->udmamodes & 0x04)
1518 return 2;
1519 if (ap->udmamodes & 0x02)
1520 return 1;
1521 if (ap->udmamodes & 0x01)
1522 return 0;
1523 }
1524 return -1;
1525}
1526
1527static int8_t *
1528active2str(int32_t active)
1529{
1530 static char buf[8];
1531
1532 switch (active) {
1533 case ATA_IDLE:
1534 return("ATA_IDLE");

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

1547 case ATA_REINITING:
1548 return("ATA_REINITING");
1549 default:
1550 sprintf(buf, "0x%02x", active);
1551 return buf;
1552 }
1553}
1554
1555static void
1556bswap(int8_t *buf, int32_t len)
1557{
1558 u_int16_t *ptr = (u_int16_t*)(buf + len);
1559
1560 while (--ptr >= (u_int16_t*)buf)
1561 *ptr = ntohs(*ptr);
1562}
1563
1564static void
1565btrim(int8_t *buf, int32_t len)
1566{
1567 int8_t *ptr;
1568
1569 for (ptr = buf; ptr < buf+len; ++ptr)
1570 if (!*ptr)
1571 *ptr = ' ';
1572 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1573 *ptr = 0;
1574}
1575
1576static void
1577bpack(int8_t *src, int8_t *dst, int32_t len)
1578{
1579 int32_t i, j, blank;
1580
1581 for (i = j = blank = 0 ; i < len; i++) {

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

1591 continue;
1592 }
1593 dst[j++] = src[i];
1594 }
1595 if (j < len)
1596 dst[j] = 0x00;
1597}
1598
1599static void
1600ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1601{
1602 int32_t s = splbio();
1603
1604 while (scp->active != ATA_IDLE)
1605 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1606 scp->active = ATA_REINITING;
1607 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1608 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1609 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1610 scp->active = ATA_IDLE;
1611 ata_start(scp);
1612 splx(s);
1613}
1614
1615static int
1616sysctl_hw_ata SYSCTL_HANDLER_ARGS
1617{
1618 struct ata_softc *scp;
1619 int ctlr, error, i;
1620
1621 /* readout internal state */
1622 bzero(ata_conf, sizeof(ata_conf));
1623 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1624 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1625 continue;
1626 for (i = 0; i < 2; i++) {
1627 if (!scp->dev_softc[i])
1628 strcat(ata_conf, "---,");
1629 else if (scp->mode[i] >= ATA_DMA)
1630 strcat(ata_conf, "dma,");
1631 else
1632 strcat(ata_conf, "pio,");
1633 }
1634 }
1635 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1636 if (error == 0 && req->newptr != NULL) {
1637 char *ptr = ata_conf;
1638
1639 /* update internal state */
1640 i = 0;
1641 while (*ptr) {
1642 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1643 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1644 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1645 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1646 }
1647 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1648 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1649 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1650 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1651 }
1652 else if (strncmp(ptr, "---", 3))
1653 break;
1654 ptr+=3;
1655 if (*ptr++ != ',' ||
1656 ++i > (devclass_get_maxunit(ata_devclass) << 1))
1657 break;
1658 }
1659 }
1660 return error;
1661}
1662SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1663 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1664
1665static void
1666ata_init(void)
1667{
1668 /* register boot attach to be run when interrupts are enabled */
1669 if (!(ata_delayed_attach = (struct intr_config_hook *)
1670 malloc(sizeof(struct intr_config_hook),
1671 M_TEMP, M_NOWAIT))) {
1672 printf("ata: malloc of delayed attach hook failed\n");
1673 return;
1674 }
1675 bzero(ata_delayed_attach, sizeof(struct intr_config_hook));
1676
1677 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1678 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1679 printf("ata: config_intrhook_establish failed\n");
1680 free(ata_delayed_attach, M_TEMP);
1681 }
1682}
1683SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)