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