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