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