ata.c revision 1.23 1 /* $NetBSD: ata.c,v 1.23 2003/12/30 16:28:37 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.23 2003/12/30 16:28:37 thorpej Exp $");
34
35 #ifndef WDCDEBUG
36 #define WDCDEBUG
37 #endif /* WDCDEBUG */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/proc.h>
45 #include <sys/kthread.h>
46 #include <sys/errno.h>
47
48 #include <machine/intr.h>
49 #include <machine/bus.h>
50
51 #include <dev/ata/atareg.h>
52 #include <dev/ata/atavar.h>
53 #include <dev/ic/wdcreg.h>
54 #include <dev/ic/wdcvar.h>
55
56 #include "locators.h"
57
58 #define DEBUG_FUNCS 0x08
59 #define DEBUG_PROBE 0x10
60 #define DEBUG_DETACH 0x20
61 #ifdef WDCDEBUG
62 extern int wdcdebug_mask; /* init'ed in wdc.c */
63 #define WDCDEBUG_PRINT(args, level) \
64 if (wdcdebug_mask & (level)) \
65 printf args
66 #else
67 #define WDCDEBUG_PRINT(args, level)
68 #endif
69
70 /*****************************************************************************
71 * ATA bus layer.
72 *
73 * ATA controllers attach an atabus instance, which handles probing the bus
74 * for drives, etc.
75 *****************************************************************************/
76
77 /*
78 * atabusprint:
79 *
80 * Autoconfiguration print routine used by ATA controllers when
81 * attaching an atabus instance.
82 */
83 int
84 atabusprint(void *aux, const char *pnp)
85 {
86 struct channel_softc *chan = aux;
87
88 if (pnp)
89 aprint_normal("atabus at %s", pnp);
90 aprint_normal(" channel %d", chan->channel);
91
92 return (UNCONF);
93 }
94
95 /*
96 * ataprint:
97 *
98 * Autoconfiguration print routine.
99 */
100 int
101 ataprint(void *aux, const char *pnp)
102 {
103 struct ata_device *adev = aux;
104
105 if (pnp)
106 aprint_normal("wd at %s", pnp);
107 aprint_normal(" drive %d", adev->adev_drv_data->drive);
108
109 return (UNCONF);
110 }
111
112 /*
113 * atabus_thread:
114 *
115 * Worker thread for the ATA bus.
116 */
117 static void
118 atabus_thread(void *arg)
119 {
120 struct atabus_softc *sc = arg;
121 struct channel_softc *chp = sc->sc_chan;
122 struct wdc_xfer *xfer;
123 int s;
124
125 s = splbio();
126 chp->ch_flags |= WDCF_TH_RUN;
127 splx(s);
128
129 /* Configure the devices on the bus. */
130 atabusconfig(sc);
131
132 for (;;) {
133 s = splbio();
134 if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
135 ((chp->ch_flags & WDCF_ACTIVE) == 0 ||
136 chp->ch_queue->queue_freeze == 0)) {
137 chp->ch_flags &= ~WDCF_TH_RUN;
138 (void) tsleep(&chp->thread, PRIBIO, "atath", 0);
139 chp->ch_flags |= WDCF_TH_RUN;
140 }
141 splx(s);
142 if (chp->ch_flags & WDCF_SHUTDOWN)
143 break;
144 s = splbio();
145 if (chp->ch_flags & WDCF_TH_RESET) {
146 int drive;
147
148 (void) wdcreset(chp, RESET_SLEEP);
149 for (drive = 0; drive < 2; drive++)
150 chp->ch_drive[drive].state = 0;
151 chp->ch_flags &= ~WDCF_TH_RESET;
152 chp->ch_queue->queue_freeze--;
153 wdcstart(chp);
154 } else if ((chp->ch_flags & WDCF_ACTIVE) != 0 &&
155 chp->ch_queue->queue_freeze == 1) {
156 /*
157 * Caller has bumped queue_freeze, decrease it.
158 */
159 chp->ch_queue->queue_freeze--;
160 xfer = TAILQ_FIRST(&chp->ch_queue->sc_xfer);
161 KASSERT(xfer != NULL);
162 (*xfer->c_start)(chp, xfer);
163 } else if (chp->ch_queue->queue_freeze > 1)
164 panic("ata_thread: queue_freeze");
165 splx(s);
166 }
167 chp->thread = NULL;
168 wakeup(&chp->ch_flags);
169 kthread_exit(0);
170 }
171
172 /*
173 * atabus_create_thread:
174 *
175 * Helper routine to create the ATA bus worker thread.
176 */
177 static void
178 atabus_create_thread(void *arg)
179 {
180 struct atabus_softc *sc = arg;
181 struct channel_softc *chp = sc->sc_chan;
182 int error;
183
184 if ((error = kthread_create1(atabus_thread, sc, &chp->thread,
185 "%s", sc->sc_dev.dv_xname)) != 0)
186 aprint_error("%s: unable to create kernel thread: error %d\n",
187 sc->sc_dev.dv_xname, error);
188 }
189
190 /*
191 * atabus_match:
192 *
193 * Autoconfiguration match routine.
194 */
195 static int
196 atabus_match(struct device *parent, struct cfdata *cf, void *aux)
197 {
198 struct channel_softc *chp = aux;
199
200 if (chp == NULL)
201 return (0);
202
203 if (cf->cf_loc[ATACF_CHANNEL] != chp->channel &&
204 cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
205 return (0);
206
207 return (1);
208 }
209
210 /*
211 * atabus_attach:
212 *
213 * Autoconfiguration attach routine.
214 */
215 static void
216 atabus_attach(struct device *parent, struct device *self, void *aux)
217 {
218 struct atabus_softc *sc = (void *) self;
219 struct channel_softc *chp = aux;
220 struct atabus_initq *initq;
221
222 sc->sc_chan = chp;
223
224 aprint_normal("\n");
225 aprint_naive("\n");
226
227 initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
228 initq->atabus_sc = sc;
229 TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
230 config_pending_incr();
231 kthread_create(atabus_create_thread, sc);
232 }
233
234 /*
235 * atabus_activate:
236 *
237 * Autoconfiguration activation routine.
238 */
239 static int
240 atabus_activate(struct device *self, enum devact act)
241 {
242 struct atabus_softc *sc = (void *) self;
243 struct channel_softc *chp = sc->sc_chan;
244 struct device *dev = NULL;
245 int s, i, error = 0;
246
247 s = splbio();
248 switch (act) {
249 case DVACT_ACTIVATE:
250 error = EOPNOTSUPP;
251 break;
252
253 case DVACT_DEACTIVATE:
254 /*
255 * We might deactivate the children of atapibus twice
256 * (once bia atapibus, once directly), but since the
257 * generic autoconfiguration code maintains the DVF_ACTIVE
258 * flag, it's safe.
259 */
260 if ((dev = chp->atapibus) != NULL) {
261 error = config_deactivate(dev);
262 if (error)
263 goto out;
264 }
265
266 for (i = 0; i < 2; i++) {
267 if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
268 WDCDEBUG_PRINT(("atabus_activate: %s: "
269 "deactivating %s\n", sc->sc_dev.dv_xname,
270 dev->dv_xname),
271 DEBUG_DETACH);
272 error = config_deactivate(dev);
273 if (error)
274 goto out;
275 }
276 }
277 break;
278 }
279 out:
280 splx(s);
281
282 #ifdef WDCDEBUG
283 if (dev != NULL && error != 0)
284 WDCDEBUG_PRINT(("atabus_activate: %s: "
285 "error %d deactivating %s\n", sc->sc_dev.dv_xname,
286 error, dev->dv_xname), DEBUG_DETACH);
287 #endif /* WDCDEBUG */
288
289 return (error);
290 }
291
292 /*
293 * atabus_detach:
294 *
295 * Autoconfiguration detach routine.
296 */
297 static int
298 atabus_detach(struct device *self, int flags)
299 {
300 struct atabus_softc *sc = (void *) self;
301 struct channel_softc *chp = sc->sc_chan;
302 struct device *dev = NULL;
303 int i, error = 0;
304
305 /* Shutdown the channel. */
306 /* XXX NEED AN INTERLOCK HERE. */
307 chp->ch_flags |= WDCF_SHUTDOWN;
308 wakeup(&chp->thread);
309 while (chp->thread != NULL)
310 (void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0);
311
312 /*
313 * Detach atapibus and its children.
314 */
315 if ((dev = chp->atapibus) != NULL) {
316 WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
317 sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
318 error = config_detach(dev, flags);
319 if (error)
320 goto out;
321 }
322
323 /*
324 * Detach our other children.
325 */
326 for (i = 0; i < 2; i++) {
327 if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
328 continue;
329 if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
330 WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
331 sc->sc_dev.dv_xname, dev->dv_xname),
332 DEBUG_DETACH);
333 error = config_detach(dev, flags);
334 if (error)
335 goto out;
336 }
337 }
338
339 wdc_kill_pending(chp);
340 out:
341 #ifdef WDCDEBUG
342 if (dev != NULL && error != 0)
343 WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
344 sc->sc_dev.dv_xname, error, dev->dv_xname),
345 DEBUG_DETACH);
346 #endif /* WDCDEBUG */
347
348 return (error);
349 }
350
351 CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
352 atabus_match, atabus_attach, atabus_detach, atabus_activate);
353
354 /*****************************************************************************
355 * Common ATA bus operations.
356 *****************************************************************************/
357
358 /* Get the disk's parameters */
359 int
360 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
361 struct ataparams *prms)
362 {
363 char tb[DEV_BSIZE];
364 struct wdc_command wdc_c;
365
366 #if BYTE_ORDER == LITTLE_ENDIAN
367 int i;
368 u_int16_t *p;
369 #endif
370
371 WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
372
373 memset(tb, 0, DEV_BSIZE);
374 memset(prms, 0, sizeof(struct ataparams));
375 memset(&wdc_c, 0, sizeof(struct wdc_command));
376
377 if (drvp->drive_flags & DRIVE_ATA) {
378 wdc_c.r_command = WDCC_IDENTIFY;
379 wdc_c.r_st_bmask = WDCS_DRDY;
380 wdc_c.r_st_pmask = 0;
381 wdc_c.timeout = 3000; /* 3s */
382 } else if (drvp->drive_flags & DRIVE_ATAPI) {
383 wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
384 wdc_c.r_st_bmask = 0;
385 wdc_c.r_st_pmask = 0;
386 wdc_c.timeout = 10000; /* 10s */
387 } else {
388 WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
389 DEBUG_FUNCS|DEBUG_PROBE);
390 return CMD_ERR;
391 }
392 wdc_c.flags = AT_READ | flags;
393 wdc_c.data = tb;
394 wdc_c.bcount = DEV_BSIZE;
395 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
396 WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
397 DEBUG_FUNCS|DEBUG_PROBE);
398 return CMD_AGAIN;
399 }
400 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
401 WDCDEBUG_PRINT(("ata_get_parms: wdc_c.flags=0x%x\n",
402 wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
403 return CMD_ERR;
404 } else {
405 /* if we didn't read any data something is wrong */
406 if ((wdc_c.flags & AT_XFDONE) == 0)
407 return CMD_ERR;
408 /* Read in parameter block. */
409 memcpy(prms, tb, sizeof(struct ataparams));
410 #if BYTE_ORDER == LITTLE_ENDIAN
411 /*
412 * Shuffle string byte order.
413 * ATAPI Mitsumi and NEC drives don't need this.
414 */
415 if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
416 WDC_CFG_ATAPI &&
417 ((prms->atap_model[0] == 'N' &&
418 prms->atap_model[1] == 'E') ||
419 (prms->atap_model[0] == 'F' &&
420 prms->atap_model[1] == 'X')))
421 return 0;
422 for (i = 0; i < sizeof(prms->atap_model); i += 2) {
423 p = (u_short *)(prms->atap_model + i);
424 *p = ntohs(*p);
425 }
426 for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
427 p = (u_short *)(prms->atap_serial + i);
428 *p = ntohs(*p);
429 }
430 for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
431 p = (u_short *)(prms->atap_revision + i);
432 *p = ntohs(*p);
433 }
434 #endif
435 return CMD_OK;
436 }
437 }
438
439 int
440 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
441 {
442 struct wdc_command wdc_c;
443
444 WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
445 memset(&wdc_c, 0, sizeof(struct wdc_command));
446
447 wdc_c.r_command = SET_FEATURES;
448 wdc_c.r_st_bmask = 0;
449 wdc_c.r_st_pmask = 0;
450 wdc_c.r_precomp = WDSF_SET_MODE;
451 wdc_c.r_count = mode;
452 wdc_c.flags = flags;
453 wdc_c.timeout = 1000; /* 1s */
454 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
455 return CMD_AGAIN;
456 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
457 return CMD_ERR;
458 }
459 return CMD_OK;
460 }
461
462 void
463 ata_dmaerr(struct ata_drive_datas *drvp, int flags)
464 {
465 /*
466 * Downgrade decision: if we get NERRS_MAX in NXFER.
467 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
468 * first error within the first NXFER ops will immediatly trigger
469 * a downgrade.
470 * If we got an error and n_xfers is bigger than NXFER reset counters.
471 */
472 drvp->n_dmaerrs++;
473 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
474 wdc_downgrade_mode(drvp, flags);
475 drvp->n_dmaerrs = NERRS_MAX-1;
476 drvp->n_xfers = 0;
477 return;
478 }
479 if (drvp->n_xfers > NXFER) {
480 drvp->n_dmaerrs = 1; /* just got an error */
481 drvp->n_xfers = 1; /* restart counting from this error */
482 }
483 }
484