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