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