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