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: head/sys/dev/ata/ata-queue.c 199050 2009-11-08 14:33:19Z mav $");
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, "\2\10BUSY\7READY\6DMA_READY"
365 "\5DSC\4DRQ\3CORRECTABLE\2INDEX\1ERROR",
366 request->error, "\2\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 mtx_unlock(&ch->state_mtx);
515 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
516 if (ch->dma.unload)
517 ch->dma.unload(request);
518 ata_finish(request);
519 }
520 else {
521 mtx_unlock(&ch->state_mtx);
522 }
523 }
524
525 void
526 ata_fail_requests(device_t dev)
527 {
528 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
529 struct ata_request *request, *tmp;
530 TAILQ_HEAD(, ata_request) fail_requests;
531 TAILQ_INIT(&fail_requests);
532
533 /* grap all channel locks to avoid races */
534 mtx_lock(&ch->queue_mtx);
535 mtx_lock(&ch->state_mtx);
536
537 /* do we have any running request to care about ? */
538 if ((request = ch->running) && (!dev || request->dev == dev)) {
539 callout_stop(&request->callout);
540 ch->running = NULL;
541 request->result = ENXIO;
542 TAILQ_INSERT_TAIL(&fail_requests, request, chain);
543 }
544
545 /* fail all requests queued on this channel for device dev if !NULL */
546 TAILQ_FOREACH_SAFE(request, &ch->ata_queue, chain, tmp) {
547 if (!dev || request->dev == dev) {
548 TAILQ_REMOVE(&ch->ata_queue, request, chain);
549 request->result = ENXIO;
550 TAILQ_INSERT_TAIL(&fail_requests, request, chain);
551 }
552 }
553
554 mtx_unlock(&ch->state_mtx);
555 mtx_unlock(&ch->queue_mtx);
556
557 /* finish up all requests collected above */
558 TAILQ_FOREACH_SAFE(request, &fail_requests, chain, tmp) {
559 TAILQ_REMOVE(&fail_requests, request, chain);
560 ata_finish(request);
561 }
562 }
563
564 /*
565 * Rudely drop all requests queued to the channel of specified device.
566 * XXX: The requests are leaked, use only in fatal case.
567 */
568 void
569 ata_drop_requests(device_t dev)
570 {
571 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
572 struct ata_request *request, *tmp;
573
574 mtx_lock(&ch->queue_mtx);
575 TAILQ_FOREACH_SAFE(request, &ch->ata_queue, chain, tmp) {
576 TAILQ_REMOVE(&ch->ata_queue, request, chain);
577 request->result = ENXIO;
578 }
579 mtx_unlock(&ch->queue_mtx);
580 }
581
582 static u_int64_t
583 ata_get_lba(struct ata_request *request)
584 {
585 if (request->flags & ATA_R_ATAPI) {
586 switch (request->u.atapi.ccb[0]) {
587 case ATAPI_READ_BIG:
588 case ATAPI_WRITE_BIG:
589 case ATAPI_READ_CD:
590 return (request->u.atapi.ccb[5]) | (request->u.atapi.ccb[4]<<8) |
591 (request->u.atapi.ccb[3]<<16)|(request->u.atapi.ccb[2]<<24);
592 case ATAPI_READ:
593 case ATAPI_WRITE:
594 return (request->u.atapi.ccb[4]) | (request->u.atapi.ccb[3]<<8) |
595 (request->u.atapi.ccb[2]<<16);
596 default:
597 return 0;
598 }
599 }
600 else
601 return request->u.ata.lba;
602 }
603
604 static void
605 ata_sort_queue(struct ata_channel *ch, struct ata_request *request)
606 {
607 struct ata_request *this, *next;
608
609 this = TAILQ_FIRST(&ch->ata_queue);
610
611 /* if the queue is empty just insert */
612 if (!this) {
613 if (request->composite)
614 ch->freezepoint = request;
615 TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
616 return;
617 }
618
619 /* dont sort frozen parts of the queue */
620 if (ch->freezepoint)
621 this = ch->freezepoint;
622
623 /* if position is less than head we add after tipping point */
624 if (ata_get_lba(request) < ata_get_lba(this)) {
625 while ((next = TAILQ_NEXT(this, chain))) {
626
627 /* have we reached the tipping point */
628 if (ata_get_lba(next) < ata_get_lba(this)) {
629
630 /* sort the insert */
631 do {
632 if (ata_get_lba(request) < ata_get_lba(next))
633 break;
634 this = next;
635 } while ((next = TAILQ_NEXT(this, chain)));
636 break;
637 }
638 this = next;
639 }
640 }
641
642 /* we are after head so sort the insert before tipping point */
643 else {
644 while ((next = TAILQ_NEXT(this, chain))) {
645 if (ata_get_lba(next) < ata_get_lba(this) ||
646 ata_get_lba(request) < ata_get_lba(next))
647 break;
648 this = next;
649 }
650 }
651
652 if (request->composite)
653 ch->freezepoint = request;
654 TAILQ_INSERT_AFTER(&ch->ata_queue, this, request, chain);
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 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
728 case 0x29: return ("READ_MUL48");
729 case 0x30: return ("WRITE");
730 case 0x34: return ("WRITE48");
731 case 0x35: return ("WRITE_DMA48");
732 case 0x36: return ("WRITE_DMA_QUEUED48");
733 case 0x37: return ("SET_MAX_ADDRESS48");
734 case 0x39: return ("WRITE_MUL48");
735 case 0x70: return ("SEEK");
736 case 0xa0: return ("PACKET_CMD");
737 case 0xa1: return ("ATAPI_IDENTIFY");
738 case 0xa2: return ("SERVICE");
739 case 0xb0: return ("SMART");
740 case 0xc0: return ("CFA ERASE");
741 case 0xc4: return ("READ_MUL");
742 case 0xc5: return ("WRITE_MUL");
743 case 0xc6: return ("SET_MULTI");
744 case 0xc7: return ("READ_DMA_QUEUED");
745 case 0xc8: return ("READ_DMA");
746 case 0xca: return ("WRITE_DMA");
747 case 0xcc: return ("WRITE_DMA_QUEUED");
748 case 0xe6: return ("SLEEP");
749 case 0xe7: return ("FLUSHCACHE");
750 case 0xea: return ("FLUSHCACHE48");
751 case 0xec: return ("ATA_IDENTIFY");
752 case 0xef:
753 switch (request->u.ata.feature) {
754 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
755 case 0x02: return ("SETFEATURES ENABLE WCACHE");
756 case 0x82: return ("SETFEATURES DISABLE WCACHE");
757 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
758 case 0x55: return ("SETFEATURES DISABLE RCACHE");
759 }
760 sprintf(buffer, "SETFEATURES 0x%02x", request->u.ata.feature);
761 return buffer;
762 case 0xf5: return ("SECURITY_FREE_LOCK");
763 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
764 case 0xf9: return ("SET_MAX_ADDRESS");
765 }
766 }
767 sprintf(buffer, "unknown CMD (0x%02x)", request->u.ata.command);
768 return buffer;
769 }
770
771 static char *
772 ata_skey2str(u_int8_t skey)
773 {
774 switch (skey) {
775 case 0x00: return ("NO SENSE");
776 case 0x01: return ("RECOVERED ERROR");
777 case 0x02: return ("NOT READY");
778 case 0x03: return ("MEDIUM ERROR");
779 case 0x04: return ("HARDWARE ERROR");
780 case 0x05: return ("ILLEGAL REQUEST");
781 case 0x06: return ("UNIT ATTENTION");
782 case 0x07: return ("DATA PROTECT");
783 case 0x08: return ("BLANK CHECK");
784 case 0x09: return ("VENDOR SPECIFIC");
785 case 0x0a: return ("COPY ABORTED");
786 case 0x0b: return ("ABORTED COMMAND");
787 case 0x0c: return ("EQUAL");
788 case 0x0d: return ("VOLUME OVERFLOW");
789 case 0x0e: return ("MISCOMPARE");
790 case 0x0f: return ("RESERVED");
791 default: return("UNKNOWN");
792 }
793 }
Cache object: 6730c112b2cb703ea0117ea631ef52fd
|