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