Wed Oct 1 18:23:55 2008 UTC ()
Use a separate thread to probe/attach atabus's childrens. Fixes a deadlock
where the interrupt routine wants to wake up the atabus thread to perform a
reset, while the thread is blocked in wd's attach function.


(bouyer)
diff -r1.99 -r1.100 src/sys/dev/ata/ata.c

cvs diff -r1.99 -r1.100 src/sys/dev/ata/ata.c (switch to unified diff)

--- src/sys/dev/ata/ata.c 2008/06/12 21:46:35 1.99
+++ src/sys/dev/ata/ata.c 2008/10/01 18:23:55 1.100
@@ -1,1559 +1,1602 @@ @@ -1,1559 +1,1602 @@
1/* $NetBSD: ata.c,v 1.99 2008/06/12 21:46:35 cegger Exp $ */ 1/* $NetBSD: ata.c,v 1.100 2008/10/01 18:23:55 bouyer Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved. 4 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software 14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement: 15 * must display the following acknowledgement:
16 * This product includes software developed by Manuel Bouyer. 16 * This product includes software developed by Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products 17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission. 18 * derived from this software without specific prior written permission.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.99 2008/06/12 21:46:35 cegger Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.100 2008/10/01 18:23:55 bouyer Exp $");
34 34
35#include "opt_ata.h" 35#include "opt_ata.h"
36 36
37#include <sys/param.h> 37#include <sys/param.h>
38#include <sys/systm.h> 38#include <sys/systm.h>
39#include <sys/kernel.h> 39#include <sys/kernel.h>
40#include <sys/malloc.h> 40#include <sys/malloc.h>
41#include <sys/device.h> 41#include <sys/device.h>
42#include <sys/conf.h> 42#include <sys/conf.h>
43#include <sys/fcntl.h> 43#include <sys/fcntl.h>
44#include <sys/proc.h> 44#include <sys/proc.h>
45#include <sys/pool.h> 45#include <sys/pool.h>
46#include <sys/kthread.h> 46#include <sys/kthread.h>
47#include <sys/errno.h> 47#include <sys/errno.h>
48#include <sys/ataio.h> 48#include <sys/ataio.h>
49#include <sys/kmem.h> 49#include <sys/kmem.h>
50#include <sys/simplelock.h> 50#include <sys/simplelock.h>
51#include <sys/intr.h> 51#include <sys/intr.h>
52#include <sys/bus.h> 52#include <sys/bus.h>
53 53
54#include <dev/ata/ataconf.h> 54#include <dev/ata/ataconf.h>
55#include <dev/ata/atareg.h> 55#include <dev/ata/atareg.h>
56#include <dev/ata/atavar.h> 56#include <dev/ata/atavar.h>
57#include <dev/ic/wdcvar.h> /* for PIOBM */ 57#include <dev/ic/wdcvar.h> /* for PIOBM */
58 58
59#include "locators.h" 59#include "locators.h"
60 60
61#include "atapibus.h" 61#include "atapibus.h"
62#include "ataraid.h" 62#include "ataraid.h"
63 63
64#if NATARAID > 0 64#if NATARAID > 0
65#include <dev/ata/ata_raidvar.h> 65#include <dev/ata/ata_raidvar.h>
66#endif 66#endif
67 67
68#define DEBUG_FUNCS 0x08 68#define DEBUG_FUNCS 0x08
69#define DEBUG_PROBE 0x10 69#define DEBUG_PROBE 0x10
70#define DEBUG_DETACH 0x20 70#define DEBUG_DETACH 0x20
71#define DEBUG_XFERS 0x40 71#define DEBUG_XFERS 0x40
72#ifdef ATADEBUG 72#ifdef ATADEBUG
73int atadebug_mask = 0; 73int atadebug_mask = 0;
74#define ATADEBUG_PRINT(args, level) \ 74#define ATADEBUG_PRINT(args, level) \
75 if (atadebug_mask & (level)) \ 75 if (atadebug_mask & (level)) \
76 printf args 76 printf args
77#else 77#else
78#define ATADEBUG_PRINT(args, level) 78#define ATADEBUG_PRINT(args, level)
79#endif 79#endif
80 80
81POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL, 81POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL,
82 IPL_BIO); 82 IPL_BIO);
83 83
84/* 84/*
85 * A queue of atabus instances, used to ensure the same bus probe order 85 * A queue of atabus instances, used to ensure the same bus probe order
86 * for a given hardware configuration at each boot. 86 * for a given hardware configuration at each boot.
87 */ 87 */
88struct atabus_initq_head atabus_initq_head = 88struct atabus_initq_head atabus_initq_head =
89 TAILQ_HEAD_INITIALIZER(atabus_initq_head); 89 TAILQ_HEAD_INITIALIZER(atabus_initq_head);
90struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER; 90struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER;
91 91
 92/* kernel thread probing devices on a atabus. Only one probing at once */
 93struct lwp *atabus_configlwp;
 94
92/***************************************************************************** 95/*****************************************************************************
93 * ATA bus layer. 96 * ATA bus layer.
94 * 97 *
95 * ATA controllers attach an atabus instance, which handles probing the bus 98 * ATA controllers attach an atabus instance, which handles probing the bus
96 * for drives, etc. 99 * for drives, etc.
97 *****************************************************************************/ 100 *****************************************************************************/
98 101
99dev_type_open(atabusopen); 102dev_type_open(atabusopen);
100dev_type_close(atabusclose); 103dev_type_close(atabusclose);
101dev_type_ioctl(atabusioctl); 104dev_type_ioctl(atabusioctl);
102 105
103const struct cdevsw atabus_cdevsw = { 106const struct cdevsw atabus_cdevsw = {
104 atabusopen, atabusclose, noread, nowrite, atabusioctl, 107 atabusopen, atabusclose, noread, nowrite, atabusioctl,
105 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER 108 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
106}; 109};
107 110
108extern struct cfdriver atabus_cd; 111extern struct cfdriver atabus_cd;
109 112
110static void atabus_childdetached(device_t, device_t); 113static void atabus_childdetached(device_t, device_t);
111static bool atabus_resume(device_t PMF_FN_PROTO); 114static bool atabus_resume(device_t PMF_FN_PROTO);
112static bool atabus_suspend(device_t PMF_FN_PROTO); 115static bool atabus_suspend(device_t PMF_FN_PROTO);
 116static void atabusconfig_thread(void *);
113 117
114/* 118/*
115 * atabusprint: 119 * atabusprint:
116 * 120 *
117 * Autoconfiguration print routine used by ATA controllers when 121 * Autoconfiguration print routine used by ATA controllers when
118 * attaching an atabus instance. 122 * attaching an atabus instance.
119 */ 123 */
120int 124int
121atabusprint(void *aux, const char *pnp) 125atabusprint(void *aux, const char *pnp)
122{ 126{
123 struct ata_channel *chan = aux; 127 struct ata_channel *chan = aux;
124 128
125 if (pnp) 129 if (pnp)
126 aprint_normal("atabus at %s", pnp); 130 aprint_normal("atabus at %s", pnp);
127 aprint_normal(" channel %d", chan->ch_channel); 131 aprint_normal(" channel %d", chan->ch_channel);
128 132
129 return (UNCONF); 133 return (UNCONF);
130} 134}
131 135
132/* 136/*
133 * ataprint: 137 * ataprint:
134 * 138 *
135 * Autoconfiguration print routine. 139 * Autoconfiguration print routine.
136 */ 140 */
137int 141int
138ataprint(void *aux, const char *pnp) 142ataprint(void *aux, const char *pnp)
139{ 143{
140 struct ata_device *adev = aux; 144 struct ata_device *adev = aux;
141 145
142 if (pnp) 146 if (pnp)
143 aprint_normal("wd at %s", pnp); 147 aprint_normal("wd at %s", pnp);
144 aprint_normal(" drive %d", adev->adev_drv_data->drive); 148 aprint_normal(" drive %d", adev->adev_drv_data->drive);
145 149
146 return (UNCONF); 150 return (UNCONF);
147} 151}
148 152
149/* 153/*
150 * ata_channel_attach: 154 * ata_channel_attach:
151 * 155 *
152 * Common parts of attaching an atabus to an ATA controller channel. 156 * Common parts of attaching an atabus to an ATA controller channel.
153 */ 157 */
154void 158void
155ata_channel_attach(struct ata_channel *chp) 159ata_channel_attach(struct ata_channel *chp)
156{ 160{
157 161
158 if (chp->ch_flags & ATACH_DISABLED) 162 if (chp->ch_flags & ATACH_DISABLED)
159 return; 163 return;
160 164
161 callout_init(&chp->ch_callout, 0); 165 callout_init(&chp->ch_callout, 0);
162 166
163 TAILQ_INIT(&chp->ch_queue->queue_xfer); 167 TAILQ_INIT(&chp->ch_queue->queue_xfer);
164 chp->ch_queue->queue_freeze = 0; 168 chp->ch_queue->queue_freeze = 0;
165 chp->ch_queue->queue_flags = 0; 169 chp->ch_queue->queue_flags = 0;
166 chp->ch_queue->active_xfer = NULL; 170 chp->ch_queue->active_xfer = NULL;
167 171
168 chp->atabus = config_found_ia(chp->ch_atac->atac_dev, "ata", chp, 172 chp->atabus = config_found_ia(chp->ch_atac->atac_dev, "ata", chp,
169 atabusprint); 173 atabusprint);
170} 174}
171 175
172static void 176static void
173atabusconfig(struct atabus_softc *atabus_sc) 177atabusconfig(struct atabus_softc *atabus_sc)
174{ 178{
175 struct ata_channel *chp = atabus_sc->sc_chan; 179 struct ata_channel *chp = atabus_sc->sc_chan;
176 struct atac_softc *atac = chp->ch_atac; 180 struct atac_softc *atac = chp->ch_atac;
177 int i, s; 
178 struct atabus_initq *atabus_initq = NULL; 181 struct atabus_initq *atabus_initq = NULL;
 182 int i, s, error;
 183
 184 /* we are in the atabus's thread context */
 185 s = splbio();
 186 chp->ch_flags |= ATACH_TH_RUN;
 187 splx(s);
179 188
180 /* Probe for the drives. */ 189 /* Probe for the drives. */
181 /* XXX for SATA devices we will power up all drives at once */ 190 /* XXX for SATA devices we will power up all drives at once */
182 (*atac->atac_probe)(chp); 191 (*atac->atac_probe)(chp);
183 192
184 ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n", 193 ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n",
185 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags), 194 chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
186 DEBUG_PROBE); 195 DEBUG_PROBE);
187 196
 197 /* next operations will occurs in a separate thread */
 198 s = splbio();
 199 chp->ch_flags &= ~ATACH_TH_RUN;
 200 splx(s);
 201
 202 /* Make sure the devices probe in atabus order to avoid jitter. */
 203 simple_lock(&atabus_interlock);
 204 while(1) {
 205 atabus_initq = TAILQ_FIRST(&atabus_initq_head);
 206 if (atabus_initq->atabus_sc == atabus_sc)
 207 break;
 208 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
 209 &atabus_interlock);
 210 }
 211 simple_unlock(&atabus_interlock);
 212
188 /* If no drives, abort here */ 213 /* If no drives, abort here */
189 for (i = 0; i < chp->ch_ndrive; i++) 214 for (i = 0; i < chp->ch_ndrive; i++)
190 if ((chp->ch_drive[i].drive_flags & DRIVE) != 0) 215 if ((chp->ch_drive[i].drive_flags & DRIVE) != 0)
191 break; 216 break;
192 if (i == chp->ch_ndrive) 217 if (i == chp->ch_ndrive)
193 goto out; 218 goto out;
194 219
195 /* Shortcut in case we've been shutdown */ 220 /* Shortcut in case we've been shutdown */
196 if (chp->ch_flags & ATACH_SHUTDOWN) 221 if (chp->ch_flags & ATACH_SHUTDOWN)
197 goto out; 222 goto out;
198 223
199 /* Make sure the devices probe in atabus order to avoid jitter. */ 224
 225 if ((error = kthread_create(PRI_NONE, 0, NULL, atabusconfig_thread,
 226 atabus_sc, &atabus_configlwp,
 227 "%scnf", device_xname(atac->atac_dev))) != 0)
 228 aprint_error_dev(atac->atac_dev,
 229 "unable to create config thread: error %d\n", error);
 230 return;
 231
 232 out:
