1 /**************************************************************************
3 ** $FreeBSD: src/sys/pci/ncr.c,v 1.155.2.3 2001/03/05 13:09:10 obrien Exp $
5 ** Device driver for the NCR 53C8XX PCI-SCSI-Controller Family.
7 **-------------------------------------------------------------------------
9 ** Written for 386bsd and FreeBSD by
10 ** Wolfgang Stanglmeier <wolf@cologne.de>
11 ** Stefan Esser <se@mi.Uni-Koeln.de>
13 **-------------------------------------------------------------------------
15 ** Copyright (c) 1994 Wolfgang Stanglmeier. All rights reserved.
17 ** Redistribution and use in source and binary forms, with or without
18 ** modification, are permitted provided that the following conditions
20 ** 1. Redistributions of source code must retain the above copyright
21 ** notice, this list of conditions and the following disclaimer.
22 ** 2. Redistributions in binary form must reproduce the above copyright
23 ** notice, this list of conditions and the following disclaimer in the
24 ** documentation and/or other materials provided with the distribution.
25 ** 3. The name of the author may not be used to endorse or promote products
26 ** derived from this software without specific prior written permission.
28 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ***************************************************************************
42 #define NCR_DATE "pl30 98/1/1"
44 #define NCR_VERSION (2)
45 #define MAX_UNITS (16)
47 #define NCR_GETCC_WITHMSG
49 #if (defined(__DragonFly__) || defined (__FreeBSD__)) && defined(_KERNEL)
53 /*==========================================================
55 ** Configuration and Debugging
57 ** May be overwritten in <arch/conf/xxxx>
59 **==========================================================
63 ** SCSI address of this device.
64 ** The boot routines should have set it.
68 #ifndef SCSI_NCR_MYADDR
69 #define SCSI_NCR_MYADDR (7)
70 #endif /* SCSI_NCR_MYADDR */
73 ** The default synchronous period factor
75 ** If maximum synchronous frequency is defined, use it instead.
78 #ifndef SCSI_NCR_MAX_SYNC
80 #ifndef SCSI_NCR_DFLT_SYNC
81 #define SCSI_NCR_DFLT_SYNC (12)
82 #endif /* SCSI_NCR_DFLT_SYNC */
86 #if SCSI_NCR_MAX_SYNC == 0
87 #define SCSI_NCR_DFLT_SYNC 0
89 #define SCSI_NCR_DFLT_SYNC (250000 / SCSI_NCR_MAX_SYNC)
95 ** The minimal asynchronous pre-scaler period (ns)
99 #ifndef SCSI_NCR_MIN_ASYNC
100 #define SCSI_NCR_MIN_ASYNC (40)
101 #endif /* SCSI_NCR_MIN_ASYNC */
104 ** The maximal bus with (in log2 byte)
105 ** (0=8 bit, 1=16 bit)
108 #ifndef SCSI_NCR_MAX_WIDE
109 #define SCSI_NCR_MAX_WIDE (1)
110 #endif /* SCSI_NCR_MAX_WIDE */
112 /*==========================================================
114 ** Configuration and Debugging
116 **==========================================================
120 ** Number of targets supported by the driver.
121 ** n permits target numbers 0..n-1.
122 ** Default is 7, meaning targets #0..#6.
126 #define MAX_TARGET (16)
129 ** Number of logic units supported by the driver.
130 ** n enables logic unit numbers 0..n-1.
131 ** The common SCSI devices require only
132 ** one lun, so take 1 as the default.
140 ** The maximum number of jobs scheduled for starting.
141 ** There should be one slot per target, and one slot
142 ** for each tag of each target in use.
145 #define MAX_START (256)
148 ** The maximum number of segments a transfer is split into.
151 #define MAX_SCATTER (33)
154 ** The maximum transfer length (should be >= 64k).
155 ** MUST NOT be greater than (MAX_SCATTER-1) * PAGE_SIZE.
158 #define MAX_SIZE ((MAX_SCATTER-1) * (long) PAGE_SIZE)
164 #define NCR_SNOOP_TIMEOUT (1000000)
166 /*==========================================================
170 **==========================================================
173 #include <sys/param.h>
174 #include <sys/time.h>
177 #include <sys/systm.h>
178 #include <sys/malloc.h>
180 #include <sys/kernel.h>
181 #include <sys/sysctl.h>
183 #include <sys/thread2.h>
184 #include <machine/clock.h>
185 #include <machine/md_var.h>
186 #include <sys/rman.h>
189 #include <vm/vm_extern.h>
192 #include <bus/pci/pcivar.h>
193 #include <bus/pci/pcireg.h>
196 #include <bus/cam/cam.h>
197 #include <bus/cam/cam_ccb.h>
198 #include <bus/cam/cam_sim.h>
199 #include <bus/cam/cam_xpt.h>
200 #include <bus/cam/cam_xpt_sim.h>
201 #include <bus/cam/cam_xpt_periph.h>
202 #include <bus/cam/cam_debug.h>
205 #include <bus/cam/scsi/scsi_all.h>
206 #include <bus/cam/scsi/scsi_message.h>
208 /*==========================================================
212 **==========================================================
215 #define DEBUG_ALLOC (0x0001)
216 #define DEBUG_PHASE (0x0002)
217 #define DEBUG_POLL (0x0004)
218 #define DEBUG_QUEUE (0x0008)
219 #define DEBUG_RESULT (0x0010)
220 #define DEBUG_SCATTER (0x0020)
221 #define DEBUG_SCRIPT (0x0040)
222 #define DEBUG_TINY (0x0080)
223 #define DEBUG_TIMING (0x0100)
224 #define DEBUG_NEGO (0x0200)
225 #define DEBUG_TAGS (0x0400)
226 #define DEBUG_FREEZE (0x0800)
227 #define DEBUG_RESTART (0x1000)
230 ** Enable/Disable debug messages.
231 ** Can be changed at runtime too.
233 #ifdef SCSI_NCR_DEBUG
234 #define DEBUG_FLAGS ncr_debug
235 #else /* SCSI_NCR_DEBUG */
236 #define SCSI_NCR_DEBUG 0
237 #define DEBUG_FLAGS 0
238 #endif /* SCSI_NCR_DEBUG */
242 /*==========================================================
246 **==========================================================
248 ** modified copy from 386bsd:/usr/include/sys/assert.h
250 **----------------------------------------------------------
254 #define assert(expression) { \
255 if (!(expression)) { \
256 (void)kprintf("assertion \"%s\" failed: " \
257 "file \"%s\", line %d\n", \
258 #expression, __FILE__, __LINE__); \
263 #define assert(expression) { \
264 if (!(expression)) { \
265 (void)kprintf("assertion \"%s\" failed: " \
266 "file \"%s\", line %d\n", \
267 #expression, __FILE__, __LINE__); \
272 /*==========================================================
274 ** Access to the controller chip.
276 **==========================================================
279 #define INB(r) bus_space_read_1(np->bst, np->bsh, offsetof(struct ncr_reg, r))
280 #define INW(r) bus_space_read_2(np->bst, np->bsh, offsetof(struct ncr_reg, r))
281 #define INL(r) bus_space_read_4(np->bst, np->bsh, offsetof(struct ncr_reg, r))
283 #define OUTB(r, val) bus_space_write_1(np->bst, np->bsh, \
284 offsetof(struct ncr_reg, r), val)
285 #define OUTW(r, val) bus_space_write_2(np->bst, np->bsh, \
286 offsetof(struct ncr_reg, r), val)
287 #define OUTL(r, val) bus_space_write_4(np->bst, np->bsh, \
288 offsetof(struct ncr_reg, r), val)
289 #define OUTL_OFF(o, val) bus_space_write_4(np->bst, np->bsh, o, val)
291 #define INB_OFF(o) bus_space_read_1(np->bst, np->bsh, o)
292 #define INW_OFF(o) bus_space_read_2(np->bst, np->bsh, o)
293 #define INL_OFF(o) bus_space_read_4(np->bst, np->bsh, o)
295 #define READSCRIPT_OFF(base, off) \
296 (base ? *((volatile u_int32_t *)((volatile char *)base + (off))) : \
297 bus_space_read_4(np->bst2, np->bsh2, off))
299 #define WRITESCRIPT_OFF(base, off, val) \
302 *((volatile u_int32_t *) \
303 ((volatile char *)base + (off))) = (val); \
305 bus_space_write_4(np->bst2, np->bsh2, off, val); \
308 #define READSCRIPT(r) \
309 READSCRIPT_OFF(np->script, offsetof(struct script, r))
311 #define WRITESCRIPT(r, val) \
312 WRITESCRIPT_OFF(np->script, offsetof(struct script, r), val)
315 ** Set bit field ON, OFF
318 #define OUTONB(r, m) OUTB(r, INB(r) | (m))
319 #define OUTOFFB(r, m) OUTB(r, INB(r) & ~(m))
320 #define OUTONW(r, m) OUTW(r, INW(r) | (m))
321 #define OUTOFFW(r, m) OUTW(r, INW(r) & ~(m))
322 #define OUTONL(r, m) OUTL(r, INL(r) | (m))
323 #define OUTOFFL(r, m) OUTL(r, INL(r) & ~(m))
325 /*==========================================================
327 ** Command control block states.
329 **==========================================================
334 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
335 #define HS_DISCONNECT (3) /* Disconnected by target */
337 #define HS_COMPLETE (4)
338 #define HS_SEL_TIMEOUT (5) /* Selection timeout */
339 #define HS_RESET (6) /* SCSI reset */
340 #define HS_ABORTED (7) /* Transfer aborted */
341 #define HS_TIMEOUT (8) /* Software timeout */
342 #define HS_FAIL (9) /* SCSI or PCI bus errors */
343 #define HS_UNEXPECTED (10) /* Unexpected disconnect */
344 #define HS_STALL (11) /* QUEUE FULL or BUSY */
346 #define HS_DONEMASK (0xfc)
348 /*==========================================================
350 ** Software Interrupt Codes
352 **==========================================================
355 #define SIR_SENSE_RESTART (1)
356 #define SIR_SENSE_FAILED (2)
357 #define SIR_STALL_RESTART (3)
358 #define SIR_STALL_QUEUE (4)
359 #define SIR_NEGO_SYNC (5)
360 #define SIR_NEGO_WIDE (6)
361 #define SIR_NEGO_FAILED (7)
362 #define SIR_NEGO_PROTO (8)
363 #define SIR_REJECT_RECEIVED (9)
364 #define SIR_REJECT_SENT (10)
365 #define SIR_IGN_RESIDUE (11)
366 #define SIR_MISSING_SAVE (12)
369 /*==========================================================
371 ** Extended error codes.
372 ** xerr_status field of struct nccb.
374 **==========================================================
378 #define XE_EXTRA_DATA (1) /* unexpected data phase */
379 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
381 /*==========================================================
383 ** Negotiation status.
384 ** nego_status field of struct nccb.
386 **==========================================================
392 /*==========================================================
394 ** XXX These are no longer used. Remove once the
395 ** script is updated.
396 ** "Special features" of targets.
397 ** quirks field of struct tcb.
398 ** actualquirks field of struct nccb.
400 **==========================================================
403 #define QUIRK_AUTOSAVE (0x01)
404 #define QUIRK_NOMSG (0x02)
405 #define QUIRK_NOSYNC (0x10)
406 #define QUIRK_NOWIDE16 (0x20)
407 #define QUIRK_NOTAGS (0x40)
408 #define QUIRK_UPDATE (0x80)
410 /*==========================================================
414 **==========================================================
417 #define CCB_MAGIC (0xf2691ad2)
418 #define MAX_TAGS (32) /* hard limit */
420 /*==========================================================
424 **==========================================================
427 #define PRINT_ADDR(ccb) xpt_print_path((ccb)->ccb_h.path)
429 /*==========================================================
431 ** Declaration of structs.
433 **==========================================================
442 typedef struct ncb
* ncb_p
;
443 typedef struct tcb
* tcb_p
;
444 typedef struct lcb
* lcb_p
;
445 typedef struct nccb
* nccb_p
;
459 #define UC_SETSYNC 10
460 #define UC_SETTAGS 11
461 #define UC_SETDEBUG 12
462 #define UC_SETORDER 13
463 #define UC_SETWIDE 14
464 #define UC_SETFLAG 15
466 #define UF_TRACE (0x01)
468 /*---------------------------------------
470 ** Timestamps for profiling
472 **---------------------------------------
475 /* Type of the kernel variable `ticks'. XXX should be declared with the var. */
489 ** profiling data (per device)
505 /*==========================================================
507 ** Declaration of structs: target control block
509 **==========================================================
512 #define NCR_TRANS_CUR 0x01 /* Modify current neogtiation status */
513 #define NCR_TRANS_ACTIVE 0x03 /* Assume this is the active target */
514 #define NCR_TRANS_GOAL 0x04 /* Modify negotiation goal */
515 #define NCR_TRANS_USER 0x08 /* Modify user negotiation settings */
517 struct ncr_transinfo
{
523 struct ncr_target_tinfo
{
524 /* Hardware version of our sync settings */
526 #define NCR_CUR_DISCENB 0x01
527 #define NCR_CUR_TAGENB 0x02
528 #define NCR_USR_DISCENB 0x04
529 #define NCR_USR_TAGENB 0x08
531 struct ncr_transinfo current
;
532 struct ncr_transinfo goal
;
533 struct ncr_transinfo user
;
534 /* Hardware version of our wide settings */
540 ** during reselection the ncr jumps to this point
541 ** with SFBR set to the encoded target number
543 ** if it's not this target, jump to the next.
545 ** JUMP IF (SFBR != #target#)
549 struct link jump_tcb
;
552 ** load the actual values for the sxfer and the scntl3
553 ** register (sync/wide mode).
556 ** @(sval field of this tcb)
559 ** @(wval field of this tcb)
560 ** @(scntl3 register)
566 ** if next message is "identify"
567 ** then load the message to SFBR,
568 ** else load 0 to SFBR.
574 struct link call_lun
;
577 ** now look for the right lun.
580 ** @(first nccb of this lun)
583 struct link jump_lcb
;
586 ** pointer to interrupted getcc nccb
592 ** pointer to nccb used for negotiating.
593 ** Avoid to start a nego for all queued commands
594 ** when tagged command queuing is enabled.
607 ** user settable limits for sync transfer
608 ** and tagged commands.
611 struct ncr_target_tinfo tinfo
;
614 ** the lcb's of this tcb
620 /*==========================================================
622 ** Declaration of structs: lun control block
624 **==========================================================
629 ** during reselection the ncr jumps to this point
630 ** with SFBR set to the "Identify" message.
631 ** if it's not this lun, jump to the next.
633 ** JUMP IF (SFBR != #lun#)
634 ** @(next lcb of this target)
637 struct link jump_lcb
;
640 ** if next message is "simple tag",
641 ** then load the tag to SFBR,
642 ** else load 0 to SFBR.
648 struct link call_tag
;
651 ** now look for the right nccb.
654 ** @(first nccb of this lun)
657 struct link jump_nccb
;
660 ** start of the nccb chain
666 ** Control of tagged queueing
676 /*==========================================================
678 ** Declaration of structs: COMMAND control block
680 **==========================================================
682 ** This substructure is copied from the nccb to a
683 ** global address after selection (or reselection)
684 ** and copied back before disconnect.
686 ** These fields are accessible to the script processor.
688 **----------------------------------------------------------
693 ** Execution of a nccb starts at this point.
694 ** It's a jump to the "SELECT" label
697 ** After successful selection the script
698 ** processor overwrites it with a jump to
699 ** the IDLE label of the script.
705 ** Saved data pointer.
706 ** Points to the position in the script
707 ** responsible for the actual transfer
709 ** It's written after reception of a
710 ** "SAVE_DATA_POINTER" message.
711 ** The goalpointer points after
712 ** the last transfer command.
720 ** The virtual address of the nccb
721 ** containing this header.
727 ** space for some timestamps to gather
728 ** profiling data about devices and this driver.
741 ** The status bytes are used by the host and the script processor.
743 ** The first four byte are copied to the scratchb register
744 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
745 ** and copied back just after disconnecting.
746 ** Inside the script the XX_REG are used.
748 ** The last four bytes are used inside the script by "COPY" commands.
749 ** Because source and destination must have the same alignment
750 ** in a longword, the fields HAVE to be at the choosen offsets.
751 ** xerr_st (4) 0 (0x34) scratcha
752 ** sync_st (5) 1 (0x05) sxfer
753 ** wide_st (7) 3 (0x03) scntl3
757 ** First four bytes (script)
761 #define HS_PRT nc_scr1
766 ** First four bytes (host)
768 #define actualquirks phys.header.status[0]
769 #define host_status phys.header.status[1]
770 #define s_status phys.header.status[2]
771 #define parity_status phys.header.status[3]
774 ** Last four bytes (script)
776 #define xerr_st header.status[4] /* MUST be ==0 mod 4 */
777 #define sync_st header.status[5] /* MUST be ==1 mod 4 */
778 #define nego_st header.status[6]
779 #define wide_st header.status[7] /* MUST be ==3 mod 4 */
782 ** Last four bytes (host)
784 #define xerr_status phys.xerr_st
785 #define sync_status phys.sync_st
786 #define nego_status phys.nego_st
787 #define wide_status phys.wide_st
789 /*==========================================================
791 ** Declaration of structs: Data structure block
793 **==========================================================
795 ** During execution of a nccb by the script processor,
796 ** the DSA (data structure address) register points
797 ** to this substructure of the nccb.
798 ** This substructure contains the header with
799 ** the script-processor-changable data and
800 ** data blocks for the indirect move commands.
802 **----------------------------------------------------------
809 ** Has to be the first entry,
810 ** because it's jumped to by the
817 ** Table data for Script
820 struct scr_tblsel select
;
821 struct scr_tblmove smsg
;
822 struct scr_tblmove smsg2
;
823 struct scr_tblmove cmd
;
824 struct scr_tblmove scmd
;
825 struct scr_tblmove sense
;
826 struct scr_tblmove data
[MAX_SCATTER
];
829 /*==========================================================
831 ** Declaration of structs: Command control block.
833 **==========================================================
835 ** During execution of a nccb by the script processor,
836 ** the DSA (data structure address) register points
837 ** to this substructure of the nccb.
838 ** This substructure contains the header with
839 ** the script-processor-changable data and then
840 ** data blocks for the indirect move commands.
842 **----------------------------------------------------------
848 ** This filler ensures that the global header is
849 ** cache line size aligned.
854 ** during reselection the ncr jumps to this point.
855 ** If a "SIMPLE_TAG" message was received,
856 ** then SFBR is set to the tag.
857 ** else SFBR is set to 0
858 ** If looking for another tag, jump to the next nccb.
860 ** JUMP IF (SFBR != #TAG#)
861 ** @(next nccb of this lun)
864 struct link jump_nccb
;
867 ** After execution of this call, the return address
868 ** (in the TEMP register) points to the following
869 ** data structure block.
870 ** So copy it to the DSA register, and start
871 ** processing of this data structure.
877 struct link call_tmp
;
880 ** This is the data structure which is
881 ** to be executed by the script processor.
887 ** If a data transfer phase is terminated too early
888 ** (after reception of a message (i.e. DISCONNECT)),
889 ** we have to prepare a mini script to transfer
890 ** the rest of the data.
896 ** The general SCSI driver provides a
897 ** pointer to a control block.
903 ** We prepare a message to be sent after selection,
904 ** and a second one to be sent after getcc selection.
905 ** Contents are IDENTIFY and SIMPLE_TAG.
906 ** While negotiating sync or wide transfer,
907 ** a SDTM or WDTM message is appended.
910 u_char scsi_smsg
[8];
911 u_char scsi_smsg2
[8];
915 ** Flag is used while looking for a free nccb.
921 ** Physical address of this instance of nccb
927 ** Completion time out for this job.
928 ** It's set to time of start + allowed number of seconds.
934 ** All nccbs of one hostadapter are chained.
940 ** All nccbs of one target/lun are chained.
952 ** Tag for this transfer.
953 ** It's patched into jump_nccb.
954 ** If it's not zero, a SIMPLE_TAG
955 ** message is included in smsg.
961 #define CCB_PHYS(cp,lbl) (cp->p_nccb + offsetof(struct nccb, lbl))
963 /*==========================================================
965 ** Declaration of structs: NCR device descriptor
967 **==========================================================
972 ** The global header.
973 ** Accessible to both the host and the
975 ** We assume it is cache line size aligned.
981 /*-----------------------------------------------
983 **-----------------------------------------------
985 ** During reselection the ncr jumps to this point.
986 ** The SFBR register is loaded with the encoded target id.
988 ** Jump to the first target.
993 struct link jump_tcb
;
995 /*-----------------------------------------------
997 **-----------------------------------------------
999 ** virtual and physical addresses
1000 ** of the 53c810 chip.
1003 struct resource
*reg_res
;
1004 bus_space_tag_t bst
;
1005 bus_space_handle_t bsh
;
1008 struct resource
*sram_res
;
1009 bus_space_tag_t bst2
;
1010 bus_space_handle_t bsh2
;
1012 struct resource
*irq_res
;
1016 ** Scripts instance virtual address.
1018 struct script
*script
;
1019 struct scripth
*scripth
;
1022 ** Scripts instance physical address.
1028 ** The SCSI address of the host adapter.
1033 ** timing parameters
1035 u_char minsync
; /* Minimum sync period factor */
1036 u_char maxsync
; /* Maximum sync period factor */
1037 u_char maxoffs
; /* Max scsi offset */
1038 u_char clock_divn
; /* Number of clock divisors */
1039 u_long clock_khz
; /* SCSI clock frequency in KHz */
1040 u_long features
; /* Chip features map */
1041 u_char multiplier
; /* Clock multiplier (1,2,4) */
1043 u_char maxburst
; /* log base 2 of dwords burst */
1046 ** BIOS supplied PCI bus options
1057 /*-----------------------------------------------
1058 ** CAM SIM information for this instance
1059 **-----------------------------------------------
1062 struct cam_sim
*sim
;
1063 struct cam_path
*path
;
1065 /*-----------------------------------------------
1067 **-----------------------------------------------
1069 ** Commands from user
1076 struct tcb target
[MAX_TARGET
];
1081 u_int32_t squeue
[MAX_START
];
1091 struct callout timeout_ch
;
1093 /*-----------------------------------------------
1094 ** Debug and profiling
1095 **-----------------------------------------------
1099 struct ncr_reg regdump
;
1105 struct profile profile
;
1110 ** Head of list of all nccbs for this controller.
1116 ** Should be longword aligned,
1117 ** because they're written with a
1118 ** COPY script command.
1125 ** Buffer for STATUS_IN phase.
1130 ** controller chip dependent maximal transfer width.
1136 ** address of the ncr control registers in io space
1142 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1143 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1145 /*==========================================================
1148 ** Script for NCR-Processor.
1150 ** Use ncr_script_fill() to create the variable parts.
1151 ** Use ncr_script_copy_and_bind() to make a copy and
1152 ** bind to physical addresses.
1155 **==========================================================
1157 ** We have to know the offsets of all labels before
1158 ** we reach them (for forward jumps).
1159 ** Therefore we declare a struct here.
1160 ** If you make changes inside the script,
1161 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1163 **----------------------------------------------------------
1167 ** Script fragments which are loaded into the on-board RAM
1168 ** of 825A, 875 and 895 chips.
1174 ncrcmd startpos
[ 1];
1179 ncrcmd select
[ 18];
1180 ncrcmd prepare
[ 4];
1181 ncrcmd loadpos
[ 14];
1182 ncrcmd prepare2
[ 24];
1185 ncrcmd dispatch
[ 33];
1186 ncrcmd no_data
[ 17];
1187 ncrcmd checkatn
[ 10];
1188 ncrcmd command
[ 15];
1189 ncrcmd status
[ 27];
1190 ncrcmd msg_in
[ 26];
1191 ncrcmd msg_bad
[ 6];
1192 ncrcmd complete
[ 13];
1193 ncrcmd cleanup
[ 12];
1194 ncrcmd cleanup0
[ 9];
1195 ncrcmd signal
[ 12];
1196 ncrcmd save_dp
[ 5];
1197 ncrcmd restore_dp
[ 5];
1198 ncrcmd disconnect
[ 12];
1199 ncrcmd disconnect0
[ 5];
1200 ncrcmd disconnect1
[ 23];
1201 ncrcmd msg_out
[ 9];
1202 ncrcmd msg_out_done
[ 7];
1203 ncrcmd badgetcc
[ 6];
1204 ncrcmd reselect
[ 8];
1205 ncrcmd reselect1
[ 8];
1206 ncrcmd reselect2
[ 8];
1207 ncrcmd resel_tmp
[ 5];
1208 ncrcmd resel_lun
[ 18];
1209 ncrcmd resel_tag
[ 24];
1210 ncrcmd data_in
[MAX_SCATTER
* 4 + 7];
1211 ncrcmd data_out
[MAX_SCATTER
* 4 + 7];
1215 ** Script fragments which stay in main memory for all chips.
1218 ncrcmd tryloop
[MAX_START
*5+2];
1219 ncrcmd msg_parity
[ 6];
1220 ncrcmd msg_reject
[ 8];
1221 ncrcmd msg_ign_residue
[ 32];
1222 ncrcmd msg_extended
[ 18];
1223 ncrcmd msg_ext_2
[ 18];
1224 ncrcmd msg_wdtr
[ 27];
1225 ncrcmd msg_ext_3
[ 18];
1226 ncrcmd msg_sdtr
[ 27];
1227 ncrcmd msg_out_abort
[ 10];
1230 #ifdef NCR_GETCC_WITHMSG
1231 ncrcmd getcc2
[ 29];
1233 ncrcmd getcc2
[ 14];
1236 ncrcmd aborttag
[ 4];
1238 ncrcmd snooptest
[ 9];
1239 ncrcmd snoopend
[ 2];
1242 /*==========================================================
1245 ** Function headers.
1248 **==========================================================
1252 static nccb_p
ncr_alloc_nccb (ncb_p np
, u_long target
, u_long lun
);
1253 static void ncr_complete (ncb_p np
, nccb_p cp
);
1254 static int ncr_delta (int * from
, int * to
);
1255 static void ncr_exception (ncb_p np
);
1256 static void ncr_free_nccb (ncb_p np
, nccb_p cp
);
1257 static void ncr_freeze_devq (ncb_p np
, struct cam_path
*path
);
1258 static void ncr_selectclock (ncb_p np
, u_char scntl3
);
1259 static void ncr_getclock (ncb_p np
, u_char multiplier
);
1260 static nccb_p
ncr_get_nccb (ncb_p np
, u_long t
,u_long l
);
1262 static u_int32_t
ncr_info (int unit
);
1264 static void ncr_init (ncb_p np
, char * msg
, u_long code
);
1265 static void ncr_intr (void *vnp
);
1266 static void ncr_int_ma (ncb_p np
, u_char dstat
);
1267 static void ncr_int_sir (ncb_p np
);
1268 static void ncr_int_sto (ncb_p np
);
1270 static void ncr_min_phys (struct buf
*bp
);
1272 static void ncr_poll (struct cam_sim
*sim
);
1273 static void ncb_profile (ncb_p np
, nccb_p cp
);
1274 static void ncr_script_copy_and_bind
1275 (ncb_p np
, ncrcmd
*src
, ncrcmd
*dst
, int len
);
1276 static void ncr_script_fill (struct script
* scr
, struct scripth
*scrh
);
1277 static int ncr_scatter (struct dsb
* phys
, vm_offset_t vaddr
,
1279 static void ncr_getsync (ncb_p np
, u_char sfac
, u_char
*fakp
,
1281 static void ncr_setsync (ncb_p np
, nccb_p cp
,u_char scntl3
,u_char sxfer
,
1283 static void ncr_setwide (ncb_p np
, nccb_p cp
, u_char wide
, u_char ack
);
1284 static int ncr_show_msg (u_char
* msg
);
1285 static int ncr_snooptest (ncb_p np
);
1286 static void ncr_action (struct cam_sim
*sim
, union ccb
*ccb
);
1287 static void ncr_timeout (void *arg
);
1288 static void ncr_wakeup (ncb_p np
, u_long code
);
1290 static int ncr_probe (device_t dev
);
1291 static int ncr_attach (device_t dev
);
1293 #endif /* _KERNEL */
1295 /*==========================================================
1298 ** Global static data.
1301 **==========================================================
1306 static int ncr_debug
= SCSI_NCR_DEBUG
;
1307 SYSCTL_INT(_debug
, OID_AUTO
, ncr_debug
, CTLFLAG_RW
, &ncr_debug
, 0,
1308 "Driver debug flags");
1310 static int ncr_cache
; /* to be aligned _NOT_ static */
1312 /*==========================================================
1315 ** Global static data: auto configure
1318 **==========================================================
1321 #define NCR_810_ID (0x00011000ul)
1322 #define NCR_815_ID (0x00041000ul)
1323 #define NCR_820_ID (0x00021000ul)
1324 #define NCR_825_ID (0x00031000ul)
1325 #define NCR_860_ID (0x00061000ul)
1326 #define NCR_875_ID (0x000f1000ul)
1327 #define NCR_875_ID2 (0x008f1000ul)
1328 #define NCR_885_ID (0x000d1000ul)
1329 #define NCR_895_ID (0x000c1000ul)
1330 #define NCR_896_ID (0x000b1000ul)
1331 #define NCR_895A_ID (0x00121000ul)
1332 #define NCR_1510D_ID (0x000a1000ul)
1335 static char *ncr_name (ncb_p np
)
1337 static char name
[10];
1338 ksnprintf(name
, sizeof(name
), "ncr%d", np
->unit
);
1342 /*==========================================================
1345 ** Scripts for NCR-Processor.
1347 ** Use ncr_script_bind for binding to physical addresses.
1350 **==========================================================
1352 ** NADDR generates a reference to a field of the controller data.
1353 ** PADDR generates a reference to another part of the script.
1354 ** RADDR generates a reference to a script processor register.
1355 ** FADDR generates a reference to a script processor register
1358 **----------------------------------------------------------
1361 #define RELOC_SOFTC 0x40000000
1362 #define RELOC_LABEL 0x50000000
1363 #define RELOC_REGISTER 0x60000000
1364 #define RELOC_KVAR 0x70000000
1365 #define RELOC_LABELH 0x80000000
1366 #define RELOC_MASK 0xf0000000
1368 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
1369 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
1370 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
1371 #define RADDR(label) (RELOC_REGISTER | REG(label))
1372 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
1373 #define KVAR(which) (RELOC_KVAR | (which))
1375 #define KVAR_SECOND (0)
1376 #define KVAR_TICKS (1)
1377 #define KVAR_NCR_CACHE (2)
1379 #define SCRIPT_KVAR_FIRST (0)
1380 #define SCRIPT_KVAR_LAST (3)
1383 * Kernel variables referenced in the scripts.
1384 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
1386 static void *script_kvars
[] =
1387 { &time_uptime
, &ticks
, &ncr_cache
};
1389 static struct script script0
= {
1390 /*--------------------------< START >-----------------------*/ {
1392 ** Claim to be still alive ...
1394 SCR_COPY (sizeof (((struct ncb
*)0)->heartbeat
)),
1398 ** Make data structure address invalid.
1401 SCR_LOAD_REG (dsa
, 0xff),
1403 SCR_FROM_REG (ctest2
),
1405 }/*-------------------------< START0 >----------------------*/,{
1407 ** Hook for interrupted GetConditionCode.
1408 ** Will be patched to ... IFTRUE by
1409 ** the interrupt handler.
1411 SCR_INT
^ IFFALSE (0),
1414 }/*-------------------------< START1 >----------------------*/,{
1416 ** Hook for stalled start queue.
1417 ** Will be patched to IFTRUE by the interrupt handler.
1419 SCR_INT
^ IFFALSE (0),
1422 ** Then jump to a certain point in tryloop.
1423 ** Due to the lack of indirect addressing the code
1424 ** is self modifying here.
1427 }/*-------------------------< STARTPOS >--------------------*/,{
1430 }/*-------------------------< TRYSEL >----------------------*/,{
1433 ** DSA: Address of a Data Structure
1434 ** or Address of the IDLE-Label.
1436 ** TEMP: Address of a script, which tries to
1437 ** start the NEXT entry.
1439 ** Save the TEMP register into the SCRATCHA register.
1440 ** Then copy the DSA to TEMP and RETURN.
1441 ** This is kind of an indirect jump.
1442 ** (The script processor has NO stack, so the
1443 ** CALL is actually a jump and link, and the
1444 ** RETURN is an indirect jump.)
1446 ** If the slot was empty, DSA contains the address
1447 ** of the IDLE part of this script. The processor
1448 ** jumps to IDLE and waits for a reselect.
1449 ** It will wake up and try the same slot again
1450 ** after the SIGP bit becomes set by the host.
1452 ** If the slot was not empty, DSA contains
1453 ** the address of the phys-part of a nccb.
1454 ** The processor jumps to this address.
1455 ** phys starts with head,
1456 ** head starts with launch,
1457 ** so actually the processor jumps to
1459 ** If the entry is scheduled for execution,
1460 ** then launch contains a jump to SELECT.
1461 ** If it's not scheduled, it contains a jump to IDLE.
1472 }/*-------------------------< SKIP >------------------------*/,{
1474 ** This entry has been canceled.
1475 ** Next time use the next slot.
1481 ** patch the launch field.
1482 ** should look like an idle process.
1489 }/*-------------------------< SKIP2 >-----------------------*/,{
1493 }/*-------------------------< IDLE >------------------------*/,{
1496 ** Wait for reselect.
1501 }/*-------------------------< SELECT >----------------------*/,{
1503 ** DSA contains the address of a scheduled
1506 ** SCRATCHA contains the address of the script,
1507 ** which starts the next entry.
1509 ** Set Initiator mode.
1511 ** (Target mode is left as an exercise for the reader)
1516 SCR_LOAD_REG (HS_REG
, 0xff),
1520 ** And try to select this target.
1522 SCR_SEL_TBL_ATN
^ offsetof (struct dsb
, select
),
1526 ** Now there are 4 possibilities:
1528 ** (1) The ncr looses arbitration.
1529 ** This is ok, because it will try again,
1530 ** when the bus becomes idle.
1531 ** (But beware of the timeout function!)
1533 ** (2) The ncr is reselected.
1534 ** Then the script processor takes the jump
1535 ** to the RESELECT label.
1537 ** (3) The ncr completes the selection.
1538 ** Then it will execute the next statement.
1540 ** (4) There is a selection timeout.
1541 ** Then the ncr should interrupt the host and stop.
1542 ** Unfortunately, it seems to continue execution
1543 ** of the script. But it will fail with an
1544 ** IID-interrupt on the next WHEN.
1547 SCR_JUMPR
^ IFTRUE (WHEN (SCR_MSG_IN
)),
1551 ** Send the IDENTIFY and SIMPLE_TAG messages
1552 ** (and the MSG_EXT_SDTR message)
1554 SCR_MOVE_TBL
^ SCR_MSG_OUT
,
1555 offsetof (struct dsb
, smsg
),
1556 #ifdef undef /* XXX better fail than try to deal with this ... */
1557 SCR_JUMPR
^ IFTRUE (WHEN (SCR_MSG_OUT
)),
1566 ** Selection complete.
1567 ** Next time use the next slot.
1572 }/*-------------------------< PREPARE >----------------------*/,{
1574 ** The ncr doesn't have an indirect load
1575 ** or store command. So we have to
1576 ** copy part of the control block to a
1577 ** fixed place, where we can access it.
1579 ** We patch the address part of a
1580 ** COPY command with the DSA-register.
1586 ** then we do the actual copy.
1588 SCR_COPY (sizeof (struct head
)),
1590 ** continued after the next label ...
1593 }/*-------------------------< LOADPOS >---------------------*/,{
1597 ** Mark this nccb as not scheduled.
1601 NADDR (header
.launch
),
1603 ** Set a time stamp for this selection
1605 SCR_COPY (sizeof (ticks
)),
1607 NADDR (header
.stamp
.select
),
1609 ** load the savep (saved pointer) into
1610 ** the TEMP register (actual pointer)
1613 NADDR (header
.savep
),
1616 ** Initialize the status registers
1619 NADDR (header
.status
),
1622 }/*-------------------------< PREPARE2 >---------------------*/,{
1624 ** Load the synchronous mode register
1630 ** Load the wide mode and timing register
1636 ** Initialize the msgout buffer with a NOOP message.
1638 SCR_LOAD_REG (scratcha
, MSG_NOOP
),
1647 ** Message in phase ?
1649 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
1652 ** Extended or reject message ?
1654 SCR_FROM_REG (sbdl
),
1656 SCR_JUMP
^ IFTRUE (DATA (MSG_EXTENDED
)),
1658 SCR_JUMP
^ IFTRUE (DATA (MSG_MESSAGE_REJECT
)),
1659 PADDRH (msg_reject
),
1661 ** normal processing
1665 }/*-------------------------< SETMSG >----------------------*/,{
1671 }/*-------------------------< CLRACK >----------------------*/,{
1673 ** Terminate possible pending message phase.
1678 }/*-----------------------< DISPATCH >----------------------*/,{
1679 SCR_FROM_REG (HS_REG
),
1681 SCR_INT
^ IFTRUE (DATA (HS_NEGOTIATE
)),
1684 ** remove bogus output signals
1686 SCR_REG_REG (socl
, SCR_AND
, CACK
|CATN
),
1688 SCR_RETURN
^ IFTRUE (WHEN (SCR_DATA_OUT
)),
1690 SCR_RETURN
^ IFTRUE (IF (SCR_DATA_IN
)),
1692 SCR_JUMP
^ IFTRUE (IF (SCR_MSG_OUT
)),
1694 SCR_JUMP
^ IFTRUE (IF (SCR_MSG_IN
)),
1696 SCR_JUMP
^ IFTRUE (IF (SCR_COMMAND
)),
1698 SCR_JUMP
^ IFTRUE (IF (SCR_STATUS
)),
1701 ** Discard one illegal phase byte, if required.
1703 SCR_LOAD_REG (scratcha
, XE_BAD_PHASE
),
1708 SCR_JUMPR
^ IFFALSE (IF (SCR_ILG_OUT
)),
1710 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT
,
1712 SCR_JUMPR
^ IFFALSE (IF (SCR_ILG_IN
)),
1714 SCR_MOVE_ABS (1) ^ SCR_ILG_IN
,
1719 }/*-------------------------< NO_DATA >--------------------*/,{
1721 ** The target wants to tranfer too much data
1722 ** or in the wrong direction.
1723 ** Remember that in extended error.
1725 SCR_LOAD_REG (scratcha
, XE_EXTRA_DATA
),
1731 ** Discard one data byte, if required.
1733 SCR_JUMPR
^ IFFALSE (WHEN (SCR_DATA_OUT
)),
1735 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT
,
1737 SCR_JUMPR
^ IFFALSE (IF (SCR_DATA_IN
)),
1739 SCR_MOVE_ABS (1) ^ SCR_DATA_IN
,
1742 ** .. and repeat as required.
1748 }/*-------------------------< CHECKATN >--------------------*/,{
1750 ** If AAP (bit 1 of scntl0 register) is set
1751 ** and a parity error is detected,
1752 ** the script processor asserts ATN.
1754 ** The target should switch to a MSG_OUT phase
1755 ** to get the message.
1757 SCR_FROM_REG (socl
),
1759 SCR_JUMP
^ IFFALSE (MASK (CATN
, CATN
)),
1764 SCR_REG_REG (PS_REG
, SCR_ADD
, 1),
1767 ** Prepare a MSG_INITIATOR_DET_ERR message
1768 ** (initiator detected error).
1769 ** The target should retry the transfer.
1771 SCR_LOAD_REG (scratcha
, MSG_INITIATOR_DET_ERR
),
1776 }/*-------------------------< COMMAND >--------------------*/,{
1778 ** If this is not a GETCC transfer ...
1780 SCR_FROM_REG (SS_REG
),
1782 /*<<<*/ SCR_JUMPR
^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND
)),
1785 ** ... set a timestamp ...
1787 SCR_COPY (sizeof (ticks
)),
1789 NADDR (header
.stamp
.command
),
1791 ** ... and send the command
1793 SCR_MOVE_TBL
^ SCR_COMMAND
,
1794 offsetof (struct dsb
, cmd
),
1798 ** Send the GETCC command
1800 /*>>>*/ SCR_MOVE_TBL
^ SCR_COMMAND
,
1801 offsetof (struct dsb
, scmd
),
1805 }/*-------------------------< STATUS >--------------------*/,{
1807 ** set the timestamp.
1809 SCR_COPY (sizeof (ticks
)),
1811 NADDR (header
.stamp
.status
),
1813 ** If this is a GETCC transfer,
1815 SCR_FROM_REG (SS_REG
),
1817 /*<<<*/ SCR_JUMPR
^ IFFALSE (DATA (SCSI_STATUS_CHECK_COND
)),
1822 SCR_MOVE_ABS (1) ^ SCR_STATUS
,
1825 ** Save status to scsi_status.
1826 ** Mark as complete.
1827 ** And wait for disconnect.
1829 SCR_TO_REG (SS_REG
),
1831 SCR_REG_REG (SS_REG
, SCR_OR
, SCSI_STATUS_SENSE
),
1833 SCR_LOAD_REG (HS_REG
, HS_COMPLETE
),
1838 ** If it was no GETCC transfer,
1839 ** save the status to scsi_status.
1841 /*>>>*/ SCR_MOVE_ABS (1) ^ SCR_STATUS
,
1843 SCR_TO_REG (SS_REG
),
1846 ** if it was no check condition ...
1848 SCR_JUMP
^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND
)),
1851 ** ... mark as complete.
1853 SCR_LOAD_REG (HS_REG
, HS_COMPLETE
),
1858 }/*-------------------------< MSG_IN >--------------------*/,{
1860 ** Get the first byte of the message
1861 ** and save it to SCRATCHA.
1863 ** The script processor doesn't negate the
1864 ** ACK signal after this transfer.
1866 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
1869 ** Check for message parity error.
1871 SCR_TO_REG (scratcha
),
1873 SCR_FROM_REG (socl
),
1875 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
1876 PADDRH (msg_parity
),
1877 SCR_FROM_REG (scratcha
),
1880 ** Parity was ok, handle this message.
1882 SCR_JUMP
^ IFTRUE (DATA (MSG_CMDCOMPLETE
)),
1884 SCR_JUMP
^ IFTRUE (DATA (MSG_SAVEDATAPOINTER
)),
1886 SCR_JUMP
^ IFTRUE (DATA (MSG_RESTOREPOINTERS
)),
1888 SCR_JUMP
^ IFTRUE (DATA (MSG_DISCONNECT
)),
1890 SCR_JUMP
^ IFTRUE (DATA (MSG_EXTENDED
)),
1891 PADDRH (msg_extended
),
1892 SCR_JUMP
^ IFTRUE (DATA (MSG_NOOP
)),
1894 SCR_JUMP
^ IFTRUE (DATA (MSG_MESSAGE_REJECT
)),
1895 PADDRH (msg_reject
),
1896 SCR_JUMP
^ IFTRUE (DATA (MSG_IGN_WIDE_RESIDUE
)),
1897 PADDRH (msg_ign_residue
),
1899 ** Rest of the messages left as
1902 ** Unimplemented messages:
1903 ** fall through to MSG_BAD.
1905 }/*-------------------------< MSG_BAD >------------------*/,{
1907 ** unimplemented message - reject it.
1911 SCR_LOAD_REG (scratcha
, MSG_MESSAGE_REJECT
),
1916 }/*-------------------------< COMPLETE >-----------------*/,{
1918 ** Complete message.
1920 ** If it's not the get condition code,
1921 ** copy TEMP register to LASTP in header.
1923 SCR_FROM_REG (SS_REG
),
1925 /*<<<*/ SCR_JUMPR
^ IFTRUE (MASK (SCSI_STATUS_SENSE
, SCSI_STATUS_SENSE
)),
1929 NADDR (header
.lastp
),
1931 ** When we terminate the cycle by clearing ACK,
1932 ** the target may disconnect immediately.
1934 ** We don't want to be told of an
1935 ** "unexpected disconnect",
1936 ** so we disable this feature.
1938 SCR_REG_REG (scntl2
, SCR_AND
, 0x7f),
1941 ** Terminate cycle ...
1943 SCR_CLR (SCR_ACK
|SCR_ATN
),
1946 ** ... and wait for the disconnect.
1950 }/*-------------------------< CLEANUP >-------------------*/,{
1952 ** dsa: Pointer to nccb
1953 ** or xxxxxxFF (no nccb)
1955 ** HS_REG: Host-Status (<>0!)
1959 SCR_JUMP
^ IFTRUE (DATA (0xff)),
1963 ** save the status registers
1967 NADDR (header
.status
),
1969 ** and copy back the header to the nccb.
1974 SCR_COPY (sizeof (struct head
)),
1976 }/*-------------------------< CLEANUP0 >--------------------*/,{
1980 ** If command resulted in "check condition"
1981 ** status and is not yet completed,
1982 ** try to get the condition code.
1984 SCR_FROM_REG (HS_REG
),
1986 /*<<<*/ SCR_JUMPR
^ IFFALSE (MASK (0, HS_DONEMASK
)),
1988 SCR_FROM_REG (SS_REG
),
1990 SCR_JUMP
^ IFTRUE (DATA (SCSI_STATUS_CHECK_COND
)),
1992 }/*-------------------------< SIGNAL >----------------------*/,{
1994 ** if status = queue full,
1995 ** reinsert in startqueue and stall queue.
1997 /*>>>*/ SCR_FROM_REG (SS_REG
),
1999 SCR_INT
^ IFTRUE (DATA (SCSI_STATUS_QUEUE_FULL
)),
2002 ** And make the DSA register invalid.
2004 SCR_LOAD_REG (dsa
, 0xff), /* invalid */
2007 ** if job completed ...
2009 SCR_FROM_REG (HS_REG
),
2012 ** ... signal completion to the host
2014 SCR_INT_FLY
^ IFFALSE (MASK (0, HS_DONEMASK
)),
2017 ** Auf zu neuen Schandtaten!
2022 }/*-------------------------< SAVE_DP >------------------*/,{
2025 ** Copy TEMP register to SAVEP in header.
2029 NADDR (header
.savep
),
2032 }/*-------------------------< RESTORE_DP >---------------*/,{
2034 ** RESTORE_DP message:
2035 ** Copy SAVEP in header to TEMP register.
2038 NADDR (header
.savep
),
2043 }/*-------------------------< DISCONNECT >---------------*/,{
2045 ** If QUIRK_AUTOSAVE is set,
2046 ** do an "save pointer" operation.
2048 SCR_FROM_REG (QU_REG
),
2050 /*<<<*/ SCR_JUMPR
^ IFFALSE (MASK (QUIRK_AUTOSAVE
, QUIRK_AUTOSAVE
)),
2053 ** like SAVE_DP message:
2054 ** Copy TEMP register to SAVEP in header.
2058 NADDR (header
.savep
),
2060 ** Check if temp==savep or temp==goalp:
2061 ** if not, log a missing save pointer message.
2062 ** In fact, it's a comparison mod 256.
2064 ** Hmmm, I hadn't thought that I would be urged to
2065 ** write this kind of ugly self modifying code.
2067 ** It's unbelievable, but the ncr53c8xx isn't able
2068 ** to subtract one register from another.
2070 SCR_FROM_REG (temp
),
2073 ** You are not expected to understand this ..
2075 ** CAUTION: only little endian architectures supported! XXX
2078 NADDR (header
.savep
),
2079 PADDR (disconnect0
),
2080 }/*-------------------------< DISCONNECT0 >--------------*/,{
2081 /*<<<*/ SCR_JUMPR
^ IFTRUE (DATA (1)),
2087 NADDR (header
.goalp
),
2088 PADDR (disconnect1
),
2089 }/*-------------------------< DISCONNECT1 >--------------*/,{
2090 SCR_INT
^ IFFALSE (DATA (1)),
2095 ** DISCONNECTing ...
2097 ** disable the "unexpected disconnect" feature,
2098 ** and remove the ACK signal.
2100 SCR_REG_REG (scntl2
, SCR_AND
, 0x7f),
2102 SCR_CLR (SCR_ACK
|SCR_ATN
),
2105 ** Wait for the disconnect.
2111 ** Set a time stamp,
2112 ** and count the disconnects.
2114 SCR_COPY (sizeof (ticks
)),
2116 NADDR (header
.stamp
.disconnect
),
2120 SCR_REG_REG (temp
, SCR_ADD
, 0x01),
2126 ** Status is: DISCONNECTED.
2128 SCR_LOAD_REG (HS_REG
, HS_DISCONNECT
),
2133 }/*-------------------------< MSG_OUT >-------------------*/,{
2135 ** The target requests a message.
2137 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT
,
2143 ** If it was no ABORT message ...
2145 SCR_JUMP
^ IFTRUE (DATA (MSG_ABORT
)),
2146 PADDRH (msg_out_abort
),
2148 ** ... wait for the next phase
2149 ** if it's a message out, send it again, ...
2151 SCR_JUMP
^ IFTRUE (WHEN (SCR_MSG_OUT
)),
2153 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2155 ** ... else clear the message ...
2157 SCR_LOAD_REG (scratcha
, MSG_NOOP
),
2163 ** ... and process the next phase
2168 }/*------------------------< BADGETCC >---------------------*/,{
2170 ** If SIGP was set, clear it and try again.
2172 SCR_FROM_REG (ctest2
),
2174 SCR_JUMP
^ IFTRUE (MASK (CSIGP
,CSIGP
)),
2178 }/*-------------------------< RESELECT >--------------------*/,{
2180 ** This NOP will be patched with LED OFF
2181 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2187 ** make the DSA invalid.
2189 SCR_LOAD_REG (dsa
, 0xff),
2194 ** Sleep waiting for a reselection.
2195 ** If SIGP is set, special treatment.
2197 ** Zu allem bereit ..
2201 }/*-------------------------< RESELECT1 >--------------------*/,{
2203 ** This NOP will be patched with LED ON
2204 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2209 ** ... zu nichts zu gebrauchen ?
2211 ** load the target id into the SFBR
2212 ** and jump to the control block.
2214 ** Look at the declarations of
2219 ** to understand what's going on.
2221 SCR_REG_SFBR (ssid
, SCR_AND
, 0x8F),
2227 }/*-------------------------< RESELECT2 >-------------------*/,{
2229 ** This NOP will be patched with LED ON
2230 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2235 ** If it's not connected :(
2236 ** -> interrupted by SIGP bit.
2239 SCR_FROM_REG (ctest2
),
2241 SCR_JUMP
^ IFTRUE (MASK (CSIGP
,CSIGP
)),
2246 }/*-------------------------< RESEL_TMP >-------------------*/,{
2248 ** The return address in TEMP
2249 ** is in fact the data structure address,
2250 ** so copy it to the DSA register.
2258 }/*-------------------------< RESEL_LUN >-------------------*/,{
2260 ** come back to this point
2261 ** to get an IDENTIFY message
2262 ** Wait for a msg_in phase.
2264 /*<<<*/ SCR_JUMPR
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2268 ** It's not a sony, it's a trick:
2269 ** read the data without acknowledging it.
2271 SCR_FROM_REG (sbdl
),
2273 /*<<<*/ SCR_JUMPR
^ IFFALSE (MASK (MSG_IDENTIFYFLAG
, 0x98)),
2276 ** It WAS an Identify message.
2277 ** get it and ack it!
2279 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2284 ** Mask out the lun.
2286 SCR_REG_REG (sfbr
, SCR_AND
, 0x07),
2291 ** No message phase or no IDENTIFY message:
2294 /*>>>*/ SCR_LOAD_SFBR (0),
2299 }/*-------------------------< RESEL_TAG >-------------------*/,{
2301 ** come back to this point
2302 ** to get a SIMPLE_TAG message
2303 ** Wait for a MSG_IN phase.
2305 /*<<<*/ SCR_JUMPR
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2309 ** It's a trick - read the data
2310 ** without acknowledging it.
2312 SCR_FROM_REG (sbdl
),
2314 /*<<<*/ SCR_JUMPR
^ IFFALSE (DATA (MSG_SIMPLE_Q_TAG
)),
2317 ** It WAS a SIMPLE_TAG message.
2318 ** get it and ack it!
2320 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2325 ** Wait for the second byte (the tag)
2327 /*<<<*/ SCR_JUMPR
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2330 ** Get it and ack it!
2332 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2334 SCR_CLR (SCR_ACK
|SCR_CARRY
),
2339 ** No message phase or no SIMPLE_TAG message
2340 ** or no second byte: return 0.
2342 /*>>>*/ SCR_LOAD_SFBR (0),
2344 SCR_SET (SCR_CARRY
),
2349 }/*-------------------------< DATA_IN >--------------------*/,{
2351 ** Because the size depends on the
2352 ** #define MAX_SCATTER parameter,
2353 ** it is filled in at runtime.
2355 ** SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2357 ** SCR_COPY (sizeof (ticks)),
2358 ** KVAR (KVAR_TICKS),
2359 ** NADDR (header.stamp.data),
2360 ** SCR_MOVE_TBL ^ SCR_DATA_IN,
2361 ** offsetof (struct dsb, data[ 0]),
2363 ** ##===========< i=1; i<MAX_SCATTER >=========
2364 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2365 ** || PADDR (checkatn),
2366 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2367 ** || offsetof (struct dsb, data[ i]),
2368 ** ##==========================================
2371 ** PADDR (checkatn),
2376 }/*-------------------------< DATA_OUT >-------------------*/,{
2378 ** Because the size depends on the
2379 ** #define MAX_SCATTER parameter,
2380 ** it is filled in at runtime.
2382 ** SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2384 ** SCR_COPY (sizeof (ticks)),
2385 ** KVAR (KVAR_TICKS),
2386 ** NADDR (header.stamp.data),
2387 ** SCR_MOVE_TBL ^ SCR_DATA_OUT,
2388 ** offsetof (struct dsb, data[ 0]),
2390 ** ##===========< i=1; i<MAX_SCATTER >=========
2391 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2392 ** || PADDR (dispatch),
2393 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2394 ** || offsetof (struct dsb, data[ i]),
2395 ** ##==========================================
2398 ** PADDR (dispatch),
2402 **---------------------------------------------------------
2406 }/*--------------------------------------------------------*/
2410 static struct scripth scripth0
= {
2411 /*-------------------------< TRYLOOP >---------------------*/{
2413 ** Load an entry of the start queue into dsa
2414 ** and try to start it by jumping to TRYSEL.
2416 ** Because the size depends on the
2417 ** #define MAX_START parameter, it is filled
2420 **-----------------------------------------------------------
2422 ** ##===========< I=0; i<MAX_START >===========
2424 ** || NADDR (squeue[i]),
2427 ** || PADDR (trysel),
2428 ** ##==========================================
2433 **-----------------------------------------------------------
2436 }/*-------------------------< MSG_PARITY >---------------*/,{
2440 SCR_REG_REG (PS_REG
, SCR_ADD
, 0x01),
2443 ** send a "message parity error" message.
2445 SCR_LOAD_REG (scratcha
, MSG_PARITY_ERROR
),
2449 }/*-------------------------< MSG_MESSAGE_REJECT >---------------*/,{
2451 ** If a negotiation was in progress,
2452 ** negotiation failed.
2454 SCR_FROM_REG (HS_REG
),
2456 SCR_INT
^ IFTRUE (DATA (HS_NEGOTIATE
)),
2459 ** else make host log this message
2461 SCR_INT
^ IFFALSE (DATA (HS_NEGOTIATE
)),
2462 SIR_REJECT_RECEIVED
,
2466 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2472 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2475 ** get residue size.
2477 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2480 ** Check for message parity error.
2482 SCR_TO_REG (scratcha
),
2484 SCR_FROM_REG (socl
),
2486 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
2487 PADDRH (msg_parity
),
2488 SCR_FROM_REG (scratcha
),
2491 ** Size is 0 .. ignore message.
2493 SCR_JUMP
^ IFTRUE (DATA (0)),
2496 ** Size is not 1 .. have to interrupt.
2498 /*<<<*/ SCR_JUMPR
^ IFFALSE (DATA (1)),
2501 ** Check for residue byte in swide register
2503 SCR_FROM_REG (scntl2
),
2505 /*<<<*/ SCR_JUMPR
^ IFFALSE (MASK (WSR
, WSR
)),
2508 ** There IS data in the swide register.
2511 SCR_REG_REG (scntl2
, SCR_OR
, WSR
),
2516 ** Load again the size to the sfbr register.
2518 /*>>>*/ SCR_FROM_REG (scratcha
),
2525 }/*-------------------------< MSG_EXTENDED >-------------*/,{
2531 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2536 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2539 ** Check for message parity error.
2541 SCR_TO_REG (scratcha
),
2543 SCR_FROM_REG (socl
),
2545 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
2546 PADDRH (msg_parity
),
2547 SCR_FROM_REG (scratcha
),
2551 SCR_JUMP
^ IFTRUE (DATA (3)),
2553 SCR_JUMP
^ IFFALSE (DATA (2)),
2555 }/*-------------------------< MSG_EXT_2 >----------------*/,{
2558 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2561 ** get extended message code.
2563 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2566 ** Check for message parity error.
2568 SCR_TO_REG (scratcha
),
2570 SCR_FROM_REG (socl
),
2572 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
2573 PADDRH (msg_parity
),
2574 SCR_FROM_REG (scratcha
),
2576 SCR_JUMP
^ IFTRUE (DATA (MSG_EXT_WDTR
)),
2579 ** unknown extended message
2583 }/*-------------------------< MSG_WDTR >-----------------*/,{
2586 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2589 ** get data bus width
2591 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2593 SCR_FROM_REG (socl
),
2595 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
2596 PADDRH (msg_parity
),
2598 ** let the host do the real work.
2603 ** let the target fetch our answer.
2610 SCR_INT
^ IFFALSE (WHEN (SCR_MSG_OUT
)),
2613 ** Send the MSG_EXT_WDTR
2615 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT
,
2623 PADDR (msg_out_done
),
2625 }/*-------------------------< MSG_EXT_3 >----------------*/,{
2628 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2631 ** get extended message code.
2633 SCR_MOVE_ABS (1) ^ SCR_MSG_IN
,
2636 ** Check for message parity error.
2638 SCR_TO_REG (scratcha
),
2640 SCR_FROM_REG (socl
),
2642 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
2643 PADDRH (msg_parity
),
2644 SCR_FROM_REG (scratcha
),
2646 SCR_JUMP
^ IFTRUE (DATA (MSG_EXT_SDTR
)),
2649 ** unknown extended message
2654 }/*-------------------------< MSG_SDTR >-----------------*/,{
2657 SCR_JUMP
^ IFFALSE (WHEN (SCR_MSG_IN
)),
2660 ** get period and offset
2662 SCR_MOVE_ABS (2) ^ SCR_MSG_IN
,
2664 SCR_FROM_REG (socl
),
2666 SCR_JUMP
^ IFTRUE (MASK (CATN
, CATN
)),
2667 PADDRH (msg_parity
),
2669 ** let the host do the real work.
2674 ** let the target fetch our answer.
2681 SCR_INT
^ IFFALSE (WHEN (SCR_MSG_OUT
)),
2684 ** Send the MSG_EXT_SDTR
2686 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT
,
2694 PADDR (msg_out_done
),
2696 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
2698 ** After ABORT message,
2700 ** expect an immediate disconnect, ...
2702 SCR_REG_REG (scntl2
, SCR_AND
, 0x7f),
2704 SCR_CLR (SCR_ACK
|SCR_ATN
),
2709 ** ... and set the status to "ABORTED"
2711 SCR_LOAD_REG (HS_REG
, HS_ABORTED
),
2716 }/*-------------------------< GETCC >-----------------------*/,{
2718 ** The ncr doesn't have an indirect load
2719 ** or store command. So we have to
2720 ** copy part of the control block to a
2721 ** fixed place, where we can modify it.
2723 ** We patch the address part of a COPY command
2724 ** with the address of the dsa register ...
2730 ** ... then we do the actual copy.
2732 SCR_COPY (sizeof (struct head
)),
2733 }/*-------------------------< GETCC1 >----------------------*/,{
2737 ** Initialize the status registers
2740 NADDR (header
.status
),
2742 }/*-------------------------< GETCC2 >----------------------*/,{
2744 ** Get the condition code from a target.
2746 ** DSA points to a data structure.
2747 ** Set TEMP to the script location
2748 ** that receives the condition code.
2750 ** Because there is no script command
2751 ** to load a longword into a register,
2752 ** we use a CALL command.
2757 ** Get the condition code.
2759 SCR_MOVE_TBL
^ SCR_DATA_IN
,
2760 offsetof (struct dsb
, sense
),
2762 ** No data phase may follow!
2771 ** The CALL jumps to this point.
2772 ** Prepare for a RESTORE_POINTER message.
2773 ** Save the TEMP register into the saved pointer.
2777 NADDR (header
.savep
),
2779 ** Load scratcha, because in case of a selection timeout,
2780 ** the host will expect a new value for startpos in
2781 ** the scratcha register.
2786 #ifdef NCR_GETCC_WITHMSG
2788 ** If QUIRK_NOMSG is set, select without ATN.
2789 ** and don't send a message.
2791 SCR_FROM_REG (QU_REG
),
2793 SCR_JUMP
^ IFTRUE (MASK (QUIRK_NOMSG
, QUIRK_NOMSG
)),
2796 ** Then try to connect to the target.
2797 ** If we are reselected, special treatment
2798 ** of the current job is required before
2799 ** accepting the reselection.
2801 SCR_SEL_TBL_ATN
^ offsetof (struct dsb
, select
),
2804 ** Send the IDENTIFY message.
2805 ** In case of short transfer, remove ATN.
2807 SCR_MOVE_TBL
^ SCR_MSG_OUT
,
2808 offsetof (struct dsb
, smsg2
),
2812 ** save the first byte of the message.
2821 }/*-------------------------< GETCC3 >----------------------*/,{
2823 ** Try to connect to the target.
2824 ** If we are reselected, special treatment
2825 ** of the current job is required before
2826 ** accepting the reselection.
2828 ** Silly target won't accept a message.
2829 ** Select without ATN.
2831 SCR_SEL_TBL
^ offsetof (struct dsb
, select
),
2834 ** Force error if selection timeout
2836 SCR_JUMPR
^ IFTRUE (WHEN (SCR_MSG_IN
)),
2843 }/*-------------------------< ABORTTAG >-------------------*/,{
2845 ** Abort a bad reselection.
2846 ** Set the message to ABORT vs. ABORT_TAG
2848 SCR_LOAD_REG (scratcha
, MSG_ABORT_TAG
),
2850 SCR_JUMPR
^ IFFALSE (CARRYSET
),
2852 }/*-------------------------< ABORT >----------------------*/,{
2853 SCR_LOAD_REG (scratcha
, MSG_ABORT
),
2864 ** we expect an immediate disconnect
2866 SCR_REG_REG (scntl2
, SCR_AND
, 0x7f),
2868 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT
,
2873 SCR_CLR (SCR_ACK
|SCR_ATN
),
2879 }/*-------------------------< SNOOPTEST >-------------------*/,{
2881 ** Read the variable.
2884 KVAR (KVAR_NCR_CACHE
),
2887 ** Write the variable.
2891 KVAR (KVAR_NCR_CACHE
),
2893 ** Read back the variable.
2896 KVAR (KVAR_NCR_CACHE
),
2898 }/*-------------------------< SNOOPEND >-------------------*/,{
2904 }/*--------------------------------------------------------*/
2908 /*==========================================================
2911 ** Fill in #define dependent parts of the script
2914 **==========================================================
2918 ncr_script_fill (struct script
* scr
, struct scripth
* scrh
)
2924 for (i
=0; i
<MAX_START
; i
++) {
2926 *p
++ =NADDR (squeue
[i
]);
2929 *p
++ =PADDR (trysel
);
2932 *p
++ =PADDRH(tryloop
);
2934 assert ((char *)p
== (char *)&scrh
->tryloop
+ sizeof (scrh
->tryloop
));
2938 *p
++ =SCR_JUMP
^ IFFALSE (WHEN (SCR_DATA_IN
));
2939 *p
++ =PADDR (no_data
);
2940 *p
++ =SCR_COPY (sizeof (ticks
));
2941 *p
++ =(ncrcmd
) KVAR (KVAR_TICKS
);
2942 *p
++ =NADDR (header
.stamp
.data
);
2943 *p
++ =SCR_MOVE_TBL
^ SCR_DATA_IN
;
2944 *p
++ =offsetof (struct dsb
, data
[ 0]);
2946 for (i
=1; i
<MAX_SCATTER
; i
++) {
2947 *p
++ =SCR_CALL
^ IFFALSE (WHEN (SCR_DATA_IN
));
2948 *p
++ =PADDR (checkatn
);
2949 *p
++ =SCR_MOVE_TBL
^ SCR_DATA_IN
;
2950 *p
++ =offsetof (struct dsb
, data
[i
]);
2954 *p
++ =PADDR (checkatn
);
2956 *p
++ =PADDR (no_data
);
2958 assert ((char *)p
== (char *)&scr
->data_in
+ sizeof (scr
->data_in
));
2962 *p
++ =SCR_JUMP
^ IFFALSE (WHEN (SCR_DATA_OUT
));
2963 *p
++ =PADDR (no_data
);
2964 *p
++ =SCR_COPY (sizeof (ticks
));
2965 *p
++ =(ncrcmd
) KVAR (KVAR_TICKS
);
2966 *p
++ =NADDR (header
.stamp
.data
);
2967 *p
++ =SCR_MOVE_TBL
^ SCR_DATA_OUT
;
2968 *p
++ =offsetof (struct dsb
, data
[ 0]);
2970 for (i
=1; i
<MAX_SCATTER
; i
++) {
2971 *p
++ =SCR_CALL
^ IFFALSE (WHEN (SCR_DATA_OUT
));
2972 *p
++ =PADDR (dispatch
);
2973 *p
++ =SCR_MOVE_TBL
^ SCR_DATA_OUT
;
2974 *p
++ =offsetof (struct dsb
, data
[i
]);
2978 *p
++ =PADDR (dispatch
);
2980 *p
++ =PADDR (no_data
);
2982 assert ((char *)p
== (char *)&scr
->data_out
+ sizeof (scr
->data_out
));
2985 /*==========================================================
2988 ** Copy and rebind a script.
2991 **==========================================================
2994 static void ncr_script_copy_and_bind (ncb_p np
, ncrcmd
*src
, ncrcmd
*dst
, int len
)
2996 ncrcmd opcode
, new, old
, tmp1
, tmp2
;
2997 ncrcmd
*start
, *end
;
3007 WRITESCRIPT_OFF(dst
, offset
, opcode
);
3011 ** If we forget to change the length
3012 ** in struct script, a field will be
3013 ** padded with 0. This is an illegal
3018 kprintf ("%s: ERROR0 IN SCRIPT at %d.\n",
3019 ncr_name(np
), (int) (src
-start
-1));
3023 if (DEBUG_FLAGS
& DEBUG_SCRIPT
)
3024 kprintf ("%p: <%x>\n",
3025 (src
-1), (unsigned)opcode
);
3028 ** We don't have to decode ALL commands
3030 switch (opcode
>> 28) {
3034 ** COPY has TWO arguments.
3038 if ((tmp1
& RELOC_MASK
) == RELOC_KVAR
)
3041 if ((tmp2
& RELOC_MASK
) == RELOC_KVAR
)
3043 if ((tmp1
^ tmp2
) & 3) {
3044 kprintf ("%s: ERROR1 IN SCRIPT at %d.\n",
3045 ncr_name(np
), (int) (src
-start
-1));
3049 ** If PREFETCH feature not enabled, remove
3050 ** the NO FLUSH bit if present.
3052 if ((opcode
& SCR_NO_FLUSH
) && !(np
->features
&FE_PFEN
))
3053 WRITESCRIPT_OFF(dst
, offset
- 4,
3054 (opcode
& ~SCR_NO_FLUSH
));
3059 ** MOVE (absolute address)
3067 ** dont't relocate if relative :-)
3069 if (opcode
& 0x00800000)
3091 switch (old
& RELOC_MASK
) {
3092 case RELOC_REGISTER
:
3093 new = (old
& ~RELOC_MASK
) + rman_get_start(np
->reg_res
);
3096 new = (old
& ~RELOC_MASK
) + np
->p_script
;
3099 new = (old
& ~RELOC_MASK
) + np
->p_scripth
;
3102 new = (old
& ~RELOC_MASK
) + vtophys(np
);
3105 if (((old
& ~RELOC_MASK
) <
3106 SCRIPT_KVAR_FIRST
) ||
3107 ((old
& ~RELOC_MASK
) >
3109 panic("ncr KVAR out of range");
3110 new = vtophys(script_kvars
[old
&
3114 /* Don't relocate a 0 address. */
3121 panic("ncr_script_copy_and_bind: weird relocation %x @ %d", old
, (int)(src
- start
));
3125 WRITESCRIPT_OFF(dst
, offset
, new);
3129 WRITESCRIPT_OFF(dst
, offset
, *src
++);
3136 /*==========================================================
3139 ** Auto configuration.
3142 **==========================================================
3146 /*----------------------------------------------------------
3148 ** Reduce the transfer length to the max value
3149 ** we can transfer safely.
3151 ** Reading a block greater then MAX_SIZE from the
3152 ** raw (character) device exercises a memory leak
3153 ** in the vm subsystem. This is common to ALL devices.
3154 ** We have submitted a description of this bug to
3155 ** <FreeBSD-bugs@freefall.cdrom.com>.
3156 ** It should be fixed in the current release.
3158 **----------------------------------------------------------
3161 void ncr_min_phys (struct buf
*bp
)
3163 if ((unsigned long)bp
->b_bcount
> MAX_SIZE
) bp
->b_bcount
= MAX_SIZE
;
3169 /*----------------------------------------------------------
3171 ** Maximal number of outstanding requests per target.
3173 **----------------------------------------------------------
3176 u_int32_t
ncr_info (int unit
)
3178 return (1); /* may be changed later */
3183 /*----------------------------------------------------------
3185 ** NCR chip devices table and chip look up function.
3186 ** Features bit are defined in ncrreg.h. Is it the
3189 **----------------------------------------------------------
3192 unsigned long device_id
;
3193 unsigned short minrevid
;
3195 unsigned char maxburst
;
3196 unsigned char maxoffs
;
3197 unsigned char clock_divn
;
3198 unsigned int features
;
3201 static ncr_chip ncr_chip_table
[] = {
3202 {NCR_810_ID
, 0x00, "ncr 53c810 fast10 scsi", 4, 8, 4,
3205 {NCR_810_ID
, 0x10, "ncr 53c810a fast10 scsi", 4, 8, 4,
3206 FE_ERL
|FE_LDSTR
|FE_PFEN
|FE_BOF
}
3208 {NCR_815_ID
, 0x00, "ncr 53c815 fast10 scsi", 4, 8, 4,
3211 {NCR_820_ID
, 0x00, "ncr 53c820 fast10 wide scsi", 4, 8, 4,
3214 {NCR_825_ID
, 0x00, "ncr 53c825 fast10 wide scsi", 4, 8, 4,
3215 FE_WIDE
|FE_ERL
|FE_BOF
}
3217 {NCR_825_ID
, 0x10, "ncr 53c825a fast10 wide scsi", 7, 8, 4,
3218 FE_WIDE
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3220 {NCR_860_ID
, 0x00, "ncr 53c860 fast20 scsi", 4, 8, 5,
3221 FE_ULTRA
|FE_CLK80
|FE_CACHE_SET
|FE_LDSTR
|FE_PFEN
}
3223 {NCR_875_ID
, 0x00, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
3224 FE_WIDE
|FE_ULTRA
|FE_CLK80
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3226 {NCR_875_ID
, 0x02, "ncr 53c875 fast20 wide scsi", 7, 16, 5,
3227 FE_WIDE
|FE_ULTRA
|FE_DBLR
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3229 {NCR_875_ID2
, 0x00, "ncr 53c875j fast20 wide scsi", 7, 16, 5,
3230 FE_WIDE
|FE_ULTRA
|FE_DBLR
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3232 {NCR_885_ID
, 0x00, "ncr 53c885 fast20 wide scsi", 7, 16, 5,
3233 FE_WIDE
|FE_ULTRA
|FE_DBLR
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3235 {NCR_895_ID
, 0x00, "ncr 53c895 fast40 wide scsi", 7, 31, 7,
3236 FE_WIDE
|FE_ULTRA2
|FE_QUAD
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3238 {NCR_896_ID
, 0x00, "ncr 53c896 fast40 wide scsi", 7, 31, 7,
3239 FE_WIDE
|FE_ULTRA2
|FE_QUAD
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3241 {NCR_895A_ID
, 0x00, "ncr 53c895a fast40 wide scsi", 7, 31, 7,
3242 FE_WIDE
|FE_ULTRA2
|FE_QUAD
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3244 {NCR_1510D_ID
, 0x00, "ncr 53c1510d fast40 wide scsi", 7, 31, 7,
3245 FE_WIDE
|FE_ULTRA2
|FE_QUAD
|FE_CACHE_SET
|FE_DFS
|FE_LDSTR
|FE_PFEN
|FE_RAM
}
3248 static int ncr_chip_lookup(u_long device_id
, u_char revision_id
)
3253 for (i
= 0; i
< NELEM(ncr_chip_table
); i
++) {
3254 if (device_id
== ncr_chip_table
[i
].device_id
&&
3255 ncr_chip_table
[i
].minrevid
<= revision_id
) {
3257 ncr_chip_table
[found
].minrevid
3258 < ncr_chip_table
[i
].minrevid
) {
3266 /*----------------------------------------------------------
3268 ** Probe the hostadapter.
3270 **----------------------------------------------------------
3275 static int ncr_probe (device_t dev
)
3279 i
= ncr_chip_lookup(pci_get_devid(dev
), pci_get_revid(dev
));
3281 device_set_desc(dev
, ncr_chip_table
[i
].name
);
3282 return (-1000); /* Allows to use both ncr and sym */
3290 /*==========================================================
3292 ** NCR chip clock divisor table.
3293 ** Divisors are multiplied by 10,000,000 in order to make
3294 ** calculations more simple.
3296 **==========================================================
3300 static u_long div_10M
[] =
3301 {2*_5M
, 3*_5M
, 4*_5M
, 6*_5M
, 8*_5M
, 12*_5M
, 16*_5M
};
3303 /*===============================================================
3305 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3306 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3307 ** We use log base 2 (burst length) as internal code, with
3308 ** value 0 meaning "burst disabled".
3310 **===============================================================
3314 * Burst length from burst code.
3316 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3319 * Burst code from io register bits.
3321 #define burst_code(dmode, ctest4, ctest5) \
3322 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
3325 * Set initial io register bits from burst code.
3328 ncr_init_burst(ncb_p np
, u_char bc
)
3330 np
->rv_ctest4
&= ~0x80;
3331 np
->rv_dmode
&= ~(0x3 << 6);
3332 np
->rv_ctest5
&= ~0x4;
3335 np
->rv_ctest4
|= 0x80;
3339 np
->rv_dmode
|= ((bc
& 0x3) << 6);
3340 np
->rv_ctest5
|= (bc
& 0x4);
3344 /*==========================================================
3347 ** Auto configuration: attach and init a host adapter.
3350 **==========================================================
3355 ncr_attach (device_t dev
)
3357 ncb_p np
= (struct ncb
*) device_get_softc(dev
);
3363 struct cam_devq
*devq
;
3366 ** allocate and initialize structures.
3369 np
->unit
= device_get_unit(dev
);
3372 ** Try to map the controller chip to
3373 ** virtual and physical memory.
3377 np
->reg_res
= bus_alloc_resource(dev
, SYS_RES_MEMORY
, &np
->reg_rid
,
3378 0, ~0, 1, RF_ACTIVE
);
3380 device_printf(dev
, "could not map memory\n");
3385 ** Make the controller's registers available.
3386 ** Now the INB INW INL OUTB OUTW OUTL macros
3387 ** can be used safely.
3390 np
->bst
= rman_get_bustag(np
->reg_res
);
3391 np
->bsh
= rman_get_bushandle(np
->reg_res
);
3396 ** Try to map the controller chip into iospace.
3399 if (!pci_map_port (config_id
, 0x10, &np
->port
))
3405 ** Save some controller register default values
3408 np
->rv_scntl3
= INB(nc_scntl3
) & 0x77;
3409 np
->rv_dmode
= INB(nc_dmode
) & 0xce;
3410 np
->rv_dcntl
= INB(nc_dcntl
) & 0xa9;
3411 np
->rv_ctest3
= INB(nc_ctest3
) & 0x01;
3412 np
->rv_ctest4
= INB(nc_ctest4
) & 0x88;
3413 np
->rv_ctest5
= INB(nc_ctest5
) & 0x24;
3414 np
->rv_gpcntl
= INB(nc_gpcntl
);
3415 np
->rv_stest2
= INB(nc_stest2
) & 0x20;
3417 if (bootverbose
>= 2) {
3418 kprintf ("\tBIOS values: SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
3419 np
->rv_scntl3
, np
->rv_dmode
, np
->rv_dcntl
);
3420 kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
3421 np
->rv_ctest3
, np
->rv_ctest4
, np
->rv_ctest5
);
3424 np
->rv_dcntl
|= NOCOM
;
3427 ** Do chip dependent initialization.
3430 rev
= pci_get_revid(dev
);
3433 ** Get chip features from chips table.
3435 i
= ncr_chip_lookup(pci_get_devid(dev
), rev
);
3438 np
->maxburst
= ncr_chip_table
[i
].maxburst
;
3439 np
->maxoffs
= ncr_chip_table
[i
].maxoffs
;
3440 np
->clock_divn
= ncr_chip_table
[i
].clock_divn
;
3441 np
->features
= ncr_chip_table
[i
].features
;
3442 } else { /* Should'nt happen if probe() is ok */
3446 np
->features
= FE_ERL
;
3449 np
->maxwide
= np
->features
& FE_WIDE
? 1 : 0;
3450 np
->clock_khz
= np
->features
& FE_CLK80
? 80000 : 40000;
3451 if (np
->features
& FE_QUAD
) np
->multiplier
= 4;
3452 else if (np
->features
& FE_DBLR
) np
->multiplier
= 2;
3453 else np
->multiplier
= 1;
3456 ** Get the frequency of the chip's clock.
3457 ** Find the right value for scntl3.
3459 if (np
->features
& (FE_ULTRA
|FE_ULTRA2
))
3460 ncr_getclock(np
, np
->multiplier
);
3462 #ifdef NCR_TEKRAM_EEPROM
3464 kprintf ("%s: Tekram EEPROM read %s\n",
3466 read_tekram_eeprom (np
, NULL
) ?
3467 "succeeded" : "failed");
3469 #endif /* NCR_TEKRAM_EEPROM */
3472 * If scntl3 != 0, we assume BIOS is present.
3475 np
->features
|= FE_BIOS
;
3478 * Divisor to be used for async (timer pre-scaler).
3480 i
= np
->clock_divn
- 1;
3483 if (10ul * SCSI_NCR_MIN_ASYNC
* np
->clock_khz
> div_10M
[i
]) {
3488 np
->rv_scntl3
= i
+1;
3491 * Minimum synchronous period factor supported by the chip.
3492 * Btw, 'period' is in tenths of nanoseconds.
3495 period
= (4 * div_10M
[0] + np
->clock_khz
- 1) / np
->clock_khz
;
3496 if (period
<= 250) np
->minsync
= 10;
3497 else if (period
<= 303) np
->minsync
= 11;
3498 else if (period
<= 500) np
->minsync
= 12;
3499 else np
->minsync
= (period
+ 40 - 1) / 40;
3502 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3505 if (np
->minsync
< 25 && !(np
->features
& (FE_ULTRA
|FE_ULTRA2
)))
3507 else if (np
->minsync
< 12 && !(np
->features
& FE_ULTRA2
))
3511 * Maximum synchronous period factor supported by the chip.
3514 period
= (11 * div_10M
[np
->clock_divn
- 1]) / (4 * np
->clock_khz
);
3515 np
->maxsync
= period
> 2540 ? 254 : period
/ 10;
3518 * Now, some features available with Symbios compatible boards.
3519 * LED support through GPIO0 and DIFF support.
3522 #ifdef SCSI_NCR_SYMBIOS_COMPAT
3523 if (!(np
->rv_gpcntl
& 0x01))
3524 np
->features
|= FE_LED0
;
3525 #if 0 /* Not safe enough without NVRAM support or user settable option */
3526 if (!(INB(nc_gpreg
) & 0x08))
3527 np
->features
|= FE_DIFF
;
3529 #endif /* SCSI_NCR_SYMBIOS_COMPAT */
3532 * Prepare initial IO registers settings.
3533 * Trust BIOS only if we believe we have one and if we want to.
3535 #ifdef SCSI_NCR_TRUST_BIOS
3536 if (!(np
->features
& FE_BIOS
)) {
3541 np
->rv_dcntl
= NOCOM
;
3543 np
->rv_ctest4
= MPEE
;
3547 if (np
->features
& FE_ERL
)
3548 np
->rv_dmode
|= ERL
; /* Enable Read Line */
3549 if (np
->features
& FE_BOF
)
3550 np
->rv_dmode
|= BOF
; /* Burst Opcode Fetch */
3551 if (np
->features
& FE_ERMP
)
3552 np
->rv_dmode
|= ERMP
; /* Enable Read Multiple */
3553 if (np
->features
& FE_CLSE
)
3554 np
->rv_dcntl
|= CLSE
; /* Cache Line Size Enable */
3555 if (np
->features
& FE_WRIE
)
3556 np
->rv_ctest3
|= WRIE
; /* Write and Invalidate */
3557 if (np
->features
& FE_PFEN
)
3558 np
->rv_dcntl
|= PFEN
; /* Prefetch Enable */
3559 if (np
->features
& FE_DFS
)
3560 np
->rv_ctest5
|= DFS
; /* Dma Fifo Size */
3561 if (np
->features
& FE_DIFF
)
3562 np
->rv_stest2
|= 0x20; /* Differential mode */
3563 ncr_init_burst(np
, np
->maxburst
); /* Max dwords burst length */
3566 burst_code(np
->rv_dmode
, np
->rv_ctest4
, np
->rv_ctest5
);
3570 ** Get on-chip SRAM address, if supported
3572 if ((np
->features
& FE_RAM
) && sizeof(struct script
) <= 4096) {
3573 np
->sram_rid
= 0x18;
3574 np
->sram_res
= bus_alloc_resource(dev
, SYS_RES_MEMORY
,
3576 0, ~0, 1, RF_ACTIVE
);
3580 ** Allocate structure for script relocation.
3582 if (np
->sram_res
!= NULL
) {
3584 np
->p_script
= rman_get_start(np
->sram_res
);
3585 np
->bst2
= rman_get_bustag(np
->sram_res
);
3586 np
->bsh2
= rman_get_bushandle(np
->sram_res
);
3587 } else if (sizeof (struct script
) > PAGE_SIZE
) {
3588 np
->script
= (struct script
*) kmem_alloc_contig
3589 (round_page(sizeof (struct script
)),
3590 0, 0xffffffff, PAGE_SIZE
);
3592 np
->script
= (struct script
*)
3593 kmalloc (sizeof (struct script
), M_DEVBUF
, M_WAITOK
);
3596 /* XXX JGibbs - Use contigmalloc */
3597 if (sizeof (struct scripth
) > PAGE_SIZE
) {
3598 np
->scripth
= (struct scripth
*) kmem_alloc_contig
3599 (round_page(sizeof (struct scripth
)),
3600 0, 0xffffffff, PAGE_SIZE
);
3603 np
->scripth
= (struct scripth
*)
3604 kmalloc (sizeof (struct scripth
), M_DEVBUF
, M_WAITOK
);
3607 #ifdef SCSI_NCR_PCI_CONFIG_FIXUP
3609 ** If cache line size is enabled, check PCI config space and
3610 ** try to fix it up if necessary.
3612 #ifdef PCIR_CACHELNSZ /* To be sure that new PCI stuff is present */
3614 u_char cachelnsz
= pci_read_config(dev
, PCIR_CACHELNSZ
, 1);
3615 u_short command
= pci_read_config(dev
, PCIR_COMMAND
, 2);
3619 kprintf("%s: setting PCI cache line size register to %d.\n",
3620 ncr_name(np
), (int)cachelnsz
);
3621 pci_write_config(dev
, PCIR_CACHELNSZ
, cachelnsz
, 1);
3624 if (!(command
& (1<<4))) {
3626 kprintf("%s: setting PCI command write and invalidate.\n",
3628 pci_write_config(dev
, PCIR_COMMAND
, command
, 2);
3631 #endif /* PCIR_CACHELNSZ */
3633 #endif /* SCSI_NCR_PCI_CONFIG_FIXUP */
3635 /* Initialize per-target user settings */
3637 if (SCSI_NCR_DFLT_SYNC
) {
3638 usrsync
= SCSI_NCR_DFLT_SYNC
;
3639 if (usrsync
> np
->maxsync
)
3640 usrsync
= np
->maxsync
;
3641 if (usrsync
< np
->minsync
)
3642 usrsync
= np
->minsync
;
3645 usrwide
= (SCSI_NCR_MAX_WIDE
);
3646 if (usrwide
> np
->maxwide
) usrwide
=np
->maxwide
;
3648 for (i
=0;i
<MAX_TARGET
;i
++) {
3649 tcb_p tp
= &np
->target
[i
];
3651 tp
->tinfo
.user
.period
= usrsync
;
3652 tp
->tinfo
.user
.offset
= usrsync
!= 0 ? np
->maxoffs
: 0;
3653 tp
->tinfo
.user
.width
= usrwide
;
3654 tp
->tinfo
.disc_tag
= NCR_CUR_DISCENB
3661 ** Bells and whistles ;-)
3664 kprintf("%s: minsync=%d, maxsync=%d, maxoffs=%d, %d dwords burst, %s dma fifo\n",
3665 ncr_name(np
), np
->minsync
, np
->maxsync
, np
->maxoffs
,
3666 burst_length(np
->maxburst
),
3667 (np
->rv_ctest5
& DFS
) ? "large" : "normal");
3670 ** Print some complementary information that can be helpfull.
3673 kprintf("%s: %s, %s IRQ driver%s\n",
3675 np
->rv_stest2
& 0x20 ? "differential" : "single-ended",
3676 np
->rv_dcntl
& IRQM
? "totem pole" : "open drain",
3677 np
->sram_res
? ", using on-chip SRAM" : "");
3680 ** Patch scripts to physical addresses
3682 ncr_script_fill (&script0
, &scripth0
);
3685 np
->p_script
= vtophys(np
->script
);
3686 np
->p_scripth
= vtophys(np
->scripth
);
3688 ncr_script_copy_and_bind (np
, (ncrcmd
*) &script0
,
3689 (ncrcmd
*) np
->script
, sizeof(struct script
));
3691 ncr_script_copy_and_bind (np
, (ncrcmd
*) &scripth0
,
3692 (ncrcmd
*) np
->scripth
, sizeof(struct scripth
));
3695 ** Patch the script for LED support.
3698 if (np
->features
& FE_LED0
) {
3699 WRITESCRIPT(reselect
[0], SCR_REG_REG(gpreg
, SCR_OR
, 0x01));
3700 WRITESCRIPT(reselect1
[0], SCR_REG_REG(gpreg
, SCR_AND
, 0xfe));
3701 WRITESCRIPT(reselect2
[0], SCR_REG_REG(gpreg
, SCR_AND
, 0xfe));
3705 ** init data structure
3708 np
->jump_tcb
.l_cmd
= SCR_JUMP
;
3709 np
->jump_tcb
.l_paddr
= NCB_SCRIPTH_PHYS (np
, abort
);
3712 ** Get SCSI addr of host adapter (set by bios?).
3715 np
->myaddr
= INB(nc_scid
) & 0x07;
3716 if (!np
->myaddr
) np
->myaddr
= SCSI_NCR_MYADDR
;
3720 ** Log the initial register contents
3724 for (reg
=0; reg
<256; reg
+=4) {
3725 if (reg
%16==0) kprintf ("reg[%2x]", reg
);
3726 kprintf (" %08x", (int)pci_conf_read (config_id
, reg
));
3727 if (reg
%16==12) kprintf ("\n");
3730 #endif /* NCR_DUMP_REG */
3736 OUTB (nc_istat
, SRST
);
3738 OUTB (nc_istat
, 0 );
3742 ** Now check the cache handling of the pci chipset.
3745 if (ncr_snooptest (np
)) {
3746 kprintf ("CACHE INCORRECTLY CONFIGURED.\n");
3751 ** Install the interrupt handler.
3755 np
->irq_res
= bus_alloc_resource(dev
, SYS_RES_IRQ
, &rid
, 0, ~0, 1,
3756 RF_SHAREABLE
| RF_ACTIVE
);
3757 if (np
->irq_res
== NULL
) {
3759 "interruptless mode: reduced performance.\n");
3761 bus_setup_intr(dev
, np
->irq_res
, 0,
3762 ncr_intr
, np
, &np
->irq_handle
, NULL
);
3766 ** Create the device queue. We only allow MAX_START-1 concurrent
3767 ** transactions so we can be sure to have one element free in our
3768 ** start queue to reset to the idle loop.
3770 devq
= cam_simq_alloc(MAX_START
- 1);
3775 ** Now tell the generic SCSI layer
3778 np
->sim
= cam_sim_alloc(ncr_action
, ncr_poll
, "ncr", np
, np
->unit
,
3779 &sim_mplock
, 1, MAX_TAGS
, devq
);
3780 cam_simq_release(devq
);
3781 if (np
->sim
== NULL
)
3785 if (xpt_bus_register(np
->sim
, 0) != CAM_SUCCESS
) {
3786 cam_sim_free(np
->sim
);
3790 if (xpt_create_path(&np
->path
, /*periph*/NULL
,
3791 cam_sim_path(np
->sim
), CAM_TARGET_WILDCARD
,
3792 CAM_LUN_WILDCARD
) != CAM_REQ_CMP
) {
3793 xpt_bus_deregister(cam_sim_path(np
->sim
));
3794 cam_sim_free(np
->sim
);
3799 ** start the timeout daemon
3801 callout_init(&np
->timeout_ch
);
3808 /*==========================================================
3811 ** Process pending device interrupts.
3814 **==========================================================
3823 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("[");
3825 if (INB(nc_istat
) & (INTF
|SIP
|DIP
)) {
3827 ** Repeat until no outstanding ints
3831 } while (INB(nc_istat
) & (INTF
|SIP
|DIP
));
3836 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("]\n");
3841 /*==========================================================
3844 ** Start execution of a SCSI command.
3845 ** This is called from the generic SCSI driver.
3848 **==========================================================
3852 ncr_action (struct cam_sim
*sim
, union ccb
*ccb
)
3856 np
= (ncb_p
) cam_sim_softc(sim
);
3858 switch (ccb
->ccb_h
.func_code
) {
3859 /* Common cases first */
3860 case XPT_SCSI_IO
: /* Execute the requested I/O operation */
3865 struct ccb_scsiio
*csio
;
3874 tp
= &np
->target
[ccb
->ccb_h
.target_id
];
3880 * Last time we need to check if this CCB needs to
3883 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_INPROG
) {
3888 ccb
->ccb_h
.status
|= CAM_SIM_QUEUED
;
3890 /*---------------------------------------------------
3892 ** Assign an nccb / bind ccb
3894 **----------------------------------------------------
3896 cp
= ncr_get_nccb (np
, ccb
->ccb_h
.target_id
,
3897 ccb
->ccb_h
.target_lun
);
3899 /* XXX JGibbs - Freeze SIMQ */
3900 ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
3908 /*---------------------------------------------------
3912 **----------------------------------------------------
3915 ** XXX JGibbs - Isn't this expensive
3916 ** enough to be conditionalized??
3919 bzero (&cp
->phys
.header
.stamp
, sizeof (struct tstamp
));
3920 cp
->phys
.header
.stamp
.start
= ticks
;
3923 if (tp
->nego_cp
== NULL
) {
3925 if (tp
->tinfo
.current
.width
3926 != tp
->tinfo
.goal
.width
) {
3929 } else if ((tp
->tinfo
.current
.period
3930 != tp
->tinfo
.goal
.period
)
3931 || (tp
->tinfo
.current
.offset
3932 != tp
->tinfo
.goal
.offset
)) {
3938 /*---------------------------------------------------
3940 ** choose a new tag ...
3942 **----------------------------------------------------
3944 lp
= tp
->lp
[ccb
->ccb_h
.target_lun
];
3946 if ((ccb
->ccb_h
.flags
& CAM_TAG_ACTION_VALID
) != 0
3947 && (ccb
->csio
.tag_action
!= CAM_TAG_ACTION_NONE
)
3950 ** assign a tag to this nccb
3953 nccb_p cp2
= lp
->next_nccb
;
3954 lp
->lasttag
= lp
->lasttag
% 255 + 1;
3955 while (cp2
&& cp2
->tag
!= lp
->lasttag
)
3956 cp2
= cp2
->next_nccb
;
3958 cp
->tag
=lp
->lasttag
;
3959 if (DEBUG_FLAGS
& DEBUG_TAGS
) {
3961 kprintf ("using tag #%d.\n", cp
->tag
);
3968 /*----------------------------------------------------
3970 ** Build the identify / tag / sdtr message
3972 **----------------------------------------------------
3974 idmsg
= MSG_IDENTIFYFLAG
| ccb
->ccb_h
.target_lun
;
3975 if (tp
->tinfo
.disc_tag
& NCR_CUR_DISCENB
)
3976 idmsg
|= MSG_IDENTIFY_DISCFLAG
;
3978 msgptr
= cp
->scsi_smsg
;
3980 msgptr
[msglen
++] = idmsg
;
3983 msgptr
[msglen
++] = ccb
->csio
.tag_action
;
3984 msgptr
[msglen
++] = cp
->tag
;
3989 msgptr
[msglen
++] = MSG_EXTENDED
;
3990 msgptr
[msglen
++] = MSG_EXT_SDTR_LEN
;
3991 msgptr
[msglen
++] = MSG_EXT_SDTR
;
3992 msgptr
[msglen
++] = tp
->tinfo
.goal
.period
;
3993 msgptr
[msglen
++] = tp
->tinfo
.goal
.offset
;
3994 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
3996 kprintf ("sync msgout: ");
3997 ncr_show_msg (&cp
->scsi_smsg
[msglen
-5]);
4002 msgptr
[msglen
++] = MSG_EXTENDED
;
4003 msgptr
[msglen
++] = MSG_EXT_WDTR_LEN
;
4004 msgptr
[msglen
++] = MSG_EXT_WDTR
;
4005 msgptr
[msglen
++] = tp
->tinfo
.goal
.width
;
4006 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
4008 kprintf ("wide msgout: ");
4009 ncr_show_msg (&cp
->scsi_smsg
[msglen
-4]);
4015 /*----------------------------------------------------
4017 ** Build the identify message for getcc.
4019 **----------------------------------------------------
4022 cp
->scsi_smsg2
[0] = idmsg
;
4025 /*----------------------------------------------------
4027 ** Build the data descriptors
4029 **----------------------------------------------------
4032 /* XXX JGibbs - Handle other types of I/O */
4033 if ((ccb
->ccb_h
.flags
& CAM_DIR_MASK
) != CAM_DIR_NONE
) {
4034 segments
= ncr_scatter(&cp
->phys
,
4035 (vm_offset_t
)csio
->data_ptr
,
4036 (vm_size_t
)csio
->dxfer_len
);
4039 ccb
->ccb_h
.status
= CAM_REQ_TOO_BIG
;
4040 ncr_free_nccb(np
, cp
);
4045 if ((ccb
->ccb_h
.flags
& CAM_DIR_MASK
) == CAM_DIR_IN
) {
4046 cp
->phys
.header
.savep
= NCB_SCRIPT_PHYS (np
, data_in
);
4047 cp
->phys
.header
.goalp
= cp
->phys
.header
.savep
+20 +segments
*16;
4048 } else { /* CAM_DIR_OUT */
4049 cp
->phys
.header
.savep
= NCB_SCRIPT_PHYS (np
, data_out
);
4050 cp
->phys
.header
.goalp
= cp
->phys
.header
.savep
+20 +segments
*16;
4053 cp
->phys
.header
.savep
= NCB_SCRIPT_PHYS (np
, no_data
);
4054 cp
->phys
.header
.goalp
= cp
->phys
.header
.savep
;
4057 cp
->phys
.header
.lastp
= cp
->phys
.header
.savep
;
4060 /*----------------------------------------------------
4064 **----------------------------------------------------
4067 ** physical -> virtual backlink
4068 ** Generic SCSI command
4070 cp
->phys
.header
.cp
= cp
;
4074 cp
->phys
.header
.launch
.l_paddr
= NCB_SCRIPT_PHYS (np
, select
);
4075 cp
->phys
.header
.launch
.l_cmd
= SCR_JUMP
;
4079 cp
->phys
.select
.sel_id
= ccb
->ccb_h
.target_id
;
4080 cp
->phys
.select
.sel_scntl3
= tp
->tinfo
.wval
;
4081 cp
->phys
.select
.sel_sxfer
= tp
->tinfo
.sval
;
4085 cp
->phys
.smsg
.addr
= CCB_PHYS (cp
, scsi_smsg
);
4086 cp
->phys
.smsg
.size
= msglen
;
4088 cp
->phys
.smsg2
.addr
= CCB_PHYS (cp
, scsi_smsg2
);
4089 cp
->phys
.smsg2
.size
= msglen2
;
4093 /* XXX JGibbs - Support other command types */
4094 cp
->phys
.cmd
.addr
= vtophys (csio
->cdb_io
.cdb_bytes
);
4095 cp
->phys
.cmd
.size
= csio
->cdb_len
;
4099 cp
->phys
.scmd
.addr
= CCB_PHYS (cp
, sensecmd
);
4100 cp
->phys
.scmd
.size
= 6;
4102 ** patch requested size into sense command
4104 cp
->sensecmd
[0] = 0x03;
4105 cp
->sensecmd
[1] = ccb
->ccb_h
.target_lun
<< 5;
4106 cp
->sensecmd
[4] = csio
->sense_len
;
4110 cp
->phys
.sense
.addr
= vtophys (&csio
->sense_data
);
4111 cp
->phys
.sense
.size
= csio
->sense_len
;
4115 cp
->actualquirks
= QUIRK_NOMSG
;
4116 cp
->host_status
= nego
? HS_NEGOTIATE
: HS_BUSY
;
4117 cp
->s_status
= SCSI_STATUS_ILLEGAL
;
4118 cp
->parity_status
= 0;
4120 cp
->xerr_status
= XE_OK
;
4121 cp
->sync_status
= tp
->tinfo
.sval
;
4122 cp
->nego_status
= nego
;
4123 cp
->wide_status
= tp
->tinfo
.wval
;
4125 /*----------------------------------------------------
4127 ** Critical region: start this job.
4129 **----------------------------------------------------
4133 ** reselect pattern and activate this job.
4136 cp
->jump_nccb
.l_cmd
= (SCR_JUMP
^ IFFALSE (DATA (cp
->tag
)));
4137 cp
->tlimit
= time_uptime
4138 + ccb
->ccb_h
.timeout
/ 1000 + 2;
4139 cp
->magic
= CCB_MAGIC
;
4142 ** insert into start queue.
4145 qidx
= np
->squeueput
+ 1;
4146 if (qidx
>= MAX_START
)
4148 np
->squeue
[qidx
] = NCB_SCRIPT_PHYS (np
, idle
);
4149 np
->squeue
[np
->squeueput
] = CCB_PHYS (cp
, phys
);
4150 np
->squeueput
= qidx
;
4152 if(DEBUG_FLAGS
& DEBUG_QUEUE
)
4153 kprintf("%s: queuepos=%d tryoffset=%d.\n",
4154 ncr_name (np
), np
->squeueput
,
4155 (unsigned)(READSCRIPT(startpos
[0]) -
4156 (NCB_SCRIPTH_PHYS (np
, tryloop
))));
4159 ** Script processor may be waiting for reselect.
4162 OUTB (nc_istat
, SIGP
);
4165 ** and reenable interrupts
4170 case XPT_RESET_DEV
: /* Bus Device Reset the specified SCSI device */
4171 case XPT_EN_LUN
: /* Enable LUN as a target */
4172 case XPT_TARGET_IO
: /* Execute target I/O request */
4173 case XPT_ACCEPT_TARGET_IO
: /* Accept Host Target Mode CDB */
4174 case XPT_CONT_TARGET_IO
: /* Continue Host Target I/O Connection*/
4175 case XPT_ABORT
: /* Abort the specified CCB */
4177 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
4180 case XPT_SET_TRAN_SETTINGS
:
4182 struct ccb_trans_settings
*cts
= &ccb
->cts
;
4185 struct ccb_trans_settings_scsi
*scsi
=
4186 &cts
->proto_specific
.scsi
;
4187 struct ccb_trans_settings_spi
*spi
=
4188 &cts
->xport_specific
.spi
;
4191 if (cts
->type
== CTS_TYPE_CURRENT_SETTINGS
)
4192 update_type
|= NCR_TRANS_GOAL
;
4193 if (cts
->type
== CTS_TYPE_USER_SETTINGS
)
4194 update_type
|= NCR_TRANS_USER
;
4197 tp
= &np
->target
[ccb
->ccb_h
.target_id
];
4198 /* Tag and disc enables */
4199 if ((spi
->valid
& CTS_SPI_VALID_DISC
) != 0) {
4200 if (update_type
& NCR_TRANS_GOAL
) {
4201 if ((spi
->flags
& CTS_SPI_FLAGS_DISC_ENB
) != 0)
4202 tp
->tinfo
.disc_tag
|= NCR_CUR_DISCENB
;
4204 tp
->tinfo
.disc_tag
&= ~NCR_CUR_DISCENB
;
4207 if (update_type
& NCR_TRANS_USER
) {
4208 if ((spi
->flags
& CTS_SPI_FLAGS_DISC_ENB
) != 0)
4209 tp
->tinfo
.disc_tag
|= NCR_USR_DISCENB
;
4211 tp
->tinfo
.disc_tag
&= ~NCR_USR_DISCENB
;
4216 if ((scsi
->valid
& CTS_SCSI_VALID_TQ
) != 0) {
4217 if (update_type
& NCR_TRANS_GOAL
) {
4218 if ((scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) != 0)
4219 tp
->tinfo
.disc_tag
|= NCR_CUR_TAGENB
;
4221 tp
->tinfo
.disc_tag
&= ~NCR_CUR_TAGENB
;
4224 if (update_type
& NCR_TRANS_USER
) {
4225 if ((scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) != 0)
4226 tp
->tinfo
.disc_tag
|= NCR_USR_TAGENB
;
4228 tp
->tinfo
.disc_tag
&= ~NCR_USR_TAGENB
;
4232 /* Filter bus width and sync negotiation settings */
4233 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0) {
4234 if (spi
->bus_width
> np
->maxwide
)
4235 spi
->bus_width
= np
->maxwide
;
4238 if (((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) != 0)
4239 || ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0)) {
4240 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) != 0) {
4241 if (spi
->sync_period
!= 0
4242 && (spi
->sync_period
< np
->minsync
))
4243 spi
->sync_period
= np
->minsync
;
4245 if ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0) {
4246 if (spi
->sync_offset
== 0)
4247 spi
->sync_period
= 0;
4248 if (spi
->sync_offset
> np
->maxoffs
)
4249 spi
->sync_offset
= np
->maxoffs
;
4252 if ((update_type
& NCR_TRANS_USER
) != 0) {
4253 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) != 0)
4254 tp
->tinfo
.user
.period
= spi
->sync_period
;
4255 if ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0)
4256 tp
->tinfo
.user
.offset
= spi
->sync_offset
;
4257 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0)
4258 tp
->tinfo
.user
.width
= spi
->bus_width
;
4260 if ((update_type
& NCR_TRANS_GOAL
) != 0) {
4261 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) != 0)
4262 tp
->tinfo
.goal
.period
= spi
->sync_period
;
4264 if ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0)
4265 tp
->tinfo
.goal
.offset
= spi
->sync_offset
;
4267 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0)
4268 tp
->tinfo
.goal
.width
= spi
->bus_width
;
4271 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
4275 case XPT_GET_TRAN_SETTINGS
:
4276 /* Get default/user set transfer settings for the target */
4278 struct ccb_trans_settings
*cts
= &ccb
->cts
;
4279 struct ncr_transinfo
*tinfo
;
4280 tcb_p tp
= &np
->target
[ccb
->ccb_h
.target_id
];
4281 struct ccb_trans_settings_scsi
*scsi
=
4282 &cts
->proto_specific
.scsi
;
4283 struct ccb_trans_settings_spi
*spi
=
4284 &cts
->xport_specific
.spi
;
4286 cts
->protocol
= PROTO_SCSI
;
4287 cts
->protocol_version
= SCSI_REV_2
;
4288 cts
->transport
= XPORT_SPI
;
4289 cts
->transport_version
= 2;
4292 if (cts
->type
== CTS_TYPE_CURRENT_SETTINGS
) {
4293 tinfo
= &tp
->tinfo
.current
;
4294 if (tp
->tinfo
.disc_tag
& NCR_CUR_DISCENB
)
4295 spi
->flags
|= CTS_SPI_FLAGS_DISC_ENB
;
4297 spi
->flags
&= ~CTS_SPI_FLAGS_DISC_ENB
;
4299 if (tp
->tinfo
.disc_tag
& NCR_CUR_TAGENB
)
4300 scsi
->flags
|= CTS_SCSI_FLAGS_TAG_ENB
;
4302 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
4304 tinfo
= &tp
->tinfo
.user
;
4305 if (tp
->tinfo
.disc_tag
& NCR_USR_DISCENB
)
4306 spi
->flags
|= CTS_SPI_FLAGS_DISC_ENB
;
4308 spi
->flags
&= ~CTS_SPI_FLAGS_DISC_ENB
;
4310 if (tp
->tinfo
.disc_tag
& NCR_USR_TAGENB
)
4311 scsi
->flags
|= CTS_SCSI_FLAGS_TAG_ENB
;
4313 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
4316 spi
->sync_period
= tinfo
->period
;
4317 spi
->sync_offset
= tinfo
->offset
;
4318 spi
->bus_width
= tinfo
->width
;
4321 spi
->valid
= CTS_SPI_VALID_SYNC_RATE
4322 | CTS_SPI_VALID_SYNC_OFFSET
4323 | CTS_SPI_VALID_BUS_WIDTH
4324 | CTS_SPI_VALID_DISC
;
4325 scsi
->valid
= CTS_SCSI_VALID_TQ
;
4327 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
4331 case XPT_CALC_GEOMETRY
:
4333 struct ccb_calc_geometry
*ccg
;
4335 u_int32_t secs_per_cylinder
;
4338 /* XXX JGibbs - I'm sure the NCR uses a different strategy,
4339 * but it should be able to deal with Adaptec
4344 size_mb
= ccg
->volume_size
4345 / ((1024L * 1024L) / ccg
->block_size
);
4347 if (size_mb
> 1024 && extended
) {
4349 ccg
->secs_per_track
= 63;
4352 ccg
->secs_per_track
= 32;
4354 secs_per_cylinder
= ccg
->heads
* ccg
->secs_per_track
;
4355 ccg
->cylinders
= ccg
->volume_size
/ secs_per_cylinder
;
4356 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
4360 case XPT_RESET_BUS
: /* Reset the specified SCSI bus */
4362 OUTB (nc_scntl1
, CRST
);
4363 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
4364 DELAY(10000); /* Wait until our interrupt handler sees it */
4368 case XPT_TERM_IO
: /* Terminate the I/O process */
4370 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
4373 case XPT_PATH_INQ
: /* Path routing inquiry */
4375 struct ccb_pathinq
*cpi
= &ccb
->cpi
;
4377 cpi
->version_num
= 1; /* XXX??? */
4378 cpi
->hba_inquiry
= PI_SDTR_ABLE
|PI_TAG_ABLE
;
4379 if ((np
->features
& FE_WIDE
) != 0)
4380 cpi
->hba_inquiry
|= PI_WIDE_16
;
4381 cpi
->target_sprt
= 0;
4383 cpi
->hba_eng_cnt
= 0;
4384 cpi
->max_target
= (np
->features
& FE_WIDE
) ? 15 : 7;
4385 cpi
->max_lun
= MAX_LUN
- 1;
4386 cpi
->initiator_id
= np
->myaddr
;
4387 cpi
->bus_id
= cam_sim_bus(sim
);
4388 cpi
->base_transfer_speed
= 3300;
4389 strncpy(cpi
->sim_vid
, "FreeBSD", SIM_IDLEN
);
4390 strncpy(cpi
->hba_vid
, "Symbios", HBA_IDLEN
);
4391 strncpy(cpi
->dev_name
, cam_sim_name(sim
), DEV_IDLEN
);
4392 cpi
->unit_number
= cam_sim_unit(sim
);
4393 cpi
->transport
= XPORT_SPI
;
4394 cpi
->transport_version
= 2;
4395 cpi
->protocol
= PROTO_SCSI
;
4396 cpi
->protocol_version
= SCSI_REV_2
;
4397 cpi
->ccb_h
.status
= CAM_REQ_CMP
;
4402 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
4408 /*==========================================================
4411 ** Complete execution of a SCSI command.
4412 ** Signal completion to the generic SCSI driver.
4415 **==========================================================
4419 ncr_complete (ncb_p np
, nccb_p cp
)
4428 if (!cp
|| (cp
->magic
!=CCB_MAGIC
) || !cp
->ccb
) return;
4433 ** No Reselect anymore.
4435 cp
->jump_nccb
.l_cmd
= (SCR_JUMP
);
4440 cp
->phys
.header
.launch
.l_paddr
= NCB_SCRIPT_PHYS (np
, idle
);
4445 ncb_profile (np
, cp
);
4447 if (DEBUG_FLAGS
& DEBUG_TINY
)
4448 kprintf ("CCB=%x STAT=%x/%x\n", (int)(intptr_t)cp
& 0xfff,
4449 cp
->host_status
,cp
->s_status
);
4453 tp
= &np
->target
[ccb
->ccb_h
.target_id
];
4456 ** We do not queue more than 1 nccb per target
4457 ** with negotiation at any time. If this nccb was
4458 ** used for negotiation, clear this info in the tcb.
4461 if (cp
== tp
->nego_cp
)
4465 ** Check for parity errors.
4467 /* XXX JGibbs - What about reporting them??? */
4469 if (cp
->parity_status
) {
4471 kprintf ("%d parity error(s), fallback.\n", cp
->parity_status
);
4473 ** fallback to asynch transfer.
4475 tp
->tinfo
.goal
.period
= 0;
4476 tp
->tinfo
.goal
.offset
= 0;
4480 ** Check for extended errors.
4483 if (cp
->xerr_status
!= XE_OK
) {
4485 switch (cp
->xerr_status
) {
4487 kprintf ("extraneous data discarded.\n");
4490 kprintf ("illegal scsi phase (4/5).\n");
4493 kprintf ("extended error %d.\n", cp
->xerr_status
);
4496 if (cp
->host_status
==HS_COMPLETE
)
4497 cp
->host_status
= HS_FAIL
;
4501 ** Check the status.
4503 if (cp
->host_status
== HS_COMPLETE
) {
4505 if (cp
->s_status
== SCSI_STATUS_OK
) {
4510 /* XXX JGibbs - Properly calculate residual */
4512 tp
->bytes
+= ccb
->csio
.dxfer_len
;
4515 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
4516 } else if ((cp
->s_status
& SCSI_STATUS_SENSE
) != 0) {
4519 * XXX Could be TERMIO too. Should record
4522 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
4523 cp
->s_status
&= ~SCSI_STATUS_SENSE
;
4524 if (cp
->s_status
== SCSI_STATUS_OK
) {
4526 CAM_AUTOSNS_VALID
|CAM_SCSI_STATUS_ERROR
;
4528 ccb
->ccb_h
.status
= CAM_AUTOSENSE_FAIL
;
4531 ccb
->ccb_h
.status
= CAM_SCSI_STATUS_ERROR
;
4532 ccb
->csio
.scsi_status
= cp
->s_status
;
4536 } else if (cp
->host_status
== HS_SEL_TIMEOUT
) {
4539 ** Device failed selection
4541 ccb
->ccb_h
.status
= CAM_SEL_TIMEOUT
;
4543 } else if (cp
->host_status
== HS_TIMEOUT
) {
4548 ccb
->ccb_h
.status
= CAM_CMD_TIMEOUT
;
4549 } else if (cp
->host_status
== HS_STALL
) {
4550 ccb
->ccb_h
.status
= CAM_REQUEUE_REQ
;
4554 ** Other protocol messes
4557 kprintf ("COMMAND FAILED (%x %x) @%p.\n",
4558 cp
->host_status
, cp
->s_status
, cp
);
4560 ccb
->ccb_h
.status
= CAM_CMD_TIMEOUT
;
4563 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
4564 xpt_freeze_devq(ccb
->ccb_h
.path
, /*count*/1);
4565 ccb
->ccb_h
.status
|= CAM_DEV_QFRZN
;
4571 ncr_free_nccb (np
, cp
);
4574 ** signal completion to generic driver.
4579 /*==========================================================
4582 ** Signal all (or one) control block done.
4585 **==========================================================
4589 ncr_wakeup (ncb_p np
, u_long code
)
4592 ** Starting at the default nccb and following
4593 ** the links, complete all jobs with a
4594 ** host_status greater than "disconnect".
4596 ** If the "code" parameter is not zero,
4597 ** complete all jobs that are not IDLE.
4600 nccb_p cp
= np
->link_nccb
;
4602 switch (cp
->host_status
) {
4608 if(DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("D");
4614 cp
->host_status
= code
;
4619 ncr_complete (np
, cp
);
4622 cp
= cp
-> link_nccb
;
4627 ncr_freeze_devq (ncb_p np
, struct cam_path
*path
)
4634 ** Starting at the first nccb and following
4635 ** the links, complete all jobs that match
4636 ** the passed in path and are in the start queue.
4643 switch (cp
->host_status
) {
4647 if ((cp
->phys
.header
.launch
.l_paddr
4648 == NCB_SCRIPT_PHYS (np
, select
))
4649 && (xpt_path_comp(path
, cp
->ccb
->ccb_h
.path
) >= 0)) {
4651 /* Mark for removal from the start queue */
4652 for (i
= 1; i
< MAX_START
; i
++) {
4655 idx
= np
->squeueput
- i
;
4658 idx
= MAX_START
+ idx
;
4660 == CCB_PHYS(cp
, phys
)) {
4662 NCB_SCRIPT_PHYS (np
, skip
);
4668 cp
->host_status
=HS_STALL
;
4669 ncr_complete (np
, cp
);
4683 /* Compress the start queue */
4685 bidx
= np
->squeueput
;
4686 i
= np
->squeueput
- firstskip
;
4693 bidx
= MAX_START
+ bidx
;
4695 if (np
->squeue
[i
] == NCB_SCRIPT_PHYS (np
, skip
)) {
4697 } else if (j
!= 0) {
4698 np
->squeue
[bidx
] = np
->squeue
[i
];
4699 if (np
->squeue
[bidx
]
4700 == NCB_SCRIPT_PHYS(np
, idle
))
4703 i
= (i
+ 1) % MAX_START
;
4705 np
->squeueput
= bidx
;
4709 /*==========================================================
4715 **==========================================================
4719 ncr_init(ncb_p np
, char * msg
, u_long code
)
4727 OUTB (nc_istat
, SRST
);
4735 if (msg
) kprintf ("%s: restart (%s).\n", ncr_name (np
), msg
);
4738 ** Clear Start Queue
4741 for (i
=0;i
<MAX_START
;i
++)
4742 np
-> squeue
[i
] = NCB_SCRIPT_PHYS (np
, idle
);
4745 ** Start at first entry.
4749 WRITESCRIPT(startpos
[0], NCB_SCRIPTH_PHYS (np
, tryloop
));
4750 WRITESCRIPT(start0
[0], SCR_INT
^ IFFALSE (0));
4753 ** Wakeup all pending jobs.
4756 ncr_wakeup (np
, code
);
4762 OUTB (nc_istat
, 0x00 ); /* Remove Reset, abort ... */
4763 OUTB (nc_scntl0
, 0xca ); /* full arb., ena parity, par->ATN */
4764 OUTB (nc_scntl1
, 0x00 ); /* odd parity, and remove CRST!! */
4765 ncr_selectclock(np
, np
->rv_scntl3
); /* Select SCSI clock */
4766 OUTB (nc_scid
, RRE
|np
->myaddr
);/* host adapter SCSI address */
4767 OUTW (nc_respid
, 1ul<<np
->myaddr
);/* id to respond to */
4768 OUTB (nc_istat
, SIGP
); /* Signal Process */
4769 OUTB (nc_dmode
, np
->rv_dmode
); /* XXX modify burstlen ??? */
4770 OUTB (nc_dcntl
, np
->rv_dcntl
);
4771 OUTB (nc_ctest3
, np
->rv_ctest3
);
4772 OUTB (nc_ctest5
, np
->rv_ctest5
);
4773 OUTB (nc_ctest4
, np
->rv_ctest4
);/* enable master parity checking */
4774 OUTB (nc_stest2
, np
->rv_stest2
|EXT
); /* Extended Sreq/Sack filtering */
4775 OUTB (nc_stest3
, TE
); /* TolerANT enable */
4776 OUTB (nc_stime0
, 0x0b ); /* HTH = disabled, STO = 0.1 sec. */
4778 if (bootverbose
>= 2) {
4779 kprintf ("\tACTUAL values:SCNTL3:%02x DMODE:%02x DCNTL:%02x\n",
4780 np
->rv_scntl3
, np
->rv_dmode
, np
->rv_dcntl
);
4781 kprintf ("\t CTEST3:%02x CTEST4:%02x CTEST5:%02x\n",
4782 np
->rv_ctest3
, np
->rv_ctest4
, np
->rv_ctest5
);
4786 ** Enable GPIO0 pin for writing if LED support.
4789 if (np
->features
& FE_LED0
) {
4790 OUTOFFB (nc_gpcntl
, 0x01);
4794 ** Fill in target structure.
4796 for (i
=0;i
<MAX_TARGET
;i
++) {
4797 tcb_p tp
= &np
->target
[i
];
4800 tp
->tinfo
.wval
= np
->rv_scntl3
;
4802 tp
->tinfo
.current
.period
= 0;
4803 tp
->tinfo
.current
.offset
= 0;
4804 tp
->tinfo
.current
.width
= MSG_EXT_WDTR_BUS_8_BIT
;
4811 OUTW (nc_sien
, STO
|HTH
|MA
|SGE
|UDC
|RST
);
4812 OUTB (nc_dien
, MDPE
|BF
|ABRT
|SSI
|SIR
|IID
);
4815 ** Start script processor.
4818 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, start
));
4821 * Notify the XPT of the event
4823 if (code
== HS_RESET
)
4824 xpt_async(AC_BUS_RESET
, np
->path
, NULL
);
4828 ncr_poll(struct cam_sim
*sim
)
4830 ncr_intr(cam_sim_softc(sim
));
4834 /*==========================================================
4836 ** Get clock factor and sync divisor for a given
4837 ** synchronous factor period.
4838 ** Returns the clock factor (in sxfer) and scntl3
4839 ** synchronous divisor field.
4841 **==========================================================
4844 static void ncr_getsync(ncb_p np
, u_char sfac
, u_char
*fakp
, u_char
*scntl3p
)
4846 u_long clk
= np
->clock_khz
; /* SCSI clock frequency in kHz */
4847 int div
= np
->clock_divn
; /* Number of divisors supported */
4848 u_long fak
; /* Sync factor in sxfer */
4849 u_long per
; /* Period in tenths of ns */
4850 u_long kpc
; /* (per * clk) */
4853 ** Compute the synchronous period in tenths of nano-seconds
4855 if (sfac
<= 10) per
= 250;
4856 else if (sfac
== 11) per
= 303;
4857 else if (sfac
== 12) per
= 500;
4858 else per
= 40 * sfac
;
4861 ** Look for the greatest clock divisor that allows an
4862 ** input speed faster than the period.
4866 if (kpc
>= (div_10M
[div
] * 4)) break;
4869 ** Calculate the lowest clock factor that allows an output
4870 ** speed not faster than the period.
4872 fak
= (kpc
- 1) / div_10M
[div
] + 1;
4874 #if 0 /* You can #if 1 if you think this optimization is useful */
4876 per
= (fak
* div_10M
[div
]) / clk
;
4879 ** Why not to try the immediate lower divisor and to choose
4880 ** the one that allows the fastest output speed ?
4881 ** We dont want input speed too much greater than output speed.
4883 if (div
>= 1 && fak
< 6) {
4885 fak2
= (kpc
- 1) / div_10M
[div
-1] + 1;
4886 per2
= (fak2
* div_10M
[div
-1]) / clk
;
4887 if (per2
< per
&& fak2
<= 6) {
4895 if (fak
< 4) fak
= 4; /* Should never happen, too bad ... */
4898 ** Compute and return sync parameters for the ncr
4901 *scntl3p
= ((div
+1) << 4) + (sfac
< 25 ? 0x80 : 0);
4904 /*==========================================================
4906 ** Switch sync mode for current job and its target
4908 **==========================================================
4912 ncr_setsync(ncb_p np
, nccb_p cp
, u_char scntl3
, u_char sxfer
, u_char period
)
4915 struct ccb_trans_settings
*neg
;
4918 u_int target
= INB (nc_sdid
) & 0x0f;
4927 assert (target
== ccb
->ccb_h
.target_id
);
4929 tp
= &np
->target
[target
];
4931 if (!scntl3
|| !(sxfer
& 0x1f))
4932 scntl3
= np
->rv_scntl3
;
4933 scntl3
= (scntl3
& 0xf0) | (tp
->tinfo
.wval
& EWS
)
4934 | (np
->rv_scntl3
& 0x07);
4937 ** Deduce the value of controller sync period from scntl3.
4938 ** period is in tenths of nano-seconds.
4941 div
= ((scntl3
>> 4) & 0x7);
4942 if ((sxfer
& 0x1f) && div
)
4944 (((sxfer
>>5)+4)*div_10M
[div
-1])/np
->clock_khz
;
4948 tp
->tinfo
.goal
.period
= period
;
4949 tp
->tinfo
.goal
.offset
= sxfer
& 0x1f;
4950 tp
->tinfo
.current
.period
= period
;
4951 tp
->tinfo
.current
.offset
= sxfer
& 0x1f;
4954 ** Stop there if sync parameters are unchanged
4956 if (tp
->tinfo
.sval
== sxfer
&& tp
->tinfo
.wval
== scntl3
) return;
4957 tp
->tinfo
.sval
= sxfer
;
4958 tp
->tinfo
.wval
= scntl3
;
4962 ** Disable extended Sreq/Sack filtering
4964 if (period_10ns
<= 2000) OUTOFFB (nc_stest2
, EXT
);
4968 ** Tell the SCSI layer about the
4969 ** new transfer parameters.
4971 neg
= &xpt_alloc_ccb()->cts
;
4972 neg
->protocol
= PROTO_SCSI
;
4973 neg
->protocol_version
= SCSI_REV_2
;
4974 neg
->transport
= XPORT_SPI
;
4975 neg
->transport_version
= 2;
4976 neg
->xport_specific
.spi
.sync_period
= period
;
4977 neg
->xport_specific
.spi
.sync_offset
= sxfer
& 0x1f;
4978 neg
->xport_specific
.spi
.valid
= CTS_SPI_VALID_SYNC_RATE
4979 | CTS_SPI_VALID_SYNC_OFFSET
;
4980 xpt_setup_ccb(&neg
->ccb_h
, ccb
->ccb_h
.path
, /*priority*/1);
4981 xpt_async(AC_TRANSFER_NEG
, ccb
->ccb_h
.path
, neg
);
4984 ** set actual value and sync_status
4986 OUTB (nc_sxfer
, sxfer
);
4987 np
->sync_st
= sxfer
;
4988 OUTB (nc_scntl3
, scntl3
);
4989 np
->wide_st
= scntl3
;
4992 ** patch ALL nccbs of this target.
4994 for (cp
= np
->link_nccb
; cp
; cp
= cp
->link_nccb
) {
4995 if (!cp
->ccb
) continue;
4996 if (cp
->ccb
->ccb_h
.target_id
!= target
) continue;
4997 cp
->sync_status
= sxfer
;
4998 cp
->wide_status
= scntl3
;
5001 xpt_free_ccb(&neg
->ccb_h
);
5004 /*==========================================================
5006 ** Switch wide mode for current job and its target
5007 ** SCSI specs say: a SCSI device that accepts a WDTR
5008 ** message shall reset the synchronous agreement to
5009 ** asynchronous mode.
5011 **==========================================================
5014 static void ncr_setwide (ncb_p np
, nccb_p cp
, u_char wide
, u_char ack
)
5017 struct ccb_trans_settings
*neg
;
5018 u_int target
= INB (nc_sdid
) & 0x0f;
5029 assert (target
== ccb
->ccb_h
.target_id
);
5031 tp
= &np
->target
[target
];
5032 tp
->tinfo
.current
.width
= wide
;
5033 tp
->tinfo
.goal
.width
= wide
;
5034 tp
->tinfo
.current
.period
= 0;
5035 tp
->tinfo
.current
.offset
= 0;
5037 scntl3
= (tp
->tinfo
.wval
& (~EWS
)) | (wide
? EWS
: 0);
5039 sxfer
= ack
? 0 : tp
->tinfo
.sval
;
5042 ** Stop there if sync/wide parameters are unchanged
5044 if (tp
->tinfo
.sval
== sxfer
&& tp
->tinfo
.wval
== scntl3
) return;
5045 tp
->tinfo
.sval
= sxfer
;
5046 tp
->tinfo
.wval
= scntl3
;
5048 /* Tell the SCSI layer about the new transfer params */
5049 neg
= &xpt_alloc_ccb()->cts
;
5051 neg
->protocol
= PROTO_SCSI
;
5052 neg
->protocol_version
= SCSI_REV_2
;
5053 neg
->transport
= XPORT_SPI
;
5054 neg
->transport_version
= 2;
5055 neg
->xport_specific
.spi
.bus_width
= (scntl3
& EWS
) ?
5056 MSG_EXT_WDTR_BUS_16_BIT
: MSG_EXT_WDTR_BUS_8_BIT
;
5057 neg
->xport_specific
.spi
.sync_period
= 0;
5058 neg
->xport_specific
.spi
.sync_offset
= 0;
5059 neg
->xport_specific
.spi
.valid
= CTS_SPI_VALID_SYNC_RATE
5060 | CTS_SPI_VALID_SYNC_OFFSET
5061 | CTS_SPI_VALID_BUS_WIDTH
;
5062 xpt_setup_ccb(&neg
->ccb_h
, ccb
->ccb_h
.path
, /*priority*/1);
5063 xpt_async(AC_TRANSFER_NEG
, ccb
->ccb_h
.path
, neg
);
5066 ** set actual value and sync_status
5068 OUTB (nc_sxfer
, sxfer
);
5069 np
->sync_st
= sxfer
;
5070 OUTB (nc_scntl3
, scntl3
);
5071 np
->wide_st
= scntl3
;
5074 ** patch ALL nccbs of this target.
5076 for (cp
= np
->link_nccb
; cp
; cp
= cp
->link_nccb
) {
5077 if (!cp
->ccb
) continue;
5078 if (cp
->ccb
->ccb_h
.target_id
!= target
) continue;
5079 cp
->sync_status
= sxfer
;
5080 cp
->wide_status
= scntl3
;
5083 xpt_free_ccb(&neg
->ccb_h
);
5086 /*==========================================================
5089 ** ncr timeout handler.
5092 **==========================================================
5094 ** Misused to keep the driver running when
5095 ** interrupts are not configured correctly.
5097 **----------------------------------------------------------
5101 ncr_timeout (void *arg
)
5104 time_t thistime
= time_uptime
;
5105 ticks_t step
= np
->ticks
;
5109 if (np
->lasttime
!= thistime
) {
5111 ** block ncr interrupts
5114 np
->lasttime
= thistime
;
5116 /*----------------------------------------------------
5118 ** handle ncr chip timeouts
5121 ** We have a chance to arbitrate for the
5122 ** SCSI bus at least every 10 seconds.
5124 **----------------------------------------------------
5127 t
= thistime
- np
->heartbeat
;
5129 if (t
<2) np
->latetime
=0; else np
->latetime
++;
5131 if (np
->latetime
>2) {
5133 ** If there are no requests, the script
5134 ** processor will sleep on SEL_WAIT_RESEL.
5135 ** But we have to check whether it died.
5136 ** Let's try to wake it up.
5138 OUTB (nc_istat
, SIGP
);
5141 /*----------------------------------------------------
5143 ** handle nccb timeouts
5145 **----------------------------------------------------
5148 for (cp
=np
->link_nccb
; cp
; cp
=cp
->link_nccb
) {
5150 ** look for timed out nccbs.
5152 if (!cp
->host_status
) continue;
5153 if (cp
->tlimit
> thistime
) continue;
5156 ** Disable reselect.
5157 ** Remove it from startqueue.
5159 cp
->jump_nccb
.l_cmd
= (SCR_JUMP
);
5160 if (cp
->phys
.header
.launch
.l_paddr
==
5161 NCB_SCRIPT_PHYS (np
, select
)) {
5162 kprintf ("%s: timeout nccb=%p (skip)\n",
5164 cp
->phys
.header
.launch
.l_paddr
5165 = NCB_SCRIPT_PHYS (np
, skip
);
5168 switch (cp
->host_status
) {
5174 cp
->host_status
=HS_TIMEOUT
;
5179 ** wakeup this nccb.
5181 ncr_complete (np
, cp
);
5186 callout_reset(&np
->timeout_ch
, step
? step
: 1, ncr_timeout
, np
);
5188 if (INB(nc_istat
) & (INTF
|SIP
|DIP
)) {
5191 ** Process pending interrupts.
5195 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("{");
5197 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("}");
5202 /*==========================================================
5204 ** log message for real hard errors
5206 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
5207 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
5209 ** exception register:
5214 ** so: control lines as driver by NCR.
5215 ** si: control lines as seen by NCR.
5216 ** sd: scsi data lines as seen by NCR.
5219 ** sxfer: (see the manual)
5220 ** scntl3: (see the manual)
5222 ** current script command:
5223 ** dsp: script adress (relative to start of script).
5224 ** dbc: first word of script command.
5226 ** First 16 register of the chip:
5229 **==========================================================
5232 static void ncr_log_hard_error(ncb_p np
, u_short sist
, u_char dstat
)
5238 u_char
*script_base
;
5243 if (np
->p_script
< dsp
&&
5244 dsp
<= np
->p_script
+ sizeof(struct script
)) {
5245 script_ofs
= dsp
- np
->p_script
;
5246 script_size
= sizeof(struct script
);
5247 script_base
= (u_char
*) np
->script
;
5248 script_name
= "script";
5250 else if (np
->p_scripth
< dsp
&&
5251 dsp
<= np
->p_scripth
+ sizeof(struct scripth
)) {
5252 script_ofs
= dsp
- np
->p_scripth
;
5253 script_size
= sizeof(struct scripth
);
5254 script_base
= (u_char
*) np
->scripth
;
5255 script_name
= "scripth";
5260 script_name
= "mem";
5263 kprintf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
5264 ncr_name (np
), (unsigned)INB (nc_sdid
)&0x0f, dstat
, sist
,
5265 (unsigned)INB (nc_socl
), (unsigned)INB (nc_sbcl
), (unsigned)INB (nc_sbdl
),
5266 (unsigned)INB (nc_sxfer
),(unsigned)INB (nc_scntl3
), script_name
, script_ofs
,
5267 (unsigned)INL (nc_dbc
));
5269 if (((script_ofs
& 3) == 0) &&
5270 (unsigned)script_ofs
< script_size
) {
5271 kprintf ("%s: script cmd = %08x\n", ncr_name(np
),
5272 (int)READSCRIPT_OFF(script_base
, script_ofs
));
5275 kprintf ("%s: regdump:", ncr_name(np
));
5277 kprintf (" %02x", (unsigned)INB_OFF(i
));
5281 /*==========================================================
5284 ** ncr chip exception handler.
5287 **==========================================================
5290 static void ncr_exception (ncb_p np
)
5292 u_char istat
, dstat
;
5296 ** interrupt on the fly ?
5298 while ((istat
= INB (nc_istat
)) & INTF
) {
5299 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("F ");
5300 OUTB (nc_istat
, INTF
);
5301 np
->profile
.num_fly
++;
5304 if (!(istat
& (SIP
|DIP
))) {
5309 ** Steinbach's Guideline for Systems Programming:
5310 ** Never test for an error condition you don't know how to handle.
5313 sist
= (istat
& SIP
) ? INW (nc_sist
) : 0;
5314 dstat
= (istat
& DIP
) ? INB (nc_dstat
) : 0;
5315 np
->profile
.num_int
++;
5317 if (DEBUG_FLAGS
& DEBUG_TINY
)
5318 kprintf ("<%d|%x:%x|%x:%x>",
5321 (unsigned)INL(nc_dsp
),
5322 (unsigned)INL(nc_dbc
));
5323 if ((dstat
==DFE
) && (sist
==PAR
)) return;
5325 /*==========================================================
5327 ** First the normal cases.
5329 **==========================================================
5331 /*-------------------------------------------
5333 **-------------------------------------------
5337 ncr_init (np
, bootverbose
? "scsi reset" : NULL
, HS_RESET
);
5341 /*-------------------------------------------
5342 ** selection timeout
5344 ** IID excluded from dstat mask!
5346 **-------------------------------------------
5350 !(sist
& (GEN
|HTH
|MA
|SGE
|UDC
|RST
|PAR
)) &&
5351 !(dstat
& (MDPE
|BF
|ABRT
|SIR
))) {
5356 /*-------------------------------------------
5358 **-------------------------------------------
5362 !(sist
& (STO
|GEN
|HTH
|SGE
|UDC
|RST
|PAR
)) &&
5363 !(dstat
& (MDPE
|BF
|ABRT
|SIR
|IID
))) {
5364 ncr_int_ma (np
, dstat
);
5368 /*----------------------------------------
5369 ** move command with length 0
5370 **----------------------------------------
5373 if ((dstat
& IID
) &&
5374 !(sist
& (STO
|GEN
|HTH
|MA
|SGE
|UDC
|RST
|PAR
)) &&
5375 !(dstat
& (MDPE
|BF
|ABRT
|SIR
)) &&
5376 ((INL(nc_dbc
) & 0xf8000000) == SCR_MOVE_TBL
)) {
5378 ** Target wants more data than available.
5379 ** The "no_data" script will do it.
5381 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, no_data
));
5385 /*-------------------------------------------
5386 ** Programmed interrupt
5387 **-------------------------------------------
5390 if ((dstat
& SIR
) &&
5391 !(sist
& (STO
|GEN
|HTH
|MA
|SGE
|UDC
|RST
|PAR
)) &&
5392 !(dstat
& (MDPE
|BF
|ABRT
|IID
)) &&
5393 (INB(nc_dsps
) <= SIR_MAX
)) {
5398 /*========================================
5399 ** log message for real hard errors
5400 **========================================
5403 ncr_log_hard_error(np
, sist
, dstat
);
5405 /*========================================
5406 ** do the register dump
5407 **========================================
5410 if (time_uptime
- np
->regtime
> 10) {
5412 np
->regtime
= time_uptime
;
5413 for (i
=0; i
<sizeof(np
->regdump
); i
++)
5414 ((volatile char*)&np
->regdump
)[i
] = INB_OFF(i
);
5415 np
->regdump
.nc_dstat
= dstat
;
5416 np
->regdump
.nc_sist
= sist
;
5420 /*----------------------------------------
5421 ** clean up the dma fifo
5422 **----------------------------------------
5425 if ( (INB(nc_sstat0
) & (ILF
|ORF
|OLF
) ) ||
5426 (INB(nc_sstat1
) & (FF3210
) ) ||
5427 (INB(nc_sstat2
) & (ILF1
|ORF1
|OLF1
)) || /* wide .. */
5429 kprintf ("%s: have to clear fifos.\n", ncr_name (np
));
5430 OUTB (nc_stest3
, TE
|CSF
); /* clear scsi fifo */
5431 OUTB (nc_ctest3
, np
->rv_ctest3
| CLF
);
5432 /* clear dma fifo */
5435 /*----------------------------------------
5436 ** handshake timeout
5437 **----------------------------------------
5441 kprintf ("%s: handshake timeout\n", ncr_name(np
));
5442 OUTB (nc_scntl1
, CRST
);
5444 OUTB (nc_scntl1
, 0x00);
5445 OUTB (nc_scr0
, HS_FAIL
);
5446 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, cleanup
));
5450 /*----------------------------------------
5451 ** unexpected disconnect
5452 **----------------------------------------
5456 !(sist
& (STO
|GEN
|HTH
|MA
|SGE
|RST
|PAR
)) &&
5457 !(dstat
& (MDPE
|BF
|ABRT
|SIR
|IID
))) {
5458 OUTB (nc_scr0
, HS_UNEXPECTED
);
5459 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, cleanup
));
5463 /*----------------------------------------
5464 ** cannot disconnect
5465 **----------------------------------------
5468 if ((dstat
& IID
) &&
5469 !(sist
& (STO
|GEN
|HTH
|MA
|SGE
|UDC
|RST
|PAR
)) &&
5470 !(dstat
& (MDPE
|BF
|ABRT
|SIR
)) &&
5471 ((INL(nc_dbc
) & 0xf8000000) == SCR_WAIT_DISC
)) {
5473 ** Unexpected data cycle while waiting for disconnect.
5475 if (INB(nc_sstat2
) & LDSC
) {
5477 ** It's an early reconnect.
5478 ** Let's continue ...
5480 OUTB (nc_dcntl
, np
->rv_dcntl
| STD
);
5484 kprintf ("%s: INFO: LDSC while IID.\n",
5488 kprintf ("%s: target %d doesn't release the bus.\n",
5489 ncr_name (np
), INB (nc_sdid
)&0x0f);
5491 ** return without restarting the NCR.
5492 ** timeout will do the real work.
5497 /*----------------------------------------
5499 **----------------------------------------
5502 if ((dstat
& SSI
) &&
5503 !(sist
& (STO
|GEN
|HTH
|MA
|SGE
|UDC
|RST
|PAR
)) &&
5504 !(dstat
& (MDPE
|BF
|ABRT
|SIR
|IID
))) {
5505 OUTB (nc_dcntl
, np
->rv_dcntl
| STD
);
5510 ** @RECOVER@ HTH, SGE, ABRT.
5512 ** We should try to recover from these interrupts.
5513 ** They may occur if there are problems with synch transfers, or
5514 ** if targets are switched on or off while the driver is running.
5518 /* clear scsi offsets */
5519 OUTB (nc_ctest3
, np
->rv_ctest3
| CLF
);
5523 ** Freeze controller to be able to read the messages.
5526 if (DEBUG_FLAGS
& DEBUG_FREEZE
) {
5529 for (i
=0; i
<0x60; i
++) {
5533 kprintf ("%s: reg[%d0]: ",
5542 val
= bus_space_read_1(np
->bst
, np
->bsh
, i
);
5543 kprintf (" %x%x", val
/16, val
%16);
5544 if (i
%16==15) kprintf (".\n");
5547 callout_stop(&np
->timeout_ch
);
5549 kprintf ("%s: halted!\n", ncr_name(np
));
5551 ** don't restart controller ...
5553 OUTB (nc_istat
, SRST
);
5559 ** Freeze system to be able to read the messages.
5561 kprintf ("ncr: fatal error: system halted - press reset to reboot ...");
5567 ** sorry, have to kill ALL jobs ...
5570 ncr_init (np
, "fatal error", HS_FAIL
);
5573 /*==========================================================
5575 ** ncr chip exception handler for selection timeout
5577 **==========================================================
5579 ** There seems to be a bug in the 53c810.
5580 ** Although a STO-Interrupt is pending,
5581 ** it continues executing script commands.
5582 ** But it will fail and interrupt (IID) on
5583 ** the next instruction where it's looking
5584 ** for a valid phase.
5586 **----------------------------------------------------------
5589 static void ncr_int_sto (ncb_p np
)
5591 u_long dsa
, scratcha
, diff
;
5593 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("T");
5596 ** look for nccb and set the status.
5601 while (cp
&& (CCB_PHYS (cp
, phys
) != dsa
))
5605 cp
-> host_status
= HS_SEL_TIMEOUT
;
5606 ncr_complete (np
, cp
);
5610 ** repair start queue
5613 scratcha
= INL (nc_scratcha
);
5614 diff
= scratcha
- NCB_SCRIPTH_PHYS (np
, tryloop
);
5616 /* assert ((diff <= MAX_START * 20) && !(diff % 20));*/
5618 if ((diff
<= MAX_START
* 20) && !(diff
% 20)) {
5619 WRITESCRIPT(startpos
[0], scratcha
);
5620 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, start
));
5623 ncr_init (np
, "selection timeout", HS_FAIL
);
5626 /*==========================================================
5629 ** ncr chip exception handler for phase errors.
5632 **==========================================================
5634 ** We have to construct a new transfer descriptor,
5635 ** to transfer the rest of the current block.
5637 **----------------------------------------------------------
5640 static void ncr_int_ma (ncb_p np
, u_char dstat
)
5647 volatile void *vdsp_base
;
5649 u_int32_t oadr
, olen
;
5650 u_int32_t
*tblp
, *newcmd
;
5651 u_char cmd
, sbcl
, ss0
, ss2
, ctest5
;
5658 ss0
= INB (nc_sstat0
);
5659 ss2
= INB (nc_sstat2
);
5660 sbcl
= INB (nc_sbcl
);
5663 rest
= dbc
& 0xffffff;
5665 ctest5
= (np
->rv_ctest5
& DFS
) ? INB (nc_ctest5
) : 0;
5667 delta
=(((ctest5
<<8) | (INB (nc_dfifo
) & 0xff)) - rest
) & 0x3ff;
5669 delta
=(INB (nc_dfifo
) - rest
) & 0x7f;
5673 ** The data in the dma fifo has not been transfered to
5674 ** the target -> add the amount to the rest
5675 ** and clear the data.
5676 ** Check the sstat2 register in case of wide transfer.
5679 if (!(dstat
& DFE
)) rest
+= delta
;
5680 if (ss0
& OLF
) rest
++;
5681 if (ss0
& ORF
) rest
++;
5682 if (INB(nc_scntl3
) & EWS
) {
5683 if (ss2
& OLF1
) rest
++;
5684 if (ss2
& ORF1
) rest
++;
5686 OUTB (nc_ctest3
, np
->rv_ctest3
| CLF
); /* clear dma fifo */
5687 OUTB (nc_stest3
, TE
|CSF
); /* clear scsi fifo */
5690 ** locate matching cp
5693 while (cp
&& (CCB_PHYS (cp
, phys
) != dsa
))
5697 kprintf ("%s: SCSI phase error fixup: CCB already dequeued (%p)\n",
5698 ncr_name (np
), (void *) np
->header
.cp
);
5701 if (cp
!= np
->header
.cp
) {
5702 kprintf ("%s: SCSI phase error fixup: CCB address mismatch "
5703 "(%p != %p) np->nccb = %p\n",
5704 ncr_name (np
), (void *)cp
, (void *)np
->header
.cp
,
5705 (void *)np
->link_nccb
);
5710 ** find the interrupted script command,
5711 ** and the address at which to continue.
5714 if (dsp
== vtophys (&cp
->patch
[2])) {
5716 vdsp_off
= offsetof(struct nccb
, patch
[0]);
5717 nxtdsp
= READSCRIPT_OFF(vdsp_base
, vdsp_off
+ 3*4);
5718 } else if (dsp
== vtophys (&cp
->patch
[6])) {
5720 vdsp_off
= offsetof(struct nccb
, patch
[4]);
5721 nxtdsp
= READSCRIPT_OFF(vdsp_base
, vdsp_off
+ 3*4);
5722 } else if (dsp
> np
->p_script
&&
5723 dsp
<= np
->p_script
+ sizeof(struct script
)) {
5724 vdsp_base
= np
->script
;
5725 vdsp_off
= dsp
- np
->p_script
- 8;
5728 vdsp_base
= np
->scripth
;
5729 vdsp_off
= dsp
- np
->p_scripth
- 8;
5734 ** log the information
5736 if (DEBUG_FLAGS
& (DEBUG_TINY
|DEBUG_PHASE
)) {
5737 kprintf ("P%x%x ",cmd
&7, sbcl
&7);
5738 kprintf ("RL=%d D=%d SS0=%x ",
5739 (unsigned) rest
, (unsigned) delta
, ss0
);
5741 if (DEBUG_FLAGS
& DEBUG_PHASE
) {
5742 kprintf ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
5745 nxtdsp
, (volatile char*)vdsp_base
+vdsp_off
, cmd
);
5749 ** get old startaddress and old length.
5752 oadr
= READSCRIPT_OFF(vdsp_base
, vdsp_off
+ 1*4);
5754 if (cmd
& 0x10) { /* Table indirect */
5755 tblp
= (u_int32_t
*) ((char*) &cp
->phys
+ oadr
);
5760 olen
= READSCRIPT_OFF(vdsp_base
, vdsp_off
) & 0xffffff;
5763 if (DEBUG_FLAGS
& DEBUG_PHASE
) {
5764 kprintf ("OCMD=%x\nTBLP=%p OLEN=%lx OADR=%lx\n",
5765 (unsigned) (READSCRIPT_OFF(vdsp_base
, vdsp_off
) >> 24),
5772 ** if old phase not dataphase, leave here.
5775 if (cmd
!= (READSCRIPT_OFF(vdsp_base
, vdsp_off
) >> 24)) {
5776 PRINT_ADDR(cp
->ccb
);
5777 kprintf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
5779 (unsigned)READSCRIPT_OFF(vdsp_base
, vdsp_off
) >> 24);
5784 PRINT_ADDR(cp
->ccb
);
5785 kprintf ("phase change %x-%x %d@%08x resid=%d.\n",
5786 cmd
&7, sbcl
&7, (unsigned)olen
,
5787 (unsigned)oadr
, (unsigned)rest
);
5789 OUTB (nc_dcntl
, np
->rv_dcntl
| STD
);
5794 ** choose the correct patch area.
5795 ** if savep points to one, choose the other.
5799 if (cp
->phys
.header
.savep
== vtophys (newcmd
)) newcmd
+=4;
5802 ** fillin the commands
5805 newcmd
[0] = ((cmd
& 0x0f) << 24) | rest
;
5806 newcmd
[1] = oadr
+ olen
- rest
;
5807 newcmd
[2] = SCR_JUMP
;
5810 if (DEBUG_FLAGS
& DEBUG_PHASE
) {
5811 PRINT_ADDR(cp
->ccb
);
5812 kprintf ("newcmd[%d] %x %x %x %x.\n",
5813 (int)(newcmd
- cp
->patch
),
5814 (unsigned)newcmd
[0],
5815 (unsigned)newcmd
[1],
5816 (unsigned)newcmd
[2],
5817 (unsigned)newcmd
[3]);
5820 ** fake the return address (to the patch).
5821 ** and restart script processor at dispatcher.
5823 np
->profile
.num_break
++;
5824 OUTL (nc_temp
, vtophys (newcmd
));
5826 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, dispatch
));
5828 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, checkatn
));
5831 /*==========================================================
5834 ** ncr chip exception handler for programmed interrupts.
5837 **==========================================================
5840 static int ncr_show_msg (u_char
* msg
)
5843 kprintf ("%x",*msg
);
5844 if (*msg
==MSG_EXTENDED
) {
5846 if (i
-1>msg
[1]) break;
5847 kprintf ("-%x",msg
[i
]);
5850 } else if ((*msg
& 0xf0) == 0x20) {
5851 kprintf ("-%x",msg
[1]);
5857 static void ncr_int_sir (ncb_p np
)
5860 u_char chg
, ofs
, per
, fak
, wide
;
5861 u_char num
= INB (nc_dsps
);
5864 u_int target
= INB (nc_sdid
) & 0x0f;
5865 tcb_p tp
= &np
->target
[target
];
5867 if (DEBUG_FLAGS
& DEBUG_TINY
) kprintf ("I#%d", num
);
5870 case SIR_SENSE_RESTART
:
5871 case SIR_STALL_RESTART
:
5880 while (cp
&& (CCB_PHYS (cp
, phys
) != dsa
))
5886 assert (cp
== np
->header
.cp
);
5887 if (cp
!= np
->header
.cp
)
5893 /*--------------------------------------------------------------------
5895 ** Processing of interrupted getcc selects
5897 **--------------------------------------------------------------------
5900 case SIR_SENSE_RESTART
:
5901 /*------------------------------------------
5902 ** Script processor is idle.
5903 ** Look for interrupted "check cond"
5904 **------------------------------------------
5907 if (DEBUG_FLAGS
& DEBUG_RESTART
)
5908 kprintf ("%s: int#%d",ncr_name (np
),num
);
5910 for (i
=0; i
<MAX_TARGET
; i
++) {
5911 if (DEBUG_FLAGS
& DEBUG_RESTART
) kprintf (" t%d", i
);
5912 tp
= &np
->target
[i
];
5913 if (DEBUG_FLAGS
& DEBUG_RESTART
) kprintf ("+");
5916 if (DEBUG_FLAGS
& DEBUG_RESTART
) kprintf ("+");
5917 if ((cp
->host_status
==HS_BUSY
) &&
5918 (cp
->s_status
==SCSI_STATUS_CHECK_COND
))
5920 if (DEBUG_FLAGS
& DEBUG_RESTART
) kprintf ("- (remove)");
5921 tp
->hold_cp
= cp
= (nccb_p
) 0;
5925 if (DEBUG_FLAGS
& DEBUG_RESTART
)
5926 kprintf ("+ restart job ..\n");
5927 OUTL (nc_dsa
, CCB_PHYS (cp
, phys
));
5928 OUTL (nc_dsp
, NCB_SCRIPTH_PHYS (np
, getcc
));
5933 ** no job, resume normal processing
5935 if (DEBUG_FLAGS
& DEBUG_RESTART
) kprintf (" -- remove trap\n");
5936 WRITESCRIPT(start0
[0], SCR_INT
^ IFFALSE (0));
5939 case SIR_SENSE_FAILED
:
5940 /*-------------------------------------------
5941 ** While trying to select for
5942 ** getting the condition code,
5943 ** a target reselected us.
5944 **-------------------------------------------
5946 if (DEBUG_FLAGS
& DEBUG_RESTART
) {
5947 PRINT_ADDR(cp
->ccb
);
5948 kprintf ("in getcc reselect by t%d.\n",
5949 INB(nc_ssid
) & 0x0f);
5955 cp
->host_status
= HS_BUSY
;
5956 cp
->s_status
= SCSI_STATUS_CHECK_COND
;
5957 np
->target
[cp
->ccb
->ccb_h
.target_id
].hold_cp
= cp
;
5960 ** And patch code to restart it.
5962 WRITESCRIPT(start0
[0], SCR_INT
);
5965 /*-----------------------------------------------------------------------------
5967 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
5969 ** We try to negotiate sync and wide transfer only after
5970 ** a successfull inquire command. We look at byte 7 of the
5971 ** inquire data to determine the capabilities if the target.
5973 ** When we try to negotiate, we append the negotiation message
5974 ** to the identify and (maybe) simple tag message.
5975 ** The host status field is set to HS_NEGOTIATE to mark this
5978 ** If the target doesn't answer this message immidiately
5979 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
5980 ** will be raised eventually.
5981 ** The handler removes the HS_NEGOTIATE status, and sets the
5982 ** negotiated value to the default (async / nowide).
5984 ** If we receive a matching answer immediately, we check it
5985 ** for validity, and set the values.
5987 ** If we receive a Reject message immediately, we assume the
5988 ** negotiation has failed, and fall back to standard values.
5990 ** If we receive a negotiation message while not in HS_NEGOTIATE
5991 ** state, it's a target initiated negotiation. We prepare a
5992 ** (hopefully) valid answer, set our parameters, and send back
5993 ** this answer to the target.
5995 ** If the target doesn't fetch the answer (no message out phase),
5996 ** we assume the negotiation has failed, and fall back to default
5999 ** When we set the values, we adjust them in all nccbs belonging
6000 ** to this target, in the controller's register, and in the "phys"
6001 ** field of the controller's struct ncb.
6003 ** Possible cases: hs sir msg_in value send goto
6004 ** We try try to negotiate:
6005 ** -> target doesnt't msgin NEG FAIL noop defa. - dispatch
6006 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
6007 ** -> target answered (ok) NEG SYNC sdtr set - clrack
6008 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
6009 ** -> target answered (ok) NEG WIDE wdtr set - clrack
6010 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
6011 ** -> any other msgin NEG FAIL noop defa. - dispatch
6013 ** Target tries to negotiate:
6014 ** -> incoming message --- SYNC sdtr set SDTR -
6015 ** -> incoming message --- WIDE wdtr set WDTR -
6016 ** We sent our answer:
6017 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
6019 **-----------------------------------------------------------------------------
6022 case SIR_NEGO_FAILED
:
6023 /*-------------------------------------------------------
6025 ** Negotiation failed.
6026 ** Target doesn't send an answer message,
6027 ** or target rejected our message.
6029 ** Remove negotiation request.
6031 **-------------------------------------------------------
6033 OUTB (HS_PRT
, HS_BUSY
);
6037 case SIR_NEGO_PROTO
:
6038 /*-------------------------------------------------------
6040 ** Negotiation failed.
6041 ** Target doesn't fetch the answer message.
6043 **-------------------------------------------------------
6046 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6047 PRINT_ADDR(cp
->ccb
);
6048 kprintf ("negotiation failed sir=%x status=%x.\n",
6049 num
, cp
->nego_status
);
6053 ** any error in negotiation:
6054 ** fall back to default mode.
6056 switch (cp
->nego_status
) {
6059 ncr_setsync (np
, cp
, 0, 0xe0, 0);
6063 ncr_setwide (np
, cp
, 0, 0);
6067 np
->msgin
[0] = MSG_NOOP
;
6068 np
->msgout
[0] = MSG_NOOP
;
6069 cp
->nego_status
= 0;
6070 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, dispatch
));
6075 ** Synchronous request message received.
6078 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6079 PRINT_ADDR(cp
->ccb
);
6080 kprintf ("sync msgin: ");
6081 (void) ncr_show_msg (np
->msgin
);
6086 ** get requested values.
6092 if (ofs
==0) per
=255;
6095 ** check values against driver limits.
6097 if (per
< np
->minsync
)
6098 {chg
= 1; per
= np
->minsync
;}
6099 if (per
< tp
->tinfo
.user
.period
)
6100 {chg
= 1; per
= tp
->tinfo
.user
.period
;}
6101 if (ofs
> tp
->tinfo
.user
.offset
)
6102 {chg
= 1; ofs
= tp
->tinfo
.user
.offset
;}
6105 ** Check against controller limits.
6111 ncr_getsync(np
, per
, &fak
, &scntl3
);
6123 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6124 PRINT_ADDR(cp
->ccb
);
6125 kprintf ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
6126 per
, scntl3
, ofs
, fak
, chg
);
6129 if (INB (HS_PRT
) == HS_NEGOTIATE
) {
6130 OUTB (HS_PRT
, HS_BUSY
);
6131 switch (cp
->nego_status
) {
6135 ** This was an answer message
6139 ** Answer wasn't acceptable.
6141 ncr_setsync (np
, cp
, 0, 0xe0, 0);
6142 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, msg_bad
));
6147 ncr_setsync (np
,cp
,scntl3
,(fak
<<5)|ofs
, per
);
6148 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, clrack
));
6153 ncr_setwide (np
, cp
, 0, 0);
6159 ** It was a request. Set value and
6160 ** prepare an answer message
6163 ncr_setsync (np
, cp
, scntl3
, (fak
<<5)|ofs
, per
);
6165 np
->msgout
[0] = MSG_EXTENDED
;
6167 np
->msgout
[2] = MSG_EXT_SDTR
;
6168 np
->msgout
[3] = per
;
6169 np
->msgout
[4] = ofs
;
6171 cp
->nego_status
= NS_SYNC
;
6173 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6174 PRINT_ADDR(cp
->ccb
);
6175 kprintf ("sync msgout: ");
6176 (void) ncr_show_msg (np
->msgout
);
6181 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, msg_bad
));
6184 np
->msgin
[0] = MSG_NOOP
;
6190 ** Wide request message received.
6192 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6193 PRINT_ADDR(cp
->ccb
);
6194 kprintf ("wide msgin: ");
6195 (void) ncr_show_msg (np
->msgin
);
6200 ** get requested values.
6204 wide
= np
->msgin
[3];
6207 ** check values against driver limits.
6210 if (wide
> tp
->tinfo
.user
.width
)
6211 {chg
= 1; wide
= tp
->tinfo
.user
.width
;}
6213 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6214 PRINT_ADDR(cp
->ccb
);
6215 kprintf ("wide: wide=%d chg=%d.\n", wide
, chg
);
6218 if (INB (HS_PRT
) == HS_NEGOTIATE
) {
6219 OUTB (HS_PRT
, HS_BUSY
);
6220 switch (cp
->nego_status
) {
6224 ** This was an answer message
6228 ** Answer wasn't acceptable.
6230 ncr_setwide (np
, cp
, 0, 1);
6231 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, msg_bad
));
6236 ncr_setwide (np
, cp
, wide
, 1);
6237 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, clrack
));
6242 ncr_setsync (np
, cp
, 0, 0xe0, 0);
6248 ** It was a request, set value and
6249 ** prepare an answer message
6252 ncr_setwide (np
, cp
, wide
, 1);
6254 np
->msgout
[0] = MSG_EXTENDED
;
6256 np
->msgout
[2] = MSG_EXT_WDTR
;
6257 np
->msgout
[3] = wide
;
6259 np
->msgin
[0] = MSG_NOOP
;
6261 cp
->nego_status
= NS_WIDE
;
6263 if (DEBUG_FLAGS
& DEBUG_NEGO
) {
6264 PRINT_ADDR(cp
->ccb
);
6265 kprintf ("wide msgout: ");
6266 (void) ncr_show_msg (np
->msgout
);
6271 /*--------------------------------------------------------------------
6273 ** Processing of special messages
6275 **--------------------------------------------------------------------
6278 case SIR_REJECT_RECEIVED
:
6279 /*-----------------------------------------------
6281 ** We received a MSG_MESSAGE_REJECT message.
6283 **-----------------------------------------------
6286 PRINT_ADDR(cp
->ccb
);
6287 kprintf ("MSG_MESSAGE_REJECT received (%x:%x).\n",
6288 (unsigned)np
->lastmsg
, np
->msgout
[0]);
6291 case SIR_REJECT_SENT
:
6292 /*-----------------------------------------------
6294 ** We received an unknown message
6296 **-----------------------------------------------
6299 PRINT_ADDR(cp
->ccb
);
6300 kprintf ("MSG_MESSAGE_REJECT sent for ");
6301 (void) ncr_show_msg (np
->msgin
);
6305 /*--------------------------------------------------------------------
6307 ** Processing of special messages
6309 **--------------------------------------------------------------------
6312 case SIR_IGN_RESIDUE
:
6313 /*-----------------------------------------------
6315 ** We received an IGNORE RESIDUE message,
6316 ** which couldn't be handled by the script.
6318 **-----------------------------------------------
6321 PRINT_ADDR(cp
->ccb
);
6322 kprintf ("MSG_IGN_WIDE_RESIDUE received, but not yet implemented.\n");
6325 case SIR_MISSING_SAVE
:
6326 /*-----------------------------------------------
6328 ** We received an DISCONNECT message,
6329 ** but the datapointer wasn't saved before.
6331 **-----------------------------------------------
6334 PRINT_ADDR(cp
->ccb
);
6335 kprintf ("MSG_DISCONNECT received, but datapointer not saved:\n"
6336 "\tdata=%x save=%x goal=%x.\n",
6337 (unsigned) INL (nc_temp
),
6338 (unsigned) np
->header
.savep
,
6339 (unsigned) np
->header
.goalp
);
6342 /*--------------------------------------------------------------------
6344 ** Processing of a "SCSI_STATUS_QUEUE_FULL" status.
6346 ** XXX JGibbs - We should do the same thing for BUSY status.
6348 ** The current command has been rejected,
6349 ** because there are too many in the command queue.
6350 ** We have started too many commands for that target.
6352 **--------------------------------------------------------------------
6354 case SIR_STALL_QUEUE
:
6355 cp
->xerr_status
= XE_OK
;
6356 cp
->host_status
= HS_COMPLETE
;
6357 cp
->s_status
= SCSI_STATUS_QUEUE_FULL
;
6358 ncr_freeze_devq(np
, cp
->ccb
->ccb_h
.path
);
6359 ncr_complete(np
, cp
);
6363 case SIR_STALL_RESTART
:
6364 /*-----------------------------------------------
6366 ** Enable selecting again,
6367 ** if NO disconnected jobs.
6369 **-----------------------------------------------
6372 ** Look for a disconnected job.
6375 while (cp
&& cp
->host_status
!= HS_DISCONNECT
)
6379 ** if there is one, ...
6383 ** wait for reselection
6385 OUTL (nc_dsp
, NCB_SCRIPT_PHYS (np
, reselect
));
6390 ** else remove the interrupt.
6393 kprintf ("%s: queue empty.\n", ncr_name (np
));
6394 WRITESCRIPT(start1
[0], SCR_INT
^ IFFALSE (0));
6399 OUTB (nc_dcntl
, np
->rv_dcntl
| STD
);
6402 /*==========================================================
6405 ** Acquire a control block
6408 **==========================================================
6411 static nccb_p ncr_get_nccb
6412 (ncb_p np
, u_long target
, u_long lun
)
6417 /* Keep our timeout handler out */
6421 ** Lun structure available ?
6424 lp
= np
->target
[target
].lp
[lun
];
6429 ** Look for free CCB
6432 while (cp
&& cp
->magic
) {
6438 ** if nothing available, create one.
6442 cp
= ncr_alloc_nccb(np
, target
, lun
);
6446 kprintf("%s: Bogus free cp found\n", ncr_name(np
));
6456 /*==========================================================
6459 ** Release one control block
6462 **==========================================================
6465 static void ncr_free_nccb (ncb_p np
, nccb_p cp
)
6471 assert (cp
!= NULL
);
6473 cp
-> host_status
= HS_IDLE
;
6477 /*==========================================================
6480 ** Allocation of resources for Targets/Luns/Tags.
6483 **==========================================================
6487 ncr_alloc_nccb (ncb_p np
, u_long target
, u_long lun
)
6493 assert (np
!= NULL
);
6495 if (target
>=MAX_TARGET
) return(NULL
);
6496 if (lun
>=MAX_LUN
) return(NULL
);
6498 tp
=&np
->target
[target
];
6500 if (!tp
->jump_tcb
.l_cmd
) {
6505 tp
->jump_tcb
.l_cmd
= (SCR_JUMP
^IFFALSE (DATA (0x80 + target
)));
6506 tp
->jump_tcb
.l_paddr
= np
->jump_tcb
.l_paddr
;
6509 (np
->features
& FE_PFEN
)? SCR_COPY(1) : SCR_COPY_F(1);
6510 tp
->getscr
[1] = vtophys (&tp
->tinfo
.sval
);
6511 tp
->getscr
[2] = rman_get_start(np
->reg_res
) + offsetof (struct ncr_reg
, nc_sxfer
);
6513 (np
->features
& FE_PFEN
)? SCR_COPY(1) : SCR_COPY_F(1);
6514 tp
->getscr
[4] = vtophys (&tp
->tinfo
.wval
);
6515 tp
->getscr
[5] = rman_get_start(np
->reg_res
) + offsetof (struct ncr_reg
, nc_scntl3
);
6517 assert (((offsetof(struct ncr_reg
, nc_sxfer
) ^
6518 (offsetof(struct tcb
,tinfo
)
6519 + offsetof(struct ncr_target_tinfo
, sval
))) & 3) == 0);
6520 assert (((offsetof(struct ncr_reg
, nc_scntl3
) ^
6521 (offsetof(struct tcb
, tinfo
)
6522 + offsetof(struct ncr_target_tinfo
, wval
))) &3) == 0);
6524 tp
->call_lun
.l_cmd
= (SCR_CALL
);
6525 tp
->call_lun
.l_paddr
= NCB_SCRIPT_PHYS (np
, resel_lun
);
6527 tp
->jump_lcb
.l_cmd
= (SCR_JUMP
);
6528 tp
->jump_lcb
.l_paddr
= NCB_SCRIPTH_PHYS (np
, abort
);
6529 np
->jump_tcb
.l_paddr
= vtophys (&tp
->jump_tcb
);
6533 ** Logic unit control block
6540 lp
= kmalloc (sizeof (struct lcb
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
6545 lp
->jump_lcb
.l_cmd
= (SCR_JUMP
^ IFFALSE (DATA (lun
)));
6546 lp
->jump_lcb
.l_paddr
= tp
->jump_lcb
.l_paddr
;
6548 lp
->call_tag
.l_cmd
= (SCR_CALL
);
6549 lp
->call_tag
.l_paddr
= NCB_SCRIPT_PHYS (np
, resel_tag
);
6551 lp
->jump_nccb
.l_cmd
= (SCR_JUMP
);
6552 lp
->jump_nccb
.l_paddr
= NCB_SCRIPTH_PHYS (np
, aborttag
);
6557 ** Chain into LUN list
6559 tp
->jump_lcb
.l_paddr
= vtophys (&lp
->jump_lcb
);
6567 cp
= kmalloc (sizeof (struct nccb
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
6569 if (DEBUG_FLAGS
& DEBUG_ALLOC
) {
6570 kprintf ("new nccb @%p.\n", cp
);
6574 ** Fill in physical addresses
6577 cp
->p_nccb
= vtophys (cp
);
6580 ** Chain into reselect list
6582 cp
->jump_nccb
.l_cmd
= SCR_JUMP
;
6583 cp
->jump_nccb
.l_paddr
= lp
->jump_nccb
.l_paddr
;
6584 lp
->jump_nccb
.l_paddr
= CCB_PHYS (cp
, jump_nccb
);
6585 cp
->call_tmp
.l_cmd
= SCR_CALL
;
6586 cp
->call_tmp
.l_paddr
= NCB_SCRIPT_PHYS (np
, resel_tmp
);
6589 ** Chain into wakeup list
6591 cp
->link_nccb
= np
->link_nccb
;
6595 ** Chain into CCB list
6597 cp
->next_nccb
= lp
->next_nccb
;
6603 /*==========================================================
6606 ** Build Scatter Gather Block
6609 **==========================================================
6611 ** The transfer area may be scattered among
6612 ** several non adjacent physical pages.
6614 ** We may use MAX_SCATTER blocks.
6616 **----------------------------------------------------------
6619 static int ncr_scatter
6620 (struct dsb
* phys
, vm_offset_t vaddr
, vm_size_t datalen
)
6622 u_long paddr
, pnext
;
6624 u_short segment
= 0;
6625 u_long segsize
, segaddr
;
6626 u_long size
, csize
= 0;
6627 u_long chunk
= MAX_SIZE
;
6630 bzero (&phys
->data
, sizeof (phys
->data
));
6631 if (!datalen
) return (0);
6633 paddr
= vtophys (vaddr
);
6636 ** insert extra break points at a distance of chunk.
6637 ** We try to reduce the number of interrupts caused
6638 ** by unexpected phase changes due to disconnects.
6639 ** A typical harddisk may disconnect before ANY block.
6640 ** If we wanted to avoid unexpected phase changes at all
6641 ** we had to use a break point every 512 bytes.
6642 ** Of course the number of scatter/gather blocks is
6646 free
= MAX_SCATTER
- 1;
6648 if (vaddr
& PAGE_MASK
) free
-= datalen
/ PAGE_SIZE
;
6651 while ((chunk
* free
>= 2 * datalen
) && (chunk
>=1024))
6654 if(DEBUG_FLAGS
& DEBUG_SCATTER
)
6655 kprintf("ncr?:\tscattering virtual=%p size=%d chunk=%d.\n",
6656 (void *) vaddr
, (unsigned) datalen
, (unsigned) chunk
);
6659 ** Build data descriptors.
6661 while (datalen
&& (segment
< MAX_SCATTER
)) {
6664 ** this segment is empty
6670 if (!csize
) csize
= chunk
;
6672 while ((datalen
) && (paddr
== pnext
) && (csize
)) {
6675 ** continue this segment
6677 pnext
= (paddr
& (~PAGE_MASK
)) + PAGE_SIZE
;
6683 size
= pnext
- paddr
; /* page size */
6684 if (size
> datalen
) size
= datalen
; /* data size */
6685 if (size
> csize
) size
= csize
; /* chunksize */
6691 paddr
= vtophys (vaddr
);
6694 if(DEBUG_FLAGS
& DEBUG_SCATTER
)
6695 kprintf ("\tseg #%d addr=%x size=%d (rest=%d).\n",
6699 (unsigned) datalen
);
6701 phys
->data
[segment
].addr
= segaddr
;
6702 phys
->data
[segment
].size
= segsize
;
6707 kprintf("ncr?: scatter/gather failed (residue=%d).\n",
6708 (unsigned) datalen
);
6715 /*==========================================================
6718 ** Test the pci bus snoop logic :-(
6720 ** Has to be called with interrupts disabled.
6723 **==========================================================
6726 #ifndef NCR_IOMAPPED
6727 static int ncr_regtest (struct ncb
* np
)
6729 volatile u_int32_t data
;
6731 ** ncr registers may NOT be cached.
6732 ** write 0xffffffff to a read only register area,
6733 ** and try to read it back.
6736 OUTL_OFF(offsetof(struct ncr_reg
, nc_dstat
), data
);
6737 data
= INL_OFF(offsetof(struct ncr_reg
, nc_dstat
));
6739 if (data
== 0xffffffff) {
6741 if ((data
& 0xe2f0fffd) != 0x02000080) {
6743 kprintf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
6751 static int ncr_snooptest (struct ncb
* np
)
6753 u_int32_t ncr_rd
, ncr_wr
, ncr_bk
, host_rd
, host_wr
, pc
;
6755 #ifndef NCR_IOMAPPED
6756 err
|= ncr_regtest (np
);
6757 if (err
) return (err
);
6762 pc
= NCB_SCRIPTH_PHYS (np
, snooptest
);
6766 ** Set memory and register.
6768 ncr_cache
= host_wr
;
6769 OUTL (nc_temp
, ncr_wr
);
6771 ** Start script (exchange values)
6775 ** Wait 'til done (with timeout)
6777 for (i
=0; i
<NCR_SNOOP_TIMEOUT
; i
++)
6778 if (INB(nc_istat
) & (INTF
|SIP
|DIP
))
6781 ** Save termination position.
6785 ** Read memory and register.
6787 host_rd
= ncr_cache
;
6788 ncr_rd
= INL (nc_scratcha
);
6789 ncr_bk
= INL (nc_temp
);
6793 OUTB (nc_istat
, SRST
);
6795 OUTB (nc_istat
, 0 );
6797 ** check for timeout
6799 if (i
>=NCR_SNOOP_TIMEOUT
) {
6800 kprintf ("CACHE TEST FAILED: timeout.\n");
6804 ** Check termination position.
6806 if (pc
!= NCB_SCRIPTH_PHYS (np
, snoopend
)+8) {
6807 kprintf ("CACHE TEST FAILED: script execution failed.\n");
6808 kprintf ("start=%08lx, pc=%08lx, end=%08lx\n",
6809 (u_long
) NCB_SCRIPTH_PHYS (np
, snooptest
), (u_long
) pc
,
6810 (u_long
) NCB_SCRIPTH_PHYS (np
, snoopend
) +8);
6816 if (host_wr
!= ncr_rd
) {
6817 kprintf ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
6818 (int) host_wr
, (int) ncr_rd
);
6821 if (host_rd
!= ncr_wr
) {
6822 kprintf ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
6823 (int) ncr_wr
, (int) host_rd
);
6826 if (ncr_bk
!= ncr_wr
) {
6827 kprintf ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
6828 (int) ncr_wr
, (int) ncr_bk
);
6834 /*==========================================================
6837 ** Profiling the drivers and targets performance.
6840 **==========================================================
6844 ** Compute the difference in milliseconds.
6847 static int ncr_delta (int *from
, int *to
)
6849 if (!from
) return (-1);
6850 if (!to
) return (-2);
6851 return ((to
- from
) * 1000 / hz
);
6854 #define PROFILE cp->phys.header.stamp
6855 static void ncb_profile (ncb_p np
, nccb_p cp
)
6857 int co
, da
, st
, en
, di
, se
, post
,work
,disc
;
6860 PROFILE
.end
= ticks
;
6862 st
= ncr_delta (&PROFILE
.start
,&PROFILE
.status
);
6863 if (st
<0) return; /* status not reached */
6865 da
= ncr_delta (&PROFILE
.start
,&PROFILE
.data
);
6866 if (da
<0) return; /* No data transfer phase */
6868 co
= ncr_delta (&PROFILE
.start
,&PROFILE
.command
);
6869 if (co
<0) return; /* command not executed */
6871 en
= ncr_delta (&PROFILE
.start
,&PROFILE
.end
),
6872 di
= ncr_delta (&PROFILE
.start
,&PROFILE
.disconnect
),
6873 se
= ncr_delta (&PROFILE
.start
,&PROFILE
.select
);
6877 ** @PROFILE@ Disconnect time invalid if multiple disconnects
6880 if (di
>=0) disc
= se
-di
; else disc
= 0;
6882 work
= (st
- co
) - disc
;
6884 diff
= (np
->disc_phys
- np
->disc_ref
) & 0xff;
6885 np
->disc_ref
+= diff
;
6887 np
->profile
.num_trans
+= 1;
6889 np
->profile
.num_bytes
+= cp
->ccb
->csio
.dxfer_len
;
6890 np
->profile
.num_disc
+= diff
;
6891 np
->profile
.ms_setup
+= co
;
6892 np
->profile
.ms_data
+= work
;
6893 np
->profile
.ms_disc
+= disc
;
6894 np
->profile
.ms_post
+= post
;
6898 /*==========================================================
6900 ** Determine the ncr's clock frequency.
6901 ** This is essential for the negotiation
6902 ** of the synchronous transfer rate.
6904 **==========================================================
6906 ** Note: we have to return the correct value.
6907 ** THERE IS NO SAVE DEFAULT VALUE.
6909 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
6910 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
6911 ** do not have a clock doubler and so are provided with a
6912 ** 80 MHz clock. All other fast20 boards incorporate a doubler
6913 ** and so should be delivered with a 40 MHz clock.
6914 ** The future fast40 chips (895/895) use a 40 Mhz base clock
6915 ** and provide a clock quadrupler (160 Mhz). The code below
6916 ** tries to deal as cleverly as possible with all this stuff.
6918 **----------------------------------------------------------
6922 * Select NCR SCSI clock frequency
6924 static void ncr_selectclock(ncb_p np
, u_char scntl3
)
6926 if (np
->multiplier
< 2) {
6927 OUTB(nc_scntl3
, scntl3
);
6931 if (bootverbose
>= 2)
6932 kprintf ("%s: enabling clock multiplier\n", ncr_name(np
));
6934 OUTB(nc_stest1
, DBLEN
); /* Enable clock multiplier */
6935 if (np
->multiplier
> 2) { /* Poll bit 5 of stest4 for quadrupler */
6937 while (!(INB(nc_stest4
) & LCKFRQ
) && --i
> 0)
6940 kprintf("%s: the chip cannot lock the frequency\n", ncr_name(np
));
6941 } else /* Wait 20 micro-seconds for doubler */
6943 OUTB(nc_stest3
, HSC
); /* Halt the scsi clock */
6944 OUTB(nc_scntl3
, scntl3
);
6945 OUTB(nc_stest1
, (DBLEN
|DBLSEL
));/* Select clock multiplier */
6946 OUTB(nc_stest3
, 0x00); /* Restart scsi clock */
6950 * calculate NCR SCSI clock frequency (in KHz)
6953 ncrgetfreq (ncb_p np
, int gen
)
6957 * Measure GEN timer delay in order
6958 * to calculate SCSI clock frequency
6960 * This code will never execute too
6961 * many loop iterations (if DELAY is
6962 * reasonably correct). It could get
6963 * too low a delay (too high a freq.)
6964 * if the CPU is slow executing the
6965 * loop for some reason (an NMI, for
6966 * example). For this reason we will
6967 * if multiple measurements are to be
6968 * performed trust the higher delay
6969 * (lower frequency returned).
6971 OUTB (nc_stest1
, 0); /* make sure clock doubler is OFF */
6972 OUTW (nc_sien
, 0); /* mask all scsi interrupts */
6973 (void) INW (nc_sist
); /* clear pending scsi interrupt */
6974 OUTB (nc_dien
, 0); /* mask all dma interrupts */
6975 (void) INW (nc_sist
); /* another one, just to be sure :) */
6976 OUTB (nc_scntl3
, 4); /* set pre-scaler to divide by 3 */
6977 OUTB (nc_stime1
, 0); /* disable general purpose timer */
6978 OUTB (nc_stime1
, gen
); /* set to nominal delay of (1<<gen) * 125us */
6979 while (!(INW(nc_sist
) & GEN
) && ms
++ < 1000)
6980 DELAY(1000); /* count ms */
6981 OUTB (nc_stime1
, 0); /* disable general purpose timer */
6982 OUTB (nc_scntl3
, 0);
6984 * Set prescaler to divide by whatever "0" means.
6985 * "0" ought to choose divide by 2, but appears
6986 * to set divide by 3.5 mode in my 53c810 ...
6988 OUTB (nc_scntl3
, 0);
6990 if (bootverbose
>= 2)
6991 kprintf ("\tDelay (GEN=%d): %u msec\n", gen
, ms
);
6993 * adjust for prescaler, and convert into KHz
6995 return ms
? ((1 << gen
) * 4440) / ms
: 0;
6998 static void ncr_getclock (ncb_p np
, u_char multiplier
)
7000 unsigned char scntl3
;
7001 unsigned char stest1
;
7002 scntl3
= INB(nc_scntl3
);
7003 stest1
= INB(nc_stest1
);
7007 if (multiplier
> 1) {
7008 np
->multiplier
= multiplier
;
7009 np
->clock_khz
= 40000 * multiplier
;
7011 if ((scntl3
& 7) == 0) {
7013 /* throw away first result */
7014 (void) ncrgetfreq (np
, 11);
7015 f1
= ncrgetfreq (np
, 11);
7016 f2
= ncrgetfreq (np
, 11);
7018 if (bootverbose
>= 2)
7019 kprintf ("\tNCR clock is %uKHz, %uKHz\n", f1
, f2
);
7020 if (f1
> f2
) f1
= f2
; /* trust lower result */
7022 scntl3
= 5; /* >45Mhz: assume 80MHz */
7024 scntl3
= 3; /* <45Mhz: assume 40MHz */
7027 else if ((scntl3
& 7) == 5)
7028 np
->clock_khz
= 80000; /* Probably a 875 rev. 1 ? */
7032 /*=========================================================================*/
7034 #ifdef NCR_TEKRAM_EEPROM
7036 struct tekram_eeprom_dev
{
7038 #define TKR_PARCHK 0x01
7039 #define TKR_TRYSYNC 0x02
7040 #define TKR_ENDISC 0x04
7041 #define TKR_STARTUNIT 0x08
7042 #define TKR_USETAGS 0x10
7043 #define TKR_TRYWIDE 0x20
7044 u_char syncparam
; /* max. sync transfer rate (table ?) */
7050 struct tekram_eeprom
{
7051 struct tekram_eeprom_dev
7055 #define TKR_ADPT_GT2DRV 0x01
7056 #define TKR_ADPT_GT1GB 0x02
7057 #define TKR_ADPT_RSTBUS 0x04
7058 #define TKR_ADPT_ACTNEG 0x08
7059 #define TKR_ADPT_NOSEEK 0x10
7060 #define TKR_ADPT_MORLUN 0x20
7061 u_char delay
; /* unit ? ( table ??? ) */
7062 u_char tags
; /* use 4 times as many ... */
7067 tekram_write_bit (ncb_p np
, int bit
)
7069 u_char val
= 0x10 + ((bit
& 1) << 1);
7072 OUTB (nc_gpreg
, val
);
7074 OUTB (nc_gpreg
, val
| 0x04);
7076 OUTB (nc_gpreg
, val
);
7081 tekram_read_bit (ncb_p np
)
7083 OUTB (nc_gpreg
, 0x10);
7085 OUTB (nc_gpreg
, 0x14);
7087 return INB (nc_gpreg
) & 1;
7091 read_tekram_eeprom_reg (ncb_p np
, int reg
)
7095 int cmd
= 0x80 | reg
;
7097 OUTB (nc_gpreg
, 0x10);
7099 tekram_write_bit (np
, 1);
7100 for (bit
= 7; bit
>= 0; bit
--)
7102 tekram_write_bit (np
, cmd
>> bit
);
7105 for (bit
= 0; bit
< 16; bit
++)
7108 result
|= tekram_read_bit (np
);
7111 OUTB (nc_gpreg
, 0x00);
7116 read_tekram_eeprom(ncb_p np
, struct tekram_eeprom
*buffer
)
7118 u_short
*p
= (u_short
*) buffer
;
7122 if (INB (nc_gpcntl
) != 0x09)
7126 for (i
= 0; i
< 64; i
++)
7129 if((i
&0x0f) == 0) kprintf ("%02x:", i
*2);
7130 val
= read_tekram_eeprom_reg (np
, i
);
7134 if((i
&0x01) == 0x00) kprintf (" ");
7135 kprintf ("%02x%02x", val
& 0xff, (val
>> 8) & 0xff);
7136 if((i
&0x0f) == 0x0f) kprintf ("\n");
7138 kprintf ("Sum = %04x\n", sum
);
7139 return sum
== 0x1234;
7141 #endif /* NCR_TEKRAM_EEPROM */
7143 static device_method_t ncr_methods
[] = {
7144 /* Device interface */
7145 DEVMETHOD(device_probe
, ncr_probe
),
7146 DEVMETHOD(device_attach
, ncr_attach
),
7151 static driver_t ncr_driver
= {
7157 static devclass_t ncr_devclass
;
7159 DRIVER_MODULE(if_ncr
, pci
, ncr_driver
, ncr_devclass
, NULL
, NULL
);
7161 /*=========================================================================*/
7162 #endif /* _KERNEL */