No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ic / adv.c
blobab7a9c326a67c6e4e4c477da2fe4e78de1246edf
1 /* $NetBSD: adv.c,v 1.43 2009/03/14 15:36:17 dsl Exp $ */
3 /*
4 * Generic driver for the Advanced Systems Inc. Narrow SCSI controllers
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * Author: Baldassare Dante Profeta <dante@mclink.it>
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.
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.
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: adv.c,v 1.43 2009/03/14 15:36:17 dsl Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
48 #include <sys/ioctl.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
51 #include <sys/buf.h>
52 #include <sys/proc.h>
54 #include <sys/bus.h>
55 #include <sys/intr.h>
57 #include <uvm/uvm_extern.h>
59 #include <dev/scsipi/scsi_all.h>
60 #include <dev/scsipi/scsipi_all.h>
61 #include <dev/scsipi/scsiconf.h>
63 #include <dev/ic/advlib.h>
64 #include <dev/ic/adv.h>
66 #ifndef DDB
67 #define Debugger() panic("should call debugger here (adv.c)")
68 #endif /* ! DDB */
71 /* #define ASC_DEBUG */
73 /******************************************************************************/
76 static int adv_alloc_control_data(ASC_SOFTC *);
77 static void adv_free_control_data(ASC_SOFTC *);
78 static int adv_create_ccbs(ASC_SOFTC *, ADV_CCB *, int);
79 static void adv_free_ccb(ASC_SOFTC *, ADV_CCB *);
80 static void adv_reset_ccb(ADV_CCB *);
81 static int adv_init_ccb(ASC_SOFTC *, ADV_CCB *);
82 static ADV_CCB *adv_get_ccb(ASC_SOFTC *);
83 static void adv_queue_ccb(ASC_SOFTC *, ADV_CCB *);
84 static void adv_start_ccbs(ASC_SOFTC *);
87 static void adv_scsipi_request(struct scsipi_channel *,
88 scsipi_adapter_req_t, void *);
89 static void advminphys(struct buf *);
90 static void adv_narrow_isr_callback(ASC_SOFTC *, ASC_QDONE_INFO *);
92 static int adv_poll(ASC_SOFTC *, struct scsipi_xfer *, int);
93 static void adv_timeout(void *);
94 static void adv_watchdog(void *);
97 /******************************************************************************/
99 #define ADV_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
100 #define ADV_WATCH_TIMEOUT 1000 /* time to wait for watchdog (mSec) */
102 /******************************************************************************/
103 /* Control Blocks routines */
104 /******************************************************************************/
107 static int
108 adv_alloc_control_data(ASC_SOFTC *sc)
110 int error;
113 * Allocate the control blocks.
115 if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adv_control),
116 PAGE_SIZE, 0, &sc->sc_control_seg, 1,
117 &sc->sc_control_nsegs, BUS_DMA_NOWAIT)) != 0) {
118 aprint_error_dev(&sc->sc_dev, "unable to allocate control structures,"
119 " error = %d\n", error);
120 return (error);
122 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_control_seg,
123 sc->sc_control_nsegs, sizeof(struct adv_control),
124 (void **) & sc->sc_control,
125 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
126 aprint_error_dev(&sc->sc_dev, "unable to map control structures, error = %d\n",
127 error);
128 return (error);
131 * Create and load the DMA map used for the control blocks.
133 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adv_control),
134 1, sizeof(struct adv_control), 0, BUS_DMA_NOWAIT,
135 &sc->sc_dmamap_control)) != 0) {
136 aprint_error_dev(&sc->sc_dev, "unable to create control DMA map, error = %d\n",
137 error);
138 return (error);
140 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
141 sc->sc_control, sizeof(struct adv_control), NULL,
142 BUS_DMA_NOWAIT)) != 0) {
143 aprint_error_dev(&sc->sc_dev, "unable to load control DMA map, error = %d\n",
144 error);
145 return (error);
149 * Initialize the overrun_buf address.
151 sc->overrun_buf = sc->sc_dmamap_control->dm_segs[0].ds_addr +
152 offsetof(struct adv_control, overrun_buf);
154 return (0);
157 static void
158 adv_free_control_data(ASC_SOFTC *sc)
161 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_control);
162 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_control);
163 sc->sc_dmamap_control = NULL;
165 bus_dmamem_unmap(sc->sc_dmat, (void *) sc->sc_control,
166 sizeof(struct adv_control));
167 bus_dmamem_free(sc->sc_dmat, &sc->sc_control_seg,
168 sc->sc_control_nsegs);
172 * Create a set of ccbs and add them to the free list. Called once
173 * by adv_init(). We return the number of CCBs successfully created.
175 static int
176 adv_create_ccbs(ASC_SOFTC *sc, ADV_CCB *ccbstore, int count)
178 ADV_CCB *ccb;
179 int i, error;
181 memset(ccbstore, 0, 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 aprint_error_dev(&sc->sc_dev, "unable to initialize ccb, error = %d\n",
186 error);
187 return (i);
189 TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
192 return (i);
197 * A ccb is put onto the free list.
199 static void
200 adv_free_ccb(ASC_SOFTC *sc, ADV_CCB *ccb)
202 int s;
204 s = splbio();
205 adv_reset_ccb(ccb);
206 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
207 splx(s);
211 static void
212 adv_reset_ccb(ADV_CCB *ccb)
215 ccb->flags = 0;
219 static int
220 adv_init_ccb(ASC_SOFTC *sc, ADV_CCB *ccb)
222 int hashnum, error;
224 callout_init(&ccb->ccb_watchdog, 0);
227 * Create the DMA map for this CCB.
229 error = bus_dmamap_create(sc->sc_dmat,
230 (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
231 ASC_MAX_SG_LIST, (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
232 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
233 if (error) {
234 aprint_error_dev(&sc->sc_dev, "unable to create DMA map, error = %d\n",
235 error);
236 return (error);
240 * put in the phystokv hash table
241 * Never gets taken out.
243 ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
244 ADV_CCB_OFF(ccb);
245 hashnum = CCB_HASH(ccb->hashkey);
246 ccb->nexthash = sc->sc_ccbhash[hashnum];
247 sc->sc_ccbhash[hashnum] = ccb;
249 adv_reset_ccb(ccb);
250 return (0);
255 * Get a free ccb
257 * If there are none, see if we can allocate a new one
259 static ADV_CCB *
260 adv_get_ccb(ASC_SOFTC *sc)
262 ADV_CCB *ccb = 0;
263 int s;
265 s = splbio();
266 ccb = TAILQ_FIRST(&sc->sc_free_ccb);
267 if (ccb != NULL) {
268 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
269 ccb->flags |= CCB_ALLOC;
271 splx(s);
272 return (ccb);
277 * Given a physical address, find the ccb that it corresponds to.
279 ADV_CCB *
280 adv_ccb_phys_kv(ASC_SOFTC *sc, u_long ccb_phys)
282 int hashnum = CCB_HASH(ccb_phys);
283 ADV_CCB *ccb = sc->sc_ccbhash[hashnum];
285 while (ccb) {
286 if (ccb->hashkey == ccb_phys)
287 break;
288 ccb = ccb->nexthash;
290 return (ccb);
295 * Queue a CCB to be sent to the controller, and send it if possible.
297 static void
298 adv_queue_ccb(ASC_SOFTC *sc, ADV_CCB *ccb)
301 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
303 adv_start_ccbs(sc);
307 static void
308 adv_start_ccbs(ASC_SOFTC *sc)
310 ADV_CCB *ccb;
312 while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
313 if (ccb->flags & CCB_WATCHDOG)
314 callout_stop(&ccb->ccb_watchdog);
316 if (AscExeScsiQueue(sc, &ccb->scsiq) == ASC_BUSY) {
317 ccb->flags |= CCB_WATCHDOG;
318 callout_reset(&ccb->ccb_watchdog,
319 (ADV_WATCH_TIMEOUT * hz) / 1000,
320 adv_watchdog, ccb);
321 break;
323 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
325 if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
326 callout_reset(&ccb->xs->xs_callout,
327 mstohz(ccb->timeout), adv_timeout, ccb);
332 /******************************************************************************/
333 /* SCSI layer interfacing routines */
334 /******************************************************************************/
338 adv_init(ASC_SOFTC *sc)
340 int warn;
342 if (!AscFindSignature(sc->sc_iot, sc->sc_ioh)) {
343 aprint_error("adv_init: failed to find signature\n");
344 return (1);
348 * Read the board configuration
350 AscInitASC_SOFTC(sc);
351 warn = AscInitFromEEP(sc);
352 if (warn) {
353 aprint_error_dev(&sc->sc_dev, "-get: ");
354 switch (warn) {
355 case -1:
356 aprint_normal("Chip is not halted\n");
357 break;
359 case -2:
360 aprint_normal("Couldn't get MicroCode Start"
361 " address\n");
362 break;
364 case ASC_WARN_IO_PORT_ROTATE:
365 aprint_normal("I/O port address modified\n");
366 break;
368 case ASC_WARN_AUTO_CONFIG:
369 aprint_normal("I/O port increment switch enabled\n");
370 break;
372 case ASC_WARN_EEPROM_CHKSUM:
373 aprint_normal("EEPROM checksum error\n");
374 break;
376 case ASC_WARN_IRQ_MODIFIED:
377 aprint_normal("IRQ modified\n");
378 break;
380 case ASC_WARN_CMD_QNG_CONFLICT:
381 aprint_normal("tag queuing enabled w/o disconnects\n");
382 break;
384 default:
385 aprint_normal("unknown warning %d\n", warn);
388 if (sc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
389 sc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
392 * Modify the board configuration
394 warn = AscInitFromASC_SOFTC(sc);
395 if (warn) {
396 aprint_error_dev(&sc->sc_dev, "-set: ");
397 switch (warn) {
398 case ASC_WARN_CMD_QNG_CONFLICT:
399 aprint_normal("tag queuing enabled w/o disconnects\n");
400 break;
402 case ASC_WARN_AUTO_CONFIG:
403 aprint_normal("I/O port increment switch enabled\n");
404 break;
406 default:
407 aprint_normal("unknown warning %d\n", warn);
410 sc->isr_callback = (ASC_CALLBACK) adv_narrow_isr_callback;
412 return (0);
416 void
417 adv_attach(ASC_SOFTC *sc)
419 struct scsipi_adapter *adapt = &sc->sc_adapter;
420 struct scsipi_channel *chan = &sc->sc_channel;
421 int i, error;
424 * Initialize board RISC chip and enable interrupts.
426 switch (AscInitDriver(sc)) {
427 case 0:
428 /* AllOK */
429 break;
431 case 1:
432 panic("%s: bad signature", device_xname(&sc->sc_dev));
433 break;
435 case 2:
436 panic("%s: unable to load MicroCode",
437 device_xname(&sc->sc_dev));
438 break;
440 case 3:
441 panic("%s: unable to initialize MicroCode",
442 device_xname(&sc->sc_dev));
443 break;
445 default:
446 panic("%s: unable to initialize board RISC chip",
447 device_xname(&sc->sc_dev));
451 * Fill in the scsipi_adapter.
453 memset(adapt, 0, sizeof(*adapt));
454 adapt->adapt_dev = &sc->sc_dev;
455 adapt->adapt_nchannels = 1;
456 /* adapt_openings initialized below */
457 /* adapt_max_periph initialized below */
458 adapt->adapt_request = adv_scsipi_request;
459 adapt->adapt_minphys = advminphys;
462 * Fill in the scsipi_channel.
464 memset(chan, 0, sizeof(*chan));
465 chan->chan_adapter = adapt;
466 chan->chan_bustype = &scsi_bustype;
467 chan->chan_channel = 0;
468 chan->chan_ntargets = 8;
469 chan->chan_nluns = 8;
470 chan->chan_id = sc->chip_scsi_id;
472 TAILQ_INIT(&sc->sc_free_ccb);
473 TAILQ_INIT(&sc->sc_waiting_ccb);
476 * Allocate the Control Blocks and the overrun buffer.
478 error = adv_alloc_control_data(sc);
479 if (error)
480 return; /* (error) */
483 * Create and initialize the Control Blocks.
485 i = adv_create_ccbs(sc, sc->sc_control->ccbs, ADV_MAX_CCB);
486 if (i == 0) {
487 aprint_error_dev(&sc->sc_dev, "unable to create control blocks\n");
488 return; /* (ENOMEM) */ ;
489 } else if (i != ADV_MAX_CCB) {
490 aprint_error_dev(&sc->sc_dev,
491 "WARNING: only %d of %d control blocks created\n",
492 i, ADV_MAX_CCB);
495 adapt->adapt_openings = i;
496 adapt->adapt_max_periph = adapt->adapt_openings;
498 sc->sc_child = config_found(&sc->sc_dev, chan, scsiprint);
502 adv_detach(ASC_SOFTC *sc, int flags)
504 int rv = 0;
506 if (sc->sc_child != NULL)
507 rv = config_detach(sc->sc_child, flags);
509 adv_free_control_data(sc);
511 return (rv);
514 static void
515 advminphys(struct buf *bp)
518 if (bp->b_bcount > ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE))
519 bp->b_bcount = ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE);
520 minphys(bp);
525 * start a scsi operation given the command and the data address. Also needs
526 * the unit, target and lu.
529 static void
530 adv_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
532 struct scsipi_xfer *xs;
533 struct scsipi_periph *periph;
534 ASC_SOFTC *sc = (void *)chan->chan_adapter->adapt_dev;
535 bus_dma_tag_t dmat = sc->sc_dmat;
536 ADV_CCB *ccb;
537 int s, flags, error, nsegs;
539 switch (req) {
540 case ADAPTER_REQ_RUN_XFER:
541 xs = arg;
542 periph = xs->xs_periph;
543 flags = xs->xs_control;
546 * Get a CCB to use.
548 ccb = adv_get_ccb(sc);
549 #ifdef DIAGNOSTIC
551 * This should never happen as we track the resources
552 * in the mid-layer.
554 if (ccb == NULL) {
555 scsipi_printaddr(periph);
556 printf("unable to allocate ccb\n");
557 panic("adv_scsipi_request");
559 #endif
561 ccb->xs = xs;
562 ccb->timeout = xs->timeout;
565 * Build up the request
567 memset(&ccb->scsiq, 0, sizeof(ASC_SCSI_Q));
569 ccb->scsiq.q2.ccb_ptr =
570 sc->sc_dmamap_control->dm_segs[0].ds_addr +
571 ADV_CCB_OFF(ccb);
573 ccb->scsiq.cdbptr = &xs->cmd->opcode;
574 ccb->scsiq.q2.cdb_len = xs->cmdlen;
575 ccb->scsiq.q1.target_id =
576 ASC_TID_TO_TARGET_ID(periph->periph_target);
577 ccb->scsiq.q1.target_lun = periph->periph_lun;
578 ccb->scsiq.q2.target_ix =
579 ASC_TIDLUN_TO_IX(periph->periph_target,
580 periph->periph_lun);
581 ccb->scsiq.q1.sense_addr =
582 sc->sc_dmamap_control->dm_segs[0].ds_addr +
583 ADV_CCB_OFF(ccb) + offsetof(struct adv_ccb, scsi_sense);
584 ccb->scsiq.q1.sense_len = sizeof(struct scsi_sense_data);
587 * If there are any outstanding requests for the current
588 * target, then every 255th request send an ORDERED request.
589 * This heuristic tries to retain the benefit of request
590 * sorting while preventing request starvation. 255 is the
591 * max number of tags or pending commands a device may have
592 * outstanding.
594 sc->reqcnt[periph->periph_target]++;
595 if (((sc->reqcnt[periph->periph_target] > 0) &&
596 (sc->reqcnt[periph->periph_target] % 255) == 0) ||
597 xs->bp == NULL || (xs->bp->b_flags & B_ASYNC) == 0) {
598 ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED;
599 } else {
600 ccb->scsiq.q2.tag_code = M2_QTAG_MSG_SIMPLE;
603 if (xs->datalen) {
605 * Map the DMA transfer.
607 #ifdef TFS
608 if (flags & SCSI_DATA_UIO) {
609 error = bus_dmamap_load_uio(dmat,
610 ccb->dmamap_xfer, (struct uio *) xs->data,
611 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
612 BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
613 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
614 BUS_DMA_WRITE));
615 } else
616 #endif /* TFS */
618 error = bus_dmamap_load(dmat, ccb->dmamap_xfer,
619 xs->data, xs->datalen, NULL,
620 ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
621 BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
622 ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
623 BUS_DMA_WRITE));
626 switch (error) {
627 case 0:
628 break;
631 case ENOMEM:
632 case EAGAIN:
633 xs->error = XS_RESOURCE_SHORTAGE;
634 goto out_bad;
636 default:
637 xs->error = XS_DRIVER_STUFFUP;
638 if (error == EFBIG) {
639 aprint_error_dev(&sc->sc_dev, "adv_scsi_cmd, more than %d"
640 " DMA segments\n",
641 ASC_MAX_SG_LIST);
642 } else {
643 aprint_error_dev(&sc->sc_dev, "adv_scsi_cmd, error %d"
644 " loading DMA map\n",
645 error);
648 out_bad:
649 adv_free_ccb(sc, ccb);
650 scsipi_done(xs);
651 return;
653 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
654 ccb->dmamap_xfer->dm_mapsize,
655 (flags & XS_CTL_DATA_IN) ?
656 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
658 memset(&ccb->sghead, 0, sizeof(ASC_SG_HEAD));
660 for (nsegs = 0;
661 nsegs < ccb->dmamap_xfer->dm_nsegs; nsegs++) {
662 ccb->sghead.sg_list[nsegs].addr =
663 ccb->dmamap_xfer->dm_segs[nsegs].ds_addr;
664 ccb->sghead.sg_list[nsegs].bytes =
665 ccb->dmamap_xfer->dm_segs[nsegs].ds_len;
668 ccb->sghead.entry_cnt = ccb->scsiq.q1.sg_queue_cnt =
669 ccb->dmamap_xfer->dm_nsegs;
671 ccb->scsiq.q1.cntl |= ASC_QC_SG_HEAD;
672 ccb->scsiq.sg_head = &ccb->sghead;
673 ccb->scsiq.q1.data_addr = 0;
674 ccb->scsiq.q1.data_cnt = 0;
675 } else {
677 * No data xfer, use non S/G values.
679 ccb->scsiq.q1.data_addr = 0;
680 ccb->scsiq.q1.data_cnt = 0;
683 #ifdef ASC_DEBUG
684 printf("id = %d, lun = %d, cmd = %d, ccb = 0x%lX\n",
685 periph->periph_target,
686 periph->periph_lun, xs->cmd->opcode,
687 (unsigned long)ccb);
688 #endif
689 s = splbio();
690 adv_queue_ccb(sc, ccb);
691 splx(s);
693 if ((flags & XS_CTL_POLL) == 0)
694 return;
696 /* Not allowed to use interrupts, poll for completion. */
697 if (adv_poll(sc, xs, ccb->timeout)) {
698 adv_timeout(ccb);
699 if (adv_poll(sc, xs, ccb->timeout))
700 adv_timeout(ccb);
702 return;
704 case ADAPTER_REQ_GROW_RESOURCES:
705 /* XXX Not supported. */
706 return;
708 case ADAPTER_REQ_SET_XFER_MODE:
711 * We can't really set the mode, but we know how to
712 * query what the firmware negotiated.
714 struct scsipi_xfer_mode *xm = arg;
715 u_int8_t sdtr_data;
716 ASC_SCSI_BIT_ID_TYPE tid_bit;
718 tid_bit = ASC_TIX_TO_TARGET_ID(xm->xm_target);
720 xm->xm_mode = 0;
721 xm->xm_period = 0;
722 xm->xm_offset = 0;
724 if (sc->init_sdtr & tid_bit) {
725 xm->xm_mode |= PERIPH_CAP_SYNC;
726 sdtr_data = sc->sdtr_data[xm->xm_target];
727 xm->xm_period =
728 sc->sdtr_period_tbl[(sdtr_data >> 4) &
729 (sc->max_sdtr_index - 1)];
730 xm->xm_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
733 if (sc->use_tagged_qng & tid_bit)
734 xm->xm_mode |= PERIPH_CAP_TQING;
736 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
737 return;
743 adv_intr(void *arg)
745 ASC_SOFTC *sc = arg;
747 #ifdef ASC_DEBUG
748 int int_pend = FALSE;
750 if(ASC_IS_INT_PENDING(sc->sc_iot, sc->sc_ioh))
752 int_pend = TRUE;
753 printf("ISR - ");
755 #endif
756 AscISR(sc);
757 #ifdef ASC_DEBUG
758 if(int_pend)
759 printf("\n");
760 #endif
762 return (1);
767 * Poll a particular unit, looking for a particular xs
769 static int
770 adv_poll(ASC_SOFTC *sc, struct scsipi_xfer *xs, int count)
773 /* timeouts are in msec, so we loop in 1000 usec cycles */
774 while (count) {
775 adv_intr(sc);
776 if (xs->xs_status & XS_STS_DONE)
777 return (0);
778 delay(1000); /* only happens in boot so ok */
779 count--;
781 return (1);
785 static void
786 adv_timeout(void *arg)
788 ADV_CCB *ccb = arg;
789 struct scsipi_xfer *xs = ccb->xs;
790 struct scsipi_periph *periph = xs->xs_periph;
791 ASC_SOFTC *sc =
792 (void *)periph->periph_channel->chan_adapter->adapt_dev;
793 int s;
795 scsipi_printaddr(periph);
796 printf("timed out");
798 s = splbio();
801 * If it has been through before, then a previous abort has failed,
802 * don't try abort again, reset the bus instead.
804 if (ccb->flags & CCB_ABORT) {
805 /* abort timed out */
806 printf(" AGAIN. Resetting Bus\n");
807 /* Lets try resetting the bus! */
808 if (AscResetBus(sc) == ASC_ERROR) {
809 ccb->timeout = sc->scsi_reset_wait;
810 adv_queue_ccb(sc, ccb);
812 } else {
813 /* abort the operation that has timed out */
814 printf("\n");
815 AscAbortCCB(sc, ccb);
816 ccb->xs->error = XS_TIMEOUT;
817 ccb->timeout = ADV_ABORT_TIMEOUT;
818 ccb->flags |= CCB_ABORT;
819 adv_queue_ccb(sc, ccb);
822 splx(s);
826 static void
827 adv_watchdog(void *arg)
829 ADV_CCB *ccb = arg;
830 struct scsipi_xfer *xs = ccb->xs;
831 struct scsipi_periph *periph = xs->xs_periph;
832 ASC_SOFTC *sc =
833 (void *)periph->periph_channel->chan_adapter->adapt_dev;
834 int s;
836 s = splbio();
838 ccb->flags &= ~CCB_WATCHDOG;
839 adv_start_ccbs(sc);
841 splx(s);
845 /******************************************************************************/
846 /* NARROW boards Interrupt callbacks */
847 /******************************************************************************/
851 * adv_narrow_isr_callback() - Second Level Interrupt Handler called by AscISR()
853 * Interrupt callback function for the Narrow SCSI Asc Library.
855 static void
856 adv_narrow_isr_callback(ASC_SOFTC *sc, ASC_QDONE_INFO *qdonep)
858 bus_dma_tag_t dmat = sc->sc_dmat;
859 ADV_CCB *ccb;
860 struct scsipi_xfer *xs;
861 struct scsi_sense_data *s1, *s2;
864 ccb = adv_ccb_phys_kv(sc, qdonep->d2.ccb_ptr);
865 xs = ccb->xs;
867 #ifdef ASC_DEBUG
868 printf(" - ccb=0x%lx, id=%d, lun=%d, cmd=%d, ",
869 (unsigned long)ccb,
870 xs->xs_periph->periph_target,
871 xs->xs_periph->periph_lun, xs->cmd->opcode);
872 #endif
873 callout_stop(&ccb->xs->xs_callout);
876 * If we were a data transfer, unload the map that described
877 * the data buffer.
879 if (xs->datalen) {
880 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
881 ccb->dmamap_xfer->dm_mapsize,
882 (xs->xs_control & XS_CTL_DATA_IN) ?
883 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
884 bus_dmamap_unload(dmat, ccb->dmamap_xfer);
886 if ((ccb->flags & CCB_ALLOC) == 0) {
887 aprint_error_dev(&sc->sc_dev, "exiting ccb not allocated!\n");
888 Debugger();
889 return;
892 * 'qdonep' contains the command's ending status.
894 #ifdef ASC_DEBUG
895 printf("d_s=%d, h_s=%d", qdonep->d3.done_stat, qdonep->d3.host_stat);
896 #endif
897 switch (qdonep->d3.done_stat) {
898 case ASC_QD_NO_ERROR:
899 switch (qdonep->d3.host_stat) {
900 case ASC_QHSTA_NO_ERROR:
901 xs->error = XS_NOERROR;
902 xs->resid = 0;
903 break;
905 default:
906 /* QHSTA error occurred */
907 xs->error = XS_DRIVER_STUFFUP;
908 break;
912 * If an INQUIRY command completed successfully, then call
913 * the AscInquiryHandling() function to patch bugged boards.
915 if ((xs->cmd->opcode == SCSICMD_Inquiry) &&
916 (xs->xs_periph->periph_lun == 0) &&
917 (xs->datalen - qdonep->remain_bytes) >= 8) {
918 AscInquiryHandling(sc,
919 xs->xs_periph->periph_target & 0x7,
920 (ASC_SCSI_INQUIRY *) xs->data);
922 break;
924 case ASC_QD_WITH_ERROR:
925 switch (qdonep->d3.host_stat) {
926 case ASC_QHSTA_NO_ERROR:
927 if (qdonep->d3.scsi_stat == SS_CHK_CONDITION) {
928 s1 = &ccb->scsi_sense;
929 s2 = &xs->sense.scsi_sense;
930 *s2 = *s1;
931 xs->error = XS_SENSE;
932 } else {
933 xs->error = XS_DRIVER_STUFFUP;
935 break;
937 case ASC_QHSTA_M_SEL_TIMEOUT:
938 xs->error = XS_SELTIMEOUT;
939 break;
941 default:
942 /* QHSTA error occurred */
943 xs->error = XS_DRIVER_STUFFUP;
944 break;
946 break;
948 case ASC_QD_ABORTED_BY_HOST:
949 default:
950 xs->error = XS_DRIVER_STUFFUP;
951 break;
955 adv_free_ccb(sc, ccb);
956 scsipi_done(xs);