[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

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

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /**************************************************************************
  2 **
  3 **
  4 **  Device driver for the   NCR 53C8XX   PCI-SCSI-Controller Family.
  5 **
  6 **-------------------------------------------------------------------------
  7 **
  8 **  Written for 386bsd and FreeBSD by
  9 **      Wolfgang Stanglmeier    <wolf@cologne.de>
 10 **      Stefan Esser            <se@mi.Uni-Koeln.de>
 11 **
 12 **-------------------------------------------------------------------------
 13 */
 14 /*-
 15 ** Copyright (c) 1994 Wolfgang Stanglmeier.  All rights reserved.
 16 **
 17 ** Redistribution and use in source and binary forms, with or without
 18 ** modification, are permitted provided that the following conditions
 19 ** are met:
 20 ** 1. Redistributions of source code must retain the above copyright
 21 **    notice, this list of conditions and the following disclaimer.
 22 ** 2. Redistributions in binary form must reproduce the above copyright
 23 **    notice, this list of conditions and the following disclaimer in the
 24 **    documentation and/or other materials provided with the distribution.
 25 ** 3. The name of the author may not be used to endorse or promote products
 26 **    derived from this software without specific prior written permission.
 27 **
 28 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 29 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 30 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 31 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 32 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 33 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 34 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 35 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 36 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 37 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 38 **
 39 ***************************************************************************
 40 */
 41 
 42 #include <sys/cdefs.h>
 43 __FBSDID("$FreeBSD: src/sys/pci/ncr.c,v 1.197 2007/06/17 05:55:53 scottl Exp $");
 44 
 45 
 46 #define NCR_DATE "pl30 98/1/1"
 47 
 48 #define NCR_VERSION     (2)
 49 #define MAX_UNITS       (16)
 50 
 51 #define NCR_GETCC_WITHMSG
 52 
 53 #if defined (__FreeBSD__) && defined(_KERNEL)
 54 #include "opt_ncr.h"
 55 #endif
 56 
 57 /*==========================================================
 58 **
 59 **      Configuration and Debugging
 60 **
 61 **      May be overwritten in <arch/conf/xxxx>
 62 **
 63 **==========================================================
 64 */
 65 
 66 /*
 67 **    SCSI address of this device.
 68 **    The boot routines should have set it.
 69 **    If not, use this.
 70 */
 71 
 72 #ifndef SCSI_NCR_MYADDR
 73 #define SCSI_NCR_MYADDR      (7)
 74 #endif /* SCSI_NCR_MYADDR */
 75 
 76 /*
 77 **    The default synchronous period factor
 78 **    (0=asynchronous)
 79 **    If maximum synchronous frequency is defined, use it instead.
 80 */
 81 
 82 #ifndef SCSI_NCR_MAX_SYNC
 83 
 84 #ifndef SCSI_NCR_DFLT_SYNC
 85 #define SCSI_NCR_DFLT_SYNC   (12)
 86 #endif /* SCSI_NCR_DFLT_SYNC */
 87 
 88 #else
 89 
 90 #if     SCSI_NCR_MAX_SYNC == 0
 91 #define SCSI_NCR_DFLT_SYNC 0
 92 #else
 93 #define SCSI_NCR_DFLT_SYNC (250000 / SCSI_NCR_MAX_SYNC)
 94 #endif
 95 
 96 #endif
 97 
 98 /*
 99 **    The minimal asynchronous pre-scaler period (ns)
100 **    Shall be 40.
101 */
102 
103 #ifndef SCSI_NCR_MIN_ASYNC
104 #define SCSI_NCR_MIN_ASYNC   (40)
105 #endif /* SCSI_NCR_MIN_ASYNC */
106 
107 /*
108 **    The maximal bus with (in log2 byte)
109 **    (0=8 bit, 1=16 bit)
110 */
111 
112 #ifndef SCSI_NCR_MAX_WIDE
113 #define SCSI_NCR_MAX_WIDE   (1)
114 #endif /* SCSI_NCR_MAX_WIDE */
115 
116 /*==========================================================
117 **
118 **      Configuration and Debugging
119 **
120 **==========================================================
121 */
122 
123 /*
124 **    Number of targets supported by the driver.
125 **    n permits target numbers 0..n-1.
126 **    Default is 7, meaning targets #0..#6.
127 **    #7 .. is myself.
128 */
129 
130 #define MAX_TARGET  (16)
131 
132 /*
133 **    Number of logic units supported by the driver.
134 **    n enables logic unit numbers 0..n-1.
135 **    The common SCSI devices require only
136 **    one lun, so take 1 as the default.
137 */
138 
139 #ifndef MAX_LUN
140 #define MAX_LUN     (8)
141 #endif  /* MAX_LUN */
142 
143 /*
144 **    The maximum number of jobs scheduled for starting.
145 **    There should be one slot per target, and one slot
146 **    for each tag of each target in use.
147 */
148 
149 #define MAX_START   (256)
150 
151 /*
152 **    The maximum number of segments a transfer is split into.
153 */
154 
155 #define MAX_SCATTER (33)
156 
157 /*
158 **    The maximum transfer length (should be >= 64k).
159 **    MUST NOT be greater than (MAX_SCATTER-1) * PAGE_SIZE.
160 */
161 
162 #define MAX_SIZE  ((MAX_SCATTER-1) * (long) PAGE_SIZE)
163 
164 /*
165 **      other
166 */
167 
168 #define NCR_SNOOP_TIMEOUT (1000000)
169 
170 /*==========================================================
171 **
172 **      Include files
173 **
174 **==========================================================
175 */
176 
177 #include <sys/param.h>
178 #include <sys/time.h>
179 
180 #ifdef _KERNEL
181 #include <sys/systm.h>
182 #include <sys/malloc.h>
183 #include <sys/kernel.h>
184 #include <sys/module.h>
185 #include <sys/sysctl.h>
186 #include <sys/lock.h>
187 #include <sys/mutex.h>
188 #include <sys/bus.h>
189 #include <machine/md_var.h>
190 #include <machine/bus.h>
191 #include <machine/resource.h>
192 #include <sys/rman.h>
193 #include <vm/vm.h>
194 #include <vm/pmap.h>
195 #include <vm/vm_extern.h>
196 #endif
197 
198 #include <dev/pci/pcivar.h>
199 #include <dev/pci/pcireg.h>
200 #include <pci/ncrreg.h>
201 
202 #include <cam/cam.h>
203 #include <cam/cam_ccb.h>
204 #include <cam/cam_sim.h>
205 #include <cam/cam_xpt_sim.h>
206 #include <cam/cam_debug.h>
207 
208 #include <cam/scsi/scsi_all.h>
209 #include <cam/scsi/scsi_message.h>
210 
211 /*==========================================================
212 **
213 **      Debugging tags
214 **
215 **==========================================================
216 */
217 
218 #define DEBUG_ALLOC    (0x0001)
219 #define DEBUG_PHASE    (0x0002)
220 #define DEBUG_POLL     (0x0004)
221 #define DEBUG_QUEUE    (0x0008)
222 #define DEBUG_RESULT   (0x0010)
223 #define DEBUG_SCATTER  (0x0020)
224 #define DEBUG_SCRIPT   (0x0040)
225 #define DEBUG_TINY     (0x0080)
226 #define DEBUG_TIMING   (0x0100)
227 #define DEBUG_NEGO     (0x0200)
228 #define DEBUG_TAGS     (0x0400)
229 #define DEBUG_FREEZE   (0x0800)
230 #define DEBUG_RESTART  (0x1000)
231 
232 /*
233 **    Enable/Disable debug messages.
234 **    Can be changed at runtime too.
235 */
236 #ifdef SCSI_NCR_DEBUG
237         #define DEBUG_FLAGS ncr_debug
238 #else /* SCSI_NCR_DEBUG */
239         #define SCSI_NCR_DEBUG  0
240         #define DEBUG_FLAGS     0
241 #endif /* SCSI_NCR_DEBUG */
242 
243 
244 
245 /*==========================================================
246 **
247 **      assert ()
248 **
249 **==========================================================
250 **
251 **      modified copy from 386bsd:/usr/include/sys/assert.h
252 **
253 **----------------------------------------------------------
254 */
255 
256 #ifdef DIAGNOSTIC
257 #define assert(expression) {                                    \
258         KASSERT(expression, ("%s", #expression));               \
259 }
260 #else
261 #define assert(expression) {                                    \
262         if (!(expression)) {                                    \
263                 (void)printf("assertion \"%s\" failed: "        \
264                              "file \"%s\", line %d\n",          \
265                              #expression, __FILE__, __LINE__);  \
266         }                                                       \
267 }
268 #endif
269 
270 /*==========================================================
271 **
272 **      Access to the controller chip.
273 **
274 **==========================================================
275 */
276 
277 #define INB(r) bus_space_read_1(np->bst, np->bsh, offsetof(struct ncr_reg, r))
278 #define INW(r) bus_space_read_2(np->bst, np->bsh, offsetof(struct ncr_reg, r))
279 #define INL(r) bus_space_read_4(np->bst, np->bsh, offsetof(struct ncr_reg, r))
280 
281 #define OUTB(r, val) bus_space_write_1(np->bst, np->bsh, \
282                                        offsetof(struct ncr_reg, r), val)
283 #define OUTW(r, val) bus_space_write_2(np->bst, np->bsh, \
284                                        offsetof(struct ncr_reg, r), val)
285 #define OUTL(r, val) bus_space_write_4(np->bst, np->bsh, \
286                                        offsetof(struct ncr_reg, r), val)
287 #define OUTL_OFF(o, val) bus_space_write_4(np->bst, np->bsh, o, val)
288 
289 #define INB_OFF(o) bus_space_read_1(np->bst, np->bsh, o)
290 #define INW_OFF(o) bus_space_read_2(np->bst, np->bsh, o)
291 #define INL_OFF(o) bus_space_read_4(np->bst, np->bsh, o)
292 
293 #define READSCRIPT_OFF(base, off)                                       \
294     (base ? *((volatile u_int32_t *)((volatile char *)base + (off))) :  \
295     bus_space_read_4(np->bst2, np->bsh2, off))
296 
297 #define WRITESCRIPT_OFF(base, off, val)                                 \
298     do {                                                                \
299         if (base)                                                       \
300                 *((volatile u_int32_t *)                                \
301                         ((volatile char *)base + (off))) = (val);       \
302         else                                                            \
303                 bus_space_write_4(np->bst2, np->bsh2, off, val);        \
304     } while (0)
305 
306 #define READSCRIPT(r) \
307     READSCRIPT_OFF(np->script, offsetof(struct script, r))
308 
309 #define WRITESCRIPT(r, val) \
310     WRITESCRIPT_OFF(np->script, offsetof(struct script, r), val)
311 
312 /*
313 **      Set bit field ON, OFF 
314 */
315 
316 #define OUTONB(r, m)    OUTB(r, INB(r) | (m))
317 #define OUTOFFB(r, m)   OUTB(r, INB(r) & ~(m))
318 #define OUTONW(r, m)    OUTW(r, INW(r) | (m))
319 #define OUTOFFW(r, m)   OUTW(r, INW(r) & ~(m))
320 #define OUTONL(r, m)    OUTL(r, INL(r) | (m))
321 #define OUTOFFL(r, m)   OUTL(r, INL(r) & ~(m))
322 
323 /*==========================================================
324 **
325 **      Command control block states.
326 **
327 **==========================================================
328 */
329 
330 #define HS_IDLE         (0)
331 #define HS_BUSY         (1)
332 #define HS_NEGOTIATE    (2)     /* sync/wide data transfer*/
333 #define HS_DISCONNECT   (3)     /* Disconnected by target */
334 
335 #define HS_COMPLETE     (4)
336 #define HS_SEL_TIMEOUT  (5)     /* Selection timeout      */
337 #define HS_RESET        (6)     /* SCSI reset        */
338 #define HS_ABORTED      (7)     /* Transfer aborted       */
339 #define HS_TIMEOUT      (8)     /* Software timeout       */
340 #define HS_FAIL         (9)     /* SCSI or PCI bus errors */
341 #define HS_UNEXPECTED   (10)    /* Unexpected disconnect  */
342 #define HS_STALL        (11)    /* QUEUE FULL or BUSY     */
343 
344 #define HS_DONEMASK     (0xfc)
345 
346 /*==========================================================
347 **
348 **      Software Interrupt Codes
349 **
350 **==========================================================
351 */
352 
353 #define SIR_SENSE_RESTART       (1)
354 #define SIR_SENSE_FAILED        (2)
355 #define SIR_STALL_RESTART       (3)
356 #define SIR_STALL_QUEUE         (4)
357 #define SIR_NEGO_SYNC           (5)
358 #define SIR_NEGO_WIDE           (6)
359 #define SIR_NEGO_FAILED         (7)
360 #define SIR_NEGO_PROTO          (8)
361 #define SIR_REJECT_RECEIVED     (9)
362 #define SIR_REJECT_SENT         (10)
363 #define SIR_IGN_RESIDUE         (11)
364 #define SIR_MISSING_SAVE        (12)
365 #define SIR_MAX                 (12)
366 
367 /*==========================================================
368 **
369 **      Extended error codes.
370 **      xerr_status field of struct nccb.
371 **
372 **==========================================================
373 */
374 
375 #define XE_OK           (0)
376 #define XE_EXTRA_DATA   (1)     /* unexpected data phase */
377 #define XE_BAD_PHASE    (2)     /* illegal phase (4/5)   */
378 
379 /*==========================================================
380 **
381 **      Negotiation status.
382 **      nego_status field       of struct nccb.
383 **
384 **==========================================================
385 */
386 
387 #define NS_SYNC         (1)
388 #define NS_WIDE         (2)
389 
390 /*==========================================================
391 **
392 **      XXX These are no longer used.  Remove once the
393 **          script is updated.
394 **      "Special features" of targets.
395 **      quirks field of struct tcb.
396 **      actualquirks field of struct nccb.
397 **
398 **==========================================================
399 */
400 
401 #define QUIRK_AUTOSAVE  (0x01)
402 #define QUIRK_NOMSG     (0x02)
403 #define QUIRK_NOSYNC    (0x10)
404 #define QUIRK_NOWIDE16  (0x20)
405 #define QUIRK_NOTAGS    (0x40)
406 #define QUIRK_UPDATE    (0x80)
407 
408 /*==========================================================
409 **
410 **      Misc.
411 **
412 **==========================================================
413 */
414 
415 #define CCB_MAGIC       (0xf2691ad2)
416 #define MAX_TAGS        (32)            /* hard limit */
417 
418 /*==========================================================
419 **
420 **      OS dependencies.
421 **
422 **==========================================================
423 */
424 
425 #define PRINT_ADDR(ccb) xpt_print_path((ccb)->ccb_h.path)
426 
427 /*==========================================================
428 **
429 **      Declaration of structs.
430 **
431 **==========================================================
432 */
433 
434 struct tcb;
435 struct lcb;
436 struct nccb;
437 struct ncb;
438 struct script;
439 
440 typedef struct ncb * ncb_p;
441 typedef struct tcb * tcb_p;
442 typedef struct lcb * lcb_p;
443 typedef struct nccb * nccb_p;
444 
445 struct link {
446         ncrcmd  l_cmd;
447         ncrcmd  l_paddr;
448 };
449 
450 struct  usrcmd {
451         u_long  target;
452         u_long  lun;
453         u_long  data;
454         u_long  cmd;
455 };
456 
457 #define UC_SETSYNC      10
458 #define UC_SETTAGS      11
459 #define UC_SETDEBUG     12
460 #define UC_SETORDER     13
461 #define UC_SETWIDE      14
462 #define UC_SETFLAG      15
463 
464 #define UF_TRACE        (0x01)
465 
466 /*---------------------------------------
467 **
468 **      Timestamps for profiling
469 **
470 **---------------------------------------
471 */
472 
473 /* Type of the kernel variable `ticks'.  XXX should be declared with the var. */
474 typedef int ticks_t;
475 
476 struct tstamp {
477         ticks_t start;
478         ticks_t end;
479         ticks_t select;
480         ticks_t command;
481         ticks_t data;
482         ticks_t status;
483         ticks_t disconnect;
484 };
485 
486 /*
487 **      profiling data (per device)
488 */
489 
490 struct profile {
491         u_long  num_trans;
492         u_long  num_bytes;
493         u_long  num_disc;
494         u_long  num_break;
495         u_long  num_int;
496         u_long  num_fly;
497         u_long  ms_setup;
498         u_long  ms_data;
499         u_long  ms_disc;
500         u_long  ms_post;
501 };
502 
503 /*==========================================================
504 **
505 **      Declaration of structs:         target control block
506 **
507 **==========================================================
508 */
509 
510 #define NCR_TRANS_CUR           0x01    /* Modify current neogtiation status */
511 #define NCR_TRANS_ACTIVE        0x03    /* Assume this is the active target */
512 #define NCR_TRANS_GOAL          0x04    /* Modify negotiation goal */
513 #define NCR_TRANS_USER          0x08    /* Modify user negotiation settings */
514 
515 struct ncr_transinfo {
516         u_int8_t width;
517         u_int8_t period;
518         u_int8_t offset;
519 };
520 
521 struct ncr_target_tinfo {
522         /* Hardware version of our sync settings */
523         u_int8_t disc_tag;
524 #define         NCR_CUR_DISCENB 0x01
525 #define         NCR_CUR_TAGENB  0x02
526 #define         NCR_USR_DISCENB 0x04
527 #define         NCR_USR_TAGENB  0x08
528         u_int8_t sval;
529         struct   ncr_transinfo current;
530         struct   ncr_transinfo goal;
531         struct   ncr_transinfo user;
532         /* Hardware version of our wide settings */
533         u_int8_t wval;
534 };
535 
536 struct tcb {
537         /*
538         **      during reselection the ncr jumps to this point
539         **      with SFBR set to the encoded target number
540         **      with bit 7 set.
541         **      if it's not this target, jump to the next.
542         **
543         **      JUMP  IF (SFBR != #target#)
544         **      @(next tcb)
545         */
546 
547         struct link   jump_tcb;
548 
549         /*
550         **      load the actual values for the sxfer and the scntl3
551         **      register (sync/wide mode).
552         **
553         **      SCR_COPY (1);
554         **      @(sval field of this tcb)
555         **      @(sxfer register)
556         **      SCR_COPY (1);
557         **      @(wval field of this tcb)
558         **      @(scntl3 register)
559         */
560 
561         ncrcmd  getscr[6];
562 
563         /*
564         **      if next message is "identify"
565         **      then load the message to SFBR,
566         **      else load 0 to SFBR.
567         **
568         **      CALL
569         **      <RESEL_LUN>
570         */
571 
572         struct link   call_lun;
573 
574         /*
575         **      now look for the right lun.
576         **
577         **      JUMP
578         **      @(first nccb of this lun)
579         */
580 
581         struct link   jump_lcb;
582 
583         /*
584         **      pointer to interrupted getcc nccb
585         */
586 
587         nccb_p   hold_cp;
588 
589         /*
590         **      pointer to nccb used for negotiating.
591         **      Avoid to start a nego for all queued commands 
592         **      when tagged command queuing is enabled.
593         */
594 
595         nccb_p   nego_cp;
596 
597         /*
598         **      statistical data
599         */
600 
601         u_long  transfers;
602         u_long  bytes;
603 
604         /*
605         **      user settable limits for sync transfer
606         **      and tagged commands.
607         */
608 
609         struct   ncr_target_tinfo tinfo;
610 
611         /*
612         **      the lcb's of this tcb
613         */
614 
615         lcb_p   lp[MAX_LUN];
616 };
617 
618 /*==========================================================
619 **
620 **      Declaration of structs:         lun control block
621 **
622 **==========================================================
623 */
624 
625 struct lcb {
626         /*
627         **      during reselection the ncr jumps to this point
628         **      with SFBR set to the "Identify" message.
629         **      if it's not this lun, jump to the next.
630         **
631         **      JUMP  IF (SFBR != #lun#)
632         **      @(next lcb of this target)
633         */
634 
635         struct link     jump_lcb;
636 
637         /*
638         **      if next message is "simple tag",
639         **      then load the tag to SFBR,
640         **      else load 0 to SFBR.
641         **
642         **      CALL
643         **      <RESEL_TAG>
644         */
645 
646         struct link     call_tag;
647 
648         /*
649         **      now look for the right nccb.
650         **
651         **      JUMP
652         **      @(first nccb of this lun)
653         */
654 
655         struct link     jump_nccb;
656 
657         /*
658         **      start of the nccb chain
659         */
660 
661         nccb_p  next_nccb;
662 
663         /*
664         **      Control of tagged queueing
665         */
666 
667         u_char          reqnccbs;
668         u_char          reqlink;
669         u_char          actlink;
670         u_char          usetags;
671         u_char          lasttag;
672 };
673 
674 /*==========================================================
675 **
676 **      Declaration of structs:     COMMAND control block
677 **
678 **==========================================================
679 **
680 **      This substructure is copied from the nccb to a
681 **      global address after selection (or reselection)
682 **      and copied back before disconnect.
683 **
684 **      These fields are accessible to the script processor.
685 **
686 **----------------------------------------------------------
687 */
688 
689 struct head {
690         /*
691         **      Execution of a nccb starts at this point.
692         **      It's a jump to the "SELECT" label
693         **      of the script.
694         **
695         **      After successful selection the script
696         **      processor overwrites it with a jump to
697         **      the IDLE label of the script.
698         */
699 
700         struct link     launch;
701 
702         /*
703         **      Saved data pointer.
704         **      Points to the position in the script
705         **      responsible for the actual transfer
706         **      of data.
707         **      It's written after reception of a
708         **      "SAVE_DATA_POINTER" message.
709         **      The goalpointer points after
710         **      the last transfer command.
711         */
712 
713         u_int32_t       savep;
714         u_int32_t       lastp;
715         u_int32_t       goalp;
716 
717         /*
718         **      The virtual address of the nccb
719         **      containing this header.
720         */
721 
722         nccb_p  cp;
723 
724         /*
725         **      space for some timestamps to gather
726         **      profiling data about devices and this driver.
727         */
728 
729         struct tstamp   stamp;
730 
731         /*
732         **      status fields.
733         */
734 
735         u_char          status[8];
736 };
737 
738 /*
739 **      The status bytes are used by the host and the script processor.
740 **
741 **      The first four byte are copied to the scratchb register
742 **      (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
743 **      and copied back just after disconnecting.
744 **      Inside the script the XX_REG are used.
745 **
746 **      The last four bytes are used inside the script by "COPY" commands.
747 **      Because source and destination must have the same alignment
748 **      in a longword, the fields HAVE to be at the choosen offsets.
749 **              xerr_st (4)     0       (0x34)  scratcha
750 **              sync_st (5)     1       (0x05)  sxfer
751 **              wide_st (7)     3       (0x03)  scntl3
752 */
753 
754 /*
755 **      First four bytes (script)
756 */
757 #define  QU_REG scr0
758 #define  HS_REG scr1
759 #define  HS_PRT nc_scr1
760 #define  SS_REG scr2
761 #define  PS_REG scr3
762 
763 /*
764 **      First four bytes (host)
765 */
766 #define  actualquirks  phys.header.status[0]
767 #define  host_status   phys.header.status[1]
768 #define  s_status      phys.header.status[2]
769 #define  parity_status phys.header.status[3]
770 
771 /*
772 **      Last four bytes (script)
773 */
774 #define  xerr_st       header.status[4] /* MUST be ==0 mod 4 */
775 #define  sync_st       header.status[5] /* MUST be ==1 mod 4 */
776 #define  nego_st       header.status[6]
777 #define  wide_st       header.status[7] /* MUST be ==3 mod 4 */
778 
779 /*
780 **      Last four bytes (host)
781 */
782 #define  xerr_status   phys.xerr_st
783 #define  sync_status   phys.sync_st
784 #define  nego_status   phys.nego_st
785 #define  wide_status   phys.wide_st
786 
787 /*==========================================================
788 **
789 **      Declaration of structs:     Data structure block
790 **
791 **==========================================================
792 **
793 **      During execution of a nccb by the script processor,
794 **      the DSA (data structure address) register points
795 **      to this substructure of the nccb.
796 **      This substructure contains the header with
797 **      the script-processor-changable data and
798 **      data blocks for the indirect move commands.
799 **
800 **----------------------------------------------------------
801 */
802 
803 struct dsb {
804 
805         /*
806         **      Header.
807         **      Has to be the first entry,
808         **      because it's jumped to by the
809         **      script processor
810         */
811 
812         struct head     header;
813 
814         /*
815         **      Table data for Script
816         */
817 
818         struct scr_tblsel  select;
819         struct scr_tblmove smsg  ;
820         struct scr_tblmove smsg2 ;
821         struct scr_tblmove cmd   ;
822         struct scr_tblmove scmd  ;
823         struct scr_tblmove sense ;
824         struct scr_tblmove data [MAX_SCATTER];
825 };
826 
827 /*==========================================================
828 **
829 **      Declaration of structs:     Command control block.
830 **
831 **==========================================================
832 **
833 **      During execution of a nccb by the script processor,
834 **      the DSA (data structure address) register points
835 **      to this substructure of the nccb.
836 **      This substructure contains the header with
837 **      the script-processor-changable data and then
838 **      data blocks for the indirect move commands.
839 **
840 **----------------------------------------------------------
841 */
842 
843 
844 struct nccb {
845         /*
846         **      This filler ensures that the global header is 
847         **      cache line size aligned.
848         */
849         ncrcmd  filler[4];
850 
851         /*
852         **      during reselection the ncr jumps to this point.
853         **      If a "SIMPLE_TAG" message was received,
854         **      then SFBR is set to the tag.
855         **      else SFBR is set to 0
856         **      If looking for another tag, jump to the next nccb.
857         **
858         **      JUMP  IF (SFBR != #TAG#)
859         **      @(next nccb of this lun)
860         */
861 
862         struct link             jump_nccb;
863 
864         /*
865         **      After execution of this call, the return address
866         **      (in  the TEMP register) points to the following
867         **      data structure block.
868         **      So copy it to the DSA register, and start
869         **      processing of this data structure.
870         **
871         **      CALL
872         **      <RESEL_TMP>
873         */
874 
875         struct link             call_tmp;
876 
877         /*
878         **      This is the data structure which is
879         **      to be executed by the script processor.
880         */
881 
882         struct dsb              phys;
883 
884         /*
885         **      If a data transfer phase is terminated too early
886         **      (after reception of a message (i.e. DISCONNECT)),
887         **      we have to prepare a mini script to transfer
888         **      the rest of the data.
889         */
890 
891         ncrcmd                  patch[8];
892 
893         /*
894         **      The general SCSI driver provides a
895         **      pointer to a control block.
896         */
897 
898         union   ccb *ccb;
899 
900         /*
901         **      We prepare a message to be sent after selection,
902         **      and a second one to be sent after getcc selection.
903         **      Contents are IDENTIFY and SIMPLE_TAG.
904         **      While negotiating sync or wide transfer,
905         **      a SDTM or WDTM message is appended.
906         */
907 
908         u_char                  scsi_smsg [8];
909         u_char                  scsi_smsg2[8];
910 
911         /*
912         **      Lock this nccb.
913         **      Flag is used while looking for a free nccb.
914         */
915 
916         u_long          magic;
917 
918         /*
919         **      Physical address of this instance of nccb
920         */
921 
922         u_long          p_nccb;
923 
924         /*
925         **      Completion time out for this job.
926         **      It's set to time of start + allowed number of seconds.
927         */
928 
929         time_t          tlimit;
930 
931         /*
932         **      All nccbs of one hostadapter are chained.
933         */
934 
935         nccb_p          link_nccb;
936 
937         /*
938         **      All nccbs of one target/lun are chained.
939         */
940 
941         nccb_p          next_nccb;
942 
943         /*
944         **      Sense command
945         */
946 
947         u_char          sensecmd[6];
948 
949         /*
950         **      Tag for this transfer.
951         **      It's patched into jump_nccb.
952         **      If it's not zero, a SIMPLE_TAG
953         **      message is included in smsg.
954         */
955 
956         u_char                  tag;
957 };
958 
959 #define CCB_PHYS(cp,lbl)        (cp->p_nccb + offsetof(struct nccb, lbl))
960 
961 /*==========================================================
962 **
963 **      Declaration of structs:     NCR device descriptor
964 **
965 **==========================================================
966 */
967 
968 struct ncb {
969         /*
970         **      The global header.
971         **      Accessible to both the host and the
972         **      script-processor.
973         **      We assume it is cache line size aligned.
974         */
975         struct head     header;
976 
977         int     unit;
978 
979         /*-----------------------------------------------
980         **      Scripts ..
981         **-----------------------------------------------
982         **
983         **      During reselection the ncr jumps to this point.
984         **      The SFBR register is loaded with the encoded target id.
985         **
986         **      Jump to the first target.
987         **
988         **      JUMP
989         **      @(next tcb)
990         */
991         struct link     jump_tcb;
992 
993         /*-----------------------------------------------
994         **      Configuration ..
995         **-----------------------------------------------
996         **
997         **      virtual and physical addresses
998         **      of the 53c810 chip.
999         */
1000         int             reg_rid;
1001         struct resource *reg_res;
1002         bus_space_tag_t bst;
1003         bus_space_handle_t bsh;
1004 
1005         int             sram_rid;
1006         struct resource *sram_res;
1007         bus_space_tag_t bst2;
1008         bus_space_handle_t bsh2;
1009 
1010         struct resource *irq_res;
1011         void            *irq_handle;
1012 
1013         /*
1014         **      Scripts instance virtual address.
1015         */
1016         struct script   *script;
1017         struct scripth  *scripth;
1018 
1019         /*
1020         **      Scripts instance physical address.
1021         */
1022         u_long          p_script;
1023         u_long          p_scripth;
1024 
1025         /*
1026         **      The SCSI address of the host adapter.
1027         */
1028         u_char          myaddr;
1029 
1030         /*
1031         **      timing parameters
1032         */
1033         u_char          minsync;        /* Minimum sync period factor   */
1034         u_char          maxsync;        /* Maximum sync period factor   */
1035         u_char          maxoffs;        /* Max scsi offset              */
1036         u_char          clock_divn;     /* Number of clock divisors     */
1037         u_long          clock_khz;      /* SCSI clock frequency in KHz  */
1038         u_long          features;       /* Chip features map            */
1039         u_char          multiplier;     /* Clock multiplier (1,2,4)     */
1040 
1041         u_char          maxburst;       /* log base 2 of dwords burst   */
1042 
1043         /*
1044         **      BIOS supplied PCI bus options
1045         */
1046         u_char          rv_scntl3;
1047         u_char          rv_dcntl;
1048         u_char          rv_dmode;
1049         u_char          rv_ctest3;
1050         u_char          rv_ctest4;
1051         u_char          rv_ctest5;
1052         u_char          rv_gpcntl;
1053         u_char          rv_stest2;
1054 
1055         /*-----------------------------------------------
1056         **      CAM SIM information for this instance
1057         **-----------------------------------------------
1058         */
1059 
1060         struct          cam_sim  *sim;
1061         struct          cam_path *path;
1062 
1063         /*-----------------------------------------------
1064         **      Job control
1065         **-----------------------------------------------
1066         **
1067         **      Commands from user
1068         */
1069         struct usrcmd   user;
1070 
1071         /*
1072         **      Target data
1073         */
1074         struct tcb      target[MAX_TARGET];
1075 
1076         /*
1077         **      Start queue.
1078         */
1079         u_int32_t       squeue [MAX_START];
1080         u_short         squeueput;
1081 
1082         /*
1083         **      Timeout handler
1084         */
1085         time_t          heartbeat;
1086         u_short         ticks;
1087         u_short         latetime;
1088         time_t          lasttime;
1089         struct          callout_handle timeout_ch;
1090 
1091         /*-----------------------------------------------
1092         **      Debug and profiling
1093         **-----------------------------------------------
1094         **
1095         **      register dump
1096         */
1097         struct ncr_reg  regdump;
1098         time_t          regtime;
1099 
1100         /*
1101         **      Profiling data
1102         */
1103         struct profile  profile;
1104         u_long          disc_phys;
1105         u_long          disc_ref;
1106 
1107         /*
1108         **      Head of list of all nccbs for this controller.
1109         */
1110         nccb_p          link_nccb;
1111         
1112         /*
1113         **      message buffers.
1114         **      Should be longword aligned,
1115         **      because they're written with a
1116         **      COPY script command.
1117         */
1118         u_char          msgout[8];
1119         u_char          msgin [8];
1120         u_int32_t       lastmsg;
1121 
1122         /*
1123         **      Buffer for STATUS_IN phase.
1124         */
1125         u_char          scratch;
1126 
1127         /*
1128         **      controller chip dependent maximal transfer width.
1129         */
1130         u_char          maxwide;
1131 
1132 #ifdef NCR_IOMAPPED
1133         /*
1134         **      address of the ncr control registers in io space
1135         */
1136         pci_port_t      port;
1137 #endif
1138 };
1139 
1140 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1141 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1142 
1143 /*==========================================================
1144 **
1145 **
1146 **      Script for NCR-Processor.
1147 **
1148 **      Use ncr_script_fill() to create the variable parts.
1149 **      Use ncr_script_copy_and_bind() to make a copy and
1150 **      bind to physical addresses.
1151 **
1152 **
1153 **==========================================================
1154 **
1155 **      We have to know the offsets of all labels before
1156 **      we reach them (for forward jumps).
1157 **      Therefore we declare a struct here.
1158 **      If you make changes inside the script,
1159 **      DONT FORGET TO CHANGE THE LENGTHS HERE!
1160 **
1161 **----------------------------------------------------------
1162 */
1163 
1164 /*
1165 **      Script fragments which are loaded into the on-board RAM 
1166 **      of 825A, 875 and 895 chips.
1167 */
1168 struct script {
1169         ncrcmd  start           [  7];
1170         ncrcmd  start0          [  2];
1171         ncrcmd  start1          [  3];
1172         ncrcmd  startpos        [  1];
1173         ncrcmd  trysel          [  8];
1174         ncrcmd  skip            [  8];
1175         ncrcmd  skip2           [  3];
1176         ncrcmd  idle            [  2];
1177         ncrcmd  select          [ 18];
1178         ncrcmd  prepare         [  4];
1179         ncrcmd  loadpos         [ 14];
1180         ncrcmd  prepare2        [ 24];
1181         ncrcmd  setmsg          [  5];
1182         ncrcmd  clrack          [  2];
1183         ncrcmd  dispatch        [ 33];
1184         ncrcmd  no_data         [ 17];
1185         ncrcmd  checkatn        [ 10];
1186         ncrcmd  command         [ 15];
1187         ncrcmd  status          [ 27];
1188         ncrcmd  msg_in          [ 26];
1189         ncrcmd  msg_bad         [  6];
1190         ncrcmd  complete        [ 13];
1191         ncrcmd  cleanup         [ 12];
1192         ncrcmd  cleanup0        [  9];
1193         ncrcmd  signal          [ 12];
1194         ncrcmd  save_dp         [  5];
1195         ncrcmd  restore_dp      [  5];
1196         ncrcmd  disconnect      [ 12];
1197         ncrcmd  disconnect0     [  5];
1198         ncrcmd  disconnect1     [ 23];
1199         ncrcmd  msg_out         [  9];
1200         ncrcmd  msg_out_done    [  7];
1201         ncrcmd  badgetcc        [  6];
1202         ncrcmd  reselect        [  8];
1203         ncrcmd  reselect1       [  8];
1204         ncrcmd  reselect2       [  8];
1205         ncrcmd  resel_tmp       [  5];
1206         ncrcmd  resel_lun       [ 18];
1207         ncrcmd  resel_tag       [ 24];
1208         ncrcmd  data_in         [MAX_SCATTER * 4 + 7];
1209         ncrcmd  data_out        [MAX_SCATTER * 4 + 7];
1210 };
1211 
1212 /*
1213 **      Script fragments which stay in main memory for all chips.
1214 */
1215 struct scripth {
1216         ncrcmd  tryloop         [MAX_START*5+2];
1217         ncrcmd  msg_parity      [  6];
1218         ncrcmd  msg_reject      [  8];
1219         ncrcmd  msg_ign_residue [ 32];
1220         ncrcmd  msg_extended    [ 18];
1221         ncrcmd  msg_ext_2       [ 18];
1222         ncrcmd  msg_wdtr        [ 27];
1223         ncrcmd  msg_ext_3       [ 18];
1224         ncrcmd  msg_sdtr        [ 27];
1225         ncrcmd  msg_out_abort   [ 10];
1226         ncrcmd  getcc           [  4];
1227         ncrcmd  getcc1          [  5];
1228 #ifdef NCR_GETCC_WITHMSG
1229         ncrcmd  getcc2          [ 29];
1230 #else
1231         ncrcmd  getcc2          [ 14];
1232 #endif
1233         ncrcmd  getcc3          [  6];
1234         ncrcmd  aborttag        [  4];
1235         ncrcmd  abort           [ 22];
1236         ncrcmd  snooptest       [  9];
1237         ncrcmd  snoopend        [  2];
1238 };
1239 
1240 /*==========================================================
1241 **
1242 **
1243 **      Function headers.
1244 **
1245 **
1246 **==========================================================
1247 */
1248 
1249 #ifdef _KERNEL
1250 static  nccb_p  ncr_alloc_nccb(ncb_p np, u_long target, u_long lun);
1251 static  void    ncr_complete(ncb_p np, nccb_p cp);
1252 static  int     ncr_delta(int * from, int * to);
1253 static  void    ncr_exception(ncb_p np);
1254 static  void    ncr_free_nccb(ncb_p np, nccb_p cp);
1255 static  void    ncr_freeze_devq(ncb_p np, struct cam_path *path);
1256 static  void    ncr_selectclock(ncb_p np, u_char scntl3);
1257 static  void    ncr_getclock(ncb_p np, u_char multiplier);
1258 static  nccb_p  ncr_get_nccb(ncb_p np, u_long t,u_long l);
1259 #if 0
1260 static  u_int32_t ncr_info(int <