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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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.0/sys/dev/aha/aha.c 146734 2005-05-29 04:42:30Z nyan $");
   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/mutex.h>
   70 
   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 compensation failed\n");
 1290                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
 1291                         csio->ccb_h.status = CAM_REQUEUE_REQ;
 1292                         break;
 1293                 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
 1294                         /* We don't even support linked commands... */
 1295                         panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
 1296                         break;
 1297                 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
 1298                         panic("%s: Invalid CCB or SG list", aha_name(aha));
 1299                         break;
 1300                 case AHASTAT_HA_SCSI_BUS_RESET:
 1301                         if ((csio->ccb_h.status & CAM_STATUS_MASK)
 1302                             != CAM_CMD_TIMEOUT)
 1303                                 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
 1304                         break;
 1305                 case AHASTAT_HA_BDR:
 1306                         if ((accb->flags & ACCB_DEVICE_RESET) == 0)
 1307                                 csio->ccb_h.status = CAM_BDR_SENT;
 1308                         else
 1309                                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
 1310                         break;
 1311                 }
 1312                 if (csio->ccb_h.status != CAM_REQ_CMP) {
 1313                         xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
 1314                         csio->ccb_h.status |= CAM_DEV_QFRZN;
 1315                 }
 1316                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
 1317                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1318                 ahafreeccb(aha, accb);
 1319                 xpt_done(ccb);
 1320                 break;
 1321         case AMBI_OK:
 1322                 /* All completed without incident */
 1323                 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
 1324                 /* I don't think so since it works???? */
 1325                 ccb->ccb_h.status |= CAM_REQ_CMP;
 1326                 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
 1327                         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1328                 ahafreeccb(aha, accb);
 1329                 xpt_done(ccb);
 1330                 break;
 1331         }
 1332 }
 1333 
 1334 static int
 1335 ahareset(struct aha_softc* aha, int hard_reset)
 1336 {
 1337         struct   ccb_hdr *ccb_h;
 1338         u_int    status;
 1339         u_int    timeout;
 1340         uint8_t reset_type;
 1341 
 1342         if (hard_reset != 0)
 1343                 reset_type = HARD_RESET;
 1344         else
 1345                 reset_type = SOFT_RESET;
 1346         aha_outb(aha, CONTROL_REG, reset_type);
 1347 
 1348         /* Wait 5sec. for Diagnostic start */
 1349         timeout = 5 * 10000;
 1350         while (--timeout) {
 1351                 status = aha_inb(aha, STATUS_REG);
 1352                 if ((status & DIAG_ACTIVE) != 0)
 1353                         break;
 1354                 DELAY(100);
 1355         }
 1356         if (timeout == 0) {
 1357                 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
 1358                     "assert. status = %#x\n", status));
 1359                 return (ETIMEDOUT);
 1360         }
 1361 
 1362         /* Wait 10sec. for Diagnostic end */
 1363         timeout = 10 * 10000;
 1364         while (--timeout) {
 1365                 status = aha_inb(aha, STATUS_REG);
 1366                 if ((status & DIAG_ACTIVE) == 0)
 1367                         break;
 1368                 DELAY(100);
 1369         }
 1370         if (timeout == 0) {
 1371                 panic("%s: ahareset - Diagnostic Active failed to drop. "
 1372                     "status = 0x%x\n", aha_name(aha), status);
 1373                 return (ETIMEDOUT);
 1374         }
 1375 
 1376         /* Wait for the host adapter to become ready or report a failure */
 1377         timeout = 10000;
 1378         while (--timeout) {
 1379                 status = aha_inb(aha, STATUS_REG);
 1380                 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
 1381                         break;
 1382                 DELAY(100);
 1383         }
 1384         if (timeout == 0) {
 1385                 device_printf(aha->dev, "ahareset - Host adapter failed to "
 1386                     "come ready. status = 0x%x\n", status);
 1387                 return (ETIMEDOUT);
 1388         }
 1389 
 1390         /* If the diagnostics failed, tell the user */
 1391         if ((status & DIAG_FAIL) != 0
 1392          || (status & HA_READY) == 0) {
 1393                 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
 1394 
 1395                 if ((status & DATAIN_REG_READY) != 0)
 1396                         device_printf(aha->dev, "ahareset - Host Adapter "
 1397                             "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
 1398                 return (ENXIO);
 1399         }
 1400 
 1401         /* If we've attached to the XPT, tell it about the event */
 1402         if (aha->path != NULL)
 1403                 xpt_async(AC_BUS_RESET, aha->path, NULL);
 1404 
 1405         /*
 1406          * Perform completion processing for all outstanding CCBs.
 1407          */
 1408         while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
 1409                 struct aha_ccb *pending_accb;
 1410 
 1411                 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
 1412                 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
 1413                 ahadone(aha, pending_accb, AMBI_ERROR);
 1414         }
 1415 
 1416         /* If we've allocated mailboxes, initialize them */
 1417         /* Must be done after we've aborted our queue, or aha_cmd fails */
 1418         if (aha->init_level > 4)
 1419                 ahainitmboxes(aha);
 1420 
 1421         return (0);
 1422 }
 1423 
 1424 /*
 1425  * Send a command to the adapter.
 1426  */
 1427 int
 1428 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
 1429         u_int param_len, uint8_t *reply_data, u_int reply_len,
 1430         u_int cmd_timeout)
 1431 {
 1432         u_int   timeout;
 1433         u_int   status;
 1434         u_int   saved_status;
 1435         u_int   intstat;
 1436         u_int   reply_buf_size;
 1437         int     s;
 1438         int     cmd_complete;
 1439         int     error;
 1440 
 1441         /* No data returned to start */
 1442         reply_buf_size = reply_len;
 1443         reply_len = 0;
 1444         intstat = 0;
 1445         cmd_complete = 0;
 1446         saved_status = 0;
 1447         error = 0;
 1448 
 1449         /*
 1450          * All commands except for the "start mailbox" and the "enable
 1451          * outgoing mailbox read interrupt" commands cannot be issued
 1452          * while there are pending transactions.  Freeze our SIMQ
 1453          * and wait for all completions to occur if necessary.
 1454          */
 1455         timeout = 10000;
 1456         s = splcam();
 1457         while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
 1458                 /* Fire the interrupt handler in case interrupts are blocked */
 1459                 aha_intr(aha);
 1460                 splx(s);
 1461                 DELAY(10);
 1462                 s = splcam();
 1463         }
 1464         splx(s);
 1465 
 1466         if (timeout == 0) {
 1467                 device_printf(aha->dev, 
 1468                     "aha_cmd: Timeout waiting for adapter idle\n");
 1469                 return (ETIMEDOUT);
 1470         }
 1471         aha->command_cmp = 0;
 1472         /*
 1473          * Wait up to 10 sec. for the adapter to become
 1474          * ready to accept commands.
 1475          */
 1476         timeout = 100000;
 1477         while (--timeout) {
 1478                 status = aha_inb(aha, STATUS_REG);
 1479                 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
 1480                         break;
 1481                 /*
 1482                  * Throw away any pending data which may be
 1483                  * left over from earlier commands that we
 1484                  * timedout on.
 1485                  */
 1486                 if ((status & DATAIN_REG_READY) != 0)
 1487                         (void)aha_inb(aha, DATAIN_REG);
 1488                 DELAY(100);
 1489         }
 1490         if (timeout == 0) {
 1491                 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
 1492                     " ready, status = 0x%x\n", status);
 1493                 return (ETIMEDOUT);
 1494         }
 1495 
 1496         /*
 1497          * Send the opcode followed by any necessary parameter bytes.
 1498          */
 1499         aha_outb(aha, COMMAND_REG, opcode);
 1500 
 1501         /*
 1502          * Wait for up to 1sec to get the parameter list sent
 1503          */
 1504         timeout = 10000;
 1505         while (param_len && --timeout) {
 1506                 DELAY(100);
 1507                 s = splcam();
 1508                 status = aha_inb(aha, STATUS_REG);
 1509                 intstat = aha_inb(aha, INTSTAT_REG);
 1510                 splx(s);
 1511 
 1512                 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
 1513                  == (INTR_PENDING|CMD_COMPLETE)) {
 1514                         saved_status = status;
 1515                         cmd_complete = 1;
 1516                         break;
 1517                 }
 1518 
 1519                 if (aha->command_cmp != 0) {
 1520                         saved_status = aha->latched_status;
 1521                         cmd_complete = 1;
 1522                         break;
 1523                 }
 1524                 if ((status & DATAIN_REG_READY) != 0)
 1525                         break;
 1526                 if ((status & CMD_REG_BUSY) == 0) {
 1527                         aha_outb(aha, COMMAND_REG, *params++);
 1528                         param_len--;
 1529                         timeout = 10000;
 1530                 }
 1531         }
 1532         if (timeout == 0) {
 1533                 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
 1534                     "status = 0x%x\n", status);
 1535                 error = ETIMEDOUT;
 1536         }
 1537 
 1538         /*
 1539          * For all other commands, we wait for any output data
 1540          * and the final comand completion interrupt.
 1541          */
 1542         while (cmd_complete == 0 && --cmd_timeout) {
 1543 
 1544                 s = splcam();
 1545                 status = aha_inb(aha, STATUS_REG);
 1546                 intstat = aha_inb(aha, INTSTAT_REG);
 1547                 splx(s);
 1548 
 1549                 if (aha->command_cmp != 0) {
 1550                         cmd_complete = 1;
 1551                         saved_status = aha->latched_status;
 1552                 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
 1553                         == (INTR_PENDING|CMD_COMPLETE)) {
 1554                         /*
 1555                          * Our poll (in case interrupts are blocked)
 1556                          * saw the CMD_COMPLETE interrupt.
 1557                          */
 1558                         cmd_complete = 1;
 1559                         saved_status = status;
 1560                 }
 1561                 if ((status & DATAIN_REG_READY) != 0) {
 1562                         uint8_t data;
 1563 
 1564                         data = aha_inb(aha, DATAIN_REG);
 1565                         if (reply_len < reply_buf_size) {
 1566                                 *reply_data++ = data;
 1567                         } else {
 1568                                 device_printf(aha->dev, 
 1569                                     "aha_cmd - Discarded reply data "
 1570                                     "byte for opcode 0x%x\n", opcode);
 1571                         }
 1572                         /*
 1573                          * Reset timeout to ensure at least a second
 1574                          * between response bytes.
 1575                          */
 1576                         cmd_timeout = MAX(cmd_timeout, 10000);
 1577                         reply_len++;
 1578                 }
 1579                 DELAY(100);
 1580         }
 1581         if (cmd_timeout == 0) {
 1582                 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
 1583                     "intstat = 0x%x, reply_len = %d\n", status, intstat,
 1584                     reply_len);
 1585                 return (ETIMEDOUT);
 1586         }
 1587 
 1588         /*
 1589          * Clear any pending interrupts.  Block interrupts so our
 1590          * interrupt handler is not re-entered.
 1591          */
 1592         s = splcam();
 1593         aha_intr(aha);
 1594         splx(s);
 1595 
 1596         if (error != 0)
 1597                 return (error);
 1598 
 1599         /*
 1600          * If the command was rejected by the controller, tell the caller.
 1601          */
 1602         if ((saved_status & CMD_INVALID) != 0) {
 1603                 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
 1604                 /*
 1605                  * Some early adapters may not recover properly from
 1606                  * an invalid command.  If it appears that the controller
 1607                  * has wedged (i.e. status was not cleared by our interrupt
 1608                  * reset above), perform a soft reset.
 1609                  */
 1610                 DELAY(1000);
 1611                 status = aha_inb(aha, STATUS_REG);
 1612                 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
 1613                               CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
 1614                  || (status & (HA_READY|INIT_REQUIRED))
 1615                   != (HA_READY|INIT_REQUIRED))
 1616                         ahareset(aha, /*hard_reset*/FALSE);
 1617                 return (EINVAL);
 1618         }
 1619 
 1620         if (param_len > 0) {
 1621                 /* The controller did not accept the full argument list */
 1622                 PRVERB((aha->dev, "Controller did not accept full argument "
 1623                     "list (%d > 0)\n", param_len));
 1624                 return (E2BIG);
 1625         }
 1626 
 1627         if (reply_len != reply_buf_size) {
 1628                 /* Too much or too little data received */
 1629                 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
 1630                     reply_len, reply_buf_size));
 1631                 return (EMSGSIZE);
 1632         }
 1633 
 1634         /* We were successful */
 1635         return (0);
 1636 }
 1637 
 1638 static int
 1639 ahainitmboxes(struct aha_softc *aha)
 1640 {
 1641         int error;
 1642         init_24b_mbox_params_t init_mbox;
 1643 
 1644         bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
 1645         bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
 1646         aha->cur_inbox = aha->in_boxes;
 1647         aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
 1648         aha->cur_outbox = aha->out_boxes;
 1649         aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
 1650 
 1651         /* Tell the adapter about them */
 1652         init_mbox.num_mboxes = aha->num_boxes;
 1653         ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
 1654         error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
 1655             /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
 1656             /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
 1657 
 1658         if (error != 0)
 1659                 printf("ahainitmboxes: Initialization command failed\n");
 1660         return (error);
 1661 }
 1662 
 1663 /*
 1664  * Update the XPT's idea of the negotiated transfer
 1665  * parameters for a particular target.
 1666  */
 1667 static void
 1668 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
 1669 {
 1670         setup_data_t    setup_info;
 1671         u_int           target;
 1672         u_int           targ_offset;
 1673         u_int           sync_period;
 1674         int             error;
 1675         uint8_t param;
 1676         targ_syncinfo_t sync_info;
 1677 
 1678         target = cts->ccb_h.target_id;
 1679         targ_offset = (target & 0x7);
 1680 
 1681         /*
 1682          * Inquire Setup Information.  This command retreives
 1683          * the sync info for older models.
 1684          */
 1685         param = sizeof(setup_info);
 1686         error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
 1687             (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
 1688 
 1689         if (error != 0) {
 1690                 device_printf(aha->dev,
 1691                     "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
 1692                     error);
 1693                 return;
 1694         }
 1695 
 1696         sync_info = setup_info.syncinfo[targ_offset];
 1697 
 1698         if (sync_info.sync == 0)
 1699                 cts->sync_offset = 0;
 1700         else
 1701                 cts->sync_offset = sync_info.offset;
 1702 
 1703         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
 1704 
 1705         if (aha->boardid >= BOARD_1542CF)
 1706                 sync_period = 1000;
 1707         else
 1708                 sync_period = 2000;
 1709         sync_period += 500 * sync_info.period;
 1710 
 1711         /* Convert ns value to standard SCSI sync rate */
 1712         if (cts->sync_offset != 0)
 1713                 cts->sync_period = scsi_calc_syncparam(sync_period);
 1714         else
 1715                 cts->sync_period = 0;
 1716 
 1717         cts->valid = CCB_TRANS_SYNC_RATE_VALID
 1718                    | CCB_TRANS_SYNC_OFFSET_VALID
 1719                    | CCB_TRANS_BUS_WIDTH_VALID;
 1720         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
 1721 }
 1722 
 1723 static void
 1724 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1725 {
 1726         struct aha_softc* aha;
 1727 
 1728         aha = (struct aha_softc*)arg;
 1729         aha->mailbox_physbase = segs->ds_addr;
 1730 }
 1731 
 1732 static void
 1733 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1734 {
 1735         struct aha_softc* aha;
 1736 
 1737         aha = (struct aha_softc*)arg;
 1738         aha->aha_ccb_physbase = segs->ds_addr;
 1739 }
 1740 
 1741 static void
 1742 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1743 {
 1744 
 1745         struct aha_softc* aha;
 1746 
 1747         aha = (struct aha_softc*)arg;
 1748         SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
 1749 }
 1750 
 1751 static void
 1752 ahapoll(struct cam_sim *sim)
 1753 {
 1754         aha_intr(cam_sim_softc(sim));
 1755 }
 1756 
 1757 static void
 1758 ahatimeout(void *arg)
 1759 {
 1760         struct aha_ccb  *accb;
 1761         union  ccb      *ccb;
 1762         struct aha_softc *aha;
 1763         int              s;
 1764         uint32_t        paddr;
 1765         struct ccb_hdr *ccb_h;
 1766 
 1767         accb = (struct aha_ccb *)arg;
 1768         ccb = accb->ccb;
 1769         aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
 1770         xpt_print_path(ccb->ccb_h.path);
 1771         printf("CCB %p - timed out\n", (void *)accb);
 1772 
 1773         s = splcam();
 1774 
 1775         if ((accb->flags & ACCB_ACTIVE) == 0) {
 1776                 xpt_print_path(ccb->ccb_h.path);
 1777                 printf("CCB %p - timed out CCB already completed\n",
 1778                     (void *)accb);
 1779                 splx(s);
 1780                 return;
 1781         }
 1782 
 1783         /*
 1784          * In order to simplify the recovery process, we ask the XPT
 1785          * layer to halt the queue of new transactions and we traverse
 1786          * the list of pending CCBs and remove their timeouts. This
 1787          * means that the driver attempts to clear only one error
 1788          * condition at a time.  In general, timeouts that occur
 1789          * close together are related anyway, so there is no benefit
 1790          * in attempting to handle errors in parrallel.  Timeouts will
 1791          * be reinstated when the recovery process ends.
 1792          */
 1793         if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
 1794                 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
 1795                         xpt_freeze_simq(aha->sim, /*count*/1);
 1796                         accb->flags |= ACCB_RELEASE_SIMQ;
 1797                 }
 1798 
 1799                 ccb_h = LIST_FIRST(&aha->pending_ccbs);
 1800                 while (ccb_h != NULL) {
 1801                         struct aha_ccb *pending_accb;
 1802 
 1803                         pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
 1804                         untimeout(ahatimeout, pending_accb, ccb_h->timeout_ch);
 1805                         ccb_h = LIST_NEXT(ccb_h, sim_links.le);
 1806                 }
 1807         }
 1808 
 1809         if ((accb->flags & ACCB_DEVICE_RESET) != 0
 1810          || aha->cur_outbox->action_code != AMBO_FREE) {
 1811                 /*
 1812                  * Try a full host adapter/SCSI bus reset.
 1813                  * We do this only if we have already attempted
 1814                  * to clear the condition with a BDR, or we cannot
 1815                  * attempt a BDR for lack of mailbox resources.
 1816                  */
 1817                 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
 1818                 ahareset(aha, /*hardreset*/TRUE);
 1819                 device_printf(aha->dev, "No longer in timeout\n");
 1820         } else {
 1821                 /*
 1822                  * Send a Bus Device Reset message:
 1823                  * The target that is holding up the bus may not
 1824                  * be the same as the one that triggered this timeout
 1825                  * (different commands have different timeout lengths),
 1826                  * but we have no way of determining this from our
 1827                  * timeout handler.  Our strategy here is to queue a
 1828                  * BDR message to the target of the timed out command.
 1829                  * If this fails, we'll get another timeout 2 seconds
 1830                  * later which will attempt a bus reset.
 1831                  */
 1832                 accb->flags |= ACCB_DEVICE_RESET;
 1833                 ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb, 2 * hz);
 1834                 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
 1835 
 1836                 /* No Data Transfer */
 1837                 aha->recovery_accb->hccb.datain = TRUE;
 1838                 aha->recovery_accb->hccb.dataout = TRUE;
 1839                 aha->recovery_accb->hccb.ahastat = 0;
 1840                 aha->recovery_accb->hccb.sdstat = 0;
 1841                 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
 1842 
 1843                 /* Tell the adapter about this command */
 1844                 paddr = ahaccbvtop(aha, aha->recovery_accb);
 1845                 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
 1846                 aha->cur_outbox->action_code = AMBO_START;
 1847                 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
 1848                 ahanextoutbox(aha);
 1849         }
 1850 
 1851         splx(s);
 1852 }
 1853 
 1854 int
 1855 aha_detach(struct aha_softc *aha)
 1856 {
 1857         xpt_async(AC_LOST_DEVICE, aha->path, NULL);
 1858         xpt_free_path(aha->path);
 1859         xpt_bus_deregister(cam_sim_path(aha->sim));
 1860         cam_sim_free(aha->sim, /*free_devq*/TRUE);
 1861         return (0);
 1862 }

Cache object: c2bd8133f241cc19dcecf0260c5e825e


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