Deleted Added
full compact
ata-all.c (74253) ata-all.c (74302)
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 74253 2001-03-14 14:00:09Z sos $
28 * $FreeBSD: head/sys/dev/ata/ata-all.c 74302 2001-03-15 15:36:25Z sos $
29 */
30
31#include "pci.h"
32#include "opt_ata.h"
33#include <sys/param.h>
34#include <sys/systm.h>
29 */
30
31#include "pci.h"
32#include "opt_ata.h"
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/ata.h>
35#include <sys/kernel.h>
36#include <sys/kernel.h>
37#include <sys/conf.h>
36#include <sys/disk.h>
37#include <sys/module.h>
38#include <sys/bus.h>
39#include <sys/bio.h>
40#include <sys/malloc.h>
41#include <sys/devicestat.h>
42#include <sys/sysctl.h>
43#include <machine/stdarg.h>
44#include <machine/resource.h>
45#include <machine/bus.h>
46#include <sys/rman.h>
47#ifdef __alpha__
48#include <machine/md_var.h>
49#endif
50#include <dev/ata/ata-all.h>
51#include <dev/ata/ata-disk.h>
52#include <dev/ata/atapi-all.h>
53
38#include <sys/disk.h>
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <sys/bio.h>
42#include <sys/malloc.h>
43#include <sys/devicestat.h>
44#include <sys/sysctl.h>
45#include <machine/stdarg.h>
46#include <machine/resource.h>
47#include <machine/bus.h>
48#include <sys/rman.h>
49#ifdef __alpha__
50#include <machine/md_var.h>
51#endif
52#include <dev/ata/ata-all.h>
53#include <dev/ata/ata-disk.h>
54#include <dev/ata/atapi-all.h>
55
56/* device structures */
57static d_ioctl_t ataioctl;
58static struct cdevsw ata_cdevsw = {
59 /* open */ nullopen,
60 /* close */ nullclose,
61 /* read */ noread,
62 /* write */ nowrite,
63 /* ioctl */ ataioctl,
64 /* poll */ nopoll,
65 /* mmap */ nommap,
66 /* strategy */ nostrategy,
67 /* name */ "ata",
68 /* maj */ 159,
69 /* dump */ nodump,
70 /* psize */ nopsize,
71 /* flags */ 0,
72 /* bmaj */ -1
73};
74
54/* prototypes */
55static void ata_boot_attach(void);
56static void ata_intr(void *);
57static int ata_getparam(struct ata_softc *, int, u_int8_t);
58static int ata_service(struct ata_softc *);
59static char *active2str(int);
60static void bswap(int8_t *, int);
61static void btrim(int8_t *, int);
62static void bpack(int8_t *, int8_t *, int);
75/* prototypes */
76static void ata_boot_attach(void);
77static void ata_intr(void *);
78static int ata_getparam(struct ata_softc *, int, u_int8_t);
79static int ata_service(struct ata_softc *);
80static char *active2str(int);
81static void bswap(int8_t *, int);
82static void btrim(int8_t *, int);
83static void bpack(int8_t *, int8_t *, int);
84static void ata_change_mode(struct ata_softc *, int, int);
63
64/* global vars */
65devclass_t ata_devclass;
66
67/* local vars */
68static struct intr_config_hook *ata_delayed_attach = NULL;
85
86/* global vars */
87devclass_t ata_devclass;
88
89/* local vars */
90static struct intr_config_hook *ata_delayed_attach = NULL;
69static char ata_conf[256];
70static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
71
72/* misc defines */
73#define MASTER 0
74#define SLAVE 1
75
76int
77ata_probe(device_t dev)
78{
79 struct ata_softc *scp;
80 int rid;
81
82 if (!dev)
83 return ENXIO;
84 scp = device_get_softc(dev);
85 if (!scp || scp->devices)
86 return ENXIO;
87
88 /* initialize the softc basics */
89 scp->active = ATA_IDLE;
90 scp->dev = dev;
91
92 rid = ATA_IOADDR_RID;
93 scp->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
94 ATA_IOSIZE, RF_ACTIVE);
95 if (!scp->r_io)
96 goto failure;
97
98 rid = ATA_ALTADDR_RID;
99 scp->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
100 ATA_ALTIOSIZE, RF_ACTIVE);
101 if (!scp->r_altio)
102 goto failure;
103
104 rid = ATA_BMADDR_RID;
105 scp->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
106 ATA_BMIOSIZE, RF_ACTIVE);
107
108 if (bootverbose)
109 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
110 (int)rman_get_start(scp->r_io),
111 (int)rman_get_start(scp->r_altio),
112 (scp->r_bmio) ? (int)rman_get_start(scp->r_bmio) : 0);
113
114 ata_reset(scp);
115
116 TAILQ_INIT(&scp->ata_queue);
117 TAILQ_INIT(&scp->atapi_queue);
118 return 0;
119
120failure:
121 if (scp->r_io)
122 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
123 if (scp->r_altio)
124 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
125 if (scp->r_bmio)
126 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
127 if (bootverbose)
128 ata_printf(scp, -1, "probe allocation failed\n");
129 return ENXIO;
130}
131
132int
133ata_attach(device_t dev)
134{
135 struct ata_softc *scp;
136 int error, rid;
137
138 if (!dev)
139 return ENXIO;
140 scp = device_get_softc(dev);
141 if (!scp)
142 return ENXIO;
143
144 rid = ATA_IRQ_RID;
145 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
146 RF_SHAREABLE | RF_ACTIVE);
147 if (!scp->r_irq) {
148 ata_printf(scp, -1, "unable to allocate interrupt\n");
149 return ENXIO;
150 }
151 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO|INTR_ENTROPY,
152 ata_intr, scp, &scp->ih)))
153 return error;
154
155 /*
156 * do not attach devices if we are in early boot, this is done later
157 * when interrupts are enabled by a hook into the boot process.
158 * otherwise attach what the probe has found in scp->devices.
159 */
160 if (!ata_delayed_attach) {
161 if (scp->devices & ATA_ATA_SLAVE)
162 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
163 scp->devices &= ~ATA_ATA_SLAVE;
164 if (scp->devices & ATA_ATAPI_SLAVE)
165 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
166 scp->devices &= ~ATA_ATAPI_SLAVE;
167 if (scp->devices & ATA_ATA_MASTER)
168 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
169 scp->devices &= ~ATA_ATA_MASTER;
170 if (scp->devices & ATA_ATAPI_MASTER)
171 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
172 scp->devices &= ~ATA_ATAPI_MASTER;
173#ifdef DEV_ATADISK
174 if (scp->devices & ATA_ATA_MASTER)
175 ad_attach(scp, ATA_MASTER);
176 if (scp->devices & ATA_ATA_SLAVE)
177 ad_attach(scp, ATA_SLAVE);
178#endif
179#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
180 if (scp->devices & ATA_ATAPI_MASTER)
181 atapi_attach(scp, ATA_MASTER);
182 if (scp->devices & ATA_ATAPI_SLAVE)
183 atapi_attach(scp, ATA_SLAVE);
184#endif
185 }
186 return 0;
187}
188
189int
190ata_detach(device_t dev)
191{
192 struct ata_softc *scp;
193 int s;
194
195 if (!dev)
196 return ENXIO;
197 scp = device_get_softc(dev);
198 if (!scp || !scp->devices)
199 return ENXIO;
200
201 /* make sure channel is not busy SOS XXX */
202 s = splbio();
203 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
204 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
205 splx(s);
206
207 /* disable interrupts on devices */
208 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
209 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
210 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
211 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
212
213#ifdef DEV_ATADISK
214 if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER])
215 ad_detach(scp->dev_softc[MASTER], 1);
216 if (scp->devices & ATA_ATA_SLAVE && scp->dev_softc[SLAVE])
217 ad_detach(scp->dev_softc[SLAVE], 1);
218#endif
219#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
220 if (scp->devices & ATA_ATAPI_MASTER && scp->dev_softc[MASTER])
221 atapi_detach(scp->dev_softc[MASTER]);
222 if (scp->devices & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE])
223 atapi_detach(scp->dev_softc[SLAVE]);
224#endif
225
226 if (scp->dev_param[MASTER]) {
227 free(scp->dev_param[MASTER], M_ATA);
228 scp->dev_param[MASTER] = NULL;
229 }
230 if (scp->dev_param[SLAVE]) {
231 free(scp->dev_param[SLAVE], M_ATA);
232 scp->dev_param[SLAVE] = NULL;
233 }
234 scp->dev_softc[MASTER] = NULL;
235 scp->dev_softc[SLAVE] = NULL;
236 scp->mode[MASTER] = ATA_PIO;
237 scp->mode[SLAVE] = ATA_PIO;
238 scp->devices = 0;
239
240 bus_teardown_intr(dev, scp->r_irq, scp->ih);
241 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, scp->r_irq);
242 if (scp->r_bmio)
243 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
244 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, scp->r_altio);
245 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
91static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
92
93/* misc defines */
94#define MASTER 0
95#define SLAVE 1
96
97int
98ata_probe(device_t dev)
99{
100 struct ata_softc *scp;
101 int rid;
102
103 if (!dev)
104 return ENXIO;
105 scp = device_get_softc(dev);
106 if (!scp || scp->devices)
107 return ENXIO;
108
109 /* initialize the softc basics */
110 scp->active = ATA_IDLE;
111 scp->dev = dev;
112
113 rid = ATA_IOADDR_RID;
114 scp->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
115 ATA_IOSIZE, RF_ACTIVE);
116 if (!scp->r_io)
117 goto failure;
118
119 rid = ATA_ALTADDR_RID;
120 scp->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
121 ATA_ALTIOSIZE, RF_ACTIVE);
122 if (!scp->r_altio)
123 goto failure;
124
125 rid = ATA_BMADDR_RID;
126 scp->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
127 ATA_BMIOSIZE, RF_ACTIVE);
128
129 if (bootverbose)
130 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
131 (int)rman_get_start(scp->r_io),
132 (int)rman_get_start(scp->r_altio),
133 (scp->r_bmio) ? (int)rman_get_start(scp->r_bmio) : 0);
134
135 ata_reset(scp);
136
137 TAILQ_INIT(&scp->ata_queue);
138 TAILQ_INIT(&scp->atapi_queue);
139 return 0;
140
141failure:
142 if (scp->r_io)
143 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
144 if (scp->r_altio)
145 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
146 if (scp->r_bmio)
147 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
148 if (bootverbose)
149 ata_printf(scp, -1, "probe allocation failed\n");
150 return ENXIO;
151}
152
153int
154ata_attach(device_t dev)
155{
156 struct ata_softc *scp;
157 int error, rid;
158
159 if (!dev)
160 return ENXIO;
161 scp = device_get_softc(dev);
162 if (!scp)
163 return ENXIO;
164
165 rid = ATA_IRQ_RID;
166 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
167 RF_SHAREABLE | RF_ACTIVE);
168 if (!scp->r_irq) {
169 ata_printf(scp, -1, "unable to allocate interrupt\n");
170 return ENXIO;
171 }
172 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO|INTR_ENTROPY,
173 ata_intr, scp, &scp->ih)))
174 return error;
175
176 /*
177 * do not attach devices if we are in early boot, this is done later
178 * when interrupts are enabled by a hook into the boot process.
179 * otherwise attach what the probe has found in scp->devices.
180 */
181 if (!ata_delayed_attach) {
182 if (scp->devices & ATA_ATA_SLAVE)
183 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
184 scp->devices &= ~ATA_ATA_SLAVE;
185 if (scp->devices & ATA_ATAPI_SLAVE)
186 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
187 scp->devices &= ~ATA_ATAPI_SLAVE;
188 if (scp->devices & ATA_ATA_MASTER)
189 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
190 scp->devices &= ~ATA_ATA_MASTER;
191 if (scp->devices & ATA_ATAPI_MASTER)
192 if (ata_getparam(scp, ATA_MASTER,ATA_C_ATAPI_IDENTIFY))
193 scp->devices &= ~ATA_ATAPI_MASTER;
194#ifdef DEV_ATADISK
195 if (scp->devices & ATA_ATA_MASTER)
196 ad_attach(scp, ATA_MASTER);
197 if (scp->devices & ATA_ATA_SLAVE)
198 ad_attach(scp, ATA_SLAVE);
199#endif
200#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
201 if (scp->devices & ATA_ATAPI_MASTER)
202 atapi_attach(scp, ATA_MASTER);
203 if (scp->devices & ATA_ATAPI_SLAVE)
204 atapi_attach(scp, ATA_SLAVE);
205#endif
206 }
207 return 0;
208}
209
210int
211ata_detach(device_t dev)
212{
213 struct ata_softc *scp;
214 int s;
215
216 if (!dev)
217 return ENXIO;
218 scp = device_get_softc(dev);
219 if (!scp || !scp->devices)
220 return ENXIO;
221
222 /* make sure channel is not busy SOS XXX */
223 s = splbio();
224 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
225 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
226 splx(s);
227
228 /* disable interrupts on devices */
229 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
230 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
231 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
232 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
233
234#ifdef DEV_ATADISK
235 if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER])
236 ad_detach(scp->dev_softc[MASTER], 1);
237 if (scp->devices & ATA_ATA_SLAVE && scp->dev_softc[SLAVE])
238 ad_detach(scp->dev_softc[SLAVE], 1);
239#endif
240#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
241 if (scp->devices & ATA_ATAPI_MASTER && scp->dev_softc[MASTER])
242 atapi_detach(scp->dev_softc[MASTER]);
243 if (scp->devices & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE])
244 atapi_detach(scp->dev_softc[SLAVE]);
245#endif
246
247 if (scp->dev_param[MASTER]) {
248 free(scp->dev_param[MASTER], M_ATA);
249 scp->dev_param[MASTER] = NULL;
250 }
251 if (scp->dev_param[SLAVE]) {
252 free(scp->dev_param[SLAVE], M_ATA);
253 scp->dev_param[SLAVE] = NULL;
254 }
255 scp->dev_softc[MASTER] = NULL;
256 scp->dev_softc[SLAVE] = NULL;
257 scp->mode[MASTER] = ATA_PIO;
258 scp->mode[SLAVE] = ATA_PIO;
259 scp->devices = 0;
260
261 bus_teardown_intr(dev, scp->r_irq, scp->ih);
262 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, scp->r_irq);
263 if (scp->r_bmio)
264 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
265 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, scp->r_altio);
266 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
267 scp->r_io = NULL;
268 scp->r_altio = NULL;
269 scp->r_bmio = NULL;
270 scp->r_irq = NULL;
246 scp->active = ATA_IDLE;
247 return 0;
248}
249
250int
251ata_resume(device_t dev)
252{
253 struct ata_softc *scp = device_get_softc(dev);
254
255 ata_reinit(scp);
256 return 0;
257}
258
259static int
271 scp->active = ATA_IDLE;
272 return 0;
273}
274
275int
276ata_resume(device_t dev)
277{
278 struct ata_softc *scp = device_get_softc(dev);
279
280 ata_reinit(scp);
281 return 0;
282}
283
284static int
285ataioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
286{
287 int error = 0;
288
289 switch (cmd) {
290 case ATAATTACH: {
291 device_t device = devclass_get_device(ata_devclass, *(int *)addr);
292 /* should enable channel HW on controller that can SOS XXX */
293 if (!device)
294 error = ENXIO;
295 if (!error)
296 error = ata_probe(device);
297 if (!error)
298 error = ata_attach(device);
299 break;
300 }
301
302 case ATADETACH: {
303 device_t device = devclass_get_device(ata_devclass, *(int *)addr);
304 if (!device)
305 error = ENXIO;
306 if (!error)
307 error = ata_detach(device);
308 /* should disable channel HW on controller that can SOS XXX */
309 break;
310 }
311
312 case ATAREINIT: {
313 device_t device = devclass_get_device(ata_devclass, *(int *)addr);
314 struct ata_softc *scp;
315 int s;
316
317 if (!device)
318 return ENXIO;
319 scp = device_get_softc(device);
320 if (!scp)
321 return ENXIO;
322
323 /* make sure channel is not busy SOS XXX */
324 s = splbio();
325 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
326 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
327 splx(s);
328 error = ata_reinit(scp);
329 break;
330 }
331
332 case ATAGMODE: {
333 struct ata_modes *mode = (struct ata_modes *)addr;
334 device_t device = devclass_get_device(ata_devclass, mode->channel);
335 struct ata_softc *scp;
336
337 if (!device)
338 return ENXIO;
339 scp = device_get_softc(device);
340 if (!scp)
341 return ENXIO;
342 mode->mode[MASTER] = scp->mode[MASTER];
343 mode->mode[SLAVE] = scp->mode[SLAVE];
344 break;
345 }
346
347 case ATASMODE: {
348 struct ata_modes *mode = (struct ata_modes *)addr;
349 device_t device = devclass_get_device(ata_devclass, mode->channel);
350 struct ata_softc *scp;
351
352 if (!device)
353 return ENXIO;
354 scp = device_get_softc(device);
355 if (!scp)
356 return ENXIO;
357 if (mode->mode[MASTER] >= 0)
358 ata_change_mode(scp, ATA_MASTER, mode->mode[MASTER]);
359 if (mode->mode[SLAVE] >= 0)
360 ata_change_mode(scp, ATA_SLAVE, mode->mode[SLAVE]);
361 mode->mode[MASTER] = scp->mode[MASTER];
362 mode->mode[SLAVE] = scp->mode[SLAVE];
363 break;
364 }
365
366 case ATAGPARM: {
367 struct ata_param *parm = (struct ata_param *)addr;
368 device_t device = devclass_get_device(ata_devclass, parm->channel);
369 struct ata_softc *scp;
370
371 if (!device)
372 return ENXIO;
373 scp = device_get_softc(device);
374 if (!scp)
375 return ENXIO;
376
377 parm->type[MASTER] =
378 scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
379 parm->type[SLAVE] =
380 scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
381
382 if (scp->dev_name[MASTER])
383 strcpy(parm->name[MASTER], scp->dev_name[MASTER]);
384 if (scp->dev_name[SLAVE])
385 strcpy(parm->name[SLAVE], scp->dev_name[SLAVE]);
386
387 if (scp->dev_param[MASTER])
388 bcopy(scp->dev_param[MASTER], &parm->params[MASTER],
389 sizeof(struct ata_params));
390 if (scp->dev_param[SLAVE])
391 bcopy(scp->dev_param[SLAVE], &parm->params[SLAVE],
392 sizeof(struct ata_params));
393 break;
394 }
395
396 default:
397 error = ENOTTY;
398 }
399 return error;
400}
401
402static int
260ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
261{
262 struct ata_params *ata_parm;
263 int8_t buffer[DEV_BSIZE];
264 int retry = 0;
265
266 /* select drive */
267 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
268 DELAY(1);
269
270 /* enable interrupt */
271 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
272 DELAY(1);
273
274 /* apparently some devices needs this repeated */
275 do {
276 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
277 ata_printf(scp, device, "identify failed\n");
278 return -1;
279 }
280 if (retry++ > 4) {
281 ata_printf(scp, device, "identify retries exceeded\n");
282 return -1;
283 }
284 } while (ata_wait(scp, device,
285 ((command == ATA_C_ATAPI_IDENTIFY) ?
286 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
287
288 ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer,
289 sizeof(buffer)/sizeof(int16_t));
290 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
291 if (!ata_parm) {
292 ata_printf(scp, device, "malloc for identify data failed\n");
293 return -1;
294 }
295 bcopy(buffer, ata_parm, sizeof(struct ata_params));
296 if (command == ATA_C_ATA_IDENTIFY ||
297 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
298 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
299 bswap(ata_parm->model, sizeof(ata_parm->model));
300 btrim(ata_parm->model, sizeof(ata_parm->model));
301 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
302 bswap(ata_parm->revision, sizeof(ata_parm->revision));
303 btrim(ata_parm->revision, sizeof(ata_parm->revision));
304 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
305 scp->dev_param[ATA_DEV(device)] = ata_parm;
306 return 0;
307}
308
309static void
310ata_boot_attach(void)
311{
312 struct ata_softc *scp;
313 int ctlr;
314
315 /*
316 * run through all ata devices and look for real ATA & ATAPI devices
317 * using the hints we found in the early probe, this avoids some of
318 * the delays probing of non-exsistent devices can cause.
319 */
320 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
321 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
322 continue;
323 if (scp->devices & ATA_ATA_SLAVE)
324 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
325 scp->devices &= ~ATA_ATA_SLAVE;
326 if (scp->devices & ATA_ATAPI_SLAVE)
327 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
328 scp->devices &= ~ATA_ATAPI_SLAVE;
329 if (scp->devices & ATA_ATA_MASTER)
330 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
331 scp->devices &= ~ATA_ATA_MASTER;
332 if (scp->devices & ATA_ATAPI_MASTER)
333 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY))
334 scp->devices &= ~ATA_ATAPI_MASTER;
335 }
336
337#ifdef DEV_ATADISK
338 /* now we know whats there, do the real attach, first the ATA disks */
339 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
340 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
341 continue;
342 if (scp->devices & ATA_ATA_MASTER)
343 ad_attach(scp, ATA_MASTER);
344 if (scp->devices & ATA_ATA_SLAVE)
345 ad_attach(scp, ATA_SLAVE);
346 }
347#endif
348#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
349 /* then the atapi devices */
350 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
351 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
352 continue;
353 if (scp->devices & ATA_ATAPI_MASTER)
354 atapi_attach(scp, ATA_MASTER);
355 if (scp->devices & ATA_ATAPI_SLAVE)
356 atapi_attach(scp, ATA_SLAVE);
357 }
358#endif
359 if (ata_delayed_attach) {
360 config_intrhook_disestablish(ata_delayed_attach);
361 free(ata_delayed_attach, M_ATA);
362 ata_delayed_attach = NULL;
363 }
364}
365
366static void
367ata_intr(void *data)
368{
369 struct ata_softc *scp = (struct ata_softc *)data;
370
371 /*
372 * on PCI systems we might share an interrupt line with another
373 * device or our twin ATA channel, so call scp->intr_func to figure
374 * out if it is really an interrupt we should process here
375 */
376 if (scp->intr_func && scp->intr_func(scp))
377 return;
378
379 /* if drive is busy it didn't interrupt */
380 if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY)
381 return;
382
383 /* clear interrupt and get status */
384 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
385
386 if (scp->status & ATA_S_ERROR)
387 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
388
389 /* find & call the responsible driver to process this interrupt */
390 switch (scp->active) {
391#ifdef DEV_ATADISK
392 case ATA_ACTIVE_ATA:
393 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
394 return;
395 break;
396#endif
397#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
398 case ATA_ACTIVE_ATAPI:
399 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
400 return;
401 break;
402#endif
403 case ATA_WAIT_INTR:
404 case ATA_WAIT_INTR | ATA_REINITING:
405 wakeup((caddr_t)scp);
406 break;
407
408 case ATA_WAIT_READY:
409 case ATA_WAIT_READY | ATA_REINITING:
410 break;
411
412 case ATA_IDLE:
413 if (scp->flags & ATA_QUEUED) {
414 scp->active = ATA_ACTIVE; /* XXX */
415 if (ata_service(scp) == ATA_OP_CONTINUES)
416 return;
417 }
418 /* FALLTHROUGH */
419
420 default:
421#ifdef ATA_DEBUG
422 {
423 static int intr_count = 0;
424
425 if (intr_count++ < 10)
426 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n",
427 intr_count, scp->status);
428 }
429#endif
430 }
431 scp->active &= ATA_REINITING;
432 if (scp->active & ATA_REINITING)
433 return;
434 scp->running = NULL;
435 ata_start(scp);
436 return;
437}
438
439void
440ata_start(struct ata_softc *scp)
441{
442#ifdef DEV_ATADISK
443 struct ad_request *ad_request;
444#endif
445#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
446 struct atapi_request *atapi_request;
447#endif
448
449 if (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
450 return;
451
452#ifdef DEV_ATADISK
453 /* find & call the responsible driver if anything on the ATA queue */
454 if (TAILQ_EMPTY(&scp->ata_queue)) {
455 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[MASTER])
456 ad_start((struct ad_softc *)scp->dev_softc[MASTER]);
457 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
458 ad_start((struct ad_softc *)scp->dev_softc[SLAVE]);
459 }
460 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
461 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
462 scp->active = ATA_ACTIVE_ATA;
463 scp->running = ad_request;
464 if (ad_transfer(ad_request) == ATA_OP_CONTINUES)
465 return;
466 }
467
468#endif
469#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
470 /* find & call the responsible driver if anything on the ATAPI queue */
471 if (TAILQ_EMPTY(&scp->atapi_queue)) {
472 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER])
473 atapi_start((struct atapi_softc *)scp->dev_softc[MASTER]);
474 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE])
475 atapi_start((struct atapi_softc *)scp->dev_softc[SLAVE]);
476 }
477 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
478 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
479 scp->active = ATA_ACTIVE_ATAPI;
480 scp->running = atapi_request;
481 atapi_transfer(atapi_request);
482 return;
483 }
484#endif
485 scp->active = ATA_IDLE;
486}
487
488void
489ata_reset(struct ata_softc *scp)
490{
491 u_int8_t lsb, msb, ostat0, ostat1;
492 u_int8_t stat0 = ATA_S_BUSY, stat1 = ATA_S_BUSY;
493 int mask = 0, timeout;
494
495 /* do we have any signs of ATA/ATAPI HW being present ? */
496 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
497 DELAY(10);
498 ostat0 = ATA_INB(scp->r_io, ATA_STATUS);
499 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5)
500 mask |= 0x01;
501 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
502 DELAY(10);
503 ostat1 = ATA_INB(scp->r_io, ATA_STATUS);
504 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5)
505 mask |= 0x02;
506
507 scp->devices = 0;
508 if (!mask)
509 return;
510
511 /* in some setups we dont want to test for a slave */
512 if (scp->flags & ATA_NO_SLAVE)
513 mask &= ~0x02;
514
515 if (bootverbose)
516 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
517 mask, ostat0, ostat1);
518
519 /* reset channel */
520 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
521 DELAY(10000);
522 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS);
523 DELAY(100000);
524 ATA_INB(scp->r_io, ATA_ERROR);
525
526 /* wait for BUSY to go inactive */
527 for (timeout = 0; timeout < 310000; timeout++) {
528 if (stat0 & ATA_S_BUSY) {
529 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
530 DELAY(10);
531 stat0 = ATA_INB(scp->r_io, ATA_STATUS);
532 if (!(stat0 & ATA_S_BUSY)) {
533 /* check for ATAPI signature while its still there */
534 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB);
535 msb = ATA_INB(scp->r_io, ATA_CYL_MSB);
536 if (bootverbose)
537 ata_printf(scp, ATA_MASTER,
538 "ATAPI probe %02x %02x\n", lsb, msb);
539 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
540 scp->devices |= ATA_ATAPI_MASTER;
541 }
542 }
543 if (stat1 & ATA_S_BUSY) {
544 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
545 DELAY(10);
546 stat1 = ATA_INB(scp->r_io, ATA_STATUS);
547 if (!(stat1 & ATA_S_BUSY)) {
548 /* check for ATAPI signature while its still there */
549 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB);
550 msb = ATA_INB(scp->r_io, ATA_CYL_MSB);
551 if (bootverbose)
552 ata_printf(scp, ATA_SLAVE,
553 "ATAPI probe %02x %02x\n", lsb, msb);
554 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
555 scp->devices |= ATA_ATAPI_SLAVE;
556 }
557 }
558 if (mask == 0x01) /* wait for master only */
559 if (!(stat0 & ATA_S_BUSY))
560 break;
561 if (mask == 0x02) /* wait for slave only */
562 if (!(stat1 & ATA_S_BUSY))
563 break;
564 if (mask == 0x03) /* wait for both master & slave */
565 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
566 break;
567 DELAY(100);
568 }
569 DELAY(10);
570 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
571
572 if (stat0 & ATA_S_BUSY)
573 mask &= ~0x01;
574 if (stat1 & ATA_S_BUSY)
575 mask &= ~0x02;
576 if (bootverbose)
577 ata_printf(scp, -1, "mask=%02x stat0=%02x stat1=%02x\n",
578 mask, stat0, stat1);
579 if (!mask)
580 return;
581
582 if (mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
583 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
584 DELAY(10);
585 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
586 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
587 lsb = ATA_INB(scp->r_io, ATA_ERROR);
588 msb = ATA_INB(scp->r_io, ATA_CYL_LSB);
589 if (bootverbose)
590 ata_printf(scp, ATA_MASTER, "ATA probe %02x %02x\n", lsb, msb);
591 if (lsb != 0x58 && msb == 0xa5)
592 scp->devices |= ATA_ATA_MASTER;
593 }
594 if (mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
595 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
596 DELAY(10);
597 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
598 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
599 lsb = ATA_INB(scp->r_io, ATA_ERROR);
600 msb = ATA_INB(scp->r_io, ATA_CYL_LSB);
601 if (bootverbose)
602 ata_printf(scp, ATA_SLAVE, "ATA probe %02x %02x\n", lsb, msb);
603 if (lsb != 0x58 && msb == 0xa5)
604 scp->devices |= ATA_ATA_SLAVE;
605 }
606 if (bootverbose)
607 ata_printf(scp, -1, "devices=%02x\n", scp->devices);
608}
609
610int
611ata_reinit(struct ata_softc *scp)
612{
613 int devices, misdev, newdev;
614
403ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
404{
405 struct ata_params *ata_parm;
406 int8_t buffer[DEV_BSIZE];
407 int retry = 0;
408
409 /* select drive */
410 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
411 DELAY(1);
412
413 /* enable interrupt */
414 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
415 DELAY(1);
416
417 /* apparently some devices needs this repeated */
418 do {
419 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
420 ata_printf(scp, device, "identify failed\n");
421 return -1;
422 }
423 if (retry++ > 4) {
424 ata_printf(scp, device, "identify retries exceeded\n");
425 return -1;
426 }
427 } while (ata_wait(scp, device,
428 ((command == ATA_C_ATAPI_IDENTIFY) ?
429 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
430
431 ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer,
432 sizeof(buffer)/sizeof(int16_t));
433 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
434 if (!ata_parm) {
435 ata_printf(scp, device, "malloc for identify data failed\n");
436 return -1;
437 }
438 bcopy(buffer, ata_parm, sizeof(struct ata_params));
439 if (command == ATA_C_ATA_IDENTIFY ||
440 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
441 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X')))
442 bswap(ata_parm->model, sizeof(ata_parm->model));
443 btrim(ata_parm->model, sizeof(ata_parm->model));
444 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
445 bswap(ata_parm->revision, sizeof(ata_parm->revision));
446 btrim(ata_parm->revision, sizeof(ata_parm->revision));
447 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
448 scp->dev_param[ATA_DEV(device)] = ata_parm;
449 return 0;
450}
451
452static void
453ata_boot_attach(void)
454{
455 struct ata_softc *scp;
456 int ctlr;
457
458 /*
459 * run through all ata devices and look for real ATA & ATAPI devices
460 * using the hints we found in the early probe, this avoids some of
461 * the delays probing of non-exsistent devices can cause.
462 */
463 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
464 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
465 continue;
466 if (scp->devices & ATA_ATA_SLAVE)
467 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
468 scp->devices &= ~ATA_ATA_SLAVE;
469 if (scp->devices & ATA_ATAPI_SLAVE)
470 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
471 scp->devices &= ~ATA_ATAPI_SLAVE;
472 if (scp->devices & ATA_ATA_MASTER)
473 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
474 scp->devices &= ~ATA_ATA_MASTER;
475 if (scp->devices & ATA_ATAPI_MASTER)
476 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY))
477 scp->devices &= ~ATA_ATAPI_MASTER;
478 }
479
480#ifdef DEV_ATADISK
481 /* now we know whats there, do the real attach, first the ATA disks */
482 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
483 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
484 continue;
485 if (scp->devices & ATA_ATA_MASTER)
486 ad_attach(scp, ATA_MASTER);
487 if (scp->devices & ATA_ATA_SLAVE)
488 ad_attach(scp, ATA_SLAVE);
489 }
490#endif
491#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
492 /* then the atapi devices */
493 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
494 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
495 continue;
496 if (scp->devices & ATA_ATAPI_MASTER)
497 atapi_attach(scp, ATA_MASTER);
498 if (scp->devices & ATA_ATAPI_SLAVE)
499 atapi_attach(scp, ATA_SLAVE);
500 }
501#endif
502 if (ata_delayed_attach) {
503 config_intrhook_disestablish(ata_delayed_attach);
504 free(ata_delayed_attach, M_ATA);
505 ata_delayed_attach = NULL;
506 }
507}
508
509static void
510ata_intr(void *data)
511{
512 struct ata_softc *scp = (struct ata_softc *)data;
513
514 /*
515 * on PCI systems we might share an interrupt line with another
516 * device or our twin ATA channel, so call scp->intr_func to figure
517 * out if it is really an interrupt we should process here
518 */
519 if (scp->intr_func && scp->intr_func(scp))
520 return;
521
522 /* if drive is busy it didn't interrupt */
523 if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY)
524 return;
525
526 /* clear interrupt and get status */
527 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
528
529 if (scp->status & ATA_S_ERROR)
530 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
531
532 /* find & call the responsible driver to process this interrupt */
533 switch (scp->active) {
534#ifdef DEV_ATADISK
535 case ATA_ACTIVE_ATA:
536 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
537 return;
538 break;
539#endif
540#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
541 case ATA_ACTIVE_ATAPI:
542 if (!scp->running || atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
543 return;
544 break;
545#endif
546 case ATA_WAIT_INTR:
547 case ATA_WAIT_INTR | ATA_REINITING:
548 wakeup((caddr_t)scp);
549 break;
550
551 case ATA_WAIT_READY:
552 case ATA_WAIT_READY | ATA_REINITING:
553 break;
554
555 case ATA_IDLE:
556 if (scp->flags & ATA_QUEUED) {
557 scp->active = ATA_ACTIVE; /* XXX */
558 if (ata_service(scp) == ATA_OP_CONTINUES)
559 return;
560 }
561 /* FALLTHROUGH */
562
563 default:
564#ifdef ATA_DEBUG
565 {
566 static int intr_count = 0;
567
568 if (intr_count++ < 10)
569 ata_printf(scp, -1, "unwanted interrupt %d status = %02x\n",
570 intr_count, scp->status);
571 }
572#endif
573 }
574 scp->active &= ATA_REINITING;
575 if (scp->active & ATA_REINITING)
576 return;
577 scp->running = NULL;
578 ata_start(scp);
579 return;
580}
581
582void
583ata_start(struct ata_softc *scp)
584{
585#ifdef DEV_ATADISK
586 struct ad_request *ad_request;
587#endif
588#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
589 struct atapi_request *atapi_request;
590#endif
591
592 if (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
593 return;
594
595#ifdef DEV_ATADISK
596 /* find & call the responsible driver if anything on the ATA queue */
597 if (TAILQ_EMPTY(&scp->ata_queue)) {
598 if (scp->devices & (ATA_ATA_MASTER) && scp->dev_softc[MASTER])
599 ad_start((struct ad_softc *)scp->dev_softc[MASTER]);
600 if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
601 ad_start((struct ad_softc *)scp->dev_softc[SLAVE]);
602 }
603 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
604 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
605 scp->active = ATA_ACTIVE_ATA;
606 scp->running = ad_request;
607 if (ad_transfer(ad_request) == ATA_OP_CONTINUES)
608 return;
609 }
610
611#endif
612#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
613 /* find & call the responsible driver if anything on the ATAPI queue */
614 if (TAILQ_EMPTY(&scp->atapi_queue)) {
615 if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER])
616 atapi_start((struct atapi_softc *)scp->dev_softc[MASTER]);
617 if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE])
618 atapi_start((struct atapi_softc *)scp->dev_softc[SLAVE]);
619 }
620 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
621 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
622 scp->active = ATA_ACTIVE_ATAPI;
623 scp->running = atapi_request;
624 atapi_transfer(atapi_request);
625 return;
626 }
627#endif
628 scp->active = ATA_IDLE;
629}
630
631void
632ata_reset(struct ata_softc *scp)
633{
634 u_int8_t lsb, msb, ostat0, ostat1;
635 u_int8_t stat0 = ATA_S_BUSY, stat1 = ATA_S_BUSY;
636 int mask = 0, timeout;
637
638 /* do we have any signs of ATA/ATAPI HW being present ? */
639 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
640 DELAY(10);
641 ostat0 = ATA_INB(scp->r_io, ATA_STATUS);
642 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5)
643 mask |= 0x01;
644 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
645 DELAY(10);
646 ostat1 = ATA_INB(scp->r_io, ATA_STATUS);
647 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5)
648 mask |= 0x02;
649
650 scp->devices = 0;
651 if (!mask)
652 return;
653
654 /* in some setups we dont want to test for a slave */
655 if (scp->flags & ATA_NO_SLAVE)
656 mask &= ~0x02;
657
658 if (bootverbose)
659 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
660 mask, ostat0, ostat1);
661
662 /* reset channel */
663 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
664 DELAY(10000);
665 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS);
666 DELAY(100000);
667 ATA_INB(scp->r_io, ATA_ERROR);
668
669 /* wait for BUSY to go inactive */
670 for (timeout = 0; timeout < 310000; timeout++) {
671 if (stat0 & ATA_S_BUSY) {
672 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
673 DELAY(10);
674 stat0 = ATA_INB(scp->r_io, ATA_STATUS);
675 if (!(stat0 & ATA_S_BUSY)) {
676 /* check for ATAPI signature while its still there */
677 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB);
678 msb = ATA_INB(scp->r_io, ATA_CYL_MSB);
679 if (bootverbose)
680 ata_printf(scp, ATA_MASTER,
681 "ATAPI probe %02x %02x\n", lsb, msb);
682 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
683 scp->devices |= ATA_ATAPI_MASTER;
684 }
685 }
686 if (stat1 & ATA_S_BUSY) {
687 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
688 DELAY(10);
689 stat1 = ATA_INB(scp->r_io, ATA_STATUS);
690 if (!(stat1 & ATA_S_BUSY)) {
691 /* check for ATAPI signature while its still there */
692 lsb = ATA_INB(scp->r_io, ATA_CYL_LSB);
693 msb = ATA_INB(scp->r_io, ATA_CYL_MSB);
694 if (bootverbose)
695 ata_printf(scp, ATA_SLAVE,
696 "ATAPI probe %02x %02x\n", lsb, msb);
697 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
698 scp->devices |= ATA_ATAPI_SLAVE;
699 }
700 }
701 if (mask == 0x01) /* wait for master only */
702 if (!(stat0 & ATA_S_BUSY))
703 break;
704 if (mask == 0x02) /* wait for slave only */
705 if (!(stat1 & ATA_S_BUSY))
706 break;
707 if (mask == 0x03) /* wait for both master & slave */
708 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
709 break;
710 DELAY(100);
711 }
712 DELAY(10);
713 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
714
715 if (stat0 & ATA_S_BUSY)
716 mask &= ~0x01;
717 if (stat1 & ATA_S_BUSY)
718 mask &= ~0x02;
719 if (bootverbose)
720 ata_printf(scp, -1, "mask=%02x stat0=%02x stat1=%02x\n",
721 mask, stat0, stat1);
722 if (!mask)
723 return;
724
725 if (mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
726 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
727 DELAY(10);
728 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
729 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
730 lsb = ATA_INB(scp->r_io, ATA_ERROR);
731 msb = ATA_INB(scp->r_io, ATA_CYL_LSB);
732 if (bootverbose)
733 ata_printf(scp, ATA_MASTER, "ATA probe %02x %02x\n", lsb, msb);
734 if (lsb != 0x58 && msb == 0xa5)
735 scp->devices |= ATA_ATA_MASTER;
736 }
737 if (mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
738 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
739 DELAY(10);
740 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
741 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
742 lsb = ATA_INB(scp->r_io, ATA_ERROR);
743 msb = ATA_INB(scp->r_io, ATA_CYL_LSB);
744 if (bootverbose)
745 ata_printf(scp, ATA_SLAVE, "ATA probe %02x %02x\n", lsb, msb);
746 if (lsb != 0x58 && msb == 0xa5)
747 scp->devices |= ATA_ATA_SLAVE;
748 }
749 if (bootverbose)
750 ata_printf(scp, -1, "devices=%02x\n", scp->devices);
751}
752
753int
754ata_reinit(struct ata_softc *scp)
755{
756 int devices, misdev, newdev;
757
758 if (!scp->r_io || !scp->r_altio || !scp->r_irq)
759 return ENXIO;
615 scp->active = ATA_REINITING;
616 scp->running = NULL;
617 devices = scp->devices;
618 ata_printf(scp, -1, "resetting devices .. ");
619 ata_reset(scp);
620
621 if ((misdev = devices & ~scp->devices)) {
760 scp->active = ATA_REINITING;
761 scp->running = NULL;
762 devices = scp->devices;
763 ata_printf(scp, -1, "resetting devices .. ");
764 ata_reset(scp);
765
766 if ((misdev = devices & ~scp->devices)) {
767 if (misdev)
768 printf("\n");
622#ifdef DEV_ATADISK
623 if (misdev & ATA_ATA_MASTER && scp->dev_softc[MASTER])
624 ad_detach(scp->dev_softc[MASTER], 0);
625 if (misdev & ATA_ATA_SLAVE && scp->dev_softc[SLAVE])
626 ad_detach(scp->dev_softc[SLAVE], 0);
627#endif
628#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
629 if (misdev & ATA_ATAPI_MASTER && scp->dev_softc[MASTER])
630 atapi_detach(scp->dev_softc[MASTER]);
631 if (misdev & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE])
632 atapi_detach(scp->dev_softc[SLAVE]);
633#endif
634 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
635 free(scp->dev_param[MASTER], M_ATA);
636 scp->dev_param[MASTER] = NULL;
637 }
638 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
639 free(scp->dev_param[SLAVE], M_ATA);
640 scp->dev_param[SLAVE] = NULL;
641 }
642 }
643 if ((newdev = ~devices & scp->devices)) {
644 if (newdev & ATA_ATA_MASTER)
645 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
646 newdev &= ~ATA_ATA_MASTER;
647 if (newdev & ATA_ATA_SLAVE)
648 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
649 newdev &= ~ATA_ATA_SLAVE;
650 if (newdev & ATA_ATAPI_MASTER)
651 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY))
652 newdev &= ~ATA_ATAPI_MASTER;
653 if (newdev & ATA_ATAPI_SLAVE)
654 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
655 newdev &= ~ATA_ATAPI_SLAVE;
769#ifdef DEV_ATADISK
770 if (misdev & ATA_ATA_MASTER && scp->dev_softc[MASTER])
771 ad_detach(scp->dev_softc[MASTER], 0);
772 if (misdev & ATA_ATA_SLAVE && scp->dev_softc[SLAVE])
773 ad_detach(scp->dev_softc[SLAVE], 0);
774#endif
775#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
776 if (misdev & ATA_ATAPI_MASTER && scp->dev_softc[MASTER])
777 atapi_detach(scp->dev_softc[MASTER]);
778 if (misdev & ATA_ATAPI_SLAVE && scp->dev_softc[SLAVE])
779 atapi_detach(scp->dev_softc[SLAVE]);
780#endif
781 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
782 free(scp->dev_param[MASTER], M_ATA);
783 scp->dev_param[MASTER] = NULL;
784 }
785 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
786 free(scp->dev_param[SLAVE], M_ATA);
787 scp->dev_param[SLAVE] = NULL;
788 }
789 }
790 if ((newdev = ~devices & scp->devices)) {
791 if (newdev & ATA_ATA_MASTER)
792 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATA_IDENTIFY))
793 newdev &= ~ATA_ATA_MASTER;
794 if (newdev & ATA_ATA_SLAVE)
795 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATA_IDENTIFY))
796 newdev &= ~ATA_ATA_SLAVE;
797 if (newdev & ATA_ATAPI_MASTER)
798 if (ata_getparam(scp, ATA_MASTER, ATA_C_ATAPI_IDENTIFY))
799 newdev &= ~ATA_ATAPI_MASTER;
800 if (newdev & ATA_ATAPI_SLAVE)
801 if (ata_getparam(scp, ATA_SLAVE, ATA_C_ATAPI_IDENTIFY))
802 newdev &= ~ATA_ATAPI_SLAVE;
656 if (newdev)
657 printf("\n");
658 }
659 scp->active = ATA_IDLE;
803 }
804 scp->active = ATA_IDLE;
805 if (!misdev && newdev)
806 printf("\n");
660#ifdef DEV_ATADISK
661 if (newdev & ATA_ATA_MASTER && !scp->dev_softc[MASTER])
662 ad_attach(scp, ATA_MASTER);
663 else if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER])
664 ad_reinit((struct ad_softc *)scp->dev_softc[MASTER]);
665 if (newdev & ATA_ATA_SLAVE && !scp->dev_softc[SLAVE])
666 ad_attach(scp, ATA_SLAVE);
667 else if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
668 ad_reinit((struct ad_softc *)scp->dev_softc[SLAVE]);
669#endif
670#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
671 if (newdev & ATA_ATAPI_MASTER && !scp->dev_softc[MASTER])
672 atapi_attach(scp, ATA_MASTER);
673 else if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER])
674 atapi_reinit((struct atapi_softc *)scp->dev_softc[MASTER]);
675 if (newdev & ATA_ATAPI_SLAVE && !scp->dev_softc[SLAVE])
676 atapi_attach(scp, ATA_SLAVE);
677 else if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE])
678 atapi_reinit((struct atapi_softc *)scp->dev_softc[SLAVE]);
679#endif
680 printf("done\n");
681 ata_start(scp);
682 return 0;
683}
684
685static int
686ata_service(struct ata_softc *scp)
687{
688 /* do we have a SERVICE request from the drive ? */
689 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
690 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
691 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
692#ifdef DEV_ATADISK
693 if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
694 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[MASTER])
695 return ad_service((struct ad_softc *)scp->dev_softc[MASTER], 0);
696 }
697 else {
698 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
699 return ad_service((struct ad_softc *)scp->dev_softc[SLAVE], 0);
700 }
701#endif
702 }
703 return ATA_OP_FINISHED;
704}
705
706int
707ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
708{
709 int timeout = 0;
710
711 DELAY(1);
712 while (timeout < 5000000) { /* timeout 5 secs */
713 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
714
715 /* if drive fails status, reselect the drive just to be sure */
716 if (scp->status == 0xff) {
717 ata_printf(scp, device, "no status, reselecting device\n");
718 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
719 DELAY(10);
720 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
721 if (scp->status == 0xff)
722 return -1;
723 }
724
725 /* are we done ? */
726 if (!(scp->status & ATA_S_BUSY))
727 break;
728
729 if (timeout > 1000) {
730 timeout += 1000;
731 DELAY(1000);
732 }
733 else {
734 timeout += 10;
735 DELAY(10);
736 }
737 }
738 if (scp->status & ATA_S_ERROR)
739 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
740 if (timeout >= 5000000)
741 return -1;
742 if (!mask)
743 return (scp->status & ATA_S_ERROR);
744
745 /* Wait 50 msec for bits wanted. */
746 timeout = 5000;
747 while (timeout--) {
748 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
749 if ((scp->status & mask) == mask) {
750 if (scp->status & ATA_S_ERROR)
751 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
752 return (scp->status & ATA_S_ERROR);
753 }
754 DELAY (10);
755 }
756 return -1;
757}
758
759int
760ata_command(struct ata_softc *scp, int device, u_int8_t command,
761 u_int16_t cylinder, u_int8_t head, u_int8_t sector,
762 u_int8_t count, u_int8_t feature, int flags)
763{
764 int error = 0;
765#ifdef ATA_DEBUG
766 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
767 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
768 rman_get_start(scp->r_io), command, cylinder, head, sector,
769 count, feature, flags);
770#endif
771
772 /* disable interrupt from device */
773 if (scp->flags & ATA_QUEUED)
774 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
775
776 /* select device */
777 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
778
779 /* ready to issue command ? */
780 if (ata_wait(scp, device, 0) < 0) {
781 ata_printf(scp, device,
782 "timeout waiting to give command=%02x s=%02x e=%02x\n",
783 command, scp->status, scp->error);
784 return -1;
785 }
786
787 ATA_OUTB(scp->r_io, ATA_FEATURE, feature);
788 ATA_OUTB(scp->r_io, ATA_COUNT, count);
789 ATA_OUTB(scp->r_io, ATA_SECTOR, sector);
790 ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8);
791 ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder);
792 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head);
793
794 switch (flags) {
795 case ATA_WAIT_INTR:
796 scp->active |= ATA_WAIT_INTR;
797 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
798 ATA_OUTB(scp->r_io, ATA_CMD, command);
799
800 /* enable interrupt */
801 if (scp->flags & ATA_QUEUED)
802 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
803
804 if (await(PRIBIO, 10 * hz)) {
805 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
806 scp->active &= ~ATA_WAIT_INTR;
807 error = -1;
808 }
809 break;
810
811 case ATA_WAIT_READY:
812 scp->active |= ATA_WAIT_READY;
813 ATA_OUTB(scp->r_io, ATA_CMD, command);
814 if (ata_wait(scp, device, ATA_S_READY) < 0) {
815 ata_printf(scp, device,
816 "timeout waiting for command=%02x s=%02x e=%02x\n",
817 command, scp->status, scp->error);
818 error = -1;
819 }
820 scp->active &= ~ATA_WAIT_READY;
821 break;
822
823 case ATA_IMMEDIATE:
824 ATA_OUTB(scp->r_io, ATA_CMD, command);
825 break;
826
827 default:
828 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
829 active2str(flags));
830 }
831 /* enable interrupt */
832 if (scp->flags & ATA_QUEUED)
833 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
834 return error;
835}
836
807#ifdef DEV_ATADISK
808 if (newdev & ATA_ATA_MASTER && !scp->dev_softc[MASTER])
809 ad_attach(scp, ATA_MASTER);
810 else if (scp->devices & ATA_ATA_MASTER && scp->dev_softc[MASTER])
811 ad_reinit((struct ad_softc *)scp->dev_softc[MASTER]);
812 if (newdev & ATA_ATA_SLAVE && !scp->dev_softc[SLAVE])
813 ad_attach(scp, ATA_SLAVE);
814 else if (scp->devices & (ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
815 ad_reinit((struct ad_softc *)scp->dev_softc[SLAVE]);
816#endif
817#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
818 if (newdev & ATA_ATAPI_MASTER && !scp->dev_softc[MASTER])
819 atapi_attach(scp, ATA_MASTER);
820 else if (scp->devices & (ATA_ATAPI_MASTER) && scp->dev_softc[MASTER])
821 atapi_reinit((struct atapi_softc *)scp->dev_softc[MASTER]);
822 if (newdev & ATA_ATAPI_SLAVE && !scp->dev_softc[SLAVE])
823 atapi_attach(scp, ATA_SLAVE);
824 else if (scp->devices & (ATA_ATAPI_SLAVE) && scp->dev_softc[SLAVE])
825 atapi_reinit((struct atapi_softc *)scp->dev_softc[SLAVE]);
826#endif
827 printf("done\n");
828 ata_start(scp);
829 return 0;
830}
831
832static int
833ata_service(struct ata_softc *scp)
834{
835 /* do we have a SERVICE request from the drive ? */
836 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
837 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
838 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
839#ifdef DEV_ATADISK
840 if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
841 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[MASTER])
842 return ad_service((struct ad_softc *)scp->dev_softc[MASTER], 0);
843 }
844 else {
845 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[SLAVE])
846 return ad_service((struct ad_softc *)scp->dev_softc[SLAVE], 0);
847 }
848#endif
849 }
850 return ATA_OP_FINISHED;
851}
852
853int
854ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
855{
856 int timeout = 0;
857
858 DELAY(1);
859 while (timeout < 5000000) { /* timeout 5 secs */
860 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
861
862 /* if drive fails status, reselect the drive just to be sure */
863 if (scp->status == 0xff) {
864 ata_printf(scp, device, "no status, reselecting device\n");
865 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
866 DELAY(10);
867 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
868 if (scp->status == 0xff)
869 return -1;
870 }
871
872 /* are we done ? */
873 if (!(scp->status & ATA_S_BUSY))
874 break;
875
876 if (timeout > 1000) {
877 timeout += 1000;
878 DELAY(1000);
879 }
880 else {
881 timeout += 10;
882 DELAY(10);
883 }
884 }
885 if (scp->status & ATA_S_ERROR)
886 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
887 if (timeout >= 5000000)
888 return -1;
889 if (!mask)
890 return (scp->status & ATA_S_ERROR);
891
892 /* Wait 50 msec for bits wanted. */
893 timeout = 5000;
894 while (timeout--) {
895 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
896 if ((scp->status & mask) == mask) {
897 if (scp->status & ATA_S_ERROR)
898 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
899 return (scp->status & ATA_S_ERROR);
900 }
901 DELAY (10);
902 }
903 return -1;
904}
905
906int
907ata_command(struct ata_softc *scp, int device, u_int8_t command,
908 u_int16_t cylinder, u_int8_t head, u_int8_t sector,
909 u_int8_t count, u_int8_t feature, int flags)
910{
911 int error = 0;
912#ifdef ATA_DEBUG
913 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
914 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
915 rman_get_start(scp->r_io), command, cylinder, head, sector,
916 count, feature, flags);
917#endif
918
919 /* disable interrupt from device */
920 if (scp->flags & ATA_QUEUED)
921 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
922
923 /* select device */
924 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
925
926 /* ready to issue command ? */
927 if (ata_wait(scp, device, 0) < 0) {
928 ata_printf(scp, device,
929 "timeout waiting to give command=%02x s=%02x e=%02x\n",
930 command, scp->status, scp->error);
931 return -1;
932 }
933
934 ATA_OUTB(scp->r_io, ATA_FEATURE, feature);
935 ATA_OUTB(scp->r_io, ATA_COUNT, count);
936 ATA_OUTB(scp->r_io, ATA_SECTOR, sector);
937 ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8);
938 ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder);
939 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head);
940
941 switch (flags) {
942 case ATA_WAIT_INTR:
943 scp->active |= ATA_WAIT_INTR;
944 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
945 ATA_OUTB(scp->r_io, ATA_CMD, command);
946
947 /* enable interrupt */
948 if (scp->flags & ATA_QUEUED)
949 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
950
951 if (await(PRIBIO, 10 * hz)) {
952 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
953 scp->active &= ~ATA_WAIT_INTR;
954 error = -1;
955 }
956 break;
957
958 case ATA_WAIT_READY:
959 scp->active |= ATA_WAIT_READY;
960 ATA_OUTB(scp->r_io, ATA_CMD, command);
961 if (ata_wait(scp, device, ATA_S_READY) < 0) {
962 ata_printf(scp, device,
963 "timeout waiting for command=%02x s=%02x e=%02x\n",
964 command, scp->status, scp->error);
965 error = -1;
966 }
967 scp->active &= ~ATA_WAIT_READY;
968 break;
969
970 case ATA_IMMEDIATE:
971 ATA_OUTB(scp->r_io, ATA_CMD, command);
972 break;
973
974 default:
975 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
976 active2str(flags));
977 }
978 /* enable interrupt */
979 if (scp->flags & ATA_QUEUED)
980 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
981 return error;
982}
983
984void
985ata_set_name(struct ata_softc *scp, int device, char *name)
986{
987 scp->dev_name[ATA_DEV(device)] = malloc(strlen(name) + 1, M_ATA, M_NOWAIT);
988 if (scp->dev_name[ATA_DEV(device)])
989 strcpy(scp->dev_name[ATA_DEV(device)], name);
990}
991
992void
993ata_free_name(struct ata_softc *scp, int device)
994{
995 if (scp->dev_name[ATA_DEV(device)])
996 free(scp->dev_name[ATA_DEV(device)], M_ATA);
997}
998
837int
838ata_get_lun(u_int32_t *map)
839{
840 int lun = ffs(~*map) - 1;
841
842 *map |= (1 << lun);
843 return lun;
844}
845
846int
847ata_test_lun(u_int32_t *map, int lun)
848{
849 return (*map & (1 << lun));
850}
851
852void
853ata_free_lun(u_int32_t *map, int lun)
854{
855 *map &= ~(1 << lun);
856}
857
858int
859ata_printf(struct ata_softc *scp, int device, const char * fmt, ...)
860{
861 va_list ap;
862 int ret;
863
864 if (device == -1)
865 ret = printf("ata%d: ", device_get_unit(scp->dev));
999int
1000ata_get_lun(u_int32_t *map)
1001{
1002 int lun = ffs(~*map) - 1;
1003
1004 *map |= (1 << lun);
1005 return lun;
1006}
1007
1008int
1009ata_test_lun(u_int32_t *map, int lun)
1010{
1011 return (*map & (1 << lun));
1012}
1013
1014void
1015ata_free_lun(u_int32_t *map, int lun)
1016{
1017 *map &= ~(1 << lun);
1018}
1019
1020int
1021ata_printf(struct ata_softc *scp, int device, const char * fmt, ...)
1022{
1023 va_list ap;
1024 int ret;
1025
1026 if (device == -1)
1027 ret = printf("ata%d: ", device_get_unit(scp->dev));
866 else
867 ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
868 (device == ATA_MASTER) ? "master" : "slave");
1028 else {
1029 if (scp->dev_name[ATA_DEV(device)])
1030 ret = printf("%s: ", scp->dev_name[ATA_DEV(device)]);
1031 else
1032 ret = printf("ata%d-%s: ", device_get_unit(scp->dev),
1033 (device == ATA_MASTER) ? "master" : "slave");
1034 }
869 va_start(ap, fmt);
870 ret += vprintf(fmt, ap);
871 va_end(ap);
872 return ret;
873}
874
875char *
876ata_mode2str(int mode)
877{
878 switch (mode) {
879 case ATA_PIO: return "BIOSPIO";
880 case ATA_PIO0: return "PIO0";
881 case ATA_PIO1: return "PIO1";
882 case ATA_PIO2: return "PIO2";
883 case ATA_PIO3: return "PIO3";
884 case ATA_PIO4: return "PIO4";
885 case ATA_WDMA2: return "WDMA2";
886 case ATA_UDMA2: return "UDMA33";
887 case ATA_UDMA4: return "UDMA66";
888 case ATA_UDMA5: return "UDMA100";
889 case ATA_DMA: return "BIOSDMA";
890 default: return "???";
891 }
892}
893
894int
895ata_pio2mode(int pio)
896{
897 switch (pio) {
898 default:
899 case 0: return ATA_PIO0;
900 case 1: return ATA_PIO1;
901 case 2: return ATA_PIO2;
902 case 3: return ATA_PIO3;
903 case 4: return ATA_PIO4;
904 }
905}
906
907int
908ata_pmode(struct ata_params *ap)
909{
910 if (ap->atavalid & ATA_FLAG_64_70) {
911 if (ap->apiomodes & 2)
912 return 4;
913 if (ap->apiomodes & 1)
914 return 3;
915 }
916 if (ap->opiomode == 2)
917 return 2;
918 if (ap->opiomode == 1)
919 return 1;
920 if (ap->opiomode == 0)
921 return 0;
922 return -1;
923}
924
925int
926ata_wmode(struct ata_params *ap)
927{
928 if (ap->wdmamodes & 4)
929 return 2;
930 if (ap->wdmamodes & 2)
931 return 1;
932 if (ap->wdmamodes & 1)
933 return 0;
934 return -1;
935}
936
937int
938ata_umode(struct ata_params *ap)
939{
940 if (ap->atavalid & ATA_FLAG_88) {
941 if (ap->udmamodes & 0x20)
942 return 5;
943 if (ap->udmamodes & 0x10)
944 return 4;
945 if (ap->udmamodes & 0x08)
946 return 3;
947 if (ap->udmamodes & 0x04)
948 return 2;
949 if (ap->udmamodes & 0x02)
950 return 1;
951 if (ap->udmamodes & 0x01)
952 return 0;
953 }
954 return -1;
955}
956
957static char *
958active2str(int active)
959{
960 static char buf[8];
961
962 switch (active) {
963 case ATA_IDLE:
964 return("ATA_IDLE");
965 case ATA_IMMEDIATE:
966 return("ATA_IMMEDIATE");
967 case ATA_WAIT_INTR:
968 return("ATA_WAIT_INTR");
969 case ATA_WAIT_READY:
970 return("ATA_WAIT_READY");
971 case ATA_ACTIVE:
972 return("ATA_ACTIVE");
973 case ATA_ACTIVE_ATA:
974 return("ATA_ACTIVE_ATA");
975 case ATA_ACTIVE_ATAPI:
976 return("ATA_ACTIVE_ATAPI");
977 case ATA_REINITING:
978 return("ATA_REINITING");
979 default:
980 sprintf(buf, "0x%02x", active);
981 return buf;
982 }
983}
984
985static void
986bswap(int8_t *buf, int len)
987{
988 u_int16_t *ptr = (u_int16_t*)(buf + len);
989
990 while (--ptr >= (u_int16_t*)buf)
991 *ptr = ntohs(*ptr);
992}
993
994static void
995btrim(int8_t *buf, int len)
996{
997 int8_t *ptr;
998
999 for (ptr = buf; ptr < buf+len; ++ptr)
1000 if (!*ptr)
1001 *ptr = ' ';
1002 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1003 *ptr = 0;
1004}
1005
1006static void
1007bpack(int8_t *src, int8_t *dst, int len)
1008{
1009 int i, j, blank;
1010
1011 for (i = j = blank = 0 ; i < len; i++) {
1012 if (blank && src[i] == ' ') continue;
1013 if (blank && src[i] != ' ') {
1014 dst[j++] = src[i];
1015 blank = 0;
1016 continue;
1017 }
1018 if (src[i] == ' ') {
1019 blank = 1;
1020 if (i == 0)
1021 continue;
1022 }
1023 dst[j++] = src[i];
1024 }
1025 if (j < len)
1026 dst[j] = 0x00;
1027}
1028
1029static void
1030ata_change_mode(struct ata_softc *scp, int device, int mode)
1031{
1035 va_start(ap, fmt);
1036 ret += vprintf(fmt, ap);
1037 va_end(ap);
1038 return ret;
1039}
1040
1041char *
1042ata_mode2str(int mode)
1043{
1044 switch (mode) {
1045 case ATA_PIO: return "BIOSPIO";
1046 case ATA_PIO0: return "PIO0";
1047 case ATA_PIO1: return "PIO1";
1048 case ATA_PIO2: return "PIO2";
1049 case ATA_PIO3: return "PIO3";
1050 case ATA_PIO4: return "PIO4";
1051 case ATA_WDMA2: return "WDMA2";
1052 case ATA_UDMA2: return "UDMA33";
1053 case ATA_UDMA4: return "UDMA66";
1054 case ATA_UDMA5: return "UDMA100";
1055 case ATA_DMA: return "BIOSDMA";
1056 default: return "???";
1057 }
1058}
1059
1060int
1061ata_pio2mode(int pio)
1062{
1063 switch (pio) {
1064 default:
1065 case 0: return ATA_PIO0;
1066 case 1: return ATA_PIO1;
1067 case 2: return ATA_PIO2;
1068 case 3: return ATA_PIO3;
1069 case 4: return ATA_PIO4;
1070 }
1071}
1072
1073int
1074ata_pmode(struct ata_params *ap)
1075{
1076 if (ap->atavalid & ATA_FLAG_64_70) {
1077 if (ap->apiomodes & 2)
1078 return 4;
1079 if (ap->apiomodes & 1)
1080 return 3;
1081 }
1082 if (ap->opiomode == 2)
1083 return 2;
1084 if (ap->opiomode == 1)
1085 return 1;
1086 if (ap->opiomode == 0)
1087 return 0;
1088 return -1;
1089}
1090
1091int
1092ata_wmode(struct ata_params *ap)
1093{
1094 if (ap->wdmamodes & 4)
1095 return 2;
1096 if (ap->wdmamodes & 2)
1097 return 1;
1098 if (ap->wdmamodes & 1)
1099 return 0;
1100 return -1;
1101}
1102
1103int
1104ata_umode(struct ata_params *ap)
1105{
1106 if (ap->atavalid & ATA_FLAG_88) {
1107 if (ap->udmamodes & 0x20)
1108 return 5;
1109 if (ap->udmamodes & 0x10)
1110 return 4;
1111 if (ap->udmamodes & 0x08)
1112 return 3;
1113 if (ap->udmamodes & 0x04)
1114 return 2;
1115 if (ap->udmamodes & 0x02)
1116 return 1;
1117 if (ap->udmamodes & 0x01)
1118 return 0;
1119 }
1120 return -1;
1121}
1122
1123static char *
1124active2str(int active)
1125{
1126 static char buf[8];
1127
1128 switch (active) {
1129 case ATA_IDLE:
1130 return("ATA_IDLE");
1131 case ATA_IMMEDIATE:
1132 return("ATA_IMMEDIATE");
1133 case ATA_WAIT_INTR:
1134 return("ATA_WAIT_INTR");
1135 case ATA_WAIT_READY:
1136 return("ATA_WAIT_READY");
1137 case ATA_ACTIVE:
1138 return("ATA_ACTIVE");
1139 case ATA_ACTIVE_ATA:
1140 return("ATA_ACTIVE_ATA");
1141 case ATA_ACTIVE_ATAPI:
1142 return("ATA_ACTIVE_ATAPI");
1143 case ATA_REINITING:
1144 return("ATA_REINITING");
1145 default:
1146 sprintf(buf, "0x%02x", active);
1147 return buf;
1148 }
1149}
1150
1151static void
1152bswap(int8_t *buf, int len)
1153{
1154 u_int16_t *ptr = (u_int16_t*)(buf + len);
1155
1156 while (--ptr >= (u_int16_t*)buf)
1157 *ptr = ntohs(*ptr);
1158}
1159
1160static void
1161btrim(int8_t *buf, int len)
1162{
1163 int8_t *ptr;
1164
1165 for (ptr = buf; ptr < buf+len; ++ptr)
1166 if (!*ptr)
1167 *ptr = ' ';
1168 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1169 *ptr = 0;
1170}
1171
1172static void
1173bpack(int8_t *src, int8_t *dst, int len)
1174{
1175 int i, j, blank;
1176
1177 for (i = j = blank = 0 ; i < len; i++) {
1178 if (blank && src[i] == ' ') continue;
1179 if (blank && src[i] != ' ') {
1180 dst[j++] = src[i];
1181 blank = 0;
1182 continue;
1183 }
1184 if (src[i] == ' ') {
1185 blank = 1;
1186 if (i == 0)
1187 continue;
1188 }
1189 dst[j++] = src[i];
1190 }
1191 if (j < len)
1192 dst[j] = 0x00;
1193}
1194
1195static void
1196ata_change_mode(struct ata_softc *scp, int device, int mode)
1197{
1198 int umode, wmode, pmode;
1032 int s = splbio();
1033
1034 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
1035 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1036
1199 int s = splbio();
1200
1201 while (!atomic_cmpset_int(&scp->active, ATA_IDLE, ATA_ACTIVE))
1202 tsleep((caddr_t)&s, PRIBIO, "atachm", hz/4);
1203
1037 ata_dmainit(scp, device, ata_pmode(ATA_PARAM(scp, device)),
1038 mode < ATA_DMA ? -1 : ata_wmode(ATA_PARAM(scp, device)),
1039 mode < ATA_DMA ? -1 : ata_umode(ATA_PARAM(scp, device)));
1204 umode = ata_umode(ATA_PARAM(scp, device));
1205 wmode = ata_wmode(ATA_PARAM(scp, device));
1206 pmode = ata_pmode(ATA_PARAM(scp, device));
1207
1208 switch (mode & ATA_DMA_MASK) {
1209 case ATA_UDMA:
1210 if ((mode & ATA_MODE_MASK) < umode)
1211 umode = mode & ATA_MODE_MASK;
1212 break;
1213 case ATA_WDMA:
1214 if ((mode & ATA_MODE_MASK) < wmode)
1215 wmode = mode & ATA_MODE_MASK;
1216 umode = -1;
1217 break;
1218 default:
1219 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1220 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1221 umode = -1;
1222 wmode = -1;
1223 }
1224 ata_dmainit(scp, device, pmode, wmode, umode);
1225
1040 scp->active = ATA_IDLE;
1041 ata_start(scp);
1042 splx(s);
1043}
1044
1226 scp->active = ATA_IDLE;
1227 ata_start(scp);
1228 splx(s);
1229}
1230
1045static int
1046sysctl_hw_ata(SYSCTL_HANDLER_ARGS)
1047{
1048 struct ata_softc *scp;
1049 int ctlr, error, i;
1050
1051 /* readout internal state */
1052 bzero(ata_conf, sizeof(ata_conf));
1053 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
1054 if (!(scp = devclass_get_softc(ata_devclass, ctlr)))
1055 continue;
1056 for (i = 0; i < 2; i++) {
1057 if (!scp->dev_softc[i])
1058 strcat(ata_conf, "---,");
1059 else if (scp->mode[i] >= ATA_DMA)
1060 strcat(ata_conf, "dma,");
1061 else
1062 strcat(ata_conf, "pio,");
1063 }
1064 }
1065 error = sysctl_handle_string(oidp, ata_conf, sizeof(ata_conf), req);
1066 if (error == 0 && req->newptr != NULL) {
1067 char *ptr = ata_conf;
1068
1069 /* update internal state */
1070 i = 0;
1071 while (*ptr) {
1072 if (!strncmp(ptr, "pio", 3) || !strncmp(ptr, "PIO", 3)) {
1073 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1074 scp->dev_softc[i & 1] && scp->mode[i & 1] >= ATA_DMA)
1075 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_PIO);
1076 }
1077 else if (!strncmp(ptr, "dma", 3) || !strncmp(ptr, "DMA", 3)) {
1078 if ((scp = devclass_get_softc(ata_devclass, i >> 1)) &&
1079 scp->dev_softc[i & 1] && scp->mode[i & 1] < ATA_DMA)
1080 ata_change_mode(scp, (i & 1)?ATA_SLAVE:ATA_MASTER, ATA_DMA);
1081 }
1082 else if (strncmp(ptr, "---", 3))
1083 break;
1084 ptr+=3;
1085 if (*ptr++ != ',' ||
1086 ++i > (devclass_get_maxunit(ata_devclass) << 1))
1087 break;
1088 }
1089 }
1090 return error;
1091}
1092SYSCTL_PROC(_hw, OID_AUTO, atamodes, CTLTYPE_STRING | CTLFLAG_RW,
1093 0, sizeof(ata_conf), sysctl_hw_ata, "A", "");
1094
1095static void
1096ata_init(void)
1097{
1231static void
1232ata_init(void)
1233{
1234 /* register controlling device */
1235 make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0666, "ata");
1236
1098 /* register boot attach to be run when interrupts are enabled */
1099 if (!(ata_delayed_attach = (struct intr_config_hook *)
1100 malloc(sizeof(struct intr_config_hook),
1101 M_TEMP, M_NOWAIT | M_ZERO))) {
1102 printf("ata: malloc of delayed attach hook failed\n");
1103 return;
1104 }
1105
1106 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1107 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1108 printf("ata: config_intrhook_establish failed\n");
1109 free(ata_delayed_attach, M_TEMP);
1110 }
1111}
1112SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
1237 /* register boot attach to be run when interrupts are enabled */
1238 if (!(ata_delayed_attach = (struct intr_config_hook *)
1239 malloc(sizeof(struct intr_config_hook),
1240 M_TEMP, M_NOWAIT | M_ZERO))) {
1241 printf("ata: malloc of delayed attach hook failed\n");
1242 return;
1243 }
1244
1245 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1246 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1247 printf("ata: config_intrhook_establish failed\n");
1248 free(ata_delayed_attach, M_TEMP);
1249 }
1250}
1251SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)