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

Cache object: 13963c9c85b764e0902a27ebe3955264


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