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