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/atapi-cam.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) 2001-2007 Thomas Quinot <thomas@cuivre.fr.eu.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  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/bus.h>
   35 #include <sys/kernel.h>
   36 #include <sys/malloc.h>
   37 #include <sys/ata.h>
   38 #include <sys/taskqueue.h>
   39 #include <sys/lock.h>
   40 #include <sys/mutex.h>
   41 #include <sys/sema.h>
   42 #include <vm/uma.h>
   43 #include <machine/resource.h>
   44 #include <machine/bus.h>
   45 
   46 #include <cam/cam.h>
   47 #include <cam/cam_ccb.h>
   48 #include <cam/cam_periph.h>
   49 #include <cam/cam_sim.h>
   50 #include <cam/cam_xpt_sim.h>
   51 #include <cam/cam_debug.h>
   52 #include <cam/scsi/scsi_all.h>
   53 
   54 #include <dev/ata/ata-all.h>
   55 #include <ata_if.h>
   56 
   57 /* private data associated with an ATA bus */
   58 struct atapi_xpt_softc {
   59     struct ata_device   atapi_cam_dev;  /* must be first */
   60     device_t            dev;
   61     device_t            parent;
   62     struct ata_channel  *ata_ch;
   63     struct cam_path     *path;
   64     struct cam_sim      *sim;
   65     int                 flags;
   66 #define BUS_REGISTERED          0x01
   67 #define RESOURCE_SHORTAGE       0x02
   68 #define DETACHING               0x04
   69 
   70     TAILQ_HEAD(,atapi_hcb) pending_hcbs;
   71     struct ata_device   *atadev[2];
   72     struct mtx          state_lock;
   73 };
   74 
   75 /* hardware command descriptor block */
   76 struct atapi_hcb {
   77     struct atapi_xpt_softc *softc;
   78     int                 unit;
   79     int                 bus;
   80     int                 target;
   81     int                 lun;
   82     union ccb           *ccb;
   83     int                 flags;
   84 #define QUEUED          0x0001
   85 #define AUTOSENSE       0x0002
   86     char                *dxfer_alloc;
   87     TAILQ_ENTRY(atapi_hcb) chain;
   88 };
   89 
   90 enum reinit_reason { BOOT_ATTACH, ATTACH, RESET };
   91 
   92 /* Device methods */
   93 static void atapi_cam_identify(driver_t *dev, device_t parent);
   94 static int atapi_cam_probe(device_t dev);
   95 static int atapi_cam_attach(device_t dev);
   96 static int atapi_cam_detach(device_t dev);
   97 static int atapi_cam_reinit(device_t dev);
   98 
   99 /* CAM XPT methods */
  100 static void atapi_action(struct cam_sim *, union ccb *);
  101 static void atapi_poll(struct cam_sim *);
  102 static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
  103 static void atapi_cb(struct ata_request *);
  104 
  105 /* Module methods */
  106 static int atapi_cam_event_handler(module_t mod, int what, void *arg);
  107 
  108 /* internal functions */
  109 static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
  110 static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
  111 static void cam_rescan(struct cam_sim *);
  112 static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
  113 static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
  114 static void free_hcb(struct atapi_hcb *hcb);
  115 static void free_softc(struct atapi_xpt_softc *scp);
  116 
  117 static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
  118 
  119 static device_method_t atapi_cam_methods[] = {
  120         DEVMETHOD(device_identify,      atapi_cam_identify),
  121         DEVMETHOD(device_probe,         atapi_cam_probe),
  122         DEVMETHOD(device_attach,        atapi_cam_attach),
  123         DEVMETHOD(device_detach,        atapi_cam_detach),
  124         DEVMETHOD(ata_reinit,           atapi_cam_reinit),
  125         DEVMETHOD_END
  126 };
  127 
  128 static driver_t atapi_cam_driver = {
  129         "atapicam",
  130         atapi_cam_methods,
  131         sizeof(struct atapi_xpt_softc)
  132 };
  133 
  134 static devclass_t       atapi_cam_devclass;
  135 DRIVER_MODULE(atapicam, ata,
  136         atapi_cam_driver,
  137         atapi_cam_devclass,
  138         atapi_cam_event_handler,
  139         /*arg*/NULL);
  140 MODULE_VERSION(atapicam, 1);
  141 MODULE_DEPEND(atapicam, ata, 1, 1, 1);
  142 MODULE_DEPEND(atapicam, cam, 1, 1, 1);
  143 
  144 static void
  145 atapi_cam_identify(driver_t *driver, device_t parent)
  146 {
  147         struct atapi_xpt_softc *scp =
  148             malloc (sizeof (struct atapi_xpt_softc), M_ATACAM, M_NOWAIT|M_ZERO);
  149         device_t child;
  150 
  151         if (scp == NULL) {
  152                 printf ("atapi_cam_identify: out of memory");
  153                 return;
  154         }
  155 
  156         /* Assume one atapicam instance per parent channel instance. */
  157         child = device_add_child(parent, "atapicam", -1);
  158         if (child == NULL) {
  159                 printf ("atapi_cam_identify: out of memory, can't add child");
  160                 free (scp, M_ATACAM);
  161                 return;
  162         }
  163         scp->atapi_cam_dev.unit = -1;
  164         scp->atapi_cam_dev.dev  = child;
  165         device_quiet(child);
  166         device_set_softc(child, scp);
  167 }
  168 
  169 static int
  170 atapi_cam_probe(device_t dev)
  171 {
  172         struct ata_device *atadev = device_get_softc (dev);
  173 
  174         KASSERT(atadev != NULL, ("expect valid struct ata_device"));
  175         if (atadev->unit < 0) {
  176                 device_set_desc(dev, "ATAPI CAM Attachment");
  177                 return (0);
  178         } else {
  179                 return ENXIO;
  180         }
  181 }
  182 
  183 static int
  184 atapi_cam_attach(device_t dev)
  185 {
  186     struct atapi_xpt_softc *scp = NULL;
  187     struct cam_devq *devq = NULL;
  188     struct cam_sim *sim = NULL;
  189     struct cam_path *path = NULL;
  190     int unit, error;
  191 
  192     scp = (struct atapi_xpt_softc *)device_get_softc(dev);
  193     if (scp == NULL) {
  194         device_printf(dev, "Cannot get softc\n");
  195         return (ENOMEM);
  196     }
  197 
  198     mtx_init(&scp->state_lock, "ATAPICAM lock", NULL, MTX_DEF);
  199 
  200     scp->dev = dev;
  201     scp->parent = device_get_parent(dev);
  202     scp->ata_ch = device_get_softc(scp->parent);
  203     TAILQ_INIT(&scp->pending_hcbs);
  204     unit = device_get_unit(device_get_parent(dev));
  205 
  206     if ((devq = cam_simq_alloc(16)) == NULL) {
  207         error = ENOMEM;
  208         goto out;
  209     }
  210 
  211     if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
  212                  (void *)scp, unit, &scp->state_lock, 1, 1, devq)) == NULL) {
  213         error = ENOMEM;
  214         goto out;
  215     }
  216     scp->sim = sim;
  217 
  218     mtx_lock(&scp->state_lock);
  219     if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
  220         error = EINVAL;
  221         mtx_unlock(&scp->state_lock);
  222         goto out;
  223     }
  224     scp->flags |= BUS_REGISTERED;
  225 
  226     if (xpt_create_path(&path, /*periph*/ NULL,
  227                 cam_sim_path(sim), CAM_TARGET_WILDCARD,
  228                 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  229         error = ENOMEM;
  230         mtx_unlock(&scp->state_lock);
  231         goto out;
  232     }
  233     scp->path = path;
  234 
  235     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
  236 
  237     setup_async_cb(scp, AC_LOST_DEVICE);
  238     reinit_bus(scp, cold ? BOOT_ATTACH : ATTACH);
  239     error = 0;
  240     mtx_unlock(&scp->state_lock);
  241 
  242 out:
  243     if (error != 0)
  244         free_softc(scp);
  245 
  246     return (error);
  247 }
  248 
  249 static int
  250 atapi_cam_detach(device_t dev)
  251 {
  252     struct atapi_xpt_softc *scp = device_get_softc(dev);
  253 
  254     mtx_lock(&scp->state_lock);
  255     if (xpt_sim_opened(scp->sim)) {
  256             mtx_unlock(&scp->state_lock);
  257             return (EBUSY);
  258     }
  259     xpt_freeze_simq(scp->sim, 1 /*count*/);
  260     scp->flags |= DETACHING;
  261     mtx_unlock(&scp->state_lock);
  262     free_softc(scp);
  263     return (0);
  264 }
  265 
  266 static int
  267 atapi_cam_reinit(device_t dev) {
  268     struct atapi_xpt_softc *scp = device_get_softc(dev);
  269 
  270     /*
  271      * scp might be null if the bus is being reinitialised during
  272      * the boot-up sequence, before the ATAPI bus is registered.
  273      */
  274 
  275     if (scp != NULL) {
  276         mtx_lock(&scp->state_lock);
  277         reinit_bus(scp, RESET);
  278         mtx_unlock(&scp->state_lock);
  279     }
  280     return (0);
  281 }
  282 
  283 static void
  284 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
  285     struct ata_device *old_atadev[2], *atadev;
  286     device_t *children;
  287     int nchildren, i, dev_changed;
  288 
  289     if (device_get_children(scp->parent, &children, &nchildren) != 0) {
  290         return;
  291     }
  292 
  293     old_atadev[0] = scp->atadev[0];
  294     old_atadev[1] = scp->atadev[1];
  295     scp->atadev[0] = NULL;
  296     scp->atadev[1] = NULL;
  297 
  298     for (i = 0; i < nchildren; i++) {
  299         /* XXX Does the child need to actually be attached yet? */
  300         if (children[i] != NULL) {
  301             atadev = device_get_softc(children[i]);
  302             if ((atadev->unit == ATA_MASTER) &&
  303                 (scp->ata_ch->devices & ATA_ATAPI_MASTER) != 0)
  304                 scp->atadev[0] = atadev;
  305             if ((atadev->unit == ATA_SLAVE) &&
  306                 (scp->ata_ch->devices & ATA_ATAPI_SLAVE) != 0)
  307                 scp->atadev[1] = atadev;
  308         }
  309     }
  310     dev_changed = (old_atadev[0] != scp->atadev[0])
  311                || (old_atadev[1] != scp->atadev[1]);
  312     free(children, M_TEMP);
  313 
  314     switch (reason) {
  315         case BOOT_ATTACH:
  316         case ATTACH:
  317             break;
  318         case RESET:
  319             xpt_async(AC_BUS_RESET, scp->path, NULL);
  320 
  321             if (!dev_changed)
  322                 break;
  323 
  324             cam_rescan(scp->sim);
  325             break;
  326     }
  327 }
  328 
  329 static void
  330 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
  331 {
  332     struct ccb_setasync csa;
  333 
  334     xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
  335     csa.ccb_h.func_code = XPT_SASYNC_CB;
  336     csa.event_enable = events;
  337     csa.callback = &atapi_async;
  338     csa.callback_arg = scp->sim;
  339     xpt_action((union ccb *) &csa);
  340 }
  341 
  342 static void
  343 atapi_action(struct cam_sim *sim, union ccb *ccb)
  344 {
  345     struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
  346     struct ccb_hdr *ccb_h = &ccb->ccb_h;
  347     struct atapi_hcb *hcb = NULL;
  348     struct ata_request *request = NULL;
  349     int unit = cam_sim_unit(sim);
  350     int bus = cam_sim_bus(sim);
  351     int len;
  352     char *buf;
  353 
  354     switch (ccb_h->func_code) {
  355     case XPT_PATH_INQ: {
  356         struct ccb_pathinq *cpi = &ccb->cpi;
  357         int tid = ccb_h->target_id;
  358 
  359         cpi->version_num = 1;
  360         cpi->hba_inquiry = 0;
  361         cpi->target_sprt = 0;
  362         cpi->hba_misc = PIM_NO_6_BYTE;
  363         cpi->hba_eng_cnt = 0;
  364         bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
  365         cpi->max_target = 1;
  366         cpi->max_lun = 0;
  367         cpi->async_flags = 0;
  368         cpi->hpath_id = 0;
  369         cpi->initiator_id = 7;
  370         strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
  371         strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
  372         strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
  373         cpi->unit_number = cam_sim_unit(sim);
  374         cpi->bus_id = cam_sim_bus(sim);
  375         cpi->base_transfer_speed = 3300;
  376         cpi->transport = XPORT_SPI;
  377         cpi->transport_version = 2;
  378         cpi->protocol = PROTO_SCSI;
  379         cpi->protocol_version = SCSI_REV_2;
  380 
  381         if (softc->ata_ch && tid != CAM_TARGET_WILDCARD) {
  382             if (softc->atadev[tid] == NULL) {
  383                 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
  384                 xpt_done(ccb);
  385                 return;
  386             }
  387             switch (softc->atadev[ccb_h->target_id]->mode) {
  388             case ATA_PIO1:
  389                 cpi->base_transfer_speed = 5200;
  390                 break;
  391             case ATA_PIO2:
  392                 cpi->base_transfer_speed = 7000;
  393                 break;
  394             case ATA_PIO3:
  395                 cpi->base_transfer_speed = 11000;
  396                 break;
  397             case ATA_PIO4:
  398             case ATA_DMA:
  399             case ATA_WDMA2:
  400                 cpi->base_transfer_speed = 16000;
  401                 break;
  402             case ATA_UDMA2:
  403                 cpi->base_transfer_speed = 33000;
  404                 break;
  405             case ATA_UDMA4:
  406                 cpi->base_transfer_speed = 66000;
  407                 break;
  408             case ATA_UDMA5:
  409                 cpi->base_transfer_speed = 100000;
  410                 break;
  411             case ATA_UDMA6:
  412                 cpi->base_transfer_speed = 133000;
  413                 break;
  414             case ATA_SA150:
  415                 cpi->base_transfer_speed = 150000;
  416                 break;
  417             case ATA_SA300:
  418                 cpi->base_transfer_speed = 300000;
  419                 break;
  420             default:
  421                 break;
  422             }
  423         }
  424         cpi->maxio = softc->ata_ch->dma.max_iosize ?
  425             softc->ata_ch->dma.max_iosize : DFLTPHYS;
  426         ccb->ccb_h.status = CAM_REQ_CMP;
  427         xpt_done(ccb);
  428         return;
  429     }
  430 
  431     case XPT_RESET_DEV: {
  432         int tid = ccb_h->target_id;
  433 
  434         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
  435         mtx_unlock(&softc->state_lock);
  436         ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0);
  437         mtx_lock(&softc->state_lock);
  438         ccb->ccb_h.status = CAM_REQ_CMP;
  439         xpt_done(ccb);
  440         return;
  441     }
  442 
  443     case XPT_RESET_BUS:
  444         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
  445         mtx_unlock(&softc->state_lock);
  446         ata_reinit(softc->parent);
  447         mtx_lock(&softc->state_lock);
  448         ccb->ccb_h.status = CAM_REQ_CMP;
  449         xpt_done(ccb);
  450         return;
  451 
  452     case XPT_SET_TRAN_SETTINGS:
  453         /* ignore these, we're not doing SCSI here */
  454         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
  455                   ("SET_TRAN_SETTINGS not supported\n"));
  456         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  457         xpt_done(ccb);
  458         return;
  459 
  460     case XPT_GET_TRAN_SETTINGS: {
  461         struct ccb_trans_settings *cts = &ccb->cts;
  462         cts->protocol = PROTO_SCSI;
  463         cts->protocol_version = SCSI_REV_2;
  464         cts->transport = XPORT_SPI;
  465         cts->transport_version = XPORT_VERSION_UNSPECIFIED;
  466         cts->proto_specific.valid = 0;
  467         cts->xport_specific.valid = 0;
  468         /* nothing more to do */
  469         ccb->ccb_h.status = CAM_REQ_CMP;
  470         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
  471         xpt_done(ccb);
  472         return;
  473     }
  474 
  475     case XPT_CALC_GEOMETRY: {
  476         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
  477         cam_calc_geometry(&ccb->ccg, /*extended*/1);
  478         xpt_done(ccb);
  479         return;
  480     }
  481 
  482     case XPT_SCSI_IO: {
  483         struct ccb_scsiio *csio = &ccb->csio;
  484         int tid = ccb_h->target_id, lid = ccb_h->target_lun;
  485         int request_flags = ATA_R_ATAPI;
  486 
  487         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
  488 
  489         if (softc->flags & DETACHING) {
  490             ccb->ccb_h.status = CAM_REQ_ABORTED;
  491             xpt_done(ccb);
  492             return;
  493         }
  494 
  495         if (softc->atadev[tid] == NULL) {
  496             ccb->ccb_h.status = CAM_DEV_NOT_THERE;
  497             xpt_done(ccb);
  498             return;
  499         }
  500 
  501         /* check that this request was not aborted already */
  502         if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
  503             printf("XPT_SCSI_IO received but already in progress?\n");
  504             xpt_done(ccb);
  505             return;
  506         }
  507         if (lid > 0) {
  508             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
  509                       ("SCSI IO received for invalid lun %d\n", lid));
  510             goto action_invalid;
  511         }
  512         if (csio->cdb_len > sizeof request->u.atapi.ccb) {
  513             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
  514                 ("CAM CCB too long for ATAPI"));
  515             goto action_invalid;
  516         }
  517 
  518         switch (ccb_h->flags & CAM_DIR_MASK) {
  519         case CAM_DIR_IN:
  520              request_flags |= ATA_R_READ;
  521              break;
  522         case CAM_DIR_OUT:
  523              request_flags |= ATA_R_WRITE;
  524              break;
  525         case CAM_DIR_NONE:
  526              /* No flags need to be set */
  527              break;
  528         default:
  529              device_printf(softc->dev, "unknown IO operation\n");
  530              goto action_invalid;
  531         }
  532 
  533         if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
  534             printf("cannot allocate ATAPI/CAM hcb\n");
  535             goto action_oom;
  536         }
  537         if ((request = ata_alloc_request()) == NULL) {
  538             printf("cannot allocate ATAPI/CAM request\n");
  539             goto action_oom;
  540         }
  541 
  542         bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
  543               csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
  544               request->u.atapi.ccb, csio->cdb_len);
  545 #ifdef CAMDEBUG
  546         if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
  547                 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
  548 
  549                 printf("atapi_action: hcb@%p: %s\n", hcb,
  550                        scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
  551         }
  552         if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_SUBTRACE)) {
  553                 request_flags |= ATA_R_DEBUG;
  554         }
  555 #endif
  556 
  557         len = csio->dxfer_len;
  558         buf = csio->data_ptr;
  559 
  560         /* some SCSI commands require special processing */
  561         switch (request->u.atapi.ccb[0]) {
  562         case INQUIRY: {
  563             /*
  564              * many ATAPI devices seem to report more than
  565              * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
  566              * information, but respond with some incorrect condition
  567              * when actually asked for it, so we are going to pretend
  568              * that only SHORT_INQUIRY_LENGTH are expected, anyway.
  569              */
  570             struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
  571 
  572             if (inq->byte2 == 0 && inq->page_code == 0 &&
  573                 scsi_2btoul(inq->length) > SHORT_INQUIRY_LENGTH) {
  574                 bzero(buf, len);
  575                 len = SHORT_INQUIRY_LENGTH;
  576                 scsi_ulto2b(len, inq->length);
  577             }
  578             break;
  579         }
  580         case READ_6:
  581             /* FALLTHROUGH */
  582 
  583         case WRITE_6:
  584             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
  585                       ("Translating %s into _10 equivalent\n",
  586                       (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
  587             request->u.atapi.ccb[0] |= 0x20;
  588             request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
  589             request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
  590             request->u.atapi.ccb[7] = 0;
  591             request->u.atapi.ccb[6] = 0;
  592             request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
  593             request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
  594             request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
  595             request->u.atapi.ccb[2] = 0;
  596             request->u.atapi.ccb[1] = 0;
  597             /* FALLTHROUGH */
  598 
  599         case READ_10:
  600             /* FALLTHROUGH */
  601         case WRITE_10:
  602             /* FALLTHROUGH */
  603         case READ_12:
  604             /* FALLTHROUGH */
  605         case WRITE_12:
  606             /*
  607              * Enable DMA (if target supports it) for READ and WRITE commands
  608              * only, as some combinations of drive, controller and chipset do
  609              * not behave correctly when DMA is enabled for other commands.
  610              */
  611             if (softc->atadev[tid]->mode >= ATA_DMA)
  612                 request_flags |= ATA_R_DMA;
  613             break;
  614 
  615         }
  616 
  617         if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
  618             /* ATA always transfers an even number of bytes */
  619             if ((buf = hcb->dxfer_alloc
  620                  = malloc(++len, M_ATACAM, M_NOWAIT | M_ZERO)) == NULL) {
  621                 printf("cannot allocate ATAPI/CAM buffer\n");
  622                 goto action_oom;
  623             }
  624         }
  625         request->dev = softc->atadev[tid]->dev;
  626         request->driver = hcb;
  627         request->data = buf;
  628         request->bytecount = len;
  629         request->transfersize = min(request->bytecount, 65534);
  630         request->timeout = (ccb_h->timeout + 999) / 1000;
  631         request->callback = &atapi_cb;
  632         request->flags = request_flags;
  633 
  634         /*
  635          * no retries are to be performed at the ATA level; any retries
  636          * will be done by CAM.
  637          */
  638         request->retries = 0;
  639 
  640         TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
  641         hcb->flags |= QUEUED;
  642         ccb_h->status |= CAM_SIM_QUEUED;
  643         mtx_unlock(&softc->state_lock);
  644 
  645         ata_queue_request(request);
  646         mtx_lock(&softc->state_lock);
  647         return;
  648     }
  649 
  650     default:
  651         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
  652                   ("unsupported function code 0x%02x\n", ccb_h->func_code));
  653         goto action_invalid;
  654     }
  655 
  656     /* NOTREACHED */
  657 
  658 action_oom:
  659     if (request != NULL)
  660         ata_free_request(request);
  661     if (hcb != NULL)
  662         free_hcb(hcb);
  663     xpt_print_path(ccb_h->path);
  664     printf("out of memory, freezing queue.\n");
  665     softc->flags |= RESOURCE_SHORTAGE;
  666     xpt_freeze_simq(sim, /*count*/ 1);
  667     ccb_h->status = CAM_REQUEUE_REQ;
  668     xpt_done(ccb);
  669     return;
  670 
  671 action_invalid:
  672     ccb_h->status = CAM_REQ_INVALID;
  673     xpt_done(ccb);
  674     return;
  675 }
  676 
  677 static void
  678 atapi_poll(struct cam_sim *sim)
  679 {
  680         struct atapi_xpt_softc *softc =
  681             (struct atapi_xpt_softc*)cam_sim_softc(sim);
  682 
  683         mtx_unlock(&softc->state_lock);
  684         ata_interrupt(softc->ata_ch);
  685         mtx_lock(&softc->state_lock);
  686 }
  687 
  688 static void
  689 atapi_cb(struct ata_request *request)
  690 {
  691     struct atapi_xpt_softc *scp;
  692     struct atapi_hcb *hcb;
  693     struct ccb_scsiio *csio;
  694     u_int32_t rc;
  695 
  696     hcb = (struct atapi_hcb *)request->driver;
  697     scp = hcb->softc;
  698     csio = &hcb->ccb->csio;
  699 
  700 #ifdef CAMDEBUG
  701 # define err (request->u.atapi.sense.key)
  702     if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
  703         printf("atapi_cb: hcb@%p sense = %02x: sk = %01x%s%s%s\n",
  704                hcb, err, err & 0x0f,
  705                (err & 0x80) ? ", Filemark" : "",
  706                (err & 0x40) ? ", EOM" : "",
  707                (err & 0x20) ? ", ILI" : "");
  708         device_printf(request->dev,
  709             "cmd %s status %02x result %02x error %02x\n",
  710             ata_cmd2str(request),
  711             request->status, request->result, request->error);
  712     }
  713 #endif
  714 
  715     if ((hcb->flags & AUTOSENSE) != 0) {
  716         rc = CAM_SCSI_STATUS_ERROR;
  717         if (request->result == 0) {
  718             csio->ccb_h.status |= CAM_AUTOSNS_VALID;
  719         }
  720     } else if (request->result != 0) {
  721         if ((request->flags & ATA_R_TIMEOUT) != 0) {
  722             rc = CAM_CMD_TIMEOUT;
  723         } else {
  724             rc = CAM_SCSI_STATUS_ERROR;
  725             csio->scsi_status = SCSI_STATUS_CHECK_COND;
  726 
  727             if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
  728 #if 0
  729                 static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
  730                     sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
  731                     0, 0, 0, 0, 0 };
  732 
  733                 bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
  734                 request->data = (caddr_t)&csio->sense_data;
  735                 request->bytecount = sizeof(struct atapi_sense);
  736                 request->transfersize = min(request->bytecount, 65534);
  737                 request->timeout = (csio->ccb_h.timeout + 999) / 1000;
  738                 request->retries = 2;
  739                 request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
  740                 hcb->flags |= AUTOSENSE;
  741 
  742                 ata_queue_request(request);
  743                 return;
  744 #else
  745                 /*
  746                  * Use auto-sense data from the ATA layer, if it has
  747                  * issued a REQUEST SENSE automatically and that operation
  748                  * returned without error.
  749                  */
  750                 if (request->u.atapi.sense.key != 0 && request->error == 0) {
  751                     bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense));
  752                     csio->ccb_h.status |= CAM_AUTOSNS_VALID;
  753                 }
  754             }
  755 #endif
  756         }
  757     } else {
  758         rc = CAM_REQ_CMP;
  759         csio->scsi_status = SCSI_STATUS_OK;
  760         if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
  761             hcb->dxfer_alloc != NULL)
  762         {
  763             bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
  764         }
  765     }
  766 
  767     mtx_lock(&scp->state_lock);
  768     free_hcb_and_ccb_done(hcb, rc);
  769     mtx_unlock(&scp->state_lock);
  770 
  771     ata_free_request(request);
  772 }
  773 
  774 static void
  775 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
  776 {
  777     struct atapi_xpt_softc *softc;
  778     union ccb *ccb;
  779 
  780     if (hcb == NULL)
  781         return;
  782 
  783     softc = hcb->softc;
  784     ccb = hcb->ccb;
  785 
  786     /* we're about to free a hcb, so the shortage has ended */
  787     if (softc->flags & RESOURCE_SHORTAGE) {
  788         softc->flags &= ~RESOURCE_SHORTAGE;
  789         status |= CAM_RELEASE_SIMQ;
  790     }
  791     free_hcb(hcb);
  792     ccb->ccb_h.status =
  793         status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
  794     xpt_done(ccb);
  795 }
  796 
  797 static void
  798 atapi_async(void *callback_arg, u_int32_t code,
  799              struct cam_path* path, void *arg)
  800 {
  801     int targ;
  802 
  803     GIANT_REQUIRED;
  804 
  805     switch (code) {
  806     case AC_LOST_DEVICE:
  807         targ = xpt_path_target_id(path);
  808         xpt_print_path(path);
  809         if (targ == -1)
  810                 printf("Lost host adapter\n");
  811         else
  812                 printf("Lost target %d???\n", targ);
  813         break;
  814 
  815     default:
  816         break;
  817     }
  818 }
  819 
  820 static void
  821 cam_rescan(struct cam_sim *sim)
  822 {
  823     union ccb *ccb;
  824 
  825     ccb = xpt_alloc_ccb_nowait();
  826     if (ccb == NULL)
  827         return;
  828     if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sim),
  829                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  830         xpt_free_ccb(ccb);
  831         return;
  832     }
  833     CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
  834     xpt_rescan(ccb);
  835     /* scan is in progress now */
  836 }
  837 
  838 static struct atapi_hcb *
  839 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
  840 {
  841     struct atapi_hcb *hcb = (struct atapi_hcb *)
  842     malloc(sizeof(struct atapi_hcb), M_ATACAM, M_NOWAIT | M_ZERO);
  843 
  844     if (hcb != NULL) {
  845         hcb->softc = softc;
  846         hcb->unit = unit;
  847         hcb->bus = bus;
  848         hcb->ccb = ccb;
  849     }
  850     return hcb;
  851 }
  852 
  853 static void
  854 free_hcb(struct atapi_hcb *hcb)
  855 {
  856     if ((hcb->flags & QUEUED) != 0)
  857         TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
  858     if (hcb->dxfer_alloc != NULL)
  859         free(hcb->dxfer_alloc, M_ATACAM);
  860     free(hcb, M_ATACAM);
  861 }
  862 
  863 static void
  864 free_softc(struct atapi_xpt_softc *scp)
  865 {
  866     struct atapi_hcb *hcb, *thcb;
  867 
  868     if (scp != NULL) {
  869         mtx_lock(&scp->state_lock);
  870         TAILQ_FOREACH_SAFE(hcb, &scp->pending_hcbs, chain, thcb) {
  871             free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
  872         }
  873         if (scp->path != NULL) {
  874             setup_async_cb(scp, 0);
  875             xpt_free_path(scp->path);
  876         }
  877         if ((scp->flags & BUS_REGISTERED) != 0) {
  878             if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
  879                 scp->flags &= ~BUS_REGISTERED;
  880         }
  881         if (scp->sim != NULL) {
  882             if ((scp->flags & BUS_REGISTERED) == 0)
  883                 cam_sim_free(scp->sim, /*free_devq*/TRUE);
  884             else
  885                 printf("Can't free %s SIM (still registered)\n",
  886                        cam_sim_name(scp->sim));
  887         }
  888         mtx_destroy(&scp->state_lock);
  889     }
  890 }
  891 
  892 static int
  893 atapi_cam_event_handler(module_t mod, int what, void *arg) {
  894     device_t *devlist;
  895     int devcount;
  896 
  897     switch (what) {
  898         case MOD_UNLOAD:
  899             if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
  900                   != 0)
  901                 return ENXIO;
  902             if (devlist != NULL) {
  903                 while (devlist != NULL && devcount > 0) {
  904                     device_t child = devlist[--devcount];
  905                     struct atapi_xpt_softc *scp = device_get_softc(child);
  906 
  907                     device_delete_child(device_get_parent(child),child);
  908                     if (scp != NULL)
  909                         free(scp, M_ATACAM);
  910                 }
  911                 free(devlist, M_TEMP);
  912             }
  913             break;
  914 
  915         default:
  916             break;
  917     }
  918     return 0;
  919 }

Cache object: 19060b9213e58951c1481cf719efac8b


[ 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.