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-05 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/6.0/sys/dev/twa/tw_osl_freebsd.c 144966 2005-04-12 22:07:11Z vkashyap $
   30  */
   31 
   32 /*
   33  * AMCC'S 3ware driver for 9000 series storage controllers.
   34  *
   35  * Author: Vinod Kashyap
   36  */
   37 
   38 
   39 /*
   40  * FreeBSD specific functions not related to CAM, and other
   41  * miscellaneous functions.
   42  */
   43 
   44 
   45 #include "tw_osl_includes.h"
   46 #include "tw_cl_fwif.h"
   47 #include "tw_cl_ioctl.h"
   48 #include "tw_osl_ioctl.h"
   49 
   50 #ifdef TW_OSL_DEBUG
   51 TW_INT32        TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
   52 TW_INT32        TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
   53 #endif /* TW_OSL_DEBUG */
   54 
   55 MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa commands", "twa commands");
   56 
   57 
   58 static  d_open_t                twa_open;
   59 static  d_close_t               twa_close;
   60 static  d_ioctl_t               twa_ioctl;
   61 
   62 static struct cdevsw twa_cdevsw = {
   63         .d_version =    D_VERSION,
   64         .d_open =       twa_open,
   65         .d_close =      twa_close,
   66         .d_ioctl =      twa_ioctl,
   67         .d_name =       "twa",
   68 };
   69 
   70 static devclass_t       twa_devclass;
   71 
   72 
   73 /*
   74  * Function name:       twa_open
   75  * Description:         Called when the controller is opened.
   76  *                      Simply marks the controller as open.
   77  *
   78  * Input:               dev     -- control device corresponding to the ctlr
   79  *                      flags   -- mode of open
   80  *                      fmt     -- device type (character/block etc.)
   81  *                      proc    -- current process
   82  * Output:              None
   83  * Return value:        0       -- success
   84  *                      non-zero-- failure
   85  */
   86 static TW_INT32
   87 twa_open(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
   88 {
   89         TW_INT32                unit = minor(dev);
   90         struct twa_softc        *sc = devclass_get_softc(twa_devclass, unit);
   91 
   92         tw_osli_dbg_dprintf(5, sc, "entered");
   93         sc->state |= TW_OSLI_CTLR_STATE_OPEN;
   94         return(0);
   95 }
   96 
   97 
   98 
   99 /*
  100  * Function name:       twa_close
  101  * Description:         Called when the controller is closed.
  102  *                      Simply marks the controller as not open.
  103  *
  104  * Input:               dev     -- control device corresponding to the ctlr
  105  *                      flags   -- mode of corresponding open
  106  *                      fmt     -- device type (character/block etc.)
  107  *                      proc    -- current process
  108  * Output:              None
  109  * Return value:        0       -- success
  110  *                      non-zero-- failure
  111  */
  112 static TW_INT32
  113 twa_close(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
  114 {
  115         TW_INT32                unit = minor(dev);
  116         struct twa_softc        *sc = devclass_get_softc(twa_devclass, unit);
  117 
  118         tw_osli_dbg_dprintf(5, sc, "entered");
  119         sc->state &= ~TW_OSLI_CTLR_STATE_OPEN;
  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, d_thread_t *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_deferred_intr(TW_VOID *context, TW_INT32 pending);
  179 
  180 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
  181 static TW_VOID  tw_osli_free_resources(struct twa_softc *sc);
  182 
  183 static TW_VOID  twa_map_load_data_callback(TW_VOID *arg,
  184         bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
  185 static TW_VOID  twa_map_load_callback(TW_VOID *arg,
  186         bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
  187 
  188 
  189 static device_method_t  twa_methods[] = {
  190         /* Device interface */
  191         DEVMETHOD(device_probe,         twa_probe),
  192         DEVMETHOD(device_attach,        twa_attach),
  193         DEVMETHOD(device_detach,        twa_detach),
  194         DEVMETHOD(device_shutdown,      twa_shutdown),
  195 
  196         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  197         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
  198         {0, 0}
  199 };
  200 
  201 static driver_t twa_pci_driver = {
  202         "twa",
  203         twa_methods,
  204         sizeof(struct twa_softc)
  205 };
  206 
  207 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
  208 
  209 
  210 
  211 /*
  212  * Function name:       twa_probe
  213  * Description:         Called at driver load time.  Claims 9000 ctlrs.
  214  *
  215  * Input:               dev     -- bus device corresponding to the ctlr
  216  * Output:              None
  217  * Return value:        <= 0    -- success
  218  *                      > 0     -- failure
  219  */
  220 static TW_INT32
  221 twa_probe(device_t dev)
  222 {
  223         static TW_UINT8 first_ctlr = 1;
  224 
  225         tw_osli_dbg_printf(3, "entered");
  226 
  227         if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
  228                 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
  229                 /* Print the driver version only once. */
  230                 if (first_ctlr) {
  231                         printf("3ware device driver for 9000 series storage "
  232                                 "controllers, version: %s\n",
  233                                 TW_OSL_DRIVER_VERSION_STRING);
  234                         first_ctlr = 0;
  235                 }
  236                 return(0);
  237         }
  238         return(ENXIO);
  239 }
  240 
  241 
  242 
  243 /*
  244  * Function name:       twa_attach
  245  * Description:         Allocates pci resources; updates sc; adds a node to the
  246  *                      sysctl tree to expose the driver version; makes calls
  247  *                      (to the Common Layer) to initialize ctlr, and to
  248  *                      attach to CAM.
  249  *
  250  * Input:               dev     -- bus device corresponding to the ctlr
  251  * Output:              None
  252  * Return value:        0       -- success
  253  *                      non-zero-- failure
  254  */
  255 static TW_INT32
  256 twa_attach(device_t dev)
  257 {
  258         struct twa_softc        *sc = device_get_softc(dev);
  259         TW_UINT32               command;
  260         TW_INT32                error;
  261 
  262         tw_osli_dbg_dprintf(3, sc, "entered");
  263 
  264         sc->ctlr_handle.osl_ctlr_ctxt = sc;
  265 
  266         /* Initialize the softc structure. */
  267         sc->bus_dev = dev;
  268 
  269         /* Initialize the mutexes right here. */
  270         sc->io_lock = &(sc->io_lock_handle);
  271         mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
  272         sc->q_lock = &(sc->q_lock_handle);
  273         mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
  274 
  275         sysctl_ctx_init(&sc->sysctl_ctxt);
  276         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
  277                 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
  278                 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
  279         if (sc->sysctl_tree == NULL) {
  280                 tw_osli_printf(sc, "error = %d",
  281                         TW_CL_SEVERITY_ERROR_STRING,
  282                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  283                         0x2000,
  284                         "Cannot add sysctl tree node",
  285                         ENXIO);
  286                 return(ENXIO);
  287         }
  288         SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
  289                 OID_AUTO, "driver_version", CTLFLAG_RD,
  290                 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
  291 
  292         /* Make sure we are going to be able to talk to this board. */
  293         command = pci_read_config(dev, PCIR_COMMAND, 2);
  294         if ((command & PCIM_CMD_PORTEN) == 0) {
  295                 tw_osli_printf(sc, "error = %d",
  296                         TW_CL_SEVERITY_ERROR_STRING,
  297                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  298                         0x2001,
  299                         "Register window not available",
  300                         ENXIO);
  301                 tw_osli_free_resources(sc);
  302                 return(ENXIO);
  303         }
  304 
  305         /* Force the busmaster enable bit on, in case the BIOS forgot. */
  306         command |= PCIM_CMD_BUSMASTEREN;
  307         pci_write_config(dev, PCIR_COMMAND, command, 2);
  308 
  309         /* Allocate the PCI register window. */
  310         sc->reg_res_id = PCIR_BARS;
  311         if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
  312                                 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
  313                                 == NULL) {
  314                 tw_osli_printf(sc, "error = %d",
  315                         TW_CL_SEVERITY_ERROR_STRING,
  316                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  317                         0x2002,
  318                         "Can't allocate register window",
  319                         ENXIO);
  320                 tw_osli_free_resources(sc);
  321                 return(ENXIO);
  322         }
  323         sc->bus_tag = rman_get_bustag(sc->reg_res);
  324         sc->bus_handle = rman_get_bushandle(sc->reg_res);
  325 
  326         /* Allocate and register our interrupt. */
  327         sc->irq_res_id = 0;
  328         if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
  329                                 &(sc->irq_res_id), 0, ~0, 1,
  330                                 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
  331                 tw_osli_printf(sc, "error = %d",
  332                         TW_CL_SEVERITY_ERROR_STRING,
  333                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  334                         0x2003,
  335                         "Can't allocate interrupt",
  336                         ENXIO);
  337                 tw_osli_free_resources(sc);
  338                 return(ENXIO);
  339         }
  340         if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
  341                         ((mp_ncpus > 1) ? (INTR_MPSAFE | INTR_FAST) : 0) |
  342                         INTR_TYPE_CAM,
  343                         twa_pci_intr, sc, &sc->intr_handle))) {
  344                 tw_osli_printf(sc, "error = %d",
  345                         TW_CL_SEVERITY_ERROR_STRING,
  346                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  347                         0x2004,
  348                         "Can't set up interrupt",
  349                         error);
  350                 tw_osli_free_resources(sc);
  351                 return(error);
  352         }
  353 
  354         TASK_INIT(&sc->deferred_intr_callback, 0, twa_deferred_intr, sc);
  355 
  356         if ((error = tw_osli_alloc_mem(sc))) {
  357                 tw_osli_printf(sc, "error = %d",
  358                         TW_CL_SEVERITY_ERROR_STRING,
  359                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  360                         0x2005,
  361                         "Memory allocation failure",
  362                         error);
  363                 tw_osli_free_resources(sc);
  364                 return(error);
  365         }
  366 
  367         /* Initialize the Common Layer for this controller. */
  368         if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags,
  369                         TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
  370                         sc->non_dma_mem, sc->dma_mem,
  371                         sc->dma_mem_phys
  372 #ifdef TW_OSL_FLASH_FIRMWARE
  373                         , sc->flash_dma_mem, sc->flash_dma_mem_phys
  374 #endif /* TW_OSL_FLASH_FIRMWARE */
  375                         ))) {
  376                 tw_osli_printf(sc, "error = %d",
  377                         TW_CL_SEVERITY_ERROR_STRING,
  378                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  379                         0x2006,
  380                         "Failed to initialize Common Layer/controller",
  381                         error);
  382                 tw_osli_free_resources(sc);
  383                 return(error);
  384         }
  385 
  386 #ifdef TW_OSL_FLASH_FIRMWARE
  387         /* Free any memory allocated for firmware flashing. */
  388         if (sc->flash_dma_mem) {
  389                 bus_dmamap_unload(sc->flash_tag, sc->flash_map);
  390                 bus_dmamem_free(sc->flash_tag, sc->flash_dma_mem,
  391                         sc->flash_map);
  392         }
  393         if (sc->flash_tag)
  394                 bus_dma_tag_destroy(sc->flash_tag);
  395         /*
  396          * Set flash_tag and flash_dma_mem to 0, so we don't try freeing them
  397          * again, later.
  398          */
  399         sc->flash_tag = 0;
  400         sc->flash_dma_mem = 0;
  401 #endif /* TW_OSL_FLASH_FIRMWARE */
  402 
  403         /* Create the control device. */
  404         sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
  405                         UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
  406                         "twa%d", device_get_unit(sc->bus_dev));
  407         sc->ctrl_dev->si_drv1 = sc;
  408 
  409         if ((error = tw_osli_cam_attach(sc))) {
  410                 tw_osli_free_resources(sc);
  411                 tw_osli_printf(sc, "error = %d",
  412                         TW_CL_SEVERITY_ERROR_STRING,
  413                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  414                         0x2007,
  415                         "Failed to initialize CAM",
  416                         error);
  417                 return(error);
  418         }
  419 
  420         return(0);
  421 }
  422 
  423 
  424 
  425 /*
  426  * Function name:       tw_osli_alloc_mem
  427  * Description:         Allocates memory needed both by CL and OSL.
  428  *
  429  * Input:               sc      -- OSL internal controller context
  430  * Output:              None
  431  * Return value:        0       -- success
  432  *                      non-zero-- failure
  433  */
  434 static TW_INT32
  435 tw_osli_alloc_mem(struct twa_softc *sc)
  436 {
  437         struct tw_osli_req_context      *req;
  438         TW_UINT32                       max_sg_elements;
  439         TW_UINT32                       non_dma_mem_size;
  440         TW_UINT32                       dma_mem_size;
  441 #ifdef TW_OSL_FLASH_FIRMWARE
  442         TW_UINT32                       flash_dma_mem_size;
  443 #endif /* TW_OSL_FLASH_FIRMWARE */
  444 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
  445         TW_UINT32                       per_req_dma_mem_size;
  446 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
  447         TW_INT32                        error;
  448         TW_INT32                        i;
  449 
  450         tw_osli_dbg_dprintf(3, sc, "entered");
  451 
  452         sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
  453         sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
  454 #ifdef TW_OSL_FLASH_FIRMWARE
  455         sc->flags |= TW_CL_FLASH_FIRMWARE; 
  456 #endif /* TW_OSL_FLASH_FIRMWARE */
  457 
  458         max_sg_elements = (sizeof(bus_addr_t) == 8) ?
  459                 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
  460 
  461         if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
  462                         TW_OSLI_MAX_NUM_IOS,  TW_OSLI_MAX_NUM_AENS,
  463                         &(sc->alignment), &(sc->sg_size_factor),
  464                         &non_dma_mem_size, &dma_mem_size
  465 #ifdef TW_OSL_FLASH_FIRMWARE
  466                         , &flash_dma_mem_size
  467 #endif /* TW_OSL_FLASH_FIRMWARE */
  468 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
  469                         , &per_req_dma_mem_size
  470 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
  471                         ))) {
  472                 tw_osli_printf(sc, "error = %d",
  473                         TW_CL_SEVERITY_ERROR_STRING,
  474                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  475                         0x2008,
  476                         "Can't get Common Layer's memory requirements",
  477                         error);
  478                 return(error);
  479         }
  480 
  481         if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
  482                                 M_WAITOK)) == NULL) {
  483                 tw_osli_printf(sc, "error = %d",
  484                         TW_CL_SEVERITY_ERROR_STRING,
  485                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  486                         0x2009,
  487                         "Can't allocate non-dma memory",
  488                         ENOMEM);
  489                 return(ENOMEM);
  490         }
  491 
  492         /* Create the parent dma tag. */
  493         if (bus_dma_tag_create(NULL,                    /* parent */
  494                                 sc->alignment,          /* alignment */
  495                                 0,                      /* boundary */
  496                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  497                                 BUS_SPACE_MAXADDR,      /* highaddr */
  498                                 NULL, NULL,             /* filter, filterarg */
  499                                 TW_CL_MAX_IO_SIZE,      /* maxsize */
  500                                 max_sg_elements,        /* nsegments */
  501                                 TW_CL_MAX_IO_SIZE,      /* maxsegsize */
  502                                 0,                      /* flags */
  503                                 NULL,                   /* lockfunc */
  504                                 NULL,                   /* lockfuncarg */
  505                                 &sc->parent_tag         /* tag */)) {
  506                 tw_osli_printf(sc, "error = %d",
  507                         TW_CL_SEVERITY_ERROR_STRING,
  508                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  509                         0x200A,
  510                         "Can't allocate parent DMA tag",
  511                         ENOMEM);
  512                 return(ENOMEM);
  513         }
  514 
  515         /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
  516         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  517                                 sc->alignment,          /* alignment */
  518                                 0,                      /* boundary */
  519                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  520                                 BUS_SPACE_MAXADDR,      /* highaddr */
  521                                 NULL, NULL,             /* filter, filterarg */
  522 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
  523                                 (TW_OSLI_MAX_NUM_IOS *
  524                                 per_req_dma_mem_size) + 
  525 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
  526                                 dma_mem_size,           /* maxsize */
  527                                 1,                      /* nsegments */
  528                                 BUS_SPACE_MAXSIZE,      /* maxsegsize */
  529                                 0,                      /* flags */
  530                                 NULL,                   /* lockfunc */
  531                                 NULL,                   /* lockfuncarg */
  532                                 &sc->cmd_tag            /* tag */)) {
  533                 tw_osli_printf(sc, "error = %d",
  534                         TW_CL_SEVERITY_ERROR_STRING,
  535                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  536                         0x200B,
  537                         "Can't allocate DMA tag for Common Layer's "
  538                         "DMA'able memory",
  539                         ENOMEM);
  540                 return(ENOMEM);
  541         }
  542 
  543         if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
  544                 BUS_DMA_NOWAIT, &sc->cmd_map)) {
  545                 /* Try a second time. */
  546                 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
  547                         BUS_DMA_NOWAIT, &sc->cmd_map)) {
  548                         tw_osli_printf(sc, "error = %d",
  549                                 TW_CL_SEVERITY_ERROR_STRING,
  550                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  551                                 0x200C,
  552                                 "Can't allocate DMA'able memory for the"
  553                                 "Common Layer",
  554                                 ENOMEM);
  555                         return(ENOMEM);
  556                 }
  557         }
  558 
  559         bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
  560                 dma_mem_size, twa_map_load_callback,
  561                 &sc->dma_mem_phys, 0);
  562 
  563 
  564 #ifdef TW_OSL_FLASH_FIRMWARE
  565         /*
  566          * Create a dma tag for Common Layer's DMA'able memory,
  567          * used to flash firmware (flash_dma_mem).
  568          */
  569         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  570                                 sc->alignment,          /* alignment */
  571                                 0,                      /* boundary */
  572                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  573                                 BUS_SPACE_MAXADDR,      /* highaddr */
  574                                 NULL, NULL,             /* filter, filterarg */
  575                                 flash_dma_mem_size,     /* maxsize */
  576                                 1,                      /* nsegments */
  577                                 flash_dma_mem_size,     /* maxsegsize */
  578                                 0,                      /* flags */
  579                                 NULL,                   /* lockfunc */
  580                                 NULL,                   /* lockfuncarg */
  581                                 &sc->flash_tag          /* tag */)) {
  582                 tw_osli_printf(sc, "error = %d",
  583                         TW_CL_SEVERITY_ERROR_STRING,
  584                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  585                         0x200D,
  586                         "Can't allocate DMA tag for Common Layer's "
  587                         "firmware flash memory",
  588                         ENOMEM);
  589                 return(ENOMEM);
  590         }
  591 
  592         if (bus_dmamem_alloc(sc->flash_tag, &sc->flash_dma_mem,
  593                 BUS_DMA_NOWAIT, &sc->flash_map)) {
  594                 tw_osli_printf(sc, "error = %d",
  595                         TW_CL_SEVERITY_ERROR_STRING,
  596                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  597                         0x200E,
  598                         "Can't allocate DMA'able memory for Common Layer's "
  599                         "firmware flash",
  600                         ENOMEM);
  601                 return(ENOMEM);
  602         }
  603 
  604         bus_dmamap_load(sc->flash_tag, sc->flash_map, sc->flash_dma_mem,
  605                 flash_dma_mem_size, twa_map_load_callback,
  606                 &sc->flash_dma_mem_phys, 0);
  607 
  608 #endif /* TW_OSL_FLASH_FIRMWARE */
  609 
  610         /*
  611          * Create a dma tag for data buffers; size will be the maximum
  612          * possible I/O size (128kB).
  613          */
  614         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  615                                 sc->alignment,          /* alignment */
  616                                 0,                      /* boundary */
  617                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  618                                 BUS_SPACE_MAXADDR,      /* highaddr */
  619                                 NULL, NULL,             /* filter, filterarg */
  620                                 TW_CL_MAX_IO_SIZE,      /* maxsize */
  621                                 max_sg_elements,        /* nsegments */
  622                                 TW_CL_MAX_IO_SIZE,      /* maxsegsize */
  623                                 BUS_DMA_ALLOCNOW,       /* flags */
  624                                 twa_busdma_lock,        /* lockfunc */
  625                                 sc->io_lock,            /* lockfuncarg */
  626                                 &sc->dma_tag            /* tag */)) {
  627                 tw_osli_printf(sc, "error = %d",
  628                         TW_CL_SEVERITY_ERROR_STRING,
  629                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  630                         0x200F,
  631                         "Can't allocate DMA tag for data buffers",
  632                         ENOMEM);
  633                 return(ENOMEM);
  634         }
  635 
  636         /*
  637          * Create a dma tag for ioctl data buffers; size will be the maximum
  638          * possible I/O size (128kB).
  639          */
  640         if (bus_dma_tag_create(sc->parent_tag,          /* parent */
  641                                 sc->alignment,          /* alignment */
  642                                 0,                      /* boundary */
  643                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  644                                 BUS_SPACE_MAXADDR,      /* highaddr */
  645                                 NULL, NULL,             /* filter, filterarg */
  646                                 TW_CL_MAX_IO_SIZE,      /* maxsize */
  647                                 max_sg_elements,        /* nsegments */
  648                                 TW_CL_MAX_IO_SIZE,      /* maxsegsize */
  649                                 BUS_DMA_ALLOCNOW,       /* flags */
  650                                 twa_busdma_lock,        /* lockfunc */
  651                                 sc->io_lock,            /* lockfuncarg */
  652                                 &sc->ioctl_tag          /* tag */)) {
  653                 tw_osli_printf(sc, "error = %d",
  654                         TW_CL_SEVERITY_ERROR_STRING,
  655                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  656                         0x2010,
  657                         "Can't allocate DMA tag for ioctl data buffers",
  658                         ENOMEM);
  659                 return(ENOMEM);
  660         }
  661 
  662         /* Create just one map for all ioctl request data buffers. */
  663         if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
  664                 tw_osli_printf(sc, "error = %d",
  665                         TW_CL_SEVERITY_ERROR_STRING,
  666                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  667                         0x2011,
  668                         "Can't create ioctl map",
  669                         ENOMEM);
  670                 return(ENOMEM);
  671         }
  672 
  673 
  674         /* Initialize request queues. */
  675         tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
  676         tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
  677 
  678         if ((sc->req_ctxt_buf = (struct tw_osli_req_context *)
  679                         malloc((sizeof(struct tw_osli_req_context) *
  680                                 TW_OSLI_MAX_NUM_IOS),
  681                                 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
  682                 tw_osli_printf(sc, "error = %d",
  683                         TW_CL_SEVERITY_ERROR_STRING,
  684                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  685                         0x2012,
  686                         "Failed to allocate request packets",
  687                         ENOMEM);
  688                 return(ENOMEM);
  689         }
  690         bzero(sc->req_ctxt_buf,
  691                 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_IOS);
  692 
  693         for (i = 0; i < TW_OSLI_MAX_NUM_IOS; i++) {
  694                 req = &(sc->req_ctxt_buf[i]);
  695                 req->ctlr = sc;
  696                 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
  697                         tw_osli_printf(sc, "request # = %d, error = %d",
  698                                 TW_CL_SEVERITY_ERROR_STRING,
  699                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  700                                 0x2013,
  701                                 "Can't create dma map",
  702                                 i, ENOMEM);
  703                         return(ENOMEM);
  704                 }
  705 
  706 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
  707 
  708                 req->req_pkt.dma_mem = ((TW_INT8 *)(sc->dma_mem)) +
  709                         (i * per_req_dma_mem_size);
  710                 req->req_pkt.dma_mem_phys = sc->dma_mem_phys +
  711                         (i * per_req_dma_mem_size);
  712 
  713 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
  714 
  715                 /* Insert request into the free queue. */
  716                 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
  717         }
  718 
  719 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
  720 
  721         sc->dma_mem = ((TW_INT8 *)(sc->dma_mem)) +
  722                 (TW_OSLI_MAX_NUM_IOS * per_req_dma_mem_size);
  723         sc->dma_mem_phys += (TW_OSLI_MAX_NUM_IOS * per_req_dma_mem_size);
  724 
  725 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
  726 
  727         return(0);
  728 }
  729 
  730 
  731 
  732 /*
  733  * Function name:       tw_osli_free_resources
  734  * Description:         Performs clean-up at the time of going down.
  735  *
  736  * Input:               sc      -- ptr to OSL internal ctlr context
  737  * Output:              None
  738  * Return value:        None
  739  */
  740 static TW_VOID
  741 tw_osli_free_resources(struct twa_softc *sc)
  742 {
  743         struct tw_osli_req_context      *req;
  744         TW_INT32                        error = 0;
  745 
  746         tw_osli_dbg_dprintf(3, sc, "entered");
  747 
  748         /* Detach from CAM */
  749         tw_osli_cam_detach(sc);
  750 
  751         if (sc->req_ctxt_buf)
  752                 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
  753                         NULL)
  754                         if ((error = bus_dmamap_destroy(sc->dma_tag,
  755                                         req->dma_map)))
  756                                 tw_osli_dbg_dprintf(1, sc,
  757                                         "dmamap_destroy(dma) returned %d",
  758                                         error);
  759 
  760         if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
  761                 tw_osli_dbg_dprintf(1, sc,
  762                         "dmamap_destroy(ioctl) returned %d", error);
  763 
  764         /* Free all memory allocated so far. */
  765         if (sc->req_ctxt_buf)
  766                 free(sc->req_ctxt_buf, TW_OSLI_MALLOC_CLASS);
  767 
  768         if (sc->non_dma_mem)
  769                 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
  770 
  771         if (sc->dma_mem) {
  772                 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
  773                 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
  774                         sc->cmd_map);
  775         }
  776         if (sc->cmd_tag)
  777                 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
  778                         tw_osli_dbg_dprintf(1, sc,
  779                                 "dma_tag_destroy(cmd) returned %d", error);
  780 
  781 
  782 #ifdef TW_OSL_FLASH_FIRMWARE
  783 
  784         if (sc->flash_dma_mem) {
  785                 /* In case this piece of memory has already been freed. */
  786                 bus_dmamap_unload(sc->flash_tag, sc->flash_map);
  787                 bus_dmamem_free(sc->flash_tag, sc->flash_dma_mem,
  788                         sc->flash_map);
  789         }
  790         if (sc->flash_tag)
  791                 if ((error = bus_dma_tag_destroy(sc->flash_tag)))
  792                         tw_osli_dbg_dprintf(1, sc,
  793                                 "dma_tag_destroy(flash) returned %d", error);
  794 
  795 #endif /* TW_OSL_FLASH_FIRMWARE */
  796 
  797         if (sc->dma_tag)
  798                 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
  799                         tw_osli_dbg_dprintf(1, sc,
  800                                 "dma_tag_destroy(dma) returned %d", error);
  801 
  802         if (sc->ioctl_tag)
  803                 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
  804                         tw_osli_dbg_dprintf(1, sc,
  805                                 "dma_tag_destroy(ioctl) returned %d", error);
  806 
  807         if (sc->parent_tag)
  808                 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
  809                         tw_osli_dbg_dprintf(1, sc,
  810                                 "dma_tag_destroy(parent) returned %d", error);
  811 
  812 
  813         /* Disconnect the interrupt handler. */
  814         if (sc->intr_handle)
  815                 if ((error = bus_teardown_intr(sc->bus_dev,
  816                                 sc->irq_res, sc->intr_handle)))
  817                         tw_osli_dbg_dprintf(1, sc,
  818                                 "teardown_intr returned %d", error);
  819 
  820         if (sc->irq_res != NULL)
  821                 if ((error = bus_release_resource(sc->bus_dev,
  822                                 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
  823                         tw_osli_dbg_dprintf(1, sc,
  824                                 "release_resource(irq) returned %d", error);
  825 
  826 
  827         /* Release the register window mapping. */
  828         if (sc->reg_res != NULL)
  829                 if ((error = bus_release_resource(sc->bus_dev,
  830                                 SYS_RES_IOPORT, sc->reg_res_id, sc->reg_res)))
  831                         tw_osli_dbg_dprintf(1, sc,
  832                                 "release_resource(io) returned %d", error);
  833 
  834 
  835         /* Destroy the control device. */
  836         if (sc->ctrl_dev != (struct cdev *)NULL)
  837                 destroy_dev(sc->ctrl_dev);
  838 
  839         if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
  840                 tw_osli_dbg_dprintf(1, sc,
  841                         "sysctl_ctx_free returned %d", error);
  842 
  843 }
  844 
  845 
  846 
  847 /*
  848  * Function name:       twa_detach
  849  * Description:         Called when the controller is being detached from
  850  *                      the pci bus.
  851  *
  852  * Input:               dev     -- bus device corresponding to the ctlr
  853  * Output:              None
  854  * Return value:        0       -- success
  855  *                      non-zero-- failure
  856  */
  857 static TW_INT32
  858 twa_detach(device_t dev)
  859 {
  860         struct twa_softc        *sc = device_get_softc(dev);
  861         TW_INT32                error;
  862 
  863         tw_osli_dbg_dprintf(3, sc, "entered");
  864 
  865         error = EBUSY;
  866         if (sc->state & TW_OSLI_CTLR_STATE_OPEN) {
  867                 tw_osli_printf(sc, "error = %d",
  868                         TW_CL_SEVERITY_ERROR_STRING,
  869                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  870                         0x2014,
  871                         "Device open",
  872                         error);
  873                 goto out;
  874         }
  875 
  876         /* Shut the controller down. */
  877         if ((error = twa_shutdown(dev)))
  878                 goto out;
  879 
  880         /* Free all resources associated with this controller. */
  881         tw_osli_free_resources(sc);
  882         error = 0;
  883 
  884 out:
  885         return(error);
  886 }
  887 
  888 
  889 
  890 /*
  891  * Function name:       twa_shutdown
  892  * Description:         Called at unload/shutdown time.  Lets the controller
  893  *                      know that we are going down.
  894  *
  895  * Input:               dev     -- bus device corresponding to the ctlr
  896  * Output:              None
  897  * Return value:        0       -- success
  898  *                      non-zero-- failure
  899  */
  900 static TW_INT32
  901 twa_shutdown(device_t dev)
  902 {
  903         struct twa_softc        *sc = device_get_softc(dev);
  904         TW_INT32                error = 0;
  905 
  906         tw_osli_dbg_dprintf(3, sc, "entered");
  907 
  908         /* Disconnect from the controller. */
  909         if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
  910                 tw_osli_printf(sc, "error = %d",
  911                         TW_CL_SEVERITY_ERROR_STRING,
  912                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
  913                         0x2015,
  914                         "Failed to shutdown Common Layer/controller",
  915                         error);
  916         }
  917         return(error);
  918 }
  919 
  920 
  921 
  922 /*
  923  * Function name:       twa_busdma_lock
  924  * Description:         Function to provide synchronization during busdma_swi.
  925  *
  926  * Input:               lock_arg -- lock mutex sent as argument
  927  *                      op -- operation (lock/unlock) expected of the function
  928  * Output:              None
  929  * Return value:        None
  930  */
  931 TW_VOID
  932 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
  933 {
  934         struct mtx      *lock;
  935 
  936         lock = (struct mtx *)lock_arg;
  937         switch (op) {
  938         case BUS_DMA_LOCK:
  939                 mtx_lock_spin(lock);
  940                 break;
  941 
  942         case BUS_DMA_UNLOCK:
  943                 mtx_unlock_spin(lock);
  944                 break;
  945 
  946         default:
  947                 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
  948         }
  949 }
  950 
  951 
  952 
  953 /*
  954  * Function name:       twa_pci_intr
  955  * Description:         Interrupt handler.  Wrapper for twa_interrupt.
  956  *
  957  * Input:               arg     -- ptr to OSL internal ctlr context
  958  * Output:              None
  959  * Return value:        None
  960  */
  961 static TW_VOID
  962 twa_pci_intr(TW_VOID *arg)
  963 {
  964         struct twa_softc        *sc = (struct twa_softc *)arg;
  965 
  966         tw_osli_dbg_dprintf(10, sc, "entered");
  967         if (tw_cl_interrupt(&(sc->ctlr_handle)))
  968                 taskqueue_enqueue_fast(taskqueue_fast,
  969                         &(sc->deferred_intr_callback));
  970 }
  971 
  972 
  973 
  974 /*
  975  * Function name:       twa_deferred_intr
  976  * Description:         Deferred interrupt handler.
  977  *
  978  * Input:               context -- ptr to OSL internal ctlr context
  979  *                      pending -- not used
  980  * Output:              None
  981  * Return value:        None
  982  */
  983 static TW_VOID
  984 twa_deferred_intr(TW_VOID *context, TW_INT32 pending)
  985 {
  986         struct twa_softc        *sc = (struct twa_softc *)context;
  987 
  988         tw_osli_dbg_dprintf(10, sc, "entered");
  989 
  990         tw_cl_deferred_interrupt(&(sc->ctlr_handle));
  991 }
  992 
  993 
  994 
  995 /*
  996  * Function name:       tw_osli_fw_passthru
  997  * Description:         Builds a fw passthru cmd pkt, and submits it to CL.
  998  *
  999  * Input:               sc      -- ptr to OSL internal ctlr context
 1000  *                      buf     -- ptr to ioctl pkt understood by CL
 1001  * Output:              None
 1002  * Return value:        0       -- success
 1003  *                      non-zero-- failure
 1004  */
 1005 TW_INT32
 1006 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
 1007 {
 1008         struct tw_osli_req_context              *req;
 1009         struct tw_osli_ioctl_no_data_buf        *user_buf =
 1010                 (struct tw_osli_ioctl_no_data_buf *)buf;
 1011         TW_TIME                                 end_time;
 1012         TW_UINT32                               timeout = 60;
 1013         TW_UINT32                               data_buf_size_adjusted;
 1014         struct tw_cl_req_packet                 *req_pkt;
 1015         struct tw_cl_passthru_req_packet        *pt_req;
 1016         TW_INT32                                error;
 1017 
 1018         tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
 1019                 
 1020         if ((req = tw_osli_get_request(sc)) == NULL)
 1021                 return(EBUSY);
 1022 
 1023         req->req_handle.osl_req_ctxt = req;
 1024         req->orig_req = buf;
 1025         req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
 1026 
 1027         req_pkt = &(req->req_pkt);
 1028         req_pkt->status = 0;
 1029         req_pkt->tw_osl_callback = tw_osl_complete_passthru;
 1030         /* Let the Common Layer retry the request on cmd queue full. */
 1031         req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
 1032 
 1033         pt_req = &(req_pkt->gen_req_pkt.pt_req);
 1034         /*
 1035          * Make sure that the data buffer sent to firmware is a 
 1036          * 512 byte multiple in size.
 1037          */
 1038         data_buf_size_adjusted =
 1039                 (user_buf->driver_pkt.buffer_length +
 1040                 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
 1041         if ((req->length = data_buf_size_adjusted)) {
 1042                 if ((req->data = malloc(data_buf_size_adjusted,
 1043                         TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
 1044                         error = ENOMEM;
 1045                         tw_osli_printf(sc, "error = %d",
 1046                                 TW_CL_SEVERITY_ERROR_STRING,
 1047                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1048                                 0x2016,
 1049                                 "Could not alloc mem for "
 1050                                 "fw_passthru data_buf",
 1051                                 error);
 1052                         goto fw_passthru_err;
 1053                 }
 1054                 /* Copy the payload. */
 1055                 if ((error = copyin((TW_VOID *)(user_buf->pdata), 
 1056                         req->data,
 1057                         user_buf->driver_pkt.buffer_length)) != 0) {
 1058                         tw_osli_printf(sc, "error = %d",
 1059                                 TW_CL_SEVERITY_ERROR_STRING,
 1060                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1061                                 0x2017,
 1062                                 "Could not copyin fw_passthru data_buf",
 1063                                 error);
 1064                         goto fw_passthru_err;
 1065                 }
 1066                 pt_req->sgl_entries = 1; /* will be updated during mapping */
 1067                 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
 1068                         TW_OSLI_REQ_FLAGS_DATA_OUT);
 1069         } else
 1070                 pt_req->sgl_entries = 0; /* no payload */
 1071 
 1072         pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
 1073         pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
 1074 
 1075         if ((error = tw_osli_map_request(req)))
 1076                 goto fw_passthru_err;
 1077 
 1078         end_time = tw_osl_get_local_time() + timeout;
 1079         while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
 1080                 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
 1081                 
 1082                 error = tsleep(req, PRIBIO, "twa_passthru", timeout * hz);
 1083 
 1084                 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
 1085                         error = 0;
 1086                 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
 1087 
 1088                 if (! error) {
 1089                         if (((error = req->error_code)) ||
 1090                                 ((error = (req->state !=
 1091                                 TW_OSLI_REQ_STATE_COMPLETE))) ||
 1092                                 ((error = req_pkt->status)))
 1093                                 goto fw_passthru_err;
 1094                         break;
 1095                 }
 1096 
 1097                 if (req_pkt->status) {
 1098                         error = req_pkt->status;
 1099                         goto fw_passthru_err;
 1100                 }
 1101 
 1102                 if (error == EWOULDBLOCK) {
 1103                         /* Time out! */
 1104                         tw_osli_printf(sc, "request = %p",
 1105                                 TW_CL_SEVERITY_ERROR_STRING,
 1106                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1107                                 0x2018,
 1108                                 "Passthru request timed out!",
 1109                                 req);
 1110                         /*
 1111                          * Should I check here if the timeout happened
 1112                          * because of yet another reset, and not do a
 1113                          * second reset?
 1114                          */
 1115                         tw_cl_reset_ctlr(&sc->ctlr_handle);
 1116                         /*
 1117                          * Don't touch req after a reset.  It (and any
 1118                          * associated data) will already have been
 1119                          * freed by the callback.  Just return.
 1120                          */
 1121                         user_buf->driver_pkt.os_status = error;
 1122                         return(ETIMEDOUT);
 1123                 }
 1124                 /* 
 1125                  * Either the request got completed, or we were woken up by a
 1126                  * signal.  Calculate the new timeout, in case it was the latter.
 1127                  */
 1128                 timeout = (end_time - tw_osl_get_local_time());
 1129         }
 1130 
 1131         /* If there was a payload, copy it back. */
 1132         if ((!error) && (req->length))
 1133                 if ((error = copyout(req->data, user_buf->pdata,
 1134                         user_buf->driver_pkt.buffer_length)))
 1135                         tw_osli_printf(sc, "error = %d",
 1136                                 TW_CL_SEVERITY_ERROR_STRING,
 1137                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1138                                 0x2019,
 1139                                 "Could not copyout fw_passthru data_buf",
 1140                                 error);
 1141         
 1142 fw_passthru_err:
 1143         /*
 1144          * Print the failure message.  For some reason, on certain OS versions,
 1145          * printing this error message during reset hangs the display (although
 1146          * the rest of the system is running fine.  So, don't print it if the
 1147          * failure was due to a reset.
 1148          */
 1149         if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
 1150                 tw_osli_printf(sc, "error = %d",                
 1151                         TW_CL_SEVERITY_ERROR_STRING,
 1152                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1153                         0x201A,
 1154                         "Firmware passthru failed!",
 1155                         error);
 1156 
 1157         user_buf->driver_pkt.os_status = error;
 1158         /* Free resources. */
 1159         if (req->data)
 1160                 free(req->data, TW_OSLI_MALLOC_CLASS);
 1161         tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
 1162         return(error);
 1163 }
 1164 
 1165 
 1166 
 1167 /*
 1168  * Function name:       tw_osl_complete_passthru
 1169  * Description:         Called to complete passthru requests.
 1170  *
 1171  * Input:               req_handle      -- ptr to request handle
 1172  * Output:              None
 1173  * Return value:        None
 1174  */
 1175 TW_VOID
 1176 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
 1177 {
 1178         struct tw_osli_req_context      *req = req_handle->osl_req_ctxt;
 1179         struct twa_softc                *sc = req->ctlr;
 1180 
 1181         tw_osli_dbg_dprintf(5, sc, "entered");
 1182 
 1183         if (req->state != TW_OSLI_REQ_STATE_BUSY) {
 1184                 tw_osli_printf(sc, "request = %p, status = %d",
 1185                         TW_CL_SEVERITY_ERROR_STRING,
 1186                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1187                         0x201B,
 1188                         "Unposted command completed!!",
 1189                         req, req->state);
 1190         }
 1191 
 1192         /*
 1193          * Remove request from the busy queue.  Just mark it complete.
 1194          * There's no need to move it into the complete queue as we are
 1195          * going to be done with it right now.
 1196          */
 1197         req->state = TW_OSLI_REQ_STATE_COMPLETE;
 1198         tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
 1199 
 1200         tw_osli_unmap_request(req);
 1201 
 1202         /*
 1203          * Don't do a wake up if there was an error even before the request
 1204          * was sent down to the Common Layer, and we hadn't gotten an
 1205          * EINPROGRESS.  The request originator will then be returned an
 1206          * error, and he can do the clean-up.
 1207          */
 1208         if ((req->error_code) &&
 1209                 (!(req->state & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
 1210                 return;
 1211 
 1212         if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1213                 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
 1214                         /* Wake up the sleeping command originator. */
 1215                         tw_osli_dbg_dprintf(5, sc,
 1216                                 "Waking up originator of request %p", req);
 1217                         req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
 1218                         wakeup_one(req);
 1219                 } else {
 1220                         /*
 1221                          * If the request completed even before tsleep
 1222                          * was called, simply return.
 1223                          */
 1224                         if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
 1225                                 return;
 1226 
 1227                         tw_osli_printf(sc, "request = %p",
 1228                                 TW_CL_SEVERITY_ERROR_STRING,
 1229                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1230                                 0x201C,
 1231                                 "Passthru callback called, "
 1232                                 "and caller not sleeping",
 1233                                 req);
 1234                 }
 1235         } else {
 1236                 tw_osli_printf(sc, "request = %p",
 1237                         TW_CL_SEVERITY_ERROR_STRING,
 1238                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1239                         0x201D,
 1240                         "Passthru callback called for non-passthru request",
 1241                         req);
 1242         }
 1243 }
 1244 
 1245 
 1246 
 1247 /*
 1248  * Function name:       tw_osli_get_request
 1249  * Description:         Gets a request pkt from the free queue.
 1250  *
 1251  * Input:               sc      -- ptr to OSL internal ctlr context
 1252  * Output:              None
 1253  * Return value:        ptr to request pkt      -- success
 1254  *                      NULL                    -- failure
 1255  */
 1256 struct tw_osli_req_context *
 1257 tw_osli_get_request(struct twa_softc *sc)
 1258 {
 1259         struct tw_osli_req_context      *req;
 1260 
 1261         tw_osli_dbg_dprintf(4, sc, "entered");
 1262 
 1263         /* Get a free request packet. */
 1264         req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
 1265 
 1266         /* Initialize some fields to their defaults. */
 1267         if (req) {
 1268                 req->req_handle.osl_req_ctxt = NULL;
 1269                 req->req_handle.cl_req_ctxt = NULL;
 1270                 req->data = NULL;
 1271                 req->length = 0;
 1272                 req->real_data = NULL;
 1273                 req->real_length = 0;
 1274                 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
 1275                 req->flags = 0;
 1276                 req->error_code = 0;
 1277                 req->orig_req = NULL;
 1278 
 1279 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
 1280 
 1281                 /* Don't zero dma_mem & dma_mem_phys in req_pkt. */
 1282                 req->req_pkt.cmd = 0;
 1283                 req->req_pkt.flags = 0;
 1284                 req->req_pkt.status = 0;
 1285                 req->req_pkt.tw_osl_callback = NULL;
 1286                 bzero(&(req->req_pkt.gen_req_pkt),
 1287                         sizeof(req->req_pkt.gen_req_pkt));
 1288 
 1289 #else /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
 1290 
 1291                 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
 1292 
 1293 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
 1294         }
 1295         return(req);
 1296 }
 1297 
 1298 
 1299 
 1300 /*
 1301  * Function name:       twa_map_load_data_callback
 1302  * Description:         Callback of bus_dmamap_load for the buffer associated
 1303  *                      with data.  Updates the cmd pkt (size/sgl_entries
 1304  *                      fields, as applicable) to reflect the number of sg
 1305  *                      elements.
 1306  *
 1307  * Input:               arg     -- ptr to OSL internal request context
 1308  *                      segs    -- ptr to a list of segment descriptors
 1309  *                      nsegments--# of segments
 1310  *                      error   -- 0 if no errors encountered before callback,
 1311  *                                 non-zero if errors were encountered
 1312  * Output:              None
 1313  * Return value:        None
 1314  */
 1315 static TW_VOID
 1316 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
 1317         TW_INT32 nsegments, TW_INT32 error)
 1318 {
 1319         struct tw_osli_req_context      *req =
 1320                 (struct tw_osli_req_context *)arg;
 1321         struct twa_softc                *sc = req->ctlr;
 1322         struct tw_cl_req_packet         *req_pkt = &(req->req_pkt);
 1323 
 1324         tw_osli_dbg_dprintf(10, sc, "entered");
 1325 
 1326         /* Mark the request as currently being processed. */
 1327         req->state = TW_OSLI_REQ_STATE_BUSY;
 1328         /* Move the request into the busy queue. */
 1329         tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
 1330 
 1331         req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
 1332         if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
 1333                 tw_osli_allow_new_requests(sc, (TW_VOID *)(req->orig_req));
 1334 
 1335         if (error == EFBIG) {
 1336                 req->error_code = error;
 1337                 goto out;
 1338         }
 1339 
 1340         if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1341                 struct tw_cl_passthru_req_packet        *pt_req;
 1342 
 1343                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
 1344                         bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
 1345                                 BUS_DMASYNC_PREREAD);
 1346 
 1347                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
 1348                         /* 
 1349                          * If we're using an alignment buffer, and we're
 1350                          * writing data, copy the real data out.
 1351                          */
 1352                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1353                                 bcopy(req->real_data, req->data, req->real_length);
 1354                         bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
 1355                                 BUS_DMASYNC_PREWRITE);
 1356                 }
 1357 
 1358                 pt_req = &(req_pkt->gen_req_pkt.pt_req);
 1359                 pt_req->sg_list = (TW_UINT8 *)segs;
 1360                 pt_req->sgl_entries += (nsegments - 1);
 1361                 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
 1362                         &(req->req_handle));
 1363         } else {
 1364                 struct tw_cl_scsi_req_packet    *scsi_req;
 1365 
 1366                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
 1367                         bus_dmamap_sync(sc->dma_tag, req->dma_map,
 1368                                 BUS_DMASYNC_PREREAD);
 1369 
 1370                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
 1371                         /* 
 1372                          * If we're using an alignment buffer, and we're
 1373                          * writing data, copy the real data out.
 1374                          */
 1375                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1376                                 bcopy(req->real_data, req->data, req->real_length);
 1377                         bus_dmamap_sync(sc->dma_tag, req->dma_map,
 1378                                 BUS_DMASYNC_PREWRITE);
 1379                 }
 1380 
 1381                 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
 1382                 scsi_req->sg_list = (TW_UINT8 *)segs;
 1383                 scsi_req->sgl_entries += (nsegments - 1);
 1384                 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
 1385                         &(req->req_handle));
 1386         }
 1387 
 1388 out:
 1389         if (error) {
 1390                 req->error_code = error;
 1391                 req_pkt->tw_osl_callback(&(req->req_handle));
 1392                 /*
 1393                  * If the caller had been returned EINPROGRESS, and he has
 1394                  * registered a callback for handling completion, the callback
 1395                  * will never get called because we were unable to submit the
 1396                  * request.  So, free up the request right here.
 1397                  */
 1398                 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
 1399                         tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
 1400         }
 1401 }
 1402 
 1403 
 1404 
 1405 /*
 1406  * Function name:       twa_map_load_callback
 1407  * Description:         Callback of bus_dmamap_load for the buffer associated
 1408  *                      with a cmd pkt.
 1409  *
 1410  * Input:               arg     -- ptr to variable to hold phys addr
 1411  *                      segs    -- ptr to a list of segment descriptors
 1412  *                      nsegments--# of segments
 1413  *                      error   -- 0 if no errors encountered before callback,
 1414  *                                 non-zero if errors were encountered
 1415  * Output:              None
 1416  * Return value:        None
 1417  */
 1418 static TW_VOID
 1419 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
 1420         TW_INT32 nsegments, TW_INT32 error)
 1421 {
 1422         *((bus_addr_t *)arg) = segs[0].ds_addr;
 1423 }
 1424 
 1425 
 1426 
 1427 /*
 1428  * Function name:       tw_osli_map_request
 1429  * Description:         Maps a cmd pkt and data associated with it, into
 1430  *                      DMA'able memory.
 1431  *
 1432  * Input:               req     -- ptr to request pkt
 1433  * Output:              None
 1434  * Return value:        0       -- success
 1435  *                      non-zero-- failure
 1436  */
 1437 TW_INT32
 1438 tw_osli_map_request(struct tw_osli_req_context *req)
 1439 {
 1440         struct twa_softc        *sc = req->ctlr;
 1441         TW_INT32                error = 0;
 1442 
 1443         tw_osli_dbg_dprintf(10, sc, "entered");
 1444 
 1445         /* If the command involves data, map that too. */
 1446         if (req->data != NULL) {
 1447                 /*
 1448                  * It's sufficient for the data pointer to be 4-byte aligned
 1449                  * to work with 9000.  However, if 4-byte aligned addresses
 1450                  * are passed to bus_dmamap_load, we can get back sg elements
 1451                  * that are not 512-byte multiples in size.  So, we will let
 1452                  * only those buffers that are 512-byte aligned to pass
 1453                  * through, and bounce the rest, so as to make sure that we
 1454                  * always get back sg elements that are 512-byte multiples
 1455                  * in size.
 1456                  */
 1457                 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
 1458                         (req->length % sc->sg_size_factor)) {
 1459                         req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
 1460                         /* Save original data pointer and length. */
 1461                         req->real_data = req->data;
 1462                         req->real_length = req->length;
 1463                         req->length = (req->length +
 1464                                 (sc->sg_size_factor - 1)) &
 1465                                 ~(sc->sg_size_factor - 1);
 1466                         req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
 1467                                         M_NOWAIT);
 1468                         if (req->data == NULL) {
 1469                                 tw_osli_printf(sc, "error = %d",
 1470                                         TW_CL_SEVERITY_ERROR_STRING,
 1471                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
 1472                                         0x201E,
 1473                                         "Failed to allocate memory "
 1474                                         "for bounce buffer",
 1475                                         ENOMEM);
 1476                                 /* Restore original data pointer and length. */
 1477                                 req->data = req->real_data;
 1478                                 req->length = req->real_length;
 1479                                 return(ENOMEM);
 1480                         }
 1481                 }
 1482         
 1483                 /*
 1484                  * Map the data buffer into bus space and build the SG list.
 1485                  */
 1486                 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1487                         /* Lock against multiple simultaneous ioctl calls. */
 1488                         mtx_lock_spin(sc->io_lock);
 1489                         error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
 1490                                 req->data, req->length,
 1491                                 twa_map_load_data_callback, req,
 1492                                 BUS_DMA_WAITOK);
 1493                         mtx_unlock_spin(sc->io_lock);
 1494                 } else {
 1495                         /*
 1496                          * There's only one CAM I/O thread running at a time.
 1497                          * So, there's no need to hold the io_lock.
 1498                          */
 1499                         error = bus_dmamap_load(sc->dma_tag, req->dma_map,
 1500                                 req->data, req->length,
 1501                                 twa_map_load_data_callback, req,
 1502                                 BUS_DMA_WAITOK);
 1503                 }
 1504                 
 1505                 if (!error)
 1506                         error = req->error_code;
 1507                 else {
 1508                         if (error == EINPROGRESS) {
 1509                                 /*
 1510                                  * Specifying sc->io_lock as the lockfuncarg
 1511                                  * in ...tag_create should protect the access
 1512                                  * of ...FLAGS_MAPPED from the callback.
 1513                                  */
 1514                                 mtx_lock_spin(sc->io_lock);
 1515                                 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED)) {
 1516                                         req->flags |=
 1517                                                 TW_OSLI_REQ_FLAGS_IN_PROGRESS;
 1518                                         tw_osli_disallow_new_requests(sc);
 1519                                 }
 1520                                 mtx_unlock_spin(sc->io_lock);
 1521                                 error = 0;
 1522                         } else {
 1523                                 /* Free alignment buffer if it was used. */
 1524                                 if (req->flags &
 1525                                         TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
 1526                                         free(req->data, TW_OSLI_MALLOC_CLASS);
 1527                                         /*
 1528                                          * Restore original data pointer
 1529                                          * and length.
 1530                                          */
 1531                                         req->data = req->real_data;
 1532                                         req->length = req->real_length;
 1533                                 }
 1534                         }
 1535                 }
 1536 
 1537         } else {
 1538                 /* Mark the request as currently being processed. */
 1539                 req->state = TW_OSLI_REQ_STATE_BUSY;
 1540                 /* Move the request into the busy queue. */
 1541                 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
 1542                 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
 1543                         error = tw_cl_fw_passthru(&sc->ctlr_handle,
 1544                                         &(req->req_pkt), &(req->req_handle));
 1545                 else
 1546                         error = tw_cl_start_io(&sc->ctlr_handle,
 1547                                         &(req->req_pkt), &(req->req_handle));
 1548                 if (error) {
 1549                         req->error_code = error;
 1550                         req->req_pkt.tw_osl_callback(&(req->req_handle));
 1551                 }
 1552         }
 1553         return(error);
 1554 }
 1555 
 1556 
 1557 
 1558 /*
 1559  * Function name:       tw_osli_unmap_request
 1560  * Description:         Undoes the mapping done by tw_osli_map_request.
 1561  *
 1562  * Input:               req     -- ptr to request pkt
 1563  * Output:              None
 1564  * Return value:        None
 1565  */
 1566 TW_VOID
 1567 tw_osli_unmap_request(struct tw_osli_req_context *req)
 1568 {
 1569         struct twa_softc        *sc = req->ctlr;
 1570 
 1571         tw_osli_dbg_dprintf(10, sc, "entered");
 1572 
 1573         /* If the command involved data, unmap that too. */
 1574         if (req->data != NULL) {
 1575                 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
 1576                         /* Lock against multiple simultaneous ioctl calls. */
 1577                         mtx_lock_spin(sc->io_lock);
 1578 
 1579                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
 1580                                 bus_dmamap_sync(sc->ioctl_tag,
 1581                                         sc->ioctl_map, BUS_DMASYNC_POSTREAD);
 1582 
 1583                                 /* 
 1584                                  * If we are using a bounce buffer, and we are
 1585                                  * reading data, copy the real data in.
 1586                                  */
 1587                                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1588                                         bcopy(req->data, req->real_data,
 1589                                                 req->real_length);
 1590                         }
 1591 
 1592                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
 1593                                 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
 1594                                         BUS_DMASYNC_POSTWRITE);
 1595 
 1596                         bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
 1597 
 1598                         mtx_unlock_spin(sc->io_lock);
 1599                 } else {
 1600                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
 1601                                 bus_dmamap_sync(sc->dma_tag,
 1602                                         req->dma_map, BUS_DMASYNC_POSTREAD);
 1603 
 1604                                 /* 
 1605                                  * If we are using a bounce buffer, and we are
 1606                                  * reading data, copy the real data in.
 1607                                  */
 1608                                 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
 1609                                         bcopy(req->data, req->real_data,
 1610                                                 req->real_length);
 1611                         }
 1612                         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
 1613                                 bus_dmamap_sync(sc->dma_tag, req->dma_map,
 1614                                         BUS_DMASYNC_POSTWRITE);
 1615 
 1616                         bus_dmamap_unload(sc->dma_tag, req->dma_map);
 1617                 }
 1618         }
 1619 
 1620         /* Free alignment buffer if it was used. */
 1621         if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
 1622                 free(req->data, TW_OSLI_MALLOC_CLASS);
 1623                 /* Restore original data pointer and length. */
 1624                 req->data = req->real_data;
 1625                 req->length = req->real_length;
 1626         }
 1627 }
 1628 
 1629 
 1630 
 1631 #ifdef TW_OSL_DEBUG
 1632 
 1633 TW_VOID twa_report_stats(TW_VOID);
 1634 TW_VOID twa_reset_stats(TW_VOID);
 1635 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
 1636 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
 1637 
 1638 
 1639 /*
 1640  * Function name:       twa_report_stats
 1641  * Description:         For being called from ddb.  Calls functions that print
 1642  *                      OSL and CL internal stats for the controller.
 1643  *
 1644  * Input:               None
 1645  * Output:              None
 1646  * Return value:        None
 1647  */
 1648 TW_VOID
 1649 twa_report_stats(TW_VOID)
 1650 {
 1651         struct twa_softc        *sc;
 1652         TW_INT32                i;
 1653 
 1654         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
 1655                 tw_osli_print_ctlr_stats(sc);
 1656                 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
 1657         }
 1658 }
 1659 
 1660 
 1661 
 1662 /*
 1663  * Function name:       tw_osli_print_ctlr_stats
 1664  * Description:         For being called from ddb.  Prints OSL controller stats
 1665  *
 1666  * Input:               sc      -- ptr to OSL internal controller context
 1667  * Output:              None
 1668  * Return value:        None
 1669  */
 1670 TW_VOID
 1671 tw_osli_print_ctlr_stats(struct twa_softc *sc)
 1672 {
 1673         twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
 1674         twa_printf(sc, "OSLq type  current  max\n");
 1675         twa_printf(sc, "free      %04d     %04d\n",
 1676                 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
 1677                 sc->q_stats[TW_OSLI_FREE_Q].max_len);
 1678         twa_printf(sc, "busy      %04d     %04d\n",
 1679                 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
 1680                 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
 1681 }       
 1682 
 1683 
 1684 
 1685 /*
 1686  * Function name:       twa_print_req_info
 1687  * Description:         For being called from ddb.  Calls functions that print
 1688  *                      OSL and CL internal details for the request.
 1689  *
 1690  * Input:               req     -- ptr to OSL internal request context
 1691  * Output:              None
 1692  * Return value:        None
 1693  */
 1694 TW_VOID
 1695 twa_print_req_info(struct tw_osli_req_context *req)
 1696 {
 1697         struct twa_softc        *sc = req->ctlr;
 1698 
 1699         twa_printf(sc, "OSL details for request:\n");
 1700         twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
 1701                 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
 1702                 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
 1703                 "next_req = %p, prev_req = %p, dma_map = %p\n",
 1704                 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
 1705                 req->data, req->length, req->real_data, req->real_length,
 1706                 req->state, req->flags, req->error_code, req->orig_req,
 1707                 req->link.next, req->link.prev, req->dma_map);
 1708         tw_cl_print_req_info(&(req->req_handle));
 1709 }
 1710 
 1711 
 1712 
 1713 /*
 1714  * Function name:       twa_reset_stats
 1715  * Description:         For being called from ddb.
 1716  *                      Resets some OSL controller stats.
 1717  *
 1718  * Input:               None
 1719  * Output:              None
 1720  * Return value:        None
 1721  */
 1722 TW_VOID
 1723 twa_reset_stats(TW_VOID)
 1724 {
 1725         struct twa_softc        *sc;
 1726         TW_INT32                i;
 1727 
 1728         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
 1729                 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
 1730                 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
 1731                 tw_cl_reset_stats(&sc->ctlr_handle);
 1732         }
 1733 }
 1734 
 1735 #endif /* TW_OSL_DEBUG */
 1736 
 1737 
 1738 
 1739 #ifdef TW_OSL_DEBUG
 1740 
 1741 /*
 1742  * Function name:       tw_osl_dbg_printf
 1743  * Description:         Prints passed info (prefixed by ctlr name)to syslog
 1744  *
 1745  * Input:               ctlr_handle -- controller handle
 1746  *                      fmt -- format string for the arguments to follow
 1747  *                      ... -- variable number of arguments, to be printed
 1748  *                              based on the fmt string
 1749  * Output:              None
 1750  * Return value:        Number of bytes printed
 1751  */
 1752 TW_INT32
 1753 tw_osl_dbg_printf(struct tw_cl_ctlr_handle *ctlr_handle,
 1754         const TW_INT8 *fmt, ...)
 1755 {
 1756         va_list         args;
 1757         TW_INT32        bytes_printed;
 1758 
 1759         bytes_printed = device_print_prettyname(((struct twa_softc *)
 1760                                 (ctlr_handle->osl_ctlr_ctxt))->bus_dev);
 1761         va_start(args, fmt);
 1762         bytes_printed += vprintf(fmt, args);
 1763         va_end(args);
 1764         return(bytes_printed);
 1765 }
 1766 
 1767 #endif /* TW_OSL_DEBUG */
 1768 
 1769 
 1770 
 1771 /*
 1772  * Function name:       tw_osl_notify_event
 1773  * Description:         Prints passed event info (prefixed by ctlr name)
 1774  *                      to syslog
 1775  *
 1776  * Input:               ctlr_handle -- controller handle
 1777  *                      event -- ptr to a packet describing the event/error
 1778  * Output:              None
 1779  * Return value:        None
 1780  */
 1781 TW_VOID
 1782 tw_osl_notify_event(struct tw_cl_ctlr_handle *ctlr_handle,
 1783         struct tw_cl_event_packet *event)
 1784 {
 1785         struct twa_softc        *sc =
 1786                 (struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt);
 1787 
 1788         twa_printf(sc, "%s: (0x%02X: 0x%04X): %s: %s\n",
 1789                 event->severity_str,
 1790                 event->event_src,
 1791                 event->aen_code,
 1792                 event->parameter_data +
 1793                         strlen(event->parameter_data) + 1,
 1794                 event->parameter_data);
 1795 }
 1796 
 1797 
 1798 
 1799 /*
 1800  * Function name:       tw_osl_read_reg
 1801  * Description:         Reads a register on the controller
 1802  *
 1803  * Input:               ctlr_handle -- controller handle
 1804  *                      offset -- offset from Base Address
 1805  *                      size -- # of bytes to read
 1806  * Output:              None
 1807  * Return value:        Value read
 1808  */
 1809 TW_UINT32
 1810 tw_osl_read_reg(struct tw_cl_ctlr_handle *ctlr_handle,
 1811         TW_INT32 offset, TW_INT32 size)
 1812 {
 1813         bus_space_tag_t         bus_tag =
 1814                 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_tag;
 1815         bus_space_handle_t      bus_handle =
 1816                 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_handle;
 1817 
 1818         if (size == 4)
 1819                 return((TW_UINT32)bus_space_read_4(bus_tag, bus_handle,
 1820                         offset));
 1821         else if (size == 2)
 1822                 return((TW_UINT32)bus_space_read_2(bus_tag, bus_handle,
 1823                         offset));
 1824         else
 1825                 return((TW_UINT32)bus_space_read_1(bus_tag, bus_handle,
 1826                         offset));
 1827 }
 1828 
 1829 
 1830 
 1831 /*
 1832  * Function name:       tw_osl_write_reg
 1833  * Description:         Writes to a register on the controller
 1834  *
 1835  * Input:               ctlr_handle -- controller handle
 1836  *                      offset -- offset from Base Address
 1837  *                      value -- value to write
 1838  *                      size -- # of bytes to write
 1839  * Output:              None
 1840  * Return value:        None
 1841  */
 1842 TW_VOID
 1843 tw_osl_write_reg(struct tw_cl_ctlr_handle *ctlr_handle,
 1844         TW_INT32 offset, TW_INT32 value, TW_INT32 size)
 1845 {
 1846         bus_space_tag_t         bus_tag =
 1847                 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_tag;
 1848         bus_space_handle_t      bus_handle =
 1849                 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_handle;
 1850 
 1851         if (size == 4)
 1852                 bus_space_write_4(bus_tag, bus_handle, offset, value);
 1853         else if (size == 2)
 1854                 bus_space_write_2(bus_tag, bus_handle, offset, (TW_INT16)value);
 1855         else
 1856                 bus_space_write_1(bus_tag, bus_handle, offset, (TW_INT8)value);
 1857 }
 1858 
 1859 
 1860 #ifdef TW_OSL_PCI_CONFIG_ACCESSIBLE
 1861 
 1862 /*
 1863  * Function name:       tw_osl_read_pci_config
 1864  * Description:         Reads from the PCI config space.
 1865  *
 1866  * Input:               sc      -- ptr to per ctlr structure
 1867  *                      offset  -- register offset
 1868  *                      size    -- # of bytes to be read
 1869  * Output:              None
 1870  * Return value:        Value read
 1871  */
 1872 TW_UINT32
 1873 tw_osl_read_pci_config(struct tw_cl_ctlr_handle *ctlr_handle,
 1874         TW_INT32 offset, TW_INT32 size)
 1875 {
 1876         struct twa_softc        *sc =
 1877                 (struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt);
 1878 
 1879         tw_osli_dbg_dprintf(1, sc, "entered");
 1880         return(pci_read_config(sc->bus_dev, offset, size));
 1881 }
 1882 
 1883 /*
 1884  * Function name:       tw_osl_write_pci_config
 1885  * Description:         Writes to the PCI config space.
 1886  *
 1887  * Input:               sc      -- ptr to per ctlr structure
 1888  *                      offset  -- register offset
 1889  *                      value   -- value to write
 1890  *                      size    -- # of bytes to be written
 1891  * Output:              None
 1892  * Return value:        None
 1893  */
 1894 TW_VOID
 1895 tw_osl_write_pci_config(struct tw_cl_ctlr_handle *ctlr_handle,
 1896         TW_INT32 offset, TW_INT32 value, TW_INT32 size)
 1897 {
 1898         struct twa_softc        *sc =
 1899                 (struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt);
 1900 
 1901         tw_osli_dbg_dprintf(1, sc, "entered");
 1902         pci_write_config(sc->bus_dev, offset/*PCIR_STATUS*/, value, size);
 1903 }
 1904 
 1905 #endif /* TW_OSL_PCI_CONFIG_ACCESSIBLE */
 1906 
 1907 
 1908 
 1909 /*
 1910  * Function name:       tw_osl_get_local_time
 1911  * Description:         Gets the local time
 1912  *
 1913  * Input:               None
 1914  * Output:              None
 1915  * Return value:        local time
 1916  */
 1917 TW_TIME
 1918 tw_osl_get_local_time()
 1919 {
 1920         return(time_second - (tz_minuteswest * 60) -
 1921                 (wall_cmos_clock ? adjkerntz : 0));
 1922 }
 1923 
 1924 
 1925 
 1926 /*
 1927  * Function name:       tw_osl_delay
 1928  * Description:         Spin for the specified time
 1929  *
 1930  * Input:               usecs -- micro-seconds to spin
 1931  * Output:              None
 1932  * Return value:        None
 1933  */
 1934 TW_VOID
 1935 tw_osl_delay(TW_INT32 usecs)
 1936 {
 1937         DELAY(usecs);
 1938 }
 1939 
 1940 
 1941 
 1942 #ifdef TW_OSL_CAN_SLEEP
 1943 
 1944 /*
 1945  * Function name:       tw_osl_sleep
 1946  * Description:         Sleep for the specified time, or until woken up
 1947  *
 1948  * Input:               ctlr_handle -- controller handle
 1949  *                      sleep_handle -- handle to sleep on
 1950  *                      timeout -- time period (in ms) to sleep
 1951  * Output:              None
 1952  * Return value:        0 -- successfully woken up
 1953  *                      EWOULDBLOCK -- time out
 1954  *                      ERESTART -- woken up by a signal
 1955  */
 1956 TW_INT32
 1957 tw_osl_sleep(struct tw_cl_ctlr_handle *ctlr_handle,
 1958         TW_SLEEP_HANDLE *sleep_handle, TW_INT32 timeout)
 1959 {
 1960         return(tsleep((TW_VOID *)sleep_handle, PRIBIO, NULL, timeout));
 1961 }
 1962 
 1963 
 1964 
 1965 /*
 1966  * Function name:       tw_osl_wakeup
 1967  * Description:         Wake up a sleeping process
 1968  *
 1969  * Input:               ctlr_handle -- controller handle
 1970  *                      sleep_handle -- handle of sleeping process to be
 1971                                         woken up
 1972  * Output:              None
 1973  * Return value:        None
 1974  */
 1975 TW_VOID
 1976 tw_osl_wakeup(struct tw_cl_ctlr_handle *ctlr_handle,
 1977         TW_SLEEP_HANDLE *sleep_handle)
 1978 {
 1979         wakeup_one(sleep_handle);
 1980 }
 1981 
 1982 #endif /* TW_OSL_CAN_SLEEP */
 1983 

Cache object: 48b9a1dc4332ca39f038bb84243a3ba0


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