xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.c (revision 315ee00f)
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 #include <dev/aic7xxx/aic7xxx_osm.h>
36 #include <dev/aic7xxx/aic7xxx_inline.h>
37 
38 #include <sys/kthread.h>
39 
40 #ifndef AHC_TMODE_ENABLE
41 #define AHC_TMODE_ENABLE 0
42 #endif
43 
44 #include <dev/aic7xxx/aic_osm_lib.c>
45 
46 #define ccb_scb_ptr spriv_ptr0
47 
48 #if 0
49 static void	ahc_dump_targcmd(struct target_cmd *cmd);
50 #endif
51 static int	ahc_modevent(module_t mod, int type, void *data);
52 static void	ahc_action(struct cam_sim *sim, union ccb *ccb);
53 static void	ahc_get_tran_settings(struct ahc_softc *ahc,
54 				      int our_id, char channel,
55 				      struct ccb_trans_settings *cts);
56 static void	ahc_async(void *callback_arg, uint32_t code,
57 			  struct cam_path *path, void *arg);
58 static void	ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
59 				int nsegments, int error);
60 static void	ahc_poll(struct cam_sim *sim);
61 static void	ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
62 			       struct ccb_scsiio *csio, struct scb *scb);
63 static void	ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
64 			      union ccb *ccb);
65 static int	ahc_create_path(struct ahc_softc *ahc,
66 				char channel, u_int target, u_int lun,
67 				struct cam_path **path);
68 
69 static int
70 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
71 	        u_int lun, struct cam_path **path)
72 {
73 	path_id_t path_id;
74 
75 	if (channel == 'B')
76 		path_id = cam_sim_path(ahc->platform_data->sim_b);
77 	else
78 		path_id = cam_sim_path(ahc->platform_data->sim);
79 
80 	return (xpt_create_path(path, /*periph*/NULL,
81 				path_id, target, lun));
82 }
83 
84 int
85 ahc_map_int(struct ahc_softc *ahc)
86 {
87 	int error;
88 	int zero;
89 	int shareable;
90 
91 	zero = 0;
92 	shareable = (ahc->flags & AHC_EDGE_INTERRUPT) ? 0: RF_SHAREABLE;
93 	ahc->platform_data->irq =
94 	    bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IRQ, &zero,
95 				   RF_ACTIVE | shareable);
96 	if (ahc->platform_data->irq == NULL) {
97 		device_printf(ahc->dev_softc,
98 			      "bus_alloc_resource() failed to allocate IRQ\n");
99 		return (ENOMEM);
100 	}
101 	ahc->platform_data->irq_res_type = SYS_RES_IRQ;
102 
103 	/* Hook up our interrupt handler */
104 	error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
105 			       INTR_TYPE_CAM|INTR_MPSAFE, NULL,
106 			       ahc_platform_intr, ahc, &ahc->platform_data->ih);
107 
108 	if (error != 0)
109 		device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
110 			      error);
111 	return (error);
112 }
113 
114 int
115 aic7770_map_registers(struct ahc_softc *ahc, u_int unused_ioport_arg)
116 {
117 	struct	resource *regs;
118 	int	rid;
119 
120 	rid = 0;
121 	regs = bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IOPORT, &rid,
122 				      RF_ACTIVE);
123 	if (regs == NULL) {
124 		device_printf(ahc->dev_softc, "Unable to map I/O space?!\n");
125 		return ENOMEM;
126 	}
127 	ahc->platform_data->regs_res_type = SYS_RES_IOPORT;
128 	ahc->platform_data->regs_res_id = rid;
129 	ahc->platform_data->regs = regs;
130 	ahc->tag = rman_get_bustag(regs);
131 	ahc->bsh = rman_get_bushandle(regs);
132 	return (0);
133 }
134 
135 /*
136  * Attach all the sub-devices we can find
137  */
138 int
139 ahc_attach(struct ahc_softc *ahc)
140 {
141 	char   ahc_info[256];
142 	struct ccb_setasync csa;
143 	struct cam_devq *devq;
144 	int bus_id;
145 	int bus_id2;
146 	struct cam_sim *sim;
147 	struct cam_sim *sim2;
148 	struct cam_path *path;
149 	struct cam_path *path2;
150 	int count;
151 
152 	count = 0;
153 	sim = NULL;
154 	sim2 = NULL;
155 	path = NULL;
156 	path2 = NULL;
157 
158 	/*
159 	 * Create a thread to perform all recovery.
160 	 */
161 	if (ahc_spawn_recovery_thread(ahc) != 0)
162 		goto fail;
163 
164 	ahc_controller_info(ahc, ahc_info);
165 	printf("%s\n", ahc_info);
166 	ahc_lock(ahc);
167 
168 	/*
169 	 * Attach secondary channel first if the user has
170 	 * declared it the primary channel.
171 	 */
172 	if ((ahc->features & AHC_TWIN) != 0
173 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
174 		bus_id = 1;
175 		bus_id2 = 0;
176 	} else {
177 		bus_id = 0;
178 		bus_id2 = 1;
179 	}
180 
181 	/*
182 	 * Create the device queue for our SIM(s).
183 	 */
184 	devq = cam_simq_alloc(AHC_MAX_QUEUE);
185 	if (devq == NULL)
186 		goto fail;
187 
188 	/*
189 	 * Construct our first channel SIM entry
190 	 */
191 	sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
192 			    device_get_unit(ahc->dev_softc),
193 			    &ahc->platform_data->mtx, 1, AHC_MAX_QUEUE, devq);
194 	if (sim == NULL) {
195 		cam_simq_free(devq);
196 		goto fail;
197 	}
198 
199 	if (xpt_bus_register(sim, ahc->dev_softc, bus_id) != CAM_SUCCESS) {
200 		cam_sim_free(sim, /*free_devq*/TRUE);
201 		sim = NULL;
202 		goto fail;
203 	}
204 
205 	if (xpt_create_path(&path, /*periph*/NULL,
206 			    cam_sim_path(sim), CAM_TARGET_WILDCARD,
207 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
208 		xpt_bus_deregister(cam_sim_path(sim));
209 		cam_sim_free(sim, /*free_devq*/TRUE);
210 		sim = NULL;
211 		goto fail;
212 	}
213 
214 	memset(&csa, 0, sizeof(csa));
215 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
216 	csa.ccb_h.func_code = XPT_SASYNC_CB;
217 	csa.event_enable = AC_LOST_DEVICE;
218 	csa.callback = ahc_async;
219 	csa.callback_arg = sim;
220 	xpt_action((union ccb *)&csa);
221 	count++;
222 
223 	if (ahc->features & AHC_TWIN) {
224 		sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
225 				    ahc, device_get_unit(ahc->dev_softc),
226 				    &ahc->platform_data->mtx, 1,
227 				    AHC_MAX_QUEUE, devq);
228 
229 		if (sim2 == NULL) {
230 			printf("ahc_attach: Unable to attach second "
231 			       "bus due to resource shortage");
232 			goto fail;
233 		}
234 
235 		if (xpt_bus_register(sim2, ahc->dev_softc, bus_id2) !=
236 		    CAM_SUCCESS) {
237 			printf("ahc_attach: Unable to attach second "
238 			       "bus due to resource shortage");
239 			/*
240 			 * We do not want to destroy the device queue
241 			 * because the first bus is using it.
242 			 */
243 			cam_sim_free(sim2, /*free_devq*/FALSE);
244 			goto fail;
245 		}
246 
247 		if (xpt_create_path(&path2, /*periph*/NULL,
248 				    cam_sim_path(sim2),
249 				    CAM_TARGET_WILDCARD,
250 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
251 			xpt_bus_deregister(cam_sim_path(sim2));
252 			cam_sim_free(sim2, /*free_devq*/FALSE);
253 			sim2 = NULL;
254 			goto fail;
255 		}
256 		xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
257 		csa.ccb_h.func_code = XPT_SASYNC_CB;
258 		csa.event_enable = AC_LOST_DEVICE;
259 		csa.callback = ahc_async;
260 		csa.callback_arg = sim2;
261 		xpt_action((union ccb *)&csa);
262 		count++;
263 	}
264 
265 fail:
266 	if ((ahc->features & AHC_TWIN) != 0
267 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
268 		ahc->platform_data->sim_b = sim;
269 		ahc->platform_data->path_b = path;
270 		ahc->platform_data->sim = sim2;
271 		ahc->platform_data->path = path2;
272 	} else {
273 		ahc->platform_data->sim = sim;
274 		ahc->platform_data->path = path;
275 		ahc->platform_data->sim_b = sim2;
276 		ahc->platform_data->path_b = path2;
277 	}
278 	ahc_unlock(ahc);
279 
280 	if (count != 0) {
281 		/* We have to wait until after any system dumps... */
282 		ahc->platform_data->eh =
283 		    EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
284 					  ahc, SHUTDOWN_PRI_DEFAULT);
285 		ahc_intr_enable(ahc, TRUE);
286 	}
287 
288 	return (count);
289 }
290 
291 /*
292  * Catch an interrupt from the adapter
293  */
294 void
295 ahc_platform_intr(void *arg)
296 {
297 	struct	ahc_softc *ahc;
298 
299 	ahc = (struct ahc_softc *)arg;
300 	ahc_lock(ahc);
301 	ahc_intr(ahc);
302 	ahc_unlock(ahc);
303 }
304 
305 /*
306  * We have an scb which has been processed by the
307  * adaptor, now we look to see how the operation
308  * went.
309  */
310 void
311 ahc_done(struct ahc_softc *ahc, struct scb *scb)
312 {
313 	union ccb *ccb;
314 
315 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
316 		  ("ahc_done - scb %d\n", scb->hscb->tag));
317 
318 	ccb = scb->io_ctx;
319 	LIST_REMOVE(scb, pending_links);
320 	if ((scb->flags & SCB_TIMEDOUT) != 0)
321 		LIST_REMOVE(scb, timedout_links);
322 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
323 		struct scb_tailq *untagged_q;
324 		int target_offset;
325 
326 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
327 		untagged_q = &ahc->untagged_queues[target_offset];
328 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
329 		scb->flags &= ~SCB_UNTAGGEDQ;
330 		ahc_run_untagged_queue(ahc, untagged_q);
331 	}
332 
333 	callout_stop(&scb->io_timer);
334 
335 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
336 		bus_dmasync_op_t op;
337 
338 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
339 			op = BUS_DMASYNC_POSTREAD;
340 		else
341 			op = BUS_DMASYNC_POSTWRITE;
342 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
343 		bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
344 	}
345 
346 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
347 		struct cam_path *ccb_path;
348 
349 		/*
350 		 * If we have finally disconnected, clean up our
351 		 * pending device state.
352 		 * XXX - There may be error states that cause where
353 		 *       we will remain connected.
354 		 */
355 		ccb_path = ccb->ccb_h.path;
356 		if (ahc->pending_device != NULL
357 		 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
358 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
359 				ahc->pending_device = NULL;
360 			} else {
361 				if (bootverbose) {
362 					xpt_print_path(ccb->ccb_h.path);
363 					printf("Still connected\n");
364 				}
365 				aic_freeze_ccb(ccb);
366 			}
367 		}
368 
369 		if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
370 			ccb->ccb_h.status |= CAM_REQ_CMP;
371 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
372 		ahc_free_scb(ahc, scb);
373 		xpt_done(ccb);
374 		return;
375 	}
376 
377 	/*
378 	 * If the recovery SCB completes, we have to be
379 	 * out of our timeout.
380 	 */
381 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
382 		struct	scb *list_scb;
383 
384 		ahc->scb_data->recovery_scbs--;
385 
386 		if (aic_get_transaction_status(scb) == CAM_BDR_SENT
387 		 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
388 			aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
389 
390 		if (ahc->scb_data->recovery_scbs == 0) {
391 			/*
392 			 * All recovery actions have completed successfully,
393 			 * so reinstate the timeouts for all other pending
394 			 * commands.
395 			 */
396 			LIST_FOREACH(list_scb, &ahc->pending_scbs,
397 				     pending_links) {
398 				aic_scb_timer_reset(list_scb,
399 						    aic_get_timeout(scb));
400 			}
401 
402 			ahc_print_path(ahc, scb);
403 			printf("no longer in timeout, status = %x\n",
404 			       ccb->ccb_h.status);
405 		}
406 	}
407 
408 	/* Don't clobber any existing error state */
409 	if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
410 		ccb->ccb_h.status |= CAM_REQ_CMP;
411 	} else if ((scb->flags & SCB_SENSE) != 0) {
412 		/*
413 		 * We performed autosense retrieval.
414 		 *
415 		 * Zero any sense not transferred by the
416 		 * device.  The SCSI spec mandates that any
417 		 * untransfered data should be assumed to be
418 		 * zero.  Complete the 'bounce' of sense information
419 		 * through buffers accessible via bus-space by
420 		 * copying it into the clients csio.
421 		 */
422 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
423 		memcpy(&ccb->csio.sense_data,
424 		       ahc_get_sense_buf(ahc, scb),
425 		       (aic_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
426 		       - ccb->csio.sense_resid);
427 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
428 	}
429 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
430 	ahc_free_scb(ahc, scb);
431 	xpt_done(ccb);
432 }
433 
434 static void
435 ahc_action(struct cam_sim *sim, union ccb *ccb)
436 {
437 	struct	ahc_softc *ahc;
438 	struct	ahc_tmode_lstate *lstate;
439 	u_int	target_id;
440 	u_int	our_id;
441 
442 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
443 
444 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
445 
446 	target_id = ccb->ccb_h.target_id;
447 	our_id = SIM_SCSI_ID(ahc, sim);
448 
449 	switch (ccb->ccb_h.func_code) {
450 	/* Common cases first */
451 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
452 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
453 	{
454 		struct	   ahc_tmode_tstate *tstate;
455 		cam_status status;
456 
457 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
458 					     &lstate, TRUE);
459 
460 		if (status != CAM_REQ_CMP) {
461 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
462 				/* Response from the black hole device */
463 				tstate = NULL;
464 				lstate = ahc->black_hole;
465 			} else {
466 				ccb->ccb_h.status = status;
467 				xpt_done(ccb);
468 				break;
469 			}
470 		}
471 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
472 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
473 					  sim_links.sle);
474 			ccb->ccb_h.status = CAM_REQ_INPROG;
475 			if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
476 				ahc_run_tqinfifo(ahc, /*paused*/FALSE);
477 			break;
478 		}
479 
480 		/*
481 		 * The target_id represents the target we attempt to
482 		 * select.  In target mode, this is the initiator of
483 		 * the original command.
484 		 */
485 		our_id = target_id;
486 		target_id = ccb->csio.init_id;
487 		/* FALLTHROUGH */
488 	}
489 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
490 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
491 	{
492 		struct	scb *scb;
493 		struct	hardware_scb *hscb;
494 
495 		if ((ahc->flags & AHC_INITIATORROLE) == 0
496 		 && (ccb->ccb_h.func_code == XPT_SCSI_IO
497 		  || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
498 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
499 			xpt_done(ccb);
500 			return;
501 		}
502 
503 		/*
504 		 * get an scb to use.
505 		 */
506 		if ((scb = ahc_get_scb(ahc)) == NULL) {
507 			xpt_freeze_simq(sim, /*count*/1);
508 			ahc->flags |= AHC_RESOURCE_SHORTAGE;
509 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
510 			xpt_done(ccb);
511 			return;
512 		}
513 
514 		hscb = scb->hscb;
515 
516 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
517 			  ("start scb(%p)\n", scb));
518 		scb->io_ctx = ccb;
519 		/*
520 		 * So we can find the SCB when an abort is requested
521 		 */
522 		ccb->ccb_h.ccb_scb_ptr = scb;
523 
524 		/*
525 		 * Put all the arguments for the xfer in the scb
526 		 */
527 		hscb->control = 0;
528 		hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
529 		hscb->lun = ccb->ccb_h.target_lun;
530 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
531 			hscb->cdb_len = 0;
532 			scb->flags |= SCB_DEVICE_RESET;
533 			hscb->control |= MK_MESSAGE;
534 			ahc_execute_scb(scb, NULL, 0, 0);
535 		} else {
536 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
537 				struct target_data *tdata;
538 
539 				tdata = &hscb->shared_data.tdata;
540 				if (ahc->pending_device == lstate)
541 					scb->flags |= SCB_TARGET_IMMEDIATE;
542 				hscb->control |= TARGET_SCB;
543 				scb->flags |= SCB_TARGET_SCB;
544 				tdata->target_phases = 0;
545 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
546 					tdata->target_phases |= SPHASE_PENDING;
547 					tdata->scsi_status =
548 					    ccb->csio.scsi_status;
549 				}
550 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
551 					tdata->target_phases |= NO_DISCONNECT;
552 
553 				tdata->initiator_tag = ccb->csio.tag_id;
554 			}
555 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
556 				hscb->control |= ccb->csio.tag_action;
557 
558 			ahc_setup_data(ahc, sim, &ccb->csio, scb);
559 		}
560 		break;
561 	}
562 	case XPT_NOTIFY_ACKNOWLEDGE:
563 	case XPT_IMMEDIATE_NOTIFY:
564 	{
565 		struct	   ahc_tmode_tstate *tstate;
566 		struct	   ahc_tmode_lstate *lstate;
567 		cam_status status;
568 
569 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
570 					     &lstate, TRUE);
571 
572 		if (status != CAM_REQ_CMP) {
573 			ccb->ccb_h.status = status;
574 			xpt_done(ccb);
575 			break;
576 		}
577 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
578 				  sim_links.sle);
579 		ccb->ccb_h.status = CAM_REQ_INPROG;
580 		ahc_send_lstate_events(ahc, lstate);
581 		break;
582 	}
583 	case XPT_EN_LUN:		/* Enable LUN as a target */
584 		ahc_handle_en_lun(ahc, sim, ccb);
585 		xpt_done(ccb);
586 		break;
587 	case XPT_ABORT:			/* Abort the specified CCB */
588 	{
589 		ahc_abort_ccb(ahc, sim, ccb);
590 		break;
591 	}
592 	case XPT_SET_TRAN_SETTINGS:
593 	{
594 		struct	ahc_devinfo devinfo;
595 		struct	ccb_trans_settings *cts;
596 		struct	ccb_trans_settings_scsi *scsi;
597 		struct	ccb_trans_settings_spi *spi;
598 		struct	ahc_initiator_tinfo *tinfo;
599 		struct	ahc_tmode_tstate *tstate;
600 		uint16_t *discenable;
601 		uint16_t *tagenable;
602 		u_int	update_type;
603 
604 		cts = &ccb->cts;
605 		scsi = &cts->proto_specific.scsi;
606 		spi = &cts->xport_specific.spi;
607 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
608 				    cts->ccb_h.target_id,
609 				    cts->ccb_h.target_lun,
610 				    SIM_CHANNEL(ahc, sim),
611 				    ROLE_UNKNOWN);
612 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
613 					    devinfo.our_scsiid,
614 					    devinfo.target, &tstate);
615 		update_type = 0;
616 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
617 			update_type |= AHC_TRANS_GOAL;
618 			discenable = &tstate->discenable;
619 			tagenable = &tstate->tagenable;
620 			tinfo->curr.protocol_version =
621 			    cts->protocol_version;
622 			tinfo->curr.transport_version =
623 			    cts->transport_version;
624 			tinfo->goal.protocol_version =
625 			    cts->protocol_version;
626 			tinfo->goal.transport_version =
627 			    cts->transport_version;
628 		} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
629 			update_type |= AHC_TRANS_USER;
630 			discenable = &ahc->user_discenable;
631 			tagenable = &ahc->user_tagenable;
632 			tinfo->user.protocol_version =
633 			    cts->protocol_version;
634 			tinfo->user.transport_version =
635 			    cts->transport_version;
636 		} else {
637 			ccb->ccb_h.status = CAM_REQ_INVALID;
638 			xpt_done(ccb);
639 			break;
640 		}
641 
642 		if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
643 			if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
644 				*discenable |= devinfo.target_mask;
645 			else
646 				*discenable &= ~devinfo.target_mask;
647 		}
648 
649 		if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
650 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
651 				*tagenable |= devinfo.target_mask;
652 			else
653 				*tagenable &= ~devinfo.target_mask;
654 		}
655 
656 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
657 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
658 					   &spi->bus_width, ROLE_UNKNOWN);
659 			ahc_set_width(ahc, &devinfo, spi->bus_width,
660 				      update_type, /*paused*/FALSE);
661 		}
662 
663 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
664 			if (update_type == AHC_TRANS_USER)
665 				spi->ppr_options = tinfo->user.ppr_options;
666 			else
667 				spi->ppr_options = tinfo->goal.ppr_options;
668 		}
669 
670 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
671 			if (update_type == AHC_TRANS_USER)
672 				spi->sync_offset = tinfo->user.offset;
673 			else
674 				spi->sync_offset = tinfo->goal.offset;
675 		}
676 
677 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
678 			if (update_type == AHC_TRANS_USER)
679 				spi->sync_period = tinfo->user.period;
680 			else
681 				spi->sync_period = tinfo->goal.period;
682 		}
683 
684 		if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
685 		 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
686 			struct ahc_syncrate *syncrate;
687 			u_int maxsync;
688 
689 			if ((ahc->features & AHC_ULTRA2) != 0)
690 				maxsync = AHC_SYNCRATE_DT;
691 			else if ((ahc->features & AHC_ULTRA) != 0)
692 				maxsync = AHC_SYNCRATE_ULTRA;
693 			else
694 				maxsync = AHC_SYNCRATE_FAST;
695 
696 			if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
697 				spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
698 
699 			syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
700 						     &spi->ppr_options,
701 						     maxsync);
702 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
703 					    syncrate, &spi->sync_offset,
704 					    spi->bus_width, ROLE_UNKNOWN);
705 
706 			/* We use a period of 0 to represent async */
707 			if (spi->sync_offset == 0) {
708 				spi->sync_period = 0;
709 				spi->ppr_options = 0;
710 			}
711 
712 			ahc_set_syncrate(ahc, &devinfo, syncrate,
713 					 spi->sync_period, spi->sync_offset,
714 					 spi->ppr_options, update_type,
715 					 /*paused*/FALSE);
716 		}
717 		ccb->ccb_h.status = CAM_REQ_CMP;
718 		xpt_done(ccb);
719 		break;
720 	}
721 	case XPT_GET_TRAN_SETTINGS:
722 	/* Get default/user set transfer settings for the target */
723 	{
724 		ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
725 				      SIM_CHANNEL(ahc, sim), &ccb->cts);
726 		xpt_done(ccb);
727 		break;
728 	}
729 	case XPT_CALC_GEOMETRY:
730 	{
731 		int extended;
732 
733 		extended = SIM_IS_SCSIBUS_B(ahc, sim)
734 			 ? ahc->flags & AHC_EXTENDED_TRANS_B
735 			 : ahc->flags & AHC_EXTENDED_TRANS_A;
736 		aic_calc_geometry(&ccb->ccg, extended);
737 		xpt_done(ccb);
738 		break;
739 	}
740 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
741 	{
742 		int  found;
743 
744 		found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
745 					  /*initiate reset*/TRUE);
746 		if (bootverbose) {
747 			xpt_print_path(SIM_PATH(ahc, sim));
748 			printf("SCSI bus reset delivered. "
749 			       "%d SCBs aborted.\n", found);
750 		}
751 		ccb->ccb_h.status = CAM_REQ_CMP;
752 		xpt_done(ccb);
753 		break;
754 	}
755 	case XPT_TERM_IO:		/* Terminate the I/O process */
756 		/* XXX Implement */
757 		ccb->ccb_h.status = CAM_REQ_INVALID;
758 		xpt_done(ccb);
759 		break;
760 	case XPT_PATH_INQ:		/* Path routing inquiry */
761 	{
762 		struct ccb_pathinq *cpi = &ccb->cpi;
763 
764 		cpi->version_num = 1; /* XXX??? */
765 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
766 		if ((ahc->features & AHC_WIDE) != 0)
767 			cpi->hba_inquiry |= PI_WIDE_16;
768 		if ((ahc->features & AHC_TARGETMODE) != 0) {
769 			cpi->target_sprt = PIT_PROCESSOR
770 					 | PIT_DISCONNECT
771 					 | PIT_TERM_IO;
772 		} else {
773 			cpi->target_sprt = 0;
774 		}
775 		cpi->hba_misc = 0;
776 		cpi->hba_eng_cnt = 0;
777 		cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
778 		cpi->max_lun = AHC_NUM_LUNS - 1;
779 		if (SIM_IS_SCSIBUS_B(ahc, sim)) {
780 			cpi->initiator_id = ahc->our_id_b;
781 			if ((ahc->flags & AHC_RESET_BUS_B) == 0)
782 				cpi->hba_misc |= PIM_NOBUSRESET;
783 		} else {
784 			cpi->initiator_id = ahc->our_id;
785 			if ((ahc->flags & AHC_RESET_BUS_A) == 0)
786 				cpi->hba_misc |= PIM_NOBUSRESET;
787 		}
788 		cpi->bus_id = cam_sim_bus(sim);
789 		cpi->base_transfer_speed = 3300;
790 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
791 		strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
792 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
793 		cpi->unit_number = cam_sim_unit(sim);
794 		cpi->protocol = PROTO_SCSI;
795 		cpi->protocol_version = SCSI_REV_2;
796 		cpi->transport = XPORT_SPI;
797 		cpi->transport_version = 2;
798 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
799 		if ((ahc->features & AHC_DT) != 0) {
800 			cpi->transport_version = 3;
801 			cpi->xport_specific.spi.ppr_options =
802 			    SID_SPI_CLOCK_DT_ST;
803 		}
804 		cpi->ccb_h.status = CAM_REQ_CMP;
805 		xpt_done(ccb);
806 		break;
807 	}
808 	default:
809 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
810 		xpt_done(ccb);
811 		break;
812 	}
813 }
814 
815 static void
816 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
817 		      struct ccb_trans_settings *cts)
818 {
819 	struct	ahc_devinfo devinfo;
820 	struct	ccb_trans_settings_scsi *scsi;
821 	struct	ccb_trans_settings_spi *spi;
822 	struct	ahc_initiator_tinfo *targ_info;
823 	struct	ahc_tmode_tstate *tstate;
824 	struct	ahc_transinfo *tinfo;
825 
826 	scsi = &cts->proto_specific.scsi;
827 	spi = &cts->xport_specific.spi;
828 	ahc_compile_devinfo(&devinfo, our_id,
829 			    cts->ccb_h.target_id,
830 			    cts->ccb_h.target_lun,
831 			    channel, ROLE_UNKNOWN);
832 	targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
833 					devinfo.our_scsiid,
834 					devinfo.target, &tstate);
835 
836 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
837 		tinfo = &targ_info->curr;
838 	else
839 		tinfo = &targ_info->user;
840 
841 	scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
842 	spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
843 	if (cts->type == CTS_TYPE_USER_SETTINGS) {
844 		if ((ahc->user_discenable & devinfo.target_mask) != 0)
845 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
846 
847 		if ((ahc->user_tagenable & devinfo.target_mask) != 0)
848 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
849 	} else {
850 		if ((tstate->discenable & devinfo.target_mask) != 0)
851 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
852 
853 		if ((tstate->tagenable & devinfo.target_mask) != 0)
854 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
855 	}
856 	cts->protocol_version = tinfo->protocol_version;
857 	cts->transport_version = tinfo->transport_version;
858 
859 	spi->sync_period = tinfo->period;
860 	spi->sync_offset = tinfo->offset;
861 	spi->bus_width = tinfo->width;
862 	spi->ppr_options = tinfo->ppr_options;
863 
864 	cts->protocol = PROTO_SCSI;
865 	cts->transport = XPORT_SPI;
866 	spi->valid = CTS_SPI_VALID_SYNC_RATE
867 		   | CTS_SPI_VALID_SYNC_OFFSET
868 		   | CTS_SPI_VALID_BUS_WIDTH
869 		   | CTS_SPI_VALID_PPR_OPTIONS;
870 
871 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
872 		scsi->valid = CTS_SCSI_VALID_TQ;
873 		spi->valid |= CTS_SPI_VALID_DISC;
874 	} else {
875 		scsi->valid = 0;
876 	}
877 
878 	cts->ccb_h.status = CAM_REQ_CMP;
879 }
880 
881 static void
882 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
883 {
884 	struct ahc_softc *ahc;
885 	struct cam_sim *sim;
886 
887 	sim = (struct cam_sim *)callback_arg;
888 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
889 	switch (code) {
890 	case AC_LOST_DEVICE:
891 	{
892 		struct	ahc_devinfo devinfo;
893 
894 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
895 				    xpt_path_target_id(path),
896 				    xpt_path_lun_id(path),
897 				    SIM_CHANNEL(ahc, sim),
898 				    ROLE_UNKNOWN);
899 
900 		/*
901 		 * Revert to async/narrow transfers
902 		 * for the next device.
903 		 */
904 		ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
905 			      AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
906 		ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
907 				 /*period*/0, /*offset*/0, /*ppr_options*/0,
908 				 AHC_TRANS_GOAL|AHC_TRANS_CUR,
909 				 /*paused*/FALSE);
910 		break;
911 	}
912 	default:
913 		break;
914 	}
915 }
916 
917 static void
918 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
919 		int error)
920 {
921 	struct	scb *scb;
922 	union	ccb *ccb;
923 	struct	ahc_softc *ahc;
924 	struct	ahc_initiator_tinfo *tinfo;
925 	struct	ahc_tmode_tstate *tstate;
926 	u_int	mask;
927 
928 	scb = (struct scb *)arg;
929 	ccb = scb->io_ctx;
930 	ahc = scb->ahc_softc;
931 
932 	if (error != 0) {
933 		if (error == EFBIG)
934 			aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
935 		else
936 			aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
937 		if (nsegments != 0)
938 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
939 		ahc_free_scb(ahc, scb);
940 		xpt_done(ccb);
941 		return;
942 	}
943 	if (nsegments != 0) {
944 		struct	  ahc_dma_seg *sg;
945 		bus_dma_segment_t *end_seg;
946 		bus_dmasync_op_t op;
947 
948 		end_seg = dm_segs + nsegments;
949 
950 		/* Copy the segments into our SG list */
951 		sg = scb->sg_list;
952 		while (dm_segs < end_seg) {
953 			uint32_t len;
954 
955 			sg->addr = aic_htole32(dm_segs->ds_addr);
956 			len = dm_segs->ds_len
957 			    | ((dm_segs->ds_addr >> 8) & 0x7F000000);
958 			sg->len = aic_htole32(len);
959 			sg++;
960 			dm_segs++;
961 		}
962 
963 		/*
964 		 * Note where to find the SG entries in bus space.
965 		 * We also set the full residual flag which the
966 		 * sequencer will clear as soon as a data transfer
967 		 * occurs.
968 		 */
969 		scb->hscb->sgptr = aic_htole32(scb->sg_list_phys|SG_FULL_RESID);
970 
971 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
972 			op = BUS_DMASYNC_PREREAD;
973 		else
974 			op = BUS_DMASYNC_PREWRITE;
975 
976 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
977 
978 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
979 			struct target_data *tdata;
980 
981 			tdata = &scb->hscb->shared_data.tdata;
982 			tdata->target_phases |= DPHASE_PENDING;
983 			/*
984 			 * CAM data direction is relative to the initiator.
985 			 */
986 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
987 				tdata->data_phase = P_DATAOUT;
988 			else
989 				tdata->data_phase = P_DATAIN;
990 
991 			/*
992 			 * If the transfer is of an odd length and in the
993 			 * "in" direction (scsi->HostBus), then it may
994 			 * trigger a bug in the 'WideODD' feature of
995 			 * non-Ultra2 chips.  Force the total data-length
996 			 * to be even by adding an extra, 1 byte, SG,
997 			 * element.  We do this even if we are not currently
998 			 * negotiated wide as negotiation could occur before
999 			 * this command is executed.
1000 			 */
1001 			if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1002 			 && (ccb->csio.dxfer_len & 0x1) != 0
1003 			 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1004 				nsegments++;
1005 				if (nsegments > AHC_NSEG) {
1006 					aic_set_transaction_status(scb,
1007 					    CAM_REQ_TOO_BIG);
1008 					bus_dmamap_unload(ahc->buffer_dmat,
1009 							  scb->dmamap);
1010 					ahc_free_scb(ahc, scb);
1011 					xpt_done(ccb);
1012 					return;
1013 				}
1014 				sg->addr = aic_htole32(ahc->dma_bug_buf);
1015 				sg->len = aic_htole32(1);
1016 				sg++;
1017 			}
1018 		}
1019 		sg--;
1020 		sg->len |= aic_htole32(AHC_DMA_LAST_SEG);
1021 
1022 		/* Copy the first SG into the "current" data pointer area */
1023 		scb->hscb->dataptr = scb->sg_list->addr;
1024 		scb->hscb->datacnt = scb->sg_list->len;
1025 	} else {
1026 		scb->hscb->sgptr = aic_htole32(SG_LIST_NULL);
1027 		scb->hscb->dataptr = 0;
1028 		scb->hscb->datacnt = 0;
1029 	}
1030 
1031 	scb->sg_count = nsegments;
1032 
1033 	/*
1034 	 * Last time we need to check if this SCB needs to
1035 	 * be aborted.
1036 	 */
1037 	if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1038 		if (nsegments != 0)
1039 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1040 		ahc_free_scb(ahc, scb);
1041 		xpt_done(ccb);
1042 		return;
1043 	}
1044 
1045 	tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1046 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1047 				    SCSIID_TARGET(ahc, scb->hscb->scsiid),
1048 				    &tstate);
1049 
1050 	mask = SCB_GET_TARGET_MASK(ahc, scb);
1051 	scb->hscb->scsirate = tinfo->scsirate;
1052 	scb->hscb->scsioffset = tinfo->curr.offset;
1053 	if ((tstate->ultraenb & mask) != 0)
1054 		scb->hscb->control |= ULTRAENB;
1055 
1056 	if ((tstate->discenable & mask) != 0
1057 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1058 		scb->hscb->control |= DISCENB;
1059 
1060 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1061 	 && (tinfo->goal.width != 0
1062 	  || tinfo->goal.offset != 0
1063 	  || tinfo->goal.ppr_options != 0)) {
1064 		scb->flags |= SCB_NEGOTIATE;
1065 		scb->hscb->control |= MK_MESSAGE;
1066 	} else if ((tstate->auto_negotiate & mask) != 0) {
1067 		scb->flags |= SCB_AUTO_NEGOTIATE;
1068 		scb->hscb->control |= MK_MESSAGE;
1069 	}
1070 
1071 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1072 
1073 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1074 
1075 	/*
1076 	 * We only allow one untagged transaction
1077 	 * per target in the initiator role unless
1078 	 * we are storing a full busy target *lun*
1079 	 * table in SCB space.
1080 	 */
1081 	if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1082 	 && (ahc->flags & AHC_SCB_BTT) == 0) {
1083 		struct scb_tailq *untagged_q;
1084 		int target_offset;
1085 
1086 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1087 		untagged_q = &(ahc->untagged_queues[target_offset]);
1088 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1089 		scb->flags |= SCB_UNTAGGEDQ;
1090 		if (TAILQ_FIRST(untagged_q) != scb) {
1091 			return;
1092 		}
1093 	}
1094 	scb->flags |= SCB_ACTIVE;
1095 
1096 	/*
1097 	 * Timers are disabled while recovery is in progress.
1098 	 */
1099 	aic_scb_timer_start(scb);
1100 
1101 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1102 		/* Define a mapping from our tag to the SCB. */
1103 		ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1104 		ahc_pause(ahc);
1105 		if ((ahc->flags & AHC_PAGESCBS) == 0)
1106 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1107 		ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1108 		ahc_unpause(ahc);
1109 	} else {
1110 		ahc_queue_scb(ahc, scb);
1111 	}
1112 }
1113 
1114 static void
1115 ahc_poll(struct cam_sim *sim)
1116 {
1117 	struct ahc_softc *ahc;
1118 
1119 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
1120 	ahc_intr(ahc);
1121 }
1122 
1123 static void
1124 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1125 	       struct ccb_scsiio *csio, struct scb *scb)
1126 {
1127 	struct hardware_scb *hscb;
1128 	struct ccb_hdr *ccb_h;
1129 	int error;
1130 
1131 	hscb = scb->hscb;
1132 	ccb_h = &csio->ccb_h;
1133 
1134 	csio->resid = 0;
1135 	csio->sense_resid = 0;
1136 	if (ccb_h->func_code == XPT_SCSI_IO) {
1137 		hscb->cdb_len = csio->cdb_len;
1138 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1139 			if (hscb->cdb_len > sizeof(hscb->cdb32)
1140 			 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1141 				aic_set_transaction_status(scb,
1142 							   CAM_REQ_INVALID);
1143 				ahc_free_scb(ahc, scb);
1144 				xpt_done((union ccb *)csio);
1145 				return;
1146 			}
1147 			if (hscb->cdb_len > 12) {
1148 				memcpy(hscb->cdb32,
1149 				       csio->cdb_io.cdb_ptr,
1150 				       hscb->cdb_len);
1151 				scb->flags |= SCB_CDB32_PTR;
1152 			} else {
1153 				memcpy(hscb->shared_data.cdb,
1154 				       csio->cdb_io.cdb_ptr,
1155 				       hscb->cdb_len);
1156 			}
1157 		} else {
1158 			if (hscb->cdb_len > 12) {
1159 				memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1160 				       hscb->cdb_len);
1161 				scb->flags |= SCB_CDB32_PTR;
1162 			} else {
1163 				memcpy(hscb->shared_data.cdb,
1164 				       csio->cdb_io.cdb_bytes,
1165 				       hscb->cdb_len);
1166 			}
1167 		}
1168 	}
1169 
1170 	error = bus_dmamap_load_ccb(ahc->buffer_dmat,
1171 				    scb->dmamap,
1172 				    (union ccb *)csio,
1173 				    ahc_execute_scb,
1174 				    scb,
1175 				    0);
1176 	if (error == EINPROGRESS) {
1177 		/*
1178 		 * So as to maintain ordering,
1179 		 * freeze the controller queue
1180 		 * until our mapping is
1181 		 * returned.
1182 		 */
1183 		xpt_freeze_simq(sim, /*count*/1);
1184 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1185 	}
1186 }
1187 
1188 static void
1189 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1190 {
1191 	union ccb *abort_ccb;
1192 
1193 	abort_ccb = ccb->cab.abort_ccb;
1194 	switch (abort_ccb->ccb_h.func_code) {
1195 	case XPT_ACCEPT_TARGET_IO:
1196 	case XPT_IMMEDIATE_NOTIFY:
1197 	case XPT_CONT_TARGET_IO:
1198 	{
1199 		struct ahc_tmode_tstate *tstate;
1200 		struct ahc_tmode_lstate *lstate;
1201 		struct ccb_hdr_slist *list;
1202 		cam_status status;
1203 
1204 		status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1205 					     &lstate, TRUE);
1206 
1207 		if (status != CAM_REQ_CMP) {
1208 			ccb->ccb_h.status = status;
1209 			break;
1210 		}
1211 
1212 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1213 			list = &lstate->accept_tios;
1214 		else if (abort_ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
1215 			list = &lstate->immed_notifies;
1216 		else
1217 			list = NULL;
1218 
1219 		if (list != NULL) {
1220 			struct ccb_hdr *curelm;
1221 			int found;
1222 
1223 			curelm = SLIST_FIRST(list);
1224 			found = 0;
1225 			if (curelm == &abort_ccb->ccb_h) {
1226 				found = 1;
1227 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1228 			} else {
1229 				while(curelm != NULL) {
1230 					struct ccb_hdr *nextelm;
1231 
1232 					nextelm =
1233 					    SLIST_NEXT(curelm, sim_links.sle);
1234 
1235 					if (nextelm == &abort_ccb->ccb_h) {
1236 						found = 1;
1237 						SLIST_NEXT(curelm,
1238 							   sim_links.sle) =
1239 						    SLIST_NEXT(nextelm,
1240 							       sim_links.sle);
1241 						break;
1242 					}
1243 					curelm = nextelm;
1244 				}
1245 			}
1246 
1247 			if (found) {
1248 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1249 				xpt_done(abort_ccb);
1250 				ccb->ccb_h.status = CAM_REQ_CMP;
1251 			} else {
1252 				xpt_print_path(abort_ccb->ccb_h.path);
1253 				printf("Not found\n");
1254 				ccb->ccb_h.status = CAM_PATH_INVALID;
1255 			}
1256 			break;
1257 		}
1258 		/* FALLTHROUGH */
1259 	}
1260 	case XPT_SCSI_IO:
1261 		/* XXX Fully implement the hard ones */
1262 		ccb->ccb_h.status = CAM_UA_ABORT;
1263 		break;
1264 	default:
1265 		ccb->ccb_h.status = CAM_REQ_INVALID;
1266 		break;
1267 	}
1268 	xpt_done(ccb);
1269 }
1270 
1271 void
1272 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1273 		u_int lun, ac_code code, void *opt_arg)
1274 {
1275 	struct	ccb_trans_settings cts;
1276 	struct cam_path *path;
1277 	void *arg;
1278 	int error;
1279 
1280 	arg = NULL;
1281 	error = ahc_create_path(ahc, channel, target, lun, &path);
1282 
1283 	if (error != CAM_REQ_CMP)
1284 		return;
1285 
1286 	switch (code) {
1287 	case AC_TRANSFER_NEG:
1288 	{
1289 		struct	ccb_trans_settings_scsi *scsi;
1290 
1291 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1292 		scsi = &cts.proto_specific.scsi;
1293 		cts.ccb_h.path = path;
1294 		cts.ccb_h.target_id = target;
1295 		cts.ccb_h.target_lun = lun;
1296 		ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1297 							  : ahc->our_id_b,
1298 				      channel, &cts);
1299 		arg = &cts;
1300 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1301 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1302 		if (opt_arg == NULL)
1303 			break;
1304 		if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1305 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1306 		scsi->valid |= CTS_SCSI_VALID_TQ;
1307 		break;
1308 	}
1309 	case AC_SENT_BDR:
1310 	case AC_BUS_RESET:
1311 		break;
1312 	default:
1313 		panic("ahc_send_async: Unexpected async event");
1314 	}
1315 	xpt_async(code, path, arg);
1316 	xpt_free_path(path);
1317 }
1318 
1319 void
1320 ahc_platform_set_tags(struct ahc_softc *ahc,
1321 		      struct ahc_devinfo *devinfo, int enable)
1322 {
1323 }
1324 
1325 int
1326 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1327 {
1328 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1329 	    M_NOWAIT | M_ZERO);
1330 	if (ahc->platform_data == NULL)
1331 		return (ENOMEM);
1332 	return (0);
1333 }
1334 
1335 void
1336 ahc_platform_free(struct ahc_softc *ahc)
1337 {
1338 	struct ahc_platform_data *pdata;
1339 
1340 	pdata = ahc->platform_data;
1341 	if (pdata != NULL) {
1342 		if (pdata->regs != NULL)
1343 			bus_release_resource(ahc->dev_softc,
1344 					     pdata->regs_res_type,
1345 					     pdata->regs_res_id,
1346 					     pdata->regs);
1347 
1348 		if (pdata->irq != NULL)
1349 			bus_release_resource(ahc->dev_softc,
1350 					     pdata->irq_res_type,
1351 					     0, pdata->irq);
1352 
1353 		if (pdata->sim_b != NULL) {
1354 			xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1355 			xpt_free_path(pdata->path_b);
1356 			xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1357 			cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1358 		}
1359 		if (pdata->sim != NULL) {
1360 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1361 			xpt_free_path(pdata->path);
1362 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1363 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1364 		}
1365 		if (pdata->eh != NULL)
1366 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1367 		free(ahc->platform_data, M_DEVBUF);
1368 	}
1369 }
1370 
1371 int
1372 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1373 {
1374 	/* We don't sort softcs under FreeBSD so report equal always */
1375 	return (0);
1376 }
1377 
1378 int
1379 ahc_detach(device_t dev)
1380 {
1381 	struct ahc_softc *ahc;
1382 
1383 	device_printf(dev, "detaching device\n");
1384 	ahc = device_get_softc(dev);
1385 	ahc_lock(ahc);
1386 	TAILQ_REMOVE(&ahc_tailq, ahc, links);
1387 	ahc_intr_enable(ahc, FALSE);
1388 	bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1389 	ahc_unlock(ahc);
1390 	ahc_free(ahc);
1391 	return (0);
1392 }
1393 
1394 #if 0
1395 static void
1396 ahc_dump_targcmd(struct target_cmd *cmd)
1397 {
1398 	uint8_t *byte;
1399 	uint8_t *last_byte;
1400 	int i;
1401 
1402 	byte = &cmd->initiator_channel;
1403 	/* Debugging info for received commands */
1404 	last_byte = &cmd[1].initiator_channel;
1405 
1406 	i = 0;
1407 	while (byte < last_byte) {
1408 		if (i == 0)
1409 			printf("\t");
1410 		printf("%#x", *byte++);
1411 		i++;
1412 		if (i == 8) {
1413 			printf("\n");
1414 			i = 0;
1415 		} else {
1416 			printf(", ");
1417 		}
1418 	}
1419 }
1420 #endif
1421 
1422 static int
1423 ahc_modevent(module_t mod, int type, void *data)
1424 {
1425 	/* XXX Deal with busy status on unload. */
1426 	/* XXX Deal with unknown events */
1427 	return 0;
1428 }
1429 
1430 static moduledata_t ahc_mod = {
1431 	"ahc",
1432 	ahc_modevent,
1433 	NULL
1434 };
1435 
1436 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1437 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1438 MODULE_VERSION(ahc, 1);
1439