xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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 			sg->addr = dm_segs->ds_addr;
1110 /* XXX Add in the 5th byte of the address later. */
1111 			sg->len = dm_segs->ds_len;
1112 			sg++;
1113 			dm_segs++;
1114 		}
1115 
1116 		/*
1117 		 * Note where to find the SG entries in bus space.
1118 		 * We also set the full residual flag which the
1119 		 * sequencer will clear as soon as a data transfer
1120 		 * occurs.
1121 		 */
1122 		scb->hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
1123 
1124 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1125 			op = BUS_DMASYNC_PREREAD;
1126 		else
1127 			op = BUS_DMASYNC_PREWRITE;
1128 
1129 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1130 
1131 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1132 			struct target_data *tdata;
1133 
1134 			tdata = &scb->hscb->shared_data.tdata;
1135 			tdata->target_phases |= DPHASE_PENDING;
1136 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1137 				tdata->data_phase = P_DATAOUT;
1138 			else
1139 				tdata->data_phase = P_DATAIN;
1140 
1141 			/*
1142 			 * If the transfer is of an odd length and in the
1143 			 * "in" direction (scsi->HostBus), then it may
1144 			 * trigger a bug in the 'WideODD' feature of
1145 			 * non-Ultra2 chips.  Force the total data-length
1146 			 * to be even by adding an extra, 1 byte, SG,
1147 			 * element.  We do this even if we are not currently
1148 			 * negotiated wide as negotiation could occur before
1149 			 * this command is executed.
1150 			 */
1151 			if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1152 			 && (ccb->csio.dxfer_len & 0x1) != 0
1153 			 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1154 
1155 				nsegments++;
1156 				if (nsegments > AHC_NSEG) {
1157 
1158 					ahc_set_transaction_status(scb,
1159 					    CAM_REQ_TOO_BIG);
1160 					bus_dmamap_unload(ahc->buffer_dmat,
1161 							  scb->dmamap);
1162 					ahc_lock(ahc, &s);
1163 					ahc_free_scb(ahc, scb);
1164 					ahc_unlock(ahc, &s);
1165 					xpt_done(ccb);
1166 					return;
1167 				}
1168 				sg->addr = ahc->dma_bug_buf;
1169 				sg->len = 1;
1170 				sg++;
1171 			}
1172 		}
1173 		sg--;
1174 		sg->len |= AHC_DMA_LAST_SEG;
1175 
1176 		/* Copy the first SG into the "current" data pointer area */
1177 		scb->hscb->dataptr = scb->sg_list->addr;
1178 		scb->hscb->datacnt = scb->sg_list->len;
1179 	} else {
1180 		scb->hscb->sgptr = SG_LIST_NULL;
1181 		scb->hscb->dataptr = 0;
1182 		scb->hscb->datacnt = 0;
1183 	}
1184 
1185 	scb->sg_count = nsegments;
1186 
1187 	ahc_lock(ahc, &s);
1188 
1189 	/*
1190 	 * Last time we need to check if this SCB needs to
1191 	 * be aborted.
1192 	 */
1193 	if (ahc_get_transaction_status(scb) != CAM_REQ_INPROG) {
1194 		if (nsegments != 0)
1195 			bus_dmamap_unload(ahc->buffer_dmat,
1196 					  scb->dmamap);
1197 		ahc_free_scb(ahc, scb);
1198 		ahc_unlock(ahc, &s);
1199 		xpt_done(ccb);
1200 		return;
1201 	}
1202 
1203 	tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1204 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1205 				    SCSIID_TARGET(ahc, scb->hscb->scsiid),
1206 				    &tstate);
1207 
1208 	mask = SCB_GET_TARGET_MASK(ahc, scb);
1209 	scb->hscb->scsirate = tinfo->scsirate;
1210 	scb->hscb->scsioffset = tinfo->curr.offset;
1211 	if ((tstate->ultraenb & mask) != 0)
1212 		scb->hscb->control |= ULTRAENB;
1213 
1214 	if ((tstate->discenable & mask) != 0
1215 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1216 		scb->hscb->control |= DISCENB;
1217 
1218 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1219 	 && (tinfo->goal.width != 0
1220 	  || tinfo->goal.period != 0
1221 	  || tinfo->goal.ppr_options != 0)) {
1222 		scb->flags |= SCB_NEGOTIATE;
1223 		scb->hscb->control |= MK_MESSAGE;
1224 	} else if ((tstate->auto_negotiate & mask) != 0) {
1225 		scb->flags |= SCB_AUTO_NEGOTIATE;
1226 		scb->hscb->control |= MK_MESSAGE;
1227 	}
1228 
1229 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1230 
1231 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1232 
1233 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1234 		uint64_t time;
1235 
1236 		if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
1237 			ccb->ccb_h.timeout = 5 * 1000;
1238 
1239 		time = ccb->ccb_h.timeout;
1240 		time *= hz;
1241 		time /= 1000;
1242 		ccb->ccb_h.timeout_ch =
1243 		    timeout(ahc_timeout, (caddr_t)scb, time);
1244 	}
1245 
1246 	/*
1247 	 * We only allow one untagged transaction
1248 	 * per target in the initiator role unless
1249 	 * we are storing a full busy target *lun*
1250 	 * table in SCB space.
1251 	 */
1252 	if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1253 	 && (ahc->flags & AHC_SCB_BTT) == 0) {
1254 		struct scb_tailq *untagged_q;
1255 		int target_offset;
1256 
1257 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1258 		untagged_q = &(ahc->untagged_queues[target_offset]);
1259 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1260 		scb->flags |= SCB_UNTAGGEDQ;
1261 		if (TAILQ_FIRST(untagged_q) != scb) {
1262 			ahc_unlock(ahc, &s);
1263 			return;
1264 		}
1265 	}
1266 	scb->flags |= SCB_ACTIVE;
1267 
1268 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1269 		/* Define a mapping from our tag to the SCB. */
1270 		ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1271 		ahc_pause(ahc);
1272 		if ((ahc->flags & AHC_PAGESCBS) == 0)
1273 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1274 		ahc_outb(ahc, SCB_TAG, scb->hscb->tag);
1275 		ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
1276 		ahc_unpause(ahc);
1277 	} else {
1278 		ahc_queue_scb(ahc, scb);
1279 	}
1280 
1281 	ahc_unlock(ahc, &s);
1282 }
1283 
1284 static void
1285 ahc_poll(struct cam_sim *sim)
1286 {
1287 	ahc_intr(cam_sim_softc(sim));
1288 }
1289 
1290 static void
1291 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1292 	       struct ccb_scsiio *csio, struct scb *scb)
1293 {
1294 	struct hardware_scb *hscb;
1295 	struct ccb_hdr *ccb_h;
1296 
1297 	hscb = scb->hscb;
1298 	ccb_h = &csio->ccb_h;
1299 
1300 	if (ccb_h->func_code == XPT_SCSI_IO) {
1301 		hscb->cdb_len = csio->cdb_len;
1302 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1303 
1304 			if (hscb->cdb_len > sizeof(hscb->cdb32)
1305 			 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1306 				u_long s;
1307 
1308 				ahc_set_transaction_status(scb,
1309 							   CAM_REQ_INVALID);
1310 				ahc_lock(ahc, &s);
1311 				ahc_free_scb(ahc, scb);
1312 				ahc_unlock(ahc, &s);
1313 				xpt_done((union ccb *)csio);
1314 				return;
1315 			}
1316 			if (hscb->cdb_len > 12) {
1317 				memcpy(hscb->cdb32,
1318 				       csio->cdb_io.cdb_ptr,
1319 				       hscb->cdb_len);
1320 				scb->flags |= SCB_CDB32_PTR;
1321 			} else {
1322 				memcpy(hscb->shared_data.cdb,
1323 				       csio->cdb_io.cdb_ptr,
1324 				       hscb->cdb_len);
1325 			}
1326 		} else {
1327 			if (hscb->cdb_len > 12) {
1328 				memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1329 				       hscb->cdb_len);
1330 				scb->flags |= SCB_CDB32_PTR;
1331 			} else {
1332 				memcpy(hscb->shared_data.cdb,
1333 				       csio->cdb_io.cdb_bytes,
1334 				       hscb->cdb_len);
1335 			}
1336 		}
1337 	}
1338 
1339 	/* Only use S/G if there is a transfer */
1340 	if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1341 		if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1342 			/* We've been given a pointer to a single buffer */
1343 			if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1344 				int s;
1345 				int error;
1346 
1347 				s = splsoftvm();
1348 				error = bus_dmamap_load(ahc->buffer_dmat,
1349 							scb->dmamap,
1350 							csio->data_ptr,
1351 							csio->dxfer_len,
1352 							ahc_execute_scb,
1353 							scb, /*flags*/0);
1354 				if (error == EINPROGRESS) {
1355 					/*
1356 					 * So as to maintain ordering,
1357 					 * freeze the controller queue
1358 					 * until our mapping is
1359 					 * returned.
1360 					 */
1361 					xpt_freeze_simq(sim,
1362 							/*count*/1);
1363 					scb->io_ctx->ccb_h.status |=
1364 					    CAM_RELEASE_SIMQ;
1365 				}
1366 				splx(s);
1367 			} else {
1368 				struct bus_dma_segment seg;
1369 
1370 				/* Pointer to physical buffer */
1371 				if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1372 					panic("ahc_setup_data - Transfer size "
1373 					      "larger than can device max");
1374 
1375 				seg.ds_addr = (bus_addr_t)csio->data_ptr;
1376 				seg.ds_len = csio->dxfer_len;
1377 				ahc_execute_scb(scb, &seg, 1, 0);
1378 			}
1379 		} else {
1380 			struct bus_dma_segment *segs;
1381 
1382 			if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1383 				panic("ahc_setup_data - Physical segment "
1384 				      "pointers unsupported");
1385 
1386 			if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1387 				panic("ahc_setup_data - Virtual segment "
1388 				      "addresses unsupported");
1389 
1390 			/* Just use the segments provided */
1391 			segs = (struct bus_dma_segment *)csio->data_ptr;
1392 			ahc_execute_scb(scb, segs, csio->sglist_cnt, 0);
1393 		}
1394 	} else {
1395 		ahc_execute_scb(scb, NULL, 0, 0);
1396 	}
1397 }
1398 
1399 static void
1400 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
1401 
1402 	if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
1403 		struct scb *list_scb;
1404 
1405 		scb->flags |= SCB_RECOVERY_SCB;
1406 
1407 		/*
1408 		 * Take all queued, but not sent SCBs out of the equation.
1409 		 * Also ensure that no new CCBs are queued to us while we
1410 		 * try to fix this problem.
1411 		 */
1412 		if ((scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1413 			xpt_freeze_simq(SCB_GET_SIM(ahc, scb), /*count*/1);
1414 			scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1415 		}
1416 
1417 		/*
1418 		 * Go through all of our pending SCBs and remove
1419 		 * any scheduled timeouts for them.  We will reschedule
1420 		 * them after we've successfully fixed this problem.
1421 		 */
1422 		LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
1423 			union ccb *ccb;
1424 
1425 			ccb = list_scb->io_ctx;
1426 			untimeout(ahc_timeout, list_scb, ccb->ccb_h.timeout_ch);
1427 		}
1428 	}
1429 }
1430 
1431 void
1432 ahc_timeout(void *arg)
1433 {
1434 	struct	scb *scb;
1435 	struct	ahc_softc *ahc;
1436 	long	s;
1437 	int	found;
1438 	u_int	last_phase;
1439 	int	target;
1440 	int	lun;
1441 	int	i;
1442 	char	channel;
1443 
1444 	scb = (struct scb *)arg;
1445 	ahc = (struct ahc_softc *)scb->ahc_softc;
1446 
1447 	ahc_lock(ahc, &s);
1448 
1449 	ahc_pause_and_flushwork(ahc);
1450 
1451 	if ((scb->flags & SCB_ACTIVE) == 0) {
1452 		/* Previous timeout took care of me already */
1453 		printf("%s: Timedout SCB already complete. "
1454 		       "Interrupts may not be functioning.\n", ahc_name(ahc));
1455 		ahc_unpause(ahc);
1456 		ahc_unlock(ahc, &s);
1457 		return;
1458 	}
1459 
1460 	target = SCB_GET_TARGET(ahc, scb);
1461 	channel = SCB_GET_CHANNEL(ahc, scb);
1462 	lun = SCB_GET_LUN(scb);
1463 
1464 	ahc_print_path(ahc, scb);
1465 	printf("SCB 0x%x - timed out\n", scb->hscb->tag);
1466 	ahc_dump_card_state(ahc);
1467 	last_phase = ahc_inb(ahc, LASTPHASE);
1468 	if (scb->sg_count > 0) {
1469 		for (i = 0; i < scb->sg_count; i++) {
1470 			printf("sg[%d] - Addr 0x%x : Length %d\n",
1471 			       i,
1472 			       scb->sg_list[i].addr,
1473 			       scb->sg_list[i].len & AHC_SG_LEN_MASK);
1474 		}
1475 	}
1476 	if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
1477 		/*
1478 		 * Been down this road before.
1479 		 * Do a full bus reset.
1480 		 */
1481 bus_reset:
1482 		ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1483 		found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
1484 		printf("%s: Issued Channel %c Bus Reset. "
1485 		       "%d SCBs aborted\n", ahc_name(ahc), channel, found);
1486 	} else {
1487 		/*
1488 		 * If we are a target, transition to bus free and report
1489 		 * the timeout.
1490 		 *
1491 		 * The target/initiator that is holding up the bus may not
1492 		 * be the same as the one that triggered this timeout
1493 		 * (different commands have different timeout lengths).
1494 		 * If the bus is idle and we are actiing as the initiator
1495 		 * for this request, queue a BDR message to the timed out
1496 		 * target.  Otherwise, if the timed out transaction is
1497 		 * active:
1498 		 *   Initiator transaction:
1499 		 *	Stuff the message buffer with a BDR message and assert
1500 		 *	ATN in the hopes that the target will let go of the bus
1501 		 *	and go to the mesgout phase.  If this fails, we'll
1502 		 *	get another timeout 2 seconds later which will attempt
1503 		 *	a bus reset.
1504 		 *
1505 		 *   Target transaction:
1506 		 *	Transition to BUS FREE and report the error.
1507 		 *	It's good to be the target!
1508 		 */
1509 		u_int active_scb_index;
1510 		u_int saved_scbptr;
1511 
1512 		saved_scbptr = ahc_inb(ahc, SCBPTR);
1513 		active_scb_index = ahc_inb(ahc, SCB_TAG);
1514 
1515 		if (last_phase != P_BUSFREE
1516 		  && (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) != 0
1517 		  && (active_scb_index < ahc->scb_data->numscbs)) {
1518 			struct scb *active_scb;
1519 
1520 			/*
1521 			 * If the active SCB is not us, assume that
1522 			 * the active SCB has a longer timeout than
1523 			 * the timedout SCB, and wait for the active
1524 			 * SCB to timeout.
1525 			 */
1526 			active_scb = ahc_lookup_scb(ahc, active_scb_index);
1527 			if (active_scb != scb) {
1528 				struct	 ccb_hdr *ccbh;
1529 				uint64_t newtimeout;
1530 
1531 				ahc_print_path(ahc, scb);
1532 				printf("Other SCB Timeout%s",
1533 			 	       (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
1534 				       ? " again\n" : "\n");
1535 				scb->flags |= SCB_OTHERTCL_TIMEOUT;
1536 				newtimeout =
1537 				    MAX(active_scb->io_ctx->ccb_h.timeout,
1538 					scb->io_ctx->ccb_h.timeout);
1539 				newtimeout *= hz;
1540 				newtimeout /= 1000;
1541 				ccbh = &scb->io_ctx->ccb_h;
1542 				scb->io_ctx->ccb_h.timeout_ch =
1543 				    timeout(ahc_timeout, scb, newtimeout);
1544 				ahc_unpause(ahc);
1545 				ahc_unlock(ahc, &s);
1546 				return;
1547 			}
1548 
1549 			/* It's us */
1550 			if ((scb->hscb->control & TARGET_SCB) != 0) {
1551 
1552 				/*
1553 				 * Send back any queued up transactions
1554 				 * and properly record the error condition.
1555 				 */
1556 				ahc_freeze_devq(ahc, scb);
1557 				ahc_set_transaction_status(scb,
1558 							   CAM_CMD_TIMEOUT);
1559 				ahc_freeze_scb(scb);
1560 				ahc_done(ahc, scb);
1561 
1562 				/* Will clear us from the bus */
1563 				ahc_restart(ahc);
1564 				ahc_unlock(ahc, &s);
1565 				return;
1566 			}
1567 
1568 			ahc_set_recoveryscb(ahc, active_scb);
1569 			ahc_outb(ahc, MSG_OUT, HOST_MSG);
1570 			ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
1571 			ahc_print_path(ahc, active_scb);
1572 			printf("BDR message in message buffer\n");
1573 			active_scb->flags |= SCB_DEVICE_RESET;
1574 			active_scb->io_ctx->ccb_h.timeout_ch =
1575 			    timeout(ahc_timeout, (caddr_t)active_scb, 2 * hz);
1576 			ahc_unpause(ahc);
1577 		} else {
1578 			int	 disconnected;
1579 
1580 			/* XXX Shouldn't panic.  Just punt instead */
1581 			if ((scb->hscb->control & TARGET_SCB) != 0)
1582 				panic("Timed-out target SCB but bus idle");
1583 
1584 			if (last_phase != P_BUSFREE
1585 			 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
1586 				/* XXX What happened to the SCB? */
1587 				/* Hung target selection.  Goto busfree */
1588 				printf("%s: Hung target selection\n",
1589 				       ahc_name(ahc));
1590 				ahc_restart(ahc);
1591 				ahc_unlock(ahc, &s);
1592 				return;
1593 			}
1594 
1595 			if (ahc_search_qinfifo(ahc, target, channel, lun,
1596 					       scb->hscb->tag, ROLE_INITIATOR,
1597 					       /*status*/0, SEARCH_COUNT) > 0) {
1598 				disconnected = FALSE;
1599 			} else {
1600 				disconnected = TRUE;
1601 			}
1602 
1603 			if (disconnected) {
1604 
1605 				ahc_set_recoveryscb(ahc, scb);
1606 				/*
1607 				 * Actually re-queue this SCB in an attempt
1608 				 * to select the device before it reconnects.
1609 				 * In either case (selection or reselection),
1610 				 * we will now issue a target reset to the
1611 				 * timed-out device.
1612 				 *
1613 				 * Set the MK_MESSAGE control bit indicating
1614 				 * that we desire to send a message.  We
1615 				 * also set the disconnected flag since
1616 				 * in the paging case there is no guarantee
1617 				 * that our SCB control byte matches the
1618 				 * version on the card.  We don't want the
1619 				 * sequencer to abort the command thinking
1620 				 * an unsolicited reselection occurred.
1621 				 */
1622 				scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1623 				scb->flags |= SCB_DEVICE_RESET;
1624 
1625 				/*
1626 				 * Remove any cached copy of this SCB in the
1627 				 * disconnected list in preparation for the
1628 				 * queuing of our abort SCB.  We use the
1629 				 * same element in the SCB, SCB_NEXT, for
1630 				 * both the qinfifo and the disconnected list.
1631 				 */
1632 				ahc_search_disc_list(ahc, target, channel,
1633 						     lun, scb->hscb->tag,
1634 						     /*stop_on_first*/TRUE,
1635 						     /*remove*/TRUE,
1636 						     /*save_state*/FALSE);
1637 
1638 				/*
1639 				 * In the non-paging case, the sequencer will
1640 				 * never re-reference the in-core SCB.
1641 				 * To make sure we are notified during
1642 				 * reslection, set the MK_MESSAGE flag in
1643 				 * the card's copy of the SCB.
1644 				 */
1645 				if ((ahc->flags & AHC_PAGESCBS) == 0) {
1646 					ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1647 					ahc_outb(ahc, SCB_CONTROL,
1648 						 ahc_inb(ahc, SCB_CONTROL)
1649 						| MK_MESSAGE);
1650 				}
1651 
1652 				/*
1653 				 * Clear out any entries in the QINFIFO first
1654 				 * so we are the next SCB for this target
1655 				 * to run.
1656 				 */
1657 				ahc_search_qinfifo(ahc,
1658 						   SCB_GET_TARGET(ahc, scb),
1659 						   channel, SCB_GET_LUN(scb),
1660 						   SCB_LIST_NULL,
1661 						   ROLE_INITIATOR,
1662 						   CAM_REQUEUE_REQ,
1663 						   SEARCH_COMPLETE);
1664 				ahc_print_path(ahc, scb);
1665 				printf("Queuing a BDR SCB\n");
1666 				ahc_qinfifo_requeue_tail(ahc, scb);
1667 				ahc_outb(ahc, SCBPTR, saved_scbptr);
1668 				scb->io_ctx->ccb_h.timeout_ch =
1669 				    timeout(ahc_timeout, (caddr_t)scb, 2 * hz);
1670 				ahc_unpause(ahc);
1671 			} else {
1672 				/* Go "immediatly" to the bus reset */
1673 				/* This shouldn't happen */
1674 				ahc_set_recoveryscb(ahc, scb);
1675 				ahc_print_path(ahc, scb);
1676 				printf("SCB %d: Immediate reset.  "
1677 					"Flags = 0x%x\n", scb->hscb->tag,
1678 					scb->flags);
1679 				goto bus_reset;
1680 			}
1681 		}
1682 	}
1683 	ahc_unlock(ahc, &s);
1684 }
1685 
1686 static void
1687 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1688 {
1689 	union ccb *abort_ccb;
1690 
1691 	abort_ccb = ccb->cab.abort_ccb;
1692 	switch (abort_ccb->ccb_h.func_code) {
1693 	case XPT_ACCEPT_TARGET_IO:
1694 	case XPT_IMMED_NOTIFY:
1695 	case XPT_CONT_TARGET_IO:
1696 	{
1697 		struct ahc_tmode_tstate *tstate;
1698 		struct ahc_tmode_lstate *lstate;
1699 		struct ccb_hdr_slist *list;
1700 		cam_status status;
1701 
1702 		status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1703 					     &lstate, TRUE);
1704 
1705 		if (status != CAM_REQ_CMP) {
1706 			ccb->ccb_h.status = status;
1707 			break;
1708 		}
1709 
1710 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1711 			list = &lstate->accept_tios;
1712 		else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1713 			list = &lstate->immed_notifies;
1714 		else
1715 			list = NULL;
1716 
1717 		if (list != NULL) {
1718 			struct ccb_hdr *curelm;
1719 			int found;
1720 
1721 			curelm = SLIST_FIRST(list);
1722 			found = 0;
1723 			if (curelm == &abort_ccb->ccb_h) {
1724 				found = 1;
1725 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1726 			} else {
1727 				while(curelm != NULL) {
1728 					struct ccb_hdr *nextelm;
1729 
1730 					nextelm =
1731 					    SLIST_NEXT(curelm, sim_links.sle);
1732 
1733 					if (nextelm == &abort_ccb->ccb_h) {
1734 						found = 1;
1735 						SLIST_NEXT(curelm,
1736 							   sim_links.sle) =
1737 						    SLIST_NEXT(nextelm,
1738 							       sim_links.sle);
1739 						break;
1740 					}
1741 					curelm = nextelm;
1742 				}
1743 			}
1744 
1745 			if (found) {
1746 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1747 				xpt_done(abort_ccb);
1748 				ccb->ccb_h.status = CAM_REQ_CMP;
1749 			} else {
1750 				xpt_print_path(abort_ccb->ccb_h.path);
1751 				printf("Not found\n");
1752 				ccb->ccb_h.status = CAM_PATH_INVALID;
1753 			}
1754 			break;
1755 		}
1756 		/* FALLTHROUGH */
1757 	}
1758 	case XPT_SCSI_IO:
1759 		/* XXX Fully implement the hard ones */
1760 		ccb->ccb_h.status = CAM_UA_ABORT;
1761 		break;
1762 	default:
1763 		ccb->ccb_h.status = CAM_REQ_INVALID;
1764 		break;
1765 	}
1766 	xpt_done(ccb);
1767 }
1768 
1769 void
1770 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1771 		u_int lun, ac_code code, void *opt_arg)
1772 {
1773 	struct	ccb_trans_settings cts;
1774 	struct cam_path *path;
1775 	void *arg;
1776 	int error;
1777 
1778 	arg = NULL;
1779 	error = ahc_create_path(ahc, channel, target, lun, &path);
1780 
1781 	if (error != CAM_REQ_CMP)
1782 		return;
1783 
1784 	switch (code) {
1785 	case AC_TRANSFER_NEG:
1786 	{
1787 #ifdef AHC_NEW_TRAN_SETTINGS
1788 		struct	ccb_trans_settings_scsi *scsi;
1789 
1790 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1791 		scsi = &cts.proto_specific.scsi;
1792 #else
1793 		cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1794 #endif
1795 		cts.ccb_h.path = path;
1796 		cts.ccb_h.target_id = target;
1797 		cts.ccb_h.target_lun = lun;
1798 		ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1799 							  : ahc->our_id_b,
1800 				      channel, &cts);
1801 		arg = &cts;
1802 #ifdef AHC_NEW_TRAN_SETTINGS
1803 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1804 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1805 #else
1806 		cts.valid &= ~CCB_TRANS_TQ_VALID;
1807 		cts.flags &= ~CCB_TRANS_TAG_ENB;
1808 #endif
1809 		if (opt_arg == NULL)
1810 			break;
1811 		if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1812 #ifdef AHC_NEW_TRAN_SETTINGS
1813 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1814 		scsi->valid |= CTS_SCSI_VALID_TQ;
1815 #else
1816 			cts.flags |= CCB_TRANS_TAG_ENB;
1817 		cts.valid |= CCB_TRANS_TQ_VALID;
1818 #endif
1819 		break;
1820 	}
1821 	case AC_SENT_BDR:
1822 	case AC_BUS_RESET:
1823 		break;
1824 	default:
1825 		panic("ahc_send_async: Unexpected async event");
1826 	}
1827 	xpt_async(code, path, arg);
1828 	xpt_free_path(path);
1829 }
1830 
1831 void
1832 ahc_platform_set_tags(struct ahc_softc *ahc,
1833 		      struct ahc_devinfo *devinfo, int enable)
1834 {
1835 }
1836 
1837 int
1838 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1839 {
1840 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1841 	    M_NOWAIT | M_ZERO);
1842 	if (ahc->platform_data == NULL)
1843 		return (ENOMEM);
1844 	return (0);
1845 }
1846 
1847 void
1848 ahc_platform_free(struct ahc_softc *ahc)
1849 {
1850 	struct ahc_platform_data *pdata;
1851 
1852 	pdata = ahc->platform_data;
1853 	if (pdata != NULL) {
1854 		if (pdata->regs != NULL)
1855 			bus_release_resource(ahc->dev_softc,
1856 					     pdata->regs_res_type,
1857 					     pdata->regs_res_id,
1858 					     pdata->regs);
1859 
1860 		if (pdata->irq != NULL)
1861 			bus_release_resource(ahc->dev_softc,
1862 					     pdata->irq_res_type,
1863 					     0, pdata->irq);
1864 
1865 		if (pdata->sim_b != NULL) {
1866 			xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1867 			xpt_free_path(pdata->path_b);
1868 			xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1869 			cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1870 		}
1871 		if (pdata->sim != NULL) {
1872 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1873 			xpt_free_path(pdata->path);
1874 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1875 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1876 		}
1877 		if (pdata->eh != NULL)
1878 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1879 		free(ahc->platform_data, M_DEVBUF);
1880 	}
1881 }
1882 
1883 int
1884 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1885 {
1886 	/* We don't sort softcs under FreeBSD so report equal always */
1887 	return (0);
1888 }
1889 
1890 int
1891 ahc_detach(device_t dev)
1892 {
1893 	struct ahc_softc *ahc;
1894 	u_long s;
1895 
1896 	device_printf(dev, "detaching device\n");
1897 	ahc = device_get_softc(dev);
1898 	ahc_lock(ahc, &s);
1899 	bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1900 	ahc_unlock(ahc, &s);
1901 	ahc_free(ahc);
1902 	return (0);
1903 }
1904 
1905 #if UNUSED
1906 static void
1907 ahc_dump_targcmd(struct target_cmd *cmd)
1908 {
1909 	uint8_t *byte;
1910 	uint8_t *last_byte;
1911 	int i;
1912 
1913 	byte = &cmd->initiator_channel;
1914 	/* Debugging info for received commands */
1915 	last_byte = &cmd[1].initiator_channel;
1916 
1917 	i = 0;
1918 	while (byte < last_byte) {
1919 		if (i == 0)
1920 			printf("\t");
1921 		printf("%#x", *byte++);
1922 		i++;
1923 		if (i == 8) {
1924 			printf("\n");
1925 			i = 0;
1926 		} else {
1927 			printf(", ");
1928 		}
1929 	}
1930 }
1931 #endif
1932 
1933 static int
1934 ahc_modevent(module_t mod, int type, void *data)
1935 {
1936 	/* XXX Deal with busy status on unload. */
1937 	return 0;
1938 }
1939 
1940 static moduledata_t ahc_mod = {
1941 	"ahc",
1942 	ahc_modevent,
1943 	NULL
1944 };
1945 
1946 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1947 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1948 MODULE_VERSION(ahc, 1);
1949