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