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