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