Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / disk / ata / atapi-all.c
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
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. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ata/atapi-all.c,v 1.46.2.18 2002/10/31 23:10:33 thomas Exp $
29  */
30
31 #include "opt_ata.h"
32 #include "atapicd.h"
33 #include "atapifd.h"
34 #include "atapist.h"
35 #include "atapicam.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/ata.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/malloc.h>
42 #include <sys/buf.h>
43 #include <sys/sysctl.h>
44 #include <machine/bus.h>
45 #include <machine/clock.h>
46 #include <sys/rman.h>
47 #include <dev/ata/ata-all.h>
48 #include <dev/ata/atapi-all.h>
49
50 /* prototypes */
51 static void atapi_read(struct atapi_request *, int);
52 static void atapi_write(struct atapi_request *, int);
53 static void atapi_finish(struct atapi_request *);
54 static void atapi_timeout(struct atapi_request *);
55 static char *atapi_cmd2str(u_int8_t);
56 static char *atapi_skey2str(u_int8_t);
57
58 /* misc defines */
59 #define ATAPI_MAX_RETRIES       3
60
61 /* internal vars */
62 static int atapi_dma = 0;
63 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
64 static MALLOC_DEFINE(M_ATAPI, "ATAPI generic", "ATAPI driver generic layer");
65
66 /* systcl vars */
67 SYSCTL_DECL(_hw_ata);
68 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RD, &atapi_dma, 0,
69            "ATAPI device DMA mode control");
70
71 void
72 atapi_attach(struct ata_device *atadev)
73 {
74     if (bootverbose) 
75         ata_prtdev(atadev, "piomode=%d dmamode=%d udmamode=%d dmaflag=%d\n",
76                    ata_pmode(atadev->param), ata_wmode(atadev->param),
77                    ata_umode(atadev->param), atadev->param->support_dma);
78
79     ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
80     if (atapi_dma && !(atadev->param->drq_type == ATAPI_DRQT_INTR)) {
81         ata_dmainit(atadev->channel, atadev->unit,
82                     (ata_pmode(atadev->param) < 0) ? 
83                     (atadev->param->support_dma ? 4:0):ata_pmode(atadev->param),
84                     (ata_wmode(atadev->param) < 0) ? 
85                     (atadev->param->support_dma ? 2:0):ata_wmode(atadev->param),
86                     ata_umode(atadev->param));
87     }
88     else
89         ata_dmainit(atadev->channel, atadev->unit,
90                     ata_pmode(atadev->param) < 0 ? 0 : ata_pmode(atadev->param),
91                     -1, -1);
92     ATA_UNLOCK_CH(atadev->channel);
93
94     if (!(atadev->result = malloc(sizeof(struct atapi_reqsense), M_ATAPI,
95                                   M_NOWAIT | M_ZERO)))
96         ata_prtdev(atadev, "no memory for sense data\n");
97
98     switch (atadev->param->type) {
99 #if NATAPICD > 0
100     case ATAPI_TYPE_CDROM:
101         if (acdattach(atadev))
102             return;
103         break; 
104 #endif
105 #if NATAPIFD > 0
106     case ATAPI_TYPE_DIRECT:
107         if (afdattach(atadev))
108             return;
109         break; 
110 #endif
111 #if NATAPIST > 0
112     case ATAPI_TYPE_TAPE:
113         if (astattach(atadev))
114             return;
115         break; 
116 #endif
117     }
118 #if NATAPICAM == 0
119     ata_prtdev(atadev, "<%.40s/%.8s> - NO DRIVER!\n",
120                atadev->param->model, atadev->param->revision);
121     free(atadev->result, M_ATAPI);
122     atadev->driver = NULL;
123 #endif
124 }
125
126 void
127 atapi_detach(struct ata_device *atadev)
128 {
129     struct atapi_request *request;
130
131     atadev->flags |= ATA_D_DETACHING;
132     ata_prtdev(atadev, "removed from configuration\n");
133     switch (atadev->param->type) {
134 #if NATAPICD > 0
135     case ATAPI_TYPE_CDROM:
136         acddetach(atadev);
137         break; 
138 #endif
139 #if NATAPIFD >0
140     case ATAPI_TYPE_DIRECT:
141         afddetach(atadev);
142         break; 
143 #endif
144 #if NATAPIST >0
145     case ATAPI_TYPE_TAPE:
146         astdetach(atadev);
147         break; 
148 #endif
149     default:
150         return;
151     }
152     TAILQ_FOREACH(request, &atadev->channel->atapi_queue, chain) {
153         if (request->device != atadev)
154             continue;
155         TAILQ_REMOVE(&atadev->channel->atapi_queue, request, chain);
156         if (request->driver) {
157             struct buf *bp = (struct buf *) request->driver;
158             bp->b_flags |= B_ERROR;
159             bp->b_error = ENXIO;
160             biodone(bp);
161         }
162         if (request->dmatab)
163             free(request->dmatab, M_DEVBUF);
164         free(request, M_ATAPI);
165     }
166     free(atadev->result, M_ATAPI);
167     atadev->driver = NULL;
168     atadev->flags = 0;
169
170 }
171
172 int       
173 atapi_queue_cmd(struct ata_device *atadev, int8_t *ccb, caddr_t data, 
174                 int count, int flags, int timeout,
175                 atapi_callback_t callback, void *driver)
176 {
177     struct atapi_request *request;
178     int error, s;
179  
180     if (!(request = malloc(sizeof(struct atapi_request), M_ATAPI,
181                            M_NOWAIT | M_ZERO)))
182         return ENOMEM;
183
184     request->device = atadev;
185     request->data = data;
186     request->bytecount = count;
187     request->flags = flags;
188     request->error = EINPROGRESS;
189     request->timeout = timeout * hz;
190     request->ccbsize = atadev->param->packet_size ? 16 : 12;
191     bcopy(ccb, request->ccb, request->ccbsize);
192     if (callback) {
193         request->callback = callback;
194         request->driver = driver;
195     }
196     if (atadev->mode >= ATA_DMA) {
197         if (!(request->dmatab = ata_dmaalloc(atadev->channel, atadev->unit)))
198             atadev->mode = ATA_PIO;
199     }
200
201 #ifdef ATAPI_DEBUG
202     ata_prtdev(atadev, "queueing %s ", atapi_cmd2str(request->ccb[0]));
203     atapi_dump("ccb = ", &request->ccb[0], sizeof(request->ccb));
204 #endif
205     /* append onto controller queue and try to start controller */
206     s = splbio();
207     if (flags & ATPR_F_AT_HEAD)
208         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
209     else
210         TAILQ_INSERT_TAIL(&atadev->channel->atapi_queue, request, chain);
211     ata_start(atadev->channel);
212
213     /* if callback used, then just return, gets called from interrupt context */
214     if (callback) {
215         splx(s);
216         return 0;
217     }
218
219     /* only sleep when command is in progress */
220     if (request->error == EINPROGRESS)
221         tsleep((caddr_t)request, PRIBIO, "atprq", 0);
222     splx(s);
223     error = request->error;
224     if (error)
225          bcopy(&request->sense, atadev->result, sizeof(struct atapi_reqsense));
226     if (request->dmatab)
227         free(request->dmatab, M_DEVBUF);
228     free(request, M_ATAPI);
229     return error;
230 }
231     
232 void
233 atapi_start(struct ata_device *atadev)
234 {
235     switch (atadev->param->type) {
236 #if NATAPICD > 0
237     case ATAPI_TYPE_CDROM:
238         acd_start(atadev);
239         break; 
240 #endif
241 #if NATAPIFD > 0
242     case ATAPI_TYPE_DIRECT:
243         afd_start(atadev);
244         break; 
245 #endif
246 #if NATAPIST > 0
247     case ATAPI_TYPE_TAPE:
248         ast_start(atadev);
249         break; 
250 #endif
251     default:
252         return;
253     }
254 }
255
256 int
257 atapi_transfer(struct atapi_request *request)
258 {
259     struct ata_device *atadev = request->device;
260     int timout;
261     u_int8_t reason;
262
263 #ifdef ATAPI_DEBUG
264     ata_prtdev(atadev, "starting %s ", atapi_cmd2str(request->ccb[0]));
265     atapi_dump("ccb = ", &request->ccb[0], sizeof(request->ccb));
266 #endif
267     /* is this just a POLL DSC command ? */
268     if (request->ccb[0] == ATAPI_POLL_DSC) {
269         ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
270         DELAY(10);
271         if (ATA_INB(atadev->channel->r_altio, ATA_ALTSTAT) & ATA_S_DSC)
272             request->error = 0;
273         else
274             request->error = EBUSY;
275         atapi_finish(request);
276         return ATA_OP_FINISHED;
277     }
278
279     /* start timeout for this command */
280     request->timeout_handle = timeout((timeout_t *)atapi_timeout, 
281                                       request, request->timeout);
282
283     if (!(request->flags & ATPR_F_INTERNAL))
284         atadev->cmd = request->ccb[0];
285
286     /* if DMA enabled setup DMA hardware */
287     request->flags &= ~ATPR_F_DMA_USED; 
288     if ((atadev->mode >= ATA_DMA) &&
289         (request->ccb[0] == ATAPI_READ || 
290          request->ccb[0] == ATAPI_READ_BIG ||
291          request->ccb[0] == ATAPI_READ_CD ||
292          ((request->ccb[0] == ATAPI_WRITE ||
293            request->ccb[0] == ATAPI_WRITE_BIG) &&
294           !(atadev->channel->flags & ATA_ATAPI_DMA_RO))) &&
295         !ata_dmasetup(atadev->channel, atadev->unit, request->dmatab,
296                       (void *)request->data, request->bytecount)) {
297         request->flags |= ATPR_F_DMA_USED;
298     }
299
300     /* start ATAPI operation */
301     if (ata_command(atadev, ATA_C_PACKET_CMD, 
302                     min(request->bytecount, 65534) << 8, 0,
303                     (request->flags & ATPR_F_DMA_USED) ? ATA_F_DMA : 0,
304                     ATA_IMMEDIATE))
305         ata_prtdev(atadev, "failure to send ATAPI packet command\n");
306
307     if (request->flags & ATPR_F_DMA_USED)
308         ata_dmastart(atadev->channel, atadev->unit, 
309                      request->dmatab, request->flags & ATPR_F_READ);
310
311     /* command interrupt device ? just return */
312     if (atadev->param->drq_type == ATAPI_DRQT_INTR)
313         return ATA_OP_CONTINUES;
314
315     /* ready to write ATAPI command */
316     timout = 5000; /* might be less for fast devices */
317     while (timout--) {
318         reason = ATA_INB(atadev->channel->r_io, ATA_IREASON);
319         atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
320         if (((reason & (ATA_I_CMD | ATA_I_IN)) |
321              (atadev->channel->status&(ATA_S_DRQ|ATA_S_BUSY)))==ATAPI_P_CMDOUT)
322             break;
323         DELAY(20);
324     }
325     if (timout <= 0) {
326         ata_prtdev(atadev, "failure to execute ATAPI packet command\n");
327         untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
328         request->error = EIO;
329         atapi_finish(request);  
330         return ATA_OP_FINISHED;
331     }
332
333     /* this seems to be needed for some (slow) devices */
334     DELAY(10);
335
336     /* send actual command */
337     ATA_OUTSW(atadev->channel->r_io, ATA_DATA, (int16_t *)request->ccb,
338               request->ccbsize / sizeof(int16_t));
339     return ATA_OP_CONTINUES;
340 }
341
342 int
343 atapi_interrupt(struct atapi_request *request)
344 {
345     struct ata_device *atadev = request->device;
346     int reason, dma_stat = 0;
347
348     reason = (ATA_INB(atadev->channel->r_io, ATA_IREASON)&(ATA_I_CMD|ATA_I_IN))|
349              (atadev->channel->status & ATA_S_DRQ);
350
351     if (reason == ATAPI_P_CMDOUT) {
352         if (!(atadev->channel->status & ATA_S_DRQ)) {
353             ata_prtdev(atadev, "command interrupt without DRQ\n");
354             untimeout((timeout_t *)atapi_timeout,
355                       request, request->timeout_handle);
356             request->error = EIO;
357             atapi_finish(request);      
358             return ATA_OP_FINISHED;
359         }
360         ATA_OUTSW(atadev->channel->r_io, ATA_DATA, (int16_t *)request->ccb,
361                   request->ccbsize / sizeof(int16_t));
362         return ATA_OP_CONTINUES;
363     }
364
365     if (request->flags & ATPR_F_DMA_USED) {
366         dma_stat = ata_dmadone(atadev->channel);
367         if ((atadev->channel->status & (ATA_S_ERROR | ATA_S_DWF)) ||
368             dma_stat & ATA_BMSTAT_ERROR) {
369             request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
370         }
371         else {
372             request->result = 0;
373             request->donecount = request->bytecount;
374             request->bytecount = 0;
375         }
376     }
377     else {
378         int length = ATA_INB(atadev->channel->r_io, ATA_CYL_LSB) |
379                      ATA_INB(atadev->channel->r_io, ATA_CYL_MSB) << 8;
380
381         switch (reason) {
382         case ATAPI_P_WRITE:
383             if (request->flags & ATPR_F_READ) {
384                 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
385                 ata_prtdev(atadev, "%s trying to write on read buffer\n",
386                            atapi_cmd2str(atadev->cmd));
387                 break;
388             }
389             atapi_write(request, length);
390             return ATA_OP_CONTINUES;
391         
392         case ATAPI_P_READ:
393             if (!(request->flags & ATPR_F_READ)) {
394                 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
395                 ata_prtdev(atadev, "%s trying to read on write buffer\n",
396                            atapi_cmd2str(atadev->cmd));
397                 break;
398             }
399             atapi_read(request, length);
400             return ATA_OP_CONTINUES;
401
402         case ATAPI_P_DONEDRQ:
403             ata_prtdev(atadev, "%s DONEDRQ\n", atapi_cmd2str(atadev->cmd));
404             if (request->flags & ATPR_F_READ)
405                 atapi_read(request, length);
406             else
407                 atapi_write(request, length);
408             /* FALLTHROUGH */
409
410         case ATAPI_P_ABORT:
411         case ATAPI_P_DONE:
412             if (atadev->channel->status & (ATA_S_ERROR | ATA_S_DWF))
413                 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
414             else 
415                 if (!(request->flags & ATPR_F_INTERNAL))
416                     request->result = 0;
417             break;
418
419         default:
420             ata_prtdev(atadev, "unknown transfer phase %d\n", reason);
421         }
422     }
423     untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
424
425     /* check for error, if valid sense key, queue a request sense cmd */
426     if ((request->result & ATAPI_SK_MASK) && 
427         request->ccb[0] != ATAPI_REQUEST_SENSE) {
428         bzero(request->ccb, request->ccbsize);
429         request->ccb[0] = ATAPI_REQUEST_SENSE;
430         request->ccb[4] = sizeof(struct atapi_reqsense);
431         request->bytecount = sizeof(struct atapi_reqsense);
432         request->flags &= ATPR_F_QUIET;
433         request->flags |= ATPR_F_READ | ATPR_F_INTERNAL;
434         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
435     }
436     else {
437         if (request->result) {
438             switch ((request->result & ATAPI_SK_MASK)) {
439             case ATAPI_SK_NO_SENSE:
440                 request->error = 0;
441                 break;
442
443             case ATAPI_SK_RECOVERED_ERROR:
444                 ata_prtdev(atadev, "%s - recovered error\n",
445                            atapi_cmd2str(atadev->cmd));
446                 request->error = 0;
447                 break;
448
449             case ATAPI_SK_NOT_READY:
450                 request->error = EBUSY;
451                 break;
452
453             case ATAPI_SK_UNIT_ATTENTION:
454                 atadev->flags |= ATA_D_MEDIA_CHANGED;
455                 request->error = EIO;
456                 break;
457
458             default: 
459                 request->error = EIO;
460
461                 if (request->flags & ATPR_F_QUIET)
462                     break;
463
464                 ata_prtdev(atadev, "%s - %s asc=0x%02x ascq=0x%02x ",
465                            atapi_cmd2str(atadev->cmd), 
466                            atapi_skey2str(request->sense.sense_key), 
467                            request->sense.asc, request->sense.ascq);
468                 if (request->sense.sksv)
469                     printf("sks=0x%02x 0x%02x 0x%02x ",
470                            request->sense.sk_specific,
471                            request->sense.sk_specific1,
472                            request->sense.sk_specific2);
473                 printf("error=0x%02x\n", request->result & ATAPI_E_MASK);
474             }
475         }
476         else
477             request->error = 0;
478         atapi_finish(request);  
479     }
480     return ATA_OP_FINISHED;
481 }
482
483 void
484 atapi_reinit(struct ata_device *atadev)
485 {
486     /* reinit device parameters */
487      if (atadev->mode >= ATA_DMA)
488         ata_dmainit(atadev->channel, atadev->unit,
489                     (ata_pmode(atadev->param) < 0) ?
490                     (atadev->param->support_dma ? 4:0):ata_pmode(atadev->param),
491                     (ata_wmode(atadev->param) < 0) ? 
492                     (atadev->param->support_dma ? 2:0):ata_wmode(atadev->param),
493                     ata_umode(atadev->param));
494     else
495         ata_dmainit(atadev->channel, atadev->unit,
496                     ata_pmode(atadev->param)<0 ? 0 : ata_pmode(atadev->param),
497                     -1, -1);
498 }
499
500 int
501 atapi_test_ready(struct ata_device *atadev)
502 {
503     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
504                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
505         
506     return atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 30, NULL, NULL);
507 }
508         
509 int
510 atapi_wait_dsc(struct ata_device *atadev, int timeout)
511 {
512     int error = 0;
513     int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
514                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
515
516     timeout *= hz;
517     while (timeout > 0) {
518         error = atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 0, NULL, NULL);
519         if (error != EBUSY)
520             break;
521         tsleep((caddr_t)&error, PRIBIO, "atpwt", hz / 2);
522         timeout -= (hz / 2);
523     }
524     return error;
525 }
526
527 void
528 atapi_dump(char *label, void *data, int len)
529 {
530     u_int8_t *p = data;
531
532     printf("%s %02x", label, *p++);
533     while (--len > 0) 
534         printf ("-%02x", *p++);
535     printf("\n");
536 }
537
538 static void
539 atapi_read(struct atapi_request *request, int length)
540 {
541     int8_t **buffer = (int8_t **)&request->data;
542     int size = min(request->bytecount, length);
543     struct ata_channel *ch = request->device->channel;
544     int resid;
545
546     if (request->flags & ATPR_F_INTERNAL)
547         *buffer = (int8_t *)&request->sense;
548
549     if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
550         ATA_INSW(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer), 
551                  size / sizeof(int16_t));
552     else
553         ATA_INSL(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
554                  size / sizeof(int32_t));
555
556     if (request->bytecount < length) {
557         ata_prtdev(request->device, "read data overrun %d/%d\n",
558                    length, request->bytecount);
559         for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
560              ATA_INW(ch->r_io, ATA_DATA);
561     }
562     *buffer += size;
563     request->bytecount -= size;
564     request->donecount += size;
565 }
566
567 static void
568 atapi_write(struct atapi_request *request, int length)
569 {
570     int8_t **buffer = (int8_t **)&request->data;
571     int size = min(request->bytecount, length);
572     struct ata_channel *ch = request->device->channel;
573     int resid;
574
575     if (request->flags & ATPR_F_INTERNAL)
576         *buffer = (int8_t *)&request->sense;
577
578     if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
579         ATA_OUTSW(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
580                   size / sizeof(int16_t));
581     else
582         ATA_OUTSL(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
583                   size / sizeof(int32_t));
584
585     if (request->bytecount < length) {
586         ata_prtdev(request->device, "write data underrun %d/%d\n",
587                    length, request->bytecount);
588         for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
589             ATA_OUTW(ch->r_io, ATA_DATA, 0);
590     }
591     *buffer += size;
592     request->bytecount -= size;
593     request->donecount += size;
594 }
595
596 static void
597 atapi_finish(struct atapi_request *request)
598 {
599 #ifdef ATAPI_DEBUG
600     ata_prtdev(request->device, "finished %s%s\n",
601                request->callback ? "callback " : "",
602                atapi_cmd2str(request->ccb[0]));
603 #endif
604     if (request->callback) {
605         if (!((request->callback)(request))) {
606             if (request->dmatab)
607                 free(request->dmatab, M_DEVBUF);
608             free(request, M_ATAPI);
609         }
610     }
611     else 
612         wakeup((caddr_t)request);       
613 }
614
615 static void 
616 atapi_timeout(struct atapi_request *request)
617 {
618     struct ata_device *atadev = request->device;
619
620     atadev->channel->running = NULL;
621     ata_prtdev(atadev, "%s command timeout - resetting\n", 
622                atapi_cmd2str(request->ccb[0]));
623
624     if (request->flags & ATPR_F_DMA_USED) {
625         ata_dmadone(atadev->channel);
626         if (request->retries == ATAPI_MAX_RETRIES) {
627             ata_dmainit(atadev->channel, atadev->unit,
628                         (ata_pmode(atadev->param) < 0) ? 0 :
629                          ata_pmode(atadev->param), -1, -1);
630             ata_prtdev(atadev, "trying fallback to PIO mode\n");
631             request->retries = 0;
632         }
633     }
634
635     /* if retries still permit, reinject this request */
636     if (request->retries++ < ATAPI_MAX_RETRIES) {
637         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
638     }
639     else {
640         /* retries all used up, return error */
641         request->error = EIO;
642         wakeup((caddr_t)request);
643     } 
644     ata_reinit(atadev->channel);
645 }
646
647 static char *
648 atapi_cmd2str(u_int8_t cmd)
649 {
650     switch (cmd) {
651     case 0x00: return ("TEST_UNIT_READY");
652     case 0x01: return ("REZERO");
653     case 0x03: return ("REQUEST_SENSE");
654     case 0x04: return ("FORMAT_UNIT");
655     case 0x08: return ("READ");
656     case 0x0a: return ("WRITE");
657     case 0x10: return ("WEOF");
658     case 0x11: return ("SPACE");
659     case 0x15: return ("MODE_SELECT");
660     case 0x19: return ("ERASE");
661     case 0x1a: return ("MODE_SENSE");
662     case 0x1b: return ("START_STOP");
663     case 0x1e: return ("PREVENT_ALLOW");
664     case 0x25: return ("READ_CAPACITY");
665     case 0x28: return ("READ_BIG");
666     case 0x2a: return ("WRITE_BIG");
667     case 0x2b: return ("LOCATE");
668     case 0x34: return ("READ_POSITION");
669     case 0x35: return ("SYNCHRONIZE_CACHE");
670     case 0x3b: return ("WRITE_BUFFER");
671     case 0x3c: return ("READ_BUFFER");
672     case 0x42: return ("READ_SUBCHANNEL");
673     case 0x43: return ("READ_TOC");
674     case 0x45: return ("PLAY_10");
675     case 0x47: return ("PLAY_MSF");
676     case 0x48: return ("PLAY_TRACK");
677     case 0x4b: return ("PAUSE");
678     case 0x51: return ("READ_DISK_INFO");
679     case 0x52: return ("READ_TRACK_INFO");
680     case 0x53: return ("RESERVE_TRACK");
681     case 0x54: return ("SEND_OPC_INFO");
682     case 0x55: return ("MODE_SELECT_BIG");
683     case 0x58: return ("REPAIR_TRACK");
684     case 0x59: return ("READ_MASTER_CUE");
685     case 0x5a: return ("MODE_SENSE_BIG");
686     case 0x5b: return ("CLOSE_TRACK/SESSION");
687     case 0x5c: return ("READ_BUFFER_CAPACITY");
688     case 0x5d: return ("SEND_CUE_SHEET");
689     case 0xa1: return ("BLANK_CMD");
690     case 0xa3: return ("SEND_KEY");
691     case 0xa4: return ("REPORT_KEY");
692     case 0xa5: return ("PLAY_12");
693     case 0xa6: return ("LOAD_UNLOAD");
694     case 0xad: return ("READ_DVD_STRUCTURE");
695     case 0xb4: return ("PLAY_CD");
696     case 0xbb: return ("SET_SPEED");
697     case 0xbd: return ("MECH_STATUS");
698     case 0xbe: return ("READ_CD");
699     case 0xff: return ("POLL_DSC");
700     default: {
701         static char buffer[20];
702         sprintf(buffer, "unknown CMD (0x%02x)", cmd);
703         return buffer;
704         }
705     }
706 }
707
708 static char *
709 atapi_skey2str(u_int8_t skey)
710 {
711     switch (skey) {
712     case 0x00: return ("NO SENSE");
713     case 0x01: return ("RECOVERED ERROR");
714     case 0x02: return ("NOT READY");
715     case 0x03: return ("MEDIUM ERROR");
716     case 0x04: return ("HARDWARE ERROR");
717     case 0x05: return ("ILLEGAL REQUEST");
718     case 0x06: return ("UNIT ATTENTION");
719     case 0x07: return ("DATA PROTECT");
720     case 0x08: return ("BLANK CHECK");
721     case 0x09: return ("VENDOR SPECIFIC");
722     case 0x0a: return ("COPY ABORTED");
723     case 0x0b: return ("ABORTED COMMAND");
724     case 0x0c: return ("EQUAL");
725     case 0x0d: return ("VOLUME OVERFLOW");
726     case 0x0e: return ("MISCOMPARE");
727     case 0x0f: return ("RESERVED");
728     default: return("UNKNOWN");
729     }
730 }