1/*-
2 * Copyright (c) 2004 Scott Long
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
--- 9 unchanged lines hidden (view full) ---
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28/* $NetBSD: ncr53c9x.c,v 1.114 2005/02/27 00:27:02 perry Exp $ */
29
30/*-
31 * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
32 * All rights reserved.
33 *
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Charles M. Hannum.
36 *
--- 62 unchanged lines hidden (view full) ---
99 * Based on aic6360 by Jarle Greipsland
100 *
101 * Acknowledgements: Many of the algorithms used in this driver are
102 * inspired by the work of Julian Elischer (julian@FreeBSD.org) and
103 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million!
104 */
105
106#include <sys/cdefs.h>
107__FBSDID("$FreeBSD: head/sys/dev/esp/ncr53c9x.c 180692 2008-07-22 13:51:21Z marius $");
108
109#include <sys/param.h>
110#include <sys/systm.h>
111#include <sys/bus.h>
112#include <sys/kernel.h>
113#include <sys/malloc.h>
114#include <sys/resource.h>
115#include <sys/lock.h>
116#include <sys/mutex.h>
117#include <sys/queue.h>
118#include <sys/time.h>
119#include <sys/callout.h>
120
121#include <cam/cam.h>
122#include <cam/cam_ccb.h>
123#include <cam/cam_debug.h>
124#include <cam/cam_sim.h>
125#include <cam/cam_xpt_sim.h>
126#include <cam/scsi/scsi_all.h>
127#include <cam/scsi/scsi_message.h>
128
129#include <dev/esp/ncr53c9xreg.h>
130#include <dev/esp/ncr53c9xvar.h>
131
132int ncr53c9x_debug =
133 NCR_SHOWMISC /* | NCR_SHOWPHASE | NCR_SHOWTRAC | NCR_SHOWCMDS */;
134#ifdef DEBUG
135int ncr53c9x_notag = 0;
136#endif
137
138static void ncr53c9x_abort(struct ncr53c9x_softc *sc,
139 struct ncr53c9x_ecb *ecb);
140static void ncr53c9x_dequeue(struct ncr53c9x_softc *sc,
141 struct ncr53c9x_ecb *ecb);
142static void ncr53c9x_done(struct ncr53c9x_softc *sc,
143 struct ncr53c9x_ecb *ecb);
144static void ncr53c9x_free_ecb(struct ncr53c9x_softc *sc,
145 struct ncr53c9x_ecb *ecb);
146static void ncr53c9x_msgin(struct ncr53c9x_softc *sc);
147static void ncr53c9x_msgout(struct ncr53c9x_softc *sc);
148static void ncr53c9x_poll(struct cam_sim *sim);
149static int ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how);
150static int ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message,
151 int tagtype, int tagid);
152static void ncr53c9x_sense(struct ncr53c9x_softc *sc,
153 struct ncr53c9x_ecb *ecb);
154static void ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc);
155static void ncr53c9x_sched(struct ncr53c9x_softc *sc);
156static void ncr53c9x_select(struct ncr53c9x_softc *sc,
157 struct ncr53c9x_ecb *ecb);
158static void ncr53c9x_timeout(void *arg);
159static void ncr53c9x_watch(void *arg);
160static void ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p,
161 int len);
162
163static struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *sc);
164static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *sc,
165 int64_t lun);
166
--- 66 unchanged lines hidden (view full) ---
233ncr53c9x_attach(struct ncr53c9x_softc *sc)
234{
235 struct cam_devq *devq;
236 struct cam_sim *sim;
237 struct cam_path *path;
238 struct ncr53c9x_ecb *ecb;
239 int error, i;
240
241 mtx_init(&sc->sc_lock, "ncr", "ncr53c9x lock", MTX_DEF);
242
243 /*
244 * Note, the front-end has set us up to print the chip variation.
245 */
246 if (sc->sc_rev >= NCR_VARIANT_MAX) {
247 device_printf(sc->sc_dev, "unknown variant %d, devices not "
248 "attached\n", sc->sc_rev);
249 return (EINVAL);
250 }
--- 36 unchanged lines hidden (view full) ---
287 M_DEVBUF, M_NOWAIT | M_ZERO);
288 if (sc->sc_tinfo == NULL) {
289 device_printf(sc->sc_dev,
290 "cannot allocate target info buffer\n");
291 error = ENOMEM;
292 goto fail_imess;
293 }
294
295 callout_init(&sc->sc_watchdog, 0);
296
297 /*
298 * Treat NCR53C90 with the 86C01 DMA chip exactly as ESP100
299 * from now on.
300 */
301 if (sc->sc_rev == NCR_VARIANT_NCR53C90_86C01)
302 sc->sc_rev = NCR_VARIANT_ESP100;
303
304 sc->sc_ccf = FREQTOCCF(sc->sc_freq);
--- 24 unchanged lines hidden (view full) ---
329 devq = cam_simq_alloc(sc->sc_ntarg);
330 if (devq == NULL) {
331 device_printf(sc->sc_dev, "cannot allocate device queue\n");
332 error = ENOMEM;
333 goto fail_tinfo;
334 }
335
336 sim = cam_sim_alloc(ncr53c9x_action, ncr53c9x_poll, "esp", sc,
337 device_get_unit(sc->sc_dev), &Giant, 1, NCR_TAG_DEPTH, devq);
338 if (sim == NULL) {
339 device_printf(sc->sc_dev, "cannot allocate SIM entry\n");
340 error = ENOMEM;
341 goto fail_devq;
342 }
343 if (xpt_bus_register(sim, sc->sc_dev, 0) != CAM_SUCCESS) {
344 device_printf(sc->sc_dev, "cannot register bus\n");
345 error = EIO;
346 goto fail_sim;
347 }
348
349 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
350 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
351 device_printf(sc->sc_dev, "cannot create path\n");
352 error = EIO;
353 goto fail_bus;
354 }
355
356 sc->sc_sim = sim;
357 sc->sc_path = path;
358
359 /* Reset state and bus. */
360#if 0
361 sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
362#else
363 sc->sc_cfflags = 0;
364#endif
365 sc->sc_state = 0;
366 ncr53c9x_init(sc, 1);
367
368 TAILQ_INIT(&sc->free_list);
369 if ((sc->ecb_array =
370 malloc(sizeof(struct ncr53c9x_ecb) * NCR_TAG_DEPTH, M_DEVBUF,
371 M_NOWAIT | M_ZERO)) == NULL) {
372 device_printf(sc->sc_dev, "cannot allocate ECB array\n");
373 error = ENOMEM;
374 goto fail_path;
375 }
376 for (i = 0; i < NCR_TAG_DEPTH; i++) {
377 ecb = &sc->ecb_array[i];
378 ecb->sc = sc;
379 ecb->tag_id = i;
380 TAILQ_INSERT_HEAD(&sc->free_list, ecb, free_links);
381 }
382
383 callout_reset(&sc->sc_watchdog, 60*hz, ncr53c9x_watch, sc);
384
385 return (0);
386
387fail_path:
388 xpt_free_path(path);
389fail_bus:
390 xpt_bus_deregister(cam_sim_path(sim));
391fail_sim:
392 cam_sim_free(sim, TRUE);
393fail_devq:
394 cam_simq_free(devq);
395fail_tinfo:
396 free(sc->sc_tinfo, M_DEVBUF);
397fail_imess:
398 if (sc->sc_imess_self)
399 free(sc->sc_imess, M_DEVBUF);
400fail_omess:
401 if (sc->sc_omess_self)
402 free(sc->sc_omess, M_DEVBUF);
403 return (error);
404}
405
406int
407ncr53c9x_detach(struct ncr53c9x_softc *sc)
408{
409
410 callout_drain(&sc->sc_watchdog);
411 mtx_lock(&sc->sc_lock);
412 ncr53c9x_init(sc, 1);
413 mtx_unlock(&sc->sc_lock);
414 xpt_free_path(sc->sc_path);
415 xpt_bus_deregister(cam_sim_path(sc->sc_sim));
416 cam_sim_free(sc->sc_sim, TRUE);
417 free(sc->ecb_array, M_DEVBUF);
418 free(sc->sc_tinfo, M_DEVBUF);
419 if (sc->sc_imess_self)
420 free(sc->sc_imess, M_DEVBUF);
421 if (sc->sc_omess_self)
422 free(sc->sc_omess, M_DEVBUF);
423 mtx_destroy(&sc->sc_lock);
424
425 return (0);
426}
427
428/*
429 * This is the generic ncr53c9x reset function. It does not reset the SCSI
430 * bus, only this controller, but kills any on-going commands, and also stops
431 * and resets the DMA.
432 *
433 * After reset, registers are loaded with the defaults from the attach
434 * routine above.
435 */
436void
437ncr53c9x_reset(struct ncr53c9x_softc *sc)
438{
439
440 /* Reset DMA first. */
441 NCRDMA_RESET(sc);
442
443 /* Reset SCSI chip. */
444 NCRCMD(sc, NCRCMD_RSTCHIP);
445 NCRCMD(sc, NCRCMD_NOP);
446 DELAY(500);
447
--- 24 unchanged lines hidden (view full) ---
472 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
473 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
474 break;
475
476 case NCR_VARIANT_FAS366:
477 sc->sc_features |=
478 NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3;
479 sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO;
480 sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI;
481 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
482 sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE | NCRCFG2_HME32 */
483 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
484 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
485 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
486 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
487 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
488 break;
489
490 default:
--- 13 unchanged lines hidden (view full) ---
504 sc->sc_rev);
505 device_printf(sc->sc_dev, "ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, "
506 "cfg3 0x%x, ccf 0x%x, timeout 0x%x\n",
507 sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3, sc->sc_ccf, sc->sc_timeout);
508#endif
509}
510
511/*
512 * Reset the SCSI bus, but not the chip.
513 */
514static void
515ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc)
516{
517
518 (*sc->sc_glue->gl_dma_stop)(sc);
519
520 NCR_MISC(("%s: resetting SCSI bus\n", device_get_nameunit(sc->sc_dev)));
521 NCRCMD(sc, NCRCMD_RSTSCSI);
522 DELAY(250000); /* Give the bus a fighting chance to settle */
523}
524
525/*
526 * Initialize ncr53c9x state machine.
527 */
528void
529ncr53c9x_init(struct ncr53c9x_softc *sc, int doreset)
530{
531 struct ncr53c9x_ecb *ecb;
532 struct ncr53c9x_linfo *li;
533 struct ncr53c9x_tinfo *ti;
534 int i, r;
535
536 NCR_MISC(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state));
537
538 if (sc->sc_state == 0) {
539 /* First time through; initialize. */
540
541 TAILQ_INIT(&sc->ready_list);
542 sc->sc_nexus = NULL;
543 memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
544 for (r = 0; r < sc->sc_ntarg; r++) {
545 LIST_INIT(&sc->sc_tinfo[r].luns);
546 }
547 } else {
548 /* Cancel any active commands. */
549 sc->sc_state = NCR_CLEANING;
550 sc->sc_msgify = 0;
551 if ((ecb = sc->sc_nexus) != NULL) {
552 ecb->ccb->ccb_h.status = CAM_CMD_TIMEOUT;
553 ncr53c9x_done(sc, ecb);
554 }
555 /* Cancel outstanding disconnected commands on each LUN. */
556 for (r = 0; r < sc->sc_ntarg; r++) {
557 LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) {
558 if ((ecb = li->untagged) != NULL) {
559 li->untagged = NULL;
560 /*
561 * XXX
562 *
563 * Should we terminate a command
564 * that never reached the disk?
565 */
566 li->busy = 0;
567 ecb->ccb->ccb_h.status =
568 CAM_CMD_TIMEOUT;
569 ncr53c9x_done(sc, ecb);
570 }
571 for (i = 0; i < 256; i++)
572 if ((ecb = li->queued[i])) {
573 li->queued[i] = NULL;
574 ecb->ccb->ccb_h.status =
575 CAM_CMD_TIMEOUT;
576 ncr53c9x_done(sc, ecb);
577 }
578 li->used = 0;
579 }
580 }
581 }
582
583 /*
584 * Reset the chip to a known state.
585 */
586 ncr53c9x_reset(sc);
587
588 sc->sc_flags = 0;
589 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
590 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
591
592 for (r = 0; r < sc->sc_ntarg; r++) {
593 ti = &sc->sc_tinfo[r];
594/* XXX - config flags per target: low bits: no reselect; high bits: no synch */
595 ti->flags = ((sc->sc_minsync != 0 &&
596 (sc->sc_cfflags & (1 << ((r & 7) + 8))) == 0) ?
597 0 : T_SYNCHOFF) |
598 ((sc->sc_cfflags & (1 << (r & 7))) == 0 ?
599 0 : T_RSELECTOFF);
600
601#ifdef DEBUG
602 if (ncr53c9x_notag)
603 ti->flags &= ~T_TAG;
604#endif
605 ti->period = sc->sc_minsync;
606 ti->offset = 0;
607 ti->cfg3 = 0;
608 }
609
610 if (doreset) {
611 sc->sc_state = NCR_SBR;
612 NCRCMD(sc, NCRCMD_RSTSCSI);
613 } else {
614 sc->sc_state = NCR_IDLE;
615 ncr53c9x_sched(sc);
616 }
617}
618
619/*
620 * Read the NCR registers, and save their contents for later use.
621 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
622 * NCR_INTR - so make sure it is the last read.
623 *
624 * I think that (from reading the docs) most bits in these registers
625 * only make sense when the DMA CSR has an interrupt showing. Call only
626 * if an interrupt is pending.
627 */
628static inline void
629ncr53c9x_readregs(struct ncr53c9x_softc *sc)
630{
631
632 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
633 /* Only the step bits are of interest. */
634 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
635
636 if (sc->sc_rev == NCR_VARIANT_FAS366)
637 sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2);
638
639 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
--- 14 unchanged lines hidden (view full) ---
654
655/*
656 * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
657 */
658static inline int
659ncr53c9x_stp2cpb(struct ncr53c9x_softc *sc, int period)
660{
661 int v;
662 v = (sc->sc_freq * period) / 250;
663 if (ncr53c9x_cpb2stp(sc, v) < period)
664 /* Correct round-down error. */
665 v++;
666 return (v);
667}
668
669static inline void
670ncr53c9x_setsync(struct ncr53c9x_softc *sc, struct ncr53c9x_tinfo *ti)
671{
672 u_char syncoff, synctp;
673 u_char cfg3 = sc->sc_cfg3 | ti->cfg3;
674
675 if (ti->flags & T_SYNCMODE) {
676 syncoff = ti->offset;
677 synctp = ncr53c9x_stp2cpb(sc, ti->period);
678 if (sc->sc_features & NCR_F_FASTSCSI) {
679 /*
680 * If the period is 200ns or less (ti->period <= 50),
681 * put the chip in Fast SCSI mode.
682 */
683 if (ti->period <= 50)
684 /*
685 * There are (at least) 4 variations of the
686 * configuration 3 register. The drive attach
687 * routine sets the appropriate bit to put the
688 * chip into Fast SCSI mode so that it doesn't
689 * have to be figured out here each time.
690 */
691 cfg3 |= sc->sc_cfg3_fscsi;
--- 6 unchanged lines hidden (view full) ---
698 if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
699 (cfg3 & NCRAMDCFG3_FSCSI) == 0)
700 synctp--;
701 } else {
702 syncoff = 0;
703 synctp = 0;
704 }
705
706 if (sc->sc_features & NCR_F_HASCFG3)
707 NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
708
709 NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
710 NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
711}
712
713/*
--- 8 unchanged lines hidden (view full) ---
722{
723 struct ncr53c9x_tinfo *ti;
724 u_char *cmd;
725 size_t dmasize;
726 int clen, selatn3, selatns;
727 int lun = ecb->ccb->ccb_h.target_lun;
728 int target = ecb->ccb->ccb_h.target_id;
729
730 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ",
731 target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1]));
732
733 ti = &sc->sc_tinfo[target];
734 sc->sc_state = NCR_SELECTING;
735 /*
736 * Schedule the timeout now, the first time we will go away
737 * expecting to come back due to an interrupt, because it is
738 * always possible that the interrupt may never happen.
739 */
740 ecb->ccb->ccb_h.timeout_ch =
741 timeout(ncr53c9x_timeout, ecb, mstohz(ecb->timeout));
742
743 /*
744 * The docs say the target register is never reset, and I
745 * can't think of a better place to set it.
746 */
747 if (sc->sc_rev == NCR_VARIANT_FAS366) {
748 NCRCMD(sc, NCRCMD_FLUSH);
749 NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME);
750 } else {
751 NCR_WRITE_REG(sc, NCR_SELID, target);
752 }
753 ncr53c9x_setsync(sc, ti);
754
755 if ((ecb->flags & ECB_SENSE) != 0) {
756 /*
757 * For REQUEST SENSE, we should not send an IDENTIFY or
758 * otherwise mangle the target. There should be no MESSAGE IN
759 * phase.
760 */
761 if (sc->sc_features & NCR_F_DMASELECT) {
762 /* setup DMA transfer for command */
763 dmasize = clen = ecb->clen;
764 sc->sc_cmdlen = clen;
765 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
766
767 /* Program the SCSI counter */
768 NCR_SET_COUNT(sc, dmasize);
769
770 if (sc->sc_rev != NCR_VARIANT_FAS366)
771 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
772
773 /* And get the target's attention */
774 NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
775 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0,
776 &dmasize);
777 NCRDMA_GO(sc);
778 } else {
779 ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
780 NCRCMD(sc, NCRCMD_SELNATN);
781 }
782 return;
783 }
784
785 selatn3 = selatns = 0;
786 if (ecb->tag[0] != 0) {
787 if (sc->sc_features & NCR_F_SELATN3)
788 /* Use SELATN3 to send tag messages. */
789 selatn3 = 1;
790 else
791 /* We don't have SELATN3; use SELATNS to send tags. */
792 selatns = 1;
793 }
794
795 if (ti->flags & T_NEGOTIATE) {
796 /* We have to use SELATNS to send sync/wide messages. */
797 selatn3 = 0;
798 selatns = 1;
799 }
800
801 cmd = (u_char *)&ecb->cmd.cmd;
802
803 if (selatn3) {
--- 10 unchanged lines hidden (view full) ---
814 cmd[0] = MSG_IDENTIFY(lun, (ti->flags & T_RSELECTOFF) == 0);
815 }
816
817 if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) {
818 /* Setup DMA transfer for command. */
819 dmasize = clen;
820 sc->sc_cmdlen = clen;
821 sc->sc_cmdp = cmd;
822
823 /* Program the SCSI counter. */
824 NCR_SET_COUNT(sc, dmasize);
825
826 /* Load the count in. */
827 /* if (sc->sc_rev != NCR_VARIANT_FAS366) */
828 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
829
830 /* And get the target's attention. */
831 if (selatn3) {
832 sc->sc_msgout = SEND_TAG;
833 sc->sc_flags |= NCR_ATN;
834 NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA);
835 } else
836 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
837 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
838 NCRDMA_GO(sc);
839 return;
840 }
841
842 /*
843 * Who am I? This is where we tell the target that we are
844 * happy for it to disconnect etc.
845 */
--- 13 unchanged lines hidden (view full) ---
859 } else
860 NCRCMD(sc, NCRCMD_SELATN);
861}
862
863static void
864ncr53c9x_free_ecb(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
865{
866
867 ecb->flags = 0;
868 TAILQ_INSERT_TAIL(&sc->free_list, ecb, free_links);
869}
870
871static struct ncr53c9x_ecb *
872ncr53c9x_get_ecb(struct ncr53c9x_softc *sc)
873{
874 struct ncr53c9x_ecb *ecb;
875
876 ecb = TAILQ_FIRST(&sc->free_list);
877 if (ecb) {
878 if (ecb->flags != 0)
879 panic("ecb flags not cleared\n");
880 TAILQ_REMOVE(&sc->free_list, ecb, free_links);
881 ecb->flags = ECB_ALLOC;
882 bzero(&ecb->ccb, sizeof(struct ncr53c9x_ecb) -
883 offsetof(struct ncr53c9x_ecb, ccb));
--- 6 unchanged lines hidden (view full) ---
890 */
891
892/*
893 * Start a SCSI-command.
894 * This function is called by the higher level SCSI-driver to queue/run
895 * SCSI-commands.
896 */
897
898void
899ncr53c9x_action(struct cam_sim *sim, union ccb *ccb)
900{
901 struct ccb_pathinq *cpi;
902 struct ccb_scsiio *csio;
903 struct ccb_trans_settings *cts;
904 struct ccb_trans_settings_scsi *scsi;
905 struct ccb_trans_settings_spi *spi;
906 struct ncr53c9x_ecb *ecb;
907 struct ncr53c9x_softc *sc;
908 struct ncr53c9x_tinfo *ti;
909 int target;
910
911 NCR_TRACE(("[ncr53c9x_action %d]", ccb->ccb_h.func_code));
912
913 sc = cam_sim_softc(sim);
914 mtx_lock(&sc->sc_lock);
915
916 switch (ccb->ccb_h.func_code) {
917 case XPT_RESET_BUS:
918 ncr53c9x_scsi_reset(sc);
919 ccb->ccb_h.status = CAM_REQ_CMP;
920 mtx_unlock(&sc->sc_lock);
921 xpt_done(ccb);
922 return;
923
924 case XPT_CALC_GEOMETRY:
925 mtx_unlock(&sc->sc_lock);
926 cam_calc_geometry(&ccb->ccg, sc->sc_extended_geom);
927 xpt_done(ccb);
928 return;
929
930 case XPT_PATH_INQ:
931 cpi = &ccb->cpi;
932 cpi->version_num = 1;
933 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
934 cpi->hba_inquiry |=
935 (sc->sc_rev == NCR_VARIANT_FAS366) ? PI_WIDE_16 : 0;
936 cpi->target_sprt = 0;
937 cpi->hba_misc = 0;
938 cpi->hba_eng_cnt = 0;
939 cpi->max_target = sc->sc_ntarg - 1;
940 cpi->max_lun = 8;
941 cpi->initiator_id = sc->sc_id;
942 cpi->bus_id = 0;
943 cpi->base_transfer_speed = 3300;
944 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
945 strncpy(cpi->hba_vid, "Sun", HBA_IDLEN);
946 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
947 cpi->unit_number = cam_sim_unit(sim);
948 cpi->transport = XPORT_SPI;
949 cpi->transport_version = 2;
950 cpi->protocol = PROTO_SCSI;
951 cpi->protocol_version = SCSI_REV_2;
952 ccb->ccb_h.status = CAM_REQ_CMP;
953 mtx_unlock(&sc->sc_lock);
954 xpt_done(ccb);
955 return;
956
957 case XPT_GET_TRAN_SETTINGS:
958 cts = &ccb->cts;
959 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
960 scsi = &cts->proto_specific.scsi;
961 spi = &cts->xport_specific.spi;
962
963 cts->protocol = PROTO_SCSI;
964 cts->protocol_version = SCSI_REV_2;
965 cts->transport = XPORT_SPI;
966 cts->transport_version = 2;
967
968 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
969 spi->sync_period = ti->period;
970 spi->sync_offset = ti->offset;
971 spi->bus_width = ti->width;
972 if ((ti->flags & T_TAG) != 0) {
973 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
974 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
975 } else {
976 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
977 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
978 }
979 } else {
980 spi->sync_period = sc->sc_maxsync;
981 spi->sync_offset = sc->sc_maxoffset;
982 spi->bus_width = sc->sc_maxwidth;
983 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
984 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
985 }
986 spi->valid =
987 CTS_SPI_VALID_BUS_WIDTH |
988 CTS_SPI_VALID_SYNC_RATE |
989 CTS_SPI_VALID_SYNC_OFFSET |
990 CTS_SPI_VALID_DISC;
991 scsi->valid = CTS_SCSI_VALID_TQ;
992 ccb->ccb_h.status = CAM_REQ_CMP;
993 mtx_unlock(&sc->sc_lock);
994 xpt_done(ccb);
995 return;
996
997 case XPT_ABORT:
998 printf("XPT_ABORT called\n");
999 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1000 mtx_unlock(&sc->sc_lock);
1001 xpt_done(ccb);
1002 return;
1003
1004 case XPT_TERM_IO:
1005 printf("XPT_TERM_IO called\n");
1006 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1007 mtx_unlock(&sc->sc_lock);
1008 xpt_done(ccb);
1009 return;
1010
1011 case XPT_RESET_DEV:
1012 printf("XPT_RESET_DEV called\n");
1013 case XPT_SCSI_IO:
1014 if (ccb->ccb_h.target_id < 0 ||
1015 ccb->ccb_h.target_id >= sc->sc_ntarg) {
1016 ccb->ccb_h.status = CAM_PATH_INVALID;
1017 mtx_unlock(&sc->sc_lock);
1018 xpt_done(ccb);
1019 return;
1020 }
1021 /* Get an ECB to use. */
1022 ecb = ncr53c9x_get_ecb(sc);
1023 /*
1024 * This should never happen as we track resources
1025 * in the mid-layer.
1026 */
1027 if (ecb == NULL) {
1028 xpt_freeze_simq(sim, 1);
1029 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1030 printf("unable to allocate ecb\n");
1031 mtx_unlock(&sc->sc_lock);
1032 xpt_done(ccb);
1033 return;
1034 }
1035
1036 /* Initialize ecb. */
1037 ecb->ccb = ccb;
1038 ecb->timeout = ccb->ccb_h.timeout;
1039
--- 34 unchanged lines hidden (view full) ---
1074 NCR_MISC(("%s: target %d: tagged queuing\n",
1075 device_get_nameunit(sc->sc_dev), target));
1076 ti->flags |= T_TAG;
1077 } else
1078 ti->flags &= ~T_TAG;
1079 }
1080
1081 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
1082 if (spi->bus_width != 0) {
1083 NCR_MISC(("%s: target %d: wide negotiation\n",
1084 device_get_nameunit(sc->sc_dev), target));
1085 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1086 ti->flags |= T_WIDE;
1087 ti->width = 1;
1088 }
1089 } else {
1090 ti->flags &= ~T_WIDE;
1091 ti->width = 0;
1092 }
1093 ti->flags |= T_NEGOTIATE;
1094 }
1095
1096 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
1097 NCR_MISC(("%s: target %d: sync period negotiation\n",
1098 device_get_nameunit(sc->sc_dev), target));
1099 ti->flags |= T_NEGOTIATE;
1100 ti->period = spi->sync_period;
1101 }
1102
1103 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
1104 NCR_MISC(("%s: target %d: sync offset negotiation\n",
1105 device_get_nameunit(sc->sc_dev), target));
1106 ti->flags |= T_NEGOTIATE;
1107 ti->offset = spi->sync_offset;
1108 }
1109
1110 mtx_unlock(&sc->sc_lock);
1111 ccb->ccb_h.status = CAM_REQ_CMP;
1112 xpt_done(ccb);
1113 return;
1114
1115 default:
1116 device_printf(sc->sc_dev, "Unhandled function code %d\n",
1117 ccb->ccb_h.func_code);
1118 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1119 mtx_unlock(&sc->sc_lock);
1120 xpt_done(ccb);
1121 return;
1122 }
1123
1124 mtx_unlock(&sc->sc_lock);
1125}
1126
1127/*
1128 * Used when interrupt driven I/O is not allowed, e.g. during boot.
1129 */
1130static void
1131ncr53c9x_poll(struct cam_sim *sim)
1132{
1133 struct ncr53c9x_softc *sc;
1134
1135 NCR_TRACE(("[ncr53c9x_poll] "));
1136 sc = cam_sim_softc(sim);
1137 if (NCRDMA_ISINTR(sc)) {
1138 ncr53c9x_intr(sc);
1139 }
1140}
1141
1142/*
1143 * LOW LEVEL SCSI UTILITIES
1144 */
1145
1146/*
1147 * Schedule a SCSI operation. This has now been pulled out of the interrupt
1148 * handler so that we may call it from ncr53c9x_scsipi_request and
1149 * ncr53c9x_done. This may save us an unnecessary interrupt just to get
1150 * things going. Should only be called when state == NCR_IDLE and at bio pl.
1151 */
1152static void
1153ncr53c9x_sched(struct ncr53c9x_softc *sc)
1154{
1155 struct ncr53c9x_ecb *ecb;
1156 struct ncr53c9x_linfo *li;
1157 struct ncr53c9x_tinfo *ti;
1158 int lun, tag;
1159
1160 NCR_TRACE(("[ncr53c9x_sched] "));
1161
1162 if (sc->sc_state != NCR_IDLE)
1163 panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
1164
1165 /*
1166 * Find first ecb in ready queue that is for a target/lunit
1167 * combinations that is not busy.
1168 */
1169 for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
1170 ecb = TAILQ_NEXT(ecb, chain)) {
1171 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1172 lun = ecb->ccb->ccb_h.target_lun;
1173
1174 /* Select type of tag for this command */
1175 if ((ti->flags & (T_RSELECTOFF)) != 0)
1176 tag = 0;
1177 else if ((ti->flags & (T_TAG)) == 0)
1178 tag = 0;
1179 else if ((ecb->flags & ECB_SENSE) != 0)
1180 tag = 0;
1181 else if ((ecb->ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0)
1182 tag = 0;
1183 else if (ecb->ccb->csio.tag_action == CAM_TAG_ACTION_NONE)
1184 tag = 0;
1185 else
1186 tag = ecb->ccb->csio.tag_action;
--- 62 unchanged lines hidden (view full) ---
1249ncr53c9x_sense(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1250{
1251 union ccb *ccb = ecb->ccb;
1252 struct ncr53c9x_linfo *li;
1253 struct ncr53c9x_tinfo *ti;
1254 struct scsi_request_sense *ss = (void *)&ecb->cmd.cmd;
1255 int lun;
1256
1257 NCR_TRACE(("requesting sense "));
1258
1259 lun = ccb->ccb_h.target_lun;
1260 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
1261
1262 /* Next, setup a REQUEST SENSE command block. */
1263 memset(ss, 0, sizeof(*ss));
1264 ss->opcode = REQUEST_SENSE;
--- 27 unchanged lines hidden (view full) ---
1292static void
1293ncr53c9x_done(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1294{
1295 union ccb *ccb = ecb->ccb;
1296 struct ncr53c9x_linfo *li;
1297 struct ncr53c9x_tinfo *ti;
1298 int lun;
1299
1300 NCR_TRACE(("[ncr53c9x_done(status:%x)] ", ccb->ccb_h.status));
1301
1302 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
1303 lun = ccb->ccb_h.target_lun;
1304 li = TINFO_LUN(ti, lun);
1305
1306 untimeout(ncr53c9x_timeout, ecb, ccb->ccb_h.timeout_ch);
1307
1308 /*
1309 * Now, if we've come here with no error code, i.e. we've kept the
1310 * initial XS_NOERROR, and the status code signals that we should
1311 * check sense, we'll need to set up a request sense cmd block and
1312 * push the command back into the ready queue *before* any other
1313 * commands for this target/lunit, else we lose the sense info.
1314 * We don't support chk sense conditions for the request sense cmd.
1315 */
1316 if (ccb->ccb_h.status == CAM_REQ_CMP) {
1317 if ((ecb->flags & ECB_ABORT) != 0) {
1318 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1319 } else if ((ecb->flags & ECB_SENSE) != 0 &&
1320 (ecb->stat != SCSI_STATUS_CHECK_COND)) {
1321 ccb->ccb_h.status = CAM_AUTOSNS_VALID;
1322 } else if (ecb->stat == SCSI_STATUS_CHECK_COND) {
1323 if ((ecb->flags & ECB_SENSE) != 0)
1324 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1325 else {
1326 /* First, save the return values. */
1327 ccb->csio.resid = ecb->dleft;
1328 ncr53c9x_sense(sc, ecb);
1329 return;
1330 }
1331 } else {
1332 ccb->csio.resid = ecb->dleft;
1333 }
1334#if 0
1335 if (xs->status == SCSI_QUEUE_FULL || xs->status == XS_BUSY)
1336 xs->error = XS_BUSY;
1337#endif
1338 }
1339
1340#ifdef NCR53C9X_DEBUG
1341 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1342 if (ccb->csio.resid != 0)
1343 printf("resid=%d ", ccb->csio.resid);
1344#if 0
1345 if (xs->error == XS_SENSE)
1346 printf("sense=0x%02x\n",
1347 xs->sense.scsi_sense.error_code);
1348 else
1349 printf("error=%d\n", xs->error);
1350#endif
1351 }
1352#endif
1353
1354 /*
1355 * Remove the ECB from whatever queue it's on.
1356 */
1357 ncr53c9x_dequeue(sc, ecb);
1358 if (ecb == sc->sc_nexus) {
--- 21 unchanged lines hidden (view full) ---
1380
1381static void
1382ncr53c9x_dequeue(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1383{
1384 struct ncr53c9x_linfo *li;
1385 struct ncr53c9x_tinfo *ti;
1386 int64_t lun;
1387
1388 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1389 lun = ecb->ccb->ccb_h.target_lun;
1390 li = TINFO_LUN(ti, lun);
1391#ifdef DIAGNOSTIC
1392 if (li == NULL || li->lun != lun)
1393 panic("ncr53c9x_dequeue: lun %qx for ecb %p does not exist",
1394 (long long)lun, ecb);
1395#endif
--- 7 unchanged lines hidden (view full) ---
1403 (li->queued[ecb->tag[1]] != ecb))
1404 panic("ncr53c9x_dequeue: slot %d for lun %qx has %p "
1405 "instead of ecb %p\n", ecb->tag[1],
1406 (long long)lun, li->queued[ecb->tag[1]], ecb);
1407#endif
1408 li->queued[ecb->tag[1]] = NULL;
1409 li->used--;
1410 }
1411
1412 if ((ecb->flags & ECB_READY) != 0) {
1413 ecb->flags &= ~ECB_READY;
1414 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1415 }
1416}
1417
1418/*
--- 10 unchanged lines hidden (view full) ---
1429 NCRCMD(sc, NCRCMD_SETATN); \
1430 sc->sc_flags |= NCR_ATN; \
1431 sc->sc_msgpriq |= (m); \
1432} while (0)
1433
1434static void
1435ncr53c9x_flushfifo(struct ncr53c9x_softc *sc)
1436{
1437 NCR_TRACE(("[flushfifo] "));
1438
1439 NCRCMD(sc, NCRCMD_FLUSH);
1440
1441 if (sc->sc_phase == COMMAND_PHASE ||
1442 sc->sc_phase == MESSAGE_OUT_PHASE)
1443 DELAY(2);
1444}
1445
1446static int
1447ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how)
1448{
1449 u_char *ibuf;
1450 int i, n;
1451
1452 switch (how) {
1453 case NCR_RDFIFO_START:
1454 ibuf = sc->sc_imess;
1455 sc->sc_imlen = 0;
1456 break;
1457
1458 case NCR_RDFIFO_CONTINUE:
1459 ibuf = sc->sc_imess + sc->sc_imlen;
--- 46 unchanged lines hidden (view full) ---
1506 return (sc->sc_imlen);
1507}
1508
1509static void
1510ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p, int len)
1511{
1512 int i;
1513
1514#ifdef NCR53C9X_DEBUG
1515 NCR_MSGS(("[wrfifo(%d):", len));
1516 if (ncr53c9x_debug & NCR_SHOWMSGS) {
1517 for (i = 0; i < len; i++)
1518 printf(" %02x", p[i]);
1519 printf("]\n");
1520 }
1521#endif
--- 10 unchanged lines hidden (view full) ---
1532ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message, int tagtype,
1533 int tagid)
1534{
1535 struct ncr53c9x_ecb *ecb = NULL;
1536 struct ncr53c9x_linfo *li;
1537 struct ncr53c9x_tinfo *ti;
1538 u_char lun, selid, target;
1539
1540
1541 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1542 target = sc->sc_selid;
1543 } else {
1544 /*
1545 * The SCSI chip made a snapshot of the data bus
1546 * while the reselection was being negotiated.
1547 * This enables us to determine which target did
--- 98 unchanged lines hidden (view full) ---
1646ncr53c9x_msgin(struct ncr53c9x_softc *sc)
1647{
1648 struct ncr53c9x_ecb *ecb;
1649 struct ncr53c9x_linfo *li;
1650 struct ncr53c9x_tinfo *ti;
1651 u_char *pb;
1652 int lun, plen;
1653
1654 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
1655
1656 if (sc->sc_imlen == 0) {
1657 device_printf(sc->sc_dev, "msgin: no msg byte available\n");
1658 return;
1659 }
1660
1661 /*
--- 11 unchanged lines hidden (view full) ---
1673 }
1674
1675 /*
1676 * If we're going to reject the message, don't bother storing
1677 * the incoming bytes. But still, we need to ACK them.
1678 */
1679 if ((sc->sc_flags & NCR_DROP_MSGI) != 0) {
1680 NCRCMD(sc, NCRCMD_MSGOK);
1681 printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]);
1682 return;
1683 }
1684
1685 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
1686 ncr53c9x_sched_msgout(SEND_REJECT);
1687 sc->sc_flags |= NCR_DROP_MSGI;
1688 } else {
1689 switch (sc->sc_state) {
--- 83 unchanged lines hidden (view full) ---
1773 li->busy = 1;
1774 break;
1775
1776 case SEND_SDTR:
1777 device_printf(sc->sc_dev, "sync transfer "
1778 "rejected: target %d\n",
1779 ecb->ccb->ccb_h.target_id);
1780
1781 sc->sc_flags &= ~NCR_SYNCHNEGO;
1782 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1783 ncr53c9x_setsync(sc, ti);
1784 break;
1785
1786 case SEND_WDTR:
1787 device_printf(sc->sc_dev, "wide transfer "
1788 "rejected: target %d\n",
1789 ecb->ccb->ccb_h.target_id);
1790 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1791 ti->width = 0;
1792 break;
1793
1794 case SEND_INIT_DET_ERR:
1795 goto abort;
1796 }
1797 break;
1798
1799 case MSG_NOOP:
--- 37 unchanged lines hidden (view full) ---
1837 case MSG_EXTENDED:
1838 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1839 switch (sc->sc_imess[2]) {
1840 case MSG_EXT_SDTR:
1841 NCR_MSGS(("SDTR period %d, offset %d ",
1842 sc->sc_imess[3], sc->sc_imess[4]));
1843 if (sc->sc_imess[1] != 3)
1844 goto reject;
1845 ti->period = sc->sc_imess[3];
1846 ti->offset = sc->sc_imess[4];
1847 ti->flags &= ~T_NEGOTIATE;
1848 if (sc->sc_minsync == 0 ||
1849 ti->offset == 0 ||
1850 ti->period > 124) {
1851#if 0
1852#ifdef NCR53C9X_DEBUG
1853 xpt_print_path(ecb->ccb->ccb_h.path);
1854 printf("async mode\n");
1855#endif
1856#endif
1857 ti->flags &= ~T_SYNCMODE;
1858 if ((sc->sc_flags & NCR_SYNCHNEGO) ==
1859 0) {
1860 /*
1861 * target initiated negotiation
1862 */
1863 ti->offset = 0;
1864 ncr53c9x_sched_msgout(
1865 SEND_SDTR);
1866 }
1867 } else {
1868 ti->period =
1869 ncr53c9x_cpb2stp(sc,
1870 ncr53c9x_stp2cpb(sc,
1871 ti->period));
1872 if ((sc->sc_flags & NCR_SYNCHNEGO) ==
1873 0) {
1874 /*
1875 * target initiated negotiation
1876 */
1877 if (ti->period < sc->sc_minsync)
1878 ti->period =
1879 sc->sc_minsync;
1880 if (ti->offset > 15)
1881 ti->offset = 15;
1882 ti->flags &= ~T_SYNCMODE;
1883 ncr53c9x_sched_msgout(
1884 SEND_SDTR);
1885 } else {
1886 /* We are sync. */
1887 ti->flags |= T_SYNCMODE;
1888 }
1889 }
1890 sc->sc_flags &= ~NCR_SYNCHNEGO;
1891 ncr53c9x_setsync(sc, ti);
1892 break;
1893
1894 case MSG_EXT_WDTR:
1895#ifdef NCR53C9X_DEBUG
1896 device_printf(sc->sc_dev, "wide mode %d\n",
1897 sc->sc_imess[3]);
1898#endif
1899 if (sc->sc_imess[3] == 1) {
1900 ti->cfg3 |= NCRFASCFG3_EWIDE;
1901 ncr53c9x_setsync(sc, ti);
1902 } else
1903 ti->width = 0;
1904 /*
1905 * Device started width negotiation.
1906 */
1907 if (!(ti->flags & T_WDTRSENT))
1908 ncr53c9x_sched_msgout(SEND_WDTR);
1909 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1910 break;
1911
1912 default:
1913 xpt_print_path(ecb->ccb->ccb_h.path);
1914 printf("unrecognized MESSAGE EXTENDED;"
1915 " sending REJECT\n");
1916 goto reject;
1917 }
--- 45 unchanged lines hidden (view full) ---
1963 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1964 break;
1965
1966 abort:
1967 ncr53c9x_sched_msgout(SEND_ABORT);
1968 }
1969
1970 /* If we have more messages to send set ATN. */
1971 if (sc->sc_msgpriq)
1972 NCRCMD(sc, NCRCMD_SETATN);
1973
1974 /* Acknowledge last message byte. */
1975 NCRCMD(sc, NCRCMD_MSGOK);
1976
1977 /* Done, reset message pointer. */
1978 sc->sc_flags &= ~NCR_DROP_MSGI;
1979 sc->sc_imlen = 0;
1980}
--- 6 unchanged lines hidden (view full) ---
1987{
1988 struct ncr53c9x_tinfo *ti;
1989 struct ncr53c9x_ecb *ecb;
1990 size_t size;
1991#ifdef NCR53C9X_DEBUG
1992 int i;
1993#endif
1994
1995 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1996 sc->sc_msgpriq, sc->sc_prevphase));
1997
1998 /*
1999 * XXX - the NCR_ATN flag is not in sync with the actual ATN
2000 * condition on the SCSI bus. The 53c9x chip
2001 * automatically turns off ATN before sending the
2002 * message byte. (See also the comment below in the
--- 25 unchanged lines hidden (view full) ---
2028 sc->sc_omlen = 1; /* "Default" message len */
2029 switch (sc->sc_msgout) {
2030 case SEND_SDTR:
2031 ecb = sc->sc_nexus;
2032 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2033 sc->sc_omess[0] = MSG_EXTENDED;
2034 sc->sc_omess[1] = MSG_EXT_SDTR_LEN;
2035 sc->sc_omess[2] = MSG_EXT_SDTR;
2036 sc->sc_omess[3] = ti->period;
2037 sc->sc_omess[4] = ti->offset;
2038 sc->sc_omlen = 5;
2039 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
2040 ti->flags |= T_SYNCMODE;
2041 ncr53c9x_setsync(sc, ti);
2042 }
2043 break;
2044
2045 case SEND_WDTR:
2046 ecb = sc->sc_nexus;
2047 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2048 sc->sc_omess[0] = MSG_EXTENDED;
2049 sc->sc_omess[1] = MSG_EXT_WDTR_LEN;
2050 sc->sc_omess[2] = MSG_EXT_WDTR;
2051 sc->sc_omess[3] = ti->width;
2052 sc->sc_omlen = 4;
2053 break;
2054
2055 case SEND_IDENTIFY:
2056 if (sc->sc_state != NCR_CONNECTED) {
2057 device_printf(sc->sc_dev, "at line %d: no "
2058 "nexus\n", __LINE__);
2059 }
--- 13 unchanged lines hidden (view full) ---
2073 sc->sc_omlen = 2;
2074 break;
2075
2076 case SEND_DEV_RESET:
2077 sc->sc_flags |= NCR_ABORTING;
2078 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
2079 ecb = sc->sc_nexus;
2080 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2081 ti->flags &= ~T_SYNCMODE;
2082 if ((ti->flags & T_SYNCHOFF) == 0)
2083 /* We can re-start sync negotiation. */
2084 ti->flags |= T_NEGOTIATE;
2085 break;
2086
2087 case SEND_PARITY_ERROR:
2088 sc->sc_omess[0] = MSG_PARITY_ERROR;
2089 break;
2090
2091 case SEND_ABORT:
2092 sc->sc_flags |= NCR_ABORTING;
--- 40 unchanged lines hidden (view full) ---
2133 /*
2134 * XXX FIFO size
2135 */
2136 ncr53c9x_flushfifo(sc);
2137 ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen);
2138 NCRCMD(sc, NCRCMD_TRANS);
2139 } else {
2140 /* (Re)send the message. */
2141 size = min(sc->sc_omlen, sc->sc_maxxfer);
2142 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
2143 /* Program the SCSI counter. */
2144 NCR_SET_COUNT(sc, size);
2145
2146 /* Load the count in and start the message-out transfer. */
2147 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
2148 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
2149 NCRDMA_GO(sc);
2150 }
2151}
2152
2153/*
2154 * This is the most critical part of the driver, and has to know
2155 * how to deal with *all* error conditions and phases from the SCSI
2156 * bus. If there are no errors and the DMA was active, then call the
2157 * DMA pseudo-interrupt handler. If this returns 1, then that was it
2158 * and we can return from here without further processing.
2159 *
2160 * Most of this needs verifying.
2161 */
2162void
2163ncr53c9x_intr(void *arg)
2164{
2165 struct ncr53c9x_softc *sc = arg;
2166 struct ncr53c9x_ecb *ecb;
2167 struct ncr53c9x_linfo *li;
2168 struct ncr53c9x_tinfo *ti;
2169 struct timeval cur, wait;
2170 size_t size;
2171 int i, nfifo;
2172 u_char msg;
2173
2174 NCR_INTS(("[ncr53c9x_intr: state %d]", sc->sc_state));
2175
2176 if (!NCRDMA_ISINTR(sc))
2177 return;
2178
2179 mtx_lock(&sc->sc_lock);
2180again:
2181 /* and what do the registers say... */
2182 ncr53c9x_readregs(sc);
2183
2184 /*
2185 * At the moment, only a SCSI Bus Reset or Illegal
2186 * Command are classed as errors. A disconnect is a
2187 * valid condition, and we let the code check is the
--- 13 unchanged lines hidden (view full) ---
2201 if ((sc->sc_espintr & NCRINTR_SBR) != 0) {
2202 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) {
2203 NCRCMD(sc, NCRCMD_FLUSH);
2204 DELAY(1);
2205 }
2206 if (sc->sc_state != NCR_SBR) {
2207 device_printf(sc->sc_dev, "SCSI bus reset\n");
2208 ncr53c9x_init(sc, 0); /* Restart everything. */
2209 goto out;
2210 }
2211#if 0
2212/*XXX*/ printf("<expected bus reset: "
2213 "[intr %x, stat %x, step %d]>\n",
2214 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2215#endif
2216 if (sc->sc_nexus != NULL)
2217 panic("%s: nexus in reset state",
2218 device_get_nameunit(sc->sc_dev));
2219 goto sched;
2220 }
--- 9 unchanged lines hidden (view full) ---
2230 NCRCMD(sc, NCRCMD_FLUSH);
2231 DELAY(1);
2232 }
2233 if (sc->sc_state == NCR_CONNECTED ||
2234 sc->sc_state == NCR_SELECTING) {
2235 ecb->ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2236 ncr53c9x_done(sc, ecb);
2237 }
2238 goto out;
2239 }
2240
2241 if ((sc->sc_espintr & NCRINTR_ILL) != 0) {
2242 if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) {
2243 /*
2244 * Eat away "Illegal command" interrupt
2245 * on a ESP100 caused by a re-selection
2246 * while we were trying to select
2247 * another target.
2248 */
2249#ifdef DEBUG
2250 device_printf(sc->sc_dev, "ESP100 work-around "
2251 "activated\n");
2252#endif
2253 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2254 goto out;
2255 }
2256 /* Illegal command, out of sync? */
2257 device_printf(sc->sc_dev, "illegal command: 0x%x "
2258 "(state %d, phase %x, prevphase %x)\n",
2259 sc->sc_lastcmd,
2260 sc->sc_state, sc->sc_phase, sc->sc_prevphase);
2261 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2262 NCRCMD(sc, NCRCMD_FLUSH);
2263 DELAY(1);
2264 }
2265 ncr53c9x_init(sc, 1); /* Restart everything. */
2266 goto out;
2267 }
2268 }
2269 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2270
2271 /*
2272 * Call if DMA is active.
2273 *
2274 * If DMA_INTR returns true, then maybe go 'round the loop
2275 * again in case there is no more DMA queued, but a phase
2276 * change is expected.
2277 */
2278 if (NCRDMA_ISACTIVE(sc)) {
2279 if (NCRDMA_INTR(sc) == -1) {
2280 device_printf(sc->sc_dev, "DMA error; resetting\n");
2281 ncr53c9x_init(sc, 1);
2282 goto out;
2283 }
2284 /* If DMA active here, then go back to work... */
2285 if (NCRDMA_ISACTIVE(sc))
2286 goto out;
2287
2288 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
2289 /*
2290 * DMA not completed. If we can not find a
2291 * acceptable explanation, print a diagnostic.
2292 */
2293 if (sc->sc_state == NCR_SELECTING)
2294 /*
--- 33 unchanged lines hidden (view full) ---
2328 device_printf(sc->sc_dev, "!TC on DATA XFER"
2329 " [intr %x, stat %x, step %d]"
2330 " prevphase %x, resid %x\n",
2331 sc->sc_espintr,
2332 sc->sc_espstat,
2333 sc->sc_espstep,
2334 sc->sc_prevphase,
2335 ecb ? ecb->dleft : -1);
2336 panic("esp: unrecoverable DMA error");
2337 }
2338 }
2339 }
2340
2341 /*
2342 * Check for less serious errors.
2343 */
2344 if ((sc->sc_espstat & NCRSTAT_PE) != 0) {
--- 40 unchanged lines hidden (view full) ---
2385 li = LIST_FIRST(&ti->luns);
2386 continue;
2387 }
2388 li = LIST_NEXT(li, link);
2389 }
2390 goto finish;
2391
2392 case NCR_CONNECTED:
2393 if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) {
2394#ifdef NCR53C9X_DEBUG
2395 if (ecb != NULL)
2396 xpt_print_path(ecb->ccb->ccb_h.path);
2397 printf("sync nego not completed!\n");
2398#endif
2399 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2400 sc->sc_flags &= ~NCR_SYNCHNEGO;
2401 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
2402 }
2403
2404 /* It may be OK to disconnect. */
2405 if ((sc->sc_flags & NCR_ABORTING) == 0) {
2406 /*
2407 * Section 5.1.1 of the SCSI 2 spec
2408 * suggests issuing a REQUEST SENSE
2409 * following an unexpected disconnect.
--- 16 unchanged lines hidden (view full) ---
2426 * safely cancel the DMA that is in
2427 * progress.
2428 */
2429 if (1 || (ecb->flags & ECB_SENSE) != 0) {
2430 printf("resetting\n");
2431 goto reset;
2432 }
2433 printf("sending REQUEST SENSE\n");
2434 untimeout(ncr53c9x_timeout, ecb,
2435 ecb->ccb->ccb_h.timeout_ch);
2436 ncr53c9x_sense(sc, ecb);
2437 goto out;
2438 }
2439
2440 ecb->ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2441 goto finish;
2442
2443 case NCR_DISCONNECT:
2444 sc->sc_nexus = NULL;
2445 goto sched;
2446
2447 case NCR_CMDCOMPLETE:
2448 ecb->ccb->ccb_h.status = CAM_REQ_CMP;
2449 goto finish;
2450 }
2451 }
2452
2453 switch (sc->sc_state) {
2454 case NCR_SBR:
2455 device_printf(sc->sc_dev, "waiting for Bus Reset to happen\n");
2456 goto out;
2457
2458 case NCR_RESELECTED:
2459 /*
2460 * We must be continuing a message?
2461 */
2462 device_printf(sc->sc_dev, "unhandled reselect continuation, "
2463 "state %d, intr %02x\n", sc->sc_state, sc->sc_espintr);
2464 ncr53c9x_init(sc, 1);
2465 goto out;
2466 break;
2467
2468 case NCR_IDENTIFIED:
2469 ecb = sc->sc_nexus;
2470 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2471 i = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
2472 /*
2473 * Things are seriously screwed up.
2474 * Pull the brakes, i.e. reset.
2475 */
2476 device_printf(sc->sc_dev, "target didn't send tag: %d "
2477 "bytes in FIFO\n", i);
2478 /* Drain and display FIFO. */
2479 while (i-- > 0)
2480 printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO));
2481
2482 ncr53c9x_init(sc, 1);
2483 goto out;
2484 } else
2485 goto msgin;
2486
2487 case NCR_IDLE:
2488 case NCR_SELECTING:
2489 ecb = sc->sc_nexus;
2490 if (sc->sc_espintr & NCRINTR_RESEL) {
2491 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
2492 sc->sc_flags = 0;
2493 /*
2494 * If we're trying to select a
2495 * target ourselves, push our command
2496 * back into the ready list.
2497 */
2498 if (sc->sc_state == NCR_SELECTING) {
2499 NCR_INTS(("backoff selector "));
2500 untimeout(ncr53c9x_timeout, ecb,
2501 ecb->ccb->ccb_h.timeout_ch);
2502 ncr53c9x_dequeue(sc, ecb);
2503 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
2504 ecb->flags |= ECB_READY;
2505 ecb = sc->sc_nexus = NULL;
2506 }
2507 sc->sc_state = NCR_RESELECTED;
2508 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2509 /*
2510 * Things are seriously screwed up.
2511 * Pull the brakes, i.e. reset
2512 */
2513 device_printf(sc->sc_dev, "target didn't "
2514 "identify\n");
2515 ncr53c9x_init(sc, 1);
2516 goto out;
2517 }
2518 /*
2519 * The C90 only inhibits FIFO writes until reselection
2520 * is complete instead of waiting until the interrupt
2521 * status register has been read. So, if the reselect
2522 * happens while we were entering command bytes (for
2523 * another target) some of those bytes can appear in
2524 * the FIFO here, after the interrupt is taken.
--- 25 unchanged lines hidden (view full) ---
2550 device_printf(sc->sc_dev, "RESELECT: %d bytes "
2551 "in FIFO! [intr %x, stat %x, step %d, "
2552 "prevphase %x]\n",
2553 nfifo,
2554 sc->sc_espintr,
2555 sc->sc_espstat,
2556 sc->sc_espstep,
2557 sc->sc_prevphase);
2558 ncr53c9x_init(sc, 1);
2559 goto out;
2560 }
2561 sc->sc_selid = sc->sc_imess[0];
2562 NCR_INTS(("selid=%02x ", sc->sc_selid));
2563
2564 /* Handle IDENTIFY message. */
2565 ncr53c9x_msgin(sc);
2566
2567 if (sc->sc_state != NCR_CONNECTED &&
2568 sc->sc_state != NCR_IDENTIFIED) {
2569 /* IDENTIFY fail?! */
2570 device_printf(sc->sc_dev, "identify failed, "
2571 "state %d, intr %02x\n", sc->sc_state,
2572 sc->sc_espintr);
2573 ncr53c9x_init(sc, 1);
2574 goto out;
2575 }
2576 goto shortcut; /* i.e. next phase expected soon */
2577 }
2578
2579#define NCRINTR_DONE (NCRINTR_FC | NCRINTR_BS)
2580 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
2581 /*
2582 * Arbitration won; examine the `step' register
2583 * to determine how far the selection could progress.
2584 */
2585 if (ecb == NULL) {
2586 /*
2587 * When doing path inquiry during boot
2588 * FAS100A trigger a stray interrupt which
2589 * we just ignore instead of panicing.
2590 */
2591 if (sc->sc_state == NCR_IDLE &&
2592 sc->sc_espstep == 0)
2593 goto out;
2594 panic("ncr53c9x: no nexus");
2595 }
2596
2597 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2598
2599 switch (sc->sc_espstep) {
2600 case 0:
2601 /*
--- 4 unchanged lines hidden (view full) ---
2606 * target should be in the command
2607 * phase.
2608 * XXX check for command phase?
2609 */
2610 NCRCMD(sc, NCRCMD_RSTATN);
2611 break;
2612
2613 case 1:
2614 if ((ti->flags & T_NEGOTIATE) == 0 &&
2615 ecb->tag[0] == 0) {
2616 device_printf(sc->sc_dev, "step 1 & "
2617 "!NEG\n");
2618 goto reset;
2619 }
2620 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
2621 device_printf(sc->sc_dev, "!MSGOUT\n");
2622 goto reset;
2623 }
2624 if (ti->flags & T_WIDE) {
2625 ti->flags |= T_WDTRSENT;
2626 ncr53c9x_sched_msgout(SEND_WDTR);
2627 }
2628 if (ti->flags & T_NEGOTIATE) {
2629 /* Start negotiating */
2630 sc->sc_flags |= NCR_SYNCHNEGO;
2631 if (ecb->tag[0])
2632 ncr53c9x_sched_msgout(
2633 SEND_TAG|SEND_SDTR);
2634 else
2635 ncr53c9x_sched_msgout(
2636 SEND_SDTR);
2637 } else {
2638 /* Could not do ATN3 so send TAG. */
2639 ncr53c9x_sched_msgout(SEND_TAG);
2640 }
2641 sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */
2642 break;
2643
2644 case 3:
2645 /*
2646 * Grr, this is supposed to mean
2647 * "target left command phase prematurely".
2648 * It seems to happen regularly when
2649 * sync mode is on.
--- 4 unchanged lines hidden (view full) ---
2654 if (sc->sc_cmdlen == 0)
2655 /* Hope for the best... */
2656 break;
2657 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
2658 & NCRFIFO_FF) == 0) {
2659 /* Hope for the best... */
2660 break;
2661 }
2662 printf("(%s:%d:%d): selection failed;"
2663 " %d left in FIFO "
2664 "[intr %x, stat %x, step %d]\n",
2665 device_get_nameunit(sc->sc_dev),
2666 ecb->ccb->ccb_h.target_id,
2667 ecb->ccb->ccb_h.target_lun,
2668 NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF,
2669 sc->sc_espintr, sc->sc_espstat,
2670 sc->sc_espstep);
2671 NCRCMD(sc, NCRCMD_FLUSH);
2672 ncr53c9x_sched_msgout(SEND_ABORT);
2673 goto out;
2674
2675 case 2:
2676 /* Select stuck at Command Phase. */
2677 NCRCMD(sc, NCRCMD_FLUSH);
2678 break;
2679
2680 case 4:
2681 if (sc->sc_features & NCR_F_DMASELECT &&
2682 sc->sc_cmdlen != 0)
2683 printf("(%s:%d:%d): select; "
2684 "%lu left in DMA buffer "
2685 "[intr %x, stat %x, step %d]\n",
2686 device_get_nameunit(sc->sc_dev),
2687 ecb->ccb->ccb_h.target_id,
2688 ecb->ccb->ccb_h.target_lun,
2689 (u_long)sc->sc_cmdlen,
2690 sc->sc_espintr,
2691 sc->sc_espstat,
2692 sc->sc_espstep);
2693 /* So far, everything went fine. */
2694 break;
2695 }
2696
2697 sc->sc_prevphase = INVALID_PHASE; /* ??? */
2698 /* Do an implicit RESTORE POINTERS. */
2699 sc->sc_dp = ecb->daddr;
2700 sc->sc_dleft = ecb->dleft;
--- 4 unchanged lines hidden (view full) ---
2705 "select: [intr %x, stat %x, step %x]\n",
2706 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2707 NCRCMD(sc, NCRCMD_FLUSH);
2708 DELAY(1);
2709 goto reset;
2710 }
2711 if (sc->sc_state == NCR_IDLE) {
2712 device_printf(sc->sc_dev, "stray interrupt\n");
2713 goto out;
2714 }
2715 break;
2716
2717 case NCR_CONNECTED:
2718 if ((sc->sc_flags & NCR_ICCS) != 0) {
2719 /* "Initiate Command Complete Steps" in progress */
2720 sc->sc_flags &= ~NCR_ICCS;
2721
--- 88 unchanged lines hidden (view full) ---
2810 */
2811
2812 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
2813 ecb->cmd.cmd.opcode, ecb->clen));
2814 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2815 NCRCMD(sc, NCRCMD_FLUSH);
2816/* DELAY(1);*/
2817 }
2818 if (sc->sc_features & NCR_F_DMASELECT) {
2819 /* Setup DMA transfer for command. */
2820 size = ecb->clen;
2821 sc->sc_cmdlen = size;
2822 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
2823 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
2824 0, &size);
2825 /* Program the SCSI counter. */
--- 10 unchanged lines hidden (view full) ---
2836 NCRCMD(sc, NCRCMD_TRANS);
2837 }
2838 sc->sc_prevphase = COMMAND_PHASE;
2839 break;
2840
2841 case DATA_OUT_PHASE:
2842 NCR_PHASE(("DATA_OUT_PHASE [%ld] ", (long)sc->sc_dleft));
2843 NCRCMD(sc, NCRCMD_FLUSH);
2844 size = min(sc->sc_dleft, sc->sc_maxxfer);
2845 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size);
2846 sc->sc_prevphase = DATA_OUT_PHASE;
2847 goto setup_xfer;
2848
2849 case DATA_IN_PHASE:
2850 NCR_PHASE(("DATA_IN_PHASE "));
2851 if (sc->sc_rev == NCR_VARIANT_ESP100)
2852 NCRCMD(sc, NCRCMD_FLUSH);
2853 size = min(sc->sc_dleft, sc->sc_maxxfer);
2854 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size);
2855 sc->sc_prevphase = DATA_IN_PHASE;
2856 setup_xfer:
2857 /* Target returned to data phase: wipe "done" memory */
2858 ecb->flags &= ~ECB_TENTATIVE_DONE;
2859
2860 /* Program the SCSI counter. */
2861 NCR_SET_COUNT(sc, size);
--- 6 unchanged lines hidden (view full) ---
2868 * all the bytes we want but we're still in DATA PHASE.
2869 * Apparently, the device needs padding. Also, a
2870 * transfer size of 0 means "maximum" to the chip
2871 * DMA logic.
2872 */
2873 NCRCMD(sc,
2874 (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA);
2875 NCRDMA_GO(sc);
2876 goto out;
2877
2878 case STATUS_PHASE:
2879 NCR_PHASE(("STATUS_PHASE "));
2880 sc->sc_flags |= NCR_ICCS;
2881 NCRCMD(sc, NCRCMD_ICCS);
2882 sc->sc_prevphase = STATUS_PHASE;
2883 goto shortcut; /* i.e. expect status results soon */
2884
2885 case INVALID_PHASE:
2886 break;
2887
2888 default:
2889 device_printf(sc->sc_dev,
2890 "unexpected bus phase; resetting\n");
2891 goto reset;
2892 }
2893
2894out:
2895 mtx_unlock(&sc->sc_lock);
2896 return;
2897
2898reset:
2899 ncr53c9x_init(sc, 1);
2900 goto out;
2901
2902finish:
2903 ncr53c9x_done(sc, ecb);
2904 goto out;
2905
2906sched:
2907 sc->sc_state = NCR_IDLE;
2908 ncr53c9x_sched(sc);
2909 goto out;
2910
2911shortcut:
2912 /*
2913 * The idea is that many of the SCSI operations take very little
2914 * time, and going away and getting interrupted is too high an
2915 * overhead to pay. For example, selecting, sending a message
2916 * and command and then doing some work can be done in one "pass".
2917 *
--- 6 unchanged lines hidden (view full) ---
2924 wait.tv_sec++;
2925 wait.tv_usec -= 1000000;
2926 }
2927 do {
2928 if (NCRDMA_ISINTR(sc))
2929 goto again;
2930 microtime(&cur);
2931 } while (cur.tv_sec <= wait.tv_sec && cur.tv_usec <= wait.tv_usec);
2932 goto out;
2933}
2934
2935static void
2936ncr53c9x_abort(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
2937{
2938
2939 /* 2 secs for the abort */
2940 ecb->timeout = NCR_ABORT_TIMEOUT;
2941 ecb->flags |= ECB_ABORT;
2942
2943 if (ecb == sc->sc_nexus) {
2944 /*
2945 * If we're still selecting, the message will be scheduled
2946 * after selection is complete.
2947 */
2948 if (sc->sc_state == NCR_CONNECTED)
2949 ncr53c9x_sched_msgout(SEND_ABORT);
2950
2951 /*
2952 * Reschedule timeout.
2953 */
2954 ecb->ccb->ccb_h.timeout_ch =
2955 timeout(ncr53c9x_timeout, ecb, mstohz(ecb->timeout));
2956 } else {
2957 /*
2958 * Just leave the command where it is.
2959 * XXX - what choice do we have but to reset the SCSI
2960 * eventually?
2961 */
2962 if (sc->sc_state == NCR_IDLE)
2963 ncr53c9x_sched(sc);
2964 }
2965}
2966
2967static void
2968ncr53c9x_timeout(void *arg)
2969{
2970 struct ncr53c9x_ecb *ecb = arg;
2971 union ccb *ccb = ecb->ccb;
2972 struct ncr53c9x_softc *sc = ecb->sc;
2973 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
2974
2975 xpt_print_path(ccb->ccb_h.path);
2976 device_printf(sc->sc_dev, "timed out [ecb %p (flags 0x%x, dleft %x, "
2977 "stat %x)], <state %d, nexus %p, phase(l %x, c %x, p %x), "
2978 "resid %lx, msg(q %x,o %x) %s>",
2979 ecb, ecb->flags, ecb->dleft, ecb->stat,
2980 sc->sc_state, sc->sc_nexus,
2981 NCR_READ_REG(sc, NCR_STAT),
2982 sc->sc_phase, sc->sc_prevphase,
2983 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2984 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2985#if defined(NCR53C9X_DEBUG) && NCR53C9X_DEBUG > 1
2986 printf("TRACE: %s.", ecb->trace);
2987#endif
2988
2989 mtx_lock(&sc->sc_lock);
2990
2991 if (ecb->flags & ECB_ABORT) {
2992 /* Abort timed out. */
2993 printf(" AGAIN\n");
2994 ncr53c9x_init(sc, 1);
2995 } else {
2996 /* Abort the operation that has timed out. */
2997 printf("\n");
2998 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2999 ncr53c9x_abort(sc, ecb);
3000
3001 /* Disable sync mode if stuck in a data phase. */
3002 if (ecb == sc->sc_nexus &&
3003 (ti->flags & T_SYNCMODE) != 0 &&
3004 (sc->sc_phase & (MSGI|CDI)) == 0) {
3005 /* XXX ASYNC CALLBACK! */
3006 xpt_print_path(ccb->ccb_h.path);
3007 printf("sync negotiation disabled\n");
3008 sc->sc_cfflags |=
3009 (1 << ((ccb->ccb_h.target_id & 7) + 8));
3010 }
3011 }
3012
3013 mtx_unlock(&sc->sc_lock);
3014}
3015
3016static void
3017ncr53c9x_watch(void *arg)
3018{
3019 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
3020 struct ncr53c9x_linfo *li;
3021 struct ncr53c9x_tinfo *ti;
3022 time_t old;
3023 int t;
3024
3025 /* Delete any structures that have not been used in 10min. */
3026 old = time_second - (10 * 60);
3027
3028 mtx_lock(&sc->sc_lock);
3029 for (t = 0; t < sc->sc_ntarg; t++) {
3030 ti = &sc->sc_tinfo[t];
3031 li = LIST_FIRST(&ti->luns);
3032 while (li) {
3033 if (li->last_used < old &&
3034 li->untagged == NULL &&
3035 li->used == 0) {
3036 if (li->lun < NCR_NLUN)
3037 ti->lun[li->lun] = NULL;
3038 LIST_REMOVE(li, link);
3039 free(li, M_DEVBUF);
3040 /* Restart the search at the beginning. */
3041 li = LIST_FIRST(&ti->luns);
3042 continue;
3043 }
3044 li = LIST_NEXT(li, link);
3045 }
3046 }
3047 mtx_unlock(&sc->sc_lock);
3048 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);
3049}
2 * Copyright (c) 2004 Scott Long
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
--- 9 unchanged lines hidden (view full) ---
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28/* $NetBSD: ncr53c9x.c,v 1.114 2005/02/27 00:27:02 perry Exp $ */
29
30/*-
31 * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
32 * All rights reserved.
33 *
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Charles M. Hannum.
36 *
--- 62 unchanged lines hidden (view full) ---
99 * Based on aic6360 by Jarle Greipsland
100 *
101 * Acknowledgements: Many of the algorithms used in this driver are
102 * inspired by the work of Julian Elischer (julian@FreeBSD.org) and
103 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million!
104 */
105
106#include <sys/cdefs.h>
107__FBSDID("$FreeBSD: head/sys/dev/esp/ncr53c9x.c 180692 2008-07-22 13:51:21Z marius $");
108
109#include <sys/param.h>
110#include <sys/systm.h>
111#include <sys/bus.h>
112#include <sys/kernel.h>
113#include <sys/malloc.h>
114#include <sys/resource.h>
115#include <sys/lock.h>
116#include <sys/mutex.h>
117#include <sys/queue.h>
118#include <sys/time.h>
119#include <sys/callout.h>
120
121#include <cam/cam.h>
122#include <cam/cam_ccb.h>
123#include <cam/cam_debug.h>
124#include <cam/cam_sim.h>
125#include <cam/cam_xpt_sim.h>
126#include <cam/scsi/scsi_all.h>
127#include <cam/scsi/scsi_message.h>
128
129#include <dev/esp/ncr53c9xreg.h>
130#include <dev/esp/ncr53c9xvar.h>
131
132int ncr53c9x_debug =
133 NCR_SHOWMISC /* | NCR_SHOWPHASE | NCR_SHOWTRAC | NCR_SHOWCMDS */;
134#ifdef DEBUG
135int ncr53c9x_notag = 0;
136#endif
137
138static void ncr53c9x_abort(struct ncr53c9x_softc *sc,
139 struct ncr53c9x_ecb *ecb);
140static void ncr53c9x_dequeue(struct ncr53c9x_softc *sc,
141 struct ncr53c9x_ecb *ecb);
142static void ncr53c9x_done(struct ncr53c9x_softc *sc,
143 struct ncr53c9x_ecb *ecb);
144static void ncr53c9x_free_ecb(struct ncr53c9x_softc *sc,
145 struct ncr53c9x_ecb *ecb);
146static void ncr53c9x_msgin(struct ncr53c9x_softc *sc);
147static void ncr53c9x_msgout(struct ncr53c9x_softc *sc);
148static void ncr53c9x_poll(struct cam_sim *sim);
149static int ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how);
150static int ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message,
151 int tagtype, int tagid);
152static void ncr53c9x_sense(struct ncr53c9x_softc *sc,
153 struct ncr53c9x_ecb *ecb);
154static void ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc);
155static void ncr53c9x_sched(struct ncr53c9x_softc *sc);
156static void ncr53c9x_select(struct ncr53c9x_softc *sc,
157 struct ncr53c9x_ecb *ecb);
158static void ncr53c9x_timeout(void *arg);
159static void ncr53c9x_watch(void *arg);
160static void ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p,
161 int len);
162
163static struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *sc);
164static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *sc,
165 int64_t lun);
166
--- 66 unchanged lines hidden (view full) ---
233ncr53c9x_attach(struct ncr53c9x_softc *sc)
234{
235 struct cam_devq *devq;
236 struct cam_sim *sim;
237 struct cam_path *path;
238 struct ncr53c9x_ecb *ecb;
239 int error, i;
240
241 mtx_init(&sc->sc_lock, "ncr", "ncr53c9x lock", MTX_DEF);
242
243 /*
244 * Note, the front-end has set us up to print the chip variation.
245 */
246 if (sc->sc_rev >= NCR_VARIANT_MAX) {
247 device_printf(sc->sc_dev, "unknown variant %d, devices not "
248 "attached\n", sc->sc_rev);
249 return (EINVAL);
250 }
--- 36 unchanged lines hidden (view full) ---
287 M_DEVBUF, M_NOWAIT | M_ZERO);
288 if (sc->sc_tinfo == NULL) {
289 device_printf(sc->sc_dev,
290 "cannot allocate target info buffer\n");
291 error = ENOMEM;
292 goto fail_imess;
293 }
294
295 callout_init(&sc->sc_watchdog, 0);
296
297 /*
298 * Treat NCR53C90 with the 86C01 DMA chip exactly as ESP100
299 * from now on.
300 */
301 if (sc->sc_rev == NCR_VARIANT_NCR53C90_86C01)
302 sc->sc_rev = NCR_VARIANT_ESP100;
303
304 sc->sc_ccf = FREQTOCCF(sc->sc_freq);
--- 24 unchanged lines hidden (view full) ---
329 devq = cam_simq_alloc(sc->sc_ntarg);
330 if (devq == NULL) {
331 device_printf(sc->sc_dev, "cannot allocate device queue\n");
332 error = ENOMEM;
333 goto fail_tinfo;
334 }
335
336 sim = cam_sim_alloc(ncr53c9x_action, ncr53c9x_poll, "esp", sc,
337 device_get_unit(sc->sc_dev), &Giant, 1, NCR_TAG_DEPTH, devq);
338 if (sim == NULL) {
339 device_printf(sc->sc_dev, "cannot allocate SIM entry\n");
340 error = ENOMEM;
341 goto fail_devq;
342 }
343 if (xpt_bus_register(sim, sc->sc_dev, 0) != CAM_SUCCESS) {
344 device_printf(sc->sc_dev, "cannot register bus\n");
345 error = EIO;
346 goto fail_sim;
347 }
348
349 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
350 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
351 device_printf(sc->sc_dev, "cannot create path\n");
352 error = EIO;
353 goto fail_bus;
354 }
355
356 sc->sc_sim = sim;
357 sc->sc_path = path;
358
359 /* Reset state and bus. */
360#if 0
361 sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
362#else
363 sc->sc_cfflags = 0;
364#endif
365 sc->sc_state = 0;
366 ncr53c9x_init(sc, 1);
367
368 TAILQ_INIT(&sc->free_list);
369 if ((sc->ecb_array =
370 malloc(sizeof(struct ncr53c9x_ecb) * NCR_TAG_DEPTH, M_DEVBUF,
371 M_NOWAIT | M_ZERO)) == NULL) {
372 device_printf(sc->sc_dev, "cannot allocate ECB array\n");
373 error = ENOMEM;
374 goto fail_path;
375 }
376 for (i = 0; i < NCR_TAG_DEPTH; i++) {
377 ecb = &sc->ecb_array[i];
378 ecb->sc = sc;
379 ecb->tag_id = i;
380 TAILQ_INSERT_HEAD(&sc->free_list, ecb, free_links);
381 }
382
383 callout_reset(&sc->sc_watchdog, 60*hz, ncr53c9x_watch, sc);
384
385 return (0);
386
387fail_path:
388 xpt_free_path(path);
389fail_bus:
390 xpt_bus_deregister(cam_sim_path(sim));
391fail_sim:
392 cam_sim_free(sim, TRUE);
393fail_devq:
394 cam_simq_free(devq);
395fail_tinfo:
396 free(sc->sc_tinfo, M_DEVBUF);
397fail_imess:
398 if (sc->sc_imess_self)
399 free(sc->sc_imess, M_DEVBUF);
400fail_omess:
401 if (sc->sc_omess_self)
402 free(sc->sc_omess, M_DEVBUF);
403 return (error);
404}
405
406int
407ncr53c9x_detach(struct ncr53c9x_softc *sc)
408{
409
410 callout_drain(&sc->sc_watchdog);
411 mtx_lock(&sc->sc_lock);
412 ncr53c9x_init(sc, 1);
413 mtx_unlock(&sc->sc_lock);
414 xpt_free_path(sc->sc_path);
415 xpt_bus_deregister(cam_sim_path(sc->sc_sim));
416 cam_sim_free(sc->sc_sim, TRUE);
417 free(sc->ecb_array, M_DEVBUF);
418 free(sc->sc_tinfo, M_DEVBUF);
419 if (sc->sc_imess_self)
420 free(sc->sc_imess, M_DEVBUF);
421 if (sc->sc_omess_self)
422 free(sc->sc_omess, M_DEVBUF);
423 mtx_destroy(&sc->sc_lock);
424
425 return (0);
426}
427
428/*
429 * This is the generic ncr53c9x reset function. It does not reset the SCSI
430 * bus, only this controller, but kills any on-going commands, and also stops
431 * and resets the DMA.
432 *
433 * After reset, registers are loaded with the defaults from the attach
434 * routine above.
435 */
436void
437ncr53c9x_reset(struct ncr53c9x_softc *sc)
438{
439
440 /* Reset DMA first. */
441 NCRDMA_RESET(sc);
442
443 /* Reset SCSI chip. */
444 NCRCMD(sc, NCRCMD_RSTCHIP);
445 NCRCMD(sc, NCRCMD_NOP);
446 DELAY(500);
447
--- 24 unchanged lines hidden (view full) ---
472 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
473 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
474 break;
475
476 case NCR_VARIANT_FAS366:
477 sc->sc_features |=
478 NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3;
479 sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO;
480 sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI;
481 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
482 sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE | NCRCFG2_HME32 */
483 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
484 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
485 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
486 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
487 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
488 break;
489
490 default:
--- 13 unchanged lines hidden (view full) ---
504 sc->sc_rev);
505 device_printf(sc->sc_dev, "ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, "
506 "cfg3 0x%x, ccf 0x%x, timeout 0x%x\n",
507 sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3, sc->sc_ccf, sc->sc_timeout);
508#endif
509}
510
511/*
512 * Reset the SCSI bus, but not the chip.
513 */
514static void
515ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc)
516{
517
518 (*sc->sc_glue->gl_dma_stop)(sc);
519
520 NCR_MISC(("%s: resetting SCSI bus\n", device_get_nameunit(sc->sc_dev)));
521 NCRCMD(sc, NCRCMD_RSTSCSI);
522 DELAY(250000); /* Give the bus a fighting chance to settle */
523}
524
525/*
526 * Initialize ncr53c9x state machine.
527 */
528void
529ncr53c9x_init(struct ncr53c9x_softc *sc, int doreset)
530{
531 struct ncr53c9x_ecb *ecb;
532 struct ncr53c9x_linfo *li;
533 struct ncr53c9x_tinfo *ti;
534 int i, r;
535
536 NCR_MISC(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state));
537
538 if (sc->sc_state == 0) {
539 /* First time through; initialize. */
540
541 TAILQ_INIT(&sc->ready_list);
542 sc->sc_nexus = NULL;
543 memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
544 for (r = 0; r < sc->sc_ntarg; r++) {
545 LIST_INIT(&sc->sc_tinfo[r].luns);
546 }
547 } else {
548 /* Cancel any active commands. */
549 sc->sc_state = NCR_CLEANING;
550 sc->sc_msgify = 0;
551 if ((ecb = sc->sc_nexus) != NULL) {
552 ecb->ccb->ccb_h.status = CAM_CMD_TIMEOUT;
553 ncr53c9x_done(sc, ecb);
554 }
555 /* Cancel outstanding disconnected commands on each LUN. */
556 for (r = 0; r < sc->sc_ntarg; r++) {
557 LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) {
558 if ((ecb = li->untagged) != NULL) {
559 li->untagged = NULL;
560 /*
561 * XXX
562 *
563 * Should we terminate a command
564 * that never reached the disk?
565 */
566 li->busy = 0;
567 ecb->ccb->ccb_h.status =
568 CAM_CMD_TIMEOUT;
569 ncr53c9x_done(sc, ecb);
570 }
571 for (i = 0; i < 256; i++)
572 if ((ecb = li->queued[i])) {
573 li->queued[i] = NULL;
574 ecb->ccb->ccb_h.status =
575 CAM_CMD_TIMEOUT;
576 ncr53c9x_done(sc, ecb);
577 }
578 li->used = 0;
579 }
580 }
581 }
582
583 /*
584 * Reset the chip to a known state.
585 */
586 ncr53c9x_reset(sc);
587
588 sc->sc_flags = 0;
589 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
590 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
591
592 for (r = 0; r < sc->sc_ntarg; r++) {
593 ti = &sc->sc_tinfo[r];
594/* XXX - config flags per target: low bits: no reselect; high bits: no synch */
595 ti->flags = ((sc->sc_minsync != 0 &&
596 (sc->sc_cfflags & (1 << ((r & 7) + 8))) == 0) ?
597 0 : T_SYNCHOFF) |
598 ((sc->sc_cfflags & (1 << (r & 7))) == 0 ?
599 0 : T_RSELECTOFF);
600
601#ifdef DEBUG
602 if (ncr53c9x_notag)
603 ti->flags &= ~T_TAG;
604#endif
605 ti->period = sc->sc_minsync;
606 ti->offset = 0;
607 ti->cfg3 = 0;
608 }
609
610 if (doreset) {
611 sc->sc_state = NCR_SBR;
612 NCRCMD(sc, NCRCMD_RSTSCSI);
613 } else {
614 sc->sc_state = NCR_IDLE;
615 ncr53c9x_sched(sc);
616 }
617}
618
619/*
620 * Read the NCR registers, and save their contents for later use.
621 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
622 * NCR_INTR - so make sure it is the last read.
623 *
624 * I think that (from reading the docs) most bits in these registers
625 * only make sense when the DMA CSR has an interrupt showing. Call only
626 * if an interrupt is pending.
627 */
628static inline void
629ncr53c9x_readregs(struct ncr53c9x_softc *sc)
630{
631
632 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
633 /* Only the step bits are of interest. */
634 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
635
636 if (sc->sc_rev == NCR_VARIANT_FAS366)
637 sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2);
638
639 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
--- 14 unchanged lines hidden (view full) ---
654
655/*
656 * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
657 */
658static inline int
659ncr53c9x_stp2cpb(struct ncr53c9x_softc *sc, int period)
660{
661 int v;
662 v = (sc->sc_freq * period) / 250;
663 if (ncr53c9x_cpb2stp(sc, v) < period)
664 /* Correct round-down error. */
665 v++;
666 return (v);
667}
668
669static inline void
670ncr53c9x_setsync(struct ncr53c9x_softc *sc, struct ncr53c9x_tinfo *ti)
671{
672 u_char syncoff, synctp;
673 u_char cfg3 = sc->sc_cfg3 | ti->cfg3;
674
675 if (ti->flags & T_SYNCMODE) {
676 syncoff = ti->offset;
677 synctp = ncr53c9x_stp2cpb(sc, ti->period);
678 if (sc->sc_features & NCR_F_FASTSCSI) {
679 /*
680 * If the period is 200ns or less (ti->period <= 50),
681 * put the chip in Fast SCSI mode.
682 */
683 if (ti->period <= 50)
684 /*
685 * There are (at least) 4 variations of the
686 * configuration 3 register. The drive attach
687 * routine sets the appropriate bit to put the
688 * chip into Fast SCSI mode so that it doesn't
689 * have to be figured out here each time.
690 */
691 cfg3 |= sc->sc_cfg3_fscsi;
--- 6 unchanged lines hidden (view full) ---
698 if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
699 (cfg3 & NCRAMDCFG3_FSCSI) == 0)
700 synctp--;
701 } else {
702 syncoff = 0;
703 synctp = 0;
704 }
705
706 if (sc->sc_features & NCR_F_HASCFG3)
707 NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
708
709 NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
710 NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
711}
712
713/*
--- 8 unchanged lines hidden (view full) ---
722{
723 struct ncr53c9x_tinfo *ti;
724 u_char *cmd;
725 size_t dmasize;
726 int clen, selatn3, selatns;
727 int lun = ecb->ccb->ccb_h.target_lun;
728 int target = ecb->ccb->ccb_h.target_id;
729
730 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ",
731 target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1]));
732
733 ti = &sc->sc_tinfo[target];
734 sc->sc_state = NCR_SELECTING;
735 /*
736 * Schedule the timeout now, the first time we will go away
737 * expecting to come back due to an interrupt, because it is
738 * always possible that the interrupt may never happen.
739 */
740 ecb->ccb->ccb_h.timeout_ch =
741 timeout(ncr53c9x_timeout, ecb, mstohz(ecb->timeout));
742
743 /*
744 * The docs say the target register is never reset, and I
745 * can't think of a better place to set it.
746 */
747 if (sc->sc_rev == NCR_VARIANT_FAS366) {
748 NCRCMD(sc, NCRCMD_FLUSH);
749 NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME);
750 } else {
751 NCR_WRITE_REG(sc, NCR_SELID, target);
752 }
753 ncr53c9x_setsync(sc, ti);
754
755 if ((ecb->flags & ECB_SENSE) != 0) {
756 /*
757 * For REQUEST SENSE, we should not send an IDENTIFY or
758 * otherwise mangle the target. There should be no MESSAGE IN
759 * phase.
760 */
761 if (sc->sc_features & NCR_F_DMASELECT) {
762 /* setup DMA transfer for command */
763 dmasize = clen = ecb->clen;
764 sc->sc_cmdlen = clen;
765 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
766
767 /* Program the SCSI counter */
768 NCR_SET_COUNT(sc, dmasize);
769
770 if (sc->sc_rev != NCR_VARIANT_FAS366)
771 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
772
773 /* And get the target's attention */
774 NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
775 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0,
776 &dmasize);
777 NCRDMA_GO(sc);
778 } else {
779 ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
780 NCRCMD(sc, NCRCMD_SELNATN);
781 }
782 return;
783 }
784
785 selatn3 = selatns = 0;
786 if (ecb->tag[0] != 0) {
787 if (sc->sc_features & NCR_F_SELATN3)
788 /* Use SELATN3 to send tag messages. */
789 selatn3 = 1;
790 else
791 /* We don't have SELATN3; use SELATNS to send tags. */
792 selatns = 1;
793 }
794
795 if (ti->flags & T_NEGOTIATE) {
796 /* We have to use SELATNS to send sync/wide messages. */
797 selatn3 = 0;
798 selatns = 1;
799 }
800
801 cmd = (u_char *)&ecb->cmd.cmd;
802
803 if (selatn3) {
--- 10 unchanged lines hidden (view full) ---
814 cmd[0] = MSG_IDENTIFY(lun, (ti->flags & T_RSELECTOFF) == 0);
815 }
816
817 if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) {
818 /* Setup DMA transfer for command. */
819 dmasize = clen;
820 sc->sc_cmdlen = clen;
821 sc->sc_cmdp = cmd;
822
823 /* Program the SCSI counter. */
824 NCR_SET_COUNT(sc, dmasize);
825
826 /* Load the count in. */
827 /* if (sc->sc_rev != NCR_VARIANT_FAS366) */
828 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
829
830 /* And get the target's attention. */
831 if (selatn3) {
832 sc->sc_msgout = SEND_TAG;
833 sc->sc_flags |= NCR_ATN;
834 NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA);
835 } else
836 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
837 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
838 NCRDMA_GO(sc);
839 return;
840 }
841
842 /*
843 * Who am I? This is where we tell the target that we are
844 * happy for it to disconnect etc.
845 */
--- 13 unchanged lines hidden (view full) ---
859 } else
860 NCRCMD(sc, NCRCMD_SELATN);
861}
862
863static void
864ncr53c9x_free_ecb(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
865{
866
867 ecb->flags = 0;
868 TAILQ_INSERT_TAIL(&sc->free_list, ecb, free_links);
869}
870
871static struct ncr53c9x_ecb *
872ncr53c9x_get_ecb(struct ncr53c9x_softc *sc)
873{
874 struct ncr53c9x_ecb *ecb;
875
876 ecb = TAILQ_FIRST(&sc->free_list);
877 if (ecb) {
878 if (ecb->flags != 0)
879 panic("ecb flags not cleared\n");
880 TAILQ_REMOVE(&sc->free_list, ecb, free_links);
881 ecb->flags = ECB_ALLOC;
882 bzero(&ecb->ccb, sizeof(struct ncr53c9x_ecb) -
883 offsetof(struct ncr53c9x_ecb, ccb));
--- 6 unchanged lines hidden (view full) ---
890 */
891
892/*
893 * Start a SCSI-command.
894 * This function is called by the higher level SCSI-driver to queue/run
895 * SCSI-commands.
896 */
897
898void
899ncr53c9x_action(struct cam_sim *sim, union ccb *ccb)
900{
901 struct ccb_pathinq *cpi;
902 struct ccb_scsiio *csio;
903 struct ccb_trans_settings *cts;
904 struct ccb_trans_settings_scsi *scsi;
905 struct ccb_trans_settings_spi *spi;
906 struct ncr53c9x_ecb *ecb;
907 struct ncr53c9x_softc *sc;
908 struct ncr53c9x_tinfo *ti;
909 int target;
910
911 NCR_TRACE(("[ncr53c9x_action %d]", ccb->ccb_h.func_code));
912
913 sc = cam_sim_softc(sim);
914 mtx_lock(&sc->sc_lock);
915
916 switch (ccb->ccb_h.func_code) {
917 case XPT_RESET_BUS:
918 ncr53c9x_scsi_reset(sc);
919 ccb->ccb_h.status = CAM_REQ_CMP;
920 mtx_unlock(&sc->sc_lock);
921 xpt_done(ccb);
922 return;
923
924 case XPT_CALC_GEOMETRY:
925 mtx_unlock(&sc->sc_lock);
926 cam_calc_geometry(&ccb->ccg, sc->sc_extended_geom);
927 xpt_done(ccb);
928 return;
929
930 case XPT_PATH_INQ:
931 cpi = &ccb->cpi;
932 cpi->version_num = 1;
933 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
934 cpi->hba_inquiry |=
935 (sc->sc_rev == NCR_VARIANT_FAS366) ? PI_WIDE_16 : 0;
936 cpi->target_sprt = 0;
937 cpi->hba_misc = 0;
938 cpi->hba_eng_cnt = 0;
939 cpi->max_target = sc->sc_ntarg - 1;
940 cpi->max_lun = 8;
941 cpi->initiator_id = sc->sc_id;
942 cpi->bus_id = 0;
943 cpi->base_transfer_speed = 3300;
944 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
945 strncpy(cpi->hba_vid, "Sun", HBA_IDLEN);
946 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
947 cpi->unit_number = cam_sim_unit(sim);
948 cpi->transport = XPORT_SPI;
949 cpi->transport_version = 2;
950 cpi->protocol = PROTO_SCSI;
951 cpi->protocol_version = SCSI_REV_2;
952 ccb->ccb_h.status = CAM_REQ_CMP;
953 mtx_unlock(&sc->sc_lock);
954 xpt_done(ccb);
955 return;
956
957 case XPT_GET_TRAN_SETTINGS:
958 cts = &ccb->cts;
959 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
960 scsi = &cts->proto_specific.scsi;
961 spi = &cts->xport_specific.spi;
962
963 cts->protocol = PROTO_SCSI;
964 cts->protocol_version = SCSI_REV_2;
965 cts->transport = XPORT_SPI;
966 cts->transport_version = 2;
967
968 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
969 spi->sync_period = ti->period;
970 spi->sync_offset = ti->offset;
971 spi->bus_width = ti->width;
972 if ((ti->flags & T_TAG) != 0) {
973 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
974 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
975 } else {
976 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
977 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
978 }
979 } else {
980 spi->sync_period = sc->sc_maxsync;
981 spi->sync_offset = sc->sc_maxoffset;
982 spi->bus_width = sc->sc_maxwidth;
983 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
984 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
985 }
986 spi->valid =
987 CTS_SPI_VALID_BUS_WIDTH |
988 CTS_SPI_VALID_SYNC_RATE |
989 CTS_SPI_VALID_SYNC_OFFSET |
990 CTS_SPI_VALID_DISC;
991 scsi->valid = CTS_SCSI_VALID_TQ;
992 ccb->ccb_h.status = CAM_REQ_CMP;
993 mtx_unlock(&sc->sc_lock);
994 xpt_done(ccb);
995 return;
996
997 case XPT_ABORT:
998 printf("XPT_ABORT called\n");
999 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1000 mtx_unlock(&sc->sc_lock);
1001 xpt_done(ccb);
1002 return;
1003
1004 case XPT_TERM_IO:
1005 printf("XPT_TERM_IO called\n");
1006 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1007 mtx_unlock(&sc->sc_lock);
1008 xpt_done(ccb);
1009 return;
1010
1011 case XPT_RESET_DEV:
1012 printf("XPT_RESET_DEV called\n");
1013 case XPT_SCSI_IO:
1014 if (ccb->ccb_h.target_id < 0 ||
1015 ccb->ccb_h.target_id >= sc->sc_ntarg) {
1016 ccb->ccb_h.status = CAM_PATH_INVALID;
1017 mtx_unlock(&sc->sc_lock);
1018 xpt_done(ccb);
1019 return;
1020 }
1021 /* Get an ECB to use. */
1022 ecb = ncr53c9x_get_ecb(sc);
1023 /*
1024 * This should never happen as we track resources
1025 * in the mid-layer.
1026 */
1027 if (ecb == NULL) {
1028 xpt_freeze_simq(sim, 1);
1029 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1030 printf("unable to allocate ecb\n");
1031 mtx_unlock(&sc->sc_lock);
1032 xpt_done(ccb);
1033 return;
1034 }
1035
1036 /* Initialize ecb. */
1037 ecb->ccb = ccb;
1038 ecb->timeout = ccb->ccb_h.timeout;
1039
--- 34 unchanged lines hidden (view full) ---
1074 NCR_MISC(("%s: target %d: tagged queuing\n",
1075 device_get_nameunit(sc->sc_dev), target));
1076 ti->flags |= T_TAG;
1077 } else
1078 ti->flags &= ~T_TAG;
1079 }
1080
1081 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
1082 if (spi->bus_width != 0) {
1083 NCR_MISC(("%s: target %d: wide negotiation\n",
1084 device_get_nameunit(sc->sc_dev), target));
1085 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1086 ti->flags |= T_WIDE;
1087 ti->width = 1;
1088 }
1089 } else {
1090 ti->flags &= ~T_WIDE;
1091 ti->width = 0;
1092 }
1093 ti->flags |= T_NEGOTIATE;
1094 }
1095
1096 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
1097 NCR_MISC(("%s: target %d: sync period negotiation\n",
1098 device_get_nameunit(sc->sc_dev), target));
1099 ti->flags |= T_NEGOTIATE;
1100 ti->period = spi->sync_period;
1101 }
1102
1103 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
1104 NCR_MISC(("%s: target %d: sync offset negotiation\n",
1105 device_get_nameunit(sc->sc_dev), target));
1106 ti->flags |= T_NEGOTIATE;
1107 ti->offset = spi->sync_offset;
1108 }
1109
1110 mtx_unlock(&sc->sc_lock);
1111 ccb->ccb_h.status = CAM_REQ_CMP;
1112 xpt_done(ccb);
1113 return;
1114
1115 default:
1116 device_printf(sc->sc_dev, "Unhandled function code %d\n",
1117 ccb->ccb_h.func_code);
1118 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1119 mtx_unlock(&sc->sc_lock);
1120 xpt_done(ccb);
1121 return;
1122 }
1123
1124 mtx_unlock(&sc->sc_lock);
1125}
1126
1127/*
1128 * Used when interrupt driven I/O is not allowed, e.g. during boot.
1129 */
1130static void
1131ncr53c9x_poll(struct cam_sim *sim)
1132{
1133 struct ncr53c9x_softc *sc;
1134
1135 NCR_TRACE(("[ncr53c9x_poll] "));
1136 sc = cam_sim_softc(sim);
1137 if (NCRDMA_ISINTR(sc)) {
1138 ncr53c9x_intr(sc);
1139 }
1140}
1141
1142/*
1143 * LOW LEVEL SCSI UTILITIES
1144 */
1145
1146/*
1147 * Schedule a SCSI operation. This has now been pulled out of the interrupt
1148 * handler so that we may call it from ncr53c9x_scsipi_request and
1149 * ncr53c9x_done. This may save us an unnecessary interrupt just to get
1150 * things going. Should only be called when state == NCR_IDLE and at bio pl.
1151 */
1152static void
1153ncr53c9x_sched(struct ncr53c9x_softc *sc)
1154{
1155 struct ncr53c9x_ecb *ecb;
1156 struct ncr53c9x_linfo *li;
1157 struct ncr53c9x_tinfo *ti;
1158 int lun, tag;
1159
1160 NCR_TRACE(("[ncr53c9x_sched] "));
1161
1162 if (sc->sc_state != NCR_IDLE)
1163 panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
1164
1165 /*
1166 * Find first ecb in ready queue that is for a target/lunit
1167 * combinations that is not busy.
1168 */
1169 for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
1170 ecb = TAILQ_NEXT(ecb, chain)) {
1171 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1172 lun = ecb->ccb->ccb_h.target_lun;
1173
1174 /* Select type of tag for this command */
1175 if ((ti->flags & (T_RSELECTOFF)) != 0)
1176 tag = 0;
1177 else if ((ti->flags & (T_TAG)) == 0)
1178 tag = 0;
1179 else if ((ecb->flags & ECB_SENSE) != 0)
1180 tag = 0;
1181 else if ((ecb->ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0)
1182 tag = 0;
1183 else if (ecb->ccb->csio.tag_action == CAM_TAG_ACTION_NONE)
1184 tag = 0;
1185 else
1186 tag = ecb->ccb->csio.tag_action;
--- 62 unchanged lines hidden (view full) ---
1249ncr53c9x_sense(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1250{
1251 union ccb *ccb = ecb->ccb;
1252 struct ncr53c9x_linfo *li;
1253 struct ncr53c9x_tinfo *ti;
1254 struct scsi_request_sense *ss = (void *)&ecb->cmd.cmd;
1255 int lun;
1256
1257 NCR_TRACE(("requesting sense "));
1258
1259 lun = ccb->ccb_h.target_lun;
1260 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
1261
1262 /* Next, setup a REQUEST SENSE command block. */
1263 memset(ss, 0, sizeof(*ss));
1264 ss->opcode = REQUEST_SENSE;
--- 27 unchanged lines hidden (view full) ---
1292static void
1293ncr53c9x_done(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1294{
1295 union ccb *ccb = ecb->ccb;
1296 struct ncr53c9x_linfo *li;
1297 struct ncr53c9x_tinfo *ti;
1298 int lun;
1299
1300 NCR_TRACE(("[ncr53c9x_done(status:%x)] ", ccb->ccb_h.status));
1301
1302 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
1303 lun = ccb->ccb_h.target_lun;
1304 li = TINFO_LUN(ti, lun);
1305
1306 untimeout(ncr53c9x_timeout, ecb, ccb->ccb_h.timeout_ch);
1307
1308 /*
1309 * Now, if we've come here with no error code, i.e. we've kept the
1310 * initial XS_NOERROR, and the status code signals that we should
1311 * check sense, we'll need to set up a request sense cmd block and
1312 * push the command back into the ready queue *before* any other
1313 * commands for this target/lunit, else we lose the sense info.
1314 * We don't support chk sense conditions for the request sense cmd.
1315 */
1316 if (ccb->ccb_h.status == CAM_REQ_CMP) {
1317 if ((ecb->flags & ECB_ABORT) != 0) {
1318 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1319 } else if ((ecb->flags & ECB_SENSE) != 0 &&
1320 (ecb->stat != SCSI_STATUS_CHECK_COND)) {
1321 ccb->ccb_h.status = CAM_AUTOSNS_VALID;
1322 } else if (ecb->stat == SCSI_STATUS_CHECK_COND) {
1323 if ((ecb->flags & ECB_SENSE) != 0)
1324 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1325 else {
1326 /* First, save the return values. */
1327 ccb->csio.resid = ecb->dleft;
1328 ncr53c9x_sense(sc, ecb);
1329 return;
1330 }
1331 } else {
1332 ccb->csio.resid = ecb->dleft;
1333 }
1334#if 0
1335 if (xs->status == SCSI_QUEUE_FULL || xs->status == XS_BUSY)
1336 xs->error = XS_BUSY;
1337#endif
1338 }
1339
1340#ifdef NCR53C9X_DEBUG
1341 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1342 if (ccb->csio.resid != 0)
1343 printf("resid=%d ", ccb->csio.resid);
1344#if 0
1345 if (xs->error == XS_SENSE)
1346 printf("sense=0x%02x\n",
1347 xs->sense.scsi_sense.error_code);
1348 else
1349 printf("error=%d\n", xs->error);
1350#endif
1351 }
1352#endif
1353
1354 /*
1355 * Remove the ECB from whatever queue it's on.
1356 */
1357 ncr53c9x_dequeue(sc, ecb);
1358 if (ecb == sc->sc_nexus) {
--- 21 unchanged lines hidden (view full) ---
1380
1381static void
1382ncr53c9x_dequeue(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1383{
1384 struct ncr53c9x_linfo *li;
1385 struct ncr53c9x_tinfo *ti;
1386 int64_t lun;
1387
1388 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1389 lun = ecb->ccb->ccb_h.target_lun;
1390 li = TINFO_LUN(ti, lun);
1391#ifdef DIAGNOSTIC
1392 if (li == NULL || li->lun != lun)
1393 panic("ncr53c9x_dequeue: lun %qx for ecb %p does not exist",
1394 (long long)lun, ecb);
1395#endif
--- 7 unchanged lines hidden (view full) ---
1403 (li->queued[ecb->tag[1]] != ecb))
1404 panic("ncr53c9x_dequeue: slot %d for lun %qx has %p "
1405 "instead of ecb %p\n", ecb->tag[1],
1406 (long long)lun, li->queued[ecb->tag[1]], ecb);
1407#endif
1408 li->queued[ecb->tag[1]] = NULL;
1409 li->used--;
1410 }
1411
1412 if ((ecb->flags & ECB_READY) != 0) {
1413 ecb->flags &= ~ECB_READY;
1414 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1415 }
1416}
1417
1418/*
--- 10 unchanged lines hidden (view full) ---
1429 NCRCMD(sc, NCRCMD_SETATN); \
1430 sc->sc_flags |= NCR_ATN; \
1431 sc->sc_msgpriq |= (m); \
1432} while (0)
1433
1434static void
1435ncr53c9x_flushfifo(struct ncr53c9x_softc *sc)
1436{
1437 NCR_TRACE(("[flushfifo] "));
1438
1439 NCRCMD(sc, NCRCMD_FLUSH);
1440
1441 if (sc->sc_phase == COMMAND_PHASE ||
1442 sc->sc_phase == MESSAGE_OUT_PHASE)
1443 DELAY(2);
1444}
1445
1446static int
1447ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how)
1448{
1449 u_char *ibuf;
1450 int i, n;
1451
1452 switch (how) {
1453 case NCR_RDFIFO_START:
1454 ibuf = sc->sc_imess;
1455 sc->sc_imlen = 0;
1456 break;
1457
1458 case NCR_RDFIFO_CONTINUE:
1459 ibuf = sc->sc_imess + sc->sc_imlen;
--- 46 unchanged lines hidden (view full) ---
1506 return (sc->sc_imlen);
1507}
1508
1509static void
1510ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p, int len)
1511{
1512 int i;
1513
1514#ifdef NCR53C9X_DEBUG
1515 NCR_MSGS(("[wrfifo(%d):", len));
1516 if (ncr53c9x_debug & NCR_SHOWMSGS) {
1517 for (i = 0; i < len; i++)
1518 printf(" %02x", p[i]);
1519 printf("]\n");
1520 }
1521#endif
--- 10 unchanged lines hidden (view full) ---
1532ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message, int tagtype,
1533 int tagid)
1534{
1535 struct ncr53c9x_ecb *ecb = NULL;
1536 struct ncr53c9x_linfo *li;
1537 struct ncr53c9x_tinfo *ti;
1538 u_char lun, selid, target;
1539
1540
1541 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1542 target = sc->sc_selid;
1543 } else {
1544 /*
1545 * The SCSI chip made a snapshot of the data bus
1546 * while the reselection was being negotiated.
1547 * This enables us to determine which target did
--- 98 unchanged lines hidden (view full) ---
1646ncr53c9x_msgin(struct ncr53c9x_softc *sc)
1647{
1648 struct ncr53c9x_ecb *ecb;
1649 struct ncr53c9x_linfo *li;
1650 struct ncr53c9x_tinfo *ti;
1651 u_char *pb;
1652 int lun, plen;
1653
1654 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
1655
1656 if (sc->sc_imlen == 0) {
1657 device_printf(sc->sc_dev, "msgin: no msg byte available\n");
1658 return;
1659 }
1660
1661 /*
--- 11 unchanged lines hidden (view full) ---
1673 }
1674
1675 /*
1676 * If we're going to reject the message, don't bother storing
1677 * the incoming bytes. But still, we need to ACK them.
1678 */
1679 if ((sc->sc_flags & NCR_DROP_MSGI) != 0) {
1680 NCRCMD(sc, NCRCMD_MSGOK);
1681 printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]);
1682 return;
1683 }
1684
1685 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
1686 ncr53c9x_sched_msgout(SEND_REJECT);
1687 sc->sc_flags |= NCR_DROP_MSGI;
1688 } else {
1689 switch (sc->sc_state) {
--- 83 unchanged lines hidden (view full) ---
1773 li->busy = 1;
1774 break;
1775
1776 case SEND_SDTR:
1777 device_printf(sc->sc_dev, "sync transfer "
1778 "rejected: target %d\n",
1779 ecb->ccb->ccb_h.target_id);
1780
1781 sc->sc_flags &= ~NCR_SYNCHNEGO;
1782 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1783 ncr53c9x_setsync(sc, ti);
1784 break;
1785
1786 case SEND_WDTR:
1787 device_printf(sc->sc_dev, "wide transfer "
1788 "rejected: target %d\n",
1789 ecb->ccb->ccb_h.target_id);
1790 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1791 ti->width = 0;
1792 break;
1793
1794 case SEND_INIT_DET_ERR:
1795 goto abort;
1796 }
1797 break;
1798
1799 case MSG_NOOP:
--- 37 unchanged lines hidden (view full) ---
1837 case MSG_EXTENDED:
1838 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1839 switch (sc->sc_imess[2]) {
1840 case MSG_EXT_SDTR:
1841 NCR_MSGS(("SDTR period %d, offset %d ",
1842 sc->sc_imess[3], sc->sc_imess[4]));
1843 if (sc->sc_imess[1] != 3)
1844 goto reject;
1845 ti->period = sc->sc_imess[3];
1846 ti->offset = sc->sc_imess[4];
1847 ti->flags &= ~T_NEGOTIATE;
1848 if (sc->sc_minsync == 0 ||
1849 ti->offset == 0 ||
1850 ti->period > 124) {
1851#if 0
1852#ifdef NCR53C9X_DEBUG
1853 xpt_print_path(ecb->ccb->ccb_h.path);
1854 printf("async mode\n");
1855#endif
1856#endif
1857 ti->flags &= ~T_SYNCMODE;
1858 if ((sc->sc_flags & NCR_SYNCHNEGO) ==
1859 0) {
1860 /*
1861 * target initiated negotiation
1862 */
1863 ti->offset = 0;
1864 ncr53c9x_sched_msgout(
1865 SEND_SDTR);
1866 }
1867 } else {
1868 ti->period =
1869 ncr53c9x_cpb2stp(sc,
1870 ncr53c9x_stp2cpb(sc,
1871 ti->period));
1872 if ((sc->sc_flags & NCR_SYNCHNEGO) ==
1873 0) {
1874 /*
1875 * target initiated negotiation
1876 */
1877 if (ti->period < sc->sc_minsync)
1878 ti->period =
1879 sc->sc_minsync;
1880 if (ti->offset > 15)
1881 ti->offset = 15;
1882 ti->flags &= ~T_SYNCMODE;
1883 ncr53c9x_sched_msgout(
1884 SEND_SDTR);
1885 } else {
1886 /* We are sync. */
1887 ti->flags |= T_SYNCMODE;
1888 }
1889 }
1890 sc->sc_flags &= ~NCR_SYNCHNEGO;
1891 ncr53c9x_setsync(sc, ti);
1892 break;
1893
1894 case MSG_EXT_WDTR:
1895#ifdef NCR53C9X_DEBUG
1896 device_printf(sc->sc_dev, "wide mode %d\n",
1897 sc->sc_imess[3]);
1898#endif
1899 if (sc->sc_imess[3] == 1) {
1900 ti->cfg3 |= NCRFASCFG3_EWIDE;
1901 ncr53c9x_setsync(sc, ti);
1902 } else
1903 ti->width = 0;
1904 /*
1905 * Device started width negotiation.
1906 */
1907 if (!(ti->flags & T_WDTRSENT))
1908 ncr53c9x_sched_msgout(SEND_WDTR);
1909 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1910 break;
1911
1912 default:
1913 xpt_print_path(ecb->ccb->ccb_h.path);
1914 printf("unrecognized MESSAGE EXTENDED;"
1915 " sending REJECT\n");
1916 goto reject;
1917 }
--- 45 unchanged lines hidden (view full) ---
1963 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1964 break;
1965
1966 abort:
1967 ncr53c9x_sched_msgout(SEND_ABORT);
1968 }
1969
1970 /* If we have more messages to send set ATN. */
1971 if (sc->sc_msgpriq)
1972 NCRCMD(sc, NCRCMD_SETATN);
1973
1974 /* Acknowledge last message byte. */
1975 NCRCMD(sc, NCRCMD_MSGOK);
1976
1977 /* Done, reset message pointer. */
1978 sc->sc_flags &= ~NCR_DROP_MSGI;
1979 sc->sc_imlen = 0;
1980}
--- 6 unchanged lines hidden (view full) ---
1987{
1988 struct ncr53c9x_tinfo *ti;
1989 struct ncr53c9x_ecb *ecb;
1990 size_t size;
1991#ifdef NCR53C9X_DEBUG
1992 int i;
1993#endif
1994
1995 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1996 sc->sc_msgpriq, sc->sc_prevphase));
1997
1998 /*
1999 * XXX - the NCR_ATN flag is not in sync with the actual ATN
2000 * condition on the SCSI bus. The 53c9x chip
2001 * automatically turns off ATN before sending the
2002 * message byte. (See also the comment below in the
--- 25 unchanged lines hidden (view full) ---
2028 sc->sc_omlen = 1; /* "Default" message len */
2029 switch (sc->sc_msgout) {
2030 case SEND_SDTR:
2031 ecb = sc->sc_nexus;
2032 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2033 sc->sc_omess[0] = MSG_EXTENDED;
2034 sc->sc_omess[1] = MSG_EXT_SDTR_LEN;
2035 sc->sc_omess[2] = MSG_EXT_SDTR;
2036 sc->sc_omess[3] = ti->period;
2037 sc->sc_omess[4] = ti->offset;
2038 sc->sc_omlen = 5;
2039 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
2040 ti->flags |= T_SYNCMODE;
2041 ncr53c9x_setsync(sc, ti);
2042 }
2043 break;
2044
2045 case SEND_WDTR:
2046 ecb = sc->sc_nexus;
2047 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2048 sc->sc_omess[0] = MSG_EXTENDED;
2049 sc->sc_omess[1] = MSG_EXT_WDTR_LEN;
2050 sc->sc_omess[2] = MSG_EXT_WDTR;
2051 sc->sc_omess[3] = ti->width;
2052 sc->sc_omlen = 4;
2053 break;
2054
2055 case SEND_IDENTIFY:
2056 if (sc->sc_state != NCR_CONNECTED) {
2057 device_printf(sc->sc_dev, "at line %d: no "
2058 "nexus\n", __LINE__);
2059 }
--- 13 unchanged lines hidden (view full) ---
2073 sc->sc_omlen = 2;
2074 break;
2075
2076 case SEND_DEV_RESET:
2077 sc->sc_flags |= NCR_ABORTING;
2078 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
2079 ecb = sc->sc_nexus;
2080 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2081 ti->flags &= ~T_SYNCMODE;
2082 if ((ti->flags & T_SYNCHOFF) == 0)
2083 /* We can re-start sync negotiation. */
2084 ti->flags |= T_NEGOTIATE;
2085 break;
2086
2087 case SEND_PARITY_ERROR:
2088 sc->sc_omess[0] = MSG_PARITY_ERROR;
2089 break;
2090
2091 case SEND_ABORT:
2092 sc->sc_flags |= NCR_ABORTING;
--- 40 unchanged lines hidden (view full) ---
2133 /*
2134 * XXX FIFO size
2135 */
2136 ncr53c9x_flushfifo(sc);
2137 ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen);
2138 NCRCMD(sc, NCRCMD_TRANS);
2139 } else {
2140 /* (Re)send the message. */
2141 size = min(sc->sc_omlen, sc->sc_maxxfer);
2142 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
2143 /* Program the SCSI counter. */
2144 NCR_SET_COUNT(sc, size);
2145
2146 /* Load the count in and start the message-out transfer. */
2147 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
2148 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
2149 NCRDMA_GO(sc);
2150 }
2151}
2152
2153/*
2154 * This is the most critical part of the driver, and has to know
2155 * how to deal with *all* error conditions and phases from the SCSI
2156 * bus. If there are no errors and the DMA was active, then call the
2157 * DMA pseudo-interrupt handler. If this returns 1, then that was it
2158 * and we can return from here without further processing.
2159 *
2160 * Most of this needs verifying.
2161 */
2162void
2163ncr53c9x_intr(void *arg)
2164{
2165 struct ncr53c9x_softc *sc = arg;
2166 struct ncr53c9x_ecb *ecb;
2167 struct ncr53c9x_linfo *li;
2168 struct ncr53c9x_tinfo *ti;
2169 struct timeval cur, wait;
2170 size_t size;
2171 int i, nfifo;
2172 u_char msg;
2173
2174 NCR_INTS(("[ncr53c9x_intr: state %d]", sc->sc_state));
2175
2176 if (!NCRDMA_ISINTR(sc))
2177 return;
2178
2179 mtx_lock(&sc->sc_lock);
2180again:
2181 /* and what do the registers say... */
2182 ncr53c9x_readregs(sc);
2183
2184 /*
2185 * At the moment, only a SCSI Bus Reset or Illegal
2186 * Command are classed as errors. A disconnect is a
2187 * valid condition, and we let the code check is the
--- 13 unchanged lines hidden (view full) ---
2201 if ((sc->sc_espintr & NCRINTR_SBR) != 0) {
2202 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) {
2203 NCRCMD(sc, NCRCMD_FLUSH);
2204 DELAY(1);
2205 }
2206 if (sc->sc_state != NCR_SBR) {
2207 device_printf(sc->sc_dev, "SCSI bus reset\n");
2208 ncr53c9x_init(sc, 0); /* Restart everything. */
2209 goto out;
2210 }
2211#if 0
2212/*XXX*/ printf("<expected bus reset: "
2213 "[intr %x, stat %x, step %d]>\n",
2214 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2215#endif
2216 if (sc->sc_nexus != NULL)
2217 panic("%s: nexus in reset state",
2218 device_get_nameunit(sc->sc_dev));
2219 goto sched;
2220 }
--- 9 unchanged lines hidden (view full) ---
2230 NCRCMD(sc, NCRCMD_FLUSH);
2231 DELAY(1);
2232 }
2233 if (sc->sc_state == NCR_CONNECTED ||
2234 sc->sc_state == NCR_SELECTING) {
2235 ecb->ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2236 ncr53c9x_done(sc, ecb);
2237 }
2238 goto out;
2239 }
2240
2241 if ((sc->sc_espintr & NCRINTR_ILL) != 0) {
2242 if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) {
2243 /*
2244 * Eat away "Illegal command" interrupt
2245 * on a ESP100 caused by a re-selection
2246 * while we were trying to select
2247 * another target.
2248 */
2249#ifdef DEBUG
2250 device_printf(sc->sc_dev, "ESP100 work-around "
2251 "activated\n");
2252#endif
2253 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2254 goto out;
2255 }
2256 /* Illegal command, out of sync? */
2257 device_printf(sc->sc_dev, "illegal command: 0x%x "
2258 "(state %d, phase %x, prevphase %x)\n",
2259 sc->sc_lastcmd,
2260 sc->sc_state, sc->sc_phase, sc->sc_prevphase);
2261 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2262 NCRCMD(sc, NCRCMD_FLUSH);
2263 DELAY(1);
2264 }
2265 ncr53c9x_init(sc, 1); /* Restart everything. */
2266 goto out;
2267 }
2268 }
2269 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2270
2271 /*
2272 * Call if DMA is active.
2273 *
2274 * If DMA_INTR returns true, then maybe go 'round the loop
2275 * again in case there is no more DMA queued, but a phase
2276 * change is expected.
2277 */
2278 if (NCRDMA_ISACTIVE(sc)) {
2279 if (NCRDMA_INTR(sc) == -1) {
2280 device_printf(sc->sc_dev, "DMA error; resetting\n");
2281 ncr53c9x_init(sc, 1);
2282 goto out;
2283 }
2284 /* If DMA active here, then go back to work... */
2285 if (NCRDMA_ISACTIVE(sc))
2286 goto out;
2287
2288 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
2289 /*
2290 * DMA not completed. If we can not find a
2291 * acceptable explanation, print a diagnostic.
2292 */
2293 if (sc->sc_state == NCR_SELECTING)
2294 /*
--- 33 unchanged lines hidden (view full) ---
2328 device_printf(sc->sc_dev, "!TC on DATA XFER"
2329 " [intr %x, stat %x, step %d]"
2330 " prevphase %x, resid %x\n",
2331 sc->sc_espintr,
2332 sc->sc_espstat,
2333 sc->sc_espstep,
2334 sc->sc_prevphase,
2335 ecb ? ecb->dleft : -1);
2336 panic("esp: unrecoverable DMA error");
2337 }
2338 }
2339 }
2340
2341 /*
2342 * Check for less serious errors.
2343 */
2344 if ((sc->sc_espstat & NCRSTAT_PE) != 0) {
--- 40 unchanged lines hidden (view full) ---
2385 li = LIST_FIRST(&ti->luns);
2386 continue;
2387 }
2388 li = LIST_NEXT(li, link);
2389 }
2390 goto finish;
2391
2392 case NCR_CONNECTED:
2393 if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) {
2394#ifdef NCR53C9X_DEBUG
2395 if (ecb != NULL)
2396 xpt_print_path(ecb->ccb->ccb_h.path);
2397 printf("sync nego not completed!\n");
2398#endif
2399 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2400 sc->sc_flags &= ~NCR_SYNCHNEGO;
2401 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
2402 }
2403
2404 /* It may be OK to disconnect. */
2405 if ((sc->sc_flags & NCR_ABORTING) == 0) {
2406 /*
2407 * Section 5.1.1 of the SCSI 2 spec
2408 * suggests issuing a REQUEST SENSE
2409 * following an unexpected disconnect.
--- 16 unchanged lines hidden (view full) ---
2426 * safely cancel the DMA that is in
2427 * progress.
2428 */
2429 if (1 || (ecb->flags & ECB_SENSE) != 0) {
2430 printf("resetting\n");
2431 goto reset;
2432 }
2433 printf("sending REQUEST SENSE\n");
2434 untimeout(ncr53c9x_timeout, ecb,
2435 ecb->ccb->ccb_h.timeout_ch);
2436 ncr53c9x_sense(sc, ecb);
2437 goto out;
2438 }
2439
2440 ecb->ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2441 goto finish;
2442
2443 case NCR_DISCONNECT:
2444 sc->sc_nexus = NULL;
2445 goto sched;
2446
2447 case NCR_CMDCOMPLETE:
2448 ecb->ccb->ccb_h.status = CAM_REQ_CMP;
2449 goto finish;
2450 }
2451 }
2452
2453 switch (sc->sc_state) {
2454 case NCR_SBR:
2455 device_printf(sc->sc_dev, "waiting for Bus Reset to happen\n");
2456 goto out;
2457
2458 case NCR_RESELECTED:
2459 /*
2460 * We must be continuing a message?
2461 */
2462 device_printf(sc->sc_dev, "unhandled reselect continuation, "
2463 "state %d, intr %02x\n", sc->sc_state, sc->sc_espintr);
2464 ncr53c9x_init(sc, 1);
2465 goto out;
2466 break;
2467
2468 case NCR_IDENTIFIED:
2469 ecb = sc->sc_nexus;
2470 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2471 i = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
2472 /*
2473 * Things are seriously screwed up.
2474 * Pull the brakes, i.e. reset.
2475 */
2476 device_printf(sc->sc_dev, "target didn't send tag: %d "
2477 "bytes in FIFO\n", i);
2478 /* Drain and display FIFO. */
2479 while (i-- > 0)
2480 printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO));
2481
2482 ncr53c9x_init(sc, 1);
2483 goto out;
2484 } else
2485 goto msgin;
2486
2487 case NCR_IDLE:
2488 case NCR_SELECTING:
2489 ecb = sc->sc_nexus;
2490 if (sc->sc_espintr & NCRINTR_RESEL) {
2491 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
2492 sc->sc_flags = 0;
2493 /*
2494 * If we're trying to select a
2495 * target ourselves, push our command
2496 * back into the ready list.
2497 */
2498 if (sc->sc_state == NCR_SELECTING) {
2499 NCR_INTS(("backoff selector "));
2500 untimeout(ncr53c9x_timeout, ecb,
2501 ecb->ccb->ccb_h.timeout_ch);
2502 ncr53c9x_dequeue(sc, ecb);
2503 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
2504 ecb->flags |= ECB_READY;
2505 ecb = sc->sc_nexus = NULL;
2506 }
2507 sc->sc_state = NCR_RESELECTED;
2508 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2509 /*
2510 * Things are seriously screwed up.
2511 * Pull the brakes, i.e. reset
2512 */
2513 device_printf(sc->sc_dev, "target didn't "
2514 "identify\n");
2515 ncr53c9x_init(sc, 1);
2516 goto out;
2517 }
2518 /*
2519 * The C90 only inhibits FIFO writes until reselection
2520 * is complete instead of waiting until the interrupt
2521 * status register has been read. So, if the reselect
2522 * happens while we were entering command bytes (for
2523 * another target) some of those bytes can appear in
2524 * the FIFO here, after the interrupt is taken.
--- 25 unchanged lines hidden (view full) ---
2550 device_printf(sc->sc_dev, "RESELECT: %d bytes "
2551 "in FIFO! [intr %x, stat %x, step %d, "
2552 "prevphase %x]\n",
2553 nfifo,
2554 sc->sc_espintr,
2555 sc->sc_espstat,
2556 sc->sc_espstep,
2557 sc->sc_prevphase);
2558 ncr53c9x_init(sc, 1);
2559 goto out;
2560 }
2561 sc->sc_selid = sc->sc_imess[0];
2562 NCR_INTS(("selid=%02x ", sc->sc_selid));
2563
2564 /* Handle IDENTIFY message. */
2565 ncr53c9x_msgin(sc);
2566
2567 if (sc->sc_state != NCR_CONNECTED &&
2568 sc->sc_state != NCR_IDENTIFIED) {
2569 /* IDENTIFY fail?! */
2570 device_printf(sc->sc_dev, "identify failed, "
2571 "state %d, intr %02x\n", sc->sc_state,
2572 sc->sc_espintr);
2573 ncr53c9x_init(sc, 1);
2574 goto out;
2575 }
2576 goto shortcut; /* i.e. next phase expected soon */
2577 }
2578
2579#define NCRINTR_DONE (NCRINTR_FC | NCRINTR_BS)
2580 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
2581 /*
2582 * Arbitration won; examine the `step' register
2583 * to determine how far the selection could progress.
2584 */
2585 if (ecb == NULL) {
2586 /*
2587 * When doing path inquiry during boot
2588 * FAS100A trigger a stray interrupt which
2589 * we just ignore instead of panicing.
2590 */
2591 if (sc->sc_state == NCR_IDLE &&
2592 sc->sc_espstep == 0)
2593 goto out;
2594 panic("ncr53c9x: no nexus");
2595 }
2596
2597 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2598
2599 switch (sc->sc_espstep) {
2600 case 0:
2601 /*
--- 4 unchanged lines hidden (view full) ---
2606 * target should be in the command
2607 * phase.
2608 * XXX check for command phase?
2609 */
2610 NCRCMD(sc, NCRCMD_RSTATN);
2611 break;
2612
2613 case 1:
2614 if ((ti->flags & T_NEGOTIATE) == 0 &&
2615 ecb->tag[0] == 0) {
2616 device_printf(sc->sc_dev, "step 1 & "
2617 "!NEG\n");
2618 goto reset;
2619 }
2620 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
2621 device_printf(sc->sc_dev, "!MSGOUT\n");
2622 goto reset;
2623 }
2624 if (ti->flags & T_WIDE) {
2625 ti->flags |= T_WDTRSENT;
2626 ncr53c9x_sched_msgout(SEND_WDTR);
2627 }
2628 if (ti->flags & T_NEGOTIATE) {
2629 /* Start negotiating */
2630 sc->sc_flags |= NCR_SYNCHNEGO;
2631 if (ecb->tag[0])
2632 ncr53c9x_sched_msgout(
2633 SEND_TAG|SEND_SDTR);
2634 else
2635 ncr53c9x_sched_msgout(
2636 SEND_SDTR);
2637 } else {
2638 /* Could not do ATN3 so send TAG. */
2639 ncr53c9x_sched_msgout(SEND_TAG);
2640 }
2641 sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */
2642 break;
2643
2644 case 3:
2645 /*
2646 * Grr, this is supposed to mean
2647 * "target left command phase prematurely".
2648 * It seems to happen regularly when
2649 * sync mode is on.
--- 4 unchanged lines hidden (view full) ---
2654 if (sc->sc_cmdlen == 0)
2655 /* Hope for the best... */
2656 break;
2657 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
2658 & NCRFIFO_FF) == 0) {
2659 /* Hope for the best... */
2660 break;
2661 }
2662 printf("(%s:%d:%d): selection failed;"
2663 " %d left in FIFO "
2664 "[intr %x, stat %x, step %d]\n",
2665 device_get_nameunit(sc->sc_dev),
2666 ecb->ccb->ccb_h.target_id,
2667 ecb->ccb->ccb_h.target_lun,
2668 NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF,
2669 sc->sc_espintr, sc->sc_espstat,
2670 sc->sc_espstep);
2671 NCRCMD(sc, NCRCMD_FLUSH);
2672 ncr53c9x_sched_msgout(SEND_ABORT);
2673 goto out;
2674
2675 case 2:
2676 /* Select stuck at Command Phase. */
2677 NCRCMD(sc, NCRCMD_FLUSH);
2678 break;
2679
2680 case 4:
2681 if (sc->sc_features & NCR_F_DMASELECT &&
2682 sc->sc_cmdlen != 0)
2683 printf("(%s:%d:%d): select; "
2684 "%lu left in DMA buffer "
2685 "[intr %x, stat %x, step %d]\n",
2686 device_get_nameunit(sc->sc_dev),
2687 ecb->ccb->ccb_h.target_id,
2688 ecb->ccb->ccb_h.target_lun,
2689 (u_long)sc->sc_cmdlen,
2690 sc->sc_espintr,
2691 sc->sc_espstat,
2692 sc->sc_espstep);
2693 /* So far, everything went fine. */
2694 break;
2695 }
2696
2697 sc->sc_prevphase = INVALID_PHASE; /* ??? */
2698 /* Do an implicit RESTORE POINTERS. */
2699 sc->sc_dp = ecb->daddr;
2700 sc->sc_dleft = ecb->dleft;
--- 4 unchanged lines hidden (view full) ---
2705 "select: [intr %x, stat %x, step %x]\n",
2706 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2707 NCRCMD(sc, NCRCMD_FLUSH);
2708 DELAY(1);
2709 goto reset;
2710 }
2711 if (sc->sc_state == NCR_IDLE) {
2712 device_printf(sc->sc_dev, "stray interrupt\n");
2713 goto out;
2714 }
2715 break;
2716
2717 case NCR_CONNECTED:
2718 if ((sc->sc_flags & NCR_ICCS) != 0) {
2719 /* "Initiate Command Complete Steps" in progress */
2720 sc->sc_flags &= ~NCR_ICCS;
2721
--- 88 unchanged lines hidden (view full) ---
2810 */
2811
2812 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
2813 ecb->cmd.cmd.opcode, ecb->clen));
2814 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2815 NCRCMD(sc, NCRCMD_FLUSH);
2816/* DELAY(1);*/
2817 }
2818 if (sc->sc_features & NCR_F_DMASELECT) {
2819 /* Setup DMA transfer for command. */
2820 size = ecb->clen;
2821 sc->sc_cmdlen = size;
2822 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
2823 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
2824 0, &size);
2825 /* Program the SCSI counter. */
--- 10 unchanged lines hidden (view full) ---
2836 NCRCMD(sc, NCRCMD_TRANS);
2837 }
2838 sc->sc_prevphase = COMMAND_PHASE;
2839 break;
2840
2841 case DATA_OUT_PHASE:
2842 NCR_PHASE(("DATA_OUT_PHASE [%ld] ", (long)sc->sc_dleft));
2843 NCRCMD(sc, NCRCMD_FLUSH);
2844 size = min(sc->sc_dleft, sc->sc_maxxfer);
2845 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size);
2846 sc->sc_prevphase = DATA_OUT_PHASE;
2847 goto setup_xfer;
2848
2849 case DATA_IN_PHASE:
2850 NCR_PHASE(("DATA_IN_PHASE "));
2851 if (sc->sc_rev == NCR_VARIANT_ESP100)
2852 NCRCMD(sc, NCRCMD_FLUSH);
2853 size = min(sc->sc_dleft, sc->sc_maxxfer);
2854 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size);
2855 sc->sc_prevphase = DATA_IN_PHASE;
2856 setup_xfer:
2857 /* Target returned to data phase: wipe "done" memory */
2858 ecb->flags &= ~ECB_TENTATIVE_DONE;
2859
2860 /* Program the SCSI counter. */
2861 NCR_SET_COUNT(sc, size);
--- 6 unchanged lines hidden (view full) ---
2868 * all the bytes we want but we're still in DATA PHASE.
2869 * Apparently, the device needs padding. Also, a
2870 * transfer size of 0 means "maximum" to the chip
2871 * DMA logic.
2872 */
2873 NCRCMD(sc,
2874 (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA);
2875 NCRDMA_GO(sc);
2876 goto out;
2877
2878 case STATUS_PHASE:
2879 NCR_PHASE(("STATUS_PHASE "));
2880 sc->sc_flags |= NCR_ICCS;
2881 NCRCMD(sc, NCRCMD_ICCS);
2882 sc->sc_prevphase = STATUS_PHASE;
2883 goto shortcut; /* i.e. expect status results soon */
2884
2885 case INVALID_PHASE:
2886 break;
2887
2888 default:
2889 device_printf(sc->sc_dev,
2890 "unexpected bus phase; resetting\n");
2891 goto reset;
2892 }
2893
2894out:
2895 mtx_unlock(&sc->sc_lock);
2896 return;
2897
2898reset:
2899 ncr53c9x_init(sc, 1);
2900 goto out;
2901
2902finish:
2903 ncr53c9x_done(sc, ecb);
2904 goto out;
2905
2906sched:
2907 sc->sc_state = NCR_IDLE;
2908 ncr53c9x_sched(sc);
2909 goto out;
2910
2911shortcut:
2912 /*
2913 * The idea is that many of the SCSI operations take very little
2914 * time, and going away and getting interrupted is too high an
2915 * overhead to pay. For example, selecting, sending a message
2916 * and command and then doing some work can be done in one "pass".
2917 *
--- 6 unchanged lines hidden (view full) ---
2924 wait.tv_sec++;
2925 wait.tv_usec -= 1000000;
2926 }
2927 do {
2928 if (NCRDMA_ISINTR(sc))
2929 goto again;
2930 microtime(&cur);
2931 } while (cur.tv_sec <= wait.tv_sec && cur.tv_usec <= wait.tv_usec);
2932 goto out;
2933}
2934
2935static void
2936ncr53c9x_abort(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
2937{
2938
2939 /* 2 secs for the abort */
2940 ecb->timeout = NCR_ABORT_TIMEOUT;
2941 ecb->flags |= ECB_ABORT;
2942
2943 if (ecb == sc->sc_nexus) {
2944 /*
2945 * If we're still selecting, the message will be scheduled
2946 * after selection is complete.
2947 */
2948 if (sc->sc_state == NCR_CONNECTED)
2949 ncr53c9x_sched_msgout(SEND_ABORT);
2950
2951 /*
2952 * Reschedule timeout.
2953 */
2954 ecb->ccb->ccb_h.timeout_ch =
2955 timeout(ncr53c9x_timeout, ecb, mstohz(ecb->timeout));
2956 } else {
2957 /*
2958 * Just leave the command where it is.
2959 * XXX - what choice do we have but to reset the SCSI
2960 * eventually?
2961 */
2962 if (sc->sc_state == NCR_IDLE)
2963 ncr53c9x_sched(sc);
2964 }
2965}
2966
2967static void
2968ncr53c9x_timeout(void *arg)
2969{
2970 struct ncr53c9x_ecb *ecb = arg;
2971 union ccb *ccb = ecb->ccb;
2972 struct ncr53c9x_softc *sc = ecb->sc;
2973 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
2974
2975 xpt_print_path(ccb->ccb_h.path);
2976 device_printf(sc->sc_dev, "timed out [ecb %p (flags 0x%x, dleft %x, "
2977 "stat %x)], <state %d, nexus %p, phase(l %x, c %x, p %x), "
2978 "resid %lx, msg(q %x,o %x) %s>",
2979 ecb, ecb->flags, ecb->dleft, ecb->stat,
2980 sc->sc_state, sc->sc_nexus,
2981 NCR_READ_REG(sc, NCR_STAT),
2982 sc->sc_phase, sc->sc_prevphase,
2983 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2984 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2985#if defined(NCR53C9X_DEBUG) && NCR53C9X_DEBUG > 1
2986 printf("TRACE: %s.", ecb->trace);
2987#endif
2988
2989 mtx_lock(&sc->sc_lock);
2990
2991 if (ecb->flags & ECB_ABORT) {
2992 /* Abort timed out. */
2993 printf(" AGAIN\n");
2994 ncr53c9x_init(sc, 1);
2995 } else {
2996 /* Abort the operation that has timed out. */
2997 printf("\n");
2998 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2999 ncr53c9x_abort(sc, ecb);
3000
3001 /* Disable sync mode if stuck in a data phase. */
3002 if (ecb == sc->sc_nexus &&
3003 (ti->flags & T_SYNCMODE) != 0 &&
3004 (sc->sc_phase & (MSGI|CDI)) == 0) {
3005 /* XXX ASYNC CALLBACK! */
3006 xpt_print_path(ccb->ccb_h.path);
3007 printf("sync negotiation disabled\n");
3008 sc->sc_cfflags |=
3009 (1 << ((ccb->ccb_h.target_id & 7) + 8));
3010 }
3011 }
3012
3013 mtx_unlock(&sc->sc_lock);
3014}
3015
3016static void
3017ncr53c9x_watch(void *arg)
3018{
3019 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
3020 struct ncr53c9x_linfo *li;
3021 struct ncr53c9x_tinfo *ti;
3022 time_t old;
3023 int t;
3024
3025 /* Delete any structures that have not been used in 10min. */
3026 old = time_second - (10 * 60);
3027
3028 mtx_lock(&sc->sc_lock);
3029 for (t = 0; t < sc->sc_ntarg; t++) {
3030 ti = &sc->sc_tinfo[t];
3031 li = LIST_FIRST(&ti->luns);
3032 while (li) {
3033 if (li->last_used < old &&
3034 li->untagged == NULL &&
3035 li->used == 0) {
3036 if (li->lun < NCR_NLUN)
3037 ti->lun[li->lun] = NULL;
3038 LIST_REMOVE(li, link);
3039 free(li, M_DEVBUF);
3040 /* Restart the search at the beginning. */
3041 li = LIST_FIRST(&ti->luns);
3042 continue;
3043 }
3044 li = LIST_NEXT(li, link);
3045 }
3046 }
3047 mtx_unlock(&sc->sc_lock);
3048 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);
3049}