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