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