ata.c revision 1.51 1 1.51 thorpej /* $NetBSD: ata.c,v 1.51 2004/08/20 22:02:40 thorpej 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.13 bouyer * 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.51 thorpej __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.51 2004/08/20 22:02:40 thorpej 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.2 bouyer #include <dev/ata/atareg.h>
56 1.2 bouyer #include <dev/ata/atavar.h>
57 1.15 bouyer #include <dev/ic/wdcreg.h>
58 1.15 bouyer #include <dev/ic/wdcvar.h>
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.51 thorpej #if NATARAID > 0
66 1.51 thorpej #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.47 thorpej 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.41 thorpej POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL);
83 1.41 thorpej
84 1.51 thorpej /*
85 1.51 thorpej * A queue of atabus instances, used to ensure the same bus probe order
86 1.51 thorpej * for a given hardware configuration at each boot.
87 1.51 thorpej */
88 1.51 thorpej struct atabus_initq_head atabus_initq_head =
89 1.51 thorpej TAILQ_HEAD_INITIALIZER(atabus_initq_head);
90 1.51 thorpej struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER;
91 1.51 thorpej
92 1.23 thorpej /*****************************************************************************
93 1.23 thorpej * ATA bus layer.
94 1.23 thorpej *
95 1.23 thorpej * ATA controllers attach an atabus instance, which handles probing the bus
96 1.23 thorpej * for drives, etc.
97 1.23 thorpej *****************************************************************************/
98 1.23 thorpej
99 1.30 bouyer dev_type_open(atabusopen);
100 1.30 bouyer dev_type_close(atabusclose);
101 1.30 bouyer dev_type_ioctl(atabusioctl);
102 1.30 bouyer
103 1.30 bouyer const struct cdevsw atabus_cdevsw = {
104 1.30 bouyer atabusopen, atabusclose, noread, nowrite, atabusioctl,
105 1.30 bouyer nostop, notty, nopoll, nommap, nokqfilter,
106 1.30 bouyer };
107 1.30 bouyer
108 1.30 bouyer extern struct cfdriver atabus_cd;
109 1.30 bouyer
110 1.30 bouyer
111 1.23 thorpej /*
112 1.23 thorpej * atabusprint:
113 1.23 thorpej *
114 1.23 thorpej * Autoconfiguration print routine used by ATA controllers when
115 1.23 thorpej * attaching an atabus instance.
116 1.23 thorpej */
117 1.23 thorpej int
118 1.23 thorpej atabusprint(void *aux, const char *pnp)
119 1.23 thorpej {
120 1.48 thorpej struct ata_channel *chan = aux;
121 1.23 thorpej
122 1.23 thorpej if (pnp)
123 1.23 thorpej aprint_normal("atabus at %s", pnp);
124 1.26 thorpej aprint_normal(" channel %d", chan->ch_channel);
125 1.23 thorpej
126 1.23 thorpej return (UNCONF);
127 1.23 thorpej }
128 1.23 thorpej
129 1.23 thorpej /*
130 1.23 thorpej * ataprint:
131 1.23 thorpej *
132 1.23 thorpej * Autoconfiguration print routine.
133 1.23 thorpej */
134 1.23 thorpej int
135 1.23 thorpej ataprint(void *aux, const char *pnp)
136 1.23 thorpej {
137 1.23 thorpej struct ata_device *adev = aux;
138 1.23 thorpej
139 1.23 thorpej if (pnp)
140 1.23 thorpej aprint_normal("wd at %s", pnp);
141 1.23 thorpej aprint_normal(" drive %d", adev->adev_drv_data->drive);
142 1.23 thorpej
143 1.23 thorpej return (UNCONF);
144 1.23 thorpej }
145 1.23 thorpej
146 1.51 thorpej static void
147 1.51 thorpej atabusconfig(struct atabus_softc *atabus_sc)
148 1.51 thorpej {
149 1.51 thorpej struct ata_channel *chp = atabus_sc->sc_chan;
150 1.51 thorpej struct atac_softc *atac = chp->ch_atac;
151 1.51 thorpej int i;
152 1.51 thorpej struct atabus_initq *atabus_initq = NULL;
153 1.51 thorpej
154 1.51 thorpej /* Probe for the drives. */
155 1.51 thorpej (*atac->atac_probe)(chp);
156 1.51 thorpej
157 1.51 thorpej ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n",
158 1.51 thorpej chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
159 1.51 thorpej DEBUG_PROBE);
160 1.51 thorpej
161 1.51 thorpej /* If no drives, abort here */
162 1.51 thorpej for (i = 0; i < chp->ch_ndrive; i++)
163 1.51 thorpej if ((chp->ch_drive[i].drive_flags & DRIVE) != 0)
164 1.51 thorpej break;
165 1.51 thorpej if (i == chp->ch_ndrive)
166 1.51 thorpej goto out;
167 1.51 thorpej
168 1.51 thorpej /* Shortcut in case we've been shutdown */
169 1.51 thorpej if (chp->ch_flags & ATACH_SHUTDOWN)
170 1.51 thorpej goto out;
171 1.51 thorpej
172 1.51 thorpej /* Make sure the devices probe in atabus order to avoid jitter. */
173 1.51 thorpej simple_lock(&atabus_interlock);
174 1.51 thorpej while(1) {
175 1.51 thorpej atabus_initq = TAILQ_FIRST(&atabus_initq_head);
176 1.51 thorpej if (atabus_initq->atabus_sc == atabus_sc)
177 1.51 thorpej break;
178 1.51 thorpej ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
179 1.51 thorpej &atabus_interlock);
180 1.51 thorpej }
181 1.51 thorpej simple_unlock(&atabus_interlock);
182 1.51 thorpej
183 1.51 thorpej /*
184 1.51 thorpej * Attach an ATAPI bus, if needed.
185 1.51 thorpej */
186 1.51 thorpej for (i = 0; i < chp->ch_ndrive; i++) {
187 1.51 thorpej if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) {
188 1.51 thorpej #if NATAPIBUS > 0
189 1.51 thorpej (*atac->atac_atapibus_attach)(atabus_sc);
190 1.51 thorpej #else
191 1.51 thorpej /*
192 1.51 thorpej * Fake the autoconfig "not configured" message
193 1.51 thorpej */
194 1.51 thorpej aprint_normal("atapibus at %s not configured\n",
195 1.51 thorpej atac->atac_dev.dv_xname);
196 1.51 thorpej chp->atapibus = NULL;
197 1.51 thorpej for (i = 0; i < chp->ch_ndrive; i++)
198 1.51 thorpej chp->ch_drive[i].drive_flags &= ~DRIVE_ATAPI;
199 1.51 thorpej #endif
200 1.51 thorpej break;
201 1.51 thorpej }
202 1.51 thorpej }
203 1.51 thorpej
204 1.51 thorpej for (i = 0; i < chp->ch_ndrive; i++) {
205 1.51 thorpej struct ata_device adev;
206 1.51 thorpej if ((chp->ch_drive[i].drive_flags &
207 1.51 thorpej (DRIVE_ATA | DRIVE_OLD)) == 0) {
208 1.51 thorpej continue;
209 1.51 thorpej }
210 1.51 thorpej memset(&adev, 0, sizeof(struct ata_device));
211 1.51 thorpej adev.adev_bustype = atac->atac_bustype_ata;
212 1.51 thorpej adev.adev_channel = chp->ch_channel;
213 1.51 thorpej adev.adev_openings = 1;
214 1.51 thorpej adev.adev_drv_data = &chp->ch_drive[i];
215 1.51 thorpej chp->ata_drives[i] = config_found(&atabus_sc->sc_dev,
216 1.51 thorpej &adev, ataprint);
217 1.51 thorpej if (chp->ata_drives[i] != NULL)
218 1.51 thorpej ata_probe_caps(&chp->ch_drive[i]);
219 1.51 thorpej else
220 1.51 thorpej chp->ch_drive[i].drive_flags &=
221 1.51 thorpej ~(DRIVE_ATA | DRIVE_OLD);
222 1.51 thorpej }
223 1.51 thorpej
224 1.51 thorpej /* now that we know the drives, the controller can set its modes */
225 1.51 thorpej if (atac->atac_set_modes) {
226 1.51 thorpej (*atac->atac_set_modes)(chp);
227 1.51 thorpej ata_print_modes(chp);
228 1.51 thorpej }
229 1.51 thorpej #if NATARAID > 0
230 1.51 thorpej if (atac->atac_cap & ATAC_CAP_RAID)
231 1.51 thorpej for (i = 0; i < chp->ch_ndrive; i++)
232 1.51 thorpej if (chp->ata_drives[i] != NULL)
233 1.51 thorpej ata_raid_check_component(chp->ata_drives[i]);
234 1.51 thorpej #endif /* NATARAID > 0 */
235 1.51 thorpej
236 1.51 thorpej /*
237 1.51 thorpej * reset drive_flags for unattached devices, reset state for attached
238 1.51 thorpej * ones
239 1.51 thorpej */
240 1.51 thorpej for (i = 0; i < chp->ch_ndrive; i++) {
241 1.51 thorpej if (chp->ch_drive[i].drv_softc == NULL)
242 1.51 thorpej chp->ch_drive[i].drive_flags = 0;
243 1.51 thorpej else
244 1.51 thorpej chp->ch_drive[i].state = 0;
245 1.51 thorpej }
246 1.51 thorpej
247 1.51 thorpej out:
248 1.51 thorpej if (atabus_initq == NULL) {
249 1.51 thorpej simple_lock(&atabus_interlock);
250 1.51 thorpej while(1) {
251 1.51 thorpej atabus_initq = TAILQ_FIRST(&atabus_initq_head);
252 1.51 thorpej if (atabus_initq->atabus_sc == atabus_sc)
253 1.51 thorpej break;
254 1.51 thorpej ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
255 1.51 thorpej &atabus_interlock);
256 1.51 thorpej }
257 1.51 thorpej simple_unlock(&atabus_interlock);
258 1.51 thorpej }
259 1.51 thorpej simple_lock(&atabus_interlock);
260 1.51 thorpej TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
261 1.51 thorpej simple_unlock(&atabus_interlock);
262 1.51 thorpej
263 1.51 thorpej free(atabus_initq, M_DEVBUF);
264 1.51 thorpej wakeup(&atabus_initq_head);
265 1.51 thorpej
266 1.51 thorpej ata_delref(chp);
267 1.51 thorpej
268 1.51 thorpej config_pending_decr();
269 1.51 thorpej }
270 1.51 thorpej
271 1.23 thorpej /*
272 1.23 thorpej * atabus_thread:
273 1.23 thorpej *
274 1.23 thorpej * Worker thread for the ATA bus.
275 1.23 thorpej */
276 1.23 thorpej static void
277 1.23 thorpej atabus_thread(void *arg)
278 1.23 thorpej {
279 1.23 thorpej struct atabus_softc *sc = arg;
280 1.48 thorpej struct ata_channel *chp = sc->sc_chan;
281 1.24 thorpej struct ata_xfer *xfer;
282 1.48 thorpej int i, s;
283 1.23 thorpej
284 1.23 thorpej s = splbio();
285 1.48 thorpej chp->ch_flags |= ATACH_TH_RUN;
286 1.48 thorpej
287 1.48 thorpej /*
288 1.48 thorpej * Probe the drives. Reset all flags to 0 to indicate to controllers
289 1.48 thorpej * that can re-probe that all drives must be probed..
290 1.48 thorpej *
291 1.48 thorpej * Note: ch_ndrive may be changed during the probe.
292 1.48 thorpej */
293 1.48 thorpej for (i = 0; i < ATA_MAXDRIVES; i++)
294 1.48 thorpej chp->ch_drive[i].drive_flags = 0;
295 1.23 thorpej splx(s);
296 1.23 thorpej
297 1.23 thorpej /* Configure the devices on the bus. */
298 1.23 thorpej atabusconfig(sc);
299 1.23 thorpej
300 1.23 thorpej for (;;) {
301 1.23 thorpej s = splbio();
302 1.48 thorpej if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 &&
303 1.33 bouyer (chp->ch_queue->active_xfer == NULL ||
304 1.23 thorpej chp->ch_queue->queue_freeze == 0)) {
305 1.48 thorpej chp->ch_flags &= ~ATACH_TH_RUN;
306 1.27 thorpej (void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
307 1.48 thorpej chp->ch_flags |= ATACH_TH_RUN;
308 1.23 thorpej }
309 1.23 thorpej splx(s);
310 1.48 thorpej if (chp->ch_flags & ATACH_SHUTDOWN)
311 1.23 thorpej break;
312 1.23 thorpej s = splbio();
313 1.48 thorpej if (chp->ch_flags & ATACH_TH_RESET) {
314 1.31 bouyer /*
315 1.31 bouyer * wdc_reset_channel() will freeze 2 times, so
316 1.31 bouyer * unfreeze one time. Not a problem as we're at splbio
317 1.31 bouyer */
318 1.23 thorpej chp->ch_queue->queue_freeze--;
319 1.31 bouyer wdc_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
320 1.33 bouyer } else if (chp->ch_queue->active_xfer != NULL &&
321 1.23 thorpej chp->ch_queue->queue_freeze == 1) {
322 1.23 thorpej /*
323 1.23 thorpej * Caller has bumped queue_freeze, decrease it.
324 1.23 thorpej */
325 1.23 thorpej chp->ch_queue->queue_freeze--;
326 1.33 bouyer xfer = chp->ch_queue->active_xfer;
327 1.23 thorpej KASSERT(xfer != NULL);
328 1.23 thorpej (*xfer->c_start)(chp, xfer);
329 1.23 thorpej } else if (chp->ch_queue->queue_freeze > 1)
330 1.23 thorpej panic("ata_thread: queue_freeze");
331 1.23 thorpej splx(s);
332 1.23 thorpej }
333 1.27 thorpej chp->ch_thread = NULL;
334 1.28 bouyer wakeup((void *)&chp->ch_flags);
335 1.23 thorpej kthread_exit(0);
336 1.23 thorpej }
337 1.23 thorpej
338 1.23 thorpej /*
339 1.23 thorpej * atabus_create_thread:
340 1.23 thorpej *
341 1.23 thorpej * Helper routine to create the ATA bus worker thread.
342 1.23 thorpej */
343 1.23 thorpej static void
344 1.23 thorpej atabus_create_thread(void *arg)
345 1.23 thorpej {
346 1.23 thorpej struct atabus_softc *sc = arg;
347 1.48 thorpej struct ata_channel *chp = sc->sc_chan;
348 1.23 thorpej int error;
349 1.23 thorpej
350 1.27 thorpej if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
351 1.23 thorpej "%s", sc->sc_dev.dv_xname)) != 0)
352 1.23 thorpej aprint_error("%s: unable to create kernel thread: error %d\n",
353 1.23 thorpej sc->sc_dev.dv_xname, error);
354 1.23 thorpej }
355 1.23 thorpej
356 1.23 thorpej /*
357 1.23 thorpej * atabus_match:
358 1.23 thorpej *
359 1.23 thorpej * Autoconfiguration match routine.
360 1.23 thorpej */
361 1.23 thorpej static int
362 1.23 thorpej atabus_match(struct device *parent, struct cfdata *cf, void *aux)
363 1.23 thorpej {
364 1.48 thorpej struct ata_channel *chp = aux;
365 1.23 thorpej
366 1.23 thorpej if (chp == NULL)
367 1.23 thorpej return (0);
368 1.23 thorpej
369 1.26 thorpej if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
370 1.23 thorpej cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
371 1.23 thorpej return (0);
372 1.23 thorpej
373 1.23 thorpej return (1);
374 1.23 thorpej }
375 1.23 thorpej
376 1.23 thorpej /*
377 1.23 thorpej * atabus_attach:
378 1.23 thorpej *
379 1.23 thorpej * Autoconfiguration attach routine.
380 1.23 thorpej */
381 1.23 thorpej static void
382 1.23 thorpej atabus_attach(struct device *parent, struct device *self, void *aux)
383 1.23 thorpej {
384 1.23 thorpej struct atabus_softc *sc = (void *) self;
385 1.48 thorpej struct ata_channel *chp = aux;
386 1.23 thorpej struct atabus_initq *initq;
387 1.23 thorpej
388 1.23 thorpej sc->sc_chan = chp;
389 1.23 thorpej
390 1.23 thorpej aprint_normal("\n");
391 1.23 thorpej aprint_naive("\n");
392 1.23 thorpej
393 1.43 thorpej if (ata_addref(chp))
394 1.35 mycroft return;
395 1.35 mycroft
396 1.23 thorpej initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
397 1.23 thorpej initq->atabus_sc = sc;
398 1.23 thorpej TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
399 1.23 thorpej config_pending_incr();
400 1.23 thorpej kthread_create(atabus_create_thread, sc);
401 1.23 thorpej }
402 1.23 thorpej
403 1.23 thorpej /*
404 1.23 thorpej * atabus_activate:
405 1.23 thorpej *
406 1.23 thorpej * Autoconfiguration activation routine.
407 1.23 thorpej */
408 1.23 thorpej static int
409 1.23 thorpej atabus_activate(struct device *self, enum devact act)
410 1.23 thorpej {
411 1.23 thorpej struct atabus_softc *sc = (void *) self;
412 1.48 thorpej struct ata_channel *chp = sc->sc_chan;
413 1.23 thorpej struct device *dev = NULL;
414 1.23 thorpej int s, i, error = 0;
415 1.23 thorpej
416 1.23 thorpej s = splbio();
417 1.23 thorpej switch (act) {
418 1.23 thorpej case DVACT_ACTIVATE:
419 1.23 thorpej error = EOPNOTSUPP;
420 1.23 thorpej break;
421 1.23 thorpej
422 1.23 thorpej case DVACT_DEACTIVATE:
423 1.23 thorpej /*
424 1.23 thorpej * We might deactivate the children of atapibus twice
425 1.23 thorpej * (once bia atapibus, once directly), but since the
426 1.23 thorpej * generic autoconfiguration code maintains the DVF_ACTIVE
427 1.23 thorpej * flag, it's safe.
428 1.23 thorpej */
429 1.23 thorpej if ((dev = chp->atapibus) != NULL) {
430 1.23 thorpej error = config_deactivate(dev);
431 1.23 thorpej if (error)
432 1.23 thorpej goto out;
433 1.23 thorpej }
434 1.23 thorpej
435 1.48 thorpej for (i = 0; i < chp->ch_ndrive; i++) {
436 1.23 thorpej if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
437 1.47 thorpej ATADEBUG_PRINT(("atabus_activate: %s: "
438 1.23 thorpej "deactivating %s\n", sc->sc_dev.dv_xname,
439 1.23 thorpej dev->dv_xname),
440 1.23 thorpej DEBUG_DETACH);
441 1.23 thorpej error = config_deactivate(dev);
442 1.23 thorpej if (error)
443 1.23 thorpej goto out;
444 1.23 thorpej }
445 1.23 thorpej }
446 1.23 thorpej break;
447 1.23 thorpej }
448 1.23 thorpej out:
449 1.23 thorpej splx(s);
450 1.23 thorpej
451 1.47 thorpej #ifdef ATADEBUG
452 1.23 thorpej if (dev != NULL && error != 0)
453 1.47 thorpej ATADEBUG_PRINT(("atabus_activate: %s: "
454 1.23 thorpej "error %d deactivating %s\n", sc->sc_dev.dv_xname,
455 1.23 thorpej error, dev->dv_xname), DEBUG_DETACH);
456 1.47 thorpej #endif /* ATADEBUG */
457 1.23 thorpej
458 1.23 thorpej return (error);
459 1.23 thorpej }
460 1.23 thorpej
461 1.23 thorpej /*
462 1.23 thorpej * atabus_detach:
463 1.23 thorpej *
464 1.23 thorpej * Autoconfiguration detach routine.
465 1.23 thorpej */
466 1.23 thorpej static int
467 1.23 thorpej atabus_detach(struct device *self, int flags)
468 1.23 thorpej {
469 1.23 thorpej struct atabus_softc *sc = (void *) self;
470 1.48 thorpej struct ata_channel *chp = sc->sc_chan;
471 1.23 thorpej struct device *dev = NULL;
472 1.23 thorpej int i, error = 0;
473 1.23 thorpej
474 1.23 thorpej /* Shutdown the channel. */
475 1.23 thorpej /* XXX NEED AN INTERLOCK HERE. */
476 1.48 thorpej chp->ch_flags |= ATACH_SHUTDOWN;
477 1.27 thorpej wakeup(&chp->ch_thread);
478 1.27 thorpej while (chp->ch_thread != NULL)
479 1.28 bouyer (void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
480 1.23 thorpej
481 1.23 thorpej /*
482 1.23 thorpej * Detach atapibus and its children.
483 1.23 thorpej */
484 1.23 thorpej if ((dev = chp->atapibus) != NULL) {
485 1.47 thorpej ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
486 1.23 thorpej sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
487 1.23 thorpej error = config_detach(dev, flags);
488 1.23 thorpej if (error)
489 1.23 thorpej goto out;
490 1.23 thorpej }
491 1.23 thorpej
492 1.23 thorpej /*
493 1.23 thorpej * Detach our other children.
494 1.23 thorpej */
495 1.48 thorpej for (i = 0; i < chp->ch_ndrive; i++) {
496 1.23 thorpej if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
497 1.23 thorpej continue;
498 1.23 thorpej if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
499 1.47 thorpej ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
500 1.23 thorpej sc->sc_dev.dv_xname, dev->dv_xname),
501 1.23 thorpej DEBUG_DETACH);
502 1.23 thorpej error = config_detach(dev, flags);
503 1.23 thorpej if (error)
504 1.23 thorpej goto out;
505 1.23 thorpej }
506 1.23 thorpej }
507 1.23 thorpej
508 1.23 thorpej out:
509 1.47 thorpej #ifdef ATADEBUG
510 1.23 thorpej if (dev != NULL && error != 0)
511 1.47 thorpej ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
512 1.23 thorpej sc->sc_dev.dv_xname, error, dev->dv_xname),
513 1.23 thorpej DEBUG_DETACH);
514 1.47 thorpej #endif /* ATADEBUG */
515 1.23 thorpej
516 1.23 thorpej return (error);
517 1.23 thorpej }
518 1.23 thorpej
519 1.23 thorpej CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
520 1.23 thorpej atabus_match, atabus_attach, atabus_detach, atabus_activate);
521 1.23 thorpej
522 1.23 thorpej /*****************************************************************************
523 1.23 thorpej * Common ATA bus operations.
524 1.23 thorpej *****************************************************************************/
525 1.23 thorpej
526 1.2 bouyer /* Get the disk's parameters */
527 1.2 bouyer int
528 1.21 thorpej ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
529 1.21 thorpej struct ataparams *prms)
530 1.2 bouyer {
531 1.2 bouyer char tb[DEV_BSIZE];
532 1.36 thorpej struct ata_command ata_c;
533 1.50 thorpej struct ata_channel *chp = drvp->chnl_softc;
534 1.50 thorpej struct atac_softc *atac = chp->ch_atac;
535 1.2 bouyer
536 1.2 bouyer #if BYTE_ORDER == LITTLE_ENDIAN
537 1.2 bouyer int i;
538 1.2 bouyer u_int16_t *p;
539 1.2 bouyer #endif
540 1.2 bouyer
541 1.47 thorpej ATADEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
542 1.2 bouyer
543 1.2 bouyer memset(tb, 0, DEV_BSIZE);
544 1.2 bouyer memset(prms, 0, sizeof(struct ataparams));
545 1.36 thorpej memset(&ata_c, 0, sizeof(struct ata_command));
546 1.2 bouyer
547 1.2 bouyer if (drvp->drive_flags & DRIVE_ATA) {
548 1.36 thorpej ata_c.r_command = WDCC_IDENTIFY;
549 1.36 thorpej ata_c.r_st_bmask = WDCS_DRDY;
550 1.36 thorpej ata_c.r_st_pmask = 0;
551 1.36 thorpej ata_c.timeout = 3000; /* 3s */
552 1.7 bouyer } else if (drvp->drive_flags & DRIVE_ATAPI) {
553 1.36 thorpej ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
554 1.36 thorpej ata_c.r_st_bmask = 0;
555 1.36 thorpej ata_c.r_st_pmask = 0;
556 1.36 thorpej ata_c.timeout = 10000; /* 10s */
557 1.7 bouyer } else {
558 1.47 thorpej ATADEBUG_PRINT(("ata_get_parms: no disks\n"),
559 1.10 bouyer DEBUG_FUNCS|DEBUG_PROBE);
560 1.7 bouyer return CMD_ERR;
561 1.2 bouyer }
562 1.36 thorpej ata_c.flags = AT_READ | flags;
563 1.36 thorpej ata_c.data = tb;
564 1.36 thorpej ata_c.bcount = DEV_BSIZE;
565 1.50 thorpej if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
566 1.50 thorpej &ata_c) != ATACMD_COMPLETE) {
567 1.47 thorpej ATADEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
568 1.10 bouyer DEBUG_FUNCS|DEBUG_PROBE);
569 1.2 bouyer return CMD_AGAIN;
570 1.10 bouyer }
571 1.36 thorpej if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
572 1.47 thorpej ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
573 1.36 thorpej ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
574 1.2 bouyer return CMD_ERR;
575 1.2 bouyer } else {
576 1.17 bouyer /* if we didn't read any data something is wrong */
577 1.36 thorpej if ((ata_c.flags & AT_XFDONE) == 0)
578 1.17 bouyer return CMD_ERR;
579 1.2 bouyer /* Read in parameter block. */
580 1.2 bouyer memcpy(prms, tb, sizeof(struct ataparams));
581 1.2 bouyer #if BYTE_ORDER == LITTLE_ENDIAN
582 1.2 bouyer /*
583 1.2 bouyer * Shuffle string byte order.
584 1.2 bouyer * ATAPI Mitsumi and NEC drives don't need this.
585 1.2 bouyer */
586 1.2 bouyer if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
587 1.2 bouyer WDC_CFG_ATAPI &&
588 1.2 bouyer ((prms->atap_model[0] == 'N' &&
589 1.2 bouyer prms->atap_model[1] == 'E') ||
590 1.2 bouyer (prms->atap_model[0] == 'F' &&
591 1.2 bouyer prms->atap_model[1] == 'X')))
592 1.2 bouyer return 0;
593 1.2 bouyer for (i = 0; i < sizeof(prms->atap_model); i += 2) {
594 1.2 bouyer p = (u_short *)(prms->atap_model + i);
595 1.2 bouyer *p = ntohs(*p);
596 1.2 bouyer }
597 1.2 bouyer for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
598 1.2 bouyer p = (u_short *)(prms->atap_serial + i);
599 1.2 bouyer *p = ntohs(*p);
600 1.2 bouyer }
601 1.2 bouyer for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
602 1.2 bouyer p = (u_short *)(prms->atap_revision + i);
603 1.2 bouyer *p = ntohs(*p);
604 1.2 bouyer }
605 1.2 bouyer #endif
606 1.2 bouyer return CMD_OK;
607 1.2 bouyer }
608 1.2 bouyer }
609 1.2 bouyer
610 1.2 bouyer int
611 1.21 thorpej ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
612 1.2 bouyer {
613 1.36 thorpej struct ata_command ata_c;
614 1.50 thorpej struct ata_channel *chp = drvp->chnl_softc;
615 1.50 thorpej struct atac_softc *atac = chp->ch_atac;
616 1.2 bouyer
617 1.47 thorpej ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
618 1.36 thorpej memset(&ata_c, 0, sizeof(struct ata_command));
619 1.2 bouyer
620 1.36 thorpej ata_c.r_command = SET_FEATURES;
621 1.36 thorpej ata_c.r_st_bmask = 0;
622 1.36 thorpej ata_c.r_st_pmask = 0;
623 1.36 thorpej ata_c.r_features = WDSF_SET_MODE;
624 1.36 thorpej ata_c.r_count = mode;
625 1.36 thorpej ata_c.flags = flags;
626 1.36 thorpej ata_c.timeout = 1000; /* 1s */
627 1.50 thorpej if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
628 1.50 thorpej &ata_c) != ATACMD_COMPLETE)
629 1.2 bouyer return CMD_AGAIN;
630 1.36 thorpej if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
631 1.2 bouyer return CMD_ERR;
632 1.2 bouyer }
633 1.2 bouyer return CMD_OK;
634 1.11 bouyer }
635 1.11 bouyer
636 1.11 bouyer void
637 1.21 thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
638 1.11 bouyer {
639 1.11 bouyer /*
640 1.11 bouyer * Downgrade decision: if we get NERRS_MAX in NXFER.
641 1.11 bouyer * We start with n_dmaerrs set to NERRS_MAX-1 so that the
642 1.11 bouyer * first error within the first NXFER ops will immediatly trigger
643 1.11 bouyer * a downgrade.
644 1.11 bouyer * If we got an error and n_xfers is bigger than NXFER reset counters.
645 1.11 bouyer */
646 1.11 bouyer drvp->n_dmaerrs++;
647 1.11 bouyer if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
648 1.39 thorpej ata_downgrade_mode(drvp, flags);
649 1.11 bouyer drvp->n_dmaerrs = NERRS_MAX-1;
650 1.11 bouyer drvp->n_xfers = 0;
651 1.11 bouyer return;
652 1.11 bouyer }
653 1.11 bouyer if (drvp->n_xfers > NXFER) {
654 1.11 bouyer drvp->n_dmaerrs = 1; /* just got an error */
655 1.11 bouyer drvp->n_xfers = 1; /* restart counting from this error */
656 1.4 bouyer }
657 1.2 bouyer }
658 1.30 bouyer
659 1.44 thorpej /*
660 1.44 thorpej * Add a command to the queue and start controller. Must be called at splbio
661 1.44 thorpej */
662 1.44 thorpej void
663 1.48 thorpej ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
664 1.44 thorpej {
665 1.44 thorpej
666 1.47 thorpej ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer,
667 1.44 thorpej chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
668 1.44 thorpej
669 1.44 thorpej /* complete xfer setup */
670 1.44 thorpej xfer->c_chp = chp;
671 1.44 thorpej
672 1.44 thorpej /* insert at the end of command list */
673 1.44 thorpej TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
674 1.47 thorpej ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n",
675 1.44 thorpej chp->ch_flags), DEBUG_XFERS);
676 1.45 thorpej atastart(chp);
677 1.45 thorpej }
678 1.45 thorpej
679 1.45 thorpej /*
680 1.45 thorpej * Start I/O on a controller, for the given channel.
681 1.45 thorpej * The first xfer may be not for our channel if the channel queues
682 1.45 thorpej * are shared.
683 1.45 thorpej */
684 1.45 thorpej void
685 1.48 thorpej atastart(struct ata_channel *chp)
686 1.45 thorpej {
687 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
688 1.45 thorpej struct ata_xfer *xfer;
689 1.45 thorpej
690 1.45 thorpej #ifdef WDC_DIAGNOSTIC
691 1.45 thorpej int spl1, spl2;
692 1.45 thorpej
693 1.45 thorpej spl1 = splbio();
694 1.45 thorpej spl2 = splbio();
695 1.45 thorpej if (spl2 != spl1) {
696 1.45 thorpej printf("atastart: not at splbio()\n");
697 1.45 thorpej panic("atastart");
698 1.45 thorpej }
699 1.45 thorpej splx(spl2);
700 1.45 thorpej splx(spl1);
701 1.45 thorpej #endif /* WDC_DIAGNOSTIC */
702 1.45 thorpej
703 1.45 thorpej /* is there a xfer ? */
704 1.45 thorpej if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL)
705 1.45 thorpej return;
706 1.45 thorpej
707 1.45 thorpej /* adjust chp, in case we have a shared queue */
708 1.45 thorpej chp = xfer->c_chp;
709 1.45 thorpej
710 1.45 thorpej if (chp->ch_queue->active_xfer != NULL) {
711 1.45 thorpej return; /* channel aleady active */
712 1.45 thorpej }
713 1.45 thorpej if (__predict_false(chp->ch_queue->queue_freeze > 0)) {
714 1.45 thorpej return; /* queue froozen */
715 1.45 thorpej }
716 1.45 thorpej #ifdef DIAGNOSTIC
717 1.48 thorpej if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0)
718 1.45 thorpej panic("atastart: channel waiting for irq");
719 1.45 thorpej #endif
720 1.49 thorpej if (atac->atac_claim_hw)
721 1.49 thorpej if (!(*atac->atac_claim_hw)(chp, 0))
722 1.45 thorpej return;
723 1.45 thorpej
724 1.47 thorpej ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer,
725 1.45 thorpej chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
726 1.45 thorpej if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) {
727 1.45 thorpej chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET;
728 1.45 thorpej chp->ch_drive[xfer->c_drive].state = 0;
729 1.45 thorpej }
730 1.45 thorpej chp->ch_queue->active_xfer = xfer;
731 1.45 thorpej TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
732 1.45 thorpej
733 1.49 thorpej if (atac->atac_cap & ATAC_CAP_NOIRQ)
734 1.45 thorpej KASSERT(xfer->c_flags & C_POLL);
735 1.45 thorpej xfer->c_start(chp, xfer);
736 1.44 thorpej }
737 1.44 thorpej
738 1.41 thorpej struct ata_xfer *
739 1.41 thorpej ata_get_xfer(int flags)
740 1.41 thorpej {
741 1.41 thorpej struct ata_xfer *xfer;
742 1.41 thorpej int s;
743 1.41 thorpej
744 1.41 thorpej s = splbio();
745 1.41 thorpej xfer = pool_get(&ata_xfer_pool,
746 1.41 thorpej ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
747 1.41 thorpej splx(s);
748 1.41 thorpej if (xfer != NULL) {
749 1.41 thorpej memset(xfer, 0, sizeof(struct ata_xfer));
750 1.41 thorpej }
751 1.41 thorpej return xfer;
752 1.41 thorpej }
753 1.41 thorpej
754 1.41 thorpej void
755 1.48 thorpej ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
756 1.41 thorpej {
757 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
758 1.41 thorpej int s;
759 1.41 thorpej
760 1.49 thorpej if (atac->atac_free_hw)
761 1.49 thorpej (*atac->atac_free_hw)(chp);
762 1.41 thorpej s = splbio();
763 1.41 thorpej pool_put(&ata_xfer_pool, xfer);
764 1.41 thorpej splx(s);
765 1.41 thorpej }
766 1.41 thorpej
767 1.42 thorpej /*
768 1.48 thorpej * Kill off all pending xfers for a ata_channel.
769 1.42 thorpej *
770 1.42 thorpej * Must be called at splbio().
771 1.42 thorpej */
772 1.42 thorpej void
773 1.42 thorpej ata_kill_pending(struct ata_drive_datas *drvp)
774 1.42 thorpej {
775 1.48 thorpej struct ata_channel *chp = drvp->chnl_softc;
776 1.42 thorpej struct ata_xfer *xfer, *next_xfer;
777 1.42 thorpej int s = splbio();
778 1.42 thorpej
779 1.42 thorpej for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
780 1.42 thorpej xfer != NULL; xfer = next_xfer) {
781 1.42 thorpej next_xfer = TAILQ_NEXT(xfer, c_xferchain);
782 1.42 thorpej if (xfer->c_chp != chp || xfer->c_drive != drvp->drive)
783 1.42 thorpej continue;
784 1.42 thorpej TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
785 1.42 thorpej (*xfer->c_kill_xfer)(chp, xfer, KILL_GONE);
786 1.42 thorpej }
787 1.42 thorpej
788 1.42 thorpej while ((xfer = chp->ch_queue->active_xfer) != NULL) {
789 1.42 thorpej if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) {
790 1.42 thorpej drvp->drive_flags |= DRIVE_WAITDRAIN;
791 1.42 thorpej (void) tsleep(&chp->ch_queue->active_xfer,
792 1.42 thorpej PRIBIO, "atdrn", 0);
793 1.42 thorpej } else {
794 1.42 thorpej /* no more xfer for us */
795 1.42 thorpej break;
796 1.42 thorpej }
797 1.42 thorpej }
798 1.42 thorpej splx(s);
799 1.42 thorpej }
800 1.42 thorpej
801 1.43 thorpej int
802 1.48 thorpej ata_addref(struct ata_channel *chp)
803 1.43 thorpej {
804 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
805 1.49 thorpej struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
806 1.43 thorpej int s, error = 0;
807 1.43 thorpej
808 1.43 thorpej s = splbio();
809 1.43 thorpej if (adapt->adapt_refcnt++ == 0 &&
810 1.43 thorpej adapt->adapt_enable != NULL) {
811 1.49 thorpej error = (*adapt->adapt_enable)(&atac->atac_dev, 1);
812 1.43 thorpej if (error)
813 1.43 thorpej adapt->adapt_refcnt--;
814 1.43 thorpej }
815 1.43 thorpej splx(s);
816 1.43 thorpej return (error);
817 1.43 thorpej }
818 1.43 thorpej
819 1.43 thorpej void
820 1.48 thorpej ata_delref(struct ata_channel *chp)
821 1.43 thorpej {
822 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
823 1.49 thorpej struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
824 1.43 thorpej int s;
825 1.43 thorpej
826 1.43 thorpej s = splbio();
827 1.43 thorpej if (adapt->adapt_refcnt-- == 1 &&
828 1.43 thorpej adapt->adapt_enable != NULL)
829 1.49 thorpej (void) (*adapt->adapt_enable)(&atac->atac_dev, 0);
830 1.43 thorpej splx(s);
831 1.43 thorpej }
832 1.43 thorpej
833 1.38 thorpej void
834 1.48 thorpej ata_print_modes(struct ata_channel *chp)
835 1.38 thorpej {
836 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
837 1.38 thorpej int drive;
838 1.38 thorpej struct ata_drive_datas *drvp;
839 1.38 thorpej
840 1.38 thorpej for (drive = 0; drive < 2; drive++) {
841 1.38 thorpej drvp = &chp->ch_drive[drive];
842 1.38 thorpej if ((drvp->drive_flags & DRIVE) == 0)
843 1.38 thorpej continue;
844 1.38 thorpej aprint_normal("%s(%s:%d:%d): using PIO mode %d",
845 1.38 thorpej drvp->drv_softc->dv_xname,
846 1.49 thorpej atac->atac_dev.dv_xname,
847 1.38 thorpej chp->ch_channel, drive, drvp->PIO_mode);
848 1.38 thorpej if (drvp->drive_flags & DRIVE_DMA)
849 1.38 thorpej aprint_normal(", DMA mode %d", drvp->DMA_mode);
850 1.38 thorpej if (drvp->drive_flags & DRIVE_UDMA) {
851 1.38 thorpej aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode);
852 1.38 thorpej if (drvp->UDMA_mode == 2)
853 1.38 thorpej aprint_normal(" (Ultra/33)");
854 1.38 thorpej else if (drvp->UDMA_mode == 4)
855 1.38 thorpej aprint_normal(" (Ultra/66)");
856 1.38 thorpej else if (drvp->UDMA_mode == 5)
857 1.38 thorpej aprint_normal(" (Ultra/100)");
858 1.38 thorpej else if (drvp->UDMA_mode == 6)
859 1.38 thorpej aprint_normal(" (Ultra/133)");
860 1.38 thorpej }
861 1.38 thorpej if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
862 1.38 thorpej aprint_normal(" (using DMA data transfers)");
863 1.38 thorpej aprint_normal("\n");
864 1.38 thorpej }
865 1.38 thorpej }
866 1.38 thorpej
867 1.39 thorpej /*
868 1.39 thorpej * downgrade the transfer mode of a drive after an error. return 1 if
869 1.39 thorpej * downgrade was possible, 0 otherwise.
870 1.39 thorpej */
871 1.39 thorpej int
872 1.39 thorpej ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
873 1.39 thorpej {
874 1.48 thorpej struct ata_channel *chp = drvp->chnl_softc;
875 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
876 1.39 thorpej struct device *drv_dev = drvp->drv_softc;
877 1.39 thorpej int cf_flags = drv_dev->dv_cfdata->cf_flags;
878 1.39 thorpej
879 1.39 thorpej /* if drive or controller don't know its mode, we can't do much */
880 1.39 thorpej if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
881 1.49 thorpej (atac->atac_set_modes == NULL))
882 1.39 thorpej return 0;
883 1.39 thorpej /* current drive mode was set by a config flag, let it this way */
884 1.39 thorpej if ((cf_flags & ATA_CONFIG_PIO_SET) ||
885 1.39 thorpej (cf_flags & ATA_CONFIG_DMA_SET) ||
886 1.39 thorpej (cf_flags & ATA_CONFIG_UDMA_SET))
887 1.39 thorpej return 0;
888 1.39 thorpej
889 1.39 thorpej /*
890 1.39 thorpej * If we were using Ultra-DMA mode, downgrade to the next lower mode.
891 1.39 thorpej */
892 1.39 thorpej if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
893 1.39 thorpej drvp->UDMA_mode--;
894 1.39 thorpej printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
895 1.39 thorpej drv_dev->dv_xname, drvp->UDMA_mode);
896 1.39 thorpej }
897 1.39 thorpej
898 1.39 thorpej /*
899 1.39 thorpej * If we were using ultra-DMA, don't downgrade to multiword DMA.
900 1.39 thorpej */
901 1.39 thorpej else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
902 1.39 thorpej drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
903 1.39 thorpej drvp->PIO_mode = drvp->PIO_cap;
904 1.39 thorpej printf("%s: transfer error, downgrading to PIO mode %d\n",
905 1.39 thorpej drv_dev->dv_xname, drvp->PIO_mode);
906 1.39 thorpej } else /* already using PIO, can't downgrade */
907 1.39 thorpej return 0;
908 1.39 thorpej
909 1.49 thorpej (*atac->atac_set_modes)(chp);
910 1.39 thorpej ata_print_modes(chp);
911 1.39 thorpej /* reset the channel, which will shedule all drives for setup */
912 1.39 thorpej wdc_reset_channel(chp, flags | AT_RST_NOCMD);
913 1.39 thorpej return 1;
914 1.39 thorpej }
915 1.39 thorpej
916 1.40 thorpej /*
917 1.40 thorpej * Probe drive's capabilities, for use by the controller later
918 1.40 thorpej * Assumes drvp points to an existing drive.
919 1.40 thorpej */
920 1.40 thorpej void
921 1.40 thorpej ata_probe_caps(struct ata_drive_datas *drvp)
922 1.40 thorpej {
923 1.40 thorpej struct ataparams params, params2;
924 1.48 thorpej struct ata_channel *chp = drvp->chnl_softc;
925 1.49 thorpej struct atac_softc *atac = chp->ch_atac;
926 1.40 thorpej struct device *drv_dev = drvp->drv_softc;
927 1.40 thorpej int i, printed;
928 1.40 thorpej char *sep = "";
929 1.40 thorpej int cf_flags;
930 1.40 thorpej
931 1.40 thorpej if (ata_get_params(drvp, AT_WAIT, ¶ms) != CMD_OK) {
932 1.40 thorpej /* IDENTIFY failed. Can't tell more about the device */
933 1.40 thorpej return;
934 1.40 thorpej }
935 1.49 thorpej if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) ==
936 1.49 thorpej (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) {
937 1.40 thorpej /*
938 1.40 thorpej * Controller claims 16 and 32 bit transfers.
939 1.40 thorpej * Re-do an IDENTIFY with 32-bit transfers,
940 1.40 thorpej * and compare results.
941 1.40 thorpej */
942 1.40 thorpej drvp->drive_flags |= DRIVE_CAP32;
943 1.40 thorpej ata_get_params(drvp, AT_WAIT, ¶ms2);
944 1.40 thorpej if (memcmp(¶ms, ¶ms2, sizeof(struct ataparams)) != 0) {
945 1.40 thorpej /* Not good. fall back to 16bits */
946 1.40 thorpej drvp->drive_flags &= ~DRIVE_CAP32;
947 1.40 thorpej } else {
948 1.40 thorpej aprint_normal("%s: 32-bit data port\n",
949 1.40 thorpej drv_dev->dv_xname);
950 1.40 thorpej }
951 1.40 thorpej }
952 1.40 thorpej #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
953 1.40 thorpej if (params.atap_ata_major > 0x01 &&
954 1.40 thorpej params.atap_ata_major != 0xffff) {
955 1.40 thorpej for (i = 14; i > 0; i--) {
956 1.40 thorpej if (params.atap_ata_major & (1 << i)) {
957 1.40 thorpej aprint_normal("%s: ATA version %d\n",
958 1.40 thorpej drv_dev->dv_xname, i);
959 1.40 thorpej drvp->ata_vers = i;
960 1.40 thorpej break;
961 1.40 thorpej }
962 1.40 thorpej }
963 1.40 thorpej }
964 1.40 thorpej #endif
965 1.40 thorpej
966 1.40 thorpej /* An ATAPI device is at last PIO mode 3 */
967 1.40 thorpej if (drvp->drive_flags & DRIVE_ATAPI)
968 1.40 thorpej drvp->PIO_mode = 3;
969 1.40 thorpej
970 1.40 thorpej /*
971 1.40 thorpej * It's not in the specs, but it seems that some drive
972 1.40 thorpej * returns 0xffff in atap_extensions when this field is invalid
973 1.40 thorpej */
974 1.40 thorpej if (params.atap_extensions != 0xffff &&
975 1.40 thorpej (params.atap_extensions & WDC_EXT_MODES)) {
976 1.40 thorpej printed = 0;
977 1.40 thorpej /*
978 1.40 thorpej * XXX some drives report something wrong here (they claim to
979 1.40 thorpej * support PIO mode 8 !). As mode is coded on 3 bits in
980 1.40 thorpej * SET FEATURE, limit it to 7 (so limit i to 4).
981 1.40 thorpej * If higher mode than 7 is found, abort.
982 1.40 thorpej */
983 1.40 thorpej for (i = 7; i >= 0; i--) {
984 1.40 thorpej if ((params.atap_piomode_supp & (1 << i)) == 0)
985 1.40 thorpej continue;
986 1.40 thorpej if (i > 4)
987 1.40 thorpej return;
988 1.40 thorpej /*
989 1.40 thorpej * See if mode is accepted.
990 1.40 thorpej * If the controller can't set its PIO mode,
991 1.40 thorpej * assume the defaults are good, so don't try
992 1.40 thorpej * to set it
993 1.40 thorpej */
994 1.49 thorpej if (atac->atac_set_modes)
995 1.40 thorpej /*
996 1.40 thorpej * It's OK to pool here, it's fast enouth
997 1.40 thorpej * to not bother waiting for interrupt
998 1.40 thorpej */
999 1.40 thorpej if (ata_set_mode(drvp, 0x08 | (i + 3),
1000 1.40 thorpej AT_WAIT) != CMD_OK)
1001 1.40 thorpej continue;
1002 1.40 thorpej if (!printed) {
1003 1.40 thorpej aprint_normal("%s: drive supports PIO mode %d",
1004 1.40 thorpej drv_dev->dv_xname, i + 3);
1005 1.40 thorpej sep = ",";
1006 1.40 thorpej printed = 1;
1007 1.40 thorpej }
1008 1.40 thorpej /*
1009 1.40 thorpej * If controller's driver can't set its PIO mode,
1010 1.40 thorpej * get the highter one for the drive.
1011 1.40 thorpej */
1012 1.49 thorpej if (atac->atac_set_modes == NULL ||
1013 1.49 thorpej atac->atac_pio_cap >= i + 3) {
1014 1.40 thorpej drvp->PIO_mode = i + 3;
1015 1.40 thorpej drvp->PIO_cap = i + 3;
1016 1.40 thorpej break;
1017 1.40 thorpej }
1018 1.40 thorpej }
1019 1.40 thorpej if (!printed) {
1020 1.40 thorpej /*
1021 1.40 thorpej * We didn't find a valid PIO mode.
1022 1.40 thorpej * Assume the values returned for DMA are buggy too
1023 1.40 thorpej */
1024 1.40 thorpej return;
1025 1.40 thorpej }
1026 1.40 thorpej drvp->drive_flags |= DRIVE_MODE;
1027 1.40 thorpej printed = 0;
1028 1.40 thorpej for (i = 7; i >= 0; i--) {
1029 1.40 thorpej if ((params.atap_dmamode_supp & (1 << i)) == 0)
1030 1.40 thorpej continue;
1031 1.49 thorpej if ((atac->atac_cap & ATAC_CAP_DMA) &&
1032 1.49 thorpej atac->atac_set_modes != NULL)
1033 1.40 thorpej if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
1034 1.40 thorpej != CMD_OK)
1035 1.40 thorpej continue;
1036 1.40 thorpej if (!printed) {
1037 1.40 thorpej aprint_normal("%s DMA mode %d", sep, i);
1038 1.40 thorpej sep = ",";
1039 1.40 thorpej printed = 1;
1040 1.40 thorpej }
1041 1.49 thorpej if (atac->atac_cap & ATAC_CAP_DMA) {
1042 1.49 thorpej if (atac->atac_set_modes != NULL &&
1043 1.49 thorpej atac->atac_dma_cap < i)
1044 1.40 thorpej continue;
1045 1.40 thorpej drvp->DMA_mode = i;
1046 1.40 thorpej drvp->DMA_cap = i;
1047 1.40 thorpej drvp->drive_flags |= DRIVE_DMA;
1048 1.40 thorpej }
1049 1.40 thorpej break;
1050 1.40 thorpej }
1051 1.40 thorpej if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
1052 1.40 thorpej printed = 0;
1053 1.40 thorpej for (i = 7; i >= 0; i--) {
1054 1.40 thorpej if ((params.atap_udmamode_supp & (1 << i))
1055 1.40 thorpej == 0)
1056 1.40 thorpej continue;
1057 1.49 thorpej if (atac->atac_set_modes != NULL &&
1058 1.49 thorpej (atac->atac_cap & ATAC_CAP_UDMA))
1059 1.40 thorpej if (ata_set_mode(drvp, 0x40 | i,
1060 1.40 thorpej AT_WAIT) != CMD_OK)
1061 1.40 thorpej continue;
1062 1.40 thorpej if (!printed) {
1063 1.40 thorpej aprint_normal("%s Ultra-DMA mode %d",
1064 1.40 thorpej sep, i);
1065 1.40 thorpej if (i == 2)
1066 1.40 thorpej aprint_normal(" (Ultra/33)");
1067 1.40 thorpej else if (i == 4)
1068 1.40 thorpej aprint_normal(" (Ultra/66)");
1069 1.40 thorpej else if (i == 5)
1070 1.40 thorpej aprint_normal(" (Ultra/100)");
1071 1.40 thorpej else if (i == 6)
1072 1.40 thorpej aprint_normal(" (Ultra/133)");
1073 1.40 thorpej sep = ",";
1074 1.40 thorpej printed = 1;
1075 1.40 thorpej }
1076 1.49 thorpej if (atac->atac_cap & ATAC_CAP_UDMA) {
1077 1.49 thorpej if (atac->atac_set_modes != NULL &&
1078 1.49 thorpej atac->atac_udma_cap < i)
1079 1.40 thorpej continue;
1080 1.40 thorpej drvp->UDMA_mode = i;
1081 1.40 thorpej drvp->UDMA_cap = i;
1082 1.40 thorpej drvp->drive_flags |= DRIVE_UDMA;
1083 1.40 thorpej }
1084 1.40 thorpej break;
1085 1.40 thorpej }
1086 1.40 thorpej }
1087 1.40 thorpej aprint_normal("\n");
1088 1.40 thorpej }
1089 1.40 thorpej
1090 1.40 thorpej drvp->drive_flags &= ~DRIVE_NOSTREAM;
1091 1.40 thorpej if (drvp->drive_flags & DRIVE_ATAPI) {
1092 1.49 thorpej if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM)
1093 1.40 thorpej drvp->drive_flags |= DRIVE_NOSTREAM;
1094 1.40 thorpej } else {
1095 1.49 thorpej if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM)
1096 1.40 thorpej drvp->drive_flags |= DRIVE_NOSTREAM;
1097 1.40 thorpej }
1098 1.40 thorpej
1099 1.40 thorpej /* Try to guess ATA version here, if it didn't get reported */
1100 1.40 thorpej if (drvp->ata_vers == 0) {
1101 1.40 thorpej if (drvp->drive_flags & DRIVE_UDMA)
1102 1.40 thorpej drvp->ata_vers = 4; /* should be at last ATA-4 */
1103 1.40 thorpej else if (drvp->PIO_cap > 2)
1104 1.40 thorpej drvp->ata_vers = 2; /* should be at last ATA-2 */
1105 1.40 thorpej }
1106 1.40 thorpej cf_flags = drv_dev->dv_cfdata->cf_flags;
1107 1.40 thorpej if (cf_flags & ATA_CONFIG_PIO_SET) {
1108 1.40 thorpej drvp->PIO_mode =
1109 1.40 thorpej (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
1110 1.40 thorpej drvp->drive_flags |= DRIVE_MODE;
1111 1.40 thorpej }
1112 1.49 thorpej if ((atac->atac_cap & ATAC_CAP_DMA) == 0) {
1113 1.40 thorpej /* don't care about DMA modes */
1114 1.40 thorpej return;
1115 1.40 thorpej }
1116 1.40 thorpej if (cf_flags & ATA_CONFIG_DMA_SET) {
1117 1.40 thorpej if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
1118 1.40 thorpej ATA_CONFIG_DMA_DISABLE) {
1119 1.40 thorpej drvp->drive_flags &= ~DRIVE_DMA;
1120 1.40 thorpej } else {
1121 1.40 thorpej drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
1122 1.40 thorpej ATA_CONFIG_DMA_OFF;
1123 1.40 thorpej drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
1124 1.40 thorpej }
1125 1.40 thorpej }
1126 1.49 thorpej if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) {
1127 1.40 thorpej /* don't care about UDMA modes */
1128 1.40 thorpej return;
1129 1.40 thorpej }
1130 1.40 thorpej if (cf_flags & ATA_CONFIG_UDMA_SET) {
1131 1.40 thorpej if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
1132 1.40 thorpej ATA_CONFIG_UDMA_DISABLE) {
1133 1.40 thorpej drvp->drive_flags &= ~DRIVE_UDMA;
1134 1.40 thorpej } else {
1135 1.40 thorpej drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
1136 1.40 thorpej ATA_CONFIG_UDMA_OFF;
1137 1.40 thorpej drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
1138 1.40 thorpej }
1139 1.40 thorpej }
1140 1.40 thorpej }
1141 1.40 thorpej
1142 1.30 bouyer /* management of the /dev/atabus* devices */
1143 1.30 bouyer int atabusopen(dev, flag, fmt, p)
1144 1.30 bouyer dev_t dev;
1145 1.30 bouyer int flag, fmt;
1146 1.30 bouyer struct proc *p;
1147 1.30 bouyer {
1148 1.30 bouyer struct atabus_softc *sc;
1149 1.30 bouyer int error, unit = minor(dev);
1150 1.30 bouyer
1151 1.30 bouyer if (unit >= atabus_cd.cd_ndevs ||
1152 1.30 bouyer (sc = atabus_cd.cd_devs[unit]) == NULL)
1153 1.30 bouyer return (ENXIO);
1154 1.30 bouyer
1155 1.30 bouyer if (sc->sc_flags & ATABUSCF_OPEN)
1156 1.30 bouyer return (EBUSY);
1157 1.30 bouyer
1158 1.43 thorpej if ((error = ata_addref(sc->sc_chan)) != 0)
1159 1.30 bouyer return (error);
1160 1.30 bouyer
1161 1.30 bouyer sc->sc_flags |= ATABUSCF_OPEN;
1162 1.30 bouyer
1163 1.30 bouyer return (0);
1164 1.30 bouyer }
1165 1.30 bouyer
1166 1.30 bouyer
1167 1.30 bouyer int
1168 1.30 bouyer atabusclose(dev, flag, fmt, p)
1169 1.30 bouyer dev_t dev;
1170 1.30 bouyer int flag, fmt;
1171 1.30 bouyer struct proc *p;
1172 1.30 bouyer {
1173 1.30 bouyer struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
1174 1.30 bouyer
1175 1.43 thorpej ata_delref(sc->sc_chan);
1176 1.30 bouyer
1177 1.30 bouyer sc->sc_flags &= ~ATABUSCF_OPEN;
1178 1.30 bouyer
1179 1.30 bouyer return (0);
1180 1.30 bouyer }
1181 1.30 bouyer
1182 1.30 bouyer int
1183 1.30 bouyer atabusioctl(dev, cmd, addr, flag, p)
1184 1.30 bouyer dev_t dev;
1185 1.30 bouyer u_long cmd;
1186 1.30 bouyer caddr_t addr;
1187 1.30 bouyer int flag;
1188 1.30 bouyer struct proc *p;
1189 1.30 bouyer {
1190 1.30 bouyer struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
1191 1.48 thorpej struct ata_channel *chp = sc->sc_chan;
1192 1.32 bouyer int min_drive, max_drive, drive;
1193 1.30 bouyer int error;
1194 1.30 bouyer int s;
1195 1.30 bouyer
1196 1.30 bouyer /*
1197 1.30 bouyer * Enforce write permission for ioctls that change the
1198 1.30 bouyer * state of the bus. Host adapter specific ioctls must
1199 1.30 bouyer * be checked by the adapter driver.
1200 1.30 bouyer */
1201 1.30 bouyer switch (cmd) {
1202 1.30 bouyer case ATABUSIOSCAN:
1203 1.30 bouyer case ATABUSIODETACH:
1204 1.30 bouyer case ATABUSIORESET:
1205 1.30 bouyer if ((flag & FWRITE) == 0)
1206 1.30 bouyer return (EBADF);
1207 1.30 bouyer }
1208 1.30 bouyer
1209 1.30 bouyer switch (cmd) {
1210 1.30 bouyer case ATABUSIORESET:
1211 1.30 bouyer s = splbio();
1212 1.30 bouyer wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
1213 1.30 bouyer splx(s);
1214 1.30 bouyer error = 0;
1215 1.30 bouyer break;
1216 1.32 bouyer case ATABUSIOSCAN:
1217 1.32 bouyer {
1218 1.32 bouyer #if 0
1219 1.32 bouyer struct atabusioscan_args *a=
1220 1.32 bouyer (struct atabusioscan_args *)addr;
1221 1.32 bouyer #endif
1222 1.32 bouyer if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
1223 1.32 bouyer (chp->ch_drive[1].drive_flags & DRIVE_OLD))
1224 1.32 bouyer return (EOPNOTSUPP);
1225 1.32 bouyer return (EOPNOTSUPP);
1226 1.32 bouyer }
1227 1.32 bouyer case ATABUSIODETACH:
1228 1.32 bouyer {
1229 1.32 bouyer struct atabusioscan_args *a=
1230 1.32 bouyer (struct atabusioscan_args *)addr;
1231 1.32 bouyer if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
1232 1.32 bouyer (chp->ch_drive[1].drive_flags & DRIVE_OLD))
1233 1.32 bouyer return (EOPNOTSUPP);
1234 1.32 bouyer switch (a->at_dev) {
1235 1.32 bouyer case -1:
1236 1.32 bouyer min_drive = 0;
1237 1.32 bouyer max_drive = 1;
1238 1.32 bouyer break;
1239 1.32 bouyer case 0:
1240 1.32 bouyer case 1:
1241 1.32 bouyer min_drive = max_drive = a->at_dev;
1242 1.32 bouyer break;
1243 1.32 bouyer default:
1244 1.32 bouyer return (EINVAL);
1245 1.32 bouyer }
1246 1.32 bouyer for (drive = min_drive; drive <= max_drive; drive++) {
1247 1.32 bouyer if (chp->ch_drive[drive].drv_softc != NULL) {
1248 1.32 bouyer error = config_detach(
1249 1.32 bouyer chp->ch_drive[drive].drv_softc, 0);
1250 1.32 bouyer if (error)
1251 1.32 bouyer return (error);
1252 1.34 bouyer chp->ch_drive[drive].drv_softc = NULL;
1253 1.32 bouyer }
1254 1.32 bouyer }
1255 1.32 bouyer error = 0;
1256 1.32 bouyer break;
1257 1.32 bouyer }
1258 1.30 bouyer default:
1259 1.30 bouyer error = ENOTTY;
1260 1.30 bouyer }
1261 1.30 bouyer return (error);
1262 1.30 bouyer };
1263