The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/pci/ncr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /**************************************************************************
    2 **
    3 ** $FreeBSD: src/sys/pci/ncr.c,v 1.82.2.11 1999/09/05 08:21:18 peter Exp $
    4 **
    5 **  Device driver for the   NCR 53C810   PCI-SCSI-Controller.
    6 **
    7 **  FreeBSD / NetBSD
    8 **
    9 **-------------------------------------------------------------------------
   10 **
   11 **  Written for 386bsd and FreeBSD by
   12 **      Wolfgang Stanglmeier    <wolf@cologne.de>
   13 **      Stefan Esser            <se@mi.Uni-Koeln.de>
   14 **
   15 **  Ported to NetBSD by
   16 **      Charles M. Hannum       <mycroft@gnu.ai.mit.edu>
   17 **
   18 **-------------------------------------------------------------------------
   19 **
   20 ** Copyright (c) 1994 Wolfgang Stanglmeier.  All rights reserved.
   21 **
   22 ** Redistribution and use in source and binary forms, with or without
   23 ** modification, are permitted provided that the following conditions
   24 ** are met:
   25 ** 1. Redistributions of source code must retain the above copyright
   26 **    notice, this list of conditions and the following disclaimer.
   27 ** 2. Redistributions in binary form must reproduce the above copyright
   28 **    notice, this list of conditions and the following disclaimer in the
   29 **    documentation and/or other materials provided with the distribution.
   30 ** 3. The name of the author may not be used to endorse or promote products
   31 **    derived from this software without specific prior written permission.
   32 **
   33 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   34 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   35 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   36 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   37 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   38 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   39 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   40 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   41 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   42 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   43 **
   44 ***************************************************************************
   45 */
   46 
   47 #define NCR_DATE "pl24 96/12/14"
   48 
   49 #define NCR_VERSION     (2)
   50 #define MAX_UNITS       (16)
   51 
   52 #define NCR_GETCC_WITHMSG
   53 
   54 #if defined (__FreeBSD__) && defined(KERNEL)
   55 #include "opt_ncr.h"
   56 #endif /* defined (__FreeBSD__) && defined(KERNEL) */
   57 
   58 #ifdef FAILSAFE
   59 #ifndef SCSI_NCR_DFLT_TAGS
   60 #define SCSI_NCR_DFLT_TAGS (0)
   61 #endif /* SCSI_NCR_DFLT_TAGS */
   62 #define CDROM_ASYNC
   63 #endif /* FAILSAFE */
   64 
   65 /*==========================================================
   66 **
   67 **      Configuration and Debugging
   68 **
   69 **      May be overwritten in <arch/conf/xxxx>
   70 **
   71 **==========================================================
   72 */
   73 
   74 /*
   75 **    SCSI address of this device.
   76 **    The boot routines should have set it.
   77 **    If not, use this.
   78 */
   79 
   80 #ifndef SCSI_NCR_MYADDR
   81 #define SCSI_NCR_MYADDR      (7)
   82 #endif /* SCSI_NCR_MYADDR */
   83 
   84 /*
   85 **    The default synchronous period factor
   86 **    (0=asynchronous)
   87 **    If maximum synchronous frequency is defined, use it instead.
   88 */
   89 
   90 #ifndef SCSI_NCR_MAX_SYNC
   91 
   92 #ifndef SCSI_NCR_DFLT_SYNC
   93 #define SCSI_NCR_DFLT_SYNC   (12)
   94 #endif /* SCSI_NCR_DFLT_SYNC */
   95 
   96 #else
   97 
   98 #if     SCSI_NCR_MAX_SYNC == 0
   99 #define SCSI_NCR_DFLT_SYNC 0
  100 #else
  101 #define SCSI_NCR_DFLT_SYNC (250000 / SCSI_NCR_MAX_SYNC)
  102 #endif
  103 
  104 #endif
  105 
  106 /*
  107 **    The minimal asynchronous pre-scaler period (ns)
  108 **    Shall be 40.
  109 */
  110 
  111 #ifndef SCSI_NCR_MIN_ASYNC
  112 #define SCSI_NCR_MIN_ASYNC   (40)
  113 #endif /* SCSI_NCR_MIN_ASYNC */
  114 
  115 /*
  116 **    The maximal bus with (in log2 byte)
  117 **    (0=8 bit, 1=16 bit)
  118 */
  119 
  120 #ifndef SCSI_NCR_MAX_WIDE
  121 #define SCSI_NCR_MAX_WIDE   (1)
  122 #endif /* SCSI_NCR_MAX_WIDE */
  123 
  124 /*
  125 **    The maximum number of tags per logic unit.
  126 **    Used only for disk devices that support tags.
  127 */
  128 
  129 #ifndef SCSI_NCR_DFLT_TAGS
  130 #define SCSI_NCR_DFLT_TAGS    (4)
  131 #endif /* SCSI_NCR_DFLT_TAGS */
  132 
  133 /*==========================================================
  134 **
  135 **      Configuration and Debugging
  136 **
  137 **==========================================================
  138 */
  139 
  140 /*
  141 **    Number of targets supported by the driver.
  142 **    n permits target numbers 0..n-1.
  143 **    Default is 7, meaning targets #0..#6.
  144 **    #7 .. is myself.
  145 */
  146 
  147 #define MAX_TARGET  (16)
  148 
  149 /*
  150 **    Number of logic units supported by the driver.
  151 **    n enables logic unit numbers 0..n-1.
  152 **    The common SCSI devices require only
  153 **    one lun, so take 1 as the default.
  154 */
  155 
  156 #ifndef MAX_LUN
  157 #define MAX_LUN     (8)
  158 #endif  /* MAX_LUN */
  159 
  160 /*
  161 **    The maximum number of jobs scheduled for starting.
  162 **    There should be one slot per target, and one slot
  163 **    for each tag of each target in use.
  164 **    The calculation below is actually quite silly ...
  165 */
  166 
  167 #define MAX_START   (32) /* (MAX_TARGET + 7 * SCSI_NCR_DFLT_TAGS) */
  168 
  169 /*
  170 **    The maximum number of segments a transfer is split into.
  171 */
  172 
  173 #define MAX_SCATTER (33)
  174 
  175 /*
  176 **    The maximum transfer length (should be >= 64k).
  177 **    MUST NOT be greater than (MAX_SCATTER-1) * PAGE_SIZE.
  178 */
  179 
  180 #define MAX_SIZE  ((MAX_SCATTER-1) * (long) PAGE_SIZE)
  181 
  182 /*
  183 **      other
  184 */
  185 
  186 #define NCR_SNOOP_TIMEOUT (1000000)
  187 
  188 /*==========================================================
  189 **
  190 **      Include files
  191 **
  192 **==========================================================
  193 */
  194 
  195 #ifdef __NetBSD__
  196 #ifdef _KERNEL
  197 #define KERNEL
  198 #endif
  199 #endif
  200 #include <stddef.h>
  201 
  202 #include <sys/param.h>
  203 #include <sys/time.h>
  204 
  205 #ifdef KERNEL
  206 #include <sys/systm.h>
  207 #include <sys/malloc.h>
  208 #include <sys/buf.h>
  209 #include <sys/kernel.h>
  210 #ifdef __NetBSD__
  211 #define bootverbose     1
  212 #endif
  213 #include <sys/sysctl.h>
  214 #ifndef __NetBSD__
  215 #include <machine/clock.h>
  216 #endif
  217 #include <vm/vm.h>
  218 #include <vm/pmap.h>
  219 #include <vm/vm_extern.h>
  220 #endif /* KERNEL */
  221 
  222 
  223 #ifndef __NetBSD__
  224 #include <pci/pcivar.h>
  225 #include <pci/pcireg.h>
  226 #include <pci/ncrreg.h>
  227 #else
  228 #include <sys/device.h>
  229 #include <dev/pci/ncr_reg.h>
  230 #include <dev/pci/pcivar.h>
  231 #include <dev/pci/pcireg.h>
  232 #define DELAY(x)        delay(x)
  233 #endif /* __NetBSD */
  234 
  235 #include <scsi/scsiconf.h>
  236 
  237 
  238 /*==========================================================
  239 **
  240 **      Debugging tags
  241 **
  242 **==========================================================
  243 */
  244 
  245 #define DEBUG_ALLOC    (0x0001)
  246 #define DEBUG_PHASE    (0x0002)
  247 #define DEBUG_POLL     (0x0004)
  248 #define DEBUG_QUEUE    (0x0008)
  249 #define DEBUG_RESULT   (0x0010)
  250 #define DEBUG_SCATTER  (0x0020)
  251 #define DEBUG_SCRIPT   (0x0040)
  252 #define DEBUG_TINY     (0x0080)
  253 #define DEBUG_TIMING   (0x0100)
  254 #define DEBUG_NEGO     (0x0200)
  255 #define DEBUG_TAGS     (0x0400)
  256 #define DEBUG_FREEZE   (0x0800)
  257 #define DEBUG_RESTART  (0x1000)
  258 
  259 /*
  260 **    Enable/Disable debug messages.
  261 **    Can be changed at runtime too.
  262 */
  263 
  264 #ifdef SCSI_NCR_DEBUG
  265         #define DEBUG_FLAGS ncr_debug
  266 #else /* SCSI_NCR_DEBUG */
  267         #define SCSI_NCR_DEBUG  0
  268         #define DEBUG_FLAGS     0
  269 #endif /* SCSI_NCR_DEBUG */
  270 
  271 
  272 
  273 /*==========================================================
  274 **
  275 **      assert ()
  276 **
  277 **==========================================================
  278 **
  279 **      modified copy from 386bsd:/usr/include/sys/assert.h
  280 **
  281 **----------------------------------------------------------
  282 */
  283 
  284 #define assert(expression) { \
  285         if (!(expression)) { \
  286                 (void)printf(\
  287                         "assertion \"%s\" failed: file \"%s\", line %d\n", \
  288                         #expression, \
  289                         __FILE__, __LINE__); \
  290         } \
  291 }
  292 
  293 /*==========================================================
  294 **
  295 **      Access to the controller chip.
  296 **
  297 **==========================================================
  298 */
  299 
  300 #ifdef NCR_IOMAPPED
  301 
  302 #define INB(r) inb (np->port + offsetof(struct ncr_reg, r))
  303 #define INW(r) inw (np->port + offsetof(struct ncr_reg, r))
  304 #define INL(r) inl (np->port + offsetof(struct ncr_reg, r))
  305 
  306 #define OUTB(r, val) outb (np->port+offsetof(struct ncr_reg,r),(val))
  307 #define OUTW(r, val) outw (np->port+offsetof(struct ncr_reg,r),(val))
  308 #define OUTL(r, val) outl (np->port+offsetof(struct ncr_reg,r),(val))
  309 
  310 #define INB_OFF(o) inb (np->port + (o))
  311 #define INW_OFF(o) inw (np->port + (o))
  312 #define INL_OFF(o) inl (np->port + (o))
  313 
  314 #else
  315 
  316 #define INB(r) (np->reg->r)
  317 #define INW(r) (np->reg->r)
  318 #define INL(r) (np->reg->r)
  319 
  320 #define OUTB(r, val) np->reg->r = (val)
  321 #define OUTW(r, val) np->reg->r = (val)
  322 #define OUTL(r, val) np->reg->r = (val)
  323 
  324 #define INB_OFF(o) *( ((u_char *) np->reg) + (o) )
  325 #define INW_OFF(o) *((u_short *) ( ((u_char *) np->reg) + (o)) )
  326 #define INL_OFF(o) *((u_int32_t *)  ( ((u_char *) np->reg) + (o)) )
  327 
  328 #endif
  329 
  330 /*
  331 **      Set bit field ON, OFF 
  332 */
  333 
  334 #define OUTONB(r, m)    OUTB(r, INB(r) | (m))
  335 #define OUTOFFB(r, m)   OUTB(r, INB(r) & ~(m))
  336 #define OUTONW(r, m)    OUTW(r, INW(r) | (m))
  337 #define OUTOFFW(r, m)   OUTW(r, INW(r) & ~(m))
  338 #define OUTONL(r, m)    OUTL(r, INL(r) | (m))
  339 #define OUTOFFL(r, m)   OUTL(r, INL(r) & ~(m))
  340 
  341 #define gettime(t)      (*(struct timeval *)(t) = time)
  342 
  343 /*==========================================================
  344 **
  345 **      Command control block states.
  346 **
  347 **==========================================================
  348 */
  349 
  350 #define HS_IDLE         (0)
  351 #define HS_BUSY         (1)
  352 #define HS_NEGOTIATE    (2)     /* sync/wide data transfer*/
  353 #define HS_DISCONNECT   (3)     /* Disconnected by target */
  354 
  355 #define HS_COMPLETE     (4)
  356 #define HS_SEL_TIMEOUT  (5)     /* Selection timeout      */
  357 #define HS_RESET        (6)     /* SCSI reset        */
  358 #define HS_ABORTED      (7)     /* Transfer aborted       */
  359 #define HS_TIMEOUT      (8)     /* Software timeout       */
  360 #define HS_FAIL         (9)     /* SCSI or PCI bus errors */
  361 #define HS_UNEXPECTED   (10)    /* Unexpected disconnect  */
  362 
  363 #define HS_DONEMASK     (0xfc)
  364 
  365 /*==========================================================
  366 **
  367 **      Software Interrupt Codes
  368 **
  369 **==========================================================
  370 */
  371 
  372 #define SIR_SENSE_RESTART       (1)
  373 #define SIR_SENSE_FAILED        (2)
  374 #define SIR_STALL_RESTART       (3)
  375 #define SIR_STALL_QUEUE         (4)
  376 #define SIR_NEGO_SYNC           (5)
  377 #define SIR_NEGO_WIDE           (6)
  378 #define SIR_NEGO_FAILED         (7)
  379 #define SIR_NEGO_PROTO          (8)
  380 #define SIR_REJECT_RECEIVED     (9)
  381 #define SIR_REJECT_SENT         (10)
  382 #define SIR_IGN_RESIDUE         (11)
  383 #define SIR_MISSING_SAVE        (12)
  384 #define SIR_MAX                 (12)
  385 
  386 /*==========================================================
  387 **
  388 **      Extended error codes.
  389 **      xerr_status field of struct ccb.
  390 **
  391 **==========================================================
  392 */
  393 
  394 #define XE_OK           (0)
  395 #define XE_EXTRA_DATA   (1)     /* unexpected data phase */
  396 #define XE_BAD_PHASE    (2)     /* illegal phase (4/5)   */
  397 
  398 /*==========================================================
  399 **
  400 **      Negotiation status.
  401 **      nego_status field       of struct ccb.
  402 **
  403 **==========================================================
  404 */
  405 
  406 #define NS_SYNC         (1)
  407 #define NS_WIDE         (2)
  408 
  409 /*==========================================================
  410 **
  411 **      "Special features" of targets.
  412 **      quirks field            of struct tcb.
  413 **      actualquirks field      of struct ccb.
  414 **
  415 **==========================================================
  416 */
  417 
  418 #define QUIRK_AUTOSAVE  (0x01)
  419 #define QUIRK_NOMSG     (0x02)
  420 #define QUIRK_NOSYNC    (0x10)
  421 #define QUIRK_NOWIDE16  (0x20)
  422 #define QUIRK_UPDATE    (0x80)
  423 
  424 /*==========================================================
  425 **
  426 **      Capability bits in Inquire response byte 7.
  427 **
  428 **==========================================================
  429 */
  430 
  431 #define INQ7_QUEUE      (0x02)
  432 #define INQ7_SYNC       (0x10)
  433 #define INQ7_WIDE16     (0x20)
  434 
  435 /*==========================================================
  436 **
  437 **      Misc.
  438 **
  439 **==========================================================
  440 */
  441 
  442 #define CCB_MAGIC       (0xf2691ad2)
  443 #define MAX_TAGS        (16)            /* hard limit */
  444 
  445 /*==========================================================
  446 **
  447 **      OS dependencies.
  448 **
  449 **==========================================================
  450 */
  451 
  452 #define PRINT_ADDR(xp) sc_print_addr(xp->sc_link)
  453 
  454 /*==========================================================
  455 **
  456 **      Declaration of structs.
  457 **
  458 **==========================================================
  459 */
  460 
  461 struct tcb;
  462 struct lcb;
  463 struct ccb;
  464 struct ncb;
  465 struct script;
  466 
  467 typedef struct ncb * ncb_p;
  468 typedef struct tcb * tcb_p;
  469 typedef struct lcb * lcb_p;
  470 typedef struct ccb * ccb_p;
  471 
  472 struct link {
  473         ncrcmd  l_cmd;
  474         ncrcmd  l_paddr;
  475 };
  476 
  477 struct  usrcmd {
  478         u_long  target;
  479         u_long  lun;
  480         u_long  data;
  481         u_long  cmd;
  482 };
  483 
  484 #define UC_SETSYNC      10
  485 #define UC_SETTAGS      11
  486 #define UC_SETDEBUG     12
  487 #define UC_SETORDER     13
  488 #define UC_SETWIDE      14
  489 #define UC_SETFLAG      15
  490 
  491 #define UF_TRACE        (0x01)
  492 
  493 /*---------------------------------------
  494 **
  495 **      Timestamps for profiling
  496 **
  497 **---------------------------------------
  498 */
  499 
  500 struct tstamp {
  501         struct timeval  start;
  502         struct timeval  end;
  503         struct timeval  select;
  504         struct timeval  command;
  505         struct timeval  data;
  506         struct timeval  status;
  507         struct timeval  disconnect;
  508         struct timeval  reselect;
  509 };
  510 
  511 /*
  512 **      profiling data (per device)
  513 */
  514 
  515 struct profile {
  516         u_long  num_trans;
  517         u_long  num_bytes;
  518         u_long  num_disc;
  519         u_long  num_break;
  520         u_long  num_int;
  521         u_long  num_fly;
  522         u_long  ms_setup;
  523         u_long  ms_data;
  524         u_long  ms_disc;
  525         u_long  ms_post;
  526 };
  527 
  528 /*==========================================================
  529 **
  530 **      Declaration of structs:         target control block
  531 **
  532 **==========================================================
  533 */
  534 
  535 struct tcb {
  536         /*
  537         **      during reselection the ncr jumps to this point
  538         **      with SFBR set to the encoded target number
  539         **      with bit 7 set.
  540         **      if it's not this target, jump to the next.
  541         **
  542         **      JUMP  IF (SFBR != #target#)
  543         **      @(next tcb)
  544         */
  545 
  546         struct link   jump_tcb;
  547 
  548         /*
  549         **      load the actual values for the sxfer and the scntl3
  550         **      register (sync/wide mode).
  551         **
  552         **      SCR_COPY (1);
  553         **      @(sval field of this tcb)
  554         **      @(sxfer register)
  555         **      SCR_COPY (1);
  556         **      @(wval field of this tcb)
  557         **      @(scntl3 register)
  558         */
  559 
  560         ncrcmd  getscr[6];
  561 
  562         /*
  563         **      if next message is "identify"
  564         **      then load the message to SFBR,
  565         **      else load 0 to SFBR.
  566         **
  567         **      CALL
  568         **      <RESEL_LUN>
  569         */
  570 
  571         struct link   call_lun;
  572 
  573         /*
  574         **      now look for the right lun.
  575         **
  576         **      JUMP
  577         **      @(first ccb of this lun)
  578         */
  579 
  580         struct link   jump_lcb;
  581 
  582         /*
  583         **      pointer to interrupted getcc ccb
  584         */
  585 
  586         ccb_p   hold_cp;
  587 
  588         /*
  589         **      pointer to ccb used for negotiating.
  590         **      Avoid to start a nego for all queued commands 
  591         **      when tagged command queuing is enabled.
  592         */
  593 
  594         ccb_p   nego_cp;
  595 
  596         /*
  597         **      statistical data
  598         */
  599 
  600         u_long  transfers;
  601         u_long  bytes;
  602 
  603         /*
  604         **      user settable limits for sync transfer
  605         **      and tagged commands.
  606         */
  607 
  608         u_char  usrsync;
  609         u_char  usrtags;
  610         u_char  usrwide;
  611         u_char  usrflag;
  612 
  613         /*
  614         **      negotiation of wide and synch transfer.
  615         **      device quirks.
  616         */
  617 
  618 /**/   u_char  minsync;
  619 /*1*/   u_char  sval;
  620 /*2*/   u_short period;
  621 /**/   u_char  maxoffs;
  622 
  623 /*1*/   u_char  quirks;
  624 
  625 /*2*/   u_char  widedone;
  626 /*3*/   u_char  wval;
  627         /*
  628         **      inquire data
  629         */
  630 #define MAX_INQUIRE 36
  631         u_char  inqdata[MAX_INQUIRE];
  632 
  633         /*
  634         **      the lcb's of this tcb
  635         */
  636 
  637         lcb_p   lp[MAX_LUN];
  638 };
  639 
  640 /*==========================================================
  641 **
  642 **      Declaration of structs:         lun control block
  643 **
  644 **==========================================================
  645 */
  646 
  647 struct lcb {
  648         /*
  649         **      during reselection the ncr jumps to this point
  650         **      with SFBR set to the "Identify" message.
  651         **      if it's not this lun, jump to the next.
  652         **
  653         **      JUMP  IF (SFBR != #lun#)
  654         **      @(next lcb of this target)
  655         */
  656 
  657         struct link     jump_lcb;
  658 
  659         /*
  660         **      if next message is "simple tag",
  661         **      then load the tag to SFBR,
  662         **      else load 0 to SFBR.
  663         **
  664         **      CALL
  665         **      <RESEL_TAG>
  666         */
  667 
  668         struct link     call_tag;
  669 
  670         /*
  671         **      now look for the right ccb.
  672         **
  673         **      JUMP
  674         **      @(first ccb of this lun)
  675         */
  676 
  677         struct link     jump_ccb;
  678 
  679         /*
  680         **      start of the ccb chain
  681         */
  682 
  683         ccb_p   next_ccb;
  684 
  685         /*
  686         **      Control of tagged queueing
  687         */
  688 
  689         u_char          reqccbs;
  690         u_char          actccbs;
  691         u_char          reqlink;
  692         u_char          actlink;
  693         u_char          usetags;
  694         u_char          lasttag;
  695 };
  696 
  697 /*==========================================================
  698 **
  699 **      Declaration of structs:     COMMAND control block
  700 **
  701 **==========================================================
  702 **
  703 **      This substructure is copied from the ccb to a
  704 **      global address after selection (or reselection)
  705 **      and copied back before disconnect.
  706 **
  707 **      These fields are accessible to the script processor.
  708 **
  709 **----------------------------------------------------------
  710 */
  711 
  712 struct head {
  713         /*
  714         **      Execution of a ccb starts at this point.
  715         **      It's a jump to the "SELECT" label
  716         **      of the script.
  717         **
  718         **      After successful selection the script
  719         **      processor overwrites it with a jump to
  720         **      the IDLE label of the script.
  721         */
  722 
  723         struct link     launch;
  724 
  725         /*
  726         **      Saved data pointer.
  727         **      Points to the position in the script
  728         **      responsible for the actual transfer
  729         **      of data.
  730         **      It's written after reception of a
  731         **      "SAVE_DATA_POINTER" message.
  732         **      The goalpointer points after
  733         **      the last transfer command.
  734         */
  735 
  736         u_int32_t       savep;
  737         u_int32_t       lastp;
  738         u_int32_t       goalp;
  739 
  740         /*
  741         **      The virtual address of the ccb
  742         **      containing this header.
  743         */
  744 
  745         ccb_p   cp;
  746 
  747         /*
  748         **      space for some timestamps to gather
  749         **      profiling data about devices and this driver.
  750         */
  751 
  752         struct tstamp   stamp;
  753 
  754         /*
  755         **      status fields.
  756         */
  757 
  758         u_char          status[8];
  759 };
  760 
  761 /*
  762 **      The status bytes are used by the host and the script processor.
  763 **
  764 **      The first four byte are copied to the scratchb register
  765 **      (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
  766 **      and copied back just after disconnecting.
  767 **      Inside the script the XX_REG are used.
  768 **
  769 **      The last four bytes are used inside the script by "COPY" commands.
  770 **      Because source and destination must have the same alignment
  771 **      in a longword, the fields HAVE to be at the choosen offsets.
  772 **              xerr_st (4)     0       (0x34)  scratcha
  773 **              sync_st (5)     1       (0x05)  sxfer
  774 **              wide_st (7)     3       (0x03)  scntl3
  775 */
  776 
  777 /*
  778 **      First four bytes (script)
  779 */
  780 #define  QU_REG scr0
  781 #define  HS_REG scr1
  782 #define  HS_PRT nc_scr1
  783 #define  SS_REG scr2
  784 #define  PS_REG scr3
  785 
  786 /*
  787 **      First four bytes (host)
  788 */
  789 #define  actualquirks  phys.header.status[0]
  790 #define  host_status   phys.header.status[1]
  791 #define  scsi_status   phys.header.status[2]
  792 #define  parity_status phys.header.status[3]
  793 
  794 /*
  795 **      Last four bytes (script)
  796 */
  797 #define  xerr_st       header.status[4] /* MUST be ==0 mod 4 */
  798 #define  sync_st       header.status[5] /* MUST be ==1 mod 4 */
  799 #define  nego_st       header.status[6]
  800 #define  wide_st       header.status[7] /* MUST be ==3 mod 4 */
  801 
  802 /*
  803 **      Last four bytes (host)
  804 */
  805 #define  xerr_status   phys.xerr_st
  806 #define  sync_status   phys.sync_st
  807 #define  nego_status   phys.nego_st
  808 #define  wide_status   phys.wide_st
  809 
  810 /*==========================================================
  811 **
  812 **      Declaration of structs:     Data structure block
  813 **
  814 **==========================================================
  815 **
  816 **      During execution of a ccb by the script processor,
  817 **      the DSA (data structure address) register points
  818 **      to this substructure of the ccb.
  819 **      This substructure contains the header with
  820 **      the script-processor-changable data and
  821 **      data blocks for the indirect move commands.
  822 **
  823 **----------------------------------------------------------
  824 */
  825 
  826 struct dsb {
  827 
  828         /*
  829         **      Header.
  830         **      Has to be the first entry,
  831         **      because it's jumped to by the
  832         **      script processor
  833         */
  834 
  835         struct head     header;
  836 
  837         /*
  838         **      Table data for Script
  839         */
  840 
  841         struct scr_tblsel  select;
  842         struct scr_tblmove smsg  ;
  843         struct scr_tblmove smsg2 ;
  844         struct scr_tblmove cmd   ;
  845         struct scr_tblmove scmd  ;
  846         struct scr_tblmove sense ;
  847         struct scr_tblmove data [MAX_SCATTER];
  848 };
  849 
  850 /*==========================================================
  851 **
  852 **      Declaration of structs:     Command control block.
  853 **
  854 **==========================================================
  855 **
  856 **      During execution of a ccb by the script processor,
  857 **      the DSA (data structure address) register points
  858 **      to this substructure of the ccb.
  859 **      This substructure contains the header with
  860 **      the script-processor-changable data and then
  861 **      data blocks for the indirect move commands.
  862 **
  863 **----------------------------------------------------------
  864 */
  865 
  866 
  867 struct ccb {
  868         /*
  869         **      This filler ensures that the global header is 
  870         **      cache line size aligned.
  871         */
  872         ncrcmd  filler[4];
  873 
  874         /*
  875         **      during reselection the ncr jumps to this point.
  876         **      If a "SIMPLE_TAG" message was received,
  877         **      then SFBR is set to the tag.
  878         **      else SFBR is set to 0
  879         **      If looking for another tag, jump to the next ccb.
  880         **
  881         **      JUMP  IF (SFBR != #TAG#)
  882         **      @(next ccb of this lun)
  883         */
  884 
  885         struct link             jump_ccb;
  886 
  887         /*
  888         **      After execution of this call, the return address
  889         **      (in  the TEMP register) points to the following
  890         **      data structure block.
  891         **      So copy it to the DSA register, and start
  892         **      processing of this data structure.
  893         **
  894         **      CALL
  895         **      <RESEL_TMP>
  896         */
  897 
  898         struct link             call_tmp;
  899 
  900         /*
  901         **      This is the data structure which is
  902         **      to be executed by the script processor.
  903         */
  904 
  905         struct dsb              phys;
  906 
  907         /*
  908         **      If a data transfer phase is terminated too early
  909         **      (after reception of a message (i.e. DISCONNECT)),
  910         **      we have to prepare a mini script to transfer
  911         **      the rest of the data.
  912         */
  913 
  914         ncrcmd                  patch[8];
  915 
  916         /*
  917         **      The general SCSI driver provides a
  918         **      pointer to a control block.
  919         */
  920 
  921         struct scsi_xfer        *xfer;
  922 
  923         /*
  924         **      We prepare a message to be sent after selection,
  925         **      and a second one to be sent after getcc selection.
  926         **      Contents are IDENTIFY and SIMPLE_TAG.
  927         **      While negotiating sync or wide transfer,
  928         **      a SDTM or WDTM message is appended.
  929         */
  930 
  931         u_char                  scsi_smsg [8];
  932         u_char                  scsi_smsg2[8];
  933 
  934         /*
  935         **      Lock this ccb.
  936         **      Flag is used while looking for a free ccb.
  937         */
  938 
  939         u_long          magic;
  940 
  941         /*
  942         **      Physical address of this instance of ccb
  943         */
  944 
  945         u_long          p_ccb;
  946 
  947         /*
  948         **      Completion time out for this job.
  949         **      It's set to time of start + allowed number of seconds.
  950         */
  951 
  952         u_long          tlimit;
  953 
  954         /*
  955         **      All ccbs of one hostadapter are chained.
  956         */
  957 
  958         ccb_p           link_ccb;
  959 
  960         /*
  961         **      All ccbs of one target/lun are chained.
  962         */
  963 
  964         ccb_p           next_ccb;
  965 
  966         /*
  967         **      Sense command
  968         */
  969 
  970         u_char          sensecmd[6];
  971 
  972         /*
  973         **      Tag for this transfer.
  974         **      It's patched into jump_ccb.
  975         **      If it's not zero, a SIMPLE_TAG
  976         **      message is included in smsg.
  977         */
  978 
  979         u_char                  tag;
  980 };
  981 
  982 #define CCB_PHYS(cp,lbl)        (cp->p_ccb + offsetof(struct ccb, lbl))
  983 
  984 /*==========================================================
  985 **
  986 **      Declaration of structs:     NCR device descriptor
  987 **
  988 **==========================================================
  989 */
  990 
  991 struct ncb {
  992         /*
  993         **      The global header.
  994         **      Accessible to both the host and the
  995         **      script-processor.
  996         **      We assume it is cache line size aligned.
  997         */
  998         struct head     header;
  999 
 1000 #ifdef __NetBSD__
 1001         struct device sc_dev;
 1002         void *sc_ih;
 1003 #else /* !__NetBSD__ */
 1004         int     unit;
 1005 #endif /* __NetBSD__ */
 1006 
 1007         /*-----------------------------------------------
 1008         **      Scripts ..
 1009         **-----------------------------------------------
 1010         **
 1011         **      During reselection the ncr jumps to this point.
 1012         **      The SFBR register is loaded with the encoded target id.
 1013         **
 1014         **      Jump to the first target.
 1015         **
 1016         **      JUMP
 1017         **      @(next tcb)
 1018         */
 1019         struct link     jump_tcb;
 1020 
 1021         /*-----------------------------------------------
 1022         **      Configuration ..
 1023         **-----------------------------------------------
 1024         **
 1025         **      virtual and physical addresses
 1026         **      of the 53c810 chip.
 1027         */
 1028         vm_offset_t     vaddr;
 1029         vm_offset_t     paddr;
 1030 
 1031         vm_offset_t     vaddr2;
 1032         vm_offset_t     paddr2;
 1033 
 1034         /*
 1035         **      pointer to the chip's registers.
 1036         */
 1037         volatile
 1038         struct ncr_reg* reg;
 1039 
 1040         /*
 1041         **      Scripts instance virtual address.
 1042         */
 1043         struct script   *script;
 1044         struct scripth  *scripth;
 1045 
 1046         /*
 1047         **      Scripts instance physical address.
 1048         */
 1049         u_long          p_script;
 1050         u_long          p_scripth;
 1051 
 1052         /*
 1053         **      The SCSI address of the host adapter.
 1054         */
 1055         u_char          myaddr;
 1056 
 1057         /*
 1058         **      timing parameters
 1059         */
 1060         u_char          minsync;        /* Minimum sync period factor   */
 1061         u_char          maxsync;        /* Maximum sync period factor   */
 1062         u_char          maxoffs;        /* Max scsi offset              */
 1063         u_char          clock_divn;     /* Number of clock divisors     */
 1064         u_long          clock_khz;      /* SCSI clock frequency in KHz  */
 1065         u_long          features;       /* Chip features map            */
 1066         u_char          multiplier;     /* Clock multiplier (1,2,4)     */
 1067 
 1068         u_char          maxburst;       /* log base 2 of dwords burst   */
 1069 
 1070         /*
 1071         **      BIOS supplied PCI bus options
 1072         */
 1073         u_char          rv_scntl3;
 1074         u_char          rv_dcntl;
 1075         u_char          rv_dmode;
 1076         u_char          rv_ctest3;
 1077         u_char          rv_ctest4;
 1078         u_char          rv_ctest5;
 1079         u_char          rv_gpcntl;
 1080         u_char          rv_stest2;
 1081 
 1082         /*-----------------------------------------------
 1083         **      Link to the generic SCSI driver
 1084         **-----------------------------------------------
 1085         */
 1086 
 1087         struct scsi_link        sc_link;
 1088 
 1089         /*-----------------------------------------------
 1090         **      Job control
 1091         **-----------------------------------------------
 1092         **
 1093         **      Commands from user
 1094         */
 1095         struct usrcmd   user;
 1096         u_char          order;
 1097 
 1098         /*
 1099         **      Target data
 1100         */
 1101         struct tcb      target[MAX_TARGET];
 1102 
 1103         /*
 1104         **      Start queue.
 1105         */
 1106         u_int32_t       squeue [MAX_START];
 1107         u_short         squeueput;
 1108         u_short         actccbs;
 1109 
 1110         /*
 1111         **      Timeout handler
 1112         */
 1113         u_long          heartbeat;
 1114         u_short         ticks;
 1115         u_short         latetime;
 1116         u_long          lasttime;
 1117 
 1118         /*-----------------------------------------------
 1119         **      Debug and profiling
 1120         **-----------------------------------------------
 1121         **
 1122         **      register dump
 1123         */
 1124         struct ncr_reg  regdump;
 1125         struct timeval  regtime;
 1126 
 1127         /*
 1128         **      Profiling data
 1129         */
 1130         struct profile  profile;
 1131         u_long          disc_phys;
 1132         u_long          disc_ref;
 1133 
 1134         /*
 1135         **      The global control block.
 1136         **      It's used only during the configuration phase.
 1137         **      A target control block will be created
 1138         **      after the first successful transfer.
 1139         **      It is allocated separately in order to insure 
 1140         **      cache line size alignment.
 1141         */
 1142         struct ccb      *ccb;
 1143 
 1144         /*
 1145         **      message buffers.
 1146         **      Should be longword aligned,
 1147         **      because they're written with a
 1148         **      COPY script command.
 1149         */
 1150         u_char          msgout[8];
 1151         u_char          msgin [8];
 1152         u_int32_t       lastmsg;
 1153 
 1154         /*
 1155         **      Buffer for STATUS_IN phase.
 1156         */
 1157         u_char          scratch;
 1158 
 1159         /*
 1160         **      controller chip dependent maximal transfer width.
 1161         */
 1162         u_char          maxwide;
 1163 
 1164         /*
 1165         **      option for M_IDENTIFY message: enables disconnecting
 1166         */
 1167         u_char          disc;
 1168 
 1169 #ifdef NCR_IOMAPPED
 1170         /*
 1171         **      address of the ncr control registers in io space
 1172         */
 1173         u_short         port;
 1174 #endif
 1175 };
 1176 
 1177 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
 1178 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
 1179 
 1180 /*==========================================================
 1181 **
 1182 **
 1183 **      Script for NCR-Processor.
 1184 **
 1185 **      Use ncr_script_fill() to create the variable parts.
 1186 **      Use ncr_script_copy_and_bind() to make a copy and
 1187 **      bind to physical addresses.
 1188 **
 1189 **
 1190 **==========================================================
 1191 **
 1192 **      We have to know the offsets of all labels before
 1193 **      we reach them (for forward jumps).
 1194 **      Therefore we declare a struct here.
 1195 **      If you make changes inside the script,
 1196 **      DONT FORGET TO CHANGE THE LENGTHS HERE!
 1197 **
 1198 **----------------------------------------------------------
 1199 */
 1200 
 1201 /*
 1202 **      Script fragments which are loaded into the on-board RAM 
 1203 **      of 825A, 875 and 895 chips.
 1204 */
 1205 struct script {
 1206         ncrcmd  start           [  7];
 1207         ncrcmd  start0          [  2];
 1208         ncrcmd  start1          [  3];
 1209         ncrcmd  startpos        [  1];
 1210         ncrcmd  trysel          [  8];
 1211         ncrcmd  skip            [  8];
 1212         ncrcmd  skip2           [  3];
 1213         ncrcmd  idle            [  2];
 1214         ncrcmd  select          [ 22];
 1215         ncrcmd  prepare         [  4];
 1216         ncrcmd  loadpos         [ 14];
 1217         ncrcmd  prepare2        [ 24];
 1218         ncrcmd  setmsg          [  5];
 1219         ncrcmd  clrack          [  2];
 1220         ncrcmd  dispatch        [ 33];
 1221         ncrcmd  no_data         [ 17];
 1222         ncrcmd  checkatn        [ 10];
 1223         ncrcmd  command         [ 15];
 1224         ncrcmd  status          [ 27];
 1225         ncrcmd  msg_in          [ 26];
 1226         ncrcmd  msg_bad         [  6];
 1227         ncrcmd  complete        [ 13];
 1228         ncrcmd  cleanup         [ 12];
 1229         ncrcmd  cleanup0        [ 11];
 1230         ncrcmd  signal          [ 10];
 1231         ncrcmd  save_dp         [  5];
 1232         ncrcmd  restore_dp      [  5];
 1233         ncrcmd  disconnect      [ 12];
 1234         ncrcmd  disconnect0     [  5];
 1235         ncrcmd  disconnect1     [ 23];
 1236         ncrcmd  msg_out         [  9];
 1237         ncrcmd  msg_out_done    [  7];
 1238         ncrcmd  badgetcc        [  6];
 1239         ncrcmd  reselect        [  8];
 1240         ncrcmd  reselect1       [  8];
 1241         ncrcmd  reselect2       [  8];
 1242         ncrcmd  resel_tmp       [  5];
 1243         ncrcmd  resel_lun       [ 18];
 1244         ncrcmd  resel_tag       [ 24];
 1245         ncrcmd  data_in         [MAX_SCATTER * 4 + 7];
 1246         ncrcmd  data_out        [MAX_SCATTER * 4 + 7];
 1247 };
 1248 
 1249 /*
 1250 **      Script fragments which stay in main memory for all chips.
 1251 */
 1252 struct scripth {
 1253         ncrcmd  tryloop         [MAX_START*5+2];
 1254         ncrcmd  msg_parity      [  6];
 1255         ncrcmd  msg_reject      [  8];
 1256         ncrcmd  msg_ign_residue [ 32];
 1257         ncrcmd  msg_extended    [ 18];
 1258         ncrcmd  msg_ext_2       [ 18];
 1259         ncrcmd  msg_wdtr        [ 27];
 1260         ncrcmd  msg_ext_3       [ 18];
 1261         ncrcmd  msg_sdtr        [ 27];
 1262         ncrcmd  msg_out_abort   [ 10];
 1263         ncrcmd  getcc           [  4];
 1264         ncrcmd  getcc1          [  5];
 1265 #ifdef NCR_GETCC_WITHMSG
 1266         ncrcmd  getcc2          [ 33];
 1267 #else
 1268         ncrcmd  getcc2          [ 14];
 1269 #endif
 1270         ncrcmd  getcc3          [ 10];
 1271         ncrcmd  aborttag        [  4];
 1272         ncrcmd  abort           [ 22];
 1273         ncrcmd  snooptest       [  9];
 1274         ncrcmd  snoopend        [  2];
 1275 };
 1276 
 1277 /*==========================================================
 1278 **
 1279 **
 1280 **      Function headers.
 1281 **
 1282 **
 1283 **==========================================================
 1284 */
 1285 
 1286 #ifdef KERNEL
 1287 static  void    ncr_alloc_ccb   (ncb_p np, u_long target, u_long lun);
 1288 static  void    ncr_complete    (ncb_p np, ccb_p cp);
 1289 static  int     ncr_delta       (struct timeval * from, struct timeval * to);
 1290 static  void    ncr_exception   (ncb_p np);
 1291 static  void    ncr_free_ccb    (ncb_p np, ccb_p cp, int flags);
 1292 static  void    ncr_selectclock (ncb_p np, u_char scntl3);
 1293 static  void    ncr_getclock    (ncb_p np, u_char multiplier);
 1294 static  ccb_p   ncr_get_ccb     (ncb_p np, u_long flags, u_long t,u_long l);
 1295 static  u_int32_t ncr_info      (int unit);
 1296 static  void    ncr_init        (ncb_p np, char * msg, u_long code);
 1297 static  void    ncr_intr        (void *vnp);
 1298 static  void    ncr_int_ma      (ncb_p np, u_char dstat);
 1299 static  void    ncr_int_sir     (ncb_p np);
 1300 static  void    ncr_int_sto     (ncb_p np);
 1301 static  u_long  ncr_lookup      (char *id);
 1302 static  void    ncr_min_phys    (struct buf *bp);
 1303 static  void    ncr_negotiate   (struct ncb* np, struct tcb* tp);
 1304 static  void    ncr_opennings   (ncb_p np, lcb_p lp, struct scsi_xfer * xp);
 1305 static  void    ncb_profile     (ncb_p np, ccb_p cp);
 1306 static  void    ncr_script_copy_and_bind
 1307                                 (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
 1308 static  void    ncr_script_fill (struct script * scr, struct scripth *scrh);
 1309 static  int     ncr_scatter     (struct dsb* phys, vm_offset_t vaddr,
 1310                                  vm_size_t datalen);
 1311 static  void    ncr_setmaxtags  (tcb_p tp, u_long usrtags);
 1312 static  void    ncr_getsync     (ncb_p np, u_char sfac, u_char *fakp,
 1313                                  u_char *scntl3p);
 1314 static  void    ncr_setsync     (ncb_p np, ccb_p cp,u_char scntl3,u_char sxfer);
 1315 static  void    ncr_settags     (tcb_p tp, lcb_p lp);
 1316 static  void    ncr_setwide     (ncb_p np, ccb_p cp, u_char wide, u_char ack);
 1317 static  int     ncr_show_msg    (u_char * msg);
 1318 static  int     ncr_snooptest   (ncb_p np);
 1319 static  int32_t ncr_start       (struct scsi_xfer *xp);
 1320 static  void    ncr_timeout     (void *arg);
 1321 static  void    ncr_usercmd     (ncb_p np);
 1322 static  void    ncr_wakeup      (ncb_p np, u_long code);
 1323 
 1324 #ifdef __NetBSD__
 1325 static  int     ncr_probe       (struct device *, void *, void *);
 1326 static  void    ncr_attach      (struct device *, struct device *, void *);
 1327 #else /* !__NetBSD */
 1328 static  char*   ncr_probe       (pcici_t tag, pcidi_t type);
 1329 static  void    ncr_attach      (pcici_t tag, int unit);
 1330 #endif /* __NetBSD__ */
 1331 
 1332 #endif /* KERNEL */
 1333 
 1334 /*==========================================================
 1335 **
 1336 **
 1337 **      Global static data.
 1338 **
 1339 **
 1340 **==========================================================
 1341 */
 1342 
 1343 
 1344 static char ident[] =
 1345         "\n$FreeBSD: src/sys/pci/ncr.c,v 1.82.2.11 1999/09/05 08:21:18 peter Exp $\n";
 1346 
 1347 static const u_long     ncr_version = NCR_VERSION       * 11
 1348         + (u_long) sizeof (struct ncb)  *  7
 1349         + (u_long) sizeof (struct ccb)  *  5
 1350         + (u_long) sizeof (struct lcb)  *  3
 1351         + (u_long) sizeof (struct tcb)  *  2;
 1352 
 1353 #ifdef KERNEL
 1354 static const int nncr=MAX_UNITS;        /* XXX to be replaced by SYSCTL */
 1355 ncb_p         ncrp [MAX_UNITS];         /* XXX to be replaced by SYSCTL */
 1356 
 1357 static int ncr_debug = SCSI_NCR_DEBUG;
 1358 SYSCTL_INT(_debug, OID_AUTO, ncr_debug, CTLFLAG_RW, &ncr_debug, 0, "");
 1359 
 1360 static int ncr_cache; /* to be aligned _NOT_ static */
 1361 
 1362 /*==========================================================
 1363 **
 1364 **
 1365 **      Global static data:     auto configure
 1366 **
 1367 **
 1368 **==========================================================
 1369 */
 1370 
 1371 #define NCR_810_ID      (0x00011000ul)
 1372 #define NCR_815_ID      (0x00041000ul)
 1373 #define NCR_820_ID      (0x00021000ul)
 1374 #define NCR_825_ID      (0x00031000ul)
 1375 #define NCR_860_ID      (0x00061000ul)
 1376 #define NCR_875_ID      (0x000f1000ul)
 1377 #define NCR_875_ID2     (0x008f1000ul)
 1378 #define NCR_885_ID      (0x000d1000ul)
 1379 #define NCR_895_ID      (0x000c1000ul)
 1380 #define NCR_896_ID      (0x000b1000ul)
 1381 
 1382 #ifdef __NetBSD__
 1383 
 1384 struct  cfdriver ncrcd = {
 1385         NULL, "ncr", ncr_probe, ncr_attach, DV_DISK, sizeof(struct ncb)
 1386 };
 1387 
 1388 #else /* !__NetBSD__ */
 1389 
 1390 static u_long ncr_count;
 1391 
 1392 static struct   pci_device ncr_device = {
 1393         "ncr",
 1394         ncr_probe,
 1395         ncr_attach,
 1396         &ncr_count,
 1397         NULL
 1398 };
 1399 
 1400 DATA_SET (pcidevice_set, ncr_device);
 1401 
 1402 #endif /* !__NetBSD__ */
 1403 
 1404 static struct scsi_adapter ncr_switch =
 1405 {
 1406         ncr_start,
 1407         ncr_min_phys,
 1408         0,
 1409         0,
 1410 #ifndef __NetBSD__
 1411         ncr_info,
 1412         "ncr",
 1413 #endif /* !__NetBSD__ */
 1414 };
 1415 
 1416 static struct scsi_device ncr_dev =
 1417 {
 1418         NULL,                   /* Use default error handler */
 1419         NULL,                   /* have a queue, served by this */
 1420         NULL,                   /* have no async handler */
 1421         NULL,                   /* Use default 'done' routine */
 1422 #ifndef __NetBSD__
 1423         "ncr",
 1424 #endif /* !__NetBSD__ */
 1425 };
 1426 
 1427 #ifdef __NetBSD__
 1428 
 1429 #define ncr_name(np)    (np->sc_dev.dv_xname)
 1430 
 1431 #else /* !__NetBSD__ */
 1432 
 1433 static char *ncr_name (ncb_p np)
 1434 {
 1435         static char name[10];
 1436         sprintf(name, "ncr%d", np->unit);
 1437         return (name);
 1438 }
 1439 #endif
 1440 
 1441 /*==========================================================
 1442 **
 1443 **
 1444 **      Scripts for NCR-Processor.
 1445 **
 1446 **      Use ncr_script_bind for binding to physical addresses.
 1447 **
 1448 **
 1449 **==========================================================
 1450 **
 1451 **      NADDR generates a reference to a field of the controller data.
 1452 **      PADDR generates a reference to another part of the script.
 1453 **      RADDR generates a reference to a script processor register.
 1454 **      FADDR generates a reference to a script processor register
 1455 **              with offset.
 1456 **
 1457 **----------------------------------------------------------
 1458 */
 1459 
 1460 #define RELOC_SOFTC     0x40000000
 1461 #define RELOC_LABEL     0x50000000
 1462 #define RELOC_REGISTER  0x60000000
 1463 #define RELOC_KVAR      0x70000000
 1464 #define RELOC_LABELH    0x80000000
 1465 #define RELOC_MASK      0xf0000000
 1466 
 1467 #define NADDR(label)    (RELOC_SOFTC | offsetof(struct ncb, label))
 1468 #define PADDR(label)    (RELOC_LABEL | offsetof(struct script, label))
 1469 #define PADDRH(label)   (RELOC_LABELH | offsetof(struct scripth, label))
 1470 #define RADDR(label)    (RELOC_REGISTER | REG(label))
 1471 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
 1472 #define KVAR(which)     (RELOC_KVAR | (which))
 1473 
 1474 #define KVAR_TIME_TV_SEC                (0)
 1475 #define KVAR_TIME                       (1)
 1476 #define KVAR_NCR_CACHE                  (2)
 1477 
 1478 #define SCRIPT_KVAR_FIRST               (0)
 1479 #define SCRIPT_KVAR_LAST                (3)
 1480 
 1481 /*
 1482  * Kernel variables referenced in the scripts.
 1483  * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
 1484  */
 1485 static void *script_kvars[] =
 1486         { &time.tv_sec, &time, &ncr_cache };
 1487 
 1488 static  struct script script0 = {
 1489 /*--------------------------< START >-----------------------*/ {
 1490         /*
 1491         **      Claim to be still alive ...
 1492         */
 1493         SCR_COPY (sizeof (((struct ncb *)0)->heartbeat)),
 1494                 KVAR (KVAR_TIME_TV_SEC),
 1495                 NADDR (heartbeat),
 1496         /*
 1497         **      Make data structure address invalid.
 1498         **      clear SIGP.
 1499         */
 1500         SCR_LOAD_REG (dsa, 0xff),
 1501                 0,
 1502         SCR_FROM_REG (ctest2),
 1503                 0,
 1504 }/*-------------------------< START0 >----------------------*/,{
 1505         /*
 1506         **      Hook for interrupted GetConditionCode.
 1507         **      Will be patched to ... IFTRUE by
 1508         **      the interrupt handler.
 1509         */
 1510         SCR_INT ^ IFFALSE (0),
 1511                 SIR_SENSE_RESTART,
 1512 
 1513 }/*-------------------------< START1 >----------------------*/,{
 1514         /*
 1515         **      Hook for stalled start queue.
 1516         **      Will be patched to IFTRUE by the interrupt handler.
 1517         */
 1518         SCR_INT ^ IFFALSE (0),
 1519                 SIR_STALL_RESTART,
 1520         /*
 1521         **      Then jump to a certain point in tryloop.
 1522         **      Due to the lack of indirect addressing the code
 1523         **      is self modifying here.
 1524         */
 1525         SCR_JUMP,
 1526 }/*-------------------------< STARTPOS >--------------------*/,{
 1527                 PADDRH(tryloop),
 1528 
 1529 }/*-------------------------< TRYSEL >----------------------*/,{
 1530         /*
 1531         **      Now:
 1532         **      DSA: Address of a Data Structure
 1533         **      or   Address of the IDLE-Label.
 1534         **
 1535         **      TEMP:   Address of a script, which tries to
 1536         **              start the NEXT entry.
 1537         **
 1538         **      Save the TEMP register into the SCRATCHA register.
 1539         **      Then copy the DSA to TEMP and RETURN.
 1540         **      This is kind of an indirect jump.
 1541         **      (The script processor has NO stack, so the
 1542         **      CALL is actually a jump and link, and the
 1543         **      RETURN is an indirect jump.)
 1544         **
 1545         **      If the slot was empty, DSA contains the address
 1546         **      of the IDLE part of this script. The processor
 1547         **      jumps to IDLE and waits for a reselect.
 1548         **      It will wake up and try the same slot again
 1549         **      after the SIGP bit becomes set by the host.
 1550         **
 1551         **      If the slot was not empty, DSA contains
 1552         **      the address of the phys-part of a ccb.
 1553         **      The processor jumps to this address.
 1554         **      phys starts with head,
 1555         **      head starts with launch,
 1556         **      so actually the processor jumps to
 1557         **      the lauch part.
 1558         **      If the entry is scheduled for execution,
 1559         **      then launch contains a jump to SELECT.
 1560         **      If it's not scheduled, it contains a jump to IDLE.
 1561         */
 1562         SCR_COPY (4),
 1563                 RADDR (temp),
 1564                 RADDR (scratcha),
 1565         SCR_COPY (4),
 1566                 RADDR (dsa),
 1567                 RADDR (temp),
 1568         SCR_RETURN,
 1569                 0
 1570 
 1571 }/*-------------------------< SKIP >------------------------*/,{
 1572         /*
 1573         **      This entry has been canceled.
 1574         **      Next time use the next slot.
 1575         */
 1576         SCR_COPY (4),
 1577                 RADDR (scratcha),
 1578                 PADDR (startpos),
 1579         /*
 1580         **      patch the launch field.
 1581         **      should look like an idle process.
 1582         */
 1583         SCR_COPY_F (4),
 1584                 RADDR (dsa),
 1585                 PADDR (skip2),
 1586         SCR_COPY (8),
 1587                 PADDR (idle),
 1588 }/*-------------------------< SKIP2 >-----------------------*/,{
 1589                 0,
 1590         SCR_JUMP,
 1591                 PADDR(start),
 1592 }/*-------------------------< IDLE >------------------------*/,{
 1593         /*
 1594         **      Nothing to do?
 1595         **      Wait for reselect.
 1596         */
 1597         SCR_JUMP,
 1598                 PADDR(reselect),
 1599 
 1600 }/*-------------------------< SELECT >----------------------*/,{
 1601         /*
 1602         **      DSA     contains the address of a scheduled
 1603         **              data structure.
 1604         **
 1605         **      SCRATCHA contains the address of the script,
 1606         **              which starts the next entry.
 1607         **
 1608         **      Set Initiator mode.
 1609         **
 1610         **      (Target mode is left as an exercise for the reader)
 1611         */
 1612 
 1613         SCR_CLR (SCR_TRG),
 1614                 0,
 1615         SCR_LOAD_REG (HS_REG, 0xff),
 1616                 0,
 1617 
 1618         /*
 1619         **      And try to select this target.
 1620         */
 1621         SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
 1622                 PADDR (reselect),
 1623 
 1624         /*
 1625         **      Now there are 4 possibilities:
 1626         **
 1627         **      (1) The ncr looses arbitration.
 1628         **      This is ok, because it will try again,
 1629         **      when the bus becomes idle.
 1630         **      (But beware of the timeout function!)
 1631         **
 1632         **      (2) The ncr is reselected.
 1633         **      Then the script processor takes the jump
 1634         **      to the RESELECT label.
 1635         **
 1636         **      (3) The ncr completes the selection.
 1637         **      Then it will execute the next statement.
 1638         **
 1639         **      (4) There is a selection timeout.
 1640         **      Then the ncr should interrupt the host and stop.
 1641         **      Unfortunately, it seems to continue execution
 1642         **      of the script. But it will fail with an
 1643         **      IID-interrupt on the next WHEN.
 1644         */
 1645 
 1646         SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
 1647                 0,
 1648 
 1649         /*
 1650         **      Save target id to ctest0 register
 1651         */
 1652 
 1653         SCR_FROM_REG (sdid),
 1654                 0,
 1655         SCR_TO_REG (ctest0),
 1656                 0,
 1657         /*
 1658         **      Send the IDENTIFY and SIMPLE_TAG messages
 1659         **      (and the M_X_SYNC_REQ message)
 1660         */
 1661         SCR_MOVE_TBL ^ SCR_MSG_OUT,
 1662                 offsetof (struct dsb, smsg),
 1663 #ifdef undef /* XXX better fail than try to deal with this ... */
 1664         SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_OUT)),
 1665                 -16,
 1666 #endif
 1667         SCR_CLR (SCR_ATN),
 1668                 0,
 1669         SCR_COPY (1),
 1670                 RADDR (sfbr),
 1671                 NADDR (lastmsg),
 1672         /*
 1673         **      Selection complete.
 1674         **      Next time use the next slot.
 1675         */
 1676         SCR_COPY (4),
 1677                 RADDR (scratcha),
 1678                 PADDR (startpos),
 1679 }/*-------------------------< PREPARE >----------------------*/,{
 1680         /*
 1681         **      The ncr doesn't have an indirect load
 1682         **      or store command. So we have to
 1683         **      copy part of the control block to a
 1684         **      fixed place, where we can access it.
 1685         **
 1686         **      We patch the address part of a
 1687         **      COPY command with the DSA-register.
 1688         */
 1689         SCR_COPY_F (4),
 1690                 RADDR (dsa),
 1691                 PADDR (loadpos),
 1692         /*
 1693         **      then we do the actual copy.
 1694         */
 1695         SCR_COPY (sizeof (struct head)),
 1696         /*
 1697         **      continued after the next label ...
 1698         */
 1699 
 1700 }/*-------------------------< LOADPOS >---------------------*/,{
 1701                 0,
 1702                 NADDR (header),
 1703         /*
 1704         **      Mark this ccb as not scheduled.
 1705         */
 1706         SCR_COPY (8),
 1707                 PADDR (idle),
 1708                 NADDR (header.launch),
 1709         /*
 1710         **      Set a time stamp for this selection
 1711         */
 1712         SCR_COPY (sizeof (struct timeval)),
 1713                 KVAR (KVAR_TIME),
 1714                 NADDR (header.stamp.select),
 1715         /*
 1716         **      load the savep (saved pointer) into
 1717         **      the TEMP register (actual pointer)
 1718         */
 1719         SCR_COPY (4),
 1720                 NADDR (header.savep),
 1721                 RADDR (temp),
 1722         /*
 1723         **      Initialize the status registers
 1724         */
 1725         SCR_COPY (4),
 1726                 NADDR (header.status),
 1727                 RADDR (scr0),
 1728 
 1729 }/*-------------------------< PREPARE2 >---------------------*/,{
 1730         /*
 1731         **      Load the synchronous mode register
 1732         */
 1733         SCR_COPY (1),
 1734                 NADDR (sync_st),
 1735                 RADDR (sxfer),
 1736         /*
 1737         **      Load the wide mode and timing register
 1738         */
 1739         SCR_COPY (1),
 1740                 NADDR (wide_st),
 1741                 RADDR (scntl3),
 1742         /*
 1743         **      Initialize the msgout buffer with a NOOP message.
 1744         */
 1745         SCR_LOAD_REG (scratcha, M_NOOP),
 1746                 0,
 1747         SCR_COPY (1),
 1748                 RADDR (scratcha),
 1749                 NADDR (msgout),
 1750         SCR_COPY (1),
 1751                 RADDR (scratcha),
 1752                 NADDR (msgin),
 1753         /*
 1754         **      Message in phase ?
 1755         */
 1756         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 1757                 PADDR (dispatch),
 1758         /*
 1759         **      Extended or reject message ?
 1760         */
 1761         SCR_FROM_REG (sbdl),
 1762                 0,
 1763         SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
 1764                 PADDR (msg_in),
 1765         SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
 1766                 PADDRH (msg_reject),
 1767         /*
 1768         **      normal processing
 1769         */
 1770         SCR_JUMP,
 1771                 PADDR (dispatch),
 1772 }/*-------------------------< SETMSG >----------------------*/,{
 1773         SCR_COPY (1),
 1774                 RADDR (scratcha),
 1775                 NADDR (msgout),
 1776         SCR_SET (SCR_ATN),
 1777                 0,
 1778 }/*-------------------------< CLRACK >----------------------*/,{
 1779         /*
 1780         **      Terminate possible pending message phase.
 1781         */
 1782         SCR_CLR (SCR_ACK),
 1783                 0,
 1784 
 1785 }/*-----------------------< DISPATCH >----------------------*/,{
 1786         SCR_FROM_REG (HS_REG),
 1787                 0,
 1788         SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
 1789                 SIR_NEGO_FAILED,
 1790         /*
 1791         **      remove bogus output signals
 1792         */
 1793         SCR_REG_REG (socl, SCR_AND, CACK|CATN),
 1794                 0,
 1795         SCR_RETURN ^ IFTRUE (WHEN (SCR_DATA_OUT)),
 1796                 0,
 1797         SCR_RETURN ^ IFTRUE (IF (SCR_DATA_IN)),
 1798                 0,
 1799         SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
 1800                 PADDR (msg_out),
 1801         SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN)),
 1802                 PADDR (msg_in),
 1803         SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
 1804                 PADDR (command),
 1805         SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
 1806                 PADDR (status),
 1807         /*
 1808         **      Discard one illegal phase byte, if required.
 1809         */
 1810         SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
 1811                 0,
 1812         SCR_COPY (1),
 1813                 RADDR (scratcha),
 1814                 NADDR (xerr_st),
 1815         SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
 1816                 8,
 1817         SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
 1818                 NADDR (scratch),
 1819         SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
 1820                 8,
 1821         SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
 1822                 NADDR (scratch),
 1823         SCR_JUMP,
 1824                 PADDR (dispatch),
 1825 
 1826 }/*-------------------------< NO_DATA >--------------------*/,{
 1827         /*
 1828         **      The target wants to tranfer too much data
 1829         **      or in the wrong direction.
 1830         **      Remember that in extended error.
 1831         */
 1832         SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
 1833                 0,
 1834         SCR_COPY (1),
 1835                 RADDR (scratcha),
 1836                 NADDR (xerr_st),
 1837         /*
 1838         **      Discard one data byte, if required.
 1839         */
 1840         SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
 1841                 8,
 1842         SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
 1843                 NADDR (scratch),
 1844         SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
 1845                 8,
 1846         SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
 1847                 NADDR (scratch),
 1848         /*
 1849         **      .. and repeat as required.
 1850         */
 1851         SCR_CALL,
 1852                 PADDR (dispatch),
 1853         SCR_JUMP,
 1854                 PADDR (no_data),
 1855 }/*-------------------------< CHECKATN >--------------------*/,{
 1856         /*
 1857         **      If AAP (bit 1 of scntl0 register) is set
 1858         **      and a parity error is detected,
 1859         **      the script processor asserts ATN.
 1860         **
 1861         **      The target should switch to a MSG_OUT phase
 1862         **      to get the message.
 1863         */
 1864         SCR_FROM_REG (socl),
 1865                 0,
 1866         SCR_JUMP ^ IFFALSE (MASK (CATN, CATN)),
 1867                 PADDR (dispatch),
 1868         /*
 1869         **      count it
 1870         */
 1871         SCR_REG_REG (PS_REG, SCR_ADD, 1),
 1872                 0,
 1873         /*
 1874         **      Prepare a M_ID_ERROR message
 1875         **      (initiator detected error).
 1876         **      The target should retry the transfer.
 1877         */
 1878         SCR_LOAD_REG (scratcha, M_ID_ERROR),
 1879                 0,
 1880         SCR_JUMP,
 1881                 PADDR (setmsg),
 1882 
 1883 }/*-------------------------< COMMAND >--------------------*/,{
 1884         /*
 1885         **      If this is not a GETCC transfer ...
 1886         */
 1887         SCR_FROM_REG (SS_REG),
 1888                 0,
 1889 /*<<<*/ SCR_JUMPR ^ IFTRUE (DATA (S_CHECK_COND)),
 1890                 28,
 1891         /*
 1892         **      ... set a timestamp ...
 1893         */
 1894         SCR_COPY (sizeof (struct timeval)),
 1895                 KVAR (KVAR_TIME),
 1896                 NADDR (header.stamp.command),
 1897         /*
 1898         **      ... and send the command
 1899         */
 1900         SCR_MOVE_TBL ^ SCR_COMMAND,
 1901                 offsetof (struct dsb, cmd),
 1902         SCR_JUMP,
 1903                 PADDR (dispatch),
 1904         /*
 1905         **      Send the GETCC command
 1906         */
 1907 /*>>>*/ SCR_MOVE_TBL ^ SCR_COMMAND,
 1908                 offsetof (struct dsb, scmd),
 1909         SCR_JUMP,
 1910                 PADDR (dispatch),
 1911 
 1912 }/*-------------------------< STATUS >--------------------*/,{
 1913         /*
 1914         **      set the timestamp.
 1915         */
 1916         SCR_COPY (sizeof (struct timeval)),
 1917                 KVAR (KVAR_TIME),
 1918                 NADDR (header.stamp.status),
 1919         /*
 1920         **      If this is a GETCC transfer,
 1921         */
 1922         SCR_FROM_REG (SS_REG),
 1923                 0,
 1924 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (S_CHECK_COND)),
 1925                 40,
 1926         /*
 1927         **      get the status
 1928         */
 1929         SCR_MOVE_ABS (1) ^ SCR_STATUS,
 1930                 NADDR (scratch),
 1931         /*
 1932         **      Save status to scsi_status.
 1933         **      Mark as complete.
 1934         **      And wait for disconnect.
 1935         */
 1936         SCR_TO_REG (SS_REG),
 1937                 0,
 1938         SCR_REG_REG (SS_REG, SCR_OR, S_SENSE),
 1939                 0,
 1940         SCR_LOAD_REG (HS_REG, HS_COMPLETE),
 1941                 0,
 1942         SCR_JUMP,
 1943                 PADDR (checkatn),
 1944         /*
 1945         **      If it was no GETCC transfer,
 1946         **      save the status to scsi_status.
 1947         */
 1948 /*>>>*/ SCR_MOVE_ABS (1) ^ SCR_STATUS,
 1949                 NADDR (scratch),
 1950         SCR_TO_REG (SS_REG),
 1951                 0,
 1952         /*
 1953         **      if it was no check condition ...
 1954         */
 1955         SCR_JUMP ^ IFTRUE (DATA (S_CHECK_COND)),
 1956                 PADDR (checkatn),
 1957         /*
 1958         **      ... mark as complete.
 1959         */
 1960         SCR_LOAD_REG (HS_REG, HS_COMPLETE),
 1961                 0,
 1962         SCR_JUMP,
 1963                 PADDR (checkatn),
 1964 
 1965 }/*-------------------------< MSG_IN >--------------------*/,{
 1966         /*
 1967         **      Get the first byte of the message
 1968         **      and save it to SCRATCHA.
 1969         **
 1970         **      The script processor doesn't negate the
 1971         **      ACK signal after this transfer.
 1972         */
 1973         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 1974                 NADDR (msgin[0]),
 1975         /*
 1976         **      Check for message parity error.
 1977         */
 1978         SCR_TO_REG (scratcha),
 1979                 0,
 1980         SCR_FROM_REG (socl),
 1981                 0,
 1982         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 1983                 PADDRH (msg_parity),
 1984         SCR_FROM_REG (scratcha),
 1985                 0,
 1986         /*
 1987         **      Parity was ok, handle this message.
 1988         */
 1989         SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
 1990                 PADDR (complete),
 1991         SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
 1992                 PADDR (save_dp),
 1993         SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
 1994                 PADDR (restore_dp),
 1995         SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
 1996                 PADDR (disconnect),
 1997         SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
 1998                 PADDRH (msg_extended),
 1999         SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
 2000                 PADDR (clrack),
 2001         SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
 2002                 PADDRH (msg_reject),
 2003         SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
 2004                 PADDRH (msg_ign_residue),
 2005         /*
 2006         **      Rest of the messages left as
 2007         **      an exercise ...
 2008         **
 2009         **      Unimplemented messages:
 2010         **      fall through to MSG_BAD.
 2011         */
 2012 }/*-------------------------< MSG_BAD >------------------*/,{
 2013         /*
 2014         **      unimplemented message - reject it.
 2015         */
 2016         SCR_INT,
 2017                 SIR_REJECT_SENT,
 2018         SCR_LOAD_REG (scratcha, M_REJECT),
 2019                 0,
 2020         SCR_JUMP,
 2021                 PADDR (setmsg),
 2022 
 2023 }/*-------------------------< COMPLETE >-----------------*/,{
 2024         /*
 2025         **      Complete message.
 2026         **
 2027         **      If it's not the get condition code,
 2028         **      copy TEMP register to LASTP in header.
 2029         */
 2030         SCR_FROM_REG (SS_REG),
 2031                 0,
 2032 /*<<<*/ SCR_JUMPR ^ IFTRUE (MASK (S_SENSE, S_SENSE)),
 2033                 12,
 2034         SCR_COPY (4),
 2035                 RADDR (temp),
 2036                 NADDR (header.lastp),
 2037 /*>>>*/ /*
 2038         **      When we terminate the cycle by clearing ACK,
 2039         **      the target may disconnect immediately.
 2040         **
 2041         **      We don't want to be told of an
 2042         **      "unexpected disconnect",
 2043         **      so we disable this feature.
 2044         */
 2045         SCR_REG_REG (scntl2, SCR_AND, 0x7f),
 2046                 0,
 2047         /*
 2048         **      Terminate cycle ...
 2049         */
 2050         SCR_CLR (SCR_ACK|SCR_ATN),
 2051                 0,
 2052         /*
 2053         **      ... and wait for the disconnect.
 2054         */
 2055         SCR_WAIT_DISC,
 2056                 0,
 2057 }/*-------------------------< CLEANUP >-------------------*/,{
 2058         /*
 2059         **      dsa:    Pointer to ccb
 2060         **            or xxxxxxFF (no ccb)
 2061         **
 2062         **      HS_REG:   Host-Status (<>0!)
 2063         */
 2064         SCR_FROM_REG (dsa),
 2065                 0,
 2066         SCR_JUMP ^ IFTRUE (DATA (0xff)),
 2067                 PADDR (signal),
 2068         /*
 2069         **      dsa is valid.
 2070         **      save the status registers
 2071         */
 2072         SCR_COPY (4),
 2073                 RADDR (scr0),
 2074                 NADDR (header.status),
 2075         /*
 2076         **      and copy back the header to the ccb.
 2077         */
 2078         SCR_COPY_F (4),
 2079                 RADDR (dsa),
 2080                 PADDR (cleanup0),
 2081         SCR_COPY (sizeof (struct head)),
 2082                 NADDR (header),
 2083 }/*-------------------------< CLEANUP0 >--------------------*/,{
 2084                 0,
 2085 
 2086         /*
 2087         **      If command resulted in "check condition"
 2088         **      status and is not yet completed,
 2089         **      try to get the condition code.
 2090         */
 2091         SCR_FROM_REG (HS_REG),
 2092                 0,
 2093 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
 2094                 16,
 2095         SCR_FROM_REG (SS_REG),
 2096                 0,
 2097         SCR_JUMP ^ IFTRUE (DATA (S_CHECK_COND)),
 2098                 PADDRH(getcc2),
 2099         /*
 2100         **      And make the DSA register invalid.
 2101         */
 2102 /*>>>*/ SCR_LOAD_REG (dsa, 0xff), /* invalid */
 2103                 0,
 2104 }/*-------------------------< SIGNAL >----------------------*/,{
 2105         /*
 2106         **      if status = queue full,
 2107         **      reinsert in startqueue and stall queue.
 2108         */
 2109         SCR_FROM_REG (SS_REG),
 2110                 0,
 2111         SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
 2112                 SIR_STALL_QUEUE,
 2113         /*
 2114         **      if job completed ...
 2115         */
 2116         SCR_FROM_REG (HS_REG),
 2117                 0,
 2118         /*
 2119         **      ... signal completion to the host
 2120         */
 2121         SCR_INT_FLY ^ IFFALSE (MASK (0, HS_DONEMASK)),
 2122                 0,
 2123         /*
 2124         **      Auf zu neuen Schandtaten!
 2125         */
 2126         SCR_JUMP,
 2127                 PADDR(start),
 2128 
 2129 }/*-------------------------< SAVE_DP >------------------*/,{
 2130         /*
 2131         **      SAVE_DP message:
 2132         **      Copy TEMP register to SAVEP in header.
 2133         */
 2134         SCR_COPY (4),
 2135                 RADDR (temp),
 2136                 NADDR (header.savep),
 2137         SCR_JUMP,
 2138                 PADDR (clrack),
 2139 }/*-------------------------< RESTORE_DP >---------------*/,{
 2140         /*
 2141         **      RESTORE_DP message:
 2142         **      Copy SAVEP in header to TEMP register.
 2143         */
 2144         SCR_COPY (4),
 2145                 NADDR (header.savep),
 2146                 RADDR (temp),
 2147         SCR_JUMP,
 2148                 PADDR (clrack),
 2149 
 2150 }/*-------------------------< DISCONNECT >---------------*/,{
 2151         /*
 2152         **      If QUIRK_AUTOSAVE is set,
 2153         **      do an "save pointer" operation.
 2154         */
 2155         SCR_FROM_REG (QU_REG),
 2156                 0,
 2157 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
 2158                 12,
 2159         /*
 2160         **      like SAVE_DP message:
 2161         **      Copy TEMP register to SAVEP in header.
 2162         */
 2163         SCR_COPY (4),
 2164                 RADDR (temp),
 2165                 NADDR (header.savep),
 2166 /*>>>*/ /*
 2167         **      Check if temp==savep or temp==goalp:
 2168         **      if not, log a missing save pointer message.
 2169         **      In fact, it's a comparison mod 256.
 2170         **
 2171         **      Hmmm, I hadn't thought that I would be urged to
 2172         **      write this kind of ugly self modifying code.
 2173         **
 2174         **      It's unbelievable, but the ncr53c8xx isn't able
 2175         **      to subtract one register from another.
 2176         */
 2177         SCR_FROM_REG (temp),
 2178                 0,
 2179         /*
 2180         **      You are not expected to understand this ..
 2181         **
 2182         **      CAUTION: only little endian architectures supported! XXX
 2183         */
 2184         SCR_COPY_F (1),
 2185                 NADDR (header.savep),
 2186                 PADDR (disconnect0),
 2187 }/*-------------------------< DISCONNECT0 >--------------*/,{
 2188 /*<<<*/ SCR_JUMPR ^ IFTRUE (DATA (1)),
 2189                 20,
 2190         /*
 2191         **      neither this
 2192         */
 2193         SCR_COPY_F (1),
 2194                 NADDR (header.goalp),
 2195                 PADDR (disconnect1),
 2196 }/*-------------------------< DISCONNECT1 >--------------*/,{
 2197         SCR_INT ^ IFFALSE (DATA (1)),
 2198                 SIR_MISSING_SAVE,
 2199 /*>>>*/
 2200 
 2201         /*
 2202         **      DISCONNECTing  ...
 2203         **
 2204         **      disable the "unexpected disconnect" feature,
 2205         **      and remove the ACK signal.
 2206         */
 2207         SCR_REG_REG (scntl2, SCR_AND, 0x7f),
 2208                 0,
 2209         SCR_CLR (SCR_ACK|SCR_ATN),
 2210                 0,
 2211         /*
 2212         **      Wait for the disconnect.
 2213         */
 2214         SCR_WAIT_DISC,
 2215                 0,
 2216         /*
 2217         **      Profiling:
 2218         **      Set a time stamp,
 2219         **      and count the disconnects.
 2220         */
 2221         SCR_COPY (sizeof (struct timeval)),
 2222                 KVAR (KVAR_TIME),
 2223                 NADDR (header.stamp.disconnect),
 2224         SCR_COPY (4),
 2225                 NADDR (disc_phys),
 2226                 RADDR (temp),
 2227         SCR_REG_REG (temp, SCR_ADD, 0x01),
 2228                 0,
 2229         SCR_COPY (4),
 2230                 RADDR (temp),
 2231                 NADDR (disc_phys),
 2232         /*
 2233         **      Status is: DISCONNECTED.
 2234         */
 2235         SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
 2236                 0,
 2237         SCR_JUMP,
 2238                 PADDR (cleanup),
 2239 
 2240 }/*-------------------------< MSG_OUT >-------------------*/,{
 2241         /*
 2242         **      The target requests a message.
 2243         */
 2244         SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
 2245                 NADDR (msgout),
 2246         SCR_COPY (1),
 2247                 RADDR (sfbr),
 2248                 NADDR (lastmsg),
 2249         /*
 2250         **      If it was no ABORT message ...
 2251         */
 2252         SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
 2253                 PADDRH (msg_out_abort),
 2254         /*
 2255         **      ... wait for the next phase
 2256         **      if it's a message out, send it again, ...
 2257         */
 2258         SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
 2259                 PADDR (msg_out),
 2260 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
 2261         /*
 2262         **      ... else clear the message ...
 2263         */
 2264         SCR_LOAD_REG (scratcha, M_NOOP),
 2265                 0,
 2266         SCR_COPY (4),
 2267                 RADDR (scratcha),
 2268                 NADDR (msgout),
 2269         /*
 2270         **      ... and process the next phase
 2271         */
 2272         SCR_JUMP,
 2273                 PADDR (dispatch),
 2274 
 2275 }/*------------------------< BADGETCC >---------------------*/,{
 2276         /*
 2277         **      If SIGP was set, clear it and try again.
 2278         */
 2279         SCR_FROM_REG (ctest2),
 2280                 0,
 2281         SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
 2282                 PADDRH (getcc2),
 2283         SCR_INT,
 2284                 SIR_SENSE_FAILED,
 2285 }/*-------------------------< RESELECT >--------------------*/,{
 2286         /*
 2287         **      This NOP will be patched with LED OFF
 2288         **      SCR_REG_REG (gpreg, SCR_OR, 0x01)
 2289         */
 2290         SCR_NO_OP,
 2291                 0,
 2292 
 2293         /*
 2294         **      make the DSA invalid.
 2295         */
 2296         SCR_LOAD_REG (dsa, 0xff),
 2297                 0,
 2298         SCR_CLR (SCR_TRG),
 2299                 0,
 2300         /*
 2301         **      Sleep waiting for a reselection.
 2302         **      If SIGP is set, special treatment.
 2303         **
 2304         **      Zu allem bereit ..
 2305         */
 2306         SCR_WAIT_RESEL,
 2307                 PADDR(reselect2),
 2308 }/*-------------------------< RESELECT1 >--------------------*/,{
 2309         /*
 2310         **      This NOP will be patched with LED ON
 2311         **      SCR_REG_REG (gpreg, SCR_AND, 0xfe)
 2312         */
 2313         SCR_NO_OP,
 2314                 0,
 2315         /*
 2316         **      ... zu nichts zu gebrauchen ?
 2317         **
 2318         **      load the target id into the SFBR
 2319         **      and jump to the control block.
 2320         **
 2321         **      Look at the declarations of
 2322         **      - struct ncb
 2323         **      - struct tcb
 2324         **      - struct lcb
 2325         **      - struct ccb
 2326         **      to understand what's going on.
 2327         */
 2328         SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
 2329                 0,
 2330         SCR_TO_REG (ctest0),
 2331                 0,
 2332         SCR_JUMP,
 2333                 NADDR (jump_tcb),
 2334 }/*-------------------------< RESELECT2 >-------------------*/,{
 2335         /*
 2336         **      This NOP will be patched with LED ON
 2337         **      SCR_REG_REG (gpreg, SCR_AND, 0xfe)
 2338         */
 2339         SCR_NO_OP,
 2340                 0,
 2341         /*
 2342         **      If it's not connected :(
 2343         **      -> interrupted by SIGP bit.
 2344         **      Jump to start.
 2345         */
 2346         SCR_FROM_REG (ctest2),
 2347                 0,
 2348         SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
 2349                 PADDR (start),
 2350         SCR_JUMP,
 2351                 PADDR (reselect),
 2352 
 2353 }/*-------------------------< RESEL_TMP >-------------------*/,{
 2354         /*
 2355         **      The return address in TEMP
 2356         **      is in fact the data structure address,
 2357         **      so copy it to the DSA register.
 2358         */
 2359         SCR_COPY (4),
 2360                 RADDR (temp),
 2361                 RADDR (dsa),
 2362         SCR_JUMP,
 2363                 PADDR (prepare),
 2364 
 2365 }/*-------------------------< RESEL_LUN >-------------------*/,{
 2366         /*
 2367         **      come back to this point
 2368         **      to get an IDENTIFY message
 2369         **      Wait for a msg_in phase.
 2370         */
 2371 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2372                 48,
 2373         /*
 2374         **      message phase
 2375         **      It's not a sony, it's a trick:
 2376         **      read the data without acknowledging it.
 2377         */
 2378         SCR_FROM_REG (sbdl),
 2379                 0,
 2380 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (M_IDENTIFY, 0x98)),
 2381                 32,
 2382         /*
 2383         **      It WAS an Identify message.
 2384         **      get it and ack it!
 2385         */
 2386         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2387                 NADDR (msgin),
 2388         SCR_CLR (SCR_ACK),
 2389                 0,
 2390         /*
 2391         **      Mask out the lun.
 2392         */
 2393         SCR_REG_REG (sfbr, SCR_AND, 0x07),
 2394                 0,
 2395         SCR_RETURN,
 2396                 0,
 2397         /*
 2398         **      No message phase or no IDENTIFY message:
 2399         **      return 0.
 2400         */
 2401 /*>>>*/ SCR_LOAD_SFBR (0),
 2402                 0,
 2403         SCR_RETURN,
 2404                 0,
 2405 
 2406 }/*-------------------------< RESEL_TAG >-------------------*/,{
 2407         /*
 2408         **      come back to this point
 2409         **      to get a SIMPLE_TAG message
 2410         **      Wait for a MSG_IN phase.
 2411         */
 2412 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2413                 64,
 2414         /*
 2415         **      message phase
 2416         **      It's a trick - read the data
 2417         **      without acknowledging it.
 2418         */
 2419         SCR_FROM_REG (sbdl),
 2420                 0,
 2421 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (M_SIMPLE_TAG)),
 2422                 48,
 2423         /*
 2424         **      It WAS a SIMPLE_TAG message.
 2425         **      get it and ack it!
 2426         */
 2427         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2428                 NADDR (msgin),
 2429         SCR_CLR (SCR_ACK),
 2430                 0,
 2431         /*
 2432         **      Wait for the second byte (the tag)
 2433         */
 2434 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2435                 24,
 2436         /*
 2437         **      Get it and ack it!
 2438         */
 2439         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2440                 NADDR (msgin),
 2441         SCR_CLR (SCR_ACK|SCR_CARRY),
 2442                 0,
 2443         SCR_RETURN,
 2444                 0,
 2445         /*
 2446         **      No message phase or no SIMPLE_TAG message
 2447         **      or no second byte: return 0.
 2448         */
 2449 /*>>>*/ SCR_LOAD_SFBR (0),
 2450                 0,
 2451         SCR_SET (SCR_CARRY),
 2452                 0,
 2453         SCR_RETURN,
 2454                 0,
 2455 
 2456 }/*-------------------------< DATA_IN >--------------------*/,{
 2457 /*
 2458 **      Because the size depends on the
 2459 **      #define MAX_SCATTER parameter,
 2460 **      it is filled in at runtime.
 2461 **
 2462 **      SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
 2463 **              PADDR (no_data),
 2464 **      SCR_COPY (sizeof (struct timeval)),
 2465 **              KVAR (KVAR_TIME),
 2466 **              NADDR (header.stamp.data),
 2467 **      SCR_MOVE_TBL ^ SCR_DATA_IN,
 2468 **              offsetof (struct dsb, data[ 0]),
 2469 **
 2470 **  ##===========< i=1; i<MAX_SCATTER >=========
 2471 **  ||  SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
 2472 **  ||          PADDR (checkatn),
 2473 **  ||  SCR_MOVE_TBL ^ SCR_DATA_IN,
 2474 **  ||          offsetof (struct dsb, data[ i]),
 2475 **  ##==========================================
 2476 **
 2477 **      SCR_CALL,
 2478 **              PADDR (checkatn),
 2479 **      SCR_JUMP,
 2480 **              PADDR (no_data),
 2481 */
 2482 
 2483 }/*-------------------------< DATA_OUT >-------------------*/,{
 2484 /*
 2485 **      Because the size depends on the
 2486 **      #define MAX_SCATTER parameter,
 2487 **      it is filled in at runtime.
 2488 **
 2489 **      SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT)),
 2490 **              PADDR (no_data),
 2491 **      SCR_COPY (sizeof (struct timeval)),
 2492 **              KVAR (KVAR_TIME),
 2493 **              NADDR (header.stamp.data),
 2494 **      SCR_MOVE_TBL ^ SCR_DATA_OUT,
 2495 **              offsetof (struct dsb, data[ 0]),
 2496 **
 2497 **  ##===========< i=1; i<MAX_SCATTER >=========
 2498 **  ||  SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
 2499 **  ||          PADDR (dispatch),
 2500 **  ||  SCR_MOVE_TBL ^ SCR_DATA_OUT,
 2501 **  ||          offsetof (struct dsb, data[ i]),
 2502 **  ##==========================================
 2503 **
 2504 **      SCR_CALL,
 2505 **              PADDR (dispatch),
 2506 **      SCR_JUMP,
 2507 **              PADDR (no_data),
 2508 **
 2509 **---------------------------------------------------------
 2510 */
 2511 (u_long)&ident
 2512 
 2513 }/*--------------------------------------------------------*/
 2514 };
 2515 
 2516 
 2517 static  struct scripth scripth0 = {
 2518 /*-------------------------< TRYLOOP >---------------------*/{
 2519 /*
 2520 **      Load an entry of the start queue into dsa
 2521 **      and try to start it by jumping to TRYSEL.
 2522 **
 2523 **      Because the size depends on the
 2524 **      #define MAX_START parameter, it is filled
 2525 **      in at runtime.
 2526 **
 2527 **-----------------------------------------------------------
 2528 **
 2529 **  ##===========< I=0; i<MAX_START >===========
 2530 **  ||  SCR_COPY (4),
 2531 **  ||          NADDR (squeue[i]),
 2532 **  ||          RADDR (dsa),
 2533 **  ||  SCR_CALL,
 2534 **  ||          PADDR (trysel),
 2535 **  ##==========================================
 2536 **
 2537 **      SCR_JUMP,
 2538 **              PADDRH(tryloop),
 2539 **
 2540 **-----------------------------------------------------------
 2541 */
 2542 
 2543 }/*-------------------------< MSG_PARITY >---------------*/,{
 2544         /*
 2545         **      count it
 2546         */
 2547         SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
 2548                 0,
 2549         /*
 2550         **      send a "message parity error" message.
 2551         */
 2552         SCR_LOAD_REG (scratcha, M_PARITY),
 2553                 0,
 2554         SCR_JUMP,
 2555                 PADDR (setmsg),
 2556 }/*-------------------------< MSG_REJECT >---------------*/,{
 2557         /*
 2558         **      If a negotiation was in progress,
 2559         **      negotiation failed.
 2560         */
 2561         SCR_FROM_REG (HS_REG),
 2562                 0,
 2563         SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
 2564                 SIR_NEGO_FAILED,
 2565         /*
 2566         **      else make host log this message
 2567         */
 2568         SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
 2569                 SIR_REJECT_RECEIVED,
 2570         SCR_JUMP,
 2571                 PADDR (clrack),
 2572 
 2573 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
 2574         /*
 2575         **      Terminate cycle
 2576         */
 2577         SCR_CLR (SCR_ACK),
 2578                 0,
 2579         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2580                 PADDR (dispatch),
 2581         /*
 2582         **      get residue size.
 2583         */
 2584         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2585                 NADDR (msgin[1]),
 2586         /*
 2587         **      Check for message parity error.
 2588         */
 2589         SCR_TO_REG (scratcha),
 2590                 0,
 2591         SCR_FROM_REG (socl),
 2592                 0,
 2593         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 2594                 PADDRH (msg_parity),
 2595         SCR_FROM_REG (scratcha),
 2596                 0,
 2597         /*
 2598         **      Size is 0 .. ignore message.
 2599         */
 2600         SCR_JUMP ^ IFTRUE (DATA (0)),
 2601                 PADDR (clrack),
 2602         /*
 2603         **      Size is not 1 .. have to interrupt.
 2604         */
 2605 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (1)),
 2606                 40,
 2607         /*
 2608         **      Check for residue byte in swide register
 2609         */
 2610         SCR_FROM_REG (scntl2),
 2611                 0,
 2612 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
 2613                 16,
 2614         /*
 2615         **      There IS data in the swide register.
 2616         **      Discard it.
 2617         */
 2618         SCR_REG_REG (scntl2, SCR_OR, WSR),
 2619                 0,
 2620         SCR_JUMP,
 2621                 PADDR (clrack),
 2622         /*
 2623         **      Load again the size to the sfbr register.
 2624         */
 2625 /*>>>*/ SCR_FROM_REG (scratcha),
 2626                 0,
 2627 /*>>>*/ SCR_INT,
 2628                 SIR_IGN_RESIDUE,
 2629         SCR_JUMP,
 2630                 PADDR (clrack),
 2631 
 2632 }/*-------------------------< MSG_EXTENDED >-------------*/,{
 2633         /*
 2634         **      Terminate cycle
 2635         */
 2636         SCR_CLR (SCR_ACK),
 2637                 0,
 2638         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2639                 PADDR (dispatch),
 2640         /*
 2641         **      get length.
 2642         */
 2643         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2644                 NADDR (msgin[1]),
 2645         /*
 2646         **      Check for message parity error.
 2647         */
 2648         SCR_TO_REG (scratcha),
 2649                 0,
 2650         SCR_FROM_REG (socl),
 2651                 0,
 2652         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 2653                 PADDRH (msg_parity),
 2654         SCR_FROM_REG (scratcha),
 2655                 0,
 2656         /*
 2657         */
 2658         SCR_JUMP ^ IFTRUE (DATA (3)),
 2659                 PADDRH (msg_ext_3),
 2660         SCR_JUMP ^ IFFALSE (DATA (2)),
 2661                 PADDR (msg_bad),
 2662 }/*-------------------------< MSG_EXT_2 >----------------*/,{
 2663         SCR_CLR (SCR_ACK),
 2664                 0,
 2665         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2666                 PADDR (dispatch),
 2667         /*
 2668         **      get extended message code.
 2669         */
 2670         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2671                 NADDR (msgin[2]),
 2672         /*
 2673         **      Check for message parity error.
 2674         */
 2675         SCR_TO_REG (scratcha),
 2676                 0,
 2677         SCR_FROM_REG (socl),
 2678                 0,
 2679         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 2680                 PADDRH (msg_parity),
 2681         SCR_FROM_REG (scratcha),
 2682                 0,
 2683         SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
 2684                 PADDRH (msg_wdtr),
 2685         /*
 2686         **      unknown extended message
 2687         */
 2688         SCR_JUMP,
 2689                 PADDR (msg_bad)
 2690 }/*-------------------------< MSG_WDTR >-----------------*/,{
 2691         SCR_CLR (SCR_ACK),
 2692                 0,
 2693         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2694                 PADDR (dispatch),
 2695         /*
 2696         **      get data bus width
 2697         */
 2698         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2699                 NADDR (msgin[3]),
 2700         SCR_FROM_REG (socl),
 2701                 0,
 2702         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 2703                 PADDRH (msg_parity),
 2704         /*
 2705         **      let the host do the real work.
 2706         */
 2707         SCR_INT,
 2708                 SIR_NEGO_WIDE,
 2709         /*
 2710         **      let the target fetch our answer.
 2711         */
 2712         SCR_SET (SCR_ATN),
 2713                 0,
 2714         SCR_CLR (SCR_ACK),
 2715                 0,
 2716 
 2717         SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
 2718                 SIR_NEGO_PROTO,
 2719         /*
 2720         **      Send the M_X_WIDE_REQ
 2721         */
 2722         SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
 2723                 NADDR (msgout),
 2724         SCR_CLR (SCR_ATN),
 2725                 0,
 2726         SCR_COPY (1),
 2727                 RADDR (sfbr),
 2728                 NADDR (lastmsg),
 2729         SCR_JUMP,
 2730                 PADDR (msg_out_done),
 2731 
 2732 }/*-------------------------< MSG_EXT_3 >----------------*/,{
 2733         SCR_CLR (SCR_ACK),
 2734                 0,
 2735         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2736                 PADDR (dispatch),
 2737         /*
 2738         **      get extended message code.
 2739         */
 2740         SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
 2741                 NADDR (msgin[2]),
 2742         /*
 2743         **      Check for message parity error.
 2744         */
 2745         SCR_TO_REG (scratcha),
 2746                 0,
 2747         SCR_FROM_REG (socl),
 2748                 0,
 2749         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 2750                 PADDRH (msg_parity),
 2751         SCR_FROM_REG (scratcha),
 2752                 0,
 2753         SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
 2754                 PADDRH (msg_sdtr),
 2755         /*
 2756         **      unknown extended message
 2757         */
 2758         SCR_JUMP,
 2759                 PADDR (msg_bad)
 2760 
 2761 }/*-------------------------< MSG_SDTR >-----------------*/,{
 2762         SCR_CLR (SCR_ACK),
 2763                 0,
 2764         SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
 2765                 PADDR (dispatch),
 2766         /*
 2767         **      get period and offset
 2768         */
 2769         SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
 2770                 NADDR (msgin[3]),
 2771         SCR_FROM_REG (socl),
 2772                 0,
 2773         SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
 2774                 PADDRH (msg_parity),
 2775         /*
 2776         **      let the host do the real work.
 2777         */
 2778         SCR_INT,
 2779                 SIR_NEGO_SYNC,
 2780         /*
 2781         **      let the target fetch our answer.
 2782         */
 2783         SCR_SET (SCR_ATN),
 2784                 0,
 2785         SCR_CLR (SCR_ACK),
 2786                 0,
 2787 
 2788         SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
 2789                 SIR_NEGO_PROTO,
 2790         /*
 2791         **      Send the M_X_SYNC_REQ
 2792         */
 2793         SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
 2794                 NADDR (msgout),
 2795         SCR_CLR (SCR_ATN),
 2796                 0,
 2797         SCR_COPY (1),
 2798                 RADDR (sfbr),
 2799                 NADDR (lastmsg),
 2800         SCR_JUMP,
 2801                 PADDR (msg_out_done),
 2802 
 2803 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
 2804         /*
 2805         **      After ABORT message,
 2806         **
 2807         **      expect an immediate disconnect, ...
 2808         */
 2809         SCR_REG_REG (scntl2, SCR_AND, 0x7f),
 2810                 0,
 2811         SCR_CLR (SCR_ACK|SCR_ATN),
 2812                 0,
 2813         SCR_WAIT_DISC,
 2814                 0,
 2815         /*
 2816         **      ... and set the status to "ABORTED"
 2817         */
 2818         SCR_LOAD_REG (HS_REG, HS_ABORTED),
 2819                 0,
 2820         SCR_JUMP,
 2821                 PADDR (cleanup),
 2822 
 2823 }/*-------------------------< GETCC >-----------------------*/,{
 2824         /*
 2825         **      The ncr doesn't have an indirect load
 2826         **      or store command. So we have to
 2827         **      copy part of the control block to a
 2828         **      fixed place, where we can modify it.
 2829         **
 2830         **      We patch the address part of a COPY command
 2831         **      with the address of the dsa register ...
 2832         */
 2833         SCR_COPY_F (4),
 2834                 RADDR (dsa),
 2835                 PADDRH (getcc1),
 2836         /*
 2837         **      ... then we do the actual copy.
 2838         */
 2839         SCR_COPY (sizeof (struct head)),
 2840 }/*-------------------------< GETCC1 >----------------------*/,{
 2841                 0,
 2842                 NADDR (header),
 2843         /*
 2844         **      Initialize the status registers
 2845         */
 2846         SCR_COPY (4),
 2847                 NADDR (header.status),
 2848                 RADDR (scr0),
 2849 }/*-------------------------< GETCC2 >----------------------*/,{
 2850         /*
 2851         **      Get the condition code from a target.
 2852         **
 2853         **      DSA points to a data structure.
 2854         **      Set TEMP to the script location
 2855         **      that receives the condition code.
 2856         **
 2857         **      Because there is no script command
 2858         **      to load a longword into a register,
 2859         **      we use a CALL command.
 2860         */
 2861 /*<<<*/ SCR_CALLR,
 2862                 24,
 2863         /*
 2864         **      Get the condition code.
 2865         */
 2866         SCR_MOVE_TBL ^ SCR_DATA_IN,
 2867                 offsetof (struct dsb, sense),
 2868         /*
 2869         **      No data phase may follow!
 2870         */
 2871         SCR_CALL,
 2872                 PADDR (checkatn),
 2873         SCR_JUMP,
 2874                 PADDR (no_data),
 2875 /*>>>*/
 2876 
 2877         /*
 2878         **      The CALL jumps to this point.
 2879         **      Prepare for a RESTORE_POINTER message.
 2880         **      Save the TEMP register into the saved pointer.
 2881         */
 2882         SCR_COPY (4),
 2883                 RADDR (temp),
 2884                 NADDR (header.savep),
 2885         /*
 2886         **      Load scratcha, because in case of a selection timeout,
 2887         **      the host will expect a new value for startpos in
 2888         **      the scratcha register.
 2889         */
 2890         SCR_COPY (4),
 2891                 PADDR (startpos),
 2892                 RADDR (scratcha),
 2893 #ifdef NCR_GETCC_WITHMSG
 2894         /*
 2895         **      If QUIRK_NOMSG is set, select without ATN.
 2896         **      and don't send a message.
 2897         */
 2898         SCR_FROM_REG (QU_REG),
 2899                 0,
 2900         SCR_JUMP ^ IFTRUE (MASK (QUIRK_NOMSG, QUIRK_NOMSG)),
 2901                 PADDRH(getcc3),
 2902         /*
 2903         **      Then try to connect to the target.
 2904         **      If we are reselected, special treatment
 2905         **      of the current job is required before
 2906         **      accepting the reselection.
 2907         */
 2908         SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
 2909                 PADDR(badgetcc),
 2910         /*
 2911         **      save target id.
 2912         */
 2913         SCR_FROM_REG (sdid),
 2914                 0,
 2915         SCR_TO_REG (ctest0),
 2916                 0,
 2917         /*
 2918         **      Send the IDENTIFY message.
 2919         **      In case of short transfer, remove ATN.
 2920         */
 2921         SCR_MOVE_TBL ^ SCR_MSG_OUT,
 2922                 offsetof (struct dsb, smsg2),
 2923         SCR_CLR (SCR_ATN),
 2924                 0,
 2925         /*
 2926         **      save the first byte of the message.
 2927         */
 2928         SCR_COPY (1),
 2929                 RADDR (sfbr),
 2930                 NADDR (lastmsg),
 2931         SCR_JUMP,
 2932                 PADDR (prepare2),
 2933 
 2934 #endif
 2935 }/*-------------------------< GETCC3 >----------------------*/,{
 2936         /*
 2937         **      Try to connect to the target.
 2938         **      If we are reselected, special treatment
 2939         **      of the current job is required before
 2940         **      accepting the reselection.
 2941         **
 2942         **      Silly target won't accept a message.
 2943         **      Select without ATN.
 2944         */
 2945         SCR_SEL_TBL ^ offsetof (struct dsb, select),
 2946                 PADDR(badgetcc),
 2947         /*
 2948         **      save target id.
 2949         */
 2950         SCR_FROM_REG (sdid),
 2951                 0,
 2952         SCR_TO_REG (ctest0),
 2953                 0,
 2954         /*
 2955         **      Force error if selection timeout
 2956         */
 2957         SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
 2958                 0,
 2959         /*
 2960         **      don't negotiate.
 2961         */
 2962         SCR_JUMP,
 2963                 PADDR (prepare2),
 2964 }/*-------------------------< ABORTTAG >-------------------*/,{
 2965         /*
 2966         **      Abort a bad reselection.
 2967         **      Set the message to ABORT vs. ABORT_TAG
 2968         */
 2969         SCR_LOAD_REG (scratcha, M_ABORT_TAG),
 2970                 0,
 2971         SCR_JUMPR ^ IFFALSE (CARRYSET),
 2972                 8,
 2973 }/*-------------------------< ABORT >----------------------*/,{
 2974         SCR_LOAD_REG (scratcha, M_ABORT),
 2975                 0,
 2976         SCR_COPY (1),
 2977                 RADDR (scratcha),
 2978                 NADDR (msgout),
 2979         SCR_SET (SCR_ATN),
 2980                 0,
 2981         SCR_CLR (SCR_ACK),
 2982                 0,
 2983         /*
 2984         **      and send it.
 2985         **      we expect an immediate disconnect
 2986         */
 2987         SCR_REG_REG (scntl2, SCR_AND, 0x7f),
 2988                 0,
 2989         SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
 2990                 NADDR (msgout),
 2991         SCR_COPY (1),
 2992                 RADDR (sfbr),
 2993                 NADDR (lastmsg),
 2994         SCR_CLR (SCR_ACK|SCR_ATN),
 2995                 0,
 2996         SCR_WAIT_DISC,
 2997                 0,
 2998         SCR_JUMP,
 2999                 PADDR (start),
 3000 }/*-------------------------< SNOOPTEST >-------------------*/,{
 3001         /*
 3002         **      Read the variable.
 3003         */
 3004         SCR_COPY (4),
 3005                 KVAR (KVAR_NCR_CACHE),
 3006                 RADDR (scratcha),
 3007         /*
 3008         **      Write the variable.
 3009         */
 3010         SCR_COPY (4),
 3011                 RADDR (temp),
 3012                 KVAR (KVAR_NCR_CACHE),
 3013         /*
 3014         **      Read back the variable.
 3015         */
 3016         SCR_COPY (4),
 3017                 KVAR (KVAR_NCR_CACHE),
 3018                 RADDR (temp),
 3019 }/*-------------------------< SNOOPEND >-------------------*/,{
 3020         /*
 3021         **      And stop.
 3022         */
 3023         SCR_INT,
 3024                 99,
 3025 }/*--------------------------------------------------------*/
 3026 };
 3027 
 3028 
 3029 /*==========================================================
 3030 **
 3031 **
 3032 **      Fill in #define dependent parts of the script
 3033 **
 3034 **
 3035 **==========================================================
 3036 */
 3037 
 3038 void ncr_script_fill (struct script * scr, struct scripth * scrh)
 3039 {
 3040         int     i;
 3041         ncrcmd  *p;
 3042 
 3043         p = scrh->tryloop;
 3044         for (i=0; i<MAX_START; i++) {
 3045                 *p++ =SCR_COPY (4);
 3046                 *p++ =NADDR (squeue[i]);
 3047                 *p++ =RADDR (dsa);
 3048                 *p++ =SCR_CALL;
 3049                 *p++ =PADDR (trysel);
 3050         };
 3051         *p++ =SCR_JUMP;
 3052         *p++ =PADDRH(tryloop);
 3053 
 3054         assert ((u_long)p == (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
 3055 
 3056         p = scr->data_in;
 3057 
 3058         *p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN));
 3059         *p++ =PADDR (no_data);
 3060         *p++ =SCR_COPY (sizeof (struct timeval));
 3061         *p++ =(ncrcmd) KVAR (KVAR_TIME);
 3062         *p++ =NADDR (header.stamp.data);
 3063         *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
 3064         *p++ =offsetof (struct dsb, data[ 0]);
 3065 
 3066         for (i=1; i<MAX_SCATTER; i++) {
 3067                 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
 3068                 *p++ =PADDR (checkatn);
 3069                 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
 3070                 *p++ =offsetof (struct dsb, data[i]);
 3071         };
 3072 
 3073         *p++ =SCR_CALL;
 3074         *p++ =PADDR (checkatn);
 3075         *p++ =SCR_JUMP;
 3076         *p++ =PADDR (no_data);
 3077 
 3078         assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
 3079 
 3080         p = scr->data_out;
 3081 
 3082         *p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT));
 3083         *p++ =PADDR (no_data);
 3084         *p++ =SCR_COPY (sizeof (struct timeval));
 3085         *p++ =(ncrcmd) KVAR (KVAR_TIME);
 3086         *p++ =NADDR (header.stamp.data);
 3087         *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
 3088         *p++ =offsetof (struct dsb, data[ 0]);
 3089 
 3090         for (i=1; i<MAX_SCATTER; i++) {
 3091                 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
 3092                 *p++ =PADDR (dispatch);
 3093                 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
 3094                 *p++ =offsetof (struct dsb, data[i]);
 3095         };
 3096 
 3097         *p++ =SCR_CALL;
 3098         *p++ =PADDR (dispatch);
 3099         *p++ =SCR_JUMP;
 3100         *p++ =PADDR (no_data);
 3101 
 3102         assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
 3103 }
 3104 
 3105 /*==========================================================
 3106 **
 3107 **
 3108 **      Copy and rebind a script.
 3109 **
 3110 **
 3111 **==========================================================
 3112 */
 3113 
 3114 static void ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
 3115 {
 3116         ncrcmd  opcode, new, old, tmp1, tmp2;
 3117         ncrcmd  *start, *end;
 3118         int relocs;
 3119 
 3120         start = src;
 3121         end = src + len/4;
 3122 
 3123         while (src < end) {
 3124 
 3125                 *dst++ = opcode = *src++;
 3126 
 3127                 /*
 3128                 **      If we forget to change the length
 3129                 **      in struct script, a field will be
 3130                 **      padded with 0. This is an illegal
 3131                 **      command.
 3132                 */
 3133 
 3134                 if (opcode == 0) {
 3135                         printf ("%s: ERROR0 IN SCRIPT at %d.\n",
 3136                                 ncr_name(np), (int) (src-start-1));
 3137                         DELAY (1000000);
 3138                 };
 3139 
 3140                 if (DEBUG_FLAGS & DEBUG_SCRIPT)
 3141                         printf ("%p:  <%x>\n",
 3142                                 (src-1), (unsigned)opcode);
 3143 
 3144                 /*
 3145                 **      We don't have to decode ALL commands
 3146                 */
 3147                 switch (opcode >> 28) {
 3148 
 3149                 case 0xc:
 3150                         /*
 3151                         **      COPY has TWO arguments.
 3152                         */
 3153                         relocs = 2;
 3154                         tmp1 = src[0];
 3155                         if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
 3156                                 tmp1 = 0;
 3157                         tmp2 = src[1];
 3158                         if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
 3159                                 tmp2 = 0;
 3160                         if ((tmp1 ^ tmp2) & 3) {
 3161                                 printf ("%s: ERROR1 IN SCRIPT at %d.\n",
 3162                                         ncr_name(np), (int) (src-start-1));
 3163                                 DELAY (1000000);
 3164                         }
 3165                         /*
 3166                         **      If PREFETCH feature not enabled, remove 
 3167                         **      the NO FLUSH bit if present.
 3168                         */
 3169                         if ((opcode & SCR_NO_FLUSH) && !(np->features&FE_PFEN))
 3170                                 dst[-1] = (opcode & ~SCR_NO_FLUSH);
 3171                         break;
 3172 
 3173                 case 0x0:
 3174                         /*
 3175                         **      MOVE (absolute address)
 3176                         */
 3177                         relocs = 1;
 3178                         break;
 3179 
 3180                 case 0x8:
 3181                         /*
 3182                         **      JUMP / CALL
 3183                         **      dont't relocate if relative :-)
 3184                         */
 3185                         if (opcode & 0x00800000)
 3186                                 relocs = 0;
 3187                         else
 3188                                 relocs = 1;
 3189                         break;
 3190 
 3191                 case 0x4:
 3192                 case 0x5:
 3193                 case 0x6:
 3194                 case 0x7:
 3195                         relocs = 1;
 3196                         break;
 3197 
 3198                 default:
 3199                         relocs = 0;
 3200                         break;
 3201                 };
 3202 
 3203                 if (relocs) {
 3204                         while (relocs--) {
 3205                                 old = *src++;
 3206 
 3207                                 switch (old & RELOC_MASK) {
 3208                                 case RELOC_REGISTER:
 3209                                         new = (old & ~RELOC_MASK) + np->paddr;
 3210                                         break;
 3211                                 case RELOC_LABEL:
 3212                                         new = (old & ~RELOC_MASK) + np->p_script;
 3213                                         break;
 3214                                 case RELOC_LABELH:
 3215                                         new = (old & ~RELOC_MASK) + np->p_scripth;
 3216                                         break;
 3217                                 case RELOC_SOFTC:
 3218                                         new = (old & ~RELOC_MASK) + vtophys(np);
 3219                                         break;
 3220                                 case RELOC_KVAR:
 3221                                         if (((old & ~RELOC_MASK) <
 3222                                              SCRIPT_KVAR_FIRST) ||
 3223                                             ((old & ~RELOC_MASK) >
 3224                                              SCRIPT_KVAR_LAST))
 3225                                                 panic("ncr KVAR out of range");
 3226                                         new = vtophys(script_kvars[old &
 3227                                             ~RELOC_MASK]);
 3228                                         break;
 3229                                 case 0:
 3230                                         /* Don't relocate a 0 address. */
 3231                                         if (old == 0) {
 3232                                                 new = old;
 3233                                                 break;
 3234                                         }
 3235                                         /* fall through */
 3236                                 default:
 3237                                         panic("ncr_script_copy_and_bind: weird relocation %x @ %d\n", old, (src - start));
 3238                                         break;
 3239                                 }
 3240 
 3241                                 *dst++ = new;
 3242                         }
 3243                 } else
 3244                         *dst++ = *src++;
 3245 
 3246         };
 3247 }
 3248 
 3249 /*==========================================================
 3250 **
 3251 **
 3252 **      Auto configuration.
 3253 **
 3254 **
 3255 **==========================================================
 3256 */
 3257 
 3258 /*----------------------------------------------------------
 3259 **
 3260 **      Reduce the transfer length to the max value
 3261 **      we can transfer safely.
 3262 **
 3263 **      Reading a block greater then MAX_SIZE from the
 3264 **      raw (character) device exercises a memory leak
 3265 **      in the vm subsystem. This is common to ALL devices.
 3266 **      We have submitted a description of this bug to
 3267 **      <FreeBSD-bugs@freefall.cdrom.com>.
 3268 **      It should be fixed in the current release.
 3269 **
 3270 **----------------------------------------------------------
 3271 */
 3272 
 3273 void ncr_min_phys (struct  buf *bp)
 3274 {
 3275         if ((unsigned long)bp->b_bcount > MAX_SIZE) bp->b_bcount = MAX_SIZE;
 3276 }
 3277 
 3278 /*----------------------------------------------------------
 3279 **
 3280 **      Maximal number of outstanding requests per target.
 3281 **
 3282 **----------------------------------------------------------
 3283 */
 3284 
 3285 u_int32_t ncr_info (int unit)
 3286 {
 3287         return (1);   /* may be changed later */
 3288 }
 3289 
 3290 /*----------------------------------------------------------
 3291 **
 3292 **      NCR chip devices table and chip look up function.
 3293 **      Features bit are defined in ncrreg.h. Is it the 
 3294 **      right place?
 3295 **
 3296 **----------------------------------------------------------
 3297 */
 3298 typedef struct {
 3299         unsigned long   device_id;
 3300         unsigned short  minrevid;
 3301         char           *name;
 3302         unsigned char   maxburst;
 3303         unsigned char   maxoffs;
 3304         unsigned char   clock_divn;
 3305         unsigned int    features;
 3306 } ncr_chip;
 3307 
 3308 static ncr_chip ncr_chip_table[] = {
 3309  {NCR_810_ID, 0x00,     "ncr 53c810 fast10 scsi",               4,  8, 4,
 3310  FE_ERL}
 3311  ,
 3312  {NCR_810_ID, 0x10,     "ncr 53c810a fast10 scsi",              4,  8, 4,
 3313  FE_ERL|FE_LDSTR|FE_PFEN|FE_BOF}
 3314  ,
 3315  {NCR_815_ID, 0x00,     "ncr 53c815 fast10 scsi",               4,  8, 4,
 3316  FE_ERL|FE_BOF}
 3317  ,
 3318  {NCR_820_ID, 0x00,     "ncr 53c820 fast10 wide scsi",          4,  8, 4,
 3319  FE_WIDE|FE_ERL}
 3320  ,
 3321  {NCR_825_ID, 0x00,     "ncr 53c825 fast10 wide scsi",          4,  8, 4,
 3322  FE_WIDE|FE_ERL|FE_BOF}
 3323  ,
 3324  {NCR_825_ID, 0x10,     "ncr 53c825a fast10 wide scsi",         7,  8, 4,
 3325  FE_WIDE|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3326  ,
 3327  {NCR_860_ID, 0x00,     "ncr 53c860 fast20 scsi",               4,  8, 5,
 3328  FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_LDSTR|FE_PFEN}
 3329  ,
 3330  {NCR_875_ID, 0x00,     "ncr 53c875 fast20 wide scsi",          7, 16, 5,
 3331  FE_WIDE|FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3332  ,
 3333  {NCR_875_ID, 0x02,     "ncr 53c875 fast20 wide scsi",          7, 16, 5,
 3334  FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3335  ,
 3336  {NCR_875_ID2, 0x00,    "ncr 53c875j fast20 wide scsi",         7, 16, 5,
 3337  FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3338  ,
 3339  {NCR_885_ID, 0x00,     "ncr 53c885 fast20 wide scsi",          7, 16, 5,
 3340  FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3341  ,
 3342  {NCR_895_ID, 0x00,     "ncr 53c895 fast40 wide scsi",          7, 31, 7,
 3343  FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3344  ,
 3345  {NCR_896_ID, 0x00,     "ncr 53c896 fast40 wide scsi",          7, 31, 7,
 3346  FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
 3347 };
 3348 
 3349 static int ncr_chip_lookup(u_long device_id, u_char revision_id)
 3350 {
 3351         int i, found;
 3352         
 3353         found = -1;
 3354         for (i = 0; i < sizeof(ncr_chip_table)/sizeof(ncr_chip_table[0]); i++) {
 3355                 if (device_id   == ncr_chip_table[i].device_id &&
 3356                     ncr_chip_table[i].minrevid <= revision_id) {
 3357                         if (found < 0 || 
 3358                             ncr_chip_table[found].minrevid 
 3359                               < ncr_chip_table[i].minrevid) {
 3360                                 found = i;
 3361                         }
 3362                 }
 3363         }
 3364         return found;
 3365 }
 3366 
 3367 /*----------------------------------------------------------
 3368 **
 3369 **      Probe the hostadapter.
 3370 **
 3371 **----------------------------------------------------------
 3372 */
 3373 
 3374 #ifdef __NetBSD__
 3375 
 3376 int
 3377 ncr_probe(parent, match, aux)
 3378         struct device *parent;
 3379         void *match, *aux;
 3380 {
 3381         struct cfdata *cf = match;
 3382         struct pci_attach_args *pa = aux;
 3383 
 3384 #if 0
 3385         if (!pci_targmatch(cf, pa))
 3386                 return 0;
 3387 #endif
 3388         if (ncr_chip_lookup(pa->pa_id, 0xff) < 0)
 3389                 return 0;
 3390 
 3391         return 1;
 3392 }
 3393 
 3394 #else /* !__NetBSD__ */
 3395 
 3396 
 3397 static  char* ncr_probe (pcici_t tag, pcidi_t type)
 3398 {
 3399         u_char rev = pci_conf_read (tag, PCI_CLASS_REG) & 0xff;
 3400         int i;
 3401 
 3402         i = ncr_chip_lookup(type, rev);
 3403         if (i >= 0)
 3404                 return ncr_chip_table[i].name;
 3405 
 3406         return (NULL);
 3407 }
 3408 
 3409 #endif /* !__NetBSD__ */
 3410 
 3411 
 3412 /*==========================================================
 3413 **
 3414 **      NCR chip clock divisor table.
 3415 **      Divisors are multiplied by 10,000,000 in order to make 
 3416 **      calculations more simple.
 3417 **
 3418 **==========================================================
 3419 */
 3420 
 3421 #define _5M 5000000
 3422 static u_long div_10M[] =
 3423         {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
 3424 
 3425 /*===============================================================
 3426 **
 3427 **      NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128 
 3428 **      transfers. 32,64,128 are only supported by 875 and 895 chips.
 3429 **      We use log base 2 (burst length) as internal code, with 
 3430 **      value 0 meaning "burst disabled".
 3431 **
 3432 **===============================================================
 3433 */
 3434 
 3435 /*
 3436  *      Burst length from burst code.
 3437  */
 3438 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
 3439 
 3440 /*
 3441  *      Burst code from io register bits.
 3442  */
 3443 #define burst_code(dmode, ctest4, ctest5) \
 3444         (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
 3445 
 3446 /*
 3447  *      Set initial io register bits from burst code.
 3448  */
 3449 static void ncr_init_burst(ncb_p np, u_char bc)
 3450 {
 3451         np->rv_ctest4   &= ~0x80;
 3452         np->rv_dmode    &= ~(0x3 << 6);
 3453         np->rv_ctest5   &= ~0x4;
 3454 
 3455         if (!bc) {
 3456                 np->rv_ctest4   |= 0x80;
 3457         }
 3458         else {
 3459                 --bc;
 3460                 np->rv_dmode    |= ((bc & 0x3) << 6);
 3461                 np->rv_ctest5   |= (bc & 0x4);
 3462         }
 3463 }
 3464 
 3465 /*==========================================================
 3466 **
 3467 **
 3468 **      Auto configuration:  attach and init a host adapter.
 3469 **
 3470 **
 3471 **==========================================================
 3472 */
 3473 
 3474 #ifdef __NetBSD__
 3475 
 3476 int
 3477 ncr_print()
 3478 {
 3479 }
 3480 
 3481 void
 3482 ncr_attach(parent, self, aux)
 3483         struct device *parent, *self;
 3484         void *aux;
 3485 {
 3486         struct pci_attach_args *pa = aux;
 3487         int retval;
 3488         ncb_p np = (void *)self;
 3489         u_char  rev = 0;
 3490         u_long  period;
 3491         int     i;
 3492         ncr_chip *chip;
 3493 
 3494         /*
 3495         ** XXX NetBSD
 3496         ** Perhaps try to figure what which model chip it is and print that
 3497         ** out.
 3498         */
 3499         printf("\n");
 3500 
 3501         /*
 3502         **      Try to map the controller chip to
 3503         **      virtual and physical memory.
 3504         */
 3505 
 3506         retval = pci_map_mem(pa->pa_tag, 0x14, &np->vaddr, &np->paddr);
 3507         if (retval)
 3508                 return;
 3509 
 3510         np->sc_ih = pci_map_int(pa->pa_tag, PCI_IPL_BIO, ncr_intr, np);
 3511         if (np->sc_ih == NULL)
 3512                 return;
 3513 
 3514 
 3515 #else /* !__NetBSD__ */
 3516 
 3517 static  void ncr_attach (pcici_t config_id, int unit)
 3518 {
 3519         ncb_p np = (struct ncb*) 0;
 3520 #if ! (__FreeBSD__ >= 2)
 3521         extern unsigned bio_imask;
 3522 #endif
 3523 
 3524 #if (__FreeBSD__ >= 2)
 3525         struct scsibus_data *scbus;
 3526 #endif
 3527         u_char  rev = 0;
 3528         u_long  period;
 3529         int     i;
 3530 
 3531         /*
 3532         **      allocate and initialize structures.
 3533         */
 3534 
 3535         if (!np) {
 3536                 np = (ncb_p) malloc (sizeof (struct ncb), M_DEVBUF, M_WAITOK);
 3537                 if (!np) return;
 3538                 ncrp[unit]=np;
 3539         }
 3540         bzero (np, sizeof (*np));
 3541 
 3542         np->ccb = (ccb_p) malloc (sizeof (struct ccb), M_DEVBUF, M_WAITOK);
 3543         if (!np->ccb) return;
 3544         bzero (np->ccb, sizeof (*np->ccb));
 3545 
 3546         np->unit = unit;
 3547 
 3548         /*
 3549         **      Try to map the controller chip to
 3550         **      virtual and physical memory.
 3551         */
 3552 
 3553         if (!pci_map_mem (config_id, 0x14, &np->vaddr, &np->paddr))
 3554                 return;
 3555 
 3556         /*
 3557         **      Make the controller's registers available.
 3558         **      Now the INB INW INL OUTB OUTW OUTL macros
 3559         **      can be used safely.
 3560         */
 3561 
 3562         np->reg = (struct ncr_reg*) np->vaddr;
 3563 
 3564 #ifdef NCR_IOMAPPED
 3565         /*
 3566         **      Try to map the controller chip into iospace.
 3567         */
 3568 
 3569         if (!pci_map_port (config_id, 0x10, &np->port))
 3570                 return;
 3571 #endif
 3572 
 3573 #endif /* !__NetBSD__ */
 3574 
 3575         /*
 3576         **      Save some controller register default values
 3577         */
 3578 
 3579         np->rv_scntl3   = INB(nc_scntl3) & 0x77;
 3580         np->rv_dmode    = INB(nc_dmode)  & 0xce;
 3581         np->rv_dcntl    = INB(nc_dcntl)  & 0xa9;
 3582         np->rv_ctest3   = INB(nc_ctest3) & 0x01;
 3583         np->rv_ctest4   = INB(nc_ctest4) & 0x88;
 3584         np->rv_ctest5   = INB(nc_ctest5) & 0x24;
 3585         np->rv_gpcntl   = INB(nc_gpcntl);
 3586         np->rv_stest2   = INB(nc_stest2) & 0x20;
 3587 
 3588         if (bootverbose >= 2) {
 3589                 printf ("\tBIOS values:  SCNTL3:%02x DMODE:%02x  DCNTL:%02x\n",
 3590                         np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
 3591                 printf ("\t              CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
 3592                         np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
 3593         }
 3594 
 3595         np->rv_dcntl  |= NOCOM;
 3596 
 3597         /*
 3598         **      Do chip dependent initialization.
 3599         */
 3600 
 3601 #ifndef __NetBSD__
 3602         rev = pci_conf_read (config_id, PCI_CLASS_REG) & 0xff;
 3603 #endif /* !__NetBSD__ */
 3604 
 3605         /*
 3606         **      Get chip features from chips table.
 3607         */
 3608 #ifdef __NetBSD__
 3609         i = ncr_chip_lookup(pa->pa_id, rev);
 3610 #else /* !__NetBSD__ */
 3611         i = ncr_chip_lookup(pci_conf_read(config_id, PCI_ID_REG), rev);
 3612 #endif /* __NetBSD__ */
 3613 
 3614         if (i >= 0) {
 3615                 np->maxburst    = ncr_chip_table[i].maxburst;
 3616                 np->maxoffs     = ncr_chip_table[i].maxoffs;
 3617                 np->clock_divn  = ncr_chip_table[i].clock_divn;
 3618                 np->features    = ncr_chip_table[i].features;
 3619         } else {        /* Should'nt happen if probe() is ok */
 3620                 np->maxburst    = 4;
 3621                 np->maxoffs     = 8;
 3622                 np->clock_divn  = 4;
 3623                 np->features    = FE_ERL;
 3624         }
 3625 
 3626         np->maxwide     = np->features & FE_WIDE ? 1 : 0;
 3627         np->clock_khz   = np->features & FE_CLK80 ? 80000 : 40000;
 3628         if      (np->features & FE_QUAD)        np->multiplier = 4;
 3629         else if (np->features & FE_DBLR)        np->multiplier = 2;
 3630         else                                    np->multiplier = 1;
 3631 
 3632         /*
 3633         **      Get the frequency of the chip's clock.
 3634         **      Find the right value for scntl3.
 3635         */
 3636         if (np->features & (FE_ULTRA|FE_ULTRA2))
 3637                 ncr_getclock(np, np->multiplier);
 3638 
 3639         /*
 3640          *      If scntl3 != 0, we assume BIOS is present.
 3641          */
 3642         if (np->rv_scntl3)
 3643                 np->features |= FE_BIOS;
 3644 
 3645         /*
 3646          * Divisor to be used for async (timer pre-scaler).
 3647          */
 3648         i = np->clock_divn - 1;
 3649         while (i >= 0) {
 3650                 --i;
 3651                 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
 3652                         ++i;
 3653                         break;
 3654                 }
 3655         }
 3656         np->rv_scntl3 = i+1;
 3657 
 3658         /*
 3659          * Minimum synchronous period factor supported by the chip.
 3660          * Btw, 'period' is in tenths of nanoseconds.
 3661          */
 3662 
 3663         period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
 3664         if      (period <= 250)         np->minsync = 10;
 3665         else if (period <= 303)         np->minsync = 11;
 3666         else if (period <= 500)         np->minsync = 12;
 3667         else                            np->minsync = (period + 40 - 1) / 40;
 3668 
 3669         /*
 3670          * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
 3671          */
 3672 
 3673         if      (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
 3674                 np->minsync = 25;
 3675         else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
 3676                 np->minsync = 12;
 3677 
 3678         /*
 3679          * Maximum synchronous period factor supported by the chip.
 3680          */
 3681 
 3682         period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
 3683         np->maxsync = period > 2540 ? 254 : period / 10;
 3684 
 3685         /*
 3686          * Now, some features available with Symbios compatible boards.
 3687          * LED support through GPIO0 and DIFF support.
 3688          */
 3689 
 3690 #ifdef  SCSI_NCR_SYMBIOS_COMPAT
 3691         if (!(np->rv_gpcntl & 0x01))
 3692                 np->features |= FE_LED0;
 3693 #if 0   /* Not safe enough without NVRAM support or user settable option */
 3694         if (!(INB(nc_gpreg) & 0x08))
 3695                 np->features |= FE_DIFF;
 3696 #endif
 3697 #endif  /* SCSI_NCR_SYMBIOS_COMPAT */
 3698 
 3699         /*
 3700          * Prepare initial IO registers settings.
 3701          * Trust BIOS only if we believe we have one and if we want to.
 3702          */
 3703 #ifdef  SCSI_NCR_TRUST_BIOS
 3704         if (!(np->features & FE_BIOS)) {
 3705 #else
 3706         if (1) {
 3707 #endif
 3708                 np->rv_dmode = 0;
 3709                 np->rv_dcntl = NOCOM;
 3710                 np->rv_ctest3 = 0;
 3711                 np->rv_ctest4 = MPEE;
 3712                 np->rv_ctest5 = 0;
 3713                 np->rv_stest2 = 0;
 3714 
 3715                 if (np->features & FE_ERL)
 3716                         np->rv_dmode    |= ERL;   /* Enable Read Line */
 3717                 if (np->features & FE_BOF)
 3718                         np->rv_dmode    |= BOF;   /* Burst Opcode Fetch */
 3719                 if (np->features & FE_ERMP)
 3720                         np->rv_dmode    |= ERMP;  /* Enable Read Multiple */
 3721                 if (np->features & FE_CLSE)
 3722                         np->rv_dcntl    |= CLSE;  /* Cache Line Size Enable */
 3723                 if (np->features & FE_WRIE)
 3724                         np->rv_ctest3   |= WRIE;  /* Write and Invalidate */
 3725                 if (np->features & FE_PFEN)
 3726                         np->rv_dcntl    |= PFEN;  /* Prefetch Enable */
 3727                 if (np->features & FE_DFS)
 3728                         np->rv_ctest5   |= DFS;   /* Dma Fifo Size */
 3729                 if (np->features & FE_DIFF)     
 3730                         np->rv_stest2   |= 0x20;  /* Differential mode */
 3731                 ncr_init_burst(np, np->maxburst); /* Max dwords burst length */
 3732         } else {
 3733                 np->maxburst =
 3734                         burst_code(np->rv_dmode, np->rv_ctest4, np->rv_ctest5);
 3735         }
 3736 
 3737 #ifndef NCR_IOMAPPED
 3738         /*
 3739         **      Get on-chip SRAM address, if supported
 3740         */
 3741         if ((np->features & FE_RAM) && sizeof(struct script) <= 4096)
 3742 #ifdef __NetBSD__
 3743                 (void)pci_map_mem(pa->pa_tag, 0x18, &np->vaddr2, &np->paddr2));
 3744 #else   /* !__NetBSD__ */
 3745                 (void)(!pci_map_mem (config_id,0x18, &np->vaddr2, &np->paddr2));
 3746 #endif  /* __NetBSD */
 3747 #endif  /* !NCR_IOMAPPED */
 3748 
 3749         /*
 3750         **      Allocate structure for script relocation.
 3751         */
 3752         if (np->vaddr2 != NULL) {
 3753                 np->script = (struct script *) np->vaddr2;
 3754                 np->p_script = np->paddr2;
 3755 #ifdef __FreeBSD__
 3756         } else if (sizeof (struct script) > PAGE_SIZE) {
 3757                 np->script  = (struct script*) vm_page_alloc_contig 
 3758                         (round_page(sizeof (struct script)), 
 3759                          0x100000, 0xffffffff, PAGE_SIZE);
 3760 #endif /* __FreeBSD__ */
 3761         } else {
 3762                 np->script  = (struct script *)
 3763                         malloc (sizeof (struct script), M_DEVBUF, M_WAITOK);
 3764         }
 3765 
 3766 #ifdef __FreeBSD__
 3767         if (sizeof (struct scripth) > PAGE_SIZE) {
 3768                 np->scripth = (struct scripth*) vm_page_alloc_contig 
 3769                         (round_page(sizeof (struct scripth)), 
 3770                          0x100000, 0xffffffff, PAGE_SIZE);
 3771         } else 
 3772 #endif /* __FreeBSD__ */
 3773                 {
 3774                 np->scripth = (struct scripth *)
 3775                         malloc (sizeof (struct scripth), M_DEVBUF, M_WAITOK);
 3776         }
 3777 
 3778         /*
 3779         **      Bells and whistles   ;-)
 3780         */
 3781         if (bootverbose)
 3782                 printf("%s: minsync=%d, maxsync=%d, maxoffs=%d, %d dwords burst, %s dma fifo\n",
 3783                 ncr_name(np), np->minsync, np->maxsync, np->maxoffs,
 3784                 burst_length(np->maxburst),
 3785                 (np->rv_ctest5 & DFS) ? "large" : "normal");
 3786 
 3787         /*
 3788         **      Print some complementary information that can be helpfull.
 3789         */
 3790         if (bootverbose)
 3791                 printf("%s: %s, %s IRQ driver%s\n",
 3792                         ncr_name(np),
 3793                         np->rv_stest2 & 0x20 ? "differential" : "single-ended",
 3794                         np->rv_dcntl & IRQM ? "totem pole" : "open drain",
 3795                         np->vaddr2 ? ", using on-chip SRAM" : "");
 3796                         
 3797         /*
 3798         **      Patch scripts to physical addresses
 3799         */
 3800         ncr_script_fill (&script0, &scripth0);
 3801 
 3802         np->p_script    = vtophys(np->script);
 3803         np->p_scripth   = vtophys(np->scripth);
 3804 
 3805         ncr_script_copy_and_bind (np, (ncrcmd *) &script0,
 3806                         (ncrcmd *) np->script, sizeof(struct script));
 3807 
 3808         ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0,
 3809                 (ncrcmd *) np->scripth, sizeof(struct scripth));
 3810 
 3811         np->ccb->p_ccb  = vtophys (np->ccb);
 3812 
 3813         /*
 3814         **    Patch the script for LED support.
 3815         */
 3816 
 3817         if (np->features & FE_LED0) {
 3818                 np->script->reselect[0]  = SCR_REG_REG(gpreg, SCR_OR,  0x01);
 3819                 np->script->reselect1[0] = SCR_REG_REG(gpreg, SCR_AND, 0xfe);
 3820                 np->script->reselect2[0] = SCR_REG_REG(gpreg, SCR_AND, 0xfe);
 3821         }
 3822 
 3823         /*
 3824         **      init data structure
 3825         */
 3826 
 3827         np->jump_tcb.l_cmd      = SCR_JUMP;
 3828         np->jump_tcb.l_paddr    = NCB_SCRIPTH_PHYS (np, abort);
 3829 
 3830         /*
 3831         **  Get SCSI addr of host adapter (set by bios?).
 3832         */
 3833 
 3834         np->myaddr = INB(nc_scid) & 0x07;
 3835         if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
 3836 
 3837 #ifdef NCR_DUMP_REG
 3838         /*
 3839         **      Log the initial register contents
 3840         */
 3841         {
 3842                 int reg;
 3843 #ifdef __NetBSD__
 3844                 u_long config_id = pa->pa_tag;
 3845 #endif /* __NetBSD__ */
 3846                 for (reg=0; reg<256; reg+=4) {
 3847                         if (reg%16==0) printf ("reg[%2x]", reg);
 3848                         printf (" %08x", (int)pci_conf_read (config_id, reg));
 3849                         if (reg%16==12) printf ("\n");
 3850                 }
 3851         }
 3852 #endif /* NCR_DUMP_REG */
 3853 
 3854         /*
 3855         **      Reset chip.
 3856         */
 3857 
 3858         OUTB (nc_istat,  SRST);
 3859         DELAY (1000);
 3860         OUTB (nc_istat,  0   );
 3861 
 3862 
 3863         /*
 3864         **      Now check the cache handling of the pci chipset.
 3865         */
 3866 
 3867         if (ncr_snooptest (np)) {
 3868                 printf ("CACHE INCORRECTLY CONFIGURED.\n");
 3869                 return;
 3870         };
 3871 
 3872 #ifndef __NetBSD__
 3873         /*
 3874         **      Install the interrupt handler.
 3875         */
 3876 
 3877         if (!pci_map_int (config_id, ncr_intr, np, &bio_imask))
 3878                 printf ("\tinterruptless mode: reduced performance.\n");
 3879 #endif /* __NetBSD__ */
 3880 
 3881         /*
 3882         **      After SCSI devices have been opened, we cannot
 3883         **      reset the bus safely, so we do it here.
 3884         **      Interrupt handler does the real work.
 3885         */
 3886 
 3887         OUTB (nc_scntl1, CRST);
 3888         DELAY (1000);
 3889 
 3890         /*
 3891         **      Process the reset exception,
 3892         **      if interrupts are not enabled yet.
 3893         **      Then enable disconnects.
 3894         */
 3895         ncr_exception (np);
 3896         np->disc = 1;
 3897 
 3898         /*
 3899         **      Now let the generic SCSI driver
 3900         **      look for the SCSI devices on the bus ..
 3901         */
 3902 
 3903 #ifdef __NetBSD__
 3904         np->sc_link.adapter_softc = np;
 3905         np->sc_link.adapter_target = np->myaddr;
 3906         np->sc_link.openings = 1;
 3907 #else /* !__NetBSD__ */
 3908         np->sc_link.adapter_unit = unit;
 3909         np->sc_link.adapter_softc = np;
 3910         np->sc_link.adapter_targ = np->myaddr;
 3911         np->sc_link.fordriver    = 0;
 3912 #endif /* !__NetBSD__ */
 3913         np->sc_link.adapter      = &ncr_switch;
 3914         np->sc_link.device       = &ncr_dev;
 3915         np->sc_link.flags        = 0;
 3916 
 3917 #ifdef __NetBSD__
 3918         config_found(self, &np->sc_link, ncr_print);
 3919 #else /* !__NetBSD__ */
 3920 #if (__FreeBSD__ >= 2)
 3921         scbus = scsi_alloc_bus();
 3922         if(!scbus)
 3923                 return;
 3924         scbus->adapter_link = &np->sc_link;
 3925 
 3926         if(np->maxwide)
 3927                 scbus->maxtarg = 15;
 3928 
 3929         if (bootverbose) {
 3930                 unsigned t_from = 0;
 3931                 unsigned t_to   = scbus->maxtarg;
 3932                 unsigned myaddr = np->myaddr;
 3933 
 3934                 char *txt_and = "";
 3935                 printf ("%s scanning for targets ", ncr_name (np));
 3936                 if (t_from < myaddr) {
 3937                         printf ("%d..%d ", t_from, myaddr -1);
 3938                         txt_and = "and ";
 3939                 }
 3940                 if (myaddr < t_to)
 3941                         printf ("%s%d..%d ", txt_and, myaddr +1, t_to);
 3942                 printf ("(V%d " NCR_DATE ")\n", NCR_VERSION);
 3943         }
 3944                 
 3945         scsi_attachdevs (scbus);
 3946         scbus = NULL;   /* Upper-level SCSI code owns this now */
 3947 #else
 3948         scsi_attachdevs (&np->sc_link);
 3949 #endif /* !__FreeBSD__ >= 2 */
 3950 #endif /* !__NetBSD__ */
 3951 
 3952         /*
 3953         **      start the timeout daemon
 3954         */
 3955         ncr_timeout (np);
 3956         np->lasttime=0;
 3957 
 3958         /*
 3959         **  use SIMPLE TAG messages by default
 3960         */
 3961 
 3962         np->order = M_SIMPLE_TAG;
 3963 
 3964         /*
 3965         **  Done.
 3966         */
 3967 
 3968         return;
 3969 }
 3970 
 3971 /*==========================================================
 3972 **
 3973 **
 3974 **      Process pending device interrupts.
 3975 **
 3976 **
 3977 **==========================================================
 3978 */
 3979 
 3980 static void
 3981 ncr_intr(vnp)
 3982         void *vnp;
 3983 {
 3984         ncb_p np = vnp;
 3985         int oldspl = splbio();
 3986 
 3987         if (DEBUG_FLAGS & DEBUG_TINY) printf ("[");
 3988 
 3989         if (INB(nc_istat) & (INTF|SIP|DIP)) {
 3990                 /*
 3991                 **      Repeat until no outstanding ints
 3992                 */
 3993                 do {
 3994                         ncr_exception (np);
 3995                 } while (INB(nc_istat) & (INTF|SIP|DIP));
 3996 
 3997                 np->ticks = 100;
 3998         };
 3999 
 4000         if (DEBUG_FLAGS & DEBUG_TINY) printf ("]\n");
 4001 
 4002         splx (oldspl);
 4003 }
 4004 
 4005 /*==========================================================
 4006 **
 4007 **
 4008 **      Start execution of a SCSI command.
 4009 **      This is called from the generic SCSI driver.
 4010 **
 4011 **
 4012 **==========================================================
 4013 */
 4014 
 4015 static int32_t ncr_start (struct scsi_xfer * xp)
 4016 {
 4017         ncb_p np  = (ncb_p) xp->sc_link->adapter_softc;
 4018 
 4019         struct scsi_generic * cmd = xp->cmd;
 4020         ccb_p cp;
 4021         lcb_p lp;
 4022         tcb_p tp = &np->target[xp->sc_link->target];
 4023 
 4024         int     i, oldspl, segments, flags = xp->flags;
 4025         u_char  qidx, nego, idmsg, *msgptr;
 4026         u_long  msglen, msglen2;
 4027 
 4028         /*---------------------------------------------
 4029         **
 4030         **   Reset SCSI bus
 4031         **
 4032         **      Interrupt handler does the real work.
 4033         **
 4034         **---------------------------------------------
 4035         */
 4036 
 4037         if (flags & SCSI_RESET) {
 4038                 OUTB (nc_scntl1, CRST);
 4039                 DELAY (1000);
 4040                 return(COMPLETE);
 4041         };
 4042 
 4043         /*---------------------------------------------
 4044         **
 4045         **      Some shortcuts ...
 4046         **
 4047         **---------------------------------------------
 4048         */
 4049 
 4050         if ((xp->sc_link->target == np->myaddr    ) ||
 4051                 (xp->sc_link->target >= MAX_TARGET) ||
 4052                 (xp->sc_link->lun    >= MAX_LUN   ) ||
 4053                 (flags    & SCSI_DATA_UIO)) {
 4054                 xp->error = XS_DRIVER_STUFFUP;
 4055                 return(COMPLETE);
 4056         };
 4057 
 4058         /*---------------------------------------------
 4059         **
 4060         **      Diskaccess to partial blocks?
 4061         **
 4062         **---------------------------------------------
 4063         */
 4064 
 4065         if ((xp->datalen & 0x1ff) && !(tp->inqdata[0] & 0x1f)) {
 4066                 switch (cmd->opcode) {
 4067                 case 0x28:  /* READ_BIG  (10) */
 4068                 case 0xa8:  /* READ_HUGE (12) */
 4069                 case 0x2a:  /* WRITE_BIG (10) */
 4070                 case 0xaa:  /* WRITE_HUGE(12) */
 4071                         PRINT_ADDR(xp);
 4072                         printf ("access to partial disk block refused.\n");
 4073                         xp->error = XS_DRIVER_STUFFUP;
 4074                         return(COMPLETE);
 4075                 };
 4076         };
 4077 
 4078         if ((unsigned)xp->datalen > 128*1024*1024) {
 4079                 PRINT_ADDR(xp);
 4080                 printf ("trying to transfer %8x bytes, mem addr = %8x\n", 
 4081                         xp->datalen, xp->data);
 4082                 {
 4083                         int i;
 4084                         PRINT_ADDR(xp);
 4085                         printf ("command: %2x (", cmd->opcode);
 4086                         for (i = 0; i<11; i++)
 4087                                 printf (" %2x", cmd->bytes[i]);
 4088                         printf (")\n");
 4089                 }
 4090         }
 4091 
 4092         if (DEBUG_FLAGS & DEBUG_TINY) {
 4093                 PRINT_ADDR(xp);
 4094                 printf ("CMD=%x F=%x A=%x L=%x ", 
 4095                         cmd->opcode, (unsigned)xp->flags, 
 4096                         (unsigned) xp->data, (unsigned) xp->datalen);
 4097         }
 4098 
 4099         /*--------------------------------------------
 4100         **
 4101         **   Sanity checks ...
 4102         **      copied from Elischer's Adaptec driver.
 4103         **
 4104         **--------------------------------------------
 4105         */
 4106 
 4107         flags = xp->flags;
 4108         if (!(flags & INUSE)) {
 4109                 printf("%s: ?INUSE?\n", ncr_name (np));
 4110                 xp->flags |= INUSE;
 4111         };
 4112 
 4113         if(flags & ITSDONE) {
 4114                 printf("%s: ?ITSDONE?\n", ncr_name (np));
 4115                 xp->flags &= ~ITSDONE;
 4116         };
 4117 
 4118         if (xp->bp)
 4119                 flags |= (SCSI_NOSLEEP); /* just to be sure */
 4120 
 4121         /*---------------------------------------------------
 4122         **
 4123         **      Assign a ccb / bind xp
 4124         **
 4125         **----------------------------------------------------
 4126         */
 4127 
 4128         oldspl = splbio();
 4129 
 4130         if (!(cp=ncr_get_ccb (np, flags, xp->sc_link->target, xp->sc_link->lun))) {
 4131                 printf ("%s: no ccb.\n", ncr_name (np));
 4132                 xp->error = XS_DRIVER_STUFFUP;
 4133                 splx(oldspl);
 4134                 return(TRY_AGAIN_LATER);
 4135         };
 4136         cp->xfer = xp;
 4137 
 4138         /*---------------------------------------------------
 4139         **
 4140         **      timestamp
 4141         **
 4142         **----------------------------------------------------
 4143         */
 4144 
 4145         bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
 4146         gettime(&cp->phys.header.stamp.start);
 4147 
 4148         /*----------------------------------------------------
 4149         **
 4150         **      Get device quirks from a speciality table.
 4151         **
 4152         **      @GENSCSI@
 4153         **      This should be a part of the device table
 4154         **      in "scsi_conf.c".
 4155         **
 4156         **----------------------------------------------------
 4157         */
 4158 
 4159         if (tp->quirks & QUIRK_UPDATE) {
 4160 #ifdef NEW_SCSICONF
 4161                 tp->quirks = xp->sc_link->quirks;
 4162 #else
 4163                 tp->quirks = ncr_lookup ((char*) &tp->inqdata[0]);
 4164 #endif
 4165 #ifndef NCR_GETCC_WITHMSG
 4166                 if (tp->quirks) {
 4167                         PRINT_ADDR(xp);
 4168                         printf ("quirks=%x.\n", tp->quirks);
 4169                 };
 4170 #endif
 4171         };
 4172 
 4173         /*---------------------------------------------------
 4174         **
 4175         **      negotiation required?
 4176         **
 4177         **----------------------------------------------------
 4178         */
 4179 
 4180         nego = 0;
 4181 
 4182         if (!tp->nego_cp && tp->inqdata[7]) {
 4183                 /*
 4184                 **      negotiate wide transfers ?
 4185                 */
 4186 
 4187                 if (!tp->widedone) {
 4188                         if (tp->inqdata[7] & INQ7_WIDE16) {
 4189                                 nego = NS_WIDE;
 4190                         } else
 4191                                 tp->widedone=1;
 4192                 };
 4193 
 4194                 /*
 4195                 **      negotiate synchronous transfers?
 4196                 */
 4197 
 4198                 if (!nego && !tp->period) {
 4199                         if (SCSI_NCR_DFLT_SYNC 
 4200 #if defined (CDROM_ASYNC)
 4201                             && ((tp->inqdata[0] & 0x1f) != 5)
 4202 #endif
 4203                             && (tp->inqdata[7] & INQ7_SYNC)) {
 4204                                 nego = NS_SYNC;
 4205                         } else {
 4206                                 tp->period  =0xffff;
 4207                                 tp->sval = 0xe0;
 4208                                 PRINT_ADDR(xp);
 4209                                 printf ("asynchronous.\n");
 4210                         };
 4211                 };
 4212 
 4213                 /*
 4214                 **      remember nego is pending for the target.
 4215                 **      Avoid to start a nego for all queued commands 
 4216                 **      when tagged command queuing is enabled.
 4217                 */
 4218 
 4219                 if (nego)
 4220                         tp->nego_cp = cp;
 4221         };
 4222 
 4223         /*---------------------------------------------------
 4224         **
 4225         **      choose a new tag ...
 4226         **
 4227         **----------------------------------------------------
 4228         */
 4229 
 4230         if ((lp = tp->lp[xp->sc_link->lun]) && (lp->usetags)) {
 4231                 /*
 4232                 **      assign a tag to this ccb!
 4233                 */
 4234                 while (!cp->tag) {
 4235                         ccb_p cp2 = lp->next_ccb;
 4236                         lp->lasttag = lp->lasttag % 255 + 1;
 4237                         while (cp2 && cp2->tag != lp->lasttag)
 4238                                 cp2 = cp2->next_ccb;
 4239                         if (cp2) continue;
 4240                         cp->tag=lp->lasttag;
 4241                         if (DEBUG_FLAGS & DEBUG_TAGS) {
 4242                                 PRINT_ADDR(xp);
 4243                                 printf ("using tag #%d.\n", cp->tag);
 4244                         };
 4245                 };
 4246         } else {
 4247                 cp->tag=0;
 4248         };
 4249 
 4250         /*----------------------------------------------------
 4251         **
 4252         **      Build the identify / tag / sdtr message
 4253         **
 4254         **----------------------------------------------------
 4255         */
 4256 
 4257         idmsg = M_IDENTIFY | xp->sc_link->lun;
 4258         if ((cp!=np->ccb) && (np->disc))
 4259                 idmsg |= 0x40;
 4260 
 4261         msgptr = cp->scsi_smsg;
 4262         msglen = 0;
 4263         msgptr[msglen++] = idmsg;
 4264 
 4265         if (cp->tag) {
 4266             char tag;
 4267 
 4268             tag = np->order;
 4269             if (tag == 0) {
 4270                 /*
 4271                 **      Ordered write ops, unordered read ops.
 4272                 */
 4273                 switch (cmd->opcode) {
 4274                 case 0x08:  /* READ_SMALL (6) */
 4275                 case 0x28:  /* READ_BIG  (10) */
 4276                 case 0xa8:  /* READ_HUGE (12) */
 4277                     tag = M_SIMPLE_TAG;
 4278                     break;
 4279                 default:
 4280                     tag = M_ORDERED_TAG;
 4281                 }
 4282             }
 4283             msgptr[msglen++] = tag;
 4284             msgptr[msglen++] = cp -> tag;
 4285         }
 4286 
 4287         switch (nego) {
 4288         case NS_SYNC:
 4289                 msgptr[msglen++] = M_EXTENDED;
 4290                 msgptr[msglen++] = 3;
 4291                 msgptr[msglen++] = M_X_SYNC_REQ;
 4292                 msgptr[msglen++] = tp->minsync;
 4293                 msgptr[msglen++] = tp->maxoffs;
 4294                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 4295                         PRINT_ADDR(cp->xfer);
 4296                         printf ("sync msgout: ");
 4297                         ncr_show_msg (&cp->scsi_smsg [msglen-5]);
 4298                         printf (".\n");
 4299                 };
 4300                 break;
 4301         case NS_WIDE:
 4302                 msgptr[msglen++] = M_EXTENDED;
 4303                 msgptr[msglen++] = 2;
 4304                 msgptr[msglen++] = M_X_WIDE_REQ;
 4305                 msgptr[msglen++] = tp->usrwide;
 4306                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 4307                         PRINT_ADDR(cp->xfer);
 4308                         printf ("wide msgout: ");
 4309                         ncr_show_msg (&cp->scsi_smsg [msglen-4]);
 4310                         printf (".\n");
 4311                 };
 4312                 break;
 4313         };
 4314 
 4315         /*----------------------------------------------------
 4316         **
 4317         **      Build the identify message for getcc.
 4318         **
 4319         **----------------------------------------------------
 4320         */
 4321 
 4322         cp -> scsi_smsg2 [0] = idmsg;
 4323         msglen2 = 1;
 4324 
 4325         /*----------------------------------------------------
 4326         **
 4327         **      Build the data descriptors
 4328         **
 4329         **----------------------------------------------------
 4330         */
 4331 
 4332         segments = ncr_scatter (&cp->phys, (vm_offset_t) xp->data,
 4333                                         (vm_size_t) xp->datalen);
 4334 
 4335         if (segments < 0) {
 4336                 xp->error = XS_DRIVER_STUFFUP;
 4337                 ncr_free_ccb(np, cp, flags);
 4338                 splx(oldspl);
 4339                 return(COMPLETE);
 4340         };
 4341 
 4342         /*----------------------------------------------------
 4343         **
 4344         **      Set the SAVED_POINTER.
 4345         **
 4346         **----------------------------------------------------
 4347         */
 4348 
 4349         if (flags & SCSI_DATA_IN) {
 4350                 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_in);
 4351                 cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
 4352         } else if (flags & SCSI_DATA_OUT) {
 4353                 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_out);
 4354                 cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
 4355         } else {
 4356                 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, no_data);
 4357                 cp->phys.header.goalp = cp->phys.header.savep;
 4358         };
 4359         cp->phys.header.lastp = cp->phys.header.savep;
 4360 
 4361 
 4362         /*----------------------------------------------------
 4363         **
 4364         **      fill in ccb
 4365         **
 4366         **----------------------------------------------------
 4367         **
 4368         **
 4369         **      physical -> virtual backlink
 4370         **      Generic SCSI command
 4371         */
 4372         cp->phys.header.cp              = cp;
 4373         /*
 4374         **      Startqueue
 4375         */
 4376         cp->phys.header.launch.l_paddr  = NCB_SCRIPT_PHYS (np, select);
 4377         cp->phys.header.launch.l_cmd    = SCR_JUMP;
 4378         /*
 4379         **      select
 4380         */
 4381         cp->phys.select.sel_id          = xp->sc_link->target;
 4382         cp->phys.select.sel_scntl3      = tp->wval;
 4383         cp->phys.select.sel_sxfer       = tp->sval;
 4384         /*
 4385         **      message
 4386         */
 4387         cp->phys.smsg.addr              = CCB_PHYS (cp, scsi_smsg);
 4388         cp->phys.smsg.size              = msglen;
 4389 
 4390         cp->phys.smsg2.addr             = CCB_PHYS (cp, scsi_smsg2);
 4391         cp->phys.smsg2.size             = msglen2;
 4392         /*
 4393         **      command
 4394         */
 4395         cp->phys.cmd.addr               = vtophys (cmd);
 4396         cp->phys.cmd.size               = xp->cmdlen;
 4397         /*
 4398         **      sense command
 4399         */
 4400         cp->phys.scmd.addr              = CCB_PHYS (cp, sensecmd);
 4401         cp->phys.scmd.size              = 6;
 4402         /*
 4403         **      patch requested size into sense command
 4404         */
 4405         cp->sensecmd[0]                 = 0x03;
 4406         cp->sensecmd[1]                 = xp->sc_link->lun << 5;
 4407         cp->sensecmd[4]                 = sizeof(struct scsi_sense_data);
 4408         if (xp->req_sense_length)
 4409                 cp->sensecmd[4]         = xp->req_sense_length;
 4410         /*
 4411         **      sense data
 4412         */
 4413         cp->phys.sense.addr             = vtophys (&cp->xfer->sense);
 4414         cp->phys.sense.size             = sizeof(struct scsi_sense_data);
 4415         /*
 4416         **      status
 4417         */
 4418         cp->actualquirks                = tp->quirks;
 4419         cp->host_status                 = nego ? HS_NEGOTIATE : HS_BUSY;
 4420         cp->scsi_status                 = S_ILLEGAL;
 4421         cp->parity_status               = 0;
 4422 
 4423         cp->xerr_status                 = XE_OK;
 4424         cp->sync_status                 = tp->sval;
 4425         cp->nego_status                 = nego;
 4426         cp->wide_status                 = tp->wval;
 4427 
 4428         /*----------------------------------------------------
 4429         **
 4430         **      Critical region: start this job.
 4431         **
 4432         **----------------------------------------------------
 4433         */
 4434 
 4435         /*
 4436         **      reselect pattern and activate this job.
 4437         */
 4438 
 4439         cp->jump_ccb.l_cmd      = (SCR_JUMP ^ IFFALSE (DATA (cp->tag)));
 4440         cp->tlimit              = time.tv_sec + xp->timeout / 1000 + 2;
 4441         cp->magic               = CCB_MAGIC;
 4442 
 4443         /*
 4444         **      insert into start queue.
 4445         */
 4446 
 4447         qidx = np->squeueput + 1;
 4448         if (qidx >= MAX_START) qidx=0;
 4449         np->squeue [qidx         ] = NCB_SCRIPT_PHYS (np, idle);
 4450         np->squeue [np->squeueput] = CCB_PHYS (cp, phys);
 4451         np->squeueput = qidx;
 4452 
 4453         if(DEBUG_FLAGS & DEBUG_QUEUE)
 4454                 printf ("%s: queuepos=%d tryoffset=%d.\n", ncr_name (np),
 4455                 np->squeueput,
 4456                 (unsigned)(np->script->startpos[0]- 
 4457                            (NCB_SCRIPTH_PHYS (np, tryloop))));
 4458 
 4459         /*
 4460         **      Script processor may be waiting for reselect.
 4461         **      Wake it up.
 4462         */
 4463         OUTB (nc_istat, SIGP);
 4464 
 4465         /*
 4466         **      and reenable interrupts
 4467         */
 4468         splx (oldspl);
 4469 
 4470         /*
 4471         **      If interrupts are enabled, return now.
 4472         **      Command is successfully queued.
 4473         */
 4474 
 4475 #ifdef __NetBSD__
 4476         if (!(flags & SCSI_POLL)) {
 4477 #else /* !__NetBSD__ */ 
 4478         if (!(flags & SCSI_NOMASK)) {
 4479 #endif /* __NetBSD__ */
 4480                 if (np->lasttime) {
 4481                         if(DEBUG_FLAGS & DEBUG_TINY) printf ("Q");
 4482                         return(SUCCESSFULLY_QUEUED);
 4483                 };
 4484         };
 4485 
 4486         /*----------------------------------------------------
 4487         **
 4488         **      Interrupts not yet enabled - have to poll.
 4489         **
 4490         **----------------------------------------------------
 4491         */
 4492 
 4493         if (DEBUG_FLAGS & DEBUG_POLL) printf("P");
 4494 
 4495         for (i=xp->timeout; i && !(xp->flags & ITSDONE);i--) {
 4496                 if ((DEBUG_FLAGS & DEBUG_POLL) && (cp->host_status))
 4497                         printf ("%c", (cp->host_status & 0xf) + '');
 4498                 DELAY (1000);
 4499                 ncr_exception (np);
 4500         };
 4501 
 4502         /*
 4503         **      Abort if command not done.
 4504         */
 4505         if (!(xp->flags & ITSDONE)) {
 4506                 printf ("%s: aborting job ...\n", ncr_name (np));
 4507                 OUTB (nc_istat, CABRT);
 4508                 DELAY (100000);
 4509                 OUTB (nc_istat, SIGP);
 4510                 ncr_exception (np);
 4511         };
 4512 
 4513         if (!(xp->flags & ITSDONE)) {
 4514                 printf ("%s: abortion failed at %x.\n",
 4515                         ncr_name (np), (unsigned) INL(nc_dsp));
 4516                 ncr_init (np, "timeout", HS_TIMEOUT);
 4517         };
 4518 
 4519         if (!(xp->flags & ITSDONE)) {
 4520                 cp-> host_status = HS_SEL_TIMEOUT;
 4521                 ncr_complete (np, cp);
 4522         };
 4523 
 4524         if (DEBUG_FLAGS & DEBUG_RESULT) {
 4525                 printf ("%s: result: %x %x.\n",
 4526                         ncr_name (np), cp->host_status, cp->scsi_status);
 4527         };
 4528 #ifdef __NetBSD__
 4529         if (!(flags & SCSI_POLL)) 
 4530 #else /* !__NetBSD__ */ 
 4531         if (!(flags & SCSI_NOMASK))
 4532 #endif /* __NetBSD__ */
 4533                 return (SUCCESSFULLY_QUEUED);
 4534         switch (xp->error) {
 4535         case  0     : return (COMPLETE);
 4536         case XS_BUSY: return (TRY_AGAIN_LATER);
 4537         };
 4538         return (COMPLETE);
 4539 }
 4540 
 4541 /*==========================================================
 4542 **
 4543 **
 4544 **      Complete execution of a SCSI command.
 4545 **      Signal completion to the generic SCSI driver.
 4546 **
 4547 **
 4548 **==========================================================
 4549 */
 4550 
 4551 void ncr_complete (ncb_p np, ccb_p cp)
 4552 {
 4553         struct scsi_xfer * xp;
 4554         tcb_p tp;
 4555         lcb_p lp;
 4556 
 4557         /*
 4558         **      Sanity check
 4559         */
 4560 
 4561         if (!cp || (cp->magic!=CCB_MAGIC) || !cp->xfer) return;
 4562         cp->magic = 1;
 4563         cp->tlimit= 0;
 4564 
 4565         /*
 4566         **      No Reselect anymore.
 4567         */
 4568         cp->jump_ccb.l_cmd = (SCR_JUMP);
 4569 
 4570         /*
 4571         **      No starting.
 4572         */
 4573         cp->phys.header.launch.l_paddr= NCB_SCRIPT_PHYS (np, idle);
 4574 
 4575         /*
 4576         **      timestamp
 4577         */
 4578         ncb_profile (np, cp);
 4579 
 4580         if (DEBUG_FLAGS & DEBUG_TINY)
 4581                 printf ("CCB=%x STAT=%x/%x\n", (unsigned)cp & 0xfff,
 4582                         cp->host_status,cp->scsi_status);
 4583 
 4584         xp = cp->xfer;
 4585         cp->xfer = NULL;
 4586         tp = &np->target[xp->sc_link->target];
 4587         lp = tp->lp[xp->sc_link->lun];
 4588 
 4589         /*
 4590         **      We donnot queue more than 1 ccb per target 
 4591         **      with negotiation at any time. If this ccb was 
 4592         **      used for negotiation, clear this info in the tcb.
 4593         */
 4594 
 4595         if (cp == tp->nego_cp)
 4596                 tp->nego_cp = 0;
 4597 
 4598         /*
 4599         **      Check for parity errors.
 4600         */
 4601 
 4602         if (cp->parity_status) {
 4603                 PRINT_ADDR(xp);
 4604                 printf ("%d parity error(s), fallback.\n", cp->parity_status);
 4605                 /*
 4606                 **      fallback to asynch transfer.
 4607                 */
 4608                 tp->usrsync=255;
 4609                 tp->period =  0;
 4610         };
 4611 
 4612         /*
 4613         **      Check for extended errors.
 4614         */
 4615 
 4616         if (cp->xerr_status != XE_OK) {
 4617                 PRINT_ADDR(xp);
 4618                 switch (cp->xerr_status) {
 4619                 case XE_EXTRA_DATA:
 4620                         printf ("extraneous data discarded.\n");
 4621                         break;
 4622                 case XE_BAD_PHASE:
 4623                         printf ("illegal scsi phase (4/5).\n");
 4624                         break;
 4625                 default:
 4626                         printf ("extended error %d.\n", cp->xerr_status);
 4627                         break;
 4628                 };
 4629                 if (cp->host_status==HS_COMPLETE)
 4630                         cp->host_status = HS_FAIL;
 4631         };
 4632 
 4633         /*
 4634         **      Check the status.
 4635         */
 4636 #ifdef __NetBSD__
 4637         if (xp->error != XS_NOERROR) { 
 4638                                 
 4639                 /*              
 4640                 **      Don't override the error value.
 4641                 */
 4642         } else                        
 4643 #endif /* __NetBSD__ */
 4644         if (   (cp->host_status == HS_COMPLETE)
 4645                 && (cp->scsi_status == S_GOOD)) {
 4646 
 4647                 /*
 4648                 **      All went well.
 4649                 */
 4650 
 4651                 xp->resid = 0;
 4652 
 4653                 /*
 4654                 ** if (cp->phys.header.lastp != cp->phys.header.goalp)...
 4655                 **
 4656                 **      @RESID@
 4657                 **      Could dig out the correct value for resid,
 4658                 **      but it would be quite complicated.
 4659                 **
 4660                 **      The ah1542.c driver sets it to 0 too ...
 4661                 */
 4662 
 4663                 /*
 4664                 **      Try to assign a ccb to this nexus
 4665                 */
 4666                 ncr_alloc_ccb (np, xp->sc_link->target, xp->sc_link->lun);
 4667 
 4668                 /*
 4669                 **      On inquire cmd (0x12) save some data.
 4670                 */
 4671                 if (xp->cmd->opcode == 0x12 && xp->sc_link->lun == 0) {
 4672                         bcopy ( xp->data,
 4673                                 &tp->inqdata,
 4674                                 sizeof (tp->inqdata));
 4675 
 4676                         /*
 4677                         **      set number of tags
 4678                         */
 4679                         ncr_setmaxtags (tp, tp->usrtags);
 4680 
 4681                         /*
 4682                         **      prepare negotiation of synch and wide.
 4683                         */
 4684                         ncr_negotiate (np, tp);
 4685 
 4686                         /*
 4687                         **      force quirks update before next command start
 4688                         */
 4689                         tp->quirks |= QUIRK_UPDATE;
 4690                 };
 4691 
 4692                 /*
 4693                 **      Announce changes to the generic driver
 4694                 */
 4695                 if (lp) {
 4696                         ncr_settags (tp, lp);
 4697                         if (lp->reqlink != lp->actlink)
 4698                                 ncr_opennings (np, lp, xp);
 4699                 };
 4700 
 4701                 tp->bytes     += xp->datalen;
 4702                 tp->transfers ++;
 4703 #ifndef __NetBSD__
 4704         } else if (xp->flags & SCSI_ERR_OK) {
 4705 
 4706                 /*
 4707                 **   Not correct, but errors expected.
 4708                 */
 4709                 xp->resid = 0;
 4710 #endif /* !__NetBSD__ */
 4711         } else if ((cp->host_status == HS_COMPLETE)
 4712                 && (cp->scsi_status == (S_SENSE|S_GOOD))) {
 4713 
 4714                 /*
 4715                 **   Check condition code
 4716                 */
 4717                 xp->error = XS_SENSE;
 4718 
 4719                 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
 4720                         u_char * p = (u_char*) & xp->sense;
 4721                         int i;
 4722                         printf ("\n%s: sense data:", ncr_name (np));
 4723                         for (i=0; i<14; i++) printf (" %x", *p++);
 4724                         printf (".\n");
 4725                 };
 4726 
 4727         } else if ((cp->host_status == HS_COMPLETE)
 4728                 && (cp->scsi_status == S_BUSY)) {
 4729 
 4730                 /*
 4731                 **   Target is busy.
 4732                 */
 4733                 xp->error = XS_BUSY;
 4734 
 4735         } else if (cp->host_status == HS_SEL_TIMEOUT) {
 4736 
 4737                 /*
 4738                 **   Device failed selection
 4739                 */
 4740                 xp->error = XS_SELTIMEOUT;
 4741         } else if(cp->host_status == HS_TIMEOUT) {
 4742 
 4743                 /*
 4744                 **   No response
 4745                 */
 4746                 xp->error = XS_TIMEOUT;
 4747 
 4748         } else {
 4749 
 4750                 /*
 4751                 **  Other protocol messes
 4752                 */
 4753                 PRINT_ADDR(xp);
 4754                 printf ("COMMAND FAILED (%x %x) @%x.\n",
 4755                         cp->host_status, cp->scsi_status, (unsigned)cp);
 4756 
 4757                 xp->error = XS_TIMEOUT;
 4758         }
 4759 
 4760         xp->flags |= ITSDONE;
 4761 
 4762         /*
 4763         **      trace output
 4764         */
 4765 
 4766         if (tp->usrflag & UF_TRACE) {
 4767                 u_char * p;
 4768                 int i;
 4769                 PRINT_ADDR(xp);
 4770                 printf (" CMD:");
 4771                 p = (u_char*) &xp->cmd->opcode;
 4772                 for (i=0; i<xp->cmdlen; i++) printf (" %x", *p++);
 4773 
 4774                 if (cp->host_status==HS_COMPLETE) {
 4775                         switch (cp->scsi_status) {
 4776                         case S_GOOD:
 4777                                 printf ("  GOOD");
 4778                                 break;
 4779                         case S_CHECK_COND:
 4780                                 printf ("  SENSE:");
 4781                                 p = (u_char*) &xp->sense;
 4782                                 for (i=0; i<xp->req_sense_length; i++)
 4783                                         printf (" %x", *p++);
 4784                                 break;
 4785                         default:
 4786                                 printf ("  STAT: %x\n", cp->scsi_status);
 4787                                 break;
 4788                         };
 4789                 } else printf ("  HOSTERROR: %x", cp->host_status);
 4790                 printf ("\n");
 4791         };
 4792 
 4793         /*
 4794         **      Free this ccb
 4795         */
 4796         ncr_free_ccb (np, cp, xp->flags);
 4797 
 4798         /*
 4799         **      signal completion to generic driver.
 4800         */
 4801         scsi_done (xp);
 4802 }
 4803 
 4804 /*==========================================================
 4805 **
 4806 **
 4807 **      Signal all (or one) control block done.
 4808 **
 4809 **
 4810 **==========================================================
 4811 */
 4812 
 4813 void ncr_wakeup (ncb_p np, u_long code)
 4814 {
 4815         /*
 4816         **      Starting at the default ccb and following
 4817         **      the links, complete all jobs with a
 4818         **      host_status greater than "disconnect".
 4819         **
 4820         **      If the "code" parameter is not zero,
 4821         **      complete all jobs that are not IDLE.
 4822         */
 4823 
 4824         ccb_p cp = np->ccb;
 4825         while (cp) {
 4826                 switch (cp->host_status) {
 4827 
 4828                 case HS_IDLE:
 4829                         break;
 4830 
 4831                 case HS_DISCONNECT:
 4832                         if(DEBUG_FLAGS & DEBUG_TINY) printf ("D");
 4833                         /* fall through */
 4834 
 4835                 case HS_BUSY:
 4836                 case HS_NEGOTIATE:
 4837                         if (!code) break;
 4838                         cp->host_status = code;
 4839 
 4840                         /* fall through */
 4841 
 4842                 default:
 4843                         ncr_complete (np, cp);
 4844                         break;
 4845                 };
 4846                 cp = cp -> link_ccb;
 4847         };
 4848 }
 4849 
 4850 /*==========================================================
 4851 **
 4852 **
 4853 **      Start NCR chip.
 4854 **
 4855 **
 4856 **==========================================================
 4857 */
 4858 
 4859 void ncr_init (ncb_p np, char * msg, u_long code)
 4860 {
 4861         int     i;
 4862         u_long  usrsync;
 4863         u_char  usrwide;
 4864 
 4865         /*
 4866         **      Reset chip.
 4867         */
 4868 
 4869         OUTB (nc_istat,  SRST);
 4870         DELAY (1000);
 4871         OUTB (nc_istat, 0);
 4872 
 4873         /*
 4874         **      Message.
 4875         */
 4876 
 4877         if (msg) printf ("%s: restart (%s).\n", ncr_name (np), msg);
 4878 
 4879         /*
 4880         **      Clear Start Queue
 4881         */
 4882 
 4883         for (i=0;i<MAX_START;i++)
 4884                 np -> squeue [i] = NCB_SCRIPT_PHYS (np, idle);
 4885 
 4886         /*
 4887         **      Start at first entry.
 4888         */
 4889 
 4890         np->squeueput = 0;
 4891         np->script->startpos[0] = NCB_SCRIPTH_PHYS (np, tryloop);
 4892         np->script->start0  [0] = SCR_INT ^ IFFALSE (0);
 4893 
 4894         /*
 4895         **      Wakeup all pending jobs.
 4896         */
 4897 
 4898         ncr_wakeup (np, code);
 4899 
 4900         /*
 4901         **      Init chip.
 4902         */
 4903 
 4904         OUTB (nc_istat,  0x00   );      /*  Remove Reset, abort ...          */
 4905         OUTB (nc_scntl0, 0xca   );      /*  full arb., ena parity, par->ATN  */
 4906         OUTB (nc_scntl1, 0x00   );      /*  odd parity, and remove CRST!!    */
 4907         ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock             */
 4908         OUTB (nc_scid  , RRE|np->myaddr);/*  host adapter SCSI address       */
 4909         OUTW (nc_respid, 1ul<<np->myaddr);/*  id to respond to               */
 4910         OUTB (nc_istat , SIGP   );      /*  Signal Process                   */
 4911         OUTB (nc_dmode , np->rv_dmode); /* XXX modify burstlen ??? */
 4912         OUTB (nc_dcntl , np->rv_dcntl);
 4913         OUTB (nc_ctest3, np->rv_ctest3);
 4914         OUTB (nc_ctest5, np->rv_ctest5);
 4915         OUTB (nc_ctest4, np->rv_ctest4);/*  enable master parity checking    */
 4916         OUTB (nc_stest2, np->rv_stest2|EXT); /* Extended Sreq/Sack filtering */
 4917         OUTB (nc_stest3, TE     );      /*  TolerANT enable                  */
 4918         OUTB (nc_stime0, 0x0b   );      /*  HTH = disabled, STO = 0.1 sec.   */
 4919 
 4920         if (bootverbose >= 2) {
 4921                 printf ("\tACTUAL values:SCNTL3:%02x DMODE:%02x  DCNTL:%02x\n",
 4922                         np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
 4923                 printf ("\t              CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
 4924                         np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
 4925         }
 4926 
 4927         /*
 4928         **    Enable GPIO0 pin for writing if LED support.
 4929         */
 4930 
 4931         if (np->features & FE_LED0) {
 4932                 OUTOFFB (nc_gpcntl, 0x01);
 4933         }
 4934 
 4935         /*
 4936         **      Reinitialize usrsync.
 4937         **      Have to renegotiate synch mode.
 4938         */
 4939 
 4940         usrsync = 255;
 4941         if (SCSI_NCR_DFLT_SYNC) {
 4942                 usrsync = SCSI_NCR_DFLT_SYNC;
 4943                 if (usrsync > np->maxsync)
 4944                         usrsync = np->maxsync;
 4945                 if (usrsync < np->minsync)
 4946                         usrsync = np->minsync;
 4947         };
 4948 
 4949         /*
 4950         **      Reinitialize usrwide.
 4951         **      Have to renegotiate wide mode.
 4952         */
 4953 
 4954         usrwide = (SCSI_NCR_MAX_WIDE);
 4955         if (usrwide > np->maxwide) usrwide=np->maxwide;
 4956 
 4957         /*
 4958         **      Disable disconnects.
 4959         */
 4960 
 4961         np->disc = 0;
 4962 
 4963         /*
 4964         **      Fill in target structure.
 4965         */
 4966 
 4967         for (i=0;i<MAX_TARGET;i++) {
 4968                 tcb_p tp = &np->target[i];
 4969 
 4970                 tp->sval    = 0;
 4971                 tp->wval    = np->rv_scntl3;
 4972 
 4973                 tp->usrsync = usrsync;
 4974                 tp->usrwide = usrwide;
 4975 
 4976                 ncr_negotiate (np, tp);
 4977         }
 4978 
 4979         /*
 4980         **      enable ints
 4981         */
 4982 
 4983         OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST);
 4984         OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
 4985 
 4986         /*
 4987         **    Start script processor.
 4988         */
 4989 
 4990         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
 4991 }
 4992 
 4993 /*==========================================================
 4994 **
 4995 **      Prepare the negotiation values for wide and
 4996 **      synchronous transfers.
 4997 **
 4998 **==========================================================
 4999 */
 5000 
 5001 static void ncr_negotiate (struct ncb* np, struct tcb* tp)
 5002 {
 5003         /*
 5004         **      minsync unit is 4ns !
 5005         */
 5006 
 5007         u_long minsync = tp->usrsync;
 5008 
 5009         /*
 5010         **      if not scsi 2
 5011         **      don't believe FAST!
 5012         */
 5013 
 5014         if ((minsync < 50) && (tp->inqdata[2] & 0x0f) < 2)
 5015                 minsync=50;
 5016 
 5017         /*
 5018         **      our limit ..
 5019         */
 5020 
 5021         if (minsync < np->minsync)
 5022                 minsync = np->minsync;
 5023 
 5024         /*
 5025         **      divider limit
 5026         */
 5027 
 5028         if (minsync > np->maxsync)
 5029                 minsync = 255;
 5030 
 5031         tp->minsync = minsync;
 5032         tp->maxoffs = (minsync<255 ? np->maxoffs : 0);
 5033 
 5034         /*
 5035         **      period=0: has to negotiate sync transfer
 5036         */
 5037 
 5038         tp->period=0;
 5039 
 5040         /*
 5041         **      widedone=0: has to negotiate wide transfer
 5042         */
 5043         tp->widedone=0;
 5044 }
 5045 
 5046 /*==========================================================
 5047 **
 5048 **      Get clock factor and sync divisor for a given 
 5049 **      synchronous factor period.
 5050 **      Returns the clock factor (in sxfer) and scntl3 
 5051 **      synchronous divisor field.
 5052 **
 5053 **==========================================================
 5054 */
 5055 
 5056 static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
 5057 {
 5058         u_long  clk = np->clock_khz;    /* SCSI clock frequency in kHz  */
 5059         int     div = np->clock_divn;   /* Number of divisors supported */
 5060         u_long  fak;                    /* Sync factor in sxfer         */
 5061         u_long  per;                    /* Period in tenths of ns       */
 5062         u_long  kpc;                    /* (per * clk)                  */
 5063 
 5064         /*
 5065         **      Compute the synchronous period in tenths of nano-seconds
 5066         */
 5067         if      (sfac <= 10)    per = 250;
 5068         else if (sfac == 11)    per = 303;
 5069         else if (sfac == 12)    per = 500;
 5070         else                    per = 40 * sfac;
 5071 
 5072         /*
 5073         **      Look for the greatest clock divisor that allows an 
 5074         **      input speed faster than the period.
 5075         */
 5076         kpc = per * clk;
 5077         while (--div >= 0)
 5078                 if (kpc >= (div_10M[div] * 4)) break;
 5079 
 5080         /*
 5081         **      Calculate the lowest clock factor that allows an output 
 5082         **      speed not faster than the period.
 5083         */
 5084         fak = (kpc - 1) / div_10M[div] + 1;
 5085 
 5086 #if 0   /* You can #if 1 if you think this optimization is usefull */
 5087 
 5088         per = (fak * div_10M[div]) / clk;
 5089 
 5090         /*
 5091         **      Why not to try the immediate lower divisor and to choose 
 5092         **      the one that allows the fastest output speed ?
 5093         **      We dont want input speed too much greater than output speed.
 5094         */
 5095         if (div >= 1 && fak < 6) {
 5096                 u_long fak2, per2;
 5097                 fak2 = (kpc - 1) / div_10M[div-1] + 1;
 5098                 per2 = (fak2 * div_10M[div-1]) / clk;
 5099                 if (per2 < per && fak2 <= 6) {
 5100                         fak = fak2;
 5101                         per = per2;
 5102                         --div;
 5103                 }
 5104         }
 5105 #endif
 5106 
 5107         if (fak < 4) fak = 4;   /* Should never happen, too bad ... */
 5108 
 5109         /*
 5110         **      Compute and return sync parameters for the ncr
 5111         */
 5112         *fakp           = fak - 4;
 5113         *scntl3p        = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
 5114 }
 5115 
 5116 /*==========================================================
 5117 **
 5118 **      Switch sync mode for current job and it's target
 5119 **
 5120 **==========================================================
 5121 */
 5122 
 5123 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer)
 5124 {
 5125         struct scsi_xfer *xp;
 5126         tcb_p tp;
 5127         int div;
 5128         u_char target = INB (nc_ctest0) & 0x0f;
 5129 
 5130         assert (cp);
 5131         if (!cp) return;
 5132 
 5133         xp = cp->xfer;
 5134         assert (xp);
 5135         if (!xp) return;
 5136         assert (target == (xp->sc_link->target & 0x0f));
 5137 
 5138         tp = &np->target[target];
 5139 
 5140         if (!scntl3 || !(sxfer & 0x1f))
 5141                 scntl3 = np->rv_scntl3;
 5142         scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
 5143 
 5144         /*
 5145         **      Deduce the value of controller sync period from scntl3.
 5146         **      period is in tenths of nano-seconds.
 5147         */
 5148 
 5149         div = ((scntl3 >> 4) & 0x7);
 5150         if ((sxfer & 0x1f) && div)
 5151                 tp->period = (((sxfer>>5)+4)*div_10M[div-1])/np->clock_khz;
 5152         else
 5153                 tp->period = 0xffff;
 5154 
 5155         /*
 5156         **       Stop there if sync parameters are unchanged
 5157         */
 5158 
 5159         if (tp->sval == sxfer && tp->wval == scntl3) return;
 5160         tp->sval = sxfer;
 5161         tp->wval = scntl3;
 5162 
 5163         /*
 5164         **      Bells and whistles   ;-)
 5165         */
 5166         PRINT_ADDR(xp);
 5167         if (sxfer & 0x1f) {
 5168                 unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
 5169                 unsigned mb10 = (f10 + tp->period/2) / tp->period;
 5170                 /*
 5171                 **  Disable extended Sreq/Sack filtering
 5172                 */
 5173                 if (tp->period <= 2000) OUTOFFB (nc_stest2, EXT);
 5174                 printf ("%d.%d MB/s (%d ns, offset %d)\n",
 5175                         mb10 / 10, mb10 % 10, tp->period / 10, sxfer & 0x1f);
 5176         } else  printf ("asynchronous.\n");
 5177 
 5178         /*
 5179         **      set actual value and sync_status
 5180         */
 5181         OUTB (nc_sxfer, sxfer);
 5182         np->sync_st = sxfer;
 5183         OUTB (nc_scntl3, scntl3);
 5184         np->wide_st = scntl3;
 5185 
 5186         /*
 5187         **      patch ALL ccbs of this target.
 5188         */
 5189         for (cp = np->ccb; cp; cp = cp->link_ccb) {
 5190                 if (!cp->xfer) continue;
 5191                 if (cp->xfer->sc_link->target != target) continue;
 5192                 cp->sync_status = sxfer;
 5193                 cp->wide_status = scntl3;
 5194         };
 5195 }
 5196 
 5197 /*==========================================================
 5198 **
 5199 **      Switch wide mode for current job and it's target
 5200 **      SCSI specs say: a SCSI device that accepts a WDTR 
 5201 **      message shall reset the synchronous agreement to 
 5202 **      asynchronous mode.
 5203 **
 5204 **==========================================================
 5205 */
 5206 
 5207 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack)
 5208 {
 5209         struct scsi_xfer *xp;
 5210         u_short target = INB (nc_ctest0) & 0x0f;
 5211         tcb_p tp;
 5212         u_char  scntl3;
 5213         u_char  sxfer;
 5214 
 5215         assert (cp);
 5216         if (!cp) return;
 5217 
 5218         xp = cp->xfer;
 5219         assert (xp);
 5220         if (!xp) return;
 5221         assert (target == (xp->sc_link->target & 0x0f));
 5222 
 5223         tp = &np->target[target];
 5224         tp->widedone  =  wide+1;
 5225         scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
 5226 
 5227         sxfer = ack ? 0 : tp->sval;
 5228 
 5229         /*
 5230         **       Stop there if sync/wide parameters are unchanged
 5231         */
 5232         if (tp->sval == sxfer && tp->wval == scntl3) return;
 5233         tp->sval = sxfer;
 5234         tp->wval = scntl3;
 5235 
 5236         /*
 5237         **      Bells and whistles   ;-)
 5238         */
 5239         PRINT_ADDR(xp);
 5240         if (scntl3 & EWS)
 5241                 printf ("WIDE SCSI (16 bit) enabled");
 5242         else
 5243                 printf ("WIDE SCSI disabled");
 5244 
 5245         /*
 5246         **      set actual value and sync_status
 5247         */
 5248         OUTB (nc_sxfer, sxfer);
 5249         np->sync_st = sxfer;
 5250         OUTB (nc_scntl3, scntl3);
 5251         np->wide_st = scntl3;
 5252 
 5253         /*
 5254         **      patch ALL ccbs of this target.
 5255         */
 5256         for (cp = np->ccb; cp; cp = cp->link_ccb) {
 5257                 if (!cp->xfer) continue;
 5258                 if (cp->xfer->sc_link->target != target) continue;
 5259                 cp->sync_status = sxfer;
 5260                 cp->wide_status = scntl3;
 5261         };
 5262 }
 5263 
 5264 /*==========================================================
 5265 **
 5266 **      Switch tagged mode for a target.
 5267 **
 5268 **==========================================================
 5269 */
 5270 
 5271 static void ncr_setmaxtags (tcb_p tp, u_long usrtags)
 5272 {
 5273         int l;
 5274         tp->usrtags = usrtags;
 5275         for (l=0; l<MAX_LUN; l++) {
 5276                 lcb_p lp;
 5277                 if (!tp) break;
 5278                 lp=tp->lp[l];
 5279                 if (!lp) continue;
 5280                 ncr_settags (tp, lp);
 5281         };
 5282 }
 5283 
 5284 static void ncr_settags (tcb_p tp, lcb_p lp)
 5285 {
 5286         u_char reqtags, tmp;
 5287 
 5288         if ((!tp) || (!lp)) return;
 5289 
 5290         /*
 5291         **      only devices capable of tagges commands
 5292         **      only disk devices
 5293         **      only if enabled by user ..
 5294         */
 5295         if ((tp->inqdata[7] & INQ7_QUEUE) == 0) {
 5296             tp->usrtags=0;
 5297         }
 5298         if (tp->usrtags && ((tp->inqdata[0] & 0x1f) == 0x00)) {
 5299                 reqtags = tp->usrtags;
 5300                 if (lp->actlink <= 1)
 5301                         lp->usetags=reqtags;
 5302         } else {
 5303                 reqtags = 1;
 5304                 if (lp->actlink <= 1)
 5305                         lp->usetags=0;
 5306         };
 5307 
 5308         /*
 5309         **      don't announce more than available.
 5310         */
 5311         tmp = lp->actccbs;
 5312         if (tmp > reqtags) tmp = reqtags;
 5313         lp->reqlink = tmp;
 5314 
 5315         /*
 5316         **      don't discard if announced.
 5317         */
 5318         tmp = lp->actlink;
 5319         if (tmp < reqtags) tmp = reqtags;
 5320         lp->reqccbs = tmp;
 5321 }
 5322 
 5323 /*----------------------------------------------------
 5324 **
 5325 **      handle user commands
 5326 **
 5327 **----------------------------------------------------
 5328 */
 5329 
 5330 static void ncr_usercmd (ncb_p np)
 5331 {
 5332         u_char t;
 5333         tcb_p tp;
 5334 
 5335         switch (np->user.cmd) {
 5336 
 5337         case 0: return;
 5338 
 5339         case UC_SETSYNC:
 5340                 for (t=0; t<MAX_TARGET; t++) {
 5341                         if (!((np->user.target>>t)&1)) continue;
 5342                         tp = &np->target[t];
 5343                         tp->usrsync = np->user.data;
 5344                         ncr_negotiate (np, tp);
 5345                 };
 5346                 break;
 5347 
 5348         case UC_SETTAGS:
 5349                 if (np->user.data > MAX_TAGS)
 5350                         break;
 5351                 for (t=0; t<MAX_TARGET; t++) {
 5352                         if (!((np->user.target>>t)&1)) continue;
 5353                         ncr_setmaxtags (&np->target[t], np->user.data);
 5354                 };
 5355                 break;
 5356 
 5357         case UC_SETDEBUG:
 5358                 ncr_debug = np->user.data;
 5359                 break;
 5360 
 5361         case UC_SETORDER:
 5362                 np->order = np->user.data;
 5363                 break;
 5364 
 5365         case UC_SETWIDE:
 5366                 for (t=0; t<MAX_TARGET; t++) {
 5367                         u_long size;
 5368                         if (!((np->user.target>>t)&1)) continue;
 5369                         tp = &np->target[t];
 5370                         size = np->user.data;
 5371                         if (size > np->maxwide) size=np->maxwide;
 5372                         tp->usrwide = size;
 5373                         ncr_negotiate (np, tp);
 5374                 };
 5375                 break;
 5376 
 5377         case UC_SETFLAG:
 5378                 for (t=0; t<MAX_TARGET; t++) {
 5379                         if (!((np->user.target>>t)&1)) continue;
 5380                         tp = &np->target[t];
 5381                         tp->usrflag = np->user.data;
 5382                 };
 5383                 break;
 5384         }
 5385         np->user.cmd=0;
 5386 }
 5387 
 5388 
 5389 
 5390 
 5391 /*==========================================================
 5392 **
 5393 **
 5394 **      ncr timeout handler.
 5395 **
 5396 **
 5397 **==========================================================
 5398 **
 5399 **      Misused to keep the driver running when
 5400 **      interrupts are not configured correctly.
 5401 **
 5402 **----------------------------------------------------------
 5403 */
 5404 
 5405 static void ncr_timeout (void *arg)
 5406 {
 5407         ncb_p   np = arg;
 5408         u_long  thistime = time.tv_sec;
 5409         u_long  step  = np->ticks;
 5410         u_long  count = 0;
 5411         long signed   t;
 5412         ccb_p cp;
 5413 
 5414         if (np->lasttime != thistime) {
 5415                 /*
 5416                 **      block ncr interrupts
 5417                 */
 5418                 int oldspl = splbio();
 5419                 np->lasttime = thistime;
 5420 
 5421                 ncr_usercmd (np);
 5422 
 5423                 /*----------------------------------------------------
 5424                 **
 5425                 **      handle ncr chip timeouts
 5426                 **
 5427                 **      Assumption:
 5428                 **      We have a chance to arbitrate for the
 5429                 **      SCSI bus at least every 10 seconds.
 5430                 **
 5431                 **----------------------------------------------------
 5432                 */
 5433 
 5434                 t = thistime - np->heartbeat;
 5435 
 5436                 if (t<2) np->latetime=0; else np->latetime++;
 5437 
 5438                 if (np->latetime>2) {
 5439                         /*
 5440                         **      If there are no requests, the script
 5441                         **      processor will sleep on SEL_WAIT_RESEL.
 5442                         **      But we have to check whether it died.
 5443                         **      Let's try to wake it up.
 5444                         */
 5445                         OUTB (nc_istat, SIGP);
 5446                 };
 5447 
 5448                 /*----------------------------------------------------
 5449                 **
 5450                 **      handle ccb timeouts
 5451                 **
 5452                 **----------------------------------------------------
 5453                 */
 5454 
 5455                 for (cp=np->ccb; cp; cp=cp->link_ccb) {
 5456                         /*
 5457                         **      look for timed out ccbs.
 5458                         */
 5459                         if (!cp->host_status) continue;
 5460                         count++;
 5461                         if (cp->tlimit > thistime) continue;
 5462 
 5463                         /*
 5464                         **      Disable reselect.
 5465                         **      Remove it from startqueue.
 5466                         */
 5467                         cp->jump_ccb.l_cmd = (SCR_JUMP);
 5468                         if (cp->phys.header.launch.l_paddr ==
 5469                                 NCB_SCRIPT_PHYS (np, select)) {
 5470                                 printf ("%s: timeout ccb=%x (skip)\n",
 5471                                         ncr_name (np), (unsigned)cp);
 5472                                 cp->phys.header.launch.l_paddr
 5473                                 = NCB_SCRIPT_PHYS (np, skip);
 5474                         };
 5475 
 5476                         switch (cp->host_status) {
 5477 
 5478                         case HS_BUSY:
 5479                         case HS_NEGOTIATE:
 5480                                 /*
 5481                                 ** still in start queue ?
 5482                                 */
 5483                                 if (cp->phys.header.launch.l_paddr ==
 5484                                         NCB_SCRIPT_PHYS (np, skip))
 5485                                         continue;
 5486 
 5487                                 /* fall through */
 5488                         case HS_DISCONNECT:
 5489                                 cp->host_status=HS_TIMEOUT;
 5490                         };
 5491                         cp->tag = 0;
 5492 
 5493                         /*
 5494                         **      wakeup this ccb.
 5495                         */
 5496                         ncr_complete (np, cp);
 5497                 };
 5498                 splx (oldspl);
 5499         }
 5500 
 5501         timeout (ncr_timeout, (caddr_t) np, step ? step : 1);
 5502 
 5503         if (INB(nc_istat) & (INTF|SIP|DIP)) {
 5504 
 5505                 /*
 5506                 **      Process pending interrupts.
 5507                 */
 5508 
 5509                 int     oldspl  = splbio ();
 5510                 if (DEBUG_FLAGS & DEBUG_TINY) printf ("{");
 5511                 ncr_exception (np);
 5512                 if (DEBUG_FLAGS & DEBUG_TINY) printf ("}");
 5513                 splx (oldspl);
 5514         };
 5515 }
 5516 
 5517 /*==========================================================
 5518 **
 5519 **      log message for real hard errors
 5520 **
 5521 **      "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
 5522 **      "             reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
 5523 **
 5524 **      exception register:
 5525 **              ds:     dstat
 5526 **              si:     sist
 5527 **
 5528 **      SCSI bus lines:
 5529 **              so:     control lines as driver by NCR.
 5530 **              si:     control lines as seen by NCR.
 5531 **              sd:     scsi data lines as seen by NCR.
 5532 **
 5533 **      wide/fastmode:
 5534 **              sxfer:  (see the manual)
 5535 **              scntl3: (see the manual)
 5536 **
 5537 **      current script command:
 5538 **              dsp:    script adress (relative to start of script).
 5539 **              dbc:    first word of script command.
 5540 **
 5541 **      First 16 register of the chip:
 5542 **              r0..rf
 5543 **
 5544 **==========================================================
 5545 */
 5546 
 5547 static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
 5548 {
 5549         u_int32_t dsp;
 5550         int     script_ofs;
 5551         int     script_size;
 5552         char    *script_name;
 5553         u_char  *script_base;
 5554         int     i;
 5555 
 5556         dsp     = INL (nc_dsp);
 5557 
 5558         if (np->p_script < dsp && 
 5559             dsp <= np->p_script + sizeof(struct script)) {
 5560                 script_ofs      = dsp - np->p_script;
 5561                 script_size     = sizeof(struct script);
 5562                 script_base     = (u_char *) np->script;
 5563                 script_name     = "script";
 5564         }
 5565         else if (np->p_scripth < dsp && 
 5566                  dsp <= np->p_scripth + sizeof(struct scripth)) {
 5567                 script_ofs      = dsp - np->p_scripth;
 5568                 script_size     = sizeof(struct scripth);
 5569                 script_base     = (u_char *) np->scripth;
 5570                 script_name     = "scripth";
 5571         } else {
 5572                 script_ofs      = dsp;
 5573                 script_size     = 0;
 5574                 script_base     = 0;
 5575                 script_name     = "mem";
 5576         }
 5577 
 5578         printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
 5579                 ncr_name (np), (unsigned)INB (nc_ctest0)&0x0f, dstat, sist,
 5580                 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
 5581                 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
 5582                 (unsigned)INL (nc_dbc));
 5583 
 5584         if (((script_ofs & 3) == 0) &&
 5585             (unsigned)script_ofs < script_size) {
 5586                 printf ("%s: script cmd = %08x\n", ncr_name(np),
 5587                         (int) *(ncrcmd *)(script_base + script_ofs));
 5588         }
 5589 
 5590         printf ("%s: regdump:", ncr_name(np));
 5591         for (i=0; i<16;i++)
 5592             printf (" %02x", (unsigned)INB_OFF(i));
 5593         printf (".\n");
 5594 }
 5595 
 5596 /*==========================================================
 5597 **
 5598 **
 5599 **      ncr chip exception handler.
 5600 **
 5601 **
 5602 **==========================================================
 5603 */
 5604 
 5605 void ncr_exception (ncb_p np)
 5606 {
 5607         u_char  istat, dstat;
 5608         u_short sist;
 5609 
 5610         /*
 5611         **      interrupt on the fly ?
 5612         */
 5613         while ((istat = INB (nc_istat)) & INTF) {
 5614                 if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
 5615                 OUTB (nc_istat, INTF);
 5616                 np->profile.num_fly++;
 5617                 ncr_wakeup (np, 0);
 5618         };
 5619         if (!(istat & (SIP|DIP))) {
 5620                 return;
 5621         }
 5622 
 5623         /*
 5624         **      Steinbach's Guideline for Systems Programming:
 5625         **      Never test for an error condition you don't know how to handle.
 5626         */
 5627 
 5628         sist  = (istat & SIP) ? INW (nc_sist)  : 0;
 5629         dstat = (istat & DIP) ? INB (nc_dstat) : 0;
 5630         np->profile.num_int++;
 5631 
 5632         if (DEBUG_FLAGS & DEBUG_TINY)
 5633                 printf ("<%d|%x:%x|%x:%x>",
 5634                         INB(nc_scr0),
 5635                         dstat,sist,
 5636                         (unsigned)INL(nc_dsp),
 5637                         (unsigned)INL(nc_dbc));
 5638         if ((dstat==DFE) && (sist==PAR)) return;
 5639 
 5640 /*==========================================================
 5641 **
 5642 **      First the normal cases.
 5643 **
 5644 **==========================================================
 5645 */
 5646         /*-------------------------------------------
 5647         **      SCSI reset
 5648         **-------------------------------------------
 5649         */
 5650 
 5651         if (sist & RST) {
 5652                 ncr_init (np, bootverbose ? "scsi reset" : NULL, HS_RESET);
 5653                 return;
 5654         };
 5655 
 5656         /*-------------------------------------------
 5657         **      selection timeout
 5658         **
 5659         **      IID excluded from dstat mask!
 5660         **      (chip bug)
 5661         **-------------------------------------------
 5662         */
 5663 
 5664         if ((sist  & STO) &&
 5665                 !(sist  & (GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
 5666                 !(dstat & (MDPE|BF|ABRT|SIR))) {
 5667                 ncr_int_sto (np);
 5668                 return;
 5669         };
 5670 
 5671         /*-------------------------------------------
 5672         **      Phase mismatch.
 5673         **-------------------------------------------
 5674         */
 5675 
 5676         if ((sist  & MA) &&
 5677                 !(sist  & (STO|GEN|HTH|SGE|UDC|RST|PAR)) &&
 5678                 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
 5679                 ncr_int_ma (np, dstat);
 5680                 return;
 5681         };
 5682 
 5683         /*----------------------------------------
 5684         **      move command with length 0
 5685         **----------------------------------------
 5686         */
 5687 
 5688         if ((dstat & IID) &&
 5689                 !(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
 5690                 !(dstat & (MDPE|BF|ABRT|SIR)) &&
 5691                 ((INL(nc_dbc) & 0xf8000000) == SCR_MOVE_TBL)) {
 5692                 /*
 5693                 **      Target wants more data than available.
 5694                 **      The "no_data" script will do it.
 5695                 */
 5696                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, no_data));
 5697                 return;
 5698         };
 5699 
 5700         /*-------------------------------------------
 5701         **      Programmed interrupt
 5702         **-------------------------------------------
 5703         */
 5704 
 5705         if ((dstat & SIR) &&
 5706                 !(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
 5707                 !(dstat & (MDPE|BF|ABRT|IID)) &&
 5708                 (INB(nc_dsps) <= SIR_MAX)) {
 5709                 ncr_int_sir (np);
 5710                 return;
 5711         };
 5712 
 5713         /*========================================
 5714         **      log message for real hard errors
 5715         **========================================
 5716         */
 5717 
 5718         ncr_log_hard_error(np, sist, dstat);
 5719 
 5720         /*========================================
 5721         **      do the register dump
 5722         **========================================
 5723         */
 5724 
 5725         if (time.tv_sec - np->regtime.tv_sec>10) {
 5726                 int i;
 5727                 gettime(&np->regtime);
 5728                 for (i=0; i<sizeof(np->regdump); i++)
 5729                         ((char*)&np->regdump)[i] = INB_OFF(i);
 5730                 np->regdump.nc_dstat = dstat;
 5731                 np->regdump.nc_sist  = sist;
 5732         };
 5733 
 5734 
 5735         /*----------------------------------------
 5736         **      clean up the dma fifo
 5737         **----------------------------------------
 5738         */
 5739 
 5740         if ( (INB(nc_sstat0) & (ILF|ORF|OLF)   ) ||
 5741              (INB(nc_sstat1) & (FF3210) ) ||
 5742              (INB(nc_sstat2) & (ILF1|ORF1|OLF1)) ||     /* wide .. */
 5743              !(dstat & DFE)) {
 5744                 printf ("%s: have to clear fifos.\n", ncr_name (np));
 5745                 OUTB (nc_stest3, TE|CSF);       /* clear scsi fifo */
 5746                 OUTB (nc_ctest3, np->rv_ctest3 | CLF);
 5747                                                 /* clear dma fifo  */
 5748         }
 5749 
 5750         /*----------------------------------------
 5751         **      handshake timeout
 5752         **----------------------------------------
 5753         */
 5754 
 5755         if (sist & HTH) {
 5756                 printf ("%s: handshake timeout\n", ncr_name(np));
 5757                 OUTB (nc_scntl1, CRST);
 5758                 DELAY (1000);
 5759                 OUTB (nc_scntl1, 0x00);
 5760                 OUTB (nc_scr0, HS_FAIL);
 5761                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
 5762                 return;
 5763         }
 5764 
 5765         /*----------------------------------------
 5766         **      unexpected disconnect
 5767         **----------------------------------------
 5768         */
 5769 
 5770         if ((sist  & UDC) &&
 5771                 !(sist  & (STO|GEN|HTH|MA|SGE|RST|PAR)) &&
 5772                 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
 5773                 OUTB (nc_scr0, HS_UNEXPECTED);
 5774                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
 5775                 return;
 5776         };
 5777 
 5778         /*----------------------------------------
 5779         **      cannot disconnect
 5780         **----------------------------------------
 5781         */
 5782 
 5783         if ((dstat & IID) &&
 5784                 !(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
 5785                 !(dstat & (MDPE|BF|ABRT|SIR)) &&
 5786                 ((INL(nc_dbc) & 0xf8000000) == SCR_WAIT_DISC)) {
 5787                 /*
 5788                 **      Unexpected data cycle while waiting for disconnect.
 5789                 */
 5790                 if (INB(nc_sstat2) & LDSC) {
 5791                         /*
 5792                         **      It's an early reconnect.
 5793                         **      Let's continue ...
 5794                         */
 5795                         OUTB (nc_dcntl, np->rv_dcntl | STD);
 5796                         /*
 5797                         **      info message
 5798                         */
 5799                         printf ("%s: INFO: LDSC while IID.\n",
 5800                                 ncr_name (np));
 5801                         return;
 5802                 };
 5803                 printf ("%s: target %d doesn't release the bus.\n",
 5804                         ncr_name (np), INB (nc_ctest0)&0x0f);
 5805                 /*
 5806                 **      return without restarting the NCR.
 5807                 **      timeout will do the real work.
 5808                 */
 5809                 return;
 5810         };
 5811 
 5812         /*----------------------------------------
 5813         **      single step
 5814         **----------------------------------------
 5815         */
 5816 
 5817         if ((dstat & SSI) &&
 5818                 !(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
 5819                 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
 5820                 OUTB (nc_dcntl, np->rv_dcntl | STD);
 5821                 return;
 5822         };
 5823 
 5824 /*
 5825 **      @RECOVER@ HTH, SGE, ABRT.
 5826 **
 5827 **      We should try to recover from these interrupts.
 5828 **      They may occur if there are problems with synch transfers, or 
 5829 **      if targets are switched on or off while the driver is running.
 5830 */
 5831 
 5832         if (sist & SGE) {
 5833                 /* clear scsi offsets */
 5834                 OUTB (nc_ctest3, np->rv_ctest3 | CLF);
 5835         }
 5836 
 5837         /*
 5838         **      Freeze controller to be able to read the messages.
 5839         */
 5840 
 5841         if (DEBUG_FLAGS & DEBUG_FREEZE) {
 5842                 int i;
 5843                 unsigned char val;
 5844                 for (i=0; i<0x60; i++) {
 5845                         switch (i%16) {
 5846 
 5847                         case 0:
 5848                                 printf ("%s: reg[%d0]: ",
 5849                                         ncr_name(np),i/16);
 5850                                 break;
 5851                         case 4:
 5852                         case 8:
 5853                         case 12:
 5854                                 printf (" ");
 5855                                 break;
 5856                         };
 5857                         val = ((unsigned char*) np->vaddr) [i];
 5858                         printf (" %x%x", val/16, val%16);
 5859                         if (i%16==15) printf (".\n");
 5860                 };
 5861 
 5862                 untimeout (ncr_timeout, (caddr_t) np);
 5863 
 5864                 printf ("%s: halted!\n", ncr_name(np));
 5865                 /*
 5866                 **      don't restart controller ...
 5867                 */
 5868                 OUTB (nc_istat,  SRST);
 5869                 return;
 5870         };
 5871 
 5872 #ifdef NCR_FREEZE
 5873         /*
 5874         **      Freeze system to be able to read the messages.
 5875         */
 5876         printf ("ncr: fatal error: system halted - press reset to reboot ...");
 5877         (void) splhigh();
 5878         for (;;);
 5879 #endif
 5880 
 5881         /*
 5882         **      sorry, have to kill ALL jobs ...
 5883         */
 5884 
 5885         ncr_init (np, "fatal error", HS_FAIL);
 5886 }
 5887 
 5888 /*==========================================================
 5889 **
 5890 **      ncr chip exception handler for selection timeout
 5891 **
 5892 **==========================================================
 5893 **
 5894 **      There seems to be a bug in the 53c810.
 5895 **      Although a STO-Interrupt is pending,
 5896 **      it continues executing script commands.
 5897 **      But it will fail and interrupt (IID) on
 5898 **      the next instruction where it's looking
 5899 **      for a valid phase.
 5900 **
 5901 **----------------------------------------------------------
 5902 */
 5903 
 5904 void ncr_int_sto (ncb_p np)
 5905 {
 5906         u_long dsa, scratcha, diff;
 5907         ccb_p cp;
 5908         if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
 5909 
 5910         /*
 5911         **      look for ccb and set the status.
 5912         */
 5913 
 5914         dsa = INL (nc_dsa);
 5915         cp = np->ccb;
 5916         while (cp && (CCB_PHYS (cp, phys) != dsa))
 5917                 cp = cp->link_ccb;
 5918 
 5919         if (cp) {
 5920                 cp-> host_status = HS_SEL_TIMEOUT;
 5921                 ncr_complete (np, cp);
 5922         };
 5923 
 5924         /*
 5925         **      repair start queue
 5926         */
 5927 
 5928         scratcha = INL (nc_scratcha);
 5929         diff = scratcha - NCB_SCRIPTH_PHYS (np, tryloop);
 5930 
 5931 /*      assert ((diff <= MAX_START * 20) && !(diff % 20));*/
 5932 
 5933         if ((diff <= MAX_START * 20) && !(diff % 20)) {
 5934                 np->script->startpos[0] = scratcha;
 5935                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
 5936                 return;
 5937         };
 5938         ncr_init (np, "selection timeout", HS_FAIL);
 5939 }
 5940 
 5941 /*==========================================================
 5942 **
 5943 **
 5944 **      ncr chip exception handler for phase errors.
 5945 **
 5946 **
 5947 **==========================================================
 5948 **
 5949 **      We have to construct a new transfer descriptor,
 5950 **      to transfer the rest of the current block.
 5951 **
 5952 **----------------------------------------------------------
 5953 */
 5954 
 5955 static void ncr_int_ma (ncb_p np, u_char dstat)
 5956 {
 5957         u_int32_t       dbc;
 5958         u_int32_t       rest;
 5959         u_int32_t       dsa;
 5960         u_int32_t       dsp;
 5961         u_int32_t       nxtdsp;
 5962         u_int32_t       *vdsp;
 5963         u_int32_t       oadr, olen;
 5964         u_int32_t       *tblp, *newcmd;
 5965         u_char  cmd, sbcl, ss0, ss2, ctest5;
 5966         u_short delta;
 5967         ccb_p   cp;
 5968 
 5969         dsp = INL (nc_dsp);
 5970         dsa = INL (nc_dsa);
 5971         dbc = INL (nc_dbc);
 5972         ss0 = INB (nc_sstat0);
 5973         ss2 = INB (nc_sstat2);
 5974         sbcl= INB (nc_sbcl);
 5975 
 5976         cmd = dbc >> 24;
 5977         rest= dbc & 0xffffff;
 5978 
 5979         ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
 5980         if (ctest5 & DFS)
 5981                 delta=(((ctest5<<8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
 5982         else
 5983                 delta=(INB (nc_dfifo) - rest) & 0x7f;
 5984 
 5985 
 5986         /*
 5987         **      The data in the dma fifo has not been transfered to
 5988         **      the target -> add the amount to the rest
 5989         **      and clear the data.
 5990         **      Check the sstat2 register in case of wide transfer.
 5991         */
 5992 
 5993         if (!(dstat & DFE)) rest += delta;
 5994         if (ss0 & OLF) rest++;
 5995         if (ss0 & ORF) rest++;
 5996         if (INB(nc_scntl3) & EWS) {
 5997                 if (ss2 & OLF1) rest++;
 5998                 if (ss2 & ORF1) rest++;
 5999         };
 6000         OUTB (nc_ctest3, np->rv_ctest3 | CLF);  /* clear dma fifo  */
 6001         OUTB (nc_stest3, TE|CSF);               /* clear scsi fifo */
 6002 
 6003         /*
 6004         **      locate matching cp
 6005         */
 6006         dsa = INL (nc_dsa);
 6007         cp = np->ccb;
 6008         while (cp && (CCB_PHYS (cp, phys) != dsa))
 6009                 cp = cp->link_ccb;
 6010 
 6011         if (!cp) {
 6012             printf ("%s: SCSI phase error fixup: CCB already dequeued (0x%08lx)\n", 
 6013                     ncr_name (np), (u_long) np->header.cp);
 6014             return;
 6015         }
 6016         if (cp != np->header.cp) {
 6017             printf ("%s: SCSI phase error fixup: CCB address mismatch (0x%08lx != 0x%08lx) np->ccb = 0x%08lx\n", 
 6018                     ncr_name (np), (u_long) cp, (u_long) np->header.cp, np->ccb);
 6019 /*          return;*/
 6020         }
 6021 
 6022         /*
 6023         **      find the interrupted script command,
 6024         **      and the address at which to continue.
 6025         */
 6026 
 6027         if (dsp == vtophys (&cp->patch[2])) {
 6028                 vdsp = &cp->patch[0];
 6029                 nxtdsp = vdsp[3];
 6030         } else if (dsp == vtophys (&cp->patch[6])) {
 6031                 vdsp = &cp->patch[4];
 6032                 nxtdsp = vdsp[3];
 6033         } else if (dsp > np->p_script &&
 6034                    dsp <= np->p_script + sizeof(struct script)) {
 6035                 vdsp = (u_int32_t *) ((char*)np->script - np->p_script + dsp-8);
 6036                 nxtdsp = dsp;
 6037         } else {
 6038                 vdsp = (u_int32_t *) ((char*)np->scripth - np->p_scripth+dsp-8);
 6039                 nxtdsp = dsp;
 6040         };
 6041 
 6042         /*
 6043         **      log the information
 6044         */
 6045         if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE)) {
 6046                 printf ("P%x%x ",cmd&7, sbcl&7);
 6047                 printf ("RL=%d D=%d SS0=%x ",
 6048                         (unsigned) rest, (unsigned) delta, ss0);
 6049         };
 6050         if (DEBUG_FLAGS & DEBUG_PHASE) {
 6051                 printf ("\nCP=%x CP2=%x DSP=%x NXT=%x VDSP=%x CMD=%x ",
 6052                         (unsigned)cp, (unsigned)np->header.cp,
 6053                         (unsigned)dsp,
 6054                         (unsigned)nxtdsp, (unsigned)vdsp, cmd);
 6055         };
 6056 
 6057         /*
 6058         **      get old startaddress and old length.
 6059         */
 6060 
 6061         oadr = vdsp[1];
 6062 
 6063         if (cmd & 0x10) {       /* Table indirect */
 6064                 tblp = (u_int32_t *) ((char*) &cp->phys + oadr);
 6065                 olen = tblp[0];
 6066                 oadr = tblp[1];
 6067         } else {
 6068                 tblp = (u_int32_t *) 0;
 6069                 olen = vdsp[0] & 0xffffff;
 6070         };
 6071 
 6072         if (DEBUG_FLAGS & DEBUG_PHASE) {
 6073                 printf ("OCMD=%x\nTBLP=%x OLEN=%x OADR=%x\n",
 6074                         (unsigned) (vdsp[0] >> 24),
 6075                         (unsigned) tblp,
 6076                         (unsigned) olen,
 6077                         (unsigned) oadr);
 6078         };
 6079 
 6080         /*
 6081         **      if old phase not dataphase, leave here.
 6082         */
 6083 
 6084         if (cmd != (vdsp[0] >> 24)) {
 6085                 PRINT_ADDR(cp->xfer);
 6086                 printf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
 6087                         (unsigned)cmd, (unsigned)vdsp[0] >> 24);
 6088                 
 6089                 return;
 6090         }
 6091         if (cmd & 0x06) {
 6092                 PRINT_ADDR(cp->xfer);
 6093                 printf ("phase change %x-%x %d@%08x resid=%d.\n",
 6094                         cmd&7, sbcl&7, (unsigned)olen,
 6095                         (unsigned)oadr, (unsigned)rest);
 6096 
 6097                 OUTB (nc_dcntl, np->rv_dcntl | STD);
 6098                 return;
 6099         };
 6100 
 6101         /*
 6102         **      choose the correct patch area.
 6103         **      if savep points to one, choose the other.
 6104         */
 6105 
 6106         newcmd = cp->patch;
 6107         if (cp->phys.header.savep == vtophys (newcmd)) newcmd+=4;
 6108 
 6109         /*
 6110         **      fillin the commands
 6111         */
 6112 
 6113         newcmd[0] = ((cmd & 0x0f) << 24) | rest;
 6114         newcmd[1] = oadr + olen - rest;
 6115         newcmd[2] = SCR_JUMP;
 6116         newcmd[3] = nxtdsp;
 6117 
 6118         if (DEBUG_FLAGS & DEBUG_PHASE) {
 6119                 PRINT_ADDR(cp->xfer);
 6120                 printf ("newcmd[%d] %x %x %x %x.\n",
 6121                         (u_long)newcmd - (u_long)cp->patch,
 6122                         (unsigned)newcmd[0],
 6123                         (unsigned)newcmd[1],
 6124                         (unsigned)newcmd[2],
 6125                         (unsigned)newcmd[3]);
 6126         }
 6127         /*
 6128         **      fake the return address (to the patch).
 6129         **      and restart script processor at dispatcher.
 6130         */
 6131         np->profile.num_break++;
 6132         OUTL (nc_temp, vtophys (newcmd));
 6133         if ((cmd & 7) == 0)
 6134                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
 6135         else
 6136                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, checkatn));
 6137 }
 6138 
 6139 /*==========================================================
 6140 **
 6141 **
 6142 **      ncr chip exception handler for programmed interrupts.
 6143 **
 6144 **
 6145 **==========================================================
 6146 */
 6147 
 6148 static int ncr_show_msg (u_char * msg)
 6149 {
 6150         u_char i;
 6151         printf ("%x",*msg);
 6152         if (*msg==M_EXTENDED) {
 6153                 for (i=1;i<8;i++) {
 6154                         if (i-1>msg[1]) break;
 6155                         printf ("-%x",msg[i]);
 6156                 };
 6157                 return (i+1);
 6158         } else if ((*msg & 0xf0) == 0x20) {
 6159                 printf ("-%x",msg[1]);
 6160                 return (2);
 6161         };
 6162         return (1);
 6163 }
 6164 
 6165 void ncr_int_sir (ncb_p np)
 6166 {
 6167         u_char scntl3;
 6168         u_char chg, ofs, per, fak, wide;
 6169         u_char num = INB (nc_dsps);
 6170         ccb_p   cp=0;
 6171         u_long  dsa;
 6172         u_char  target = INB (nc_ctest0) & 0x0f;
 6173         tcb_p   tp     = &np->target[target];
 6174         int     i;
 6175         if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
 6176 
 6177         switch (num) {
 6178         case SIR_SENSE_RESTART:
 6179         case SIR_STALL_RESTART:
 6180                 break;
 6181 
 6182         default:
 6183                 /*
 6184                 **      lookup the ccb
 6185                 */
 6186                 dsa = INL (nc_dsa);
 6187                 cp = np->ccb;
 6188                 while (cp && (CCB_PHYS (cp, phys) != dsa))
 6189                         cp = cp->link_ccb;
 6190 
 6191                 assert (cp);
 6192                 if (!cp)
 6193                         goto out;
 6194                 assert (cp == np->header.cp);
 6195                 if (cp != np->header.cp)
 6196                         goto out;
 6197         }
 6198 
 6199         switch (num) {
 6200 
 6201 /*--------------------------------------------------------------------
 6202 **
 6203 **      Processing of interrupted getcc selects
 6204 **
 6205 **--------------------------------------------------------------------
 6206 */
 6207 
 6208         case SIR_SENSE_RESTART:
 6209                 /*------------------------------------------
 6210                 **      Script processor is idle.
 6211                 **      Look for interrupted "check cond"
 6212                 **------------------------------------------
 6213                 */
 6214 
 6215                 if (DEBUG_FLAGS & DEBUG_RESTART)
 6216                         printf ("%s: int#%d",ncr_name (np),num);
 6217                 cp = (ccb_p) 0;
 6218                 for (i=0; i<MAX_TARGET; i++) {
 6219                         if (DEBUG_FLAGS & DEBUG_RESTART) printf (" t%d", i);
 6220                         tp = &np->target[i];
 6221                         if (DEBUG_FLAGS & DEBUG_RESTART) printf ("+");
 6222                         cp = tp->hold_cp;
 6223                         if (!cp) continue;
 6224                         if (DEBUG_FLAGS & DEBUG_RESTART) printf ("+");
 6225                         if ((cp->host_status==HS_BUSY) &&
 6226                                 (cp->scsi_status==S_CHECK_COND))
 6227                                 break;
 6228                         if (DEBUG_FLAGS & DEBUG_RESTART) printf ("- (remove)");
 6229                         tp->hold_cp = cp = (ccb_p) 0;
 6230                 };
 6231 
 6232                 if (cp) {
 6233                         if (DEBUG_FLAGS & DEBUG_RESTART)
 6234                                 printf ("+ restart job ..\n");
 6235                         OUTL (nc_dsa, CCB_PHYS (cp, phys));
 6236                         OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, getcc));
 6237                         return;
 6238                 };
 6239 
 6240                 /*
 6241                 **      no job, resume normal processing
 6242                 */
 6243                 if (DEBUG_FLAGS & DEBUG_RESTART) printf (" -- remove trap\n");
 6244                 np->script->start0[0] =  SCR_INT ^ IFFALSE (0);
 6245                 break;
 6246 
 6247         case SIR_SENSE_FAILED:
 6248                 /*-------------------------------------------
 6249                 **      While trying to select for
 6250                 **      getting the condition code,
 6251                 **      a target reselected us.
 6252                 **-------------------------------------------
 6253                 */
 6254                 if (DEBUG_FLAGS & DEBUG_RESTART) {
 6255                         PRINT_ADDR(cp->xfer);
 6256                         printf ("in getcc reselect by t%d.\n",
 6257                                 INB(nc_ssid) & 0x0f);
 6258                 }
 6259 
 6260                 /*
 6261                 **      Mark this job
 6262                 */
 6263                 cp->host_status = HS_BUSY;
 6264                 cp->scsi_status = S_CHECK_COND;
 6265                 np->target[cp->xfer->sc_link->target].hold_cp = cp;
 6266 
 6267                 /*
 6268                 **      And patch code to restart it.
 6269                 */
 6270                 np->script->start0[0] =  SCR_INT;
 6271                 break;
 6272 
 6273 /*-----------------------------------------------------------------------------
 6274 **
 6275 **      Was Sie schon immer ueber transfermode negotiation wissen wollten ...
 6276 **
 6277 **      We try to negotiate sync and wide transfer only after
 6278 **      a successfull inquire command. We look at byte 7 of the
 6279 **      inquire data to determine the capabilities if the target.
 6280 **
 6281 **      When we try to negotiate, we append the negotiation message
 6282 **      to the identify and (maybe) simple tag message.
 6283 **      The host status field is set to HS_NEGOTIATE to mark this
 6284 **      situation.
 6285 **
 6286 **      If the target doesn't answer this message immidiately
 6287 **      (as required by the standard), the SIR_NEGO_FAIL interrupt
 6288 **      will be raised eventually.
 6289 **      The handler removes the HS_NEGOTIATE status, and sets the
 6290 **      negotiated value to the default (async / nowide).
 6291 **
 6292 **      If we receive a matching answer immediately, we check it
 6293 **      for validity, and set the values.
 6294 **
 6295 **      If we receive a Reject message immediately, we assume the
 6296 **      negotiation has failed, and fall back to standard values.
 6297 **
 6298 **      If we receive a negotiation message while not in HS_NEGOTIATE
 6299 **      state, it's a target initiated negotiation. We prepare a
 6300 **      (hopefully) valid answer, set our parameters, and send back 
 6301 **      this answer to the target.
 6302 **
 6303 **      If the target doesn't fetch the answer (no message out phase),
 6304 **      we assume the negotiation has failed, and fall back to default
 6305 **      settings.
 6306 **
 6307 **      When we set the values, we adjust them in all ccbs belonging 
 6308 **      to this target, in the controller's register, and in the "phys"
 6309 **      field of the controller's struct ncb.
 6310 **
 6311 **      Possible cases:            hs  sir   msg_in value  send   goto
 6312 **      We try try to negotiate:
 6313 **      -> target doesnt't msgin   NEG FAIL  noop   defa.  -      dispatch
 6314 **      -> target rejected our msg NEG FAIL  reject defa.  -      dispatch
 6315 **      -> target answered  (ok)   NEG SYNC  sdtr   set    -      clrack
 6316 **      -> target answered (!ok)   NEG SYNC  sdtr   defa.  REJ--->msg_bad
 6317 **      -> target answered  (ok)   NEG WIDE  wdtr   set    -      clrack
 6318 **      -> target answered (!ok)   NEG WIDE  wdtr   defa.  REJ--->msg_bad
 6319 **      -> any other msgin         NEG FAIL  noop   defa.  -      dispatch
 6320 **
 6321 **      Target tries to negotiate:
 6322 **      -> incoming message        --- SYNC  sdtr   set    SDTR   -
 6323 **      -> incoming message        --- WIDE  wdtr   set    WDTR   -
 6324 **      We sent our answer:
 6325 **      -> target doesn't msgout   --- PROTO ?      defa.  -      dispatch
 6326 **
 6327 **-----------------------------------------------------------------------------
 6328 */
 6329 
 6330         case SIR_NEGO_FAILED:
 6331                 /*-------------------------------------------------------
 6332                 **
 6333                 **      Negotiation failed.
 6334                 **      Target doesn't send an answer message,
 6335                 **      or target rejected our message.
 6336                 **
 6337                 **      Remove negotiation request.
 6338                 **
 6339                 **-------------------------------------------------------
 6340                 */
 6341                 OUTB (HS_PRT, HS_BUSY);
 6342 
 6343                 /* fall through */
 6344 
 6345         case SIR_NEGO_PROTO:
 6346                 /*-------------------------------------------------------
 6347                 **
 6348                 **      Negotiation failed.
 6349                 **      Target doesn't fetch the answer message.
 6350                 **
 6351                 **-------------------------------------------------------
 6352                 */
 6353 
 6354                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6355                         PRINT_ADDR(cp->xfer);
 6356                         printf ("negotiation failed sir=%x status=%x.\n",
 6357                                 num, cp->nego_status);
 6358                 };
 6359 
 6360                 /*
 6361                 **      any error in negotiation:
 6362                 **      fall back to default mode.
 6363                 */
 6364                 switch (cp->nego_status) {
 6365 
 6366                 case NS_SYNC:
 6367                         ncr_setsync (np, cp, 0, 0xe0);
 6368                         break;
 6369 
 6370                 case NS_WIDE:
 6371                         ncr_setwide (np, cp, 0, 0);
 6372                         break;
 6373 
 6374                 };
 6375                 np->msgin [0] = M_NOOP;
 6376                 np->msgout[0] = M_NOOP;
 6377                 cp->nego_status = 0;
 6378                 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
 6379                 break;
 6380 
 6381         case SIR_NEGO_SYNC:
 6382                 /*
 6383                 **      Synchronous request message received.
 6384                 */
 6385 
 6386                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6387                         PRINT_ADDR(cp->xfer);
 6388                         printf ("sync msgin: ");
 6389                         (void) ncr_show_msg (np->msgin);
 6390                         printf (".\n");
 6391                 };
 6392 
 6393                 /*
 6394                 **      get requested values.
 6395                 */
 6396 
 6397                 chg = 0;
 6398                 per = np->msgin[3];
 6399                 ofs = np->msgin[4];
 6400                 if (ofs==0) per=255;
 6401 
 6402                 /*
 6403                 **      if target sends SDTR message,
 6404                 **            it CAN transfer synch.
 6405                 */
 6406 
 6407                 if (ofs)
 6408                         tp->inqdata[7] |= INQ7_SYNC;
 6409 
 6410                 /*
 6411                 **      check values against driver limits.
 6412                 */
 6413 
 6414                 if (per < np->minsync)
 6415                         {chg = 1; per = np->minsync;}
 6416                 if (per < tp->minsync)
 6417                         {chg = 1; per = tp->minsync;}
 6418                 if (ofs > tp->maxoffs)
 6419                         {chg = 1; ofs = tp->maxoffs;}
 6420 
 6421                 /*
 6422                 **      Check against controller limits.
 6423                 */
 6424 
 6425                 fak     = 7;
 6426                 scntl3  = 0;
 6427                 if (ofs != 0) {
 6428                         ncr_getsync(np, per, &fak, &scntl3);
 6429                         if (fak > 7) {
 6430                                 chg = 1;
 6431                                 ofs = 0;
 6432                         }
 6433                 }
 6434                 if (ofs == 0) {
 6435                         fak     = 7;
 6436                         per     = 0;
 6437                         scntl3  = 0;
 6438                         tp->minsync = 0;
 6439                 }
 6440 
 6441                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6442                         PRINT_ADDR(cp->xfer);
 6443                         printf ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
 6444                                 per, scntl3, ofs, fak, chg);
 6445                 }
 6446 
 6447                 if (INB (HS_PRT) == HS_NEGOTIATE) {
 6448                         OUTB (HS_PRT, HS_BUSY);
 6449                         switch (cp->nego_status) {
 6450 
 6451                         case NS_SYNC:
 6452                                 /*
 6453                                 **      This was an answer message
 6454                                 */
 6455                                 if (chg) {
 6456                                         /*
 6457                                         **      Answer wasn't acceptable.
 6458                                         */
 6459                                         ncr_setsync (np, cp, 0, 0xe0);
 6460                                         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
 6461                                 } else {
 6462                                         /*
 6463                                         **      Answer is ok.
 6464                                         */
 6465                                         ncr_setsync (np,cp,scntl3,(fak<<5)|ofs);
 6466                                         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
 6467                                 };
 6468                                 return;
 6469 
 6470                         case NS_WIDE:
 6471                                 ncr_setwide (np, cp, 0, 0);
 6472                                 break;
 6473                         };
 6474                 };
 6475 
 6476                 /*
 6477                 **      It was a request. Set value and
 6478                 **      prepare an answer message
 6479                 */
 6480 
 6481                 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
 6482 
 6483                 np->msgout[0] = M_EXTENDED;
 6484                 np->msgout[1] = 3;
 6485                 np->msgout[2] = M_X_SYNC_REQ;
 6486                 np->msgout[3] = per;
 6487                 np->msgout[4] = ofs;
 6488 
 6489                 cp->nego_status = NS_SYNC;
 6490 
 6491                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6492                         PRINT_ADDR(cp->xfer);
 6493                         printf ("sync msgout: ");
 6494                         (void) ncr_show_msg (np->msgout);
 6495                         printf (".\n");
 6496                 }
 6497 
 6498                 if (!ofs) {
 6499                         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
 6500                         return;
 6501                 }
 6502                 np->msgin [0] = M_NOOP;
 6503 
 6504                 break;
 6505 
 6506         case SIR_NEGO_WIDE:
 6507                 /*
 6508                 **      Wide request message received.
 6509                 */
 6510                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6511                         PRINT_ADDR(cp->xfer);
 6512                         printf ("wide msgin: ");
 6513                         (void) ncr_show_msg (np->msgin);
 6514                         printf (".\n");
 6515                 };
 6516 
 6517                 /*
 6518                 **      get requested values.
 6519                 */
 6520 
 6521                 chg  = 0;
 6522                 wide = np->msgin[3];
 6523 
 6524                 /*
 6525                 **      if target sends WDTR message,
 6526                 **            it CAN transfer wide.
 6527                 */
 6528 
 6529                 if (wide)
 6530                         tp->inqdata[7] |= INQ7_WIDE16;
 6531 
 6532                 /*
 6533                 **      check values against driver limits.
 6534                 */
 6535 
 6536                 if (wide > tp->usrwide)
 6537                         {chg = 1; wide = tp->usrwide;}
 6538 
 6539                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6540                         PRINT_ADDR(cp->xfer);
 6541                         printf ("wide: wide=%d chg=%d.\n", wide, chg);
 6542                 }
 6543 
 6544                 if (INB (HS_PRT) == HS_NEGOTIATE) {
 6545                         OUTB (HS_PRT, HS_BUSY);
 6546                         switch (cp->nego_status) {
 6547 
 6548                         case NS_WIDE:
 6549                                 /*
 6550                                 **      This was an answer message
 6551                                 */
 6552                                 if (chg) {
 6553                                         /*
 6554                                         **      Answer wasn't acceptable.
 6555                                         */
 6556                                         ncr_setwide (np, cp, 0, 1);
 6557                                         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
 6558                                 } else {
 6559                                         /*
 6560                                         **      Answer is ok.
 6561                                         */
 6562                                         ncr_setwide (np, cp, wide, 1);
 6563                                         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
 6564                                 };
 6565                                 return;
 6566 
 6567                         case NS_SYNC:
 6568                                 ncr_setsync (np, cp, 0, 0xe0);
 6569                                 break;
 6570                         };
 6571                 };
 6572 
 6573                 /*
 6574                 **      It was a request, set value and
 6575                 **      prepare an answer message
 6576                 */
 6577 
 6578                 ncr_setwide (np, cp, wide, 1);
 6579 
 6580                 np->msgout[0] = M_EXTENDED;
 6581                 np->msgout[1] = 2;
 6582                 np->msgout[2] = M_X_WIDE_REQ;
 6583                 np->msgout[3] = wide;
 6584 
 6585                 np->msgin [0] = M_NOOP;
 6586 
 6587                 cp->nego_status = NS_WIDE;
 6588 
 6589                 if (DEBUG_FLAGS & DEBUG_NEGO) {
 6590                         PRINT_ADDR(cp->xfer);
 6591                         printf ("wide msgout: ");
 6592                         (void) ncr_show_msg (np->msgout);
 6593                         printf (".\n");
 6594                 }
 6595                 break;
 6596 
 6597 /*--------------------------------------------------------------------
 6598 **
 6599 **      Processing of special messages
 6600 **
 6601 **--------------------------------------------------------------------
 6602 */
 6603 
 6604         case SIR_REJECT_RECEIVED:
 6605                 /*-----------------------------------------------
 6606                 **
 6607                 **      We received a M_REJECT message.
 6608                 **
 6609                 **-----------------------------------------------
 6610                 */
 6611 
 6612                 PRINT_ADDR(cp->xfer);
 6613                 printf ("M_REJECT received (%x:%x).\n",
 6614                         (unsigned)np->lastmsg, np->msgout[0]);
 6615                 break;
 6616 
 6617         case SIR_REJECT_SENT:
 6618                 /*-----------------------------------------------
 6619                 **
 6620                 **      We received an unknown message
 6621                 **
 6622                 **-----------------------------------------------
 6623                 */
 6624 
 6625                 PRINT_ADDR(cp->xfer);
 6626                 printf ("M_REJECT sent for ");
 6627                 (void) ncr_show_msg (np->msgin);
 6628                 printf (".\n");
 6629                 break;
 6630 
 6631 /*--------------------------------------------------------------------
 6632 **
 6633 **      Processing of special messages
 6634 **
 6635 **--------------------------------------------------------------------
 6636 */
 6637 
 6638         case SIR_IGN_RESIDUE:
 6639                 /*-----------------------------------------------
 6640                 **
 6641                 **      We received an IGNORE RESIDUE message,
 6642                 **      which couldn't be handled by the script.
 6643                 **
 6644                 **-----------------------------------------------
 6645                 */
 6646 
 6647                 PRINT_ADDR(cp->xfer);
 6648                 printf ("M_IGN_RESIDUE received, but not yet implemented.\n");
 6649                 break;
 6650 
 6651         case SIR_MISSING_SAVE:
 6652                 /*-----------------------------------------------
 6653                 **
 6654                 **      We received an DISCONNECT message,
 6655                 **      but the datapointer wasn't saved before.
 6656                 **
 6657                 **-----------------------------------------------
 6658                 */
 6659 
 6660                 PRINT_ADDR(cp->xfer);
 6661                 printf ("M_DISCONNECT received, but datapointer not saved:\n"
 6662                         "\tdata=%x save=%x goal=%x.\n",
 6663                         (unsigned) INL (nc_temp),
 6664                         (unsigned) np->header.savep,
 6665                         (unsigned) np->header.goalp);
 6666                 break;
 6667 
 6668 /*--------------------------------------------------------------------
 6669 **
 6670 **      Processing of a "S_QUEUE_FULL" status.
 6671 **
 6672 **      The current command has been rejected,
 6673 **      because there are too many in the command queue.
 6674 **      We have started too many commands for that target.
 6675 **
 6676 **      If possible, reinsert at head of queue.
 6677 **      Stall queue until there are no disconnected jobs
 6678 **      (ncr is REALLY idle). Then restart processing.
 6679 **
 6680 **      We should restart the current job after the controller
 6681 **      has become idle. But this is not yet implemented.
 6682 **
 6683 **--------------------------------------------------------------------
 6684 */
 6685         case SIR_STALL_QUEUE:
 6686                 /*-----------------------------------------------
 6687                 **
 6688                 **      Stall the start queue.
 6689                 **
 6690                 **-----------------------------------------------
 6691                 */
 6692                 PRINT_ADDR(cp->xfer);
 6693                 printf ("queue full.\n");
 6694 
 6695                 np->script->start1[0] =  SCR_INT;
 6696 
 6697                 /*
 6698                 **      Try to disable tagged transfers.
 6699                 */
 6700                 ncr_setmaxtags (&np->target[target], 0);
 6701 
 6702                 /*
 6703                 ** @QUEUE@
 6704                 **
 6705                 **      Should update the launch field of the
 6706                 **      current job to be able to restart it.
 6707                 **      Then prepend it to the start queue.
 6708                 */
 6709 
 6710                 /* fall through */
 6711 
 6712         case SIR_STALL_RESTART:
 6713                 /*-----------------------------------------------
 6714                 **
 6715                 **      Enable selecting again,
 6716                 **      if NO disconnected jobs.
 6717                 **
 6718                 **-----------------------------------------------
 6719                 */
 6720                 /*
 6721                 **      Look for a disconnected job.
 6722                 */
 6723                 cp = np->ccb;
 6724                 while (cp && cp->host_status != HS_DISCONNECT)
 6725                         cp = cp->link_ccb;
 6726 
 6727                 /*
 6728                 **      if there is one, ...
 6729                 */
 6730                 if (cp) {
 6731                         /*
 6732                         **      wait for reselection
 6733                         */
 6734                         OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
 6735                         return;
 6736                 };
 6737 
 6738                 /*
 6739                 **      else remove the interrupt.
 6740                 */
 6741 
 6742                 printf ("%s: queue empty.\n", ncr_name (np));
 6743                 np->script->start1[0] =  SCR_INT ^ IFFALSE (0);
 6744                 break;
 6745         };
 6746 
 6747 out:
 6748         OUTB (nc_dcntl, np->rv_dcntl | STD);
 6749 }
 6750 
 6751 /*==========================================================
 6752 **
 6753 **
 6754 **      Aquire a control block
 6755 **
 6756 **
 6757 **==========================================================
 6758 */
 6759 
 6760 static  ccb_p ncr_get_ccb
 6761         (ncb_p np, u_long flags, u_long target, u_long lun)
 6762 {
 6763         lcb_p lp;
 6764         ccb_p cp = (ccb_p) 0;
 6765         int oldspl;
 6766 
 6767         oldspl = splhigh();
 6768         /*
 6769         **      Lun structure available ?
 6770         */
 6771 
 6772         lp = np->target[target].lp[lun];
 6773         if (lp) {
 6774                 cp = lp->next_ccb;
 6775 
 6776                 /*
 6777                 **      Look for free CCB
 6778                 */
 6779 
 6780                 while (cp && cp->magic) {
 6781                         cp = cp->next_ccb;
 6782                 }
 6783         }
 6784 
 6785         /*
 6786         **      if nothing available, take the default.
 6787         */
 6788 
 6789         if (!cp) cp = np->ccb;
 6790 
 6791         /*
 6792         **      Wait until available.
 6793         */
 6794 
 6795         while (cp->magic) {
 6796                 if (flags & SCSI_NOSLEEP) break;
 6797                 if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
 6798                         break;
 6799         };
 6800 
 6801         if (cp->magic) {
 6802                 splx(oldspl);
 6803                 return ((ccb_p) 0);
 6804         }
 6805 
 6806         cp->magic = 1;
 6807         splx(oldspl);
 6808         return (cp);
 6809 }
 6810 
 6811 /*==========================================================
 6812 **
 6813 **
 6814 **      Release one control block
 6815 **
 6816 **
 6817 **==========================================================
 6818 */
 6819 
 6820 void ncr_free_ccb (ncb_p np, ccb_p cp, int flags)
 6821 {
 6822         /*
 6823         **    sanity
 6824         */
 6825 
 6826         assert (cp != NULL);
 6827 
 6828         cp -> host_status = HS_IDLE;
 6829         cp -> magic = 0;
 6830         if (cp == np->ccb)
 6831                 wakeup ((caddr_t) cp);
 6832 }
 6833 
 6834 /*==========================================================
 6835 **
 6836 **
 6837 **      Allocation of resources for Targets/Luns/Tags.
 6838 **
 6839 **
 6840 **==========================================================
 6841 */
 6842 
 6843 static  void ncr_alloc_ccb (ncb_p np, u_long target, u_long lun)
 6844 {
 6845         tcb_p tp;
 6846         lcb_p lp;
 6847         ccb_p cp;
 6848 
 6849         assert (np != NULL);
 6850 
 6851         if (target>=MAX_TARGET) return;
 6852         if (lun   >=MAX_LUN   ) return;
 6853 
 6854         tp=&np->target[target];
 6855 
 6856         if (!tp->jump_tcb.l_cmd) {
 6857 
 6858                 /*
 6859                 **      initialize it.
 6860                 */
 6861                 tp->jump_tcb.l_cmd   = (SCR_JUMP^IFFALSE (DATA (0x80 + target)));
 6862                 tp->jump_tcb.l_paddr = np->jump_tcb.l_paddr;
 6863 
 6864                 tp->getscr[0] =
 6865                         (np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
 6866                 tp->getscr[1] = vtophys (&tp->sval);
 6867                 tp->getscr[2] = np->paddr + offsetof (struct ncr_reg, nc_sxfer);
 6868                 tp->getscr[3] =
 6869                         (np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
 6870                 tp->getscr[4] = vtophys (&tp->wval);
 6871                 tp->getscr[5] = np->paddr + offsetof (struct ncr_reg, nc_scntl3);
 6872 
 6873                 assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
 6874                         offsetof(struct tcb    , sval    )) &3) == 0);
 6875                 assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
 6876                         offsetof(struct tcb    , wval    )) &3) == 0);
 6877 
 6878                 tp->call_lun.l_cmd   = (SCR_CALL);
 6879                 tp->call_lun.l_paddr = NCB_SCRIPT_PHYS (np, resel_lun);
 6880 
 6881                 tp->jump_lcb.l_cmd   = (SCR_JUMP);
 6882                 tp->jump_lcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
 6883                 np->jump_tcb.l_paddr = vtophys (&tp->jump_tcb);
 6884 
 6885                 ncr_setmaxtags (tp, SCSI_NCR_DFLT_TAGS);
 6886         }
 6887 
 6888         /*
 6889         **      Logic unit control block
 6890         */
 6891         lp = tp->lp[lun];
 6892         if (!lp) {
 6893                 /*
 6894                 **      Allocate a lcb
 6895                 */
 6896                 lp = (lcb_p) malloc (sizeof (struct lcb), M_DEVBUF, M_NOWAIT);
 6897                 if (!lp) return;
 6898 
 6899                 /*
 6900                 **      Initialize it
 6901                 */
 6902                 bzero (lp, sizeof (*lp));
 6903                 lp->jump_lcb.l_cmd   = (SCR_JUMP ^ IFFALSE (DATA (lun)));
 6904                 lp->jump_lcb.l_paddr = tp->jump_lcb.l_paddr;
 6905 
 6906                 lp->call_tag.l_cmd   = (SCR_CALL);
 6907                 lp->call_tag.l_paddr = NCB_SCRIPT_PHYS (np, resel_tag);
 6908 
 6909                 lp->jump_ccb.l_cmd   = (SCR_JUMP);
 6910                 lp->jump_ccb.l_paddr = NCB_SCRIPTH_PHYS (np, aborttag);
 6911 
 6912                 lp->actlink = 1;
 6913 
 6914                 /*
 6915                 **   Chain into LUN list
 6916                 */
 6917                 tp->jump_lcb.l_paddr = vtophys (&lp->jump_lcb);
 6918                 tp->lp[lun] = lp;
 6919 
 6920         }
 6921 
 6922         /*
 6923         **      Limit possible number of ccbs.
 6924         **
 6925         **      If tagged command queueing is enabled,
 6926         **      can use more than one ccb.
 6927         */
 6928 
 6929         if (np->actccbs >= MAX_START-2) return;
 6930         if (lp->actccbs && (lp->actccbs >= lp->reqccbs))
 6931                 return;
 6932 
 6933         /*
 6934         **      Allocate a ccb
 6935         */
 6936         cp = (ccb_p) malloc (sizeof (struct ccb), M_DEVBUF, M_NOWAIT);
 6937 
 6938         if (!cp)
 6939                 return;
 6940 
 6941         if (DEBUG_FLAGS & DEBUG_ALLOC) {
 6942                 printf ("new ccb @%x.\n", (unsigned) cp);
 6943         }
 6944 
 6945         /*
 6946         **      Count it
 6947         */
 6948         lp->actccbs++;
 6949         np->actccbs++;
 6950 
 6951         /*
 6952         **      Initialize it
 6953         */
 6954         bzero (cp, sizeof (*cp));
 6955 
 6956         /*
 6957         **      Fill in physical addresses
 6958         */
 6959 
 6960         cp->p_ccb            = vtophys (cp);
 6961 
 6962         /*
 6963         **      Chain into reselect list
 6964         */
 6965         cp->jump_ccb.l_cmd   = SCR_JUMP;
 6966         cp->jump_ccb.l_paddr = lp->jump_ccb.l_paddr;
 6967         lp->jump_ccb.l_paddr = CCB_PHYS (cp, jump_ccb);
 6968         cp->call_tmp.l_cmd   = SCR_CALL;
 6969         cp->call_tmp.l_paddr = NCB_SCRIPT_PHYS (np, resel_tmp);
 6970 
 6971         /*
 6972         **      Chain into wakeup list
 6973         */
 6974         cp->link_ccb      = np->ccb->link_ccb;
 6975         np->ccb->link_ccb  = cp;
 6976 
 6977         /*
 6978         **      Chain into CCB list
 6979         */
 6980         cp->next_ccb    = lp->next_ccb;
 6981         lp->next_ccb    = cp;
 6982 }
 6983 
 6984 /*==========================================================
 6985 **
 6986 **
 6987 **      Announce the number of ccbs/tags to the scsi driver.
 6988 **
 6989 **
 6990 **==========================================================
 6991 */
 6992 
 6993 static void ncr_opennings (ncb_p np, lcb_p lp, struct scsi_xfer * xp)
 6994 {
 6995         /*
 6996         **      want to reduce the number ...
 6997         */
 6998         if (lp->actlink > lp->reqlink) {
 6999 
 7000                 /*
 7001                 **      Try to  reduce the count.
 7002                 **      We assume to run at splbio ..
 7003                 */
 7004                 u_char diff = lp->actlink - lp->reqlink;
 7005 
 7006                 if (!diff) return;
 7007 
 7008 #ifdef __NetBSD__
 7009                 if (diff > xp->sc_link->openings)
 7010                         diff = xp->sc_link->openings;
 7011 
 7012                 xp->sc_link->openings   -= diff;
 7013 #else /* !__NetBSD__ */
 7014                 if (diff > xp->sc_link->opennings)
 7015                         diff = xp->sc_link->opennings;
 7016 
 7017                 xp->sc_link->opennings  -= diff;
 7018 #endif /* __NetBSD__ */
 7019                 lp->actlink             -= diff;
 7020                 if (DEBUG_FLAGS & DEBUG_TAGS)
 7021                         printf ("%s: actlink: diff=%d, new=%d, req=%d\n",
 7022                                 ncr_name(np), diff, lp->actlink, lp->reqlink);
 7023                 return;
 7024         };
 7025 
 7026         /*
 7027         **      want to increase the number ?
 7028         */
 7029         if (lp->reqlink > lp->actlink) {
 7030                 u_char diff = lp->reqlink - lp->actlink;
 7031 
 7032 #ifdef __NetBSD__
 7033                 xp->sc_link->openings   += diff;
 7034 #else /* !__NetBSD__ */
 7035                 xp->sc_link->opennings  += diff;
 7036 #endif /* __NetBSD__ */
 7037                 lp->actlink             += diff;
 7038                 wakeup ((caddr_t) xp->sc_link);
 7039                 if (DEBUG_FLAGS & DEBUG_TAGS)
 7040                         printf ("%s: actlink: diff=%d, new=%d, req=%d\n",
 7041                                 ncr_name(np), diff, lp->actlink, lp->reqlink);
 7042         };
 7043 }
 7044 
 7045 /*==========================================================
 7046 **
 7047 **
 7048 **      Build Scatter Gather Block
 7049 **
 7050 **
 7051 **==========================================================
 7052 **
 7053 **      The transfer area may be scattered among
 7054 **      several non adjacent physical pages.
 7055 **
 7056 **      We may use MAX_SCATTER blocks.
 7057 **
 7058 **----------------------------------------------------------
 7059 */
 7060 
 7061 static  int     ncr_scatter
 7062         (struct dsb* phys, vm_offset_t vaddr, vm_size_t datalen)
 7063 {
 7064         u_long  paddr, pnext;
 7065 
 7066         u_short segment  = 0;
 7067         u_long  segsize, segaddr;
 7068         u_long  size, csize    = 0;
 7069         u_long  chunk = MAX_SIZE;
 7070         int     free;
 7071 
 7072         bzero (&phys->data, sizeof (phys->data));
 7073         if (!datalen) return (0);
 7074 
 7075         paddr = vtophys (vaddr);
 7076 
 7077         /*
 7078         **      insert extra break points at a distance of chunk.
 7079         **      We try to reduce the number of interrupts caused
 7080         **      by unexpected phase changes due to disconnects.
 7081         **      A typical harddisk may disconnect before ANY block.
 7082         **      If we wanted to avoid unexpected phase changes at all
 7083         **      we had to use a break point every 512 bytes.
 7084         **      Of course the number of scatter/gather blocks is
 7085         **      limited.
 7086         */
 7087 
 7088         free = MAX_SCATTER - 1;
 7089 
 7090         if (vaddr & PAGE_MASK) free -= datalen / PAGE_SIZE;
 7091 
 7092         if (free>1)
 7093                 while ((chunk * free >= 2 * datalen) && (chunk>=1024))
 7094                         chunk /= 2;
 7095 
 7096         if(DEBUG_FLAGS & DEBUG_SCATTER)
 7097                 printf("ncr?:\tscattering virtual=0x%x size=%d chunk=%d.\n",
 7098                         (unsigned) vaddr, (unsigned) datalen, (unsigned) chunk);
 7099 
 7100         /*
 7101         **   Build data descriptors.
 7102         */
 7103         while (datalen && (segment < MAX_SCATTER)) {
 7104 
 7105                 /*
 7106                 **      this segment is empty
 7107                 */
 7108                 segsize = 0;
 7109                 segaddr = paddr;
 7110                 pnext   = paddr;
 7111 
 7112                 if (!csize) csize = chunk;
 7113 
 7114                 while ((datalen) && (paddr == pnext) && (csize)) {
 7115 
 7116                         /*
 7117                         **      continue this segment
 7118                         */
 7119                         pnext = (paddr & (~PAGE_MASK)) + PAGE_SIZE;
 7120 
 7121                         /*
 7122                         **      Compute max size
 7123                         */
 7124 
 7125                         size = pnext - paddr;           /* page size */
 7126                         if (size > datalen) size = datalen;  /* data size */
 7127                         if (size > csize  ) size = csize  ;  /* chunksize */
 7128 
 7129                         segsize += size;
 7130                         vaddr   += size;
 7131                         csize   -= size;
 7132                         datalen -= size;
 7133                         paddr    = vtophys (vaddr);
 7134                 };
 7135 
 7136                 if(DEBUG_FLAGS & DEBUG_SCATTER)
 7137                         printf ("\tseg #%d  addr=%x  size=%d  (rest=%d).\n",
 7138                         segment,
 7139                         (unsigned) segaddr,
 7140                         (unsigned) segsize,
 7141                         (unsigned) datalen);
 7142 
 7143                 phys->data[segment].addr = segaddr;
 7144                 phys->data[segment].size = segsize;
 7145                 segment++;
 7146         }
 7147 
 7148         if (datalen) {
 7149                 printf("ncr?: scatter/gather failed (residue=%d).\n",
 7150                         (unsigned) datalen);
 7151                 return (-1);
 7152         };
 7153 
 7154         return (segment);
 7155 }
 7156 
 7157 /*==========================================================
 7158 **
 7159 **
 7160 **      Test the pci bus snoop logic :-(
 7161 **
 7162 **      Has to be called with interrupts disabled.
 7163 **
 7164 **
 7165 **==========================================================
 7166 */
 7167 
 7168 #ifndef NCR_IOMAPPED
 7169 static int ncr_regtest (struct ncb* np)
 7170 {
 7171         register volatile u_int32_t data, *addr;
 7172         /*
 7173         **      ncr registers may NOT be cached.
 7174         **      write 0xffffffff to a read only register area,
 7175         **      and try to read it back.
 7176         */
 7177         addr = (volatile u_int32_t *) &np->reg->nc_dstat;
 7178         data = 0xffffffff;
 7179         *addr= data;
 7180         data = *addr;
 7181 #if 1
 7182         if (data == 0xffffffff) {
 7183 #else
 7184         if ((data & 0xe2f0fffd) != 0x02000080) {
 7185 #endif
 7186                 printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
 7187                         (unsigned) data);
 7188                 return (0x10);
 7189         };
 7190         return (0);
 7191 }
 7192 #endif
 7193 
 7194 static int ncr_snooptest (struct ncb* np)
 7195 {
 7196         u_int32_t ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
 7197         int     i, err=0;
 7198 #ifndef NCR_IOMAPPED
 7199         err |= ncr_regtest (np);
 7200         if (err) return (err);
 7201 #endif
 7202         /*
 7203         **      init
 7204         */
 7205         pc  = NCB_SCRIPTH_PHYS (np, snooptest);
 7206         host_wr = 1;
 7207         ncr_wr  = 2;
 7208         /*
 7209         **      Set memory and register.
 7210         */
 7211         ncr_cache = host_wr;
 7212         OUTL (nc_temp, ncr_wr);
 7213         /*
 7214         **      Start script (exchange values)
 7215         */
 7216         OUTL (nc_dsp, pc);
 7217         /*
 7218         **      Wait 'til done (with timeout)
 7219         */
 7220         for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
 7221                 if (INB(nc_istat) & (INTF|SIP|DIP))
 7222                         break;
 7223         /*
 7224         **      Save termination position.
 7225         */
 7226         pc = INL (nc_dsp);
 7227         /*
 7228         **      Read memory and register.
 7229         */
 7230         host_rd = ncr_cache;
 7231         ncr_rd  = INL (nc_scratcha);
 7232         ncr_bk  = INL (nc_temp);
 7233         /*
 7234         **      Reset ncr chip
 7235         */
 7236         OUTB (nc_istat,  SRST);
 7237         DELAY (1000);
 7238         OUTB (nc_istat,  0   );
 7239         /*
 7240         **      check for timeout
 7241         */
 7242         if (i>=NCR_SNOOP_TIMEOUT) {
 7243                 printf ("CACHE TEST FAILED: timeout.\n");
 7244                 return (0x20);
 7245         };
 7246         /*
 7247         **      Check termination position.
 7248         */
 7249         if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
 7250                 printf ("CACHE TEST FAILED: script execution failed.\n");
 7251                 printf ("start=%08lx, pc=%08lx, end=%08lx\n", 
 7252                         (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
 7253                         (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
 7254                 return (0x40);
 7255         };
 7256         /*
 7257         **      Show results.
 7258         */
 7259         if (host_wr != ncr_rd) {
 7260                 printf ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
 7261                         (int) host_wr, (int) ncr_rd);
 7262                 err |= 1;
 7263         };
 7264         if (host_rd != ncr_wr) {
 7265                 printf ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
 7266                         (int) ncr_wr, (int) host_rd);
 7267                 err |= 2;
 7268         };
 7269         if (ncr_bk != ncr_wr) {
 7270                 printf ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
 7271                         (int) ncr_wr, (int) ncr_bk);
 7272                 err |= 4;
 7273         };
 7274         return (err);
 7275 }
 7276 
 7277 /*==========================================================
 7278 **
 7279 **
 7280 **      Profiling the drivers and targets performance.
 7281 **
 7282 **
 7283 **==========================================================
 7284 */
 7285 
 7286 /*
 7287 **      Compute the difference in milliseconds.
 7288 **/
 7289 
 7290 static  int ncr_delta (struct timeval * from, struct timeval * to)
 7291 {
 7292         if (!from->tv_sec) return (-1);
 7293         if (!to  ->tv_sec) return (-2);
 7294         return ( (to->tv_sec  - from->tv_sec  -       2)*1000+
 7295                 +(to->tv_usec - from->tv_usec + 2000000)/1000);
 7296 }
 7297 
 7298 #define PROFILE  cp->phys.header.stamp
 7299 static  void ncb_profile (ncb_p np, ccb_p cp)
 7300 {
 7301         int co, da, st, en, di, se, post,work,disc;
 7302         u_long diff;
 7303 
 7304         gettime(&PROFILE.end);
 7305 
 7306         st = ncr_delta (&PROFILE.start,&PROFILE.status);
 7307         if (st<0) return;       /* status  not reached  */
 7308 
 7309         da = ncr_delta (&PROFILE.start,&PROFILE.data);
 7310         if (da<0) return;       /* No data transfer phase */
 7311 
 7312         co = ncr_delta (&PROFILE.start,&PROFILE.command);
 7313         if (co<0) return;       /* command not executed */
 7314 
 7315         en = ncr_delta (&PROFILE.start,&PROFILE.end),
 7316         di = ncr_delta (&PROFILE.start,&PROFILE.disconnect),
 7317         se = ncr_delta (&PROFILE.start,&PROFILE.select);
 7318         post = en - st;
 7319 
 7320         /*
 7321         **      @PROFILE@  Disconnect time invalid if multiple disconnects
 7322         */
 7323 
 7324         if (di>=0) disc = se-di; else  disc = 0;
 7325 
 7326         work = (st - co) - disc;
 7327 
 7328         diff = (np->disc_phys - np->disc_ref) & 0xff;
 7329         np->disc_ref += diff;
 7330 
 7331         np->profile.num_trans   += 1;
 7332         if (cp->xfer)
 7333         np->profile.num_bytes   += cp->xfer->datalen;
 7334         np->profile.num_disc    += diff;
 7335         np->profile.ms_setup    += co;
 7336         np->profile.ms_data     += work;
 7337         np->profile.ms_disc     += disc;
 7338         np->profile.ms_post     += post;
 7339 }
 7340 #undef PROFILE
 7341 
 7342 /*==========================================================
 7343 **
 7344 **
 7345 **      Device lookup.
 7346 **
 7347 **      @GENSCSI@ should be integrated to scsiconf.c
 7348 **
 7349 **
 7350 **==========================================================
 7351 */
 7352 
 7353 #ifndef NEW_SCSICONF
 7354 
 7355 struct table_entry {
 7356         char *  manufacturer;
 7357         char *  model;
 7358         char *  version;
 7359         u_long  info;
 7360 };
 7361 
 7362 static struct table_entry device_tab[] =
 7363 {
 7364 #ifdef NCR_GETCC_WITHMSG
 7365         {"", "", "", QUIRK_NOMSG},
 7366         {"SONY", "SDT-5000", "3.17", QUIRK_NOMSG},
 7367         {"WangDAT", "Model 2600", "01.7", QUIRK_NOMSG},
 7368         {"WangDAT", "Model 3200", "02.2", QUIRK_NOMSG},
 7369         {"WangDAT", "Model 1300", "02.4", QUIRK_NOMSG},
 7370 #endif
 7371         {"", "", "", 0} /* catch all: must be last entry. */
 7372 };
 7373 
 7374 static u_long ncr_lookup(char * id)
 7375 {
 7376         struct table_entry * p = device_tab;
 7377         char *d, *r, c;
 7378 
 7379         for (;;p++) {
 7380 
 7381                 d = id+8;
 7382                 r = p->manufacturer;
 7383                 while ((c=*r++)) if (c!=*d++) break;
 7384                 if (c) continue;
 7385 
 7386                 d = id+16;
 7387                 r = p->model;
 7388                 while ((c=*r++)) if (c!=*d++) break;
 7389                 if (c) continue;
 7390 
 7391                 d = id+32;
 7392                 r = p->version;
 7393                 while ((c=*r++)) if (c!=*d++) break;
 7394                 if (c) continue;
 7395 
 7396                 return (p->info);
 7397         }
 7398 }
 7399 #endif
 7400 
 7401 /*==========================================================
 7402 **
 7403 **      Determine the ncr's clock frequency.
 7404 **      This is important for the negotiation
 7405 **      of the synchronous transfer rate.
 7406 **
 7407 **==========================================================
 7408 **
 7409 **      Note: we have to return the correct value.
 7410 **      THERE IS NO SAVE DEFAULT VALUE.
 7411 **
 7412 **      Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
 7413 **      53C860 and 53C875 rev. 1 support fast20 transfers but 
 7414 **      do not have a clock doubler and so are provided with a 
 7415 **      80 MHz clock. All other fast20 boards incorporate a doubler 
 7416 **      and so should be delivered with a 40 MHz clock.
 7417 **      The future fast40 chips (895/895) use a 40 Mhz base clock 
 7418 **      and provide a clock quadrupler (160 Mhz). The code below 
 7419 **      tries to deal as cleverly as possible with all this stuff.
 7420 **
 7421 **----------------------------------------------------------
 7422 */
 7423 
 7424 /*
 7425  *      Select NCR SCSI clock frequency
 7426  */
 7427 static void ncr_selectclock(ncb_p np, u_char scntl3)
 7428 {
 7429         if (np->multiplier < 2) {
 7430                 OUTB(nc_scntl3, scntl3);
 7431                 return;
 7432         }
 7433 
 7434         if (bootverbose >= 2)
 7435                 printf ("%s: enabling clock multiplier\n", ncr_name(np));
 7436 
 7437         OUTB(nc_stest1, DBLEN);    /* Enable clock multiplier             */
 7438         if (np->multiplier > 2) {  /* Poll bit 5 of stest4 for quadrupler */
 7439                 int i = 20;
 7440                 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
 7441                         DELAY(20);
 7442                 if (!i)
 7443                         printf("%s: the chip cannot lock the frequency\n", ncr_name(np));
 7444         } else                  /* Wait 20 micro-seconds for doubler    */
 7445                 DELAY(20);
 7446         OUTB(nc_stest3, HSC);           /* Halt the scsi clock          */
 7447         OUTB(nc_scntl3, scntl3);
 7448         OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier      */
 7449         OUTB(nc_stest3, 0x00);          /* Restart scsi clock           */
 7450 }
 7451 
 7452 /*
 7453  *      calculate NCR SCSI clock frequency (in KHz)
 7454  */
 7455 static unsigned
 7456 ncrgetfreq (ncb_p np, int gen)
 7457 {
 7458         int ms = 0;
 7459         /*
 7460          * Measure GEN timer delay in order 
 7461          * to calculate SCSI clock frequency
 7462          *
 7463          * This code will never execute too
 7464          * many loop iterations (if DELAY is 
 7465          * reasonably correct). It could get
 7466          * too low a delay (too high a freq.)
 7467          * if the CPU is slow executing the 
 7468          * loop for some reason (an NMI, for
 7469          * example). For this reason we will
 7470          * if multiple measurements are to be 
 7471          * performed trust the higher delay 
 7472          * (lower frequency returned).
 7473          */
 7474         OUTB (nc_stest1, 0);    /* make sure clock doubler is OFF           */
 7475         OUTW (nc_sien , 0);     /* mask all scsi interrupts                 */
 7476         (void) INW (nc_sist);   /* clear pending scsi interrupt             */
 7477         OUTB (nc_dien , 0);     /* mask all dma interrupts                  */
 7478         (void) INW (nc_sist);   /* another one, just to be sure :)          */
 7479         OUTB (nc_scntl3, 4);    /* set pre-scaler to divide by 3            */
 7480         OUTB (nc_stime1, 0);    /* disable general purpose timer            */
 7481         OUTB (nc_stime1, gen);  /* set to nominal delay of (1<<gen) * 125us */
 7482         while (!(INW(nc_sist) & GEN) && ms++ < 1000)
 7483                 DELAY(1000);    /* count ms                                 */
 7484         OUTB (nc_stime1, 0);    /* disable general purpose timer            */
 7485         OUTB (nc_scntl3, 0);
 7486         /*
 7487          * Set prescaler to divide by whatever "" means.
 7488          * "" ought to choose divide by 2, but appears
 7489          * to set divide by 3.5 mode in my 53c810 ...
 7490          */
 7491         OUTB (nc_scntl3, 0);
 7492 
 7493         if (bootverbose >= 2)
 7494                 printf ("\tDelay (GEN=%d): %lu msec\n", gen, ms);
 7495         /*
 7496          * adjust for prescaler, and convert into KHz 
 7497          */
 7498         return ms ? ((1 << gen) * 4440) / ms : 0;
 7499 }
 7500 
 7501 static void ncr_getclock (ncb_p np, u_char multiplier)
 7502 {
 7503         unsigned char scntl3;
 7504         unsigned char stest1;
 7505         scntl3 = INB(nc_scntl3);
 7506         stest1 = INB(nc_stest1);
 7507           
 7508         np->multiplier = 1;
 7509         /* always false, except for 875 with clock doubler selected */
 7510         if ((stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
 7511                 np->multiplier  = multiplier;
 7512                 np->clock_khz   = 40000 * multiplier;
 7513         } else {
 7514                 if ((scntl3 & 7) == 0) {
 7515                         unsigned f1, f2;
 7516                         /* throw away first result */
 7517                         (void) ncrgetfreq (np, 11);
 7518                         f1 = ncrgetfreq (np, 11);
 7519                         f2 = ncrgetfreq (np, 11);
 7520 
 7521                         if (bootverbose >= 2)
 7522                           printf ("\tNCR clock is %luKHz, %luKHz\n", f1, f2);
 7523                         if (f1 > f2) f1 = f2;   /* trust lower result   */
 7524                         if (f1 > 45000) {
 7525                                 scntl3 = 5;     /* >45Mhz: assume 80MHz */
 7526                         } else {
 7527                                 scntl3 = 3;     /* <45Mhz: assume 40MHz */
 7528                         }
 7529                 }
 7530                 else if ((scntl3 & 7) == 5)
 7531                         np->clock_khz = 80000;  /* Probably a 875 rev. 1 ? */
 7532         }
 7533 }
 7534 
 7535 #endif /* KERNEL */

Cache object: f16edaa57b8edbf78163738adf58e05a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.