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/buslogic/bt.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  * Generic driver for the BusLogic MultiMaster SCSI host adapters
    3  * Product specific probe and attach routines can be found in:
    4  * sys/dev/buslogic/bt_isa.c    BT-54X, BT-445 cards
    5  * sys/dev/buslogic/bt_mca.c    BT-64X, SDC3211B, SDC3211F
    6  * sys/dev/buslogic/bt_eisa.c   BT-74X, BT-75x cards, SDC3222F
    7  * sys/dev/buslogic/bt_pci.c    BT-946, BT-948, BT-956, BT-958 cards
    8  *
    9  * Copyright (c) 1998, 1999 Justin T. Gibbs.
   10  * All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions, and the following disclaimer,
   17  *    without modification, immediately at the beginning of the file.
   18  * 2. The name of the author may not be used to endorse or promote products
   19  *    derived from this software without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/6.2/sys/dev/buslogic/bt.c 165449 2006-12-21 21:00:52Z mjacob $");
   36 
   37  /*
   38   * Special thanks to Leonard N. Zubkoff for writing such a complete and
   39   * well documented Mylex/BusLogic MultiMaster driver for Linux.  Support
   40   * in this driver for the wide range of MultiMaster controllers and
   41   * firmware revisions, with their otherwise undocumented quirks, would not
   42   * have been possible without his efforts.
   43   */
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h> 
   47 #include <sys/malloc.h>
   48 #include <sys/kernel.h>
   49 #include <sys/lock.h>
   50 #include <sys/module.h>
   51 #include <sys/mutex.h>
   52 #include <sys/sysctl.h>
   53 #include <sys/bus.h>
   54  
   55 #include <machine/bus.h>
   56 #include <sys/rman.h>
   57 
   58 #include <cam/cam.h>
   59 #include <cam/cam_ccb.h>
   60 #include <cam/cam_sim.h>
   61 #include <cam/cam_xpt_sim.h>
   62 #include <cam/cam_debug.h>
   63 
   64 #include <cam/scsi/scsi_message.h>
   65 
   66 #include <vm/vm.h>
   67 #include <vm/pmap.h>
   68  
   69 #include <dev/buslogic/btreg.h>
   70 
   71 /* MailBox Management functions */
   72 static __inline void    btnextinbox(struct bt_softc *bt);
   73 static __inline void    btnextoutbox(struct bt_softc *bt);
   74 
   75 static __inline void
   76 btnextinbox(struct bt_softc *bt)
   77 {
   78         if (bt->cur_inbox == bt->last_inbox)
   79                 bt->cur_inbox = bt->in_boxes;
   80         else
   81                 bt->cur_inbox++;
   82 }
   83 
   84 static __inline void
   85 btnextoutbox(struct bt_softc *bt)
   86 {
   87         if (bt->cur_outbox == bt->last_outbox)
   88                 bt->cur_outbox = bt->out_boxes;
   89         else
   90                 bt->cur_outbox++;
   91 }
   92 
   93 /* CCB Mangement functions */
   94 static __inline u_int32_t               btccbvtop(struct bt_softc *bt,
   95                                                   struct bt_ccb *bccb);
   96 static __inline struct bt_ccb*          btccbptov(struct bt_softc *bt,
   97                                                   u_int32_t ccb_addr);
   98 static __inline u_int32_t               btsensepaddr(struct bt_softc *bt,
   99                                                      struct bt_ccb *bccb);
  100 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
  101                                                      struct bt_ccb *bccb);
  102 
  103 static __inline u_int32_t
  104 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
  105 {
  106         return (bt->bt_ccb_physbase
  107               + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
  108 }
  109 
  110 static __inline struct bt_ccb *
  111 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
  112 {
  113         return (bt->bt_ccb_array +
  114                 ((struct bt_ccb*)(uintptr_t)ccb_addr - (struct bt_ccb*)(uintptr_t)bt->bt_ccb_physbase));
  115 }
  116 
  117 static __inline u_int32_t
  118 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
  119 {
  120         u_int index;
  121 
  122         index = (u_int)(bccb - bt->bt_ccb_array);
  123         return (bt->sense_buffers_physbase
  124                 + (index * sizeof(struct scsi_sense_data)));
  125 }
  126 
  127 static __inline struct scsi_sense_data *
  128 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
  129 {
  130         u_int index;
  131 
  132         index = (u_int)(bccb - bt->bt_ccb_array);
  133         return (bt->sense_buffers + index);
  134 }
  135 
  136 static __inline struct bt_ccb*  btgetccb(struct bt_softc *bt);
  137 static __inline void            btfreeccb(struct bt_softc *bt,
  138                                           struct bt_ccb *bccb);
  139 static void             btallocccbs(struct bt_softc *bt);
  140 static bus_dmamap_callback_t btexecuteccb;
  141 static void             btdone(struct bt_softc *bt, struct bt_ccb *bccb,
  142                                bt_mbi_comp_code_t comp_code);
  143 
  144 /* Host adapter command functions */
  145 static int      btreset(struct bt_softc* bt, int hard_reset);
  146 
  147 /* Initialization functions */
  148 static int                      btinitmboxes(struct bt_softc *bt);
  149 static bus_dmamap_callback_t    btmapmboxes;
  150 static bus_dmamap_callback_t    btmapccbs;
  151 static bus_dmamap_callback_t    btmapsgs;
  152 
  153 /* Transfer Negotiation Functions */
  154 static void btfetchtransinfo(struct bt_softc *bt,
  155                              struct ccb_trans_settings *cts);
  156 
  157 /* CAM SIM entry points */
  158 #define ccb_bccb_ptr spriv_ptr0
  159 #define ccb_bt_ptr spriv_ptr1
  160 static void     btaction(struct cam_sim *sim, union ccb *ccb);
  161 static void     btpoll(struct cam_sim *sim);
  162 
  163 /* Our timeout handler */
  164 timeout_t bttimeout;
  165 
  166 u_long bt_unit = 0;
  167 
  168 /*
  169  * XXX
  170  * Do our own re-probe protection until a configuration
  171  * manager can do it for us.  This ensures that we don't
  172  * reprobe a card already found by the EISA or PCI probes.
  173  */
  174 struct bt_isa_port bt_isa_ports[] =
  175 {
  176         { 0x130, 0, 4 },
  177         { 0x134, 0, 5 },
  178         { 0x230, 0, 2 },
  179         { 0x234, 0, 3 },
  180         { 0x330, 0, 0 },
  181         { 0x334, 0, 1 }
  182 };
  183 
  184 /*
  185  * I/O ports listed in the order enumerated by the
  186  * card for certain op codes.
  187  */
  188 u_int16_t bt_board_ports[] =
  189 {
  190         0x330,
  191         0x334,
  192         0x230,
  193         0x234,
  194         0x130,
  195         0x134
  196 };
  197 
  198 /* Exported functions */
  199 void
  200 bt_init_softc(device_t dev, struct resource *port,
  201               struct resource *irq, struct resource *drq)
  202 {
  203         struct bt_softc *bt = device_get_softc(dev);
  204 
  205         SLIST_INIT(&bt->free_bt_ccbs);
  206         LIST_INIT(&bt->pending_ccbs);
  207         SLIST_INIT(&bt->sg_maps);
  208         bt->dev = dev;
  209         bt->unit = device_get_unit(dev);
  210         bt->port = port;
  211         bt->irq = irq;
  212         bt->drq = drq;
  213         bt->tag = rman_get_bustag(port);
  214         bt->bsh = rman_get_bushandle(port);
  215 }
  216 
  217 void
  218 bt_free_softc(device_t dev)
  219 {
  220         struct bt_softc *bt = device_get_softc(dev);
  221 
  222         switch (bt->init_level) {
  223         default:
  224         case 11:
  225                 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
  226         case 10:
  227                 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
  228                                 bt->sense_dmamap);
  229         case 9:
  230                 bus_dma_tag_destroy(bt->sense_dmat);
  231         case 8:
  232         {
  233                 struct sg_map_node *sg_map;
  234 
  235                 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
  236                         SLIST_REMOVE_HEAD(&bt->sg_maps, links);
  237                         bus_dmamap_unload(bt->sg_dmat,
  238                                           sg_map->sg_dmamap);
  239                         bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
  240                                         sg_map->sg_dmamap);
  241                         free(sg_map, M_DEVBUF);
  242                 }
  243                 bus_dma_tag_destroy(bt->sg_dmat);
  244         }
  245         case 7:
  246                 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
  247                 /* FALLTHROUGH */
  248         case 6:
  249                 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
  250                                 bt->ccb_dmamap);
  251                 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
  252                 /* FALLTHROUGH */
  253         case 5:
  254                 bus_dma_tag_destroy(bt->ccb_dmat);
  255                 /* FALLTHROUGH */
  256         case 4:
  257                 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
  258                 /* FALLTHROUGH */
  259         case 3:
  260                 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
  261                                 bt->mailbox_dmamap);
  262                 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
  263                 /* FALLTHROUGH */
  264         case 2:
  265                 bus_dma_tag_destroy(bt->buffer_dmat);
  266                 /* FALLTHROUGH */
  267         case 1:
  268                 bus_dma_tag_destroy(bt->mailbox_dmat);
  269                 /* FALLTHROUGH */
  270         case 0:
  271                 break;
  272         }
  273 }
  274 
  275 int
  276 bt_port_probe(device_t dev, struct bt_probe_info *info)
  277 {
  278         struct bt_softc *bt = device_get_softc(dev);
  279         config_data_t config_data;
  280         int error;
  281 
  282         /* See if there is really a card present */
  283         if (bt_probe(dev) || bt_fetch_adapter_info(dev))
  284                 return(1);
  285 
  286         /*
  287          * Determine our IRQ, and DMA settings and
  288          * export them to the configuration system.
  289          */
  290         error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
  291                        (u_int8_t*)&config_data, sizeof(config_data),
  292                        DEFAULT_CMD_TIMEOUT);
  293         if (error != 0) {
  294                 printf("bt_port_probe: Could not determine IRQ or DMA "
  295                        "settings for adapter.\n");
  296                 return (1);
  297         }
  298 
  299         if (bt->model[0] == '5') {
  300                 /* DMA settings only make sense for ISA cards */
  301                 switch (config_data.dma_chan) {
  302                 case DMA_CHAN_5:
  303                         info->drq = 5;
  304                         break;
  305                 case DMA_CHAN_6:
  306                         info->drq = 6;
  307                         break;
  308                 case DMA_CHAN_7:
  309                         info->drq = 7;
  310                         break;
  311                 default:
  312                         printf("bt_port_probe: Invalid DMA setting "
  313                                "detected for adapter.\n");
  314                         return (1);
  315                 }
  316         } else {
  317                 /* VL/EISA/PCI DMA */
  318                 info->drq = -1;
  319         }
  320         switch (config_data.irq) {
  321         case IRQ_9:
  322         case IRQ_10:
  323         case IRQ_11:
  324         case IRQ_12:
  325         case IRQ_14:
  326         case IRQ_15:
  327                 info->irq = ffs(config_data.irq) + 8;
  328                 break;
  329         default:
  330                 printf("bt_port_probe: Invalid IRQ setting %x"
  331                        "detected for adapter.\n", config_data.irq);
  332                 return (1);
  333         }
  334         return (0);
  335 }
  336 
  337 /*
  338  * Probe the adapter and verify that the card is a BusLogic.
  339  */
  340 int
  341 bt_probe(device_t dev)
  342 {
  343         struct bt_softc *bt = device_get_softc(dev);
  344         esetup_info_data_t esetup_info;
  345         u_int    status;
  346         u_int    intstat;
  347         u_int    geometry;
  348         int      error;
  349         u_int8_t param;
  350 
  351         /*
  352          * See if the three I/O ports look reasonable.
  353          * Touch the minimal number of registers in the
  354          * failure case.
  355          */
  356         status = bt_inb(bt, STATUS_REG);
  357         if ((status == 0)
  358          || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
  359                        STATUS_REG_RSVD|CMD_INVALID)) != 0) {
  360                 if (bootverbose)
  361                         device_printf(dev, "Failed Status Reg Test - %x\n",
  362                                status);
  363                 return (ENXIO);
  364         }
  365 
  366         intstat = bt_inb(bt, INTSTAT_REG);
  367         if ((intstat & INTSTAT_REG_RSVD) != 0) {
  368                 device_printf(dev, "Failed Intstat Reg Test\n");
  369                 return (ENXIO);
  370         }
  371 
  372         geometry = bt_inb(bt, GEOMETRY_REG);
  373         if (geometry == 0xFF) {
  374                 if (bootverbose)
  375                         device_printf(dev, "Failed Geometry Reg Test\n");
  376                 return (ENXIO);
  377         }
  378 
  379         /*
  380          * Looking good so far.  Final test is to reset the
  381          * adapter and attempt to fetch the extended setup
  382          * information.  This should filter out all 1542 cards.
  383          */
  384         if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
  385                 if (bootverbose)
  386                         device_printf(dev, "Failed Reset\n");
  387                 return (ENXIO);
  388         }
  389         
  390         param = sizeof(esetup_info);
  391         error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &param, /*parmlen*/1,
  392                        (u_int8_t*)&esetup_info, sizeof(esetup_info),
  393                        DEFAULT_CMD_TIMEOUT);
  394         if (error != 0) {
  395                 return (ENXIO);
  396         }
  397 
  398         return (0);
  399 }
  400 
  401 /*
  402  * Pull the boards setup information and record it in our softc.
  403  */
  404 int
  405 bt_fetch_adapter_info(device_t dev)
  406 {
  407         struct bt_softc *bt = device_get_softc(dev);
  408         board_id_data_t board_id;
  409         esetup_info_data_t esetup_info;
  410         config_data_t config_data;
  411         int      error;
  412         u_int8_t length_param;
  413 
  414         /* First record the firmware version */
  415         error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
  416                        (u_int8_t*)&board_id, sizeof(board_id),
  417                        DEFAULT_CMD_TIMEOUT);
  418         if (error != 0) {
  419                 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
  420                 return (error);
  421         }
  422         bt->firmware_ver[0] = board_id.firmware_rev_major;
  423         bt->firmware_ver[1] = '.';
  424         bt->firmware_ver[2] = board_id.firmware_rev_minor;
  425         bt->firmware_ver[3] = '\0';
  426                 
  427         /*
  428          * Depending on the firmware major and minor version,
  429          * we may be able to fetch additional minor version info.
  430          */
  431         if (bt->firmware_ver[0] > '') {
  432                 
  433                 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
  434                                (u_int8_t*)&bt->firmware_ver[3], 1,
  435                                DEFAULT_CMD_TIMEOUT);
  436                 if (error != 0) {
  437                         device_printf(dev,
  438                                       "bt_fetch_adapter_info - Failed Get "
  439                                       "Firmware 3rd Digit\n");
  440                         return (error);
  441                 }
  442                 if (bt->firmware_ver[3] == ' ')
  443                         bt->firmware_ver[3] = '\0';
  444                 bt->firmware_ver[4] = '\0';
  445         }
  446 
  447         if (strcmp(bt->firmware_ver, "3.3") >= 0) {
  448 
  449                 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
  450                                (u_int8_t*)&bt->firmware_ver[4], 1,
  451                                DEFAULT_CMD_TIMEOUT);
  452                 if (error != 0) {
  453                         device_printf(dev,
  454                                       "bt_fetch_adapter_info - Failed Get "
  455                                       "Firmware 4th Digit\n");
  456                         return (error);
  457                 }
  458                 if (bt->firmware_ver[4] == ' ')
  459                         bt->firmware_ver[4] = '\0';
  460                 bt->firmware_ver[5] = '\0';
  461         }
  462 
  463         /*
  464          * Some boards do not handle the "recently documented"
  465          * Inquire Board Model Number command correctly or do not give
  466          * exact information.  Use the Firmware and Extended Setup
  467          * information in these cases to come up with the right answer.
  468          * The major firmware revision number indicates:
  469          *
  470          *      5.xx    BusLogic "W" Series Host Adapters:
  471          *              BT-948/958/958D
  472          *      4.xx    BusLogic "C" Series Host Adapters:
  473          *              BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
  474          *      3.xx    BusLogic "S" Series Host Adapters:
  475          *              BT-747S/747D/757S/757D/445S/545S/542D
  476          *              BT-542B/742A (revision H)
  477          *      2.xx    BusLogic "A" Series Host Adapters:
  478          *              BT-542B/742A (revision G and below)
  479          *      0.xx    AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
  480          */
  481         length_param = sizeof(esetup_info);
  482         error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
  483                        (u_int8_t*)&esetup_info, sizeof(esetup_info),
  484                        DEFAULT_CMD_TIMEOUT);
  485         if (error != 0) {
  486                 return (error);
  487         }
  488         
  489         bt->bios_addr = esetup_info.bios_addr << 12;
  490 
  491         bt->mailbox_addrlimit = BUS_SPACE_MAXADDR;
  492         if (esetup_info.bus_type == 'A'
  493          && bt->firmware_ver[0] == '2') {
  494                 snprintf(bt->model, sizeof(bt->model), "542B");
  495         } else if (esetup_info.bus_type == 'E'
  496                 && bt->firmware_ver[0] == '2') {
  497 
  498                 /*
  499                  * The 742A seems to object if its mailboxes are
  500                  * allocated above the 16MB mark.
  501                  */
  502                 bt->mailbox_addrlimit = BUS_SPACE_MAXADDR_24BIT;
  503                 snprintf(bt->model, sizeof(bt->model), "742A");
  504         } else if (esetup_info.bus_type == 'E'
  505                 && bt->firmware_ver[0] == '') {
  506                 /* AMI FastDisk EISA Series 441 0.x */
  507                 snprintf(bt->model, sizeof(bt->model), "747A");
  508         } else {
  509                 ha_model_data_t model_data;
  510                 int i;
  511 
  512                 length_param = sizeof(model_data);
  513                 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
  514                                (u_int8_t*)&model_data, sizeof(model_data),
  515                                DEFAULT_CMD_TIMEOUT);
  516                 if (error != 0) {
  517                         device_printf(dev,
  518                                       "bt_fetch_adapter_info - Failed Inquire "
  519                                       "Model Number\n");
  520                         return (error);
  521                 }
  522                 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
  523                         bt->model[i] = model_data.ascii_model[i];
  524                         if (bt->model[i] == ' ')
  525                                 break;
  526                 }
  527                 bt->model[i] = '\0';
  528         }
  529 
  530         bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
  531 
  532         /* SG element limits */
  533         bt->max_sg = esetup_info.max_sg;
  534 
  535         /* Set feature flags */
  536         bt->wide_bus = esetup_info.wide_bus;
  537         bt->diff_bus = esetup_info.diff_bus;
  538         bt->ultra_scsi = esetup_info.ultra_scsi;
  539 
  540         if ((bt->firmware_ver[0] == '5')
  541          || (bt->firmware_ver[0] == '4' && bt->wide_bus))
  542                 bt->extended_lun = TRUE;
  543 
  544         bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
  545 
  546         bt->extended_trans =
  547             ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
  548 
  549         /*
  550          * Determine max CCB count and whether tagged queuing is
  551          * available based on controller type. Tagged queuing
  552          * only works on 'W' series adapters, 'C' series adapters
  553          * with firmware of rev 4.42 and higher, and 'S' series
  554          * adapters with firmware of rev 3.35 and higher.  The
  555          * maximum CCB counts are as follows:
  556          *
  557          *      192     BT-948/958/958D
  558          *      100     BT-946C/956C/956CD/747C/757C/757CD/445C
  559          *      50      BT-545C/540CF
  560          *      30      BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
  561          */
  562         if (bt->firmware_ver[0] == '5') {
  563                 bt->max_ccbs = 192;
  564                 bt->tag_capable = TRUE;
  565         } else if (bt->firmware_ver[0] == '4') {
  566                 if (bt->model[0] == '5')
  567                         bt->max_ccbs = 50;
  568                 else
  569                         bt->max_ccbs = 100;
  570                 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
  571         } else {
  572                 bt->max_ccbs = 30;
  573                 if (bt->firmware_ver[0] == '3'
  574                  && (strcmp(bt->firmware_ver, "3.35") >= 0))
  575                         bt->tag_capable = TRUE;
  576                 else
  577                         bt->tag_capable = FALSE;
  578         }
  579 
  580         if (bt->tag_capable != FALSE)
  581                 bt->tags_permitted = ALL_TARGETS;
  582 
  583         /* Determine Sync/Wide/Disc settings */
  584         if (bt->firmware_ver[0] >= '4') {
  585                 auto_scsi_data_t auto_scsi_data;
  586                 fetch_lram_params_t fetch_lram_params;
  587                 int error;
  588                 
  589                 /*
  590                  * These settings are stored in the
  591                  * AutoSCSI data in LRAM of 'W' and 'C'
  592                  * adapters.
  593                  */
  594                 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
  595                 fetch_lram_params.response_len = sizeof(auto_scsi_data);
  596                 error = bt_cmd(bt, BOP_FETCH_LRAM,
  597                                (u_int8_t*)&fetch_lram_params,
  598                                sizeof(fetch_lram_params),
  599                                (u_int8_t*)&auto_scsi_data,
  600                                sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
  601 
  602                 if (error != 0) {
  603                         device_printf(dev,
  604                                       "bt_fetch_adapter_info - Failed "
  605                                       "Get Auto SCSI Info\n");
  606                         return (error);
  607                 }
  608 
  609                 bt->disc_permitted = auto_scsi_data.low_disc_permitted
  610                                    | (auto_scsi_data.high_disc_permitted << 8);
  611                 bt->sync_permitted = auto_scsi_data.low_sync_permitted
  612                                    | (auto_scsi_data.high_sync_permitted << 8);
  613                 bt->fast_permitted = auto_scsi_data.low_fast_permitted
  614                                    | (auto_scsi_data.high_fast_permitted << 8);
  615                 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
  616                                    | (auto_scsi_data.high_ultra_permitted << 8);
  617                 bt->wide_permitted = auto_scsi_data.low_wide_permitted
  618                                    | (auto_scsi_data.high_wide_permitted << 8);
  619 
  620                 if (bt->ultra_scsi == FALSE)
  621                         bt->ultra_permitted = 0;
  622 
  623                 if (bt->wide_bus == FALSE)
  624                         bt->wide_permitted = 0;
  625         } else {
  626                 /*
  627                  * 'S' and 'A' series have this information in the setup
  628                  * information structure.
  629                  */
  630                 setup_data_t    setup_info;
  631 
  632                 length_param = sizeof(setup_info);
  633                 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
  634                                /*paramlen*/1, (u_int8_t*)&setup_info,
  635                                sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
  636 
  637                 if (error != 0) {
  638                         device_printf(dev,
  639                                       "bt_fetch_adapter_info - Failed "
  640                                       "Get Setup Info\n");
  641                         return (error);
  642                 }
  643 
  644                 if (setup_info.initiate_sync != 0) {
  645                         bt->sync_permitted = ALL_TARGETS;
  646 
  647                         if (bt->model[0] == '7') {
  648                                 if (esetup_info.sync_neg10MB != 0)
  649                                         bt->fast_permitted = ALL_TARGETS;
  650                                 if (strcmp(bt->model, "757") == 0)
  651                                         bt->wide_permitted = ALL_TARGETS;
  652                         }
  653                 }
  654                 bt->disc_permitted = ALL_TARGETS;
  655         }
  656 
  657         /* We need as many mailboxes as we can have ccbs */
  658         bt->num_boxes = bt->max_ccbs;
  659 
  660         /* Determine our SCSI ID */
  661         
  662         error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
  663                        (u_int8_t*)&config_data, sizeof(config_data),
  664                        DEFAULT_CMD_TIMEOUT);
  665         if (error != 0) {
  666                 device_printf(dev,
  667                               "bt_fetch_adapter_info - Failed Get Config\n");
  668                 return (error);
  669         }
  670         bt->scsi_id = config_data.scsi_id;
  671 
  672         return (0);
  673 }
  674 
  675 /*
  676  * Start the board, ready for normal operation
  677  */
  678 int
  679 bt_init(device_t dev)
  680 {
  681         struct bt_softc *bt = device_get_softc(dev);
  682 
  683         /* Announce the Adapter */
  684         device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
  685 
  686         if (bt->ultra_scsi != 0)
  687                 printf("Ultra ");
  688 
  689         if (bt->wide_bus != 0)
  690                 printf("Wide ");
  691         else
  692                 printf("Narrow ");
  693 
  694         if (bt->diff_bus != 0)
  695                 printf("Diff ");
  696 
  697         printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
  698                bt->max_ccbs);
  699 
  700         /*
  701          * Create our DMA tags.  These tags define the kinds of device
  702          * accessible memory allocations and memory mappings we will 
  703          * need to perform during normal operation.
  704          *
  705          * Unless we need to further restrict the allocation, we rely
  706          * on the restrictions of the parent dmat, hence the common
  707          * use of MAXADDR and MAXSIZE.
  708          */
  709 
  710         /* DMA tag for mapping buffers into device visible space. */
  711         if (bus_dma_tag_create( /* parent       */ bt->parent_dmat,
  712                                 /* alignment    */ 1,
  713                                 /* boundary     */ 0,
  714                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  715                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  716                                 /* filter       */ NULL,
  717                                 /* filterarg    */ NULL,
  718                                 /* maxsize      */ MAXBSIZE,
  719                                 /* nsegments    */ BT_NSEG,
  720                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_32BIT,
  721                                 /* flags        */ BUS_DMA_ALLOCNOW,
  722                                 /* lockfunc     */ busdma_lock_mutex,
  723                                 /* lockarg      */ &Giant,
  724                                 &bt->buffer_dmat) != 0) {
  725                 goto error_exit;
  726         }
  727 
  728         bt->init_level++;
  729         /* DMA tag for our mailboxes */
  730         if (bus_dma_tag_create( /* parent       */ bt->parent_dmat,
  731                                 /* alignment    */ 1,
  732                                 /* boundary     */ 0,
  733                                 /* lowaddr      */ bt->mailbox_addrlimit,
  734                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  735                                 /* filter       */ NULL,
  736                                 /* filterarg    */ NULL,
  737                                 /* maxsize      */ bt->num_boxes *
  738                                                    (sizeof(bt_mbox_in_t) +
  739                                                     sizeof(bt_mbox_out_t)),
  740                                 /* nsegments    */ 1,
  741                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_32BIT,
  742                                 /* flags        */ 0,
  743                                 /* lockfunc     */ busdma_lock_mutex,
  744                                 /* lockarg      */ &Giant,
  745                                 &bt->mailbox_dmat) != 0) {
  746                 goto error_exit;
  747         }
  748 
  749         bt->init_level++;
  750 
  751         /* Allocation for our mailboxes */
  752         if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
  753                              BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
  754                 goto error_exit;
  755         }
  756 
  757         bt->init_level++;
  758 
  759         /* And permanently map them */
  760         bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
  761                         bt->out_boxes,
  762                         bt->num_boxes * (sizeof(bt_mbox_in_t)
  763                                        + sizeof(bt_mbox_out_t)),
  764                         btmapmboxes, bt, /*flags*/0);
  765 
  766         bt->init_level++;
  767 
  768         bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
  769 
  770         btinitmboxes(bt);
  771 
  772         /* DMA tag for our ccb structures */
  773         if (bus_dma_tag_create( /* parent       */ bt->parent_dmat,
  774                                 /* alignment    */ 1,
  775                                 /* boundary     */ 0,
  776                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  777                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  778                                 /* filter       */ NULL,
  779                                 /* filterarg    */ NULL,
  780                                 /* maxsize      */ bt->max_ccbs *
  781                                                    sizeof(struct bt_ccb),
  782                                 /* nsegments    */ 1,
  783                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_32BIT,
  784                                 /* flags        */ 0,
  785                                 /* lockfunc     */ busdma_lock_mutex,
  786                                 /* lockarg      */ &Giant,
  787                                 &bt->ccb_dmat) != 0) {
  788                 goto error_exit;
  789         }
  790 
  791         bt->init_level++;
  792 
  793         /* Allocation for our ccbs */
  794         if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
  795                              BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
  796                 goto error_exit;
  797         }
  798 
  799         bt->init_level++;
  800 
  801         /* And permanently map them */
  802         bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
  803                         bt->bt_ccb_array,
  804                         bt->max_ccbs * sizeof(struct bt_ccb),
  805                         btmapccbs, bt, /*flags*/0);
  806 
  807         bt->init_level++;
  808 
  809         /* DMA tag for our S/G structures.  We allocate in page sized chunks */
  810         if (bus_dma_tag_create( /* parent       */ bt->parent_dmat,
  811                                 /* alignment    */ 1,
  812                                 /* boundary     */ 0,
  813                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  814                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  815                                 /* filter       */ NULL,
  816                                 /* filterarg    */ NULL,
  817                                 /* maxsize      */ PAGE_SIZE,
  818                                 /* nsegments    */ 1,
  819                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_32BIT,
  820                                 /* flags        */ 0,
  821                                 /* lockfunc     */ busdma_lock_mutex,
  822                                 /* lockarg      */ &Giant,
  823                                 &bt->sg_dmat) != 0) {
  824                 goto error_exit;
  825         }
  826 
  827         bt->init_level++;
  828 
  829         /* Perform initial CCB allocation */
  830         bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
  831         btallocccbs(bt);
  832 
  833         if (bt->num_ccbs == 0) {
  834                 device_printf(dev,
  835                               "bt_init - Unable to allocate initial ccbs\n");
  836                 goto error_exit;
  837         }
  838 
  839         /*
  840          * Note that we are going and return (to probe)
  841          */
  842         return 0;
  843 
  844 error_exit:
  845 
  846         return (ENXIO);
  847 }
  848 
  849 int
  850 bt_attach(device_t dev)
  851 {
  852         struct bt_softc *bt = device_get_softc(dev);
  853         int tagged_dev_openings;
  854         struct cam_devq *devq;
  855         int error;
  856 
  857         /*
  858          * We reserve 1 ccb for error recovery, so don't
  859          * tell the XPT about it.
  860          */
  861         if (bt->tag_capable != 0)
  862                 tagged_dev_openings = bt->max_ccbs - 1;
  863         else
  864                 tagged_dev_openings = 0;
  865 
  866         /*
  867          * Create the device queue for our SIM.
  868          */
  869         devq = cam_simq_alloc(bt->max_ccbs - 1);
  870         if (devq == NULL)
  871                 return (ENOMEM);
  872 
  873         /*
  874          * Construct our SIM entry
  875          */
  876         bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
  877                                 2, tagged_dev_openings, devq);
  878         if (bt->sim == NULL) {
  879                 cam_simq_free(devq);
  880                 return (ENOMEM);
  881         }
  882         
  883         if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
  884                 cam_sim_free(bt->sim, /*free_devq*/TRUE);
  885                 return (ENXIO);
  886         }
  887         
  888         if (xpt_create_path(&bt->path, /*periph*/NULL,
  889                             cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
  890                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  891                 xpt_bus_deregister(cam_sim_path(bt->sim));
  892                 cam_sim_free(bt->sim, /*free_devq*/TRUE);
  893                 return (ENXIO);
  894         }
  895                 
  896         /*
  897          * Setup interrupt.
  898          */
  899         error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM|INTR_ENTROPY,
  900                                bt_intr, bt, &bt->ih);
  901         if (error) {
  902                 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
  903                 return (error);
  904         }
  905 
  906         return (0);
  907 }
  908 
  909 int
  910 bt_check_probed_iop(u_int ioport)
  911 {
  912         u_int i;
  913 
  914         for (i = 0; i < BT_NUM_ISAPORTS; i++) {
  915                 if (bt_isa_ports[i].addr == ioport) {
  916                         if (bt_isa_ports[i].probed != 0)
  917                                 return (1);
  918                         else {
  919                                 return (0);
  920                         }
  921                 }
  922         }
  923         return (1);
  924 }
  925 
  926 void
  927 bt_mark_probed_bio(isa_compat_io_t port)
  928 {
  929         if (port < BIO_DISABLED)
  930                 bt_mark_probed_iop(bt_board_ports[port]);
  931 }
  932 
  933 void
  934 bt_mark_probed_iop(u_int ioport)
  935 {
  936         u_int i;
  937 
  938         for (i = 0; i < BT_NUM_ISAPORTS; i++) {
  939                 if (ioport == bt_isa_ports[i].addr) {
  940                         bt_isa_ports[i].probed = 1;
  941                         break;
  942                 }
  943         }
  944 }
  945 
  946 void
  947 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
  948 {
  949         if (ioport > 0) {
  950                 int i;
  951 
  952                 for (i = 0;i < BT_NUM_ISAPORTS; i++)
  953                         if (ioport <= bt_isa_ports[i].addr)
  954                                 break;
  955                 if ((i >= BT_NUM_ISAPORTS)
  956                  || (ioport != bt_isa_ports[i].addr)) {
  957                         printf(
  958 "bt_find_probe_range: Invalid baseport of 0x%x specified.\n"
  959 "bt_find_probe_range: Nearest valid baseport is 0x%x.\n"
  960 "bt_find_probe_range: Failing probe.\n",
  961                                ioport,
  962                                (i < BT_NUM_ISAPORTS)
  963                                     ? bt_isa_ports[i].addr
  964                                     : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
  965                         *port_index = *max_port_index = -1;
  966                         return;
  967                 }
  968                 *port_index = *max_port_index = bt_isa_ports[i].bio;
  969         } else {
  970                 *port_index = 0;
  971                 *max_port_index = BT_NUM_ISAPORTS - 1;
  972         }
  973 }
  974 
  975 int
  976 bt_iop_from_bio(isa_compat_io_t bio_index)
  977 {
  978         if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
  979                 return (bt_board_ports[bio_index]);
  980         return (-1);
  981 }
  982 
  983 
  984 static void
  985 btallocccbs(struct bt_softc *bt)
  986 {
  987         struct bt_ccb *next_ccb;
  988         struct sg_map_node *sg_map;
  989         bus_addr_t physaddr;
  990         bt_sg_t *segs;
  991         int newcount;
  992         int i;
  993 
  994         if (bt->num_ccbs >= bt->max_ccbs)
  995                 /* Can't allocate any more */
  996                 return;
  997 
  998         next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
  999 
 1000         sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
 1001 
 1002         if (sg_map == NULL)
 1003                 goto error_exit;
 1004 
 1005         /* Allocate S/G space for the next batch of CCBS */
 1006         if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
 1007                              BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
 1008                 free(sg_map, M_DEVBUF);
 1009                 goto error_exit;
 1010         }
 1011 
 1012         SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
 1013 
 1014         bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
 1015                         PAGE_SIZE, btmapsgs, bt, /*flags*/0);
 1016         
 1017         segs = sg_map->sg_vaddr;
 1018         physaddr = sg_map->sg_physaddr;
 1019 
 1020         newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
 1021         for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
 1022                 int error;
 1023 
 1024                 next_ccb->sg_list = segs;
 1025                 next_ccb->sg_list_phys = physaddr;
 1026                 next_ccb->flags = BCCB_FREE;
 1027                 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
 1028                                           &next_ccb->dmamap);
 1029                 if (error != 0)
 1030                         break;
 1031                 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
 1032                 segs += BT_NSEG;
 1033                 physaddr += (BT_NSEG * sizeof(bt_sg_t));
 1034                 next_ccb++;
 1035                 bt->num_ccbs++;
 1036         }
 1037 
 1038         /* Reserve a CCB for error recovery */
 1039         if (bt->recovery_bccb == NULL) {
 1040                 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
 1041                 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
 1042         }
 1043 
 1044         if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
 1045                 return;
 1046 
 1047 error_exit:
 1048         device_printf(bt->dev, "Can't malloc BCCBs\n");
 1049 }
 1050 
 1051 static __inline void
 1052 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
 1053 {
 1054         int s;
 1055 
 1056         s = splcam();
 1057         if ((bccb->flags & BCCB_ACTIVE) != 0)
 1058                 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
 1059         if (bt->resource_shortage != 0
 1060          && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
 1061                 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1062                 bt->resource_shortage = FALSE;
 1063         }
 1064         bccb->flags = BCCB_FREE;
 1065         SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
 1066         bt->active_ccbs--;
 1067         splx(s);
 1068 }
 1069 
 1070 static __inline struct bt_ccb*
 1071 btgetccb(struct bt_softc *bt)
 1072 {
 1073         struct  bt_ccb* bccb;
 1074         int     s;
 1075 
 1076         s = splcam();
 1077         if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
 1078                 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
 1079                 bt->active_ccbs++;
 1080         } else {
 1081                 btallocccbs(bt);
 1082                 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
 1083                 if (bccb != NULL) {
 1084                         SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
 1085                         bt->active_ccbs++;
 1086                 }
 1087         }
 1088         splx(s);
 1089 
 1090         return (bccb);
 1091 }
 1092 
 1093 static void
 1094 btaction(struct cam_sim *sim, union ccb *ccb)
 1095 {
 1096         struct  bt_softc *bt;
 1097 
 1098         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
 1099         
 1100         bt = (struct bt_softc *)cam_sim_softc(sim);
 1101         
 1102         switch (ccb->ccb_h.func_code) {
 1103         /* Common cases first */
 1104         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
 1105         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
 1106         {
 1107                 struct  bt_ccb  *bccb;
 1108                 struct  bt_hccb *hccb;
 1109 
 1110                 /*
 1111                  * get a bccb to use.
 1112                  */
 1113                 if ((bccb = btgetccb(bt)) == NULL) {
 1114                         int s;
 1115 
 1116                         s = splcam();
 1117                         bt->resource_shortage = TRUE;
 1118                         splx(s);
 1119                         xpt_freeze_simq(bt->sim, /*count*/1);
 1120                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
 1121                         xpt_done(ccb);
 1122                         return;
 1123                 }
 1124                 
 1125                 hccb = &bccb->hccb;
 1126 
 1127                 /*
 1128                  * So we can find the BCCB when an abort is requested
 1129                  */
 1130                 bccb->ccb = ccb;
 1131                 ccb->ccb_h.ccb_bccb_ptr = bccb;
 1132                 ccb->ccb_h.ccb_bt_ptr = bt;
 1133 
 1134                 /*
 1135                  * Put all the arguments for the xfer in the bccb
 1136                  */
 1137                 hccb->target_id = ccb->ccb_h.target_id;
 1138                 hccb->target_lun = ccb->ccb_h.target_lun;
 1139                 hccb->btstat = 0;
 1140                 hccb->sdstat = 0;
 1141 
 1142                 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
 1143                         struct ccb_scsiio *csio;
 1144                         struct ccb_hdr *ccbh;
 1145 
 1146                         csio = &ccb->csio;
 1147                         ccbh = &csio->ccb_h;
 1148                         hccb->opcode = INITIATOR_CCB_WRESID;
 1149                         hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
 1150                         hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
 1151                         hccb->cmd_len = csio->cdb_len;
 1152                         if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
 1153                                 ccb->ccb_h.status = CAM_REQ_INVALID;
 1154                                 btfreeccb(bt, bccb);
 1155                                 xpt_done(ccb);
 1156                                 return;
 1157                         }
 1158                         hccb->sense_len = csio->sense_len;
 1159                         if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
 1160                          && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
 1161                                 hccb->tag_enable = TRUE;
 1162                                 hccb->tag_type = (ccb->csio.tag_action & 0x3);
 1163                         } else {
 1164                                 hccb->tag_enable = FALSE;
 1165                                 hccb->tag_type = 0;
 1166                         }
 1167                         if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
 1168                                 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
 1169                                         bcopy(csio->cdb_io.cdb_ptr,
 1170                                               hccb->scsi_cdb, hccb->cmd_len);
 1171                                 } else {
 1172                                         /* I guess I could map it in... */
 1173                                         ccbh->status = CAM_REQ_INVALID;
 1174                                         btfreeccb(bt, bccb);
 1175                                         xpt_done(ccb);
 1176                                         return;
 1177                                 }
 1178                         } else {
 1179                                 bcopy(csio->cdb_io.cdb_bytes,
 1180                                       hccb->scsi_cdb, hccb->cmd_len);
 1181                         }
 1182                         /* If need be, bounce our sense buffer */
 1183                         if (bt->sense_buffers != NULL) {
 1184                                 hccb->sense_addr = btsensepaddr(bt, bccb);
 1185                         } else {
 1186                                 hccb->sense_addr = vtophys(&csio->sense_data);
 1187                         }
 1188                         /*
 1189                          * If we have any data to send with this command,
 1190                          * map it into bus space.
 1191                          */
 1192                         /* Only use S/G if there is a transfer */
 1193                         if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
 1194                                 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
 1195                                         /*
 1196                                          * We've been given a pointer
 1197                                          * to a single buffer.
 1198                                          */
 1199                                         if ((ccbh->flags & CAM_DATA_PHYS)==0) {
 1200                                                 int s;
 1201                                                 int error;
 1202 
 1203                                                 s = splsoftvm();
 1204                                                 error = bus_dmamap_load(
 1205                                                     bt->buffer_dmat,
 1206                                                     bccb->dmamap,
 1207                                                     csio->data_ptr,
 1208                                                     csio->dxfer_len,
 1209                                                     btexecuteccb,
 1210                                                     bccb,
 1211                                                     /*flags*/0);
 1212                                                 if (error == EINPROGRESS) {
 1213                                                         /*
 1214                                                          * So as to maintain
 1215                                                          * ordering, freeze the
 1216                                                          * controller queue
 1217                                                          * until our mapping is
 1218                                                          * returned.
 1219                                                          */
 1220                                                         xpt_freeze_simq(bt->sim,
 1221                                                                         1);
 1222                                                         csio->ccb_h.status |=
 1223                                                             CAM_RELEASE_SIMQ;
 1224                                                 }
 1225                                                 splx(s);
 1226                                         } else {
 1227                                                 struct bus_dma_segment seg; 
 1228 
 1229                                                 /* Pointer to physical buffer */
 1230                                                 seg.ds_addr =
 1231                                                     (bus_addr_t)csio->data_ptr;
 1232                                                 seg.ds_len = csio->dxfer_len;
 1233                                                 btexecuteccb(bccb, &seg, 1, 0);
 1234                                         }
 1235                                 } else {
 1236                                         struct bus_dma_segment *segs;
 1237 
 1238                                         if ((ccbh->flags & CAM_DATA_PHYS) != 0)
 1239                                                 panic("btaction - Physical "
 1240                                                       "segment pointers "
 1241                                                       "unsupported");
 1242 
 1243                                         if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
 1244                                                 panic("btaction - Virtual "
 1245                                                       "segment addresses "
 1246                                                       "unsupported");
 1247 
 1248                                         /* Just use the segments provided */
 1249                                         segs = (struct bus_dma_segment *)
 1250                                             csio->data_ptr;
 1251                                         btexecuteccb(bccb, segs,
 1252                                                      csio->sglist_cnt, 0);
 1253                                 }
 1254                         } else {
 1255                                 btexecuteccb(bccb, NULL, 0, 0);
 1256                         }
 1257                 } else {
 1258                         hccb->opcode = INITIATOR_BUS_DEV_RESET;
 1259                         /* No data transfer */
 1260                         hccb->datain = TRUE;
 1261                         hccb->dataout = TRUE;
 1262                         hccb->cmd_len = 0;
 1263                         hccb->sense_len = 0;
 1264                         hccb->tag_enable = FALSE;
 1265                         hccb->tag_type = 0;
 1266                         btexecuteccb(bccb, NULL, 0, 0);
 1267                 }
 1268                 break;
 1269         }
 1270         case XPT_EN_LUN:                /* Enable LUN as a target */
 1271         case XPT_TARGET_IO:             /* Execute target I/O request */
 1272         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
 1273         case XPT_CONT_TARGET_IO:        /* Continue Host Target I/O Connection*/
 1274         case XPT_ABORT:                 /* Abort the specified CCB */
 1275                 /* XXX Implement */
 1276                 ccb->ccb_h.status = CAM_REQ_INVALID;
 1277                 xpt_done(ccb);
 1278                 break;
 1279         case XPT_SET_TRAN_SETTINGS:
 1280         {
 1281                 /* XXX Implement */
 1282                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
 1283                 xpt_done(ccb);
 1284                 break;
 1285         }
 1286         case XPT_GET_TRAN_SETTINGS:
 1287         /* Get default/user set transfer settings for the target */
 1288         {
 1289                 struct  ccb_trans_settings *cts;
 1290                 u_int   target_mask;
 1291 
 1292                 cts = &ccb->cts;
 1293                 target_mask = 0x01 << ccb->ccb_h.target_id;
 1294 #ifdef  CAM_NEW_TRAN_CODE
 1295                 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
 1296                         struct ccb_trans_settings_scsi *scsi =
 1297                             &cts->proto_specific.scsi;
 1298                         struct ccb_trans_settings_spi *spi =
 1299                             &cts->xport_specific.spi;
 1300                         cts->protocol = PROTO_SCSI;
 1301                         cts->protocol_version = SCSI_REV_2;
 1302                         cts->transport = XPORT_SPI;
 1303                         cts->transport_version = 2;
 1304 
 1305                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
 1306                         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
 1307 
 1308                         if ((bt->disc_permitted & target_mask) != 0)
 1309                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
 1310                         if ((bt->tags_permitted & target_mask) != 0)
 1311                                 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
 1312 
 1313                         if ((bt->ultra_permitted & target_mask) != 0)
 1314                                 spi->sync_period = 12;
 1315                         else if ((bt->fast_permitted & target_mask) != 0)
 1316                                 spi->sync_period = 25;
 1317                         else if ((bt->sync_permitted & target_mask) != 0)
 1318                                 spi->sync_period = 50;
 1319                         else
 1320                                 spi->sync_period = 0;
 1321 
 1322                         if (spi->sync_period != 0)
 1323                                 spi->sync_offset = 15;
 1324 
 1325                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
 1326                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
 1327 
 1328                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
 1329                         if ((bt->wide_permitted & target_mask) != 0)
 1330                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
 1331                         else
 1332                                 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
 1333 
 1334                         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
 1335                                 scsi->valid = CTS_SCSI_VALID_TQ;
 1336                                 spi->valid |= CTS_SPI_VALID_DISC;
 1337                         } else
 1338                                 scsi->valid = 0;
 1339                 } else {
 1340 #else
 1341                 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
 1342                         cts->flags = 0;
 1343                         if ((bt->disc_permitted & target_mask) != 0)
 1344                                 cts->flags |= CCB_TRANS_DISC_ENB;
 1345                         if ((bt->tags_permitted & target_mask) != 0)
 1346                                 cts->flags |= CCB_TRANS_TAG_ENB;
 1347                         if ((bt->wide_permitted & target_mask) != 0)
 1348                                 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
 1349                         else
 1350                                 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
 1351                         if ((bt->ultra_permitted & target_mask) != 0)
 1352                                 cts->sync_period = 12;
 1353                         else if ((bt->fast_permitted & target_mask) != 0)
 1354                                 cts->sync_period = 25;
 1355                         else if ((bt->sync_permitted & target_mask) != 0)
 1356                                 cts->sync_period = 50;
 1357                         else
 1358                                 cts->sync_period = 0;
 1359 
 1360                         if (cts->sync_period != 0)
 1361                                 cts->sync_offset = 15;
 1362 
 1363                         cts->valid = CCB_TRANS_SYNC_RATE_VALID
 1364                                    | CCB_TRANS_SYNC_OFFSET_VALID
 1365                                    | CCB_TRANS_BUS_WIDTH_VALID
 1366                                    | CCB_TRANS_DISC_VALID
 1367                                    | CCB_TRANS_TQ_VALID;
 1368                 } else {
 1369 #endif
 1370                         btfetchtransinfo(bt, cts);
 1371                 }
 1372 
 1373                 ccb->ccb_h.status = CAM_REQ_CMP;
 1374                 xpt_done(ccb);
 1375                 break;
 1376         }
 1377         case XPT_CALC_GEOMETRY:
 1378         {
 1379                 struct    ccb_calc_geometry *ccg;
 1380                 u_int32_t size_mb;
 1381                 u_int32_t secs_per_cylinder;
 1382 
 1383                 ccg = &ccb->ccg;
 1384                 size_mb = ccg->volume_size
 1385                         / ((1024L * 1024L) / ccg->block_size);
 1386                 
 1387                 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
 1388                         if (size_mb >= 2048) {
 1389                                 ccg->heads = 255;
 1390                                 ccg->secs_per_track = 63;
 1391                         } else {
 1392                                 ccg->heads = 128;
 1393                                 ccg->secs_per_track = 32;
 1394                         }
 1395                 } else {
 1396                         ccg->heads = 64;
 1397                         ccg->secs_per_track = 32;
 1398                 }
 1399                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
 1400                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
 1401                 ccb->ccb_h.status = CAM_REQ_CMP;
 1402                 xpt_done(ccb);
 1403                 break;
 1404         }
 1405         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
 1406         {
 1407                 btreset(bt, /*hardreset*/TRUE);
 1408                 ccb->ccb_h.status = CAM_REQ_CMP;
 1409                 xpt_done(ccb);
 1410                 break;
 1411         }
 1412         case XPT_TERM_IO:               /* Terminate the I/O process */
 1413                 /* XXX Implement */
 1414                 ccb->ccb_h.status = CAM_REQ_INVALID;
 1415                 xpt_done(ccb);
 1416                 break;
 1417         case XPT_PATH_INQ:              /* Path routing inquiry */
 1418         {
 1419                 struct ccb_pathinq *cpi = &ccb->cpi;
 1420                 
 1421                 cpi->version_num = 1; /* XXX??? */
 1422                 cpi->hba_inquiry = PI_SDTR_ABLE;
 1423                 if (bt->tag_capable != 0)
 1424                         cpi->hba_inquiry |= PI_TAG_ABLE;
 1425                 if (bt->wide_bus != 0)
 1426                         cpi->hba_inquiry |= PI_WIDE_16;
 1427                 cpi->target_sprt = 0;
 1428                 cpi->hba_misc = 0;
 1429                 cpi->hba_eng_cnt = 0;
 1430                 cpi->max_target = bt->wide_bus ? 15 : 7;
 1431                 cpi->max_lun = 7;
 1432                 cpi->initiator_id = bt->scsi_id;
 1433                 cpi->bus_id = cam_sim_bus(sim);
 1434                 cpi->base_transfer_speed = 3300;
 1435                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
 1436                 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
 1437                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
 1438                 cpi->unit_number = cam_sim_unit(sim);
 1439                 cpi->ccb_h.status = CAM_REQ_CMP;
 1440 #ifdef  CAM_NEW_TRAN_CODE
 1441                 cpi->transport = XPORT_SPI;
 1442                 cpi->transport_version = 2;
 1443                 cpi->protocol = PROTO_SCSI;
 1444                 cpi->protocol_version = SCSI_REV_2;
 1445 #endif
 1446                 xpt_done(ccb);
 1447                 break;
 1448         }
 1449         default:
 1450                 ccb->ccb_h.status = CAM_REQ_INVALID;
 1451                 xpt_done(ccb);
 1452                 break;
 1453         }
 1454 }
 1455 
 1456 static void
 1457 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
 1458 {
 1459         struct   bt_ccb *bccb;
 1460         union    ccb *ccb;
 1461         struct   bt_softc *bt;
 1462         int      s;
 1463 
 1464         bccb = (struct bt_ccb *)arg;
 1465         ccb = bccb->ccb;
 1466         bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
 1467 
 1468         if (error != 0) {
 1469                 if (error != EFBIG)
 1470                         device_printf(bt->dev,
 1471                                       "Unexepected error 0x%x returned from "
 1472                                       "bus_dmamap_load\n", error);
 1473                 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
 1474                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
 1475                         ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
 1476                 }
 1477                 btfreeccb(bt, bccb);
 1478                 xpt_done(ccb);
 1479                 return;
 1480         }
 1481                 
 1482         if (nseg != 0) {
 1483                 bt_sg_t *sg;
 1484                 bus_dma_segment_t *end_seg;
 1485                 bus_dmasync_op_t op;
 1486 
 1487                 end_seg = dm_segs + nseg;
 1488 
 1489                 /* Copy the segments into our SG list */
 1490                 sg = bccb->sg_list;
 1491                 while (dm_segs < end_seg) {
 1492                         sg->len = dm_segs->ds_len;
 1493                         sg->addr = dm_segs->ds_addr;
 1494                         sg++;
 1495                         dm_segs++;
 1496                 }
 1497 
 1498                 if (nseg > 1) {
 1499                         bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
 1500                         bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
 1501                         bccb->hccb.data_addr = bccb->sg_list_phys;
 1502                 } else {
 1503                         bccb->hccb.data_len = bccb->sg_list->len;
 1504                         bccb->hccb.data_addr = bccb->sg_list->addr;
 1505                 }
 1506 
 1507                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
 1508                         op = BUS_DMASYNC_PREREAD;
 1509                 else
 1510                         op = BUS_DMASYNC_PREWRITE;
 1511 
 1512                 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
 1513 
 1514         } else {
 1515                 bccb->hccb.opcode = INITIATOR_CCB;
 1516                 bccb->hccb.data_len = 0;
 1517                 bccb->hccb.data_addr = 0;
 1518         }
 1519 
 1520         s = splcam();
 1521 
 1522         /*
 1523          * Last time we need to check if this CCB needs to
 1524          * be aborted.
 1525          */
 1526         if (ccb->ccb_h.status != CAM_REQ_INPROG) {
 1527                 if (nseg != 0)
 1528                         bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
 1529                 btfreeccb(bt, bccb);
 1530                 xpt_done(ccb);
 1531                 splx(s);
 1532                 return;
 1533         }
 1534                 
 1535         bccb->flags = BCCB_ACTIVE;
 1536         ccb->ccb_h.status |= CAM_SIM_QUEUED;
 1537         LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
 1538 
 1539         ccb->ccb_h.timeout_ch =
 1540             timeout(bttimeout, (caddr_t)bccb,
 1541                     (ccb->ccb_h.timeout * hz) / 1000);
 1542 
 1543         /* Tell the adapter about this command */
 1544         bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
 1545         if (bt->cur_outbox->action_code != BMBO_FREE) {
 1546                 /*
 1547                  * We should never encounter a busy mailbox.
 1548                  * If we do, warn the user, and treat it as
 1549                  * a resource shortage.  If the controller is
 1550                  * hung, one of the pending transactions will
 1551                  * timeout causing us to start recovery operations.
 1552                  */
 1553                 device_printf(bt->dev,
 1554                               "Encountered busy mailbox with %d out of %d "
 1555                               "commands active!!!\n", bt->active_ccbs,
 1556                               bt->max_ccbs);
 1557                 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
 1558                 if (nseg != 0)
 1559                         bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
 1560                 btfreeccb(bt, bccb);
 1561                 bt->resource_shortage = TRUE;
 1562                 xpt_freeze_simq(bt->sim, /*count*/1);
 1563                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
 1564                 xpt_done(ccb);
 1565                 return;
 1566         }
 1567         bt->cur_outbox->action_code = BMBO_START;       
 1568         bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
 1569         btnextoutbox(bt);
 1570         splx(s);
 1571 }
 1572 
 1573 void
 1574 bt_intr(void *arg)
 1575 {
 1576         struct  bt_softc *bt;
 1577         u_int   intstat;
 1578 
 1579         bt = (struct bt_softc *)arg;
 1580         while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
 1581 
 1582                 if ((intstat & CMD_COMPLETE) != 0) {
 1583                         bt->latched_status = bt_inb(bt, STATUS_REG);
 1584                         bt->command_cmp = TRUE;
 1585                 }
 1586 
 1587                 bt_outb(bt, CONTROL_REG, RESET_INTR);
 1588 
 1589                 if ((intstat & IMB_LOADED) != 0) {
 1590                         while (bt->cur_inbox->comp_code != BMBI_FREE) {
 1591                                 btdone(bt,
 1592                                        btccbptov(bt, bt->cur_inbox->ccb_addr),
 1593                                        bt->cur_inbox->comp_code);
 1594                                 bt->cur_inbox->comp_code = BMBI_FREE;
 1595                                 btnextinbox(bt);
 1596                         }
 1597                 }
 1598 
 1599                 if ((intstat & SCSI_BUS_RESET) != 0) {
 1600                         btreset(bt, /*hardreset*/FALSE);
 1601                 }
 1602         }
 1603 }
 1604 
 1605 static void
 1606 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
 1607 {
 1608         union  ccb        *ccb;
 1609         struct ccb_scsiio *csio;
 1610 
 1611         ccb = bccb->ccb;
 1612         csio = &bccb->ccb->csio;
 1613 
 1614         if ((bccb->flags & BCCB_ACTIVE) == 0) {
 1615                 device_printf(bt->dev,
 1616                               "btdone - Attempt to free non-active BCCB %p\n",
 1617                               (void *)bccb);
 1618                 return;
 1619         }
 1620 
 1621         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
 1622                 bus_dmasync_op_t op;
 1623 
 1624                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
 1625                         op = BUS_DMASYNC_POSTREAD;
 1626                 else
 1627                         op = BUS_DMASYNC_POSTWRITE;
 1628                 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
 1629                 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
 1630         }
 1631 
 1632         if (bccb == bt->recovery_bccb) {
 1633                 /*
 1634                  * The recovery BCCB does not have a CCB associated
 1635                  * with it, so short circuit the normal error handling.
 1636                  * We now traverse our list of pending CCBs and process
 1637                  * any that were terminated by the recovery CCBs action.
 1638                  * We also reinstate timeouts for all remaining, pending,
 1639                  * CCBs.
 1640                  */
 1641                 struct cam_path *path;
 1642                 struct ccb_hdr *ccb_h;
 1643                 cam_status error;
 1644 
 1645                 /* Notify all clients that a BDR occured */
 1646                 error = xpt_create_path(&path, /*periph*/NULL,
 1647                                         cam_sim_path(bt->sim),
 1648                                         bccb->hccb.target_id,
 1649                                         CAM_LUN_WILDCARD);
 1650                 
 1651                 if (error == CAM_REQ_CMP)
 1652                         xpt_async(AC_SENT_BDR, path, NULL);
 1653 
 1654                 ccb_h = LIST_FIRST(&bt->pending_ccbs);
 1655                 while (ccb_h != NULL) {
 1656                         struct bt_ccb *pending_bccb;
 1657 
 1658                         pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
 1659                         if (pending_bccb->hccb.target_id
 1660                          == bccb->hccb.target_id) {
 1661                                 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
 1662                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 1663                                 btdone(bt, pending_bccb, BMBI_ERROR);
 1664                         } else {
 1665                                 ccb_h->timeout_ch =
 1666                                     timeout(bttimeout, (caddr_t)pending_bccb,
 1667                                             (ccb_h->timeout * hz) / 1000);
 1668                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 1669                         }
 1670                 }
 1671                 device_printf(bt->dev, "No longer in timeout\n");
 1672                 return;
 1673         }
 1674 
 1675         untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
 1676 
 1677         switch (comp_code) {
 1678         case BMBI_FREE:
 1679                 device_printf(bt->dev,
 1680                               "btdone - CCB completed with free status!\n");
 1681                 break;
 1682         case BMBI_NOT_FOUND:
 1683                 device_printf(bt->dev,
 1684                               "btdone - CCB Abort failed to find CCB\n");
 1685                 break;
 1686         case BMBI_ABORT:
 1687         case BMBI_ERROR:
 1688                 if (bootverbose) {
 1689                         printf("bt: ccb %p - error %x occured.  "
 1690                                "btstat = %x, sdstat = %x\n",
 1691                                (void *)bccb, comp_code, bccb->hccb.btstat,
 1692                                bccb->hccb.sdstat);
 1693                 }
 1694                 /* An error occured */
 1695                 switch(bccb->hccb.btstat) {
 1696                 case BTSTAT_DATARUN_ERROR:
 1697                         if (bccb->hccb.data_len == 0) {
 1698                                 /*
 1699                                  * At least firmware 4.22, does this
 1700                                  * for a QUEUE FULL condition.
 1701                                  */
 1702                                 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
 1703                         } else if (bccb->hccb.data_len < 0) {
 1704                                 csio->ccb_h.status = CAM_DATA_RUN_ERR;
 1705                                 break;
 1706                         }
 1707                         /* FALLTHROUGH */
 1708                 case BTSTAT_NOERROR:
 1709                 case BTSTAT_LINKED_CMD_COMPLETE:
 1710                 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
 1711                 case BTSTAT_DATAUNDERUN_ERROR:
 1712 
 1713                         csio->scsi_status = bccb->hccb.sdstat;
 1714                         csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
 1715                         switch(csio->scsi_status) {
 1716                         case SCSI_STATUS_CHECK_COND:
 1717                         case SCSI_STATUS_CMD_TERMINATED:
 1718                                 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
 1719                                 /* Bounce sense back if necessary */
 1720                                 if (bt->sense_buffers != NULL) {
 1721                                         csio->sense_data =
 1722                                             *btsensevaddr(bt, bccb);
 1723                                 }
 1724                                 break;
 1725                         default:
 1726                                 break;
 1727                         case SCSI_STATUS_OK:
 1728                                 csio->ccb_h.status = CAM_REQ_CMP;
 1729                                 break;
 1730                         }
 1731                         csio->resid = bccb->hccb.data_len;
 1732                         break;
 1733                 case BTSTAT_SELTIMEOUT:
 1734                         csio->ccb_h.status = CAM_SEL_TIMEOUT;
 1735                         break;
 1736                 case BTSTAT_UNEXPECTED_BUSFREE:
 1737                         csio->ccb_h.status = CAM_UNEXP_BUSFREE;
 1738                         break;
 1739                 case BTSTAT_INVALID_PHASE:
 1740                         csio->ccb_h.status = CAM_SEQUENCE_FAIL;
 1741                         break;
 1742                 case BTSTAT_INVALID_ACTION_CODE:
 1743                         panic("%s: Inavlid Action code", bt_name(bt));
 1744                         break;
 1745                 case BTSTAT_INVALID_OPCODE:
 1746                         panic("%s: Inavlid CCB Opcode code", bt_name(bt));
 1747                         break;
 1748                 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
 1749                         /* We don't even support linked commands... */
 1750                         panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
 1751                         break;
 1752                 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
 1753                         panic("%s: Invalid CCB or SG list", bt_name(bt));
 1754                         break;
 1755                 case BTSTAT_AUTOSENSE_FAILED:
 1756                         csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
 1757                         break;
 1758                 case BTSTAT_TAGGED_MSG_REJECTED:
 1759                 {
 1760                         struct ccb_trans_settings neg; 
 1761 #ifdef  CAM_NEW_TRAN_CODE
 1762                         struct ccb_trans_settings_scsi *scsi =
 1763                             &neg.proto_specific.scsi;
 1764 
 1765                         neg.protocol = PROTO_SCSI;
 1766                         neg.protocol_version = SCSI_REV_2;
 1767                         neg.transport = XPORT_SPI;
 1768                         neg.transport_version = 2;
 1769                         scsi->valid = CTS_SCSI_VALID_TQ;
 1770                         scsi->flags = 0;
 1771 #else
 1772 
 1773                         neg.flags = 0;
 1774                         neg.valid = CCB_TRANS_TQ_VALID;
 1775 #endif
 1776                         xpt_print_path(csio->ccb_h.path);
 1777                         printf("refuses tagged commands.  Performing "
 1778                                "non-tagged I/O\n");
 1779                         xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
 1780                                       /*priority*/1); 
 1781                         xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
 1782                         bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
 1783                         csio->ccb_h.status = CAM_MSG_REJECT_REC;
 1784                         break;
 1785                 }
 1786                 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
 1787                         /*
 1788                          * XXX You would think that this is
 1789                          *     a recoverable error... Hmmm.
 1790                          */
 1791                         csio->ccb_h.status = CAM_REQ_CMP_ERR;
 1792                         break;
 1793                 case BTSTAT_HA_SOFTWARE_ERROR:
 1794                 case BTSTAT_HA_WATCHDOG_ERROR:
 1795                 case BTSTAT_HARDWARE_FAILURE:
 1796                         /* Hardware reset ??? Can we recover ??? */
 1797                         csio->ccb_h.status = CAM_NO_HBA;
 1798                         break;
 1799                 case BTSTAT_TARGET_IGNORED_ATN:
 1800                 case BTSTAT_OTHER_SCSI_BUS_RESET:
 1801                 case BTSTAT_HA_SCSI_BUS_RESET:
 1802                         if ((csio->ccb_h.status & CAM_STATUS_MASK)
 1803                          != CAM_CMD_TIMEOUT)
 1804                                 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
 1805                         break;
 1806                 case BTSTAT_HA_BDR:
 1807                         if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
 1808                                 csio->ccb_h.status = CAM_BDR_SENT;
 1809                         else
 1810                                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
 1811                         break;
 1812                 case BTSTAT_INVALID_RECONNECT:
 1813                 case BTSTAT_ABORT_QUEUE_GENERATED:
 1814                         csio->ccb_h.status = CAM_REQ_TERMIO;
 1815                         break;
 1816                 case BTSTAT_SCSI_PERROR_DETECTED:
 1817                         csio->ccb_h.status = CAM_UNCOR_PARITY;
 1818                         break;
 1819                 }
 1820                 if (csio->ccb_h.status != CAM_REQ_CMP) {
 1821                         xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
 1822                         csio->ccb_h.status |= CAM_DEV_QFRZN;
 1823                 }
 1824                 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
 1825                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1826                 btfreeccb(bt, bccb);
 1827                 xpt_done(ccb);
 1828                 break;
 1829         case BMBI_OK:
 1830                 /* All completed without incident */
 1831                 ccb->ccb_h.status |= CAM_REQ_CMP;
 1832                 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
 1833                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1834                 btfreeccb(bt, bccb);
 1835                 xpt_done(ccb);
 1836                 break;
 1837         }
 1838 }
 1839 
 1840 static int
 1841 btreset(struct bt_softc* bt, int hard_reset)
 1842 {
 1843         struct   ccb_hdr *ccb_h;
 1844         u_int    status;
 1845         u_int    timeout;
 1846         u_int8_t reset_type;
 1847 
 1848         if (hard_reset != 0)
 1849                 reset_type = HARD_RESET;
 1850         else
 1851                 reset_type = SOFT_RESET;
 1852         bt_outb(bt, CONTROL_REG, reset_type);
 1853 
 1854         /* Wait 5sec. for Diagnostic start */
 1855         timeout = 5 * 10000;
 1856         while (--timeout) {
 1857                 status = bt_inb(bt, STATUS_REG);
 1858                 if ((status & DIAG_ACTIVE) != 0)
 1859                         break;
 1860                 DELAY(100);
 1861         }
 1862         if (timeout == 0) {
 1863                 if (bootverbose)
 1864                         printf("%s: btreset - Diagnostic Active failed to "
 1865                                 "assert. status = 0x%x\n", bt_name(bt), status);
 1866                 return (ETIMEDOUT);
 1867         }
 1868 
 1869         /* Wait 10sec. for Diagnostic end */
 1870         timeout = 10 * 10000;
 1871         while (--timeout) {
 1872                 status = bt_inb(bt, STATUS_REG);
 1873                 if ((status & DIAG_ACTIVE) == 0)
 1874                         break;
 1875                 DELAY(100);
 1876         }
 1877         if (timeout == 0) {
 1878                 panic("%s: btreset - Diagnostic Active failed to drop. "
 1879                        "status = 0x%x\n", bt_name(bt), status);
 1880                 return (ETIMEDOUT);
 1881         }
 1882 
 1883         /* Wait for the host adapter to become ready or report a failure */
 1884         timeout = 10000;
 1885         while (--timeout) {
 1886                 status = bt_inb(bt, STATUS_REG);
 1887                 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
 1888                         break;
 1889                 DELAY(100);
 1890         }
 1891         if (timeout == 0) {
 1892                 printf("%s: btreset - Host adapter failed to come ready. "
 1893                        "status = 0x%x\n", bt_name(bt), status);
 1894                 return (ETIMEDOUT);
 1895         }
 1896 
 1897         /* If the diagnostics failed, tell the user */
 1898         if ((status & DIAG_FAIL) != 0
 1899          || (status & HA_READY) == 0) {
 1900                 printf("%s: btreset - Adapter failed diagnostics\n",
 1901                        bt_name(bt));
 1902 
 1903                 if ((status & DATAIN_REG_READY) != 0)
 1904                         printf("%s: btreset - Host Adapter Error code = 0x%x\n",
 1905                                bt_name(bt), bt_inb(bt, DATAIN_REG));
 1906                 return (ENXIO);
 1907         }
 1908 
 1909         /* If we've allocated mailboxes, initialize them */
 1910         if (bt->init_level > 4)
 1911                 btinitmboxes(bt);
 1912 
 1913         /* If we've attached to the XPT, tell it about the event */
 1914         if (bt->path != NULL)
 1915                 xpt_async(AC_BUS_RESET, bt->path, NULL);
 1916 
 1917         /*
 1918          * Perform completion processing for all outstanding CCBs.
 1919          */
 1920         while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
 1921                 struct bt_ccb *pending_bccb;
 1922 
 1923                 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
 1924                 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
 1925                 btdone(bt, pending_bccb, BMBI_ERROR);
 1926         }
 1927 
 1928         return (0);
 1929 }
 1930 
 1931 /*
 1932  * Send a command to the adapter.
 1933  */
 1934 int
 1935 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
 1936       u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
 1937 {
 1938         u_int   timeout;
 1939         u_int   status;
 1940         u_int   saved_status;
 1941         u_int   intstat;
 1942         u_int   reply_buf_size;
 1943         int     s;
 1944         int     cmd_complete;
 1945         int     error;
 1946 
 1947         /* No data returned to start */
 1948         reply_buf_size = reply_len;
 1949         reply_len = 0;
 1950         intstat = 0;
 1951         cmd_complete = 0;
 1952         saved_status = 0;
 1953         error = 0;
 1954 
 1955         bt->command_cmp = 0;
 1956         /*
 1957          * Wait up to 10 sec. for the adapter to become
 1958          * ready to accept commands.
 1959          */
 1960         timeout = 100000;
 1961         while (--timeout) {
 1962                 status = bt_inb(bt, STATUS_REG);
 1963                 if ((status & HA_READY) != 0
 1964                  && (status & CMD_REG_BUSY) == 0)
 1965                         break;
 1966                 /*
 1967                  * Throw away any pending data which may be
 1968                  * left over from earlier commands that we
 1969                  * timedout on.
 1970                  */
 1971                 if ((status & DATAIN_REG_READY) != 0)
 1972                         (void)bt_inb(bt, DATAIN_REG);
 1973                 DELAY(100);
 1974         }
 1975         if (timeout == 0) {
 1976                 printf("%s: bt_cmd: Timeout waiting for adapter ready, "
 1977                        "status = 0x%x\n", bt_name(bt), status);
 1978                 return (ETIMEDOUT);
 1979         }
 1980 
 1981         /*
 1982          * Send the opcode followed by any necessary parameter bytes.
 1983          */
 1984         bt_outb(bt, COMMAND_REG, opcode);
 1985 
 1986         /*
 1987          * Wait for up to 1sec for each byte of the the
 1988          * parameter list sent to be sent.
 1989          */
 1990         timeout = 10000;
 1991         while (param_len && --timeout) {
 1992                 DELAY(100);
 1993                 s = splcam();
 1994                 status = bt_inb(bt, STATUS_REG);
 1995                 intstat = bt_inb(bt, INTSTAT_REG);
 1996                 splx(s);
 1997         
 1998                 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
 1999                  == (INTR_PENDING|CMD_COMPLETE)) {
 2000                         saved_status = status;
 2001                         cmd_complete = 1;
 2002                         break;
 2003                 }
 2004                 if (bt->command_cmp != 0) {
 2005                         saved_status = bt->latched_status;
 2006                         cmd_complete = 1;
 2007                         break;
 2008                 }
 2009                 if ((status & DATAIN_REG_READY) != 0)
 2010                         break;
 2011                 if ((status & CMD_REG_BUSY) == 0) {
 2012                         bt_outb(bt, COMMAND_REG, *params++);
 2013                         param_len--;
 2014                         timeout = 10000;
 2015                 }
 2016         }
 2017         if (timeout == 0) {
 2018                 printf("%s: bt_cmd: Timeout sending parameters, "
 2019                        "status = 0x%x\n", bt_name(bt), status);
 2020                 cmd_complete = 1;
 2021                 saved_status = status;
 2022                 error = ETIMEDOUT;
 2023         }
 2024 
 2025         /*
 2026          * Wait for the command to complete.
 2027          */
 2028         while (cmd_complete == 0 && --cmd_timeout) {
 2029 
 2030                 s = splcam();
 2031                 status = bt_inb(bt, STATUS_REG);
 2032                 intstat = bt_inb(bt, INTSTAT_REG);
 2033                 /*
 2034                  * It may be that this command was issued with
 2035                  * controller interrupts disabled.  We'll never
 2036                  * get to our command if an incoming mailbox
 2037                  * interrupt is pending, so take care of completed
 2038                  * mailbox commands by calling our interrupt handler.
 2039                  */
 2040                 if ((intstat & (INTR_PENDING|IMB_LOADED))
 2041                  == (INTR_PENDING|IMB_LOADED))
 2042                         bt_intr(bt);
 2043                 splx(s);
 2044 
 2045                 if (bt->command_cmp != 0) {
 2046                         /*
 2047                          * Our interrupt handler saw CMD_COMPLETE
 2048                          * status before we did.
 2049                          */
 2050                         cmd_complete = 1;
 2051                         saved_status = bt->latched_status;
 2052                 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
 2053                         == (INTR_PENDING|CMD_COMPLETE)) {
 2054                         /*
 2055                          * Our poll (in case interrupts are blocked)
 2056                          * saw the CMD_COMPLETE interrupt.
 2057                          */
 2058                         cmd_complete = 1;
 2059                         saved_status = status;
 2060                 } else if (opcode == BOP_MODIFY_IO_ADDR
 2061                         && (status & CMD_REG_BUSY) == 0) {
 2062                         /*
 2063                          * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
 2064                          * but it should update the status register.  So, we
 2065                          * consider this command complete when the CMD_REG_BUSY
 2066                          * status clears.
 2067                          */
 2068                         saved_status = status;
 2069                         cmd_complete = 1;
 2070                 } else if ((status & DATAIN_REG_READY) != 0) {
 2071                         u_int8_t data;
 2072 
 2073                         data = bt_inb(bt, DATAIN_REG);
 2074                         if (reply_len < reply_buf_size) {
 2075                                 *reply_data++ = data;
 2076                         } else {
 2077                                 printf("%s: bt_cmd - Discarded reply data byte "
 2078                                        "for opcode 0x%x\n", bt_name(bt),
 2079                                        opcode);
 2080                         }
 2081                         /*
 2082                          * Reset timeout to ensure at least a second
 2083                          * between response bytes.
 2084                          */
 2085                         cmd_timeout = MAX(cmd_timeout, 10000);
 2086                         reply_len++;
 2087 
 2088                 } else if ((opcode == BOP_FETCH_LRAM)
 2089                         && (status & HA_READY) != 0) {
 2090                                 saved_status = status;
 2091                                 cmd_complete = 1;
 2092                 }
 2093                 DELAY(100);
 2094         }
 2095         if (cmd_timeout == 0) {
 2096                 printf("%s: bt_cmd: Timeout waiting for command (%x) "
 2097                        "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
 2098                        "rlen %d\n", bt_name(bt), opcode,
 2099                        bt_name(bt), status, intstat, reply_len);
 2100                 error = (ETIMEDOUT);
 2101         }
 2102 
 2103         /*
 2104          * Clear any pending interrupts.  Block interrupts so our
 2105          * interrupt handler is not re-entered.
 2106          */
 2107         s = splcam();
 2108         bt_intr(bt);
 2109         splx(s);
 2110         
 2111         if (error != 0)
 2112                 return (error);
 2113 
 2114         /*
 2115          * If the command was rejected by the controller, tell the caller.
 2116          */
 2117         if ((saved_status & CMD_INVALID) != 0) {
 2118                 /*
 2119                  * Some early adapters may not recover properly from
 2120                  * an invalid command.  If it appears that the controller
 2121                  * has wedged (i.e. status was not cleared by our interrupt
 2122                  * reset above), perform a soft reset.
 2123                  */
 2124                 if (bootverbose)
 2125                         printf("%s: Invalid Command 0x%x\n", bt_name(bt), 
 2126                                 opcode);
 2127                 DELAY(1000);
 2128                 status = bt_inb(bt, STATUS_REG);
 2129                 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
 2130                               CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
 2131                  || (status & (HA_READY|INIT_REQUIRED))
 2132                   != (HA_READY|INIT_REQUIRED)) {
 2133                         btreset(bt, /*hard_reset*/FALSE);
 2134                 }
 2135                 return (EINVAL);
 2136         }
 2137 
 2138         if (param_len > 0) {
 2139                 /* The controller did not accept the full argument list */
 2140                 return (E2BIG);
 2141         }
 2142 
 2143         if (reply_len != reply_buf_size) {
 2144                 /* Too much or too little data received */
 2145                 return (EMSGSIZE);
 2146         }
 2147 
 2148         /* We were successful */
 2149         return (0);
 2150 }
 2151 
 2152 static int
 2153 btinitmboxes(struct bt_softc *bt) {
 2154         init_32b_mbox_params_t init_mbox;
 2155         int error;
 2156 
 2157         bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
 2158         bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
 2159         bt->cur_inbox = bt->in_boxes;
 2160         bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
 2161         bt->cur_outbox = bt->out_boxes;
 2162         bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
 2163 
 2164         /* Tell the adapter about them */
 2165         init_mbox.num_boxes = bt->num_boxes;
 2166         init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
 2167         init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
 2168         init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
 2169         init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
 2170         error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
 2171                        /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
 2172                        /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
 2173 
 2174         if (error != 0)
 2175                 printf("btinitmboxes: Initialization command failed\n");
 2176         else if (bt->strict_rr != 0) {
 2177                 /*
 2178                  * If the controller supports
 2179                  * strict round robin mode,
 2180                  * enable it
 2181                  */
 2182                 u_int8_t param;
 2183 
 2184                 param = 0;
 2185                 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, &param, 1,
 2186                                /*reply_buf*/NULL, /*reply_len*/0,
 2187                                DEFAULT_CMD_TIMEOUT);
 2188 
 2189                 if (error != 0) {
 2190                         printf("btinitmboxes: Unable to enable strict RR\n");
 2191                         error = 0;
 2192                 } else if (bootverbose) {
 2193                         printf("%s: Using Strict Round Robin Mailbox Mode\n",
 2194                                bt_name(bt));
 2195                 }
 2196         }
 2197         
 2198         return (error);
 2199 }
 2200 
 2201 /*
 2202  * Update the XPT's idea of the negotiated transfer
 2203  * parameters for a particular target.
 2204  */
 2205 static void
 2206 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings *cts)
 2207 {
 2208         setup_data_t    setup_info;
 2209         u_int           target;
 2210         u_int           targ_offset;
 2211         u_int           targ_mask;
 2212         u_int           sync_period;
 2213         u_int           sync_offset;
 2214         u_int           bus_width;
 2215         int             error;
 2216         u_int8_t        param;
 2217         targ_syncinfo_t sync_info;
 2218 #ifdef  CAM_NEW_TRAN_CODE
 2219         struct ccb_trans_settings_scsi *scsi =
 2220             &cts->proto_specific.scsi;
 2221         struct ccb_trans_settings_spi *spi =
 2222             &cts->xport_specific.spi;
 2223 
 2224         spi->valid = 0;
 2225         scsi->valid = 0;
 2226 #else
 2227 
 2228         cts->valid = 0;
 2229 #endif
 2230 
 2231         target = cts->ccb_h.target_id;
 2232         targ_offset = (target & 0x7);
 2233         targ_mask = (0x01 << targ_offset);
 2234 
 2235         /*
 2236          * Inquire Setup Information.  This command retreives the
 2237          * Wide negotiation status for recent adapters as well as
 2238          * the sync info for older models.
 2239          */
 2240         param = sizeof(setup_info);
 2241         error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
 2242                        (u_int8_t*)&setup_info, sizeof(setup_info),
 2243                        DEFAULT_CMD_TIMEOUT);
 2244 
 2245         if (error != 0) {
 2246                 printf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
 2247                        bt_name(bt), error);
 2248                 return;
 2249         }
 2250 
 2251         sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
 2252                                  : setup_info.high_syncinfo[targ_offset];
 2253 
 2254         if (sync_info.sync == 0)
 2255                 sync_offset = 0;
 2256         else
 2257                 sync_offset = sync_info.offset;
 2258 
 2259 
 2260         bus_width = MSG_EXT_WDTR_BUS_8_BIT;
 2261         if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
 2262                 u_int wide_active;
 2263 
 2264                 wide_active =
 2265                     (target < 8) ? (setup_info.low_wide_active & targ_mask)
 2266                                  : (setup_info.high_wide_active & targ_mask);
 2267 
 2268                 if (wide_active)
 2269                         bus_width = MSG_EXT_WDTR_BUS_16_BIT;
 2270         } else if ((bt->wide_permitted & targ_mask) != 0) {
 2271                 struct ccb_getdev cgd;
 2272 
 2273                 /*
 2274                  * Prior to rev 5.06L, wide status isn't provided,
 2275                  * so we "guess" that wide transfers are in effect
 2276                  * if the user settings allow for wide and the inquiry
 2277                  * data for the device indicates that it can handle
 2278                  * wide transfers.
 2279                  */
 2280                 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
 2281                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
 2282                 xpt_action((union ccb *)&cgd);
 2283                 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
 2284                  && (cgd.inq_data.flags & SID_WBus16) != 0)
 2285                         bus_width = MSG_EXT_WDTR_BUS_16_BIT;
 2286         }
 2287 
 2288         if (bt->firmware_ver[0] >= '3') {
 2289                 /*
 2290                  * For adapters that can do fast or ultra speeds,
 2291                  * use the more exact Target Sync Information command.
 2292                  */
 2293                 target_sync_info_data_t sync_info;
 2294 
 2295                 param = sizeof(sync_info);
 2296                 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, &param, /*paramlen*/1,
 2297                                (u_int8_t*)&sync_info, sizeof(sync_info),
 2298                                DEFAULT_CMD_TIMEOUT);
 2299                 
 2300                 if (error != 0) {
 2301                         printf("%s: btfetchtransinfo - Inquire Sync "
 2302                                "Info Failed 0x%x\n", bt_name(bt), error);
 2303                         return;
 2304                 }
 2305                 sync_period = sync_info.sync_rate[target] * 100;
 2306         } else {
 2307                 sync_period = 2000 + (500 * sync_info.period);
 2308         }
 2309 
 2310 #ifdef  CAM_NEW_TRAN_CODE
 2311         cts->protocol = PROTO_SCSI;
 2312         cts->protocol_version = SCSI_REV_2;
 2313         cts->transport = XPORT_SPI;
 2314         cts->transport_version = 2;
 2315 
 2316         spi->sync_period = sync_period;
 2317         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
 2318         spi->sync_offset = sync_offset;
 2319         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
 2320 
 2321         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
 2322         spi->bus_width = bus_width;
 2323 
 2324         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
 2325                 scsi->valid = CTS_SCSI_VALID_TQ;
 2326                 spi->valid |= CTS_SPI_VALID_DISC;
 2327         } else
 2328                 scsi->valid = 0;
 2329         
 2330 #else
 2331         /* Convert ns value to standard SCSI sync rate */
 2332         if (cts->sync_offset != 0)
 2333                 cts->sync_period = scsi_calc_syncparam(sync_period);
 2334         else
 2335                 cts->sync_period = 0;
 2336         cts->sync_offset = sync_offset;
 2337         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
 2338         
 2339         cts->valid = CCB_TRANS_SYNC_RATE_VALID
 2340                    | CCB_TRANS_SYNC_OFFSET_VALID
 2341                    | CCB_TRANS_BUS_WIDTH_VALID;
 2342 
 2343 #endif
 2344         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
 2345 }
 2346 
 2347 static void
 2348 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 2349 {
 2350         struct bt_softc* bt;
 2351 
 2352         bt = (struct bt_softc*)arg;
 2353         bt->mailbox_physbase = segs->ds_addr;
 2354 }
 2355 
 2356 static void
 2357 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 2358 {
 2359         struct bt_softc* bt;
 2360 
 2361         bt = (struct bt_softc*)arg;
 2362         bt->bt_ccb_physbase = segs->ds_addr;
 2363 }
 2364 
 2365 static void
 2366 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 2367 {
 2368 
 2369         struct bt_softc* bt;
 2370 
 2371         bt = (struct bt_softc*)arg;
 2372         SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
 2373 }
 2374 
 2375 static void
 2376 btpoll(struct cam_sim *sim)
 2377 {
 2378         bt_intr(cam_sim_softc(sim));
 2379 }
 2380 
 2381 void
 2382 bttimeout(void *arg)
 2383 {
 2384         struct bt_ccb   *bccb;
 2385         union  ccb      *ccb;
 2386         struct bt_softc *bt;
 2387         int              s;
 2388 
 2389         bccb = (struct bt_ccb *)arg;
 2390         ccb = bccb->ccb;
 2391         bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
 2392         xpt_print_path(ccb->ccb_h.path);
 2393         printf("CCB %p - timed out\n", (void *)bccb);
 2394 
 2395         s = splcam();
 2396 
 2397         if ((bccb->flags & BCCB_ACTIVE) == 0) {
 2398                 xpt_print_path(ccb->ccb_h.path);
 2399                 printf("CCB %p - timed out CCB already completed\n",
 2400                        (void *)bccb);
 2401                 splx(s);
 2402                 return;
 2403         }
 2404 
 2405         /*
 2406          * In order to simplify the recovery process, we ask the XPT
 2407          * layer to halt the queue of new transactions and we traverse
 2408          * the list of pending CCBs and remove their timeouts. This
 2409          * means that the driver attempts to clear only one error
 2410          * condition at a time.  In general, timeouts that occur
 2411          * close together are related anyway, so there is no benefit
 2412          * in attempting to handle errors in parrallel.  Timeouts will
 2413          * be reinstated when the recovery process ends.
 2414          */
 2415         if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
 2416                 struct ccb_hdr *ccb_h;
 2417 
 2418                 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
 2419                         xpt_freeze_simq(bt->sim, /*count*/1);
 2420                         bccb->flags |= BCCB_RELEASE_SIMQ;
 2421                 }
 2422 
 2423                 ccb_h = LIST_FIRST(&bt->pending_ccbs);
 2424                 while (ccb_h != NULL) {
 2425                         struct bt_ccb *pending_bccb;
 2426 
 2427                         pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
 2428                         untimeout(bttimeout, pending_bccb, ccb_h->timeout_ch);
 2429                         ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 2430                 }
 2431         }
 2432 
 2433         if ((bccb->flags & BCCB_DEVICE_RESET) != 0
 2434          || bt->cur_outbox->action_code != BMBO_FREE
 2435          || ((bccb->hccb.tag_enable == TRUE)
 2436           && (bt->firmware_ver[0] < '5'))) {
 2437                 /*
 2438                  * Try a full host adapter/SCSI bus reset.
 2439                  * We do this only if we have already attempted
 2440                  * to clear the condition with a BDR, or we cannot
 2441                  * attempt a BDR for lack of mailbox resources
 2442                  * or because of faulty firmware.  It turns out
 2443                  * that firmware versions prior to 5.xx treat BDRs
 2444                  * as untagged commands that cannot be sent until
 2445                  * all outstanding tagged commands have been processed.
 2446                  * This makes it somewhat difficult to use a BDR to
 2447                  * clear up a problem with an uncompleted tagged command.
 2448                  */
 2449                 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
 2450                 btreset(bt, /*hardreset*/TRUE);
 2451                 printf("%s: No longer in timeout\n", bt_name(bt));
 2452         } else {
 2453                 /*    
 2454                  * Send a Bus Device Reset message:
 2455                  * The target that is holding up the bus may not
 2456                  * be the same as the one that triggered this timeout
 2457                  * (different commands have different timeout lengths),
 2458                  * but we have no way of determining this from our
 2459                  * timeout handler.  Our strategy here is to queue a
 2460                  * BDR message to the target of the timed out command.
 2461                  * If this fails, we'll get another timeout 2 seconds
 2462                  * later which will attempt a bus reset.
 2463                  */
 2464                 bccb->flags |= BCCB_DEVICE_RESET;
 2465                 ccb->ccb_h.timeout_ch =
 2466                     timeout(bttimeout, (caddr_t)bccb, 2 * hz);
 2467 
 2468                 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
 2469 
 2470                 /* No Data Transfer */
 2471                 bt->recovery_bccb->hccb.datain = TRUE;
 2472                 bt->recovery_bccb->hccb.dataout = TRUE;
 2473                 bt->recovery_bccb->hccb.btstat = 0;
 2474                 bt->recovery_bccb->hccb.sdstat = 0;
 2475                 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
 2476 
 2477                 /* Tell the adapter about this command */
 2478                 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
 2479                 bt->cur_outbox->action_code = BMBO_START;
 2480                 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
 2481                 btnextoutbox(bt);
 2482         }
 2483 
 2484         splx(s);
 2485 }
 2486 
 2487 MODULE_VERSION(bt, 1);
 2488 MODULE_DEPEND(bt, cam, 1, 1, 1);

Cache object: 712c40c22d0b422a7ecc831150d00753


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