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/twa_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) 2003-04 3ware, Inc.
    3  * Copyright (c) 2000 Michael Smith
    4  * Copyright (c) 2000 BSDi
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  *      $FreeBSD: releng/5.3/sys/dev/twa/twa_freebsd.c 134297 2004-08-25 17:15:26Z vkashyap $
   29  */
   30 
   31 /*
   32  * 3ware driver for 9000 series storage controllers.
   33  *
   34  * Author: Vinod Kashyap
   35  */
   36 
   37 
   38 #include <dev/twa/twa_includes.h>
   39 
   40 static void     twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
   41                                                 int nsegments, int error);
   42 static void     twa_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
   43                                                 int nsegments, int error);
   44 
   45 MALLOC_DEFINE(TWA_MALLOC_CLASS, "twa commands", "twa commands");
   46 
   47 
   48 static  d_open_t                twa_open;
   49 static  d_close_t               twa_close;
   50 static  d_ioctl_t               twa_ioctl_wrapper;
   51 
   52 static struct cdevsw twa_cdevsw = {
   53         .d_version =    D_VERSION,
   54         .d_flags =      D_NEEDGIANT,
   55         .d_open =       twa_open,
   56         .d_close =      twa_close,
   57         .d_ioctl =      twa_ioctl_wrapper,
   58         .d_name =       "twa",
   59 };
   60 
   61 static devclass_t       twa_devclass;
   62 
   63 
   64 /*
   65  * Function name:       twa_open
   66  * Description:         Called when the controller is opened.
   67  *                      Simply marks the controller as open.
   68  *
   69  * Input:               dev     -- control device corresponding to the ctlr
   70  *                      flags   -- mode of open
   71  *                      fmt     -- device type (character/block etc.)
   72  *                      proc    -- current process
   73  * Output:              None
   74  * Return value:        0       -- success
   75  *                      non-zero-- failure
   76  */
   77 static int
   78 twa_open(struct cdev *dev, int flags, int fmt, d_thread_t *proc)
   79 {
   80         int                     unit = minor(dev);
   81         struct twa_softc        *sc = devclass_get_softc(twa_devclass, unit);
   82 
   83         sc->twa_state |= TWA_STATE_OPEN;
   84         return(0);
   85 }
   86 
   87 
   88 
   89 /*
   90  * Function name:       twa_close
   91  * Description:         Called when the controller is closed.
   92  *                      Simply marks the controller as not open.
   93  *
   94  * Input:               dev     -- control device corresponding to the ctlr
   95  *                      flags   -- mode of corresponding open
   96  *                      fmt     -- device type (character/block etc.)
   97  *                      proc    -- current process
   98  * Output:              None
   99  * Return value:        0       -- success
  100  *                      non-zero-- failure
  101  */
  102 static int
  103 twa_close(struct cdev *dev, int flags, int fmt, d_thread_t *proc)
  104 {
  105         int                     unit = minor(dev);
  106         struct twa_softc        *sc = devclass_get_softc(twa_devclass, unit);
  107 
  108         sc->twa_state &= ~TWA_STATE_OPEN;
  109         return(0);
  110 }
  111 
  112 
  113 
  114 /*
  115  * Function name:       twa_ioctl_wrapper
  116  * Description:         Called when an ioctl is posted to the controller.
  117  *                      Simply calls the ioctl handler.
  118  *
  119  * Input:               dev     -- control device corresponding to the ctlr
  120  *                      cmd     -- ioctl cmd
  121  *                      buf     -- ptr to buffer in kernel memory, which is
  122  *                                 a copy of the input buffer in user-space
  123  *                      flags   -- mode of corresponding open
  124  *                      proc    -- current process
  125  * Output:              buf     -- ptr to buffer in kernel memory, which will
  126  *                                 be copied to the output buffer in user-space
  127  * Return value:        0       -- success
  128  *                      non-zero-- failure
  129  */
  130 static int
  131 twa_ioctl_wrapper(struct cdev *dev, u_long cmd, caddr_t buf,
  132                                         int flags, d_thread_t *proc)
  133 {
  134         struct twa_softc        *sc = (struct twa_softc *)(dev->si_drv1);
  135 
  136         return(twa_ioctl(sc, cmd, buf));
  137 }
  138 
  139 
  140 
  141 static int      twa_probe (device_t dev);
  142 static int      twa_attach (device_t dev);
  143 static void     twa_free (struct twa_softc *sc);
  144 static int      twa_detach (device_t dev);
  145 static int      twa_shutdown (device_t dev);
  146 static int      twa_suspend (device_t dev);
  147 static int      twa_resume (device_t dev);
  148 static void     twa_pci_intr(void *arg);
  149 static void     twa_intrhook (void *arg);
  150 
  151 static device_method_t  twa_methods[] = {
  152         /* Device interface */
  153         DEVMETHOD(device_probe,         twa_probe),
  154         DEVMETHOD(device_attach,        twa_attach),
  155         DEVMETHOD(device_detach,        twa_detach),
  156         DEVMETHOD(device_shutdown,      twa_shutdown),
  157         DEVMETHOD(device_suspend,       twa_suspend),
  158         DEVMETHOD(device_resume,        twa_resume),
  159 
  160         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  161         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
  162         {0, 0}
  163 };
  164 
  165 static driver_t twa_pci_driver = {
  166         "twa",
  167         twa_methods,
  168         sizeof(struct twa_softc)
  169 };
  170 
  171 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
  172 
  173 
  174 
  175 /*
  176  * Function name:       twa_probe
  177  * Description:         Called at driver load time.  Claims 9000 ctlrs.
  178  *
  179  * Input:               dev     -- bus device corresponding to the ctlr
  180  * Output:              None
  181  * Return value:        <= 0    -- success
  182  *                      > 0     -- failure
  183  */
  184 static int
  185 twa_probe(device_t dev)
  186 {
  187         static u_int8_t first_ctlr = 1;
  188 
  189         twa_dbg_print(3, "entered");
  190 
  191         if ((pci_get_vendor(dev) == TWA_VENDOR_ID) &&
  192                         (pci_get_device(dev) == TWA_DEVICE_ID_9K)) {
  193                 device_set_desc(dev, TWA_DEVICE_NAME);
  194                 /* Print the driver version only once. */
  195                 if (first_ctlr) {
  196                         printf("3ware device driver for 9000 series storage controllers, version: %s\n",
  197                                         TWA_DRIVER_VERSION_STRING);
  198                         first_ctlr = 0;
  199                 }
  200                 return(0);
  201         }
  202         return(ENXIO);
  203 }
  204 
  205 
  206 
  207 /*
  208  * Function name:       twa_attach
  209  * Description:         Allocates pci resources; updates sc; adds a node to the
  210  *                      sysctl tree to expose the driver version; makes calls
  211  *                      to initialize ctlr, and to attach to CAM.
  212  *
  213  * Input:               dev     -- bus device corresponding to the ctlr
  214  * Output:              None
  215  * Return value:        0       -- success
  216  *                      non-zero-- failure
  217  */
  218 static int
  219 twa_attach(device_t dev)
  220 {
  221         struct twa_softc        *sc = device_get_softc(dev);
  222         u_int32_t               command;
  223         int                     res_id;
  224         int                     error;
  225 
  226         twa_dbg_dprint_enter(3, sc);
  227 
  228         /* Initialize the softc structure. */
  229         sc->twa_bus_dev = dev;
  230 
  231         sysctl_ctx_init(&sc->twa_sysctl_ctx);
  232         sc->twa_sysctl_tree = SYSCTL_ADD_NODE(&sc->twa_sysctl_ctx,
  233                                 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
  234                                 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
  235         if (sc->twa_sysctl_tree == NULL) {
  236                 twa_printf(sc, "Cannot add sysctl tree node.\n");
  237                 return(ENXIO);
  238         }
  239         SYSCTL_ADD_STRING(&sc->twa_sysctl_ctx, SYSCTL_CHILDREN(sc->twa_sysctl_tree),
  240                                 OID_AUTO, "driver_version", CTLFLAG_RD,
  241                                 TWA_DRIVER_VERSION_STRING, 0, "TWA driver version");
  242 
  243         /* Make sure we are going to be able to talk to this board. */
  244         command = pci_read_config(dev, PCIR_COMMAND, 2);
  245         if ((command & PCIM_CMD_PORTEN) == 0) {
  246                 twa_printf(sc, "Register window not available.\n");
  247                 return(ENXIO);
  248         }
  249         
  250         /* Force the busmaster enable bit on, in case the BIOS forgot. */
  251         command |= PCIM_CMD_BUSMASTEREN;
  252         pci_write_config(dev, PCIR_COMMAND, command, 2);
  253 
  254         /* Allocate the PCI register window. */
  255         res_id = TWA_IO_CONFIG_REG;
  256         if ((sc->twa_io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &res_id,
  257                                         0, ~0, 1, RF_ACTIVE)) == NULL) {
  258                 twa_printf(sc, "can't allocate register window.\n");
  259                 twa_free(sc);
  260                 return(ENXIO);
  261         }
  262         sc->twa_bus_tag = rman_get_bustag(sc->twa_io_res);
  263         sc->twa_bus_handle = rman_get_bushandle(sc->twa_io_res);
  264 
  265         /* Allocate and connect our interrupt. */
  266         res_id = 0;
  267         if ((sc->twa_irq_res = bus_alloc_resource(sc->twa_bus_dev, SYS_RES_IRQ,
  268                                         &res_id, 0, ~0, 1,
  269                                         RF_SHAREABLE | RF_ACTIVE)) == NULL) {
  270                 twa_printf(sc, "Can't allocate interrupt.\n");
  271                 twa_free(sc);
  272                 return(ENXIO);
  273         }
  274         if (bus_setup_intr(sc->twa_bus_dev, sc->twa_irq_res, INTR_TYPE_CAM,
  275                                 twa_pci_intr, sc, &sc->twa_intr_handle)) {
  276                 twa_printf(sc, "Can't set up interrupt.\n");
  277                 twa_free(sc);
  278                 return(ENXIO);
  279         }
  280 
  281         /* Initialize the driver for this controller. */
  282         if ((error = twa_setup(sc))) {
  283                 twa_free(sc);
  284                 return(error);
  285         }
  286 
  287         /* Print some information about the controller and configuration. */
  288         twa_describe_controller(sc);
  289 
  290         /* Create the control device. */
  291         sc->twa_ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->twa_bus_dev),
  292                                         UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
  293                                         "twa%d", device_get_unit(sc->twa_bus_dev));
  294         sc->twa_ctrl_dev->si_drv1 = sc;
  295 
  296         /*
  297          * Schedule ourselves to bring the controller up once interrupts are
  298          * available.  This isn't strictly necessary, since we disable
  299          * interrupts while probing the controller, but it is more in keeping
  300          * with common practice for other disk devices.
  301          */
  302         sc->twa_ich.ich_func = twa_intrhook;
  303         sc->twa_ich.ich_arg = sc;
  304         if (config_intrhook_establish(&sc->twa_ich) != 0) {
  305                 twa_printf(sc, "Can't establish configuration hook.\n");
  306                 twa_free(sc);
  307                 return(ENXIO);
  308         }
  309 
  310         if ((error = twa_cam_setup(sc))) {
  311                 twa_free(sc);
  312                 return(error);
  313         }
  314         return(0);
  315 }
  316 
  317 
  318 
  319 /*
  320  * Function name:       twa_free
  321  * Description:         Performs clean-up at the time of going down.
  322  *
  323  * Input:               sc      -- ptr to per ctlr structure
  324  * Output:              None
  325  * Return value:        None
  326  */
  327 static void
  328 twa_free(struct twa_softc *sc)
  329 {
  330         struct twa_request      *tr;
  331 
  332         twa_dbg_dprint_enter(3, sc);
  333 
  334         /* Detach from CAM */
  335         twa_cam_detach(sc);
  336 
  337         /* Destroy dma handles. */
  338 
  339         bus_dmamap_unload(sc->twa_dma_tag, sc->twa_cmd_map); 
  340         while ((tr = twa_dequeue_free(sc)) != NULL)
  341                 bus_dmamap_destroy(sc->twa_dma_tag, tr->tr_dma_map);
  342 
  343         /* Free all memory allocated so far. */
  344         if (sc->twa_req_buf)
  345                 free(sc->twa_req_buf, TWA_MALLOC_CLASS);
  346         if (sc->twa_cmd_pkt_buf)
  347                 bus_dmamem_free(sc->twa_dma_tag, sc->twa_cmd_pkt_buf,
  348                                         sc->twa_cmd_map);
  349         if (sc->twa_aen_queue[0])
  350                 free (sc->twa_aen_queue[0], M_DEVBUF);
  351 
  352         /* Destroy the data-transfer DMA tag. */
  353         if (sc->twa_dma_tag)
  354                 bus_dma_tag_destroy(sc->twa_dma_tag);
  355 
  356         /* Disconnect the interrupt handler. */
  357         if (sc->twa_intr_handle)
  358                 bus_teardown_intr(sc->twa_bus_dev, sc->twa_irq_res,
  359                                         sc->twa_intr_handle);
  360         if (sc->twa_irq_res != NULL)
  361                 bus_release_resource(sc->twa_bus_dev, SYS_RES_IRQ,
  362                                         0, sc->twa_irq_res);
  363 
  364         /* Release the register window mapping. */
  365         if (sc->twa_io_res != NULL)
  366                 bus_release_resource(sc->twa_bus_dev, SYS_RES_IOPORT,
  367                                         TWA_IO_CONFIG_REG, sc->twa_io_res);
  368 
  369         /* Destroy the control device. */
  370         if (sc->twa_ctrl_dev != (struct cdev *)NULL)
  371                 destroy_dev(sc->twa_ctrl_dev);
  372 
  373         sysctl_ctx_free(&sc->twa_sysctl_ctx);
  374 }
  375 
  376 
  377 
  378 /*
  379  * Function name:       twa_detach
  380  * Description:         Called when the controller is being detached from
  381  *                      the pci bus.
  382  *
  383  * Input:               dev     -- bus device corresponding to the ctlr
  384  * Output:              None
  385  * Return value:        0       -- success
  386  *                      non-zero-- failure
  387  */
  388 static int
  389 twa_detach(device_t dev)
  390 {
  391         struct twa_softc        *sc = device_get_softc(dev);
  392         int                     s;
  393         int                     error;
  394 
  395         twa_dbg_dprint_enter(3, sc);
  396 
  397         error = EBUSY;
  398         s = splcam();
  399         if (sc->twa_state & TWA_STATE_OPEN)
  400                 goto out;
  401 
  402         /* Shut the controller down. */
  403         if ((error = twa_shutdown(dev)))
  404                 goto out;
  405 
  406         /* Free all resources associated with this controller. */
  407         twa_free(sc);
  408         error = 0;
  409 
  410 out:
  411         splx(s);
  412         return(error);
  413 }
  414 
  415 
  416 
  417 /*
  418  * Function name:       twa_shutdown
  419  * Description:         Called at unload/shutdown time.  Lets the controller
  420  *                      know that we are going down.
  421  *
  422  * Input:               dev     -- bus device corresponding to the ctlr
  423  * Output:              None
  424  * Return value:        0       -- success
  425  *                      non-zero-- failure
  426  */
  427 static int
  428 twa_shutdown(device_t dev)
  429 {
  430         struct twa_softc        *sc = device_get_softc(dev);
  431         int                     s;
  432         int                     error = 0;
  433 
  434         twa_dbg_dprint_enter(3, sc);
  435 
  436         s = splcam();
  437 
  438         /* Disconnect from the controller. */
  439         error = twa_deinit_ctlr(sc);
  440 
  441         splx(s);
  442         return(error);
  443 }
  444 
  445 
  446 
  447 /*
  448  * Function name:       twa_suspend
  449  * Description:         Called to suspend I/O before hot-swapping PCI ctlrs.
  450  *                      Doesn't do much as of now.
  451  *
  452  * Input:               dev     -- bus device corresponding to the ctlr
  453  * Output:              None
  454  * Return value:        0       -- success
  455  *                      non-zero-- failure
  456  */
  457 static int
  458 twa_suspend(device_t dev)
  459 {
  460         struct twa_softc        *sc = device_get_softc(dev);
  461         int                     s;
  462 
  463         twa_dbg_dprint_enter(3, sc);
  464 
  465         s = splcam();
  466         sc->twa_state |= TWA_STATE_SUSPEND;
  467     
  468         twa_disable_interrupts(sc);
  469         splx(s);
  470 
  471         return(1);
  472 }
  473 
  474 
  475 
  476 /*
  477  * Function name:       twa_resume
  478  * Description:         Called to resume I/O after hot-swapping PCI ctlrs.
  479  *                      Doesn't do much as of now.
  480  *
  481  * Input:               dev     -- bus device corresponding to the ctlr
  482  * Output:              None
  483  * Return value:        0       -- success
  484  *                      non-zero-- failure
  485  */
  486 static int
  487 twa_resume(device_t dev)
  488 {
  489         struct twa_softc        *sc = device_get_softc(dev);
  490 
  491         twa_dbg_dprint_enter(3, sc);
  492 
  493         sc->twa_state &= ~TWA_STATE_SUSPEND;
  494         twa_enable_interrupts(sc);
  495 
  496         return(1);
  497 }
  498 
  499 
  500 
  501 /*
  502  * Function name:       twa_pci_intr
  503  * Description:         Interrupt handler.  Wrapper for twa_interrupt.
  504  *
  505  * Input:               arg     -- ptr to per ctlr structure
  506  * Output:              None
  507  * Return value:        None
  508  */
  509 static void
  510 twa_pci_intr(void *arg)
  511 {
  512         struct twa_softc        *sc = (struct twa_softc *)arg;
  513 
  514         twa_interrupt(sc);
  515 }
  516 
  517 
  518 
  519 /*
  520  * Function name:       twa_intrhook
  521  * Description:         Callback for us to enable interrupts.
  522  *
  523  * Input:               arg     -- ptr to per ctlr structure
  524  * Output:              None
  525  * Return value:        None
  526  */
  527 static void
  528 twa_intrhook(void *arg)
  529 {
  530         struct twa_softc        *sc = (struct twa_softc *)arg;
  531 
  532         twa_dbg_dprint(4, sc, "twa_intrhook Entered");
  533 
  534         /* Pull ourselves off the intrhook chain. */
  535         config_intrhook_disestablish(&sc->twa_ich);
  536 
  537         /* Enable interrupts. */
  538         twa_enable_interrupts(sc);
  539 }
  540 
  541 
  542 
  543 /*
  544  * Function name:       twa_write_pci_config
  545  * Description:         Writes to the PCI config space.
  546  *
  547  * Input:               sc      -- ptr to per ctlr structure
  548  *                      value   -- value to be written
  549  *                      size    -- # of bytes to be written
  550  * Output:              None
  551  * Return value:        None
  552  */
  553 void
  554 twa_write_pci_config(struct twa_softc *sc, u_int32_t value, int size)
  555 {
  556         pci_write_config(sc->twa_bus_dev, PCIR_STATUS, value, size);
  557 }
  558 
  559 
  560 
  561 /*
  562  * Function name:       twa_alloc_req_pkts
  563  * Description:         Allocates memory for, and initializes request pkts,
  564  *                      and queues them in the free queue.
  565  *
  566  * Input:               sc      -- ptr to per ctlr structure
  567  *                      num_reqs-- # of request pkts to allocate and initialize.
  568  * Output:              None
  569  * Return value:        0       -- success
  570  *                      non-zero-- failure
  571  */
  572 int
  573 twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
  574 {
  575         struct twa_request      *tr;
  576         int                     i;
  577 
  578         if ((sc->twa_req_buf = malloc(num_reqs * sizeof(struct twa_request),
  579                                         TWA_MALLOC_CLASS, M_NOWAIT)) == NULL)
  580                 return(ENOMEM);
  581 
  582         /* Allocate the bus DMA tag appropriate for PCI. */
  583         if (bus_dma_tag_create(NULL,                    /* parent */
  584                                 TWA_ALIGNMENT,          /* alignment */
  585                                 0,                      /* boundary */
  586                                 BUS_SPACE_MAXADDR,      /* lowaddr */
  587                                 BUS_SPACE_MAXADDR,      /* highaddr */
  588                                 NULL, NULL,             /* filter, filterarg */
  589                                 TWA_Q_LENGTH *
  590                                 (sizeof(struct twa_command_packet)),/* maxsize */
  591                                 TWA_MAX_SG_ELEMENTS,    /* nsegments */
  592                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
  593                                 BUS_DMA_ALLOCNOW,       /* flags */
  594                                 busdma_lock_mutex,      /* lockfunc */
  595                                 &Giant,                 /* lockfuncarg */
  596                                 &sc->twa_dma_tag        /* tag */)) {
  597                 twa_printf(sc, "Can't allocate DMA tag.\n");
  598                 return(ENOMEM);
  599         }
  600 
  601         /* Allocate memory for cmd pkts. */
  602         if (bus_dmamem_alloc(sc->twa_dma_tag,
  603                                 (void *)(&(sc->twa_cmd_pkt_buf)),
  604                                 BUS_DMA_WAITOK, &(sc->twa_cmd_map)))
  605                 return(ENOMEM);
  606 
  607         bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
  608                                 sc->twa_cmd_pkt_buf,
  609                                 num_reqs * sizeof(struct twa_command_packet),
  610                                 twa_setup_request_dmamap, sc, 0);
  611         bzero(sc->twa_req_buf, num_reqs * sizeof(struct twa_request));
  612         bzero(sc->twa_cmd_pkt_buf,
  613                         num_reqs * sizeof(struct twa_command_packet));
  614 
  615         for (i = 0; i < num_reqs; i++) {
  616                 tr = &(sc->twa_req_buf[i]);
  617                 tr->tr_command = &(sc->twa_cmd_pkt_buf[i]);
  618                 tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
  619                                         (i * sizeof(struct twa_command_packet));
  620                 tr->tr_request_id = i;
  621                 tr->tr_sc = sc;
  622                 sc->twa_lookup[i] = tr;
  623 
  624                 /*
  625                  * Create a map for data buffers.  maxsize (256 * 1024) used in
  626                  * bus_dma_tag_create above should suffice the bounce page needs
  627                  * for data buffers, since the max I/O size we support is 128KB.
  628                  * If we supported I/O's bigger than 256KB, we would have to
  629                  * create a second dma_tag, with the appropriate maxsize.
  630                  */
  631                 if (bus_dmamap_create(sc->twa_dma_tag, 0,
  632                                                 &tr->tr_dma_map))
  633                         return(ENOMEM);
  634 
  635                 /* Insert request into the free queue. */
  636                 twa_release_request(tr);
  637         }
  638         return(0);
  639 }
  640 
  641 
  642 
  643 /*
  644  * Function name:       twa_fillin_sgl
  645  * Description:         Fills in the scatter/gather list.
  646  *
  647  * Input:               sgl     -- ptr to sg list
  648  *                      segs    -- ptr to fill the sg list from
  649  *                      nsegments--# of segments
  650  * Output:              None
  651  * Return value:        None
  652  */
  653 static void
  654 twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
  655 {
  656         int     i;
  657 
  658         for (i = 0; i < nsegments; i++) {
  659                 sgl[i].address = segs[i].ds_addr;
  660                 sgl[i].length = (u_int32_t)(segs[i].ds_len);
  661         }
  662 }
  663 
  664 
  665 
  666 /*
  667  * Function name:       twa_setup_data_dmamap
  668  * Description:         Callback of bus_dmamap_load for the buffer associated
  669  *                      with data.  Updates the cmd pkt (size/sgl_entries
  670  *                      fields, as applicable) to reflect the number of sg
  671  *                      elements.
  672  *
  673  * Input:               arg     -- ptr to request pkt
  674  *                      segs    -- ptr to a list of segment descriptors
  675  *                      nsegments--# of segments
  676  *                      error   -- 0 if no errors encountered before callback,
  677  *                                 non-zero if errors were encountered
  678  * Output:              None
  679  * Return value:        None
  680  */
  681 static void
  682 twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
  683                                         int nsegments, int error)
  684 {
  685         struct twa_request              *tr = (struct twa_request *)arg;
  686         struct twa_command_packet       *cmdpkt = tr->tr_command;
  687         struct twa_command_9k           *cmd9k;
  688         union twa_command_7k            *cmd7k;
  689         u_int8_t                        sgl_offset;
  690 
  691         twa_dbg_dprint_enter(10, tr->tr_sc);
  692 
  693         if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) &&
  694                         (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL))
  695                 twa_allow_new_requests(tr->tr_sc, (void *)(tr->tr_private));
  696 
  697         if (error == EFBIG) {
  698                 tr->tr_error = error;
  699                 goto out;
  700         }
  701 
  702         if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
  703                 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
  704                 twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
  705                 cmd9k->sgl_entries += nsegments - 1;
  706         } else {
  707                 /* It's a 7000 command packet. */
  708                 cmd7k = &(cmdpkt->command.cmd_pkt_7k);
  709                 if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
  710                         twa_fillin_sgl((struct twa_sg *)
  711                                         (((u_int32_t *)cmd7k) + sgl_offset),
  712                                         segs, nsegments);
  713                 /* Modify the size field, based on sg address size. */
  714                 cmd7k->generic.size += 
  715                                 ((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
  716         }
  717 
  718         if (tr->tr_flags & TWA_CMD_DATA_IN)
  719                 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
  720                                                         BUS_DMASYNC_PREREAD);
  721         if (tr->tr_flags & TWA_CMD_DATA_OUT) {
  722                 /* 
  723                  * If we're using an alignment buffer, and we're
  724                  * writing data, copy the real data out.
  725                  */
  726                 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
  727                         bcopy(tr->tr_real_data, tr->tr_data, tr->tr_real_length);
  728                 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
  729                                                 BUS_DMASYNC_PREWRITE);
  730         }
  731         error = twa_submit_io(tr);
  732 
  733 out:
  734         if (error) {
  735                 twa_unmap_request(tr);
  736                 /*
  737                  * If the caller had been returned EINPROGRESS, and he has
  738                  * registered a callback for handling completion, the callback
  739                  * will never get called because we were unable to submit the
  740                  * request.  So, free up the request right here.
  741                  */
  742                 if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
  743                         twa_release_request(tr);
  744         }
  745 }
  746 
  747 
  748 
  749 /*
  750  * Function name:       twa_setup_request_dmamap
  751  * Description:         Callback of bus_dmamap_load for the buffer associated
  752  *                      with a cmd pkt.
  753  *
  754  * Input:               arg     -- ptr to request pkt
  755  *                      segs    -- ptr to a list of segment descriptors
  756  *                      nsegments--# of segments
  757  *                      error   -- 0 if no errors encountered before callback,
  758  *                                 non-zero if errors were encountered
  759  * Output:              None
  760  * Return value:        None
  761  */
  762 static void
  763 twa_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
  764                                                 int nsegments, int error)
  765 {
  766         struct twa_softc        *sc = (struct twa_softc *)arg;
  767 
  768         twa_dbg_dprint_enter(10, sc);
  769 
  770         sc->twa_cmd_pkt_phys = segs[0].ds_addr;
  771 }
  772 
  773 
  774 
  775 /*
  776  * Function name:       twa_map_request
  777  * Description:         Maps a cmd pkt and data associated with it, into
  778  *                      DMA'able memory.
  779  *
  780  * Input:               tr      -- ptr to request pkt
  781  * Output:              None
  782  * Return value:        0       -- success
  783  *                      non-zero-- failure
  784  */
  785 int
  786 twa_map_request(struct twa_request *tr)
  787 {
  788         struct twa_softc        *sc = tr->tr_sc;
  789         int                     error = 0;
  790 
  791         twa_dbg_dprint_enter(10, sc);
  792 
  793         /* If the command involves data, map that too. */
  794         if (tr->tr_data != NULL) {
  795                 /*
  796                  * It's sufficient for the data pointer to be 4-byte aligned
  797                  * to work with 9000.  However, if 4-byte aligned addresses
  798                  * are passed to bus_dmamap_load, we can get back sg elements
  799                  * that are not 512-byte multiples in size.  So, we will let
  800                  * only those buffers that are 512-byte aligned to pass
  801                  * through, and bounce the rest, so as to make sure that we
  802                  * always get back sg elements that are 512-byte multiples
  803                  * in size.
  804                  */
  805                 if (((vm_offset_t)tr->tr_data % 512) || (tr->tr_length % 512)) {
  806                         tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
  807                         tr->tr_real_data = tr->tr_data; /* save original data pointer */
  808                         tr->tr_real_length = tr->tr_length; /* save original data length */
  809                         tr->tr_length = (tr->tr_length + 511) & ~511;
  810                         tr->tr_data = malloc(tr->tr_length, TWA_MALLOC_CLASS, M_NOWAIT);
  811                         if (tr->tr_data == NULL) {
  812                                 twa_printf(sc, "%s: malloc failed\n", __func__);
  813                                 tr->tr_data = tr->tr_real_data; /* restore original data pointer */
  814                                 tr->tr_length = tr->tr_real_length; /* restore original data length */
  815                                 return(ENOMEM);
  816                         }
  817                 }
  818         
  819                 /*
  820                  * Map the data buffer into bus space and build the s/g list.
  821                  */
  822                 if ((error = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
  823                                         tr->tr_data, tr->tr_length, 
  824                                         twa_setup_data_dmamap, tr,
  825                                         BUS_DMA_WAITOK))) {
  826                         if (error == EINPROGRESS) {
  827                                 tr->tr_flags |= TWA_CMD_IN_PROGRESS;
  828                                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL)
  829                                         twa_disallow_new_requests(sc);
  830                                 error = 0;
  831                         } else {
  832                                 /* Free alignment buffer if it was used. */
  833                                 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
  834                                         free(tr->tr_data, TWA_MALLOC_CLASS);
  835                                         tr->tr_data = tr->tr_real_data; /* restore 'real' data pointer */
  836                                         tr->tr_length = tr->tr_real_length;/* restore 'real' data length */
  837                                 }
  838                         }
  839                 } else
  840                         error = tr->tr_error;
  841 
  842         } else
  843                 if ((error = twa_submit_io(tr)))
  844                         twa_unmap_request(tr);
  845 
  846         return(error);
  847 }
  848 
  849 
  850 
  851 /*
  852  * Function name:       twa_unmap_request
  853  * Description:         Undoes the mapping done by twa_map_request.
  854  *
  855  * Input:               tr      -- ptr to request pkt
  856  * Output:              None
  857  * Return value:        None
  858  */
  859 void
  860 twa_unmap_request(struct twa_request *tr)
  861 {
  862         struct twa_softc        *sc = tr->tr_sc;
  863         u_int8_t                cmd_status;
  864 
  865         twa_dbg_dprint_enter(10, sc);
  866 
  867         /* If the command involved data, unmap that too. */
  868         if (tr->tr_data != NULL) {
  869                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
  870                         cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
  871                 else
  872                         cmd_status = tr->tr_command->command.cmd_pkt_7k.generic.status;
  873 
  874                 if (tr->tr_flags & TWA_CMD_DATA_IN) {
  875                         bus_dmamap_sync(sc->twa_dma_tag,
  876                                         tr->tr_dma_map, BUS_DMASYNC_POSTREAD);
  877 
  878                         /* 
  879                          * If we are using a bounce buffer, and we are reading
  880                          * data, copy the real data in.
  881                          */
  882                         if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
  883                                 if (cmd_status == 0)
  884                                         bcopy(tr->tr_data, tr->tr_real_data,
  885                                                         tr->tr_real_length);
  886                 }
  887                 if (tr->tr_flags & TWA_CMD_DATA_OUT)
  888                         bus_dmamap_sync(sc->twa_dma_tag, tr->tr_dma_map,
  889                                                         BUS_DMASYNC_POSTWRITE);
  890 
  891                 bus_dmamap_unload(sc->twa_dma_tag, tr->tr_dma_map); 
  892         }
  893 
  894         /* Free alignment buffer if it was used. */
  895         if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
  896                 free(tr->tr_data, TWA_MALLOC_CLASS);
  897                 tr->tr_data = tr->tr_real_data; /* restore 'real' data pointer */
  898                 tr->tr_length = tr->tr_real_length;/* restore 'real' data length */
  899         }
  900 }
  901 
  902 
  903 
  904 #ifdef TWA_DEBUG
  905 void    twa_report(void);
  906 void    twa_reset_stats(void);
  907 void    twa_print_request(struct twa_request *tr, int req_type);
  908 
  909 
  910 
  911 /*
  912  * Function name:       twa_report
  913  * Description:         For being called from ddb.  Prints controller stats,
  914  *                      and requests, if any, that are in the wrong queue.
  915  *
  916  * Input:               None
  917  * Output:              None
  918  * Return value:        None
  919  */
  920 void
  921 twa_report(void)
  922 {
  923         struct twa_softc        *sc;
  924         struct twa_request      *tr;
  925         int                     s;
  926         int                     i;
  927 
  928         s = splcam();
  929         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
  930                 twa_print_controller(sc);
  931                 TAILQ_FOREACH(tr, &sc->twa_busy, tr_link)
  932                         twa_print_request(tr, TWA_CMD_BUSY);
  933                 TAILQ_FOREACH(tr, &sc->twa_complete, tr_link)
  934                         twa_print_request(tr, TWA_CMD_COMPLETE);
  935         }
  936         splx(s);
  937 }
  938 
  939 
  940 
  941 /*
  942  * Function name:       twa_reset_stats
  943  * Description:         For being called from ddb.
  944  *                      Resets some controller stats.
  945  *
  946  * Input:               None
  947  * Output:              None
  948  * Return value:        None
  949  */
  950 void
  951 twa_reset_stats(void)
  952 {
  953         struct twa_softc        *sc;
  954         int                     s;
  955         int                     i;
  956 
  957         s = splcam();
  958         for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
  959                 sc->twa_qstats[TWAQ_FREE].q_max = 0;
  960                 sc->twa_qstats[TWAQ_BUSY].q_max = 0;
  961                 sc->twa_qstats[TWAQ_PENDING].q_max = 0;
  962                 sc->twa_qstats[TWAQ_COMPLETE].q_max = 0;
  963         }
  964         splx(s);
  965 }
  966 
  967 
  968 
  969 /*
  970  * Function name:       twa_print_request
  971  * Description:         Prints a given request if it's in the wrong queue.
  972  *
  973  * Input:               tr      -- ptr to request pkt
  974  *                      req_type-- expected status of the given request
  975  * Output:              None
  976  * Return value:        None
  977  */
  978 void
  979 twa_print_request(struct twa_request *tr, int req_type)
  980 {
  981         struct twa_softc                *sc = tr->tr_sc;
  982         struct twa_command_packet       *cmdpkt = tr->tr_command;
  983         struct twa_command_9k           *cmd9k;
  984         union twa_command_7k            *cmd7k;
  985         u_int8_t                        *cdb;
  986         int                             cmd_phys_addr;
  987 
  988         if (tr->tr_status != req_type) {
  989                 twa_printf(sc, "Invalid %s request %p in queue! req_type = %x, queue_type = %x\n",
  990                         (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ? "INTERNAL" : "EXTERNAL",
  991                         tr, tr->tr_status, req_type);
  992 
  993                 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
  994                         cmd9k = &(cmdpkt->command.cmd_pkt_9k);
  995                         cmd_phys_addr = cmd9k->sg_list[0].address;
  996                         twa_printf(sc, "9K cmd = %x %x %x %x %x %x %x %x %jx\n",
  997                                         cmd9k->command.opcode,
  998                                         cmd9k->command.reserved,
  999                                         cmd9k->unit,
 1000                                         cmd9k->request_id,
 1001                                         cmd9k->status,
 1002                                         cmd9k->sgl_offset,
 1003                                         cmd9k->sgl_entries,
 1004                                         cmd_phys_addr,
 1005                                         (uintmax_t)cmd9k->sg_list[0].length);
 1006                         cdb = (u_int8_t *)(cmdpkt->command.cmd_pkt_9k.cdb);
 1007                         twa_printf(sc, "cdb = %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
 1008                                 cdb[0], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5], cdb[6], cdb[7],
 1009                                 cdb[8], cdb[9], cdb[10], cdb[11], cdb[12], cdb[13], cdb[14], cdb[15]);
 1010                 } else {
 1011                         cmd7k = &(cmdpkt->command.cmd_pkt_7k);
 1012                         twa_printf(sc, "7K cmd = %x %x %x %x %x %x %x %x %x\n",
 1013                                         cmd7k->generic.opcode,
 1014                                         cmd7k->generic.sgl_offset,
 1015                                         cmd7k->generic.size,
 1016                                         cmd7k->generic.request_id,
 1017                                         cmd7k->generic.unit,
 1018                                         cmd7k->generic.host_id,
 1019                                         cmd7k->generic.status,
 1020                                         cmd7k->generic.flags,
 1021                                         cmd7k->generic.count);
 1022                 }
 1023 
 1024                 cmd_phys_addr = (int)(tr->tr_cmd_phys);
 1025                 twa_printf(sc, "cmdphys=0x%x data=%p length=0x%jx\n",
 1026                                 cmd_phys_addr, tr->tr_data, (uintmax_t)tr->tr_length);
 1027                 twa_printf(sc, "req_id=0x%x flags=0x%x callback=%p private=%p\n",
 1028                                         tr->tr_request_id, tr->tr_flags,
 1029                                         tr->tr_callback, tr->tr_private);
 1030         }
 1031 }
 1032 #endif

Cache object: 82247db715c9b43a1cba03b17b3d4abf


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