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

Cache object: eb4c7bbdf53489f4cc8eb1d1a7b4f332


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