2 * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers
4 * Copyright (c) 1994-2001 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU Public License ("GPL").
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
31 * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.c#13 $
33 * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx_osm.c,v 1.27.2.6 2003/06/10 03:26:09 gibbs Exp $
34 * $DragonFly: src/sys/dev/disk/aic7xxx/aic7xxx_osm.c,v 1.12 2007/01/27 15:03:25 swildner Exp $
37 #include "aic7xxx_osm.h"
38 #include "aic7xxx_inline.h"
40 #ifndef AHC_TMODE_ENABLE
41 #define AHC_TMODE_ENABLE 0
44 #define ccb_scb_ptr spriv_ptr0
46 devclass_t ahc_devclass;
49 static void ahc_dump_targcmd(struct target_cmd *cmd);
51 static int ahc_modevent(module_t mod, int type, void *data);
52 static void ahc_action(struct cam_sim *sim, union ccb *ccb);
53 static void ahc_get_tran_settings(struct ahc_softc *ahc,
54 int our_id, char channel,
55 struct ccb_trans_settings *cts);
56 static void ahc_async(void *callback_arg, uint32_t code,
57 struct cam_path *path, void *arg);
58 static void ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
59 int nsegments, int error);
60 static void ahc_poll(struct cam_sim *sim);
61 static void ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
62 struct ccb_scsiio *csio, struct scb *scb);
63 static void ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
65 static int ahc_create_path(struct ahc_softc *ahc,
66 char channel, u_int target, u_int lun,
67 struct cam_path **path);
69 static void ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb);
72 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
73 u_int lun, struct cam_path **path)
78 path_id = cam_sim_path(ahc->platform_data->sim_b);
80 path_id = cam_sim_path(ahc->platform_data->sim);
82 return (xpt_create_path(path, /*periph*/NULL,
83 path_id, target, lun));
87 ahc_map_int(struct ahc_softc *ahc)
91 /* Hook up our interrupt handler */
92 error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
93 0, ahc_platform_intr, ahc,
94 &ahc->platform_data->ih, NULL);
97 device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
103 * Attach all the sub-devices we can find
106 ahc_attach(struct ahc_softc *ahc)
109 struct ccb_setasync csa;
113 struct cam_sim *sim2;
114 struct cam_path *path;
115 struct cam_path *path2;
122 ahc_controller_info(ahc, ahc_info);
123 kprintf("%s\n", ahc_info);
126 * Attach secondary channel first if the user has
127 * declared it the primary channel.
129 if ((ahc->features & AHC_TWIN) != 0
130 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
139 * Construct our first channel SIM entry
141 sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
142 device_get_unit(ahc->dev_softc),
143 1, AHC_MAX_QUEUE, NULL);
147 if (xpt_bus_register(sim, bus_id) != CAM_SUCCESS) {
153 if (xpt_create_path(&path, /*periph*/NULL,
154 cam_sim_path(sim), CAM_TARGET_WILDCARD,
155 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
156 xpt_bus_deregister(cam_sim_path(sim));
162 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
163 csa.ccb_h.func_code = XPT_SASYNC_CB;
164 csa.event_enable = AC_LOST_DEVICE;
165 csa.callback = ahc_async;
166 csa.callback_arg = sim;
167 xpt_action((union ccb *)&csa);
170 if (ahc->features & AHC_TWIN) {
171 sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
172 ahc, device_get_unit(ahc->dev_softc), 1,
173 AHC_MAX_QUEUE, NULL);
176 kprintf("ahc_attach: Unable to attach second "
177 "bus due to resource shortage");
181 if (xpt_bus_register(sim2, bus_id2) != CAM_SUCCESS) {
182 kprintf("ahc_attach: Unable to attach second "
183 "bus due to resource shortage");
185 * We do not want to destroy the device queue
186 * because the first bus is using it.
192 if (xpt_create_path(&path2, /*periph*/NULL,
195 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
196 xpt_bus_deregister(cam_sim_path(sim2));
201 xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
202 csa.ccb_h.func_code = XPT_SASYNC_CB;
203 csa.event_enable = AC_LOST_DEVICE;
204 csa.callback = ahc_async;
205 csa.callback_arg = sim2;
206 xpt_action((union ccb *)&csa);
211 if ((ahc->features & AHC_TWIN) != 0
212 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
213 ahc->platform_data->sim_b = sim;
214 ahc->platform_data->path_b = path;
215 ahc->platform_data->sim = sim2;
216 ahc->platform_data->path = path2;
218 ahc->platform_data->sim = sim;
219 ahc->platform_data->path = path;
220 ahc->platform_data->sim_b = sim2;
221 ahc->platform_data->path_b = path2;
225 /* We have to wait until after any system dumps... */
226 ahc->platform_data->eh =
227 EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
228 ahc, SHUTDOWN_PRI_DEFAULT);
229 ahc_intr_enable(ahc, TRUE);
237 * Catch an interrupt from the adapter
240 ahc_platform_intr(void *arg)
242 struct ahc_softc *ahc;
244 ahc = (struct ahc_softc *)arg;
249 * We have an scb which has been processed by the
250 * adaptor, now we look to see how the operation
254 ahc_done(struct ahc_softc *ahc, struct scb *scb)
258 CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
259 ("ahc_done - scb %d\n", scb->hscb->tag));
262 LIST_REMOVE(scb, pending_links);
263 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
264 struct scb_tailq *untagged_q;
267 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
268 untagged_q = &ahc->untagged_queues[target_offset];
269 TAILQ_REMOVE(untagged_q, scb, links.tqe);
270 scb->flags &= ~SCB_UNTAGGEDQ;
271 ahc_run_untagged_queue(ahc, untagged_q);
274 callout_stop(&ccb->ccb_h.timeout_ch);
276 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
279 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
280 op = BUS_DMASYNC_POSTREAD;
282 op = BUS_DMASYNC_POSTWRITE;
283 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
284 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
287 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
288 struct cam_path *ccb_path;
291 * If we have finally disconnected, clean up our
292 * pending device state.
293 * XXX - There may be error states that cause where
294 * we will remain connected.
296 ccb_path = ccb->ccb_h.path;
297 if (ahc->pending_device != NULL
298 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
300 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
301 ahc->pending_device = NULL;
304 xpt_print_path(ccb->ccb_h.path);
305 kprintf("Still connected\n");
311 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG)
312 ccb->ccb_h.status |= CAM_REQ_CMP;
313 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
314 ahc_free_scb(ahc, scb);
320 * If the recovery SCB completes, we have to be
321 * out of our timeout.
323 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
324 struct scb *list_scb;
327 * We were able to complete the command successfully,
328 * so reinstate the timeouts for all other pending
331 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
335 ccb = list_scb->io_ctx;
336 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
339 time = ccb->ccb_h.timeout;
342 callout_reset(&ccb->ccb_h.timeout_ch, time,
343 ahc_timeout, list_scb);
346 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
347 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
348 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
349 ahc_print_path(ahc, scb);
350 kprintf("no longer in timeout, status = %x\n",
354 /* Don't clobber any existing error state */
355 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
356 ccb->ccb_h.status |= CAM_REQ_CMP;
357 } else if ((scb->flags & SCB_SENSE) != 0) {
359 * We performed autosense retrieval.
361 * Zero any sense not transferred by the
362 * device. The SCSI spec mandates that any
363 * untransfered data should be assumed to be
364 * zero. Complete the 'bounce' of sense information
365 * through buffers accessible via bus-space by
366 * copying it into the clients csio.
368 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
369 memcpy(&ccb->csio.sense_data,
370 ahc_get_sense_buf(ahc, scb),
371 (ahc_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
372 - ccb->csio.sense_resid);
373 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
375 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
376 ahc_free_scb(ahc, scb);
381 ahc_action(struct cam_sim *sim, union ccb *ccb)
383 struct ahc_softc *ahc;
384 struct ahc_tmode_lstate *lstate;
388 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
390 ahc = (struct ahc_softc *)cam_sim_softc(sim);
392 target_id = ccb->ccb_h.target_id;
393 our_id = SIM_SCSI_ID(ahc, sim);
395 switch (ccb->ccb_h.func_code) {
396 /* Common cases first */
397 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
398 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
400 struct ahc_tmode_tstate *tstate;
403 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
406 if (status != CAM_REQ_CMP) {
407 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
408 /* Response from the black hole device */
410 lstate = ahc->black_hole;
412 ccb->ccb_h.status = status;
417 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
420 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
422 ccb->ccb_h.status = CAM_REQ_INPROG;
423 if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
424 ahc_run_tqinfifo(ahc, /*paused*/FALSE);
430 * The target_id represents the target we attempt to
431 * select. In target mode, this is the initiator of
432 * the original command.
435 target_id = ccb->csio.init_id;
438 case XPT_SCSI_IO: /* Execute the requested I/O operation */
439 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
442 struct hardware_scb *hscb;
444 if ((ahc->flags & AHC_INITIATORROLE) == 0
445 && (ccb->ccb_h.func_code == XPT_SCSI_IO
446 || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
447 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
456 if ((scb = ahc_get_scb(ahc)) == NULL) {
458 xpt_freeze_simq(sim, /*count*/1);
459 ahc->flags |= AHC_RESOURCE_SHORTAGE;
461 ccb->ccb_h.status = CAM_REQUEUE_REQ;
469 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
470 ("start scb(%p)\n", scb));
473 * So we can find the SCB when an abort is requested
475 ccb->ccb_h.ccb_scb_ptr = scb;
478 * Put all the arguments for the xfer in the scb
481 hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
482 hscb->lun = ccb->ccb_h.target_lun;
483 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
485 scb->flags |= SCB_DEVICE_RESET;
486 hscb->control |= MK_MESSAGE;
487 ahc_execute_scb(scb, NULL, 0, 0);
489 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
490 struct target_data *tdata;
492 tdata = &hscb->shared_data.tdata;
493 if (ahc->pending_device == lstate)
494 scb->flags |= SCB_TARGET_IMMEDIATE;
495 hscb->control |= TARGET_SCB;
496 scb->flags |= SCB_TARGET_SCB;
497 tdata->target_phases = 0;
498 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
499 tdata->target_phases |= SPHASE_PENDING;
501 ccb->csio.scsi_status;
503 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
504 tdata->target_phases |= NO_DISCONNECT;
506 tdata->initiator_tag = ccb->csio.tag_id;
508 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
509 hscb->control |= ccb->csio.tag_action;
511 ahc_setup_data(ahc, sim, &ccb->csio, scb);
516 case XPT_IMMED_NOTIFY:
518 struct ahc_tmode_tstate *tstate;
519 struct ahc_tmode_lstate *lstate;
522 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
525 if (status != CAM_REQ_CMP) {
526 ccb->ccb_h.status = status;
530 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
532 ccb->ccb_h.status = CAM_REQ_INPROG;
533 ahc_send_lstate_events(ahc, lstate);
536 case XPT_EN_LUN: /* Enable LUN as a target */
537 ahc_handle_en_lun(ahc, sim, ccb);
540 case XPT_ABORT: /* Abort the specified CCB */
542 ahc_abort_ccb(ahc, sim, ccb);
545 case XPT_SET_TRAN_SETTINGS:
547 #ifdef AHC_NEW_TRAN_SETTINGS
548 struct ahc_devinfo devinfo;
549 struct ccb_trans_settings *cts;
550 struct ccb_trans_settings_scsi *scsi;
551 struct ccb_trans_settings_spi *spi;
552 struct ahc_initiator_tinfo *tinfo;
553 struct ahc_tmode_tstate *tstate;
554 uint16_t *discenable;
559 scsi = &cts->proto_specific.scsi;
560 spi = &cts->xport_specific.spi;
561 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
562 cts->ccb_h.target_id,
563 cts->ccb_h.target_lun,
564 SIM_CHANNEL(ahc, sim),
566 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
568 devinfo.target, &tstate);
570 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
571 update_type |= AHC_TRANS_GOAL;
572 discenable = &tstate->discenable;
573 tagenable = &tstate->tagenable;
574 tinfo->curr.protocol_version =
575 cts->protocol_version;
576 tinfo->curr.transport_version =
577 cts->transport_version;
578 tinfo->goal.protocol_version =
579 cts->protocol_version;
580 tinfo->goal.transport_version =
581 cts->transport_version;
582 } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
583 update_type |= AHC_TRANS_USER;
584 discenable = &ahc->user_discenable;
585 tagenable = &ahc->user_tagenable;
586 tinfo->user.protocol_version =
587 cts->protocol_version;
588 tinfo->user.transport_version =
589 cts->transport_version;
591 ccb->ccb_h.status = CAM_REQ_INVALID;
598 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
599 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
600 *discenable |= devinfo.target_mask;
602 *discenable &= ~devinfo.target_mask;
605 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
606 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
607 *tagenable |= devinfo.target_mask;
609 *tagenable &= ~devinfo.target_mask;
612 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
613 ahc_validate_width(ahc, /*tinfo limit*/NULL,
614 &spi->bus_width, ROLE_UNKNOWN);
615 ahc_set_width(ahc, &devinfo, spi->bus_width,
616 update_type, /*paused*/FALSE);
619 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
620 if (update_type == AHC_TRANS_USER)
621 spi->ppr_options = tinfo->user.ppr_options;
623 spi->ppr_options = tinfo->goal.ppr_options;
626 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
627 if (update_type == AHC_TRANS_USER)
628 spi->sync_offset = tinfo->user.offset;
630 spi->sync_offset = tinfo->goal.offset;
633 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
634 if (update_type == AHC_TRANS_USER)
635 spi->sync_period = tinfo->user.period;
637 spi->sync_period = tinfo->goal.period;
640 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
641 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
642 struct ahc_syncrate *syncrate;
645 if ((ahc->features & AHC_ULTRA2) != 0)
646 maxsync = AHC_SYNCRATE_DT;
647 else if ((ahc->features & AHC_ULTRA) != 0)
648 maxsync = AHC_SYNCRATE_ULTRA;
650 maxsync = AHC_SYNCRATE_FAST;
652 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
653 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
655 syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
658 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
659 syncrate, &spi->sync_offset,
660 spi->bus_width, ROLE_UNKNOWN);
662 /* We use a period of 0 to represent async */
663 if (spi->sync_offset == 0) {
664 spi->sync_period = 0;
665 spi->ppr_options = 0;
668 ahc_set_syncrate(ahc, &devinfo, syncrate,
669 spi->sync_period, spi->sync_offset,
670 spi->ppr_options, update_type,
674 ccb->ccb_h.status = CAM_REQ_CMP;
677 struct ahc_devinfo devinfo;
678 struct ccb_trans_settings *cts;
679 struct ahc_initiator_tinfo *tinfo;
680 struct ahc_tmode_tstate *tstate;
681 uint16_t *discenable;
686 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
687 cts->ccb_h.target_id,
688 cts->ccb_h.target_lun,
689 SIM_CHANNEL(ahc, sim),
691 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
693 devinfo.target, &tstate);
695 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
696 update_type |= AHC_TRANS_GOAL;
697 discenable = &tstate->discenable;
698 tagenable = &tstate->tagenable;
699 } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
700 update_type |= AHC_TRANS_USER;
701 discenable = &ahc->user_discenable;
702 tagenable = &ahc->user_tagenable;
704 ccb->ccb_h.status = CAM_REQ_INVALID;
711 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
712 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
713 *discenable |= devinfo.target_mask;
715 *discenable &= ~devinfo.target_mask;
718 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
719 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
720 *tagenable |= devinfo.target_mask;
722 *tagenable &= ~devinfo.target_mask;
725 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
726 ahc_validate_width(ahc, /*tinfo limit*/NULL,
727 &cts->bus_width, ROLE_UNKNOWN);
728 ahc_set_width(ahc, &devinfo, cts->bus_width,
729 update_type, /*paused*/FALSE);
732 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
733 if (update_type == AHC_TRANS_USER)
734 cts->sync_offset = tinfo->user.offset;
736 cts->sync_offset = tinfo->goal.offset;
739 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
740 if (update_type == AHC_TRANS_USER)
741 cts->sync_period = tinfo->user.period;
743 cts->sync_period = tinfo->goal.period;
746 if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
747 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)) {
748 struct ahc_syncrate *syncrate;
752 if ((ahc->features & AHC_ULTRA2) != 0)
753 maxsync = AHC_SYNCRATE_DT;
754 else if ((ahc->features & AHC_ULTRA) != 0)
755 maxsync = AHC_SYNCRATE_ULTRA;
757 maxsync = AHC_SYNCRATE_FAST;
760 if (cts->sync_period <= 9
761 && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
762 ppr_options = MSG_EXT_PPR_DT_REQ;
764 syncrate = ahc_find_syncrate(ahc, &cts->sync_period,
767 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
768 syncrate, &cts->sync_offset,
769 MSG_EXT_WDTR_BUS_8_BIT,
772 /* We use a period of 0 to represent async */
773 if (cts->sync_offset == 0) {
774 cts->sync_period = 0;
778 if (ppr_options == MSG_EXT_PPR_DT_REQ
779 && tinfo->user.transport_version >= 3) {
780 tinfo->goal.transport_version =
781 tinfo->user.transport_version;
782 tinfo->curr.transport_version =
783 tinfo->user.transport_version;
786 ahc_set_syncrate(ahc, &devinfo, syncrate,
787 cts->sync_period, cts->sync_offset,
788 ppr_options, update_type,
792 ccb->ccb_h.status = CAM_REQ_CMP;
797 case XPT_GET_TRAN_SETTINGS:
798 /* Get default/user set transfer settings for the target */
802 ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
803 SIM_CHANNEL(ahc, sim), &ccb->cts);
808 case XPT_CALC_GEOMETRY:
810 struct ccb_calc_geometry *ccg;
812 uint32_t secs_per_cylinder;
816 size_mb = ccg->volume_size
817 / ((1024L * 1024L) / ccg->block_size);
818 extended = SIM_IS_SCSIBUS_B(ahc, sim)
819 ? ahc->flags & AHC_EXTENDED_TRANS_B
820 : ahc->flags & AHC_EXTENDED_TRANS_A;
822 if (size_mb > 1024 && extended) {
824 ccg->secs_per_track = 63;
827 ccg->secs_per_track = 32;
829 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
830 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
831 ccb->ccb_h.status = CAM_REQ_CMP;
835 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
840 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
841 /*initiate reset*/TRUE);
844 xpt_print_path(SIM_PATH(ahc, sim));
845 kprintf("SCSI bus reset delivered. "
846 "%d SCBs aborted.\n", found);
848 ccb->ccb_h.status = CAM_REQ_CMP;
852 case XPT_TERM_IO: /* Terminate the I/O process */
854 ccb->ccb_h.status = CAM_REQ_INVALID;
857 case XPT_PATH_INQ: /* Path routing inquiry */
859 struct ccb_pathinq *cpi = &ccb->cpi;
861 cpi->version_num = 1; /* XXX??? */
862 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
863 if ((ahc->features & AHC_WIDE) != 0)
864 cpi->hba_inquiry |= PI_WIDE_16;
865 if ((ahc->features & AHC_TARGETMODE) != 0) {
866 cpi->target_sprt = PIT_PROCESSOR
870 cpi->target_sprt = 0;
873 cpi->hba_eng_cnt = 0;
874 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
875 cpi->max_lun = AHC_NUM_LUNS - 1;
876 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
877 cpi->initiator_id = ahc->our_id_b;
878 if ((ahc->flags & AHC_RESET_BUS_B) == 0)
879 cpi->hba_misc |= PIM_NOBUSRESET;
881 cpi->initiator_id = ahc->our_id;
882 if ((ahc->flags & AHC_RESET_BUS_A) == 0)
883 cpi->hba_misc |= PIM_NOBUSRESET;
885 cpi->bus_id = cam_sim_bus(sim);
886 cpi->base_transfer_speed = 3300;
887 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
888 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
889 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
890 cpi->unit_number = cam_sim_unit(sim);
891 #ifdef AHC_NEW_TRAN_SETTINGS
892 cpi->protocol = PROTO_SCSI;
893 cpi->protocol_version = SCSI_REV_2;
894 cpi->transport = XPORT_SPI;
895 cpi->transport_version = 2;
896 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
897 if ((ahc->features & AHC_DT) != 0) {
898 cpi->transport_version = 3;
899 cpi->xport_specific.spi.ppr_options =
903 cpi->ccb_h.status = CAM_REQ_CMP;
908 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
915 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
916 struct ccb_trans_settings *cts)
918 #ifdef AHC_NEW_TRAN_SETTINGS
919 struct ahc_devinfo devinfo;
920 struct ccb_trans_settings_scsi *scsi;
921 struct ccb_trans_settings_spi *spi;
922 struct ahc_initiator_tinfo *targ_info;
923 struct ahc_tmode_tstate *tstate;
924 struct ahc_transinfo *tinfo;
926 scsi = &cts->proto_specific.scsi;
927 spi = &cts->xport_specific.spi;
928 ahc_compile_devinfo(&devinfo, our_id,
929 cts->ccb_h.target_id,
930 cts->ccb_h.target_lun,
931 channel, ROLE_UNKNOWN);
932 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
934 devinfo.target, &tstate);
936 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
937 tinfo = &targ_info->curr;
939 tinfo = &targ_info->user;
941 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
942 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
943 if (cts->type == CTS_TYPE_USER_SETTINGS) {
944 if ((ahc->user_discenable & devinfo.target_mask) != 0)
945 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
947 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
948 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
950 if ((tstate->discenable & devinfo.target_mask) != 0)
951 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
953 if ((tstate->tagenable & devinfo.target_mask) != 0)
954 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
956 cts->protocol_version = tinfo->protocol_version;
957 cts->transport_version = tinfo->transport_version;
959 spi->sync_period = tinfo->period;
960 spi->sync_offset = tinfo->offset;
961 spi->bus_width = tinfo->width;
962 spi->ppr_options = tinfo->ppr_options;
964 cts->protocol = PROTO_SCSI;
965 cts->transport = XPORT_SPI;
966 spi->valid = CTS_SPI_VALID_SYNC_RATE
967 | CTS_SPI_VALID_SYNC_OFFSET
968 | CTS_SPI_VALID_BUS_WIDTH
969 | CTS_SPI_VALID_PPR_OPTIONS;
971 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
972 scsi->valid = CTS_SCSI_VALID_TQ;
973 spi->valid |= CTS_SPI_VALID_DISC;
978 cts->ccb_h.status = CAM_REQ_CMP;
980 struct ahc_devinfo devinfo;
981 struct ahc_initiator_tinfo *targ_info;
982 struct ahc_tmode_tstate *tstate;
983 struct ahc_transinfo *tinfo;
985 ahc_compile_devinfo(&devinfo, our_id,
986 cts->ccb_h.target_id,
987 cts->ccb_h.target_lun,
988 channel, ROLE_UNKNOWN);
989 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
991 devinfo.target, &tstate);
993 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
994 tinfo = &targ_info->curr;
996 tinfo = &targ_info->user;
998 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
999 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
1000 if ((ahc->user_discenable & devinfo.target_mask) != 0)
1001 cts->flags |= CCB_TRANS_DISC_ENB;
1003 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1004 cts->flags |= CCB_TRANS_TAG_ENB;
1006 if ((tstate->discenable & devinfo.target_mask) != 0)
1007 cts->flags |= CCB_TRANS_DISC_ENB;
1009 if ((tstate->tagenable & devinfo.target_mask) != 0)
1010 cts->flags |= CCB_TRANS_TAG_ENB;
1012 cts->sync_period = tinfo->period;
1013 cts->sync_offset = tinfo->offset;
1014 cts->bus_width = tinfo->width;
1016 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1017 | CCB_TRANS_SYNC_OFFSET_VALID
1018 | CCB_TRANS_BUS_WIDTH_VALID;
1020 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1021 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1023 cts->ccb_h.status = CAM_REQ_CMP;
1028 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1030 struct ahc_softc *ahc;
1031 struct cam_sim *sim;
1033 sim = (struct cam_sim *)callback_arg;
1034 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1036 case AC_LOST_DEVICE:
1038 struct ahc_devinfo devinfo;
1040 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1041 xpt_path_target_id(path),
1042 xpt_path_lun_id(path),
1043 SIM_CHANNEL(ahc, sim),
1047 * Revert to async/narrow transfers
1048 * for the next device.
1051 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1052 AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1053 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1054 /*period*/0, /*offset*/0, /*ppr_options*/0,
1055 AHC_TRANS_GOAL|AHC_TRANS_CUR,
1066 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1071 struct ahc_softc *ahc;
1072 struct ahc_initiator_tinfo *tinfo;
1073 struct ahc_tmode_tstate *tstate;
1076 scb = (struct scb *)arg;
1078 ahc = scb->ahc_softc;
1082 ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1084 ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1086 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1088 ahc_free_scb(ahc, scb);
1093 if (nsegments != 0) {
1094 struct ahc_dma_seg *sg;
1095 bus_dma_segment_t *end_seg;
1096 bus_dmasync_op_t op;
1098 end_seg = dm_segs + nsegments;
1100 /* Copy the segments into our SG list */
1102 while (dm_segs < end_seg) {
1105 sg->addr = ahc_htole32(dm_segs->ds_addr);
1106 len = dm_segs->ds_len
1107 | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1108 sg->len = ahc_htole32(len);
1114 * Note where to find the SG entries in bus space.
1115 * We also set the full residual flag which the
1116 * sequencer will clear as soon as a data transfer
1119 scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
1121 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1122 op = BUS_DMASYNC_PREREAD;
1124 op = BUS_DMASYNC_PREWRITE;
1126 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1128 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1129 struct target_data *tdata;
1131 tdata = &scb->hscb->shared_data.tdata;
1132 tdata->target_phases |= DPHASE_PENDING;
1133 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1134 tdata->data_phase = P_DATAOUT;
1136 tdata->data_phase = P_DATAIN;
1139 * If the transfer is of an odd length and in the
1140 * "in" direction (scsi->HostBus), then it may
1141 * trigger a bug in the 'WideODD' feature of
1142 * non-Ultra2 chips. Force the total data-length
1143 * to be even by adding an extra, 1 byte, SG,
1144 * element. We do this even if we are not currently
1145 * negotiated wide as negotiation could occur before
1146 * this command is executed.
1148 if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1149 && (ccb->csio.dxfer_len & 0x1) != 0
1150 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1153 if (nsegments > AHC_NSEG) {
1155 ahc_set_transaction_status(scb,
1157 bus_dmamap_unload(ahc->buffer_dmat,
1160 ahc_free_scb(ahc, scb);
1165 sg->addr = ahc_htole32(ahc->dma_bug_buf);
1166 sg->len = ahc_htole32(1);
1171 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1173 /* Copy the first SG into the "current" data pointer area */
1174 scb->hscb->dataptr = scb->sg_list->addr;
1175 scb->hscb->datacnt = scb->sg_list->len;
1177 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1178 scb->hscb->dataptr = 0;
1179 scb->hscb->datacnt = 0;
1182 scb->sg_count = nsegments;
1187 * Last time we need to check if this SCB needs to
1190 if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1192 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1193 ahc_free_scb(ahc, scb);
1199 tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1200 SCSIID_OUR_ID(scb->hscb->scsiid),
1201 SCSIID_TARGET(ahc, scb->hscb->scsiid),
1204 mask = SCB_GET_TARGET_MASK(ahc, scb);
1205 scb->hscb->scsirate = tinfo->scsirate;
1206 scb->hscb->scsioffset = tinfo->curr.offset;
1207 if ((tstate->ultraenb & mask) != 0)
1208 scb->hscb->control |= ULTRAENB;
1210 if ((tstate->discenable & mask) != 0
1211 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1212 scb->hscb->control |= DISCENB;
1214 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1215 && (tinfo->goal.width != 0
1216 || tinfo->goal.offset != 0
1217 || tinfo->goal.ppr_options != 0)) {
1218 scb->flags |= SCB_NEGOTIATE;
1219 scb->hscb->control |= MK_MESSAGE;
1220 } else if ((tstate->auto_negotiate & mask) != 0) {
1221 scb->flags |= SCB_AUTO_NEGOTIATE;
1222 scb->hscb->control |= MK_MESSAGE;
1225 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1227 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1229 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1232 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1233 ccb->ccb_h.timeout = 5 * 1000;
1235 time = ccb->ccb_h.timeout;
1238 callout_reset(&ccb->ccb_h.timeout_ch, time, ahc_timeout, scb);
1242 * We only allow one untagged transaction
1243 * per target in the initiator role unless
1244 * we are storing a full busy target *lun*
1245 * table in SCB space.
1247 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1248 && (ahc->flags & AHC_SCB_BTT) == 0) {
1249 struct scb_tailq *untagged_q;
1252 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1253 untagged_q = &(ahc->untagged_queues[target_offset]);
1254 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1255 scb->flags |= SCB_UNTAGGEDQ;
1256 if (TAILQ_FIRST(untagged_q) != scb) {
1261 scb->flags |= SCB_ACTIVE;
1263 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1264 /* Define a mapping from our tag to the SCB. */
1265 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1267 if ((ahc->flags & AHC_PAGESCBS) == 0)
1268 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1269 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1272 ahc_queue_scb(ahc, scb);
1279 ahc_poll(struct cam_sim *sim)
1281 struct ahc_softc *ahc;
1283 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1288 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1289 struct ccb_scsiio *csio, struct scb *scb)
1291 struct hardware_scb *hscb;
1292 struct ccb_hdr *ccb_h;
1295 ccb_h = &csio->ccb_h;
1298 csio->sense_resid = 0;
1299 if (ccb_h->func_code == XPT_SCSI_IO) {
1300 hscb->cdb_len = csio->cdb_len;
1301 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1303 if (hscb->cdb_len > sizeof(hscb->cdb32)
1304 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1305 ahc_set_transaction_status(scb,
1308 ahc_free_scb(ahc, scb);
1310 xpt_done((union ccb *)csio);
1313 if (hscb->cdb_len > 12) {
1315 csio->cdb_io.cdb_ptr,
1317 scb->flags |= SCB_CDB32_PTR;
1319 memcpy(hscb->shared_data.cdb,
1320 csio->cdb_io.cdb_ptr,
1324 if (hscb->cdb_len > 12) {
1325 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1327 scb->flags |= SCB_CDB32_PTR;
1329 memcpy(hscb->shared_data.cdb,
1330 csio->cdb_io.cdb_bytes,
1336 /* Only use S/G if there is a transfer */
1337 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1338 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1339 /* We've been given a pointer to a single buffer */
1340 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1344 error = bus_dmamap_load(ahc->buffer_dmat,
1350 if (error == EINPROGRESS) {
1352 * So as to maintain ordering,
1353 * freeze the controller queue
1354 * until our mapping is
1357 xpt_freeze_simq(sim,
1359 scb->io_ctx->ccb_h.status |=
1364 struct bus_dma_segment seg;
1366 /* Pointer to physical buffer */
1367 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1368 panic("ahc_setup_data - Transfer size "
1369 "larger than can device max");
1372 (bus_addr_t)(vm_offset_t)csio->data_ptr;
1373 seg.ds_len = csio->dxfer_len;
1374 ahc_execute_scb(scb, &seg, 1, 0);
1377 struct bus_dma_segment *segs;
1379 if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1380 panic("ahc_setup_data - Physical segment "
1381 "pointers unsupported");
1383 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1384 panic("ahc_setup_data - Virtual segment "
1385 "addresses unsupported");
1387 /* Just use the segments provided */
1388 segs = (struct bus_dma_segment *)csio->data_ptr;
1389 ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1392 ahc_execute_scb(scb, NULL, 0, 0);
1397 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1399 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1400 struct scb *list_scb;
1402 scb->flags |= SCB_RECOVERY_SCB;
1405 * Take all queued, but not sent SCBs out of the equation.
1406 * Also ensure that no new CCBs are queued to us while we
1407 * try to fix this problem.
1409 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1410 xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1411 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1415 * Go through all of our pending SCBs and remove
1416 * any scheduled timeouts for them. We will reschedule
1417 * them after we've successfully fixed this problem.
1419 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1422 ccb = list_scb->io_ctx;
1423 callout_stop(&ccb->ccb_h.timeout_ch);
1429 ahc_timeout(void *arg)
1432 struct ahc_softc *ahc;
1440 scb = (struct scb *)arg;
1441 ahc = (struct ahc_softc *)scb->ahc_softc;
1445 ahc_pause_and_flushwork(ahc);
1447 if ((scb->flags & SCB_ACTIVE) == 0) {
1448 /* Previous timeout took care of me already */
1449 kprintf("%s: Timedout SCB already complete. "
1450 "Interrupts may not be functioning.\n", ahc_name(ahc));
1456 target = SCB_GET_TARGET(ahc, scb);
1457 channel = SCB_GET_CHANNEL(ahc, scb);
1458 lun = SCB_GET_LUN(scb);
1460 ahc_print_path(ahc, scb);
1461 kprintf("SCB 0x%x - timed out\n", scb->hscb->tag);
1462 ahc_dump_card_state(ahc);
1463 last_phase = ahc_inb(ahc, LASTPHASE);
1464 if (scb->sg_count > 0) {
1465 for (i = 0; i < scb->sg_count; i++) {
1466 kprintf("sg[%d] - Addr 0x%x : Length %d\n",
1468 scb->sg_list[i].addr,
1469 scb->sg_list[i].len & AHC_SG_LEN_MASK);
1472 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1474 * Been down this road before.
1475 * Do a full bus reset.
1478 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1479 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1480 kprintf("%s: Issued Channel %c Bus Reset. "
1481 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1484 * If we are a target, transition to bus free and report
1487 * The target/initiator that is holding up the bus may not
1488 * be the same as the one that triggered this timeout
1489 * (different commands have different timeout lengths).
1490 * If the bus is idle and we are actiing as the initiator
1491 * for this request, queue a BDR message to the timed out
1492 * target. Otherwise, if the timed out transaction is
1494 * Initiator transaction:
1495 * Stuff the message buffer with a BDR message and assert
1496 * ATN in the hopes that the target will let go of the bus
1497 * and go to the mesgout phase. If this fails, we'll
1498 * get another timeout 2 seconds later which will attempt
1501 * Target transaction:
1502 * Transition to BUS FREE and report the error.
1503 * It's good to be the target!
1505 u_int active_scb_index;
1508 saved_scbptr = ahc_inb(ahc, SCBPTR);
1509 active_scb_index = ahc_inb(ahc, SCB_TAG);
1511 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1512 && (active_scb_index < ahc->scb_data->numscbs)) {
1513 struct scb *active_scb;
1516 * If the active SCB is not us, assume that
1517 * the active SCB has a longer timeout than
1518 * the timedout SCB, and wait for the active
1521 active_scb = ahc_lookup_scb(ahc, active_scb_index);
1522 if (active_scb != scb) {
1523 struct ccb_hdr *ccbh;
1524 uint64_t newtimeout;
1526 ahc_print_path(ahc, scb);
1527 kprintf("Other SCB Timeout%s",
1528 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1529 ? " again\n" : "\n");
1530 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1532 MAX(active_scb->io_ctx->ccb_h.timeout,
1533 scb->io_ctx->ccb_h.timeout);
1536 ccbh = &scb->io_ctx->ccb_h;
1537 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1538 newtimeout, ahc_timeout, scb);
1545 if ((scb->flags & SCB_TARGET_SCB) != 0) {
1548 * Send back any queued up transactions
1549 * and properly record the error condition.
1551 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1552 SCB_GET_CHANNEL(ahc, scb),
1558 /* Will clear us from the bus */
1564 ahc_set_recoveryscb(ahc, active_scb);
1565 ahc_outb(ahc, MSG_OUT, HOST_MSG);
1566 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1567 ahc_print_path(ahc, active_scb);
1568 kprintf("BDR message in message buffer\n");
1569 active_scb->flags |= SCB_DEVICE_RESET;
1570 callout_reset(&active_scb->io_ctx->ccb_h.timeout_ch,
1571 2 * hz, ahc_timeout, active_scb);
1576 /* XXX Shouldn't panic. Just punt instead? */
1577 if ((scb->flags & SCB_TARGET_SCB) != 0)
1578 panic("Timed-out target SCB but bus idle");
1580 if (last_phase != P_BUSFREE
1581 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1582 /* XXX What happened to the SCB? */
1583 /* Hung target selection. Goto busfree */
1584 kprintf("%s: Hung target selection\n",
1591 if (ahc_search_qinfifo(ahc, target, channel, lun,
1592 scb->hscb->tag, ROLE_INITIATOR,
1593 /*status*/0, SEARCH_COUNT) > 0) {
1594 disconnected = FALSE;
1596 disconnected = TRUE;
1601 ahc_set_recoveryscb(ahc, scb);
1603 * Actually re-queue this SCB in an attempt
1604 * to select the device before it reconnects.
1605 * In either case (selection or reselection),
1606 * we will now issue a target reset to the
1609 * Set the MK_MESSAGE control bit indicating
1610 * that we desire to send a message. We
1611 * also set the disconnected flag since
1612 * in the paging case there is no guarantee
1613 * that our SCB control byte matches the
1614 * version on the card. We don't want the
1615 * sequencer to abort the command thinking
1616 * an unsolicited reselection occurred.
1618 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1619 scb->flags |= SCB_DEVICE_RESET;
1622 * Remove any cached copy of this SCB in the
1623 * disconnected list in preparation for the
1624 * queuing of our abort SCB. We use the
1625 * same element in the SCB, SCB_NEXT, for
1626 * both the qinfifo and the disconnected list.
1628 ahc_search_disc_list(ahc, target, channel,
1629 lun, scb->hscb->tag,
1630 /*stop_on_first*/TRUE,
1632 /*save_state*/FALSE);
1635 * In the non-paging case, the sequencer will
1636 * never re-reference the in-core SCB.
1637 * To make sure we are notified during
1638 * reslection, set the MK_MESSAGE flag in
1639 * the card's copy of the SCB.
1641 if ((ahc->flags & AHC_PAGESCBS) == 0) {
1642 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1643 ahc_outb(ahc, SCB_CONTROL,
1644 ahc_inb(ahc, SCB_CONTROL)
1649 * Clear out any entries in the QINFIFO first
1650 * so we are the next SCB for this target
1653 ahc_search_qinfifo(ahc,
1654 SCB_GET_TARGET(ahc, scb),
1655 channel, SCB_GET_LUN(scb),
1660 ahc_print_path(ahc, scb);
1661 kprintf("Queuing a BDR SCB\n");
1662 ahc_qinfifo_requeue_tail(ahc, scb);
1663 ahc_outb(ahc, SCBPTR, saved_scbptr);
1664 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1665 2 * hz, ahc_timeout, scb);
1668 /* Go "immediatly" to the bus reset */
1669 /* This shouldn't happen */
1670 ahc_set_recoveryscb(ahc, scb);
1671 ahc_print_path(ahc, scb);
1672 kprintf("SCB %d: Immediate reset. "
1673 "Flags = 0x%x\n", scb->hscb->tag,
1683 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1685 union ccb *abort_ccb;
1687 abort_ccb = ccb->cab.abort_ccb;
1688 switch (abort_ccb->ccb_h.func_code) {
1689 case XPT_ACCEPT_TARGET_IO:
1690 case XPT_IMMED_NOTIFY:
1691 case XPT_CONT_TARGET_IO:
1693 struct ahc_tmode_tstate *tstate;
1694 struct ahc_tmode_lstate *lstate;
1695 struct ccb_hdr_slist *list;
1698 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1701 if (status != CAM_REQ_CMP) {
1702 ccb->ccb_h.status = status;
1706 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1707 list = &lstate->accept_tios;
1708 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1709 list = &lstate->immed_notifies;
1714 struct ccb_hdr *curelm;
1717 curelm = SLIST_FIRST(list);
1719 if (curelm == &abort_ccb->ccb_h) {
1721 SLIST_REMOVE_HEAD(list, sim_links.sle);
1723 while(curelm != NULL) {
1724 struct ccb_hdr *nextelm;
1727 SLIST_NEXT(curelm, sim_links.sle);
1729 if (nextelm == &abort_ccb->ccb_h) {
1742 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1743 xpt_done(abort_ccb);
1744 ccb->ccb_h.status = CAM_REQ_CMP;
1746 xpt_print_path(abort_ccb->ccb_h.path);
1747 kprintf("Not found\n");
1748 ccb->ccb_h.status = CAM_PATH_INVALID;
1755 /* XXX Fully implement the hard ones */
1756 ccb->ccb_h.status = CAM_UA_ABORT;
1759 ccb->ccb_h.status = CAM_REQ_INVALID;
1766 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1767 u_int lun, ac_code code, void *opt_arg)
1769 struct ccb_trans_settings cts;
1770 struct cam_path *path;
1775 error = ahc_create_path(ahc, channel, target, lun, &path);
1777 if (error != CAM_REQ_CMP)
1781 case AC_TRANSFER_NEG:
1783 #ifdef AHC_NEW_TRAN_SETTINGS
1784 struct ccb_trans_settings_scsi *scsi;
1786 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1787 scsi = &cts.proto_specific.scsi;
1789 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1791 cts.ccb_h.path = path;
1792 cts.ccb_h.target_id = target;
1793 cts.ccb_h.target_lun = lun;
1794 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1798 #ifdef AHC_NEW_TRAN_SETTINGS
1799 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1800 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1802 cts.valid &= ~CCB_TRANS_TQ_VALID;
1803 cts.flags &= ~CCB_TRANS_TAG_ENB;
1805 if (opt_arg == NULL)
1807 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1808 #ifdef AHC_NEW_TRAN_SETTINGS
1809 scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1810 scsi->valid |= CTS_SCSI_VALID_TQ;
1812 cts.flags |= CCB_TRANS_TAG_ENB;
1813 cts.valid |= CCB_TRANS_TQ_VALID;
1821 panic("ahc_send_async: Unexpected async event");
1823 xpt_async(code, path, arg);
1824 xpt_free_path(path);
1828 ahc_platform_set_tags(struct ahc_softc *ahc,
1829 struct ahc_devinfo *devinfo, int enable)
1834 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1836 ahc->platform_data = kmalloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1837 M_INTWAIT | M_ZERO);
1842 ahc_platform_free(struct ahc_softc *ahc)
1844 struct ahc_platform_data *pdata;
1846 pdata = ahc->platform_data;
1847 if (pdata != NULL) {
1848 if (pdata->regs != NULL)
1849 bus_release_resource(ahc->dev_softc,
1850 pdata->regs_res_type,
1854 if (pdata->irq != NULL)
1855 bus_release_resource(ahc->dev_softc,
1856 pdata->irq_res_type,
1859 if (pdata->sim_b != NULL) {
1860 xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1861 xpt_free_path(pdata->path_b);
1862 xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1863 cam_sim_free(pdata->sim_b);
1865 if (pdata->sim != NULL) {
1866 xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1867 xpt_free_path(pdata->path);
1868 xpt_bus_deregister(cam_sim_path(pdata->sim));
1869 cam_sim_free(pdata->sim);
1871 if (pdata->eh != NULL)
1872 EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1873 kfree(ahc->platform_data, M_DEVBUF);
1878 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1880 /* We don't sort softcs under FreeBSD so report equal always */
1885 ahc_detach(device_t dev)
1887 struct ahc_softc *ahc;
1889 device_printf(dev, "detaching device\n");
1890 ahc = device_get_softc(dev);
1891 ahc = ahc_find_softc(ahc);
1893 device_printf(dev, "aic7xxx already detached\n");
1897 ahc_intr_enable(ahc, FALSE);
1898 bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1906 ahc_dump_targcmd(struct target_cmd *cmd)
1912 byte = &cmd->initiator_channel;
1913 /* Debugging info for received commands */
1914 last_byte = &cmd[1].initiator_channel;
1917 while (byte < last_byte) {
1920 kprintf("%#x", *byte++);
1933 ahc_modevent(module_t mod, int type, void *data)
1935 /* XXX Deal with busy status on unload. */
1939 static moduledata_t ahc_mod = {
1945 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1946 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1947 MODULE_VERSION(ahc, 1);