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