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