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/aha/aha.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 register and struct definitions for the Adaptech 154x/164x
    3  * SCSI host adapters. Product specific probe and attach routines can
    4  * be found in:
    5  *      aha 1542B/1542C/1542CF/1542CP   aha_isa.c
    6  *      aha 1640                        aha_mca.c
    7  *
    8  * Copyright (c) 1998 M. Warner Losh.
    9  * All Rights Reserved.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions, and the following disclaimer,
   16  *    without modification, immediately at the beginning of the file.
   17  * 2. The name of the author may not be used to endorse or promote products
   18  *    derived from this software without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  * Derived from bt.c written by:
   33  *
   34  * Copyright (c) 1998 Justin T. Gibbs.
   35  * All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions, and the following disclaimer,
   42  *    without modification, immediately at the beginning of the file.
   43  * 2. The name of the author may not be used to endorse or promote products
   44  *    derived from this software without specific prior written permission.
   45  *
   46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   49  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   50  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   56  * SUCH DAMAGE.
   57  */
   58 
   59 #include <sys/cdefs.h>
   60 __FBSDID("$FreeBSD: releng/5.2/sys/dev/aha/aha.c 122597 2003-11-13 04:14:53Z imp $");
   61 
   62 #include <sys/param.h>
   63 #include <sys/bus.h>
   64 #include <sys/systm.h>
   65 #include <sys/malloc.h>
   66 #include <sys/kernel.h>
   67 #include <sys/lock.h>
   68 #include <sys/mutex.h>
   69 
   70 #include <machine/bus_pio.h>
   71 #include <machine/bus.h>
   72 
   73 #include <cam/cam.h>
   74 #include <cam/cam_ccb.h>
   75 #include <cam/cam_sim.h>
   76 #include <cam/cam_xpt_sim.h>
   77 #include <cam/cam_debug.h>
   78 
   79 #include <cam/scsi/scsi_message.h>
   80 
   81 #include <dev/aha/ahareg.h>
   82 
   83 #define PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
   84 
   85 /* Macro to determine that a rev is potentially a new valid one
   86  * so that the driver doesn't keep breaking on new revs as it
   87  * did for the CF and CP.
   88  */
   89 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
   90 
   91 /* MailBox Management functions */
   92 static __inline void    ahanextinbox(struct aha_softc *aha);
   93 static __inline void    ahanextoutbox(struct aha_softc *aha);
   94 
   95 #define aha_name(aha)   device_get_nameunit(aha->dev)
   96 
   97 static __inline void
   98 ahanextinbox(struct aha_softc *aha)
   99 {
  100         if (aha->cur_inbox == aha->last_inbox)
  101                 aha->cur_inbox = aha->in_boxes;
  102         else
  103                 aha->cur_inbox++;
  104 }
  105 
  106 static __inline void
  107 ahanextoutbox(struct aha_softc *aha)
  108 {
  109         if (aha->cur_outbox == aha->last_outbox)
  110                 aha->cur_outbox = aha->out_boxes;
  111         else
  112                 aha->cur_outbox++;
  113 }
  114 
  115 #define ahautoa24(u,s3)                 \
  116         (s3)[0] = ((u) >> 16) & 0xff;   \
  117         (s3)[1] = ((u) >> 8) & 0xff;    \
  118         (s3)[2] = (u) & 0xff;
  119 
  120 #define aha_a24tou(s3) \
  121         (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
  122 
  123 /* CCB Mangement functions */
  124 static __inline uint32_t                ahaccbvtop(struct aha_softc *aha,
  125                                                   struct aha_ccb *accb);
  126 static __inline struct aha_ccb*         ahaccbptov(struct aha_softc *aha,
  127                                                   uint32_t ccb_addr);
  128 
  129 static __inline uint32_t
  130 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
  131 {
  132         return (aha->aha_ccb_physbase
  133               + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
  134 }
  135 static __inline struct aha_ccb *
  136 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
  137 {
  138         return (aha->aha_ccb_array +
  139               + ((struct aha_ccb*)(uintptr_t)ccb_addr -
  140                  (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
  141 }
  142 
  143 static struct aha_ccb*  ahagetccb(struct aha_softc *aha);
  144 static __inline void    ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
  145 static void             ahaallocccbs(struct aha_softc *aha);
  146 static bus_dmamap_callback_t ahaexecuteccb;
  147 static void             ahadone(struct aha_softc *aha, struct aha_ccb *accb,
  148                                aha_mbi_comp_code_t comp_code);
  149 
  150 /* Host adapter command functions */
  151 static int      ahareset(struct aha_softc* aha, int hard_reset);
  152 
  153 /* Initialization functions */
  154 static int                      ahainitmboxes(struct aha_softc *aha);
  155 static bus_dmamap_callback_t    ahamapmboxes;
  156 static bus_dmamap_callback_t    ahamapccbs;
  157 static bus_dmamap_callback_t    ahamapsgs;
  158 
  159 /* Transfer Negotiation Functions */
  160 static void ahafetchtransinfo(struct aha_softc *aha,
  161                              struct ccb_trans_settings *cts);
  162 
  163 /* CAM SIM entry points */
  164 #define ccb_accb_ptr spriv_ptr0
  165 #define ccb_aha_ptr spriv_ptr1
  166 static void     ahaaction(struct cam_sim *sim, union ccb *ccb);
  167 static void     ahapoll(struct cam_sim *sim);
  168 
  169 /* Our timeout handler */
  170 static timeout_t ahatimeout;
  171 
  172 /* Exported functions */
  173 void
  174 aha_alloc(struct aha_softc *aha, int unit, bus_space_tag_t tag,
  175   bus_space_handle_t bsh)
  176 {
  177 
  178         SLIST_INIT(&aha->free_aha_ccbs);
  179         LIST_INIT(&aha->pending_ccbs);
  180         SLIST_INIT(&aha->sg_maps);
  181         aha->unit = unit;
  182         aha->tag = tag;
  183         aha->bsh = bsh;
  184         aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
  185         aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
  186 }
  187 
  188 void
  189 aha_free(struct aha_softc *aha)
  190 {
  191         switch (aha->init_level) {
  192         default:
  193         case 8:
  194         {
  195                 struct sg_map_node *sg_map;
  196 
  197                 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
  198                         SLIST_REMOVE_HEAD(&aha->sg_maps, links);
  199                         bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
  200                         bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
  201                             sg_map->sg_dmamap);
  202                         free(sg_map, M_DEVBUF);
  203                 }
  204                 bus_dma_tag_destroy(aha->sg_dmat);
  205         }
  206         case 7:
  207                 bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
  208         case 6:
  209                 bus_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);
  210                 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
  211                     aha->ccb_dmamap);
  212         case 5:
  213                 bus_dma_tag_destroy(aha->ccb_dmat);
  214         case 4:
  215                 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
  216         case 3:
  217                 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
  218                     aha->mailbox_dmamap);
  219                 bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);
  220         case 2:
  221                 bus_dma_tag_destroy(aha->buffer_dmat);
  222         case 1:
  223                 bus_dma_tag_destroy(aha->mailbox_dmat);
  224         case 0:
  225                 break;
  226         }
  227 }
  228 
  229 /*
  230  * Probe the adapter and verify that the card is an Adaptec.
  231  */
  232 int
  233 aha_probe(struct aha_softc* aha)
  234 {
  235         u_int    status;
  236         u_int    intstat;
  237         int      error;
  238         board_id_data_t board_id;
  239 
  240         /*
  241          * See if the three I/O ports look reasonable.
  242          * Touch the minimal number of registers in the
  243          * failure case.
  244          */
  245         status = aha_inb(aha, STATUS_REG);
  246         if ((status == 0) ||
  247             (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
  248                 PRVERB((aha->dev, "status reg test failed %x\n", status));
  249                 return (ENXIO);
  250         }
  251 
  252         intstat = aha_inb(aha, INTSTAT_REG);
  253         if ((intstat & INTSTAT_REG_RSVD) != 0) {
  254                 PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
  255                 return (ENXIO);
  256         }
  257 
  258         /*
  259          * Looking good so far.  Final test is to reset the
  260          * adapter and fetch the board ID and ensure we aren't
  261          * looking at a BusLogic.
  262          */
  263         if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
  264                 PRVERB((aha->dev, "Failed Reset\n"));
  265                 return (ENXIO);
  266         }
  267 
  268         /*
  269          * Get the board ID.  We use this to see if we're dealing with
  270          * a buslogic card or an aha card (or clone).
  271          */
  272         error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
  273             (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
  274         if (error != 0) {
  275                 PRVERB((aha->dev, "INQUIRE failed %x\n", error));
  276                 return (ENXIO);
  277         }
  278         aha->fw_major = board_id.firmware_rev_major;
  279         aha->fw_minor = board_id.firmware_rev_minor;
  280         aha->boardid = board_id.board_type;
  281 
  282         /*
  283          * The Buslogic cards have an id of either 0x41 or 0x42.  So
  284          * if those come up in the probe, we test the geometry register
  285          * of the board.  Adaptec boards that are this old will not have
  286          * this register, and return 0xff, while buslogic cards will return
  287          * something different.
  288          *
  289          * It appears that for reasons unknow, for the for the
  290          * aha-1542B cards, we need to wait a little bit before trying
  291          * to read the geometry register.  I picked 10ms since we have
  292          * reports that a for loop to 1000 did the trick, and this
  293          * errs on the side of conservatism.  Besides, no one will
  294          * notice a 10mS delay here, even the 1542B card users :-)
  295          *
  296          * Some compatible cards return 0 here.  Some cards also
  297          * seem to return 0x7f.
  298          *
  299          * XXX I'm not sure how this will impact other cloned cards
  300          *
  301          * This really should be replaced with the esetup command, since
  302          * that appears to be more reliable.  This becomes more and more
  303          * true over time as we discover more cards that don't read the
  304          * geometry register consistantly.
  305          */
  306         if (aha->boardid <= 0x42) {
  307                 /* Wait 10ms before reading */
  308                 DELAY(10000);
  309                 status = aha_inb(aha, GEOMETRY_REG);
  310                 if (status != 0xff && status != 0x00 && status != 0x7f) {
  311                         PRVERB((aha->dev, "Geometry Register test failed %#x\n",
  312                                 status));
  313                         return (ENXIO);
  314                 }
  315         }
  316 
  317         return (0);
  318 }
  319 
  320 /*
  321  * Pull the boards setup information and record it in our softc.
  322  */
  323 int
  324 aha_fetch_adapter_info(struct aha_softc *aha)
  325 {
  326         setup_data_t    setup_info;
  327         config_data_t config_data;
  328         uint8_t length_param;
  329         int      error;
  330         struct  aha_extbios extbios;
  331 
  332         switch (aha->boardid) {
  333         case BOARD_1540_16HEAD_BIOS:
  334                 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
  335                 break;
  336         case BOARD_1540_64HEAD_BIOS:
  337                 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
  338                 break;
  339         case BOARD_1542:
  340                 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
  341                 break;
  342         case BOARD_1640:
  343                 snprintf(aha->model, sizeof(aha->model), "1640");
  344                 break;
  345         case BOARD_1740:
  346                 snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");
  347                 break;
  348         case BOARD_1542C:
  349                 snprintf(aha->model, sizeof(aha->model), "1542C");
  350                 break;
  351         case BOARD_1542CF:
  352                 snprintf(aha->model, sizeof(aha->model), "1542CF");
  353                 break;
  354         case BOARD_1542CP:
  355                 snprintf(aha->model, sizeof(aha->model), "1542CP");
  356                 break;
  357         default:
  358                 snprintf(aha->model, sizeof(aha->model), "Unknown");
  359                 break;
  360         }
  361         /*
  362          * If we are a new type of 1542 board (anything newer than a 1542C)
  363          * then disable the extended bios so that the
  364          * mailbox interface is unlocked.
  365          * This is also true for the 1542B Version 3.20. First Adaptec
  366          * board that supports >1Gb drives.
  367          * No need to check the extended bios flags as some of the
  368          * extensions that cause us problems are not flagged in that byte.
  369          */
  370         if (PROBABLY_NEW_BOARD(aha->boardid) ||
  371                 (aha->boardid == 0x41
  372                 && aha->fw_major == 0x31 &&
  373                 aha->fw_minor >= 0x34)) {
  374                 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
  375                     /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
  376                     DEFAULT_CMD_TIMEOUT);
  377                 if (error != 0) {
  378                         device_printf(aha->dev,
  379                             "AOP_RETURN_EXT_BIOS_INFO - Failed.");
  380                         return (error);
  381                 }
  382                 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
  383                     /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
  384                 if (error != 0) {
  385                         device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
  386                         return (error);
  387                 }
  388         }
  389         if (aha->boardid < 0x41)
  390                 device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
  391 
  392         aha->max_sg = 17;               /* Need >= 17 to do 64k I/O */
  393         aha->diff_bus = 0;
  394         aha->extended_lun = 0;
  395         aha->extended_trans = 0;
  396         aha->max_ccbs = 16;
  397         /* Determine Sync/Wide/Disc settings */
  398         length_param = sizeof(setup_info);
  399         error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
  400             /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
  401             DEFAULT_CMD_TIMEOUT);
  402         if (error != 0) {
  403                 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
  404                     "Get Setup Info\n");
  405                 return (error);
  406         }
  407         if (setup_info.initiate_sync != 0) {
  408                 aha->sync_permitted = ALL_TARGETS;
  409         }
  410         aha->disc_permitted = ALL_TARGETS;
  411 
  412         /* We need as many mailboxes as we can have ccbs */
  413         aha->num_boxes = aha->max_ccbs;
  414 
  415         /* Determine our SCSI ID */
  416         error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
  417             (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
  418         if (error != 0) {
  419                 device_printf(aha->dev,
  420                     "aha_fetch_adapter_info - Failed Get Config\n");
  421                 return (error);
  422         }
  423         aha->scsi_id = config_data.scsi_id;
  424         return (0);
  425 }
  426 
  427 /*
  428  * Start the board, ready for normal operation
  429  */
  430 int
  431 aha_init(struct aha_softc* aha)
  432 {
  433         /* Announce the Adapter */
  434         device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
  435             aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
  436 
  437         if (aha->diff_bus != 0)
  438                 printf("Diff ");
  439 
  440         printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
  441             aha->max_ccbs);
  442 
  443         /*
  444          * Create our DMA tags.  These tags define the kinds of device
  445          * accessible memory allocations and memory mappings we will
  446          * need to perform during normal operation.
  447          *
  448          * Unless we need to further restrict the allocation, we rely
  449          * on the restrictions of the parent dmat, hence the common
  450          * use of MAXADDR and MAXSIZE.
  451          */
  452 
  453         /* DMA tag for mapping buffers into device visible space. */
  454         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
  455                                 /* alignment    */ 1,
  456                                 /* boundary     */ 0,
  457                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  458                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  459                                 /* filter       */ NULL,
  460                                 /* filterarg    */ NULL,
  461                                 /* maxsize      */ MAXBSIZE,
  462                                 /* nsegments    */ AHA_NSEG,
  463                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
  464                                 /* flags        */ BUS_DMA_ALLOCNOW,
  465                                 /* lockfunc     */ busdma_lock_mutex,
  466                                 /* lockarg      */ &Giant,
  467                                 &aha->buffer_dmat) != 0) {
  468                 goto error_exit;
  469         }
  470 
  471         aha->init_level++;
  472         /* DMA tag for our mailboxes */
  473         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
  474                                 /* alignment    */ 1,
  475                                 /* boundary     */ 0,
  476                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  477                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  478                                 /* filter       */ NULL,
  479                                 /* filterarg    */ NULL,
  480                                 /* maxsize      */ aha->num_boxes *
  481                                                    (sizeof(aha_mbox_in_t) +
  482                                                     sizeof(aha_mbox_out_t)),
  483                                 /* nsegments    */ 1,
  484                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
  485                                 /* flags        */ 0,
  486                                 /* lockfunc     */ busdma_lock_mutex,
  487                                 /* lockarg      */ &Giant,
  488                                 &aha->mailbox_dmat) != 0) {
  489                 goto error_exit;
  490         }
  491 
  492         aha->init_level++;
  493 
  494         /* Allocation for our mailboxes */
  495         if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
  496             BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
  497                 goto error_exit;
  498 
  499         aha->init_level++;
  500 
  501         /* And permanently map them */
  502         bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
  503             aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
  504             sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
  505 
  506         aha->init_level++;
  507 
  508         aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
  509 
  510         ahainitmboxes(aha);
  511 
  512         /* DMA tag for our ccb structures */
  513         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
  514                                 /* alignment    */ 1,
  515                                 /* boundary     */ 0,
  516                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  517                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  518                                 /* filter       */ NULL,
  519                                 /* filterarg    */ NULL,
  520                                 /* maxsize      */ aha->max_ccbs *
  521                                                    sizeof(struct aha_ccb),
  522                                 /* nsegments    */ 1,
  523                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
  524                                 /* flags        */ 0,
  525                                 /* lockfunc     */ busdma_lock_mutex,
  526                                 /* lockarg      */ &Giant,
  527                                 &aha->ccb_dmat) != 0) {
  528                 goto error_exit;
  529         }
  530 
  531         aha->init_level++;
  532 
  533         /* Allocation for our ccbs */
  534         if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
  535             BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
  536                 goto error_exit;
  537 
  538         aha->init_level++;
  539 
  540         /* And permanently map them */
  541         bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
  542             aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
  543 
  544         aha->init_level++;
  545 
  546         /* DMA tag for our S/G structures.  We allocate in page sized chunks */
  547         if (bus_dma_tag_create( /* parent       */ aha->parent_dmat,
  548                                 /* alignment    */ 1,
  549                                 /* boundary     */ 0,
  550                                 /* lowaddr      */ BUS_SPACE_MAXADDR,
  551                                 /* highaddr     */ BUS_SPACE_MAXADDR,
  552                                 /* filter       */ NULL,
  553                                 /* filterarg    */ NULL,
  554                                 /* maxsize      */ PAGE_SIZE,
  555                                 /* nsegments    */ 1,
  556                                 /* maxsegsz     */ BUS_SPACE_MAXSIZE_24BIT,
  557                                 /* flags        */ 0,
  558                                 /* lockfunc     */ busdma_lock_mutex,
  559                                 /* lockarg      */ &Giant,
  560                                 &aha->sg_dmat) != 0)
  561                 goto error_exit;
  562 
  563         aha->init_level++;
  564 
  565         /* Perform initial CCB allocation */
  566         bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
  567         ahaallocccbs(aha);
  568 
  569         if (aha->num_ccbs == 0) {
  570                 device_printf(aha->dev,
  571                     "aha_init - Unable to allocate initial ccbs\n");
  572                 goto error_exit;
  573         }
  574 
  575         /*
  576          * Note that we are going and return (to probe)
  577          */
  578         return (0);
  579 
  580 error_exit:
  581 
  582         return (ENXIO);
  583 }
  584 
  585 int
  586 aha_attach(struct aha_softc *aha)
  587 {
  588         int tagged_dev_openings;
  589         struct cam_devq *devq;
  590 
  591         /*
  592          * We don't do tagged queueing, since the aha cards don't
  593          * support it.
  594          */
  595         tagged_dev_openings = 0;
  596 
  597         /*
  598          * Create the device queue for our SIM.
  599          */
  600         devq = cam_simq_alloc(aha->max_ccbs - 1);
  601         if (devq == NULL)
  602                 return (ENOMEM);
  603 
  604         /*
  605          * Construct our SIM entry
  606          */
  607         aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha, aha->unit, 2,
  608             tagged_dev_openings, devq);
  609         if (aha->sim == NULL) {
  610                 cam_simq_free(devq);
  611                 return (ENOMEM);
  612         }
  613         if (xpt_bus_register(aha->sim, 0) != CAM_SUCCESS) {
  614                 cam_sim_free(aha->sim, /*free_devq*/TRUE);
  615                 return (ENXIO);
  616         }
  617         if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
  618             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  619                 xpt_bus_deregister(cam_sim_path(aha->sim));
  620                 cam_sim_free(aha->sim, /*free_devq*/TRUE);
  621                 return (ENXIO);
  622         }
  623 
  624         return (0);
  625 }
  626 
  627 static void
  628 ahaallocccbs(struct aha_softc *aha)
  629 {
  630         struct aha_ccb *next_ccb;
  631         struct sg_map_node *sg_map;
  632         bus_addr_t physaddr;
  633         aha_sg_t *segs;
  634         int newcount;
  635         int i;
  636 
  637         next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
  638 
  639         sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  640 
  641         if (sg_map == NULL)
  642                 return;
  643 
  644         /* Allocate S/G space for the next batch of CCBS */
  645         if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
  646             BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
  647                 free(sg_map, M_DEVBUF);
  648                 return;
  649         }
  650 
  651         SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
  652 
  653         bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
  654             PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
  655 
  656         segs = sg_map->sg_vaddr;
  657         physaddr = sg_map->sg_physaddr;
  658 
  659         newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
  660         for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
  661                 int error;
  662 
  663                 next_ccb->sg_list = segs;
  664                 next_ccb->sg_list_phys = physaddr;
  665                 next_ccb->flags = ACCB_FREE;
  666                 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
  667                     &next_ccb->dmamap);
  668                 if (error != 0)
  669                         break;
  670                 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
  671                 segs += AHA_NSEG;
  672                 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
  673                 next_ccb++;
  674                 aha->num_ccbs++;
  675         }
  676 
  677         /* Reserve a CCB for error recovery */
  678         if (aha->recovery_accb == NULL) {
  679                 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
  680                 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
  681         }
  682 }
  683 
  684 static __inline void
  685 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
  686 {
  687         int s;
  688 
  689         s = splcam();
  690         if ((accb->flags & ACCB_ACTIVE) != 0)
  691                 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
  692         if (aha->resource_shortage != 0
  693             && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
  694                 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
  695                 aha->resource_shortage = FALSE;
  696         }
  697         accb->flags = ACCB_FREE;
  698         SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
  699         aha->active_ccbs--;
  700         splx(s);
  701 }
  702 
  703 static struct aha_ccb*
  704 ahagetccb(struct aha_softc *aha)
  705 {
  706         struct  aha_ccb* accb;
  707         int     s;
  708 
  709         s = splcam();
  710         if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
  711                 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
  712                 aha->active_ccbs++;
  713         } else if (aha->num_ccbs < aha->max_ccbs) {
  714                 ahaallocccbs(aha);
  715                 accb = SLIST_FIRST(&aha->free_aha_ccbs);
  716                 if (accb == NULL)
  717                         device_printf(aha->dev, "Can't malloc ACCB\n");
  718                 else {
  719                         SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
  720                         aha->active_ccbs++;
  721                 }
  722         }
  723         splx(s);
  724 
  725         return (accb);
  726 }
  727 
  728 static void
  729 ahaaction(struct cam_sim *sim, union ccb *ccb)
  730 {
  731         struct  aha_softc *aha;
  732         int s;
  733 
  734         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
  735 
  736         aha = (struct aha_softc *)cam_sim_softc(sim);
  737 
  738         switch (ccb->ccb_h.func_code) {
  739         /* Common cases first */
  740         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
  741         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */        {
  742                 struct  aha_ccb *accb;
  743                 struct  aha_hccb *hccb;
  744 
  745                 /*
  746                  * Get an accb to use.
  747                  */
  748                 if ((accb = ahagetccb(aha)) == NULL) {
  749                         s = splcam();
  750                         aha->resource_shortage = TRUE;
  751                         splx(s);
  752                         xpt_freeze_simq(aha->sim, /*count*/1);
  753                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
  754                         xpt_done(ccb);
  755                         return;
  756                 }
  757                 hccb = &accb->hccb;
  758 
  759                 /*
  760                  * So we can find the ACCB when an abort is requested
  761                  */
  762                 accb->ccb = ccb;
  763                 ccb->ccb_h.ccb_accb_ptr = accb;
  764                 ccb->ccb_h.ccb_aha_ptr = aha;
  765 
  766                 /*
  767                  * Put all the arguments for the xfer in the accb
  768                  */
  769                 hccb->target = ccb->ccb_h.target_id;
  770                 hccb->lun = ccb->ccb_h.target_lun;
  771                 hccb->ahastat = 0;
  772                 hccb->sdstat = 0;
  773 
  774                 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
  775                         struct ccb_scsiio *csio;
  776                         struct ccb_hdr *ccbh;
  777 
  778                         csio = &ccb->csio;
  779                         ccbh = &csio->ccb_h;
  780                         hccb->opcode = aha->ccb_ccb_opcode;
  781                         hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
  782                         hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
  783                         hccb->cmd_len = csio->cdb_len;
  784                         if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
  785                                 ccb->ccb_h.status = CAM_REQ_INVALID;
  786                                 ahafreeccb(aha, accb);
  787                                 xpt_done(ccb);
  788                                 return;
  789                         }
  790                         hccb->sense_len = csio->sense_len;
  791                         if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
  792                                 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
  793                                         bcopy(csio->cdb_io.cdb_ptr,
  794                                               hccb->scsi_cdb, hccb->cmd_len);
  795                                 } else {
  796                                         /* I guess I could map it in... */
  797                                         ccbh->status = CAM_REQ_INVALID;
  798                                         ahafreeccb(aha, accb);
  799                                         xpt_done(ccb);
  800                                         return;
  801                                 }
  802                         } else {
  803                                 bcopy(csio->cdb_io.cdb_bytes,
  804                                       hccb->scsi_cdb, hccb->cmd_len);
  805                         }
  806                         /*
  807                          * If we have any data to send with this command,
  808                          * map it into bus space.
  809                          */
  810                         /* Only use S/G if there is a transfer */
  811                         if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  812                                 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
  813                                         /*
  814                                          * We've been given a pointer
  815                                          * to a single buffer.
  816                                          */
  817                                         if ((ccbh->flags & CAM_DATA_PHYS)==0) {
  818                                                 int error;
  819 
  820                                                 s = splsoftvm();
  821                                                 error = bus_dmamap_load(
  822                                                     aha->buffer_dmat,
  823                                                     accb->dmamap,
  824                                                     csio->data_ptr,
  825                                                     csio->dxfer_len,
  826                                                     ahaexecuteccb,
  827                                                     accb,
  828                                                     /*flags*/0);
  829                                                 if (error == EINPROGRESS) {
  830                                                         /*
  831                                                          * So as to maintain
  832                                                          * ordering, freeze the
  833                                                          * controller queue
  834                                                          * until our mapping is
  835                                                          * returned.
  836                                                          */
  837                                                         xpt_freeze_simq(aha->sim,
  838                                                                         1);
  839                                                         csio->ccb_h.status |=
  840                                                             CAM_RELEASE_SIMQ;
  841                                                 }
  842                                                 splx(s);
  843                                         } else {
  844                                                 struct bus_dma_segment seg;
  845 
  846                                                 /* Pointer to physical buffer */
  847                                                 seg.ds_addr =
  848                                                     (bus_addr_t)csio->data_ptr;
  849                                                 seg.ds_len = csio->dxfer_len;
  850                                                 ahaexecuteccb(accb, &seg, 1, 0);
  851                                         }
  852                                 } else {
  853                                         struct bus_dma_segment *segs;
  854 
  855                                         if ((ccbh->flags & CAM_DATA_PHYS) != 0)
  856                                                 panic("ahaaction - Physical "
  857                                                       "segment pointers "
  858                                                       "unsupported");
  859 
  860                                         if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
  861                                                 panic("ahaaction - Virtual "
  862                                                       "segment addresses "
  863                                                       "unsupported");
  864 
  865                                         /* Just use the segments provided */
  866                                         segs = (struct bus_dma_segment *)
  867                                             csio->data_ptr;
  868                                         ahaexecuteccb(accb, segs,
  869                                                      csio->sglist_cnt, 0);
  870                                 }
  871                         } else {
  872                                 ahaexecuteccb(accb, NULL, 0, 0);
  873                         }
  874                 } else {
  875                         hccb->opcode = INITIATOR_BUS_DEV_RESET;
  876                         /* No data transfer */
  877                         hccb->datain = TRUE;
  878                         hccb->dataout = TRUE;
  879                         hccb->cmd_len = 0;
  880                         hccb->sense_len = 0;
  881                         ahaexecuteccb(accb, NULL, 0, 0);
  882                 }
  883                 break;
  884         }
  885         case XPT_EN_LUN:                /* Enable LUN as a target */
  886         case XPT_TARGET_IO:             /* Execute target I/O request */
  887         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
  888         case XPT_CONT_TARGET_IO:        /* Continue Host Target I/O Connection*/
  889         case XPT_ABORT:                 /* Abort the specified CCB */
  890                 /* XXX Implement */
  891                 ccb->ccb_h.status = CAM_REQ_INVALID;
  892                 xpt_done(ccb);
  893                 break;
  894         case XPT_SET_TRAN_SETTINGS:
  895                 /* XXX Implement */
  896                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
  897                 xpt_done(ccb);
  898                 break;
  899         case XPT_GET_TRAN_SETTINGS:
  900         /* Get default/user set transfer settings for the target */
  901         {
  902                 struct  ccb_trans_settings *cts;
  903                 u_int   target_mask;
  904 
  905                 cts = &ccb->cts;
  906                 target_mask = 0x01 << ccb->ccb_h.target_id;
  907                 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
  908                         cts->flags = 0;
  909                         if ((aha->disc_permitted & target_mask) != 0)
  910                                 cts->flags |= CCB_TRANS_DISC_ENB;
  911                         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  912                         if ((aha->sync_permitted & target_mask) != 0) {
  913                                 if (aha->boardid >= BOARD_1542CF)
  914                                         cts->sync_period = 25;
  915                                 else
  916                                         cts->sync_period = 50;
  917                         } else
  918                                 cts->sync_period = 0;
  919 
  920                         if (cts->sync_period != 0)
  921                                 cts->sync_offset = 15;
  922 
  923                         cts->valid = CCB_TRANS_SYNC_RATE_VALID
  924                                    | CCB_TRANS_SYNC_OFFSET_VALID
  925                                    | CCB_TRANS_BUS_WIDTH_VALID
  926                                    | CCB_TRANS_DISC_VALID
  927                                    | CCB_TRANS_TQ_VALID;
  928                 } else {
  929                         ahafetchtransinfo(aha, cts);
  930                 }
  931 
  932                 ccb->ccb_h.status = CAM_REQ_CMP;
  933                 xpt_done(ccb);
  934                 break;
  935         }
  936         case XPT_CALC_GEOMETRY:
  937         {
  938                 struct    ccb_calc_geometry *ccg;
  939                 uint32_t size_mb;
  940                 uint32_t secs_per_cylinder;
  941 
  942                 ccg = &ccb->ccg;
  943                 size_mb = ccg->volume_size
  944                         / ((1024L * 1024L) / ccg->block_size);
  945                 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
  946                         if (size_mb >= 2048) {
  947                                 ccg->heads = 255;
  948                                 ccg->secs_per_track = 63;
  949                         } else {
  950                                 ccg->heads = 128;
  951                                 ccg->secs_per_track = 32;
  952                         }
  953                 } else {
  954                         ccg->heads = 64;
  955                         ccg->secs_per_track = 32;
  956                 }
  957                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
  958                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
  959                 ccb->ccb_h.status = CAM_REQ_CMP;
  960                 xpt_done(ccb);
  961                 break;
  962         }
  963         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
  964                 ahareset(aha, /*hardreset*/TRUE);
  965                 ccb->ccb_h.status = CAM_REQ_CMP;
  966                 xpt_done(ccb);
  967                 break;
  968         case XPT_TERM_IO:               /* Terminate the I/O process */
  969                 /* XXX Implement */
  970                 ccb->ccb_h.status = CAM_REQ_INVALID;
  971                 xpt_done(ccb);
  972                 break;
  973         case XPT_PATH_INQ:              /* Path routing inquiry */
  974         {
  975                 struct ccb_pathinq *cpi = &ccb->cpi;
  976 
  977                 cpi->version_num = 1; /* XXX??? */
  978                 cpi->hba_inquiry = PI_SDTR_ABLE;
  979                 cpi->target_sprt = 0;
  980                 cpi->hba_misc = 0;
  981                 cpi->hba_eng_cnt = 0;
  982                 cpi->max_target = 7;
  983                 cpi->max_lun = 7;
  984                 cpi->initiator_id = aha->scsi_id;
  985                 cpi->bus_id = cam_sim_bus(sim);
  986                 cpi->base_transfer_speed = 3300;
  987                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
  988                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
  989                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
  990                 cpi->unit_number = cam_sim_unit(sim);
  991                 cpi->ccb_h.status = CAM_REQ_CMP;
  992                 xpt_done(ccb);
  993                 break;
  994         }
  995         default:
  996                 ccb->ccb_h.status = CAM_REQ_INVALID;
  997                 xpt_done(ccb);
  998                 break;
  999         }
 1000 }
 1001 
 1002 static void
 1003 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
 1004 {
 1005         struct   aha_ccb *accb;
 1006         union    ccb *ccb;
 1007         struct   aha_softc *aha;
 1008         int      s;
 1009         uint32_t paddr;
 1010 
 1011         accb = (struct aha_ccb *)arg;
 1012         ccb = accb->ccb;
 1013         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
 1014 
 1015         if (error != 0) {
 1016                 if (error != EFBIG)
 1017                         device_printf(aha->dev,
 1018                             "Unexepected error 0x%x returned from "
 1019                             "bus_dmamap_load\n", error);
 1020                 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
 1021                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
 1022                         ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
 1023                 }
 1024                 ahafreeccb(aha, accb);
 1025                 xpt_done(ccb);
 1026                 return;
 1027         }
 1028 
 1029         if (nseg != 0) {
 1030                 aha_sg_t *sg;
 1031                 bus_dma_segment_t *end_seg;
 1032                 bus_dmasync_op_t op;
 1033 
 1034                 end_seg = dm_segs + nseg;
 1035 
 1036                 /* Copy the segments into our SG list */
 1037                 sg = accb->sg_list;
 1038                 while (dm_segs < end_seg) {
 1039                         ahautoa24(dm_segs->ds_len, sg->len);
 1040                         ahautoa24(dm_segs->ds_addr, sg->addr);
 1041                         sg++;
 1042                         dm_segs++;
 1043                 }
 1044 
 1045                 if (nseg > 1) {
 1046                         accb->hccb.opcode = aha->ccb_sg_opcode;
 1047                         ahautoa24((sizeof(aha_sg_t) * nseg),
 1048                             accb->hccb.data_len);
 1049                         ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
 1050                 } else {
 1051                         bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
 1052                         bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
 1053                 }
 1054 
 1055                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
 1056                         op = BUS_DMASYNC_PREREAD;
 1057                 else
 1058                         op = BUS_DMASYNC_PREWRITE;
 1059 
 1060                 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
 1061 
 1062         } else {
 1063                 accb->hccb.opcode = INITIATOR_CCB;
 1064                 ahautoa24(0, accb->hccb.data_len);
 1065                 ahautoa24(0, accb->hccb.data_addr);
 1066         }
 1067 
 1068         s = splcam();
 1069 
 1070         /*
 1071          * Last time we need to check if this CCB needs to
 1072          * be aborted.
 1073          */
 1074         if (ccb->ccb_h.status != CAM_REQ_INPROG) {
 1075                 if (nseg != 0)
 1076                         bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
 1077                 ahafreeccb(aha, accb);
 1078                 xpt_done(ccb);
 1079                 splx(s);
 1080                 return;
 1081         }
 1082 
 1083         accb->flags = ACCB_ACTIVE;
 1084         ccb->ccb_h.status |= CAM_SIM_QUEUED;
 1085         LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
 1086 
 1087         ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb,
 1088             (ccb->ccb_h.timeout * hz) / 1000);
 1089 
 1090         /* Tell the adapter about this command */
 1091         if (aha->cur_outbox->action_code != AMBO_FREE) {
 1092                 /*
 1093                  * We should never encounter a busy mailbox.
 1094                  * If we do, warn the user, and treat it as
 1095                  * a resource shortage.  If the controller is
 1096                  * hung, one of the pending transactions will
 1097                  * timeout causing us to start recovery operations.
 1098                  */
 1099                 device_printf(aha->dev,
 1100                     "Encountered busy mailbox with %d out of %d "
 1101                     "commands active!!!", aha->active_ccbs, aha->max_ccbs);
 1102                 untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
 1103                 if (nseg != 0)
 1104                         bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
 1105                 ahafreeccb(aha, accb);
 1106                 aha->resource_shortage = TRUE;
 1107                 xpt_freeze_simq(aha->sim, /*count*/1);
 1108                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
 1109                 xpt_done(ccb);
 1110                 return;
 1111         }
 1112         paddr = ahaccbvtop(aha, accb);
 1113         ahautoa24(paddr, aha->cur_outbox->ccb_addr);
 1114         aha->cur_outbox->action_code = AMBO_START;
 1115         aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
 1116 
 1117         ahanextoutbox(aha);
 1118         splx(s);
 1119 }
 1120 
 1121 void
 1122 aha_intr(void *arg)
 1123 {
 1124         struct  aha_softc *aha;
 1125         u_int   intstat;
 1126         uint32_t paddr;
 1127 
 1128         aha = (struct aha_softc *)arg;
 1129         while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
 1130                 if ((intstat & CMD_COMPLETE) != 0) {
 1131                         aha->latched_status = aha_inb(aha, STATUS_REG);
 1132                         aha->command_cmp = TRUE;
 1133                 }
 1134 
 1135                 aha_outb(aha, CONTROL_REG, RESET_INTR);
 1136 
 1137                 if ((intstat & IMB_LOADED) != 0) {
 1138                         while (aha->cur_inbox->comp_code != AMBI_FREE) {
 1139                                 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
 1140                                 ahadone(aha, ahaccbptov(aha, paddr),
 1141                                     aha->cur_inbox->comp_code);
 1142                                 aha->cur_inbox->comp_code = AMBI_FREE;
 1143                                 ahanextinbox(aha);
 1144                         }
 1145                 }
 1146 
 1147                 if ((intstat & SCSI_BUS_RESET) != 0) {
 1148                         ahareset(aha, /*hardreset*/FALSE);
 1149                 }
 1150         }
 1151 }
 1152 
 1153 static void
 1154 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
 1155 {
 1156         union  ccb        *ccb;
 1157         struct ccb_scsiio *csio;
 1158 
 1159         ccb = accb->ccb;
 1160         csio = &accb->ccb->csio;
 1161 
 1162         if ((accb->flags & ACCB_ACTIVE) == 0) {
 1163                 device_printf(aha->dev, 
 1164                     "ahadone - Attempt to free non-active ACCB %p\n",
 1165                     (void *)accb);
 1166                 return;
 1167         }
 1168 
 1169         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
 1170                 bus_dmasync_op_t op;
 1171 
 1172                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
 1173                         op = BUS_DMASYNC_POSTREAD;
 1174                 else
 1175                         op = BUS_DMASYNC_POSTWRITE;
 1176                 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
 1177                 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
 1178         }
 1179 
 1180         if (accb == aha->recovery_accb) {
 1181                 /*
 1182                  * The recovery ACCB does not have a CCB associated
 1183                  * with it, so short circuit the normal error handling.
 1184                  * We now traverse our list of pending CCBs and process
 1185                  * any that were terminated by the recovery CCBs action.
 1186                  * We also reinstate timeouts for all remaining, pending,
 1187                  * CCBs.
 1188                  */
 1189                 struct cam_path *path;
 1190                 struct ccb_hdr *ccb_h;
 1191                 cam_status error;
 1192 
 1193                 /* Notify all clients that a BDR occured */
 1194                 error = xpt_create_path(&path, /*periph*/NULL,
 1195                     cam_sim_path(aha->sim), accb->hccb.target,
 1196                     CAM_LUN_WILDCARD);
 1197 
 1198                 if (error == CAM_REQ_CMP)
 1199                         xpt_async(AC_SENT_BDR, path, NULL);
 1200 
 1201                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
 1202                 while (ccb_h != NULL) {
 1203                         struct aha_ccb *pending_accb;
 1204 
 1205                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
 1206                         if (pending_accb->hccb.target == accb->hccb.target) {
 1207                                 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
 1208                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 1209                                 ahadone(aha, pending_accb, AMBI_ERROR);
 1210                         } else {
 1211                                 ccb_h->timeout_ch = timeout(ahatimeout,
 1212                                     (caddr_t)pending_accb,
 1213                                     (ccb_h->timeout * hz) / 1000);
 1214                                 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 1215                         }
 1216                 }
 1217                 device_printf(aha->dev, "No longer in timeout\n");
 1218                 return;
 1219         }
 1220 
 1221         untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
 1222 
 1223         switch (comp_code) {
 1224         case AMBI_FREE:
 1225                 device_printf(aha->dev,
 1226                     "ahadone - CCB completed with free status!\n");
 1227                 break;
 1228         case AMBI_NOT_FOUND:
 1229                 device_printf(aha->dev,
 1230                     "ahadone - CCB Abort failed to find CCB\n");
 1231                 break;
 1232         case AMBI_ABORT:
 1233         case AMBI_ERROR:
 1234                 /* An error occured */
 1235                 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
 1236                         csio->resid = 0;
 1237                 else
 1238                         csio->resid = aha_a24tou(accb->hccb.data_len);
 1239                 switch(accb->hccb.ahastat) {
 1240                 case AHASTAT_DATARUN_ERROR:
 1241                 {
 1242                         if (csio->resid <= 0) {
 1243                                 csio->ccb_h.status = CAM_DATA_RUN_ERR;
 1244                                 break;
 1245                         }
 1246                         /* FALLTHROUGH */
 1247                 }
 1248                 case AHASTAT_NOERROR:
 1249                         csio->scsi_status = accb->hccb.sdstat;
 1250                         csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
 1251                         switch(csio->scsi_status) {
 1252                         case SCSI_STATUS_CHECK_COND:
 1253                         case SCSI_STATUS_CMD_TERMINATED:
 1254                                 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
 1255                                 /*
 1256                                  * The aha writes the sense data at different
 1257                                  * offsets based on the scsi cmd len
 1258                                  */
 1259                                 bcopy((caddr_t) &accb->hccb.scsi_cdb +
 1260                                     accb->hccb.cmd_len,
 1261                                     (caddr_t) &csio->sense_data,
 1262                                     accb->hccb.sense_len);
 1263                                 break;
 1264                         default:
 1265                                 break;
 1266                         case SCSI_STATUS_OK:
 1267                                 csio->ccb_h.status = CAM_REQ_CMP;
 1268                                 break;
 1269                         }
 1270                         break;
 1271                 case AHASTAT_SELTIMEOUT:
 1272                         csio->ccb_h.status = CAM_SEL_TIMEOUT;
 1273                         break;
 1274                 case AHASTAT_UNEXPECTED_BUSFREE:
 1275                         csio->ccb_h.status = CAM_UNEXP_BUSFREE;
 1276                         break;
 1277                 case AHASTAT_INVALID_PHASE:
 1278                         csio->ccb_h.status = CAM_SEQUENCE_FAIL;
 1279                         break;
 1280                 case AHASTAT_INVALID_ACTION_CODE:
 1281                         panic("%s: Inavlid Action code", aha_name(aha));
 1282                         break;
 1283                 case AHASTAT_INVALID_OPCODE:
 1284                         if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
 1285                                 panic("%s: Invalid CCB Opcode %x hccb = %p",
 1286                                     aha_name(aha), accb->hccb.opcode,
 1287                                     &accb->hccb);
 1288                         device_printf(aha->dev,
 1289                             "AHA-1540A detected, maybe compensating\n");
 1290                         aha->ccb_sg_opcode = INITIATOR_SG_CCB;
 1291                         aha->ccb_ccb_opcode = INITIATOR_CCB;
 1292                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
 1293                         csio->ccb_h.status = CAM_REQUEUE_REQ;
 1294                         break;
 1295                 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
 1296                         /* We don't even support linked commands... */
 1297                         panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
 1298                         break;
 1299                 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
 1300                         panic("%s: Invalid CCB or SG list", aha_name(aha));
 1301                         break;
 1302                 case AHASTAT_HA_SCSI_BUS_RESET:
 1303                         if ((csio->ccb_h.status & CAM_STATUS_MASK)
 1304                             != CAM_CMD_TIMEOUT)
 1305                                 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
 1306                         break;
 1307                 case AHASTAT_HA_BDR:
 1308                         if ((accb->flags & ACCB_DEVICE_RESET) == 0)
 1309                                 csio->ccb_h.status = CAM_BDR_SENT;
 1310                         else
 1311                                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
 1312                         break;
 1313                 }
 1314                 if (csio->ccb_h.status != CAM_REQ_CMP) {
 1315                         xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
 1316                         csio->ccb_h.status |= CAM_DEV_QFRZN;
 1317                 }
 1318                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
 1319                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1320                 ahafreeccb(aha, accb);
 1321                 xpt_done(ccb);
 1322                 break;
 1323         case AMBI_OK:
 1324                 /* All completed without incident */
 1325                 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
 1326                 /* I don't think so since it works???? */
 1327                 ccb->ccb_h.status |= CAM_REQ_CMP;
 1328                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
 1329                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1330                 ahafreeccb(aha, accb);
 1331                 xpt_done(ccb);
 1332                 break;
 1333         }
 1334 }
 1335 
 1336 static int
 1337 ahareset(struct aha_softc* aha, int hard_reset)
 1338 {
 1339         struct   ccb_hdr *ccb_h;
 1340         u_int    status;
 1341         u_int    timeout;
 1342         uint8_t reset_type;
 1343 
 1344         if (hard_reset != 0)
 1345                 reset_type = HARD_RESET;
 1346         else
 1347                 reset_type = SOFT_RESET;
 1348         aha_outb(aha, CONTROL_REG, reset_type);
 1349 
 1350         /* Wait 5sec. for Diagnostic start */
 1351         timeout = 5 * 10000;
 1352         while (--timeout) {
 1353                 status = aha_inb(aha, STATUS_REG);
 1354                 if ((status & DIAG_ACTIVE) != 0)
 1355                         break;
 1356                 DELAY(100);
 1357         }
 1358         if (timeout == 0) {
 1359                 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
 1360                     "assert. status = %#x\n", status));
 1361                 return (ETIMEDOUT);
 1362         }
 1363 
 1364         /* Wait 10sec. for Diagnostic end */
 1365         timeout = 10 * 10000;
 1366         while (--timeout) {
 1367                 status = aha_inb(aha, STATUS_REG);
 1368                 if ((status & DIAG_ACTIVE) == 0)
 1369                         break;
 1370                 DELAY(100);
 1371         }
 1372         if (timeout == 0) {
 1373                 panic("%s: ahareset - Diagnostic Active failed to drop. "
 1374                     "status = 0x%x\n", aha_name(aha), status);
 1375                 return (ETIMEDOUT);
 1376         }
 1377 
 1378         /* Wait for the host adapter to become ready or report a failure */
 1379         timeout = 10000;
 1380         while (--timeout) {
 1381                 status = aha_inb(aha, STATUS_REG);
 1382                 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
 1383                         break;
 1384                 DELAY(100);
 1385         }
 1386         if (timeout == 0) {
 1387                 device_printf(aha->dev, "ahareset - Host adapter failed to "
 1388                     "come ready. status = 0x%x\n", status);
 1389                 return (ETIMEDOUT);
 1390         }
 1391 
 1392         /* If the diagnostics failed, tell the user */
 1393         if ((status & DIAG_FAIL) != 0
 1394          || (status & HA_READY) == 0) {
 1395                 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
 1396 
 1397                 if ((status & DATAIN_REG_READY) != 0)
 1398                         device_printf(aha->dev, "ahareset - Host Adapter "
 1399                             "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
 1400                 return (ENXIO);
 1401         }
 1402 
 1403         /* If we've attached to the XPT, tell it about the event */
 1404         if (aha->path != NULL)
 1405                 xpt_async(AC_BUS_RESET, aha->path, NULL);
 1406 
 1407         /*
 1408          * Perform completion processing for all outstanding CCBs.
 1409          */
 1410         while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
 1411                 struct aha_ccb *pending_accb;
 1412 
 1413                 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
 1414                 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
 1415                 ahadone(aha, pending_accb, AMBI_ERROR);
 1416         }
 1417 
 1418         /* If we've allocated mailboxes, initialize them */
 1419         /* Must be done after we've aborted our queue, or aha_cmd fails */
 1420         if (aha->init_level > 4)
 1421                 ahainitmboxes(aha);
 1422 
 1423         return (0);
 1424 }
 1425 
 1426 /*
 1427  * Send a command to the adapter.
 1428  */
 1429 int
 1430 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
 1431         u_int param_len, uint8_t *reply_data, u_int reply_len,
 1432         u_int cmd_timeout)
 1433 {
 1434         u_int   timeout;
 1435         u_int   status;
 1436         u_int   saved_status;
 1437         u_int   intstat;
 1438         u_int   reply_buf_size;
 1439         int     s;
 1440         int     cmd_complete;
 1441         int     error;
 1442 
 1443         /* No data returned to start */
 1444         reply_buf_size = reply_len;
 1445         reply_len = 0;
 1446         intstat = 0;
 1447         cmd_complete = 0;
 1448         saved_status = 0;
 1449         error = 0;
 1450 
 1451         /*
 1452          * All commands except for the "start mailbox" and the "enable
 1453          * outgoing mailbox read interrupt" commands cannot be issued
 1454          * while there are pending transactions.  Freeze our SIMQ
 1455          * and wait for all completions to occur if necessary.
 1456          */
 1457         timeout = 10000;
 1458         s = splcam();
 1459         while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
 1460                 /* Fire the interrupt handler in case interrupts are blocked */
 1461                 aha_intr(aha);
 1462                 splx(s);
 1463                 DELAY(10);
 1464                 s = splcam();
 1465         }
 1466         splx(s);
 1467 
 1468         if (timeout == 0) {
 1469                 device_printf(aha->dev, 
 1470                     "aha_cmd: Timeout waiting for adapter idle\n");
 1471                 return (ETIMEDOUT);
 1472         }
 1473         aha->command_cmp = 0;
 1474         /*
 1475          * Wait up to 10 sec. for the adapter to become
 1476          * ready to accept commands.
 1477          */
 1478         timeout = 100000;
 1479         while (--timeout) {
 1480                 status = aha_inb(aha, STATUS_REG);
 1481                 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
 1482                         break;
 1483                 /*
 1484                  * Throw away any pending data which may be
 1485                  * left over from earlier commands that we
 1486                  * timedout on.
 1487                  */
 1488                 if ((status & DATAIN_REG_READY) != 0)
 1489                         (void)aha_inb(aha, DATAIN_REG);
 1490                 DELAY(100);
 1491         }
 1492         if (timeout == 0) {
 1493                 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
 1494                     " ready, status = 0x%x\n", status);
 1495                 return (ETIMEDOUT);
 1496         }
 1497 
 1498         /*
 1499          * Send the opcode followed by any necessary parameter bytes.
 1500          */
 1501         aha_outb(aha, COMMAND_REG, opcode);
 1502 
 1503         /*
 1504          * Wait for up to 1sec to get the parameter list sent
 1505          */
 1506         timeout = 10000;
 1507         while (param_len && --timeout) {
 1508                 DELAY(100);
 1509                 s = splcam();
 1510                 status = aha_inb(aha, STATUS_REG);
 1511                 intstat = aha_inb(aha, INTSTAT_REG);
 1512                 splx(s);
 1513 
 1514                 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
 1515                  == (INTR_PENDING|CMD_COMPLETE)) {
 1516                         saved_status = status;
 1517                         cmd_complete = 1;
 1518                         break;
 1519                 }
 1520 
 1521                 if (aha->command_cmp != 0) {
 1522                         saved_status = aha->latched_status;
 1523                         cmd_complete = 1;
 1524                         break;
 1525                 }
 1526                 if ((status & DATAIN_REG_READY) != 0)
 1527                         break;
 1528                 if ((status & CMD_REG_BUSY) == 0) {
 1529                         aha_outb(aha, COMMAND_REG, *params++);
 1530                         param_len--;
 1531                         timeout = 10000;
 1532                 }
 1533         }
 1534         if (timeout == 0) {
 1535                 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
 1536                     "status = 0x%x\n", status);
 1537                 error = ETIMEDOUT;
 1538         }
 1539 
 1540         /*
 1541          * For all other commands, we wait for any output data
 1542          * and the final comand completion interrupt.
 1543          */
 1544         while (cmd_complete == 0 && --cmd_timeout) {
 1545 
 1546                 s = splcam();
 1547                 status = aha_inb(aha, STATUS_REG);
 1548                 intstat = aha_inb(aha, INTSTAT_REG);
 1549                 splx(s);
 1550 
 1551                 if (aha->command_cmp != 0) {
 1552                         cmd_complete = 1;
 1553                         saved_status = aha->latched_status;
 1554                 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
 1555                         == (INTR_PENDING|CMD_COMPLETE)) {
 1556                         /*
 1557                          * Our poll (in case interrupts are blocked)
 1558                          * saw the CMD_COMPLETE interrupt.
 1559                          */
 1560                         cmd_complete = 1;
 1561                         saved_status = status;
 1562                 }
 1563                 if ((status & DATAIN_REG_READY) != 0) {
 1564                         uint8_t data;
 1565 
 1566                         data = aha_inb(aha, DATAIN_REG);
 1567                         if (reply_len < reply_buf_size) {
 1568                                 *reply_data++ = data;
 1569                         } else {
 1570                                 device_printf(aha->dev, 
 1571                                     "aha_cmd - Discarded reply data "
 1572                                     "byte for opcode 0x%x\n", opcode);
 1573                         }
 1574                         /*
 1575                          * Reset timeout to ensure at least a second
 1576                          * between response bytes.
 1577                          */
 1578                         cmd_timeout = MAX(cmd_timeout, 10000);
 1579                         reply_len++;
 1580                 }
 1581                 DELAY(100);
 1582         }
 1583         if (cmd_timeout == 0) {
 1584                 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
 1585                     "intstat = 0x%x, reply_len = %d\n", status, intstat,
 1586                     reply_len);
 1587                 return (ETIMEDOUT);
 1588         }
 1589 
 1590         /*
 1591          * Clear any pending interrupts.  Block interrupts so our
 1592          * interrupt handler is not re-entered.
 1593          */
 1594         s = splcam();
 1595         aha_intr(aha);
 1596         splx(s);
 1597 
 1598         if (error != 0)
 1599                 return (error);
 1600 
 1601         /*
 1602          * If the command was rejected by the controller, tell the caller.
 1603          */
 1604         if ((saved_status & CMD_INVALID) != 0) {
 1605                 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
 1606                 /*
 1607                  * Some early adapters may not recover properly from
 1608                  * an invalid command.  If it appears that the controller
 1609                  * has wedged (i.e. status was not cleared by our interrupt
 1610                  * reset above), perform a soft reset.
 1611                  */
 1612                 DELAY(1000);
 1613                 status = aha_inb(aha, STATUS_REG);
 1614                 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
 1615                               CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
 1616                  || (status & (HA_READY|INIT_REQUIRED))
 1617                   != (HA_READY|INIT_REQUIRED))
 1618                         ahareset(aha, /*hard_reset*/FALSE);
 1619                 return (EINVAL);
 1620         }
 1621 
 1622         if (param_len > 0) {
 1623                 /* The controller did not accept the full argument list */
 1624                 PRVERB((aha->dev, "Controller did not accept full argument "
 1625                     "list (%d > 0)\n", param_len));
 1626                 return (E2BIG);
 1627         }
 1628 
 1629         if (reply_len != reply_buf_size) {
 1630                 /* Too much or too little data received */
 1631                 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
 1632                     reply_len, reply_buf_size));
 1633                 return (EMSGSIZE);
 1634         }
 1635 
 1636         /* We were successful */
 1637         return (0);
 1638 }
 1639 
 1640 static int
 1641 ahainitmboxes(struct aha_softc *aha)
 1642 {
 1643         int error;
 1644         init_24b_mbox_params_t init_mbox;
 1645 
 1646         bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
 1647         bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
 1648         aha->cur_inbox = aha->in_boxes;
 1649         aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
 1650         aha->cur_outbox = aha->out_boxes;
 1651         aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
 1652 
 1653         /* Tell the adapter about them */
 1654         init_mbox.num_mboxes = aha->num_boxes;
 1655         ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
 1656         error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
 1657             /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
 1658             /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
 1659 
 1660         if (error != 0)
 1661                 printf("ahainitmboxes: Initialization command failed\n");
 1662         return (error);
 1663 }
 1664 
 1665 /*
 1666  * Update the XPT's idea of the negotiated transfer
 1667  * parameters for a particular target.
 1668  */
 1669 static void
 1670 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
 1671 {
 1672         setup_data_t    setup_info;
 1673         u_int           target;
 1674         u_int           targ_offset;
 1675         u_int           sync_period;
 1676         int             error;
 1677         uint8_t param;
 1678         targ_syncinfo_t sync_info;
 1679 
 1680         target = cts->ccb_h.target_id;
 1681         targ_offset = (target & 0x7);
 1682 
 1683         /*
 1684          * Inquire Setup Information.  This command retreives
 1685          * the sync info for older models.
 1686          */
 1687         param = sizeof(setup_info);
 1688         error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
 1689             (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
 1690 
 1691         if (error != 0) {
 1692                 device_printf(aha->dev,
 1693                     "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
 1694                     error);
 1695                 return;
 1696         }
 1697 
 1698         sync_info = setup_info.syncinfo[targ_offset];
 1699 
 1700         if (sync_info.sync == 0)
 1701                 cts->sync_offset = 0;
 1702         else
 1703                 cts->sync_offset = sync_info.offset;
 1704 
 1705         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
 1706 
 1707         if (aha->boardid >= BOARD_1542CF)
 1708                 sync_period = 1000;
 1709         else
 1710                 sync_period = 2000;
 1711         sync_period += 500 * sync_info.period;
 1712 
 1713         /* Convert ns value to standard SCSI sync rate */
 1714         if (cts->sync_offset != 0)
 1715                 cts->sync_period = scsi_calc_syncparam(sync_period);
 1716         else
 1717                 cts->sync_period = 0;
 1718 
 1719         cts->valid = CCB_TRANS_SYNC_RATE_VALID
 1720                    | CCB_TRANS_SYNC_OFFSET_VALID
 1721                    | CCB_TRANS_BUS_WIDTH_VALID;
 1722         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
 1723 }
 1724 
 1725 static void
 1726 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1727 {
 1728         struct aha_softc* aha;
 1729 
 1730         aha = (struct aha_softc*)arg;
 1731         aha->mailbox_physbase = segs->ds_addr;
 1732 }
 1733 
 1734 static void
 1735 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1736 {
 1737         struct aha_softc* aha;
 1738 
 1739         aha = (struct aha_softc*)arg;
 1740         aha->aha_ccb_physbase = segs->ds_addr;
 1741 }
 1742 
 1743 static void
 1744 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1745 {
 1746 
 1747         struct aha_softc* aha;
 1748 
 1749         aha = (struct aha_softc*)arg;
 1750         SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
 1751 }
 1752 
 1753 static void
 1754 ahapoll(struct cam_sim *sim)
 1755 {
 1756         aha_intr(cam_sim_softc(sim));
 1757 }
 1758 
 1759 static void
 1760 ahatimeout(void *arg)
 1761 {
 1762         struct aha_ccb  *accb;
 1763         union  ccb      *ccb;
 1764         struct aha_softc *aha;
 1765         int              s;
 1766         uint32_t        paddr;
 1767         struct ccb_hdr *ccb_h;
 1768 
 1769         accb = (struct aha_ccb *)arg;
 1770         ccb = accb->ccb;
 1771         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
 1772         xpt_print_path(ccb->ccb_h.path);
 1773         printf("CCB %p - timed out\n", (void *)accb);
 1774 
 1775         s = splcam();
 1776 
 1777         if ((accb->flags & ACCB_ACTIVE) == 0) {
 1778                 xpt_print_path(ccb->ccb_h.path);
 1779                 printf("CCB %p - timed out CCB already completed\n",
 1780                     (void *)accb);
 1781                 splx(s);
 1782                 return;
 1783         }
 1784 
 1785         /*
 1786          * In order to simplify the recovery process, we ask the XPT
 1787          * layer to halt the queue of new transactions and we traverse
 1788          * the list of pending CCBs and remove their timeouts. This
 1789          * means that the driver attempts to clear only one error
 1790          * condition at a time.  In general, timeouts that occur
 1791          * close together are related anyway, so there is no benefit
 1792          * in attempting to handle errors in parrallel.  Timeouts will
 1793          * be reinstated when the recovery process ends.
 1794          */
 1795         if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
 1796                 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
 1797                         xpt_freeze_simq(aha->sim, /*count*/1);
 1798                         accb->flags |= ACCB_RELEASE_SIMQ;
 1799                 }
 1800 
 1801                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
 1802                 while (ccb_h != NULL) {
 1803                         struct aha_ccb *pending_accb;
 1804 
 1805                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
 1806                         untimeout(ahatimeout, pending_accb, ccb_h->timeout_ch);
 1807                         ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 1808                 }
 1809         }
 1810 
 1811         if ((accb->flags & ACCB_DEVICE_RESET) != 0
 1812          || aha->cur_outbox->action_code != AMBO_FREE) {
 1813                 /*
 1814                  * Try a full host adapter/SCSI bus reset.
 1815                  * We do this only if we have already attempted
 1816                  * to clear the condition with a BDR, or we cannot
 1817                  * attempt a BDR for lack of mailbox resources.
 1818                  */
 1819                 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
 1820                 ahareset(aha, /*hardreset*/TRUE);
 1821                 device_printf(aha->dev, "No longer in timeout\n");
 1822         } else {
 1823                 /*
 1824                  * Send a Bus Device Reset message:
 1825                  * The target that is holding up the bus may not
 1826                  * be the same as the one that triggered this timeout
 1827                  * (different commands have different timeout lengths),
 1828                  * but we have no way of determining this from our
 1829                  * timeout handler.  Our strategy here is to queue a
 1830                  * BDR message to the target of the timed out command.
 1831                  * If this fails, we'll get another timeout 2 seconds
 1832                  * later which will attempt a bus reset.
 1833                  */
 1834                 accb->flags |= ACCB_DEVICE_RESET;
 1835                 ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb, 2 * hz);
 1836                 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
 1837 
 1838                 /* No Data Transfer */
 1839                 aha->recovery_accb->hccb.datain = TRUE;
 1840                 aha->recovery_accb->hccb.dataout = TRUE;
 1841                 aha->recovery_accb->hccb.ahastat = 0;
 1842                 aha->recovery_accb->hccb.sdstat = 0;
 1843                 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
 1844 
 1845                 /* Tell the adapter about this command */
 1846                 paddr = ahaccbvtop(aha, aha->recovery_accb);
 1847                 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
 1848                 aha->cur_outbox->action_code = AMBO_START;
 1849                 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
 1850                 ahanextoutbox(aha);
 1851         }
 1852 
 1853         splx(s);
 1854 }
 1855 
 1856 int
 1857 aha_detach(struct aha_softc *aha)
 1858 {
 1859         xpt_async(AC_LOST_DEVICE, aha->path, NULL);
 1860         xpt_free_path(aha->path);
 1861         xpt_bus_deregister(cam_sim_path(aha->sim));
 1862         cam_sim_free(aha->sim, /*free_devq*/TRUE);
 1863         return (0);
 1864 }

Cache object: 1bfa4b39f64409609dac929f155083fc


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