xref: /freebsd/sys/dev/aic7xxx/aic7xxx_osm.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 0
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 static int
74 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
75 	        u_int lun, struct cam_path **path)
76 {
77 	path_id_t path_id;
78 
79 	if (channel == 'B')
80 		path_id = cam_sim_path(ahc->platform_data->sim_b);
81 	else
82 		path_id = cam_sim_path(ahc->platform_data->sim);
83 
84 	return (xpt_create_path(path, /*periph*/NULL,
85 				path_id, target, lun));
86 }
87 
88 int
89 ahc_map_int(struct ahc_softc *ahc)
90 {
91 	int error;
92 	int zero;
93 	int shareable;
94 
95 	zero = 0;
96 	shareable = (ahc->flags & AHC_EDGE_INTERRUPT) ? 0: RF_SHAREABLE;
97 	ahc->platform_data->irq =
98 	    bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IRQ, &zero,
99 				   RF_ACTIVE | shareable);
100 	if (ahc->platform_data->irq == NULL) {
101 		device_printf(ahc->dev_softc,
102 			      "bus_alloc_resource() failed to allocate IRQ\n");
103 		return (ENOMEM);
104 	}
105 	ahc->platform_data->irq_res_type = SYS_RES_IRQ;
106 
107 	/* Hook up our interrupt handler */
108 	error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
109 			       INTR_TYPE_CAM|INTR_MPSAFE, NULL,
110 			       ahc_platform_intr, ahc, &ahc->platform_data->ih);
111 
112 	if (error != 0)
113 		device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
114 			      error);
115 	return (error);
116 }
117 
118 int
119 aic7770_map_registers(struct ahc_softc *ahc, u_int unused_ioport_arg)
120 {
121 	struct	resource *regs;
122 	int	rid;
123 
124 	rid = 0;
125 	regs = bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IOPORT, &rid,
126 				      RF_ACTIVE);
127 	if (regs == NULL) {
128 		device_printf(ahc->dev_softc, "Unable to map I/O space?!\n");
129 		return ENOMEM;
130 	}
131 	ahc->platform_data->regs_res_type = SYS_RES_IOPORT;
132 	ahc->platform_data->regs_res_id = rid;
133 	ahc->platform_data->regs = regs;
134 	ahc->tag = rman_get_bustag(regs);
135 	ahc->bsh = rman_get_bushandle(regs);
136 	return (0);
137 }
138 
139 /*
140  * Attach all the sub-devices we can find
141  */
142 int
143 ahc_attach(struct ahc_softc *ahc)
144 {
145 	char   ahc_info[256];
146 	struct ccb_setasync csa;
147 	struct cam_devq *devq;
148 	int bus_id;
149 	int bus_id2;
150 	struct cam_sim *sim;
151 	struct cam_sim *sim2;
152 	struct cam_path *path;
153 	struct cam_path *path2;
154 	int count;
155 
156 	count = 0;
157 	sim = NULL;
158 	sim2 = NULL;
159 	path = NULL;
160 	path2 = 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);
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 			    &ahc->platform_data->mtx, 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, ahc->dev_softc, 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 	memset(&csa, 0, sizeof(csa));
219 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
220 	csa.ccb_h.func_code = XPT_SASYNC_CB;
221 	csa.event_enable = AC_LOST_DEVICE;
222 	csa.callback = ahc_async;
223 	csa.callback_arg = sim;
224 	xpt_action((union ccb *)&csa);
225 	count++;
226 
227 	if (ahc->features & AHC_TWIN) {
228 		sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
229 				    ahc, device_get_unit(ahc->dev_softc),
230 				    &ahc->platform_data->mtx, 1,
231 				    AHC_MAX_QUEUE, devq);
232 
233 		if (sim2 == NULL) {
234 			printf("ahc_attach: Unable to attach second "
235 			       "bus due to resource shortage");
236 			goto fail;
237 		}
238 
239 		if (xpt_bus_register(sim2, ahc->dev_softc, bus_id2) !=
240 		    CAM_SUCCESS) {
241 			printf("ahc_attach: Unable to attach second "
242 			       "bus due to resource shortage");
243 			/*
244 			 * We do not want to destroy the device queue
245 			 * because the first bus is using it.
246 			 */
247 			cam_sim_free(sim2, /*free_devq*/FALSE);
248 			goto fail;
249 		}
250 
251 		if (xpt_create_path(&path2, /*periph*/NULL,
252 				    cam_sim_path(sim2),
253 				    CAM_TARGET_WILDCARD,
254 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
255 			xpt_bus_deregister(cam_sim_path(sim2));
256 			cam_sim_free(sim2, /*free_devq*/FALSE);
257 			sim2 = NULL;
258 			goto fail;
259 		}
260 		xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
261 		csa.ccb_h.func_code = XPT_SASYNC_CB;
262 		csa.event_enable = AC_LOST_DEVICE;
263 		csa.callback = ahc_async;
264 		csa.callback_arg = sim2;
265 		xpt_action((union ccb *)&csa);
266 		count++;
267 	}
268 
269 fail:
270 	if ((ahc->features & AHC_TWIN) != 0
271 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
272 		ahc->platform_data->sim_b = sim;
273 		ahc->platform_data->path_b = path;
274 		ahc->platform_data->sim = sim2;
275 		ahc->platform_data->path = path2;
276 	} else {
277 		ahc->platform_data->sim = sim;
278 		ahc->platform_data->path = path;
279 		ahc->platform_data->sim_b = sim2;
280 		ahc->platform_data->path_b = path2;
281 	}
282 	ahc_unlock(ahc);
283 
284 	if (count != 0) {
285 		/* We have to wait until after any system dumps... */
286 		ahc->platform_data->eh =
287 		    EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
288 					  ahc, SHUTDOWN_PRI_DEFAULT);
289 		ahc_intr_enable(ahc, TRUE);
290 	}
291 
292 	return (count);
293 }
294 
295 /*
296  * Catch an interrupt from the adapter
297  */
298 void
299 ahc_platform_intr(void *arg)
300 {
301 	struct	ahc_softc *ahc;
302 
303 	ahc = (struct ahc_softc *)arg;
304 	ahc_lock(ahc);
305 	ahc_intr(ahc);
306 	ahc_unlock(ahc);
307 }
308 
309 /*
310  * We have an scb which has been processed by the
311  * adaptor, now we look to see how the operation
312  * went.
313  */
314 void
315 ahc_done(struct ahc_softc *ahc, struct scb *scb)
316 {
317 	union ccb *ccb;
318 
319 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
320 		  ("ahc_done - scb %d\n", scb->hscb->tag));
321 
322 	ccb = scb->io_ctx;
323 	LIST_REMOVE(scb, pending_links);
324 	if ((scb->flags & SCB_TIMEDOUT) != 0)
325 		LIST_REMOVE(scb, timedout_links);
326 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
327 		struct scb_tailq *untagged_q;
328 		int target_offset;
329 
330 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
331 		untagged_q = &ahc->untagged_queues[target_offset];
332 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
333 		scb->flags &= ~SCB_UNTAGGEDQ;
334 		ahc_run_untagged_queue(ahc, untagged_q);
335 	}
336 
337 	callout_stop(&scb->io_timer);
338 
339 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
340 		bus_dmasync_op_t op;
341 
342 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
343 			op = BUS_DMASYNC_POSTREAD;
344 		else
345 			op = BUS_DMASYNC_POSTWRITE;
346 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
347 		bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
348 	}
349 
350 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
351 		struct cam_path *ccb_path;
352 
353 		/*
354 		 * If we have finally disconnected, clean up our
355 		 * pending device state.
356 		 * XXX - There may be error states that cause where
357 		 *       we will remain connected.
358 		 */
359 		ccb_path = ccb->ccb_h.path;
360 		if (ahc->pending_device != NULL
361 		 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
362 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
363 				ahc->pending_device = NULL;
364 			} else {
365 				if (bootverbose) {
366 					xpt_print_path(ccb->ccb_h.path);
367 					printf("Still connected\n");
368 				}
369 				aic_freeze_ccb(ccb);
370 			}
371 		}
372 
373 		if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
374 			ccb->ccb_h.status |= CAM_REQ_CMP;
375 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
376 		ahc_free_scb(ahc, scb);
377 		xpt_done(ccb);
378 		return;
379 	}
380 
381 	/*
382 	 * If the recovery SCB completes, we have to be
383 	 * out of our timeout.
384 	 */
385 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
386 		struct	scb *list_scb;
387 
388 		ahc->scb_data->recovery_scbs--;
389 
390 		if (aic_get_transaction_status(scb) == CAM_BDR_SENT
391 		 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
392 			aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
393 
394 		if (ahc->scb_data->recovery_scbs == 0) {
395 			/*
396 			 * All recovery actions have completed successfully,
397 			 * so reinstate the timeouts for all other pending
398 			 * commands.
399 			 */
400 			LIST_FOREACH(list_scb, &ahc->pending_scbs,
401 				     pending_links) {
402 				aic_scb_timer_reset(list_scb,
403 						    aic_get_timeout(scb));
404 			}
405 
406 			ahc_print_path(ahc, scb);
407 			printf("no longer in timeout, status = %x\n",
408 			       ccb->ccb_h.status);
409 		}
410 	}
411 
412 	/* Don't clobber any existing error state */
413 	if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
414 		ccb->ccb_h.status |= CAM_REQ_CMP;
415 	} else if ((scb->flags & SCB_SENSE) != 0) {
416 		/*
417 		 * We performed autosense retrieval.
418 		 *
419 		 * Zero any sense not transferred by the
420 		 * device.  The SCSI spec mandates that any
421 		 * untransfered data should be assumed to be
422 		 * zero.  Complete the 'bounce' of sense information
423 		 * through buffers accessible via bus-space by
424 		 * copying it into the clients csio.
425 		 */
426 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
427 		memcpy(&ccb->csio.sense_data,
428 		       ahc_get_sense_buf(ahc, scb),
429 		       (aic_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
430 		       - ccb->csio.sense_resid);
431 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
432 	}
433 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
434 	ahc_free_scb(ahc, scb);
435 	xpt_done(ccb);
436 }
437 
438 static void
439 ahc_action(struct cam_sim *sim, union ccb *ccb)
440 {
441 	struct	ahc_softc *ahc;
442 	struct	ahc_tmode_lstate *lstate;
443 	u_int	target_id;
444 	u_int	our_id;
445 
446 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
447 
448 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
449 
450 	target_id = ccb->ccb_h.target_id;
451 	our_id = SIM_SCSI_ID(ahc, sim);
452 
453 	switch (ccb->ccb_h.func_code) {
454 	/* Common cases first */
455 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
456 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
457 	{
458 		struct	   ahc_tmode_tstate *tstate;
459 		cam_status status;
460 
461 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
462 					     &lstate, TRUE);
463 
464 		if (status != CAM_REQ_CMP) {
465 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
466 				/* Response from the black hole device */
467 				tstate = NULL;
468 				lstate = ahc->black_hole;
469 			} else {
470 				ccb->ccb_h.status = status;
471 				xpt_done(ccb);
472 				break;
473 			}
474 		}
475 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
476 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
477 					  sim_links.sle);
478 			ccb->ccb_h.status = CAM_REQ_INPROG;
479 			if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
480 				ahc_run_tqinfifo(ahc, /*paused*/FALSE);
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 		if ((scb = ahc_get_scb(ahc)) == NULL) {
511 			xpt_freeze_simq(sim, /*count*/1);
512 			ahc->flags |= AHC_RESOURCE_SHORTAGE;
513 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
514 			xpt_done(ccb);
515 			return;
516 		}
517 
518 		hscb = scb->hscb;
519 
520 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
521 			  ("start scb(%p)\n", scb));
522 		scb->io_ctx = ccb;
523 		/*
524 		 * So we can find the SCB when an abort is requested
525 		 */
526 		ccb->ccb_h.ccb_scb_ptr = scb;
527 
528 		/*
529 		 * Put all the arguments for the xfer in the scb
530 		 */
531 		hscb->control = 0;
532 		hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
533 		hscb->lun = ccb->ccb_h.target_lun;
534 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
535 			hscb->cdb_len = 0;
536 			scb->flags |= SCB_DEVICE_RESET;
537 			hscb->control |= MK_MESSAGE;
538 			ahc_execute_scb(scb, NULL, 0, 0);
539 		} else {
540 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
541 				struct target_data *tdata;
542 
543 				tdata = &hscb->shared_data.tdata;
544 				if (ahc->pending_device == lstate)
545 					scb->flags |= SCB_TARGET_IMMEDIATE;
546 				hscb->control |= TARGET_SCB;
547 				scb->flags |= SCB_TARGET_SCB;
548 				tdata->target_phases = 0;
549 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
550 					tdata->target_phases |= SPHASE_PENDING;
551 					tdata->scsi_status =
552 					    ccb->csio.scsi_status;
553 				}
554 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
555 					tdata->target_phases |= NO_DISCONNECT;
556 
557 				tdata->initiator_tag = ccb->csio.tag_id;
558 			}
559 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
560 				hscb->control |= ccb->csio.tag_action;
561 
562 			ahc_setup_data(ahc, sim, &ccb->csio, scb);
563 		}
564 		break;
565 	}
566 	case XPT_NOTIFY_ACKNOWLEDGE:
567 	case XPT_IMMEDIATE_NOTIFY:
568 	{
569 		struct	   ahc_tmode_tstate *tstate;
570 		struct	   ahc_tmode_lstate *lstate;
571 		cam_status status;
572 
573 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
574 					     &lstate, TRUE);
575 
576 		if (status != CAM_REQ_CMP) {
577 			ccb->ccb_h.status = status;
578 			xpt_done(ccb);
579 			break;
580 		}
581 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
582 				  sim_links.sle);
583 		ccb->ccb_h.status = CAM_REQ_INPROG;
584 		ahc_send_lstate_events(ahc, lstate);
585 		break;
586 	}
587 	case XPT_EN_LUN:		/* Enable LUN as a target */
588 		ahc_handle_en_lun(ahc, sim, ccb);
589 		xpt_done(ccb);
590 		break;
591 	case XPT_ABORT:			/* Abort the specified CCB */
592 	{
593 		ahc_abort_ccb(ahc, sim, ccb);
594 		break;
595 	}
596 	case XPT_SET_TRAN_SETTINGS:
597 	{
598 		struct	ahc_devinfo devinfo;
599 		struct	ccb_trans_settings *cts;
600 		struct	ccb_trans_settings_scsi *scsi;
601 		struct	ccb_trans_settings_spi *spi;
602 		struct	ahc_initiator_tinfo *tinfo;
603 		struct	ahc_tmode_tstate *tstate;
604 		uint16_t *discenable;
605 		uint16_t *tagenable;
606 		u_int	update_type;
607 
608 		cts = &ccb->cts;
609 		scsi = &cts->proto_specific.scsi;
610 		spi = &cts->xport_specific.spi;
611 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
612 				    cts->ccb_h.target_id,
613 				    cts->ccb_h.target_lun,
614 				    SIM_CHANNEL(ahc, sim),
615 				    ROLE_UNKNOWN);
616 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
617 					    devinfo.our_scsiid,
618 					    devinfo.target, &tstate);
619 		update_type = 0;
620 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
621 			update_type |= AHC_TRANS_GOAL;
622 			discenable = &tstate->discenable;
623 			tagenable = &tstate->tagenable;
624 			tinfo->curr.protocol_version =
625 			    cts->protocol_version;
626 			tinfo->curr.transport_version =
627 			    cts->transport_version;
628 			tinfo->goal.protocol_version =
629 			    cts->protocol_version;
630 			tinfo->goal.transport_version =
631 			    cts->transport_version;
632 		} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
633 			update_type |= AHC_TRANS_USER;
634 			discenable = &ahc->user_discenable;
635 			tagenable = &ahc->user_tagenable;
636 			tinfo->user.protocol_version =
637 			    cts->protocol_version;
638 			tinfo->user.transport_version =
639 			    cts->transport_version;
640 		} else {
641 			ccb->ccb_h.status = CAM_REQ_INVALID;
642 			xpt_done(ccb);
643 			break;
644 		}
645 
646 		if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
647 			if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
648 				*discenable |= devinfo.target_mask;
649 			else
650 				*discenable &= ~devinfo.target_mask;
651 		}
652 
653 		if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
654 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
655 				*tagenable |= devinfo.target_mask;
656 			else
657 				*tagenable &= ~devinfo.target_mask;
658 		}
659 
660 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
661 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
662 					   &spi->bus_width, ROLE_UNKNOWN);
663 			ahc_set_width(ahc, &devinfo, spi->bus_width,
664 				      update_type, /*paused*/FALSE);
665 		}
666 
667 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
668 			if (update_type == AHC_TRANS_USER)
669 				spi->ppr_options = tinfo->user.ppr_options;
670 			else
671 				spi->ppr_options = tinfo->goal.ppr_options;
672 		}
673 
674 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
675 			if (update_type == AHC_TRANS_USER)
676 				spi->sync_offset = tinfo->user.offset;
677 			else
678 				spi->sync_offset = tinfo->goal.offset;
679 		}
680 
681 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
682 			if (update_type == AHC_TRANS_USER)
683 				spi->sync_period = tinfo->user.period;
684 			else
685 				spi->sync_period = tinfo->goal.period;
686 		}
687 
688 		if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
689 		 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
690 			struct ahc_syncrate *syncrate;
691 			u_int maxsync;
692 
693 			if ((ahc->features & AHC_ULTRA2) != 0)
694 				maxsync = AHC_SYNCRATE_DT;
695 			else if ((ahc->features & AHC_ULTRA) != 0)
696 				maxsync = AHC_SYNCRATE_ULTRA;
697 			else
698 				maxsync = AHC_SYNCRATE_FAST;
699 
700 			if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
701 				spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
702 
703 			syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
704 						     &spi->ppr_options,
705 						     maxsync);
706 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
707 					    syncrate, &spi->sync_offset,
708 					    spi->bus_width, ROLE_UNKNOWN);
709 
710 			/* We use a period of 0 to represent async */
711 			if (spi->sync_offset == 0) {
712 				spi->sync_period = 0;
713 				spi->ppr_options = 0;
714 			}
715 
716 			ahc_set_syncrate(ahc, &devinfo, syncrate,
717 					 spi->sync_period, spi->sync_offset,
718 					 spi->ppr_options, update_type,
719 					 /*paused*/FALSE);
720 		}
721 		ccb->ccb_h.status = CAM_REQ_CMP;
722 		xpt_done(ccb);
723 		break;
724 	}
725 	case XPT_GET_TRAN_SETTINGS:
726 	/* Get default/user set transfer settings for the target */
727 	{
728 		ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
729 				      SIM_CHANNEL(ahc, sim), &ccb->cts);
730 		xpt_done(ccb);
731 		break;
732 	}
733 	case XPT_CALC_GEOMETRY:
734 	{
735 		int extended;
736 
737 		extended = SIM_IS_SCSIBUS_B(ahc, sim)
738 			 ? ahc->flags & AHC_EXTENDED_TRANS_B
739 			 : ahc->flags & AHC_EXTENDED_TRANS_A;
740 		aic_calc_geometry(&ccb->ccg, extended);
741 		xpt_done(ccb);
742 		break;
743 	}
744 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
745 	{
746 		int  found;
747 
748 		found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
749 					  /*initiate reset*/TRUE);
750 		if (bootverbose) {
751 			xpt_print_path(SIM_PATH(ahc, sim));
752 			printf("SCSI bus reset delivered. "
753 			       "%d SCBs aborted.\n", found);
754 		}
755 		ccb->ccb_h.status = CAM_REQ_CMP;
756 		xpt_done(ccb);
757 		break;
758 	}
759 	case XPT_TERM_IO:		/* Terminate the I/O process */
760 		/* XXX Implement */
761 		ccb->ccb_h.status = CAM_REQ_INVALID;
762 		xpt_done(ccb);
763 		break;
764 	case XPT_PATH_INQ:		/* Path routing inquiry */
765 	{
766 		struct ccb_pathinq *cpi = &ccb->cpi;
767 
768 		cpi->version_num = 1; /* XXX??? */
769 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
770 		if ((ahc->features & AHC_WIDE) != 0)
771 			cpi->hba_inquiry |= PI_WIDE_16;
772 		if ((ahc->features & AHC_TARGETMODE) != 0) {
773 			cpi->target_sprt = PIT_PROCESSOR
774 					 | PIT_DISCONNECT
775 					 | PIT_TERM_IO;
776 		} else {
777 			cpi->target_sprt = 0;
778 		}
779 		cpi->hba_misc = 0;
780 		cpi->hba_eng_cnt = 0;
781 		cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
782 		cpi->max_lun = AHC_NUM_LUNS - 1;
783 		if (SIM_IS_SCSIBUS_B(ahc, sim)) {
784 			cpi->initiator_id = ahc->our_id_b;
785 			if ((ahc->flags & AHC_RESET_BUS_B) == 0)
786 				cpi->hba_misc |= PIM_NOBUSRESET;
787 		} else {
788 			cpi->initiator_id = ahc->our_id;
789 			if ((ahc->flags & AHC_RESET_BUS_A) == 0)
790 				cpi->hba_misc |= PIM_NOBUSRESET;
791 		}
792 		cpi->bus_id = cam_sim_bus(sim);
793 		cpi->base_transfer_speed = 3300;
794 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
795 		strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
796 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
797 		cpi->unit_number = cam_sim_unit(sim);
798 		cpi->protocol = PROTO_SCSI;
799 		cpi->protocol_version = SCSI_REV_2;
800 		cpi->transport = XPORT_SPI;
801 		cpi->transport_version = 2;
802 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
803 		if ((ahc->features & AHC_DT) != 0) {
804 			cpi->transport_version = 3;
805 			cpi->xport_specific.spi.ppr_options =
806 			    SID_SPI_CLOCK_DT_ST;
807 		}
808 		cpi->ccb_h.status = CAM_REQ_CMP;
809 		xpt_done(ccb);
810 		break;
811 	}
812 	default:
813 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
814 		xpt_done(ccb);
815 		break;
816 	}
817 }
818 
819 static void
820 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
821 		      struct ccb_trans_settings *cts)
822 {
823 	struct	ahc_devinfo devinfo;
824 	struct	ccb_trans_settings_scsi *scsi;
825 	struct	ccb_trans_settings_spi *spi;
826 	struct	ahc_initiator_tinfo *targ_info;
827 	struct	ahc_tmode_tstate *tstate;
828 	struct	ahc_transinfo *tinfo;
829 
830 	scsi = &cts->proto_specific.scsi;
831 	spi = &cts->xport_specific.spi;
832 	ahc_compile_devinfo(&devinfo, our_id,
833 			    cts->ccb_h.target_id,
834 			    cts->ccb_h.target_lun,
835 			    channel, ROLE_UNKNOWN);
836 	targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
837 					devinfo.our_scsiid,
838 					devinfo.target, &tstate);
839 
840 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
841 		tinfo = &targ_info->curr;
842 	else
843 		tinfo = &targ_info->user;
844 
845 	scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
846 	spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
847 	if (cts->type == CTS_TYPE_USER_SETTINGS) {
848 		if ((ahc->user_discenable & devinfo.target_mask) != 0)
849 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
850 
851 		if ((ahc->user_tagenable & devinfo.target_mask) != 0)
852 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
853 	} else {
854 		if ((tstate->discenable & devinfo.target_mask) != 0)
855 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
856 
857 		if ((tstate->tagenable & devinfo.target_mask) != 0)
858 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
859 	}
860 	cts->protocol_version = tinfo->protocol_version;
861 	cts->transport_version = tinfo->transport_version;
862 
863 	spi->sync_period = tinfo->period;
864 	spi->sync_offset = tinfo->offset;
865 	spi->bus_width = tinfo->width;
866 	spi->ppr_options = tinfo->ppr_options;
867 
868 	cts->protocol = PROTO_SCSI;
869 	cts->transport = XPORT_SPI;
870 	spi->valid = CTS_SPI_VALID_SYNC_RATE
871 		   | CTS_SPI_VALID_SYNC_OFFSET
872 		   | CTS_SPI_VALID_BUS_WIDTH
873 		   | CTS_SPI_VALID_PPR_OPTIONS;
874 
875 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
876 		scsi->valid = CTS_SCSI_VALID_TQ;
877 		spi->valid |= CTS_SPI_VALID_DISC;
878 	} else {
879 		scsi->valid = 0;
880 	}
881 
882 	cts->ccb_h.status = CAM_REQ_CMP;
883 }
884 
885 static void
886 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
887 {
888 	struct ahc_softc *ahc;
889 	struct cam_sim *sim;
890 
891 	sim = (struct cam_sim *)callback_arg;
892 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
893 	switch (code) {
894 	case AC_LOST_DEVICE:
895 	{
896 		struct	ahc_devinfo devinfo;
897 
898 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
899 				    xpt_path_target_id(path),
900 				    xpt_path_lun_id(path),
901 				    SIM_CHANNEL(ahc, sim),
902 				    ROLE_UNKNOWN);
903 
904 		/*
905 		 * Revert to async/narrow transfers
906 		 * for the next device.
907 		 */
908 		ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
909 			      AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
910 		ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
911 				 /*period*/0, /*offset*/0, /*ppr_options*/0,
912 				 AHC_TRANS_GOAL|AHC_TRANS_CUR,
913 				 /*paused*/FALSE);
914 		break;
915 	}
916 	default:
917 		break;
918 	}
919 }
920 
921 static void
922 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
923 		int error)
924 {
925 	struct	scb *scb;
926 	union	ccb *ccb;
927 	struct	ahc_softc *ahc;
928 	struct	ahc_initiator_tinfo *tinfo;
929 	struct	ahc_tmode_tstate *tstate;
930 	u_int	mask;
931 
932 	scb = (struct scb *)arg;
933 	ccb = scb->io_ctx;
934 	ahc = scb->ahc_softc;
935 
936 	if (error != 0) {
937 		if (error == EFBIG)
938 			aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
939 		else
940 			aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
941 		if (nsegments != 0)
942 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
943 		ahc_free_scb(ahc, scb);
944 		xpt_done(ccb);
945 		return;
946 	}
947 	if (nsegments != 0) {
948 		struct	  ahc_dma_seg *sg;
949 		bus_dma_segment_t *end_seg;
950 		bus_dmasync_op_t op;
951 
952 		end_seg = dm_segs + nsegments;
953 
954 		/* Copy the segments into our SG list */
955 		sg = scb->sg_list;
956 		while (dm_segs < end_seg) {
957 			uint32_t len;
958 
959 			sg->addr = aic_htole32(dm_segs->ds_addr);
960 			len = dm_segs->ds_len
961 			    | ((dm_segs->ds_addr >> 8) & 0x7F000000);
962 			sg->len = aic_htole32(len);
963 			sg++;
964 			dm_segs++;
965 		}
966 
967 		/*
968 		 * Note where to find the SG entries in bus space.
969 		 * We also set the full residual flag which the
970 		 * sequencer will clear as soon as a data transfer
971 		 * occurs.
972 		 */
973 		scb->hscb->sgptr = aic_htole32(scb->sg_list_phys|SG_FULL_RESID);
974 
975 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
976 			op = BUS_DMASYNC_PREREAD;
977 		else
978 			op = BUS_DMASYNC_PREWRITE;
979 
980 		bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
981 
982 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
983 			struct target_data *tdata;
984 
985 			tdata = &scb->hscb->shared_data.tdata;
986 			tdata->target_phases |= DPHASE_PENDING;
987 			/*
988 			 * CAM data direction is relative to the initiator.
989 			 */
990 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
991 				tdata->data_phase = P_DATAOUT;
992 			else
993 				tdata->data_phase = P_DATAIN;
994 
995 			/*
996 			 * If the transfer is of an odd length and in the
997 			 * "in" direction (scsi->HostBus), then it may
998 			 * trigger a bug in the 'WideODD' feature of
999 			 * non-Ultra2 chips.  Force the total data-length
1000 			 * to be even by adding an extra, 1 byte, SG,
1001 			 * element.  We do this even if we are not currently
1002 			 * negotiated wide as negotiation could occur before
1003 			 * this command is executed.
1004 			 */
1005 			if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1006 			 && (ccb->csio.dxfer_len & 0x1) != 0
1007 			 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1008 				nsegments++;
1009 				if (nsegments > AHC_NSEG) {
1010 					aic_set_transaction_status(scb,
1011 					    CAM_REQ_TOO_BIG);
1012 					bus_dmamap_unload(ahc->buffer_dmat,
1013 							  scb->dmamap);
1014 					ahc_free_scb(ahc, scb);
1015 					xpt_done(ccb);
1016 					return;
1017 				}
1018 				sg->addr = aic_htole32(ahc->dma_bug_buf);
1019 				sg->len = aic_htole32(1);
1020 				sg++;
1021 			}
1022 		}
1023 		sg--;
1024 		sg->len |= aic_htole32(AHC_DMA_LAST_SEG);
1025 
1026 		/* Copy the first SG into the "current" data pointer area */
1027 		scb->hscb->dataptr = scb->sg_list->addr;
1028 		scb->hscb->datacnt = scb->sg_list->len;
1029 	} else {
1030 		scb->hscb->sgptr = aic_htole32(SG_LIST_NULL);
1031 		scb->hscb->dataptr = 0;
1032 		scb->hscb->datacnt = 0;
1033 	}
1034 
1035 	scb->sg_count = nsegments;
1036 
1037 	/*
1038 	 * Last time we need to check if this SCB needs to
1039 	 * be aborted.
1040 	 */
1041 	if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1042 		if (nsegments != 0)
1043 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1044 		ahc_free_scb(ahc, scb);
1045 		xpt_done(ccb);
1046 		return;
1047 	}
1048 
1049 	tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1050 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1051 				    SCSIID_TARGET(ahc, scb->hscb->scsiid),
1052 				    &tstate);
1053 
1054 	mask = SCB_GET_TARGET_MASK(ahc, scb);
1055 	scb->hscb->scsirate = tinfo->scsirate;
1056 	scb->hscb->scsioffset = tinfo->curr.offset;
1057 	if ((tstate->ultraenb & mask) != 0)
1058 		scb->hscb->control |= ULTRAENB;
1059 
1060 	if ((tstate->discenable & mask) != 0
1061 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1062 		scb->hscb->control |= DISCENB;
1063 
1064 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1065 	 && (tinfo->goal.width != 0
1066 	  || tinfo->goal.offset != 0
1067 	  || tinfo->goal.ppr_options != 0)) {
1068 		scb->flags |= SCB_NEGOTIATE;
1069 		scb->hscb->control |= MK_MESSAGE;
1070 	} else if ((tstate->auto_negotiate & mask) != 0) {
1071 		scb->flags |= SCB_AUTO_NEGOTIATE;
1072 		scb->hscb->control |= MK_MESSAGE;
1073 	}
1074 
1075 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1076 
1077 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1078 
1079 	/*
1080 	 * We only allow one untagged transaction
1081 	 * per target in the initiator role unless
1082 	 * we are storing a full busy target *lun*
1083 	 * table in SCB space.
1084 	 */
1085 	if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1086 	 && (ahc->flags & AHC_SCB_BTT) == 0) {
1087 		struct scb_tailq *untagged_q;
1088 		int target_offset;
1089 
1090 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1091 		untagged_q = &(ahc->untagged_queues[target_offset]);
1092 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1093 		scb->flags |= SCB_UNTAGGEDQ;
1094 		if (TAILQ_FIRST(untagged_q) != scb) {
1095 			return;
1096 		}
1097 	}
1098 	scb->flags |= SCB_ACTIVE;
1099 
1100 	/*
1101 	 * Timers are disabled while recovery is in progress.
1102 	 */
1103 	aic_scb_timer_start(scb);
1104 
1105 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1106 		/* Define a mapping from our tag to the SCB. */
1107 		ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1108 		ahc_pause(ahc);
1109 		if ((ahc->flags & AHC_PAGESCBS) == 0)
1110 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1111 		ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1112 		ahc_unpause(ahc);
1113 	} else {
1114 		ahc_queue_scb(ahc, scb);
1115 	}
1116 }
1117 
1118 static void
1119 ahc_poll(struct cam_sim *sim)
1120 {
1121 	struct ahc_softc *ahc;
1122 
1123 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
1124 	ahc_intr(ahc);
1125 }
1126 
1127 static void
1128 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1129 	       struct ccb_scsiio *csio, struct scb *scb)
1130 {
1131 	struct hardware_scb *hscb;
1132 	struct ccb_hdr *ccb_h;
1133 	int error;
1134 
1135 	hscb = scb->hscb;
1136 	ccb_h = &csio->ccb_h;
1137 
1138 	csio->resid = 0;
1139 	csio->sense_resid = 0;
1140 	if (ccb_h->func_code == XPT_SCSI_IO) {
1141 		hscb->cdb_len = csio->cdb_len;
1142 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1143 			if (hscb->cdb_len > sizeof(hscb->cdb32)
1144 			 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1145 				aic_set_transaction_status(scb,
1146 							   CAM_REQ_INVALID);
1147 				ahc_free_scb(ahc, scb);
1148 				xpt_done((union ccb *)csio);
1149 				return;
1150 			}
1151 			if (hscb->cdb_len > 12) {
1152 				memcpy(hscb->cdb32,
1153 				       csio->cdb_io.cdb_ptr,
1154 				       hscb->cdb_len);
1155 				scb->flags |= SCB_CDB32_PTR;
1156 			} else {
1157 				memcpy(hscb->shared_data.cdb,
1158 				       csio->cdb_io.cdb_ptr,
1159 				       hscb->cdb_len);
1160 			}
1161 		} else {
1162 			if (hscb->cdb_len > 12) {
1163 				memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1164 				       hscb->cdb_len);
1165 				scb->flags |= SCB_CDB32_PTR;
1166 			} else {
1167 				memcpy(hscb->shared_data.cdb,
1168 				       csio->cdb_io.cdb_bytes,
1169 				       hscb->cdb_len);
1170 			}
1171 		}
1172 	}
1173 
1174 	error = bus_dmamap_load_ccb(ahc->buffer_dmat,
1175 				    scb->dmamap,
1176 				    (union ccb *)csio,
1177 				    ahc_execute_scb,
1178 				    scb,
1179 				    0);
1180 	if (error == EINPROGRESS) {
1181 		/*
1182 		 * So as to maintain ordering,
1183 		 * freeze the controller queue
1184 		 * until our mapping is
1185 		 * returned.
1186 		 */
1187 		xpt_freeze_simq(sim, /*count*/1);
1188 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1189 	}
1190 }
1191 
1192 static void
1193 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1194 {
1195 	union ccb *abort_ccb;
1196 
1197 	abort_ccb = ccb->cab.abort_ccb;
1198 	switch (abort_ccb->ccb_h.func_code) {
1199 	case XPT_ACCEPT_TARGET_IO:
1200 	case XPT_IMMEDIATE_NOTIFY:
1201 	case XPT_CONT_TARGET_IO:
1202 	{
1203 		struct ahc_tmode_tstate *tstate;
1204 		struct ahc_tmode_lstate *lstate;
1205 		struct ccb_hdr_slist *list;
1206 		cam_status status;
1207 
1208 		status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1209 					     &lstate, TRUE);
1210 
1211 		if (status != CAM_REQ_CMP) {
1212 			ccb->ccb_h.status = status;
1213 			break;
1214 		}
1215 
1216 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1217 			list = &lstate->accept_tios;
1218 		else if (abort_ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
1219 			list = &lstate->immed_notifies;
1220 		else
1221 			list = NULL;
1222 
1223 		if (list != NULL) {
1224 			struct ccb_hdr *curelm;
1225 			int found;
1226 
1227 			curelm = SLIST_FIRST(list);
1228 			found = 0;
1229 			if (curelm == &abort_ccb->ccb_h) {
1230 				found = 1;
1231 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1232 			} else {
1233 				while(curelm != NULL) {
1234 					struct ccb_hdr *nextelm;
1235 
1236 					nextelm =
1237 					    SLIST_NEXT(curelm, sim_links.sle);
1238 
1239 					if (nextelm == &abort_ccb->ccb_h) {
1240 						found = 1;
1241 						SLIST_NEXT(curelm,
1242 							   sim_links.sle) =
1243 						    SLIST_NEXT(nextelm,
1244 							       sim_links.sle);
1245 						break;
1246 					}
1247 					curelm = nextelm;
1248 				}
1249 			}
1250 
1251 			if (found) {
1252 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1253 				xpt_done(abort_ccb);
1254 				ccb->ccb_h.status = CAM_REQ_CMP;
1255 			} else {
1256 				xpt_print_path(abort_ccb->ccb_h.path);
1257 				printf("Not found\n");
1258 				ccb->ccb_h.status = CAM_PATH_INVALID;
1259 			}
1260 			break;
1261 		}
1262 		/* FALLTHROUGH */
1263 	}
1264 	case XPT_SCSI_IO:
1265 		/* XXX Fully implement the hard ones */
1266 		ccb->ccb_h.status = CAM_UA_ABORT;
1267 		break;
1268 	default:
1269 		ccb->ccb_h.status = CAM_REQ_INVALID;
1270 		break;
1271 	}
1272 	xpt_done(ccb);
1273 }
1274 
1275 void
1276 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1277 		u_int lun, ac_code code, void *opt_arg)
1278 {
1279 	struct	ccb_trans_settings cts;
1280 	struct cam_path *path;
1281 	void *arg;
1282 	int error;
1283 
1284 	arg = NULL;
1285 	error = ahc_create_path(ahc, channel, target, lun, &path);
1286 
1287 	if (error != CAM_REQ_CMP)
1288 		return;
1289 
1290 	switch (code) {
1291 	case AC_TRANSFER_NEG:
1292 	{
1293 		struct	ccb_trans_settings_scsi *scsi;
1294 
1295 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1296 		scsi = &cts.proto_specific.scsi;
1297 		cts.ccb_h.path = path;
1298 		cts.ccb_h.target_id = target;
1299 		cts.ccb_h.target_lun = lun;
1300 		ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1301 							  : ahc->our_id_b,
1302 				      channel, &cts);
1303 		arg = &cts;
1304 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1305 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1306 		if (opt_arg == NULL)
1307 			break;
1308 		if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1309 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1310 		scsi->valid |= CTS_SCSI_VALID_TQ;
1311 		break;
1312 	}
1313 	case AC_SENT_BDR:
1314 	case AC_BUS_RESET:
1315 		break;
1316 	default:
1317 		panic("ahc_send_async: Unexpected async event");
1318 	}
1319 	xpt_async(code, path, arg);
1320 	xpt_free_path(path);
1321 }
1322 
1323 void
1324 ahc_platform_set_tags(struct ahc_softc *ahc,
1325 		      struct ahc_devinfo *devinfo, int enable)
1326 {
1327 }
1328 
1329 int
1330 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1331 {
1332 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1333 	    M_NOWAIT | M_ZERO);
1334 	if (ahc->platform_data == NULL)
1335 		return (ENOMEM);
1336 	return (0);
1337 }
1338 
1339 void
1340 ahc_platform_free(struct ahc_softc *ahc)
1341 {
1342 	struct ahc_platform_data *pdata;
1343 
1344 	pdata = ahc->platform_data;
1345 	if (pdata != NULL) {
1346 		if (pdata->regs != NULL)
1347 			bus_release_resource(ahc->dev_softc,
1348 					     pdata->regs_res_type,
1349 					     pdata->regs_res_id,
1350 					     pdata->regs);
1351 
1352 		if (pdata->irq != NULL)
1353 			bus_release_resource(ahc->dev_softc,
1354 					     pdata->irq_res_type,
1355 					     0, pdata->irq);
1356 
1357 		if (pdata->sim_b != NULL) {
1358 			xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1359 			xpt_free_path(pdata->path_b);
1360 			xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1361 			cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1362 		}
1363 		if (pdata->sim != NULL) {
1364 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1365 			xpt_free_path(pdata->path);
1366 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1367 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1368 		}
1369 		if (pdata->eh != NULL)
1370 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1371 		free(ahc->platform_data, M_DEVBUF);
1372 	}
1373 }
1374 
1375 int
1376 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1377 {
1378 	/* We don't sort softcs under FreeBSD so report equal always */
1379 	return (0);
1380 }
1381 
1382 int
1383 ahc_detach(device_t dev)
1384 {
1385 	struct ahc_softc *ahc;
1386 
1387 	device_printf(dev, "detaching device\n");
1388 	ahc = device_get_softc(dev);
1389 	ahc_lock(ahc);
1390 	TAILQ_REMOVE(&ahc_tailq, ahc, links);
1391 	ahc_intr_enable(ahc, FALSE);
1392 	bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1393 	ahc_unlock(ahc);
1394 	ahc_free(ahc);
1395 	return (0);
1396 }
1397 
1398 #if 0
1399 static void
1400 ahc_dump_targcmd(struct target_cmd *cmd)
1401 {
1402 	uint8_t *byte;
1403 	uint8_t *last_byte;
1404 	int i;
1405 
1406 	byte = &cmd->initiator_channel;
1407 	/* Debugging info for received commands */
1408 	last_byte = &cmd[1].initiator_channel;
1409 
1410 	i = 0;
1411 	while (byte < last_byte) {
1412 		if (i == 0)
1413 			printf("\t");
1414 		printf("%#x", *byte++);
1415 		i++;
1416 		if (i == 8) {
1417 			printf("\n");
1418 			i = 0;
1419 		} else {
1420 			printf(", ");
1421 		}
1422 	}
1423 }
1424 #endif
1425 
1426 static int
1427 ahc_modevent(module_t mod, int type, void *data)
1428 {
1429 	/* XXX Deal with busy status on unload. */
1430 	/* XXX Deal with unknown events */
1431 	return 0;
1432 }
1433 
1434 static moduledata_t ahc_mod = {
1435 	"ahc",
1436 	ahc_modevent,
1437 	NULL
1438 };
1439 
1440 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1441 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1442 MODULE_VERSION(ahc, 1);
1443