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