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