xref: /freebsd/sys/dev/aic7xxx/aic79xx_osm.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Bus independent FreeBSD shim for the aic79xx based Adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2002, 2004 Justin T. Gibbs.
5  * Copyright (c) 2001-2002 Adaptec Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.c#35 $
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <dev/aic7xxx/aic79xx_osm.h>
39 #include <dev/aic7xxx/aic79xx_inline.h>
40 
41 #include <sys/kthread.h>
42 
43 #include "opt_ddb.h"
44 #ifdef DDB
45 #include <ddb/ddb.h>
46 #endif
47 
48 #ifndef AHD_TMODE_ENABLE
49 #define AHD_TMODE_ENABLE 0
50 #endif
51 
52 #include <dev/aic7xxx/aic_osm_lib.c>
53 
54 #define ccb_scb_ptr spriv_ptr0
55 
56 #if UNUSED
57 static void	ahd_dump_targcmd(struct target_cmd *cmd);
58 #endif
59 static int	ahd_modevent(module_t mod, int type, void *data);
60 static void	ahd_action(struct cam_sim *sim, union ccb *ccb);
61 static void	ahd_set_tran_settings(struct ahd_softc *ahd,
62 				      int our_id, char channel,
63 				      struct ccb_trans_settings *cts);
64 static void	ahd_get_tran_settings(struct ahd_softc *ahd,
65 				      int our_id, char channel,
66 				      struct ccb_trans_settings *cts);
67 static void	ahd_async(void *callback_arg, uint32_t code,
68 			  struct cam_path *path, void *arg);
69 static void	ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
70 				int nsegments, int error);
71 static void	ahd_poll(struct cam_sim *sim);
72 static void	ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
73 			       struct ccb_scsiio *csio, struct scb *scb);
74 static void	ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
75 			      union ccb *ccb);
76 static int	ahd_create_path(struct ahd_softc *ahd,
77 				char channel, u_int target, u_int lun,
78 				struct cam_path **path);
79 
80 static int
81 ahd_create_path(struct ahd_softc *ahd, char channel, u_int target,
82 	        u_int lun, struct cam_path **path)
83 {
84 	path_id_t path_id;
85 
86 	path_id = cam_sim_path(ahd->platform_data->sim);
87 	return (xpt_create_path(path, /*periph*/NULL,
88 				path_id, target, lun));
89 }
90 
91 int
92 ahd_map_int(struct ahd_softc *ahd)
93 {
94 	int error;
95 
96 	/* Hook up our interrupt handler */
97 	error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
98 			       INTR_TYPE_CAM, ahd_platform_intr, ahd,
99 			       &ahd->platform_data->ih);
100 	if (error != 0)
101 		device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
102 			      error);
103 	return (error);
104 }
105 
106 /*
107  * Attach all the sub-devices we can find
108  */
109 int
110 ahd_attach(struct ahd_softc *ahd)
111 {
112 	char   ahd_info[256];
113 	struct ccb_setasync csa;
114 	struct cam_devq *devq;
115 	struct cam_sim *sim;
116 	struct cam_path *path;
117 	long s;
118 	int count;
119 
120 	count = 0;
121 	devq = NULL;
122 	sim = NULL;
123 
124 	/*
125 	 * Create a thread to perform all recovery.
126 	 */
127 	if (ahd_spawn_recovery_thread(ahd) != 0)
128 		goto fail;
129 
130 	ahd_controller_info(ahd, ahd_info);
131 	printf("%s\n", ahd_info);
132 	ahd_lock(ahd, &s);
133 
134 	/*
135 	 * Create the device queue for our SIM(s).
136 	 */
137 	devq = cam_simq_alloc(AHD_MAX_QUEUE);
138 	if (devq == NULL)
139 		goto fail;
140 
141 	/*
142 	 * Construct our SIM entry
143 	 */
144 	sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
145 			    device_get_unit(ahd->dev_softc),
146 			    1, /*XXX*/256, devq);
147 	if (sim == NULL) {
148 		cam_simq_free(devq);
149 		goto fail;
150 	}
151 
152 	if (xpt_bus_register(sim, /*bus_id*/0) != CAM_SUCCESS) {
153 		cam_sim_free(sim, /*free_devq*/TRUE);
154 		sim = NULL;
155 		goto fail;
156 	}
157 
158 	if (xpt_create_path(&path, /*periph*/NULL,
159 			    cam_sim_path(sim), CAM_TARGET_WILDCARD,
160 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
161 		xpt_bus_deregister(cam_sim_path(sim));
162 		cam_sim_free(sim, /*free_devq*/TRUE);
163 		sim = NULL;
164 		goto fail;
165 	}
166 
167 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
168 	csa.ccb_h.func_code = XPT_SASYNC_CB;
169 	csa.event_enable = AC_LOST_DEVICE;
170 	csa.callback = ahd_async;
171 	csa.callback_arg = sim;
172 	xpt_action((union ccb *)&csa);
173 	count++;
174 
175 fail:
176 	ahd->platform_data->sim = sim;
177 	ahd->platform_data->path = path;
178 	if (count != 0) {
179 		/* We have to wait until after any system dumps... */
180 		ahd->platform_data->eh =
181 		    EVENTHANDLER_REGISTER(shutdown_final, ahd_shutdown,
182 					  ahd, SHUTDOWN_PRI_DEFAULT);
183 		ahd_intr_enable(ahd, TRUE);
184 	}
185 
186 	ahd_unlock(ahd, &s);
187 
188 	return (count);
189 }
190 
191 /*
192  * Catch an interrupt from the adapter
193  */
194 void
195 ahd_platform_intr(void *arg)
196 {
197 	struct	ahd_softc *ahd;
198 
199 	ahd = (struct ahd_softc *)arg;
200 	ahd_intr(ahd);
201 }
202 
203 /*
204  * We have an scb which has been processed by the
205  * adaptor, now we look to see how the operation
206  * went.
207  */
208 void
209 ahd_done(struct ahd_softc *ahd, struct scb *scb)
210 {
211 	union ccb *ccb;
212 
213 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
214 		  ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
215 
216 	ccb = scb->io_ctx;
217 	LIST_REMOVE(scb, pending_links);
218 	if ((scb->flags & SCB_TIMEDOUT) != 0)
219 		LIST_REMOVE(scb, timedout_links);
220 
221 	untimeout(ahd_platform_timeout, (caddr_t)scb, ccb->ccb_h.timeout_ch);
222 
223 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
224 		bus_dmasync_op_t op;
225 
226 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
227 			op = BUS_DMASYNC_POSTREAD;
228 		else
229 			op = BUS_DMASYNC_POSTWRITE;
230 		bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
231 		bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
232 	}
233 
234 #ifdef AHD_TARGET_MODE
235 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
236 		struct cam_path *ccb_path;
237 
238 		/*
239 		 * If we have finally disconnected, clean up our
240 		 * pending device state.
241 		 * XXX - There may be error states that cause where
242 		 *       we will remain connected.
243 		 */
244 		ccb_path = ccb->ccb_h.path;
245 		if (ahd->pending_device != NULL
246 		 && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
247 
248 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
249 				ahd->pending_device = NULL;
250 			} else {
251 				xpt_print_path(ccb->ccb_h.path);
252 				printf("Still disconnected\n");
253 				ahd_freeze_ccb(ccb);
254 			}
255 		}
256 
257 		if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
258 			ccb->ccb_h.status |= CAM_REQ_CMP;
259 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
260 		ahd_free_scb(ahd, scb);
261 		xpt_done(ccb);
262 		return;
263 	}
264 #endif
265 
266 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
267 		struct	scb *list_scb;
268 
269 		ahd->scb_data.recovery_scbs--;
270 
271 		if (aic_get_transaction_status(scb) == CAM_BDR_SENT
272 		 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
273 			aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
274 
275 		if (ahd->scb_data.recovery_scbs == 0) {
276 			/*
277 			 * All recovery actions have completed successfully,
278 			 * so reinstate the timeouts for all other pending
279 			 * commands.
280 			 */
281 			LIST_FOREACH(list_scb,
282 				     &ahd->pending_scbs, pending_links) {
283 
284 				aic_scb_timer_reset(scb, aic_get_timeout(scb));
285 			}
286 
287 			ahd_print_path(ahd, scb);
288 			printf("no longer in timeout, status = %x\n",
289 			       ccb->ccb_h.status);
290 		}
291 	}
292 
293 	/* Don't clobber any existing error state */
294 	if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
295 		ccb->ccb_h.status |= CAM_REQ_CMP;
296 	} else if ((scb->flags & SCB_SENSE) != 0) {
297 		/*
298 		 * We performed autosense retrieval.
299 		 *
300 		 * Zero any sense not transferred by the
301 		 * device.  The SCSI spec mandates that any
302 		 * untransfered data should be assumed to be
303 		 * zero.  Complete the 'bounce' of sense information
304 		 * through buffers accessible via bus-space by
305 		 * copying it into the clients csio.
306 		 */
307 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
308 		memcpy(&ccb->csio.sense_data,
309 		       ahd_get_sense_buf(ahd, scb),
310 /* XXX What size do we want to use??? */
311 			sizeof(ccb->csio.sense_data)
312 		       - ccb->csio.sense_resid);
313 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
314 	} else if ((scb->flags & SCB_PKT_SENSE) != 0) {
315 		struct scsi_status_iu_header *siu;
316 		u_int sense_len;
317 		int i;
318 
319 		/*
320 		 * Copy only the sense data into the provided buffer.
321 		 */
322 		siu = (struct scsi_status_iu_header *)scb->sense_data;
323 		sense_len = MIN(scsi_4btoul(siu->sense_length),
324 				sizeof(ccb->csio.sense_data));
325 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
326 		memcpy(&ccb->csio.sense_data,
327 		       ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
328 		       sense_len);
329 		printf("Copied %d bytes of sense data offset %d:", sense_len,
330 		       SIU_SENSE_OFFSET(siu));
331 		for (i = 0; i < sense_len; i++)
332 			printf(" 0x%x", ((uint8_t *)&ccb->csio.sense_data)[i]);
333 		printf("\n");
334 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
335 	}
336 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
337 	ahd_free_scb(ahd, scb);
338 	xpt_done(ccb);
339 }
340 
341 static void
342 ahd_action(struct cam_sim *sim, union ccb *ccb)
343 {
344 	struct	ahd_softc *ahd;
345 #ifdef AHD_TARGET_MODE
346 	struct	ahd_tmode_lstate *lstate;
347 #endif
348 	u_int	target_id;
349 	u_int	our_id;
350 	long	s;
351 
352 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
353 
354 	ahd = (struct ahd_softc *)cam_sim_softc(sim);
355 
356 	target_id = ccb->ccb_h.target_id;
357 	our_id = SIM_SCSI_ID(ahd, sim);
358 
359 	switch (ccb->ccb_h.func_code) {
360 	/* Common cases first */
361 #ifdef AHD_TARGET_MODE
362 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
363 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
364 	{
365 		struct	   ahd_tmode_tstate *tstate;
366 		cam_status status;
367 
368 		status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
369 					     &lstate, TRUE);
370 
371 		if (status != CAM_REQ_CMP) {
372 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
373 				/* Response from the black hole device */
374 				tstate = NULL;
375 				lstate = ahd->black_hole;
376 			} else {
377 				ccb->ccb_h.status = status;
378 				xpt_done(ccb);
379 				break;
380 			}
381 		}
382 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
383 
384 			ahd_lock(ahd, &s);
385 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
386 					  sim_links.sle);
387 			ccb->ccb_h.status = CAM_REQ_INPROG;
388 			if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
389 				ahd_run_tqinfifo(ahd, /*paused*/FALSE);
390 			ahd_unlock(ahd, &s);
391 			break;
392 		}
393 
394 		/*
395 		 * The target_id represents the target we attempt to
396 		 * select.  In target mode, this is the initiator of
397 		 * the original command.
398 		 */
399 		our_id = target_id;
400 		target_id = ccb->csio.init_id;
401 		/* FALLTHROUGH */
402 	}
403 #endif
404 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
405 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
406 	{
407 		struct	scb *scb;
408 		struct	hardware_scb *hscb;
409 		struct	ahd_initiator_tinfo *tinfo;
410 		struct	ahd_tmode_tstate *tstate;
411 		u_int	col_idx;
412 
413 		if ((ahd->flags & AHD_INITIATORROLE) == 0
414 		 && (ccb->ccb_h.func_code == XPT_SCSI_IO
415 		  || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
416 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
417 			xpt_done(ccb);
418 			return;
419 		}
420 
421 		/*
422 		 * get an scb to use.
423 		 */
424 		ahd_lock(ahd, &s);
425 		tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
426 					    target_id, &tstate);
427 		if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
428 		 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
429 		 || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
430 			col_idx = AHD_NEVER_COL_IDX;
431 		} else {
432 			col_idx = AHD_BUILD_COL_IDX(target_id,
433 						    ccb->ccb_h.target_lun);
434 		}
435 		if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
436 
437 			xpt_freeze_simq(sim, /*count*/1);
438 			ahd->flags |= AHD_RESOURCE_SHORTAGE;
439 			ahd_unlock(ahd, &s);
440 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
441 			xpt_done(ccb);
442 			return;
443 		}
444 		ahd_unlock(ahd, &s);
445 
446 		hscb = scb->hscb;
447 
448 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
449 			  ("start scb(%p)\n", scb));
450 		scb->io_ctx = ccb;
451 		/*
452 		 * So we can find the SCB when an abort is requested
453 		 */
454 		ccb->ccb_h.ccb_scb_ptr = scb;
455 
456 		/*
457 		 * Put all the arguments for the xfer in the scb
458 		 */
459 		hscb->control = 0;
460 		hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
461 		hscb->lun = ccb->ccb_h.target_lun;
462 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
463 			hscb->cdb_len = 0;
464 			scb->flags |= SCB_DEVICE_RESET;
465 			hscb->control |= MK_MESSAGE;
466 			hscb->task_management = SIU_TASKMGMT_LUN_RESET;
467 			ahd_execute_scb(scb, NULL, 0, 0);
468 		} else {
469 #ifdef AHD_TARGET_MODE
470 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
471 				struct target_data *tdata;
472 
473 				tdata = &hscb->shared_data.tdata;
474 				if (ahd->pending_device == lstate)
475 					scb->flags |= SCB_TARGET_IMMEDIATE;
476 				hscb->control |= TARGET_SCB;
477 				tdata->target_phases = 0;
478 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
479 					tdata->target_phases |= SPHASE_PENDING;
480 					tdata->scsi_status =
481 					    ccb->csio.scsi_status;
482 				}
483 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
484 					tdata->target_phases |= NO_DISCONNECT;
485 
486 				tdata->initiator_tag =
487 				    ahd_htole16(ccb->csio.tag_id);
488 			}
489 #endif
490 			hscb->task_management = 0;
491 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
492 				hscb->control |= ccb->csio.tag_action;
493 
494 			ahd_setup_data(ahd, sim, &ccb->csio, scb);
495 		}
496 		break;
497 	}
498 #ifdef AHD_TARGET_MODE
499 	case XPT_NOTIFY_ACK:
500 	case XPT_IMMED_NOTIFY:
501 	{
502 		struct	   ahd_tmode_tstate *tstate;
503 		struct	   ahd_tmode_lstate *lstate;
504 		cam_status status;
505 
506 		status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
507 					     &lstate, TRUE);
508 
509 		if (status != CAM_REQ_CMP) {
510 			ccb->ccb_h.status = status;
511 			xpt_done(ccb);
512 			break;
513 		}
514 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
515 				  sim_links.sle);
516 		ccb->ccb_h.status = CAM_REQ_INPROG;
517 		ahd_send_lstate_events(ahd, lstate);
518 		break;
519 	}
520 	case XPT_EN_LUN:		/* Enable LUN as a target */
521 		ahd_handle_en_lun(ahd, sim, ccb);
522 		xpt_done(ccb);
523 		break;
524 #endif
525 	case XPT_ABORT:			/* Abort the specified CCB */
526 	{
527 		ahd_abort_ccb(ahd, sim, ccb);
528 		break;
529 	}
530 	case XPT_SET_TRAN_SETTINGS:
531 	{
532 		ahd_lock(ahd, &s);
533 		ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
534 				      SIM_CHANNEL(ahd, sim), &ccb->cts);
535 		ahd_unlock(ahd, &s);
536 		xpt_done(ccb);
537 		break;
538 	}
539 	case XPT_GET_TRAN_SETTINGS:
540 	/* Get default/user set transfer settings for the target */
541 	{
542 		ahd_lock(ahd, &s);
543 		ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
544 				      SIM_CHANNEL(ahd, sim), &ccb->cts);
545 		ahd_unlock(ahd, &s);
546 		xpt_done(ccb);
547 		break;
548 	}
549 	case XPT_CALC_GEOMETRY:
550 	{
551 		aic_calc_geometry(&ccb->ccg, ahd->flags & AHD_EXTENDED_TRANS_A);
552 		xpt_done(ccb);
553 		break;
554 	}
555 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
556 	{
557 		int  found;
558 
559 		ahd_lock(ahd, &s);
560 		found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
561 					  /*initiate reset*/TRUE);
562 		ahd_unlock(ahd, &s);
563 		if (bootverbose) {
564 			xpt_print_path(SIM_PATH(ahd, sim));
565 			printf("SCSI bus reset delivered. "
566 			       "%d SCBs aborted.\n", found);
567 		}
568 		ccb->ccb_h.status = CAM_REQ_CMP;
569 		xpt_done(ccb);
570 		break;
571 	}
572 	case XPT_TERM_IO:		/* Terminate the I/O process */
573 		/* XXX Implement */
574 		ccb->ccb_h.status = CAM_REQ_INVALID;
575 		xpt_done(ccb);
576 		break;
577 	case XPT_PATH_INQ:		/* Path routing inquiry */
578 	{
579 		struct ccb_pathinq *cpi = &ccb->cpi;
580 
581 		cpi->version_num = 1; /* XXX??? */
582 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
583 		if ((ahd->features & AHD_WIDE) != 0)
584 			cpi->hba_inquiry |= PI_WIDE_16;
585 		if ((ahd->features & AHD_TARGETMODE) != 0) {
586 			cpi->target_sprt = PIT_PROCESSOR
587 					 | PIT_DISCONNECT
588 					 | PIT_TERM_IO;
589 		} else {
590 			cpi->target_sprt = 0;
591 		}
592 		cpi->hba_misc = 0;
593 		cpi->hba_eng_cnt = 0;
594 		cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
595 		cpi->max_lun = AHD_NUM_LUNS - 1;
596 		cpi->initiator_id = ahd->our_id;
597 		if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
598 			cpi->hba_misc |= PIM_NOBUSRESET;
599 		}
600 		cpi->bus_id = cam_sim_bus(sim);
601 		cpi->base_transfer_speed = 3300;
602 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
603 		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
604 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
605 		cpi->unit_number = cam_sim_unit(sim);
606 #ifdef AHD_NEW_TRAN_SETTINGS
607 		cpi->protocol = PROTO_SCSI;
608 		cpi->protocol_version = SCSI_REV_2;
609 		cpi->transport = XPORT_SPI;
610 		cpi->transport_version = 2;
611 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
612 		cpi->transport_version = 4;
613 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST;
614 #endif
615 		cpi->ccb_h.status = CAM_REQ_CMP;
616 		xpt_done(ccb);
617 		break;
618 	}
619 	default:
620 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
621 		xpt_done(ccb);
622 		break;
623 	}
624 }
625 
626 
627 static void
628 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
629 		      struct ccb_trans_settings *cts)
630 {
631 #ifdef AHD_NEW_TRAN_SETTINGS
632 	struct	  ahd_devinfo devinfo;
633 	struct	  ccb_trans_settings_scsi *scsi;
634 	struct	  ccb_trans_settings_spi *spi;
635 	struct	  ahd_initiator_tinfo *tinfo;
636 	struct	  ahd_tmode_tstate *tstate;
637 	uint16_t *discenable;
638 	uint16_t *tagenable;
639 	u_int	  update_type;
640 
641 	scsi = &cts->proto_specific.scsi;
642 	spi = &cts->xport_specific.spi;
643 	ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
644 			    cts->ccb_h.target_id,
645 			    cts->ccb_h.target_lun,
646 			    SIM_CHANNEL(ahd, sim),
647 			    ROLE_UNKNOWN);
648 	tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
649 				    devinfo.our_scsiid,
650 				    devinfo.target, &tstate);
651 	update_type = 0;
652 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
653 		update_type |= AHD_TRANS_GOAL;
654 		discenable = &tstate->discenable;
655 		tagenable = &tstate->tagenable;
656 		tinfo->curr.protocol_version = cts->protocol_version;
657 		tinfo->curr.transport_version = cts->transport_version;
658 		tinfo->goal.protocol_version = cts->protocol_version;
659 		tinfo->goal.transport_version = cts->transport_version;
660 	} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
661 		update_type |= AHD_TRANS_USER;
662 		discenable = &ahd->user_discenable;
663 		tagenable = &ahd->user_tagenable;
664 		tinfo->user.protocol_version = cts->protocol_version;
665 		tinfo->user.transport_version = cts->transport_version;
666 	} else {
667 		cts->ccb_h.status = CAM_REQ_INVALID;
668 		return;
669 	}
670 
671 	if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
672 		if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
673 			*discenable |= devinfo.target_mask;
674 		else
675 			*discenable &= ~devinfo.target_mask;
676 	}
677 
678 	if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
679 		if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
680 			*tagenable |= devinfo.target_mask;
681 		else
682 			*tagenable &= ~devinfo.target_mask;
683 	}
684 
685 	if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
686 		ahd_validate_width(ahd, /*tinfo limit*/NULL,
687 				   &spi->bus_width, ROLE_UNKNOWN);
688 		ahd_set_width(ahd, &devinfo, spi->bus_width,
689 			      update_type, /*paused*/FALSE);
690 	}
691 
692 	if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
693 		if (update_type == AHD_TRANS_USER)
694 			spi->ppr_options = tinfo->user.ppr_options;
695 		else
696 			spi->ppr_options = tinfo->goal.ppr_options;
697 	}
698 
699 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
700 		if (update_type == AHD_TRANS_USER)
701 			spi->sync_offset = tinfo->user.offset;
702 		else
703 			spi->sync_offset = tinfo->goal.offset;
704 	}
705 
706 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
707 		if (update_type == AHD_TRANS_USER)
708 			spi->sync_period = tinfo->user.period;
709 		else
710 			spi->sync_period = tinfo->goal.period;
711 	}
712 
713 	if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
714 	 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
715 		u_int	maxsync;
716 
717 		maxsync = AHD_SYNCRATE_MAX;
718 
719 		if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
720 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
721 
722 		if ((*discenable & devinfo.target_mask) == 0)
723 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
724 
725 		ahd_find_syncrate(ahd, &spi->sync_period,
726 				  &spi->ppr_options, maxsync);
727 		ahd_validate_offset(ahd, /*tinfo limit*/NULL,
728 				    spi->sync_period, &spi->sync_offset,
729 				    spi->bus_width, ROLE_UNKNOWN);
730 
731 		/* We use a period of 0 to represent async */
732 		if (spi->sync_offset == 0) {
733 			spi->sync_period = 0;
734 			spi->ppr_options = 0;
735 		}
736 
737 		ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
738 				 spi->sync_offset, spi->ppr_options,
739 				 update_type, /*paused*/FALSE);
740 	}
741 	cts->ccb_h.status = CAM_REQ_CMP;
742 #else
743 	struct	  ahd_devinfo devinfo;
744 	struct	  ahd_initiator_tinfo *tinfo;
745 	struct	  ahd_tmode_tstate *tstate;
746 	uint16_t *discenable;
747 	uint16_t *tagenable;
748 	u_int	  update_type;
749 
750 	ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
751 			    cts->ccb_h.target_id,
752 			    cts->ccb_h.target_lun,
753 			    SIM_CHANNEL(ahd, sim),
754 			    ROLE_UNKNOWN);
755 	tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
756 				    devinfo.our_scsiid,
757 				    devinfo.target, &tstate);
758 	update_type = 0;
759 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
760 		update_type |= AHD_TRANS_GOAL;
761 		discenable = &tstate->discenable;
762 		tagenable = &tstate->tagenable;
763 	} else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
764 		update_type |= AHD_TRANS_USER;
765 		discenable = &ahd->user_discenable;
766 		tagenable = &ahd->user_tagenable;
767 	} else {
768 		cts->ccb_h.status = CAM_REQ_INVALID;
769 		return;
770 	}
771 
772 	if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
773 		if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
774 			*discenable |= devinfo.target_mask;
775 		else
776 			*discenable &= ~devinfo.target_mask;
777 	}
778 
779 	if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
780 		if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
781 			*tagenable |= devinfo.target_mask;
782 		else
783 			*tagenable &= ~devinfo.target_mask;
784 	}
785 
786 	if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
787 		ahd_validate_width(ahd, /*tinfo limit*/NULL,
788 				   &cts->bus_width, ROLE_UNKNOWN);
789 		ahd_set_width(ahd, &devinfo, cts->bus_width,
790 			      update_type, /*paused*/FALSE);
791 	}
792 
793 	if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
794 		if (update_type == AHD_TRANS_USER)
795 			cts->sync_offset = tinfo->user.offset;
796 		else
797 			cts->sync_offset = tinfo->goal.offset;
798 	}
799 
800 	if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
801 		if (update_type == AHD_TRANS_USER)
802 			cts->sync_period = tinfo->user.period;
803 		else
804 			cts->sync_period = tinfo->goal.period;
805 	}
806 
807 	if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
808 	 || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
809 	 || ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
810 	 || ((cts->valid & CCB_TRANS_DISC_VALID) != 0)) {
811 		u_int ppr_options;
812 		u_int maxsync;
813 
814 		maxsync = AHD_SYNCRATE_MAX;
815 		ppr_options = 0;
816 		if (cts->sync_period <= AHD_SYNCRATE_DT
817 		 && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT) {
818 			ppr_options = tinfo->user.ppr_options
819 				    | MSG_EXT_PPR_DT_REQ;
820 		}
821 
822 		if ((*tagenable & devinfo.target_mask) == 0
823 		 || (*discenable & devinfo.target_mask) == 0)
824 			ppr_options &= ~MSG_EXT_PPR_IU_REQ;
825 
826 		ahd_find_syncrate(ahd, &cts->sync_period,
827 				  &ppr_options, maxsync);
828 		ahd_validate_offset(ahd, /*tinfo limit*/NULL,
829 				    cts->sync_period, &cts->sync_offset,
830 				    MSG_EXT_WDTR_BUS_8_BIT,
831 				    ROLE_UNKNOWN);
832 
833 		/* We use a period of 0 to represent async */
834 		if (cts->sync_offset == 0) {
835 			cts->sync_period = 0;
836 			ppr_options = 0;
837 		}
838 
839 		if (ppr_options != 0
840 		 && tinfo->user.transport_version >= 3) {
841 			tinfo->goal.transport_version =
842 			    tinfo->user.transport_version;
843 			tinfo->curr.transport_version =
844 			    tinfo->user.transport_version;
845 		}
846 
847 		ahd_set_syncrate(ahd, &devinfo, cts->sync_period,
848 				 cts->sync_offset, ppr_options,
849 				 update_type, /*paused*/FALSE);
850 	}
851 	cts->ccb_h.status = CAM_REQ_CMP;
852 #endif
853 }
854 
855 static void
856 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
857 		      struct ccb_trans_settings *cts)
858 {
859 #ifdef AHD_NEW_TRAN_SETTINGS
860 	struct	ahd_devinfo devinfo;
861 	struct	ccb_trans_settings_scsi *scsi;
862 	struct	ccb_trans_settings_spi *spi;
863 	struct	ahd_initiator_tinfo *targ_info;
864 	struct	ahd_tmode_tstate *tstate;
865 	struct	ahd_transinfo *tinfo;
866 
867 	scsi = &cts->proto_specific.scsi;
868 	spi = &cts->xport_specific.spi;
869 	ahd_compile_devinfo(&devinfo, our_id,
870 			    cts->ccb_h.target_id,
871 			    cts->ccb_h.target_lun,
872 			    channel, ROLE_UNKNOWN);
873 	targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
874 					devinfo.our_scsiid,
875 					devinfo.target, &tstate);
876 
877 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
878 		tinfo = &targ_info->curr;
879 	else
880 		tinfo = &targ_info->user;
881 
882 	scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
883 	spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
884 	if (cts->type == CTS_TYPE_USER_SETTINGS) {
885 		if ((ahd->user_discenable & devinfo.target_mask) != 0)
886 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
887 
888 		if ((ahd->user_tagenable & devinfo.target_mask) != 0)
889 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
890 	} else {
891 		if ((tstate->discenable & devinfo.target_mask) != 0)
892 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
893 
894 		if ((tstate->tagenable & devinfo.target_mask) != 0)
895 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
896 	}
897 	cts->protocol_version = tinfo->protocol_version;
898 	cts->transport_version = tinfo->transport_version;
899 
900 	spi->sync_period = tinfo->period;
901 	spi->sync_offset = tinfo->offset;
902 	spi->bus_width = tinfo->width;
903 	spi->ppr_options = tinfo->ppr_options;
904 
905 	cts->protocol = PROTO_SCSI;
906 	cts->transport = XPORT_SPI;
907 	spi->valid = CTS_SPI_VALID_SYNC_RATE
908 		   | CTS_SPI_VALID_SYNC_OFFSET
909 		   | CTS_SPI_VALID_BUS_WIDTH
910 		   | CTS_SPI_VALID_PPR_OPTIONS;
911 
912 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
913 		scsi->valid = CTS_SCSI_VALID_TQ;
914 		spi->valid |= CTS_SPI_VALID_DISC;
915 	} else {
916 		scsi->valid = 0;
917 	}
918 
919 	cts->ccb_h.status = CAM_REQ_CMP;
920 #else
921 	struct	ahd_devinfo devinfo;
922 	struct	ahd_initiator_tinfo *targ_info;
923 	struct	ahd_tmode_tstate *tstate;
924 	struct	ahd_transinfo *tinfo;
925 
926 	ahd_compile_devinfo(&devinfo, our_id,
927 			    cts->ccb_h.target_id,
928 			    cts->ccb_h.target_lun,
929 			    channel, ROLE_UNKNOWN);
930 	targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
931 					devinfo.our_scsiid,
932 					devinfo.target, &tstate);
933 
934 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
935 		tinfo = &targ_info->curr;
936 	else
937 		tinfo = &targ_info->user;
938 
939 	cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
940 	if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
941 		if ((ahd->user_discenable & devinfo.target_mask) != 0)
942 			cts->flags |= CCB_TRANS_DISC_ENB;
943 
944 		if ((ahd->user_tagenable & devinfo.target_mask) != 0)
945 			cts->flags |= CCB_TRANS_TAG_ENB;
946 	} else {
947 		if ((tstate->discenable & devinfo.target_mask) != 0)
948 			cts->flags |= CCB_TRANS_DISC_ENB;
949 
950 		if ((tstate->tagenable & devinfo.target_mask) != 0)
951 			cts->flags |= CCB_TRANS_TAG_ENB;
952 	}
953 	cts->sync_period = tinfo->period;
954 	cts->sync_offset = tinfo->offset;
955 	cts->bus_width = tinfo->width;
956 
957 	cts->valid = CCB_TRANS_SYNC_RATE_VALID
958 		   | CCB_TRANS_SYNC_OFFSET_VALID
959 		   | CCB_TRANS_BUS_WIDTH_VALID;
960 
961 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
962 		cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
963 
964 	cts->ccb_h.status = CAM_REQ_CMP;
965 #endif
966 }
967 
968 static void
969 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
970 {
971 	struct ahd_softc *ahd;
972 	struct cam_sim *sim;
973 
974 	sim = (struct cam_sim *)callback_arg;
975 	ahd = (struct ahd_softc *)cam_sim_softc(sim);
976 	switch (code) {
977 	case AC_LOST_DEVICE:
978 	{
979 		struct	ahd_devinfo devinfo;
980 		long	s;
981 
982 		ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
983 				    xpt_path_target_id(path),
984 				    xpt_path_lun_id(path),
985 				    SIM_CHANNEL(ahd, sim),
986 				    ROLE_UNKNOWN);
987 
988 		/*
989 		 * Revert to async/narrow transfers
990 		 * for the next device.
991 		 */
992 		ahd_lock(ahd, &s);
993 		ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
994 			      AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
995 		ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
996 				 /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
997 				 /*paused*/FALSE);
998 		ahd_unlock(ahd, &s);
999 		break;
1000 	}
1001 	default:
1002 		break;
1003 	}
1004 }
1005 
1006 static void
1007 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
1008 		int error)
1009 {
1010 	struct	scb *scb;
1011 	union	ccb *ccb;
1012 	struct	ahd_softc *ahd;
1013 	struct	ahd_initiator_tinfo *tinfo;
1014 	struct	ahd_tmode_tstate *tstate;
1015 	u_int	mask;
1016 	u_long	s;
1017 
1018 	scb = (struct scb *)arg;
1019 	ccb = scb->io_ctx;
1020 	ahd = scb->ahd_softc;
1021 
1022 	if (error != 0) {
1023 		if (error == EFBIG)
1024 			aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1025 		else
1026 			aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1027 		if (nsegments != 0)
1028 			bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
1029 		ahd_lock(ahd, &s);
1030 		ahd_free_scb(ahd, scb);
1031 		ahd_unlock(ahd, &s);
1032 		xpt_done(ccb);
1033 		return;
1034 	}
1035 	scb->sg_count = 0;
1036 	if (nsegments != 0) {
1037 		void *sg;
1038 		bus_dmasync_op_t op;
1039 		u_int i;
1040 
1041 		/* Copy the segments into our SG list */
1042 		for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
1043 
1044 			sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
1045 					  dm_segs->ds_len,
1046 					  /*last*/i == 1);
1047 			dm_segs++;
1048 		}
1049 
1050 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1051 			op = BUS_DMASYNC_PREREAD;
1052 		else
1053 			op = BUS_DMASYNC_PREWRITE;
1054 
1055 		bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
1056 
1057 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1058 			struct target_data *tdata;
1059 
1060 			tdata = &scb->hscb->shared_data.tdata;
1061 			tdata->target_phases |= DPHASE_PENDING;
1062 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1063 				tdata->data_phase = P_DATAOUT;
1064 			else
1065 				tdata->data_phase = P_DATAIN;
1066 		}
1067 	}
1068 
1069 	ahd_lock(ahd, &s);
1070 
1071 	/*
1072 	 * Last time we need to check if this SCB needs to
1073 	 * be aborted.
1074 	 */
1075 	if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1076 		if (nsegments != 0)
1077 			bus_dmamap_unload(ahd->buffer_dmat,
1078 					  scb->dmamap);
1079 		ahd_free_scb(ahd, scb);
1080 		ahd_unlock(ahd, &s);
1081 		xpt_done(ccb);
1082 		return;
1083 	}
1084 
1085 	tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
1086 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1087 				    SCSIID_TARGET(ahd, scb->hscb->scsiid),
1088 				    &tstate);
1089 
1090 	mask = SCB_GET_TARGET_MASK(ahd, scb);
1091 
1092 	if ((tstate->discenable & mask) != 0
1093 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1094 		scb->hscb->control |= DISCENB;
1095 
1096 	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1097 		scb->flags |= SCB_PACKETIZED;
1098 		if (scb->hscb->task_management != 0)
1099 			scb->hscb->control &= ~MK_MESSAGE;
1100 	}
1101 
1102 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1103 	 && (tinfo->goal.width != 0
1104 	  || tinfo->goal.period != 0
1105 	  || tinfo->goal.ppr_options != 0)) {
1106 		scb->flags |= SCB_NEGOTIATE;
1107 		scb->hscb->control |= MK_MESSAGE;
1108 	} else if ((tstate->auto_negotiate & mask) != 0) {
1109 		scb->flags |= SCB_AUTO_NEGOTIATE;
1110 		scb->hscb->control |= MK_MESSAGE;
1111 	}
1112 
1113 	LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1114 
1115 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1116 
1117 	aic_scb_timer_start(scb);
1118 
1119 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1120 		/* Define a mapping from our tag to the SCB. */
1121 		ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
1122 		ahd_pause(ahd);
1123 		ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1124 		ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
1125 		ahd_unpause(ahd);
1126 	} else {
1127 		ahd_queue_scb(ahd, scb);
1128 	}
1129 
1130 	ahd_unlock(ahd, &s);
1131 }
1132 
1133 static void
1134 ahd_poll(struct cam_sim *sim)
1135 {
1136 	ahd_intr(cam_sim_softc(sim));
1137 }
1138 
1139 static void
1140 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
1141 	       struct ccb_scsiio *csio, struct scb *scb)
1142 {
1143 	struct hardware_scb *hscb;
1144 	struct ccb_hdr *ccb_h;
1145 
1146 	hscb = scb->hscb;
1147 	ccb_h = &csio->ccb_h;
1148 
1149 	csio->resid = 0;
1150 	csio->sense_resid = 0;
1151 	if (ccb_h->func_code == XPT_SCSI_IO) {
1152 		hscb->cdb_len = csio->cdb_len;
1153 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1154 
1155 			if (hscb->cdb_len > MAX_CDB_LEN
1156 			 && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
1157 				u_long s;
1158 
1159 				/*
1160 				 * Should CAM start to support CDB sizes
1161 				 * greater than 16 bytes, we could use
1162 				 * the sense buffer to store the CDB.
1163 				 */
1164 				aic_set_transaction_status(scb,
1165 							   CAM_REQ_INVALID);
1166 				ahd_lock(ahd, &s);
1167 				ahd_free_scb(ahd, scb);
1168 				ahd_unlock(ahd, &s);
1169 				xpt_done((union ccb *)csio);
1170 				return;
1171 			}
1172 			if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
1173 				hscb->shared_data.idata.cdb_from_host.cdbptr =
1174 				   aic_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
1175 				hscb->shared_data.idata.cdb_from_host.cdblen =
1176 				   csio->cdb_len;
1177 				hscb->cdb_len |= SCB_CDB_LEN_PTR;
1178 			} else {
1179 				memcpy(hscb->shared_data.idata.cdb,
1180 				       csio->cdb_io.cdb_ptr,
1181 				       hscb->cdb_len);
1182 			}
1183 		} else {
1184 			if (hscb->cdb_len > MAX_CDB_LEN) {
1185 				u_long s;
1186 
1187 				aic_set_transaction_status(scb,
1188 							   CAM_REQ_INVALID);
1189 				ahd_lock(ahd, &s);
1190 				ahd_free_scb(ahd, scb);
1191 				ahd_unlock(ahd, &s);
1192 				xpt_done((union ccb *)csio);
1193 				return;
1194 			}
1195 			memcpy(hscb->shared_data.idata.cdb,
1196 			       csio->cdb_io.cdb_bytes, hscb->cdb_len);
1197 		}
1198 	}
1199 
1200 	/* Only use S/G if there is a transfer */
1201 	if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1202 		if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1203 			/* We've been given a pointer to a single buffer */
1204 			if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1205 				int s;
1206 				int error;
1207 
1208 				s = splsoftvm();
1209 				error = bus_dmamap_load(ahd->buffer_dmat,
1210 							scb->dmamap,
1211 							csio->data_ptr,
1212 							csio->dxfer_len,
1213 							ahd_execute_scb,
1214 							scb, /*flags*/0);
1215 				if (error == EINPROGRESS) {
1216 					/*
1217 					 * So as to maintain ordering,
1218 					 * freeze the controller queue
1219 					 * until our mapping is
1220 					 * returned.
1221 					 */
1222 					xpt_freeze_simq(sim,
1223 							/*count*/1);
1224 					scb->io_ctx->ccb_h.status |=
1225 					    CAM_RELEASE_SIMQ;
1226 				}
1227 				splx(s);
1228 			} else {
1229 				struct bus_dma_segment seg;
1230 
1231 				/* Pointer to physical buffer */
1232 				if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1233 					panic("ahd_setup_data - Transfer size "
1234 					      "larger than can device max");
1235 
1236 				seg.ds_addr =
1237 				    (bus_addr_t)(vm_offset_t)csio->data_ptr;
1238 				seg.ds_len = csio->dxfer_len;
1239 				ahd_execute_scb(scb, &seg, 1, 0);
1240 			}
1241 		} else {
1242 			struct bus_dma_segment *segs;
1243 
1244 			if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1245 				panic("ahd_setup_data - Physical segment "
1246 				      "pointers unsupported");
1247 
1248 			if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1249 				panic("ahd_setup_data - Virtual segment "
1250 				      "addresses unsupported");
1251 
1252 			/* Just use the segments provided */
1253 			segs = (struct bus_dma_segment *)csio->data_ptr;
1254 			ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1255 		}
1256 	} else {
1257 		ahd_execute_scb(scb, NULL, 0, 0);
1258 	}
1259 }
1260 
1261 static void
1262 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1263 {
1264 	union ccb *abort_ccb;
1265 
1266 	abort_ccb = ccb->cab.abort_ccb;
1267 	switch (abort_ccb->ccb_h.func_code) {
1268 #ifdef AHD_TARGET_MODE
1269 	case XPT_ACCEPT_TARGET_IO:
1270 	case XPT_IMMED_NOTIFY:
1271 	case XPT_CONT_TARGET_IO:
1272 	{
1273 		struct ahd_tmode_tstate *tstate;
1274 		struct ahd_tmode_lstate *lstate;
1275 		struct ccb_hdr_slist *list;
1276 		cam_status status;
1277 
1278 		status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1279 					     &lstate, TRUE);
1280 
1281 		if (status != CAM_REQ_CMP) {
1282 			ccb->ccb_h.status = status;
1283 			break;
1284 		}
1285 
1286 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1287 			list = &lstate->accept_tios;
1288 		else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1289 			list = &lstate->immed_notifies;
1290 		else
1291 			list = NULL;
1292 
1293 		if (list != NULL) {
1294 			struct ccb_hdr *curelm;
1295 			int found;
1296 
1297 			curelm = SLIST_FIRST(list);
1298 			found = 0;
1299 			if (curelm == &abort_ccb->ccb_h) {
1300 				found = 1;
1301 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1302 			} else {
1303 				while(curelm != NULL) {
1304 					struct ccb_hdr *nextelm;
1305 
1306 					nextelm =
1307 					    SLIST_NEXT(curelm, sim_links.sle);
1308 
1309 					if (nextelm == &abort_ccb->ccb_h) {
1310 						found = 1;
1311 						SLIST_NEXT(curelm,
1312 							   sim_links.sle) =
1313 						    SLIST_NEXT(nextelm,
1314 							       sim_links.sle);
1315 						break;
1316 					}
1317 					curelm = nextelm;
1318 				}
1319 			}
1320 
1321 			if (found) {
1322 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1323 				xpt_done(abort_ccb);
1324 				ccb->ccb_h.status = CAM_REQ_CMP;
1325 			} else {
1326 				xpt_print_path(abort_ccb->ccb_h.path);
1327 				printf("Not found\n");
1328 				ccb->ccb_h.status = CAM_PATH_INVALID;
1329 			}
1330 			break;
1331 		}
1332 		/* FALLTHROUGH */
1333 	}
1334 #endif
1335 	case XPT_SCSI_IO:
1336 		/* XXX Fully implement the hard ones */
1337 		ccb->ccb_h.status = CAM_UA_ABORT;
1338 		break;
1339 	default:
1340 		ccb->ccb_h.status = CAM_REQ_INVALID;
1341 		break;
1342 	}
1343 	xpt_done(ccb);
1344 }
1345 
1346 void
1347 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1348 		u_int lun, ac_code code, void *opt_arg)
1349 {
1350 	struct	ccb_trans_settings cts;
1351 	struct cam_path *path;
1352 	void *arg;
1353 	int error;
1354 
1355 	arg = NULL;
1356 	error = ahd_create_path(ahd, channel, target, lun, &path);
1357 
1358 	if (error != CAM_REQ_CMP)
1359 		return;
1360 
1361 	switch (code) {
1362 	case AC_TRANSFER_NEG:
1363 	{
1364 #ifdef AHD_NEW_TRAN_SETTINGS
1365 		struct	ccb_trans_settings_scsi *scsi;
1366 
1367 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1368 		scsi = &cts.proto_specific.scsi;
1369 #else
1370 		cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1371 #endif
1372 		cts.ccb_h.path = path;
1373 		cts.ccb_h.target_id = target;
1374 		cts.ccb_h.target_lun = lun;
1375 		ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1376 		arg = &cts;
1377 #ifdef AHD_NEW_TRAN_SETTINGS
1378 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1379 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1380 #else
1381 		cts.valid &= ~CCB_TRANS_TQ_VALID;
1382 		cts.flags &= ~CCB_TRANS_TAG_ENB;
1383 #endif
1384 		if (opt_arg == NULL)
1385 			break;
1386 		if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1387 #ifdef AHD_NEW_TRAN_SETTINGS
1388 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1389 		scsi->valid |= CTS_SCSI_VALID_TQ;
1390 #else
1391 			cts.flags |= CCB_TRANS_TAG_ENB;
1392 		cts.valid |= CCB_TRANS_TQ_VALID;
1393 #endif
1394 		break;
1395 	}
1396 	case AC_SENT_BDR:
1397 	case AC_BUS_RESET:
1398 		break;
1399 	default:
1400 		panic("ahd_send_async: Unexpected async event");
1401 	}
1402 	xpt_async(code, path, arg);
1403 	xpt_free_path(path);
1404 }
1405 
1406 void
1407 ahd_platform_set_tags(struct ahd_softc *ahd,
1408 		      struct ahd_devinfo *devinfo, int enable)
1409 {
1410 }
1411 
1412 int
1413 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1414 {
1415 	ahd->platform_data = malloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1416 	    M_NOWAIT | M_ZERO);
1417 	if (ahd->platform_data == NULL)
1418 		return (ENOMEM);
1419 	return (0);
1420 }
1421 
1422 void
1423 ahd_platform_free(struct ahd_softc *ahd)
1424 {
1425 	struct ahd_platform_data *pdata;
1426 
1427 	pdata = ahd->platform_data;
1428 	if (pdata != NULL) {
1429 		if (pdata->regs[0] != NULL)
1430 			bus_release_resource(ahd->dev_softc,
1431 					     pdata->regs_res_type[0],
1432 					     pdata->regs_res_id[0],
1433 					     pdata->regs[0]);
1434 
1435 		if (pdata->regs[1] != NULL)
1436 			bus_release_resource(ahd->dev_softc,
1437 					     pdata->regs_res_type[1],
1438 					     pdata->regs_res_id[1],
1439 					     pdata->regs[1]);
1440 
1441 		if (pdata->irq != NULL)
1442 			bus_release_resource(ahd->dev_softc,
1443 					     pdata->irq_res_type,
1444 					     0, pdata->irq);
1445 
1446 		if (pdata->sim != NULL) {
1447 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1448 			xpt_free_path(pdata->path);
1449 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1450 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1451 		}
1452 		if (pdata->eh != NULL)
1453 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1454 		free(ahd->platform_data, M_DEVBUF);
1455 	}
1456 }
1457 
1458 int
1459 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1460 {
1461 	/* We don't sort softcs under FreeBSD so report equal always */
1462 	return (0);
1463 }
1464 
1465 int
1466 ahd_detach(device_t dev)
1467 {
1468 	struct ahd_softc *ahd;
1469 	u_long l;
1470 	u_long s;
1471 
1472 	ahd_list_lock(&l);
1473 	device_printf(dev, "detaching device\n");
1474 	ahd = device_get_softc(dev);
1475 	ahd = ahd_find_softc(ahd);
1476 	if (ahd == NULL) {
1477 		device_printf(dev, "aic7xxx already detached\n");
1478 		ahd_list_unlock(&l);
1479 		return (ENOENT);
1480 	}
1481 	TAILQ_REMOVE(&ahd_tailq, ahd, links);
1482 	ahd_list_unlock(&l);
1483 	ahd_lock(ahd, &s);
1484 	ahd_intr_enable(ahd, FALSE);
1485 	bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1486 	ahd_unlock(ahd, &s);
1487 	ahd_free(ahd);
1488 	return (0);
1489 }
1490 
1491 #if UNUSED
1492 static void
1493 ahd_dump_targcmd(struct target_cmd *cmd)
1494 {
1495 	uint8_t *byte;
1496 	uint8_t *last_byte;
1497 	int i;
1498 
1499 	byte = &cmd->initiator_channel;
1500 	/* Debugging info for received commands */
1501 	last_byte = &cmd[1].initiator_channel;
1502 
1503 	i = 0;
1504 	while (byte < last_byte) {
1505 		if (i == 0)
1506 			printf("\t");
1507 		printf("%#x", *byte++);
1508 		i++;
1509 		if (i == 8) {
1510 			printf("\n");
1511 			i = 0;
1512 		} else {
1513 			printf(", ");
1514 		}
1515 	}
1516 }
1517 #endif
1518 
1519 static int
1520 ahd_modevent(module_t mod, int type, void *data)
1521 {
1522 	/* XXX Deal with busy status on unload. */
1523 	/* XXX Deal with unknown events */
1524 	return 0;
1525 }
1526 
1527 static moduledata_t ahd_mod = {
1528 	"ahd",
1529 	ahd_modevent,
1530 	NULL
1531 };
1532 
1533 /********************************** DDB Hooks *********************************/
1534 #ifdef DDB
1535 static struct ahd_softc *ahd_ddb_softc;
1536 static int ahd_ddb_paused;
1537 static int ahd_ddb_paused_on_entry;
1538 DB_COMMAND(ahd_sunit, ahd_ddb_sunit)
1539 {
1540 	struct ahd_softc *list_ahd;
1541 
1542 	ahd_ddb_softc = NULL;
1543 	TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1544 		if (list_ahd->unit == addr)
1545 			ahd_ddb_softc = list_ahd;
1546 	}
1547 	if (ahd_ddb_softc == NULL)
1548 		db_error("No matching softc found!\n");
1549 }
1550 
1551 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1552 {
1553 	if (ahd_ddb_softc == NULL) {
1554 		db_error("Must set unit with ahd_sunit first!\n");
1555 		return;
1556 	}
1557 	if (ahd_ddb_paused == 0) {
1558 		ahd_ddb_paused++;
1559 		if (ahd_is_paused(ahd_ddb_softc)) {
1560 			ahd_ddb_paused_on_entry++;
1561 			return;
1562 		}
1563 		ahd_pause(ahd_ddb_softc);
1564 	}
1565 }
1566 
1567 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1568 {
1569 	if (ahd_ddb_softc == NULL) {
1570 		db_error("Must set unit with ahd_sunit first!\n");
1571 		return;
1572 	}
1573 	if (ahd_ddb_paused != 0) {
1574 		ahd_ddb_paused = 0;
1575 		if (ahd_ddb_paused_on_entry)
1576 			return;
1577 		ahd_unpause(ahd_ddb_softc);
1578 	} else if (ahd_ddb_paused_on_entry != 0) {
1579 		/* Two unpauses to clear a paused on entry. */
1580 		ahd_ddb_paused_on_entry = 0;
1581 		ahd_unpause(ahd_ddb_softc);
1582 	}
1583 }
1584 
1585 DB_COMMAND(ahd_in, ahd_ddb_in)
1586 {
1587 	int c;
1588 	int size;
1589 
1590 	if (ahd_ddb_softc == NULL) {
1591 		db_error("Must set unit with ahd_sunit first!\n");
1592 		return;
1593 	}
1594 	if (have_addr == 0)
1595 		return;
1596 
1597 	size = 1;
1598 	while ((c = *modif++) != '\0') {
1599 		switch (c) {
1600 		case 'b':
1601 			size = 1;
1602 			break;
1603 		case 'w':
1604 			size = 2;
1605 			break;
1606 		case 'l':
1607 			size = 4;
1608 		break;
1609 		}
1610 	}
1611 
1612 	if (count <= 0)
1613 		count = 1;
1614 	while (--count >= 0) {
1615 		db_printf("%04lx (M)%x: \t", (u_long)addr,
1616 			  ahd_inb(ahd_ddb_softc, MODE_PTR));
1617 		switch (size) {
1618 		case 1:
1619 			db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1620 			break;
1621 		case 2:
1622 			db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1623 			break;
1624 		case 4:
1625 			db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1626 			break;
1627 		}
1628 	}
1629 }
1630 
1631 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1632 {
1633 	db_expr_t old_value;
1634 	db_expr_t new_value;
1635 	int	  size;
1636 
1637 	if (ahd_ddb_softc == NULL) {
1638 		db_error("Must set unit with ahd_sunit first!\n");
1639 		return;
1640 	}
1641 
1642 	switch (modif[0]) {
1643 	case '\0':
1644 	case 'b':
1645 		size = 1;
1646 		break;
1647 	case 'h':
1648 		size = 2;
1649 		break;
1650 	case 'l':
1651 		size = 4;
1652 		break;
1653 	default:
1654 		db_error("Unknown size\n");
1655 		return;
1656 	}
1657 
1658 	while (db_expression(&new_value)) {
1659 		switch (size) {
1660 		default:
1661 		case 1:
1662 			old_value = ahd_inb(ahd_ddb_softc, addr);
1663 			ahd_outb(ahd_ddb_softc, addr, new_value);
1664 			break;
1665 		case 2:
1666 			old_value = ahd_inw(ahd_ddb_softc, addr);
1667 			ahd_outw(ahd_ddb_softc, addr, new_value);
1668 			break;
1669 		case 4:
1670 			old_value = ahd_inl(ahd_ddb_softc, addr);
1671 			ahd_outl(ahd_ddb_softc, addr, new_value);
1672 			break;
1673 		}
1674 		db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1675 			  (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1676 			  (u_long)old_value, (u_long)new_value);
1677 		addr += size;
1678 	}
1679 	db_skip_to_eol();
1680 }
1681 
1682 DB_COMMAND(ahd_dump, ahd_ddb_dump)
1683 {
1684 	if (ahd_ddb_softc == NULL) {
1685 		db_error("Must set unit with ahd_sunit first!\n");
1686 		return;
1687 	}
1688 	ahd_dump_card_state(ahd_ddb_softc);
1689 }
1690 
1691 #endif
1692 
1693 
1694 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1695 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1696 MODULE_VERSION(ahd, 1);
1697