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/ciss/ciss.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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2001 Michael Smith
    5  * Copyright (c) 2004 Paul Saab
    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$
   30  */
   31 
   32 /*
   33  * Common Interface for SCSI-3 Support driver.
   34  *
   35  * CISS claims to provide a common interface between a generic SCSI
   36  * transport and an intelligent host adapter.
   37  *
   38  * This driver supports CISS as defined in the document "CISS Command
   39  * Interface for SCSI-3 Support Open Specification", Version 1.04,
   40  * Valence Number 1, dated 20001127, produced by Compaq Computer
   41  * Corporation.  This document appears to be a hastily and somewhat
   42  * arbitrarlily cut-down version of a larger (and probably even more
   43  * chaotic and inconsistent) Compaq internal document.  Various
   44  * details were also gleaned from Compaq's "cciss" driver for Linux.
   45  *
   46  * We provide a shim layer between the CISS interface and CAM,
   47  * offloading most of the queueing and being-a-disk chores onto CAM.
   48  * Entry to the driver is via the PCI bus attachment (ciss_probe,
   49  * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
   50  * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
   51  * citizens and we have to fake up some responses to get reasonable
   52  * behaviour out of them.  In addition, the CISS command set is by no
   53  * means adequate to support the functionality of a RAID controller,
   54  * and thus the supported Compaq adapters utilise portions of the
   55  * control protocol from earlier Compaq adapter families.
   56  *
   57  * Note that we only support the "simple" transport layer over PCI.
   58  * This interface (ab)uses the I2O register set (specifically the post
   59  * queues) to exchange commands with the adapter.  Other interfaces
   60  * are available, but we aren't supposed to know about them, and it is
   61  * dubious whether they would provide major performance improvements
   62  * except under extreme load.
   63  *
   64  * Currently the only supported CISS adapters are the Compaq Smart
   65  * Array 5* series (5300, 5i, 532).  Even with only three adapters,
   66  * Compaq still manage to have interface variations.
   67  *
   68  *
   69  * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
   70  * well as Paul Saab at Yahoo! for their assistance in making this
   71  * driver happen.
   72  *
   73  * More thanks must go to John Cagle at HP for the countless hours
   74  * spent making this driver "work" with the MSA* series storage
   75  * enclosures.  Without his help (and nagging), this driver could not
   76  * be used with these enclosures.
   77  */
   78 
   79 #include <sys/param.h>
   80 #include <sys/systm.h>
   81 #include <sys/malloc.h>
   82 #include <sys/kernel.h>
   83 #include <sys/bus.h>
   84 #include <sys/conf.h>
   85 #include <sys/stat.h>
   86 #include <sys/kthread.h>
   87 #include <sys/queue.h>
   88 #include <sys/sysctl.h>
   89 
   90 #include <cam/cam.h>
   91 #include <cam/cam_ccb.h>
   92 #include <cam/cam_periph.h>
   93 #include <cam/cam_sim.h>
   94 #include <cam/cam_xpt_sim.h>
   95 #include <cam/scsi/scsi_all.h>
   96 #include <cam/scsi/scsi_message.h>
   97 
   98 #include <machine/bus.h>
   99 #include <machine/endian.h>
  100 #include <machine/resource.h>
  101 #include <sys/rman.h>
  102 
  103 #include <dev/pci/pcireg.h>
  104 #include <dev/pci/pcivar.h>
  105 
  106 #include <dev/ciss/cissreg.h>
  107 #include <dev/ciss/cissio.h>
  108 #include <dev/ciss/cissvar.h>
  109 
  110 #ifdef CISS_DEBUG
  111 #include "opt_ddb.h"
  112 #endif
  113 
  114 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
  115     "ciss internal data buffers");
  116 
  117 /* pci interface */
  118 static int      ciss_lookup(device_t dev);
  119 static int      ciss_probe(device_t dev);
  120 static int      ciss_attach(device_t dev);
  121 static int      ciss_detach(device_t dev);
  122 static int      ciss_shutdown(device_t dev);
  123 
  124 /* (de)initialisation functions, control wrappers */
  125 static int      ciss_init_pci(struct ciss_softc *sc);
  126 static int      ciss_setup_msix(struct ciss_softc *sc);
  127 static int      ciss_init_perf(struct ciss_softc *sc);
  128 static int      ciss_wait_adapter(struct ciss_softc *sc);
  129 static int      ciss_flush_adapter(struct ciss_softc *sc);
  130 static int      ciss_init_requests(struct ciss_softc *sc);
  131 static void     ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
  132                                         int nseg, int error);
  133 static int      ciss_identify_adapter(struct ciss_softc *sc);
  134 static int      ciss_init_logical(struct ciss_softc *sc);
  135 static int      ciss_init_physical(struct ciss_softc *sc);
  136 static int      ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
  137 static int      ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
  138 static int      ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
  139 static int      ciss_update_config(struct ciss_softc *sc);
  140 static int      ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
  141 static void     ciss_init_sysctl(struct ciss_softc *sc);
  142 static void     ciss_soft_reset(struct ciss_softc *sc);
  143 static void     ciss_free(struct ciss_softc *sc);
  144 static void     ciss_spawn_notify_thread(struct ciss_softc *sc);
  145 static void     ciss_kill_notify_thread(struct ciss_softc *sc);
  146 
  147 /* request submission/completion */
  148 static int      ciss_start(struct ciss_request *cr);
  149 static void     ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
  150 static void     ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
  151 static void     ciss_intr(void *arg);
  152 static void     ciss_perf_intr(void *arg);
  153 static void     ciss_perf_msi_intr(void *arg);
  154 static void     ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
  155 static int      _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
  156 static int      ciss_synch_request(struct ciss_request *cr, int timeout);
  157 static int      ciss_poll_request(struct ciss_request *cr, int timeout);
  158 static int      ciss_wait_request(struct ciss_request *cr, int timeout);
  159 #if 0
  160 static int      ciss_abort_request(struct ciss_request *cr);
  161 #endif
  162 
  163 /* request queueing */
  164 static int      ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
  165 static void     ciss_preen_command(struct ciss_request *cr);
  166 static void     ciss_release_request(struct ciss_request *cr);
  167 
  168 /* request helpers */
  169 static int      ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
  170                                       int opcode, void **bufp, size_t bufsize);
  171 static int      ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
  172 
  173 /* DMA map/unmap */
  174 static int      ciss_map_request(struct ciss_request *cr);
  175 static void     ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
  176                                         int nseg, int error);
  177 static void     ciss_unmap_request(struct ciss_request *cr);
  178 
  179 /* CAM interface */
  180 static int      ciss_cam_init(struct ciss_softc *sc);
  181 static void     ciss_cam_rescan_target(struct ciss_softc *sc,
  182                                        int bus, int target);
  183 static void     ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
  184 static int      ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
  185 static int      ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
  186 static void     ciss_cam_poll(struct cam_sim *sim);
  187 static void     ciss_cam_complete(struct ciss_request *cr);
  188 static void     ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
  189 static int      ciss_name_device(struct ciss_softc *sc, int bus, int target);
  190 
  191 /* periodic status monitoring */
  192 static void     ciss_periodic(void *arg);
  193 static void     ciss_nop_complete(struct ciss_request *cr);
  194 static void     ciss_disable_adapter(struct ciss_softc *sc);
  195 static void     ciss_notify_event(struct ciss_softc *sc);
  196 static void     ciss_notify_complete(struct ciss_request *cr);
  197 static int      ciss_notify_abort(struct ciss_softc *sc);
  198 static int      ciss_notify_abort_bmic(struct ciss_softc *sc);
  199 static void     ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
  200 static void     ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
  201 static void     ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
  202 
  203 /* debugging output */
  204 #ifdef DDB
  205 static void     ciss_print_request(struct ciss_request *cr);
  206 #endif
  207 static void     ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
  208 static const char *ciss_name_ldrive_status(int status);
  209 static int      ciss_decode_ldrive_status(int status);
  210 static const char *ciss_name_ldrive_org(int org);
  211 static const char *ciss_name_command_status(int status);
  212 
  213 /*
  214  * PCI bus interface.
  215  */
  216 static device_method_t ciss_methods[] = {
  217     /* Device interface */
  218     DEVMETHOD(device_probe,     ciss_probe),
  219     DEVMETHOD(device_attach,    ciss_attach),
  220     DEVMETHOD(device_detach,    ciss_detach),
  221     DEVMETHOD(device_shutdown,  ciss_shutdown),
  222     { 0, 0 }
  223 };
  224 
  225 static driver_t ciss_pci_driver = {
  226     "ciss",
  227     ciss_methods,
  228     sizeof(struct ciss_softc)
  229 };
  230 
  231 /*
  232  * Control device interface.
  233  */
  234 static d_open_t         ciss_open;
  235 static d_close_t        ciss_close;
  236 static d_ioctl_t        ciss_ioctl;
  237 
  238 static struct cdevsw ciss_cdevsw = {
  239         .d_version =    D_VERSION,
  240         .d_flags =      0,
  241         .d_open =       ciss_open,
  242         .d_close =      ciss_close,
  243         .d_ioctl =      ciss_ioctl,
  244         .d_name =       "ciss",
  245 };
  246 
  247 /*
  248  * This tunable can be set at boot time and controls whether physical devices
  249  * that are marked hidden by the firmware should be exposed anyways.
  250  */
  251 static unsigned int ciss_expose_hidden_physical = 0;
  252 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
  253 
  254 static unsigned int ciss_nop_message_heartbeat = 0;
  255 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
  256 
  257 /*
  258  * This tunable can force a particular transport to be used:
  259  * <= 0 : use default
  260  *    1 : force simple
  261  *    2 : force performant
  262  */
  263 static int ciss_force_transport = 0;
  264 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
  265 
  266 /*
  267  * This tunable can force a particular interrupt delivery method to be used:
  268  * <= 0 : use default
  269  *    1 : force INTx
  270  *    2 : force MSIX
  271  */
  272 static int ciss_force_interrupt = 0;
  273 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
  274 
  275 
  276 /************************************************************************
  277  * CISS adapters amazingly don't have a defined programming interface
  278  * value.  (One could say some very despairing things about PCI and
  279  * people just not getting the general idea.)  So we are forced to
  280  * stick with matching against subvendor/subdevice, and thus have to
  281  * be updated for every new CISS adapter that appears.
  282  */
  283 #define CISS_BOARD_UNKNWON      0
  284 #define CISS_BOARD_SA5          1
  285 #define CISS_BOARD_SA5B         2
  286 #define CISS_BOARD_NOMSI        (1<<4)
  287 #define CISS_BOARD_SIMPLE       (1<<5)
  288 
  289 static struct
  290 {
  291     u_int16_t   subvendor;
  292     u_int16_t   subdevice;
  293     int         flags;
  294     char        *desc;
  295 } ciss_vendor_data[] = {
  296     { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
  297                                                         "Compaq Smart Array 5300" },
  298     { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
  299     { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
  300     { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
  301     { 0x0e11, 0x4091, CISS_BOARD_SA5,   "HP Smart Array 6i" },
  302     { 0x0e11, 0x409A, CISS_BOARD_SA5,   "HP Smart Array 641" },
  303     { 0x0e11, 0x409B, CISS_BOARD_SA5,   "HP Smart Array 642" },
  304     { 0x0e11, 0x409C, CISS_BOARD_SA5,   "HP Smart Array 6400" },
  305     { 0x0e11, 0x409D, CISS_BOARD_SA5,   "HP Smart Array 6400 EM" },
  306     { 0x103C, 0x3211, CISS_BOARD_SA5,   "HP Smart Array E200i" },
  307     { 0x103C, 0x3212, CISS_BOARD_SA5,   "HP Smart Array E200" },
  308     { 0x103C, 0x3213, CISS_BOARD_SA5,   "HP Smart Array E200i" },
  309     { 0x103C, 0x3214, CISS_BOARD_SA5,   "HP Smart Array E200i" },
  310     { 0x103C, 0x3215, CISS_BOARD_SA5,   "HP Smart Array E200i" },
  311     { 0x103C, 0x3220, CISS_BOARD_SA5,   "HP Smart Array" },
  312     { 0x103C, 0x3222, CISS_BOARD_SA5,   "HP Smart Array" },
  313     { 0x103C, 0x3223, CISS_BOARD_SA5,   "HP Smart Array P800" },
  314     { 0x103C, 0x3225, CISS_BOARD_SA5,   "HP Smart Array P600" },
  315     { 0x103C, 0x3230, CISS_BOARD_SA5,   "HP Smart Array" },
  316     { 0x103C, 0x3231, CISS_BOARD_SA5,   "HP Smart Array" },
  317     { 0x103C, 0x3232, CISS_BOARD_SA5,   "HP Smart Array" },
  318     { 0x103C, 0x3233, CISS_BOARD_SA5,   "HP Smart Array" },
  319     { 0x103C, 0x3234, CISS_BOARD_SA5,   "HP Smart Array P400" },
  320     { 0x103C, 0x3235, CISS_BOARD_SA5,   "HP Smart Array P400i" },
  321     { 0x103C, 0x3236, CISS_BOARD_SA5,   "HP Smart Array" },
  322     { 0x103C, 0x3237, CISS_BOARD_SA5,   "HP Smart Array E500" },
  323     { 0x103C, 0x3238, CISS_BOARD_SA5,   "HP Smart Array" },
  324     { 0x103C, 0x3239, CISS_BOARD_SA5,   "HP Smart Array" },
  325     { 0x103C, 0x323A, CISS_BOARD_SA5,   "HP Smart Array" },
  326     { 0x103C, 0x323B, CISS_BOARD_SA5,   "HP Smart Array" },
  327     { 0x103C, 0x323C, CISS_BOARD_SA5,   "HP Smart Array" },
  328     { 0x103C, 0x323D, CISS_BOARD_SA5,   "HP Smart Array P700m" },
  329     { 0x103C, 0x3241, CISS_BOARD_SA5,   "HP Smart Array P212" },
  330     { 0x103C, 0x3243, CISS_BOARD_SA5,   "HP Smart Array P410" },
  331     { 0x103C, 0x3245, CISS_BOARD_SA5,   "HP Smart Array P410i" },
  332     { 0x103C, 0x3247, CISS_BOARD_SA5,   "HP Smart Array P411" },
  333     { 0x103C, 0x3249, CISS_BOARD_SA5,   "HP Smart Array P812" },
  334     { 0x103C, 0x324A, CISS_BOARD_SA5,   "HP Smart Array P712m" },
  335     { 0x103C, 0x324B, CISS_BOARD_SA5,   "HP Smart Array" },
  336     { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
  337     { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
  338     { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
  339     { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
  340     { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
  341     { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
  342     { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
  343     { 0x103C, 0x1920, CISS_BOARD_SA5,   "HP Smart Array P430i" },
  344     { 0x103C, 0x1921, CISS_BOARD_SA5,   "HP Smart Array P830i" },
  345     { 0x103C, 0x1922, CISS_BOARD_SA5,   "HP Smart Array P430" },
  346     { 0x103C, 0x1923, CISS_BOARD_SA5,   "HP Smart Array P431" },
  347     { 0x103C, 0x1924, CISS_BOARD_SA5,   "HP Smart Array P830" },
  348     { 0x103C, 0x1926, CISS_BOARD_SA5,   "HP Smart Array P731m" },
  349     { 0x103C, 0x1928, CISS_BOARD_SA5,   "HP Smart Array P230i" },
  350     { 0x103C, 0x1929, CISS_BOARD_SA5,   "HP Smart Array P530" },
  351     { 0x103C, 0x192A, CISS_BOARD_SA5,   "HP Smart Array P531" },
  352     { 0x103C, 0x21BD, CISS_BOARD_SA5,   "HP Smart Array P244br" },
  353     { 0x103C, 0x21BE, CISS_BOARD_SA5,   "HP Smart Array P741m" },
  354     { 0x103C, 0x21BF, CISS_BOARD_SA5,   "HP Smart Array H240ar" },
  355     { 0x103C, 0x21C0, CISS_BOARD_SA5,   "HP Smart Array P440ar" },
  356     { 0x103C, 0x21C1, CISS_BOARD_SA5,   "HP Smart Array P840ar" },
  357     { 0x103C, 0x21C2, CISS_BOARD_SA5,   "HP Smart Array P440" },
  358     { 0x103C, 0x21C3, CISS_BOARD_SA5,   "HP Smart Array P441" },
  359     { 0x103C, 0x21C5, CISS_BOARD_SA5,   "HP Smart Array P841" },
  360     { 0x103C, 0x21C6, CISS_BOARD_SA5,   "HP Smart Array H244br" },
  361     { 0x103C, 0x21C7, CISS_BOARD_SA5,   "HP Smart Array H240" },
  362     { 0x103C, 0x21C8, CISS_BOARD_SA5,   "HP Smart Array H241" },
  363     { 0x103C, 0x21CA, CISS_BOARD_SA5,   "HP Smart Array P246br" },
  364     { 0x103C, 0x21CB, CISS_BOARD_SA5,   "HP Smart Array P840" },
  365     { 0x103C, 0x21CC, CISS_BOARD_SA5,   "HP Smart Array P542d" },
  366     { 0x103C, 0x21CD, CISS_BOARD_SA5,   "HP Smart Array P240nr" },
  367     { 0x103C, 0x21CE, CISS_BOARD_SA5,   "HP Smart Array H240nr" },
  368     { 0, 0, 0, NULL }
  369 };
  370 
  371 static devclass_t       ciss_devclass;
  372 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
  373 MODULE_PNP_INFO("U16:vendor;U16:device;", pci, ciss, ciss_vendor_data,
  374     nitems(ciss_vendor_data) - 1);
  375 MODULE_DEPEND(ciss, cam, 1, 1, 1);
  376 MODULE_DEPEND(ciss, pci, 1, 1, 1);
  377 
  378 /************************************************************************
  379  * Find a match for the device in our list of known adapters.
  380  */
  381 static int
  382 ciss_lookup(device_t dev)
  383 {
  384     int         i;
  385 
  386     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
  387         if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
  388             (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
  389             return(i);
  390         }
  391     return(-1);
  392 }
  393 
  394 /************************************************************************
  395  * Match a known CISS adapter.
  396  */
  397 static int
  398 ciss_probe(device_t dev)
  399 {
  400     int         i;
  401 
  402     i = ciss_lookup(dev);
  403     if (i != -1) {
  404         device_set_desc(dev, ciss_vendor_data[i].desc);
  405         return(BUS_PROBE_DEFAULT);
  406     }
  407     return(ENOENT);
  408 }
  409 
  410 /************************************************************************
  411  * Attach the driver to this adapter.
  412  */
  413 static int
  414 ciss_attach(device_t dev)
  415 {
  416     struct ciss_softc   *sc;
  417     int                 error;
  418 
  419     debug_called(1);
  420 
  421 #ifdef CISS_DEBUG
  422     /* print structure/union sizes */
  423     debug_struct(ciss_command);
  424     debug_struct(ciss_header);
  425     debug_union(ciss_device_address);
  426     debug_struct(ciss_cdb);
  427     debug_struct(ciss_report_cdb);
  428     debug_struct(ciss_notify_cdb);
  429     debug_struct(ciss_notify);
  430     debug_struct(ciss_message_cdb);
  431     debug_struct(ciss_error_info_pointer);
  432     debug_struct(ciss_error_info);
  433     debug_struct(ciss_sg_entry);
  434     debug_struct(ciss_config_table);
  435     debug_struct(ciss_bmic_cdb);
  436     debug_struct(ciss_bmic_id_ldrive);
  437     debug_struct(ciss_bmic_id_lstatus);
  438     debug_struct(ciss_bmic_id_table);
  439     debug_struct(ciss_bmic_id_pdrive);
  440     debug_struct(ciss_bmic_blink_pdrive);
  441     debug_struct(ciss_bmic_flush_cache);
  442     debug_const(CISS_MAX_REQUESTS);
  443     debug_const(CISS_MAX_LOGICAL);
  444     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
  445     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
  446     debug_const(CISS_COMMAND_ALLOC_SIZE);
  447     debug_const(CISS_COMMAND_SG_LENGTH);
  448 
  449     debug_type(cciss_pci_info_struct);
  450     debug_type(cciss_coalint_struct);
  451     debug_type(cciss_coalint_struct);
  452     debug_type(NodeName_type);
  453     debug_type(NodeName_type);
  454     debug_type(Heartbeat_type);
  455     debug_type(BusTypes_type);
  456     debug_type(FirmwareVer_type);
  457     debug_type(DriverVer_type);
  458     debug_type(IOCTL_Command_struct);
  459 #endif
  460 
  461     sc = device_get_softc(dev);
  462     sc->ciss_dev = dev;
  463     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
  464     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
  465 
  466     /*
  467      * Do PCI-specific init.
  468      */
  469     if ((error = ciss_init_pci(sc)) != 0)
  470         goto out;
  471 
  472     /*
  473      * Initialise driver queues.
  474      */
  475     ciss_initq_free(sc);
  476     ciss_initq_notify(sc);
  477 
  478     /*
  479      * Initialize device sysctls.
  480      */
  481     ciss_init_sysctl(sc);
  482 
  483     /*
  484      * Initialise command/request pool.
  485      */
  486     if ((error = ciss_init_requests(sc)) != 0)
  487         goto out;
  488 
  489     /*
  490      * Get adapter information.
  491      */
  492     if ((error = ciss_identify_adapter(sc)) != 0)
  493         goto out;
  494 
  495     /*
  496      * Find all the physical devices.
  497      */
  498     if ((error = ciss_init_physical(sc)) != 0)
  499         goto out;
  500 
  501     /*
  502      * Build our private table of logical devices.
  503      */
  504     if ((error = ciss_init_logical(sc)) != 0)
  505         goto out;
  506 
  507     /*
  508      * Enable interrupts so that the CAM scan can complete.
  509      */
  510     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
  511 
  512     /*
  513      * Initialise the CAM interface.
  514      */
  515     if ((error = ciss_cam_init(sc)) != 0)
  516         goto out;
  517 
  518     /*
  519      * Start the heartbeat routine and event chain.
  520      */
  521     ciss_periodic(sc);
  522 
  523    /*
  524      * Create the control device.
  525      */
  526     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
  527                               UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
  528                               "ciss%d", device_get_unit(sc->ciss_dev));
  529     sc->ciss_dev_t->si_drv1 = sc;
  530 
  531     /*
  532      * The adapter is running; synchronous commands can now sleep
  533      * waiting for an interrupt to signal completion.
  534      */
  535     sc->ciss_flags |= CISS_FLAG_RUNNING;
  536 
  537     ciss_spawn_notify_thread(sc);
  538 
  539     error = 0;
  540  out:
  541     if (error != 0) {
  542         /* ciss_free() expects the mutex to be held */
  543         mtx_lock(&sc->ciss_mtx);
  544         ciss_free(sc);
  545     }
  546     return(error);
  547 }
  548 
  549 /************************************************************************
  550  * Detach the driver from this adapter.
  551  */
  552 static int
  553 ciss_detach(device_t dev)
  554 {
  555     struct ciss_softc   *sc = device_get_softc(dev);
  556 
  557     debug_called(1);
  558 
  559     mtx_lock(&sc->ciss_mtx);
  560     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
  561         mtx_unlock(&sc->ciss_mtx);
  562         return (EBUSY);
  563     }
  564 
  565     /* flush adapter cache */
  566     ciss_flush_adapter(sc);
  567 
  568     /* release all resources.  The mutex is released and freed here too. */
  569     ciss_free(sc);
  570 
  571     return(0);
  572 }
  573 
  574 /************************************************************************
  575  * Prepare adapter for system shutdown.
  576  */
  577 static int
  578 ciss_shutdown(device_t dev)
  579 {
  580     struct ciss_softc   *sc = device_get_softc(dev);
  581 
  582     debug_called(1);
  583 
  584     mtx_lock(&sc->ciss_mtx);
  585     /* flush adapter cache */
  586     ciss_flush_adapter(sc);
  587 
  588     if (sc->ciss_soft_reset)
  589         ciss_soft_reset(sc);
  590     mtx_unlock(&sc->ciss_mtx);
  591 
  592     return(0);
  593 }
  594 
  595 static void
  596 ciss_init_sysctl(struct ciss_softc *sc)
  597 {
  598 
  599     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
  600         SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
  601         OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
  602 }
  603 
  604 /************************************************************************
  605  * Perform PCI-specific attachment actions.
  606  */
  607 static int
  608 ciss_init_pci(struct ciss_softc *sc)
  609 {
  610     uintptr_t           cbase, csize, cofs;
  611     uint32_t            method, supported_methods;
  612     int                 error, sqmask, i;
  613     void                *intr;
  614 
  615     debug_called(1);
  616 
  617     /*
  618      * Work out adapter type.
  619      */
  620     i = ciss_lookup(sc->ciss_dev);
  621     if (i < 0) {
  622         ciss_printf(sc, "unknown adapter type\n");
  623         return (ENXIO);
  624     }
  625 
  626     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
  627         sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
  628     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
  629         sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
  630     } else {
  631         /*
  632          * XXX Big hammer, masks/unmasks all possible interrupts.  This should
  633          * work on all hardware variants.  Need to add code to handle the
  634          * "controller crashed" interrupt bit that this unmasks.
  635          */
  636         sqmask = ~0;
  637     }
  638 
  639     /*
  640      * Allocate register window first (we need this to find the config
  641      * struct).
  642      */
  643     error = ENXIO;
  644     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
  645     if ((sc->ciss_regs_resource =
  646          bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
  647                                 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
  648         ciss_printf(sc, "can't allocate register window\n");
  649         return(ENXIO);
  650     }
  651     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
  652     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
  653 
  654     /*
  655      * Find the BAR holding the config structure.  If it's not the one
  656      * we already mapped for registers, map it too.
  657      */
  658     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
  659     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
  660         if ((sc->ciss_cfg_resource =
  661              bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
  662                                     &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
  663             ciss_printf(sc, "can't allocate config window\n");
  664             return(ENXIO);
  665         }
  666         cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
  667         csize = rman_get_end(sc->ciss_cfg_resource) -
  668             rman_get_start(sc->ciss_cfg_resource) + 1;
  669     } else {
  670         cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
  671         csize = rman_get_end(sc->ciss_regs_resource) -
  672             rman_get_start(sc->ciss_regs_resource) + 1;
  673     }
  674     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
  675 
  676     /*
  677      * Use the base/size/offset values we just calculated to
  678      * sanity-check the config structure.  If it's OK, point to it.
  679      */
  680     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
  681         ciss_printf(sc, "config table outside window\n");
  682         return(ENXIO);
  683     }
  684     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
  685     debug(1, "config struct at %p", sc->ciss_cfg);
  686 
  687     /*
  688      * Calculate the number of request structures/commands we are
  689      * going to provide for this adapter.
  690      */
  691     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
  692 
  693     /*
  694      * Validate the config structure.  If we supported other transport
  695      * methods, we could select amongst them at this point in time.
  696      */
  697     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
  698         ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
  699                     sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
  700                     sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
  701         return(ENXIO);
  702     }
  703 
  704     /*
  705      * Select the mode of operation, prefer Performant.
  706      */
  707     if (!(sc->ciss_cfg->supported_methods &
  708         (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
  709         ciss_printf(sc, "No supported transport layers: 0x%x\n",
  710             sc->ciss_cfg->supported_methods);
  711     }
  712 
  713     switch (ciss_force_transport) {
  714     case 1:
  715         supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
  716         break;
  717     case 2:
  718         supported_methods = CISS_TRANSPORT_METHOD_PERF;
  719         break;
  720     default:
  721         /*
  722          * Override the capabilities of the BOARD and specify SIMPLE
  723          * MODE 
  724          */
  725         if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
  726                 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
  727         else
  728                 supported_methods = sc->ciss_cfg->supported_methods;
  729         break;
  730     }
  731 
  732 setup:
  733     if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
  734         method = CISS_TRANSPORT_METHOD_PERF;
  735         sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
  736             sc->ciss_cfg->transport_offset);
  737         if (ciss_init_perf(sc)) {
  738             supported_methods &= ~method;
  739             goto setup;
  740         }
  741     } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
  742         method = CISS_TRANSPORT_METHOD_SIMPLE;
  743     } else {
  744         ciss_printf(sc, "No supported transport methods: 0x%x\n",
  745             sc->ciss_cfg->supported_methods);
  746         return(ENXIO);
  747     }
  748 
  749     /*
  750      * Tell it we're using the low 4GB of RAM.  Set the default interrupt
  751      * coalescing options.
  752      */
  753     sc->ciss_cfg->requested_method = method;
  754     sc->ciss_cfg->command_physlimit = 0;
  755     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
  756     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
  757 
  758 #ifdef __i386__
  759     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
  760 #endif
  761 
  762     if (ciss_update_config(sc)) {
  763         ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
  764                     CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
  765         return(ENXIO);
  766     }
  767     if ((sc->ciss_cfg->active_method & method) == 0) {
  768         supported_methods &= ~method;
  769         if (supported_methods == 0) {
  770             ciss_printf(sc, "adapter refuses to go into available transports "
  771                 "mode (0x%x, 0x%x)\n", supported_methods,
  772                 sc->ciss_cfg->active_method);
  773             return(ENXIO);
  774         } else 
  775             goto setup;
  776     }
  777 
  778     /*
  779      * Wait for the adapter to come ready.
  780      */
  781     if ((error = ciss_wait_adapter(sc)) != 0)
  782         return(error);
  783 
  784     /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
  785      * interrupts have a rid of 0, this will be overridden if MSIX is used.
  786      */
  787     sc->ciss_irq_rid[0] = 0;
  788     if (method == CISS_TRANSPORT_METHOD_PERF) {
  789         ciss_printf(sc, "PERFORMANT Transport\n");
  790         if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
  791             intr = ciss_perf_msi_intr;
  792         } else {
  793             intr = ciss_perf_intr;
  794         }
  795         /* XXX The docs say that the 0x01 bit is only for SAS controllers.
  796          * Unfortunately, there is no good way to know if this is a SAS
  797          * controller.  Hopefully enabling this bit universally will work OK.
  798          * It seems to work fine for SA6i controllers.
  799          */
  800         sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
  801 
  802     } else {
  803         ciss_printf(sc, "SIMPLE Transport\n");
  804         /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
  805         if (ciss_force_interrupt == 2)
  806             /* If this fails, we automatically revert to INTx */
  807             ciss_setup_msix(sc);
  808         sc->ciss_perf = NULL;
  809         intr = ciss_intr;
  810         sc->ciss_interrupt_mask = sqmask;
  811     }
  812 
  813     /*
  814      * Turn off interrupts before we go routing anything.
  815      */
  816     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
  817 
  818     /*
  819      * Allocate and set up our interrupt.
  820      */
  821     if ((sc->ciss_irq_resource =
  822          bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
  823                                 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
  824         ciss_printf(sc, "can't allocate interrupt\n");
  825         return(ENXIO);
  826     }
  827 
  828     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
  829                        INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
  830                        &sc->ciss_intr)) {
  831         ciss_printf(sc, "can't set up interrupt\n");
  832         return(ENXIO);
  833     }
  834 
  835     /*
  836      * Allocate the parent bus DMA tag appropriate for our PCI
  837      * interface.
  838      *
  839      * Note that "simple" adapters can only address within a 32-bit
  840      * span.
  841      */
  842     if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
  843                            1, 0,                        /* alignment, boundary */
  844                            BUS_SPACE_MAXADDR,           /* lowaddr */
  845                            BUS_SPACE_MAXADDR,           /* highaddr */
  846                            NULL, NULL,                  /* filter, filterarg */
  847                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsize */
  848                            BUS_SPACE_UNRESTRICTED,      /* nsegments */
  849                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
  850                            0,                           /* flags */
  851                            NULL, NULL,                  /* lockfunc, lockarg */
  852                            &sc->ciss_parent_dmat)) {
  853         ciss_printf(sc, "can't allocate parent DMA tag\n");
  854         return(ENOMEM);
  855     }
  856 
  857     /*
  858      * Create DMA tag for mapping buffers into adapter-addressable
  859      * space.
  860      */
  861     if (bus_dma_tag_create(sc->ciss_parent_dmat,        /* parent */
  862                            1, 0,                        /* alignment, boundary */
  863                            BUS_SPACE_MAXADDR,           /* lowaddr */
  864                            BUS_SPACE_MAXADDR,           /* highaddr */
  865                            NULL, NULL,                  /* filter, filterarg */
  866                            (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE, /* maxsize */
  867                            CISS_MAX_SG_ELEMENTS,        /* nsegments */
  868                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
  869                            BUS_DMA_ALLOCNOW,            /* flags */
  870                            busdma_lock_mutex, &sc->ciss_mtx,    /* lockfunc, lockarg */
  871                            &sc->ciss_buffer_dmat)) {
  872         ciss_printf(sc, "can't allocate buffer DMA tag\n");
  873         return(ENOMEM);
  874     }
  875     return(0);
  876 }
  877 
  878 /************************************************************************
  879  * Setup MSI/MSIX operation (Performant only)
  880  * Four interrupts are available, but we only use 1 right now.  If MSI-X
  881  * isn't avaialble, try using MSI instead.
  882  */
  883 static int
  884 ciss_setup_msix(struct ciss_softc *sc)
  885 {
  886     int val, i;
  887 
  888     /* Weed out devices that don't actually support MSI */
  889     i = ciss_lookup(sc->ciss_dev);
  890     if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
  891         return (EINVAL);
  892 
  893     /*
  894      * Only need to use the minimum number of MSI vectors, as the driver
  895      * doesn't support directed MSIX interrupts.
  896      */
  897     val = pci_msix_count(sc->ciss_dev);
  898     if (val < CISS_MSI_COUNT) {
  899         val = pci_msi_count(sc->ciss_dev);
  900         device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
  901         if (val < CISS_MSI_COUNT)
  902             return (EINVAL);
  903     }
  904     val = MIN(val, CISS_MSI_COUNT);
  905     if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
  906         if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
  907             return (EINVAL);
  908     }
  909 
  910     sc->ciss_msi = val;
  911     if (bootverbose)
  912         ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
  913             (val != 1) ? "s" : "");
  914 
  915     for (i = 0; i < val; i++)
  916         sc->ciss_irq_rid[i] = i + 1;
  917 
  918     return (0);
  919 
  920 }
  921 
  922 /************************************************************************
  923  * Setup the Performant structures.
  924  */
  925 static int
  926 ciss_init_perf(struct ciss_softc *sc)
  927 {
  928     struct ciss_perf_config *pc = sc->ciss_perf;
  929     int reply_size;
  930 
  931     /*
  932      * Create the DMA tag for the reply queue.
  933      */
  934     reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
  935     if (bus_dma_tag_create(sc->ciss_parent_dmat,        /* parent */
  936                            1, 0,                        /* alignment, boundary */
  937                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
  938                            BUS_SPACE_MAXADDR,           /* highaddr */
  939                            NULL, NULL,                  /* filter, filterarg */
  940                            reply_size, 1,               /* maxsize, nsegments */
  941                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
  942                            0,                           /* flags */
  943                            NULL, NULL,                  /* lockfunc, lockarg */
  944                            &sc->ciss_reply_dmat)) {
  945         ciss_printf(sc, "can't allocate reply DMA tag\n");
  946         return(ENOMEM);
  947     }
  948     /*
  949      * Allocate memory and make it available for DMA.
  950      */
  951     if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
  952                          BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
  953         ciss_printf(sc, "can't allocate reply memory\n");
  954         return(ENOMEM);
  955     }
  956     bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
  957                     reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
  958     bzero(sc->ciss_reply, reply_size);
  959 
  960     sc->ciss_cycle = 0x1;
  961     sc->ciss_rqidx = 0;
  962 
  963     /*
  964      * Preload the fetch table with common command sizes.  This allows the
  965      * hardware to not waste bus cycles for typical i/o commands, but also not
  966      * tax the driver to be too exact in choosing sizes.  The table is optimized
  967      * for page-aligned i/o's, but since most i/o comes from the various pagers,
  968      * it's a reasonable assumption to make.
  969      */
  970     pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
  971     pc->fetch_count[CISS_SG_FETCH_1] =
  972         (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
  973     pc->fetch_count[CISS_SG_FETCH_2] =
  974         (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
  975     pc->fetch_count[CISS_SG_FETCH_4] =
  976         (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
  977     pc->fetch_count[CISS_SG_FETCH_8] =
  978         (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
  979     pc->fetch_count[CISS_SG_FETCH_16] =
  980         (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
  981     pc->fetch_count[CISS_SG_FETCH_32] =
  982         (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
  983     pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
  984 
  985     pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
  986     pc->rq_count = 1;   /* XXX Hardcode for a single queue */
  987     pc->rq_bank_hi = 0;
  988     pc->rq_bank_lo = 0;
  989     pc->rq[0].rq_addr_hi = 0x0;
  990     pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
  991 
  992     return(0);
  993 }
  994 
  995 /************************************************************************
  996  * Wait for the adapter to come ready.
  997  */
  998 static int
  999 ciss_wait_adapter(struct ciss_softc *sc)
 1000 {
 1001     int         i;
 1002 
 1003     debug_called(1);
 1004 
 1005     /*
 1006      * Wait for the adapter to come ready.
 1007      */
 1008     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
 1009         ciss_printf(sc, "waiting for adapter to come ready...\n");
 1010         for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
 1011             DELAY(1000000);     /* one second */
 1012             if (i > 30) {
 1013                 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
 1014                 return(EIO);
 1015             }
 1016         }
 1017     }
 1018     return(0);
 1019 }
 1020 
 1021 /************************************************************************
 1022  * Flush the adapter cache.
 1023  */
 1024 static int
 1025 ciss_flush_adapter(struct ciss_softc *sc)
 1026 {
 1027     struct ciss_request                 *cr;
 1028     struct ciss_bmic_flush_cache        *cbfc;
 1029     int                                 error, command_status;
 1030 
 1031     debug_called(1);
 1032 
 1033     cr = NULL;
 1034     cbfc = NULL;
 1035 
 1036     /*
 1037      * Build a BMIC request to flush the cache.  We don't disable
 1038      * it, as we may be going to do more I/O (eg. we are emulating
 1039      * the Synchronise Cache command).
 1040      */
 1041     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
 1042         error = ENOMEM;
 1043         goto out;
 1044     }
 1045     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
 1046                                        (void **)&cbfc, sizeof(*cbfc))) != 0)
 1047         goto out;
 1048 
 1049     /*
 1050      * Submit the request and wait for it to complete.
 1051      */
 1052     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1053         ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
 1054         goto out;
 1055     }
 1056 
 1057     /*
 1058      * Check response.
 1059      */
 1060     ciss_report_request(cr, &command_status, NULL);
 1061     switch(command_status) {
 1062     case CISS_CMD_STATUS_SUCCESS:
 1063         break;
 1064     default:
 1065         ciss_printf(sc, "error flushing cache (%s)\n",
 1066                     ciss_name_command_status(command_status));
 1067         error = EIO;
 1068         goto out;
 1069     }
 1070 
 1071 out:
 1072     if (cbfc != NULL)
 1073         free(cbfc, CISS_MALLOC_CLASS);
 1074     if (cr != NULL)
 1075         ciss_release_request(cr);
 1076     return(error);
 1077 }
 1078 
 1079 static void
 1080 ciss_soft_reset(struct ciss_softc *sc)
 1081 {
 1082     struct ciss_request         *cr = NULL;
 1083     struct ciss_command         *cc;
 1084     int                         i, error = 0;
 1085 
 1086     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 1087         /* only reset proxy controllers */
 1088         if (sc->ciss_controllers[i].physical.bus == 0)
 1089             continue;
 1090 
 1091         if ((error = ciss_get_request(sc, &cr)) != 0)
 1092             break;
 1093 
 1094         if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
 1095                                            NULL, 0)) != 0)
 1096             break;
 1097 
 1098         cc = cr->cr_cc;
 1099         cc->header.address = sc->ciss_controllers[i];
 1100 
 1101         if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
 1102             break;
 1103 
 1104         ciss_release_request(cr);
 1105     }
 1106 
 1107     if (error)
 1108         ciss_printf(sc, "error resetting controller (%d)\n", error);
 1109 
 1110     if (cr != NULL)
 1111         ciss_release_request(cr);
 1112 }
 1113 
 1114 /************************************************************************
 1115  * Allocate memory for the adapter command structures, initialise
 1116  * the request structures.
 1117  *
 1118  * Note that the entire set of commands are allocated in a single
 1119  * contiguous slab.
 1120  */
 1121 static int
 1122 ciss_init_requests(struct ciss_softc *sc)
 1123 {
 1124     struct ciss_request *cr;
 1125     int                 i;
 1126 
 1127     debug_called(1);
 1128 
 1129     if (bootverbose)
 1130         ciss_printf(sc, "using %d of %d available commands\n",
 1131                     sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
 1132 
 1133     /*
 1134      * Create the DMA tag for commands.
 1135      */
 1136     if (bus_dma_tag_create(sc->ciss_parent_dmat,        /* parent */
 1137                            32, 0,                       /* alignment, boundary */
 1138                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
 1139                            BUS_SPACE_MAXADDR,           /* highaddr */
 1140                            NULL, NULL,                  /* filter, filterarg */
 1141                            CISS_COMMAND_ALLOC_SIZE *
 1142                            sc->ciss_max_requests, 1,    /* maxsize, nsegments */
 1143                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
 1144                            0,                           /* flags */
 1145                            NULL, NULL,                  /* lockfunc, lockarg */
 1146                            &sc->ciss_command_dmat)) {
 1147         ciss_printf(sc, "can't allocate command DMA tag\n");
 1148         return(ENOMEM);
 1149     }
 1150     /*
 1151      * Allocate memory and make it available for DMA.
 1152      */
 1153     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
 1154                          BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
 1155         ciss_printf(sc, "can't allocate command memory\n");
 1156         return(ENOMEM);
 1157     }
 1158     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
 1159                     CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
 1160                     ciss_command_map_helper, &sc->ciss_command_phys, 0);
 1161     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
 1162 
 1163     /*
 1164      * Set up the request and command structures, push requests onto
 1165      * the free queue.
 1166      */
 1167     for (i = 1; i < sc->ciss_max_requests; i++) {
 1168         cr = &sc->ciss_request[i];
 1169         cr->cr_sc = sc;
 1170         cr->cr_tag = i;
 1171         cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
 1172             CISS_COMMAND_ALLOC_SIZE * i);
 1173         cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
 1174         bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
 1175         ciss_enqueue_free(cr);
 1176     }
 1177     return(0);
 1178 }
 1179 
 1180 static void
 1181 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1182 {
 1183     uint32_t *addr;
 1184 
 1185     addr = arg;
 1186     *addr = segs[0].ds_addr;
 1187 }
 1188 
 1189 /************************************************************************
 1190  * Identify the adapter, print some information about it.
 1191  */
 1192 static int
 1193 ciss_identify_adapter(struct ciss_softc *sc)
 1194 {
 1195     struct ciss_request *cr;
 1196     int                 error, command_status;
 1197 
 1198     debug_called(1);
 1199 
 1200     cr = NULL;
 1201 
 1202     /*
 1203      * Get a request, allocate storage for the adapter data.
 1204      */
 1205     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
 1206                                        (void **)&sc->ciss_id,
 1207                                        sizeof(*sc->ciss_id))) != 0)
 1208         goto out;
 1209 
 1210     /*
 1211      * Submit the request and wait for it to complete.
 1212      */
 1213     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1214         ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
 1215         goto out;
 1216     }
 1217 
 1218     /*
 1219      * Check response.
 1220      */
 1221     ciss_report_request(cr, &command_status, NULL);
 1222     switch(command_status) {
 1223     case CISS_CMD_STATUS_SUCCESS:               /* buffer right size */
 1224         break;
 1225     case CISS_CMD_STATUS_DATA_UNDERRUN:
 1226     case CISS_CMD_STATUS_DATA_OVERRUN:
 1227         ciss_printf(sc, "data over/underrun reading adapter information\n");
 1228     default:
 1229         ciss_printf(sc, "error reading adapter information (%s)\n",
 1230                     ciss_name_command_status(command_status));
 1231         error = EIO;
 1232         goto out;
 1233     }
 1234 
 1235     /* sanity-check reply */
 1236     if (!(sc->ciss_id->controller_flags & CONTROLLER_FLAGS_BIG_MAP_SUPPORT)) {
 1237         ciss_printf(sc, "adapter does not support BIG_MAP\n");
 1238         error = ENXIO;
 1239         goto out;
 1240     }
 1241 
 1242 #if 0
 1243     /* XXX later revisions may not need this */
 1244     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
 1245 #endif
 1246 
 1247     /* XXX only really required for old 5300 adapters? */
 1248     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
 1249 
 1250     /*
 1251      * Earlier controller specs do not contain these config
 1252      * entries, so assume that a 0 means its old and assign
 1253      * these values to the defaults that were established 
 1254      * when this driver was developed for them
 1255      */
 1256     if (sc->ciss_cfg->max_logical_supported == 0) 
 1257         sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
 1258     if (sc->ciss_cfg->max_physical_supported == 0) 
 1259         sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
 1260     /* print information */
 1261     if (bootverbose) {
 1262         ciss_printf(sc, "  %d logical drive%s configured\n",
 1263                     sc->ciss_id->configured_logical_drives,
 1264                     (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
 1265         ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
 1266         ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_chip_count);
 1267 
 1268         ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
 1269         ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
 1270         ciss_printf(sc, "  supported I/O methods 0x%b\n",
 1271                     sc->ciss_cfg->supported_methods,
 1272                     "\2\1READY\2simple\3performant\4MEMQ\n");
 1273         ciss_printf(sc, "  active I/O method 0x%b\n",
 1274                     sc->ciss_cfg->active_method, "\2\2simple\3performant\4MEMQ\n");
 1275         ciss_printf(sc, "  4G page base 0x%08x\n",
 1276                     sc->ciss_cfg->command_physlimit);
 1277         ciss_printf(sc, "  interrupt coalesce delay %dus\n",
 1278                     sc->ciss_cfg->interrupt_coalesce_delay);
 1279         ciss_printf(sc, "  interrupt coalesce count %d\n",
 1280                     sc->ciss_cfg->interrupt_coalesce_count);
 1281         ciss_printf(sc, "  max outstanding commands %d\n",
 1282                     sc->ciss_cfg->max_outstanding_commands);
 1283         ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
 1284                     "\2\1ultra2\2ultra3\10fibre1\11fibre2\n");
 1285         ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
 1286         ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
 1287         ciss_printf(sc, "  max logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
 1288         ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
 1289         ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
 1290         ciss_printf(sc, "  JBOD Support is %s\n", (sc->ciss_id->uiYetMoreControllerFlags & YMORE_CONTROLLER_FLAGS_JBOD_SUPPORTED) ?
 1291                         "Available" : "Unavailable");
 1292         ciss_printf(sc, "  JBOD Mode is %s\n", (sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED) ?
 1293                         "Enabled" : "Disabled");
 1294     }
 1295 
 1296 out:
 1297     if (error) {
 1298         if (sc->ciss_id != NULL) {
 1299             free(sc->ciss_id, CISS_MALLOC_CLASS);
 1300             sc->ciss_id = NULL;
 1301         }
 1302     }
 1303     if (cr != NULL)
 1304         ciss_release_request(cr);
 1305     return(error);
 1306 }
 1307 
 1308 /************************************************************************
 1309  * Helper routine for generating a list of logical and physical luns.
 1310  */
 1311 static struct ciss_lun_report *
 1312 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
 1313 {
 1314     struct ciss_request         *cr;
 1315     struct ciss_command         *cc;
 1316     struct ciss_report_cdb      *crc;
 1317     struct ciss_lun_report      *cll;
 1318     int                         command_status;
 1319     int                         report_size;
 1320     int                         error = 0;
 1321 
 1322     debug_called(1);
 1323 
 1324     cr = NULL;
 1325     cll = NULL;
 1326 
 1327     /*
 1328      * Get a request, allocate storage for the address list.
 1329      */
 1330     if ((error = ciss_get_request(sc, &cr)) != 0)
 1331         goto out;
 1332     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
 1333     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
 1334         ciss_printf(sc, "can't allocate memory for lun report\n");
 1335         error = ENOMEM;
 1336         goto out;
 1337     }
 1338 
 1339     /*
 1340      * Build the Report Logical/Physical LUNs command.
 1341      */
 1342     cc = cr->cr_cc;
 1343     cr->cr_data = cll;
 1344     cr->cr_length = report_size;
 1345     cr->cr_flags = CISS_REQ_DATAIN;
 1346 
 1347     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
 1348     cc->header.address.physical.bus = 0;
 1349     cc->header.address.physical.target = 0;
 1350     cc->cdb.cdb_length = sizeof(*crc);
 1351     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
 1352     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 1353     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
 1354     cc->cdb.timeout = 30;       /* XXX better suggestions? */
 1355 
 1356     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
 1357     bzero(crc, sizeof(*crc));
 1358     crc->opcode = opcode;
 1359     crc->length = htonl(report_size);                   /* big-endian field */
 1360     cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
 1361 
 1362     /*
 1363      * Submit the request and wait for it to complete.  (timeout
 1364      * here should be much greater than above)
 1365      */
 1366     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1367         ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
 1368         goto out;
 1369     }
 1370 
 1371     /*
 1372      * Check response.  Note that data over/underrun is OK.
 1373      */
 1374     ciss_report_request(cr, &command_status, NULL);
 1375     switch(command_status) {
 1376     case CISS_CMD_STATUS_SUCCESS:       /* buffer right size */
 1377     case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
 1378         break;
 1379     case CISS_CMD_STATUS_DATA_OVERRUN:
 1380         ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
 1381                     sc->ciss_cfg->max_logical_supported);
 1382         break;
 1383     default:
 1384         ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
 1385                     ciss_name_command_status(command_status));
 1386         error = EIO;
 1387         goto out;
 1388     }
 1389     ciss_release_request(cr);
 1390     cr = NULL;
 1391 
 1392 out:
 1393     if (cr != NULL)
 1394         ciss_release_request(cr);
 1395     if (error && cll != NULL) {
 1396         free(cll, CISS_MALLOC_CLASS);
 1397         cll = NULL;
 1398     }
 1399     return(cll);
 1400 }
 1401 
 1402 /************************************************************************
 1403  * Find logical drives on the adapter.
 1404  */
 1405 static int
 1406 ciss_init_logical(struct ciss_softc *sc)
 1407 {
 1408     struct ciss_lun_report      *cll;
 1409     int                         error = 0, i, j;
 1410     int                         ndrives;
 1411 
 1412     debug_called(1);
 1413 
 1414     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
 1415                            sc->ciss_cfg->max_logical_supported);
 1416     if (cll == NULL) {
 1417         error = ENXIO;
 1418         goto out;
 1419     }
 1420 
 1421     /* sanity-check reply */
 1422     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
 1423     if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
 1424         ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
 1425                 ndrives, sc->ciss_cfg->max_logical_supported);
 1426         error = ENXIO;
 1427         goto out;
 1428     }
 1429 
 1430     /*
 1431      * Save logical drive information.
 1432      */
 1433     if (bootverbose) {
 1434         ciss_printf(sc, "%d logical drive%s\n",
 1435             ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
 1436     }
 1437 
 1438     sc->ciss_logical =
 1439         malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
 1440                CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
 1441     if (sc->ciss_logical == NULL) {
 1442         error = ENXIO;
 1443         goto out;
 1444     }
 1445 
 1446     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 1447         sc->ciss_logical[i] =
 1448             malloc(sc->ciss_cfg->max_logical_supported *
 1449                    sizeof(struct ciss_ldrive),
 1450                    CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
 1451         if (sc->ciss_logical[i] == NULL) {
 1452             error = ENXIO;
 1453             goto out;
 1454         }
 1455 
 1456         for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
 1457             sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
 1458     }
 1459 
 1460 
 1461     for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
 1462         if (i < ndrives) {
 1463             struct ciss_ldrive  *ld;
 1464             int                 bus, target;
 1465 
 1466             bus         = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
 1467             target      = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
 1468             ld          = &sc->ciss_logical[bus][target];
 1469 
 1470             ld->cl_address      = cll->lun[i];
 1471             ld->cl_controller   = &sc->ciss_controllers[bus];
 1472             if (ciss_identify_logical(sc, ld) != 0)
 1473                 continue;
 1474             /*
 1475              * If the drive has had media exchanged, we should bring it online.
 1476              */
 1477             if (ld->cl_lstatus->media_exchanged)
 1478                 ciss_accept_media(sc, ld);
 1479 
 1480         }
 1481     }
 1482 
 1483  out:
 1484     if (cll != NULL)
 1485         free(cll, CISS_MALLOC_CLASS);
 1486     return(error);
 1487 }
 1488 
 1489 static int
 1490 ciss_init_physical(struct ciss_softc *sc)
 1491 {
 1492     struct ciss_lun_report      *cll;
 1493     int                         error = 0, i;
 1494     int                         nphys;
 1495     int                         bus, target;
 1496 
 1497     debug_called(1);
 1498 
 1499     bus = 0;
 1500     target = 0;
 1501 
 1502     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
 1503                            sc->ciss_cfg->max_physical_supported);
 1504     if (cll == NULL) {
 1505         error = ENXIO;
 1506         goto out;
 1507     }
 1508 
 1509     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
 1510 
 1511     if (bootverbose) {
 1512         ciss_printf(sc, "%d physical device%s\n",
 1513             nphys, (nphys > 1 || nphys == 0) ? "s" : "");
 1514     }
 1515 
 1516     /*
 1517      * Figure out the bus mapping.
 1518      * Logical buses include both the local logical bus for local arrays and
 1519      * proxy buses for remote arrays.  Physical buses are numbered by the
 1520      * controller and represent physical buses that hold physical devices.
 1521      * We shift these bus numbers so that everything fits into a single flat
 1522      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
 1523      * numbers, and the physical bus numbers are shifted to be above that.
 1524      * This results in the various driver arrays being indexed as follows:
 1525      *
 1526      * ciss_controllers[] - indexed by logical bus
 1527      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
 1528      *                      being shifted by 32.
 1529      * ciss_logical[][]   - indexed by logical bus
 1530      * ciss_physical[][]  - indexed by physical bus
 1531      *
 1532      * XXX This is getting more and more hackish.  CISS really doesn't play
 1533      *     well with a standard SCSI model; devices are addressed via magic
 1534      *     cookies, not via b/t/l addresses.  Since there is no way to store
 1535      *     the cookie in the CAM device object, we have to keep these lookup
 1536      *     tables handy so that the devices can be found quickly at the cost
 1537      *     of wasting memory and having a convoluted lookup scheme.  This
 1538      *     driver should probably be converted to block interface.
 1539      */
 1540     /*
 1541      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
 1542      * controller. A proxy controller is another physical controller
 1543      * behind the primary PCI controller. We need to know about this
 1544      * so that BMIC commands can be properly targeted.  There can be
 1545      * proxy controllers attached to a single PCI controller, so
 1546      * find the highest numbered one so the array can be properly
 1547      * sized.
 1548      */
 1549     sc->ciss_max_logical_bus = 1;
 1550     for (i = 0; i < nphys; i++) {
 1551         if (cll->lun[i].physical.extra_address == 0) {
 1552             bus = cll->lun[i].physical.bus;
 1553             sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
 1554         } else {
 1555             bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
 1556             sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
 1557         }
 1558     }
 1559 
 1560     sc->ciss_controllers =
 1561         malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
 1562                CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
 1563 
 1564     if (sc->ciss_controllers == NULL) {
 1565         ciss_printf(sc, "Could not allocate memory for controller map\n");
 1566         error = ENOMEM;
 1567         goto out;
 1568     }
 1569 
 1570     /* setup a map of controller addresses */
 1571     for (i = 0; i < nphys; i++) {
 1572         if (cll->lun[i].physical.extra_address == 0) {
 1573             sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
 1574         }
 1575     }
 1576 
 1577     sc->ciss_physical =
 1578         malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
 1579                CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
 1580     if (sc->ciss_physical == NULL) {
 1581         ciss_printf(sc, "Could not allocate memory for physical device map\n");
 1582         error = ENOMEM;
 1583         goto out;
 1584     }
 1585 
 1586     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
 1587         sc->ciss_physical[i] =
 1588             malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
 1589                    CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
 1590         if (sc->ciss_physical[i] == NULL) {
 1591             ciss_printf(sc, "Could not allocate memory for target map\n");
 1592             error = ENOMEM;
 1593             goto out;
 1594         }
 1595     }
 1596 
 1597     ciss_filter_physical(sc, cll);
 1598 
 1599 out:
 1600     if (cll != NULL)
 1601         free(cll, CISS_MALLOC_CLASS);
 1602 
 1603     return(error);
 1604 }
 1605 
 1606 static int
 1607 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
 1608 {
 1609     u_int32_t ea;
 1610     int i, nphys;
 1611     int bus, target;
 1612 
 1613     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
 1614     for (i = 0; i < nphys; i++) {
 1615         if (cll->lun[i].physical.extra_address == 0)
 1616             continue;
 1617 
 1618         /*
 1619          * Filter out devices that we don't want.  Level 3 LUNs could
 1620          * probably be supported, but the docs don't give enough of a
 1621          * hint to know how.
 1622          *
 1623          * The mode field of the physical address is likely set to have
 1624          * hard disks masked out.  Honor it unless the user has overridden
 1625          * us with the tunable.  We also munge the inquiry data for these
 1626          * disks so that they only show up as passthrough devices.  Keeping
 1627          * them visible in this fashion is useful for doing things like
 1628          * flashing firmware.
 1629          */
 1630         ea = cll->lun[i].physical.extra_address;
 1631         if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
 1632             (CISS_EXTRA_MODE2(ea) == 0x3))
 1633             continue;
 1634         if ((ciss_expose_hidden_physical == 0) &&
 1635            (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
 1636             continue;
 1637 
 1638         /*
 1639          * Note: CISS firmware numbers physical busses starting at '1', not
 1640          *       ''.  This numbering is internal to the firmware and is only
 1641          *       used as a hint here.
 1642          */
 1643         bus = CISS_EXTRA_BUS2(ea) - 1;
 1644         target = CISS_EXTRA_TARGET2(ea);
 1645         sc->ciss_physical[bus][target].cp_address = cll->lun[i];
 1646         sc->ciss_physical[bus][target].cp_online = 1;
 1647     }
 1648 
 1649     return (0);
 1650 }
 1651 
 1652 static int
 1653 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
 1654 {
 1655     struct ciss_request                 *cr;
 1656     struct ciss_command                 *cc;
 1657     struct scsi_inquiry                 *inq;
 1658     int                                 error;
 1659     int                                 command_status;
 1660 
 1661     cr = NULL;
 1662 
 1663     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
 1664 
 1665     if ((error = ciss_get_request(sc, &cr)) != 0)
 1666         goto out;
 1667 
 1668     cc = cr->cr_cc;
 1669     cr->cr_data = &ld->cl_geometry;
 1670     cr->cr_length = sizeof(ld->cl_geometry);
 1671     cr->cr_flags = CISS_REQ_DATAIN;
 1672 
 1673     cc->header.address = ld->cl_address;
 1674     cc->cdb.cdb_length = 6;
 1675     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
 1676     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 1677     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
 1678     cc->cdb.timeout = 30;
 1679 
 1680     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
 1681     inq->opcode = INQUIRY;
 1682     inq->byte2 = SI_EVPD;
 1683     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
 1684     scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
 1685 
 1686     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1687         ciss_printf(sc, "error getting geometry (%d)\n", error);
 1688         goto out;
 1689     }
 1690 
 1691     ciss_report_request(cr, &command_status, NULL);
 1692     switch(command_status) {
 1693     case CISS_CMD_STATUS_SUCCESS:
 1694     case CISS_CMD_STATUS_DATA_UNDERRUN:
 1695         break;
 1696     case CISS_CMD_STATUS_DATA_OVERRUN:
 1697         ciss_printf(sc, "WARNING: Data overrun\n");
 1698         break;
 1699     default:
 1700         ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
 1701                     ciss_name_command_status(command_status));
 1702         break;
 1703     }
 1704 
 1705 out:
 1706     if (cr != NULL)
 1707         ciss_release_request(cr);
 1708     return(error);
 1709 }
 1710 /************************************************************************
 1711  * Identify a logical drive, initialise state related to it.
 1712  */
 1713 static int
 1714 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
 1715 {
 1716     struct ciss_request         *cr;
 1717     struct ciss_command         *cc;
 1718     struct ciss_bmic_cdb        *cbc;
 1719     int                         error, command_status;
 1720 
 1721     debug_called(1);
 1722 
 1723     cr = NULL;
 1724 
 1725     /*
 1726      * Build a BMIC request to fetch the drive ID.
 1727      */
 1728     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
 1729                                        (void **)&ld->cl_ldrive,
 1730                                        sizeof(*ld->cl_ldrive))) != 0)
 1731         goto out;
 1732     cc = cr->cr_cc;
 1733     cc->header.address = *ld->cl_controller;    /* target controller */
 1734     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
 1735     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
 1736 
 1737     /*
 1738      * Submit the request and wait for it to complete.
 1739      */
 1740     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1741         ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
 1742         goto out;
 1743     }
 1744 
 1745     /*
 1746      * Check response.
 1747      */
 1748     ciss_report_request(cr, &command_status, NULL);
 1749     switch(command_status) {
 1750     case CISS_CMD_STATUS_SUCCESS:               /* buffer right size */
 1751         break;
 1752     case CISS_CMD_STATUS_DATA_UNDERRUN:
 1753     case CISS_CMD_STATUS_DATA_OVERRUN:
 1754         ciss_printf(sc, "data over/underrun reading logical drive ID\n");
 1755     default:
 1756         ciss_printf(sc, "error reading logical drive ID (%s)\n",
 1757                     ciss_name_command_status(command_status));
 1758         error = EIO;
 1759         goto out;
 1760     }
 1761     ciss_release_request(cr);
 1762     cr = NULL;
 1763 
 1764     /*
 1765      * Build a CISS BMIC command to get the logical drive status.
 1766      */
 1767     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
 1768         goto out;
 1769 
 1770     /*
 1771      * Get the logical drive geometry.
 1772      */
 1773     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
 1774         goto out;
 1775 
 1776     /*
 1777      * Print the drive's basic characteristics.
 1778      */
 1779     if (bootverbose) {
 1780         ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
 1781                     CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
 1782                     CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
 1783                     ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
 1784                     ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
 1785                      ld->cl_ldrive->block_size));
 1786 
 1787         ciss_print_ldrive(sc, ld);
 1788     }
 1789 out:
 1790     if (error != 0) {
 1791         /* make the drive not-exist */
 1792         ld->cl_status = CISS_LD_NONEXISTENT;
 1793         if (ld->cl_ldrive != NULL) {
 1794             free(ld->cl_ldrive, CISS_MALLOC_CLASS);
 1795             ld->cl_ldrive = NULL;
 1796         }
 1797         if (ld->cl_lstatus != NULL) {
 1798             free(ld->cl_lstatus, CISS_MALLOC_CLASS);
 1799             ld->cl_lstatus = NULL;
 1800         }
 1801     }
 1802     if (cr != NULL)
 1803         ciss_release_request(cr);
 1804 
 1805     return(error);
 1806 }
 1807 
 1808 /************************************************************************
 1809  * Get status for a logical drive.
 1810  *
 1811  * XXX should we also do this in response to Test Unit Ready?
 1812  */
 1813 static int
 1814 ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
 1815 {
 1816     struct ciss_request         *cr;
 1817     struct ciss_command         *cc;
 1818     struct ciss_bmic_cdb        *cbc;
 1819     int                         error, command_status;
 1820 
 1821     /*
 1822      * Build a CISS BMIC command to get the logical drive status.
 1823      */
 1824     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
 1825                                        (void **)&ld->cl_lstatus,
 1826                                        sizeof(*ld->cl_lstatus))) != 0)
 1827         goto out;
 1828     cc = cr->cr_cc;
 1829     cc->header.address = *ld->cl_controller;    /* target controller */
 1830     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
 1831     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
 1832 
 1833     /*
 1834      * Submit the request and wait for it to complete.
 1835      */
 1836     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1837         ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
 1838         goto out;
 1839     }
 1840 
 1841     /*
 1842      * Check response.
 1843      */
 1844     ciss_report_request(cr, &command_status, NULL);
 1845     switch(command_status) {
 1846     case CISS_CMD_STATUS_SUCCESS:               /* buffer right size */
 1847         break;
 1848     case CISS_CMD_STATUS_DATA_UNDERRUN:
 1849     case CISS_CMD_STATUS_DATA_OVERRUN:
 1850         ciss_printf(sc, "data over/underrun reading logical drive status\n");
 1851     default:
 1852         ciss_printf(sc, "error reading logical drive status (%s)\n",
 1853                     ciss_name_command_status(command_status));
 1854         error = EIO;
 1855         goto out;
 1856     }
 1857 
 1858     /*
 1859      * Set the drive's summary status based on the returned status.
 1860      *
 1861      * XXX testing shows that a failed JBOD drive comes back at next
 1862      * boot in "queued for expansion" mode.  WTF?
 1863      */
 1864     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
 1865 
 1866 out:
 1867     if (cr != NULL)
 1868         ciss_release_request(cr);
 1869     return(error);
 1870 }
 1871 
 1872 /************************************************************************
 1873  * Notify the adapter of a config update.
 1874  */
 1875 static int
 1876 ciss_update_config(struct ciss_softc *sc)
 1877 {
 1878     int         i;
 1879 
 1880     debug_called(1);
 1881 
 1882     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
 1883     for (i = 0; i < 1000; i++) {
 1884         if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
 1885               CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
 1886             return(0);
 1887         }
 1888         DELAY(1000);
 1889     }
 1890     return(1);
 1891 }
 1892 
 1893 /************************************************************************
 1894  * Accept new media into a logical drive.
 1895  *
 1896  * XXX The drive has previously been offline; it would be good if we
 1897  *     could make sure it's not open right now.
 1898  */
 1899 static int
 1900 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
 1901 {
 1902     struct ciss_request         *cr;
 1903     struct ciss_command         *cc;
 1904     struct ciss_bmic_cdb        *cbc;
 1905     int                         command_status;
 1906     int                         error = 0, ldrive;
 1907 
 1908     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
 1909 
 1910     debug(0, "bringing logical drive %d back online", ldrive);
 1911 
 1912     /*
 1913      * Build a CISS BMIC command to bring the drive back online.
 1914      */
 1915     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
 1916                                        NULL, 0)) != 0)
 1917         goto out;
 1918     cc = cr->cr_cc;
 1919     cc->header.address = *ld->cl_controller;    /* target controller */
 1920     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
 1921     cbc->log_drive = ldrive;
 1922 
 1923     /*
 1924      * Submit the request and wait for it to complete.
 1925      */
 1926     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 1927         ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
 1928         goto out;
 1929     }
 1930 
 1931     /*
 1932      * Check response.
 1933      */
 1934     ciss_report_request(cr, &command_status, NULL);
 1935     switch(command_status) {
 1936     case CISS_CMD_STATUS_SUCCESS:               /* all OK */
 1937         /* we should get a logical drive status changed event here */
 1938         break;
 1939     default:
 1940         ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
 1941                     ciss_name_command_status(command_status));
 1942         break;
 1943     }
 1944 
 1945 out:
 1946     if (cr != NULL)
 1947         ciss_release_request(cr);
 1948     return(error);
 1949 }
 1950 
 1951 /************************************************************************
 1952  * Release adapter resources.
 1953  */
 1954 static void
 1955 ciss_free(struct ciss_softc *sc)
 1956 {
 1957     struct ciss_request *cr;
 1958     int                 i, j;
 1959 
 1960     debug_called(1);
 1961 
 1962     /* we're going away */
 1963     sc->ciss_flags |= CISS_FLAG_ABORTING;
 1964 
 1965     /* terminate the periodic heartbeat routine */
 1966     callout_stop(&sc->ciss_periodic);
 1967 
 1968     /* cancel the Event Notify chain */
 1969     ciss_notify_abort(sc);
 1970 
 1971     ciss_kill_notify_thread(sc);
 1972 
 1973     /* disconnect from CAM */
 1974     if (sc->ciss_cam_sim) {
 1975         for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 1976             if (sc->ciss_cam_sim[i]) {
 1977                 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
 1978                 cam_sim_free(sc->ciss_cam_sim[i], 0);
 1979             }
 1980         }
 1981         for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
 1982              CISS_PHYSICAL_BASE; i++) {
 1983             if (sc->ciss_cam_sim[i]) {
 1984                 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
 1985                 cam_sim_free(sc->ciss_cam_sim[i], 0);
 1986             }
 1987         }
 1988         free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
 1989     }
 1990     if (sc->ciss_cam_devq)
 1991         cam_simq_free(sc->ciss_cam_devq);
 1992 
 1993     /* remove the control device */
 1994     mtx_unlock(&sc->ciss_mtx);
 1995     if (sc->ciss_dev_t != NULL)
 1996         destroy_dev(sc->ciss_dev_t);
 1997 
 1998     /* Final cleanup of the callout. */
 1999     callout_drain(&sc->ciss_periodic);
 2000     mtx_destroy(&sc->ciss_mtx);
 2001 
 2002     /* free the controller data */
 2003     if (sc->ciss_id != NULL)
 2004         free(sc->ciss_id, CISS_MALLOC_CLASS);
 2005 
 2006     /* release I/O resources */
 2007     if (sc->ciss_regs_resource != NULL)
 2008         bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
 2009                              sc->ciss_regs_rid, sc->ciss_regs_resource);
 2010     if (sc->ciss_cfg_resource != NULL)
 2011         bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
 2012                              sc->ciss_cfg_rid, sc->ciss_cfg_resource);
 2013     if (sc->ciss_intr != NULL)
 2014         bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
 2015     if (sc->ciss_irq_resource != NULL)
 2016         bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
 2017                              sc->ciss_irq_rid[0], sc->ciss_irq_resource);
 2018     if (sc->ciss_msi)
 2019         pci_release_msi(sc->ciss_dev);
 2020 
 2021     while ((cr = ciss_dequeue_free(sc)) != NULL)
 2022         bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
 2023     if (sc->ciss_buffer_dmat)
 2024         bus_dma_tag_destroy(sc->ciss_buffer_dmat);
 2025 
 2026     /* destroy command memory and DMA tag */
 2027     if (sc->ciss_command != NULL) {
 2028         bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
 2029         bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
 2030     }
 2031     if (sc->ciss_command_dmat)
 2032         bus_dma_tag_destroy(sc->ciss_command_dmat);
 2033 
 2034     if (sc->ciss_reply) {
 2035         bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
 2036         bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
 2037     }
 2038     if (sc->ciss_reply_dmat)
 2039         bus_dma_tag_destroy(sc->ciss_reply_dmat);
 2040 
 2041     /* destroy DMA tags */
 2042     if (sc->ciss_parent_dmat)
 2043         bus_dma_tag_destroy(sc->ciss_parent_dmat);
 2044     if (sc->ciss_logical) {
 2045         for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 2046             for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
 2047                 if (sc->ciss_logical[i][j].cl_ldrive)
 2048                     free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
 2049                 if (sc->ciss_logical[i][j].cl_lstatus)
 2050                     free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
 2051             }
 2052             free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
 2053         }
 2054         free(sc->ciss_logical, CISS_MALLOC_CLASS);
 2055     }
 2056 
 2057     if (sc->ciss_physical) {
 2058         for (i = 0; i < sc->ciss_max_physical_bus; i++)
 2059             free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
 2060         free(sc->ciss_physical, CISS_MALLOC_CLASS);
 2061     }
 2062 
 2063     if (sc->ciss_controllers)
 2064         free(sc->ciss_controllers, CISS_MALLOC_CLASS);
 2065 
 2066 }
 2067 
 2068 /************************************************************************
 2069  * Give a command to the adapter.
 2070  *
 2071  * Note that this uses the simple transport layer directly.  If we
 2072  * want to add support for other layers, we'll need a switch of some
 2073  * sort.
 2074  *
 2075  * Note that the simple transport layer has no way of refusing a
 2076  * command; we only have as many request structures as the adapter
 2077  * supports commands, so we don't have to check (this presumes that
 2078  * the adapter can handle commands as fast as we throw them at it).
 2079  */
 2080 static int
 2081 ciss_start(struct ciss_request *cr)
 2082 {
 2083     struct ciss_command *cc;    /* XXX debugging only */
 2084     int                 error;
 2085 
 2086     cc = cr->cr_cc;
 2087     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
 2088 
 2089     /*
 2090      * Map the request's data.
 2091      */
 2092     if ((error = ciss_map_request(cr)))
 2093         return(error);
 2094 
 2095 #if 0
 2096     ciss_print_request(cr);
 2097 #endif
 2098 
 2099     return(0);
 2100 }
 2101 
 2102 /************************************************************************
 2103  * Fetch completed request(s) from the adapter, queue them for
 2104  * completion handling.
 2105  *
 2106  * Note that this uses the simple transport layer directly.  If we
 2107  * want to add support for other layers, we'll need a switch of some
 2108  * sort.
 2109  *
 2110  * Note that the simple transport mechanism does not require any
 2111  * reentrancy protection; the OPQ read is atomic.  If there is a
 2112  * chance of a race with something else that might move the request
 2113  * off the busy list, then we will have to lock against that
 2114  * (eg. timeouts, etc.)
 2115  */
 2116 static void
 2117 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
 2118 {
 2119     struct ciss_request *cr;
 2120     struct ciss_command *cc;
 2121     u_int32_t           tag, index;
 2122 
 2123     debug_called(3);
 2124 
 2125     /*
 2126      * Loop quickly taking requests from the adapter and moving them
 2127      * to the completed queue.
 2128      */
 2129     for (;;) {
 2130 
 2131         tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
 2132         if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
 2133             break;
 2134         index = tag >> 2;
 2135         debug(2, "completed command %d%s", index,
 2136               (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
 2137         if (index >= sc->ciss_max_requests) {
 2138             ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
 2139             continue;
 2140         }
 2141         cr = &(sc->ciss_request[index]);
 2142         cc = cr->cr_cc;
 2143         cc->header.host_tag = tag;      /* not updated by adapter */
 2144         ciss_enqueue_complete(cr, qh);
 2145     }
 2146 
 2147 }
 2148 
 2149 static void
 2150 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
 2151 {
 2152     struct ciss_request *cr;
 2153     struct ciss_command *cc;
 2154     u_int32_t           tag, index;
 2155 
 2156     debug_called(3);
 2157 
 2158     /*
 2159      * Loop quickly taking requests from the adapter and moving them
 2160      * to the completed queue.
 2161      */
 2162     for (;;) {
 2163         tag = sc->ciss_reply[sc->ciss_rqidx];
 2164         if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
 2165             break;
 2166         index = tag >> 2;
 2167         debug(2, "completed command %d%s\n", index,
 2168               (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
 2169         if (index < sc->ciss_max_requests) {
 2170             cr = &(sc->ciss_request[index]);
 2171             cc = cr->cr_cc;
 2172             cc->header.host_tag = tag;  /* not updated by adapter */
 2173             ciss_enqueue_complete(cr, qh);
 2174         } else {
 2175             ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
 2176         }
 2177         if (++sc->ciss_rqidx == sc->ciss_max_requests) {
 2178             sc->ciss_rqidx = 0;
 2179             sc->ciss_cycle ^= 1;
 2180         }
 2181     }
 2182 
 2183 }
 2184 
 2185 /************************************************************************
 2186  * Take an interrupt from the adapter.
 2187  */
 2188 static void
 2189 ciss_intr(void *arg)
 2190 {
 2191     cr_qhead_t qh;
 2192     struct ciss_softc   *sc = (struct ciss_softc *)arg;
 2193 
 2194     /*
 2195      * The only interrupt we recognise indicates that there are
 2196      * entries in the outbound post queue.
 2197      */
 2198     STAILQ_INIT(&qh);
 2199     ciss_done(sc, &qh);
 2200     mtx_lock(&sc->ciss_mtx);
 2201     ciss_complete(sc, &qh);
 2202     mtx_unlock(&sc->ciss_mtx);
 2203 }
 2204 
 2205 static void
 2206 ciss_perf_intr(void *arg)
 2207 {
 2208     struct ciss_softc   *sc = (struct ciss_softc *)arg;
 2209 
 2210     /* Clear the interrupt and flush the bridges.  Docs say that the flush
 2211      * needs to be done twice, which doesn't seem right.
 2212      */
 2213     CISS_TL_PERF_CLEAR_INT(sc);
 2214     CISS_TL_PERF_FLUSH_INT(sc);
 2215 
 2216     ciss_perf_msi_intr(sc);
 2217 }
 2218 
 2219 static void
 2220 ciss_perf_msi_intr(void *arg)
 2221 {
 2222     cr_qhead_t qh;
 2223     struct ciss_softc   *sc = (struct ciss_softc *)arg;
 2224 
 2225     STAILQ_INIT(&qh);
 2226     ciss_perf_done(sc, &qh);
 2227     mtx_lock(&sc->ciss_mtx);
 2228     ciss_complete(sc, &qh);
 2229     mtx_unlock(&sc->ciss_mtx);
 2230 }
 2231 
 2232 
 2233 /************************************************************************
 2234  * Process completed requests.
 2235  *
 2236  * Requests can be completed in three fashions:
 2237  *
 2238  * - by invoking a callback function (cr_complete is non-null)
 2239  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
 2240  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
 2241  */
 2242 static void
 2243 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
 2244 {
 2245     struct ciss_request *cr;
 2246 
 2247     debug_called(2);
 2248 
 2249     /*
 2250      * Loop taking requests off the completed queue and performing
 2251      * completion processing on them.
 2252      */
 2253     for (;;) {
 2254         if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
 2255             break;
 2256         ciss_unmap_request(cr);
 2257 
 2258         if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
 2259             ciss_printf(sc, "WARNING: completing non-busy request\n");
 2260         cr->cr_flags &= ~CISS_REQ_BUSY;
 2261 
 2262         /*
 2263          * If the request has a callback, invoke it.
 2264          */
 2265         if (cr->cr_complete != NULL) {
 2266             cr->cr_complete(cr);
 2267             continue;
 2268         }
 2269 
 2270         /*
 2271          * If someone is sleeping on this request, wake them up.
 2272          */
 2273         if (cr->cr_flags & CISS_REQ_SLEEP) {
 2274             cr->cr_flags &= ~CISS_REQ_SLEEP;
 2275             wakeup(cr);
 2276             continue;
 2277         }
 2278 
 2279         /*
 2280          * If someone is polling this request for completion, signal.
 2281          */
 2282         if (cr->cr_flags & CISS_REQ_POLL) {
 2283             cr->cr_flags &= ~CISS_REQ_POLL;
 2284             continue;
 2285         }
 2286 
 2287         /*
 2288          * Give up and throw the request back on the free queue.  This
 2289          * should never happen; resources will probably be lost.
 2290          */
 2291         ciss_printf(sc, "WARNING: completed command with no submitter\n");
 2292         ciss_enqueue_free(cr);
 2293     }
 2294 }
 2295 
 2296 /************************************************************************
 2297  * Report on the completion status of a request, and pass back SCSI
 2298  * and command status values.
 2299  */
 2300 static int
 2301 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
 2302 {
 2303     struct ciss_command         *cc;
 2304     struct ciss_error_info      *ce;
 2305 
 2306     debug_called(2);
 2307 
 2308     cc = cr->cr_cc;
 2309     ce = (struct ciss_error_info *)&(cc->sg[0]);
 2310 
 2311     /*
 2312      * We don't consider data under/overrun an error for the Report
 2313      * Logical/Physical LUNs commands.
 2314      */
 2315     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
 2316         ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
 2317          (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
 2318         ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
 2319          (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
 2320          (cc->cdb.cdb[0] == INQUIRY))) {
 2321         cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
 2322         debug(2, "ignoring irrelevant under/overrun error");
 2323     }
 2324 
 2325     /*
 2326      * Check the command's error bit, if clear, there's no status and
 2327      * everything is OK.
 2328      */
 2329     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
 2330         if (scsi_status != NULL)
 2331             *scsi_status = SCSI_STATUS_OK;
 2332         if (command_status != NULL)
 2333             *command_status = CISS_CMD_STATUS_SUCCESS;
 2334         return(0);
 2335     } else {
 2336         if (command_status != NULL)
 2337             *command_status = ce->command_status;
 2338         if (scsi_status != NULL) {
 2339             if (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN) {
 2340                 *scsi_status = SCSI_STATUS_OK;
 2341             } else if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
 2342                 *scsi_status = ce->scsi_status;
 2343             } else {
 2344                 *scsi_status = -1;
 2345             }
 2346         }
 2347         if (bootverbose && ce->command_status != CISS_CMD_STATUS_DATA_UNDERRUN)
 2348             ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
 2349                         ce->command_status, ciss_name_command_status(ce->command_status),
 2350                         ce->scsi_status);
 2351         if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
 2352             ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
 2353                         ce->additional_error_info.invalid_command.offense_size,
 2354                         ce->additional_error_info.invalid_command.offense_offset,
 2355                         ce->additional_error_info.invalid_command.offense_value,
 2356                         func);
 2357         }
 2358     }
 2359 #if 0
 2360     ciss_print_request(cr);
 2361 #endif
 2362     return(1);
 2363 }
 2364 
 2365 /************************************************************************
 2366  * Issue a request and don't return until it's completed.
 2367  *
 2368  * Depending on adapter status, we may poll or sleep waiting for
 2369  * completion.
 2370  */
 2371 static int
 2372 ciss_synch_request(struct ciss_request *cr, int timeout)
 2373 {
 2374     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
 2375         return(ciss_wait_request(cr, timeout));
 2376     } else {
 2377         return(ciss_poll_request(cr, timeout));
 2378     }
 2379 }
 2380 
 2381 /************************************************************************
 2382  * Issue a request and poll for completion.
 2383  *
 2384  * Timeout in milliseconds.
 2385  */
 2386 static int
 2387 ciss_poll_request(struct ciss_request *cr, int timeout)
 2388 {
 2389     cr_qhead_t qh;
 2390     struct ciss_softc *sc;
 2391     int         error;
 2392 
 2393     debug_called(2);
 2394 
 2395     STAILQ_INIT(&qh);
 2396     sc = cr->cr_sc;
 2397     cr->cr_flags |= CISS_REQ_POLL;
 2398     if ((error = ciss_start(cr)) != 0)
 2399         return(error);
 2400 
 2401     do {
 2402         if (sc->ciss_perf)
 2403             ciss_perf_done(sc, &qh);
 2404         else
 2405             ciss_done(sc, &qh);
 2406         ciss_complete(sc, &qh);
 2407         if (!(cr->cr_flags & CISS_REQ_POLL))
 2408             return(0);
 2409         DELAY(1000);
 2410     } while (timeout-- >= 0);
 2411     return(EWOULDBLOCK);
 2412 }
 2413 
 2414 /************************************************************************
 2415  * Issue a request and sleep waiting for completion.
 2416  *
 2417  * Timeout in milliseconds.  Note that a spurious wakeup will reset
 2418  * the timeout.
 2419  */
 2420 static int
 2421 ciss_wait_request(struct ciss_request *cr, int timeout)
 2422 {
 2423     int         error;
 2424 
 2425     debug_called(2);
 2426 
 2427     cr->cr_flags |= CISS_REQ_SLEEP;
 2428     if ((error = ciss_start(cr)) != 0)
 2429         return(error);
 2430 
 2431     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
 2432         error = msleep_sbt(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ",
 2433             SBT_1MS * timeout, 0, 0);
 2434     }
 2435     return(error);
 2436 }
 2437 
 2438 #if 0
 2439 /************************************************************************
 2440  * Abort a request.  Note that a potential exists here to race the
 2441  * request being completed; the caller must deal with this.
 2442  */
 2443 static int
 2444 ciss_abort_request(struct ciss_request *ar)
 2445 {
 2446     struct ciss_request         *cr;
 2447     struct ciss_command         *cc;
 2448     struct ciss_message_cdb     *cmc;
 2449     int                         error;
 2450 
 2451     debug_called(1);
 2452 
 2453     /* get a request */
 2454     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
 2455         return(error);
 2456 
 2457     /* build the abort command */
 2458     cc = cr->cr_cc;
 2459     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;    /* addressing? */
 2460     cc->header.address.physical.target = 0;
 2461     cc->header.address.physical.bus = 0;
 2462     cc->cdb.cdb_length = sizeof(*cmc);
 2463     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
 2464     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 2465     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
 2466     cc->cdb.timeout = 30;
 2467 
 2468     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
 2469     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
 2470     cmc->type = CISS_MESSAGE_ABORT_TASK;
 2471     cmc->abort_tag = ar->cr_tag;        /* endianness?? */
 2472 
 2473     /*
 2474      * Send the request and wait for a response.  If we believe we
 2475      * aborted the request OK, clear the flag that indicates it's
 2476      * running.
 2477      */
 2478     error = ciss_synch_request(cr, 35 * 1000);
 2479     if (!error)
 2480         error = ciss_report_request(cr, NULL, NULL);
 2481     ciss_release_request(cr);
 2482 
 2483     return(error);
 2484 }
 2485 #endif
 2486 
 2487 
 2488 /************************************************************************
 2489  * Fetch and initialise a request
 2490  */
 2491 static int
 2492 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
 2493 {
 2494     struct ciss_request *cr;
 2495 
 2496     debug_called(2);
 2497 
 2498     /*
 2499      * Get a request and clean it up.
 2500      */
 2501     if ((cr = ciss_dequeue_free(sc)) == NULL)
 2502         return(ENOMEM);
 2503 
 2504     cr->cr_data = NULL;
 2505     cr->cr_flags = 0;
 2506     cr->cr_complete = NULL;
 2507     cr->cr_private = NULL;
 2508     cr->cr_sg_tag = CISS_SG_MAX;        /* Backstop to prevent accidents */
 2509 
 2510     ciss_preen_command(cr);
 2511     *crp = cr;
 2512     return(0);
 2513 }
 2514 
 2515 static void
 2516 ciss_preen_command(struct ciss_request *cr)
 2517 {
 2518     struct ciss_command *cc;
 2519     u_int32_t           cmdphys;
 2520 
 2521     /*
 2522      * Clean up the command structure.
 2523      *
 2524      * Note that we set up the error_info structure here, since the
 2525      * length can be overwritten by any command.
 2526      */
 2527     cc = cr->cr_cc;
 2528     cc->header.sg_in_list = 0;          /* kinda inefficient this way */
 2529     cc->header.sg_total = 0;
 2530     cc->header.host_tag = cr->cr_tag << 2;
 2531     cc->header.host_tag_zeroes = 0;
 2532     bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
 2533     cmdphys = cr->cr_ccphys;
 2534     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
 2535     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
 2536 }
 2537 
 2538 /************************************************************************
 2539  * Release a request to the free list.
 2540  */
 2541 static void
 2542 ciss_release_request(struct ciss_request *cr)
 2543 {
 2544     struct ciss_softc   *sc;
 2545 
 2546     debug_called(2);
 2547 
 2548     sc = cr->cr_sc;
 2549 
 2550     /* release the request to the free queue */
 2551     ciss_requeue_free(cr);
 2552 }
 2553 
 2554 /************************************************************************
 2555  * Allocate a request that will be used to send a BMIC command.  Do some
 2556  * of the common setup here to avoid duplicating it everywhere else.
 2557  */
 2558 static int
 2559 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
 2560                       int opcode, void **bufp, size_t bufsize)
 2561 {
 2562     struct ciss_request         *cr;
 2563     struct ciss_command         *cc;
 2564     struct ciss_bmic_cdb        *cbc;
 2565     void                        *buf;
 2566     int                         error;
 2567     int                         dataout;
 2568 
 2569     debug_called(2);
 2570 
 2571     cr = NULL;
 2572     buf = NULL;
 2573 
 2574     /*
 2575      * Get a request.
 2576      */
 2577     if ((error = ciss_get_request(sc, &cr)) != 0)
 2578         goto out;
 2579 
 2580     /*
 2581      * Allocate data storage if requested, determine the data direction.
 2582      */
 2583     dataout = 0;
 2584     if ((bufsize > 0) && (bufp != NULL)) {
 2585         if (*bufp == NULL) {
 2586             if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
 2587                 error = ENOMEM;
 2588                 goto out;
 2589             }
 2590         } else {
 2591             buf = *bufp;
 2592             dataout = 1;        /* we are given a buffer, so we are writing */
 2593         }
 2594     }
 2595 
 2596     /*
 2597      * Build a CISS BMIC command to get the logical drive ID.
 2598      */
 2599     cr->cr_data = buf;
 2600     cr->cr_length = bufsize;
 2601     if (!dataout)
 2602         cr->cr_flags = CISS_REQ_DATAIN;
 2603 
 2604     cc = cr->cr_cc;
 2605     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
 2606     cc->header.address.physical.bus = 0;
 2607     cc->header.address.physical.target = 0;
 2608     cc->cdb.cdb_length = sizeof(*cbc);
 2609     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
 2610     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 2611     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
 2612     cc->cdb.timeout = 0;
 2613 
 2614     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
 2615     bzero(cbc, sizeof(*cbc));
 2616     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
 2617     cbc->bmic_opcode = opcode;
 2618     cbc->size = htons((u_int16_t)bufsize);
 2619 
 2620 out:
 2621     if (error) {
 2622         if (cr != NULL)
 2623             ciss_release_request(cr);
 2624     } else {
 2625         *crp = cr;
 2626         if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
 2627             *bufp = buf;
 2628     }
 2629     return(error);
 2630 }
 2631 
 2632 /************************************************************************
 2633  * Handle a command passed in from userspace.
 2634  */
 2635 static int
 2636 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
 2637 {
 2638     struct ciss_request         *cr;
 2639     struct ciss_command         *cc;
 2640     struct ciss_error_info      *ce;
 2641     int                         error = 0;
 2642 
 2643     debug_called(1);
 2644 
 2645     cr = NULL;
 2646 
 2647     /*
 2648      * Get a request.
 2649      */
 2650     while (ciss_get_request(sc, &cr) != 0)
 2651         msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
 2652     cc = cr->cr_cc;
 2653 
 2654     /*
 2655      * Allocate an in-kernel databuffer if required, copy in user data.
 2656      */
 2657     mtx_unlock(&sc->ciss_mtx);
 2658     cr->cr_length = ioc->buf_size;
 2659     if (ioc->buf_size > 0) {
 2660         if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
 2661             error = ENOMEM;
 2662             goto out_unlocked;
 2663         }
 2664         if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
 2665             debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
 2666             goto out_unlocked;
 2667         }
 2668     }
 2669 
 2670     /*
 2671      * Build the request based on the user command.
 2672      */
 2673     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
 2674     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
 2675 
 2676     /* XXX anything else to populate here? */
 2677     mtx_lock(&sc->ciss_mtx);
 2678 
 2679     /*
 2680      * Run the command.
 2681      */
 2682     if ((error = ciss_synch_request(cr, 60 * 1000))) {
 2683         debug(0, "request failed - %d", error);
 2684         goto out;
 2685     }
 2686 
 2687     /*
 2688      * Check to see if the command succeeded.
 2689      */
 2690     ce = (struct ciss_error_info *)&(cc->sg[0]);
 2691     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
 2692         bzero(ce, sizeof(*ce));
 2693 
 2694     /*
 2695      * Copy the results back to the user.
 2696      */
 2697     bcopy(ce, &ioc->error_info, sizeof(*ce));
 2698     mtx_unlock(&sc->ciss_mtx);
 2699     if ((ioc->buf_size > 0) &&
 2700         (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
 2701         debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
 2702         goto out_unlocked;
 2703     }
 2704 
 2705     /* done OK */
 2706     error = 0;
 2707 
 2708 out_unlocked:
 2709     mtx_lock(&sc->ciss_mtx);
 2710 
 2711 out:
 2712     if ((cr != NULL) && (cr->cr_data != NULL))
 2713         free(cr->cr_data, CISS_MALLOC_CLASS);
 2714     if (cr != NULL)
 2715         ciss_release_request(cr);
 2716     return(error);
 2717 }
 2718 
 2719 /************************************************************************
 2720  * Map a request into bus-visible space, initialise the scatter/gather
 2721  * list.
 2722  */
 2723 static int
 2724 ciss_map_request(struct ciss_request *cr)
 2725 {
 2726     struct ciss_softc   *sc;
 2727     int                 error = 0;
 2728 
 2729     debug_called(2);
 2730 
 2731     sc = cr->cr_sc;
 2732 
 2733     /* check that mapping is necessary */
 2734     if (cr->cr_flags & CISS_REQ_MAPPED)
 2735         return(0);
 2736 
 2737     cr->cr_flags |= CISS_REQ_MAPPED;
 2738 
 2739     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
 2740                     BUS_DMASYNC_PREWRITE);
 2741 
 2742     if (cr->cr_data != NULL) {
 2743         if (cr->cr_flags & CISS_REQ_CCB)
 2744                 error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
 2745                                         cr->cr_datamap, cr->cr_data,
 2746                                         ciss_request_map_helper, cr, 0);
 2747         else
 2748                 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
 2749                                         cr->cr_data, cr->cr_length,
 2750                                         ciss_request_map_helper, cr, 0);
 2751         if (error != 0)
 2752             return (error);
 2753     } else {
 2754         /*
 2755          * Post the command to the adapter.
 2756          */
 2757         cr->cr_sg_tag = CISS_SG_NONE;
 2758         cr->cr_flags |= CISS_REQ_BUSY;
 2759         if (sc->ciss_perf)
 2760             CISS_TL_PERF_POST_CMD(sc, cr);
 2761         else
 2762             CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
 2763     }
 2764 
 2765     return(0);
 2766 }
 2767 
 2768 static void
 2769 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 2770 {
 2771     struct ciss_command *cc;
 2772     struct ciss_request *cr;
 2773     struct ciss_softc   *sc;
 2774     int                 i;
 2775 
 2776     debug_called(2);
 2777 
 2778     cr = (struct ciss_request *)arg;
 2779     sc = cr->cr_sc;
 2780     cc = cr->cr_cc;
 2781 
 2782     for (i = 0; i < nseg; i++) {
 2783         cc->sg[i].address = segs[i].ds_addr;
 2784         cc->sg[i].length = segs[i].ds_len;
 2785         cc->sg[i].extension = 0;
 2786     }
 2787     /* we leave the s/g table entirely within the command */
 2788     cc->header.sg_in_list = nseg;
 2789     cc->header.sg_total = nseg;
 2790 
 2791     if (cr->cr_flags & CISS_REQ_DATAIN)
 2792         bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
 2793     if (cr->cr_flags & CISS_REQ_DATAOUT)
 2794         bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
 2795 
 2796     if (nseg == 0)
 2797         cr->cr_sg_tag = CISS_SG_NONE;
 2798     else if (nseg == 1)
 2799         cr->cr_sg_tag = CISS_SG_1;
 2800     else if (nseg == 2)
 2801         cr->cr_sg_tag = CISS_SG_2;
 2802     else if (nseg <= 4)
 2803         cr->cr_sg_tag = CISS_SG_4;
 2804     else if (nseg <= 8)
 2805         cr->cr_sg_tag = CISS_SG_8;
 2806     else if (nseg <= 16)
 2807         cr->cr_sg_tag = CISS_SG_16;
 2808     else if (nseg <= 32)
 2809         cr->cr_sg_tag = CISS_SG_32;
 2810     else
 2811         cr->cr_sg_tag = CISS_SG_MAX;
 2812 
 2813     /*
 2814      * Post the command to the adapter.
 2815      */
 2816     cr->cr_flags |= CISS_REQ_BUSY;
 2817     if (sc->ciss_perf)
 2818         CISS_TL_PERF_POST_CMD(sc, cr);
 2819     else
 2820         CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
 2821 }
 2822 
 2823 /************************************************************************
 2824  * Unmap a request from bus-visible space.
 2825  */
 2826 static void
 2827 ciss_unmap_request(struct ciss_request *cr)
 2828 {
 2829     struct ciss_softc   *sc;
 2830 
 2831     debug_called(2);
 2832 
 2833     sc = cr->cr_sc;
 2834 
 2835     /* check that unmapping is necessary */
 2836     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
 2837         return;
 2838 
 2839     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
 2840                     BUS_DMASYNC_POSTWRITE);
 2841 
 2842     if (cr->cr_data == NULL)
 2843         goto out;
 2844 
 2845     if (cr->cr_flags & CISS_REQ_DATAIN)
 2846         bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
 2847     if (cr->cr_flags & CISS_REQ_DATAOUT)
 2848         bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
 2849 
 2850     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
 2851 out:
 2852     cr->cr_flags &= ~CISS_REQ_MAPPED;
 2853 }
 2854 
 2855 /************************************************************************
 2856  * Attach the driver to CAM.
 2857  *
 2858  * We put all the logical drives on a single SCSI bus.
 2859  */
 2860 static int
 2861 ciss_cam_init(struct ciss_softc *sc)
 2862 {
 2863     int                 i, maxbus;
 2864 
 2865     debug_called(1);
 2866 
 2867     /*
 2868      * Allocate a devq.  We can reuse this for the masked physical
 2869      * devices if we decide to export these as well.
 2870      */
 2871     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
 2872         ciss_printf(sc, "can't allocate CAM SIM queue\n");
 2873         return(ENOMEM);
 2874     }
 2875 
 2876     /*
 2877      * Create a SIM.
 2878      *
 2879      * This naturally wastes a bit of memory.  The alternative is to allocate
 2880      * and register each bus as it is found, and then track them on a linked
 2881      * list.  Unfortunately, the driver has a few places where it needs to
 2882      * look up the SIM based solely on bus number, and it's unclear whether
 2883      * a list traversal would work for these situations.
 2884      */
 2885     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
 2886                  CISS_PHYSICAL_BASE);
 2887     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
 2888                               CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
 2889     if (sc->ciss_cam_sim == NULL) {
 2890         ciss_printf(sc, "can't allocate memory for controller SIM\n");
 2891         return(ENOMEM);
 2892     }
 2893 
 2894     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 2895         if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
 2896                                                  "ciss", sc,
 2897                                                  device_get_unit(sc->ciss_dev),
 2898                                                  &sc->ciss_mtx,
 2899                                                  2,
 2900                                                  sc->ciss_max_requests - 2,
 2901                                                  sc->ciss_cam_devq)) == NULL) {
 2902             ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
 2903             return(ENOMEM);
 2904         }
 2905 
 2906         /*
 2907          * Register bus with this SIM.
 2908          */
 2909         mtx_lock(&sc->ciss_mtx);
 2910         if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) { 
 2911             if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
 2912                 ciss_printf(sc, "can't register SCSI bus %d\n", i);
 2913                 mtx_unlock(&sc->ciss_mtx);
 2914                 return (ENXIO);
 2915             }
 2916         }
 2917         mtx_unlock(&sc->ciss_mtx);
 2918     }
 2919 
 2920     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
 2921          CISS_PHYSICAL_BASE; i++) {
 2922         if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
 2923                                                  "ciss", sc,
 2924                                                  device_get_unit(sc->ciss_dev),
 2925                                                  &sc->ciss_mtx, 1,
 2926                                                  sc->ciss_max_requests - 2,
 2927                                                  sc->ciss_cam_devq)) == NULL) {
 2928             ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
 2929             return (ENOMEM);
 2930         }
 2931 
 2932         mtx_lock(&sc->ciss_mtx);
 2933         if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
 2934             ciss_printf(sc, "can't register SCSI bus %d\n", i);
 2935             mtx_unlock(&sc->ciss_mtx);
 2936             return (ENXIO);
 2937         }
 2938         mtx_unlock(&sc->ciss_mtx);
 2939     }
 2940 
 2941     return(0);
 2942 }
 2943 
 2944 /************************************************************************
 2945  * Initiate a rescan of the 'logical devices' SIM
 2946  */
 2947 static void
 2948 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
 2949 {
 2950     union ccb           *ccb;
 2951 
 2952     debug_called(1);
 2953 
 2954     if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
 2955         ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
 2956         return;
 2957     }
 2958 
 2959     if (xpt_create_path(&ccb->ccb_h.path, NULL,
 2960             cam_sim_path(sc->ciss_cam_sim[bus]),
 2961             target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
 2962         ciss_printf(sc, "rescan failed (can't create path)\n");
 2963         xpt_free_ccb(ccb);
 2964         return;
 2965     }
 2966     xpt_rescan(ccb);
 2967     /* scan is now in progress */
 2968 }
 2969 
 2970 /************************************************************************
 2971  * Handle requests coming from CAM
 2972  */
 2973 static void
 2974 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
 2975 {
 2976     struct ciss_softc   *sc;
 2977     struct ccb_scsiio   *csio;
 2978     int                 bus, target;
 2979     int                 physical;
 2980 
 2981     sc = cam_sim_softc(sim);
 2982     bus = cam_sim_bus(sim);
 2983     csio = (struct ccb_scsiio *)&ccb->csio;
 2984     target = csio->ccb_h.target_id;
 2985     physical = CISS_IS_PHYSICAL(bus);
 2986 
 2987     switch (ccb->ccb_h.func_code) {
 2988 
 2989         /* perform SCSI I/O */
 2990     case XPT_SCSI_IO:
 2991         if (!ciss_cam_action_io(sim, csio))
 2992             return;
 2993         break;
 2994 
 2995         /* perform geometry calculations */
 2996     case XPT_CALC_GEOMETRY:
 2997     {
 2998         struct ccb_calc_geometry        *ccg = &ccb->ccg;
 2999         struct ciss_ldrive              *ld;
 3000 
 3001         debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
 3002 
 3003         ld = NULL;
 3004         if (!physical)
 3005             ld = &sc->ciss_logical[bus][target];
 3006             
 3007         /*
 3008          * Use the cached geometry settings unless the fault tolerance
 3009          * is invalid.
 3010          */
 3011         if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
 3012             u_int32_t                   secs_per_cylinder;
 3013 
 3014             ccg->heads = 255;
 3015             ccg->secs_per_track = 32;
 3016             secs_per_cylinder = ccg->heads * ccg->secs_per_track;
 3017             ccg->cylinders = ccg->volume_size / secs_per_cylinder;
 3018         } else {
 3019             ccg->heads = ld->cl_geometry.heads;
 3020             ccg->secs_per_track = ld->cl_geometry.sectors;
 3021             ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
 3022         }
 3023         ccb->ccb_h.status = CAM_REQ_CMP;
 3024         break;
 3025     }
 3026 
 3027         /* handle path attribute inquiry */
 3028     case XPT_PATH_INQ:
 3029     {
 3030         struct ccb_pathinq      *cpi = &ccb->cpi;
 3031         int                     sg_length;
 3032 
 3033         debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
 3034 
 3035         cpi->version_num = 1;
 3036         cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
 3037         cpi->target_sprt = 0;
 3038         cpi->hba_misc = 0;
 3039         cpi->max_target = sc->ciss_cfg->max_logical_supported;
 3040         cpi->max_lun = 0;               /* 'logical drive' channel only */
 3041         cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
 3042         strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
 3043         strlcpy(cpi->hba_vid, "CISS", HBA_IDLEN);
 3044         strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
 3045         cpi->unit_number = cam_sim_unit(sim);
 3046         cpi->bus_id = cam_sim_bus(sim);
 3047         cpi->base_transfer_speed = 132 * 1024;  /* XXX what to set this to? */
 3048         cpi->transport = XPORT_SPI;
 3049         cpi->transport_version = 2;
 3050         cpi->protocol = PROTO_SCSI;
 3051         cpi->protocol_version = SCSI_REV_2;
 3052         if (sc->ciss_cfg->max_sg_length == 0) {
 3053                 sg_length = 17;
 3054         } else {
 3055         /* XXX Fix for ZMR cards that advertise max_sg_length == 32
 3056          * Confusing bit here. max_sg_length is usually a power of 2. We always
 3057          * need to subtract 1 to account for partial pages. Then we need to 
 3058          * align on a valid PAGE_SIZE so we round down to the nearest power of 2. 
 3059          * Add 1 so we can then subtract it out in the assignment to maxio.
 3060          * The reason for all these shenanigans is to create a maxio value that
 3061          * creates IO operations to volumes that yield consistent operations
 3062          * with good performance.
 3063          */
 3064                 sg_length = sc->ciss_cfg->max_sg_length - 1;
 3065                 sg_length = (1 << (fls(sg_length) - 1)) + 1;
 3066         }
 3067         cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
 3068         ccb->ccb_h.status = CAM_REQ_CMP;
 3069         break;
 3070     }
 3071 
 3072     case XPT_GET_TRAN_SETTINGS:
 3073     {
 3074         struct ccb_trans_settings       *cts = &ccb->cts;
 3075         int                             bus, target;
 3076         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
 3077         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
 3078 
 3079         bus = cam_sim_bus(sim);
 3080         target = cts->ccb_h.target_id;
 3081 
 3082         debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
 3083         /* disconnect always OK */
 3084         cts->protocol = PROTO_SCSI;
 3085         cts->protocol_version = SCSI_REV_2;
 3086         cts->transport = XPORT_SPI;
 3087         cts->transport_version = 2;
 3088 
 3089         spi->valid = CTS_SPI_VALID_DISC;
 3090         spi->flags = CTS_SPI_FLAGS_DISC_ENB;
 3091 
 3092         scsi->valid = CTS_SCSI_VALID_TQ;
 3093         scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
 3094 
 3095         cts->ccb_h.status = CAM_REQ_CMP;
 3096         break;
 3097     }
 3098 
 3099     default:            /* we can't do this */
 3100         debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
 3101         ccb->ccb_h.status = CAM_REQ_INVALID;
 3102         break;
 3103     }
 3104 
 3105     xpt_done(ccb);
 3106 }
 3107 
 3108 /************************************************************************
 3109  * Handle a CAM SCSI I/O request.
 3110  */
 3111 static int
 3112 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
 3113 {
 3114     struct ciss_softc   *sc;
 3115     int                 bus, target;
 3116     struct ciss_request *cr;
 3117     struct ciss_command *cc;
 3118     int                 error;
 3119 
 3120     sc = cam_sim_softc(sim);
 3121     bus = cam_sim_bus(sim);
 3122     target = csio->ccb_h.target_id;
 3123 
 3124     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
 3125 
 3126     /* check that the CDB pointer is not to a physical address */
 3127     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
 3128         debug(3, "  CDB pointer is to physical address");
 3129         csio->ccb_h.status = CAM_REQ_CMP_ERR;
 3130     }
 3131 
 3132     /* abandon aborted ccbs or those that have failed validation */
 3133     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
 3134         debug(3, "abandoning CCB due to abort/validation failure");
 3135         return(EINVAL);
 3136     }
 3137 
 3138     /* handle emulation of some SCSI commands ourself */
 3139     if (ciss_cam_emulate(sc, csio))
 3140         return(0);
 3141 
 3142     /*
 3143      * Get a request to manage this command.  If we can't, return the
 3144      * ccb, freeze the queue and flag so that we unfreeze it when a
 3145      * request completes.
 3146      */
 3147     if ((error = ciss_get_request(sc, &cr)) != 0) {
 3148         xpt_freeze_simq(sim, 1);
 3149         sc->ciss_flags |= CISS_FLAG_BUSY;
 3150         csio->ccb_h.status |= CAM_REQUEUE_REQ;
 3151         return(error);
 3152     }
 3153 
 3154     /*
 3155      * Build the command.
 3156      */
 3157     cc = cr->cr_cc;
 3158     cr->cr_data = csio;
 3159     cr->cr_length = csio->dxfer_len;
 3160     cr->cr_complete = ciss_cam_complete;
 3161     cr->cr_private = csio;
 3162 
 3163     /*
 3164      * Target the right logical volume.
 3165      */
 3166     if (CISS_IS_PHYSICAL(bus))
 3167         cc->header.address =
 3168             sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
 3169     else
 3170         cc->header.address =
 3171             sc->ciss_logical[bus][target].cl_address;
 3172     cc->cdb.cdb_length = csio->cdb_len;
 3173     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
 3174     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;      /* XXX ordered tags? */
 3175     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
 3176         cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
 3177         cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
 3178     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
 3179         cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
 3180         cc->cdb.direction = CISS_CDB_DIRECTION_READ;
 3181     } else {
 3182         cr->cr_data = NULL;
 3183         cr->cr_flags = 0;
 3184         cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
 3185     }
 3186     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
 3187     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
 3188         bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
 3189     } else {
 3190         bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
 3191     }
 3192 
 3193     /*
 3194      * Submit the request to the adapter.
 3195      *
 3196      * Note that this may fail if we're unable to map the request (and
 3197      * if we ever learn a transport layer other than simple, may fail
 3198      * if the adapter rejects the command).
 3199      */
 3200     if ((error = ciss_start(cr)) != 0) {
 3201         xpt_freeze_simq(sim, 1);
 3202         csio->ccb_h.status |= CAM_RELEASE_SIMQ;
 3203         if (error == EINPROGRESS) {
 3204             error = 0;
 3205         } else {
 3206             csio->ccb_h.status |= CAM_REQUEUE_REQ;
 3207             ciss_release_request(cr);
 3208         }
 3209         return(error);
 3210     }
 3211 
 3212     return(0);
 3213 }
 3214 
 3215 /************************************************************************
 3216  * Emulate SCSI commands the adapter doesn't handle as we might like.
 3217  */
 3218 static int
 3219 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
 3220 {
 3221     int         bus, target;
 3222     u_int8_t    opcode;
 3223 
 3224     target = csio->ccb_h.target_id;
 3225     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
 3226     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
 3227         *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
 3228 
 3229     if (CISS_IS_PHYSICAL(bus)) {
 3230         if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
 3231             csio->ccb_h.status |= CAM_SEL_TIMEOUT;
 3232             xpt_done((union ccb *)csio);
 3233             return(1);
 3234         } else
 3235             return(0);
 3236     }
 3237 
 3238     /*
 3239      * Handle requests for volumes that don't exist or are not online.
 3240      * A selection timeout is slightly better than an illegal request.
 3241      * Other errors might be better.
 3242      */
 3243     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
 3244         csio->ccb_h.status |= CAM_SEL_TIMEOUT;
 3245         xpt_done((union ccb *)csio);
 3246         return(1);
 3247     }
 3248 
 3249     /* if we have to fake Synchronise Cache */
 3250     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
 3251         /*
 3252          * If this is a Synchronise Cache command, typically issued when
 3253          * a device is closed, flush the adapter and complete now.
 3254          */
 3255         if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
 3256              *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
 3257             ciss_flush_adapter(sc);
 3258             csio->ccb_h.status |= CAM_REQ_CMP;
 3259             xpt_done((union ccb *)csio);
 3260             return(1);
 3261         }
 3262     }
 3263 
 3264     /* 
 3265      * A CISS target can only ever have one lun per target. REPORT_LUNS requires
 3266      * at least one LUN field to be pre created for us, so snag it and fill in
 3267      * the least significant byte indicating 1 LUN here.  Emulate the command
 3268      * return to shut up warning on console of a CDB error.  swb 
 3269      */
 3270     if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
 3271        csio->data_ptr[3] = 8;
 3272        csio->ccb_h.status |= CAM_REQ_CMP;
 3273        xpt_done((union ccb *)csio);
 3274        return(1);
 3275     }
 3276 
 3277     return(0);
 3278 }
 3279 
 3280 /************************************************************************
 3281  * Check for possibly-completed commands.
 3282  */
 3283 static void
 3284 ciss_cam_poll(struct cam_sim *sim)
 3285 {
 3286     cr_qhead_t qh;
 3287     struct ciss_softc   *sc = cam_sim_softc(sim);
 3288 
 3289     debug_called(2);
 3290 
 3291     STAILQ_INIT(&qh);
 3292     if (sc->ciss_perf)
 3293         ciss_perf_done(sc, &qh);
 3294     else
 3295         ciss_done(sc, &qh);
 3296     ciss_complete(sc, &qh);
 3297 }
 3298 
 3299 /************************************************************************
 3300  * Handle completion of a command - pass results back through the CCB
 3301  */
 3302 static void
 3303 ciss_cam_complete(struct ciss_request *cr)
 3304 {
 3305     struct ciss_softc           *sc;
 3306     struct ciss_command         *cc;
 3307     struct ciss_error_info      *ce;
 3308     struct ccb_scsiio           *csio;
 3309     int                         scsi_status;
 3310     int                         command_status;
 3311 
 3312     debug_called(2);
 3313 
 3314     sc = cr->cr_sc;
 3315     cc = cr->cr_cc;
 3316     ce = (struct ciss_error_info *)&(cc->sg[0]);
 3317     csio = (struct ccb_scsiio *)cr->cr_private;
 3318 
 3319     /*
 3320      * Extract status values from request.
 3321      */
 3322     ciss_report_request(cr, &command_status, &scsi_status);
 3323     switch(command_status) {
 3324     case CISS_CMD_STATUS_DATA_UNDERRUN:
 3325         csio->resid = ce->residual_count;
 3326         /* FALLTHROUGH */
 3327     case CISS_CMD_STATUS_SUCCESS:
 3328         csio->scsi_status = scsi_status;
 3329         debug(2, "SCSI_STATUS_OK");
 3330         csio->ccb_h.status |= CAM_REQ_CMP;
 3331         break;
 3332     case CISS_CMD_STATUS_TARGET_STATUS:
 3333         csio->scsi_status = scsi_status;
 3334         bzero(&csio->sense_data, SSD_FULL_SIZE);
 3335         bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
 3336         if (csio->sense_len > ce->sense_length)
 3337                 csio->sense_resid = csio->sense_len - ce->sense_length;
 3338         else
 3339                 csio->sense_resid = 0;
 3340         csio->resid = ce->residual_count;
 3341         csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
 3342         break;
 3343     case CISS_CMD_STATUS_DATA_OVERRUN:
 3344         csio->ccb_h.status |= CAM_DATA_RUN_ERR;
 3345         break;
 3346     default:
 3347         csio->ccb_h.status |= CAM_REQ_CMP_ERR;
 3348         break;
 3349     }
 3350 
 3351     /* handle post-command fixup */
 3352     ciss_cam_complete_fixup(sc, csio);
 3353 
 3354     ciss_release_request(cr);
 3355     if (sc->ciss_flags & CISS_FLAG_BUSY) {
 3356         sc->ciss_flags &= ~CISS_FLAG_BUSY;
 3357         if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
 3358             xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
 3359         else
 3360             csio->ccb_h.status |= CAM_RELEASE_SIMQ;
 3361     }
 3362     xpt_done((union ccb *)csio);
 3363 }
 3364 
 3365 /********************************************************************************
 3366  * Fix up the result of some commands here.
 3367  */
 3368 static void
 3369 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
 3370 {
 3371     struct scsi_inquiry_data    *inq;
 3372     struct ciss_ldrive          *cl;
 3373     uint8_t                     *cdb;
 3374     int                         bus, target;
 3375 
 3376     cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
 3377          (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
 3378     if (cdb[0] == INQUIRY && 
 3379         (cdb[1] & SI_EVPD) == 0 &&
 3380         (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
 3381         csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
 3382 
 3383         inq = (struct scsi_inquiry_data *)csio->data_ptr;
 3384         target = csio->ccb_h.target_id;
 3385         bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
 3386 
 3387         /*
 3388          * If the controller is in JBOD mode, there are no logical volumes.
 3389          * Let the disks be probed and dealt with via CAM.  Else, mask off 
 3390          * the physical disks and setup the parts of the inq structure for
 3391          * the logical volume.  swb
 3392          */
 3393         if( !(sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED)){
 3394                 if (CISS_IS_PHYSICAL(bus)) {
 3395                         if (SID_TYPE(inq) == T_DIRECT)
 3396                                 inq->device = (inq->device & 0xe0) | T_NODEVICE;
 3397                         return;
 3398                 }
 3399                 cl = &sc->ciss_logical[bus][target];
 3400 
 3401                 padstr(inq->vendor, "HP",
 3402                         SID_VENDOR_SIZE);
 3403                 padstr(inq->product,
 3404                         ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
 3405                         SID_PRODUCT_SIZE);
 3406                 padstr(inq->revision,
 3407                         ciss_name_ldrive_status(cl->cl_lstatus->status),
 3408                         SID_REVISION_SIZE);
 3409         }
 3410     }
 3411 }
 3412 
 3413 
 3414 /********************************************************************************
 3415  * Name the device at (target)
 3416  *
 3417  * XXX is this strictly correct?
 3418  */
 3419 static int
 3420 ciss_name_device(struct ciss_softc *sc, int bus, int target)
 3421 {
 3422     struct cam_periph   *periph;
 3423     struct cam_path     *path;
 3424     int                 status;
 3425 
 3426     if (CISS_IS_PHYSICAL(bus))
 3427         return (0);
 3428 
 3429     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
 3430                              target, 0);
 3431 
 3432     if (status == CAM_REQ_CMP) {
 3433         xpt_path_lock(path);
 3434         periph = cam_periph_find(path, NULL);
 3435         xpt_path_unlock(path);
 3436         xpt_free_path(path);
 3437         if (periph != NULL) {
 3438                 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
 3439                         periph->periph_name, periph->unit_number);
 3440                 return(0);
 3441         }
 3442     }
 3443     sc->ciss_logical[bus][target].cl_name[0] = 0;
 3444     return(ENOENT);
 3445 }
 3446 
 3447 /************************************************************************
 3448  * Periodic status monitoring.
 3449  */
 3450 static void
 3451 ciss_periodic(void *arg)
 3452 {
 3453     struct ciss_softc   *sc;
 3454     struct ciss_request *cr = NULL;
 3455     struct ciss_command *cc = NULL;
 3456     int                 error = 0;
 3457 
 3458     debug_called(1);
 3459 
 3460     sc = (struct ciss_softc *)arg;
 3461 
 3462     /*
 3463      * Check the adapter heartbeat.
 3464      */
 3465     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
 3466         sc->ciss_heart_attack++;
 3467         debug(0, "adapter heart attack in progress 0x%x/%d",
 3468               sc->ciss_heartbeat, sc->ciss_heart_attack);
 3469         if (sc->ciss_heart_attack == 3) {
 3470             ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
 3471             ciss_disable_adapter(sc);
 3472             return;
 3473         }
 3474     } else {
 3475         sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
 3476         sc->ciss_heart_attack = 0;
 3477         debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
 3478     }
 3479 
 3480     /*
 3481      * Send the NOP message and wait for a response.
 3482      */
 3483     if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
 3484         cc = cr->cr_cc;
 3485         cr->cr_complete = ciss_nop_complete;
 3486         cc->cdb.cdb_length = 1;
 3487         cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
 3488         cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 3489         cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
 3490         cc->cdb.timeout = 0;
 3491         cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
 3492 
 3493         if ((error = ciss_start(cr)) != 0) {
 3494             ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
 3495         }
 3496     }
 3497 
 3498     /*
 3499      * If the notify event request has died for some reason, or has
 3500      * not started yet, restart it.
 3501      */
 3502     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
 3503         debug(0, "(re)starting Event Notify chain");
 3504         ciss_notify_event(sc);
 3505     }
 3506 
 3507     /*
 3508      * Reschedule.
 3509      */
 3510     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
 3511 }
 3512 
 3513 static void
 3514 ciss_nop_complete(struct ciss_request *cr)
 3515 {
 3516     struct ciss_softc           *sc;
 3517     static int                  first_time = 1;
 3518 
 3519     sc = cr->cr_sc;
 3520     if (ciss_report_request(cr, NULL, NULL) != 0) {
 3521         if (first_time == 1) {
 3522             first_time = 0;
 3523             ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
 3524         }
 3525     }
 3526 
 3527     ciss_release_request(cr);
 3528 }
 3529 
 3530 /************************************************************************
 3531  * Disable the adapter.
 3532  *
 3533  * The all requests in completed queue is failed with hardware error.
 3534  * This will cause failover in a multipath configuration.
 3535  */
 3536 static void
 3537 ciss_disable_adapter(struct ciss_softc *sc)
 3538 {
 3539     cr_qhead_t                  qh;
 3540     struct ciss_request         *cr;
 3541     struct ciss_command         *cc;
 3542     struct ciss_error_info      *ce;
 3543     int                         i;
 3544 
 3545     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
 3546     pci_disable_busmaster(sc->ciss_dev);
 3547     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
 3548 
 3549     STAILQ_INIT(&qh);
 3550     for (i = 1; i < sc->ciss_max_requests; i++) {
 3551         cr = &sc->ciss_request[i];
 3552         if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
 3553             continue;
 3554 
 3555         cc = cr->cr_cc;
 3556         ce = (struct ciss_error_info *)&(cc->sg[0]);
 3557         ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
 3558         ciss_enqueue_complete(cr, &qh);
 3559     }
 3560 
 3561     for (;;) {
 3562         if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
 3563             break;
 3564     
 3565         /*
 3566          * If the request has a callback, invoke it.
 3567          */
 3568         if (cr->cr_complete != NULL) {
 3569             cr->cr_complete(cr);
 3570             continue;
 3571         }
 3572 
 3573         /*
 3574          * If someone is sleeping on this request, wake them up.
 3575          */
 3576         if (cr->cr_flags & CISS_REQ_SLEEP) {
 3577             cr->cr_flags &= ~CISS_REQ_SLEEP;
 3578             wakeup(cr);
 3579             continue;
 3580         }
 3581     }
 3582 }
 3583 
 3584 /************************************************************************
 3585  * Request a notification response from the adapter.
 3586  *
 3587  * If (cr) is NULL, this is the first request of the adapter, so
 3588  * reset the adapter's message pointer and start with the oldest
 3589  * message available.
 3590  */
 3591 static void
 3592 ciss_notify_event(struct ciss_softc *sc)
 3593 {
 3594     struct ciss_request         *cr;
 3595     struct ciss_command         *cc;
 3596     struct ciss_notify_cdb      *cnc;
 3597     int                         error;
 3598 
 3599     debug_called(1);
 3600 
 3601     cr = sc->ciss_periodic_notify;
 3602 
 3603     /* get a request if we don't already have one */
 3604     if (cr == NULL) {
 3605         if ((error = ciss_get_request(sc, &cr)) != 0) {
 3606             debug(0, "can't get notify event request");
 3607             goto out;
 3608         }
 3609         sc->ciss_periodic_notify = cr;
 3610         cr->cr_complete = ciss_notify_complete;
 3611         debug(1, "acquired request %d", cr->cr_tag);
 3612     }
 3613 
 3614     /*
 3615      * Get a databuffer if we don't already have one, note that the
 3616      * adapter command wants a larger buffer than the actual
 3617      * structure.
 3618      */
 3619     if (cr->cr_data == NULL) {
 3620         if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
 3621             debug(0, "can't get notify event request buffer");
 3622             error = ENOMEM;
 3623             goto out;
 3624         }
 3625         cr->cr_length = CISS_NOTIFY_DATA_SIZE;
 3626     }
 3627 
 3628     /* re-setup the request's command (since we never release it) XXX overkill*/
 3629     ciss_preen_command(cr);
 3630 
 3631     /* (re)build the notify event command */
 3632     cc = cr->cr_cc;
 3633     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
 3634     cc->header.address.physical.bus = 0;
 3635     cc->header.address.physical.target = 0;
 3636 
 3637     cc->cdb.cdb_length = sizeof(*cnc);
 3638     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
 3639     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 3640     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
 3641     cc->cdb.timeout = 0;        /* no timeout, we hope */
 3642 
 3643     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
 3644     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
 3645     cnc->opcode = CISS_OPCODE_READ;
 3646     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
 3647     cnc->timeout = 0;           /* no timeout, we hope */
 3648     cnc->synchronous = 0;
 3649     cnc->ordered = 0;
 3650     cnc->seek_to_oldest = 0;
 3651     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
 3652         cnc->new_only = 1;
 3653     else
 3654         cnc->new_only = 0;
 3655     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
 3656 
 3657     /* submit the request */
 3658     error = ciss_start(cr);
 3659 
 3660  out:
 3661     if (error) {
 3662         if (cr != NULL) {
 3663             if (cr->cr_data != NULL)
 3664                 free(cr->cr_data, CISS_MALLOC_CLASS);
 3665             ciss_release_request(cr);
 3666         }
 3667         sc->ciss_periodic_notify = NULL;
 3668         debug(0, "can't submit notify event request");
 3669         sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
 3670     } else {
 3671         debug(1, "notify event submitted");
 3672         sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
 3673     }
 3674 }
 3675 
 3676 static void
 3677 ciss_notify_complete(struct ciss_request *cr)
 3678 {
 3679     struct ciss_command *cc;
 3680     struct ciss_notify  *cn;
 3681     struct ciss_softc   *sc;
 3682     int                 scsi_status;
 3683     int                 command_status;
 3684     debug_called(1);
 3685 
 3686     cc = cr->cr_cc;
 3687     cn = (struct ciss_notify *)cr->cr_data;
 3688     sc = cr->cr_sc;
 3689 
 3690     /*
 3691      * Report request results, decode status.
 3692      */
 3693     ciss_report_request(cr, &command_status, &scsi_status);
 3694 
 3695     /*
 3696      * Abort the chain on a fatal error.
 3697      *
 3698      * XXX which of these are actually errors?
 3699      */
 3700     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
 3701         (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
 3702         (command_status != CISS_CMD_STATUS_TIMEOUT)) {  /* XXX timeout? */
 3703         ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
 3704                     ciss_name_command_status(command_status));
 3705         ciss_release_request(cr);
 3706         sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
 3707         return;
 3708     }
 3709 
 3710     /*
 3711      * If the adapter gave us a text message, print it.
 3712      */
 3713     if (cn->message[0] != 0)
 3714         ciss_printf(sc, "*** %.80s\n", cn->message);
 3715 
 3716     debug(0, "notify event class %d subclass %d detail %d",
 3717                 cn->class, cn->subclass, cn->detail);
 3718 
 3719     /*
 3720      * If the response indicates that the notifier has been aborted,
 3721      * release the notifier command.
 3722      */
 3723     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
 3724         (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
 3725         (cn->detail == 1)) {
 3726         debug(0, "notifier exiting");
 3727         sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
 3728         ciss_release_request(cr);
 3729         sc->ciss_periodic_notify = NULL;
 3730         wakeup(&sc->ciss_periodic_notify);
 3731     } else {
 3732         /* Handle notify events in a kernel thread */
 3733         ciss_enqueue_notify(cr);
 3734         sc->ciss_periodic_notify = NULL;
 3735         wakeup(&sc->ciss_periodic_notify);
 3736         wakeup(&sc->ciss_notify);
 3737     }
 3738     /*
 3739      * Send a new notify event command, if we're not aborting.
 3740      */
 3741     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
 3742         ciss_notify_event(sc);
 3743     }
 3744 }
 3745 
 3746 /************************************************************************
 3747  * Abort the Notify Event chain.
 3748  *
 3749  * Note that we can't just abort the command in progress; we have to
 3750  * explicitly issue an Abort Notify Event command in order for the
 3751  * adapter to clean up correctly.
 3752  *
 3753  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
 3754  * the chain will not restart itself.
 3755  */
 3756 static int
 3757 ciss_notify_abort(struct ciss_softc *sc)
 3758 {
 3759     struct ciss_request         *cr;
 3760     struct ciss_command         *cc;
 3761     struct ciss_notify_cdb      *cnc;
 3762     int                         error, command_status, scsi_status;
 3763 
 3764     debug_called(1);
 3765 
 3766     cr = NULL;
 3767     error = 0;
 3768 
 3769     /* verify that there's an outstanding command */
 3770     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
 3771         goto out;
 3772 
 3773     /* get a command to issue the abort with */
 3774     if ((error = ciss_get_request(sc, &cr)))
 3775         goto out;
 3776 
 3777     /* get a buffer for the result */
 3778     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
 3779         debug(0, "can't get notify event request buffer");
 3780         error = ENOMEM;
 3781         goto out;
 3782     }
 3783     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
 3784 
 3785     /* build the CDB */
 3786     cc = cr->cr_cc;
 3787     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
 3788     cc->header.address.physical.bus = 0;
 3789     cc->header.address.physical.target = 0;
 3790     cc->cdb.cdb_length = sizeof(*cnc);
 3791     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
 3792     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
 3793     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
 3794     cc->cdb.timeout = 0;        /* no timeout, we hope */
 3795 
 3796     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
 3797     bzero(cnc, sizeof(*cnc));
 3798     cnc->opcode = CISS_OPCODE_WRITE;
 3799     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
 3800     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
 3801 #if 0
 3802     ciss_print_request(cr);
 3803 #endif
 3804 
 3805     /*
 3806      * Submit the request and wait for it to complete.
 3807      */
 3808     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 3809         ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
 3810         goto out;
 3811     }
 3812 
 3813     /*
 3814      * Check response.
 3815      */
 3816     ciss_report_request(cr, &command_status, &scsi_status);
 3817     switch(command_status) {
 3818     case CISS_CMD_STATUS_SUCCESS:
 3819         break;
 3820     case CISS_CMD_STATUS_INVALID_COMMAND:
 3821         /*
 3822          * Some older adapters don't support the CISS version of this
 3823          * command.  Fall back to using the BMIC version.
 3824          */
 3825         error = ciss_notify_abort_bmic(sc);
 3826         if (error != 0)
 3827             goto out;
 3828         break;
 3829 
 3830     case CISS_CMD_STATUS_TARGET_STATUS:
 3831         /*
 3832          * This can happen if the adapter thinks there wasn't an outstanding
 3833          * Notify Event command but we did.  We clean up here.
 3834          */
 3835         if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
 3836             if (sc->ciss_periodic_notify != NULL)
 3837                 ciss_release_request(sc->ciss_periodic_notify);
 3838             error = 0;
 3839             goto out;
 3840         }
 3841         /* FALLTHROUGH */
 3842 
 3843     default:
 3844         ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
 3845                     ciss_name_command_status(command_status));
 3846         error = EIO;
 3847         goto out;
 3848     }
 3849 
 3850     /*
 3851      * Sleep waiting for the notifier command to complete.  Note
 3852      * that if it doesn't, we may end up in a bad situation, since
 3853      * the adapter may deliver it later.  Also note that the adapter
 3854      * requires the Notify Event command to be cancelled in order to
 3855      * maintain internal bookkeeping.
 3856      */
 3857     while (sc->ciss_periodic_notify != NULL) {
 3858         error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
 3859         if (error == EWOULDBLOCK) {
 3860             ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
 3861             break;
 3862         }
 3863     }
 3864 
 3865  out:
 3866     /* release the cancel request */
 3867     if (cr != NULL) {
 3868         if (cr->cr_data != NULL)
 3869             free(cr->cr_data, CISS_MALLOC_CLASS);
 3870         ciss_release_request(cr);
 3871     }
 3872     if (error == 0)
 3873         sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
 3874     return(error);
 3875 }
 3876 
 3877 /************************************************************************
 3878  * Abort the Notify Event chain using a BMIC command.
 3879  */
 3880 static int
 3881 ciss_notify_abort_bmic(struct ciss_softc *sc)
 3882 {
 3883     struct ciss_request                 *cr;
 3884     int                                 error, command_status;
 3885 
 3886     debug_called(1);
 3887 
 3888     cr = NULL;
 3889     error = 0;
 3890 
 3891     /* verify that there's an outstanding command */
 3892     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
 3893         goto out;
 3894 
 3895     /*
 3896      * Build a BMIC command to cancel the Notify on Event command.
 3897      *
 3898      * Note that we are sending a CISS opcode here.  Odd.
 3899      */
 3900     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
 3901                                        NULL, 0)) != 0)
 3902         goto out;
 3903 
 3904     /*
 3905      * Submit the request and wait for it to complete.
 3906      */
 3907     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 3908         ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
 3909         goto out;
 3910     }
 3911 
 3912     /*
 3913      * Check response.
 3914      */
 3915     ciss_report_request(cr, &command_status, NULL);
 3916     switch(command_status) {
 3917     case CISS_CMD_STATUS_SUCCESS:
 3918         break;
 3919     default:
 3920         ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
 3921                     ciss_name_command_status(command_status));
 3922         error = EIO;
 3923         goto out;
 3924     }
 3925 
 3926 out:
 3927     if (cr != NULL)
 3928         ciss_release_request(cr);
 3929     return(error);
 3930 }
 3931 
 3932 /************************************************************************
 3933  * Handle rescanning all the logical volumes when a notify event
 3934  * causes the drives to come online or offline.
 3935  */
 3936 static void
 3937 ciss_notify_rescan_logical(struct ciss_softc *sc)
 3938 {
 3939     struct ciss_lun_report      *cll;
 3940     struct ciss_ldrive          *ld;
 3941     int                         i, j, ndrives;
 3942 
 3943     /*
 3944      * We must rescan all logical volumes to get the right logical
 3945      * drive address.
 3946      */
 3947     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
 3948                            sc->ciss_cfg->max_logical_supported);
 3949     if (cll == NULL)
 3950         return;
 3951 
 3952     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
 3953 
 3954     /*
 3955      * Delete any of the drives which were destroyed by the
 3956      * firmware.
 3957      */
 3958     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 3959         for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
 3960             ld = &sc->ciss_logical[i][j];
 3961 
 3962             if (ld->cl_update == 0)
 3963                 continue;
 3964 
 3965             if (ld->cl_status != CISS_LD_ONLINE) {
 3966                 ciss_cam_rescan_target(sc, i, j);
 3967                 ld->cl_update = 0;
 3968                 if (ld->cl_ldrive)
 3969                     free(ld->cl_ldrive, CISS_MALLOC_CLASS);
 3970                 if (ld->cl_lstatus)
 3971                     free(ld->cl_lstatus, CISS_MALLOC_CLASS);
 3972 
 3973                 ld->cl_ldrive = NULL;
 3974                 ld->cl_lstatus = NULL;
 3975             }
 3976         }
 3977     }
 3978 
 3979     /*
 3980      * Scan for new drives.
 3981      */
 3982     for (i = 0; i < ndrives; i++) {
 3983         int     bus, target;
 3984 
 3985         bus     = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
 3986         target  = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
 3987         ld      = &sc->ciss_logical[bus][target];
 3988 
 3989         if (ld->cl_update == 0)
 3990                 continue;
 3991 
 3992         ld->cl_update           = 0;
 3993         ld->cl_address          = cll->lun[i];
 3994         ld->cl_controller       = &sc->ciss_controllers[bus];
 3995         if (ciss_identify_logical(sc, ld) == 0) {
 3996             ciss_cam_rescan_target(sc, bus, target);
 3997         }
 3998     }
 3999     free(cll, CISS_MALLOC_CLASS);
 4000 }
 4001 
 4002 /************************************************************************
 4003  * Handle a notify event relating to the status of a logical drive.
 4004  *
 4005  * XXX need to be able to defer some of these to properly handle
 4006  *     calling the "ID Physical drive" command, unless the 'extended'
 4007  *     drive IDs are always in BIG_MAP format.
 4008  */
 4009 static void
 4010 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
 4011 {
 4012     struct ciss_ldrive  *ld;
 4013     int                 ostatus, bus, target;
 4014 
 4015     debug_called(2);
 4016 
 4017     bus         = cn->device.physical.bus;
 4018     target      = cn->data.logical_status.logical_drive;
 4019     ld          = &sc->ciss_logical[bus][target];
 4020 
 4021     switch (cn->subclass) {
 4022     case CISS_NOTIFY_LOGICAL_STATUS:
 4023         switch (cn->detail) {
 4024         case 0:
 4025             ciss_name_device(sc, bus, target);
 4026             ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
 4027                         cn->data.logical_status.logical_drive, ld->cl_name,
 4028                         ciss_name_ldrive_status(cn->data.logical_status.previous_state),
 4029                         ciss_name_ldrive_status(cn->data.logical_status.new_state),
 4030                         cn->data.logical_status.spare_state,
 4031                         "\2\1configured\2rebuilding\3failed\4in use\5available\n");
 4032 
 4033             /*
 4034              * Update our idea of the drive's status.
 4035              */
 4036             ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
 4037             ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
 4038             if (ld->cl_lstatus != NULL)
 4039                 ld->cl_lstatus->status = cn->data.logical_status.new_state;
 4040 
 4041             /*
 4042              * Have CAM rescan the drive if its status has changed.
 4043              */
 4044             if (ostatus != ld->cl_status) {
 4045                 ld->cl_update = 1;
 4046                 ciss_notify_rescan_logical(sc);
 4047             }
 4048 
 4049             break;
 4050 
 4051         case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
 4052             ciss_name_device(sc, bus, target);
 4053             ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
 4054                         cn->data.logical_status.logical_drive, ld->cl_name);
 4055             ciss_accept_media(sc, ld);
 4056 
 4057             ld->cl_update = 1;
 4058             ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
 4059             ciss_notify_rescan_logical(sc);
 4060             break;
 4061 
 4062         case 2:
 4063         case 3:
 4064             ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
 4065                         cn->data.rebuild_aborted.logical_drive,
 4066                         ld->cl_name,
 4067                         (cn->detail == 2) ? "read" : "write");
 4068             break;
 4069         }
 4070         break;
 4071 
 4072     case CISS_NOTIFY_LOGICAL_ERROR:
 4073         if (cn->detail == 0) {
 4074             ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
 4075                         cn->data.io_error.logical_drive,
 4076                         ld->cl_name,
 4077                         cn->data.io_error.failure_bus,
 4078                         cn->data.io_error.failure_drive);
 4079             /* XXX should we take the drive down at this point, or will we be told? */
 4080         }
 4081         break;
 4082 
 4083     case CISS_NOTIFY_LOGICAL_SURFACE:
 4084         if (cn->detail == 0)
 4085             ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
 4086                         cn->data.consistency_completed.logical_drive,
 4087                         ld->cl_name);
 4088         break;
 4089     }
 4090 }
 4091 
 4092 /************************************************************************
 4093  * Handle a notify event relating to the status of a physical drive.
 4094  */
 4095 static void
 4096 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
 4097 {
 4098 }
 4099 
 4100 /************************************************************************
 4101  * Handle a notify event relating to the status of a physical drive.
 4102  */
 4103 static void
 4104 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
 4105 {
 4106     struct ciss_lun_report *cll = NULL;
 4107     int bus, target;
 4108 
 4109     switch (cn->subclass) {
 4110     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
 4111     case CISS_NOTIFY_HOTPLUG_NONDISK:
 4112         bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
 4113         target =
 4114             CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
 4115 
 4116         if (cn->detail == 0) {
 4117             /*
 4118              * Mark the device offline so that it'll start producing selection
 4119              * timeouts to the upper layer.
 4120              */
 4121             if ((bus >= 0) && (target >= 0))
 4122                 sc->ciss_physical[bus][target].cp_online = 0;
 4123         } else {
 4124             /*
 4125              * Rescan the physical lun list for new items
 4126              */
 4127             cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
 4128                                    sc->ciss_cfg->max_physical_supported);
 4129             if (cll == NULL) {
 4130                 ciss_printf(sc, "Warning, cannot get physical lun list\n");
 4131                 break;
 4132             }
 4133             ciss_filter_physical(sc, cll);
 4134         }
 4135         break;
 4136 
 4137     default:
 4138         ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
 4139         return;
 4140     }
 4141 
 4142     if (cll != NULL)
 4143         free(cll, CISS_MALLOC_CLASS);
 4144 }
 4145 
 4146 /************************************************************************
 4147  * Handle deferred processing of notify events.  Notify events may need
 4148  * sleep which is unsafe during an interrupt.
 4149  */
 4150 static void
 4151 ciss_notify_thread(void *arg)
 4152 {
 4153     struct ciss_softc           *sc;
 4154     struct ciss_request         *cr;
 4155     struct ciss_notify          *cn;
 4156 
 4157     sc = (struct ciss_softc *)arg;
 4158     mtx_lock(&sc->ciss_mtx);
 4159 
 4160     for (;;) {
 4161         if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
 4162             (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
 4163             msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
 4164         }
 4165 
 4166         if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
 4167             break;
 4168 
 4169         cr = ciss_dequeue_notify(sc);
 4170 
 4171         if (cr == NULL)
 4172                 panic("cr null");
 4173         cn = (struct ciss_notify *)cr->cr_data;
 4174 
 4175         switch (cn->class) {
 4176         case CISS_NOTIFY_HOTPLUG:
 4177             ciss_notify_hotplug(sc, cn);
 4178             break;
 4179         case CISS_NOTIFY_LOGICAL:
 4180             ciss_notify_logical(sc, cn);
 4181             break;
 4182         case CISS_NOTIFY_PHYSICAL:
 4183             ciss_notify_physical(sc, cn);
 4184             break;
 4185         }
 4186 
 4187         ciss_release_request(cr);
 4188 
 4189     }
 4190     sc->ciss_notify_thread = NULL;
 4191     wakeup(&sc->ciss_notify_thread);
 4192 
 4193     mtx_unlock(&sc->ciss_mtx);
 4194     kproc_exit(0);
 4195 }
 4196 
 4197 /************************************************************************
 4198  * Start the notification kernel thread.
 4199  */
 4200 static void
 4201 ciss_spawn_notify_thread(struct ciss_softc *sc)
 4202 {
 4203 
 4204     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
 4205                        &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
 4206                        device_get_unit(sc->ciss_dev)))
 4207         panic("Could not create notify thread\n");
 4208 }
 4209 
 4210 /************************************************************************
 4211  * Kill the notification kernel thread.
 4212  */
 4213 static void
 4214 ciss_kill_notify_thread(struct ciss_softc *sc)
 4215 {
 4216 
 4217     if (sc->ciss_notify_thread == NULL)
 4218         return;
 4219 
 4220     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
 4221     wakeup(&sc->ciss_notify);
 4222     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
 4223 }
 4224 
 4225 /************************************************************************
 4226  * Print a request.
 4227  */
 4228 #ifdef DDB
 4229 static void
 4230 ciss_print_request(struct ciss_request *cr)
 4231 {
 4232     struct ciss_softc   *sc;
 4233     struct ciss_command *cc;
 4234     int                 i;
 4235 
 4236     sc = cr->cr_sc;
 4237     cc = cr->cr_cc;
 4238 
 4239     ciss_printf(sc, "REQUEST @ %p\n", cr);
 4240     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
 4241               cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
 4242               "\2\1mapped\2sleep\3poll\4dataout\5datain\n");
 4243     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
 4244                 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
 4245     switch(cc->header.address.mode.mode) {
 4246     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
 4247     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
 4248         ciss_printf(sc, "  physical bus %d target %d\n",
 4249                     cc->header.address.physical.bus, cc->header.address.physical.target);
 4250         break;
 4251     case CISS_HDR_ADDRESS_MODE_LOGICAL:
 4252         ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
 4253         break;
 4254     }
 4255     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
 4256                 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
 4257                 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
 4258                 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
 4259                 cc->cdb.cdb_length,
 4260                 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
 4261                 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
 4262                 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
 4263                 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
 4264                 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
 4265                 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
 4266                 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
 4267     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
 4268 
 4269     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
 4270         /* XXX print error info */
 4271     } else {
 4272         /* since we don't use chained s/g, don't support it here */
 4273         for (i = 0; i < cc->header.sg_in_list; i++) {
 4274             if ((i % 4) == 0)
 4275                 ciss_printf(sc, "   ");
 4276             printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
 4277             if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
 4278                 printf("\n");
 4279         }
 4280     }
 4281 }
 4282 #endif
 4283 
 4284 /************************************************************************
 4285  * Print information about the status of a logical drive.
 4286  */
 4287 static void
 4288 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
 4289 {
 4290     int         bus, target, i;
 4291 
 4292     if (ld->cl_lstatus == NULL) {
 4293         printf("does not exist\n");
 4294         return;
 4295     }
 4296 
 4297     /* print drive status */
 4298     switch(ld->cl_lstatus->status) {
 4299     case CISS_LSTATUS_OK:
 4300         printf("online\n");
 4301         break;
 4302     case CISS_LSTATUS_INTERIM_RECOVERY:
 4303         printf("in interim recovery mode\n");
 4304         break;
 4305     case CISS_LSTATUS_READY_RECOVERY:
 4306         printf("ready to begin recovery\n");
 4307         break;
 4308     case CISS_LSTATUS_RECOVERING:
 4309         bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
 4310         target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
 4311         printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
 4312                bus, target, ld->cl_lstatus->blocks_to_recover);
 4313         break;
 4314     case CISS_LSTATUS_EXPANDING:
 4315         printf("being expanded, %u blocks remaining\n",
 4316                ld->cl_lstatus->blocks_to_recover);
 4317         break;
 4318     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
 4319         printf("queued for expansion\n");
 4320         break;
 4321     case CISS_LSTATUS_FAILED:
 4322         printf("queued for expansion\n");
 4323         break;
 4324     case CISS_LSTATUS_WRONG_PDRIVE:
 4325         printf("wrong physical drive inserted\n");
 4326         break;
 4327     case CISS_LSTATUS_MISSING_PDRIVE:
 4328         printf("missing a needed physical drive\n");
 4329         break;
 4330     case CISS_LSTATUS_BECOMING_READY:
 4331         printf("becoming ready\n");
 4332         break;
 4333     }
 4334 
 4335     /* print failed physical drives */
 4336     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
 4337         bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
 4338         target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
 4339         if (bus == -1)
 4340             continue;
 4341         ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
 4342                     ld->cl_lstatus->drive_failure_map[i]);
 4343     }
 4344 }
 4345 
 4346 #ifdef DDB
 4347 #include <ddb/ddb.h>
 4348 /************************************************************************
 4349  * Print information about the controller/driver.
 4350  */
 4351 static void
 4352 ciss_print_adapter(struct ciss_softc *sc)
 4353 {
 4354     int         i, j;
 4355 
 4356     ciss_printf(sc, "ADAPTER:\n");
 4357     for (i = 0; i < CISSQ_COUNT; i++) {
 4358         ciss_printf(sc, "%s     %d/%d\n",
 4359             i == 0 ? "free" :
 4360             i == 1 ? "busy" : "complete",
 4361             sc->ciss_qstat[i].q_length,
 4362             sc->ciss_qstat[i].q_max);
 4363     }
 4364     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
 4365     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
 4366         "\2\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
 4367 
 4368     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
 4369         for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
 4370             ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
 4371             ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
 4372         }
 4373     }
 4374 
 4375     /* XXX Should physical drives be printed out here? */
 4376 
 4377     for (i = 1; i < sc->ciss_max_requests; i++)
 4378         ciss_print_request(sc->ciss_request + i);
 4379 }
 4380 
 4381 /* DDB hook */
 4382 DB_COMMAND(ciss_prt, db_ciss_prt)
 4383 {
 4384     struct ciss_softc   *sc;
 4385     devclass_t dc;
 4386     int maxciss, i;
 4387 
 4388     dc = devclass_find("ciss");
 4389     if ( dc == NULL ) {
 4390         printf("%s: can't find devclass!\n", __func__);
 4391         return;
 4392     }
 4393     maxciss = devclass_get_maxunit(dc);
 4394     for (i = 0; i < maxciss; i++) {
 4395         sc = devclass_get_softc(dc, i);
 4396         ciss_print_adapter(sc);
 4397     }
 4398 }
 4399 #endif
 4400 
 4401 /************************************************************************
 4402  * Return a name for a logical drive status value.
 4403  */
 4404 static const char *
 4405 ciss_name_ldrive_status(int status)
 4406 {
 4407     switch (status) {
 4408     case CISS_LSTATUS_OK:
 4409         return("OK");
 4410     case CISS_LSTATUS_FAILED:
 4411         return("failed");
 4412     case CISS_LSTATUS_NOT_CONFIGURED:
 4413         return("not configured");
 4414     case CISS_LSTATUS_INTERIM_RECOVERY:
 4415         return("interim recovery");
 4416     case CISS_LSTATUS_READY_RECOVERY:
 4417         return("ready for recovery");
 4418     case CISS_LSTATUS_RECOVERING:
 4419         return("recovering");
 4420     case CISS_LSTATUS_WRONG_PDRIVE:
 4421         return("wrong physical drive inserted");
 4422     case CISS_LSTATUS_MISSING_PDRIVE:
 4423         return("missing physical drive");
 4424     case CISS_LSTATUS_EXPANDING:
 4425         return("expanding");
 4426     case CISS_LSTATUS_BECOMING_READY:
 4427         return("becoming ready");
 4428     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
 4429         return("queued for expansion");
 4430     }
 4431     return("unknown status");
 4432 }
 4433 
 4434 /************************************************************************
 4435  * Return an online/offline/nonexistent value for a logical drive
 4436  * status value.
 4437  */
 4438 static int
 4439 ciss_decode_ldrive_status(int status)
 4440 {
 4441     switch(status) {
 4442     case CISS_LSTATUS_NOT_CONFIGURED:
 4443         return(CISS_LD_NONEXISTENT);
 4444 
 4445     case CISS_LSTATUS_OK:
 4446     case CISS_LSTATUS_INTERIM_RECOVERY:
 4447     case CISS_LSTATUS_READY_RECOVERY:
 4448     case CISS_LSTATUS_RECOVERING:
 4449     case CISS_LSTATUS_EXPANDING:
 4450     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
 4451         return(CISS_LD_ONLINE);
 4452 
 4453     case CISS_LSTATUS_FAILED:
 4454     case CISS_LSTATUS_WRONG_PDRIVE:
 4455     case CISS_LSTATUS_MISSING_PDRIVE:
 4456     case CISS_LSTATUS_BECOMING_READY:
 4457     default:
 4458         return(CISS_LD_OFFLINE);
 4459     }
 4460 }
 4461 
 4462 
 4463 /************************************************************************
 4464  * Return a name for a logical drive's organisation.
 4465  */
 4466 static const char *
 4467 ciss_name_ldrive_org(int org)
 4468 {
 4469     switch(org) {
 4470     case CISS_LDRIVE_RAID0:
 4471         return("RAID 0");
 4472     case CISS_LDRIVE_RAID1:
 4473         return("RAID 1(1+0)");
 4474     case CISS_LDRIVE_RAID4:
 4475         return("RAID 4");
 4476     case CISS_LDRIVE_RAID5:
 4477         return("RAID 5");
 4478     case CISS_LDRIVE_RAID51:
 4479         return("RAID 5+1");
 4480     case CISS_LDRIVE_RAIDADG:
 4481         return("RAID ADG");
 4482     }
 4483     return("unknown");
 4484 }
 4485 
 4486 /************************************************************************
 4487  * Return a name for a command status value.
 4488  */
 4489 static const char *
 4490 ciss_name_command_status(int status)
 4491 {
 4492     switch(status) {
 4493     case CISS_CMD_STATUS_SUCCESS:
 4494         return("success");
 4495     case CISS_CMD_STATUS_TARGET_STATUS:
 4496         return("target status");
 4497     case CISS_CMD_STATUS_DATA_UNDERRUN:
 4498         return("data underrun");
 4499     case CISS_CMD_STATUS_DATA_OVERRUN:
 4500         return("data overrun");
 4501     case CISS_CMD_STATUS_INVALID_COMMAND:
 4502         return("invalid command");
 4503     case CISS_CMD_STATUS_PROTOCOL_ERROR:
 4504         return("protocol error");
 4505     case CISS_CMD_STATUS_HARDWARE_ERROR:
 4506         return("hardware error");
 4507     case CISS_CMD_STATUS_CONNECTION_LOST:
 4508         return("connection lost");
 4509     case CISS_CMD_STATUS_ABORTED:
 4510         return("aborted");
 4511     case CISS_CMD_STATUS_ABORT_FAILED:
 4512         return("abort failed");
 4513     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
 4514         return("unsolicited abort");
 4515     case CISS_CMD_STATUS_TIMEOUT:
 4516         return("timeout");
 4517     case CISS_CMD_STATUS_UNABORTABLE:
 4518         return("unabortable");
 4519     }
 4520     return("unknown status");
 4521 }
 4522 
 4523 /************************************************************************
 4524  * Handle an open on the control device.
 4525  */
 4526 static int
 4527 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
 4528 {
 4529     struct ciss_softc   *sc;
 4530 
 4531     debug_called(1);
 4532 
 4533     sc = (struct ciss_softc *)dev->si_drv1;
 4534 
 4535     /* we might want to veto if someone already has us open */
 4536 
 4537     mtx_lock(&sc->ciss_mtx);
 4538     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
 4539     mtx_unlock(&sc->ciss_mtx);
 4540     return(0);
 4541 }
 4542 
 4543 /************************************************************************
 4544  * Handle the last close on the control device.
 4545  */
 4546 static int
 4547 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
 4548 {
 4549     struct ciss_softc   *sc;
 4550 
 4551     debug_called(1);
 4552 
 4553     sc = (struct ciss_softc *)dev->si_drv1;
 4554 
 4555     mtx_lock(&sc->ciss_mtx);
 4556     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
 4557     mtx_unlock(&sc->ciss_mtx);
 4558     return (0);
 4559 }
 4560 
 4561 /********************************************************************************
 4562  * Handle adapter-specific control operations.
 4563  *
 4564  * Note that the API here is compatible with the Linux driver, in order to
 4565  * simplify the porting of Compaq's userland tools.
 4566  */
 4567 static int
 4568 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
 4569 {
 4570     struct ciss_softc           *sc;
 4571     IOCTL_Command_struct        *ioc    = (IOCTL_Command_struct *)addr;
 4572 #ifdef __amd64__
 4573     IOCTL_Command_struct32      *ioc32  = (IOCTL_Command_struct32 *)addr;
 4574     IOCTL_Command_struct        ioc_swab;
 4575 #endif
 4576     int                         error;
 4577 
 4578     debug_called(1);
 4579 
 4580     sc = (struct ciss_softc *)dev->si_drv1;
 4581     error = 0;
 4582     mtx_lock(&sc->ciss_mtx);
 4583 
 4584     switch(cmd) {
 4585     case CCISS_GETQSTATS:
 4586     {
 4587         union ciss_statrequest *cr = (union ciss_statrequest *)addr;
 4588 
 4589         switch (cr->cs_item) {
 4590         case CISSQ_FREE:
 4591         case CISSQ_NOTIFY:
 4592             bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
 4593                 sizeof(struct ciss_qstat));
 4594             break;
 4595         default:
 4596             error = ENOIOCTL;
 4597             break;
 4598         }
 4599 
 4600         break;
 4601     }
 4602 
 4603     case CCISS_GETPCIINFO:
 4604     {
 4605         cciss_pci_info_struct   *pis = (cciss_pci_info_struct *)addr;
 4606 
 4607         pis->bus = pci_get_bus(sc->ciss_dev);
 4608         pis->dev_fn = pci_get_slot(sc->ciss_dev);
 4609         pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
 4610                 pci_get_subdevice(sc->ciss_dev);
 4611 
 4612         break;
 4613     }
 4614 
 4615     case CCISS_GETINTINFO:
 4616     {
 4617         cciss_coalint_struct    *cis = (cciss_coalint_struct *)addr;
 4618 
 4619         cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
 4620         cis->count = sc->ciss_cfg->interrupt_coalesce_count;
 4621 
 4622         break;
 4623     }
 4624 
 4625     case CCISS_SETINTINFO:
 4626     {
 4627         cciss_coalint_struct    *cis = (cciss_coalint_struct *)addr;
 4628 
 4629         if ((cis->delay == 0) && (cis->count == 0)) {
 4630             error = EINVAL;
 4631             break;
 4632         }
 4633 
 4634         /*
 4635          * XXX apparently this is only safe if the controller is idle,
 4636          *     we should suspend it before doing this.
 4637          */
 4638         sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
 4639         sc->ciss_cfg->interrupt_coalesce_count = cis->count;
 4640 
 4641         if (ciss_update_config(sc))
 4642             error = EIO;
 4643 
 4644         /* XXX resume the controller here */
 4645         break;
 4646     }
 4647 
 4648     case CCISS_GETNODENAME:
 4649         bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
 4650               sizeof(NodeName_type));
 4651         break;
 4652 
 4653     case CCISS_SETNODENAME:
 4654         bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
 4655               sizeof(NodeName_type));
 4656         if (ciss_update_config(sc))
 4657             error = EIO;
 4658         break;
 4659 
 4660     case CCISS_GETHEARTBEAT:
 4661         *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
 4662         break;
 4663 
 4664     case CCISS_GETBUSTYPES:
 4665         *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
 4666         break;
 4667 
 4668     case CCISS_GETFIRMVER:
 4669         bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
 4670               sizeof(FirmwareVer_type));
 4671         break;
 4672 
 4673     case CCISS_GETDRIVERVER:
 4674         *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
 4675         break;
 4676 
 4677     case CCISS_REVALIDVOLS:
 4678         /*
 4679          * This is a bit ugly; to do it "right" we really need
 4680          * to find any disks that have changed, kick CAM off them,
 4681          * then rescan only these disks.  It'd be nice if they
 4682          * a) told us which disk(s) they were going to play with,
 4683          * and b) which ones had arrived. 8(
 4684          */
 4685         break;
 4686 
 4687 #ifdef __amd64__
 4688     case CCISS_PASSTHRU32:
 4689         ioc_swab.LUN_info       = ioc32->LUN_info;
 4690         ioc_swab.Request        = ioc32->Request;
 4691         ioc_swab.error_info     = ioc32->error_info;
 4692         ioc_swab.buf_size       = ioc32->buf_size;
 4693         ioc_swab.buf            = (u_int8_t *)(uintptr_t)ioc32->buf;
 4694         ioc                     = &ioc_swab;
 4695         /* FALLTHROUGH */
 4696 #endif
 4697 
 4698     case CCISS_PASSTHRU:
 4699         error = ciss_user_command(sc, ioc);
 4700         break;
 4701 
 4702     default:
 4703         debug(0, "unknown ioctl 0x%lx", cmd);
 4704 
 4705         debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
 4706         debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
 4707         debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
 4708         debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
 4709         debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
 4710         debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
 4711         debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
 4712         debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
 4713         debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
 4714         debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
 4715         debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
 4716 
 4717         error = ENOIOCTL;
 4718         break;
 4719     }
 4720 
 4721     mtx_unlock(&sc->ciss_mtx);
 4722     return(error);
 4723 }

Cache object: 932fb1929233a1f3376394563ba5f539


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