1 /**************************************************************************
3 ** $FreeBSD: src/sys/pci/ncr.c,v 1.155.2.3 2001/03/05 13:09:10 obrien Exp $
4 ** $DragonFly: src/sys/dev/disk/ncr/ncr.c,v 1.22 2008/05/18 20:30:22 pavalos Exp $
6 ** Device driver for the NCR 53C8XX PCI-SCSI-Controller Family.
8 **-------------------------------------------------------------------------
10 ** Written for 386bsd and FreeBSD by
11 ** Wolfgang Stanglmeier <wolf@cologne.de>
12 ** Stefan Esser <se@mi.Uni-Koeln.de>
14 **-------------------------------------------------------------------------
16 ** Copyright (c) 1994 Wolfgang Stanglmeier. All rights reserved.
18 ** Redistribution and use in source and binary forms, with or without
19 ** modification, are permitted provided that the following conditions
21 ** 1. Redistributions of source code must retain the above copyright
22 ** notice, this list of conditions and the following disclaimer.
23 ** 2. Redistributions in binary form must reproduce the above copyright
24 ** notice, this list of conditions and the following disclaimer in the
25 ** documentation and/or other materials provided with the distribution.
26 ** 3. The name of the author may not be used to endorse or promote products
27 ** derived from this software without specific prior written permission.
29 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 ***************************************************************************
43 #define NCR_DATE "pl30 98/1/1"
45 #define NCR_VERSION (2)
46 #define MAX_UNITS (16)
48 #define NCR_GETCC_WITHMSG
50 #if (defined(__DragonFly__) || defined (__FreeBSD__)) && defined(_KERNEL)
54 /*==========================================================
56 ** Configuration and Debugging
58 ** May be overwritten in <arch/conf/xxxx>
60 **==========================================================
64 ** SCSI address of this device.
65 ** The boot routines should have set it.
69 #ifndef SCSI_NCR_MYADDR
70 #define SCSI_NCR_MYADDR (7)
71 #endif /* SCSI_NCR_MYADDR */
74 ** The default synchronous period factor
76 ** If maximum synchronous frequency is defined, use it instead.
79 #ifndef SCSI_NCR_MAX_SYNC
81 #ifndef SCSI_NCR_DFLT_SYNC
82 #define SCSI_NCR_DFLT_SYNC (12)
83 #endif /* SCSI_NCR_DFLT_SYNC */
87 #if SCSI_NCR_MAX_SYNC == 0
88 #define SCSI_NCR_DFLT_SYNC 0
90 #define SCSI_NCR_DFLT_SYNC (250000 / SCSI_NCR_MAX_SYNC)
96 ** The minimal asynchronous pre-scaler period (ns)
100 #ifndef SCSI_NCR_MIN_ASYNC
101 #define SCSI_NCR_MIN_ASYNC (40)
102 #endif /* SCSI_NCR_MIN_ASYNC */
105 ** The maximal bus with (in log2 byte)
106 ** (0=8 bit, 1=16 bit)
109 #ifndef SCSI_NCR_MAX_WIDE
110 #define SCSI_NCR_MAX_WIDE (1)
111 #endif /* SCSI_NCR_MAX_WIDE */
113 /*==========================================================
115 ** Configuration and Debugging
117 **==========================================================
121 ** Number of targets supported by the driver.
122 ** n permits target numbers 0..n-1.
123 ** Default is 7, meaning targets #0..#6.
127 #define MAX_TARGET (16)
130 ** Number of logic units supported by the driver.
131 ** n enables logic unit numbers 0..n-1.
132 ** The common SCSI devices require only
133 ** one lun, so take 1 as the default.
141 ** The maximum number of jobs scheduled for starting.
142 ** There should be one slot per target, and one slot
143 ** for each tag of each target in use.
146 #define MAX_START (256)
149 ** The maximum number of segments a transfer is split into.
152 #define MAX_SCATTER (33)
155 ** The maximum transfer length (should be >= 64k).
156 ** MUST NOT be greater than (MAX_SCATTER-1) * PAGE_SIZE.
159 #define MAX_SIZE ((MAX_SCATTER-1) * (long) PAGE_SIZE)
165 #define NCR_SNOOP_TIMEOUT (1000000)
167 /*==========================================================
171 **==========================================================
174 #include <sys/param.h>
175 #include <sys/time.h>
178 #include <sys/systm.h>
179 #include <sys/malloc.h>
181 #include <sys/kernel.h>
182 #include <sys/sysctl.h>
184 #include <sys/thread2.h>
185 #include <machine/clock.h>
186 #include <machine/md_var.h>
187 #include <sys/rman.h>
190 #include <vm/vm_extern.h>
193 #include <bus/pci/pcivar.h>
194 #include <bus/pci/pcireg.h>
197 #include <bus/cam/cam.h>
198 #include <bus/cam/cam_ccb.h>
199 #include <bus/cam/cam_sim.h>
200 #include <bus/cam/cam_xpt_sim.h>
201 #include <bus/cam/cam_debug.h>
203 #include <bus/cam/scsi/scsi_all.h>
204 #include <bus/cam/scsi/scsi_message.h>
206 /*==========================================================
210 **==========================================================
213 #define DEBUG_ALLOC (0x0001)
214 #define DEBUG_PHASE (0x0002)
215 #define DEBUG_POLL (0x0004)
216 #define DEBUG_QUEUE (0x0008)
217 #define DEBUG_RESULT (0x0010)
218 #define DEBUG_SCATTER (0x0020)
219 #define DEBUG_SCRIPT (0x0040)
220 #define DEBUG_TINY (0x0080)
221 #define DEBUG_TIMING (0x0100)
222 #define DEBUG_NEGO (0x0200)
223 #define DEBUG_TAGS (0x0400)
224 #define DEBUG_FREEZE (0x0800)
225 #define DEBUG_RESTART (0x1000)
228 ** Enable/Disable debug messages.
229 ** Can be changed at runtime too.
231 #ifdef SCSI_NCR_DEBUG
232 #define DEBUG_FLAGS ncr_debug
233 #else /* SCSI_NCR_DEBUG */
234 #define SCSI_NCR_DEBUG 0
235 #define DEBUG_FLAGS 0
236 #endif /* SCSI_NCR_DEBUG */
240 /*==========================================================
244 **==========================================================
246 ** modified copy from 386bsd:/usr/include/sys/assert.h
248 **----------------------------------------------------------
252 #define assert(expression) { \
253 if (!(expression)) { \
254 (void)kprintf("assertion \"%s\" failed: " \
255 "file \"%s\", line %d\n", \
256 #expression, __FILE__, __LINE__); \
261 #define assert(expression) { \
262 if (!(expression)) { \
263 (void)kprintf("assertion \"%s\" failed: " \
264 "file \"%s\", line %d\n", \
265 #expression, __FILE__, __LINE__); \
270 /*==========================================================
272 ** Access to the controller chip.
274 **==========================================================
277 #define INB(r) bus_space_read_1(np->bst, np->bsh, offsetof(struct ncr_reg, r))
278 #define INW(r) bus_space_read_2(np->bst, np->bsh, offsetof(struct ncr_reg, r))
279 #define INL(r) bus_space_read_4(np->bst, np->bsh, offsetof(struct ncr_reg, r))
281 #define OUTB(r, val) bus_space_write_1(np->bst, np->bsh, \
282 offsetof(struct ncr_reg, r), val)
283 #define OUTW(r, val) bus_space_write_2(np->bst, np->bsh, \
284 offsetof(struct ncr_reg, r), val)
285 #define OUTL(r, val) bus_space_write_4(np->bst, np->bsh, \
286 offsetof(struct ncr_reg, r), val)
287 #define OUTL_OFF(o, val) bus_space_write_4(np->bst, np->bsh, o, val)
289 #define INB_OFF(o) bus_space_read_1(np->bst, np->bsh, o)
290 #define INW_OFF(o) bus_space_read_2(np->bst, np->bsh, o)
291 #define INL_OFF(o) bus_space_read_4(np->bst, np->bsh, o)
293 #define READSCRIPT_OFF(base, off) \
294 (base ? *((volatile u_int32_t *)((volatile char *)base + (off))) : \
295 bus_space_read_4(np->bst2, np->bsh2, off))
297 #define WRITESCRIPT_OFF(base, off, val) \
300 *((volatile u_int32_t *) \
301 ((volatile char *)base + (off))) = (val); \
303 bus_space_write_4(np->bst2, np->bsh2, off, val); \
306 #define READSCRIPT(r) \
307 READSCRIPT_OFF(np->script, offsetof(struct script, r))
309 #define WRITESCRIPT(r, val) \
310 WRITESCRIPT_OFF(np->script, offsetof(struct script, r), val)
313 ** Set bit field ON, OFF
316 #define OUTONB(r, m) OUTB(r, INB(r) | (m))
317 #define OUTOFFB(r, m) OUTB(r, INB(r) & ~(m))
318 #define OUTONW(r, m) OUTW(r, INW(r) | (m))
319 #define OUTOFFW(r, m) OUTW(r, INW(r) & ~(m))
320 #define OUTONL(r, m) OUTL(r, INL(r) | (m))
321 #define OUTOFFL(r, m) OUTL(r, INL(r) & ~(m))
323 /*==========================================================
325 ** Command control block states.
327 **==========================================================
332 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
333 #define HS_DISCONNECT (3) /* Disconnected by target */
335 #define HS_COMPLETE (4)
336 #define HS_SEL_TIMEOUT (5) /* Selection timeout */
337 #define HS_RESET (6) /* SCSI reset */
338 #define HS_ABORTED (7) /* Transfer aborted */
339 #define HS_TIMEOUT (8) /* Software timeout */
340 #define HS_FAIL (9) /* SCSI or PCI bus errors */
341 #define HS_UNEXPECTED (10) /* Unexpected disconnect */
342 #define HS_STALL (11) /* QUEUE FULL or BUSY */
344 #define HS_DONEMASK (0xfc)
346 /*==========================================================
348 ** Software Interrupt Codes
350 **==========================================================
353 #define SIR_SENSE_RESTART (1)
354 #define SIR_SENSE_FAILED (2)
355 #define SIR_STALL_RESTART (3)
356 #define SIR_STALL_QUEUE (4)
357 #define SIR_NEGO_SYNC (5)
358 #define SIR_NEGO_WIDE (6)
359 #define SIR_NEGO_FAILED (7)
360 #define SIR_NEGO_PROTO (8)
361 #define SIR_REJECT_RECEIVED (9)
362 #define SIR_REJECT_SENT (10)
363 #define SIR_IGN_RESIDUE (11)
364 #define SIR_MISSING_SAVE (12)
367 /*==========================================================
369 ** Extended error codes.
370 ** xerr_status field of struct nccb.
372 **==========================================================
376 #define XE_EXTRA_DATA (1) /* unexpected data phase */
377 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
379 /*==========================================================
381 ** Negotiation status.
382 ** nego_status field of struct nccb.
384 **==========================================================
390 /*==========================================================
392 ** XXX These are no longer used. Remove once the
393 ** script is updated.
394 ** "Special features" of targets.
395 ** quirks field of struct tcb.
396 ** actualquirks field of struct nccb.
398 **==========================================================
401 #define QUIRK_AUTOSAVE (0x01)
402 #define QUIRK_NOMSG (0x02)
403 #define QUIRK_NOSYNC (0x10)
404 #define QUIRK_NOWIDE16 (0x20)
405 #define QUIRK_NOTAGS (0x40)
406 #define QUIRK_UPDATE (0x80)
408 /*==========================================================
412 **==========================================================
415 #define CCB_MAGIC (0xf2691ad2)
416 #define MAX_TAGS (32) /* hard limit */
418 /*==========================================================
422 **==========================================================
425 #define PRINT_ADDR(ccb) xpt_print_path((ccb)->ccb_h.path)
427 /*==========================================================
429 ** Declaration of structs.
431 **==========================================================
440 typedef struct ncb * ncb_p;
441 typedef struct tcb * tcb_p;
442 typedef struct lcb * lcb_p;
443 typedef struct nccb * nccb_p;
457 #define UC_SETSYNC 10
458 #define UC_SETTAGS 11
459 #define UC_SETDEBUG 12
460 #define UC_SETORDER 13
461 #define UC_SETWIDE 14
462 #define UC_SETFLAG 15
464 #define UF_TRACE (0x01)
466 /*---------------------------------------
468 ** Timestamps for profiling
470 **---------------------------------------
473 /* Type of the kernel variable `ticks'. XXX should be declared with the var. */
487 ** profiling data (per device)
503 /*==========================================================
505 ** Declaration of structs: target control block
507 **==========================================================
510 #define NCR_TRANS_CUR 0x01 /* Modify current neogtiation status */
511 #define NCR_TRANS_ACTIVE 0x03 /* Assume this is the active target */
512 #define NCR_TRANS_GOAL 0x04 /* Modify negotiation goal */
513 #define NCR_TRANS_USER 0x08 /* Modify user negotiation settings */
515 struct ncr_transinfo {
521 struct ncr_target_tinfo {
522 /* Hardware version of our sync settings */
524 #define NCR_CUR_DISCENB 0x01
525 #define NCR_CUR_TAGENB 0x02
526 #define NCR_USR_DISCENB 0x04
527 #define NCR_USR_TAGENB 0x08
529 struct ncr_transinfo current;
530 struct ncr_transinfo goal;
531 struct ncr_transinfo user;
532 /* Hardware version of our wide settings */
538 ** during reselection the ncr jumps to this point
539 ** with SFBR set to the encoded target number
541 ** if it's not this target, jump to the next.
543 ** JUMP IF (SFBR != #target#)
547 struct link jump_tcb;
550 ** load the actual values for the sxfer and the scntl3
551 ** register (sync/wide mode).
554 ** @(sval field of this tcb)
557 ** @(wval field of this tcb)
558 ** @(scntl3 register)
564 ** if next message is "identify"
565 ** then load the message to SFBR,
566 ** else load 0 to SFBR.
572 struct link call_lun;
575 ** now look for the right lun.
578 ** @(first nccb of this lun)
581 struct link jump_lcb;
584 ** pointer to interrupted getcc nccb
590 ** pointer to nccb used for negotiating.
591 ** Avoid to start a nego for all queued commands
592 ** when tagged command queuing is enabled.
605 ** user settable limits for sync transfer
606 ** and tagged commands.
609 struct ncr_target_tinfo tinfo;
612 ** the lcb's of this tcb
618 /*==========================================================
620 ** Declaration of structs: lun control block
622 **==========================================================
627 ** during reselection the ncr jumps to this point
628 ** with SFBR set to the "Identify" message.
629 ** if it's not this lun, jump to the next.
631 ** JUMP IF (SFBR != #lun#)
632 ** @(next lcb of this target)
635 struct link jump_lcb;
638 ** if next message is "simple tag",
639 ** then load the tag to SFBR,
640 ** else load 0 to SFBR.
646 struct link call_tag;
649 ** now look for the right nccb.
652 ** @(first nccb of this lun)
655 struct link jump_nccb;
658 ** start of the nccb chain
664 ** Control of tagged queueing
674 /*==========================================================
676 ** Declaration of structs: COMMAND control block
678 **==========================================================
680 ** This substructure is copied from the nccb to a
681 ** global address after selection (or reselection)
682 ** and copied back before disconnect.
684 ** These fields are accessible to the script processor.
686 **----------------------------------------------------------
691 ** Execution of a nccb starts at this point.
692 ** It's a jump to the "SELECT" label
695 ** After successful selection the script
696 ** processor overwrites it with a jump to
697 ** the IDLE label of the script.
703 ** Saved data pointer.
704 ** Points to the position in the script
705 ** responsible for the actual transfer
707 ** It's written after reception of a
708 ** "SAVE_DATA_POINTER" message.
709 ** The goalpointer points after
710 ** the last transfer command.
718 ** The virtual address of the nccb
719 ** containing this header.
725 ** space for some timestamps to gather
726 ** profiling data about devices and this driver.
739 ** The status bytes are used by the host and the script processor.
741 ** The first four byte are copied to the scratchb register
742 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
743 ** and copied back just after disconnecting.
744 ** Inside the script the XX_REG are used.
746 ** The last four bytes are used inside the script by "COPY" commands.
747 ** Because source and destination must have the same alignment
748 ** in a longword, the fields HAVE to be at the choosen offsets.
749 ** xerr_st (4) 0 (0x34) scratcha
750 ** sync_st (5) 1 (0x05) sxfer
751 ** wide_st (7) 3 (0x03) scntl3
755 ** First four bytes (script)
759 #define HS_PRT nc_scr1
764 ** First four bytes (host)
766 #define actualquirks phys.header.status[0]
767 #define host_status phys.header.status[1]
768 #define s_status phys.header.status[2]
769 #define parity_status phys.header.status[3]
772 ** Last four bytes (script)
774 #define xerr_st header.status[4] /* MUST be ==0 mod 4 */
775 #define sync_st header.status[5] /* MUST be ==1 mod 4 */
776 #define nego_st header.status[6]
777 #define wide_st header.status[7] /* MUST be ==3 mod 4 */
780 ** Last four bytes (host)
782 #define xerr_status phys.xerr_st
783 #define sync_status phys.sync_st
784 #define nego_status phys.nego_st
785 #define wide_status phys.wide_st
787 /*==========================================================
789 ** Declaration of structs: Data structure block
791 **==========================================================
793 ** During execution of a nccb by the script processor,
794 ** the DSA (data structure address) register points
795 ** to this substructure of the nccb.
796 ** This substructure contains the header with
797 ** the script-processor-changable data and
798 ** data blocks for the indirect move commands.
800 **----------------------------------------------------------
807 ** Has to be the first entry,
808 ** because it's jumped to by the
815 ** Table data for Script
818 struct scr_tblsel select;
819 struct scr_tblmove smsg ;
820 struct scr_tblmove smsg2 ;
821 struct scr_tblmove cmd ;
822 struct scr_tblmove scmd ;
823 struct scr_tblmove sense ;
824 struct scr_tblmove data [MAX_SCATTER];
827 /*==========================================================
829 ** Declaration of structs: Command control block.
831 **==========================================================
833 ** During execution of a nccb by the script processor,
834 ** the DSA (data structure address) register points
835 ** to this substructure of the nccb.
836 ** This substructure contains the header with
837 ** the script-processor-changable data and then
838 ** data blocks for the indirect move commands.
840 **----------------------------------------------------------
846 ** This filler ensures that the global header is
847 ** cache line size aligned.
852 ** during reselection the ncr jumps to this point.
853 ** If a "SIMPLE_TAG" message was received,
854 ** then SFBR is set to the tag.
855 ** else SFBR is set to 0
856 ** If looking for another tag, jump to the next nccb.
858 ** JUMP IF (SFBR != #TAG#)
859 ** @(next nccb of this lun)
862 struct link jump_nccb;
865 ** After execution of this call, the return address
866 ** (in the TEMP register) points to the following
867 ** data structure block.
868 ** So copy it to the DSA register, and start
869 ** processing of this data structure.
875 struct link call_tmp;
878 ** This is the data structure which is
879 ** to be executed by the script processor.
885 ** If a data transfer phase is terminated too early
886 ** (after reception of a message (i.e. DISCONNECT)),
887 ** we have to prepare a mini script to transfer
888 ** the rest of the data.
894 ** The general SCSI driver provides a
895 ** pointer to a control block.
901 ** We prepare a message to be sent after selection,
902 ** and a second one to be sent after getcc selection.
903 ** Contents are IDENTIFY and SIMPLE_TAG.
904 ** While negotiating sync or wide transfer,
905 ** a SDTM or WDTM message is appended.
908 u_char scsi_smsg [8];
909 u_char scsi_smsg2[8];
913 ** Flag is used while looking for a free nccb.
919 ** Physical address of this instance of nccb
925 ** Completion time out for this job.
926 ** It's set to time of start + allowed number of seconds.
932 ** All nccbs of one hostadapter are chained.
938 ** All nccbs of one target/lun are chained.
950 ** Tag for this transfer.
951 ** It's patched into jump_nccb.
952 ** If it's not zero, a SIMPLE_TAG
953 ** message is included in smsg.
959 #define CCB_PHYS(cp,lbl) (cp->p_nccb + offsetof(struct nccb, lbl))
961 /*==========================================================
963 ** Declaration of structs: NCR device descriptor
965 **==========================================================
970 ** The global header.
971 ** Accessible to both the host and the
973 ** We assume it is cache line size aligned.
979 /*-----------------------------------------------
981 **-----------------------------------------------
983 ** During reselection the ncr jumps to this point.
984 ** The SFBR register is loaded with the encoded target id.
986 ** Jump to the first target.
991 struct link jump_tcb;
993 /*-----------------------------------------------
995 **-----------------------------------------------
997 ** virtual and physical addresses
998 ** of the 53c810 chip.
1001 struct resource *reg_res;
1002 bus_space_tag_t bst;
1003 bus_space_handle_t bsh;
1006 struct resource *sram_res;
1007 bus_space_tag_t bst2;
1008 bus_space_handle_t bsh2;
1010 struct resource *irq_res;
1014 ** Scripts instance virtual address.
1016 struct script *script;
1017 struct scripth *scripth;
1020 ** Scripts instance physical address.
1026 ** The SCSI address of the host adapter.
1031 ** timing parameters
1033 u_char minsync; /* Minimum sync period factor */
1034 u_char maxsync; /* Maximum sync period factor */
1035 u_char maxoffs; /* Max scsi offset */
1036 u_char clock_divn; /* Number of clock divisors */
1037 u_long clock_khz; /* SCSI clock frequency in KHz */
1038 u_long features; /* Chip features map */
1039 u_char multiplier; /* Clock multiplier (1,2,4) */
1041 u_char maxburst; /* log base 2 of dwords burst */
1044 ** BIOS supplied PCI bus options
1055 /*-----------------------------------------------
1056 ** CAM SIM information for this instance
1057 **-----------------------------------------------
1060 struct cam_sim *sim;
1061 struct cam_path *path;
1063 /*-----------------------------------------------
1065 **-----------------------------------------------
1067 ** Commands from user
1074 struct tcb target[MAX_TARGET];
1079 u_int32_t squeue [MAX_START];
1089 struct callout timeout_ch;
1091 /*-----------------------------------------------
1092 ** Debug and profiling
1093 **-----------------------------------------------
1097 struct ncr_reg regdump;
1103 struct profile profile;
1108 ** Head of list of all nccbs for this controller.
1114 ** Should be longword aligned,
1115 ** because they're written with a
1116 ** COPY script command.
1123 ** Buffer for STATUS_IN phase.
1128 ** controller chip dependent maximal transfer width.
1134 ** address of the ncr control registers in io space
1140 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1141 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1143 /*==========================================================
1146 ** Script for NCR-Processor.
1148 ** Use ncr_script_fill() to create the variable parts.
1149 ** Use ncr_script_copy_and_bind() to make a copy and
1150 ** bind to physical addresses.
1153 **==========================================================
1155 ** We have to know the offsets of all labels before
1156 ** we reach them (for forward jumps).
1157 ** Therefore we declare a struct here.
1158 ** If you make changes inside the script,
1159 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1161 **----------------------------------------------------------
1165 ** Script fragments which are loaded into the on-board RAM
1166 ** of 825A, 875 and 895 chips.
1172 ncrcmd startpos [ 1];
1177 ncrcmd select [ 18];
1178 ncrcmd prepare [ 4];
1179 ncrcmd loadpos [ 14];
1180 ncrcmd prepare2 [ 24];
1183 ncrcmd dispatch [ 33];
1184 ncrcmd no_data [ 17];
1185 ncrcmd checkatn [ 10];
1186 ncrcmd command [ 15];
1187 ncrcmd status [ 27];
1188 ncrcmd msg_in [ 26];
1189 ncrcmd msg_bad [ 6];
1190 ncrcmd complete [ 13];
1191 ncrcmd cleanup [ 12];
1192 ncrcmd cleanup0 [ 9];
1193 ncrcmd signal [ 12];
1194 ncrcmd save_dp [ 5];
1195 ncrcmd restore_dp [ 5];
1196 ncrcmd disconnect [ 12];
1197 ncrcmd disconnect0 [ 5];
1198 ncrcmd disconnect1 [ 23];
1199 ncrcmd msg_out [ 9];
1200 ncrcmd msg_out_done [ 7];
1201 ncrcmd badgetcc [ 6];
1202 ncrcmd reselect [ 8];
1203 ncrcmd reselect1 [ 8];
1204 ncrcmd reselect2 [ 8];
1205 ncrcmd resel_tmp [ 5];
1206 ncrcmd resel_lun [ 18];
1207 ncrcmd resel_tag [ 24];
1208 ncrcmd data_in [MAX_SCATTER * 4 + 7];
1209 ncrcmd data_out [MAX_SCATTER * 4 + 7];
1213 ** Script fragments which stay in main memory for all chips.
1216 ncrcmd tryloop [MAX_START*5+2];
1217 ncrcmd msg_parity [ 6];
1218 ncrcmd msg_reject [ 8];
1219 ncrcmd msg_ign_residue [ 32];
1220 ncrcmd msg_extended [ 18];
1221 ncrcmd msg_ext_2 [ 18];
1222 ncrcmd msg_wdtr [ 27];
1223 ncrcmd msg_ext_3 [ 18];
1224 ncrcmd msg_sdtr [ 27];
1225 ncrcmd msg_out_abort [ 10];
1228 #ifdef NCR_GETCC_WITHMSG
1229 ncrcmd getcc2 [ 29];
1231 ncrcmd getcc2 [ 14];
1234 ncrcmd aborttag [ 4];
1236 ncrcmd snooptest [ 9];
1237 ncrcmd snoopend [ 2];
1240 /*==========================================================
1243 ** Function headers.
1246 **==========================================================
1250 static nccb_p ncr_alloc_nccb (ncb_p np, u_long target, u_long lun);
1251 static void ncr_complete (ncb_p np, nccb_p cp);
1252 static int ncr_delta (int * from, int * to);
1253 static void ncr_exception (ncb_p np);
1254 static void ncr_free_nccb (ncb_p np, nccb_p cp);
1255 static void ncr_freeze_devq (ncb_p np, struct cam_path *path);
1256 static void ncr_selectclock (ncb_p np, u_char scntl3);
1257 static void ncr_getclock (ncb_p np, u_char multiplier);
1258 static nccb_p ncr_get_nccb (ncb_p np, u_long t,u_long l);
1260 static u_int32_t ncr_info (int unit);
1262 static void ncr_init (ncb_p np, char * msg, u_long code);
1263 static void ncr_intr (void *vnp);
1264 static void ncr_int_ma (ncb_p np, u_char dstat);
1265 static void ncr_int_sir (ncb_p np);
1266 static void ncr_int_sto (ncb_p np);
1268 static void ncr_min_phys (struct buf *bp);
1270 static void ncr_poll (struct cam_sim *sim);
1271 static void ncb_profile (ncb_p np, nccb_p cp);
1272 static void ncr_script_copy_and_bind
1273 (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
1274 static void ncr_script_fill (struct script * scr, struct scripth *scrh);
1275 static int ncr_scatter (struct dsb* phys, vm_offset_t vaddr,
1277 static void ncr_getsync (ncb_p np, u_char sfac, u_char *fakp,
1279 static void ncr_setsync (ncb_p np, nccb_p cp,u_char scntl3,u_char sxfer,
1281 static void ncr_setwide (ncb_p np, nccb_p cp, u_char wide, u_char ack);
1282 static int ncr_show_msg (u_char * msg);
1283 static int ncr_snooptest (ncb_p np);
1284 static void ncr_action (struct cam_sim *sim, union ccb *ccb);
1285 static void ncr_timeout (void *arg);
1286 static void ncr_wakeup (ncb_p np, u_long code);
1288 static int ncr_probe (device_t dev);
1289 static int ncr_attach (device_t dev);
1291 #endif /* _KERNEL */
1293 /*==========================================================
1296 ** Global static data.
1299 **==========================================================
1304 * $FreeBSD: src/sys/pci/ncr.c,v 1.155.2.3 2001/03/05 13:09:10 obrien Exp $
1306 static const u_long ncr_version = NCR_VERSION * 11
1307 + (u_long) sizeof (struct ncb) * 7
1308 + (u_long) sizeof (struct nccb) * 5
1309 + (u_long) sizeof (struct lcb) * 3
1310 + (u_long) sizeof (struct tcb) * 2;
1314 static int ncr_debug = SCSI_NCR_DEBUG;
1315 SYSCTL_INT(_debug, OID_AUTO, ncr_debug, CTLFLAG_RW, &ncr_debug, 0,
1316 "Driver debug flags");
1318 static int ncr_cache; /* to be aligned _NOT_ static */
1320 /*==========================================================
1323 ** Global static data: auto configure
1326 **==========================================================
1329 #define NCR_810_ID (0x00011000ul)
1330 #define NCR_815_ID (0x00041000ul)
1331 #define NCR_820_ID (0x00021000ul)
1332 #define NCR_825_ID (0x00031000ul)
1333 #define NCR_860_ID (0x00061000ul)
1334 #define NCR_875_ID (0x000f1000ul)
1335 #define NCR_875_ID2 (0x008f1000ul)
1336 #define NCR_885_ID (0x000d1000ul)
1337 #define NCR_895_ID (0x000c1000ul)
1338 #define NCR_896_ID (0x000b1000ul)
1339 #define NCR_895A_ID (0x00121000ul)
1340 #define NCR_1510D_ID (0x000a1000ul)
1343 static char *ncr_name (ncb_p np)
1345 static char name[10];
1346 ksnprintf(name, sizeof(name), "ncr%d", np->unit);
1350 /*==========================================================
1353 ** Scripts for NCR-Processor.
1355 ** Use ncr_script_bind for binding to physical addresses.
1358 **==========================================================
1360 ** NADDR generates a reference to a field of the controller data.
1361 ** PADDR generates a reference to another part of the script.
1362 ** RADDR generates a reference to a script processor register.
1363 ** FADDR generates a reference to a script processor register
1366 **----------------------------------------------------------
1369 #define RELOC_SOFTC 0x40000000
1370 #define RELOC_LABEL 0x50000000
1371 #define RELOC_REGISTER 0x60000000
1372 #define RELOC_KVAR 0x70000000
1373 #define RELOC_LABELH 0x80000000
1374 #define RELOC_MASK 0xf0000000
1376 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
1377 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
1378 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
1379 #define RADDR(label) (RELOC_REGISTER | REG(label))
1380 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
1381 #define KVAR(which) (RELOC_KVAR | (which))
1383 #define KVAR_SECOND (0)
1384 #define KVAR_TICKS (1)
1385 #define KVAR_NCR_CACHE (2)
1387 #define SCRIPT_KVAR_FIRST (0)
1388 #define SCRIPT_KVAR_LAST (3)
1391 * Kernel variables referenced in the scripts.
1392 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
1394 static void *script_kvars[] =
1395 { &time_second, &ticks, &ncr_cache };
1397 static struct script script0 = {
1398 /*--------------------------< START >-----------------------*/ {
1400 ** Claim to be still alive ...
1402 SCR_COPY (sizeof (((struct ncb *)0)->heartbeat)),
1406 ** Make data structure address invalid.
1409 SCR_LOAD_REG (dsa, 0xff),
1411 SCR_FROM_REG (ctest2),
1413 }/*-------------------------< START0 >----------------------*/,{
1415 ** Hook for interrupted GetConditionCode.
1416 ** Will be patched to ... IFTRUE by
1417 ** the interrupt handler.
1419 SCR_INT ^ IFFALSE (0),
1422 }/*-------------------------< START1 >----------------------*/,{
1424 ** Hook for stalled start queue.
1425 ** Will be patched to IFTRUE by the interrupt handler.
1427 SCR_INT ^ IFFALSE (0),
1430 ** Then jump to a certain point in tryloop.
1431 ** Due to the lack of indirect addressing the code
1432 ** is self modifying here.
1435 }/*-------------------------< STARTPOS >--------------------*/,{
1438 }/*-------------------------< TRYSEL >----------------------*/,{
1441 ** DSA: Address of a Data Structure
1442 ** or Address of the IDLE-Label.
1444 ** TEMP: Address of a script, which tries to
1445 ** start the NEXT entry.
1447 ** Save the TEMP register into the SCRATCHA register.
1448 ** Then copy the DSA to TEMP and RETURN.
1449 ** This is kind of an indirect jump.
1450 ** (The script processor has NO stack, so the
1451 ** CALL is actually a jump and link, and the
1452 ** RETURN is an indirect jump.)
1454 ** If the slot was empty, DSA contains the address
1455 ** of the IDLE part of this script. The processor
1456 ** jumps to IDLE and waits for a reselect.
1457 ** It will wake up and try the same slot again
1458 ** after the SIGP bit becomes set by the host.
1460 ** If the slot was not empty, DSA contains
1461 ** the address of the phys-part of a nccb.
1462 ** The processor jumps to this address.
1463 ** phys starts with head,
1464 ** head starts with launch,
1465 ** so actually the processor jumps to
1467 ** If the entry is scheduled for execution,
1468 ** then launch contains a jump to SELECT.
1469 ** If it's not scheduled, it contains a jump to IDLE.
1480 }/*-------------------------< SKIP >------------------------*/,{
1482 ** This entry has been canceled.
1483 ** Next time use the next slot.
1489 ** patch the launch field.
1490 ** should look like an idle process.
1497 }/*-------------------------< SKIP2 >-----------------------*/,{
1501 }/*-------------------------< IDLE >------------------------*/,{
1504 ** Wait for reselect.
1509 }/*-------------------------< SELECT >----------------------*/,{
1511 ** DSA contains the address of a scheduled
1514 ** SCRATCHA contains the address of the script,
1515 ** which starts the next entry.
1517 ** Set Initiator mode.
1519 ** (Target mode is left as an exercise for the reader)
1524 SCR_LOAD_REG (HS_REG, 0xff),
1528 ** And try to select this target.
1530 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
1534 ** Now there are 4 possibilities:
1536 ** (1) The ncr looses arbitration.
1537 ** This is ok, because it will try again,
1538 ** when the bus becomes idle.
1539 ** (But beware of the timeout function!)
1541 ** (2) The ncr is reselected.
1542 ** Then the script processor takes the jump
1543 ** to the RESELECT label.
1545 ** (3) The ncr completes the selection.
1546 ** Then it will execute the next statement.
1548 ** (4) There is a selection timeout.
1549 ** Then the ncr should interrupt the host and stop.
1550 ** Unfortunately, it seems to continue execution
1551 ** of the script. But it will fail with an
1552 ** IID-interrupt on the next WHEN.
1555 SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
1559 ** Send the IDENTIFY and SIMPLE_TAG messages
1560 ** (and the MSG_EXT_SDTR message)
1562 SCR_MOVE_TBL ^ SCR_MSG_OUT,
1563 offsetof (struct dsb, smsg),
1564 #ifdef undef /* XXX better fail than try to deal with this ... */
1565 SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_OUT)),
1574 ** Selection complete.
1575 ** Next time use the next slot.
1580 }/*-------------------------< PREPARE >----------------------*/,{
1582 ** The ncr doesn't have an indirect load
1583 ** or store command. So we have to
1584 ** copy part of the control block to a
1585 ** fixed place, where we can access it.
1587 ** We patch the address part of a
1588 ** COPY command with the DSA-register.
1594 ** then we do the actual copy.
1596 SCR_COPY (sizeof (struct head)),
1598 ** continued after the next label ...
1601 }/*-------------------------< LOADPOS >---------------------*/,{
1605 ** Mark this nccb as not scheduled.
1609 NADDR (header.launch),
1611 ** Set a time stamp for this selection
1613 SCR_COPY (sizeof (ticks)),
1615 NADDR (header.stamp.select),
1617 ** load the savep (saved pointer) into
1618 ** the TEMP register (actual pointer)
1621 NADDR (header.savep),
1624 ** Initialize the status registers
1627 NADDR (header.status),
1630 }/*-------------------------< PREPARE2 >---------------------*/,{
1632 ** Load the synchronous mode register
1638 ** Load the wide mode and timing register
1644 ** Initialize the msgout buffer with a NOOP message.
1646 SCR_LOAD_REG (scratcha, MSG_NOOP),
1655 ** Message in phase ?
1657 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
1660 ** Extended or reject message ?
1662 SCR_FROM_REG (sbdl),
1664 SCR_JUMP ^ IFTRUE (DATA (MSG_EXTENDED)),
1666 SCR_JUMP ^ IFTRUE (DATA (MSG_MESSAGE_REJECT)),
1667 PADDRH (msg_reject),
1669 ** normal processing
1673 }/*-------------------------< SETMSG >----------------------*/,{
1679 }/*-------------------------< CLRACK >----------------------*/,{
1681 ** Terminate possible pending message phase.
1686 }/*-----------------------< DISPATCH >----------------------*/,{
1687 SCR_FROM_REG (HS_REG),
1689 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
1692 ** remove bogus output signals
1694 SCR_REG_REG (socl, SCR_AND, CACK|CATN),
1696 SCR_RETURN ^ IFTRUE (WHEN (SCR_DATA_OUT)),
1698 SCR_RETURN ^ IFTRUE (IF (SCR_DATA_IN)),
1700 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
1702 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN)),
1704 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
1706 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
1709 ** Discard one illegal phase byte, if required.
1711 SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
1716 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
1718 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
1720 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
1722 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
1727 }/*-------------------------< NO_DATA >--------------------*/,{
1729 ** The target wants to tranfer too much data
1730 ** or in the wrong direction.
1731 ** Remember that in extended error.
1733 SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
1739 ** Discard one data byte, if required.
1741 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
1743 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
1745 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1747 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
1750 ** .. and repeat as required.
1756 }/*-------------------------< CHECKATN >--------------------*/,{
1758 ** If AAP (bit 1 of scntl0 register) is set
1759 ** and a parity error is detected,
1760 ** the script processor asserts ATN.
1762 ** The target should switch to a MSG_OUT phase
1763 ** to get the message.
1765 SCR_FROM_REG (socl),
1767 SCR_JUMP ^ IFFALSE (MASK (CATN, CATN)),
1772 SCR_REG_REG (PS_REG, SCR_ADD, 1),
1775 ** Prepare a MSG_INITIATOR_DET_ERR message
1776 ** (initiator detected error).
1777 ** The target should retry the transfer.
1779 SCR_LOAD_REG (scratcha, MSG_INITIATOR_DET_ERR),
1784 }/*-------------------------< COMMAND >--------------------*/,{
1786 ** If this is not a GETCC transfer ...
1788 SCR_FROM_REG (SS_REG),
1790 /*<<<*/ SCR_JUMPR ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
1793 ** ... set a timestamp ...
1795 SCR_COPY (sizeof (ticks)),
1797 NADDR (header.stamp.command),
1799 ** ... and send the command
1801 SCR_MOVE_TBL ^ SCR_COMMAND,
1802 offsetof (struct dsb, cmd),
1806 ** Send the GETCC command
1808 /*>>>*/ SCR_MOVE_TBL ^ SCR_COMMAND,
1809 offsetof (struct dsb, scmd),
1813 }/*-------------------------< STATUS >--------------------*/,{
1815 ** set the timestamp.
1817 SCR_COPY (sizeof (ticks)),
1819 NADDR (header.stamp.status),
1821 ** If this is a GETCC transfer,
1823 SCR_FROM_REG (SS_REG),
1825 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (SCSI_STATUS_CHECK_COND)),
1830 SCR_MOVE_ABS (1) ^ SCR_STATUS,
1833 ** Save status to scsi_status.
1834 ** Mark as complete.
1835 ** And wait for disconnect.
1837 SCR_TO_REG (SS_REG),
1839 SCR_REG_REG (SS_REG, SCR_OR, SCSI_STATUS_SENSE),
1841 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1846 ** If it was no GETCC transfer,
1847 ** save the status to scsi_status.
1849 /*>>>*/ SCR_MOVE_ABS (1) ^ SCR_STATUS,
1851 SCR_TO_REG (SS_REG),
1854 ** if it was no check condition ...
1856 SCR_JUMP ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
1859 ** ... mark as complete.
1861 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1866 }/*-------------------------< MSG_IN >--------------------*/,{
1868 ** Get the first byte of the message
1869 ** and save it to SCRATCHA.
1871 ** The script processor doesn't negate the
1872 ** ACK signal after this transfer.
1874 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
1877 ** Check for message parity error.
1879 SCR_TO_REG (scratcha),
1881 SCR_FROM_REG (socl),
1883 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
1884 PADDRH (msg_parity),
1885 SCR_FROM_REG (scratcha),
1888 ** Parity was ok, handle this message.
1890 SCR_JUMP ^ IFTRUE (DATA (MSG_CMDCOMPLETE)),
1892 SCR_JUMP ^ IFTRUE (DATA (MSG_SAVEDATAPOINTER)),
1894 SCR_JUMP ^ IFTRUE (DATA (MSG_RESTOREPOINTERS)),
1896 SCR_JUMP ^ IFTRUE (DATA (MSG_DISCONNECT)),
1898 SCR_JUMP ^ IFTRUE (DATA (MSG_EXTENDED)),
1899 PADDRH (msg_extended),
1900 SCR_JUMP ^ IFTRUE (DATA (MSG_NOOP)),
1902 SCR_JUMP ^ IFTRUE (DATA (MSG_MESSAGE_REJECT)),
1903 PADDRH (msg_reject),
1904 SCR_JUMP ^ IFTRUE (DATA (MSG_IGN_WIDE_RESIDUE)),
1905 PADDRH (msg_ign_residue),
1907 ** Rest of the messages left as
1910 ** Unimplemented messages:
1911 ** fall through to MSG_BAD.
1913 }/*-------------------------< MSG_BAD >------------------*/,{
1915 ** unimplemented message - reject it.
1919 SCR_LOAD_REG (scratcha, MSG_MESSAGE_REJECT),
1924 }/*-------------------------< COMPLETE >-----------------*/,{
1926 ** Complete message.
1928 ** If it's not the get condition code,
1929 ** copy TEMP register to LASTP in header.
1931 SCR_FROM_REG (SS_REG),
1933 /*<<<*/ SCR_JUMPR ^ IFTRUE (MASK (SCSI_STATUS_SENSE, SCSI_STATUS_SENSE)),
1937 NADDR (header.lastp),
1939 ** When we terminate the cycle by clearing ACK,
1940 ** the target may disconnect immediately.
1942 ** We don't want to be told of an
1943 ** "unexpected disconnect",
1944 ** so we disable this feature.
1946 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
1949 ** Terminate cycle ...
1951 SCR_CLR (SCR_ACK|SCR_ATN),
1954 ** ... and wait for the disconnect.
1958 }/*-------------------------< CLEANUP >-------------------*/,{
1960 ** dsa: Pointer to nccb
1961 ** or xxxxxxFF (no nccb)
1963 ** HS_REG: Host-Status (<>0!)
1967 SCR_JUMP ^ IFTRUE (DATA (0xff)),
1971 ** save the status registers
1975 NADDR (header.status),
1977 ** and copy back the header to the nccb.
1982 SCR_COPY (sizeof (struct head)),
1984 }/*-------------------------< CLEANUP0 >--------------------*/,{
1988 ** If command resulted in "check condition"
1989 ** status and is not yet completed,
1990 ** try to get the condition code.
1992 SCR_FROM_REG (HS_REG),
1994 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
1996 SCR_FROM_REG (SS_REG),
1998 SCR_JUMP ^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND)),
2000 }/*-------------------------< SIGNAL >----------------------*/,{
2002 ** if status = queue full,
2003 ** reinsert in startqueue and stall queue.
2005 /*>>>*/ SCR_FROM_REG (SS_REG),
2007 SCR_INT ^ IFTRUE (DATA (SCSI_STATUS_QUEUE_FULL)),
2010 ** And make the DSA register invalid.
2012 SCR_LOAD_REG (dsa, 0xff), /* invalid */
2015 ** if job completed ...
2017 SCR_FROM_REG (HS_REG),
2020 ** ... signal completion to the host
2022 SCR_INT_FLY ^ IFFALSE (MASK (0, HS_DONEMASK)),
2025 ** Auf zu neuen Schandtaten!
2030 }/*-------------------------< SAVE_DP >------------------*/,{
2033 ** Copy TEMP register to SAVEP in header.
2037 NADDR (header.savep),
2040 }/*-------------------------< RESTORE_DP >---------------*/,{
2042 ** RESTORE_DP message:
2043 ** Copy SAVEP in header to TEMP register.
2046 NADDR (header.savep),
2051 }/*-------------------------< DISCONNECT >---------------*/,{
2053 ** If QUIRK_AUTOSAVE is set,
2054 ** do an "save pointer" operation.
2056 SCR_FROM_REG (QU_REG),
2058 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
2061 ** like SAVE_DP message:
2062 ** Copy TEMP register to SAVEP in header.
2066 NADDR (header.savep),
2068 ** Check if temp==savep or temp==goalp:
2069 ** if not, log a missing save pointer message.
2070 ** In fact, it's a comparison mod 256.
2072 ** Hmmm, I hadn't thought that I would be urged to
2073 ** write this kind of ugly self modifying code.
2075 ** It's unbelievable, but the ncr53c8xx isn't able
2076 ** to subtract one register from another.
2078 SCR_FROM_REG (temp),
2081 ** You are not expected to understand this ..
2083 ** CAUTION: only little endian architectures supported! XXX
2086 NADDR (header.savep),
2087 PADDR (disconnect0),
2088 }/*-------------------------< DISCONNECT0 >--------------*/,{
2089 /*<<<*/ SCR_JUMPR ^ IFTRUE (DATA (1)),
2095 NADDR (header.goalp),
2096 PADDR (disconnect1),
2097 }/*-------------------------< DISCONNECT1 >--------------*/,{
2098 SCR_INT ^ IFFALSE (DATA (1)),
2103 ** DISCONNECTing ...
2105 ** disable the "unexpected disconnect" feature,
2106 ** and remove the ACK signal.
2108 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2110 SCR_CLR (SCR_ACK|SCR_ATN),
2113 ** Wait for the disconnect.
2119 ** Set a time stamp,
2120 ** and count the disconnects.
2122 SCR_COPY (sizeof (ticks)),
2124 NADDR (header.stamp.disconnect),
2128 SCR_REG_REG (temp, SCR_ADD, 0x01),
2134 ** Status is: DISCONNECTED.
2136 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2141 }/*-------------------------< MSG_OUT >-------------------*/,{
2143 ** The target requests a message.
2145 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2151 ** If it was no ABORT message ...
2153 SCR_JUMP ^ IFTRUE (DATA (MSG_ABORT)),
2154 PADDRH (msg_out_abort),
2156 ** ... wait for the next phase
2157 ** if it's a message out, send it again, ...
2159 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2161 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2163 ** ... else clear the message ...
2165 SCR_LOAD_REG (scratcha, MSG_NOOP),
2171 ** ... and process the next phase
2176 }/*------------------------< BADGETCC >---------------------*/,{
2178 ** If SIGP was set, clear it and try again.
2180 SCR_FROM_REG (ctest2),
2182 SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
2186 }/*-------------------------< RESELECT >--------------------*/,{
2188 ** This NOP will be patched with LED OFF
2189 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2195 ** make the DSA invalid.
2197 SCR_LOAD_REG (dsa, 0xff),
2202 ** Sleep waiting for a reselection.
2203 ** If SIGP is set, special treatment.
2205 ** Zu allem bereit ..
2209 }/*-------------------------< RESELECT1 >--------------------*/,{
2211 ** This NOP will be patched with LED ON
2212 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2217 ** ... zu nichts zu gebrauchen ?
2219 ** load the target id into the SFBR
2220 ** and jump to the control block.
2222 ** Look at the declarations of
2227 ** to understand what's going on.
2229 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2235 }/*-------------------------< RESELECT2 >-------------------*/,{
2237 ** This NOP will be patched with LED ON
2238 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2243 ** If it's not connected :(
2244 ** -> interrupted by SIGP bit.
2247 SCR_FROM_REG (ctest2),
2249 SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
2254 }/*-------------------------< RESEL_TMP >-------------------*/,{
2256 ** The return address in TEMP
2257 ** is in fact the data structure address,
2258 ** so copy it to the DSA register.
2266 }/*-------------------------< RESEL_LUN >-------------------*/,{
2268 ** come back to this point
2269 ** to get an IDENTIFY message
2270 ** Wait for a msg_in phase.
2272 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2276 ** It's not a sony, it's a trick:
2277 ** read the data without acknowledging it.
2279 SCR_FROM_REG (sbdl),
2281 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (MSG_IDENTIFYFLAG, 0x98)),
2284 ** It WAS an Identify message.
2285 ** get it and ack it!
2287 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2292 ** Mask out the lun.
2294 SCR_REG_REG (sfbr, SCR_AND, 0x07),
2299 ** No message phase or no IDENTIFY message:
2302 /*>>>*/ SCR_LOAD_SFBR (0),
2307 }/*-------------------------< RESEL_TAG >-------------------*/,{
2309 ** come back to this point
2310 ** to get a SIMPLE_TAG message
2311 ** Wait for a MSG_IN phase.
2313 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2317 ** It's a trick - read the data
2318 ** without acknowledging it.
2320 SCR_FROM_REG (sbdl),
2322 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (MSG_SIMPLE_Q_TAG)),
2325 ** It WAS a SIMPLE_TAG message.
2326 ** get it and ack it!
2328 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2333 ** Wait for the second byte (the tag)
2335 /*<<<*/ SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2338 ** Get it and ack it!
2340 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2342 SCR_CLR (SCR_ACK|SCR_CARRY),
2347 ** No message phase or no SIMPLE_TAG message
2348 ** or no second byte: return 0.
2350 /*>>>*/ SCR_LOAD_SFBR (0),
2352 SCR_SET (SCR_CARRY),
2357 }/*-------------------------< DATA_IN >--------------------*/,{
2359 ** Because the size depends on the
2360 ** #define MAX_SCATTER parameter,
2361 ** it is filled in at runtime.
2363 ** SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2365 ** SCR_COPY (sizeof (ticks)),
2366 ** KVAR (KVAR_TICKS),
2367 ** NADDR (header.stamp.data),
2368 ** SCR_MOVE_TBL ^ SCR_DATA_IN,
2369 ** offsetof (struct dsb, data[ 0]),
2371 ** ##===========< i=1; i<MAX_SCATTER >=========
2372 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2373 ** || PADDR (checkatn),
2374 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2375 ** || offsetof (struct dsb, data[ i]),
2376 ** ##==========================================
2379 ** PADDR (checkatn),
2384 }/*-------------------------< DATA_OUT >-------------------*/,{
2386 ** Because the size depends on the
2387 ** #define MAX_SCATTER parameter,
2388 ** it is filled in at runtime.
2390 ** SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2392 ** SCR_COPY (sizeof (ticks)),
2393 ** KVAR (KVAR_TICKS),
2394 ** NADDR (header.stamp.data),
2395 ** SCR_MOVE_TBL ^ SCR_DATA_OUT,
2396 ** offsetof (struct dsb, data[ 0]),
2398 ** ##===========< i=1; i<MAX_SCATTER >=========
2399 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2400 ** || PADDR (dispatch),
2401 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2402 ** || offsetof (struct dsb, data[ i]),
2403 ** ##==========================================
2406 ** PADDR (dispatch),
2410 **---------------------------------------------------------
2414 }/*--------------------------------------------------------*/
2418 static struct scripth scripth0 = {
2419 /*-------------------------< TRYLOOP >---------------------*/{
2421 ** Load an entry of the start queue into dsa
2422 ** and try to start it by jumping to TRYSEL.
2424 ** Because the size depends on the
2425 ** #define MAX_START parameter, it is filled
2428 **-----------------------------------------------------------
2430 ** ##===========< I=0; i<MAX_START >===========
2432 ** || NADDR (squeue[i]),
2435 ** || PADDR (trysel),
2436 ** ##==========================================
2441 **-----------------------------------------------------------
2444 }/*-------------------------< MSG_PARITY >---------------*/,{
2448 SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
2451 ** send a "message parity error" message.
2453 SCR_LOAD_REG (scratcha, MSG_PARITY_ERROR),
2457 }/*-------------------------< MSG_MESSAGE_REJECT >---------------*/,{
2459 ** If a negotiation was in progress,
2460 ** negotiation failed.
2462 SCR_FROM_REG (HS_REG),
2464 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2467 ** else make host log this message
2469 SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
2470 SIR_REJECT_RECEIVED,
2474 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2480 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2483 ** get residue size.
2485 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2488 ** Check for message parity error.
2490 SCR_TO_REG (scratcha),
2492 SCR_FROM_REG (socl),
2494 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2495 PADDRH (msg_parity),
2496 SCR_FROM_REG (scratcha),
2499 ** Size is 0 .. ignore message.
2501 SCR_JUMP ^ IFTRUE (DATA (0)),
2504 ** Size is not 1 .. have to interrupt.
2506 /*<<<*/ SCR_JUMPR ^ IFFALSE (DATA (1)),
2509 ** Check for residue byte in swide register
2511 SCR_FROM_REG (scntl2),
2513 /*<<<*/ SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2516 ** There IS data in the swide register.
2519 SCR_REG_REG (scntl2, SCR_OR, WSR),
2524 ** Load again the size to the sfbr register.
2526 /*>>>*/ SCR_FROM_REG (scratcha),
2533 }/*-------------------------< MSG_EXTENDED >-------------*/,{
2539 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2544 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2547 ** Check for message parity error.
2549 SCR_TO_REG (scratcha),
2551 SCR_FROM_REG (socl),
2553 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2554 PADDRH (msg_parity),
2555 SCR_FROM_REG (scratcha),
2559 SCR_JUMP ^ IFTRUE (DATA (3)),
2561 SCR_JUMP ^ IFFALSE (DATA (2)),
2563 }/*-------------------------< MSG_EXT_2 >----------------*/,{
2566 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2569 ** get extended message code.
2571 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2574 ** Check for message parity error.
2576 SCR_TO_REG (scratcha),
2578 SCR_FROM_REG (socl),
2580 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2581 PADDRH (msg_parity),
2582 SCR_FROM_REG (scratcha),
2584 SCR_JUMP ^ IFTRUE (DATA (MSG_EXT_WDTR)),
2587 ** unknown extended message
2591 }/*-------------------------< MSG_WDTR >-----------------*/,{
2594 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2597 ** get data bus width
2599 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2601 SCR_FROM_REG (socl),
2603 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2604 PADDRH (msg_parity),
2606 ** let the host do the real work.
2611 ** let the target fetch our answer.
2618 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2621 ** Send the MSG_EXT_WDTR
2623 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
2631 PADDR (msg_out_done),
2633 }/*-------------------------< MSG_EXT_3 >----------------*/,{
2636 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2639 ** get extended message code.
2641 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2644 ** Check for message parity error.
2646 SCR_TO_REG (scratcha),
2648 SCR_FROM_REG (socl),
2650 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2651 PADDRH (msg_parity),
2652 SCR_FROM_REG (scratcha),
2654 SCR_JUMP ^ IFTRUE (DATA (MSG_EXT_SDTR)),
2657 ** unknown extended message
2662 }/*-------------------------< MSG_SDTR >-----------------*/,{
2665 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2668 ** get period and offset
2670 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
2672 SCR_FROM_REG (socl),
2674 SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2675 PADDRH (msg_parity),
2677 ** let the host do the real work.
2682 ** let the target fetch our answer.
2689 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2692 ** Send the MSG_EXT_SDTR
2694 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
2702 PADDR (msg_out_done),
2704 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
2706 ** After ABORT message,
2708 ** expect an immediate disconnect, ...
2710 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2712 SCR_CLR (SCR_ACK|SCR_ATN),
2717 ** ... and set the status to "ABORTED"
2719 SCR_LOAD_REG (HS_REG, HS_ABORTED),
2724 }/*-------------------------< GETCC >-----------------------*/,{
2726 ** The ncr doesn't have an indirect load
2727 ** or store command. So we have to
2728 ** copy part of the control block to a
2729 ** fixed place, where we can modify it.
2731 ** We patch the address part of a COPY command
2732 ** with the address of the dsa register ...
2738 ** ... then we do the actual copy.
2740 SCR_COPY (sizeof (struct head)),
2741 }/*-------------------------< GETCC1 >----------------------*/,{
2745 ** Initialize the status registers
2748 NADDR (header.status),
2750 }/*-------------------------< GETCC2 >----------------------*/,{
2752 ** Get the condition code from a target.
2754 ** DSA points to a data structure.
2755 ** Set TEMP to the script location
2756 ** that receives the condition code.
2758 ** Because there is no script command
2759 ** to load a longword into a register,
2760 ** we use a CALL command.
2765 ** Get the condition code.
2767 SCR_MOVE_TBL ^ SCR_DATA_IN,
2768 offsetof (struct dsb, sense),
2770 ** No data phase may follow!
2779 ** The CALL jumps to this point.
2780 ** Prepare for a RESTORE_POINTER message.
2781 ** Save the TEMP register into the saved pointer.
2785 NADDR (header.savep),
2787 ** Load scratcha, because in case of a selection timeout,
2788 ** the host will expect a new value for startpos in
2789 ** the scratcha register.
2794 #ifdef NCR_GETCC_WITHMSG
2796 ** If QUIRK_NOMSG is set, select without ATN.
2797 ** and don't send a message.
2799 SCR_FROM_REG (QU_REG),
2801 SCR_JUMP ^ IFTRUE (MASK (QUIRK_NOMSG, QUIRK_NOMSG)),
2804 ** Then try to connect to the target.
2805 ** If we are reselected, special treatment
2806 ** of the current job is required before
2807 ** accepting the reselection.
2809 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2812 ** Send the IDENTIFY message.
2813 ** In case of short transfer, remove ATN.
2815 SCR_MOVE_TBL ^ SCR_MSG_OUT,
2816 offsetof (struct dsb, smsg2),
2820 ** save the first byte of the message.
2829 }/*-------------------------< GETCC3 >----------------------*/,{
2831 ** Try to connect to the target.
2832 ** If we are reselected, special treatment
2833 ** of the current job is required before
2834 ** accepting the reselection.
2836 ** Silly target won't accept a message.
2837 ** Select without ATN.
2839 SCR_SEL_TBL ^ offsetof (struct dsb, select),
2842 ** Force error if selection timeout
2844 SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
2851 }/*-------------------------< ABORTTAG >-------------------*/,{
2853 ** Abort a bad reselection.
2854 ** Set the message to ABORT vs. ABORT_TAG
2856 SCR_LOAD_REG (scratcha, MSG_ABORT_TAG),
2858 SCR_JUMPR ^ IFFALSE (CARRYSET),
2860 }/*-------------------------< ABORT >----------------------*/,{
2861 SCR_LOAD_REG (scratcha, MSG_ABORT),
2872 ** we expect an immediate disconnect
2874 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2876 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2881 SCR_CLR (SCR_ACK|SCR_ATN),
2887 }/*-------------------------< SNOOPTEST >-------------------*/,{
2889 ** Read the variable.
2892 KVAR (KVAR_NCR_CACHE),
2895 ** Write the variable.
2899 KVAR (KVAR_NCR_CACHE),
2901 ** Read back the variable.
2904 KVAR (KVAR_NCR_CACHE),
2906 }/*-------------------------< SNOOPEND >-------------------*/,{
2912 }/*--------------------------------------------------------*/
2916 /*==========================================================
2919 ** Fill in #define dependent parts of the script
2922 **==========================================================
2925 void ncr_script_fill (struct script * scr, struct scripth * scrh)
2931 for (i=0; i<MAX_START; i++) {
2933 *p++ =NADDR (squeue[i]);
2936 *p++ =PADDR (trysel);
2939 *p++ =PADDRH(tryloop);
2941 assert ((char *)p == (char *)&scrh->tryloop + sizeof (scrh->tryloop));
2945 *p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN));
2946 *p++ =PADDR (no_data);
2947 *p++ =SCR_COPY (sizeof (ticks));
2948 *p++ =(ncrcmd) KVAR (KVAR_TICKS);
2949 *p++ =NADDR (header.stamp.data);
2950 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2951 *p++ =offsetof (struct dsb, data[ 0]);
2953 for (i=1; i<MAX_SCATTER; i++) {
2954 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
2955 *p++ =PADDR (checkatn);
2956 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2957 *p++ =offsetof (struct dsb, data[i]);
2961 *p++ =PADDR (checkatn);
2963 *p++ =PADDR (no_data);
2965 assert ((char *)p == (char *)&scr->data_in + sizeof (scr->data_in));
2969 *p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT));
2970 *p++ =PADDR (no_data);
2971 *p++ =SCR_COPY (sizeof (ticks));
2972 *p++ =(ncrcmd) KVAR (KVAR_TICKS);
2973 *p++ =NADDR (header.stamp.data);
2974 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2975 *p++ =offsetof (struct dsb, data[ 0]);
2977 for (i=1; i<MAX_SCATTER; i++) {
2978 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
2979 *p++ =PADDR (dispatch);
2980 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2981 *p++ =offsetof (struct dsb, data[i]);
2985 *p++ =PADDR (dispatch);
2987 *p++ =PADDR (no_data);
2989 assert ((char *)p == (char *)&scr->data_out + sizeof (scr->data_out));
2992 /*==========================================================
2995 ** Copy and rebind a script.
2998 **==========================================================
3001 static void ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
3003 ncrcmd opcode, new, old, tmp1, tmp2;
3004 ncrcmd *start, *end;
3014 WRITESCRIPT_OFF(dst, offset, opcode);
3018 ** If we forget to change the length
3019 ** in struct script, a field will be
3020 ** padded with 0. This is an illegal
3025 kprintf ("%s: ERROR0 IN SCRIPT at %d.\n",
3026 ncr_name(np), (int) (src-start-1));
3030 if (DEBUG_FLAGS & DEBUG_SCRIPT)
3031 kprintf ("%p: <%x>\n",
3032 (src-1), (unsigned)opcode);
3035 ** We don't have to decode ALL commands
3037 switch (opcode >> 28) {
3041 ** COPY has TWO arguments.
3045 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3048 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3050 if ((tmp1 ^ tmp2) & 3) {
3051 kprintf ("%s: ERROR1 IN SCRIPT at %d.\n",
3052 ncr_name(np), (int) (src-start-1));
3056 ** If PREFETCH feature not enabled, remove
3057 ** the NO FLUSH bit if present.
3059 if ((opcode & SCR_NO_FLUSH) && !(np->features&FE_PFEN))
3060 WRITESCRIPT_OFF(dst, offset - 4,
3061 (opcode & ~SCR_NO_FLUSH));
3066 ** MOVE (absolute address)
3074 ** dont't relocate if relative :-)
3076 if (opcode & 0x00800000)
3098 switch (old & RELOC_MASK) {
3099 case RELOC_REGISTER:
3100 new = (old & ~RELOC_MASK) + rman_get_start(np->reg_res);
3103 new = (old & ~RELOC_MASK) + np->p_script;
3106 new = (old & ~RELOC_MASK) + np->p_scripth;
3109 new = (old & ~RELOC_MASK) + vtophys(np);
3112 if (((old & ~RELOC_MASK) <
3113 SCRIPT_KVAR_FIRST) ||
3114 ((old & ~RELOC_MASK) >
3116 panic("ncr KVAR out of range");
3117 new = vtophys(script_kvars[old &
3121 /* Don't relocate a 0 address. */
3128 panic("ncr_script_copy_and_bind: weird relocation %x @ %d\n", old, (int)(src - start));
3132 WRITESCRIPT_OFF(dst, offset, new);
3136 WRITESCRIPT_OFF(dst, offset, *src++);
3143 /*==========================================================
3146 ** Auto configuration.
3149 **==========================================================
3153 /*----------------------------------------------------------
3155 ** Reduce the transfer length to the max value
3156 ** we can transfer safely.
3158 ** Reading a block greater then MAX_SIZE from the
3159 ** raw (character) device exercises a memory leak
3160 ** in the vm subsystem. This is common to ALL devices.
3161 ** We have submitted a description of this bug to
3162 ** <FreeBSD-bugs@freefall.cdrom.com>.
3163 ** It should be fixed in the current release.
3165 **----------------------------------------------------------
3168 void ncr_min_phys (struct buf *bp)
3170 if ((unsigned long)bp->b_bcount > MAX_SIZE) bp->b_bcount = MAX_SIZE;
3176 /*----------------------------------------------------------
3178 ** Maximal number of outstanding requests per target.
3180 **----------------------------------------------------------
3183 u_int32_t ncr_info (int unit)
3185 return (1); /* may be changed later */
3190 /*----------------------------------------------------------
3192 ** NCR chip devices table and chip look up function.
3193 ** Features bit are defined in ncrreg.h. Is it the
3196 **----------------------------------------------------------
3199 unsigned long device_id;
3200 unsigned short minrevid;
3202 unsigned char maxburst;
3203 unsigned char maxoffs;
3204 unsigned char clock_divn;
3205 unsigned int features;
3208 static ncr_chip ncr_chip_table[] = {
3209 {NCR_810_ID, 0x00, "ncr 53c810 fast10 scsi", 4, 8, 4,
3212 {NCR_810_ID, 0x10, "ncr 53c810a fast10 scsi", 4, 8, 4,
3213 FE_ERL|FE_LDSTR|FE_PFEN|FE_BOF}
3215 {NCR_815_ID, 0x00, "ncr 53c815 fast10 scsi", 4, 8, 4,
3218 {NCR_820_ID, 0x00, "ncr 53c820 fast10 wide scsi", 4, 8, 4,
3221 {NCR_825_ID, 0x00, "ncr 53c825 fast10 wide scsi", 4, 8, 4,
3222 FE_WIDE|FE_ERL|FE_BOF}
3224 {NCR_825_ID, 0x10, "ncr 53c825a fast10 wide scsi", 7, 8, 4,
3225 FE_WIDE|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3227 {NCR_860_ID, 0x00, "ncr 53c860 fast20 scsi", 4, 8, 5,
3228 FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_LDSTR|FE_PFEN}
3230 {NCR_875_ID, 0x00, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
3231 FE_WIDE|FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3233 {NCR_875_ID, 0x02, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
3234 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3236 {NCR_875_ID2, 0x00, "ncr 53c875j fast20 wide scsi", 7, 16, 5,
3237 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3239 {NCR_885_ID, 0x00, "ncr 53c885 fast20 wide scsi", 7, 16, 5,
3240 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3242 {NCR_895_ID, 0x00, "ncr 53c895 fast40 wide scsi", 7, 31, 7,
3243 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3245 {NCR_896_ID, 0x00, "ncr 53c896 fast40 wide scsi", 7, 31, 7,
3246 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3248 {NCR_895A_ID, 0x00, "ncr 53c895a fast40 wide scsi", 7, 31, 7,
3249 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3251 {NCR_1510D_ID, 0x00, "ncr 53c1510d fast40 wide scsi", 7, 31, 7,
3252 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM}
3255 static int ncr_chip_lookup(u_long device_id, u_char revision_id)
3260 for (i = 0; i < sizeof(ncr_chip_table)/sizeof(ncr_chip_table[0]); i++) {
3261 if (device_id == ncr_chip_table[i].device_id &&
3262 ncr_chip_table[i].minrevid <= revision_id) {
3264 ncr_chip_table[found].minrevid
3265 < ncr_chip_table[i].minrevid) {
3273 /*----------------------------------------------------------
3275 ** Probe the hostadapter.
3277 **----------------------------------------------------------
3282 static int ncr_probe (device_t dev)
3286 i = ncr_chip_lookup(pci_get_devid(dev), pci_get_revid(dev));
3288 device_set_desc(dev, ncr_chip_table[i].name);
3289 return (-1000); /* Allows to use both ncr and sym */
3297 /*==========================================================
3299 ** NCR chip clock divisor table.
3300 ** Divisors are multiplied by 10,000,000 in order to make
3301 ** calculations more simple.
3303 **==========================================================
3307 static u_long div_10M[] =
3308 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3310 /*===============================================================
3312 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3313 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3314 ** We use log base 2 (burst length) as internal code, with
3315 ** value 0 meaning "burst disabled".
3317 **===============================================================
3321 * Burst length from burst code.
3323 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3326 * Burst code from io register bits.
3328 #define burst_code(dmode, ctest4, ctest5) \
3329 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
3332 * Set initial io register bits from burst code.
3335 ncr_init_burst(ncb_p np, u_char bc)
3337 np->rv_ctest4 &= ~0x80;
3338 np->rv_dmode &= ~(0x3 << 6);
3339 np->rv_ctest5 &= ~0x4;
3342 np->rv_ctest4 |= 0x80;
3346 np->rv_dmode |= ((bc & 0x3) << 6);
3347 np->rv_ctest5 |= (bc & 0x4);
3351 /*==========================================================
3354 ** Auto configuration: attach and init a host adapter.
3357 **==========================================================
3362 ncr_attach (device_t dev)
3364 ncb_p np = (struct ncb*) device_get_softc(dev);
3370 struct cam_devq *devq;
3373 ** allocate and initialize structures.
3376 np->unit = device_get_unit(dev);
3379 ** Try to map the controller chip to
3380 ** virtual and physical memory.
3384 np->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &np->reg_rid,
3385 0, ~0, 1, RF_ACTIVE);
3387 device_printf(dev, "could not map memory\n");
3392 ** Make the controller's registers available.
3393 ** Now the INB INW INL OUTB OUTW OUTL macros
3394 ** can be used safely.
3397 np->bst = rman_get_bustag(np->reg_res);
3398 np->bsh = rman_get_bushandle(np->reg_res);
3403 ** Try to map the controller chip into iospace.
3406 if (!pci_map_port (config_id, 0x10, &np->port))
3412 ** Save some controller register default values
3415 np->rv_scntl3 = INB(nc_scntl3) & 0x77;
3416 np->rv_dmode = INB(nc_dmode) & 0xce;
3417 np->rv_dcntl = INB(nc_dcntl) & 0xa9;
3418 np->rv_ctest3 = INB(nc_ctest3) & 0x01;
3419 np->rv_ctest4 = INB(nc_ctest4) & 0x88;
3420 np->rv_ctest5 = INB(nc_ctest5) & 0x24;
3421 np->rv_gpcntl = INB(nc_gpcntl);
3422 np->rv_stest2 = INB(nc_stest2) & 0x20;
3424 if (bootverbose >= 2) {
3425 kprintf ("\tBIOS values: SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
3426 np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
3427 kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
3428 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
3431 np->rv_dcntl |= NOCOM;
3434 ** Do chip dependent initialization.
3437 rev = pci_get_revid(dev);
3440 ** Get chip features from chips table.
3442 i = ncr_chip_lookup(pci_get_devid(dev), rev);
3445 np->maxburst = ncr_chip_table[i].maxburst;
3446 np->maxoffs = ncr_chip_table[i].maxoffs;
3447 np->clock_divn = ncr_chip_table[i].clock_divn;
3448 np->features = ncr_chip_table[i].features;
3449 } else { /* Should'nt happen if probe() is ok */
3453 np->features = FE_ERL;
3456 np->maxwide = np->features & FE_WIDE ? 1 : 0;
3457 np->clock_khz = np->features & FE_CLK80 ? 80000 : 40000;
3458 if (np->features & FE_QUAD) np->multiplier = 4;
3459 else if (np->features & FE_DBLR) np->multiplier = 2;
3460 else np->multiplier = 1;
3463 ** Get the frequency of the chip's clock.
3464 ** Find the right value for scntl3.
3466 if (np->features & (FE_ULTRA|FE_ULTRA2))
3467 ncr_getclock(np, np->multiplier);
3469 #ifdef NCR_TEKRAM_EEPROM
3471 kprintf ("%s: Tekram EEPROM read %s\n",
3473 read_tekram_eeprom (np, NULL) ?
3474 "succeeded" : "failed");
3476 #endif /* NCR_TEKRAM_EEPROM */
3479 * If scntl3 != 0, we assume BIOS is present.
3482 np->features |= FE_BIOS;
3485 * Divisor to be used for async (timer pre-scaler).
3487 i = np->clock_divn - 1;
3490 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
3495 np->rv_scntl3 = i+1;
3498 * Minimum synchronous period factor supported by the chip.
3499 * Btw, 'period' is in tenths of nanoseconds.
3502 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
3503 if (period <= 250) np->minsync = 10;
3504 else if (period <= 303) np->minsync = 11;
3505 else if (period <= 500) np->minsync = 12;
3506 else np->minsync = (period + 40 - 1) / 40;
3509 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3512 if (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
3514 else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
3518 * Maximum synchronous period factor supported by the chip.
3521 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
3522 np->maxsync = period > 2540 ? 254 : period / 10;
3525 * Now, some features available with Symbios compatible boards.
3526 * LED support through GPIO0 and DIFF support.
3529 #ifdef SCSI_NCR_SYMBIOS_COMPAT
3530 if (!(np->rv_gpcntl & 0x01))
3531 np->features |= FE_LED0;
3532 #if 0 /* Not safe enough without NVRAM support or user settable option */
3533 if (!(INB(nc_gpreg) & 0x08))
3534 np->features |= FE_DIFF;
3536 #endif /* SCSI_NCR_SYMBIOS_COMPAT */
3539 * Prepare initial IO registers settings.
3540 * Trust BIOS only if we believe we have one and if we want to.
3542 #ifdef SCSI_NCR_TRUST_BIOS
3543 if (!(np->features & FE_BIOS)) {
3548 np->rv_dcntl = NOCOM;
3550 np->rv_ctest4 = MPEE;
3554 if (np->features & FE_ERL)
3555 np->rv_dmode |= ERL; /* Enable Read Line */
3556 if (np->features & FE_BOF)
3557 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
3558 if (np->features & FE_ERMP)
3559 np->rv_dmode |= ERMP; /* Enable Read Multiple */
3560 if (np->features & FE_CLSE)
3561 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
3562 if (np->features & FE_WRIE)
3563 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
3564 if (np->features & FE_PFEN)
3565 np->rv_dcntl |= PFEN; /* Prefetch Enable */
3566 if (np->features & FE_DFS)
3567 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
3568 if (np->features & FE_DIFF)
3569 np->rv_stest2 |= 0x20; /* Differential mode */
3570 ncr_init_burst(np, np->maxburst); /* Max dwords burst length */
3573 burst_code(np->rv_dmode, np->rv_ctest4, np->rv_ctest5);
3577 ** Get on-chip SRAM address, if supported
3579 if ((np->features & FE_RAM) && sizeof(struct script) <= 4096) {
3580 np->sram_rid = 0x18;
3581 np->sram_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
3583 0, ~0, 1, RF_ACTIVE);
3587 ** Allocate structure for script relocation.
3589 if (np->sram_res != NULL) {
3591 np->p_script = rman_get_start(np->sram_res);
3592 np->bst2 = rman_get_bustag(np->sram_res);
3593 np->bsh2 = rman_get_bushandle(np->sram_res);
3594 } else if (sizeof (struct script) > PAGE_SIZE) {
3595 np->script = (struct script*) vm_page_alloc_contig
3596 (round_page(sizeof (struct script)),
3597 0, 0xffffffff, PAGE_SIZE);
3599 np->script = (struct script *)
3600 kmalloc (sizeof (struct script), M_DEVBUF, M_WAITOK);
3603 /* XXX JGibbs - Use contigmalloc */
3604 if (sizeof (struct scripth) > PAGE_SIZE) {
3605 np->scripth = (struct scripth*) vm_page_alloc_contig
3606 (round_page(sizeof (struct scripth)),
3607 0, 0xffffffff, PAGE_SIZE);
3610 np->scripth = (struct scripth *)
3611 kmalloc (sizeof (struct scripth), M_DEVBUF, M_WAITOK);
3614 #ifdef SCSI_NCR_PCI_CONFIG_FIXUP
3616 ** If cache line size is enabled, check PCI config space and
3617 ** try to fix it up if necessary.
3619 #ifdef PCIR_CACHELNSZ /* To be sure that new PCI stuff is present */
3621 u_char cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3622 u_short command = pci_read_config(dev, PCIR_COMMAND, 2);
3626 kprintf("%s: setting PCI cache line size register to %d.\n",
3627 ncr_name(np), (int)cachelnsz);
3628 pci_write_config(dev, PCIR_CACHELNSZ, cachelnsz, 1);
3631 if (!(command & (1<<4))) {
3633 kprintf("%s: setting PCI command write and invalidate.\n",
3635 pci_write_config(dev, PCIR_COMMAND, command, 2);
3638 #endif /* PCIR_CACHELNSZ */
3640 #endif /* SCSI_NCR_PCI_CONFIG_FIXUP */
3642 /* Initialize per-target user settings */
3644 if (SCSI_NCR_DFLT_SYNC) {
3645 usrsync = SCSI_NCR_DFLT_SYNC;
3646 if (usrsync > np->maxsync)
3647 usrsync = np->maxsync;
3648 if (usrsync < np->minsync)
3649 usrsync = np->minsync;
3652 usrwide = (SCSI_NCR_MAX_WIDE);
3653 if (usrwide > np->maxwide) usrwide=np->maxwide;
3655 for (i=0;i<MAX_TARGET;i++) {
3656 tcb_p tp = &np->target[i];
3658 tp->tinfo.user.period = usrsync;
3659 tp->tinfo.user.offset = usrsync != 0 ? np->maxoffs : 0;
3660 tp->tinfo.user.width = usrwide;
3661 tp->tinfo.disc_tag = NCR_CUR_DISCENB
3668 ** Bells and whistles ;-)
3671 kprintf("%s: minsync=%d, maxsync=%d, maxoffs=%d, %d dwords burst, %s dma fifo\n",
3672 ncr_name(np), np->minsync, np->maxsync, np->maxoffs,
3673 burst_length(np->maxburst),
3674 (np->rv_ctest5 & DFS) ? "large" : "normal");
3677 ** Print some complementary information that can be helpfull.
3680 kprintf("%s: %s, %s IRQ driver%s\n",
3682 np->rv_stest2 & 0x20 ? "differential" : "single-ended",
3683 np->rv_dcntl & IRQM ? "totem pole" : "open drain",
3684 np->sram_res ? ", using on-chip SRAM" : "");
3687 ** Patch scripts to physical addresses
3689 ncr_script_fill (&script0, &scripth0);
3692 np->p_script = vtophys(np->script);
3693 np->p_scripth = vtophys(np->scripth);
3695 ncr_script_copy_and_bind (np, (ncrcmd *) &script0,
3696 (ncrcmd *) np->script, sizeof(struct script));
3698 ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0,
3699 (ncrcmd *) np->scripth, sizeof(struct scripth));
3702 ** Patch the script for LED support.
3705 if (np->features & FE_LED0) {
3706 WRITESCRIPT(reselect[0], SCR_REG_REG(gpreg, SCR_OR, 0x01));
3707 WRITESCRIPT(reselect1[0], SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3708 WRITESCRIPT(reselect2[0], SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3712 ** init data structure
3715 np->jump_tcb.l_cmd = SCR_JUMP;
3716 np->jump_tcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
3719 ** Get SCSI addr of host adapter (set by bios?).
3722 np->myaddr = INB(nc_scid) & 0x07;
3723 if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
3727 ** Log the initial register contents
3731 for (reg=0; reg<256; reg+=4) {
3732 if (reg%16==0) kprintf ("reg[%2x]", reg);
3733 kprintf (" %08x", (int)pci_conf_read (config_id, reg));
3734 if (reg%16==12) kprintf ("\n");
3737 #endif /* NCR_DUMP_REG */
3743 OUTB (nc_istat, SRST);
3745 OUTB (nc_istat, 0 );
3749 ** Now check the cache handling of the pci chipset.
3752 if (ncr_snooptest (np)) {
3753 kprintf ("CACHE INCORRECTLY CONFIGURED.\n");
3758 ** Install the interrupt handler.
3762 np->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
3763 RF_SHAREABLE | RF_ACTIVE);
3764 if (np->irq_res == NULL) {
3766 "interruptless mode: reduced performance.\n");
3768 bus_setup_intr(dev, np->irq_res, 0,
3769 ncr_intr, np, &np->irq_handle, NULL);
3773 ** Create the device queue. We only allow MAX_START-1 concurrent
3774 ** transactions so we can be sure to have one element free in our
3775 ** start queue to reset to the idle loop.
3777 devq = cam_simq_alloc(MAX_START - 1);
3782 ** Now tell the generic SCSI layer
3785 np->sim = cam_sim_alloc(ncr_action, ncr_poll, "ncr", np, np->unit,
3786 &sim_mplock, 1, MAX_TAGS, devq);
3787 cam_simq_release(devq);
3788 if (np->sim == NULL)
3792 if (xpt_bus_register(np->sim, 0) != CAM_SUCCESS) {
3793 cam_sim_free(np->sim);
3797 if (xpt_create_path(&np->path, /*periph*/NULL,
3798 cam_sim_path(np->sim), CAM_TARGET_WILDCARD,
3799 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3800 xpt_bus_deregister(cam_sim_path(np->sim));
3801 cam_sim_free(np->sim);
3806 ** start the timeout daemon
3808 callout_init(&np->timeout_ch);
3815 /*==========================================================
3818 ** Process pending device interrupts.
3821 **==========================================================
3830 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("[");
3832 if (INB(nc_istat) & (INTF|SIP|DIP)) {
3834 ** Repeat until no outstanding ints
3838 } while (INB(nc_istat) & (INTF|SIP|DIP));
3843 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("]\n");
3848 /*==========================================================
3851 ** Start execution of a SCSI command.
3852 ** This is called from the generic SCSI driver.
3855 **==========================================================
3859 ncr_action (struct cam_sim *sim, union ccb *ccb)
3863 np = (ncb_p) cam_sim_softc(sim);
3865 switch (ccb->ccb_h.func_code) {
3866 /* Common cases first */
3867 case XPT_SCSI_IO: /* Execute the requested I/O operation */
3872 struct ccb_scsiio *csio;
3881 tp = &np->target[ccb->ccb_h.target_id];
3887 * Last time we need to check if this CCB needs to
3890 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3895 ccb->ccb_h.status |= CAM_SIM_QUEUED;
3897 /*---------------------------------------------------
3899 ** Assign an nccb / bind ccb
3901 **----------------------------------------------------
3903 cp = ncr_get_nccb (np, ccb->ccb_h.target_id,
3904 ccb->ccb_h.target_lun);
3906 /* XXX JGibbs - Freeze SIMQ */
3907 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3914 /*---------------------------------------------------
3918 **----------------------------------------------------
3921 ** XXX JGibbs - Isn't this expensive
3922 ** enough to be conditionalized??
3925 bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
3926 cp->phys.header.stamp.start = ticks;
3929 if (tp->nego_cp == NULL) {
3931 if (tp->tinfo.current.width
3932 != tp->tinfo.goal.width) {
3935 } else if ((tp->tinfo.current.period
3936 != tp->tinfo.goal.period)
3937 || (tp->tinfo.current.offset
3938 != tp->tinfo.goal.offset)) {
3944 /*---------------------------------------------------
3946 ** choose a new tag ...
3948 **----------------------------------------------------
3950 lp = tp->lp[ccb->ccb_h.target_lun];
3952 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
3953 && (ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3956 ** assign a tag to this nccb
3959 nccb_p cp2 = lp->next_nccb;
3960 lp->lasttag = lp->lasttag % 255 + 1;
3961 while (cp2 && cp2->tag != lp->lasttag)
3962 cp2 = cp2->next_nccb;
3964 cp->tag=lp->lasttag;
3965 if (DEBUG_FLAGS & DEBUG_TAGS) {
3967 kprintf ("using tag #%d.\n", cp->tag);
3974 /*----------------------------------------------------
3976 ** Build the identify / tag / sdtr message
3978 **----------------------------------------------------
3980 idmsg = MSG_IDENTIFYFLAG | ccb->ccb_h.target_lun;
3981 if (tp->tinfo.disc_tag & NCR_CUR_DISCENB)
3982 idmsg |= MSG_IDENTIFY_DISCFLAG;
3984 msgptr = cp->scsi_smsg;
3986 msgptr[msglen++] = idmsg;
3989 msgptr[msglen++] = ccb->csio.tag_action;
3990 msgptr[msglen++] = cp->tag;
3995 msgptr[msglen++] = MSG_EXTENDED;
3996 msgptr[msglen++] = MSG_EXT_SDTR_LEN;
3997 msgptr[msglen++] = MSG_EXT_SDTR;
3998 msgptr[msglen++] = tp->tinfo.goal.period;
3999 msgptr[msglen++] = tp->tinfo.goal.offset;
4000 if (DEBUG_FLAGS & DEBUG_NEGO) {
4002 kprintf ("sync msgout: ");
4003 ncr_show_msg (&cp->scsi_smsg [msglen-5]);
4008 msgptr[msglen++] = MSG_EXTENDED;
4009 msgptr[msglen++] = MSG_EXT_WDTR_LEN;
4010 msgptr[msglen++] = MSG_EXT_WDTR;
4011 msgptr[msglen++] = tp->tinfo.goal.width;
4012 if (DEBUG_FLAGS & DEBUG_NEGO) {
4014 kprintf ("wide msgout: ");
4015 ncr_show_msg (&cp->scsi_smsg [msglen-4]);
4021 /*----------------------------------------------------
4023 ** Build the identify message for getcc.
4025 **----------------------------------------------------
4028 cp->scsi_smsg2 [0] = idmsg;
4031 /*----------------------------------------------------
4033 ** Build the data descriptors
4035 **----------------------------------------------------
4038 /* XXX JGibbs - Handle other types of I/O */
4039 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
4040 segments = ncr_scatter(&cp->phys,
4041 (vm_offset_t)csio->data_ptr,
4042 (vm_size_t)csio->dxfer_len);
4045 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
4046 ncr_free_nccb(np, cp);
4051 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4052 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_in);
4053 cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
4054 } else { /* CAM_DIR_OUT */
4055 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_out);
4056 cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
4059 cp->phys.header.savep = NCB_SCRIPT_PHYS (np, no_data);
4060 cp->phys.header.goalp = cp->phys.header.savep;
4063 cp->phys.header.lastp = cp->phys.header.savep;
4066 /*----------------------------------------------------
4070 **----------------------------------------------------
4073 ** physical -> virtual backlink
4074 ** Generic SCSI command
4076 cp->phys.header.cp = cp;
4080 cp->phys.header.launch.l_paddr = NCB_SCRIPT_PHYS (np, select);
4081 cp->phys.header.launch.l_cmd = SCR_JUMP;
4085 cp->phys.select.sel_id = ccb->ccb_h.target_id;
4086 cp->phys.select.sel_scntl3 = tp->tinfo.wval;
4087 cp->phys.select.sel_sxfer = tp->tinfo.sval;
4091 cp->phys.smsg.addr = CCB_PHYS (cp, scsi_smsg);
4092 cp->phys.smsg.size = msglen;
4094 cp->phys.smsg2.addr = CCB_PHYS (cp, scsi_smsg2);
4095 cp->phys.smsg2.size = msglen2;
4099 /* XXX JGibbs - Support other command types */
4100 cp->phys.cmd.addr = vtophys (csio->cdb_io.cdb_bytes);
4101 cp->phys.cmd.size = csio->cdb_len;
4105 cp->phys.scmd.addr = CCB_PHYS (cp, sensecmd);
4106 cp->phys.scmd.size = 6;
4108 ** patch requested size into sense command
4110 cp->sensecmd[0] = 0x03;
4111 cp->sensecmd[1] = ccb->ccb_h.target_lun << 5;
4112 cp->sensecmd[4] = sizeof(struct scsi_sense_data);
4113 cp->sensecmd[4] = csio->sense_len;
4117 cp->phys.sense.addr = vtophys (&csio->sense_data);
4118 cp->phys.sense.size = csio->sense_len;
4122 cp->actualquirks = QUIRK_NOMSG;
4123 cp->host_status = nego ? HS_NEGOTIATE : HS_BUSY;
4124 cp->s_status = SCSI_STATUS_ILLEGAL;
4125 cp->parity_status = 0;
4127 cp->xerr_status = XE_OK;
4128 cp->sync_status = tp->tinfo.sval;
4129 cp->nego_status = nego;
4130 cp->wide_status = tp->tinfo.wval;
4132 /*----------------------------------------------------
4134 ** Critical region: start this job.
4136 **----------------------------------------------------
4140 ** reselect pattern and activate this job.
4143 cp->jump_nccb.l_cmd = (SCR_JUMP ^ IFFALSE (DATA (cp->tag)));
4144 cp->tlimit = time_second
4145 + ccb->ccb_h.timeout / 1000 + 2;
4146 cp->magic = CCB_MAGIC;
4149 ** insert into start queue.
4152 qidx = np->squeueput + 1;
4153 if (qidx >= MAX_START)
4155 np->squeue [qidx ] = NCB_SCRIPT_PHYS (np, idle);
4156 np->squeue [np->squeueput] = CCB_PHYS (cp, phys);
4157 np->squeueput = qidx;
4159 if(DEBUG_FLAGS & DEBUG_QUEUE)
4160 kprintf("%s: queuepos=%d tryoffset=%d.\n",
4161 ncr_name (np), np->squeueput,
4162 (unsigned)(READSCRIPT(startpos[0]) -
4163 (NCB_SCRIPTH_PHYS (np, tryloop))));
4166 ** Script processor may be waiting for reselect.
4169 OUTB (nc_istat, SIGP);
4172 ** and reenable interrupts
4177 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
4178 case XPT_EN_LUN: /* Enable LUN as a target */
4179 case XPT_TARGET_IO: /* Execute target I/O request */
4180 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
4181 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
4182 case XPT_ABORT: /* Abort the specified CCB */
4184 ccb->ccb_h.status = CAM_REQ_INVALID;
4187 case XPT_SET_TRAN_SETTINGS:
4189 struct ccb_trans_settings *cts = &ccb->cts;
4192 struct ccb_trans_settings_scsi *scsi =
4193 &cts->proto_specific.scsi;
4194 struct ccb_trans_settings_spi *spi =
4195 &cts->xport_specific.spi;
4198 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
4199 update_type |= NCR_TRANS_GOAL;
4200 if (cts->type == CTS_TYPE_USER_SETTINGS)
4201 update_type |= NCR_TRANS_USER;
4204 tp = &np->target[ccb->ccb_h.target_id];
4205 /* Tag and disc enables */
4206 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4207 if (update_type & NCR_TRANS_GOAL) {
4208 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4209 tp->tinfo.disc_tag |= NCR_CUR_DISCENB;
4211 tp->tinfo.disc_tag &= ~NCR_CUR_DISCENB;
4214 if (update_type & NCR_TRANS_USER) {
4215 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4216 tp->tinfo.disc_tag |= NCR_USR_DISCENB;
4218 tp->tinfo.disc_tag &= ~NCR_USR_DISCENB;
4223 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4224 if (update_type & NCR_TRANS_GOAL) {
4225 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4226 tp->tinfo.disc_tag |= NCR_CUR_TAGENB;
4228 tp->tinfo.disc_tag &= ~NCR_CUR_TAGENB;
4231 if (update_type & NCR_TRANS_USER) {
4232 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4233 tp->tinfo.disc_tag |= NCR_USR_TAGENB;
4235 tp->tinfo.disc_tag &= ~NCR_USR_TAGENB;
4239 /* Filter bus width and sync negotiation settings */
4240 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4241 if (spi->bus_width > np->maxwide)
4242 spi->bus_width = np->maxwide;
4245 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
4246 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
4247 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
4248 if (spi->sync_period != 0
4249 && (spi->sync_period < np->minsync))
4250 spi->sync_period = np->minsync;
4252 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
4253 if (spi->sync_offset == 0)
4254 spi->sync_period = 0;
4255 if (spi->sync_offset > np->maxoffs)
4256 spi->sync_offset = np->maxoffs;
4259 if ((update_type & NCR_TRANS_USER) != 0) {
4260 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
4261 tp->tinfo.user.period = spi->sync_period;
4262 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
4263 tp->tinfo.user.offset = spi->sync_offset;
4264 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
4265 tp->tinfo.user.width = spi->bus_width;
4267 if ((update_type & NCR_TRANS_GOAL) != 0) {
4268 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
4269 tp->tinfo.goal.period = spi->sync_period;
4271 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
4272 tp->tinfo.goal.offset = spi->sync_offset;
4274 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
4275 tp->tinfo.goal.width = spi->bus_width;
4278 ccb->ccb_h.status = CAM_REQ_CMP;
4282 case XPT_GET_TRAN_SETTINGS:
4283 /* Get default/user set transfer settings for the target */
4285 struct ccb_trans_settings *cts = &ccb->cts;
4286 struct ncr_transinfo *tinfo;
4287 tcb_p tp = &np->target[ccb->ccb_h.target_id];
4288 struct ccb_trans_settings_scsi *scsi =
4289 &cts->proto_specific.scsi;
4290 struct ccb_trans_settings_spi *spi =
4291 &cts->xport_specific.spi;
4293 cts->protocol = PROTO_SCSI;
4294 cts->protocol_version = SCSI_REV_2;
4295 cts->transport = XPORT_SPI;
4296 cts->transport_version = 2;
4299 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
4300 tinfo = &tp->tinfo.current;
4301 if (tp->tinfo.disc_tag & NCR_CUR_DISCENB)
4302 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4304 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
4306 if (tp->tinfo.disc_tag & NCR_CUR_TAGENB)
4307 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4309 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
4311 tinfo = &tp->tinfo.user;
4312 if (tp->tinfo.disc_tag & NCR_USR_DISCENB)
4313 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4315 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
4317 if (tp->tinfo.disc_tag & NCR_USR_TAGENB)
4318 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4320 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
4323 spi->sync_period = tinfo->period;
4324 spi->sync_offset = tinfo->offset;
4325 spi->bus_width = tinfo->width;
4328 spi->valid = CTS_SPI_VALID_SYNC_RATE
4329 | CTS_SPI_VALID_SYNC_OFFSET
4330 | CTS_SPI_VALID_BUS_WIDTH
4331 | CTS_SPI_VALID_DISC;
4332 scsi->valid = CTS_SCSI_VALID_TQ;
4334 ccb->ccb_h.status = CAM_REQ_CMP;
4338 case XPT_CALC_GEOMETRY:
4340 struct ccb_calc_geometry *ccg;
4342 u_int32_t secs_per_cylinder;
4345 /* XXX JGibbs - I'm sure the NCR uses a different strategy,
4346 * but it should be able to deal with Adaptec
4351 size_mb = ccg->volume_size
4352 / ((1024L * 1024L) / ccg->block_size);
4354 if (size_mb > 1024 && extended) {
4356 ccg->secs_per_track = 63;
4359 ccg->secs_per_track = 32;
4361 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
4362 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
4363 ccb->ccb_h.status = CAM_REQ_CMP;
4367 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
4369 OUTB (nc_scntl1, CRST);
4370 ccb->ccb_h.status = CAM_REQ_CMP;
4371 DELAY(10000); /* Wait until our interrupt handler sees it */
4375 case XPT_TERM_IO: /* Terminate the I/O process */
4377 ccb->ccb_h.status = CAM_REQ_INVALID;
4380 case XPT_PATH_INQ: /* Path routing inquiry */
4382 struct ccb_pathinq *cpi = &ccb->cpi;
4384 cpi->version_num = 1; /* XXX??? */
4385 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
4386 if ((np->features & FE_WIDE) != 0)
4387 cpi->hba_inquiry |= PI_WIDE_16;
4388 cpi->target_sprt = 0;
4390 cpi->hba_eng_cnt = 0;
4391 cpi->max_target = (np->features & FE_WIDE) ? 15 : 7;
4392 cpi->max_lun = MAX_LUN - 1;
4393 cpi->initiator_id = np->myaddr;
4394 cpi->bus_id = cam_sim_bus(sim);
4395 cpi->base_transfer_speed = 3300;
4396 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4397 strncpy(cpi->hba_vid, "Symbios", HBA_IDLEN);
4398 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4399 cpi->unit_number = cam_sim_unit(sim);
4400 cpi->transport = XPORT_SPI;
4401 cpi->transport_version = 2;
4402 cpi->protocol = PROTO_SCSI;
4403 cpi->protocol_version = SCSI_REV_2;
4404 cpi->ccb_h.status = CAM_REQ_CMP;
4409 ccb->ccb_h.status = CAM_REQ_INVALID;
4415 /*==========================================================
4418 ** Complete execution of a SCSI command.
4419 ** Signal completion to the generic SCSI driver.
4422 **==========================================================
4426 ncr_complete (ncb_p np, nccb_p cp)
4436 if (!cp || (cp->magic!=CCB_MAGIC) || !cp->ccb) return;
4441 ** No Reselect anymore.
4443 cp->jump_nccb.l_cmd = (SCR_JUMP);
4448 cp->phys.header.launch.l_paddr= NCB_SCRIPT_PHYS (np, idle);
4453 ncb_profile (np, cp);
4455 if (DEBUG_FLAGS & DEBUG_TINY)
4456 kprintf ("CCB=%x STAT=%x/%x\n", (int)(intptr_t)cp & 0xfff,
4457 cp->host_status,cp->s_status);
4461 tp = &np->target[ccb->ccb_h.target_id];
4462 lp = tp->lp[ccb->ccb_h.target_lun];
4465 ** We do not queue more than 1 nccb per target
4466 ** with negotiation at any time. If this nccb was
4467 ** used for negotiation, clear this info in the tcb.
4470 if (cp == tp->nego_cp)
4474 ** Check for parity errors.
4476 /* XXX JGibbs - What about reporting them??? */
4478 if (cp->parity_status) {
4480 kprintf ("%d parity error(s), fallback.\n", cp->parity_status);
4482 ** fallback to asynch transfer.
4484 tp->tinfo.goal.period = 0;
4485 tp->tinfo.goal.offset = 0;
4489 ** Check for extended errors.
4492 if (cp->xerr_status != XE_OK) {
4494 switch (cp->xerr_status) {
4496 kprintf ("extraneous data discarded.\n");
4499 kprintf ("illegal scsi phase (4/5).\n");
4502 kprintf ("extended error %d.\n", cp->xerr_status);
4505 if (cp->host_status==HS_COMPLETE)
4506 cp->host_status = HS_FAIL;
4510 ** Check the status.
4512 if (cp->host_status == HS_COMPLETE) {
4514 if (cp->s_status == SCSI_STATUS_OK) {
4519 /* XXX JGibbs - Properly calculate residual */
4521 tp->bytes += ccb->csio.dxfer_len;
4524 ccb->ccb_h.status = CAM_REQ_CMP;
4525 } else if ((cp->s_status & SCSI_STATUS_SENSE) != 0) {
4528 * XXX Could be TERMIO too. Should record
4531 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
4532 cp->s_status &= ~SCSI_STATUS_SENSE;
4533 if (cp->s_status == SCSI_STATUS_OK) {
4535 CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR;
4537 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
4540 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
4541 ccb->csio.scsi_status = cp->s_status;
4545 } else if (cp->host_status == HS_SEL_TIMEOUT) {
4548 ** Device failed selection
4550 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
4552 } else if (cp->host_status == HS_TIMEOUT) {
4557 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
4558 } else if (cp->host_status == HS_STALL) {
4559 ccb->ccb_h.status = CAM_REQUEUE_REQ;
4563 ** Other protocol messes
4566 kprintf ("COMMAND FAILED (%x %x) @%p.\n",
4567 cp->host_status, cp->s_status, cp);
4569 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
4572 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4573 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
4574 ccb->ccb_h.status |= CAM_DEV_QFRZN;
4580 ncr_free_nccb (np, cp);
4583 ** signal completion to generic driver.
4588 /*==========================================================
4591 ** Signal all (or one) control block done.
4594 **==========================================================
4598 ncr_wakeup (ncb_p np, u_long code)
4601 ** Starting at the default nccb and following
4602 ** the links, complete all jobs with a
4603 ** host_status greater than "disconnect".
4605 ** If the "code" parameter is not zero,
4606 ** complete all jobs that are not IDLE.
4609 nccb_p cp = np->link_nccb;
4611 switch (cp->host_status) {
4617 if(DEBUG_FLAGS & DEBUG_TINY) kprintf ("D");
4623 cp->host_status = code;
4628 ncr_complete (np, cp);
4631 cp = cp -> link_nccb;
4636 ncr_freeze_devq (ncb_p np, struct cam_path *path)
4643 ** Starting at the first nccb and following
4644 ** the links, complete all jobs that match
4645 ** the passed in path and are in the start queue.
4652 switch (cp->host_status) {
4656 if ((cp->phys.header.launch.l_paddr
4657 == NCB_SCRIPT_PHYS (np, select))
4658 && (xpt_path_comp(path, cp->ccb->ccb_h.path) >= 0)) {
4660 /* Mark for removal from the start queue */
4661 for (i = 1; i < MAX_START; i++) {
4664 idx = np->squeueput - i;
4667 idx = MAX_START + idx;
4669 == CCB_PHYS(cp, phys)) {
4671 NCB_SCRIPT_PHYS (np, skip);
4677 cp->host_status=HS_STALL;
4678 ncr_complete (np, cp);
4692 /* Compress the start queue */
4694 bidx = np->squeueput;
4695 i = np->squeueput - firstskip;
4702 bidx = MAX_START + bidx;
4704 if (np->squeue[i] == NCB_SCRIPT_PHYS (np, skip)) {
4706 } else if (j != 0) {
4707 np->squeue[bidx] = np->squeue[i];
4708 if (np->squeue[bidx]
4709 == NCB_SCRIPT_PHYS(np, idle))
4712 i = (i + 1) % MAX_START;
4714 np->squeueput = bidx;
4718 /*==========================================================
4724 **==========================================================
4728 ncr_init(ncb_p np, char * msg, u_long code)
4736 OUTB (nc_istat, SRST);
4744 if (msg) kprintf ("%s: restart (%s).\n", ncr_name (np), msg);
4747 ** Clear Start Queue
4750 for (i=0;i<MAX_START;i++)
4751 np -> squeue [i] = NCB_SCRIPT_PHYS (np, idle);
4754 ** Start at first entry.
4758 WRITESCRIPT(startpos[0], NCB_SCRIPTH_PHYS (np, tryloop));
4759 WRITESCRIPT(start0 [0], SCR_INT ^ IFFALSE (0));
4762 ** Wakeup all pending jobs.
4765 ncr_wakeup (np, code);
4771 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort ... */
4772 OUTB (nc_scntl0, 0xca ); /* full arb., ena parity, par->ATN */
4773 OUTB (nc_scntl1, 0x00 ); /* odd parity, and remove CRST!! */
4774 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
4775 OUTB (nc_scid , RRE|np->myaddr);/* host adapter SCSI address */
4776 OUTW (nc_respid, 1ul<<np->myaddr);/* id to respond to */
4777 OUTB (nc_istat , SIGP ); /* Signal Process */
4778 OUTB (nc_dmode , np->rv_dmode); /* XXX modify burstlen ??? */
4779 OUTB (nc_dcntl , np->rv_dcntl);
4780 OUTB (nc_ctest3, np->rv_ctest3);
4781 OUTB (nc_ctest5, np->rv_ctest5);
4782 OUTB (nc_ctest4, np->rv_ctest4);/* enable master parity checking */
4783 OUTB (nc_stest2, np->rv_stest2|EXT); /* Extended Sreq/Sack filtering */
4784 OUTB (nc_stest3, TE ); /* TolerANT enable */
4785 OUTB (nc_stime0, 0x0b ); /* HTH = disabled, STO = 0.1 sec. */
4787 if (bootverbose >= 2) {
4788 kprintf ("\tACTUAL values:SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
4789 np->rv_scntl3, np->rv_dmode, np->rv_dcntl);
4790 kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
4791 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
4795 ** Enable GPIO0 pin for writing if LED support.
4798 if (np->features & FE_LED0) {
4799 OUTOFFB (nc_gpcntl, 0x01);
4803 ** Fill in target structure.
4805 for (i=0;i<MAX_TARGET;i++) {
4806 tcb_p tp = &np->target[i];
4809 tp->tinfo.wval = np->rv_scntl3;
4811 tp->tinfo.current.period = 0;
4812 tp->tinfo.current.offset = 0;
4813 tp->tinfo.current.width = MSG_EXT_WDTR_BUS_8_BIT;
4820 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST);
4821 OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
4824 ** Start script processor.
4827 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
4830 * Notify the XPT of the event
4832 if (code == HS_RESET)
4833 xpt_async(AC_BUS_RESET, np->path, NULL);
4837 ncr_poll(struct cam_sim *sim)
4839 ncr_intr(cam_sim_softc(sim));
4843 /*==========================================================
4845 ** Get clock factor and sync divisor for a given
4846 ** synchronous factor period.
4847 ** Returns the clock factor (in sxfer) and scntl3
4848 ** synchronous divisor field.
4850 **==========================================================
4853 static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
4855 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
4856 int div = np->clock_divn; /* Number of divisors supported */
4857 u_long fak; /* Sync factor in sxfer */
4858 u_long per; /* Period in tenths of ns */
4859 u_long kpc; /* (per * clk) */
4862 ** Compute the synchronous period in tenths of nano-seconds
4864 if (sfac <= 10) per = 250;
4865 else if (sfac == 11) per = 303;
4866 else if (sfac == 12) per = 500;
4867 else per = 40 * sfac;
4870 ** Look for the greatest clock divisor that allows an
4871 ** input speed faster than the period.
4875 if (kpc >= (div_10M[div] * 4)) break;
4878 ** Calculate the lowest clock factor that allows an output
4879 ** speed not faster than the period.
4881 fak = (kpc - 1) / div_10M[div] + 1;
4883 #if 0 /* You can #if 1 if you think this optimization is useful */
4885 per = (fak * div_10M[div]) / clk;
4888 ** Why not to try the immediate lower divisor and to choose
4889 ** the one that allows the fastest output speed ?
4890 ** We dont want input speed too much greater than output speed.
4892 if (div >= 1 && fak < 6) {
4894 fak2 = (kpc - 1) / div_10M[div-1] + 1;
4895 per2 = (fak2 * div_10M[div-1]) / clk;
4896 if (per2 < per && fak2 <= 6) {
4904 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
4907 ** Compute and return sync parameters for the ncr
4910 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
4913 /*==========================================================
4915 ** Switch sync mode for current job and its target
4917 **==========================================================
4921 ncr_setsync(ncb_p np, nccb_p cp, u_char scntl3, u_char sxfer, u_char period)
4924 struct ccb_trans_settings neg;
4927 u_int target = INB (nc_sdid) & 0x0f;
4936 assert (target == ccb->ccb_h.target_id);
4938 tp = &np->target[target];
4940 if (!scntl3 || !(sxfer & 0x1f))
4941 scntl3 = np->rv_scntl3;
4942 scntl3 = (scntl3 & 0xf0) | (tp->tinfo.wval & EWS)
4943 | (np->rv_scntl3 & 0x07);
4946 ** Deduce the value of controller sync period from scntl3.
4947 ** period is in tenths of nano-seconds.
4950 div = ((scntl3 >> 4) & 0x7);
4951 if ((sxfer & 0x1f) && div)
4953 (((sxfer>>5)+4)*div_10M[div-1])/np->clock_khz;
4957 tp->tinfo.goal.period = period;
4958 tp->tinfo.goal.offset = sxfer & 0x1f;
4959 tp->tinfo.current.period = period;
4960 tp->tinfo.current.offset = sxfer & 0x1f;
4963 ** Stop there if sync parameters are unchanged
4965 if (tp->tinfo.sval == sxfer && tp->tinfo.wval == scntl3) return;
4966 tp->tinfo.sval = sxfer;
4967 tp->tinfo.wval = scntl3;
4971 ** Disable extended Sreq/Sack filtering
4973 if (period_10ns <= 2000) OUTOFFB (nc_stest2, EXT);
4977 ** Tell the SCSI layer about the
4978 ** new transfer parameters.
4980 memset(&neg, 0, sizeof (neg));
4981 neg.protocol = PROTO_SCSI;
4982 neg.protocol_version = SCSI_REV_2;
4983 neg.transport = XPORT_SPI;
4984 neg.transport_version = 2;
4985 neg.xport_specific.spi.sync_period = period;
4986 neg.xport_specific.spi.sync_offset = sxfer & 0x1f;
4987 neg.xport_specific.spi.valid = CTS_SPI_VALID_SYNC_RATE
4988 | CTS_SPI_VALID_SYNC_OFFSET;
4989 xpt_setup_ccb(&neg.ccb_h, ccb->ccb_h.path,
4991 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, &neg);
4994 ** set actual value and sync_status
4996 OUTB (nc_sxfer, sxfer);
4997 np->sync_st = sxfer;
4998 OUTB (nc_scntl3, scntl3);
4999 np->wide_st = scntl3;
5002 ** patch ALL nccbs of this target.
5004 for (cp = np->link_nccb; cp; cp = cp->link_nccb) {
5005 if (!cp->ccb) continue;
5006 if (cp->ccb->ccb_h.target_id != target) continue;
5007 cp->sync_status = sxfer;
5008 cp->wide_status = scntl3;
5012 /*==========================================================
5014 ** Switch wide mode for current job and its target
5015 ** SCSI specs say: a SCSI device that accepts a WDTR
5016 ** message shall reset the synchronous agreement to
5017 ** asynchronous mode.
5019 **==========================================================
5022 static void ncr_setwide (ncb_p np, nccb_p cp, u_char wide, u_char ack)
5025 struct ccb_trans_settings neg;
5026 u_int target = INB (nc_sdid) & 0x0f;
5037 assert (target == ccb->ccb_h.target_id);
5039 tp = &np->target[target];
5040 tp->tinfo.current.width = wide;
5041 tp->tinfo.goal.width = wide;
5042 tp->tinfo.current.period = 0;
5043 tp->tinfo.current.offset = 0;
5045 scntl3 = (tp->tinfo.wval & (~EWS)) | (wide ? EWS : 0);
5047 sxfer = ack ? 0 : tp->tinfo.sval;
5050 ** Stop there if sync/wide parameters are unchanged
5052 if (tp->tinfo.sval == sxfer && tp->tinfo.wval == scntl3) return;
5053 tp->tinfo.sval = sxfer;
5054 tp->tinfo.wval = scntl3;
5056 /* Tell the SCSI layer about the new transfer params */
5057 memset(&neg, 0, sizeof (neg));
5058 neg.protocol = PROTO_SCSI;
5059 neg.protocol_version = SCSI_REV_2;
5060 neg.transport = XPORT_SPI;
5061 neg.transport_version = 2;
5062 neg.xport_specific.spi.bus_width = (scntl3 & EWS) ?
5063 MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT;
5064 neg.xport_specific.spi.sync_period = 0;
5065 neg.xport_specific.spi.sync_offset = 0;
5066 neg.xport_specific.spi.valid = CTS_SPI_VALID_SYNC_RATE
5067 | CTS_SPI_VALID_SYNC_OFFSET
5068 | CTS_SPI_VALID_BUS_WIDTH;
5069 xpt_setup_ccb(&neg.ccb_h, ccb->ccb_h.path, /*priority*/1);
5070 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, &neg);
5073 ** set actual value and sync_status
5075 OUTB (nc_sxfer, sxfer);
5076 np->sync_st = sxfer;
5077 OUTB (nc_scntl3, scntl3);
5078 np->wide_st = scntl3;
5081 ** patch ALL nccbs of this target.
5083 for (cp = np->link_nccb; cp; cp = cp->link_nccb) {
5084 if (!cp->ccb) continue;
5085 if (cp->ccb->ccb_h.target_id != target) continue;
5086 cp->sync_status = sxfer;
5087 cp->wide_status = scntl3;
5091 /*==========================================================
5094 ** ncr timeout handler.
5097 **==========================================================
5099 ** Misused to keep the driver running when
5100 ** interrupts are not configured correctly.
5102 **----------------------------------------------------------
5106 ncr_timeout (void *arg)
5109 time_t thistime = time_second;
5110 ticks_t step = np->ticks;
5115 if (np->lasttime != thistime) {
5117 ** block ncr interrupts
5120 np->lasttime = thistime;
5122 /*----------------------------------------------------
5124 ** handle ncr chip timeouts
5127 ** We have a chance to arbitrate for the
5128 ** SCSI bus at least every 10 seconds.
5130 **----------------------------------------------------
5133 t = thistime - np->heartbeat;
5135 if (t<2) np->latetime=0; else np->latetime++;
5137 if (np->latetime>2) {
5139 ** If there are no requests, the script
5140 ** processor will sleep on SEL_WAIT_RESEL.
5141 ** But we have to check whether it died.
5142 ** Let's try to wake it up.
5144 OUTB (nc_istat, SIGP);
5147 /*----------------------------------------------------
5149 ** handle nccb timeouts
5151 **----------------------------------------------------
5154 for (cp=np->link_nccb; cp; cp=cp->link_nccb) {
5156 ** look for timed out nccbs.
5158 if (!cp->host_status) continue;
5160 if (cp->tlimit > thistime) continue;
5163 ** Disable reselect.
5164 ** Remove it from startqueue.
5166 cp->jump_nccb.l_cmd = (SCR_JUMP);
5167 if (cp->phys.header.launch.l_paddr ==
5168 NCB_SCRIPT_PHYS (np, select)) {
5169 kprintf ("%s: timeout nccb=%p (skip)\n",
5171 cp->phys.header.launch.l_paddr
5172 = NCB_SCRIPT_PHYS (np, skip);
5175 switch (cp->host_status) {
5181 cp->host_status=HS_TIMEOUT;
5186 ** wakeup this nccb.
5188 ncr_complete (np, cp);
5193 callout_reset(&np->timeout_ch, step ? step : 1, ncr_timeout, np);
5195 if (INB(nc_istat) & (INTF|SIP|DIP)) {
5198 ** Process pending interrupts.
5202 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("{");
5204 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("}");
5209 /*==========================================================
5211 ** log message for real hard errors
5213 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
5214 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
5216 ** exception register:
5221 ** so: control lines as driver by NCR.
5222 ** si: control lines as seen by NCR.
5223 ** sd: scsi data lines as seen by NCR.
5226 ** sxfer: (see the manual)
5227 ** scntl3: (see the manual)
5229 ** current script command:
5230 ** dsp: script adress (relative to start of script).
5231 ** dbc: first word of script command.
5233 ** First 16 register of the chip:
5236 **==========================================================
5239 static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
5245 u_char *script_base;
5250 if (np->p_script < dsp &&
5251 dsp <= np->p_script + sizeof(struct script)) {
5252 script_ofs = dsp - np->p_script;
5253 script_size = sizeof(struct script);
5254 script_base = (u_char *) np->script;
5255 script_name = "script";
5257 else if (np->p_scripth < dsp &&
5258 dsp <= np->p_scripth + sizeof(struct scripth)) {
5259 script_ofs = dsp - np->p_scripth;
5260 script_size = sizeof(struct scripth);
5261 script_base = (u_char *) np->scripth;
5262 script_name = "scripth";
5267 script_name = "mem";
5270 kprintf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
5271 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
5272 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
5273 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
5274 (unsigned)INL (nc_dbc));
5276 if (((script_ofs & 3) == 0) &&
5277 (unsigned)script_ofs < script_size) {
5278 kprintf ("%s: script cmd = %08x\n", ncr_name(np),
5279 (int)READSCRIPT_OFF(script_base, script_ofs));
5282 kprintf ("%s: regdump:", ncr_name(np));
5284 kprintf (" %02x", (unsigned)INB_OFF(i));
5288 /*==========================================================
5291 ** ncr chip exception handler.
5294 **==========================================================
5297 void ncr_exception (ncb_p np)
5299 u_char istat, dstat;
5303 ** interrupt on the fly ?
5305 while ((istat = INB (nc_istat)) & INTF) {
5306 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("F ");
5307 OUTB (nc_istat, INTF);
5308 np->profile.num_fly++;
5311 if (!(istat & (SIP|DIP))) {
5316 ** Steinbach's Guideline for Systems Programming:
5317 ** Never test for an error condition you don't know how to handle.
5320 sist = (istat & SIP) ? INW (nc_sist) : 0;
5321 dstat = (istat & DIP) ? INB (nc_dstat) : 0;
5322 np->profile.num_int++;
5324 if (DEBUG_FLAGS & DEBUG_TINY)
5325 kprintf ("<%d|%x:%x|%x:%x>",
5328 (unsigned)INL(nc_dsp),
5329 (unsigned)INL(nc_dbc));
5330 if ((dstat==DFE) && (sist==PAR)) return;
5332 /*==========================================================
5334 ** First the normal cases.
5336 **==========================================================
5338 /*-------------------------------------------
5340 **-------------------------------------------
5344 ncr_init (np, bootverbose ? "scsi reset" : NULL, HS_RESET);
5348 /*-------------------------------------------
5349 ** selection timeout
5351 ** IID excluded from dstat mask!
5353 **-------------------------------------------
5357 !(sist & (GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5358 !(dstat & (MDPE|BF|ABRT|SIR))) {
5363 /*-------------------------------------------
5365 **-------------------------------------------
5369 !(sist & (STO|GEN|HTH|SGE|UDC|RST|PAR)) &&
5370 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5371 ncr_int_ma (np, dstat);
5375 /*----------------------------------------
5376 ** move command with length 0
5377 **----------------------------------------
5380 if ((dstat & IID) &&
5381 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5382 !(dstat & (MDPE|BF|ABRT|SIR)) &&
5383 ((INL(nc_dbc) & 0xf8000000) == SCR_MOVE_TBL)) {
5385 ** Target wants more data than available.
5386 ** The "no_data" script will do it.
5388 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, no_data));
5392 /*-------------------------------------------
5393 ** Programmed interrupt
5394 **-------------------------------------------
5397 if ((dstat & SIR) &&
5398 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5399 !(dstat & (MDPE|BF|ABRT|IID)) &&
5400 (INB(nc_dsps) <= SIR_MAX)) {
5405 /*========================================
5406 ** log message for real hard errors
5407 **========================================
5410 ncr_log_hard_error(np, sist, dstat);
5412 /*========================================
5413 ** do the register dump
5414 **========================================
5417 if (time_second - np->regtime > 10) {
5419 np->regtime = time_second;
5420 for (i=0; i<sizeof(np->regdump); i++)
5421 ((volatile char*)&np->regdump)[i] = INB_OFF(i);
5422 np->regdump.nc_dstat = dstat;
5423 np->regdump.nc_sist = sist;
5427 /*----------------------------------------
5428 ** clean up the dma fifo
5429 **----------------------------------------
5432 if ( (INB(nc_sstat0) & (ILF|ORF|OLF) ) ||
5433 (INB(nc_sstat1) & (FF3210) ) ||
5434 (INB(nc_sstat2) & (ILF1|ORF1|OLF1)) || /* wide .. */
5436 kprintf ("%s: have to clear fifos.\n", ncr_name (np));
5437 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
5438 OUTB (nc_ctest3, np->rv_ctest3 | CLF);
5439 /* clear dma fifo */
5442 /*----------------------------------------
5443 ** handshake timeout
5444 **----------------------------------------
5448 kprintf ("%s: handshake timeout\n", ncr_name(np));
5449 OUTB (nc_scntl1, CRST);
5451 OUTB (nc_scntl1, 0x00);
5452 OUTB (nc_scr0, HS_FAIL);
5453 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
5457 /*----------------------------------------
5458 ** unexpected disconnect
5459 **----------------------------------------
5463 !(sist & (STO|GEN|HTH|MA|SGE|RST|PAR)) &&
5464 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5465 OUTB (nc_scr0, HS_UNEXPECTED);
5466 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
5470 /*----------------------------------------
5471 ** cannot disconnect
5472 **----------------------------------------
5475 if ((dstat & IID) &&
5476 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5477 !(dstat & (MDPE|BF|ABRT|SIR)) &&
5478 ((INL(nc_dbc) & 0xf8000000) == SCR_WAIT_DISC)) {
5480 ** Unexpected data cycle while waiting for disconnect.
5482 if (INB(nc_sstat2) & LDSC) {
5484 ** It's an early reconnect.
5485 ** Let's continue ...
5487 OUTB (nc_dcntl, np->rv_dcntl | STD);
5491 kprintf ("%s: INFO: LDSC while IID.\n",
5495 kprintf ("%s: target %d doesn't release the bus.\n",
5496 ncr_name (np), INB (nc_sdid)&0x0f);
5498 ** return without restarting the NCR.
5499 ** timeout will do the real work.
5504 /*----------------------------------------
5506 **----------------------------------------
5509 if ((dstat & SSI) &&
5510 !(sist & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5511 !(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5512 OUTB (nc_dcntl, np->rv_dcntl | STD);
5517 ** @RECOVER@ HTH, SGE, ABRT.
5519 ** We should try to recover from these interrupts.
5520 ** They may occur if there are problems with synch transfers, or
5521 ** if targets are switched on or off while the driver is running.
5525 /* clear scsi offsets */
5526 OUTB (nc_ctest3, np->rv_ctest3 | CLF);
5530 ** Freeze controller to be able to read the messages.
5533 if (DEBUG_FLAGS & DEBUG_FREEZE) {
5536 for (i=0; i<0x60; i++) {
5540 kprintf ("%s: reg[%d0]: ",
5549 val = bus_space_read_1(np->bst, np->bsh, i);
5550 kprintf (" %x%x", val/16, val%16);
5551 if (i%16==15) kprintf (".\n");
5554 callout_stop(&np->timeout_ch);
5556 kprintf ("%s: halted!\n", ncr_name(np));
5558 ** don't restart controller ...
5560 OUTB (nc_istat, SRST);
5566 ** Freeze system to be able to read the messages.
5568 kprintf ("ncr: fatal error: system halted - press reset to reboot ...");
5574 ** sorry, have to kill ALL jobs ...
5577 ncr_init (np, "fatal error", HS_FAIL);
5580 /*==========================================================
5582 ** ncr chip exception handler for selection timeout
5584 **==========================================================
5586 ** There seems to be a bug in the 53c810.
5587 ** Although a STO-Interrupt is pending,
5588 ** it continues executing script commands.
5589 ** But it will fail and interrupt (IID) on
5590 ** the next instruction where it's looking
5591 ** for a valid phase.
5593 **----------------------------------------------------------
5596 void ncr_int_sto (ncb_p np)
5598 u_long dsa, scratcha, diff;
5600 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("T");
5603 ** look for nccb and set the status.
5608 while (cp && (CCB_PHYS (cp, phys) != dsa))
5612 cp-> host_status = HS_SEL_TIMEOUT;
5613 ncr_complete (np, cp);
5617 ** repair start queue
5620 scratcha = INL (nc_scratcha);
5621 diff = scratcha - NCB_SCRIPTH_PHYS (np, tryloop);
5623 /* assert ((diff <= MAX_START * 20) && !(diff % 20));*/
5625 if ((diff <= MAX_START * 20) && !(diff % 20)) {
5626 WRITESCRIPT(startpos[0], scratcha);
5627 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
5630 ncr_init (np, "selection timeout", HS_FAIL);
5633 /*==========================================================
5636 ** ncr chip exception handler for phase errors.
5639 **==========================================================
5641 ** We have to construct a new transfer descriptor,
5642 ** to transfer the rest of the current block.
5644 **----------------------------------------------------------
5647 static void ncr_int_ma (ncb_p np, u_char dstat)
5654 volatile void *vdsp_base;
5656 u_int32_t oadr, olen;
5657 u_int32_t *tblp, *newcmd;
5658 u_char cmd, sbcl, ss0, ss2, ctest5;
5665 ss0 = INB (nc_sstat0);
5666 ss2 = INB (nc_sstat2);
5667 sbcl= INB (nc_sbcl);
5670 rest= dbc & 0xffffff;
5672 ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
5674 delta=(((ctest5<<8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
5676 delta=(INB (nc_dfifo) - rest) & 0x7f;
5680 ** The data in the dma fifo has not been transfered to
5681 ** the target -> add the amount to the rest
5682 ** and clear the data.
5683 ** Check the sstat2 register in case of wide transfer.
5686 if (!(dstat & DFE)) rest += delta;
5687 if (ss0 & OLF) rest++;
5688 if (ss0 & ORF) rest++;
5689 if (INB(nc_scntl3) & EWS) {
5690 if (ss2 & OLF1) rest++;
5691 if (ss2 & ORF1) rest++;
5693 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
5694 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
5697 ** locate matching cp
5700 while (cp && (CCB_PHYS (cp, phys) != dsa))
5704 kprintf ("%s: SCSI phase error fixup: CCB already dequeued (%p)\n",
5705 ncr_name (np), (void *) np->header.cp);
5708 if (cp != np->header.cp) {
5709 kprintf ("%s: SCSI phase error fixup: CCB address mismatch "
5710 "(%p != %p) np->nccb = %p\n",
5711 ncr_name (np), (void *)cp, (void *)np->header.cp,
5712 (void *)np->link_nccb);
5717 ** find the interrupted script command,
5718 ** and the address at which to continue.
5721 if (dsp == vtophys (&cp->patch[2])) {
5723 vdsp_off = offsetof(struct nccb, patch[0]);
5724 nxtdsp = READSCRIPT_OFF(vdsp_base, vdsp_off + 3*4);
5725 } else if (dsp == vtophys (&cp->patch[6])) {
5727 vdsp_off = offsetof(struct nccb, patch[4]);
5728 nxtdsp = READSCRIPT_OFF(vdsp_base, vdsp_off + 3*4);
5729 } else if (dsp > np->p_script &&
5730 dsp <= np->p_script + sizeof(struct script)) {
5731 vdsp_base = np->script;
5732 vdsp_off = dsp - np->p_script - 8;
5735 vdsp_base = np->scripth;
5736 vdsp_off = dsp - np->p_scripth - 8;
5741 ** log the information
5743 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE)) {
5744 kprintf ("P%x%x ",cmd&7, sbcl&7);
5745 kprintf ("RL=%d D=%d SS0=%x ",
5746 (unsigned) rest, (unsigned) delta, ss0);
5748 if (DEBUG_FLAGS & DEBUG_PHASE) {
5749 kprintf ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
5752 nxtdsp, (volatile char*)vdsp_base+vdsp_off, cmd);
5756 ** get old startaddress and old length.
5759 oadr = READSCRIPT_OFF(vdsp_base, vdsp_off + 1*4);
5761 if (cmd & 0x10) { /* Table indirect */
5762 tblp = (u_int32_t *) ((char*) &cp->phys + oadr);
5767 olen = READSCRIPT_OFF(vdsp_base, vdsp_off) & 0xffffff;
5770 if (DEBUG_FLAGS & DEBUG_PHASE) {
5771 kprintf ("OCMD=%x\nTBLP=%p OLEN=%lx OADR=%lx\n",
5772 (unsigned) (READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24),
5779 ** if old phase not dataphase, leave here.
5782 if (cmd != (READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24)) {
5783 PRINT_ADDR(cp->ccb);
5784 kprintf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
5786 (unsigned)READSCRIPT_OFF(vdsp_base, vdsp_off) >> 24);
5791 PRINT_ADDR(cp->ccb);
5792 kprintf ("phase change %x-%x %d@%08x resid=%d.\n",
5793 cmd&7, sbcl&7, (unsigned)olen,
5794 (unsigned)oadr, (unsigned)rest);
5796 OUTB (nc_dcntl, np->rv_dcntl | STD);
5801 ** choose the correct patch area.
5802 ** if savep points to one, choose the other.
5806 if (cp->phys.header.savep == vtophys (newcmd)) newcmd+=4;
5809 ** fillin the commands
5812 newcmd[0] = ((cmd & 0x0f) << 24) | rest;
5813 newcmd[1] = oadr + olen - rest;
5814 newcmd[2] = SCR_JUMP;
5817 if (DEBUG_FLAGS & DEBUG_PHASE) {
5818 PRINT_ADDR(cp->ccb);
5819 kprintf ("newcmd[%d] %x %x %x %x.\n",
5820 (int)(newcmd - cp->patch),
5821 (unsigned)newcmd[0],
5822 (unsigned)newcmd[1],
5823 (unsigned)newcmd[2],
5824 (unsigned)newcmd[3]);
5827 ** fake the return address (to the patch).
5828 ** and restart script processor at dispatcher.
5830 np->profile.num_break++;
5831 OUTL (nc_temp, vtophys (newcmd));
5833 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
5835 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, checkatn));
5838 /*==========================================================
5841 ** ncr chip exception handler for programmed interrupts.
5844 **==========================================================
5847 static int ncr_show_msg (u_char * msg)
5850 kprintf ("%x",*msg);
5851 if (*msg==MSG_EXTENDED) {
5853 if (i-1>msg[1]) break;
5854 kprintf ("-%x",msg[i]);
5857 } else if ((*msg & 0xf0) == 0x20) {
5858 kprintf ("-%x",msg[1]);
5864 void ncr_int_sir (ncb_p np)
5867 u_char chg, ofs, per, fak, wide;
5868 u_char num = INB (nc_dsps);
5871 u_int target = INB (nc_sdid) & 0x0f;
5872 tcb_p tp = &np->target[target];
5874 if (DEBUG_FLAGS & DEBUG_TINY) kprintf ("I#%d", num);
5877 case SIR_SENSE_RESTART:
5878 case SIR_STALL_RESTART:
5887 while (cp && (CCB_PHYS (cp, phys) != dsa))
5893 assert (cp == np->header.cp);
5894 if (cp != np->header.cp)
5900 /*--------------------------------------------------------------------
5902 ** Processing of interrupted getcc selects
5904 **--------------------------------------------------------------------
5907 case SIR_SENSE_RESTART:
5908 /*------------------------------------------
5909 ** Script processor is idle.
5910 ** Look for interrupted "check cond"
5911 **------------------------------------------
5914 if (DEBUG_FLAGS & DEBUG_RESTART)
5915 kprintf ("%s: int#%d",ncr_name (np),num);
5917 for (i=0; i<MAX_TARGET; i++) {
5918 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf (" t%d", i);
5919 tp = &np->target[i];
5920 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("+");
5923 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("+");
5924 if ((cp->host_status==HS_BUSY) &&
5925 (cp->s_status==SCSI_STATUS_CHECK_COND))
5927 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf ("- (remove)");
5928 tp->hold_cp = cp = (nccb_p) 0;
5932 if (DEBUG_FLAGS & DEBUG_RESTART)
5933 kprintf ("+ restart job ..\n");
5934 OUTL (nc_dsa, CCB_PHYS (cp, phys));
5935 OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, getcc));
5940 ** no job, resume normal processing
5942 if (DEBUG_FLAGS & DEBUG_RESTART) kprintf (" -- remove trap\n");
5943 WRITESCRIPT(start0[0], SCR_INT ^ IFFALSE (0));
5946 case SIR_SENSE_FAILED:
5947 /*-------------------------------------------
5948 ** While trying to select for
5949 ** getting the condition code,
5950 ** a target reselected us.
5951 **-------------------------------------------
5953 if (DEBUG_FLAGS & DEBUG_RESTART) {
5954 PRINT_ADDR(cp->ccb);
5955 kprintf ("in getcc reselect by t%d.\n",
5956 INB(nc_ssid) & 0x0f);
5962 cp->host_status = HS_BUSY;
5963 cp->s_status = SCSI_STATUS_CHECK_COND;
5964 np->target[cp->ccb->ccb_h.target_id].hold_cp = cp;
5967 ** And patch code to restart it.
5969 WRITESCRIPT(start0[0], SCR_INT);
5972 /*-----------------------------------------------------------------------------
5974 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
5976 ** We try to negotiate sync and wide transfer only after
5977 ** a successfull inquire command. We look at byte 7 of the
5978 ** inquire data to determine the capabilities if the target.
5980 ** When we try to negotiate, we append the negotiation message
5981 ** to the identify and (maybe) simple tag message.
5982 ** The host status field is set to HS_NEGOTIATE to mark this
5985 ** If the target doesn't answer this message immidiately
5986 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
5987 ** will be raised eventually.
5988 ** The handler removes the HS_NEGOTIATE status, and sets the
5989 ** negotiated value to the default (async / nowide).
5991 ** If we receive a matching answer immediately, we check it
5992 ** for validity, and set the values.
5994 ** If we receive a Reject message immediately, we assume the
5995 ** negotiation has failed, and fall back to standard values.
5997 ** If we receive a negotiation message while not in HS_NEGOTIATE
5998 ** state, it's a target initiated negotiation. We prepare a
5999 ** (hopefully) valid answer, set our parameters, and send back
6000 ** this answer to the target.
6002 ** If the target doesn't fetch the answer (no message out phase),
6003 ** we assume the negotiation has failed, and fall back to default
6006 ** When we set the values, we adjust them in all nccbs belonging
6007 ** to this target, in the controller's register, and in the "phys"
6008 ** field of the controller's struct ncb.
6010 ** Possible cases: hs sir msg_in value send goto
6011 ** We try try to negotiate:
6012 ** -> target doesnt't msgin NEG FAIL noop defa. - dispatch
6013 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
6014 ** -> target answered (ok) NEG SYNC sdtr set - clrack
6015 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
6016 ** -> target answered (ok) NEG WIDE wdtr set - clrack
6017 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
6018 ** -> any other msgin NEG FAIL noop defa. - dispatch
6020 ** Target tries to negotiate:
6021 ** -> incoming message --- SYNC sdtr set SDTR -
6022 ** -> incoming message --- WIDE wdtr set WDTR -
6023 ** We sent our answer:
6024 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
6026 **-----------------------------------------------------------------------------
6029 case SIR_NEGO_FAILED:
6030 /*-------------------------------------------------------
6032 ** Negotiation failed.
6033 ** Target doesn't send an answer message,
6034 ** or target rejected our message.
6036 ** Remove negotiation request.
6038 **-------------------------------------------------------
6040 OUTB (HS_PRT, HS_BUSY);
6044 case SIR_NEGO_PROTO:
6045 /*-------------------------------------------------------
6047 ** Negotiation failed.
6048 ** Target doesn't fetch the answer message.
6050 **-------------------------------------------------------
6053 if (DEBUG_FLAGS & DEBUG_NEGO) {
6054 PRINT_ADDR(cp->ccb);
6055 kprintf ("negotiation failed sir=%x status=%x.\n",
6056 num, cp->nego_status);
6060 ** any error in negotiation:
6061 ** fall back to default mode.
6063 switch (cp->nego_status) {
6066 ncr_setsync (np, cp, 0, 0xe0, 0);
6070 ncr_setwide (np, cp, 0, 0);
6074 np->msgin [0] = MSG_NOOP;
6075 np->msgout[0] = MSG_NOOP;
6076 cp->nego_status = 0;
6077 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
6082 ** Synchronous request message received.
6085 if (DEBUG_FLAGS & DEBUG_NEGO) {
6086 PRINT_ADDR(cp->ccb);
6087 kprintf ("sync msgin: ");
6088 (void) ncr_show_msg (np->msgin);
6093 ** get requested values.
6099 if (ofs==0) per=255;
6102 ** check values against driver limits.
6104 if (per < np->minsync)
6105 {chg = 1; per = np->minsync;}
6106 if (per < tp->tinfo.user.period)
6107 {chg = 1; per = tp->tinfo.user.period;}
6108 if (ofs > tp->tinfo.user.offset)
6109 {chg = 1; ofs = tp->tinfo.user.offset;}
6112 ** Check against controller limits.
6118 ncr_getsync(np, per, &fak, &scntl3);
6130 if (DEBUG_FLAGS & DEBUG_NEGO) {
6131 PRINT_ADDR(cp->ccb);
6132 kprintf ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
6133 per, scntl3, ofs, fak, chg);
6136 if (INB (HS_PRT) == HS_NEGOTIATE) {
6137 OUTB (HS_PRT, HS_BUSY);
6138 switch (cp->nego_status) {
6142 ** This was an answer message
6146 ** Answer wasn't acceptable.
6148 ncr_setsync (np, cp, 0, 0xe0, 0);
6149 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
6154 ncr_setsync (np,cp,scntl3,(fak<<5)|ofs, per);
6155 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
6160 ncr_setwide (np, cp, 0, 0);
6166 ** It was a request. Set value and
6167 ** prepare an answer message
6170 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs, per);
6172 np->msgout[0] = MSG_EXTENDED;
6174 np->msgout[2] = MSG_EXT_SDTR;
6175 np->msgout[3] = per;
6176 np->msgout[4] = ofs;
6178 cp->nego_status = NS_SYNC;
6180 if (DEBUG_FLAGS & DEBUG_NEGO) {
6181 PRINT_ADDR(cp->ccb);
6182 kprintf ("sync msgout: ");
6183 (void) ncr_show_msg (np->msgout);
6188 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
6191 np->msgin [0] = MSG_NOOP;
6197 ** Wide request message received.
6199 if (DEBUG_FLAGS & DEBUG_NEGO) {
6200 PRINT_ADDR(cp->ccb);
6201 kprintf ("wide msgin: ");
6202 (void) ncr_show_msg (np->msgin);
6207 ** get requested values.
6211 wide = np->msgin[3];
6214 ** check values against driver limits.
6217 if (wide > tp->tinfo.user.width)
6218 {chg = 1; wide = tp->tinfo.user.width;}
6220 if (DEBUG_FLAGS & DEBUG_NEGO) {
6221 PRINT_ADDR(cp->ccb);
6222 kprintf ("wide: wide=%d chg=%d.\n", wide, chg);
6225 if (INB (HS_PRT) == HS_NEGOTIATE) {
6226 OUTB (HS_PRT, HS_BUSY);
6227 switch (cp->nego_status) {
6231 ** This was an answer message
6235 ** Answer wasn't acceptable.
6237 ncr_setwide (np, cp, 0, 1);
6238 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
6243 ncr_setwide (np, cp, wide, 1);
6244 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
6249 ncr_setsync (np, cp, 0, 0xe0, 0);
6255 ** It was a request, set value and
6256 ** prepare an answer message
6259 ncr_setwide (np, cp, wide, 1);
6261 np->msgout[0] = MSG_EXTENDED;
6263 np->msgout[2] = MSG_EXT_WDTR;
6264 np->msgout[3] = wide;
6266 np->msgin [0] = MSG_NOOP;
6268 cp->nego_status = NS_WIDE;
6270 if (DEBUG_FLAGS & DEBUG_NEGO) {
6271 PRINT_ADDR(cp->ccb);
6272 kprintf ("wide msgout: ");
6273 (void) ncr_show_msg (np->msgout);
6278 /*--------------------------------------------------------------------
6280 ** Processing of special messages
6282 **--------------------------------------------------------------------
6285 case SIR_REJECT_RECEIVED:
6286 /*-----------------------------------------------
6288 ** We received a MSG_MESSAGE_REJECT message.
6290 **-----------------------------------------------
6293 PRINT_ADDR(cp->ccb);
6294 kprintf ("MSG_MESSAGE_REJECT received (%x:%x).\n",
6295 (unsigned)np->lastmsg, np->msgout[0]);
6298 case SIR_REJECT_SENT:
6299 /*-----------------------------------------------
6301 ** We received an unknown message
6303 **-----------------------------------------------
6306 PRINT_ADDR(cp->ccb);
6307 kprintf ("MSG_MESSAGE_REJECT sent for ");
6308 (void) ncr_show_msg (np->msgin);
6312 /*--------------------------------------------------------------------
6314 ** Processing of special messages
6316 **--------------------------------------------------------------------
6319 case SIR_IGN_RESIDUE:
6320 /*-----------------------------------------------
6322 ** We received an IGNORE RESIDUE message,
6323 ** which couldn't be handled by the script.
6325 **-----------------------------------------------
6328 PRINT_ADDR(cp->ccb);
6329 kprintf ("MSG_IGN_WIDE_RESIDUE received, but not yet implemented.\n");
6332 case SIR_MISSING_SAVE:
6333 /*-----------------------------------------------
6335 ** We received an DISCONNECT message,
6336 ** but the datapointer wasn't saved before.
6338 **-----------------------------------------------
6341 PRINT_ADDR(cp->ccb);
6342 kprintf ("MSG_DISCONNECT received, but datapointer not saved:\n"
6343 "\tdata=%x save=%x goal=%x.\n",
6344 (unsigned) INL (nc_temp),
6345 (unsigned) np->header.savep,
6346 (unsigned) np->header.goalp);
6349 /*--------------------------------------------------------------------
6351 ** Processing of a "SCSI_STATUS_QUEUE_FULL" status.
6353 ** XXX JGibbs - We should do the same thing for BUSY status.
6355 ** The current command has been rejected,
6356 ** because there are too many in the command queue.
6357 ** We have started too many commands for that target.
6359 **--------------------------------------------------------------------
6361 case SIR_STALL_QUEUE:
6362 cp->xerr_status = XE_OK;
6363 cp->host_status = HS_COMPLETE;
6364 cp->s_status = SCSI_STATUS_QUEUE_FULL;
6365 ncr_freeze_devq(np, cp->ccb->ccb_h.path);
6366 ncr_complete(np, cp);
6370 case SIR_STALL_RESTART:
6371 /*-----------------------------------------------
6373 ** Enable selecting again,
6374 ** if NO disconnected jobs.
6376 **-----------------------------------------------
6379 ** Look for a disconnected job.
6382 while (cp && cp->host_status != HS_DISCONNECT)
6386 ** if there is one, ...
6390 ** wait for reselection
6392 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
6397 ** else remove the interrupt.
6400 kprintf ("%s: queue empty.\n", ncr_name (np));
6401 WRITESCRIPT(start1[0], SCR_INT ^ IFFALSE (0));
6406 OUTB (nc_dcntl, np->rv_dcntl | STD);
6409 /*==========================================================
6412 ** Acquire a control block
6415 **==========================================================
6418 static nccb_p ncr_get_nccb
6419 (ncb_p np, u_long target, u_long lun)
6424 /* Keep our timeout handler out */
6428 ** Lun structure available ?
6431 lp = np->target[target].lp[lun];
6436 ** Look for free CCB
6439 while (cp && cp->magic) {
6445 ** if nothing available, create one.
6449 cp = ncr_alloc_nccb(np, target, lun);
6453 kprintf("%s: Bogus free cp found\n", ncr_name(np));
6463 /*==========================================================
6466 ** Release one control block
6469 **==========================================================
6472 void ncr_free_nccb (ncb_p np, nccb_p cp)
6478 assert (cp != NULL);
6480 cp -> host_status = HS_IDLE;
6484 /*==========================================================
6487 ** Allocation of resources for Targets/Luns/Tags.
6490 **==========================================================
6494 ncr_alloc_nccb (ncb_p np, u_long target, u_long lun)
6500 assert (np != NULL);
6502 if (target>=MAX_TARGET) return(NULL);
6503 if (lun >=MAX_LUN ) return(NULL);
6505 tp=&np->target[target];
6507 if (!tp->jump_tcb.l_cmd) {
6512 tp->jump_tcb.l_cmd = (SCR_JUMP^IFFALSE (DATA (0x80 + target)));
6513 tp->jump_tcb.l_paddr = np->jump_tcb.l_paddr;
6516 (np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
6517 tp->getscr[1] = vtophys (&tp->tinfo.sval);
6518 tp->getscr[2] = rman_get_start(np->reg_res) + offsetof (struct ncr_reg, nc_sxfer);
6520 (np->features & FE_PFEN)? SCR_COPY(1) : SCR_COPY_F(1);
6521 tp->getscr[4] = vtophys (&tp->tinfo.wval);
6522 tp->getscr[5] = rman_get_start(np->reg_res) + offsetof (struct ncr_reg, nc_scntl3);
6524 assert (((offsetof(struct ncr_reg, nc_sxfer) ^
6525 (offsetof(struct tcb ,tinfo)
6526 + offsetof(struct ncr_target_tinfo, sval))) & 3) == 0);
6527 assert (((offsetof(struct ncr_reg, nc_scntl3) ^
6528 (offsetof(struct tcb, tinfo)
6529 + offsetof(struct ncr_target_tinfo, wval))) &3) == 0);
6531 tp->call_lun.l_cmd = (SCR_CALL);
6532 tp->call_lun.l_paddr = NCB_SCRIPT_PHYS (np, resel_lun);
6534 tp->jump_lcb.l_cmd = (SCR_JUMP);
6535 tp->jump_lcb.l_paddr = NCB_SCRIPTH_PHYS (np, abort);
6536 np->jump_tcb.l_paddr = vtophys (&tp->jump_tcb);
6540 ** Logic unit control block
6547 lp = kmalloc (sizeof (struct lcb), M_DEVBUF, M_WAITOK | M_ZERO);
6552 lp->jump_lcb.l_cmd = (SCR_JUMP ^ IFFALSE (DATA (lun)));
6553 lp->jump_lcb.l_paddr = tp->jump_lcb.l_paddr;
6555 lp->call_tag.l_cmd = (SCR_CALL);
6556 lp->call_tag.l_paddr = NCB_SCRIPT_PHYS (np, resel_tag);
6558 lp->jump_nccb.l_cmd = (SCR_JUMP);
6559 lp->jump_nccb.l_paddr = NCB_SCRIPTH_PHYS (np, aborttag);
6564 ** Chain into LUN list
6566 tp->jump_lcb.l_paddr = vtophys (&lp->jump_lcb);
6574 cp = kmalloc (sizeof (struct nccb), M_DEVBUF, M_WAITOK | M_ZERO);
6576 if (DEBUG_FLAGS & DEBUG_ALLOC) {
6577 kprintf ("new nccb @%p.\n", cp);
6581 ** Fill in physical addresses
6584 cp->p_nccb = vtophys (cp);
6587 ** Chain into reselect list
6589 cp->jump_nccb.l_cmd = SCR_JUMP;
6590 cp->jump_nccb.l_paddr = lp->jump_nccb.l_paddr;
6591 lp->jump_nccb.l_paddr = CCB_PHYS (cp, jump_nccb);
6592 cp->call_tmp.l_cmd = SCR_CALL;
6593 cp->call_tmp.l_paddr = NCB_SCRIPT_PHYS (np, resel_tmp);
6596 ** Chain into wakeup list
6598 cp->link_nccb = np->link_nccb;
6602 ** Chain into CCB list
6604 cp->next_nccb = lp->next_nccb;
6610 /*==========================================================
6613 ** Build Scatter Gather Block
6616 **==========================================================
6618 ** The transfer area may be scattered among
6619 ** several non adjacent physical pages.
6621 ** We may use MAX_SCATTER blocks.
6623 **----------------------------------------------------------
6626 static int ncr_scatter
6627 (struct dsb* phys, vm_offset_t vaddr, vm_size_t datalen)
6629 u_long paddr, pnext;
6631 u_short segment = 0;
6632 u_long segsize, segaddr;
6633 u_long size, csize = 0;
6634 u_long chunk = MAX_SIZE;
6637 bzero (&phys->data, sizeof (phys->data));
6638 if (!datalen) return (0);
6640 paddr = vtophys (vaddr);
6643 ** insert extra break points at a distance of chunk.
6644 ** We try to reduce the number of interrupts caused
6645 ** by unexpected phase changes due to disconnects.
6646 ** A typical harddisk may disconnect before ANY block.
6647 ** If we wanted to avoid unexpected phase changes at all
6648 ** we had to use a break point every 512 bytes.
6649 ** Of course the number of scatter/gather blocks is
6653 free = MAX_SCATTER - 1;
6655 if (vaddr & PAGE_MASK) free -= datalen / PAGE_SIZE;
6658 while ((chunk * free >= 2 * datalen) && (chunk>=1024))
6661 if(DEBUG_FLAGS & DEBUG_SCATTER)
6662 kprintf("ncr?:\tscattering virtual=%p size=%d chunk=%d.\n",
6663 (void *) vaddr, (unsigned) datalen, (unsigned) chunk);
6666 ** Build data descriptors.
6668 while (datalen && (segment < MAX_SCATTER)) {
6671 ** this segment is empty
6677 if (!csize) csize = chunk;
6679 while ((datalen) && (paddr == pnext) && (csize)) {
6682 ** continue this segment
6684 pnext = (paddr & (~PAGE_MASK)) + PAGE_SIZE;
6690 size = pnext - paddr; /* page size */
6691 if (size > datalen) size = datalen; /* data size */
6692 if (size > csize ) size = csize ; /* chunksize */
6698 paddr = vtophys (vaddr);
6701 if(DEBUG_FLAGS & DEBUG_SCATTER)
6702 kprintf ("\tseg #%d addr=%x size=%d (rest=%d).\n",
6706 (unsigned) datalen);
6708 phys->data[segment].addr = segaddr;
6709 phys->data[segment].size = segsize;
6714 kprintf("ncr?: scatter/gather failed (residue=%d).\n",
6715 (unsigned) datalen);
6722 /*==========================================================
6725 ** Test the pci bus snoop logic :-(
6727 ** Has to be called with interrupts disabled.
6730 **==========================================================
6733 #ifndef NCR_IOMAPPED
6734 static int ncr_regtest (struct ncb* np)
6736 volatile u_int32_t data;
6738 ** ncr registers may NOT be cached.
6739 ** write 0xffffffff to a read only register area,
6740 ** and try to read it back.
6743 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
6744 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
6746 if (data == 0xffffffff) {
6748 if ((data & 0xe2f0fffd) != 0x02000080) {
6750 kprintf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
6758 static int ncr_snooptest (struct ncb* np)
6760 u_int32_t ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
6762 #ifndef NCR_IOMAPPED
6763 err |= ncr_regtest (np);
6764 if (err) return (err);
6769 pc = NCB_SCRIPTH_PHYS (np, snooptest);
6773 ** Set memory and register.
6775 ncr_cache = host_wr;
6776 OUTL (nc_temp, ncr_wr);
6778 ** Start script (exchange values)
6782 ** Wait 'til done (with timeout)
6784 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
6785 if (INB(nc_istat) & (INTF|SIP|DIP))
6788 ** Save termination position.
6792 ** Read memory and register.
6794 host_rd = ncr_cache;
6795 ncr_rd = INL (nc_scratcha);
6796 ncr_bk = INL (nc_temp);
6800 OUTB (nc_istat, SRST);
6802 OUTB (nc_istat, 0 );
6804 ** check for timeout
6806 if (i>=NCR_SNOOP_TIMEOUT) {
6807 kprintf ("CACHE TEST FAILED: timeout.\n");
6811 ** Check termination position.
6813 if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
6814 kprintf ("CACHE TEST FAILED: script execution failed.\n");
6815 kprintf ("start=%08lx, pc=%08lx, end=%08lx\n",
6816 (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
6817 (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
6823 if (host_wr != ncr_rd) {
6824 kprintf ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
6825 (int) host_wr, (int) ncr_rd);
6828 if (host_rd != ncr_wr) {
6829 kprintf ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
6830 (int) ncr_wr, (int) host_rd);
6833 if (ncr_bk != ncr_wr) {
6834 kprintf ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
6835 (int) ncr_wr, (int) ncr_bk);
6841 /*==========================================================
6844 ** Profiling the drivers and targets performance.
6847 **==========================================================
6851 ** Compute the difference in milliseconds.
6854 static int ncr_delta (int *from, int *to)
6856 if (!from) return (-1);
6857 if (!to) return (-2);
6858 return ((to - from) * 1000 / hz);
6861 #define PROFILE cp->phys.header.stamp
6862 static void ncb_profile (ncb_p np, nccb_p cp)
6864 int co, da, st, en, di, se, post,work,disc;
6867 PROFILE.end = ticks;
6869 st = ncr_delta (&PROFILE.start,&PROFILE.status);
6870 if (st<0) return; /* status not reached */
6872 da = ncr_delta (&PROFILE.start,&PROFILE.data);
6873 if (da<0) return; /* No data transfer phase */
6875 co = ncr_delta (&PROFILE.start,&PROFILE.command);
6876 if (co<0) return; /* command not executed */
6878 en = ncr_delta (&PROFILE.start,&PROFILE.end),
6879 di = ncr_delta (&PROFILE.start,&PROFILE.disconnect),
6880 se = ncr_delta (&PROFILE.start,&PROFILE.select);
6884 ** @PROFILE@ Disconnect time invalid if multiple disconnects
6887 if (di>=0) disc = se-di; else disc = 0;
6889 work = (st - co) - disc;
6891 diff = (np->disc_phys - np->disc_ref) & 0xff;
6892 np->disc_ref += diff;
6894 np->profile.num_trans += 1;
6896 np->profile.num_bytes += cp->ccb->csio.dxfer_len;
6897 np->profile.num_disc += diff;
6898 np->profile.ms_setup += co;
6899 np->profile.ms_data += work;
6900 np->profile.ms_disc += disc;
6901 np->profile.ms_post += post;
6905 /*==========================================================
6907 ** Determine the ncr's clock frequency.
6908 ** This is essential for the negotiation
6909 ** of the synchronous transfer rate.
6911 **==========================================================
6913 ** Note: we have to return the correct value.
6914 ** THERE IS NO SAVE DEFAULT VALUE.
6916 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
6917 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
6918 ** do not have a clock doubler and so are provided with a
6919 ** 80 MHz clock. All other fast20 boards incorporate a doubler
6920 ** and so should be delivered with a 40 MHz clock.
6921 ** The future fast40 chips (895/895) use a 40 Mhz base clock
6922 ** and provide a clock quadrupler (160 Mhz). The code below
6923 ** tries to deal as cleverly as possible with all this stuff.
6925 **----------------------------------------------------------
6929 * Select NCR SCSI clock frequency
6931 static void ncr_selectclock(ncb_p np, u_char scntl3)
6933 if (np->multiplier < 2) {
6934 OUTB(nc_scntl3, scntl3);
6938 if (bootverbose >= 2)
6939 kprintf ("%s: enabling clock multiplier\n", ncr_name(np));
6941 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
6942 if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
6944 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
6947 kprintf("%s: the chip cannot lock the frequency\n", ncr_name(np));
6948 } else /* Wait 20 micro-seconds for doubler */
6950 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
6951 OUTB(nc_scntl3, scntl3);
6952 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
6953 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
6957 * calculate NCR SCSI clock frequency (in KHz)
6960 ncrgetfreq (ncb_p np, int gen)
6964 * Measure GEN timer delay in order
6965 * to calculate SCSI clock frequency
6967 * This code will never execute too
6968 * many loop iterations (if DELAY is
6969 * reasonably correct). It could get
6970 * too low a delay (too high a freq.)
6971 * if the CPU is slow executing the
6972 * loop for some reason (an NMI, for
6973 * example). For this reason we will
6974 * if multiple measurements are to be
6975 * performed trust the higher delay
6976 * (lower frequency returned).
6978 OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
6979 OUTW (nc_sien , 0); /* mask all scsi interrupts */
6980 (void) INW (nc_sist); /* clear pending scsi interrupt */
6981 OUTB (nc_dien , 0); /* mask all dma interrupts */
6982 (void) INW (nc_sist); /* another one, just to be sure :) */
6983 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
6984 OUTB (nc_stime1, 0); /* disable general purpose timer */
6985 OUTB (nc_stime1, gen); /* set to nominal delay of (1<<gen) * 125us */
6986 while (!(INW(nc_sist) & GEN) && ms++ < 1000)
6987 DELAY(1000); /* count ms */
6988 OUTB (nc_stime1, 0); /* disable general purpose timer */
6989 OUTB (nc_scntl3, 0);
6991 * Set prescaler to divide by whatever "0" means.
6992 * "0" ought to choose divide by 2, but appears
6993 * to set divide by 3.5 mode in my 53c810 ...
6995 OUTB (nc_scntl3, 0);
6997 if (bootverbose >= 2)
6998 kprintf ("\tDelay (GEN=%d): %u msec\n", gen, ms);
7000 * adjust for prescaler, and convert into KHz
7002 return ms ? ((1 << gen) * 4440) / ms : 0;
7005 static void ncr_getclock (ncb_p np, u_char multiplier)
7007 unsigned char scntl3;
7008 unsigned char stest1;
7009 scntl3 = INB(nc_scntl3);
7010 stest1 = INB(nc_stest1);
7014 if (multiplier > 1) {
7015 np->multiplier = multiplier;
7016 np->clock_khz = 40000 * multiplier;
7018 if ((scntl3 & 7) == 0) {
7020 /* throw away first result */
7021 (void) ncrgetfreq (np, 11);
7022 f1 = ncrgetfreq (np, 11);
7023 f2 = ncrgetfreq (np, 11);
7025 if (bootverbose >= 2)
7026 kprintf ("\tNCR clock is %uKHz, %uKHz\n", f1, f2);
7027 if (f1 > f2) f1 = f2; /* trust lower result */
7029 scntl3 = 5; /* >45Mhz: assume 80MHz */
7031 scntl3 = 3; /* <45Mhz: assume 40MHz */
7034 else if ((scntl3 & 7) == 5)
7035 np->clock_khz = 80000; /* Probably a 875 rev. 1 ? */
7039 /*=========================================================================*/
7041 #ifdef NCR_TEKRAM_EEPROM
7043 struct tekram_eeprom_dev {
7045 #define TKR_PARCHK 0x01
7046 #define TKR_TRYSYNC 0x02
7047 #define TKR_ENDISC 0x04
7048 #define TKR_STARTUNIT 0x08
7049 #define TKR_USETAGS 0x10
7050 #define TKR_TRYWIDE 0x20
7051 u_char syncparam; /* max. sync transfer rate (table ?) */
7057 struct tekram_eeprom {
7058 struct tekram_eeprom_dev
7062 #define TKR_ADPT_GT2DRV 0x01
7063 #define TKR_ADPT_GT1GB 0x02
7064 #define TKR_ADPT_RSTBUS 0x04
7065 #define TKR_ADPT_ACTNEG 0x08
7066 #define TKR_ADPT_NOSEEK 0x10
7067 #define TKR_ADPT_MORLUN 0x20
7068 u_char delay; /* unit ? ( table ??? ) */
7069 u_char tags; /* use 4 times as many ... */
7074 tekram_write_bit (ncb_p np, int bit)
7076 u_char val = 0x10 + ((bit & 1) << 1);
7079 OUTB (nc_gpreg, val);
7081 OUTB (nc_gpreg, val | 0x04);
7083 OUTB (nc_gpreg, val);
7088 tekram_read_bit (ncb_p np)
7090 OUTB (nc_gpreg, 0x10);
7092 OUTB (nc_gpreg, 0x14);
7094 return INB (nc_gpreg) & 1;
7098 read_tekram_eeprom_reg (ncb_p np, int reg)
7102 int cmd = 0x80 | reg;
7104 OUTB (nc_gpreg, 0x10);
7106 tekram_write_bit (np, 1);
7107 for (bit = 7; bit >= 0; bit--)
7109 tekram_write_bit (np, cmd >> bit);
7112 for (bit = 0; bit < 16; bit++)
7115 result |= tekram_read_bit (np);
7118 OUTB (nc_gpreg, 0x00);
7123 read_tekram_eeprom(ncb_p np, struct tekram_eeprom *buffer)
7125 u_short *p = (u_short *) buffer;
7129 if (INB (nc_gpcntl) != 0x09)
7133 for (i = 0; i < 64; i++)
7136 if((i&0x0f) == 0) kprintf ("%02x:", i*2);
7137 val = read_tekram_eeprom_reg (np, i);
7141 if((i&0x01) == 0x00) kprintf (" ");
7142 kprintf ("%02x%02x", val & 0xff, (val >> 8) & 0xff);
7143 if((i&0x0f) == 0x0f) kprintf ("\n");
7145 kprintf ("Sum = %04x\n", sum);
7146 return sum == 0x1234;
7148 #endif /* NCR_TEKRAM_EEPROM */
7150 static device_method_t ncr_methods[] = {
7151 /* Device interface */
7152 DEVMETHOD(device_probe, ncr_probe),
7153 DEVMETHOD(device_attach, ncr_attach),
7158 static driver_t ncr_driver = {
7164 static devclass_t ncr_devclass;
7166 DRIVER_MODULE(if_ncr, pci, ncr_driver, ncr_devclass, 0, 0);
7168 /*=========================================================================*/
7169 #endif /* _KERNEL */