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