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