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