ahb.c revision 110232
1/*
2 * CAM SCSI device driver for the Adaptec 174X SCSI Host adapter
3 *
4 * Copyright (c) 1998 Justin T. Gibbs
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice immediately at the beginning of the file, without modification,
12 *    this list of conditions, and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/ahb/ahb.c 110232 2003-02-02 13:17:30Z alfred $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37
38#include <machine/bus_pio.h>
39#include <machine/bus.h>
40#include <machine/resource.h>
41#include <sys/rman.h>
42
43#include <cam/cam.h>
44#include <cam/cam_ccb.h>
45#include <cam/cam_sim.h>
46#include <cam/cam_xpt_sim.h>
47#include <cam/cam_debug.h>
48
49#include <cam/scsi/scsi_message.h>
50
51#include <dev/eisa/eisaconf.h>
52
53#include <dev/ahb/ahbreg.h>
54
55#define ccb_ecb_ptr spriv_ptr0
56#define ccb_ahb_ptr spriv_ptr1
57
58#define ahb_inb(ahb, port)				\
59	bus_space_read_1((ahb)->tag, (ahb)->bsh, port)
60
61#define ahb_inl(ahb, port)				\
62	bus_space_read_4((ahb)->tag, (ahb)->bsh, port)
63
64#define ahb_outb(ahb, port, value)			\
65	bus_space_write_1((ahb)->tag, (ahb)->bsh, port, value)
66
67#define ahb_outl(ahb, port, value)			\
68	bus_space_write_4((ahb)->tag, (ahb)->bsh, port, value)
69
70static const char		*ahbmatch(eisa_id_t type);
71static struct ahb_softc		*ahballoc(u_long unit, struct resource *res);
72static void			 ahbfree(struct ahb_softc *ahb);
73static int			 ahbreset(struct ahb_softc *ahb);
74static void			 ahbmapecbs(void *arg, bus_dma_segment_t *segs,
75					    int nseg, int error);
76static int			 ahbxptattach(struct ahb_softc *ahb);
77static void			 ahbhandleimmed(struct ahb_softc *ahb,
78						u_int32_t mbox, u_int intstat);
79static void			 ahbcalcresid(struct ahb_softc *ahb,
80					      struct ecb *ecb, union ccb *ccb);
81static __inline void		 ahbdone(struct ahb_softc *ahb, u_int32_t mbox,
82					 u_int intstat);
83static void			 ahbintr(void *arg);
84static bus_dmamap_callback_t	 ahbexecuteecb;
85static void			 ahbaction(struct cam_sim *sim, union ccb *ccb);
86static void			 ahbpoll(struct cam_sim *sim);
87
88/* Our timeout handler */
89static timeout_t ahbtimeout;
90
91static __inline struct ecb*	ahbecbget(struct ahb_softc *ahb);
92static __inline void	 	ahbecbfree(struct ahb_softc* ahb,
93					   struct ecb* ecb);
94static __inline u_int32_t	ahbecbvtop(struct ahb_softc *ahb,
95					   struct ecb *ecb);
96static __inline struct ecb*	ahbecbptov(struct ahb_softc *ahb,
97					   u_int32_t ecb_addr);
98static __inline u_int32_t	ahbstatuspaddr(u_int32_t ecb_paddr);
99static __inline u_int32_t	ahbsensepaddr(u_int32_t ecb_paddr);
100static __inline u_int32_t	ahbsgpaddr(u_int32_t ecb_paddr);
101static __inline void		ahbqueuembox(struct ahb_softc *ahb,
102					     u_int32_t mboxval,
103					     u_int attn_code);
104
105static __inline struct ecb*
106ahbecbget(struct ahb_softc *ahb)
107{
108	struct	ecb* ecb;
109	int	s;
110
111	s = splcam();
112	if ((ecb = SLIST_FIRST(&ahb->free_ecbs)) != NULL)
113		SLIST_REMOVE_HEAD(&ahb->free_ecbs, links);
114	splx(s);
115
116	return (ecb);
117}
118
119static __inline void
120ahbecbfree(struct ahb_softc* ahb, struct ecb* ecb)
121{
122	int s;
123
124	s = splcam();
125	ecb->state = ECB_FREE;
126	SLIST_INSERT_HEAD(&ahb->free_ecbs, ecb, links);
127	splx(s);
128}
129
130static __inline u_int32_t
131ahbecbvtop(struct ahb_softc *ahb, struct ecb *ecb)
132{
133	return (ahb->ecb_physbase
134	      + (u_int32_t)((caddr_t)ecb - (caddr_t)ahb->ecb_array));
135}
136
137static __inline struct ecb*
138ahbecbptov(struct ahb_softc *ahb, u_int32_t ecb_addr)
139{
140	return (ahb->ecb_array
141	      + ((struct ecb*)ecb_addr - (struct ecb*)ahb->ecb_physbase));
142}
143
144static __inline u_int32_t
145ahbstatuspaddr(u_int32_t ecb_paddr)
146{
147	return (ecb_paddr + offsetof(struct ecb, status));
148}
149
150static __inline u_int32_t
151ahbsensepaddr(u_int32_t ecb_paddr)
152{
153	return (ecb_paddr + offsetof(struct ecb, sense));
154}
155
156static __inline u_int32_t
157ahbsgpaddr(u_int32_t ecb_paddr)
158{
159	return (ecb_paddr + offsetof(struct ecb, sg_list));
160}
161
162static __inline void
163ahbqueuembox(struct ahb_softc *ahb, u_int32_t mboxval, u_int attn_code)
164{
165	u_int loopmax = 300;
166	while (--loopmax) {
167		u_int status;
168
169		status = ahb_inb(ahb, HOSTSTAT);
170		if ((status & (HOSTSTAT_MBOX_EMPTY|HOSTSTAT_BUSY))
171		   == HOSTSTAT_MBOX_EMPTY)
172			break;
173		DELAY(20);
174	}
175	if (loopmax == 0)
176		panic("ahb%ld: adapter not taking commands\n", ahb->unit);
177
178	ahb_outl(ahb, MBOXOUT0, mboxval);
179	ahb_outb(ahb, ATTN, attn_code);
180}
181
182static const char *
183ahbmatch(eisa_id_t type)
184{
185	switch(type & 0xfffffe00) {
186		case EISA_DEVICE_ID_ADAPTEC_1740:
187			return ("Adaptec 174x SCSI host adapter");
188			break;
189		default:
190			break;
191	}
192	return (NULL);
193}
194
195static int
196ahbprobe(device_t dev)
197{
198	const char *desc;
199	u_int32_t iobase;
200	u_int32_t irq;
201	u_int8_t  intdef;
202	int shared;
203
204	desc = ahbmatch(eisa_get_id(dev));
205	if (!desc)
206	    return (ENXIO);
207	device_set_desc(dev, desc);
208
209	iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE) +
210	    AHB_EISA_SLOT_OFFSET;
211
212	eisa_add_iospace(dev, iobase, AHB_EISA_IOSIZE, RESVADDR_NONE);
213
214	intdef = inb(INTDEF + iobase);
215	switch (intdef & 0x7) {
216	case INT9:
217	    irq = 9;
218	    break;
219	case INT10:
220	    irq = 10;
221	    break;
222	case INT11:
223	    irq = 11;
224	    break;
225	case INT12:
226	    irq = 12;
227	    break;
228	case INT14:
229	    irq = 14;
230	    break;
231	case INT15:
232	    irq = 15;
233	    break;
234	default:
235	    printf("Adaptec 174X at slot %d: illegal "
236		   "irq setting %d\n", eisa_get_slot(dev),
237		   (intdef & 0x7));
238	    irq = 0;
239	    break;
240	}
241	if (irq == 0)
242	    return ENXIO;
243
244	shared = (inb(INTDEF + iobase) & INTLEVEL) ?
245		 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
246
247	eisa_add_intr(dev, irq, shared);
248
249	return 0;
250}
251
252static int
253ahbattach(device_t dev)
254{
255	/*
256	 * find unit and check we have that many defined
257	 */
258	struct	    ahb_softc *ahb;
259	struct	    ecb* next_ecb;
260	struct	    resource *io = 0;
261	struct	    resource *irq = 0;
262	int	    rid;
263	void	    *ih;
264
265	rid = 0;
266	io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
267				0, ~0, 1, RF_ACTIVE);
268	if (!io) {
269		device_printf(dev, "No I/O space?!\n");
270		return ENOMEM;
271	}
272
273	if ((ahb = ahballoc(device_get_unit(dev), io)) == NULL) {
274		goto error_exit2;
275	}
276
277	if (ahbreset(ahb) != 0)
278		goto error_exit;
279
280	rid = 0;
281	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
282				 0, ~0, 1, RF_ACTIVE);
283	if (!irq) {
284		device_printf(dev, "Can't allocate interrupt\n");
285		goto error_exit;
286	}
287
288	/*
289	 * Create our DMA tags.  These tags define the kinds of device
290	 * accessible memory allocations and memory mappings we will
291	 * need to perform during normal operation.
292	 */
293	/* DMA tag for mapping buffers into device visible space. */
294	/* XXX Should be a child of the EISA bus dma tag */
295	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
296			       /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
297			       /*highaddr*/BUS_SPACE_MAXADDR,
298			       /*filter*/NULL, /*filterarg*/NULL,
299			       /*maxsize*/MAXBSIZE, /*nsegments*/AHB_NSEG,
300			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
301			       /*flags*/BUS_DMA_ALLOCNOW,
302			       &ahb->buffer_dmat) != 0)
303		goto error_exit;
304
305	ahb->init_level++;
306
307	/* DMA tag for our ccb structures and ha inquiry data */
308	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
309			       /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
310			       /*highaddr*/BUS_SPACE_MAXADDR,
311			       /*filter*/NULL, /*filterarg*/NULL,
312			       (AHB_NECB * sizeof(struct ecb))
313			       + sizeof(*ahb->ha_inq_data),
314			       /*nsegments*/1,
315			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
316			       /*flags*/0, &ahb->ecb_dmat) != 0)
317		goto error_exit;
318
319	ahb->init_level++;
320
321	/* Allocation for our ccbs */
322	if (bus_dmamem_alloc(ahb->ecb_dmat, (void **)&ahb->ecb_array,
323			     BUS_DMA_NOWAIT, &ahb->ecb_dmamap) != 0)
324		goto error_exit;
325
326	ahb->ha_inq_data = (struct ha_inquiry_data *)&ahb->ecb_array[AHB_NECB];
327
328	ahb->init_level++;
329
330	/* And permanently map them */
331	bus_dmamap_load(ahb->ecb_dmat, ahb->ecb_dmamap,
332			ahb->ecb_array, AHB_NSEG * sizeof(struct ecb),
333			ahbmapecbs, ahb, /*flags*/0);
334
335	ahb->init_level++;
336
337	/* Allocate the buffer dmamaps for each of our ECBs */
338	bzero(ahb->ecb_array, (AHB_NECB * sizeof(struct ecb))
339	      + sizeof(*ahb->ha_inq_data));
340	next_ecb = ahb->ecb_array;
341	while (ahb->num_ecbs < AHB_NECB) {
342		u_int32_t ecb_paddr;
343
344		if (bus_dmamap_create(ahb->buffer_dmat, /*flags*/0,
345				      &next_ecb->dmamap))
346			break;
347		ecb_paddr = ahbecbvtop(ahb, next_ecb);
348		next_ecb->hecb.status_ptr = ahbstatuspaddr(ecb_paddr);
349		next_ecb->hecb.sense_ptr = ahbsensepaddr(ecb_paddr);
350		ahb->num_ecbs++;
351		ahbecbfree(ahb, next_ecb);
352		next_ecb++;
353	}
354
355	if (ahb->num_ecbs == 0)
356		goto error_exit;
357
358	ahb->init_level++;
359
360	/*
361	 * Now that we know we own the resources we need, register
362	 * our bus with the XPT.
363	 */
364	if (ahbxptattach(ahb))
365		goto error_exit;
366
367	/* Enable our interrupt */
368	bus_setup_intr(dev, irq, INTR_TYPE_CAM|INTR_ENTROPY, ahbintr, ahb, &ih);
369	return (0);
370
371error_exit:
372	/*
373	 * The board's IRQ line will not be left enabled
374	 * if we can't intialize correctly, so its safe
375	 * to release the irq.
376	 */
377	ahbfree(ahb);
378error_exit2:
379	if (io)
380		bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
381	if (irq)
382		bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
383	return (-1);
384}
385
386static struct ahb_softc *
387ahballoc(u_long unit, struct resource *res)
388{
389	struct	ahb_softc *ahb;
390
391	/*
392	 * Allocate a storage area for us
393	 */
394	ahb = malloc(sizeof(struct ahb_softc), M_DEVBUF, M_NOWAIT | M_ZERO);
395	if (!ahb) {
396		printf("ahb%ld: cannot malloc!\n", unit);
397		return (NULL);
398	}
399	SLIST_INIT(&ahb->free_ecbs);
400	LIST_INIT(&ahb->pending_ccbs);
401	ahb->unit = unit;
402	ahb->tag = rman_get_bustag(res);
403	ahb->bsh = rman_get_bushandle(res);
404	ahb->disc_permitted = ~0;
405	ahb->tags_permitted = ~0;
406
407	return (ahb);
408}
409
410static void
411ahbfree(struct ahb_softc *ahb)
412{
413	switch (ahb->init_level) {
414	default:
415	case 4:
416		bus_dmamap_unload(ahb->ecb_dmat, ahb->ecb_dmamap);
417	case 3:
418		bus_dmamem_free(ahb->ecb_dmat, ahb->ecb_array,
419				ahb->ecb_dmamap);
420		bus_dmamap_destroy(ahb->ecb_dmat, ahb->ecb_dmamap);
421	case 2:
422		bus_dma_tag_destroy(ahb->ecb_dmat);
423	case 1:
424		bus_dma_tag_destroy(ahb->buffer_dmat);
425	case 0:
426		break;
427	}
428	free(ahb, M_DEVBUF);
429}
430
431/*
432 * reset board, If it doesn't respond, return failure
433 */
434static int
435ahbreset(struct ahb_softc *ahb)
436{
437	int	wait = 1000;	/* 1 sec enough? */
438	int	test;
439
440	if ((ahb_inb(ahb, PORTADDR) & PORTADDR_ENHANCED) == 0) {
441		printf("ahb_reset: Controller not in enhanced mode\n");
442		return (-1);
443	}
444
445	ahb_outb(ahb, CONTROL, CNTRL_HARD_RST);
446	DELAY(1000);
447	ahb_outb(ahb, CONTROL, 0);
448	while (--wait) {
449		DELAY(1000);
450		if ((ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_BUSY) == 0)
451			break;
452	}
453
454	if (wait == 0) {
455		printf("ahbreset: No answer from aha1742 board\n");
456		return (-1);
457	}
458	if ((test = ahb_inb(ahb, MBOXIN0)) != 0) {
459		printf("ahb_reset: self test failed, val = 0x%x\n", test);
460		return (-1);
461	}
462	while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
463		ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
464		DELAY(10000);
465	}
466	return (0);
467}
468
469static void
470ahbmapecbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
471{
472	struct ahb_softc* ahb;
473
474	ahb = (struct ahb_softc*)arg;
475	ahb->ecb_physbase = segs->ds_addr;
476	/*
477	 * Space for adapter inquiry information is on the
478	 * tail of the ecb array.
479	 */
480	ahb->ha_inq_physbase = ahbecbvtop(ahb, &ahb->ecb_array[AHB_NECB]);
481}
482
483static int
484ahbxptattach(struct ahb_softc *ahb)
485{
486	struct cam_devq *devq;
487	struct ecb *ecb;
488	u_int  i;
489
490	/* Remeber who are we on the scsi bus */
491	ahb->scsi_id = ahb_inb(ahb, SCSIDEF) & HSCSIID;
492
493	/* Use extended translation?? */
494    	ahb->extended_trans = ahb_inb(ahb, RESV1) & EXTENDED_TRANS;
495
496	/* Fetch adapter inquiry data */
497	ecb = ahbecbget(ahb);	/* Always succeeds - no outstanding commands */
498	ecb->hecb.opcode = ECBOP_READ_HA_INQDATA;
499	ecb->hecb.flag_word1 = FW1_SUPPRESS_URUN_ERR|FW1_ERR_STATUS_BLK_ONLY;
500	ecb->hecb.data_ptr = ahb->ha_inq_physbase;
501	ecb->hecb.data_len = sizeof(struct ha_inquiry_data);
502	ecb->hecb.sense_ptr = 0;
503	ecb->state = ECB_ACTIVE;
504
505	/* Tell the adapter about this command */
506	ahbqueuembox(ahb, ahbecbvtop(ahb, ecb),
507		     ATTN_STARTECB|ahb->scsi_id);
508
509	/* Poll for interrupt completion */
510	for (i = 1000; ecb->state != ECB_FREE && i != 0; i--) {
511		ahbintr(ahb);
512		DELAY(1000);
513	}
514
515	ahb->num_ecbs = MIN(ahb->num_ecbs,
516			    ahb->ha_inq_data->scsi_data.reserved[1]);
517	printf("ahb%ld: %.8s %s SCSI Adapter, FW Rev. %.4s, ID=%d, %d ECBs\n",
518	       ahb->unit, ahb->ha_inq_data->scsi_data.product,
519	       (ahb->ha_inq_data->scsi_data.flags & 0x4) ? "Differential"
520							 : "Single Ended",
521	       ahb->ha_inq_data->scsi_data.revision,
522	       ahb->scsi_id, ahb->num_ecbs);
523
524	/* Restore sense paddr for future CCB clients */
525	ecb->hecb.sense_ptr = ahbsensepaddr(ahbecbvtop(ahb, ecb));
526
527	ahbecbfree(ahb, ecb);
528
529	/*
530	 * Create the device queue for our SIM.
531	 */
532	devq = cam_simq_alloc(ahb->num_ecbs);
533	if (devq == NULL)
534		return (ENOMEM);
535
536	/*
537	 * Construct our SIM entry
538	 */
539	ahb->sim = cam_sim_alloc(ahbaction, ahbpoll, "ahb", ahb, ahb->unit,
540				 2, ahb->num_ecbs, devq);
541	if (ahb->sim == NULL) {
542		cam_simq_free(devq);
543		return (ENOMEM);
544	}
545
546	if (xpt_bus_register(ahb->sim, 0) != CAM_SUCCESS) {
547		cam_sim_free(ahb->sim, /*free_devq*/TRUE);
548		return (ENXIO);
549	}
550
551	if (xpt_create_path(&ahb->path, /*periph*/NULL,
552			    cam_sim_path(ahb->sim), CAM_TARGET_WILDCARD,
553			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
554		xpt_bus_deregister(cam_sim_path(ahb->sim));
555		cam_sim_free(ahb->sim, /*free_devq*/TRUE);
556		return (ENXIO);
557	}
558
559	/*
560	 * Allow the board to generate interrupts.
561	 */
562	ahb_outb(ahb, INTDEF, ahb_inb(ahb, INTDEF) | INTEN);
563
564	return (0);
565}
566
567static void
568ahbhandleimmed(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
569{
570	struct ccb_hdr *ccb_h;
571	u_int target_id;
572
573	if (ahb->immed_cmd == 0) {
574		printf("ahb%ld: Immediate Command complete with no "
575		       " pending command\n", ahb->unit);
576		return;
577	}
578
579	target_id = intstat & INTSTAT_TARGET_MASK;
580
581	ccb_h = LIST_FIRST(&ahb->pending_ccbs);
582	while (ccb_h != NULL) {
583		struct ecb *pending_ecb;
584		union ccb *ccb;
585
586		pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
587		ccb = pending_ecb->ccb;
588		ccb_h = LIST_NEXT(ccb_h, sim_links.le);
589		if (ccb->ccb_h.target_id == target_id
590		 || target_id == ahb->scsi_id) {
591			untimeout(ahbtimeout, pending_ecb,
592				  ccb->ccb_h.timeout_ch);
593			LIST_REMOVE(&ccb->ccb_h, sim_links.le);
594			if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
595				bus_dmamap_unload(ahb->buffer_dmat,
596						  pending_ecb->dmamap);
597			if (pending_ecb == ahb->immed_ecb)
598				ccb->ccb_h.status =
599				    CAM_CMD_TIMEOUT|CAM_RELEASE_SIMQ;
600			else if (target_id == ahb->scsi_id)
601				ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
602			else
603				ccb->ccb_h.status = CAM_BDR_SENT;
604			ahbecbfree(ahb, pending_ecb);
605			xpt_done(ccb);
606		} else if (ahb->immed_ecb != NULL) {
607			/* Re-instate timeout */
608			ccb->ccb_h.timeout_ch =
609			    timeout(ahbtimeout, (caddr_t)pending_ecb,
610				    (ccb->ccb_h.timeout * hz) / 1000);
611		}
612	}
613
614	if (ahb->immed_ecb != NULL) {
615		ahb->immed_ecb = NULL;
616		printf("ahb%ld: No longer in timeout\n", ahb->unit);
617	} else if (target_id == ahb->scsi_id)
618		printf("ahb%ld: SCSI Bus Reset Delivered\n", ahb->unit);
619	else
620		printf("ahb%ld:  Bus Device Reset Delibered to target %d\n",
621		       ahb->unit, target_id);
622
623	ahb->immed_cmd = 0;
624}
625
626static void
627ahbcalcresid(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
628{
629	if (ecb->status.data_overrun != 0) {
630		/*
631		 * Overrun Condition.  The hardware doesn't
632		 * provide a meaningful byte count in this case
633		 * (the residual is always 0).  Tell the XPT
634		 * layer about the error.
635		 */
636		ccb->ccb_h.status = CAM_DATA_RUN_ERR;
637	} else {
638		ccb->csio.resid = ecb->status.resid_count;
639
640		if ((ecb->hecb.flag_word1 & FW1_SG_ECB) != 0) {
641			/*
642			 * For S/G transfers, the adapter provides a pointer
643			 * to the address in the last S/G element used and a
644			 * residual for that element.  So, we need to sum up
645			 * the elements that follow it in order to get a real
646			 * residual number.  If we have an overrun, the residual
647			 * reported will be 0 and we already know that all S/G
648			 * segments have been exhausted, so we can skip this
649			 * step.
650			 */
651			ahb_sg_t *sg;
652			int	  num_sg;
653
654			num_sg = ecb->hecb.data_len / sizeof(ahb_sg_t);
655
656			/* Find the S/G the adapter was working on */
657			for (sg = ecb->sg_list;
658			     num_sg != 0 && sg->addr != ecb->status.resid_addr;
659			     num_sg--, sg++)
660				;
661
662			/* Skip it */
663			num_sg--;
664			sg++;
665
666			/* Sum the rest */
667			for (; num_sg != 0; num_sg--, sg++)
668				ccb->csio.resid += sg->len;
669		}
670		/* Underruns are not errors */
671		ccb->ccb_h.status = CAM_REQ_CMP;
672	}
673}
674
675static void
676ahbprocesserror(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
677{
678	struct hardware_ecb *hecb;
679	struct ecb_status *status;
680
681	hecb = &ecb->hecb;
682	status = &ecb->status;
683	switch (status->ha_status) {
684	case HS_OK:
685		ccb->csio.scsi_status = status->scsi_status;
686		if (status->scsi_status != 0) {
687			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
688			if (status->sense_stored) {
689				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
690				ccb->csio.sense_resid =
691				    ccb->csio.sense_len - status->sense_len;
692				bcopy(&ecb->sense, &ccb->csio.sense_data,
693				      status->sense_len);
694			}
695		}
696		break;
697	case HS_TARGET_NOT_ASSIGNED:
698		ccb->ccb_h.status = CAM_PATH_INVALID;
699		break;
700	case HS_SEL_TIMEOUT:
701		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
702		break;
703	case HS_DATA_RUN_ERR:
704		ahbcalcresid(ahb, ecb, ccb);
705		break;
706	case HS_UNEXPECTED_BUSFREE:
707		ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
708		break;
709	case HS_INVALID_PHASE:
710		ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
711		break;
712	case HS_REQUEST_SENSE_FAILED:
713		ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
714		break;
715	case HS_TAG_MSG_REJECTED:
716	{
717		struct ccb_trans_settings neg;
718
719		xpt_print_path(ccb->ccb_h.path);
720		printf("refuses tagged commands.  Performing "
721		       "non-tagged I/O\n");
722		neg.flags = 0;
723		neg.valid = CCB_TRANS_TQ_VALID;
724		xpt_setup_ccb(&neg.ccb_h, ccb->ccb_h.path, /*priority*/1);
725		xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, &neg);
726		ahb->tags_permitted &= ~(0x01 << ccb->ccb_h.target_id);
727		ccb->ccb_h.status = CAM_MSG_REJECT_REC;
728		break;
729	}
730	case HS_FIRMWARE_LOAD_REQ:
731	case HS_HARDWARE_ERR:
732		/*
733		 * Tell the system that the Adapter
734		 * is no longer functional.
735		 */
736		ccb->ccb_h.status = CAM_NO_HBA;
737		break;
738	case HS_CMD_ABORTED_HOST:
739	case HS_CMD_ABORTED_ADAPTER:
740	case HS_ATN_TARGET_FAILED:
741	case HS_SCSI_RESET_ADAPTER:
742	case HS_SCSI_RESET_INCOMING:
743		ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
744		break;
745	case HS_INVALID_ECB_PARAM:
746		printf("ahb%ld: opcode 0x%02x, flag_word1 0x%02x, flag_word2 0x%02x\n",
747			ahb->unit, hecb->opcode, hecb->flag_word1, hecb->flag_word2);
748		ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
749		break;
750	case HS_DUP_TCB_RECEIVED:
751	case HS_INVALID_OPCODE:
752	case HS_INVALID_CMD_LINK:
753	case HS_PROGRAM_CKSUM_ERROR:
754		panic("ahb%ld: Can't happen host status %x occurred",
755		      ahb->unit, status->ha_status);
756		break;
757	}
758	if (ccb->ccb_h.status != CAM_REQ_CMP) {
759		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
760		ccb->ccb_h.status |= CAM_DEV_QFRZN;
761	}
762}
763
764static void
765ahbdone(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
766{
767	struct ecb *ecb;
768	union ccb *ccb;
769
770	ecb = ahbecbptov(ahb, mbox);
771
772	if ((ecb->state & ECB_ACTIVE) == 0)
773		panic("ecb not active");
774
775	ccb = ecb->ccb;
776
777	if (ccb != NULL) {
778		untimeout(ahbtimeout, ecb, ccb->ccb_h.timeout_ch);
779		LIST_REMOVE(&ccb->ccb_h, sim_links.le);
780
781		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
782			bus_dmasync_op_t op;
783
784			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
785				op = BUS_DMASYNC_POSTREAD;
786			else
787				op = BUS_DMASYNC_POSTWRITE;
788			bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
789			bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
790		}
791
792		if ((intstat & INTSTAT_MASK) == INTSTAT_ECB_OK) {
793			ccb->ccb_h.status = CAM_REQ_CMP;
794			ccb->csio.resid = 0;
795		} else {
796			ahbprocesserror(ahb, ecb, ccb);
797		}
798		ahbecbfree(ahb, ecb);
799		xpt_done(ccb);
800	} else {
801		/* Non CCB Command */
802		if ((intstat & INTSTAT_MASK) != INTSTAT_ECB_OK) {
803			printf("ahb%ld: Command 0%x Failed %x:%x:%x\n",
804			       ahb->unit, ecb->hecb.opcode,
805			       *((u_int16_t*)&ecb->status),
806			       ecb->status.ha_status, ecb->status.resid_count);
807		}
808		/* Client owns this ECB and will release it. */
809	}
810}
811
812/*
813 * Catch an interrupt from the adaptor
814 */
815static void
816ahbintr(void *arg)
817{
818	struct	  ahb_softc *ahb;
819	u_int	  intstat;
820	u_int32_t mbox;
821
822	ahb = (struct ahb_softc *)arg;
823
824	while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
825		/*
826		 * Fetch information about this interrupt.
827		 */
828		intstat = ahb_inb(ahb, INTSTAT);
829		mbox = ahb_inl(ahb, MBOXIN0);
830
831		/*
832		 * Reset interrupt latch.
833		 */
834		ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
835
836		/*
837		 * Process the completed operation
838		 */
839		switch (intstat & INTSTAT_MASK) {
840		case INTSTAT_ECB_OK:
841		case INTSTAT_ECB_CMPWRETRY:
842		case INTSTAT_ECB_CMPWERR:
843			ahbdone(ahb, mbox, intstat);
844			break;
845		case INTSTAT_AEN_OCCURED:
846			if ((intstat & INTSTAT_TARGET_MASK) == ahb->scsi_id) {
847				/* Bus Reset */
848				xpt_print_path(ahb->path);
849				switch (mbox) {
850				case HS_SCSI_RESET_ADAPTER:
851					printf("Host Adapter Initiated "
852					       "Bus Reset occurred\n");
853					break;
854				case HS_SCSI_RESET_INCOMING:
855					printf("Bus Reset Initiated "
856					       "by another device occurred\n");
857					break;
858				}
859				/* Notify the XPT */
860				xpt_async(AC_BUS_RESET, ahb->path, NULL);
861				break;
862			}
863			printf("Unsupported initiator selection AEN occured\n");
864			break;
865		case INTSTAT_IMMED_OK:
866		case INTSTAT_IMMED_ERR:
867			ahbhandleimmed(ahb, mbox, intstat);
868			break;
869		case INTSTAT_HW_ERR:
870			panic("Unrecoverable hardware Error Occurred\n");
871		}
872	}
873}
874
875static void
876ahbexecuteecb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
877{
878	struct	  ecb *ecb;
879	union	  ccb *ccb;
880	struct	  ahb_softc *ahb;
881	u_int32_t ecb_paddr;
882	int	  s;
883
884	ecb = (struct ecb *)arg;
885	ccb = ecb->ccb;
886	ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
887
888	if (error != 0) {
889		if (error != EFBIG)
890			printf("ahb%ld: Unexepected error 0x%x returned from "
891			       "bus_dmamap_load\n", ahb->unit, error);
892		if (ccb->ccb_h.status == CAM_REQ_INPROG) {
893			xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
894			ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
895		}
896		ahbecbfree(ahb, ecb);
897		xpt_done(ccb);
898		return;
899	}
900
901	ecb_paddr = ahbecbvtop(ahb, ecb);
902
903	if (nseg != 0) {
904		ahb_sg_t *sg;
905		bus_dma_segment_t *end_seg;
906		bus_dmasync_op_t op;
907
908		end_seg = dm_segs + nseg;
909
910		/* Copy the segments into our SG list */
911		sg = ecb->sg_list;
912		while (dm_segs < end_seg) {
913			sg->addr = dm_segs->ds_addr;
914			sg->len = dm_segs->ds_len;
915			sg++;
916			dm_segs++;
917		}
918
919		if (nseg > 1) {
920			ecb->hecb.flag_word1 |= FW1_SG_ECB;
921			ecb->hecb.data_ptr = ahbsgpaddr(ecb_paddr);
922			ecb->hecb.data_len = sizeof(ahb_sg_t) * nseg;
923		} else {
924			ecb->hecb.data_ptr = ecb->sg_list->addr;
925			ecb->hecb.data_len = ecb->sg_list->len;
926		}
927
928		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
929/*			ecb->hecb.flag_word2 |= FW2_DATA_DIR_IN; */
930			op = BUS_DMASYNC_PREREAD;
931		} else {
932			op = BUS_DMASYNC_PREWRITE;
933		}
934		/* ecb->hecb.flag_word2 |= FW2_CHECK_DATA_DIR; */
935
936		bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
937
938	} else {
939		ecb->hecb.data_ptr = 0;
940		ecb->hecb.data_len = 0;
941	}
942
943	s = splcam();
944
945	/*
946	 * Last time we need to check if this CCB needs to
947	 * be aborted.
948	 */
949	if (ccb->ccb_h.status != CAM_REQ_INPROG) {
950		if (nseg != 0)
951			bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
952		ahbecbfree(ahb, ecb);
953		xpt_done(ccb);
954		splx(s);
955		return;
956	}
957
958	ecb->state = ECB_ACTIVE;
959	ccb->ccb_h.status |= CAM_SIM_QUEUED;
960	LIST_INSERT_HEAD(&ahb->pending_ccbs, &ccb->ccb_h, sim_links.le);
961
962	/* Tell the adapter about this command */
963	ahbqueuembox(ahb, ecb_paddr, ATTN_STARTECB|ccb->ccb_h.target_id);
964
965	ccb->ccb_h.timeout_ch = timeout(ahbtimeout, (caddr_t)ecb,
966					(ccb->ccb_h.timeout * hz) / 1000);
967	splx(s);
968}
969
970static void
971ahbaction(struct cam_sim *sim, union ccb *ccb)
972{
973	struct	ahb_softc *ahb;
974
975	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahbaction\n"));
976
977	ahb = (struct ahb_softc *)cam_sim_softc(sim);
978
979	switch (ccb->ccb_h.func_code) {
980	/* Common cases first */
981	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
982	{
983		struct ecb *ecb;
984		struct hardware_ecb *hecb;
985
986		/*
987		 * get an ecb to use.
988		 */
989		if ((ecb = ahbecbget(ahb)) == NULL) {
990			/* Should never occur */
991			panic("Failed to get an ecb");
992		}
993
994		/*
995		 * So we can find the ECB when an abort is requested
996		 */
997		ecb->ccb = ccb;
998		ccb->ccb_h.ccb_ecb_ptr = ecb;
999		ccb->ccb_h.ccb_ahb_ptr = ahb;
1000
1001		/*
1002		 * Put all the arguments for the xfer in the ecb
1003		 */
1004		hecb = &ecb->hecb;
1005		hecb->opcode = ECBOP_INITIATOR_SCSI_CMD;
1006		hecb->flag_word1 = FW1_AUTO_REQUEST_SENSE
1007				 | FW1_ERR_STATUS_BLK_ONLY;
1008		hecb->flag_word2 = ccb->ccb_h.target_lun
1009				 | FW2_NO_RETRY_ON_BUSY;
1010		if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
1011			hecb->flag_word2 |= FW2_TAG_ENB
1012					 | ((ccb->csio.tag_action & 0x3)
1013					    << FW2_TAG_TYPE_SHIFT);
1014		}
1015		if ((ccb->ccb_h.flags & CAM_DIS_DISCONNECT) != 0)
1016			hecb->flag_word2 |= FW2_DISABLE_DISC;
1017		hecb->sense_len = ccb->csio.sense_len;
1018		hecb->cdb_len = ccb->csio.cdb_len;
1019		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1020			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
1021				bcopy(ccb->csio.cdb_io.cdb_ptr,
1022				      hecb->cdb, hecb->cdb_len);
1023			} else {
1024				/* I guess I could map it in... */
1025				ccb->ccb_h.status = CAM_REQ_INVALID;
1026				ahbecbfree(ahb, ecb);
1027				xpt_done(ccb);
1028				return;
1029			}
1030		} else {
1031			bcopy(ccb->csio.cdb_io.cdb_bytes,
1032			      hecb->cdb, hecb->cdb_len);
1033		}
1034
1035		/*
1036		 * If we have any data to send with this command,
1037		 * map it into bus space.
1038		 */
1039		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1040			if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1041				/*
1042				 * We've been given a pointer
1043				 * to a single buffer.
1044				 */
1045				if ((ccb->ccb_h.flags & CAM_DATA_PHYS)==0) {
1046					int s;
1047					int error;
1048
1049					s = splsoftvm();
1050					error = bus_dmamap_load(
1051					    ahb->buffer_dmat,
1052					    ecb->dmamap,
1053					    ccb->csio.data_ptr,
1054					    ccb->csio.dxfer_len,
1055					    ahbexecuteecb,
1056					    ecb, /*flags*/0);
1057					if (error == EINPROGRESS) {
1058						/*
1059						 * So as to maintain ordering,
1060						 * freeze the controller queue
1061						 * until our mapping is
1062						 * returned.
1063						 */
1064						xpt_freeze_simq(ahb->sim, 1);
1065						ccb->ccb_h.status |=
1066						    CAM_RELEASE_SIMQ;
1067					}
1068					splx(s);
1069				} else {
1070					struct bus_dma_segment seg;
1071
1072					/* Pointer to physical buffer */
1073					seg.ds_addr =
1074					    (bus_addr_t)ccb->csio.data_ptr;
1075					seg.ds_len = ccb->csio.dxfer_len;
1076					ahbexecuteecb(ecb, &seg, 1, 0);
1077				}
1078			} else {
1079				struct bus_dma_segment *segs;
1080
1081				if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
1082					panic("ahbaction - Physical segment "
1083					      "pointers unsupported");
1084
1085				if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0)
1086					panic("btaction - Virtual segment "
1087					      "addresses unsupported");
1088
1089				/* Just use the segments provided */
1090				segs = (struct bus_dma_segment *)
1091				    ccb->csio.data_ptr;
1092				ahbexecuteecb(ecb, segs, ccb->csio.sglist_cnt,
1093					     0);
1094			}
1095		} else {
1096			ahbexecuteecb(ecb, NULL, 0, 0);
1097		}
1098		break;
1099	}
1100	case XPT_EN_LUN:		/* Enable LUN as a target */
1101	case XPT_TARGET_IO:		/* Execute target I/O request */
1102	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1103	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1104	case XPT_ABORT:			/* Abort the specified CCB */
1105		/* XXX Implement */
1106		ccb->ccb_h.status = CAM_REQ_INVALID;
1107		xpt_done(ccb);
1108		break;
1109	case XPT_SET_TRAN_SETTINGS:
1110	{
1111		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1112		xpt_done(ccb);
1113		break;
1114	}
1115	case XPT_GET_TRAN_SETTINGS:
1116	/* Get default/user set transfer settings for the target */
1117	{
1118		struct	ccb_trans_settings *cts;
1119		u_int	target_mask;
1120
1121		cts = &ccb->cts;
1122		target_mask = 0x01 << ccb->ccb_h.target_id;
1123		if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1124			cts->flags = 0;
1125			if ((ahb->disc_permitted & target_mask) != 0)
1126				cts->flags |= CCB_TRANS_DISC_ENB;
1127			if ((ahb->tags_permitted & target_mask) != 0)
1128				cts->flags |= CCB_TRANS_TAG_ENB;
1129			cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1130			cts->sync_period = 25; /* 10MHz */
1131
1132			if (cts->sync_period != 0)
1133				cts->sync_offset = 15;
1134
1135			cts->valid = CCB_TRANS_SYNC_RATE_VALID
1136				   | CCB_TRANS_SYNC_OFFSET_VALID
1137				   | CCB_TRANS_BUS_WIDTH_VALID
1138				   | CCB_TRANS_DISC_VALID
1139				   | CCB_TRANS_TQ_VALID;
1140			ccb->ccb_h.status = CAM_REQ_CMP;
1141		} else {
1142			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1143		}
1144		xpt_done(ccb);
1145		break;
1146	}
1147	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1148	{
1149		int i;
1150		int s;
1151
1152		s = splcam();
1153		ahb->immed_cmd = IMMED_RESET;
1154		ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1155		/* Poll for interrupt completion */
1156		for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--) {
1157			DELAY(1000);
1158			ahbintr(cam_sim_softc(sim));
1159		}
1160		splx(s);
1161		break;
1162	}
1163	case XPT_CALC_GEOMETRY:
1164	{
1165		struct	  ccb_calc_geometry *ccg;
1166		u_int32_t size_mb;
1167		u_int32_t secs_per_cylinder;
1168
1169		ccg = &ccb->ccg;
1170		size_mb = ccg->volume_size
1171			/ ((1024L * 1024L) / ccg->block_size);
1172
1173		if (size_mb > 1024 && (ahb->extended_trans != 0)) {
1174			ccg->heads = 255;
1175			ccg->secs_per_track = 63;
1176		} else {
1177			ccg->heads = 64;
1178			ccg->secs_per_track = 32;
1179		}
1180		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1181		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1182		ccb->ccb_h.status = CAM_REQ_CMP;
1183		xpt_done(ccb);
1184		break;
1185	}
1186	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1187	{
1188		int i;
1189
1190		ahb->immed_cmd = IMMED_RESET;
1191		ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1192		/* Poll for interrupt completion */
1193		for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--)
1194			DELAY(1000);
1195		ccb->ccb_h.status = CAM_REQ_CMP;
1196		xpt_done(ccb);
1197		break;
1198	}
1199	case XPT_TERM_IO:		/* Terminate the I/O process */
1200		/* XXX Implement */
1201		ccb->ccb_h.status = CAM_REQ_INVALID;
1202		xpt_done(ccb);
1203		break;
1204	case XPT_PATH_INQ:		/* Path routing inquiry */
1205	{
1206		struct ccb_pathinq *cpi = &ccb->cpi;
1207
1208		cpi->version_num = 1; /* XXX??? */
1209		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1210		cpi->target_sprt = 0;
1211		cpi->hba_misc = 0;
1212		cpi->hba_eng_cnt = 0;
1213		cpi->max_target = 7;
1214		cpi->max_lun = 7;
1215		cpi->initiator_id = ahb->scsi_id;
1216		cpi->bus_id = cam_sim_bus(sim);
1217		cpi->base_transfer_speed = 3300;
1218		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1219		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
1220		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1221		cpi->unit_number = cam_sim_unit(sim);
1222		cpi->ccb_h.status = CAM_REQ_CMP;
1223		xpt_done(ccb);
1224		break;
1225	}
1226#if 0
1227	/* Need these??? */
1228        case XPT_IMMED_NOTIFY:		/* Notify Host Target driver of event */
1229        case XPT_NOTIFY_ACK:		/* Acknowledgement of event */
1230#endif
1231	default:
1232		ccb->ccb_h.status = CAM_REQ_INVALID;
1233		xpt_done(ccb);
1234		break;
1235	}
1236}
1237
1238static void
1239ahbpoll(struct cam_sim *sim)
1240{
1241	ahbintr(cam_sim_softc(sim));
1242}
1243
1244static void
1245ahbtimeout(void *arg)
1246{
1247	struct ecb	 *ecb;
1248	union  ccb	 *ccb;
1249	struct ahb_softc *ahb;
1250	int		  s;
1251
1252	ecb = (struct ecb *)arg;
1253	ccb = ecb->ccb;
1254	ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
1255	xpt_print_path(ccb->ccb_h.path);
1256	printf("ECB %p - timed out\n", (void *)ecb);
1257
1258	s = splcam();
1259
1260	if ((ecb->state & ECB_ACTIVE) == 0) {
1261		xpt_print_path(ccb->ccb_h.path);
1262		printf("ECB %p - timed out ECB already completed\n",
1263		       (void *)ecb);
1264		splx(s);
1265		return;
1266	}
1267	/*
1268	 * In order to simplify the recovery process, we ask the XPT
1269	 * layer to halt the queue of new transactions and we traverse
1270	 * the list of pending CCBs and remove their timeouts. This
1271	 * means that the driver attempts to clear only one error
1272	 * condition at a time.  In general, timeouts that occur
1273	 * close together are related anyway, so there is no benefit
1274	 * in attempting to handle errors in parrallel.  Timeouts will
1275	 * be reinstated when the recovery process ends.
1276	 */
1277	if ((ecb->state & ECB_DEVICE_RESET) == 0) {
1278		struct ccb_hdr *ccb_h;
1279
1280		if ((ecb->state & ECB_RELEASE_SIMQ) == 0) {
1281			xpt_freeze_simq(ahb->sim, /*count*/1);
1282			ecb->state |= ECB_RELEASE_SIMQ;
1283		}
1284
1285		ccb_h = LIST_FIRST(&ahb->pending_ccbs);
1286		while (ccb_h != NULL) {
1287			struct ecb *pending_ecb;
1288
1289			pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
1290			untimeout(ahbtimeout, pending_ecb, ccb_h->timeout_ch);
1291			ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1292		}
1293
1294		/* Store for our interrupt handler */
1295		ahb->immed_ecb = ecb;
1296
1297		/*
1298		 * Send a Bus Device Reset message:
1299		 * The target that is holding up the bus may not
1300		 * be the same as the one that triggered this timeout
1301		 * (different commands have different timeout lengths),
1302		 * but we have no way of determining this from our
1303		 * timeout handler.  Our strategy here is to queue a
1304		 * BDR message to the target of the timed out command.
1305		 * If this fails, we'll get another timeout 2 seconds
1306		 * later which will attempt a bus reset.
1307		 */
1308		xpt_print_path(ccb->ccb_h.path);
1309		printf("Queuing BDR\n");
1310		ecb->state |= ECB_DEVICE_RESET;
1311		ccb->ccb_h.timeout_ch =
1312		    timeout(ahbtimeout, (caddr_t)ecb, 2 * hz);
1313
1314		ahb->immed_cmd = IMMED_RESET;
1315		ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1316	} else if ((ecb->state & ECB_SCSIBUS_RESET) != 0) {
1317		/*
1318		 * Try a SCSI bus reset.  We do this only if we
1319		 * have already attempted to clear the condition with a BDR.
1320		 */
1321		xpt_print_path(ccb->ccb_h.path);
1322		printf("Attempting SCSI Bus reset\n");
1323		ecb->state |= ECB_SCSIBUS_RESET;
1324		ccb->ccb_h.timeout_ch =
1325		    timeout(ahbtimeout, (caddr_t)ecb, 2 * hz);
1326		ahb->immed_cmd = IMMED_RESET;
1327		ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1328	} else {
1329		/* Bring out the hammer... */
1330		ahbreset(ahb);
1331
1332		/* Simulate the reset complete interrupt */
1333		ahbhandleimmed(ahb, 0, ahb->scsi_id|INTSTAT_IMMED_OK);
1334	}
1335
1336	splx(s);
1337}
1338
1339static device_method_t ahb_eisa_methods[] = {
1340	/* Device interface */
1341	DEVMETHOD(device_probe,		ahbprobe),
1342	DEVMETHOD(device_attach,	ahbattach),
1343
1344	{ 0, 0 }
1345};
1346
1347static driver_t ahb_eisa_driver = {
1348	"ahb",
1349	ahb_eisa_methods,
1350	1,			/* unused */
1351};
1352
1353static devclass_t ahb_devclass;
1354
1355DRIVER_MODULE(ahb, eisa, ahb_eisa_driver, ahb_devclass, 0, 0);
1356