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