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