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