]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-queue.c
This commit was generated by cvs2svn to compensate for changes in r155518,
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-queue.c
1 /*-
2  * Copyright (c) 1998 - 2006 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ata.h>
34 #include <sys/kernel.h>
35 #include <sys/bio.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/sema.h>
39 #include <sys/taskqueue.h>
40 #include <vm/uma.h>
41 #include <machine/bus.h>
42 #include <sys/rman.h>
43 #include <dev/ata/ata-all.h>
44 #include <ata_if.h>
45
46 /* prototypes */
47 static void ata_completed(void *, int);
48 static void ata_sort_queue(struct ata_channel *ch, struct ata_request *request);
49 static char *ata_skey2str(u_int8_t);
50
51 void
52 ata_queue_request(struct ata_request *request)
53 {
54     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
55
56     /* mark request as virgin (this might be a ATA_R_REQUEUE) */
57     request->result = request->status = request->error = 0;
58     callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
59
60     if (!request->callback && !(request->flags & ATA_R_REQUEUE))
61         sema_init(&request->done, 0, "ATA request done");
62
63     /* in ATA_STALL_QUEUE state we call HW directly (used only during reinit) */
64     if ((ch->state & ATA_STALL_QUEUE) && (request->flags & ATA_R_CONTROL)) {
65         mtx_lock(&ch->state_mtx);
66         ch->running = request;
67         if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
68             ch->running = NULL;
69             if (!request->callback) 
70                 sema_destroy(&request->done);
71             mtx_unlock(&ch->state_mtx);
72             return;
73         }
74         mtx_unlock(&ch->state_mtx);
75     }
76     /* otherwise put request on the locked queue at the specified location */
77     else  {
78         mtx_lock(&ch->queue_mtx);
79         if (request->flags & ATA_R_AT_HEAD)
80             TAILQ_INSERT_HEAD(&ch->ata_queue, request, chain);
81         else if (request->flags & ATA_R_ORDERED)
82             ata_sort_queue(ch, request);
83         else
84             TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
85         mtx_unlock(&ch->queue_mtx);
86         ATA_DEBUG_RQ(request, "queued");
87         ata_start(ch->dev);
88     }
89
90     /* if this is a requeued request callback/sleep we're done */
91     if (request->flags & ATA_R_REQUEUE)
92         return;
93
94     /* if this is not a callback wait until request is completed */
95     if (!request->callback) {
96         ATA_DEBUG_RQ(request, "wait for completition");
97         while (!dumping &&
98                sema_timedwait(&request->done, request->timeout * hz * 4)) {
99             device_printf(request->dev,
100                 "req=%p %s semaphore timeout !! DANGER Will Robinson !!\n",
101                       request, ata_cmd2str(request));
102             ata_start(ch->dev);
103         }
104         sema_destroy(&request->done);
105     }
106 }
107
108 int
109 ata_controlcmd(device_t dev, u_int8_t command, u_int16_t feature,
110                u_int64_t lba, u_int16_t count)
111 {
112     struct ata_request *request = ata_alloc_request();
113     int error = ENOMEM;
114
115     if (request) {
116         request->dev = dev;
117         request->u.ata.command = command;
118         request->u.ata.lba = lba;
119         request->u.ata.count = count;
120         request->u.ata.feature = feature;
121         request->flags = ATA_R_CONTROL;
122         request->timeout = 1;
123         request->retries = 0;
124         ata_queue_request(request);
125         error = request->result;
126         ata_free_request(request);
127     }
128     return error;
129 }
130
131 int
132 ata_atapicmd(device_t dev, u_int8_t *ccb, caddr_t data,
133              int count, int flags, int timeout)
134 {
135     struct ata_request *request = ata_alloc_request();
136     struct ata_device *atadev = device_get_softc(dev);
137     int error = ENOMEM;
138
139     if (request) {
140         request->dev = dev;
141         if ((atadev->param.config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12)
142             bcopy(ccb, request->u.atapi.ccb, 12);
143         else
144             bcopy(ccb, request->u.atapi.ccb, 16);
145         request->data = data;
146         request->bytecount = count;
147         request->transfersize = min(request->bytecount, 65534);
148         request->flags = flags | ATA_R_ATAPI;
149         request->timeout = timeout;
150         request->retries = 0;
151         ata_queue_request(request);
152         error = request->result;
153         ata_free_request(request);
154     }
155     return error;
156 }
157
158 void
159 ata_start(device_t dev)
160 {
161     struct ata_channel *ch = device_get_softc(dev);
162     struct ata_request *request;
163     struct ata_composite *cptr;
164     int dependencies = 0;
165
166     /* if we have a request on the queue try to get it running */
167     mtx_lock(&ch->queue_mtx);
168     if ((request = TAILQ_FIRST(&ch->ata_queue))) {
169
170         /* we need the locking function to get the lock for this channel */
171         if (ATA_LOCKING(dev, ATA_LF_LOCK) == ch->unit) {
172
173             /* check for composite dependencies */
174             if ((cptr = request->composite)) {
175                 mtx_lock(&cptr->lock);
176                 if ((request->flags & ATA_R_WRITE) &&
177                     (cptr->wr_depend & cptr->rd_done) != cptr->wr_depend) {
178                     dependencies = 1;
179                 }
180                 mtx_unlock(&cptr->lock);
181             }
182
183             /* check we are in the right state and has no dependencies */
184             mtx_lock(&ch->state_mtx);
185             if (ch->state == ATA_IDLE && !dependencies) {
186                 ATA_DEBUG_RQ(request, "starting");
187                 TAILQ_REMOVE(&ch->ata_queue, request, chain);
188                 ch->running = request;
189                 ch->state = ATA_ACTIVE;
190
191                 /* if we are the freezing point release it */
192                 if (ch->freezepoint == request)
193                     ch->freezepoint = NULL;
194
195                 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
196                     ch->running = NULL;
197                     ch->state = ATA_IDLE;
198                     mtx_unlock(&ch->state_mtx);
199                     mtx_unlock(&ch->queue_mtx);
200                     ATA_LOCKING(dev, ATA_LF_UNLOCK);
201                     ata_finish(request);
202                     return;
203                 }
204                 if (dumping) {
205                     mtx_unlock(&ch->state_mtx);
206                     mtx_unlock(&ch->queue_mtx);
207                     while (!ata_interrupt(ch))
208                         DELAY(10);
209                     return;
210                 }       
211             }
212             mtx_unlock(&ch->state_mtx);
213         }
214     }
215     mtx_unlock(&ch->queue_mtx);
216 }
217
218 void
219 ata_finish(struct ata_request *request)
220 {
221     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
222
223     /*
224      * if in ATA_STALL_QUEUE state or request has ATA_R_DIRECT flags set
225      * we need to call ata_complete() directly here (no taskqueue involvement)
226      */
227     if (dumping ||
228         (ch->state & ATA_STALL_QUEUE) || (request->flags & ATA_R_DIRECT)) {
229         ATA_DEBUG_RQ(request, "finish directly");
230         ata_completed(request, 0);
231     }
232     else {
233         /* put request on the proper taskqueue for completition */
234         if (request->bio && !(request->flags & (ATA_R_THREAD | ATA_R_TIMEOUT))){
235             ATA_DEBUG_RQ(request, "finish bio_taskqueue");
236             bio_taskqueue(request->bio, (bio_task_t *)ata_completed, request);
237         }
238         else {
239             TASK_INIT(&request->task, 0, ata_completed, request);
240             ATA_DEBUG_RQ(request, "finish taskqueue_swi");
241             taskqueue_enqueue(taskqueue_swi, &request->task);
242         }
243     }
244 }
245
246 static void
247 ata_completed(void *context, int dummy)
248 {
249     struct ata_request *request = (struct ata_request *)context;
250     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
251     struct ata_device *atadev = device_get_softc(request->dev);
252     struct ata_composite *composite;
253
254     ATA_DEBUG_RQ(request, "completed entered");
255
256     /* if we had a timeout, reinit channel and deal with the falldown */
257     if (request->flags & ATA_R_TIMEOUT) {
258         /*
259          * if reinit succeeds and the device doesn't get detached and
260          * there are retries left we reinject this request
261          */
262         if (!ata_reinit(ch->dev) && !request->result &&
263             (request->retries-- > 0)) {
264             if (!(request->flags & ATA_R_QUIET)) {
265                 device_printf(request->dev,
266                               "TIMEOUT - %s retrying (%d retr%s left)",
267                               ata_cmd2str(request), request->retries,
268                               request->retries == 1 ? "y" : "ies");
269                 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
270                     printf(" LBA=%llu", (unsigned long long)request->u.ata.lba);
271                 printf("\n");
272             }
273             request->flags &= ~(ATA_R_TIMEOUT | ATA_R_DEBUG);
274             request->flags |= (ATA_R_AT_HEAD | ATA_R_REQUEUE);
275             ATA_DEBUG_RQ(request, "completed reinject");
276             ata_queue_request(request);
277             return;
278         }
279
280         /* ran out of good intentions so finish with error */
281         if (!request->result) {
282             if (!(request->flags & ATA_R_QUIET)) {
283                 if (request->dev) {
284                     device_printf(request->dev, "FAILURE - %s timed out",
285                                   ata_cmd2str(request));
286                     if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
287                         printf(" LBA=%llu",
288                                (unsigned long long)request->u.ata.lba);
289                     printf("\n");
290                 }
291             }
292             request->result = EIO;
293         }
294     }
295     else {
296         /* if this is a soft ECC error warn about it */
297         /* XXX SOS we could do WARF here */
298         if ((request->status & (ATA_S_CORR | ATA_S_ERROR)) == ATA_S_CORR) {
299             device_printf(request->dev,
300                           "WARNING - %s soft error (ECC corrected)",
301                           ata_cmd2str(request));
302             if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
303                 printf(" LBA=%llu", (unsigned long long)request->u.ata.lba);
304             printf("\n");
305         }
306
307         /* if this is a UDMA CRC error we reinject if there are retries left */
308         if (request->flags & ATA_R_DMA && request->error & ATA_E_ICRC) {
309             if (request->retries-- > 0) {
310                 device_printf(request->dev,
311                               "WARNING - %s UDMA ICRC error (retrying request)",
312                               ata_cmd2str(request));
313                 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
314                     printf(" LBA=%llu", (unsigned long long)request->u.ata.lba);
315                 printf("\n");
316                 request->flags |= (ATA_R_AT_HEAD | ATA_R_REQUEUE);
317                 ata_queue_request(request);
318                 return;
319             }
320         }
321     }
322
323     switch (request->flags & ATA_R_ATAPI) {
324
325     /* ATA errors */
326     default:
327         if (!request->result && request->status & ATA_S_ERROR) {
328             if (!(request->flags & ATA_R_QUIET)) {
329                 device_printf(request->dev,
330                               "FAILURE - %s status=%b error=%b", 
331                               ata_cmd2str(request),
332                               request->status, "\20\10BUSY\7READY\6DMA_READY"
333                               "\5DSC\4DRQ\3CORRECTABLE\2INDEX\1ERROR",
334                               request->error, "\20\10ICRC\7UNCORRECTABLE"
335                               "\6MEDIA_CHANGED\5NID_NOT_FOUND"
336                               "\4MEDIA_CHANGE_REQEST"
337                               "\3ABORTED\2NO_MEDIA\1ILLEGAL_LENGTH");
338                 if ((request->flags & ATA_R_DMA) &&
339                     (request->dmastat & ATA_BMSTAT_ERROR))
340                     printf(" dma=0x%02x", request->dmastat);
341                 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
342                     printf(" LBA=%llu", (unsigned long long)request->u.ata.lba);
343                 printf("\n");
344             }
345             request->result = EIO;
346         }
347         break;
348
349     /* ATAPI errors */
350     case ATA_R_ATAPI:
351         /* skip if result already set */
352         if (request->result)
353             break;
354
355         /* if we have a sensekey -> request sense from device */
356         if (request->error & ATA_SK_MASK &&
357             request->u.atapi.ccb[0] != ATAPI_REQUEST_SENSE) {
358             static u_int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
359                                         sizeof(struct atapi_sense),
360                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
361
362             request->u.atapi.sense_key = request->error;
363             request->u.atapi.sense_cmd = request->u.atapi.ccb[0];
364             bcopy(ccb, request->u.atapi.ccb, 16);
365             request->data = (caddr_t)&request->u.atapi.sense_data;
366             request->bytecount = sizeof(struct atapi_sense);
367             request->donecount = 0;
368             request->transfersize = sizeof(struct atapi_sense);
369             request->timeout = 5;
370             request->flags &= (ATA_R_ATAPI | ATA_R_QUIET);
371             request->flags |= (ATA_R_READ | ATA_R_AT_HEAD | ATA_R_REQUEUE);
372             ATA_DEBUG_RQ(request, "autoissue request sense");
373             ata_queue_request(request);
374             return;
375         }
376
377         switch (request->u.atapi.sense_key & ATA_SK_MASK) {
378         case ATA_SK_RECOVERED_ERROR:
379             device_printf(request->dev, "WARNING - %s recovered error\n",
380                           ata_cmd2str(request));
381             /* FALLTHROUGH */
382
383         case ATA_SK_NO_SENSE:
384             request->result = 0;
385             break;
386
387         case ATA_SK_NOT_READY: 
388             request->result = EBUSY;
389             break;
390
391         case ATA_SK_UNIT_ATTENTION:
392             atadev->flags |= ATA_D_MEDIA_CHANGED;
393             request->result = EIO;
394             break;
395
396         default:
397             request->result = EIO;
398             if (request->flags & ATA_R_QUIET)
399                 break;
400
401             device_printf(request->dev,
402                           "FAILURE - %s %s asc=0x%02x ascq=0x%02x ",
403                           ata_cmd2str(request), ata_skey2str(
404                           (request->u.atapi.sense_key & ATA_SK_MASK) >> 4),
405                           request->u.atapi.sense_data.asc,
406                           request->u.atapi.sense_data.ascq);
407             if (request->u.atapi.sense_data.sksv)
408                 printf("sks=0x%02x 0x%02x 0x%02x ",
409                        request->u.atapi.sense_data.sk_specific,
410                        request->u.atapi.sense_data.sk_specific1,
411                        request->u.atapi.sense_data.sk_specific2);
412             printf("error=%b\n",
413                    (request->u.atapi.sense_key & ATA_E_MASK),
414                    "\20\4MEDIA_CHANGE_REQUEST\3ABORTED"
415                    "\2NO_MEDIA\1ILLEGAL_LENGTH");
416         }
417
418         if ((request->u.atapi.sense_key ?
419              request->u.atapi.sense_key : request->error) & ATA_E_MASK)
420             request->result = EIO;
421     }
422
423     ATA_DEBUG_RQ(request, "completed callback/wakeup");
424
425     /* if we are part of a composite operation we need to maintain progress */
426     if ((composite = request->composite)) {
427         int index = 0;
428
429         mtx_lock(&composite->lock);
430
431         /* update whats done */
432         if (request->flags & ATA_R_READ)
433             composite->rd_done |= (1 << request->this);
434         if (request->flags & ATA_R_WRITE)
435             composite->wr_done |= (1 << request->this);
436
437         /* find ready to go dependencies */
438         if (composite->wr_depend &&
439             (composite->rd_done & composite->wr_depend)==composite->wr_depend &&
440             (composite->wr_needed & (~composite->wr_done))) {
441             index = composite->wr_needed & ~composite->wr_done;
442         }
443
444         mtx_unlock(&composite->lock);
445
446         /* if we have any ready candidates kick them off */
447         if (index) {
448             int bit;
449             
450             for (bit = 0; bit < MAX_COMPOSITES; bit++) {
451                 if (index & (1 << bit))
452                     ata_start(device_get_parent(composite->request[bit]->dev));
453             }
454         }
455     }
456
457     /* get results back to the initiator for this request */
458     if (request->callback)
459         (request->callback)(request);
460     else
461         sema_post(&request->done);
462
463     ata_start(ch->dev);
464 }
465
466 void
467 ata_timeout(struct ata_request *request)
468 {
469     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
470
471     //request->flags |= ATA_R_DEBUG;
472     ATA_DEBUG_RQ(request, "timeout");
473
474     /*
475      * if we have an ATA_ACTIVE request running, we flag the request 
476      * ATA_R_TIMEOUT so ata_finish will handle it correctly
477      * also NULL out the running request so we wont loose 
478      * the race with an eventual interrupt arriving late
479      */
480     if (ch->state == ATA_ACTIVE) {
481         request->flags |= ATA_R_TIMEOUT;
482         ch->running = NULL;
483         mtx_unlock(&ch->state_mtx);
484         ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
485         ata_finish(request);
486     }
487     else {
488         mtx_unlock(&ch->state_mtx);
489     }
490 }
491
492 void
493 ata_fail_requests(device_t dev)
494 {
495     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
496     struct ata_request *request;
497
498     /* do we have any outstanding request to care about ?*/
499     mtx_lock(&ch->state_mtx);
500     if ((request = ch->running) && (!dev || request->dev == dev)) {
501         callout_stop(&request->callout);
502         ch->running = NULL;
503     }
504     else
505         request = NULL;
506     mtx_unlock(&ch->state_mtx);
507     if (request)  {
508         request->result = ENXIO;
509         ata_finish(request);
510     }
511
512     /* fail all requests queued on this channel for device dev if !NULL */
513     mtx_lock(&ch->queue_mtx);
514     while ((request = TAILQ_FIRST(&ch->ata_queue))) {
515         if (!dev || request->dev == dev) {
516             TAILQ_REMOVE(&ch->ata_queue, request, chain);
517             mtx_unlock(&ch->queue_mtx);
518             request->result = ENXIO;
519             ata_finish(request);
520             mtx_lock(&ch->queue_mtx);
521         }
522     }
523     mtx_unlock(&ch->queue_mtx);
524 }
525
526 static u_int64_t
527 ata_get_lba(struct ata_request *request)
528 {
529     if (request->flags & ATA_R_ATAPI) {
530         switch (request->u.atapi.ccb[0]) {
531         case ATAPI_READ_BIG:
532         case ATAPI_WRITE_BIG:
533         case ATAPI_READ_CD:
534             return (request->u.atapi.ccb[5]) | (request->u.atapi.ccb[4]<<8) |
535                    (request->u.atapi.ccb[3]<<16)|(request->u.atapi.ccb[2]<<24);
536         case ATAPI_READ:
537         case ATAPI_WRITE:
538             return (request->u.atapi.ccb[4]) | (request->u.atapi.ccb[3]<<8) |
539                    (request->u.atapi.ccb[2]<<16);
540         default:
541             return 0;
542         }
543     }
544     else
545         return request->u.ata.lba;
546 }
547
548 static void
549 ata_sort_queue(struct ata_channel *ch, struct ata_request *request)
550 {
551     struct ata_request *this, *next;
552
553     this = TAILQ_FIRST(&ch->ata_queue);
554
555     /* if the queue is empty just insert */
556     if (!this) {
557         if (request->composite)
558             ch->freezepoint = request;
559         TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
560         return;
561     }
562
563     /* dont sort frozen parts of the queue */
564     if (ch->freezepoint)
565         this = ch->freezepoint;
566         
567     /* if position is less than head we add after tipping point */
568     if (ata_get_lba(request) < ata_get_lba(this)) {
569         while ((next = TAILQ_NEXT(this, chain))) {
570
571             /* have we reached the tipping point */
572             if (ata_get_lba(next) < ata_get_lba(this)) {
573
574                 /* sort the insert */
575                 do {
576                     if (ata_get_lba(request) < ata_get_lba(next))
577                         break;
578                     this = next;
579                 } while ((next = TAILQ_NEXT(this, chain)));
580                 break;
581             }
582             this = next;
583         }
584     }
585
586     /* we are after head so sort the insert before tipping point */
587     else {
588         while ((next = TAILQ_NEXT(this, chain))) {
589             if (ata_get_lba(next) < ata_get_lba(this) ||
590                 ata_get_lba(request) < ata_get_lba(next))
591                 break;
592             this = next;
593         }
594     }
595
596     if (request->composite)
597         ch->freezepoint = request;
598     TAILQ_INSERT_AFTER(&ch->ata_queue, this, request, chain);
599 }
600
601 char *
602 ata_cmd2str(struct ata_request *request)
603 {
604     static char buffer[20];
605
606     if (request->flags & ATA_R_ATAPI) {
607         switch (request->u.atapi.sense_key ?
608                 request->u.atapi.sense_cmd : request->u.atapi.ccb[0]) {
609         case 0x00: return ("TEST_UNIT_READY");
610         case 0x01: return ("REZERO");
611         case 0x03: return ("REQUEST_SENSE");
612         case 0x04: return ("FORMAT");
613         case 0x08: return ("READ");
614         case 0x0a: return ("WRITE");
615         case 0x10: return ("WEOF");
616         case 0x11: return ("SPACE");
617         case 0x12: return ("INQUIRY");
618         case 0x15: return ("MODE_SELECT");
619         case 0x19: return ("ERASE");
620         case 0x1a: return ("MODE_SENSE");
621         case 0x1b: return ("START_STOP");
622         case 0x1e: return ("PREVENT_ALLOW");
623         case 0x23: return ("ATAPI_READ_FORMAT_CAPACITIES");
624         case 0x25: return ("READ_CAPACITY");
625         case 0x28: return ("READ_BIG");
626         case 0x2a: return ("WRITE_BIG");
627         case 0x2b: return ("LOCATE");
628         case 0x34: return ("READ_POSITION");
629         case 0x35: return ("SYNCHRONIZE_CACHE");
630         case 0x3b: return ("WRITE_BUFFER");
631         case 0x3c: return ("READ_BUFFER");
632         case 0x42: return ("READ_SUBCHANNEL");
633         case 0x43: return ("READ_TOC");
634         case 0x45: return ("PLAY_10");
635         case 0x47: return ("PLAY_MSF");
636         case 0x48: return ("PLAY_TRACK");
637         case 0x4b: return ("PAUSE");
638         case 0x51: return ("READ_DISK_INFO");
639         case 0x52: return ("READ_TRACK_INFO");
640         case 0x53: return ("RESERVE_TRACK");
641         case 0x54: return ("SEND_OPC_INFO");
642         case 0x55: return ("MODE_SELECT_BIG");
643         case 0x58: return ("REPAIR_TRACK");
644         case 0x59: return ("READ_MASTER_CUE");
645         case 0x5a: return ("MODE_SENSE_BIG");
646         case 0x5b: return ("CLOSE_TRACK/SESSION");
647         case 0x5c: return ("READ_BUFFER_CAPACITY");
648         case 0x5d: return ("SEND_CUE_SHEET");
649         case 0xa1: return ("BLANK_CMD");
650         case 0xa3: return ("SEND_KEY");
651         case 0xa4: return ("REPORT_KEY");
652         case 0xa5: return ("PLAY_12");
653         case 0xa6: return ("LOAD_UNLOAD");
654         case 0xad: return ("READ_DVD_STRUCTURE");
655         case 0xb4: return ("PLAY_CD");
656         case 0xbb: return ("SET_SPEED");
657         case 0xbd: return ("MECH_STATUS");
658         case 0xbe: return ("READ_CD");
659         case 0xff: return ("POLL_DSC");
660         }
661     }
662     else {
663         switch (request->u.ata.command) {
664         case 0x00: return ("NOP");
665         case 0x08: return ("DEVICE_RESET");
666         case 0x20: return ("READ");
667         case 0x24: return ("READ48");
668         case 0x25: return ("READ_DMA48");
669         case 0x26: return ("READ_DMA_QUEUED48");
670         case 0x29: return ("READ_MUL48");
671         case 0x30: return ("WRITE");
672         case 0x34: return ("WRITE48");
673         case 0x35: return ("WRITE_DMA48");
674         case 0x36: return ("WRITE_DMA_QUEUED48");
675         case 0x39: return ("WRITE_MUL48");
676         case 0x70: return ("SEEK");
677         case 0xa0: return ("PACKET_CMD");
678         case 0xa1: return ("ATAPI_IDENTIFY");
679         case 0xa2: return ("SERVICE");
680         case 0xc0: return ("CFA ERASE");
681         case 0xc4: return ("READ_MUL");
682         case 0xc5: return ("WRITE_MUL");
683         case 0xc6: return ("SET_MULTI");
684         case 0xc7: return ("READ_DMA_QUEUED");
685         case 0xc8: return ("READ_DMA");
686         case 0xca: return ("WRITE_DMA");
687         case 0xcc: return ("WRITE_DMA_QUEUED");
688         case 0xe6: return ("SLEEP");
689         case 0xe7: return ("FLUSHCACHE");
690         case 0xea: return ("FLUSHCACHE48");
691         case 0xec: return ("ATA_IDENTIFY");
692         case 0xef:
693             switch (request->u.ata.feature) {
694             case 0x03: return ("SETFEATURES SET TRANSFER MODE");
695             case 0x02: return ("SETFEATURES ENABLE WCACHE");
696             case 0x82: return ("SETFEATURES DISABLE WCACHE");
697             case 0xaa: return ("SETFEATURES ENABLE RCACHE");
698             case 0x55: return ("SETFEATURES DISABLE RCACHE");
699             }
700             sprintf(buffer, "SETFEATURES 0x%02x", request->u.ata.feature);
701             return buffer;
702         }
703     }
704     sprintf(buffer, "unknown CMD (0x%02x)", request->u.ata.command);
705     return buffer;
706 }
707
708 static char *
709 ata_skey2str(u_int8_t skey)
710 {
711     switch (skey) {
712     case 0x00: return ("NO SENSE");
713     case 0x01: return ("RECOVERED ERROR");
714     case 0x02: return ("NOT READY");
715     case 0x03: return ("MEDIUM ERROR");
716     case 0x04: return ("HARDWARE ERROR");
717     case 0x05: return ("ILLEGAL REQUEST");
718     case 0x06: return ("UNIT ATTENTION");
719     case 0x07: return ("DATA PROTECT");
720     case 0x08: return ("BLANK CHECK");
721     case 0x09: return ("VENDOR SPECIFIC");
722     case 0x0a: return ("COPY ABORTED");
723     case 0x0b: return ("ABORTED COMMAND");
724     case 0x0c: return ("EQUAL");
725     case 0x0d: return ("VOLUME OVERFLOW");
726     case 0x0e: return ("MISCOMPARE");
727     case 0x0f: return ("RESERVED");
728     default: return("UNKNOWN");
729     }
730 }