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

Cache object: 5acbeded597db52a05384f95703414ad


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