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

Cache object: ce31c95ea2016a78ac4b188962a9d46f


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