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