xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.c#20 $
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <dev/aic7xxx/aic7xxx_osm.h>
38 #include <dev/aic7xxx/aic7xxx_inline.h>
39 
40 #include <sys/kthread.h>
41 
42 #ifndef AHC_TMODE_ENABLE
43 #define AHC_TMODE_ENABLE 0
44 #endif
45 
46 #include <dev/aic7xxx/aic_osm_lib.c>
47 
48 #define ccb_scb_ptr spriv_ptr0
49 
50 devclass_t ahc_devclass;
51 
52 #if UNUSED
53 static void	ahc_dump_targcmd(struct target_cmd *cmd);
54 #endif
55 static int	ahc_modevent(module_t mod, int type, void *data);
56 static void	ahc_action(struct cam_sim *sim, union ccb *ccb);
57 static void	ahc_get_tran_settings(struct ahc_softc *ahc,
58 				      int our_id, char channel,
59 				      struct ccb_trans_settings *cts);
60 static void	ahc_async(void *callback_arg, uint32_t code,
61 			  struct cam_path *path, void *arg);
62 static void	ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
63 				int nsegments, int error);
64 static void	ahc_poll(struct cam_sim *sim);
65 static void	ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
66 			       struct ccb_scsiio *csio, struct scb *scb);
67 static void	ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
68 			      union ccb *ccb);
69 static int	ahc_create_path(struct ahc_softc *ahc,
70 				char channel, u_int target, u_int lun,
71 				struct cam_path **path);
72 
73 
74 static int
75 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
76 	        u_int lun, struct cam_path **path)
77 {
78 	path_id_t path_id;
79 
80 	if (channel == 'B')
81 		path_id = cam_sim_path(ahc->platform_data->sim_b);
82 	else
83 		path_id = cam_sim_path(ahc->platform_data->sim);
84 
85 	return (xpt_create_path(path, /*periph*/NULL,
86 				path_id, target, lun));
87 }
88 
89 int
90 ahc_map_int(struct ahc_softc *ahc)
91 {
92 	int error;
93 
94 	/* Hook up our interrupt handler */
95 	error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
96 			       INTR_TYPE_CAM, ahc_platform_intr, ahc,
97 			       &ahc->platform_data->ih);
98 
99 	if (error != 0)
100 		device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
101 			      error);
102 	return (error);
103 }
104 
105 /*
106  * Attach all the sub-devices we can find
107  */
108 int
109 ahc_attach(struct ahc_softc *ahc)
110 {
111 	char   ahc_info[256];
112 	struct ccb_setasync csa;
113 	struct cam_devq *devq;
114 	int bus_id;
115 	int bus_id2;
116 	struct cam_sim *sim;
117 	struct cam_sim *sim2;
118 	struct cam_path *path;
119 	struct cam_path *path2;
120 	long s;
121 	int count;
122 
123 	count = 0;
124 	sim = NULL;
125 	sim2 = NULL;
126 
127 	/*
128 	 * Create a thread to perform all recovery.
129 	 */
130 	if (ahc_spawn_recovery_thread(ahc) != 0)
131 		goto fail;
132 
133 	ahc_controller_info(ahc, ahc_info);
134 	printf("%s\n", ahc_info);
135 	ahc_lock(ahc, &s);
136 
137 	/*
138 	 * Attach secondary channel first if the user has
139 	 * declared it the primary channel.
140 	 */
141 	if ((ahc->features & AHC_TWIN) != 0
142 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
143 		bus_id = 1;
144 		bus_id2 = 0;
145 	} else {
146 		bus_id = 0;
147 		bus_id2 = 1;
148 	}
149 
150 	/*
151 	 * Create the device queue for our SIM(s).
152 	 */
153 	devq = cam_simq_alloc(AHC_MAX_QUEUE);
154 	if (devq == NULL)
155 		goto fail;
156 
157 	/*
158 	 * Construct our first channel SIM entry
159 	 */
160 	sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
161 			    device_get_unit(ahc->dev_softc),
162 			    1, AHC_MAX_QUEUE, devq);
163 	if (sim == NULL) {
164 		cam_simq_free(devq);
165 		goto fail;
166 	}
167 
168 	if (xpt_bus_register(sim, bus_id) != CAM_SUCCESS) {
169 		cam_sim_free(sim, /*free_devq*/TRUE);
170 		sim = NULL;
171 		goto fail;
172 	}
173 
174 	if (xpt_create_path(&path, /*periph*/NULL,
175 			    cam_sim_path(sim), CAM_TARGET_WILDCARD,
176 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
177 		xpt_bus_deregister(cam_sim_path(sim));
178 		cam_sim_free(sim, /*free_devq*/TRUE);
179 		sim = NULL;
180 		goto fail;
181 	}
182 
183 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
184 	csa.ccb_h.func_code = XPT_SASYNC_CB;
185 	csa.event_enable = AC_LOST_DEVICE;
186 	csa.callback = ahc_async;
187 	csa.callback_arg = sim;
188 	xpt_action((union ccb *)&csa);
189 	count++;
190 
191 	if (ahc->features & AHC_TWIN) {
192 		sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
193 				    ahc, device_get_unit(ahc->dev_softc), 1,
194 				    AHC_MAX_QUEUE, devq);
195 
196 		if (sim2 == NULL) {
197 			printf("ahc_attach: Unable to attach second "
198 			       "bus due to resource shortage");
199 			goto fail;
200 		}
201 
202 		if (xpt_bus_register(sim2, bus_id2) != CAM_SUCCESS) {
203 			printf("ahc_attach: Unable to attach second "
204 			       "bus due to resource shortage");
205 			/*
206 			 * We do not want to destroy the device queue
207 			 * because the first bus is using it.
208 			 */
209 			cam_sim_free(sim2, /*free_devq*/FALSE);
210 			goto fail;
211 		}
212 
213 		if (xpt_create_path(&path2, /*periph*/NULL,
214 				    cam_sim_path(sim2),
215 				    CAM_TARGET_WILDCARD,
216 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
217 			xpt_bus_deregister(cam_sim_path(sim2));
218 			cam_sim_free(sim2, /*free_devq*/FALSE);
219 			sim2 = NULL;
220 			goto fail;
221 		}
222 		xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
223 		csa.ccb_h.func_code = XPT_SASYNC_CB;
224 		csa.event_enable = AC_LOST_DEVICE;
225 		csa.callback = ahc_async;
226 		csa.callback_arg = sim2;
227 		xpt_action((union ccb *)&csa);
228 		count++;
229 	}
230 
231 fail:
232 	if ((ahc->features & AHC_TWIN) != 0
233 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
234 		ahc->platform_data->sim_b = sim;
235 		ahc->platform_data->path_b = path;
236 		ahc->platform_data->sim = sim2;
237 		ahc->platform_data->path = path2;
238 	} else {
239 		ahc->platform_data->sim = sim;
240 		ahc->platform_data->path = path;
241 		ahc->platform_data->sim_b = sim2;
242 		ahc->platform_data->path_b = path2;
243 	}
244 
245 	if (count != 0) {
246 		/* We have to wait until after any system dumps... */
247 		ahc->platform_data->eh =
248 		    EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
249 					  ahc, SHUTDOWN_PRI_DEFAULT);
250 		ahc_intr_enable(ahc, TRUE);
251 	}
252 
253 	ahc_unlock(ahc, &s);
254 	return (count);
255 }
256 
257 /*
258  * Catch an interrupt from the adapter
259  */
260 void
261 ahc_platform_intr(void *arg)
262 {
263 	struct	ahc_softc *ahc;
264 
265 	ahc = (struct ahc_softc *)arg;
266 	ahc_intr(ahc);
267 }
268 
269 /*
270  * We have an scb which has been processed by the
271  * adaptor, now we look to see how the operation
272  * went.
273  */
274 void
275 ahc_done(struct ahc_softc *ahc, struct scb *scb)
276 {
277 	union ccb *ccb;
278 
279 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
280 		  ("ahc_done - scb %d\n", scb->hscb->tag));
281 
282 	ccb = scb->io_ctx;
283 	LIST_REMOVE(scb, pending_links);
284 	if ((scb->flags & SCB_TIMEDOUT) != 0)
285 		LIST_REMOVE(scb, timedout_links);
286 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
287 		struct scb_tailq *untagged_q;
288 		int target_offset;
289 
290 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
291 		untagged_q = &ahc->untagged_queues[target_offset];
292 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
293 		scb->flags &= ~SCB_UNTAGGEDQ;
294 		ahc_run_untagged_queue(ahc, untagged_q);
295 	}
296 
297 	untimeout(ahc_platform_timeout, (caddr_t)scb, ccb->ccb_h.timeout_ch);
298 
299 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
300 		bus_dmasync_op_t op;
301 
302 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
303 			op = BUS_DMASYNC_POSTREAD;
304 		else
305 			op = BUS_DMASYNC_POSTWRITE;
306 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
307 		bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
308 	}
309 
310 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
311 		struct cam_path *ccb_path;
312 
313 		/*
314 		 * If we have finally disconnected, clean up our
315 		 * pending device state.
316 		 * XXX - There may be error states that cause where
317 		 *       we will remain connected.
318 		 */
319 		ccb_path = ccb->ccb_h.path;
320 		if (ahc->pending_device != NULL
321 		 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
322 
323 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
324 				ahc->pending_device = NULL;
325 			} else {
326 				if (bootverbose) {
327 					xpt_print_path(ccb->ccb_h.path);
328 					printf("Still connected\n");
329 				}
330 				aic_freeze_ccb(ccb);
331 			}
332 		}
333 
334 		if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
335 			ccb->ccb_h.status |= CAM_REQ_CMP;
336 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
337 		ahc_free_scb(ahc, scb);
338 		xpt_done(ccb);
339 		return;
340 	}
341 
342 	/*
343 	 * If the recovery SCB completes, we have to be
344 	 * out of our timeout.
345 	 */
346 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
347 		struct	scb *list_scb;
348 
349 		/*
350 		 * We were able to complete the command successfully,
351 		 * so renew the timeouts for all other pending
352 		 * commands.
353 		 */
354 		LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
355 
356 			aic_scb_timer_reset(scb, aic_get_timeout(scb));
357 		}
358 
359 		if (aic_get_transaction_status(scb) == CAM_BDR_SENT
360 		 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
361 			aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
362 		ahc_print_path(ahc, scb);
363 		printf("no longer in timeout, status = %x\n",
364 		       ccb->ccb_h.status);
365 	}
366 
367 	/* Don't clobber any existing error state */
368 	if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
369 		ccb->ccb_h.status |= CAM_REQ_CMP;
370 	} else if ((scb->flags & SCB_SENSE) != 0) {
371 		/*
372 		 * We performed autosense retrieval.
373 		 *
374 		 * Zero any sense not transferred by the
375 		 * device.  The SCSI spec mandates that any
376 		 * untransfered data should be assumed to be
377 		 * zero.  Complete the 'bounce' of sense information
378 		 * through buffers accessible via bus-space by
379 		 * copying it into the clients csio.
380 		 */
381 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
382 		memcpy(&ccb->csio.sense_data,
383 		       ahc_get_sense_buf(ahc, scb),
384 		       (aic_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
385 		       - ccb->csio.sense_resid);
386 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
387 	}
388 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
389 	ahc_free_scb(ahc, scb);
390 	xpt_done(ccb);
391 }
392 
393 static void
394 ahc_action(struct cam_sim *sim, union ccb *ccb)
395 {
396 	struct	ahc_softc *ahc;
397 	struct	ahc_tmode_lstate *lstate;
398 	u_int	target_id;
399 	u_int	our_id;
400 	long	s;
401 
402 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
403 
404 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
405 
406 	target_id = ccb->ccb_h.target_id;
407 	our_id = SIM_SCSI_ID(ahc, sim);
408 
409 	switch (ccb->ccb_h.func_code) {
410 	/* Common cases first */
411 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
412 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
413 	{
414 		struct	   ahc_tmode_tstate *tstate;
415 		cam_status status;
416 
417 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
418 					     &lstate, TRUE);
419 
420 		if (status != CAM_REQ_CMP) {
421 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
422 				/* Response from the black hole device */
423 				tstate = NULL;
424 				lstate = ahc->black_hole;
425 			} else {
426 				ccb->ccb_h.status = status;
427 				xpt_done(ccb);
428 				break;
429 			}
430 		}
431 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
432 
433 			ahc_lock(ahc, &s);
434 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
435 					  sim_links.sle);
436 			ccb->ccb_h.status = CAM_REQ_INPROG;
437 			if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
438 				ahc_run_tqinfifo(ahc, /*paused*/FALSE);
439 			ahc_unlock(ahc, &s);
440 			break;
441 		}
442 
443 		/*
444 		 * The target_id represents the target we attempt to
445 		 * select.  In target mode, this is the initiator of
446 		 * the original command.
447 		 */
448 		our_id = target_id;
449 		target_id = ccb->csio.init_id;
450 		/* FALLTHROUGH */
451 	}
452 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
453 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
454 	{
455 		struct	scb *scb;
456 		struct	hardware_scb *hscb;
457 
458 		if ((ahc->flags & AHC_INITIATORROLE) == 0
459 		 && (ccb->ccb_h.func_code == XPT_SCSI_IO
460 		  || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
461 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
462 			xpt_done(ccb);
463 			return;
464 		}
465 
466 		/*
467 		 * get an scb to use.
468 		 */
469 		ahc_lock(ahc, &s);
470 		if ((scb = ahc_get_scb(ahc)) == NULL) {
471 
472 			xpt_freeze_simq(sim, /*count*/1);
473 			ahc->flags |= AHC_RESOURCE_SHORTAGE;
474 			ahc_unlock(ahc, &s);
475 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
476 			xpt_done(ccb);
477 			return;
478 		}
479 		ahc_unlock(ahc, &s);
480 
481 		hscb = scb->hscb;
482 
483 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
484 			  ("start scb(%p)\n", scb));
485 		scb->io_ctx = ccb;
486 		/*
487 		 * So we can find the SCB when an abort is requested
488 		 */
489 		ccb->ccb_h.ccb_scb_ptr = scb;
490 
491 		/*
492 		 * Put all the arguments for the xfer in the scb
493 		 */
494 		hscb->control = 0;
495 		hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
496 		hscb->lun = ccb->ccb_h.target_lun;
497 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
498 			hscb->cdb_len = 0;
499 			scb->flags |= SCB_DEVICE_RESET;
500 			hscb->control |= MK_MESSAGE;
501 			ahc_execute_scb(scb, NULL, 0, 0);
502 		} else {
503 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
504 				struct target_data *tdata;
505 
506 				tdata = &hscb->shared_data.tdata;
507 				if (ahc->pending_device == lstate)
508 					scb->flags |= SCB_TARGET_IMMEDIATE;
509 				hscb->control |= TARGET_SCB;
510 				scb->flags |= SCB_TARGET_SCB;
511 				tdata->target_phases = 0;
512 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
513 					tdata->target_phases |= SPHASE_PENDING;
514 					tdata->scsi_status =
515 					    ccb->csio.scsi_status;
516 				}
517 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
518 					tdata->target_phases |= NO_DISCONNECT;
519 
520 				tdata->initiator_tag = ccb->csio.tag_id;
521 			}
522 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
523 				hscb->control |= ccb->csio.tag_action;
524 
525 			ahc_setup_data(ahc, sim, &ccb->csio, scb);
526 		}
527 		break;
528 	}
529 	case XPT_NOTIFY_ACK:
530 	case XPT_IMMED_NOTIFY:
531 	{
532 		struct	   ahc_tmode_tstate *tstate;
533 		struct	   ahc_tmode_lstate *lstate;
534 		cam_status status;
535 
536 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
537 					     &lstate, TRUE);
538 
539 		if (status != CAM_REQ_CMP) {
540 			ccb->ccb_h.status = status;
541 			xpt_done(ccb);
542 			break;
543 		}
544 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
545 				  sim_links.sle);
546 		ccb->ccb_h.status = CAM_REQ_INPROG;
547 		ahc_send_lstate_events(ahc, lstate);
548 		break;
549 	}
550 	case XPT_EN_LUN:		/* Enable LUN as a target */
551 		ahc_handle_en_lun(ahc, sim, ccb);
552 		xpt_done(ccb);
553 		break;
554 	case XPT_ABORT:			/* Abort the specified CCB */
555 	{
556 		ahc_abort_ccb(ahc, sim, ccb);
557 		break;
558 	}
559 	case XPT_SET_TRAN_SETTINGS:
560 	{
561 #ifdef AHC_NEW_TRAN_SETTINGS
562 		struct	ahc_devinfo devinfo;
563 		struct	ccb_trans_settings *cts;
564 		struct	ccb_trans_settings_scsi *scsi;
565 		struct	ccb_trans_settings_spi *spi;
566 		struct	ahc_initiator_tinfo *tinfo;
567 		struct	ahc_tmode_tstate *tstate;
568 		uint16_t *discenable;
569 		uint16_t *tagenable;
570 		u_int	update_type;
571 
572 		cts = &ccb->cts;
573 		scsi = &cts->proto_specific.scsi;
574 		spi = &cts->xport_specific.spi;
575 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
576 				    cts->ccb_h.target_id,
577 				    cts->ccb_h.target_lun,
578 				    SIM_CHANNEL(ahc, sim),
579 				    ROLE_UNKNOWN);
580 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
581 					    devinfo.our_scsiid,
582 					    devinfo.target, &tstate);
583 		update_type = 0;
584 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
585 			update_type |= AHC_TRANS_GOAL;
586 			discenable = &tstate->discenable;
587 			tagenable = &tstate->tagenable;
588 			tinfo->curr.protocol_version =
589 			    cts->protocol_version;
590 			tinfo->curr.transport_version =
591 			    cts->transport_version;
592 			tinfo->goal.protocol_version =
593 			    cts->protocol_version;
594 			tinfo->goal.transport_version =
595 			    cts->transport_version;
596 		} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
597 			update_type |= AHC_TRANS_USER;
598 			discenable = &ahc->user_discenable;
599 			tagenable = &ahc->user_tagenable;
600 			tinfo->user.protocol_version =
601 			    cts->protocol_version;
602 			tinfo->user.transport_version =
603 			    cts->transport_version;
604 		} else {
605 			ccb->ccb_h.status = CAM_REQ_INVALID;
606 			xpt_done(ccb);
607 			break;
608 		}
609 
610 		ahc_lock(ahc, &s);
611 
612 		if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
613 			if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
614 				*discenable |= devinfo.target_mask;
615 			else
616 				*discenable &= ~devinfo.target_mask;
617 		}
618 
619 		if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
620 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
621 				*tagenable |= devinfo.target_mask;
622 			else
623 				*tagenable &= ~devinfo.target_mask;
624 		}
625 
626 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
627 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
628 					   &spi->bus_width, ROLE_UNKNOWN);
629 			ahc_set_width(ahc, &devinfo, spi->bus_width,
630 				      update_type, /*paused*/FALSE);
631 		}
632 
633 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
634 			if (update_type == AHC_TRANS_USER)
635 				spi->ppr_options = tinfo->user.ppr_options;
636 			else
637 				spi->ppr_options = tinfo->goal.ppr_options;
638 		}
639 
640 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
641 			if (update_type == AHC_TRANS_USER)
642 				spi->sync_offset = tinfo->user.offset;
643 			else
644 				spi->sync_offset = tinfo->goal.offset;
645 		}
646 
647 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
648 			if (update_type == AHC_TRANS_USER)
649 				spi->sync_period = tinfo->user.period;
650 			else
651 				spi->sync_period = tinfo->goal.period;
652 		}
653 
654 		if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
655 		 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
656 			struct ahc_syncrate *syncrate;
657 			u_int maxsync;
658 
659 			if ((ahc->features & AHC_ULTRA2) != 0)
660 				maxsync = AHC_SYNCRATE_DT;
661 			else if ((ahc->features & AHC_ULTRA) != 0)
662 				maxsync = AHC_SYNCRATE_ULTRA;
663 			else
664 				maxsync = AHC_SYNCRATE_FAST;
665 
666 			if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
667 				spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
668 
669 			syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
670 						     &spi->ppr_options,
671 						     maxsync);
672 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
673 					    syncrate, &spi->sync_offset,
674 					    spi->bus_width, ROLE_UNKNOWN);
675 
676 			/* We use a period of 0 to represent async */
677 			if (spi->sync_offset == 0) {
678 				spi->sync_period = 0;
679 				spi->ppr_options = 0;
680 			}
681 
682 			ahc_set_syncrate(ahc, &devinfo, syncrate,
683 					 spi->sync_period, spi->sync_offset,
684 					 spi->ppr_options, update_type,
685 					 /*paused*/FALSE);
686 		}
687 		ahc_unlock(ahc, &s);
688 		ccb->ccb_h.status = CAM_REQ_CMP;
689 		xpt_done(ccb);
690 #else
691 		struct	  ahc_devinfo devinfo;
692 		struct	  ccb_trans_settings *cts;
693 		struct	  ahc_initiator_tinfo *tinfo;
694 		struct	  ahc_tmode_tstate *tstate;
695 		uint16_t *discenable;
696 		uint16_t *tagenable;
697 		u_int	  update_type;
698 		long	  s;
699 
700 		cts = &ccb->cts;
701 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
702 				    cts->ccb_h.target_id,
703 				    cts->ccb_h.target_lun,
704 				    SIM_CHANNEL(ahc, sim),
705 				    ROLE_UNKNOWN);
706 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
707 					    devinfo.our_scsiid,
708 					    devinfo.target, &tstate);
709 		update_type = 0;
710 		if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
711 			update_type |= AHC_TRANS_GOAL;
712 			discenable = &tstate->discenable;
713 			tagenable = &tstate->tagenable;
714 		} else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
715 			update_type |= AHC_TRANS_USER;
716 			discenable = &ahc->user_discenable;
717 			tagenable = &ahc->user_tagenable;
718 		} else {
719 			ccb->ccb_h.status = CAM_REQ_INVALID;
720 			xpt_done(ccb);
721 			break;
722 		}
723 
724 		ahc_lock(ahc, &s);
725 
726 		if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
727 			if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
728 				*discenable |= devinfo.target_mask;
729 			else
730 				*discenable &= ~devinfo.target_mask;
731 		}
732 
733 		if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
734 			if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
735 				*tagenable |= devinfo.target_mask;
736 			else
737 				*tagenable &= ~devinfo.target_mask;
738 		}
739 
740 		if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
741 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
742 					   &cts->bus_width, ROLE_UNKNOWN);
743 			ahc_set_width(ahc, &devinfo, cts->bus_width,
744 				      update_type, /*paused*/FALSE);
745 		}
746 
747 		if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
748 			if (update_type == AHC_TRANS_USER)
749 				cts->sync_offset = tinfo->user.offset;
750 			else
751 				cts->sync_offset = tinfo->goal.offset;
752 		}
753 
754 		if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
755 			if (update_type == AHC_TRANS_USER)
756 				cts->sync_period = tinfo->user.period;
757 			else
758 				cts->sync_period = tinfo->goal.period;
759 		}
760 
761 		if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
762 		 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)) {
763 			struct ahc_syncrate *syncrate;
764 			u_int ppr_options;
765 			u_int maxsync;
766 
767 			if ((ahc->features & AHC_ULTRA2) != 0)
768 				maxsync = AHC_SYNCRATE_DT;
769 			else if ((ahc->features & AHC_ULTRA) != 0)
770 				maxsync = AHC_SYNCRATE_ULTRA;
771 			else
772 				maxsync = AHC_SYNCRATE_FAST;
773 
774 			ppr_options = 0;
775 			if (cts->sync_period <= 9
776 			 && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
777 				ppr_options = MSG_EXT_PPR_DT_REQ;
778 
779 			syncrate = ahc_find_syncrate(ahc, &cts->sync_period,
780 						     &ppr_options,
781 						     maxsync);
782 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
783 					    syncrate, &cts->sync_offset,
784 					    MSG_EXT_WDTR_BUS_8_BIT,
785 					    ROLE_UNKNOWN);
786 
787 			/* We use a period of 0 to represent async */
788 			if (cts->sync_offset == 0) {
789 				cts->sync_period = 0;
790 				ppr_options = 0;
791 			}
792 
793 			if (ppr_options == MSG_EXT_PPR_DT_REQ
794 			 && tinfo->user.transport_version >= 3) {
795 				tinfo->goal.transport_version =
796 				    tinfo->user.transport_version;
797 				tinfo->curr.transport_version =
798 				    tinfo->user.transport_version;
799 			}
800 
801 			ahc_set_syncrate(ahc, &devinfo, syncrate,
802 					 cts->sync_period, cts->sync_offset,
803 					 ppr_options, update_type,
804 					 /*paused*/FALSE);
805 		}
806 		ahc_unlock(ahc, &s);
807 		ccb->ccb_h.status = CAM_REQ_CMP;
808 		xpt_done(ccb);
809 #endif
810 		break;
811 	}
812 	case XPT_GET_TRAN_SETTINGS:
813 	/* Get default/user set transfer settings for the target */
814 	{
815 
816 		ahc_lock(ahc, &s);
817 		ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
818 				      SIM_CHANNEL(ahc, sim), &ccb->cts);
819 		ahc_unlock(ahc, &s);
820 		xpt_done(ccb);
821 		break;
822 	}
823 	case XPT_CALC_GEOMETRY:
824 	{
825 		int extended;
826 
827 		extended = SIM_IS_SCSIBUS_B(ahc, sim)
828 			 ? ahc->flags & AHC_EXTENDED_TRANS_B
829 			 : ahc->flags & AHC_EXTENDED_TRANS_A;
830 		aic_calc_geometry(&ccb->ccg, extended);
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 
984 	ahc_compile_devinfo(&devinfo, our_id,
985 			    cts->ccb_h.target_id,
986 			    cts->ccb_h.target_lun,
987 			    channel, ROLE_UNKNOWN);
988 	targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
989 					devinfo.our_scsiid,
990 					devinfo.target, &tstate);
991 
992 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
993 		tinfo = &targ_info->curr;
994 	else
995 		tinfo = &targ_info->user;
996 
997 	cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
998 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
999 		if ((ahc->user_discenable & devinfo.target_mask) != 0)
1000 			cts->flags |= CCB_TRANS_DISC_ENB;
1001 
1002 		if ((ahc->user_tagenable & devinfo.target_mask) != 0)
1003 			cts->flags |= CCB_TRANS_TAG_ENB;
1004 	} else {
1005 		if ((tstate->discenable & devinfo.target_mask) != 0)
1006 			cts->flags |= CCB_TRANS_DISC_ENB;
1007 
1008 		if ((tstate->tagenable & devinfo.target_mask) != 0)
1009 			cts->flags |= CCB_TRANS_TAG_ENB;
1010 	}
1011 	cts->sync_period = tinfo->period;
1012 	cts->sync_offset = tinfo->offset;
1013 	cts->bus_width = tinfo->width;
1014 
1015 	cts->valid = CCB_TRANS_SYNC_RATE_VALID
1016 		   | CCB_TRANS_SYNC_OFFSET_VALID
1017 		   | CCB_TRANS_BUS_WIDTH_VALID;
1018 
1019 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
1020 		cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
1021 
1022 	cts->ccb_h.status = CAM_REQ_CMP;
1023 #endif
1024 }
1025 
1026 static void
1027 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
1028 {
1029 	struct ahc_softc *ahc;
1030 	struct cam_sim *sim;
1031 
1032 	sim = (struct cam_sim *)callback_arg;
1033 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
1034 	switch (code) {
1035 	case AC_LOST_DEVICE:
1036 	{
1037 		struct	ahc_devinfo devinfo;
1038 		long	s;
1039 
1040 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
1041 				    xpt_path_target_id(path),
1042 				    xpt_path_lun_id(path),
1043 				    SIM_CHANNEL(ahc, sim),
1044 				    ROLE_UNKNOWN);
1045 
1046 		/*
1047 		 * Revert to async/narrow transfers
1048 		 * for the next device.
1049 		 */
1050 		ahc_lock(ahc, &s);
1051 		ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1052 			      AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
1053 		ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
1054 				 /*period*/0, /*offset*/0, /*ppr_options*/0,
1055 				 AHC_TRANS_GOAL|AHC_TRANS_CUR,
1056 				 /*paused*/FALSE);
1057 		ahc_unlock(ahc, &s);
1058 		break;
1059 	}
1060 	default:
1061 		break;
1062 	}
1063 }
1064 
1065 static void
1066 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1067 		int error)
1068 {
1069 	struct	scb *scb;
1070 	union	ccb *ccb;
1071 	struct	ahc_softc *ahc;
1072 	struct	ahc_initiator_tinfo *tinfo;
1073 	struct	ahc_tmode_tstate *tstate;
1074 	u_int	mask;
1075 	long	s;
1076 
1077 	scb = (struct scb *)arg;
1078 	ccb = scb->io_ctx;
1079 	ahc = scb->ahc_softc;
1080 
1081 	if (error != 0) {
1082 		if (error == EFBIG)
1083 			aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1084 		else
1085 			aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1086 		if (nsegments != 0)
1087 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1088 		ahc_lock(ahc, &s);
1089 		ahc_free_scb(ahc, scb);
1090 		ahc_unlock(ahc, &s);
1091 		xpt_done(ccb);
1092 		return;
1093 	}
1094 	if (nsegments != 0) {
1095 		struct	  ahc_dma_seg *sg;
1096 		bus_dma_segment_t *end_seg;
1097 		bus_dmasync_op_t op;
1098 
1099 		end_seg = dm_segs + nsegments;
1100 
1101 		/* Copy the segments into our SG list */
1102 		sg = scb->sg_list;
1103 		while (dm_segs < end_seg) {
1104 			uint32_t len;
1105 
1106 			sg->addr = aic_htole32(dm_segs->ds_addr);
1107 			len = dm_segs->ds_len
1108 			    | ((dm_segs->ds_addr >> 8) & 0x7F000000);
1109 			sg->len = aic_htole32(len);
1110 			sg++;
1111 			dm_segs++;
1112 		}
1113 
1114 		/*
1115 		 * Note where to find the SG entries in bus space.
1116 		 * We also set the full residual flag which the
1117 		 * sequencer will clear as soon as a data transfer
1118 		 * occurs.
1119 		 */
1120 		scb->hscb->sgptr = aic_htole32(scb->sg_list_phys|SG_FULL_RESID);
1121 
1122 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1123 			op = BUS_DMASYNC_PREREAD;
1124 		else
1125 			op = BUS_DMASYNC_PREWRITE;
1126 
1127 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
1128 
1129 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1130 			struct target_data *tdata;
1131 
1132 			tdata = &scb->hscb->shared_data.tdata;
1133 			tdata->target_phases |= DPHASE_PENDING;
1134 			/*
1135 			 * CAM data direction is relative to the initiator.
1136 			 */
1137 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1138 				tdata->data_phase = P_DATAOUT;
1139 			else
1140 				tdata->data_phase = P_DATAIN;
1141 
1142 			/*
1143 			 * If the transfer is of an odd length and in the
1144 			 * "in" direction (scsi->HostBus), then it may
1145 			 * trigger a bug in the 'WideODD' feature of
1146 			 * non-Ultra2 chips.  Force the total data-length
1147 			 * to be even by adding an extra, 1 byte, SG,
1148 			 * element.  We do this even if we are not currently
1149 			 * negotiated wide as negotiation could occur before
1150 			 * this command is executed.
1151 			 */
1152 			if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1153 			 && (ccb->csio.dxfer_len & 0x1) != 0
1154 			 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1155 
1156 				nsegments++;
1157 				if (nsegments > AHC_NSEG) {
1158 
1159 					aic_set_transaction_status(scb,
1160 					    CAM_REQ_TOO_BIG);
1161 					bus_dmamap_unload(ahc->buffer_dmat,
1162 							  scb->dmamap);
1163 					ahc_lock(ahc, &s);
1164 					ahc_free_scb(ahc, scb);
1165 					ahc_unlock(ahc, &s);
1166 					xpt_done(ccb);
1167 					return;
1168 				}
1169 				sg->addr = aic_htole32(ahc->dma_bug_buf);
1170 				sg->len = aic_htole32(1);
1171 				sg++;
1172 			}
1173 		}
1174 		sg--;
1175 		sg->len |= aic_htole32(AHC_DMA_LAST_SEG);
1176 
1177 		/* Copy the first SG into the "current" data pointer area */
1178 		scb->hscb->dataptr = scb->sg_list->addr;
1179 		scb->hscb->datacnt = scb->sg_list->len;
1180 	} else {
1181 		scb->hscb->sgptr = aic_htole32(SG_LIST_NULL);
1182 		scb->hscb->dataptr = 0;
1183 		scb->hscb->datacnt = 0;
1184 	}
1185 
1186 	scb->sg_count = nsegments;
1187 
1188 	ahc_lock(ahc, &s);
1189 
1190 	/*
1191 	 * Last time we need to check if this SCB needs to
1192 	 * be aborted.
1193 	 */
1194 	if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1195 		if (nsegments != 0)
1196 			bus_dmamap_unload(ahc->buffer_dmat, 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.offset != 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_platform_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, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1275 		ahc_unpause(ahc);
1276 	} else {
1277 		ahc_queue_scb(ahc, scb);
1278 	}
1279 
1280 	ahc_unlock(ahc, &s);
1281 }
1282 
1283 static void
1284 ahc_poll(struct cam_sim *sim)
1285 {
1286 	struct ahc_softc *ahc;
1287 
1288 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
1289 	ahc_intr(ahc);
1290 }
1291 
1292 static void
1293 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1294 	       struct ccb_scsiio *csio, struct scb *scb)
1295 {
1296 	struct hardware_scb *hscb;
1297 	struct ccb_hdr *ccb_h;
1298 
1299 	hscb = scb->hscb;
1300 	ccb_h = &csio->ccb_h;
1301 
1302 	csio->resid = 0;
1303 	csio->sense_resid = 0;
1304 	if (ccb_h->func_code == XPT_SCSI_IO) {
1305 		hscb->cdb_len = csio->cdb_len;
1306 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1307 
1308 			if (hscb->cdb_len > sizeof(hscb->cdb32)
1309 			 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1310 				u_long s;
1311 
1312 				aic_set_transaction_status(scb,
1313 							   CAM_REQ_INVALID);
1314 				ahc_lock(ahc, &s);
1315 				ahc_free_scb(ahc, scb);
1316 				ahc_unlock(ahc, &s);
1317 				xpt_done((union ccb *)csio);
1318 				return;
1319 			}
1320 			if (hscb->cdb_len > 12) {
1321 				memcpy(hscb->cdb32,
1322 				       csio->cdb_io.cdb_ptr,
1323 				       hscb->cdb_len);
1324 				scb->flags |= SCB_CDB32_PTR;
1325 			} else {
1326 				memcpy(hscb->shared_data.cdb,
1327 				       csio->cdb_io.cdb_ptr,
1328 				       hscb->cdb_len);
1329 			}
1330 		} else {
1331 			if (hscb->cdb_len > 12) {
1332 				memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1333 				       hscb->cdb_len);
1334 				scb->flags |= SCB_CDB32_PTR;
1335 			} else {
1336 				memcpy(hscb->shared_data.cdb,
1337 				       csio->cdb_io.cdb_bytes,
1338 				       hscb->cdb_len);
1339 			}
1340 		}
1341 	}
1342 
1343 	/* Only use S/G if there is a transfer */
1344 	if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1345 		if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1346 			/* We've been given a pointer to a single buffer */
1347 			if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1348 				int s;
1349 				int error;
1350 
1351 				s = splsoftvm();
1352 				error = bus_dmamap_load(ahc->buffer_dmat,
1353 							scb->dmamap,
1354 							csio->data_ptr,
1355 							csio->dxfer_len,
1356 							ahc_execute_scb,
1357 							scb, /*flags*/0);
1358 				if (error == EINPROGRESS) {
1359 					/*
1360 					 * So as to maintain ordering,
1361 					 * freeze the controller queue
1362 					 * until our mapping is
1363 					 * returned.
1364 					 */
1365 					xpt_freeze_simq(sim,
1366 							/*count*/1);
1367 					scb->io_ctx->ccb_h.status |=
1368 					    CAM_RELEASE_SIMQ;
1369 				}
1370 				splx(s);
1371 			} else {
1372 				struct bus_dma_segment seg;
1373 
1374 				/* Pointer to physical buffer */
1375 				if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE)
1376 					panic("ahc_setup_data - Transfer size "
1377 					      "larger than can device max");
1378 
1379 				seg.ds_addr =
1380 				    (bus_addr_t)(vm_offset_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_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1406 {
1407 	union ccb *abort_ccb;
1408 
1409 	abort_ccb = ccb->cab.abort_ccb;
1410 	switch (abort_ccb->ccb_h.func_code) {
1411 	case XPT_ACCEPT_TARGET_IO:
1412 	case XPT_IMMED_NOTIFY:
1413 	case XPT_CONT_TARGET_IO:
1414 	{
1415 		struct ahc_tmode_tstate *tstate;
1416 		struct ahc_tmode_lstate *lstate;
1417 		struct ccb_hdr_slist *list;
1418 		cam_status status;
1419 
1420 		status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1421 					     &lstate, TRUE);
1422 
1423 		if (status != CAM_REQ_CMP) {
1424 			ccb->ccb_h.status = status;
1425 			break;
1426 		}
1427 
1428 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1429 			list = &lstate->accept_tios;
1430 		else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1431 			list = &lstate->immed_notifies;
1432 		else
1433 			list = NULL;
1434 
1435 		if (list != NULL) {
1436 			struct ccb_hdr *curelm;
1437 			int found;
1438 
1439 			curelm = SLIST_FIRST(list);
1440 			found = 0;
1441 			if (curelm == &abort_ccb->ccb_h) {
1442 				found = 1;
1443 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1444 			} else {
1445 				while(curelm != NULL) {
1446 					struct ccb_hdr *nextelm;
1447 
1448 					nextelm =
1449 					    SLIST_NEXT(curelm, sim_links.sle);
1450 
1451 					if (nextelm == &abort_ccb->ccb_h) {
1452 						found = 1;
1453 						SLIST_NEXT(curelm,
1454 							   sim_links.sle) =
1455 						    SLIST_NEXT(nextelm,
1456 							       sim_links.sle);
1457 						break;
1458 					}
1459 					curelm = nextelm;
1460 				}
1461 			}
1462 
1463 			if (found) {
1464 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1465 				xpt_done(abort_ccb);
1466 				ccb->ccb_h.status = CAM_REQ_CMP;
1467 			} else {
1468 				xpt_print_path(abort_ccb->ccb_h.path);
1469 				printf("Not found\n");
1470 				ccb->ccb_h.status = CAM_PATH_INVALID;
1471 			}
1472 			break;
1473 		}
1474 		/* FALLTHROUGH */
1475 	}
1476 	case XPT_SCSI_IO:
1477 		/* XXX Fully implement the hard ones */
1478 		ccb->ccb_h.status = CAM_UA_ABORT;
1479 		break;
1480 	default:
1481 		ccb->ccb_h.status = CAM_REQ_INVALID;
1482 		break;
1483 	}
1484 	xpt_done(ccb);
1485 }
1486 
1487 void
1488 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1489 		u_int lun, ac_code code, void *opt_arg)
1490 {
1491 	struct	ccb_trans_settings cts;
1492 	struct cam_path *path;
1493 	void *arg;
1494 	int error;
1495 
1496 	arg = NULL;
1497 	error = ahc_create_path(ahc, channel, target, lun, &path);
1498 
1499 	if (error != CAM_REQ_CMP)
1500 		return;
1501 
1502 	switch (code) {
1503 	case AC_TRANSFER_NEG:
1504 	{
1505 #ifdef AHC_NEW_TRAN_SETTINGS
1506 		struct	ccb_trans_settings_scsi *scsi;
1507 
1508 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1509 		scsi = &cts.proto_specific.scsi;
1510 #else
1511 		cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1512 #endif
1513 		cts.ccb_h.path = path;
1514 		cts.ccb_h.target_id = target;
1515 		cts.ccb_h.target_lun = lun;
1516 		ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1517 							  : ahc->our_id_b,
1518 				      channel, &cts);
1519 		arg = &cts;
1520 #ifdef AHC_NEW_TRAN_SETTINGS
1521 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1522 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1523 #else
1524 		cts.valid &= ~CCB_TRANS_TQ_VALID;
1525 		cts.flags &= ~CCB_TRANS_TAG_ENB;
1526 #endif
1527 		if (opt_arg == NULL)
1528 			break;
1529 		if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1530 #ifdef AHC_NEW_TRAN_SETTINGS
1531 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1532 		scsi->valid |= CTS_SCSI_VALID_TQ;
1533 #else
1534 			cts.flags |= CCB_TRANS_TAG_ENB;
1535 		cts.valid |= CCB_TRANS_TQ_VALID;
1536 #endif
1537 		break;
1538 	}
1539 	case AC_SENT_BDR:
1540 	case AC_BUS_RESET:
1541 		break;
1542 	default:
1543 		panic("ahc_send_async: Unexpected async event");
1544 	}
1545 	xpt_async(code, path, arg);
1546 	xpt_free_path(path);
1547 }
1548 
1549 void
1550 ahc_platform_set_tags(struct ahc_softc *ahc,
1551 		      struct ahc_devinfo *devinfo, int enable)
1552 {
1553 }
1554 
1555 int
1556 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1557 {
1558 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1559 	    M_NOWAIT | M_ZERO);
1560 	if (ahc->platform_data == NULL)
1561 		return (ENOMEM);
1562 	return (0);
1563 }
1564 
1565 void
1566 ahc_platform_free(struct ahc_softc *ahc)
1567 {
1568 	struct ahc_platform_data *pdata;
1569 
1570 	pdata = ahc->platform_data;
1571 	if (pdata != NULL) {
1572 		if (pdata->regs != NULL)
1573 			bus_release_resource(ahc->dev_softc,
1574 					     pdata->regs_res_type,
1575 					     pdata->regs_res_id,
1576 					     pdata->regs);
1577 
1578 		if (pdata->irq != NULL)
1579 			bus_release_resource(ahc->dev_softc,
1580 					     pdata->irq_res_type,
1581 					     0, pdata->irq);
1582 
1583 		if (pdata->sim_b != NULL) {
1584 			xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1585 			xpt_free_path(pdata->path_b);
1586 			xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1587 			cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1588 		}
1589 		if (pdata->sim != NULL) {
1590 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1591 			xpt_free_path(pdata->path);
1592 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1593 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1594 		}
1595 		if (pdata->eh != NULL)
1596 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1597 		free(ahc->platform_data, M_DEVBUF);
1598 	}
1599 }
1600 
1601 int
1602 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1603 {
1604 	/* We don't sort softcs under FreeBSD so report equal always */
1605 	return (0);
1606 }
1607 
1608 int
1609 ahc_detach(device_t dev)
1610 {
1611 	struct ahc_softc *ahc;
1612 	u_long l;
1613 	u_long s;
1614 
1615 	ahc_list_lock(&l);
1616 	device_printf(dev, "detaching device\n");
1617 	ahc = device_get_softc(dev);
1618 	ahc = ahc_find_softc(ahc);
1619 	if (ahc == NULL) {
1620 		device_printf(dev, "aic7xxx already detached\n");
1621 		ahc_list_unlock(&l);
1622 		return (ENOENT);
1623 	}
1624 	TAILQ_REMOVE(&ahc_tailq, ahc, links);
1625 	ahc_list_unlock(&l);
1626 	ahc_lock(ahc, &s);
1627 	ahc_intr_enable(ahc, FALSE);
1628 	bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1629 	ahc_unlock(ahc, &s);
1630 	ahc_free(ahc);
1631 	return (0);
1632 }
1633 
1634 #if UNUSED
1635 static void
1636 ahc_dump_targcmd(struct target_cmd *cmd)
1637 {
1638 	uint8_t *byte;
1639 	uint8_t *last_byte;
1640 	int i;
1641 
1642 	byte = &cmd->initiator_channel;
1643 	/* Debugging info for received commands */
1644 	last_byte = &cmd[1].initiator_channel;
1645 
1646 	i = 0;
1647 	while (byte < last_byte) {
1648 		if (i == 0)
1649 			printf("\t");
1650 		printf("%#x", *byte++);
1651 		i++;
1652 		if (i == 8) {
1653 			printf("\n");
1654 			i = 0;
1655 		} else {
1656 			printf(", ");
1657 		}
1658 	}
1659 }
1660 #endif
1661 
1662 static int
1663 ahc_modevent(module_t mod, int type, void *data)
1664 {
1665 	/* XXX Deal with busy status on unload. */
1666 	return 0;
1667 }
1668 
1669 static moduledata_t ahc_mod = {
1670 	"ahc",
1671 	ahc_modevent,
1672 	NULL
1673 };
1674 
1675 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1676 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1677 MODULE_VERSION(ahc, 1);
1678