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