Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / scsi / ncr53c8xx.c
blob3f8854aafeb4f711f8d26f685fb1bd97b062ee38
1 /******************************************************************************
2 ** Device driver for the PCI-SCSI NCR538XX controller family.
3 **
4 ** Copyright (C) 1994 Wolfgang Stanglmeier
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **-----------------------------------------------------------------------------
22 ** This driver has been ported to Linux from the FreeBSD NCR53C8XX driver
23 ** and is currently maintained by
25 ** Gerard Roudier <groudier@club-internet.fr>
27 ** Being given that this driver originates from the FreeBSD version, and
28 ** in order to keep synergy on both, any suggested enhancements and corrections
29 ** received on Linux are automatically a potential candidate for the FreeBSD
30 ** version.
32 ** The original driver has been written for 386bsd and FreeBSD by
33 ** Wolfgang Stanglmeier <wolf@cologne.de>
34 ** Stefan Esser <se@mi.Uni-Koeln.de>
36 ** And has been ported to NetBSD by
37 ** Charles M. Hannum <mycroft@gnu.ai.mit.edu>
39 **-----------------------------------------------------------------------------
41 ** Brief history
43 ** December 10 1995 by Gerard Roudier:
44 ** Initial port to Linux.
46 ** June 23 1996 by Gerard Roudier:
47 ** Support for 64 bits architectures (Alpha).
49 ** November 30 1996 by Gerard Roudier:
50 ** Support for Fast-20 scsi.
51 ** Support for large DMA fifo and 128 dwords bursting.
53 ** February 27 1997 by Gerard Roudier:
54 ** Support for Fast-40 scsi.
55 ** Support for on-Board RAM.
57 ** May 3 1997 by Gerard Roudier:
58 ** Full support for scsi scripts instructions pre-fetching.
60 ** May 19 1997 by Richard Waltham <dormouse@farsrobt.demon.co.uk>:
61 ** Support for NvRAM detection and reading.
63 ** August 18 1997 by Cort <cort@cs.nmt.edu>:
64 ** Support for Power/PC (Big Endian).
66 ** June 20 1998 by Gerard Roudier <groudier@club-internet.fr>:
67 ** Support for up to 64 tags per lun.
68 ** O(1) everywhere (C and SCRIPTS) for normal cases.
69 ** Low PCI traffic for command handling when on-chip RAM is present.
70 ** Aggressive SCSI SCRIPTS optimizations.
72 *******************************************************************************
76 ** May 11 2000, version 3.3b
78 ** Supported SCSI-II features:
79 ** Synchronous negotiation
80 ** Wide negotiation (depends on the NCR Chip)
81 ** Enable disconnection
82 ** Tagged command queuing
83 ** Parity checking
84 ** Etc...
86 ** Supported NCR/SYMBIOS chips:
87 ** 53C810 (8 bits, Fast SCSI-2, no rom BIOS)
88 ** 53C815 (8 bits, Fast SCSI-2, on board rom BIOS)
89 ** 53C820 (Wide, Fast SCSI-2, no rom BIOS)
90 ** 53C825 (Wide, Fast SCSI-2, on board rom BIOS)
91 ** 53C860 (8 bits, Fast 20, no rom BIOS)
92 ** 53C875 (Wide, Fast 20, on board rom BIOS)
93 ** 53C895 (Wide, Fast 40, on board rom BIOS)
94 ** 53C895A (Wide, Fast 40, on board rom BIOS)
95 ** 53C896 (Wide, Fast 40, on board rom BIOS)
96 ** 53C897 (Wide, Fast 40, on board rom BIOS)
97 ** 53C1510D (Wide, Fast 40, on board rom BIOS)
99 ** Other features:
100 ** Memory mapped IO (linux-1.3.X and above only)
101 ** Module
102 ** Shared IRQ (since linux-1.3.72)
106 ** Name and version of the driver
108 #define SCSI_NCR_DRIVER_NAME "ncr53c8xx - version 3.3b"
110 #define SCSI_NCR_DEBUG_FLAGS (0)
112 /*==========================================================
114 ** Include files
116 **==========================================================
119 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
121 #ifdef MODULE
122 #include <linux/module.h>
123 #endif
125 #include <asm/dma.h>
126 #include <asm/io.h>
127 #include <asm/system.h>
128 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,17)
129 #include <linux/spinlock.h>
130 #elif LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
131 #include <asm/spinlock.h>
132 #endif
133 #include <linux/delay.h>
134 #include <linux/signal.h>
135 #include <linux/sched.h>
136 #include <linux/errno.h>
137 #include <linux/pci.h>
138 #include <linux/string.h>
139 #include <linux/malloc.h>
140 #include <linux/mm.h>
141 #include <linux/ioport.h>
142 #include <linux/time.h>
143 #include <linux/timer.h>
144 #include <linux/stat.h>
146 #include <linux/version.h>
147 #include <linux/blk.h>
149 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,35)
150 #include <linux/init.h>
151 #endif
153 #ifndef __init
154 #define __init
155 #endif
156 #ifndef __initdata
157 #define __initdata
158 #endif
160 #if LINUX_VERSION_CODE <= LinuxVersionCode(2,1,92)
161 #include <linux/bios32.h>
162 #endif
164 #include "scsi.h"
165 #include "hosts.h"
166 #include "constants.h"
167 #include "sd.h"
169 #include <linux/types.h>
172 ** Define BITS_PER_LONG for earlier linux versions.
174 #ifndef BITS_PER_LONG
175 #if (~0UL) == 0xffffffffUL
176 #define BITS_PER_LONG 32
177 #else
178 #define BITS_PER_LONG 64
179 #endif
180 #endif
183 ** Define the BSD style u_int32 and u_int64 type.
184 ** Are in fact u_int32_t and u_int64_t :-)
186 typedef u32 u_int32;
187 typedef u64 u_int64;
188 typedef u_long vm_offset_t;
189 #include "ncr53c8xx.h"
192 ** Donnot compile integrity checking code for Linux-2.3.0
193 ** and above since SCSI data structures are not ready yet.
195 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,0)
196 #define SCSI_NCR_INTEGRITY_CHECKING
197 #endif
199 #define NAME53C "ncr53c"
200 #define NAME53C8XX "ncr53c8xx"
201 #define DRIVER_SMP_LOCK ncr53c8xx_lock
203 #include "sym53c8xx_comm.h"
205 /*==========================================================
207 ** The CCB done queue uses an array of CCB virtual
208 ** addresses. Empty entries are flagged using the bogus
209 ** virtual address 0xffffffff.
211 ** Since PCI ensures that only aligned DWORDs are accessed
212 ** atomically, 64 bit little-endian architecture requires
213 ** to test the high order DWORD of the entry to determine
214 ** if it is empty or valid.
216 ** BTW, I will make things differently as soon as I will
217 ** have a better idea, but this is simple and should work.
219 **==========================================================
222 #define SCSI_NCR_CCB_DONE_SUPPORT
223 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
225 #define MAX_DONE 24
226 #define CCB_DONE_EMPTY 0xffffffffUL
228 /* All 32 bit architectures */
229 #if BITS_PER_LONG == 32
230 #define CCB_DONE_VALID(cp) (((u_long) cp) != CCB_DONE_EMPTY)
232 /* All > 32 bit (64 bit) architectures regardless endian-ness */
233 #else
234 #define CCB_DONE_VALID(cp) \
235 ((((u_long) cp) & 0xffffffff00000000ul) && \
236 (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
237 #endif
239 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
241 /*==========================================================
243 ** Configuration and Debugging
245 **==========================================================
249 ** SCSI address of this device.
250 ** The boot routines should have set it.
251 ** If not, use this.
254 #ifndef SCSI_NCR_MYADDR
255 #define SCSI_NCR_MYADDR (7)
256 #endif
259 ** The maximum number of tags per logic unit.
260 ** Used only for disk devices that support tags.
263 #ifndef SCSI_NCR_MAX_TAGS
264 #define SCSI_NCR_MAX_TAGS (8)
265 #endif
268 ** TAGS are actually limited to 64 tags/lun.
269 ** We need to deal with power of 2, for alignment constraints.
271 #if SCSI_NCR_MAX_TAGS > 64
272 #define MAX_TAGS (64)
273 #else
274 #define MAX_TAGS SCSI_NCR_MAX_TAGS
275 #endif
277 #define NO_TAG (255)
280 ** Choose appropriate type for tag bitmap.
282 #if MAX_TAGS > 32
283 typedef u_int64 tagmap_t;
284 #else
285 typedef u_int32 tagmap_t;
286 #endif
289 ** Number of targets supported by the driver.
290 ** n permits target numbers 0..n-1.
291 ** Default is 16, meaning targets #0..#15.
292 ** #7 .. is myself.
295 #ifdef SCSI_NCR_MAX_TARGET
296 #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
297 #else
298 #define MAX_TARGET (16)
299 #endif
302 ** Number of logic units supported by the driver.
303 ** n enables logic unit numbers 0..n-1.
304 ** The common SCSI devices require only
305 ** one lun, so take 1 as the default.
308 #ifdef SCSI_NCR_MAX_LUN
309 #define MAX_LUN SCSI_NCR_MAX_LUN
310 #else
311 #define MAX_LUN (1)
312 #endif
315 ** Asynchronous pre-scaler (ns). Shall be 40
318 #ifndef SCSI_NCR_MIN_ASYNC
319 #define SCSI_NCR_MIN_ASYNC (40)
320 #endif
323 ** The maximum number of jobs scheduled for starting.
324 ** There should be one slot per target, and one slot
325 ** for each tag of each target in use.
326 ** The calculation below is actually quite silly ...
329 #ifdef SCSI_NCR_CAN_QUEUE
330 #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
331 #else
332 #define MAX_START (MAX_TARGET + 7 * MAX_TAGS)
333 #endif
336 ** We limit the max number of pending IO to 250.
337 ** since we donnot want to allocate more than 1
338 ** PAGE for 'scripth'.
340 #if MAX_START > 250
341 #undef MAX_START
342 #define MAX_START 250
343 #endif
346 ** The maximum number of segments a transfer is split into.
347 ** We support up to 127 segments for both read and write.
348 ** The data scripts are broken into 2 sub-scripts.
349 ** 80 (MAX_SCATTERL) segments are moved from a sub-script
350 ** in on-chip RAM. This makes data transfers shorter than
351 ** 80k (assuming 1k fs) as fast as possible.
354 #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
356 #if (MAX_SCATTER > 80)
357 #define MAX_SCATTERL 80
358 #define MAX_SCATTERH (MAX_SCATTER - MAX_SCATTERL)
359 #else
360 #define MAX_SCATTERL (MAX_SCATTER-1)
361 #define MAX_SCATTERH 1
362 #endif
365 ** other
368 #define NCR_SNOOP_TIMEOUT (1000000)
371 ** Head of list of NCR boards
373 ** For kernel version < 1.3.70, host is retrieved by its irq level.
374 ** For later kernels, the internal host control block address
375 ** (struct ncb) is used as device id parameter of the irq stuff.
378 static struct Scsi_Host *first_host = NULL;
379 static Scsi_Host_Template *the_template = NULL;
382 ** Other definitions
385 #define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
387 static void ncr53c8xx_select_queue_depths(
388 struct Scsi_Host *host, struct scsi_device *devlist);
389 static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs);
390 static void ncr53c8xx_timeout(unsigned long np);
392 #define initverbose (driver_setup.verbose)
393 #define bootverbose (np->verbose)
395 #ifdef SCSI_NCR_NVRAM_SUPPORT
396 static u_char Tekram_sync[16] __initdata =
397 {25,31,37,43, 50,62,75,125, 12,15,18,21, 6,7,9,10};
398 #endif /* SCSI_NCR_NVRAM_SUPPORT */
400 /*==========================================================
402 ** Command control block states.
404 **==========================================================
407 #define HS_IDLE (0)
408 #define HS_BUSY (1)
409 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
410 #define HS_DISCONNECT (3) /* Disconnected by target */
412 #define HS_DONEMASK (0x80)
413 #define HS_COMPLETE (4|HS_DONEMASK)
414 #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
415 #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
416 #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
417 #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
418 #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
419 #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
422 ** Invalid host status values used by the SCRIPTS processor
423 ** when the nexus is not fully identified.
424 ** Shall never appear in a CCB.
427 #define HS_INVALMASK (0x40)
428 #define HS_SELECTING (0|HS_INVALMASK)
429 #define HS_IN_RESELECT (1|HS_INVALMASK)
430 #define HS_STARTING (2|HS_INVALMASK)
433 ** Flags set by the SCRIPT processor for commands
434 ** that have been skipped.
436 #define HS_SKIPMASK (0x20)
438 /*==========================================================
440 ** Software Interrupt Codes
442 **==========================================================
445 #define SIR_BAD_STATUS (1)
446 #define SIR_XXXXXXXXXX (2)
447 #define SIR_NEGO_SYNC (3)
448 #define SIR_NEGO_WIDE (4)
449 #define SIR_NEGO_FAILED (5)
450 #define SIR_NEGO_PROTO (6)
451 #define SIR_REJECT_RECEIVED (7)
452 #define SIR_REJECT_SENT (8)
453 #define SIR_IGN_RESIDUE (9)
454 #define SIR_MISSING_SAVE (10)
455 #define SIR_RESEL_NO_MSG_IN (11)
456 #define SIR_RESEL_NO_IDENTIFY (12)
457 #define SIR_RESEL_BAD_LUN (13)
458 #define SIR_RESEL_BAD_TARGET (14)
459 #define SIR_RESEL_BAD_I_T_L (15)
460 #define SIR_RESEL_BAD_I_T_L_Q (16)
461 #define SIR_DONE_OVERFLOW (17)
462 #define SIR_MAX (17)
464 /*==========================================================
466 ** Extended error codes.
467 ** xerr_status field of struct ccb.
469 **==========================================================
472 #define XE_OK (0)
473 #define XE_EXTRA_DATA (1) /* unexpected data phase */
474 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
476 /*==========================================================
478 ** Negotiation status.
479 ** nego_status field of struct ccb.
481 **==========================================================
484 #define NS_NOCHANGE (0)
485 #define NS_SYNC (1)
486 #define NS_WIDE (2)
487 #define NS_PPR (4)
489 /*==========================================================
491 ** "Special features" of targets.
492 ** quirks field of struct tcb.
493 ** actualquirks field of struct ccb.
495 **==========================================================
498 #define QUIRK_AUTOSAVE (0x01)
499 #define QUIRK_NOMSG (0x02)
500 #define QUIRK_NOSYNC (0x10)
501 #define QUIRK_NOWIDE16 (0x20)
503 /*==========================================================
505 ** Capability bits in Inquire response byte 7.
507 **==========================================================
510 #define INQ7_QUEUE (0x02)
511 #define INQ7_SYNC (0x10)
512 #define INQ7_WIDE16 (0x20)
514 /*==========================================================
516 ** Misc.
518 **==========================================================
521 #define CCB_MAGIC (0xf2691ad2)
523 /*==========================================================
525 ** Declaration of structs.
527 **==========================================================
530 struct tcb;
531 struct lcb;
532 struct ccb;
533 struct ncb;
534 struct script;
536 typedef struct ncb * ncb_p;
537 typedef struct tcb * tcb_p;
538 typedef struct lcb * lcb_p;
539 typedef struct ccb * ccb_p;
541 struct link {
542 ncrcmd l_cmd;
543 ncrcmd l_paddr;
546 struct usrcmd {
547 u_long target;
548 u_long lun;
549 u_long data;
550 u_long cmd;
553 #define UC_SETSYNC 10
554 #define UC_SETTAGS 11
555 #define UC_SETDEBUG 12
556 #define UC_SETORDER 13
557 #define UC_SETWIDE 14
558 #define UC_SETFLAG 15
559 #define UC_CLEARPROF 16
560 #define UC_SETVERBOSE 17
562 #define UF_TRACE (0x01)
563 #define UF_NODISC (0x02)
564 #define UF_NOSCAN (0x04)
566 /*---------------------------------------
568 ** Timestamps for profiling
570 **---------------------------------------
573 #ifdef SCSI_NCR_PROFILE_SUPPORT
575 struct tstamp {
576 u_long start;
577 u_long end;
578 u_long command;
579 u_long status;
580 u_long disconnect;
581 u_long reselect;
585 ** profiling data (per device)
588 struct profile {
589 u_long num_trans;
590 u_long num_kbytes;
591 u_long rest_bytes;
592 u_long num_disc;
593 u_long num_break;
594 u_long num_int;
595 u_long num_fly;
596 u_long ms_setup;
597 u_long ms_data;
598 u_long ms_disc;
599 u_long ms_post;
601 #endif
603 /*========================================================================
605 ** Declaration of structs: target control block
607 **========================================================================
609 struct tcb {
610 /*----------------------------------------------------------------
611 ** During reselection the ncr jumps to this point with SFBR
612 ** set to the encoded target number with bit 7 set.
613 ** if it's not this target, jump to the next.
615 ** JUMP IF (SFBR != #target#), @(next tcb)
616 **----------------------------------------------------------------
618 struct link jump_tcb;
620 /*----------------------------------------------------------------
621 ** Load the actual values for the sxfer and the scntl3
622 ** register (sync/wide mode).
624 ** SCR_COPY (1), @(sval field of this tcb), @(sxfer register)
625 ** SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
626 **----------------------------------------------------------------
628 ncrcmd getscr[6];
630 /*----------------------------------------------------------------
631 ** Get the IDENTIFY message and load the LUN to SFBR.
633 ** CALL, <RESEL_LUN>
634 **----------------------------------------------------------------
636 struct link call_lun;
638 /*----------------------------------------------------------------
639 ** Now look for the right lun.
641 ** For i = 0 to 3
642 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
644 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
645 ** It is kind of hashcoding.
646 **----------------------------------------------------------------
648 struct link jump_lcb[4]; /* JUMPs for reselection */
649 lcb_p lp[MAX_LUN]; /* The lcb's of this tcb */
650 u_char inq_done; /* Target capabilities received */
651 u_char inq_byte7; /* Contains these capabilities */
653 /*----------------------------------------------------------------
654 ** Pointer to the ccb used for negotiation.
655 ** Prevent from starting a negotiation for all queued commands
656 ** when tagged command queuing is enabled.
657 **----------------------------------------------------------------
659 ccb_p nego_cp;
661 /*----------------------------------------------------------------
662 ** statistical data
663 **----------------------------------------------------------------
665 u_long transfers;
666 u_long bytes;
668 /*----------------------------------------------------------------
669 ** negotiation of wide and synch transfer and device quirks.
670 **----------------------------------------------------------------
672 /*0*/ u_char minsync;
673 /*1*/ u_char sval;
674 /*2*/ u_short period;
675 /*0*/ u_char maxoffs;
676 /*1*/ u_char quirks;
677 /*2*/ u_char widedone;
678 /*3*/ u_char wval;
680 #ifdef SCSI_NCR_INTEGRITY_CHECKING
681 u_char ic_min_sync;
682 u_char ic_max_width;
683 u_char ic_maximums_set;
684 u_char ic_done;
685 #endif
687 /*----------------------------------------------------------------
688 ** User settable limits and options.
689 ** These limits are read from the NVRAM if present.
690 **----------------------------------------------------------------
692 u_char usrsync;
693 u_char usrwide;
694 u_char usrtags;
695 u_char usrflag;
698 /*========================================================================
700 ** Declaration of structs: lun control block
702 **========================================================================
704 struct lcb {
705 /*----------------------------------------------------------------
706 ** During reselection the ncr jumps to this point
707 ** with SFBR set to the "Identify" message.
708 ** if it's not this lun, jump to the next.
710 ** JUMP IF (SFBR != #lun#), @(next lcb of this target)
712 ** It is this lun. Load TEMP with the nexus jumps table
713 ** address and jump to RESEL_TAG (or RESEL_NOTAG).
715 ** SCR_COPY (4), p_jump_ccb, TEMP,
716 ** SCR_JUMP, <RESEL_TAG>
717 **----------------------------------------------------------------
719 struct link jump_lcb;
720 ncrcmd load_jump_ccb[3];
721 struct link jump_tag;
722 ncrcmd p_jump_ccb; /* Jump table bus address */
724 /*----------------------------------------------------------------
725 ** Jump table used by the script processor to directly jump
726 ** to the CCB corresponding to the reselected nexus.
727 ** Address is allocated on 256 bytes boundary in order to
728 ** allow 8 bit calculation of the tag jump entry for up to
729 ** 64 possible tags.
730 **----------------------------------------------------------------
732 u_int32 jump_ccb_0; /* Default table if no tags */
733 u_int32 *jump_ccb; /* Virtual address */
735 /*----------------------------------------------------------------
736 ** CCB queue management.
737 **----------------------------------------------------------------
739 XPT_QUEHEAD free_ccbq; /* Queue of available CCBs */
740 XPT_QUEHEAD busy_ccbq; /* Queue of busy CCBs */
741 XPT_QUEHEAD wait_ccbq; /* Queue of waiting for IO CCBs */
742 XPT_QUEHEAD skip_ccbq; /* Queue of skipped CCBs */
743 u_char actccbs; /* Number of allocated CCBs */
744 u_char busyccbs; /* CCBs busy for this lun */
745 u_char queuedccbs; /* CCBs queued to the controller*/
746 u_char queuedepth; /* Queue depth for this lun */
747 u_char scdev_depth; /* SCSI device queue depth */
748 u_char maxnxs; /* Max possible nexuses */
750 /*----------------------------------------------------------------
751 ** Control of tagged command queuing.
752 ** Tags allocation is performed using a circular buffer.
753 ** This avoids using a loop for tag allocation.
754 **----------------------------------------------------------------
756 u_char ia_tag; /* Allocation index */
757 u_char if_tag; /* Freeing index */
758 u_char cb_tags[MAX_TAGS]; /* Circular tags buffer */
759 u_char usetags; /* Command queuing is active */
760 u_char maxtags; /* Max nr of tags asked by user */
761 u_char numtags; /* Current number of tags */
762 u_char inq_byte7; /* Store unit CmdQ capabitility */
764 /*----------------------------------------------------------------
765 ** QUEUE FULL control and ORDERED tag control.
766 **----------------------------------------------------------------
768 /*----------------------------------------------------------------
769 ** QUEUE FULL and ORDERED tag control.
770 **----------------------------------------------------------------
772 u_short num_good; /* Nr of GOOD since QUEUE FULL */
773 tagmap_t tags_umap; /* Used tags bitmap */
774 tagmap_t tags_smap; /* Tags in use at 'tag_stime' */
775 u_long tags_stime; /* Last time we set smap=umap */
776 ccb_p held_ccb; /* CCB held for QUEUE FULL */
779 /*========================================================================
781 ** Declaration of structs: the launch script.
783 **========================================================================
785 ** It is part of the CCB and is called by the scripts processor to
786 ** start or restart the data structure (nexus).
787 ** This 6 DWORDs mini script makes use of prefetching.
789 **------------------------------------------------------------------------
791 struct launch {
792 /*----------------------------------------------------------------
793 ** SCR_COPY(4), @(p_phys), @(dsa register)
794 ** SCR_JUMP, @(scheduler_point)
795 **----------------------------------------------------------------
797 ncrcmd setup_dsa[3]; /* Copy 'phys' address to dsa */
798 struct link schedule; /* Jump to scheduler point */
799 ncrcmd p_phys; /* 'phys' header bus address */
802 /*========================================================================
804 ** Declaration of structs: global HEADER.
806 **========================================================================
808 ** This substructure is copied from the ccb to a global address after
809 ** selection (or reselection) and copied back before disconnect.
811 ** These fields are accessible to the script processor.
813 **------------------------------------------------------------------------
816 struct head {
817 /*----------------------------------------------------------------
818 ** Saved data pointer.
819 ** Points to the position in the script responsible for the
820 ** actual transfer transfer of data.
821 ** It's written after reception of a SAVE_DATA_POINTER message.
822 ** The goalpointer points after the last transfer command.
823 **----------------------------------------------------------------
825 u_int32 savep;
826 u_int32 lastp;
827 u_int32 goalp;
829 /*----------------------------------------------------------------
830 ** Alternate data pointer.
831 ** They are copied back to savep/lastp/goalp by the SCRIPTS
832 ** when the direction is unknown and the device claims data out.
833 **----------------------------------------------------------------
835 u_int32 wlastp;
836 u_int32 wgoalp;
838 /*----------------------------------------------------------------
839 ** The virtual address of the ccb containing this header.
840 **----------------------------------------------------------------
842 ccb_p cp;
844 #ifdef SCSI_NCR_PROFILE_SUPPORT
845 /*----------------------------------------------------------------
846 ** Space for some timestamps to gather profiling data.
847 **----------------------------------------------------------------
849 struct tstamp stamp;
850 #endif
852 /*----------------------------------------------------------------
853 ** Status fields.
854 **----------------------------------------------------------------
856 u_char scr_st[4]; /* script status */
857 u_char status[4]; /* host status. must be the */
858 /* last DWORD of the header. */
862 ** The status bytes are used by the host and the script processor.
864 ** The byte corresponding to the host_status must be stored in the
865 ** last DWORD of the CCB header since it is used for command
866 ** completion (ncr_wakeup()). Doing so, we are sure that the header
867 ** has been entirely copied back to the CCB when the host_status is
868 ** seen complete by the CPU.
870 ** The last four bytes (status[4]) are copied to the scratchb register
871 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
872 ** and copied back just after disconnecting.
873 ** Inside the script the XX_REG are used.
875 ** The first four bytes (scr_st[4]) are used inside the script by
876 ** "COPY" commands.
877 ** Because source and destination must have the same alignment
878 ** in a DWORD, the fields HAVE to be at the choosen offsets.
879 ** xerr_st 0 (0x34) scratcha
880 ** sync_st 1 (0x05) sxfer
881 ** wide_st 3 (0x03) scntl3
885 ** Last four bytes (script)
887 #define QU_REG scr0
888 #define HS_REG scr1
889 #define HS_PRT nc_scr1
890 #define SS_REG scr2
891 #define SS_PRT nc_scr2
892 #define PS_REG scr3
895 ** Last four bytes (host)
897 #define actualquirks phys.header.status[0]
898 #define host_status phys.header.status[1]
899 #define scsi_status phys.header.status[2]
900 #define parity_status phys.header.status[3]
903 ** First four bytes (script)
905 #define xerr_st header.scr_st[0]
906 #define sync_st header.scr_st[1]
907 #define nego_st header.scr_st[2]
908 #define wide_st header.scr_st[3]
911 ** First four bytes (host)
913 #define xerr_status phys.xerr_st
914 #define nego_status phys.nego_st
916 #if 0
917 #define sync_status phys.sync_st
918 #define wide_status phys.wide_st
919 #endif
921 /*==========================================================
923 ** Declaration of structs: Data structure block
925 **==========================================================
927 ** During execution of a ccb by the script processor,
928 ** the DSA (data structure address) register points
929 ** to this substructure of the ccb.
930 ** This substructure contains the header with
931 ** the script-processor-changable data and
932 ** data blocks for the indirect move commands.
934 **----------------------------------------------------------
937 struct dsb {
940 ** Header.
943 struct head header;
946 ** Table data for Script
949 struct scr_tblsel select;
950 struct scr_tblmove smsg ;
951 struct scr_tblmove cmd ;
952 struct scr_tblmove sense ;
953 struct scr_tblmove data [MAX_SCATTER];
957 /*========================================================================
959 ** Declaration of structs: Command control block.
961 **========================================================================
963 struct ccb {
964 /*----------------------------------------------------------------
965 ** This is the data structure which is pointed by the DSA
966 ** register when it is executed by the script processor.
967 ** It must be the first entry because it contains the header
968 ** as first entry that must be cache line aligned.
969 **----------------------------------------------------------------
971 struct dsb phys;
973 /*----------------------------------------------------------------
974 ** Mini-script used at CCB execution start-up.
975 ** Load the DSA with the data structure address (phys) and
976 ** jump to SELECT. Jump to CANCEL if CCB is to be canceled.
977 **----------------------------------------------------------------
979 struct launch start;
981 /*----------------------------------------------------------------
982 ** Mini-script used at CCB relection to restart the nexus.
983 ** Load the DSA with the data structure address (phys) and
984 ** jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
985 **----------------------------------------------------------------
987 struct launch restart;
989 /*----------------------------------------------------------------
990 ** If a data transfer phase is terminated too early
991 ** (after reception of a message (i.e. DISCONNECT)),
992 ** we have to prepare a mini script to transfer
993 ** the rest of the data.
994 **----------------------------------------------------------------
996 ncrcmd patch[8];
998 /*----------------------------------------------------------------
999 ** The general SCSI driver provides a
1000 ** pointer to a control block.
1001 **----------------------------------------------------------------
1003 Scsi_Cmnd *cmd; /* SCSI command */
1004 u_char cdb_buf[16]; /* Copy of CDB */
1005 u_char sense_buf[64];
1006 int data_len; /* Total data length */
1008 /*----------------------------------------------------------------
1009 ** Message areas.
1010 ** We prepare a message to be sent after selection.
1011 ** We may use a second one if the command is rescheduled
1012 ** due to GETCC or QFULL.
1013 ** Contents are IDENTIFY and SIMPLE_TAG.
1014 ** While negotiating sync or wide transfer,
1015 ** a SDTR or WDTR message is appended.
1016 **----------------------------------------------------------------
1018 u_char scsi_smsg [8];
1019 u_char scsi_smsg2[8];
1021 /*----------------------------------------------------------------
1022 ** Other fields.
1023 **----------------------------------------------------------------
1025 u_long p_ccb; /* BUS address of this CCB */
1026 u_char sensecmd[6]; /* Sense command */
1027 u_char tag; /* Tag for this transfer */
1028 /* 255 means no tag */
1029 u_char target;
1030 u_char lun;
1031 u_char queued;
1032 u_char auto_sense;
1033 ccb_p link_ccb; /* Host adapter CCB chain */
1034 XPT_QUEHEAD link_ccbq; /* Link to unit CCB queue */
1035 u_int32 startp; /* Initial data pointer */
1036 u_long magic; /* Free / busy CCB flag */
1039 #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
1042 /*========================================================================
1044 ** Declaration of structs: NCR device descriptor
1046 **========================================================================
1048 struct ncb {
1049 /*----------------------------------------------------------------
1050 ** The global header.
1051 ** It is accessible to both the host and the script processor.
1052 ** Must be cache line size aligned (32 for x86) in order to
1053 ** allow cache line bursting when it is copied to/from CCB.
1054 **----------------------------------------------------------------
1056 struct head header;
1058 /*----------------------------------------------------------------
1059 ** CCBs management queues.
1060 **----------------------------------------------------------------
1062 Scsi_Cmnd *waiting_list; /* Commands waiting for a CCB */
1063 /* when lcb is not allocated. */
1064 Scsi_Cmnd *done_list; /* Commands waiting for done() */
1065 /* callback to be invoked. */
1066 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
1067 spinlock_t smp_lock; /* Lock for SMP threading */
1068 #endif
1070 /*----------------------------------------------------------------
1071 ** Chip and controller indentification.
1072 **----------------------------------------------------------------
1074 int unit; /* Unit number */
1075 char chip_name[8]; /* Chip name */
1076 char inst_name[16]; /* ncb instance name */
1078 /*----------------------------------------------------------------
1079 ** Initial value of some IO register bits.
1080 ** These values are assumed to have been set by BIOS, and may
1081 ** be used for probing adapter implementation differences.
1082 **----------------------------------------------------------------
1084 u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
1085 sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
1087 /*----------------------------------------------------------------
1088 ** Actual initial value of IO register bits used by the
1089 ** driver. They are loaded at initialisation according to
1090 ** features that are to be enabled.
1091 **----------------------------------------------------------------
1093 u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4,
1094 rv_ctest5, rv_stest2;
1096 /*----------------------------------------------------------------
1097 ** Targets management.
1098 ** During reselection the ncr jumps to jump_tcb.
1099 ** The SFBR register is loaded with the encoded target id.
1100 ** For i = 0 to 3
1101 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
1103 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1104 ** It is kind of hashcoding.
1105 **----------------------------------------------------------------
1107 struct link jump_tcb[4]; /* JUMPs for reselection */
1108 struct tcb target[MAX_TARGET]; /* Target data */
1110 /*----------------------------------------------------------------
1111 ** Virtual and physical bus addresses of the chip.
1112 **----------------------------------------------------------------
1114 vm_offset_t vaddr; /* Virtual and bus address of */
1115 vm_offset_t paddr; /* chip's IO registers. */
1116 vm_offset_t paddr2; /* On-chip RAM bus address. */
1117 volatile /* Pointer to volatile for */
1118 struct ncr_reg *reg; /* memory mapped IO. */
1120 /*----------------------------------------------------------------
1121 ** SCRIPTS virtual and physical bus addresses.
1122 ** 'script' is loaded in the on-chip RAM if present.
1123 ** 'scripth' stays in main memory.
1124 **----------------------------------------------------------------
1126 struct script *script0; /* Copies of script and scripth */
1127 struct scripth *scripth0; /* relocated for this ncb. */
1128 struct scripth *scripth; /* Actual scripth virt. address */
1129 u_long p_script; /* Actual script and scripth */
1130 u_long p_scripth; /* bus addresses. */
1132 /*----------------------------------------------------------------
1133 ** General controller parameters and configuration.
1134 **----------------------------------------------------------------
1136 pcidev_t pdev;
1137 u_short device_id; /* PCI device id */
1138 u_char revision_id; /* PCI device revision id */
1139 u_char bus; /* PCI BUS number */
1140 u_char device_fn; /* PCI BUS device and function */
1141 u_long base_io; /* IO space base address */
1142 u_int irq; /* IRQ level */
1143 u_int features; /* Chip features map */
1144 u_char myaddr; /* SCSI id of the adapter */
1145 u_char maxburst; /* log base 2 of dwords burst */
1146 u_char maxwide; /* Maximum transfer width */
1147 u_char minsync; /* Minimum sync period factor */
1148 u_char maxsync; /* Maximum sync period factor */
1149 u_char maxoffs; /* Max scsi offset */
1150 u_char multiplier; /* Clock multiplier (1,2,4) */
1151 u_char clock_divn; /* Number of clock divisors */
1152 u_long clock_khz; /* SCSI clock frequency in KHz */
1154 /*----------------------------------------------------------------
1155 ** Start queue management.
1156 ** It is filled up by the host processor and accessed by the
1157 ** SCRIPTS processor in order to start SCSI commands.
1158 **----------------------------------------------------------------
1160 u_short squeueput; /* Next free slot of the queue */
1161 u_short actccbs; /* Number of allocated CCBs */
1162 u_short queuedccbs; /* Number of CCBs in start queue*/
1163 u_short queuedepth; /* Start queue depth */
1165 /*----------------------------------------------------------------
1166 ** Timeout handler.
1167 **----------------------------------------------------------------
1169 struct timer_list timer; /* Timer handler link header */
1170 u_long lasttime;
1171 u_long settle_time; /* Resetting the SCSI BUS */
1173 /*----------------------------------------------------------------
1174 ** Debugging and profiling.
1175 **----------------------------------------------------------------
1177 struct ncr_reg regdump; /* Register dump */
1178 u_long regtime; /* Time it has been done */
1179 #ifdef SCSI_NCR_PROFILE_SUPPORT
1180 struct profile profile; /* Profiling data */
1181 u_int disc_phys; /* Disconnection counters */
1182 u_int disc_ref;
1183 #endif
1185 /*----------------------------------------------------------------
1186 ** Miscellaneous buffers accessed by the scripts-processor.
1187 ** They shall be DWORD aligned, because they may be read or
1188 ** written with a SCR_COPY script command.
1189 **----------------------------------------------------------------
1191 u_char msgout[8]; /* Buffer for MESSAGE OUT */
1192 u_char msgin [8]; /* Buffer for MESSAGE IN */
1193 u_int32 lastmsg; /* Last SCSI message sent */
1194 u_char scratch; /* Scratch for SCSI receive */
1196 /*----------------------------------------------------------------
1197 ** Miscellaneous configuration and status parameters.
1198 **----------------------------------------------------------------
1200 u_char disc; /* Diconnection allowed */
1201 u_char scsi_mode; /* Current SCSI BUS mode */
1202 u_char order; /* Tag order to use */
1203 u_char verbose; /* Verbosity for this controller*/
1204 int ncr_cache; /* Used for cache test at init. */
1205 u_long p_ncb; /* BUS address of this NCB */
1207 /*----------------------------------------------------------------
1208 ** Command completion handling.
1209 **----------------------------------------------------------------
1211 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1212 struct ccb *(ccb_done[MAX_DONE]);
1213 int ccb_done_ic;
1214 #endif
1215 /*----------------------------------------------------------------
1216 ** Fields that should be removed or changed.
1217 **----------------------------------------------------------------
1219 #ifdef SCSI_NCR_PROFILE_SUPPORT
1220 u_long ktime; /* Copy of kernel time */
1221 #endif
1222 struct ccb *ccb; /* Global CCB */
1223 struct usrcmd user; /* Command from user */
1224 u_char release_stage; /* Synchronisation stage on release */
1226 #ifdef SCSI_NCR_INTEGRITY_CHECKING
1227 /*----------------------------------------------------------------
1228 ** Fields that are used for integrity check
1229 **----------------------------------------------------------------
1231 unsigned char check_integrity; /* Enable midlayer integ.check on
1232 * bus scan. */
1233 unsigned char check_integ_par; /* Set if par or Init. Det. error
1234 * used only during integ check */
1235 #endif
1238 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1239 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1241 /*==========================================================
1244 ** Script for NCR-Processor.
1246 ** Use ncr_script_fill() to create the variable parts.
1247 ** Use ncr_script_copy_and_bind() to make a copy and
1248 ** bind to physical addresses.
1251 **==========================================================
1253 ** We have to know the offsets of all labels before
1254 ** we reach them (for forward jumps).
1255 ** Therefore we declare a struct here.
1256 ** If you make changes inside the script,
1257 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1259 **----------------------------------------------------------
1263 ** Script fragments which are loaded into the on-chip RAM
1264 ** of 825A, 875 and 895 chips.
1266 struct script {
1267 ncrcmd start [ 5];
1268 ncrcmd startpos [ 1];
1269 ncrcmd select [ 6];
1270 ncrcmd select2 [ 9];
1271 ncrcmd loadpos [ 4];
1272 ncrcmd send_ident [ 9];
1273 ncrcmd prepare [ 6];
1274 ncrcmd prepare2 [ 7];
1275 #ifdef SCSI_NCR_PROFILE_SUPPORT
1276 ncrcmd command [ 9];
1277 #else
1278 ncrcmd command [ 6];
1279 #endif
1280 ncrcmd dispatch [ 32];
1281 ncrcmd clrack [ 4];
1282 ncrcmd no_data [ 17];
1283 #ifdef SCSI_NCR_PROFILE_SUPPORT
1284 ncrcmd status [ 11];
1285 #else
1286 ncrcmd status [ 8];
1287 #endif
1288 ncrcmd msg_in [ 2];
1289 ncrcmd msg_in2 [ 16];
1290 ncrcmd msg_bad [ 4];
1291 ncrcmd setmsg [ 7];
1292 ncrcmd cleanup [ 6];
1293 ncrcmd complete [ 9];
1294 ncrcmd cleanup_ok [ 8];
1295 ncrcmd cleanup0 [ 1];
1296 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
1297 ncrcmd signal [ 12];
1298 #else
1299 ncrcmd signal [ 9];
1300 ncrcmd done_pos [ 1];
1301 ncrcmd done_plug [ 2];
1302 ncrcmd done_end [ 7];
1303 #endif
1304 ncrcmd save_dp [ 7];
1305 ncrcmd restore_dp [ 5];
1306 #ifdef SCSI_NCR_PROFILE_SUPPORT
1307 ncrcmd disconnect [ 28];
1308 #else
1309 ncrcmd disconnect [ 17];
1310 #endif
1311 ncrcmd msg_out [ 9];
1312 ncrcmd msg_out_done [ 7];
1313 ncrcmd idle [ 2];
1314 ncrcmd reselect [ 8];
1315 ncrcmd reselected [ 8];
1316 ncrcmd resel_dsa [ 6];
1317 #ifdef SCSI_NCR_PROFILE_SUPPORT
1318 ncrcmd loadpos1 [ 7];
1319 #else
1320 ncrcmd loadpos1 [ 4];
1321 #endif
1322 ncrcmd resel_lun [ 6];
1323 ncrcmd resel_tag [ 6];
1324 ncrcmd jump_to_nexus [ 4];
1325 ncrcmd nexus_indirect [ 4];
1326 ncrcmd resel_notag [ 4];
1327 ncrcmd data_in [MAX_SCATTERL * 4];
1328 ncrcmd data_in2 [ 4];
1329 ncrcmd data_out [MAX_SCATTERL * 4];
1330 ncrcmd data_out2 [ 4];
1334 ** Script fragments which stay in main memory for all chips.
1336 struct scripth {
1337 ncrcmd tryloop [MAX_START*2];
1338 ncrcmd tryloop2 [ 2];
1339 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1340 ncrcmd done_queue [MAX_DONE*5];
1341 ncrcmd done_queue2 [ 2];
1342 #endif
1343 ncrcmd select_no_atn [ 8];
1344 ncrcmd cancel [ 4];
1345 ncrcmd skip [ 9];
1346 ncrcmd skip2 [ 19];
1347 ncrcmd par_err_data_in [ 6];
1348 ncrcmd par_err_other [ 4];
1349 ncrcmd msg_reject [ 8];
1350 ncrcmd msg_ign_residue [ 24];
1351 ncrcmd msg_extended [ 10];
1352 ncrcmd msg_ext_2 [ 10];
1353 ncrcmd msg_wdtr [ 14];
1354 ncrcmd send_wdtr [ 7];
1355 ncrcmd msg_ext_3 [ 10];
1356 ncrcmd msg_sdtr [ 14];
1357 ncrcmd send_sdtr [ 7];
1358 ncrcmd nego_bad_phase [ 4];
1359 ncrcmd msg_out_abort [ 10];
1360 ncrcmd hdata_in [MAX_SCATTERH * 4];
1361 ncrcmd hdata_in2 [ 2];
1362 ncrcmd hdata_out [MAX_SCATTERH * 4];
1363 ncrcmd hdata_out2 [ 2];
1364 ncrcmd reset [ 4];
1365 ncrcmd aborttag [ 4];
1366 ncrcmd abort [ 2];
1367 ncrcmd abort_resel [ 20];
1368 ncrcmd resend_ident [ 4];
1369 ncrcmd clratn_go_on [ 3];
1370 ncrcmd nxtdsp_go_on [ 1];
1371 ncrcmd sdata_in [ 8];
1372 ncrcmd data_io [ 18];
1373 ncrcmd bad_identify [ 12];
1374 ncrcmd bad_i_t_l [ 4];
1375 ncrcmd bad_i_t_l_q [ 4];
1376 ncrcmd bad_target [ 8];
1377 ncrcmd bad_status [ 8];
1378 ncrcmd start_ram [ 4];
1379 ncrcmd start_ram0 [ 4];
1380 ncrcmd sto_restart [ 5];
1381 ncrcmd snooptest [ 9];
1382 ncrcmd snoopend [ 2];
1385 /*==========================================================
1388 ** Function headers.
1391 **==========================================================
1394 static void ncr_alloc_ccb (ncb_p np, u_char tn, u_char ln);
1395 static void ncr_complete (ncb_p np, ccb_p cp);
1396 static void ncr_exception (ncb_p np);
1397 static void ncr_free_ccb (ncb_p np, ccb_p cp);
1398 static void ncr_init_ccb (ncb_p np, ccb_p cp);
1399 static void ncr_init_tcb (ncb_p np, u_char tn);
1400 static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln);
1401 static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln,
1402 u_char *inq_data);
1403 static void ncr_getclock (ncb_p np, int mult);
1404 static void ncr_selectclock (ncb_p np, u_char scntl3);
1405 static ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln);
1406 static void ncr_init (ncb_p np, int reset, char * msg, u_long code);
1407 static int ncr_int_sbmc (ncb_p np);
1408 static int ncr_int_par (ncb_p np);
1409 static void ncr_int_ma (ncb_p np);
1410 static void ncr_int_sir (ncb_p np);
1411 static void ncr_int_sto (ncb_p np);
1412 static u_long ncr_lookup (char* id);
1413 static void ncr_negotiate (struct ncb* np, struct tcb* tp);
1414 static int ncr_prepare_nego(ncb_p np, ccb_p cp, u_char *msgptr);
1415 #ifdef SCSI_NCR_INTEGRITY_CHECKING
1416 static int ncr_ic_nego(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd, u_char *msgptr);
1417 #endif
1419 #ifdef SCSI_NCR_PROFILE_SUPPORT
1420 static void ncb_profile (ncb_p np, ccb_p cp);
1421 #endif
1423 static void ncr_script_copy_and_bind
1424 (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
1425 static void ncr_script_fill (struct script * scr, struct scripth * scripth);
1426 static int ncr_scatter (ncb_p np, ccb_p cp, Scsi_Cmnd *cmd);
1427 static void ncr_getsync (ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p);
1428 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer);
1429 static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln);
1430 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack);
1431 static int ncr_show_msg (u_char * msg);
1432 static void ncr_print_msg (ccb_p cp, char *label, u_char *msg);
1433 static int ncr_snooptest (ncb_p np);
1434 static void ncr_timeout (ncb_p np);
1435 static void ncr_wakeup (ncb_p np, u_long code);
1436 static void ncr_wakeup_done (ncb_p np);
1437 static void ncr_start_next_ccb (ncb_p np, lcb_p lp, int maxn);
1438 static void ncr_put_start_queue(ncb_p np, ccb_p cp);
1439 static void ncr_start_reset (ncb_p np);
1440 static int ncr_reset_scsi_bus (ncb_p np, int enab_int, int settle_delay);
1442 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
1443 static void ncr_usercmd (ncb_p np);
1444 #endif
1446 static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device);
1448 static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd);
1449 static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd);
1450 static void process_waiting_list(ncb_p np, int sts);
1452 #define remove_from_waiting_list(np, cmd) \
1453 retrieve_from_waiting_list(1, (np), (cmd))
1454 #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
1455 #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
1457 static inline char *ncr_name (ncb_p np)
1459 return np->inst_name;
1463 /*==========================================================
1466 ** Scripts for NCR-Processor.
1468 ** Use ncr_script_bind for binding to physical addresses.
1471 **==========================================================
1473 ** NADDR generates a reference to a field of the controller data.
1474 ** PADDR generates a reference to another part of the script.
1475 ** RADDR generates a reference to a script processor register.
1476 ** FADDR generates a reference to a script processor register
1477 ** with offset.
1479 **----------------------------------------------------------
1482 #define RELOC_SOFTC 0x40000000
1483 #define RELOC_LABEL 0x50000000
1484 #define RELOC_REGISTER 0x60000000
1485 #if 0
1486 #define RELOC_KVAR 0x70000000
1487 #endif
1488 #define RELOC_LABELH 0x80000000
1489 #define RELOC_MASK 0xf0000000
1491 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
1492 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
1493 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
1494 #define RADDR(label) (RELOC_REGISTER | REG(label))
1495 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
1496 #if 0
1497 #define KVAR(which) (RELOC_KVAR | (which))
1498 #endif
1500 #if 0
1501 #define SCRIPT_KVAR_JIFFIES (0)
1502 #define SCRIPT_KVAR_FIRST SCRIPT_KVAR_JIFFIES
1503 #define SCRIPT_KVAR_LAST SCRIPT_KVAR_JIFFIES
1505 * Kernel variables referenced in the scripts.
1506 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
1508 static void *script_kvars[] __initdata =
1509 { (void *)&jiffies };
1510 #endif
1512 static struct script script0 __initdata = {
1513 /*--------------------------< START >-----------------------*/ {
1515 ** This NOP will be patched with LED ON
1516 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
1518 SCR_NO_OP,
1521 ** Clear SIGP.
1523 SCR_FROM_REG (ctest2),
1526 ** Then jump to a certain point in tryloop.
1527 ** Due to the lack of indirect addressing the code
1528 ** is self modifying here.
1530 SCR_JUMP,
1531 }/*-------------------------< STARTPOS >--------------------*/,{
1532 PADDRH(tryloop),
1534 }/*-------------------------< SELECT >----------------------*/,{
1536 ** DSA contains the address of a scheduled
1537 ** data structure.
1539 ** SCRATCHA contains the address of the script,
1540 ** which starts the next entry.
1542 ** Set Initiator mode.
1544 ** (Target mode is left as an exercise for the reader)
1547 SCR_CLR (SCR_TRG),
1549 SCR_LOAD_REG (HS_REG, HS_SELECTING),
1553 ** And try to select this target.
1555 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
1556 PADDR (reselect),
1558 }/*-------------------------< SELECT2 >----------------------*/,{
1560 ** Now there are 4 possibilities:
1562 ** (1) The ncr looses arbitration.
1563 ** This is ok, because it will try again,
1564 ** when the bus becomes idle.
1565 ** (But beware of the timeout function!)
1567 ** (2) The ncr is reselected.
1568 ** Then the script processor takes the jump
1569 ** to the RESELECT label.
1571 ** (3) The ncr wins arbitration.
1572 ** Then it will execute SCRIPTS instruction until
1573 ** the next instruction that checks SCSI phase.
1574 ** Then will stop and wait for selection to be
1575 ** complete or selection time-out to occur.
1576 ** As a result the SCRIPTS instructions until
1577 ** LOADPOS + 2 should be executed in parallel with
1578 ** the SCSI core performing selection.
1582 ** The M_REJECT problem seems to be due to a selection
1583 ** timing problem.
1584 ** Wait immediately for the selection to complete.
1585 ** (2.5x behaves so)
1587 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
1591 ** Next time use the next slot.
1593 SCR_COPY (4),
1594 RADDR (temp),
1595 PADDR (startpos),
1597 ** The ncr doesn't have an indirect load
1598 ** or store command. So we have to
1599 ** copy part of the control block to a
1600 ** fixed place, where we can access it.
1602 ** We patch the address part of a
1603 ** COPY command with the DSA-register.
1605 SCR_COPY_F (4),
1606 RADDR (dsa),
1607 PADDR (loadpos),
1609 ** then we do the actual copy.
1611 SCR_COPY (sizeof (struct head)),
1613 ** continued after the next label ...
1615 }/*-------------------------< LOADPOS >---------------------*/,{
1617 NADDR (header),
1619 ** Wait for the next phase or the selection
1620 ** to complete or time-out.
1622 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
1623 PADDR (prepare),
1625 }/*-------------------------< SEND_IDENT >----------------------*/,{
1627 ** Selection complete.
1628 ** Send the IDENTIFY and SIMPLE_TAG messages
1629 ** (and the M_X_SYNC_REQ message)
1631 SCR_MOVE_TBL ^ SCR_MSG_OUT,
1632 offsetof (struct dsb, smsg),
1633 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
1634 PADDRH (resend_ident),
1635 SCR_LOAD_REG (scratcha, 0x80),
1637 SCR_COPY (1),
1638 RADDR (scratcha),
1639 NADDR (lastmsg),
1640 }/*-------------------------< PREPARE >----------------------*/,{
1642 ** load the savep (saved pointer) into
1643 ** the TEMP register (actual pointer)
1645 SCR_COPY (4),
1646 NADDR (header.savep),
1647 RADDR (temp),
1649 ** Initialize the status registers
1651 SCR_COPY (4),
1652 NADDR (header.status),
1653 RADDR (scr0),
1654 }/*-------------------------< PREPARE2 >---------------------*/,{
1656 ** Initialize the msgout buffer with a NOOP message.
1658 SCR_LOAD_REG (scratcha, M_NOOP),
1660 SCR_COPY (1),
1661 RADDR (scratcha),
1662 NADDR (msgout),
1663 #if 0
1664 SCR_COPY (1),
1665 RADDR (scratcha),
1666 NADDR (msgin),
1667 #endif
1669 ** Anticipate the COMMAND phase.
1670 ** This is the normal case for initial selection.
1672 SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
1673 PADDR (dispatch),
1675 }/*-------------------------< COMMAND >--------------------*/,{
1676 #ifdef SCSI_NCR_PROFILE_SUPPORT
1678 ** ... set a timestamp ...
1680 SCR_COPY (sizeof (u_long)),
1681 NADDR (ktime),
1682 NADDR (header.stamp.command),
1683 #endif
1685 ** ... and send the command
1687 SCR_MOVE_TBL ^ SCR_COMMAND,
1688 offsetof (struct dsb, cmd),
1690 ** If status is still HS_NEGOTIATE, negotiation failed.
1691 ** We check this here, since we want to do that
1692 ** only once.
1694 SCR_FROM_REG (HS_REG),
1696 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
1697 SIR_NEGO_FAILED,
1699 }/*-----------------------< DISPATCH >----------------------*/,{
1701 ** MSG_IN is the only phase that shall be
1702 ** entered at least once for each (re)selection.
1703 ** So we test it first.
1705 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
1706 PADDR (msg_in),
1708 SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
1711 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
1712 ** Possible data corruption during Memory Write and Invalidate.
1713 ** This work-around resets the addressing logic prior to the
1714 ** start of the first MOVE of a DATA IN phase.
1715 ** (See README.ncr53c8xx for more information)
1717 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1719 SCR_COPY (4),
1720 RADDR (scratcha),
1721 RADDR (scratcha),
1722 SCR_RETURN,
1724 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
1725 PADDR (status),
1726 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
1727 PADDR (command),
1728 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
1729 PADDR (msg_out),
1731 ** Discard one illegal phase byte, if required.
1733 SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
1735 SCR_COPY (1),
1736 RADDR (scratcha),
1737 NADDR (xerr_st),
1738 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
1740 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
1741 NADDR (scratch),
1742 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
1744 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
1745 NADDR (scratch),
1746 SCR_JUMP,
1747 PADDR (dispatch),
1749 }/*-------------------------< CLRACK >----------------------*/,{
1751 ** Terminate possible pending message phase.
1753 SCR_CLR (SCR_ACK),
1755 SCR_JUMP,
1756 PADDR (dispatch),
1758 }/*-------------------------< NO_DATA >--------------------*/,{
1760 ** The target wants to tranfer too much data
1761 ** or in the wrong direction.
1762 ** Remember that in extended error.
1764 SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
1766 SCR_COPY (1),
1767 RADDR (scratcha),
1768 NADDR (xerr_st),
1770 ** Discard one data byte, if required.
1772 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
1774 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
1775 NADDR (scratch),
1776 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1778 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
1779 NADDR (scratch),
1781 ** .. and repeat as required.
1783 SCR_CALL,
1784 PADDR (dispatch),
1785 SCR_JUMP,
1786 PADDR (no_data),
1788 }/*-------------------------< STATUS >--------------------*/,{
1789 #ifdef SCSI_NCR_PROFILE_SUPPORT
1791 ** set the timestamp.
1793 SCR_COPY (sizeof (u_long)),
1794 NADDR (ktime),
1795 NADDR (header.stamp.status),
1796 #endif
1798 ** get the status
1800 SCR_MOVE_ABS (1) ^ SCR_STATUS,
1801 NADDR (scratch),
1803 ** save status to scsi_status.
1804 ** mark as complete.
1806 SCR_TO_REG (SS_REG),
1808 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1810 SCR_JUMP,
1811 PADDR (dispatch),
1812 }/*-------------------------< MSG_IN >--------------------*/,{
1814 ** Get the first byte of the message
1815 ** and save it to SCRATCHA.
1817 ** The script processor doesn't negate the
1818 ** ACK signal after this transfer.
1820 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
1821 NADDR (msgin[0]),
1822 }/*-------------------------< MSG_IN2 >--------------------*/,{
1824 ** Handle this message.
1826 SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
1827 PADDR (complete),
1828 SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
1829 PADDR (disconnect),
1830 SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
1831 PADDR (save_dp),
1832 SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
1833 PADDR (restore_dp),
1834 SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
1835 PADDRH (msg_extended),
1836 SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
1837 PADDR (clrack),
1838 SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
1839 PADDRH (msg_reject),
1840 SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
1841 PADDRH (msg_ign_residue),
1843 ** Rest of the messages left as
1844 ** an exercise ...
1846 ** Unimplemented messages:
1847 ** fall through to MSG_BAD.
1849 }/*-------------------------< MSG_BAD >------------------*/,{
1851 ** unimplemented message - reject it.
1853 SCR_INT,
1854 SIR_REJECT_SENT,
1855 SCR_LOAD_REG (scratcha, M_REJECT),
1857 }/*-------------------------< SETMSG >----------------------*/,{
1858 SCR_COPY (1),
1859 RADDR (scratcha),
1860 NADDR (msgout),
1861 SCR_SET (SCR_ATN),
1863 SCR_JUMP,
1864 PADDR (clrack),
1865 }/*-------------------------< CLEANUP >-------------------*/,{
1867 ** dsa: Pointer to ccb
1868 ** or xxxxxxFF (no ccb)
1870 ** HS_REG: Host-Status (<>0!)
1872 SCR_FROM_REG (dsa),
1874 SCR_JUMP ^ IFTRUE (DATA (0xff)),
1875 PADDR (start),
1877 ** dsa is valid.
1878 ** complete the cleanup.
1880 SCR_JUMP,
1881 PADDR (cleanup_ok),
1883 }/*-------------------------< COMPLETE >-----------------*/,{
1885 ** Complete message.
1887 ** Copy TEMP register to LASTP in header.
1889 SCR_COPY (4),
1890 RADDR (temp),
1891 NADDR (header.lastp),
1893 ** When we terminate the cycle by clearing ACK,
1894 ** the target may disconnect immediately.
1896 ** We don't want to be told of an
1897 ** "unexpected disconnect",
1898 ** so we disable this feature.
1900 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
1903 ** Terminate cycle ...
1905 SCR_CLR (SCR_ACK|SCR_ATN),
1908 ** ... and wait for the disconnect.
1910 SCR_WAIT_DISC,
1912 }/*-------------------------< CLEANUP_OK >----------------*/,{
1914 ** Save host status to header.
1916 SCR_COPY (4),
1917 RADDR (scr0),
1918 NADDR (header.status),
1920 ** and copy back the header to the ccb.
1922 SCR_COPY_F (4),
1923 RADDR (dsa),
1924 PADDR (cleanup0),
1925 SCR_COPY (sizeof (struct head)),
1926 NADDR (header),
1927 }/*-------------------------< CLEANUP0 >--------------------*/,{
1929 }/*-------------------------< SIGNAL >----------------------*/,{
1931 ** if job not completed ...
1933 SCR_FROM_REG (HS_REG),
1936 ** ... start the next command.
1938 SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
1939 PADDR(start),
1941 ** If command resulted in not GOOD status,
1942 ** call the C code if needed.
1944 SCR_FROM_REG (SS_REG),
1946 SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
1947 PADDRH (bad_status),
1949 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
1952 ** ... signal completion to the host
1954 SCR_INT_FLY,
1957 ** Auf zu neuen Schandtaten!
1959 SCR_JUMP,
1960 PADDR(start),
1962 #else /* defined SCSI_NCR_CCB_DONE_SUPPORT */
1965 ** ... signal completion to the host
1967 SCR_JUMP,
1968 }/*------------------------< DONE_POS >---------------------*/,{
1969 PADDRH (done_queue),
1970 }/*------------------------< DONE_PLUG >--------------------*/,{
1971 SCR_INT,
1972 SIR_DONE_OVERFLOW,
1973 }/*------------------------< DONE_END >---------------------*/,{
1974 SCR_INT_FLY,
1976 SCR_COPY (4),
1977 RADDR (temp),
1978 PADDR (done_pos),
1979 SCR_JUMP,
1980 PADDR (start),
1982 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
1984 }/*-------------------------< SAVE_DP >------------------*/,{
1986 ** SAVE_DP message:
1987 ** Copy TEMP register to SAVEP in header.
1989 SCR_COPY (4),
1990 RADDR (temp),
1991 NADDR (header.savep),
1992 SCR_CLR (SCR_ACK),
1994 SCR_JUMP,
1995 PADDR (dispatch),
1996 }/*-------------------------< RESTORE_DP >---------------*/,{
1998 ** RESTORE_DP message:
1999 ** Copy SAVEP in header to TEMP register.
2001 SCR_COPY (4),
2002 NADDR (header.savep),
2003 RADDR (temp),
2004 SCR_JUMP,
2005 PADDR (clrack),
2007 }/*-------------------------< DISCONNECT >---------------*/,{
2009 ** DISCONNECTing ...
2011 ** disable the "unexpected disconnect" feature,
2012 ** and remove the ACK signal.
2014 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2016 SCR_CLR (SCR_ACK|SCR_ATN),
2019 ** Wait for the disconnect.
2021 SCR_WAIT_DISC,
2023 #ifdef SCSI_NCR_PROFILE_SUPPORT
2025 ** Profiling:
2026 ** Set a time stamp,
2027 ** and count the disconnects.
2029 SCR_COPY (sizeof (u_long)),
2030 NADDR (ktime),
2031 NADDR (header.stamp.disconnect),
2032 SCR_COPY (4),
2033 NADDR (disc_phys),
2034 RADDR (scratcha),
2035 SCR_REG_REG (scratcha, SCR_ADD, 0x01),
2037 SCR_COPY (4),
2038 RADDR (scratcha),
2039 NADDR (disc_phys),
2040 #endif
2042 ** Status is: DISCONNECTED.
2044 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2047 ** If QUIRK_AUTOSAVE is set,
2048 ** do an "save pointer" operation.
2050 SCR_FROM_REG (QU_REG),
2052 SCR_JUMP ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
2053 PADDR (cleanup_ok),
2055 ** like SAVE_DP message:
2056 ** Copy TEMP register to SAVEP in header.
2058 SCR_COPY (4),
2059 RADDR (temp),
2060 NADDR (header.savep),
2061 SCR_JUMP,
2062 PADDR (cleanup_ok),
2064 }/*-------------------------< MSG_OUT >-------------------*/,{
2066 ** The target requests a message.
2068 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2069 NADDR (msgout),
2070 SCR_COPY (1),
2071 NADDR (msgout),
2072 NADDR (lastmsg),
2074 ** If it was no ABORT message ...
2076 SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
2077 PADDRH (msg_out_abort),
2079 ** ... wait for the next phase
2080 ** if it's a message out, send it again, ...
2082 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2083 PADDR (msg_out),
2084 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2086 ** ... else clear the message ...
2088 SCR_LOAD_REG (scratcha, M_NOOP),
2090 SCR_COPY (4),
2091 RADDR (scratcha),
2092 NADDR (msgout),
2094 ** ... and process the next phase
2096 SCR_JUMP,
2097 PADDR (dispatch),
2098 }/*-------------------------< IDLE >------------------------*/,{
2100 ** Nothing to do?
2101 ** Wait for reselect.
2102 ** This NOP will be patched with LED OFF
2103 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2105 SCR_NO_OP,
2107 }/*-------------------------< RESELECT >--------------------*/,{
2109 ** make the DSA invalid.
2111 SCR_LOAD_REG (dsa, 0xff),
2113 SCR_CLR (SCR_TRG),
2115 SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
2118 ** Sleep waiting for a reselection.
2119 ** If SIGP is set, special treatment.
2121 ** Zu allem bereit ..
2123 SCR_WAIT_RESEL,
2124 PADDR(start),
2125 }/*-------------------------< RESELECTED >------------------*/,{
2127 ** This NOP will be patched with LED ON
2128 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2130 SCR_NO_OP,
2133 ** ... zu nichts zu gebrauchen ?
2135 ** load the target id into the SFBR
2136 ** and jump to the control block.
2138 ** Look at the declarations of
2139 ** - struct ncb
2140 ** - struct tcb
2141 ** - struct lcb
2142 ** - struct ccb
2143 ** to understand what's going on.
2145 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2147 SCR_TO_REG (sdid),
2149 SCR_JUMP,
2150 NADDR (jump_tcb),
2152 }/*-------------------------< RESEL_DSA >-------------------*/,{
2154 ** Ack the IDENTIFY or TAG previously received.
2156 SCR_CLR (SCR_ACK),
2159 ** The ncr doesn't have an indirect load
2160 ** or store command. So we have to
2161 ** copy part of the control block to a
2162 ** fixed place, where we can access it.
2164 ** We patch the address part of a
2165 ** COPY command with the DSA-register.
2167 SCR_COPY_F (4),
2168 RADDR (dsa),
2169 PADDR (loadpos1),
2171 ** then we do the actual copy.
2173 SCR_COPY (sizeof (struct head)),
2175 ** continued after the next label ...
2178 }/*-------------------------< LOADPOS1 >-------------------*/,{
2180 NADDR (header),
2181 #ifdef SCSI_NCR_PROFILE_SUPPORT
2183 ** Set a time stamp for this reselection
2185 SCR_COPY (sizeof (u_long)),
2186 NADDR (ktime),
2187 NADDR (header.stamp.reselect),
2188 #endif
2190 ** The DSA contains the data structure address.
2192 SCR_JUMP,
2193 PADDR (prepare),
2195 }/*-------------------------< RESEL_LUN >-------------------*/,{
2197 ** come back to this point
2198 ** to get an IDENTIFY message
2199 ** Wait for a msg_in phase.
2201 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2202 SIR_RESEL_NO_MSG_IN,
2204 ** message phase.
2205 ** Read the data directly from the BUS DATA lines.
2206 ** This helps to support very old SCSI devices that
2207 ** may reselect without sending an IDENTIFY.
2209 SCR_FROM_REG (sbdl),
2212 ** It should be an Identify message.
2214 SCR_RETURN,
2216 }/*-------------------------< RESEL_TAG >-------------------*/,{
2218 ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
2219 ** Agressive optimization, is'nt it?
2220 ** No need to test the SIMPLE TAG message, since the
2221 ** driver only supports conformant devices for tags. ;-)
2223 SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
2224 NADDR (msgin),
2226 ** Read the TAG from the SIDL.
2227 ** Still an aggressive optimization. ;-)
2228 ** Compute the CCB indirect jump address which
2229 ** is (#TAG*2 & 0xfc) due to tag numbering using
2230 ** 1,3,5..MAXTAGS*2+1 actual values.
2232 SCR_REG_SFBR (sidl, SCR_SHL, 0),
2234 SCR_SFBR_REG (temp, SCR_AND, 0xfc),
2236 }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
2237 SCR_COPY_F (4),
2238 RADDR (temp),
2239 PADDR (nexus_indirect),
2240 SCR_COPY (4),
2241 }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
2243 RADDR (temp),
2244 SCR_RETURN,
2246 }/*-------------------------< RESEL_NOTAG >-------------------*/,{
2248 ** No tag expected.
2249 ** Read an throw away the IDENTIFY.
2251 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2252 NADDR (msgin),
2253 SCR_JUMP,
2254 PADDR (jump_to_nexus),
2255 }/*-------------------------< DATA_IN >--------------------*/,{
2257 ** Because the size depends on the
2258 ** #define MAX_SCATTERL parameter,
2259 ** it is filled in at runtime.
2261 ** ##===========< i=0; i<MAX_SCATTERL >=========
2262 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2263 ** || PADDR (dispatch),
2264 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2265 ** || offsetof (struct dsb, data[ i]),
2266 ** ##==========================================
2268 **---------------------------------------------------------
2271 }/*-------------------------< DATA_IN2 >-------------------*/,{
2272 SCR_CALL,
2273 PADDR (dispatch),
2274 SCR_JUMP,
2275 PADDR (no_data),
2276 }/*-------------------------< DATA_OUT >--------------------*/,{
2278 ** Because the size depends on the
2279 ** #define MAX_SCATTERL parameter,
2280 ** it is filled in at runtime.
2282 ** ##===========< i=0; i<MAX_SCATTERL >=========
2283 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2284 ** || PADDR (dispatch),
2285 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2286 ** || offsetof (struct dsb, data[ i]),
2287 ** ##==========================================
2289 **---------------------------------------------------------
2292 }/*-------------------------< DATA_OUT2 >-------------------*/,{
2293 SCR_CALL,
2294 PADDR (dispatch),
2295 SCR_JUMP,
2296 PADDR (no_data),
2297 }/*--------------------------------------------------------*/
2300 static struct scripth scripth0 __initdata = {
2301 /*-------------------------< TRYLOOP >---------------------*/{
2303 ** Start the next entry.
2304 ** Called addresses point to the launch script in the CCB.
2305 ** They are patched by the main processor.
2307 ** Because the size depends on the
2308 ** #define MAX_START parameter, it is filled
2309 ** in at runtime.
2311 **-----------------------------------------------------------
2313 ** ##===========< I=0; i<MAX_START >===========
2314 ** || SCR_CALL,
2315 ** || PADDR (idle),
2316 ** ##==========================================
2318 **-----------------------------------------------------------
2321 }/*------------------------< TRYLOOP2 >---------------------*/,{
2322 SCR_JUMP,
2323 PADDRH(tryloop),
2325 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2327 }/*------------------------< DONE_QUEUE >-------------------*/,{
2329 ** Copy the CCB address to the next done entry.
2330 ** Because the size depends on the
2331 ** #define MAX_DONE parameter, it is filled
2332 ** in at runtime.
2334 **-----------------------------------------------------------
2336 ** ##===========< I=0; i<MAX_DONE >===========
2337 ** || SCR_COPY (sizeof(ccb_p)),
2338 ** || NADDR (header.cp),
2339 ** || NADDR (ccb_done[i]),
2340 ** || SCR_CALL,
2341 ** || PADDR (done_end),
2342 ** ##==========================================
2344 **-----------------------------------------------------------
2347 }/*------------------------< DONE_QUEUE2 >------------------*/,{
2348 SCR_JUMP,
2349 PADDRH (done_queue),
2351 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2352 }/*------------------------< SELECT_NO_ATN >-----------------*/,{
2354 ** Set Initiator mode.
2355 ** And try to select this target without ATN.
2358 SCR_CLR (SCR_TRG),
2360 SCR_LOAD_REG (HS_REG, HS_SELECTING),
2362 SCR_SEL_TBL ^ offsetof (struct dsb, select),
2363 PADDR (reselect),
2364 SCR_JUMP,
2365 PADDR (select2),
2367 }/*-------------------------< CANCEL >------------------------*/,{
2369 SCR_LOAD_REG (scratcha, HS_ABORTED),
2371 SCR_JUMPR,
2373 }/*-------------------------< SKIP >------------------------*/,{
2374 SCR_LOAD_REG (scratcha, 0),
2377 ** This entry has been canceled.
2378 ** Next time use the next slot.
2380 SCR_COPY (4),
2381 RADDR (temp),
2382 PADDR (startpos),
2384 ** The ncr doesn't have an indirect load
2385 ** or store command. So we have to
2386 ** copy part of the control block to a
2387 ** fixed place, where we can access it.
2389 ** We patch the address part of a
2390 ** COPY command with the DSA-register.
2392 SCR_COPY_F (4),
2393 RADDR (dsa),
2394 PADDRH (skip2),
2396 ** then we do the actual copy.
2398 SCR_COPY (sizeof (struct head)),
2400 ** continued after the next label ...
2402 }/*-------------------------< SKIP2 >---------------------*/,{
2404 NADDR (header),
2406 ** Initialize the status registers
2408 SCR_COPY (4),
2409 NADDR (header.status),
2410 RADDR (scr0),
2412 ** Force host status.
2414 SCR_FROM_REG (scratcha),
2416 SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
2418 SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
2420 SCR_JUMPR,
2422 SCR_TO_REG (HS_REG),
2424 SCR_LOAD_REG (SS_REG, S_GOOD),
2426 SCR_JUMP,
2427 PADDR (cleanup_ok),
2429 },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
2431 ** Ignore all data in byte, until next phase
2433 SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2434 PADDRH (par_err_other),
2435 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2436 NADDR (scratch),
2437 SCR_JUMPR,
2438 -24,
2439 },/*-------------------------< PAR_ERR_OTHER >------------------*/{
2441 ** count it.
2443 SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
2446 ** jump to dispatcher.
2448 SCR_JUMP,
2449 PADDR (dispatch),
2450 }/*-------------------------< MSG_REJECT >---------------*/,{
2452 ** If a negotiation was in progress,
2453 ** negotiation failed.
2454 ** Otherwise, let the C code print
2455 ** some message.
2457 SCR_FROM_REG (HS_REG),
2459 SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
2460 SIR_REJECT_RECEIVED,
2461 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2462 SIR_NEGO_FAILED,
2463 SCR_JUMP,
2464 PADDR (clrack),
2466 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2468 ** Terminate cycle
2470 SCR_CLR (SCR_ACK),
2472 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2473 PADDR (dispatch),
2475 ** get residue size.
2477 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2478 NADDR (msgin[1]),
2480 ** Size is 0 .. ignore message.
2482 SCR_JUMP ^ IFTRUE (DATA (0)),
2483 PADDR (clrack),
2485 ** Size is not 1 .. have to interrupt.
2487 SCR_JUMPR ^ IFFALSE (DATA (1)),
2490 ** Check for residue byte in swide register
2492 SCR_FROM_REG (scntl2),
2494 SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2497 ** There IS data in the swide register.
2498 ** Discard it.
2500 SCR_REG_REG (scntl2, SCR_OR, WSR),
2502 SCR_JUMP,
2503 PADDR (clrack),
2505 ** Load again the size to the sfbr register.
2507 SCR_FROM_REG (scratcha),
2509 SCR_INT,
2510 SIR_IGN_RESIDUE,
2511 SCR_JUMP,
2512 PADDR (clrack),
2514 }/*-------------------------< MSG_EXTENDED >-------------*/,{
2516 ** Terminate cycle
2518 SCR_CLR (SCR_ACK),
2520 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2521 PADDR (dispatch),
2523 ** get length.
2525 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2526 NADDR (msgin[1]),
2529 SCR_JUMP ^ IFTRUE (DATA (3)),
2530 PADDRH (msg_ext_3),
2531 SCR_JUMP ^ IFFALSE (DATA (2)),
2532 PADDR (msg_bad),
2533 }/*-------------------------< MSG_EXT_2 >----------------*/,{
2534 SCR_CLR (SCR_ACK),
2536 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2537 PADDR (dispatch),
2539 ** get extended message code.
2541 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2542 NADDR (msgin[2]),
2543 SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
2544 PADDRH (msg_wdtr),
2546 ** unknown extended message
2548 SCR_JUMP,
2549 PADDR (msg_bad)
2550 }/*-------------------------< MSG_WDTR >-----------------*/,{
2551 SCR_CLR (SCR_ACK),
2553 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2554 PADDR (dispatch),
2556 ** get data bus width
2558 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2559 NADDR (msgin[3]),
2561 ** let the host do the real work.
2563 SCR_INT,
2564 SIR_NEGO_WIDE,
2566 ** let the target fetch our answer.
2568 SCR_SET (SCR_ATN),
2570 SCR_CLR (SCR_ACK),
2572 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2573 PADDRH (nego_bad_phase),
2575 }/*-------------------------< SEND_WDTR >----------------*/,{
2577 ** Send the M_X_WIDE_REQ
2579 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
2580 NADDR (msgout),
2581 SCR_COPY (1),
2582 NADDR (msgout),
2583 NADDR (lastmsg),
2584 SCR_JUMP,
2585 PADDR (msg_out_done),
2587 }/*-------------------------< MSG_EXT_3 >----------------*/,{
2588 SCR_CLR (SCR_ACK),
2590 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2591 PADDR (dispatch),
2593 ** get extended message code.
2595 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2596 NADDR (msgin[2]),
2597 SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
2598 PADDRH (msg_sdtr),
2600 ** unknown extended message
2602 SCR_JUMP,
2603 PADDR (msg_bad)
2605 }/*-------------------------< MSG_SDTR >-----------------*/,{
2606 SCR_CLR (SCR_ACK),
2608 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2609 PADDR (dispatch),
2611 ** get period and offset
2613 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
2614 NADDR (msgin[3]),
2616 ** let the host do the real work.
2618 SCR_INT,
2619 SIR_NEGO_SYNC,
2621 ** let the target fetch our answer.
2623 SCR_SET (SCR_ATN),
2625 SCR_CLR (SCR_ACK),
2627 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2628 PADDRH (nego_bad_phase),
2630 }/*-------------------------< SEND_SDTR >-------------*/,{
2632 ** Send the M_X_SYNC_REQ
2634 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
2635 NADDR (msgout),
2636 SCR_COPY (1),
2637 NADDR (msgout),
2638 NADDR (lastmsg),
2639 SCR_JUMP,
2640 PADDR (msg_out_done),
2642 }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
2643 SCR_INT,
2644 SIR_NEGO_PROTO,
2645 SCR_JUMP,
2646 PADDR (dispatch),
2648 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
2650 ** After ABORT message,
2652 ** expect an immediate disconnect, ...
2654 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2656 SCR_CLR (SCR_ACK|SCR_ATN),
2658 SCR_WAIT_DISC,
2661 ** ... and set the status to "ABORTED"
2663 SCR_LOAD_REG (HS_REG, HS_ABORTED),
2665 SCR_JUMP,
2666 PADDR (cleanup),
2668 }/*-------------------------< HDATA_IN >-------------------*/,{
2670 ** Because the size depends on the
2671 ** #define MAX_SCATTERH parameter,
2672 ** it is filled in at runtime.
2674 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
2675 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2676 ** || PADDR (dispatch),
2677 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2678 ** || offsetof (struct dsb, data[ i]),
2679 ** ##===================================================
2681 **---------------------------------------------------------
2684 }/*-------------------------< HDATA_IN2 >------------------*/,{
2685 SCR_JUMP,
2686 PADDR (data_in),
2688 }/*-------------------------< HDATA_OUT >-------------------*/,{
2690 ** Because the size depends on the
2691 ** #define MAX_SCATTERH parameter,
2692 ** it is filled in at runtime.
2694 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
2695 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2696 ** || PADDR (dispatch),
2697 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2698 ** || offsetof (struct dsb, data[ i]),
2699 ** ##===================================================
2701 **---------------------------------------------------------
2704 }/*-------------------------< HDATA_OUT2 >------------------*/,{
2705 SCR_JUMP,
2706 PADDR (data_out),
2708 }/*-------------------------< RESET >----------------------*/,{
2710 ** Send a M_RESET message if bad IDENTIFY
2711 ** received on reselection.
2713 SCR_LOAD_REG (scratcha, M_ABORT_TAG),
2715 SCR_JUMP,
2716 PADDRH (abort_resel),
2717 }/*-------------------------< ABORTTAG >-------------------*/,{
2719 ** Abort a wrong tag received on reselection.
2721 SCR_LOAD_REG (scratcha, M_ABORT_TAG),
2723 SCR_JUMP,
2724 PADDRH (abort_resel),
2725 }/*-------------------------< ABORT >----------------------*/,{
2727 ** Abort a reselection when no active CCB.
2729 SCR_LOAD_REG (scratcha, M_ABORT),
2731 }/*-------------------------< ABORT_RESEL >----------------*/,{
2732 SCR_COPY (1),
2733 RADDR (scratcha),
2734 NADDR (msgout),
2735 SCR_SET (SCR_ATN),
2737 SCR_CLR (SCR_ACK),
2740 ** and send it.
2741 ** we expect an immediate disconnect
2743 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2745 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2746 NADDR (msgout),
2747 SCR_COPY (1),
2748 NADDR (msgout),
2749 NADDR (lastmsg),
2750 SCR_CLR (SCR_ACK|SCR_ATN),
2752 SCR_WAIT_DISC,
2754 SCR_JUMP,
2755 PADDR (start),
2756 }/*-------------------------< RESEND_IDENT >-------------------*/,{
2758 ** The target stays in MSG OUT phase after having acked
2759 ** Identify [+ Tag [+ Extended message ]]. Targets shall
2760 ** behave this way on parity error.
2761 ** We must send it again all the messages.
2763 SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
2764 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
2765 SCR_JUMP,
2766 PADDR (send_ident),
2767 }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
2768 SCR_CLR (SCR_ATN),
2770 SCR_JUMP,
2771 }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
2773 }/*-------------------------< SDATA_IN >-------------------*/,{
2774 SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2775 PADDR (dispatch),
2776 SCR_MOVE_TBL ^ SCR_DATA_IN,
2777 offsetof (struct dsb, sense),
2778 SCR_CALL,
2779 PADDR (dispatch),
2780 SCR_JUMP,
2781 PADDR (no_data),
2782 }/*-------------------------< DATA_IO >--------------------*/,{
2784 ** We jump here if the data direction was unknown at the
2785 ** time we had to queue the command to the scripts processor.
2786 ** Pointers had been set as follow in this situation:
2787 ** savep --> DATA_IO
2788 ** lastp --> start pointer when DATA_IN
2789 ** goalp --> goal pointer when DATA_IN
2790 ** wlastp --> start pointer when DATA_OUT
2791 ** wgoalp --> goal pointer when DATA_OUT
2792 ** This script sets savep/lastp/goalp according to the
2793 ** direction chosen by the target.
2795 SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
2798 ** Direction is DATA IN.
2799 ** Warning: we jump here, even when phase is DATA OUT.
2801 SCR_COPY (4),
2802 NADDR (header.lastp),
2803 NADDR (header.savep),
2806 ** Jump to the SCRIPTS according to actual direction.
2808 SCR_COPY (4),
2809 NADDR (header.savep),
2810 RADDR (temp),
2811 SCR_RETURN,
2814 ** Direction is DATA OUT.
2816 SCR_COPY (4),
2817 NADDR (header.wlastp),
2818 NADDR (header.lastp),
2819 SCR_COPY (4),
2820 NADDR (header.wgoalp),
2821 NADDR (header.goalp),
2822 SCR_JUMPR,
2823 -64,
2824 }/*-------------------------< BAD_IDENTIFY >---------------*/,{
2826 ** If message phase but not an IDENTIFY,
2827 ** get some help from the C code.
2828 ** Old SCSI device may behave so.
2830 SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
2832 SCR_INT,
2833 SIR_RESEL_NO_IDENTIFY,
2834 SCR_JUMP,
2835 PADDRH (reset),
2837 ** Message is an IDENTIFY, but lun is unknown.
2838 ** Read the message, since we got it directly
2839 ** from the SCSI BUS data lines.
2840 ** Signal problem to C code for logging the event.
2841 ** Send a M_ABORT to clear all pending tasks.
2843 SCR_INT,
2844 SIR_RESEL_BAD_LUN,
2845 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2846 NADDR (msgin),
2847 SCR_JUMP,
2848 PADDRH (abort),
2849 }/*-------------------------< BAD_I_T_L >------------------*/,{
2851 ** We donnot have a task for that I_T_L.
2852 ** Signal problem to C code for logging the event.
2853 ** Send a M_ABORT message.
2855 SCR_INT,
2856 SIR_RESEL_BAD_I_T_L,
2857 SCR_JUMP,
2858 PADDRH (abort),
2859 }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
2861 ** We donnot have a task that matches the tag.
2862 ** Signal problem to C code for logging the event.
2863 ** Send a M_ABORTTAG message.
2865 SCR_INT,
2866 SIR_RESEL_BAD_I_T_L_Q,
2867 SCR_JUMP,
2868 PADDRH (aborttag),
2869 }/*-------------------------< BAD_TARGET >-----------------*/,{
2871 ** We donnot know the target that reselected us.
2872 ** Grab the first message if any (IDENTIFY).
2873 ** Signal problem to C code for logging the event.
2874 ** M_RESET message.
2876 SCR_INT,
2877 SIR_RESEL_BAD_TARGET,
2878 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2880 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2881 NADDR (msgin),
2882 SCR_JUMP,
2883 PADDRH (reset),
2884 }/*-------------------------< BAD_STATUS >-----------------*/,{
2886 ** If command resulted in either QUEUE FULL,
2887 ** CHECK CONDITION or COMMAND TERMINATED,
2888 ** call the C code.
2890 SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
2891 SIR_BAD_STATUS,
2892 SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
2893 SIR_BAD_STATUS,
2894 SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
2895 SIR_BAD_STATUS,
2896 SCR_RETURN,
2898 }/*-------------------------< START_RAM >-------------------*/,{
2900 ** Load the script into on-chip RAM,
2901 ** and jump to start point.
2903 SCR_COPY_F (4),
2904 RADDR (scratcha),
2905 PADDRH (start_ram0),
2906 SCR_COPY (sizeof (struct script)),
2907 }/*-------------------------< START_RAM0 >--------------------*/,{
2909 PADDR (start),
2910 SCR_JUMP,
2911 PADDR (start),
2912 }/*-------------------------< STO_RESTART >-------------------*/,{
2915 ** Repair start queue (e.g. next time use the next slot)
2916 ** and jump to start point.
2918 SCR_COPY (4),
2919 RADDR (temp),
2920 PADDR (startpos),
2921 SCR_JUMP,
2922 PADDR (start),
2923 }/*-------------------------< SNOOPTEST >-------------------*/,{
2925 ** Read the variable.
2927 SCR_COPY (4),
2928 NADDR(ncr_cache),
2929 RADDR (scratcha),
2931 ** Write the variable.
2933 SCR_COPY (4),
2934 RADDR (temp),
2935 NADDR(ncr_cache),
2937 ** Read back the variable.
2939 SCR_COPY (4),
2940 NADDR(ncr_cache),
2941 RADDR (temp),
2942 }/*-------------------------< SNOOPEND >-------------------*/,{
2944 ** And stop.
2946 SCR_INT,
2948 }/*--------------------------------------------------------*/
2951 /*==========================================================
2954 ** Fill in #define dependent parts of the script
2957 **==========================================================
2960 void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
2962 int i;
2963 ncrcmd *p;
2965 p = scrh->tryloop;
2966 for (i=0; i<MAX_START; i++) {
2967 *p++ =SCR_CALL;
2968 *p++ =PADDR (idle);
2971 assert ((u_long)p == (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
2973 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2975 p = scrh->done_queue;
2976 for (i = 0; i<MAX_DONE; i++) {
2977 *p++ =SCR_COPY (sizeof(ccb_p));
2978 *p++ =NADDR (header.cp);
2979 *p++ =NADDR (ccb_done[i]);
2980 *p++ =SCR_CALL;
2981 *p++ =PADDR (done_end);
2984 assert ((u_long)p ==(u_long)&scrh->done_queue+sizeof(scrh->done_queue));
2986 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2988 p = scrh->hdata_in;
2989 for (i=0; i<MAX_SCATTERH; i++) {
2990 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
2991 *p++ =PADDR (dispatch);
2992 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2993 *p++ =offsetof (struct dsb, data[i]);
2995 assert ((u_long)p == (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
2997 p = scr->data_in;
2998 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
2999 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3000 *p++ =PADDR (dispatch);
3001 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3002 *p++ =offsetof (struct dsb, data[i]);
3004 assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
3006 p = scrh->hdata_out;
3007 for (i=0; i<MAX_SCATTERH; i++) {
3008 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3009 *p++ =PADDR (dispatch);
3010 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3011 *p++ =offsetof (struct dsb, data[i]);
3013 assert ((u_long)p==(u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
3015 p = scr->data_out;
3016 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3017 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3018 *p++ =PADDR (dispatch);
3019 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3020 *p++ =offsetof (struct dsb, data[i]);
3023 assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
3026 /*==========================================================
3029 ** Copy and rebind a script.
3032 **==========================================================
3035 static void __init
3036 ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
3038 ncrcmd opcode, new, old, tmp1, tmp2;
3039 ncrcmd *start, *end;
3040 int relocs;
3041 int opchanged = 0;
3043 start = src;
3044 end = src + len/4;
3046 while (src < end) {
3048 opcode = *src++;
3049 *dst++ = cpu_to_scr(opcode);
3052 ** If we forget to change the length
3053 ** in struct script, a field will be
3054 ** padded with 0. This is an illegal
3055 ** command.
3058 if (opcode == 0) {
3059 printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
3060 ncr_name(np), (int) (src-start-1));
3061 MDELAY (1000);
3064 if (DEBUG_FLAGS & DEBUG_SCRIPT)
3065 printk (KERN_DEBUG "%p: <%x>\n",
3066 (src-1), (unsigned)opcode);
3069 ** We don't have to decode ALL commands
3071 switch (opcode >> 28) {
3073 case 0xc:
3075 ** COPY has TWO arguments.
3077 relocs = 2;
3078 tmp1 = src[0];
3079 #ifdef RELOC_KVAR
3080 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3081 tmp1 = 0;
3082 #endif
3083 tmp2 = src[1];
3084 #ifdef RELOC_KVAR
3085 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3086 tmp2 = 0;
3087 #endif
3088 if ((tmp1 ^ tmp2) & 3) {
3089 printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
3090 ncr_name(np), (int) (src-start-1));
3091 MDELAY (1000);
3094 ** If PREFETCH feature not enabled, remove
3095 ** the NO FLUSH bit if present.
3097 if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
3098 dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
3099 ++opchanged;
3101 break;
3103 case 0x0:
3105 ** MOVE (absolute address)
3107 relocs = 1;
3108 break;
3110 case 0x8:
3112 ** JUMP / CALL
3113 ** dont't relocate if relative :-)
3115 if (opcode & 0x00800000)
3116 relocs = 0;
3117 else
3118 relocs = 1;
3119 break;
3121 case 0x4:
3122 case 0x5:
3123 case 0x6:
3124 case 0x7:
3125 relocs = 1;
3126 break;
3128 default:
3129 relocs = 0;
3130 break;
3133 if (relocs) {
3134 while (relocs--) {
3135 old = *src++;
3137 switch (old & RELOC_MASK) {
3138 case RELOC_REGISTER:
3139 new = (old & ~RELOC_MASK)
3140 + pcivtobus(np->paddr);
3141 break;
3142 case RELOC_LABEL:
3143 new = (old & ~RELOC_MASK) + np->p_script;
3144 break;
3145 case RELOC_LABELH:
3146 new = (old & ~RELOC_MASK) + np->p_scripth;
3147 break;
3148 case RELOC_SOFTC:
3149 new = (old & ~RELOC_MASK) + np->p_ncb;
3150 break;
3151 #ifdef RELOC_KVAR
3152 case RELOC_KVAR:
3153 if (((old & ~RELOC_MASK) <
3154 SCRIPT_KVAR_FIRST) ||
3155 ((old & ~RELOC_MASK) >
3156 SCRIPT_KVAR_LAST))
3157 panic("ncr KVAR out of range");
3158 new = vtophys(script_kvars[old &
3159 ~RELOC_MASK]);
3160 break;
3161 #endif
3162 case 0:
3163 /* Don't relocate a 0 address. */
3164 if (old == 0) {
3165 new = old;
3166 break;
3168 /* fall through */
3169 default:
3170 panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
3171 break;
3174 *dst++ = cpu_to_scr(new);
3176 } else
3177 *dst++ = cpu_to_scr(*src++);
3182 /*==========================================================
3185 ** Auto configuration: attach and init a host adapter.
3188 **==========================================================
3192 ** Linux host data structure
3194 ** The script area is allocated in the host data structure
3195 ** because kmalloc() returns NULL during scsi initialisations
3196 ** with Linux 1.2.X
3199 struct host_data {
3200 struct ncb *ncb;
3204 ** Print something which allows to retrieve the controler type, unit,
3205 ** target, lun concerned by a kernel message.
3208 static void PRINT_TARGET(ncb_p np, int target)
3210 printk(KERN_INFO "%s-<%d,*>: ", ncr_name(np), target);
3213 static void PRINT_LUN(ncb_p np, int target, int lun)
3215 printk(KERN_INFO "%s-<%d,%d>: ", ncr_name(np), target, lun);
3218 static void PRINT_ADDR(Scsi_Cmnd *cmd)
3220 struct host_data *host_data = (struct host_data *) cmd->host->hostdata;
3221 PRINT_LUN(host_data->ncb, cmd->target, cmd->lun);
3224 /*==========================================================
3226 ** NCR chip clock divisor table.
3227 ** Divisors are multiplied by 10,000,000 in order to make
3228 ** calculations more simple.
3230 **==========================================================
3233 #define _5M 5000000
3234 static u_long div_10M[] =
3235 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3238 /*===============================================================
3240 ** Prepare io register values used by ncr_init() according
3241 ** to selected and supported features.
3243 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3244 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3245 ** We use log base 2 (burst length) as internal code, with
3246 ** value 0 meaning "burst disabled".
3248 **===============================================================
3252 * Burst length from burst code.
3254 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3257 * Burst code from io register bits.
3259 #define burst_code(dmode, ctest4, ctest5) \
3260 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
3263 * Set initial io register bits from burst code.
3265 static inline void ncr_init_burst(ncb_p np, u_char bc)
3267 np->rv_ctest4 &= ~0x80;
3268 np->rv_dmode &= ~(0x3 << 6);
3269 np->rv_ctest5 &= ~0x4;
3271 if (!bc) {
3272 np->rv_ctest4 |= 0x80;
3274 else {
3275 --bc;
3276 np->rv_dmode |= ((bc & 0x3) << 6);
3277 np->rv_ctest5 |= (bc & 0x4);
3281 #ifdef SCSI_NCR_NVRAM_SUPPORT
3284 ** Get target set-up from Symbios format NVRAM.
3287 static void __init
3288 ncr_Symbios_setup_target(ncb_p np, int target, Symbios_nvram *nvram)
3290 tcb_p tp = &np->target[target];
3291 Symbios_target *tn = &nvram->target[target];
3293 tp->usrsync = tn->sync_period ? (tn->sync_period + 3) / 4 : 255;
3294 tp->usrwide = tn->bus_width == 0x10 ? 1 : 0;
3295 tp->usrtags =
3296 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? MAX_TAGS : 0;
3298 if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
3299 tp->usrflag |= UF_NODISC;
3300 if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
3301 tp->usrflag |= UF_NOSCAN;
3305 ** Get target set-up from Tekram format NVRAM.
3308 static void __init
3309 ncr_Tekram_setup_target(ncb_p np, int target, Tekram_nvram *nvram)
3311 tcb_p tp = &np->target[target];
3312 struct Tekram_target *tn = &nvram->target[target];
3313 int i;
3315 if (tn->flags & TEKRAM_SYNC_NEGO) {
3316 i = tn->sync_index & 0xf;
3317 tp->usrsync = Tekram_sync[i];
3320 tp->usrwide = (tn->flags & TEKRAM_WIDE_NEGO) ? 1 : 0;
3322 if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
3323 tp->usrtags = 2 << nvram->max_tags_index;
3326 if (!(tn->flags & TEKRAM_DISCONNECT_ENABLE))
3327 tp->usrflag = UF_NODISC;
3329 /* If any device does not support parity, we will not use this option */
3330 if (!(tn->flags & TEKRAM_PARITY_CHECK))
3331 np->rv_scntl0 &= ~0x0a; /* SCSI parity checking disabled */
3333 #endif /* SCSI_NCR_NVRAM_SUPPORT */
3335 static int __init ncr_prepare_setting(ncb_p np, ncr_nvram *nvram)
3337 u_char burst_max;
3338 u_long period;
3339 int i;
3342 ** Save assumed BIOS setting
3345 np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
3346 np->sv_scntl3 = INB(nc_scntl3) & 0x07;
3347 np->sv_dmode = INB(nc_dmode) & 0xce;
3348 np->sv_dcntl = INB(nc_dcntl) & 0xa8;
3349 np->sv_ctest3 = INB(nc_ctest3) & 0x01;
3350 np->sv_ctest4 = INB(nc_ctest4) & 0x80;
3351 np->sv_ctest5 = INB(nc_ctest5) & 0x24;
3352 np->sv_gpcntl = INB(nc_gpcntl);
3353 np->sv_stest2 = INB(nc_stest2) & 0x20;
3354 np->sv_stest4 = INB(nc_stest4);
3357 ** Wide ?
3360 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
3363 ** Get the frequency of the chip's clock.
3364 ** Find the right value for scntl3.
3367 if (np->features & FE_QUAD)
3368 np->multiplier = 4;
3369 else if (np->features & FE_DBLR)
3370 np->multiplier = 2;
3371 else
3372 np->multiplier = 1;
3374 np->clock_khz = (np->features & FE_CLK80)? 80000 : 40000;
3375 np->clock_khz *= np->multiplier;
3377 if (np->clock_khz != 40000)
3378 ncr_getclock(np, np->multiplier);
3381 * Divisor to be used for async (timer pre-scaler).
3383 i = np->clock_divn - 1;
3384 while (--i >= 0) {
3385 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
3386 ++i;
3387 break;
3390 np->rv_scntl3 = i+1;
3393 * Minimum synchronous period factor supported by the chip.
3394 * Btw, 'period' is in tenths of nanoseconds.
3397 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
3398 if (period <= 250) np->minsync = 10;
3399 else if (period <= 303) np->minsync = 11;
3400 else if (period <= 500) np->minsync = 12;
3401 else np->minsync = (period + 40 - 1) / 40;
3404 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3407 if (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
3408 np->minsync = 25;
3409 else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
3410 np->minsync = 12;
3413 * Maximum synchronous period factor supported by the chip.
3416 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
3417 np->maxsync = period > 2540 ? 254 : period / 10;
3420 ** Prepare initial value of other IO registers
3422 #if defined SCSI_NCR_TRUST_BIOS_SETTING
3423 np->rv_scntl0 = np->sv_scntl0;
3424 np->rv_dmode = np->sv_dmode;
3425 np->rv_dcntl = np->sv_dcntl;
3426 np->rv_ctest3 = np->sv_ctest3;
3427 np->rv_ctest4 = np->sv_ctest4;
3428 np->rv_ctest5 = np->sv_ctest5;
3429 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
3430 #else
3433 ** Select burst length (dwords)
3435 burst_max = driver_setup.burst_max;
3436 if (burst_max == 255)
3437 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
3438 if (burst_max > 7)
3439 burst_max = 7;
3440 if (burst_max > np->maxburst)
3441 burst_max = np->maxburst;
3444 ** Select all supported special features
3446 if (np->features & FE_ERL)
3447 np->rv_dmode |= ERL; /* Enable Read Line */
3448 if (np->features & FE_BOF)
3449 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
3450 if (np->features & FE_ERMP)
3451 np->rv_dmode |= ERMP; /* Enable Read Multiple */
3452 if (np->features & FE_PFEN)
3453 np->rv_dcntl |= PFEN; /* Prefetch Enable */
3454 if (np->features & FE_CLSE)
3455 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
3456 if (np->features & FE_WRIE)
3457 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
3458 if (np->features & FE_DFS)
3459 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
3462 ** Select some other
3464 if (driver_setup.master_parity)
3465 np->rv_ctest4 |= MPEE; /* Master parity checking */
3466 if (driver_setup.scsi_parity)
3467 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
3469 #ifdef SCSI_NCR_NVRAM_SUPPORT
3471 ** Get parity checking, host ID and verbose mode from NVRAM
3473 if (nvram) {
3474 switch(nvram->type) {
3475 case SCSI_NCR_TEKRAM_NVRAM:
3476 np->myaddr = nvram->data.Tekram.host_id & 0x0f;
3477 break;
3478 case SCSI_NCR_SYMBIOS_NVRAM:
3479 if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
3480 np->rv_scntl0 &= ~0x0a;
3481 np->myaddr = nvram->data.Symbios.host_id & 0x0f;
3482 if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
3483 np->verbose += 1;
3484 break;
3487 #endif
3489 ** Get SCSI addr of host adapter (set by bios?).
3491 if (np->myaddr == 255) {
3492 np->myaddr = INB(nc_scid) & 0x07;
3493 if (!np->myaddr)
3494 np->myaddr = SCSI_NCR_MYADDR;
3497 #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
3500 * Prepare initial io register bits for burst length
3502 ncr_init_burst(np, burst_max);
3505 ** Set SCSI BUS mode.
3507 ** - ULTRA2 chips (895/895A/896) report the current
3508 ** BUS mode through the STEST4 IO register.
3509 ** - For previous generation chips (825/825A/875),
3510 ** user has to tell us how to check against HVD,
3511 ** since a 100% safe algorithm is not possible.
3513 np->scsi_mode = SMODE_SE;
3514 if (np->features & FE_ULTRA2)
3515 np->scsi_mode = (np->sv_stest4 & SMODE);
3516 else if (np->features & FE_DIFF) {
3517 switch(driver_setup.diff_support) {
3518 case 4: /* Trust previous settings if present, then GPIO3 */
3519 if (np->sv_scntl3) {
3520 if (np->sv_stest2 & 0x20)
3521 np->scsi_mode = SMODE_HVD;
3522 break;
3524 case 3: /* SYMBIOS controllers report HVD through GPIO3 */
3525 if (nvram && nvram->type != SCSI_NCR_SYMBIOS_NVRAM)
3526 break;
3527 if (INB(nc_gpreg) & 0x08)
3528 break;
3529 case 2: /* Set HVD unconditionally */
3530 np->scsi_mode = SMODE_HVD;
3531 case 1: /* Trust previous settings for HVD */
3532 if (np->sv_stest2 & 0x20)
3533 np->scsi_mode = SMODE_HVD;
3534 break;
3535 default:/* Don't care about HVD */
3536 break;
3539 if (np->scsi_mode == SMODE_HVD)
3540 np->rv_stest2 |= 0x20;
3543 ** Set LED support from SCRIPTS.
3544 ** Ignore this feature for boards known to use a
3545 ** specific GPIO wiring and for the 895A or 896
3546 ** that drive the LED directly.
3547 ** Also probe initial setting of GPIO0 as output.
3549 if ((driver_setup.led_pin ||
3550 (nvram && nvram->type == SCSI_NCR_SYMBIOS_NVRAM)) &&
3551 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
3552 np->features |= FE_LED0;
3555 ** Set irq mode.
3557 switch(driver_setup.irqm & 3) {
3558 case 2:
3559 np->rv_dcntl |= IRQM;
3560 break;
3561 case 1:
3562 np->rv_dcntl |= (np->sv_dcntl & IRQM);
3563 break;
3564 default:
3565 break;
3569 ** Configure targets according to driver setup.
3570 ** If NVRAM present get targets setup from NVRAM.
3571 ** Allow to override sync, wide and NOSCAN from
3572 ** boot command line.
3574 for (i = 0 ; i < MAX_TARGET ; i++) {
3575 tcb_p tp = &np->target[i];
3577 tp->usrsync = 255;
3578 #ifdef SCSI_NCR_NVRAM_SUPPORT
3579 if (nvram) {
3580 switch(nvram->type) {
3581 case SCSI_NCR_TEKRAM_NVRAM:
3582 ncr_Tekram_setup_target(np, i, &nvram->data.Tekram);
3583 break;
3584 case SCSI_NCR_SYMBIOS_NVRAM:
3585 ncr_Symbios_setup_target(np, i, &nvram->data.Symbios);
3586 break;
3588 if (driver_setup.use_nvram & 0x2)
3589 tp->usrsync = driver_setup.default_sync;
3590 if (driver_setup.use_nvram & 0x4)
3591 tp->usrwide = driver_setup.max_wide;
3592 if (driver_setup.use_nvram & 0x8)
3593 tp->usrflag &= ~UF_NOSCAN;
3595 else {
3596 #else
3597 if (1) {
3598 #endif
3599 tp->usrsync = driver_setup.default_sync;
3600 tp->usrwide = driver_setup.max_wide;
3601 tp->usrtags = MAX_TAGS;
3602 if (!driver_setup.disconnection)
3603 np->target[i].usrflag = UF_NODISC;
3608 ** Announce all that stuff to user.
3611 i = nvram ? nvram->type : 0;
3612 printk(KERN_INFO "%s: %sID %d, Fast-%d%s%s\n", ncr_name(np),
3613 i == SCSI_NCR_SYMBIOS_NVRAM ? "Symbios format NVRAM, " :
3614 (i == SCSI_NCR_TEKRAM_NVRAM ? "Tekram format NVRAM, " : ""),
3615 np->myaddr,
3616 np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
3617 (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
3618 (np->rv_stest2 & 0x20) ? ", Differential" : "");
3620 if (bootverbose > 1) {
3621 printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
3622 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
3623 ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
3624 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
3626 printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
3627 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
3628 ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
3629 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
3632 if (bootverbose && np->paddr2)
3633 printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
3634 ncr_name(np), np->paddr2);
3636 return 0;
3640 ** Host attach and initialisations.
3642 ** Allocate host data and ncb structure.
3643 ** Request IO region and remap MMIO region.
3644 ** Do chip initialization.
3645 ** If all is OK, install interrupt handling and
3646 ** start the timer daemon.
3649 static int __init
3650 ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device)
3652 struct host_data *host_data;
3653 ncb_p np = 0;
3654 struct Scsi_Host *instance = 0;
3655 u_long flags = 0;
3656 ncr_nvram *nvram = device->nvram;
3657 int i;
3659 printk(KERN_INFO "ncr53c%s-%d: rev 0x%x on pci bus %d device %d function %d "
3660 #ifdef __sparc__
3661 "irq %s\n",
3662 #else
3663 "irq %d\n",
3664 #endif
3665 device->chip.name, unit, device->chip.revision_id,
3666 device->slot.bus, (device->slot.device_fn & 0xf8) >> 3,
3667 device->slot.device_fn & 7,
3668 #ifdef __sparc__
3669 __irq_itoa(device->slot.irq));
3670 #else
3671 device->slot.irq);
3672 #endif
3675 ** Allocate host_data structure
3677 if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
3678 goto attach_error;
3679 host_data = (struct host_data *) instance->hostdata;
3682 ** Allocate the host control block.
3684 np = __m_calloc_dma(device->pdev, sizeof(struct ncb), "NCB");
3685 if (!np)
3686 goto attach_error;
3687 NCR_INIT_LOCK_NCB(np);
3688 np->pdev = device->pdev;
3689 np->p_ncb = vtobus(np);
3690 host_data->ncb = np;
3693 ** Allocate the default CCB.
3695 np->ccb = (ccb_p) m_calloc_dma(sizeof(struct ccb), "CCB");
3696 if (!np->ccb)
3697 goto attach_error;
3700 ** Store input informations in the host data structure.
3702 strncpy(np->chip_name, device->chip.name, sizeof(np->chip_name) - 1);
3703 np->unit = unit;
3704 np->verbose = driver_setup.verbose;
3705 sprintf(np->inst_name, "ncr53c%s-%d", np->chip_name, np->unit);
3706 np->device_id = device->chip.device_id;
3707 np->revision_id = device->chip.revision_id;
3708 np->bus = device->slot.bus;
3709 np->device_fn = device->slot.device_fn;
3710 np->features = device->chip.features;
3711 np->clock_divn = device->chip.nr_divisor;
3712 np->maxoffs = device->chip.offset_max;
3713 np->maxburst = device->chip.burst_max;
3714 np->myaddr = device->host_id;
3717 ** Allocate SCRIPTS areas.
3719 np->script0 = (struct script *)
3720 m_calloc_dma(sizeof(struct script), "SCRIPT");
3721 if (!np->script0)
3722 goto attach_error;
3723 np->scripth0 = (struct scripth *)
3724 m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
3725 if (!np->scripth0)
3726 goto attach_error;
3729 ** Initialize timer structure
3732 init_timer(&np->timer);
3733 np->timer.data = (unsigned long) np;
3734 np->timer.function = ncr53c8xx_timeout;
3737 ** Try to map the controller chip to
3738 ** virtual and physical memory.
3741 np->paddr = device->slot.base;
3742 np->paddr2 = (np->features & FE_RAM)? device->slot.base_2 : 0;
3744 #ifndef NCR_IOMAPPED
3745 np->vaddr = remap_pci_mem((u_long) np->paddr, (u_long) 128);
3746 if (!np->vaddr) {
3747 printk(KERN_ERR
3748 "%s: can't map memory mapped IO region\n",ncr_name(np));
3749 goto attach_error;
3751 else
3752 if (bootverbose > 1)
3753 printk(KERN_INFO
3754 "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
3757 ** Make the controller's registers available.
3758 ** Now the INB INW INL OUTB OUTW OUTL macros
3759 ** can be used safely.
3762 np->reg = (struct ncr_reg*) np->vaddr;
3764 #endif /* !defined NCR_IOMAPPED */
3767 ** Try to map the controller chip into iospace.
3770 request_region(device->slot.io_port, 128, "ncr53c8xx");
3771 np->base_io = device->slot.io_port;
3773 #ifdef SCSI_NCR_NVRAM_SUPPORT
3774 if (nvram) {
3775 switch(nvram->type) {
3776 case SCSI_NCR_SYMBIOS_NVRAM:
3777 #ifdef SCSI_NCR_DEBUG_NVRAM
3778 ncr_display_Symbios_nvram(&nvram->data.Symbios);
3779 #endif
3780 break;
3781 case SCSI_NCR_TEKRAM_NVRAM:
3782 #ifdef SCSI_NCR_DEBUG_NVRAM
3783 ncr_display_Tekram_nvram(&nvram->data.Tekram);
3784 #endif
3785 break;
3786 default:
3787 nvram = 0;
3788 #ifdef SCSI_NCR_DEBUG_NVRAM
3789 printk(KERN_DEBUG "%s: NVRAM: None or invalid data.\n", ncr_name(np));
3790 #endif
3793 #endif
3796 ** Do chip dependent initialization.
3798 (void)ncr_prepare_setting(np, nvram);
3800 if (np->paddr2 && sizeof(struct script) > 4096) {
3801 np->paddr2 = 0;
3802 printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
3803 ncr_name(np));
3807 ** Fill Linux host instance structure
3809 instance->max_channel = 0;
3810 instance->this_id = np->myaddr;
3811 instance->max_id = np->maxwide ? 16 : 8;
3812 instance->max_lun = SCSI_NCR_MAX_LUN;
3813 #ifndef NCR_IOMAPPED
3814 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,29)
3815 instance->base = (unsigned long) np->reg;
3816 #else
3817 instance->base = (char *) np->reg;
3818 #endif
3819 #endif
3820 instance->irq = device->slot.irq;
3821 instance->unique_id = device->slot.io_port;
3822 instance->io_port = device->slot.io_port;
3823 instance->n_io_port = 128;
3824 instance->dma_channel = 0;
3825 instance->cmd_per_lun = MAX_TAGS;
3826 instance->can_queue = (MAX_START-4);
3827 instance->select_queue_depths = ncr53c8xx_select_queue_depths;
3829 #ifdef SCSI_NCR_INTEGRITY_CHECKING
3830 np->check_integrity = 0;
3831 instance->check_integrity = 0;
3833 #ifdef SCSI_NCR_ENABLE_INTEGRITY_CHECK
3834 if ( !(driver_setup.bus_check & 0x04) ) {
3835 np->check_integrity = 1;
3836 instance->check_integrity = 1;
3838 #endif
3839 #endif
3841 ** Patch script to physical addresses
3843 ncr_script_fill (&script0, &scripth0);
3845 np->scripth = np->scripth0;
3846 np->p_scripth = vtobus(np->scripth);
3848 np->p_script = (np->paddr2) ?
3849 pcivtobus(np->paddr2) : vtobus(np->script0);
3851 ncr_script_copy_and_bind (np, (ncrcmd *) &script0, (ncrcmd *) np->script0, sizeof(struct script));
3852 ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0, (ncrcmd *) np->scripth0, sizeof(struct scripth));
3853 np->ccb->p_ccb = vtobus (np->ccb);
3856 ** Patch the script for LED support.
3859 if (np->features & FE_LED0) {
3860 np->script0->idle[0] =
3861 cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
3862 np->script0->reselected[0] =
3863 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3864 np->script0->start[0] =
3865 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3869 ** Look for the target control block of this nexus.
3870 ** For i = 0 to 3
3871 ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
3873 for (i = 0 ; i < 4 ; i++) {
3874 np->jump_tcb[i].l_cmd =
3875 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
3876 np->jump_tcb[i].l_paddr =
3877 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
3881 ** Reset chip.
3884 OUTB (nc_istat, SRST);
3885 UDELAY (100);
3886 OUTB (nc_istat, 0 );
3889 ** Now check the cache handling of the pci chipset.
3892 if (ncr_snooptest (np)) {
3893 printk (KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
3894 goto attach_error;
3898 ** Install the interrupt handler.
3901 if (request_irq(device->slot.irq, ncr53c8xx_intr,
3902 ((driver_setup.irqm & 0x10) ? 0 : SA_SHIRQ) |
3903 #if LINUX_VERSION_CODE < LinuxVersionCode(2,2,0)
3904 ((driver_setup.irqm & 0x20) ? 0 : SA_INTERRUPT),
3905 #else
3907 #endif
3908 "ncr53c8xx", np)) {
3909 #ifdef __sparc__
3910 printk(KERN_ERR "%s: request irq %s failure\n",
3911 ncr_name(np), __irq_itoa(device->slot.irq));
3912 #else
3913 printk(KERN_ERR "%s: request irq %d failure\n",
3914 ncr_name(np), device->slot.irq);
3915 #endif
3916 goto attach_error;
3919 np->irq = device->slot.irq;
3922 ** Initialize the fixed part of the default ccb.
3924 ncr_init_ccb(np, np->ccb);
3927 ** After SCSI devices have been opened, we cannot
3928 ** reset the bus safely, so we do it here.
3929 ** Interrupt handler does the real work.
3930 ** Process the reset exception,
3931 ** if interrupts are not enabled yet.
3932 ** Then enable disconnects.
3934 NCR_LOCK_NCB(np, flags);
3935 if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
3936 printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
3938 NCR_UNLOCK_NCB(np, flags);
3939 goto attach_error;
3941 ncr_exception (np);
3943 np->disc = 1;
3946 ** The middle-level SCSI driver does not
3947 ** wait for devices to settle.
3948 ** Wait synchronously if more than 2 seconds.
3950 if (driver_setup.settle_delay > 2) {
3951 printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
3952 ncr_name(np), driver_setup.settle_delay);
3953 MDELAY (1000 * driver_setup.settle_delay);
3957 ** Now let the generic SCSI driver
3958 ** look for the SCSI devices on the bus ..
3962 ** start the timeout daemon
3964 np->lasttime=0;
3965 ncr_timeout (np);
3968 ** use SIMPLE TAG messages by default
3970 #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
3971 np->order = M_SIMPLE_TAG;
3972 #endif
3975 ** Done.
3977 if (!the_template) {
3978 the_template = instance->hostt;
3979 first_host = instance;
3982 NCR_UNLOCK_NCB(np, flags);
3984 return 0;
3986 attach_error:
3987 if (!instance) return -1;
3988 printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
3989 if (!np)
3990 goto unregister;
3991 #ifndef NCR_IOMAPPED
3992 if (np->vaddr) {
3993 #ifdef DEBUG_NCR53C8XX
3994 printk(KERN_DEBUG "%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
3995 #endif
3996 unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
3998 #endif /* !NCR_IOMAPPED */
3999 if (np->base_io) {
4000 #ifdef DEBUG_NCR53C8XX
4001 printk(KERN_DEBUG "%s: releasing IO region %x[%d]\n", ncr_name(np), np->base_io, 128);
4002 #endif
4003 release_region(np->base_io, 128);
4005 if (np->irq) {
4006 #ifdef DEBUG_NCR53C8XX
4007 #ifdef __sparc__
4008 printk(KERN_INFO "%s: freeing irq %s\n", ncr_name(np),
4009 __irq_itoa(np->irq));
4010 #else
4011 printk(KERN_INFO "%s: freeing irq %d\n", ncr_name(np), np->irq);
4012 #endif
4013 #endif
4014 free_irq(np->irq, np);
4016 if (np->scripth0)
4017 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
4018 if (np->script0)
4019 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
4020 if (np->ccb)
4021 m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
4022 m_free_dma(np, sizeof(struct ncb), "NCB");
4024 unregister:
4025 scsi_unregister(instance);
4027 return -1;
4031 /*==========================================================
4034 ** Done SCSI commands list management.
4036 ** We donnot enter the scsi_done() callback immediately
4037 ** after a command has been seen as completed but we
4038 ** insert it into a list which is flushed outside any kind
4039 ** of driver critical section.
4040 ** This allows to do minimal stuff under interrupt and
4041 ** inside critical sections and to also avoid locking up
4042 ** on recursive calls to driver entry points under SMP.
4043 ** In fact, the only kernel point which is entered by the
4044 ** driver with a driver lock set is kmalloc(GFP_ATOMIC)
4045 ** that shall not reenter the driver under any circumstances,
4046 ** AFAIK.
4048 **==========================================================
4050 static inline void ncr_queue_done_cmd(ncb_p np, Scsi_Cmnd *cmd)
4052 unmap_scsi_data(np, cmd);
4053 cmd->host_scribble = (char *) np->done_list;
4054 np->done_list = cmd;
4057 static inline void ncr_flush_done_cmds(Scsi_Cmnd *lcmd)
4059 Scsi_Cmnd *cmd;
4061 while (lcmd) {
4062 cmd = lcmd;
4063 lcmd = (Scsi_Cmnd *) cmd->host_scribble;
4064 cmd->scsi_done(cmd);
4068 /*==========================================================
4071 ** Prepare the next negotiation message for integrity check,
4072 ** if needed.
4074 ** Fill in the part of message buffer that contains the
4075 ** negotiation and the nego_status field of the CCB.
4076 ** Returns the size of the message in bytes.
4079 **==========================================================
4082 #ifdef SCSI_NCR_INTEGRITY_CHECKING
4083 static int ncr_ic_nego(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd, u_char *msgptr)
4085 tcb_p tp = &np->target[cp->target];
4086 int msglen = 0;
4087 int nego = 0;
4088 u_char no_increase;
4090 if (tp->inq_done) {
4092 if (!tp->ic_maximums_set) {
4093 tp->ic_maximums_set = 1;
4095 /* check target and host adapter capabilities */
4096 if ( (tp->inq_byte7 & INQ7_WIDE16) &&
4097 np->maxwide && tp->usrwide )
4098 tp->ic_max_width = 1;
4099 else
4100 tp->ic_max_width = 0;
4102 if ((tp->inq_byte7 & INQ7_SYNC) && tp->maxoffs) {
4103 tp->ic_min_sync = (tp->minsync < np->minsync) ?
4104 np->minsync : tp->minsync;
4106 else
4107 tp->ic_min_sync = 255;
4109 tp->period = 1;
4110 tp->widedone = 1;
4113 if (DEBUG_FLAGS & DEBUG_IC) {
4114 printk("%s: cmd->ic_nego %d, 1st byte 0x%2X\n",
4115 ncr_name(np), cmd->ic_nego, cmd->cmnd[0]);
4118 /* First command from integrity check routine will request
4119 * a PPR message. Disable.
4121 if ((cmd->ic_nego & NS_PPR) == NS_PPR)
4122 cmd->ic_nego &= ~NS_PPR;
4123 /* Previous command recorded a parity or an initiator
4124 * detected error condition. Force bus to narrow for this
4125 * target. Clear flag. Negotation on request sense.
4126 * Note: kernel forces 2 bus resets :o( but clears itself out.
4127 * Minor bug? in scsi_obsolete.c (ugly)
4129 if (np->check_integ_par) {
4130 printk("%s: Parity Error. Target set to narrow.\n",
4131 ncr_name(np));
4132 tp->ic_max_width = 0;
4133 tp->widedone = tp->period = 0;
4136 /* In case of a bus reset, ncr_negotiate will reset
4137 * the flags tp->widedone and tp->period to 0, forcing
4138 * a new negotiation.
4140 no_increase = 0;
4141 if (tp->widedone == 0) {
4142 cmd->ic_nego = NS_WIDE;
4143 tp->widedone = 1;
4144 no_increase = 1;
4146 else if (tp->period == 0) {
4147 cmd->ic_nego = NS_SYNC;
4148 tp->period = 1;
4149 no_increase = 1;
4152 switch (cmd->ic_nego) {
4153 case NS_WIDE:
4155 ** negotiate wide transfers ?
4156 ** Do NOT negotiate if device only supports
4157 ** narrow.
4159 if (tp->ic_max_width | np->check_integ_par) {
4160 nego = NS_WIDE;
4162 msgptr[msglen++] = M_EXTENDED;
4163 msgptr[msglen++] = 2;
4164 msgptr[msglen++] = M_X_WIDE_REQ;
4165 msgptr[msglen++] = cmd->ic_nego_width & tp->ic_max_width;
4167 else
4168 cmd->ic_nego_width &= tp->ic_max_width;
4170 break;
4172 case NS_SYNC:
4174 ** negotiate synchronous transfers?
4175 ** Target must support sync transfers.
4177 ** If period becomes longer than max, reset to async
4180 if (tp->inq_byte7 & INQ7_SYNC) {
4182 nego = NS_SYNC;
4184 msgptr[msglen++] = M_EXTENDED;
4185 msgptr[msglen++] = 3;
4186 msgptr[msglen++] = M_X_SYNC_REQ;
4188 switch (cmd->ic_nego_sync) {
4189 case 2: /* increase the period */
4190 if (!no_increase) {
4191 if (tp->ic_min_sync <= 0x0A)
4192 tp->ic_min_sync = 0x0C;
4193 else if (tp->ic_min_sync <= 0x0C)
4194 tp->ic_min_sync = 0x19;
4195 else if (tp->ic_min_sync <= 0x19)
4196 tp->ic_min_sync *= 2;
4197 else {
4198 tp->ic_min_sync = 255;
4199 cmd->ic_nego_sync = 0;
4200 tp->maxoffs = 0;
4203 msgptr[msglen++] = tp->maxoffs?tp->ic_min_sync:0;
4204 msgptr[msglen++] = tp->maxoffs;
4205 break;
4207 case 1: /* nego. to maximum */
4208 msgptr[msglen++] = tp->maxoffs?tp->ic_min_sync:0;
4209 msgptr[msglen++] = tp->maxoffs;
4210 break;
4212 case 0: /* nego to async */
4213 default:
4214 msgptr[msglen++] = 0;
4215 msgptr[msglen++] = 0;
4216 break;
4219 else
4220 cmd->ic_nego_sync = 0;
4221 break;
4223 case NS_NOCHANGE:
4224 default:
4225 break;
4229 cp->nego_status = nego;
4230 np->check_integ_par = 0;
4232 if (nego) {
4233 tp->nego_cp = cp;
4234 if (DEBUG_FLAGS & DEBUG_NEGO) {
4235 ncr_print_msg(cp, nego == NS_WIDE ?
4236 "wide/narrow msgout": "sync/async msgout", msgptr);
4240 return msglen;
4242 #endif /* SCSI_NCR_INTEGRITY_CHECKING */
4244 /*==========================================================
4247 ** Prepare the next negotiation message if needed.
4249 ** Fill in the part of message buffer that contains the
4250 ** negotiation and the nego_status field of the CCB.
4251 ** Returns the size of the message in bytes.
4254 **==========================================================
4258 static int ncr_prepare_nego(ncb_p np, ccb_p cp, u_char *msgptr)
4260 tcb_p tp = &np->target[cp->target];
4261 int msglen = 0;
4262 int nego = 0;
4264 if (tp->inq_done) {
4267 ** negotiate wide transfers ?
4270 if (!tp->widedone) {
4271 if (tp->inq_byte7 & INQ7_WIDE16) {
4272 nego = NS_WIDE;
4273 #ifdef SCSI_NCR_INTEGRITY_CHECKING
4274 if (tp->ic_done)
4275 tp->usrwide &= tp->ic_max_width;
4276 #endif
4277 } else
4278 tp->widedone=1;
4283 ** negotiate synchronous transfers?
4286 if (!nego && !tp->period) {
4287 if (tp->inq_byte7 & INQ7_SYNC) {
4288 nego = NS_SYNC;
4289 #ifdef SCSI_NCR_INTEGRITY_CHECKING
4290 if ((tp->ic_done) &&
4291 (tp->minsync < tp->ic_min_sync))
4292 tp->minsync = tp->ic_min_sync;
4293 #endif
4294 } else {
4295 tp->period =0xffff;
4296 PRINT_TARGET(np, cp->target);
4297 printk ("target did not report SYNC.\n");
4302 switch (nego) {
4303 case NS_SYNC:
4304 msgptr[msglen++] = M_EXTENDED;
4305 msgptr[msglen++] = 3;
4306 msgptr[msglen++] = M_X_SYNC_REQ;
4307 msgptr[msglen++] = tp->maxoffs ? tp->minsync : 0;
4308 msgptr[msglen++] = tp->maxoffs;
4309 break;
4310 case NS_WIDE:
4311 msgptr[msglen++] = M_EXTENDED;
4312 msgptr[msglen++] = 2;
4313 msgptr[msglen++] = M_X_WIDE_REQ;
4314 msgptr[msglen++] = tp->usrwide;
4315 break;
4318 cp->nego_status = nego;
4320 if (nego) {
4321 tp->nego_cp = cp;
4322 if (DEBUG_FLAGS & DEBUG_NEGO) {
4323 ncr_print_msg(cp, nego == NS_WIDE ?
4324 "wide msgout":"sync_msgout", msgptr);
4328 return msglen;
4333 /*==========================================================
4336 ** Start execution of a SCSI command.
4337 ** This is called from the generic SCSI driver.
4340 **==========================================================
4342 static int ncr_queue_command (ncb_p np, Scsi_Cmnd *cmd)
4344 /* Scsi_Device *device = cmd->device; */
4345 tcb_p tp = &np->target[cmd->target];
4346 lcb_p lp = tp->lp[cmd->lun];
4347 ccb_p cp;
4349 int segments;
4350 u_char idmsg, *msgptr;
4351 u_int msglen;
4352 int direction;
4353 u_int32 lastp, goalp;
4355 /*---------------------------------------------
4357 ** Some shortcuts ...
4359 **---------------------------------------------
4361 if ((cmd->target == np->myaddr ) ||
4362 (cmd->target >= MAX_TARGET) ||
4363 (cmd->lun >= MAX_LUN )) {
4364 return(DID_BAD_TARGET);
4367 /*---------------------------------------------
4369 ** Complete the 1st TEST UNIT READY command
4370 ** with error condition if the device is
4371 ** flagged NOSCAN, in order to speed up
4372 ** the boot.
4374 **---------------------------------------------
4376 if (cmd->cmnd[0] == 0 && (tp->usrflag & UF_NOSCAN)) {
4377 tp->usrflag &= ~UF_NOSCAN;
4378 return DID_BAD_TARGET;
4381 if (DEBUG_FLAGS & DEBUG_TINY) {
4382 PRINT_ADDR(cmd);
4383 printk ("CMD=%x ", cmd->cmnd[0]);
4386 /*---------------------------------------------------
4388 ** Assign a ccb / bind cmd.
4389 ** If resetting, shorten settle_time if necessary
4390 ** in order to avoid spurious timeouts.
4391 ** If resetting or no free ccb,
4392 ** insert cmd into the waiting list.
4394 **----------------------------------------------------
4396 if (np->settle_time && cmd->timeout_per_command >= HZ) {
4397 u_long tlimit = ktime_get(cmd->timeout_per_command - HZ);
4398 if (ktime_dif(np->settle_time, tlimit) > 0)
4399 np->settle_time = tlimit;
4402 if (np->settle_time || !(cp=ncr_get_ccb (np, cmd->target, cmd->lun))) {
4403 insert_into_waiting_list(np, cmd);
4404 return(DID_OK);
4406 cp->cmd = cmd;
4408 /*---------------------------------------------------
4410 ** Enable tagged queue if asked by scsi ioctl
4412 **----------------------------------------------------
4414 #if 0 /* This stuff was only usefull for linux-1.2.13 */
4415 if (lp && !lp->numtags && cmd->device && cmd->device->tagged_queue) {
4416 lp->numtags = tp->usrtags;
4417 ncr_setup_tags (np, cmd->target, cmd->lun);
4419 #endif
4421 /*---------------------------------------------------
4423 ** timestamp
4425 **----------------------------------------------------
4427 #ifdef SCSI_NCR_PROFILE_SUPPORT
4428 bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
4429 cp->phys.header.stamp.start = jiffies;
4430 #endif
4433 /*----------------------------------------------------
4435 ** Build the identify / tag / sdtr message
4437 **----------------------------------------------------
4440 idmsg = M_IDENTIFY | cmd->lun;
4442 if (cp ->tag != NO_TAG ||
4443 (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
4444 idmsg |= 0x40;
4446 msgptr = cp->scsi_smsg;
4447 msglen = 0;
4448 msgptr[msglen++] = idmsg;
4450 if (cp->tag != NO_TAG) {
4451 char order = np->order;
4454 ** Force ordered tag if necessary to avoid timeouts
4455 ** and to preserve interactivity.
4457 if (lp && ktime_exp(lp->tags_stime)) {
4458 if (lp->tags_smap) {
4459 order = M_ORDERED_TAG;
4460 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
4461 PRINT_ADDR(cmd);
4462 printk("ordered tag forced.\n");
4465 lp->tags_stime = ktime_get(3*HZ);
4466 lp->tags_smap = lp->tags_umap;
4469 if (order == 0) {
4471 ** Ordered write ops, unordered read ops.
4473 switch (cmd->cmnd[0]) {
4474 case 0x08: /* READ_SMALL (6) */
4475 case 0x28: /* READ_BIG (10) */
4476 case 0xa8: /* READ_HUGE (12) */
4477 order = M_SIMPLE_TAG;
4478 break;
4479 default:
4480 order = M_ORDERED_TAG;
4483 msgptr[msglen++] = order;
4485 ** Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
4486 ** since we may have to deal with devices that have
4487 ** problems with #TAG 0 or too great #TAG numbers.
4489 msgptr[msglen++] = (cp->tag << 1) + 1;
4492 /*----------------------------------------------------
4494 ** Build the data descriptors
4496 **----------------------------------------------------
4499 direction = scsi_data_direction(cmd);
4500 if (direction != SCSI_DATA_NONE) {
4501 segments = ncr_scatter (np, cp, cp->cmd);
4502 if (segments < 0) {
4503 ncr_free_ccb(np, cp);
4504 return(DID_ERROR);
4507 else {
4508 cp->data_len = 0;
4509 segments = 0;
4512 /*---------------------------------------------------
4514 ** negotiation required?
4516 ** (nego_status is filled by ncr_prepare_nego())
4518 **---------------------------------------------------
4521 cp->nego_status = 0;
4523 #ifdef SCSI_NCR_INTEGRITY_CHECKING
4524 if ((np->check_integrity && tp->ic_done) || !np->check_integrity) {
4525 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
4526 msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
4529 else if (np->check_integrity && (cmd->ic_in_progress)) {
4530 msglen += ncr_ic_nego (np, cp, cmd, msgptr + msglen);
4532 else if (np->check_integrity && cmd->ic_complete) {
4534 * Midlayer signal to the driver that all of the scsi commands
4535 * for the integrity check have completed. Save the negotiated
4536 * parameters (extracted from sval and wval).
4540 u_char idiv;
4541 idiv = (tp->wval>>4) & 0x07;
4542 if ((tp->sval&0x1f) && idiv )
4543 tp->period = (((tp->sval>>5)+4)
4544 *div_10M[idiv-1])/np->clock_khz;
4545 else
4546 tp->period = 0xffff;
4549 * tp->period contains 10 times the transfer period,
4550 * which itself is 4 * the requested negotiation rate.
4552 if (tp->period <= 250) tp->ic_min_sync = 10;
4553 else if (tp->period <= 303) tp->ic_min_sync = 11;
4554 else if (tp->period <= 500) tp->ic_min_sync = 12;
4555 else
4556 tp->ic_min_sync = (tp->period + 40 - 1) / 40;
4560 * Negotiation for this target it complete.
4562 tp->ic_max_width = (tp->wval & EWS) ? 1: 0;
4563 tp->ic_done = 1;
4564 tp->widedone = 1;
4566 printk("%s: Integrity Check Complete: \n", ncr_name(np));
4568 printk("%s: %s %s SCSI", ncr_name(np),
4569 (tp->sval&0x1f)?"SYNC":"ASYNC",
4570 tp->ic_max_width?"WIDE":"NARROW");
4572 if (tp->sval&0x1f) {
4573 u_long mbs = 10000 * (tp->ic_max_width + 1);
4575 printk(" %d.%d MB/s",
4576 (int) (mbs / tp->period), (int) (mbs % tp->period));
4578 printk(" (%d ns, %d offset)\n",
4579 tp->period/10, tp->sval&0x1f);
4581 else
4582 printk(" %d MB/s. \n ", (tp->ic_max_width+1)*5);
4584 #else
4585 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
4586 msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
4588 #endif /* SCSI_NCR_INTEGRITY_CHECKING */
4590 /*----------------------------------------------------
4592 ** Determine xfer direction.
4594 **----------------------------------------------------
4596 if (!cp->data_len)
4597 direction = SCSI_DATA_NONE;
4600 ** If data direction is UNKNOWN, speculate DATA_READ
4601 ** but prepare alternate pointers for WRITE in case
4602 ** of our speculation will be just wrong.
4603 ** SCRIPTS will swap values if needed.
4605 switch(direction) {
4606 case SCSI_DATA_UNKNOWN:
4607 case SCSI_DATA_WRITE:
4608 goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
4609 if (segments <= MAX_SCATTERL)
4610 lastp = goalp - 8 - (segments * 16);
4611 else {
4612 lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
4613 lastp -= (segments - MAX_SCATTERL) * 16;
4615 if (direction != SCSI_DATA_UNKNOWN)
4616 break;
4617 cp->phys.header.wgoalp = cpu_to_scr(goalp);
4618 cp->phys.header.wlastp = cpu_to_scr(lastp);
4619 /* fall through */
4620 case SCSI_DATA_READ:
4621 goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
4622 if (segments <= MAX_SCATTERL)
4623 lastp = goalp - 8 - (segments * 16);
4624 else {
4625 lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
4626 lastp -= (segments - MAX_SCATTERL) * 16;
4628 break;
4629 default:
4630 case SCSI_DATA_NONE:
4631 lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
4632 break;
4636 ** Set all pointers values needed by SCRIPTS.
4637 ** If direction is unknown, start at data_io.
4639 cp->phys.header.lastp = cpu_to_scr(lastp);
4640 cp->phys.header.goalp = cpu_to_scr(goalp);
4642 if (direction == SCSI_DATA_UNKNOWN)
4643 cp->phys.header.savep =
4644 cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
4645 else
4646 cp->phys.header.savep= cpu_to_scr(lastp);
4649 ** Save the initial data pointer in order to be able
4650 ** to redo the command.
4652 cp->startp = cp->phys.header.savep;
4654 /*----------------------------------------------------
4656 ** fill in ccb
4658 **----------------------------------------------------
4661 ** physical -> virtual backlink
4662 ** Generic SCSI command
4666 ** Startqueue
4668 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
4669 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
4671 ** select
4673 cp->phys.select.sel_id = cmd->target;
4674 cp->phys.select.sel_scntl3 = tp->wval;
4675 cp->phys.select.sel_sxfer = tp->sval;
4677 ** message
4679 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
4680 cp->phys.smsg.size = cpu_to_scr(msglen);
4683 ** command
4685 memcpy(cp->cdb_buf, cmd->cmnd, MIN(cmd->cmd_len, sizeof(cp->cdb_buf)));
4686 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
4687 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
4690 ** status
4692 cp->actualquirks = tp->quirks;
4693 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
4694 cp->scsi_status = S_ILLEGAL;
4695 cp->parity_status = 0;
4697 cp->xerr_status = XE_OK;
4698 #if 0
4699 cp->sync_status = tp->sval;
4700 cp->wide_status = tp->wval;
4701 #endif
4703 /*----------------------------------------------------
4705 ** Critical region: start this job.
4707 **----------------------------------------------------
4711 ** activate this job.
4713 cp->magic = CCB_MAGIC;
4716 ** insert next CCBs into start queue.
4717 ** 2 max at a time is enough to flush the CCB wait queue.
4719 cp->auto_sense = 0;
4720 if (lp)
4721 ncr_start_next_ccb(np, lp, 2);
4722 else
4723 ncr_put_start_queue(np, cp);
4726 ** Command is successfully queued.
4729 return(DID_OK);
4733 /*==========================================================
4736 ** Insert a CCB into the start queue and wake up the
4737 ** SCRIPTS processor.
4740 **==========================================================
4743 static void ncr_start_next_ccb(ncb_p np, lcb_p lp, int maxn)
4745 XPT_QUEHEAD *qp;
4746 ccb_p cp;
4748 if (lp->held_ccb)
4749 return;
4751 while (maxn-- && lp->queuedccbs < lp->queuedepth) {
4752 qp = xpt_remque_head(&lp->wait_ccbq);
4753 if (!qp)
4754 break;
4755 ++lp->queuedccbs;
4756 cp = xpt_que_entry(qp, struct ccb, link_ccbq);
4757 xpt_insque_tail(qp, &lp->busy_ccbq);
4758 lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
4759 cpu_to_scr(CCB_PHYS (cp, restart));
4760 ncr_put_start_queue(np, cp);
4764 static void ncr_put_start_queue(ncb_p np, ccb_p cp)
4766 u_short qidx;
4769 ** insert into start queue.
4771 if (!np->squeueput) np->squeueput = 1;
4772 qidx = np->squeueput + 2;
4773 if (qidx >= MAX_START + MAX_START) qidx = 1;
4775 np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
4776 MEMORY_BARRIER();
4777 np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
4779 np->squeueput = qidx;
4780 ++np->queuedccbs;
4781 cp->queued = 1;
4783 if (DEBUG_FLAGS & DEBUG_QUEUE)
4784 printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
4787 ** Script processor may be waiting for reselect.
4788 ** Wake it up.
4790 MEMORY_BARRIER();
4791 OUTB (nc_istat, SIGP);
4795 /*==========================================================
4798 ** Start reset process.
4799 ** If reset in progress do nothing.
4800 ** The interrupt handler will reinitialize the chip.
4801 ** The timeout handler will wait for settle_time before
4802 ** clearing it and so resuming command processing.
4805 **==========================================================
4807 static void ncr_start_reset(ncb_p np)
4809 if (!np->settle_time) {
4810 (void) ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
4814 static int ncr_reset_scsi_bus(ncb_p np, int enab_int, int settle_delay)
4816 u_int32 term;
4817 int retv = 0;
4819 np->settle_time = ktime_get(settle_delay * HZ);
4821 if (bootverbose > 1)
4822 printk("%s: resetting, "
4823 "command processing suspended for %d seconds\n",
4824 ncr_name(np), settle_delay);
4826 OUTB (nc_istat, SRST);
4827 UDELAY (100);
4828 OUTB (nc_istat, 0);
4829 UDELAY (2000); /* The 895 needs time for the bus mode to settle */
4830 if (enab_int)
4831 OUTW (nc_sien, RST);
4833 ** Enable Tolerant, reset IRQD if present and
4834 ** properly set IRQ mode, prior to resetting the bus.
4836 OUTB (nc_stest3, TE);
4837 OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
4838 OUTB (nc_scntl1, CRST);
4839 UDELAY (200);
4841 if (!driver_setup.bus_check)
4842 goto out;
4844 ** Check for no terminators or SCSI bus shorts to ground.
4845 ** Read SCSI data bus, data parity bits and control signals.
4846 ** We are expecting RESET to be TRUE and other signals to be
4847 ** FALSE.
4850 term = INB(nc_sstat0);
4851 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
4852 term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
4853 ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
4854 ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
4855 INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
4857 if (!(np->features & FE_WIDE))
4858 term &= 0x3ffff;
4860 if (term != (2<<7)) {
4861 printk("%s: suspicious SCSI data while resetting the BUS.\n",
4862 ncr_name(np));
4863 printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
4864 "0x%lx, expecting 0x%lx\n",
4865 ncr_name(np),
4866 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
4867 (u_long)term, (u_long)(2<<7));
4868 if (driver_setup.bus_check == 1)
4869 retv = 1;
4871 out:
4872 OUTB (nc_scntl1, 0);
4873 return retv;
4876 /*==========================================================
4879 ** Reset the SCSI BUS.
4880 ** This is called from the generic SCSI driver.
4883 **==========================================================
4885 static int ncr_reset_bus (ncb_p np, Scsi_Cmnd *cmd, int sync_reset)
4887 /* Scsi_Device *device = cmd->device; */
4888 ccb_p cp;
4889 int found;
4892 * Return immediately if reset is in progress.
4894 if (np->settle_time) {
4895 return SCSI_RESET_PUNT;
4898 * Start the reset process.
4899 * The script processor is then assumed to be stopped.
4900 * Commands will now be queued in the waiting list until a settle
4901 * delay of 2 seconds will be completed.
4903 ncr_start_reset(np);
4905 * First, look in the wakeup list
4907 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4909 ** look for the ccb of this command.
4911 if (cp->host_status == HS_IDLE) continue;
4912 if (cp->cmd == cmd) {
4913 found = 1;
4914 break;
4918 * Then, look in the waiting list
4920 if (!found && retrieve_from_waiting_list(0, np, cmd))
4921 found = 1;
4923 * Wake-up all awaiting commands with DID_RESET.
4925 reset_waiting_list(np);
4927 * Wake-up all pending commands with HS_RESET -> DID_RESET.
4929 ncr_wakeup(np, HS_RESET);
4931 * If the involved command was not in a driver queue, and the
4932 * scsi driver told us reset is synchronous, and the command is not
4933 * currently in the waiting list, complete it with DID_RESET status,
4934 * in order to keep it alive.
4936 if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
4937 cmd->result = ScsiResult(DID_RESET, 0);
4938 ncr_queue_done_cmd(np, cmd);
4941 return SCSI_RESET_SUCCESS;
4944 /*==========================================================
4947 ** Abort an SCSI command.
4948 ** This is called from the generic SCSI driver.
4951 **==========================================================
4953 static int ncr_abort_command (ncb_p np, Scsi_Cmnd *cmd)
4955 /* Scsi_Device *device = cmd->device; */
4956 ccb_p cp;
4957 int found;
4958 int retv;
4961 * First, look for the scsi command in the waiting list
4963 if (remove_from_waiting_list(np, cmd)) {
4964 cmd->result = ScsiResult(DID_ABORT, 0);
4965 ncr_queue_done_cmd(np, cmd);
4966 return SCSI_ABORT_SUCCESS;
4970 * Then, look in the wakeup list
4972 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4974 ** look for the ccb of this command.
4976 if (cp->host_status == HS_IDLE) continue;
4977 if (cp->cmd == cmd) {
4978 found = 1;
4979 break;
4983 if (!found) {
4984 return SCSI_ABORT_NOT_RUNNING;
4987 if (np->settle_time) {
4988 return SCSI_ABORT_SNOOZE;
4992 ** If the CCB is active, patch schedule jumps for the
4993 ** script to abort the command.
4996 switch(cp->host_status) {
4997 case HS_BUSY:
4998 case HS_NEGOTIATE:
4999 printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
5000 cp->start.schedule.l_paddr =
5001 cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
5002 retv = SCSI_ABORT_PENDING;
5003 break;
5004 case HS_DISCONNECT:
5005 cp->restart.schedule.l_paddr =
5006 cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
5007 retv = SCSI_ABORT_PENDING;
5008 break;
5009 default:
5010 retv = SCSI_ABORT_NOT_RUNNING;
5011 break;
5016 ** If there are no requests, the script
5017 ** processor will sleep on SEL_WAIT_RESEL.
5018 ** Let's wake it up, since it may have to work.
5020 OUTB (nc_istat, SIGP);
5022 return retv;
5025 /*==========================================================
5027 ** Linux release module stuff.
5029 ** Called before unloading the module
5030 ** Detach the host.
5031 ** We have to free resources and halt the NCR chip
5033 **==========================================================
5036 #ifdef MODULE
5037 static int ncr_detach(ncb_p np)
5039 ccb_p cp;
5040 tcb_p tp;
5041 lcb_p lp;
5042 int target, lun;
5043 int i;
5045 printk("%s: releasing host resources\n", ncr_name(np));
5048 ** Stop the ncr_timeout process
5049 ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
5052 #ifdef DEBUG_NCR53C8XX
5053 printk("%s: stopping the timer\n", ncr_name(np));
5054 #endif
5055 np->release_stage = 1;
5056 for (i = 50 ; i && np->release_stage != 2 ; i--) MDELAY (100);
5057 if (np->release_stage != 2)
5058 printk("%s: the timer seems to be already stopped\n", ncr_name(np));
5059 else np->release_stage = 2;
5062 ** Disable chip interrupts
5065 #ifdef DEBUG_NCR53C8XX
5066 printk("%s: disabling chip interrupts\n", ncr_name(np));
5067 #endif
5068 OUTW (nc_sien , 0);
5069 OUTB (nc_dien , 0);
5072 ** Free irq
5075 #ifdef DEBUG_NCR53C8XX
5076 #ifdef __sparc__
5077 printk("%s: freeing irq %s\n", ncr_name(np), __irq_itoa(np->irq));
5078 #else
5079 printk("%s: freeing irq %d\n", ncr_name(np), np->irq);
5080 #endif
5081 #endif
5082 free_irq(np->irq, np);
5085 ** Reset NCR chip
5086 ** Restore bios setting for automatic clock detection.
5089 printk("%s: resetting chip\n", ncr_name(np));
5090 OUTB (nc_istat, SRST);
5091 UDELAY (100);
5092 OUTB (nc_istat, 0 );
5094 OUTB(nc_dmode, np->sv_dmode);
5095 OUTB(nc_dcntl, np->sv_dcntl);
5096 OUTB(nc_ctest3, np->sv_ctest3);
5097 OUTB(nc_ctest4, np->sv_ctest4);
5098 OUTB(nc_ctest5, np->sv_ctest5);
5099 OUTB(nc_gpcntl, np->sv_gpcntl);
5100 OUTB(nc_stest2, np->sv_stest2);
5102 ncr_selectclock(np, np->sv_scntl3);
5105 ** Release Memory mapped IO region and IO mapped region
5108 #ifndef NCR_IOMAPPED
5109 #ifdef DEBUG_NCR53C8XX
5110 printk("%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
5111 #endif
5112 unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
5113 #endif /* !NCR_IOMAPPED */
5115 #ifdef DEBUG_NCR53C8XX
5116 printk("%s: releasing IO region %x[%d]\n", ncr_name(np), np->base_io, 128);
5117 #endif
5118 release_region(np->base_io, 128);
5121 ** Free allocated ccb(s)
5124 while ((cp=np->ccb->link_ccb) != NULL) {
5125 np->ccb->link_ccb = cp->link_ccb;
5126 if (cp->host_status) {
5127 printk("%s: shall free an active ccb (host_status=%d)\n",
5128 ncr_name(np), cp->host_status);
5130 #ifdef DEBUG_NCR53C8XX
5131 printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
5132 #endif
5133 m_free_dma(cp, sizeof(*cp), "CCB");
5137 ** Free allocated tp(s)
5140 for (target = 0; target < MAX_TARGET ; target++) {
5141 tp=&np->target[target];
5142 for (lun = 0 ; lun < MAX_LUN ; lun++) {
5143 lp = tp->lp[lun];
5144 if (lp) {
5145 #ifdef DEBUG_NCR53C8XX
5146 printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
5147 #endif
5148 if (lp->jump_ccb != &lp->jump_ccb_0)
5149 m_free_dma(lp->jump_ccb,256,"JUMP_CCB");
5150 m_free_dma(lp, sizeof(*lp), "LCB");
5155 if (np->scripth0)
5156 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
5157 if (np->script0)
5158 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
5159 if (np->ccb)
5160 m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
5161 m_free_dma(np, sizeof(struct ncb), "NCB");
5163 printk("%s: host resources successfully released\n", ncr_name(np));
5165 return 1;
5167 #endif
5169 /*==========================================================
5172 ** Complete execution of a SCSI command.
5173 ** Signal completion to the generic SCSI driver.
5176 **==========================================================
5179 void ncr_complete (ncb_p np, ccb_p cp)
5181 Scsi_Cmnd *cmd;
5182 tcb_p tp;
5183 lcb_p lp;
5186 ** Sanity check
5189 if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
5190 return;
5193 ** timestamp
5194 ** Optional, spare some CPU time
5196 #ifdef SCSI_NCR_PROFILE_SUPPORT
5197 ncb_profile (np, cp);
5198 #endif
5200 if (DEBUG_FLAGS & DEBUG_TINY)
5201 printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
5202 cp->host_status,cp->scsi_status);
5205 ** Get command, target and lun pointers.
5208 cmd = cp->cmd;
5209 cp->cmd = NULL;
5210 tp = &np->target[cmd->target];
5211 lp = tp->lp[cmd->lun];
5214 ** We donnot queue more than 1 ccb per target
5215 ** with negotiation at any time. If this ccb was
5216 ** used for negotiation, clear this info in the tcb.
5219 if (cp == tp->nego_cp)
5220 tp->nego_cp = 0;
5223 ** If auto-sense performed, change scsi status.
5225 if (cp->auto_sense) {
5226 cp->scsi_status = cp->auto_sense;
5230 ** If we were recovering from queue full or performing
5231 ** auto-sense, requeue skipped CCBs to the wait queue.
5234 if (lp && lp->held_ccb) {
5235 if (cp == lp->held_ccb) {
5236 xpt_que_splice(&lp->skip_ccbq, &lp->wait_ccbq);
5237 xpt_que_init(&lp->skip_ccbq);
5238 lp->held_ccb = 0;
5243 ** Check for parity errors.
5246 if (cp->parity_status > 1) {
5247 PRINT_ADDR(cmd);
5248 printk ("%d parity error(s).\n",cp->parity_status);
5252 ** Check for extended errors.
5255 if (cp->xerr_status != XE_OK) {
5256 PRINT_ADDR(cmd);
5257 switch (cp->xerr_status) {
5258 case XE_EXTRA_DATA:
5259 printk ("extraneous data discarded.\n");
5260 break;
5261 case XE_BAD_PHASE:
5262 printk ("illegal scsi phase (4/5).\n");
5263 break;
5264 default:
5265 printk ("extended error %d.\n", cp->xerr_status);
5266 break;
5268 if (cp->host_status==HS_COMPLETE)
5269 cp->host_status = HS_FAIL;
5273 ** Print out any error for debugging purpose.
5275 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5276 if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
5277 PRINT_ADDR(cmd);
5278 printk ("ERROR: cmd=%x host_status=%x scsi_status=%x\n",
5279 cmd->cmnd[0], cp->host_status, cp->scsi_status);
5284 ** Check the status.
5286 if ( (cp->host_status == HS_COMPLETE)
5287 && (cp->scsi_status == S_GOOD ||
5288 cp->scsi_status == S_COND_MET)) {
5290 ** All went well (GOOD status).
5291 ** CONDITION MET status is returned on
5292 ** `Pre-Fetch' or `Search data' success.
5294 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5297 ** @RESID@
5298 ** Could dig out the correct value for resid,
5299 ** but it would be quite complicated.
5301 /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
5304 ** Allocate the lcb if not yet.
5306 if (!lp)
5307 ncr_alloc_lcb (np, cmd->target, cmd->lun);
5310 ** On standard INQUIRY response (EVPD and CmDt
5311 ** not set), setup logical unit according to
5312 ** announced capabilities (we need the 1rst 7 bytes).
5314 if (cmd->cmnd[0] == 0x12 && !(cmd->cmnd[1] & 0x3) &&
5315 cmd->cmnd[4] >= 7 && !cmd->use_sg) {
5316 sync_scsi_data(np, cmd); /* SYNC the data */
5317 ncr_setup_lcb (np, cmd->target, cmd->lun,
5318 (char *) cmd->request_buffer);
5321 tp->bytes += cp->data_len;
5322 tp->transfers ++;
5325 ** If tags was reduced due to queue full,
5326 ** increase tags if 1000 good status received.
5328 if (lp && lp->usetags && lp->numtags < lp->maxtags) {
5329 ++lp->num_good;
5330 if (lp->num_good >= 1000) {
5331 lp->num_good = 0;
5332 ++lp->numtags;
5333 ncr_setup_tags (np, cmd->target, cmd->lun);
5336 } else if ((cp->host_status == HS_COMPLETE)
5337 && (cp->scsi_status == S_CHECK_COND)) {
5339 ** Check condition code
5341 cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
5344 ** Copy back sense data to caller's buffer.
5346 memcpy(cmd->sense_buffer, cp->sense_buf,
5347 MIN(sizeof(cmd->sense_buffer), sizeof(cp->sense_buf)));
5349 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5350 u_char * p = (u_char*) & cmd->sense_buffer;
5351 int i;
5352 PRINT_ADDR(cmd);
5353 printk ("sense data:");
5354 for (i=0; i<14; i++) printk (" %x", *p++);
5355 printk (".\n");
5357 } else if ((cp->host_status == HS_COMPLETE)
5358 && (cp->scsi_status == S_CONFLICT)) {
5360 ** Reservation Conflict condition code
5362 cmd->result = ScsiResult(DID_OK, S_CONFLICT);
5364 } else if ((cp->host_status == HS_COMPLETE)
5365 && (cp->scsi_status == S_BUSY ||
5366 cp->scsi_status == S_QUEUE_FULL)) {
5369 ** Target is busy.
5371 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5373 } else if ((cp->host_status == HS_SEL_TIMEOUT)
5374 || (cp->host_status == HS_TIMEOUT)) {
5377 ** No response
5379 cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
5381 } else if (cp->host_status == HS_RESET) {
5384 ** SCSI bus reset
5386 cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
5388 } else if (cp->host_status == HS_ABORTED) {
5391 ** Transfer aborted
5393 cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
5395 } else {
5398 ** Other protocol messes
5400 PRINT_ADDR(cmd);
5401 printk ("COMMAND FAILED (%x %x) @%p.\n",
5402 cp->host_status, cp->scsi_status, cp);
5404 cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
5408 ** trace output
5411 if (tp->usrflag & UF_TRACE) {
5412 u_char * p;
5413 int i;
5414 PRINT_ADDR(cmd);
5415 printk (" CMD:");
5416 p = (u_char*) &cmd->cmnd[0];
5417 for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
5419 if (cp->host_status==HS_COMPLETE) {
5420 switch (cp->scsi_status) {
5421 case S_GOOD:
5422 printk (" GOOD");
5423 break;
5424 case S_CHECK_COND:
5425 printk (" SENSE:");
5426 p = (u_char*) &cmd->sense_buffer;
5427 for (i=0; i<14; i++)
5428 printk (" %x", *p++);
5429 break;
5430 default:
5431 printk (" STAT: %x\n", cp->scsi_status);
5432 break;
5434 } else printk (" HOSTERROR: %x", cp->host_status);
5435 printk ("\n");
5439 ** Free this ccb
5441 ncr_free_ccb (np, cp);
5444 ** requeue awaiting scsi commands for this lun.
5446 if (lp && lp->queuedccbs < lp->queuedepth &&
5447 !xpt_que_empty(&lp->wait_ccbq))
5448 ncr_start_next_ccb(np, lp, 2);
5451 ** requeue awaiting scsi commands for this controller.
5453 if (np->waiting_list)
5454 requeue_waiting_list(np);
5457 ** signal completion to generic driver.
5459 ncr_queue_done_cmd(np, cmd);
5462 /*==========================================================
5465 ** Signal all (or one) control block done.
5468 **==========================================================
5472 ** This CCB has been skipped by the NCR.
5473 ** Queue it in the correponding unit queue.
5475 static void ncr_ccb_skipped(ncb_p np, ccb_p cp)
5477 tcb_p tp = &np->target[cp->target];
5478 lcb_p lp = tp->lp[cp->lun];
5480 if (lp && cp != np->ccb) {
5481 cp->host_status &= ~HS_SKIPMASK;
5482 cp->start.schedule.l_paddr =
5483 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5484 xpt_remque(&cp->link_ccbq);
5485 xpt_insque_tail(&cp->link_ccbq, &lp->skip_ccbq);
5486 if (cp->queued) {
5487 --lp->queuedccbs;
5490 if (cp->queued) {
5491 --np->queuedccbs;
5492 cp->queued = 0;
5497 ** The NCR has completed CCBs.
5498 ** Look at the DONE QUEUE if enabled, otherwise scan all CCBs
5500 void ncr_wakeup_done (ncb_p np)
5502 ccb_p cp;
5503 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5504 int i, j;
5506 i = np->ccb_done_ic;
5507 while (1) {
5508 j = i+1;
5509 if (j >= MAX_DONE)
5510 j = 0;
5512 cp = np->ccb_done[j];
5513 if (!CCB_DONE_VALID(cp))
5514 break;
5516 np->ccb_done[j] = (ccb_p) CCB_DONE_EMPTY;
5517 np->scripth->done_queue[5*j + 4] =
5518 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5519 MEMORY_BARRIER();
5520 np->scripth->done_queue[5*i + 4] =
5521 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5523 if (cp->host_status & HS_DONEMASK)
5524 ncr_complete (np, cp);
5525 else if (cp->host_status & HS_SKIPMASK)
5526 ncr_ccb_skipped (np, cp);
5528 i = j;
5530 np->ccb_done_ic = i;
5531 #else
5532 cp = np->ccb;
5533 while (cp) {
5534 if (cp->host_status & HS_DONEMASK)
5535 ncr_complete (np, cp);
5536 else if (cp->host_status & HS_SKIPMASK)
5537 ncr_ccb_skipped (np, cp);
5538 cp = cp->link_ccb;
5540 #endif
5544 ** Complete all active CCBs.
5546 void ncr_wakeup (ncb_p np, u_long code)
5548 ccb_p cp = np->ccb;
5550 while (cp) {
5551 if (cp->host_status != HS_IDLE) {
5552 cp->host_status = code;
5553 ncr_complete (np, cp);
5555 cp = cp->link_ccb;
5559 /*==========================================================
5562 ** Start NCR chip.
5565 **==========================================================
5568 void ncr_init (ncb_p np, int reset, char * msg, u_long code)
5570 int i;
5573 ** Reset chip if asked, otherwise just clear fifos.
5576 if (reset) {
5577 OUTB (nc_istat, SRST);
5578 UDELAY (100);
5580 else {
5581 OUTB (nc_stest3, TE|CSF);
5582 OUTONB (nc_ctest3, CLF);
5586 ** Message.
5589 if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
5592 ** Clear Start Queue
5594 np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
5595 for (i = 1; i < MAX_START + MAX_START; i += 2)
5596 np->scripth0->tryloop[i] =
5597 cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5600 ** Start at first entry.
5602 np->squeueput = 0;
5603 np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
5606 ** Clear Done Queue
5608 for (i = 0; i < MAX_DONE; i++) {
5609 np->ccb_done[i] = (ccb_p) CCB_DONE_EMPTY;
5610 np->scripth0->done_queue[5*i + 4] =
5611 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5615 ** Start at first entry.
5617 np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
5618 np->ccb_done_ic = MAX_DONE-1;
5619 np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
5620 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5623 ** Wakeup all pending jobs.
5625 ncr_wakeup (np, code);
5628 ** Init chip.
5631 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort */
5632 UDELAY (2000); /* The 895 needs time for the bus mode to settle */
5634 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
5635 /* full arb., ena parity, par->ATN */
5636 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
5638 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
5640 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
5641 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
5642 OUTB (nc_istat , SIGP ); /* Signal Process */
5643 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
5644 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
5646 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
5647 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
5648 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
5650 OUTB (nc_stest2, EXT|np->rv_stest2); /* Extended Sreq/Sack filtering */
5651 OUTB (nc_stest3, TE); /* TolerANT enable */
5652 OUTB (nc_stime0, 0x0c ); /* HTH disabled STO 0.25 sec */
5655 ** Disable disconnects.
5658 np->disc = 0;
5661 ** Enable GPIO0 pin for writing if LED support.
5664 if (np->features & FE_LED0) {
5665 OUTOFFB (nc_gpcntl, 0x01);
5669 ** enable ints
5672 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
5673 OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
5676 ** For 895/6 enable SBMC interrupt and save current SCSI bus mode.
5678 if (np->features & FE_ULTRA2) {
5679 OUTONW (nc_sien, SBMC);
5680 np->scsi_mode = INB (nc_stest4) & SMODE;
5684 ** DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
5685 ** Disable overlapped arbitration.
5686 ** All 896 chips are also affected by this errata.
5688 if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
5689 OUTB (nc_ctest0, (1<<5));
5690 else if (np->device_id == PCI_DEVICE_ID_NCR_53C896)
5691 OUTB (nc_ccntl0, DPR);
5694 ** Fill in target structure.
5695 ** Reinitialize usrsync.
5696 ** Reinitialize usrwide.
5697 ** Prepare sync negotiation according to actual SCSI bus mode.
5700 for (i=0;i<MAX_TARGET;i++) {
5701 tcb_p tp = &np->target[i];
5703 tp->sval = 0;
5704 tp->wval = np->rv_scntl3;
5706 if (tp->usrsync != 255) {
5707 if (tp->usrsync <= np->maxsync) {
5708 if (tp->usrsync < np->minsync) {
5709 tp->usrsync = np->minsync;
5712 else
5713 tp->usrsync = 255;
5716 if (tp->usrwide > np->maxwide)
5717 tp->usrwide = np->maxwide;
5719 ncr_negotiate (np, tp);
5723 ** Start script processor.
5725 MEMORY_BARRIER();
5726 if (np->paddr2) {
5727 if (bootverbose)
5728 printk ("%s: Downloading SCSI SCRIPTS.\n",
5729 ncr_name(np));
5730 OUTL (nc_scratcha, vtobus(np->script0));
5731 OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, start_ram));
5733 else
5734 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
5737 /*==========================================================
5739 ** Prepare the negotiation values for wide and
5740 ** synchronous transfers.
5742 **==========================================================
5745 static void ncr_negotiate (struct ncb* np, struct tcb* tp)
5748 ** minsync unit is 4ns !
5751 u_long minsync = tp->usrsync;
5754 ** SCSI bus mode limit
5757 if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
5758 if (minsync < 12) minsync = 12;
5762 ** our limit ..
5765 if (minsync < np->minsync)
5766 minsync = np->minsync;
5769 ** divider limit
5772 if (minsync > np->maxsync)
5773 minsync = 255;
5775 tp->minsync = minsync;
5776 tp->maxoffs = (minsync<255 ? np->maxoffs : 0);
5779 ** period=0: has to negotiate sync transfer
5782 tp->period=0;
5785 ** widedone=0: has to negotiate wide transfer
5787 tp->widedone=0;
5790 /*==========================================================
5792 ** Get clock factor and sync divisor for a given
5793 ** synchronous factor period.
5794 ** Returns the clock factor (in sxfer) and scntl3
5795 ** synchronous divisor field.
5797 **==========================================================
5800 static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
5802 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
5803 int div = np->clock_divn; /* Number of divisors supported */
5804 u_long fak; /* Sync factor in sxfer */
5805 u_long per; /* Period in tenths of ns */
5806 u_long kpc; /* (per * clk) */
5809 ** Compute the synchronous period in tenths of nano-seconds
5811 if (sfac <= 10) per = 250;
5812 else if (sfac == 11) per = 303;
5813 else if (sfac == 12) per = 500;
5814 else per = 40 * sfac;
5817 ** Look for the greatest clock divisor that allows an
5818 ** input speed faster than the period.
5820 kpc = per * clk;
5821 while (--div >= 0)
5822 if (kpc >= (div_10M[div] << 2)) break;
5825 ** Calculate the lowest clock factor that allows an output
5826 ** speed not faster than the period.
5828 fak = (kpc - 1) / div_10M[div] + 1;
5830 #if 0 /* This optimization does not seem very usefull */
5832 per = (fak * div_10M[div]) / clk;
5835 ** Why not to try the immediate lower divisor and to choose
5836 ** the one that allows the fastest output speed ?
5837 ** We dont want input speed too much greater than output speed.
5839 if (div >= 1 && fak < 8) {
5840 u_long fak2, per2;
5841 fak2 = (kpc - 1) / div_10M[div-1] + 1;
5842 per2 = (fak2 * div_10M[div-1]) / clk;
5843 if (per2 < per && fak2 <= 8) {
5844 fak = fak2;
5845 per = per2;
5846 --div;
5849 #endif
5851 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
5854 ** Compute and return sync parameters for the ncr
5856 *fakp = fak - 4;
5857 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
5861 /*==========================================================
5863 ** Set actual values, sync status and patch all ccbs of
5864 ** a target according to new sync/wide agreement.
5866 **==========================================================
5869 static void ncr_set_sync_wide_status (ncb_p np, u_char target)
5871 ccb_p cp;
5872 tcb_p tp = &np->target[target];
5875 ** set actual value and sync_status
5877 OUTB (nc_sxfer, tp->sval);
5878 np->sync_st = tp->sval;
5879 OUTB (nc_scntl3, tp->wval);
5880 np->wide_st = tp->wval;
5883 ** patch ALL ccbs of this target.
5885 for (cp = np->ccb; cp; cp = cp->link_ccb) {
5886 if (!cp->cmd) continue;
5887 if (cp->cmd->target != target) continue;
5888 #if 0
5889 cp->sync_status = tp->sval;
5890 cp->wide_status = tp->wval;
5891 #endif
5892 cp->phys.select.sel_scntl3 = tp->wval;
5893 cp->phys.select.sel_sxfer = tp->sval;
5897 /*==========================================================
5899 ** Switch sync mode for current job and it's target
5901 **==========================================================
5904 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer)
5906 Scsi_Cmnd *cmd;
5907 tcb_p tp;
5908 u_char target = INB (nc_sdid) & 0x0f;
5909 u_char idiv;
5911 assert (cp && cp->cmd);
5912 if (!cp) return;
5914 cmd = cp->cmd;
5915 if (!cmd) return;
5917 assert (target == (cmd->target & 0xf));
5919 tp = &np->target[target];
5921 if (!scntl3 || !(sxfer & 0x1f))
5922 scntl3 = np->rv_scntl3;
5923 scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
5926 ** Deduce the value of controller sync period from scntl3.
5927 ** period is in tenths of nano-seconds.
5930 idiv = ((scntl3 >> 4) & 0x7);
5931 if ((sxfer & 0x1f) && idiv)
5932 tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
5933 else
5934 tp->period = 0xffff;
5937 ** Stop there if sync parameters are unchanged
5939 if (tp->sval == sxfer && tp->wval == scntl3) return;
5940 tp->sval = sxfer;
5941 tp->wval = scntl3;
5944 ** Bells and whistles ;-)
5946 PRINT_TARGET(np, target);
5947 if (sxfer & 0x01f) {
5948 unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
5949 unsigned mb10 = (f10 + tp->period/2) / tp->period;
5950 char *scsi;
5953 ** Disable extended Sreq/Sack filtering
5955 if (tp->period <= 2000) OUTOFFB (nc_stest2, EXT);
5958 ** Bells and whistles ;-)
5960 if (tp->period < 500) scsi = "FAST-40";
5961 else if (tp->period < 1000) scsi = "FAST-20";
5962 else if (tp->period < 2000) scsi = "FAST-10";
5963 else scsi = "FAST-5";
5965 printk ("%s %sSCSI %d.%d MB/s (%d ns, offset %d)\n", scsi,
5966 tp->widedone > 1 ? "WIDE " : "",
5967 mb10 / 10, mb10 % 10, tp->period / 10, sxfer & 0x1f);
5968 } else
5969 printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
5972 ** set actual value and sync_status
5973 ** patch ALL ccbs of this target.
5975 ncr_set_sync_wide_status(np, target);
5978 /*==========================================================
5980 ** Switch wide mode for current job and it's target
5981 ** SCSI specs say: a SCSI device that accepts a WDTR
5982 ** message shall reset the synchronous agreement to
5983 ** asynchronous mode.
5985 **==========================================================
5988 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack)
5990 Scsi_Cmnd *cmd;
5991 u_short target = INB (nc_sdid) & 0x0f;
5992 tcb_p tp;
5993 u_char scntl3;
5994 u_char sxfer;
5996 assert (cp && cp->cmd);
5997 if (!cp) return;
5999 cmd = cp->cmd;
6000 if (!cmd) return;
6002 assert (target == (cmd->target & 0xf));
6004 tp = &np->target[target];
6005 tp->widedone = wide+1;
6006 scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
6008 sxfer = ack ? 0 : tp->sval;
6011 ** Stop there if sync/wide parameters are unchanged
6013 if (tp->sval == sxfer && tp->wval == scntl3) return;
6014 tp->sval = sxfer;
6015 tp->wval = scntl3;
6018 ** Bells and whistles ;-)
6020 if (bootverbose >= 2) {
6021 PRINT_TARGET(np, target);
6022 if (scntl3 & EWS)
6023 printk ("WIDE SCSI (16 bit) enabled.\n");
6024 else
6025 printk ("WIDE SCSI disabled.\n");
6029 ** set actual value and sync_status
6030 ** patch ALL ccbs of this target.
6032 ncr_set_sync_wide_status(np, target);
6035 /*==========================================================
6037 ** Switch tagged mode for a target.
6039 **==========================================================
6042 static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln)
6044 tcb_p tp = &np->target[tn];
6045 lcb_p lp = tp->lp[ln];
6046 u_char reqtags, maxdepth;
6049 ** Just in case ...
6051 if ((!tp) || (!lp))
6052 return;
6055 ** If SCSI device queue depth is not yet set, leave here.
6057 if (!lp->scdev_depth)
6058 return;
6061 ** Donnot allow more tags than the SCSI driver can queue
6062 ** for this device.
6063 ** Donnot allow more tags than we can handle.
6065 maxdepth = lp->scdev_depth;
6066 if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
6067 if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
6068 if (lp->numtags > maxdepth) lp->numtags = maxdepth;
6071 ** only devices conformant to ANSI Version >= 2
6072 ** only devices capable of tagged commands
6073 ** only if enabled by user ..
6075 if ((lp->inq_byte7 & INQ7_QUEUE) && lp->numtags > 1) {
6076 reqtags = lp->numtags;
6077 } else {
6078 reqtags = 1;
6082 ** Update max number of tags
6084 lp->numtags = reqtags;
6085 if (lp->numtags > lp->maxtags)
6086 lp->maxtags = lp->numtags;
6089 ** If we want to switch tag mode, we must wait
6090 ** for no CCB to be active.
6092 if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
6093 if (lp->queuedepth == reqtags) /* Already announced */
6094 return;
6095 lp->queuedepth = reqtags;
6097 else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
6098 lp->queuedepth = reqtags;
6099 return;
6101 else { /* Want to switch tag mode */
6102 if (lp->busyccbs) /* If not yet safe, return */
6103 return;
6104 lp->queuedepth = reqtags;
6105 lp->usetags = reqtags > 1 ? 1 : 0;
6109 ** Patch the lun mini-script, according to tag mode.
6111 lp->jump_tag.l_paddr = lp->usetags?
6112 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
6113 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
6116 ** Announce change to user.
6118 if (bootverbose) {
6119 PRINT_LUN(np, tn, ln);
6120 if (lp->usetags) {
6121 printk("tagged command queue depth set to %d\n", reqtags);
6123 else {
6124 printk("tagged command queueing disabled\n");
6129 /*----------------------------------------------------
6131 ** handle user commands
6133 **----------------------------------------------------
6136 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
6138 static void ncr_usercmd (ncb_p np)
6140 u_char t;
6141 tcb_p tp;
6143 switch (np->user.cmd) {
6145 case 0: return;
6147 case UC_SETSYNC:
6148 for (t=0; t<MAX_TARGET; t++) {
6149 if (!((np->user.target>>t)&1)) continue;
6150 tp = &np->target[t];
6151 tp->usrsync = np->user.data;
6152 ncr_negotiate (np, tp);
6154 break;
6156 case UC_SETTAGS:
6157 for (t=0; t<MAX_TARGET; t++) {
6158 int ln;
6159 if (!((np->user.target>>t)&1)) continue;
6160 np->target[t].usrtags = np->user.data;
6161 for (ln = 0; ln < MAX_LUN; ln++) {
6162 lcb_p lp = np->target[t].lp[ln];
6163 if (!lp)
6164 continue;
6165 lp->maxtags = lp->numtags = np->user.data;
6166 ncr_setup_tags (np, t, ln);
6169 break;
6171 case UC_SETDEBUG:
6172 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
6173 ncr_debug = np->user.data;
6174 #endif
6175 break;
6177 case UC_SETORDER:
6178 np->order = np->user.data;
6179 break;
6181 case UC_SETVERBOSE:
6182 np->verbose = np->user.data;
6183 break;
6185 case UC_SETWIDE:
6186 for (t=0; t<MAX_TARGET; t++) {
6187 u_long size;
6188 if (!((np->user.target>>t)&1)) continue;
6189 tp = &np->target[t];
6190 size = np->user.data;
6191 if (size > np->maxwide) size=np->maxwide;
6192 tp->usrwide = size;
6193 ncr_negotiate (np, tp);
6195 break;
6197 case UC_SETFLAG:
6198 for (t=0; t<MAX_TARGET; t++) {
6199 if (!((np->user.target>>t)&1)) continue;
6200 tp = &np->target[t];
6201 tp->usrflag = np->user.data;
6203 break;
6205 #ifdef SCSI_NCR_PROFILE_SUPPORT
6206 case UC_CLEARPROF:
6207 bzero(&np->profile, sizeof(np->profile));
6208 break;
6209 #endif
6211 np->user.cmd=0;
6213 #endif
6215 /*==========================================================
6218 ** ncr timeout handler.
6221 **==========================================================
6223 ** Misused to keep the driver running when
6224 ** interrupts are not configured correctly.
6226 **----------------------------------------------------------
6229 static void ncr_timeout (ncb_p np)
6231 u_long thistime = ktime_get(0);
6234 ** If release process in progress, let's go
6235 ** Set the release stage from 1 to 2 to synchronize
6236 ** with the release process.
6239 if (np->release_stage) {
6240 if (np->release_stage == 1) np->release_stage = 2;
6241 return;
6244 #ifdef SCSI_NCR_PROFILE_SUPPORT
6245 np->ktime = thistime;
6246 np->timer.expires = ktime_get(1);
6247 #else
6248 np->timer.expires = ktime_get(SCSI_NCR_TIMER_INTERVAL);
6249 #endif
6250 add_timer(&np->timer);
6253 ** If we are resetting the ncr, wait for settle_time before
6254 ** clearing it. Then command processing will be resumed.
6256 if (np->settle_time) {
6257 if (np->settle_time <= thistime) {
6258 if (bootverbose > 1)
6259 printk("%s: command processing resumed\n", ncr_name(np));
6260 np->settle_time = 0;
6261 np->disc = 1;
6262 requeue_waiting_list(np);
6264 return;
6268 ** Since the generic scsi driver only allows us 0.5 second
6269 ** to perform abort of a command, we must look at ccbs about
6270 ** every 0.25 second.
6272 if (np->lasttime + 4*HZ < thistime) {
6274 ** block ncr interrupts
6276 np->lasttime = thistime;
6278 #ifdef SCSI_NCR_PROFILE_SUPPORT
6280 ** Reset profile data to avoid ugly overflow
6281 ** (Limited to 1024 GB for 32 bit architecture)
6283 if (np->profile.num_kbytes > (~0UL >> 2))
6284 bzero(&np->profile, sizeof(np->profile));
6285 #endif
6288 #ifdef SCSI_NCR_BROKEN_INTR
6289 if (INB(nc_istat) & (INTF|SIP|DIP)) {
6292 ** Process pending interrupts.
6294 if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
6295 ncr_exception (np);
6296 if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
6298 #endif /* SCSI_NCR_BROKEN_INTR */
6301 /*==========================================================
6303 ** log message for real hard errors
6305 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
6306 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
6308 ** exception register:
6309 ** ds: dstat
6310 ** si: sist
6312 ** SCSI bus lines:
6313 ** so: control lines as driver by NCR.
6314 ** si: control lines as seen by NCR.
6315 ** sd: scsi data lines as seen by NCR.
6317 ** wide/fastmode:
6318 ** sxfer: (see the manual)
6319 ** scntl3: (see the manual)
6321 ** current script command:
6322 ** dsp: script address (relative to start of script).
6323 ** dbc: first word of script command.
6325 ** First 16 register of the chip:
6326 ** r0..rf
6328 **==========================================================
6331 static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
6333 u_int32 dsp;
6334 int script_ofs;
6335 int script_size;
6336 char *script_name;
6337 u_char *script_base;
6338 int i;
6340 dsp = INL (nc_dsp);
6342 if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
6343 script_ofs = dsp - np->p_script;
6344 script_size = sizeof(struct script);
6345 script_base = (u_char *) np->script0;
6346 script_name = "script";
6348 else if (np->p_scripth < dsp &&
6349 dsp <= np->p_scripth + sizeof(struct scripth)) {
6350 script_ofs = dsp - np->p_scripth;
6351 script_size = sizeof(struct scripth);
6352 script_base = (u_char *) np->scripth0;
6353 script_name = "scripth";
6354 } else {
6355 script_ofs = dsp;
6356 script_size = 0;
6357 script_base = 0;
6358 script_name = "mem";
6361 printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
6362 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
6363 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
6364 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
6365 (unsigned)INL (nc_dbc));
6367 if (((script_ofs & 3) == 0) &&
6368 (unsigned)script_ofs < script_size) {
6369 printk ("%s: script cmd = %08x\n", ncr_name(np),
6370 scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
6373 printk ("%s: regdump:", ncr_name(np));
6374 for (i=0; i<16;i++)
6375 printk (" %02x", (unsigned)INB_OFF(i));
6376 printk (".\n");
6379 /*============================================================
6381 ** ncr chip exception handler.
6383 **============================================================
6385 ** In normal cases, interrupt conditions occur one at a
6386 ** time. The ncr is able to stack in some extra registers
6387 ** other interrupts that will occurs after the first one.
6388 ** But severall interrupts may occur at the same time.
6390 ** We probably should only try to deal with the normal
6391 ** case, but it seems that multiple interrupts occur in
6392 ** some cases that are not abnormal at all.
6394 ** The most frequent interrupt condition is Phase Mismatch.
6395 ** We should want to service this interrupt quickly.
6396 ** A SCSI parity error may be delivered at the same time.
6397 ** The SIR interrupt is not very frequent in this driver,
6398 ** since the INTFLY is likely used for command completion
6399 ** signaling.
6400 ** The Selection Timeout interrupt may be triggered with
6401 ** IID and/or UDC.
6402 ** The SBMC interrupt (SCSI Bus Mode Change) may probably
6403 ** occur at any time.
6405 ** This handler try to deal as cleverly as possible with all
6406 ** the above.
6408 **============================================================
6411 void ncr_exception (ncb_p np)
6413 u_char istat, dstat;
6414 u_short sist;
6415 int i;
6418 ** interrupt on the fly ?
6419 ** Since the global header may be copied back to a CCB
6420 ** using a posted PCI memory write, the last operation on
6421 ** the istat register is a READ in order to flush posted
6422 ** PCI write commands.
6424 istat = INB (nc_istat);
6425 if (istat & INTF) {
6426 OUTB (nc_istat, (istat & SIGP) | INTF);
6427 istat = INB (nc_istat);
6428 if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
6429 #ifdef SCSI_NCR_PROFILE_SUPPORT
6430 np->profile.num_fly++;
6431 #endif
6432 ncr_wakeup_done (np);
6435 if (!(istat & (SIP|DIP)))
6436 return;
6438 #ifdef SCSI_NCR_PROFILE_SUPPORT
6439 np->profile.num_int++;
6440 #endif
6442 if (istat & CABRT)
6443 OUTB (nc_istat, CABRT);
6446 ** Steinbach's Guideline for Systems Programming:
6447 ** Never test for an error condition you don't know how to handle.
6450 sist = (istat & SIP) ? INW (nc_sist) : 0;
6451 dstat = (istat & DIP) ? INB (nc_dstat) : 0;
6453 if (DEBUG_FLAGS & DEBUG_TINY)
6454 printk ("<%d|%x:%x|%x:%x>",
6455 (int)INB(nc_scr0),
6456 dstat,sist,
6457 (unsigned)INL(nc_dsp),
6458 (unsigned)INL(nc_dbc));
6460 /*========================================================
6461 ** First, interrupts we want to service cleanly.
6463 ** Phase mismatch is the most frequent interrupt, and
6464 ** so we have to service it as quickly and as cleanly
6465 ** as possible.
6466 ** Programmed interrupts are rarely used in this driver,
6467 ** but we must handle them cleanly anyway.
6468 ** We try to deal with PAR and SBMC combined with
6469 ** some other interrupt(s).
6470 **=========================================================
6473 if (!(sist & (STO|GEN|HTH|SGE|UDC|RST)) &&
6474 !(dstat & (MDPE|BF|ABRT|IID))) {
6475 if ((sist & SBMC) && ncr_int_sbmc (np))
6476 return;
6477 if ((sist & PAR) && ncr_int_par (np))
6478 return;
6479 if (sist & MA) {
6480 ncr_int_ma (np);
6481 return;
6483 if (dstat & SIR) {
6484 ncr_int_sir (np);
6485 return;
6488 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
6490 if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
6491 printk( "%s: unknown interrupt(s) ignored, "
6492 "ISTAT=%x DSTAT=%x SIST=%x\n",
6493 ncr_name(np), istat, dstat, sist);
6494 return;
6496 OUTONB (nc_dcntl, (STD|NOCOM));
6497 return;
6500 /*========================================================
6501 ** Now, interrupts that need some fixing up.
6502 ** Order and multiple interrupts is so less important.
6504 ** If SRST has been asserted, we just reset the chip.
6506 ** Selection is intirely handled by the chip. If the
6507 ** chip says STO, we trust it. Seems some other
6508 ** interrupts may occur at the same time (UDC, IID), so
6509 ** we ignore them. In any case we do enough fix-up
6510 ** in the service routine.
6511 ** We just exclude some fatal dma errors.
6512 **=========================================================
6515 if (sist & RST) {
6516 ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
6517 return;
6520 if ((sist & STO) &&
6521 !(dstat & (MDPE|BF|ABRT))) {
6523 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
6525 OUTONB (nc_ctest3, CLF);
6527 ncr_int_sto (np);
6528 return;
6531 /*=========================================================
6532 ** Now, interrupts we are not able to recover cleanly.
6533 ** (At least for the moment).
6535 ** Do the register dump.
6536 ** Log message for real hard errors.
6537 ** Clear all fifos.
6538 ** For MDPE, BF, ABORT, IID, SGE and HTH we reset the
6539 ** BUS and the chip.
6540 ** We are more soft for UDC.
6541 **=========================================================
6544 if (ktime_exp(np->regtime)) {
6545 np->regtime = ktime_get(10*HZ);
6546 for (i = 0; i<sizeof(np->regdump); i++)
6547 ((char*)&np->regdump)[i] = INB_OFF(i);
6548 np->regdump.nc_dstat = dstat;
6549 np->regdump.nc_sist = sist;
6552 ncr_log_hard_error(np, sist, dstat);
6554 printk ("%s: have to clear fifos.\n", ncr_name (np));
6555 OUTB (nc_stest3, TE|CSF);
6556 OUTONB (nc_ctest3, CLF);
6558 if ((sist & (SGE)) ||
6559 (dstat & (MDPE|BF|ABRT|IID))) {
6560 ncr_start_reset(np);
6561 return;
6564 if (sist & HTH) {
6565 printk ("%s: handshake timeout\n", ncr_name(np));
6566 ncr_start_reset(np);
6567 return;
6570 if (sist & UDC) {
6571 printk ("%s: unexpected disconnect\n", ncr_name(np));
6572 OUTB (HS_PRT, HS_UNEXPECTED);
6573 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
6574 return;
6577 /*=========================================================
6578 ** We just miss the cause of the interrupt. :(
6579 ** Print a message. The timeout will do the real work.
6580 **=========================================================
6582 printk ("%s: unknown interrupt\n", ncr_name(np));
6585 /*==========================================================
6587 ** ncr chip exception handler for selection timeout
6589 **==========================================================
6591 ** There seems to be a bug in the 53c810.
6592 ** Although a STO-Interrupt is pending,
6593 ** it continues executing script commands.
6594 ** But it will fail and interrupt (IID) on
6595 ** the next instruction where it's looking
6596 ** for a valid phase.
6598 **----------------------------------------------------------
6601 void ncr_int_sto (ncb_p np)
6603 u_long dsa;
6604 ccb_p cp;
6605 if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
6608 ** look for ccb and set the status.
6611 dsa = INL (nc_dsa);
6612 cp = np->ccb;
6613 while (cp && (CCB_PHYS (cp, phys) != dsa))
6614 cp = cp->link_ccb;
6616 if (cp) {
6617 cp-> host_status = HS_SEL_TIMEOUT;
6618 ncr_complete (np, cp);
6622 ** repair start queue and jump to start point.
6625 OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, sto_restart));
6626 return;
6629 /*==========================================================
6631 ** ncr chip exception handler for SCSI bus mode change
6633 **==========================================================
6635 ** spi2-r12 11.2.3 says a transceiver mode change must
6636 ** generate a reset event and a device that detects a reset
6637 ** event shall initiate a hard reset. It says also that a
6638 ** device that detects a mode change shall set data transfer
6639 ** mode to eight bit asynchronous, etc...
6640 ** So, just resetting should be enough.
6643 **----------------------------------------------------------
6646 static int ncr_int_sbmc (ncb_p np)
6648 u_char scsi_mode = INB (nc_stest4) & SMODE;
6650 if (scsi_mode != np->scsi_mode) {
6651 printk("%s: SCSI bus mode change from %x to %x.\n",
6652 ncr_name(np), np->scsi_mode, scsi_mode);
6654 np->scsi_mode = scsi_mode;
6658 ** Suspend command processing for 1 second and
6659 ** reinitialize all except the chip.
6661 np->settle_time = ktime_get(1*HZ);
6662 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
6663 return 1;
6665 return 0;
6668 /*==========================================================
6670 ** ncr chip exception handler for SCSI parity error.
6672 **==========================================================
6675 **----------------------------------------------------------
6678 static int ncr_int_par (ncb_p np)
6680 u_char hsts = INB (HS_PRT);
6681 u_int32 dbc = INL (nc_dbc);
6682 u_char sstat1 = INB (nc_sstat1);
6683 int phase = -1;
6684 int msg = -1;
6685 u_int32 jmp;
6687 printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
6688 ncr_name(np), hsts, dbc, sstat1);
6691 * Ignore the interrupt if the NCR is not connected
6692 * to the SCSI bus, since the right work should have
6693 * been done on unexpected disconnection handling.
6695 if (!(INB (nc_scntl1) & ISCON))
6696 return 0;
6699 * If the nexus is not clearly identified, reset the bus.
6700 * We will try to do better later.
6702 if (hsts & HS_INVALMASK)
6703 goto reset_all;
6706 * If the SCSI parity error occurs in MSG IN phase, prepare a
6707 * MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
6708 * ERROR message and let the device decide to retry the command
6709 * or to terminate with check condition. If we were in MSG IN
6710 * phase waiting for the response of a negotiation, we will
6711 * get SIR_NEGO_FAILED at dispatch.
6713 if (!(dbc & 0xc0000000))
6714 phase = (dbc >> 24) & 7;
6715 if (phase == 7)
6716 msg = M_PARITY;
6717 else
6718 msg = M_ID_ERROR;
6720 #ifdef SCSI_NCR_INTEGRITY_CHECKING
6722 ** Save error message. For integrity check use only.
6724 if (np->check_integrity)
6725 np->check_integ_par = msg;
6726 #endif
6729 * If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
6730 * script that will ignore all data in bytes until phase
6731 * change, since we are not sure the chip will wait the phase
6732 * change prior to delivering the interrupt.
6734 if (phase == 1)
6735 jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
6736 else
6737 jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
6739 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
6740 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
6742 np->msgout[0] = msg;
6743 OUTL (nc_dsp, jmp);
6744 return 1;
6746 reset_all:
6747 ncr_start_reset(np);
6748 return 1;
6751 /*==========================================================
6754 ** ncr chip exception handler for phase errors.
6757 **==========================================================
6759 ** We have to construct a new transfer descriptor,
6760 ** to transfer the rest of the current block.
6762 **----------------------------------------------------------
6765 static void ncr_int_ma (ncb_p np)
6767 u_int32 dbc;
6768 u_int32 rest;
6769 u_int32 dsp;
6770 u_int32 dsa;
6771 u_int32 nxtdsp;
6772 u_int32 newtmp;
6773 u_int32 *vdsp;
6774 u_int32 oadr, olen;
6775 u_int32 *tblp;
6776 ncrcmd *newcmd;
6777 u_char cmd, sbcl;
6778 ccb_p cp;
6780 dsp = INL (nc_dsp);
6781 dbc = INL (nc_dbc);
6782 sbcl = INB (nc_sbcl);
6784 cmd = dbc >> 24;
6785 rest = dbc & 0xffffff;
6788 ** Take into account dma fifo and various buffers and latches,
6789 ** only if the interrupted phase is an OUTPUT phase.
6792 if ((cmd & 1) == 0) {
6793 u_char ctest5, ss0, ss2;
6794 u_short delta;
6796 ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
6797 if (ctest5 & DFS)
6798 delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
6799 else
6800 delta=(INB (nc_dfifo) - rest) & 0x7f;
6803 ** The data in the dma fifo has not been transfered to
6804 ** the target -> add the amount to the rest
6805 ** and clear the data.
6806 ** Check the sstat2 register in case of wide transfer.
6809 rest += delta;
6810 ss0 = INB (nc_sstat0);
6811 if (ss0 & OLF) rest++;
6812 if (ss0 & ORF) rest++;
6813 if (INB(nc_scntl3) & EWS) {
6814 ss2 = INB (nc_sstat2);
6815 if (ss2 & OLF1) rest++;
6816 if (ss2 & ORF1) rest++;
6819 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6820 printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
6821 (unsigned) rest, (unsigned) delta, ss0);
6823 } else {
6824 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6825 printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
6829 ** Clear fifos.
6831 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
6832 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
6835 ** locate matching cp.
6836 ** if the interrupted phase is DATA IN or DATA OUT,
6837 ** trust the global header.
6839 dsa = INL (nc_dsa);
6840 if (!(cmd & 6)) {
6841 cp = np->header.cp;
6842 if (CCB_PHYS(cp, phys) != dsa)
6843 cp = 0;
6844 } else {
6845 cp = np->ccb;
6846 while (cp && (CCB_PHYS (cp, phys) != dsa))
6847 cp = cp->link_ccb;
6851 ** try to find the interrupted script command,
6852 ** and the address at which to continue.
6854 vdsp = 0;
6855 nxtdsp = 0;
6856 if (dsp > np->p_script &&
6857 dsp <= np->p_script + sizeof(struct script)) {
6858 vdsp = (u_int32 *)((char*)np->script0 + (dsp-np->p_script-8));
6859 nxtdsp = dsp;
6861 else if (dsp > np->p_scripth &&
6862 dsp <= np->p_scripth + sizeof(struct scripth)) {
6863 vdsp = (u_int32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
6864 nxtdsp = dsp;
6866 else if (cp) {
6867 if (dsp == CCB_PHYS (cp, patch[2])) {
6868 vdsp = &cp->patch[0];
6869 nxtdsp = scr_to_cpu(vdsp[3]);
6871 else if (dsp == CCB_PHYS (cp, patch[6])) {
6872 vdsp = &cp->patch[4];
6873 nxtdsp = scr_to_cpu(vdsp[3]);
6878 ** log the information
6881 if (DEBUG_FLAGS & DEBUG_PHASE) {
6882 printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
6883 cp, np->header.cp,
6884 (unsigned)dsp,
6885 (unsigned)nxtdsp, vdsp, cmd);
6889 ** cp=0 means that the DSA does not point to a valid control
6890 ** block. This should not happen since we donnot use multi-byte
6891 ** move while we are being reselected ot after command complete.
6892 ** We are not able to recover from such a phase error.
6894 if (!cp) {
6895 printk ("%s: SCSI phase error fixup: "
6896 "CCB already dequeued (0x%08lx)\n",
6897 ncr_name (np), (u_long) np->header.cp);
6898 goto reset_all;
6902 ** get old startaddress and old length.
6905 oadr = scr_to_cpu(vdsp[1]);
6907 if (cmd & 0x10) { /* Table indirect */
6908 tblp = (u_int32 *) ((char*) &cp->phys + oadr);
6909 olen = scr_to_cpu(tblp[0]);
6910 oadr = scr_to_cpu(tblp[1]);
6911 } else {
6912 tblp = (u_int32 *) 0;
6913 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
6916 if (DEBUG_FLAGS & DEBUG_PHASE) {
6917 printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
6918 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
6919 tblp,
6920 (unsigned) olen,
6921 (unsigned) oadr);
6925 ** check cmd against assumed interrupted script command.
6928 if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
6929 PRINT_ADDR(cp->cmd);
6930 printk ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
6931 (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
6933 goto reset_all;
6937 ** cp != np->header.cp means that the header of the CCB
6938 ** currently being processed has not yet been copied to
6939 ** the global header area. That may happen if the device did
6940 ** not accept all our messages after having been selected.
6942 if (cp != np->header.cp) {
6943 printk ("%s: SCSI phase error fixup: "
6944 "CCB address mismatch (0x%08lx != 0x%08lx)\n",
6945 ncr_name (np), (u_long) cp, (u_long) np->header.cp);
6949 ** if old phase not dataphase, leave here.
6952 if (cmd & 0x06) {
6953 PRINT_ADDR(cp->cmd);
6954 printk ("phase change %x-%x %d@%08x resid=%d.\n",
6955 cmd&7, sbcl&7, (unsigned)olen,
6956 (unsigned)oadr, (unsigned)rest);
6957 goto unexpected_phase;
6961 ** choose the correct patch area.
6962 ** if savep points to one, choose the other.
6965 newcmd = cp->patch;
6966 newtmp = CCB_PHYS (cp, patch);
6967 if (newtmp == scr_to_cpu(cp->phys.header.savep)) {
6968 newcmd = &cp->patch[4];
6969 newtmp = CCB_PHYS (cp, patch[4]);
6973 ** fillin the commands
6976 newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
6977 newcmd[1] = cpu_to_scr(oadr + olen - rest);
6978 newcmd[2] = cpu_to_scr(SCR_JUMP);
6979 newcmd[3] = cpu_to_scr(nxtdsp);
6981 if (DEBUG_FLAGS & DEBUG_PHASE) {
6982 PRINT_ADDR(cp->cmd);
6983 printk ("newcmd[%d] %x %x %x %x.\n",
6984 (int) (newcmd - cp->patch),
6985 (unsigned)scr_to_cpu(newcmd[0]),
6986 (unsigned)scr_to_cpu(newcmd[1]),
6987 (unsigned)scr_to_cpu(newcmd[2]),
6988 (unsigned)scr_to_cpu(newcmd[3]));
6991 ** fake the return address (to the patch).
6992 ** and restart script processor at dispatcher.
6994 #ifdef SCSI_NCR_PROFILE_SUPPORT
6995 np->profile.num_break++;
6996 #endif
6997 OUTL (nc_temp, newtmp);
6998 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
6999 return;
7002 ** Unexpected phase changes that occurs when the current phase
7003 ** is not a DATA IN or DATA OUT phase are due to error conditions.
7004 ** Such event may only happen when the SCRIPTS is using a
7005 ** multibyte SCSI MOVE.
7007 ** Phase change Some possible cause
7009 ** COMMAND --> MSG IN SCSI parity error detected by target.
7010 ** COMMAND --> STATUS Bad command or refused by target.
7011 ** MSG OUT --> MSG IN Message rejected by target.
7012 ** MSG OUT --> COMMAND Bogus target that discards extended
7013 ** negotiation messages.
7015 ** The code below does not care of the new phase and so
7016 ** trusts the target. Why to annoy it ?
7017 ** If the interrupted phase is COMMAND phase, we restart at
7018 ** dispatcher.
7019 ** If a target does not get all the messages after selection,
7020 ** the code assumes blindly that the target discards extended
7021 ** messages and clears the negotiation status.
7022 ** If the target does not want all our response to negotiation,
7023 ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
7024 ** bloat for such a should_not_happen situation).
7025 ** In all other situation, we reset the BUS.
7026 ** Are these assumptions reasonnable ? (Wait and see ...)
7028 unexpected_phase:
7029 dsp -= 8;
7030 nxtdsp = 0;
7032 switch (cmd & 7) {
7033 case 2: /* COMMAND phase */
7034 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
7035 break;
7036 #if 0
7037 case 3: /* STATUS phase */
7038 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
7039 break;
7040 #endif
7041 case 6: /* MSG OUT phase */
7042 np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
7043 if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
7044 cp->host_status = HS_BUSY;
7045 nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
7047 else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
7048 dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
7049 nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
7051 break;
7052 #if 0
7053 case 7: /* MSG IN phase */
7054 nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
7055 break;
7056 #endif
7059 if (nxtdsp) {
7060 OUTL (nc_dsp, nxtdsp);
7061 return;
7064 reset_all:
7065 ncr_start_reset(np);
7069 static void ncr_sir_to_redo(ncb_p np, int num, ccb_p cp)
7071 Scsi_Cmnd *cmd = cp->cmd;
7072 tcb_p tp = &np->target[cmd->target];
7073 lcb_p lp = tp->lp[cmd->lun];
7074 XPT_QUEHEAD *qp;
7075 ccb_p cp2;
7076 int disc_cnt = 0;
7077 int busy_cnt = 0;
7078 u_int32 startp;
7079 u_char s_status = INB (SS_PRT);
7082 ** Let the SCRIPTS processor skip all not yet started CCBs,
7083 ** and count disconnected CCBs. Since the busy queue is in
7084 ** the same order as the chip start queue, disconnected CCBs
7085 ** are before cp and busy ones after.
7087 if (lp) {
7088 qp = lp->busy_ccbq.blink;
7089 while (qp != &lp->busy_ccbq) {
7090 cp2 = xpt_que_entry(qp, struct ccb, link_ccbq);
7091 qp = qp->blink;
7092 ++busy_cnt;
7093 if (cp2 == cp)
7094 break;
7095 cp2->start.schedule.l_paddr =
7096 cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
7098 lp->held_ccb = cp; /* Requeue when this one completes */
7099 disc_cnt = lp->queuedccbs - busy_cnt;
7102 switch(s_status) {
7103 default: /* Just for safety, should never happen */
7104 case S_QUEUE_FULL:
7106 ** Decrease number of tags to the number of
7107 ** disconnected commands.
7109 if (!lp)
7110 goto out;
7111 if (bootverbose >= 1) {
7112 PRINT_ADDR(cmd);
7113 printk ("QUEUE FULL! %d busy, %d disconnected CCBs\n",
7114 busy_cnt, disc_cnt);
7116 if (disc_cnt < lp->numtags) {
7117 lp->numtags = disc_cnt > 2 ? disc_cnt : 2;
7118 lp->num_good = 0;
7119 ncr_setup_tags (np, cmd->target, cmd->lun);
7122 ** Requeue the command to the start queue.
7123 ** If any disconnected commands,
7124 ** Clear SIGP.
7125 ** Jump to reselect.
7127 cp->phys.header.savep = cp->startp;
7128 cp->host_status = HS_BUSY;
7129 cp->scsi_status = S_ILLEGAL;
7131 ncr_put_start_queue(np, cp);
7132 if (disc_cnt)
7133 INB (nc_ctest2); /* Clear SIGP */
7134 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
7135 return;
7136 case S_TERMINATED:
7137 case S_CHECK_COND:
7139 ** If we were requesting sense, give up.
7141 if (cp->auto_sense)
7142 goto out;
7145 ** Device returned CHECK CONDITION status.
7146 ** Prepare all needed data strutures for getting
7147 ** sense data.
7149 ** identify message
7151 cp->scsi_smsg2[0] = M_IDENTIFY | cmd->lun;
7152 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
7153 cp->phys.smsg.size = cpu_to_scr(1);
7156 ** sense command
7158 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
7159 cp->phys.cmd.size = cpu_to_scr(6);
7162 ** patch requested size into sense command
7164 cp->sensecmd[0] = 0x03;
7165 cp->sensecmd[1] = cmd->lun << 5;
7166 cp->sensecmd[4] = sizeof(cp->sense_buf);
7169 ** sense data
7171 bzero(cp->sense_buf, sizeof(cp->sense_buf));
7172 cp->phys.sense.addr = cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
7173 cp->phys.sense.size = cpu_to_scr(sizeof(cp->sense_buf));
7176 ** requeue the command.
7178 startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
7180 cp->phys.header.savep = startp;
7181 cp->phys.header.goalp = startp + 24;
7182 cp->phys.header.lastp = startp;
7183 cp->phys.header.wgoalp = startp + 24;
7184 cp->phys.header.wlastp = startp;
7186 cp->host_status = HS_BUSY;
7187 cp->scsi_status = S_ILLEGAL;
7188 cp->auto_sense = s_status;
7190 cp->start.schedule.l_paddr =
7191 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
7194 ** Select without ATN for quirky devices.
7196 if (tp->quirks & QUIRK_NOMSG)
7197 cp->start.schedule.l_paddr =
7198 cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
7200 ncr_put_start_queue(np, cp);
7202 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
7203 return;
7206 out:
7207 OUTONB (nc_dcntl, (STD|NOCOM));
7208 return;
7212 /*==========================================================
7215 ** ncr chip exception handler for programmed interrupts.
7218 **==========================================================
7221 static int ncr_show_msg (u_char * msg)
7223 u_char i;
7224 printk ("%x",*msg);
7225 if (*msg==M_EXTENDED) {
7226 for (i=1;i<8;i++) {
7227 if (i-1>msg[1]) break;
7228 printk ("-%x",msg[i]);
7230 return (i+1);
7231 } else if ((*msg & 0xf0) == 0x20) {
7232 printk ("-%x",msg[1]);
7233 return (2);
7235 return (1);
7238 static void ncr_print_msg ( ccb_p cp, char *label, u_char *msg)
7240 if (cp)
7241 PRINT_ADDR(cp->cmd);
7242 if (label)
7243 printk("%s: ", label);
7245 (void) ncr_show_msg (msg);
7246 printk(".\n");
7249 void ncr_int_sir (ncb_p np)
7251 u_char scntl3;
7252 u_char chg, ofs, per, fak, wide;
7253 u_char num = INB (nc_dsps);
7254 ccb_p cp=0;
7255 u_long dsa = INL (nc_dsa);
7256 u_char target = INB (nc_sdid) & 0x0f;
7257 tcb_p tp = &np->target[target];
7259 if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
7261 switch (num) {
7262 case SIR_RESEL_NO_MSG_IN:
7263 case SIR_RESEL_NO_IDENTIFY:
7265 ** If devices reselecting without sending an IDENTIFY
7266 ** message still exist, this should help.
7267 ** We just assume lun=0, 1 CCB, no tag.
7269 if (tp->lp[0]) {
7270 OUTL (nc_dsp, scr_to_cpu(tp->lp[0]->jump_ccb[0]));
7271 return;
7273 case SIR_RESEL_BAD_TARGET: /* Will send a TARGET RESET message */
7274 case SIR_RESEL_BAD_LUN: /* Will send a TARGET RESET message */
7275 case SIR_RESEL_BAD_I_T_L_Q: /* Will send an ABORT TAG message */
7276 case SIR_RESEL_BAD_I_T_L: /* Will send an ABORT message */
7277 printk ("%s:%d: SIR %d, "
7278 "incorrect nexus identification on reselection\n",
7279 ncr_name (np), target, num);
7280 goto out;
7281 case SIR_DONE_OVERFLOW:
7282 printk ("%s:%d: SIR %d, "
7283 "CCB done queue overflow\n",
7284 ncr_name (np), target, num);
7285 goto out;
7286 case SIR_BAD_STATUS:
7287 cp = np->header.cp;
7288 if (!cp || CCB_PHYS (cp, phys) != dsa)
7289 goto out;
7290 ncr_sir_to_redo(np, num, cp);
7291 return;
7292 default:
7294 ** lookup the ccb
7296 cp = np->ccb;
7297 while (cp && (CCB_PHYS (cp, phys) != dsa))
7298 cp = cp->link_ccb;
7300 assert (cp && cp == np->header.cp);
7302 if (!cp || cp != np->header.cp)
7303 goto out;
7306 switch (num) {
7307 /*-----------------------------------------------------------------------------
7309 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
7311 ** We try to negotiate sync and wide transfer only after
7312 ** a successfull inquire command. We look at byte 7 of the
7313 ** inquire data to determine the capabilities of the target.
7315 ** When we try to negotiate, we append the negotiation message
7316 ** to the identify and (maybe) simple tag message.
7317 ** The host status field is set to HS_NEGOTIATE to mark this
7318 ** situation.
7320 ** If the target doesn't answer this message immidiately
7321 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
7322 ** will be raised eventually.
7323 ** The handler removes the HS_NEGOTIATE status, and sets the
7324 ** negotiated value to the default (async / nowide).
7326 ** If we receive a matching answer immediately, we check it
7327 ** for validity, and set the values.
7329 ** If we receive a Reject message immediately, we assume the
7330 ** negotiation has failed, and fall back to standard values.
7332 ** If we receive a negotiation message while not in HS_NEGOTIATE
7333 ** state, it's a target initiated negotiation. We prepare a
7334 ** (hopefully) valid answer, set our parameters, and send back
7335 ** this answer to the target.
7337 ** If the target doesn't fetch the answer (no message out phase),
7338 ** we assume the negotiation has failed, and fall back to default
7339 ** settings.
7341 ** When we set the values, we adjust them in all ccbs belonging
7342 ** to this target, in the controller's register, and in the "phys"
7343 ** field of the controller's struct ncb.
7345 ** Possible cases: hs sir msg_in value send goto
7346 ** We try to negotiate:
7347 ** -> target doesnt't msgin NEG FAIL noop defa. - dispatch
7348 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
7349 ** -> target answered (ok) NEG SYNC sdtr set - clrack
7350 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
7351 ** -> target answered (ok) NEG WIDE wdtr set - clrack
7352 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
7353 ** -> any other msgin NEG FAIL noop defa. - dispatch
7355 ** Target tries to negotiate:
7356 ** -> incoming message --- SYNC sdtr set SDTR -
7357 ** -> incoming message --- WIDE wdtr set WDTR -
7358 ** We sent our answer:
7359 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
7361 **-----------------------------------------------------------------------------
7364 case SIR_NEGO_FAILED:
7365 /*-------------------------------------------------------
7367 ** Negotiation failed.
7368 ** Target doesn't send an answer message,
7369 ** or target rejected our message.
7371 ** Remove negotiation request.
7373 **-------------------------------------------------------
7375 OUTB (HS_PRT, HS_BUSY);
7377 /* fall through */
7379 case SIR_NEGO_PROTO:
7380 /*-------------------------------------------------------
7382 ** Negotiation failed.
7383 ** Target doesn't fetch the answer message.
7385 **-------------------------------------------------------
7388 if (DEBUG_FLAGS & DEBUG_NEGO) {
7389 PRINT_ADDR(cp->cmd);
7390 printk ("negotiation failed sir=%x status=%x.\n",
7391 num, cp->nego_status);
7395 ** any error in negotiation:
7396 ** fall back to default mode.
7398 switch (cp->nego_status) {
7400 case NS_SYNC:
7401 ncr_setsync (np, cp, 0, 0xe0);
7402 break;
7404 case NS_WIDE:
7405 ncr_setwide (np, cp, 0, 0);
7406 break;
7409 np->msgin [0] = M_NOOP;
7410 np->msgout[0] = M_NOOP;
7411 cp->nego_status = 0;
7412 break;
7414 case SIR_NEGO_SYNC:
7416 ** Synchronous request message received.
7419 if (DEBUG_FLAGS & DEBUG_NEGO) {
7420 PRINT_ADDR(cp->cmd);
7421 printk ("sync msgin: ");
7422 (void) ncr_show_msg (np->msgin);
7423 printk (".\n");
7427 ** get requested values.
7430 chg = 0;
7431 per = np->msgin[3];
7432 ofs = np->msgin[4];
7433 if (ofs==0) per=255;
7436 ** if target sends SDTR message,
7437 ** it CAN transfer synch.
7440 if (ofs)
7441 tp->inq_byte7 |= INQ7_SYNC;
7444 ** check values against driver limits.
7447 if (per < np->minsync)
7448 {chg = 1; per = np->minsync;}
7449 if (per < tp->minsync)
7450 {chg = 1; per = tp->minsync;}
7451 if (ofs > tp->maxoffs)
7452 {chg = 1; ofs = tp->maxoffs;}
7455 ** Check against controller limits.
7457 fak = 7;
7458 scntl3 = 0;
7459 if (ofs != 0) {
7460 ncr_getsync(np, per, &fak, &scntl3);
7461 if (fak > 7) {
7462 chg = 1;
7463 ofs = 0;
7466 if (ofs == 0) {
7467 fak = 7;
7468 per = 0;
7469 scntl3 = 0;
7470 tp->minsync = 0;
7473 if (DEBUG_FLAGS & DEBUG_NEGO) {
7474 PRINT_ADDR(cp->cmd);
7475 printk ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
7476 per, scntl3, ofs, fak, chg);
7479 if (INB (HS_PRT) == HS_NEGOTIATE) {
7480 OUTB (HS_PRT, HS_BUSY);
7481 switch (cp->nego_status) {
7483 case NS_SYNC:
7485 ** This was an answer message
7487 if (chg) {
7489 ** Answer wasn't acceptable.
7491 ncr_setsync (np, cp, 0, 0xe0);
7492 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
7493 } else {
7495 ** Answer is ok.
7497 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7498 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
7500 return;
7502 case NS_WIDE:
7503 ncr_setwide (np, cp, 0, 0);
7504 break;
7509 ** It was a request. Set value and
7510 ** prepare an answer message
7513 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7515 np->msgout[0] = M_EXTENDED;
7516 np->msgout[1] = 3;
7517 np->msgout[2] = M_X_SYNC_REQ;
7518 np->msgout[3] = per;
7519 np->msgout[4] = ofs;
7521 cp->nego_status = NS_SYNC;
7523 if (DEBUG_FLAGS & DEBUG_NEGO) {
7524 PRINT_ADDR(cp->cmd);
7525 printk ("sync msgout: ");
7526 (void) ncr_show_msg (np->msgout);
7527 printk (".\n");
7530 if (!ofs) {
7531 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
7532 return;
7534 np->msgin [0] = M_NOOP;
7536 break;
7538 case SIR_NEGO_WIDE:
7540 ** Wide request message received.
7542 if (DEBUG_FLAGS & DEBUG_NEGO) {
7543 PRINT_ADDR(cp->cmd);
7544 printk ("wide msgin: ");
7545 (void) ncr_show_msg (np->msgin);
7546 printk (".\n");
7550 ** get requested values.
7553 chg = 0;
7554 wide = np->msgin[3];
7557 ** if target sends WDTR message,
7558 ** it CAN transfer wide.
7561 if (wide)
7562 tp->inq_byte7 |= INQ7_WIDE16;
7565 ** check values against driver limits.
7568 if (wide > tp->usrwide)
7569 {chg = 1; wide = tp->usrwide;}
7571 if (DEBUG_FLAGS & DEBUG_NEGO) {
7572 PRINT_ADDR(cp->cmd);
7573 printk ("wide: wide=%d chg=%d.\n", wide, chg);
7576 if (INB (HS_PRT) == HS_NEGOTIATE) {
7577 OUTB (HS_PRT, HS_BUSY);
7578 switch (cp->nego_status) {
7580 case NS_WIDE:
7582 ** This was an answer message
7584 if (chg) {
7586 ** Answer wasn't acceptable.
7588 ncr_setwide (np, cp, 0, 1);
7589 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
7590 } else {
7592 ** Answer is ok.
7594 ncr_setwide (np, cp, wide, 1);
7595 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
7597 return;
7599 case NS_SYNC:
7600 ncr_setsync (np, cp, 0, 0xe0);
7601 break;
7606 ** It was a request, set value and
7607 ** prepare an answer message
7610 ncr_setwide (np, cp, wide, 1);
7612 np->msgout[0] = M_EXTENDED;
7613 np->msgout[1] = 2;
7614 np->msgout[2] = M_X_WIDE_REQ;
7615 np->msgout[3] = wide;
7617 np->msgin [0] = M_NOOP;
7619 cp->nego_status = NS_WIDE;
7621 if (DEBUG_FLAGS & DEBUG_NEGO) {
7622 PRINT_ADDR(cp->cmd);
7623 printk ("wide msgout: ");
7624 (void) ncr_show_msg (np->msgin);
7625 printk (".\n");
7627 break;
7629 /*--------------------------------------------------------------------
7631 ** Processing of special messages
7633 **--------------------------------------------------------------------
7636 case SIR_REJECT_RECEIVED:
7637 /*-----------------------------------------------
7639 ** We received a M_REJECT message.
7641 **-----------------------------------------------
7644 PRINT_ADDR(cp->cmd);
7645 printk ("M_REJECT received (%x:%x).\n",
7646 (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
7647 break;
7649 case SIR_REJECT_SENT:
7650 /*-----------------------------------------------
7652 ** We received an unknown message
7654 **-----------------------------------------------
7657 PRINT_ADDR(cp->cmd);
7658 printk ("M_REJECT sent for ");
7659 (void) ncr_show_msg (np->msgin);
7660 printk (".\n");
7661 break;
7663 /*--------------------------------------------------------------------
7665 ** Processing of special messages
7667 **--------------------------------------------------------------------
7670 case SIR_IGN_RESIDUE:
7671 /*-----------------------------------------------
7673 ** We received an IGNORE RESIDUE message,
7674 ** which couldn't be handled by the script.
7676 **-----------------------------------------------
7679 PRINT_ADDR(cp->cmd);
7680 printk ("M_IGN_RESIDUE received, but not yet implemented.\n");
7681 break;
7682 #if 0
7683 case SIR_MISSING_SAVE:
7684 /*-----------------------------------------------
7686 ** We received an DISCONNECT message,
7687 ** but the datapointer wasn't saved before.
7689 **-----------------------------------------------
7692 PRINT_ADDR(cp->cmd);
7693 printk ("M_DISCONNECT received, but datapointer not saved: "
7694 "data=%x save=%x goal=%x.\n",
7695 (unsigned) INL (nc_temp),
7696 (unsigned) scr_to_cpu(np->header.savep),
7697 (unsigned) scr_to_cpu(np->header.goalp));
7698 break;
7699 #endif
7702 out:
7703 OUTONB (nc_dcntl, (STD|NOCOM));
7706 /*==========================================================
7709 ** Aquire a control block
7712 **==========================================================
7715 static ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln)
7717 tcb_p tp = &np->target[tn];
7718 lcb_p lp = tp->lp[ln];
7719 u_char tag = NO_TAG;
7720 ccb_p cp = (ccb_p) 0;
7723 ** Lun structure available ?
7725 if (lp) {
7726 XPT_QUEHEAD *qp;
7728 ** Keep from using more tags than we can handle.
7730 if (lp->usetags && lp->busyccbs >= lp->maxnxs)
7731 return (ccb_p) 0;
7734 ** Allocate a new CCB if needed.
7736 if (xpt_que_empty(&lp->free_ccbq))
7737 ncr_alloc_ccb(np, tn, ln);
7740 ** Tune tag mode if asked by user.
7742 if (lp->queuedepth != lp->numtags) {
7743 ncr_setup_tags(np, tn, ln);
7747 ** Look for free CCB
7749 qp = xpt_remque_head(&lp->free_ccbq);
7750 if (qp) {
7751 cp = xpt_que_entry(qp, struct ccb, link_ccbq);
7752 if (cp->magic) {
7753 PRINT_LUN(np, tn, ln);
7754 printk ("ccb free list corrupted (@%p)\n", cp);
7755 cp = 0;
7757 else {
7758 xpt_insque_tail(qp, &lp->wait_ccbq);
7759 ++lp->busyccbs;
7764 ** If a CCB is available,
7765 ** Get a tag for this nexus if required.
7767 if (cp) {
7768 if (lp->usetags)
7769 tag = lp->cb_tags[lp->ia_tag];
7771 else if (lp->actccbs > 0)
7772 return (ccb_p) 0;
7776 ** if nothing available, take the default.
7778 if (!cp)
7779 cp = np->ccb;
7782 ** Wait until available.
7784 #if 0
7785 while (cp->magic) {
7786 if (flags & SCSI_NOSLEEP) break;
7787 if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
7788 break;
7790 #endif
7792 if (cp->magic)
7793 return ((ccb_p) 0);
7795 cp->magic = 1;
7798 ** Move to next available tag if tag used.
7800 if (lp) {
7801 if (tag != NO_TAG) {
7802 ++lp->ia_tag;
7803 if (lp->ia_tag == MAX_TAGS)
7804 lp->ia_tag = 0;
7805 lp->tags_umap |= (((tagmap_t) 1) << tag);
7810 ** Remember all informations needed to free this CCB.
7812 cp->tag = tag;
7813 cp->target = tn;
7814 cp->lun = ln;
7816 if (DEBUG_FLAGS & DEBUG_TAGS) {
7817 PRINT_LUN(np, tn, ln);
7818 printk ("ccb @%p using tag %d.\n", cp, tag);
7821 return cp;
7824 /*==========================================================
7827 ** Release one control block
7830 **==========================================================
7833 static void ncr_free_ccb (ncb_p np, ccb_p cp)
7835 tcb_p tp = &np->target[cp->target];
7836 lcb_p lp = tp->lp[cp->lun];
7838 if (DEBUG_FLAGS & DEBUG_TAGS) {
7839 PRINT_LUN(np, cp->target, cp->lun);
7840 printk ("ccb @%p freeing tag %d.\n", cp, cp->tag);
7844 ** If lun control block available,
7845 ** decrement active commands and increment credit,
7846 ** free the tag if any and remove the JUMP for reselect.
7848 if (lp) {
7849 if (cp->tag != NO_TAG) {
7850 lp->cb_tags[lp->if_tag++] = cp->tag;
7851 if (lp->if_tag == MAX_TAGS)
7852 lp->if_tag = 0;
7853 lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
7854 lp->tags_smap &= lp->tags_umap;
7855 lp->jump_ccb[cp->tag] =
7856 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
7857 } else {
7858 lp->jump_ccb[0] =
7859 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
7864 ** Make this CCB available.
7867 if (lp) {
7868 if (cp != np->ccb) {
7869 xpt_remque(&cp->link_ccbq);
7870 xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
7872 --lp->busyccbs;
7873 if (cp->queued) {
7874 --lp->queuedccbs;
7877 cp -> host_status = HS_IDLE;
7878 cp -> magic = 0;
7879 if (cp->queued) {
7880 --np->queuedccbs;
7881 cp->queued = 0;
7884 #if 0
7885 if (cp == np->ccb)
7886 wakeup ((caddr_t) cp);
7887 #endif
7891 #define ncr_reg_bus_addr(r) \
7892 (pcivtobus(np->paddr) + offsetof (struct ncr_reg, r))
7894 /*------------------------------------------------------------------------
7895 ** Initialize the fixed part of a CCB structure.
7896 **------------------------------------------------------------------------
7897 **------------------------------------------------------------------------
7899 static void ncr_init_ccb(ncb_p np, ccb_p cp)
7901 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7904 ** Remember virtual and bus address of this ccb.
7906 cp->p_ccb = vtobus(cp);
7907 cp->phys.header.cp = cp;
7910 ** This allows xpt_remque to work for the default ccb.
7912 xpt_que_init(&cp->link_ccbq);
7915 ** Initialyze the start and restart launch script.
7917 ** COPY(4) @(...p_phys), @(dsa)
7918 ** JUMP @(sched_point)
7920 cp->start.setup_dsa[0] = cpu_to_scr(copy_4);
7921 cp->start.setup_dsa[1] = cpu_to_scr(CCB_PHYS(cp, start.p_phys));
7922 cp->start.setup_dsa[2] = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
7923 cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
7924 cp->start.p_phys = cpu_to_scr(CCB_PHYS(cp, phys));
7926 bcopy(&cp->start, &cp->restart, sizeof(cp->restart));
7928 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
7929 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
7933 /*------------------------------------------------------------------------
7934 ** Allocate a CCB and initialize its fixed part.
7935 **------------------------------------------------------------------------
7936 **------------------------------------------------------------------------
7938 static void ncr_alloc_ccb(ncb_p np, u_char tn, u_char ln)
7940 tcb_p tp = &np->target[tn];
7941 lcb_p lp = tp->lp[ln];
7942 ccb_p cp = 0;
7945 ** Allocate memory for this CCB.
7947 cp = m_calloc_dma(sizeof(struct ccb), "CCB");
7948 if (!cp)
7949 return;
7952 ** Count it and initialyze it.
7954 lp->actccbs++;
7955 np->actccbs++;
7956 bzero (cp, sizeof (*cp));
7957 ncr_init_ccb(np, cp);
7960 ** Chain into wakeup list and free ccb queue and take it
7961 ** into account for tagged commands.
7963 cp->link_ccb = np->ccb->link_ccb;
7964 np->ccb->link_ccb = cp;
7966 xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
7967 ncr_setup_tags (np, tn, ln);
7970 /*==========================================================
7973 ** Allocation of resources for Targets/Luns/Tags.
7976 **==========================================================
7980 /*------------------------------------------------------------------------
7981 ** Target control block initialisation.
7982 **------------------------------------------------------------------------
7983 ** This data structure is fully initialized after a SCSI command
7984 ** has been successfully completed for this target.
7985 ** It contains a SCRIPT that is called on target reselection.
7986 **------------------------------------------------------------------------
7988 static void ncr_init_tcb (ncb_p np, u_char tn)
7990 tcb_p tp = &np->target[tn];
7991 ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
7992 int th = tn & 3;
7993 int i;
7996 ** Jump to next tcb if SFBR does not match this target.
7997 ** JUMP IF (SFBR != #target#), @(next tcb)
7999 tp->jump_tcb.l_cmd =
8000 cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
8001 tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
8004 ** Load the synchronous transfer register.
8005 ** COPY @(tp->sval), @(sxfer)
8007 tp->getscr[0] = cpu_to_scr(copy_1);
8008 tp->getscr[1] = cpu_to_scr(vtobus (&tp->sval));
8009 tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
8012 ** Load the timing register.
8013 ** COPY @(tp->wval), @(scntl3)
8015 tp->getscr[3] = cpu_to_scr(copy_1);
8016 tp->getscr[4] = cpu_to_scr(vtobus (&tp->wval));
8017 tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
8020 ** Get the IDENTIFY message and the lun.
8021 ** CALL @script(resel_lun)
8023 tp->call_lun.l_cmd = cpu_to_scr(SCR_CALL);
8024 tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
8027 ** Look for the lun control block of this nexus.
8028 ** For i = 0 to 3
8029 ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
8031 for (i = 0 ; i < 4 ; i++) {
8032 tp->jump_lcb[i].l_cmd =
8033 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
8034 tp->jump_lcb[i].l_paddr =
8035 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
8039 ** Link this target control block to the JUMP chain.
8041 np->jump_tcb[th].l_paddr = cpu_to_scr(vtobus (&tp->jump_tcb));
8044 ** These assert's should be moved at driver initialisations.
8046 assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
8047 offsetof(struct tcb , sval )) &3) == 0);
8048 assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
8049 offsetof(struct tcb , wval )) &3) == 0);
8053 /*------------------------------------------------------------------------
8054 ** Lun control block allocation and initialization.
8055 **------------------------------------------------------------------------
8056 ** This data structure is allocated and initialized after a SCSI
8057 ** command has been successfully completed for this target/lun.
8058 **------------------------------------------------------------------------
8060 static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln)
8062 tcb_p tp = &np->target[tn];
8063 lcb_p lp = tp->lp[ln];
8064 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
8065 int lh = ln & 3;
8068 ** Already done, return.
8070 if (lp)
8071 return lp;
8074 ** Allocate the lcb.
8076 lp = m_calloc_dma(sizeof(struct lcb), "LCB");
8077 if (!lp)
8078 goto fail;
8079 bzero(lp, sizeof(*lp));
8080 tp->lp[ln] = lp;
8083 ** Initialize the target control block if not yet.
8085 if (!tp->jump_tcb.l_cmd)
8086 ncr_init_tcb(np, tn);
8089 ** Initialize the CCB queue headers.
8091 xpt_que_init(&lp->free_ccbq);
8092 xpt_que_init(&lp->busy_ccbq);
8093 xpt_que_init(&lp->wait_ccbq);
8094 xpt_que_init(&lp->skip_ccbq);
8097 ** Set max CCBs to 1 and use the default 1 entry
8098 ** jump table by default.
8100 lp->maxnxs = 1;
8101 lp->jump_ccb = &lp->jump_ccb_0;
8102 lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
8105 ** Initilialyze the reselect script:
8107 ** Jump to next lcb if SFBR does not match this lun.
8108 ** Load TEMP with the CCB direct jump table bus address.
8109 ** Get the SIMPLE TAG message and the tag.
8111 ** JUMP IF (SFBR != #lun#), @(next lcb)
8112 ** COPY @(lp->p_jump_ccb), @(temp)
8113 ** JUMP @script(resel_notag)
8115 lp->jump_lcb.l_cmd =
8116 cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
8117 lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
8119 lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
8120 lp->load_jump_ccb[1] = cpu_to_scr(vtobus (&lp->p_jump_ccb));
8121 lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
8123 lp->jump_tag.l_cmd = cpu_to_scr(SCR_JUMP);
8124 lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
8127 ** Link this lun control block to the JUMP chain.
8129 tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtobus (&lp->jump_lcb));
8132 ** Initialize command queuing control.
8134 lp->busyccbs = 1;
8135 lp->queuedccbs = 1;
8136 lp->queuedepth = 1;
8137 fail:
8138 return lp;
8142 /*------------------------------------------------------------------------
8143 ** Lun control block setup on INQUIRY data received.
8144 **------------------------------------------------------------------------
8145 ** We only support WIDE, SYNC for targets and CMDQ for logical units.
8146 ** This setup is done on each INQUIRY since we are expecting user
8147 ** will play with CHANGE DEFINITION commands. :-)
8148 **------------------------------------------------------------------------
8150 static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln, u_char *inq_data)
8152 tcb_p tp = &np->target[tn];
8153 lcb_p lp = tp->lp[ln];
8154 u_char inq_byte7;
8157 ** If no lcb, try to allocate it.
8159 if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
8160 goto fail;
8163 ** Get device quirks from a speciality table.
8165 tp->quirks = ncr_lookup (inq_data);
8166 if (tp->quirks && bootverbose) {
8167 PRINT_LUN(np, tn, ln);
8168 printk ("quirks=%x.\n", tp->quirks);
8172 ** Evaluate trustable target/unit capabilities.
8173 ** We only believe device version >= SCSI-2 that
8174 ** use appropriate response data format (2).
8175 ** But it seems that some CCS devices also
8176 ** support SYNC and I donnot want to frustrate
8177 ** anybody. ;-)
8179 inq_byte7 = 0;
8180 if ((inq_data[2] & 0x7) >= 2 && (inq_data[3] & 0xf) == 2)
8181 inq_byte7 = inq_data[7];
8182 else if ((inq_data[2] & 0x7) == 1 && (inq_data[3] & 0xf) == 1)
8183 inq_byte7 = INQ7_SYNC;
8186 ** Throw away announced LUN capabilities if we are told
8187 ** that there is no real device supported by the logical unit.
8189 if ((inq_data[0] & 0xe0) > 0x20 || (inq_data[0] & 0x1f) == 0x1f)
8190 inq_byte7 &= (INQ7_SYNC | INQ7_WIDE16);
8193 ** If user is wanting SYNC, force this feature.
8195 if (driver_setup.force_sync_nego)
8196 inq_byte7 |= INQ7_SYNC;
8199 ** Prepare negotiation if SIP capabilities have changed.
8201 tp->inq_done = 1;
8202 if ((inq_byte7 ^ tp->inq_byte7) & (INQ7_SYNC | INQ7_WIDE16)) {
8203 tp->inq_byte7 = inq_byte7;
8204 ncr_negotiate(np, tp);
8208 ** If unit supports tagged commands, allocate the
8209 ** CCB JUMP table if not yet.
8211 if ((inq_byte7 & INQ7_QUEUE) && lp->jump_ccb == &lp->jump_ccb_0) {
8212 int i;
8213 lp->jump_ccb = m_calloc_dma(256, "JUMP_CCB");
8214 if (!lp->jump_ccb) {
8215 lp->jump_ccb = &lp->jump_ccb_0;
8216 goto fail;
8218 lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
8219 for (i = 0 ; i < 64 ; i++)
8220 lp->jump_ccb[i] =
8221 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
8222 for (i = 0 ; i < MAX_TAGS ; i++)
8223 lp->cb_tags[i] = i;
8224 lp->maxnxs = MAX_TAGS;
8225 lp->tags_stime = ktime_get(3*HZ);
8229 ** Adjust tagged queueing status if needed.
8231 if ((inq_byte7 ^ lp->inq_byte7) & INQ7_QUEUE) {
8232 lp->inq_byte7 = inq_byte7;
8233 lp->numtags = lp->maxtags;
8234 ncr_setup_tags (np, tn, ln);
8237 fail:
8238 return lp;
8241 /*==========================================================
8244 ** Build Scatter Gather Block
8247 **==========================================================
8249 ** The transfer area may be scattered among
8250 ** several non adjacent physical pages.
8252 ** We may use MAX_SCATTER blocks.
8254 **----------------------------------------------------------
8258 ** We try to reduce the number of interrupts caused
8259 ** by unexpected phase changes due to disconnects.
8260 ** A typical harddisk may disconnect before ANY block.
8261 ** If we wanted to avoid unexpected phase changes at all
8262 ** we had to use a break point every 512 bytes.
8263 ** Of course the number of scatter/gather blocks is
8264 ** limited.
8265 ** Under Linux, the scatter/gatter blocks are provided by
8266 ** the generic driver. We just have to copy addresses and
8267 ** sizes to the data segment array.
8270 static int ncr_scatter(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd)
8272 struct scr_tblmove *data;
8273 int segment = 0;
8274 int use_sg = (int) cmd->use_sg;
8276 data = cp->phys.data;
8277 cp->data_len = 0;
8279 if (!use_sg) {
8280 if (cmd->request_bufflen) {
8281 u_long baddr = map_scsi_single_data(np, cmd);
8283 data = &data[MAX_SCATTER - 1];
8284 data[0].addr = cpu_to_scr(baddr);
8285 data[0].size = cpu_to_scr(cmd->request_bufflen);
8286 cp->data_len = cmd->request_bufflen;
8287 segment = 1;
8290 else if (use_sg <= MAX_SCATTER) {
8291 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
8293 use_sg = map_scsi_sg_data(np, cmd);
8294 data = &data[MAX_SCATTER - use_sg];
8296 while (segment < use_sg) {
8297 u_long baddr = scsi_sg_dma_address(&scatter[segment]);
8298 unsigned int len = scsi_sg_dma_len(&scatter[segment]);
8300 data[segment].addr = cpu_to_scr(baddr);
8301 data[segment].size = cpu_to_scr(len);
8302 cp->data_len += len;
8303 ++segment;
8306 else {
8307 return -1;
8310 return segment;
8313 /*==========================================================
8316 ** Test the pci bus snoop logic :-(
8318 ** Has to be called with interrupts disabled.
8321 **==========================================================
8324 #ifndef NCR_IOMAPPED
8325 static int __init ncr_regtest (struct ncb* np)
8327 register volatile u_int32 data;
8329 ** ncr registers may NOT be cached.
8330 ** write 0xffffffff to a read only register area,
8331 ** and try to read it back.
8333 data = 0xffffffff;
8334 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
8335 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
8336 #if 1
8337 if (data == 0xffffffff) {
8338 #else
8339 if ((data & 0xe2f0fffd) != 0x02000080) {
8340 #endif
8341 printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
8342 (unsigned) data);
8343 return (0x10);
8345 return (0);
8347 #endif
8349 static int __init ncr_snooptest (struct ncb* np)
8351 u_int32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
8352 int i, err=0;
8353 #ifndef NCR_IOMAPPED
8354 if (np->reg) {
8355 err |= ncr_regtest (np);
8356 if (err) return (err);
8358 #endif
8360 ** init
8362 pc = NCB_SCRIPTH_PHYS (np, snooptest);
8363 host_wr = 1;
8364 ncr_wr = 2;
8366 ** Set memory and register.
8368 np->ncr_cache = cpu_to_scr(host_wr);
8369 OUTL (nc_temp, ncr_wr);
8371 ** Start script (exchange values)
8373 OUTL (nc_dsp, pc);
8375 ** Wait 'til done (with timeout)
8377 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
8378 if (INB(nc_istat) & (INTF|SIP|DIP))
8379 break;
8381 ** Save termination position.
8383 pc = INL (nc_dsp);
8385 ** Read memory and register.
8387 host_rd = scr_to_cpu(np->ncr_cache);
8388 ncr_rd = INL (nc_scratcha);
8389 ncr_bk = INL (nc_temp);
8391 ** Reset ncr chip
8393 OUTB (nc_istat, SRST);
8394 UDELAY (100);
8395 OUTB (nc_istat, 0 );
8397 ** check for timeout
8399 if (i>=NCR_SNOOP_TIMEOUT) {
8400 printk ("CACHE TEST FAILED: timeout.\n");
8401 return (0x20);
8404 ** Check termination position.
8406 if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
8407 printk ("CACHE TEST FAILED: script execution failed.\n");
8408 printk ("start=%08lx, pc=%08lx, end=%08lx\n",
8409 (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
8410 (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
8411 return (0x40);
8414 ** Show results.
8416 if (host_wr != ncr_rd) {
8417 printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
8418 (int) host_wr, (int) ncr_rd);
8419 err |= 1;
8421 if (host_rd != ncr_wr) {
8422 printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
8423 (int) ncr_wr, (int) host_rd);
8424 err |= 2;
8426 if (ncr_bk != ncr_wr) {
8427 printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
8428 (int) ncr_wr, (int) ncr_bk);
8429 err |= 4;
8431 return (err);
8434 /*==========================================================
8437 ** Profiling the drivers and targets performance.
8440 **==========================================================
8443 #ifdef SCSI_NCR_PROFILE_SUPPORT
8446 ** Compute the difference in jiffies ticks.
8449 #define ncr_delta(from, to) \
8450 ( ((to) && (from))? (to) - (from) : -1 )
8452 #define PROFILE cp->phys.header.stamp
8453 static void ncb_profile (ncb_p np, ccb_p cp)
8455 long co, st, en, di, re, post, work, disc;
8456 u_int diff;
8458 PROFILE.end = jiffies;
8460 st = ncr_delta (PROFILE.start,PROFILE.status);
8461 if (st<0) return; /* status not reached */
8463 co = ncr_delta (PROFILE.start,PROFILE.command);
8464 if (co<0) return; /* command not executed */
8466 en = ncr_delta (PROFILE.start,PROFILE.end),
8467 di = ncr_delta (PROFILE.start,PROFILE.disconnect),
8468 re = ncr_delta (PROFILE.start,PROFILE.reselect);
8469 post = en - st;
8472 ** @PROFILE@ Disconnect time invalid if multiple disconnects
8475 if (di>=0) disc = re - di; else disc = 0;
8477 work = (st - co) - disc;
8479 diff = (scr_to_cpu(np->disc_phys) - np->disc_ref) & 0xff;
8480 np->disc_ref += diff;
8482 np->profile.num_trans += 1;
8483 if (cp->cmd) {
8484 np->profile.num_kbytes += (cp->cmd->request_bufflen >> 10);
8485 np->profile.rest_bytes += (cp->cmd->request_bufflen & (0x400-1));
8486 if (np->profile.rest_bytes >= 0x400) {
8487 ++np->profile.num_kbytes;
8488 np->profile.rest_bytes -= 0x400;
8491 np->profile.num_disc += diff;
8492 np->profile.ms_setup += co;
8493 np->profile.ms_data += work;
8494 np->profile.ms_disc += disc;
8495 np->profile.ms_post += post;
8497 #undef PROFILE
8499 #endif /* SCSI_NCR_PROFILE_SUPPORT */
8501 /*==========================================================
8504 ** Device lookup.
8506 ** @GENSCSI@ should be integrated to scsiconf.c
8509 **==========================================================
8512 struct table_entry {
8513 char * manufacturer;
8514 char * model;
8515 char * version;
8516 u_long info;
8519 static struct table_entry device_tab[] =
8521 #if 0
8522 {"", "", "", QUIRK_NOMSG},
8523 #endif
8524 {"SONY", "SDT-5000", "3.17", QUIRK_NOMSG},
8525 {"WangDAT", "Model 2600", "01.7", QUIRK_NOMSG},
8526 {"WangDAT", "Model 3200", "02.2", QUIRK_NOMSG},
8527 {"WangDAT", "Model 1300", "02.4", QUIRK_NOMSG},
8528 {"", "", "", 0} /* catch all: must be last entry. */
8531 static u_long ncr_lookup(char * id)
8533 struct table_entry * p = device_tab;
8534 char *d, *r, c;
8536 for (;;p++) {
8538 d = id+8;
8539 r = p->manufacturer;
8540 while ((c=*r++)) if (c!=*d++) break;
8541 if (c) continue;
8543 d = id+16;
8544 r = p->model;
8545 while ((c=*r++)) if (c!=*d++) break;
8546 if (c) continue;
8548 d = id+32;
8549 r = p->version;
8550 while ((c=*r++)) if (c!=*d++) break;
8551 if (c) continue;
8553 return (p->info);
8557 /*==========================================================
8559 ** Determine the ncr's clock frequency.
8560 ** This is essential for the negotiation
8561 ** of the synchronous transfer rate.
8563 **==========================================================
8565 ** Note: we have to return the correct value.
8566 ** THERE IS NO SAVE DEFAULT VALUE.
8568 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
8569 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
8570 ** do not have a clock doubler and so are provided with a
8571 ** 80 MHz clock. All other fast20 boards incorporate a doubler
8572 ** and so should be delivered with a 40 MHz clock.
8573 ** The future fast40 chips (895/895) use a 40 Mhz base clock
8574 ** and provide a clock quadrupler (160 Mhz). The code below
8575 ** tries to deal as cleverly as possible with all this stuff.
8577 **----------------------------------------------------------
8581 * Select NCR SCSI clock frequency
8583 static void ncr_selectclock(ncb_p np, u_char scntl3)
8585 if (np->multiplier < 2) {
8586 OUTB(nc_scntl3, scntl3);
8587 return;
8590 if (bootverbose >= 2)
8591 printk ("%s: enabling clock multiplier\n", ncr_name(np));
8593 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
8594 if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
8595 int i = 20;
8596 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
8597 UDELAY (20);
8598 if (!i)
8599 printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
8600 } else /* Wait 20 micro-seconds for doubler */
8601 UDELAY (20);
8602 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
8603 OUTB(nc_scntl3, scntl3);
8604 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
8605 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
8610 * calculate NCR SCSI clock frequency (in KHz)
8612 static unsigned __init ncrgetfreq (ncb_p np, int gen)
8614 unsigned ms = 0;
8615 char count = 0;
8618 * Measure GEN timer delay in order
8619 * to calculate SCSI clock frequency
8621 * This code will never execute too
8622 * many loop iterations (if DELAY is
8623 * reasonably correct). It could get
8624 * too low a delay (too high a freq.)
8625 * if the CPU is slow executing the
8626 * loop for some reason (an NMI, for
8627 * example). For this reason we will
8628 * if multiple measurements are to be
8629 * performed trust the higher delay
8630 * (lower frequency returned).
8632 OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
8633 OUTW (nc_sien , 0); /* mask all scsi interrupts */
8634 (void) INW (nc_sist); /* clear pending scsi interrupt */
8635 OUTB (nc_dien , 0); /* mask all dma interrupts */
8636 (void) INW (nc_sist); /* another one, just to be sure :) */
8637 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
8638 OUTB (nc_stime1, 0); /* disable general purpose timer */
8639 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
8640 while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
8641 for (count = 0; count < 10; count ++)
8642 UDELAY (100); /* count ms */
8644 OUTB (nc_stime1, 0); /* disable general purpose timer */
8646 * set prescaler to divide by whatever 0 means
8647 * 0 ought to choose divide by 2, but appears
8648 * to set divide by 3.5 mode in my 53c810 ...
8650 OUTB (nc_scntl3, 0);
8652 if (bootverbose >= 2)
8653 printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
8655 * adjust for prescaler, and convert into KHz
8657 return ms ? ((1 << gen) * 4340) / ms : 0;
8661 * Get/probe NCR SCSI clock frequency
8663 static void __init ncr_getclock (ncb_p np, int mult)
8665 unsigned char scntl3 = INB(nc_scntl3);
8666 unsigned char stest1 = INB(nc_stest1);
8667 unsigned f1;
8669 np->multiplier = 1;
8670 f1 = 40000;
8673 ** True with 875 or 895 with clock multiplier selected
8675 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
8676 if (bootverbose >= 2)
8677 printk ("%s: clock multiplier found\n", ncr_name(np));
8678 np->multiplier = mult;
8682 ** If multiplier not found or scntl3 not 7,5,3,
8683 ** reset chip and get frequency from general purpose timer.
8684 ** Otherwise trust scntl3 BIOS setting.
8686 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
8687 unsigned f2;
8689 OUTB(nc_istat, SRST); UDELAY (5); OUTB(nc_istat, 0);
8691 (void) ncrgetfreq (np, 11); /* throw away first result */
8692 f1 = ncrgetfreq (np, 11);
8693 f2 = ncrgetfreq (np, 11);
8695 if (bootverbose)
8696 printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
8698 if (f1 > f2) f1 = f2; /* trust lower result */
8700 if (f1 < 45000) f1 = 40000;
8701 else if (f1 < 55000) f1 = 50000;
8702 else f1 = 80000;
8704 if (f1 < 80000 && mult > 1) {
8705 if (bootverbose >= 2)
8706 printk ("%s: clock multiplier assumed\n", ncr_name(np));
8707 np->multiplier = mult;
8709 } else {
8710 if ((scntl3 & 7) == 3) f1 = 40000;
8711 else if ((scntl3 & 7) == 5) f1 = 80000;
8712 else f1 = 160000;
8714 f1 /= np->multiplier;
8718 ** Compute controller synchronous parameters.
8720 f1 *= np->multiplier;
8721 np->clock_khz = f1;
8724 /*===================== LINUX ENTRY POINTS SECTION ==========================*/
8727 ** Linux select queue depths function
8730 static void ncr53c8xx_select_queue_depths(struct Scsi_Host *host, struct scsi_device *devlist)
8732 struct scsi_device *device;
8734 for (device = devlist; device; device = device->next) {
8735 ncb_p np;
8736 tcb_p tp;
8737 lcb_p lp;
8738 int numtags;
8740 if (device->host != host)
8741 continue;
8743 np = ((struct host_data *) host->hostdata)->ncb;
8744 tp = &np->target[device->id];
8745 lp = tp->lp[device->lun];
8748 ** Select queue depth from driver setup.
8749 ** Donnot use more than configured by user.
8750 ** Use at least 2.
8751 ** Donnot use more than our maximum.
8753 numtags = device_queue_depth(np->unit, device->id, device->lun);
8754 if (numtags > tp->usrtags)
8755 numtags = tp->usrtags;
8756 if (!device->tagged_supported)
8757 numtags = 1;
8758 device->queue_depth = numtags;
8759 if (device->queue_depth < 2)
8760 device->queue_depth = 2;
8761 if (device->queue_depth > MAX_TAGS)
8762 device->queue_depth = MAX_TAGS;
8765 ** Since the queue depth is not tunable under Linux,
8766 ** we need to know this value in order not to
8767 ** announce stupid things to user.
8769 if (lp) {
8770 lp->numtags = lp->maxtags = numtags;
8771 lp->scdev_depth = device->queue_depth;
8773 ncr_setup_tags (np, device->id, device->lun);
8775 #ifdef DEBUG_NCR53C8XX
8776 printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
8777 np->unit, device->id, device->lun, device->queue_depth);
8778 #endif
8783 ** Linux entry point of queuecommand() function
8786 int ncr53c8xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
8788 ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
8789 unsigned long flags;
8790 int sts;
8792 #ifdef DEBUG_NCR53C8XX
8793 printk("ncr53c8xx_queue_command\n");
8794 #endif
8796 cmd->scsi_done = done;
8797 cmd->host_scribble = NULL;
8798 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
8799 cmd->__data_mapped = 0;
8800 cmd->__data_mapping = 0;
8801 #endif
8803 NCR_LOCK_NCB(np, flags);
8805 if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
8806 cmd->result = ScsiResult(sts, 0);
8807 #ifdef DEBUG_NCR53C8XX
8808 printk("ncr53c8xx : command not queued - result=%d\n", sts);
8809 #endif
8811 #ifdef DEBUG_NCR53C8XX
8812 else
8813 printk("ncr53c8xx : command successfully queued\n");
8814 #endif
8816 NCR_UNLOCK_NCB(np, flags);
8818 if (sts != DID_OK) {
8819 unmap_scsi_data(np, cmd);
8820 done(cmd);
8823 return sts;
8827 ** Linux entry point of the interrupt handler.
8828 ** Since linux versions > 1.3.70, we trust the kernel for
8829 ** passing the internal host descriptor as 'dev_id'.
8830 ** Otherwise, we scan the host list and call the interrupt
8831 ** routine for each host that uses this IRQ.
8834 static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
8836 unsigned long flags;
8837 ncb_p np = (ncb_p) dev_id;
8838 Scsi_Cmnd *done_list;
8840 #ifdef DEBUG_NCR53C8XX
8841 printk("ncr53c8xx : interrupt received\n");
8842 #endif
8844 if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
8846 NCR_LOCK_NCB(np, flags);
8847 ncr_exception(np);
8848 done_list = np->done_list;
8849 np->done_list = 0;
8850 NCR_UNLOCK_NCB(np, flags);
8852 if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
8854 if (done_list) {
8855 NCR_LOCK_SCSI_DONE(np, flags);
8856 ncr_flush_done_cmds(done_list);
8857 NCR_UNLOCK_SCSI_DONE(np, flags);
8862 ** Linux entry point of the timer handler
8865 static void ncr53c8xx_timeout(unsigned long npref)
8867 ncb_p np = (ncb_p) npref;
8868 unsigned long flags;
8869 Scsi_Cmnd *done_list;
8871 NCR_LOCK_NCB(np, flags);
8872 ncr_timeout((ncb_p) np);
8873 done_list = np->done_list;
8874 np->done_list = 0;
8875 NCR_UNLOCK_NCB(np, flags);
8877 if (done_list) {
8878 NCR_LOCK_SCSI_DONE(np, flags);
8879 ncr_flush_done_cmds(done_list);
8880 NCR_UNLOCK_SCSI_DONE(np, flags);
8885 ** Linux entry point of reset() function
8888 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8889 int ncr53c8xx_reset(Scsi_Cmnd *cmd, unsigned int reset_flags)
8890 #else
8891 int ncr53c8xx_reset(Scsi_Cmnd *cmd)
8892 #endif
8894 ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
8895 int sts;
8896 unsigned long flags;
8897 Scsi_Cmnd *done_list;
8899 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8900 printk("ncr53c8xx_reset: pid=%lu reset_flags=%x serial_number=%ld serial_number_at_timeout=%ld\n",
8901 cmd->pid, reset_flags, cmd->serial_number, cmd->serial_number_at_timeout);
8902 #else
8903 printk("ncr53c8xx_reset: command pid %lu\n", cmd->pid);
8904 #endif
8906 NCR_LOCK_NCB(np, flags);
8909 * We have to just ignore reset requests in some situations.
8911 #if defined SCSI_RESET_NOT_RUNNING
8912 if (cmd->serial_number != cmd->serial_number_at_timeout) {
8913 sts = SCSI_RESET_NOT_RUNNING;
8914 goto out;
8916 #endif
8918 * If the mid-level driver told us reset is synchronous, it seems
8919 * that we must call the done() callback for the involved command,
8920 * even if this command was not queued to the low-level driver,
8921 * before returning SCSI_RESET_SUCCESS.
8924 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8925 sts = ncr_reset_bus(np, cmd,
8926 (reset_flags & (SCSI_RESET_SYNCHRONOUS | SCSI_RESET_ASYNCHRONOUS)) == SCSI_RESET_SYNCHRONOUS);
8927 #else
8928 sts = ncr_reset_bus(np, cmd, 0);
8929 #endif
8932 * Since we always reset the controller, when we return success,
8933 * we add this information to the return code.
8935 #if defined SCSI_RESET_HOST_RESET
8936 if (sts == SCSI_RESET_SUCCESS)
8937 sts |= SCSI_RESET_HOST_RESET;
8938 #endif
8940 out:
8941 done_list = np->done_list;
8942 np->done_list = 0;
8943 NCR_UNLOCK_NCB(np, flags);
8945 ncr_flush_done_cmds(done_list);
8947 return sts;
8951 ** Linux entry point of abort() function
8954 int ncr53c8xx_abort(Scsi_Cmnd *cmd)
8956 ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
8957 int sts;
8958 unsigned long flags;
8959 Scsi_Cmnd *done_list;
8961 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8962 printk("ncr53c8xx_abort: pid=%lu serial_number=%ld serial_number_at_timeout=%ld\n",
8963 cmd->pid, cmd->serial_number, cmd->serial_number_at_timeout);
8964 #else
8965 printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
8966 #endif
8968 NCR_LOCK_NCB(np, flags);
8970 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8972 * We have to just ignore abort requests in some situations.
8974 if (cmd->serial_number != cmd->serial_number_at_timeout) {
8975 sts = SCSI_ABORT_NOT_RUNNING;
8976 goto out;
8978 #endif
8980 sts = ncr_abort_command(np, cmd);
8981 out:
8982 done_list = np->done_list;
8983 np->done_list = 0;
8984 NCR_UNLOCK_NCB(np, flags);
8986 ncr_flush_done_cmds(done_list);
8988 return sts;
8992 #ifdef MODULE
8993 int ncr53c8xx_release(struct Scsi_Host *host)
8995 #ifdef DEBUG_NCR53C8XX
8996 printk("ncr53c8xx : release\n");
8997 #endif
8998 ncr_detach(((struct host_data *) host->hostdata)->ncb);
9000 return 1;
9002 #endif
9006 ** Scsi command waiting list management.
9008 ** It may happen that we cannot insert a scsi command into the start queue,
9009 ** in the following circumstances.
9010 ** Too few preallocated ccb(s),
9011 ** maxtags < cmd_per_lun of the Linux host control block,
9012 ** etc...
9013 ** Such scsi commands are inserted into a waiting list.
9014 ** When a scsi command complete, we try to requeue the commands of the
9015 ** waiting list.
9018 #define next_wcmd host_scribble
9020 static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd)
9022 Scsi_Cmnd *wcmd;
9024 #ifdef DEBUG_WAITING_LIST
9025 printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
9026 #endif
9027 cmd->next_wcmd = 0;
9028 if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
9029 else {
9030 while ((wcmd->next_wcmd) != 0)
9031 wcmd = (Scsi_Cmnd *) wcmd->next_wcmd;
9032 wcmd->next_wcmd = (char *) cmd;
9036 static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd)
9038 Scsi_Cmnd **pcmd = &np->waiting_list;
9040 while (*pcmd) {
9041 if (cmd == *pcmd) {
9042 if (to_remove) {
9043 *pcmd = (Scsi_Cmnd *) cmd->next_wcmd;
9044 cmd->next_wcmd = 0;
9046 #ifdef DEBUG_WAITING_LIST
9047 printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
9048 #endif
9049 return cmd;
9051 pcmd = (Scsi_Cmnd **) &(*pcmd)->next_wcmd;
9053 return 0;
9056 static void process_waiting_list(ncb_p np, int sts)
9058 Scsi_Cmnd *waiting_list, *wcmd;
9060 waiting_list = np->waiting_list;
9061 np->waiting_list = 0;
9063 #ifdef DEBUG_WAITING_LIST
9064 if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
9065 #endif
9066 while ((wcmd = waiting_list) != 0) {
9067 waiting_list = (Scsi_Cmnd *) wcmd->next_wcmd;
9068 wcmd->next_wcmd = 0;
9069 if (sts == DID_OK) {
9070 #ifdef DEBUG_WAITING_LIST
9071 printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
9072 #endif
9073 sts = ncr_queue_command(np, wcmd);
9075 if (sts != DID_OK) {
9076 #ifdef DEBUG_WAITING_LIST
9077 printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
9078 #endif
9079 wcmd->result = ScsiResult(sts, 0);
9080 ncr_queue_done_cmd(np, wcmd);
9085 #undef next_wcmd
9087 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
9089 /*=========================================================================
9090 ** Proc file system stuff
9092 ** A read operation returns profile information.
9093 ** A write operation is a control command.
9094 ** The string is parsed in the driver code and the command is passed
9095 ** to the ncr_usercmd() function.
9096 **=========================================================================
9099 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
9101 #define is_digit(c) ((c) >= '0' && (c) <= '9')
9102 #define digit_to_bin(c) ((c) - '0')
9103 #define is_space(c) ((c) == ' ' || (c) == '\t')
9105 static int skip_spaces(char *ptr, int len)
9107 int cnt, c;
9109 for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
9111 return (len - cnt);
9114 static int get_int_arg(char *ptr, int len, u_long *pv)
9116 int cnt, c;
9117 u_long v;
9119 for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
9120 v = (v * 10) + digit_to_bin(c);
9123 if (pv)
9124 *pv = v;
9126 return (len - cnt);
9129 static int is_keyword(char *ptr, int len, char *verb)
9131 int verb_len = strlen(verb);
9133 if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
9134 return verb_len;
9135 else
9136 return 0;
9140 #define SKIP_SPACES(min_spaces) \
9141 if ((arg_len = skip_spaces(ptr, len)) < (min_spaces)) \
9142 return -EINVAL; \
9143 ptr += arg_len; len -= arg_len;
9145 #define GET_INT_ARG(v) \
9146 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
9147 return -EINVAL; \
9148 ptr += arg_len; len -= arg_len;
9152 ** Parse a control command
9155 static int ncr_user_command(ncb_p np, char *buffer, int length)
9157 char *ptr = buffer;
9158 int len = length;
9159 struct usrcmd *uc = &np->user;
9160 int arg_len;
9161 u_long target;
9163 bzero(uc, sizeof(*uc));
9165 if (len > 0 && ptr[len-1] == '\n')
9166 --len;
9168 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
9169 uc->cmd = UC_SETSYNC;
9170 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
9171 uc->cmd = UC_SETTAGS;
9172 else if ((arg_len = is_keyword(ptr, len, "setorder")) != 0)
9173 uc->cmd = UC_SETORDER;
9174 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
9175 uc->cmd = UC_SETVERBOSE;
9176 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
9177 uc->cmd = UC_SETWIDE;
9178 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
9179 uc->cmd = UC_SETDEBUG;
9180 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
9181 uc->cmd = UC_SETFLAG;
9182 else if ((arg_len = is_keyword(ptr, len, "clearprof")) != 0)
9183 uc->cmd = UC_CLEARPROF;
9184 else
9185 arg_len = 0;
9187 #ifdef DEBUG_PROC_INFO
9188 printk("ncr_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
9189 #endif
9191 if (!arg_len)
9192 return -EINVAL;
9193 ptr += arg_len; len -= arg_len;
9195 switch(uc->cmd) {
9196 case UC_SETSYNC:
9197 case UC_SETTAGS:
9198 case UC_SETWIDE:
9199 case UC_SETFLAG:
9200 SKIP_SPACES(1);
9201 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
9202 ptr += arg_len; len -= arg_len;
9203 uc->target = ~0;
9204 } else {
9205 GET_INT_ARG(target);
9206 uc->target = (1<<target);
9207 #ifdef DEBUG_PROC_INFO
9208 printk("ncr_user_command: target=%ld\n", target);
9209 #endif
9211 break;
9214 switch(uc->cmd) {
9215 case UC_SETVERBOSE:
9216 case UC_SETSYNC:
9217 case UC_SETTAGS:
9218 case UC_SETWIDE:
9219 SKIP_SPACES(1);
9220 GET_INT_ARG(uc->data);
9221 #ifdef DEBUG_PROC_INFO
9222 printk("ncr_user_command: data=%ld\n", uc->data);
9223 #endif
9224 break;
9225 case UC_SETORDER:
9226 SKIP_SPACES(1);
9227 if ((arg_len = is_keyword(ptr, len, "simple")))
9228 uc->data = M_SIMPLE_TAG;
9229 else if ((arg_len = is_keyword(ptr, len, "ordered")))
9230 uc->data = M_ORDERED_TAG;
9231 else if ((arg_len = is_keyword(ptr, len, "default")))
9232 uc->data = 0;
9233 else
9234 return -EINVAL;
9235 break;
9236 case UC_SETDEBUG:
9237 while (len > 0) {
9238 SKIP_SPACES(1);
9239 if ((arg_len = is_keyword(ptr, len, "alloc")))
9240 uc->data |= DEBUG_ALLOC;
9241 else if ((arg_len = is_keyword(ptr, len, "phase")))
9242 uc->data |= DEBUG_PHASE;
9243 else if ((arg_len = is_keyword(ptr, len, "queue")))
9244 uc->data |= DEBUG_QUEUE;
9245 else if ((arg_len = is_keyword(ptr, len, "result")))
9246 uc->data |= DEBUG_RESULT;
9247 else if ((arg_len = is_keyword(ptr, len, "scatter")))
9248 uc->data |= DEBUG_SCATTER;
9249 else if ((arg_len = is_keyword(ptr, len, "script")))
9250 uc->data |= DEBUG_SCRIPT;
9251 else if ((arg_len = is_keyword(ptr, len, "tiny")))
9252 uc->data |= DEBUG_TINY;
9253 else if ((arg_len = is_keyword(ptr, len, "timing")))
9254 uc->data |= DEBUG_TIMING;
9255 else if ((arg_len = is_keyword(ptr, len, "nego")))
9256 uc->data |= DEBUG_NEGO;
9257 else if ((arg_len = is_keyword(ptr, len, "tags")))
9258 uc->data |= DEBUG_TAGS;
9259 else
9260 return -EINVAL;
9261 ptr += arg_len; len -= arg_len;
9263 #ifdef DEBUG_PROC_INFO
9264 printk("ncr_user_command: data=%ld\n", uc->data);
9265 #endif
9266 break;
9267 case UC_SETFLAG:
9268 while (len > 0) {
9269 SKIP_SPACES(1);
9270 if ((arg_len = is_keyword(ptr, len, "trace")))
9271 uc->data |= UF_TRACE;
9272 else if ((arg_len = is_keyword(ptr, len, "no_disc")))
9273 uc->data |= UF_NODISC;
9274 else
9275 return -EINVAL;
9276 ptr += arg_len; len -= arg_len;
9278 break;
9279 default:
9280 break;
9283 if (len)
9284 return -EINVAL;
9285 else {
9286 long flags;
9288 NCR_LOCK_NCB(np, flags);
9289 ncr_usercmd (np);
9290 NCR_UNLOCK_NCB(np, flags);
9292 return length;
9295 #endif /* SCSI_NCR_USER_COMMAND_SUPPORT */
9298 #ifdef SCSI_NCR_USER_INFO_SUPPORT
9300 ** Copy formatted profile information into the input buffer.
9303 #define to_ms(t) ((t) * 1000 / HZ)
9305 static int ncr_host_info(ncb_p np, char *ptr, off_t offset, int len)
9307 struct info_str info;
9309 info.buffer = ptr;
9310 info.length = len;
9311 info.offset = offset;
9312 info.pos = 0;
9314 copy_info(&info, " Chip NCR53C%s, device id 0x%x, "
9315 "revision id 0x%x\n",
9316 np->chip_name, np->device_id, np->revision_id);
9317 copy_info(&info, " On PCI bus %d, device %d, function %d, "
9318 #ifdef __sparc__
9319 "IRQ %s\n",
9320 #else
9321 "IRQ %d\n",
9322 #endif
9323 np->bus, (np->device_fn & 0xf8) >> 3, np->device_fn & 7,
9324 #ifdef __sparc__
9325 __irq_itoa(np->irq));
9326 #else
9327 (int) np->irq);
9328 #endif
9329 copy_info(&info, " Synchronous period factor %d, "
9330 "max commands per lun %d\n",
9331 (int) np->minsync, MAX_TAGS);
9333 if (driver_setup.debug || driver_setup.verbose > 1) {
9334 copy_info(&info, " Debug flags 0x%x, verbosity level %d\n",
9335 driver_setup.debug, driver_setup.verbose);
9338 #ifdef SCSI_NCR_PROFILE_SUPPORT
9339 copy_info(&info, "Profiling information:\n");
9340 copy_info(&info, " %-12s = %lu\n", "num_trans",np->profile.num_trans);
9341 copy_info(&info, " %-12s = %lu\n", "num_kbytes",np->profile.num_kbytes);
9342 copy_info(&info, " %-12s = %lu\n", "num_disc", np->profile.num_disc);
9343 copy_info(&info, " %-12s = %lu\n", "num_break",np->profile.num_break);
9344 copy_info(&info, " %-12s = %lu\n", "num_int", np->profile.num_int);
9345 copy_info(&info, " %-12s = %lu\n", "num_fly", np->profile.num_fly);
9346 copy_info(&info, " %-12s = %lu\n", "ms_setup", to_ms(np->profile.ms_setup));
9347 copy_info(&info, " %-12s = %lu\n", "ms_data", to_ms(np->profile.ms_data));
9348 copy_info(&info, " %-12s = %lu\n", "ms_disc", to_ms(np->profile.ms_disc));
9349 copy_info(&info, " %-12s = %lu\n", "ms_post", to_ms(np->profile.ms_post));
9350 #endif
9352 return info.pos > info.offset? info.pos - info.offset : 0;
9355 #endif /* SCSI_NCR_USER_INFO_SUPPORT */
9358 ** Entry point of the scsi proc fs of the driver.
9359 ** - func = 0 means read (returns profile data)
9360 ** - func = 1 means write (parse user control command)
9363 static int ncr53c8xx_proc_info(char *buffer, char **start, off_t offset,
9364 int length, int hostno, int func)
9366 struct Scsi_Host *host;
9367 struct host_data *host_data;
9368 ncb_p ncb = 0;
9369 int retv;
9371 #ifdef DEBUG_PROC_INFO
9372 printk("ncr53c8xx_proc_info: hostno=%d, func=%d\n", hostno, func);
9373 #endif
9375 for (host = first_host; host; host = host->next) {
9376 if (host->hostt == the_template && host->host_no == hostno) {
9377 host_data = (struct host_data *) host->hostdata;
9378 ncb = host_data->ncb;
9379 break;
9383 if (!ncb)
9384 return -EINVAL;
9386 if (func) {
9387 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
9388 retv = ncr_user_command(ncb, buffer, length);
9389 #else
9390 retv = -EINVAL;
9391 #endif
9393 else {
9394 if (start)
9395 *start = buffer;
9396 #ifdef SCSI_NCR_USER_INFO_SUPPORT
9397 retv = ncr_host_info(ncb, buffer, offset, length);
9398 #else
9399 retv = -EINVAL;
9400 #endif
9403 return retv;
9406 /*=========================================================================
9407 ** End of proc file system stuff
9408 **=========================================================================
9410 #endif
9413 /*==========================================================
9415 ** /proc directory entry.
9417 **==========================================================
9419 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
9420 static struct proc_dir_entry proc_scsi_ncr53c8xx = {
9421 PROC_SCSI_NCR53C8XX, 9, NAME53C8XX,
9422 S_IFDIR | S_IRUGO | S_IXUGO, 2
9424 #endif
9426 /*==========================================================
9428 ** Boot command line.
9430 **==========================================================
9432 #ifdef MODULE
9433 char *ncr53c8xx = 0; /* command line passed by insmod */
9434 # if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,30)
9435 MODULE_PARM(ncr53c8xx, "s");
9436 # endif
9437 #endif
9439 int __init ncr53c8xx_setup(char *str)
9441 return sym53c8xx__setup(str);
9444 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,13)
9445 #ifndef MODULE
9446 __setup("ncr53c8xx=", ncr53c8xx_setup);
9447 #endif
9448 #endif
9450 /*===================================================================
9452 ** SYM53C8XX supported device list
9454 **===================================================================
9457 static u_short ncr_chip_ids[] __initdata = {
9458 PCI_DEVICE_ID_NCR_53C810,
9459 PCI_DEVICE_ID_NCR_53C815,
9460 PCI_DEVICE_ID_NCR_53C820,
9461 PCI_DEVICE_ID_NCR_53C825,
9462 PCI_DEVICE_ID_NCR_53C860,
9463 PCI_DEVICE_ID_NCR_53C875,
9464 PCI_DEVICE_ID_NCR_53C875J,
9465 PCI_DEVICE_ID_NCR_53C885,
9466 PCI_DEVICE_ID_NCR_53C895,
9467 PCI_DEVICE_ID_NCR_53C896,
9468 PCI_DEVICE_ID_NCR_53C895A,
9469 PCI_DEVICE_ID_NCR_53C1510D
9472 /*==========================================================
9474 ** Chip detection entry point.
9476 **==========================================================
9478 int __init ncr53c8xx_detect(Scsi_Host_Template *tpnt)
9481 ** Initialize driver general stuff.
9483 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
9484 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
9485 tpnt->proc_dir = &proc_scsi_ncr53c8xx;
9486 #else
9487 tpnt->proc_name = NAME53C8XX;
9488 #endif
9489 tpnt->proc_info = ncr53c8xx_proc_info;
9490 #endif
9492 #if defined(SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT) && defined(MODULE)
9493 if (ncr53c8xx)
9494 ncr53c8xx_setup(ncr53c8xx);
9495 #endif
9497 return sym53c8xx__detect(tpnt, ncr_chip_ids,
9498 sizeof(ncr_chip_ids)/sizeof(ncr_chip_ids[0]));
9501 /*==========================================================
9503 ** Entry point for info() function
9505 **==========================================================
9507 const char *ncr53c8xx_info (struct Scsi_Host *host)
9509 return SCSI_NCR_DRIVER_NAME;
9513 ** Module stuff
9516 static Scsi_Host_Template driver_template = NCR53C8XX;
9517 #include "scsi_module.c"