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/twa/tw_osl_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) 2004-05 Applied Micro Circuits Corporation.
    3  * Copyright (c) 2004-05 Vinod Kashyap.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   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 AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  *      $FreeBSD: src/sys/dev/twa/tw_osl_cam.c,v 1.2.2.3 2005/12/07 18:20:37 vkashyap Exp $
   28  */
   29 
   30 /*
   31  * AMCC'S 3ware driver for 9000 series storage controllers.
   32  *
   33  * Author: Vinod Kashyap
   34  */
   35 
   36 
   37 /*
   38  * FreeBSD CAM related functions.
   39  */
   40 
   41 
   42 #include <dev/twa/tw_osl_includes.h>
   43 
   44 #include <cam/cam.h>
   45 #include <cam/cam_ccb.h>
   46 #include <cam/cam_sim.h>
   47 #include <cam/cam_xpt_sim.h>
   48 #include <cam/cam_xpt_periph.h>
   49 #include <cam/cam_debug.h>
   50 #include <cam/cam_periph.h>
   51 
   52 #include <cam/scsi/scsi_all.h>
   53 #include <cam/scsi/scsi_message.h>
   54 
   55 static TW_VOID  twa_action(struct cam_sim *sim, union ccb *ccb);
   56 static TW_VOID  twa_poll(struct cam_sim *sim);
   57 static TW_VOID  twa_async(TW_VOID *callback_arg, TW_UINT32 code,
   58         struct cam_path *path, TW_VOID *arg);
   59 static TW_VOID  twa_timeout(TW_VOID *arg);
   60 static TW_VOID  twa_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
   61 
   62 static TW_INT32 tw_osli_execute_scsi(struct tw_osli_req_context *req,
   63         union ccb *ccb);
   64 
   65 
   66 
   67 /*
   68  * Function name:       tw_osli_cam_attach
   69  * Description:         Attaches the driver to CAM.
   70  *
   71  * Input:               sc      -- ptr to OSL internal ctlr context
   72  * Output:              None
   73  * Return value:        0       -- success
   74  *                      non-zero-- failure
   75  */
   76 TW_INT32
   77 tw_osli_cam_attach(struct twa_softc *sc)
   78 {
   79         struct cam_devq         *devq;
   80         struct ccb_setasync     csa;
   81         TW_INT32                error;
   82 
   83         tw_osli_dbg_dprintf(3, sc, "entered");
   84 
   85         /*
   86          * Create the device queue for our SIM.
   87          */
   88         if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_IOS)) == NULL) {
   89                 tw_osli_printf(sc, "error = %d",
   90                         TW_CL_SEVERITY_ERROR_STRING,
   91                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
   92                         0x2100,
   93                         "Failed to create SIM device queue",
   94                         ENOMEM);
   95                 return(ENOMEM);
   96         }
   97 
   98         /*
   99          * Create a SIM entry.  Though we can support TW_OSLI_MAX_NUM_IOS
  100          * simultaneous requests, we claim to be able to handle only
  101          * (TW_OSLI_MAX_NUM_IOS - 1), so that we always have a request
  102          * packet available to service ioctls.
  103          */
  104         tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
  105         sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
  106                         device_get_unit(sc->bus_dev),
  107                         TW_OSLI_MAX_NUM_IOS - 1, 1, devq);
  108         if (sc->sim == NULL) {
  109                 cam_simq_free(devq);
  110                 tw_osli_printf(sc, "error = %d",
  111                         TW_CL_SEVERITY_ERROR_STRING,
  112                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  113                         0x2101,
  114                         "Failed to create a SIM entry",
  115                         ENOMEM);
  116                 return(ENOMEM);
  117         }
  118 
  119         /*
  120          * Register the bus.
  121          */
  122         tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
  123         mtx_lock(&Giant);
  124         if (xpt_bus_register(sc->sim, 0) != CAM_SUCCESS) {
  125                 cam_sim_free(sc->sim, TRUE);
  126                 sc->sim = NULL; /* so cam_detach will not try to free it */
  127                 tw_osli_printf(sc, "error = %d",
  128                         TW_CL_SEVERITY_ERROR_STRING,
  129                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  130                         0x2102,
  131                         "Failed to register the bus",
  132                         ENXIO);
  133                 return(ENXIO);
  134         }
  135 
  136         tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
  137         if (xpt_create_path(&sc->path, NULL,
  138                                 cam_sim_path(sc->sim),
  139                                 CAM_TARGET_WILDCARD,
  140                                 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  141                 xpt_bus_deregister(cam_sim_path (sc->sim));
  142                 /* Passing TRUE to cam_sim_free will free the devq as well. */
  143                 cam_sim_free(sc->sim, TRUE);
  144                 tw_osli_printf(sc, "error = %d",
  145                         TW_CL_SEVERITY_ERROR_STRING,
  146                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  147                         0x2103,
  148                         "Failed to create path",
  149                         ENXIO);
  150                 return(ENXIO);
  151         }
  152 
  153         tw_osli_dbg_dprintf(3, sc, "Calling xpt_setup_ccb");
  154         xpt_setup_ccb(&csa.ccb_h, sc->path, 5);
  155         csa.ccb_h.func_code = XPT_SASYNC_CB;
  156         csa.event_enable = AC_FOUND_DEVICE | AC_LOST_DEVICE;
  157         csa.callback = twa_async;
  158         csa.callback_arg = sc;
  159         xpt_action((union ccb *)&csa);
  160         mtx_unlock(&Giant);
  161 
  162         tw_osli_dbg_dprintf(3, sc, "Calling tw_osli_request_bus_scan");
  163         /*
  164          * Request a bus scan, so that CAM gets to know of
  165          * the logical units that we control.
  166          */
  167         if ((error = tw_osli_request_bus_scan(sc)))
  168                 tw_osli_printf(sc, "error = %d",
  169                         TW_CL_SEVERITY_ERROR_STRING,
  170                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  171                         0x2104,
  172                         "Bus scan request to CAM failed",
  173                         error);
  174         
  175         tw_osli_dbg_dprintf(3, sc, "exiting");
  176         return(0);
  177 }
  178 
  179 
  180 
  181 /*
  182  * Function name:       tw_osli_cam_detach
  183  * Description:         Detaches the driver from CAM.
  184  *
  185  * Input:               sc      -- ptr to OSL internal ctlr context
  186  * Output:              None
  187  * Return value:        None
  188  */
  189 TW_VOID
  190 tw_osli_cam_detach(struct twa_softc *sc)
  191 {
  192         tw_osli_dbg_dprintf(3, sc, "entered");
  193 
  194         mtx_lock(&Giant);
  195         if (sc->path)
  196                 xpt_free_path(sc->path);
  197         if (sc->sim) {
  198                 xpt_bus_deregister(cam_sim_path(sc->sim));
  199                 /* Passing TRUE to cam_sim_free will free the devq as well. */
  200                 cam_sim_free(sc->sim, TRUE);
  201         }
  202         mtx_unlock(&Giant);
  203 }
  204 
  205 
  206 
  207 /*
  208  * Function name:       tw_osli_complete_ccb
  209  * Description:         Completes a ccb to CAM.
  210  *
  211  * Input:               ccb     -- ptr to CAM request
  212  * Output:              None
  213  * Return value:        None
  214  */
  215 static __inline TW_VOID
  216 tw_osli_complete_ccb(union ccb *ccb)
  217 {
  218         mtx_lock(&Giant);
  219         xpt_done(ccb);
  220         mtx_unlock(&Giant);
  221 }
  222 
  223 
  224 
  225 /*
  226  * Function name:       tw_osli_execute_scsi
  227  * Description:         Build a fw cmd, based on a CAM style ccb, and
  228  *                      send it down.
  229  *
  230  * Input:               req     -- ptr to OSL internal request context
  231  *                      ccb     -- ptr to CAM style ccb
  232  * Output:              None
  233  * Return value:        0       -- success
  234  *                      non-zero-- failure
  235  */
  236 TW_INT32
  237 tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
  238 {
  239         struct twa_softc                *sc = req->ctlr;
  240         struct tw_cl_req_packet         *req_pkt;
  241         struct tw_cl_scsi_req_packet    *scsi_req;
  242         struct ccb_hdr                  *ccb_h = &(ccb->ccb_h);
  243         struct ccb_scsiio               *csio = &(ccb->csio);
  244         TW_INT32                        error;
  245 
  246         tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
  247                 csio->cdb_io.cdb_bytes[0]);
  248 
  249         if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
  250                 tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %x",
  251                         ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
  252                 ccb_h->status |= CAM_TID_INVALID;
  253                 tw_osli_complete_ccb(ccb);
  254                 return(1);
  255         }
  256         if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
  257                 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %x",
  258                         ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
  259                 ccb_h->status |= CAM_LUN_INVALID;
  260                 tw_osli_complete_ccb(ccb);
  261                 return(1);
  262         }
  263 
  264         if(ccb_h->flags & CAM_CDB_PHYS) {
  265                 tw_osli_printf(sc, "",
  266                         TW_CL_SEVERITY_ERROR_STRING,
  267                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  268                         0x2105,
  269                         "Physical CDB address!");
  270                 ccb_h->status = CAM_REQ_CMP_ERR;
  271                 tw_osli_complete_ccb(ccb);
  272                 return(1);
  273         }
  274 
  275         /*
  276          * We are going to work on this request.  Mark it as enqueued (though
  277          * we don't actually queue it...)
  278          */
  279         ccb_h->status |= CAM_SIM_QUEUED;
  280 
  281         if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  282                 if(ccb_h->flags & CAM_DIR_IN)
  283                         req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
  284                 else
  285                         req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
  286         }
  287 
  288         /* Build the CL understood request packet for SCSI cmds. */
  289         req_pkt = &req->req_pkt;
  290         req_pkt->status = 0;
  291         req_pkt->tw_osl_callback = tw_osl_complete_io;
  292         scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
  293         scsi_req->unit = ccb_h->target_id;
  294         scsi_req->lun = ccb_h->target_lun;
  295         scsi_req->sense_len = 0;
  296         scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
  297         scsi_req->scsi_status = 0;
  298         if(ccb_h->flags & CAM_CDB_POINTER)
  299                 scsi_req->cdb = csio->cdb_io.cdb_ptr;
  300         else
  301                 scsi_req->cdb = csio->cdb_io.cdb_bytes;
  302         scsi_req->cdb_len = csio->cdb_len;
  303 
  304         if (!(ccb_h->flags & CAM_DATA_PHYS)) {
  305                 /* Virtual data addresses.  Need to convert them... */
  306                 tw_osli_dbg_dprintf(3, sc,
  307                         "XPT_SCSI_IO: Single virtual address!");
  308                 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
  309                         if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
  310                                 tw_osli_printf(sc, "size = %d",
  311                                         TW_CL_SEVERITY_ERROR_STRING,
  312                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  313                                         0x2106,
  314                                         "I/O size too big",
  315                                         csio->dxfer_len);
  316                                 ccb_h->status = CAM_REQ_TOO_BIG;
  317                                 tw_osli_complete_ccb(ccb);
  318                                 return(1);
  319                         }
  320 
  321                         if ((req->length = csio->dxfer_len)) {
  322                                 req->data = csio->data_ptr;
  323                                 scsi_req->sgl_entries = 1;
  324                         }
  325                 } else {
  326                         tw_osli_printf(sc, "",
  327                                 TW_CL_SEVERITY_ERROR_STRING,
  328                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  329                                 0x2107,
  330                                 "XPT_SCSI_IO: Got SGList");
  331                         ccb_h->status = CAM_REQ_CMP_ERR;
  332                         tw_osli_complete_ccb(ccb);
  333                         return(1);
  334                 }
  335         } else {
  336                 /* Data addresses are physical. */
  337                 tw_osli_printf(sc, "",
  338                         TW_CL_SEVERITY_ERROR_STRING,
  339                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  340                         0x2108,
  341                         "XPT_SCSI_IO: Physical data addresses");
  342                 ccb_h->status = CAM_REQ_CMP_ERR;
  343                 ccb_h->status |= CAM_RELEASE_SIMQ;
  344                 ccb_h->status &= ~CAM_SIM_QUEUED;
  345                 tw_osli_complete_ccb(ccb);
  346                 return(1);
  347         }
  348 
  349         ccb_h->timeout_ch = timeout(twa_timeout, req,
  350                 (ccb_h->timeout * hz) / 1000);
  351         /*
  352          * twa_map_load_data_callback will fill in the SGL,
  353          * and submit the I/O.
  354          */
  355         error = tw_osli_map_request(req);
  356         return(error);
  357 }
  358 
  359 
  360 
  361 /*
  362  * Function name:       twa_action
  363  * Description:         Driver entry point for CAM's use.
  364  *
  365  * Input:               sim     -- sim corresponding to the ctlr
  366  *                      ccb     -- ptr to CAM request
  367  * Output:              None
  368  * Return value:        None
  369  */
  370 TW_VOID
  371 twa_action(struct cam_sim *sim, union ccb *ccb)
  372 {
  373         struct twa_softc        *sc = (struct twa_softc *)cam_sim_softc(sim);
  374         struct ccb_hdr          *ccb_h = &(ccb->ccb_h);
  375 
  376         switch (ccb_h->func_code) {
  377         case XPT_SCSI_IO:       /* SCSI I/O */
  378         {
  379                 struct tw_osli_req_context      *req;
  380 
  381                 if ((sc->state & TW_OSLI_CTLR_STATE_SIMQ_FROZEN) ||
  382                                 ((req = tw_osli_get_request(sc)) == NULL)) {
  383                         tw_osli_dbg_dprintf(2, sc,
  384                                 "simq frozen/Cannot get request pkt.");
  385                         /*
  386                          * Freeze the simq to maintain ccb ordering.  The next
  387                          * ccb that gets completed will unfreeze the simq.
  388                          */
  389                         tw_osli_disallow_new_requests(sc);
  390                         ccb_h->status |= CAM_REQUEUE_REQ;
  391                         tw_osli_complete_ccb(ccb);
  392                         break;
  393                 }
  394                 req->req_handle.osl_req_ctxt = req;
  395                 req->orig_req = ccb;
  396                 if (tw_osli_execute_scsi(req, ccb))
  397                         tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
  398                 break;
  399         }
  400 
  401         case XPT_ABORT:
  402                 tw_osli_dbg_dprintf(2, sc, "Abort request.");
  403                 ccb_h->status = CAM_UA_ABORT;
  404                 tw_osli_complete_ccb(ccb);
  405                 break;
  406 
  407         case XPT_RESET_BUS:
  408                 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
  409                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  410                         0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
  411                         "Received Reset Bus request from CAM",
  412                         " ");
  413 
  414                 if (tw_cl_reset_ctlr(&sc->ctlr_handle)) {
  415                         tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
  416                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  417                                 0x2109, 0x1, TW_CL_SEVERITY_ERROR_STRING,
  418                                 "Failed to reset bus",
  419                                 " ");
  420                         ccb_h->status = CAM_REQ_CMP_ERR;
  421                 }
  422                 else
  423                         ccb_h->status = CAM_REQ_CMP;
  424 
  425                 tw_osli_complete_ccb(ccb);
  426                 break;
  427 
  428         case XPT_SET_TRAN_SETTINGS:
  429                 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
  430 
  431                 /*
  432                  * This command is not supported, since it's very specific
  433                  * to SCSI, and we are doing ATA.
  434                  */
  435                 ccb_h->status = CAM_FUNC_NOTAVAIL;
  436                 tw_osli_complete_ccb(ccb);
  437                 break;
  438 
  439         case XPT_GET_TRAN_SETTINGS: 
  440         {
  441                 struct ccb_trans_settings       *cts = &ccb->cts;
  442 
  443                 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
  444                 cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
  445                 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
  446                 ccb_h->status = CAM_REQ_CMP;
  447                 tw_osli_complete_ccb(ccb);
  448                 break;
  449         }
  450 
  451         case XPT_CALC_GEOMETRY:
  452                 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
  453                 cam_calc_geometry(&ccb->ccg, 1/* extended */);
  454                 tw_osli_complete_ccb(ccb);
  455                 break;
  456 
  457         case XPT_PATH_INQ:    /* Path inquiry -- get twa properties */
  458         {
  459                 struct ccb_pathinq      *path_inq = &ccb->cpi;
  460 
  461                 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
  462 
  463                 path_inq->version_num = 1;
  464                 path_inq->hba_inquiry = 0;
  465                 path_inq->target_sprt = 0;
  466                 path_inq->hba_misc = 0;
  467                 path_inq->hba_eng_cnt = 0;
  468                 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
  469                 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
  470                 path_inq->unit_number = cam_sim_unit(sim);
  471                 path_inq->bus_id = cam_sim_bus(sim);
  472                 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
  473                 path_inq->base_transfer_speed = 100000;
  474                 strncpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
  475                 strncpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
  476                 strncpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
  477                 ccb_h->status = CAM_REQ_CMP;
  478                 tw_osli_complete_ccb(ccb);
  479                 break;
  480         }
  481 
  482         default:
  483                 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
  484                 ccb_h->status = CAM_REQ_INVALID;
  485                 tw_osli_complete_ccb(ccb);
  486                 break;
  487         }
  488 }
  489 
  490 
  491 
  492 /*
  493  * Function name:       twa_poll
  494  * Description:         Driver entry point called when interrupts are not
  495  *                      available.
  496  *
  497  * Input:               sim     -- sim corresponding to the controller
  498  * Output:              None
  499  * Return value:        None
  500  */
  501 TW_VOID
  502 twa_poll(struct cam_sim *sim)
  503 {
  504         struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
  505 
  506         tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
  507         /*
  508          * It's been observed that twa_poll can get called (from
  509          * dashutdown --> xpt_polled_action) even when interrupts are
  510          * active, in which case, the ISR might clear the interrupt,
  511          * leaving the call to tw_cl_interrupt below, no way of determining
  512          * that the response from firmware is ready, resulting in
  513          * tw_cl_deferred_interrupt never getting called.  To cover this case,
  514          * we will make the call to tw_cl_deferred_interrupt not dependent
  515          * on the return value from tw_cl_interrupt.
  516          */
  517         tw_cl_interrupt(&(sc->ctlr_handle));
  518         tw_cl_deferred_interrupt(&(sc->ctlr_handle));
  519         tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
  520 }
  521 
  522 
  523 
  524 /*
  525  * Function name:       twa_async
  526  * Description:         Driver entry point for CAM to notify driver of special
  527  *                      events.  We don't use this for now.
  528  *
  529  * Input:               callback_arg    -- ptr to per ctlr structure
  530  *                      code            -- code associated with the event
  531  *                      path            -- cam path
  532  *                      arg             -- 
  533  * Output:              None
  534  * Return value:        0       -- success
  535  *                      non-zero-- failure
  536  */
  537 TW_VOID
  538 twa_async(TW_VOID *callback_arg, TW_UINT32 code, 
  539         struct cam_path *path, TW_VOID *arg)
  540 {
  541 #ifdef TW_OSL_DEBUG
  542         struct twa_softc *sc = (struct twa_softc *)callback_arg;
  543 #endif /* TW_OSL_DEBUG */
  544 
  545         tw_osli_dbg_dprintf(3, sc, "sc = %p, code = %x, path = %p, arg = %p",
  546                 sc, code, path, arg);
  547 }
  548 
  549 
  550 
  551 /*
  552  * Function name:       twa_timeout
  553  * Description:         Driver entry point for being alerted on a request
  554  *                      timing out.
  555  *
  556  * Input:               arg     -- ptr to timed out request
  557  * Output:              None
  558  * Return value:        None
  559  */
  560 static TW_VOID
  561 twa_timeout(TW_VOID *arg)
  562 {
  563         struct tw_osli_req_context      *req =
  564                 (struct tw_osli_req_context *)arg;
  565 
  566         tw_cl_create_event(&(req->ctlr->ctlr_handle), TW_CL_TRUE,
  567                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  568                 0x210B, 0x1, TW_CL_SEVERITY_ERROR_STRING,
  569                 "Request timed out!",
  570                 "request = %p", req);
  571         tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
  572 }
  573 
  574 
  575 
  576 /*
  577  * Function name:       tw_osli_request_bus_scan
  578  * Description:         Requests CAM for a scan of the bus.
  579  *
  580  * Input:               sc      -- ptr to per ctlr structure
  581  * Output:              None
  582  * Return value:        0       -- success
  583  *                      non-zero-- failure
  584  */
  585 TW_INT32
  586 tw_osli_request_bus_scan(struct twa_softc *sc)
  587 {
  588         struct cam_path *path;
  589         union ccb       *ccb;
  590 
  591         tw_osli_dbg_dprintf(3, sc, "entering");
  592 
  593         /* If we get here before sc->sim is initialized, return an error. */
  594         if (!(sc->sim))
  595                 return(ENXIO);
  596         if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK)) == NULL)
  597                 return(ENOMEM);
  598         bzero(ccb, sizeof(union ccb));
  599         mtx_lock(&Giant);
  600         if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->sim),
  601                 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
  602                 return(EIO);
  603 
  604         xpt_setup_ccb(&ccb->ccb_h, path, 5);
  605         ccb->ccb_h.func_code = XPT_SCAN_BUS;
  606         ccb->ccb_h.cbfcnp = twa_bus_scan_cb;
  607         ccb->crcn.flags = CAM_FLAG_NONE;
  608         xpt_action(ccb);
  609         mtx_unlock(&Giant);
  610         return(0);
  611 }
  612 
  613 
  614 
  615 /*
  616  * Function name:       twa_bus_scan_cb
  617  * Description:         Callback from CAM on a bus scan request.
  618  *
  619  * Input:               periph  -- we don't use this
  620  *                      ccb     -- bus scan request ccb that we sent to CAM
  621  * Output:              None
  622  * Return value:        None
  623  */
  624 static TW_VOID
  625 twa_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
  626 {
  627         tw_osli_dbg_printf(3, "entering");
  628 
  629         if (ccb->ccb_h.status != CAM_REQ_CMP)
  630                 printf("cam_scan_callback: failure status = %x\n",
  631                         ccb->ccb_h.status);
  632         else
  633                 tw_osli_dbg_printf(3, "success");
  634 
  635         xpt_free_path(ccb->ccb_h.path);
  636         free(ccb, M_TEMP);
  637 }
  638 
  639 
  640 
  641 /*
  642  * Function name:       tw_osli_allow_new_requests
  643  * Description:         Sets the appropriate status bits in a ccb such that,
  644  *                      when the ccb is completed by a call to xpt_done,
  645  *                      CAM knows that it's ok to unfreeze the flow of new
  646  *                      requests to this controller, if the flow is frozen.
  647  *
  648  * Input:               sc      -- ptr to OSL internal ctlr context
  649  *                      ccb     -- ptr to CAM request
  650  * Output:              None
  651  * Return value:        None
  652  */
  653 TW_VOID
  654 tw_osli_allow_new_requests(struct twa_softc *sc, TW_VOID *ccb)
  655 {
  656         ((union ccb *)(ccb))->ccb_h.status |= CAM_RELEASE_SIMQ;
  657         sc->state &= ~TW_OSLI_CTLR_STATE_SIMQ_FROZEN;
  658 }
  659 
  660 
  661 
  662 /*
  663  * Function name:       tw_osli_disallow_new_requests
  664  * Description:         Calls the appropriate CAM function, so as to freeze
  665  *                      the flow of new requests from CAM to this controller.
  666  *
  667  * Input:               sc      -- ptr to OSL internal ctlr context
  668  * Output:              None
  669  * Return value:        None
  670  */
  671 TW_VOID
  672 tw_osli_disallow_new_requests(struct twa_softc *sc)
  673 {
  674         mtx_lock(&Giant);
  675         xpt_freeze_simq(sc->sim, 1);
  676         mtx_unlock(&Giant);
  677         sc->state |= TW_OSLI_CTLR_STATE_SIMQ_FROZEN;
  678 }
  679 
  680 
  681 
  682 /*
  683  * Function name:       tw_osl_ctlr_busy
  684  * Description:         CL calls this function on cmd queue full or otherwise,
  685  *                      when it is too busy to accept new requests.
  686  *
  687  * Input:               ctlr_handle     -- ptr to controller handle
  688  *                      req_handle      -- ptr to request handle sent by OSL.
  689  * Output:              None
  690  * Return value:        None
  691  */
  692 TW_VOID
  693 tw_osl_ctlr_busy(struct tw_cl_ctlr_handle *ctlr_handle,
  694         struct tw_cl_req_handle *req_handle)
  695 {
  696         tw_osli_disallow_new_requests(ctlr_handle->osl_ctlr_ctxt);
  697 }
  698 
  699 
  700 
  701 /*
  702  * Function name:       tw_osl_scan_bus
  703  * Description:         CL calls this function to request for a bus scan.
  704  *
  705  * Input:               ctlr_handle     -- ptr to controller handle
  706  * Output:              None
  707  * Return value:        None
  708  */
  709 TW_VOID
  710 tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
  711 {
  712         struct twa_softc        *sc = ctlr_handle->osl_ctlr_ctxt;
  713         TW_INT32                error;
  714 
  715         if ((error = tw_osli_request_bus_scan(sc)))
  716                 tw_osli_printf(sc, "error = %d",
  717                         TW_CL_SEVERITY_ERROR_STRING,
  718                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  719                         0x2109,
  720                         "Bus scan request to CAM failed",
  721                         error);
  722 }
  723 
  724 
  725 
  726 /*
  727  * Function name:       tw_osl_complete_io
  728  * Description:         Called to complete CAM scsi requests.
  729  *
  730  * Input:               req_handle      -- ptr to request handle
  731  * Output:              None
  732  * Return value:        None
  733  */
  734 TW_VOID
  735 tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
  736 {
  737         struct tw_osli_req_context      *req = req_handle->osl_req_ctxt;
  738         struct tw_cl_req_packet         *req_pkt =
  739                 (struct tw_cl_req_packet *)(&req->req_pkt);
  740         struct tw_cl_scsi_req_packet    *scsi_req;
  741         struct twa_softc                *sc = req->ctlr;
  742         union ccb                       *ccb = (union ccb *)(req->orig_req);
  743 
  744         tw_osli_dbg_dprintf(10, sc, "entering");
  745 
  746         if (req->state != TW_OSLI_REQ_STATE_BUSY)
  747                 tw_osli_printf(sc, "request = %p, status = %d",
  748                         TW_CL_SEVERITY_ERROR_STRING,
  749                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  750                         0x210A,
  751                         "Unposted command completed!!",
  752                         req, req->state);
  753 
  754         /*
  755          * Remove request from the busy queue.  Just mark it complete.
  756          * There's no need to move it into the complete queue as we are
  757          * going to be done with it right now.
  758          */
  759         req->state = TW_OSLI_REQ_STATE_COMPLETE;
  760         tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
  761 
  762         tw_osli_unmap_request(req);
  763 
  764         untimeout(twa_timeout, req, ccb->ccb_h.timeout_ch);
  765         if (req->error_code) {
  766                 /* This request never got submitted to the firmware. */
  767                 if (req->error_code == EBUSY) {
  768                         /*
  769                          * Cmd queue is full, or the Common Layer is out of
  770                          * resources.  The simq will already have been frozen
  771                          * by CL's call to tw_osl_ctlr_busy, and this will
  772                          * maintain ccb ordering.  The next ccb that gets
  773                          * completed will unfreeze the simq.
  774                          */
  775                         ccb->ccb_h.status |= CAM_REQUEUE_REQ;
  776                 }
  777                 else if (req->error_code == EFBIG)
  778                         ccb->ccb_h.status = CAM_REQ_TOO_BIG;
  779                 else
  780                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
  781         } else {
  782                 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
  783                 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
  784                         ccb->ccb_h.status = CAM_REQ_CMP;
  785                 else {
  786                         if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
  787                                 ccb->ccb_h.status |= CAM_TID_INVALID;
  788                         else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
  789                                 ccb->ccb_h.status |= CAM_LUN_INVALID;
  790                         else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
  791                                 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
  792                         else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
  793                                 ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
  794                         /*
  795                          * If none of the above errors occurred, simply
  796                          * mark completion error.
  797                          */
  798                         if (ccb->ccb_h.status == 0)
  799                                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
  800 
  801                         if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
  802                                 ccb->csio.sense_len = scsi_req->sense_len;
  803                                 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
  804                         }
  805                 }
  806 
  807                 ccb->csio.scsi_status = scsi_req->scsi_status;
  808                 /* If simq is frozen, unfreeze it. */
  809                 if (sc->state & TW_OSLI_CTLR_STATE_SIMQ_FROZEN)
  810                         tw_osli_allow_new_requests(sc, (TW_VOID *)ccb);
  811         }
  812 
  813         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
  814         tw_osli_complete_ccb(ccb);
  815         if (! req->error_code)
  816                  /* twa_action will free the request otherwise */
  817                 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
  818 }
  819 

Cache object: 8c054be1493410e4313752d2b306c098


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