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