The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/ata-queue.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/8.4/sys/dev/ata/ata-queue.c 233718 2012-03-30 23:56:19Z 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
   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
  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
  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
  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
  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
  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, "\2\10BUSY\7READY\6DMA_READY"
  379                               "\5DSC\4DRQ\3CORRECTABLE\2INDEX\1ERROR",
  380                               request->error, "\2\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 void
  514 ata_timeout(struct ata_request *request)
  515 {
  516     struct ata_channel *ch = device_get_softc(request->parent);
  517 
  518     //request->flags |= ATA_R_DEBUG;
  519     ATA_DEBUG_RQ(request, "timeout");
  520 
  521     /*
  522      * if we have an ATA_ACTIVE request running, we flag the request 
  523      * ATA_R_TIMEOUT so ata_finish will handle it correctly
  524      * also NULL out the running request so we wont loose 
  525      * the race with an eventual interrupt arriving late
  526      */
  527     if (ch->state == ATA_ACTIVE) {
  528         request->flags |= ATA_R_TIMEOUT;
  529         if (ch->dma.unload)
  530             ch->dma.unload(request);
  531         ch->running = NULL;
  532         ch->state = ATA_IDLE;
  533 #ifdef ATA_CAM
  534         ata_cam_end_transaction(ch->dev, request);
  535 #endif
  536         mtx_unlock(&ch->state_mtx);
  537 #ifndef ATA_CAM
  538         ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
  539         ata_finish(request);
  540 #endif
  541     }
  542     else {
  543         mtx_unlock(&ch->state_mtx);
  544     }
  545 }
  546 
  547 #ifndef ATA_CAM
  548 void
  549 ata_fail_requests(device_t dev)
  550 {
  551     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
  552     struct ata_request *request, *tmp;
  553     TAILQ_HEAD(, ata_request) fail_requests;
  554     TAILQ_INIT(&fail_requests);
  555 
  556     /* grap all channel locks to avoid races */
  557     mtx_lock(&ch->queue_mtx);
  558     mtx_lock(&ch->state_mtx);
  559 
  560     /* do we have any running request to care about ? */
  561     if ((request = ch->running) && (!dev || request->dev == dev)) {
  562         callout_stop(&request->callout);
  563         ch->running = NULL;
  564         request->result = ENXIO;
  565         TAILQ_INSERT_TAIL(&fail_requests, request, chain);
  566     }
  567 
  568     /* fail all requests queued on this channel for device dev if !NULL */
  569     TAILQ_FOREACH_SAFE(request, &ch->ata_queue, chain, tmp) {
  570         if (!dev || request->dev == dev) {
  571             TAILQ_REMOVE(&ch->ata_queue, request, chain);
  572             request->result = ENXIO;
  573             TAILQ_INSERT_TAIL(&fail_requests, request, chain);
  574         }
  575     }
  576 
  577     mtx_unlock(&ch->state_mtx);
  578     mtx_unlock(&ch->queue_mtx);
  579    
  580     /* finish up all requests collected above */
  581     TAILQ_FOREACH_SAFE(request, &fail_requests, chain, tmp) {
  582         TAILQ_REMOVE(&fail_requests, request, chain);
  583         ata_finish(request);
  584     }
  585 }
  586 #endif
  587 
  588 #ifndef ATA_CAM
  589 /*
  590  * Rudely drop all requests queued to the channel of specified device.
  591  * XXX: The requests are leaked, use only in fatal case.
  592  */
  593 void
  594 ata_drop_requests(device_t dev)
  595 {
  596     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
  597     struct ata_request *request, *tmp;
  598 
  599     mtx_lock(&ch->queue_mtx);
  600     TAILQ_FOREACH_SAFE(request, &ch->ata_queue, chain, tmp) {
  601         TAILQ_REMOVE(&ch->ata_queue, request, chain);
  602         request->result = ENXIO;
  603     }
  604     mtx_unlock(&ch->queue_mtx);
  605 }
  606 #endif
  607 
  608 #ifndef ATA_CAM
  609 static u_int64_t
  610 ata_get_lba(struct ata_request *request)
  611 {
  612     if (request->flags & ATA_R_ATAPI) {
  613         switch (request->u.atapi.ccb[0]) {
  614         case ATAPI_READ_BIG:
  615         case ATAPI_WRITE_BIG:
  616         case ATAPI_READ_CD:
  617             return (request->u.atapi.ccb[5]) | (request->u.atapi.ccb[4]<<8) |
  618                    (request->u.atapi.ccb[3]<<16)|(request->u.atapi.ccb[2]<<24);
  619         case ATAPI_READ:
  620         case ATAPI_WRITE:
  621             return (request->u.atapi.ccb[4]) | (request->u.atapi.ccb[3]<<8) |
  622                    (request->u.atapi.ccb[2]<<16);
  623         default:
  624             return 0;
  625         }
  626     }
  627     else
  628         return request->u.ata.lba;
  629 }
  630 #endif
  631 
  632 #ifndef ATA_CAM
  633 static void
  634 ata_sort_queue(struct ata_channel *ch, struct ata_request *request)
  635 {
  636     struct ata_request *this, *next;
  637 
  638     this = TAILQ_FIRST(&ch->ata_queue);
  639 
  640     /* if the queue is empty just insert */
  641     if (!this) {
  642         if (request->composite)
  643             ch->freezepoint = request;
  644         TAILQ_INSERT_TAIL(&ch->ata_queue, request, chain);
  645         return;
  646     }
  647 
  648     /* dont sort frozen parts of the queue */
  649     if (ch->freezepoint)
  650         this = ch->freezepoint;
  651         
  652     /* if position is less than head we add after tipping point */
  653     if (ata_get_lba(request) < ata_get_lba(this)) {
  654         while ((next = TAILQ_NEXT(this, chain))) {
  655 
  656             /* have we reached the tipping point */
  657             if (ata_get_lba(next) < ata_get_lba(this)) {
  658 
  659                 /* sort the insert */
  660                 do {
  661                     if (ata_get_lba(request) < ata_get_lba(next))
  662                         break;
  663                     this = next;
  664                 } while ((next = TAILQ_NEXT(this, chain)));
  665                 break;
  666             }
  667             this = next;
  668         }
  669     }
  670 
  671     /* we are after head so sort the insert before tipping point */
  672     else {
  673         while ((next = TAILQ_NEXT(this, chain))) {
  674             if (ata_get_lba(next) < ata_get_lba(this) ||
  675                 ata_get_lba(request) < ata_get_lba(next))
  676                 break;
  677             this = next;
  678         }
  679     }
  680 
  681     if (request->composite)
  682         ch->freezepoint = request;
  683     TAILQ_INSERT_AFTER(&ch->ata_queue, this, request, chain);
  684 }
  685 #endif
  686 
  687 const char *
  688 ata_cmd2str(struct ata_request *request)
  689 {
  690     static char buffer[20];
  691 
  692     if (request->flags & ATA_R_ATAPI) {
  693         switch (request->u.atapi.sense.key ?
  694                 request->u.atapi.saved_cmd : request->u.atapi.ccb[0]) {
  695         case 0x00: return ("TEST_UNIT_READY");
  696         case 0x01: return ("REZERO");
  697         case 0x03: return ("REQUEST_SENSE");
  698         case 0x04: return ("FORMAT");
  699         case 0x08: return ("READ");
  700         case 0x0a: return ("WRITE");
  701         case 0x10: return ("WEOF");
  702         case 0x11: return ("SPACE");
  703         case 0x12: return ("INQUIRY");
  704         case 0x15: return ("MODE_SELECT");
  705         case 0x19: return ("ERASE");
  706         case 0x1a: return ("MODE_SENSE");
  707         case 0x1b: return ("START_STOP");
  708         case 0x1e: return ("PREVENT_ALLOW");
  709         case 0x23: return ("ATAPI_READ_FORMAT_CAPACITIES");
  710         case 0x25: return ("READ_CAPACITY");
  711         case 0x28: return ("READ_BIG");
  712         case 0x2a: return ("WRITE_BIG");
  713         case 0x2b: return ("LOCATE");
  714         case 0x34: return ("READ_POSITION");
  715         case 0x35: return ("SYNCHRONIZE_CACHE");
  716         case 0x3b: return ("WRITE_BUFFER");
  717         case 0x3c: return ("READ_BUFFER");
  718         case 0x42: return ("READ_SUBCHANNEL");
  719         case 0x43: return ("READ_TOC");
  720         case 0x45: return ("PLAY_10");
  721         case 0x47: return ("PLAY_MSF");
  722         case 0x48: return ("PLAY_TRACK");
  723         case 0x4b: return ("PAUSE");
  724         case 0x51: return ("READ_DISK_INFO");
  725         case 0x52: return ("READ_TRACK_INFO");
  726         case 0x53: return ("RESERVE_TRACK");
  727         case 0x54: return ("SEND_OPC_INFO");
  728         case 0x55: return ("MODE_SELECT_BIG");
  729         case 0x58: return ("REPAIR_TRACK");
  730         case 0x59: return ("READ_MASTER_CUE");
  731         case 0x5a: return ("MODE_SENSE_BIG");
  732         case 0x5b: return ("CLOSE_TRACK/SESSION");
  733         case 0x5c: return ("READ_BUFFER_CAPACITY");
  734         case 0x5d: return ("SEND_CUE_SHEET");
  735         case 0x96: return ("SERVICE_ACTION_IN");
  736         case 0xa1: return ("BLANK_CMD");
  737         case 0xa3: return ("SEND_KEY");
  738         case 0xa4: return ("REPORT_KEY");
  739         case 0xa5: return ("PLAY_12");
  740         case 0xa6: return ("LOAD_UNLOAD");
  741         case 0xad: return ("READ_DVD_STRUCTURE");
  742         case 0xb4: return ("PLAY_CD");
  743         case 0xbb: return ("SET_SPEED");
  744         case 0xbd: return ("MECH_STATUS");
  745         case 0xbe: return ("READ_CD");
  746         case 0xff: return ("POLL_DSC");
  747         }
  748     }
  749     else {
  750         switch (request->u.ata.command) {
  751         case 0x00: return ("NOP");
  752         case 0x08: return ("DEVICE_RESET");
  753         case 0x20: return ("READ");
  754         case 0x24: return ("READ48");
  755         case 0x25: return ("READ_DMA48");
  756         case 0x26: return ("READ_DMA_QUEUED48");
  757         case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
  758         case 0x29: return ("READ_MUL48");
  759         case 0x30: return ("WRITE");
  760         case 0x34: return ("WRITE48");
  761         case 0x35: return ("WRITE_DMA48");
  762         case 0x36: return ("WRITE_DMA_QUEUED48");
  763         case 0x37: return ("SET_MAX_ADDRESS48");
  764         case 0x39: return ("WRITE_MUL48");
  765         case 0x70: return ("SEEK");
  766         case 0xa0: return ("PACKET_CMD");
  767         case 0xa1: return ("ATAPI_IDENTIFY");
  768         case 0xa2: return ("SERVICE");
  769         case 0xb0: return ("SMART");
  770         case 0xc0: return ("CFA ERASE");
  771         case 0xc4: return ("READ_MUL");
  772         case 0xc5: return ("WRITE_MUL");
  773         case 0xc6: return ("SET_MULTI");
  774         case 0xc7: return ("READ_DMA_QUEUED");
  775         case 0xc8: return ("READ_DMA");
  776         case 0xca: return ("WRITE_DMA");
  777         case 0xcc: return ("WRITE_DMA_QUEUED");
  778         case 0xe6: return ("SLEEP");
  779         case 0xe7: return ("FLUSHCACHE");
  780         case 0xea: return ("FLUSHCACHE48");
  781         case 0xec: return ("ATA_IDENTIFY");
  782         case 0xef:
  783             switch (request->u.ata.feature) {
  784             case 0x03: return ("SETFEATURES SET TRANSFER MODE");
  785             case 0x02: return ("SETFEATURES ENABLE WCACHE");
  786             case 0x82: return ("SETFEATURES DISABLE WCACHE");
  787             case 0xaa: return ("SETFEATURES ENABLE RCACHE");
  788             case 0x55: return ("SETFEATURES DISABLE RCACHE");
  789             }
  790             sprintf(buffer, "SETFEATURES 0x%02x", request->u.ata.feature);
  791             return buffer;
  792         case 0xf5: return ("SECURITY_FREE_LOCK");
  793         case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
  794         case 0xf9: return ("SET_MAX_ADDRESS");
  795         }
  796     }
  797     sprintf(buffer, "unknown CMD (0x%02x)", request->u.ata.command);
  798     return buffer;
  799 }
  800 
  801 #ifndef ATA_CAM
  802 static const char *
  803 ata_skey2str(u_int8_t skey)
  804 {
  805     switch (skey) {
  806     case 0x00: return ("NO SENSE");
  807     case 0x01: return ("RECOVERED ERROR");
  808     case 0x02: return ("NOT READY");
  809     case 0x03: return ("MEDIUM ERROR");
  810     case 0x04: return ("HARDWARE ERROR");
  811     case 0x05: return ("ILLEGAL REQUEST");
  812     case 0x06: return ("UNIT ATTENTION");
  813     case 0x07: return ("DATA PROTECT");
  814     case 0x08: return ("BLANK CHECK");
  815     case 0x09: return ("VENDOR SPECIFIC");
  816     case 0x0a: return ("COPY ABORTED");
  817     case 0x0b: return ("ABORTED COMMAND");
  818     case 0x0c: return ("EQUAL");
  819     case 0x0d: return ("VOLUME OVERFLOW");
  820     case 0x0e: return ("MISCOMPARE");
  821     case 0x0f: return ("RESERVED");
  822     default: return("UNKNOWN");
  823     }
  824 }
  825 #endif

Cache object: aecce6b7ddf0039bd34380acd4d00a2c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.