adv.c revision 1.19 1 /* $NetBSD: adv.c,v 1.19 2000/08/11 21:31:19 tls 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 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 if((xs->bp != NULL) && xs->bp->b_flags & B_ASYNC)
665 ccb->scsiq.q2.tag_code = M2_QTAG_MSG_SIMPLE;
666 else
667 ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED;
668 }
669
670
671 if (xs->datalen) {
672 /*
673 * Map the DMA transfer.
674 */
675 #ifdef TFS
676 if (flags & SCSI_DATA_UIO) {
677 error = bus_dmamap_load_uio(dmat,
678 ccb->dmamap_xfer, (struct uio *) xs->data,
679 (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
680 } else
681 #endif /* TFS */
682 {
683 error = bus_dmamap_load(dmat,
684 ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
685 (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
686 }
687
688 if (error) {
689 if (error == EFBIG) {
690 printf("%s: adv_scsi_cmd, more than %d dma"
691 " segments\n",
692 sc->sc_dev.dv_xname, ASC_MAX_SG_LIST);
693 } else {
694 printf("%s: adv_scsi_cmd, error %d loading"
695 " dma map\n",
696 sc->sc_dev.dv_xname, error);
697 }
698
699 xs->error = XS_DRIVER_STUFFUP;
700 adv_free_ccb(sc, ccb);
701 return (COMPLETE);
702 }
703 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
704 ccb->dmamap_xfer->dm_mapsize,
705 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
706 BUS_DMASYNC_PREWRITE);
707
708
709 memset(&ccb->sghead, 0, sizeof(ASC_SG_HEAD));
710
711 for (nsegs = 0; nsegs < ccb->dmamap_xfer->dm_nsegs; nsegs++) {
712
713 ccb->sghead.sg_list[nsegs].addr =
714 ccb->dmamap_xfer->dm_segs[nsegs].ds_addr;
715 ccb->sghead.sg_list[nsegs].bytes =
716 ccb->dmamap_xfer->dm_segs[nsegs].ds_len;
717 }
718
719 ccb->sghead.entry_cnt = ccb->scsiq.q1.sg_queue_cnt =
720 ccb->dmamap_xfer->dm_nsegs;
721
722 ccb->scsiq.q1.cntl |= ASC_QC_SG_HEAD;
723 ccb->scsiq.sg_head = &ccb->sghead;
724 ccb->scsiq.q1.data_addr = 0;
725 ccb->scsiq.q1.data_cnt = 0;
726 } else {
727 /*
728 * No data xfer, use non S/G values.
729 */
730 ccb->scsiq.q1.data_addr = 0;
731 ccb->scsiq.q1.data_cnt = 0;
732 }
733
734 #ifdef ASC_DEBUG
735 printf("id = %d, lun = %d, cmd = %d, ccb = 0x%lX \n",
736 sc_link->scsipi_scsi.target,
737 sc_link->scsipi_scsi.lun, xs->cmd->opcode,
738 (unsigned long)ccb);
739 #endif
740 s = splbio();
741 adv_queue_ccb(sc, ccb);
742 splx(s);
743
744 /*
745 * Usually return SUCCESSFULLY QUEUED
746 */
747 if ((flags & XS_CTL_POLL) == 0)
748 return (SUCCESSFULLY_QUEUED);
749
750 /*
751 * If we can't use interrupts, poll on completion
752 */
753 if (adv_poll(sc, xs, ccb->timeout)) {
754 adv_timeout(ccb);
755 if (adv_poll(sc, xs, ccb->timeout))
756 adv_timeout(ccb);
757 }
758 return (COMPLETE);
759 }
760
761
762 int
763 adv_intr(arg)
764 void *arg;
765 {
766 ASC_SOFTC *sc = arg;
767 struct scsipi_xfer *xs;
768
769 #ifdef ASC_DEBUG
770 int int_pend = FALSE;
771
772 if(ASC_IS_INT_PENDING(sc->sc_iot, sc->sc_ioh))
773 {
774 int_pend = TRUE;
775 printf("ISR - ");
776 }
777 #endif
778 AscISR(sc);
779 #ifdef ASC_DEBUG
780 if(int_pend)
781 printf("\n");
782 #endif
783
784 /*
785 * If there are queue entries in the software queue, try to
786 * run the first one. We should be more or less guaranteed
787 * to succeed, since we just freed a CCB.
788 *
789 * NOTE: adv_scsi_cmd() relies on our calling it with
790 * the first entry in the queue.
791 */
792 if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
793 (void) adv_scsi_cmd(xs);
794
795 return (1);
796 }
797
798
799 /*
800 * Poll a particular unit, looking for a particular xs
801 */
802 static int
803 adv_poll(sc, xs, count)
804 ASC_SOFTC *sc;
805 struct scsipi_xfer *xs;
806 int count;
807 {
808
809 /* timeouts are in msec, so we loop in 1000 usec cycles */
810 while (count) {
811 adv_intr(sc);
812 if (xs->xs_status & XS_STS_DONE)
813 return (0);
814 delay(1000); /* only happens in boot so ok */
815 count--;
816 }
817 return (1);
818 }
819
820
821 static void
822 adv_timeout(arg)
823 void *arg;
824 {
825 ADV_CCB *ccb = arg;
826 struct scsipi_xfer *xs = ccb->xs;
827 struct scsipi_link *sc_link = xs->sc_link;
828 ASC_SOFTC *sc = sc_link->adapter_softc;
829 int s;
830
831 scsi_print_addr(sc_link);
832 printf("timed out");
833
834 s = splbio();
835
836 /*
837 * If it has been through before, then a previous abort has failed,
838 * don't try abort again, reset the bus instead.
839 */
840 if (ccb->flags & CCB_ABORT) {
841 /* abort timed out */
842 printf(" AGAIN. Resetting Bus\n");
843 /* Lets try resetting the bus! */
844 if (AscResetBus(sc) == ASC_ERROR) {
845 ccb->timeout = sc->scsi_reset_wait;
846 adv_queue_ccb(sc, ccb);
847 }
848 } else {
849 /* abort the operation that has timed out */
850 printf("\n");
851 AscAbortCCB(sc, ccb);
852 ccb->xs->error = XS_TIMEOUT;
853 ccb->timeout = ADV_ABORT_TIMEOUT;
854 ccb->flags |= CCB_ABORT;
855 adv_queue_ccb(sc, ccb);
856 }
857
858 splx(s);
859 }
860
861
862 static void
863 adv_watchdog(arg)
864 void *arg;
865 {
866 ADV_CCB *ccb = arg;
867 struct scsipi_xfer *xs = ccb->xs;
868 struct scsipi_link *sc_link = xs->sc_link;
869 ASC_SOFTC *sc = sc_link->adapter_softc;
870 int s;
871
872 s = splbio();
873
874 ccb->flags &= ~CCB_WATCHDOG;
875 adv_start_ccbs(sc);
876
877 splx(s);
878 }
879
880
881 /******************************************************************************/
882 /* NARROW boards Interrupt callbacks */
883 /******************************************************************************/
884
885
886 /*
887 * adv_narrow_isr_callback() - Second Level Interrupt Handler called by AscISR()
888 *
889 * Interrupt callback function for the Narrow SCSI Asc Library.
890 */
891 static void
892 adv_narrow_isr_callback(sc, qdonep)
893 ASC_SOFTC *sc;
894 ASC_QDONE_INFO *qdonep;
895 {
896 bus_dma_tag_t dmat = sc->sc_dmat;
897 ADV_CCB *ccb;
898 struct scsipi_xfer *xs;
899 struct scsipi_sense_data *s1, *s2;
900
901
902 ccb = adv_ccb_phys_kv(sc, qdonep->d2.ccb_ptr);
903 xs = ccb->xs;
904
905 #ifdef ASC_DEBUG
906 printf(" - ccb=0x%lx, id=%d, lun=%d, cmd=%d, ",
907 (unsigned long)ccb,
908 xs->sc_link->scsipi_scsi.target,
909 xs->sc_link->scsipi_scsi.lun, xs->cmd->opcode);
910 #endif
911 callout_stop(&ccb->xs->xs_callout);
912
913 /*
914 * If we were a data transfer, unload the map that described
915 * the data buffer.
916 */
917 if (xs->datalen) {
918 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
919 ccb->dmamap_xfer->dm_mapsize,
920 (xs->xs_control & XS_CTL_DATA_IN) ?
921 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
922 bus_dmamap_unload(dmat, ccb->dmamap_xfer);
923 }
924 if ((ccb->flags & CCB_ALLOC) == 0) {
925 printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
926 Debugger();
927 return;
928 }
929 /*
930 * 'qdonep' contains the command's ending status.
931 */
932 #ifdef ASC_DEBUG
933 printf("d_s=%d, h_s=%d", qdonep->d3.done_stat, qdonep->d3.host_stat);
934 #endif
935 switch (qdonep->d3.done_stat) {
936 case ASC_QD_NO_ERROR:
937 switch (qdonep->d3.host_stat) {
938 case ASC_QHSTA_NO_ERROR:
939 xs->error = XS_NOERROR;
940 xs->resid = 0;
941 break;
942
943 default:
944 /* QHSTA error occurred */
945 xs->error = XS_DRIVER_STUFFUP;
946 break;
947 }
948
949 /*
950 * If an INQUIRY command completed successfully, then call
951 * the AscInquiryHandling() function to patch bugged boards.
952 */
953 if ((xs->cmd->opcode == SCSICMD_Inquiry) &&
954 (xs->sc_link->scsipi_scsi.lun == 0) &&
955 (xs->datalen - qdonep->remain_bytes) >= 8) {
956 AscInquiryHandling(sc,
957 xs->sc_link->scsipi_scsi.target & 0x7,
958 (ASC_SCSI_INQUIRY *) xs->data);
959 }
960 break;
961
962 case ASC_QD_WITH_ERROR:
963 switch (qdonep->d3.host_stat) {
964 case ASC_QHSTA_NO_ERROR:
965 if (qdonep->d3.scsi_stat == SS_CHK_CONDITION) {
966 s1 = &ccb->scsi_sense;
967 s2 = &xs->sense.scsi_sense;
968 *s2 = *s1;
969 xs->error = XS_SENSE;
970 } else {
971 xs->error = XS_DRIVER_STUFFUP;
972 }
973 break;
974
975 default:
976 /* QHSTA error occurred */
977 xs->error = XS_DRIVER_STUFFUP;
978 break;
979 }
980 break;
981
982 case ASC_QD_ABORTED_BY_HOST:
983 default:
984 xs->error = XS_DRIVER_STUFFUP;
985 break;
986 }
987
988
989 adv_free_ccb(sc, ccb);
990 xs->xs_status |= XS_STS_DONE;
991 scsipi_done(xs);
992 }
993