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