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.6 2004/09/17 03:39:39 joerg 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 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;
113 struct cam_sim *sim2;
114 struct cam_path *path;
115 struct cam_path *path2;
123 ahc_controller_info(ahc, ahc_info);
124 printf("%s\n", ahc_info);
127 * Attach secondary channel first if the user has
128 * declared it the primary channel.
130 if ((ahc->features & AHC_TWIN) != 0
131 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
140 * Construct our first channel SIM entry
142 sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
143 device_get_unit(ahc->dev_softc),
144 1, AHC_MAX_QUEUE, NULL);
148 if (xpt_bus_register(sim, bus_id) != CAM_SUCCESS) {
154 if (xpt_create_path(&path, /*periph*/NULL,
155 cam_sim_path(sim), CAM_TARGET_WILDCARD,
156 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
157 xpt_bus_deregister(cam_sim_path(sim));
163 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
164 csa.ccb_h.func_code = XPT_SASYNC_CB;
165 csa.event_enable = AC_LOST_DEVICE;
166 csa.callback = ahc_async;
167 csa.callback_arg = sim;
168 xpt_action((union ccb *)&csa);
171 if (ahc->features & AHC_TWIN) {
172 sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
173 ahc, device_get_unit(ahc->dev_softc), 1,
174 AHC_MAX_QUEUE, NULL);
177 printf("ahc_attach: Unable to attach second "
178 "bus due to resource shortage");
182 if (xpt_bus_register(sim2, bus_id2) != CAM_SUCCESS) {
183 printf("ahc_attach: Unable to attach second "
184 "bus due to resource shortage");
186 * We do not want to destroy the device queue
187 * because the first bus is using it.
193 if (xpt_create_path(&path2, /*periph*/NULL,
196 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
197 xpt_bus_deregister(cam_sim_path(sim2));
202 xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
203 csa.ccb_h.func_code = XPT_SASYNC_CB;
204 csa.event_enable = AC_LOST_DEVICE;
205 csa.callback = ahc_async;
206 csa.callback_arg = sim2;
207 xpt_action((union ccb *)&csa);
212 if ((ahc->features & AHC_TWIN) != 0
213 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
214 ahc->platform_data->sim_b = sim;
215 ahc->platform_data->path_b = path;
216 ahc->platform_data->sim = sim2;
217 ahc->platform_data->path = path2;
219 ahc->platform_data->sim = sim;
220 ahc->platform_data->path = path;
221 ahc->platform_data->sim_b = sim2;
222 ahc->platform_data->path_b = path2;
226 /* We have to wait until after any system dumps... */
227 ahc->platform_data->eh =
228 EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
229 ahc, SHUTDOWN_PRI_DEFAULT);
230 ahc_intr_enable(ahc, TRUE);
238 * Catch an interrupt from the adapter
241 ahc_platform_intr(void *arg)
243 struct ahc_softc *ahc;
245 ahc = (struct ahc_softc *)arg;
250 * We have an scb which has been processed by the
251 * adaptor, now we look to see how the operation
255 ahc_done(struct ahc_softc *ahc, struct scb *scb)
259 CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
260 ("ahc_done - scb %d\n", scb->hscb->tag));
263 LIST_REMOVE(scb, pending_links);
264 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
265 struct scb_tailq *untagged_q;
268 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
269 untagged_q = &ahc->untagged_queues[target_offset];
270 TAILQ_REMOVE(untagged_q, scb, links.tqe);
271 scb->flags &= ~SCB_UNTAGGEDQ;
272 ahc_run_untagged_queue(ahc, untagged_q);
275 callout_stop(&ccb->ccb_h.timeout_ch);
277 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
280 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
281 op = BUS_DMASYNC_POSTREAD;
283 op = BUS_DMASYNC_POSTWRITE;
284 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
285 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
288 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
289 struct cam_path *ccb_path;
292 * If we have finally disconnected, clean up our
293 * pending device state.
294 * XXX - There may be error states that cause where
295 * we will remain connected.
297 ccb_path = ccb->ccb_h.path;
298 if (ahc->pending_device != NULL
299 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
301 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
302 ahc->pending_device = NULL;
305 xpt_print_path(ccb->ccb_h.path);
306 printf("Still connected\n");
312 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG)
313 ccb->ccb_h.status |= CAM_REQ_CMP;
314 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
315 ahc_free_scb(ahc, scb);
321 * If the recovery SCB completes, we have to be
322 * out of our timeout.
324 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
325 struct scb *list_scb;
328 * We were able to complete the command successfully,
329 * so reinstate the timeouts for all other pending
332 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
336 ccb = list_scb->io_ctx;
337 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
340 time = ccb->ccb_h.timeout;
343 callout_reset(&ccb->ccb_h.timeout_ch, time,
344 ahc_timeout, list_scb);
347 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
348 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
349 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
350 ahc_print_path(ahc, scb);
351 printf("no longer in timeout, status = %x\n",
355 /* Don't clobber any existing error state */
356 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
357 ccb->ccb_h.status |= CAM_REQ_CMP;
358 } else if ((scb->flags & SCB_SENSE) != 0) {
360 * We performed autosense retrieval.
362 * Zero any sense not transferred by the
363 * device. The SCSI spec mandates that any
364 * untransfered data should be assumed to be
365 * zero. Complete the 'bounce' of sense information
366 * through buffers accessible via bus-space by
367 * copying it into the clients csio.
369 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
370 memcpy(&ccb->csio.sense_data,
371 ahc_get_sense_buf(ahc, scb),
372 (ahc_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
373 - ccb->csio.sense_resid);
374 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
376 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
377 ahc_free_scb(ahc, scb);
382 ahc_action(struct cam_sim *sim, union ccb *ccb)
384 struct ahc_softc *ahc;
385 struct ahc_tmode_lstate *lstate;
390 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
392 ahc = (struct ahc_softc *)cam_sim_softc(sim);
394 target_id = ccb->ccb_h.target_id;
395 our_id = SIM_SCSI_ID(ahc, sim);
397 switch (ccb->ccb_h.func_code) {
398 /* Common cases first */
399 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
400 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
402 struct ahc_tmode_tstate *tstate;
405 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
408 if (status != CAM_REQ_CMP) {
409 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
410 /* Response from the black hole device */
412 lstate = ahc->black_hole;
414 ccb->ccb_h.status = status;
419 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
422 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
424 ccb->ccb_h.status = CAM_REQ_INPROG;
425 if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
426 ahc_run_tqinfifo(ahc, /*paused*/FALSE);
432 * The target_id represents the target we attempt to
433 * select. In target mode, this is the initiator of
434 * the original command.
437 target_id = ccb->csio.init_id;
440 case XPT_SCSI_IO: /* Execute the requested I/O operation */
441 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
444 struct hardware_scb *hscb;
446 if ((ahc->flags & AHC_INITIATORROLE) == 0
447 && (ccb->ccb_h.func_code == XPT_SCSI_IO
448 || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
449 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
458 if ((scb = ahc_get_scb(ahc)) == NULL) {
460 xpt_freeze_simq(sim, /*count*/1);
461 ahc->flags |= AHC_RESOURCE_SHORTAGE;
463 ccb->ccb_h.status = CAM_REQUEUE_REQ;
471 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
472 ("start scb(%p)\n", scb));
475 * So we can find the SCB when an abort is requested
477 ccb->ccb_h.ccb_scb_ptr = scb;
480 * Put all the arguments for the xfer in the scb
483 hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
484 hscb->lun = ccb->ccb_h.target_lun;
485 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
487 scb->flags |= SCB_DEVICE_RESET;
488 hscb->control |= MK_MESSAGE;
489 ahc_execute_scb(scb, NULL, 0, 0);
491 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
492 struct target_data *tdata;
494 tdata = &hscb->shared_data.tdata;
495 if (ahc->pending_device == lstate)
496 scb->flags |= SCB_TARGET_IMMEDIATE;
497 hscb->control |= TARGET_SCB;
498 scb->flags |= SCB_TARGET_SCB;
499 tdata->target_phases = 0;
500 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
501 tdata->target_phases |= SPHASE_PENDING;
503 ccb->csio.scsi_status;
505 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
506 tdata->target_phases |= NO_DISCONNECT;
508 tdata->initiator_tag = ccb->csio.tag_id;
510 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
511 hscb->control |= ccb->csio.tag_action;
513 ahc_setup_data(ahc, sim, &ccb->csio, scb);
518 case XPT_IMMED_NOTIFY:
520 struct ahc_tmode_tstate *tstate;
521 struct ahc_tmode_lstate *lstate;
524 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
527 if (status != CAM_REQ_CMP) {
528 ccb->ccb_h.status = status;
532 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
534 ccb->ccb_h.status = CAM_REQ_INPROG;
535 ahc_send_lstate_events(ahc, lstate);
538 case XPT_EN_LUN: /* Enable LUN as a target */
539 ahc_handle_en_lun(ahc, sim, ccb);
542 case XPT_ABORT: /* Abort the specified CCB */
544 ahc_abort_ccb(ahc, sim, ccb);
547 case XPT_SET_TRAN_SETTINGS:
549 #ifdef AHC_NEW_TRAN_SETTINGS
550 struct ahc_devinfo devinfo;
551 struct ccb_trans_settings *cts;
552 struct ccb_trans_settings_scsi *scsi;
553 struct ccb_trans_settings_spi *spi;
554 struct ahc_initiator_tinfo *tinfo;
555 struct ahc_tmode_tstate *tstate;
556 uint16_t *discenable;
561 scsi = &cts->proto_specific.scsi;
562 spi = &cts->xport_specific.spi;
563 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
564 cts->ccb_h.target_id,
565 cts->ccb_h.target_lun,
566 SIM_CHANNEL(ahc, sim),
568 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
570 devinfo.target, &tstate);
572 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
573 update_type |= AHC_TRANS_GOAL;
574 discenable = &tstate->discenable;
575 tagenable = &tstate->tagenable;
576 tinfo->curr.protocol_version =
577 cts->protocol_version;
578 tinfo->curr.transport_version =
579 cts->transport_version;
580 tinfo->goal.protocol_version =
581 cts->protocol_version;
582 tinfo->goal.transport_version =
583 cts->transport_version;
584 } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
585 update_type |= AHC_TRANS_USER;
586 discenable = &ahc->user_discenable;
587 tagenable = &ahc->user_tagenable;
588 tinfo->user.protocol_version =
589 cts->protocol_version;
590 tinfo->user.transport_version =
591 cts->transport_version;
593 ccb->ccb_h.status = CAM_REQ_INVALID;
600 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
601 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
602 *discenable |= devinfo.target_mask;
604 *discenable &= ~devinfo.target_mask;
607 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
608 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
609 *tagenable |= devinfo.target_mask;
611 *tagenable &= ~devinfo.target_mask;
614 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
615 ahc_validate_width(ahc, /*tinfo limit*/NULL,
616 &spi->bus_width, ROLE_UNKNOWN);
617 ahc_set_width(ahc, &devinfo, spi->bus_width,
618 update_type, /*paused*/FALSE);
621 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
622 if (update_type == AHC_TRANS_USER)
623 spi->ppr_options = tinfo->user.ppr_options;
625 spi->ppr_options = tinfo->goal.ppr_options;
628 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
629 if (update_type == AHC_TRANS_USER)
630 spi->sync_offset = tinfo->user.offset;
632 spi->sync_offset = tinfo->goal.offset;
635 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
636 if (update_type == AHC_TRANS_USER)
637 spi->sync_period = tinfo->user.period;
639 spi->sync_period = tinfo->goal.period;
642 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
643 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
644 struct ahc_syncrate *syncrate;
647 if ((ahc->features & AHC_ULTRA2) != 0)
648 maxsync = AHC_SYNCRATE_DT;
649 else if ((ahc->features & AHC_ULTRA) != 0)
650 maxsync = AHC_SYNCRATE_ULTRA;
652 maxsync = AHC_SYNCRATE_FAST;
654 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
655 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
657 syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
660 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
661 syncrate, &spi->sync_offset,
662 spi->bus_width, ROLE_UNKNOWN);
664 /* We use a period of 0 to represent async */
665 if (spi->sync_offset == 0) {
666 spi->sync_period = 0;
667 spi->ppr_options = 0;
670 ahc_set_syncrate(ahc, &devinfo, syncrate,
671 spi->sync_period, spi->sync_offset,
672 spi->ppr_options, update_type,
676 ccb->ccb_h.status = CAM_REQ_CMP;
679 struct ahc_devinfo devinfo;
680 struct ccb_trans_settings *cts;
681 struct ahc_initiator_tinfo *tinfo;
682 struct ahc_tmode_tstate *tstate;
683 uint16_t *discenable;
689 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
690 cts->ccb_h.target_id,
691 cts->ccb_h.target_lun,
692 SIM_CHANNEL(ahc, sim),
694 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
696 devinfo.target, &tstate);
698 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
699 update_type |= AHC_TRANS_GOAL;
700 discenable = &tstate->discenable;
701 tagenable = &tstate->tagenable;
702 } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
703 update_type |= AHC_TRANS_USER;
704 discenable = &ahc->user_discenable;
705 tagenable = &ahc->user_tagenable;
707 ccb->ccb_h.status = CAM_REQ_INVALID;
714 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
715 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
716 *discenable |= devinfo.target_mask;
718 *discenable &= ~devinfo.target_mask;
721 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
722 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
723 *tagenable |= devinfo.target_mask;
725 *tagenable &= ~devinfo.target_mask;
728 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
729 ahc_validate_width(ahc, /*tinfo limit*/NULL,
730 &cts->bus_width, ROLE_UNKNOWN);
731 ahc_set_width(ahc, &devinfo, cts->bus_width,
732 update_type, /*paused*/FALSE);
735 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
736 if (update_type == AHC_TRANS_USER)
737 cts->sync_offset = tinfo->user.offset;
739 cts->sync_offset = tinfo->goal.offset;
742 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
743 if (update_type == AHC_TRANS_USER)
744 cts->sync_period = tinfo->user.period;
746 cts->sync_period = tinfo->goal.period;
749 if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
750 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)) {
751 struct ahc_syncrate *syncrate;
755 if ((ahc->features & AHC_ULTRA2) != 0)
756 maxsync = AHC_SYNCRATE_DT;
757 else if ((ahc->features & AHC_ULTRA) != 0)
758 maxsync = AHC_SYNCRATE_ULTRA;
760 maxsync = AHC_SYNCRATE_FAST;
763 if (cts->sync_period <= 9
764 && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
765 ppr_options = MSG_EXT_PPR_DT_REQ;
767 syncrate = ahc_find_syncrate(ahc, &cts->sync_period,
770 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
771 syncrate, &cts->sync_offset,
772 MSG_EXT_WDTR_BUS_8_BIT,
775 /* We use a period of 0 to represent async */
776 if (cts->sync_offset == 0) {
777 cts->sync_period = 0;
781 if (ppr_options == MSG_EXT_PPR_DT_REQ
782 && tinfo->user.transport_version >= 3) {
783 tinfo->goal.transport_version =
784 tinfo->user.transport_version;
785 tinfo->curr.transport_version =
786 tinfo->user.transport_version;
789 ahc_set_syncrate(ahc, &devinfo, syncrate,
790 cts->sync_period, cts->sync_offset,
791 ppr_options, update_type,
795 ccb->ccb_h.status = CAM_REQ_CMP;
800 case XPT_GET_TRAN_SETTINGS:
801 /* Get default/user set transfer settings for the target */
805 ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
806 SIM_CHANNEL(ahc, sim), &ccb->cts);
811 case XPT_CALC_GEOMETRY:
813 struct ccb_calc_geometry *ccg;
815 uint32_t secs_per_cylinder;
819 size_mb = ccg->volume_size
820 / ((1024L * 1024L) / ccg->block_size);
821 extended = SIM_IS_SCSIBUS_B(ahc, sim)
822 ? ahc->flags & AHC_EXTENDED_TRANS_B
823 : ahc->flags & AHC_EXTENDED_TRANS_A;
825 if (size_mb > 1024 && extended) {
827 ccg->secs_per_track = 63;
830 ccg->secs_per_track = 32;
832 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
833 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
834 ccb->ccb_h.status = CAM_REQ_CMP;
838 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
843 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
844 /*initiate reset*/TRUE);
847 xpt_print_path(SIM_PATH(ahc, sim));
848 printf("SCSI bus reset delivered. "
849 "%d SCBs aborted.\n", found);
851 ccb->ccb_h.status = CAM_REQ_CMP;
855 case XPT_TERM_IO: /* Terminate the I/O process */
857 ccb->ccb_h.status = CAM_REQ_INVALID;
860 case XPT_PATH_INQ: /* Path routing inquiry */
862 struct ccb_pathinq *cpi = &ccb->cpi;
864 cpi->version_num = 1; /* XXX??? */
865 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
866 if ((ahc->features & AHC_WIDE) != 0)
867 cpi->hba_inquiry |= PI_WIDE_16;
868 if ((ahc->features & AHC_TARGETMODE) != 0) {
869 cpi->target_sprt = PIT_PROCESSOR
873 cpi->target_sprt = 0;
876 cpi->hba_eng_cnt = 0;
877 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
878 cpi->max_lun = AHC_NUM_LUNS - 1;
879 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
880 cpi->initiator_id = ahc->our_id_b;
881 if ((ahc->flags & AHC_RESET_BUS_B) == 0)
882 cpi->hba_misc |= PIM_NOBUSRESET;
884 cpi->initiator_id = ahc->our_id;
885 if ((ahc->flags & AHC_RESET_BUS_A) == 0)
886 cpi->hba_misc |= PIM_NOBUSRESET;
888 cpi->bus_id = cam_sim_bus(sim);
889 cpi->base_transfer_speed = 3300;
890 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
891 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
892 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
893 cpi->unit_number = cam_sim_unit(sim);
894 #ifdef AHC_NEW_TRAN_SETTINGS
895 cpi->protocol = PROTO_SCSI;
896 cpi->protocol_version = SCSI_REV_2;
897 cpi->transport = XPORT_SPI;
898 cpi->transport_version = 2;
899 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
900 if ((ahc->features & AHC_DT) != 0) {
901 cpi->transport_version = 3;
902 cpi->xport_specific.spi.ppr_options =
906 cpi->ccb_h.status = CAM_REQ_CMP;
911 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
918 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
919 struct ccb_trans_settings *cts)
921 #ifdef AHC_NEW_TRAN_SETTINGS
922 struct ahc_devinfo devinfo;
923 struct ccb_trans_settings_scsi *scsi;
924 struct ccb_trans_settings_spi *spi;
925 struct ahc_initiator_tinfo *targ_info;
926 struct ahc_tmode_tstate *tstate;
927 struct ahc_transinfo *tinfo;
929 scsi = &cts->proto_specific.scsi;
930 spi = &cts->xport_specific.spi;
931 ahc_compile_devinfo(&devinfo, our_id,
932 cts->ccb_h.target_id,
933 cts->ccb_h.target_lun,
934 channel, ROLE_UNKNOWN);
935 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
937 devinfo.target, &tstate);
939 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
940 tinfo = &targ_info->curr;
942 tinfo = &targ_info->user;
944 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
945 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
946 if (cts->type == CTS_TYPE_USER_SETTINGS) {
947 if ((ahc->user_discenable & devinfo.target_mask) != 0)
948 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
950 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
951 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
953 if ((tstate->discenable & devinfo.target_mask) != 0)
954 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
956 if ((tstate->tagenable & devinfo.target_mask) != 0)
957 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
959 cts->protocol_version = tinfo->protocol_version;
960 cts->transport_version = tinfo->transport_version;
962 spi->sync_period = tinfo->period;
963 spi->sync_offset = tinfo->offset;
964 spi->bus_width = tinfo->width;
965 spi->ppr_options = tinfo->ppr_options;
967 cts->protocol = PROTO_SCSI;
968 cts->transport = XPORT_SPI;
969 spi->valid = CTS_SPI_VALID_SYNC_RATE
970 | CTS_SPI_VALID_SYNC_OFFSET
971 | CTS_SPI_VALID_BUS_WIDTH
972 | CTS_SPI_VALID_PPR_OPTIONS;
974 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
975 scsi->valid = CTS_SCSI_VALID_TQ;
976 spi->valid |= CTS_SPI_VALID_DISC;
981 cts->ccb_h.status = CAM_REQ_CMP;
983 struct ahc_devinfo devinfo;
984 struct ahc_initiator_tinfo *targ_info;
985 struct ahc_tmode_tstate *tstate;
986 struct ahc_transinfo *tinfo;
988 ahc_compile_devinfo(&devinfo, our_id,
989 cts->ccb_h.target_id,
990 cts->ccb_h.target_lun,
991 channel, ROLE_UNKNOWN);
992 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
994 devinfo.target, &tstate);
996 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
997 tinfo = &targ_info->curr;
999 tinfo = &targ_info->user;
1001 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
1002 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
1003 if ((ahc->user_discenable & devinfo.target_mask) != 0)
1004 cts->flags |= CCB_TRANS_DISC_ENB;
1006 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1007 cts->flags |= CCB_TRANS_TAG_ENB;
1009 if ((tstate->discenable & devinfo.target_mask) != 0)
1010 cts->flags |= CCB_TRANS_DISC_ENB;
1012 if ((tstate->tagenable & devinfo.target_mask) != 0)
1013 cts->flags |= CCB_TRANS_TAG_ENB;
1015 cts->sync_period = tinfo->period;
1016 cts->sync_offset = tinfo->offset;
1017 cts->bus_width = tinfo->width;
1019 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1020 | CCB_TRANS_SYNC_OFFSET_VALID
1021 | CCB_TRANS_BUS_WIDTH_VALID;
1023 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1024 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1026 cts->ccb_h.status = CAM_REQ_CMP;
1031 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1033 struct ahc_softc *ahc;
1034 struct cam_sim *sim;
1036 sim = (struct cam_sim *)callback_arg;
1037 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1039 case AC_LOST_DEVICE:
1041 struct ahc_devinfo devinfo;
1044 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1045 xpt_path_target_id(path),
1046 xpt_path_lun_id(path),
1047 SIM_CHANNEL(ahc, sim),
1051 * Revert to async/narrow transfers
1052 * for the next device.
1055 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1056 AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1057 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1058 /*period*/0, /*offset*/0, /*ppr_options*/0,
1059 AHC_TRANS_GOAL|AHC_TRANS_CUR,
1061 ahc_unlock(ahc, &s);
1070 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1075 struct ahc_softc *ahc;
1076 struct ahc_initiator_tinfo *tinfo;
1077 struct ahc_tmode_tstate *tstate;
1081 scb = (struct scb *)arg;
1083 ahc = scb->ahc_softc;
1087 ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1089 ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1091 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1093 ahc_free_scb(ahc, scb);
1094 ahc_unlock(ahc, &s);
1098 if (nsegments != 0) {
1099 struct ahc_dma_seg *sg;
1100 bus_dma_segment_t *end_seg;
1101 bus_dmasync_op_t op;
1103 end_seg = dm_segs + nsegments;
1105 /* Copy the segments into our SG list */
1107 while (dm_segs < end_seg) {
1110 sg->addr = ahc_htole32(dm_segs->ds_addr);
1111 len = dm_segs->ds_len
1112 | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1113 sg->len = ahc_htole32(len);
1119 * Note where to find the SG entries in bus space.
1120 * We also set the full residual flag which the
1121 * sequencer will clear as soon as a data transfer
1124 scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
1126 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1127 op = BUS_DMASYNC_PREREAD;
1129 op = BUS_DMASYNC_PREWRITE;
1131 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1133 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1134 struct target_data *tdata;
1136 tdata = &scb->hscb->shared_data.tdata;
1137 tdata->target_phases |= DPHASE_PENDING;
1138 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1139 tdata->data_phase = P_DATAOUT;
1141 tdata->data_phase = P_DATAIN;
1144 * If the transfer is of an odd length and in the
1145 * "in" direction (scsi->HostBus), then it may
1146 * trigger a bug in the 'WideODD' feature of
1147 * non-Ultra2 chips. Force the total data-length
1148 * to be even by adding an extra, 1 byte, SG,
1149 * element. We do this even if we are not currently
1150 * negotiated wide as negotiation could occur before
1151 * this command is executed.
1153 if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1154 && (ccb->csio.dxfer_len & 0x1) != 0
1155 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1158 if (nsegments > AHC_NSEG) {
1160 ahc_set_transaction_status(scb,
1162 bus_dmamap_unload(ahc->buffer_dmat,
1165 ahc_free_scb(ahc, scb);
1166 ahc_unlock(ahc, &s);
1170 sg->addr = ahc_htole32(ahc->dma_bug_buf);
1171 sg->len = ahc_htole32(1);
1176 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1178 /* Copy the first SG into the "current" data pointer area */
1179 scb->hscb->dataptr = scb->sg_list->addr;
1180 scb->hscb->datacnt = scb->sg_list->len;
1182 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1183 scb->hscb->dataptr = 0;
1184 scb->hscb->datacnt = 0;
1187 scb->sg_count = nsegments;
1192 * Last time we need to check if this SCB needs to
1195 if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1197 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1198 ahc_free_scb(ahc, scb);
1199 ahc_unlock(ahc, &s);
1204 tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1205 SCSIID_OUR_ID(scb->hscb->scsiid),
1206 SCSIID_TARGET(ahc, scb->hscb->scsiid),
1209 mask = SCB_GET_TARGET_MASK(ahc, scb);
1210 scb->hscb->scsirate = tinfo->scsirate;
1211 scb->hscb->scsioffset = tinfo->curr.offset;
1212 if ((tstate->ultraenb & mask) != 0)
1213 scb->hscb->control |= ULTRAENB;
1215 if ((tstate->discenable & mask) != 0
1216 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1217 scb->hscb->control |= DISCENB;
1219 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1220 && (tinfo->goal.width != 0
1221 || tinfo->goal.offset != 0
1222 || tinfo->goal.ppr_options != 0)) {
1223 scb->flags |= SCB_NEGOTIATE;
1224 scb->hscb->control |= MK_MESSAGE;
1225 } else if ((tstate->auto_negotiate & mask) != 0) {
1226 scb->flags |= SCB_AUTO_NEGOTIATE;
1227 scb->hscb->control |= MK_MESSAGE;
1230 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1232 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1234 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1237 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1238 ccb->ccb_h.timeout = 5 * 1000;
1240 time = ccb->ccb_h.timeout;
1243 callout_reset(&ccb->ccb_h.timeout_ch, time, ahc_timeout, scb);
1247 * We only allow one untagged transaction
1248 * per target in the initiator role unless
1249 * we are storing a full busy target *lun*
1250 * table in SCB space.
1252 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1253 && (ahc->flags & AHC_SCB_BTT) == 0) {
1254 struct scb_tailq *untagged_q;
1257 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1258 untagged_q = &(ahc->untagged_queues[target_offset]);
1259 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1260 scb->flags |= SCB_UNTAGGEDQ;
1261 if (TAILQ_FIRST(untagged_q) != scb) {
1262 ahc_unlock(ahc, &s);
1266 scb->flags |= SCB_ACTIVE;
1268 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1269 /* Define a mapping from our tag to the SCB. */
1270 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1272 if ((ahc->flags & AHC_PAGESCBS) == 0)
1273 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1274 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1277 ahc_queue_scb(ahc, scb);
1280 ahc_unlock(ahc, &s);
1284 ahc_poll(struct cam_sim *sim)
1286 struct ahc_softc *ahc;
1288 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1293 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1294 struct ccb_scsiio *csio, struct scb *scb)
1296 struct hardware_scb *hscb;
1297 struct ccb_hdr *ccb_h;
1300 ccb_h = &csio->ccb_h;
1303 csio->sense_resid = 0;
1304 if (ccb_h->func_code == XPT_SCSI_IO) {
1305 hscb->cdb_len = csio->cdb_len;
1306 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1308 if (hscb->cdb_len > sizeof(hscb->cdb32)
1309 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1312 ahc_set_transaction_status(scb,
1315 ahc_free_scb(ahc, scb);
1316 ahc_unlock(ahc, &s);
1317 xpt_done((union ccb *)csio);
1320 if (hscb->cdb_len > 12) {
1322 csio->cdb_io.cdb_ptr,
1324 scb->flags |= SCB_CDB32_PTR;
1326 memcpy(hscb->shared_data.cdb,
1327 csio->cdb_io.cdb_ptr,
1331 if (hscb->cdb_len > 12) {
1332 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1334 scb->flags |= SCB_CDB32_PTR;
1336 memcpy(hscb->shared_data.cdb,
1337 csio->cdb_io.cdb_bytes,
1343 /* Only use S/G if there is a transfer */
1344 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1345 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1346 /* We've been given a pointer to a single buffer */
1347 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1352 error = bus_dmamap_load(ahc->buffer_dmat,
1358 if (error == EINPROGRESS) {
1360 * So as to maintain ordering,
1361 * freeze the controller queue
1362 * until our mapping is
1365 xpt_freeze_simq(sim,
1367 scb->io_ctx->ccb_h.status |=
1372 struct bus_dma_segment seg;
1374 /* Pointer to physical buffer */
1375 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1376 panic("ahc_setup_data - Transfer size "
1377 "larger than can device max");
1380 (bus_addr_t)(vm_offset_t)csio->data_ptr;
1381 seg.ds_len = csio->dxfer_len;
1382 ahc_execute_scb(scb, &seg, 1, 0);
1385 struct bus_dma_segment *segs;
1387 if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1388 panic("ahc_setup_data - Physical segment "
1389 "pointers unsupported");
1391 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1392 panic("ahc_setup_data - Virtual segment "
1393 "addresses unsupported");
1395 /* Just use the segments provided */
1396 segs = (struct bus_dma_segment *)csio->data_ptr;
1397 ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1400 ahc_execute_scb(scb, NULL, 0, 0);
1405 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1407 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1408 struct scb *list_scb;
1410 scb->flags |= SCB_RECOVERY_SCB;
1413 * Take all queued, but not sent SCBs out of the equation.
1414 * Also ensure that no new CCBs are queued to us while we
1415 * try to fix this problem.
1417 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1418 xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1419 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1423 * Go through all of our pending SCBs and remove
1424 * any scheduled timeouts for them. We will reschedule
1425 * them after we've successfully fixed this problem.
1427 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1430 ccb = list_scb->io_ctx;
1431 callout_stop(&ccb->ccb_h.timeout_ch);
1437 ahc_timeout(void *arg)
1440 struct ahc_softc *ahc;
1449 scb = (struct scb *)arg;
1450 ahc = (struct ahc_softc *)scb->ahc_softc;
1454 ahc_pause_and_flushwork(ahc);
1456 if ((scb->flags & SCB_ACTIVE) == 0) {
1457 /* Previous timeout took care of me already */
1458 printf("%s: Timedout SCB already complete. "
1459 "Interrupts may not be functioning.\n", ahc_name(ahc));
1461 ahc_unlock(ahc, &s);
1465 target = SCB_GET_TARGET(ahc, scb);
1466 channel = SCB_GET_CHANNEL(ahc, scb);
1467 lun = SCB_GET_LUN(scb);
1469 ahc_print_path(ahc, scb);
1470 printf("SCB 0x%x - timed out\n", scb->hscb->tag);
1471 ahc_dump_card_state(ahc);
1472 last_phase = ahc_inb(ahc, LASTPHASE);
1473 if (scb->sg_count > 0) {
1474 for (i = 0; i < scb->sg_count; i++) {
1475 printf("sg[%d] - Addr 0x%x : Length %d\n",
1477 scb->sg_list[i].addr,
1478 scb->sg_list[i].len & AHC_SG_LEN_MASK);
1481 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1483 * Been down this road before.
1484 * Do a full bus reset.
1487 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1488 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1489 printf("%s: Issued Channel %c Bus Reset. "
1490 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1493 * If we are a target, transition to bus free and report
1496 * The target/initiator that is holding up the bus may not
1497 * be the same as the one that triggered this timeout
1498 * (different commands have different timeout lengths).
1499 * If the bus is idle and we are actiing as the initiator
1500 * for this request, queue a BDR message to the timed out
1501 * target. Otherwise, if the timed out transaction is
1503 * Initiator transaction:
1504 * Stuff the message buffer with a BDR message and assert
1505 * ATN in the hopes that the target will let go of the bus
1506 * and go to the mesgout phase. If this fails, we'll
1507 * get another timeout 2 seconds later which will attempt
1510 * Target transaction:
1511 * Transition to BUS FREE and report the error.
1512 * It's good to be the target!
1514 u_int active_scb_index;
1517 saved_scbptr = ahc_inb(ahc, SCBPTR);
1518 active_scb_index = ahc_inb(ahc, SCB_TAG);
1520 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1521 && (active_scb_index < ahc->scb_data->numscbs)) {
1522 struct scb *active_scb;
1525 * If the active SCB is not us, assume that
1526 * the active SCB has a longer timeout than
1527 * the timedout SCB, and wait for the active
1530 active_scb = ahc_lookup_scb(ahc, active_scb_index);
1531 if (active_scb != scb) {
1532 struct ccb_hdr *ccbh;
1533 uint64_t newtimeout;
1535 ahc_print_path(ahc, scb);
1536 printf("Other SCB Timeout%s",
1537 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1538 ? " again\n" : "\n");
1539 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1541 MAX(active_scb->io_ctx->ccb_h.timeout,
1542 scb->io_ctx->ccb_h.timeout);
1545 ccbh = &scb->io_ctx->ccb_h;
1546 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1547 newtimeout, ahc_timeout, scb);
1549 ahc_unlock(ahc, &s);
1554 if ((scb->flags & SCB_TARGET_SCB) != 0) {
1557 * Send back any queued up transactions
1558 * and properly record the error condition.
1560 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1561 SCB_GET_CHANNEL(ahc, scb),
1567 /* Will clear us from the bus */
1569 ahc_unlock(ahc, &s);
1573 ahc_set_recoveryscb(ahc, active_scb);
1574 ahc_outb(ahc, MSG_OUT, HOST_MSG);
1575 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1576 ahc_print_path(ahc, active_scb);
1577 printf("BDR message in message buffer\n");
1578 active_scb->flags |= SCB_DEVICE_RESET;
1579 callout_reset(&active_scb->io_ctx->ccb_h.timeout_ch,
1580 2 * hz, ahc_timeout, active_scb);
1585 /* XXX Shouldn't panic. Just punt instead? */
1586 if ((scb->flags & SCB_TARGET_SCB) != 0)
1587 panic("Timed-out target SCB but bus idle");
1589 if (last_phase != P_BUSFREE
1590 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1591 /* XXX What happened to the SCB? */
1592 /* Hung target selection. Goto busfree */
1593 printf("%s: Hung target selection\n",
1596 ahc_unlock(ahc, &s);
1600 if (ahc_search_qinfifo(ahc, target, channel, lun,
1601 scb->hscb->tag, ROLE_INITIATOR,
1602 /*status*/0, SEARCH_COUNT) > 0) {
1603 disconnected = FALSE;
1605 disconnected = TRUE;
1610 ahc_set_recoveryscb(ahc, scb);
1612 * Actually re-queue this SCB in an attempt
1613 * to select the device before it reconnects.
1614 * In either case (selection or reselection),
1615 * we will now issue a target reset to the
1618 * Set the MK_MESSAGE control bit indicating
1619 * that we desire to send a message. We
1620 * also set the disconnected flag since
1621 * in the paging case there is no guarantee
1622 * that our SCB control byte matches the
1623 * version on the card. We don't want the
1624 * sequencer to abort the command thinking
1625 * an unsolicited reselection occurred.
1627 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1628 scb->flags |= SCB_DEVICE_RESET;
1631 * Remove any cached copy of this SCB in the
1632 * disconnected list in preparation for the
1633 * queuing of our abort SCB. We use the
1634 * same element in the SCB, SCB_NEXT, for
1635 * both the qinfifo and the disconnected list.
1637 ahc_search_disc_list(ahc, target, channel,
1638 lun, scb->hscb->tag,
1639 /*stop_on_first*/TRUE,
1641 /*save_state*/FALSE);
1644 * In the non-paging case, the sequencer will
1645 * never re-reference the in-core SCB.
1646 * To make sure we are notified during
1647 * reslection, set the MK_MESSAGE flag in
1648 * the card's copy of the SCB.
1650 if ((ahc->flags & AHC_PAGESCBS) == 0) {
1651 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1652 ahc_outb(ahc, SCB_CONTROL,
1653 ahc_inb(ahc, SCB_CONTROL)
1658 * Clear out any entries in the QINFIFO first
1659 * so we are the next SCB for this target
1662 ahc_search_qinfifo(ahc,
1663 SCB_GET_TARGET(ahc, scb),
1664 channel, SCB_GET_LUN(scb),
1669 ahc_print_path(ahc, scb);
1670 printf("Queuing a BDR SCB\n");
1671 ahc_qinfifo_requeue_tail(ahc, scb);
1672 ahc_outb(ahc, SCBPTR, saved_scbptr);
1673 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1674 2 * hz, ahc_timeout, scb);
1677 /* Go "immediatly" to the bus reset */
1678 /* This shouldn't happen */
1679 ahc_set_recoveryscb(ahc, scb);
1680 ahc_print_path(ahc, scb);
1681 printf("SCB %d: Immediate reset. "
1682 "Flags = 0x%x\n", scb->hscb->tag,
1688 ahc_unlock(ahc, &s);
1692 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1694 union ccb *abort_ccb;
1696 abort_ccb = ccb->cab.abort_ccb;
1697 switch (abort_ccb->ccb_h.func_code) {
1698 case XPT_ACCEPT_TARGET_IO:
1699 case XPT_IMMED_NOTIFY:
1700 case XPT_CONT_TARGET_IO:
1702 struct ahc_tmode_tstate *tstate;
1703 struct ahc_tmode_lstate *lstate;
1704 struct ccb_hdr_slist *list;
1707 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1710 if (status != CAM_REQ_CMP) {
1711 ccb->ccb_h.status = status;
1715 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1716 list = &lstate->accept_tios;
1717 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1718 list = &lstate->immed_notifies;
1723 struct ccb_hdr *curelm;
1726 curelm = SLIST_FIRST(list);
1728 if (curelm == &abort_ccb->ccb_h) {
1730 SLIST_REMOVE_HEAD(list, sim_links.sle);
1732 while(curelm != NULL) {
1733 struct ccb_hdr *nextelm;
1736 SLIST_NEXT(curelm, sim_links.sle);
1738 if (nextelm == &abort_ccb->ccb_h) {
1751 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1752 xpt_done(abort_ccb);
1753 ccb->ccb_h.status = CAM_REQ_CMP;
1755 xpt_print_path(abort_ccb->ccb_h.path);
1756 printf("Not found\n");
1757 ccb->ccb_h.status = CAM_PATH_INVALID;
1764 /* XXX Fully implement the hard ones */
1765 ccb->ccb_h.status = CAM_UA_ABORT;
1768 ccb->ccb_h.status = CAM_REQ_INVALID;
1775 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1776 u_int lun, ac_code code, void *opt_arg)
1778 struct ccb_trans_settings cts;
1779 struct cam_path *path;
1784 error = ahc_create_path(ahc, channel, target, lun, &path);
1786 if (error != CAM_REQ_CMP)
1790 case AC_TRANSFER_NEG:
1792 #ifdef AHC_NEW_TRAN_SETTINGS
1793 struct ccb_trans_settings_scsi *scsi;
1795 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1796 scsi = &cts.proto_specific.scsi;
1798 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1800 cts.ccb_h.path = path;
1801 cts.ccb_h.target_id = target;
1802 cts.ccb_h.target_lun = lun;
1803 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1807 #ifdef AHC_NEW_TRAN_SETTINGS
1808 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1809 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1811 cts.valid &= ~CCB_TRANS_TQ_VALID;
1812 cts.flags &= ~CCB_TRANS_TAG_ENB;
1814 if (opt_arg == NULL)
1816 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1817 #ifdef AHC_NEW_TRAN_SETTINGS
1818 scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1819 scsi->valid |= CTS_SCSI_VALID_TQ;
1821 cts.flags |= CCB_TRANS_TAG_ENB;
1822 cts.valid |= CCB_TRANS_TQ_VALID;
1830 panic("ahc_send_async: Unexpected async event");
1832 xpt_async(code, path, arg);
1833 xpt_free_path(path);
1837 ahc_platform_set_tags(struct ahc_softc *ahc,
1838 struct ahc_devinfo *devinfo, int enable)
1843 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1845 ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1846 M_INTWAIT | M_ZERO);
1851 ahc_platform_free(struct ahc_softc *ahc)
1853 struct ahc_platform_data *pdata;
1855 pdata = ahc->platform_data;
1856 if (pdata != NULL) {
1857 if (pdata->regs != NULL)
1858 bus_release_resource(ahc->dev_softc,
1859 pdata->regs_res_type,
1863 if (pdata->irq != NULL)
1864 bus_release_resource(ahc->dev_softc,
1865 pdata->irq_res_type,
1868 if (pdata->sim_b != NULL) {
1869 xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1870 xpt_free_path(pdata->path_b);
1871 xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1872 cam_sim_free(pdata->sim_b);
1874 if (pdata->sim != NULL) {
1875 xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1876 xpt_free_path(pdata->path);
1877 xpt_bus_deregister(cam_sim_path(pdata->sim));
1878 cam_sim_free(pdata->sim);
1880 if (pdata->eh != NULL)
1881 EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1882 free(ahc->platform_data, M_DEVBUF);
1887 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1889 /* We don't sort softcs under FreeBSD so report equal always */
1894 ahc_detach(device_t dev)
1896 struct ahc_softc *ahc;
1901 device_printf(dev, "detaching device\n");
1902 ahc = device_get_softc(dev);
1903 ahc = ahc_find_softc(ahc);
1905 device_printf(dev, "aic7xxx already detached\n");
1906 ahc_list_unlock(&l);
1910 ahc_intr_enable(ahc, FALSE);
1911 bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1912 ahc_unlock(ahc, &s);
1914 ahc_list_unlock(&l);
1920 ahc_dump_targcmd(struct target_cmd *cmd)
1926 byte = &cmd->initiator_channel;
1927 /* Debugging info for received commands */
1928 last_byte = &cmd[1].initiator_channel;
1931 while (byte < last_byte) {
1934 printf("%#x", *byte++);
1947 ahc_modevent(module_t mod, int type, void *data)
1949 /* XXX Deal with busy status on unload. */
1953 static moduledata_t ahc_mod = {
1959 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1960 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1961 MODULE_VERSION(ahc, 1);