200 simple_lock(&atabus_interlock); 233 simple_lock(&atabus_interlock);
201 while(1) { 234 TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
202 atabus_initq = TAILQ_FIRST(&atabus_initq_head); 
203 if (atabus_initq->atabus_sc == atabus_sc) 
204 break; 
205 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0, 
206 &atabus_interlock); 
207 } 
208 simple_unlock(&atabus_interlock); 235 simple_unlock(&atabus_interlock);
209 236
 237 free(atabus_initq, M_DEVBUF);
 238 wakeup(&atabus_initq_head);
 239
 240 ata_delref(chp);
 241
 242 config_pending_decr();
 243}
 244
 245/*
 246 * atabus_configthread: finish attach of atabus's childrens, in a separate
 247 * kernel thread.
 248 */
 249static void
 250atabusconfig_thread(void *arg)
 251{
 252 struct atabus_softc *atabus_sc = arg;
 253 struct ata_channel *chp = atabus_sc->sc_chan;
 254 struct atac_softc *atac = chp->ch_atac;
 255 int i, s;
 256 struct atabus_initq *atabus_initq = NULL;
 257
 258 simple_lock(&atabus_interlock);
 259 atabus_initq = TAILQ_FIRST(&atabus_initq_head);
 260 simple_unlock(&atabus_interlock);
 261 KASSERT(atabus_initq->atabus_sc == atabus_sc);
210 /* 262 /*
211 * Attach an ATAPI bus, if needed. 263 * Attach an ATAPI bus, if needed.
212 */ 264 */
213 for (i = 0; i < chp->ch_ndrive; i++) { 265 for (i = 0; i < chp->ch_ndrive; i++) {
214 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) { 266 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) {
215#if NATAPIBUS > 0 267#if NATAPIBUS > 0
216 (*atac->atac_atapibus_attach)(atabus_sc); 268 (*atac->atac_atapibus_attach)(atabus_sc);
217#else 269#else
218 /* 270 /*
219 * Fake the autoconfig "not configured" message 271 * Fake the autoconfig "not configured" message
220 */ 272 */
221 aprint_normal("atapibus at %s not configured\n", 273 aprint_normal("atapibus at %s not configured\n",
222 device_xname(atac->atac_dev)); 274 device_xname(atac->atac_dev));
223 chp->atapibus = NULL; 275 chp->atapibus = NULL;
224 s = splbio(); 276 s = splbio();
225 for (i = 0; i < chp->ch_ndrive; i++) 277 for (i = 0; i < chp->ch_ndrive; i++)
226 chp->ch_drive[i].drive_flags &= ~DRIVE_ATAPI; 278 chp->ch_drive[i].drive_flags &= ~DRIVE_ATAPI;
227 splx(s); 279 splx(s);
228#endif 280#endif
229 break; 281 break;
230 } 282 }
231 } 283 }
232 284
233 for (i = 0; i < chp->ch_ndrive; i++) { 285 for (i = 0; i < chp->ch_ndrive; i++) {
234 struct ata_device adev; 286 struct ata_device adev;
235 if ((chp->ch_drive[i].drive_flags & 287 if ((chp->ch_drive[i].drive_flags &
236 (DRIVE_ATA | DRIVE_OLD)) == 0) { 288 (DRIVE_ATA | DRIVE_OLD)) == 0) {
237 continue; 289 continue;
238 } 290 }
239 memset(&adev, 0, sizeof(struct ata_device)); 291 memset(&adev, 0, sizeof(struct ata_device));
240 adev.adev_bustype = atac->atac_bustype_ata; 292 adev.adev_bustype = atac->atac_bustype_ata;
241 adev.adev_channel = chp->ch_channel; 293 adev.adev_channel = chp->ch_channel;
242 adev.adev_openings = 1; 294 adev.adev_openings = 1;
243 adev.adev_drv_data = &chp->ch_drive[i]; 295 adev.adev_drv_data = &chp->ch_drive[i];
244 chp->ata_drives[i] = config_found_ia(atabus_sc->sc_dev, 296 chp->ata_drives[i] = config_found_ia(atabus_sc->sc_dev,
245 "ata_hl", &adev, ataprint); 297 "ata_hl", &adev, ataprint);
246 if (chp->ata_drives[i] != NULL) 298 if (chp->ata_drives[i] != NULL)
247 ata_probe_caps(&chp->ch_drive[i]); 299 ata_probe_caps(&chp->ch_drive[i]);
248 else { 300 else {
249 s = splbio(); 301 s = splbio();
250 chp->ch_drive[i].drive_flags &= 302 chp->ch_drive[i].drive_flags &=
251 ~(DRIVE_ATA | DRIVE_OLD); 303 ~(DRIVE_ATA | DRIVE_OLD);
252 splx(s); 304 splx(s);
253 } 305 }
254 } 306 }
255 307
256 /* now that we know the drives, the controller can set its modes */ 308 /* now that we know the drives, the controller can set its modes */
257 if (atac->atac_set_modes) { 309 if (atac->atac_set_modes) {
258 (*atac->atac_set_modes)(chp); 310 (*atac->atac_set_modes)(chp);
259 ata_print_modes(chp); 311 ata_print_modes(chp);
260 } 312 }
261#if NATARAID > 0 313#if NATARAID > 0
262 if (atac->atac_cap & ATAC_CAP_RAID) 314 if (atac->atac_cap & ATAC_CAP_RAID)
263 for (i = 0; i < chp->ch_ndrive; i++) 315 for (i = 0; i < chp->ch_ndrive; i++)
264 if (chp->ata_drives[i] != NULL) 316 if (chp->ata_drives[i] != NULL)
265 ata_raid_check_component(chp->ata_drives[i]); 317 ata_raid_check_component(chp->ata_drives[i]);
266#endif /* NATARAID > 0 */ 318#endif /* NATARAID > 0 */
267 319
268 /* 320 /*
269 * reset drive_flags for unattached devices, reset state for attached 321 * reset drive_flags for unattached devices, reset state for attached
270 * ones 322 * ones
271 */ 323 */
272 s = splbio(); 324 s = splbio();
273 for (i = 0; i < chp->ch_ndrive; i++) { 325 for (i = 0; i < chp->ch_ndrive; i++) {
274 if (chp->ch_drive[i].drv_softc == NULL) 326 if (chp->ch_drive[i].drv_softc == NULL)
275 chp->ch_drive[i].drive_flags = 0; 327 chp->ch_drive[i].drive_flags = 0;
276 else 328 else
277 chp->ch_drive[i].state = 0; 329 chp->ch_drive[i].state = 0;
278 } 330 }
279 splx(s); 331 splx(s);
280 332
281 out: 
282 if (atabus_initq == NULL) { 
283 simple_lock(&atabus_interlock); 
284 while(1) { 
285 atabus_initq = TAILQ_FIRST(&atabus_initq_head); 
286 if (atabus_initq->atabus_sc == atabus_sc) 
287 break; 
288 ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0, 
289 &atabus_interlock); 
290 } 
291 simple_unlock(&atabus_interlock); 
292 } 
293 simple_lock(&atabus_interlock); 333 simple_lock(&atabus_interlock);
294 TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq); 334 TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
295 simple_unlock(&atabus_interlock); 335 simple_unlock(&atabus_interlock);
296 336
297 free(atabus_initq, M_DEVBUF); 337 free(atabus_initq, M_DEVBUF);
298 wakeup(&atabus_initq_head); 338 wakeup(&atabus_initq_head);
299 339
300 ata_delref(chp); 340 ata_delref(chp);
301 341
302 config_pending_decr(); 342 config_pending_decr();
 343 kthread_exit(0);
