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