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