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