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