Deleted Added
full compact
ata-all.c (64303) ata-all.c (64307)
1/*-
2 * Copyright (c) 1998,1999,2000 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
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,2000 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
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 64303 2000-08-06 19:10:05Z sos $
28 * $FreeBSD: head/sys/dev/ata/ata-all.c 64307 2000-08-06 19:51:58Z sos $
29 */
30
31#include "ata.h"
32#include "isa.h"
33#include "card.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include "opt_ata.h"
41#include <sys/param.h>
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/bio.h>
48#include <sys/malloc.h>
49#include <sys/devicestat.h>
50#include <sys/sysctl.h>
51#include <machine/stdarg.h>
52#include <vm/vm.h>
53#include <vm/pmap.h>
54#include <machine/resource.h>
55#include <machine/bus.h>
56#include <sys/rman.h>
57#if NPCI > 0
58#include <pci/pcivar.h>
59#include <pci/pcireg.h>
60#endif
61#include <isa/isavar.h>
62#include <isa/isareg.h>
63#include <machine/clock.h>
64#ifdef __i386__
65#include <machine/smp.h>
66#include <i386/isa/intr_machdep.h>
67#endif
68#ifdef __alpha__
69#include <machine/md_var.h>
70#endif
71#include <dev/ata/ata-all.h>
72#include <dev/ata/ata-disk.h>
73#include <dev/ata/atapi-all.h>
74
75/* misc defines */
76#define IOMASK 0xfffffffc
77#define ATA_IOADDR_RID 0
78#define ATA_ALTADDR_RID 1
79#define ATA_BMADDR_RID 2
80
81/* prototypes */
82static int ata_probe(device_t);
83static int ata_attach(device_t);
84static int ata_detach(device_t);
85static int ata_resume(device_t);
86static void ata_boot_attach(void);
87static void ata_intr(void *);
88static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
89static int8_t *active2str(int32_t);
90static void bswap(int8_t *, int32_t);
91static void btrim(int8_t *, int32_t);
92static void bpack(int8_t *, int8_t *, int32_t);
93
94/* local vars */
95static devclass_t ata_devclass;
96static devclass_t ata_pci_devclass;
97static struct intr_config_hook *ata_delayed_attach = NULL;
98static char ata_conf[256];
99MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
100
101#if NISA > 0
102static struct isa_pnp_id ata_ids[] = {
103 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
104 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
105 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
106 {0x0306d041, "Generic ATA"}, /* PNP0603 */
107 {0}
108};
109
110static int
111ata_isa_probe(device_t dev)
112{
113 struct ata_softc *scp = device_get_softc(dev);
114 struct resource *port;
115 int rid;
116 u_long tmp;
117
118 /* check isapnp ids */
119 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
120 return ENXIO;
121
122 /* allocate the port range */
123 rid = ATA_IOADDR_RID;
124 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
126 if (!port)
127 return ENOMEM;
128
129 /* alloctate the altport range */
130 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
131 bus_set_resource(dev, SYS_RES_IOPORT, 1,
132 rman_get_start(port) + ATA_ALTPORT,
133 ATA_ALTIOSIZE);
134 }
135 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
136 scp->unit = device_get_unit(dev);
137 scp->flags |= ATA_USE_16BIT;
138 return ata_probe(dev);
139}
140
141static device_method_t ata_isa_methods[] = {
142 /* device interface */
143 DEVMETHOD(device_probe, ata_isa_probe),
144 DEVMETHOD(device_attach, ata_attach),
145 DEVMETHOD(device_resume, ata_resume),
146 { 0, 0 }
147};
148
149static driver_t ata_isa_driver = {
150 "ata",
151 ata_isa_methods,
152 sizeof(struct ata_softc),
153};
154
155DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
156#endif
157
158#if NCARD > 0
159static int
160ata_pccard_probe(device_t dev)
161{
162 struct ata_softc *scp = device_get_softc(dev);
163 struct resource *port;
164 int rid, len;
165
166 /* allocate the port range */
167 rid = ATA_IOADDR_RID;
168 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
169 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE);
170 if (!port)
171 return ENOMEM;
172
173 /*
174 * if we got more than the default ATA_IOSIZE ports, this is likely
175 * a pccard system where the altio ports are located just after the
176 * normal io ports, so no need to allocate them.
177 */
178 if (len <= ATA_IOSIZE) {
179 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
180 rman_get_start(port) + ATA_ALTPORT, ATA_ALTIOSIZE);
181 }
182 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
183 scp->unit = device_get_unit(dev);
184 scp->flags |= ATA_USE_16BIT;
185 return ata_probe(dev);
186}
187
188static device_method_t ata_pccard_methods[] = {
189 /* device interface */
190 DEVMETHOD(device_probe, ata_pccard_probe),
191 DEVMETHOD(device_attach, ata_attach),
192 DEVMETHOD(device_detach, ata_detach),
193 { 0, 0 }
194};
195
196static driver_t ata_pccard_driver = {
197 "ata",
198 ata_pccard_methods,
199 sizeof(struct ata_softc),
200};
201
202DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
203#endif
204
205#if NPCI > 0
206struct ata_pci_softc {
207 struct resource *bmio;
208 struct resource bmio_1;
209 struct resource bmio_2;
210 struct resource *irq;
211 int32_t irqcnt;
212};
213
214int32_t
215ata_find_dev(device_t dev, int32_t type, int32_t revid)
216{
217 device_t *children, child;
218 int nchildren, i;
219
220 if (device_get_children(device_get_parent(dev), &children, &nchildren))
221 return 0;
222
223 for (i = 0; i < nchildren; i++) {
224 child = children[i];
225
226 /* check that it's on the same silicon and the device we want */
227 if (pci_get_slot(dev) == pci_get_slot(child) &&
228 pci_get_vendor(child) == (type & 0xffff) &&
229 pci_get_device(child) == ((type & 0xffff0000) >> 16) &&
230 pci_get_revid(child) >= revid) {
231 free(children, M_TEMP);
232 return 1;
233 }
234 }
235 free(children, M_TEMP);
236 return 0;
237}
238
239static const char *
240ata_pci_match(device_t dev)
241{
242 if (pci_get_class(dev) != PCIC_STORAGE)
243 return NULL;
244
245 switch (pci_get_devid(dev)) {
246 /* supported chipsets */
247 case 0x12308086:
248 return "Intel PIIX ATA controller";
249
250 case 0x70108086:
251 return "Intel PIIX3 ATA controller";
252
253 case 0x71118086:
254 case 0x71998086:
255 return "Intel PIIX4 ATA33 controller";
256
29 */
30
31#include "ata.h"
32#include "isa.h"
33#include "card.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include "opt_ata.h"
41#include <sys/param.h>
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/bio.h>
48#include <sys/malloc.h>
49#include <sys/devicestat.h>
50#include <sys/sysctl.h>
51#include <machine/stdarg.h>
52#include <vm/vm.h>
53#include <vm/pmap.h>
54#include <machine/resource.h>
55#include <machine/bus.h>
56#include <sys/rman.h>
57#if NPCI > 0
58#include <pci/pcivar.h>
59#include <pci/pcireg.h>
60#endif
61#include <isa/isavar.h>
62#include <isa/isareg.h>
63#include <machine/clock.h>
64#ifdef __i386__
65#include <machine/smp.h>
66#include <i386/isa/intr_machdep.h>
67#endif
68#ifdef __alpha__
69#include <machine/md_var.h>
70#endif
71#include <dev/ata/ata-all.h>
72#include <dev/ata/ata-disk.h>
73#include <dev/ata/atapi-all.h>
74
75/* misc defines */
76#define IOMASK 0xfffffffc
77#define ATA_IOADDR_RID 0
78#define ATA_ALTADDR_RID 1
79#define ATA_BMADDR_RID 2
80
81/* prototypes */
82static int ata_probe(device_t);
83static int ata_attach(device_t);
84static int ata_detach(device_t);
85static int ata_resume(device_t);
86static void ata_boot_attach(void);
87static void ata_intr(void *);
88static int32_t ata_getparam(struct ata_softc *, int32_t, u_int8_t);
89static int8_t *active2str(int32_t);
90static void bswap(int8_t *, int32_t);
91static void btrim(int8_t *, int32_t);
92static void bpack(int8_t *, int8_t *, int32_t);
93
94/* local vars */
95static devclass_t ata_devclass;
96static devclass_t ata_pci_devclass;
97static struct intr_config_hook *ata_delayed_attach = NULL;
98static char ata_conf[256];
99MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
100
101#if NISA > 0
102static struct isa_pnp_id ata_ids[] = {
103 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
104 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
105 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
106 {0x0306d041, "Generic ATA"}, /* PNP0603 */
107 {0}
108};
109
110static int
111ata_isa_probe(device_t dev)
112{
113 struct ata_softc *scp = device_get_softc(dev);
114 struct resource *port;
115 int rid;
116 u_long tmp;
117
118 /* check isapnp ids */
119 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
120 return ENXIO;
121
122 /* allocate the port range */
123 rid = ATA_IOADDR_RID;
124 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
125 ATA_IOSIZE, RF_ACTIVE);
126 if (!port)
127 return ENOMEM;
128
129 /* alloctate the altport range */
130 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
131 bus_set_resource(dev, SYS_RES_IOPORT, 1,
132 rman_get_start(port) + ATA_ALTPORT,
133 ATA_ALTIOSIZE);
134 }
135 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
136 scp->unit = device_get_unit(dev);
137 scp->flags |= ATA_USE_16BIT;
138 return ata_probe(dev);
139}
140
141static device_method_t ata_isa_methods[] = {
142 /* device interface */
143 DEVMETHOD(device_probe, ata_isa_probe),
144 DEVMETHOD(device_attach, ata_attach),
145 DEVMETHOD(device_resume, ata_resume),
146 { 0, 0 }
147};
148
149static driver_t ata_isa_driver = {
150 "ata",
151 ata_isa_methods,
152 sizeof(struct ata_softc),
153};
154
155DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
156#endif
157
158#if NCARD > 0
159static int
160ata_pccard_probe(device_t dev)
161{
162 struct ata_softc *scp = device_get_softc(dev);
163 struct resource *port;
164 int rid, len;
165
166 /* allocate the port range */
167 rid = ATA_IOADDR_RID;
168 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
169 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE);
170 if (!port)
171 return ENOMEM;
172
173 /*
174 * if we got more than the default ATA_IOSIZE ports, this is likely
175 * a pccard system where the altio ports are located just after the
176 * normal io ports, so no need to allocate them.
177 */
178 if (len <= ATA_IOSIZE) {
179 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
180 rman_get_start(port) + ATA_ALTPORT, ATA_ALTIOSIZE);
181 }
182 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
183 scp->unit = device_get_unit(dev);
184 scp->flags |= ATA_USE_16BIT;
185 return ata_probe(dev);
186}
187
188static device_method_t ata_pccard_methods[] = {
189 /* device interface */
190 DEVMETHOD(device_probe, ata_pccard_probe),
191 DEVMETHOD(device_attach, ata_attach),
192 DEVMETHOD(device_detach, ata_detach),
193 { 0, 0 }
194};
195
196static driver_t ata_pccard_driver = {
197 "ata",
198 ata_pccard_methods,
199 sizeof(struct ata_softc),
200};
201
202DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
203#endif
204
205#if NPCI > 0
206struct ata_pci_softc {
207 struct resource *bmio;
208 struct resource bmio_1;
209 struct resource bmio_2;
210 struct resource *irq;
211 int32_t irqcnt;
212};
213
214int32_t
215ata_find_dev(device_t dev, int32_t type, int32_t revid)
216{
217 device_t *children, child;
218 int nchildren, i;
219
220 if (device_get_children(device_get_parent(dev), &children, &nchildren))
221 return 0;
222
223 for (i = 0; i < nchildren; i++) {
224 child = children[i];
225
226 /* check that it's on the same silicon and the device we want */
227 if (pci_get_slot(dev) == pci_get_slot(child) &&
228 pci_get_vendor(child) == (type & 0xffff) &&
229 pci_get_device(child) == ((type & 0xffff0000) >> 16) &&
230 pci_get_revid(child) >= revid) {
231 free(children, M_TEMP);
232 return 1;
233 }
234 }
235 free(children, M_TEMP);
236 return 0;
237}
238
239static const char *
240ata_pci_match(device_t dev)
241{
242 if (pci_get_class(dev) != PCIC_STORAGE)
243 return NULL;
244
245 switch (pci_get_devid(dev)) {
246 /* supported chipsets */
247 case 0x12308086:
248 return "Intel PIIX ATA controller";
249
250 case 0x70108086:
251 return "Intel PIIX3 ATA controller";
252
253 case 0x71118086:
254 case 0x71998086:
255 return "Intel PIIX4 ATA33 controller";
256
257 case 0x24218086:
258 return "Intel ICH0 ATA33 controller";
259
257 case 0x24118086:
258 return "Intel ICH ATA66 controller";
259
260 case 0x24118086:
261 return "Intel ICH ATA66 controller";
262
260 case 0x24218086:
261 return "Intel ICH0 ATA33 controller";
263 case 0x244b8086:
264 return "Intel ICH2 ATA100 controller";
262
263 case 0x522910b9:
264 return "AcerLabs Aladdin ATA33 controller";
265
266 case 0x05711106:
267 if (ata_find_dev(dev, 0x05861106, 0))
268 return "VIA 82C586 ATA33 controller";
269 if (ata_find_dev(dev, 0x05961106, 0x12))
270 return "VIA 82C596B ATA66 controller";
271 if (ata_find_dev(dev, 0x05961106, 0))
272 return "VIA 82C596 ATA33 controller";
273 if (ata_find_dev(dev, 0x06861106, 0))
274 return "VIA 82C686 ATA66 controller";
275 return "VIA Apollo ATA controller";
276
277 case 0x55131039:
278 return "SiS 5591 ATA33 controller";
279
280 case 0x06461095:
281 return "CMD 646 ATA controller";
282
283 case 0xc6931080:
284 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
285 return "Cypress 82C693 ATA controller";
286 break;
287
288 case 0x74091022:
289 return "AMD 756 ATA66 controller";
290
291 case 0x4d33105a:
292 return "Promise ATA33 controller";
293
294 case 0x4d38105a:
295 return "Promise ATA66 controller";
296
265
266 case 0x522910b9:
267 return "AcerLabs Aladdin ATA33 controller";
268
269 case 0x05711106:
270 if (ata_find_dev(dev, 0x05861106, 0))
271 return "VIA 82C586 ATA33 controller";
272 if (ata_find_dev(dev, 0x05961106, 0x12))
273 return "VIA 82C596B ATA66 controller";
274 if (ata_find_dev(dev, 0x05961106, 0))
275 return "VIA 82C596 ATA33 controller";
276 if (ata_find_dev(dev, 0x06861106, 0))
277 return "VIA 82C686 ATA66 controller";
278 return "VIA Apollo ATA controller";
279
280 case 0x55131039:
281 return "SiS 5591 ATA33 controller";
282
283 case 0x06461095:
284 return "CMD 646 ATA controller";
285
286 case 0xc6931080:
287 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
288 return "Cypress 82C693 ATA controller";
289 break;
290
291 case 0x74091022:
292 return "AMD 756 ATA66 controller";
293
294 case 0x4d33105a:
295 return "Promise ATA33 controller";
296
297 case 0x4d38105a:
298 return "Promise ATA66 controller";
299
300 case 0x4d30105a:
301 return "Promise ATA100 controller";
302
297 case 0x00041103:
303 case 0x00041103:
298 return "HighPoint HPT366 ATA66 controller";
304 switch (pci_get_revid(dev)) {
305 case 0x00:
306 case 0x01:
307 return "HighPoint HPT366 ATA66 controller";
308 case 0x02:
309 return "HighPoint HPT368 ATA66 controller";
310 case 0x03:
311 case 0x04:
312 return "HighPoint HPT370 ATA100 controller";
313 default:
314 return "Unknown revision HighPoint ATA controller";
315 }
299
300 /* unsupported but known chipsets, generic DMA only */
301 case 0x10001042:
302 case 0x10011042:
303 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
304
305 case 0x06401095:
306 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
307
308 case 0x01021078:
309 return "Cyrix 5530 ATA controller (generic mode)";
310
311 /* unknown chipsets, try generic DMA if it seems possible */
312 default:
313 if (pci_get_class(dev) == PCIC_STORAGE &&
314 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
315 return "Generic PCI ATA controller";
316 }
317 return NULL;
318}
319
320static int
321ata_pci_probe(device_t dev)
322{
323 const char *desc = ata_pci_match(dev);
324
325 if (desc) {
326 device_set_desc(dev, desc);
327 return 0;
328 }
329 else
330 return ENXIO;
331}
332
333static int
334ata_pci_add_child(device_t dev, int unit)
335{
336 device_t child;
337
338 /* check if this is located at one of the std addresses */
339 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
340 if (!(child = device_add_child(dev, "ata", unit)))
341 return ENOMEM;
342 }
343 else {
344 if (!(child = device_add_child(dev, "ata", 2)))
345 return ENOMEM;
346 }
347 device_set_ivars(child, (void *)(uintptr_t) unit);
348 return 0;
349}
350
351static int
352ata_pci_attach(device_t dev)
353{
354 struct ata_pci_softc *sc = device_get_softc(dev);
355 u_int8_t class, subclass;
356 u_int32_t type, cmd;
357 int rid;
358
359 /* set up vendor-specific stuff */
360 type = pci_get_devid(dev);
361 class = pci_get_class(dev);
362 subclass = pci_get_subclass(dev);
363 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
364
365 /* is this controller busmaster DMA capable ? */
366 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
367 /* is busmastering support turned on ? */
368 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
369 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
370
371 /* is there a valid port range to connect to ? */
372 rid = 0x20;
373 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
374 0, ~0, 1, RF_ACTIVE);
375 if (!sc->bmio)
376 device_printf(dev, "Busmastering DMA not configured\n");
377 }
378 else
379 device_printf(dev, "Busmastering DMA not enabled\n");
380 }
381 else {
316
317 /* unsupported but known chipsets, generic DMA only */
318 case 0x10001042:
319 case 0x10011042:
320 return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
321
322 case 0x06401095:
323 return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
324
325 case 0x01021078:
326 return "Cyrix 5530 ATA controller (generic mode)";
327
328 /* unknown chipsets, try generic DMA if it seems possible */
329 default:
330 if (pci_get_class(dev) == PCIC_STORAGE &&
331 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
332 return "Generic PCI ATA controller";
333 }
334 return NULL;
335}
336
337static int
338ata_pci_probe(device_t dev)
339{
340 const char *desc = ata_pci_match(dev);
341
342 if (desc) {
343 device_set_desc(dev, desc);
344 return 0;
345 }
346 else
347 return ENXIO;
348}
349
350static int
351ata_pci_add_child(device_t dev, int unit)
352{
353 device_t child;
354
355 /* check if this is located at one of the std addresses */
356 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
357 if (!(child = device_add_child(dev, "ata", unit)))
358 return ENOMEM;
359 }
360 else {
361 if (!(child = device_add_child(dev, "ata", 2)))
362 return ENOMEM;
363 }
364 device_set_ivars(child, (void *)(uintptr_t) unit);
365 return 0;
366}
367
368static int
369ata_pci_attach(device_t dev)
370{
371 struct ata_pci_softc *sc = device_get_softc(dev);
372 u_int8_t class, subclass;
373 u_int32_t type, cmd;
374 int rid;
375
376 /* set up vendor-specific stuff */
377 type = pci_get_devid(dev);
378 class = pci_get_class(dev);
379 subclass = pci_get_subclass(dev);
380 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
381
382 /* is this controller busmaster DMA capable ? */
383 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
384 /* is busmastering support turned on ? */
385 if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
386 (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
387
388 /* is there a valid port range to connect to ? */
389 rid = 0x20;
390 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
391 0, ~0, 1, RF_ACTIVE);
392 if (!sc->bmio)
393 device_printf(dev, "Busmastering DMA not configured\n");
394 }
395 else
396 device_printf(dev, "Busmastering DMA not enabled\n");
397 }
398 else {
382 if (type == 0x4d33105a || type == 0x4d38105a || type == 0x00041103) {
383 /* Promise and HPT366 controllers support busmastering DMA */
399 if (type == 0x4d33105a || type == 0x4d38105a ||
400 type == 0x4d30105a || type == 0x00041103) {
401 /* Promise and HighPoint controllers support busmastering DMA */
384 rid = 0x20;
385 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
386 0, ~0, 1, RF_ACTIVE);
387 }
388 else
389 /* we dont know this controller, no busmastering DMA */
390 device_printf(dev, "Busmastering DMA not supported\n");
391 }
392
393 /* do extra chipset specific setups */
394 switch (type) {
395 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
396 pci_write_config(dev, 0x53,
397 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
398 break;
399
400 case 0x4d38105a: /* Promise 66's need their clock changed */
402 rid = 0x20;
403 sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
404 0, ~0, 1, RF_ACTIVE);
405 }
406 else
407 /* we dont know this controller, no busmastering DMA */
408 device_printf(dev, "Busmastering DMA not supported\n");
409 }
410
411 /* do extra chipset specific setups */
412 switch (type) {
413 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
414 pci_write_config(dev, 0x53,
415 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
416 break;
417
418 case 0x4d38105a: /* Promise 66's need their clock changed */
419 case 0x4d30105a: /* Promise 100 too */
401 outb(rman_get_start(sc->bmio) + 0x11,
402 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
403 /* FALLTHROUGH */
404
405 case 0x4d33105a: /* Promise's need burst mode to be turned on */
406 outb(rman_get_start(sc->bmio) + 0x1f,
407 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
408 break;
409
420 outb(rman_get_start(sc->bmio) + 0x11,
421 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
422 /* FALLTHROUGH */
423
424 case 0x4d33105a: /* Promise's need burst mode to be turned on */
425 outb(rman_get_start(sc->bmio) + 0x1f,
426 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
427 break;
428
410 case 0x00041103: /* HPT366 turn of fast interrupt prediction */
411 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
429 case 0x00041103: /* HighPoint's need to turn off interrupt prediction */
430 switch (pci_get_revid(dev)) {
431 case 0x00:
432 case 0x01:
433 pci_write_config(dev, 0x51,
434 (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
435 break;
436
437 case 0x02:
438 case 0x03:
439 case 0x04:
440 pci_write_config(dev, 0x51,
441 (pci_read_config(dev, 0x51, 1) & ~0x02), 1);
442 pci_write_config(dev, 0x55,
443 (pci_read_config(dev, 0x55, 1) & ~0x02), 1);
444 pci_write_config(dev, 0x5a,
445 (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
446
447 }
412 break;
413
414 case 0x05711106:
415 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */
416 /* set prefetch, postwrite */
417 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
418
419 /* set fifo configuration half'n'half */
420 pci_write_config(dev, 0x43,
421 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
422
423 /* set status register read retry */
424 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
425
426 /* set DMA read & end-of-sector fifo flush */
427 pci_write_config(dev, 0x46,
428 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
429
430 /* set sector size */
431 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
432 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
433
434 /* prepare for ATA-66 on the 82C686 and rev 0x12 and newer 82C596's */
435 if (ata_find_dev(dev, 0x06861106, 0) ||
436 ata_find_dev(dev, 0x05961106, 0x12)) {
437 pci_write_config(dev, 0x50,
438 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
439 }
440 break;
441 }
442
443 /*
444 * the Cypress chip is a mess, it contains two ATA functions, but
445 * both channels are visible on the first one.
446 * simply ignore the second function for now, as the right
447 * solution (ignoring the second channel on the first function)
448 * doesn't work with the crappy ATA interrupt setup on the alpha.
449 */
450 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
451 return 0;
452
453 ata_pci_add_child(dev, 0);
454
455 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV ||
456 pci_read_config(dev, 0x18, 4) & IOMASK)
457 ata_pci_add_child(dev, 1);
458
459 return bus_generic_attach(dev);
460}
461
462static int
463ata_pci_print_child(device_t dev, device_t child)
464{
465 struct ata_softc *scp = device_get_softc(child);
466 int unit = (uintptr_t) device_get_ivars(child);
467 int retval = 0;
468
469 retval += bus_print_child_header(dev, child);
470 retval += printf(": at 0x%x", scp->ioaddr);
471
472 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
473 retval += printf(" irq %d", 14 + unit);
474
475 retval += bus_print_child_footer(dev, child);
476
477 return retval;
478}
479
480static struct resource *
481ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
482 u_long start, u_long end, u_long count, u_int flags)
483{
484 struct ata_pci_softc *sc = device_get_softc(dev);
485 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
486 int unit = (int)device_get_ivars(child);
487 int myrid;
488
489 if (type == SYS_RES_IOPORT) {
490 switch (*rid) {
491 case ATA_IOADDR_RID:
492 if (masterdev) {
493 myrid = 0;
494 start = (unit == 0 ? IO_WD1 : IO_WD2);
495 end = start + ATA_IOSIZE - 1;
496 count = ATA_IOSIZE;
497 }
498 else
499 myrid = 0x10 + 8 * unit;
500 break;
501
502 case ATA_ALTADDR_RID:
503 if (masterdev) {
504 myrid = 0;
505 start = (unit == 0 ? IO_WD1 : IO_WD2) + ATA_ALTPORT;
506 end = start + ATA_ALTIOSIZE - 1;
507 count = ATA_ALTIOSIZE;
508 }
509 else
510 myrid = 0x14 + 8 * unit;
511 break;
512
513 case ATA_BMADDR_RID:
514 /* the busmaster resource is shared between the two channels */
515 if (sc->bmio) {
516 if (unit == 0) {
517 sc->bmio_1 = *sc->bmio;
518 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
519 return &sc->bmio_1;
520 } else {
521 sc->bmio_2 = *sc->bmio;
522 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
523 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
524 return &sc->bmio_2;
525 }
526 }
527 break;
528
529 default:
530 return 0;
531 }
532
533 if (masterdev)
534 /* make the parent just pass through the allocation. */
535 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
536 SYS_RES_IOPORT, &myrid,
537 start, end, count, flags);
538 else
539 /* we are using the parent resource directly. */
540 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
541 SYS_RES_IOPORT, &myrid,
542 start, end, count, flags);
543 }
544
545 if (type == SYS_RES_IRQ) {
546 if (*rid != 0)
547 return 0;
548
549 if (masterdev) {
550#ifdef __i386__
551 int irq = (unit == 0 ? 14 : 15);
552
553 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
554 SYS_RES_IRQ, rid,
555 irq, irq, 1, flags & ~RF_SHAREABLE);
556#else
557 return alpha_platform_alloc_ide_intr(unit);
558#endif
559 } else {
560 /* primary and secondary channels share the same interrupt */
561 sc->irqcnt++;
562 if (!sc->irq)
563 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
564 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
565 return sc->irq;
566 }
567 }
568 return 0;
569}
570
571static int
572ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
573 struct resource *r)
574{
575 struct ata_pci_softc *sc = device_get_softc(dev);
576 int unit = (uintptr_t) device_get_ivars(child);
577 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
578 int myrid = 0;
579
580 if (type == SYS_RES_IOPORT) {
581 switch (rid) {
582 case ATA_IOADDR_RID:
583 if (masterdev)
584 myrid = 0;
585 else
586 myrid = 0x10 + 8 * unit;
587 break;
588
589 case ATA_ALTADDR_RID:
590 if (masterdev)
591 myrid = 0;
592 else
593 myrid = 0x14 + 8 * unit;
594 break;
595
596 case ATA_BMADDR_RID:
597 return 0;
598
599 default:
600 return ENOENT;
601 }
602
603 if (masterdev)
604 /* make the parent just pass through the allocation. */
605 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
606 SYS_RES_IOPORT, myrid, r);
607 else
608 /* we are using the parent resource directly. */
609 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
610 SYS_RES_IOPORT, myrid, r);
611 }
612 if (type == SYS_RES_IRQ) {
613 if (rid != 0)
614 return ENOENT;
615
616 if (masterdev) {
617#ifdef __i386__
618 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
619 child, SYS_RES_IRQ, rid, r);
620#else
621 return alpha_platform_release_ide_intr(unit, r);
622#endif
623 }
624 else {
625 if (--sc->irqcnt)
626 return 0;
627
628 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
629 dev, SYS_RES_IRQ, rid, r);
630 }
631 }
632 return EINVAL;
633}
634
635static int
636ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
637 int flags, driver_intr_t *intr, void *arg,
638 void **cookiep)
639{
640 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
641#ifdef __i386__
642 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
643 flags, intr, arg, cookiep);
644#else
645 return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
646#endif
647 }
648 else
649 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
650 flags, intr, arg, cookiep);
651}
652
653static int
654ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
655 void *cookie)
656{
657 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
658#ifdef __i386__
659 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
660#else
661 return alpha_platform_teardown_ide_intr(irq, cookie);
662#endif
663 }
664 else
665 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
666}
667
668static device_method_t ata_pci_methods[] = {
669 /* device interface */
670 DEVMETHOD(device_probe, ata_pci_probe),
671 DEVMETHOD(device_attach, ata_pci_attach),
672 DEVMETHOD(device_shutdown, bus_generic_shutdown),
673 DEVMETHOD(device_suspend, bus_generic_suspend),
674 DEVMETHOD(device_resume, bus_generic_resume),
675
676 /* bus methods */
677 DEVMETHOD(bus_print_child, ata_pci_print_child),
678 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
679 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
680 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
681 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
682 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
683 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
684 { 0, 0 }
685};
686
687static driver_t ata_pci_driver = {
688 "atapci",
689 ata_pci_methods,
690 sizeof(struct ata_pci_softc),
691};
692
693DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
694
695static int
696ata_pcisub_probe(device_t dev)
697{
698 struct ata_softc *scp = device_get_softc(dev);
699
700 /* kids of pci ata chipsets has their physical unit number in ivars */
701 scp->unit = (uintptr_t) device_get_ivars(dev);
702
703 /* set the chiptype to the hostchip ID, makes life easier */
704 if (ata_find_dev(device_get_parent(dev), 0x05861106, 0))
705 scp->chiptype = 0x05861106;
706 else if (ata_find_dev(device_get_parent(dev), 0x05961106, 0))
707 scp->chiptype = 0x05961106;
708 else if (ata_find_dev(device_get_parent(dev), 0x06861106, 0))
709 scp->chiptype = 0x06861106;
710 else
711 scp->chiptype = pci_get_devid(device_get_parent(dev));
712 return ata_probe(dev);
713}
714
715static device_method_t ata_pcisub_methods[] = {
716 /* device interface */
717 DEVMETHOD(device_probe, ata_pcisub_probe),
718 DEVMETHOD(device_attach, ata_attach),
719 DEVMETHOD(device_detach, ata_detach),
720 DEVMETHOD(device_resume, ata_resume),
721 { 0, 0 }
722};
723
724static driver_t ata_pcisub_driver = {
725 "ata",
726 ata_pcisub_methods,
727 sizeof(struct ata_softc),
728};
729
730DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
731#endif
732
733static int
734ata_probe(device_t dev)
735{
736 struct ata_softc *scp = device_get_softc(dev);
737 struct resource *io = 0;
738 struct resource *altio = 0;
739 struct resource *bmio = 0;
740 int rid;
741 int32_t ioaddr, altioaddr, bmaddr;
742 int32_t mask = 0;
743 u_int8_t status0, status1;
744
745 if (!scp || scp->flags & ATA_ATTACHED)
746 return ENXIO;
747
748 /* initialize the softc basics */
749 scp->active = ATA_IDLE;
750 scp->dev = dev;
751 scp->devices = 0;
752
753 rid = ATA_IOADDR_RID;
754 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
755 ATA_IOSIZE, RF_ACTIVE);
756 if (!io)
757 goto failure;
758 ioaddr = rman_get_start(io);
759
760 rid = ATA_ALTADDR_RID;
761 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
762 ATA_ALTIOSIZE, RF_ACTIVE);
763 if (altio)
764 altioaddr = rman_get_start(altio);
765 else
766 altioaddr = ioaddr + ATA_IOSIZE;
767
768 rid = ATA_BMADDR_RID;
769 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
770 bmaddr = bmio ? rman_get_start(bmio) : 0;
771
772 /* store the IO resources for eventual later release */
773 scp->r_io = io;
774 scp->r_altio = altio;
775 scp->r_bmio = bmio;
776
777 /* store the physical IO addresse for easy access */
778 scp->ioaddr = ioaddr;
779 scp->altioaddr = altioaddr;
780 scp->bmaddr = bmaddr;
781
782 if (bootverbose)
783 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
784 scp->ioaddr, scp->altioaddr, scp->bmaddr);
785
786 /* do we have any signs of ATA/ATAPI HW being present ? */
787 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
788 DELAY(1);
789 status0 = inb(scp->ioaddr + ATA_STATUS);
790 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
791 DELAY(1);
792 status1 = inb(scp->ioaddr + ATA_STATUS);
793 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
794 mask |= 0x01;
795 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
796 mask |= 0x02;
797 if (bootverbose)
798 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
799 mask, status0, status1);
800 if (!mask)
801 goto failure;
802
803 ata_reset(scp, &mask);
804 if (!mask)
805 goto failure;
806
807 /*
808 * OK, we have at least one device on the chain, check for ATAPI
809 * signatures, if none check if its a good old ATA device.
810 */
811 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
812 DELAY(1);
813 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
814 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
815 scp->devices |= ATA_ATAPI_MASTER;
816 }
817 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
818 DELAY(1);
819 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
820 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
821 scp->devices |= ATA_ATAPI_SLAVE;
822 }
823 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
824 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
825 DELAY(1);
826 outb(scp->ioaddr + ATA_ERROR, 0x58);
827 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
828 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
829 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
830 scp->devices |= ATA_ATA_MASTER;
831 }
832 }
833 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
834 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
835 DELAY(1);
836 outb(scp->ioaddr + ATA_ERROR, 0x58);
837 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
838 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
839 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
840 scp->devices |= ATA_ATA_SLAVE;
841 }
842 }
843 if (bootverbose)
844 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
845 if (!scp->devices) {
846 goto failure;
847 }
848 TAILQ_INIT(&scp->ata_queue);
849 TAILQ_INIT(&scp->atapi_queue);
850 return 0;
851
852failure:
853 if (io)
854 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
855 if (altio)
856 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio);
857 if (bmio)
858 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
859 if (bootverbose)
860 ata_printf(scp, -1, "probe allocation failed\n");
861 return ENXIO;
862}
863
864static int
865ata_attach(device_t dev)
866{
867 struct ata_softc *scp = device_get_softc(dev);
868 int error, rid = 0;
869
870 if (!scp || scp->flags & ATA_ATTACHED)
871 return ENXIO;
872
873 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
874 RF_SHAREABLE | RF_ACTIVE);
875 if (!scp->r_irq) {
876 ata_printf(scp, -1, "unable to allocate interrupt\n");
877 return ENXIO;
878 }
879 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
880 scp, &scp->ih)))
881 return error;
882
883 /*
884 * do not attach devices if we are in early boot, this is done later
885 * when interrupts are enabled by a hook into the boot process.
886 * otherwise attach what the probe has found in scp->devices.
887 */
888 if (!ata_delayed_attach) {
889 if (scp->devices & ATA_ATA_SLAVE)
890 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
891 scp->devices &= ~ATA_ATA_SLAVE;
892 if (scp->devices & ATA_ATAPI_SLAVE)
893 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
894 scp->devices &= ~ATA_ATAPI_SLAVE;
895 if (scp->devices & ATA_ATA_MASTER)
896 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
897 scp->devices &= ~ATA_ATA_MASTER;
898 if (scp->devices & ATA_ATAPI_MASTER)
899 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
900 scp->devices &= ~ATA_ATAPI_MASTER;
901#if NATADISK > 0
902 if (scp->devices & ATA_ATA_MASTER)
903 ad_attach(scp, ATA_MASTER);
904 if (scp->devices & ATA_ATA_SLAVE)
905 ad_attach(scp, ATA_SLAVE);
906#endif
907#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
908 if (scp->devices & ATA_ATAPI_MASTER)
909 atapi_attach(scp, ATA_MASTER);
910 if (scp->devices & ATA_ATAPI_SLAVE)
911 atapi_attach(scp, ATA_SLAVE);
912#endif
913 }
914 scp->flags |= ATA_ATTACHED;
915 return 0;
916}
917
918static int
919ata_detach(device_t dev)
920{
921 struct ata_softc *scp = device_get_softc(dev);
922
923 if (!scp || !(scp->flags & ATA_ATTACHED))
924 return ENXIO;
925
926#if NATADISK > 0
927 if (scp->devices & ATA_ATA_MASTER)
928 ad_detach(scp->dev_softc[0]);
929 if (scp->devices & ATA_ATA_SLAVE)
930 ad_detach(scp->dev_softc[1]);
931#endif
932#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
933 if (scp->devices & ATA_ATAPI_MASTER)
934 atapi_detach(scp->dev_softc[0]);
935 if (scp->devices & ATA_ATAPI_SLAVE)
936 atapi_detach(scp->dev_softc[1]);
937#endif
938 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
939 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
940 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
941 }
942 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
943 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
944 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
945 }
946 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL;
947 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL;
948 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
949 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
950 bus_teardown_intr(dev, scp->r_irq, scp->ih);
951 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
952 if (scp->r_bmio)
953 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
954 if (scp->r_altio)
955 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
956 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
957 scp->flags &= ~ATA_ATTACHED;
958 return 0;
959}
960
961static int
962ata_resume(device_t dev)
963{
964 struct ata_softc *scp = device_get_softc(dev);
965
966 ata_reinit(scp);
967 return 0;
968}
969
970static int32_t
971ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
972{
973 struct ata_params *ata_parm;
974 int8_t buffer[DEV_BSIZE];
975 int retry = 0;
976
977 /* select drive */
978 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
979 DELAY(1);
980
981 /* enable interrupt */
982 outb(scp->altioaddr, ATA_A_4BIT);
983 DELAY(1);
984
985 /* apparently some devices needs this repeated */
986 do {
987 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
988 ata_printf(scp, device, "identify failed\n");
989 return -1;
990 }
991 if (retry++ > 4) {
992 ata_printf(scp, device, "identify retries exceeded\n");
993 return -1;
994 }
995 } while (ata_wait(scp, device,
996 ((command == ATA_C_ATAPI_IDENTIFY) ?
997 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
998
999 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
1000 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
1001 if (!ata_parm) {
1002 ata_printf(scp, device, "malloc for identify data failed\n");
1003 return -1;
1004 }
1005 bcopy(buffer, ata_parm, sizeof(struct ata_params));
1006 if (command == ATA_C_ATA_IDENTIFY ||
1007 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
1008 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
1009 bswap(ata_parm->model, sizeof(ata_parm->model));
1010 btrim(ata_parm->model, sizeof(ata_parm->model));
1011 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
1012 bswap(ata_parm->revision, sizeof(ata_parm->revision));
1013 btrim(ata_parm->revision, sizeof(ata_parm->revision));
1014 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
1015 scp->dev_param[ATA_DEV(device)] = ata_parm;
1016 return 0;
1017}
1018
1019static void
1020ata_boot_attach(void)
1021{
1022 struct ata_softc *scp;
1023 int32_t ctlr;
1024
1025 /*
1026 * run through all ata devices and look for real ATA & ATAPI devices
1027 * using the hints we found in the early probe, this avoids some of
1028 * the delays probing of non-exsistent devices can cause.
1029 */
1030 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1031 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1032 continue;
1033 if (scp->devices & ATA_ATA_SLAVE)
1034 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
1035 scp->devices &= ~ATA_ATA_SLAVE;
1036 if (scp->devices & ATA_ATAPI_SLAVE)
1037 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
1038 scp->devices &= ~ATA_ATAPI_SLAVE;
1039 if (scp->devices & ATA_ATA_MASTER)
1040 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
1041 scp->devices &= ~ATA_ATA_MASTER;
1042 if (scp->devices & ATA_ATAPI_MASTER)
1043 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
1044 scp->devices &= ~ATA_ATAPI_MASTER;
1045 }
1046
1047#if NATADISK > 0
1048 /* now we know whats there, do the real attach, first the ATA disks */
1049 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1050 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1051 continue;
1052 if (scp->devices & ATA_ATA_MASTER)
1053 ad_attach(scp, ATA_MASTER);
1054 if (scp->devices & ATA_ATA_SLAVE)
1055 ad_attach(scp, ATA_SLAVE);
1056 }
1057#endif
1058#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1059 /* then the atapi devices */
1060 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1061 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1062 continue;
1063 if (scp->devices & ATA_ATAPI_MASTER)
1064 atapi_attach(scp, ATA_MASTER);
1065 if (scp->devices & ATA_ATAPI_SLAVE)
1066 atapi_attach(scp, ATA_SLAVE);
1067 }
1068#endif
1069 if (ata_delayed_attach) {
1070 config_intrhook_disestablish(ata_delayed_attach);
1071 free(ata_delayed_attach, M_ATA);
1072 ata_delayed_attach = NULL;
1073 }
1074}
1075
1076static void
1077ata_intr(void *data)
1078{
1079 struct ata_softc *scp = (struct ata_softc *)data;
1080 u_int8_t dmastat;
1081
1082 /*
1083 * since we might share the IRQ with another device, and in some
1084 * cases with our twin channel, we only want to process interrupts
1085 * that we know this channel generated.
1086 */
1087 switch (scp->chiptype) {
1088#if NPCI > 0
448 break;
449
450 case 0x05711106:
451 case 0x74091022: /* VIA 82C586, 82C596, 82C686 & AMD 756 default setup */
452 /* set prefetch, postwrite */
453 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
454
455 /* set fifo configuration half'n'half */
456 pci_write_config(dev, 0x43,
457 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
458
459 /* set status register read retry */
460 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
461
462 /* set DMA read & end-of-sector fifo flush */
463 pci_write_config(dev, 0x46,
464 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
465
466 /* set sector size */
467 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
468 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
469
470 /* prepare for ATA-66 on the 82C686 and rev 0x12 and newer 82C596's */
471 if (ata_find_dev(dev, 0x06861106, 0) ||
472 ata_find_dev(dev, 0x05961106, 0x12)) {
473 pci_write_config(dev, 0x50,
474 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
475 }
476 break;
477 }
478
479 /*
480 * the Cypress chip is a mess, it contains two ATA functions, but
481 * both channels are visible on the first one.
482 * simply ignore the second function for now, as the right
483 * solution (ignoring the second channel on the first function)
484 * doesn't work with the crappy ATA interrupt setup on the alpha.
485 */
486 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
487 return 0;
488
489 ata_pci_add_child(dev, 0);
490
491 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV ||
492 pci_read_config(dev, 0x18, 4) & IOMASK)
493 ata_pci_add_child(dev, 1);
494
495 return bus_generic_attach(dev);
496}
497
498static int
499ata_pci_print_child(device_t dev, device_t child)
500{
501 struct ata_softc *scp = device_get_softc(child);
502 int unit = (uintptr_t) device_get_ivars(child);
503 int retval = 0;
504
505 retval += bus_print_child_header(dev, child);
506 retval += printf(": at 0x%x", scp->ioaddr);
507
508 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV)
509 retval += printf(" irq %d", 14 + unit);
510
511 retval += bus_print_child_footer(dev, child);
512
513 return retval;
514}
515
516static struct resource *
517ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
518 u_long start, u_long end, u_long count, u_int flags)
519{
520 struct ata_pci_softc *sc = device_get_softc(dev);
521 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
522 int unit = (int)device_get_ivars(child);
523 int myrid;
524
525 if (type == SYS_RES_IOPORT) {
526 switch (*rid) {
527 case ATA_IOADDR_RID:
528 if (masterdev) {
529 myrid = 0;
530 start = (unit == 0 ? IO_WD1 : IO_WD2);
531 end = start + ATA_IOSIZE - 1;
532 count = ATA_IOSIZE;
533 }
534 else
535 myrid = 0x10 + 8 * unit;
536 break;
537
538 case ATA_ALTADDR_RID:
539 if (masterdev) {
540 myrid = 0;
541 start = (unit == 0 ? IO_WD1 : IO_WD2) + ATA_ALTPORT;
542 end = start + ATA_ALTIOSIZE - 1;
543 count = ATA_ALTIOSIZE;
544 }
545 else
546 myrid = 0x14 + 8 * unit;
547 break;
548
549 case ATA_BMADDR_RID:
550 /* the busmaster resource is shared between the two channels */
551 if (sc->bmio) {
552 if (unit == 0) {
553 sc->bmio_1 = *sc->bmio;
554 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
555 return &sc->bmio_1;
556 } else {
557 sc->bmio_2 = *sc->bmio;
558 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
559 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
560 return &sc->bmio_2;
561 }
562 }
563 break;
564
565 default:
566 return 0;
567 }
568
569 if (masterdev)
570 /* make the parent just pass through the allocation. */
571 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
572 SYS_RES_IOPORT, &myrid,
573 start, end, count, flags);
574 else
575 /* we are using the parent resource directly. */
576 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
577 SYS_RES_IOPORT, &myrid,
578 start, end, count, flags);
579 }
580
581 if (type == SYS_RES_IRQ) {
582 if (*rid != 0)
583 return 0;
584
585 if (masterdev) {
586#ifdef __i386__
587 int irq = (unit == 0 ? 14 : 15);
588
589 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
590 SYS_RES_IRQ, rid,
591 irq, irq, 1, flags & ~RF_SHAREABLE);
592#else
593 return alpha_platform_alloc_ide_intr(unit);
594#endif
595 } else {
596 /* primary and secondary channels share the same interrupt */
597 sc->irqcnt++;
598 if (!sc->irq)
599 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
600 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
601 return sc->irq;
602 }
603 }
604 return 0;
605}
606
607static int
608ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
609 struct resource *r)
610{
611 struct ata_pci_softc *sc = device_get_softc(dev);
612 int unit = (uintptr_t) device_get_ivars(child);
613 int masterdev = pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV;
614 int myrid = 0;
615
616 if (type == SYS_RES_IOPORT) {
617 switch (rid) {
618 case ATA_IOADDR_RID:
619 if (masterdev)
620 myrid = 0;
621 else
622 myrid = 0x10 + 8 * unit;
623 break;
624
625 case ATA_ALTADDR_RID:
626 if (masterdev)
627 myrid = 0;
628 else
629 myrid = 0x14 + 8 * unit;
630 break;
631
632 case ATA_BMADDR_RID:
633 return 0;
634
635 default:
636 return ENOENT;
637 }
638
639 if (masterdev)
640 /* make the parent just pass through the allocation. */
641 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
642 SYS_RES_IOPORT, myrid, r);
643 else
644 /* we are using the parent resource directly. */
645 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
646 SYS_RES_IOPORT, myrid, r);
647 }
648 if (type == SYS_RES_IRQ) {
649 if (rid != 0)
650 return ENOENT;
651
652 if (masterdev) {
653#ifdef __i386__
654 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
655 child, SYS_RES_IRQ, rid, r);
656#else
657 return alpha_platform_release_ide_intr(unit, r);
658#endif
659 }
660 else {
661 if (--sc->irqcnt)
662 return 0;
663
664 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
665 dev, SYS_RES_IRQ, rid, r);
666 }
667 }
668 return EINVAL;
669}
670
671static int
672ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
673 int flags, driver_intr_t *intr, void *arg,
674 void **cookiep)
675{
676 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
677#ifdef __i386__
678 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
679 flags, intr, arg, cookiep);
680#else
681 return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
682#endif
683 }
684 else
685 return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
686 flags, intr, arg, cookiep);
687}
688
689static int
690ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
691 void *cookie)
692{
693 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
694#ifdef __i386__
695 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
696#else
697 return alpha_platform_teardown_ide_intr(irq, cookie);
698#endif
699 }
700 else
701 return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
702}
703
704static device_method_t ata_pci_methods[] = {
705 /* device interface */
706 DEVMETHOD(device_probe, ata_pci_probe),
707 DEVMETHOD(device_attach, ata_pci_attach),
708 DEVMETHOD(device_shutdown, bus_generic_shutdown),
709 DEVMETHOD(device_suspend, bus_generic_suspend),
710 DEVMETHOD(device_resume, bus_generic_resume),
711
712 /* bus methods */
713 DEVMETHOD(bus_print_child, ata_pci_print_child),
714 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
715 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
716 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
717 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
718 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
719 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
720 { 0, 0 }
721};
722
723static driver_t ata_pci_driver = {
724 "atapci",
725 ata_pci_methods,
726 sizeof(struct ata_pci_softc),
727};
728
729DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
730
731static int
732ata_pcisub_probe(device_t dev)
733{
734 struct ata_softc *scp = device_get_softc(dev);
735
736 /* kids of pci ata chipsets has their physical unit number in ivars */
737 scp->unit = (uintptr_t) device_get_ivars(dev);
738
739 /* set the chiptype to the hostchip ID, makes life easier */
740 if (ata_find_dev(device_get_parent(dev), 0x05861106, 0))
741 scp->chiptype = 0x05861106;
742 else if (ata_find_dev(device_get_parent(dev), 0x05961106, 0))
743 scp->chiptype = 0x05961106;
744 else if (ata_find_dev(device_get_parent(dev), 0x06861106, 0))
745 scp->chiptype = 0x06861106;
746 else
747 scp->chiptype = pci_get_devid(device_get_parent(dev));
748 return ata_probe(dev);
749}
750
751static device_method_t ata_pcisub_methods[] = {
752 /* device interface */
753 DEVMETHOD(device_probe, ata_pcisub_probe),
754 DEVMETHOD(device_attach, ata_attach),
755 DEVMETHOD(device_detach, ata_detach),
756 DEVMETHOD(device_resume, ata_resume),
757 { 0, 0 }
758};
759
760static driver_t ata_pcisub_driver = {
761 "ata",
762 ata_pcisub_methods,
763 sizeof(struct ata_softc),
764};
765
766DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
767#endif
768
769static int
770ata_probe(device_t dev)
771{
772 struct ata_softc *scp = device_get_softc(dev);
773 struct resource *io = 0;
774 struct resource *altio = 0;
775 struct resource *bmio = 0;
776 int rid;
777 int32_t ioaddr, altioaddr, bmaddr;
778 int32_t mask = 0;
779 u_int8_t status0, status1;
780
781 if (!scp || scp->flags & ATA_ATTACHED)
782 return ENXIO;
783
784 /* initialize the softc basics */
785 scp->active = ATA_IDLE;
786 scp->dev = dev;
787 scp->devices = 0;
788
789 rid = ATA_IOADDR_RID;
790 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
791 ATA_IOSIZE, RF_ACTIVE);
792 if (!io)
793 goto failure;
794 ioaddr = rman_get_start(io);
795
796 rid = ATA_ALTADDR_RID;
797 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
798 ATA_ALTIOSIZE, RF_ACTIVE);
799 if (altio)
800 altioaddr = rman_get_start(altio);
801 else
802 altioaddr = ioaddr + ATA_IOSIZE;
803
804 rid = ATA_BMADDR_RID;
805 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
806 bmaddr = bmio ? rman_get_start(bmio) : 0;
807
808 /* store the IO resources for eventual later release */
809 scp->r_io = io;
810 scp->r_altio = altio;
811 scp->r_bmio = bmio;
812
813 /* store the physical IO addresse for easy access */
814 scp->ioaddr = ioaddr;
815 scp->altioaddr = altioaddr;
816 scp->bmaddr = bmaddr;
817
818 if (bootverbose)
819 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
820 scp->ioaddr, scp->altioaddr, scp->bmaddr);
821
822 /* do we have any signs of ATA/ATAPI HW being present ? */
823 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
824 DELAY(1);
825 status0 = inb(scp->ioaddr + ATA_STATUS);
826 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
827 DELAY(1);
828 status1 = inb(scp->ioaddr + ATA_STATUS);
829 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
830 mask |= 0x01;
831 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
832 mask |= 0x02;
833 if (bootverbose)
834 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
835 mask, status0, status1);
836 if (!mask)
837 goto failure;
838
839 ata_reset(scp, &mask);
840 if (!mask)
841 goto failure;
842
843 /*
844 * OK, we have at least one device on the chain, check for ATAPI
845 * signatures, if none check if its a good old ATA device.
846 */
847 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
848 DELAY(1);
849 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
850 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
851 scp->devices |= ATA_ATAPI_MASTER;
852 }
853 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
854 DELAY(1);
855 if (inb(scp->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
856 inb(scp->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB) {
857 scp->devices |= ATA_ATAPI_SLAVE;
858 }
859 if (status0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
860 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_MASTER));
861 DELAY(1);
862 outb(scp->ioaddr + ATA_ERROR, 0x58);
863 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
864 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
865 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
866 scp->devices |= ATA_ATA_MASTER;
867 }
868 }
869 if (status1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
870 outb(scp->ioaddr + ATA_DRIVE, (ATA_D_IBM | ATA_SLAVE));
871 DELAY(1);
872 outb(scp->ioaddr + ATA_ERROR, 0x58);
873 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
874 if (inb(scp->ioaddr + ATA_ERROR) != 0x58 &&
875 inb(scp->ioaddr + ATA_CYL_LSB) == 0xa5) {
876 scp->devices |= ATA_ATA_SLAVE;
877 }
878 }
879 if (bootverbose)
880 ata_printf(scp, -1, "devices = 0x%x\n", scp->devices);
881 if (!scp->devices) {
882 goto failure;
883 }
884 TAILQ_INIT(&scp->ata_queue);
885 TAILQ_INIT(&scp->atapi_queue);
886 return 0;
887
888failure:
889 if (io)
890 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
891 if (altio)
892 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio);
893 if (bmio)
894 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
895 if (bootverbose)
896 ata_printf(scp, -1, "probe allocation failed\n");
897 return ENXIO;
898}
899
900static int
901ata_attach(device_t dev)
902{
903 struct ata_softc *scp = device_get_softc(dev);
904 int error, rid = 0;
905
906 if (!scp || scp->flags & ATA_ATTACHED)
907 return ENXIO;
908
909 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
910 RF_SHAREABLE | RF_ACTIVE);
911 if (!scp->r_irq) {
912 ata_printf(scp, -1, "unable to allocate interrupt\n");
913 return ENXIO;
914 }
915 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
916 scp, &scp->ih)))
917 return error;
918
919 /*
920 * do not attach devices if we are in early boot, this is done later
921 * when interrupts are enabled by a hook into the boot process.
922 * otherwise attach what the probe has found in scp->devices.
923 */
924 if (!ata_delayed_attach) {
925 if (scp->devices & ATA_ATA_SLAVE)
926 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
927 scp->devices &= ~ATA_ATA_SLAVE;
928 if (scp->devices & ATA_ATAPI_SLAVE)
929 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
930 scp->devices &= ~ATA_ATAPI_SLAVE;
931 if (scp->devices & ATA_ATA_MASTER)
932 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
933 scp->devices &= ~ATA_ATA_MASTER;
934 if (scp->devices & ATA_ATAPI_MASTER)
935 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
936 scp->devices &= ~ATA_ATAPI_MASTER;
937#if NATADISK > 0
938 if (scp->devices & ATA_ATA_MASTER)
939 ad_attach(scp, ATA_MASTER);
940 if (scp->devices & ATA_ATA_SLAVE)
941 ad_attach(scp, ATA_SLAVE);
942#endif
943#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
944 if (scp->devices & ATA_ATAPI_MASTER)
945 atapi_attach(scp, ATA_MASTER);
946 if (scp->devices & ATA_ATAPI_SLAVE)
947 atapi_attach(scp, ATA_SLAVE);
948#endif
949 }
950 scp->flags |= ATA_ATTACHED;
951 return 0;
952}
953
954static int
955ata_detach(device_t dev)
956{
957 struct ata_softc *scp = device_get_softc(dev);
958
959 if (!scp || !(scp->flags & ATA_ATTACHED))
960 return ENXIO;
961
962#if NATADISK > 0
963 if (scp->devices & ATA_ATA_MASTER)
964 ad_detach(scp->dev_softc[0]);
965 if (scp->devices & ATA_ATA_SLAVE)
966 ad_detach(scp->dev_softc[1]);
967#endif
968#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
969 if (scp->devices & ATA_ATAPI_MASTER)
970 atapi_detach(scp->dev_softc[0]);
971 if (scp->devices & ATA_ATAPI_SLAVE)
972 atapi_detach(scp->dev_softc[1]);
973#endif
974 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
975 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
976 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
977 }
978 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
979 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
980 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
981 }
982 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL;
983 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL;
984 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
985 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
986 bus_teardown_intr(dev, scp->r_irq, scp->ih);
987 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
988 if (scp->r_bmio)
989 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
990 if (scp->r_altio)
991 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
992 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
993 scp->flags &= ~ATA_ATTACHED;
994 return 0;
995}
996
997static int
998ata_resume(device_t dev)
999{
1000 struct ata_softc *scp = device_get_softc(dev);
1001
1002 ata_reinit(scp);
1003 return 0;
1004}
1005
1006static int32_t
1007ata_getparam(struct ata_softc *scp, int32_t device, u_int8_t command)
1008{
1009 struct ata_params *ata_parm;
1010 int8_t buffer[DEV_BSIZE];
1011 int retry = 0;
1012
1013 /* select drive */
1014 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1015 DELAY(1);
1016
1017 /* enable interrupt */
1018 outb(scp->altioaddr, ATA_A_4BIT);
1019 DELAY(1);
1020
1021 /* apparently some devices needs this repeated */
1022 do {
1023 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
1024 ata_printf(scp, device, "identify failed\n");
1025 return -1;
1026 }
1027 if (retry++ > 4) {
1028 ata_printf(scp, device, "identify retries exceeded\n");
1029 return -1;
1030 }
1031 } while (ata_wait(scp, device,
1032 ((command == ATA_C_ATAPI_IDENTIFY) ?
1033 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
1034
1035 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
1036 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
1037 if (!ata_parm) {
1038 ata_printf(scp, device, "malloc for identify data failed\n");
1039 return -1;
1040 }
1041 bcopy(buffer, ata_parm, sizeof(struct ata_params));
1042 if (command == ATA_C_ATA_IDENTIFY ||
1043 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
1044 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
1045 bswap(ata_parm->model, sizeof(ata_parm->model));
1046 btrim(ata_parm->model, sizeof(ata_parm->model));
1047 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
1048 bswap(ata_parm->revision, sizeof(ata_parm->revision));
1049 btrim(ata_parm->revision, sizeof(ata_parm->revision));
1050 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
1051 scp->dev_param[ATA_DEV(device)] = ata_parm;
1052 return 0;
1053}
1054
1055static void
1056ata_boot_attach(void)
1057{
1058 struct ata_softc *scp;
1059 int32_t ctlr;
1060
1061 /*
1062 * run through all ata devices and look for real ATA & ATAPI devices
1063 * using the hints we found in the early probe, this avoids some of
1064 * the delays probing of non-exsistent devices can cause.
1065 */
1066 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1067 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1068 continue;
1069 if (scp->devices & ATA_ATA_SLAVE)
1070 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
1071 scp->devices &= ~ATA_ATA_SLAVE;
1072 if (scp->devices & ATA_ATAPI_SLAVE)
1073 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
1074 scp->devices &= ~ATA_ATAPI_SLAVE;
1075 if (scp->devices & ATA_ATA_MASTER)
1076 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
1077 scp->devices &= ~ATA_ATA_MASTER;
1078 if (scp->devices & ATA_ATAPI_MASTER)
1079 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
1080 scp->devices &= ~ATA_ATAPI_MASTER;
1081 }
1082
1083#if NATADISK > 0
1084 /* now we know whats there, do the real attach, first the ATA disks */
1085 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1086 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1087 continue;
1088 if (scp->devices & ATA_ATA_MASTER)
1089 ad_attach(scp, ATA_MASTER);
1090 if (scp->devices & ATA_ATA_SLAVE)
1091 ad_attach(scp, ATA_SLAVE);
1092 }
1093#endif
1094#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1095 /* then the atapi devices */
1096 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1097 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1098 continue;
1099 if (scp->devices & ATA_ATAPI_MASTER)
1100 atapi_attach(scp, ATA_MASTER);
1101 if (scp->devices & ATA_ATAPI_SLAVE)
1102 atapi_attach(scp, ATA_SLAVE);
1103 }
1104#endif
1105 if (ata_delayed_attach) {
1106 config_intrhook_disestablish(ata_delayed_attach);
1107 free(ata_delayed_attach, M_ATA);
1108 ata_delayed_attach = NULL;
1109 }
1110}
1111
1112static void
1113ata_intr(void *data)
1114{
1115 struct ata_softc *scp = (struct ata_softc *)data;
1116 u_int8_t dmastat;
1117
1118 /*
1119 * since we might share the IRQ with another device, and in some
1120 * cases with our twin channel, we only want to process interrupts
1121 * that we know this channel generated.
1122 */
1123 switch (scp->chiptype) {
1124#if NPCI > 0
1089 case 0x00041103: /* HighPoint HPT366 */
1125 case 0x00041103: /* HighPoint HPT366/368/370 */
1090 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1091 return;
1092 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1093 break;
1094
1095 case 0x4d33105a: /* Promise 33's */
1096 case 0x4d38105a: /* Promise 66's */
1126 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1127 return;
1128 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1129 break;
1130
1131 case 0x4d33105a: /* Promise 33's */
1132 case 0x4d38105a: /* Promise 66's */
1133 case 0x4d30105a: /* Promise 100's */
1097 {
1098 struct ata_pci_softc *sc=device_get_softc(device_get_parent(scp->dev));
1099
1100 if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
1101 ((scp->unit) ? 0x00004000 : 0x00000400)))
1102 return;
1103 }
1104 /* FALLTHROUGH */
1105#endif
1106 default:
1107 if (scp->flags & ATA_DMA_ACTIVE) {
1108 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1109 return;
1110 else
1111 outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);
1112 }
1113 }
1114 DELAY(1);
1115
1116 /* get status, if drive is busy it didn't interrupt so return */
1117 if ((scp->status = inb(scp->ioaddr + ATA_STATUS)) & ATA_S_BUSY)
1118 return;
1119
1120 /* find & call the responsible driver to process this interrupt */
1121 switch (scp->active) {
1122#if NATADISK > 0
1123 case ATA_ACTIVE_ATA:
1124 if (!scp->running)
1125 return;
1126 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1127 return;
1128 break;
1129#endif
1130#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1131 case ATA_ACTIVE_ATAPI:
1132 if (!scp->running)
1133 return;
1134 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
1135 return;
1136 break;
1137#endif
1138 case ATA_WAIT_INTR:
1139 wakeup((caddr_t)scp);
1140 break;
1141
1142 case ATA_WAIT_READY:
1143 break;
1144
1145 case ATA_REINITING:
1146 return;
1147
1148 default:
1149 case ATA_IDLE:
1150#ifdef ATA_DEBUG
1151 {
1152 static int32_t intr_count = 0;
1153 if (intr_count++ < 10)
1154 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n",
1155 intr_count, scp->status);
1156 }
1157#endif
1158 }
1159 scp->active = ATA_IDLE;
1160 scp->running = NULL;
1161 ata_start(scp);
1162}
1163
1164void
1165ata_start(struct ata_softc *scp)
1166{
1167#if NATADISK > 0
1168 struct ad_request *ad_request;
1169#endif
1170#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1171 struct atapi_request *atapi_request;
1172#endif
1173
1174 if (scp->active != ATA_IDLE)
1175 return;
1176 scp->active = ATA_ACTIVE;
1177
1178#if NATADISK > 0
1179 /* find & call the responsible driver if anything on the ATA queue */
1180 if (TAILQ_EMPTY(&scp->ata_queue)) {
1181 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1182 ad_start((struct ad_softc *)scp->dev_softc[0]);
1183 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1184 ad_start((struct ad_softc *)scp->dev_softc[1]);
1185 }
1186 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
1187 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
1188 scp->active = ATA_ACTIVE_ATA;
1189 scp->running = ad_request;
1190 ad_transfer(ad_request);
1191 return;
1192 }
1193#endif
1194#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1195 /* find & call the responsible driver if anything on the ATAPI queue */
1196 if (TAILQ_EMPTY(&scp->atapi_queue)) {
1197 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1198 atapi_start((struct atapi_softc *)scp->dev_softc[0]);
1199 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1200 atapi_start((struct atapi_softc *)scp->dev_softc[1]);
1201 }
1202 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
1203 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
1204 scp->active = ATA_ACTIVE_ATAPI;
1205 scp->running = atapi_request;
1206 atapi_transfer(atapi_request);
1207 return;
1208 }
1209#endif
1210 scp->active = ATA_IDLE;
1211}
1212
1213void
1214ata_reset(struct ata_softc *scp, int32_t *mask)
1215{
1216 int32_t timeout;
1217 u_int8_t status0 = 0, status1 = 0;
1218
1219 /* reset channel */
1220 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1221 DELAY(1);
1222 inb(scp->ioaddr + ATA_STATUS);
1223 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1224 DELAY(10000);
1225 outb(scp->altioaddr, ATA_A_IDS);
1226 DELAY(10000);
1227 inb(scp->ioaddr + ATA_ERROR);
1228 DELAY(3000);
1229
1230 /* wait for BUSY to go inactive */
1231 for (timeout = 0; timeout < 310000; timeout++) {
1232 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1233 DELAY(1);
1234 status0 = inb(scp->ioaddr + ATA_STATUS);
1235 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1236 DELAY(1);
1237 status1 = inb(scp->ioaddr + ATA_STATUS);
1238 if (*mask == 0x01) /* wait for master only */
1239 if (!(status0 & ATA_S_BUSY))
1240 break;
1241 if (*mask == 0x02) /* wait for slave only */
1242 if (!(status1 & ATA_S_BUSY))
1243 break;
1244 if (*mask == 0x03) /* wait for both master & slave */
1245 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1246 break;
1247 if (ata_delayed_attach)
1248 DELAY(100);
1249 else
1250 tsleep(ata_delayed_attach, PRIBIO, "atarst", 1);
1251 }
1252 DELAY(1);
1253 outb(scp->altioaddr, ATA_A_4BIT);
1254 if (status0 & ATA_S_BUSY)
1255 *mask &= ~0x01;
1256 if (status1 & ATA_S_BUSY)
1257 *mask &= ~0x02;
1258 if (bootverbose)
1259 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1260 *mask, status0, status1);
1261}
1262
1263int32_t
1264ata_reinit(struct ata_softc *scp)
1265{
1266 int32_t mask = 0, omask;
1267
1268 scp->active = ATA_REINITING;
1269 scp->running = NULL;
1270 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
1271 mask |= 0x01;
1272 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
1273 mask |= 0x02;
1274 if (mask) {
1275 omask = mask;
1276 ata_printf(scp, -1, "resetting devices .. ");
1277 ata_reset(scp, &mask);
1278 if (omask != mask)
1279 printf(" device dissapeared! %d ", omask & ~mask);
1280
1281#if NATADISK > 0
1282 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1283 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
1284 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1285 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
1286#endif
1287#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1288 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1289 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
1290 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1291 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
1292#endif
1293 printf("done\n");
1294 }
1295 scp->active = ATA_IDLE;
1296 ata_start(scp);
1297 return 0;
1298}
1299
1300int32_t
1301ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
1302{
1303 u_int32_t timeout = 0;
1304
1305 DELAY(1);
1306 while (timeout < 5000000) { /* timeout 5 secs */
1307 scp->status = inb(scp->ioaddr + ATA_STATUS);
1308
1309 /* if drive fails status, reselect the drive just to be sure */
1310 if (scp->status == 0xff) {
1311 ata_printf(scp, device, "no status, reselecting device\n");
1312 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1313 DELAY(1);
1314 scp->status = inb(scp->ioaddr + ATA_STATUS);
1315 }
1316
1317 /* are we done ? */
1318 if (!(scp->status & ATA_S_BUSY))
1319 break;
1320
1321 if (timeout > 1000) {
1322 timeout += 1000;
1323 DELAY(1000);
1324 }
1325 else {
1326 timeout += 10;
1327 DELAY(10);
1328 }
1329 }
1330 if (scp->status & ATA_S_ERROR)
1331 scp->error = inb(scp->ioaddr + ATA_ERROR);
1332 if (timeout >= 5000000)
1333 return -1;
1334 if (!mask)
1335 return (scp->status & ATA_S_ERROR);
1336
1337 /* Wait 50 msec for bits wanted. */
1338 timeout = 5000;
1339 while (timeout--) {
1340 scp->status = inb(scp->ioaddr + ATA_STATUS);
1341 if ((scp->status & mask) == mask) {
1342 if (scp->status & ATA_S_ERROR)
1343 scp->error = inb(scp->ioaddr + ATA_ERROR);
1344 return (scp->status & ATA_S_ERROR);
1345 }
1346 DELAY (10);
1347 }
1348 return -1;
1349}
1350
1351int32_t
1352ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
1353 u_int32_t cylinder, u_int32_t head, u_int32_t sector,
1354 u_int32_t count, u_int32_t feature, int32_t flags)
1355{
1356#ifdef ATA_DEBUG
1357 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1358 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
1359 scp->ioaddr, command, cylinder, head, sector, count, flags);
1360#endif
1361
1362 /* ready to issue command ? */
1363 if (ata_wait(scp, device, 0) < 0) {
1364 ata_printf(scp, device,
1365 "timeout waiting to give command=%02x s=%02x e=%02x\n",
1366 command, scp->status, scp->error);
1367 return -1;
1368 }
1369 outb(scp->ioaddr + ATA_FEATURE, feature);
1370 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1371 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1372 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1373 outb(scp->ioaddr + ATA_SECTOR, sector);
1374 outb(scp->ioaddr + ATA_COUNT, count);
1375
1376 switch (flags) {
1377 case ATA_WAIT_INTR:
1378 if (scp->active != ATA_IDLE)
1379 ata_printf(scp, device, "WARNING: WAIT_INTR active=%s\n",
1380 active2str(scp->active));
1381 scp->active = ATA_WAIT_INTR;
1382 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1383 outb(scp->ioaddr + ATA_CMD, command);
1384 if (await(PRIBIO, 10 * hz)) {
1385 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1386 scp->active = ATA_IDLE;
1387 return -1;
1388 }
1389 break;
1390
1391 case ATA_WAIT_READY:
1392 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING)
1393 ata_printf(scp, device, "WARNING: WAIT_READY active=%s\n",
1394 active2str(scp->active));
1395 if (scp->active != ATA_REINITING)
1396 scp->active = ATA_WAIT_READY;
1397 outb(scp->ioaddr + ATA_CMD, command);
1398 if (ata_wait(scp, device, ATA_S_READY) < 0) {
1399 ata_printf(scp, device,
1400 "timeout waiting for command=%02x s=%02x e=%02x\n",
1401 command, scp->status, scp->error);
1402 if (scp->active != ATA_REINITING)
1403 scp->active = ATA_IDLE;
1404 return -1;
1405 }
1406 if (scp->active != ATA_REINITING)
1407 scp->active = ATA_IDLE;
1408 break;
1409
1410 case ATA_IMMEDIATE:
1411 outb(scp->ioaddr + ATA_CMD, command);
1412 break;
1413
1414 default:
1415 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1416 active2str(flags));
1417 }
1418 return 0;
1419}
1420
1421int
1422ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1423{
1424 va_list ap;
1425 int ret;
1426
1427 if (device == -1)
1428 ret = printf("ata%d: ", device_get_unit(scp->dev));
1429 else
1430 ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1431 (device == ATA_MASTER) ? "master" : "slave");
1432 va_start(ap, fmt);
1433 ret += vprintf(fmt, ap);
1434 va_end(ap);
1435 return ret;
1436}
1437
1438int
1439ata_get_lun(u_int32_t *map)
1440{
1441 int lun = ffs(~*map) - 1;
1442
1443 *map |= (1 << lun);
1444 return lun;
1445}
1446
1447void
1448ata_free_lun(u_int32_t *map, int lun)
1449{
1450 *map &= ~(1 << lun);
1451}
1452
1453int8_t *
1454ata_mode2str(int32_t mode)
1455{
1456 switch (mode) {
1457 case ATA_PIO: return "BIOSPIO";
1458 case ATA_PIO0: return "PIO0";
1459 case ATA_PIO1: return "PIO1";
1460 case ATA_PIO2: return "PIO2";
1461 case ATA_PIO3: return "PIO3";
1462 case ATA_PIO4: return "PIO4";
1463 case ATA_WDMA2: return "WDMA2";
1464 case ATA_UDMA2: return "UDMA33";
1465 case ATA_UDMA4: return "UDMA66";
1134 {
1135 struct ata_pci_softc *sc=device_get_softc(device_get_parent(scp->dev));
1136
1137 if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
1138 ((scp->unit) ? 0x00004000 : 0x00000400)))
1139 return;
1140 }
1141 /* FALLTHROUGH */
1142#endif
1143 default:
1144 if (scp->flags & ATA_DMA_ACTIVE) {
1145 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1146 return;
1147 else
1148 outb(scp->bmaddr+ATA_BMSTAT_PORT, dmastat|ATA_BMSTAT_INTERRUPT);
1149 }
1150 }
1151 DELAY(1);
1152
1153 /* get status, if drive is busy it didn't interrupt so return */
1154 if ((scp->status = inb(scp->ioaddr + ATA_STATUS)) & ATA_S_BUSY)
1155 return;
1156
1157 /* find & call the responsible driver to process this interrupt */
1158 switch (scp->active) {
1159#if NATADISK > 0
1160 case ATA_ACTIVE_ATA:
1161 if (!scp->running)
1162 return;
1163 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1164 return;
1165 break;
1166#endif
1167#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1168 case ATA_ACTIVE_ATAPI:
1169 if (!scp->running)
1170 return;
1171 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
1172 return;
1173 break;
1174#endif
1175 case ATA_WAIT_INTR:
1176 wakeup((caddr_t)scp);
1177 break;
1178
1179 case ATA_WAIT_READY:
1180 break;
1181
1182 case ATA_REINITING:
1183 return;
1184
1185 default:
1186 case ATA_IDLE:
1187#ifdef ATA_DEBUG
1188 {
1189 static int32_t intr_count = 0;
1190 if (intr_count++ < 10)
1191 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n",
1192 intr_count, scp->status);
1193 }
1194#endif
1195 }
1196 scp->active = ATA_IDLE;
1197 scp->running = NULL;
1198 ata_start(scp);
1199}
1200
1201void
1202ata_start(struct ata_softc *scp)
1203{
1204#if NATADISK > 0
1205 struct ad_request *ad_request;
1206#endif
1207#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1208 struct atapi_request *atapi_request;
1209#endif
1210
1211 if (scp->active != ATA_IDLE)
1212 return;
1213 scp->active = ATA_ACTIVE;
1214
1215#if NATADISK > 0
1216 /* find & call the responsible driver if anything on the ATA queue */
1217 if (TAILQ_EMPTY(&scp->ata_queue)) {
1218 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1219 ad_start((struct ad_softc *)scp->dev_softc[0]);
1220 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1221 ad_start((struct ad_softc *)scp->dev_softc[1]);
1222 }
1223 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
1224 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
1225 scp->active = ATA_ACTIVE_ATA;
1226 scp->running = ad_request;
1227 ad_transfer(ad_request);
1228 return;
1229 }
1230#endif
1231#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1232 /* find & call the responsible driver if anything on the ATAPI queue */
1233 if (TAILQ_EMPTY(&scp->atapi_queue)) {
1234 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1235 atapi_start((struct atapi_softc *)scp->dev_softc[0]);
1236 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1237 atapi_start((struct atapi_softc *)scp->dev_softc[1]);
1238 }
1239 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
1240 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
1241 scp->active = ATA_ACTIVE_ATAPI;
1242 scp->running = atapi_request;
1243 atapi_transfer(atapi_request);
1244 return;
1245 }
1246#endif
1247 scp->active = ATA_IDLE;
1248}
1249
1250void
1251ata_reset(struct ata_softc *scp, int32_t *mask)
1252{
1253 int32_t timeout;
1254 u_int8_t status0 = 0, status1 = 0;
1255
1256 /* reset channel */
1257 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1258 DELAY(1);
1259 inb(scp->ioaddr + ATA_STATUS);
1260 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1261 DELAY(10000);
1262 outb(scp->altioaddr, ATA_A_IDS);
1263 DELAY(10000);
1264 inb(scp->ioaddr + ATA_ERROR);
1265 DELAY(3000);
1266
1267 /* wait for BUSY to go inactive */
1268 for (timeout = 0; timeout < 310000; timeout++) {
1269 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1270 DELAY(1);
1271 status0 = inb(scp->ioaddr + ATA_STATUS);
1272 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1273 DELAY(1);
1274 status1 = inb(scp->ioaddr + ATA_STATUS);
1275 if (*mask == 0x01) /* wait for master only */
1276 if (!(status0 & ATA_S_BUSY))
1277 break;
1278 if (*mask == 0x02) /* wait for slave only */
1279 if (!(status1 & ATA_S_BUSY))
1280 break;
1281 if (*mask == 0x03) /* wait for both master & slave */
1282 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1283 break;
1284 if (ata_delayed_attach)
1285 DELAY(100);
1286 else
1287 tsleep(ata_delayed_attach, PRIBIO, "atarst", 1);
1288 }
1289 DELAY(1);
1290 outb(scp->altioaddr, ATA_A_4BIT);
1291 if (status0 & ATA_S_BUSY)
1292 *mask &= ~0x01;
1293 if (status1 & ATA_S_BUSY)
1294 *mask &= ~0x02;
1295 if (bootverbose)
1296 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1297 *mask, status0, status1);
1298}
1299
1300int32_t
1301ata_reinit(struct ata_softc *scp)
1302{
1303 int32_t mask = 0, omask;
1304
1305 scp->active = ATA_REINITING;
1306 scp->running = NULL;
1307 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
1308 mask |= 0x01;
1309 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
1310 mask |= 0x02;
1311 if (mask) {
1312 omask = mask;
1313 ata_printf(scp, -1, "resetting devices .. ");
1314 ata_reset(scp, &mask);
1315 if (omask != mask)
1316 printf(" device dissapeared! %d ", omask & ~mask);
1317
1318#if NATADISK > 0
1319 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[0])
1320 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
1321 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[1])
1322 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
1323#endif
1324#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
1325 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[0])
1326 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
1327 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[1])
1328 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
1329#endif
1330 printf("done\n");
1331 }
1332 scp->active = ATA_IDLE;
1333 ata_start(scp);
1334 return 0;
1335}
1336
1337int32_t
1338ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
1339{
1340 u_int32_t timeout = 0;
1341
1342 DELAY(1);
1343 while (timeout < 5000000) { /* timeout 5 secs */
1344 scp->status = inb(scp->ioaddr + ATA_STATUS);
1345
1346 /* if drive fails status, reselect the drive just to be sure */
1347 if (scp->status == 0xff) {
1348 ata_printf(scp, device, "no status, reselecting device\n");
1349 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1350 DELAY(1);
1351 scp->status = inb(scp->ioaddr + ATA_STATUS);
1352 }
1353
1354 /* are we done ? */
1355 if (!(scp->status & ATA_S_BUSY))
1356 break;
1357
1358 if (timeout > 1000) {
1359 timeout += 1000;
1360 DELAY(1000);
1361 }
1362 else {
1363 timeout += 10;
1364 DELAY(10);
1365 }
1366 }
1367 if (scp->status & ATA_S_ERROR)
1368 scp->error = inb(scp->ioaddr + ATA_ERROR);
1369 if (timeout >= 5000000)
1370 return -1;
1371 if (!mask)
1372 return (scp->status & ATA_S_ERROR);
1373
1374 /* Wait 50 msec for bits wanted. */
1375 timeout = 5000;
1376 while (timeout--) {
1377 scp->status = inb(scp->ioaddr + ATA_STATUS);
1378 if ((scp->status & mask) == mask) {
1379 if (scp->status & ATA_S_ERROR)
1380 scp->error = inb(scp->ioaddr + ATA_ERROR);
1381 return (scp->status & ATA_S_ERROR);
1382 }
1383 DELAY (10);
1384 }
1385 return -1;
1386}
1387
1388int32_t
1389ata_command(struct ata_softc *scp, int32_t device, u_int32_t command,
1390 u_int32_t cylinder, u_int32_t head, u_int32_t sector,
1391 u_int32_t count, u_int32_t feature, int32_t flags)
1392{
1393#ifdef ATA_DEBUG
1394 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1395 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
1396 scp->ioaddr, command, cylinder, head, sector, count, flags);
1397#endif
1398
1399 /* ready to issue command ? */
1400 if (ata_wait(scp, device, 0) < 0) {
1401 ata_printf(scp, device,
1402 "timeout waiting to give command=%02x s=%02x e=%02x\n",
1403 command, scp->status, scp->error);
1404 return -1;
1405 }
1406 outb(scp->ioaddr + ATA_FEATURE, feature);
1407 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1408 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1409 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1410 outb(scp->ioaddr + ATA_SECTOR, sector);
1411 outb(scp->ioaddr + ATA_COUNT, count);
1412
1413 switch (flags) {
1414 case ATA_WAIT_INTR:
1415 if (scp->active != ATA_IDLE)
1416 ata_printf(scp, device, "WARNING: WAIT_INTR active=%s\n",
1417 active2str(scp->active));
1418 scp->active = ATA_WAIT_INTR;
1419 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1420 outb(scp->ioaddr + ATA_CMD, command);
1421 if (await(PRIBIO, 10 * hz)) {
1422 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1423 scp->active = ATA_IDLE;
1424 return -1;
1425 }
1426 break;
1427
1428 case ATA_WAIT_READY:
1429 if (scp->active != ATA_IDLE && scp->active != ATA_REINITING)
1430 ata_printf(scp, device, "WARNING: WAIT_READY active=%s\n",
1431 active2str(scp->active));
1432 if (scp->active != ATA_REINITING)
1433 scp->active = ATA_WAIT_READY;
1434 outb(scp->ioaddr + ATA_CMD, command);
1435 if (ata_wait(scp, device, ATA_S_READY) < 0) {
1436 ata_printf(scp, device,
1437 "timeout waiting for command=%02x s=%02x e=%02x\n",
1438 command, scp->status, scp->error);
1439 if (scp->active != ATA_REINITING)
1440 scp->active = ATA_IDLE;
1441 return -1;
1442 }
1443 if (scp->active != ATA_REINITING)
1444 scp->active = ATA_IDLE;
1445 break;
1446
1447 case ATA_IMMEDIATE:
1448 outb(scp->ioaddr + ATA_CMD, command);
1449 break;
1450
1451 default:
1452 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1453 active2str(flags));
1454 }
1455 return 0;
1456}
1457
1458int
1459ata_printf(struct ata_softc *scp, int32_t device, const char * fmt, ...)
1460{
1461 va_list ap;
1462 int ret;
1463
1464 if (device == -1)
1465 ret = printf("ata%d: ", device_get_unit(scp->dev));
1466 else
1467 ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1468 (device == ATA_MASTER) ? "master" : "slave");
1469 va_start(ap, fmt);
1470 ret += vprintf(fmt, ap);
1471 va_end(ap);
1472 return ret;
1473}
1474
1475int
1476ata_get_lun(u_int32_t *map)
1477{
1478 int lun = ffs(~*map) - 1;
1479
1480 *map |= (1 << lun);
1481 return lun;
1482}
1483
1484void
1485ata_free_lun(u_int32_t *map, int lun)
1486{
1487 *map &= ~(1 << lun);
1488}
1489
1490int8_t *
1491ata_mode2str(int32_t mode)
1492{
1493 switch (mode) {
1494 case ATA_PIO: return "BIOSPIO";
1495 case ATA_PIO0: return "PIO0";
1496 case ATA_PIO1: return "PIO1";
1497 case ATA_PIO2: return "PIO2";
1498 case ATA_PIO3: return "PIO3";
1499 case ATA_PIO4: return "PIO4";
1500 case ATA_WDMA2: return "WDMA2";
1501 case ATA_UDMA2: return "UDMA33";
1502 case ATA_UDMA4: return "UDMA66";
1503 case ATA_UDMA5: return "UDMA100";
1466 case ATA_DMA: return "BIOSDMA";
1467 default: return "???";
1468 }
1469}
1470
1471int8_t
1472ata_pio2mode(int32_t pio)
1473{
1474 switch (pio) {
1475 default:
1476 case 0: return ATA_PIO0;
1477 case 1: return ATA_PIO1;
1478 case 2: return ATA_PIO2;
1479 case 3: return ATA_PIO3;
1480 case 4: return ATA_PIO4;
1481 }
1482}
1483
1484int
1485ata_pmode(struct ata_params *ap)
1486{
1487 if (ap->atavalid & ATA_FLAG_64_70) {
1488 if (ap->apiomodes & 2)
1489 return 4;
1490 if (ap->apiomodes & 1)
1491 return 3;
1492 }
1493 if (ap->opiomode == 2)
1494 return 2;
1495 if (ap->opiomode == 1)
1496 return 1;
1497 if (ap->opiomode == 0)
1498 return 0;
1499 return -1;
1500}
1501
1502int
1503ata_wmode(struct ata_params *ap)
1504{
1505 if (ap->wdmamodes & 4)
1506 return 2;
1507 if (ap->wdmamodes & 2)
1508 return 1;
1509 if (ap->wdmamodes & 1)
1510 return 0;
1511 return -1;
1512}
1513
1514int
1515ata_umode(struct ata_params *ap)
1516{
1517 if (ap->atavalid & ATA_FLAG_88) {
1504 case ATA_DMA: return "BIOSDMA";
1505 default: return "???";
1506 }
1507}
1508
1509int8_t
1510ata_pio2mode(int32_t pio)
1511{
1512 switch (pio) {
1513 default:
1514 case 0: return ATA_PIO0;
1515 case 1: return ATA_PIO1;
1516 case 2: return ATA_PIO2;
1517 case 3: return ATA_PIO3;
1518 case 4: return ATA_PIO4;
1519 }
1520}
1521
1522int
1523ata_pmode(struct ata_params *ap)
1524{
1525 if (ap->atavalid & ATA_FLAG_64_70) {
1526 if (ap->apiomodes & 2)
1527 return 4;
1528 if (ap->apiomodes & 1)
1529 return 3;
1530 }
1531 if (ap->opiomode == 2)
1532 return 2;
1533 if (ap->opiomode == 1)
1534 return 1;
1535 if (ap->opiomode == 0)
1536 return 0;
1537 return -1;
1538}
1539
1540int
1541ata_wmode(struct ata_params *ap)
1542{
1543 if (ap->wdmamodes & 4)
1544 return 2;
1545 if (ap->wdmamodes & 2)
1546 return 1;
1547 if (ap->wdmamodes & 1)
1548 return 0;
1549 return -1;
1550}
1551
1552int
1553ata_umode(struct ata_params *ap)
1554{
1555 if (ap->atavalid & ATA_FLAG_88) {
1556 if (ap->udmamodes & 0x20)
1557 return 5;
1518 if (ap->udmamodes & 0x10)
1519 return 4;
1520 if (ap->udmamodes & 0x08)
1521 return 3;
1522 if (ap->udmamodes & 0x04)
1523 return 2;
1524 if (ap->udmamodes & 0x02)
1525 return 1;
1526 if (ap->udmamodes & 0x01)
1527 return 0;
1528 }
1529 return -1;
1530}
1531
1532static int8_t *
1533active2str(int32_t active)
1534{
1535 static char buf[8];
1536
1537 switch (active) {
1538 case ATA_IDLE:
1539 return("ATA_IDLE");
1540 case ATA_IMMEDIATE:
1541 return("ATA_IMMEDIATE");
1542 case ATA_WAIT_INTR:
1543 return("ATA_WAIT_INTR");
1544 case ATA_WAIT_READY:
1545 return("ATA_WAIT_READY");
1546 case ATA_ACTIVE:
1547 return("ATA_ACTIVE");
1548 case ATA_ACTIVE_ATA:
1549 return("ATA_ACTIVE_ATA");
1550 case ATA_ACTIVE_ATAPI:
1551 return("ATA_ACTIVE_ATAPI");
1552 case ATA_REINITING:
1553 return("ATA_REINITING");
1554 default:
1555 sprintf(buf, "0x%02x", active);
1556 return buf;
1557 }
1558}
1559
1560static void
1561bswap(int8_t *buf, int32_t len)
1562{
1563 u_int16_t *ptr = (u_int16_t*)(buf + len);
1564
1565 while (--ptr >= (u_int16_t*)buf)
1566 *ptr = ntohs(*ptr);
1567}
1568
1569static void
1570btrim(int8_t *buf, int32_t len)
1571{
1572 int8_t *ptr;
1573
1574 for (ptr = buf; ptr < buf+len; ++ptr)
1575 if (!*ptr)
1576 *ptr = ' ';
1577 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1578 *ptr = 0;
1579}
1580
1581static void
1582bpack(int8_t *src, int8_t *dst, int32_t len)
1583{
1584 int32_t i, j, blank;
1585
1586 for (i = j = blank = 0 ; i < len; i++) {
1587 if (blank && src[i] == ' ') continue;
1588 if (blank && src[i] != ' ') {
1589 dst[j++] = src[i];
1590 blank = 0;
1591 continue;
1592 }
1593 if (src[i] == ' ') {
1594 blank = 1;
1595 if (i == 0)
1596 continue;
1597 }
1598 dst[j++] = src[i];
1599 }
1600 if (j < len)
1601 dst[j] = 0x00;
1602}
1603
1604static void
1605ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1606{
1607 int32_t s = splbio();
1608
1609 while (scp->active != ATA_IDLE)
1610 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1611 scp->active = ATA_REINITING;
1612 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1613 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1614 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1615 scp->active = ATA_IDLE;
1616 ata_start(scp);
1617 splx(s);
1618}
1619
1620static int
1621sysctl_hw_ata(SYSCTL_HANDLER_ARGS)
1622{
1623 struct ata_softc *scp;
1624 int ctlr, error, i;
1625
1626 /* readout internal state */
1627 bzero(ata_conf, sizeof(ata_conf));
1628 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1629 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1630 continue;
1631 for (i = 0; i < 2; i++) {
1632 if (!scp->dev_softc[i])
1633 strcat(ata_conf, "---,");
1634 else if (scp->mode[i] >= ATA_DMA)
1635 strcat(ata_conf, "dma,");
1636 else
1637 strcat(ata_conf, "pio,");
1638 }
1639 }
1640 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1641 if (error == 0 && req->newptr != NULL) {
1642 char *ptr = ata_conf;
1643
1644 /* update internal state */
1645 i = 0;
1646 while (*ptr) {
1647 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1648 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1649 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1650 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1651 }
1652 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1653 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1654 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1655 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1656 }
1657 else if (strncmp(ptr, "---", 3))
1658 break;
1659 ptr+=3;
1660 if (*ptr++ != ',' ||
1661 ++i > (devclass_get_maxunit(ata_devclass) << 1))
1662 break;
1663 }
1664 }
1665 return error;
1666}
1667SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1668 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1669
1670static void
1671ata_init(void)
1672{
1673 /* register boot attach to be run when interrupts are enabled */
1674 if (!(ata_delayed_attach = (struct intr_config_hook *)
1675 malloc(sizeof(struct intr_config_hook),
1676 M_TEMP, M_NOWAIT))) {
1677 printf("ata: malloc of delayed attach hook failed\n");
1678 return;
1679 }
1680 bzero(ata_delayed_attach, sizeof(struct intr_config_hook));
1681
1682 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1683 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1684 printf("ata: config_intrhook_establish failed\n");
1685 free(ata_delayed_attach, M_TEMP);
1686 }
1687}
1688SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
1558 if (ap->udmamodes & 0x10)
1559 return 4;
1560 if (ap->udmamodes & 0x08)
1561 return 3;
1562 if (ap->udmamodes & 0x04)
1563 return 2;
1564 if (ap->udmamodes & 0x02)
1565 return 1;
1566 if (ap->udmamodes & 0x01)
1567 return 0;
1568 }
1569 return -1;
1570}
1571
1572static int8_t *
1573active2str(int32_t active)
1574{
1575 static char buf[8];
1576
1577 switch (active) {
1578 case ATA_IDLE:
1579 return("ATA_IDLE");
1580 case ATA_IMMEDIATE:
1581 return("ATA_IMMEDIATE");
1582 case ATA_WAIT_INTR:
1583 return("ATA_WAIT_INTR");
1584 case ATA_WAIT_READY:
1585 return("ATA_WAIT_READY");
1586 case ATA_ACTIVE:
1587 return("ATA_ACTIVE");
1588 case ATA_ACTIVE_ATA:
1589 return("ATA_ACTIVE_ATA");
1590 case ATA_ACTIVE_ATAPI:
1591 return("ATA_ACTIVE_ATAPI");
1592 case ATA_REINITING:
1593 return("ATA_REINITING");
1594 default:
1595 sprintf(buf, "0x%02x", active);
1596 return buf;
1597 }
1598}
1599
1600static void
1601bswap(int8_t *buf, int32_t len)
1602{
1603 u_int16_t *ptr = (u_int16_t*)(buf + len);
1604
1605 while (--ptr >= (u_int16_t*)buf)
1606 *ptr = ntohs(*ptr);
1607}
1608
1609static void
1610btrim(int8_t *buf, int32_t len)
1611{
1612 int8_t *ptr;
1613
1614 for (ptr = buf; ptr < buf+len; ++ptr)
1615 if (!*ptr)
1616 *ptr = ' ';
1617 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1618 *ptr = 0;
1619}
1620
1621static void
1622bpack(int8_t *src, int8_t *dst, int32_t len)
1623{
1624 int32_t i, j, blank;
1625
1626 for (i = j = blank = 0 ; i < len; i++) {
1627 if (blank && src[i] == ' ') continue;
1628 if (blank && src[i] != ' ') {
1629 dst[j++] = src[i];
1630 blank = 0;
1631 continue;
1632 }
1633 if (src[i] == ' ') {
1634 blank = 1;
1635 if (i == 0)
1636 continue;
1637 }
1638 dst[j++] = src[i];
1639 }
1640 if (j < len)
1641 dst[j] = 0x00;
1642}
1643
1644static void
1645ata_change_mode(struct ata_softc *scp, int32_t device, int32_t mode)
1646{
1647 int32_t s = splbio();
1648
1649 while (scp->active != ATA_IDLE)
1650 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1651 scp->active = ATA_REINITING;
1652 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1653 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1654 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1655 scp->active = ATA_IDLE;
1656 ata_start(scp);
1657 splx(s);
1658}
1659
1660static int
1661sysctl_hw_ata(SYSCTL_HANDLER_ARGS)
1662{
1663 struct ata_softc *scp;
1664 int ctlr, error, i;
1665
1666 /* readout internal state */
1667 bzero(ata_conf, sizeof(ata_conf));
1668 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1669 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1670 continue;
1671 for (i = 0; i < 2; i++) {
1672 if (!scp->dev_softc[i])
1673 strcat(ata_conf, "---,");
1674 else if (scp->mode[i] >= ATA_DMA)
1675 strcat(ata_conf, "dma,");
1676 else
1677 strcat(ata_conf, "pio,");
1678 }
1679 }
1680 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1681 if (error == 0 && req->newptr != NULL) {
1682 char *ptr = ata_conf;
1683
1684 /* update internal state */
1685 i = 0;
1686 while (*ptr) {
1687 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1688 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1689 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1690 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1691 }
1692 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1693 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1694 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1695 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1696 }
1697 else if (strncmp(ptr, "---", 3))
1698 break;
1699 ptr+=3;
1700 if (*ptr++ != ',' ||
1701 ++i > (devclass_get_maxunit(ata_devclass) << 1))
1702 break;
1703 }
1704 }
1705 return error;
1706}
1707SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1708 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1709
1710static void
1711ata_init(void)
1712{
1713 /* register boot attach to be run when interrupts are enabled */
1714 if (!(ata_delayed_attach = (struct intr_config_hook *)
1715 malloc(sizeof(struct intr_config_hook),
1716 M_TEMP, M_NOWAIT))) {
1717 printf("ata: malloc of delayed attach hook failed\n");
1718 return;
1719 }
1720 bzero(ata_delayed_attach, sizeof(struct intr_config_hook));
1721
1722 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1723 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1724 printf("ata: config_intrhook_establish failed\n");
1725 free(ata_delayed_attach, M_TEMP);
1726 }
1727}
1728SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)