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