Fix UTIME_OMIT handling
[dragonfly.git] / sys / dev / disk / ncr / ncr.c
blob3977339cd9e7f75d0f91a11a2d9070a8cf353e38
1 /**************************************************************************
2 **
3 ** $FreeBSD: src/sys/pci/ncr.c,v 1.155.2.3 2001/03/05 13:09:10 obrien Exp $
4 **
5 ** Device driver for the NCR 53C8XX PCI-SCSI-Controller Family.
6 **
7 **-------------------------------------------------------------------------
8 **
9 ** Written for 386bsd and FreeBSD by
10 ** Wolfgang Stanglmeier <wolf@cologne.de>
11 ** Stefan Esser <se@mi.Uni-Koeln.de>
13 **-------------------------------------------------------------------------
15 ** Copyright (c) 1994 Wolfgang Stanglmeier. All rights reserved.
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.
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.
39 ***************************************************************************
42 #define NCR_DATE "pl30 98/1/1"
44 #define NCR_VERSION (2)
45 #define MAX_UNITS (16)
47 #define NCR_GETCC_WITHMSG
49 #if (defined(__DragonFly__) || defined (__FreeBSD__)) && defined(_KERNEL)
50 #include "opt_ncr.h"
51 #endif
53 /*==========================================================
55 ** Configuration and Debugging
57 ** May be overwritten in <arch/conf/xxxx>
59 **==========================================================
63 ** SCSI address of this device.
64 ** The boot routines should have set it.
65 ** If not, use this.
68 #ifndef SCSI_NCR_MYADDR
69 #define SCSI_NCR_MYADDR (7)
70 #endif /* SCSI_NCR_MYADDR */
73 ** The default synchronous period factor
74 ** (0=asynchronous)
75 ** If maximum synchronous frequency is defined, use it instead.
78 #ifndef SCSI_NCR_MAX_SYNC
80 #ifndef SCSI_NCR_DFLT_SYNC
81 #define SCSI_NCR_DFLT_SYNC (12)
82 #endif /* SCSI_NCR_DFLT_SYNC */
84 #else
86 #if SCSI_NCR_MAX_SYNC == 0
87 #define SCSI_NCR_DFLT_SYNC 0
88 #else
89 #define SCSI_NCR_DFLT_SYNC (250000 / SCSI_NCR_MAX_SYNC)
90 #endif
92 #endif
95 ** The minimal asynchronous pre-scaler period (ns)
96 ** Shall be 40.
99 #ifndef SCSI_NCR_MIN_ASYNC
100 #define SCSI_NCR_MIN_ASYNC (40)
101 #endif /* SCSI_NCR_MIN_ASYNC */
104 ** The maximal bus with (in log2 byte)
105 ** (0=8 bit, 1=16 bit)
108 #ifndef SCSI_NCR_MAX_WIDE
109 #define SCSI_NCR_MAX_WIDE (1)
110 #endif /* SCSI_NCR_MAX_WIDE */
112 /*==========================================================
114 ** Configuration and Debugging
116 **==========================================================
120 ** Number of targets supported by the driver.
121 ** n permits target numbers 0..n-1.
122 ** Default is 7, meaning targets #0..#6.
123 ** #7 .. is myself.
126 #define MAX_TARGET (16)
129 ** Number of logic units supported by the driver.
130 ** n enables logic unit numbers 0..n-1.
131 ** The common SCSI devices require only
132 ** one lun, so take 1 as the default.
135 #ifndef MAX_LUN
136 #define MAX_LUN (8)
137 #endif /* MAX_LUN */
140 ** The maximum number of jobs scheduled for starting.
141 ** There should be one slot per target, and one slot
142 ** for each tag of each target in use.
145 #define MAX_START (256)
148 ** The maximum number of segments a transfer is split into.
151 #define MAX_SCATTER (33)
154 ** The maximum transfer length (should be >= 64k).
155 ** MUST NOT be greater than (MAX_SCATTER-1) * PAGE_SIZE.
158 #define MAX_SIZE ((MAX_SCATTER-1) * (long) PAGE_SIZE)
161 ** other
164 #define NCR_SNOOP_TIMEOUT (1000000)
166 /*==========================================================
168 ** Include files
170 **==========================================================
173 #include <sys/param.h>
174 #include <sys/time.h>
176 #ifdef _KERNEL
177 #include <sys/systm.h>
178 #include <sys/malloc.h>
179 #include <sys/buf.h>
180 #include <sys/kernel.h>
181 #include <sys/sysctl.h>
182 #include <sys/bus.h>
183 #include <sys/thread2.h>
184 #include <machine/clock.h>
185 #include <machine/md_var.h>
186 #include <sys/rman.h>
187 #include <vm/vm.h>
188 #include <vm/pmap.h>
189 #include <vm/vm_extern.h>
190 #endif
192 #include <bus/pci/pcivar.h>
193 #include <bus/pci/pcireg.h>
194 #include "ncrreg.h"
196 #include <bus/cam/cam.h>
197 #include <bus/cam/cam_ccb.h>
198 #include <bus/cam/cam_sim.h>
199 #include <bus/cam/cam_xpt.h>
200 #include <bus/cam/cam_xpt_sim.h>
201 #include <bus/cam/cam_xpt_periph.h>
202 #include <bus/cam/cam_debug.h>
205 #include <bus/cam/scsi/scsi_all.h>
206 #include <bus/cam/scsi/scsi_message.h>
208 /*==========================================================
210 ** Debugging tags
212 **==========================================================
215 #define DEBUG_ALLOC (0x0001)
216 #define DEBUG_PHASE (0x0002)
217 #define DEBUG_POLL (0x0004)
218 #define DEBUG_QUEUE (0x0008)
219 #define DEBUG_RESULT (0x0010)
220 #define DEBUG_SCATTER (0x0020)
221 #define DEBUG_SCRIPT (0x0040)
222 #define DEBUG_TINY (0x0080)
223 #define DEBUG_TIMING (0x0100)
224 #define DEBUG_NEGO (0x0200)
225 #define DEBUG_TAGS (0x0400)
226 #define DEBUG_FREEZE (0x0800)
227 #define DEBUG_RESTART (0x1000)
230 ** Enable/Disable debug messages.
231 ** Can be changed at runtime too.
233 #ifdef SCSI_NCR_DEBUG
234 #define DEBUG_FLAGS ncr_debug
235 #else /* SCSI_NCR_DEBUG */
236 #define SCSI_NCR_DEBUG 0
237 #define DEBUG_FLAGS 0
238 #endif /* SCSI_NCR_DEBUG */
242 /*==========================================================
244 ** assert ()
246 **==========================================================
248 ** modified copy from 386bsd:/usr/include/sys/assert.h
250 **----------------------------------------------------------
253 #ifdef DIAGNOSTIC
254 #define assert(expression) { \
255 if (!(expression)) { \
256 (void)kprintf("assertion \"%s\" failed: " \
257 "file \"%s\", line %d\n", \
258 #expression, __FILE__, __LINE__); \
259 Debugger(""); \
262 #else
263 #define assert(expression) { \
264 if (!(expression)) { \
265 (void)kprintf("assertion \"%s\" failed: " \
266 "file \"%s\", line %d\n", \
267 #expression, __FILE__, __LINE__); \
270 #endif
272 /*==========================================================
274 ** Access to the controller chip.
276 **==========================================================
279 #define INB(r) bus_space_read_1(np->bst, np->bsh, offsetof(struct ncr_reg, r))
280 #define INW(r) bus_space_read_2(np->bst, np->bsh, offsetof(struct ncr_reg, r))
281 #define INL(r) bus_space_read_4(np->bst, np->bsh, offsetof(struct ncr_reg, r))
283 #define OUTB(r, val) bus_space_write_1(np->bst, np->bsh, \
284 offsetof(struct ncr_reg, r), val)
285 #define OUTW(r, val) bus_space_write_2(np->bst, np->bsh, \
286 offsetof(struct ncr_reg, r), val)
287 #define OUTL(r, val) bus_space_write_4(np->bst, np->bsh, \
288 offsetof(struct ncr_reg, r), val)
289 #define OUTL_OFF(o, val) bus_space_write_4(np->bst, np->bsh, o, val)
291 #define INB_OFF(o) bus_space_read_1(np->bst, np->bsh, o)
292 #define INW_OFF(o) bus_space_read_2(np->bst, np->bsh, o)
293 #define INL_OFF(o) bus_space_read_4(np->bst, np->bsh, o)
295 #define READSCRIPT_OFF(base, off) \
296 (base ? *((volatile u_int32_t *)((volatile char *)base + (off))) : \
297 bus_space_read_4(np->bst2, np->bsh2, off))
299 #define WRITESCRIPT_OFF(base, off, val) \
300 do { \
301 if (base) \
302 *((volatile u_int32_t *) \
303 ((volatile char *)base + (off))) = (val); \
304 else \
305 bus_space_write_4(np->bst2, np->bsh2, off, val); \
306 } while (0)
308 #define READSCRIPT(r) \
309 READSCRIPT_OFF(np->script, offsetof(struct script, r))
311 #define WRITESCRIPT(r, val) \
312 WRITESCRIPT_OFF(np->script, offsetof(struct script, r), val)
315 ** Set bit field ON, OFF
318 #define OUTONB(r, m) OUTB(r, INB(r) | (m))
319 #define OUTOFFB(r, m) OUTB(r, INB(r) & ~(m))
320 #define OUTONW(r, m) OUTW(r, INW(r) | (m))
321 #define OUTOFFW(r, m) OUTW(r, INW(r) & ~(m))
322 #define OUTONL(r, m) OUTL(r, INL(r) | (m))
323 #define OUTOFFL(r, m) OUTL(r, INL(r) & ~(m))
325 /*==========================================================
327 ** Command control block states.
329 **==========================================================
332 #define HS_IDLE (0)
333 #define HS_BUSY (1)
334 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
335 #define HS_DISCONNECT (3) /* Disconnected by target */
337 #define HS_COMPLETE (4)
338 #define HS_SEL_TIMEOUT (5) /* Selection timeout */
339 #define HS_RESET (6) /* SCSI reset */
340 #define HS_ABORTED (7) /* Transfer aborted */
341 #define HS_TIMEOUT (8) /* Software timeout */
342 #define HS_FAIL (9) /* SCSI or PCI bus errors */
343 #define HS_UNEXPECTED (10) /* Unexpected disconnect */
344 #define HS_STALL (11) /* QUEUE FULL or BUSY */
346 #define HS_DONEMASK (0xfc)
348 /*==========================================================
350 ** Software Interrupt Codes
352 **==========================================================
355 #define SIR_SENSE_RESTART (1)
356 #define SIR_SENSE_FAILED (2)
357 #define SIR_STALL_RESTART (3)
358 #define SIR_STALL_QUEUE (4)
359 #define SIR_NEGO_SYNC (5)
360 #define SIR_NEGO_WIDE (6)
361 #define SIR_NEGO_FAILED (7)
362 #define SIR_NEGO_PROTO (8)
363 #define SIR_REJECT_RECEIVED (9)
364 #define SIR_REJECT_SENT (10)
365 #define SIR_IGN_RESIDUE (11)
366 #define SIR_MISSING_SAVE (12)
367 #define SIR_MAX (12)
369 /*==========================================================
371 ** Extended error codes.
372 ** xerr_status field of struct nccb.
374 **==========================================================
377 #define XE_OK (0)
378 #define XE_EXTRA_DATA (1) /* unexpected data phase */
379 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
381 /*==========================================================
383 ** Negotiation status.
384 ** nego_status field of struct nccb.
386 **==========================================================
389 #define NS_SYNC (1)
390 #define NS_WIDE (2)
392 /*==========================================================
394 ** XXX These are no longer used. Remove once the
395 ** script is updated.
396 ** "Special features" of targets.
397 ** quirks field of struct tcb.
398 ** actualquirks field of struct nccb.
400 **==========================================================
403 #define QUIRK_AUTOSAVE (0x01)
404 #define QUIRK_NOMSG (0x02)
405 #define QUIRK_NOSYNC (0x10)
406 #define QUIRK_NOWIDE16 (0x20)
407 #define QUIRK_NOTAGS (0x40)
408 #define QUIRK_UPDATE (0x80)
410 /*==========================================================
412 ** Misc.
414 **==========================================================
417 #define CCB_MAGIC (0xf2691ad2)
418 #define MAX_TAGS (32) /* hard limit */
420 /*==========================================================
422 ** OS dependencies.
424 **==========================================================
427 #define PRINT_ADDR(ccb) xpt_print_path((ccb)->ccb_h.path)
429 /*==========================================================
431 ** Declaration of structs.
433 **==========================================================
436 struct tcb;
437 struct lcb;
438 struct nccb;
439 struct ncb;
440 struct script;
442 typedef struct ncb * ncb_p;
443 typedef struct tcb * tcb_p;
444 typedef struct lcb * lcb_p;
445 typedef struct nccb * nccb_p;
447 struct link {
448 ncrcmd l_cmd;
449 ncrcmd l_paddr;
452 struct usrcmd {
453 u_long target;
454 u_long lun;
455 u_long data;
456 u_long cmd;
459 #define UC_SETSYNC 10
460 #define UC_SETTAGS 11
461 #define UC_SETDEBUG 12
462 #define UC_SETORDER 13
463 #define UC_SETWIDE 14
464 #define UC_SETFLAG 15
466 #define UF_TRACE (0x01)
468 /*---------------------------------------
470 ** Timestamps for profiling
472 **---------------------------------------
475 /* Type of the kernel variable `ticks'. XXX should be declared with the var. */
476 typedef int ticks_t;
478 struct tstamp {
479 ticks_t start;
480 ticks_t end;
481 ticks_t select;
482 ticks_t command;
483 ticks_t data;
484 ticks_t status;
485 ticks_t disconnect;
489 ** profiling data (per device)
492 struct profile {
493 u_long num_trans;
494 u_long num_bytes;
495 u_long num_disc;
496 u_long num_break;
497 u_long num_int;
498 u_long num_fly;
499 u_long ms_setup;
500 u_long ms_data;
501 u_long ms_disc;
502 u_long ms_post;
505 /*==========================================================
507 ** Declaration of structs: target control block
509 **==========================================================
512 #define NCR_TRANS_CUR 0x01 /* Modify current neogtiation status */
513 #define NCR_TRANS_ACTIVE 0x03 /* Assume this is the active target */
514 #define NCR_TRANS_GOAL 0x04 /* Modify negotiation goal */
515 #define NCR_TRANS_USER 0x08 /* Modify user negotiation settings */
517 struct ncr_transinfo {
518 u_int8_t width;
519 u_int8_t period;
520 u_int8_t offset;
523 struct ncr_target_tinfo {
524 /* Hardware version of our sync settings */
525 u_int8_t disc_tag;
526 #define NCR_CUR_DISCENB 0x01
527 #define NCR_CUR_TAGENB 0x02
528 #define NCR_USR_DISCENB 0x04
529 #define NCR_USR_TAGENB 0x08
530 u_int8_t sval;
531 struct ncr_transinfo current;
532 struct ncr_transinfo goal;
533 struct ncr_transinfo user;
534 /* Hardware version of our wide settings */
535 u_int8_t wval;
538 struct tcb {
540 ** during reselection the ncr jumps to this point
541 ** with SFBR set to the encoded target number
542 ** with bit 7 set.
543 ** if it's not this target, jump to the next.
545 ** JUMP IF (SFBR != #target#)
546 ** @(next tcb)
549 struct link jump_tcb;
552 ** load the actual values for the sxfer and the scntl3
553 ** register (sync/wide mode).
555 ** SCR_COPY (1);
556 ** @(sval field of this tcb)
557 ** @(sxfer register)
558 ** SCR_COPY (1);
559 ** @(wval field of this tcb)
560 ** @(scntl3 register)
563 ncrcmd getscr[6];
566 ** if next message is "identify"
567 ** then load the message to SFBR,
568 ** else load 0 to SFBR.
570 ** CALL
571 ** <RESEL_LUN>
574 struct link call_lun;
577 ** now look for the right lun.
579 ** JUMP
580 ** @(first nccb of this lun)
583 struct link jump_lcb;
586 ** pointer to interrupted getcc nccb
589 nccb_p hold_cp;
592 ** pointer to nccb used for negotiating.
593 ** Avoid to start a nego for all queued commands
594 ** when tagged command queuing is enabled.
597 nccb_p nego_cp;
600 ** statistical data
603 u_long transfers;
604 u_long bytes;
607 ** user settable limits for sync transfer
608 ** and tagged commands.
611 struct ncr_target_tinfo tinfo;
614 ** the lcb's of this tcb
617 lcb_p lp[MAX_LUN];
620 /*==========================================================
622 ** Declaration of structs: lun control block
624 **==========================================================
627 struct lcb {
629 ** during reselection the ncr jumps to this point
630 ** with SFBR set to the "Identify" message.
631 ** if it's not this lun, jump to the next.
633 ** JUMP IF (SFBR != #lun#)
634 ** @(next lcb of this target)
637 struct link jump_lcb;
640 ** if next message is "simple tag",
641 ** then load the tag to SFBR,
642 ** else load 0 to SFBR.
644 ** CALL
645 ** <RESEL_TAG>
648 struct link call_tag;
651 ** now look for the right nccb.
653 ** JUMP
654 ** @(first nccb of this lun)
657 struct link jump_nccb;
660 ** start of the nccb chain
663 nccb_p next_nccb;
666 ** Control of tagged queueing
669 u_char reqnccbs;
670 u_char reqlink;
671 u_char actlink;
672 u_char usetags;
673 u_char lasttag;
676 /*==========================================================
678 ** Declaration of structs: COMMAND control block
680 **==========================================================
682 ** This substructure is copied from the nccb to a
683 ** global address after selection (or reselection)
684 ** and copied back before disconnect.
686 ** These fields are accessible to the script processor.
688 **----------------------------------------------------------
691 struct head {
693 ** Execution of a nccb starts at this point.
694 ** It's a jump to the "SELECT" label
695 ** of the script.
697 ** After successful selection the script
698 ** processor overwrites it with a jump to
699 ** the IDLE label of the script.
702 struct link launch;
705 ** Saved data pointer.
706 ** Points to the position in the script
707 ** responsible for the actual transfer
708 ** of data.
709 ** It's written after reception of a
710 ** "SAVE_DATA_POINTER" message.
711 ** The goalpointer points after
712 ** the last transfer command.
715 u_int32_t savep;
716 u_int32_t lastp;
717 u_int32_t goalp;
720 ** The virtual address of the nccb
721 ** containing this header.
724 nccb_p cp;
727 ** space for some timestamps to gather
728 ** profiling data about devices and this driver.
731 struct tstamp stamp;
734 ** status fields.
737 u_char status[8];
741 ** The status bytes are used by the host and the script processor.
743 ** The first four byte are copied to the scratchb register
744 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
745 ** and copied back just after disconnecting.
746 ** Inside the script the XX_REG are used.
748 ** The last four bytes are used inside the script by "COPY" commands.
749 ** Because source and destination must have the same alignment
750 ** in a longword, the fields HAVE to be at the choosen offsets.
751 ** xerr_st (4) 0 (0x34) scratcha
752 ** sync_st (5) 1 (0x05) sxfer
753 ** wide_st (7) 3 (0x03) scntl3
757 ** First four bytes (script)
759 #define QU_REG scr0
760 #define HS_REG scr1
761 #define HS_PRT nc_scr1
762 #define SS_REG scr2
763 #define PS_REG scr3
766 ** First four bytes (host)
768 #define actualquirks phys.header.status[0]
769 #define host_status phys.header.status[1]
770 #define s_status phys.header.status[2]
771 #define parity_status phys.header.status[3]
774 ** Last four bytes (script)
776 #define xerr_st header.status[4] /* MUST be ==0 mod 4 */
777 #define sync_st header.status[5] /* MUST be ==1 mod 4 */
778 #define nego_st header.status[6]
779 #define wide_st header.status[7] /* MUST be ==3 mod 4 */
782 ** Last four bytes (host)
784 #define xerr_status phys.xerr_st
785 #define sync_status phys.sync_st
786 #define nego_status phys.nego_st
787 #define wide_status phys.wide_st
789 /*==========================================================
791 ** Declaration of structs: Data structure block
793 **==========================================================
795 ** During execution of a nccb by the script processor,
796 ** the DSA (data structure address) register points
797 ** to this substructure of the nccb.
798 ** This substructure contains the header with
799 ** the script-processor-changable data and
800 ** data blocks for the indirect move commands.
802 **----------------------------------------------------------
805 struct dsb {
808 ** Header.
809 ** Has to be the first entry,
810 ** because it's jumped to by the
811 ** script processor
814 struct head header;
817 ** Table data for Script
820 struct scr_tblsel select;
821 struct scr_tblmove smsg ;
822 struct scr_tblmove smsg2 ;
823 struct scr_tblmove cmd ;
824 struct scr_tblmove scmd ;
825 struct scr_tblmove sense ;
826 struct scr_tblmove data [MAX_SCATTER];
829 /*==========================================================
831 ** Declaration of structs: Command control block.
833 **==========================================================
835 ** During execution of a nccb by the script processor,
836 ** the DSA (data structure address) register points
837 ** to this substructure of the nccb.
838 ** This substructure contains the header with
839 ** the script-processor-changable data and then
840 ** data blocks for the indirect move commands.
842 **----------------------------------------------------------
846 struct nccb {
848 ** This filler ensures that the global header is
849 ** cache line size aligned.
851 ncrcmd filler[4];
854 ** during reselection the ncr jumps to this point.
855 ** If a "SIMPLE_TAG" message was received,
856 ** then SFBR is set to the tag.
857 ** else SFBR is set to 0
858 ** If looking for another tag, jump to the next nccb.
860 ** JUMP IF (SFBR != #TAG#)
861 ** @(next nccb of this lun)
864 struct link jump_nccb;
867 ** After execution of this call, the return address
868 ** (in the TEMP register) points to the following
869 ** data structure block.
870 ** So copy it to the DSA register, and start
871 ** processing of this data structure.
873 ** CALL
874 ** <RESEL_TMP>
877 struct link call_tmp;
880 ** This is the data structure which is
881 ** to be executed by the script processor.
884 struct dsb phys;
887 ** If a data transfer phase is terminated too early
888 ** (after reception of a message (i.e. DISCONNECT)),
889 ** we have to prepare a mini script to transfer
890 ** the rest of the data.
893 ncrcmd patch[8];
896 ** The general SCSI driver provides a
897 ** pointer to a control block.
900 union ccb *ccb;
903 ** We prepare a message to be sent after selection,
904 ** and a second one to be sent after getcc selection.
905 ** Contents are IDENTIFY and SIMPLE_TAG.
906 ** While negotiating sync or wide transfer,
907 ** a SDTM or WDTM message is appended.
910 u_char scsi_smsg [8];
911 u_char scsi_smsg2[8];
914 ** Lock this nccb.
915 ** Flag is used while looking for a free nccb.
918 u_long magic;
921 ** Physical address of this instance of nccb
924 u_long p_nccb;
927 ** Completion time out for this job.
928 ** It's set to time of start + allowed number of seconds.
931 time_t tlimit;
934 ** All nccbs of one hostadapter are chained.
937 nccb_p link_nccb;
940 ** All nccbs of one target/lun are chained.
943 nccb_p next_nccb;
946 ** Sense command
949 u_char sensecmd[6];
952 ** Tag for this transfer.
953 ** It's patched into jump_nccb.
954 ** If it's not zero, a SIMPLE_TAG
955 ** message is included in smsg.
958 u_char tag;
961 #define CCB_PHYS(cp,lbl) (cp->p_nccb + offsetof(struct nccb, lbl))
963 /*==========================================================
965 ** Declaration of structs: NCR device descriptor
967 **==========================================================
970 struct ncb {
972 ** The global header.
973 ** Accessible to both the host and the
974 ** script-processor.
975 ** We assume it is cache line size aligned.
977 struct head header;
979 int unit;
981 /*-----------------------------------------------
982 ** Scripts ..
983 **-----------------------------------------------
985 ** During reselection the ncr jumps to this point.
986 ** The SFBR register is loaded with the encoded target id.
988 ** Jump to the first target.
990 ** JUMP
991 ** @(next tcb)
993 struct link jump_tcb;
995 /*-----------------------------------------------
996 ** Configuration ..
997 **-----------------------------------------------
999 ** virtual and physical addresses
1000 ** of the 53c810 chip.
1002 int reg_rid;
1003 struct resource *reg_res;
1004 bus_space_tag_t bst;
1005 bus_space_handle_t bsh;
1007 int sram_rid;
1008 struct resource *sram_res;
1009 bus_space_tag_t bst2;
1010 bus_space_handle_t bsh2;
1012 struct resource *irq_res;
1013 void *irq_handle;
1016 ** Scripts instance virtual address.
1018 struct script *script;
1019 struct scripth *scripth;
1022 ** Scripts instance physical address.
1024 u_long p_script;
1025 u_long p_scripth;
1028 ** The SCSI address of the host adapter.
1030 u_char myaddr;
1033 ** timing parameters
1035 u_char minsync; /* Minimum sync period factor */
1036 u_char maxsync; /* Maximum sync period factor */
1037 u_char maxoffs; /* Max scsi offset */
1038 u_char clock_divn; /* Number of clock divisors */
1039 u_long clock_khz; /* SCSI clock frequency in KHz */
1040 u_long features; /* Chip features map */
1041 u_char multiplier; /* Clock multiplier (1,2,4) */
1043 u_char maxburst; /* log base 2 of dwords burst */
1046 ** BIOS supplied PCI bus options
1048 u_char rv_scntl3;
1049 u_char rv_dcntl;
1050 u_char rv_dmode;
1051 u_char rv_ctest3;
1052 u_char rv_ctest4;
1053 u_char rv_ctest5;
1054 u_char rv_gpcntl;
1055 u_char rv_stest2;
1057 /*-----------------------------------------------
1058 ** CAM SIM information for this instance
1059 **-----------------------------------------------
1062 struct cam_sim *sim;
1063 struct cam_path *path;
1065 /*-----------------------------------------------
1066 ** Job control
1067 **-----------------------------------------------
1069 ** Commands from user
1071 struct usrcmd user;
1074 ** Target data
1076 struct tcb target[MAX_TARGET];
1079 ** Start queue.
1081 u_int32_t squeue [MAX_START];
1082 u_short squeueput;
1085 ** Timeout handler
1087 time_t heartbeat;
1088 u_short ticks;
1089 u_short latetime;
1090 time_t lasttime;
1091 struct callout timeout_ch;
1093 /*-----------------------------------------------
1094 ** Debug and profiling
1095 **-----------------------------------------------
1097 ** register dump
1099 struct ncr_reg regdump;
1100 time_t regtime;
1103 ** Profiling data
1105 struct profile profile;
1106 u_long disc_phys;
1107 u_long disc_ref;
1110 ** Head of list of all nccbs for this controller.
1112 nccb_p link_nccb;
1115 ** message buffers.
1116 ** Should be longword aligned,
1117 ** because they're written with a
1118 ** COPY script command.
1120 u_char msgout[8];
1121 u_char msgin [8];
1122 u_int32_t lastmsg;
1125 ** Buffer for STATUS_IN phase.
1127 u_char scratch;
1130 ** controller chip dependent maximal transfer width.
1132 u_char maxwide;
1134 #ifdef NCR_IOMAPPED
1136 ** address of the ncr control registers in io space
1138 pci_port_t port;
1139 #endif
1142 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1143 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1145 /*==========================================================
1148 ** Script for NCR-Processor.
1150 ** Use ncr_script_fill() to create the variable parts.
1151 ** Use ncr_script_copy_and_bind() to make a copy and
1152 ** bind to physical addresses.
1155 **==========================================================
1157 ** We have to know the offsets of all labels before
1158 ** we reach them (for forward jumps).
1159 ** Therefore we declare a struct here.
1160 ** If you make changes inside the script,
1161 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1163 **----------------------------------------------------------
1167 ** Script fragments which are loaded into the on-board RAM
1168 ** of 825A, 875 and 895 chips.
1170 struct script {
1171 ncrcmd start [ 7];
1172 ncrcmd start0 [ 2];
1173 ncrcmd start1 [ 3];
1174 ncrcmd startpos [ 1];
1175 ncrcmd trysel [ 8];
1176 ncrcmd skip [ 8];
1177 ncrcmd skip2 [ 3];
1178 ncrcmd idle [ 2];
1179 ncrcmd select [ 18];
1180 ncrcmd prepare [ 4];
1181 ncrcmd loadpos [ 14];
1182 ncrcmd prepare2 [ 24];
1183 ncrcmd setmsg [ 5];
1184 ncrcmd clrack [ 2];
1185 ncrcmd dispatch [ 33];
1186 ncrcmd no_data [ 17];
1187 ncrcmd checkatn [ 10];
1188 ncrcmd command [ 15];
1189 ncrcmd status [ 27];
1190 ncrcmd msg_in [ 26];
1191 ncrcmd msg_bad [ 6];
1192 ncrcmd complete [ 13];
1193 ncrcmd cleanup [ 12];
1194 ncrcmd cleanup0 [ 9];
1195 ncrcmd signal [ 12];
1196 ncrcmd save_dp [ 5];
1197 ncrcmd restore_dp [ 5];
1198 ncrcmd disconnect [ 12];
1199 ncrcmd disconnect0 [ 5];
1200 ncrcmd disconnect1 [ 23];
1201 ncrcmd msg_out [ 9];
1202 ncrcmd msg_out_done [ 7];
1203 ncrcmd badgetcc [ 6];
1204 ncrcmd reselect [ 8];
1205 ncrcmd reselect1 [ 8];
1206 ncrcmd reselect2 [ 8];
1207 ncrcmd resel_tmp [ 5];
1208 ncrcmd resel_lun [ 18];
1209 ncrcmd resel_tag [ 24];
1210 ncrcmd data_in [MAX_SCATTER * 4 + 7];
1211 ncrcmd data_out [MAX_SCATTER * 4 + 7];
1215 ** Script fragments which stay in main memory for all chips.
1217 struct scripth {
1218 ncrcmd tryloop [MAX_START*5+2];
1219 ncrcmd msg_parity [ 6];
1220 ncrcmd msg_reject [ 8];
1221 ncrcmd msg_ign_residue [ 32];
1222 ncrcmd msg_extended [ 18];
1223 ncrcmd msg_ext_2 [ 18];
1224 ncrcmd msg_wdtr [ 27];
1225 ncrcmd msg_ext_3 [ 18];
1226 ncrcmd msg_sdtr [ 27];
1227 ncrcmd msg_out_abort [ 10];
1228 ncrcmd getcc [ 4];
1229 ncrcmd getcc1 [ 5];
1230 #ifdef NCR_GETCC_WITHMSG
1231 ncrcmd getcc2 [ 29];
1232 #else
1233 ncrcmd getcc2 [ 14];
1234 #endif
1235 ncrcmd getcc3 [ 6];
1236 ncrcmd aborttag [ 4];
1237 ncrcmd abort [ 22];
1238 ncrcmd snooptest [ 9];
1239 ncrcmd snoopend [ 2];
1242 /*==========================================================
1245 ** Function headers.
1248 **==========================================================
1251 #ifdef _KERNEL
1252 static nccb_p ncr_alloc_nccb (ncb_p np, u_long target, u_long lun);
1253 static void ncr_complete (ncb_p np, nccb_p cp);
1254 static int ncr_delta (int * from, int * to);
1255 static void ncr_exception (ncb_p np);
1256 static void ncr_free_nccb (ncb_p np, nccb_p cp);
1257 static void ncr_freeze_devq (ncb_p np, struct cam_path *path);
1258 static void ncr_selectclock (ncb_p np, u_char scntl3);
1259 static void ncr_getclock (ncb_p np, u_char multiplier);
1260 static nccb_p ncr_get_nccb (ncb_p np, u_long t,u_long l);
1261 #if 0
1262 static u_int32_t ncr_info (int unit);
1263 #endif
1264 static void ncr_init (ncb_p np, char * msg, u_long code);
1265 static void ncr_intr (void *vnp);
1266 static void ncr_int_ma (ncb_p np, u_char dstat);
1267 static void ncr_int_sir (ncb_p np);
1268 static void ncr_int_sto (ncb_p np);
1269 #if 0
1270 static void ncr_min_phys (struct buf *bp);
1271 #endif
1272 static void ncr_poll (struct cam_sim *sim);
1273 static void ncb_profile (ncb_p np, nccb_p cp);
1274 static void ncr_script_copy_and_bind
1275 (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
1276 static void ncr_script_fill (struct script * scr, struct scripth *scrh);
1277 static int ncr_scatter (struct dsb* phys, vm_offset_t vaddr,
1278 vm_size_t datalen);
1279 static void ncr_getsync (ncb_p np, u_char sfac, u_char *fakp,
1280 u_char *scntl3p);
1281 static void ncr_setsync (ncb_p np, nccb_p cp,u_char scntl3,u_char sxfer,
1282 u_char period);
1283 static void ncr_setwide (ncb_p np, nccb_p cp, u_char wide, u_char ack);
1284 static int ncr_show_msg (u_char * msg);
1285 static int ncr_snooptest (ncb_p np);
1286 static void ncr_action (struct cam_sim *sim, union ccb *ccb);
1287 static void ncr_timeout (void *arg);
1288 static void ncr_wakeup (ncb_p np, u_long code);
1290 static int ncr_probe (device_t dev);
1291 static int ncr_attach (device_t dev);
1293 #endif /* _KERNEL */
1295 /*==========================================================
1298 ** Global static data.
1301 **==========================================================
1304 #ifdef _KERNEL
1306 static int ncr_debug = SCSI_NCR_DEBUG;
1307 SYSCTL_INT(_debug, OID_AUTO, ncr_debug, CTLFLAG_RW, &ncr_debug, 0,
1308 "Driver debug flags");
1310 static int ncr_cache; /* to be aligned _NOT_ static */
1312 /*==========================================================
1315 ** Global static data: auto configure
1318 **==========================================================
1321 #define NCR_810_ID (0x00011000ul)
1322 #define NCR_815_ID (0x00041000ul)
1323 #define NCR_820_ID (0x00021000ul)
1324 #define NCR_825_ID (0x00031000ul)
1325 #define NCR_860_ID (0x00061000ul)
1326 #define NCR_875_ID (0x000f1000ul)
1327 #define NCR_875_ID2 (0x008f1000ul)
1328 #define NCR_885_ID (0x000d1000ul)
1329 #define NCR_895_ID (0x000c1000ul)
1330 #define NCR_896_ID (0x000b1000ul)
1331 #define NCR_895A_ID (0x00121000ul)
1332 #define NCR_1510D_ID (0x000a1000ul)
1335 static char *ncr_name (ncb_p np)
1337 static char name[10];
1338 ksnprintf(name, sizeof(name), "ncr%d", np->unit);
1339 return (name);
1342 /*==========================================================
1345 ** Scripts for NCR-Processor.
1347 ** Use ncr_script_bind for binding to physical addresses.
1350 **==========================================================
1352 ** NADDR generates a reference to a field of the controller data.
1353 ** PADDR generates a reference to another part of the script.
1354 ** RADDR generates a reference to a script processor register.
1355 ** FADDR generates a reference to a script processor register
1356 ** with offset.
1358 **----------------------------------------------------------
1361 #define RELOC_SOFTC 0x40000000
1362 #define RELOC_LABEL 0x50000000
1363 #define RELOC_REGISTER 0x60000000
1364 #define RELOC_KVAR 0x70000000
1365 #define RELOC_LABELH 0x80000000
1366 #define RELOC_MASK 0xf0000000
1368 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
1369 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
1370 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
1371 #define RADDR(label) (RELOC_REGISTER | REG(label))
1372 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
1373 #define KVAR(which) (RELOC_KVAR | (which))
1375 #define KVAR_SECOND (0)
1376 #define KVAR_TICKS (1)
1377 #define KVAR_NCR_CACHE (2)
1379 #define SCRIPT_KVAR_FIRST (0)
1380 #define SCRIPT_KVAR_LAST (3)
1383 * Kernel variables referenced in the scripts.
1384 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
1386 static void *script_kvars[] =
1387 { &time_uptime, &ticks, &ncr_cache };
1389 static struct script script0 = {
1390 /*--------------------------< START >-----------------------*/ {
1392 ** Claim to be still alive ...
1394 SCR_COPY (sizeof (((struct ncb *)0)->heartbeat)),
1395 KVAR (KVAR_SECOND),
1396 NADDR (heartbeat),
1398 ** Make data structure address invalid.
1399 ** clear SIGP.
1401 SCR_LOAD_REG (dsa, 0xff),
1403 SCR_FROM_REG (ctest2),
1405 }/*-------------------------< START0 >----------------------*/,{
1407 ** Hook for interrupted GetConditionCode.
1408 ** Will be patched to ... IFTRUE by
1409 ** the interrupt handler.
1411 SCR_INT ^ IFFALSE (0),
1412 SIR_SENSE_RESTART,
1414 }/*-------------------------< START1 >----------------------*/,{
1416 ** Hook for stalled start queue.
1417 ** Will be patched to IFTRUE by the interrupt handler.
1419 SCR_INT ^ IFFALSE (0),
1420 SIR_STALL_RESTART,
1422 ** Then jump to a certain point in tryloop.
1423 ** Due to the lack of indirect addressing the code
1424 ** is self modifying here.
1426 SCR_JUMP,
1427 }/*-------------------------< STARTPOS >--------------------*/,{
1428 PADDRH(tryloop),
1430 }/*-------------------------< TRYSEL >----------------------*/,{
1432 ** Now:
1433 ** DSA: Address of a Data Structure
1434 ** or Address of the IDLE-Label.
1436 ** TEMP: Address of a script, which tries to
1437 ** start the NEXT entry.
1439 ** Save the TEMP register into the SCRATCHA register.
1440 ** Then copy the DSA to TEMP and RETURN.
1441 ** This is kind of an indirect jump.
1442 ** (The script processor has NO stack, so the
1443 ** CALL is actually a jump and link, and the
1444 ** RETURN is an indirect jump.)
1446 ** If the slot was empty, DSA contains the address
1447 ** of the IDLE part of this script. The processor
1448 ** jumps to IDLE and waits for a reselect.
1449 ** It will wake up and try the same slot again
1450 ** after the SIGP bit becomes set by the host.
1452 ** If the slot was not empty, DSA contains
1453 ** the address of the phys-part of a nccb.
1454 ** The processor jumps to this address.
1455 ** phys starts with head,
1456 ** head starts with launch,
1457 ** so actually the processor jumps to
1458 ** the lauch part.
1459 ** If the entry is scheduled for execution,
1460 ** then launch contains a jump to SELECT.
1461 ** If it's not scheduled, it contains a jump to IDLE.
1463 SCR_COPY (4),
1464 RADDR (temp),
1465 RADDR (scratcha),
1466 SCR_COPY (4),
1467 RADDR (dsa),
1468 RADDR (temp),
1469 SCR_RETURN,
1472 }/*-------------------------< SKIP >------------------------*/,{
1474 ** This entry has been canceled.
1475 ** Next time use the next slot.
1477 SCR_COPY (4),
1478 RADDR (scratcha),
1479 PADDR (startpos),
1481 ** patch the launch field.
1482 ** should look like an idle process.
1484 SCR_COPY_F (4),
1485 RADDR (dsa),
1486 PADDR (skip2),
1487 SCR_COPY (8),
1488 PADDR (idle),
1489 }/*-------------------------< SKIP2 >-----------------------*/,{
1491 SCR_JUMP,
1492 PADDR(start),
1493 }/*-------------------------< IDLE >------------------------*/,{
1495 ** Nothing to do?
1496 ** Wait for reselect.
1498 SCR_JUMP,
1499 PADDR(reselect),
1501 }/*-------------------------< SELECT >----------------------*/,{
1503 ** DSA contains the address of a scheduled
1504 ** data structure.
1506 ** SCRATCHA contains the address of the script,
1507 ** which starts the next entry.
1509 ** Set Initiator mode.
1511 ** (Target mode is left as an exercise for the reader)
1514 SCR_CLR (SCR_TRG),
1516 SCR_LOAD_REG (HS_REG, 0xff),
1520 ** And try to select this target.
1522 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
1523 PADDR (reselect),
1526 ** Now there are 4 possibilities:
1528 ** (1) The ncr looses arbitration.
1529 ** This is ok, because it will try again,
1530 ** when the bus becomes idle.
1531 ** (But beware of the timeout function!)
1533 ** (2) The ncr is reselected.
1534 ** Then the script processor takes the jump
1535 ** to the RESELECT label.
1537 ** (3) The ncr completes the selection.
1538 ** Then it will execute the next statement.
1540 ** (4) There is a selection timeout.
1541 ** Then the ncr should interrupt the host and stop.
1542 ** Unfortunately, it seems to continue execution
1543 ** of the script. But it will fail with an
1544 ** IID-interrupt on the next WHEN.
1547 SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
1551 ** Send the IDENTIFY and SIMPLE_TAG messages
1552 ** (and the MSG_EXT_SDTR message)
1554 SCR_MOVE_TBL ^ SCR_MSG_OUT,
1555 offsetof (struct dsb, smsg),
1556 #ifdef undef /* XXX better fail than try to deal with this ... */
1557 SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_OUT)),
1558 -16,
1559 #endif
1560 SCR_CLR (SCR_ATN),
1562 SCR_COPY (1),
1563 RADDR (sfbr),
1564 NADDR (lastmsg),
1566 ** Selection complete.
1567 ** Next time use the next slot.
1569 SCR_COPY (4),
1570 RADDR (scratcha),
1571 PADDR (startpos),
1572 }/*-------------------------< PREPARE >----------------------*/,{
1574 ** The ncr doesn't have an indirect load
1575 ** or store command. So we have to
1576 ** copy part of the control block to a
1577 ** fixed place, where we can access it.
1579 ** We patch the address part of a
1580 ** COPY command with the DSA-register.
1582 SCR_COPY_F (4),
1583 RADDR (dsa),
1584 PADDR (loadpos),
1586 ** then we do the actual copy.
1588 SCR_COPY (sizeof (struct head)),
1590 ** continued after the next label ...
1593 }/*-------------------------< LOADPOS >---------------------*/,{
1595 NADDR (header),
1597 ** Mark this nccb as not scheduled.
1599 SCR_COPY (8),
1600 PADDR (idle),
1601 NADDR (header.launch),
1603 ** Set a time stamp for this selection
1605 SCR_COPY (sizeof (ticks)),
1606 KVAR (KVAR_TICKS),
1607 NADDR (header.stamp.select),
1609 ** load the savep (saved pointer) into
1610 ** the TEMP register (actual pointer)
1612 SCR_COPY (4),
1613 NADDR (header.savep),
1614 RADDR (temp),
1616 ** Initialize the status registers
1618 SCR_COPY (4),
1619 NADDR (header.status),
1620 RADDR (scr0),
1622 }/*-------------------------< PREPARE2 >---------------------*/,{
1624 ** Load the synchronous mode register
1626 SCR_COPY (1),
1627 NADDR (sync_st),
1628 RADDR (sxfer),
1630 ** Load the wide mode and timing register
1632 SCR_COPY (1),
1633 NADDR (wide_st),
1634 RADDR (scntl3),
1636 ** Initialize the msgout buffer with a NOOP message.
1638 SCR_LOAD_REG (scratcha, MSG_NOOP),
1640 SCR_COPY (1),
1641 RADDR (scratcha),
1642 NADDR (msgout),
1643 SCR_COPY (1),
1644 RADDR (scratcha),
1645 NADDR (msgin),
1647 ** Message in phase ?
1649 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
1650 PADDR (dispatch),
1652 ** Extended or reject message ?
1654 SCR_FROM_REG (sbdl),
1656 SCR_JUMP ^ IFTRUE (DATA (MSG_EXTENDED)),
1657 PADDR (msg_in),
1658 SCR_JUMP ^ IFTRUE (DATA (MSG_MESSAGE_REJECT)),
1659 PADDRH (msg_reject),
1661 ** normal processing
1663 SCR_JUMP,
1664 PADDR (dispatch),
1665 }/*-------------------------< SETMSG >----------------------*/,{
1666 SCR_COPY (1),
1667 RADDR (scratcha),
1668 NADDR (msgout),
1669 SCR_SET (SCR_ATN),
1671 }/*-------------------------< CLRACK >----------------------*/,{
1673 ** Terminate possible pending message phase.
1675 SCR_CLR (SCR_ACK),
1678 }/*-----------------------< DISPATCH >----------------------*/,{
1679 SCR_FROM_REG (HS_REG),
1681 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
1682 SIR_NEGO_FAILED,
1684 ** remove bogus output signals
1686 SCR_REG_REG (socl, SCR_AND, CACK|CATN),
1688 SCR_RETURN ^ IFTRUE (WHEN (SCR_DATA_OUT)),
1690 SCR_RETURN ^ IFTRUE (IF (SCR_DATA_IN)),
1692 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
1693 PADDR (msg_out),
1694 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN)),
1695 PADDR (msg_in),
1696 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
1697 PADDR (command),
1698 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
1699 PADDR (status),
1701 ** Discard one illegal phase byte, if required.
1703 SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
1705 SCR_COPY (1),
1706 RADDR (scratcha),
1707 NADDR (xerr_st),
1708 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
1710 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
1711 NADDR (scratch),
1712 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
1714 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
1715 NADDR (scratch),
1716 SCR_JUMP,
1717 PADDR (dispatch),
1719 }/*-------------------------< NO_DATA >--------------------*/,{
1721 ** The target wants to tranfer too much data
1722 ** or in the wrong direction.
1723 ** Remember that in extended error.
1725 SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
1727 SCR_COPY (1),
1728 RADDR (scratcha),
1729 NADDR (xerr_st),
1731 ** Discard one data byte, if required.
1733 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
1735 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
1736 NADDR (scratch),
1737 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1739 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
1740 NADDR (scratch),
1742 ** .. and repeat as required.
1744 SCR_CALL,
1745 PADDR (dispatch),
1746 SCR_JUMP,
1747 PADDR (no_data),
1748 }/*-------------------------< CHECKATN >--------------------*/,{
1750 ** If AAP (bit 1 of scntl0 register) is set
1751 ** and a parity error is detected,
1752 ** the script processor asserts ATN.
1754 ** The target should switch to a MSG_OUT phase
1755 ** to get the message.
1757 SCR_FROM_REG (socl),
1759 SCR_JUMP ^ IFFALSE (MASK (CATN, CATN)),
1760 PADDR (dispatch),
1762 ** count it
1764 SCR_REG_REG (PS_REG, SCR_ADD, 1),
1767 ** Prepare a MSG_INITIATOR_DET_ERR message
1768 ** (initiator detected error).
1769 ** The target should retry the transfer.
1771 SCR_LOAD_REG (scratcha, MSG_INITIATOR_DET_ERR),
1773 SCR_JUMP,
1774 PADDR (setmsg),
1776 }/*-------------------------< COMMAND >--------------------*/,{
1778 ** If this is not a GETCC transfer ...
1780 SCR_FROM_REG (SS_REG),
1782 /*<<<*/ SCR_JUMPR ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
1785 ** ... set a timestamp ...
1787 SCR_COPY (sizeof (ticks)),
1788 KVAR (KVAR_TICKS),
1789 NADDR (header.stamp.command),
1791 ** ... and send the command
1793 SCR_MOVE_TBL ^ SCR_COMMAND,
1794 offsetof (struct dsb, cmd),
1795 SCR_JUMP,
1796 PADDR (dispatch),
1798 ** Send the GETCC command
1800 /*>>>*/ SCR_MOVE_TBL ^ SCR_COMMAND,
1801 offsetof (struct dsb, scmd),
1802 SCR_JUMP,
1803 PADDR (dispatch),
1805 }/*-------------------------< STATUS >--------------------*/,{
1807 ** set the timestamp.
1809 SCR_COPY (sizeof (ticks)),
1810 KVAR (KVAR_TICKS),
1811 NADDR (header.stamp.status),
1813 ** If this is a GETCC transfer,
1815 SCR_FROM_REG (SS_REG),
1817 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (SCSI_STATUS_CHECK_COND)),
1820 ** get the status
1822 SCR_MOVE_ABS (1) ^ SCR_STATUS,
1823 NADDR (scratch),
1825 ** Save status to scsi_status.
1826 ** Mark as complete.
1827 ** And wait for disconnect.
1829 SCR_TO_REG (SS_REG),
1831 SCR_REG_REG (SS_REG, SCR_OR, SCSI_STATUS_SENSE),
1833 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1835 SCR_JUMP,
1836 PADDR (checkatn),
1838 ** If it was no GETCC transfer,
1839 ** save the status to scsi_status.
1841 /*>>>*/ SCR_MOVE_ABS (1) ^ SCR_STATUS,
1842 NADDR (scratch),
1843 SCR_TO_REG (SS_REG),
1846 ** if it was no check condition ...
1848 SCR_JUMP ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
1849 PADDR (checkatn),
1851 ** ... mark as complete.
1853 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1855 SCR_JUMP,
1856 PADDR (checkatn),
1858 }/*-------------------------< MSG_IN >--------------------*/,{
1860 ** Get the first byte of the message
1861 ** and save it to SCRATCHA.
1863 ** The script processor doesn't negate the
1864 ** ACK signal after this transfer.
1866 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
1867 NADDR (msgin[0]),
1869 ** Check for message parity error.
1871 SCR_TO_REG (scratcha),
1873 SCR_FROM_REG (socl),
1875 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
1876 PADDRH (msg_parity),
1877 SCR_FROM_REG (scratcha),
1880 ** Parity was ok, handle this message.
1882 SCR_JUMP ^ IFTRUE (DATA (MSG_CMDCOMPLETE)),
1883 PADDR (complete),
1884 SCR_JUMP ^ IFTRUE (DATA (MSG_SAVEDATAPOINTER)),
1885 PADDR (save_dp),
1886 SCR_JUMP ^ IFTRUE (DATA (MSG_RESTOREPOINTERS)),
1887 PADDR (restore_dp),
1888 SCR_JUMP ^ IFTRUE (DATA (MSG_DISCONNECT)),
1889 PADDR (disconnect),
1890 SCR_JUMP ^ IFTRUE (DATA (MSG_EXTENDED)),
1891 PADDRH (msg_extended),
1892 SCR_JUMP ^ IFTRUE (DATA (MSG_NOOP)),
1893 PADDR (clrack),
1894 SCR_JUMP ^ IFTRUE (DATA (MSG_MESSAGE_REJECT)),
1895 PADDRH (msg_reject),
1896 SCR_JUMP ^ IFTRUE (DATA (MSG_IGN_WIDE_RESIDUE)),
1897 PADDRH (msg_ign_residue),
1899 ** Rest of the messages left as
1900 ** an exercise ...
1902 ** Unimplemented messages:
1903 ** fall through to MSG_BAD.
1905 }/*-------------------------< MSG_BAD >------------------*/,{
1907 ** unimplemented message - reject it.
1909 SCR_INT,
1910 SIR_REJECT_SENT,
1911 SCR_LOAD_REG (scratcha, MSG_MESSAGE_REJECT),
1913 SCR_JUMP,
1914 PADDR (setmsg),
1916 }/*-------------------------< COMPLETE >-----------------*/,{
1918 ** Complete message.
1920 ** If it's not the get condition code,
1921 ** copy TEMP register to LASTP in header.
1923 SCR_FROM_REG (SS_REG),
1925 /*<<<*/ SCR_JUMPR ^ IFTRUE (MASK (SCSI_STATUS_SENSE, SCSI_STATUS_SENSE)),
1927 SCR_COPY (4),
1928 RADDR (temp),
1929 NADDR (header.lastp),
1930 /*>>>*/ /*
1931 ** When we terminate the cycle by clearing ACK,
1932 ** the target may disconnect immediately.
1934 ** We don't want to be told of an
1935 ** "unexpected disconnect",
1936 ** so we disable this feature.
1938 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
1941 ** Terminate cycle ...
1943 SCR_CLR (SCR_ACK|SCR_ATN),
1946 ** ... and wait for the disconnect.
1948 SCR_WAIT_DISC,
1950 }/*-------------------------< CLEANUP >-------------------*/,{
1952 ** dsa: Pointer to nccb
1953 ** or xxxxxxFF (no nccb)
1955 ** HS_REG: Host-Status (<>0!)
1957 SCR_FROM_REG (dsa),
1959 SCR_JUMP ^ IFTRUE (DATA (0xff)),
1960 PADDR (signal),
1962 ** dsa is valid.
1963 ** save the status registers
1965 SCR_COPY (4),
1966 RADDR (scr0),
1967 NADDR (header.status),
1969 ** and copy back the header to the nccb.
1971 SCR_COPY_F (4),
1972 RADDR (dsa),
1973 PADDR (cleanup0),
1974 SCR_COPY (sizeof (struct head)),
1975 NADDR (header),
1976 }/*-------------------------< CLEANUP0 >--------------------*/,{
1980 ** If command resulted in "check condition"
1981 ** status and is not yet completed,
1982 ** try to get the condition code.
1984 SCR_FROM_REG (HS_REG),
1986 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
1988 SCR_FROM_REG (SS_REG),
1990 SCR_JUMP ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
1991 PADDRH(getcc2),
1992 }/*-------------------------< SIGNAL >----------------------*/,{
1994 ** if status = queue full,
1995 ** reinsert in startqueue and stall queue.
1997 /*>>>*/ SCR_FROM_REG (SS_REG),
1999 SCR_INT ^ IFTRUE (DATA (SCSI_STATUS_QUEUE_FULL)),
2000 SIR_STALL_QUEUE,
2002 ** And make the DSA register invalid.
2004 SCR_LOAD_REG (dsa, 0xff), /* invalid */
2007 ** if job completed ...
2009 SCR_FROM_REG (HS_REG),
2012 ** ... signal completion to the host
2014 SCR_INT_FLY ^ IFFALSE (MASK (0, HS_DONEMASK)),
2017 ** Auf zu neuen Schandtaten!
2019 SCR_JUMP,
2020 PADDR(start),
2022 }/*-------------------------< SAVE_DP >------------------*/,{
2024 ** SAVE_DP message:
2025 ** Copy TEMP register to SAVEP in header.
2027 SCR_COPY (4),
2028 RADDR (temp),
2029 NADDR (header.savep),
2030 SCR_JUMP,
2031 PADDR (clrack),
2032 }/*-------------------------< RESTORE_DP >---------------*/,{
2034 ** RESTORE_DP message:
2035 ** Copy SAVEP in header to TEMP register.
2037 SCR_COPY (4),
2038 NADDR (header.savep),
2039 RADDR (temp),
2040 SCR_JUMP,
2041 PADDR (clrack),
2043 }/*-------------------------< DISCONNECT >---------------*/,{
2045 ** If QUIRK_AUTOSAVE is set,
2046 ** do an "save pointer" operation.
2048 SCR_FROM_REG (QU_REG),
2050 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
2053 ** like SAVE_DP message:
2054 ** Copy TEMP register to SAVEP in header.
2056 SCR_COPY (4),
2057 RADDR (temp),
2058 NADDR (header.savep),
2059 /*>>>*/ /*
2060 ** Check if temp==savep or temp==goalp:
2061 ** if not, log a missing save pointer message.
2062 ** In fact, it's a comparison mod 256.
2064 ** Hmmm, I hadn't thought that I would be urged to
2065 ** write this kind of ugly self modifying code.
2067 ** It's unbelievable, but the ncr53c8xx isn't able
2068 ** to subtract one register from another.
2070 SCR_FROM_REG (temp),
2073 ** You are not expected to understand this ..
2075 ** CAUTION: only little endian architectures supported! XXX
2077 SCR_COPY_F (1),
2078 NADDR (header.savep),
2079 PADDR (disconnect0),
2080 }/*-------------------------< DISCONNECT0 >--------------*/,{
2081 /*<<<*/ SCR_JUMPR ^ IFTRUE (DATA (1)),
2084 ** neither this
2086 SCR_COPY_F (1),
2087 NADDR (header.goalp),
2088 PADDR (disconnect1),
2089 }/*-------------------------< DISCONNECT1 >--------------*/,{
2090 SCR_INT ^ IFFALSE (DATA (1)),
2091 SIR_MISSING_SAVE,
2092 /*>>>*/
2095 ** DISCONNECTing ...
2097 ** disable the "unexpected disconnect" feature,
2098 ** and remove the ACK signal.
2100 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2102 SCR_CLR (SCR_ACK|SCR_ATN),
2105 ** Wait for the disconnect.
2107 SCR_WAIT_DISC,
2110 ** Profiling:
2111 ** Set a time stamp,
2112 ** and count the disconnects.
2114 SCR_COPY (sizeof (ticks)),
2115 KVAR (KVAR_TICKS),
2116 NADDR (header.stamp.disconnect),
2117 SCR_COPY (4),
2118 NADDR (disc_phys),
2119 RADDR (temp),
2120 SCR_REG_REG (temp, SCR_ADD, 0x01),
2122 SCR_COPY (4),
2123 RADDR (temp),
2124 NADDR (disc_phys),
2126 ** Status is: DISCONNECTED.
2128 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2130 SCR_JUMP,
2131 PADDR (cleanup),
2133 }/*-------------------------< MSG_OUT >-------------------*/,{
2135 ** The target requests a message.
2137 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2138 NADDR (msgout),
2139 SCR_COPY (1),
2140 RADDR (sfbr),
2141 NADDR (lastmsg),
2143 ** If it was no ABORT message ...
2145 SCR_JUMP ^ IFTRUE (DATA (MSG_ABORT)),
2146 PADDRH (msg_out_abort),
2148 ** ... wait for the next phase
2149 ** if it's a message out, send it again, ...
2151 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2152 PADDR (msg_out),
2153 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2155 ** ... else clear the message ...
2157 SCR_LOAD_REG (scratcha, MSG_NOOP),
2159 SCR_COPY (4),
2160 RADDR (scratcha),
2161 NADDR (msgout),
2163 ** ... and process the next phase
2165 SCR_JUMP,
2166 PADDR (dispatch),
2168 }/*------------------------< BADGETCC >---------------------*/,{
2170 ** If SIGP was set, clear it and try again.
2172 SCR_FROM_REG (ctest2),
2174 SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
2175 PADDRH (getcc2),
2176 SCR_INT,
2177 SIR_SENSE_FAILED,
2178 }/*-------------------------< RESELECT >--------------------*/,{
2180 ** This NOP will be patched with LED OFF
2181 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2183 SCR_NO_OP,
2187 ** make the DSA invalid.
2189 SCR_LOAD_REG (dsa, 0xff),
2191 SCR_CLR (SCR_TRG),
2194 ** Sleep waiting for a reselection.
2195 ** If SIGP is set, special treatment.
2197 ** Zu allem bereit ..
2199 SCR_WAIT_RESEL,
2200 PADDR(reselect2),
2201 }/*-------------------------< RESELECT1 >--------------------*/,{
2203 ** This NOP will be patched with LED ON
2204 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2206 SCR_NO_OP,
2209 ** ... zu nichts zu gebrauchen ?
2211 ** load the target id into the SFBR
2212 ** and jump to the control block.
2214 ** Look at the declarations of
2215 ** - struct ncb
2216 ** - struct tcb
2217 ** - struct lcb
2218 ** - struct nccb
2219 ** to understand what's going on.
2221 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2223 SCR_TO_REG (sdid),
2225 SCR_JUMP,
2226 NADDR (jump_tcb),
2227 }/*-------------------------< RESELECT2 >-------------------*/,{
2229 ** This NOP will be patched with LED ON
2230 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2232 SCR_NO_OP,
2235 ** If it's not connected :(
2236 ** -> interrupted by SIGP bit.
2237 ** Jump to start.
2239 SCR_FROM_REG (ctest2),
2241 SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
2242 PADDR (start),
2243 SCR_JUMP,
2244 PADDR (reselect),
2246 }/*-------------------------< RESEL_TMP >-------------------*/,{
2248 ** The return address in TEMP
2249 ** is in fact the data structure address,
2250 ** so copy it to the DSA register.
2252 SCR_COPY (4),
2253 RADDR (temp),
2254 RADDR (dsa),
2255 SCR_JUMP,
2256 PADDR (prepare),
2258 }/*-------------------------< RESEL_LUN >-------------------*/,{
2260 ** come back to this point
2261 ** to get an IDENTIFY message
2262 ** Wait for a msg_in phase.
2264 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2267 ** message phase
2268 ** It's not a sony, it's a trick:
2269 ** read the data without acknowledging it.
2271 SCR_FROM_REG (sbdl),
2273 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (MSG_IDENTIFYFLAG, 0x98)),
2276 ** It WAS an Identify message.
2277 ** get it and ack it!
2279 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2280 NADDR (msgin),
2281 SCR_CLR (SCR_ACK),
2284 ** Mask out the lun.
2286 SCR_REG_REG (sfbr, SCR_AND, 0x07),
2288 SCR_RETURN,
2291 ** No message phase or no IDENTIFY message:
2292 ** return 0.
2294 /*>>>*/ SCR_LOAD_SFBR (0),
2296 SCR_RETURN,
2299 }/*-------------------------< RESEL_TAG >-------------------*/,{
2301 ** come back to this point
2302 ** to get a SIMPLE_TAG message
2303 ** Wait for a MSG_IN phase.
2305 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2308 ** message phase
2309 ** It's a trick - read the data
2310 ** without acknowledging it.
2312 SCR_FROM_REG (sbdl),
2314 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (MSG_SIMPLE_Q_TAG)),
2317 ** It WAS a SIMPLE_TAG message.
2318 ** get it and ack it!
2320 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2321 NADDR (msgin),
2322 SCR_CLR (SCR_ACK),
2325 ** Wait for the second byte (the tag)
2327 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2330 ** Get it and ack it!
2332 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2333 NADDR (msgin),
2334 SCR_CLR (SCR_ACK|SCR_CARRY),
2336 SCR_RETURN,
2339 ** No message phase or no SIMPLE_TAG message
2340 ** or no second byte: return 0.
2342 /*>>>*/ SCR_LOAD_SFBR (0),
2344 SCR_SET (SCR_CARRY),
2346 SCR_RETURN,
2349 }/*-------------------------< DATA_IN >--------------------*/,{
2351 ** Because the size depends on the
2352 ** #define MAX_SCATTER parameter,
2353 ** it is filled in at runtime.
2355 ** SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2356 ** PADDR (no_data),
2357 ** SCR_COPY (sizeof (ticks)),
2358 ** KVAR (KVAR_TICKS),
2359 ** NADDR (header.stamp.data),
2360 ** SCR_MOVE_TBL ^ SCR_DATA_IN,
2361 ** offsetof (struct dsb, data[ 0]),
2363 ** ##===========< i=1; i<MAX_SCATTER >=========
2364 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2365 ** || PADDR (checkatn),
2366 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2367 ** || offsetof (struct dsb, data[ i]),
2368 ** ##==========================================
2370 ** SCR_CALL,
2371 ** PADDR (checkatn),
2372 ** SCR_JUMP,
2373 ** PADDR (no_data),
2376 }/*-------------------------< DATA_OUT >-------------------*/,{
2378 ** Because the size depends on the
2379 ** #define MAX_SCATTER parameter,
2380 ** it is filled in at runtime.
2382 ** SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2383 ** PADDR (no_data),
2384 ** SCR_COPY (sizeof (ticks)),
2385 ** KVAR (KVAR_TICKS),
2386 ** NADDR (header.stamp.data),
2387 ** SCR_MOVE_TBL ^ SCR_DATA_OUT,
2388 ** offsetof (struct dsb, data[ 0]),
2390 ** ##===========< i=1; i<MAX_SCATTER >=========
2391 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2392 ** || PADDR (dispatch),
2393 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2394 ** || offsetof (struct dsb, data[ i]),
2395 ** ##==========================================
2397 ** SCR_CALL,
2398 ** PADDR (dispatch),
2399 ** SCR_JUMP,
2400 ** PADDR (no_data),
2402 **---------------------------------------------------------
2404 (u_long)0
2406 }/*--------------------------------------------------------*/
2410 static struct scripth scripth0 = {
2411 /*-------------------------< TRYLOOP >---------------------*/{
2413 ** Load an entry of the start queue into dsa
2414 ** and try to start it by jumping to TRYSEL.
2416 ** Because the size depends on the
2417 ** #define MAX_START parameter, it is filled
2418 ** in at runtime.
2420 **-----------------------------------------------------------
2422 ** ##===========< I=0; i<MAX_START >===========
2423 ** || SCR_COPY (4),
2424 ** || NADDR (squeue[i]),
2425 ** || RADDR (dsa),
2426 ** || SCR_CALL,
2427 ** || PADDR (trysel),
2428 ** ##==========================================
2430 ** SCR_JUMP,
2431 ** PADDRH(tryloop),
2433 **-----------------------------------------------------------
2436 }/*-------------------------< MSG_PARITY >---------------*/,{
2438 ** count it
2440 SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
2443 ** send a "message parity error" message.
2445 SCR_LOAD_REG (scratcha, MSG_PARITY_ERROR),
2447 SCR_JUMP,
2448 PADDR (setmsg),
2449 }/*-------------------------< MSG_MESSAGE_REJECT >---------------*/,{
2451 ** If a negotiation was in progress,
2452 ** negotiation failed.
2454 SCR_FROM_REG (HS_REG),
2456 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2457 SIR_NEGO_FAILED,
2459 ** else make host log this message
2461 SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
2462 SIR_REJECT_RECEIVED,
2463 SCR_JUMP,
2464 PADDR (clrack),
2466 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2468 ** Terminate cycle
2470 SCR_CLR (SCR_ACK),
2472 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2473 PADDR (dispatch),
2475 ** get residue size.
2477 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2478 NADDR (msgin[1]),
2480 ** Check for message parity error.
2482 SCR_TO_REG (scratcha),
2484 SCR_FROM_REG (socl),
2486 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2487 PADDRH (msg_parity),
2488 SCR_FROM_REG (scratcha),
2491 ** Size is 0 .. ignore message.
2493 SCR_JUMP ^ IFTRUE (DATA (0)),
2494 PADDR (clrack),
2496 ** Size is not 1 .. have to interrupt.
2498 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (1)),
2501 ** Check for residue byte in swide register
2503 SCR_FROM_REG (scntl2),
2505 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2508 ** There IS data in the swide register.
2509 ** Discard it.
2511 SCR_REG_REG (scntl2, SCR_OR, WSR),
2513 SCR_JUMP,
2514 PADDR (clrack),
2516 ** Load again the size to the sfbr register.
2518 /*>>>*/ SCR_FROM_REG (scratcha),
2520 /*>>>*/ SCR_INT,
2521 SIR_IGN_RESIDUE,
2522 SCR_JUMP,
2523 PADDR (clrack),
2525 }/*-------------------------< MSG_EXTENDED >-------------*/,{
2527 ** Terminate cycle
2529 SCR_CLR (SCR_ACK),
2531 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2532 PADDR (dispatch),
2534 ** get length.
2536 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2537 NADDR (msgin[1]),
2539 ** Check for message parity error.
2541 SCR_TO_REG (scratcha),
2543 SCR_FROM_REG (socl),
2545 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2546 PADDRH (msg_parity),
2547 SCR_FROM_REG (scratcha),
2551 SCR_JUMP ^ IFTRUE (DATA (3)),
2552 PADDRH (msg_ext_3),
2553 SCR_JUMP ^ IFFALSE (DATA (2)),
2554 PADDR (msg_bad),
2555 }/*-------------------------< MSG_EXT_2 >----------------*/,{
2556 SCR_CLR (SCR_ACK),
2558 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2559 PADDR (dispatch),
2561 ** get extended message code.
2563 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2564 NADDR (msgin[2]),
2566 ** Check for message parity error.
2568 SCR_TO_REG (scratcha),
2570 SCR_FROM_REG (socl),
2572 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2573 PADDRH (msg_parity),
2574 SCR_FROM_REG (scratcha),
2576 SCR_JUMP ^ IFTRUE (DATA (MSG_EXT_WDTR)),
2577 PADDRH (msg_wdtr),
2579 ** unknown extended message
2581 SCR_JUMP,
2582 PADDR (msg_bad)
2583 }/*-------------------------< MSG_WDTR >-----------------*/,{
2584 SCR_CLR (SCR_ACK),
2586 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2587 PADDR (dispatch),
2589 ** get data bus width
2591 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2592 NADDR (msgin[3]),
2593 SCR_FROM_REG (socl),
2595 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2596 PADDRH (msg_parity),
2598 ** let the host do the real work.
2600 SCR_INT,
2601 SIR_NEGO_WIDE,
2603 ** let the target fetch our answer.
2605 SCR_SET (SCR_ATN),
2607 SCR_CLR (SCR_ACK),
2610 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2611 SIR_NEGO_PROTO,
2613 ** Send the MSG_EXT_WDTR
2615 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
2616 NADDR (msgout),
2617 SCR_CLR (SCR_ATN),
2619 SCR_COPY (1),
2620 RADDR (sfbr),
2621 NADDR (lastmsg),
2622 SCR_JUMP,
2623 PADDR (msg_out_done),
2625 }/*-------------------------< MSG_EXT_3 >----------------*/,{
2626 SCR_CLR (SCR_ACK),
2628 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2629 PADDR (dispatch),
2631 ** get extended message code.
2633 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2634 NADDR (msgin[2]),
2636 ** Check for message parity error.
2638 SCR_TO_REG (scratcha),
2640 SCR_FROM_REG (socl),
2642 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2643 PADDRH (msg_parity),
2644 SCR_FROM_REG (scratcha),
2646 SCR_JUMP ^ IFTRUE (DATA (MSG_EXT_SDTR)),
2647 PADDRH (msg_sdtr),
2649 ** unknown extended message
2651 SCR_JUMP,
2652 PADDR (msg_bad)
2654 }/*-------------------------< MSG_SDTR >-----------------*/,{
2655 SCR_CLR (SCR_ACK),
2657 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2658 PADDR (dispatch),
2660 ** get period and offset
2662 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
2663 NADDR (msgin[3]),
2664 SCR_FROM_REG (socl),
2666 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2667 PADDRH (msg_parity),
2669 ** let the host do the real work.
2671 SCR_INT,
2672 SIR_NEGO_SYNC,
2674 ** let the target fetch our answer.
2676 SCR_SET (SCR_ATN),
2678 SCR_CLR (SCR_ACK),
2681 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2682 SIR_NEGO_PROTO,
2684 ** Send the MSG_EXT_SDTR
2686 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
2687 NADDR (msgout),
2688 SCR_CLR (SCR_ATN),
2690 SCR_COPY (1),
2691 RADDR (sfbr),
2692 NADDR (lastmsg),
2693 SCR_JUMP,
2694 PADDR (msg_out_done),
2696 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
2698 ** After ABORT message,
2700 ** expect an immediate disconnect, ...
2702 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2704 SCR_CLR (SCR_ACK|SCR_ATN),
2706 SCR_WAIT_DISC,
2709 ** ... and set the status to "ABORTED"
2711 SCR_LOAD_REG (HS_REG, HS_ABORTED),
2713 SCR_JUMP,
2714 PADDR (cleanup),
2716 }/*-------------------------< GETCC >-----------------------*/,{
2718 ** The ncr doesn't have an indirect load
2719 ** or store command. So we have to
2720 ** copy part of the control block to a
2721 ** fixed place, where we can modify it.
2723 ** We patch the address part of a COPY command
2724 ** with the address of the dsa register ...
2726 SCR_COPY_F (4),
2727 RADDR (dsa),
2728 PADDRH (getcc1),
2730 ** ... then we do the actual copy.
2732 SCR_COPY (sizeof (struct head)),
2733 }/*-------------------------< GETCC1 >----------------------*/,{
2735 NADDR (header),
2737 ** Initialize the status registers
2739 SCR_COPY (4),
2740 NADDR (header.status),
2741 RADDR (scr0),
2742 }/*-------------------------< GETCC2 >----------------------*/,{
2744 ** Get the condition code from a target.
2746 ** DSA points to a data structure.
2747 ** Set TEMP to the script location
2748 ** that receives the condition code.
2750 ** Because there is no script command
2751 ** to load a longword into a register,
2752 ** we use a CALL command.
2754 /*<<<*/ SCR_CALLR,
2757 ** Get the condition code.
2759 SCR_MOVE_TBL ^ SCR_DATA_IN,
2760 offsetof (struct dsb, sense),
2762 ** No data phase may follow!
2764 SCR_CALL,
2765 PADDR (checkatn),
2766 SCR_JUMP,
2767 PADDR (no_data),
2768 /*>>>*/
2771 ** The CALL jumps to this point.
2772 ** Prepare for a RESTORE_POINTER message.
2773 ** Save the TEMP register into the saved pointer.
2775 SCR_COPY (4),
2776 RADDR (temp),
2777 NADDR (header.savep),
2779 ** Load scratcha, because in case of a selection timeout,
2780 ** the host will expect a new value for startpos in
2781 ** the scratcha register.
2783 SCR_COPY (4),
2784 PADDR (startpos),
2785 RADDR (scratcha),
2786 #ifdef NCR_GETCC_WITHMSG
2788 ** If QUIRK_NOMSG is set, select without ATN.
2789 ** and don't send a message.
2791 SCR_FROM_REG (QU_REG),
2793 SCR_JUMP ^ IFTRUE (MASK (QUIRK_NOMSG, QUIRK_NOMSG)),
2794 PADDRH(getcc3),
2796 ** Then try to connect to the target.
2797 ** If we are reselected, special treatment
2798 ** of the current job is required before
2799 ** accepting the reselection.
2801 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2802 PADDR(badgetcc),
2804 ** Send the IDENTIFY message.
2805 ** In case of short transfer, remove ATN.
2807 SCR_MOVE_TBL ^ SCR_MSG_OUT,
2808 offsetof (struct dsb, smsg2),
2809 SCR_CLR (SCR_ATN),
2812 ** save the first byte of the message.
2814 SCR_COPY (1),
2815 RADDR (sfbr),
2816 NADDR (lastmsg),
2817 SCR_JUMP,
2818 PADDR (prepare2),
2820 #endif
2821 }/*-------------------------< GETCC3 >----------------------*/,{
2823 ** Try to connect to the target.
2824 ** If we are reselected, special treatment
2825 ** of the current job is required before
2826 ** accepting the reselection.
2828 ** Silly target won't accept a message.
2829 ** Select without ATN.
2831 SCR_SEL_TBL ^ offsetof (struct dsb, select),
2832 PADDR(badgetcc),
2834 ** Force error if selection timeout
2836 SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
2839 ** don't negotiate.
2841 SCR_JUMP,
2842 PADDR (prepare2),
2843 }/*-------------------------< ABORTTAG >-------------------*/,{
2845 ** Abort a bad reselection.
2846 ** Set the message to ABORT vs. ABORT_TAG
2848 SCR_LOAD_REG (scratcha, MSG_ABORT_TAG),
2850 SCR_JUMPR ^ IFFALSE (CARRYSET),
2852 }/*-------------------------< ABORT >----------------------*/,{
2853 SCR_LOAD_REG (scratcha, MSG_ABORT),
2855 SCR_COPY (1),
2856 RADDR (scratcha),
2857 NADDR (msgout),
2858 SCR_SET (SCR_ATN),
2860 SCR_CLR (SCR_ACK),
2863 ** and send it.
2864 ** we expect an immediate disconnect
2866 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2868 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2869 NADDR (msgout),
2870 SCR_COPY (1),
2871 RADDR (sfbr),
2872 NADDR (lastmsg),
2873 SCR_CLR (SCR_ACK|SCR_ATN),
2875 SCR_WAIT_DISC,
2877 SCR_JUMP,
2878 PADDR (start),
2879 }/*-------------------------< SNOOPTEST >-------------------*/,{
2881 ** Read the variable.
2883 SCR_COPY (4),
2884 KVAR (KVAR_NCR_CACHE),
2885 RADDR (scratcha),
2887 ** Write the variable.
2889 SCR_COPY (4),
2890 RADDR (temp),
2891 KVAR (KVAR_NCR_CACHE),
2893 ** Read back the variable.
2895 SCR_COPY (4),
2896 KVAR (KVAR_NCR_CACHE),
2897 RADDR (temp),
2898 }/*-------------------------< SNOOPEND >-------------------*/,{
2900 ** And stop.
2902 SCR_INT,
2904 }/*--------------------------------------------------------*/
2908 /*==========================================================
2911 ** Fill in #define dependent parts of the script
2914 **==========================================================
2917 static void
2918 ncr_script_fill (struct script * scr, struct scripth * scrh)
2920 int i;
2921 ncrcmd *p;
2923 p = scrh->tryloop;
2924 for (i=0; i<MAX_START; i++) {
2925 *p++ =SCR_COPY (4);
2926 *p++ =NADDR (squeue[i]);
2927 *p++ =RADDR (dsa);
2928 *p++ =SCR_CALL;
2929 *p++ =PADDR (trysel);
2931 *p++ =SCR_JUMP;
2932 *p++ =PADDRH(tryloop);
2934 assert ((char *)p == (char *)&scrh->tryloop + sizeof (scrh->tryloop));
2936 p = scr->data_in;
2938 *p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN));
2939 *p++ =PADDR (no_data);
2940 *p++ =SCR_COPY (sizeof (ticks));
2941 *p++ =(ncrcmd) KVAR (KVAR_TICKS);
2942 *p++ =NADDR (header.stamp.data);
2943 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2944 *p++ =offsetof (struct dsb, data[ 0]);
2946 for (i=1; i<MAX_SCATTER; i++) {
2947 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
2948 *p++ =PADDR (checkatn);
2949 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2950 *p++ =offsetof (struct dsb, data[i]);
2953 *p++ =SCR_CALL;
2954 *p++ =PADDR (checkatn);
2955 *p++ =SCR_JUMP;
2956 *p++ =PADDR (no_data);
2958 assert ((char *)p == (char *)&scr->data_in + sizeof (scr->data_in));
2960 p = scr->data_out;
2962 *p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT));
2963 *p++ =PADDR (no_data);
2964 *p++ =SCR_COPY (sizeof (ticks));
2965 *p++ =(ncrcmd) KVAR (KVAR_TICKS);
2966 *p++ =NADDR (header.stamp.data);
2967 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2968 *p++ =offsetof (struct dsb, data[ 0]);
2970 for (i=1; i<MAX_SCATTER; i++) {
2971 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
2972 *p++ =PADDR (dispatch);
2973 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2974 *p++ =offsetof (struct dsb, data[i]);
2977 *p++ =SCR_CALL;
2978 *p++ =PADDR (dispatch);
2979 *p++ =SCR_JUMP;
2980 *p++ =PADDR (no_data);
2982 assert ((char *)p == (char *)&scr->data_out + sizeof (scr->data_out));
2985 /*==========================================================
2988 ** Copy and rebind a script.
2991 **==========================================================
2994 static void ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
2996 ncrcmd opcode, new, old, tmp1, tmp2;
2997 ncrcmd *start, *end;
2998 int relocs, offset;
3000 start = src;
3001 end = src + len/4;
3002 offset = 0;
3004 while (src < end) {
3006 opcode = *src++;
3007 WRITESCRIPT_OFF(dst, offset, opcode);
3008 offset += 4;
3011 ** If we forget to change the length
3012 ** in struct script, a field will be
3013 ** padded with 0. This is an illegal
3014 ** command.
3017 if (opcode == 0) {
3018 kprintf ("%s: ERROR0 IN SCRIPT at %d.\n",
3019 ncr_name(np), (int) (src-start-1));
3020 DELAY (1000000);
3023 if (DEBUG_FLAGS & DEBUG_SCRIPT)
3024 kprintf ("%p: <%x>\n",
3025 (src-1), (unsigned)opcode);
3028 ** We don't have to decode ALL commands
3030 switch (opcode >> 28) {
3032 case 0xc:
3034 ** COPY has TWO arguments.
3036 relocs = 2;
3037 tmp1 = src[0];
3038 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3039 tmp1 = 0;
3040 tmp2 = src[1];
3041 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3042 tmp2 = 0;
3043 if ((tmp1 ^ tmp2) & 3) {
3044 kprintf ("%s: ERROR1 IN SCRIPT at %d.\n",
3045 ncr_name(np), (int) (src-start-1));
3046 DELAY (1000000);
3049 ** If PREFETCH feature not enabled, remove
3050 ** the NO FLUSH bit if present.
3052 if ((opcode & SCR_NO_FLUSH) && !(np->features&FE_PFEN))
3053 WRITESCRIPT_OFF(dst, offset - 4,
3054 (opcode & ~SCR_NO_FLUSH));
3055 break;
3057 case 0x0:
3059 ** MOVE (absolute address)
3061 relocs = 1;
3062 break;
3064 case 0x8:
3066 ** JUMP / CALL
3067 ** dont't relocate if relative :-)
3069 if (opcode & 0x00800000)
3070 relocs = 0;
3071 else
3072 relocs = 1;
3073 break;
3075 case 0x4:
3076 case 0x5:
3077 case 0x6:
3078 case 0x7:
3079 relocs = 1;
3080 break;
3082 default:
3083 relocs = 0;
3084 break;
3087 if (relocs) {
3088 while (relocs--) {
3089 old = *src++;
3091 switch (old & RELOC_MASK) {
3092 case RELOC_REGISTER:
3093 new = (old & ~RELOC_MASK) + rman_get_start(np->reg_res);
3094 break;
3095 case RELOC_LABEL:
3096 new = (old & ~RELOC_MASK) + np->p_script;
3097 break;
3098 case RELOC_LABELH:
3099 new = (old & ~RELOC_MASK) + np->p_scripth;
3100 break;
3101 case RELOC_SOFTC:
3102 new = (old & ~RELOC_MASK) + vtophys(np);
3103 break;
3104 case RELOC_KVAR:
3105 if (((old & ~RELOC_MASK) <
3106 SCRIPT_KVAR_FIRST) ||
3107 ((old & ~RELOC_MASK) >
3108 SCRIPT_KVAR_LAST))
3109 panic("ncr KVAR out of range");
3110 new = vtophys(script_kvars[old &
3111 ~RELOC_MASK]);
3112 break;
3113 case 0:
3114 /* Don't relocate a 0 address. */
3115 if (old == 0) {
3116 new = old;
3117 break;
3119 /* fall through */
3120 default:
3121 panic("ncr_script_copy_and_bind: weird relocation %x @ %d", old, (int)(src - start));
3122 break;
3125 WRITESCRIPT_OFF(dst, offset, new);
3126 offset += 4;
3128 } else {
3129 WRITESCRIPT_OFF(dst, offset, *src++);
3130 offset += 4;
3136 /*==========================================================
3139 ** Auto configuration.
3142 **==========================================================
3145 #if 0
3146 /*----------------------------------------------------------
3148 ** Reduce the transfer length to the max value
3149 ** we can transfer safely.
3151 ** Reading a block greater then MAX_SIZE from the
3152 ** raw (character) device exercises a memory leak
3153 ** in the vm subsystem. This is common to ALL devices.
3154 ** We have submitted a description of this bug to
3155 ** <FreeBSD-bugs@freefall.cdrom.com>.
3156 ** It should be fixed in the current release.
3158 **----------------------------------------------------------
3161 void ncr_min_phys (struct buf *bp)
3163 if ((unsigned long)bp->b_bcount > MAX_SIZE) bp->b_bcount = MAX_SIZE;
3166 #endif
3168 #if 0
3169 /*----------------------------------------------------------
3171 ** Maximal number of outstanding requests per target.
3173 **----------------------------------------------------------
3176 u_int32_t ncr_info (int unit)
3178 return (1); /* may be changed later */
3181 #endif
3183 /*----------------------------------------------------------
3185 ** NCR chip devices table and chip look up function.
3186 ** Features bit are defined in ncrreg.h. Is it the
3187 ** right place?
3189 **----------------------------------------------------------
3191 typedef struct {
3192 unsigned long device_id;
3193 unsigned short minrevid;
3194 char *name;
3195 unsigned char maxburst;
3196 unsigned char maxoffs;
3197 unsigned char clock_divn;
3198 unsigned int features;
3199 } ncr_chip;
3201 static ncr_chip ncr_chip_table[] = {
3202 {NCR_810_ID, 0x00, "ncr 53c810 fast10 scsi", 4, 8, 4,
3203 FE_ERL}
3205 {NCR_810_ID, 0x10, "ncr 53c810a fast10 scsi", 4, 8, 4,
3206 FE_ERL|FE_LDSTR|FE_PFEN|FE_BOF}
3208 {NCR_815_ID, 0x00, "ncr 53c815 fast10 scsi", 4, 8, 4,
3209 FE_ERL|FE_BOF}
3211 {NCR_820_ID, 0x00, "ncr 53c820 fast10 wide scsi", 4, 8, 4,
3212 FE_WIDE|FE_ERL}
3214 {NCR_825_ID, 0x00, "ncr 53c825 fast10 wide scsi", 4, 8, 4,
3215 FE_WIDE|FE_ERL|FE_BOF}
3217 {NCR_825_ID, 0x10, "ncr 53c825a fast10 wide scsi", 7, 8, 4,
3218 FE_WIDE|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3220 {NCR_860_ID, 0x00, "ncr 53c860 fast20 scsi", 4, 8, 5,
3221 FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_LDSTR|FE_PFEN}
3223 {NCR_875_ID, 0x00, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
3224 FE_WIDE|FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3226 {NCR_875_ID, 0x02, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
3227 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3229 {NCR_875_ID2, 0x00, "ncr 53c875j fast20 wide scsi", 7, 16, 5,
3230 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3232 {NCR_885_ID, 0x00, "ncr 53c885 fast20 wide scsi", 7, 16, 5,
3233 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3235 {NCR_895_ID, 0x00, "ncr 53c895 fast40 wide scsi", 7, 31, 7,
3236 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3238 {NCR_896_ID, 0x00, "ncr 53c896 fast40 wide scsi", 7, 31, 7,
3239 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3241 {NCR_895A_ID, 0x00, "ncr 53c895a fast40 wide scsi", 7, 31, 7,
3242 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3244 {NCR_1510D_ID, 0x00, "ncr 53c1510d fast40 wide scsi", 7, 31, 7,
3245 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3248 static int ncr_chip_lookup(u_long device_id, u_char revision_id)
3250 int i, found;
3252 found = -1;
3253 for (i = 0; i < NELEM(ncr_chip_table); i++) {
3254 if (device_id == ncr_chip_table[i].device_id &&
3255 ncr_chip_table[i].minrevid <= revision_id) {
3256 if (found < 0 ||
3257 ncr_chip_table[found].minrevid
3258 < ncr_chip_table[i].minrevid) {
3259 found = i;
3263 return found;
3266 /*----------------------------------------------------------
3268 ** Probe the hostadapter.
3270 **----------------------------------------------------------
3275 static int ncr_probe (device_t dev)
3277 int i;
3279 i = ncr_chip_lookup(pci_get_devid(dev), pci_get_revid(dev));
3280 if (i >= 0) {
3281 device_set_desc(dev, ncr_chip_table[i].name);
3282 return (-1000); /* Allows to use both ncr and sym */
3285 return (ENXIO);
3290 /*==========================================================
3292 ** NCR chip clock divisor table.
3293 ** Divisors are multiplied by 10,000,000 in order to make
3294 ** calculations more simple.
3296 **==========================================================
3299 #define _5M 5000000
3300 static u_long div_10M[] =
3301 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3303 /*===============================================================
3305 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3306 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3307 ** We use log base 2 (burst length) as internal code, with
3308 ** value 0 meaning "burst disabled".
3310 **===============================================================
3314 * Burst length from burst code.
3316 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3319 * Burst code from io register bits.
3321 #define burst_code(dmode, ctest4, ctest5) \
3322 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
3325 * Set initial io register bits from burst code.
3327 static void
3328 ncr_init_burst(ncb_p np, u_char bc)
3330 np->rv_ctest4 &= ~0x80;
3331 np->rv_dmode &= ~(0x3 << 6);
3332 np->rv_ctest5 &= ~0x4;
3334 if (!bc) {
3335 np->rv_ctest4 |= 0x80;
3337 else {
3338 --bc;
3339 np->rv_dmode |= ((bc & 0x3) << 6);
3340 np->rv_ctest5 |= (bc & 0x4);
3344 /*==========================================================
3347 ** Auto configuration: attach and init a host adapter.
3350 **==========================================================
3354 static int
3355 ncr_attach (device_t dev)
3357 ncb_p np = (struct ncb*) device_get_softc(dev);
3358 u_char rev = 0;
3359 u_long period;
3360 int i, rid;
3361 u_int8_t usrsync;
3362 u_int8_t usrwide;
3363 struct cam_devq *devq;
3366 ** allocate and initialize structures.
3369 np->unit = device_get_unit(dev);
3372 ** Try to map the controller chip to
3373 ** virtual and physical memory.
3376 np->reg_rid = 0x14;
3377 np->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &np->reg_rid,
3378 0, ~0, 1, RF_ACTIVE);
3379 if (!np->reg_res) {
3380 device_printf(dev, "could not map memory\n");
3381 return ENXIO;
3385 ** Make the controller's registers available.
3386 ** Now the INB INW INL OUTB OUTW OUTL macros
3387 ** can be used safely.
3390 np->bst = rman_get_bustag(np->reg_res);
3391 np->bsh = rman_get_bushandle(np->reg_res);
3394 #ifdef NCR_IOMAPPED
3396 ** Try to map the controller chip into iospace.
3399 if (!pci_map_port (config_id, 0x10, &np->port))
3400 return;
3401 #endif
3405 ** Save some controller register default values
3408 np->rv_scntl3 = INB(nc_scntl3) & 0x77;
3409 np->rv_dmode = INB(nc_dmode) & 0xce;
3410 np->rv_dcntl = INB(nc_dcntl) & 0xa9;
3411 np->rv_ctest3 = INB(nc_ctest3) & 0x01;
3412 np->rv_ctest4 = INB(nc_ctest4) & 0x88;
3413 np->rv_ctest5 = INB(nc_ctest5) & 0x24;
3414 np->rv_gpcntl = INB(nc_gpcntl);
3415 np->rv_stest2 = INB(nc_stest2) & 0x20;
3417 if (bootverbose >= 2) {
3418 kprintf ("\tBIOS values: SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
3419 np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
3420 kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
3421 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
3424 np->rv_dcntl |= NOCOM;
3427 ** Do chip dependent initialization.
3430 rev = pci_get_revid(dev);
3433 ** Get chip features from chips table.
3435 i = ncr_chip_lookup(pci_get_devid(dev), rev);
3437 if (i >= 0) {
3438 np->maxburst = ncr_chip_table[i].maxburst;
3439 np->maxoffs = ncr_chip_table[i].maxoffs;
3440 np->clock_divn = ncr_chip_table[i].clock_divn;
3441 np->features = ncr_chip_table[i].features;
3442 } else { /* Should'nt happen if probe() is ok */
3443 np->maxburst = 4;
3444 np->maxoffs = 8;
3445 np->clock_divn = 4;
3446 np->features = FE_ERL;
3449 np->maxwide = np->features & FE_WIDE ? 1 : 0;
3450 np->clock_khz = np->features & FE_CLK80 ? 80000 : 40000;
3451 if (np->features & FE_QUAD) np->multiplier = 4;
3452 else if (np->features & FE_DBLR) np->multiplier = 2;
3453 else np->multiplier = 1;
3456 ** Get the frequency of the chip's clock.
3457 ** Find the right value for scntl3.
3459 if (np->features & (FE_ULTRA|FE_ULTRA2))
3460 ncr_getclock(np, np->multiplier);
3462 #ifdef NCR_TEKRAM_EEPROM
3463 if (bootverbose) {
3464 kprintf ("%s: Tekram EEPROM read %s\n",
3465 ncr_name(np),
3466 read_tekram_eeprom (np, NULL) ?
3467 "succeeded" : "failed");
3469 #endif /* NCR_TEKRAM_EEPROM */
3472 * If scntl3 != 0, we assume BIOS is present.
3474 if (np->rv_scntl3)
3475 np->features |= FE_BIOS;
3478 * Divisor to be used for async (timer pre-scaler).
3480 i = np->clock_divn - 1;
3481 while (i >= 0) {
3482 --i;
3483 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
3484 ++i;
3485 break;
3488 np->rv_scntl3 = i+1;
3491 * Minimum synchronous period factor supported by the chip.
3492 * Btw, 'period' is in tenths of nanoseconds.
3495 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
3496 if (period <= 250) np->minsync = 10;
3497 else if (period <= 303) np->minsync = 11;
3498 else if (period <= 500) np->minsync = 12;
3499 else np->minsync = (period + 40 - 1) / 40;
3502 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3505 if (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
3506 np->minsync = 25;
3507 else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
3508 np->minsync = 12;
3511 * Maximum synchronous period factor supported by the chip.
3514 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
3515 np->maxsync = period > 2540 ? 254 : period / 10;
3518 * Now, some features available with Symbios compatible boards.
3519 * LED support through GPIO0 and DIFF support.
3522 #ifdef SCSI_NCR_SYMBIOS_COMPAT
3523 if (!(np->rv_gpcntl & 0x01))
3524 np->features |= FE_LED0;
3525 #if 0 /* Not safe enough without NVRAM support or user settable option */
3526 if (!(INB(nc_gpreg) & 0x08))
3527 np->features |= FE_DIFF;
3528 #endif
3529 #endif /* SCSI_NCR_SYMBIOS_COMPAT */
3532 * Prepare initial IO registers settings.
3533 * Trust BIOS only if we believe we have one and if we want to.
3535 #ifdef SCSI_NCR_TRUST_BIOS
3536 if (!(np->features & FE_BIOS)) {
3537 #else
3538 if (1) {
3539 #endif
3540 np->rv_dmode = 0;
3541 np->rv_dcntl = NOCOM;
3542 np->rv_ctest3 = 0;
3543 np->rv_ctest4 = MPEE;
3544 np->rv_ctest5 = 0;
3545 np->rv_stest2 = 0;
3547 if (np->features & FE_ERL)
3548 np->rv_dmode |= ERL; /* Enable Read Line */
3549 if (np->features & FE_BOF)
3550 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
3551 if (np->features & FE_ERMP)
3552 np->rv_dmode |= ERMP; /* Enable Read Multiple */
3553 if (np->features & FE_CLSE)
3554 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
3555 if (np->features & FE_WRIE)
3556 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
3557 if (np->features & FE_PFEN)
3558 np->rv_dcntl |= PFEN; /* Prefetch Enable */
3559 if (np->features & FE_DFS)
3560 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
3561 if (np->features & FE_DIFF)
3562 np->rv_stest2 |= 0x20; /* Differential mode */
3563 ncr_init_burst(np, np->maxburst); /* Max dwords burst length */
3564 } else {
3565 np->maxburst =
3566 burst_code(np->rv_dmode, np->rv_ctest4, np->rv_ctest5);
3570 ** Get on-chip SRAM address, if supported
3572 if ((np->features & FE_RAM) && sizeof(struct script) <= 4096) {
3573 np->sram_rid = 0x18;
3574 np->sram_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
3575 &np->sram_rid,
3576 0, ~0, 1, RF_ACTIVE);
3580 ** Allocate structure for script relocation.
3582 if (np->sram_res != NULL) {
3583 np->script = NULL;
3584 np->p_script = rman_get_start(np->sram_res);
3585 np->bst2 = rman_get_bustag(np->sram_res);
3586 np->bsh2 = rman_get_bushandle(np->sram_res);
3587 } else if (sizeof (struct script) > PAGE_SIZE) {
3588 np->script = (struct script*) kmem_alloc_contig
3589 (round_page(sizeof (struct script)),
3590 0, 0xffffffff, PAGE_SIZE);
3591 } else {
3592 np->script = (struct script *)
3593 kmalloc (sizeof (struct script), M_DEVBUF, M_WAITOK);
3596 /* XXX JGibbs - Use contigmalloc */
3597 if (sizeof (struct scripth) > PAGE_SIZE) {
3598 np->scripth = (struct scripth*) kmem_alloc_contig
3599 (round_page(sizeof (struct scripth)),
3600 0, 0xffffffff, PAGE_SIZE);
3601 } else
3603 np->scripth = (struct scripth *)
3604 kmalloc (sizeof (struct scripth), M_DEVBUF, M_WAITOK);
3607 #ifdef SCSI_NCR_PCI_CONFIG_FIXUP
3609 ** If cache line size is enabled, check PCI config space and
3610 ** try to fix it up if necessary.
3612 #ifdef PCIR_CACHELNSZ /* To be sure that new PCI stuff is present */
3614 u_char cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3615 u_short command = pci_read_config(dev, PCIR_COMMAND, 2);
3617 if (!cachelnsz) {
3618 cachelnsz = 8;
3619 kprintf("%s: setting PCI cache line size register to %d.\n",
3620 ncr_name(np), (int)cachelnsz);
3621 pci_write_config(dev, PCIR_CACHELNSZ, cachelnsz, 1);
3624 if (!(command & (1<<4))) {
3625 command |= (1<<4);
3626 kprintf("%s: setting PCI command write and invalidate.\n",
3627 ncr_name(np));
3628 pci_write_config(dev, PCIR_COMMAND, command, 2);
3631 #endif /* PCIR_CACHELNSZ */
3633 #endif /* SCSI_NCR_PCI_CONFIG_FIXUP */
3635 /* Initialize per-target user settings */
3636 usrsync = 0;
3637 if (SCSI_NCR_DFLT_SYNC) {
3638 usrsync = SCSI_NCR_DFLT_SYNC;
3639 if (usrsync > np->maxsync)
3640 usrsync = np->maxsync;
3641 if (usrsync < np->minsync)
3642 usrsync = np->minsync;
3645 usrwide = (SCSI_NCR_MAX_WIDE);
3646 if (usrwide > np->maxwide) usrwide=np->maxwide;
3648 for (i=0;i<MAX_TARGET;i++) {
3649 tcb_p tp = &np->target[i];
3651 tp->tinfo.user.period = usrsync;
3652 tp->tinfo.user.offset = usrsync != 0 ? np->maxoffs : 0;
3653 tp->tinfo.user.width = usrwide;
3654 tp->tinfo.disc_tag = NCR_CUR_DISCENB
3655 | NCR_CUR_TAGENB
3656 | NCR_USR_DISCENB
3657 | NCR_USR_TAGENB;
3661 ** Bells and whistles ;-)
3663 if (bootverbose)
3664 kprintf("%s: minsync=%d, maxsync=%d, maxoffs=%d, %d dwords burst, %s dma fifo\n",
3665 ncr_name(np), np->minsync, np->maxsync, np->maxoffs,
3666 burst_length(np->maxburst),
3667 (np->rv_ctest5 & DFS) ? "large" : "normal");
3670 ** Print some complementary information that can be helpfull.
3672 if (bootverbose)
3673 kprintf("%s: %s, %s IRQ driver%s\n",
3674 ncr_name(np),
3675 np->rv_stest2 & 0x20 ? "differential" : "single-ended",
3676 np->rv_dcntl & IRQM ? "totem pole" : "open drain",
3677 np->sram_res ? ", using on-chip SRAM" : "");
3680 ** Patch scripts to physical addresses
3682 ncr_script_fill (&script0, &scripth0);
3684 if (np->script)
3685 np->p_script = vtophys(np->script);
3686 np->p_scripth = vtophys(np->scripth);
3688 ncr_script_copy_and_bind (np, (ncrcmd *) &script0,
3689 (ncrcmd *) np->script, sizeof(struct script));
3691 ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0,
3692 (ncrcmd *) np->scripth, sizeof(struct scripth));
3695 ** Patch the script for LED support.
3698 if (np->features & FE_LED0) {
3699 WRITESCRIPT(reselect[0], SCR_REG_REG(gpreg, SCR_OR, 0x01));
3700 WRITESCRIPT(reselect1[0], SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3701 WRITESCRIPT(reselect2[0], SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3705 ** init data structure
3708 np->jump_tcb.l_cmd = SCR_JUMP;
3709 np->jump_tcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
3712 ** Get SCSI addr of host adapter (set by bios?).
3715 np->myaddr = INB(nc_scid) & 0x07;
3716 if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
3718 #ifdef NCR_DUMP_REG
3720 ** Log the initial register contents
3723 int reg;
3724 for (reg=0; reg<256; reg+=4) {
3725 if (reg%16==0) kprintf ("reg[%2x]", reg);
3726 kprintf (" %08x", (int)pci_conf_read (config_id, reg));
3727 if (reg%16==12) kprintf ("\n");
3730 #endif /* NCR_DUMP_REG */
3733 ** Reset chip.
3736 OUTB (nc_istat, SRST);
3737 DELAY (1000);
3738 OUTB (nc_istat, 0 );
3742 ** Now check the cache handling of the pci chipset.
3745 if (ncr_snooptest (np)) {
3746 kprintf ("CACHE INCORRECTLY CONFIGURED.\n");
3747 return EINVAL;
3751 ** Install the interrupt handler.
3754 rid = 0;
3755 np->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
3756 RF_SHAREABLE | RF_ACTIVE);
3757 if (np->irq_res == NULL) {
3758 device_printf(dev,
3759 "interruptless mode: reduced performance.\n");
3760 } else {
3761 bus_setup_intr(dev, np->irq_res, 0,
3762 ncr_intr, np, &np->irq_handle, NULL);
3766 ** Create the device queue. We only allow MAX_START-1 concurrent
3767 ** transactions so we can be sure to have one element free in our
3768 ** start queue to reset to the idle loop.
3770 devq = cam_simq_alloc(MAX_START - 1);
3771 if (devq == NULL)
3772 return ENOMEM;
3775 ** Now tell the generic SCSI layer
3776 ** about our bus.
3778 np->sim = cam_sim_alloc(ncr_action, ncr_poll, "ncr", np, np->unit,
3779 &sim_mplock, 1, MAX_TAGS, devq);
3780 cam_simq_release(devq);
3781 if (np->sim == NULL)
3782 return ENOMEM;
3785 if (xpt_bus_register(np->sim, 0) != CAM_SUCCESS) {
3786 cam_sim_free(np->sim);
3787 return ENOMEM;
3790 if (xpt_create_path(&np->path, /*periph*/NULL,
3791 cam_sim_path(np->sim), CAM_TARGET_WILDCARD,
3792 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3793 xpt_bus_deregister(cam_sim_path(np->sim));
3794 cam_sim_free(np->sim);
3795 return ENOMEM;
3799 ** start the timeout daemon
3801 callout_init(&np->timeout_ch);
3802 ncr_timeout (np);
3803 np->lasttime=0;
3805 return 0;
3808 /*==========================================================
3811 ** Process pending device interrupts.
3814 **==========================================================
3817 static void
3818 ncr_intr(void *vnp)
3820 ncb_p np = vnp;
3821 crit_enter();
3823 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("[");
3825 if (INB(nc_istat) & (INTF|SIP|DIP)) {
3827 ** Repeat until no outstanding ints
3829 do {
3830 ncr_exception (np);
3831 } while (INB(nc_istat) & (INTF|SIP|DIP));
3833 np->ticks = 100;
3836 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("]\n");
3838 crit_exit();
3841 /*==========================================================
3844 ** Start execution of a SCSI command.
3845 ** This is called from the generic SCSI driver.
3848 **==========================================================
3851 static void
3852 ncr_action (struct cam_sim *sim, union ccb *ccb)
3854 ncb_p np;
3856 np = (ncb_p) cam_sim_softc(sim);
3858 switch (ccb->ccb_h.func_code) {
3859 /* Common cases first */
3860 case XPT_SCSI_IO: /* Execute the requested I/O operation */
3862 nccb_p cp;
3863 lcb_p lp;
3864 tcb_p tp;
3865 struct ccb_scsiio *csio;
3866 u_int8_t *msgptr;
3867 u_int msglen;
3868 u_int msglen2;
3869 int segments;
3870 u_int8_t nego;
3871 u_int8_t idmsg;
3872 int qidx;
3874 tp = &np->target[ccb->ccb_h.target_id];
3875 csio = &ccb->csio;
3877 crit_enter();
3880 * Last time we need to check if this CCB needs to
3881 * be aborted.
3883 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3884 xpt_done(ccb);
3885 crit_exit();
3886 return;
3888 ccb->ccb_h.status |= CAM_SIM_QUEUED;
3890 /*---------------------------------------------------
3892 ** Assign an nccb / bind ccb
3894 **----------------------------------------------------
3896 cp = ncr_get_nccb (np, ccb->ccb_h.target_id,
3897 ccb->ccb_h.target_lun);
3898 if (cp == NULL) {
3899 /* XXX JGibbs - Freeze SIMQ */
3900 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3901 xpt_done(ccb);
3902 crit_exit();
3903 return;
3906 cp->ccb = ccb;
3908 /*---------------------------------------------------
3910 ** timestamp
3912 **----------------------------------------------------
3915 ** XXX JGibbs - Isn't this expensive
3916 ** enough to be conditionalized??
3919 bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
3920 cp->phys.header.stamp.start = ticks;
3922 nego = 0;
3923 if (tp->nego_cp == NULL) {
3925 if (tp->tinfo.current.width
3926 != tp->tinfo.goal.width) {
3927 tp->nego_cp = cp;
3928 nego = NS_WIDE;
3929 } else if ((tp->tinfo.current.period
3930 != tp->tinfo.goal.period)
3931 || (tp->tinfo.current.offset
3932 != tp->tinfo.goal.offset)) {
3933 tp->nego_cp = cp;
3934 nego = NS_SYNC;
3938 /*---------------------------------------------------
3940 ** choose a new tag ...
3942 **----------------------------------------------------
3944 lp = tp->lp[ccb->ccb_h.target_lun];
3946 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
3947 && (ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3948 && (nego == 0)) {
3950 ** assign a tag to this nccb
3952 while (!cp->tag) {
3953 nccb_p cp2 = lp->next_nccb;
3954 lp->lasttag = lp->lasttag % 255 + 1;
3955 while (cp2 && cp2->tag != lp->lasttag)
3956 cp2 = cp2->next_nccb;
3957 if (cp2) continue;
3958 cp->tag=lp->lasttag;
3959 if (DEBUG_FLAGS & DEBUG_TAGS) {
3960 PRINT_ADDR(ccb);
3961 kprintf ("using tag #%d.\n", cp->tag);
3964 } else {
3965 cp->tag=0;
3968 /*----------------------------------------------------
3970 ** Build the identify / tag / sdtr message
3972 **----------------------------------------------------
3974 idmsg = MSG_IDENTIFYFLAG | ccb->ccb_h.target_lun;
3975 if (tp->tinfo.disc_tag & NCR_CUR_DISCENB)
3976 idmsg |= MSG_IDENTIFY_DISCFLAG;
3978 msgptr = cp->scsi_smsg;
3979 msglen = 0;
3980 msgptr[msglen++] = idmsg;
3982 if (cp->tag) {
3983 msgptr[msglen++] = ccb->csio.tag_action;
3984 msgptr[msglen++] = cp->tag;
3987 switch (nego) {
3988 case NS_SYNC:
3989 msgptr[msglen++] = MSG_EXTENDED;
3990 msgptr[msglen++] = MSG_EXT_SDTR_LEN;
3991 msgptr[msglen++] = MSG_EXT_SDTR;
3992 msgptr[msglen++] = tp->tinfo.goal.period;
3993 msgptr[msglen++] = tp->tinfo.goal.offset;
3994 if (DEBUG_FLAGS & DEBUG_NEGO) {
3995 PRINT_ADDR(ccb);
3996 kprintf ("sync msgout: ");
3997 ncr_show_msg (&cp->scsi_smsg [msglen-5]);
3998 kprintf (".\n");
4000 break;
4001 case NS_WIDE:
4002 msgptr[msglen++] = MSG_EXTENDED;
4003 msgptr[msglen++] = MSG_EXT_WDTR_LEN;
4004 msgptr[msglen++] = MSG_EXT_WDTR;
4005 msgptr[msglen++] = tp->tinfo.goal.width;
4006 if (DEBUG_FLAGS & DEBUG_NEGO) {
4007 PRINT_ADDR(ccb);
4008 kprintf ("wide msgout: ");
4009 ncr_show_msg (&cp->scsi_smsg [msglen-4]);
4010 kprintf (".\n");
4012 break;
4015 /*----------------------------------------------------
4017 ** Build the identify message for getcc.
4019 **----------------------------------------------------
4022 cp->scsi_smsg2 [0] = idmsg;
4023 msglen2 = 1;
4025 /*----------------------------------------------------
4027 ** Build the data descriptors
4029 **----------------------------------------------------
4032 /* XXX JGibbs - Handle other types of I/O */
4033 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
4034 segments = ncr_scatter(&cp->phys,
4035 (vm_offset_t)csio->data_ptr,
4036 (vm_size_t)csio->dxfer_len);
4038 if (segments < 0) {
4039 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
4040 ncr_free_nccb(np, cp);
4041 crit_exit();
4042 xpt_done(ccb);
4043 return;
4045 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4046 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_in);
4047 cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
4048 } else { /* CAM_DIR_OUT */
4049 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_out);
4050 cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
4052 } else {
4053 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, no_data);
4054 cp->phys.header.goalp = cp->phys.header.savep;
4057 cp->phys.header.lastp = cp->phys.header.savep;
4060 /*----------------------------------------------------
4062 ** fill in nccb
4064 **----------------------------------------------------
4067 ** physical -> virtual backlink
4068 ** Generic SCSI command
4070 cp->phys.header.cp = cp;
4072 ** Startqueue
4074 cp->phys.header.launch.l_paddr = NCB_SCRIPT_PHYS (np, select);
4075 cp->phys.header.launch.l_cmd = SCR_JUMP;
4077 ** select
4079 cp->phys.select.sel_id = ccb->ccb_h.target_id;
4080 cp->phys.select.sel_scntl3 = tp->tinfo.wval;
4081 cp->phys.select.sel_sxfer = tp->tinfo.sval;
4083 ** message
4085 cp->phys.smsg.addr = CCB_PHYS (cp, scsi_smsg);
4086 cp->phys.smsg.size = msglen;
4088 cp->phys.smsg2.addr = CCB_PHYS (cp, scsi_smsg2);
4089 cp->phys.smsg2.size = msglen2;
4091 ** command
4093 /* XXX JGibbs - Support other command types */
4094 cp->phys.cmd.addr = vtophys (csio->cdb_io.cdb_bytes);
4095 cp->phys.cmd.size = csio->cdb_len;
4097 ** sense command
4099 cp->phys.scmd.addr = CCB_PHYS (cp, sensecmd);
4100 cp->phys.scmd.size = 6;
4102 ** patch requested size into sense command
4104 cp->sensecmd[0] = 0x03;
4105 cp->sensecmd[1] = ccb->ccb_h.target_lun << 5;
4106 cp->sensecmd[4] = csio->sense_len;
4108 ** sense data
4110 cp->phys.sense.addr = vtophys (&csio->sense_data);
4111 cp->phys.sense.size = csio->sense_len;
4113 ** status
4115 cp->actualquirks = QUIRK_NOMSG;
4116 cp->host_status = nego ? HS_NEGOTIATE : HS_BUSY;
4117 cp->s_status = SCSI_STATUS_ILLEGAL;
4118 cp->parity_status = 0;
4120 cp->xerr_status = XE_OK;
4121 cp->sync_status = tp->tinfo.sval;
4122 cp->nego_status = nego;
4123 cp->wide_status = tp->tinfo.wval;
4125 /*----------------------------------------------------
4127 ** Critical region: start this job.
4129 **----------------------------------------------------
4133 ** reselect pattern and activate this job.
4136 cp->jump_nccb.l_cmd = (SCR_JUMP ^ IFFALSE (DATA (cp->tag)));
4137 cp->tlimit = time_uptime
4138 + ccb->ccb_h.timeout / 1000 + 2;
4139 cp->magic = CCB_MAGIC;
4142 ** insert into start queue.
4145 qidx = np->squeueput + 1;
4146 if (qidx >= MAX_START)
4147 qidx = 0;
4148 np->squeue [qidx ] = NCB_SCRIPT_PHYS (np, idle);
4149 np->squeue [np->squeueput] = CCB_PHYS (cp, phys);
4150 np->squeueput = qidx;
4152 if(DEBUG_FLAGS & DEBUG_QUEUE)
4153 kprintf("%s: queuepos=%d tryoffset=%d.\n",
4154 ncr_name (np), np->squeueput,
4155 (unsigned)(READSCRIPT(startpos[0]) -
4156 (NCB_SCRIPTH_PHYS (np, tryloop))));
4159 ** Script processor may be waiting for reselect.
4160 ** Wake it up.
4162 OUTB (nc_istat, SIGP);
4165 ** and reenable interrupts
4167 crit_exit();
4168 break;
4170 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
4171 case XPT_EN_LUN: /* Enable LUN as a target */
4172 case XPT_TARGET_IO: /* Execute target I/O request */
4173 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
4174 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
4175 case XPT_ABORT: /* Abort the specified CCB */
4176 /* XXX Implement */
4177 ccb->ccb_h.status = CAM_REQ_INVALID;
4178 xpt_done(ccb);
4179 break;
4180 case XPT_SET_TRAN_SETTINGS:
4182 struct ccb_trans_settings *cts = &ccb->cts;
4183 tcb_p tp;
4184 u_int update_type;
4185 struct ccb_trans_settings_scsi *scsi =
4186 &cts->proto_specific.scsi;
4187 struct ccb_trans_settings_spi *spi =
4188 &cts->xport_specific.spi;
4190 update_type = 0;
4191 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
4192 update_type |= NCR_TRANS_GOAL;
4193 if (cts->type == CTS_TYPE_USER_SETTINGS)
4194 update_type |= NCR_TRANS_USER;
4196 crit_enter();
4197 tp = &np->target[ccb->ccb_h.target_id];
4198 /* Tag and disc enables */
4199 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4200 if (update_type & NCR_TRANS_GOAL) {
4201 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4202 tp->tinfo.disc_tag |= NCR_CUR_DISCENB;
4203 else
4204 tp->tinfo.disc_tag &= ~NCR_CUR_DISCENB;
4207 if (update_type & NCR_TRANS_USER) {
4208 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4209 tp->tinfo.disc_tag |= NCR_USR_DISCENB;
4210 else
4211 tp->tinfo.disc_tag &= ~NCR_USR_DISCENB;
4216 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4217 if (update_type & NCR_TRANS_GOAL) {
4218 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4219 tp->tinfo.disc_tag |= NCR_CUR_TAGENB;
4220 else
4221 tp->tinfo.disc_tag &= ~NCR_CUR_TAGENB;
4224 if (update_type & NCR_TRANS_USER) {
4225 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4226 tp->tinfo.disc_tag |= NCR_USR_TAGENB;
4227 else
4228 tp->tinfo.disc_tag &= ~NCR_USR_TAGENB;
4232 /* Filter bus width and sync negotiation settings */
4233 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4234 if (spi->bus_width > np->maxwide)
4235 spi->bus_width = np->maxwide;
4238 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
4239 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
4240 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
4241 if (spi->sync_period != 0
4242 && (spi->sync_period < np->minsync))
4243 spi->sync_period = np->minsync;
4245 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
4246 if (spi->sync_offset == 0)
4247 spi->sync_period = 0;
4248 if (spi->sync_offset > np->maxoffs)
4249 spi->sync_offset = np->maxoffs;
4252 if ((update_type & NCR_TRANS_USER) != 0) {
4253 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
4254 tp->tinfo.user.period = spi->sync_period;
4255 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
4256 tp->tinfo.user.offset = spi->sync_offset;
4257 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
4258 tp->tinfo.user.width = spi->bus_width;
4260 if ((update_type & NCR_TRANS_GOAL) != 0) {
4261 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
4262 tp->tinfo.goal.period = spi->sync_period;
4264 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
4265 tp->tinfo.goal.offset = spi->sync_offset;
4267 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
4268 tp->tinfo.goal.width = spi->bus_width;
4270 crit_exit();
4271 ccb->ccb_h.status = CAM_REQ_CMP;
4272 xpt_done(ccb);
4273 break;
4275 case XPT_GET_TRAN_SETTINGS:
4276 /* Get default/user set transfer settings for the target */
4278 struct ccb_trans_settings *cts = &ccb->cts;
4279 struct ncr_transinfo *tinfo;
4280 tcb_p tp = &np->target[ccb->ccb_h.target_id];
4281 struct ccb_trans_settings_scsi *scsi =
4282 &cts->proto_specific.scsi;
4283 struct ccb_trans_settings_spi *spi =
4284 &cts->xport_specific.spi;
4286 cts->protocol = PROTO_SCSI;
4287 cts->protocol_version = SCSI_REV_2;
4288 cts->transport = XPORT_SPI;
4289 cts->transport_version = 2;
4291 crit_enter();
4292 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
4293 tinfo = &tp->tinfo.current;
4294 if (tp->tinfo.disc_tag & NCR_CUR_DISCENB)
4295 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4296 else
4297 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
4299 if (tp->tinfo.disc_tag & NCR_CUR_TAGENB)
4300 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4301 else
4302 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
4303 } else {
4304 tinfo = &tp->tinfo.user;
4305 if (tp->tinfo.disc_tag & NCR_USR_DISCENB)
4306 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4307 else
4308 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
4310 if (tp->tinfo.disc_tag & NCR_USR_TAGENB)
4311 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4312 else
4313 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
4316 spi->sync_period = tinfo->period;
4317 spi->sync_offset = tinfo->offset;
4318 spi->bus_width = tinfo->width;
4320 crit_exit();
4321 spi->valid = CTS_SPI_VALID_SYNC_RATE
4322 | CTS_SPI_VALID_SYNC_OFFSET
4323 | CTS_SPI_VALID_BUS_WIDTH
4324 | CTS_SPI_VALID_DISC;
4325 scsi->valid = CTS_SCSI_VALID_TQ;
4327 ccb->ccb_h.status = CAM_REQ_CMP;
4328 xpt_done(ccb);
4329 break;
4331 case XPT_CALC_GEOMETRY:
4333 struct ccb_calc_geometry *ccg;
4334 u_int32_t size_mb;
4335 u_int32_t secs_per_cylinder;
4336 int extended;
4338 /* XXX JGibbs - I'm sure the NCR uses a different strategy,
4339 * but it should be able to deal with Adaptec
4340 * geometry too.
4342 extended = 1;
4343 ccg = &ccb->ccg;
4344 size_mb = ccg->volume_size
4345 / ((1024L * 1024L) / ccg->block_size);
4347 if (size_mb > 1024 && extended) {
4348 ccg->heads = 255;
4349 ccg->secs_per_track = 63;
4350 } else {
4351 ccg->heads = 64;
4352 ccg->secs_per_track = 32;
4354 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
4355 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
4356 ccb->ccb_h.status = CAM_REQ_CMP;
4357 xpt_done(ccb);
4358 break;
4360 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
4362 OUTB (nc_scntl1, CRST);
4363 ccb->ccb_h.status = CAM_REQ_CMP;
4364 DELAY(10000); /* Wait until our interrupt handler sees it */
4365 xpt_done(ccb);
4366 break;
4368 case XPT_TERM_IO: /* Terminate the I/O process */
4369 /* XXX Implement */
4370 ccb->ccb_h.status = CAM_REQ_INVALID;
4371 xpt_done(ccb);
4372 break;
4373 case XPT_PATH_INQ: /* Path routing inquiry */
4375 struct ccb_pathinq *cpi = &ccb->cpi;
4377 cpi->version_num = 1; /* XXX??? */
4378 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
4379 if ((np->features & FE_WIDE) != 0)
4380 cpi->hba_inquiry |= PI_WIDE_16;
4381 cpi->target_sprt = 0;
4382 cpi->hba_misc = 0;
4383 cpi->hba_eng_cnt = 0;
4384 cpi->max_target = (np->features & FE_WIDE) ? 15 : 7;
4385 cpi->max_lun = MAX_LUN - 1;
4386 cpi->initiator_id = np->myaddr;
4387 cpi->bus_id = cam_sim_bus(sim);
4388 cpi->base_transfer_speed = 3300;
4389 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4390 strncpy(cpi->hba_vid, "Symbios", HBA_IDLEN);
4391 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4392 cpi->unit_number = cam_sim_unit(sim);
4393 cpi->transport = XPORT_SPI;
4394 cpi->transport_version = 2;
4395 cpi->protocol = PROTO_SCSI;
4396 cpi->protocol_version = SCSI_REV_2;
4397 cpi->ccb_h.status = CAM_REQ_CMP;
4398 xpt_done(ccb);
4399 break;
4401 default:
4402 ccb->ccb_h.status = CAM_REQ_INVALID;
4403 xpt_done(ccb);
4404 break;
4408 /*==========================================================
4411 ** Complete execution of a SCSI command.
4412 ** Signal completion to the generic SCSI driver.
4415 **==========================================================
4418 static void
4419 ncr_complete (ncb_p np, nccb_p cp)
4421 union ccb *ccb;
4422 tcb_p tp;
4425 ** Sanity check
4428 if (!cp || (cp->magic!=CCB_MAGIC) || !cp->ccb) return;
4429 cp->magic = 1;
4430 cp->tlimit= 0;
4433 ** No Reselect anymore.
4435 cp->jump_nccb.l_cmd = (SCR_JUMP);
4438 ** No starting.
4440 cp->phys.header.launch.l_paddr= NCB_SCRIPT_PHYS (np, idle);
4443 ** timestamp
4445 ncb_profile (np, cp);
4447 if (DEBUG_FLAGS & DEBUG_TINY)
4448 kprintf ("CCB=%x STAT=%x/%x\n", (int)(intptr_t)cp & 0xfff,
4449 cp->host_status,cp->s_status);
4451 ccb = cp->ccb;
4452 cp->ccb = NULL;
4453 tp = &np->target[ccb->ccb_h.target_id];
4456 ** We do not queue more than 1 nccb per target
4457 ** with negotiation at any time. If this nccb was
4458 ** used for negotiation, clear this info in the tcb.
4461 if (cp == tp->nego_cp)
4462 tp->nego_cp = NULL;
4465 ** Check for parity errors.
4467 /* XXX JGibbs - What about reporting them??? */
4469 if (cp->parity_status) {
4470 PRINT_ADDR(ccb);
4471 kprintf ("%d parity error(s), fallback.\n", cp->parity_status);
4473 ** fallback to asynch transfer.
4475 tp->tinfo.goal.period = 0;
4476 tp->tinfo.goal.offset = 0;
4480 ** Check for extended errors.
4483 if (cp->xerr_status != XE_OK) {
4484 PRINT_ADDR(ccb);
4485 switch (cp->xerr_status) {
4486 case XE_EXTRA_DATA:
4487 kprintf ("extraneous data discarded.\n");
4488 break;
4489 case XE_BAD_PHASE:
4490 kprintf ("illegal scsi phase (4/5).\n");
4491 break;
4492 default:
4493 kprintf ("extended error %d.\n", cp->xerr_status);
4494 break;
4496 if (cp->host_status==HS_COMPLETE)
4497 cp->host_status = HS_FAIL;
4501 ** Check the status.
4503 if (cp->host_status == HS_COMPLETE) {
4505 if (cp->s_status == SCSI_STATUS_OK) {
4508 ** All went well.
4510 /* XXX JGibbs - Properly calculate residual */
4512 tp->bytes += ccb->csio.dxfer_len;
4513 tp->transfers ++;
4515 ccb->ccb_h.status = CAM_REQ_CMP;
4516 } else if ((cp->s_status & SCSI_STATUS_SENSE) != 0) {
4519 * XXX Could be TERMIO too. Should record
4520 * original status.
4522 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
4523 cp->s_status &= ~SCSI_STATUS_SENSE;
4524 if (cp->s_status == SCSI_STATUS_OK) {
4525 ccb->ccb_h.status =
4526 CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR;
4527 } else {
4528 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
4530 } else {
4531 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
4532 ccb->csio.scsi_status = cp->s_status;
4536 } else if (cp->host_status == HS_SEL_TIMEOUT) {
4539 ** Device failed selection
4541 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
4543 } else if (cp->host_status == HS_TIMEOUT) {
4546 ** No response
4548 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
4549 } else if (cp->host_status == HS_STALL) {
4550 ccb->ccb_h.status = CAM_REQUEUE_REQ;
4551 } else {
4554 ** Other protocol messes
4556 PRINT_ADDR(ccb);
4557 kprintf ("COMMAND FAILED (%x %x) @%p.\n",
4558 cp->host_status, cp->s_status, cp);
4560 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
4563 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4564 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
4565 ccb->ccb_h.status |= CAM_DEV_QFRZN;
4569 ** Free this nccb
4571 ncr_free_nccb (np, cp);
4574 ** signal completion to generic driver.
4576 xpt_done (ccb);
4579 /*==========================================================
4582 ** Signal all (or one) control block done.
4585 **==========================================================
4588 static void
4589 ncr_wakeup (ncb_p np, u_long code)
4592 ** Starting at the default nccb and following
4593 ** the links, complete all jobs with a
4594 ** host_status greater than "disconnect".
4596 ** If the "code" parameter is not zero,
4597 ** complete all jobs that are not IDLE.
4600 nccb_p cp = np->link_nccb;
4601 while (cp) {
4602 switch (cp->host_status) {
4604 case HS_IDLE:
4605 break;
4607 case HS_DISCONNECT:
4608 if(DEBUG_FLAGS & DEBUG_TINY) kprintf ("D");
4609 /* fall through */
4611 case HS_BUSY:
4612 case HS_NEGOTIATE:
4613 if (!code) break;
4614 cp->host_status = code;
4616 /* fall through */
4618 default:
4619 ncr_complete (np, cp);
4620 break;
4622 cp = cp -> link_nccb;
4626 static void
4627 ncr_freeze_devq (ncb_p np, struct cam_path *path)
4629 nccb_p cp;
4630 int i;
4631 int count;
4632 int firstskip;
4634 ** Starting at the first nccb and following
4635 ** the links, complete all jobs that match
4636 ** the passed in path and are in the start queue.
4639 cp = np->link_nccb;
4640 count = 0;
4641 firstskip = 0;
4642 while (cp) {
4643 switch (cp->host_status) {
4645 case HS_BUSY:
4646 case HS_NEGOTIATE:
4647 if ((cp->phys.header.launch.l_paddr
4648 == NCB_SCRIPT_PHYS (np, select))
4649 && (xpt_path_comp(path, cp->ccb->ccb_h.path) >= 0)) {
4651 /* Mark for removal from the start queue */
4652 for (i = 1; i < MAX_START; i++) {
4653 int idx;
4655 idx = np->squeueput - i;
4657 if (idx < 0)
4658 idx = MAX_START + idx;
4659 if (np->squeue[idx]
4660 == CCB_PHYS(cp, phys)) {
4661 np->squeue[idx] =
4662 NCB_SCRIPT_PHYS (np, skip);
4663 if (i > firstskip)
4664 firstskip = i;
4665 break;
4668 cp->host_status=HS_STALL;
4669 ncr_complete (np, cp);
4670 count++;
4672 break;
4673 default:
4674 break;
4676 cp = cp->link_nccb;
4679 if (count > 0) {
4680 int j;
4681 int bidx;
4683 /* Compress the start queue */
4684 j = 0;
4685 bidx = np->squeueput;
4686 i = np->squeueput - firstskip;
4687 if (i < 0)
4688 i = MAX_START + i;
4689 for (;;) {
4691 bidx = i - j;
4692 if (bidx < 0)
4693 bidx = MAX_START + bidx;
4695 if (np->squeue[i] == NCB_SCRIPT_PHYS (np, skip)) {
4696 j++;
4697 } else if (j != 0) {
4698 np->squeue[bidx] = np->squeue[i];
4699 if (np->squeue[bidx]
4700 == NCB_SCRIPT_PHYS(np, idle))
4701 break;
4703 i = (i + 1) % MAX_START;
4705 np->squeueput = bidx;
4709 /*==========================================================
4712 ** Start NCR chip.
4715 **==========================================================
4718 static void
4719 ncr_init(ncb_p np, char * msg, u_long code)
4721 int i;
4724 ** Reset chip.
4727 OUTB (nc_istat, SRST);
4728 DELAY (1000);
4729 OUTB (nc_istat, 0);
4732 ** Message.
4735 if (msg) kprintf ("%s: restart (%s).\n", ncr_name (np), msg);
4738 ** Clear Start Queue
4741 for (i=0;i<MAX_START;i++)
4742 np -> squeue [i] = NCB_SCRIPT_PHYS (np, idle);
4745 ** Start at first entry.
4748 np->squeueput = 0;
4749 WRITESCRIPT(startpos[0], NCB_SCRIPTH_PHYS (np, tryloop));
4750 WRITESCRIPT(start0 [0], SCR_INT ^ IFFALSE (0));
4753 ** Wakeup all pending jobs.
4756 ncr_wakeup (np, code);
4759 ** Init chip.
4762 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort ... */
4763 OUTB (nc_scntl0, 0xca ); /* full arb., ena parity, par->ATN */
4764 OUTB (nc_scntl1, 0x00 ); /* odd parity, and remove CRST!! */
4765 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
4766 OUTB (nc_scid , RRE|np->myaddr);/* host adapter SCSI address */
4767 OUTW (nc_respid, 1ul<<np->myaddr);/* id to respond to */
4768 OUTB (nc_istat , SIGP ); /* Signal Process */
4769 OUTB (nc_dmode , np->rv_dmode); /* XXX modify burstlen ??? */
4770 OUTB (nc_dcntl , np->rv_dcntl);
4771 OUTB (nc_ctest3, np->rv_ctest3);
4772 OUTB (nc_ctest5, np->rv_ctest5);
4773 OUTB (nc_ctest4, np->rv_ctest4);/* enable master parity checking */
4774 OUTB (nc_stest2, np->rv_stest2|EXT); /* Extended Sreq/Sack filtering */
4775 OUTB (nc_stest3, TE ); /* TolerANT enable */
4776 OUTB (nc_stime0, 0x0b ); /* HTH = disabled, STO = 0.1 sec. */
4778 if (bootverbose >= 2) {
4779 kprintf ("\tACTUAL values:SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
4780 np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
4781 kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
4782 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
4786 ** Enable GPIO0 pin for writing if LED support.
4789 if (np->features & FE_LED0) {
4790 OUTOFFB (nc_gpcntl, 0x01);
4794 ** Fill in target structure.
4796 for (i=0;i<MAX_TARGET;i++) {
4797 tcb_p tp = &np->target[i];
4799 tp->tinfo.sval = 0;
4800 tp->tinfo.wval = np->rv_scntl3;
4802 tp->tinfo.current.period = 0;
4803 tp->tinfo.current.offset = 0;
4804 tp->tinfo.current.width = MSG_EXT_WDTR_BUS_8_BIT;
4808 ** enable ints
4811 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST);
4812 OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
4815 ** Start script processor.
4818 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
4821 * Notify the XPT of the event
4823 if (code == HS_RESET)
4824 xpt_async(AC_BUS_RESET, np->path, NULL);
4827 static void
4828 ncr_poll(struct cam_sim *sim)
4830 ncr_intr(cam_sim_softc(sim));
4834 /*==========================================================
4836 ** Get clock factor and sync divisor for a given
4837 ** synchronous factor period.
4838 ** Returns the clock factor (in sxfer) and scntl3
4839 ** synchronous divisor field.
4841 **==========================================================
4844 static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
4846 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
4847 int div = np->clock_divn; /* Number of divisors supported */
4848 u_long fak; /* Sync factor in sxfer */
4849 u_long per; /* Period in tenths of ns */
4850 u_long kpc; /* (per * clk) */
4853 ** Compute the synchronous period in tenths of nano-seconds
4855 if (sfac <= 10) per = 250;
4856 else if (sfac == 11) per = 303;
4857 else if (sfac == 12) per = 500;
4858 else per = 40 * sfac;
4861 ** Look for the greatest clock divisor that allows an
4862 ** input speed faster than the period.
4864 kpc = per * clk;
4865 while (--div >= 0)
4866 if (kpc >= (div_10M[div] * 4)) break;
4869 ** Calculate the lowest clock factor that allows an output
4870 ** speed not faster than the period.
4872 fak = (kpc - 1) / div_10M[div] + 1;
4874 #if 0 /* You can #if 1 if you think this optimization is useful */
4876 per = (fak * div_10M[div]) / clk;
4879 ** Why not to try the immediate lower divisor and to choose
4880 ** the one that allows the fastest output speed ?
4881 ** We dont want input speed too much greater than output speed.
4883 if (div >= 1 && fak < 6) {
4884 u_long fak2, per2;
4885 fak2 = (kpc - 1) / div_10M[div-1] + 1;
4886 per2 = (fak2 * div_10M[div-1]) / clk;
4887 if (per2 < per && fak2 <= 6) {
4888 fak = fak2;
4889 per = per2;
4890 --div;
4893 #endif
4895 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
4898 ** Compute and return sync parameters for the ncr
4900 *fakp = fak - 4;
4901 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
4904 /*==========================================================
4906 ** Switch sync mode for current job and its target
4908 **==========================================================
4911 static void
4912 ncr_setsync(ncb_p np, nccb_p cp, u_char scntl3, u_char sxfer, u_char period)
4914 union ccb *ccb;
4915 struct ccb_trans_settings *neg;
4916 tcb_p tp;
4917 int div;
4918 u_int target = INB (nc_sdid) & 0x0f;
4919 u_int period_10ns;
4921 assert (cp);
4922 if (!cp) return;
4924 ccb = cp->ccb;
4925 assert (ccb);
4926 if (!ccb) return;
4927 assert (target == ccb->ccb_h.target_id);
4929 tp = &np->target[target];
4931 if (!scntl3 || !(sxfer & 0x1f))
4932 scntl3 = np->rv_scntl3;
4933 scntl3 = (scntl3 & 0xf0) | (tp->tinfo.wval & EWS)
4934 | (np->rv_scntl3 & 0x07);
4937 ** Deduce the value of controller sync period from scntl3.
4938 ** period is in tenths of nano-seconds.
4941 div = ((scntl3 >> 4) & 0x7);
4942 if ((sxfer & 0x1f) && div)
4943 period_10ns =
4944 (((sxfer>>5)+4)*div_10M[div-1])/np->clock_khz;
4945 else
4946 period_10ns = 0;
4948 tp->tinfo.goal.period = period;
4949 tp->tinfo.goal.offset = sxfer & 0x1f;
4950 tp->tinfo.current.period = period;
4951 tp->tinfo.current.offset = sxfer & 0x1f;
4954 ** Stop there if sync parameters are unchanged
4956 if (tp->tinfo.sval == sxfer && tp->tinfo.wval == scntl3) return;
4957 tp->tinfo.sval = sxfer;
4958 tp->tinfo.wval = scntl3;
4960 if (sxfer & 0x1f) {
4962 ** Disable extended Sreq/Sack filtering
4964 if (period_10ns <= 2000) OUTOFFB (nc_stest2, EXT);
4968 ** Tell the SCSI layer about the
4969 ** new transfer parameters.
4971 neg = &xpt_alloc_ccb()->cts;
4972 neg->protocol = PROTO_SCSI;
4973 neg->protocol_version = SCSI_REV_2;
4974 neg->transport = XPORT_SPI;
4975 neg->transport_version = 2;
4976 neg->xport_specific.spi.sync_period = period;
4977 neg->xport_specific.spi.sync_offset = sxfer & 0x1f;
4978 neg->xport_specific.spi.valid = CTS_SPI_VALID_SYNC_RATE
4979 | CTS_SPI_VALID_SYNC_OFFSET;
4980 xpt_setup_ccb(&neg->ccb_h, ccb->ccb_h.path, /*priority*/1);
4981 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, neg);
4984 ** set actual value and sync_status
4986 OUTB (nc_sxfer, sxfer);
4987 np->sync_st = sxfer;
4988 OUTB (nc_scntl3, scntl3);
4989 np->wide_st = scntl3;
4992 ** patch ALL nccbs of this target.
4994 for (cp = np->link_nccb; cp; cp = cp->link_nccb) {
4995 if (!cp->ccb) continue;
4996 if (cp->ccb->ccb_h.target_id != target) continue;
4997 cp->sync_status = sxfer;
4998 cp->wide_status = scntl3;
5001 xpt_free_ccb(&neg->ccb_h);
5004 /*==========================================================
5006 ** Switch wide mode for current job and its target
5007 ** SCSI specs say: a SCSI device that accepts a WDTR
5008 ** message shall reset the synchronous agreement to
5009 ** asynchronous mode.
5011 **==========================================================
5014 static void ncr_setwide (ncb_p np, nccb_p cp, u_char wide, u_char ack)
5016 union ccb *ccb;
5017 struct ccb_trans_settings *neg;
5018 u_int target = INB (nc_sdid) & 0x0f;
5019 tcb_p tp;
5020 u_char scntl3;
5021 u_char sxfer;
5023 assert (cp);
5024 if (!cp) return;
5026 ccb = cp->ccb;
5027 assert (ccb);
5028 if (!ccb) return;
5029 assert (target == ccb->ccb_h.target_id);
5031 tp = &np->target[target];
5032 tp->tinfo.current.width = wide;
5033 tp->tinfo.goal.width = wide;
5034 tp->tinfo.current.period = 0;
5035 tp->tinfo.current.offset = 0;
5037 scntl3 = (tp->tinfo.wval & (~EWS)) | (wide ? EWS : 0);
5039 sxfer = ack ? 0 : tp->tinfo.sval;
5042 ** Stop there if sync/wide parameters are unchanged
5044 if (tp->tinfo.sval == sxfer && tp->tinfo.wval == scntl3) return;
5045 tp->tinfo.sval = sxfer;
5046 tp->tinfo.wval = scntl3;
5048 /* Tell the SCSI layer about the new transfer params */
5049 neg = &xpt_alloc_ccb()->cts;
5051 neg->protocol = PROTO_SCSI;
5052 neg->protocol_version = SCSI_REV_2;
5053 neg->transport = XPORT_SPI;
5054 neg->transport_version = 2;
5055 neg->xport_specific.spi.bus_width = (scntl3 & EWS) ?
5056 MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT;
5057 neg->xport_specific.spi.sync_period = 0;
5058 neg->xport_specific.spi.sync_offset = 0;
5059 neg->xport_specific.spi.valid = CTS_SPI_VALID_SYNC_RATE
5060 | CTS_SPI_VALID_SYNC_OFFSET
5061 | CTS_SPI_VALID_BUS_WIDTH;
5062 xpt_setup_ccb(&neg->ccb_h, ccb->ccb_h.path, /*priority*/1);
5063 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, neg);
5066 ** set actual value and sync_status
5068 OUTB (nc_sxfer, sxfer);
5069 np->sync_st = sxfer;
5070 OUTB (nc_scntl3, scntl3);
5071 np->wide_st = scntl3;
5074 ** patch ALL nccbs of this target.
5076 for (cp = np->link_nccb; cp; cp = cp->link_nccb) {
5077 if (!cp->ccb) continue;
5078 if (cp->ccb->ccb_h.target_id != target) continue;
5079 cp->sync_status = sxfer;
5080 cp->wide_status = scntl3;
5083 xpt_free_ccb(&neg->ccb_h);
5086 /*==========================================================
5089 ** ncr timeout handler.
5092 **==========================================================
5094 ** Misused to keep the driver running when
5095 ** interrupts are not configured correctly.
5097 **----------------------------------------------------------
5100 static void
5101 ncr_timeout (void *arg)
5103 ncb_p np = arg;
5104 time_t thistime = time_uptime;
5105 ticks_t step = np->ticks;
5106 long t;
5107 nccb_p cp;
5109 if (np->lasttime != thistime) {
5111 ** block ncr interrupts
5113 crit_enter();
5114 np->lasttime = thistime;
5116 /*----------------------------------------------------
5118 ** handle ncr chip timeouts
5120 ** Assumption:
5121 ** We have a chance to arbitrate for the
5122 ** SCSI bus at least every 10 seconds.
5124 **----------------------------------------------------
5127 t = thistime - np->heartbeat;
5129 if (t<2) np->latetime=0; else np->latetime++;
5131 if (np->latetime>2) {
5133 ** If there are no requests, the script
5134 ** processor will sleep on SEL_WAIT_RESEL.
5135 ** But we have to check whether it died.
5136 ** Let's try to wake it up.
5138 OUTB (nc_istat, SIGP);
5141 /*----------------------------------------------------
5143 ** handle nccb timeouts
5145 **----------------------------------------------------
5148 for (cp=np->link_nccb; cp; cp=cp->link_nccb) {
5150 ** look for timed out nccbs.
5152 if (!cp->host_status) continue;
5153 if (cp->tlimit > thistime) continue;
5156 ** Disable reselect.
5157 ** Remove it from startqueue.
5159 cp->jump_nccb.l_cmd = (SCR_JUMP);
5160 if (cp->phys.header.launch.l_paddr ==
5161 NCB_SCRIPT_PHYS (np, select)) {
5162 kprintf ("%s: timeout nccb=%p (skip)\n",
5163 ncr_name (np), cp);
5164 cp->phys.header.launch.l_paddr
5165 = NCB_SCRIPT_PHYS (np, skip);
5168 switch (cp->host_status) {
5170 case HS_BUSY:
5171 case HS_NEGOTIATE:
5172 /* fall through */
5173 case HS_DISCONNECT:
5174 cp->host_status=HS_TIMEOUT;
5176 cp->tag = 0;
5179 ** wakeup this nccb.
5181 ncr_complete (np, cp);
5183 crit_exit();
5186 callout_reset(&np->timeout_ch, step ? step : 1, ncr_timeout, np);
5188 if (INB(nc_istat) & (INTF|SIP|DIP)) {
5191 ** Process pending interrupts.
5194 crit_enter();
5195 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("{");
5196 ncr_exception (np);
5197 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("}");
5198 crit_exit();
5202 /*==========================================================
5204 ** log message for real hard errors
5206 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
5207 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
5209 ** exception register:
5210 ** ds: dstat
5211 ** si: sist
5213 ** SCSI bus lines:
5214 ** so: control lines as driver by NCR.
5215 ** si: control lines as seen by NCR.
5216 ** sd: scsi data lines as seen by NCR.
5218 ** wide/fastmode:
5219 ** sxfer: (see the manual)
5220 ** scntl3: (see the manual)
5222 ** current script command:
5223 ** dsp: script adress (relative to start of script).
5224 ** dbc: first word of script command.
5226 ** First 16 register of the chip:
5227 ** r0..rf
5229 **==========================================================
5232 static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
5234 u_int32_t dsp;
5235 int script_ofs;
5236 int script_size;
5237 char *script_name;
5238 u_char *script_base;
5239 int i;
5241 dsp = INL (nc_dsp);
5243 if (np->p_script < dsp &&
5244 dsp <= np->p_script + sizeof(struct script)) {
5245 script_ofs = dsp - np->p_script;
5246 script_size = sizeof(struct script);
5247 script_base = (u_char *) np->script;
5248 script_name = "script";
5250 else if (np->p_scripth < dsp &&
5251 dsp <= np->p_scripth + sizeof(struct scripth)) {
5252 script_ofs = dsp - np->p_scripth;
5253 script_size = sizeof(struct scripth);
5254 script_base = (u_char *) np->scripth;
5255 script_name = "scripth";
5256 } else {
5257 script_ofs = dsp;
5258 script_size = 0;
5259 script_base = NULL;
5260 script_name = "mem";
5263 kprintf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
5264 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
5265 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
5266 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
5267 (unsigned)INL (nc_dbc));
5269 if (((script_ofs & 3) == 0) &&
5270 (unsigned)script_ofs < script_size) {
5271 kprintf ("%s: script cmd = %08x\n", ncr_name(np),
5272 (int)READSCRIPT_OFF(script_base, script_ofs));
5275 kprintf ("%s: regdump:", ncr_name(np));
5276 for (i=0; i<16;i++)
5277 kprintf (" %02x", (unsigned)INB_OFF(i));
5278 kprintf (".\n");
5281 /*==========================================================
5284 ** ncr chip exception handler.
5287 **==========================================================
5290 static void ncr_exception (ncb_p np)
5292 u_char istat, dstat;
5293 u_short sist;
5296 ** interrupt on the fly ?
5298 while ((istat = INB (nc_istat)) & INTF) {
5299 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("F ");
5300 OUTB (nc_istat, INTF);
5301 np->profile.num_fly++;
5302 ncr_wakeup (np, 0);
5304 if (!(istat & (SIP|DIP))) {
5305 return;
5309 ** Steinbach's Guideline for Systems Programming:
5310 ** Never test for an error condition you don't know how to handle.
5313 sist = (istat & SIP) ? INW (nc_sist) : 0;
5314 dstat = (istat & DIP) ? INB (nc_dstat) : 0;
5315 np->profile.num_int++;
5317 if (DEBUG_FLAGS & DEBUG_TINY)
5318 kprintf ("<%d|%x:%x|%x:%x>",
5319 INB(nc_scr0),
5320 dstat,sist,
5321 (unsigned)INL(nc_dsp),
5322 (unsigned)INL(nc_dbc));
5323 if ((dstat==DFE) && (sist==PAR)) return;
5325 /*==========================================================
5327 ** First the normal cases.
5329 **==========================================================
5331 /*-------------------------------------------
5332 ** SCSI reset
5333 **-------------------------------------------
5336 if (sist & RST) {
5337 ncr_init (np, bootverbose ? "scsi reset" : NULL, HS_RESET);
5338 return;
5341 /*-------------------------------------------
5342 ** selection timeout
5344 ** IID excluded from dstat mask!
5345 ** (chip bug)
5346 **-------------------------------------------
5349 if ((sist & STO) &&
5350 !(sist & (GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5351 !(dstat & (MDPE|BF|ABRT|SIR))) {
5352 ncr_int_sto (np);
5353 return;
5356 /*-------------------------------------------
5357 ** Phase mismatch.
5358 **-------------------------------------------
5361 if ((sist & MA) &&
5362 !(sist & (STO|GEN|HTH|SGE|UDC|RST|PAR)) &&
5363 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5364 ncr_int_ma (np, dstat);
5365 return;
5368 /*----------------------------------------
5369 ** move command with length 0
5370 **----------------------------------------
5373 if ((dstat & IID) &&
5374 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5375 !(dstat & (MDPE|BF|ABRT|SIR)) &&
5376 ((INL(nc_dbc) & 0xf8000000) == SCR_MOVE_TBL)) {
5378 ** Target wants more data than available.
5379 ** The "no_data" script will do it.
5381 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, no_data));
5382 return;
5385 /*-------------------------------------------
5386 ** Programmed interrupt
5387 **-------------------------------------------
5390 if ((dstat & SIR) &&
5391 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5392 !(dstat & (MDPE|BF|ABRT|IID)) &&
5393 (INB(nc_dsps) <= SIR_MAX)) {
5394 ncr_int_sir (np);
5395 return;
5398 /*========================================
5399 ** log message for real hard errors
5400 **========================================
5403 ncr_log_hard_error(np, sist, dstat);
5405 /*========================================
5406 ** do the register dump
5407 **========================================
5410 if (time_uptime - np->regtime > 10) {
5411 int i;
5412 np->regtime = time_uptime;
5413 for (i=0; i<sizeof(np->regdump); i++)
5414 ((volatile char*)&np->regdump)[i] = INB_OFF(i);
5415 np->regdump.nc_dstat = dstat;
5416 np->regdump.nc_sist = sist;
5420 /*----------------------------------------
5421 ** clean up the dma fifo
5422 **----------------------------------------
5425 if ( (INB(nc_sstat0) & (ILF|ORF|OLF) ) ||
5426 (INB(nc_sstat1) & (FF3210) ) ||
5427 (INB(nc_sstat2) & (ILF1|ORF1|OLF1)) || /* wide .. */
5428 !(dstat & DFE)) {
5429 kprintf ("%s: have to clear fifos.\n", ncr_name (np));
5430 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
5431 OUTB (nc_ctest3, np->rv_ctest3 | CLF);
5432 /* clear dma fifo */
5435 /*----------------------------------------
5436 ** handshake timeout
5437 **----------------------------------------
5440 if (sist & HTH) {
5441 kprintf ("%s: handshake timeout\n", ncr_name(np));
5442 OUTB (nc_scntl1, CRST);
5443 DELAY (1000);
5444 OUTB (nc_scntl1, 0x00);
5445 OUTB (nc_scr0, HS_FAIL);
5446 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
5447 return;
5450 /*----------------------------------------
5451 ** unexpected disconnect
5452 **----------------------------------------
5455 if ((sist & UDC) &&
5456 !(sist & (STO|GEN|HTH|MA|SGE|RST|PAR)) &&
5457 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5458 OUTB (nc_scr0, HS_UNEXPECTED);
5459 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
5460 return;
5463 /*----------------------------------------
5464 ** cannot disconnect
5465 **----------------------------------------
5468 if ((dstat & IID) &&
5469 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5470 !(dstat & (MDPE|BF|ABRT|SIR)) &&
5471 ((INL(nc_dbc) & 0xf8000000) == SCR_WAIT_DISC)) {
5473 ** Unexpected data cycle while waiting for disconnect.
5475 if (INB(nc_sstat2) & LDSC) {
5477 ** It's an early reconnect.
5478 ** Let's continue ...
5480 OUTB (nc_dcntl, np->rv_dcntl | STD);
5482 ** info message
5484 kprintf ("%s: INFO: LDSC while IID.\n",
5485 ncr_name (np));
5486 return;
5488 kprintf ("%s: target %d doesn't release the bus.\n",
5489 ncr_name (np), INB (nc_sdid)&0x0f);
5491 ** return without restarting the NCR.
5492 ** timeout will do the real work.
5494 return;
5497 /*----------------------------------------
5498 ** single step
5499 **----------------------------------------
5502 if ((dstat & SSI) &&
5503 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5504 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5505 OUTB (nc_dcntl, np->rv_dcntl | STD);
5506 return;
5510 ** @RECOVER@ HTH, SGE, ABRT.
5512 ** We should try to recover from these interrupts.
5513 ** They may occur if there are problems with synch transfers, or
5514 ** if targets are switched on or off while the driver is running.
5517 if (sist & SGE) {
5518 /* clear scsi offsets */
5519 OUTB (nc_ctest3, np->rv_ctest3 | CLF);
5523 ** Freeze controller to be able to read the messages.
5526 if (DEBUG_FLAGS & DEBUG_FREEZE) {
5527 int i;
5528 unsigned char val;
5529 for (i=0; i<0x60; i++) {
5530 switch (i%16) {
5532 case 0:
5533 kprintf ("%s: reg[%d0]: ",
5534 ncr_name(np),i/16);
5535 break;
5536 case 4:
5537 case 8:
5538 case 12:
5539 kprintf (" ");
5540 break;
5542 val = bus_space_read_1(np->bst, np->bsh, i);
5543 kprintf (" %x%x", val/16, val%16);
5544 if (i%16==15) kprintf (".\n");
5547 callout_stop(&np->timeout_ch);
5549 kprintf ("%s: halted!\n", ncr_name(np));
5551 ** don't restart controller ...
5553 OUTB (nc_istat, SRST);
5554 return;
5557 #ifdef NCR_FREEZE
5559 ** Freeze system to be able to read the messages.
5561 kprintf ("ncr: fatal error: system halted - press reset to reboot ...");
5562 crit_enter();
5563 for (;;);
5564 #endif
5567 ** sorry, have to kill ALL jobs ...
5570 ncr_init (np, "fatal error", HS_FAIL);
5573 /*==========================================================
5575 ** ncr chip exception handler for selection timeout
5577 **==========================================================
5579 ** There seems to be a bug in the 53c810.
5580 ** Although a STO-Interrupt is pending,
5581 ** it continues executing script commands.
5582 ** But it will fail and interrupt (IID) on
5583 ** the next instruction where it's looking
5584 ** for a valid phase.
5586 **----------------------------------------------------------
5589 static void ncr_int_sto (ncb_p np)
5591 u_long dsa, scratcha, diff;
5592 nccb_p cp;
5593 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("T");
5596 ** look for nccb and set the status.
5599 dsa = INL (nc_dsa);
5600 cp = np->link_nccb;
5601 while (cp && (CCB_PHYS (cp, phys) != dsa))
5602 cp = cp->link_nccb;
5604 if (cp) {
5605 cp-> host_status = HS_SEL_TIMEOUT;
5606 ncr_complete (np, cp);
5610 ** repair start queue
5613 scratcha = INL (nc_scratcha);
5614 diff = scratcha - NCB_SCRIPTH_PHYS (np, tryloop);
5616 /* assert ((diff <= MAX_START * 20) && !(diff % 20));*/
5618 if ((diff <= MAX_START * 20) && !(diff % 20)) {
5619 WRITESCRIPT(startpos[0], scratcha);
5620 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
5621 return;
5623 ncr_init (np, "selection timeout", HS_FAIL);
5626 /*==========================================================
5629 ** ncr chip exception handler for phase errors.
5632 **==========================================================
5634 ** We have to construct a new transfer descriptor,
5635 ** to transfer the rest of the current block.
5637 **----------------------------------------------------------
5640 static void ncr_int_ma (ncb_p np, u_char dstat)
5642 u_int32_t dbc;
5643 u_int32_t rest;
5644 u_int32_t dsa;
5645 u_int32_t dsp;
5646 u_int32_t nxtdsp;
5647 volatile void *vdsp_base;
5648 size_t vdsp_off;
5649 u_int32_t oadr, olen;
5650 u_int32_t *tblp, *newcmd;
5651 u_char cmd, sbcl, ss0, ss2, ctest5;
5652 u_short delta;
5653 nccb_p cp;
5655 dsp = INL (nc_dsp);
5656 dsa = INL (nc_dsa);
5657 dbc = INL (nc_dbc);
5658 ss0 = INB (nc_sstat0);
5659 ss2 = INB (nc_sstat2);
5660 sbcl= INB (nc_sbcl);
5662 cmd = dbc >> 24;
5663 rest= dbc & 0xffffff;
5665 ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
5666 if (ctest5 & DFS)
5667 delta=(((ctest5<<8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
5668 else
5669 delta=(INB (nc_dfifo) - rest) & 0x7f;
5673 ** The data in the dma fifo has not been transfered to
5674 ** the target -> add the amount to the rest
5675 ** and clear the data.
5676 ** Check the sstat2 register in case of wide transfer.
5679 if (!(dstat & DFE)) rest += delta;
5680 if (ss0 & OLF) rest++;
5681 if (ss0 & ORF) rest++;
5682 if (INB(nc_scntl3) & EWS) {
5683 if (ss2 & OLF1) rest++;
5684 if (ss2 & ORF1) rest++;
5686 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
5687 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
5690 ** locate matching cp
5692 cp = np->link_nccb;
5693 while (cp && (CCB_PHYS (cp, phys) != dsa))
5694 cp = cp->link_nccb;
5696 if (!cp) {
5697 kprintf ("%s: SCSI phase error fixup: CCB already dequeued (%p)\n",
5698 ncr_name (np), (void *) np->header.cp);
5699 return;
5701 if (cp != np->header.cp) {
5702 kprintf ("%s: SCSI phase error fixup: CCB address mismatch "
5703 "(%p != %p) np->nccb = %p\n",
5704 ncr_name (np), (void *)cp, (void *)np->header.cp,
5705 (void *)np->link_nccb);
5706 /* return;*/
5710 ** find the interrupted script command,
5711 ** and the address at which to continue.
5714 if (dsp == vtophys (&cp->patch[2])) {
5715 vdsp_base = cp;
5716 vdsp_off = offsetof(struct nccb, patch[0]);
5717 nxtdsp = READSCRIPT_OFF(vdsp_base, vdsp_off + 3*4);
5718 } else if (dsp == vtophys (&cp->patch[6])) {
5719 vdsp_base = cp;
5720 vdsp_off = offsetof(struct nccb, patch[4]);
5721 nxtdsp = READSCRIPT_OFF(vdsp_base, vdsp_off + 3*4);
5722 } else if (dsp > np->p_script &&
5723 dsp <= np->p_script + sizeof(struct script)) {
5724 vdsp_base = np->script;
5725 vdsp_off = dsp - np->p_script - 8;
5726 nxtdsp = dsp;
5727 } else {
5728 vdsp_base = np->scripth;
5729 vdsp_off = dsp - np->p_scripth - 8;
5730 nxtdsp = dsp;
5734 ** log the information
5736 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE)) {
5737 kprintf ("P%x%x ",cmd&7, sbcl&7);
5738 kprintf ("RL=%d D=%d SS0=%x ",
5739 (unsigned) rest, (unsigned) delta, ss0);
5741 if (DEBUG_FLAGS & DEBUG_PHASE) {
5742 kprintf ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
5743 cp, np->header.cp,
5744 dsp,
5745 nxtdsp, (volatile char*)vdsp_base+vdsp_off, cmd);
5749 ** get old startaddress and old length.
5752 oadr = READSCRIPT_OFF(vdsp_base, vdsp_off + 1*4);
5754 if (cmd & 0x10) { /* Table indirect */
5755 tblp = (u_int32_t *) ((char*) &cp->phys + oadr);
5756 olen = tblp[0];
5757 oadr = tblp[1];
5758 } else {
5759 tblp = NULL;
5760 olen = READSCRIPT_OFF(vdsp_base, vdsp_off) & 0xffffff;
5763 if (DEBUG_FLAGS & DEBUG_PHASE) {
5764 kprintf ("OCMD=%x\nTBLP=%p OLEN=%lx OADR=%lx\n",
5765 (unsigned) (READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24),
5766 (void *) tblp,
5767 (u_long) olen,
5768 (u_long) oadr);
5772 ** if old phase not dataphase, leave here.
5775 if (cmd != (READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24)) {
5776 PRINT_ADDR(cp->ccb);
5777 kprintf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
5778 (unsigned)cmd,
5779 (unsigned)READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24);
5781 return;
5783 if (cmd & 0x06) {
5784 PRINT_ADDR(cp->ccb);
5785 kprintf ("phase change %x-%x %d@%08x resid=%d.\n",
5786 cmd&7, sbcl&7, (unsigned)olen,
5787 (unsigned)oadr, (unsigned)rest);
5789 OUTB (nc_dcntl, np->rv_dcntl | STD);
5790 return;
5794 ** choose the correct patch area.
5795 ** if savep points to one, choose the other.
5798 newcmd = cp->patch;
5799 if (cp->phys.header.savep == vtophys (newcmd)) newcmd+=4;
5802 ** fillin the commands
5805 newcmd[0] = ((cmd & 0x0f) << 24) | rest;
5806 newcmd[1] = oadr + olen - rest;
5807 newcmd[2] = SCR_JUMP;
5808 newcmd[3] = nxtdsp;
5810 if (DEBUG_FLAGS & DEBUG_PHASE) {
5811 PRINT_ADDR(cp->ccb);
5812 kprintf ("newcmd[%d] %x %x %x %x.\n",
5813 (int)(newcmd - cp->patch),
5814 (unsigned)newcmd[0],
5815 (unsigned)newcmd[1],
5816 (unsigned)newcmd[2],
5817 (unsigned)newcmd[3]);
5820 ** fake the return address (to the patch).
5821 ** and restart script processor at dispatcher.
5823 np->profile.num_break++;
5824 OUTL (nc_temp, vtophys (newcmd));
5825 if ((cmd & 7) == 0)
5826 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
5827 else
5828 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, checkatn));
5831 /*==========================================================
5834 ** ncr chip exception handler for programmed interrupts.
5837 **==========================================================
5840 static int ncr_show_msg (u_char * msg)
5842 u_char i;
5843 kprintf ("%x",*msg);
5844 if (*msg==MSG_EXTENDED) {
5845 for (i=1;i<8;i++) {
5846 if (i-1>msg[1]) break;
5847 kprintf ("-%x",msg[i]);
5849 return (i+1);
5850 } else if ((*msg & 0xf0) == 0x20) {
5851 kprintf ("-%x",msg[1]);
5852 return (2);
5854 return (1);
5857 static void ncr_int_sir (ncb_p np)
5859 u_char scntl3;
5860 u_char chg, ofs, per, fak, wide;
5861 u_char num = INB (nc_dsps);
5862 nccb_p cp=NULL;
5863 u_long dsa;
5864 u_int target = INB (nc_sdid) & 0x0f;
5865 tcb_p tp = &np->target[target];
5866 int i;
5867 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("I#%d", num);
5869 switch (num) {
5870 case SIR_SENSE_RESTART:
5871 case SIR_STALL_RESTART:
5872 break;
5874 default:
5876 ** lookup the nccb
5878 dsa = INL (nc_dsa);
5879 cp = np->link_nccb;
5880 while (cp && (CCB_PHYS (cp, phys) != dsa))
5881 cp = cp->link_nccb;
5883 assert (cp);
5884 if (!cp)
5885 goto out;
5886 assert (cp == np->header.cp);
5887 if (cp != np->header.cp)
5888 goto out;
5891 switch (num) {
5893 /*--------------------------------------------------------------------
5895 ** Processing of interrupted getcc selects
5897 **--------------------------------------------------------------------
5900 case SIR_SENSE_RESTART:
5901 /*------------------------------------------
5902 ** Script processor is idle.
5903 ** Look for interrupted "check cond"
5904 **------------------------------------------
5907 if (DEBUG_FLAGS & DEBUG_RESTART)
5908 kprintf ("%s: int#%d",ncr_name (np),num);
5909 cp = (nccb_p) 0;
5910 for (i=0; i<MAX_TARGET; i++) {
5911 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf (" t%d", i);
5912 tp = &np->target[i];
5913 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("+");
5914 cp = tp->hold_cp;
5915 if (!cp) continue;
5916 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("+");
5917 if ((cp->host_status==HS_BUSY) &&
5918 (cp->s_status==SCSI_STATUS_CHECK_COND))
5919 break;
5920 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("- (remove)");
5921 tp->hold_cp = cp = (nccb_p) 0;
5924 if (cp) {
5925 if (DEBUG_FLAGS & DEBUG_RESTART)
5926 kprintf ("+ restart job ..\n");
5927 OUTL (nc_dsa, CCB_PHYS (cp, phys));
5928 OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, getcc));
5929 return;
5933 ** no job, resume normal processing
5935 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf (" -- remove trap\n");
5936 WRITESCRIPT(start0[0], SCR_INT ^ IFFALSE (0));
5937 break;
5939 case SIR_SENSE_FAILED:
5940 /*-------------------------------------------
5941 ** While trying to select for
5942 ** getting the condition code,
5943 ** a target reselected us.
5944 **-------------------------------------------
5946 if (DEBUG_FLAGS & DEBUG_RESTART) {
5947 PRINT_ADDR(cp->ccb);
5948 kprintf ("in getcc reselect by t%d.\n",
5949 INB(nc_ssid) & 0x0f);
5953 ** Mark this job
5955 cp->host_status = HS_BUSY;
5956 cp->s_status = SCSI_STATUS_CHECK_COND;
5957 np->target[cp->ccb->ccb_h.target_id].hold_cp = cp;
5960 ** And patch code to restart it.
5962 WRITESCRIPT(start0[0], SCR_INT);
5963 break;
5965 /*-----------------------------------------------------------------------------
5967 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
5969 ** We try to negotiate sync and wide transfer only after
5970 ** a successfull inquire command. We look at byte 7 of the
5971 ** inquire data to determine the capabilities if the target.
5973 ** When we try to negotiate, we append the negotiation message
5974 ** to the identify and (maybe) simple tag message.
5975 ** The host status field is set to HS_NEGOTIATE to mark this
5976 ** situation.
5978 ** If the target doesn't answer this message immidiately
5979 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
5980 ** will be raised eventually.
5981 ** The handler removes the HS_NEGOTIATE status, and sets the
5982 ** negotiated value to the default (async / nowide).
5984 ** If we receive a matching answer immediately, we check it
5985 ** for validity, and set the values.
5987 ** If we receive a Reject message immediately, we assume the
5988 ** negotiation has failed, and fall back to standard values.
5990 ** If we receive a negotiation message while not in HS_NEGOTIATE
5991 ** state, it's a target initiated negotiation. We prepare a
5992 ** (hopefully) valid answer, set our parameters, and send back
5993 ** this answer to the target.
5995 ** If the target doesn't fetch the answer (no message out phase),
5996 ** we assume the negotiation has failed, and fall back to default
5997 ** settings.
5999 ** When we set the values, we adjust them in all nccbs belonging
6000 ** to this target, in the controller's register, and in the "phys"
6001 ** field of the controller's struct ncb.
6003 ** Possible cases: hs sir msg_in value send goto
6004 ** We try try to negotiate:
6005 ** -> target doesnt't msgin NEG FAIL noop defa. - dispatch
6006 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
6007 ** -> target answered (ok) NEG SYNC sdtr set - clrack
6008 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
6009 ** -> target answered (ok) NEG WIDE wdtr set - clrack
6010 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
6011 ** -> any other msgin NEG FAIL noop defa. - dispatch
6013 ** Target tries to negotiate:
6014 ** -> incoming message --- SYNC sdtr set SDTR -
6015 ** -> incoming message --- WIDE wdtr set WDTR -
6016 ** We sent our answer:
6017 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
6019 **-----------------------------------------------------------------------------
6022 case SIR_NEGO_FAILED:
6023 /*-------------------------------------------------------
6025 ** Negotiation failed.
6026 ** Target doesn't send an answer message,
6027 ** or target rejected our message.
6029 ** Remove negotiation request.
6031 **-------------------------------------------------------
6033 OUTB (HS_PRT, HS_BUSY);
6035 /* fall through */
6037 case SIR_NEGO_PROTO:
6038 /*-------------------------------------------------------
6040 ** Negotiation failed.
6041 ** Target doesn't fetch the answer message.
6043 **-------------------------------------------------------
6046 if (DEBUG_FLAGS & DEBUG_NEGO) {
6047 PRINT_ADDR(cp->ccb);
6048 kprintf ("negotiation failed sir=%x status=%x.\n",
6049 num, cp->nego_status);
6053 ** any error in negotiation:
6054 ** fall back to default mode.
6056 switch (cp->nego_status) {
6058 case NS_SYNC:
6059 ncr_setsync (np, cp, 0, 0xe0, 0);
6060 break;
6062 case NS_WIDE:
6063 ncr_setwide (np, cp, 0, 0);
6064 break;
6067 np->msgin [0] = MSG_NOOP;
6068 np->msgout[0] = MSG_NOOP;
6069 cp->nego_status = 0;
6070 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
6071 break;
6073 case SIR_NEGO_SYNC:
6075 ** Synchronous request message received.
6078 if (DEBUG_FLAGS & DEBUG_NEGO) {
6079 PRINT_ADDR(cp->ccb);
6080 kprintf ("sync msgin: ");
6081 (void) ncr_show_msg (np->msgin);
6082 kprintf (".\n");
6086 ** get requested values.
6089 chg = 0;
6090 per = np->msgin[3];
6091 ofs = np->msgin[4];
6092 if (ofs==0) per=255;
6095 ** check values against driver limits.
6097 if (per < np->minsync)
6098 {chg = 1; per = np->minsync;}
6099 if (per < tp->tinfo.user.period)
6100 {chg = 1; per = tp->tinfo.user.period;}
6101 if (ofs > tp->tinfo.user.offset)
6102 {chg = 1; ofs = tp->tinfo.user.offset;}
6105 ** Check against controller limits.
6108 fak = 7;
6109 scntl3 = 0;
6110 if (ofs != 0) {
6111 ncr_getsync(np, per, &fak, &scntl3);
6112 if (fak > 7) {
6113 chg = 1;
6114 ofs = 0;
6117 if (ofs == 0) {
6118 fak = 7;
6119 per = 0;
6120 scntl3 = 0;
6123 if (DEBUG_FLAGS & DEBUG_NEGO) {
6124 PRINT_ADDR(cp->ccb);
6125 kprintf ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
6126 per, scntl3, ofs, fak, chg);
6129 if (INB (HS_PRT) == HS_NEGOTIATE) {
6130 OUTB (HS_PRT, HS_BUSY);
6131 switch (cp->nego_status) {
6133 case NS_SYNC:
6135 ** This was an answer message
6137 if (chg) {
6139 ** Answer wasn't acceptable.
6141 ncr_setsync (np, cp, 0, 0xe0, 0);
6142 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
6143 } else {
6145 ** Answer is ok.
6147 ncr_setsync (np,cp,scntl3,(fak<<5)|ofs, per);
6148 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
6150 return;
6152 case NS_WIDE:
6153 ncr_setwide (np, cp, 0, 0);
6154 break;
6159 ** It was a request. Set value and
6160 ** prepare an answer message
6163 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs, per);
6165 np->msgout[0] = MSG_EXTENDED;
6166 np->msgout[1] = 3;
6167 np->msgout[2] = MSG_EXT_SDTR;
6168 np->msgout[3] = per;
6169 np->msgout[4] = ofs;
6171 cp->nego_status = NS_SYNC;
6173 if (DEBUG_FLAGS & DEBUG_NEGO) {
6174 PRINT_ADDR(cp->ccb);
6175 kprintf ("sync msgout: ");
6176 (void) ncr_show_msg (np->msgout);
6177 kprintf (".\n");
6180 if (!ofs) {
6181 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
6182 return;
6184 np->msgin [0] = MSG_NOOP;
6186 break;
6188 case SIR_NEGO_WIDE:
6190 ** Wide request message received.
6192 if (DEBUG_FLAGS & DEBUG_NEGO) {
6193 PRINT_ADDR(cp->ccb);
6194 kprintf ("wide msgin: ");
6195 (void) ncr_show_msg (np->msgin);
6196 kprintf (".\n");
6200 ** get requested values.
6203 chg = 0;
6204 wide = np->msgin[3];
6207 ** check values against driver limits.
6210 if (wide > tp->tinfo.user.width)
6211 {chg = 1; wide = tp->tinfo.user.width;}
6213 if (DEBUG_FLAGS & DEBUG_NEGO) {
6214 PRINT_ADDR(cp->ccb);
6215 kprintf ("wide: wide=%d chg=%d.\n", wide, chg);
6218 if (INB (HS_PRT) == HS_NEGOTIATE) {
6219 OUTB (HS_PRT, HS_BUSY);
6220 switch (cp->nego_status) {
6222 case NS_WIDE:
6224 ** This was an answer message
6226 if (chg) {
6228 ** Answer wasn't acceptable.
6230 ncr_setwide (np, cp, 0, 1);
6231 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
6232 } else {
6234 ** Answer is ok.
6236 ncr_setwide (np, cp, wide, 1);
6237 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
6239 return;
6241 case NS_SYNC:
6242 ncr_setsync (np, cp, 0, 0xe0, 0);
6243 break;
6248 ** It was a request, set value and
6249 ** prepare an answer message
6252 ncr_setwide (np, cp, wide, 1);
6254 np->msgout[0] = MSG_EXTENDED;
6255 np->msgout[1] = 2;
6256 np->msgout[2] = MSG_EXT_WDTR;
6257 np->msgout[3] = wide;
6259 np->msgin [0] = MSG_NOOP;
6261 cp->nego_status = NS_WIDE;
6263 if (DEBUG_FLAGS & DEBUG_NEGO) {
6264 PRINT_ADDR(cp->ccb);
6265 kprintf ("wide msgout: ");
6266 (void) ncr_show_msg (np->msgout);
6267 kprintf (".\n");
6269 break;
6271 /*--------------------------------------------------------------------
6273 ** Processing of special messages
6275 **--------------------------------------------------------------------
6278 case SIR_REJECT_RECEIVED:
6279 /*-----------------------------------------------
6281 ** We received a MSG_MESSAGE_REJECT message.
6283 **-----------------------------------------------
6286 PRINT_ADDR(cp->ccb);
6287 kprintf ("MSG_MESSAGE_REJECT received (%x:%x).\n",
6288 (unsigned)np->lastmsg, np->msgout[0]);
6289 break;
6291 case SIR_REJECT_SENT:
6292 /*-----------------------------------------------
6294 ** We received an unknown message
6296 **-----------------------------------------------
6299 PRINT_ADDR(cp->ccb);
6300 kprintf ("MSG_MESSAGE_REJECT sent for ");
6301 (void) ncr_show_msg (np->msgin);
6302 kprintf (".\n");
6303 break;
6305 /*--------------------------------------------------------------------
6307 ** Processing of special messages
6309 **--------------------------------------------------------------------
6312 case SIR_IGN_RESIDUE:
6313 /*-----------------------------------------------
6315 ** We received an IGNORE RESIDUE message,
6316 ** which couldn't be handled by the script.
6318 **-----------------------------------------------
6321 PRINT_ADDR(cp->ccb);
6322 kprintf ("MSG_IGN_WIDE_RESIDUE received, but not yet implemented.\n");
6323 break;
6325 case SIR_MISSING_SAVE:
6326 /*-----------------------------------------------
6328 ** We received an DISCONNECT message,
6329 ** but the datapointer wasn't saved before.
6331 **-----------------------------------------------
6334 PRINT_ADDR(cp->ccb);
6335 kprintf ("MSG_DISCONNECT received, but datapointer not saved:\n"
6336 "\tdata=%x save=%x goal=%x.\n",
6337 (unsigned) INL (nc_temp),
6338 (unsigned) np->header.savep,
6339 (unsigned) np->header.goalp);
6340 break;
6342 /*--------------------------------------------------------------------
6344 ** Processing of a "SCSI_STATUS_QUEUE_FULL" status.
6346 ** XXX JGibbs - We should do the same thing for BUSY status.
6348 ** The current command has been rejected,
6349 ** because there are too many in the command queue.
6350 ** We have started too many commands for that target.
6352 **--------------------------------------------------------------------
6354 case SIR_STALL_QUEUE:
6355 cp->xerr_status = XE_OK;
6356 cp->host_status = HS_COMPLETE;
6357 cp->s_status = SCSI_STATUS_QUEUE_FULL;
6358 ncr_freeze_devq(np, cp->ccb->ccb_h.path);
6359 ncr_complete(np, cp);
6361 /* FALL THROUGH */
6363 case SIR_STALL_RESTART:
6364 /*-----------------------------------------------
6366 ** Enable selecting again,
6367 ** if NO disconnected jobs.
6369 **-----------------------------------------------
6372 ** Look for a disconnected job.
6374 cp = np->link_nccb;
6375 while (cp && cp->host_status != HS_DISCONNECT)
6376 cp = cp->link_nccb;
6379 ** if there is one, ...
6381 if (cp) {
6383 ** wait for reselection
6385 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
6386 return;
6390 ** else remove the interrupt.
6393 kprintf ("%s: queue empty.\n", ncr_name (np));
6394 WRITESCRIPT(start1[0], SCR_INT ^ IFFALSE (0));
6395 break;
6398 out:
6399 OUTB (nc_dcntl, np->rv_dcntl | STD);
6402 /*==========================================================
6405 ** Acquire a control block
6408 **==========================================================
6411 static nccb_p ncr_get_nccb
6412 (ncb_p np, u_long target, u_long lun)
6414 lcb_p lp;
6415 nccb_p cp = NULL;
6417 /* Keep our timeout handler out */
6418 crit_enter();
6421 ** Lun structure available ?
6424 lp = np->target[target].lp[lun];
6425 if (lp) {
6426 cp = lp->next_nccb;
6429 ** Look for free CCB
6432 while (cp && cp->magic) {
6433 cp = cp->next_nccb;
6438 ** if nothing available, create one.
6441 if (cp == NULL)
6442 cp = ncr_alloc_nccb(np, target, lun);
6444 if (cp != NULL) {
6445 if (cp->magic) {
6446 kprintf("%s: Bogus free cp found\n", ncr_name(np));
6447 crit_exit();
6448 return (NULL);
6450 cp->magic = 1;
6452 crit_exit();
6453 return (cp);
6456 /*==========================================================
6459 ** Release one control block
6462 **==========================================================
6465 static void ncr_free_nccb (ncb_p np, nccb_p cp)
6468 ** sanity
6471 assert (cp != NULL);
6473 cp -> host_status = HS_IDLE;
6474 cp -> magic = 0;
6477 /*==========================================================
6480 ** Allocation of resources for Targets/Luns/Tags.
6483 **==========================================================
6486 static nccb_p
6487 ncr_alloc_nccb (ncb_p np, u_long target, u_long lun)
6489 tcb_p tp;
6490 lcb_p lp;
6491 nccb_p cp;
6493 assert (np != NULL);
6495 if (target>=MAX_TARGET) return(NULL);
6496 if (lun >=MAX_LUN ) return(NULL);
6498 tp=&np->target[target];
6500 if (!tp->jump_tcb.l_cmd) {
6503 ** initialize it.
6505 tp->jump_tcb.l_cmd = (SCR_JUMP^IFFALSE (DATA (0x80 + target)));
6506 tp->jump_tcb.l_paddr = np->jump_tcb.l_paddr;
6508 tp->getscr[0] =
6509 (np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
6510 tp->getscr[1] = vtophys (&tp->tinfo.sval);
6511 tp->getscr[2] = rman_get_start(np->reg_res) + offsetof (struct ncr_reg, nc_sxfer);
6512 tp->getscr[3] =
6513 (np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
6514 tp->getscr[4] = vtophys (&tp->tinfo.wval);
6515 tp->getscr[5] = rman_get_start(np->reg_res) + offsetof (struct ncr_reg, nc_scntl3);
6517 assert (((offsetof(struct ncr_reg, nc_sxfer) ^
6518 (offsetof(struct tcb ,tinfo)
6519 + offsetof(struct ncr_target_tinfo, sval))) & 3) == 0);
6520 assert (((offsetof(struct ncr_reg, nc_scntl3) ^
6521 (offsetof(struct tcb, tinfo)
6522 + offsetof(struct ncr_target_tinfo, wval))) &3) == 0);
6524 tp->call_lun.l_cmd = (SCR_CALL);
6525 tp->call_lun.l_paddr = NCB_SCRIPT_PHYS (np, resel_lun);
6527 tp->jump_lcb.l_cmd = (SCR_JUMP);
6528 tp->jump_lcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
6529 np->jump_tcb.l_paddr = vtophys (&tp->jump_tcb);
6533 ** Logic unit control block
6535 lp = tp->lp[lun];
6536 if (!lp) {
6538 ** Allocate a lcb
6540 lp = kmalloc (sizeof (struct lcb), M_DEVBUF, M_WAITOK | M_ZERO);
6543 ** Initialize it
6545 lp->jump_lcb.l_cmd = (SCR_JUMP ^ IFFALSE (DATA (lun)));
6546 lp->jump_lcb.l_paddr = tp->jump_lcb.l_paddr;
6548 lp->call_tag.l_cmd = (SCR_CALL);
6549 lp->call_tag.l_paddr = NCB_SCRIPT_PHYS (np, resel_tag);
6551 lp->jump_nccb.l_cmd = (SCR_JUMP);
6552 lp->jump_nccb.l_paddr = NCB_SCRIPTH_PHYS (np, aborttag);
6554 lp->actlink = 1;
6557 ** Chain into LUN list
6559 tp->jump_lcb.l_paddr = vtophys (&lp->jump_lcb);
6560 tp->lp[lun] = lp;
6565 ** Allocate a nccb
6567 cp = kmalloc (sizeof (struct nccb), M_DEVBUF, M_WAITOK | M_ZERO);
6569 if (DEBUG_FLAGS & DEBUG_ALLOC) {
6570 kprintf ("new nccb @%p.\n", cp);
6574 ** Fill in physical addresses
6577 cp->p_nccb = vtophys (cp);
6580 ** Chain into reselect list
6582 cp->jump_nccb.l_cmd = SCR_JUMP;
6583 cp->jump_nccb.l_paddr = lp->jump_nccb.l_paddr;
6584 lp->jump_nccb.l_paddr = CCB_PHYS (cp, jump_nccb);
6585 cp->call_tmp.l_cmd = SCR_CALL;
6586 cp->call_tmp.l_paddr = NCB_SCRIPT_PHYS (np, resel_tmp);
6589 ** Chain into wakeup list
6591 cp->link_nccb = np->link_nccb;
6592 np->link_nccb = cp;
6595 ** Chain into CCB list
6597 cp->next_nccb = lp->next_nccb;
6598 lp->next_nccb = cp;
6600 return (cp);
6603 /*==========================================================
6606 ** Build Scatter Gather Block
6609 **==========================================================
6611 ** The transfer area may be scattered among
6612 ** several non adjacent physical pages.
6614 ** We may use MAX_SCATTER blocks.
6616 **----------------------------------------------------------
6619 static int ncr_scatter
6620 (struct dsb* phys, vm_offset_t vaddr, vm_size_t datalen)
6622 u_long paddr, pnext;
6624 u_short segment = 0;
6625 u_long segsize, segaddr;
6626 u_long size, csize = 0;
6627 u_long chunk = MAX_SIZE;
6628 int free;
6630 bzero (&phys->data, sizeof (phys->data));
6631 if (!datalen) return (0);
6633 paddr = vtophys (vaddr);
6636 ** insert extra break points at a distance of chunk.
6637 ** We try to reduce the number of interrupts caused
6638 ** by unexpected phase changes due to disconnects.
6639 ** A typical harddisk may disconnect before ANY block.
6640 ** If we wanted to avoid unexpected phase changes at all
6641 ** we had to use a break point every 512 bytes.
6642 ** Of course the number of scatter/gather blocks is
6643 ** limited.
6646 free = MAX_SCATTER - 1;
6648 if (vaddr & PAGE_MASK) free -= datalen / PAGE_SIZE;
6650 if (free>1)
6651 while ((chunk * free >= 2 * datalen) && (chunk>=1024))
6652 chunk /= 2;
6654 if(DEBUG_FLAGS & DEBUG_SCATTER)
6655 kprintf("ncr?:\tscattering virtual=%p size=%d chunk=%d.\n",
6656 (void *) vaddr, (unsigned) datalen, (unsigned) chunk);
6659 ** Build data descriptors.
6661 while (datalen && (segment < MAX_SCATTER)) {
6664 ** this segment is empty
6666 segsize = 0;
6667 segaddr = paddr;
6668 pnext = paddr;
6670 if (!csize) csize = chunk;
6672 while ((datalen) && (paddr == pnext) && (csize)) {
6675 ** continue this segment
6677 pnext = (paddr & (~PAGE_MASK)) + PAGE_SIZE;
6680 ** Compute max size
6683 size = pnext - paddr; /* page size */
6684 if (size > datalen) size = datalen; /* data size */
6685 if (size > csize ) size = csize ; /* chunksize */
6687 segsize += size;
6688 vaddr += size;
6689 csize -= size;
6690 datalen -= size;
6691 paddr = vtophys (vaddr);
6694 if(DEBUG_FLAGS & DEBUG_SCATTER)
6695 kprintf ("\tseg #%d addr=%x size=%d (rest=%d).\n",
6696 segment,
6697 (unsigned) segaddr,
6698 (unsigned) segsize,
6699 (unsigned) datalen);
6701 phys->data[segment].addr = segaddr;
6702 phys->data[segment].size = segsize;
6703 segment++;
6706 if (datalen) {
6707 kprintf("ncr?: scatter/gather failed (residue=%d).\n",
6708 (unsigned) datalen);
6709 return (-1);
6712 return (segment);
6715 /*==========================================================
6718 ** Test the pci bus snoop logic :-(
6720 ** Has to be called with interrupts disabled.
6723 **==========================================================
6726 #ifndef NCR_IOMAPPED
6727 static int ncr_regtest (struct ncb* np)
6729 volatile u_int32_t data;
6731 ** ncr registers may NOT be cached.
6732 ** write 0xffffffff to a read only register area,
6733 ** and try to read it back.
6735 data = 0xffffffff;
6736 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
6737 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
6738 #if 1
6739 if (data == 0xffffffff) {
6740 #else
6741 if ((data & 0xe2f0fffd) != 0x02000080) {
6742 #endif
6743 kprintf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
6744 (unsigned) data);
6745 return (0x10);
6747 return (0);
6749 #endif
6751 static int ncr_snooptest (struct ncb* np)
6753 u_int32_t ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
6754 int i, err=0;
6755 #ifndef NCR_IOMAPPED
6756 err |= ncr_regtest (np);
6757 if (err) return (err);
6758 #endif
6760 ** init
6762 pc = NCB_SCRIPTH_PHYS (np, snooptest);
6763 host_wr = 1;
6764 ncr_wr = 2;
6766 ** Set memory and register.
6768 ncr_cache = host_wr;
6769 OUTL (nc_temp, ncr_wr);
6771 ** Start script (exchange values)
6773 OUTL (nc_dsp, pc);
6775 ** Wait 'til done (with timeout)
6777 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
6778 if (INB(nc_istat) & (INTF|SIP|DIP))
6779 break;
6781 ** Save termination position.
6783 pc = INL (nc_dsp);
6785 ** Read memory and register.
6787 host_rd = ncr_cache;
6788 ncr_rd = INL (nc_scratcha);
6789 ncr_bk = INL (nc_temp);
6791 ** Reset ncr chip
6793 OUTB (nc_istat, SRST);
6794 DELAY (1000);
6795 OUTB (nc_istat, 0 );
6797 ** check for timeout
6799 if (i>=NCR_SNOOP_TIMEOUT) {
6800 kprintf ("CACHE TEST FAILED: timeout.\n");
6801 return (0x20);
6804 ** Check termination position.
6806 if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
6807 kprintf ("CACHE TEST FAILED: script execution failed.\n");
6808 kprintf ("start=%08lx, pc=%08lx, end=%08lx\n",
6809 (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
6810 (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
6811 return (0x40);
6814 ** Show results.
6816 if (host_wr != ncr_rd) {
6817 kprintf ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
6818 (int) host_wr, (int) ncr_rd);
6819 err |= 1;
6821 if (host_rd != ncr_wr) {
6822 kprintf ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
6823 (int) ncr_wr, (int) host_rd);
6824 err |= 2;
6826 if (ncr_bk != ncr_wr) {
6827 kprintf ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
6828 (int) ncr_wr, (int) ncr_bk);
6829 err |= 4;
6831 return (err);
6834 /*==========================================================
6837 ** Profiling the drivers and targets performance.
6840 **==========================================================
6844 ** Compute the difference in milliseconds.
6847 static int ncr_delta (int *from, int *to)
6849 if (!from) return (-1);
6850 if (!to) return (-2);
6851 return ((to - from) * 1000 / hz);
6854 #define PROFILE cp->phys.header.stamp
6855 static void ncb_profile (ncb_p np, nccb_p cp)
6857 int co, da, st, en, di, se, post,work,disc;
6858 u_long diff;
6860 PROFILE.end = ticks;
6862 st = ncr_delta (&PROFILE.start,&PROFILE.status);
6863 if (st<0) return; /* status not reached */
6865 da = ncr_delta (&PROFILE.start,&PROFILE.data);
6866 if (da<0) return; /* No data transfer phase */
6868 co = ncr_delta (&PROFILE.start,&PROFILE.command);
6869 if (co<0) return; /* command not executed */
6871 en = ncr_delta (&PROFILE.start,&PROFILE.end),
6872 di = ncr_delta (&PROFILE.start,&PROFILE.disconnect),
6873 se = ncr_delta (&PROFILE.start,&PROFILE.select);
6874 post = en - st;
6877 ** @PROFILE@ Disconnect time invalid if multiple disconnects
6880 if (di>=0) disc = se-di; else disc = 0;
6882 work = (st - co) - disc;
6884 diff = (np->disc_phys - np->disc_ref) & 0xff;
6885 np->disc_ref += diff;
6887 np->profile.num_trans += 1;
6888 if (cp->ccb)
6889 np->profile.num_bytes += cp->ccb->csio.dxfer_len;
6890 np->profile.num_disc += diff;
6891 np->profile.ms_setup += co;
6892 np->profile.ms_data += work;
6893 np->profile.ms_disc += disc;
6894 np->profile.ms_post += post;
6896 #undef PROFILE
6898 /*==========================================================
6900 ** Determine the ncr's clock frequency.
6901 ** This is essential for the negotiation
6902 ** of the synchronous transfer rate.
6904 **==========================================================
6906 ** Note: we have to return the correct value.
6907 ** THERE IS NO SAVE DEFAULT VALUE.
6909 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
6910 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
6911 ** do not have a clock doubler and so are provided with a
6912 ** 80 MHz clock. All other fast20 boards incorporate a doubler
6913 ** and so should be delivered with a 40 MHz clock.
6914 ** The future fast40 chips (895/895) use a 40 Mhz base clock
6915 ** and provide a clock quadrupler (160 Mhz). The code below
6916 ** tries to deal as cleverly as possible with all this stuff.
6918 **----------------------------------------------------------
6922 * Select NCR SCSI clock frequency
6924 static void ncr_selectclock(ncb_p np, u_char scntl3)
6926 if (np->multiplier < 2) {
6927 OUTB(nc_scntl3, scntl3);
6928 return;
6931 if (bootverbose >= 2)
6932 kprintf ("%s: enabling clock multiplier\n", ncr_name(np));
6934 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
6935 if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
6936 int i = 20;
6937 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
6938 DELAY(20);
6939 if (!i)
6940 kprintf("%s: the chip cannot lock the frequency\n", ncr_name(np));
6941 } else /* Wait 20 micro-seconds for doubler */
6942 DELAY(20);
6943 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
6944 OUTB(nc_scntl3, scntl3);
6945 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
6946 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
6950 * calculate NCR SCSI clock frequency (in KHz)
6952 static unsigned
6953 ncrgetfreq (ncb_p np, int gen)
6955 int ms = 0;
6957 * Measure GEN timer delay in order
6958 * to calculate SCSI clock frequency
6960 * This code will never execute too
6961 * many loop iterations (if DELAY is
6962 * reasonably correct). It could get
6963 * too low a delay (too high a freq.)
6964 * if the CPU is slow executing the
6965 * loop for some reason (an NMI, for
6966 * example). For this reason we will
6967 * if multiple measurements are to be
6968 * performed trust the higher delay
6969 * (lower frequency returned).
6971 OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
6972 OUTW (nc_sien , 0); /* mask all scsi interrupts */
6973 (void) INW (nc_sist); /* clear pending scsi interrupt */
6974 OUTB (nc_dien , 0); /* mask all dma interrupts */
6975 (void) INW (nc_sist); /* another one, just to be sure :) */
6976 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
6977 OUTB (nc_stime1, 0); /* disable general purpose timer */
6978 OUTB (nc_stime1, gen); /* set to nominal delay of (1<<gen) * 125us */
6979 while (!(INW(nc_sist) & GEN) && ms++ < 1000)
6980 DELAY(1000); /* count ms */
6981 OUTB (nc_stime1, 0); /* disable general purpose timer */
6982 OUTB (nc_scntl3, 0);
6984 * Set prescaler to divide by whatever "0" means.
6985 * "0" ought to choose divide by 2, but appears
6986 * to set divide by 3.5 mode in my 53c810 ...
6988 OUTB (nc_scntl3, 0);
6990 if (bootverbose >= 2)
6991 kprintf ("\tDelay (GEN=%d): %u msec\n", gen, ms);
6993 * adjust for prescaler, and convert into KHz
6995 return ms ? ((1 << gen) * 4440) / ms : 0;
6998 static void ncr_getclock (ncb_p np, u_char multiplier)
7000 unsigned char scntl3;
7001 unsigned char stest1;
7002 scntl3 = INB(nc_scntl3);
7003 stest1 = INB(nc_stest1);
7005 np->multiplier = 1;
7007 if (multiplier > 1) {
7008 np->multiplier = multiplier;
7009 np->clock_khz = 40000 * multiplier;
7010 } else {
7011 if ((scntl3 & 7) == 0) {
7012 unsigned f1, f2;
7013 /* throw away first result */
7014 (void) ncrgetfreq (np, 11);
7015 f1 = ncrgetfreq (np, 11);
7016 f2 = ncrgetfreq (np, 11);
7018 if (bootverbose >= 2)
7019 kprintf ("\tNCR clock is %uKHz, %uKHz\n", f1, f2);
7020 if (f1 > f2) f1 = f2; /* trust lower result */
7021 if (f1 > 45000) {
7022 scntl3 = 5; /* >45Mhz: assume 80MHz */
7023 } else {
7024 scntl3 = 3; /* <45Mhz: assume 40MHz */
7027 else if ((scntl3 & 7) == 5)
7028 np->clock_khz = 80000; /* Probably a 875 rev. 1 ? */
7032 /*=========================================================================*/
7034 #ifdef NCR_TEKRAM_EEPROM
7036 struct tekram_eeprom_dev {
7037 u_char devmode;
7038 #define TKR_PARCHK 0x01
7039 #define TKR_TRYSYNC 0x02
7040 #define TKR_ENDISC 0x04
7041 #define TKR_STARTUNIT 0x08
7042 #define TKR_USETAGS 0x10
7043 #define TKR_TRYWIDE 0x20
7044 u_char syncparam; /* max. sync transfer rate (table ?) */
7045 u_char filler1;
7046 u_char filler2;
7050 struct tekram_eeprom {
7051 struct tekram_eeprom_dev
7052 dev[16];
7053 u_char adaptid;
7054 u_char adaptmode;
7055 #define TKR_ADPT_GT2DRV 0x01
7056 #define TKR_ADPT_GT1GB 0x02
7057 #define TKR_ADPT_RSTBUS 0x04
7058 #define TKR_ADPT_ACTNEG 0x08
7059 #define TKR_ADPT_NOSEEK 0x10
7060 #define TKR_ADPT_MORLUN 0x20
7061 u_char delay; /* unit ? ( table ??? ) */
7062 u_char tags; /* use 4 times as many ... */
7063 u_char filler[60];
7066 static void
7067 tekram_write_bit (ncb_p np, int bit)
7069 u_char val = 0x10 + ((bit & 1) << 1);
7071 DELAY(10);
7072 OUTB (nc_gpreg, val);
7073 DELAY(10);
7074 OUTB (nc_gpreg, val | 0x04);
7075 DELAY(10);
7076 OUTB (nc_gpreg, val);
7077 DELAY(10);
7080 static int
7081 tekram_read_bit (ncb_p np)
7083 OUTB (nc_gpreg, 0x10);
7084 DELAY(10);
7085 OUTB (nc_gpreg, 0x14);
7086 DELAY(10);
7087 return INB (nc_gpreg) & 1;
7090 static u_short
7091 read_tekram_eeprom_reg (ncb_p np, int reg)
7093 int bit;
7094 u_short result = 0;
7095 int cmd = 0x80 | reg;
7097 OUTB (nc_gpreg, 0x10);
7099 tekram_write_bit (np, 1);
7100 for (bit = 7; bit >= 0; bit--)
7102 tekram_write_bit (np, cmd >> bit);
7105 for (bit = 0; bit < 16; bit++)
7107 result <<= 1;
7108 result |= tekram_read_bit (np);
7111 OUTB (nc_gpreg, 0x00);
7112 return result;
7115 static int
7116 read_tekram_eeprom(ncb_p np, struct tekram_eeprom *buffer)
7118 u_short *p = (u_short *) buffer;
7119 u_short sum = 0;
7120 int i;
7122 if (INB (nc_gpcntl) != 0x09)
7124 return 0;
7126 for (i = 0; i < 64; i++)
7128 u_short val;
7129 if((i&0x0f) == 0) kprintf ("%02x:", i*2);
7130 val = read_tekram_eeprom_reg (np, i);
7131 if (p)
7132 *p++ = val;
7133 sum += val;
7134 if((i&0x01) == 0x00) kprintf (" ");
7135 kprintf ("%02x%02x", val & 0xff, (val >> 8) & 0xff);
7136 if((i&0x0f) == 0x0f) kprintf ("\n");
7138 kprintf ("Sum = %04x\n", sum);
7139 return sum == 0x1234;
7141 #endif /* NCR_TEKRAM_EEPROM */
7143 static device_method_t ncr_methods[] = {
7144 /* Device interface */
7145 DEVMETHOD(device_probe, ncr_probe),
7146 DEVMETHOD(device_attach, ncr_attach),
7148 DEVMETHOD_END
7151 static driver_t ncr_driver = {
7152 "ncr",
7153 ncr_methods,
7154 sizeof(struct ncb),
7157 static devclass_t ncr_devclass;
7159 DRIVER_MODULE(if_ncr, pci, ncr_driver, ncr_devclass, NULL, NULL);
7161 /*=========================================================================*/
7162 #endif /* _KERNEL */