Merge from vendor branch OPENSSH:
[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.12 2004/07/06 19:00:06 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 %#x:%#x\n",
416                 ATA_INB(atadev->channel->r_io, ATA_IREASON), reason);
417             request->result = ATA_S_ERROR;
418         }
419     }
420     untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
421
422     /* check for error, if valid sense key, queue a request sense cmd */
423     if ((request->result & ATAPI_SK_MASK) && 
424         request->ccb[0] != ATAPI_REQUEST_SENSE) {
425         bzero(request->ccb, request->ccbsize);
426         request->ccb[0] = ATAPI_REQUEST_SENSE;
427         request->ccb[4] = sizeof(struct atapi_reqsense);
428         request->bytecount = sizeof(struct atapi_reqsense);
429         request->flags &= ATPR_F_QUIET;
430         request->flags |= ATPR_F_READ | ATPR_F_INTERNAL;
431         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
432     }
433     else {
434         if (request->result) {
435             switch ((request->result & ATAPI_SK_MASK)) {
436             case ATAPI_SK_NO_SENSE:
437                 request->error = 0;
438                 break;
439
440             case ATAPI_SK_RECOVERED_ERROR:
441                 ata_prtdev(atadev, "%s - recovered error\n",
442                            atapi_cmd2str(atadev->cmd));
443                 request->error = 0;
444                 break;
445
446             case ATAPI_SK_NOT_READY:
447                 request->error = EBUSY;
448                 break;
449
450             case ATAPI_SK_UNIT_ATTENTION:
451                 atadev->flags |= ATA_D_MEDIA_CHANGED;
452                 request->error = EIO;
453                 break;
454
455             default: 
456                 request->error = EIO;
457
458                 if (request->flags & ATPR_F_QUIET)
459                     break;
460
461                 ata_prtdev(atadev, "%s - %s asc=0x%02x ascq=0x%02x ",
462                            atapi_cmd2str(atadev->cmd), 
463                            atapi_skey2str(request->sense.sense_key), 
464                            request->sense.asc, request->sense.ascq);
465                 if (request->sense.sksv)
466                     printf("sks=0x%02x 0x%02x 0x%02x ",
467                            request->sense.sk_specific,
468                            request->sense.sk_specific1,
469                            request->sense.sk_specific2);
470                 printf("error=0x%02x\n", request->result & ATAPI_E_MASK);
471             }
472         }
473         else
474             request->error = 0;
475         atapi_finish(request);  
476     }
477     return ATA_OP_FINISHED;
478 }
479
480 void
481 atapi_reinit(struct ata_device *atadev)
482 {
483     /* reinit device parameters */
484      if (atadev->mode >= ATA_DMA)
485         ata_dmainit(atadev,
486                     (ata_pmode(atadev->param) < 0) ?
487                     (atadev->param->support_dma ? 4:0):ata_pmode(atadev->param),
488                     (ata_wmode(atadev->param) < 0) ? 
489                     (atadev->param->support_dma ? 2:0):ata_wmode(atadev->param),
490                     ata_umode(atadev->param));
491     else
492         ata_dmainit(atadev,
493                     ata_pmode(atadev->param)<0 ? 0 : ata_pmode(atadev->param),
494                     -1, -1);
495 }
496
497 int
498 atapi_test_ready(struct ata_device *atadev)
499 {
500     int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
501                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
502         
503     return atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 30, NULL, NULL);
504 }
505         
506 int
507 atapi_wait_dsc(struct ata_device *atadev, int timeout)
508 {
509     int error = 0;
510     int8_t ccb[16] = { ATAPI_POLL_DSC, 0, 0, 0, 0,
511                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
512
513     timeout *= hz;
514     while (timeout > 0) {
515         error = atapi_queue_cmd(atadev, ccb, NULL, 0, 0, 0, NULL, NULL);
516         if (error != EBUSY)
517             break;
518         tsleep((caddr_t)&error, 0, "atpwt", hz / 2);
519         timeout -= (hz / 2);
520     }
521     return error;
522 }
523
524 void
525 atapi_dump(char *label, void *data, int len)
526 {
527     u_int8_t *p = data;
528
529     printf("%s %02x", label, *p++);
530     while (--len > 0) 
531         printf ("-%02x", *p++);
532     printf("\n");
533 }
534
535 static void
536 atapi_read(struct atapi_request *request, int length)
537 {
538     int8_t **buffer = (int8_t **)&request->data;
539     int size = min(request->bytecount, length);
540     struct ata_channel *ch = request->device->channel;
541     int resid;
542
543     if (request->flags & ATPR_F_INTERNAL)
544         *buffer = (int8_t *)&request->sense;
545
546     if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
547         ATA_INSW(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer), 
548                  size / sizeof(int16_t));
549     else
550         ATA_INSL(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
551                  size / sizeof(int32_t));
552
553     if (request->bytecount < length) {
554         ata_prtdev(request->device, "read data overrun %d/%d\n",
555                    length, request->bytecount);
556         for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
557              ATA_INW(ch->r_io, ATA_DATA);
558     }
559     *buffer += size;
560     request->bytecount -= size;
561     request->donecount += size;
562 }
563
564 static void
565 atapi_write(struct atapi_request *request, int length)
566 {
567     int8_t **buffer = (int8_t **)&request->data;
568     int size = min(request->bytecount, length);
569     struct ata_channel *ch = request->device->channel;
570     int resid;
571
572     if (request->flags & ATPR_F_INTERNAL)
573         *buffer = (int8_t *)&request->sense;
574
575     if (ch->flags & ATA_USE_16BIT || (size % sizeof(int32_t)))
576         ATA_OUTSW(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
577                   size / sizeof(int16_t));
578     else
579         ATA_OUTSL(ch->r_io, ATA_DATA, (void *)((uintptr_t)*buffer),
580                   size / sizeof(int32_t));
581
582     if (request->bytecount < length) {
583         ata_prtdev(request->device, "write data underrun %d/%d\n",
584                    length, request->bytecount);
585         for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
586             ATA_OUTW(ch->r_io, ATA_DATA, 0);
587     }
588     *buffer += size;
589     request->bytecount -= size;
590     request->donecount += size;
591 }
592
593 static void
594 atapi_finish(struct atapi_request *request)
595 {
596 #ifdef ATAPI_DEBUG
597     ata_prtdev(request->device, "finished %s%s\n",
598                request->callback ? "callback " : "",
599                atapi_cmd2str(request->ccb[0]));
600 #endif
601     if (request->callback) {
602         if (!((request->callback)(request))) {
603             free(request, M_ATAPI);
604         }
605     }
606     else 
607         wakeup((caddr_t)request);       
608 }
609
610 static void 
611 atapi_timeout(struct atapi_request *request)
612 {
613     struct ata_device *atadev = request->device;
614
615     atadev->channel->running = NULL;
616     ata_prtdev(atadev, "%s command timeout - resetting\n", 
617                atapi_cmd2str(request->ccb[0]));
618
619     if (request->flags & ATPR_F_DMA_USED) {
620         ata_dmadone(atadev);
621         if (request->retries == ATAPI_MAX_RETRIES) {
622             ata_dmainit(atadev,
623                         (ata_pmode(atadev->param) < 0) ? 0 :
624                          ata_pmode(atadev->param), -1, -1);
625             ata_prtdev(atadev, "trying fallback to PIO mode\n");
626             request->retries = 0;
627         }
628     }
629
630     /* if retries still permit, reinject this request */
631     if (request->retries++ < ATAPI_MAX_RETRIES) {
632         TAILQ_INSERT_HEAD(&atadev->channel->atapi_queue, request, chain);
633     }
634     else {
635         /* retries all used up, return error */
636         request->error = EIO;
637         wakeup((caddr_t)request);
638     } 
639     ata_reinit(atadev->channel);
640 }
641
642 static char *
643 atapi_cmd2str(u_int8_t cmd)
644 {
645     switch (cmd) {
646     case 0x00: return ("TEST_UNIT_READY");
647     case 0x01: return ("REZERO");
648     case 0x03: return ("REQUEST_SENSE");
649     case 0x04: return ("FORMAT_UNIT");
650     case 0x08: return ("READ");
651     case 0x0a: return ("WRITE");
652     case 0x10: return ("WEOF");
653     case 0x11: return ("SPACE");
654     case 0x15: return ("MODE_SELECT");
655     case 0x19: return ("ERASE");
656     case 0x1a: return ("MODE_SENSE");
657     case 0x1b: return ("START_STOP");
658     case 0x1e: return ("PREVENT_ALLOW");
659     case 0x25: return ("READ_CAPACITY");
660     case 0x28: return ("READ_BIG");
661     case 0x2a: return ("WRITE_BIG");
662     case 0x2b: return ("LOCATE");
663     case 0x34: return ("READ_POSITION");
664     case 0x35: return ("SYNCHRONIZE_CACHE");
665     case 0x3b: return ("WRITE_BUFFER");
666     case 0x3c: return ("READ_BUFFER");
667     case 0x42: return ("READ_SUBCHANNEL");
668     case 0x43: return ("READ_TOC");
669     case 0x45: return ("PLAY_10");
670     case 0x47: return ("PLAY_MSF");
671     case 0x48: return ("PLAY_TRACK");
672     case 0x4b: return ("PAUSE");
673     case 0x51: return ("READ_DISK_INFO");
674     case 0x52: return ("READ_TRACK_INFO");
675     case 0x53: return ("RESERVE_TRACK");
676     case 0x54: return ("SEND_OPC_INFO");
677     case 0x55: return ("MODE_SELECT_BIG");
678     case 0x58: return ("REPAIR_TRACK");
679     case 0x59: return ("READ_MASTER_CUE");
680     case 0x5a: return ("MODE_SENSE_BIG");
681     case 0x5b: return ("CLOSE_TRACK/SESSION");
682     case 0x5c: return ("READ_BUFFER_CAPACITY");
683     case 0x5d: return ("SEND_CUE_SHEET");
684     case 0xa1: return ("BLANK_CMD");
685     case 0xa3: return ("SEND_KEY");
686     case 0xa4: return ("REPORT_KEY");
687     case 0xa5: return ("PLAY_12");
688     case 0xa6: return ("LOAD_UNLOAD");
689     case 0xad: return ("READ_DVD_STRUCTURE");
690     case 0xb4: return ("PLAY_CD");
691     case 0xbb: return ("SET_SPEED");
692     case 0xbd: return ("MECH_STATUS");
693     case 0xbe: return ("READ_CD");
694     case 0xff: return ("POLL_DSC");
695     default: {
696         static char buffer[20];
697         sprintf(buffer, "unknown CMD (0x%02x)", cmd);
698         return buffer;
699         }
700     }
701 }
702
703 static char *
704 atapi_skey2str(u_int8_t skey)
705 {
706     switch (skey) {
707     case 0x00: return ("NO SENSE");
708     case 0x01: return ("RECOVERED ERROR");
709     case 0x02: return ("NOT READY");
710     case 0x03: return ("MEDIUM ERROR");
711     case 0x04: return ("HARDWARE ERROR");
712     case 0x05: return ("ILLEGAL REQUEST");
713     case 0x06: return ("UNIT ATTENTION");
714     case 0x07: return ("DATA PROTECT");
715     case 0x08: return ("BLANK CHECK");
716     case 0x09: return ("VENDOR SPECIFIC");
717     case 0x0a: return ("COPY ABORTED");
718     case 0x0b: return ("ABORTED COMMAND");
719     case 0x0c: return ("EQUAL");
720     case 0x0d: return ("VOLUME OVERFLOW");
721     case 0x0e: return ("MISCOMPARE");
722     case 0x0f: return ("RESERVED");
723     default: return("UNKNOWN");
724     }
725 }