xref: /openbsd-src/sys/dev/ic/adv.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: adv.c,v 1.24 2009/02/16 21:19:06 miod Exp $	*/
2 /*	$NetBSD: adv.c,v 1.6 1998/10/28 20:39:45 dante Exp $	*/
3 
4 /*
5  * Generic driver for the Advanced Systems Inc. Narrow SCSI controllers
6  *
7  * Copyright (c) 1998 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * Author: Baldassare Dante Profeta <dante@mclink.it>
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/user.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 
49 #include <scsi/scsi_all.h>
50 #include <scsi/scsiconf.h>
51 
52 #include <dev/ic/adv.h>
53 #include <dev/ic/advlib.h>
54 
55 #ifndef DDB
56 #define	Debugger()	panic("should call debugger here (adv.c)")
57 #endif /* ! DDB */
58 
59 
60 /* #define ASC_DEBUG */
61 
62 /******************************************************************************/
63 
64 
65 static int adv_alloc_ccbs(ASC_SOFTC *);
66 static int adv_create_ccbs(ASC_SOFTC *, ADV_CCB *, int);
67 static void adv_free_ccb(ASC_SOFTC *, ADV_CCB *);
68 static void adv_reset_ccb(ADV_CCB *);
69 static int adv_init_ccb(ASC_SOFTC *, ADV_CCB *);
70 static ADV_CCB *adv_get_ccb(ASC_SOFTC *, int);
71 static void adv_queue_ccb(ASC_SOFTC *, ADV_CCB *);
72 static void adv_start_ccbs(ASC_SOFTC *);
73 
74 static u_int8_t *adv_alloc_overrunbuf(char *dvname, bus_dma_tag_t);
75 
76 static int adv_scsi_cmd(struct scsi_xfer *);
77 static void advminphys(struct buf *, struct scsi_link *);
78 static void adv_narrow_isr_callback(ASC_SOFTC *, ASC_QDONE_INFO *);
79 
80 static int adv_poll(ASC_SOFTC *, struct scsi_xfer *, int);
81 static void adv_timeout(void *);
82 static void adv_watchdog(void *);
83 
84 
85 /******************************************************************************/
86 
87 
88 struct cfdriver adv_cd = {
89 	NULL, "adv", DV_DULL
90 };
91 
92 
93 struct scsi_adapter adv_switch =
94 {
95 	adv_scsi_cmd,		/* called to start/enqueue a SCSI command */
96 	advminphys,		/* to limit the transfer to max device can do */
97 	0,			/* IT SEEMS IT IS NOT USED YET */
98 	0,			/* as above... */
99 };
100 
101 
102 /* the below structure is so we have a default dev struct for out link struct */
103 struct scsi_device adv_dev =
104 {
105 	NULL,			/* Use default error handler */
106 	NULL,			/* have a queue, served by this */
107 	NULL,			/* have no async handler */
108 	NULL,			/* Use default 'done' routine */
109 };
110 
111 
112 #define ADV_ABORT_TIMEOUT       2000	/* time to wait for abort (mSec) */
113 #define ADV_WATCH_TIMEOUT       1000	/* time to wait for watchdog (mSec) */
114 
115 
116 /******************************************************************************/
117 /*                             Control Blocks routines                        */
118 /******************************************************************************/
119 
120 
121 static int
122 adv_alloc_ccbs(sc)
123 	ASC_SOFTC      *sc;
124 {
125 	bus_dma_segment_t seg;
126 	int             error, rseg;
127 
128 	/*
129          * Allocate the control blocks.
130          */
131 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adv_control),
132 			   NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
133 		printf("%s: unable to allocate control structures,"
134 		       " error = %d\n", sc->sc_dev.dv_xname, error);
135 		return (error);
136 	}
137 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
138 		   sizeof(struct adv_control), (caddr_t *) & sc->sc_control,
139 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
140 		printf("%s: unable to map control structures, error = %d\n",
141 		       sc->sc_dev.dv_xname, error);
142 		return (error);
143 	}
144 	/*
145          * Create and load the DMA map used for the control blocks.
146          */
147 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adv_control),
148 			   1, sizeof(struct adv_control), 0, BUS_DMA_NOWAIT,
149 				       &sc->sc_dmamap_control)) != 0) {
150 		printf("%s: unable to create control DMA map, error = %d\n",
151 		       sc->sc_dev.dv_xname, error);
152 		return (error);
153 	}
154 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
155 			   sc->sc_control, sizeof(struct adv_control), NULL,
156 				     BUS_DMA_NOWAIT)) != 0) {
157 		printf("%s: unable to load control DMA map, error = %d\n",
158 		       sc->sc_dev.dv_xname, error);
159 		return (error);
160 	}
161 	return (0);
162 }
163 
164 
165 /*
166  * Create a set of ccbs and add them to the free list.  Called once
167  * by adv_init().  We return the number of CCBs successfully created.
168  */
169 static int
170 adv_create_ccbs(sc, ccbstore, count)
171 	ASC_SOFTC      *sc;
172 	ADV_CCB        *ccbstore;
173 	int             count;
174 {
175 	ADV_CCB        *ccb;
176 	int             i, error;
177 
178 	bzero(ccbstore, sizeof(ADV_CCB) * count);
179 	for (i = 0; i < count; i++) {
180 		ccb = &ccbstore[i];
181 		if ((error = adv_init_ccb(sc, ccb)) != 0) {
182 			printf("%s: unable to initialize ccb, error = %d\n",
183 			       sc->sc_dev.dv_xname, error);
184 			return (i);
185 		}
186 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
187 	}
188 
189 	return (i);
190 }
191 
192 
193 /*
194  * A ccb is put onto the free list.
195  */
196 static void
197 adv_free_ccb(sc, ccb)
198 	ASC_SOFTC      *sc;
199 	ADV_CCB        *ccb;
200 {
201 	int             s;
202 
203 	s = splbio();
204 
205 	adv_reset_ccb(ccb);
206 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
207 
208 	/*
209          * If there were none, wake anybody waiting for one to come free,
210          * starting with queued entries.
211          */
212 	if (TAILQ_NEXT(ccb, chain) == NULL)
213 		wakeup(&sc->sc_free_ccb);
214 
215 	splx(s);
216 }
217 
218 
219 static void
220 adv_reset_ccb(ccb)
221 	ADV_CCB        *ccb;
222 {
223 
224 	ccb->flags = 0;
225 }
226 
227 
228 static int
229 adv_init_ccb(sc, ccb)
230 	ASC_SOFTC      *sc;
231 	ADV_CCB        *ccb;
232 {
233 	int             error;
234 
235 	/*
236          * Create the DMA map for this CCB.
237          */
238 	error = bus_dmamap_create(sc->sc_dmat,
239 				  (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
240 			 ASC_MAX_SG_LIST, (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
241 		   0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
242 	if (error) {
243 		printf("%s: unable to create DMA map, error = %d\n",
244 		       sc->sc_dev.dv_xname, error);
245 		return (error);
246 	}
247 	adv_reset_ccb(ccb);
248 	return (0);
249 }
250 
251 
252 /*
253  * Get a free ccb
254  *
255  * If there are none, see if we can allocate a new one
256  */
257 static ADV_CCB *
258 adv_get_ccb(sc, flags)
259 	ASC_SOFTC      *sc;
260 	int             flags;
261 {
262 	ADV_CCB        *ccb = 0;
263 	int             s;
264 
265 	s = splbio();
266 
267 	/*
268          * If we can and have to, sleep waiting for one to come free
269          * but only if we can't allocate a new one.
270          */
271 	for (;;) {
272 		ccb = TAILQ_FIRST(&sc->sc_free_ccb);
273 		if (ccb) {
274 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
275 			break;
276 		}
277 		if ((flags & SCSI_NOSLEEP) != 0)
278 			goto out;
279 
280 		tsleep(&sc->sc_free_ccb, PRIBIO, "advccb", 0);
281 	}
282 
283 	ccb->flags |= CCB_ALLOC;
284 
285 out:
286 	splx(s);
287 	return (ccb);
288 }
289 
290 
291 /*
292  * Queue a CCB to be sent to the controller, and send it if possible.
293  */
294 static void
295 adv_queue_ccb(sc, ccb)
296 	ASC_SOFTC      *sc;
297 	ADV_CCB        *ccb;
298 {
299 
300 	timeout_set(&ccb->xs->stimeout, adv_timeout, ccb);
301 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
302 
303 	adv_start_ccbs(sc);
304 }
305 
306 
307 static void
308 adv_start_ccbs(sc)
309 	ASC_SOFTC      *sc;
310 {
311 	ADV_CCB        *ccb;
312 	struct scsi_xfer *xs;
313 
314 	while ((ccb = TAILQ_FIRST(&sc->sc_waiting_ccb)) != NULL) {
315 
316 		xs = ccb->xs;
317 		if (ccb->flags & CCB_WATCHDOG)
318 			timeout_del(&xs->stimeout);
319 
320 		if (AscExeScsiQueue(sc, &ccb->scsiq) == ASC_BUSY) {
321 			ccb->flags |= CCB_WATCHDOG;
322 			timeout_set(&xs->stimeout, adv_watchdog, ccb);
323 			timeout_add_msec(&xs->stimeout, ADV_WATCH_TIMEOUT);
324 			break;
325 		}
326 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
327 
328 		if ((ccb->xs->flags & SCSI_POLL) == 0) {
329 			timeout_set(&xs->stimeout, adv_timeout, ccb);
330 			timeout_add_msec(&xs->stimeout, ccb->timeout);
331 		}
332 	}
333 }
334 
335 
336 /******************************************************************************/
337 /*                      DMA able memory allocation routines                   */
338 /******************************************************************************/
339 
340 
341 /*
342  * Allocate a DMA able memory for overrun_buffer.
343  * This memory can be safely shared among all the AdvanSys boards.
344  */
345 u_int8_t       *
346 adv_alloc_overrunbuf(dvname, dmat)
347 	char           *dvname;
348 	bus_dma_tag_t   dmat;
349 {
350 	static u_int8_t *overrunbuf = NULL;
351 
352 	bus_dmamap_t    ovrbuf_dmamap;
353 	bus_dma_segment_t seg;
354 	int             rseg, error;
355 
356 
357 	/*
358          * if an overrun buffer has been already allocated don't allocate it
359          * again. Instead return the address of the allocated buffer.
360          */
361 	if (overrunbuf)
362 		return (overrunbuf);
363 
364 
365 	if ((error = bus_dmamem_alloc(dmat, ASC_OVERRUN_BSIZE,
366 			   NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
367 		printf("%s: unable to allocate overrun buffer, error = %d\n",
368 		       dvname, error);
369 		return (0);
370 	}
371 	if ((error = bus_dmamem_map(dmat, &seg, rseg, ASC_OVERRUN_BSIZE,
372 	(caddr_t *) & overrunbuf, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
373 		printf("%s: unable to map overrun buffer, error = %d\n",
374 		       dvname, error);
375 
376 		bus_dmamem_free(dmat, &seg, 1);
377 		return (0);
378 	}
379 	if ((error = bus_dmamap_create(dmat, ASC_OVERRUN_BSIZE, 1,
380 	      ASC_OVERRUN_BSIZE, 0, BUS_DMA_NOWAIT, &ovrbuf_dmamap)) != 0) {
381 		printf("%s: unable to create overrun buffer DMA map,"
382 		       " error = %d\n", dvname, error);
383 
384 		bus_dmamem_unmap(dmat, overrunbuf, ASC_OVERRUN_BSIZE);
385 		bus_dmamem_free(dmat, &seg, 1);
386 		return (0);
387 	}
388 	if ((error = bus_dmamap_load(dmat, ovrbuf_dmamap, overrunbuf,
389 			   ASC_OVERRUN_BSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
390 		printf("%s: unable to load overrun buffer DMA map,"
391 		       " error = %d\n", dvname, error);
392 
393 		bus_dmamap_destroy(dmat, ovrbuf_dmamap);
394 		bus_dmamem_unmap(dmat, overrunbuf, ASC_OVERRUN_BSIZE);
395 		bus_dmamem_free(dmat, &seg, 1);
396 		return (0);
397 	}
398 	return (overrunbuf);
399 }
400 
401 
402 /******************************************************************************/
403 /*                         SCSI layer interfacing routines                    */
404 /******************************************************************************/
405 
406 
407 int
408 adv_init(sc)
409 	ASC_SOFTC      *sc;
410 {
411 	int             warn;
412 
413 	if (!AscFindSignature(sc->sc_iot, sc->sc_ioh))
414 		panic("adv_init: adv_find_signature failed");
415 
416 	/*
417          * Read the board configuration
418          */
419 	AscInitASC_SOFTC(sc);
420 	warn = AscInitFromEEP(sc);
421 	if (warn) {
422 		printf("%s -get: ", sc->sc_dev.dv_xname);
423 		switch (warn) {
424 		case -1:
425 			printf("Chip is not halted\n");
426 			break;
427 
428 		case -2:
429 			printf("Couldn't get MicroCode Start"
430 			       " address\n");
431 			break;
432 
433 		case ASC_WARN_IO_PORT_ROTATE:
434 			printf("I/O port address modified\n");
435 			break;
436 
437 		case ASC_WARN_AUTO_CONFIG:
438 			printf("I/O port increment switch enabled\n");
439 			break;
440 
441 		case ASC_WARN_EEPROM_CHKSUM:
442 			printf("EEPROM checksum error\n");
443 			break;
444 
445 		case ASC_WARN_IRQ_MODIFIED:
446 			printf("IRQ modified\n");
447 			break;
448 
449 		case ASC_WARN_CMD_QNG_CONFLICT:
450 			printf("tag queuing enabled w/o disconnects\n");
451 			break;
452 
453 		default:
454 			printf("unknown warning %d\n", warn);
455 		}
456 	}
457 	if (sc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
458 		sc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
459 
460 	/*
461          * Modify the board configuration
462          */
463 	warn = AscInitFromASC_SOFTC(sc);
464 	if (warn) {
465 		printf("%s -set: ", sc->sc_dev.dv_xname);
466 		switch (warn) {
467 		case ASC_WARN_CMD_QNG_CONFLICT:
468 			printf("tag queuing enabled w/o disconnects\n");
469 			break;
470 
471 		case ASC_WARN_AUTO_CONFIG:
472 			printf("I/O port increment switch enabled\n");
473 			break;
474 
475 		default:
476 			printf("unknown warning %d\n", warn);
477 		}
478 	}
479 	sc->isr_callback = (ulong) adv_narrow_isr_callback;
480 
481 	if (!(sc->overrun_buf = adv_alloc_overrunbuf(sc->sc_dev.dv_xname,
482 						     sc->sc_dmat))) {
483 		return (1);
484 	}
485 
486 	return (0);
487 }
488 
489 
490 void
491 adv_attach(sc)
492 	ASC_SOFTC      *sc;
493 {
494 	struct scsibus_attach_args	saa;
495 	int				i, error;
496 
497 	/*
498          * Initialize board RISC chip and enable interrupts.
499          */
500 	switch (AscInitDriver(sc)) {
501 	case 0:
502 		/* AllOK */
503 		break;
504 
505 	case 1:
506 		panic("%s: bad signature", sc->sc_dev.dv_xname);
507 		break;
508 
509 	case 2:
510 		panic("%s: unable to load MicroCode",
511 		      sc->sc_dev.dv_xname);
512 		break;
513 
514 	case 3:
515 		panic("%s: unable to initialize MicroCode",
516 		      sc->sc_dev.dv_xname);
517 		break;
518 
519 	default:
520 		panic("%s: unable to initialize board RISC chip",
521 		      sc->sc_dev.dv_xname);
522 	}
523 
524 
525 	/*
526          * fill in the prototype scsi_link.
527          */
528 	sc->sc_link.adapter_softc = sc;
529 	sc->sc_link.adapter_target = sc->chip_scsi_id;
530 	sc->sc_link.adapter = &adv_switch;
531 	sc->sc_link.device = &adv_dev;
532 	sc->sc_link.openings = 4;
533 	sc->sc_link.adapter_buswidth = 7;
534 
535 
536 	TAILQ_INIT(&sc->sc_free_ccb);
537 	TAILQ_INIT(&sc->sc_waiting_ccb);
538 
539 
540 	/*
541          * Allocate the Control Blocks.
542          */
543 	error = adv_alloc_ccbs(sc);
544 	if (error)
545 		return; /* (error) */ ;
546 
547 	/*
548          * Create and initialize the Control Blocks.
549          */
550 	i = adv_create_ccbs(sc, sc->sc_control->ccbs, ADV_MAX_CCB);
551 	if (i == 0) {
552 		printf("%s: unable to create control blocks\n",
553 		       sc->sc_dev.dv_xname);
554 		return; /* (ENOMEM) */ ;
555 	} else if (i != ADV_MAX_CCB) {
556 		printf("%s: WARNING: only %d of %d control blocks created\n",
557 		       sc->sc_dev.dv_xname, i, ADV_MAX_CCB);
558 	}
559 
560 	bzero(&saa, sizeof(saa));
561 	saa.saa_sc_link = &sc->sc_link;
562 	config_found(&sc->sc_dev, &saa, scsiprint);
563 }
564 
565 
566 static void
567 advminphys(struct buf *bp, struct scsi_link *sl)
568 {
569 	if (bp->b_bcount > ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE))
570 		bp->b_bcount = ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE);
571 	minphys(bp);
572 }
573 
574 
575 /*
576  * start a scsi operation given the command and the data address.  Also needs
577  * the unit, target and lu.
578  */
579 static int
580 adv_scsi_cmd(xs)
581 	struct scsi_xfer *xs;
582 {
583 	struct scsi_link *sc_link = xs->sc_link;
584 	ASC_SOFTC      *sc = sc_link->adapter_softc;
585 	bus_dma_tag_t   dmat = sc->sc_dmat;
586 	ADV_CCB        *ccb;
587 	int             s, flags, error, nsegs;
588 
589 	s = splbio();		/* protect the queue */
590 
591 	/*
592          * get a ccb to use. If the transfer
593          * is from a buf (possibly from interrupt time)
594          * then we can't allow it to sleep
595          */
596 
597 	flags = xs->flags;
598 	if ((ccb = adv_get_ccb(sc, flags)) == NULL) {
599 		splx(s);
600 		return (NO_CCB);
601 	}
602 	splx(s);		/* done playing with the queue */
603 
604 	ccb->xs = xs;
605 	ccb->timeout = xs->timeout;
606 
607 	/*
608          * Build up the request
609          */
610 	memset(&ccb->scsiq, 0, sizeof(ASC_SCSI_Q));
611 
612 	ccb->scsiq.q2.ccb_ptr = (ulong) ccb;
613 
614 	ccb->scsiq.cdbptr = &xs->cmd->opcode;
615 	ccb->scsiq.q2.cdb_len = xs->cmdlen;
616 	ccb->scsiq.q1.target_id = ASC_TID_TO_TARGET_ID(sc_link->target);
617 	ccb->scsiq.q1.target_lun = sc_link->lun;
618 	ccb->scsiq.q2.target_ix = ASC_TIDLUN_TO_IX(sc_link->target,
619 						   sc_link->lun);
620 	ccb->scsiq.q1.sense_addr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
621 		ADV_CCB_OFF(ccb) + offsetof(struct adv_ccb, scsi_sense);
622 	ccb->scsiq.q1.sense_len = sizeof(struct scsi_sense_data);
623 
624 	/*
625          * If  there  are  any  outstanding  requests  for  the  current target,
626          * then  every  255th request  send an  ORDERED request.  This heuristic
627          * tries  to  retain  the  benefit  of request  sorting while preventing
628          * request starvation. 255 is the max number of tags or pending commands
629          * a device may have outstanding.
630          */
631 	sc->reqcnt[sc_link->target]++;
632 	if ((sc->reqcnt[sc_link->target] > 0) &&
633 	    (sc->reqcnt[sc_link->target] % 255) == 0) {
634 		ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED;
635 	} else {
636 		ccb->scsiq.q2.tag_code = M2_QTAG_MSG_SIMPLE;
637 	}
638 
639 
640 	if (xs->datalen) {
641 		/*
642                  * Map the DMA transfer.
643                  */
644 		error = bus_dmamap_load(dmat,
645 		      ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
646 					(flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
647 
648 		if (error) {
649 			if (error == EFBIG) {
650 				printf("%s: adv_scsi_cmd, more than %d dma"
651 				       " segments\n",
652 				       sc->sc_dev.dv_xname, ASC_MAX_SG_LIST);
653 			} else {
654 				printf("%s: adv_scsi_cmd, error %d loading"
655 				       " dma map\n",
656 				       sc->sc_dev.dv_xname, error);
657 			}
658 
659 			xs->error = XS_DRIVER_STUFFUP;
660 			adv_free_ccb(sc, ccb);
661 			return (COMPLETE);
662 		}
663 		bus_dmamap_sync(dmat, ccb->dmamap_xfer,
664 		    0, ccb->dmamap_xfer->dm_mapsize,
665 		    ((flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
666 			BUS_DMASYNC_PREWRITE));
667 
668 
669 		memset(&ccb->sghead, 0, sizeof(ASC_SG_HEAD));
670 
671 		for (nsegs = 0; nsegs < ccb->dmamap_xfer->dm_nsegs; nsegs++) {
672 
673 			ccb->sghead.sg_list[nsegs].addr =
674 				ccb->dmamap_xfer->dm_segs[nsegs].ds_addr;
675 			ccb->sghead.sg_list[nsegs].bytes =
676 				ccb->dmamap_xfer->dm_segs[nsegs].ds_len;
677 		}
678 
679 		ccb->sghead.entry_cnt = ccb->scsiq.q1.sg_queue_cnt =
680 			ccb->dmamap_xfer->dm_nsegs;
681 
682 		ccb->scsiq.q1.cntl |= ASC_QC_SG_HEAD;
683 		ccb->scsiq.sg_head = &ccb->sghead;
684 		ccb->scsiq.q1.data_addr = 0;
685 		ccb->scsiq.q1.data_cnt = 0;
686 	} else {
687 		/*
688                  * No data xfer, use non S/G values.
689                  */
690 		ccb->scsiq.q1.data_addr = 0;
691 		ccb->scsiq.q1.data_cnt = 0;
692 	}
693 
694 #ifdef ASC_DEBUG
695 	printf("id = %d, lun = %d, cmd = %d, ccb = 0x%lX \n",
696 			sc_link->scsipi_scsi.target,
697 			sc_link->scsipi_scsi.lun, xs->cmd->opcode,
698 			(unsigned long)ccb);
699 #endif
700 	/*
701          * Usually return SUCCESSFULLY QUEUED
702          */
703 	if ((flags & SCSI_POLL) == 0)
704 		return (SUCCESSFULLY_QUEUED);
705 
706 	/*
707          * If we can't use interrupts, poll on completion
708          */
709 	if (adv_poll(sc, xs, ccb->timeout)) {
710 		adv_timeout(ccb);
711 		if (adv_poll(sc, xs, ccb->timeout))
712 			adv_timeout(ccb);
713 	}
714 	return (COMPLETE);
715 }
716 
717 
718 int
719 adv_intr(arg)
720 	void           *arg;
721 {
722 	ASC_SOFTC      *sc = arg;
723 
724 #ifdef ASC_DEBUG
725 	int int_pend = FALSE;
726 
727 	if(ASC_IS_INT_PENDING(sc->sc_iot, sc->sc_ioh))
728 	{
729 		int_pend = TRUE;
730 		printf("ISR - ");
731 	}
732 #endif
733 	AscISR(sc);
734 #ifdef ASC_DEBUG
735 	if(int_pend)
736 		printf("\n");
737 #endif
738 
739 	return (1);
740 }
741 
742 
743 /*
744  * Poll a particular unit, looking for a particular xs
745  */
746 static int
747 adv_poll(sc, xs, count)
748 	ASC_SOFTC      *sc;
749 	struct scsi_xfer *xs;
750 	int             count;
751 {
752 	int s;
753 
754 	/* timeouts are in msec, so we loop in 1000 usec cycles */
755 	while (count) {
756 		s = splbio();
757 		adv_intr(sc);
758 		splx(s);
759 		if (xs->flags & ITSDONE)
760 			return (0);
761 		delay(1000);	/* only happens in boot so ok */
762 		count--;
763 	}
764 	return (1);
765 }
766 
767 
768 static void
769 adv_timeout(arg)
770 	void           *arg;
771 {
772 	ADV_CCB        *ccb = arg;
773 	struct scsi_xfer *xs = ccb->xs;
774 	struct scsi_link *sc_link = xs->sc_link;
775 	ASC_SOFTC      *sc = sc_link->adapter_softc;
776 	int             s;
777 
778 	sc_print_addr(sc_link);
779 	printf("timed out");
780 
781 	s = splbio();
782 
783 	/*
784          * If it has been through before, then a previous abort has failed,
785          * don't try abort again, reset the bus instead.
786          */
787 	if (ccb->flags & CCB_ABORT) {
788 		/* abort timed out */
789 		printf(" AGAIN. Resetting Bus\n");
790 		/* Lets try resetting the bus! */
791 		if (AscResetBus(sc) == ASC_ERROR) {
792 			ccb->timeout = sc->scsi_reset_wait;
793 			adv_queue_ccb(sc, ccb);
794 		}
795 	} else {
796 		/* abort the operation that has timed out */
797 		printf("\n");
798 		AscAbortCCB(sc, (u_int32_t) ccb);
799 		ccb->xs->error = XS_TIMEOUT;
800 		ccb->timeout = ADV_ABORT_TIMEOUT;
801 		ccb->flags |= CCB_ABORT;
802 		adv_queue_ccb(sc, ccb);
803 	}
804 
805 	splx(s);
806 }
807 
808 
809 static void
810 adv_watchdog(arg)
811 	void           *arg;
812 {
813 	ADV_CCB        *ccb = arg;
814 	struct scsi_xfer *xs = ccb->xs;
815 	struct scsi_link *sc_link = xs->sc_link;
816 	ASC_SOFTC      *sc = sc_link->adapter_softc;
817 	int             s;
818 
819 	s = splbio();
820 
821 	ccb->flags &= ~CCB_WATCHDOG;
822 	adv_start_ccbs(sc);
823 
824 	splx(s);
825 }
826 
827 
828 /******************************************************************************/
829 /*                  NARROW and WIDE boards Interrupt callbacks                */
830 /******************************************************************************/
831 
832 
833 /*
834  * adv_narrow_isr_callback() - Second Level Interrupt Handler called by AscISR()
835  *
836  * Interrupt callback function for the Narrow SCSI Asc Library.
837  */
838 static void
839 adv_narrow_isr_callback(sc, qdonep)
840 	ASC_SOFTC      *sc;
841 	ASC_QDONE_INFO *qdonep;
842 {
843 	bus_dma_tag_t   dmat = sc->sc_dmat;
844 	ADV_CCB        *ccb = (ADV_CCB *) qdonep->d2.ccb_ptr;
845 	struct scsi_xfer *xs = ccb->xs;
846 	struct scsi_sense_data *s1, *s2;
847 
848 
849 #ifdef ASC_DEBUG
850 	printf(" - ccb=0x%lx, id=%d, lun=%d, cmd=%d, ",
851 			(unsigned long)ccb,
852 			xs->sc_link->scsipi_scsi.target,
853 			xs->sc_link->scsipi_scsi.lun, xs->cmd->opcode);
854 #endif
855 	timeout_del(&xs->stimeout);
856 
857 	/*
858          * If we were a data transfer, unload the map that described
859          * the data buffer.
860          */
861 	if (xs->datalen) {
862 		bus_dmamap_sync(dmat, ccb->dmamap_xfer,
863 		    0, ccb->dmamap_xfer->dm_mapsize,
864 		    ((xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
865 			BUS_DMASYNC_POSTWRITE));
866 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
867 	}
868 	if ((ccb->flags & CCB_ALLOC) == 0) {
869 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
870 		Debugger();
871 		return;
872 	}
873 	/*
874          * 'qdonep' contains the command's ending status.
875          */
876 #ifdef ASC_DEBUG
877 	printf("d_s=%d, h_s=%d", qdonep->d3.done_stat, qdonep->d3.host_stat);
878 #endif
879 	switch (qdonep->d3.done_stat) {
880 	case ASC_QD_NO_ERROR:
881 		switch (qdonep->d3.host_stat) {
882 		case ASC_QHSTA_NO_ERROR:
883 			xs->error = XS_NOERROR;
884 			xs->resid = 0;
885 			break;
886 
887 		default:
888 			/* QHSTA error occurred */
889 			xs->error = XS_DRIVER_STUFFUP;
890 			break;
891 		}
892 
893 		/*
894                  * If an INQUIRY command completed successfully, then call
895                  * the AscInquiryHandling() function to patch bugged boards.
896                  */
897 		if ((xs->cmd->opcode == SCSICMD_Inquiry) &&
898 		    (xs->sc_link->lun == 0) &&
899 		    (xs->datalen - qdonep->remain_bytes) >= 8) {
900 			AscInquiryHandling(sc,
901 				      xs->sc_link->target & 0x7,
902 					   (ASC_SCSI_INQUIRY *) xs->data);
903 		}
904 		break;
905 
906 	case ASC_QD_WITH_ERROR:
907 		switch (qdonep->d3.host_stat) {
908 		case ASC_QHSTA_NO_ERROR:
909 			if (qdonep->d3.scsi_stat == SS_CHK_CONDITION) {
910 				s1 = &ccb->scsi_sense;
911 				s2 = &xs->sense;
912 				*s2 = *s1;
913 				xs->error = XS_SENSE;
914 			} else {
915 				xs->error = XS_DRIVER_STUFFUP;
916 			}
917 			break;
918 
919 		default:
920 			/* QHSTA error occurred */
921 			xs->error = XS_DRIVER_STUFFUP;
922 			break;
923 		}
924 		break;
925 
926 	case ASC_QD_ABORTED_BY_HOST:
927 	default:
928 		xs->error = XS_DRIVER_STUFFUP;
929 		break;
930 	}
931 
932 
933 	adv_free_ccb(sc, ccb);
934 	xs->flags |= ITSDONE;
935 	scsi_done(xs);
936 }
937