2 * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers
4 * Copyright (c) 1994-2002 Justin T. Gibbs.
5 * Copyright (c) 2001-2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU Public License ("GPL").
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.c#27 $
34 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.c,v 1.3.2.4 2003/06/10 03:26:07 gibbs Exp $
35 * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.c,v 1.13 2007/06/04 17:21:55 dillon Exp $
38 #include "aic79xx_osm.h"
39 #include "aic79xx_inline.h"
46 #ifndef AHD_TMODE_ENABLE
47 #define AHD_TMODE_ENABLE 0
50 #define ccb_scb_ptr spriv_ptr0
53 static void ahd_dump_targcmd(struct target_cmd *cmd);
55 static int ahd_modevent(module_t mod, int type, void *data);
56 static void ahd_action(struct cam_sim *sim, union ccb *ccb);
57 static void ahd_set_tran_settings(struct ahd_softc *ahd,
58 int our_id, char channel,
59 struct ccb_trans_settings *cts);
60 static void ahd_get_tran_settings(struct ahd_softc *ahd,
61 int our_id, char channel,
62 struct ccb_trans_settings *cts);
63 static void ahd_async(void *callback_arg, uint32_t code,
64 struct cam_path *path, void *arg);
65 static void ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
66 int nsegments, int error);
67 static void ahd_poll(struct cam_sim *sim);
68 static void ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
69 struct ccb_scsiio *csio, struct scb *scb);
70 static void ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
72 static int ahd_create_path(struct ahd_softc *ahd,
73 char channel, u_int target, u_int lun,
74 struct cam_path **path);
77 static void ahd_set_recoveryscb(struct ahd_softc *ahd, struct scb *scb);
81 ahd_create_path(struct ahd_softc *ahd, char channel, u_int target,
82 u_int lun, struct cam_path **path)
87 path_id = cam_sim_path(ahd->platform_data->sim_b);
89 path_id = cam_sim_path(ahd->platform_data->sim);
91 return (xpt_create_path(path, /*periph*/NULL,
92 path_id, target, lun));
96 ahd_map_int(struct ahd_softc *ahd)
100 /* Hook up our interrupt handler */
101 error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
102 0, ahd_platform_intr, ahd,
103 &ahd->platform_data->ih, NULL);
105 device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
111 * Attach all the sub-devices we can find
114 ahd_attach(struct ahd_softc *ahd)
117 struct ccb_setasync csa;
119 struct cam_path *path;
125 ahd_controller_info(ahd, ahd_info);
126 kprintf("%s\n", ahd_info);
130 * Construct our SIM entry
132 sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
133 device_get_unit(ahd->dev_softc),
134 1, AHD_MAX_QUEUE, NULL);
138 if (xpt_bus_register(sim, /*bus_id*/0) != CAM_SUCCESS) {
144 if (xpt_create_path(&path, /*periph*/NULL,
145 cam_sim_path(sim), CAM_TARGET_WILDCARD,
146 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
147 xpt_bus_deregister(cam_sim_path(sim));
153 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
154 csa.ccb_h.func_code = XPT_SASYNC_CB;
155 csa.event_enable = AC_LOST_DEVICE;
156 csa.callback = ahd_async;
157 csa.callback_arg = sim;
158 xpt_action((union ccb *)&csa);
162 ahd->platform_data->sim = sim;
163 ahd->platform_data->path = path;
165 /* We have to wait until after any system dumps... */
166 ahd->platform_data->eh =
167 EVENTHANDLER_REGISTER(shutdown_post_sync, ahd_shutdown,
168 ahd, SHUTDOWN_PRI_DEFAULT);
169 ahd_intr_enable(ahd, TRUE);
178 * Catch an interrupt from the adapter
181 ahd_platform_intr(void *arg)
183 struct ahd_softc *ahd;
185 ahd = (struct ahd_softc *)arg;
190 * We have an scb which has been processed by the
191 * adaptor, now we look to see how the operation
195 ahd_done(struct ahd_softc *ahd, struct scb *scb)
199 CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
200 ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
203 LIST_REMOVE(scb, pending_links);
205 callout_stop(&ccb->ccb_h.timeout_ch);
207 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
210 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
211 op = BUS_DMASYNC_POSTREAD;
213 op = BUS_DMASYNC_POSTWRITE;
214 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
215 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
218 #ifdef AHD_TARGET_MODE
219 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
220 struct cam_path *ccb_path;
223 * If we have finally disconnected, clean up our
224 * pending device state.
225 * XXX - There may be error states that cause where
226 * we will remain connected.
228 ccb_path = ccb->ccb_h.path;
229 if (ahd->pending_device != NULL
230 && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
232 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
233 ahd->pending_device = NULL;
235 xpt_print_path(ccb->ccb_h.path);
236 kprintf("Still disconnected\n");
241 if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG)
242 ccb->ccb_h.status |= CAM_REQ_CMP;
243 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
244 ahd_free_scb(ahd, scb);
251 * If the recovery SCB completes, we have to be
252 * out of our timeout.
254 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
255 struct scb *list_scb;
258 * We were able to complete the command successfully,
259 * so reinstate the timeouts for all other pending
262 LIST_FOREACH(list_scb, &ahd->pending_scbs, pending_links) {
266 ccb = list_scb->io_ctx;
267 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
270 time = ccb->ccb_h.timeout;
273 callout_reset(&ccb->ccb_h.timeout_ch, time,
274 ahd_timeout, list_scb);
277 if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
278 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
279 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
280 ahd_print_path(ahd, scb);
281 kprintf("no longer in timeout, status = %x\n",
285 /* Don't clobber any existing error state */
286 if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
287 ccb->ccb_h.status |= CAM_REQ_CMP;
288 } else if ((scb->flags & SCB_SENSE) != 0) {
290 * We performed autosense retrieval.
292 * Zero any sense not transferred by the
293 * device. The SCSI spec mandates that any
294 * untransfered data should be assumed to be
295 * zero. Complete the 'bounce' of sense information
296 * through buffers accessible via bus-space by
297 * copying it into the clients csio.
299 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
300 memcpy(&ccb->csio.sense_data,
301 ahd_get_sense_buf(ahd, scb),
302 /* XXX What size do we want to use??? */
303 sizeof(ccb->csio.sense_data)
304 - ccb->csio.sense_resid);
305 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
306 } else if ((scb->flags & SCB_PKT_SENSE) != 0) {
307 struct scsi_status_iu_header *siu;
312 * Copy only the sense data into the provided buffer.
314 siu = (struct scsi_status_iu_header *)scb->sense_data;
315 sense_len = MIN(scsi_4btoul(siu->sense_length),
316 sizeof(ccb->csio.sense_data));
317 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
318 memcpy(&ccb->csio.sense_data,
319 ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
321 kprintf("Copied %d bytes of sense data offset %d:", sense_len,
322 SIU_SENSE_OFFSET(siu));
323 for (i = 0; i < sense_len; i++)
324 kprintf(" 0x%x", ((uint8_t *)&ccb->csio.sense_data)[i]);
326 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
328 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
329 ahd_free_scb(ahd, scb);
334 ahd_action(struct cam_sim *sim, union ccb *ccb)
336 struct ahd_softc *ahd;
337 #ifdef AHD_TARGET_MODE
338 struct ahd_tmode_lstate *lstate;
343 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
345 ahd = (struct ahd_softc *)cam_sim_softc(sim);
347 target_id = ccb->ccb_h.target_id;
348 our_id = SIM_SCSI_ID(ahd, sim);
350 switch (ccb->ccb_h.func_code) {
351 /* Common cases first */
352 #ifdef AHD_TARGET_MODE
353 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
354 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
356 struct ahd_tmode_tstate *tstate;
359 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
362 if (status != CAM_REQ_CMP) {
363 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
364 /* Response from the black hole device */
366 lstate = ahd->black_hole;
368 ccb->ccb_h.status = status;
373 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
376 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
378 ccb->ccb_h.status = CAM_REQ_INPROG;
379 if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
380 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
386 * The target_id represents the target we attempt to
387 * select. In target mode, this is the initiator of
388 * the original command.
391 target_id = ccb->csio.init_id;
395 case XPT_SCSI_IO: /* Execute the requested I/O operation */
396 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
399 struct hardware_scb *hscb;
400 struct ahd_initiator_tinfo *tinfo;
401 struct ahd_tmode_tstate *tstate;
404 if ((ahd->flags & AHD_INITIATORROLE) == 0
405 && (ccb->ccb_h.func_code == XPT_SCSI_IO
406 || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
407 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
416 tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
418 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
419 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
420 || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
421 col_idx = AHD_NEVER_COL_IDX;
423 col_idx = AHD_BUILD_COL_IDX(target_id,
424 ccb->ccb_h.target_lun);
426 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
428 xpt_freeze_simq(sim, /*count*/1);
429 ahd->flags |= AHD_RESOURCE_SHORTAGE;
431 ccb->ccb_h.status = CAM_REQUEUE_REQ;
439 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
440 ("start scb(%p)\n", scb));
443 * So we can find the SCB when an abort is requested
445 ccb->ccb_h.ccb_scb_ptr = scb;
448 * Put all the arguments for the xfer in the scb
451 hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
452 hscb->lun = ccb->ccb_h.target_lun;
453 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
455 scb->flags |= SCB_DEVICE_RESET;
456 hscb->control |= MK_MESSAGE;
457 hscb->task_management = SIU_TASKMGMT_LUN_RESET;
458 ahd_execute_scb(scb, NULL, 0, 0);
460 #ifdef AHD_TARGET_MODE
461 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
462 struct target_data *tdata;
464 tdata = &hscb->shared_data.tdata;
465 if (ahd->pending_device == lstate)
466 scb->flags |= SCB_TARGET_IMMEDIATE;
467 hscb->control |= TARGET_SCB;
468 tdata->target_phases = 0;
469 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
470 tdata->target_phases |= SPHASE_PENDING;
472 ccb->csio.scsi_status;
474 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
475 tdata->target_phases |= NO_DISCONNECT;
477 tdata->initiator_tag =
478 ahd_htole16(ccb->csio.tag_id);
481 hscb->task_management = 0;
482 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
483 hscb->control |= ccb->csio.tag_action;
485 ahd_setup_data(ahd, sim, &ccb->csio, scb);
489 #ifdef AHD_TARGET_MODE
491 case XPT_IMMED_NOTIFY:
493 struct ahd_tmode_tstate *tstate;
494 struct ahd_tmode_lstate *lstate;
497 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
500 if (status != CAM_REQ_CMP) {
501 ccb->ccb_h.status = status;
505 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
507 ccb->ccb_h.status = CAM_REQ_INPROG;
508 ahd_send_lstate_events(ahd, lstate);
511 case XPT_EN_LUN: /* Enable LUN as a target */
512 ahd_handle_en_lun(ahd, sim, ccb);
516 case XPT_ABORT: /* Abort the specified CCB */
518 ahd_abort_ccb(ahd, sim, ccb);
521 case XPT_SET_TRAN_SETTINGS:
524 ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
525 SIM_CHANNEL(ahd, sim), &ccb->cts);
530 case XPT_GET_TRAN_SETTINGS:
531 /* Get default/user set transfer settings for the target */
534 ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
535 SIM_CHANNEL(ahd, sim), &ccb->cts);
540 case XPT_CALC_GEOMETRY:
542 struct ccb_calc_geometry *ccg;
544 uint32_t secs_per_cylinder;
548 size_mb = ccg->volume_size
549 / ((1024L * 1024L) / ccg->block_size);
550 extended = ahd->flags & AHD_EXTENDED_TRANS_A;
552 if (size_mb > 1024 && extended) {
554 ccg->secs_per_track = 63;
557 ccg->secs_per_track = 32;
559 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
560 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
561 ccb->ccb_h.status = CAM_REQ_CMP;
565 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
570 found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
571 /*initiate reset*/TRUE);
574 xpt_print_path(SIM_PATH(ahd, sim));
575 kprintf("SCSI bus reset delivered. "
576 "%d SCBs aborted.\n", found);
578 ccb->ccb_h.status = CAM_REQ_CMP;
582 case XPT_TERM_IO: /* Terminate the I/O process */
584 ccb->ccb_h.status = CAM_REQ_INVALID;
587 case XPT_PATH_INQ: /* Path routing inquiry */
589 struct ccb_pathinq *cpi = &ccb->cpi;
591 cpi->version_num = 1; /* XXX??? */
592 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
593 if ((ahd->features & AHD_WIDE) != 0)
594 cpi->hba_inquiry |= PI_WIDE_16;
595 if ((ahd->features & AHD_TARGETMODE) != 0) {
596 cpi->target_sprt = PIT_PROCESSOR
600 cpi->target_sprt = 0;
603 cpi->hba_eng_cnt = 0;
604 cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
605 cpi->max_lun = AHD_NUM_LUNS - 1;
606 cpi->initiator_id = ahd->our_id;
607 if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
608 cpi->hba_misc |= PIM_NOBUSRESET;
610 cpi->bus_id = cam_sim_bus(sim);
611 cpi->base_transfer_speed = 3300;
612 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
613 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
614 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
615 cpi->unit_number = cam_sim_unit(sim);
616 #ifdef AHD_NEW_TRAN_SETTINGS
617 cpi->protocol = PROTO_SCSI;
618 cpi->protocol_version = SCSI_REV_2;
619 cpi->transport = XPORT_SPI;
620 cpi->transport_version = 2;
621 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
622 cpi->transport_version = 4;
623 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST;
625 cpi->ccb_h.status = CAM_REQ_CMP;
630 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
638 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
639 struct ccb_trans_settings *cts)
641 #ifdef AHD_NEW_TRAN_SETTINGS
642 struct ahd_devinfo devinfo;
643 struct ccb_trans_settings_scsi *scsi;
644 struct ccb_trans_settings_spi *spi;
645 struct ahd_initiator_tinfo *tinfo;
646 struct ahd_tmode_tstate *tstate;
647 uint16_t *discenable;
651 scsi = &cts->proto_specific.scsi;
652 spi = &cts->xport_specific.spi;
653 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
654 cts->ccb_h.target_id,
655 cts->ccb_h.target_lun,
656 SIM_CHANNEL(ahd, sim),
658 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
660 devinfo.target, &tstate);
662 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
663 update_type |= AHD_TRANS_GOAL;
664 discenable = &tstate->discenable;
665 tagenable = &tstate->tagenable;
666 tinfo->curr.protocol_version = cts->protocol_version;
667 tinfo->curr.transport_version = cts->transport_version;
668 tinfo->goal.protocol_version = cts->protocol_version;
669 tinfo->goal.transport_version = cts->transport_version;
670 } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
671 update_type |= AHD_TRANS_USER;
672 discenable = &ahd->user_discenable;
673 tagenable = &ahd->user_tagenable;
674 tinfo->user.protocol_version = cts->protocol_version;
675 tinfo->user.transport_version = cts->transport_version;
677 cts->ccb_h.status = CAM_REQ_INVALID;
681 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
682 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
683 *discenable |= devinfo.target_mask;
685 *discenable &= ~devinfo.target_mask;
688 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
689 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
690 *tagenable |= devinfo.target_mask;
692 *tagenable &= ~devinfo.target_mask;
695 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
696 ahd_validate_width(ahd, /*tinfo limit*/NULL,
697 &spi->bus_width, ROLE_UNKNOWN);
698 ahd_set_width(ahd, &devinfo, spi->bus_width,
699 update_type, /*paused*/FALSE);
702 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
703 if (update_type == AHD_TRANS_USER)
704 spi->ppr_options = tinfo->user.ppr_options;
706 spi->ppr_options = tinfo->goal.ppr_options;
709 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
710 if (update_type == AHD_TRANS_USER)
711 spi->sync_offset = tinfo->user.offset;
713 spi->sync_offset = tinfo->goal.offset;
716 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
717 if (update_type == AHD_TRANS_USER)
718 spi->sync_period = tinfo->user.period;
720 spi->sync_period = tinfo->goal.period;
723 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
724 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
727 maxsync = AHD_SYNCRATE_MAX;
729 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
730 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
732 if ((*discenable & devinfo.target_mask) == 0)
733 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
735 ahd_find_syncrate(ahd, &spi->sync_period,
736 &spi->ppr_options, maxsync);
737 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
738 spi->sync_period, &spi->sync_offset,
739 spi->bus_width, ROLE_UNKNOWN);
741 /* We use a period of 0 to represent async */
742 if (spi->sync_offset == 0) {
743 spi->sync_period = 0;
744 spi->ppr_options = 0;
747 ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
748 spi->sync_offset, spi->ppr_options,
749 update_type, /*paused*/FALSE);
751 cts->ccb_h.status = CAM_REQ_CMP;
753 struct ahd_devinfo devinfo;
754 struct ahd_initiator_tinfo *tinfo;
755 struct ahd_tmode_tstate *tstate;
756 uint16_t *discenable;
760 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
761 cts->ccb_h.target_id,
762 cts->ccb_h.target_lun,
763 SIM_CHANNEL(ahd, sim),
765 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
767 devinfo.target, &tstate);
769 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
770 update_type |= AHD_TRANS_GOAL;
771 discenable = &tstate->discenable;
772 tagenable = &tstate->tagenable;
773 } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
774 update_type |= AHD_TRANS_USER;
775 discenable = &ahd->user_discenable;
776 tagenable = &ahd->user_tagenable;
778 cts->ccb_h.status = CAM_REQ_INVALID;
782 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
783 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
784 *discenable |= devinfo.target_mask;
786 *discenable &= ~devinfo.target_mask;
789 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
790 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
791 *tagenable |= devinfo.target_mask;
793 *tagenable &= ~devinfo.target_mask;
796 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
797 ahd_validate_width(ahd, /*tinfo limit*/NULL,
798 &cts->bus_width, ROLE_UNKNOWN);
799 ahd_set_width(ahd, &devinfo, cts->bus_width,
800 update_type, /*paused*/FALSE);
803 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
804 if (update_type == AHD_TRANS_USER)
805 cts->sync_offset = tinfo->user.offset;
807 cts->sync_offset = tinfo->goal.offset;
810 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
811 if (update_type == AHD_TRANS_USER)
812 cts->sync_period = tinfo->user.period;
814 cts->sync_period = tinfo->goal.period;
817 if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
818 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
819 || ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
820 || ((cts->valid & CCB_TRANS_DISC_VALID) != 0)) {
824 maxsync = AHD_SYNCRATE_MAX;
826 if (cts->sync_period <= AHD_SYNCRATE_DT
827 && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT) {
828 ppr_options = tinfo->user.ppr_options
829 | MSG_EXT_PPR_DT_REQ;
832 if ((*tagenable & devinfo.target_mask) == 0
833 || (*discenable & devinfo.target_mask) == 0)
834 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
836 ahd_find_syncrate(ahd, &cts->sync_period,
837 &ppr_options, maxsync);
838 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
839 cts->sync_period, &cts->sync_offset,
840 MSG_EXT_WDTR_BUS_8_BIT,
843 /* We use a period of 0 to represent async */
844 if (cts->sync_offset == 0) {
845 cts->sync_period = 0;
850 && tinfo->user.transport_version >= 3) {
851 tinfo->goal.transport_version =
852 tinfo->user.transport_version;
853 tinfo->curr.transport_version =
854 tinfo->user.transport_version;
857 ahd_set_syncrate(ahd, &devinfo, cts->sync_period,
858 cts->sync_offset, ppr_options,
859 update_type, /*paused*/FALSE);
861 cts->ccb_h.status = CAM_REQ_CMP;
866 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
867 struct ccb_trans_settings *cts)
869 #ifdef AHD_NEW_TRAN_SETTINGS
870 struct ahd_devinfo devinfo;
871 struct ccb_trans_settings_scsi *scsi;
872 struct ccb_trans_settings_spi *spi;
873 struct ahd_initiator_tinfo *targ_info;
874 struct ahd_tmode_tstate *tstate;
875 struct ahd_transinfo *tinfo;
877 scsi = &cts->proto_specific.scsi;
878 spi = &cts->xport_specific.spi;
879 ahd_compile_devinfo(&devinfo, our_id,
880 cts->ccb_h.target_id,
881 cts->ccb_h.target_lun,
882 channel, ROLE_UNKNOWN);
883 targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
885 devinfo.target, &tstate);
887 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
888 tinfo = &targ_info->curr;
890 tinfo = &targ_info->user;
892 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
893 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
894 if (cts->type == CTS_TYPE_USER_SETTINGS) {
895 if ((ahd->user_discenable & devinfo.target_mask) != 0)
896 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
898 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
899 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
901 if ((tstate->discenable & devinfo.target_mask) != 0)
902 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
904 if ((tstate->tagenable & devinfo.target_mask) != 0)
905 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
907 cts->protocol_version = tinfo->protocol_version;
908 cts->transport_version = tinfo->transport_version;
910 spi->sync_period = tinfo->period;
911 spi->sync_offset = tinfo->offset;
912 spi->bus_width = tinfo->width;
913 spi->ppr_options = tinfo->ppr_options;
915 cts->protocol = PROTO_SCSI;
916 cts->transport = XPORT_SPI;
917 spi->valid = CTS_SPI_VALID_SYNC_RATE
918 | CTS_SPI_VALID_SYNC_OFFSET
919 | CTS_SPI_VALID_BUS_WIDTH
920 | CTS_SPI_VALID_PPR_OPTIONS;
922 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
923 scsi->valid = CTS_SCSI_VALID_TQ;
924 spi->valid |= CTS_SPI_VALID_DISC;
929 cts->ccb_h.status = CAM_REQ_CMP;
931 struct ahd_devinfo devinfo;
932 struct ahd_initiator_tinfo *targ_info;
933 struct ahd_tmode_tstate *tstate;
934 struct ahd_transinfo *tinfo;
936 ahd_compile_devinfo(&devinfo, our_id,
937 cts->ccb_h.target_id,
938 cts->ccb_h.target_lun,
939 channel, ROLE_UNKNOWN);
940 targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
942 devinfo.target, &tstate);
944 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
945 tinfo = &targ_info->curr;
947 tinfo = &targ_info->user;
949 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
950 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
951 if ((ahd->user_discenable & devinfo.target_mask) != 0)
952 cts->flags |= CCB_TRANS_DISC_ENB;
954 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
955 cts->flags |= CCB_TRANS_TAG_ENB;
957 if ((tstate->discenable & devinfo.target_mask) != 0)
958 cts->flags |= CCB_TRANS_DISC_ENB;
960 if ((tstate->tagenable & devinfo.target_mask) != 0)
961 cts->flags |= CCB_TRANS_TAG_ENB;
963 cts->sync_period = tinfo->period;
964 cts->sync_offset = tinfo->offset;
965 cts->bus_width = tinfo->width;
967 cts->valid = CCB_TRANS_SYNC_RATE_VALID
968 | CCB_TRANS_SYNC_OFFSET_VALID
969 | CCB_TRANS_BUS_WIDTH_VALID;
971 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
972 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
974 cts->ccb_h.status = CAM_REQ_CMP;
979 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
981 struct ahd_softc *ahd;
984 sim = (struct cam_sim *)callback_arg;
985 ahd = (struct ahd_softc *)cam_sim_softc(sim);
989 struct ahd_devinfo devinfo;
991 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
992 xpt_path_target_id(path),
993 xpt_path_lun_id(path),
994 SIM_CHANNEL(ahd, sim),
998 * Revert to async/narrow transfers
999 * for the next device.
1002 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1003 AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
1004 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
1005 /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
1016 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1021 struct ahd_softc *ahd;
1022 struct ahd_initiator_tinfo *tinfo;
1023 struct ahd_tmode_tstate *tstate;
1026 scb = (struct scb *)arg;
1028 ahd = scb->ahd_softc;
1032 ahd_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1034 ahd_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1036 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
1038 ahd_free_scb(ahd, scb);
1044 if (nsegments != 0) {
1046 bus_dmasync_op_t op;
1049 /* Copy the segments into our SG list */
1050 for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
1052 sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
1058 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1059 op = BUS_DMASYNC_PREREAD;
1061 op = BUS_DMASYNC_PREWRITE;
1063 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
1065 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1066 struct target_data *tdata;
1068 tdata = &scb->hscb->shared_data.tdata;
1069 tdata->target_phases |= DPHASE_PENDING;
1070 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1071 tdata->data_phase = P_DATAOUT;
1073 tdata->data_phase = P_DATAIN;
1080 * Last time we need to check if this SCB needs to
1083 if (ahd_get_transaction_status(scb) != CAM_REQ_INPROG) {
1085 bus_dmamap_unload(ahd->buffer_dmat,
1087 ahd_free_scb(ahd, scb);
1093 tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
1094 SCSIID_OUR_ID(scb->hscb->scsiid),
1095 SCSIID_TARGET(ahd, scb->hscb->scsiid),
1098 mask = SCB_GET_TARGET_MASK(ahd, scb);
1100 if ((tstate->discenable & mask) != 0
1101 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1102 scb->hscb->control |= DISCENB;
1104 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1105 scb->flags |= SCB_PACKETIZED;
1106 if (scb->hscb->task_management != 0)
1107 scb->hscb->control &= ~MK_MESSAGE;
1110 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1111 && (tinfo->goal.width != 0
1112 || tinfo->goal.period != 0
1113 || tinfo->goal.ppr_options != 0)) {
1114 scb->flags |= SCB_NEGOTIATE;
1115 scb->hscb->control |= MK_MESSAGE;
1116 } else if ((tstate->auto_negotiate & mask) != 0) {
1117 scb->flags |= SCB_AUTO_NEGOTIATE;
1118 scb->hscb->control |= MK_MESSAGE;
1121 LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1123 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1125 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1128 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1129 ccb->ccb_h.timeout = 5 * 1000;
1131 time = ccb->ccb_h.timeout;
1134 callout_reset(&ccb->ccb_h.timeout_ch, time, ahd_timeout, scb);
1137 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1138 /* Define a mapping from our tag to the SCB. */
1139 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
1141 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1142 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
1145 ahd_queue_scb(ahd, scb);
1152 ahd_poll(struct cam_sim *sim)
1154 ahd_intr(cam_sim_softc(sim));
1158 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
1159 struct ccb_scsiio *csio, struct scb *scb)
1161 struct hardware_scb *hscb;
1162 struct ccb_hdr *ccb_h;
1165 ccb_h = &csio->ccb_h;
1168 csio->sense_resid = 0;
1169 if (ccb_h->func_code == XPT_SCSI_IO) {
1170 hscb->cdb_len = csio->cdb_len;
1171 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1173 if (hscb->cdb_len > MAX_CDB_LEN
1174 && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
1176 * Should CAM start to support CDB sizes
1177 * greater than 16 bytes, we could use
1178 * the sense buffer to store the CDB.
1180 ahd_set_transaction_status(scb,
1183 ahd_free_scb(ahd, scb);
1185 xpt_done((union ccb *)csio);
1188 if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
1189 hscb->shared_data.idata.cdb_from_host.cdbptr =
1190 ahd_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
1191 hscb->shared_data.idata.cdb_from_host.cdblen =
1193 hscb->cdb_len |= SCB_CDB_LEN_PTR;
1195 memcpy(hscb->shared_data.idata.cdb,
1196 csio->cdb_io.cdb_ptr,
1200 if (hscb->cdb_len > MAX_CDB_LEN) {
1201 ahd_set_transaction_status(scb,
1204 ahd_free_scb(ahd, scb);
1206 xpt_done((union ccb *)csio);
1209 memcpy(hscb->shared_data.idata.cdb,
1210 csio->cdb_io.cdb_bytes, hscb->cdb_len);
1214 /* Only use S/G if there is a transfer */
1215 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1216 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1217 /* We've been given a pointer to a single buffer */
1218 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1222 error = bus_dmamap_load(ahd->buffer_dmat,
1228 if (error == EINPROGRESS) {
1230 * So as to maintain ordering,
1231 * freeze the controller queue
1232 * until our mapping is
1235 xpt_freeze_simq(sim,
1237 scb->io_ctx->ccb_h.status |=
1242 struct bus_dma_segment seg;
1244 /* Pointer to physical buffer */
1245 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1246 panic("ahd_setup_data - Transfer size "
1247 "larger than can device max");
1250 (bus_addr_t)(vm_offset_t)csio->data_ptr;
1251 seg.ds_len = csio->dxfer_len;
1252 ahd_execute_scb(scb, &seg, 1, 0);
1255 struct bus_dma_segment *segs;
1257 if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1258 panic("ahd_setup_data - Physical segment "
1259 "pointers unsupported");
1261 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1262 panic("ahd_setup_data - Virtual segment "
1263 "addresses unsupported");
1265 /* Just use the segments provided */
1266 segs = (struct bus_dma_segment *)csio->data_ptr;
1267 ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1270 ahd_execute_scb(scb, NULL, 0, 0);
1276 ahd_set_recoveryscb(struct ahd_softc *ahd, struct scb *scb) {
1278 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1279 struct scb *list_scb;
1281 scb->flags |= SCB_RECOVERY_SCB;
1284 * Take all queued, but not sent SCBs out of the equation.
1285 * Also ensure that no new CCBs are queued to us while we
1286 * try to fix this problem.
1288 if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1289 xpt_freeze_simq(SCB_GET_SIM(ahd, scb), /*count*/1);
1290 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1294 * Go through all of our pending SCBs and remove
1295 * any scheduled timeouts for them. We will reschedule
1296 * them after we've successfully fixed this problem.
1298 LIST_FOREACH(list_scb, &ahd->pending_scbs, pending_links) {
1301 ccb = list_scb->io_ctx;
1302 callout_stop(&ccb->ccb_h.timeout_ch);
1309 ahd_timeout(void *arg)
1312 struct ahd_softc *ahd;
1313 ahd_mode_state saved_modes;
1324 scb = (struct scb *)arg;
1325 ahd = (struct ahd_softc *)scb->ahd_softc;
1329 ahd_pause_and_flushwork(ahd);
1331 saved_modes = ahd_save_modes(ahd);
1333 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1334 ahd_outb(ahd, SCSISIGO, ACKO);
1335 kprintf("set ACK\n");
1336 ahd_outb(ahd, SCSISIGO, 0);
1337 kprintf("clearing Ack\n");
1338 ahd_restore_modes(ahd, saved_modes);
1340 if ((scb->flags & SCB_ACTIVE) == 0) {
1341 /* Previous timeout took care of me already */
1342 kprintf("%s: Timedout SCB already complete. "
1343 "Interrupts may not be functioning.\n", ahd_name(ahd));
1349 target = SCB_GET_TARGET(ahd, scb);
1350 channel = SCB_GET_CHANNEL(ahd, scb);
1351 lun = SCB_GET_LUN(scb);
1353 ahd_print_path(ahd, scb);
1354 kprintf("SCB 0x%x - timed out\n", SCB_GET_TAG(scb));
1355 ahd_dump_card_state(ahd);
1356 ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
1357 /*initiate reset*/TRUE);
1361 last_phase = ahd_inb(ahd, LASTPHASE);
1362 if (scb->sg_count > 0) {
1363 for (i = 0; i < scb->sg_count; i++) {
1364 kprintf("sg[%d] - Addr 0x%x : Length %d\n",
1366 ((struct ahd_dma_seg *)scb->sg_list)[i].addr,
1367 ((struct ahd_dma_seg *)scb->sg_list)[i].len
1371 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1373 * Been down this road before.
1374 * Do a full bus reset.
1377 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1378 found = ahd_reset_channel(ahd, channel, /*Initiate Reset*/TRUE);
1379 kprintf("%s: Issued Channel %c Bus Reset. "
1380 "%d SCBs aborted\n", ahd_name(ahd), channel, found);
1383 * If we are a target, transition to bus free and report
1386 * The target/initiator that is holding up the bus may not
1387 * be the same as the one that triggered this timeout
1388 * (different commands have different timeout lengths).
1389 * If the bus is idle and we are actiing as the initiator
1390 * for this request, queue a BDR message to the timed out
1391 * target. Otherwise, if the timed out transaction is
1393 * Initiator transaction:
1394 * Stuff the message buffer with a BDR message and assert
1395 * ATN in the hopes that the target will let go of the bus
1396 * and go to the mesgout phase. If this fails, we'll
1397 * get another timeout 2 seconds later which will attempt
1400 * Target transaction:
1401 * Transition to BUS FREE and report the error.
1402 * It's good to be the target!
1404 u_int active_scb_index;
1407 saved_scbptr = ahd_get_scbptr(ahd);
1408 active_scb_index = saved_scbptr;
1410 if (last_phase != P_BUSFREE
1411 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
1412 && (active_scb_index < ahd->scb_data.numscbs)) {
1413 struct scb *active_scb;
1416 * If the active SCB is not us, assume that
1417 * the active SCB has a longer timeout than
1418 * the timedout SCB, and wait for the active
1421 active_scb = ahd_lookup_scb(ahd, active_scb_index);
1422 if (active_scb != scb) {
1423 struct ccb_hdr *ccbh;
1424 uint64_t newtimeout;
1426 ahd_print_path(ahd, scb);
1427 kprintf("Other SCB Timeout%s",
1428 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1429 ? " again\n" : "\n");
1430 scb->flags |= SCB_OTHERTCL_TIMEOUT;
1432 MAX(active_scb->io_ctx->ccb_h.timeout,
1433 scb->io_ctx->ccb_h.timeout);
1436 ccbh = &scb->io_ctx->ccb_h;
1437 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1438 newtimeout, ahd_timeout, scb);
1445 if ((scb->hscb->control & TARGET_SCB) != 0) {
1448 * Send back any queued up transactions
1449 * and properly record the error condition.
1451 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1452 SCB_GET_CHANNEL(ahd, scb),
1458 /* Will clear us from the bus */
1464 ahd_set_recoveryscb(ahd, active_scb);
1465 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1466 ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
1467 ahd_print_path(ahd, active_scb);
1468 kprintf("BDR message in message buffer\n");
1469 active_scb->flags |= SCB_DEVICE_RESET;
1470 callout_reset(&active_scb->io_ctx->ccb_h.timeout_ch,
1471 2 * hz, ahd_timeout, active_scb);
1476 /* XXX Shouldn't panic. Just punt instead? */
1477 if ((scb->hscb->control & TARGET_SCB) != 0)
1478 panic("Timed-out target SCB but bus idle");
1480 if (last_phase != P_BUSFREE
1481 && (ahd_inb(ahd, SSTAT0) & TARGET) != 0) {
1482 /* XXX What happened to the SCB? */
1483 /* Hung target selection. Goto busfree */
1484 kprintf("%s: Hung target selection\n",
1491 if (ahd_search_qinfifo(ahd, target, channel, lun,
1492 SCB_GET_TAG(scb), ROLE_INITIATOR,
1493 /*status*/0, SEARCH_COUNT) > 0) {
1494 disconnected = FALSE;
1496 disconnected = TRUE;
1501 ahd_set_recoveryscb(ahd, scb);
1503 * Actually re-queue this SCB in an attempt
1504 * to select the device before it reconnects.
1505 * In either case (selection or reselection),
1506 * we will now issue a target reset to the
1509 * Set the MK_MESSAGE control bit indicating
1510 * that we desire to send a message. We
1511 * also set the disconnected flag since
1512 * in the paging case there is no guarantee
1513 * that our SCB control byte matches the
1514 * version on the card. We don't want the
1515 * sequencer to abort the command thinking
1516 * an unsolicited reselection occurred.
1518 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1519 scb->flags |= SCB_DEVICE_RESET;
1522 * The sequencer will never re-reference the
1523 * in-core SCB. To make sure we are notified
1524 * during reslection, set the MK_MESSAGE flag
1525 * in the card's copy of the SCB.
1527 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1528 ahd_outb(ahd, SCB_CONTROL,
1529 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
1532 * Clear out any entries in the QINFIFO first
1533 * so we are the next SCB for this target
1536 ahd_search_qinfifo(ahd,
1537 SCB_GET_TARGET(ahd, scb),
1538 channel, SCB_GET_LUN(scb),
1543 ahd_print_path(ahd, scb);
1544 kprintf("Queuing a BDR SCB\n");
1545 ahd_qinfifo_requeue_tail(ahd, scb);
1546 ahd_set_scbptr(ahd, saved_scbptr);
1547 callout_reset(&scb->io_ctx->ccb_h.timeout_ch,
1548 2 * hz, ahd_timeout, scb);
1551 /* Go "immediatly" to the bus reset */
1552 /* This shouldn't happen */
1553 ahd_set_recoveryscb(ahd, scb);
1554 ahd_print_path(ahd, scb);
1555 kprintf("SCB %d: Immediate reset. "
1556 "Flags = 0x%x\n", SCB_GET_TAG(scb),
1567 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1569 union ccb *abort_ccb;
1571 abort_ccb = ccb->cab.abort_ccb;
1572 switch (abort_ccb->ccb_h.func_code) {
1573 #ifdef AHD_TARGET_MODE
1574 case XPT_ACCEPT_TARGET_IO:
1575 case XPT_IMMED_NOTIFY:
1576 case XPT_CONT_TARGET_IO:
1578 struct ahd_tmode_tstate *tstate;
1579 struct ahd_tmode_lstate *lstate;
1580 struct ccb_hdr_slist *list;
1583 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1586 if (status != CAM_REQ_CMP) {
1587 ccb->ccb_h.status = status;
1591 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1592 list = &lstate->accept_tios;
1593 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1594 list = &lstate->immed_notifies;
1599 struct ccb_hdr *curelm;
1602 curelm = SLIST_FIRST(list);
1604 if (curelm == &abort_ccb->ccb_h) {
1606 SLIST_REMOVE_HEAD(list, sim_links.sle);
1608 while(curelm != NULL) {
1609 struct ccb_hdr *nextelm;
1612 SLIST_NEXT(curelm, sim_links.sle);
1614 if (nextelm == &abort_ccb->ccb_h) {
1627 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1628 xpt_done(abort_ccb);
1629 ccb->ccb_h.status = CAM_REQ_CMP;
1631 xpt_print_path(abort_ccb->ccb_h.path);
1632 kprintf("Not found\n");
1633 ccb->ccb_h.status = CAM_PATH_INVALID;
1641 /* XXX Fully implement the hard ones */
1642 ccb->ccb_h.status = CAM_UA_ABORT;
1645 ccb->ccb_h.status = CAM_REQ_INVALID;
1652 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1653 u_int lun, ac_code code, void *opt_arg)
1655 struct ccb_trans_settings cts;
1656 struct cam_path *path;
1661 error = ahd_create_path(ahd, channel, target, lun, &path);
1663 if (error != CAM_REQ_CMP)
1667 case AC_TRANSFER_NEG:
1669 #ifdef AHD_NEW_TRAN_SETTINGS
1670 struct ccb_trans_settings_scsi *scsi;
1672 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1673 scsi = &cts.proto_specific.scsi;
1675 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1677 cts.ccb_h.path = path;
1678 cts.ccb_h.target_id = target;
1679 cts.ccb_h.target_lun = lun;
1680 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1682 #ifdef AHD_NEW_TRAN_SETTINGS
1683 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1684 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1686 cts.valid &= ~CCB_TRANS_TQ_VALID;
1687 cts.flags &= ~CCB_TRANS_TAG_ENB;
1689 if (opt_arg == NULL)
1691 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1692 #ifdef AHD_NEW_TRAN_SETTINGS
1693 scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1694 scsi->valid |= CTS_SCSI_VALID_TQ;
1696 cts.flags |= CCB_TRANS_TAG_ENB;
1697 cts.valid |= CCB_TRANS_TQ_VALID;
1705 panic("ahd_send_async: Unexpected async event");
1707 xpt_async(code, path, arg);
1708 xpt_free_path(path);
1712 ahd_platform_set_tags(struct ahd_softc *ahd,
1713 struct ahd_devinfo *devinfo, int enable)
1718 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1720 ahd->platform_data = kmalloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1721 M_INTWAIT | M_ZERO);
1726 ahd_platform_free(struct ahd_softc *ahd)
1728 struct ahd_platform_data *pdata;
1730 pdata = ahd->platform_data;
1731 if (pdata != NULL) {
1732 if (pdata->regs[0] != NULL)
1733 bus_release_resource(ahd->dev_softc,
1734 pdata->regs_res_type[0],
1735 pdata->regs_res_id[0],
1738 if (pdata->regs[1] != NULL)
1739 bus_release_resource(ahd->dev_softc,
1740 pdata->regs_res_type[1],
1741 pdata->regs_res_id[1],
1744 if (pdata->irq != NULL)
1745 bus_release_resource(ahd->dev_softc,
1746 pdata->irq_res_type,
1749 if (pdata->sim_b != NULL) {
1750 xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1751 xpt_free_path(pdata->path_b);
1752 xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1753 cam_sim_free(pdata->sim_b);
1755 if (pdata->sim != NULL) {
1756 xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1757 xpt_free_path(pdata->path);
1758 xpt_bus_deregister(cam_sim_path(pdata->sim));
1759 cam_sim_free(pdata->sim);
1761 if (pdata->eh != NULL)
1762 EVENTHANDLER_DEREGISTER(shutdown_post_sync, pdata->eh);
1763 kfree(ahd->platform_data, M_DEVBUF);
1768 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1770 /* We don't sort softcs under FreeBSD so report equal always */
1775 ahd_detach(device_t dev)
1777 struct ahd_softc *ahd;
1779 device_printf(dev, "detaching device\n");
1780 ahd = device_get_softc(dev);
1781 ahd = ahd_find_softc(ahd);
1783 device_printf(dev, "aic7xxx already detached\n");
1787 ahd_intr_enable(ahd, FALSE);
1788 bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1796 ahd_dump_targcmd(struct target_cmd *cmd)
1802 byte = &cmd->initiator_channel;
1803 /* Debugging info for received commands */
1804 last_byte = &cmd[1].initiator_channel;
1807 while (byte < last_byte) {
1810 kprintf("%#x", *byte++);
1823 ahd_modevent(module_t mod, int type, void *data)
1825 /* XXX Deal with busy status on unload. */
1829 static moduledata_t ahd_mod = {
1835 /********************************** DDB Hooks *********************************/
1837 static struct ahd_softc *ahd_ddb_softc;
1838 static int ahd_ddb_paused;
1839 static int ahd_ddb_paused_on_entry;
1840 DB_COMMAND(ahd_set_unit, ahd_ddb_set_unit)
1842 struct ahd_softc *list_ahd;
1844 ahd_ddb_softc = NULL;
1845 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1846 if (list_ahd->unit == addr)
1847 ahd_ddb_softc = list_ahd;
1849 if (ahd_ddb_softc == NULL)
1850 db_error("No matching softc found!\n");
1853 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1855 if (ahd_ddb_softc == NULL) {
1856 db_error("Must set unit with ahd_set_unit first!\n");
1859 if (ahd_ddb_paused == 0) {
1861 if (ahd_is_paused(ahd_ddb_softc)) {
1862 ahd_ddb_paused_on_entry++;
1865 ahd_pause(ahd_ddb_softc);
1869 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1871 if (ahd_ddb_softc == NULL) {
1872 db_error("Must set unit with ahd_set_unit first!\n");
1875 if (ahd_ddb_paused != 0) {
1877 if (ahd_ddb_paused_on_entry)
1879 ahd_unpause(ahd_ddb_softc);
1880 } else if (ahd_ddb_paused_on_entry != 0) {
1881 /* Two unpauses to clear a paused on entry. */
1882 ahd_ddb_paused_on_entry = 0;
1883 ahd_unpause(ahd_ddb_softc);
1887 DB_COMMAND(ahd_in, ahd_ddb_in)
1892 if (ahd_ddb_softc == NULL) {
1893 db_error("Must set unit with ahd_set_unit first!\n");
1900 while ((c = *modif++) != '\0') {
1916 while (--count >= 0) {
1917 db_printf("%04lx (M)%x: \t", (u_long)addr,
1918 ahd_inb(ahd_ddb_softc, MODE_PTR));
1921 db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1924 db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1927 db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1933 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1935 db_expr_t old_value;
1936 db_expr_t new_value;
1939 if (ahd_ddb_softc == NULL) {
1940 db_error("Must set unit with ahd_set_unit first!\n");
1956 db_error("Unknown size\n");
1960 while (db_expression(&new_value)) {
1964 old_value = ahd_inb(ahd_ddb_softc, addr);
1965 ahd_outb(ahd_ddb_softc, addr, new_value);
1968 old_value = ahd_inw(ahd_ddb_softc, addr);
1969 ahd_outw(ahd_ddb_softc, addr, new_value);
1972 old_value = ahd_inl(ahd_ddb_softc, addr);
1973 ahd_outl(ahd_ddb_softc, addr, new_value);
1976 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1977 (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1978 (u_long)old_value, (u_long)new_value);
1987 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1988 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1989 MODULE_VERSION(ahd, 1);