Merge all the FreeBSD work done since our initial import of NATA, except
[dragonfly.git] / sys / dev / disk / nata / ata-queue.c
1 /*-
2  * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ata/ata-queue.c,v 1.65 2006/07/21 19:13:05 imp Exp $
27  * $DragonFly: src/sys/dev/disk/nata/ata-queue.c,v 1.5 2007/06/01 00:31:15 dillon Exp $
28  */
29
30 #include "opt_ata.h"
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/callout.h>
35 #include <sys/nata.h>
36 #include <sys/queue.h>
37 #include <sys/spinlock2.h>
38 #include <sys/systm.h>
39 #include <sys/taskqueue.h>
40
41 #include "ata-all.h"
42 #include "ata_if.h"
43
44 /* prototypes */
45 static void ata_completed(void *, int);
46 static void ata_sort_queue(struct ata_channel *ch, struct ata_request *request);
47 static char *ata_skey2str(u_int8_t);
48
49 void
50 ata_queue_request(struct ata_request *request)
51 {
52     struct ata_channel *ch;
53
54     /* treat request as virgin (this might be an ATA_R_REQUEUE) */
55     request->result = request->status = request->error = 0;
56
57     /* check that that the device is still valid */
58     if (!(request->parent = device_get_parent(request->dev))) {
59         request->result = ENXIO;
60         if (request->callback)
61             (request->callback)(request);
62         return;
63     }
64     ch = device_get_softc(request->parent);
65     callout_init_mp(&request->callout); /* serialization done via state_mtx */
66     if (!request->callback && !(request->flags & ATA_R_REQUEUE))
67         spin_init(&request->done);
68
69     /* in ATA_STALL_QUEUE state we call HW directly */
70     if ((ch->state & ATA_STALL_QUEUE) && (request->flags & ATA_R_CONTROL)) {
71         spin_lock_wr(&ch->state_mtx);
72         ch->running = request;
73         if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
74             ch->running = NULL;
75             if (!request->callback) 
76                 spin_uninit(&request->done);
77             spin_unlock_wr(&ch->state_mtx);
78             return;
79         }
80         spin_unlock_wr(&ch->state_mtx);
81     }
82     /* otherwise put request on the locked queue at the specified location */
83     else  {
84         spin_lock_wr(&ch->queue_mtx);
85         if (request->flags & ATA_R_AT_HEAD)
86             TAILQ_INSERT_HEAD(&ch->ata_queue, request, chain);
87         else if (request->flags & ATA_R_ORDERED)
88             ata_sort_queue(ch, request);
89         else
90             TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
91         spin_unlock_wr(&ch->queue_mtx);
92         ATA_DEBUG_RQ(request, "queued");
93         ata_start(ch->dev);
94     }
95
96     /* if this is a requeued request callback/sleep we're done */
97     if (request->flags & ATA_R_REQUEUE)
98         return;
99
100     /* if this is not a callback wait until request is completed */
101     if (!request->callback) {
102         ATA_DEBUG_RQ(request, "wait for completion");
103         if (!dumping) {
104             /* interlock against wakeup */
105             spin_lock_wr(&request->done);
106             /* check if the request was completed already */
107             if (!(request->flags & ATA_R_COMPLETED))
108                 msleep(request, &request->done, 0, "ATA request completion "
109                        "wait", request->timeout * hz * 4);
110             spin_unlock_wr(&request->done);
111             /* check if the request was completed while sleeping */
112             if (!(request->flags & ATA_R_COMPLETED)) {
113                 /* apparently not */
114                 device_printf(request->dev, "WARNING - %s taskqueue timeout - "
115                               "completing request directly\n",
116                               ata_cmd2str(request));
117                 request->flags |= ATA_R_DANGER1;
118                 ata_completed(request, 0);
119             }
120         }
121         spin_uninit(&request->done);
122     }
123 }
124
125 int
126 ata_controlcmd(device_t dev, u_int8_t command, u_int16_t feature,
127                u_int64_t lba, u_int16_t count)
128 {
129     struct ata_request *request = ata_alloc_request();
130     int error = ENOMEM;
131
132     if (request) {
133         request->dev = dev;
134         request->u.ata.command = command;
135         request->u.ata.lba = lba;
136         request->u.ata.count = count;
137         request->u.ata.feature = feature;
138         request->flags = ATA_R_CONTROL;
139         request->timeout = 1;
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     spin_lock_wr(&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                 spin_lock_wr(&cptr->lock);
193                 if ((request->flags & ATA_R_WRITE) &&
194                     (cptr->wr_depend & cptr->rd_done) != cptr->wr_depend) {
195                     dependencies = 1;
196                 }
197                 spin_unlock_wr(&cptr->lock);
198             }
199
200             /* check we are in the right state and has no dependencies */
201             spin_lock_wr(&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                     spin_unlock_wr(&ch->state_mtx);
216                     spin_unlock_wr(&ch->queue_mtx);
217                     ATA_LOCKING(dev, ATA_LF_UNLOCK);
218                     ata_finish(request);
219                     return;
220                 }
221                 if (dumping) {
222                     spin_unlock_wr(&ch->state_mtx);
223                     spin_unlock_wr(&ch->queue_mtx);
224                     while (!ata_interrupt(ch))
225                         DELAY(10);
226                     return;
227                 }       
228             }
229             spin_unlock_wr(&ch->state_mtx);
230         }
231     }
232     spin_unlock_wr(&ch->queue_mtx);
233 }
234
235 void
236 ata_finish(struct ata_request *request)
237 {
238     struct ata_channel *ch = device_get_softc(request->parent);
239
240     /*
241      * if in ATA_STALL_QUEUE state or request has ATA_R_DIRECT flags set
242      * we need to call ata_complete() directly here (no taskqueue involvement)
243      */
244     if (dumping ||
245         (ch->state & ATA_STALL_QUEUE) || (request->flags & ATA_R_DIRECT)) {
246         ATA_DEBUG_RQ(request, "finish directly");
247         ata_completed(request, 0);
248     }
249     else {
250         /* put request on the proper taskqueue for completion */
251         /* XXX FreeBSD has some sort of bio_taskqueue code here */
252         TASK_INIT(&request->task, 0, ata_completed, request);
253         ATA_DEBUG_RQ(request, "finish taskqueue_swi");
254         taskqueue_enqueue(taskqueue_swi, &request->task);
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                     kprintf(" LBA=%ju", request->u.ata.lba);
296                 kprintf("\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                         kprintf(" LBA=%ju", request->u.ata.lba);
313                     kprintf("\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                 kprintf(" LBA=%ju", request->u.ata.lba);
328             kprintf("\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                     kprintf(" LBA=%ju", request->u.ata.lba);
339                 kprintf("\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                     kprintf(" dma=0x%02x", request->dmastat);
365                 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
366                     kprintf(" LBA=%ju", request->u.ata.lba);
367                 kprintf("\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);
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                 kprintf("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                 kprintf("\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         spin_lock_wr(&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         spin_unlock_wr(&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         spin_lock_wr(&request->done);
484         request->flags |= ATA_R_COMPLETED;
485         spin_unlock_wr(&request->done);
486         wakeup_one(request);
487     }
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     /* acquire state_mtx, softclock_handler() doesn't do this for us */
500     spin_lock_wr(&ch->state_mtx);
501
502     /*request->flags |= ATA_R_DEBUG;*/
503     ATA_DEBUG_RQ(request, "timeout");
504
505     /*
506      * if we have an ATA_ACTIVE request running, we flag the request 
507      * ATA_R_TIMEOUT so ata_finish will handle it correctly
508      * also NULL out the running request so we wont loose 
509      * the race with an eventual interrupt arriving late
510      */
511     if (ch->state == ATA_ACTIVE) {
512         request->flags |= ATA_R_TIMEOUT;
513         spin_unlock_wr(&ch->state_mtx);
514         ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
515         ata_finish(request);
516     }
517     else {
518         spin_unlock_wr(&ch->state_mtx);
519     }
520 }
521
522 void
523 ata_fail_requests(device_t dev)
524 {
525     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
526     struct ata_request *request, *tmp;
527     TAILQ_HEAD(, ata_request) fail_requests;
528     TAILQ_INIT(&fail_requests);
529
530     /* grap all channel locks to avoid races */
531     spin_lock_wr(&ch->queue_mtx);
532     spin_lock_wr(&ch->state_mtx);
533
534     /* do we have any running request to care about ? */
535     if ((request = ch->running) && (!dev || request->dev == dev)) {
536         callout_stop(&request->callout);
537         ch->running = NULL;
538         request->result = ENXIO;
539         TAILQ_INSERT_TAIL(&fail_requests, request, chain);
540     }
541
542     /* fail all requests queued on this channel for device dev if !NULL */
543     TAILQ_FOREACH_MUTABLE(request, &ch->ata_queue, chain, tmp) {
544         if (!dev || request->dev == dev) {
545             TAILQ_REMOVE(&ch->ata_queue, request, chain);
546             request->result = ENXIO;
547             TAILQ_INSERT_TAIL(&fail_requests, request, chain);
548         }
549     }
550
551     spin_unlock_wr(&ch->state_mtx);
552     spin_unlock_wr(&ch->queue_mtx);
553    
554     /* finish up all requests collected above */
555     TAILQ_FOREACH_MUTABLE(request, &fail_requests, chain, tmp) {
556         TAILQ_REMOVE(&fail_requests, request, chain);
557         ata_finish(request);
558     }
559 }
560
561 static u_int64_t
562 ata_get_lba(struct ata_request *request)
563 {
564     if (request->flags & ATA_R_ATAPI) {
565         switch (request->u.atapi.ccb[0]) {
566         case ATAPI_READ_BIG:
567         case ATAPI_WRITE_BIG:
568         case ATAPI_READ_CD:
569             return (request->u.atapi.ccb[5]) | (request->u.atapi.ccb[4]<<8) |
570                    (request->u.atapi.ccb[3]<<16)|(request->u.atapi.ccb[2]<<24);
571         case ATAPI_READ:
572         case ATAPI_WRITE:
573             return (request->u.atapi.ccb[4]) | (request->u.atapi.ccb[3]<<8) |
574                    (request->u.atapi.ccb[2]<<16);
575         default:
576             return 0;
577         }
578     }
579     else
580         return request->u.ata.lba;
581 }
582
583 static void
584 ata_sort_queue(struct ata_channel *ch, struct ata_request *request)
585 {
586     struct ata_request *this, *next;
587
588     this = TAILQ_FIRST(&ch->ata_queue);
589
590     /* if the queue is empty just insert */
591     if (!this) {
592         if (request->composite)
593             ch->freezepoint = request;
594         TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
595         return;
596     }
597
598     /* dont sort frozen parts of the queue */
599     if (ch->freezepoint)
600         this = ch->freezepoint;
601         
602     /* if position is less than head we add after tipping point */
603     if (ata_get_lba(request) < ata_get_lba(this)) {
604         while ((next = TAILQ_NEXT(this, chain))) {
605
606             /* have we reached the tipping point */
607             if (ata_get_lba(next) < ata_get_lba(this)) {
608
609                 /* sort the insert */
610                 do {
611                     if (ata_get_lba(request) < ata_get_lba(next))
612                         break;
613                     this = next;
614                 } while ((next = TAILQ_NEXT(this, chain)));
615                 break;
616             }
617             this = next;
618         }
619     }
620
621     /* we are after head so sort the insert before tipping point */
622     else {
623         while ((next = TAILQ_NEXT(this, chain))) {
624             if (ata_get_lba(next) < ata_get_lba(this) ||
625                 ata_get_lba(request) < ata_get_lba(next))
626                 break;
627             this = next;
628         }
629     }
630
631     if (request->composite)
632         ch->freezepoint = request;
633     TAILQ_INSERT_AFTER(&ch->ata_queue, this, request, chain);
634 }
635
636 char *
637 ata_cmd2str(struct ata_request *request)
638 {
639     static char buffer[20];
640
641     if (request->flags & ATA_R_ATAPI) {
642         switch (request->u.atapi.sense.key ?
643                 request->u.atapi.saved_cmd : request->u.atapi.ccb[0]) {
644         case 0x00: return ("TEST_UNIT_READY");
645         case 0x01: return ("REZERO");
646         case 0x03: return ("REQUEST_SENSE");
647         case 0x04: return ("FORMAT");
648         case 0x08: return ("READ");
649         case 0x0a: return ("WRITE");
650         case 0x10: return ("WEOF");
651         case 0x11: return ("SPACE");
652         case 0x12: return ("INQUIRY");
653         case 0x15: return ("MODE_SELECT");
654         case 0x19: return ("ERASE");
655         case 0x1a: return ("MODE_SENSE");
656         case 0x1b: return ("START_STOP");
657         case 0x1e: return ("PREVENT_ALLOW");
658         case 0x23: return ("ATAPI_READ_FORMAT_CAPACITIES");
659         case 0x25: return ("READ_CAPACITY");
660         case 0x28: return ("READ_BIG");
661         case 0x2a: return ("WRITE_BIG");
662         case 0x2b: return ("LOCATE");
663         case 0x34: return ("READ_POSITION");
664         case 0x35: return ("SYNCHRONIZE_CACHE");
665         case 0x3b: return ("WRITE_BUFFER");
666         case 0x3c: return ("READ_BUFFER");
667         case 0x42: return ("READ_SUBCHANNEL");
668         case 0x43: return ("READ_TOC");
669         case 0x45: return ("PLAY_10");
670         case 0x47: return ("PLAY_MSF");
671         case 0x48: return ("PLAY_TRACK");
672         case 0x4b: return ("PAUSE");
673         case 0x51: return ("READ_DISK_INFO");
674         case 0x52: return ("READ_TRACK_INFO");
675         case 0x53: return ("RESERVE_TRACK");
676         case 0x54: return ("SEND_OPC_INFO");
677         case 0x55: return ("MODE_SELECT_BIG");
678         case 0x58: return ("REPAIR_TRACK");
679         case 0x59: return ("READ_MASTER_CUE");
680         case 0x5a: return ("MODE_SENSE_BIG");
681         case 0x5b: return ("CLOSE_TRACK/SESSION");
682         case 0x5c: return ("READ_BUFFER_CAPACITY");
683         case 0x5d: return ("SEND_CUE_SHEET");
684         case 0x96: return ("SERVICE_ACTION_IN");
685         case 0xa1: return ("BLANK_CMD");
686         case 0xa3: return ("SEND_KEY");
687         case 0xa4: return ("REPORT_KEY");
688         case 0xa5: return ("PLAY_12");
689         case 0xa6: return ("LOAD_UNLOAD");
690         case 0xad: return ("READ_DVD_STRUCTURE");
691         case 0xb4: return ("PLAY_CD");
692         case 0xbb: return ("SET_SPEED");
693         case 0xbd: return ("MECH_STATUS");
694         case 0xbe: return ("READ_CD");
695         case 0xff: return ("POLL_DSC");
696         }
697     }
698     else {
699         switch (request->u.ata.command) {
700         case 0x00: return ("NOP");
701         case 0x08: return ("DEVICE_RESET");
702         case 0x20: return ("READ");
703         case 0x24: return ("READ48");
704         case 0x25: return ("READ_DMA48");
705         case 0x26: return ("READ_DMA_QUEUED48");
706         case 0x29: return ("READ_MUL48");
707         case 0x30: return ("WRITE");
708         case 0x34: return ("WRITE48");
709         case 0x35: return ("WRITE_DMA48");
710         case 0x36: return ("WRITE_DMA_QUEUED48");
711         case 0x39: return ("WRITE_MUL48");
712         case 0x70: return ("SEEK");
713         case 0xa0: return ("PACKET_CMD");
714         case 0xa1: return ("ATAPI_IDENTIFY");
715         case 0xa2: return ("SERVICE");
716         case 0xb0: return ("SMART");
717         case 0xc0: return ("CFA ERASE");
718         case 0xc4: return ("READ_MUL");
719         case 0xc5: return ("WRITE_MUL");
720         case 0xc6: return ("SET_MULTI");
721         case 0xc7: return ("READ_DMA_QUEUED");
722         case 0xc8: return ("READ_DMA");
723         case 0xca: return ("WRITE_DMA");
724         case 0xcc: return ("WRITE_DMA_QUEUED");
725         case 0xe6: return ("SLEEP");
726         case 0xe7: return ("FLUSHCACHE");
727         case 0xea: return ("FLUSHCACHE48");
728         case 0xec: return ("ATA_IDENTIFY");
729         case 0xef:
730             switch (request->u.ata.feature) {
731             case 0x03: return ("SETFEATURES SET TRANSFER MODE");
732             case 0x02: return ("SETFEATURES ENABLE WCACHE");
733             case 0x82: return ("SETFEATURES DISABLE WCACHE");
734             case 0xaa: return ("SETFEATURES ENABLE RCACHE");
735             case 0x55: return ("SETFEATURES DISABLE RCACHE");
736             }
737             ksprintf(buffer, "SETFEATURES 0x%02x", request->u.ata.feature);
738             return buffer;
739         }
740     }
741     ksprintf(buffer, "unknown CMD (0x%02x)", request->u.ata.command);
742     return buffer;
743 }
744
745 static char *
746 ata_skey2str(u_int8_t skey)
747 {
748     switch (skey) {
749     case 0x00: return ("NO SENSE");
750     case 0x01: return ("RECOVERED ERROR");
751     case 0x02: return ("NOT READY");
752     case 0x03: return ("MEDIUM ERROR");
753     case 0x04: return ("HARDWARE ERROR");
754     case 0x05: return ("ILLEGAL REQUEST");
755     case 0x06: return ("UNIT ATTENTION");
756     case 0x07: return ("DATA PROTECT");
757     case 0x08: return ("BLANK CHECK");
758     case 0x09: return ("VENDOR SPECIFIC");
759     case 0x0a: return ("COPY ABORTED");
760     case 0x0b: return ("ABORTED COMMAND");
761     case 0x0c: return ("EQUAL");
762     case 0x0d: return ("VOLUME OVERFLOW");
763     case 0x0e: return ("MISCOMPARE");
764     case 0x0f: return ("RESERVED");
765     default: return("UNKNOWN");
766     }
767 }