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