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 $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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:
825 extended = SIM_IS_SCSIBUS_B(ahc, sim)
826 ? ahc->flags & AHC_EXTENDED_TRANS_B
827 : ahc->flags & AHC_EXTENDED_TRANS_A;
828 cam_calc_geometry(&ccb->ccg, extended);
832 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
837 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
838 /*initiate reset*/TRUE);
841 xpt_print_path(SIM_PATH(ahc, sim));
842 printf("SCSI bus reset delivered. "
843 "%d SCBs aborted.\n", found);
845 ccb->ccb_h.status = CAM_REQ_CMP;
849 case XPT_TERM_IO: /* Terminate the I/O process */
851 ccb->ccb_h.status = CAM_REQ_INVALID;
854 case XPT_PATH_INQ: /* Path routing inquiry */
856 struct ccb_pathinq *cpi = &ccb->cpi;
858 cpi->version_num = 1; /* XXX??? */
859 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
860 if ((ahc->features & AHC_WIDE) != 0)
861 cpi->hba_inquiry |= PI_WIDE_16;
862 if ((ahc->features & AHC_TARGETMODE) != 0) {
863 cpi->target_sprt = PIT_PROCESSOR
867 cpi->target_sprt = 0;
870 cpi->hba_eng_cnt = 0;
871 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
872 cpi->max_lun = AHC_NUM_LUNS - 1;
873 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
874 cpi->initiator_id = ahc->our_id_b;
875 if ((ahc->flags & AHC_RESET_BUS_B) == 0)
876 cpi->hba_misc |= PIM_NOBUSRESET;
878 cpi->initiator_id = ahc->our_id;
879 if ((ahc->flags & AHC_RESET_BUS_A) == 0)
880 cpi->hba_misc |= PIM_NOBUSRESET;
882 cpi->bus_id = cam_sim_bus(sim);
883 cpi->base_transfer_speed = 3300;
884 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
885 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
886 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
887 cpi->unit_number = cam_sim_unit(sim);
888 #ifdef AHC_NEW_TRAN_SETTINGS
889 cpi->protocol = PROTO_SCSI;
890 cpi->protocol_version = SCSI_REV_2;
891 cpi->transport = XPORT_SPI;
892 cpi->transport_version = 2;
893 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
894 if ((ahc->features & AHC_DT) != 0) {
895 cpi->transport_version = 3;
896 cpi->xport_specific.spi.ppr_options =
900 cpi->ccb_h.status = CAM_REQ_CMP;
905 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
912 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
913 struct ccb_trans_settings *cts)
915 #ifdef AHC_NEW_TRAN_SETTINGS
916 struct ahc_devinfo devinfo;
917 struct ccb_trans_settings_scsi *scsi;
918 struct ccb_trans_settings_spi *spi;
919 struct ahc_initiator_tinfo *targ_info;
920 struct ahc_tmode_tstate *tstate;
921 struct ahc_transinfo *tinfo;
923 scsi = &cts->proto_specific.scsi;
924 spi = &cts->xport_specific.spi;
925 ahc_compile_devinfo(&devinfo, our_id,
926 cts->ccb_h.target_id,
927 cts->ccb_h.target_lun,
928 channel, ROLE_UNKNOWN);
929 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
931 devinfo.target, &tstate);
933 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
934 tinfo = &targ_info->curr;
936 tinfo = &targ_info->user;
938 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
939 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
940 if (cts->type == CTS_TYPE_USER_SETTINGS) {
941 if ((ahc->user_discenable & devinfo.target_mask) != 0)
942 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
944 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
945 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
947 if ((tstate->discenable & devinfo.target_mask) != 0)
948 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
950 if ((tstate->tagenable & devinfo.target_mask) != 0)
951 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
953 cts->protocol_version = tinfo->protocol_version;
954 cts->transport_version = tinfo->transport_version;
956 spi->sync_period = tinfo->period;
957 spi->sync_offset = tinfo->offset;
958 spi->bus_width = tinfo->width;
959 spi->ppr_options = tinfo->ppr_options;
961 cts->protocol = PROTO_SCSI;
962 cts->transport = XPORT_SPI;
963 spi->valid = CTS_SPI_VALID_SYNC_RATE
964 | CTS_SPI_VALID_SYNC_OFFSET
965 | CTS_SPI_VALID_BUS_WIDTH
966 | CTS_SPI_VALID_PPR_OPTIONS;
968 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
969 scsi->valid = CTS_SCSI_VALID_TQ;
970 spi->valid |= CTS_SPI_VALID_DISC;
975 cts->ccb_h.status = CAM_REQ_CMP;
977 struct ahc_devinfo devinfo;
978 struct ahc_initiator_tinfo *targ_info;
979 struct ahc_tmode_tstate *tstate;
980 struct ahc_transinfo *tinfo;
982 ahc_compile_devinfo(&devinfo, our_id,
983 cts->ccb_h.target_id,
984 cts->ccb_h.target_lun,
985 channel, ROLE_UNKNOWN);
986 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
988 devinfo.target, &tstate);
990 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
991 tinfo = &targ_info->curr;
993 tinfo = &targ_info->user;
995 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
996 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
997 if ((ahc->user_discenable & devinfo.target_mask) != 0)
998 cts->flags |= CCB_TRANS_DISC_ENB;
1000 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1001 cts->flags |= CCB_TRANS_TAG_ENB;
1003 if ((tstate->discenable & devinfo.target_mask) != 0)
1004 cts->flags |= CCB_TRANS_DISC_ENB;
1006 if ((tstate->tagenable & devinfo.target_mask) != 0)
1007 cts->flags |= CCB_TRANS_TAG_ENB;
1009 cts->sync_period = tinfo->period;
1010 cts->sync_offset = tinfo->offset;
1011 cts->bus_width = tinfo->width;
1013 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1014 | CCB_TRANS_SYNC_OFFSET_VALID
1015 | CCB_TRANS_BUS_WIDTH_VALID;
1017 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1018 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1020 cts->ccb_h.status = CAM_REQ_CMP;
1025 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1027 struct ahc_softc *ahc;
1028 struct cam_sim *sim;
1030 sim = (struct cam_sim *)callback_arg;
1031 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1033 case AC_LOST_DEVICE:
1035 struct ahc_devinfo devinfo;
1038 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1039 xpt_path_target_id(path),
1040 xpt_path_lun_id(path),
1041 SIM_CHANNEL(ahc, sim),
1045 * Revert to async/narrow transfers
1046 * for the next device.
1049 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1050 AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1051 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1052 /*period*/0, /*offset*/0, /*ppr_options*/0,
1053 AHC_TRANS_GOAL|AHC_TRANS_CUR,
1055 ahc_unlock(ahc, &s);
1064 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1069 struct ahc_softc *ahc;
1070 struct ahc_initiator_tinfo *tinfo;
1071 struct ahc_tmode_tstate *tstate;
1075 scb = (struct scb *)arg;
1077 ahc = scb->ahc_softc;
1081 ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1083 ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1085 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1087 ahc_free_scb(ahc, scb);
1088 ahc_unlock(ahc, &s);
1092 if (nsegments != 0) {
1093 struct ahc_dma_seg *sg;
1094 bus_dma_segment_t *end_seg;
1095 bus_dmasync_op_t op;
1097 end_seg = dm_segs + nsegments;
1099 /* Copy the segments into our SG list */
1101 while (dm_segs < end_seg) {
1104 sg->addr = ahc_htole32(dm_segs->ds_addr);
1105 len = dm_segs->ds_len
1106 | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1107 sg->len = ahc_htole32(len);
1113 * Note where to find the SG entries in bus space.
1114 * We also set the full residual flag which the
1115 * sequencer will clear as soon as a data transfer
1118 scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
1120 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1121 op = BUS_DMASYNC_PREREAD;
1123 op = BUS_DMASYNC_PREWRITE;
1125 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1127 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1128 struct target_data *tdata;
1130 tdata = &scb->hscb->shared_data.tdata;
1131 tdata->target_phases |= DPHASE_PENDING;
1132 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1133 tdata->data_phase = P_DATAOUT;
1135 tdata->data_phase = P_DATAIN;
1138 * If the transfer is of an odd length and in the
1139 * "in" direction (scsi->HostBus), then it may
1140 * trigger a bug in the 'WideODD' feature of
1141 * non-Ultra2 chips. Force the total data-length
1142 * to be even by adding an extra, 1 byte, SG,
1143 * element. We do this even if we are not currently
1144 * negotiated wide as negotiation could occur before
1145 * this command is executed.
1147 if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1148 && (ccb->csio.dxfer_len & 0x1) != 0
1149 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1152 if (nsegments > AHC_NSEG) {
1154 ahc_set_transaction_status(scb,
1156 bus_dmamap_unload(ahc->buffer_dmat,
1159 ahc_free_scb(ahc, scb);
1160 ahc_unlock(ahc, &s);
1164 sg->addr = ahc_htole32(ahc->dma_bug_buf);
1165 sg->len = ahc_htole32(1);
1170 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1172 /* Copy the first SG into the "current" data pointer area */
1173 scb->hscb->dataptr = scb->sg_list->addr;
1174 scb->hscb->datacnt = scb->sg_list->len;
1176 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1177 scb->hscb->dataptr = 0;
1178 scb->hscb->datacnt = 0;
1181 scb->sg_count = nsegments;
1186 * Last time we need to check if this SCB needs to
1189 if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1191 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1192 ahc_free_scb(ahc, scb);
1193 ahc_unlock(ahc, &s);
1198 tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1199 SCSIID_OUR_ID(scb->hscb->scsiid),
1200 SCSIID_TARGET(ahc, scb->hscb->scsiid),
1203 mask = SCB_GET_TARGET_MASK(ahc, scb);
1204 scb->hscb->scsirate = tinfo->scsirate;
1205 scb->hscb->scsioffset = tinfo->curr.offset;
1206 if ((tstate->ultraenb & mask) != 0)
1207 scb->hscb->control |= ULTRAENB;
1209 if ((tstate->discenable & mask) != 0
1210 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1211 scb->hscb->control |= DISCENB;
1213 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1214 && (tinfo->goal.width != 0
1215 || tinfo->goal.offset != 0
1216 || tinfo->goal.ppr_options != 0)) {
1217 scb->flags |= SCB_NEGOTIATE;
1218 scb->hscb->control |= MK_MESSAGE;
1219 } else if ((tstate->auto_negotiate & mask) != 0) {
1220 scb->flags |= SCB_AUTO_NEGOTIATE;
1221 scb->hscb->control |= MK_MESSAGE;
1224 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1226 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1228 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1231 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1232 ccb->ccb_h.timeout = 5 * 1000;
1234 time = ccb->ccb_h.timeout;
1237 ccb->ccb_h.timeout_ch =
1238 timeout(ahc_timeout, (caddr_t)scb, time);
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) {
1257 ahc_unlock(ahc, &s);
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);
1275 ahc_unlock(ahc, &s);
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) {
1307 ahc_set_transaction_status(scb,
1310 ahc_free_scb(ahc, scb);
1311 ahc_unlock(ahc, &s);
1312 xpt_done((union ccb *)csio);
1315 if (hscb->cdb_len > 12) {
1317 csio->cdb_io.cdb_ptr,
1319 scb->flags |= SCB_CDB32_PTR;
1321 memcpy(hscb->shared_data.cdb,
1322 csio->cdb_io.cdb_ptr,
1326 if (hscb->cdb_len > 12) {
1327 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1329 scb->flags |= SCB_CDB32_PTR;
1331 memcpy(hscb->shared_data.cdb,
1332 csio->cdb_io.cdb_bytes,
1338 /* Only use S/G if there is a transfer */
1339 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1340 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1341 /* We've been given a pointer to a single buffer */
1342 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1347 error = bus_dmamap_load(ahc->buffer_dmat,
1353 if (error == EINPROGRESS) {
1355 * So as to maintain ordering,
1356 * freeze the controller queue
1357 * until our mapping is
1360 xpt_freeze_simq(sim,
1362 scb->io_ctx->ccb_h.status |=
1367 struct bus_dma_segment seg;
1369 /* Pointer to physical buffer */
1370 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1371 panic("ahc_setup_data - Transfer size "
1372 "larger than can device max");
1375 (bus_addr_t)(vm_offset_t)csio->data_ptr;
1376 seg.ds_len = csio->dxfer_len;
1377 ahc_execute_scb(scb, &seg, 1, 0);
1380 struct bus_dma_segment *segs;
1382 if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1383 panic("ahc_setup_data - Physical segment "
1384 "pointers unsupported");
1386 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1387 panic("ahc_setup_data - Virtual segment "
1388 "addresses unsupported");
1390 /* Just use the segments provided */
1391 segs = (struct bus_dma_segment *)csio->data_ptr;
1392 ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1395 ahc_execute_scb(scb, NULL, 0, 0);
1400 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1402 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1403 struct scb *list_scb;
1405 scb->flags |= SCB_RECOVERY_SCB;
1408 * Take all queued, but not sent SCBs out of the equation.
1409 * Also ensure that no new CCBs are queued to us while we
1410 * try to fix this problem.
1412 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1413 xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1414 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1418 * Go through all of our pending SCBs and remove
1419 * any scheduled timeouts for them. We will reschedule
1420 * them after we've successfully fixed this problem.
1422 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1425 ccb = list_scb->io_ctx;
1426 untimeout(ahc_timeout, list_scb, ccb->ccb_h.timeout_ch);
1432 ahc_timeout(void *arg)
1435 struct ahc_softc *ahc;
1444 scb = (struct scb *)arg;
1445 ahc = (struct ahc_softc *)scb->ahc_softc;
1449 ahc_pause_and_flushwork(ahc);
1451 if ((scb->flags & SCB_ACTIVE) == 0) {
1452 /* Previous timeout took care of me already */
1453 printf("%s: Timedout SCB already complete. "
1454 "Interrupts may not be functioning.\n", ahc_name(ahc));
1456 ahc_unlock(ahc, &s);
1460 target = SCB_GET_TARGET(ahc, scb);
1461 channel = SCB_GET_CHANNEL(ahc, scb);
1462 lun = SCB_GET_LUN(scb);
1464 ahc_print_path(ahc, scb);
1465 printf("SCB 0x%x - timed out\n", scb->hscb->tag);
1466 ahc_dump_card_state(ahc);
1467 last_phase = ahc_inb(ahc, LASTPHASE);
1468 if (scb->sg_count > 0) {
1469 for (i = 0; i < scb->sg_count; i++) {
1470 printf("sg[%d] - Addr 0x%x : Length %d\n",
1472 scb->sg_list[i].addr,
1473 scb->sg_list[i].len & AHC_SG_LEN_MASK);
1476 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1478 * Been down this road before.
1479 * Do a full bus reset.
1482 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1483 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1484 printf("%s: Issued Channel %c Bus Reset. "
1485 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1488 * If we are a target, transition to bus free and report
1491 * The target/initiator that is holding up the bus may not
1492 * be the same as the one that triggered this timeout
1493 * (different commands have different timeout lengths).
1494 * If the bus is idle and we are actiing as the initiator
1495 * for this request, queue a BDR message to the timed out
1496 * target. Otherwise, if the timed out transaction is
1498 * Initiator transaction:
1499 * Stuff the message buffer with a BDR message and assert
1500 * ATN in the hopes that the target will let go of the bus
1501 * and go to the mesgout phase. If this fails, we'll
1502 * get another timeout 2 seconds later which will attempt
1505 * Target transaction:
1506 * Transition to BUS FREE and report the error.
1507 * It's good to be the target!
1509 u_int active_scb_index;
1512 saved_scbptr = ahc_inb(ahc, SCBPTR);
1513 active_scb_index = ahc_inb(ahc, SCB_TAG);
1515 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1516 && (active_scb_index < ahc->scb_data->numscbs)) {
1517 struct scb *active_scb;
1520 * If the active SCB is not us, assume that
1521 * the active SCB has a longer timeout than
1522 * the timedout SCB, and wait for the active
1525 active_scb = ahc_lookup_scb(ahc, active_scb_index);
1526 if (active_scb != scb) {
1527 struct ccb_hdr *ccbh;
1528 uint64_t newtimeout;
1530 ahc_print_path(ahc, scb);
1531 printf("Other SCB Timeout%s",
1532 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1533 ? " again\n" : "\n");
1534 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1536 MAX(active_scb->io_ctx->ccb_h.timeout,
1537 scb->io_ctx->ccb_h.timeout);
1540 ccbh = &scb->io_ctx->ccb_h;
1541 scb->io_ctx->ccb_h.timeout_ch =
1542 timeout(ahc_timeout, scb, newtimeout);
1544 ahc_unlock(ahc, &s);
1549 if ((scb->flags & SCB_TARGET_SCB) != 0) {
1552 * Send back any queued up transactions
1553 * and properly record the error condition.
1555 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1556 SCB_GET_CHANNEL(ahc, scb),
1562 /* Will clear us from the bus */
1564 ahc_unlock(ahc, &s);
1568 ahc_set_recoveryscb(ahc, active_scb);
1569 ahc_outb(ahc, MSG_OUT, HOST_MSG);
1570 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1571 ahc_print_path(ahc, active_scb);
1572 printf("BDR message in message buffer\n");
1573 active_scb->flags |= SCB_DEVICE_RESET;
1574 active_scb->io_ctx->ccb_h.timeout_ch =
1575 timeout(ahc_timeout, (caddr_t)active_scb, 2 * hz);
1580 /* XXX Shouldn't panic. Just punt instead? */
1581 if ((scb->flags & SCB_TARGET_SCB) != 0)
1582 panic("Timed-out target SCB but bus idle");
1584 if (last_phase != P_BUSFREE
1585 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1586 /* XXX What happened to the SCB? */
1587 /* Hung target selection. Goto busfree */
1588 printf("%s: Hung target selection\n",
1591 ahc_unlock(ahc, &s);
1595 if (ahc_search_qinfifo(ahc, target, channel, lun,
1596 scb->hscb->tag, ROLE_INITIATOR,
1597 /*status*/0, SEARCH_COUNT) > 0) {
1598 disconnected = FALSE;
1600 disconnected = TRUE;
1605 ahc_set_recoveryscb(ahc, scb);
1607 * Actually re-queue this SCB in an attempt
1608 * to select the device before it reconnects.
1609 * In either case (selection or reselection),
1610 * we will now issue a target reset to the
1613 * Set the MK_MESSAGE control bit indicating
1614 * that we desire to send a message. We
1615 * also set the disconnected flag since
1616 * in the paging case there is no guarantee
1617 * that our SCB control byte matches the
1618 * version on the card. We don't want the
1619 * sequencer to abort the command thinking
1620 * an unsolicited reselection occurred.
1622 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1623 scb->flags |= SCB_DEVICE_RESET;
1626 * Remove any cached copy of this SCB in the
1627 * disconnected list in preparation for the
1628 * queuing of our abort SCB. We use the
1629 * same element in the SCB, SCB_NEXT, for
1630 * both the qinfifo and the disconnected list.
1632 ahc_search_disc_list(ahc, target, channel,
1633 lun, scb->hscb->tag,
1634 /*stop_on_first*/TRUE,
1636 /*save_state*/FALSE);
1639 * In the non-paging case, the sequencer will
1640 * never re-reference the in-core SCB.
1641 * To make sure we are notified during
1642 * reslection, set the MK_MESSAGE flag in
1643 * the card's copy of the SCB.
1645 if ((ahc->flags & AHC_PAGESCBS) == 0) {
1646 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1647 ahc_outb(ahc, SCB_CONTROL,
1648 ahc_inb(ahc, SCB_CONTROL)
1653 * Clear out any entries in the QINFIFO first
1654 * so we are the next SCB for this target
1657 ahc_search_qinfifo(ahc,
1658 SCB_GET_TARGET(ahc, scb),
1659 channel, SCB_GET_LUN(scb),
1664 ahc_print_path(ahc, scb);
1665 printf("Queuing a BDR SCB\n");
1666 ahc_qinfifo_requeue_tail(ahc, scb);
1667 ahc_outb(ahc, SCBPTR, saved_scbptr);
1668 scb->io_ctx->ccb_h.timeout_ch =
1669 timeout(ahc_timeout, (caddr_t)scb, 2 * hz);
1672 /* Go "immediatly" to the bus reset */
1673 /* This shouldn't happen */
1674 ahc_set_recoveryscb(ahc, scb);
1675 ahc_print_path(ahc, scb);
1676 printf("SCB %d: Immediate reset. "
1677 "Flags = 0x%x\n", scb->hscb->tag,
1683 ahc_unlock(ahc, &s);
1687 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1689 union ccb *abort_ccb;
1691 abort_ccb = ccb->cab.abort_ccb;
1692 switch (abort_ccb->ccb_h.func_code) {
1693 case XPT_ACCEPT_TARGET_IO:
1694 case XPT_IMMED_NOTIFY:
1695 case XPT_CONT_TARGET_IO:
1697 struct ahc_tmode_tstate *tstate;
1698 struct ahc_tmode_lstate *lstate;
1699 struct ccb_hdr_slist *list;
1702 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1705 if (status != CAM_REQ_CMP) {
1706 ccb->ccb_h.status = status;
1710 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1711 list = &lstate->accept_tios;
1712 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1713 list = &lstate->immed_notifies;
1718 struct ccb_hdr *curelm;
1721 curelm = SLIST_FIRST(list);
1723 if (curelm == &abort_ccb->ccb_h) {
1725 SLIST_REMOVE_HEAD(list, sim_links.sle);
1727 while(curelm != NULL) {
1728 struct ccb_hdr *nextelm;
1731 SLIST_NEXT(curelm, sim_links.sle);
1733 if (nextelm == &abort_ccb->ccb_h) {
1746 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1747 xpt_done(abort_ccb);
1748 ccb->ccb_h.status = CAM_REQ_CMP;
1750 xpt_print_path(abort_ccb->ccb_h.path);
1751 printf("Not found\n");
1752 ccb->ccb_h.status = CAM_PATH_INVALID;
1759 /* XXX Fully implement the hard ones */
1760 ccb->ccb_h.status = CAM_UA_ABORT;
1763 ccb->ccb_h.status = CAM_REQ_INVALID;
1770 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1771 u_int lun, ac_code code, void *opt_arg)
1773 struct ccb_trans_settings cts;
1774 struct cam_path *path;
1779 error = ahc_create_path(ahc, channel, target, lun, &path);
1781 if (error != CAM_REQ_CMP)
1785 case AC_TRANSFER_NEG:
1787 #ifdef AHC_NEW_TRAN_SETTINGS
1788 struct ccb_trans_settings_scsi *scsi;
1790 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1791 scsi = &cts.proto_specific.scsi;
1793 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1795 cts.ccb_h.path = path;
1796 cts.ccb_h.target_id = target;
1797 cts.ccb_h.target_lun = lun;
1798 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1802 #ifdef AHC_NEW_TRAN_SETTINGS
1803 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1804 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1806 cts.valid &= ~CCB_TRANS_TQ_VALID;
1807 cts.flags &= ~CCB_TRANS_TAG_ENB;
1809 if (opt_arg == NULL)
1811 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1812 #ifdef AHC_NEW_TRAN_SETTINGS
1813 scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1814 scsi->valid |= CTS_SCSI_VALID_TQ;
1816 cts.flags |= CCB_TRANS_TAG_ENB;
1817 cts.valid |= CCB_TRANS_TQ_VALID;
1825 panic("ahc_send_async: Unexpected async event");
1827 xpt_async(code, path, arg);
1828 xpt_free_path(path);
1832 ahc_platform_set_tags(struct ahc_softc *ahc,
1833 struct ahc_devinfo *devinfo, int enable)
1838 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1840 ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1842 if (ahc->platform_data == NULL)
1848 ahc_platform_free(struct ahc_softc *ahc)
1850 struct ahc_platform_data *pdata;
1852 pdata = ahc->platform_data;
1853 if (pdata != NULL) {
1854 if (pdata->regs != NULL)
1855 bus_release_resource(ahc->dev_softc,
1856 pdata->regs_res_type,
1860 if (pdata->irq != NULL)
1861 bus_release_resource(ahc->dev_softc,
1862 pdata->irq_res_type,
1865 if (pdata->sim_b != NULL) {
1866 xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1867 xpt_free_path(pdata->path_b);
1868 xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1869 cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1871 if (pdata->sim != NULL) {
1872 xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1873 xpt_free_path(pdata->path);
1874 xpt_bus_deregister(cam_sim_path(pdata->sim));
1875 cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1877 if (pdata->eh != NULL)
1878 EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1879 free(ahc->platform_data, M_DEVBUF);
1884 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1886 /* We don't sort softcs under FreeBSD so report equal always */
1891 ahc_detach(device_t dev)
1893 struct ahc_softc *ahc;
1898 device_printf(dev, "detaching device\n");
1899 ahc = device_get_softc(dev);
1900 ahc = ahc_find_softc(ahc);
1902 device_printf(dev, "aic7xxx already detached\n");
1903 ahc_list_unlock(&l);
1907 ahc_intr_enable(ahc, FALSE);
1908 bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1909 ahc_unlock(ahc, &s);
1911 ahc_list_unlock(&l);
1917 ahc_dump_targcmd(struct target_cmd *cmd)
1923 byte = &cmd->initiator_channel;
1924 /* Debugging info for received commands */
1925 last_byte = &cmd[1].initiator_channel;
1928 while (byte < last_byte) {
1931 printf("%#x", *byte++);
1944 ahc_modevent(module_t mod, int type, void *data)
1946 /* XXX Deal with busy status on unload. */
1950 static moduledata_t ahc_mod = {
1956 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1957 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1958 MODULE_VERSION(ahc, 1);