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