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