303} 344}
304 345
305/* 346/*
306 * atabus_thread: 347 * atabus_thread:
307 * 348 *
308 * Worker thread for the ATA bus. 349 * Worker thread for the ATA bus.
309 */ 350 */
310static void 351static void
311atabus_thread(void *arg) 352atabus_thread(void *arg)
312{ 353{
313 struct atabus_softc *sc = arg; 354 struct atabus_softc *sc = arg;
314 struct ata_channel *chp = sc->sc_chan; 355 struct ata_channel *chp = sc->sc_chan;
315 struct ata_xfer *xfer; 356 struct ata_xfer *xfer;
316 int i, s; 357 int i, s;
317 358
318 s = splbio(); 359 s = splbio();
319 chp->ch_flags |= ATACH_TH_RUN; 360 chp->ch_flags |= ATACH_TH_RUN;
320 361
321 /* 362 /*
322 * Probe the drives. Reset all flags to 0 to indicate to controllers 363 * Probe the drives. Reset all flags to 0 to indicate to controllers
323 * that can re-probe that all drives must be probed.. 364 * that can re-probe that all drives must be probed..
324 * 365 *
325 * Note: ch_ndrive may be changed during the probe. 366 * Note: ch_ndrive may be changed during the probe.
326 */ 367 */
327 for (i = 0; i < ATA_MAXDRIVES; i++) 368 for (i = 0; i < ATA_MAXDRIVES; i++)
328 chp->ch_drive[i].drive_flags = 0; 369 chp->ch_drive[i].drive_flags = 0;
329 splx(s); 370 splx(s);
330 371
331 /* Configure the devices on the bus. */ 372 /* Configure the devices on the bus. */
332 atabusconfig(sc); 373 atabusconfig(sc);
333 374
334 s = splbio(); 375 s = splbio();
335 for (;;) { 376 for (;;) {
336 if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 && 377 if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 &&
337 (chp->ch_queue->active_xfer == NULL || 378 (chp->ch_queue->active_xfer == NULL ||
338 chp->ch_queue->queue_freeze == 0)) { 379 chp->ch_queue->queue_freeze == 0)) {
339 chp->ch_flags &= ~ATACH_TH_RUN; 380 chp->ch_flags &= ~ATACH_TH_RUN;
340 (void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0); 381 (void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
341 chp->ch_flags |= ATACH_TH_RUN; 382 chp->ch_flags |= ATACH_TH_RUN;
342 } 383 }
343 if (chp->ch_flags & ATACH_SHUTDOWN) { 384 if (chp->ch_flags & ATACH_SHUTDOWN) {
344 break; 385 break;
345 } 386 }
346 if (chp->ch_flags & ATACH_TH_RESET) { 387 if (chp->ch_flags & ATACH_TH_RESET) {
347 /* 388 /*
348 * ata_reset_channel() will freeze 2 times, so 389 * ata_reset_channel() will freeze 2 times, so
349 * unfreeze one time. Not a problem as we're at splbio 390 * unfreeze one time. Not a problem as we're at splbio
350 */ 391 */
351 chp->ch_queue->queue_freeze--; 392 chp->ch_queue->queue_freeze--;
352 ata_reset_channel(chp, AT_WAIT | chp->ch_reset_flags); 393 ata_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
353 } else if (chp->ch_queue->active_xfer != NULL && 394 } else if (chp->ch_queue->active_xfer != NULL &&
354 chp->ch_queue->queue_freeze == 1) { 395 chp->ch_queue->queue_freeze == 1) {
355 /* 396 /*
356 * Caller has bumped queue_freeze, decrease it. 397 * Caller has bumped queue_freeze, decrease it.
357 */ 398 */
358 chp->ch_queue->queue_freeze--; 399 chp->ch_queue->queue_freeze--;
359 xfer = chp->ch_queue->active_xfer; 400 xfer = chp->ch_queue->active_xfer;
360 KASSERT(xfer != NULL); 401 KASSERT(xfer != NULL);
361 (*xfer->c_start)(xfer->c_chp, xfer); 402 (*xfer->c_start)(xfer->c_chp, xfer);
362 } else if (chp->ch_queue->queue_freeze > 1) 403 } else if (chp->ch_queue->queue_freeze > 1)
363 panic("ata_thread: queue_freeze"); 404 panic("ata_thread: queue_freeze");
364 } 405 }
365 splx(s); 406 splx(s);
366 chp->ch_thread = NULL; 407 chp->ch_thread = NULL;
367 wakeup(&chp->ch_flags); 408 wakeup(&chp->ch_flags);
368 kthread_exit(0); 409 kthread_exit(0);
369} 410}
370 411
371/* 412/*
372 * atabus_match: 413 * atabus_match:
373 * 414 *
374 * Autoconfiguration match routine. 415 * Autoconfiguration match routine.
375 */ 416 */
376static int 417static int
377atabus_match(device_t parent, struct cfdata *cf, void *aux) 418atabus_match(device_t parent, struct cfdata *cf, void *aux)
378{ 419{
379 struct ata_channel *chp = aux; 420 struct ata_channel *chp = aux;
380 421
381 if (chp == NULL) 422 if (chp == NULL)
382 return (0); 423 return (0);
383 424
384 if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel && 425 if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
385 cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT) 426 cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
386 return (0); 427 return (0);
387 428
388 return (1); 429 return (1);
389} 430}
390 431
391/* 432/*
392 * atabus_attach: 433 * atabus_attach:
393 * 434 *
394 * Autoconfiguration attach routine. 435 * Autoconfiguration attach routine.
395 */ 436 */
396static void 437static void
397atabus_attach(device_t parent, device_t self, void *aux) 438atabus_attach(device_t parent, device_t self, void *aux)
398{ 439{
399 struct atabus_softc *sc = device_private(self); 440 struct atabus_softc *sc = device_private(self);
400 struct ata_channel *chp = aux; 441 struct ata_channel *chp = aux;
401 struct atabus_initq *initq; 442 struct atabus_initq *initq;
402 int error; 443 int error;
403 444
404 sc->sc_chan = chp; 445 sc->sc_chan = chp;
405 446
406 aprint_normal("\n"); 447 aprint_normal("\n");
407 aprint_naive("\n"); 448 aprint_naive("\n");
408 449
409 sc->sc_dev = self; 450 sc->sc_dev = self;
410 451
411 if (ata_addref(chp)) 452 if (ata_addref(chp))
412 return; 453 return;
413 454
414 initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK); 455 initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
415 initq->atabus_sc = sc; 456 initq->atabus_sc = sc;
416 TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq); 457 TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
417 config_pending_incr(); 458 config_pending_incr();
418 459
419 if ((error = kthread_create(PRI_NONE, 0, NULL, atabus_thread, sc, 460 if ((error = kthread_create(PRI_NONE, 0, NULL, atabus_thread, sc,
420 &chp->ch_thread, "%s", device_xname(self))) != 0) 461 &chp->ch_thread, "%s", device_xname(self))) != 0)
421 aprint_error_dev(self, 462 aprint_error_dev(self,
422 "unable to create kernel thread: error %d\n", error); 463 "unable to create kernel thread: error %d\n", error);
423 464
424 if (!pmf_device_register(self, atabus_suspend, atabus_resume)) 465 if (!pmf_device_register(self, atabus_suspend, atabus_resume))
425 aprint_error_dev(self, "couldn't establish power handler\n"); 466 aprint_error_dev(self, "couldn't establish power handler\n");
426} 467}
427 468
428/* 469/*
429 * atabus_activate: 470 * atabus_activate:
430 * 471 *
431 * Autoconfiguration activation routine. 472 * Autoconfiguration activation routine.
432 */ 473 */
433static int 474static int
434atabus_activate(device_t self, enum devact act) 475atabus_activate(device_t self, enum devact act)
435{ 476{
436 struct atabus_softc *sc = device_private(self); 477 struct atabus_softc *sc = device_private(self);
437 struct ata_channel *chp = sc->sc_chan; 478 struct ata_channel *chp = sc->sc_chan;
438 device_t dev = NULL; 479 device_t dev = NULL;
439 int s, i, error = 0; 480 int s, i, error = 0;
440 481
441 s = splbio(); 482 s = splbio();
442 switch (act) { 483 switch (act) {
443 case DVACT_ACTIVATE: 484 case DVACT_ACTIVATE:
444 error = EOPNOTSUPP; 485 error = EOPNOTSUPP;
445 break; 486 break;
446 487
447 case DVACT_DEACTIVATE: 488 case DVACT_DEACTIVATE:
448 /* 489 /*
449 * We might deactivate the children of atapibus twice 490 * We might deactivate the children of atapibus twice
450 * (once bia atapibus, once directly), but since the 491 * (once bia atapibus, once directly), but since the
451 * generic autoconfiguration code maintains the DVF_ACTIVE 492 * generic autoconfiguration code maintains the DVF_ACTIVE
452 * flag, it's safe. 493 * flag, it's safe.
453 */ 494 */
454 if ((dev = chp->atapibus) != NULL) { 495 if ((dev = chp->atapibus) != NULL) {
455 error = config_deactivate(dev); 496 error = config_deactivate(dev);
456 if (error) 497 if (error)
457 goto out; 498 goto out;
458 } 499 }
459 500
460 for (i = 0; i < chp->ch_ndrive; i++) { 501 for (i = 0; i < chp->ch_ndrive; i++) {
461 if ((dev = chp->ch_drive[i].drv_softc) != NULL) { 502 if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
462 ATADEBUG_PRINT(("atabus_activate: %s: " 503 ATADEBUG_PRINT(("atabus_activate: %s: "
463 "deactivating %s\n", device_xname(self), 504 "deactivating %s\n", device_xname(self),
464 device_xname(dev)), 505 device_xname(dev)),
465 DEBUG_DETACH); 506 DEBUG_DETACH);
466 error = config_deactivate(dev); 507 error = config_deactivate(dev);
467 if (error) 508 if (error)
468 goto out; 509 goto out;
469 } 510 }
470 } 511 }
471 break; 512 break;
472 } 513 }
473 out: 514 out:
474 splx(s); 515 splx(s);
475 516
476#ifdef ATADEBUG 517#ifdef ATADEBUG
477 if (dev != NULL && error != 0) 518 if (dev != NULL && error != 0)
478 ATADEBUG_PRINT(("atabus_activate: %s: " 519 ATADEBUG_PRINT(("atabus_activate: %s: "
479 "error %d deactivating %s\n", device_xname(self), 520 "error %d deactivating %s\n", device_xname(self),
480 error, device_xname(dev)), DEBUG_DETACH); 521 error, device_xname(dev)), DEBUG_DETACH);
481#endif /* ATADEBUG */ 522#endif /* ATADEBUG */
482 523
483 return (error); 524 return (error);
484} 525}
485 526
486/* 527/*
487 * atabus_detach: 528 * atabus_detach:
488 * 529 *
489 * Autoconfiguration detach routine. 530 * Autoconfiguration detach routine.
490 */ 531 */
491static int 532static int
492atabus_detach(device_t self, int flags) 533atabus_detach(device_t self, int flags)
493{ 534{
494 struct atabus_softc *sc = device_private(self); 535 struct atabus_softc *sc = device_private(self);
495 struct ata_channel *chp = sc->sc_chan; 536 struct ata_channel *chp = sc->sc_chan;
496 device_t dev = NULL; 537 device_t dev = NULL;
497 int s, i, error = 0; 538 int s, i, error = 0;
498 539
499 /* Shutdown the channel. */ 540 /* Shutdown the channel. */
500 s = splbio(); /* XXX ALSO NEED AN INTERLOCK HERE. */ 541 s = splbio(); /* XXX ALSO NEED AN INTERLOCK HERE. */
501 chp->ch_flags |= ATACH_SHUTDOWN; 542 chp->ch_flags |= ATACH_SHUTDOWN;
502 splx(s); 543 splx(s);
503 544
504 wakeup(&chp->ch_thread); 545 wakeup(&chp->ch_thread);
505 546
506 while (chp->ch_thread != NULL) 547 while (chp->ch_thread != NULL)
507 (void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0); 548 (void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0);
508 549
509 550
510 /* 551 /*
511 * Detach atapibus and its children. 552 * Detach atapibus and its children.
512 */ 553 */
513 if ((dev = chp->atapibus) != NULL) { 554 if ((dev = chp->atapibus) != NULL) {
514 ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n", 555 ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
515 device_xname(self), device_xname(dev)), DEBUG_DETACH); 556 device_xname(self), device_xname(dev)), DEBUG_DETACH);
516 557
517 error = config_detach(dev, flags); 558 error = config_detach(dev, flags);
518 if (error) 559 if (error)
519 goto out; 560 goto out;
520 } 561 }
521 562
522 /* 563 /*
523 * Detach our other children. 564 * Detach our other children.
524 */ 565 */
525 for (i = 0; i < chp->ch_ndrive; i++) { 566 for (i = 0; i < chp->ch_ndrive; i++) {
526 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) 567 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
527 continue; 568 continue;
528 if ((dev = chp->ch_drive[i].drv_softc) != NULL) { 569 if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
529 ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n", 570 ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
530 device_xname(self), device_xname(dev)), 571 device_xname(self), device_xname(dev)),
531 DEBUG_DETACH); 572 DEBUG_DETACH);
532 error = config_detach(dev, flags); 573 error = config_detach(dev, flags);
533 if (error) 574 if (error)
534 goto out; 575 goto out;
535 } 576 }
536 } 577 }
537 578
538 out: 579 out:
539#ifdef ATADEBUG 580#ifdef ATADEBUG
540 if (dev != NULL && error != 0) 581 if (dev != NULL && error != 0)
541 ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n", 582 ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
542 device_xname(self), error, device_xname(dev)), 583 device_xname(self), error, device_xname(dev)),
543 DEBUG_DETACH); 584 DEBUG_DETACH);
544#endif /* ATADEBUG */ 585#endif /* ATADEBUG */
545 586
546 return (error); 587 return (error);
547} 588}
548 589
549void 590void
550atabus_childdetached(device_t self, device_t child) 591atabus_childdetached(device_t self, device_t child)
551{ 592{
552 struct atabus_softc *sc = device_private(self); 593 struct atabus_softc *sc = device_private(self);
553 struct ata_channel *chp = sc->sc_chan; 594 struct ata_channel *chp = sc->sc_chan;
554 int i; 595 int i;
555 596
556 /* 597 /*
557 * atapibus detached. 598 * atapibus detached.
558 */ 599 */
559 if (child == chp->atapibus) { 600 if (child == chp->atapibus) {
560 chp->atapibus = NULL; 601 chp->atapibus = NULL;
561 return; 602 return;
562 } 603 }
563 604
564 /* 605 /*
565 * Detach our other children. 606 * Detach our other children.
566 */ 607 */
567 for (i = 0; i < chp->ch_ndrive; i++) { 608 for (i = 0; i < chp->ch_ndrive; i++) {
568 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) 609 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
569 continue; 610 continue;
570 if (child == chp->ch_drive[i].drv_softc) { 611 if (child == chp->ch_drive[i].drv_softc) {
571 chp->ch_drive[i].drv_softc = NULL; 612 chp->ch_drive[i].drv_softc = NULL;
572 chp->ch_drive[i].drive_flags = 0; 613 chp->ch_drive[i].drive_flags = 0;
573 return; 614 return;
574 } 615 }
575 } 616 }
576 617
577 aprint_error_dev(self, "unknown child %p", (const void *)child); 618 aprint_error_dev(self, "unknown child %p", (const void *)child);
578} 619}
579 620
580CFATTACH_DECL2_NEW(atabus, sizeof(struct atabus_softc), 621CFATTACH_DECL2_NEW(atabus, sizeof(struct atabus_softc),
581 atabus_match, atabus_attach, atabus_detach, atabus_activate, NULL, 622 atabus_match, atabus_attach, atabus_detach, atabus_activate, NULL,
582 atabus_childdetached); 623 atabus_childdetached);
583 624
584/***************************************************************************** 625/*****************************************************************************
585 * Common ATA bus operations. 626 * Common ATA bus operations.
586 *****************************************************************************/ 627 *****************************************************************************/
587 628
588/* Get the disk's parameters */ 629/* Get the disk's parameters */
589int 630int
590ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags, 631ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
591 struct ataparams *prms) 632 struct ataparams *prms)
592{ 633{
593 struct ata_command ata_c; 634 struct ata_command ata_c;
594 struct ata_channel *chp = drvp->chnl_softc; 635 struct ata_channel *chp = drvp->chnl_softc;
595 struct atac_softc *atac = chp->ch_atac; 636 struct atac_softc *atac = chp->ch_atac;
596 char *tb; 637 char *tb;
597 int i, rv; 638 int i, rv;
598 u_int16_t *p; 639 u_int16_t *p;
599 640
600 ATADEBUG_PRINT(("%s\n", __func__), DEBUG_FUNCS); 641 ATADEBUG_PRINT(("%s\n", __func__), DEBUG_FUNCS);
601 642
602 tb = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 643 tb = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
603 memset(prms, 0, sizeof(struct ataparams)); 644 memset(prms, 0, sizeof(struct ataparams));
604 memset(&ata_c, 0, sizeof(struct ata_command)); 645 memset(&ata_c, 0, sizeof(struct ata_command));
605 646
606 if (drvp->drive_flags & DRIVE_ATA) { 647 if (drvp->drive_flags & DRIVE_ATA) {
607 ata_c.r_command = WDCC_IDENTIFY; 648 ata_c.r_command = WDCC_IDENTIFY;
608 ata_c.r_st_bmask = WDCS_DRDY; 649 ata_c.r_st_bmask = WDCS_DRDY;
609 ata_c.r_st_pmask = WDCS_DRQ; 650 ata_c.r_st_pmask = WDCS_DRQ;
610 ata_c.timeout = 3000; /* 3s */ 651 ata_c.timeout = 3000; /* 3s */
611 } else if (drvp->drive_flags & DRIVE_ATAPI) { 652 } else if (drvp->drive_flags & DRIVE_ATAPI) {
612 ata_c.r_command = ATAPI_IDENTIFY_DEVICE; 653 ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
613 ata_c.r_st_bmask = 0; 654 ata_c.r_st_bmask = 0;
614 ata_c.r_st_pmask = WDCS_DRQ; 655 ata_c.r_st_pmask = WDCS_DRQ;
615 ata_c.timeout = 10000; /* 10s */ 656 ata_c.timeout = 10000; /* 10s */
616 } else { 657 } else {
617 ATADEBUG_PRINT(("ata_get_parms: no disks\n"), 658 ATADEBUG_PRINT(("ata_get_parms: no disks\n"),
618 DEBUG_FUNCS|DEBUG_PROBE); 659 DEBUG_FUNCS|DEBUG_PROBE);
619 rv = CMD_ERR; 660 rv = CMD_ERR;
620 goto out; 661 goto out;
621 } 662 }
622 ata_c.flags = AT_READ | flags; 663 ata_c.flags = AT_READ | flags;
623 ata_c.data = tb; 664 ata_c.data = tb;
624 ata_c.bcount = DEV_BSIZE; 665 ata_c.bcount = DEV_BSIZE;
625 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp, 666 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
626 &ata_c) != ATACMD_COMPLETE) { 667 &ata_c) != ATACMD_COMPLETE) {
627 ATADEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"), 668 ATADEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
628 DEBUG_FUNCS|DEBUG_PROBE); 669 DEBUG_FUNCS|DEBUG_PROBE);
629 rv = CMD_AGAIN; 670 rv = CMD_AGAIN;
630 goto out; 671 goto out;
631 } 672 }
632 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 673 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
633 ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n", 674 ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
634 ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE); 675 ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
635 rv = CMD_ERR; 676 rv = CMD_ERR;
636 goto out; 677 goto out;
637 } 678 }
638 /* if we didn't read any data something is wrong */ 679 /* if we didn't read any data something is wrong */
639 if ((ata_c.flags & AT_XFDONE) == 0) { 680 if ((ata_c.flags & AT_XFDONE) == 0) {
640 rv = CMD_ERR; 681 rv = CMD_ERR;
641 goto out; 682 goto out;
642 } 683 }
643 684
644 /* Read in parameter block. */ 685 /* Read in parameter block. */
645 memcpy(prms, tb, sizeof(struct ataparams)); 686 memcpy(prms, tb, sizeof(struct ataparams));
646 687
647 /* 688 /*
648 * Shuffle string byte order. 689 * Shuffle string byte order.
649 * ATAPI NEC, Mitsumi and Pioneer drives and 690 * ATAPI NEC, Mitsumi and Pioneer drives and
650 * old ATA TDK CompactFlash cards 691 * old ATA TDK CompactFlash cards
651 * have different byte order. 692 * have different byte order.
652 */ 693 */
653#if BYTE_ORDER == BIG_ENDIAN 694#if BYTE_ORDER == BIG_ENDIAN
654# define M(n) prms->atap_model[(n) ^ 1] 695# define M(n) prms->atap_model[(n) ^ 1]
655#else 696#else
656# define M(n) prms->atap_model[n] 697# define M(n) prms->atap_model[n]
657#endif 698#endif
658 if ( 699 if (
659#if BYTE_ORDER == BIG_ENDIAN 700#if BYTE_ORDER == BIG_ENDIAN
660 ! 701 !
661#endif 702#endif
662 ((drvp->drive_flags & DRIVE_ATAPI) ? 703 ((drvp->drive_flags & DRIVE_ATAPI) ?
663 ((M(0) == 'N' && M(1) == 'E') || 704 ((M(0) == 'N' && M(1) == 'E') ||
664 (M(0) == 'F' && M(1) == 'X') || 705 (M(0) == 'F' && M(1) == 'X') ||
665 (M(0) == 'P' && M(1) == 'i')) : 706 (M(0) == 'P' && M(1) == 'i')) :
666 ((M(0) == 'T' && M(1) == 'D' && M(2) == 'K')))) { 707 ((M(0) == 'T' && M(1) == 'D' && M(2) == 'K')))) {
667 rv = CMD_OK; 708 rv = CMD_OK;
668 goto out; 709 goto out;
669 } 710 }
670#undef M 711#undef M
671 for (i = 0; i < sizeof(prms->atap_model); i += 2) { 712 for (i = 0; i < sizeof(prms->atap_model); i += 2) {
672 p = (u_int16_t *)(prms->atap_model + i); 713 p = (u_int16_t *)(prms->atap_model + i);
673 *p = bswap16(*p); 714 *p = bswap16(*p);
674 } 715 }
675 for (i = 0; i < sizeof(prms->atap_serial); i += 2) { 716 for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
676 p = (u_int16_t *)(prms->atap_serial + i); 717 p = (u_int16_t *)(prms->atap_serial + i);
677 *p = bswap16(*p); 718 *p = bswap16(*p);
678 } 719 }
679 for (i = 0; i < sizeof(prms->atap_revision); i += 2) { 720 for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
680 p = (u_int16_t *)(prms->atap_revision + i); 721 p = (u_int16_t *)(prms->atap_revision + i);
681 *p = bswap16(*p); 722 *p = bswap16(*p);
682 } 723 }
683 724
684 rv = CMD_OK; 725 rv = CMD_OK;
685 out: 726 out:
686 kmem_free(tb, DEV_BSIZE); 727 kmem_free(tb, DEV_BSIZE);
687 return rv; 728 return rv;
688} 729}
689 730
690int 731int
691ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags) 732ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
692{ 733{
693 struct ata_command ata_c; 734 struct ata_command ata_c;
694 struct ata_channel *chp = drvp->chnl_softc; 735 struct ata_channel *chp = drvp->chnl_softc;
695 struct atac_softc *atac = chp->ch_atac; 736 struct atac_softc *atac = chp->ch_atac;
696 737
697 ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS); 738 ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
698 memset(&ata_c, 0, sizeof(struct ata_command)); 739 memset(&ata_c, 0, sizeof(struct ata_command));
699 740
700 ata_c.r_command = SET_FEATURES; 741 ata_c.r_command = SET_FEATURES;
701 ata_c.r_st_bmask = 0; 742 ata_c.r_st_bmask = 0;
702 ata_c.r_st_pmask = 0; 743 ata_c.r_st_pmask = 0;
703 ata_c.r_features = WDSF_SET_MODE; 744 ata_c.r_features = WDSF_SET_MODE;
704 ata_c.r_count = mode; 745 ata_c.r_count = mode;
705 ata_c.flags = flags; 746 ata_c.flags = flags;
706 ata_c.timeout = 1000; /* 1s */ 747 ata_c.timeout = 1000; /* 1s */
707 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp, 748 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
708 &ata_c) != ATACMD_COMPLETE) 749 &ata_c) != ATACMD_COMPLETE)
709 return CMD_AGAIN; 750 return CMD_AGAIN;
710 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 751 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
711 return CMD_ERR; 752 return CMD_ERR;
712 } 753 }
713 return CMD_OK; 754 return CMD_OK;
714} 755}
715 756
716#if NATA_DMA 757#if NATA_DMA
717void 758void
718ata_dmaerr(struct ata_drive_datas *drvp, int flags) 759ata_dmaerr(struct ata_drive_datas *drvp, int flags)
719{ 760{
720 /* 761 /*
721 * Downgrade decision: if we get NERRS_MAX in NXFER. 762 * Downgrade decision: if we get NERRS_MAX in NXFER.
722 * We start with n_dmaerrs set to NERRS_MAX-1 so that the 763 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
723 * first error within the first NXFER ops will immediatly trigger 764 * first error within the first NXFER ops will immediatly trigger
724 * a downgrade. 765 * a downgrade.
725 * If we got an error and n_xfers is bigger than NXFER reset counters. 766 * If we got an error and n_xfers is bigger than NXFER reset counters.
726 */ 767 */
727 drvp->n_dmaerrs++; 768 drvp->n_dmaerrs++;
728 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) { 769 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
729 ata_downgrade_mode(drvp, flags); 770 ata_downgrade_mode(drvp, flags);
730 drvp->n_dmaerrs = NERRS_MAX-1; 771 drvp->n_dmaerrs = NERRS_MAX-1;
731 drvp->n_xfers = 0; 772 drvp->n_xfers = 0;
732 return; 773 return;
733 } 774 }
734 if (drvp->n_xfers > NXFER) { 775 if (drvp->n_xfers > NXFER) {
735 drvp->n_dmaerrs = 1; /* just got an error */ 776 drvp->n_dmaerrs = 1; /* just got an error */
736 drvp->n_xfers = 1; /* restart counting from this error */ 777 drvp->n_xfers = 1; /* restart counting from this error */
737 } 778 }
738} 779}
739#endif /* NATA_DMA */ 780#endif /* NATA_DMA */
740 781
741/* 782/*
742 * freeze the queue and wait for the controller to be idle. Caller has to 783 * freeze the queue and wait for the controller to be idle. Caller has to
743 * unfreeze/restart the queue 784 * unfreeze/restart the queue
744 */ 785 */
745void 786void
746ata_queue_idle(struct ata_queue *queue) 787ata_queue_idle(struct ata_queue *queue)
747{ 788{
748 int s = splbio(); 789 int s = splbio();
749 queue->queue_freeze++; 790 queue->queue_freeze++;
750 while (queue->active_xfer != NULL) { 791 while (queue->active_xfer != NULL) {
751 queue->queue_flags |= QF_IDLE_WAIT; 792 queue->queue_flags |= QF_IDLE_WAIT;
752 tsleep(&queue->queue_flags, PRIBIO, "qidl", 0); 793 tsleep(&queue->queue_flags, PRIBIO, "qidl", 0);
753 } 794 }
754 splx(s); 795 splx(s);
755} 796}
756 797
757/* 798/*
758 * Add a command to the queue and start controller. 799 * Add a command to the queue and start controller.
759 * 800 *
760 * MUST BE CALLED AT splbio()! 801 * MUST BE CALLED AT splbio()!
761 */ 802 */
762void 803void
763ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer) 804ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
764{ 805{
765 806
766 ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer, 807 ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer,
767 chp->ch_channel, xfer->c_drive), DEBUG_XFERS); 808 chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
768 809
769 /* complete xfer setup */ 810 /* complete xfer setup */
770 xfer->c_chp = chp; 811 xfer->c_chp = chp;
771 812
772 /* insert at the end of command list */ 813 /* insert at the end of command list */
773 TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain); 814 TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
774 ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n", 815 ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n",
775 chp->ch_flags), DEBUG_XFERS); 816 chp->ch_flags), DEBUG_XFERS);
776 /* 817 /*
777 * if polling and can sleep, wait for the xfer to be at head of queue 818 * if polling and can sleep, wait for the xfer to be at head of queue
778 */ 819 */
779 if ((xfer->c_flags & (C_POLL | C_WAIT)) == (C_POLL | C_WAIT)) { 820 if ((xfer->c_flags & (C_POLL | C_WAIT)) == (C_POLL | C_WAIT)) {
780 while (chp->ch_queue->active_xfer != NULL || 821 while (chp->ch_queue->active_xfer != NULL ||
781 TAILQ_FIRST(&chp->ch_queue->queue_xfer) != xfer) { 822 TAILQ_FIRST(&chp->ch_queue->queue_xfer) != xfer) {
782 xfer->c_flags |= C_WAITACT; 823 xfer->c_flags |= C_WAITACT;
783 tsleep(xfer, PRIBIO, "ataact", 0); 824 tsleep(xfer, PRIBIO, "ataact", 0);
784 xfer->c_flags &= ~C_WAITACT; 825 xfer->c_flags &= ~C_WAITACT;
785 if (xfer->c_flags & C_FREE) { 826 if (xfer->c_flags & C_FREE) {
786 ata_free_xfer(chp, xfer); 827 ata_free_xfer(chp, xfer);
787 return; 828 return;
788 } 829 }
789 } 830 }
790 } 831 }
791 atastart(chp); 832 atastart(chp);
792} 833}
793 834
794/* 835/*
795 * Start I/O on a controller, for the given channel. 836 * Start I/O on a controller, for the given channel.
796 * The first xfer may be not for our channel if the channel queues 837 * The first xfer may be not for our channel if the channel queues
797 * are shared. 838 * are shared.
798 * 839 *
799 * MUST BE CALLED AT splbio()! 840 * MUST BE CALLED AT splbio()!
800 */ 841 */
801void 842void
802atastart(struct ata_channel *chp) 843atastart(struct ata_channel *chp)
803{ 844{
804 struct atac_softc *atac = chp->ch_atac; 845 struct atac_softc *atac = chp->ch_atac;
805 struct ata_xfer *xfer; 846 struct ata_xfer *xfer;
806 847
807#ifdef ATA_DEBUG 848#ifdef ATA_DEBUG
808 int spl1, spl2; 849 int spl1, spl2;
809 850
810 spl1 = splbio(); 851 spl1 = splbio();
811 spl2 = splbio(); 852 spl2 = splbio();
812 if (spl2 != spl1) { 853 if (spl2 != spl1) {
813 printf("atastart: not at splbio()\n"); 854 printf("atastart: not at splbio()\n");
814 panic("atastart"); 855 panic("atastart");
815 } 856 }
816 splx(spl2); 857 splx(spl2);
817 splx(spl1); 858 splx(spl1);
818#endif /* ATA_DEBUG */ 859#endif /* ATA_DEBUG */
819 860
820 /* is there a xfer ? */ 861 /* is there a xfer ? */
821 if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL) 862 if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL)
822 return; 863 return;
823 864
824 /* adjust chp, in case we have a shared queue */ 865 /* adjust chp, in case we have a shared queue */
825 chp = xfer->c_chp; 866 chp = xfer->c_chp;
826 867
827 if (chp->ch_queue->active_xfer != NULL) { 868 if (chp->ch_queue->active_xfer != NULL) {
828 return; /* channel aleady active */ 869 return; /* channel aleady active */
829 } 870 }
830 if (__predict_false(chp->ch_queue->queue_freeze > 0)) { 871 if (__predict_false(chp->ch_queue->queue_freeze > 0)) {
831 if (chp->ch_queue->queue_flags & QF_IDLE_WAIT) { 872 if (chp->ch_queue->queue_flags & QF_IDLE_WAIT) {
832 chp->ch_queue->queue_flags &= ~QF_IDLE_WAIT; 873 chp->ch_queue->queue_flags &= ~QF_IDLE_WAIT;
833 wakeup(&chp->ch_queue->queue_flags); 874 wakeup(&chp->ch_queue->queue_flags);
834 } 875 }
835 return; /* queue frozen */ 876 return; /* queue frozen */
836 } 877 }
837 /* 878 /*
838 * if someone is waiting for the command to be active, wake it up 879 * if someone is waiting for the command to be active, wake it up
839 * and let it process the command 880 * and let it process the command
840 */ 881 */
841 if (xfer->c_flags & C_WAITACT) { 882 if (xfer->c_flags & C_WAITACT) {
842 ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d " 883 ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d "
843 "wait active\n", xfer, chp->ch_channel, xfer->c_drive), 884 "wait active\n", xfer, chp->ch_channel, xfer->c_drive),
844 DEBUG_XFERS); 885 DEBUG_XFERS);
845 wakeup(xfer); 886 wakeup(xfer);
846 return; 887 return;
847 } 888 }
848#ifdef DIAGNOSTIC 889#ifdef DIAGNOSTIC
849 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) 890 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0)
850 panic("atastart: channel waiting for irq"); 891 panic("atastart: channel waiting for irq");
851#endif 892#endif
852 if (atac->atac_claim_hw) 893 if (atac->atac_claim_hw)
853 if (!(*atac->atac_claim_hw)(chp, 0)) 894 if (!(*atac->atac_claim_hw)(chp, 0))
854 return; 895 return;
855 896
856 ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer, 897 ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer,
857 chp->ch_channel, xfer->c_drive), DEBUG_XFERS); 898 chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
858 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) { 899 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) {
859 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET; 900 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET;
860 chp->ch_drive[xfer->c_drive].state = 0; 901 chp->ch_drive[xfer->c_drive].state = 0;
861 } 902 }
862 chp->ch_queue->active_xfer = xfer; 903 chp->ch_queue->active_xfer = xfer;
863 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain); 904 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
864 905
865 if (atac->atac_cap & ATAC_CAP_NOIRQ) 906 if (atac->atac_cap & ATAC_CAP_NOIRQ)
866 KASSERT(xfer->c_flags & C_POLL); 907 KASSERT(xfer->c_flags & C_POLL);
867 908
868 xfer->c_start(chp, xfer); 909 xfer->c_start(chp, xfer);
869} 910}
870 911
871struct ata_xfer * 912struct ata_xfer *
872ata_get_xfer(int flags) 913ata_get_xfer(int flags)
873{ 914{
874 struct ata_xfer *xfer; 915 struct ata_xfer *xfer;
875 int s; 916 int s;
876 917
877 s = splbio(); 918 s = splbio();
878 xfer = pool_get(&ata_xfer_pool, 919 xfer = pool_get(&ata_xfer_pool,
879 ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK)); 920 ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
880 splx(s); 921 splx(s);
881 if (xfer != NULL) { 922 if (xfer != NULL) {
882 memset(xfer, 0, sizeof(struct ata_xfer)); 923 memset(xfer, 0, sizeof(struct ata_xfer));
883 } 924 }
884 return xfer; 925 return xfer;
885} 926}
886 927
887void 928void
888ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer) 929ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
889{ 930{
890 struct atac_softc *atac = chp->ch_atac; 931 struct atac_softc *atac = chp->ch_atac;
891 int s; 932 int s;
892 933
893 if (xfer->c_flags & C_WAITACT) { 934 if (xfer->c_flags & C_WAITACT) {
894 /* Someone is waiting for this xfer, so we can't free now */ 935 /* Someone is waiting for this xfer, so we can't free now */
895 xfer->c_flags |= C_FREE; 936 xfer->c_flags |= C_FREE;
896 wakeup(xfer); 937 wakeup(xfer);
897 return; 938 return;
898 } 939 }
899 940
900#if NATA_PIOBM /* XXX wdc dependent code */ 941#if NATA_PIOBM /* XXX wdc dependent code */
901 if (xfer->c_flags & C_PIOBM) { 942 if (xfer->c_flags & C_PIOBM) {
902 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 943 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
903 944
904 /* finish the busmastering PIO */ 945 /* finish the busmastering PIO */
905 (*wdc->piobm_done)(wdc->dma_arg, 946 (*wdc->piobm_done)(wdc->dma_arg,
906 chp->ch_channel, xfer->c_drive); 947 chp->ch_channel, xfer->c_drive);
907 chp->ch_flags &= ~(ATACH_DMA_WAIT | ATACH_PIOBM_WAIT | ATACH_IRQ_WAIT); 948 chp->ch_flags &= ~(ATACH_DMA_WAIT | ATACH_PIOBM_WAIT | ATACH_IRQ_WAIT);
908 } 949 }
909#endif 950#endif
910 951
911 if (atac->atac_free_hw) 952 if (atac->atac_free_hw)
912 (*atac->atac_free_hw)(chp); 953 (*atac->atac_free_hw)(chp);
913 s = splbio(); 954 s = splbio();
914 pool_put(&ata_xfer_pool, xfer); 955 pool_put(&ata_xfer_pool, xfer);
915 splx(s); 956 splx(s);
916} 957}
917 958
918/* 959/*
919 * Kill off all pending xfers for a ata_channel. 960 * Kill off all pending xfers for a ata_channel.
920 * 961 *
921 * Must be called at splbio(). 962 * Must be called at splbio().
922 */ 963 */
923void 964void
924ata_kill_pending(struct ata_drive_datas *drvp) 965ata_kill_pending(struct ata_drive_datas *drvp)
925{ 966{
926 struct ata_channel *chp = drvp->chnl_softc; 967 struct ata_channel *chp = drvp->chnl_softc;
927 struct ata_xfer *xfer, *next_xfer; 968 struct ata_xfer *xfer, *next_xfer;
928 int s = splbio(); 969 int s = splbio();
929 970
930 for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer); 971 for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
931 xfer != NULL; xfer = next_xfer) { 972 xfer != NULL; xfer = next_xfer) {
932 next_xfer = TAILQ_NEXT(xfer, c_xferchain); 973 next_xfer = TAILQ_NEXT(xfer, c_xferchain);
933 if (xfer->c_chp != chp || xfer->c_drive != drvp->drive) 974 if (xfer->c_chp != chp || xfer->c_drive != drvp->drive)
934 continue; 975 continue;
935 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain); 976 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
936 (*xfer->c_kill_xfer)(chp, xfer, KILL_GONE); 977 (*xfer->c_kill_xfer)(chp, xfer, KILL_GONE);
937 } 978 }
938 979
939 while ((xfer = chp->ch_queue->active_xfer) != NULL) { 980 while ((xfer = chp->ch_queue->active_xfer) != NULL) {
940 if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) { 981 if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) {
941 drvp->drive_flags |= DRIVE_WAITDRAIN; 982 drvp->drive_flags |= DRIVE_WAITDRAIN;
942 (void) tsleep(&chp->ch_queue->active_xfer, 983 (void) tsleep(&chp->ch_queue->active_xfer,
943 PRIBIO, "atdrn", 0); 984 PRIBIO, "atdrn", 0);
944 } else { 985 } else {
945 /* no more xfer for us */ 986 /* no more xfer for us */
946 break; 987 break;
947 } 988 }
948 } 989 }
949 splx(s); 990 splx(s);
950} 991}
951 992
952/* 993/*
953 * ata_reset_channel: 994 * ata_reset_channel:
954 * 995 *
955 * Reset and ATA channel. 996 * Reset and ATA channel.
956 * 997 *
957 * MUST BE CALLED AT splbio()! 998 * MUST BE CALLED AT splbio()!
958 */ 999 */
959void 1000void
960ata_reset_channel(struct ata_channel *chp, int flags) 1001ata_reset_channel(struct ata_channel *chp, int flags)
961{ 1002{
962 struct atac_softc *atac = chp->ch_atac; 1003 struct atac_softc *atac = chp->ch_atac;
963 int drive; 1004 int drive;
964 1005
965#ifdef ATA_DEBUG 1006#ifdef ATA_DEBUG
966 int spl1, spl2; 1007 int spl1, spl2;
967 1008
968 spl1 = splbio(); 1009 spl1 = splbio();
969 spl2 = splbio(); 1010 spl2 = splbio();
970 if (spl2 != spl1) { 1011 if (spl2 != spl1) {
971 printf("ata_reset_channel: not at splbio()\n"); 1012 printf("ata_reset_channel: not at splbio()\n");
972 panic("ata_reset_channel"); 1013 panic("ata_reset_channel");
973 } 1014 }
974 splx(spl2); 1015 splx(spl2);
975 splx(spl1); 1016 splx(spl1);
976#endif /* ATA_DEBUG */ 1017#endif /* ATA_DEBUG */
977 1018
978 chp->ch_queue->queue_freeze++; 1019 chp->ch_queue->queue_freeze++;
979 1020
980 /* 1021 /*
981 * If we can poll or wait it's OK, otherwise wake up the 1022 * If we can poll or wait it's OK, otherwise wake up the
982 * kernel thread to do it for us. 1023 * kernel thread to do it for us.
983 */ 1024 */
 1025 ATADEBUG_PRINT(("ata_reset_channel flags 0x%x ch_flags 0x%x\n",
 1026 flags, chp->ch_flags), DEBUG_FUNCS | DEBUG_XFERS);
984 if ((flags & (AT_POLL | AT_WAIT)) == 0) { 1027 if ((flags & (AT_POLL | AT_WAIT)) == 0) {
985 if (chp->ch_flags & ATACH_TH_RESET) { 1028 if (chp->ch_flags & ATACH_TH_RESET) {
986 /* No need to schedule a reset more than one time. */ 1029 /* No need to schedule a reset more than one time. */
987 chp->ch_queue->queue_freeze--; 1030 chp->ch_queue->queue_freeze--;
988 return; 1031 return;
989 } 1032 }
990 chp->ch_flags |= ATACH_TH_RESET; 1033 chp->ch_flags |= ATACH_TH_RESET;
991 chp->ch_reset_flags = flags & (AT_RST_EMERG | AT_RST_NOCMD); 1034 chp->ch_reset_flags = flags & (AT_RST_EMERG | AT_RST_NOCMD);
992 wakeup(&chp->ch_thread); 1035 wakeup(&chp->ch_thread);
993 return; 1036 return;
994 } 1037 }
995 1038
996 (*atac->atac_bustype_ata->ata_reset_channel)(chp, flags); 1039 (*atac->atac_bustype_ata->ata_reset_channel)(chp, flags);
997 1040
998 for (drive = 0; drive < chp->ch_ndrive; drive++) 1041 for (drive = 0; drive < chp->ch_ndrive; drive++)
999 chp->ch_drive[drive].state = 0; 1042 chp->ch_drive[drive].state = 0;
1000 1043
1001 chp->ch_flags &= ~ATACH_TH_RESET; 1044 chp->ch_flags &= ~ATACH_TH_RESET;
1002 if ((flags & AT_RST_EMERG) == 0) { 1045 if ((flags & AT_RST_EMERG) == 0) {
1003 chp->ch_queue->queue_freeze--; 1046 chp->ch_queue->queue_freeze--;
1004 atastart(chp); 1047 atastart(chp);
1005 } else { 1048 } else {
1006 /* make sure that we can use polled commands */ 1049 /* make sure that we can use polled commands */
1007 TAILQ_INIT(&chp->ch_queue->queue_xfer); 1050 TAILQ_INIT(&chp->ch_queue->queue_xfer);
1008 chp->ch_queue->queue_freeze = 0; 1051 chp->ch_queue->queue_freeze = 0;
1009 chp->ch_queue->active_xfer = NULL; 1052 chp->ch_queue->active_xfer = NULL;
1010 } 1053 }
1011} 1054}
1012 1055
1013int 1056int
1014ata_addref(struct ata_channel *chp) 1057ata_addref(struct ata_channel *chp)
1015{ 1058{
1016 struct atac_softc *atac = chp->ch_atac; 1059 struct atac_softc *atac = chp->ch_atac;
1017 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 1060 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1018 int s, error = 0; 1061 int s, error = 0;
1019 1062
1020 s = splbio(); 1063 s = splbio();
1021 if (adapt->adapt_refcnt++ == 0 && 1064 if (adapt->adapt_refcnt++ == 0 &&
1022 adapt->adapt_enable != NULL) { 1065 adapt->adapt_enable != NULL) {
1023 error = (*adapt->adapt_enable)(atac->atac_dev, 1); 1066 error = (*adapt->adapt_enable)(atac->atac_dev, 1);
1024 if (error) 1067 if (error)
1025 adapt->adapt_refcnt--; 1068 adapt->adapt_refcnt--;
1026 } 1069 }
1027 splx(s); 1070 splx(s);
1028 return (error); 1071 return (error);
1029} 1072}
1030 1073
1031void 1074void
1032ata_delref(struct ata_channel *chp) 1075ata_delref(struct ata_channel *chp)
1033{ 1076{
1034 struct atac_softc *atac = chp->ch_atac; 1077 struct atac_softc *atac = chp->ch_atac;
1035 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 1078 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1036 int s; 1079 int s;
1037 1080
1038 s = splbio(); 1081 s = splbio();
1039 if (adapt->adapt_refcnt-- == 1 && 1082 if (adapt->adapt_refcnt-- == 1 &&
1040 adapt->adapt_enable != NULL) 1083 adapt->adapt_enable != NULL)
1041 (void) (*adapt->adapt_enable)(atac->atac_dev, 0); 1084 (void) (*adapt->adapt_enable)(atac->atac_dev, 0);
1042 splx(s); 1085 splx(s);
1043} 1086}
1044 1087
1045void 1088void
1046ata_print_modes(struct ata_channel *chp) 1089ata_print_modes(struct ata_channel *chp)
1047{ 1090{
1048 struct atac_softc *atac = chp->ch_atac; 1091 struct atac_softc *atac = chp->ch_atac;
1049 int drive; 1092 int drive;
1050 struct ata_drive_datas *drvp; 1093 struct ata_drive_datas *drvp;
1051 1094
1052 for (drive = 0; drive < chp->ch_ndrive; drive++) { 1095 for (drive = 0; drive < chp->ch_ndrive; drive++) {
1053 drvp = &chp->ch_drive[drive]; 1096 drvp = &chp->ch_drive[drive];
1054 if ((drvp->drive_flags & DRIVE) == 0 || drvp->drv_softc == NULL) 1097 if ((drvp->drive_flags & DRIVE) == 0 || drvp->drv_softc == NULL)
1055 continue; 1098 continue;
1056 aprint_verbose("%s(%s:%d:%d): using PIO mode %d", 1099 aprint_verbose("%s(%s:%d:%d): using PIO mode %d",
1057 device_xname(drvp->drv_softc), 1100 device_xname(drvp->drv_softc),
1058 device_xname(atac->atac_dev), 1101 device_xname(atac->atac_dev),
1059 chp->ch_channel, drvp->drive, drvp->PIO_mode); 1102 chp->ch_channel, drvp->drive, drvp->PIO_mode);
1060#if NATA_DMA 1103#if NATA_DMA
1061 if (drvp->drive_flags & DRIVE_DMA) 1104 if (drvp->drive_flags & DRIVE_DMA)
1062 aprint_verbose(", DMA mode %d", drvp->DMA_mode); 1105 aprint_verbose(", DMA mode %d", drvp->DMA_mode);
1063#if NATA_UDMA 1106#if NATA_UDMA
1064 if (drvp->drive_flags & DRIVE_UDMA) { 1107 if (drvp->drive_flags & DRIVE_UDMA) {
1065 aprint_verbose(", Ultra-DMA mode %d", drvp->UDMA_mode); 1108 aprint_verbose(", Ultra-DMA mode %d", drvp->UDMA_mode);
1066 if (drvp->UDMA_mode == 2) 1109 if (drvp->UDMA_mode == 2)
1067 aprint_verbose(" (Ultra/33)"); 1110 aprint_verbose(" (Ultra/33)");
1068 else if (drvp->UDMA_mode == 4) 1111 else if (drvp->UDMA_mode == 4)
1069 aprint_verbose(" (Ultra/66)"); 1112 aprint_verbose(" (Ultra/66)");
1070 else if (drvp->UDMA_mode == 5) 1113 else if (drvp->UDMA_mode == 5)
1071 aprint_verbose(" (Ultra/100)"); 1114 aprint_verbose(" (Ultra/100)");
1072 else if (drvp->UDMA_mode == 6) 1115 else if (drvp->UDMA_mode == 6)
1073 aprint_verbose(" (Ultra/133)"); 1116 aprint_verbose(" (Ultra/133)");
1074 } 1117 }
1075#endif /* NATA_UDMA */ 1118#endif /* NATA_UDMA */
1076#endif /* NATA_DMA */ 1119#endif /* NATA_DMA */
1077#if NATA_DMA || NATA_PIOBM 1120#if NATA_DMA || NATA_PIOBM
1078 if (0 1121 if (0
1079#if NATA_DMA 1122#if NATA_DMA
1080 || (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) 1123 || (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
1081#endif 1124#endif
1082#if NATA_PIOBM 1125#if NATA_PIOBM
1083 /* PIOBM capable controllers use DMA for PIO commands */ 1126 /* PIOBM capable controllers use DMA for PIO commands */
1084 || (atac->atac_cap & ATAC_CAP_PIOBM) 1127 || (atac->atac_cap & ATAC_CAP_PIOBM)
1085#endif 1128#endif
1086 ) 1129 )
1087 aprint_verbose(" (using DMA)"); 1130 aprint_verbose(" (using DMA)");
1088#endif /* NATA_DMA || NATA_PIOBM */ 1131#endif /* NATA_DMA || NATA_PIOBM */
1089 aprint_verbose("\n"); 1132 aprint_verbose("\n");
1090 } 1133 }
1091} 1134}
1092 1135
1093#if NATA_DMA 1136#if NATA_DMA
1094/* 1137/*
1095 * downgrade the transfer mode of a drive after an error. return 1 if 1138 * downgrade the transfer mode of a drive after an error. return 1 if
1096 * downgrade was possible, 0 otherwise. 1139 * downgrade was possible, 0 otherwise.
1097 * 1140 *
1098 * MUST BE CALLED AT splbio()! 1141 * MUST BE CALLED AT splbio()!
1099 */ 1142 */
1100int 1143int
1101ata_downgrade_mode(struct ata_drive_datas *drvp, int flags) 1144ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
1102{ 1145{
1103 struct ata_channel *chp = drvp->chnl_softc; 1146 struct ata_channel *chp = drvp->chnl_softc;
1104 struct atac_softc *atac = chp->ch_atac; 1147 struct atac_softc *atac = chp->ch_atac;
1105 device_t drv_dev = drvp->drv_softc; 1148 device_t drv_dev = drvp->drv_softc;
1106 int cf_flags = device_cfdata(drv_dev)->cf_flags; 1149 int cf_flags = device_cfdata(drv_dev)->cf_flags;
1107 1150
1108 /* if drive or controller don't know its mode, we can't do much */ 1151 /* if drive or controller don't know its mode, we can't do much */
1109 if ((drvp->drive_flags & DRIVE_MODE) == 0 || 1152 if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
1110 (atac->atac_set_modes == NULL)) 1153 (atac->atac_set_modes == NULL))
1111 return 0; 1154 return 0;
1112 /* current drive mode was set by a config flag, let it this way */ 1155 /* current drive mode was set by a config flag, let it this way */
1113 if ((cf_flags & ATA_CONFIG_PIO_SET) || 1156 if ((cf_flags & ATA_CONFIG_PIO_SET) ||
1114 (cf_flags & ATA_CONFIG_DMA_SET) || 1157 (cf_flags & ATA_CONFIG_DMA_SET) ||
1115 (cf_flags & ATA_CONFIG_UDMA_SET)) 1158 (cf_flags & ATA_CONFIG_UDMA_SET))
1116 return 0; 1159 return 0;
1117 1160
1118#if NATA_UDMA 1161#if NATA_UDMA
1119 /* 1162 /*
1120 * If we were using Ultra-DMA mode, downgrade to the next lower mode. 1163 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
1121 */ 1164 */
1122 if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) { 1165 if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
1123 drvp->UDMA_mode--; 1166 drvp->UDMA_mode--;
1124 aprint_error_dev(drv_dev, 1167 aprint_error_dev(drv_dev,
1125 "transfer error, downgrading to Ultra-DMA mode %d\n", 1168 "transfer error, downgrading to Ultra-DMA mode %d\n",
1126 drvp->UDMA_mode); 1169 drvp->UDMA_mode);
1127 } 1170 }
1128#endif 1171#endif
1129 1172
1130 /* 1173 /*
1131 * If we were using ultra-DMA, don't downgrade to multiword DMA. 1174 * If we were using ultra-DMA, don't downgrade to multiword DMA.
1132 */ 1175 */
1133 else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) { 1176 else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
1134 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); 1177 drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
1135 drvp->PIO_mode = drvp->PIO_cap; 1178 drvp->PIO_mode = drvp->PIO_cap;
1136 aprint_error_dev(drv_dev, 1179 aprint_error_dev(drv_dev,
1137 "transfer error, downgrading to PIO mode %d\n", 1180 "transfer error, downgrading to PIO mode %d\n",
1138 drvp->PIO_mode); 1181 drvp->PIO_mode);
1139 } else /* already using PIO, can't downgrade */ 1182 } else /* already using PIO, can't downgrade */
1140 return 0; 1183 return 0;
1141 1184
1142 (*atac->atac_set_modes)(chp); 1185 (*atac->atac_set_modes)(chp);
1143 ata_print_modes(chp); 1186 ata_print_modes(chp);
1144 /* reset the channel, which will schedule all drives for setup */ 1187 /* reset the channel, which will schedule all drives for setup */
1145 ata_reset_channel(chp, flags | AT_RST_NOCMD); 1188 ata_reset_channel(chp, flags | AT_RST_NOCMD);
1146 return 1; 1189 return 1;
1147} 1190}
1148#endif /* NATA_DMA */ 1191#endif /* NATA_DMA */
1149 1192
1150/* 1193/*
1151 * Probe drive's capabilities, for use by the controller later 1194 * Probe drive's capabilities, for use by the controller later
1152 * Assumes drvp points to an existing drive. 1195 * Assumes drvp points to an existing drive.
1153 */ 1196 */
1154void 1197void
1155ata_probe_caps(struct ata_drive_datas *drvp) 1198ata_probe_caps(struct ata_drive_datas *drvp)
1156{ 1199{
1157 struct ataparams params, params2; 1200 struct ataparams params, params2;
1158 struct ata_channel *chp = drvp->chnl_softc; 1201 struct ata_channel *chp = drvp->chnl_softc;
1159 struct atac_softc *atac = chp->ch_atac; 1202 struct atac_softc *atac = chp->ch_atac;
1160 device_t drv_dev = drvp->drv_softc; 1203 device_t drv_dev = drvp->drv_softc;
1161 int i, printed, s; 1204 int i, printed, s;
1162 const char *sep = ""; 1205 const char *sep = "";
1163 int cf_flags; 1206 int cf_flags;
1164 1207
1165 if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) { 1208 if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) {
1166 /* IDENTIFY failed. Can't tell more about the device */ 1209 /* IDENTIFY failed. Can't tell more about the device */
1167 return; 1210 return;
1168 } 1211 }
1169 if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) == 1212 if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) ==
1170 (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) { 1213 (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) {
1171 /* 1214 /*
1172 * Controller claims 16 and 32 bit transfers. 1215 * Controller claims 16 and 32 bit transfers.
1173 * Re-do an IDENTIFY with 32-bit transfers, 1216 * Re-do an IDENTIFY with 32-bit transfers,
1174 * and compare results. 1217 * and compare results.
1175 */ 1218 */
1176 s = splbio(); 1219 s = splbio();
1177 drvp->drive_flags |= DRIVE_CAP32; 1220 drvp->drive_flags |= DRIVE_CAP32;
1178 splx(s); 1221 splx(s);
1179 ata_get_params(drvp, AT_WAIT, &params2); 1222 ata_get_params(drvp, AT_WAIT, &params2);
1180 if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) { 1223 if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) {
1181 /* Not good. fall back to 16bits */ 1224 /* Not good. fall back to 16bits */
1182 s = splbio(); 1225 s = splbio();
1183 drvp->drive_flags &= ~DRIVE_CAP32; 1226 drvp->drive_flags &= ~DRIVE_CAP32;
1184 splx(s); 1227 splx(s);
1185 } else { 1228 } else {
1186 aprint_verbose_dev(drv_dev, "32-bit data port\n"); 1229 aprint_verbose_dev(drv_dev, "32-bit data port\n");
1187 } 1230 }
1188 } 1231 }
1189#if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */ 1232#if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
1190 if (params.atap_ata_major > 0x01 && 1233 if (params.atap_ata_major > 0x01 &&
1191 params.atap_ata_major != 0xffff) { 1234 params.atap_ata_major != 0xffff) {
1192 for (i = 14; i > 0; i--) { 1235 for (i = 14; i > 0; i--) {
1193 if (params.atap_ata_major & (1 << i)) { 1236 if (params.atap_ata_major & (1 << i)) {
1194 aprint_verbose_dev(drv_dev, 1237 aprint_verbose_dev(drv_dev,
1195 "ATA version %d\n", i); 1238 "ATA version %d\n", i);
1196 drvp->ata_vers = i; 1239 drvp->ata_vers = i;
1197 break; 1240 break;
1198 } 1241 }
1199 } 1242 }
1200 } 1243 }
1201#endif 1244#endif
1202 1245
1203 /* An ATAPI device is at last PIO mode 3 */ 1246 /* An ATAPI device is at last PIO mode 3 */
1204 if (drvp->drive_flags & DRIVE_ATAPI) 1247 if (drvp->drive_flags & DRIVE_ATAPI)
1205 drvp->PIO_mode = 3; 1248 drvp->PIO_mode = 3;
1206 1249
1207 /* 1250 /*
1208 * It's not in the specs, but it seems that some drive 1251 * It's not in the specs, but it seems that some drive
1209 * returns 0xffff in atap_extensions when this field is invalid 1252 * returns 0xffff in atap_extensions when this field is invalid
1210 */ 1253 */
1211 if (params.atap_extensions != 0xffff && 1254 if (params.atap_extensions != 0xffff &&
1212 (params.atap_extensions & WDC_EXT_MODES)) { 1255 (params.atap_extensions & WDC_EXT_MODES)) {
1213 printed = 0; 1256 printed = 0;
1214 /* 1257 /*
1215 * XXX some drives report something wrong here (they claim to 1258 * XXX some drives report something wrong here (they claim to
1216 * support PIO mode 8 !). As mode is coded on 3 bits in 1259 * support PIO mode 8 !). As mode is coded on 3 bits in
1217 * SET FEATURE, limit it to 7 (so limit i to 4). 1260 * SET FEATURE, limit it to 7 (so limit i to 4).
1218 * If higher mode than 7 is found, abort. 1261 * If higher mode than 7 is found, abort.
1219 */ 1262 */
1220 for (i = 7; i >= 0; i--) { 1263 for (i = 7; i >= 0; i--) {
1221 if ((params.atap_piomode_supp & (1 << i)) == 0) 1264 if ((params.atap_piomode_supp & (1 << i)) == 0)
1222 continue; 1265 continue;
1223 if (i > 4) 1266 if (i > 4)
1224 return; 1267 return;
1225 /* 1268 /*
1226 * See if mode is accepted. 1269 * See if mode is accepted.
1227 * If the controller can't set its PIO mode, 1270 * If the controller can't set its PIO mode,
1228 * assume the defaults are good, so don't try 1271 * assume the defaults are good, so don't try
1229 * to set it 1272 * to set it
1230 */ 1273 */
1231 if (atac->atac_set_modes) 1274 if (atac->atac_set_modes)
1232 /* 1275 /*
1233 * It's OK to pool here, it's fast enouth 1276 * It's OK to pool here, it's fast enouth
1234 * to not bother waiting for interrupt 1277 * to not bother waiting for interrupt
1235 */ 1278 */
1236 if (ata_set_mode(drvp, 0x08 | (i + 3), 1279 if (ata_set_mode(drvp, 0x08 | (i + 3),
1237 AT_WAIT) != CMD_OK) 1280 AT_WAIT) != CMD_OK)
1238 continue; 1281 continue;
1239 if (!printed) { 1282 if (!printed) {
1240 aprint_verbose_dev(drv_dev, 1283 aprint_verbose_dev(drv_dev,
1241 "drive supports PIO mode %d", i + 3); 1284 "drive supports PIO mode %d", i + 3);
1242 sep = ","; 1285 sep = ",";
1243 printed = 1; 1286 printed = 1;
1244 } 1287 }
1245 /* 1288 /*
1246 * If controller's driver can't set its PIO mode, 1289 * If controller's driver can't set its PIO mode,
1247 * get the highter one for the drive. 1290 * get the highter one for the drive.
1248 */ 1291 */
1249 if (atac->atac_set_modes == NULL || 1292 if (atac->atac_set_modes == NULL ||
1250 atac->atac_pio_cap >= i + 3) { 1293 atac->atac_pio_cap >= i + 3) {
1251 drvp->PIO_mode = i + 3; 1294 drvp->PIO_mode = i + 3;
1252 drvp->PIO_cap = i + 3; 1295 drvp->PIO_cap = i + 3;
1253 break; 1296 break;
1254 } 1297 }
1255 } 1298 }
1256 if (!printed) { 1299 if (!printed) {
1257 /* 1300 /*
1258 * We didn't find a valid PIO mode. 1301 * We didn't find a valid PIO mode.
1259 * Assume the values returned for DMA are buggy too 1302 * Assume the values returned for DMA are buggy too
1260 */ 1303 */
1261 return; 1304 return;
1262 } 1305 }
1263 s = splbio(); 1306 s = splbio();
1264 drvp->drive_flags |= DRIVE_MODE; 1307 drvp->drive_flags |= DRIVE_MODE;
1265 splx(s); 1308 splx(s);
1266 printed = 0; 1309 printed = 0;
1267 for (i = 7; i >= 0; i--) { 1310 for (i = 7; i >= 0; i--) {
1268 if ((params.atap_dmamode_supp & (1 << i)) == 0) 1311 if ((params.atap_dmamode_supp & (1 << i)) == 0)
1269 continue; 1312 continue;
1270#if NATA_DMA 1313#if NATA_DMA
1271 if ((atac->atac_cap & ATAC_CAP_DMA) && 1314 if ((atac->atac_cap & ATAC_CAP_DMA) &&
1272 atac->atac_set_modes != NULL) 1315 atac->atac_set_modes != NULL)
1273 if (ata_set_mode(drvp, 0x20 | i, AT_WAIT) 1316 if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
1274 != CMD_OK) 1317 != CMD_OK)
1275 continue; 1318 continue;
1276#endif 1319#endif
1277 if (!printed) { 1320 if (!printed) {
1278 aprint_verbose("%s DMA mode %d", sep, i); 1321 aprint_verbose("%s DMA mode %d", sep, i);
1279 sep = ","; 1322 sep = ",";
1280 printed = 1; 1323 printed = 1;
1281 } 1324 }
1282#if NATA_DMA 1325#if NATA_DMA
1283 if (atac->atac_cap & ATAC_CAP_DMA) { 1326 if (atac->atac_cap & ATAC_CAP_DMA) {
1284 if (atac->atac_set_modes != NULL && 1327 if (atac->atac_set_modes != NULL &&
1285 atac->atac_dma_cap < i) 1328 atac->atac_dma_cap < i)
1286 continue; 1329 continue;
1287 drvp->DMA_mode = i; 1330 drvp->DMA_mode = i;
1288 drvp->DMA_cap = i; 1331 drvp->DMA_cap = i;
1289 s = splbio(); 1332 s = splbio();
1290 drvp->drive_flags |= DRIVE_DMA; 1333 drvp->drive_flags |= DRIVE_DMA;
1291 splx(s); 1334 splx(s);
1292 } 1335 }
1293#endif 1336#endif
1294 break; 1337 break;
1295 } 1338 }
1296 if (params.atap_extensions & WDC_EXT_UDMA_MODES) { 1339 if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
1297 printed = 0; 1340 printed = 0;
1298 for (i = 7; i >= 0; i--) { 1341 for (i = 7; i >= 0; i--) {
1299 if ((params.atap_udmamode_supp & (1 << i)) 1342 if ((params.atap_udmamode_supp & (1 << i))
1300 == 0) 1343 == 0)
1301 continue; 1344 continue;
1302#if NATA_UDMA 1345#if NATA_UDMA
1303 if (atac->atac_set_modes != NULL && 1346 if (atac->atac_set_modes != NULL &&
1304 (atac->atac_cap & ATAC_CAP_UDMA)) 1347 (atac->atac_cap & ATAC_CAP_UDMA))
1305 if (ata_set_mode(drvp, 0x40 | i, 1348 if (ata_set_mode(drvp, 0x40 | i,
1306 AT_WAIT) != CMD_OK) 1349 AT_WAIT) != CMD_OK)
1307 continue; 1350 continue;
1308#endif 1351#endif
1309 if (!printed) { 1352 if (!printed) {
1310 aprint_verbose("%s Ultra-DMA mode %d", 1353 aprint_verbose("%s Ultra-DMA mode %d",
1311 sep, i); 1354 sep, i);
1312 if (i == 2) 1355 if (i == 2)
1313 aprint_verbose(" (Ultra/33)"); 1356 aprint_verbose(" (Ultra/33)");
1314 else if (i == 4) 1357 else if (i == 4)
1315 aprint_verbose(" (Ultra/66)"); 1358 aprint_verbose(" (Ultra/66)");
1316 else if (i == 5) 1359 else if (i == 5)
1317 aprint_verbose(" (Ultra/100)"); 1360 aprint_verbose(" (Ultra/100)");
1318 else if (i == 6) 1361 else if (i == 6)
1319 aprint_verbose(" (Ultra/133)"); 1362 aprint_verbose(" (Ultra/133)");
1320 sep = ","; 1363 sep = ",";
1321 printed = 1; 1364 printed = 1;
1322 } 1365 }
1323#if NATA_UDMA 1366#if NATA_UDMA
1324 if (atac->atac_cap & ATAC_CAP_UDMA) { 1367 if (atac->atac_cap & ATAC_CAP_UDMA) {
1325 if (atac->atac_set_modes != NULL && 1368 if (atac->atac_set_modes != NULL &&
1326 atac->atac_udma_cap < i) 1369 atac->atac_udma_cap < i)
1327 continue; 1370 continue;
1328 drvp->UDMA_mode = i; 1371 drvp->UDMA_mode = i;
1329 drvp->UDMA_cap = i; 1372 drvp->UDMA_cap = i;
1330 s = splbio(); 1373 s = splbio();
1331 drvp->drive_flags |= DRIVE_UDMA; 1374 drvp->drive_flags |= DRIVE_UDMA;
1332 splx(s); 1375 splx(s);
1333 } 1376 }
1334#endif 1377#endif
1335 break; 1378 break;
1336 } 1379 }
1337 } 1380 }
1338 aprint_verbose("\n"); 1381 aprint_verbose("\n");
1339 } 1382 }
1340 1383
1341 s = splbio(); 1384 s = splbio();
1342 drvp->drive_flags &= ~DRIVE_NOSTREAM; 1385 drvp->drive_flags &= ~DRIVE_NOSTREAM;
1343 if (drvp->drive_flags & DRIVE_ATAPI) { 1386 if (drvp->drive_flags & DRIVE_ATAPI) {
1344 if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM) 1387 if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM)
1345 drvp->drive_flags |= DRIVE_NOSTREAM; 1388 drvp->drive_flags |= DRIVE_NOSTREAM;
1346 } else { 1389 } else {
1347 if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM) 1390 if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM)
1348 drvp->drive_flags |= DRIVE_NOSTREAM; 1391 drvp->drive_flags |= DRIVE_NOSTREAM;
1349 } 1392 }
1350 splx(s); 1393 splx(s);
1351 1394
1352 /* Try to guess ATA version here, if it didn't get reported */ 1395 /* Try to guess ATA version here, if it didn't get reported */
1353 if (drvp->ata_vers == 0) { 1396 if (drvp->ata_vers == 0) {
1354#if NATA_UDMA 1397#if NATA_UDMA
1355 if (drvp->drive_flags & DRIVE_UDMA) 1398 if (drvp->drive_flags & DRIVE_UDMA)
1356 drvp->ata_vers = 4; /* should be at last ATA-4 */ 1399 drvp->ata_vers = 4; /* should be at last ATA-4 */
1357 else 1400 else
1358#endif 1401#endif
1359 if (drvp->PIO_cap > 2) 1402 if (drvp->PIO_cap > 2)
1360 drvp->ata_vers = 2; /* should be at last ATA-2 */ 1403 drvp->ata_vers = 2; /* should be at last ATA-2 */
1361 } 1404 }
1362 cf_flags = device_cfdata(drv_dev)->cf_flags; 1405 cf_flags = device_cfdata(drv_dev)->cf_flags;
1363 if (cf_flags & ATA_CONFIG_PIO_SET) { 1406 if (cf_flags & ATA_CONFIG_PIO_SET) {
1364 s = splbio(); 1407 s = splbio();
1365 drvp->PIO_mode = 1408 drvp->PIO_mode =
1366 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF; 1409 (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
1367 drvp->drive_flags |= DRIVE_MODE; 1410 drvp->drive_flags |= DRIVE_MODE;
1368 splx(s); 1411 splx(s);
1369 } 1412 }
1370#if NATA_DMA 1413#if NATA_DMA
1371 if ((atac->atac_cap & ATAC_CAP_DMA) == 0) { 1414 if ((atac->atac_cap & ATAC_CAP_DMA) == 0) {
1372 /* don't care about DMA modes */ 1415 /* don't care about DMA modes */
1373 return; 1416 return;
1374 } 1417 }
1375 if (cf_flags & ATA_CONFIG_DMA_SET) { 1418 if (cf_flags & ATA_CONFIG_DMA_SET) {
1376 s = splbio(); 1419 s = splbio();
1377 if ((cf_flags & ATA_CONFIG_DMA_MODES) == 1420 if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
1378 ATA_CONFIG_DMA_DISABLE) { 1421 ATA_CONFIG_DMA_DISABLE) {
1379 drvp->drive_flags &= ~DRIVE_DMA; 1422 drvp->drive_flags &= ~DRIVE_DMA;
1380 } else { 1423 } else {
1381 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >> 1424 drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
1382 ATA_CONFIG_DMA_OFF; 1425 ATA_CONFIG_DMA_OFF;
1383 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE; 1426 drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
1384 } 1427 }
1385 splx(s); 1428 splx(s);
1386 } 1429 }
1387#if NATA_UDMA 1430#if NATA_UDMA
1388 if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) { 1431 if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) {
1389 /* don't care about UDMA modes */ 1432 /* don't care about UDMA modes */
1390 return; 1433 return;
1391 } 1434 }
1392 if (cf_flags & ATA_CONFIG_UDMA_SET) { 1435 if (cf_flags & ATA_CONFIG_UDMA_SET) {
1393 s = splbio(); 1436 s = splbio();
1394 if ((cf_flags & ATA_CONFIG_UDMA_MODES) == 1437 if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
1395 ATA_CONFIG_UDMA_DISABLE) { 1438 ATA_CONFIG_UDMA_DISABLE) {
1396 drvp->drive_flags &= ~DRIVE_UDMA; 1439 drvp->drive_flags &= ~DRIVE_UDMA;
1397 } else { 1440 } else {
1398 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >> 1441 drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
1399 ATA_CONFIG_UDMA_OFF; 1442 ATA_CONFIG_UDMA_OFF;
1400 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE; 1443 drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
1401 } 1444 }
1402 splx(s); 1445 splx(s);
1403 } 1446 }
1404#endif /* NATA_UDMA */ 1447#endif /* NATA_UDMA */
1405#endif /* NATA_DMA */ 1448#endif /* NATA_DMA */
1406} 1449}
1407 1450
1408/* management of the /dev/atabus* devices */ 1451/* management of the /dev/atabus* devices */
1409int 1452int
1410atabusopen(dev_t dev, int flag, int fmt, 1453atabusopen(dev_t dev, int flag, int fmt,
1411 struct lwp *l) 1454 struct lwp *l)
1412{ 1455{
1413 struct atabus_softc *sc; 1456 struct atabus_softc *sc;
1414 int error; 1457 int error;
1415 1458
1416 sc = device_lookup_private(&atabus_cd, minor(dev)); 1459 sc = device_lookup_private(&atabus_cd, minor(dev));
1417 if (sc == NULL) 1460 if (sc == NULL)
1418 return (ENXIO); 1461 return (ENXIO);
1419 1462
1420 if (sc->sc_flags & ATABUSCF_OPEN) 1463 if (sc->sc_flags & ATABUSCF_OPEN)
1421 return (EBUSY); 1464 return (EBUSY);
1422 1465
1423 if ((error = ata_addref(sc->sc_chan)) != 0) 1466 if ((error = ata_addref(sc->sc_chan)) != 0)
1424 return (error); 1467 return (error);
1425 1468
1426 sc->sc_flags |= ATABUSCF_OPEN; 1469 sc->sc_flags |= ATABUSCF_OPEN;
1427 1470
1428 return (0); 1471 return (0);
1429} 1472}
1430 1473
1431 1474
1432int 1475int
1433atabusclose(dev_t dev, int flag, int fmt, 1476atabusclose(dev_t dev, int flag, int fmt,
1434 struct lwp *l) 1477 struct lwp *l)
1435{ 1478{
1436 struct atabus_softc *sc = 1479 struct atabus_softc *sc =
1437 device_lookup_private(&atabus_cd, minor(dev)); 1480 device_lookup_private(&atabus_cd, minor(dev));
1438 1481
1439 ata_delref(sc->sc_chan); 1482 ata_delref(sc->sc_chan);
1440 1483
1441 sc->sc_flags &= ~ATABUSCF_OPEN; 1484 sc->sc_flags &= ~ATABUSCF_OPEN;
1442 1485
1443 return (0); 1486 return (0);
1444} 1487}
1445 1488
1446int 1489int
1447atabusioctl(dev_t dev, u_long cmd, void *addr, int flag, 1490atabusioctl(dev_t dev, u_long cmd, void *addr, int flag,
1448 struct lwp *l) 1491 struct lwp *l)
1449{ 1492{
1450 struct atabus_softc *sc = 1493 struct atabus_softc *sc =
1451 device_lookup_private(&atabus_cd, minor(dev)); 1494 device_lookup_private(&atabus_cd, minor(dev));
1452 struct ata_channel *chp = sc->sc_chan; 1495 struct ata_channel *chp = sc->sc_chan;
1453 int min_drive, max_drive, drive; 1496 int min_drive, max_drive, drive;
1454 int error; 1497 int error;
1455 int s; 1498 int s;
1456 1499
1457 /* 1500 /*
1458 * Enforce write permission for ioctls that change the 1501 * Enforce write permission for ioctls that change the
1459 * state of the bus. Host adapter specific ioctls must 1502 * state of the bus. Host adapter specific ioctls must
1460 * be checked by the adapter driver. 1503 * be checked by the adapter driver.
1461 */ 1504 */
1462 switch (cmd) { 1505 switch (cmd) {
1463 case ATABUSIOSCAN: 1506 case ATABUSIOSCAN:
1464 case ATABUSIODETACH: 1507 case ATABUSIODETACH:
1465 case ATABUSIORESET: 1508 case ATABUSIORESET:
1466 if ((flag & FWRITE) == 0) 1509 if ((flag & FWRITE) == 0)
1467 return (EBADF); 1510 return (EBADF);
1468 } 1511 }
1469 1512
1470 switch (cmd) { 1513 switch (cmd) {
1471 case ATABUSIORESET: 1514 case ATABUSIORESET:
1472 s = splbio(); 1515 s = splbio();
1473 ata_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL); 1516 ata_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
1474 splx(s); 1517 splx(s);
1475 error = 0; 1518 error = 0;
1476 break; 1519 break;
1477 case ATABUSIOSCAN: 1520 case ATABUSIOSCAN:
1478 { 1521 {
1479#if 0 1522#if 0
1480 struct atabusioscan_args *a= 1523 struct atabusioscan_args *a=
1481 (struct atabusioscan_args *)addr; 1524 (struct atabusioscan_args *)addr;
1482#endif 1525#endif
1483 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) || 1526 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
1484 (chp->ch_drive[1].drive_flags & DRIVE_OLD)) 1527 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
1485 return (EOPNOTSUPP); 1528 return (EOPNOTSUPP);
1486 return (EOPNOTSUPP); 1529 return (EOPNOTSUPP);
1487 } 1530 }
1488 case ATABUSIODETACH: 1531 case ATABUSIODETACH:
1489 { 1532 {
1490 struct atabusioscan_args *a= 1533 struct atabusioscan_args *a=
1491 (struct atabusioscan_args *)addr; 1534 (struct atabusioscan_args *)addr;
1492 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) || 1535 if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
1493 (chp->ch_drive[1].drive_flags & DRIVE_OLD)) 1536 (chp->ch_drive[1].drive_flags & DRIVE_OLD))
1494 return (EOPNOTSUPP); 1537 return (EOPNOTSUPP);
1495 switch (a->at_dev) { 1538 switch (a->at_dev) {
1496 case -1: 1539 case -1:
1497 min_drive = 0; 1540 min_drive = 0;
1498 max_drive = 1; 1541 max_drive = 1;
1499 break; 1542 break;
1500 case 0: 1543 case 0:
1501 case 1: 1544 case 1:
1502 min_drive = max_drive = a->at_dev; 1545 min_drive = max_drive = a->at_dev;
1503 break; 1546 break;
1504 default: 1547 default:
1505 return (EINVAL); 1548 return (EINVAL);
1506 } 1549 }
1507 for (drive = min_drive; drive <= max_drive; drive++) { 1550 for (drive = min_drive; drive <= max_drive; drive++) {
1508 if (chp->ch_drive[drive].drv_softc != NULL) { 1551 if (chp->ch_drive[drive].drv_softc != NULL) {
1509 error = config_detach( 1552 error = config_detach(
1510 chp->ch_drive[drive].drv_softc, 0); 1553 chp->ch_drive[drive].drv_softc, 0);
1511 if (error) 1554 if (error)
1512 return (error); 1555 return (error);
1513 KASSERT(chp->ch_drive[drive].drv_softc == NULL); 1556 KASSERT(chp->ch_drive[drive].drv_softc == NULL);
1514 } 1557 }
1515 } 1558 }
1516 error = 0; 1559 error = 0;
1517 break; 1560 break;
1518 } 1561 }
1519 default: 1562 default:
1520 error = ENOTTY; 1563 error = ENOTTY;
1521 } 1564 }
1522 return (error); 1565 return (error);
1523}; 1566};
1524 1567
1525static bool 1568static bool
1526atabus_suspend(device_t dv PMF_FN_ARGS) 1569atabus_suspend(device_t dv PMF_FN_ARGS)
1527{ 1570{
1528 struct atabus_softc *sc = device_private(dv); 1571 struct atabus_softc *sc = device_private(dv);
1529 struct ata_channel *chp = sc->sc_chan; 1572 struct ata_channel *chp = sc->sc_chan;
1530 1573
1531 ata_queue_idle(chp->ch_queue); 1574 ata_queue_idle(chp->ch_queue);
1532 1575
1533 return true; 1576 return true;
1534} 1577}
1535 1578
1536static bool 1579static bool
1537atabus_resume(device_t dv PMF_FN_ARGS) 1580atabus_resume(device_t dv PMF_FN_ARGS)
1538{ 1581{
1539 struct atabus_softc *sc = device_private(dv); 1582 struct atabus_softc *sc = device_private(dv);
1540 struct ata_channel *chp = sc->sc_chan; 1583 struct ata_channel *chp = sc->sc_chan;
1541 int s; 1584 int s;
1542 1585
1543 /* 1586 /*
1544 * XXX joerg: with wdc, the first channel unfreezes the controler. 1587 * XXX joerg: with wdc, the first channel unfreezes the controler.
1545 * Move this the reset and queue idling into wdc. 1588 * Move this the reset and queue idling into wdc.
1546 */ 1589 */
1547 s = splbio(); 1590 s = splbio();
1548 if (chp->ch_queue->queue_freeze == 0) { 1591 if (chp->ch_queue->queue_freeze == 0) {
1549 splx(s); 1592 splx(s);
1550 return true; 1593 return true;
1551 } 1594 }
1552 KASSERT(chp->ch_queue->queue_freeze > 0); 1595 KASSERT(chp->ch_queue->queue_freeze > 0);
1553 /* unfreeze the queue and reset drives */ 1596 /* unfreeze the queue and reset drives */
1554 chp->ch_queue->queue_freeze--; 1597 chp->ch_queue->queue_freeze--;
1555 ata_reset_channel(chp, AT_WAIT); 1598 ata_reset_channel(chp, AT_WAIT);
1556 splx(s); 1599 splx(s);
1557 1600
1558 return true; 1601 return true;
1559} 1602}