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