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