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