advansys.c revision 45846
1/*
2 * Generic driver for the Advanced Systems Inc. SCSI controllers
3 * Product specific probe and attach routines can be found in:
4 *
5 * i386/isa/adv_isa.c	ABP5140, ABP542, ABP5150, ABP842, ABP852
6 * i386/eisa/adv_eisa.c	ABP742, ABP752
7 * pci/adv_pci.c	ABP920, ABP930, ABP930U, ABP930UA, ABP940, ABP940U,
8 *			ABP940UA, ABP950, ABP960, ABP960U, ABP960UA,
9 *			ABP970, ABP970U
10 *
11 * Copyright (c) 1996-1998 Justin Gibbs.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions, and the following disclaimer,
19 *    without modification, immediately at the beginning of the file.
20 * 2. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *      $Id: advansys.c,v 1.8 1999/04/07 22:59:12 gibbs Exp $
36 */
37/*
38 * Ported from:
39 * advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
40 *
41 * Copyright (c) 1995-1997 Advanced System Products, Inc.
42 * All Rights Reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that redistributions of source
46 * code retain the above copyright notice and this comment without
47 * modification.
48 */
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/malloc.h>
53#include <sys/buf.h>
54#include <sys/kernel.h>
55
56#include <machine/bus_pio.h>
57#include <machine/bus.h>
58#include <machine/clock.h>
59
60#include <cam/cam.h>
61#include <cam/cam_ccb.h>
62#include <cam/cam_sim.h>
63#include <cam/cam_xpt_sim.h>
64#include <cam/cam_xpt_periph.h>
65#include <cam/cam_debug.h>
66
67#include <cam/scsi/scsi_all.h>
68#include <cam/scsi/scsi_message.h>
69
70#include <vm/vm.h>
71#include <vm/vm_param.h>
72#include <vm/pmap.h>
73
74#include <dev/advansys/advansys.h>
75
76u_long adv_unit;
77
78static void	adv_action(struct cam_sim *sim, union ccb *ccb);
79static void	adv_execute_ccb(void *arg, bus_dma_segment_t *dm_segs,
80				int nsegments, int error);
81static void	adv_poll(struct cam_sim *sim);
82static void	adv_run_doneq(struct adv_softc *adv);
83static struct adv_ccb_info *
84		adv_alloc_ccb_info(struct adv_softc *adv);
85static void	adv_destroy_ccb_info(struct adv_softc *adv,
86				     struct adv_ccb_info *cinfo);
87static __inline struct adv_ccb_info *
88		adv_get_ccb_info(struct adv_softc *adv);
89static __inline void adv_free_ccb_info(struct adv_softc *adv,
90				       struct adv_ccb_info *cinfo);
91
92
93struct adv_softc *advsoftcs[NADV];   /* XXX Config should handle this */
94
95static __inline struct adv_ccb_info *
96adv_get_ccb_info(struct adv_softc *adv)
97{
98	struct adv_ccb_info *cinfo;
99	int opri;
100
101	opri = splcam();
102	if ((cinfo = SLIST_FIRST(&adv->free_ccb_infos)) != NULL) {
103		SLIST_REMOVE_HEAD(&adv->free_ccb_infos, links);
104	} else {
105		cinfo = adv_alloc_ccb_info(adv);
106	}
107	splx(opri);
108
109	return (cinfo);
110}
111
112static __inline void
113adv_free_ccb_info(struct adv_softc *adv, struct adv_ccb_info *cinfo)
114{
115	int opri;
116
117	opri = splcam();
118	cinfo->state = ACCB_FREE;
119	SLIST_INSERT_HEAD(&adv->free_ccb_infos, cinfo, links);
120	splx(opri);
121}
122
123void
124adv_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
125{
126	bus_addr_t* physaddr;
127
128	physaddr = (bus_addr_t*)arg;
129	*physaddr = segs->ds_addr;
130}
131
132char *
133adv_name(struct adv_softc *adv)
134{
135	static char name[10];
136
137	snprintf(name, sizeof(name), "adv%d", adv->unit);
138	return (name);
139}
140
141static void
142adv_action(struct cam_sim *sim, union ccb *ccb)
143{
144	struct adv_softc *adv;
145
146	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("adv_action\n"));
147
148	adv = (struct adv_softc *)cam_sim_softc(sim);
149
150	switch (ccb->ccb_h.func_code) {
151	/* Common cases first */
152	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
153	{
154		struct	ccb_hdr *ccb_h;
155		struct	ccb_scsiio *csio;
156		struct	adv_ccb_info *cinfo;
157
158		ccb_h = &ccb->ccb_h;
159		csio = &ccb->csio;
160		cinfo = adv_get_ccb_info(adv);
161		if (cinfo == NULL)
162			panic("XXX Handle CCB info error!!!");
163
164		ccb_h->ccb_cinfo_ptr = cinfo;
165
166		/* Only use S/G if there is a transfer */
167		if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
168			if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
169				/*
170				 * We've been given a pointer
171				 * to a single buffer
172				 */
173				if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
174					int s;
175					int error;
176
177					s = splsoftvm();
178					error =
179					    bus_dmamap_load(adv->buffer_dmat,
180							    cinfo->dmamap,
181							    csio->data_ptr,
182							    csio->dxfer_len,
183							    adv_execute_ccb,
184							    csio, /*flags*/0);
185					if (error == EINPROGRESS) {
186						/*
187						 * So as to maintain ordering,
188						 * freeze the controller queue
189						 * until our mapping is
190						 * returned.
191						 */
192						xpt_freeze_simq(adv->sim,
193								/*count*/1);
194						cinfo->state |=
195						    ACCB_RELEASE_SIMQ;
196					}
197					splx(s);
198				} else {
199					struct bus_dma_segment seg;
200
201					/* Pointer to physical buffer */
202					seg.ds_addr =
203					     (bus_addr_t)csio->data_ptr;
204					seg.ds_len = csio->dxfer_len;
205					adv_execute_ccb(csio, &seg, 1, 0);
206				}
207			} else {
208				struct bus_dma_segment *segs;
209				if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
210					panic("adv_setup_data - Physical "
211					      "segment pointers unsupported");
212
213				if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
214					panic("adv_setup_data - Virtual "
215					      "segment addresses unsupported");
216
217				/* Just use the segments provided */
218				segs = (struct bus_dma_segment *)csio->data_ptr;
219				adv_execute_ccb(ccb, segs, csio->sglist_cnt, 0);
220			}
221		} else {
222			adv_execute_ccb(ccb, NULL, 0, 0);
223		}
224		break;
225	}
226	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
227	case XPT_TARGET_IO:	/* Execute target I/O request */
228	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
229	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
230	case XPT_EN_LUN:		/* Enable LUN as a target */
231	case XPT_ABORT:			/* Abort the specified CCB */
232		/* XXX Implement */
233		ccb->ccb_h.status = CAM_REQ_INVALID;
234		xpt_done(ccb);
235		break;
236	case XPT_SET_TRAN_SETTINGS:
237	{
238		struct	 ccb_trans_settings *cts;
239		target_bit_vector targ_mask;
240		struct adv_target_transinfo *tconf;
241		u_int	 update_type;
242		int	 s;
243
244		cts = &ccb->cts;
245		targ_mask = ADV_TID_TO_TARGET_MASK(cts->ccb_h.target_id);
246		tconf = &adv->tinfo[cts->ccb_h.target_id];
247		update_type = 0;
248		if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
249			update_type |= ADV_TRANS_GOAL;
250		if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0)
251			update_type |= ADV_TRANS_USER;
252
253		s = splcam();
254
255		if ((update_type & ADV_TRANS_GOAL) != 0) {
256			if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
257				if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
258					adv->disc_enable |= targ_mask;
259				else
260					adv->disc_enable &= ~targ_mask;
261				adv_write_lram_8(adv, ADVV_DISC_ENABLE_B,
262						 adv->disc_enable);
263			}
264
265			if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
266				if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
267					adv->cmd_qng_enabled |= targ_mask;
268				else
269					adv->cmd_qng_enabled &= ~targ_mask;
270			}
271		}
272
273		if ((update_type & ADV_TRANS_USER) != 0) {
274			if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
275				if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
276					adv->user_disc_enable |= targ_mask;
277				else
278					adv->user_disc_enable &= ~targ_mask;
279			}
280
281			if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
282				if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
283					adv->user_cmd_qng_enabled |= targ_mask;
284				else
285					adv->user_cmd_qng_enabled &= ~targ_mask;
286			}
287		}
288
289		if ((cts->valid &  CCB_TRANS_SYNC_RATE_VALID) != 0) {
290			if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
291				cts->sync_offset = 0;
292
293			adv_period_offset_to_sdtr(adv, &cts->sync_period,
294						  &cts->sync_offset,
295						  cts->ccb_h.target_id);
296
297			adv_set_syncrate(adv, /*struct cam_path */NULL,
298					 cts->ccb_h.target_id, cts->sync_period,
299					 cts->sync_offset, update_type);
300		}
301		splx(s);
302		ccb->ccb_h.status = CAM_REQ_CMP;
303		xpt_done(ccb);
304		break;
305	}
306	case XPT_GET_TRAN_SETTINGS:
307	/* Get default/user set transfer settings for the target */
308	{
309		struct ccb_trans_settings *cts;
310		struct adv_transinfo *tconf;
311		target_bit_vector target_mask;
312		int s;
313
314		cts = &ccb->cts;
315		target_mask = ADV_TID_TO_TARGET_MASK(cts->ccb_h.target_id);
316
317		cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
318
319		s = splcam();
320		if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
321			tconf = &adv->tinfo[cts->ccb_h.target_id].current;
322			if ((adv->disc_enable & target_mask) != 0)
323				cts->flags |= CCB_TRANS_DISC_ENB;
324			if ((adv->cmd_qng_enabled & target_mask) != 0)
325				cts->flags |= CCB_TRANS_TAG_ENB;
326		} else {
327			tconf = &adv->tinfo[cts->ccb_h.target_id].user;
328			if ((adv->user_disc_enable & target_mask) != 0)
329				cts->flags |= CCB_TRANS_DISC_ENB;
330			if ((adv->user_cmd_qng_enabled & target_mask) != 0)
331				cts->flags |= CCB_TRANS_TAG_ENB;
332		}
333
334		cts->sync_period = tconf->period;
335		cts->sync_offset = tconf->offset;
336		splx(s);
337
338		cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
339		cts->valid = CCB_TRANS_SYNC_RATE_VALID
340			   | CCB_TRANS_SYNC_OFFSET_VALID
341			   | CCB_TRANS_BUS_WIDTH_VALID
342			   | CCB_TRANS_DISC_VALID
343			   | CCB_TRANS_TQ_VALID;
344		ccb->ccb_h.status = CAM_REQ_CMP;
345		xpt_done(ccb);
346		break;
347	}
348	case XPT_CALC_GEOMETRY:
349	{
350		struct	  ccb_calc_geometry *ccg;
351		u_int32_t size_mb;
352		u_int32_t secs_per_cylinder;
353		int	  extended;
354
355		ccg = &ccb->ccg;
356		size_mb = ccg->volume_size
357			/ ((1024L * 1024L) / ccg->block_size);
358		extended = (adv->control & ADV_CNTL_BIOS_GT_1GB) != 0;
359
360		if (size_mb > 1024 && extended) {
361			ccg->heads = 255;
362			ccg->secs_per_track = 63;
363		} else {
364			ccg->heads = 64;
365			ccg->secs_per_track = 32;
366		}
367		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
368		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
369		ccb->ccb_h.status = CAM_REQ_CMP;
370		xpt_done(ccb);
371		break;
372	}
373	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
374	{
375		int s;
376
377		s = splcam();
378		adv_stop_execution(adv);
379		adv_reset_bus(adv);
380		adv_start_execution(adv);
381		splx(s);
382
383		ccb->ccb_h.status = CAM_REQ_CMP;
384		xpt_done(ccb);
385		break;
386	}
387	case XPT_TERM_IO:		/* Terminate the I/O process */
388		/* XXX Implement */
389		ccb->ccb_h.status = CAM_REQ_INVALID;
390		xpt_done(ccb);
391		break;
392	case XPT_PATH_INQ:		/* Path routing inquiry */
393	{
394		struct ccb_pathinq *cpi = &ccb->cpi;
395
396		cpi->version_num = 1; /* XXX??? */
397		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
398		cpi->target_sprt = 0;
399		cpi->hba_misc = 0;
400		cpi->hba_eng_cnt = 0;
401		cpi->max_target = 7;
402		cpi->max_lun = 7;
403		cpi->initiator_id = adv->scsi_id;
404		cpi->bus_id = cam_sim_bus(sim);
405		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
406		strncpy(cpi->hba_vid, "Advansys", HBA_IDLEN);
407		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
408		cpi->unit_number = cam_sim_unit(sim);
409		cpi->ccb_h.status = CAM_REQ_CMP;
410		xpt_done(ccb);
411		break;
412	}
413	default:
414		ccb->ccb_h.status = CAM_REQ_INVALID;
415		xpt_done(ccb);
416		break;
417	}
418}
419
420/*
421 * Currently, the output of bus_dmammap_load suits our needs just
422 * fine, but should it change, we'd need to do something here.
423 */
424#define adv_fixup_dmasegs(adv, dm_segs) (struct adv_sg_entry *)(dm_segs)
425
426static void
427adv_execute_ccb(void *arg, bus_dma_segment_t *dm_segs,
428		int nsegments, int error)
429{
430	struct	ccb_scsiio *csio;
431	struct	ccb_hdr *ccb_h;
432	struct	cam_sim *sim;
433        struct	adv_softc *adv;
434	struct	adv_ccb_info *cinfo;
435	struct	adv_scsi_q scsiq;
436	struct	adv_sg_head sghead;
437	int	s;
438
439	csio = (struct ccb_scsiio *)arg;
440	ccb_h = &csio->ccb_h;
441	sim = xpt_path_sim(ccb_h->path);
442	adv = (struct adv_softc *)cam_sim_softc(sim);
443	cinfo = (struct adv_ccb_info *)csio->ccb_h.ccb_cinfo_ptr;
444
445	if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
446		if ((ccb_h->flags & CAM_CDB_PHYS) == 0) {
447			/* XXX Need phystovirt!!!! */
448			/* How about pmap_kenter??? */
449			scsiq.cdbptr = csio->cdb_io.cdb_ptr;
450		} else {
451			scsiq.cdbptr = csio->cdb_io.cdb_ptr;
452		}
453	} else {
454		scsiq.cdbptr = csio->cdb_io.cdb_bytes;
455	}
456	/*
457	 * Build up the request
458	 */
459	scsiq.q1.status = 0;
460	scsiq.q1.q_no = 0;
461	scsiq.q1.cntl = 0;
462	scsiq.q1.sg_queue_cnt = 0;
463	scsiq.q1.target_id = ADV_TID_TO_TARGET_MASK(ccb_h->target_id);
464	scsiq.q1.target_lun = ccb_h->target_lun;
465	scsiq.q1.sense_len = csio->sense_len;
466	scsiq.q1.extra_bytes = 0;
467	scsiq.q2.ccb_ptr = (u_int32_t)csio;
468	scsiq.q2.target_ix = ADV_TIDLUN_TO_IX(ccb_h->target_id,
469					      ccb_h->target_lun);
470	scsiq.q2.flag = 0;
471	scsiq.q2.cdb_len = csio->cdb_len;
472	if ((ccb_h->flags & CAM_TAG_ACTION_VALID) != 0)
473		scsiq.q2.tag_code = csio->tag_action;
474	else
475		scsiq.q2.tag_code = 0;
476	scsiq.q2.vm_id = 0;
477
478	if (nsegments != 0) {
479		bus_dmasync_op_t op;
480
481		scsiq.q1.data_addr = dm_segs->ds_addr;
482                scsiq.q1.data_cnt = dm_segs->ds_len;
483		if (nsegments > 1) {
484			scsiq.q1.cntl |= QC_SG_HEAD;
485			sghead.entry_cnt
486			    = sghead.entry_to_copy
487			    = nsegments;
488			sghead.res = 0;
489			sghead.sg_list = adv_fixup_dmasegs(adv, dm_segs);
490			scsiq.sg_head = &sghead;
491		} else {
492			scsiq.sg_head = NULL;
493		}
494		if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN)
495			op = BUS_DMASYNC_PREREAD;
496		else
497			op = BUS_DMASYNC_PREWRITE;
498		bus_dmamap_sync(adv->buffer_dmat, cinfo->dmamap, op);
499	} else {
500		scsiq.q1.data_addr = 0;
501		scsiq.q1.data_cnt = 0;
502		scsiq.sg_head = NULL;
503	}
504
505	s = splcam();
506
507	/*
508	 * Last time we need to check if this SCB needs to
509	 * be aborted.
510	 */
511	if (ccb_h->status != CAM_REQ_INPROG) {
512		if (nsegments != 0) {
513			bus_dmamap_unload(adv->buffer_dmat, cinfo->dmamap);
514		}
515		if ((cinfo->state & ACCB_RELEASE_SIMQ) != 0) {
516			ccb_h->status |= CAM_RELEASE_SIMQ;
517		}
518		adv_free_ccb_info(adv, cinfo);
519		xpt_done((union ccb *)csio);
520		splx(s);
521		return;
522	}
523
524	if (adv_execute_scsi_queue(adv, &scsiq, csio->dxfer_len) != 0) {
525		/* Temporary resource shortage */
526		if (nsegments != 0) {
527			bus_dmamap_unload(adv->buffer_dmat, cinfo->dmamap);
528		}
529		ccb_h->status = CAM_REQUEUE_REQ;
530		if ((cinfo->state & ACCB_RELEASE_SIMQ) != 0)
531			ccb_h->status |= CAM_RELEASE_SIMQ;
532
533		/* Unfreeze when resources are available */
534		xpt_freeze_simq(adv->sim, /*count*/1);
535
536		adv_free_ccb_info(adv, cinfo);
537		xpt_done((union ccb *)csio);
538		splx(s);
539		return;
540	}
541	cinfo->state |= ACCB_ACTIVE;
542	ccb_h->status |= CAM_SIM_QUEUED;
543	LIST_INSERT_HEAD(&adv->pending_ccbs, ccb_h, sim_links.le);
544	/* Schedule our timeout */
545	ccb_h->timeout_ch =
546	    timeout(adv_timeout, csio, (ccb_h->timeout * hz)/1000);
547	splx(s);
548}
549
550static struct adv_ccb_info *
551adv_alloc_ccb_info(struct adv_softc *adv)
552{
553	int error;
554	struct adv_ccb_info *cinfo;
555
556	cinfo = malloc(sizeof(*cinfo), M_DEVBUF, M_NOWAIT);
557	if (cinfo == NULL) {
558		printf("%s: Can't malloc CCB info\n", adv_name(adv));
559		return (NULL);
560	}
561	cinfo->state = ACCB_FREE;
562	error = bus_dmamap_create(adv->buffer_dmat, /*flags*/0,
563				  &cinfo->dmamap);
564	if (error != 0) {
565		printf("%s: Unable to allocate CCB info "
566		       "dmamap - error %d\n", adv_name(adv), error);
567		free(cinfo, M_DEVBUF);
568		cinfo = NULL;
569	}
570	return (cinfo);
571}
572
573static void
574adv_destroy_ccb_info(struct adv_softc *adv, struct adv_ccb_info *cinfo)
575{
576	bus_dmamap_destroy(adv->buffer_dmat, cinfo->dmamap);
577	free(cinfo, M_DEVBUF);
578}
579
580void
581adv_timeout(void *arg)
582{
583	int s;
584	union ccb *ccb;
585	struct adv_softc *adv;
586	struct adv_ccb_info *cinfo;
587
588	ccb = (union ccb *)arg;
589	adv = (struct adv_softc *)xpt_path_sim(ccb->ccb_h.path)->softc;
590	cinfo = (struct adv_ccb_info *)ccb->ccb_h.ccb_cinfo_ptr;
591
592	xpt_print_path(ccb->ccb_h.path);
593	printf("Timed out\n");
594
595	s = splcam();
596	/* Have we been taken care of already?? */
597	if (cinfo == NULL || cinfo->state == ACCB_FREE) {
598		splx(s);
599		return;
600	}
601
602	adv_stop_execution(adv);
603
604	if ((cinfo->state & ACCB_ABORT_QUEUED) == 0) {
605		struct ccb_hdr *ccb_h;
606
607		/*
608		 * In order to simplify the recovery process, we ask the XPT
609		 * layer to halt the queue of new transactions and we traverse
610		 * the list of pending CCBs and remove their timeouts. This
611		 * means that the driver attempts to clear only one error
612		 * condition at a time.  In general, timeouts that occur
613		 * close together are related anyway, so there is no benefit
614		 * in attempting to handle errors in parrallel.  Timeouts will
615		 * be reinstated when the recovery process ends.
616		 */
617		if ((cinfo->state & ACCB_RELEASE_SIMQ) == 0) {
618			xpt_freeze_simq(adv->sim, /*count*/1);
619			cinfo->state |= ACCB_RELEASE_SIMQ;
620		}
621
622		/* This CCB is the CCB representing our recovery actions */
623		cinfo->state |= ACCB_RECOVERY_CCB|ACCB_ABORT_QUEUED;
624
625		ccb_h = LIST_FIRST(&adv->pending_ccbs);
626		while (ccb_h != NULL) {
627			untimeout(adv_timeout, ccb_h, ccb_h->timeout_ch);
628			ccb_h = LIST_NEXT(ccb_h, sim_links.le);
629		}
630
631		/* XXX Should send a BDR */
632		/* Attempt an abort as our first tact */
633		xpt_print_path(ccb->ccb_h.path);
634		printf("Attempting abort\n");
635		adv_abort_ccb(adv, ccb->ccb_h.target_id,
636			      ccb->ccb_h.target_lun, ccb,
637			      CAM_CMD_TIMEOUT, /*queued_only*/FALSE);
638		ccb->ccb_h.timeout_ch =
639		    timeout(adv_timeout, ccb, 2 * hz);
640	} else {
641		/* Our attempt to perform an abort failed, go for a reset */
642		xpt_print_path(ccb->ccb_h.path);
643		printf("Resetting bus\n");
644		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
645		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
646		adv_reset_bus(adv);
647	}
648	adv_start_execution(adv);
649	splx(s);
650}
651
652struct adv_softc *
653adv_alloc(int unit, bus_space_tag_t tag, bus_space_handle_t bsh)
654{
655	struct	 adv_softc *adv;
656
657	if (unit >= NADV) {
658		printf("adv: unit number (%d) too high\n", unit);
659		return NULL;
660	}
661
662	/*
663	 * Allocate a storage area for us
664	 */
665	if (advsoftcs[unit]) {
666		printf("adv%d: memory already allocated\n", unit);
667		return NULL;
668	}
669
670	adv = malloc(sizeof(struct adv_softc), M_DEVBUF, M_NOWAIT);
671	if (!adv) {
672		printf("adv%d: cannot malloc!\n", unit);
673		return NULL;
674	}
675	bzero(adv, sizeof(struct adv_softc));
676	LIST_INIT(&adv->pending_ccbs);
677	SLIST_INIT(&adv->free_ccb_infos);
678	advsoftcs[unit] = adv;
679	adv->unit = unit;
680	adv->tag = tag;
681	adv->bsh = bsh;
682
683	return(adv);
684}
685
686void
687adv_free(struct adv_softc *adv)
688{
689	switch (adv->init_level) {
690	case 5:
691	{
692		struct adv_ccb_info *cinfo;
693
694		while ((cinfo = SLIST_FIRST(&adv->free_ccb_infos)) != NULL) {
695			SLIST_REMOVE_HEAD(&adv->free_ccb_infos, links);
696			adv_destroy_ccb_info(adv, cinfo);
697		}
698
699		bus_dmamap_unload(adv->sense_dmat, adv->sense_dmamap);
700	}
701	case 4:
702		bus_dmamem_free(adv->sense_dmat, adv->sense_buffers,
703                                adv->sense_dmamap);
704	case 3:
705		bus_dma_tag_destroy(adv->sense_dmat);
706	case 2:
707		bus_dma_tag_destroy(adv->buffer_dmat);
708	case 1:
709		bus_dma_tag_destroy(adv->parent_dmat);
710	case 0:
711		break;
712	}
713	free(adv, M_DEVBUF);
714}
715
716int
717adv_init(struct adv_softc *adv)
718{
719	struct	  adv_eeprom_config eeprom_config;
720	int	  checksum, i;
721	u_int16_t config_lsw;
722	u_int16_t config_msw;
723
724	adv_reset_chip_and_scsi_bus(adv);
725	adv_lib_init(adv);
726
727        /*
728         * Stop script execution.
729         */
730        adv_write_lram_16(adv, ADV_HALTCODE_W, 0x00FE);
731        adv_stop_execution(adv);
732	if (adv_is_chip_halted(adv) == 0) {
733		printf("adv%d: Unable to halt adapter. Initialization"
734		       "failed\n", adv->unit);
735		return (1);
736	}
737	ADV_OUTW(adv, ADV_REG_PROG_COUNTER, ADV_MCODE_START_ADDR);
738	if (ADV_INW(adv, ADV_REG_PROG_COUNTER) != ADV_MCODE_START_ADDR) {
739		printf("adv%d: Unable to set program counter. Initialization"
740		       "failed\n", adv->unit);
741		return (1);
742	}
743
744	config_msw = ADV_INW(adv, ADV_CONFIG_MSW);
745	config_lsw = ADV_INW(adv, ADV_CONFIG_LSW);
746
747	if ((config_msw & ADV_CFG_MSW_CLR_MASK) != 0) {
748		config_msw &= (~(ADV_CFG_MSW_CLR_MASK));
749		/*
750		 * XXX The Linux code flags this as an error,
751		 * but what should we report to the user???
752		 * It seems that clearing the config register
753		 * makes this error recoverable.
754		 */
755		ADV_OUTW(adv, ADV_CONFIG_MSW, config_msw);
756	}
757
758	/* Suck in the configuration from the EEProm */
759	checksum = adv_get_eeprom_config(adv, &eeprom_config);
760
761	eeprom_config.cfg_msw &= (~(ADV_CFG_MSW_CLR_MASK));
762
763	if (ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_AUTO_CONFIG) {
764		/*
765		 * XXX The Linux code sets a warning level for this
766		 * condition, yet nothing of meaning is printed to
767		 * the user.  What does this mean???
768		 */
769		if (adv->chip_version == 3) {
770			if (eeprom_config.cfg_lsw != config_lsw) {
771				eeprom_config.cfg_lsw =
772						ADV_INW(adv, ADV_CONFIG_LSW);
773			}
774			if (eeprom_config.cfg_msw != config_msw) {
775				eeprom_config.cfg_msw =
776						ADV_INW(adv, ADV_CONFIG_MSW);
777			}
778		}
779	}
780	eeprom_config.cfg_lsw |= ADV_CFG_LSW_HOST_INT_ON;
781	if (adv_test_external_lram(adv) == 0) {
782		/*
783		 * XXX What about non PCI cards with no
784		 *     external LRAM????
785		 */
786		if ((adv->type & (ADV_PCI|ADV_ULTRA)) == (ADV_PCI|ADV_ULTRA)) {
787			eeprom_config.max_total_qng =
788			    ADV_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
789			eeprom_config.max_tag_qng =
790			    ADV_MAX_PCI_ULTRA_INRAM_TAG_QNG;
791		} else {
792			eeprom_config.cfg_msw |= 0x0800;
793			config_msw |= 0x0800;
794			ADV_OUTW(adv, ADV_CONFIG_MSW, config_msw);
795			eeprom_config.max_total_qng =
796			     ADV_MAX_PCI_INRAM_TOTAL_QNG;
797			eeprom_config.max_tag_qng = ADV_MAX_INRAM_TAG_QNG;
798		}
799		adv->max_openings = eeprom_config.max_total_qng;
800	}
801	if (checksum == eeprom_config.chksum) {
802		/* Range/Sanity checking */
803		if (eeprom_config.max_total_qng < ADV_MIN_TOTAL_QNG) {
804			eeprom_config.max_total_qng = ADV_MIN_TOTAL_QNG;
805		}
806		if (eeprom_config.max_total_qng > ADV_MAX_TOTAL_QNG) {
807			eeprom_config.max_total_qng = ADV_MAX_TOTAL_QNG;
808		}
809		if (eeprom_config.max_tag_qng > eeprom_config.max_total_qng) {
810			eeprom_config.max_tag_qng = eeprom_config.max_total_qng;
811		}
812		if (eeprom_config.max_tag_qng < ADV_MIN_TAG_Q_PER_DVC) {
813			eeprom_config.max_tag_qng = ADV_MIN_TAG_Q_PER_DVC;
814		}
815		adv->max_openings = eeprom_config.max_total_qng;
816
817		adv->user_disc_enable = eeprom_config.disc_enable;
818		adv->user_cmd_qng_enabled = eeprom_config.use_cmd_qng;
819		adv->isa_dma_speed = EEPROM_DMA_SPEED(eeprom_config);
820		adv->scsi_id = EEPROM_SCSIID(eeprom_config) & ADV_MAX_TID;
821		EEPROM_SET_SCSIID(eeprom_config, adv->scsi_id);
822		adv->control = eeprom_config.cntl;
823		for (i = 0; i <= ADV_MAX_TID; i++)
824			adv_sdtr_to_period_offset(adv,
825						  eeprom_config.sdtr_data[i],
826						  &adv->tinfo[i].user.period,
827						  &adv->tinfo[i].user.offset,
828						  i);
829	} else {
830		u_int8_t sync_data;
831
832		printf("adv%d: Warning EEPROM Checksum mismatch. "
833		       "Using default device parameters\n", adv->unit);
834
835		/* Set reasonable defaults since we can't read the EEPROM */
836		adv->isa_dma_speed = /*ADV_DEF_ISA_DMA_SPEED*/1;
837		adv->max_openings = ADV_DEF_MAX_TOTAL_QNG;
838		adv->disc_enable = TARGET_BIT_VECTOR_SET;
839		adv->user_disc_enable = TARGET_BIT_VECTOR_SET;
840		adv->cmd_qng_enabled = TARGET_BIT_VECTOR_SET;
841		adv->user_cmd_qng_enabled = TARGET_BIT_VECTOR_SET;
842		adv->scsi_id = 7;
843
844		sync_data = ADV_DEF_SDTR_OFFSET | (ADV_DEF_SDTR_INDEX << 4);
845		for (i = 0; i <= ADV_MAX_TID; i++)
846			adv_sdtr_to_period_offset(adv, sync_data,
847						  &adv->tinfo[i].user.period,
848						  &adv->tinfo[i].user.offset,
849						  i);
850	}
851
852	if (adv_set_eeprom_config(adv, &eeprom_config) != 0)
853		printf("%s: WARNING! Failure writing to EEPROM.\n",
854		       adv_name(adv));
855
856	adv_set_chip_scsiid(adv, adv->scsi_id);
857	if (adv_init_lram_and_mcode(adv))
858		return (1);
859
860	adv->disc_enable = adv->user_disc_enable;
861
862	adv_write_lram_8(adv, ADVV_DISC_ENABLE_B, adv->disc_enable);
863	for (i = 0; i <= ADV_MAX_TID; i++) {
864		/*
865		 * Start off in async mode.
866		 */
867		adv_set_syncrate(adv, /*struct cam_path */NULL,
868				 i, /*period*/0, /*offset*/0,
869				 ADV_TRANS_CUR);
870		/*
871		 * Enable the use of tagged commands on all targets.
872		 * This allows the kernel driver to make up it's own mind
873		 * as it sees fit to tag queue instead of having the
874		 * firmware try and second guess the tag_code settins.
875		 */
876		adv_write_lram_8(adv, ADVV_MAX_DVC_QNG_BEG + i,
877				 adv->max_openings);
878	}
879	adv_write_lram_8(adv, ADVV_USE_TAGGED_QNG_B, TARGET_BIT_VECTOR_SET);
880	adv_write_lram_8(adv, ADVV_CAN_TAGGED_QNG_B, TARGET_BIT_VECTOR_SET);
881	printf("adv%d: AdvanSys %s Host Adapter, SCSI ID %d, queue depth %d\n",
882	       adv->unit, (adv->type & ADV_ULTRA) ? "Ultra SCSI" : "SCSI",
883	       adv->scsi_id, adv->max_openings);
884	return (0);
885}
886
887void
888adv_intr(void *arg)
889{
890	struct	  adv_softc *adv;
891	u_int16_t chipstat;
892	u_int16_t saved_ram_addr;
893	u_int8_t  ctrl_reg;
894	u_int8_t  saved_ctrl_reg;
895	u_int8_t  host_flag;
896
897	adv = (struct adv_softc *)arg;
898
899	ctrl_reg = ADV_INB(adv, ADV_CHIP_CTRL);
900	saved_ctrl_reg = ctrl_reg & (~(ADV_CC_SCSI_RESET | ADV_CC_CHIP_RESET |
901				       ADV_CC_SINGLE_STEP | ADV_CC_DIAG |
902				       ADV_CC_TEST));
903
904
905	if ((chipstat = ADV_INW(adv, ADV_CHIP_STATUS)) & ADV_CSW_INT_PENDING) {
906
907		saved_ram_addr = ADV_INW(adv, ADV_LRAM_ADDR);
908		host_flag = adv_read_lram_8(adv, ADVV_HOST_FLAG_B);
909		adv_write_lram_8(adv, ADVV_HOST_FLAG_B,
910				 host_flag | ADV_HOST_FLAG_IN_ISR);
911
912		adv_ack_interrupt(adv);
913
914		if ((chipstat & ADV_CSW_HALTED)
915		    && (ctrl_reg & ADV_CC_SINGLE_STEP)) {
916			adv_isr_chip_halted(adv);
917			saved_ctrl_reg &= ~ADV_CC_HALT;
918		} else {
919			adv_run_doneq(adv);
920		}
921		ADV_OUTW(adv, ADV_LRAM_ADDR, saved_ram_addr);
922#ifdef DIAGNOSTIC
923		if (ADV_INW(adv, ADV_LRAM_ADDR) != saved_ram_addr)
924			panic("adv_intr: Unable to set LRAM addr");
925#endif
926		adv_write_lram_8(adv, ADVV_HOST_FLAG_B, host_flag);
927	}
928
929	ADV_OUTB(adv, ADV_CHIP_CTRL, saved_ctrl_reg);
930}
931
932void
933adv_run_doneq(struct adv_softc *adv)
934{
935	struct adv_q_done_info scsiq;
936	u_int		  doneq_head;
937	u_int		  done_qno;
938
939	doneq_head = adv_read_lram_16(adv, ADVV_DONE_Q_TAIL_W) & 0xFF;
940	done_qno = adv_read_lram_8(adv, ADV_QNO_TO_QADDR(doneq_head)
941				   + ADV_SCSIQ_B_FWD);
942	while (done_qno != ADV_QLINK_END) {
943		union ccb* ccb;
944		u_int done_qaddr;
945		u_int sg_queue_cnt;
946		int   aborted;
947
948		done_qaddr = ADV_QNO_TO_QADDR(done_qno);
949
950		/* Pull status from this request */
951		sg_queue_cnt = adv_copy_lram_doneq(adv, done_qaddr, &scsiq,
952						   adv->max_dma_count);
953
954		/* Mark it as free */
955		adv_write_lram_8(adv, done_qaddr + ADV_SCSIQ_B_STATUS,
956				 scsiq.q_status & ~(QS_READY|QS_ABORTED));
957
958		/* Process request based on retrieved info */
959		if ((scsiq.cntl & QC_SG_HEAD) != 0) {
960			u_int i;
961
962			/*
963			 * S/G based request.  Free all of the queue
964			 * structures that contained S/G information.
965			 */
966			for (i = 0; i < sg_queue_cnt; i++) {
967				done_qno = adv_read_lram_8(adv, done_qaddr
968							   + ADV_SCSIQ_B_FWD);
969
970#ifdef DIAGNOSTIC
971				if (done_qno == ADV_QLINK_END) {
972					panic("adv_qdone: Corrupted SG "
973					      "list encountered");
974				}
975#endif
976				done_qaddr = ADV_QNO_TO_QADDR(done_qno);
977
978				/* Mark SG queue as free */
979				adv_write_lram_8(adv, done_qaddr
980						 + ADV_SCSIQ_B_STATUS, QS_FREE);
981			}
982		} else
983			sg_queue_cnt = 0;
984#ifdef DIAGNOSTIC
985		if (adv->cur_active < (sg_queue_cnt + 1))
986			panic("adv_qdone: Attempting to free more "
987			      "queues than are active");
988#endif
989		adv->cur_active -= sg_queue_cnt + 1;
990
991		aborted = (scsiq.q_status & QS_ABORTED) != 0;
992
993		if ((scsiq.q_status != QS_DONE)
994		 && (scsiq.q_status & QS_ABORTED) == 0)
995			panic("adv_qdone: completed scsiq with unknown status");
996
997		scsiq.remain_bytes += scsiq.extra_bytes;
998
999		if ((scsiq.d3.done_stat == QD_WITH_ERROR) &&
1000		    (scsiq.d3.host_stat == QHSTA_M_DATA_OVER_RUN)) {
1001			if ((scsiq.cntl & (QC_DATA_IN|QC_DATA_OUT)) == 0) {
1002				scsiq.d3.done_stat = QD_NO_ERROR;
1003				scsiq.d3.host_stat = QHSTA_NO_ERROR;
1004			}
1005		}
1006
1007		ccb = (union ccb *)scsiq.d2.ccb_ptr;
1008		ccb->csio.resid = scsiq.remain_bytes;
1009		adv_done(adv, (union ccb *)scsiq.d2.ccb_ptr,
1010			 scsiq.d3.done_stat, scsiq.d3.host_stat,
1011			 scsiq.d3.scsi_stat, scsiq.q_no);
1012
1013		doneq_head = done_qno;
1014		done_qno = adv_read_lram_8(adv, done_qaddr + ADV_SCSIQ_B_FWD);
1015	}
1016	adv_write_lram_16(adv, ADVV_DONE_Q_TAIL_W, doneq_head);
1017}
1018
1019
1020void
1021adv_done(struct adv_softc *adv, union ccb *ccb, u_int done_stat,
1022	 u_int host_stat, u_int scsi_status, u_int q_no)
1023{
1024	struct	   adv_ccb_info *cinfo;
1025
1026	cinfo = (struct adv_ccb_info *)ccb->ccb_h.ccb_cinfo_ptr;
1027	/*
1028	 * Null this out so that we catch driver bugs that cause a
1029	 * ccb to be completed twice.
1030	 */
1031	ccb->ccb_h.ccb_cinfo_ptr = NULL;
1032
1033	LIST_REMOVE(&ccb->ccb_h, sim_links.le);
1034	untimeout(adv_timeout, ccb, ccb->ccb_h.timeout_ch);
1035	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1036		bus_dmasync_op_t op;
1037
1038		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1039			op = BUS_DMASYNC_POSTREAD;
1040		else
1041			op = BUS_DMASYNC_POSTWRITE;
1042		bus_dmamap_sync(adv->buffer_dmat, cinfo->dmamap, op);
1043		bus_dmamap_unload(adv->buffer_dmat, cinfo->dmamap);
1044	}
1045
1046	switch (done_stat) {
1047	case QD_NO_ERROR:
1048		if (host_stat == QHSTA_NO_ERROR) {
1049			ccb->ccb_h.status = CAM_REQ_CMP;
1050			break;
1051		}
1052		xpt_print_path(ccb->ccb_h.path);
1053		printf("adv_done - queue done without error, "
1054		       "but host status non-zero(%x)\n", host_stat);
1055		/*FALLTHROUGH*/
1056	case QD_WITH_ERROR:
1057		switch (host_stat) {
1058		case QHSTA_M_TARGET_STATUS_BUSY:
1059		case QHSTA_M_BAD_QUEUE_FULL_OR_BUSY:
1060			/*
1061			 * Assume that if we were a tagged transaction
1062			 * the target reported queue full.  Otherwise,
1063			 * report busy.  The firmware really should just
1064			 * pass the original status back up to us even
1065			 * if it thinks the target was in error for
1066			 * returning this status as no other transactions
1067			 * from this initiator are in effect, but this
1068			 * ignores multi-initiator setups and there is
1069			 * evidence that the firmware gets its per-device
1070			 * transaction counts screwed up occassionally.
1071			 */
1072			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1073			if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
1074			 && host_stat != QHSTA_M_TARGET_STATUS_BUSY)
1075				scsi_status = SCSI_STATUS_QUEUE_FULL;
1076			else
1077				scsi_status = SCSI_STATUS_BUSY;
1078			adv_abort_ccb(adv, ccb->ccb_h.target_id,
1079				      ccb->ccb_h.target_lun,
1080				      /*ccb*/NULL, CAM_REQUEUE_REQ,
1081				      /*queued_only*/TRUE);
1082			/*FALLTHROUGH*/
1083		case QHSTA_M_NO_AUTO_REQ_SENSE:
1084		case QHSTA_NO_ERROR:
1085			ccb->csio.scsi_status = scsi_status;
1086			switch (scsi_status) {
1087			case SCSI_STATUS_CHECK_COND:
1088			case SCSI_STATUS_CMD_TERMINATED:
1089				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1090				/* Structure copy */
1091				ccb->csio.sense_data =
1092				    adv->sense_buffers[q_no - 1];
1093				/* FALLTHROUGH */
1094			case SCSI_STATUS_BUSY:
1095			case SCSI_STATUS_RESERV_CONFLICT:
1096			case SCSI_STATUS_QUEUE_FULL:
1097			case SCSI_STATUS_COND_MET:
1098			case SCSI_STATUS_INTERMED:
1099			case SCSI_STATUS_INTERMED_COND_MET:
1100				ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1101				break;
1102			case SCSI_STATUS_OK:
1103				ccb->ccb_h.status |= CAM_REQ_CMP;
1104				break;
1105			}
1106			break;
1107		case QHSTA_M_SEL_TIMEOUT:
1108			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1109			break;
1110		case QHSTA_M_DATA_OVER_RUN:
1111			ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1112			break;
1113		case QHSTA_M_UNEXPECTED_BUS_FREE:
1114			ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1115			break;
1116		case QHSTA_M_BAD_BUS_PHASE_SEQ:
1117			ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1118			break;
1119		case QHSTA_M_BAD_CMPL_STATUS_IN:
1120			/* No command complete after a status message */
1121			ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1122			break;
1123		case QHSTA_D_EXE_SCSI_Q_BUSY_TIMEOUT:
1124		case QHSTA_M_WTM_TIMEOUT:
1125		case QHSTA_M_HUNG_REQ_SCSI_BUS_RESET:
1126			/* The SCSI bus hung in a phase */
1127			ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1128			adv_reset_bus(adv);
1129			break;
1130		case QHSTA_M_AUTO_REQ_SENSE_FAIL:
1131			ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1132			break;
1133		case QHSTA_D_QDONE_SG_LIST_CORRUPTED:
1134		case QHSTA_D_ASC_DVC_ERROR_CODE_SET:
1135		case QHSTA_D_HOST_ABORT_FAILED:
1136		case QHSTA_D_EXE_SCSI_Q_FAILED:
1137		case QHSTA_D_ASPI_NO_BUF_POOL:
1138		case QHSTA_M_BAD_TAG_CODE:
1139		case QHSTA_D_LRAM_CMP_ERROR:
1140		case QHSTA_M_MICRO_CODE_ERROR_HALT:
1141		default:
1142			panic("%s: Unhandled Host status error %x",
1143			      adv_name(adv), host_stat);
1144			/* NOTREACHED */
1145		}
1146		break;
1147
1148	case QD_ABORTED_BY_HOST:
1149		/* Don't clobber any, more explicit, error codes we've set */
1150		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1151			ccb->ccb_h.status = CAM_REQ_ABORTED;
1152		break;
1153
1154	default:
1155		xpt_print_path(ccb->ccb_h.path);
1156		printf("adv_done - queue done with unknown status %x:%x\n",
1157		       done_stat, host_stat);
1158		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1159		break;
1160	}
1161	if ((cinfo->state & ACCB_RELEASE_SIMQ) != 0)
1162		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1163	else if (adv->openings_needed > 0) {
1164		int openings;
1165
1166		openings = adv->max_openings - adv->cur_active - ADV_MIN_FREE_Q;
1167		if (openings >= adv->openings_needed) {
1168			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1169			adv->openings_needed = 0;
1170		}
1171	}
1172	if ((cinfo->state & ACCB_RECOVERY_CCB) != 0) {
1173		/*
1174		 * We now traverse our list of pending CCBs and reinstate
1175		 * their timeouts.
1176		 */
1177		struct ccb_hdr *ccb_h;
1178
1179		ccb_h = LIST_FIRST(&adv->pending_ccbs);
1180		while (ccb_h != NULL) {
1181			ccb_h->timeout_ch =
1182			    timeout(adv_timeout, (caddr_t)ccb_h,
1183					    (ccb_h->timeout * hz) / 1000);
1184			ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1185		}
1186		printf("%s: No longer in timeout\n", adv_name(adv));
1187	}
1188	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP
1189	 && (ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1190		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1191		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1192	}
1193	adv_free_ccb_info(adv, cinfo);
1194	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1195	xpt_done(ccb);
1196}
1197
1198/*
1199 * Function to poll for command completion when
1200 * interrupts are disabled (crash dumps)
1201 */
1202static void
1203adv_poll(struct cam_sim *sim)
1204{
1205	adv_intr(cam_sim_softc(sim));
1206}
1207
1208/*
1209 * Attach all the sub-devices we can find
1210 */
1211int
1212adv_attach(adv)
1213	struct adv_softc *adv;
1214{
1215	struct ccb_setasync csa;
1216	struct cam_devq *devq;
1217
1218	/*
1219	 * Create our DMA tags.  These tags define the kinds of device
1220	 * accessable memory allocations and memory mappings we will
1221	 * need to perform during normal operation.
1222	 *
1223	 * Unless we need to further restrict the allocation, we rely
1224	 * on the restrictions of the parent dmat, hence the common
1225	 * use of MAXADDR and MAXSIZE.
1226	 */
1227
1228	/* DMA tag for mapping buffers into device visible space. */
1229	if (bus_dma_tag_create(adv->parent_dmat, /*alignment*/0, /*boundary*/0,
1230			       /*lowaddr*/BUS_SPACE_MAXADDR,
1231			       /*highaddr*/BUS_SPACE_MAXADDR,
1232			       /*filter*/NULL, /*filterarg*/NULL,
1233			       /*maxsize*/MAXBSIZE,
1234			       /*nsegments*/ADV_MAX_SG_LIST,
1235			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1236			       /*flags*/BUS_DMA_ALLOCNOW,
1237			       &adv->buffer_dmat) != 0) {
1238		goto error_exit;
1239	}
1240	adv->init_level++;
1241
1242	/* DMA tag for our sense buffers */
1243	if (bus_dma_tag_create(adv->parent_dmat, /*alignment*/0, /*boundary*/0,
1244			       /*lowaddr*/BUS_SPACE_MAXADDR,
1245			       /*highaddr*/BUS_SPACE_MAXADDR,
1246			       /*filter*/NULL, /*filterarg*/NULL,
1247			       sizeof(struct scsi_sense_data)*adv->max_openings,
1248			       /*nsegments*/1,
1249			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1250			       /*flags*/0, &adv->sense_dmat) != 0) {
1251		goto error_exit;
1252        }
1253
1254	adv->init_level++;
1255
1256	/* Allocation for our sense buffers */
1257	if (bus_dmamem_alloc(adv->sense_dmat, (void **)&adv->sense_buffers,
1258			     BUS_DMA_NOWAIT, &adv->sense_dmamap) != 0) {
1259		goto error_exit;
1260	}
1261
1262	adv->init_level++;
1263
1264	/* And permanently map them */
1265	bus_dmamap_load(adv->sense_dmat, adv->sense_dmamap,
1266       			adv->sense_buffers,
1267			sizeof(struct scsi_sense_data)*adv->max_openings,
1268			adv_map, &adv->sense_physbase, /*flags*/0);
1269
1270	adv->init_level++;
1271
1272	/*
1273	 * Fire up the chip
1274	 */
1275	if (adv_start_chip(adv) != 1) {
1276		printf("adv%d: Unable to start on board processor. Aborting.\n",
1277		       adv->unit);
1278		return (0);
1279	}
1280
1281	/*
1282	 * Create the device queue for our SIM.
1283	 */
1284	devq = cam_simq_alloc(adv->max_openings);
1285	if (devq == NULL)
1286		return (0);
1287
1288	/*
1289	 * Construct our SIM entry.
1290	 */
1291	adv->sim = cam_sim_alloc(adv_action, adv_poll, "adv", adv, adv->unit,
1292				 1, adv->max_openings, devq);
1293	if (adv->sim == NULL)
1294		return (0);
1295
1296	/*
1297	 * Register the bus.
1298	 *
1299	 * XXX Twin Channel EISA Cards???
1300	 */
1301	if (xpt_bus_register(adv->sim, 0) != CAM_SUCCESS) {
1302		cam_sim_free(adv->sim, /*free devq*/TRUE);
1303		return (0);
1304	}
1305
1306	if (xpt_create_path(&adv->path, /*periph*/NULL, cam_sim_path(adv->sim),
1307			    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
1308	   == CAM_REQ_CMP) {
1309		xpt_setup_ccb(&csa.ccb_h, adv->path, /*priority*/5);
1310		csa.ccb_h.func_code = XPT_SASYNC_CB;
1311		csa.event_enable = AC_FOUND_DEVICE|AC_LOST_DEVICE;
1312		csa.callback = advasync;
1313		csa.callback_arg = adv;
1314		xpt_action((union ccb *)&csa);
1315	}
1316	return (1);
1317
1318error_exit:
1319	return (0);
1320}
1321