aha.c revision 163896
1/*
2 * Generic register and struct definitions for the Adaptech 154x/164x
3 * SCSI host adapters. Product specific probe and attach routines can
4 * be found in:
5 *      aha 1542A/1542B/1542C/1542CF/1542CP	aha_isa.c
6 *      aha 1640			aha_mca.c
7 */
8/*-
9 * Copyright (c) 1998 M. Warner Losh.
10 * All Rights Reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Derived from bt.c written by:
34 *
35 * Copyright (c) 1998 Justin T. Gibbs.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions, and the following disclaimer,
43 *    without modification, immediately at the beginning of the file.
44 * 2. The name of the author may not be used to endorse or promote products
45 *    derived from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
51 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60#include <sys/cdefs.h>
61__FBSDID("$FreeBSD: head/sys/dev/aha/aha.c 163896 2006-11-02 00:54:38Z mjacob $");
62
63#include <sys/param.h>
64#include <sys/bus.h>
65#include <sys/systm.h>
66#include <sys/malloc.h>
67#include <sys/kernel.h>
68#include <sys/lock.h>
69#include <sys/mutex.h>
70
71#include <machine/bus.h>
72
73#include <cam/cam.h>
74#include <cam/cam_ccb.h>
75#include <cam/cam_sim.h>
76#include <cam/cam_xpt_sim.h>
77#include <cam/cam_debug.h>
78
79#include <cam/scsi/scsi_message.h>
80
81#include <dev/aha/ahareg.h>
82
83#define	PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
84
85/* Macro to determine that a rev is potentially a new valid one
86 * so that the driver doesn't keep breaking on new revs as it
87 * did for the CF and CP.
88 */
89#define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
90
91/* MailBox Management functions */
92static __inline void	ahanextinbox(struct aha_softc *aha);
93static __inline void	ahanextoutbox(struct aha_softc *aha);
94
95#define aha_name(aha)	device_get_nameunit(aha->dev)
96
97static __inline void
98ahanextinbox(struct aha_softc *aha)
99{
100	if (aha->cur_inbox == aha->last_inbox)
101		aha->cur_inbox = aha->in_boxes;
102	else
103		aha->cur_inbox++;
104}
105
106static __inline void
107ahanextoutbox(struct aha_softc *aha)
108{
109	if (aha->cur_outbox == aha->last_outbox)
110		aha->cur_outbox = aha->out_boxes;
111	else
112		aha->cur_outbox++;
113}
114
115#define ahautoa24(u,s3)			\
116	(s3)[0] = ((u) >> 16) & 0xff;	\
117	(s3)[1] = ((u) >> 8) & 0xff;	\
118	(s3)[2] = (u) & 0xff;
119
120#define aha_a24tou(s3) \
121	(((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
122
123/* CCB Mangement functions */
124static __inline uint32_t		ahaccbvtop(struct aha_softc *aha,
125						  struct aha_ccb *accb);
126static __inline struct aha_ccb*		ahaccbptov(struct aha_softc *aha,
127						  uint32_t ccb_addr);
128
129static __inline uint32_t
130ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
131{
132	return (aha->aha_ccb_physbase
133	      + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
134}
135static __inline struct aha_ccb *
136ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
137{
138	return (aha->aha_ccb_array +
139	      + ((struct aha_ccb*)(uintptr_t)ccb_addr -
140	         (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
141}
142
143static struct aha_ccb*	ahagetccb(struct aha_softc *aha);
144static __inline void	ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
145static void		ahaallocccbs(struct aha_softc *aha);
146static bus_dmamap_callback_t ahaexecuteccb;
147static void		ahadone(struct aha_softc *aha, struct aha_ccb *accb,
148			       aha_mbi_comp_code_t comp_code);
149
150/* Host adapter command functions */
151static int	ahareset(struct aha_softc* aha, int hard_reset);
152
153/* Initialization functions */
154static int			ahainitmboxes(struct aha_softc *aha);
155static bus_dmamap_callback_t	ahamapmboxes;
156static bus_dmamap_callback_t	ahamapccbs;
157static bus_dmamap_callback_t	ahamapsgs;
158
159/* Transfer Negotiation Functions */
160static void ahafetchtransinfo(struct aha_softc *aha,
161			     struct ccb_trans_settings *cts);
162
163/* CAM SIM entry points */
164#define ccb_accb_ptr spriv_ptr0
165#define ccb_aha_ptr spriv_ptr1
166static void	ahaaction(struct cam_sim *sim, union ccb *ccb);
167static void	ahapoll(struct cam_sim *sim);
168
169/* Our timeout handler */
170static timeout_t ahatimeout;
171
172/* Exported functions */
173void
174aha_alloc(struct aha_softc *aha, int unit, bus_space_tag_t tag,
175  bus_space_handle_t bsh)
176{
177
178	SLIST_INIT(&aha->free_aha_ccbs);
179	LIST_INIT(&aha->pending_ccbs);
180	SLIST_INIT(&aha->sg_maps);
181	aha->unit = unit;
182	aha->tag = tag;
183	aha->bsh = bsh;
184	aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
185	aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
186}
187
188void
189aha_free(struct aha_softc *aha)
190{
191	switch (aha->init_level) {
192	default:
193	case 8:
194	{
195		struct sg_map_node *sg_map;
196
197		while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
198			SLIST_REMOVE_HEAD(&aha->sg_maps, links);
199			bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
200			bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
201			    sg_map->sg_dmamap);
202			free(sg_map, M_DEVBUF);
203		}
204		bus_dma_tag_destroy(aha->sg_dmat);
205	}
206	case 7:
207		bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
208	case 6:
209		bus_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);
210		bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
211		    aha->ccb_dmamap);
212	case 5:
213		bus_dma_tag_destroy(aha->ccb_dmat);
214	case 4:
215		bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
216	case 3:
217		bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
218		    aha->mailbox_dmamap);
219		bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);
220	case 2:
221		bus_dma_tag_destroy(aha->buffer_dmat);
222	case 1:
223		bus_dma_tag_destroy(aha->mailbox_dmat);
224	case 0:
225		break;
226	}
227}
228
229/*
230 * Probe the adapter and verify that the card is an Adaptec.
231 */
232int
233aha_probe(struct aha_softc* aha)
234{
235	u_int	 status;
236	u_int	 intstat;
237	int	 error;
238	board_id_data_t	board_id;
239
240	/*
241	 * See if the three I/O ports look reasonable.
242	 * Touch the minimal number of registers in the
243	 * failure case.
244	 */
245	status = aha_inb(aha, STATUS_REG);
246	if ((status == 0) ||
247	    (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
248		PRVERB((aha->dev, "status reg test failed %x\n", status));
249		return (ENXIO);
250	}
251
252	intstat = aha_inb(aha, INTSTAT_REG);
253	if ((intstat & INTSTAT_REG_RSVD) != 0) {
254		PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
255		return (ENXIO);
256	}
257
258	/*
259	 * Looking good so far.  Final test is to reset the
260	 * adapter and fetch the board ID and ensure we aren't
261	 * looking at a BusLogic.
262	 */
263	if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
264		PRVERB((aha->dev, "Failed Reset\n"));
265		return (ENXIO);
266	}
267
268	/*
269	 * Get the board ID.  We use this to see if we're dealing with
270	 * a buslogic card or an aha card (or clone).
271	 */
272	error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
273	    (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
274	if (error != 0) {
275		PRVERB((aha->dev, "INQUIRE failed %x\n", error));
276		return (ENXIO);
277	}
278	aha->fw_major = board_id.firmware_rev_major;
279	aha->fw_minor = board_id.firmware_rev_minor;
280	aha->boardid = board_id.board_type;
281
282	/*
283	 * The Buslogic cards have an id of either 0x41 or 0x42.  So
284	 * if those come up in the probe, we test the geometry register
285	 * of the board.  Adaptec boards that are this old will not have
286	 * this register, and return 0xff, while buslogic cards will return
287	 * something different.
288	 *
289	 * It appears that for reasons unknow, for the for the
290	 * aha-1542B cards, we need to wait a little bit before trying
291	 * to read the geometry register.  I picked 10ms since we have
292	 * reports that a for loop to 1000 did the trick, and this
293	 * errs on the side of conservatism.  Besides, no one will
294	 * notice a 10mS delay here, even the 1542B card users :-)
295	 *
296	 * Some compatible cards return 0 here.  Some cards also
297	 * seem to return 0x7f.
298	 *
299	 * XXX I'm not sure how this will impact other cloned cards
300	 *
301	 * This really should be replaced with the esetup command, since
302	 * that appears to be more reliable.  This becomes more and more
303	 * true over time as we discover more cards that don't read the
304	 * geometry register consistantly.
305	 */
306	if (aha->boardid <= 0x42) {
307		/* Wait 10ms before reading */
308		DELAY(10000);
309		status = aha_inb(aha, GEOMETRY_REG);
310		if (status != 0xff && status != 0x00 && status != 0x7f) {
311			PRVERB((aha->dev, "Geometry Register test failed %#x\n",
312				status));
313			return (ENXIO);
314		}
315	}
316
317	return (0);
318}
319
320/*
321 * Pull the boards setup information and record it in our softc.
322 */
323int
324aha_fetch_adapter_info(struct aha_softc *aha)
325{
326	setup_data_t	setup_info;
327	config_data_t config_data;
328	uint8_t length_param;
329	int	 error;
330	struct	aha_extbios extbios;
331
332	switch (aha->boardid) {
333	case BOARD_1540_16HEAD_BIOS:
334		snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
335		break;
336	case BOARD_1540_64HEAD_BIOS:
337		snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
338		break;
339	case BOARD_1542:
340		snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
341		break;
342	case BOARD_1640:
343		snprintf(aha->model, sizeof(aha->model), "1640");
344		break;
345	case BOARD_1740:
346		snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");
347		break;
348	case BOARD_1542C:
349		snprintf(aha->model, sizeof(aha->model), "1542C");
350		break;
351	case BOARD_1542CF:
352		snprintf(aha->model, sizeof(aha->model), "1542CF");
353		break;
354	case BOARD_1542CP:
355		snprintf(aha->model, sizeof(aha->model), "1542CP");
356		break;
357	default:
358		snprintf(aha->model, sizeof(aha->model), "Unknown");
359		break;
360	}
361	/*
362	 * If we are a new type of 1542 board (anything newer than a 1542C)
363	 * then disable the extended bios so that the
364	 * mailbox interface is unlocked.
365	 * This is also true for the 1542B Version 3.20. First Adaptec
366	 * board that supports >1Gb drives.
367	 * No need to check the extended bios flags as some of the
368	 * extensions that cause us problems are not flagged in that byte.
369	 */
370	if (PROBABLY_NEW_BOARD(aha->boardid) ||
371		(aha->boardid == 0x41
372		&& aha->fw_major == 0x31 &&
373		aha->fw_minor >= 0x34)) {
374		error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
375		    /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
376		    DEFAULT_CMD_TIMEOUT);
377		if (error != 0) {
378			device_printf(aha->dev,
379			    "AOP_RETURN_EXT_BIOS_INFO - Failed.");
380			return (error);
381		}
382		error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
383		    /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
384		if (error != 0) {
385			device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
386			return (error);
387		}
388	}
389	if (aha->boardid < 0x41)
390		device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
391
392	aha->max_sg = 17;		/* Need >= 17 to do 64k I/O */
393	aha->diff_bus = 0;
394	aha->extended_lun = 0;
395	aha->extended_trans = 0;
396	aha->max_ccbs = 16;
397	/* Determine Sync/Wide/Disc settings */
398	length_param = sizeof(setup_info);
399	error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
400	    /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
401	    DEFAULT_CMD_TIMEOUT);
402	if (error != 0) {
403		device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
404		    "Get Setup Info\n");
405		return (error);
406	}
407	if (setup_info.initiate_sync != 0) {
408		aha->sync_permitted = ALL_TARGETS;
409	}
410	aha->disc_permitted = ALL_TARGETS;
411
412	/* We need as many mailboxes as we can have ccbs */
413	aha->num_boxes = aha->max_ccbs;
414
415	/* Determine our SCSI ID */
416	error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
417	    (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
418	if (error != 0) {
419		device_printf(aha->dev,
420		    "aha_fetch_adapter_info - Failed Get Config\n");
421		return (error);
422	}
423	aha->scsi_id = config_data.scsi_id;
424	return (0);
425}
426
427/*
428 * Start the board, ready for normal operation
429 */
430int
431aha_init(struct aha_softc* aha)
432{
433	/* Announce the Adapter */
434	device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
435	    aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
436
437	if (aha->diff_bus != 0)
438		printf("Diff ");
439
440	printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
441	    aha->max_ccbs);
442
443	/*
444	 * Create our DMA tags.  These tags define the kinds of device
445	 * accessible memory allocations and memory mappings we will
446	 * need to perform during normal operation.
447	 *
448	 * Unless we need to further restrict the allocation, we rely
449	 * on the restrictions of the parent dmat, hence the common
450	 * use of MAXADDR and MAXSIZE.
451	 */
452
453	/* DMA tag for mapping buffers into device visible space. */
454	if (bus_dma_tag_create( /* parent	*/ aha->parent_dmat,
455				/* alignment	*/ 1,
456				/* boundary	*/ 0,
457				/* lowaddr	*/ BUS_SPACE_MAXADDR,
458				/* highaddr	*/ BUS_SPACE_MAXADDR,
459				/* filter	*/ NULL,
460				/* filterarg	*/ NULL,
461				/* maxsize	*/ MAXBSIZE,
462				/* nsegments	*/ AHA_NSEG,
463				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
464				/* flags	*/ BUS_DMA_ALLOCNOW,
465				/* lockfunc	*/ busdma_lock_mutex,
466				/* lockarg	*/ &Giant,
467				&aha->buffer_dmat) != 0) {
468		goto error_exit;
469	}
470
471	aha->init_level++;
472	/* DMA tag for our mailboxes */
473	if (bus_dma_tag_create(	/* parent	*/ aha->parent_dmat,
474				/* alignment	*/ 1,
475				/* boundary	*/ 0,
476				/* lowaddr	*/ BUS_SPACE_MAXADDR,
477				/* highaddr	*/ BUS_SPACE_MAXADDR,
478				/* filter	*/ NULL,
479				/* filterarg	*/ NULL,
480				/* maxsize	*/ aha->num_boxes *
481						   (sizeof(aha_mbox_in_t) +
482						    sizeof(aha_mbox_out_t)),
483				/* nsegments	*/ 1,
484				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
485				/* flags	*/ 0,
486				/* lockfunc	*/ busdma_lock_mutex,
487				/* lockarg	*/ &Giant,
488				&aha->mailbox_dmat) != 0) {
489		goto error_exit;
490        }
491
492	aha->init_level++;
493
494	/* Allocation for our mailboxes */
495	if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
496	    BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
497		goto error_exit;
498
499	aha->init_level++;
500
501	/* And permanently map them */
502	bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
503	    aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
504	    sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
505
506	aha->init_level++;
507
508	aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
509
510	ahainitmboxes(aha);
511
512	/* DMA tag for our ccb structures */
513	if (bus_dma_tag_create(	/* parent	*/ aha->parent_dmat,
514				/* alignment	*/ 1,
515				/* boundary	*/ 0,
516				/* lowaddr	*/ BUS_SPACE_MAXADDR,
517				/* highaddr	*/ BUS_SPACE_MAXADDR,
518				/* filter	*/ NULL,
519				/* filterarg	*/ NULL,
520				/* maxsize	*/ aha->max_ccbs *
521						   sizeof(struct aha_ccb),
522				/* nsegments	*/ 1,
523				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
524				/* flags	*/ 0,
525				/* lockfunc	*/ busdma_lock_mutex,
526				/* lockarg	*/ &Giant,
527				&aha->ccb_dmat) != 0) {
528		goto error_exit;
529        }
530
531	aha->init_level++;
532
533	/* Allocation for our ccbs */
534	if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
535	    BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
536		goto error_exit;
537
538	aha->init_level++;
539
540	/* And permanently map them */
541	bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
542	    aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
543
544	aha->init_level++;
545
546	/* DMA tag for our S/G structures.  We allocate in page sized chunks */
547	if (bus_dma_tag_create(	/* parent	*/ aha->parent_dmat,
548				/* alignment	*/ 1,
549				/* boundary	*/ 0,
550				/* lowaddr	*/ BUS_SPACE_MAXADDR,
551				/* highaddr	*/ BUS_SPACE_MAXADDR,
552				/* filter	*/ NULL,
553				/* filterarg	*/ NULL,
554				/* maxsize	*/ PAGE_SIZE,
555				/* nsegments	*/ 1,
556				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
557				/* flags	*/ 0,
558				/* lockfunc	*/ busdma_lock_mutex,
559				/* lockarg	*/ &Giant,
560				&aha->sg_dmat) != 0)
561		goto error_exit;
562
563	aha->init_level++;
564
565	/* Perform initial CCB allocation */
566	bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
567	ahaallocccbs(aha);
568
569	if (aha->num_ccbs == 0) {
570		device_printf(aha->dev,
571		    "aha_init - Unable to allocate initial ccbs\n");
572		goto error_exit;
573	}
574
575	/*
576	 * Note that we are going and return (to probe)
577	 */
578	return (0);
579
580error_exit:
581
582	return (ENXIO);
583}
584
585int
586aha_attach(struct aha_softc *aha)
587{
588	int tagged_dev_openings;
589	struct cam_devq *devq;
590
591	/*
592	 * We don't do tagged queueing, since the aha cards don't
593	 * support it.
594	 */
595	tagged_dev_openings = 0;
596
597	/*
598	 * Create the device queue for our SIM.
599	 */
600	devq = cam_simq_alloc(aha->max_ccbs - 1);
601	if (devq == NULL)
602		return (ENOMEM);
603
604	/*
605	 * Construct our SIM entry
606	 */
607	aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha, aha->unit, 2,
608	    tagged_dev_openings, devq);
609	if (aha->sim == NULL) {
610		cam_simq_free(devq);
611		return (ENOMEM);
612	}
613	if (xpt_bus_register(aha->sim, 0) != CAM_SUCCESS) {
614		cam_sim_free(aha->sim, /*free_devq*/TRUE);
615		return (ENXIO);
616	}
617	if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
618	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
619		xpt_bus_deregister(cam_sim_path(aha->sim));
620		cam_sim_free(aha->sim, /*free_devq*/TRUE);
621		return (ENXIO);
622	}
623
624	return (0);
625}
626
627static void
628ahaallocccbs(struct aha_softc *aha)
629{
630	struct aha_ccb *next_ccb;
631	struct sg_map_node *sg_map;
632	bus_addr_t physaddr;
633	aha_sg_t *segs;
634	int newcount;
635	int i;
636
637	next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
638
639	sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
640
641	if (sg_map == NULL)
642		return;
643
644	/* Allocate S/G space for the next batch of CCBS */
645	if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
646	    BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
647		free(sg_map, M_DEVBUF);
648		return;
649	}
650
651	SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
652
653	bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
654	    PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
655
656	segs = sg_map->sg_vaddr;
657	physaddr = sg_map->sg_physaddr;
658
659	newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
660	for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
661		int error;
662
663		next_ccb->sg_list = segs;
664		next_ccb->sg_list_phys = physaddr;
665		next_ccb->flags = ACCB_FREE;
666		error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
667		    &next_ccb->dmamap);
668		if (error != 0)
669			break;
670		SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
671		segs += AHA_NSEG;
672		physaddr += (AHA_NSEG * sizeof(aha_sg_t));
673		next_ccb++;
674		aha->num_ccbs++;
675	}
676
677	/* Reserve a CCB for error recovery */
678	if (aha->recovery_accb == NULL) {
679		aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
680		SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
681	}
682}
683
684static __inline void
685ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
686{
687	int s;
688
689	s = splcam();
690	if ((accb->flags & ACCB_ACTIVE) != 0)
691		LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
692	if (aha->resource_shortage != 0
693	    && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
694		accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
695		aha->resource_shortage = FALSE;
696	}
697	accb->flags = ACCB_FREE;
698	SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
699	aha->active_ccbs--;
700	splx(s);
701}
702
703static struct aha_ccb*
704ahagetccb(struct aha_softc *aha)
705{
706	struct	aha_ccb* accb;
707	int	s;
708
709	s = splcam();
710	if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
711		SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
712		aha->active_ccbs++;
713	} else if (aha->num_ccbs < aha->max_ccbs) {
714		ahaallocccbs(aha);
715		accb = SLIST_FIRST(&aha->free_aha_ccbs);
716		if (accb == NULL)
717			device_printf(aha->dev, "Can't malloc ACCB\n");
718		else {
719			SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
720			aha->active_ccbs++;
721		}
722	}
723	splx(s);
724
725	return (accb);
726}
727
728static void
729ahaaction(struct cam_sim *sim, union ccb *ccb)
730{
731	struct	aha_softc *aha;
732	int s;
733
734	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
735
736	aha = (struct aha_softc *)cam_sim_softc(sim);
737
738	switch (ccb->ccb_h.func_code) {
739	/* Common cases first */
740	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
741	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */	{
742		struct	aha_ccb	*accb;
743		struct	aha_hccb *hccb;
744
745		/*
746		 * Get an accb to use.
747		 */
748		if ((accb = ahagetccb(aha)) == NULL) {
749			s = splcam();
750			aha->resource_shortage = TRUE;
751			splx(s);
752			xpt_freeze_simq(aha->sim, /*count*/1);
753			ccb->ccb_h.status = CAM_REQUEUE_REQ;
754			xpt_done(ccb);
755			return;
756		}
757		hccb = &accb->hccb;
758
759		/*
760		 * So we can find the ACCB when an abort is requested
761		 */
762		accb->ccb = ccb;
763		ccb->ccb_h.ccb_accb_ptr = accb;
764		ccb->ccb_h.ccb_aha_ptr = aha;
765
766		/*
767		 * Put all the arguments for the xfer in the accb
768		 */
769		hccb->target = ccb->ccb_h.target_id;
770		hccb->lun = ccb->ccb_h.target_lun;
771		hccb->ahastat = 0;
772		hccb->sdstat = 0;
773
774		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
775			struct ccb_scsiio *csio;
776			struct ccb_hdr *ccbh;
777
778			csio = &ccb->csio;
779			ccbh = &csio->ccb_h;
780			hccb->opcode = aha->ccb_ccb_opcode;
781			hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
782			hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
783			hccb->cmd_len = csio->cdb_len;
784			if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
785				ccb->ccb_h.status = CAM_REQ_INVALID;
786				ahafreeccb(aha, accb);
787				xpt_done(ccb);
788				return;
789			}
790			hccb->sense_len = csio->sense_len;
791			if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
792				if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
793					bcopy(csio->cdb_io.cdb_ptr,
794					      hccb->scsi_cdb, hccb->cmd_len);
795				} else {
796					/* I guess I could map it in... */
797					ccbh->status = CAM_REQ_INVALID;
798					ahafreeccb(aha, accb);
799					xpt_done(ccb);
800					return;
801				}
802			} else {
803				bcopy(csio->cdb_io.cdb_bytes,
804				      hccb->scsi_cdb, hccb->cmd_len);
805			}
806			/*
807			 * If we have any data to send with this command,
808			 * map it into bus space.
809			 */
810		        /* Only use S/G if there is a transfer */
811			if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
812				if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
813					/*
814					 * We've been given a pointer
815					 * to a single buffer.
816					 */
817					if ((ccbh->flags & CAM_DATA_PHYS)==0) {
818						int error;
819
820						s = splsoftvm();
821						error = bus_dmamap_load(
822						    aha->buffer_dmat,
823						    accb->dmamap,
824						    csio->data_ptr,
825						    csio->dxfer_len,
826						    ahaexecuteccb,
827						    accb,
828						    /*flags*/0);
829						if (error == EINPROGRESS) {
830							/*
831							 * So as to maintain
832							 * ordering, freeze the
833							 * controller queue
834							 * until our mapping is
835							 * returned.
836							 */
837							xpt_freeze_simq(aha->sim,
838									1);
839							csio->ccb_h.status |=
840							    CAM_RELEASE_SIMQ;
841						}
842						splx(s);
843					} else {
844						struct bus_dma_segment seg;
845
846						/* Pointer to physical buffer */
847						seg.ds_addr =
848						    (bus_addr_t)csio->data_ptr;
849						seg.ds_len = csio->dxfer_len;
850						ahaexecuteccb(accb, &seg, 1, 0);
851					}
852				} else {
853					struct bus_dma_segment *segs;
854
855					if ((ccbh->flags & CAM_DATA_PHYS) != 0)
856						panic("ahaaction - Physical "
857						      "segment pointers "
858						      "unsupported");
859
860					if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
861						panic("ahaaction - Virtual "
862						      "segment addresses "
863						      "unsupported");
864
865					/* Just use the segments provided */
866					segs = (struct bus_dma_segment *)
867					    csio->data_ptr;
868					ahaexecuteccb(accb, segs,
869						     csio->sglist_cnt, 0);
870				}
871			} else {
872				ahaexecuteccb(accb, NULL, 0, 0);
873			}
874		} else {
875			hccb->opcode = INITIATOR_BUS_DEV_RESET;
876			/* No data transfer */
877			hccb->datain = TRUE;
878			hccb->dataout = TRUE;
879			hccb->cmd_len = 0;
880			hccb->sense_len = 0;
881			ahaexecuteccb(accb, NULL, 0, 0);
882		}
883		break;
884	}
885	case XPT_EN_LUN:		/* Enable LUN as a target */
886	case XPT_TARGET_IO:		/* Execute target I/O request */
887	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
888	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
889	case XPT_ABORT:			/* Abort the specified CCB */
890		/* XXX Implement */
891		ccb->ccb_h.status = CAM_REQ_INVALID;
892		xpt_done(ccb);
893		break;
894	case XPT_SET_TRAN_SETTINGS:
895		/* XXX Implement */
896		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
897		xpt_done(ccb);
898		break;
899	case XPT_GET_TRAN_SETTINGS:
900	/* Get default/user set transfer settings for the target */
901	{
902		struct	ccb_trans_settings *cts = &ccb->cts;
903		u_int	target_mask = 0x01 << ccb->ccb_h.target_id;
904		struct ccb_trans_settings_scsi *scsi =
905		    &cts->proto_specific.scsi;
906		struct ccb_trans_settings_spi *spi =
907		    &cts->xport_specific.spi;
908
909		cts->protocol = PROTO_SCSI;
910		cts->protocol_version = SCSI_REV_2;
911		cts->transport = XPORT_SPI;
912		cts->transport_version = 2;
913		if (cts->type == CTS_TYPE_USER_SETTINGS) {
914			spi->flags = 0;
915			if ((aha->disc_permitted & target_mask) != 0)
916				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
917			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
918			if ((aha->sync_permitted & target_mask) != 0) {
919				if (aha->boardid >= BOARD_1542CF)
920					spi->sync_period = 25;
921				else
922					spi->sync_period = 50;
923			} else {
924				spi->sync_period = 0;
925			}
926
927			if (spi->sync_period != 0)
928				spi->sync_offset = 15;
929
930			spi->valid = CTS_SPI_VALID_SYNC_RATE
931				   | CTS_SPI_VALID_SYNC_OFFSET
932				   | CTS_SPI_VALID_BUS_WIDTH
933				   | CTS_SPI_VALID_DISC;
934			scsi->valid = CTS_SCSI_VALID_TQ;
935		} else {
936			ahafetchtransinfo(aha, cts);
937		}
938
939		ccb->ccb_h.status = CAM_REQ_CMP;
940		xpt_done(ccb);
941		break;
942	}
943	case XPT_CALC_GEOMETRY:
944	{
945		struct	  ccb_calc_geometry *ccg;
946		uint32_t size_mb;
947		uint32_t secs_per_cylinder;
948
949		ccg = &ccb->ccg;
950		size_mb = ccg->volume_size
951			/ ((1024L * 1024L) / ccg->block_size);
952		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
953			if (size_mb >= 2048) {
954				ccg->heads = 255;
955				ccg->secs_per_track = 63;
956			} else {
957				ccg->heads = 128;
958				ccg->secs_per_track = 32;
959			}
960		} else {
961			ccg->heads = 64;
962			ccg->secs_per_track = 32;
963		}
964		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
965		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
966		ccb->ccb_h.status = CAM_REQ_CMP;
967		xpt_done(ccb);
968		break;
969	}
970	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
971		ahareset(aha, /*hardreset*/TRUE);
972		ccb->ccb_h.status = CAM_REQ_CMP;
973		xpt_done(ccb);
974		break;
975	case XPT_TERM_IO:		/* Terminate the I/O process */
976		/* XXX Implement */
977		ccb->ccb_h.status = CAM_REQ_INVALID;
978		xpt_done(ccb);
979		break;
980	case XPT_PATH_INQ:		/* Path routing inquiry */
981	{
982		struct ccb_pathinq *cpi = &ccb->cpi;
983
984		cpi->version_num = 1; /* XXX??? */
985		cpi->hba_inquiry = PI_SDTR_ABLE;
986		cpi->target_sprt = 0;
987		cpi->hba_misc = 0;
988		cpi->hba_eng_cnt = 0;
989		cpi->max_target = 7;
990		cpi->max_lun = 7;
991		cpi->initiator_id = aha->scsi_id;
992		cpi->bus_id = cam_sim_bus(sim);
993		cpi->base_transfer_speed = 3300;
994		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
995		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
996		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
997		cpi->unit_number = cam_sim_unit(sim);
998                cpi->transport = XPORT_SPI;
999                cpi->transport_version = 2;
1000                cpi->protocol = PROTO_SCSI;
1001                cpi->protocol_version = SCSI_REV_2;
1002		cpi->ccb_h.status = CAM_REQ_CMP;
1003		xpt_done(ccb);
1004		break;
1005	}
1006	default:
1007		ccb->ccb_h.status = CAM_REQ_INVALID;
1008		xpt_done(ccb);
1009		break;
1010	}
1011}
1012
1013static void
1014ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1015{
1016	struct	 aha_ccb *accb;
1017	union	 ccb *ccb;
1018	struct	 aha_softc *aha;
1019	int	 s;
1020	uint32_t paddr;
1021
1022	accb = (struct aha_ccb *)arg;
1023	ccb = accb->ccb;
1024	aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1025
1026	if (error != 0) {
1027		if (error != EFBIG)
1028			device_printf(aha->dev,
1029			    "Unexepected error 0x%x returned from "
1030			    "bus_dmamap_load\n", error);
1031		if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1032			xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1033			ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1034		}
1035		ahafreeccb(aha, accb);
1036		xpt_done(ccb);
1037		return;
1038	}
1039
1040	if (nseg != 0) {
1041		aha_sg_t *sg;
1042		bus_dma_segment_t *end_seg;
1043		bus_dmasync_op_t op;
1044
1045		end_seg = dm_segs + nseg;
1046
1047		/* Copy the segments into our SG list */
1048		sg = accb->sg_list;
1049		while (dm_segs < end_seg) {
1050			ahautoa24(dm_segs->ds_len, sg->len);
1051			ahautoa24(dm_segs->ds_addr, sg->addr);
1052			sg++;
1053			dm_segs++;
1054		}
1055
1056		if (nseg > 1) {
1057			accb->hccb.opcode = aha->ccb_sg_opcode;
1058			ahautoa24((sizeof(aha_sg_t) * nseg),
1059			    accb->hccb.data_len);
1060			ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1061		} else {
1062			bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1063			bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1064		}
1065
1066		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1067			op = BUS_DMASYNC_PREREAD;
1068		else
1069			op = BUS_DMASYNC_PREWRITE;
1070
1071		bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1072
1073	} else {
1074		accb->hccb.opcode = INITIATOR_CCB;
1075		ahautoa24(0, accb->hccb.data_len);
1076		ahautoa24(0, accb->hccb.data_addr);
1077	}
1078
1079	s = splcam();
1080
1081	/*
1082	 * Last time we need to check if this CCB needs to
1083	 * be aborted.
1084	 */
1085	if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1086		if (nseg != 0)
1087			bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1088		ahafreeccb(aha, accb);
1089		xpt_done(ccb);
1090		splx(s);
1091		return;
1092	}
1093
1094	accb->flags = ACCB_ACTIVE;
1095	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1096	LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1097
1098	ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb,
1099	    (ccb->ccb_h.timeout * hz) / 1000);
1100
1101	/* Tell the adapter about this command */
1102	if (aha->cur_outbox->action_code != AMBO_FREE) {
1103		/*
1104		 * We should never encounter a busy mailbox.
1105		 * If we do, warn the user, and treat it as
1106		 * a resource shortage.  If the controller is
1107		 * hung, one of the pending transactions will
1108		 * timeout causing us to start recovery operations.
1109		 */
1110		device_printf(aha->dev,
1111		    "Encountered busy mailbox with %d out of %d "
1112		    "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1113		untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
1114		if (nseg != 0)
1115			bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1116		ahafreeccb(aha, accb);
1117		aha->resource_shortage = TRUE;
1118		xpt_freeze_simq(aha->sim, /*count*/1);
1119		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1120		xpt_done(ccb);
1121		return;
1122	}
1123	paddr = ahaccbvtop(aha, accb);
1124	ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1125	aha->cur_outbox->action_code = AMBO_START;
1126	aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1127
1128	ahanextoutbox(aha);
1129	splx(s);
1130}
1131
1132void
1133aha_intr(void *arg)
1134{
1135	struct	aha_softc *aha;
1136	u_int	intstat;
1137	uint32_t paddr;
1138
1139	aha = (struct aha_softc *)arg;
1140	while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1141		if ((intstat & CMD_COMPLETE) != 0) {
1142			aha->latched_status = aha_inb(aha, STATUS_REG);
1143			aha->command_cmp = TRUE;
1144		}
1145
1146		aha_outb(aha, CONTROL_REG, RESET_INTR);
1147
1148		if ((intstat & IMB_LOADED) != 0) {
1149			while (aha->cur_inbox->comp_code != AMBI_FREE) {
1150				paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1151				ahadone(aha, ahaccbptov(aha, paddr),
1152				    aha->cur_inbox->comp_code);
1153				aha->cur_inbox->comp_code = AMBI_FREE;
1154				ahanextinbox(aha);
1155			}
1156		}
1157
1158		if ((intstat & SCSI_BUS_RESET) != 0) {
1159			ahareset(aha, /*hardreset*/FALSE);
1160		}
1161	}
1162}
1163
1164static void
1165ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1166{
1167	union  ccb	  *ccb;
1168	struct ccb_scsiio *csio;
1169
1170	ccb = accb->ccb;
1171	csio = &accb->ccb->csio;
1172
1173	if ((accb->flags & ACCB_ACTIVE) == 0) {
1174		device_printf(aha->dev,
1175		    "ahadone - Attempt to free non-active ACCB %p\n",
1176		    (void *)accb);
1177		return;
1178	}
1179
1180	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1181		bus_dmasync_op_t op;
1182
1183		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1184			op = BUS_DMASYNC_POSTREAD;
1185		else
1186			op = BUS_DMASYNC_POSTWRITE;
1187		bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1188		bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1189	}
1190
1191	if (accb == aha->recovery_accb) {
1192		/*
1193		 * The recovery ACCB does not have a CCB associated
1194		 * with it, so short circuit the normal error handling.
1195		 * We now traverse our list of pending CCBs and process
1196		 * any that were terminated by the recovery CCBs action.
1197		 * We also reinstate timeouts for all remaining, pending,
1198		 * CCBs.
1199		 */
1200		struct cam_path *path;
1201		struct ccb_hdr *ccb_h;
1202		cam_status error;
1203
1204		/* Notify all clients that a BDR occured */
1205		error = xpt_create_path(&path, /*periph*/NULL,
1206		    cam_sim_path(aha->sim), accb->hccb.target,
1207		    CAM_LUN_WILDCARD);
1208
1209		if (error == CAM_REQ_CMP)
1210			xpt_async(AC_SENT_BDR, path, NULL);
1211
1212		ccb_h = LIST_FIRST(&aha->pending_ccbs);
1213		while (ccb_h != NULL) {
1214			struct aha_ccb *pending_accb;
1215
1216			pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1217			if (pending_accb->hccb.target == accb->hccb.target) {
1218				pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1219				ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1220				ahadone(aha, pending_accb, AMBI_ERROR);
1221			} else {
1222				ccb_h->timeout_ch = timeout(ahatimeout,
1223				    (caddr_t)pending_accb,
1224				    (ccb_h->timeout * hz) / 1000);
1225				ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1226			}
1227		}
1228		device_printf(aha->dev, "No longer in timeout\n");
1229		return;
1230	}
1231
1232	untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
1233
1234	switch (comp_code) {
1235	case AMBI_FREE:
1236		device_printf(aha->dev,
1237		    "ahadone - CCB completed with free status!\n");
1238		break;
1239	case AMBI_NOT_FOUND:
1240		device_printf(aha->dev,
1241		    "ahadone - CCB Abort failed to find CCB\n");
1242		break;
1243	case AMBI_ABORT:
1244	case AMBI_ERROR:
1245		/* An error occured */
1246		if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1247			csio->resid = 0;
1248		else
1249			csio->resid = aha_a24tou(accb->hccb.data_len);
1250		switch(accb->hccb.ahastat) {
1251		case AHASTAT_DATARUN_ERROR:
1252		{
1253			if (csio->resid <= 0) {
1254				csio->ccb_h.status = CAM_DATA_RUN_ERR;
1255				break;
1256			}
1257			/* FALLTHROUGH */
1258		}
1259		case AHASTAT_NOERROR:
1260			csio->scsi_status = accb->hccb.sdstat;
1261			csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1262			switch(csio->scsi_status) {
1263			case SCSI_STATUS_CHECK_COND:
1264			case SCSI_STATUS_CMD_TERMINATED:
1265				csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1266				/*
1267				 * The aha writes the sense data at different
1268				 * offsets based on the scsi cmd len
1269				 */
1270				bcopy((caddr_t) &accb->hccb.scsi_cdb +
1271				    accb->hccb.cmd_len,
1272				    (caddr_t) &csio->sense_data,
1273				    accb->hccb.sense_len);
1274				break;
1275			default:
1276				break;
1277			case SCSI_STATUS_OK:
1278				csio->ccb_h.status = CAM_REQ_CMP;
1279				break;
1280			}
1281			break;
1282		case AHASTAT_SELTIMEOUT:
1283			csio->ccb_h.status = CAM_SEL_TIMEOUT;
1284			break;
1285		case AHASTAT_UNEXPECTED_BUSFREE:
1286			csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1287			break;
1288		case AHASTAT_INVALID_PHASE:
1289			csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1290			break;
1291		case AHASTAT_INVALID_ACTION_CODE:
1292			panic("%s: Inavlid Action code", aha_name(aha));
1293			break;
1294		case AHASTAT_INVALID_OPCODE:
1295			if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1296				panic("%s: Invalid CCB Opcode %x hccb = %p",
1297				    aha_name(aha), accb->hccb.opcode,
1298				    &accb->hccb);
1299			device_printf(aha->dev,
1300			    "AHA-1540A compensation failed\n");
1301			xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1302			csio->ccb_h.status = CAM_REQUEUE_REQ;
1303			break;
1304		case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1305			/* We don't even support linked commands... */
1306			panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1307			break;
1308		case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1309			panic("%s: Invalid CCB or SG list", aha_name(aha));
1310			break;
1311		case AHASTAT_HA_SCSI_BUS_RESET:
1312			if ((csio->ccb_h.status & CAM_STATUS_MASK)
1313			    != CAM_CMD_TIMEOUT)
1314				csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1315			break;
1316		case AHASTAT_HA_BDR:
1317			if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1318				csio->ccb_h.status = CAM_BDR_SENT;
1319			else
1320				csio->ccb_h.status = CAM_CMD_TIMEOUT;
1321			break;
1322		}
1323		if (csio->ccb_h.status != CAM_REQ_CMP) {
1324			xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1325			csio->ccb_h.status |= CAM_DEV_QFRZN;
1326		}
1327		if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1328			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1329		ahafreeccb(aha, accb);
1330		xpt_done(ccb);
1331		break;
1332	case AMBI_OK:
1333		/* All completed without incident */
1334		/* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1335		/* I don't think so since it works???? */
1336		ccb->ccb_h.status |= CAM_REQ_CMP;
1337		if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1338			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1339		ahafreeccb(aha, accb);
1340		xpt_done(ccb);
1341		break;
1342	}
1343}
1344
1345static int
1346ahareset(struct aha_softc* aha, int hard_reset)
1347{
1348	struct	 ccb_hdr *ccb_h;
1349	u_int	 status;
1350	u_int	 timeout;
1351	uint8_t reset_type;
1352
1353	if (hard_reset != 0)
1354		reset_type = HARD_RESET;
1355	else
1356		reset_type = SOFT_RESET;
1357	aha_outb(aha, CONTROL_REG, reset_type);
1358
1359	/* Wait 5sec. for Diagnostic start */
1360	timeout = 5 * 10000;
1361	while (--timeout) {
1362		status = aha_inb(aha, STATUS_REG);
1363		if ((status & DIAG_ACTIVE) != 0)
1364			break;
1365		DELAY(100);
1366	}
1367	if (timeout == 0) {
1368		PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1369		    "assert. status = %#x\n", status));
1370		return (ETIMEDOUT);
1371	}
1372
1373	/* Wait 10sec. for Diagnostic end */
1374	timeout = 10 * 10000;
1375	while (--timeout) {
1376		status = aha_inb(aha, STATUS_REG);
1377		if ((status & DIAG_ACTIVE) == 0)
1378			break;
1379		DELAY(100);
1380	}
1381	if (timeout == 0) {
1382		panic("%s: ahareset - Diagnostic Active failed to drop. "
1383		    "status = 0x%x\n", aha_name(aha), status);
1384		return (ETIMEDOUT);
1385	}
1386
1387	/* Wait for the host adapter to become ready or report a failure */
1388	timeout = 10000;
1389	while (--timeout) {
1390		status = aha_inb(aha, STATUS_REG);
1391		if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1392			break;
1393		DELAY(100);
1394	}
1395	if (timeout == 0) {
1396		device_printf(aha->dev, "ahareset - Host adapter failed to "
1397		    "come ready. status = 0x%x\n", status);
1398		return (ETIMEDOUT);
1399	}
1400
1401	/* If the diagnostics failed, tell the user */
1402	if ((status & DIAG_FAIL) != 0
1403	 || (status & HA_READY) == 0) {
1404		device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1405
1406		if ((status & DATAIN_REG_READY) != 0)
1407			device_printf(aha->dev, "ahareset - Host Adapter "
1408			    "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1409		return (ENXIO);
1410	}
1411
1412	/* If we've attached to the XPT, tell it about the event */
1413	if (aha->path != NULL)
1414		xpt_async(AC_BUS_RESET, aha->path, NULL);
1415
1416	/*
1417	 * Perform completion processing for all outstanding CCBs.
1418	 */
1419	while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1420		struct aha_ccb *pending_accb;
1421
1422		pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1423		pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1424		ahadone(aha, pending_accb, AMBI_ERROR);
1425	}
1426
1427	/* If we've allocated mailboxes, initialize them */
1428	/* Must be done after we've aborted our queue, or aha_cmd fails */
1429	if (aha->init_level > 4)
1430		ahainitmboxes(aha);
1431
1432	return (0);
1433}
1434
1435/*
1436 * Send a command to the adapter.
1437 */
1438int
1439aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1440	u_int param_len, uint8_t *reply_data, u_int reply_len,
1441	u_int cmd_timeout)
1442{
1443	u_int	timeout;
1444	u_int	status;
1445	u_int	saved_status;
1446	u_int	intstat;
1447	u_int	reply_buf_size;
1448	int	s;
1449	int	cmd_complete;
1450	int	error;
1451
1452	/* No data returned to start */
1453	reply_buf_size = reply_len;
1454	reply_len = 0;
1455	intstat = 0;
1456	cmd_complete = 0;
1457	saved_status = 0;
1458	error = 0;
1459
1460	/*
1461	 * All commands except for the "start mailbox" and the "enable
1462	 * outgoing mailbox read interrupt" commands cannot be issued
1463	 * while there are pending transactions.  Freeze our SIMQ
1464	 * and wait for all completions to occur if necessary.
1465	 */
1466	timeout = 10000;
1467	s = splcam();
1468	while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1469		/* Fire the interrupt handler in case interrupts are blocked */
1470		aha_intr(aha);
1471		splx(s);
1472		DELAY(10);
1473		s = splcam();
1474	}
1475	splx(s);
1476
1477	if (timeout == 0) {
1478		device_printf(aha->dev,
1479		    "aha_cmd: Timeout waiting for adapter idle\n");
1480		return (ETIMEDOUT);
1481	}
1482	aha->command_cmp = 0;
1483	/*
1484	 * Wait up to 10 sec. for the adapter to become
1485	 * ready to accept commands.
1486	 */
1487	timeout = 100000;
1488	while (--timeout) {
1489		status = aha_inb(aha, STATUS_REG);
1490		if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1491			break;
1492		/*
1493		 * Throw away any pending data which may be
1494		 * left over from earlier commands that we
1495		 * timedout on.
1496		 */
1497		if ((status & DATAIN_REG_READY) != 0)
1498			(void)aha_inb(aha, DATAIN_REG);
1499		DELAY(100);
1500	}
1501	if (timeout == 0) {
1502		device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1503		    " ready, status = 0x%x\n", status);
1504		return (ETIMEDOUT);
1505	}
1506
1507	/*
1508	 * Send the opcode followed by any necessary parameter bytes.
1509	 */
1510	aha_outb(aha, COMMAND_REG, opcode);
1511
1512	/*
1513	 * Wait for up to 1sec to get the parameter list sent
1514	 */
1515	timeout = 10000;
1516	while (param_len && --timeout) {
1517		DELAY(100);
1518		s = splcam();
1519		status = aha_inb(aha, STATUS_REG);
1520		intstat = aha_inb(aha, INTSTAT_REG);
1521		splx(s);
1522
1523		if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1524		 == (INTR_PENDING|CMD_COMPLETE)) {
1525			saved_status = status;
1526			cmd_complete = 1;
1527			break;
1528		}
1529
1530		if (aha->command_cmp != 0) {
1531			saved_status = aha->latched_status;
1532			cmd_complete = 1;
1533			break;
1534		}
1535		if ((status & DATAIN_REG_READY) != 0)
1536			break;
1537		if ((status & CMD_REG_BUSY) == 0) {
1538			aha_outb(aha, COMMAND_REG, *params++);
1539			param_len--;
1540			timeout = 10000;
1541		}
1542	}
1543	if (timeout == 0) {
1544		device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1545		    "status = 0x%x\n", status);
1546		error = ETIMEDOUT;
1547	}
1548
1549	/*
1550	 * For all other commands, we wait for any output data
1551	 * and the final comand completion interrupt.
1552	 */
1553	while (cmd_complete == 0 && --cmd_timeout) {
1554
1555		s = splcam();
1556		status = aha_inb(aha, STATUS_REG);
1557		intstat = aha_inb(aha, INTSTAT_REG);
1558		splx(s);
1559
1560		if (aha->command_cmp != 0) {
1561			cmd_complete = 1;
1562			saved_status = aha->latched_status;
1563		} else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1564			== (INTR_PENDING|CMD_COMPLETE)) {
1565			/*
1566			 * Our poll (in case interrupts are blocked)
1567			 * saw the CMD_COMPLETE interrupt.
1568			 */
1569			cmd_complete = 1;
1570			saved_status = status;
1571		}
1572		if ((status & DATAIN_REG_READY) != 0) {
1573			uint8_t data;
1574
1575			data = aha_inb(aha, DATAIN_REG);
1576			if (reply_len < reply_buf_size) {
1577				*reply_data++ = data;
1578			} else {
1579				device_printf(aha->dev,
1580				    "aha_cmd - Discarded reply data "
1581				    "byte for opcode 0x%x\n", opcode);
1582			}
1583			/*
1584			 * Reset timeout to ensure at least a second
1585			 * between response bytes.
1586			 */
1587			cmd_timeout = MAX(cmd_timeout, 10000);
1588			reply_len++;
1589		}
1590		DELAY(100);
1591	}
1592	if (cmd_timeout == 0) {
1593		device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1594		    "intstat = 0x%x, reply_len = %d\n", status, intstat,
1595		    reply_len);
1596		return (ETIMEDOUT);
1597	}
1598
1599	/*
1600	 * Clear any pending interrupts.  Block interrupts so our
1601	 * interrupt handler is not re-entered.
1602	 */
1603	s = splcam();
1604	aha_intr(aha);
1605	splx(s);
1606
1607	if (error != 0)
1608		return (error);
1609
1610	/*
1611	 * If the command was rejected by the controller, tell the caller.
1612	 */
1613	if ((saved_status & CMD_INVALID) != 0) {
1614		PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1615		/*
1616		 * Some early adapters may not recover properly from
1617		 * an invalid command.  If it appears that the controller
1618		 * has wedged (i.e. status was not cleared by our interrupt
1619		 * reset above), perform a soft reset.
1620      		 */
1621		DELAY(1000);
1622		status = aha_inb(aha, STATUS_REG);
1623		if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1624			      CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1625		 || (status & (HA_READY|INIT_REQUIRED))
1626		  != (HA_READY|INIT_REQUIRED))
1627			ahareset(aha, /*hard_reset*/FALSE);
1628		return (EINVAL);
1629	}
1630
1631	if (param_len > 0) {
1632		/* The controller did not accept the full argument list */
1633		PRVERB((aha->dev, "Controller did not accept full argument "
1634		    "list (%d > 0)\n", param_len));
1635	 	return (E2BIG);
1636	}
1637
1638	if (reply_len != reply_buf_size) {
1639		/* Too much or too little data received */
1640		PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1641		    reply_len, reply_buf_size));
1642		return (EMSGSIZE);
1643	}
1644
1645	/* We were successful */
1646	return (0);
1647}
1648
1649static int
1650ahainitmboxes(struct aha_softc *aha)
1651{
1652	int error;
1653	init_24b_mbox_params_t init_mbox;
1654
1655	bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1656	bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1657	aha->cur_inbox = aha->in_boxes;
1658	aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1659	aha->cur_outbox = aha->out_boxes;
1660	aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1661
1662	/* Tell the adapter about them */
1663	init_mbox.num_mboxes = aha->num_boxes;
1664	ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1665	error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1666	    /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1667	    /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1668
1669	if (error != 0)
1670		printf("ahainitmboxes: Initialization command failed\n");
1671	return (error);
1672}
1673
1674/*
1675 * Update the XPT's idea of the negotiated transfer
1676 * parameters for a particular target.
1677 */
1678static void
1679ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1680{
1681	setup_data_t	setup_info;
1682	u_int		target;
1683	u_int		targ_offset;
1684	u_int		sync_period;
1685	int		error;
1686	uint8_t	param;
1687	targ_syncinfo_t	sync_info;
1688	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1689
1690	target = cts->ccb_h.target_id;
1691	targ_offset = (target & 0x7);
1692
1693	/*
1694	 * Inquire Setup Information.  This command retreives
1695	 * the sync info for older models.
1696	 */
1697	param = sizeof(setup_info);
1698	error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
1699	    (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1700
1701	if (error != 0) {
1702		device_printf(aha->dev,
1703		    "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1704		    error);
1705		return;
1706	}
1707
1708	sync_info = setup_info.syncinfo[targ_offset];
1709
1710	if (sync_info.sync == 0)
1711		spi->sync_offset = 0;
1712	else
1713		spi->sync_offset = sync_info.offset;
1714
1715	spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1716
1717	if (aha->boardid >= BOARD_1542CF)
1718		sync_period = 1000;
1719	else
1720		sync_period = 2000;
1721	sync_period += 500 * sync_info.period;
1722
1723	/* Convert ns value to standard SCSI sync rate */
1724	if (spi->sync_offset != 0)
1725		spi->sync_period = scsi_calc_syncparam(sync_period);
1726	else
1727		spi->sync_period = 0;
1728
1729	spi->valid = CTS_SPI_VALID_SYNC_RATE
1730		   | CTS_SPI_VALID_SYNC_OFFSET
1731		   | CTS_SPI_VALID_BUS_WIDTH;
1732        xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1733}
1734
1735static void
1736ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1737{
1738	struct aha_softc* aha;
1739
1740	aha = (struct aha_softc*)arg;
1741	aha->mailbox_physbase = segs->ds_addr;
1742}
1743
1744static void
1745ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1746{
1747	struct aha_softc* aha;
1748
1749	aha = (struct aha_softc*)arg;
1750	aha->aha_ccb_physbase = segs->ds_addr;
1751}
1752
1753static void
1754ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1755{
1756
1757	struct aha_softc* aha;
1758
1759	aha = (struct aha_softc*)arg;
1760	SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1761}
1762
1763static void
1764ahapoll(struct cam_sim *sim)
1765{
1766	aha_intr(cam_sim_softc(sim));
1767}
1768
1769static void
1770ahatimeout(void *arg)
1771{
1772	struct aha_ccb	*accb;
1773	union  ccb	*ccb;
1774	struct aha_softc *aha;
1775	int		 s;
1776	uint32_t	paddr;
1777	struct ccb_hdr *ccb_h;
1778
1779	accb = (struct aha_ccb *)arg;
1780	ccb = accb->ccb;
1781	aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1782	xpt_print_path(ccb->ccb_h.path);
1783	printf("CCB %p - timed out\n", (void *)accb);
1784
1785	s = splcam();
1786
1787	if ((accb->flags & ACCB_ACTIVE) == 0) {
1788		xpt_print_path(ccb->ccb_h.path);
1789		printf("CCB %p - timed out CCB already completed\n",
1790		    (void *)accb);
1791		splx(s);
1792		return;
1793	}
1794
1795	/*
1796	 * In order to simplify the recovery process, we ask the XPT
1797	 * layer to halt the queue of new transactions and we traverse
1798	 * the list of pending CCBs and remove their timeouts. This
1799	 * means that the driver attempts to clear only one error
1800	 * condition at a time.  In general, timeouts that occur
1801	 * close together are related anyway, so there is no benefit
1802	 * in attempting to handle errors in parrallel.  Timeouts will
1803	 * be reinstated when the recovery process ends.
1804	 */
1805	if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1806		if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1807			xpt_freeze_simq(aha->sim, /*count*/1);
1808			accb->flags |= ACCB_RELEASE_SIMQ;
1809		}
1810
1811		ccb_h = LIST_FIRST(&aha->pending_ccbs);
1812		while (ccb_h != NULL) {
1813			struct aha_ccb *pending_accb;
1814
1815			pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1816			untimeout(ahatimeout, pending_accb, ccb_h->timeout_ch);
1817			ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1818		}
1819	}
1820
1821	if ((accb->flags & ACCB_DEVICE_RESET) != 0
1822	 || aha->cur_outbox->action_code != AMBO_FREE) {
1823		/*
1824		 * Try a full host adapter/SCSI bus reset.
1825		 * We do this only if we have already attempted
1826		 * to clear the condition with a BDR, or we cannot
1827		 * attempt a BDR for lack of mailbox resources.
1828		 */
1829		ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1830		ahareset(aha, /*hardreset*/TRUE);
1831		device_printf(aha->dev, "No longer in timeout\n");
1832	} else {
1833		/*
1834		 * Send a Bus Device Reset message:
1835		 * The target that is holding up the bus may not
1836		 * be the same as the one that triggered this timeout
1837		 * (different commands have different timeout lengths),
1838		 * but we have no way of determining this from our
1839		 * timeout handler.  Our strategy here is to queue a
1840		 * BDR message to the target of the timed out command.
1841		 * If this fails, we'll get another timeout 2 seconds
1842		 * later which will attempt a bus reset.
1843		 */
1844		accb->flags |= ACCB_DEVICE_RESET;
1845		ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb, 2 * hz);
1846		aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1847
1848		/* No Data Transfer */
1849		aha->recovery_accb->hccb.datain = TRUE;
1850		aha->recovery_accb->hccb.dataout = TRUE;
1851		aha->recovery_accb->hccb.ahastat = 0;
1852		aha->recovery_accb->hccb.sdstat = 0;
1853		aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1854
1855		/* Tell the adapter about this command */
1856		paddr = ahaccbvtop(aha, aha->recovery_accb);
1857		ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1858		aha->cur_outbox->action_code = AMBO_START;
1859		aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1860		ahanextoutbox(aha);
1861	}
1862
1863	splx(s);
1864}
1865
1866int
1867aha_detach(struct aha_softc *aha)
1868{
1869	xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1870	xpt_free_path(aha->path);
1871	xpt_bus_deregister(cam_sim_path(aha->sim));
1872	cam_sim_free(aha->sim, /*free_devq*/TRUE);
1873	return (0);
1874}
1875