xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /*
2  * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2001 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU Public License ("GPL").
18  *
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
29  * SUCH DAMAGE.
30  *
31  * $Id$
32  *
33  * $FreeBSD$
34  */
35 
36 #include <dev/aic7xxx/aic7xxx_freebsd.h>
37 #include <dev/aic7xxx/aic7xxx_inline.h>
38 
39 #ifndef AHC_TMODE_ENABLE
40 #define AHC_TMODE_ENABLE 0
41 #endif
42 
43 #define ccb_scb_ptr spriv_ptr0
44 
45 #ifdef AHC_DEBUG
46 static int     ahc_debug = AHC_DEBUG;
47 #endif
48 
49 #if UNUSED
50 static void	ahc_dump_targcmd(struct target_cmd *cmd);
51 #endif
52 static int	ahc_modevent(module_t mod, int type, void *data);
53 static void	ahc_action(struct cam_sim *sim, union ccb *ccb);
54 static void	ahc_get_tran_settings(struct ahc_softc *ahc,
55 				      int our_id, char channel,
56 				      struct ccb_trans_settings *cts);
57 static void	ahc_async(void *callback_arg, uint32_t code,
58 			  struct cam_path *path, void *arg);
59 static void	ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
60 				int nsegments, int error);
61 static void	ahc_poll(struct cam_sim *sim);
62 static void	ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
63 			       struct ccb_scsiio *csio, struct scb *scb);
64 static void	ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
65 			      union ccb *ccb);
66 static int	ahc_create_path(struct ahc_softc *ahc,
67 				char channel, u_int target, u_int lun,
68 				struct cam_path **path);
69 
70 static void	ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb);
71 
72 static int
73 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
74 	        u_int lun, struct cam_path **path)
75 {
76 	path_id_t path_id;
77 
78 	if (channel == 'B')
79 		path_id = cam_sim_path(ahc->platform_data->sim_b);
80 	else
81 		path_id = cam_sim_path(ahc->platform_data->sim);
82 
83 	return (xpt_create_path(path, /*periph*/NULL,
84 				path_id, target, lun));
85 }
86 
87 /*
88  * Attach all the sub-devices we can find
89  */
90 int
91 ahc_attach(struct ahc_softc *ahc)
92 {
93 	char   ahc_info[256];
94 	struct ccb_setasync csa;
95 	struct cam_devq *devq;
96 	int bus_id;
97 	int bus_id2;
98 	struct cam_sim *sim;
99 	struct cam_sim *sim2;
100 	struct cam_path *path;
101 	struct cam_path *path2;
102 	long s;
103 	int count;
104 	int error;
105 
106 	count = 0;
107 	sim = NULL;
108 	sim2 = NULL;
109 
110 	ahc_controller_info(ahc, ahc_info);
111 	printf("%s\n", ahc_info);
112 	ahc_lock(ahc, &s);
113 	/* Hook up our interrupt handler */
114 	if ((error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
115 				    INTR_TYPE_CAM|INTR_ENTROPY, ahc_platform_intr, ahc,
116 				    &ahc->platform_data->ih)) != 0) {
117 		device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
118 			      error);
119 		goto fail;
120 	}
121 
122 	/*
123 	 * Attach secondary channel first if the user has
124 	 * declared it the primary channel.
125 	 */
126 	if ((ahc->features & AHC_TWIN) != 0
127 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
128 		bus_id = 1;
129 		bus_id2 = 0;
130 	} else {
131 		bus_id = 0;
132 		bus_id2 = 1;
133 	}
134 
135 	/*
136 	 * Create the device queue for our SIM(s).
137 	 */
138 	devq = cam_simq_alloc(AHC_MAX_QUEUE);
139 	if (devq == NULL)
140 		goto fail;
141 
142 	/*
143 	 * Construct our first channel SIM entry
144 	 */
145 	sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
146 			    device_get_unit(ahc->dev_softc),
147 			    1, AHC_MAX_QUEUE, devq);
148 	if (sim == NULL) {
149 		cam_simq_free(devq);
150 		goto fail;
151 	}
152 
153 	if (xpt_bus_register(sim, bus_id) != CAM_SUCCESS) {
154 		cam_sim_free(sim, /*free_devq*/TRUE);
155 		sim = NULL;
156 		goto fail;
157 	}
158 
159 	if (xpt_create_path(&path, /*periph*/NULL,
160 			    cam_sim_path(sim), CAM_TARGET_WILDCARD,
161 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
162 		xpt_bus_deregister(cam_sim_path(sim));
163 		cam_sim_free(sim, /*free_devq*/TRUE);
164 		sim = NULL;
165 		goto fail;
166 	}
167 
168 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
169 	csa.ccb_h.func_code = XPT_SASYNC_CB;
170 	csa.event_enable = AC_LOST_DEVICE;
171 	csa.callback = ahc_async;
172 	csa.callback_arg = sim;
173 	xpt_action((union ccb *)&csa);
174 	count++;
175 
176 	if (ahc->features & AHC_TWIN) {
177 		sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
178 				    ahc, device_get_unit(ahc->dev_softc), 1,
179 				    AHC_MAX_QUEUE, devq);
180 
181 		if (sim2 == NULL) {
182 			printf("ahc_attach: Unable to attach second "
183 			       "bus due to resource shortage");
184 			goto fail;
185 		}
186 
187 		if (xpt_bus_register(sim2, bus_id2) != CAM_SUCCESS) {
188 			printf("ahc_attach: Unable to attach second "
189 			       "bus due to resource shortage");
190 			/*
191 			 * We do not want to destroy the device queue
192 			 * because the first bus is using it.
193 			 */
194 			cam_sim_free(sim2, /*free_devq*/FALSE);
195 			goto fail;
196 		}
197 
198 		if (xpt_create_path(&path2, /*periph*/NULL,
199 				    cam_sim_path(sim2),
200 				    CAM_TARGET_WILDCARD,
201 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
202 			xpt_bus_deregister(cam_sim_path(sim2));
203 			cam_sim_free(sim2, /*free_devq*/FALSE);
204 			sim2 = NULL;
205 			goto fail;
206 		}
207 		xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
208 		csa.ccb_h.func_code = XPT_SASYNC_CB;
209 		csa.event_enable = AC_LOST_DEVICE;
210 		csa.callback = ahc_async;
211 		csa.callback_arg = sim2;
212 		xpt_action((union ccb *)&csa);
213 		count++;
214 	}
215 
216 fail:
217 	if ((ahc->features & AHC_TWIN) != 0
218 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
219 		ahc->platform_data->sim_b = sim;
220 		ahc->platform_data->path_b = path;
221 		ahc->platform_data->sim = sim2;
222 		ahc->platform_data->path = path2;
223 	} else {
224 		ahc->platform_data->sim = sim;
225 		ahc->platform_data->path = path;
226 		ahc->platform_data->sim_b = sim2;
227 		ahc->platform_data->path_b = path2;
228 	}
229 	ahc_unlock(ahc, &s);
230 
231 	if (count != 0)
232 		/* We have to wait until after any system dumps... */
233 		ahc->platform_data->eh =
234 		    EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
235 					  ahc, SHUTDOWN_PRI_DEFAULT);
236 
237 	return (count);
238 }
239 
240 /*
241  * Catch an interrupt from the adapter
242  */
243 void
244 ahc_platform_intr(void *arg)
245 {
246 	struct	ahc_softc *ahc;
247 
248 	ahc = (struct ahc_softc *)arg;
249 	ahc_intr(ahc);
250 }
251 
252 /*
253  * We have an scb which has been processed by the
254  * adaptor, now we look to see how the operation
255  * went.
256  */
257 void
258 ahc_done(struct ahc_softc *ahc, struct scb *scb)
259 {
260 	union ccb *ccb;
261 
262 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
263 		  ("ahc_done - scb %d\n", scb->hscb->tag));
264 
265 	ccb = scb->io_ctx;
266 	LIST_REMOVE(scb, pending_links);
267 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
268 		struct scb_tailq *untagged_q;
269 		int target_offset;
270 
271 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
272 		untagged_q = &ahc->untagged_queues[target_offset];
273 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
274 		scb->flags &= ~SCB_UNTAGGEDQ;
275 		ahc_run_untagged_queue(ahc, untagged_q);
276 	}
277 
278 	untimeout(ahc_timeout, (caddr_t)scb, ccb->ccb_h.timeout_ch);
279 
280 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
281 		bus_dmasync_op_t op;
282 
283 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
284 			op = BUS_DMASYNC_POSTREAD;
285 		else
286 			op = BUS_DMASYNC_POSTWRITE;
287 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
288 		bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
289 	}
290 
291 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
292 		struct cam_path *ccb_path;
293 
294 		/*
295 		 * If we have finally disconnected, clean up our
296 		 * pending device state.
297 		 * XXX - There may be error states that cause where
298 		 *       we will remain connected.
299 		 */
300 		ccb_path = ccb->ccb_h.path;
301 		if (ahc->pending_device != NULL
302 		 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
303 
304 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
305 				ahc->pending_device = NULL;
306 			} else {
307 				xpt_print_path(ccb->ccb_h.path);
308 				printf("Still disconnected\n");
309 				ahc_freeze_ccb(ccb);
310 			}
311 		}
312 
313 		if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG)
314 			ccb->ccb_h.status |= CAM_REQ_CMP;
315 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
316 		ahc_free_scb(ahc, scb);
317 		xpt_done(ccb);
318 		return;
319 	}
320 
321 	/*
322 	 * If the recovery SCB completes, we have to be
323 	 * out of our timeout.
324 	 */
325 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
326 		struct	scb *list_scb;
327 
328 		/*
329 		 * We were able to complete the command successfully,
330 		 * so reinstate the timeouts for all other pending
331 		 * commands.
332 		 */
333 		LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
334 			union ccb *ccb;
335 			uint64_t time;
336 
337 			ccb = list_scb->io_ctx;
338 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
339 				continue;
340 
341 			time = ccb->ccb_h.timeout;
342 			time *= hz;
343 			time /= 1000;
344 			ccb->ccb_h.timeout_ch =
345 			    timeout(ahc_timeout, list_scb, time);
346 		}
347 
348 		if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
349 		 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
350 			ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
351 		ahc_print_path(ahc, scb);
352 		printf("no longer in timeout, status = %x\n",
353 		       ccb->ccb_h.status);
354 	}
355 
356 	/* Don't clobber any existing error state */
357 	if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
358 		ccb->ccb_h.status |= CAM_REQ_CMP;
359 	} else if ((scb->flags & SCB_SENSE) != 0) {
360 		/*
361 		 * We performed autosense retrieval.
362 		 *
363 		 * Zero any sense not transferred by the
364 		 * device.  The SCSI spec mandates that any
365 		 * untransfered data should be assumed to be
366 		 * zero.  Complete the 'bounce' of sense information
367 		 * through buffers accessible via bus-space by
368 		 * copying it into the clients csio.
369 		 */
370 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
371 		memcpy(&ccb->csio.sense_data,
372 		       ahc_get_sense_buf(ahc, scb),
373 		       (scb->sg_list->len & AHC_SG_LEN_MASK)
374 		       - ccb->csio.sense_resid);
375 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
376 	}
377 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
378 	ahc_free_scb(ahc, scb);
379 	xpt_done(ccb);
380 }
381 
382 static void
383 ahc_action(struct cam_sim *sim, union ccb *ccb)
384 {
385 	struct	ahc_softc *ahc;
386 	struct	ahc_tmode_lstate *lstate;
387 	u_int	target_id;
388 	u_int	our_id;
389 	long	s;
390 
391 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
392 
393 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
394 
395 	target_id = ccb->ccb_h.target_id;
396 	our_id = SIM_SCSI_ID(ahc, sim);
397 
398 	switch (ccb->ccb_h.func_code) {
399 	/* Common cases first */
400 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
401 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
402 	{
403 		struct	   ahc_tmode_tstate *tstate;
404 		cam_status status;
405 
406 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
407 					     &lstate, TRUE);
408 
409 		if (status != CAM_REQ_CMP) {
410 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
411 				/* Response from the black hole device */
412 				tstate = NULL;
413 				lstate = ahc->black_hole;
414 			} else {
415 				ccb->ccb_h.status = status;
416 				xpt_done(ccb);
417 				break;
418 			}
419 		}
420 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
421 
422 			ahc_lock(ahc, &s);
423 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
424 					  sim_links.sle);
425 			ccb->ccb_h.status = CAM_REQ_INPROG;
426 			if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
427 				ahc_run_tqinfifo(ahc, /*paused*/FALSE);
428 			ahc_unlock(ahc, &s);
429 			break;
430 		}
431 
432 		/*
433 		 * The target_id represents the target we attempt to
434 		 * select.  In target mode, this is the initiator of
435 		 * the original command.
436 		 */
437 		our_id = target_id;
438 		target_id = ccb->csio.init_id;
439 		/* FALLTHROUGH */
440 	}
441 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
442 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
443 	{
444 		struct	scb *scb;
445 		struct	hardware_scb *hscb;
446 
447 		if ((ahc->flags & AHC_INITIATORROLE) == 0
448 		 && (ccb->ccb_h.func_code == XPT_SCSI_IO
449 		  || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
450 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
451 			xpt_done(ccb);
452 			return;
453 		}
454 
455 		/*
456 		 * get an scb to use.
457 		 */
458 		ahc_lock(ahc, &s);
459 		if ((scb = ahc_get_scb(ahc)) == NULL) {
460 
461 			xpt_freeze_simq(sim, /*count*/1);
462 			ahc->flags |= AHC_RESOURCE_SHORTAGE;
463 			ahc_unlock(ahc, &s);
464 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
465 			xpt_done(ccb);
466 			return;
467 		}
468 		ahc_unlock(ahc, &s);
469 
470 		hscb = scb->hscb;
471 
472 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
473 			  ("start scb(%p)\n", scb));
474 		scb->io_ctx = ccb;
475 		/*
476 		 * So we can find the SCB when an abort is requested
477 		 */
478 		ccb->ccb_h.ccb_scb_ptr = scb;
479 
480 		/*
481 		 * Put all the arguments for the xfer in the scb
482 		 */
483 		hscb->control = 0;
484 		hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
485 		hscb->lun = ccb->ccb_h.target_lun;
486 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
487 			hscb->cdb_len = 0;
488 			scb->flags |= SCB_DEVICE_RESET;
489 			hscb->control |= MK_MESSAGE;
490 			ahc_execute_scb(scb, NULL, 0, 0);
491 		} else {
492 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
493 				struct target_data *tdata;
494 
495 				tdata = &hscb->shared_data.tdata;
496 				if (ahc->pending_device == lstate)
497 					scb->flags |= SCB_TARGET_IMMEDIATE;
498 				hscb->control |= TARGET_SCB;
499 				tdata->target_phases = IDENTIFY_SEEN;
500 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
501 					tdata->target_phases |= SPHASE_PENDING;
502 					tdata->scsi_status =
503 					    ccb->csio.scsi_status;
504 				}
505 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
506 					tdata->target_phases |= NO_DISCONNECT;
507 
508 				tdata->initiator_tag = ccb->csio.tag_id;
509 			}
510 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
511 				hscb->control |= ccb->csio.tag_action;
512 
513 			ahc_setup_data(ahc, sim, &ccb->csio, scb);
514 		}
515 		break;
516 	}
517 	case XPT_NOTIFY_ACK:
518 	case XPT_IMMED_NOTIFY:
519 	{
520 		struct	   ahc_tmode_tstate *tstate;
521 		struct	   ahc_tmode_lstate *lstate;
522 		cam_status status;
523 
524 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
525 					     &lstate, TRUE);
526 
527 		if (status != CAM_REQ_CMP) {
528 			ccb->ccb_h.status = status;
529 			xpt_done(ccb);
530 			break;
531 		}
532 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
533 				  sim_links.sle);
534 		ccb->ccb_h.status = CAM_REQ_INPROG;
535 		ahc_send_lstate_events(ahc, lstate);
536 		break;
537 	}
538 	case XPT_EN_LUN:		/* Enable LUN as a target */
539 		ahc_handle_en_lun(ahc, sim, ccb);
540 		xpt_done(ccb);
541 		break;
542 	case XPT_ABORT:			/* Abort the specified CCB */
543 	{
544 		ahc_abort_ccb(ahc, sim, ccb);
545 		break;
546 	}
547 	case XPT_SET_TRAN_SETTINGS:
548 	{
549 #ifdef AHC_NEW_TRAN_SETTINGS
550 		struct	ahc_devinfo devinfo;
551 		struct	ccb_trans_settings *cts;
552 		struct	ccb_trans_settings_scsi *scsi;
553 		struct	ccb_trans_settings_spi *spi;
554 		struct	ahc_initiator_tinfo *tinfo;
555 		struct	ahc_tmode_tstate *tstate;
556 		uint16_t *discenable;
557 		uint16_t *tagenable;
558 		u_int	update_type;
559 
560 		cts = &ccb->cts;
561 		scsi = &cts->proto_specific.scsi;
562 		spi = &cts->xport_specific.spi;
563 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
564 				    cts->ccb_h.target_id,
565 				    cts->ccb_h.target_lun,
566 				    SIM_CHANNEL(ahc, sim),
567 				    ROLE_UNKNOWN);
568 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
569 					    devinfo.our_scsiid,
570 					    devinfo.target, &tstate);
571 		update_type = 0;
572 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
573 			update_type |= AHC_TRANS_GOAL;
574 			discenable = &tstate->discenable;
575 			tagenable = &tstate->tagenable;
576 			tinfo->curr.protocol_version =
577 			    cts->protocol_version;
578 			tinfo->curr.transport_version =
579 			    cts->transport_version;
580 			tinfo->goal.protocol_version =
581 			    cts->protocol_version;
582 			tinfo->goal.transport_version =
583 			    cts->transport_version;
584 		} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
585 			update_type |= AHC_TRANS_USER;
586 			discenable = &ahc->user_discenable;
587 			tagenable = &ahc->user_tagenable;
588 			tinfo->user.protocol_version =
589 			    cts->protocol_version;
590 			tinfo->user.transport_version =
591 			    cts->transport_version;
592 		} else {
593 			ccb->ccb_h.status = CAM_REQ_INVALID;
594 			xpt_done(ccb);
595 			break;
596 		}
597 
598 		ahc_lock(ahc, &s);
599 
600 		if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
601 			if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
602 				*discenable |= devinfo.target_mask;
603 			else
604 				*discenable &= ~devinfo.target_mask;
605 		}
606 
607 		if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
608 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
609 				*tagenable |= devinfo.target_mask;
610 			else
611 				*tagenable &= ~devinfo.target_mask;
612 		}
613 
614 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
615 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
616 					   &spi->bus_width, ROLE_UNKNOWN);
617 			ahc_set_width(ahc, &devinfo, spi->bus_width,
618 				      update_type, /*paused*/FALSE);
619 		}
620 
621 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
622 			if (update_type == AHC_TRANS_USER)
623 				spi->ppr_options = tinfo->user.ppr_options;
624 			else
625 				spi->ppr_options = tinfo->goal.ppr_options;
626 		}
627 
628 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
629 			if (update_type == AHC_TRANS_USER)
630 				spi->sync_offset = tinfo->user.offset;
631 			else
632 				spi->sync_offset = tinfo->goal.offset;
633 		}
634 
635 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
636 			if (update_type == AHC_TRANS_USER)
637 				spi->sync_period = tinfo->user.period;
638 			else
639 				spi->sync_period = tinfo->goal.period;
640 		}
641 
642 		if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
643 		 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
644 			struct ahc_syncrate *syncrate;
645 			u_int maxsync;
646 
647 			if ((ahc->features & AHC_ULTRA2) != 0)
648 				maxsync = AHC_SYNCRATE_DT;
649 			else if ((ahc->features & AHC_ULTRA) != 0)
650 				maxsync = AHC_SYNCRATE_ULTRA;
651 			else
652 				maxsync = AHC_SYNCRATE_FAST;
653 
654 			syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
655 						     &spi->ppr_options,
656 						     maxsync);
657 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
658 					    syncrate, &spi->sync_offset,
659 					    spi->bus_width, ROLE_UNKNOWN);
660 
661 			/* We use a period of 0 to represent async */
662 			if (spi->sync_offset == 0) {
663 				spi->sync_period = 0;
664 				spi->ppr_options = 0;
665 			}
666 
667 			ahc_set_syncrate(ahc, &devinfo, syncrate,
668 					 spi->sync_period, spi->sync_offset,
669 					 spi->ppr_options, update_type,
670 					 /*paused*/FALSE);
671 		}
672 		ahc_unlock(ahc, &s);
673 		ccb->ccb_h.status = CAM_REQ_CMP;
674 		xpt_done(ccb);
675 #else
676 		struct	  ahc_devinfo devinfo;
677 		struct	  ccb_trans_settings *cts;
678 		struct	  ahc_initiator_tinfo *tinfo;
679 		struct	  ahc_tmode_tstate *tstate;
680 		uint16_t *discenable;
681 		uint16_t *tagenable;
682 		u_int	  update_type;
683 		long	  s;
684 
685 		cts = &ccb->cts;
686 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
687 				    cts->ccb_h.target_id,
688 				    cts->ccb_h.target_lun,
689 				    SIM_CHANNEL(ahc, sim),
690 				    ROLE_UNKNOWN);
691 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
692 					    devinfo.our_scsiid,
693 					    devinfo.target, &tstate);
694 		update_type = 0;
695 		if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
696 			update_type |= AHC_TRANS_GOAL;
697 			discenable = &tstate->discenable;
698 			tagenable = &tstate->tagenable;
699 		} else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
700 			update_type |= AHC_TRANS_USER;
701 			discenable = &ahc->user_discenable;
702 			tagenable = &ahc->user_tagenable;
703 		} else {
704 			ccb->ccb_h.status = CAM_REQ_INVALID;
705 			xpt_done(ccb);
706 			break;
707 		}
708 
709 		ahc_lock(ahc, &s);
710 
711 		if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
712 			if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
713 				*discenable |= devinfo.target_mask;
714 			else
715 				*discenable &= ~devinfo.target_mask;
716 		}
717 
718 		if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
719 			if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
720 				*tagenable |= devinfo.target_mask;
721 			else
722 				*tagenable &= ~devinfo.target_mask;
723 		}
724 
725 		if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
726 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
727 					   &cts->bus_width, ROLE_UNKNOWN);
728 			ahc_set_width(ahc, &devinfo, cts->bus_width,
729 				      update_type, /*paused*/FALSE);
730 		}
731 
732 		if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
733 			if (update_type == AHC_TRANS_USER)
734 				cts->sync_offset = tinfo->user.offset;
735 			else
736 				cts->sync_offset = tinfo->goal.offset;
737 		}
738 
739 		if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
740 			if (update_type == AHC_TRANS_USER)
741 				cts->sync_period = tinfo->user.period;
742 			else
743 				cts->sync_period = tinfo->goal.period;
744 		}
745 
746 		if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
747 		 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)) {
748 			struct ahc_syncrate *syncrate;
749 			u_int ppr_options;
750 			u_int maxsync;
751 
752 			if ((ahc->features & AHC_ULTRA2) != 0)
753 				maxsync = AHC_SYNCRATE_DT;
754 			else if ((ahc->features & AHC_ULTRA) != 0)
755 				maxsync = AHC_SYNCRATE_ULTRA;
756 			else
757 				maxsync = AHC_SYNCRATE_FAST;
758 
759 			ppr_options = 0;
760 			if (cts->sync_period <= 9)
761 				ppr_options = MSG_EXT_PPR_DT_REQ;
762 
763 			syncrate = ahc_find_syncrate(ahc, &cts->sync_period,
764 						     &ppr_options,
765 						     maxsync);
766 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
767 					    syncrate, &cts->sync_offset,
768 					    MSG_EXT_WDTR_BUS_8_BIT,
769 					    ROLE_UNKNOWN);
770 
771 			/* We use a period of 0 to represent async */
772 			if (cts->sync_offset == 0) {
773 				cts->sync_period = 0;
774 				ppr_options = 0;
775 			}
776 
777 			if (ppr_options == MSG_EXT_PPR_DT_REQ
778 			 && tinfo->user.transport_version >= 3) {
779 				tinfo->goal.transport_version =
780 				    tinfo->user.transport_version;
781 				tinfo->curr.transport_version =
782 				    tinfo->user.transport_version;
783 			}
784 
785 			ahc_set_syncrate(ahc, &devinfo, syncrate,
786 					 cts->sync_period, cts->sync_offset,
787 					 ppr_options, update_type,
788 					 /*paused*/FALSE);
789 		}
790 		ahc_unlock(ahc, &s);
791 		ccb->ccb_h.status = CAM_REQ_CMP;
792 		xpt_done(ccb);
793 #endif
794 		break;
795 	}
796 	case XPT_GET_TRAN_SETTINGS:
797 	/* Get default/user set transfer settings for the target */
798 	{
799 
800 		ahc_lock(ahc, &s);
801 		ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
802 				      SIM_CHANNEL(ahc, sim), &ccb->cts);
803 		ahc_unlock(ahc, &s);
804 		xpt_done(ccb);
805 		break;
806 	}
807 	case XPT_CALC_GEOMETRY:
808 	{
809 		struct	  ccb_calc_geometry *ccg;
810 		uint32_t size_mb;
811 		uint32_t secs_per_cylinder;
812 		int	  extended;
813 
814 		ccg = &ccb->ccg;
815 		size_mb = ccg->volume_size
816 			/ ((1024L * 1024L) / ccg->block_size);
817 		extended = SIM_IS_SCSIBUS_B(ahc, sim)
818 			? ahc->flags & AHC_EXTENDED_TRANS_B
819 			: ahc->flags & AHC_EXTENDED_TRANS_A;
820 
821 		if (size_mb > 1024 && extended) {
822 			ccg->heads = 255;
823 			ccg->secs_per_track = 63;
824 		} else {
825 			ccg->heads = 64;
826 			ccg->secs_per_track = 32;
827 		}
828 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
829 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
830 		ccb->ccb_h.status = CAM_REQ_CMP;
831 		xpt_done(ccb);
832 		break;
833 	}
834 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
835 	{
836 		int  found;
837 
838 		ahc_lock(ahc, &s);
839 		found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
840 					  /*initiate reset*/TRUE);
841 		ahc_unlock(ahc, &s);
842 		if (bootverbose) {
843 			xpt_print_path(SIM_PATH(ahc, sim));
844 			printf("SCSI bus reset delivered. "
845 			       "%d SCBs aborted.\n", found);
846 		}
847 		ccb->ccb_h.status = CAM_REQ_CMP;
848 		xpt_done(ccb);
849 		break;
850 	}
851 	case XPT_TERM_IO:		/* Terminate the I/O process */
852 		/* XXX Implement */
853 		ccb->ccb_h.status = CAM_REQ_INVALID;
854 		xpt_done(ccb);
855 		break;
856 	case XPT_PATH_INQ:		/* Path routing inquiry */
857 	{
858 		struct ccb_pathinq *cpi = &ccb->cpi;
859 
860 		cpi->version_num = 1; /* XXX??? */
861 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
862 		if ((ahc->features & AHC_WIDE) != 0)
863 			cpi->hba_inquiry |= PI_WIDE_16;
864 		if ((ahc->features & AHC_TARGETMODE) != 0) {
865 			cpi->target_sprt = PIT_PROCESSOR
866 					 | PIT_DISCONNECT
867 					 | PIT_TERM_IO;
868 		} else {
869 			cpi->target_sprt = 0;
870 		}
871 		cpi->hba_misc = 0;
872 		cpi->hba_eng_cnt = 0;
873 		cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
874 		cpi->max_lun = AHC_NUM_LUNS - 1;
875 		if (SIM_IS_SCSIBUS_B(ahc, sim)) {
876 			cpi->initiator_id = ahc->our_id_b;
877 			if ((ahc->flags & AHC_RESET_BUS_B) == 0)
878 				cpi->hba_misc |= PIM_NOBUSRESET;
879 		} else {
880 			cpi->initiator_id = ahc->our_id;
881 			if ((ahc->flags & AHC_RESET_BUS_A) == 0)
882 				cpi->hba_misc |= PIM_NOBUSRESET;
883 		}
884 		cpi->bus_id = cam_sim_bus(sim);
885 		cpi->base_transfer_speed = 3300;
886 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
887 		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
888 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
889 		cpi->unit_number = cam_sim_unit(sim);
890 #ifdef AHC_NEW_TRAN_SETTINGS
891 		cpi->protocol = PROTO_SCSI;
892 		cpi->protocol_version = SCSI_REV_2;
893 		cpi->transport = XPORT_SPI;
894 		cpi->transport_version = 2;
895 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
896 		if ((ahc->features & AHC_DT) != 0) {
897 			cpi->transport_version = 3;
898 			cpi->xport_specific.spi.ppr_options =
899 			    SID_SPI_CLOCK_DT_ST;
900 		}
901 #endif
902 		cpi->ccb_h.status = CAM_REQ_CMP;
903 		xpt_done(ccb);
904 		break;
905 	}
906 	default:
907 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
908 		xpt_done(ccb);
909 		break;
910 	}
911 }
912 
913 static void
914 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
915 		      struct ccb_trans_settings *cts)
916 {
917 #ifdef AHC_NEW_TRAN_SETTINGS
918 	struct	ahc_devinfo devinfo;
919 	struct	ccb_trans_settings_scsi *scsi;
920 	struct	ccb_trans_settings_spi *spi;
921 	struct	ahc_initiator_tinfo *targ_info;
922 	struct	ahc_tmode_tstate *tstate;
923 	struct	ahc_transinfo *tinfo;
924 
925 	scsi = &cts->proto_specific.scsi;
926 	spi = &cts->xport_specific.spi;
927 	ahc_compile_devinfo(&devinfo, our_id,
928 			    cts->ccb_h.target_id,
929 			    cts->ccb_h.target_lun,
930 			    channel, ROLE_UNKNOWN);
931 	targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
932 					devinfo.our_scsiid,
933 					devinfo.target, &tstate);
934 
935 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
936 		tinfo = &targ_info->curr;
937 	else
938 		tinfo = &targ_info->user;
939 
940 	scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
941 	spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
942 	if (cts->type == CTS_TYPE_USER_SETTINGS) {
943 		if ((ahc->user_discenable & devinfo.target_mask) != 0)
944 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
945 
946 		if ((ahc->user_tagenable & devinfo.target_mask) != 0)
947 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
948 	} else {
949 		if ((tstate->discenable & devinfo.target_mask) != 0)
950 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
951 
952 		if ((tstate->tagenable & devinfo.target_mask) != 0)
953 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
954 	}
955 	cts->protocol_version = tinfo->protocol_version;
956 	cts->transport_version = tinfo->transport_version;
957 
958 	spi->sync_period = tinfo->period;
959 	spi->sync_offset = tinfo->offset;
960 	spi->bus_width = tinfo->width;
961 	spi->ppr_options = tinfo->ppr_options;
962 
963 	cts->protocol = PROTO_SCSI;
964 	cts->transport = XPORT_SPI;
965 	spi->valid = CTS_SPI_VALID_SYNC_RATE
966 		   | CTS_SPI_VALID_SYNC_OFFSET
967 		   | CTS_SPI_VALID_BUS_WIDTH
968 		   | CTS_SPI_VALID_PPR_OPTIONS;
969 
970 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
971 		scsi->valid = CTS_SCSI_VALID_TQ;
972 		spi->valid |= CTS_SPI_VALID_DISC;
973 	} else {
974 		scsi->valid = 0;
975 	}
976 
977 	cts->ccb_h.status = CAM_REQ_CMP;
978 #else
979 	struct	ahc_devinfo devinfo;
980 	struct	ahc_initiator_tinfo *targ_info;
981 	struct	ahc_tmode_tstate *tstate;
982 	struct	ahc_transinfo *tinfo;
983 	long	s;
984 
985 	ahc_compile_devinfo(&devinfo, our_id,
986 			    cts->ccb_h.target_id,
987 			    cts->ccb_h.target_lun,
988 			    channel, ROLE_UNKNOWN);
989 	targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
990 					devinfo.our_scsiid,
991 					devinfo.target, &tstate);
992 
993 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
994 		tinfo = &targ_info->curr;
995 	else
996 		tinfo = &targ_info->user;
997 
998 	ahc_lock(ahc, &s);
999 
1000 	cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
1001 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
1002 		if ((ahc->user_discenable & devinfo.target_mask) != 0)
1003 			cts->flags |= CCB_TRANS_DISC_ENB;
1004 
1005 		if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1006 			cts->flags |= CCB_TRANS_TAG_ENB;
1007 	} else {
1008 		if ((tstate->discenable & devinfo.target_mask) != 0)
1009 			cts->flags |= CCB_TRANS_DISC_ENB;
1010 
1011 		if ((tstate->tagenable & devinfo.target_mask) != 0)
1012 			cts->flags |= CCB_TRANS_TAG_ENB;
1013 	}
1014 	cts->sync_period = tinfo->period;
1015 	cts->sync_offset = tinfo->offset;
1016 	cts->bus_width = tinfo->width;
1017 
1018 	ahc_unlock(ahc, &s);
1019 
1020 	cts->valid = CCB_TRANS_SYNC_RATE_VALID
1021 		   | CCB_TRANS_SYNC_OFFSET_VALID
1022 		   | CCB_TRANS_BUS_WIDTH_VALID;
1023 
1024 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1025 		cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1026 
1027 	cts->ccb_h.status = CAM_REQ_CMP;
1028 #endif
1029 }
1030 
1031 static void
1032 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1033 {
1034 	struct ahc_softc *ahc;
1035 	struct cam_sim *sim;
1036 
1037 	sim = (struct cam_sim *)callback_arg;
1038 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
1039 	switch (code) {
1040 	case AC_LOST_DEVICE:
1041 	{
1042 		struct	ahc_devinfo devinfo;
1043 		long	s;
1044 
1045 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1046 				    xpt_path_target_id(path),
1047 				    xpt_path_lun_id(path),
1048 				    SIM_CHANNEL(ahc, sim),
1049 				    ROLE_UNKNOWN);
1050 
1051 		/*
1052 		 * Revert to async/narrow transfers
1053 		 * for the next device.
1054 		 */
1055 		ahc_lock(ahc, &s);
1056 		ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1057 			      AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1058 		ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1059 				 /*period*/0, /*offset*/0, /*ppr_options*/0,
1060 				 AHC_TRANS_GOAL|AHC_TRANS_CUR,
1061 				 /*paused*/FALSE);
1062 		ahc_unlock(ahc, &s);
1063 		break;
1064 	}
1065 	default:
1066 		break;
1067 	}
1068 }
1069 
1070 static void
1071 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1072 		int error)
1073 {
1074 	struct	scb *scb;
1075 	union	ccb *ccb;
1076 	struct	ahc_softc *ahc;
1077 	struct	ahc_initiator_tinfo *tinfo;
1078 	struct	ahc_tmode_tstate *tstate;
1079 	u_int	mask;
1080 	long	s;
1081 
1082 	scb = (struct scb *)arg;
1083 	ccb = scb->io_ctx;
1084 	ahc = scb->ahc_softc;
1085 
1086 	if (error != 0) {
1087 		if (error == EFBIG)
1088 			ahc_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1089 		else
1090 			ahc_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1091 		if (nsegments != 0)
1092 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1093 		ahc_lock(ahc, &s);
1094 		ahc_free_scb(ahc, scb);
1095 		ahc_unlock(ahc, &s);
1096 		xpt_done(ccb);
1097 		return;
1098 	}
1099 	if (nsegments != 0) {
1100 		struct	  ahc_dma_seg *sg;
1101 		bus_dma_segment_t *end_seg;
1102 		bus_dmasync_op_t op;
1103 
1104 		end_seg = dm_segs + nsegments;
1105 
1106 		/* Copy the segments into our SG list */
1107 		sg = scb->sg_list;
1108 		while (dm_segs < end_seg) {
1109 			uint32_t len;
1110 
1111 			sg->addr = ahc_htole32(dm_segs->ds_addr);
1112 			len = dm_segs->ds_len
1113 			    | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1114 			sg->len = ahc_htole32(len);
1115 			sg++;
1116 			dm_segs++;
1117 		}
1118 
1119 		/*
1120 		 * Note where to find the SG entries in bus space.
1121 		 * We also set the full residual flag which the
1122 		 * sequencer will clear as soon as a data transfer
1123 		 * occurs.
1124 		 */
1125 		scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
1126 
1127 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1128 			op = BUS_DMASYNC_PREREAD;
1129 		else
1130 			op = BUS_DMASYNC_PREWRITE;
1131 
1132 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1133 
1134 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1135 			struct target_data *tdata;
1136 
1137 			tdata = &scb->hscb->shared_data.tdata;
1138 			tdata->target_phases |= DPHASE_PENDING;
1139 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1140 				tdata->data_phase = P_DATAOUT;
1141 			else
1142 				tdata->data_phase = P_DATAIN;
1143 
1144 			/*
1145 			 * If the transfer is of an odd length and in the
1146 			 * "in" direction (scsi->HostBus), then it may
1147 			 * trigger a bug in the 'WideODD' feature of
1148 			 * non-Ultra2 chips.  Force the total data-length
1149 			 * to be even by adding an extra, 1 byte, SG,
1150 			 * element.  We do this even if we are not currently
1151 			 * negotiated wide as negotiation could occur before
1152 			 * this command is executed.
1153 			 */
1154 			if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1155 			 && (ccb->csio.dxfer_len & 0x1) != 0
1156 			 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1157 
1158 				nsegments++;
1159 				if (nsegments > AHC_NSEG) {
1160 
1161 					ahc_set_transaction_status(scb,
1162 					    CAM_REQ_TOO_BIG);
1163 					bus_dmamap_unload(ahc->buffer_dmat,
1164 							  scb->dmamap);
1165 					ahc_lock(ahc, &s);
1166 					ahc_free_scb(ahc, scb);
1167 					ahc_unlock(ahc, &s);
1168 					xpt_done(ccb);
1169 					return;
1170 				}
1171 				sg->addr = ahc_htole32(ahc->dma_bug_buf);
1172 				sg->len = ahc_htole32(1);
1173 				sg++;
1174 			}
1175 		}
1176 		sg--;
1177 		sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1178 
1179 		/* Copy the first SG into the "current" data pointer area */
1180 		scb->hscb->dataptr = scb->sg_list->addr;
1181 		scb->hscb->datacnt = scb->sg_list->len;
1182 	} else {
1183 		scb->hscb->sgptr = SG_LIST_NULL;
1184 		scb->hscb->dataptr = 0;
1185 		scb->hscb->datacnt = 0;
1186 	}
1187 
1188 	scb->sg_count = nsegments;
1189 
1190 	ahc_lock(ahc, &s);
1191 
1192 	/*
1193 	 * Last time we need to check if this SCB needs to
1194 	 * be aborted.
1195 	 */
1196 	if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1197 		if (nsegments != 0)
1198 			bus_dmamap_unload(ahc->buffer_dmat,
1199 					  scb->dmamap);
1200 		ahc_free_scb(ahc, scb);
1201 		ahc_unlock(ahc, &s);
1202 		xpt_done(ccb);
1203 		return;
1204 	}
1205 
1206 	tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1207 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1208 				    SCSIID_TARGET(ahc, scb->hscb->scsiid),
1209 				    &tstate);
1210 
1211 	mask = SCB_GET_TARGET_MASK(ahc, scb);
1212 	scb->hscb->scsirate = tinfo->scsirate;
1213 	scb->hscb->scsioffset = tinfo->curr.offset;
1214 	if ((tstate->ultraenb & mask) != 0)
1215 		scb->hscb->control |= ULTRAENB;
1216 
1217 	if ((tstate->discenable & mask) != 0
1218 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1219 		scb->hscb->control |= DISCENB;
1220 
1221 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1222 	 && (tinfo->goal.width != 0
1223 	  || tinfo->goal.period != 0
1224 	  || tinfo->goal.ppr_options != 0)) {
1225 		scb->flags |= SCB_NEGOTIATE;
1226 		scb->hscb->control |= MK_MESSAGE;
1227 	} else if ((tstate->auto_negotiate & mask) != 0) {
1228 		scb->flags |= SCB_AUTO_NEGOTIATE;
1229 		scb->hscb->control |= MK_MESSAGE;
1230 	}
1231 
1232 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1233 
1234 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1235 
1236 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1237 		uint64_t time;
1238 
1239 		if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1240 			ccb->ccb_h.timeout = 5 * 1000;
1241 
1242 		time = ccb->ccb_h.timeout;
1243 		time *= hz;
1244 		time /= 1000;
1245 		ccb->ccb_h.timeout_ch =
1246 		    timeout(ahc_timeout, (caddr_t)scb, time);
1247 	}
1248 
1249 	/*
1250 	 * We only allow one untagged transaction
1251 	 * per target in the initiator role unless
1252 	 * we are storing a full busy target *lun*
1253 	 * table in SCB space.
1254 	 */
1255 	if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1256 	 && (ahc->flags & AHC_SCB_BTT) == 0) {
1257 		struct scb_tailq *untagged_q;
1258 		int target_offset;
1259 
1260 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1261 		untagged_q = &(ahc->untagged_queues[target_offset]);
1262 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1263 		scb->flags |= SCB_UNTAGGEDQ;
1264 		if (TAILQ_FIRST(untagged_q) != scb) {
1265 			ahc_unlock(ahc, &s);
1266 			return;
1267 		}
1268 	}
1269 	scb->flags |= SCB_ACTIVE;
1270 
1271 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1272 		/* Define a mapping from our tag to the SCB. */
1273 		ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1274 		ahc_pause(ahc);
1275 		if ((ahc->flags & AHC_PAGESCBS) == 0)
1276 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1277 		ahc_outb(ahc, SCB_TAG, scb->hscb->tag);
1278 		ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
1279 		ahc_unpause(ahc);
1280 	} else {
1281 		ahc_queue_scb(ahc, scb);
1282 	}
1283 
1284 	ahc_unlock(ahc, &s);
1285 }
1286 
1287 static void
1288 ahc_poll(struct cam_sim *sim)
1289 {
1290 	ahc_intr(cam_sim_softc(sim));
1291 }
1292 
1293 static void
1294 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1295 	       struct ccb_scsiio *csio, struct scb *scb)
1296 {
1297 	struct hardware_scb *hscb;
1298 	struct ccb_hdr *ccb_h;
1299 
1300 	hscb = scb->hscb;
1301 	ccb_h = &csio->ccb_h;
1302 
1303 	csio->resid = 0;
1304 	csio->sense_resid = 0;
1305 	if (ccb_h->func_code == XPT_SCSI_IO) {
1306 		hscb->cdb_len = csio->cdb_len;
1307 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1308 
1309 			if (hscb->cdb_len > sizeof(hscb->cdb32)
1310 			 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1311 				u_long s;
1312 
1313 				ahc_set_transaction_status(scb,
1314 							   CAM_REQ_INVALID);
1315 				ahc_lock(ahc, &s);
1316 				ahc_free_scb(ahc, scb);
1317 				ahc_unlock(ahc, &s);
1318 				xpt_done((union ccb *)csio);
1319 				return;
1320 			}
1321 			if (hscb->cdb_len > 12) {
1322 				memcpy(hscb->cdb32,
1323 				       csio->cdb_io.cdb_ptr,
1324 				       hscb->cdb_len);
1325 				scb->flags |= SCB_CDB32_PTR;
1326 			} else {
1327 				memcpy(hscb->shared_data.cdb,
1328 				       csio->cdb_io.cdb_ptr,
1329 				       hscb->cdb_len);
1330 			}
1331 		} else {
1332 			if (hscb->cdb_len > 12) {
1333 				memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1334 				       hscb->cdb_len);
1335 				scb->flags |= SCB_CDB32_PTR;
1336 			} else {
1337 				memcpy(hscb->shared_data.cdb,
1338 				       csio->cdb_io.cdb_bytes,
1339 				       hscb->cdb_len);
1340 			}
1341 		}
1342 	}
1343 
1344 	/* Only use S/G if there is a transfer */
1345 	if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1346 		if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1347 			/* We've been given a pointer to a single buffer */
1348 			if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1349 				int s;
1350 				int error;
1351 
1352 				s = splsoftvm();
1353 				error = bus_dmamap_load(ahc->buffer_dmat,
1354 							scb->dmamap,
1355 							csio->data_ptr,
1356 							csio->dxfer_len,
1357 							ahc_execute_scb,
1358 							scb, /*flags*/0);
1359 				if (error == EINPROGRESS) {
1360 					/*
1361 					 * So as to maintain ordering,
1362 					 * freeze the controller queue
1363 					 * until our mapping is
1364 					 * returned.
1365 					 */
1366 					xpt_freeze_simq(sim,
1367 							/*count*/1);
1368 					scb->io_ctx->ccb_h.status |=
1369 					    CAM_RELEASE_SIMQ;
1370 				}
1371 				splx(s);
1372 			} else {
1373 				struct bus_dma_segment seg;
1374 
1375 				/* Pointer to physical buffer */
1376 				if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1377 					panic("ahc_setup_data - Transfer size "
1378 					      "larger than can device max");
1379 
1380 				seg.ds_addr = (bus_addr_t)csio->data_ptr;
1381 				seg.ds_len = csio->dxfer_len;
1382 				ahc_execute_scb(scb, &seg, 1, 0);
1383 			}
1384 		} else {
1385 			struct bus_dma_segment *segs;
1386 
1387 			if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1388 				panic("ahc_setup_data - Physical segment "
1389 				      "pointers unsupported");
1390 
1391 			if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1392 				panic("ahc_setup_data - Virtual segment "
1393 				      "addresses unsupported");
1394 
1395 			/* Just use the segments provided */
1396 			segs = (struct bus_dma_segment *)csio->data_ptr;
1397 			ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1398 		}
1399 	} else {
1400 		ahc_execute_scb(scb, NULL, 0, 0);
1401 	}
1402 }
1403 
1404 static void
1405 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1406 
1407 	if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1408 		struct scb *list_scb;
1409 
1410 		scb->flags |= SCB_RECOVERY_SCB;
1411 
1412 		/*
1413 		 * Take all queued, but not sent SCBs out of the equation.
1414 		 * Also ensure that no new CCBs are queued to us while we
1415 		 * try to fix this problem.
1416 		 */
1417 		if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1418 			xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1419 			scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1420 		}
1421 
1422 		/*
1423 		 * Go through all of our pending SCBs and remove
1424 		 * any scheduled timeouts for them.  We will reschedule
1425 		 * them after we've successfully fixed this problem.
1426 		 */
1427 		LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1428 			union ccb *ccb;
1429 
1430 			ccb = list_scb->io_ctx;
1431 			untimeout(ahc_timeout, list_scb, ccb->ccb_h.timeout_ch);
1432 		}
1433 	}
1434 }
1435 
1436 void
1437 ahc_timeout(void *arg)
1438 {
1439 	struct	scb *scb;
1440 	struct	ahc_softc *ahc;
1441 	long	s;
1442 	int	found;
1443 	u_int	last_phase;
1444 	int	target;
1445 	int	lun;
1446 	int	i;
1447 	char	channel;
1448 
1449 	scb = (struct scb *)arg;
1450 	ahc = (struct ahc_softc *)scb->ahc_softc;
1451 
1452 	ahc_lock(ahc, &s);
1453 
1454 	ahc_pause_and_flushwork(ahc);
1455 
1456 	if ((scb->flags & SCB_ACTIVE) == 0) {
1457 		/* Previous timeout took care of me already */
1458 		printf("%s: Timedout SCB already complete. "
1459 		       "Interrupts may not be functioning.\n", ahc_name(ahc));
1460 		ahc_unpause(ahc);
1461 		ahc_unlock(ahc, &s);
1462 		return;
1463 	}
1464 
1465 	target = SCB_GET_TARGET(ahc, scb);
1466 	channel = SCB_GET_CHANNEL(ahc, scb);
1467 	lun = SCB_GET_LUN(scb);
1468 
1469 	ahc_print_path(ahc, scb);
1470 	printf("SCB 0x%x - timed out\n", scb->hscb->tag);
1471 	ahc_dump_card_state(ahc);
1472 	last_phase = ahc_inb(ahc, LASTPHASE);
1473 	if (scb->sg_count > 0) {
1474 		for (i = 0; i < scb->sg_count; i++) {
1475 			printf("sg[%d] - Addr 0x%x : Length %d\n",
1476 			       i,
1477 			       scb->sg_list[i].addr,
1478 			       scb->sg_list[i].len & AHC_SG_LEN_MASK);
1479 		}
1480 	}
1481 	if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1482 		/*
1483 		 * Been down this road before.
1484 		 * Do a full bus reset.
1485 		 */
1486 bus_reset:
1487 		ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1488 		found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1489 		printf("%s: Issued Channel %c Bus Reset. "
1490 		       "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1491 	} else {
1492 		/*
1493 		 * If we are a target, transition to bus free and report
1494 		 * the timeout.
1495 		 *
1496 		 * The target/initiator that is holding up the bus may not
1497 		 * be the same as the one that triggered this timeout
1498 		 * (different commands have different timeout lengths).
1499 		 * If the bus is idle and we are actiing as the initiator
1500 		 * for this request, queue a BDR message to the timed out
1501 		 * target.  Otherwise, if the timed out transaction is
1502 		 * active:
1503 		 *   Initiator transaction:
1504 		 *	Stuff the message buffer with a BDR message and assert
1505 		 *	ATN in the hopes that the target will let go of the bus
1506 		 *	and go to the mesgout phase.  If this fails, we'll
1507 		 *	get another timeout 2 seconds later which will attempt
1508 		 *	a bus reset.
1509 		 *
1510 		 *   Target transaction:
1511 		 *	Transition to BUS FREE and report the error.
1512 		 *	It's good to be the target!
1513 		 */
1514 		u_int active_scb_index;
1515 		u_int saved_scbptr;
1516 
1517 		saved_scbptr = ahc_inb(ahc, SCBPTR);
1518 		active_scb_index = ahc_inb(ahc, SCB_TAG);
1519 
1520 		if (last_phase != P_BUSFREE
1521 		  && (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) != 0
1522 		  && (active_scb_index < ahc->scb_data->numscbs)) {
1523 			struct scb *active_scb;
1524 
1525 			/*
1526 			 * If the active SCB is not us, assume that
1527 			 * the active SCB has a longer timeout than
1528 			 * the timedout SCB, and wait for the active
1529 			 * SCB to timeout.
1530 			 */
1531 			active_scb = ahc_lookup_scb(ahc, active_scb_index);
1532 			if (active_scb != scb) {
1533 				struct	 ccb_hdr *ccbh;
1534 				uint64_t newtimeout;
1535 
1536 				ahc_print_path(ahc, scb);
1537 				printf("Other SCB Timeout%s",
1538 			 	       (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1539 				       ? " again\n" : "\n");
1540 				scb->flags |= SCB_OTHERTCL_TIMEOUT;
1541 				newtimeout =
1542 				    MAX(active_scb->io_ctx->ccb_h.timeout,
1543 					scb->io_ctx->ccb_h.timeout);
1544 				newtimeout *= hz;
1545 				newtimeout /= 1000;
1546 				ccbh = &scb->io_ctx->ccb_h;
1547 				scb->io_ctx->ccb_h.timeout_ch =
1548 				    timeout(ahc_timeout, scb, newtimeout);
1549 				ahc_unpause(ahc);
1550 				ahc_unlock(ahc, &s);
1551 				return;
1552 			}
1553 
1554 			/* It's us */
1555 			if ((scb->hscb->control & TARGET_SCB) != 0) {
1556 
1557 				/*
1558 				 * Send back any queued up transactions
1559 				 * and properly record the error condition.
1560 				 */
1561 				ahc_freeze_devq(ahc, scb);
1562 				ahc_set_transaction_status(scb,
1563 							   CAM_CMD_TIMEOUT);
1564 				ahc_freeze_scb(scb);
1565 				ahc_done(ahc, scb);
1566 
1567 				/* Will clear us from the bus */
1568 				ahc_restart(ahc);
1569 				ahc_unlock(ahc, &s);
1570 				return;
1571 			}
1572 
1573 			ahc_set_recoveryscb(ahc, active_scb);
1574 			ahc_outb(ahc, MSG_OUT, HOST_MSG);
1575 			ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1576 			ahc_print_path(ahc, active_scb);
1577 			printf("BDR message in message buffer\n");
1578 			active_scb->flags |= SCB_DEVICE_RESET;
1579 			active_scb->io_ctx->ccb_h.timeout_ch =
1580 			    timeout(ahc_timeout, (caddr_t)active_scb, 2 * hz);
1581 			ahc_unpause(ahc);
1582 		} else {
1583 			int	 disconnected;
1584 
1585 			/* XXX Shouldn't panic.  Just punt instead */
1586 			if ((scb->hscb->control & TARGET_SCB) != 0)
1587 				panic("Timed-out target SCB but bus idle");
1588 
1589 			if (last_phase != P_BUSFREE
1590 			 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1591 				/* XXX What happened to the SCB? */
1592 				/* Hung target selection.  Goto busfree */
1593 				printf("%s: Hung target selection\n",
1594 				       ahc_name(ahc));
1595 				ahc_restart(ahc);
1596 				ahc_unlock(ahc, &s);
1597 				return;
1598 			}
1599 
1600 			if (ahc_search_qinfifo(ahc, target, channel, lun,
1601 					       scb->hscb->tag, ROLE_INITIATOR,
1602 					       /*status*/0, SEARCH_COUNT) > 0) {
1603 				disconnected = FALSE;
1604 			} else {
1605 				disconnected = TRUE;
1606 			}
1607 
1608 			if (disconnected) {
1609 
1610 				ahc_set_recoveryscb(ahc, scb);
1611 				/*
1612 				 * Actually re-queue this SCB in an attempt
1613 				 * to select the device before it reconnects.
1614 				 * In either case (selection or reselection),
1615 				 * we will now issue a target reset to the
1616 				 * timed-out device.
1617 				 *
1618 				 * Set the MK_MESSAGE control bit indicating
1619 				 * that we desire to send a message.  We
1620 				 * also set the disconnected flag since
1621 				 * in the paging case there is no guarantee
1622 				 * that our SCB control byte matches the
1623 				 * version on the card.  We don't want the
1624 				 * sequencer to abort the command thinking
1625 				 * an unsolicited reselection occurred.
1626 				 */
1627 				scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1628 				scb->flags |= SCB_DEVICE_RESET;
1629 
1630 				/*
1631 				 * Remove any cached copy of this SCB in the
1632 				 * disconnected list in preparation for the
1633 				 * queuing of our abort SCB.  We use the
1634 				 * same element in the SCB, SCB_NEXT, for
1635 				 * both the qinfifo and the disconnected list.
1636 				 */
1637 				ahc_search_disc_list(ahc, target, channel,
1638 						     lun, scb->hscb->tag,
1639 						     /*stop_on_first*/TRUE,
1640 						     /*remove*/TRUE,
1641 						     /*save_state*/FALSE);
1642 
1643 				/*
1644 				 * In the non-paging case, the sequencer will
1645 				 * never re-reference the in-core SCB.
1646 				 * To make sure we are notified during
1647 				 * reslection, set the MK_MESSAGE flag in
1648 				 * the card's copy of the SCB.
1649 				 */
1650 				if ((ahc->flags & AHC_PAGESCBS) == 0) {
1651 					ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1652 					ahc_outb(ahc, SCB_CONTROL,
1653 						 ahc_inb(ahc, SCB_CONTROL)
1654 						| MK_MESSAGE);
1655 				}
1656 
1657 				/*
1658 				 * Clear out any entries in the QINFIFO first
1659 				 * so we are the next SCB for this target
1660 				 * to run.
1661 				 */
1662 				ahc_search_qinfifo(ahc,
1663 						   SCB_GET_TARGET(ahc, scb),
1664 						   channel, SCB_GET_LUN(scb),
1665 						   SCB_LIST_NULL,
1666 						   ROLE_INITIATOR,
1667 						   CAM_REQUEUE_REQ,
1668 						   SEARCH_COMPLETE);
1669 				ahc_print_path(ahc, scb);
1670 				printf("Queuing a BDR SCB\n");
1671 				ahc_qinfifo_requeue_tail(ahc, scb);
1672 				ahc_outb(ahc, SCBPTR, saved_scbptr);
1673 				scb->io_ctx->ccb_h.timeout_ch =
1674 				    timeout(ahc_timeout, (caddr_t)scb, 2 * hz);
1675 				ahc_unpause(ahc);
1676 			} else {
1677 				/* Go "immediatly" to the bus reset */
1678 				/* This shouldn't happen */
1679 				ahc_set_recoveryscb(ahc, scb);
1680 				ahc_print_path(ahc, scb);
1681 				printf("SCB %d: Immediate reset.  "
1682 					"Flags = 0x%x\n", scb->hscb->tag,
1683 					scb->flags);
1684 				goto bus_reset;
1685 			}
1686 		}
1687 	}
1688 	ahc_unlock(ahc, &s);
1689 }
1690 
1691 static void
1692 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1693 {
1694 	union ccb *abort_ccb;
1695 
1696 	abort_ccb = ccb->cab.abort_ccb;
1697 	switch (abort_ccb->ccb_h.func_code) {
1698 	case XPT_ACCEPT_TARGET_IO:
1699 	case XPT_IMMED_NOTIFY:
1700 	case XPT_CONT_TARGET_IO:
1701 	{
1702 		struct ahc_tmode_tstate *tstate;
1703 		struct ahc_tmode_lstate *lstate;
1704 		struct ccb_hdr_slist *list;
1705 		cam_status status;
1706 
1707 		status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1708 					     &lstate, TRUE);
1709 
1710 		if (status != CAM_REQ_CMP) {
1711 			ccb->ccb_h.status = status;
1712 			break;
1713 		}
1714 
1715 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1716 			list = &lstate->accept_tios;
1717 		else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1718 			list = &lstate->immed_notifies;
1719 		else
1720 			list = NULL;
1721 
1722 		if (list != NULL) {
1723 			struct ccb_hdr *curelm;
1724 			int found;
1725 
1726 			curelm = SLIST_FIRST(list);
1727 			found = 0;
1728 			if (curelm == &abort_ccb->ccb_h) {
1729 				found = 1;
1730 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1731 			} else {
1732 				while(curelm != NULL) {
1733 					struct ccb_hdr *nextelm;
1734 
1735 					nextelm =
1736 					    SLIST_NEXT(curelm, sim_links.sle);
1737 
1738 					if (nextelm == &abort_ccb->ccb_h) {
1739 						found = 1;
1740 						SLIST_NEXT(curelm,
1741 							   sim_links.sle) =
1742 						    SLIST_NEXT(nextelm,
1743 							       sim_links.sle);
1744 						break;
1745 					}
1746 					curelm = nextelm;
1747 				}
1748 			}
1749 
1750 			if (found) {
1751 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1752 				xpt_done(abort_ccb);
1753 				ccb->ccb_h.status = CAM_REQ_CMP;
1754 			} else {
1755 				xpt_print_path(abort_ccb->ccb_h.path);
1756 				printf("Not found\n");
1757 				ccb->ccb_h.status = CAM_PATH_INVALID;
1758 			}
1759 			break;
1760 		}
1761 		/* FALLTHROUGH */
1762 	}
1763 	case XPT_SCSI_IO:
1764 		/* XXX Fully implement the hard ones */
1765 		ccb->ccb_h.status = CAM_UA_ABORT;
1766 		break;
1767 	default:
1768 		ccb->ccb_h.status = CAM_REQ_INVALID;
1769 		break;
1770 	}
1771 	xpt_done(ccb);
1772 }
1773 
1774 void
1775 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1776 		u_int lun, ac_code code, void *opt_arg)
1777 {
1778 	struct	ccb_trans_settings cts;
1779 	struct cam_path *path;
1780 	void *arg;
1781 	int error;
1782 
1783 	arg = NULL;
1784 	error = ahc_create_path(ahc, channel, target, lun, &path);
1785 
1786 	if (error != CAM_REQ_CMP)
1787 		return;
1788 
1789 	switch (code) {
1790 	case AC_TRANSFER_NEG:
1791 	{
1792 #ifdef AHC_NEW_TRAN_SETTINGS
1793 		struct	ccb_trans_settings_scsi *scsi;
1794 
1795 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1796 		scsi = &cts.proto_specific.scsi;
1797 #else
1798 		cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1799 #endif
1800 		cts.ccb_h.path = path;
1801 		cts.ccb_h.target_id = target;
1802 		cts.ccb_h.target_lun = lun;
1803 		ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1804 							  : ahc->our_id_b,
1805 				      channel, &cts);
1806 		arg = &cts;
1807 #ifdef AHC_NEW_TRAN_SETTINGS
1808 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1809 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1810 #else
1811 		cts.valid &= ~CCB_TRANS_TQ_VALID;
1812 		cts.flags &= ~CCB_TRANS_TAG_ENB;
1813 #endif
1814 		if (opt_arg == NULL)
1815 			break;
1816 		if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1817 #ifdef AHC_NEW_TRAN_SETTINGS
1818 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1819 		scsi->valid |= CTS_SCSI_VALID_TQ;
1820 #else
1821 			cts.flags |= CCB_TRANS_TAG_ENB;
1822 		cts.valid |= CCB_TRANS_TQ_VALID;
1823 #endif
1824 		break;
1825 	}
1826 	case AC_SENT_BDR:
1827 	case AC_BUS_RESET:
1828 		break;
1829 	default:
1830 		panic("ahc_send_async: Unexpected async event");
1831 	}
1832 	xpt_async(code, path, arg);
1833 	xpt_free_path(path);
1834 }
1835 
1836 void
1837 ahc_platform_set_tags(struct ahc_softc *ahc,
1838 		      struct ahc_devinfo *devinfo, int enable)
1839 {
1840 }
1841 
1842 int
1843 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1844 {
1845 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1846 	    M_NOWAIT | M_ZERO);
1847 	if (ahc->platform_data == NULL)
1848 		return (ENOMEM);
1849 	return (0);
1850 }
1851 
1852 void
1853 ahc_platform_free(struct ahc_softc *ahc)
1854 {
1855 	struct ahc_platform_data *pdata;
1856 
1857 	pdata = ahc->platform_data;
1858 	if (pdata != NULL) {
1859 		if (pdata->regs != NULL)
1860 			bus_release_resource(ahc->dev_softc,
1861 					     pdata->regs_res_type,
1862 					     pdata->regs_res_id,
1863 					     pdata->regs);
1864 
1865 		if (pdata->irq != NULL)
1866 			bus_release_resource(ahc->dev_softc,
1867 					     pdata->irq_res_type,
1868 					     0, pdata->irq);
1869 
1870 		if (pdata->sim_b != NULL) {
1871 			xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1872 			xpt_free_path(pdata->path_b);
1873 			xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1874 			cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1875 		}
1876 		if (pdata->sim != NULL) {
1877 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1878 			xpt_free_path(pdata->path);
1879 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1880 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1881 		}
1882 		if (pdata->eh != NULL)
1883 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1884 		free(ahc->platform_data, M_DEVBUF);
1885 	}
1886 }
1887 
1888 int
1889 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1890 {
1891 	/* We don't sort softcs under FreeBSD so report equal always */
1892 	return (0);
1893 }
1894 
1895 int
1896 ahc_detach(device_t dev)
1897 {
1898 	struct ahc_softc *ahc;
1899 	u_long s;
1900 
1901 	device_printf(dev, "detaching device\n");
1902 	ahc = device_get_softc(dev);
1903 	ahc_lock(ahc, &s);
1904 	bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1905 	ahc_unlock(ahc, &s);
1906 	ahc_free(ahc);
1907 	return (0);
1908 }
1909 
1910 #if UNUSED
1911 static void
1912 ahc_dump_targcmd(struct target_cmd *cmd)
1913 {
1914 	uint8_t *byte;
1915 	uint8_t *last_byte;
1916 	int i;
1917 
1918 	byte = &cmd->initiator_channel;
1919 	/* Debugging info for received commands */
1920 	last_byte = &cmd[1].initiator_channel;
1921 
1922 	i = 0;
1923 	while (byte < last_byte) {
1924 		if (i == 0)
1925 			printf("\t");
1926 		printf("%#x", *byte++);
1927 		i++;
1928 		if (i == 8) {
1929 			printf("\n");
1930 			i = 0;
1931 		} else {
1932 			printf(", ");
1933 		}
1934 	}
1935 }
1936 #endif
1937 
1938 static int
1939 ahc_modevent(module_t mod, int type, void *data)
1940 {
1941 	/* XXX Deal with busy status on unload. */
1942 	return 0;
1943 }
1944 
1945 static moduledata_t ahc_mod = {
1946 	"ahc",
1947 	ahc_modevent,
1948 	NULL
1949 };
1950 
1951 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1952 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1953 MODULE_VERSION(ahc, 1);
1954