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