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