xref: /netbsd-src/sys/dev/eisa/ahb.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: ahb.c,v 1.6 1996/10/21 22:30:56 thorpej Exp $	*/
2 
3 #undef	AHBDEBUG
4 #ifdef DDB
5 #define	integrate
6 #else
7 #define	integrate	static inline
8 #endif
9 
10 /*
11  * Copyright (c) 1994, 1996 Charles M. Hannum.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by Charles M. Hannum.
24  * 4. The name of the author may not be used to endorse or promote products
25  *    derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Originally written by Julian Elischer (julian@tfs.com)
41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
42  *
43  * TRW Financial Systems, in accordance with their agreement with Carnegie
44  * Mellon University, makes this software available to CMU to distribute
45  * or use in any manner that they see fit as long as this message is kept with
46  * the software. For this reason TFS also grants any other persons or
47  * organisations permission to use or modify this software.
48  *
49  * TFS supplies this software to be publicly redistributed
50  * on the understanding that TFS is not responsible for the correct
51  * functioning of this software in any circumstances.
52  */
53 
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/errno.h>
59 #include <sys/ioctl.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62 #include <sys/buf.h>
63 #include <sys/proc.h>
64 #include <sys/user.h>
65 
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68 
69 #include <scsi/scsi_all.h>
70 #include <scsi/scsiconf.h>
71 
72 #include <dev/eisa/eisareg.h>
73 #include <dev/eisa/eisavar.h>
74 #include <dev/eisa/eisadevs.h>
75 #include <dev/eisa/ahbreg.h>
76 
77 #ifndef DDB
78 #define Debugger() panic("should call debugger here (aha1742.c)")
79 #endif /* ! DDB */
80 
81 #define AHB_ECB_MAX	32	/* store up to 32 ECBs at one time */
82 #define	ECB_HASH_SIZE	32	/* hash table size for phystokv */
83 #define	ECB_HASH_SHIFT	9
84 #define ECB_HASH(x)	((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
85 
86 #define	KVTOPHYS(x)	vtophys(x)
87 
88 struct ahb_softc {
89 	struct device sc_dev;
90 	bus_space_tag_t sc_iot;
91 
92 	bus_space_handle_t sc_ioh;
93 	int sc_irq;
94 	void *sc_ih;
95 
96 	struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
97 	TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
98 	struct ahb_ecb *sc_immed_ecb;	/* an outstanding immediete command */
99 	int sc_numecbs;
100 	int sc_scsi_dev;		/* our scsi id */
101 	struct scsi_link sc_link;
102 };
103 
104 void ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
105 void ahb_send_immed __P((struct ahb_softc *, u_long, struct ahb_ecb *));
106 int ahbintr __P((void *));
107 void ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
108 struct ahb_ecb *ahb_get_ecb __P((struct ahb_softc *, int));
109 struct ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
110 void ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
111 int ahb_find __P((bus_space_tag_t, bus_space_handle_t, struct ahb_softc *));
112 void ahb_init __P((struct ahb_softc *));
113 void ahbminphys __P((struct buf *));
114 int ahb_scsi_cmd __P((struct scsi_xfer *));
115 int ahb_poll __P((struct ahb_softc *, struct scsi_xfer *, int));
116 void ahb_timeout __P((void *));
117 
118 integrate void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
119 integrate void ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
120 
121 struct scsi_adapter ahb_switch = {
122 	ahb_scsi_cmd,
123 	ahbminphys,
124 	0,
125 	0,
126 };
127 
128 /* the below structure is so we have a default dev struct for our link struct */
129 struct scsi_device ahb_dev = {
130 	NULL,			/* Use default error handler */
131 	NULL,			/* have a queue, served by this */
132 	NULL,			/* have no async handler */
133 	NULL,			/* Use default 'done' routine */
134 };
135 
136 int	ahbmatch __P((struct device *, void *, void *));
137 void	ahbattach __P((struct device *, struct device *, void *));
138 
139 struct cfattach ahb_ca = {
140 	sizeof(struct ahb_softc), ahbmatch, ahbattach
141 };
142 
143 struct cfdriver ahb_cd = {
144 	NULL, "ahb", DV_DULL
145 };
146 
147 #define	AHB_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
148 
149 /*
150  * Check the slots looking for a board we recognise
151  * If we find one, note it's address (slot) and call
152  * the actual probe routine to check it out.
153  */
154 int
155 ahbmatch(parent, match, aux)
156 	struct device *parent;
157 	void *match, *aux;
158 {
159 	struct eisa_attach_args *ea = aux;
160 	bus_space_tag_t iot = ea->ea_iot;
161 	bus_space_handle_t ioh;
162 	int rv;
163 
164 	/* must match one of our known ID strings */
165 	if (strcmp(ea->ea_idstring, "ADP0000") &&
166 	    strcmp(ea->ea_idstring, "ADP0001") &&
167 	    strcmp(ea->ea_idstring, "ADP0002") &&
168 	    strcmp(ea->ea_idstring, "ADP0400"))
169 		return (0);
170 
171 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot),
172 	    EISA_SLOT_SIZE, 0, &ioh))
173 		return (0);
174 
175 	rv = !ahb_find(iot, ioh, NULL);
176 
177 	bus_space_unmap(iot, ioh, EISA_SLOT_SIZE);
178 
179 	return (rv);
180 }
181 
182 /*
183  * Attach all the sub-devices we can find
184  */
185 void
186 ahbattach(parent, self, aux)
187 	struct device *parent, *self;
188 	void *aux;
189 {
190 	struct eisa_attach_args *ea = aux;
191 	struct ahb_softc *sc = (void *)self;
192 	bus_space_tag_t iot = ea->ea_iot;
193 	bus_space_handle_t ioh;
194 	eisa_chipset_tag_t ec = ea->ea_ec;
195 	eisa_intr_handle_t ih;
196 	const char *model, *intrstr;
197 
198 	if (!strcmp(ea->ea_idstring, "ADP0000"))
199 		model = EISA_PRODUCT_ADP0000;
200 	else if (!strcmp(ea->ea_idstring, "ADP0001"))
201 		model = EISA_PRODUCT_ADP0001;
202 	else if (!strcmp(ea->ea_idstring, "ADP0002"))
203 		model = EISA_PRODUCT_ADP0002;
204 	else if (!strcmp(ea->ea_idstring, "ADP0400"))
205 		model = EISA_PRODUCT_ADP0400;
206 	else
207 		model = "unknown model!";
208 	printf(": %s\n", model);
209 
210 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot),
211 	   EISA_SLOT_SIZE, 0, &ioh))
212 		panic("ahbattach: could not map I/O addresses");
213 
214 	sc->sc_iot = iot;
215 	sc->sc_ioh = ioh;
216 	if (ahb_find(iot, ioh, sc))
217 		panic("ahbattach: ahb_find failed!");
218 
219 	ahb_init(sc);
220 	TAILQ_INIT(&sc->sc_free_ecb);
221 
222 	/*
223 	 * fill in the prototype scsi_link.
224 	 */
225 	sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
226 	sc->sc_link.adapter_softc = sc;
227 	sc->sc_link.adapter_target = sc->sc_scsi_dev;
228 	sc->sc_link.adapter = &ahb_switch;
229 	sc->sc_link.device = &ahb_dev;
230 	sc->sc_link.openings = 4;
231 
232 	if (eisa_intr_map(ec, sc->sc_irq, &ih)) {
233 		printf("%s: couldn't map interrupt (%d)\n",
234 		    sc->sc_dev.dv_xname, sc->sc_irq);
235 		return;
236 	}
237 	intrstr = eisa_intr_string(ec, ih);
238 	sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
239 	    ahbintr, sc);
240 	if (sc->sc_ih == NULL) {
241 		printf("%s: couldn't establish interrupt",
242 		    sc->sc_dev.dv_xname);
243 		if (intrstr != NULL)
244 			printf(" at %s", intrstr);
245 		printf("\n");
246 		return;
247 	}
248 	if (intrstr != NULL)
249 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
250 		    intrstr);
251 
252 	/*
253 	 * ask the adapter what subunits are present
254 	 */
255 	config_found(self, &sc->sc_link, scsiprint);
256 }
257 
258 /*
259  * Function to send a command out through a mailbox
260  */
261 void
262 ahb_send_mbox(sc, opcode, ecb)
263 	struct ahb_softc *sc;
264 	int opcode;
265 	struct ahb_ecb *ecb;
266 {
267 	bus_space_tag_t iot = sc->sc_iot;
268 	bus_space_handle_t ioh = sc->sc_ioh;
269 	int wait = 300;	/* 1ms should be enough */
270 
271 	while (--wait) {
272 		if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
273 		    == (G2STAT_MBOX_EMPTY))
274 			break;
275 		delay(10);
276 	}
277 	if (!wait) {
278 		printf("%s: board not responding\n", sc->sc_dev.dv_xname);
279 		Debugger();
280 	}
281 
282 	bus_space_write_4(iot, ioh, MBOXOUT0, KVTOPHYS(ecb)); /* don't know this will work */
283 	bus_space_write_1(iot, ioh, ATTN, opcode | ecb->xs->sc_link->target);
284 
285 	if ((ecb->xs->flags & SCSI_POLL) == 0)
286 		timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
287 }
288 
289 /*
290  * Function to  send an immediate type command to the adapter
291  */
292 void
293 ahb_send_immed(sc, cmd, ecb)
294 	struct ahb_softc *sc;
295 	u_long cmd;
296 	struct ahb_ecb *ecb;
297 {
298 	bus_space_tag_t iot = sc->sc_iot;
299 	bus_space_handle_t ioh = sc->sc_ioh;
300 	int wait = 100;	/* 1 ms enough? */
301 
302 	while (--wait) {
303 		if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
304 		    == (G2STAT_MBOX_EMPTY))
305 			break;
306 		delay(10);
307 	}
308 	if (!wait) {
309 		printf("%s: board not responding\n", sc->sc_dev.dv_xname);
310 		Debugger();
311 	}
312 
313 	bus_space_write_4(iot, ioh, MBOXOUT0, cmd);	/* don't know this will work */
314 	bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
315 	bus_space_write_1(iot, ioh, ATTN, OP_IMMED | ecb->xs->sc_link->target);
316 
317 	if ((ecb->xs->flags & SCSI_POLL) == 0)
318 		timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
319 }
320 
321 /*
322  * Catch an interrupt from the adaptor
323  */
324 int
325 ahbintr(arg)
326 	void *arg;
327 {
328 	struct ahb_softc *sc = arg;
329 	bus_space_tag_t iot = sc->sc_iot;
330 	bus_space_handle_t ioh = sc->sc_ioh;
331 	struct ahb_ecb *ecb;
332 	u_char ahbstat;
333 	u_long mboxval;
334 
335 #ifdef	AHBDEBUG
336 	printf("%s: ahbintr ", sc->sc_dev.dv_xname);
337 #endif /* AHBDEBUG */
338 
339 	if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
340 		return 0;
341 
342 	for (;;) {
343 		/*
344 		 * First get all the information and then
345 		 * acknowlege the interrupt
346 		 */
347 		ahbstat = bus_space_read_1(iot, ioh, G2INTST);
348 		mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
349 		bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
350 
351 #ifdef	AHBDEBUG
352 		printf("status = 0x%x ", ahbstat);
353 #endif /* AHBDEBUG */
354 
355 		/*
356 		 * Process the completed operation
357 		 */
358 		switch (ahbstat & G2INTST_INT_STAT) {
359 		case AHB_ECB_OK:
360 		case AHB_ECB_RECOVERED:
361 		case AHB_ECB_ERR:
362 			ecb = ahb_ecb_phys_kv(sc, mboxval);
363 			if (!ecb) {
364 				printf("%s: BAD ECB RETURNED!\n",
365 				    sc->sc_dev.dv_xname);
366 				goto next;	/* whatever it was, it'll timeout */
367 			}
368 			break;
369 
370 		case AHB_IMMED_ERR:
371 			ecb = sc->sc_immed_ecb;
372 			sc->sc_immed_ecb = 0;
373 			ecb->flags |= ECB_IMMED_FAIL;
374 			break;
375 
376 		case AHB_IMMED_OK:
377 			ecb = sc->sc_immed_ecb;
378 			sc->sc_immed_ecb = 0;
379 			break;
380 
381 		default:
382 			printf("%s: unexpected interrupt %x\n",
383 			    sc->sc_dev.dv_xname, ahbstat);
384 			goto next;
385 		}
386 
387 		untimeout(ahb_timeout, ecb);
388 		ahb_done(sc, ecb);
389 
390 	next:
391 		if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
392 			return 1;
393 	}
394 }
395 
396 integrate void
397 ahb_reset_ecb(sc, ecb)
398 	struct ahb_softc *sc;
399 	struct ahb_ecb *ecb;
400 {
401 
402 	ecb->flags = 0;
403 }
404 
405 /*
406  * A ecb (and hence a mbx-out is put onto the
407  * free list.
408  */
409 void
410 ahb_free_ecb(sc, ecb)
411 	struct ahb_softc *sc;
412 	struct ahb_ecb *ecb;
413 {
414 	int s;
415 
416 	s = splbio();
417 
418 	ahb_reset_ecb(sc, ecb);
419 	TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
420 
421 	/*
422 	 * If there were none, wake anybody waiting for one to come free,
423 	 * starting with queued entries.
424 	 */
425 	if (ecb->chain.tqe_next == 0)
426 		wakeup(&sc->sc_free_ecb);
427 
428 	splx(s);
429 }
430 
431 integrate void
432 ahb_init_ecb(sc, ecb)
433 	struct ahb_softc *sc;
434 	struct ahb_ecb *ecb;
435 {
436 	int hashnum;
437 
438 	bzero(ecb, sizeof(struct ahb_ecb));
439 	/*
440 	 * put in the phystokv hash table
441 	 * Never gets taken out.
442 	 */
443 	ecb->hashkey = KVTOPHYS(ecb);
444 	hashnum = ECB_HASH(ecb->hashkey);
445 	ecb->nexthash = sc->sc_ecbhash[hashnum];
446 	sc->sc_ecbhash[hashnum] = ecb;
447 	ahb_reset_ecb(sc, ecb);
448 }
449 
450 /*
451  * Get a free ecb
452  *
453  * If there are none, see if we can allocate a new one. If so, put it in the
454  * hash table too otherwise either return an error or sleep.
455  */
456 struct ahb_ecb *
457 ahb_get_ecb(sc, flags)
458 	struct ahb_softc *sc;
459 	int flags;
460 {
461 	struct ahb_ecb *ecb;
462 	int s;
463 
464 	s = splbio();
465 
466 	/*
467 	 * If we can and have to, sleep waiting for one to come free
468 	 * but only if we can't allocate a new one.
469 	 */
470 	for (;;) {
471 		ecb = sc->sc_free_ecb.tqh_first;
472 		if (ecb) {
473 			TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
474 			break;
475 		}
476 		if (sc->sc_numecbs < AHB_ECB_MAX) {
477 			ecb = (struct ahb_ecb *) malloc(sizeof(struct ahb_ecb),
478 			    M_TEMP, M_NOWAIT);
479 			if (!ecb) {
480 				printf("%s: can't malloc ecb\n",
481 				    sc->sc_dev.dv_xname);
482 				goto out;
483 			}
484 			ahb_init_ecb(sc, ecb);
485 			sc->sc_numecbs++;
486 			break;
487 		}
488 		if ((flags & SCSI_NOSLEEP) != 0)
489 			goto out;
490 		tsleep(&sc->sc_free_ecb, PRIBIO, "ahbecb", 0);
491 	}
492 
493 	ecb->flags |= ECB_ALLOC;
494 
495 out:
496 	splx(s);
497 	return ecb;
498 }
499 
500 /*
501  * given a physical address, find the ecb that it corresponds to.
502  */
503 struct ahb_ecb *
504 ahb_ecb_phys_kv(sc, ecb_phys)
505 	struct ahb_softc *sc;
506 	physaddr ecb_phys;
507 {
508 	int hashnum = ECB_HASH(ecb_phys);
509 	struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
510 
511 	while (ecb) {
512 		if (ecb->hashkey == ecb_phys)
513 			break;
514 		ecb = ecb->nexthash;
515 	}
516 	return ecb;
517 }
518 
519 /*
520  * We have a ecb which has been processed by the adaptor, now we look to see
521  * how the operation went.
522  */
523 void
524 ahb_done(sc, ecb)
525 	struct ahb_softc *sc;
526 	struct ahb_ecb *ecb;
527 {
528 	struct scsi_sense_data *s1, *s2;
529 	struct scsi_xfer *xs = ecb->xs;
530 
531 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahb_done\n"));
532 	/*
533 	 * Otherwise, put the results of the operation
534 	 * into the xfer and call whoever started it
535 	 */
536 	if ((ecb->flags & ECB_ALLOC) == 0) {
537 		printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
538 		Debugger();
539 	}
540 	if (ecb->flags & ECB_IMMED) {
541 		if (ecb->flags & ECB_IMMED_FAIL)
542 			xs->error = XS_DRIVER_STUFFUP;
543 		goto done;
544 	}
545 	if (xs->error == XS_NOERROR) {
546 		if (ecb->ecb_status.host_stat != HS_OK) {
547 			switch (ecb->ecb_status.host_stat) {
548 			case HS_TIMED_OUT:	/* No response */
549 				xs->error = XS_SELTIMEOUT;
550 				break;
551 			default:	/* Other scsi protocol messes */
552 				printf("%s: host_stat %x\n",
553 				    sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
554 				xs->error = XS_DRIVER_STUFFUP;
555 			}
556 		} else if (ecb->ecb_status.target_stat != SCSI_OK) {
557 			switch (ecb->ecb_status.target_stat) {
558 			case SCSI_CHECK:
559 				s1 = &ecb->ecb_sense;
560 				s2 = &xs->sense;
561 				*s2 = *s1;
562 				xs->error = XS_SENSE;
563 				break;
564 			case SCSI_BUSY:
565 				xs->error = XS_BUSY;
566 				break;
567 			default:
568 				printf("%s: target_stat %x\n",
569 				    sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
570 				xs->error = XS_DRIVER_STUFFUP;
571 			}
572 		} else
573 			xs->resid = 0;
574 	}
575 done:
576 	ahb_free_ecb(sc, ecb);
577 	xs->flags |= ITSDONE;
578 	scsi_done(xs);
579 }
580 
581 /*
582  * Start the board, ready for normal operation
583  */
584 int
585 ahb_find(iot, ioh, sc)
586 	bus_space_tag_t iot;
587 	bus_space_handle_t ioh;
588 	struct ahb_softc *sc;
589 {
590 	u_char intdef;
591 	int i, irq, busid;
592 	int wait = 1000;	/* 1 sec enough? */
593 
594 	bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
595 
596 #define	NO_NO 1
597 #ifdef NO_NO
598 	/*
599 	 * reset board, If it doesn't respond, assume
600 	 * that it's not there.. good for the probe
601 	 */
602 	bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
603 	delay(1000);
604 	bus_space_write_1(iot, ioh, G2CNTRL, 0);
605 	delay(10000);
606 	while (--wait) {
607 		if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
608 			break;
609 		delay(1000);
610 	}
611 	if (!wait) {
612 #ifdef	AHBDEBUG
613 		printf("ahb_find: No answer from aha1742 board\n");
614 #endif /* AHBDEBUG */
615 		return ENXIO;
616 	}
617 	i = bus_space_read_1(iot, ioh, MBOXIN0);
618 	if (i) {
619 		printf("self test failed, val = 0x%x\n", i);
620 		return EIO;
621 	}
622 
623 	/* Set it again, just to be sure. */
624 	bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
625 #endif
626 
627 	while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
628 		printf(".");
629 		bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
630 		delay(10000);
631 	}
632 
633 	intdef = bus_space_read_1(iot, ioh, INTDEF);
634 	switch (intdef & 0x07) {
635 	case INT9:
636 		irq = 9;
637 		break;
638 	case INT10:
639 		irq = 10;
640 		break;
641 	case INT11:
642 		irq = 11;
643 		break;
644 	case INT12:
645 		irq = 12;
646 		break;
647 	case INT14:
648 		irq = 14;
649 		break;
650 	case INT15:
651 		irq = 15;
652 		break;
653 	default:
654 		printf("illegal int setting %x\n", intdef);
655 		return EIO;
656 	}
657 
658 	bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN));	/* make sure we can interrupt */
659 
660 	/* who are we on the scsi bus? */
661 	busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
662 
663 	/* if we want to fill in softc, do so now */
664 	if (sc != NULL) {
665 		sc->sc_irq = irq;
666 		sc->sc_scsi_dev = busid;
667 	}
668 
669 	/*
670 	 * Note that we are going and return (to probe)
671 	 */
672 	return 0;
673 }
674 
675 void
676 ahb_init(sc)
677 	struct ahb_softc *sc;
678 {
679 
680 }
681 
682 void
683 ahbminphys(bp)
684 	struct buf *bp;
685 {
686 
687 	if (bp->b_bcount > ((AHB_NSEG - 1) << PGSHIFT))
688 		bp->b_bcount = ((AHB_NSEG - 1) << PGSHIFT);
689 	minphys(bp);
690 }
691 
692 /*
693  * start a scsi operation given the command and the data address.  Also needs
694  * the unit, target and lu.
695  */
696 int
697 ahb_scsi_cmd(xs)
698 	struct scsi_xfer *xs;
699 {
700 	struct scsi_link *sc_link = xs->sc_link;
701 	struct ahb_softc *sc = sc_link->adapter_softc;
702 	struct ahb_ecb *ecb;
703 	struct ahb_dma_seg *sg;
704 	int seg;		/* scatter gather seg being worked on */
705 	u_long thiskv, thisphys, nextphys;
706 	int bytes_this_seg, bytes_this_page, datalen, flags;
707 	int s;
708 
709 	SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
710 	/*
711 	 * get a ecb (mbox-out) to use. If the transfer
712 	 * is from a buf (possibly from interrupt time)
713 	 * then we can't allow it to sleep
714 	 */
715 	flags = xs->flags;
716 	if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
717 		xs->error = XS_DRIVER_STUFFUP;
718 		return TRY_AGAIN_LATER;
719 	}
720 	ecb->xs = xs;
721 	ecb->timeout = xs->timeout;
722 
723 	/*
724 	 * If it's a reset, we need to do an 'immediate'
725 	 * command, and store its ecb for later
726 	 * if there is already an immediate waiting,
727 	 * then WE must wait
728 	 */
729 	if (flags & SCSI_RESET) {
730 		ecb->flags |= ECB_IMMED;
731 		if (sc->sc_immed_ecb)
732 			return TRY_AGAIN_LATER;
733 		sc->sc_immed_ecb = ecb;
734 
735 		s = splbio();
736 		ahb_send_immed(sc, AHB_TARG_RESET, ecb);
737 		splx(s);
738 
739 		if ((flags & SCSI_POLL) == 0)
740 			return SUCCESSFULLY_QUEUED;
741 
742 		/*
743 		 * If we can't use interrupts, poll on completion
744 		 */
745 		if (ahb_poll(sc, xs, ecb->timeout))
746 			ahb_timeout(ecb);
747 		return COMPLETE;
748 	}
749 
750 	/*
751 	 * Put all the arguments for the xfer in the ecb
752 	 */
753 	ecb->opcode = ECB_SCSI_OP;
754 	ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
755 	ecb->opt2 = sc_link->lun | ECB_NRB;
756 	bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
757 	ecb->sense_ptr = KVTOPHYS(&ecb->ecb_sense);
758 	ecb->req_sense_length = sizeof(ecb->ecb_sense);
759 	ecb->status = KVTOPHYS(&ecb->ecb_status);
760 	ecb->ecb_status.host_stat = 0x00;
761 	ecb->ecb_status.target_stat = 0x00;
762 
763 	if (xs->datalen) {
764 		sg = ecb->ahb_dma;
765 		seg = 0;
766 #ifdef	TFS
767 		if (flags & SCSI_DATA_UIO) {
768 			struct iovec *iovp = ((struct uio *) xs->data)->uio_iov;
769 			datalen = ((struct uio *) xs->data)->uio_iovcnt;
770 			xs->datalen = 0;
771 			while (datalen && seg < AHB_NSEG) {
772 				sg->seg_addr = (physaddr)iovp->iov_base;
773 				sg->seg_len = iovp->iov_len;
774 				xs->datalen += iovp->iov_len;
775 				SC_DEBUGN(sc_link, SDEV_DB4, ("(0x%x@0x%x)",
776 				    iovp->iov_len, iovp->iov_base));
777 				sg++;
778 				iovp++;
779 				seg++;
780 				datalen--;
781 			}
782 		}
783 		else
784 #endif /*TFS */
785 		{
786 			/*
787 			 * Set up the scatter gather block
788 			 */
789 			SC_DEBUG(sc_link, SDEV_DB4,
790 			    ("%d @0x%x:- ", xs->datalen, xs->data));
791 			datalen = xs->datalen;
792 			thiskv = (long) xs->data;
793 			thisphys = KVTOPHYS(thiskv);
794 
795 			while (datalen && seg < AHB_NSEG) {
796 				bytes_this_seg = 0;
797 
798 				/* put in the base address */
799 				sg->seg_addr = thisphys;
800 
801 				SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
802 
803 				/* do it at least once */
804 				nextphys = thisphys;
805 				while (datalen && thisphys == nextphys) {
806 					/*
807 					 * This page is contiguous (physically)
808 					 * with the the last, just extend the
809 					 * length
810 					 */
811 					/* how far to the end of the page */
812 					nextphys = (thisphys & ~PGOFSET) + NBPG;
813 					bytes_this_page = nextphys - thisphys;
814 					/**** or the data ****/
815 					bytes_this_page = min(bytes_this_page,
816 							      datalen);
817 					bytes_this_seg += bytes_this_page;
818 					datalen -= bytes_this_page;
819 
820 					/* get more ready for the next page */
821 					thiskv = (thiskv & ~PGOFSET) + NBPG;
822 					if (datalen)
823 						thisphys = KVTOPHYS(thiskv);
824 				}
825 				/*
826 				 * next page isn't contiguous, finish the seg
827 				 */
828 				SC_DEBUGN(sc_link, SDEV_DB4,
829 				    ("(0x%x)", bytes_this_seg));
830 				sg->seg_len = bytes_this_seg;
831 				sg++;
832 				seg++;
833 			}
834 		}
835 		/*end of iov/kv decision */
836 		SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
837 		if (datalen) {
838 			/*
839 			 * there's still data, must have run out of segs!
840 			 */
841 			printf("%s: ahb_scsi_cmd, more than %d dma segs\n",
842 			    sc->sc_dev.dv_xname, AHB_NSEG);
843 			goto bad;
844 		}
845 		ecb->data_addr = KVTOPHYS(ecb->ahb_dma);
846 		ecb->data_length = seg * sizeof(struct ahb_dma_seg);
847 		ecb->opt1 |= ECB_S_G;
848 	} else {	/* No data xfer, use non S/G values */
849 		ecb->data_addr = (physaddr)0;
850 		ecb->data_length = 0;
851 	}
852 	ecb->link_addr = (physaddr)0;
853 
854 	s = splbio();
855 	ahb_send_mbox(sc, OP_START_ECB, ecb);
856 	splx(s);
857 
858 	/*
859 	 * Usually return SUCCESSFULLY QUEUED
860 	 */
861 	if ((flags & SCSI_POLL) == 0)
862 		return SUCCESSFULLY_QUEUED;
863 
864 	/*
865 	 * If we can't use interrupts, poll on completion
866 	 */
867 	if (ahb_poll(sc, xs, ecb->timeout)) {
868 		ahb_timeout(ecb);
869 		if (ahb_poll(sc, xs, ecb->timeout))
870 			ahb_timeout(ecb);
871 	}
872 	return COMPLETE;
873 
874 bad:
875 	xs->error = XS_DRIVER_STUFFUP;
876 	ahb_free_ecb(sc, ecb);
877 	return COMPLETE;
878 }
879 
880 /*
881  * Function to poll for command completion when in poll mode
882  */
883 int
884 ahb_poll(sc, xs, count)
885 	struct ahb_softc *sc;
886 	struct scsi_xfer *xs;
887 	int count;
888 {				/* in msec  */
889 	bus_space_tag_t iot = sc->sc_iot;
890 	bus_space_handle_t ioh = sc->sc_ioh;
891 
892 	while (count) {
893 		/*
894 		 * If we had interrupts enabled, would we
895 		 * have got an interrupt?
896 		 */
897 		if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
898 			ahbintr(sc);
899 		if (xs->flags & ITSDONE)
900 			return 0;
901 		delay(1000);
902 		count--;
903 	}
904 	return 1;
905 }
906 
907 void
908 ahb_timeout(arg)
909 	void *arg;
910 {
911 	struct ahb_ecb *ecb = arg;
912 	struct scsi_xfer *xs = ecb->xs;
913 	struct scsi_link *sc_link = xs->sc_link;
914 	struct ahb_softc *sc = sc_link->adapter_softc;
915 	int s;
916 
917 	sc_print_addr(sc_link);
918 	printf("timed out");
919 
920 	s = splbio();
921 
922 	if (ecb->flags & ECB_IMMED) {
923 		printf("\n");
924 		ecb->flags |= ECB_IMMED_FAIL;
925 		/* XXX Must reset! */
926 	} else
927 
928 	/*
929 	 * If it has been through before, then
930 	 * a previous abort has failed, don't
931 	 * try abort again
932 	 */
933 	if (ecb->flags & ECB_ABORT) {
934 		/* abort timed out */
935 		printf(" AGAIN\n");
936 		/* XXX Must reset! */
937 	} else {
938 		/* abort the operation that has timed out */
939 		printf("\n");
940 		ecb->xs->error = XS_TIMEOUT;
941 		ecb->timeout = AHB_ABORT_TIMEOUT;
942 		ecb->flags |= ECB_ABORT;
943 		ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
944 	}
945 
946 	splx(s);
947 }
948