1cfdf75dc5cdcbe8811dae1c0c6d2df651fac331
[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.9 2004/02/18 04:08:49 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     free(request, M_ATAPI);
234     return error;
235 }
236     
237 void
238 atapi_start(struct ata_device *atadev)
239 {
240     switch (atadev->param->type) {
241 #if NATAPICD > 0
242     case ATAPI_TYPE_CDROM:
243         acd_start(atadev);
244         break; 
245 #endif
246 #if NATAPIFD > 0
247     case ATAPI_TYPE_DIRECT:
248         afd_start(atadev);
249         break; 
250 #endif
251 #if NATAPIST > 0
252     case ATAPI_TYPE_TAPE:
253         ast_start(atadev);
254         break; 
255 #endif
256     default:
257         return;
258     }
259 }
260
261 int
262 atapi_transfer(struct atapi_request *request)
263 {
264     struct ata_device *atadev = request->device;
265     int timout;
266     u_int8_t reason;
267
268 #ifdef ATAPI_DEBUG
269     ata_prtdev(atadev, "starting %s ", atapi_cmd2str(request->ccb[0]));
270     atapi_dump("ccb = ", &request->ccb[0], sizeof(request->ccb));
271 #endif
272     /* is this just a POLL DSC command ? */
273     if (request->ccb[0] == ATAPI_POLL_DSC) {
274         ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
275         DELAY(10);
276         if (ATA_INB(atadev->channel->r_altio, ATA_ALTSTAT) & ATA_S_DSC)
277             request->error = 0;
278         else
279             request->error = EBUSY;
280         atapi_finish(request);
281         return ATA_OP_FINISHED;
282     }
283
284     /* start timeout for this command */
285     request->timeout_handle = timeout((timeout_t *)atapi_timeout, 
286                                       request, request->timeout);
287
288     if (!(request->flags & ATPR_F_INTERNAL))
289         atadev->cmd = request->ccb[0];
290
291     /* if DMA enabled setup DMA hardware */
292     request->flags &= ~ATPR_F_DMA_USED; 
293     if ((atadev->mode >= ATA_DMA) &&
294         (request->ccb[0] == ATAPI_READ || 
295          request->ccb[0] == ATAPI_READ_BIG ||
296          request->ccb[0] == ATAPI_READ_CD ||
297          ((request->ccb[0] == ATAPI_WRITE ||
298            request->ccb[0] == ATAPI_WRITE_BIG) &&
299           !(atadev->channel->flags & ATA_ATAPI_DMA_RO))) &&
300         !ata_dmasetup(atadev, (void *)request->data, request->bytecount)) {
301         request->flags |= ATPR_F_DMA_USED;
302     }
303
304     /* start ATAPI operation */
305     if (ata_command(atadev, ATA_C_PACKET_CMD, 
306                     min(request->bytecount, 65534) << 8, 0,
307                     (request->flags & ATPR_F_DMA_USED) ? ATA_F_DMA : 0,
308                     ATA_IMMEDIATE))
309         ata_prtdev(atadev, "failure to send ATAPI packet command\n");
310
311     if (request->flags & ATPR_F_DMA_USED)
312         ata_dmastart(atadev, request->data, request->bytecount,
313                      request->flags & ATPR_F_READ);
314
315     /* command interrupt device ? just return */
316     if (atadev->param->drq_type == ATAPI_DRQT_INTR)
317         return ATA_OP_CONTINUES;
318
319     /* ready to write ATAPI command */
320     timout = 5000; /* might be less for fast devices */
321     while (timout--) {
322         reason = ATA_INB(atadev->channel->r_io, ATA_IREASON);
323         atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
324         if (((reason & (ATA_I_CMD | ATA_I_IN)) |
325              (atadev->channel->status&(ATA_S_DRQ|ATA_S_BUSY)))==ATAPI_P_CMDOUT)
326             break;
327         DELAY(20);
328     }
329     if (timout <= 0) {
330         ata_prtdev(atadev, "failure to execute ATAPI packet command\n");
331         untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
332         request->error = EIO;
333         atapi_finish(request);  
334         return ATA_OP_FINISHED;
335     }
336
337     /* this seems to be needed for some (slow) devices */
338     DELAY(10);
339
340     /* send actual command */
341     ATA_OUTSW(atadev->channel->r_io, ATA_DATA, (int16_t *)request->ccb,
342               request->ccbsize / sizeof(int16_t));
343     return ATA_OP_CONTINUES;
344 }
345
346 int
347 atapi_interrupt(struct atapi_request *request)
348 {
349     struct ata_device *atadev = request->device;
350     int reason, dma_stat = 0;
351
352     reason = (ATA_INB(atadev->channel->r_io, ATA_IREASON)&(ATA_I_CMD|ATA_I_IN))|
353              (atadev->channel->status & ATA_S_DRQ);
354
355     if (reason == ATAPI_P_CMDOUT) {
356         if (!(atadev->channel->status & ATA_S_DRQ)) {
357             ata_prtdev(atadev, "command interrupt without DRQ\n");
358             untimeout((timeout_t *)atapi_timeout,
359                       request, request->timeout_handle);
360             request->error = EIO;
361             atapi_finish(request);      
362             return ATA_OP_FINISHED;
363         }
364         ATA_OUTSW(atadev->channel->r_io, ATA_DATA, (int16_t *)request->ccb,
365                   request->ccbsize / sizeof(int16_t));
366         return ATA_OP_CONTINUES;
367     }
368
369     if (request->flags & ATPR_F_DMA_USED) {
370         dma_stat = ata_dmadone(atadev);
371         if ((atadev->channel->status & (ATA_S_ERROR | ATA_S_DWF)) ||
372             dma_stat & ATA_BMSTAT_ERROR) {
373             request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
374         }
375         else {
376             request->result = 0;
377             request->donecount = request->bytecount;
378             request->bytecount = 0;
379         }
380     }
381     else {
382         int length = ATA_INB(atadev->channel->r_io, ATA_CYL_LSB) |
383                      ATA_INB(atadev->channel->r_io, ATA_CYL_MSB) << 8;
384
385         switch (reason) {
386         case ATAPI_P_WRITE:
387             if (request->flags & ATPR_F_READ) {
388                 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
389                 ata_prtdev(atadev, "%s trying to write on read buffer\n",
390                            atapi_cmd2str(atadev->cmd));
391                 break;
392             }
393             atapi_write(request, length);
394             return ATA_OP_CONTINUES;
395         
396         case ATAPI_P_READ:
397             if (!(request->flags & ATPR_F_READ)) {
398                 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
399                 ata_prtdev(atadev, "%s trying to read on write buffer\n",
400                            atapi_cmd2str(atadev->cmd));
401                 break;
402             }
403             atapi_read(request, length);
404             return ATA_OP_CONTINUES;
405
406         case ATAPI_P_DONEDRQ:
407             ata_prtdev(atadev, "%s DONEDRQ\n", atapi_cmd2str(atadev->cmd));
408             if (request->flags & ATPR_F_READ)
409                 atapi_read(request, length);
410             else
411                 atapi_write(request, length);
412             /* FALLTHROUGH */
413
414         case ATAPI_P_ABORT:
415         case ATAPI_P_DONE:
416             if (atadev->channel->status & (ATA_S_ERROR | ATA_S_DWF))
417                 request->result = ATA_INB(atadev->channel->r_io, ATA_ERROR);
418             else 
419                 if (!(request->flags & ATPR_F_INTERNAL))
420                     request->result = 0;
421             break;
422
423         default:
424             ata_prtdev(atadev, "unknown transfer phase %d\n", reason);
425         }
426     }
427     untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
428
429     /* check for error, if valid sense key, queue a request sense cmd */
430     if ((request->result & ATAPI_SK_MASK) && 
431         request->ccb[0] != ATAPI_REQUEST_SENSE) {
432         bzero(request->ccb, request->ccbsize);
433         request->ccb[0] = ATAPI_REQUEST_SENSE;
434         request->ccb[4] = sizeof(struct atapi_reqsense);
435         request->bytecount = sizeof(struct atapi_reqsense);
436         request->flags &= ATPR_F_QUIET;
437         request->flags |= ATPR_F_READ | ATPR_F_INTERNAL;
438         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
439     }
440     else {
441         if (request->result) {
442             switch ((request->result & ATAPI_SK_MASK)) {
443             case ATAPI_SK_NO_SENSE:
444                 request->error = 0;
445                 break;
446
447             case ATAPI_SK_RECOVERED_ERROR:
448                 ata_prtdev(atadev, "%s - recovered error\n",
449                            atapi_cmd2str(atadev->cmd));
450                 request->error = 0;
451                 break;
452
453             case ATAPI_SK_NOT_READY:
454                 request->error = EBUSY;
455                 break;
456
457             case ATAPI_SK_UNIT_ATTENTION:
458                 atadev->flags |= ATA_D_MEDIA_CHANGED;
459                 request->error = EIO;
460                 break;
461
462             default: 
463                 request->error = EIO;
464
465                 if (request->flags & ATPR_F_QUIET)
466                     break;
467
468                 ata_prtdev(atadev, "%s - %s asc=0x%02x ascq=0x%02x ",
469                            atapi_cmd2str(atadev->cmd), 
470                            atapi_skey2str(request->sense.sense_key), 
471                            request->sense.asc, request->sense.ascq);
472                 if (request->sense.sksv)
473                     printf("sks=0x%02x 0x%02x 0x%02x ",
474                            request->sense.sk_specific,
475                            request->sense.sk_specific1,
476                            request->sense.sk_specific2);
477                 printf("error=0x%02x\n", request->result & ATAPI_E_MASK);
478             }
479         }
480         else
481             request->error = 0;
482         atapi_finish(request);  
483     }
484     return ATA_OP_FINISHED;
485 }
486
487 void
488 atapi_reinit(struct ata_device *atadev)
489 {
490     /* reinit device parameters */
491      if (atadev->mode >= ATA_DMA)
492         ata_dmainit(atadev,
493                     (ata_pmode(atadev->param) < 0) ?
494                     (atadev->param->support_dma ? 4:0):ata_pmode(atadev->param),
495                     (ata_wmode(atadev->param) < 0) ? 
496                     (atadev->param->support_dma ? 2:0):ata_wmode(atadev->param),
497                     ata_umode(atadev->param));
498     else
499         ata_dmainit(atadev,
500                     ata_pmode(atadev->param)<0 ? 0 : ata_pmode(atadev->param),
501                     -1, -1);
502 }
503
504 int
505 atapi_test_ready(struct ata_device *atadev)
506 {
507     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
508                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
509         
510     return atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 30, NULL, NULL);
511 }
512         
513 int
514 atapi_wait_dsc(struct ata_device *atadev, int timeout)
515 {
516     int error = 0;
517     int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
518                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
519
520     timeout *= hz;
521     while (timeout > 0) {
522         error = atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 0, NULL, NULL);
523         if (error != EBUSY)
524             break;
525         tsleep((caddr_t)&error, 0, "atpwt", hz / 2);
526         timeout -= (hz / 2);
527     }
528     return error;
529 }
530
531 void
532 atapi_dump(char *label, void *data, int len)
533 {
534     u_int8_t *p = data;
535
536     printf("%s %02x", label, *p++);
537     while (--len > 0) 
538         printf ("-%02x", *p++);
539     printf("\n");
540 }
541
542 static void
543 atapi_read(struct atapi_request *request, int length)
544 {
545     int8_t **buffer = (int8_t **)&request->data;
546     int size = min(request->bytecount, length);
547     struct ata_channel *ch = request->device->channel;
548     int resid;
549
550     if (request->flags & ATPR_F_INTERNAL)
551         *buffer = (int8_t *)&request->sense;
552
553     if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
554         ATA_INSW(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer), 
555                  size / sizeof(int16_t));
556     else
557         ATA_INSL(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
558                  size / sizeof(int32_t));
559
560     if (request->bytecount < length) {
561         ata_prtdev(request->device, "read data overrun %d/%d\n",
562                    length, request->bytecount);
563         for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
564              ATA_INW(ch->r_io, ATA_DATA);
565     }
566     *buffer += size;
567     request->bytecount -= size;
568     request->donecount += size;
569 }
570
571 static void
572 atapi_write(struct atapi_request *request, int length)
573 {
574     int8_t **buffer = (int8_t **)&request->data;
575     int size = min(request->bytecount, length);
576     struct ata_channel *ch = request->device->channel;
577     int resid;
578
579     if (request->flags & ATPR_F_INTERNAL)
580         *buffer = (int8_t *)&request->sense;
581
582     if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
583         ATA_OUTSW(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
584                   size / sizeof(int16_t));
585     else
586         ATA_OUTSL(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
587                   size / sizeof(int32_t));
588
589     if (request->bytecount < length) {
590         ata_prtdev(request->device, "write data underrun %d/%d\n",
591                    length, request->bytecount);
592         for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
593             ATA_OUTW(ch->r_io, ATA_DATA, 0);
594     }
595     *buffer += size;
596     request->bytecount -= size;
597     request->donecount += size;
598 }
599
600 static void
601 atapi_finish(struct atapi_request *request)
602 {
603 #ifdef ATAPI_DEBUG
604     ata_prtdev(request->device, "finished %s%s\n",
605                request->callback ? "callback " : "",
606                atapi_cmd2str(request->ccb[0]));
607 #endif
608     if (request->callback) {
609         if (!((request->callback)(request))) {
610             free(request, M_ATAPI);
611         }
612     }
613     else 
614         wakeup((caddr_t)request);       
615 }
616
617 static void 
618 atapi_timeout(struct atapi_request *request)
619 {
620     struct ata_device *atadev = request->device;
621
622     atadev->channel->running = NULL;
623     ata_prtdev(atadev, "%s command timeout - resetting\n", 
624                atapi_cmd2str(request->ccb[0]));
625
626     if (request->flags & ATPR_F_DMA_USED) {
627         ata_dmadone(atadev);
628         if (request->retries == ATAPI_MAX_RETRIES) {
629             ata_dmainit(atadev,
630                         (ata_pmode(atadev->param) < 0) ? 0 :
631                          ata_pmode(atadev->param), -1, -1);
632             ata_prtdev(atadev, "trying fallback to PIO mode\n");
633             request->retries = 0;
634         }
635     }
636
637     /* if retries still permit, reinject this request */
638     if (request->retries++ < ATAPI_MAX_RETRIES) {
639         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
640     }
641     else {
642         /* retries all used up, return error */
643         request->error = EIO;
644         wakeup((caddr_t)request);
645     } 
646     ata_reinit(atadev->channel);
647 }
648
649 static char *
650 atapi_cmd2str(u_int8_t cmd)
651 {
652     switch (cmd) {
653     case 0x00: return ("TEST_UNIT_READY");
654     case 0x01: return ("REZERO");
655     case 0x03: return ("REQUEST_SENSE");
656     case 0x04: return ("FORMAT_UNIT");
657     case 0x08: return ("READ");
658     case 0x0a: return ("WRITE");
659     case 0x10: return ("WEOF");
660     case 0x11: return ("SPACE");
661     case 0x15: return ("MODE_SELECT");
662     case 0x19: return ("ERASE");
663     case 0x1a: return ("MODE_SENSE");
664     case 0x1b: return ("START_STOP");
665     case 0x1e: return ("PREVENT_ALLOW");
666     case 0x25: return ("READ_CAPACITY");
667     case 0x28: return ("READ_BIG");
668     case 0x2a: return ("WRITE_BIG");
669     case 0x2b: return ("LOCATE");
670     case 0x34: return ("READ_POSITION");
671     case 0x35: return ("SYNCHRONIZE_CACHE");
672     case 0x3b: return ("WRITE_BUFFER");
673     case 0x3c: return ("READ_BUFFER");
674     case 0x42: return ("READ_SUBCHANNEL");
675     case 0x43: return ("READ_TOC");
676     case 0x45: return ("PLAY_10");
677     case 0x47: return ("PLAY_MSF");
678     case 0x48: return ("PLAY_TRACK");
679     case 0x4b: return ("PAUSE");
680     case 0x51: return ("READ_DISK_INFO");
681     case 0x52: return ("READ_TRACK_INFO");
682     case 0x53: return ("RESERVE_TRACK");
683     case 0x54: return ("SEND_OPC_INFO");
684     case 0x55: return ("MODE_SELECT_BIG");
685     case 0x58: return ("REPAIR_TRACK");
686     case 0x59: return ("READ_MASTER_CUE");
687     case 0x5a: return ("MODE_SENSE_BIG");
688     case 0x5b: return ("CLOSE_TRACK/SESSION");
689     case 0x5c: return ("READ_BUFFER_CAPACITY");
690     case 0x5d: return ("SEND_CUE_SHEET");
691     case 0xa1: return ("BLANK_CMD");
692     case 0xa3: return ("SEND_KEY");
693     case 0xa4: return ("REPORT_KEY");
694     case 0xa5: return ("PLAY_12");
695     case 0xa6: return ("LOAD_UNLOAD");
696     case 0xad: return ("READ_DVD_STRUCTURE");
697     case 0xb4: return ("PLAY_CD");
698     case 0xbb: return ("SET_SPEED");
699     case 0xbd: return ("MECH_STATUS");
700     case 0xbe: return ("READ_CD");
701     case 0xff: return ("POLL_DSC");
702     default: {
703         static char buffer[20];
704         sprintf(buffer, "unknown CMD (0x%02x)", cmd);
705         return buffer;
706         }
707     }
708 }
709
710 static char *
711 atapi_skey2str(u_int8_t skey)
712 {
713     switch (skey) {
714     case 0x00: return ("NO SENSE");
715     case 0x01: return ("RECOVERED ERROR");
716     case 0x02: return ("NOT READY");
717     case 0x03: return ("MEDIUM ERROR");
718     case 0x04: return ("HARDWARE ERROR");
719     case 0x05: return ("ILLEGAL REQUEST");
720     case 0x06: return ("UNIT ATTENTION");
721     case 0x07: return ("DATA PROTECT");
722     case 0x08: return ("BLANK CHECK");
723     case 0x09: return ("VENDOR SPECIFIC");
724     case 0x0a: return ("COPY ABORTED");
725     case 0x0b: return ("ABORTED COMMAND");
726     case 0x0c: return ("EQUAL");
727     case 0x0d: return ("VOLUME OVERFLOW");
728     case 0x0e: return ("MISCOMPARE");
729     case 0x0f: return ("RESERVED");
730     default: return("UNKNOWN");
731     }
732 }