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.2 2003/06/17 04:28:22 dillon Exp $
37 #include <dev/aic7xxx/aic7xxx_osm.h>
38 #include <dev/aic7xxx/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 INTR_TYPE_CAM, ahc_platform_intr, ahc,
94 &ahc->platform_data->ih);
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;
110 struct cam_devq *devq;
114 struct cam_sim *sim2;
115 struct cam_path *path;
116 struct cam_path *path2;
124 ahc_controller_info(ahc, ahc_info);
125 printf("%s\n", ahc_info);
128 * Attach secondary channel first if the user has
129 * declared it the primary channel.
131 if ((ahc->features & AHC_TWIN) != 0
132 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
141 * Create the device queue for our SIM(s).
143 devq = cam_simq_alloc(AHC_MAX_QUEUE);
148 * Construct our first channel SIM entry
150 sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
151 device_get_unit(ahc->dev_softc),
152 1, AHC_MAX_QUEUE, devq);
158 if (xpt_bus_register(sim, bus_id) != CAM_SUCCESS) {
159 cam_sim_free(sim, /*free_devq*/TRUE);
164 if (xpt_create_path(&path, /*periph*/NULL,
165 cam_sim_path(sim), CAM_TARGET_WILDCARD,
166 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
167 xpt_bus_deregister(cam_sim_path(sim));
168 cam_sim_free(sim, /*free_devq*/TRUE);
173 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
174 csa.ccb_h.func_code = XPT_SASYNC_CB;
175 csa.event_enable = AC_LOST_DEVICE;
176 csa.callback = ahc_async;
177 csa.callback_arg = sim;
178 xpt_action((union ccb *)&csa);
181 if (ahc->features & AHC_TWIN) {
182 sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
183 ahc, device_get_unit(ahc->dev_softc), 1,
184 AHC_MAX_QUEUE, devq);
187 printf("ahc_attach: Unable to attach second "
188 "bus due to resource shortage");
192 if (xpt_bus_register(sim2, bus_id2) != CAM_SUCCESS) {
193 printf("ahc_attach: Unable to attach second "
194 "bus due to resource shortage");
196 * We do not want to destroy the device queue
197 * because the first bus is using it.
199 cam_sim_free(sim2, /*free_devq*/FALSE);
203 if (xpt_create_path(&path2, /*periph*/NULL,
206 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
207 xpt_bus_deregister(cam_sim_path(sim2));
208 cam_sim_free(sim2, /*free_devq*/FALSE);
212 xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
213 csa.ccb_h.func_code = XPT_SASYNC_CB;
214 csa.event_enable = AC_LOST_DEVICE;
215 csa.callback = ahc_async;
216 csa.callback_arg = sim2;
217 xpt_action((union ccb *)&csa);
222 if ((ahc->features & AHC_TWIN) != 0
223 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
224 ahc->platform_data->sim_b = sim;
225 ahc->platform_data->path_b = path;
226 ahc->platform_data->sim = sim2;
227 ahc->platform_data->path = path2;
229 ahc->platform_data->sim = sim;
230 ahc->platform_data->path = path;
231 ahc->platform_data->sim_b = sim2;
232 ahc->platform_data->path_b = path2;
236 /* We have to wait until after any system dumps... */
237 ahc->platform_data->eh =
238 EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
239 ahc, SHUTDOWN_PRI_DEFAULT);
240 ahc_intr_enable(ahc, TRUE);
248 * Catch an interrupt from the adapter
251 ahc_platform_intr(void *arg)
253 struct ahc_softc *ahc;
255 ahc = (struct ahc_softc *)arg;
260 * We have an scb which has been processed by the
261 * adaptor, now we look to see how the operation
265 ahc_done(struct ahc_softc *ahc, struct scb *scb)
269 CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
270 ("ahc_done - scb %d\n", scb->hscb->tag));
273 LIST_REMOVE(scb, pending_links);
274 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
275 struct scb_tailq *untagged_q;
278 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
279 untagged_q = &ahc->untagged_queues[target_offset];
280 TAILQ_REMOVE(untagged_q, scb, links.tqe);
281 scb->flags &= ~SCB_UNTAGGEDQ;
282 ahc_run_untagged_queue(ahc, untagged_q);
285 untimeout(ahc_timeout, (caddr_t)scb, ccb->ccb_h.timeout_ch);
287 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
290 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
291 op = BUS_DMASYNC_POSTREAD;
293 op = BUS_DMASYNC_POSTWRITE;
294 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
295 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
298 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
299 struct cam_path *ccb_path;
302 * If we have finally disconnected, clean up our
303 * pending device state.
304 * XXX - There may be error states that cause where
305 * we will remain connected.
307 ccb_path = ccb->ccb_h.path;
308 if (ahc->pending_device != NULL
309 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
311 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
312 ahc->pending_device = NULL;
315 xpt_print_path(ccb->ccb_h.path);
316 printf("Still connected\n");
322 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG)
323 ccb->ccb_h.status |= CAM_REQ_CMP;
324 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
325 ahc_free_scb(ahc, scb);
331 * If the recovery SCB completes, we have to be
332 * out of our timeout.
334 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
335 struct scb *list_scb;
338 * We were able to complete the command successfully,
339 * so reinstate the timeouts for all other pending
342 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
346 ccb = list_scb->io_ctx;
347 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
350 time = ccb->ccb_h.timeout;
353 ccb->ccb_h.timeout_ch =
354 timeout(ahc_timeout, list_scb, time);
357 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
358 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
359 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
360 ahc_print_path(ahc, scb);
361 printf("no longer in timeout, status = %x\n",
365 /* Don't clobber any existing error state */
366 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
367 ccb->ccb_h.status |= CAM_REQ_CMP;
368 } else if ((scb->flags & SCB_SENSE) != 0) {
370 * We performed autosense retrieval.
372 * Zero any sense not transferred by the
373 * device. The SCSI spec mandates that any
374 * untransfered data should be assumed to be
375 * zero. Complete the 'bounce' of sense information
376 * through buffers accessible via bus-space by
377 * copying it into the clients csio.
379 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
380 memcpy(&ccb->csio.sense_data,
381 ahc_get_sense_buf(ahc, scb),
382 (ahc_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
383 - ccb->csio.sense_resid);
384 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
386 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
387 ahc_free_scb(ahc, scb);
392 ahc_action(struct cam_sim *sim, union ccb *ccb)
394 struct ahc_softc *ahc;
395 struct ahc_tmode_lstate *lstate;
400 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
402 ahc = (struct ahc_softc *)cam_sim_softc(sim);
404 target_id = ccb->ccb_h.target_id;
405 our_id = SIM_SCSI_ID(ahc, sim);
407 switch (ccb->ccb_h.func_code) {
408 /* Common cases first */
409 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
410 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
412 struct ahc_tmode_tstate *tstate;
415 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
418 if (status != CAM_REQ_CMP) {
419 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
420 /* Response from the black hole device */
422 lstate = ahc->black_hole;
424 ccb->ccb_h.status = status;
429 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
432 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
434 ccb->ccb_h.status = CAM_REQ_INPROG;
435 if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
436 ahc_run_tqinfifo(ahc, /*paused*/FALSE);
442 * The target_id represents the target we attempt to
443 * select. In target mode, this is the initiator of
444 * the original command.
447 target_id = ccb->csio.init_id;
450 case XPT_SCSI_IO: /* Execute the requested I/O operation */
451 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
454 struct hardware_scb *hscb;
456 if ((ahc->flags & AHC_INITIATORROLE) == 0
457 && (ccb->ccb_h.func_code == XPT_SCSI_IO
458 || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
459 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
468 if ((scb = ahc_get_scb(ahc)) == NULL) {
470 xpt_freeze_simq(sim, /*count*/1);
471 ahc->flags |= AHC_RESOURCE_SHORTAGE;
473 ccb->ccb_h.status = CAM_REQUEUE_REQ;
481 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
482 ("start scb(%p)\n", scb));
485 * So we can find the SCB when an abort is requested
487 ccb->ccb_h.ccb_scb_ptr = scb;
490 * Put all the arguments for the xfer in the scb
493 hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
494 hscb->lun = ccb->ccb_h.target_lun;
495 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
497 scb->flags |= SCB_DEVICE_RESET;
498 hscb->control |= MK_MESSAGE;
499 ahc_execute_scb(scb, NULL, 0, 0);
501 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
502 struct target_data *tdata;
504 tdata = &hscb->shared_data.tdata;
505 if (ahc->pending_device == lstate)
506 scb->flags |= SCB_TARGET_IMMEDIATE;
507 hscb->control |= TARGET_SCB;
508 scb->flags |= SCB_TARGET_SCB;
509 tdata->target_phases = 0;
510 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
511 tdata->target_phases |= SPHASE_PENDING;
513 ccb->csio.scsi_status;
515 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
516 tdata->target_phases |= NO_DISCONNECT;
518 tdata->initiator_tag = ccb->csio.tag_id;
520 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
521 hscb->control |= ccb->csio.tag_action;
523 ahc_setup_data(ahc, sim, &ccb->csio, scb);
528 case XPT_IMMED_NOTIFY:
530 struct ahc_tmode_tstate *tstate;
531 struct ahc_tmode_lstate *lstate;
534 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
537 if (status != CAM_REQ_CMP) {
538 ccb->ccb_h.status = status;
542 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
544 ccb->ccb_h.status = CAM_REQ_INPROG;
545 ahc_send_lstate_events(ahc, lstate);
548 case XPT_EN_LUN: /* Enable LUN as a target */
549 ahc_handle_en_lun(ahc, sim, ccb);
552 case XPT_ABORT: /* Abort the specified CCB */
554 ahc_abort_ccb(ahc, sim, ccb);
557 case XPT_SET_TRAN_SETTINGS:
559 #ifdef AHC_NEW_TRAN_SETTINGS
560 struct ahc_devinfo devinfo;
561 struct ccb_trans_settings *cts;
562 struct ccb_trans_settings_scsi *scsi;
563 struct ccb_trans_settings_spi *spi;
564 struct ahc_initiator_tinfo *tinfo;
565 struct ahc_tmode_tstate *tstate;
566 uint16_t *discenable;
571 scsi = &cts->proto_specific.scsi;
572 spi = &cts->xport_specific.spi;
573 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
574 cts->ccb_h.target_id,
575 cts->ccb_h.target_lun,
576 SIM_CHANNEL(ahc, sim),
578 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
580 devinfo.target, &tstate);
582 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
583 update_type |= AHC_TRANS_GOAL;
584 discenable = &tstate->discenable;
585 tagenable = &tstate->tagenable;
586 tinfo->curr.protocol_version =
587 cts->protocol_version;
588 tinfo->curr.transport_version =
589 cts->transport_version;
590 tinfo->goal.protocol_version =
591 cts->protocol_version;
592 tinfo->goal.transport_version =
593 cts->transport_version;
594 } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
595 update_type |= AHC_TRANS_USER;
596 discenable = &ahc->user_discenable;
597 tagenable = &ahc->user_tagenable;
598 tinfo->user.protocol_version =
599 cts->protocol_version;
600 tinfo->user.transport_version =
601 cts->transport_version;
603 ccb->ccb_h.status = CAM_REQ_INVALID;
610 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
611 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
612 *discenable |= devinfo.target_mask;
614 *discenable &= ~devinfo.target_mask;
617 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
618 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
619 *tagenable |= devinfo.target_mask;
621 *tagenable &= ~devinfo.target_mask;
624 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
625 ahc_validate_width(ahc, /*tinfo limit*/NULL,
626 &spi->bus_width, ROLE_UNKNOWN);
627 ahc_set_width(ahc, &devinfo, spi->bus_width,
628 update_type, /*paused*/FALSE);
631 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
632 if (update_type == AHC_TRANS_USER)
633 spi->ppr_options = tinfo->user.ppr_options;
635 spi->ppr_options = tinfo->goal.ppr_options;
638 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
639 if (update_type == AHC_TRANS_USER)
640 spi->sync_offset = tinfo->user.offset;
642 spi->sync_offset = tinfo->goal.offset;
645 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
646 if (update_type == AHC_TRANS_USER)
647 spi->sync_period = tinfo->user.period;
649 spi->sync_period = tinfo->goal.period;
652 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
653 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
654 struct ahc_syncrate *syncrate;
657 if ((ahc->features & AHC_ULTRA2) != 0)
658 maxsync = AHC_SYNCRATE_DT;
659 else if ((ahc->features & AHC_ULTRA) != 0)
660 maxsync = AHC_SYNCRATE_ULTRA;
662 maxsync = AHC_SYNCRATE_FAST;
664 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
665 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
667 syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
670 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
671 syncrate, &spi->sync_offset,
672 spi->bus_width, ROLE_UNKNOWN);
674 /* We use a period of 0 to represent async */
675 if (spi->sync_offset == 0) {
676 spi->sync_period = 0;
677 spi->ppr_options = 0;
680 ahc_set_syncrate(ahc, &devinfo, syncrate,
681 spi->sync_period, spi->sync_offset,
682 spi->ppr_options, update_type,
686 ccb->ccb_h.status = CAM_REQ_CMP;
689 struct ahc_devinfo devinfo;
690 struct ccb_trans_settings *cts;
691 struct ahc_initiator_tinfo *tinfo;
692 struct ahc_tmode_tstate *tstate;
693 uint16_t *discenable;
699 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
700 cts->ccb_h.target_id,
701 cts->ccb_h.target_lun,
702 SIM_CHANNEL(ahc, sim),
704 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
706 devinfo.target, &tstate);
708 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
709 update_type |= AHC_TRANS_GOAL;
710 discenable = &tstate->discenable;
711 tagenable = &tstate->tagenable;
712 } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
713 update_type |= AHC_TRANS_USER;
714 discenable = &ahc->user_discenable;
715 tagenable = &ahc->user_tagenable;
717 ccb->ccb_h.status = CAM_REQ_INVALID;
724 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
725 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
726 *discenable |= devinfo.target_mask;
728 *discenable &= ~devinfo.target_mask;
731 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
732 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
733 *tagenable |= devinfo.target_mask;
735 *tagenable &= ~devinfo.target_mask;
738 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
739 ahc_validate_width(ahc, /*tinfo limit*/NULL,
740 &cts->bus_width, ROLE_UNKNOWN);
741 ahc_set_width(ahc, &devinfo, cts->bus_width,
742 update_type, /*paused*/FALSE);
745 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
746 if (update_type == AHC_TRANS_USER)
747 cts->sync_offset = tinfo->user.offset;
749 cts->sync_offset = tinfo->goal.offset;
752 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
753 if (update_type == AHC_TRANS_USER)
754 cts->sync_period = tinfo->user.period;
756 cts->sync_period = tinfo->goal.period;
759 if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
760 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)) {
761 struct ahc_syncrate *syncrate;
765 if ((ahc->features & AHC_ULTRA2) != 0)
766 maxsync = AHC_SYNCRATE_DT;
767 else if ((ahc->features & AHC_ULTRA) != 0)
768 maxsync = AHC_SYNCRATE_ULTRA;
770 maxsync = AHC_SYNCRATE_FAST;
773 if (cts->sync_period <= 9
774 && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
775 ppr_options = MSG_EXT_PPR_DT_REQ;
777 syncrate = ahc_find_syncrate(ahc, &cts->sync_period,
780 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
781 syncrate, &cts->sync_offset,
782 MSG_EXT_WDTR_BUS_8_BIT,
785 /* We use a period of 0 to represent async */
786 if (cts->sync_offset == 0) {
787 cts->sync_period = 0;
791 if (ppr_options == MSG_EXT_PPR_DT_REQ
792 && tinfo->user.transport_version >= 3) {
793 tinfo->goal.transport_version =
794 tinfo->user.transport_version;
795 tinfo->curr.transport_version =
796 tinfo->user.transport_version;
799 ahc_set_syncrate(ahc, &devinfo, syncrate,
800 cts->sync_period, cts->sync_offset,
801 ppr_options, update_type,
805 ccb->ccb_h.status = CAM_REQ_CMP;
810 case XPT_GET_TRAN_SETTINGS:
811 /* Get default/user set transfer settings for the target */
815 ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
816 SIM_CHANNEL(ahc, sim), &ccb->cts);
821 case XPT_CALC_GEOMETRY:
823 struct ccb_calc_geometry *ccg;
825 uint32_t secs_per_cylinder;
829 size_mb = ccg->volume_size
830 / ((1024L * 1024L) / ccg->block_size);
831 extended = SIM_IS_SCSIBUS_B(ahc, sim)
832 ? ahc->flags & AHC_EXTENDED_TRANS_B
833 : ahc->flags & AHC_EXTENDED_TRANS_A;
835 if (size_mb > 1024 && extended) {
837 ccg->secs_per_track = 63;
840 ccg->secs_per_track = 32;
842 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
843 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
844 ccb->ccb_h.status = CAM_REQ_CMP;
848 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
853 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
854 /*initiate reset*/TRUE);
857 xpt_print_path(SIM_PATH(ahc, sim));
858 printf("SCSI bus reset delivered. "
859 "%d SCBs aborted.\n", found);
861 ccb->ccb_h.status = CAM_REQ_CMP;
865 case XPT_TERM_IO: /* Terminate the I/O process */
867 ccb->ccb_h.status = CAM_REQ_INVALID;
870 case XPT_PATH_INQ: /* Path routing inquiry */
872 struct ccb_pathinq *cpi = &ccb->cpi;
874 cpi->version_num = 1; /* XXX??? */
875 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
876 if ((ahc->features & AHC_WIDE) != 0)
877 cpi->hba_inquiry |= PI_WIDE_16;
878 if ((ahc->features & AHC_TARGETMODE) != 0) {
879 cpi->target_sprt = PIT_PROCESSOR
883 cpi->target_sprt = 0;
886 cpi->hba_eng_cnt = 0;
887 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
888 cpi->max_lun = AHC_NUM_LUNS - 1;
889 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
890 cpi->initiator_id = ahc->our_id_b;
891 if ((ahc->flags & AHC_RESET_BUS_B) == 0)
892 cpi->hba_misc |= PIM_NOBUSRESET;
894 cpi->initiator_id = ahc->our_id;
895 if ((ahc->flags & AHC_RESET_BUS_A) == 0)
896 cpi->hba_misc |= PIM_NOBUSRESET;
898 cpi->bus_id = cam_sim_bus(sim);
899 cpi->base_transfer_speed = 3300;
900 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
901 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
902 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
903 cpi->unit_number = cam_sim_unit(sim);
904 #ifdef AHC_NEW_TRAN_SETTINGS
905 cpi->protocol = PROTO_SCSI;
906 cpi->protocol_version = SCSI_REV_2;
907 cpi->transport = XPORT_SPI;
908 cpi->transport_version = 2;
909 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
910 if ((ahc->features & AHC_DT) != 0) {
911 cpi->transport_version = 3;
912 cpi->xport_specific.spi.ppr_options =
916 cpi->ccb_h.status = CAM_REQ_CMP;
921 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
928 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
929 struct ccb_trans_settings *cts)
931 #ifdef AHC_NEW_TRAN_SETTINGS
932 struct ahc_devinfo devinfo;
933 struct ccb_trans_settings_scsi *scsi;
934 struct ccb_trans_settings_spi *spi;
935 struct ahc_initiator_tinfo *targ_info;
936 struct ahc_tmode_tstate *tstate;
937 struct ahc_transinfo *tinfo;
939 scsi = &cts->proto_specific.scsi;
940 spi = &cts->xport_specific.spi;
941 ahc_compile_devinfo(&devinfo, our_id,
942 cts->ccb_h.target_id,
943 cts->ccb_h.target_lun,
944 channel, ROLE_UNKNOWN);
945 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
947 devinfo.target, &tstate);
949 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
950 tinfo = &targ_info->curr;
952 tinfo = &targ_info->user;
954 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
955 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
956 if (cts->type == CTS_TYPE_USER_SETTINGS) {
957 if ((ahc->user_discenable & devinfo.target_mask) != 0)
958 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
960 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
961 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
963 if ((tstate->discenable & devinfo.target_mask) != 0)
964 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
966 if ((tstate->tagenable & devinfo.target_mask) != 0)
967 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
969 cts->protocol_version = tinfo->protocol_version;
970 cts->transport_version = tinfo->transport_version;
972 spi->sync_period = tinfo->period;
973 spi->sync_offset = tinfo->offset;
974 spi->bus_width = tinfo->width;
975 spi->ppr_options = tinfo->ppr_options;
977 cts->protocol = PROTO_SCSI;
978 cts->transport = XPORT_SPI;
979 spi->valid = CTS_SPI_VALID_SYNC_RATE
980 | CTS_SPI_VALID_SYNC_OFFSET
981 | CTS_SPI_VALID_BUS_WIDTH
982 | CTS_SPI_VALID_PPR_OPTIONS;
984 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
985 scsi->valid = CTS_SCSI_VALID_TQ;
986 spi->valid |= CTS_SPI_VALID_DISC;
991 cts->ccb_h.status = CAM_REQ_CMP;
993 struct ahc_devinfo devinfo;
994 struct ahc_initiator_tinfo *targ_info;
995 struct ahc_tmode_tstate *tstate;
996 struct ahc_transinfo *tinfo;
998 ahc_compile_devinfo(&devinfo, our_id,
999 cts->ccb_h.target_id,
1000 cts->ccb_h.target_lun,
1001 channel, ROLE_UNKNOWN);
1002 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
1004 devinfo.target, &tstate);
1006 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
1007 tinfo = &targ_info->curr;
1009 tinfo = &targ_info->user;
1011 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
1012 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
1013 if ((ahc->user_discenable & devinfo.target_mask) != 0)
1014 cts->flags |= CCB_TRANS_DISC_ENB;
1016 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1017 cts->flags |= CCB_TRANS_TAG_ENB;
1019 if ((tstate->discenable & devinfo.target_mask) != 0)
1020 cts->flags |= CCB_TRANS_DISC_ENB;
1022 if ((tstate->tagenable & devinfo.target_mask) != 0)
1023 cts->flags |= CCB_TRANS_TAG_ENB;
1025 cts->sync_period = tinfo->period;
1026 cts->sync_offset = tinfo->offset;
1027 cts->bus_width = tinfo->width;
1029 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1030 | CCB_TRANS_SYNC_OFFSET_VALID
1031 | CCB_TRANS_BUS_WIDTH_VALID;
1033 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1034 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1036 cts->ccb_h.status = CAM_REQ_CMP;
1041 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1043 struct ahc_softc *ahc;
1044 struct cam_sim *sim;
1046 sim = (struct cam_sim *)callback_arg;
1047 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1049 case AC_LOST_DEVICE:
1051 struct ahc_devinfo devinfo;
1054 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1055 xpt_path_target_id(path),
1056 xpt_path_lun_id(path),
1057 SIM_CHANNEL(ahc, sim),
1061 * Revert to async/narrow transfers
1062 * for the next device.
1065 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1066 AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1067 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1068 /*period*/0, /*offset*/0, /*ppr_options*/0,
1069 AHC_TRANS_GOAL|AHC_TRANS_CUR,
1071 ahc_unlock(ahc, &s);
1080 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1085 struct ahc_softc *ahc;
1086 struct ahc_initiator_tinfo *tinfo;
1087 struct ahc_tmode_tstate *tstate;
1091 scb = (struct scb *)arg;
1093 ahc = scb->ahc_softc;
1097 ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1099 ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1101 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1103 ahc_free_scb(ahc, scb);
1104 ahc_unlock(ahc, &s);
1108 if (nsegments != 0) {
1109 struct ahc_dma_seg *sg;
1110 bus_dma_segment_t *end_seg;
1111 bus_dmasync_op_t op;
1113 end_seg = dm_segs + nsegments;
1115 /* Copy the segments into our SG list */
1117 while (dm_segs < end_seg) {
1120 sg->addr = ahc_htole32(dm_segs->ds_addr);
1121 len = dm_segs->ds_len
1122 | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1123 sg->len = ahc_htole32(len);
1129 * Note where to find the SG entries in bus space.
1130 * We also set the full residual flag which the
1131 * sequencer will clear as soon as a data transfer
1134 scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
1136 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1137 op = BUS_DMASYNC_PREREAD;
1139 op = BUS_DMASYNC_PREWRITE;
1141 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1143 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1144 struct target_data *tdata;
1146 tdata = &scb->hscb->shared_data.tdata;
1147 tdata->target_phases |= DPHASE_PENDING;
1148 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1149 tdata->data_phase = P_DATAOUT;
1151 tdata->data_phase = P_DATAIN;
1154 * If the transfer is of an odd length and in the
1155 * "in" direction (scsi->HostBus), then it may
1156 * trigger a bug in the 'WideODD' feature of
1157 * non-Ultra2 chips. Force the total data-length
1158 * to be even by adding an extra, 1 byte, SG,
1159 * element. We do this even if we are not currently
1160 * negotiated wide as negotiation could occur before
1161 * this command is executed.
1163 if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1164 && (ccb->csio.dxfer_len & 0x1) != 0
1165 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1168 if (nsegments > AHC_NSEG) {
1170 ahc_set_transaction_status(scb,
1172 bus_dmamap_unload(ahc->buffer_dmat,
1175 ahc_free_scb(ahc, scb);
1176 ahc_unlock(ahc, &s);
1180 sg->addr = ahc_htole32(ahc->dma_bug_buf);
1181 sg->len = ahc_htole32(1);
1186 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1188 /* Copy the first SG into the "current" data pointer area */
1189 scb->hscb->dataptr = scb->sg_list->addr;
1190 scb->hscb->datacnt = scb->sg_list->len;
1192 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1193 scb->hscb->dataptr = 0;
1194 scb->hscb->datacnt = 0;
1197 scb->sg_count = nsegments;
1202 * Last time we need to check if this SCB needs to
1205 if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1207 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1208 ahc_free_scb(ahc, scb);
1209 ahc_unlock(ahc, &s);
1214 tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1215 SCSIID_OUR_ID(scb->hscb->scsiid),
1216 SCSIID_TARGET(ahc, scb->hscb->scsiid),
1219 mask = SCB_GET_TARGET_MASK(ahc, scb);
1220 scb->hscb->scsirate = tinfo->scsirate;
1221 scb->hscb->scsioffset = tinfo->curr.offset;
1222 if ((tstate->ultraenb & mask) != 0)
1223 scb->hscb->control |= ULTRAENB;
1225 if ((tstate->discenable & mask) != 0
1226 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1227 scb->hscb->control |= DISCENB;
1229 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1230 && (tinfo->goal.width != 0
1231 || tinfo->goal.offset != 0
1232 || tinfo->goal.ppr_options != 0)) {
1233 scb->flags |= SCB_NEGOTIATE;
1234 scb->hscb->control |= MK_MESSAGE;
1235 } else if ((tstate->auto_negotiate & mask) != 0) {
1236 scb->flags |= SCB_AUTO_NEGOTIATE;
1237 scb->hscb->control |= MK_MESSAGE;
1240 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1242 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1244 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1247 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1248 ccb->ccb_h.timeout = 5 * 1000;
1250 time = ccb->ccb_h.timeout;
1253 ccb->ccb_h.timeout_ch =
1254 timeout(ahc_timeout, (caddr_t)scb, time);
1258 * We only allow one untagged transaction
1259 * per target in the initiator role unless
1260 * we are storing a full busy target *lun*
1261 * table in SCB space.
1263 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1264 && (ahc->flags & AHC_SCB_BTT) == 0) {
1265 struct scb_tailq *untagged_q;
1268 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1269 untagged_q = &(ahc->untagged_queues[target_offset]);
1270 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1271 scb->flags |= SCB_UNTAGGEDQ;
1272 if (TAILQ_FIRST(untagged_q) != scb) {
1273 ahc_unlock(ahc, &s);
1277 scb->flags |= SCB_ACTIVE;
1279 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1280 /* Define a mapping from our tag to the SCB. */
1281 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1283 if ((ahc->flags & AHC_PAGESCBS) == 0)
1284 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1285 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1288 ahc_queue_scb(ahc, scb);
1291 ahc_unlock(ahc, &s);
1295 ahc_poll(struct cam_sim *sim)
1297 struct ahc_softc *ahc;
1299 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1304 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1305 struct ccb_scsiio *csio, struct scb *scb)
1307 struct hardware_scb *hscb;
1308 struct ccb_hdr *ccb_h;
1311 ccb_h = &csio->ccb_h;
1314 csio->sense_resid = 0;
1315 if (ccb_h->func_code == XPT_SCSI_IO) {
1316 hscb->cdb_len = csio->cdb_len;
1317 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1319 if (hscb->cdb_len > sizeof(hscb->cdb32)
1320 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1323 ahc_set_transaction_status(scb,
1326 ahc_free_scb(ahc, scb);
1327 ahc_unlock(ahc, &s);
1328 xpt_done((union ccb *)csio);
1331 if (hscb->cdb_len > 12) {
1333 csio->cdb_io.cdb_ptr,
1335 scb->flags |= SCB_CDB32_PTR;
1337 memcpy(hscb->shared_data.cdb,
1338 csio->cdb_io.cdb_ptr,
1342 if (hscb->cdb_len > 12) {
1343 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1345 scb->flags |= SCB_CDB32_PTR;
1347 memcpy(hscb->shared_data.cdb,
1348 csio->cdb_io.cdb_bytes,
1354 /* Only use S/G if there is a transfer */
1355 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1356 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1357 /* We've been given a pointer to a single buffer */
1358 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1363 error = bus_dmamap_load(ahc->buffer_dmat,
1369 if (error == EINPROGRESS) {
1371 * So as to maintain ordering,
1372 * freeze the controller queue
1373 * until our mapping is
1376 xpt_freeze_simq(sim,
1378 scb->io_ctx->ccb_h.status |=
1383 struct bus_dma_segment seg;
1385 /* Pointer to physical buffer */
1386 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1387 panic("ahc_setup_data - Transfer size "
1388 "larger than can device max");
1391 (bus_addr_t)(vm_offset_t)csio->data_ptr;
1392 seg.ds_len = csio->dxfer_len;
1393 ahc_execute_scb(scb, &seg, 1, 0);
1396 struct bus_dma_segment *segs;
1398 if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1399 panic("ahc_setup_data - Physical segment "
1400 "pointers unsupported");
1402 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1403 panic("ahc_setup_data - Virtual segment "
1404 "addresses unsupported");
1406 /* Just use the segments provided */
1407 segs = (struct bus_dma_segment *)csio->data_ptr;
1408 ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1411 ahc_execute_scb(scb, NULL, 0, 0);
1416 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1418 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1419 struct scb *list_scb;
1421 scb->flags |= SCB_RECOVERY_SCB;
1424 * Take all queued, but not sent SCBs out of the equation.
1425 * Also ensure that no new CCBs are queued to us while we
1426 * try to fix this problem.
1428 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1429 xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1430 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1434 * Go through all of our pending SCBs and remove
1435 * any scheduled timeouts for them. We will reschedule
1436 * them after we've successfully fixed this problem.
1438 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1441 ccb = list_scb->io_ctx;
1442 untimeout(ahc_timeout, list_scb, ccb->ccb_h.timeout_ch);
1448 ahc_timeout(void *arg)
1451 struct ahc_softc *ahc;
1460 scb = (struct scb *)arg;
1461 ahc = (struct ahc_softc *)scb->ahc_softc;
1465 ahc_pause_and_flushwork(ahc);
1467 if ((scb->flags & SCB_ACTIVE) == 0) {
1468 /* Previous timeout took care of me already */
1469 printf("%s: Timedout SCB already complete. "
1470 "Interrupts may not be functioning.\n", ahc_name(ahc));
1472 ahc_unlock(ahc, &s);
1476 target = SCB_GET_TARGET(ahc, scb);
1477 channel = SCB_GET_CHANNEL(ahc, scb);
1478 lun = SCB_GET_LUN(scb);
1480 ahc_print_path(ahc, scb);
1481 printf("SCB 0x%x - timed out\n", scb->hscb->tag);
1482 ahc_dump_card_state(ahc);
1483 last_phase = ahc_inb(ahc, LASTPHASE);
1484 if (scb->sg_count > 0) {
1485 for (i = 0; i < scb->sg_count; i++) {
1486 printf("sg[%d] - Addr 0x%x : Length %d\n",
1488 scb->sg_list[i].addr,
1489 scb->sg_list[i].len & AHC_SG_LEN_MASK);
1492 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1494 * Been down this road before.
1495 * Do a full bus reset.
1498 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1499 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1500 printf("%s: Issued Channel %c Bus Reset. "
1501 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1504 * If we are a target, transition to bus free and report
1507 * The target/initiator that is holding up the bus may not
1508 * be the same as the one that triggered this timeout
1509 * (different commands have different timeout lengths).
1510 * If the bus is idle and we are actiing as the initiator
1511 * for this request, queue a BDR message to the timed out
1512 * target. Otherwise, if the timed out transaction is
1514 * Initiator transaction:
1515 * Stuff the message buffer with a BDR message and assert
1516 * ATN in the hopes that the target will let go of the bus
1517 * and go to the mesgout phase. If this fails, we'll
1518 * get another timeout 2 seconds later which will attempt
1521 * Target transaction:
1522 * Transition to BUS FREE and report the error.
1523 * It's good to be the target!
1525 u_int active_scb_index;
1528 saved_scbptr = ahc_inb(ahc, SCBPTR);
1529 active_scb_index = ahc_inb(ahc, SCB_TAG);
1531 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1532 && (active_scb_index < ahc->scb_data->numscbs)) {
1533 struct scb *active_scb;
1536 * If the active SCB is not us, assume that
1537 * the active SCB has a longer timeout than
1538 * the timedout SCB, and wait for the active
1541 active_scb = ahc_lookup_scb(ahc, active_scb_index);
1542 if (active_scb != scb) {
1543 struct ccb_hdr *ccbh;
1544 uint64_t newtimeout;
1546 ahc_print_path(ahc, scb);
1547 printf("Other SCB Timeout%s",
1548 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1549 ? " again\n" : "\n");
1550 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1552 MAX(active_scb->io_ctx->ccb_h.timeout,
1553 scb->io_ctx->ccb_h.timeout);
1556 ccbh = &scb->io_ctx->ccb_h;
1557 scb->io_ctx->ccb_h.timeout_ch =
1558 timeout(ahc_timeout, scb, newtimeout);
1560 ahc_unlock(ahc, &s);
1565 if ((scb->flags & SCB_TARGET_SCB) != 0) {
1568 * Send back any queued up transactions
1569 * and properly record the error condition.
1571 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1572 SCB_GET_CHANNEL(ahc, scb),
1578 /* Will clear us from the bus */
1580 ahc_unlock(ahc, &s);
1584 ahc_set_recoveryscb(ahc, active_scb);
1585 ahc_outb(ahc, MSG_OUT, HOST_MSG);
1586 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1587 ahc_print_path(ahc, active_scb);
1588 printf("BDR message in message buffer\n");
1589 active_scb->flags |= SCB_DEVICE_RESET;
1590 active_scb->io_ctx->ccb_h.timeout_ch =
1591 timeout(ahc_timeout, (caddr_t)active_scb, 2 * hz);
1596 /* XXX Shouldn't panic. Just punt instead? */
1597 if ((scb->flags & SCB_TARGET_SCB) != 0)
1598 panic("Timed-out target SCB but bus idle");
1600 if (last_phase != P_BUSFREE
1601 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1602 /* XXX What happened to the SCB? */
1603 /* Hung target selection. Goto busfree */
1604 printf("%s: Hung target selection\n",
1607 ahc_unlock(ahc, &s);
1611 if (ahc_search_qinfifo(ahc, target, channel, lun,
1612 scb->hscb->tag, ROLE_INITIATOR,
1613 /*status*/0, SEARCH_COUNT) > 0) {
1614 disconnected = FALSE;
1616 disconnected = TRUE;
1621 ahc_set_recoveryscb(ahc, scb);
1623 * Actually re-queue this SCB in an attempt
1624 * to select the device before it reconnects.
1625 * In either case (selection or reselection),
1626 * we will now issue a target reset to the
1629 * Set the MK_MESSAGE control bit indicating
1630 * that we desire to send a message. We
1631 * also set the disconnected flag since
1632 * in the paging case there is no guarantee
1633 * that our SCB control byte matches the
1634 * version on the card. We don't want the
1635 * sequencer to abort the command thinking
1636 * an unsolicited reselection occurred.
1638 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1639 scb->flags |= SCB_DEVICE_RESET;
1642 * Remove any cached copy of this SCB in the
1643 * disconnected list in preparation for the
1644 * queuing of our abort SCB. We use the
1645 * same element in the SCB, SCB_NEXT, for
1646 * both the qinfifo and the disconnected list.
1648 ahc_search_disc_list(ahc, target, channel,
1649 lun, scb->hscb->tag,
1650 /*stop_on_first*/TRUE,
1652 /*save_state*/FALSE);
1655 * In the non-paging case, the sequencer will
1656 * never re-reference the in-core SCB.
1657 * To make sure we are notified during
1658 * reslection, set the MK_MESSAGE flag in
1659 * the card's copy of the SCB.
1661 if ((ahc->flags & AHC_PAGESCBS) == 0) {
1662 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1663 ahc_outb(ahc, SCB_CONTROL,
1664 ahc_inb(ahc, SCB_CONTROL)
1669 * Clear out any entries in the QINFIFO first
1670 * so we are the next SCB for this target
1673 ahc_search_qinfifo(ahc,
1674 SCB_GET_TARGET(ahc, scb),
1675 channel, SCB_GET_LUN(scb),
1680 ahc_print_path(ahc, scb);
1681 printf("Queuing a BDR SCB\n");
1682 ahc_qinfifo_requeue_tail(ahc, scb);
1683 ahc_outb(ahc, SCBPTR, saved_scbptr);
1684 scb->io_ctx->ccb_h.timeout_ch =
1685 timeout(ahc_timeout, (caddr_t)scb, 2 * hz);
1688 /* Go "immediatly" to the bus reset */
1689 /* This shouldn't happen */
1690 ahc_set_recoveryscb(ahc, scb);
1691 ahc_print_path(ahc, scb);
1692 printf("SCB %d: Immediate reset. "
1693 "Flags = 0x%x\n", scb->hscb->tag,
1699 ahc_unlock(ahc, &s);
1703 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1705 union ccb *abort_ccb;
1707 abort_ccb = ccb->cab.abort_ccb;
1708 switch (abort_ccb->ccb_h.func_code) {
1709 case XPT_ACCEPT_TARGET_IO:
1710 case XPT_IMMED_NOTIFY:
1711 case XPT_CONT_TARGET_IO:
1713 struct ahc_tmode_tstate *tstate;
1714 struct ahc_tmode_lstate *lstate;
1715 struct ccb_hdr_slist *list;
1718 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1721 if (status != CAM_REQ_CMP) {
1722 ccb->ccb_h.status = status;
1726 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1727 list = &lstate->accept_tios;
1728 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1729 list = &lstate->immed_notifies;
1734 struct ccb_hdr *curelm;
1737 curelm = SLIST_FIRST(list);
1739 if (curelm == &abort_ccb->ccb_h) {
1741 SLIST_REMOVE_HEAD(list, sim_links.sle);
1743 while(curelm != NULL) {
1744 struct ccb_hdr *nextelm;
1747 SLIST_NEXT(curelm, sim_links.sle);
1749 if (nextelm == &abort_ccb->ccb_h) {
1762 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1763 xpt_done(abort_ccb);
1764 ccb->ccb_h.status = CAM_REQ_CMP;
1766 xpt_print_path(abort_ccb->ccb_h.path);
1767 printf("Not found\n");
1768 ccb->ccb_h.status = CAM_PATH_INVALID;
1775 /* XXX Fully implement the hard ones */
1776 ccb->ccb_h.status = CAM_UA_ABORT;
1779 ccb->ccb_h.status = CAM_REQ_INVALID;
1786 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1787 u_int lun, ac_code code, void *opt_arg)
1789 struct ccb_trans_settings cts;
1790 struct cam_path *path;
1795 error = ahc_create_path(ahc, channel, target, lun, &path);
1797 if (error != CAM_REQ_CMP)
1801 case AC_TRANSFER_NEG:
1803 #ifdef AHC_NEW_TRAN_SETTINGS
1804 struct ccb_trans_settings_scsi *scsi;
1806 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1807 scsi = &cts.proto_specific.scsi;
1809 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1811 cts.ccb_h.path = path;
1812 cts.ccb_h.target_id = target;
1813 cts.ccb_h.target_lun = lun;
1814 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1818 #ifdef AHC_NEW_TRAN_SETTINGS
1819 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1820 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1822 cts.valid &= ~CCB_TRANS_TQ_VALID;
1823 cts.flags &= ~CCB_TRANS_TAG_ENB;
1825 if (opt_arg == NULL)
1827 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1828 #ifdef AHC_NEW_TRAN_SETTINGS
1829 scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1830 scsi->valid |= CTS_SCSI_VALID_TQ;
1832 cts.flags |= CCB_TRANS_TAG_ENB;
1833 cts.valid |= CCB_TRANS_TQ_VALID;
1841 panic("ahc_send_async: Unexpected async event");
1843 xpt_async(code, path, arg);
1844 xpt_free_path(path);
1848 ahc_platform_set_tags(struct ahc_softc *ahc,
1849 struct ahc_devinfo *devinfo, int enable)
1854 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1856 ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1858 if (ahc->platform_data == NULL)
1864 ahc_platform_free(struct ahc_softc *ahc)
1866 struct ahc_platform_data *pdata;
1868 pdata = ahc->platform_data;
1869 if (pdata != NULL) {
1870 if (pdata->regs != NULL)
1871 bus_release_resource(ahc->dev_softc,
1872 pdata->regs_res_type,
1876 if (pdata->irq != NULL)
1877 bus_release_resource(ahc->dev_softc,
1878 pdata->irq_res_type,
1881 if (pdata->sim_b != NULL) {
1882 xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1883 xpt_free_path(pdata->path_b);
1884 xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1885 cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1887 if (pdata->sim != NULL) {
1888 xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1889 xpt_free_path(pdata->path);
1890 xpt_bus_deregister(cam_sim_path(pdata->sim));
1891 cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1893 if (pdata->eh != NULL)
1894 EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1895 free(ahc->platform_data, M_DEVBUF);
1900 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1902 /* We don't sort softcs under FreeBSD so report equal always */
1907 ahc_detach(device_t dev)
1909 struct ahc_softc *ahc;
1914 device_printf(dev, "detaching device\n");
1915 ahc = device_get_softc(dev);
1916 ahc = ahc_find_softc(ahc);
1918 device_printf(dev, "aic7xxx already detached\n");
1919 ahc_list_unlock(&l);
1923 ahc_intr_enable(ahc, FALSE);
1924 bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1925 ahc_unlock(ahc, &s);
1927 ahc_list_unlock(&l);
1933 ahc_dump_targcmd(struct target_cmd *cmd)
1939 byte = &cmd->initiator_channel;
1940 /* Debugging info for received commands */
1941 last_byte = &cmd[1].initiator_channel;
1944 while (byte < last_byte) {
1947 printf("%#x", *byte++);
1960 ahc_modevent(module_t mod, int type, void *data)
1962 /* XXX Deal with busy status on unload. */
1966 static moduledata_t ahc_mod = {
1972 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1973 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1974 MODULE_VERSION(ahc, 1);