Home | History | Annotate | Line # | Download | only in ic
aic7xxx_osm.c revision 1.8
      1 /*	$NetBSD: aic7xxx_osm.c,v 1.8 2003/05/01 23:00:20 fvdl Exp $	*/
      2 
      3 /*
      4  * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers
      5  *
      6  * Copyright (c) 1994-2001 Justin T. Gibbs.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions, and the following disclaimer,
     14  *    without modification.
     15  * 2. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU Public License ("GPL").
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  * //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.c#12 $
     34  *
     35  * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx_osm.c,v 1.31 2002/11/30 19:08:58 scottl Exp $
     36  */
     37 /*
     38  * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
     39  */
     40 #include <dev/ic/aic7xxx_osm.h>
     41 #include <dev/ic/aic7xxx_inline.h>
     42 
     43 #ifndef AHC_TMODE_ENABLE
     44 #define AHC_TMODE_ENABLE 0
     45 #endif
     46 
     47 
     48 static void	ahc_action(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg);
     49 static void	ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments);
     50 static int	ahc_poll(struct ahc_softc *ahc, int wait);
     51 static void	ahc_setup_data(struct ahc_softc *ahc,
     52 			       struct scsipi_xfer *xs, struct scb *scb);
     53 static void	ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb);
     54 static int	ahc_ioctl(struct scsipi_channel *channel, u_long cmd, caddr_t addr, int flag,
     55 			  struct proc *p);
     56 
     57 
     58 
     59 /*
     60  * Attach all the sub-devices we can find
     61  */
     62 int
     63 ahc_attach(struct ahc_softc *ahc)
     64 {
     65 	u_long 	s;
     66 	int i;
     67 	char ahc_info[256];
     68 
     69 	LIST_INIT(&ahc->pending_scbs);
     70 	for (i = 0; i < AHC_NUM_TARGETS; i++)
     71 		TAILQ_INIT(&ahc->untagged_queues[i]);
     72 
     73         ahc_lock(ahc, &s);
     74 
     75 	ahc->sc_adapter.adapt_dev = &ahc->sc_dev;
     76 	ahc->sc_adapter.adapt_nchannels = (ahc->features & AHC_TWIN) ? 2 : 1;
     77 
     78 	ahc->sc_adapter.adapt_openings = AHC_MAX_QUEUE;
     79 	ahc->sc_adapter.adapt_max_periph = 16;
     80 
     81 	ahc->sc_adapter.adapt_ioctl = ahc_ioctl;
     82 	ahc->sc_adapter.adapt_minphys = ahc_minphys;
     83 	ahc->sc_adapter.adapt_request = ahc_action;
     84 
     85 	ahc->sc_channel.chan_adapter = &ahc->sc_adapter;
     86         ahc->sc_channel.chan_bustype = &scsi_bustype;
     87         ahc->sc_channel.chan_channel = 0;
     88         ahc->sc_channel.chan_ntargets = (ahc->features & AHC_WIDE) ? 16 : 8;
     89         ahc->sc_channel.chan_nluns = 8 /*AHC_NUM_LUNS*/;
     90         ahc->sc_channel.chan_id = ahc->our_id;
     91 
     92 	if (ahc->features & AHC_TWIN) {
     93 		ahc->sc_channel_b = ahc->sc_channel;
     94 		ahc->sc_channel_b.chan_id = ahc->our_id_b;
     95 		ahc->sc_channel_b.chan_channel = 1;
     96 	}
     97 
     98 	ahc_controller_info(ahc, ahc_info);
     99 	printf("%s: %s\n", ahc->sc_dev.dv_xname, ahc_info);
    100 
    101 	if ((ahc->flags & AHC_PRIMARY_CHANNEL) == 0) {
    102 		ahc->sc_child = config_found((void *)&ahc->sc_dev,
    103 		    &ahc->sc_channel, scsiprint);
    104 		if (ahc->features & AHC_TWIN)
    105 			ahc->sc_child_b = config_found((void *)&ahc->sc_dev,
    106 			    &ahc->sc_channel_b, scsiprint);
    107 	} else {
    108 		if (ahc->features & AHC_TWIN)
    109 			ahc->sc_child = config_found((void *)&ahc->sc_dev,
    110 			    &ahc->sc_channel_b, scsiprint);
    111 		ahc->sc_child_b = config_found((void *)&ahc->sc_dev,
    112 		    &ahc->sc_channel, scsiprint);
    113 	}
    114 
    115 	ahc_intr_enable(ahc, TRUE);
    116 
    117 	if (ahc->flags & AHC_RESET_BUS_A)
    118 		ahc_reset_channel(ahc, 'A', TRUE);
    119 	if ((ahc->features & AHC_TWIN) && ahc->flags & AHC_RESET_BUS_B)
    120 		ahc_reset_channel(ahc, 'B', TRUE);
    121 
    122 	ahc_unlock(ahc, &s);
    123 	return (1);
    124 }
    125 
    126 /*
    127  * Catch an interrupt from the adapter
    128  */
    129 void
    130 ahc_platform_intr(void *arg)
    131 {
    132 	struct	ahc_softc *ahc;
    133 
    134 	ahc = (struct ahc_softc *)arg;
    135 	ahc_intr(ahc);
    136 }
    137 
    138 /*
    139  * We have an scb which has been processed by the
    140  * adaptor, now we look to see how the operation
    141  * went.
    142  */
    143 void
    144 ahc_done(struct ahc_softc *ahc, struct scb *scb)
    145 {
    146 	struct scsipi_xfer *xs;
    147   	struct scsipi_periph *periph;
    148 	u_long s;
    149 
    150 	xs = scb->xs;
    151 	periph = xs->xs_periph;
    152 	LIST_REMOVE(scb, pending_links);
    153 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
    154 		struct scb_tailq *untagged_q;
    155 		int target_offset;
    156 
    157 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
    158 		untagged_q = &ahc->untagged_queues[target_offset];
    159 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
    160 		scb->flags &= ~SCB_UNTAGGEDQ;
    161 		ahc_run_untagged_queue(ahc, untagged_q);
    162 	}
    163 
    164 	callout_stop(&scb->xs->xs_callout);
    165 
    166 	if (xs->datalen) {
    167 		int op;
    168 
    169 		if (xs->xs_control & XS_CTL_DATA_IN)
    170 			op = BUS_DMASYNC_POSTREAD;
    171 		else
    172 			op = BUS_DMASYNC_POSTWRITE;
    173 		bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
    174 				scb->dmamap->dm_mapsize, op);
    175 		bus_dmamap_unload(ahc->parent_dmat, scb->dmamap);
    176 	}
    177 
    178 	/*
    179 	 * If the recovery SCB completes, we have to be
    180 	 * out of our timeout.
    181 	 */
    182 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
    183 		struct	scb *list_scb;
    184 
    185 		/*
    186 		 * We were able to complete the command successfully,
    187 		 * so reinstate the timeouts for all other pending
    188 		 * commands.
    189 		 */
    190 		LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
    191 			struct scsipi_xfer *xs = list_scb->xs;
    192 
    193 			if (!(xs->xs_control & XS_CTL_POLL)) {
    194 				callout_reset(&list_scb->xs->xs_callout,
    195 				    (list_scb->xs->timeout > 1000000) ?
    196 				    (list_scb->xs->timeout / 1000) * hz :
    197 				    (list_scb->xs->timeout * hz) / 1000,
    198 				    ahc_timeout, list_scb);
    199 			}
    200 		}
    201 
    202 		if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
    203 		 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
    204 			ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
    205 		scsipi_printaddr(xs->xs_periph);
    206 		printf("%s: no longer in timeout, status = %x\n",
    207 		       ahc_name(ahc), xs->status);
    208 	}
    209 
    210 	/* Don't clobber any existing error state */
    211 	if (xs->error != XS_NOERROR) {
    212 	  /* Don't clobber any existing error state */
    213 	} else if ((scb->flags & SCB_SENSE) != 0) {
    214 		/*
    215 		 * We performed autosense retrieval.
    216 		 *
    217 		 * Zero any sense not transferred by the
    218 		 * device.  The SCSI spec mandates that any
    219 		 * untransfered data should be assumed to be
    220 		 * zero.  Complete the 'bounce' of sense information
    221 		 * through buffers accessible via bus-space by
    222 		 * copying it into the clients csio.
    223 		 */
    224 		memset(&xs->sense.scsi_sense, 0, sizeof(xs->sense.scsi_sense));
    225 		memcpy(&xs->sense.scsi_sense,
    226 		       ahc_get_sense_buf(ahc, scb),
    227 		       sizeof(xs->sense.scsi_sense));
    228 		xs->error = XS_SENSE;
    229 	}
    230 	if (scb->flags & SCB_FREEZE_QUEUE) {
    231 		scsipi_periph_thaw(periph, 1);
    232 		scb->flags &= ~SCB_FREEZE_QUEUE;
    233 	}
    234 
    235         ahc_lock(ahc, &s);
    236 	ahc_free_scb(ahc, scb);
    237         ahc_unlock(ahc, &s);
    238 
    239 	scsipi_done(xs);
    240 }
    241 
    242 static int
    243 ahc_ioctl(struct scsipi_channel *channel, u_long cmd, caddr_t addr, int flag,
    244 	  struct proc *p)
    245 {
    246 	struct ahc_softc *ahc = (void *)channel->chan_adapter->adapt_dev;
    247 	int s, ret = ENOTTY;
    248 
    249 	switch (cmd) {
    250 	case SCBUSIORESET:
    251 		s = splbio();
    252 		ahc_reset_channel(ahc, channel->chan_channel == 1 ? 'B' : 'A',
    253 		    TRUE);
    254 		splx(s);
    255 		ret = 0;
    256 		break;
    257 	default:
    258 		break;
    259 	}
    260 
    261 	return ret;
    262 }
    263 
    264 static void
    265 ahc_action(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
    266 {
    267 	struct ahc_softc *ahc;
    268 	int s;
    269 	struct ahc_initiator_tinfo *tinfo;
    270 	struct ahc_tmode_tstate *tstate;
    271 
    272 	ahc  = (void *)chan->chan_adapter->adapt_dev;
    273 
    274 	switch (req) {
    275 
    276 	case ADAPTER_REQ_RUN_XFER:
    277 	  {
    278 		struct scsipi_xfer *xs;
    279 		struct scsipi_periph *periph;
    280 	        struct scb *scb;
    281         	struct hardware_scb *hscb;
    282 		u_int target_id;
    283 		u_int our_id;
    284 		u_long s;
    285 
    286 		xs = arg;
    287 		periph = xs->xs_periph;
    288 
    289 		target_id = periph->periph_target;
    290                 our_id = ahc->our_id;
    291 
    292 		SC_DEBUG(xs->xs_periph, SCSIPI_DB3, ("ahc_action\n"));
    293 
    294 		/*
    295 		 * get an scb to use.
    296 		 */
    297 		ahc_lock(ahc, &s);
    298 		if ((scb = ahc_get_scb(ahc)) == NULL) {
    299 			xs->error = XS_RESOURCE_SHORTAGE;
    300 			ahc_unlock(ahc, &s);
    301 			scsipi_done(xs);
    302 			return;
    303 		}
    304 		ahc_unlock(ahc, &s);
    305 
    306 		hscb = scb->hscb;
    307 
    308 		SC_DEBUG(periph, SCSIPI_DB3, ("start scb(%p)\n", scb));
    309 		scb->xs = xs;
    310 
    311 		/*
    312 		 * Put all the arguments for the xfer in the scb
    313 		 */
    314 		hscb->control = 0;
    315 		hscb->scsiid = BUILD_SCSIID(ahc, 0, target_id, our_id);
    316 		hscb->lun = periph->periph_lun;
    317 		if (xs->xs_control & XS_CTL_RESET) {
    318 			hscb->cdb_len = 0;
    319 			scb->flags |= SCB_DEVICE_RESET;
    320 			hscb->control |= MK_MESSAGE;
    321 			ahc_execute_scb(scb, NULL, 0);
    322 		}
    323 
    324 		ahc_setup_data(ahc, xs, scb);
    325 
    326 		break;
    327 	  }
    328 	case ADAPTER_REQ_GROW_RESOURCES:
    329   		printf("%s: ADAPTER_REQ_GROW_RESOURCES\n", ahc_name(ahc));
    330 		return;
    331 
    332 	case ADAPTER_REQ_SET_XFER_MODE:
    333 	    {
    334 		struct scsipi_xfer_mode *xm = arg;
    335 		struct ahc_devinfo devinfo;
    336 		int target_id, our_id, first;
    337 		u_int width;
    338 		char channel;
    339 
    340 		target_id = xm->xm_target;
    341 		our_id = chan->chan_id;
    342 		channel = (chan->chan_channel == 1) ? 'B' : 'A';
    343 		s = splbio();
    344 		tinfo = ahc_fetch_transinfo(ahc, channel, our_id, target_id,
    345 		    &tstate);
    346 		ahc_compile_devinfo(&devinfo, our_id, target_id,
    347 		    0, channel, ROLE_INITIATOR);
    348 
    349 		/*
    350 		 * XXX since the period and offset are not provided here,
    351 		 * fake things by forcing a renegotiation using the user
    352 		 * settings if this is called for the first time (i.e.
    353 		 * during probe). Also, cap various values at the user
    354 		 * values, assuming that the user set it up that way.
    355 		 */
    356 		if (ahc->inited_target[target_id] == 0) {
    357 			tinfo->goal = tinfo->user;
    358 			tstate->tagenable |=
    359 			    (ahc->user_tagenable & devinfo.target_mask);
    360 			tstate->discenable |=
    361 			    (ahc->user_discenable & devinfo.target_mask);
    362 			ahc->inited_target[target_id] = 1;
    363 			first = 1;
    364 		} else
    365 			first = 0;
    366 
    367 		if (xm->xm_mode & (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
    368 			width = MSG_EXT_WDTR_BUS_16_BIT;
    369 		else
    370 			width = MSG_EXT_WDTR_BUS_8_BIT;
    371 
    372 		ahc_validate_width(ahc, NULL, &width, ROLE_UNKNOWN);
    373 		if (width > tinfo->user.width)
    374 			width = tinfo->user.width;
    375 		tinfo->goal.width = width;
    376 
    377 		if (!(xm->xm_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT))) {
    378 			tinfo->goal.period = 0;
    379 			tinfo->goal.offset = 0;
    380 			tinfo->goal.ppr_options = 0;
    381 		}
    382 
    383 		if ((xm->xm_mode & PERIPH_CAP_DT) &&
    384 		    (tinfo->user.ppr_options & MSG_EXT_PPR_DT_REQ))
    385 			tinfo->goal.ppr_options |= MSG_EXT_PPR_DT_REQ;
    386 		else
    387 			tinfo->goal.ppr_options &= ~MSG_EXT_PPR_DT_REQ;
    388 
    389 		if ((xm->xm_mode & PERIPH_CAP_TQING) &&
    390 		    (ahc->user_tagenable & devinfo.target_mask))
    391 			tstate->tagenable |= devinfo.target_mask;
    392 		else
    393 			tstate->tagenable &= ~devinfo.target_mask;
    394 
    395 		/*
    396 		 * If this is the first request, and no negotiation is
    397 		 * needed, just confirm the state to the scsipi layer,
    398 		 * so that it can print a message.
    399 		 */
    400 		if (!ahc_update_neg_request(ahc, &devinfo, tstate,
    401 		    tinfo, AHC_NEG_IF_NON_ASYNC) && first)
    402 			scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    403 		splx(s);
    404 	    }
    405 	}
    406 
    407 	return;
    408 }
    409 
    410 static void
    411 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments)
    412 {
    413 	struct	scb *scb;
    414 	struct scsipi_xfer *xs;
    415 	struct	ahc_softc *ahc;
    416 	struct	ahc_initiator_tinfo *tinfo;
    417 	struct	ahc_tmode_tstate *tstate;
    418 
    419 	u_int	mask;
    420 	long	s;
    421 
    422 	scb = (struct scb *)arg;
    423 	xs = scb->xs;
    424 	xs->error = 0;
    425 	xs->status = 0;
    426 	xs->xs_status = 0;
    427 	ahc = (void *)xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
    428 
    429 	if (nsegments != 0) {
    430 		struct	  ahc_dma_seg *sg;
    431 		bus_dma_segment_t *end_seg;
    432 		int op;
    433 
    434 		end_seg = dm_segs + nsegments;
    435 
    436 		/* Copy the segments into our SG list */
    437 		sg = scb->sg_list;
    438 		while (dm_segs < end_seg) {
    439 			uint32_t len;
    440 
    441 			sg->addr = ahc_htole32(dm_segs->ds_addr);
    442 			len = dm_segs->ds_len
    443 			    | ((dm_segs->ds_addr >> 8) & 0x7F000000);
    444 			sg->len = ahc_htole32(len);
    445 			sg++;
    446 			dm_segs++;
    447 		}
    448 
    449 		/*
    450 		 * Note where to find the SG entries in bus space.
    451 		 * We also set the full residual flag which the
    452 		 * sequencer will clear as soon as a data transfer
    453 		 * occurs.
    454 		 */
    455 		scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
    456 
    457 		if (xs->xs_control & XS_CTL_DATA_IN)
    458 			op = BUS_DMASYNC_PREREAD;
    459 		else
    460 			op = BUS_DMASYNC_PREWRITE;
    461 
    462 		bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
    463 				scb->dmamap->dm_mapsize, op);
    464 
    465 		sg--;
    466 		sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
    467 
    468 		/* Copy the first SG into the "current" data pointer area */
    469 		scb->hscb->dataptr = scb->sg_list->addr;
    470 		scb->hscb->datacnt = scb->sg_list->len;
    471 	} else {
    472 		scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
    473 		scb->hscb->dataptr = 0;
    474 		scb->hscb->datacnt = 0;
    475 	}
    476 
    477 	scb->sg_count = nsegments;
    478 
    479 	ahc_lock(ahc, &s);
    480 
    481 	/*
    482 	 * Last time we need to check if this SCB needs to
    483 	 * be aborted.
    484 	 */
    485 	if (xs->xs_status & XS_STS_DONE) {
    486 		if (nsegments != 0)
    487 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
    488 		ahc_free_scb(ahc, scb);
    489 		ahc_unlock(ahc, &s);
    490 		scsipi_done(xs);
    491 		return;
    492 	}
    493 
    494 	tinfo = ahc_fetch_transinfo(ahc, ahc->channel,
    495 				    SCSIID_OUR_ID(scb->hscb->scsiid),
    496 				    SCSIID_TARGET(ahc, scb->hscb->scsiid),
    497 				    &tstate);
    498 
    499 	mask = SCB_GET_TARGET_MASK(ahc, scb);
    500 	scb->hscb->scsirate = tinfo->scsirate;
    501 	scb->hscb->scsioffset = tinfo->curr.offset;
    502 
    503 	if ((tstate->ultraenb & mask) != 0)
    504 		scb->hscb->control |= ULTRAENB;
    505 
    506 	if ((tstate->discenable & mask) != 0)
    507 	    	scb->hscb->control |= DISCENB;
    508 
    509 	if (xs->xs_tag_type)
    510 		scb->hscb->control |= xs->xs_tag_type;
    511 
    512 	if ((xs->xs_control & XS_CTL_DISCOVERY) && (tinfo->goal.width == 0
    513 	     && tinfo->goal.offset == 0
    514 	     && tinfo->goal.ppr_options == 0)) {
    515 		scb->flags |= SCB_NEGOTIATE;
    516 		scb->hscb->control |= MK_MESSAGE;
    517 	} else if ((tstate->auto_negotiate & mask) != 0) {
    518 		scb->flags |= SCB_AUTO_NEGOTIATE;
    519 		scb->hscb->control |= MK_MESSAGE;
    520 	}
    521 
    522 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
    523 
    524 	if (!(xs->xs_control & XS_CTL_POLL)) {
    525 		callout_reset(&scb->xs->xs_callout, xs->timeout > 1000000 ?
    526 			      (xs->timeout / 1000) * hz : (xs->timeout * hz) / 1000,
    527 			      ahc_timeout, scb);
    528 	}
    529 
    530 	/*
    531 	 * We only allow one untagged transaction
    532 	 * per target in the initiator role unless
    533 	 * we are storing a full busy target *lun*
    534 	 * table in SCB space.
    535 	 */
    536 	if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
    537 	    && (ahc->flags & AHC_SCB_BTT) == 0) {
    538 		struct scb_tailq *untagged_q;
    539 		int target_offset;
    540 
    541 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
    542 		untagged_q = &(ahc->untagged_queues[target_offset]);
    543 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
    544 		scb->flags |= SCB_UNTAGGEDQ;
    545 		if (TAILQ_FIRST(untagged_q) != scb) {
    546 			ahc_unlock(ahc, &s);
    547 			return;
    548 		}
    549 	}
    550 	scb->flags |= SCB_ACTIVE;
    551 
    552 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
    553 		/* Define a mapping from our tag to the SCB. */
    554 		ahc->scb_data->scbindex[scb->hscb->tag] = scb;
    555 		ahc_pause(ahc);
    556 		if ((ahc->flags & AHC_PAGESCBS) == 0)
    557 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
    558 		ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
    559 		ahc_unpause(ahc);
    560 	} else {
    561 		ahc_queue_scb(ahc, scb);
    562 	}
    563 
    564 	if (!(xs->xs_control & XS_CTL_POLL)) {
    565 		ahc_unlock(ahc, &s);
    566 		return;
    567 	}
    568 
    569 	/*
    570 	 * If we can't use interrupts, poll for completion
    571 	 */
    572 	SC_DEBUG(xs->xs_periph, SCSIPI_DB3, ("cmd_poll\n"));
    573 	do {
    574 		if (ahc_poll(ahc, xs->timeout)) {
    575 			if (!(xs->xs_control & XS_CTL_SILENT))
    576 				printf("cmd fail\n");
    577 			ahc_timeout(scb);
    578 			break;
    579 		}
    580 	} while (!(xs->xs_status & XS_STS_DONE));
    581 	ahc_unlock(ahc, &s);
    582 
    583 	return;
    584 }
    585 
    586 static int
    587 ahc_poll(struct ahc_softc *ahc, int wait)
    588 {
    589 	while (--wait) {
    590 		DELAY(1000);
    591 		if (ahc_inb(ahc, INTSTAT) & INT_PEND)
    592 			break;
    593 	}
    594 
    595 	if (wait == 0) {
    596 		printf("%s: board is not responding\n", ahc_name(ahc));
    597 		return (EIO);
    598 	}
    599 
    600 	ahc_intr((void *)ahc);
    601 	return (0);
    602 }
    603 
    604 static void
    605 ahc_setup_data(struct ahc_softc *ahc, struct scsipi_xfer *xs,
    606 	       struct scb *scb)
    607 {
    608 	struct hardware_scb *hscb;
    609 
    610 	hscb = scb->hscb;
    611 	xs->resid = xs->status = 0;
    612 
    613 	hscb->cdb_len = xs->cmdlen;
    614 	if (hscb->cdb_len > sizeof(hscb->cdb32)) {
    615 		u_long s;
    616 
    617 		ahc_set_transaction_status(scb, CAM_REQ_INVALID);
    618 		ahc_lock(ahc, &s);
    619 		ahc_free_scb(ahc, scb);
    620 		ahc_unlock(ahc, &s);
    621 		scsipi_done(xs);
    622 		return;
    623 	}
    624 
    625 	if (hscb->cdb_len > 12) {
    626 		memcpy(hscb->cdb32, xs->cmd, hscb->cdb_len);
    627 		scb->flags |= SCB_CDB32_PTR;
    628 	} else {
    629 		memcpy(hscb->shared_data.cdb, xs->cmd, hscb->cdb_len);
    630 	}
    631 
    632 	/* Only use S/G if there is a transfer */
    633 	if (xs->datalen) {
    634 		int error;
    635 
    636                 error = bus_dmamap_load(ahc->parent_dmat,
    637 					scb->dmamap, xs->data,
    638 					xs->datalen, NULL,
    639 					((xs->xs_control & XS_CTL_NOSLEEP) ?
    640 					 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
    641 					BUS_DMA_STREAMING |
    642 					((xs->xs_control & XS_CTL_DATA_IN) ?
    643 					 BUS_DMA_READ : BUS_DMA_WRITE));
    644                 if (error) {
    645 #ifdef AHC_DEBUG
    646                         printf("%s: in ahc_setup_data(): bus_dmamap_load() "
    647 			       "= %d\n",
    648 			       ahc_name(ahc), error);
    649 #endif
    650                         xs->error = XS_RESOURCE_SHORTAGE;
    651                         scsipi_done(xs);
    652                         return;
    653                 }
    654                 ahc_execute_scb(scb,
    655 				scb->dmamap->dm_segs,
    656 				scb->dmamap->dm_nsegs);
    657 	} else {
    658 		ahc_execute_scb(scb, NULL, 0);
    659 	}
    660 }
    661 
    662 static void
    663 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
    664 
    665 	if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
    666 		struct scb *list_scb;
    667 
    668 		scb->flags |= SCB_RECOVERY_SCB;
    669 
    670 		/*
    671 		 * Take all queued, but not sent SCBs out of the equation.
    672 		 * Also ensure that no new CCBs are queued to us while we
    673 		 * try to fix this problem.
    674 		 */
    675 		scsipi_channel_freeze(&ahc->sc_channel, 1);
    676 		if (ahc->features & AHC_TWIN)
    677 			scsipi_channel_freeze(&ahc->sc_channel_b, 1);
    678 
    679 		/*
    680 		 * Go through all of our pending SCBs and remove
    681 		 * any scheduled timeouts for them.  We will reschedule
    682 		 * them after we've successfully fixed this problem.
    683 		 */
    684 		LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
    685 			callout_stop(&list_scb->xs->xs_callout);
    686 		}
    687 	}
    688 }
    689 
    690 void
    691 ahc_timeout(void *arg)
    692 {
    693 	struct	scb *scb;
    694 	struct	ahc_softc *ahc;
    695 	long	s;
    696 	int	found;
    697 	u_int	last_phase;
    698 	int	target;
    699 	int	lun;
    700 	int	i;
    701 	char	channel;
    702 
    703 	scb = (struct scb *)arg;
    704 	ahc = (struct ahc_softc *)scb->ahc_softc;
    705 
    706 	ahc_lock(ahc, &s);
    707 
    708 	ahc_pause_and_flushwork(ahc);
    709 
    710 	if ((scb->flags & SCB_ACTIVE) == 0) {
    711 		/* Previous timeout took care of me already */
    712 		printf("%s: Timedout SCB already complete. "
    713 		       "Interrupts may not be functioning.\n", ahc_name(ahc));
    714 		ahc_unpause(ahc);
    715 		ahc_unlock(ahc, &s);
    716 		return;
    717 	}
    718 
    719 	target = SCB_GET_TARGET(ahc, scb);
    720 	channel = SCB_GET_CHANNEL(ahc, scb);
    721 	lun = SCB_GET_LUN(scb);
    722 
    723 	ahc_print_path(ahc, scb);
    724 	printf("SCB 0x%x - timed out\n", scb->hscb->tag);
    725 	ahc_dump_card_state(ahc);
    726 	last_phase = ahc_inb(ahc, LASTPHASE);
    727 	if (scb->sg_count > 0) {
    728 		for (i = 0; i < scb->sg_count; i++) {
    729 			printf("sg[%d] - Addr 0x%x : Length %d\n",
    730 			       i,
    731 			       scb->sg_list[i].addr,
    732 			       scb->sg_list[i].len & AHC_SG_LEN_MASK);
    733 		}
    734 	}
    735 	if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
    736 		/*
    737 		 * Been down this road before.
    738 		 * Do a full bus reset.
    739 		 */
    740 bus_reset:
    741 		ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
    742 		found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
    743 		printf("%s: Issued Channel %c Bus Reset. "
    744 		       "%d SCBs aborted\n", ahc_name(ahc), channel, found);
    745 	} else {
    746 		/*
    747 		 * If we are a target, transition to bus free and report
    748 		 * the timeout.
    749 		 *
    750 		 * The target/initiator that is holding up the bus may not
    751 		 * be the same as the one that triggered this timeout
    752 		 * (different commands have different timeout lengths).
    753 		 * If the bus is idle and we are actiing as the initiator
    754 		 * for this request, queue a BDR message to the timed out
    755 		 * target.  Otherwise, if the timed out transaction is
    756 		 * active:
    757 		 *   Initiator transaction:
    758 		 *	Stuff the message buffer with a BDR message and assert
    759 		 *	ATN in the hopes that the target will let go of the bus
    760 		 *	and go to the mesgout phase.  If this fails, we'll
    761 		 *	get another timeout 2 seconds later which will attempt
    762 		 *	a bus reset.
    763 		 *
    764 		 *   Target transaction:
    765 		 *	Transition to BUS FREE and report the error.
    766 		 *	It's good to be the target!
    767 		 */
    768 		u_int active_scb_index;
    769 		u_int saved_scbptr;
    770 
    771 		saved_scbptr = ahc_inb(ahc, SCBPTR);
    772 		active_scb_index = ahc_inb(ahc, SCB_TAG);
    773 
    774 		if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
    775 		  && (active_scb_index < ahc->scb_data->numscbs)) {
    776 			struct scb *active_scb;
    777 
    778 			/*
    779 			 * If the active SCB is not us, assume that
    780 			 * the active SCB has a longer timeout than
    781 			 * the timedout SCB, and wait for the active
    782 			 * SCB to timeout.
    783 			 */
    784 			active_scb = ahc_lookup_scb(ahc, active_scb_index);
    785 			if (active_scb != scb) {
    786 				uint64_t newtimeout;
    787 
    788 				ahc_print_path(ahc, scb);
    789 				printf("Other SCB Timeout%s",
    790 			 	       (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
    791 				       ? " again\n" : "\n");
    792 				scb->flags |= SCB_OTHERTCL_TIMEOUT;
    793 				newtimeout = MAX(active_scb->xs->timeout,
    794 						 scb->xs->timeout);
    795 				callout_reset(&scb->xs->xs_callout,
    796 				    newtimeout > 1000000 ?
    797 				    (newtimeout / 1000) * hz :
    798 				    (newtimeout * hz) / 1000,
    799 				    ahc_timeout, scb);
    800 				ahc_unpause(ahc);
    801 				ahc_unlock(ahc, &s);
    802 				return;
    803 			}
    804 
    805 			/* It's us */
    806 			if ((scb->flags & SCB_TARGET_SCB) != 0) {
    807 
    808 				/*
    809 				 * Send back any queued up transactions
    810 				 * and properly record the error condition.
    811 				 */
    812 				ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
    813 					       SCB_GET_CHANNEL(ahc, scb),
    814 					       SCB_GET_LUN(scb),
    815 					       scb->hscb->tag,
    816 					       ROLE_TARGET,
    817 					       CAM_CMD_TIMEOUT);
    818 
    819 				/* Will clear us from the bus */
    820 				ahc_restart(ahc);
    821 				ahc_unlock(ahc, &s);
    822 				return;
    823 			}
    824 
    825 			ahc_set_recoveryscb(ahc, active_scb);
    826 			ahc_outb(ahc, MSG_OUT, HOST_MSG);
    827 			ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
    828 			ahc_print_path(ahc, active_scb);
    829 			printf("BDR message in message buffer\n");
    830 			active_scb->flags |= SCB_DEVICE_RESET;
    831 			callout_reset(&active_scb->xs->xs_callout,
    832 				      2 * hz, ahc_timeout, active_scb);
    833 			ahc_unpause(ahc);
    834 		} else {
    835 			int	 disconnected;
    836 
    837 			/* XXX Shouldn't panic.  Just punt instead? */
    838 			if ((scb->flags & SCB_TARGET_SCB) != 0)
    839 				panic("Timed-out target SCB but bus idle");
    840 
    841 			if (last_phase != P_BUSFREE
    842 			 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
    843 				/* XXX What happened to the SCB? */
    844 				/* Hung target selection.  Goto busfree */
    845 				printf("%s: Hung target selection\n",
    846 				       ahc_name(ahc));
    847 				ahc_restart(ahc);
    848 				ahc_unlock(ahc, &s);
    849 				return;
    850 			}
    851 
    852 			if (ahc_search_qinfifo(ahc, target, channel, lun,
    853 					       scb->hscb->tag, ROLE_INITIATOR,
    854 					       /*status*/0, SEARCH_COUNT) > 0) {
    855 				disconnected = FALSE;
    856 			} else {
    857 				disconnected = TRUE;
    858 			}
    859 
    860 			if (disconnected) {
    861 
    862 				ahc_set_recoveryscb(ahc, scb);
    863 				/*
    864 				 * Actually re-queue this SCB in an attempt
    865 				 * to select the device before it reconnects.
    866 				 * In either case (selection or reselection),
    867 				 * we will now issue a target reset to the
    868 				 * timed-out device.
    869 				 *
    870 				 * Set the MK_MESSAGE control bit indicating
    871 				 * that we desire to send a message.  We
    872 				 * also set the disconnected flag since
    873 				 * in the paging case there is no guarantee
    874 				 * that our SCB control byte matches the
    875 				 * version on the card.  We don't want the
    876 				 * sequencer to abort the command thinking
    877 				 * an unsolicited reselection occurred.
    878 				 */
    879 				scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
    880 				scb->flags |= SCB_DEVICE_RESET;
    881 
    882 				/*
    883 				 * Remove any cached copy of this SCB in the
    884 				 * disconnected list in preparation for the
    885 				 * queuing of our abort SCB.  We use the
    886 				 * same element in the SCB, SCB_NEXT, for
    887 				 * both the qinfifo and the disconnected list.
    888 				 */
    889 				ahc_search_disc_list(ahc, target, channel,
    890 						     lun, scb->hscb->tag,
    891 						     /*stop_on_first*/TRUE,
    892 						     /*remove*/TRUE,
    893 						     /*save_state*/FALSE);
    894 
    895 				/*
    896 				 * In the non-paging case, the sequencer will
    897 				 * never re-reference the in-core SCB.
    898 				 * To make sure we are notified during
    899 				 * reslection, set the MK_MESSAGE flag in
    900 				 * the card's copy of the SCB.
    901 				 */
    902 				if ((ahc->flags & AHC_PAGESCBS) == 0) {
    903 					ahc_outb(ahc, SCBPTR, scb->hscb->tag);
    904 					ahc_outb(ahc, SCB_CONTROL,
    905 						 ahc_inb(ahc, SCB_CONTROL)
    906 						| MK_MESSAGE);
    907 				}
    908 
    909 				/*
    910 				 * Clear out any entries in the QINFIFO first
    911 				 * so we are the next SCB for this target
    912 				 * to run.
    913 				 */
    914 				ahc_search_qinfifo(ahc,
    915 						   SCB_GET_TARGET(ahc, scb),
    916 						   channel, SCB_GET_LUN(scb),
    917 						   SCB_LIST_NULL,
    918 						   ROLE_INITIATOR,
    919 						   CAM_REQUEUE_REQ,
    920 						   SEARCH_COMPLETE);
    921 				ahc_print_path(ahc, scb);
    922 				printf("Queuing a BDR SCB\n");
    923 				ahc_qinfifo_requeue_tail(ahc, scb);
    924 				ahc_outb(ahc, SCBPTR, saved_scbptr);
    925 				callout_reset(&scb->xs->xs_callout, 2 * hz,
    926 					      ahc_timeout, scb);
    927 				ahc_unpause(ahc);
    928 			} else {
    929 				/* Go "immediatly" to the bus reset */
    930 				/* This shouldn't happen */
    931 				ahc_set_recoveryscb(ahc, scb);
    932 				ahc_print_path(ahc, scb);
    933 				printf("SCB %d: Immediate reset.  "
    934 					"Flags = 0x%x\n", scb->hscb->tag,
    935 					scb->flags);
    936 				goto bus_reset;
    937 			}
    938 		}
    939 	}
    940 	ahc_unlock(ahc, &s);
    941 }
    942 
    943 void
    944 ahc_platform_set_tags(struct ahc_softc *ahc,
    945 		      struct ahc_devinfo *devinfo, int enable)
    946 {
    947 	struct ahc_initiator_tinfo *tinfo;
    948         struct ahc_tmode_tstate *tstate;
    949 
    950         tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
    951                                     devinfo->target, &tstate);
    952 
    953         if (enable)
    954                 tstate->tagenable |= devinfo->target_mask;
    955 	else
    956 	  	tstate->tagenable &= ~devinfo->target_mask;
    957 }
    958 
    959 int
    960 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
    961 {
    962 	if (sizeof(struct ahc_platform_data) == 0)
    963 		return 0;
    964 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
    965 				    M_NOWAIT);
    966 	if (ahc->platform_data == NULL)
    967 		return (ENOMEM);
    968 	return (0);
    969 }
    970 
    971 void
    972 ahc_platform_free(struct ahc_softc *ahc)
    973 {
    974 	if (sizeof(struct ahc_platform_data) == 0)
    975 		return;
    976 	free(ahc->platform_data, M_DEVBUF);
    977 }
    978 
    979 int
    980 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
    981 {
    982 	return (0);
    983 }
    984 
    985 int
    986 ahc_detach(struct device *self, int flags)
    987 {
    988 	int rv = 0;
    989 
    990 	struct ahc_softc *ahc = (struct ahc_softc*)self;
    991 
    992 	ahc_intr_enable(ahc, FALSE);
    993 	if (ahc->sc_child != NULL)
    994 		rv = config_detach(ahc->sc_child, flags);
    995 	if (rv == 0 && ahc->sc_child_b != NULL)
    996 		rv = config_detach(ahc->sc_child_b, flags);
    997 
    998 	shutdownhook_disestablish(ahc->shutdown_hook);
    999 
   1000 	ahc_free(ahc);
   1001 
   1002 	return (rv);
   1003 }
   1004 
   1005 
   1006 void
   1007 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target, u_int lun,
   1008 	       ac_code code, void *opt_arg)
   1009 {
   1010 	struct ahc_tmode_tstate *tstate;
   1011 	struct ahc_initiator_tinfo *tinfo;
   1012 	struct ahc_devinfo devinfo;
   1013 	struct scsipi_channel *chan;
   1014 	struct scsipi_xfer_mode xm;
   1015 
   1016 	chan = channel == 'B' ? &ahc->sc_channel_b : &ahc->sc_channel;
   1017 	switch (code) {
   1018 	case AC_TRANSFER_NEG:
   1019 		tinfo = ahc_fetch_transinfo(ahc, channel, ahc->our_id, target,
   1020 			    &tstate);
   1021 		ahc_compile_devinfo(&devinfo, ahc->our_id, target, lun,
   1022 		    channel, ROLE_UNKNOWN);
   1023 		/*
   1024 		 * Don't bother if negotiating. XXX?
   1025 		 */
   1026 		if (tinfo->curr.period != tinfo->goal.period
   1027 		    || tinfo->curr.width != tinfo->goal.width
   1028 		    || tinfo->curr.offset != tinfo->goal.offset
   1029 		    || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
   1030 			break;
   1031 		xm.xm_target = target;
   1032 		xm.xm_mode = 0;
   1033 		xm.xm_period = tinfo->curr.period;
   1034 		xm.xm_offset = tinfo->curr.offset;
   1035 		if (tinfo->curr.width == MSG_EXT_WDTR_BUS_16_BIT)
   1036 			xm.xm_mode |= PERIPH_CAP_WIDE16;
   1037 		if (tinfo->curr.period)
   1038 			xm.xm_mode |= PERIPH_CAP_SYNC;
   1039 		if (tstate->tagenable & devinfo.target_mask)
   1040 			xm.xm_mode |= PERIPH_CAP_TQING;
   1041 		if (tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ)
   1042 			xm.xm_mode |= PERIPH_CAP_DT;
   1043 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, &xm);
   1044 		break;
   1045 	case AC_BUS_RESET:
   1046 		scsipi_async_event(chan, ASYNC_EVENT_RESET, NULL);
   1047 	case AC_SENT_BDR:
   1048 	default:
   1049 		break;
   1050 	}
   1051 }
   1052