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#20 $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <dev/aic7xxx/aic7xxx_osm.h>
38 #include <dev/aic7xxx/aic7xxx_inline.h>
40 #include <sys/kthread.h>
42 #ifndef AHC_TMODE_ENABLE
43 #define AHC_TMODE_ENABLE 0
46 #include <dev/aic7xxx/aic_osm_lib.c>
48 #define ccb_scb_ptr spriv_ptr0
50 devclass_t ahc_devclass;
53 static void ahc_dump_targcmd(struct target_cmd *cmd);
55 static int ahc_modevent(module_t mod, int type, void *data);
56 static void ahc_action(struct cam_sim *sim, union ccb *ccb);
57 static void ahc_get_tran_settings(struct ahc_softc *ahc,
58 int our_id, char channel,
59 struct ccb_trans_settings *cts);
60 static void ahc_async(void *callback_arg, uint32_t code,
61 struct cam_path *path, void *arg);
62 static void ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
63 int nsegments, int error);
64 static void ahc_poll(struct cam_sim *sim);
65 static void ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
66 struct ccb_scsiio *csio, struct scb *scb);
67 static void ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
69 static int ahc_create_path(struct ahc_softc *ahc,
70 char channel, u_int target, u_int lun,
71 struct cam_path **path);
74 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
75 u_int lun, struct cam_path **path)
80 path_id = cam_sim_path(ahc->platform_data->sim_b);
82 path_id = cam_sim_path(ahc->platform_data->sim);
84 return (xpt_create_path(path, /*periph*/NULL,
85 path_id, target, lun));
89 ahc_map_int(struct ahc_softc *ahc)
96 shareable = (ahc->flags & AHC_EDGE_INTERRUPT) ? 0: RF_SHAREABLE;
97 ahc->platform_data->irq =
98 bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IRQ, &zero,
99 RF_ACTIVE | shareable);
100 if (ahc->platform_data->irq == NULL) {
101 device_printf(ahc->dev_softc,
102 "bus_alloc_resource() failed to allocate IRQ\n");
105 ahc->platform_data->irq_res_type = SYS_RES_IRQ;
107 /* Hook up our interrupt handler */
108 error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
109 INTR_TYPE_CAM|INTR_MPSAFE, NULL,
110 ahc_platform_intr, ahc, &ahc->platform_data->ih);
113 device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
119 aic7770_map_registers(struct ahc_softc *ahc, u_int unused_ioport_arg)
121 struct resource *regs;
125 regs = bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IOPORT, &rid,
128 device_printf(ahc->dev_softc, "Unable to map I/O space?!\n");
131 ahc->platform_data->regs_res_type = SYS_RES_IOPORT;
132 ahc->platform_data->regs_res_id = rid;
133 ahc->platform_data->regs = regs;
134 ahc->tag = rman_get_bustag(regs);
135 ahc->bsh = rman_get_bushandle(regs);
140 * Attach all the sub-devices we can find
143 ahc_attach(struct ahc_softc *ahc)
146 struct ccb_setasync csa;
147 struct cam_devq *devq;
151 struct cam_sim *sim2;
152 struct cam_path *path;
153 struct cam_path *path2;
163 * Create a thread to perform all recovery.
165 if (ahc_spawn_recovery_thread(ahc) != 0)
168 ahc_controller_info(ahc, ahc_info);
169 printf("%s\n", ahc_info);
173 * Attach secondary channel first if the user has
174 * declared it the primary channel.
176 if ((ahc->features & AHC_TWIN) != 0
177 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
186 * Create the device queue for our SIM(s).
188 devq = cam_simq_alloc(AHC_MAX_QUEUE);
193 * Construct our first channel SIM entry
195 sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
196 device_get_unit(ahc->dev_softc),
197 &ahc->platform_data->mtx, 1, AHC_MAX_QUEUE, devq);
203 if (xpt_bus_register(sim, ahc->dev_softc, bus_id) != CAM_SUCCESS) {
204 cam_sim_free(sim, /*free_devq*/TRUE);
209 if (xpt_create_path(&path, /*periph*/NULL,
210 cam_sim_path(sim), CAM_TARGET_WILDCARD,
211 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
212 xpt_bus_deregister(cam_sim_path(sim));
213 cam_sim_free(sim, /*free_devq*/TRUE);
218 memset(&csa, 0, sizeof(csa));
219 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
220 csa.ccb_h.func_code = XPT_SASYNC_CB;
221 csa.event_enable = AC_LOST_DEVICE;
222 csa.callback = ahc_async;
223 csa.callback_arg = sim;
224 xpt_action((union ccb *)&csa);
227 if (ahc->features & AHC_TWIN) {
228 sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
229 ahc, device_get_unit(ahc->dev_softc),
230 &ahc->platform_data->mtx, 1,
231 AHC_MAX_QUEUE, devq);
234 printf("ahc_attach: Unable to attach second "
235 "bus due to resource shortage");
239 if (xpt_bus_register(sim2, ahc->dev_softc, bus_id2) !=
241 printf("ahc_attach: Unable to attach second "
242 "bus due to resource shortage");
244 * We do not want to destroy the device queue
245 * because the first bus is using it.
247 cam_sim_free(sim2, /*free_devq*/FALSE);
251 if (xpt_create_path(&path2, /*periph*/NULL,
254 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
255 xpt_bus_deregister(cam_sim_path(sim2));
256 cam_sim_free(sim2, /*free_devq*/FALSE);
260 xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
261 csa.ccb_h.func_code = XPT_SASYNC_CB;
262 csa.event_enable = AC_LOST_DEVICE;
263 csa.callback = ahc_async;
264 csa.callback_arg = sim2;
265 xpt_action((union ccb *)&csa);
270 if ((ahc->features & AHC_TWIN) != 0
271 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
272 ahc->platform_data->sim_b = sim;
273 ahc->platform_data->path_b = path;
274 ahc->platform_data->sim = sim2;
275 ahc->platform_data->path = path2;
277 ahc->platform_data->sim = sim;
278 ahc->platform_data->path = path;
279 ahc->platform_data->sim_b = sim2;
280 ahc->platform_data->path_b = path2;
285 /* We have to wait until after any system dumps... */
286 ahc->platform_data->eh =
287 EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
288 ahc, SHUTDOWN_PRI_DEFAULT);
289 ahc_intr_enable(ahc, TRUE);
296 * Catch an interrupt from the adapter
299 ahc_platform_intr(void *arg)
301 struct ahc_softc *ahc;
303 ahc = (struct ahc_softc *)arg;
310 * We have an scb which has been processed by the
311 * adaptor, now we look to see how the operation
315 ahc_done(struct ahc_softc *ahc, struct scb *scb)
319 CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
320 ("ahc_done - scb %d\n", scb->hscb->tag));
323 LIST_REMOVE(scb, pending_links);
324 if ((scb->flags & SCB_TIMEDOUT) != 0)
325 LIST_REMOVE(scb, timedout_links);
326 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
327 struct scb_tailq *untagged_q;
330 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
331 untagged_q = &ahc->untagged_queues[target_offset];
332 TAILQ_REMOVE(untagged_q, scb, links.tqe);
333 scb->flags &= ~SCB_UNTAGGEDQ;
334 ahc_run_untagged_queue(ahc, untagged_q);
337 callout_stop(&scb->io_timer);
339 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
342 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
343 op = BUS_DMASYNC_POSTREAD;
345 op = BUS_DMASYNC_POSTWRITE;
346 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
347 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
350 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
351 struct cam_path *ccb_path;
354 * If we have finally disconnected, clean up our
355 * pending device state.
356 * XXX - There may be error states that cause where
357 * we will remain connected.
359 ccb_path = ccb->ccb_h.path;
360 if (ahc->pending_device != NULL
361 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
362 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
363 ahc->pending_device = NULL;
366 xpt_print_path(ccb->ccb_h.path);
367 printf("Still connected\n");
373 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
374 ccb->ccb_h.status |= CAM_REQ_CMP;
375 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
376 ahc_free_scb(ahc, scb);
382 * If the recovery SCB completes, we have to be
383 * out of our timeout.
385 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
386 struct scb *list_scb;
388 ahc->scb_data->recovery_scbs--;
390 if (aic_get_transaction_status(scb) == CAM_BDR_SENT
391 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
392 aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
394 if (ahc->scb_data->recovery_scbs == 0) {
396 * All recovery actions have completed successfully,
397 * so reinstate the timeouts for all other pending
400 LIST_FOREACH(list_scb, &ahc->pending_scbs,
402 aic_scb_timer_reset(list_scb,
403 aic_get_timeout(scb));
406 ahc_print_path(ahc, scb);
407 printf("no longer in timeout, status = %x\n",
412 /* Don't clobber any existing error state */
413 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
414 ccb->ccb_h.status |= CAM_REQ_CMP;
415 } else if ((scb->flags & SCB_SENSE) != 0) {
417 * We performed autosense retrieval.
419 * Zero any sense not transferred by the
420 * device. The SCSI spec mandates that any
421 * untransfered data should be assumed to be
422 * zero. Complete the 'bounce' of sense information
423 * through buffers accessible via bus-space by
424 * copying it into the clients csio.
426 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
427 memcpy(&ccb->csio.sense_data,
428 ahc_get_sense_buf(ahc, scb),
429 (aic_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
430 - ccb->csio.sense_resid);
431 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
433 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
434 ahc_free_scb(ahc, scb);
439 ahc_action(struct cam_sim *sim, union ccb *ccb)
441 struct ahc_softc *ahc;
442 struct ahc_tmode_lstate *lstate;
446 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
448 ahc = (struct ahc_softc *)cam_sim_softc(sim);
450 target_id = ccb->ccb_h.target_id;
451 our_id = SIM_SCSI_ID(ahc, sim);
453 switch (ccb->ccb_h.func_code) {
454 /* Common cases first */
455 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
456 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
458 struct ahc_tmode_tstate *tstate;
461 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
464 if (status != CAM_REQ_CMP) {
465 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
466 /* Response from the black hole device */
468 lstate = ahc->black_hole;
470 ccb->ccb_h.status = status;
475 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
476 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
478 ccb->ccb_h.status = CAM_REQ_INPROG;
479 if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
480 ahc_run_tqinfifo(ahc, /*paused*/FALSE);
485 * The target_id represents the target we attempt to
486 * select. In target mode, this is the initiator of
487 * the original command.
490 target_id = ccb->csio.init_id;
493 case XPT_SCSI_IO: /* Execute the requested I/O operation */
494 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
497 struct hardware_scb *hscb;
499 if ((ahc->flags & AHC_INITIATORROLE) == 0
500 && (ccb->ccb_h.func_code == XPT_SCSI_IO
501 || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
502 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
510 if ((scb = ahc_get_scb(ahc)) == NULL) {
511 xpt_freeze_simq(sim, /*count*/1);
512 ahc->flags |= AHC_RESOURCE_SHORTAGE;
513 ccb->ccb_h.status = CAM_REQUEUE_REQ;
520 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
521 ("start scb(%p)\n", scb));
524 * So we can find the SCB when an abort is requested
526 ccb->ccb_h.ccb_scb_ptr = scb;
529 * Put all the arguments for the xfer in the scb
532 hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
533 hscb->lun = ccb->ccb_h.target_lun;
534 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
536 scb->flags |= SCB_DEVICE_RESET;
537 hscb->control |= MK_MESSAGE;
538 ahc_execute_scb(scb, NULL, 0, 0);
540 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
541 struct target_data *tdata;
543 tdata = &hscb->shared_data.tdata;
544 if (ahc->pending_device == lstate)
545 scb->flags |= SCB_TARGET_IMMEDIATE;
546 hscb->control |= TARGET_SCB;
547 scb->flags |= SCB_TARGET_SCB;
548 tdata->target_phases = 0;
549 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
550 tdata->target_phases |= SPHASE_PENDING;
552 ccb->csio.scsi_status;
554 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
555 tdata->target_phases |= NO_DISCONNECT;
557 tdata->initiator_tag = ccb->csio.tag_id;
559 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
560 hscb->control |= ccb->csio.tag_action;
562 ahc_setup_data(ahc, sim, &ccb->csio, scb);
566 case XPT_NOTIFY_ACKNOWLEDGE:
567 case XPT_IMMEDIATE_NOTIFY:
569 struct ahc_tmode_tstate *tstate;
570 struct ahc_tmode_lstate *lstate;
573 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
576 if (status != CAM_REQ_CMP) {
577 ccb->ccb_h.status = status;
581 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
583 ccb->ccb_h.status = CAM_REQ_INPROG;
584 ahc_send_lstate_events(ahc, lstate);
587 case XPT_EN_LUN: /* Enable LUN as a target */
588 ahc_handle_en_lun(ahc, sim, ccb);
591 case XPT_ABORT: /* Abort the specified CCB */
593 ahc_abort_ccb(ahc, sim, ccb);
596 case XPT_SET_TRAN_SETTINGS:
598 struct ahc_devinfo devinfo;
599 struct ccb_trans_settings *cts;
600 struct ccb_trans_settings_scsi *scsi;
601 struct ccb_trans_settings_spi *spi;
602 struct ahc_initiator_tinfo *tinfo;
603 struct ahc_tmode_tstate *tstate;
604 uint16_t *discenable;
609 scsi = &cts->proto_specific.scsi;
610 spi = &cts->xport_specific.spi;
611 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
612 cts->ccb_h.target_id,
613 cts->ccb_h.target_lun,
614 SIM_CHANNEL(ahc, sim),
616 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
618 devinfo.target, &tstate);
620 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
621 update_type |= AHC_TRANS_GOAL;
622 discenable = &tstate->discenable;
623 tagenable = &tstate->tagenable;
624 tinfo->curr.protocol_version =
625 cts->protocol_version;
626 tinfo->curr.transport_version =
627 cts->transport_version;
628 tinfo->goal.protocol_version =
629 cts->protocol_version;
630 tinfo->goal.transport_version =
631 cts->transport_version;
632 } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
633 update_type |= AHC_TRANS_USER;
634 discenable = &ahc->user_discenable;
635 tagenable = &ahc->user_tagenable;
636 tinfo->user.protocol_version =
637 cts->protocol_version;
638 tinfo->user.transport_version =
639 cts->transport_version;
641 ccb->ccb_h.status = CAM_REQ_INVALID;
646 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
647 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
648 *discenable |= devinfo.target_mask;
650 *discenable &= ~devinfo.target_mask;
653 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
654 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
655 *tagenable |= devinfo.target_mask;
657 *tagenable &= ~devinfo.target_mask;
660 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
661 ahc_validate_width(ahc, /*tinfo limit*/NULL,
662 &spi->bus_width, ROLE_UNKNOWN);
663 ahc_set_width(ahc, &devinfo, spi->bus_width,
664 update_type, /*paused*/FALSE);
667 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
668 if (update_type == AHC_TRANS_USER)
669 spi->ppr_options = tinfo->user.ppr_options;
671 spi->ppr_options = tinfo->goal.ppr_options;
674 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
675 if (update_type == AHC_TRANS_USER)
676 spi->sync_offset = tinfo->user.offset;
678 spi->sync_offset = tinfo->goal.offset;
681 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
682 if (update_type == AHC_TRANS_USER)
683 spi->sync_period = tinfo->user.period;
685 spi->sync_period = tinfo->goal.period;
688 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
689 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
690 struct ahc_syncrate *syncrate;
693 if ((ahc->features & AHC_ULTRA2) != 0)
694 maxsync = AHC_SYNCRATE_DT;
695 else if ((ahc->features & AHC_ULTRA) != 0)
696 maxsync = AHC_SYNCRATE_ULTRA;
698 maxsync = AHC_SYNCRATE_FAST;
700 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
701 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
703 syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
706 ahc_validate_offset(ahc, /*tinfo limit*/NULL,
707 syncrate, &spi->sync_offset,
708 spi->bus_width, ROLE_UNKNOWN);
710 /* We use a period of 0 to represent async */
711 if (spi->sync_offset == 0) {
712 spi->sync_period = 0;
713 spi->ppr_options = 0;
716 ahc_set_syncrate(ahc, &devinfo, syncrate,
717 spi->sync_period, spi->sync_offset,
718 spi->ppr_options, update_type,
721 ccb->ccb_h.status = CAM_REQ_CMP;
725 case XPT_GET_TRAN_SETTINGS:
726 /* Get default/user set transfer settings for the target */
728 ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
729 SIM_CHANNEL(ahc, sim), &ccb->cts);
733 case XPT_CALC_GEOMETRY:
737 extended = SIM_IS_SCSIBUS_B(ahc, sim)
738 ? ahc->flags & AHC_EXTENDED_TRANS_B
739 : ahc->flags & AHC_EXTENDED_TRANS_A;
740 aic_calc_geometry(&ccb->ccg, extended);
744 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
748 found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
749 /*initiate reset*/TRUE);
751 xpt_print_path(SIM_PATH(ahc, sim));
752 printf("SCSI bus reset delivered. "
753 "%d SCBs aborted.\n", found);
755 ccb->ccb_h.status = CAM_REQ_CMP;
759 case XPT_TERM_IO: /* Terminate the I/O process */
761 ccb->ccb_h.status = CAM_REQ_INVALID;
764 case XPT_PATH_INQ: /* Path routing inquiry */
766 struct ccb_pathinq *cpi = &ccb->cpi;
768 cpi->version_num = 1; /* XXX??? */
769 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
770 if ((ahc->features & AHC_WIDE) != 0)
771 cpi->hba_inquiry |= PI_WIDE_16;
772 if ((ahc->features & AHC_TARGETMODE) != 0) {
773 cpi->target_sprt = PIT_PROCESSOR
777 cpi->target_sprt = 0;
780 cpi->hba_eng_cnt = 0;
781 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
782 cpi->max_lun = AHC_NUM_LUNS - 1;
783 if (SIM_IS_SCSIBUS_B(ahc, sim)) {
784 cpi->initiator_id = ahc->our_id_b;
785 if ((ahc->flags & AHC_RESET_BUS_B) == 0)
786 cpi->hba_misc |= PIM_NOBUSRESET;
788 cpi->initiator_id = ahc->our_id;
789 if ((ahc->flags & AHC_RESET_BUS_A) == 0)
790 cpi->hba_misc |= PIM_NOBUSRESET;
792 cpi->bus_id = cam_sim_bus(sim);
793 cpi->base_transfer_speed = 3300;
794 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
795 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
796 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
797 cpi->unit_number = cam_sim_unit(sim);
798 cpi->protocol = PROTO_SCSI;
799 cpi->protocol_version = SCSI_REV_2;
800 cpi->transport = XPORT_SPI;
801 cpi->transport_version = 2;
802 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
803 if ((ahc->features & AHC_DT) != 0) {
804 cpi->transport_version = 3;
805 cpi->xport_specific.spi.ppr_options =
808 cpi->ccb_h.status = CAM_REQ_CMP;
813 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
820 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
821 struct ccb_trans_settings *cts)
823 struct ahc_devinfo devinfo;
824 struct ccb_trans_settings_scsi *scsi;
825 struct ccb_trans_settings_spi *spi;
826 struct ahc_initiator_tinfo *targ_info;
827 struct ahc_tmode_tstate *tstate;
828 struct ahc_transinfo *tinfo;
830 scsi = &cts->proto_specific.scsi;
831 spi = &cts->xport_specific.spi;
832 ahc_compile_devinfo(&devinfo, our_id,
833 cts->ccb_h.target_id,
834 cts->ccb_h.target_lun,
835 channel, ROLE_UNKNOWN);
836 targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
838 devinfo.target, &tstate);
840 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
841 tinfo = &targ_info->curr;
843 tinfo = &targ_info->user;
845 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
846 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
847 if (cts->type == CTS_TYPE_USER_SETTINGS) {
848 if ((ahc->user_discenable & devinfo.target_mask) != 0)
849 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
851 if ((ahc->user_tagenable & devinfo.target_mask) != 0)
852 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
854 if ((tstate->discenable & devinfo.target_mask) != 0)
855 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
857 if ((tstate->tagenable & devinfo.target_mask) != 0)
858 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
860 cts->protocol_version = tinfo->protocol_version;
861 cts->transport_version = tinfo->transport_version;
863 spi->sync_period = tinfo->period;
864 spi->sync_offset = tinfo->offset;
865 spi->bus_width = tinfo->width;
866 spi->ppr_options = tinfo->ppr_options;
868 cts->protocol = PROTO_SCSI;
869 cts->transport = XPORT_SPI;
870 spi->valid = CTS_SPI_VALID_SYNC_RATE
871 | CTS_SPI_VALID_SYNC_OFFSET
872 | CTS_SPI_VALID_BUS_WIDTH
873 | CTS_SPI_VALID_PPR_OPTIONS;
875 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
876 scsi->valid = CTS_SCSI_VALID_TQ;
877 spi->valid |= CTS_SPI_VALID_DISC;
882 cts->ccb_h.status = CAM_REQ_CMP;
886 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
888 struct ahc_softc *ahc;
891 sim = (struct cam_sim *)callback_arg;
892 ahc = (struct ahc_softc *)cam_sim_softc(sim);
896 struct ahc_devinfo devinfo;
898 ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
899 xpt_path_target_id(path),
900 xpt_path_lun_id(path),
901 SIM_CHANNEL(ahc, sim),
905 * Revert to async/narrow transfers
906 * for the next device.
908 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
909 AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
910 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
911 /*period*/0, /*offset*/0, /*ppr_options*/0,
912 AHC_TRANS_GOAL|AHC_TRANS_CUR,
922 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
927 struct ahc_softc *ahc;
928 struct ahc_initiator_tinfo *tinfo;
929 struct ahc_tmode_tstate *tstate;
932 scb = (struct scb *)arg;
934 ahc = scb->ahc_softc;
938 aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
940 aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
942 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
943 ahc_free_scb(ahc, scb);
947 if (nsegments != 0) {
948 struct ahc_dma_seg *sg;
949 bus_dma_segment_t *end_seg;
952 end_seg = dm_segs + nsegments;
954 /* Copy the segments into our SG list */
956 while (dm_segs < end_seg) {
959 sg->addr = aic_htole32(dm_segs->ds_addr);
960 len = dm_segs->ds_len
961 | ((dm_segs->ds_addr >> 8) & 0x7F000000);
962 sg->len = aic_htole32(len);
968 * Note where to find the SG entries in bus space.
969 * We also set the full residual flag which the
970 * sequencer will clear as soon as a data transfer
973 scb->hscb->sgptr = aic_htole32(scb->sg_list_phys|SG_FULL_RESID);
975 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
976 op = BUS_DMASYNC_PREREAD;
978 op = BUS_DMASYNC_PREWRITE;
980 bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
982 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
983 struct target_data *tdata;
985 tdata = &scb->hscb->shared_data.tdata;
986 tdata->target_phases |= DPHASE_PENDING;
988 * CAM data direction is relative to the initiator.
990 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
991 tdata->data_phase = P_DATAOUT;
993 tdata->data_phase = P_DATAIN;
996 * If the transfer is of an odd length and in the
997 * "in" direction (scsi->HostBus), then it may
998 * trigger a bug in the 'WideODD' feature of
999 * non-Ultra2 chips. Force the total data-length
1000 * to be even by adding an extra, 1 byte, SG,
1001 * element. We do this even if we are not currently
1002 * negotiated wide as negotiation could occur before
1003 * this command is executed.
1005 if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1006 && (ccb->csio.dxfer_len & 0x1) != 0
1007 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1009 if (nsegments > AHC_NSEG) {
1010 aic_set_transaction_status(scb,
1012 bus_dmamap_unload(ahc->buffer_dmat,
1014 ahc_free_scb(ahc, scb);
1018 sg->addr = aic_htole32(ahc->dma_bug_buf);
1019 sg->len = aic_htole32(1);
1024 sg->len |= aic_htole32(AHC_DMA_LAST_SEG);
1026 /* Copy the first SG into the "current" data pointer area */
1027 scb->hscb->dataptr = scb->sg_list->addr;
1028 scb->hscb->datacnt = scb->sg_list->len;
1030 scb->hscb->sgptr = aic_htole32(SG_LIST_NULL);
1031 scb->hscb->dataptr = 0;
1032 scb->hscb->datacnt = 0;
1035 scb->sg_count = nsegments;
1038 * Last time we need to check if this SCB needs to
1041 if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1043 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1044 ahc_free_scb(ahc, scb);
1049 tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1050 SCSIID_OUR_ID(scb->hscb->scsiid),
1051 SCSIID_TARGET(ahc, scb->hscb->scsiid),
1054 mask = SCB_GET_TARGET_MASK(ahc, scb);
1055 scb->hscb->scsirate = tinfo->scsirate;
1056 scb->hscb->scsioffset = tinfo->curr.offset;
1057 if ((tstate->ultraenb & mask) != 0)
1058 scb->hscb->control |= ULTRAENB;
1060 if ((tstate->discenable & mask) != 0
1061 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1062 scb->hscb->control |= DISCENB;
1064 if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1065 && (tinfo->goal.width != 0
1066 || tinfo->goal.offset != 0
1067 || tinfo->goal.ppr_options != 0)) {
1068 scb->flags |= SCB_NEGOTIATE;
1069 scb->hscb->control |= MK_MESSAGE;
1070 } else if ((tstate->auto_negotiate & mask) != 0) {
1071 scb->flags |= SCB_AUTO_NEGOTIATE;
1072 scb->hscb->control |= MK_MESSAGE;
1075 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1077 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1080 * We only allow one untagged transaction
1081 * per target in the initiator role unless
1082 * we are storing a full busy target *lun*
1083 * table in SCB space.
1085 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1086 && (ahc->flags & AHC_SCB_BTT) == 0) {
1087 struct scb_tailq *untagged_q;
1090 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1091 untagged_q = &(ahc->untagged_queues[target_offset]);
1092 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1093 scb->flags |= SCB_UNTAGGEDQ;
1094 if (TAILQ_FIRST(untagged_q) != scb) {
1098 scb->flags |= SCB_ACTIVE;
1101 * Timers are disabled while recovery is in progress.
1103 aic_scb_timer_start(scb);
1105 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1106 /* Define a mapping from our tag to the SCB. */
1107 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1109 if ((ahc->flags & AHC_PAGESCBS) == 0)
1110 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1111 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1114 ahc_queue_scb(ahc, scb);
1119 ahc_poll(struct cam_sim *sim)
1121 struct ahc_softc *ahc;
1123 ahc = (struct ahc_softc *)cam_sim_softc(sim);
1128 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1129 struct ccb_scsiio *csio, struct scb *scb)
1131 struct hardware_scb *hscb;
1132 struct ccb_hdr *ccb_h;
1136 ccb_h = &csio->ccb_h;
1139 csio->sense_resid = 0;
1140 if (ccb_h->func_code == XPT_SCSI_IO) {
1141 hscb->cdb_len = csio->cdb_len;
1142 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1143 if (hscb->cdb_len > sizeof(hscb->cdb32)
1144 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1145 aic_set_transaction_status(scb,
1147 ahc_free_scb(ahc, scb);
1148 xpt_done((union ccb *)csio);
1151 if (hscb->cdb_len > 12) {
1153 csio->cdb_io.cdb_ptr,
1155 scb->flags |= SCB_CDB32_PTR;
1157 memcpy(hscb->shared_data.cdb,
1158 csio->cdb_io.cdb_ptr,
1162 if (hscb->cdb_len > 12) {
1163 memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1165 scb->flags |= SCB_CDB32_PTR;
1167 memcpy(hscb->shared_data.cdb,
1168 csio->cdb_io.cdb_bytes,
1174 error = bus_dmamap_load_ccb(ahc->buffer_dmat,
1180 if (error == EINPROGRESS) {
1182 * So as to maintain ordering,
1183 * freeze the controller queue
1184 * until our mapping is
1187 xpt_freeze_simq(sim, /*count*/1);
1188 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1193 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1195 union ccb *abort_ccb;
1197 abort_ccb = ccb->cab.abort_ccb;
1198 switch (abort_ccb->ccb_h.func_code) {
1199 case XPT_ACCEPT_TARGET_IO:
1200 case XPT_IMMEDIATE_NOTIFY:
1201 case XPT_CONT_TARGET_IO:
1203 struct ahc_tmode_tstate *tstate;
1204 struct ahc_tmode_lstate *lstate;
1205 struct ccb_hdr_slist *list;
1208 status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1211 if (status != CAM_REQ_CMP) {
1212 ccb->ccb_h.status = status;
1216 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1217 list = &lstate->accept_tios;
1218 else if (abort_ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
1219 list = &lstate->immed_notifies;
1224 struct ccb_hdr *curelm;
1227 curelm = SLIST_FIRST(list);
1229 if (curelm == &abort_ccb->ccb_h) {
1231 SLIST_REMOVE_HEAD(list, sim_links.sle);
1233 while(curelm != NULL) {
1234 struct ccb_hdr *nextelm;
1237 SLIST_NEXT(curelm, sim_links.sle);
1239 if (nextelm == &abort_ccb->ccb_h) {
1252 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1253 xpt_done(abort_ccb);
1254 ccb->ccb_h.status = CAM_REQ_CMP;
1256 xpt_print_path(abort_ccb->ccb_h.path);
1257 printf("Not found\n");
1258 ccb->ccb_h.status = CAM_PATH_INVALID;
1265 /* XXX Fully implement the hard ones */
1266 ccb->ccb_h.status = CAM_UA_ABORT;
1269 ccb->ccb_h.status = CAM_REQ_INVALID;
1276 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1277 u_int lun, ac_code code, void *opt_arg)
1279 struct ccb_trans_settings cts;
1280 struct cam_path *path;
1285 error = ahc_create_path(ahc, channel, target, lun, &path);
1287 if (error != CAM_REQ_CMP)
1291 case AC_TRANSFER_NEG:
1293 struct ccb_trans_settings_scsi *scsi;
1295 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1296 scsi = &cts.proto_specific.scsi;
1297 cts.ccb_h.path = path;
1298 cts.ccb_h.target_id = target;
1299 cts.ccb_h.target_lun = lun;
1300 ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1304 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1305 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1306 if (opt_arg == NULL)
1308 if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1309 scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1310 scsi->valid |= CTS_SCSI_VALID_TQ;
1317 panic("ahc_send_async: Unexpected async event");
1319 xpt_async(code, path, arg);
1320 xpt_free_path(path);
1324 ahc_platform_set_tags(struct ahc_softc *ahc,
1325 struct ahc_devinfo *devinfo, int enable)
1330 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1332 ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1334 if (ahc->platform_data == NULL)
1340 ahc_platform_free(struct ahc_softc *ahc)
1342 struct ahc_platform_data *pdata;
1344 pdata = ahc->platform_data;
1345 if (pdata != NULL) {
1346 if (pdata->regs != NULL)
1347 bus_release_resource(ahc->dev_softc,
1348 pdata->regs_res_type,
1352 if (pdata->irq != NULL)
1353 bus_release_resource(ahc->dev_softc,
1354 pdata->irq_res_type,
1357 if (pdata->sim_b != NULL) {
1358 xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1359 xpt_free_path(pdata->path_b);
1360 xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1361 cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1363 if (pdata->sim != NULL) {
1364 xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1365 xpt_free_path(pdata->path);
1366 xpt_bus_deregister(cam_sim_path(pdata->sim));
1367 cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1369 if (pdata->eh != NULL)
1370 EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1371 free(ahc->platform_data, M_DEVBUF);
1376 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1378 /* We don't sort softcs under FreeBSD so report equal always */
1383 ahc_detach(device_t dev)
1385 struct ahc_softc *ahc;
1387 device_printf(dev, "detaching device\n");
1388 ahc = device_get_softc(dev);
1390 TAILQ_REMOVE(&ahc_tailq, ahc, links);
1391 ahc_intr_enable(ahc, FALSE);
1392 bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1400 ahc_dump_targcmd(struct target_cmd *cmd)
1406 byte = &cmd->initiator_channel;
1407 /* Debugging info for received commands */
1408 last_byte = &cmd[1].initiator_channel;
1411 while (byte < last_byte) {
1414 printf("%#x", *byte++);
1427 ahc_modevent(module_t mod, int type, void *data)
1429 /* XXX Deal with busy status on unload. */
1430 /* XXX Deal with unknown events */
1434 static moduledata_t ahc_mod = {
1440 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1441 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1442 MODULE_VERSION(ahc, 1);