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: stable/9/sys/dev/ata/ata-queue.c 251224 2013-06-01 13:10:24Z marius $");
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 #ifndef ATA_CAM
47 /* prototypes */
48 static void ata_completed(void *, int);
49 static void ata_sort_queue(struct ata_channel *ch, struct ata_request *request);
50 static const char *ata_skey2str(u_int8_t);
51 #endif
52 
53 #ifndef ATA_CAM
54 void
ata_queue_request(struct ata_request * request)55 ata_queue_request(struct ata_request *request)
56 {
57     struct ata_channel *ch;
58     struct ata_device *atadev = device_get_softc(request->dev);
59 
60     /* treat request as virgin (this might be an ATA_R_REQUEUE) */
61     request->result = request->status = request->error = 0;
62 
63     /* Prepare paramers required by low-level code. */
64     request->unit = atadev->unit;
65     if (!(request->parent = device_get_parent(request->dev))) {
66 	request->result = ENXIO;
67 	if (request->callback)
68 	    (request->callback)(request);
69 	return;
70     }
71     if ((atadev->param.config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16)
72 	request->flags |= ATA_R_ATAPI16;
73     if ((atadev->param.config & ATA_DRQ_MASK) == ATA_DRQ_INTR)
74 	request->flags |= ATA_R_ATAPI_INTR;
75     if ((request->flags & ATA_R_ATAPI) == 0)
76 	ata_modify_if_48bit(request);
77     ch = device_get_softc(request->parent);
78     callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
79     if (!request->callback && !(request->flags & ATA_R_REQUEUE))
80 	sema_init(&request->done, 0, "ATA request done");
81 
82     /* in ATA_STALL_QUEUE state we call HW directly */
83     if ((ch->state & ATA_STALL_QUEUE) && (request->flags & ATA_R_CONTROL)) {
84 	mtx_lock(&ch->state_mtx);
85 	ch->running = request;
86 	if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
87 	    ch->running = NULL;
88 	    if (!request->callback)
89 		sema_destroy(&request->done);
90 	    mtx_unlock(&ch->state_mtx);
91 	    return;
92 	}
93 	mtx_unlock(&ch->state_mtx);
94     }
95     /* otherwise put request on the locked queue at the specified location */
96     else  {
97 	mtx_lock(&ch->queue_mtx);
98 	if (request->flags & ATA_R_AT_HEAD)
99 	    TAILQ_INSERT_HEAD(&ch->ata_queue, request, chain);
100 	else if (request->flags & ATA_R_ORDERED)
101 	    ata_sort_queue(ch, request);
102 	else
103 	    TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
104 	mtx_unlock(&ch->queue_mtx);
105 	ATA_DEBUG_RQ(request, "queued");
106 	ata_start(ch->dev);
107     }
108 
109     /* if this is a requeued request callback/sleep we're done */
110     if (request->flags & ATA_R_REQUEUE)
111 	return;
112 
113     /* if this is not a callback wait until request is completed */
114     if (!request->callback) {
115 	ATA_DEBUG_RQ(request, "wait for completion");
116 	if (!dumping &&
117 	    sema_timedwait(&request->done, request->timeout * hz * 4)) {
118 	    callout_drain(&request->callout);
119 	    device_printf(request->dev,
120 			  "WARNING - %s taskqueue timeout "
121 			  "- completing request directly\n",
122 			  ata_cmd2str(request));
123 	    request->flags |= ATA_R_DANGER1;
124 	    ata_completed(request, 0);
125 	}
126 	sema_destroy(&request->done);
127     }
128 }
129 #endif
130 
131 #ifndef ATA_CAM
132 int
ata_controlcmd(device_t dev,u_int8_t command,u_int16_t feature,u_int64_t lba,u_int16_t count)133 ata_controlcmd(device_t dev, u_int8_t command, u_int16_t feature,
134 	       u_int64_t lba, u_int16_t count)
135 {
136     struct ata_device *atadev = device_get_softc(dev);
137     struct ata_request *request = ata_alloc_request();
138     int error = ENOMEM;
139 
140     if (request) {
141 	request->dev = dev;
142 	request->u.ata.command = command;
143 	request->u.ata.lba = lba;
144 	request->u.ata.count = count;
145 	request->u.ata.feature = feature;
146 	request->flags = ATA_R_CONTROL;
147 	if (atadev->spindown_state) {
148 	    device_printf(dev, "request while spun down, starting.\n");
149 	    atadev->spindown_state = 0;
150 	    request->timeout = MAX(ATA_REQUEST_TIMEOUT, 31);
151 	} else {
152 	    request->timeout = ATA_REQUEST_TIMEOUT;
153 	}
154 	request->retries = 0;
155 	ata_queue_request(request);
156 	error = request->result;
157 	ata_free_request(request);
158     }
159     return error;
160 }
161 #endif
162 
163 #ifndef ATA_CAM
164 int
ata_atapicmd(device_t dev,u_int8_t * ccb,caddr_t data,int count,int flags,int timeout)165 ata_atapicmd(device_t dev, u_int8_t *ccb, caddr_t data,
166 	     int count, int flags, int timeout)
167 {
168     struct ata_request *request = ata_alloc_request();
169     int error = ENOMEM;
170 
171     if (request) {
172 	request->dev = dev;
173 	bcopy(ccb, request->u.atapi.ccb, 16);
174 	request->data = data;
175 	request->bytecount = count;
176 	request->transfersize = min(request->bytecount, 65534);
177 	request->flags = flags | ATA_R_ATAPI;
178 	request->timeout = timeout;
179 	request->retries = 0;
180 	ata_queue_request(request);
181 	error = request->result;
182 	ata_free_request(request);
183     }
184     return error;
185 }
186 #endif
187 
188 #ifndef ATA_CAM
189 void
ata_start(device_t dev)190 ata_start(device_t dev)
191 {
192     struct ata_channel *ch = device_get_softc(dev);
193     struct ata_request *request;
194     struct ata_composite *cptr;
195     int dependencies = 0;
196 
197     /* if we have a request on the queue try to get it running */
198     mtx_lock(&ch->queue_mtx);
199     if ((request = TAILQ_FIRST(&ch->ata_queue))) {
200 
201 	/* we need the locking function to get the lock for this channel */
202 	if (ATA_LOCKING(dev, ATA_LF_LOCK) == ch->unit) {
203 
204 	    /* check for composite dependencies */
205 	    if ((cptr = request->composite)) {
206 		mtx_lock(&cptr->lock);
207 		if ((request->flags & ATA_R_WRITE) &&
208 		    (cptr->wr_depend & cptr->rd_done) != cptr->wr_depend) {
209 		    dependencies = 1;
210 		}
211 		mtx_unlock(&cptr->lock);
212 	    }
213 
214 	    /* check we are in the right state and has no dependencies */
215 	    mtx_lock(&ch->state_mtx);
216 	    if (ch->state == ATA_IDLE && !dependencies) {
217 		ATA_DEBUG_RQ(request, "starting");
218 		TAILQ_REMOVE(&ch->ata_queue, request, chain);
219 		ch->running = request;
220 		ch->state = ATA_ACTIVE;
221 
222 		/* if we are the freezing point release it */
223 		if (ch->freezepoint == request)
224 		    ch->freezepoint = NULL;
225 
226 		if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
227 		    ch->running = NULL;
228 		    ch->state = ATA_IDLE;
229 		    mtx_unlock(&ch->state_mtx);
230 		    mtx_unlock(&ch->queue_mtx);
231 		    ATA_LOCKING(dev, ATA_LF_UNLOCK);
232 		    ata_finish(request);
233 		    return;
234 		}
235 	    }
236 	    mtx_unlock(&ch->state_mtx);
237 	}
238     }
239     mtx_unlock(&ch->queue_mtx);
240     if (dumping) {
241 	while (ch->running) {
242 	    ata_interrupt(ch);
243 	    DELAY(10);
244 	}
245     }
246 }
247 #endif
248 
249 #ifndef ATA_CAM
250 void
ata_finish(struct ata_request * request)251 ata_finish(struct ata_request *request)
252 {
253     struct ata_channel *ch = device_get_softc(request->parent);
254 
255     /*
256      * if in ATA_STALL_QUEUE state or request has ATA_R_DIRECT flags set
257      * we need to call ata_complete() directly here (no taskqueue involvement)
258      */
259     if (dumping ||
260 	(ch->state & ATA_STALL_QUEUE) || (request->flags & ATA_R_DIRECT)) {
261 	ATA_DEBUG_RQ(request, "finish directly");
262 	ata_completed(request, 0);
263     }
264     else {
265 	/* put request on the proper taskqueue for completion */
266 	if (request->bio && !(request->flags & (ATA_R_THREAD | ATA_R_TIMEOUT))){
267 	    ATA_DEBUG_RQ(request, "finish bio_taskqueue");
268 	    bio_taskqueue(request->bio, (bio_task_t *)ata_completed, request);
269 	}
270 	else {
271 	    TASK_INIT(&request->task, 0, ata_completed, request);
272 	    ATA_DEBUG_RQ(request, "finish taskqueue_swi");
273 	    taskqueue_enqueue(taskqueue_swi, &request->task);
274 	}
275     }
276 }
277 #endif
278 
279 #ifndef ATA_CAM
280 static void
ata_completed(void * context,int dummy)281 ata_completed(void *context, int dummy)
282 {
283     struct ata_request *request = (struct ata_request *)context;
284     struct ata_channel *ch = device_get_softc(request->parent);
285     struct ata_device *atadev = device_get_softc(request->dev);
286     struct ata_composite *composite;
287 
288     if (request->flags & ATA_R_DANGER2) {
289 	device_printf(request->dev,
290 		      "WARNING - %s freeing taskqueue zombie request\n",
291 		      ata_cmd2str(request));
292 	request->flags &= ~(ATA_R_DANGER1 | ATA_R_DANGER2);
293 	ata_free_request(request);
294 	return;
295     }
296     if (request->flags & ATA_R_DANGER1)
297 	request->flags |= ATA_R_DANGER2;
298 
299     ATA_DEBUG_RQ(request, "completed entered");
300 
301     /* if we had a timeout, reinit channel and deal with the falldown */
302     if (request->flags & ATA_R_TIMEOUT) {
303 	/*
304 	 * if the channel is still present and
305 	 * reinit succeeds and
306 	 * the device doesn't get detached and
307 	 * there are retries left we reinject this request
308 	 */
309 	if (ch && !ata_reinit(ch->dev) && !request->result &&
310 	    (request->retries-- > 0)) {
311 	    if (!(request->flags & ATA_R_QUIET)) {
312 		device_printf(request->dev,
313 			      "TIMEOUT - %s retrying (%d retr%s left)",
314 			      ata_cmd2str(request), request->retries,
315 			      request->retries == 1 ? "y" : "ies");
316 		if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
317 		    printf(" LBA=%ju", request->u.ata.lba);
318 		printf("\n");
319 	    }
320 	    request->flags &= ~(ATA_R_TIMEOUT | ATA_R_DEBUG);
321 	    request->flags |= (ATA_R_AT_HEAD | ATA_R_REQUEUE);
322 	    ATA_DEBUG_RQ(request, "completed reinject");
323 	    ata_queue_request(request);
324 	    return;
325 	}
326 
327 	/* ran out of good intentions so finish with error */
328 	if (!request->result) {
329 	    if (!(request->flags & ATA_R_QUIET)) {
330 		if (request->dev) {
331 		    device_printf(request->dev, "FAILURE - %s timed out",
332 				  ata_cmd2str(request));
333 		    if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
334 			printf(" LBA=%ju", request->u.ata.lba);
335 		    printf("\n");
336 		}
337 	    }
338 	    request->result = EIO;
339 	}
340     }
341     else if (!(request->flags & ATA_R_ATAPI) ){
342 	/* if this is a soft ECC error warn about it */
343 	/* XXX SOS we could do WARF here */
344 	if ((request->status & (ATA_S_CORR | ATA_S_ERROR)) == ATA_S_CORR) {
345 	    device_printf(request->dev,
346 			  "WARNING - %s soft error (ECC corrected)",
347 			  ata_cmd2str(request));
348 	    if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
349 		printf(" LBA=%ju", request->u.ata.lba);
350 	    printf("\n");
351 	}
352 
353 	/* if this is a UDMA CRC error we reinject if there are retries left */
354 	if (request->flags & ATA_R_DMA && request->error & ATA_E_ICRC) {
355 	    if (request->retries-- > 0) {
356 		device_printf(request->dev,
357 			      "WARNING - %s UDMA ICRC error (retrying request)",
358 			      ata_cmd2str(request));
359 		if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
360 		    printf(" LBA=%ju", request->u.ata.lba);
361 		printf("\n");
362 		request->flags |= (ATA_R_AT_HEAD | ATA_R_REQUEUE);
363 		ata_queue_request(request);
364 		return;
365 	    }
366 	}
367     }
368 
369     switch (request->flags & ATA_R_ATAPI) {
370 
371     /* ATA errors */
372     default:
373 	if (!request->result && request->status & ATA_S_ERROR) {
374 	    if (!(request->flags & ATA_R_QUIET)) {
375 		device_printf(request->dev,
376 			      "FAILURE - %s status=%b error=%b",
377 			      ata_cmd2str(request),
378 			      request->status, "\20\10BUSY\7READY\6DMA_READY"
379 			      "\5DSC\4DRQ\3CORRECTABLE\2INDEX\1ERROR",
380 			      request->error, "\20\10ICRC\7UNCORRECTABLE"
381 			      "\6MEDIA_CHANGED\5NID_NOT_FOUND"
382 			      "\4MEDIA_CHANGE_REQEST"
383 			      "\3ABORTED\2NO_MEDIA\1ILLEGAL_LENGTH");
384 		if ((request->flags & ATA_R_DMA) && request->dma &&
385 		    (request->dma->status & ATA_BMSTAT_ERROR))
386 		    printf(" dma=0x%02x", request->dma->status);
387 		if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
388 		    printf(" LBA=%ju", request->u.ata.lba);
389 		printf("\n");
390 	    }
391 	    request->result = EIO;
392 	}
393 	break;
394 
395     /* ATAPI errors */
396     case ATA_R_ATAPI:
397 	/* skip if result already set */
398 	if (request->result)
399 	    break;
400 
401 	/* if we have a sensekey -> request sense from device */
402 	if ((request->error & ATA_E_ATAPI_SENSE_MASK) &&
403 	    (request->u.atapi.ccb[0] != ATAPI_REQUEST_SENSE)) {
404 	    static u_int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
405 					sizeof(struct atapi_sense),
406 					0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
407 
408 	    request->u.atapi.saved_cmd = request->u.atapi.ccb[0];
409 	    bcopy(ccb, request->u.atapi.ccb, 16);
410 	    request->data = (caddr_t)&request->u.atapi.sense;
411 	    request->bytecount = sizeof(struct atapi_sense);
412 	    request->donecount = 0;
413 	    request->transfersize = sizeof(struct atapi_sense);
414 	    request->timeout = ATA_REQUEST_TIMEOUT;
415 	    request->flags &= (ATA_R_ATAPI | ATA_R_QUIET | ATA_R_DEBUG);
416 	    request->flags |= (ATA_R_READ | ATA_R_AT_HEAD | ATA_R_REQUEUE);
417 	    ATA_DEBUG_RQ(request, "autoissue request sense");
418 	    ata_queue_request(request);
419 	    return;
420 	}
421 
422 	switch (request->u.atapi.sense.key & ATA_SENSE_KEY_MASK) {
423 	case ATA_SENSE_RECOVERED_ERROR:
424 	    device_printf(request->dev, "WARNING - %s recovered error\n",
425 			  ata_cmd2str(request));
426 	    /* FALLTHROUGH */
427 
428 	case ATA_SENSE_NO_SENSE:
429 	    request->result = 0;
430 	    break;
431 
432 	case ATA_SENSE_NOT_READY:
433 	    request->result = EBUSY;
434 	    break;
435 
436 	case ATA_SENSE_UNIT_ATTENTION:
437 	    atadev->flags |= ATA_D_MEDIA_CHANGED;
438 	    request->result = EIO;
439 	    break;
440 
441 	default:
442 	    request->result = EIO;
443 	    if (request->flags & ATA_R_QUIET)
444 		break;
445 
446 	    device_printf(request->dev,
447 			  "FAILURE - %s %s asc=0x%02x ascq=0x%02x ",
448 			  ata_cmd2str(request), ata_skey2str(
449 			  (request->u.atapi.sense.key & ATA_SENSE_KEY_MASK)),
450 			  request->u.atapi.sense.asc,
451 			  request->u.atapi.sense.ascq);
452 	    if (request->u.atapi.sense.specific & ATA_SENSE_SPEC_VALID)
453 		printf("sks=0x%02x 0x%02x 0x%02x\n",
454 		       request->u.atapi.sense.specific & ATA_SENSE_SPEC_MASK,
455 		       request->u.atapi.sense.specific1,
456 		       request->u.atapi.sense.specific2);
457 	    else
458 		printf("\n");
459 	}
460 
461 	if (!request->result &&
462 	     (request->u.atapi.sense.key & ATA_SENSE_KEY_MASK ||
463 	     request->error))
464 	    request->result = EIO;
465     }
466 
467     ATA_DEBUG_RQ(request, "completed callback/wakeup");
468 
469     /* if we are part of a composite operation we need to maintain progress */
470     if ((composite = request->composite)) {
471 	int index = 0;
472 
473 	mtx_lock(&composite->lock);
474 
475 	/* update whats done */
476 	if (request->flags & ATA_R_READ)
477 	    composite->rd_done |= (1 << request->this);
478 	if (request->flags & ATA_R_WRITE)
479 	    composite->wr_done |= (1 << request->this);
480 
481 	/* find ready to go dependencies */
482 	if (composite->wr_depend &&
483 	    (composite->rd_done & composite->wr_depend)==composite->wr_depend &&
484 	    (composite->wr_needed & (~composite->wr_done))) {
485 	    index = composite->wr_needed & ~composite->wr_done;
486 	}
487 
488 	mtx_unlock(&composite->lock);
489 
490 	/* if we have any ready candidates kick them off */
491 	if (index) {
492 	    int bit;
493 
494 	    for (bit = 0; bit < MAX_COMPOSITES; bit++) {
495 		if (index & (1 << bit))
496 		    ata_start(device_get_parent(composite->request[bit]->dev));
497 	    }
498 	}
499     }
500 
501     /* get results back to the initiator for this request */
502     if (request->callback)
503 	(request->callback)(request);
504     else
505 	sema_post(&request->done);
506 
507     /* only call ata_start if channel is present */
508     if (ch)
509 	ata_start(ch->dev);
510 }
511 #endif
512 
513 #ifndef ATA_CAM
514 void
ata_fail_requests(device_t dev)515 ata_fail_requests(device_t dev)
516 {
517     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
518     struct ata_request *request, *tmp;
519     TAILQ_HEAD(, ata_request) fail_requests;
520     TAILQ_INIT(&fail_requests);
521 
522     /* grap all channel locks to avoid races */
523     mtx_lock(&ch->queue_mtx);
524     mtx_lock(&ch->state_mtx);
525 
526     /* do we have any running request to care about ? */
527     if ((request = ch->running) && (!dev || request->dev == dev)) {
528 	callout_stop(&request->callout);
529 	ch->running = NULL;
530 	request->result = ENXIO;
531 	TAILQ_INSERT_TAIL(&fail_requests, request, chain);
532     }
533 
534     /* fail all requests queued on this channel for device dev if !NULL */
535     TAILQ_FOREACH_SAFE(request, &ch->ata_queue, chain, tmp) {
536 	if (!dev || request->dev == dev) {
537 	    TAILQ_REMOVE(&ch->ata_queue, request, chain);
538 	    request->result = ENXIO;
539 	    TAILQ_INSERT_TAIL(&fail_requests, request, chain);
540 	}
541     }
542 
543     mtx_unlock(&ch->state_mtx);
544     mtx_unlock(&ch->queue_mtx);
545 
546     /* finish up all requests collected above */
547     TAILQ_FOREACH_SAFE(request, &fail_requests, chain, tmp) {
548         TAILQ_REMOVE(&fail_requests, request, chain);
549         ata_finish(request);
550     }
551 }
552 #endif
553 
554 #ifndef ATA_CAM
555 /*
556  * Rudely drop all requests queued to the channel of specified device.
557  * XXX: The requests are leaked, use only in fatal case.
558  */
559 void
ata_drop_requests(device_t dev)560 ata_drop_requests(device_t dev)
561 {
562     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
563     struct ata_request *request, *tmp;
564 
565     mtx_lock(&ch->queue_mtx);
566     TAILQ_FOREACH_SAFE(request, &ch->ata_queue, chain, tmp) {
567 	TAILQ_REMOVE(&ch->ata_queue, request, chain);
568 	request->result = ENXIO;
569     }
570     mtx_unlock(&ch->queue_mtx);
571 }
572 #endif
573 
574 #ifndef ATA_CAM
575 static u_int64_t
ata_get_lba(struct ata_request * request)576 ata_get_lba(struct ata_request *request)
577 {
578     if (request->flags & ATA_R_ATAPI) {
579 	switch (request->u.atapi.ccb[0]) {
580 	case ATAPI_READ_BIG:
581 	case ATAPI_WRITE_BIG:
582 	case ATAPI_READ_CD:
583 	    return (request->u.atapi.ccb[5]) | (request->u.atapi.ccb[4]<<8) |
584 		   (request->u.atapi.ccb[3]<<16)|(request->u.atapi.ccb[2]<<24);
585 	case ATAPI_READ:
586 	case ATAPI_WRITE:
587 	    return (request->u.atapi.ccb[4]) | (request->u.atapi.ccb[3]<<8) |
588 		   (request->u.atapi.ccb[2]<<16);
589 	default:
590 	    return 0;
591 	}
592     }
593     else
594 	return request->u.ata.lba;
595 }
596 #endif
597 
598 #ifndef ATA_CAM
599 static void
ata_sort_queue(struct ata_channel * ch,struct ata_request * request)600 ata_sort_queue(struct ata_channel *ch, struct ata_request *request)
601 {
602     struct ata_request *this, *next;
603 
604     this = TAILQ_FIRST(&ch->ata_queue);
605 
606     /* if the queue is empty just insert */
607     if (!this) {
608 	if (request->composite)
609 	    ch->freezepoint = request;
610 	TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
611 	return;
612     }
613 
614     /* dont sort frozen parts of the queue */
615     if (ch->freezepoint)
616 	this = ch->freezepoint;
617 
618     /* if position is less than head we add after tipping point */
619     if (ata_get_lba(request) < ata_get_lba(this)) {
620 	while ((next = TAILQ_NEXT(this, chain))) {
621 
622 	    /* have we reached the tipping point */
623 	    if (ata_get_lba(next) < ata_get_lba(this)) {
624 
625 		/* sort the insert */
626 		do {
627 		    if (ata_get_lba(request) < ata_get_lba(next))
628 			break;
629 		    this = next;
630 		} while ((next = TAILQ_NEXT(this, chain)));
631 		break;
632 	    }
633 	    this = next;
634 	}
635     }
636 
637     /* we are after head so sort the insert before tipping point */
638     else {
639 	while ((next = TAILQ_NEXT(this, chain))) {
640 	    if (ata_get_lba(next) < ata_get_lba(this) ||
641 		ata_get_lba(request) < ata_get_lba(next))
642 		break;
643 	    this = next;
644 	}
645     }
646 
647     if (request->composite)
648 	ch->freezepoint = request;
649     TAILQ_INSERT_AFTER(&ch->ata_queue, this, request, chain);
650 }
651 #endif
652 
653 #ifndef ATA_CAM
654 static const char *
ata_skey2str(u_int8_t skey)655 ata_skey2str(u_int8_t skey)
656 {
657     switch (skey) {
658     case 0x00: return ("NO SENSE");
659     case 0x01: return ("RECOVERED ERROR");
660     case 0x02: return ("NOT READY");
661     case 0x03: return ("MEDIUM ERROR");
662     case 0x04: return ("HARDWARE ERROR");
663     case 0x05: return ("ILLEGAL REQUEST");
664     case 0x06: return ("UNIT ATTENTION");
665     case 0x07: return ("DATA PROTECT");
666     case 0x08: return ("BLANK CHECK");
667     case 0x09: return ("VENDOR SPECIFIC");
668     case 0x0a: return ("COPY ABORTED");
669     case 0x0b: return ("ABORTED COMMAND");
670     case 0x0c: return ("EQUAL");
671     case 0x0d: return ("VOLUME OVERFLOW");
672     case 0x0e: return ("MISCOMPARE");
673     case 0x0f: return ("RESERVED");
674     default: return("UNKNOWN");
675     }
676 }
677 #endif
678