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