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/aic7xxx/aic7xxx_osm.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  * Bus independent FreeBSD shim for the aic7xxx based Adaptec SCSI controllers
    3  *
    4  * Copyright (c) 1994-2001 Justin T. Gibbs.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions, and the following disclaimer,
   12  *    without modification.
   13  * 2. The name of the author may not be used to endorse or promote products
   14  *    derived from this software without specific prior written permission.
   15  *
   16  * Alternatively, this software may be distributed under the terms of the
   17  * GNU Public License ("GPL").
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.c#20 $
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/8.4/sys/dev/aic7xxx/aic7xxx_osm.c 170872 2007-06-17 05:55:54Z scottl $");
   36 
   37 #include <dev/aic7xxx/aic7xxx_osm.h>
   38 #include <dev/aic7xxx/aic7xxx_inline.h>
   39 
   40 #include <sys/kthread.h>
   41 
   42 #ifndef AHC_TMODE_ENABLE
   43 #define AHC_TMODE_ENABLE 0
   44 #endif
   45 
   46 #include <dev/aic7xxx/aic_osm_lib.c>
   47 
   48 #define ccb_scb_ptr spriv_ptr0
   49 
   50 devclass_t ahc_devclass;
   51 
   52 #if 0
   53 static void     ahc_dump_targcmd(struct target_cmd *cmd);
   54 #endif
   55 static int      ahc_modevent(module_t mod, int type, void *data);
   56 static void     ahc_action(struct cam_sim *sim, union ccb *ccb);
   57 static void     ahc_get_tran_settings(struct ahc_softc *ahc,
   58                                       int our_id, char channel,
   59                                       struct ccb_trans_settings *cts);
   60 static void     ahc_async(void *callback_arg, uint32_t code,
   61                           struct cam_path *path, void *arg);
   62 static void     ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
   63                                 int nsegments, int error);
   64 static void     ahc_poll(struct cam_sim *sim);
   65 static void     ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
   66                                struct ccb_scsiio *csio, struct scb *scb);
   67 static void     ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
   68                               union ccb *ccb);
   69 static int      ahc_create_path(struct ahc_softc *ahc,
   70                                 char channel, u_int target, u_int lun,
   71                                 struct cam_path **path);
   72 
   73 
   74 static int
   75 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
   76                 u_int lun, struct cam_path **path)
   77 {
   78         path_id_t path_id;
   79 
   80         if (channel == 'B')
   81                 path_id = cam_sim_path(ahc->platform_data->sim_b);
   82         else 
   83                 path_id = cam_sim_path(ahc->platform_data->sim);
   84 
   85         return (xpt_create_path(path, /*periph*/NULL,
   86                                 path_id, target, lun));
   87 }
   88 
   89 int
   90 ahc_map_int(struct ahc_softc *ahc)
   91 {
   92         int error;
   93         int zero;
   94         int shareable;
   95 
   96         zero = 0;
   97         shareable = (ahc->flags & AHC_EDGE_INTERRUPT) ? 0: RF_SHAREABLE;
   98         ahc->platform_data->irq =
   99             bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IRQ, &zero,
  100                                    RF_ACTIVE | shareable);
  101         if (ahc->platform_data->irq == NULL) {
  102                 device_printf(ahc->dev_softc,
  103                               "bus_alloc_resource() failed to allocate IRQ\n");
  104                 return (ENOMEM);
  105         }
  106         ahc->platform_data->irq_res_type = SYS_RES_IRQ;
  107 
  108         /* Hook up our interrupt handler */
  109         error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
  110                                INTR_TYPE_CAM|INTR_MPSAFE, NULL, 
  111                                ahc_platform_intr, ahc, &ahc->platform_data->ih);
  112 
  113         if (error != 0)
  114                 device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
  115                               error);
  116         return (error);
  117 }
  118 
  119 int
  120 aic7770_map_registers(struct ahc_softc *ahc, u_int unused_ioport_arg)
  121 {
  122         struct  resource *regs;
  123         int     rid;
  124 
  125         rid = 0;
  126         regs = bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IOPORT, &rid,
  127                                       RF_ACTIVE);
  128         if (regs == NULL) {
  129                 device_printf(ahc->dev_softc, "Unable to map I/O space?!\n");
  130                 return ENOMEM;
  131         }
  132         ahc->platform_data->regs_res_type = SYS_RES_IOPORT;
  133         ahc->platform_data->regs_res_id = rid,
  134         ahc->platform_data->regs = regs;
  135         ahc->tag = rman_get_bustag(regs);
  136         ahc->bsh = rman_get_bushandle(regs);
  137         return (0);
  138 }
  139 
  140 /*
  141  * Attach all the sub-devices we can find
  142  */
  143 int
  144 ahc_attach(struct ahc_softc *ahc)
  145 {
  146         char   ahc_info[256];
  147         struct ccb_setasync csa;
  148         struct cam_devq *devq;
  149         int bus_id;
  150         int bus_id2;
  151         struct cam_sim *sim;
  152         struct cam_sim *sim2;
  153         struct cam_path *path;
  154         struct cam_path *path2;
  155         int count;
  156 
  157         count = 0;
  158         sim = NULL;
  159         sim2 = NULL;
  160         path = NULL;
  161         path2 = NULL;
  162 
  163 
  164         /*
  165          * Create a thread to perform all recovery.
  166          */
  167         if (ahc_spawn_recovery_thread(ahc) != 0)
  168                 goto fail;
  169 
  170         ahc_controller_info(ahc, ahc_info);
  171         printf("%s\n", ahc_info);
  172         ahc_lock(ahc);
  173 
  174         /*
  175          * Attach secondary channel first if the user has
  176          * declared it the primary channel.
  177          */
  178         if ((ahc->features & AHC_TWIN) != 0
  179          && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
  180                 bus_id = 1;
  181                 bus_id2 = 0;
  182         } else {
  183                 bus_id = 0;
  184                 bus_id2 = 1;
  185         }
  186 
  187         /*
  188          * Create the device queue for our SIM(s).
  189          */
  190         devq = cam_simq_alloc(AHC_MAX_QUEUE);
  191         if (devq == NULL)
  192                 goto fail;
  193 
  194         /*
  195          * Construct our first channel SIM entry
  196          */
  197         sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
  198                             device_get_unit(ahc->dev_softc),
  199                             &ahc->platform_data->mtx, 1, AHC_MAX_QUEUE, devq);
  200         if (sim == NULL) {
  201                 cam_simq_free(devq);
  202                 goto fail;
  203         }
  204 
  205         if (xpt_bus_register(sim, ahc->dev_softc, bus_id) != CAM_SUCCESS) {
  206                 cam_sim_free(sim, /*free_devq*/TRUE);
  207                 sim = NULL;
  208                 goto fail;
  209         }
  210         
  211         if (xpt_create_path(&path, /*periph*/NULL,
  212                             cam_sim_path(sim), CAM_TARGET_WILDCARD,
  213                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  214                 xpt_bus_deregister(cam_sim_path(sim));
  215                 cam_sim_free(sim, /*free_devq*/TRUE);
  216                 sim = NULL;
  217                 goto fail;
  218         }
  219                 
  220         xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
  221         csa.ccb_h.func_code = XPT_SASYNC_CB;
  222         csa.event_enable = AC_LOST_DEVICE;
  223         csa.callback = ahc_async;
  224         csa.callback_arg = sim;
  225         xpt_action((union ccb *)&csa);
  226         count++;
  227 
  228         if (ahc->features & AHC_TWIN) {
  229                 sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
  230                                     ahc, device_get_unit(ahc->dev_softc),
  231                                     &ahc->platform_data->mtx, 1,
  232                                     AHC_MAX_QUEUE, devq);
  233 
  234                 if (sim2 == NULL) {
  235                         printf("ahc_attach: Unable to attach second "
  236                                "bus due to resource shortage");
  237                         goto fail;
  238                 }
  239                 
  240                 if (xpt_bus_register(sim2, ahc->dev_softc, bus_id2) !=
  241                     CAM_SUCCESS) {
  242                         printf("ahc_attach: Unable to attach second "
  243                                "bus due to resource shortage");
  244                         /*
  245                          * We do not want to destroy the device queue
  246                          * because the first bus is using it.
  247                          */
  248                         cam_sim_free(sim2, /*free_devq*/FALSE);
  249                         goto fail;
  250                 }
  251 
  252                 if (xpt_create_path(&path2, /*periph*/NULL,
  253                                     cam_sim_path(sim2),
  254                                     CAM_TARGET_WILDCARD,
  255                                     CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  256                         xpt_bus_deregister(cam_sim_path(sim2));
  257                         cam_sim_free(sim2, /*free_devq*/FALSE);
  258                         sim2 = NULL;
  259                         goto fail;
  260                 }
  261                 xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
  262                 csa.ccb_h.func_code = XPT_SASYNC_CB;
  263                 csa.event_enable = AC_LOST_DEVICE;
  264                 csa.callback = ahc_async;
  265                 csa.callback_arg = sim2;
  266                 xpt_action((union ccb *)&csa);
  267                 count++;
  268         }
  269 
  270 fail:
  271         if ((ahc->features & AHC_TWIN) != 0
  272          && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
  273                 ahc->platform_data->sim_b = sim;
  274                 ahc->platform_data->path_b = path;
  275                 ahc->platform_data->sim = sim2;
  276                 ahc->platform_data->path = path2;
  277         } else {
  278                 ahc->platform_data->sim = sim;
  279                 ahc->platform_data->path = path;
  280                 ahc->platform_data->sim_b = sim2;
  281                 ahc->platform_data->path_b = path2;
  282         }
  283         ahc_unlock(ahc);
  284 
  285         if (count != 0) {
  286                 /* We have to wait until after any system dumps... */
  287                 ahc->platform_data->eh =
  288                     EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
  289                                           ahc, SHUTDOWN_PRI_DEFAULT);
  290                 ahc_intr_enable(ahc, TRUE);
  291         }
  292 
  293         return (count);
  294 }
  295 
  296 /*
  297  * Catch an interrupt from the adapter
  298  */
  299 void
  300 ahc_platform_intr(void *arg)
  301 {
  302         struct  ahc_softc *ahc;
  303 
  304         ahc = (struct ahc_softc *)arg; 
  305         ahc_lock(ahc);
  306         ahc_intr(ahc);
  307         ahc_unlock(ahc);
  308 }
  309 
  310 /*
  311  * We have an scb which has been processed by the
  312  * adaptor, now we look to see how the operation
  313  * went.
  314  */
  315 void
  316 ahc_done(struct ahc_softc *ahc, struct scb *scb)
  317 {
  318         union ccb *ccb;
  319 
  320         CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
  321                   ("ahc_done - scb %d\n", scb->hscb->tag));
  322 
  323         ccb = scb->io_ctx;
  324         LIST_REMOVE(scb, pending_links);
  325         if ((scb->flags & SCB_TIMEDOUT) != 0)
  326                 LIST_REMOVE(scb, timedout_links);
  327         if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
  328                 struct scb_tailq *untagged_q;
  329                 int target_offset;
  330 
  331                 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
  332                 untagged_q = &ahc->untagged_queues[target_offset];
  333                 TAILQ_REMOVE(untagged_q, scb, links.tqe);
  334                 scb->flags &= ~SCB_UNTAGGEDQ;
  335                 ahc_run_untagged_queue(ahc, untagged_q);
  336         }
  337 
  338         callout_stop(&scb->io_timer);
  339 
  340         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  341                 bus_dmasync_op_t op;
  342 
  343                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
  344                         op = BUS_DMASYNC_POSTREAD;
  345                 else
  346                         op = BUS_DMASYNC_POSTWRITE;
  347                 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
  348                 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
  349         }
  350 
  351         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
  352                 struct cam_path *ccb_path;
  353 
  354                 /*
  355                  * If we have finally disconnected, clean up our
  356                  * pending device state.
  357                  * XXX - There may be error states that cause where
  358                  *       we will remain connected.
  359                  */
  360                 ccb_path = ccb->ccb_h.path;
  361                 if (ahc->pending_device != NULL
  362                  && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
  363 
  364                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
  365                                 ahc->pending_device = NULL;
  366                         } else {
  367                                 if (bootverbose) {
  368                                         xpt_print_path(ccb->ccb_h.path);
  369                                         printf("Still connected\n");
  370                                 }
  371                                 aic_freeze_ccb(ccb);
  372                         }
  373                 }
  374 
  375                 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
  376                         ccb->ccb_h.status |= CAM_REQ_CMP;
  377                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
  378                 ahc_free_scb(ahc, scb);
  379                 xpt_done(ccb);
  380                 return;
  381         }
  382 
  383         /*
  384          * If the recovery SCB completes, we have to be
  385          * out of our timeout.
  386          */
  387         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
  388                 struct  scb *list_scb;
  389 
  390                 ahc->scb_data->recovery_scbs--;
  391 
  392                 if (aic_get_transaction_status(scb) == CAM_BDR_SENT
  393                  || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
  394                         aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
  395 
  396                 if (ahc->scb_data->recovery_scbs == 0) {
  397                         /*
  398                          * All recovery actions have completed successfully,
  399                          * so reinstate the timeouts for all other pending
  400                          * commands.
  401                          */
  402                         LIST_FOREACH(list_scb, &ahc->pending_scbs,
  403                                      pending_links) {
  404 
  405                                 aic_scb_timer_reset(list_scb,
  406                                                     aic_get_timeout(scb));
  407                         }
  408 
  409                         ahc_print_path(ahc, scb);
  410                         printf("no longer in timeout, status = %x\n",
  411                                ccb->ccb_h.status);
  412                 }
  413         }
  414 
  415         /* Don't clobber any existing error state */
  416         if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
  417                 ccb->ccb_h.status |= CAM_REQ_CMP;
  418         } else if ((scb->flags & SCB_SENSE) != 0) {
  419                 /*
  420                  * We performed autosense retrieval.
  421                  *
  422                  * Zero any sense not transferred by the
  423                  * device.  The SCSI spec mandates that any
  424                  * untransfered data should be assumed to be
  425                  * zero.  Complete the 'bounce' of sense information
  426                  * through buffers accessible via bus-space by
  427                  * copying it into the clients csio.
  428                  */
  429                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
  430                 memcpy(&ccb->csio.sense_data,
  431                        ahc_get_sense_buf(ahc, scb),
  432                        (aic_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
  433                        - ccb->csio.sense_resid);
  434                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
  435         }
  436         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
  437         ahc_free_scb(ahc, scb);
  438         xpt_done(ccb);
  439 }
  440 
  441 static void
  442 ahc_action(struct cam_sim *sim, union ccb *ccb)
  443 {
  444         struct  ahc_softc *ahc;
  445         struct  ahc_tmode_lstate *lstate;
  446         u_int   target_id;
  447         u_int   our_id;
  448 
  449         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
  450         
  451         ahc = (struct ahc_softc *)cam_sim_softc(sim);
  452 
  453         target_id = ccb->ccb_h.target_id;
  454         our_id = SIM_SCSI_ID(ahc, sim);
  455         
  456         switch (ccb->ccb_h.func_code) {
  457         /* Common cases first */
  458         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
  459         case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
  460         {
  461                 struct     ahc_tmode_tstate *tstate;
  462                 cam_status status;
  463 
  464                 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
  465                                              &lstate, TRUE);
  466 
  467                 if (status != CAM_REQ_CMP) {
  468                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
  469                                 /* Response from the black hole device */
  470                                 tstate = NULL;
  471                                 lstate = ahc->black_hole;
  472                         } else {
  473                                 ccb->ccb_h.status = status;
  474                                 xpt_done(ccb);
  475                                 break;
  476                         }
  477                 }
  478                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
  479 
  480                         SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
  481                                           sim_links.sle);
  482                         ccb->ccb_h.status = CAM_REQ_INPROG;
  483                         if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
  484                                 ahc_run_tqinfifo(ahc, /*paused*/FALSE);
  485                         break;
  486                 }
  487 
  488                 /*
  489                  * The target_id represents the target we attempt to
  490                  * select.  In target mode, this is the initiator of
  491                  * the original command.
  492                  */
  493                 our_id = target_id;
  494                 target_id = ccb->csio.init_id;
  495                 /* FALLTHROUGH */
  496         }
  497         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
  498         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
  499         {
  500                 struct  scb *scb;
  501                 struct  hardware_scb *hscb;     
  502 
  503                 if ((ahc->flags & AHC_INITIATORROLE) == 0
  504                  && (ccb->ccb_h.func_code == XPT_SCSI_IO
  505                   || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
  506                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
  507                         xpt_done(ccb);
  508                         return;
  509                 }
  510 
  511                 /*
  512                  * get an scb to use.
  513                  */
  514                 if ((scb = ahc_get_scb(ahc)) == NULL) {
  515         
  516                         xpt_freeze_simq(sim, /*count*/1);
  517                         ahc->flags |= AHC_RESOURCE_SHORTAGE;
  518                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
  519                         xpt_done(ccb);
  520                         return;
  521                 }
  522                 
  523                 hscb = scb->hscb;
  524                 
  525                 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
  526                           ("start scb(%p)\n", scb));
  527                 scb->io_ctx = ccb;
  528                 /*
  529                  * So we can find the SCB when an abort is requested
  530                  */
  531                 ccb->ccb_h.ccb_scb_ptr = scb;
  532 
  533                 /*
  534                  * Put all the arguments for the xfer in the scb
  535                  */
  536                 hscb->control = 0;
  537                 hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
  538                 hscb->lun = ccb->ccb_h.target_lun;
  539                 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
  540                         hscb->cdb_len = 0;
  541                         scb->flags |= SCB_DEVICE_RESET;
  542                         hscb->control |= MK_MESSAGE;
  543                         ahc_execute_scb(scb, NULL, 0, 0);
  544                 } else {
  545                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
  546                                 struct target_data *tdata;
  547 
  548                                 tdata = &hscb->shared_data.tdata;
  549                                 if (ahc->pending_device == lstate)
  550                                         scb->flags |= SCB_TARGET_IMMEDIATE;
  551                                 hscb->control |= TARGET_SCB;
  552                                 scb->flags |= SCB_TARGET_SCB;
  553                                 tdata->target_phases = 0;
  554                                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
  555                                         tdata->target_phases |= SPHASE_PENDING;
  556                                         tdata->scsi_status =
  557                                             ccb->csio.scsi_status;
  558                                 }
  559                                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
  560                                         tdata->target_phases |= NO_DISCONNECT;
  561 
  562                                 tdata->initiator_tag = ccb->csio.tag_id;
  563                         }
  564                         if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
  565                                 hscb->control |= ccb->csio.tag_action;
  566                         
  567                         ahc_setup_data(ahc, sim, &ccb->csio, scb);
  568                 }
  569                 break;
  570         }
  571         case XPT_NOTIFY_ACK:
  572         case XPT_IMMED_NOTIFY:
  573         {
  574                 struct     ahc_tmode_tstate *tstate;
  575                 struct     ahc_tmode_lstate *lstate;
  576                 cam_status status;
  577 
  578                 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
  579                                              &lstate, TRUE);
  580 
  581                 if (status != CAM_REQ_CMP) {
  582                         ccb->ccb_h.status = status;
  583                         xpt_done(ccb);
  584                         break;
  585                 }
  586                 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
  587                                   sim_links.sle);
  588                 ccb->ccb_h.status = CAM_REQ_INPROG;
  589                 ahc_send_lstate_events(ahc, lstate);
  590                 break;
  591         }
  592         case XPT_EN_LUN:                /* Enable LUN as a target */
  593                 ahc_handle_en_lun(ahc, sim, ccb);
  594                 xpt_done(ccb);
  595                 break;
  596         case XPT_ABORT:                 /* Abort the specified CCB */
  597         {
  598                 ahc_abort_ccb(ahc, sim, ccb);
  599                 break;
  600         }
  601         case XPT_SET_TRAN_SETTINGS:
  602         {
  603                 struct  ahc_devinfo devinfo;
  604                 struct  ccb_trans_settings *cts;
  605                 struct  ccb_trans_settings_scsi *scsi;
  606                 struct  ccb_trans_settings_spi *spi;
  607                 struct  ahc_initiator_tinfo *tinfo;
  608                 struct  ahc_tmode_tstate *tstate;
  609                 uint16_t *discenable;
  610                 uint16_t *tagenable;
  611                 u_int   update_type;
  612 
  613                 cts = &ccb->cts;
  614                 scsi = &cts->proto_specific.scsi;
  615                 spi = &cts->xport_specific.spi;
  616                 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
  617                                     cts->ccb_h.target_id,
  618                                     cts->ccb_h.target_lun,
  619                                     SIM_CHANNEL(ahc, sim),
  620                                     ROLE_UNKNOWN);
  621                 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
  622                                             devinfo.our_scsiid,
  623                                             devinfo.target, &tstate);
  624                 update_type = 0;
  625                 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
  626                         update_type |= AHC_TRANS_GOAL;
  627                         discenable = &tstate->discenable;
  628                         tagenable = &tstate->tagenable;
  629                         tinfo->curr.protocol_version =
  630                             cts->protocol_version;
  631                         tinfo->curr.transport_version =
  632                             cts->transport_version;
  633                         tinfo->goal.protocol_version =
  634                             cts->protocol_version;
  635                         tinfo->goal.transport_version =
  636                             cts->transport_version;
  637                 } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
  638                         update_type |= AHC_TRANS_USER;
  639                         discenable = &ahc->user_discenable;
  640                         tagenable = &ahc->user_tagenable;
  641                         tinfo->user.protocol_version =
  642                             cts->protocol_version;
  643                         tinfo->user.transport_version =
  644                             cts->transport_version;
  645                 } else {
  646                         ccb->ccb_h.status = CAM_REQ_INVALID;
  647                         xpt_done(ccb);
  648                         break;
  649                 }
  650                 
  651                 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
  652                         if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
  653                                 *discenable |= devinfo.target_mask;
  654                         else
  655                                 *discenable &= ~devinfo.target_mask;
  656                 }
  657                 
  658                 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
  659                         if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
  660                                 *tagenable |= devinfo.target_mask;
  661                         else
  662                                 *tagenable &= ~devinfo.target_mask;
  663                 }       
  664 
  665                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
  666                         ahc_validate_width(ahc, /*tinfo limit*/NULL,
  667                                            &spi->bus_width, ROLE_UNKNOWN);
  668                         ahc_set_width(ahc, &devinfo, spi->bus_width,
  669                                       update_type, /*paused*/FALSE);
  670                 }
  671 
  672                 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
  673                         if (update_type == AHC_TRANS_USER)
  674                                 spi->ppr_options = tinfo->user.ppr_options;
  675                         else
  676                                 spi->ppr_options = tinfo->goal.ppr_options;
  677                 }
  678 
  679                 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
  680                         if (update_type == AHC_TRANS_USER)
  681                                 spi->sync_offset = tinfo->user.offset;
  682                         else
  683                                 spi->sync_offset = tinfo->goal.offset;
  684                 }
  685 
  686                 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
  687                         if (update_type == AHC_TRANS_USER)
  688                                 spi->sync_period = tinfo->user.period;
  689                         else
  690                                 spi->sync_period = tinfo->goal.period;
  691                 }
  692 
  693                 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
  694                  || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
  695                         struct ahc_syncrate *syncrate;
  696                         u_int maxsync;
  697 
  698                         if ((ahc->features & AHC_ULTRA2) != 0)
  699                                 maxsync = AHC_SYNCRATE_DT;
  700                         else if ((ahc->features & AHC_ULTRA) != 0)
  701                                 maxsync = AHC_SYNCRATE_ULTRA;
  702                         else
  703                                 maxsync = AHC_SYNCRATE_FAST;
  704 
  705                         if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
  706                                 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  707 
  708                         syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
  709                                                      &spi->ppr_options,
  710                                                      maxsync);
  711                         ahc_validate_offset(ahc, /*tinfo limit*/NULL,
  712                                             syncrate, &spi->sync_offset,
  713                                             spi->bus_width, ROLE_UNKNOWN);
  714 
  715                         /* We use a period of 0 to represent async */
  716                         if (spi->sync_offset == 0) {
  717                                 spi->sync_period = 0;
  718                                 spi->ppr_options = 0;
  719                         }
  720 
  721                         ahc_set_syncrate(ahc, &devinfo, syncrate,
  722                                          spi->sync_period, spi->sync_offset,
  723                                          spi->ppr_options, update_type,
  724                                          /*paused*/FALSE);
  725                 }
  726                 ccb->ccb_h.status = CAM_REQ_CMP;
  727                 xpt_done(ccb);
  728                 break;
  729         }
  730         case XPT_GET_TRAN_SETTINGS:
  731         /* Get default/user set transfer settings for the target */
  732         {
  733 
  734                 ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
  735                                       SIM_CHANNEL(ahc, sim), &ccb->cts);
  736                 xpt_done(ccb);
  737                 break;
  738         }
  739         case XPT_CALC_GEOMETRY:
  740         {
  741                 int extended;
  742 
  743                 extended = SIM_IS_SCSIBUS_B(ahc, sim)
  744                          ? ahc->flags & AHC_EXTENDED_TRANS_B
  745                          : ahc->flags & AHC_EXTENDED_TRANS_A;
  746                 aic_calc_geometry(&ccb->ccg, extended);
  747                 xpt_done(ccb);
  748                 break;
  749         }
  750         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
  751         {
  752                 int  found;
  753                 
  754                 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
  755                                           /*initiate reset*/TRUE);
  756                 if (bootverbose) {
  757                         xpt_print_path(SIM_PATH(ahc, sim));
  758                         printf("SCSI bus reset delivered. "
  759                                "%d SCBs aborted.\n", found);
  760                 }
  761                 ccb->ccb_h.status = CAM_REQ_CMP;
  762                 xpt_done(ccb);
  763                 break;
  764         }
  765         case XPT_TERM_IO:               /* Terminate the I/O process */
  766                 /* XXX Implement */
  767                 ccb->ccb_h.status = CAM_REQ_INVALID;
  768                 xpt_done(ccb);
  769                 break;
  770         case XPT_PATH_INQ:              /* Path routing inquiry */
  771         {
  772                 struct ccb_pathinq *cpi = &ccb->cpi;
  773                 
  774                 cpi->version_num = 1; /* XXX??? */
  775                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
  776                 if ((ahc->features & AHC_WIDE) != 0)
  777                         cpi->hba_inquiry |= PI_WIDE_16;
  778                 if ((ahc->features & AHC_TARGETMODE) != 0) {
  779                         cpi->target_sprt = PIT_PROCESSOR
  780                                          | PIT_DISCONNECT
  781                                          | PIT_TERM_IO;
  782                 } else {
  783                         cpi->target_sprt = 0;
  784                 }
  785                 cpi->hba_misc = 0;
  786                 cpi->hba_eng_cnt = 0;
  787                 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
  788                 cpi->max_lun = AHC_NUM_LUNS - 1;
  789                 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
  790                         cpi->initiator_id = ahc->our_id_b;
  791                         if ((ahc->flags & AHC_RESET_BUS_B) == 0)
  792                                 cpi->hba_misc |= PIM_NOBUSRESET;
  793                 } else {
  794                         cpi->initiator_id = ahc->our_id;
  795                         if ((ahc->flags & AHC_RESET_BUS_A) == 0)
  796                                 cpi->hba_misc |= PIM_NOBUSRESET;
  797                 }
  798                 cpi->bus_id = cam_sim_bus(sim);
  799                 cpi->base_transfer_speed = 3300;
  800                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
  801                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
  802                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
  803                 cpi->unit_number = cam_sim_unit(sim);
  804                 cpi->protocol = PROTO_SCSI;
  805                 cpi->protocol_version = SCSI_REV_2;
  806                 cpi->transport = XPORT_SPI;
  807                 cpi->transport_version = 2;
  808                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
  809                 if ((ahc->features & AHC_DT) != 0) {
  810                         cpi->transport_version = 3;
  811                         cpi->xport_specific.spi.ppr_options =
  812                             SID_SPI_CLOCK_DT_ST;
  813                 }
  814                 cpi->ccb_h.status = CAM_REQ_CMP;
  815                 xpt_done(ccb);
  816                 break;
  817         }
  818         default:
  819                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
  820                 xpt_done(ccb);
  821                 break;
  822         }
  823 }
  824 
  825 static void
  826 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
  827                       struct ccb_trans_settings *cts)
  828 {
  829         struct  ahc_devinfo devinfo;
  830         struct  ccb_trans_settings_scsi *scsi;
  831         struct  ccb_trans_settings_spi *spi;
  832         struct  ahc_initiator_tinfo *targ_info;
  833         struct  ahc_tmode_tstate *tstate;
  834         struct  ahc_transinfo *tinfo;
  835 
  836         scsi = &cts->proto_specific.scsi;
  837         spi = &cts->xport_specific.spi;
  838         ahc_compile_devinfo(&devinfo, our_id,
  839                             cts->ccb_h.target_id,
  840                             cts->ccb_h.target_lun,
  841                             channel, ROLE_UNKNOWN);
  842         targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
  843                                         devinfo.our_scsiid,
  844                                         devinfo.target, &tstate);
  845         
  846         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
  847                 tinfo = &targ_info->curr;
  848         else
  849                 tinfo = &targ_info->user;
  850         
  851         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
  852         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
  853         if (cts->type == CTS_TYPE_USER_SETTINGS) {
  854                 if ((ahc->user_discenable & devinfo.target_mask) != 0)
  855                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
  856 
  857                 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
  858                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
  859         } else {
  860                 if ((tstate->discenable & devinfo.target_mask) != 0)
  861                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
  862 
  863                 if ((tstate->tagenable & devinfo.target_mask) != 0)
  864                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
  865         }
  866         cts->protocol_version = tinfo->protocol_version;
  867         cts->transport_version = tinfo->transport_version;
  868 
  869         spi->sync_period = tinfo->period;
  870         spi->sync_offset = tinfo->offset;
  871         spi->bus_width = tinfo->width;
  872         spi->ppr_options = tinfo->ppr_options;
  873         
  874         cts->protocol = PROTO_SCSI;
  875         cts->transport = XPORT_SPI;
  876         spi->valid = CTS_SPI_VALID_SYNC_RATE
  877                    | CTS_SPI_VALID_SYNC_OFFSET
  878                    | CTS_SPI_VALID_BUS_WIDTH
  879                    | CTS_SPI_VALID_PPR_OPTIONS;
  880 
  881         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
  882                 scsi->valid = CTS_SCSI_VALID_TQ;
  883                 spi->valid |= CTS_SPI_VALID_DISC;
  884         } else {
  885                 scsi->valid = 0;
  886         }
  887 
  888         cts->ccb_h.status = CAM_REQ_CMP;
  889 }
  890 
  891 static void
  892 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
  893 {
  894         struct ahc_softc *ahc;
  895         struct cam_sim *sim;
  896 
  897         sim = (struct cam_sim *)callback_arg;
  898         ahc = (struct ahc_softc *)cam_sim_softc(sim);
  899         switch (code) {
  900         case AC_LOST_DEVICE:
  901         {
  902                 struct  ahc_devinfo devinfo;
  903 
  904                 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
  905                                     xpt_path_target_id(path),
  906                                     xpt_path_lun_id(path),
  907                                     SIM_CHANNEL(ahc, sim),
  908                                     ROLE_UNKNOWN);
  909 
  910                 /*
  911                  * Revert to async/narrow transfers
  912                  * for the next device.
  913                  */
  914                 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  915                               AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
  916                 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
  917                                  /*period*/0, /*offset*/0, /*ppr_options*/0,
  918                                  AHC_TRANS_GOAL|AHC_TRANS_CUR,
  919                                  /*paused*/FALSE);
  920                 break;
  921         }
  922         default:
  923                 break;
  924         }
  925 }
  926 
  927 static void
  928 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
  929                 int error)
  930 {
  931         struct  scb *scb;
  932         union   ccb *ccb;
  933         struct  ahc_softc *ahc;
  934         struct  ahc_initiator_tinfo *tinfo;
  935         struct  ahc_tmode_tstate *tstate;
  936         u_int   mask;
  937 
  938         scb = (struct scb *)arg;
  939         ccb = scb->io_ctx;
  940         ahc = scb->ahc_softc;
  941 
  942         if (error != 0) {
  943                 if (error == EFBIG)
  944                         aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
  945                 else
  946                         aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
  947                 if (nsegments != 0)
  948                         bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
  949                 ahc_free_scb(ahc, scb);
  950                 xpt_done(ccb);
  951                 return;
  952         }
  953         if (nsegments != 0) {
  954                 struct    ahc_dma_seg *sg;
  955                 bus_dma_segment_t *end_seg;
  956                 bus_dmasync_op_t op;
  957 
  958                 end_seg = dm_segs + nsegments;
  959 
  960                 /* Copy the segments into our SG list */
  961                 sg = scb->sg_list;
  962                 while (dm_segs < end_seg) {
  963                         uint32_t len;
  964 
  965                         sg->addr = aic_htole32(dm_segs->ds_addr);
  966                         len = dm_segs->ds_len
  967                             | ((dm_segs->ds_addr >> 8) & 0x7F000000);
  968                         sg->len = aic_htole32(len);
  969                         sg++;
  970                         dm_segs++;
  971                 }
  972                 
  973                 /*
  974                  * Note where to find the SG entries in bus space.
  975                  * We also set the full residual flag which the 
  976                  * sequencer will clear as soon as a data transfer
  977                  * occurs.
  978                  */
  979                 scb->hscb->sgptr = aic_htole32(scb->sg_list_phys|SG_FULL_RESID);
  980 
  981                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
  982                         op = BUS_DMASYNC_PREREAD;
  983                 else
  984                         op = BUS_DMASYNC_PREWRITE;
  985 
  986                 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
  987 
  988                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
  989                         struct target_data *tdata;
  990 
  991                         tdata = &scb->hscb->shared_data.tdata;
  992                         tdata->target_phases |= DPHASE_PENDING;
  993                         /*
  994                          * CAM data direction is relative to the initiator.
  995                          */
  996                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
  997                                 tdata->data_phase = P_DATAOUT;
  998                         else
  999                                 tdata->data_phase = P_DATAIN;
 1000 
 1001                         /*
 1002                          * If the transfer is of an odd length and in the
 1003                          * "in" direction (scsi->HostBus), then it may
 1004                          * trigger a bug in the 'WideODD' feature of
 1005                          * non-Ultra2 chips.  Force the total data-length
 1006                          * to be even by adding an extra, 1 byte, SG,
 1007                          * element.  We do this even if we are not currently
 1008                          * negotiated wide as negotiation could occur before
 1009                          * this command is executed.
 1010                          */
 1011                         if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
 1012                          && (ccb->csio.dxfer_len & 0x1) != 0
 1013                          && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
 1014 
 1015                                 nsegments++;
 1016                                 if (nsegments > AHC_NSEG) {
 1017 
 1018                                         aic_set_transaction_status(scb,
 1019                                             CAM_REQ_TOO_BIG);
 1020                                         bus_dmamap_unload(ahc->buffer_dmat,
 1021                                                           scb->dmamap);
 1022                                         ahc_free_scb(ahc, scb);
 1023                                         xpt_done(ccb);
 1024                                         return;
 1025                                 }
 1026                                 sg->addr = aic_htole32(ahc->dma_bug_buf);
 1027                                 sg->len = aic_htole32(1);
 1028                                 sg++;
 1029                         }
 1030                 }
 1031                 sg--;
 1032                 sg->len |= aic_htole32(AHC_DMA_LAST_SEG);
 1033 
 1034                 /* Copy the first SG into the "current" data pointer area */
 1035                 scb->hscb->dataptr = scb->sg_list->addr;
 1036                 scb->hscb->datacnt = scb->sg_list->len;
 1037         } else {
 1038                 scb->hscb->sgptr = aic_htole32(SG_LIST_NULL);
 1039                 scb->hscb->dataptr = 0;
 1040                 scb->hscb->datacnt = 0;
 1041         }
 1042         
 1043         scb->sg_count = nsegments;
 1044 
 1045         /*
 1046          * Last time we need to check if this SCB needs to
 1047          * be aborted.
 1048          */
 1049         if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
 1050                 if (nsegments != 0)
 1051                         bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
 1052                 ahc_free_scb(ahc, scb);
 1053                 xpt_done(ccb);
 1054                 return;
 1055         }
 1056 
 1057         tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
 1058                                     SCSIID_OUR_ID(scb->hscb->scsiid),
 1059                                     SCSIID_TARGET(ahc, scb->hscb->scsiid),
 1060                                     &tstate);
 1061 
 1062         mask = SCB_GET_TARGET_MASK(ahc, scb);
 1063         scb->hscb->scsirate = tinfo->scsirate;
 1064         scb->hscb->scsioffset = tinfo->curr.offset;
 1065         if ((tstate->ultraenb & mask) != 0)
 1066                 scb->hscb->control |= ULTRAENB;
 1067 
 1068         if ((tstate->discenable & mask) != 0
 1069          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
 1070                 scb->hscb->control |= DISCENB;
 1071 
 1072         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
 1073          && (tinfo->goal.width != 0
 1074           || tinfo->goal.offset != 0
 1075           || tinfo->goal.ppr_options != 0)) {
 1076                 scb->flags |= SCB_NEGOTIATE;
 1077                 scb->hscb->control |= MK_MESSAGE;
 1078         } else if ((tstate->auto_negotiate & mask) != 0) {
 1079                 scb->flags |= SCB_AUTO_NEGOTIATE;
 1080                 scb->hscb->control |= MK_MESSAGE;
 1081         }
 1082 
 1083         LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
 1084 
 1085         ccb->ccb_h.status |= CAM_SIM_QUEUED;
 1086 
 1087         /*
 1088          * We only allow one untagged transaction
 1089          * per target in the initiator role unless
 1090          * we are storing a full busy target *lun*
 1091          * table in SCB space.
 1092          */
 1093         if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
 1094          && (ahc->flags & AHC_SCB_BTT) == 0) {
 1095                 struct scb_tailq *untagged_q;
 1096                 int target_offset;
 1097 
 1098                 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
 1099                 untagged_q = &(ahc->untagged_queues[target_offset]);
 1100                 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
 1101                 scb->flags |= SCB_UNTAGGEDQ;
 1102                 if (TAILQ_FIRST(untagged_q) != scb) {
 1103                         return;
 1104                 }
 1105         }
 1106         scb->flags |= SCB_ACTIVE;
 1107 
 1108         /*
 1109          * Timers are disabled while recovery is in progress.
 1110          */
 1111         aic_scb_timer_start(scb);
 1112 
 1113         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
 1114                 /* Define a mapping from our tag to the SCB. */
 1115                 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
 1116                 ahc_pause(ahc);
 1117                 if ((ahc->flags & AHC_PAGESCBS) == 0)
 1118                         ahc_outb(ahc, SCBPTR, scb->hscb->tag);
 1119                 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
 1120                 ahc_unpause(ahc);
 1121         } else {
 1122                 ahc_queue_scb(ahc, scb);
 1123         }
 1124 }
 1125 
 1126 static void
 1127 ahc_poll(struct cam_sim *sim)
 1128 {
 1129         struct ahc_softc *ahc;
 1130 
 1131         ahc = (struct ahc_softc *)cam_sim_softc(sim);
 1132         ahc_intr(ahc);
 1133 }
 1134 
 1135 static void
 1136 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
 1137                struct ccb_scsiio *csio, struct scb *scb)
 1138 {
 1139         struct hardware_scb *hscb;
 1140         struct ccb_hdr *ccb_h;
 1141         
 1142         hscb = scb->hscb;
 1143         ccb_h = &csio->ccb_h;
 1144         
 1145         csio->resid = 0;
 1146         csio->sense_resid = 0;
 1147         if (ccb_h->func_code == XPT_SCSI_IO) {
 1148                 hscb->cdb_len = csio->cdb_len;
 1149                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
 1150 
 1151                         if (hscb->cdb_len > sizeof(hscb->cdb32)
 1152                          || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
 1153                                 aic_set_transaction_status(scb,
 1154                                                            CAM_REQ_INVALID);
 1155                                 ahc_free_scb(ahc, scb);
 1156                                 xpt_done((union ccb *)csio);
 1157                                 return;
 1158                         }
 1159                         if (hscb->cdb_len > 12) {
 1160                                 memcpy(hscb->cdb32, 
 1161                                        csio->cdb_io.cdb_ptr,
 1162                                        hscb->cdb_len);
 1163                                 scb->flags |= SCB_CDB32_PTR;
 1164                         } else {
 1165                                 memcpy(hscb->shared_data.cdb, 
 1166                                        csio->cdb_io.cdb_ptr,
 1167                                        hscb->cdb_len);
 1168                         }
 1169                 } else {
 1170                         if (hscb->cdb_len > 12) {
 1171                                 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
 1172                                        hscb->cdb_len);
 1173                                 scb->flags |= SCB_CDB32_PTR;
 1174                         } else {
 1175                                 memcpy(hscb->shared_data.cdb,
 1176                                        csio->cdb_io.cdb_bytes,
 1177                                        hscb->cdb_len);
 1178                         }
 1179                 }
 1180         }
 1181                 
 1182         /* Only use S/G if there is a transfer */
 1183         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
 1184                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
 1185                         /* We've been given a pointer to a single buffer */
 1186                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
 1187                                 int s;
 1188                                 int error;
 1189 
 1190                                 s = splsoftvm();
 1191                                 error = bus_dmamap_load(ahc->buffer_dmat,
 1192                                                         scb->dmamap,
 1193                                                         csio->data_ptr,
 1194                                                         csio->dxfer_len,
 1195                                                         ahc_execute_scb,
 1196                                                         scb, /*flags*/0);
 1197                                 if (error == EINPROGRESS) {
 1198                                         /*
 1199                                          * So as to maintain ordering,
 1200                                          * freeze the controller queue
 1201                                          * until our mapping is
 1202                                          * returned.
 1203                                          */
 1204                                         xpt_freeze_simq(sim,
 1205                                                         /*count*/1);
 1206                                         scb->io_ctx->ccb_h.status |=
 1207                                             CAM_RELEASE_SIMQ;
 1208                                 }
 1209                                 splx(s);
 1210                         } else {
 1211                                 struct bus_dma_segment seg;
 1212 
 1213                                 /* Pointer to physical buffer */
 1214                                 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
 1215                                         panic("ahc_setup_data - Transfer size "
 1216                                               "larger than can device max");
 1217 
 1218                                 seg.ds_addr =
 1219                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
 1220                                 seg.ds_len = csio->dxfer_len;
 1221                                 ahc_execute_scb(scb, &seg, 1, 0);
 1222                         }
 1223                 } else {
 1224                         struct bus_dma_segment *segs;
 1225 
 1226                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
 1227                                 panic("ahc_setup_data - Physical segment "
 1228                                       "pointers unsupported");
 1229 
 1230                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
 1231                                 panic("ahc_setup_data - Virtual segment "
 1232                                       "addresses unsupported");
 1233 
 1234                         /* Just use the segments provided */
 1235                         segs = (struct bus_dma_segment *)csio->data_ptr;
 1236                         ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
 1237                 }
 1238         } else {
 1239                 ahc_execute_scb(scb, NULL, 0, 0);
 1240         }
 1241 }
 1242 
 1243 static void
 1244 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
 1245 {
 1246         union ccb *abort_ccb;
 1247 
 1248         abort_ccb = ccb->cab.abort_ccb;
 1249         switch (abort_ccb->ccb_h.func_code) {
 1250         case XPT_ACCEPT_TARGET_IO:
 1251         case XPT_IMMED_NOTIFY:
 1252         case XPT_CONT_TARGET_IO:
 1253         {
 1254                 struct ahc_tmode_tstate *tstate;
 1255                 struct ahc_tmode_lstate *lstate;
 1256                 struct ccb_hdr_slist *list;
 1257                 cam_status status;
 1258 
 1259                 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
 1260                                              &lstate, TRUE);
 1261 
 1262                 if (status != CAM_REQ_CMP) {
 1263                         ccb->ccb_h.status = status;
 1264                         break;
 1265                 }
 1266 
 1267                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
 1268                         list = &lstate->accept_tios;
 1269                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
 1270                         list = &lstate->immed_notifies;
 1271                 else
 1272                         list = NULL;
 1273 
 1274                 if (list != NULL) {
 1275                         struct ccb_hdr *curelm;
 1276                         int found;
 1277 
 1278                         curelm = SLIST_FIRST(list);
 1279                         found = 0;
 1280                         if (curelm == &abort_ccb->ccb_h) {
 1281                                 found = 1;
 1282                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
 1283                         } else {
 1284                                 while(curelm != NULL) {
 1285                                         struct ccb_hdr *nextelm;
 1286 
 1287                                         nextelm =
 1288                                             SLIST_NEXT(curelm, sim_links.sle);
 1289 
 1290                                         if (nextelm == &abort_ccb->ccb_h) {
 1291                                                 found = 1;
 1292                                                 SLIST_NEXT(curelm,
 1293                                                            sim_links.sle) =
 1294                                                     SLIST_NEXT(nextelm,
 1295                                                                sim_links.sle);
 1296                                                 break;
 1297                                         }
 1298                                         curelm = nextelm;
 1299                                 }
 1300                         }
 1301 
 1302                         if (found) {
 1303                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
 1304                                 xpt_done(abort_ccb);
 1305                                 ccb->ccb_h.status = CAM_REQ_CMP;
 1306                         } else {
 1307                                 xpt_print_path(abort_ccb->ccb_h.path);
 1308                                 printf("Not found\n");
 1309                                 ccb->ccb_h.status = CAM_PATH_INVALID;
 1310                         }
 1311                         break;
 1312                 }
 1313                 /* FALLTHROUGH */
 1314         }
 1315         case XPT_SCSI_IO:
 1316                 /* XXX Fully implement the hard ones */
 1317                 ccb->ccb_h.status = CAM_UA_ABORT;
 1318                 break;
 1319         default:
 1320                 ccb->ccb_h.status = CAM_REQ_INVALID;
 1321                 break;
 1322         }
 1323         xpt_done(ccb);
 1324 }
 1325 
 1326 void
 1327 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
 1328                 u_int lun, ac_code code, void *opt_arg)
 1329 {
 1330         struct  ccb_trans_settings cts;
 1331         struct cam_path *path;
 1332         void *arg;
 1333         int error;
 1334 
 1335         arg = NULL;
 1336         error = ahc_create_path(ahc, channel, target, lun, &path);
 1337 
 1338         if (error != CAM_REQ_CMP)
 1339                 return;
 1340 
 1341         switch (code) {
 1342         case AC_TRANSFER_NEG:
 1343         {
 1344                 struct  ccb_trans_settings_scsi *scsi;
 1345         
 1346                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
 1347                 scsi = &cts.proto_specific.scsi;
 1348                 cts.ccb_h.path = path;
 1349                 cts.ccb_h.target_id = target;
 1350                 cts.ccb_h.target_lun = lun;
 1351                 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
 1352                                                           : ahc->our_id_b,
 1353                                       channel, &cts);
 1354                 arg = &cts;
 1355                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
 1356                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
 1357                 if (opt_arg == NULL)
 1358                         break;
 1359                 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
 1360                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
 1361                 scsi->valid |= CTS_SCSI_VALID_TQ;
 1362                 break;
 1363         }
 1364         case AC_SENT_BDR:
 1365         case AC_BUS_RESET:
 1366                 break;
 1367         default:
 1368                 panic("ahc_send_async: Unexpected async event");
 1369         }
 1370         xpt_async(code, path, arg);
 1371         xpt_free_path(path);
 1372 }
 1373 
 1374 void
 1375 ahc_platform_set_tags(struct ahc_softc *ahc,
 1376                       struct ahc_devinfo *devinfo, int enable)
 1377 {
 1378 }
 1379 
 1380 int
 1381 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
 1382 {
 1383         ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
 1384             M_NOWAIT | M_ZERO);
 1385         if (ahc->platform_data == NULL)
 1386                 return (ENOMEM);
 1387         return (0);
 1388 }
 1389 
 1390 void
 1391 ahc_platform_free(struct ahc_softc *ahc)
 1392 {
 1393         struct ahc_platform_data *pdata;
 1394 
 1395         pdata = ahc->platform_data;
 1396         if (pdata != NULL) {
 1397                 if (pdata->regs != NULL)
 1398                         bus_release_resource(ahc->dev_softc,
 1399                                              pdata->regs_res_type,
 1400                                              pdata->regs_res_id,
 1401                                              pdata->regs);
 1402 
 1403                 if (pdata->irq != NULL)
 1404                         bus_release_resource(ahc->dev_softc,
 1405                                              pdata->irq_res_type,
 1406                                              0, pdata->irq);
 1407 
 1408                 if (pdata->sim_b != NULL) {
 1409                         xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
 1410                         xpt_free_path(pdata->path_b);
 1411                         xpt_bus_deregister(cam_sim_path(pdata->sim_b));
 1412                         cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
 1413                 }
 1414                 if (pdata->sim != NULL) {
 1415                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
 1416                         xpt_free_path(pdata->path);
 1417                         xpt_bus_deregister(cam_sim_path(pdata->sim));
 1418                         cam_sim_free(pdata->sim, /*free_devq*/TRUE);
 1419                 }
 1420                 if (pdata->eh != NULL)
 1421                         EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
 1422                 free(ahc->platform_data, M_DEVBUF);
 1423         }
 1424 }
 1425 
 1426 int
 1427 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
 1428 {
 1429         /* We don't sort softcs under FreeBSD so report equal always */
 1430         return (0);
 1431 }
 1432 
 1433 int
 1434 ahc_detach(device_t dev)
 1435 {
 1436         struct ahc_softc *ahc;
 1437 
 1438         device_printf(dev, "detaching device\n");
 1439         ahc = device_get_softc(dev);
 1440         ahc_lock(ahc);
 1441         TAILQ_REMOVE(&ahc_tailq, ahc, links);
 1442         ahc_intr_enable(ahc, FALSE);
 1443         bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
 1444         ahc_unlock(ahc);
 1445         ahc_free(ahc);
 1446         return (0);
 1447 }
 1448 
 1449 #if 0
 1450 static void
 1451 ahc_dump_targcmd(struct target_cmd *cmd)
 1452 {
 1453         uint8_t *byte;
 1454         uint8_t *last_byte;
 1455         int i;
 1456 
 1457         byte = &cmd->initiator_channel;
 1458         /* Debugging info for received commands */
 1459         last_byte = &cmd[1].initiator_channel;
 1460 
 1461         i = 0;
 1462         while (byte < last_byte) {
 1463                 if (i == 0)
 1464                         printf("\t");
 1465                 printf("%#x", *byte++);
 1466                 i++;
 1467                 if (i == 8) {
 1468                         printf("\n");
 1469                         i = 0;
 1470                 } else {
 1471                         printf(", ");
 1472                 }
 1473         }
 1474 }
 1475 #endif
 1476 
 1477 static int
 1478 ahc_modevent(module_t mod, int type, void *data)
 1479 {
 1480         /* XXX Deal with busy status on unload. */
 1481         /* XXX Deal with unknown events */
 1482         return 0;
 1483 }
 1484   
 1485 static moduledata_t ahc_mod = {
 1486         "ahc",
 1487         ahc_modevent,
 1488         NULL
 1489 };
 1490 
 1491 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
 1492 MODULE_DEPEND(ahc, cam, 1, 1, 1);
 1493 MODULE_VERSION(ahc, 1);

Cache object: a20c987d5ecb923efe626c63d49c5113


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