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_freebsd.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-07 Applied Micro Circuits Corporation.
    3  * Copyright (c) 2004-05 Vinod Kashyap.
    4  * Copyright (c) 2000 Michael Smith
    5  * Copyright (c) 2000 BSDi
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $FreeBSD: releng/9.0/sys/dev/twa/tw_osl_freebsd.c 212210 2010-09-04 16:27:14Z bz $
   30  */
   31 
   32 /*
   33  * AMCC'S 3ware driver for 9000 series storage controllers.
   34  *
   35  * Author: Vinod Kashyap
   36  * Modifications by: Adam Radford
   37  * Modifications by: Manjunath Ranganathaiah
   38  */
   39 
   40 
   41 /*
   42  * FreeBSD specific functions not related to CAM, and other
   43  * miscellaneous functions.
   44  */
   45 
   46 
   47 #include <dev/twa/tw_osl_includes.h>
   48 #include <dev/twa/tw_cl_fwif.h>
   49 #include <dev/twa/tw_cl_ioctl.h>
   50 #include <dev/twa/tw_osl_ioctl.h>
   51 
   52 #ifdef TW_OSL_DEBUG
   53 TW_INT32        TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
   54 TW_INT32        TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
   55 #endif /* TW_OSL_DEBUG */
   56 
   57 MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
   58 
   59 
   60 static  d_open_t                twa_open;
   61 static  d_close_t               twa_close;
   62 static  d_ioctl_t               twa_ioctl;
   63 
   64 static struct cdevsw twa_cdevsw = {
   65         .d_version =    D_VERSION,
   66         .d_open =       twa_open,
   67         .d_close =      twa_close,
   68         .d_ioctl =      twa_ioctl,
   69         .d_name =       "twa",
   70 };
   71 
   72 static devclass_t       twa_devclass;
   73 
   74 
   75 /*
   76  * Function name:       twa_open
   77  * Description:         Called when the controller is opened.
   78  *                      Simply marks the controller as open.
   79  *
   80  * Input:               dev     -- control device corresponding to the ctlr
   81  *                      flags   -- mode of open
   82  *                      fmt     -- device type (character/block etc.)
   83  *                      proc    -- current process
   84  * Output:              None
   85  * Return value:        0       -- success
   86  *                      non-zero-- failure
   87  */
   88 static TW_INT32
   89 twa_open(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
   90 {
   91         struct twa_softc        *sc = (struct twa_softc *)(dev->si_drv1);
   92 
   93         tw_osli_dbg_dprintf(5, sc, "entered");
   94         sc->open = TW_CL_TRUE;
   95         return(0);
   96 }
   97 
   98 
   99 
  100 /*
  101  * Function name:       twa_close
  102  * Description:         Called when the controller is closed.
  103  *                      Simply marks the controller as not open.
  104  *
  105  * Input:               dev     -- control device corresponding to the ctlr
  106  *                      flags   -- mode of corresponding open
  107  *                      fmt     -- device type (character/block etc.)
  108  *                      proc    -- current process
  109  * Output:              None
  110  * Return value:        0       -- success
  111  *                      non-zero-- failure
  112  */
  113 static TW_INT32
  114 twa_close(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
  115 {
  116         struct twa_softc        *sc = (struct twa_softc *)(dev->si_drv1);
  117 
  118         tw_osli_dbg_dprintf(5, sc, "entered");
  119         sc->open = TW_CL_FALSE;
  120         return(0);
  121 }
  122 
  123 
  124 
  125 /*
  126  * Function name:       twa_ioctl
  127  * Description:         Called when an ioctl is posted to the controller.
  128  *                      Handles any OS Layer specific cmds, passes the rest
  129  *                      on to the Common Layer.
  130  *
  131  * Input:               dev     -- control device corresponding to the ctlr
  132  *                      cmd     -- ioctl cmd
  133  *                      buf     -- ptr to buffer in kernel memory, which is
  134  *                                 a copy of the input buffer in user-space
  135  *                      flags   -- mode of corresponding open
  136  *                      proc    -- current process
  137  * Output:              buf     -- ptr to buffer in kernel memory, which will
  138  *                                 be copied to the output buffer in user-space
  139  * Return value:        0       -- success
  140  *                      non-zero-- failure
  141  */
  142 static TW_INT32
  143 twa_ioctl(struct cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, struct thread *proc)
  144 {
  145         struct twa_softc        *sc = (struct twa_softc *)(dev->si_drv1);
  146         TW_INT32                error;
  147 
  148         tw_osli_dbg_dprintf(5, sc, "entered");
  149 
  150         switch (cmd) {
  151         case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
  152                 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
  153                 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
  154                 break;
  155 
  156         case TW_OSL_IOCTL_SCAN_BUS:
  157                 /* Request CAM for a bus scan. */
  158                 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
  159                 error = tw_osli_request_bus_scan(sc);
  160                 break;
  161 
  162         default:
  163                 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
  164                 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
  165                 break;
  166         }
  167         return(error);
  168 }
  169 
  170 
  171 
  172 static TW_INT32 twa_probe(device_t dev);
  173 static TW_INT32 twa_attach(device_t dev);
  174 static TW_INT32 twa_detach(device_t dev);
  175 static TW_INT32 twa_shutdown(device_t dev);
  176 static TW_VOID  twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
  177 static TW_VOID  twa_pci_intr(TW_VOID *arg);
  178 static TW_VOID  twa_watchdog(TW_VOID *arg);
  179 int twa_setup_intr(struct twa_softc *sc);
  180 int twa_teardown_intr(struct twa_softc *sc);
  181 
  182 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
  183 static TW_VOID  tw_osli_free_resources(struct twa_softc *sc);
  184 
  185 static TW_VOID  twa_map_load_data_callback(TW_VOID *arg,
  186         bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
  187 static TW_VOID  twa_map_load_callback(TW_VOID *arg,
  188         bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
  189 
  190 
  191 static device_method_t  twa_methods[] = {
  192         /* Device interface */
  193         DEVMETHOD(device_probe,         twa_probe),
  194         DEVMETHOD(device_attach,        twa_attach),
  195         DEVMETHOD(device_detach,        twa_detach),
  196         DEVMETHOD(device_shutdown,      twa_shutdown),
  197 
  198         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  199         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
  200         {0, 0}
  201 };
  202 
  203 static driver_t twa_pci_driver = {
  204         "twa",
  205         twa_methods,
  206         sizeof(struct twa_softc)
  207 };
  208 
  209 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
  210 MODULE_DEPEND(twa, cam, 1, 1, 1);
  211 MODULE_DEPEND(twa, pci, 1, 1, 1);
  212 
  213 
  214 /*
  215  * Function name:       twa_probe
  216  * Description:         Called at driver load time.  Claims 9000 ctlrs.
  217  *
  218  * Input:               dev     -- bus device corresponding to the ctlr
  219  * Output:              None
  220  * Return value:        <= 0    -- success
  221  *                      > 0     -- failure
  222  */
  223 static TW_INT32
  224 twa_probe(device_t dev)
  225 {
  226         static TW_UINT8 first_ctlr = 1;
  227 
  228         tw_osli_dbg_printf(3, "entered");
  229 
  230         if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
  231                 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
  232                 /* Print the driver version only once. */
  233                 if (first_ctlr) {
  234                         printf("3ware device driver for 9000 series storage "
  235                                 "controllers, version: %s\n",
  236                                 TW_OSL_DRIVER_VERSION_STRING);
  237                         first_ctlr = 0;
  238                 }
  239                 return(0);
  240         }
  241         return(ENXIO);
  242 }
  243 
  244 int twa_setup_intr(struct twa_softc *sc)
  245 {
  246         int error = 0;
  247 
  248         if (!(sc->intr_handle) && (sc->irq_res)) {
  249                 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
  250                                         INTR_TYPE_CAM | INTR_MPSAFE,
  251                                         NULL, twa_pci_intr,
  252                                         sc, &sc->intr_handle);
  253         }
  254         return( error );
  255 }
  256 
  257 
  258 int twa_teardown_intr(struct twa_softc *sc)
  259 {
  260         int error = 0;
  261 
  262         if ((sc->intr_handle) && (sc->irq_res)) {
  263                 error = bus_teardown_intr(sc->bus_dev,
  264                                                 sc->irq_res, sc->intr_handle);
  265                 sc->intr_handle = NULL;
  266         }
  267         return( error );
  268 }
  269 
  270 
  271 
  272 /*
  273  * Function name:       twa_attach
  274  * Description:         Allocates pci resources; updates sc; adds a node to the
  275  *                      sysctl tree to expose the driver version; makes calls
  276  *                      (to the Common Layer) to initialize ctlr, and to
  277  *                      attach to CAM.
  278  *
  279  * Input:               dev     -- bus device corresponding to the ctlr
  280  * Output:              None
  281  * Return value:        0       -- success
  282  *                      non-zero-- failure
  283  */
  284 static TW_INT32
  285 twa_attach(device_t dev)
  286 {
  287         struct twa_softc        *sc = device_get_softc(dev);
  288         TW_UINT32               command;
  289         TW_INT32                bar_num;
  290         TW_INT32                bar0_offset;
  291         TW_INT32                bar_size;
  292         TW_INT32                error;
  293 
  294         tw_osli_dbg_dprintf(3, sc, "entered");
  295 
  296         sc->ctlr_handle.osl_ctlr_ctxt = sc;
  297 
  298         /* Initialize the softc structure. */
  299         sc->bus_dev = dev;
  300         sc->device_id = pci_get_device(dev);
  301 
  302         /* Initialize the mutexes right here. */
  303         sc->io_lock = &(sc->io_lock_handle);
  304         mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
  305         sc->q_lock = &(sc->q_lock_handle);
  306         mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
  307         sc->sim_lock = &(sc->sim_lock_handle);
  308         mtx_init(sc->sim_lock, "tw_osl_sim_lock", NULL, MTX_DEF | MTX_RECURSE);
  309 
  310         sysctl_ctx_init(&sc->sysctl_ctxt);
  311         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
  312                 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
  313                 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
  314         if (sc->sysctl_tree == NULL) {
  315                 tw_osli_printf(sc, "error = %d",
  316                         TW_CL_SEVERITY_ERROR_STRING,
  317                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  318                         0x2000,
  319                         "Cannot add sysctl tree node",
  320                         ENXIO);
  321                 return(ENXIO);
  322         }
  323         SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
  324                 OID_AUTO, "driver_version", CTLFLAG_RD,
  325                 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
  326 
  327         /* Make sure we are going to be able to talk to this board. */
  328         command = pci_read_config(dev, PCIR_COMMAND, 2);
  329         if ((command & PCIM_CMD_PORTEN) == 0) {
  330                 tw_osli_printf(sc, "error = %d",
  331                         TW_CL_SEVERITY_ERROR_STRING,
  332                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  333                         0x2001,
  334                         "Register window not available",
  335                         ENXIO);
  336                 tw_osli_free_resources(sc);
  337                 return(ENXIO);
  338         }
  339 
  340         /* Force the busmaster enable bit on, in case the BIOS forgot. */
  341         command |= PCIM_CMD_BUSMASTEREN;
  342         pci_write_config(dev, PCIR_COMMAND, command, 2);
  343 
  344         /* Allocate the PCI register window. */
  345         if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
  346                 &bar_num, &bar0_offset, &bar_size))) {
  347                 tw_osli_printf(sc, "error = %d",
  348                         TW_CL_SEVERITY_ERROR_STRING,
  349                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  350                         0x201F,
  351                         "Can't get PCI BAR info",
  352                         error);
  353                 tw_osli_free_resources(sc);
  354                 return(error);
  355         }
  356         sc->reg_res_id = PCIR_BARS + bar0_offset;
  357         if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
  358                                 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
  359                                 == NULL) {
  360                 tw_osli_printf(sc, "error = %d",
  361                         TW_CL_SEVERITY_ERROR_STRING,
  362                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  363                         0x2002,
  364                         "Can't allocate register window",
  365                         ENXIO);
  366                 tw_osli_free_resources(sc);
  367                 return(ENXIO);
  368         }
  369         sc->bus_tag = rman_get_bustag(sc->reg_res);
  370         sc->bus_handle = rman_get_bushandle(sc->reg_res);
  371 
  372         /* Allocate and register our interrupt. */
  373         sc->irq_res_id = 0;
  374         if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
  375                                 &(sc->irq_res_id), 0, ~0, 1,
  376                                 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
  377                 tw_osli_printf(sc, "error = %d",
  378                         TW_CL_SEVERITY_ERROR_STRING,
  379                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  380                         0x2003,
  381                         "Can't allocate interrupt",
  382                         ENXIO);
  383                 tw_osli_free_resources(sc);
  384                 return(ENXIO);
  385         }
  386         if ((error = twa_setup_intr(sc))) {
  387                 tw_osli_printf(sc, "error = %d",
  388                         TW_CL_SEVERITY_ERROR_STRING,
  389                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  390                         0x2004,
  391                         "Can't set up interrupt",
  392                         error);
  393                 tw_osli_free_resources(sc);
  394                 return(error);
  395         }
  396 
  397         if ((error = tw_osli_alloc_mem(sc))) {
  398                 tw_osli_printf(sc, "error = %d",
  399                         TW_CL_SEVERITY_ERROR_STRING,
  400                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  401                         0x2005,
  402                         "Memory allocation failure",
  403                         error);
  404                 tw_osli_free_resources(sc);
  405                 return(error);
  406         }
  407 
  408         /* Initialize the Common Layer for this controller. */
  409         if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
  410                         TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
  411                         sc->non_dma_mem, sc->dma_mem,
  412                         sc->dma_mem_phys
  413                         ))) {
  414                 tw_osli_printf(sc, "error = %d",
  415                         TW_CL_SEVERITY_ERROR_STRING,
  416                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  417                         0x2006,
  418                         "Failed to initialize Common Layer/controller",
  419                         error);
  420                 tw_osli_free_resources(sc);
  421                 return(error);
  422         }
  423 
  424         /* Create the control device. */
  425         sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
  426                         UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
  427                         "twa%d", device_get_unit(sc->bus_dev));
  428         sc->ctrl_dev->si_drv1 = sc;
  429 
  430         if ((error = tw_osli_cam_attach(sc))) {
  431                 tw_osli_free_resources(sc);
  432                 tw_osli_printf(sc, "error = %d",
  433                         TW_CL_SEVERITY_ERROR_STRING,
  434                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  435                         0x2007,
  436                         "Failed to initialize CAM",
  437                         error);
  438                 return(error);
  439         }
  440 
  441         sc->watchdog_index = 0;
  442         callout_init(&(sc->watchdog_callout[0]), CALLOUT_MPSAFE);
  443         callout_init(&(sc->watchdog_callout[1]), CALLOUT_MPSAFE);
  444         callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
  445 
  446         return(0);
  447 }
  448 
  449 
  450 static TW_VOID
  451 twa_watchdog(TW_VOID *arg)
  452 {
  453         struct tw_cl_ctlr_handle *ctlr_handle =
  454                 (struct tw_cl_ctlr_handle *)arg;
  455         struct twa_softc                *sc = ctlr_handle->osl_ctlr_ctxt;
  456         int                             i;
  457         int                             i_need_a_reset = 0;
  458         int                             driver_is_active = 0;
  459         int                             my_watchdog_was_pending = 1234;
  460         TW_UINT64                       current_time;
  461         struct tw_osli_req_context      *my_req;
  462 
  463 
  464 //==============================================================================
  465         current_time = (TW_UINT64) (tw_osl_get_local_time());
  466 
  467         for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
  468                 my_req = &(sc->req_ctx_buf[i]);
  469 
  470                 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
  471                         (my_req->deadline) &&
  472                         (my_req->deadline < current_time)) {
  473                         tw_cl_set_reset_needed(ctlr_handle);
  474 #ifdef    TW_OSL_DEBUG
  475                         device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
  476 #else  /* TW_OSL_DEBUG */
  477                         device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
  478 #endif /* TW_OSL_DEBUG */
  479                         break;
  480                 }
  481         }
  482 //==============================================================================
  483 
  484         i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
  485 
  486         i = (int) ((sc->watchdog_index++) & 1);
  487 
  488         driver_is_active = tw_cl_is_active(ctlr_handle);
  489 
  490         if (i_need_a_reset) {
  491 #ifdef    TW_OSL_DEBUG
  492                 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
  493 #endif /* TW_OSL_DEBUG */
  494                 my_watchdog_was_pending =
  495                         callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
  496                 tw_cl_reset_ctlr(ctlr_handle);
  497 #ifdef    TW_OSL_DEBUG
  498                 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
  499 #endif /* TW_OSL_DEBUG */
  500         } else if (driver_is_active) {
  501                 my_watchdog_was_pending =
  502                         callout_reset(&(sc->watchdog_callout[i]),  5*hz, twa_watchdog, &sc->ctlr_handle);
  503         }
  504 #ifdef    TW_OSL_DEBUG
  505         if (i_need_a_reset || my_watchdog_was_pending)
  506                 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
  507                 "driver_is_active = %d, my_watchdog_was_pending = %d\n",
  508                 i_need_a_reset, driver_is_active, my_watchdog_was_pending);
  509 #endif /* TW_OSL_DEBUG */
  510 }
  511 
  512 
  513 /*
  514  * Function name:       tw_osli_alloc_mem
  515  * Description:         Allocates memory needed both by CL and OSL.
  516  *
  517  * Input:               sc      -- OSL internal controller context
  518  * Output:              None
  519  * Return value:        0       -- success
  520  *                      non-zero-- failure
  521  */
  522 static TW_INT32
  523 tw_osli_alloc_mem(struct twa_softc *sc)
  524 {
  525         struct tw_osli_req_context      *req;
  526         TW_UINT32                       max_sg_elements;
  527         TW_UINT32                       non_dma_mem_size;
  528         TW_UINT32                       dma_mem_size;
  529         TW_INT32                        error;
  530         TW_INT32                        i;
  531 
  532         tw_osli_dbg_dprintf(3, sc, "entered");
  533 
  534         sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
  535         sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
  536 
  537         max_sg_elements = (sizeof(bus_addr_t) == 8) ?
  538                 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
  539 
  540         if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
  541                         sc->device_id, TW_OSLI_MAX_NUM_REQUESTS,  TW_OSLI_MAX_NUM_AENS,
  542                         &(sc->alignment), &(sc->sg_size_factor),
  543                         &non_dma_mem_size, &dma_mem_size
  544                         ))) {
  545                 tw_osli_printf(sc, "error = %d",
  546                         TW_CL_SEVERITY_ERROR_STRING,
  547                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  548                         0x2008,
  549                         "Can't get Common Layer's memory requirements",
  550                         error);
  551                 return(error);
  552         }
  553 
  554         if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
  555                                 M_WAITOK)) == NULL) {
  556                 tw_osli_printf(sc, "error = %d",
  557                         TW_CL_SEVERITY_ERROR_STRING,
  558                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  559                         0x2009,
  560                         "Can't allocate non-dma memory",
  561                         ENOMEM);
  562                 return(ENOMEM);
  563         }
  564 
  565         /* Create the parent dma tag. */
  566         if (bus_dma_tag_create(NULL,                    /* parent */
  567                                 sc->alignment,          /* alignment */
  568                                 TW_OSLI_DMA_BOUNDARY,   /* boundary */
  569                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  570                                 BUS_SPACE_MAXADDR,      /* highaddr */
  571                                 NULL, NULL,             /* filter, filterarg */
  572                                 TW_CL_MAX_IO_SIZE,      /* maxsize */
  573                                 max_sg_elements,        /* nsegments */
  574                                 TW_CL_MAX_IO_SIZE,      /* maxsegsize */
  575                                 0,                      /* flags */
  576                                 NULL,                   /* lockfunc */
  577                                 NULL,                   /* lockfuncarg */
  578                                 &sc->parent_tag         /* tag */)) {
  579                 tw_osli_printf(sc, "error = %d",
  580                         TW_CL_SEVERITY_ERROR_STRING,
  581                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  582                         0x200A,
  583                         "Can't allocate parent DMA tag",
  584                         ENOMEM);
  585                 return(ENOMEM);
  586         }
  587 
  588         /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
  589         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  590                                 sc->alignment,          /* alignment */
  591                                 0,                      /* boundary */
  592                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  593                                 BUS_SPACE_MAXADDR,      /* highaddr */
  594                                 NULL, NULL,             /* filter, filterarg */
  595                                 dma_mem_size,           /* maxsize */
  596                                 1,                      /* nsegments */
  597                                 BUS_SPACE_MAXSIZE,      /* maxsegsize */
  598                                 0,                      /* flags */
  599                                 NULL,                   /* lockfunc */
  600                                 NULL,                   /* lockfuncarg */
  601                                 &sc->cmd_tag            /* tag */)) {
  602                 tw_osli_printf(sc, "error = %d",
  603                         TW_CL_SEVERITY_ERROR_STRING,
  604                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  605                         0x200B,
  606                         "Can't allocate DMA tag for Common Layer's "
  607                         "DMA'able memory",
  608                         ENOMEM);
  609                 return(ENOMEM);
  610         }
  611 
  612         if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
  613                 BUS_DMA_NOWAIT, &sc->cmd_map)) {
  614                 /* Try a second time. */
  615                 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
  616                         BUS_DMA_NOWAIT, &sc->cmd_map)) {
  617                         tw_osli_printf(sc, "error = %d",
  618                                 TW_CL_SEVERITY_ERROR_STRING,
  619                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  620                                 0x200C,
  621                                 "Can't allocate DMA'able memory for the"
  622                                 "Common Layer",
  623                                 ENOMEM);
  624                         return(ENOMEM);
  625                 }
  626         }
  627 
  628         bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
  629                 dma_mem_size, twa_map_load_callback,
  630                 &sc->dma_mem_phys, 0);
  631 
  632         /*
  633          * Create a dma tag for data buffers; size will be the maximum
  634          * possible I/O size (128kB).
  635          */
  636         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  637                                 sc->alignment,          /* alignment */
  638                                 0,                      /* boundary */
  639                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  640                                 BUS_SPACE_MAXADDR,      /* highaddr */
  641                                 NULL, NULL,             /* filter, filterarg */
  642                                 TW_CL_MAX_IO_SIZE,      /* maxsize */
  643                                 max_sg_elements,        /* nsegments */
  644                                 TW_CL_MAX_IO_SIZE,      /* maxsegsize */
  645                                 BUS_DMA_ALLOCNOW,       /* flags */
  646                                 twa_busdma_lock,        /* lockfunc */
  647                                 sc->io_lock,            /* lockfuncarg */
  648                                 &sc->dma_tag            /* tag */)) {
  649                 tw_osli_printf(sc, "error = %d",
  650                         TW_CL_SEVERITY_ERROR_STRING,
  651                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  652                         0x200F,
  653                         "Can't allocate DMA tag for data buffers",
  654                         ENOMEM);
  655                 return(ENOMEM);
  656         }
  657 
  658         /*
  659          * Create a dma tag for ioctl data buffers; size will be the maximum
  660          * possible I/O size (128kB).
  661          */
  662         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  663                                 sc->alignment,          /* alignment */
  664                                 0,                      /* boundary */
  665                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  666                                 BUS_SPACE_MAXADDR,      /* highaddr */
  667                                 NULL, NULL,             /* filter, filterarg */
  668                                 TW_CL_MAX_IO_SIZE,      /* maxsize */
  669                                 max_sg_elements,        /* nsegments */
  670                                 TW_CL_MAX_IO_SIZE,      /* maxsegsize */
  671                                 BUS_DMA_ALLOCNOW,       /* flags */
  672                                 twa_busdma_lock,        /* lockfunc */
  673                                 sc->io_lock,            /* lockfuncarg */
  674                                 &sc->ioctl_tag          /* tag */)) {
  675                 tw_osli_printf(sc, "error = %d",
  676                         TW_CL_SEVERITY_ERROR_STRING,
  677                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  678                         0x2010,
  679                         "Can't allocate DMA tag for ioctl data buffers",
  680                         ENOMEM);
  681                 return(ENOMEM);
  682         }
  683 
  684         /* Create just one map for all ioctl request data buffers. */
  685         if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
  686                 tw_osli_printf(sc, "error = %d",
  687                         TW_CL_SEVERITY_ERROR_STRING,
  688                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  689                         0x2011,
  690                         "Can't create ioctl map",
  691                         ENOMEM);
  692                 return(ENOMEM);
  693         }
  694 
  695 
  696         /* Initialize request queues. */
  697         tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
  698         tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
  699 
  700         if ((sc->req_ctx_buf = (struct tw_osli_req_context *)
  701                         malloc((sizeof(struct tw_osli_req_context) *
  702                                 TW_OSLI_MAX_NUM_REQUESTS),
  703                                 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
  704                 tw_osli_printf(sc, "error = %d",
  705                         TW_CL_SEVERITY_ERROR_STRING,
  706                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  707                         0x2012,
  708                         "Failed to allocate request packets",
  709                         ENOMEM);
  710                 return(ENOMEM);
  711         }
  712         bzero(sc->req_ctx_buf,
  713                 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_REQUESTS);
  714 
  715         for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
  716                 req = &(sc->req_ctx_buf[i]);
  717                 req->ctlr = sc;
  718                 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
  719                         tw_osli_printf(sc, "request # = %d, error = %d",
  720                                 TW_CL_SEVERITY_ERROR_STRING,
  721                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  722                                 0x2013,
  723                                 "Can't create dma map",
  724                                 i, ENOMEM);
  725                         return(ENOMEM);
  726                 }
  727 
  728                 /* Initialize the ioctl wakeup/ timeout mutex */
  729                 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
  730                 mtx_init(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", NULL, MTX_DEF);
  731 
  732                 /* Insert request into the free queue. */
  733                 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
  734         }
  735 
  736         return(0);
  737 }
  738 
  739 
  740 
  741 /*
  742  * Function name:       tw_osli_free_resources
  743  * Description:         Performs clean-up at the time of going down.
  744  *
  745  * Input:               sc      -- ptr to OSL internal ctlr context
  746  * Output:              None
  747  * Return value:        None
  748  */
  749 static TW_VOID
  750 tw_osli_free_resources(struct twa_softc *sc)
  751 {
  752         struct tw_osli_req_context      *req;
  753         TW_INT32                        error = 0;
  754 
  755         tw_osli_dbg_dprintf(3, sc, "entered");
  756 
  757         /* Detach from CAM */
  758         tw_osli_cam_detach(sc);
  759 
  760         if (sc->req_ctx_buf)
  761                 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
  762                         NULL) {
  763                         mtx_destroy(req->ioctl_wake_timeout_lock);
  764 
  765                         if ((error = bus_dmamap_destroy(sc->dma_tag,
  766                                         req->dma_map)))
  767                                 tw_osli_dbg_dprintf(1, sc,
  768                                         "dmamap_destroy(dma) returned %d",
  769                                         error);
  770                 }
  771 
  772         if ((sc->ioctl_tag) && (sc->ioctl_map))
  773                 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
  774                         tw_osli_dbg_dprintf(1, sc,
  775                                 "dmamap_destroy(ioctl) returned %d", error);
  776 
  777         /* Free all memory allocated so far. */
  778         if (sc->req_ctx_buf)
  779                 free(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
  780 
  781         if (sc->non_dma_mem)
  782                 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
  783 
  784         if (sc->dma_mem) {
  785                 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
  786                 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
  787                         sc->cmd_map);
  788         }
  789         if (sc->cmd_tag)
  790                 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
  791                         tw_osli_dbg_dprintf(1, sc,
  792                                 "dma_tag_destroy(cmd) returned %d", error);
  793 
  794         if (sc->dma_tag)
  795                 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
  796                         tw_osli_dbg_dprintf(1, sc,
  797                                 "dma_tag_destroy(dma) returned %d", error);
  798 
  799         if (sc->ioctl_tag)
  800                 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
  801                         tw_osli_dbg_dprintf(1, sc,
  802                                 "dma_tag_destroy(ioctl) returned %d", error);
  803 
  804         if (sc->parent_tag)
  805                 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
  806                         tw_osli_dbg_dprintf(1, sc,
  807                                 "dma_tag_destroy(parent) returned %d", error);
  808 
  809 
  810         /* Disconnect the interrupt handler. */
  811         if ((error = twa_teardown_intr(sc)))
  812                         tw_osli_dbg_dprintf(1, sc,
  813                                 "teardown_intr returned %d", error);
  814 
  815         if (sc->irq_res != NULL)
  816                 if ((error = bus_release_resource(sc->bus_dev,
  817                                 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
  818                         tw_osli_dbg_dprintf(1, sc,
  819                                 "release_resource(irq) returned %d", error);
  820 
  821 
  822         /* Release the register window mapping. */
  823         if (sc->reg_res != NULL)
  824                 if ((error = bus_release_resource(sc->bus_dev,
  825                                 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
  826                         tw_osli_dbg_dprintf(1, sc,
  827                                 "release_resource(io) returned %d", error);
  828 
  829 
  830         /* Destroy the control device. */
  831         if (sc->ctrl_dev != (struct cdev *)NULL)
  832                 destroy_dev(sc->ctrl_dev);
  833 
  834         if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
  835                 tw_osli_dbg_dprintf(1, sc,
  836                         "sysctl_ctx_free returned %d", error);
  837 
  838 }
  839 
  840 
  841 
  842 /*
  843  * Function name:       twa_detach
  844  * Description:         Called when the controller is being detached from
  845  *                      the pci bus.
  846  *
  847  * Input:               dev     -- bus device corresponding to the ctlr
  848  * Output:              None
  849  * Return value:        0       -- success
  850  *                      non-zero-- failure
  851  */
  852 static TW_INT32
  853 twa_detach(device_t dev)
  854 {
  855         struct twa_softc        *sc = device_get_softc(dev);
  856         TW_INT32                error;
  857 
  858         tw_osli_dbg_dprintf(3, sc, "entered");
  859 
  860         error = EBUSY;
  861         if (sc->open) {
  862                 tw_osli_printf(sc, "error = %d",
  863                         TW_CL_SEVERITY_ERROR_STRING,
  864                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  865                         0x2014,
  866                         "Device open",
  867                         error);
  868                 goto out;
  869         }
  870 
  871         /* Shut the controller down. */
  872         if ((error = twa_shutdown(dev)))
  873                 goto out;
  874 
  875         /* Free all resources associated with this controller. */
  876         tw_osli_free_resources(sc);
  877         error = 0;
  878 
  879 out:
  880         return(error);
  881 }
  882 
  883 
  884 
  885 /*
  886  * Function name:       twa_shutdown
  887  * Description:         Called at unload/shutdown time.  Lets the controller
  888  *                      know that we are going down.
  889  *
  890  * Input:               dev     -- bus device corresponding to the ctlr
  891  * Output:              None
  892  * Return value:        0       -- success
  893  *                      non-zero-- failure
  894  */
  895 static TW_INT32
  896 twa_shutdown(device_t dev)
  897 {
  898         struct twa_softc        *sc = device_get_softc(dev);
  899         TW_INT32                error = 0;
  900 
  901         tw_osli_dbg_dprintf(3, sc, "entered");
  902 
  903         /* Disconnect interrupts. */
  904         error = twa_teardown_intr(sc);
  905 
  906         /* Stop watchdog task. */
  907         callout_drain(&(sc->watchdog_callout[0]));
  908         callout_drain(&(sc->watchdog_callout[1]));
  909 
  910         /* Disconnect from the controller. */
  911         if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
  912                 tw_osli_printf(sc, "error = %d",
  913                         TW_CL_SEVERITY_ERROR_STRING,
  914                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  915                         0x2015,
  916                         "Failed to shutdown Common Layer/controller",
  917                         error);
  918         }
  919         return(error);
  920 }
  921 
  922 
  923 
  924 /*
  925  * Function name:       twa_busdma_lock
  926  * Description:         Function to provide synchronization during busdma_swi.
  927  *
  928  * Input:               lock_arg -- lock mutex sent as argument
  929  *                      op -- operation (lock/unlock) expected of the function
  930  * Output:              None
  931  * Return value:        None
  932  */
  933 TW_VOID
  934 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
  935 {
  936         struct mtx      *lock;
  937 
  938         lock = (struct mtx *)lock_arg;
  939         switch (op) {
  940         case BUS_DMA_LOCK:
  941                 mtx_lock_spin(lock);
  942                 break;
  943 
  944         case BUS_DMA_UNLOCK:
  945                 mtx_unlock_spin(lock);
  946                 break;
  947 
  948         default:
  949                 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
  950         }
  951 }
  952 
  953 
  954 /*
  955  * Function name:       twa_pci_intr
  956  * Description:         Interrupt handler.  Wrapper for twa_interrupt.
  957  *
  958  * Input:               arg     -- ptr to OSL internal ctlr context
  959  * Output:              None
  960  * Return value:        None
  961  */
  962 static TW_VOID
  963 twa_pci_intr(TW_VOID *arg)
  964 {
  965         struct twa_softc        *sc = (struct twa_softc *)arg;
  966 
  967         tw_osli_dbg_dprintf(10, sc, "entered");
  968         tw_cl_interrupt(&(sc->ctlr_handle));
  969 }
  970 
  971 
  972 /*
  973  * Function name:       tw_osli_fw_passthru
  974  * Description:         Builds a fw passthru cmd pkt, and submits it to CL.
  975  *
  976  * Input:               sc      -- ptr to OSL internal ctlr context
  977  *                      buf     -- ptr to ioctl pkt understood by CL
  978  * Output:              None
  979  * Return value:        0       -- success
  980  *                      non-zero-- failure
  981  */
  982 TW_INT32
  983 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
  984 {
  985         struct tw_osli_req_context              *req;
  986         struct tw_osli_ioctl_no_data_buf        *user_buf =
  987                 (struct tw_osli_ioctl_no_data_buf *)buf;
  988         TW_TIME                                 end_time;
  989         TW_UINT32                               timeout = 60;
  990         TW_UINT32                               data_buf_size_adjusted;
  991         struct tw_cl_req_packet                 *req_pkt;
  992         struct tw_cl_passthru_req_packet        *pt_req;
  993         TW_INT32                                error;
  994 
  995         tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
  996                 
  997         if ((req = tw_osli_get_request(sc)) == NULL)
  998                 return(EBUSY);
  999 
 1000         req->req_handle.osl_req_ctxt = req;
 1001         req->orig_req = buf;
 1002         req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
 1003 
 1004         req_pkt = &(req->req_pkt);
 1005         req_pkt->status = 0;
 1006         req_pkt->tw_osl_callback = tw_osl_complete_passthru;
 1007         /* Let the Common Layer retry the request on cmd queue full. */
 1008         req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
 1009 
 1010         pt_req = &(req_pkt->gen_req_pkt.pt_req);
 1011         /*
 1012          * Make sure that the data buffer sent to firmware is a 
 1013          * 512 byte multiple in size.
 1014          */
 1015         data_buf_size_adjusted =
 1016                 (user_buf->driver_pkt.buffer_length +
 1017                 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
 1018         if ((req->length = data_buf_size_adjusted)) {
 1019                 if ((req->data = malloc(data_buf_size_adjusted,
 1020                         TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
 1021                         error = ENOMEM;
 1022                         tw_osli_printf(sc, "error = %d",
 1023                                 TW_CL_SEVERITY_ERROR_STRING,
 1024                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1025                                 0x2016,
 1026                                 "Could not alloc mem for "
 1027                                 "fw_passthru data_buf",
 1028                                 error);
 1029                         goto fw_passthru_err;
 1030                 }
 1031                 /* Copy the payload. */
 1032                 if ((error = copyin((TW_VOID *)(user_buf->pdata), 
 1033                         req->data,
 1034                         user_buf->driver_pkt.buffer_length)) != 0) {
 1035                         tw_osli_printf(sc, "error = %d",
 1036                                 TW_CL_SEVERITY_ERROR_STRING,
 1037                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1038                                 0x2017,
 1039                                 "Could not copyin fw_passthru data_buf",
 1040                                 error);
 1041                         goto fw_passthru_err;
 1042                 }
 1043                 pt_req->sgl_entries = 1; /* will be updated during mapping */
 1044                 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
 1045                         TW_OSLI_REQ_FLAGS_DATA_OUT);
 1046         } else
 1047                 pt_req->sgl_entries = 0; /* no payload */
 1048 
 1049         pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
 1050         pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
 1051 
 1052         if ((error = tw_osli_map_request(req)))
 1053                 goto fw_passthru_err;
 1054 
 1055         end_time = tw_osl_get_local_time() + timeout;
 1056         while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
 1057                 mtx_lock(req->ioctl_wake_timeout_lock);
 1058                 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
 1059 
 1060                 error = mtx_sleep(req, req->ioctl_wake_timeout_lock, 0,
 1061                             "twa_passthru", timeout*hz);
 1062                 mtx_unlock(req->ioctl_wake_timeout_lock);
 1063 
 1064                 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
 1065                         error = 0;
 1066                 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
 1067 
 1068                 if (! error) {
 1069                         if (((error = req->error_code)) ||
 1070                                 ((error = (req->state !=
 1071                                 TW_OSLI_REQ_STATE_COMPLETE))) ||
 1072                                 ((error = req_pkt->status)))
 1073                                 goto fw_passthru_err;
 1074                         break;
 1075                 }
 1076 
 1077                 if (req_pkt->status) {
 1078                         error = req_pkt->status;
 1079                         goto fw_passthru_err;
 1080                 }
 1081 
 1082                 if (error == EWOULDBLOCK) {
 1083                         /* Time out! */
 1084                         if ((!(req->error_code))                       &&
 1085                             (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
 1086                             (!(req_pkt->status))                          ) {
 1087 #ifdef    TW_OSL_DEBUG
 1088                                 tw_osli_printf(sc, "request = %p",
 1089                                         TW_CL_SEVERITY_ERROR_STRING,
 1090                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1091                                         0x7777,
 1092                                         "FALSE Passthru timeout!",
 1093                                         req);
 1094 #endif /* TW_OSL_DEBUG */
 1095                                 error = 0; /* False error */
 1096                                 break;
 1097                         }
 1098                         if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
 1099 #ifdef    TW_OSL_DEBUG
 1100                                 tw_osli_printf(sc, "request = %p",
 1101                                         TW_CL_SEVERITY_ERROR_STRING,
 1102                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1103                                         0x2018,
 1104                                         "Passthru request timed out!",
 1105                                         req);
 1106 #else  /* TW_OSL_DEBUG */
 1107                         device_printf((sc)->bus_dev, "Passthru request timed out!\n");
 1108 #endif /* TW_OSL_DEBUG */
 1109                                 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
 1110                         }
 1111 
 1112                         error = 0;
 1113                         end_time = tw_osl_get_local_time() + timeout;
 1114                         continue;
 1115                         /*
 1116                          * Don't touch req after a reset.  It (and any
 1117                          * associated data) will be
 1118                          * unmapped by the callback.
 1119                          */
 1120                 }
 1121                 /* 
 1122                  * Either the request got completed, or we were woken up by a
 1123                  * signal.  Calculate the new timeout, in case it was the latter.
 1124                  */
 1125                 timeout = (end_time - tw_osl_get_local_time());
 1126         } /* End of while loop */
 1127 
 1128         /* If there was a payload, copy it back. */
 1129         if ((!error) && (req->length))
 1130                 if ((error = copyout(req->data, user_buf->pdata,
 1131                         user_buf->driver_pkt.buffer_length)))
 1132                         tw_osli_printf(sc, "error = %d",
 1133                                 TW_CL_SEVERITY_ERROR_STRING,
 1134                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1135                                 0x2019,
 1136                                 "Could not copyout fw_passthru data_buf",
 1137                                 error);
 1138         
 1139 fw_passthru_err:
 1140 
 1141         if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
 1142                 error = EBUSY;
 1143 
 1144         user_buf->driver_pkt.os_status = error;
 1145         /* Free resources. */
 1146         if (req->data)
 1147                 free(req->data, TW_OSLI_MALLOC_CLASS);
 1148         tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
 1149         return(error);
 1150 }
 1151 
 1152 
 1153 
 1154 /*
 1155  * Function name:       tw_osl_complete_passthru
 1156  * Description:         Called to complete passthru requests.
 1157  *
 1158  * Input:               req_handle      -- ptr to request handle
 1159  * Output:              None
 1160  * Return value:        None
 1161  */
 1162 TW_VOID
 1163 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
 1164 {
 1165         struct tw_osli_req_context      *req = req_handle->osl_req_ctxt;
 1166         struct tw_cl_req_packet         *req_pkt =
 1167                 (struct tw_cl_req_packet *)(&req->req_pkt);
 1168         struct twa_softc                *sc = req->ctlr;
 1169 
 1170         tw_osli_dbg_dprintf(5, sc, "entered");
 1171 
 1172         if (req->state != TW_OSLI_REQ_STATE_BUSY) {
 1173                 tw_osli_printf(sc, "request = %p, status = %d",
 1174                         TW_CL_SEVERITY_ERROR_STRING,
 1175                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1176                         0x201B,
 1177                         "Unposted command completed!!",
 1178                         req, req->state);
 1179         }
 1180 
 1181         /*
 1182          * Remove request from the busy queue.  Just mark it complete.
 1183          * There's no need to move it into the complete queue as we are
 1184          * going to be done with it right now.
 1185          */
 1186         req->state = TW_OSLI_REQ_STATE_COMPLETE;
 1187         tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
 1188 
 1189         tw_osli_unmap_request(req);
 1190 
 1191         /*
 1192          * Don't do a wake up if there was an error even before the request
 1193          * was sent down to the Common Layer, and we hadn't gotten an
 1194          * EINPROGRESS.  The request originator will then be returned an
 1195          * error, and he can do the clean-up.
 1196          */
 1197         if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
 1198                 return;
 1199 
 1200         if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1201                 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
 1202                         /* Wake up the sleeping command originator. */
 1203                         tw_osli_dbg_dprintf(5, sc,
 1204                                 "Waking up originator of request %p", req);
 1205                         req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
 1206                         wakeup_one(req);
 1207                 } else {
 1208                         /*
 1209                          * If the request completed even before mtx_sleep
 1210                          * was called, simply return.
 1211                          */
 1212                         if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
 1213                                 return;
 1214 
 1215                         if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
 1216                                 return;
 1217 
 1218                         tw_osli_printf(sc, "request = %p",
 1219                                 TW_CL_SEVERITY_ERROR_STRING,
 1220                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1221                                 0x201C,
 1222                                 "Passthru callback called, "
 1223                                 "and caller not sleeping",
 1224                                 req);
 1225                 }
 1226         } else {
 1227                 tw_osli_printf(sc, "request = %p",
 1228                         TW_CL_SEVERITY_ERROR_STRING,
 1229                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1230                         0x201D,
 1231                         "Passthru callback called for non-passthru request",
 1232                         req);
 1233         }
 1234 }
 1235 
 1236 
 1237 
 1238 /*
 1239  * Function name:       tw_osli_get_request
 1240  * Description:         Gets a request pkt from the free queue.
 1241  *
 1242  * Input:               sc      -- ptr to OSL internal ctlr context
 1243  * Output:              None
 1244  * Return value:        ptr to request pkt      -- success
 1245  *                      NULL                    -- failure
 1246  */
 1247 struct tw_osli_req_context *
 1248 tw_osli_get_request(struct twa_softc *sc)
 1249 {
 1250         struct tw_osli_req_context      *req;
 1251 
 1252         tw_osli_dbg_dprintf(4, sc, "entered");
 1253 
 1254         /* Get a free request packet. */
 1255         req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
 1256 
 1257         /* Initialize some fields to their defaults. */
 1258         if (req) {
 1259                 req->req_handle.osl_req_ctxt = NULL;
 1260                 req->req_handle.cl_req_ctxt = NULL;
 1261                 req->req_handle.is_io = 0;
 1262                 req->data = NULL;
 1263                 req->length = 0;
 1264                 req->deadline = 0;
 1265                 req->real_data = NULL;
 1266                 req->real_length = 0;
 1267                 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
 1268                 req->flags = 0;
 1269                 req->error_code = 0;
 1270                 req->orig_req = NULL;
 1271 
 1272                 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
 1273 
 1274         }
 1275         return(req);
 1276 }
 1277 
 1278 
 1279 
 1280 /*
 1281  * Function name:       twa_map_load_data_callback
 1282  * Description:         Callback of bus_dmamap_load for the buffer associated
 1283  *                      with data.  Updates the cmd pkt (size/sgl_entries
 1284  *                      fields, as applicable) to reflect the number of sg
 1285  *                      elements.
 1286  *
 1287  * Input:               arg     -- ptr to OSL internal request context
 1288  *                      segs    -- ptr to a list of segment descriptors
 1289  *                      nsegments--# of segments
 1290  *                      error   -- 0 if no errors encountered before callback,
 1291  *                                 non-zero if errors were encountered
 1292  * Output:              None
 1293  * Return value:        None
 1294  */
 1295 static TW_VOID
 1296 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
 1297         TW_INT32 nsegments, TW_INT32 error)
 1298 {
 1299         struct tw_osli_req_context      *req =
 1300                 (struct tw_osli_req_context *)arg;
 1301         struct twa_softc                *sc = req->ctlr;
 1302         struct tw_cl_req_packet         *req_pkt = &(req->req_pkt);
 1303 
 1304         tw_osli_dbg_dprintf(10, sc, "entered");
 1305 
 1306         if (error == EINVAL) {
 1307                 req->error_code = error;
 1308                 return;
 1309         }
 1310 
 1311         /* Mark the request as currently being processed. */
 1312         req->state = TW_OSLI_REQ_STATE_BUSY;
 1313         /* Move the request into the busy queue. */
 1314         tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
 1315 
 1316         req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
 1317 
 1318         if (error == EFBIG) {
 1319                 req->error_code = error;
 1320                 goto out;
 1321         }
 1322 
 1323         if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1324                 struct tw_cl_passthru_req_packet        *pt_req;
 1325 
 1326                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
 1327                         bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
 1328                                 BUS_DMASYNC_PREREAD);
 1329 
 1330                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
 1331                         /* 
 1332                          * If we're using an alignment buffer, and we're
 1333                          * writing data, copy the real data out.
 1334                          */
 1335                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1336                                 bcopy(req->real_data, req->data, req->real_length);
 1337                         bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
 1338                                 BUS_DMASYNC_PREWRITE);
 1339                 }
 1340 
 1341                 pt_req = &(req_pkt->gen_req_pkt.pt_req);
 1342                 pt_req->sg_list = (TW_UINT8 *)segs;
 1343                 pt_req->sgl_entries += (nsegments - 1);
 1344                 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
 1345                         &(req->req_handle));
 1346         } else {
 1347                 struct tw_cl_scsi_req_packet    *scsi_req;
 1348 
 1349                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
 1350                         bus_dmamap_sync(sc->dma_tag, req->dma_map,
 1351                                 BUS_DMASYNC_PREREAD);
 1352 
 1353                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
 1354                         /* 
 1355                          * If we're using an alignment buffer, and we're
 1356                          * writing data, copy the real data out.
 1357                          */
 1358                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1359                                 bcopy(req->real_data, req->data, req->real_length);
 1360                         bus_dmamap_sync(sc->dma_tag, req->dma_map,
 1361                                 BUS_DMASYNC_PREWRITE);
 1362                 }
 1363 
 1364                 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
 1365                 scsi_req->sg_list = (TW_UINT8 *)segs;
 1366                 scsi_req->sgl_entries += (nsegments - 1);
 1367                 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
 1368                         &(req->req_handle));
 1369         }
 1370 
 1371 out:
 1372         if (error) {
 1373                 req->error_code = error;
 1374                 req_pkt->tw_osl_callback(&(req->req_handle));
 1375                 /*
 1376                  * If the caller had been returned EINPROGRESS, and he has
 1377                  * registered a callback for handling completion, the callback
 1378                  * will never get called because we were unable to submit the
 1379                  * request.  So, free up the request right here.
 1380                  */
 1381                 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
 1382                         tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
 1383         }
 1384 }
 1385 
 1386 
 1387 
 1388 /*
 1389  * Function name:       twa_map_load_callback
 1390  * Description:         Callback of bus_dmamap_load for the buffer associated
 1391  *                      with a cmd pkt.
 1392  *
 1393  * Input:               arg     -- ptr to variable to hold phys addr
 1394  *                      segs    -- ptr to a list of segment descriptors
 1395  *                      nsegments--# of segments
 1396  *                      error   -- 0 if no errors encountered before callback,
 1397  *                                 non-zero if errors were encountered
 1398  * Output:              None
 1399  * Return value:        None
 1400  */
 1401 static TW_VOID
 1402 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
 1403         TW_INT32 nsegments, TW_INT32 error)
 1404 {
 1405         *((bus_addr_t *)arg) = segs[0].ds_addr;
 1406 }
 1407 
 1408 
 1409 
 1410 /*
 1411  * Function name:       tw_osli_map_request
 1412  * Description:         Maps a cmd pkt and data associated with it, into
 1413  *                      DMA'able memory.
 1414  *
 1415  * Input:               req     -- ptr to request pkt
 1416  * Output:              None
 1417  * Return value:        0       -- success
 1418  *                      non-zero-- failure
 1419  */
 1420 TW_INT32
 1421 tw_osli_map_request(struct tw_osli_req_context *req)
 1422 {
 1423         struct twa_softc        *sc = req->ctlr;
 1424         TW_INT32                error = 0;
 1425 
 1426         tw_osli_dbg_dprintf(10, sc, "entered");
 1427 
 1428         /* If the command involves data, map that too. */
 1429         if (req->data != NULL) {
 1430                 /*
 1431                  * It's sufficient for the data pointer to be 4-byte aligned
 1432                  * to work with 9000.  However, if 4-byte aligned addresses
 1433                  * are passed to bus_dmamap_load, we can get back sg elements
 1434                  * that are not 512-byte multiples in size.  So, we will let
 1435                  * only those buffers that are 512-byte aligned to pass
 1436                  * through, and bounce the rest, so as to make sure that we
 1437                  * always get back sg elements that are 512-byte multiples
 1438                  * in size.
 1439                  */
 1440                 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
 1441                         (req->length % sc->sg_size_factor)) {
 1442                         req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
 1443                         /* Save original data pointer and length. */
 1444                         req->real_data = req->data;
 1445                         req->real_length = req->length;
 1446                         req->length = (req->length +
 1447                                 (sc->sg_size_factor - 1)) &
 1448                                 ~(sc->sg_size_factor - 1);
 1449                         req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
 1450                                         M_NOWAIT);
 1451                         if (req->data == NULL) {
 1452                                 tw_osli_printf(sc, "error = %d",
 1453                                         TW_CL_SEVERITY_ERROR_STRING,
 1454                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1455                                         0x201E,
 1456                                         "Failed to allocate memory "
 1457                                         "for bounce buffer",
 1458                                         ENOMEM);
 1459                                 /* Restore original data pointer and length. */
 1460                                 req->data = req->real_data;
 1461                                 req->length = req->real_length;
 1462                                 return(ENOMEM);
 1463                         }
 1464                 }
 1465         
 1466                 /*
 1467                  * Map the data buffer into bus space and build the SG list.
 1468                  */
 1469                 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1470                         /* Lock against multiple simultaneous ioctl calls. */
 1471                         mtx_lock_spin(sc->io_lock);
 1472                         error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
 1473                                 req->data, req->length,
 1474                                 twa_map_load_data_callback, req,
 1475                                 BUS_DMA_WAITOK);
 1476                         mtx_unlock_spin(sc->io_lock);
 1477                 } else {
 1478                         /*
 1479                          * There's only one CAM I/O thread running at a time.
 1480                          * So, there's no need to hold the io_lock.
 1481                          */
 1482                         error = bus_dmamap_load(sc->dma_tag, req->dma_map,
 1483                                 req->data, req->length,
 1484                                 twa_map_load_data_callback, req,
 1485                                 BUS_DMA_WAITOK);
 1486                 }
 1487                 
 1488                 if (!error)
 1489                         error = req->error_code;
 1490                 else {
 1491                         if (error == EINPROGRESS) {
 1492                                 /*
 1493                                  * Specifying sc->io_lock as the lockfuncarg
 1494                                  * in ...tag_create should protect the access
 1495                                  * of ...FLAGS_MAPPED from the callback.
 1496                                  */
 1497                                 mtx_lock_spin(sc->io_lock);
 1498                                 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
 1499                                         req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
 1500                                 tw_osli_disallow_new_requests(sc, &(req->req_handle));
 1501                                 mtx_unlock_spin(sc->io_lock);
 1502                                 error = 0;
 1503                         } else {
 1504                                 tw_osli_printf(sc, "error = %d",
 1505                                         TW_CL_SEVERITY_ERROR_STRING,
 1506                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1507                                         0x9999,
 1508                                         "Failed to map DMA memory "
 1509                                         "for I/O request",
 1510                                         error);
 1511                                 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
 1512                                 /* Free alignment buffer if it was used. */
 1513                                 if (req->flags &
 1514                                         TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
 1515                                         free(req->data, TW_OSLI_MALLOC_CLASS);
 1516                                         /*
 1517                                          * Restore original data pointer
 1518                                          * and length.
 1519                                          */
 1520                                         req->data = req->real_data;
 1521                                         req->length = req->real_length;
 1522                                 }
 1523                         }
 1524                 }
 1525 
 1526         } else {
 1527                 /* Mark the request as currently being processed. */
 1528                 req->state = TW_OSLI_REQ_STATE_BUSY;
 1529                 /* Move the request into the busy queue. */
 1530                 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
 1531                 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
 1532                         error = tw_cl_fw_passthru(&sc->ctlr_handle,
 1533                                         &(req->req_pkt), &(req->req_handle));
 1534                 else
 1535                         error = tw_cl_start_io(&sc->ctlr_handle,
 1536                                         &(req->req_pkt), &(req->req_handle));
 1537                 if (error) {
 1538                         req->error_code = error;
 1539                         req->req_pkt.tw_osl_callback(&(req->req_handle));
 1540                 }
 1541         }
 1542         return(error);
 1543 }
 1544 
 1545 
 1546 
 1547 /*
 1548  * Function name:       tw_osli_unmap_request
 1549  * Description:         Undoes the mapping done by tw_osli_map_request.
 1550  *
 1551  * Input:               req     -- ptr to request pkt
 1552  * Output:              None
 1553  * Return value:        None
 1554  */
 1555 TW_VOID
 1556 tw_osli_unmap_request(struct tw_osli_req_context *req)
 1557 {
 1558         struct twa_softc        *sc = req->ctlr;
 1559 
 1560         tw_osli_dbg_dprintf(10, sc, "entered");
 1561 
 1562         /* If the command involved data, unmap that too. */
 1563         if (req->data != NULL) {
 1564                 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1565                         /* Lock against multiple simultaneous ioctl calls. */
 1566                         mtx_lock_spin(sc->io_lock);
 1567 
 1568                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
 1569                                 bus_dmamap_sync(sc->ioctl_tag,
 1570                                         sc->ioctl_map, BUS_DMASYNC_POSTREAD);
 1571 
 1572                                 /* 
 1573                                  * If we are using a bounce buffer, and we are
 1574                                  * reading data, copy the real data in.
 1575                                  */
 1576                                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1577                                         bcopy(req->data, req->real_data,
 1578                                                 req->real_length);
 1579                         }
 1580 
 1581                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
 1582                                 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
 1583                                         BUS_DMASYNC_POSTWRITE);
 1584 
 1585                         bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
 1586 
 1587                         mtx_unlock_spin(sc->io_lock);
 1588                 } else {
 1589                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
 1590                                 bus_dmamap_sync(sc->dma_tag,
 1591                                         req->dma_map, BUS_DMASYNC_POSTREAD);
 1592 
 1593                                 /* 
 1594                                  * If we are using a bounce buffer, and we are
 1595                                  * reading data, copy the real data in.
 1596                                  */
 1597                                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1598                                         bcopy(req->data, req->real_data,
 1599                                                 req->real_length);
 1600                         }
 1601                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
 1602                                 bus_dmamap_sync(sc->dma_tag, req->dma_map,
 1603                                         BUS_DMASYNC_POSTWRITE);
 1604 
 1605                         bus_dmamap_unload(sc->dma_tag, req->dma_map);
 1606                 }
 1607         }
 1608 
 1609         /* Free alignment buffer if it was used. */
 1610         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
 1611                 free(req->data, TW_OSLI_MALLOC_CLASS);
 1612                 /* Restore original data pointer and length. */
 1613                 req->data = req->real_data;
 1614                 req->length = req->real_length;
 1615         }
 1616 }
 1617 
 1618 
 1619 
 1620 #ifdef TW_OSL_DEBUG
 1621 
 1622 TW_VOID twa_report_stats(TW_VOID);
 1623 TW_VOID twa_reset_stats(TW_VOID);
 1624 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
 1625 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
 1626 
 1627 
 1628 /*
 1629  * Function name:       twa_report_stats
 1630  * Description:         For being called from ddb.  Calls functions that print
 1631  *                      OSL and CL internal stats for the controller.
 1632  *
 1633  * Input:               None
 1634  * Output:              None
 1635  * Return value:        None
 1636  */
 1637 TW_VOID
 1638 twa_report_stats(TW_VOID)
 1639 {
 1640         struct twa_softc        *sc;
 1641         TW_INT32                i;
 1642 
 1643         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
 1644                 tw_osli_print_ctlr_stats(sc);
 1645                 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
 1646         }
 1647 }
 1648 
 1649 
 1650 
 1651 /*
 1652  * Function name:       tw_osli_print_ctlr_stats
 1653  * Description:         For being called from ddb.  Prints OSL controller stats
 1654  *
 1655  * Input:               sc      -- ptr to OSL internal controller context
 1656  * Output:              None
 1657  * Return value:        None
 1658  */
 1659 TW_VOID
 1660 tw_osli_print_ctlr_stats(struct twa_softc *sc)
 1661 {
 1662         twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
 1663         twa_printf(sc, "OSLq type  current  max\n");
 1664         twa_printf(sc, "free      %04d     %04d\n",
 1665                 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
 1666                 sc->q_stats[TW_OSLI_FREE_Q].max_len);
 1667         twa_printf(sc, "busy      %04d     %04d\n",
 1668                 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
 1669                 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
 1670 }       
 1671 
 1672 
 1673 
 1674 /*
 1675  * Function name:       twa_print_req_info
 1676  * Description:         For being called from ddb.  Calls functions that print
 1677  *                      OSL and CL internal details for the request.
 1678  *
 1679  * Input:               req     -- ptr to OSL internal request context
 1680  * Output:              None
 1681  * Return value:        None
 1682  */
 1683 TW_VOID
 1684 twa_print_req_info(struct tw_osli_req_context *req)
 1685 {
 1686         struct twa_softc        *sc = req->ctlr;
 1687 
 1688         twa_printf(sc, "OSL details for request:\n");
 1689         twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
 1690                 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
 1691                 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
 1692                 "next_req = %p, prev_req = %p, dma_map = %p\n",
 1693                 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
 1694                 req->data, req->length, req->real_data, req->real_length,
 1695                 req->state, req->flags, req->error_code, req->orig_req,
 1696                 req->link.next, req->link.prev, req->dma_map);
 1697         tw_cl_print_req_info(&(req->req_handle));
 1698 }
 1699 
 1700 
 1701 
 1702 /*
 1703  * Function name:       twa_reset_stats
 1704  * Description:         For being called from ddb.
 1705  *                      Resets some OSL controller stats.
 1706  *
 1707  * Input:               None
 1708  * Output:              None
 1709  * Return value:        None
 1710  */
 1711 TW_VOID
 1712 twa_reset_stats(TW_VOID)
 1713 {
 1714         struct twa_softc        *sc;
 1715         TW_INT32                i;
 1716 
 1717         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
 1718                 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
 1719                 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
 1720                 tw_cl_reset_stats(&sc->ctlr_handle);
 1721         }
 1722 }
 1723 
 1724 #endif /* TW_OSL_DEBUG */

Cache object: 6898809abc8a877405f1cd7b6d21f97c


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