1 /******************************************************************************
2 ** Device driver for the PCI-SCSI NCR538XX controller family.
4 ** Copyright (C) 1994 Wolfgang Stanglmeier
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@free.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
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 **-----------------------------------------------------------------------------
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
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 ** 2005 by Matthew Wilcox and James Bottomley
73 ** PCI-ectomy. This driver now supports only the 720 chip (see the
74 ** NCR_Q720 and zalon drivers for the bus probe logic).
76 *******************************************************************************
80 ** Supported SCSI-II features:
81 ** Synchronous negotiation
82 ** Wide negotiation (depends on the NCR Chip)
83 ** Enable disconnection
84 ** Tagged command queuing
88 ** Supported NCR/SYMBIOS chips:
89 ** 53C720 (Wide, Fast SCSI-2, intfly problems)
92 /* Name and version of the driver */
93 #define SCSI_NCR_DRIVER_NAME "ncr53c8xx-3.4.3g"
95 #define SCSI_NCR_DEBUG_FLAGS (0)
97 #include <linux/blkdev.h>
98 #include <linux/delay.h>
99 #include <linux/dma-mapping.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/interrupt.h>
103 #include <linux/ioport.h>
104 #include <linux/mm.h>
105 #include <linux/module.h>
106 #include <linux/sched.h>
107 #include <linux/signal.h>
108 #include <linux/spinlock.h>
109 #include <linux/stat.h>
110 #include <linux/string.h>
111 #include <linux/time.h>
112 #include <linux/timer.h>
113 #include <linux/types.h>
117 #include <asm/system.h>
119 #include <scsi/scsi.h>
120 #include <scsi/scsi_cmnd.h>
121 #include <scsi/scsi_dbg.h>
122 #include <scsi/scsi_device.h>
123 #include <scsi/scsi_tcq.h>
124 #include <scsi/scsi_transport.h>
125 #include <scsi/scsi_transport_spi.h>
127 #include "ncr53c8xx.h"
129 #define NAME53C8XX "ncr53c8xx"
131 /*==========================================================
135 **==========================================================
138 #define DEBUG_ALLOC (0x0001)
139 #define DEBUG_PHASE (0x0002)
140 #define DEBUG_QUEUE (0x0008)
141 #define DEBUG_RESULT (0x0010)
142 #define DEBUG_POINTER (0x0020)
143 #define DEBUG_SCRIPT (0x0040)
144 #define DEBUG_TINY (0x0080)
145 #define DEBUG_TIMING (0x0100)
146 #define DEBUG_NEGO (0x0200)
147 #define DEBUG_TAGS (0x0400)
148 #define DEBUG_SCATTER (0x0800)
149 #define DEBUG_IC (0x1000)
152 ** Enable/Disable debug messages.
153 ** Can be changed at runtime too.
156 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
157 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
158 #define DEBUG_FLAGS ncr_debug
160 #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
163 static inline struct list_head *ncr_list_pop(struct list_head *head)
165 if (!list_empty(head)) {
166 struct list_head *elem = head->next;
175 /*==========================================================
177 ** Simple power of two buddy-like allocator.
179 ** This simple code is not intended to be fast, but to
180 ** provide power of 2 aligned memory allocations.
181 ** Since the SCRIPTS processor only supplies 8 bit
182 ** arithmetic, this allocator allows simple and fast
183 ** address calculations from the SCRIPTS code.
184 ** In addition, cache line alignment is guaranteed for
185 ** power of 2 cache line size.
186 ** Enhanced in linux-2.3.44 to provide a memory pool
187 ** per pcidev to support dynamic dma mapping. (I would
188 ** have preferred a real bus abstraction, btw).
190 **==========================================================
193 #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
194 #if PAGE_SIZE >= 8192
195 #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
197 #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
199 #define MEMO_FREE_UNUSED /* Free unused pages immediately */
201 #define MEMO_GFP_FLAGS GFP_ATOMIC
202 #define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER)
203 #define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT)
204 #define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1)
206 typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
207 typedef struct device *m_bush_t; /* Something that addresses DMAable */
209 typedef struct m_link { /* Link between free memory chunks */
213 typedef struct m_vtob { /* Virtual to Bus address translation */
218 #define VTOB_HASH_SHIFT 5
219 #define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT)
220 #define VTOB_HASH_MASK (VTOB_HASH_SIZE-1)
221 #define VTOB_HASH_CODE(m) \
222 ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
224 typedef struct m_pool { /* Memory pool of a given kind */
226 m_addr_t (*getp)(struct m_pool *);
227 void (*freep)(struct m_pool *, m_addr_t);
229 m_vtob_s *(vtob[VTOB_HASH_SIZE]);
231 struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
234 static void *___m_alloc(m_pool_s *mp, int size)
237 int s = (1 << MEMO_SHIFT);
242 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
252 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
253 h[j].next = (m_link_s *)mp->getp(mp);
255 h[j].next->next = NULL;
261 a = (m_addr_t) h[j].next;
263 h[j].next = h[j].next->next;
267 h[j].next = (m_link_s *) (a+s);
268 h[j].next->next = NULL;
272 printk("___m_alloc(%d) = %p\n", size, (void *) a);
277 static void ___m_free(m_pool_s *mp, void *ptr, int size)
280 int s = (1 << MEMO_SHIFT);
286 printk("___m_free(%p, %d)\n", ptr, size);
289 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
300 #ifdef MEMO_FREE_UNUSED
301 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
308 while (q->next && q->next != (m_link_s *) b) {
312 ((m_link_s *) a)->next = h[i].next;
313 h[i].next = (m_link_s *) a;
316 q->next = q->next->next;
323 static DEFINE_SPINLOCK(ncr53c8xx_lock);
325 static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
329 p = ___m_alloc(mp, size);
331 if (DEBUG_FLAGS & DEBUG_ALLOC)
332 printk ("new %-10s[%4d] @%p.\n", name, size, p);
336 else if (uflags & MEMO_WARN)
337 printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
342 #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
344 static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
346 if (DEBUG_FLAGS & DEBUG_ALLOC)
347 printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
349 ___m_free(mp, ptr, size);
354 * With pci bus iommu support, we use a default pool of unmapped memory
355 * for memory we donnot need to DMA from/to and one pool per pcidev for
356 * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
359 static m_addr_t ___mp0_getp(m_pool_s *mp)
361 m_addr_t m = __get_free_pages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER);
367 static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
369 free_pages(m, MEMO_PAGE_ORDER);
373 static m_pool_s mp0 = {NULL, ___mp0_getp, ___mp0_freep};
380 * With pci bus iommu support, we maintain one pool per pcidev and a
381 * hashed reverse table for virtual to bus physical address translations.
383 static m_addr_t ___dma_getp(m_pool_s *mp)
388 vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
391 vp = (m_addr_t) dma_alloc_coherent(mp->bush,
392 PAGE_SIZE<<MEMO_PAGE_ORDER,
395 int hc = VTOB_HASH_CODE(vp);
398 vbp->next = mp->vtob[hc];
405 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
409 static void ___dma_freep(m_pool_s *mp, m_addr_t m)
411 m_vtob_s **vbpp, *vbp;
412 int hc = VTOB_HASH_CODE(m);
414 vbpp = &mp->vtob[hc];
415 while (*vbpp && (*vbpp)->vaddr != m)
416 vbpp = &(*vbpp)->next;
419 *vbpp = (*vbpp)->next;
420 dma_free_coherent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
421 (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
422 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
427 static inline m_pool_s *___get_dma_pool(m_bush_t bush)
430 for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
434 static m_pool_s *___cre_dma_pool(m_bush_t bush)
437 mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
439 memset(mp, 0, sizeof(*mp));
441 mp->getp = ___dma_getp;
442 mp->freep = ___dma_freep;
449 static void ___del_dma_pool(m_pool_s *p)
451 struct m_pool **pp = &mp0.next;
453 while (*pp && *pp != p)
457 __m_free(&mp0, p, sizeof(*p), "MPOOL");
461 static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
467 spin_lock_irqsave(&ncr53c8xx_lock, flags);
468 mp = ___get_dma_pool(bush);
470 mp = ___cre_dma_pool(bush);
472 m = __m_calloc(mp, size, name);
475 spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
480 static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
485 spin_lock_irqsave(&ncr53c8xx_lock, flags);
486 mp = ___get_dma_pool(bush);
488 __m_free(mp, m, size, name);
491 spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
494 static m_addr_t __vtobus(m_bush_t bush, void *m)
498 int hc = VTOB_HASH_CODE(m);
500 m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
502 spin_lock_irqsave(&ncr53c8xx_lock, flags);
503 mp = ___get_dma_pool(bush);
506 while (vp && (m_addr_t) vp->vaddr != a)
509 spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
510 return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
513 #define _m_calloc_dma(np, s, n) __m_calloc_dma(np->dev, s, n)
514 #define _m_free_dma(np, p, s, n) __m_free_dma(np->dev, p, s, n)
515 #define m_calloc_dma(s, n) _m_calloc_dma(np, s, n)
516 #define m_free_dma(p, s, n) _m_free_dma(np, p, s, n)
517 #define _vtobus(np, p) __vtobus(np->dev, p)
518 #define vtobus(p) _vtobus(np, p)
521 * Deal with DMA mapping/unmapping.
524 /* To keep track of the dma mapping (sg/single) that has been set */
525 #define __data_mapped SCp.phase
526 #define __data_mapping SCp.have_data_in
528 static void __unmap_scsi_data(struct device *dev, struct scsi_cmnd *cmd)
530 switch(cmd->__data_mapped) {
532 dma_unmap_sg(dev, cmd->request_buffer, cmd->use_sg,
533 cmd->sc_data_direction);
536 dma_unmap_single(dev, cmd->__data_mapping,
537 cmd->request_bufflen,
538 cmd->sc_data_direction);
541 cmd->__data_mapped = 0;
544 static u_long __map_scsi_single_data(struct device *dev, struct scsi_cmnd *cmd)
548 if (cmd->request_bufflen == 0)
551 mapping = dma_map_single(dev, cmd->request_buffer,
552 cmd->request_bufflen,
553 cmd->sc_data_direction);
554 cmd->__data_mapped = 1;
555 cmd->__data_mapping = mapping;
560 static int __map_scsi_sg_data(struct device *dev, struct scsi_cmnd *cmd)
564 if (cmd->use_sg == 0)
567 use_sg = dma_map_sg(dev, cmd->request_buffer, cmd->use_sg,
568 cmd->sc_data_direction);
569 cmd->__data_mapped = 2;
570 cmd->__data_mapping = use_sg;
575 #define unmap_scsi_data(np, cmd) __unmap_scsi_data(np->dev, cmd)
576 #define map_scsi_single_data(np, cmd) __map_scsi_single_data(np->dev, cmd)
577 #define map_scsi_sg_data(np, cmd) __map_scsi_sg_data(np->dev, cmd)
579 /*==========================================================
583 ** This structure is initialized from linux config
584 ** options. It can be overridden at boot-up by the boot
587 **==========================================================
589 static struct ncr_driver_setup
590 driver_setup = SCSI_NCR_DRIVER_SETUP;
593 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
594 static struct ncr_driver_setup
595 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
599 #define initverbose (driver_setup.verbose)
600 #define bootverbose (np->verbose)
603 /*===================================================================
605 ** Driver setup from the boot command line
607 **===================================================================
617 #define OPT_MASTER_PARITY 2
618 #define OPT_SCSI_PARITY 3
619 #define OPT_DISCONNECTION 4
620 #define OPT_SPECIAL_FEATURES 5
621 #define OPT_UNUSED_1 6
622 #define OPT_FORCE_SYNC_NEGO 7
623 #define OPT_REVERSE_PROBE 8
624 #define OPT_DEFAULT_SYNC 9
625 #define OPT_VERBOSE 10
627 #define OPT_BURST_MAX 12
628 #define OPT_LED_PIN 13
629 #define OPT_MAX_WIDE 14
630 #define OPT_SETTLE_DELAY 15
631 #define OPT_DIFF_SUPPORT 16
633 #define OPT_PCI_FIX_UP 18
634 #define OPT_BUS_CHECK 19
635 #define OPT_OPTIMIZE 20
636 #define OPT_RECOVERY 21
637 #define OPT_SAFE_SETUP 22
638 #define OPT_USE_NVRAM 23
639 #define OPT_EXCLUDE 24
640 #define OPT_HOST_ID 25
642 #ifdef SCSI_NCR_IARB_SUPPORT
653 static char setup_token[] __initdata =
667 #ifdef SCSI_NCR_IARB_SUPPORT
670 ; /* DONNOT REMOVE THIS ';' */
672 static int __init get_setup_token(char *p)
674 char *cur = setup_token;
678 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
681 if (!strncmp(p, cur, pc - cur))
688 static int __init sym53c8xx__setup(char *str)
690 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
696 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
708 val = (int) simple_strtoul(pv, &pe, 0);
710 switch (get_setup_token(cur)) {
712 driver_setup.default_tags = val;
713 if (pe && *pe == '/') {
715 while (*pe && *pe != ARG_SEP &&
716 i < sizeof(driver_setup.tag_ctrl)-1) {
717 driver_setup.tag_ctrl[i++] = *pe++;
719 driver_setup.tag_ctrl[i] = '\0';
722 case OPT_MASTER_PARITY:
723 driver_setup.master_parity = val;
725 case OPT_SCSI_PARITY:
726 driver_setup.scsi_parity = val;
728 case OPT_DISCONNECTION:
729 driver_setup.disconnection = val;
731 case OPT_SPECIAL_FEATURES:
732 driver_setup.special_features = val;
734 case OPT_FORCE_SYNC_NEGO:
735 driver_setup.force_sync_nego = val;
737 case OPT_REVERSE_PROBE:
738 driver_setup.reverse_probe = val;
740 case OPT_DEFAULT_SYNC:
741 driver_setup.default_sync = val;
744 driver_setup.verbose = val;
747 driver_setup.debug = val;
750 driver_setup.burst_max = val;
753 driver_setup.led_pin = val;
756 driver_setup.max_wide = val? 1:0;
758 case OPT_SETTLE_DELAY:
759 driver_setup.settle_delay = val;
761 case OPT_DIFF_SUPPORT:
762 driver_setup.diff_support = val;
765 driver_setup.irqm = val;
768 driver_setup.pci_fix_up = val;
771 driver_setup.bus_check = val;
774 driver_setup.optimize = val;
777 driver_setup.recovery = val;
780 driver_setup.use_nvram = val;
783 memcpy(&driver_setup, &driver_safe_setup,
784 sizeof(driver_setup));
787 if (xi < SCSI_NCR_MAX_EXCLUDES)
788 driver_setup.excludes[xi++] = val;
791 driver_setup.host_id = val;
793 #ifdef SCSI_NCR_IARB_SUPPORT
795 driver_setup.iarb = val;
799 printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
803 if ((cur = strchr(cur, ARG_SEP)) != NULL)
806 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
811 /*===================================================================
813 ** Get device queue depth from boot command line.
815 **===================================================================
817 #define DEF_DEPTH (driver_setup.default_tags)
818 #define ALL_TARGETS -2
823 static int device_queue_depth(int unit, int target, int lun)
826 char *p = driver_setup.tag_ctrl;
832 while ((c = *p++) != 0) {
833 v = simple_strtoul(p, &ep, 0);
842 t = (target == v) ? v : NO_TARGET;
847 u = (lun == v) ? v : NO_LUN;
851 (t == ALL_TARGETS || t == target) &&
852 (u == ALL_LUNS || u == lun))
868 /*==========================================================
870 ** The CCB done queue uses an array of CCB virtual
871 ** addresses. Empty entries are flagged using the bogus
872 ** virtual address 0xffffffff.
874 ** Since PCI ensures that only aligned DWORDs are accessed
875 ** atomically, 64 bit little-endian architecture requires
876 ** to test the high order DWORD of the entry to determine
877 ** if it is empty or valid.
879 ** BTW, I will make things differently as soon as I will
880 ** have a better idea, but this is simple and should work.
882 **==========================================================
885 #define SCSI_NCR_CCB_DONE_SUPPORT
886 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
889 #define CCB_DONE_EMPTY 0xffffffffUL
891 /* All 32 bit architectures */
892 #if BITS_PER_LONG == 32
893 #define CCB_DONE_VALID(cp) (((u_long) cp) != CCB_DONE_EMPTY)
895 /* All > 32 bit (64 bit) architectures regardless endian-ness */
897 #define CCB_DONE_VALID(cp) \
898 ((((u_long) cp) & 0xffffffff00000000ul) && \
899 (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
902 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
904 /*==========================================================
906 ** Configuration and Debugging
908 **==========================================================
912 ** SCSI address of this device.
913 ** The boot routines should have set it.
917 #ifndef SCSI_NCR_MYADDR
918 #define SCSI_NCR_MYADDR (7)
922 ** The maximum number of tags per logic unit.
923 ** Used only for disk devices that support tags.
926 #ifndef SCSI_NCR_MAX_TAGS
927 #define SCSI_NCR_MAX_TAGS (8)
931 ** TAGS are actually limited to 64 tags/lun.
932 ** We need to deal with power of 2, for alignment constraints.
934 #if SCSI_NCR_MAX_TAGS > 64
935 #define MAX_TAGS (64)
937 #define MAX_TAGS SCSI_NCR_MAX_TAGS
943 ** Choose appropriate type for tag bitmap.
946 typedef u64 tagmap_t;
948 typedef u32 tagmap_t;
952 ** Number of targets supported by the driver.
953 ** n permits target numbers 0..n-1.
954 ** Default is 16, meaning targets #0..#15.
958 #ifdef SCSI_NCR_MAX_TARGET
959 #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
961 #define MAX_TARGET (16)
965 ** Number of logic units supported by the driver.
966 ** n enables logic unit numbers 0..n-1.
967 ** The common SCSI devices require only
968 ** one lun, so take 1 as the default.
971 #ifdef SCSI_NCR_MAX_LUN
972 #define MAX_LUN SCSI_NCR_MAX_LUN
978 ** Asynchronous pre-scaler (ns). Shall be 40
981 #ifndef SCSI_NCR_MIN_ASYNC
982 #define SCSI_NCR_MIN_ASYNC (40)
986 ** The maximum number of jobs scheduled for starting.
987 ** There should be one slot per target, and one slot
988 ** for each tag of each target in use.
989 ** The calculation below is actually quite silly ...
992 #ifdef SCSI_NCR_CAN_QUEUE
993 #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
995 #define MAX_START (MAX_TARGET + 7 * MAX_TAGS)
999 ** We limit the max number of pending IO to 250.
1000 ** since we donnot want to allocate more than 1
1001 ** PAGE for 'scripth'.
1005 #define MAX_START 250
1009 ** The maximum number of segments a transfer is split into.
1010 ** We support up to 127 segments for both read and write.
1011 ** The data scripts are broken into 2 sub-scripts.
1012 ** 80 (MAX_SCATTERL) segments are moved from a sub-script
1013 ** in on-chip RAM. This makes data transfers shorter than
1014 ** 80k (assuming 1k fs) as fast as possible.
1017 #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
1019 #if (MAX_SCATTER > 80)
1020 #define MAX_SCATTERL 80
1021 #define MAX_SCATTERH (MAX_SCATTER - MAX_SCATTERL)
1023 #define MAX_SCATTERL (MAX_SCATTER-1)
1024 #define MAX_SCATTERH 1
1031 #define NCR_SNOOP_TIMEOUT (1000000)
1034 ** Other definitions
1037 #define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
1039 #define initverbose (driver_setup.verbose)
1040 #define bootverbose (np->verbose)
1042 /*==========================================================
1044 ** Command control block states.
1046 **==========================================================
1051 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
1052 #define HS_DISCONNECT (3) /* Disconnected by target */
1054 #define HS_DONEMASK (0x80)
1055 #define HS_COMPLETE (4|HS_DONEMASK)
1056 #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
1057 #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
1058 #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
1059 #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
1060 #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
1061 #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
1064 ** Invalid host status values used by the SCRIPTS processor
1065 ** when the nexus is not fully identified.
1066 ** Shall never appear in a CCB.
1069 #define HS_INVALMASK (0x40)
1070 #define HS_SELECTING (0|HS_INVALMASK)
1071 #define HS_IN_RESELECT (1|HS_INVALMASK)
1072 #define HS_STARTING (2|HS_INVALMASK)
1075 ** Flags set by the SCRIPT processor for commands
1076 ** that have been skipped.
1078 #define HS_SKIPMASK (0x20)
1080 /*==========================================================
1082 ** Software Interrupt Codes
1084 **==========================================================
1087 #define SIR_BAD_STATUS (1)
1088 #define SIR_XXXXXXXXXX (2)
1089 #define SIR_NEGO_SYNC (3)
1090 #define SIR_NEGO_WIDE (4)
1091 #define SIR_NEGO_FAILED (5)
1092 #define SIR_NEGO_PROTO (6)
1093 #define SIR_REJECT_RECEIVED (7)
1094 #define SIR_REJECT_SENT (8)
1095 #define SIR_IGN_RESIDUE (9)
1096 #define SIR_MISSING_SAVE (10)
1097 #define SIR_RESEL_NO_MSG_IN (11)
1098 #define SIR_RESEL_NO_IDENTIFY (12)
1099 #define SIR_RESEL_BAD_LUN (13)
1100 #define SIR_RESEL_BAD_TARGET (14)
1101 #define SIR_RESEL_BAD_I_T_L (15)
1102 #define SIR_RESEL_BAD_I_T_L_Q (16)
1103 #define SIR_DONE_OVERFLOW (17)
1104 #define SIR_INTFLY (18)
1105 #define SIR_MAX (18)
1107 /*==========================================================
1109 ** Extended error codes.
1110 ** xerr_status field of struct ccb.
1112 **==========================================================
1116 #define XE_EXTRA_DATA (1) /* unexpected data phase */
1117 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
1119 /*==========================================================
1121 ** Negotiation status.
1122 ** nego_status field of struct ccb.
1124 **==========================================================
1127 #define NS_NOCHANGE (0)
1132 /*==========================================================
1136 **==========================================================
1139 #define CCB_MAGIC (0xf2691ad2)
1141 /*==========================================================
1143 ** Declaration of structs.
1145 **==========================================================
1148 static struct scsi_transport_template *ncr53c8xx_transport_template = NULL;
1168 #define UC_SETSYNC 10
1169 #define UC_SETTAGS 11
1170 #define UC_SETDEBUG 12
1171 #define UC_SETORDER 13
1172 #define UC_SETWIDE 14
1173 #define UC_SETFLAG 15
1174 #define UC_SETVERBOSE 17
1176 #define UF_TRACE (0x01)
1177 #define UF_NODISC (0x02)
1178 #define UF_NOSCAN (0x04)
1180 /*========================================================================
1182 ** Declaration of structs: target control block
1184 **========================================================================
1187 /*----------------------------------------------------------------
1188 ** During reselection the ncr jumps to this point with SFBR
1189 ** set to the encoded target number with bit 7 set.
1190 ** if it's not this target, jump to the next.
1192 ** JUMP IF (SFBR != #target#), @(next tcb)
1193 **----------------------------------------------------------------
1195 struct link jump_tcb;
1197 /*----------------------------------------------------------------
1198 ** Load the actual values for the sxfer and the scntl3
1199 ** register (sync/wide mode).
1201 ** SCR_COPY (1), @(sval field of this tcb), @(sxfer register)
1202 ** SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
1203 **----------------------------------------------------------------
1207 /*----------------------------------------------------------------
1208 ** Get the IDENTIFY message and load the LUN to SFBR.
1210 ** CALL, <RESEL_LUN>
1211 **----------------------------------------------------------------
1213 struct link call_lun;
1215 /*----------------------------------------------------------------
1216 ** Now look for the right lun.
1219 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
1221 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1222 ** It is kind of hashcoding.
1223 **----------------------------------------------------------------
1225 struct link jump_lcb[4]; /* JUMPs for reselection */
1226 struct lcb * lp[MAX_LUN]; /* The lcb's of this tcb */
1228 /*----------------------------------------------------------------
1229 ** Pointer to the ccb used for negotiation.
1230 ** Prevent from starting a negotiation for all queued commands
1231 ** when tagged command queuing is enabled.
1232 **----------------------------------------------------------------
1234 struct ccb * nego_cp;
1236 /*----------------------------------------------------------------
1238 **----------------------------------------------------------------
1243 /*----------------------------------------------------------------
1244 ** negotiation of wide and synch transfer and device quirks.
1245 **----------------------------------------------------------------
1247 #ifdef SCSI_NCR_BIG_ENDIAN
1250 /*3*/ u_char minsync;
1252 /*1*/ u_char widedone;
1253 /*2*/ u_char quirks;
1254 /*3*/ u_char maxoffs;
1256 /*0*/ u_char minsync;
1259 /*0*/ u_char maxoffs;
1260 /*1*/ u_char quirks;
1261 /*2*/ u_char widedone;
1265 /* User settable limits and options. */
1270 struct scsi_target *starget;
1273 /*========================================================================
1275 ** Declaration of structs: lun control block
1277 **========================================================================
1280 /*----------------------------------------------------------------
1281 ** During reselection the ncr jumps to this point
1282 ** with SFBR set to the "Identify" message.
1283 ** if it's not this lun, jump to the next.
1285 ** JUMP IF (SFBR != #lun#), @(next lcb of this target)
1287 ** It is this lun. Load TEMP with the nexus jumps table
1288 ** address and jump to RESEL_TAG (or RESEL_NOTAG).
1290 ** SCR_COPY (4), p_jump_ccb, TEMP,
1291 ** SCR_JUMP, <RESEL_TAG>
1292 **----------------------------------------------------------------
1294 struct link jump_lcb;
1295 ncrcmd load_jump_ccb[3];
1296 struct link jump_tag;
1297 ncrcmd p_jump_ccb; /* Jump table bus address */
1299 /*----------------------------------------------------------------
1300 ** Jump table used by the script processor to directly jump
1301 ** to the CCB corresponding to the reselected nexus.
1302 ** Address is allocated on 256 bytes boundary in order to
1303 ** allow 8 bit calculation of the tag jump entry for up to
1304 ** 64 possible tags.
1305 **----------------------------------------------------------------
1307 u32 jump_ccb_0; /* Default table if no tags */
1308 u32 *jump_ccb; /* Virtual address */
1310 /*----------------------------------------------------------------
1311 ** CCB queue management.
1312 **----------------------------------------------------------------
1314 struct list_head free_ccbq; /* Queue of available CCBs */
1315 struct list_head busy_ccbq; /* Queue of busy CCBs */
1316 struct list_head wait_ccbq; /* Queue of waiting for IO CCBs */
1317 struct list_head skip_ccbq; /* Queue of skipped CCBs */
1318 u_char actccbs; /* Number of allocated CCBs */
1319 u_char busyccbs; /* CCBs busy for this lun */
1320 u_char queuedccbs; /* CCBs queued to the controller*/
1321 u_char queuedepth; /* Queue depth for this lun */
1322 u_char scdev_depth; /* SCSI device queue depth */
1323 u_char maxnxs; /* Max possible nexuses */
1325 /*----------------------------------------------------------------
1326 ** Control of tagged command queuing.
1327 ** Tags allocation is performed using a circular buffer.
1328 ** This avoids using a loop for tag allocation.
1329 **----------------------------------------------------------------
1331 u_char ia_tag; /* Allocation index */
1332 u_char if_tag; /* Freeing index */
1333 u_char cb_tags[MAX_TAGS]; /* Circular tags buffer */
1334 u_char usetags; /* Command queuing is active */
1335 u_char maxtags; /* Max nr of tags asked by user */
1336 u_char numtags; /* Current number of tags */
1338 /*----------------------------------------------------------------
1339 ** QUEUE FULL control and ORDERED tag control.
1340 **----------------------------------------------------------------
1342 /*----------------------------------------------------------------
1343 ** QUEUE FULL and ORDERED tag control.
1344 **----------------------------------------------------------------
1346 u16 num_good; /* Nr of GOOD since QUEUE FULL */
1347 tagmap_t tags_umap; /* Used tags bitmap */
1348 tagmap_t tags_smap; /* Tags in use at 'tag_stime' */
1349 u_long tags_stime; /* Last time we set smap=umap */
1350 struct ccb * held_ccb; /* CCB held for QUEUE FULL */
1353 /*========================================================================
1355 ** Declaration of structs: the launch script.
1357 **========================================================================
1359 ** It is part of the CCB and is called by the scripts processor to
1360 ** start or restart the data structure (nexus).
1361 ** This 6 DWORDs mini script makes use of prefetching.
1363 **------------------------------------------------------------------------
1366 /*----------------------------------------------------------------
1367 ** SCR_COPY(4), @(p_phys), @(dsa register)
1368 ** SCR_JUMP, @(scheduler_point)
1369 **----------------------------------------------------------------
1371 ncrcmd setup_dsa[3]; /* Copy 'phys' address to dsa */
1372 struct link schedule; /* Jump to scheduler point */
1373 ncrcmd p_phys; /* 'phys' header bus address */
1376 /*========================================================================
1378 ** Declaration of structs: global HEADER.
1380 **========================================================================
1382 ** This substructure is copied from the ccb to a global address after
1383 ** selection (or reselection) and copied back before disconnect.
1385 ** These fields are accessible to the script processor.
1387 **------------------------------------------------------------------------
1391 /*----------------------------------------------------------------
1392 ** Saved data pointer.
1393 ** Points to the position in the script responsible for the
1394 ** actual transfer transfer of data.
1395 ** It's written after reception of a SAVE_DATA_POINTER message.
1396 ** The goalpointer points after the last transfer command.
1397 **----------------------------------------------------------------
1403 /*----------------------------------------------------------------
1404 ** Alternate data pointer.
1405 ** They are copied back to savep/lastp/goalp by the SCRIPTS
1406 ** when the direction is unknown and the device claims data out.
1407 **----------------------------------------------------------------
1412 /*----------------------------------------------------------------
1413 ** The virtual address of the ccb containing this header.
1414 **----------------------------------------------------------------
1418 /*----------------------------------------------------------------
1420 **----------------------------------------------------------------
1422 u_char scr_st[4]; /* script status */
1423 u_char status[4]; /* host status. must be the */
1424 /* last DWORD of the header. */
1428 ** The status bytes are used by the host and the script processor.
1430 ** The byte corresponding to the host_status must be stored in the
1431 ** last DWORD of the CCB header since it is used for command
1432 ** completion (ncr_wakeup()). Doing so, we are sure that the header
1433 ** has been entirely copied back to the CCB when the host_status is
1434 ** seen complete by the CPU.
1436 ** The last four bytes (status[4]) are copied to the scratchb register
1437 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
1438 ** and copied back just after disconnecting.
1439 ** Inside the script the XX_REG are used.
1441 ** The first four bytes (scr_st[4]) are used inside the script by
1443 ** Because source and destination must have the same alignment
1444 ** in a DWORD, the fields HAVE to be at the chosen offsets.
1445 ** xerr_st 0 (0x34) scratcha
1446 ** sync_st 1 (0x05) sxfer
1447 ** wide_st 3 (0x03) scntl3
1451 ** Last four bytes (script)
1455 #define HS_PRT nc_scr1
1457 #define SS_PRT nc_scr2
1461 ** Last four bytes (host)
1463 #ifdef SCSI_NCR_BIG_ENDIAN
1464 #define actualquirks phys.header.status[3]
1465 #define host_status phys.header.status[2]
1466 #define scsi_status phys.header.status[1]
1467 #define parity_status phys.header.status[0]
1469 #define actualquirks phys.header.status[0]
1470 #define host_status phys.header.status[1]
1471 #define scsi_status phys.header.status[2]
1472 #define parity_status phys.header.status[3]
1476 ** First four bytes (script)
1478 #define xerr_st header.scr_st[0]
1479 #define sync_st header.scr_st[1]
1480 #define nego_st header.scr_st[2]
1481 #define wide_st header.scr_st[3]
1484 ** First four bytes (host)
1486 #define xerr_status phys.xerr_st
1487 #define nego_status phys.nego_st
1490 #define sync_status phys.sync_st
1491 #define wide_status phys.wide_st
1494 /*==========================================================
1496 ** Declaration of structs: Data structure block
1498 **==========================================================
1500 ** During execution of a ccb by the script processor,
1501 ** the DSA (data structure address) register points
1502 ** to this substructure of the ccb.
1503 ** This substructure contains the header with
1504 ** the script-processor-changeable data and
1505 ** data blocks for the indirect move commands.
1507 **----------------------------------------------------------
1519 ** Table data for Script
1522 struct scr_tblsel select;
1523 struct scr_tblmove smsg ;
1524 struct scr_tblmove cmd ;
1525 struct scr_tblmove sense ;
1526 struct scr_tblmove data[MAX_SCATTER];
1530 /*========================================================================
1532 ** Declaration of structs: Command control block.
1534 **========================================================================
1537 /*----------------------------------------------------------------
1538 ** This is the data structure which is pointed by the DSA
1539 ** register when it is executed by the script processor.
1540 ** It must be the first entry because it contains the header
1541 ** as first entry that must be cache line aligned.
1542 **----------------------------------------------------------------
1546 /*----------------------------------------------------------------
1547 ** Mini-script used at CCB execution start-up.
1548 ** Load the DSA with the data structure address (phys) and
1549 ** jump to SELECT. Jump to CANCEL if CCB is to be canceled.
1550 **----------------------------------------------------------------
1552 struct launch start;
1554 /*----------------------------------------------------------------
1555 ** Mini-script used at CCB relection to restart the nexus.
1556 ** Load the DSA with the data structure address (phys) and
1557 ** jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
1558 **----------------------------------------------------------------
1560 struct launch restart;
1562 /*----------------------------------------------------------------
1563 ** If a data transfer phase is terminated too early
1564 ** (after reception of a message (i.e. DISCONNECT)),
1565 ** we have to prepare a mini script to transfer
1566 ** the rest of the data.
1567 **----------------------------------------------------------------
1571 /*----------------------------------------------------------------
1572 ** The general SCSI driver provides a
1573 ** pointer to a control block.
1574 **----------------------------------------------------------------
1576 struct scsi_cmnd *cmd; /* SCSI command */
1577 u_char cdb_buf[16]; /* Copy of CDB */
1578 u_char sense_buf[64];
1579 int data_len; /* Total data length */
1581 /*----------------------------------------------------------------
1583 ** We prepare a message to be sent after selection.
1584 ** We may use a second one if the command is rescheduled
1585 ** due to GETCC or QFULL.
1586 ** Contents are IDENTIFY and SIMPLE_TAG.
1587 ** While negotiating sync or wide transfer,
1588 ** a SDTR or WDTR message is appended.
1589 **----------------------------------------------------------------
1591 u_char scsi_smsg [8];
1592 u_char scsi_smsg2[8];
1594 /*----------------------------------------------------------------
1596 **----------------------------------------------------------------
1598 u_long p_ccb; /* BUS address of this CCB */
1599 u_char sensecmd[6]; /* Sense command */
1600 u_char tag; /* Tag for this transfer */
1601 /* 255 means no tag */
1606 struct ccb * link_ccb; /* Host adapter CCB chain */
1607 struct list_head link_ccbq; /* Link to unit CCB queue */
1608 u32 startp; /* Initial data pointer */
1609 u_long magic; /* Free / busy CCB flag */
1612 #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
1615 /*========================================================================
1617 ** Declaration of structs: NCR device descriptor
1619 **========================================================================
1622 /*----------------------------------------------------------------
1623 ** The global header.
1624 ** It is accessible to both the host and the script processor.
1625 ** Must be cache line size aligned (32 for x86) in order to
1626 ** allow cache line bursting when it is copied to/from CCB.
1627 **----------------------------------------------------------------
1631 /*----------------------------------------------------------------
1632 ** CCBs management queues.
1633 **----------------------------------------------------------------
1635 struct scsi_cmnd *waiting_list; /* Commands waiting for a CCB */
1636 /* when lcb is not allocated. */
1637 struct scsi_cmnd *done_list; /* Commands waiting for done() */
1638 /* callback to be invoked. */
1639 spinlock_t smp_lock; /* Lock for SMP threading */
1641 /*----------------------------------------------------------------
1642 ** Chip and controller indentification.
1643 **----------------------------------------------------------------
1645 int unit; /* Unit number */
1646 char inst_name[16]; /* ncb instance name */
1648 /*----------------------------------------------------------------
1649 ** Initial value of some IO register bits.
1650 ** These values are assumed to have been set by BIOS, and may
1651 ** be used for probing adapter implementation differences.
1652 **----------------------------------------------------------------
1654 u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest0, sv_ctest3,
1655 sv_ctest4, sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
1657 /*----------------------------------------------------------------
1658 ** Actual initial value of IO register bits used by the
1659 ** driver. They are loaded at initialisation according to
1660 ** features that are to be enabled.
1661 **----------------------------------------------------------------
1663 u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest0, rv_ctest3,
1664 rv_ctest4, rv_ctest5, rv_stest2;
1666 /*----------------------------------------------------------------
1667 ** Targets management.
1668 ** During reselection the ncr jumps to jump_tcb.
1669 ** The SFBR register is loaded with the encoded target id.
1671 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
1673 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1674 ** It is kind of hashcoding.
1675 **----------------------------------------------------------------
1677 struct link jump_tcb[4]; /* JUMPs for reselection */
1678 struct tcb target[MAX_TARGET]; /* Target data */
1680 /*----------------------------------------------------------------
1681 ** Virtual and physical bus addresses of the chip.
1682 **----------------------------------------------------------------
1684 void __iomem *vaddr; /* Virtual and bus address of */
1685 unsigned long paddr; /* chip's IO registers. */
1686 unsigned long paddr2; /* On-chip RAM bus address. */
1687 volatile /* Pointer to volatile for */
1688 struct ncr_reg __iomem *reg; /* memory mapped IO. */
1690 /*----------------------------------------------------------------
1691 ** SCRIPTS virtual and physical bus addresses.
1692 ** 'script' is loaded in the on-chip RAM if present.
1693 ** 'scripth' stays in main memory.
1694 **----------------------------------------------------------------
1696 struct script *script0; /* Copies of script and scripth */
1697 struct scripth *scripth0; /* relocated for this ncb. */
1698 struct scripth *scripth; /* Actual scripth virt. address */
1699 u_long p_script; /* Actual script and scripth */
1700 u_long p_scripth; /* bus addresses. */
1702 /*----------------------------------------------------------------
1703 ** General controller parameters and configuration.
1704 **----------------------------------------------------------------
1707 u_char revision_id; /* PCI device revision id */
1708 u32 irq; /* IRQ level */
1709 u32 features; /* Chip features map */
1710 u_char myaddr; /* SCSI id of the adapter */
1711 u_char maxburst; /* log base 2 of dwords burst */
1712 u_char maxwide; /* Maximum transfer width */
1713 u_char minsync; /* Minimum sync period factor */
1714 u_char maxsync; /* Maximum sync period factor */
1715 u_char maxoffs; /* Max scsi offset */
1716 u_char multiplier; /* Clock multiplier (1,2,4) */
1717 u_char clock_divn; /* Number of clock divisors */
1718 u_long clock_khz; /* SCSI clock frequency in KHz */
1720 /*----------------------------------------------------------------
1721 ** Start queue management.
1722 ** It is filled up by the host processor and accessed by the
1723 ** SCRIPTS processor in order to start SCSI commands.
1724 **----------------------------------------------------------------
1726 u16 squeueput; /* Next free slot of the queue */
1727 u16 actccbs; /* Number of allocated CCBs */
1728 u16 queuedccbs; /* Number of CCBs in start queue*/
1729 u16 queuedepth; /* Start queue depth */
1731 /*----------------------------------------------------------------
1733 **----------------------------------------------------------------
1735 struct timer_list timer; /* Timer handler link header */
1737 u_long settle_time; /* Resetting the SCSI BUS */
1739 /*----------------------------------------------------------------
1740 ** Debugging and profiling.
1741 **----------------------------------------------------------------
1743 struct ncr_reg regdump; /* Register dump */
1744 u_long regtime; /* Time it has been done */
1746 /*----------------------------------------------------------------
1747 ** Miscellaneous buffers accessed by the scripts-processor.
1748 ** They shall be DWORD aligned, because they may be read or
1749 ** written with a SCR_COPY script command.
1750 **----------------------------------------------------------------
1752 u_char msgout[8]; /* Buffer for MESSAGE OUT */
1753 u_char msgin [8]; /* Buffer for MESSAGE IN */
1754 u32 lastmsg; /* Last SCSI message sent */
1755 u_char scratch; /* Scratch for SCSI receive */
1757 /*----------------------------------------------------------------
1758 ** Miscellaneous configuration and status parameters.
1759 **----------------------------------------------------------------
1761 u_char disc; /* Diconnection allowed */
1762 u_char scsi_mode; /* Current SCSI BUS mode */
1763 u_char order; /* Tag order to use */
1764 u_char verbose; /* Verbosity for this controller*/
1765 int ncr_cache; /* Used for cache test at init. */
1766 u_long p_ncb; /* BUS address of this NCB */
1768 /*----------------------------------------------------------------
1769 ** Command completion handling.
1770 **----------------------------------------------------------------
1772 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1773 struct ccb *(ccb_done[MAX_DONE]);
1776 /*----------------------------------------------------------------
1777 ** Fields that should be removed or changed.
1778 **----------------------------------------------------------------
1780 struct ccb *ccb; /* Global CCB */
1781 struct usrcmd user; /* Command from user */
1782 volatile u_char release_stage; /* Synchronisation stage on release */
1785 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1786 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1788 /*==========================================================
1791 ** Script for NCR-Processor.
1793 ** Use ncr_script_fill() to create the variable parts.
1794 ** Use ncr_script_copy_and_bind() to make a copy and
1795 ** bind to physical addresses.
1798 **==========================================================
1800 ** We have to know the offsets of all labels before
1801 ** we reach them (for forward jumps).
1802 ** Therefore we declare a struct here.
1803 ** If you make changes inside the script,
1804 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1806 **----------------------------------------------------------
1810 ** For HP Zalon/53c720 systems, the Zalon interface
1811 ** between CPU and 53c720 does prefetches, which causes
1812 ** problems with self modifying scripts. The problem
1813 ** is overcome by calling a dummy subroutine after each
1814 ** modification, to force a refetch of the script on
1815 ** return from the subroutine.
1818 #ifdef CONFIG_NCR53C8XX_PREFETCH
1819 #define PREFETCH_FLUSH_CNT 2
1820 #define PREFETCH_FLUSH SCR_CALL, PADDRH (wait_dma),
1822 #define PREFETCH_FLUSH_CNT 0
1823 #define PREFETCH_FLUSH
1827 ** Script fragments which are loaded into the on-chip RAM
1828 ** of 825A, 875 and 895 chips.
1832 ncrcmd startpos [ 1];
1834 ncrcmd select2 [ 9 + PREFETCH_FLUSH_CNT];
1835 ncrcmd loadpos [ 4];
1836 ncrcmd send_ident [ 9];
1837 ncrcmd prepare [ 6];
1838 ncrcmd prepare2 [ 7];
1839 ncrcmd command [ 6];
1840 ncrcmd dispatch [ 32];
1842 ncrcmd no_data [ 17];
1845 ncrcmd msg_in2 [ 16];
1846 ncrcmd msg_bad [ 4];
1848 ncrcmd cleanup [ 6];
1849 ncrcmd complete [ 9];
1850 ncrcmd cleanup_ok [ 8 + PREFETCH_FLUSH_CNT];
1851 ncrcmd cleanup0 [ 1];
1852 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
1853 ncrcmd signal [ 12];
1856 ncrcmd done_pos [ 1];
1857 ncrcmd done_plug [ 2];
1858 ncrcmd done_end [ 7];
1860 ncrcmd save_dp [ 7];
1861 ncrcmd restore_dp [ 5];
1862 ncrcmd disconnect [ 10];
1863 ncrcmd msg_out [ 9];
1864 ncrcmd msg_out_done [ 7];
1866 ncrcmd reselect [ 8];
1867 ncrcmd reselected [ 8];
1868 ncrcmd resel_dsa [ 6 + PREFETCH_FLUSH_CNT];
1869 ncrcmd loadpos1 [ 4];
1870 ncrcmd resel_lun [ 6];
1871 ncrcmd resel_tag [ 6];
1872 ncrcmd jump_to_nexus [ 4 + PREFETCH_FLUSH_CNT];
1873 ncrcmd nexus_indirect [ 4];
1874 ncrcmd resel_notag [ 4];
1875 ncrcmd data_in [MAX_SCATTERL * 4];
1876 ncrcmd data_in2 [ 4];
1877 ncrcmd data_out [MAX_SCATTERL * 4];
1878 ncrcmd data_out2 [ 4];
1882 ** Script fragments which stay in main memory for all chips.
1885 ncrcmd tryloop [MAX_START*2];
1886 ncrcmd tryloop2 [ 2];
1887 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1888 ncrcmd done_queue [MAX_DONE*5];
1889 ncrcmd done_queue2 [ 2];
1891 ncrcmd select_no_atn [ 8];
1893 ncrcmd skip [ 9 + PREFETCH_FLUSH_CNT];
1895 ncrcmd par_err_data_in [ 6];
1896 ncrcmd par_err_other [ 4];
1897 ncrcmd msg_reject [ 8];
1898 ncrcmd msg_ign_residue [ 24];
1899 ncrcmd msg_extended [ 10];
1900 ncrcmd msg_ext_2 [ 10];
1901 ncrcmd msg_wdtr [ 14];
1902 ncrcmd send_wdtr [ 7];
1903 ncrcmd msg_ext_3 [ 10];
1904 ncrcmd msg_sdtr [ 14];
1905 ncrcmd send_sdtr [ 7];
1906 ncrcmd nego_bad_phase [ 4];
1907 ncrcmd msg_out_abort [ 10];
1908 ncrcmd hdata_in [MAX_SCATTERH * 4];
1909 ncrcmd hdata_in2 [ 2];
1910 ncrcmd hdata_out [MAX_SCATTERH * 4];
1911 ncrcmd hdata_out2 [ 2];
1913 ncrcmd aborttag [ 4];
1915 ncrcmd abort_resel [ 20];
1916 ncrcmd resend_ident [ 4];
1917 ncrcmd clratn_go_on [ 3];
1918 ncrcmd nxtdsp_go_on [ 1];
1919 ncrcmd sdata_in [ 8];
1920 ncrcmd data_io [ 18];
1921 ncrcmd bad_identify [ 12];
1922 ncrcmd bad_i_t_l [ 4];
1923 ncrcmd bad_i_t_l_q [ 4];
1924 ncrcmd bad_target [ 8];
1925 ncrcmd bad_status [ 8];
1926 ncrcmd start_ram [ 4 + PREFETCH_FLUSH_CNT];
1927 ncrcmd start_ram0 [ 4];
1928 ncrcmd sto_restart [ 5];
1929 ncrcmd wait_dma [ 2];
1930 ncrcmd snooptest [ 9];
1931 ncrcmd snoopend [ 2];
1934 /*==========================================================
1937 ** Function headers.
1940 **==========================================================
1943 static void ncr_alloc_ccb (struct ncb *np, u_char tn, u_char ln);
1944 static void ncr_complete (struct ncb *np, struct ccb *cp);
1945 static void ncr_exception (struct ncb *np);
1946 static void ncr_free_ccb (struct ncb *np, struct ccb *cp);
1947 static void ncr_init_ccb (struct ncb *np, struct ccb *cp);
1948 static void ncr_init_tcb (struct ncb *np, u_char tn);
1949 static struct lcb * ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln);
1950 static struct lcb * ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev);
1951 static void ncr_getclock (struct ncb *np, int mult);
1952 static void ncr_selectclock (struct ncb *np, u_char scntl3);
1953 static struct ccb *ncr_get_ccb (struct ncb *np, struct scsi_cmnd *cmd);
1954 static void ncr_chip_reset (struct ncb *np, int delay);
1955 static void ncr_init (struct ncb *np, int reset, char * msg, u_long code);
1956 static int ncr_int_sbmc (struct ncb *np);
1957 static int ncr_int_par (struct ncb *np);
1958 static void ncr_int_ma (struct ncb *np);
1959 static void ncr_int_sir (struct ncb *np);
1960 static void ncr_int_sto (struct ncb *np);
1961 static void ncr_negotiate (struct ncb* np, struct tcb* tp);
1962 static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr);
1964 static void ncr_script_copy_and_bind
1965 (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len);
1966 static void ncr_script_fill (struct script * scr, struct scripth * scripth);
1967 static int ncr_scatter (struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd);
1968 static void ncr_getsync (struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p);
1969 static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer);
1970 static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev);
1971 static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack);
1972 static int ncr_snooptest (struct ncb *np);
1973 static void ncr_timeout (struct ncb *np);
1974 static void ncr_wakeup (struct ncb *np, u_long code);
1975 static void ncr_wakeup_done (struct ncb *np);
1976 static void ncr_start_next_ccb (struct ncb *np, struct lcb * lp, int maxn);
1977 static void ncr_put_start_queue(struct ncb *np, struct ccb *cp);
1979 static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd);
1980 static struct scsi_cmnd *retrieve_from_waiting_list(int to_remove, struct ncb *np, struct scsi_cmnd *cmd);
1981 static void process_waiting_list(struct ncb *np, int sts);
1983 #define remove_from_waiting_list(np, cmd) \
1984 retrieve_from_waiting_list(1, (np), (cmd))
1985 #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
1986 #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
1988 static inline char *ncr_name (struct ncb *np)
1990 return np->inst_name;
1994 /*==========================================================
1997 ** Scripts for NCR-Processor.
1999 ** Use ncr_script_bind for binding to physical addresses.
2002 **==========================================================
2004 ** NADDR generates a reference to a field of the controller data.
2005 ** PADDR generates a reference to another part of the script.
2006 ** RADDR generates a reference to a script processor register.
2007 ** FADDR generates a reference to a script processor register
2010 **----------------------------------------------------------
2013 #define RELOC_SOFTC 0x40000000
2014 #define RELOC_LABEL 0x50000000
2015 #define RELOC_REGISTER 0x60000000
2017 #define RELOC_KVAR 0x70000000
2019 #define RELOC_LABELH 0x80000000
2020 #define RELOC_MASK 0xf0000000
2022 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
2023 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
2024 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
2025 #define RADDR(label) (RELOC_REGISTER | REG(label))
2026 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
2028 #define KVAR(which) (RELOC_KVAR | (which))
2032 #define SCRIPT_KVAR_JIFFIES (0)
2033 #define SCRIPT_KVAR_FIRST SCRIPT_KVAR_JIFFIES
2034 #define SCRIPT_KVAR_LAST SCRIPT_KVAR_JIFFIES
2036 * Kernel variables referenced in the scripts.
2037 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
2039 static void *script_kvars[] __initdata =
2040 { (void *)&jiffies };
2043 static struct script script0 __initdata = {
2044 /*--------------------------< START >-----------------------*/ {
2046 ** This NOP will be patched with LED ON
2047 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2054 SCR_FROM_REG (ctest2),
2057 ** Then jump to a certain point in tryloop.
2058 ** Due to the lack of indirect addressing the code
2059 ** is self modifying here.
2062 }/*-------------------------< STARTPOS >--------------------*/,{
2065 }/*-------------------------< SELECT >----------------------*/,{
2067 ** DSA contains the address of a scheduled
2070 ** SCRATCHA contains the address of the script,
2071 ** which starts the next entry.
2073 ** Set Initiator mode.
2075 ** (Target mode is left as an exercise for the reader)
2080 SCR_LOAD_REG (HS_REG, HS_SELECTING),
2084 ** And try to select this target.
2086 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2089 }/*-------------------------< SELECT2 >----------------------*/,{
2091 ** Now there are 4 possibilities:
2093 ** (1) The ncr loses arbitration.
2094 ** This is ok, because it will try again,
2095 ** when the bus becomes idle.
2096 ** (But beware of the timeout function!)
2098 ** (2) The ncr is reselected.
2099 ** Then the script processor takes the jump
2100 ** to the RESELECT label.
2102 ** (3) The ncr wins arbitration.
2103 ** Then it will execute SCRIPTS instruction until
2104 ** the next instruction that checks SCSI phase.
2105 ** Then will stop and wait for selection to be
2106 ** complete or selection time-out to occur.
2107 ** As a result the SCRIPTS instructions until
2108 ** LOADPOS + 2 should be executed in parallel with
2109 ** the SCSI core performing selection.
2113 ** The MESSAGE_REJECT problem seems to be due to a selection
2115 ** Wait immediately for the selection to complete.
2116 ** (2.5x behaves so)
2118 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2122 ** Next time use the next slot.
2128 ** The ncr doesn't have an indirect load
2129 ** or store command. So we have to
2130 ** copy part of the control block to a
2131 ** fixed place, where we can access it.
2133 ** We patch the address part of a
2134 ** COPY command with the DSA-register.
2140 ** Flush script prefetch if required
2144 ** then we do the actual copy.
2146 SCR_COPY (sizeof (struct head)),
2148 ** continued after the next label ...
2150 }/*-------------------------< LOADPOS >---------------------*/,{
2154 ** Wait for the next phase or the selection
2155 ** to complete or time-out.
2157 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2160 }/*-------------------------< SEND_IDENT >----------------------*/,{
2162 ** Selection complete.
2163 ** Send the IDENTIFY and SIMPLE_TAG messages
2164 ** (and the EXTENDED_SDTR message)
2166 SCR_MOVE_TBL ^ SCR_MSG_OUT,
2167 offsetof (struct dsb, smsg),
2168 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2169 PADDRH (resend_ident),
2170 SCR_LOAD_REG (scratcha, 0x80),
2175 }/*-------------------------< PREPARE >----------------------*/,{
2177 ** load the savep (saved pointer) into
2178 ** the TEMP register (actual pointer)
2181 NADDR (header.savep),
2184 ** Initialize the status registers
2187 NADDR (header.status),
2189 }/*-------------------------< PREPARE2 >---------------------*/,{
2191 ** Initialize the msgout buffer with a NOOP message.
2193 SCR_LOAD_REG (scratcha, NOP),
2204 ** Anticipate the COMMAND phase.
2205 ** This is the normal case for initial selection.
2207 SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
2210 }/*-------------------------< COMMAND >--------------------*/,{
2212 ** ... and send the command
2214 SCR_MOVE_TBL ^ SCR_COMMAND,
2215 offsetof (struct dsb, cmd),
2217 ** If status is still HS_NEGOTIATE, negotiation failed.
2218 ** We check this here, since we want to do that
2221 SCR_FROM_REG (HS_REG),
2223 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2226 }/*-----------------------< DISPATCH >----------------------*/,{
2228 ** MSG_IN is the only phase that shall be
2229 ** entered at least once for each (re)selection.
2230 ** So we test it first.
2232 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
2235 SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
2238 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
2239 ** Possible data corruption during Memory Write and Invalidate.
2240 ** This work-around resets the addressing logic prior to the
2241 ** start of the first MOVE of a DATA IN phase.
2242 ** (See Documentation/scsi/ncr53c8xx.txt for more information)
2244 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2251 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
2253 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
2255 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
2258 ** Discard one illegal phase byte, if required.
2260 SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
2265 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
2267 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
2269 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
2271 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
2276 }/*-------------------------< CLRACK >----------------------*/,{
2278 ** Terminate possible pending message phase.
2285 }/*-------------------------< NO_DATA >--------------------*/,{
2287 ** The target wants to tranfer too much data
2288 ** or in the wrong direction.
2289 ** Remember that in extended error.
2291 SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
2297 ** Discard one data byte, if required.
2299 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2301 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
2303 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2305 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2308 ** .. and repeat as required.
2315 }/*-------------------------< STATUS >--------------------*/,{
2319 SCR_MOVE_ABS (1) ^ SCR_STATUS,
2322 ** save status to scsi_status.
2323 ** mark as complete.
2325 SCR_TO_REG (SS_REG),
2327 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
2331 }/*-------------------------< MSG_IN >--------------------*/,{
2333 ** Get the first byte of the message
2334 ** and save it to SCRATCHA.
2336 ** The script processor doesn't negate the
2337 ** ACK signal after this transfer.
2339 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2341 }/*-------------------------< MSG_IN2 >--------------------*/,{
2343 ** Handle this message.
2345 SCR_JUMP ^ IFTRUE (DATA (COMMAND_COMPLETE)),
2347 SCR_JUMP ^ IFTRUE (DATA (DISCONNECT)),
2349 SCR_JUMP ^ IFTRUE (DATA (SAVE_POINTERS)),
2351 SCR_JUMP ^ IFTRUE (DATA (RESTORE_POINTERS)),
2353 SCR_JUMP ^ IFTRUE (DATA (EXTENDED_MESSAGE)),
2354 PADDRH (msg_extended),
2355 SCR_JUMP ^ IFTRUE (DATA (NOP)),
2357 SCR_JUMP ^ IFTRUE (DATA (MESSAGE_REJECT)),
2358 PADDRH (msg_reject),
2359 SCR_JUMP ^ IFTRUE (DATA (IGNORE_WIDE_RESIDUE)),
2360 PADDRH (msg_ign_residue),
2362 ** Rest of the messages left as
2365 ** Unimplemented messages:
2366 ** fall through to MSG_BAD.
2368 }/*-------------------------< MSG_BAD >------------------*/,{
2370 ** unimplemented message - reject it.
2374 SCR_LOAD_REG (scratcha, MESSAGE_REJECT),
2376 }/*-------------------------< SETMSG >----------------------*/,{
2384 }/*-------------------------< CLEANUP >-------------------*/,{
2386 ** dsa: Pointer to ccb
2387 ** or xxxxxxFF (no ccb)
2389 ** HS_REG: Host-Status (<>0!)
2393 SCR_JUMP ^ IFTRUE (DATA (0xff)),
2397 ** complete the cleanup.
2402 }/*-------------------------< COMPLETE >-----------------*/,{
2404 ** Complete message.
2406 ** Copy TEMP register to LASTP in header.
2410 NADDR (header.lastp),
2412 ** When we terminate the cycle by clearing ACK,
2413 ** the target may disconnect immediately.
2415 ** We don't want to be told of an
2416 ** "unexpected disconnect",
2417 ** so we disable this feature.
2419 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2422 ** Terminate cycle ...
2424 SCR_CLR (SCR_ACK|SCR_ATN),
2427 ** ... and wait for the disconnect.
2431 }/*-------------------------< CLEANUP_OK >----------------*/,{
2433 ** Save host status to header.
2437 NADDR (header.status),
2439 ** and copy back the header to the ccb.
2445 ** Flush script prefetch if required
2448 SCR_COPY (sizeof (struct head)),
2450 }/*-------------------------< CLEANUP0 >--------------------*/,{
2452 }/*-------------------------< SIGNAL >----------------------*/,{
2454 ** if job not completed ...
2456 SCR_FROM_REG (HS_REG),
2459 ** ... start the next command.
2461 SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
2464 ** If command resulted in not GOOD status,
2465 ** call the C code if needed.
2467 SCR_FROM_REG (SS_REG),
2469 SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
2470 PADDRH (bad_status),
2472 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
2475 ** ... signal completion to the host
2480 ** Auf zu neuen Schandtaten!
2485 #else /* defined SCSI_NCR_CCB_DONE_SUPPORT */
2488 ** ... signal completion to the host
2491 }/*------------------------< DONE_POS >---------------------*/,{
2492 PADDRH (done_queue),
2493 }/*------------------------< DONE_PLUG >--------------------*/,{
2496 }/*------------------------< DONE_END >---------------------*/,{
2505 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2507 }/*-------------------------< SAVE_DP >------------------*/,{
2510 ** Copy TEMP register to SAVEP in header.
2514 NADDR (header.savep),
2519 }/*-------------------------< RESTORE_DP >---------------*/,{
2521 ** RESTORE_DP message:
2522 ** Copy SAVEP in header to TEMP register.
2525 NADDR (header.savep),
2530 }/*-------------------------< DISCONNECT >---------------*/,{
2532 ** DISCONNECTing ...
2534 ** disable the "unexpected disconnect" feature,
2535 ** and remove the ACK signal.
2537 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2539 SCR_CLR (SCR_ACK|SCR_ATN),
2542 ** Wait for the disconnect.
2547 ** Status is: DISCONNECTED.
2549 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2554 }/*-------------------------< MSG_OUT >-------------------*/,{
2556 ** The target requests a message.
2558 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2564 ** If it was no ABORT message ...
2566 SCR_JUMP ^ IFTRUE (DATA (ABORT_TASK_SET)),
2567 PADDRH (msg_out_abort),
2569 ** ... wait for the next phase
2570 ** if it's a message out, send it again, ...
2572 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2574 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2576 ** ... else clear the message ...
2578 SCR_LOAD_REG (scratcha, NOP),
2584 ** ... and process the next phase
2588 }/*-------------------------< IDLE >------------------------*/,{
2591 ** Wait for reselect.
2592 ** This NOP will be patched with LED OFF
2593 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2597 }/*-------------------------< RESELECT >--------------------*/,{
2599 ** make the DSA invalid.
2601 SCR_LOAD_REG (dsa, 0xff),
2605 SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
2608 ** Sleep waiting for a reselection.
2609 ** If SIGP is set, special treatment.
2611 ** Zu allem bereit ..
2615 }/*-------------------------< RESELECTED >------------------*/,{
2617 ** This NOP will be patched with LED ON
2618 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2623 ** ... zu nichts zu gebrauchen ?
2625 ** load the target id into the SFBR
2626 ** and jump to the control block.
2628 ** Look at the declarations of
2633 ** to understand what's going on.
2635 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2642 }/*-------------------------< RESEL_DSA >-------------------*/,{
2644 ** Ack the IDENTIFY or TAG previously received.
2649 ** The ncr doesn't have an indirect load
2650 ** or store command. So we have to
2651 ** copy part of the control block to a
2652 ** fixed place, where we can access it.
2654 ** We patch the address part of a
2655 ** COPY command with the DSA-register.
2661 ** Flush script prefetch if required
2665 ** then we do the actual copy.
2667 SCR_COPY (sizeof (struct head)),
2669 ** continued after the next label ...
2672 }/*-------------------------< LOADPOS1 >-------------------*/,{
2676 ** The DSA contains the data structure address.
2681 }/*-------------------------< RESEL_LUN >-------------------*/,{
2683 ** come back to this point
2684 ** to get an IDENTIFY message
2685 ** Wait for a msg_in phase.
2687 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2688 SIR_RESEL_NO_MSG_IN,
2691 ** Read the data directly from the BUS DATA lines.
2692 ** This helps to support very old SCSI devices that
2693 ** may reselect without sending an IDENTIFY.
2695 SCR_FROM_REG (sbdl),
2698 ** It should be an Identify message.
2702 }/*-------------------------< RESEL_TAG >-------------------*/,{
2704 ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
2705 ** Agressive optimization, is'nt it?
2706 ** No need to test the SIMPLE TAG message, since the
2707 ** driver only supports conformant devices for tags. ;-)
2709 SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
2712 ** Read the TAG from the SIDL.
2713 ** Still an aggressive optimization. ;-)
2714 ** Compute the CCB indirect jump address which
2715 ** is (#TAG*2 & 0xfc) due to tag numbering using
2716 ** 1,3,5..MAXTAGS*2+1 actual values.
2718 SCR_REG_SFBR (sidl, SCR_SHL, 0),
2720 SCR_SFBR_REG (temp, SCR_AND, 0xfc),
2722 }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
2725 PADDR (nexus_indirect),
2727 ** Flush script prefetch if required
2731 }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
2736 }/*-------------------------< RESEL_NOTAG >-------------------*/,{
2739 ** Read an throw away the IDENTIFY.
2741 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2744 PADDR (jump_to_nexus),
2745 }/*-------------------------< DATA_IN >--------------------*/,{
2747 ** Because the size depends on the
2748 ** #define MAX_SCATTERL parameter,
2749 ** it is filled in at runtime.
2751 ** ##===========< i=0; i<MAX_SCATTERL >=========
2752 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2753 ** || PADDR (dispatch),
2754 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2755 ** || offsetof (struct dsb, data[ i]),
2756 ** ##==========================================
2758 **---------------------------------------------------------
2761 }/*-------------------------< DATA_IN2 >-------------------*/,{
2766 }/*-------------------------< DATA_OUT >--------------------*/,{
2768 ** Because the size depends on the
2769 ** #define MAX_SCATTERL parameter,
2770 ** it is filled in at runtime.
2772 ** ##===========< i=0; i<MAX_SCATTERL >=========
2773 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2774 ** || PADDR (dispatch),
2775 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2776 ** || offsetof (struct dsb, data[ i]),
2777 ** ##==========================================
2779 **---------------------------------------------------------
2782 }/*-------------------------< DATA_OUT2 >-------------------*/,{
2787 }/*--------------------------------------------------------*/
2790 static struct scripth scripth0 __initdata = {
2791 /*-------------------------< TRYLOOP >---------------------*/{
2793 ** Start the next entry.
2794 ** Called addresses point to the launch script in the CCB.
2795 ** They are patched by the main processor.
2797 ** Because the size depends on the
2798 ** #define MAX_START parameter, it is filled
2801 **-----------------------------------------------------------
2803 ** ##===========< I=0; i<MAX_START >===========
2806 ** ##==========================================
2808 **-----------------------------------------------------------
2811 }/*------------------------< TRYLOOP2 >---------------------*/,{
2815 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2817 }/*------------------------< DONE_QUEUE >-------------------*/,{
2819 ** Copy the CCB address to the next done entry.
2820 ** Because the size depends on the
2821 ** #define MAX_DONE parameter, it is filled
2824 **-----------------------------------------------------------
2826 ** ##===========< I=0; i<MAX_DONE >===========
2827 ** || SCR_COPY (sizeof(struct ccb *),
2828 ** || NADDR (header.cp),
2829 ** || NADDR (ccb_done[i]),
2831 ** || PADDR (done_end),
2832 ** ##==========================================
2834 **-----------------------------------------------------------
2837 }/*------------------------< DONE_QUEUE2 >------------------*/,{
2839 PADDRH (done_queue),
2841 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2842 }/*------------------------< SELECT_NO_ATN >-----------------*/,{
2844 ** Set Initiator mode.
2845 ** And try to select this target without ATN.
2850 SCR_LOAD_REG (HS_REG, HS_SELECTING),
2852 SCR_SEL_TBL ^ offsetof (struct dsb, select),
2857 }/*-------------------------< CANCEL >------------------------*/,{
2859 SCR_LOAD_REG (scratcha, HS_ABORTED),
2863 }/*-------------------------< SKIP >------------------------*/,{
2864 SCR_LOAD_REG (scratcha, 0),
2867 ** This entry has been canceled.
2868 ** Next time use the next slot.
2874 ** The ncr doesn't have an indirect load
2875 ** or store command. So we have to
2876 ** copy part of the control block to a
2877 ** fixed place, where we can access it.
2879 ** We patch the address part of a
2880 ** COPY command with the DSA-register.
2886 ** Flush script prefetch if required
2890 ** then we do the actual copy.
2892 SCR_COPY (sizeof (struct head)),
2894 ** continued after the next label ...
2896 }/*-------------------------< SKIP2 >---------------------*/,{
2900 ** Initialize the status registers
2903 NADDR (header.status),
2906 ** Force host status.
2908 SCR_FROM_REG (scratcha),
2910 SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
2912 SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
2916 SCR_TO_REG (HS_REG),
2918 SCR_LOAD_REG (SS_REG, S_GOOD),
2923 },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
2925 ** Ignore all data in byte, until next phase
2927 SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2928 PADDRH (par_err_other),
2929 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2933 },/*-------------------------< PAR_ERR_OTHER >------------------*/{
2937 SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
2940 ** jump to dispatcher.
2944 }/*-------------------------< MSG_REJECT >---------------*/,{
2946 ** If a negotiation was in progress,
2947 ** negotiation failed.
2948 ** Otherwise, let the C code print
2951 SCR_FROM_REG (HS_REG),
2953 SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
2954 SIR_REJECT_RECEIVED,
2955 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2960 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2966 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2969 ** get residue size.
2971 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2974 ** Size is 0 .. ignore message.
2976 SCR_JUMP ^ IFTRUE (DATA (0)),
2979 ** Size is not 1 .. have to interrupt.
2981 SCR_JUMPR ^ IFFALSE (DATA (1)),
2984 ** Check for residue byte in swide register
2986 SCR_FROM_REG (scntl2),
2988 SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2991 ** There IS data in the swide register.
2994 SCR_REG_REG (scntl2, SCR_OR, WSR),
2999 ** Load again the size to the sfbr register.
3001 SCR_FROM_REG (scratcha),
3008 }/*-------------------------< MSG_EXTENDED >-------------*/,{
3014 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3019 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3023 SCR_JUMP ^ IFTRUE (DATA (3)),
3025 SCR_JUMP ^ IFFALSE (DATA (2)),
3027 }/*-------------------------< MSG_EXT_2 >----------------*/,{
3030 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3033 ** get extended message code.
3035 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3037 SCR_JUMP ^ IFTRUE (DATA (EXTENDED_WDTR)),
3040 ** unknown extended message
3044 }/*-------------------------< MSG_WDTR >-----------------*/,{
3047 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3050 ** get data bus width
3052 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3055 ** let the host do the real work.
3060 ** let the target fetch our answer.
3066 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3067 PADDRH (nego_bad_phase),
3069 }/*-------------------------< SEND_WDTR >----------------*/,{
3071 ** Send the EXTENDED_WDTR
3073 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
3079 PADDR (msg_out_done),
3081 }/*-------------------------< MSG_EXT_3 >----------------*/,{
3084 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3087 ** get extended message code.
3089 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3091 SCR_JUMP ^ IFTRUE (DATA (EXTENDED_SDTR)),
3094 ** unknown extended message
3099 }/*-------------------------< MSG_SDTR >-----------------*/,{
3102 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3105 ** get period and offset
3107 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
3110 ** let the host do the real work.
3115 ** let the target fetch our answer.
3121 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3122 PADDRH (nego_bad_phase),
3124 }/*-------------------------< SEND_SDTR >-------------*/,{
3126 ** Send the EXTENDED_SDTR
3128 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
3134 PADDR (msg_out_done),
3136 }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
3142 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
3144 ** After ABORT message,
3146 ** expect an immediate disconnect, ...
3148 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3150 SCR_CLR (SCR_ACK|SCR_ATN),
3155 ** ... and set the status to "ABORTED"
3157 SCR_LOAD_REG (HS_REG, HS_ABORTED),
3162 }/*-------------------------< HDATA_IN >-------------------*/,{
3164 ** Because the size depends on the
3165 ** #define MAX_SCATTERH parameter,
3166 ** it is filled in at runtime.
3168 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3169 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3170 ** || PADDR (dispatch),
3171 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
3172 ** || offsetof (struct dsb, data[ i]),
3173 ** ##===================================================
3175 **---------------------------------------------------------
3178 }/*-------------------------< HDATA_IN2 >------------------*/,{
3182 }/*-------------------------< HDATA_OUT >-------------------*/,{
3184 ** Because the size depends on the
3185 ** #define MAX_SCATTERH parameter,
3186 ** it is filled in at runtime.
3188 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3189 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3190 ** || PADDR (dispatch),
3191 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
3192 ** || offsetof (struct dsb, data[ i]),
3193 ** ##===================================================
3195 **---------------------------------------------------------
3198 }/*-------------------------< HDATA_OUT2 >------------------*/,{
3202 }/*-------------------------< RESET >----------------------*/,{
3204 ** Send a TARGET_RESET message if bad IDENTIFY
3205 ** received on reselection.
3207 SCR_LOAD_REG (scratcha, ABORT_TASK),
3210 PADDRH (abort_resel),
3211 }/*-------------------------< ABORTTAG >-------------------*/,{
3213 ** Abort a wrong tag received on reselection.
3215 SCR_LOAD_REG (scratcha, ABORT_TASK),
3218 PADDRH (abort_resel),
3219 }/*-------------------------< ABORT >----------------------*/,{
3221 ** Abort a reselection when no active CCB.
3223 SCR_LOAD_REG (scratcha, ABORT_TASK_SET),
3225 }/*-------------------------< ABORT_RESEL >----------------*/,{
3235 ** we expect an immediate disconnect
3237 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3239 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
3244 SCR_CLR (SCR_ACK|SCR_ATN),
3250 }/*-------------------------< RESEND_IDENT >-------------------*/,{
3252 ** The target stays in MSG OUT phase after having acked
3253 ** Identify [+ Tag [+ Extended message ]]. Targets shall
3254 ** behave this way on parity error.
3255 ** We must send it again all the messages.
3257 SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
3258 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
3261 }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
3265 }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
3267 }/*-------------------------< SDATA_IN >-------------------*/,{
3268 SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3270 SCR_MOVE_TBL ^ SCR_DATA_IN,
3271 offsetof (struct dsb, sense),
3276 }/*-------------------------< DATA_IO >--------------------*/,{
3278 ** We jump here if the data direction was unknown at the
3279 ** time we had to queue the command to the scripts processor.
3280 ** Pointers had been set as follow in this situation:
3281 ** savep --> DATA_IO
3282 ** lastp --> start pointer when DATA_IN
3283 ** goalp --> goal pointer when DATA_IN
3284 ** wlastp --> start pointer when DATA_OUT
3285 ** wgoalp --> goal pointer when DATA_OUT
3286 ** This script sets savep/lastp/goalp according to the
3287 ** direction chosen by the target.
3289 SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
3292 ** Direction is DATA IN.
3293 ** Warning: we jump here, even when phase is DATA OUT.
3296 NADDR (header.lastp),
3297 NADDR (header.savep),
3300 ** Jump to the SCRIPTS according to actual direction.
3303 NADDR (header.savep),
3308 ** Direction is DATA OUT.
3311 NADDR (header.wlastp),
3312 NADDR (header.lastp),
3314 NADDR (header.wgoalp),
3315 NADDR (header.goalp),
3318 }/*-------------------------< BAD_IDENTIFY >---------------*/,{
3320 ** If message phase but not an IDENTIFY,
3321 ** get some help from the C code.
3322 ** Old SCSI device may behave so.
3324 SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
3327 SIR_RESEL_NO_IDENTIFY,
3331 ** Message is an IDENTIFY, but lun is unknown.
3332 ** Read the message, since we got it directly
3333 ** from the SCSI BUS data lines.
3334 ** Signal problem to C code for logging the event.
3335 ** Send an ABORT_TASK_SET to clear all pending tasks.
3339 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3343 }/*-------------------------< BAD_I_T_L >------------------*/,{
3345 ** We donnot have a task for that I_T_L.
3346 ** Signal problem to C code for logging the event.
3347 ** Send an ABORT_TASK_SET message.
3350 SIR_RESEL_BAD_I_T_L,
3353 }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
3355 ** We donnot have a task that matches the tag.
3356 ** Signal problem to C code for logging the event.
3357 ** Send an ABORT_TASK message.
3360 SIR_RESEL_BAD_I_T_L_Q,
3363 }/*-------------------------< BAD_TARGET >-----------------*/,{
3365 ** We donnot know the target that reselected us.
3366 ** Grab the first message if any (IDENTIFY).
3367 ** Signal problem to C code for logging the event.
3368 ** TARGET_RESET message.
3371 SIR_RESEL_BAD_TARGET,
3372 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
3374 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3378 }/*-------------------------< BAD_STATUS >-----------------*/,{
3380 ** If command resulted in either QUEUE FULL,
3381 ** CHECK CONDITION or COMMAND TERMINATED,
3384 SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
3386 SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
3388 SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
3392 }/*-------------------------< START_RAM >-------------------*/,{
3394 ** Load the script into on-chip RAM,
3395 ** and jump to start point.
3399 PADDRH (start_ram0),
3401 ** Flush script prefetch if required
3404 SCR_COPY (sizeof (struct script)),
3405 }/*-------------------------< START_RAM0 >--------------------*/,{
3410 }/*-------------------------< STO_RESTART >-------------------*/,{
3413 ** Repair start queue (e.g. next time use the next slot)
3414 ** and jump to start point.
3421 }/*-------------------------< WAIT_DMA >-------------------*/,{
3423 ** For HP Zalon/53c720 systems, the Zalon interface
3424 ** between CPU and 53c720 does prefetches, which causes
3425 ** problems with self modifying scripts. The problem
3426 ** is overcome by calling a dummy subroutine after each
3427 ** modification, to force a refetch of the script on
3428 ** return from the subroutine.
3432 }/*-------------------------< SNOOPTEST >-------------------*/,{
3434 ** Read the variable.
3440 ** Write the variable.
3446 ** Read back the variable.
3451 }/*-------------------------< SNOOPEND >-------------------*/,{
3457 }/*--------------------------------------------------------*/
3460 /*==========================================================
3463 ** Fill in #define dependent parts of the script
3466 **==========================================================
3469 void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
3475 for (i=0; i<MAX_START; i++) {
3480 BUG_ON((u_long)p != (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
3482 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
3484 p = scrh->done_queue;
3485 for (i = 0; i<MAX_DONE; i++) {
3486 *p++ =SCR_COPY (sizeof(struct ccb *));
3487 *p++ =NADDR (header.cp);
3488 *p++ =NADDR (ccb_done[i]);
3490 *p++ =PADDR (done_end);
3493 BUG_ON((u_long)p != (u_long)&scrh->done_queue+sizeof(scrh->done_queue));
3495 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
3498 for (i=0; i<MAX_SCATTERH; i++) {
3499 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3500 *p++ =PADDR (dispatch);
3501 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3502 *p++ =offsetof (struct dsb, data[i]);
3505 BUG_ON((u_long)p != (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
3508 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3509 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3510 *p++ =PADDR (dispatch);
3511 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3512 *p++ =offsetof (struct dsb, data[i]);
3515 BUG_ON((u_long)p != (u_long)&scr->data_in + sizeof (scr->data_in));
3517 p = scrh->hdata_out;
3518 for (i=0; i<MAX_SCATTERH; i++) {
3519 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3520 *p++ =PADDR (dispatch);
3521 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3522 *p++ =offsetof (struct dsb, data[i]);
3525 BUG_ON((u_long)p != (u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
3528 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3529 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3530 *p++ =PADDR (dispatch);
3531 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3532 *p++ =offsetof (struct dsb, data[i]);
3535 BUG_ON((u_long) p != (u_long)&scr->data_out + sizeof (scr->data_out));
3538 /*==========================================================
3541 ** Copy and rebind a script.
3544 **==========================================================
3548 ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
3550 ncrcmd opcode, new, old, tmp1, tmp2;
3551 ncrcmd *start, *end;
3561 *dst++ = cpu_to_scr(opcode);
3564 ** If we forget to change the length
3565 ** in struct script, a field will be
3566 ** padded with 0. This is an illegal
3571 printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
3572 ncr_name(np), (int) (src-start-1));
3576 if (DEBUG_FLAGS & DEBUG_SCRIPT)
3577 printk (KERN_DEBUG "%p: <%x>\n",
3578 (src-1), (unsigned)opcode);
3581 ** We don't have to decode ALL commands
3583 switch (opcode >> 28) {
3587 ** COPY has TWO arguments.
3592 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3597 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3600 if ((tmp1 ^ tmp2) & 3) {
3601 printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
3602 ncr_name(np), (int) (src-start-1));
3606 ** If PREFETCH feature not enabled, remove
3607 ** the NO FLUSH bit if present.
3609 if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
3610 dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
3617 ** MOVE (absolute address)
3625 ** don't relocate if relative :-)
3627 if (opcode & 0x00800000)
3649 switch (old & RELOC_MASK) {
3650 case RELOC_REGISTER:
3651 new = (old & ~RELOC_MASK) + np->paddr;
3654 new = (old & ~RELOC_MASK) + np->p_script;
3657 new = (old & ~RELOC_MASK) + np->p_scripth;
3660 new = (old & ~RELOC_MASK) + np->p_ncb;
3664 if (((old & ~RELOC_MASK) <
3665 SCRIPT_KVAR_FIRST) ||
3666 ((old & ~RELOC_MASK) >
3668 panic("ncr KVAR out of range");
3669 new = vtophys(script_kvars[old &
3674 /* Don't relocate a 0 address. */
3681 panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
3685 *dst++ = cpu_to_scr(new);
3688 *dst++ = cpu_to_scr(*src++);
3694 ** Linux host data structure
3701 #define PRINT_ADDR(cmd, arg...) dev_info(&cmd->device->sdev_gendev , ## arg)
3703 static void ncr_print_msg(struct ccb *cp, char *label, u_char *msg)
3705 PRINT_ADDR(cp->cmd, "%s: ", label);
3711 /*==========================================================
3713 ** NCR chip clock divisor table.
3714 ** Divisors are multiplied by 10,000,000 in order to make
3715 ** calculations more simple.
3717 **==========================================================
3721 static u_long div_10M[] =
3722 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3725 /*===============================================================
3727 ** Prepare io register values used by ncr_init() according
3728 ** to selected and supported features.
3730 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3731 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3732 ** We use log base 2 (burst length) as internal code, with
3733 ** value 0 meaning "burst disabled".
3735 **===============================================================
3739 * Burst length from burst code.
3741 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3744 * Burst code from io register bits. Burst enable is ctest0 for c720
3746 #define burst_code(dmode, ctest0) \
3747 (ctest0) & 0x80 ? 0 : (((dmode) & 0xc0) >> 6) + 1
3750 * Set initial io register bits from burst code.
3752 static inline void ncr_init_burst(struct ncb *np, u_char bc)
3754 u_char *be = &np->rv_ctest0;
3756 np->rv_dmode &= ~(0x3 << 6);
3757 np->rv_ctest5 &= ~0x4;
3763 np->rv_dmode |= ((bc & 0x3) << 6);
3764 np->rv_ctest5 |= (bc & 0x4);
3768 static void __init ncr_prepare_setting(struct ncb *np)
3775 ** Save assumed BIOS setting
3778 np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
3779 np->sv_scntl3 = INB(nc_scntl3) & 0x07;
3780 np->sv_dmode = INB(nc_dmode) & 0xce;
3781 np->sv_dcntl = INB(nc_dcntl) & 0xa8;
3782 np->sv_ctest0 = INB(nc_ctest0) & 0x84;
3783 np->sv_ctest3 = INB(nc_ctest3) & 0x01;
3784 np->sv_ctest4 = INB(nc_ctest4) & 0x80;
3785 np->sv_ctest5 = INB(nc_ctest5) & 0x24;
3786 np->sv_gpcntl = INB(nc_gpcntl);
3787 np->sv_stest2 = INB(nc_stest2) & 0x20;
3788 np->sv_stest4 = INB(nc_stest4);
3794 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
3797 * Guess the frequency of the chip's clock.
3799 if (np->features & FE_ULTRA)
3800 np->clock_khz = 80000;
3802 np->clock_khz = 40000;
3805 * Get the clock multiplier factor.
3807 if (np->features & FE_QUAD)
3809 else if (np->features & FE_DBLR)
3815 * Measure SCSI clock frequency for chips
3816 * it may vary from assumed one.
3818 if (np->features & FE_VARCLK)
3819 ncr_getclock(np, np->multiplier);
3822 * Divisor to be used for async (timer pre-scaler).
3824 i = np->clock_divn - 1;
3826 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
3831 np->rv_scntl3 = i+1;
3834 * Minimum synchronous period factor supported by the chip.
3835 * Btw, 'period' is in tenths of nanoseconds.
3838 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
3839 if (period <= 250) np->minsync = 10;
3840 else if (period <= 303) np->minsync = 11;
3841 else if (period <= 500) np->minsync = 12;
3842 else np->minsync = (period + 40 - 1) / 40;
3845 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3848 if (np->minsync < 25 && !(np->features & FE_ULTRA))
3852 * Maximum synchronous period factor supported by the chip.
3855 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
3856 np->maxsync = period > 2540 ? 254 : period / 10;
3859 ** Prepare initial value of other IO registers
3861 #if defined SCSI_NCR_TRUST_BIOS_SETTING
3862 np->rv_scntl0 = np->sv_scntl0;
3863 np->rv_dmode = np->sv_dmode;
3864 np->rv_dcntl = np->sv_dcntl;
3865 np->rv_ctest0 = np->sv_ctest0;
3866 np->rv_ctest3 = np->sv_ctest3;
3867 np->rv_ctest4 = np->sv_ctest4;
3868 np->rv_ctest5 = np->sv_ctest5;
3869 burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
3873 ** Select burst length (dwords)
3875 burst_max = driver_setup.burst_max;
3876 if (burst_max == 255)
3877 burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
3880 if (burst_max > np->maxburst)
3881 burst_max = np->maxburst;
3884 ** Select all supported special features
3886 if (np->features & FE_ERL)
3887 np->rv_dmode |= ERL; /* Enable Read Line */
3888 if (np->features & FE_BOF)
3889 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
3890 if (np->features & FE_ERMP)
3891 np->rv_dmode |= ERMP; /* Enable Read Multiple */
3892 if (np->features & FE_PFEN)
3893 np->rv_dcntl |= PFEN; /* Prefetch Enable */
3894 if (np->features & FE_CLSE)
3895 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
3896 if (np->features & FE_WRIE)
3897 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
3898 if (np->features & FE_DFS)
3899 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
3900 if (np->features & FE_MUX)
3901 np->rv_ctest4 |= MUX; /* Host bus multiplex mode */
3902 if (np->features & FE_EA)
3903 np->rv_dcntl |= EA; /* Enable ACK */
3904 if (np->features & FE_EHP)
3905 np->rv_ctest0 |= EHP; /* Even host parity */
3908 ** Select some other
3910 if (driver_setup.master_parity)
3911 np->rv_ctest4 |= MPEE; /* Master parity checking */
3912 if (driver_setup.scsi_parity)
3913 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
3916 ** Get SCSI addr of host adapter (set by bios?).
3918 if (np->myaddr == 255) {
3919 np->myaddr = INB(nc_scid) & 0x07;
3921 np->myaddr = SCSI_NCR_MYADDR;
3924 #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
3927 * Prepare initial io register bits for burst length
3929 ncr_init_burst(np, burst_max);
3932 ** Set SCSI BUS mode.
3934 ** - ULTRA2 chips (895/895A/896) report the current
3935 ** BUS mode through the STEST4 IO register.
3936 ** - For previous generation chips (825/825A/875),
3937 ** user has to tell us how to check against HVD,
3938 ** since a 100% safe algorithm is not possible.
3940 np->scsi_mode = SMODE_SE;
3941 if (np->features & FE_DIFF) {
3942 switch(driver_setup.diff_support) {
3943 case 4: /* Trust previous settings if present, then GPIO3 */
3944 if (np->sv_scntl3) {
3945 if (np->sv_stest2 & 0x20)
3946 np->scsi_mode = SMODE_HVD;
3949 case 3: /* SYMBIOS controllers report HVD through GPIO3 */
3950 if (INB(nc_gpreg) & 0x08)
3952 case 2: /* Set HVD unconditionally */
3953 np->scsi_mode = SMODE_HVD;
3954 case 1: /* Trust previous settings for HVD */
3955 if (np->sv_stest2 & 0x20)
3956 np->scsi_mode = SMODE_HVD;
3958 default:/* Don't care about HVD */
3962 if (np->scsi_mode == SMODE_HVD)
3963 np->rv_stest2 |= 0x20;
3966 ** Set LED support from SCRIPTS.
3967 ** Ignore this feature for boards known to use a
3968 ** specific GPIO wiring and for the 895A or 896
3969 ** that drive the LED directly.
3970 ** Also probe initial setting of GPIO0 as output.
3972 if ((driver_setup.led_pin) &&
3973 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
3974 np->features |= FE_LED0;
3979 switch(driver_setup.irqm & 3) {
3981 np->rv_dcntl |= IRQM;
3984 np->rv_dcntl |= (np->sv_dcntl & IRQM);
3991 ** Configure targets according to driver setup.
3992 ** Allow to override sync, wide and NOSCAN from
3993 ** boot command line.
3995 for (i = 0 ; i < MAX_TARGET ; i++) {
3996 struct tcb *tp = &np->target[i];
3998 tp->usrsync = driver_setup.default_sync;
3999 tp->usrwide = driver_setup.max_wide;
4000 tp->usrtags = MAX_TAGS;
4001 tp->period = 0xffff;
4002 if (!driver_setup.disconnection)
4003 np->target[i].usrflag = UF_NODISC;
4007 ** Announce all that stuff to user.
4010 printk(KERN_INFO "%s: ID %d, Fast-%d%s%s\n", ncr_name(np),
4012 np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
4013 (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
4014 (np->rv_stest2 & 0x20) ? ", Differential" : "");
4016 if (bootverbose > 1) {
4017 printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4018 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4019 ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
4020 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
4022 printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4023 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4024 ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
4025 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
4028 if (bootverbose && np->paddr2)
4029 printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
4030 ncr_name(np), np->paddr2);
4033 /*==========================================================
4036 ** Done SCSI commands list management.
4038 ** We donnot enter the scsi_done() callback immediately
4039 ** after a command has been seen as completed but we
4040 ** insert it into a list which is flushed outside any kind
4041 ** of driver critical section.
4042 ** This allows to do minimal stuff under interrupt and
4043 ** inside critical sections and to also avoid locking up
4044 ** on recursive calls to driver entry points under SMP.
4045 ** In fact, the only kernel point which is entered by the
4046 ** driver with a driver lock set is kmalloc(GFP_ATOMIC)
4047 ** that shall not reenter the driver under any circumstances,
4050 **==========================================================
4052 static inline void ncr_queue_done_cmd(struct ncb *np, struct scsi_cmnd *cmd)
4054 unmap_scsi_data(np, cmd);
4055 cmd->host_scribble = (char *) np->done_list;
4056 np->done_list = cmd;
4059 static inline void ncr_flush_done_cmds(struct scsi_cmnd *lcmd)
4061 struct scsi_cmnd *cmd;
4065 lcmd = (struct scsi_cmnd *) cmd->host_scribble;
4066 cmd->scsi_done(cmd);
4070 /*==========================================================
4073 ** Prepare the next negotiation message if needed.
4075 ** Fill in the part of message buffer that contains the
4076 ** negotiation and the nego_status field of the CCB.
4077 ** Returns the size of the message in bytes.
4080 **==========================================================
4084 static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
4086 struct tcb *tp = &np->target[cp->target];
4089 struct scsi_target *starget = tp->starget;
4091 /* negotiate wide transfers ? */
4092 if (!tp->widedone) {
4093 if (spi_support_wide(starget)) {
4099 /* negotiate synchronous transfers? */
4100 if (!nego && !tp->period) {
4101 if (spi_support_sync(starget)) {
4105 dev_info(&starget->dev, "target did not report SYNC.\n");
4111 msglen += spi_populate_sync_msg(msgptr + msglen,
4112 tp->maxoffs ? tp->minsync : 0, tp->maxoffs);
4115 msglen += spi_populate_width_msg(msgptr + msglen, tp->usrwide);
4119 cp->nego_status = nego;
4123 if (DEBUG_FLAGS & DEBUG_NEGO) {
4124 ncr_print_msg(cp, nego == NS_WIDE ?
4125 "wide msgout":"sync_msgout", msgptr);
4134 /*==========================================================
4137 ** Start execution of a SCSI command.
4138 ** This is called from the generic SCSI driver.
4141 **==========================================================
4143 static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
4145 struct scsi_device *sdev = cmd->device;
4146 struct tcb *tp = &np->target[sdev->id];
4147 struct lcb *lp = tp->lp[sdev->lun];
4151 u_char idmsg, *msgptr;
4156 /*---------------------------------------------
4158 ** Some shortcuts ...
4160 **---------------------------------------------
4162 if ((sdev->id == np->myaddr ) ||
4163 (sdev->id >= MAX_TARGET) ||
4164 (sdev->lun >= MAX_LUN )) {
4165 return(DID_BAD_TARGET);
4168 /*---------------------------------------------
4170 ** Complete the 1st TEST UNIT READY command
4171 ** with error condition if the device is
4172 ** flagged NOSCAN, in order to speed up
4175 **---------------------------------------------
4177 if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12) &&
4178 (tp->usrflag & UF_NOSCAN)) {
4179 tp->usrflag &= ~UF_NOSCAN;
4180 return DID_BAD_TARGET;
4183 if (DEBUG_FLAGS & DEBUG_TINY) {
4184 PRINT_ADDR(cmd, "CMD=%x ", cmd->cmnd[0]);
4187 /*---------------------------------------------------
4189 ** Assign a ccb / bind cmd.
4190 ** If resetting, shorten settle_time if necessary
4191 ** in order to avoid spurious timeouts.
4192 ** If resetting or no free ccb,
4193 ** insert cmd into the waiting list.
4195 **----------------------------------------------------
4197 if (np->settle_time && cmd->timeout_per_command >= HZ) {
4198 u_long tlimit = jiffies + cmd->timeout_per_command - HZ;
4199 if (time_after(np->settle_time, tlimit))
4200 np->settle_time = tlimit;
4203 if (np->settle_time || !(cp=ncr_get_ccb (np, cmd))) {
4204 insert_into_waiting_list(np, cmd);
4209 /*----------------------------------------------------
4211 ** Build the identify / tag / sdtr message
4213 **----------------------------------------------------
4216 idmsg = IDENTIFY(0, sdev->lun);
4218 if (cp ->tag != NO_TAG ||
4219 (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
4222 msgptr = cp->scsi_smsg;
4224 msgptr[msglen++] = idmsg;
4226 if (cp->tag != NO_TAG) {
4227 char order = np->order;
4230 ** Force ordered tag if necessary to avoid timeouts
4231 ** and to preserve interactivity.
4233 if (lp && time_after(jiffies, lp->tags_stime)) {
4234 if (lp->tags_smap) {
4235 order = ORDERED_QUEUE_TAG;
4236 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
4238 "ordered tag forced.\n");
4241 lp->tags_stime = jiffies + 3*HZ;
4242 lp->tags_smap = lp->tags_umap;
4247 ** Ordered write ops, unordered read ops.
4249 switch (cmd->cmnd[0]) {
4250 case 0x08: /* READ_SMALL (6) */
4251 case 0x28: /* READ_BIG (10) */
4252 case 0xa8: /* READ_HUGE (12) */
4253 order = SIMPLE_QUEUE_TAG;
4256 order = ORDERED_QUEUE_TAG;
4259 msgptr[msglen++] = order;
4261 ** Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
4262 ** since we may have to deal with devices that have
4263 ** problems with #TAG 0 or too great #TAG numbers.
4265 msgptr[msglen++] = (cp->tag << 1) + 1;
4268 /*----------------------------------------------------
4270 ** Build the data descriptors
4272 **----------------------------------------------------
4275 direction = cmd->sc_data_direction;
4276 if (direction != DMA_NONE) {
4277 segments = ncr_scatter(np, cp, cp->cmd);
4279 ncr_free_ccb(np, cp);
4288 /*---------------------------------------------------
4290 ** negotiation required?
4292 ** (nego_status is filled by ncr_prepare_nego())
4294 **---------------------------------------------------
4297 cp->nego_status = 0;
4299 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
4300 msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
4303 /*----------------------------------------------------
4305 ** Determine xfer direction.
4307 **----------------------------------------------------
4310 direction = DMA_NONE;
4313 ** If data direction is BIDIRECTIONAL, speculate FROM_DEVICE
4314 ** but prepare alternate pointers for TO_DEVICE in case
4315 ** of our speculation will be just wrong.
4316 ** SCRIPTS will swap values if needed.
4319 case DMA_BIDIRECTIONAL:
4321 goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
4322 if (segments <= MAX_SCATTERL)
4323 lastp = goalp - 8 - (segments * 16);
4325 lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
4326 lastp -= (segments - MAX_SCATTERL) * 16;
4328 if (direction != DMA_BIDIRECTIONAL)
4330 cp->phys.header.wgoalp = cpu_to_scr(goalp);
4331 cp->phys.header.wlastp = cpu_to_scr(lastp);
4333 case DMA_FROM_DEVICE:
4334 goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
4335 if (segments <= MAX_SCATTERL)
4336 lastp = goalp - 8 - (segments * 16);
4338 lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
4339 lastp -= (segments - MAX_SCATTERL) * 16;
4344 lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
4349 ** Set all pointers values needed by SCRIPTS.
4350 ** If direction is unknown, start at data_io.
4352 cp->phys.header.lastp = cpu_to_scr(lastp);
4353 cp->phys.header.goalp = cpu_to_scr(goalp);
4355 if (direction == DMA_BIDIRECTIONAL)
4356 cp->phys.header.savep =
4357 cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
4359 cp->phys.header.savep= cpu_to_scr(lastp);
4362 ** Save the initial data pointer in order to be able
4363 ** to redo the command.
4365 cp->startp = cp->phys.header.savep;
4367 /*----------------------------------------------------
4371 **----------------------------------------------------
4374 ** physical -> virtual backlink
4375 ** Generic SCSI command
4381 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
4382 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
4386 cp->phys.select.sel_id = sdev_id(sdev);
4387 cp->phys.select.sel_scntl3 = tp->wval;
4388 cp->phys.select.sel_sxfer = tp->sval;
4392 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
4393 cp->phys.smsg.size = cpu_to_scr(msglen);
4398 memcpy(cp->cdb_buf, cmd->cmnd, min_t(int, cmd->cmd_len, sizeof(cp->cdb_buf)));
4399 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
4400 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
4405 cp->actualquirks = 0;
4406 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
4407 cp->scsi_status = S_ILLEGAL;
4408 cp->parity_status = 0;
4410 cp->xerr_status = XE_OK;
4412 cp->sync_status = tp->sval;
4413 cp->wide_status = tp->wval;
4416 /*----------------------------------------------------
4418 ** Critical region: start this job.
4420 **----------------------------------------------------
4423 /* activate this job. */
4424 cp->magic = CCB_MAGIC;
4427 ** insert next CCBs into start queue.
4428 ** 2 max at a time is enough to flush the CCB wait queue.
4432 ncr_start_next_ccb(np, lp, 2);
4434 ncr_put_start_queue(np, cp);
4436 /* Command is successfully queued. */
4442 /*==========================================================
4445 ** Insert a CCB into the start queue and wake up the
4446 ** SCRIPTS processor.
4449 **==========================================================
4452 static void ncr_start_next_ccb(struct ncb *np, struct lcb *lp, int maxn)
4454 struct list_head *qp;
4460 while (maxn-- && lp->queuedccbs < lp->queuedepth) {
4461 qp = ncr_list_pop(&lp->wait_ccbq);
4465 cp = list_entry(qp, struct ccb, link_ccbq);
4466 list_add_tail(qp, &lp->busy_ccbq);
4467 lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
4468 cpu_to_scr(CCB_PHYS (cp, restart));
4469 ncr_put_start_queue(np, cp);
4473 static void ncr_put_start_queue(struct ncb *np, struct ccb *cp)
4478 ** insert into start queue.
4480 if (!np->squeueput) np->squeueput = 1;
4481 qidx = np->squeueput + 2;
4482 if (qidx >= MAX_START + MAX_START) qidx = 1;
4484 np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
4486 np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
4488 np->squeueput = qidx;
4492 if (DEBUG_FLAGS & DEBUG_QUEUE)
4493 printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
4496 ** Script processor may be waiting for reselect.
4500 OUTB (nc_istat, SIGP);
4504 static int ncr_reset_scsi_bus(struct ncb *np, int enab_int, int settle_delay)
4509 np->settle_time = jiffies + settle_delay * HZ;
4511 if (bootverbose > 1)
4512 printk("%s: resetting, "
4513 "command processing suspended for %d seconds\n",
4514 ncr_name(np), settle_delay);
4516 ncr_chip_reset(np, 100);
4517 udelay(2000); /* The 895 needs time for the bus mode to settle */
4519 OUTW (nc_sien, RST);
4521 ** Enable Tolerant, reset IRQD if present and
4522 ** properly set IRQ mode, prior to resetting the bus.
4524 OUTB (nc_stest3, TE);
4525 OUTB (nc_scntl1, CRST);
4528 if (!driver_setup.bus_check)
4531 ** Check for no terminators or SCSI bus shorts to ground.
4532 ** Read SCSI data bus, data parity bits and control signals.
4533 ** We are expecting RESET to be TRUE and other signals to be
4537 term = INB(nc_sstat0);
4538 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
4539 term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
4540 ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
4541 ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
4542 INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
4544 if (!(np->features & FE_WIDE))
4547 if (term != (2<<7)) {
4548 printk("%s: suspicious SCSI data while resetting the BUS.\n",
4550 printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
4551 "0x%lx, expecting 0x%lx\n",
4553 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
4554 (u_long)term, (u_long)(2<<7));
4555 if (driver_setup.bus_check == 1)
4559 OUTB (nc_scntl1, 0);
4564 * Start reset process.
4565 * If reset in progress do nothing.
4566 * The interrupt handler will reinitialize the chip.
4567 * The timeout handler will wait for settle_time before
4568 * clearing it and so resuming command processing.
4570 static void ncr_start_reset(struct ncb *np)
4572 if (!np->settle_time) {
4573 ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
4577 /*==========================================================
4580 ** Reset the SCSI BUS.
4581 ** This is called from the generic SCSI driver.
4584 **==========================================================
4586 static int ncr_reset_bus (struct ncb *np, struct scsi_cmnd *cmd, int sync_reset)
4588 /* struct scsi_device *device = cmd->device; */
4593 * Return immediately if reset is in progress.
4595 if (np->settle_time) {
4599 * Start the reset process.
4600 * The script processor is then assumed to be stopped.
4601 * Commands will now be queued in the waiting list until a settle
4602 * delay of 2 seconds will be completed.
4604 ncr_start_reset(np);
4606 * First, look in the wakeup list
4608 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4610 ** look for the ccb of this command.
4612 if (cp->host_status == HS_IDLE) continue;
4613 if (cp->cmd == cmd) {
4619 * Then, look in the waiting list
4621 if (!found && retrieve_from_waiting_list(0, np, cmd))
4624 * Wake-up all awaiting commands with DID_RESET.
4626 reset_waiting_list(np);
4628 * Wake-up all pending commands with HS_RESET -> DID_RESET.
4630 ncr_wakeup(np, HS_RESET);
4632 * If the involved command was not in a driver queue, and the
4633 * scsi driver told us reset is synchronous, and the command is not
4634 * currently in the waiting list, complete it with DID_RESET status,
4635 * in order to keep it alive.
4637 if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
4638 cmd->result = ScsiResult(DID_RESET, 0);
4639 ncr_queue_done_cmd(np, cmd);
4645 #if 0 /* unused and broken.. */
4646 /*==========================================================
4649 ** Abort an SCSI command.
4650 ** This is called from the generic SCSI driver.
4653 **==========================================================
4655 static int ncr_abort_command (struct ncb *np, struct scsi_cmnd *cmd)
4657 /* struct scsi_device *device = cmd->device; */
4663 * First, look for the scsi command in the waiting list
4665 if (remove_from_waiting_list(np, cmd)) {
4666 cmd->result = ScsiResult(DID_ABORT, 0);
4667 ncr_queue_done_cmd(np, cmd);
4668 return SCSI_ABORT_SUCCESS;
4672 * Then, look in the wakeup list
4674 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4676 ** look for the ccb of this command.
4678 if (cp->host_status == HS_IDLE) continue;
4679 if (cp->cmd == cmd) {
4686 return SCSI_ABORT_NOT_RUNNING;
4689 if (np->settle_time) {
4690 return SCSI_ABORT_SNOOZE;
4694 ** If the CCB is active, patch schedule jumps for the
4695 ** script to abort the command.
4698 switch(cp->host_status) {
4701 printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
4702 cp->start.schedule.l_paddr =
4703 cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
4704 retv = SCSI_ABORT_PENDING;
4707 cp->restart.schedule.l_paddr =
4708 cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
4709 retv = SCSI_ABORT_PENDING;
4712 retv = SCSI_ABORT_NOT_RUNNING;
4718 ** If there are no requests, the script
4719 ** processor will sleep on SEL_WAIT_RESEL.
4720 ** Let's wake it up, since it may have to work.
4722 OUTB (nc_istat, SIGP);
4728 static void ncr_detach(struct ncb *np)
4737 /* Local copy so we don't access np after freeing it! */
4738 strlcpy(inst_name, ncr_name(np), sizeof(inst_name));
4740 printk("%s: releasing host resources\n", ncr_name(np));
4743 ** Stop the ncr_timeout process
4744 ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
4747 #ifdef DEBUG_NCR53C8XX
4748 printk("%s: stopping the timer\n", ncr_name(np));
4750 np->release_stage = 1;
4751 for (i = 50 ; i && np->release_stage != 2 ; i--)
4753 if (np->release_stage != 2)
4754 printk("%s: the timer seems to be already stopped\n", ncr_name(np));
4755 else np->release_stage = 2;
4758 ** Disable chip interrupts
4761 #ifdef DEBUG_NCR53C8XX
4762 printk("%s: disabling chip interrupts\n", ncr_name(np));
4769 ** Restore bios setting for automatic clock detection.
4772 printk("%s: resetting chip\n", ncr_name(np));
4773 ncr_chip_reset(np, 100);
4775 OUTB(nc_dmode, np->sv_dmode);
4776 OUTB(nc_dcntl, np->sv_dcntl);
4777 OUTB(nc_ctest0, np->sv_ctest0);
4778 OUTB(nc_ctest3, np->sv_ctest3);
4779 OUTB(nc_ctest4, np->sv_ctest4);
4780 OUTB(nc_ctest5, np->sv_ctest5);
4781 OUTB(nc_gpcntl, np->sv_gpcntl);
4782 OUTB(nc_stest2, np->sv_stest2);
4784 ncr_selectclock(np, np->sv_scntl3);
4787 ** Free allocated ccb(s)
4790 while ((cp=np->ccb->link_ccb) != NULL) {
4791 np->ccb->link_ccb = cp->link_ccb;
4792 if (cp->host_status) {
4793 printk("%s: shall free an active ccb (host_status=%d)\n",
4794 ncr_name(np), cp->host_status);
4796 #ifdef DEBUG_NCR53C8XX
4797 printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
4799 m_free_dma(cp, sizeof(*cp), "CCB");
4802 /* Free allocated tp(s) */
4804 for (target = 0; target < MAX_TARGET ; target++) {
4805 tp=&np->target[target];
4806 for (lun = 0 ; lun < MAX_LUN ; lun++) {
4809 #ifdef DEBUG_NCR53C8XX
4810 printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
4812 if (lp->jump_ccb != &lp->jump_ccb_0)
4813 m_free_dma(lp->jump_ccb,256,"JUMP_CCB");
4814 m_free_dma(lp, sizeof(*lp), "LCB");
4820 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
4822 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
4824 m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
4825 m_free_dma(np, sizeof(struct ncb), "NCB");
4827 printk("%s: host resources successfully released\n", inst_name);
4830 /*==========================================================
4833 ** Complete execution of a SCSI command.
4834 ** Signal completion to the generic SCSI driver.
4837 **==========================================================
4840 void ncr_complete (struct ncb *np, struct ccb *cp)
4842 struct scsi_cmnd *cmd;
4850 if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
4854 ** Print minimal debug information.
4857 if (DEBUG_FLAGS & DEBUG_TINY)
4858 printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
4859 cp->host_status,cp->scsi_status);
4862 ** Get command, target and lun pointers.
4867 tp = &np->target[cmd->device->id];
4868 lp = tp->lp[cmd->device->lun];
4871 ** We donnot queue more than 1 ccb per target
4872 ** with negotiation at any time. If this ccb was
4873 ** used for negotiation, clear this info in the tcb.
4876 if (cp == tp->nego_cp)
4880 ** If auto-sense performed, change scsi status.
4882 if (cp->auto_sense) {
4883 cp->scsi_status = cp->auto_sense;
4887 ** If we were recovering from queue full or performing
4888 ** auto-sense, requeue skipped CCBs to the wait queue.
4891 if (lp && lp->held_ccb) {
4892 if (cp == lp->held_ccb) {
4893 list_splice_init(&lp->skip_ccbq, &lp->wait_ccbq);
4894 lp->held_ccb = NULL;
4899 ** Check for parity errors.
4902 if (cp->parity_status > 1) {
4903 PRINT_ADDR(cmd, "%d parity error(s).\n",cp->parity_status);
4907 ** Check for extended errors.
4910 if (cp->xerr_status != XE_OK) {
4911 switch (cp->xerr_status) {
4913 PRINT_ADDR(cmd, "extraneous data discarded.\n");
4916 PRINT_ADDR(cmd, "invalid scsi phase (4/5).\n");
4919 PRINT_ADDR(cmd, "extended error %d.\n",
4923 if (cp->host_status==HS_COMPLETE)
4924 cp->host_status = HS_FAIL;
4928 ** Print out any error for debugging purpose.
4930 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
4931 if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
4932 PRINT_ADDR(cmd, "ERROR: cmd=%x host_status=%x "
4933 "scsi_status=%x\n", cmd->cmnd[0],
4934 cp->host_status, cp->scsi_status);
4939 ** Check the status.
4941 if ( (cp->host_status == HS_COMPLETE)
4942 && (cp->scsi_status == S_GOOD ||
4943 cp->scsi_status == S_COND_MET)) {
4945 * All went well (GOOD status).
4946 * CONDITION MET status is returned on
4947 * `Pre-Fetch' or `Search data' success.
4949 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
4953 ** Could dig out the correct value for resid,
4954 ** but it would be quite complicated.
4956 /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
4959 ** Allocate the lcb if not yet.
4962 ncr_alloc_lcb (np, cmd->device->id, cmd->device->lun);
4964 tp->bytes += cp->data_len;
4968 ** If tags was reduced due to queue full,
4969 ** increase tags if 1000 good status received.
4971 if (lp && lp->usetags && lp->numtags < lp->maxtags) {
4973 if (lp->num_good >= 1000) {
4976 ncr_setup_tags (np, cmd->device);
4979 } else if ((cp->host_status == HS_COMPLETE)
4980 && (cp->scsi_status == S_CHECK_COND)) {
4982 ** Check condition code
4984 cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
4987 ** Copy back sense data to caller's buffer.
4989 memcpy(cmd->sense_buffer, cp->sense_buf,
4990 min(sizeof(cmd->sense_buffer), sizeof(cp->sense_buf)));
4992 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
4993 u_char * p = (u_char*) & cmd->sense_buffer;
4995 PRINT_ADDR(cmd, "sense data:");
4996 for (i=0; i<14; i++) printk (" %x", *p++);
4999 } else if ((cp->host_status == HS_COMPLETE)
5000 && (cp->scsi_status == S_CONFLICT)) {
5002 ** Reservation Conflict condition code
5004 cmd->result = ScsiResult(DID_OK, S_CONFLICT);
5006 } else if ((cp->host_status == HS_COMPLETE)
5007 && (cp->scsi_status == S_BUSY ||
5008 cp->scsi_status == S_QUEUE_FULL)) {
5013 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5015 } else if ((cp->host_status == HS_SEL_TIMEOUT)
5016 || (cp->host_status == HS_TIMEOUT)) {
5021 cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
5023 } else if (cp->host_status == HS_RESET) {
5028 cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
5030 } else if (cp->host_status == HS_ABORTED) {
5035 cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
5040 ** Other protocol messes
5042 PRINT_ADDR(cmd, "COMMAND FAILED (%x %x) @%p.\n",
5043 cp->host_status, cp->scsi_status, cp);
5045 cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
5052 if (tp->usrflag & UF_TRACE) {
5055 PRINT_ADDR(cmd, " CMD:");
5056 p = (u_char*) &cmd->cmnd[0];
5057 for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
5059 if (cp->host_status==HS_COMPLETE) {
5060 switch (cp->scsi_status) {
5066 p = (u_char*) &cmd->sense_buffer;
5067 for (i=0; i<14; i++)
5068 printk (" %x", *p++);
5071 printk (" STAT: %x\n", cp->scsi_status);
5074 } else printk (" HOSTERROR: %x", cp->host_status);
5081 ncr_free_ccb (np, cp);
5084 ** requeue awaiting scsi commands for this lun.
5086 if (lp && lp->queuedccbs < lp->queuedepth &&
5087 !list_empty(&lp->wait_ccbq))
5088 ncr_start_next_ccb(np, lp, 2);
5091 ** requeue awaiting scsi commands for this controller.
5093 if (np->waiting_list)
5094 requeue_waiting_list(np);
5097 ** signal completion to generic driver.
5099 ncr_queue_done_cmd(np, cmd);
5102 /*==========================================================
5105 ** Signal all (or one) control block done.
5108 **==========================================================
5112 ** This CCB has been skipped by the NCR.
5113 ** Queue it in the corresponding unit queue.
5115 static void ncr_ccb_skipped(struct ncb *np, struct ccb *cp)
5117 struct tcb *tp = &np->target[cp->target];
5118 struct lcb *lp = tp->lp[cp->lun];
5120 if (lp && cp != np->ccb) {
5121 cp->host_status &= ~HS_SKIPMASK;
5122 cp->start.schedule.l_paddr =
5123 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5124 list_move_tail(&cp->link_ccbq, &lp->skip_ccbq);
5136 ** The NCR has completed CCBs.
5137 ** Look at the DONE QUEUE if enabled, otherwise scan all CCBs
5139 void ncr_wakeup_done (struct ncb *np)
5142 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5145 i = np->ccb_done_ic;
5151 cp = np->ccb_done[j];
5152 if (!CCB_DONE_VALID(cp))
5155 np->ccb_done[j] = (struct ccb *)CCB_DONE_EMPTY;
5156 np->scripth->done_queue[5*j + 4] =
5157 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5159 np->scripth->done_queue[5*i + 4] =
5160 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5162 if (cp->host_status & HS_DONEMASK)
5163 ncr_complete (np, cp);
5164 else if (cp->host_status & HS_SKIPMASK)
5165 ncr_ccb_skipped (np, cp);
5169 np->ccb_done_ic = i;
5173 if (cp->host_status & HS_DONEMASK)
5174 ncr_complete (np, cp);
5175 else if (cp->host_status & HS_SKIPMASK)
5176 ncr_ccb_skipped (np, cp);
5183 ** Complete all active CCBs.
5185 void ncr_wakeup (struct ncb *np, u_long code)
5187 struct ccb *cp = np->ccb;
5190 if (cp->host_status != HS_IDLE) {
5191 cp->host_status = code;
5192 ncr_complete (np, cp);
5202 /* Some initialisation must be done immediately following reset, for 53c720,
5203 * at least. EA (dcntl bit 5) isn't set here as it is set once only in
5204 * the _detect function.
5206 static void ncr_chip_reset(struct ncb *np, int delay)
5208 OUTB (nc_istat, SRST);
5210 OUTB (nc_istat, 0 );
5212 if (np->features & FE_EHP)
5213 OUTB (nc_ctest0, EHP);
5214 if (np->features & FE_MUX)
5215 OUTB (nc_ctest4, MUX);
5219 /*==========================================================
5225 **==========================================================
5228 void ncr_init (struct ncb *np, int reset, char * msg, u_long code)
5233 ** Reset chip if asked, otherwise just clear fifos.
5237 OUTB (nc_istat, SRST);
5241 OUTB (nc_stest3, TE|CSF);
5242 OUTONB (nc_ctest3, CLF);
5249 if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
5252 ** Clear Start Queue
5254 np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
5255 for (i = 1; i < MAX_START + MAX_START; i += 2)
5256 np->scripth0->tryloop[i] =
5257 cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5260 ** Start at first entry.
5263 np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
5265 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5269 for (i = 0; i < MAX_DONE; i++) {
5270 np->ccb_done[i] = (struct ccb *)CCB_DONE_EMPTY;
5271 np->scripth0->done_queue[5*i + 4] =
5272 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5277 ** Start at first entry.
5279 np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
5280 np->ccb_done_ic = MAX_DONE-1;
5281 np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
5282 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5285 ** Wakeup all pending jobs.
5287 ncr_wakeup (np, code);
5294 ** Remove reset; big delay because the 895 needs time for the
5295 ** bus mode to settle
5297 ncr_chip_reset(np, 2000);
5299 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
5300 /* full arb., ena parity, par->ATN */
5301 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
5303 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
5305 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
5306 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
5307 OUTB (nc_istat , SIGP ); /* Signal Process */
5308 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
5309 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
5311 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
5312 OUTB (nc_ctest0, np->rv_ctest0); /* 720: CDIS and EHP */
5313 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
5314 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
5316 OUTB (nc_stest2, EXT|np->rv_stest2); /* Extended Sreq/Sack filtering */
5317 OUTB (nc_stest3, TE); /* TolerANT enable */
5318 OUTB (nc_stime0, 0x0c ); /* HTH disabled STO 0.25 sec */
5321 ** Disable disconnects.
5327 ** Enable GPIO0 pin for writing if LED support.
5330 if (np->features & FE_LED0) {
5331 OUTOFFB (nc_gpcntl, 0x01);
5338 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
5339 OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
5342 ** Fill in target structure.
5343 ** Reinitialize usrsync.
5344 ** Reinitialize usrwide.
5345 ** Prepare sync negotiation according to actual SCSI bus mode.
5348 for (i=0;i<MAX_TARGET;i++) {
5349 struct tcb *tp = &np->target[i];
5352 tp->wval = np->rv_scntl3;
5354 if (tp->usrsync != 255) {
5355 if (tp->usrsync <= np->maxsync) {
5356 if (tp->usrsync < np->minsync) {
5357 tp->usrsync = np->minsync;
5364 if (tp->usrwide > np->maxwide)
5365 tp->usrwide = np->maxwide;
5370 ** Start script processor.
5374 printk ("%s: Downloading SCSI SCRIPTS.\n",
5376 OUTL (nc_scratcha, vtobus(np->script0));
5377 OUTL_DSP (NCB_SCRIPTH_PHYS (np, start_ram));
5380 OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
5383 /*==========================================================
5385 ** Prepare the negotiation values for wide and
5386 ** synchronous transfers.
5388 **==========================================================
5391 static void ncr_negotiate (struct ncb* np, struct tcb* tp)
5394 ** minsync unit is 4ns !
5397 u_long minsync = tp->usrsync;
5400 ** SCSI bus mode limit
5403 if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
5404 if (minsync < 12) minsync = 12;
5411 if (minsync < np->minsync)
5412 minsync = np->minsync;
5418 if (minsync > np->maxsync)
5421 if (tp->maxoffs > np->maxoffs)
5422 tp->maxoffs = np->maxoffs;
5424 tp->minsync = minsync;
5425 tp->maxoffs = (minsync<255 ? tp->maxoffs : 0);
5428 ** period=0: has to negotiate sync transfer
5434 ** widedone=0: has to negotiate wide transfer
5439 /*==========================================================
5441 ** Get clock factor and sync divisor for a given
5442 ** synchronous factor period.
5443 ** Returns the clock factor (in sxfer) and scntl3
5444 ** synchronous divisor field.
5446 **==========================================================
5449 static void ncr_getsync(struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p)
5451 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
5452 int div = np->clock_divn; /* Number of divisors supported */
5453 u_long fak; /* Sync factor in sxfer */
5454 u_long per; /* Period in tenths of ns */
5455 u_long kpc; /* (per * clk) */
5458 ** Compute the synchronous period in tenths of nano-seconds
5460 if (sfac <= 10) per = 250;
5461 else if (sfac == 11) per = 303;
5462 else if (sfac == 12) per = 500;
5463 else per = 40 * sfac;
5466 ** Look for the greatest clock divisor that allows an
5467 ** input speed faster than the period.
5471 if (kpc >= (div_10M[div] << 2)) break;
5474 ** Calculate the lowest clock factor that allows an output
5475 ** speed not faster than the period.
5477 fak = (kpc - 1) / div_10M[div] + 1;
5479 #if 0 /* This optimization does not seem very useful */
5481 per = (fak * div_10M[div]) / clk;
5484 ** Why not to try the immediate lower divisor and to choose
5485 ** the one that allows the fastest output speed ?
5486 ** We don't want input speed too much greater than output speed.
5488 if (div >= 1 && fak < 8) {
5490 fak2 = (kpc - 1) / div_10M[div-1] + 1;
5491 per2 = (fak2 * div_10M[div-1]) / clk;
5492 if (per2 < per && fak2 <= 8) {
5500 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
5503 ** Compute and return sync parameters for the ncr
5506 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
5510 /*==========================================================
5512 ** Set actual values, sync status and patch all ccbs of
5513 ** a target according to new sync/wide agreement.
5515 **==========================================================
5518 static void ncr_set_sync_wide_status (struct ncb *np, u_char target)
5521 struct tcb *tp = &np->target[target];
5524 ** set actual value and sync_status
5526 OUTB (nc_sxfer, tp->sval);
5527 np->sync_st = tp->sval;
5528 OUTB (nc_scntl3, tp->wval);
5529 np->wide_st = tp->wval;
5532 ** patch ALL ccbs of this target.
5534 for (cp = np->ccb; cp; cp = cp->link_ccb) {
5535 if (!cp->cmd) continue;
5536 if (scmd_id(cp->cmd) != target) continue;
5538 cp->sync_status = tp->sval;
5539 cp->wide_status = tp->wval;
5541 cp->phys.select.sel_scntl3 = tp->wval;
5542 cp->phys.select.sel_sxfer = tp->sval;
5546 /*==========================================================
5548 ** Switch sync mode for current job and it's target
5550 **==========================================================
5553 static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer)
5555 struct scsi_cmnd *cmd = cp->cmd;
5557 u_char target = INB (nc_sdid) & 0x0f;
5560 BUG_ON(target != (scmd_id(cmd) & 0xf));
5562 tp = &np->target[target];
5564 if (!scntl3 || !(sxfer & 0x1f))
5565 scntl3 = np->rv_scntl3;
5566 scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
5569 ** Deduce the value of controller sync period from scntl3.
5570 ** period is in tenths of nano-seconds.
5573 idiv = ((scntl3 >> 4) & 0x7);
5574 if ((sxfer & 0x1f) && idiv)
5575 tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
5577 tp->period = 0xffff;
5579 /* Stop there if sync parameters are unchanged */
5580 if (tp->sval == sxfer && tp->wval == scntl3)
5585 if (sxfer & 0x01f) {
5586 /* Disable extended Sreq/Sack filtering */
5587 if (tp->period <= 2000)
5588 OUTOFFB(nc_stest2, EXT);
5591 spi_display_xfer_agreement(tp->starget);
5594 ** set actual value and sync_status
5595 ** patch ALL ccbs of this target.
5597 ncr_set_sync_wide_status(np, target);
5600 /*==========================================================
5602 ** Switch wide mode for current job and it's target
5603 ** SCSI specs say: a SCSI device that accepts a WDTR
5604 ** message shall reset the synchronous agreement to
5605 ** asynchronous mode.
5607 **==========================================================
5610 static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack)
5612 struct scsi_cmnd *cmd = cp->cmd;
5613 u16 target = INB (nc_sdid) & 0x0f;
5618 BUG_ON(target != (scmd_id(cmd) & 0xf));
5620 tp = &np->target[target];
5621 tp->widedone = wide+1;
5622 scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
5624 sxfer = ack ? 0 : tp->sval;
5627 ** Stop there if sync/wide parameters are unchanged
5629 if (tp->sval == sxfer && tp->wval == scntl3) return;
5634 ** Bells and whistles ;-)
5636 if (bootverbose >= 2) {
5637 dev_info(&cmd->device->sdev_target->dev, "WIDE SCSI %sabled.\n",
5638 (scntl3 & EWS) ? "en" : "dis");
5642 ** set actual value and sync_status
5643 ** patch ALL ccbs of this target.
5645 ncr_set_sync_wide_status(np, target);
5648 /*==========================================================
5650 ** Switch tagged mode for a target.
5652 **==========================================================
5655 static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
5657 unsigned char tn = sdev->id, ln = sdev->lun;
5658 struct tcb *tp = &np->target[tn];
5659 struct lcb *lp = tp->lp[ln];
5660 u_char reqtags, maxdepth;
5665 if ((!tp) || (!lp) || !sdev)
5669 ** If SCSI device queue depth is not yet set, leave here.
5671 if (!lp->scdev_depth)
5675 ** Donnot allow more tags than the SCSI driver can queue
5677 ** Donnot allow more tags than we can handle.
5679 maxdepth = lp->scdev_depth;
5680 if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
5681 if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
5682 if (lp->numtags > maxdepth) lp->numtags = maxdepth;
5685 ** only devices conformant to ANSI Version >= 2
5686 ** only devices capable of tagged commands
5687 ** only if enabled by user ..
5689 if (sdev->tagged_supported && lp->numtags > 1) {
5690 reqtags = lp->numtags;
5696 ** Update max number of tags
5698 lp->numtags = reqtags;
5699 if (lp->numtags > lp->maxtags)
5700 lp->maxtags = lp->numtags;
5703 ** If we want to switch tag mode, we must wait
5704 ** for no CCB to be active.
5706 if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
5707 if (lp->queuedepth == reqtags) /* Already announced */
5709 lp->queuedepth = reqtags;
5711 else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
5712 lp->queuedepth = reqtags;
5715 else { /* Want to switch tag mode */
5716 if (lp->busyccbs) /* If not yet safe, return */
5718 lp->queuedepth = reqtags;
5719 lp->usetags = reqtags > 1 ? 1 : 0;
5723 ** Patch the lun mini-script, according to tag mode.
5725 lp->jump_tag.l_paddr = lp->usetags?
5726 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
5727 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
5730 ** Announce change to user.
5734 dev_info(&sdev->sdev_gendev,
5735 "tagged command queue depth set to %d\n",
5738 dev_info(&sdev->sdev_gendev,
5739 "tagged command queueing disabled\n");
5744 /*==========================================================
5747 ** ncr timeout handler.
5750 **==========================================================
5752 ** Misused to keep the driver running when
5753 ** interrupts are not configured correctly.
5755 **----------------------------------------------------------
5758 static void ncr_timeout (struct ncb *np)
5760 u_long thistime = jiffies;
5763 ** If release process in progress, let's go
5764 ** Set the release stage from 1 to 2 to synchronize
5765 ** with the release process.
5768 if (np->release_stage) {
5769 if (np->release_stage == 1) np->release_stage = 2;
5773 np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
5774 add_timer(&np->timer);
5777 ** If we are resetting the ncr, wait for settle_time before
5778 ** clearing it. Then command processing will be resumed.
5780 if (np->settle_time) {
5781 if (np->settle_time <= thistime) {
5782 if (bootverbose > 1)
5783 printk("%s: command processing resumed\n", ncr_name(np));
5784 np->settle_time = 0;
5786 requeue_waiting_list(np);
5792 ** Since the generic scsi driver only allows us 0.5 second
5793 ** to perform abort of a command, we must look at ccbs about
5794 ** every 0.25 second.
5796 if (np->lasttime + 4*HZ < thistime) {
5798 ** block ncr interrupts
5800 np->lasttime = thistime;
5803 #ifdef SCSI_NCR_BROKEN_INTR
5804 if (INB(nc_istat) & (INTF|SIP|DIP)) {
5807 ** Process pending interrupts.
5809 if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
5811 if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
5813 #endif /* SCSI_NCR_BROKEN_INTR */
5816 /*==========================================================
5818 ** log message for real hard errors
5820 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
5821 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
5823 ** exception register:
5828 ** so: control lines as driver by NCR.
5829 ** si: control lines as seen by NCR.
5830 ** sd: scsi data lines as seen by NCR.
5833 ** sxfer: (see the manual)
5834 ** scntl3: (see the manual)
5836 ** current script command:
5837 ** dsp: script address (relative to start of script).
5838 ** dbc: first word of script command.
5840 ** First 16 register of the chip:
5843 **==========================================================
5846 static void ncr_log_hard_error(struct ncb *np, u16 sist, u_char dstat)
5852 u_char *script_base;
5857 if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
5858 script_ofs = dsp - np->p_script;
5859 script_size = sizeof(struct script);
5860 script_base = (u_char *) np->script0;
5861 script_name = "script";
5863 else if (np->p_scripth < dsp &&
5864 dsp <= np->p_scripth + sizeof(struct scripth)) {
5865 script_ofs = dsp - np->p_scripth;
5866 script_size = sizeof(struct scripth);
5867 script_base = (u_char *) np->scripth0;
5868 script_name = "scripth";
5873 script_name = "mem";
5876 printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
5877 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
5878 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
5879 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
5880 (unsigned)INL (nc_dbc));
5882 if (((script_ofs & 3) == 0) &&
5883 (unsigned)script_ofs < script_size) {
5884 printk ("%s: script cmd = %08x\n", ncr_name(np),
5885 scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
5888 printk ("%s: regdump:", ncr_name(np));
5890 printk (" %02x", (unsigned)INB_OFF(i));
5894 /*============================================================
5896 ** ncr chip exception handler.
5898 **============================================================
5900 ** In normal cases, interrupt conditions occur one at a
5901 ** time. The ncr is able to stack in some extra registers
5902 ** other interrupts that will occur after the first one.
5903 ** But, several interrupts may occur at the same time.
5905 ** We probably should only try to deal with the normal
5906 ** case, but it seems that multiple interrupts occur in
5907 ** some cases that are not abnormal at all.
5909 ** The most frequent interrupt condition is Phase Mismatch.
5910 ** We should want to service this interrupt quickly.
5911 ** A SCSI parity error may be delivered at the same time.
5912 ** The SIR interrupt is not very frequent in this driver,
5913 ** since the INTFLY is likely used for command completion
5915 ** The Selection Timeout interrupt may be triggered with
5917 ** The SBMC interrupt (SCSI Bus Mode Change) may probably
5918 ** occur at any time.
5920 ** This handler try to deal as cleverly as possible with all
5923 **============================================================
5926 void ncr_exception (struct ncb *np)
5928 u_char istat, dstat;
5933 ** interrupt on the fly ?
5934 ** Since the global header may be copied back to a CCB
5935 ** using a posted PCI memory write, the last operation on
5936 ** the istat register is a READ in order to flush posted
5937 ** PCI write commands.
5939 istat = INB (nc_istat);
5941 OUTB (nc_istat, (istat & SIGP) | INTF);
5942 istat = INB (nc_istat);
5943 if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
5944 ncr_wakeup_done (np);
5947 if (!(istat & (SIP|DIP)))
5951 OUTB (nc_istat, CABRT);
5954 ** Steinbach's Guideline for Systems Programming:
5955 ** Never test for an error condition you don't know how to handle.
5958 sist = (istat & SIP) ? INW (nc_sist) : 0;
5959 dstat = (istat & DIP) ? INB (nc_dstat) : 0;
5961 if (DEBUG_FLAGS & DEBUG_TINY)
5962 printk ("<%d|%x:%x|%x:%x>",
5965 (unsigned)INL(nc_dsp),
5966 (unsigned)INL(nc_dbc));
5968 /*========================================================
5969 ** First, interrupts we want to service cleanly.
5971 ** Phase mismatch is the most frequent interrupt, and
5972 ** so we have to service it as quickly and as cleanly
5974 ** Programmed interrupts are rarely used in this driver,
5975 ** but we must handle them cleanly anyway.
5976 ** We try to deal with PAR and SBMC combined with
5977 ** some other interrupt(s).
5978 **=========================================================
5981 if (!(sist & (STO|GEN|HTH|SGE|UDC|RST)) &&
5982 !(dstat & (MDPE|BF|ABRT|IID))) {
5983 if ((sist & SBMC) && ncr_int_sbmc (np))
5985 if ((sist & PAR) && ncr_int_par (np))
5996 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
5998 if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
5999 printk( "%s: unknown interrupt(s) ignored, "
6000 "ISTAT=%x DSTAT=%x SIST=%x\n",
6001 ncr_name(np), istat, dstat, sist);
6008 /*========================================================
6009 ** Now, interrupts that need some fixing up.
6010 ** Order and multiple interrupts is so less important.
6012 ** If SRST has been asserted, we just reset the chip.
6014 ** Selection is intirely handled by the chip. If the
6015 ** chip says STO, we trust it. Seems some other
6016 ** interrupts may occur at the same time (UDC, IID), so
6017 ** we ignore them. In any case we do enough fix-up
6018 ** in the service routine.
6019 ** We just exclude some fatal dma errors.
6020 **=========================================================
6024 ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
6029 !(dstat & (MDPE|BF|ABRT))) {
6031 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
6033 OUTONB (nc_ctest3, CLF);
6039 /*=========================================================
6040 ** Now, interrupts we are not able to recover cleanly.
6041 ** (At least for the moment).
6043 ** Do the register dump.
6044 ** Log message for real hard errors.
6046 ** For MDPE, BF, ABORT, IID, SGE and HTH we reset the
6047 ** BUS and the chip.
6048 ** We are more soft for UDC.
6049 **=========================================================
6052 if (time_after(jiffies, np->regtime)) {
6053 np->regtime = jiffies + 10*HZ;
6054 for (i = 0; i<sizeof(np->regdump); i++)
6055 ((char*)&np->regdump)[i] = INB_OFF(i);
6056 np->regdump.nc_dstat = dstat;
6057 np->regdump.nc_sist = sist;
6060 ncr_log_hard_error(np, sist, dstat);
6062 printk ("%s: have to clear fifos.\n", ncr_name (np));
6063 OUTB (nc_stest3, TE|CSF);
6064 OUTONB (nc_ctest3, CLF);
6066 if ((sist & (SGE)) ||
6067 (dstat & (MDPE|BF|ABRT|IID))) {
6068 ncr_start_reset(np);
6073 printk ("%s: handshake timeout\n", ncr_name(np));
6074 ncr_start_reset(np);
6079 printk ("%s: unexpected disconnect\n", ncr_name(np));
6080 OUTB (HS_PRT, HS_UNEXPECTED);
6081 OUTL_DSP (NCB_SCRIPT_PHYS (np, cleanup));
6085 /*=========================================================
6086 ** We just miss the cause of the interrupt. :(
6087 ** Print a message. The timeout will do the real work.
6088 **=========================================================
6090 printk ("%s: unknown interrupt\n", ncr_name(np));
6093 /*==========================================================
6095 ** ncr chip exception handler for selection timeout
6097 **==========================================================
6099 ** There seems to be a bug in the 53c810.
6100 ** Although a STO-Interrupt is pending,
6101 ** it continues executing script commands.
6102 ** But it will fail and interrupt (IID) on
6103 ** the next instruction where it's looking
6104 ** for a valid phase.
6106 **----------------------------------------------------------
6109 void ncr_int_sto (struct ncb *np)
6113 if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
6116 ** look for ccb and set the status.
6121 while (cp && (CCB_PHYS (cp, phys) != dsa))
6125 cp-> host_status = HS_SEL_TIMEOUT;
6126 ncr_complete (np, cp);
6130 ** repair start queue and jump to start point.
6133 OUTL_DSP (NCB_SCRIPTH_PHYS (np, sto_restart));
6137 /*==========================================================
6139 ** ncr chip exception handler for SCSI bus mode change
6141 **==========================================================
6143 ** spi2-r12 11.2.3 says a transceiver mode change must
6144 ** generate a reset event and a device that detects a reset
6145 ** event shall initiate a hard reset. It says also that a
6146 ** device that detects a mode change shall set data transfer
6147 ** mode to eight bit asynchronous, etc...
6148 ** So, just resetting should be enough.
6151 **----------------------------------------------------------
6154 static int ncr_int_sbmc (struct ncb *np)
6156 u_char scsi_mode = INB (nc_stest4) & SMODE;
6158 if (scsi_mode != np->scsi_mode) {
6159 printk("%s: SCSI bus mode change from %x to %x.\n",
6160 ncr_name(np), np->scsi_mode, scsi_mode);
6162 np->scsi_mode = scsi_mode;
6166 ** Suspend command processing for 1 second and
6167 ** reinitialize all except the chip.
6169 np->settle_time = jiffies + HZ;
6170 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
6176 /*==========================================================
6178 ** ncr chip exception handler for SCSI parity error.
6180 **==========================================================
6183 **----------------------------------------------------------
6186 static int ncr_int_par (struct ncb *np)
6188 u_char hsts = INB (HS_PRT);
6189 u32 dbc = INL (nc_dbc);
6190 u_char sstat1 = INB (nc_sstat1);
6195 printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
6196 ncr_name(np), hsts, dbc, sstat1);
6199 * Ignore the interrupt if the NCR is not connected
6200 * to the SCSI bus, since the right work should have
6201 * been done on unexpected disconnection handling.
6203 if (!(INB (nc_scntl1) & ISCON))
6207 * If the nexus is not clearly identified, reset the bus.
6208 * We will try to do better later.
6210 if (hsts & HS_INVALMASK)
6214 * If the SCSI parity error occurs in MSG IN phase, prepare a
6215 * MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
6216 * ERROR message and let the device decide to retry the command
6217 * or to terminate with check condition. If we were in MSG IN
6218 * phase waiting for the response of a negotiation, we will
6219 * get SIR_NEGO_FAILED at dispatch.
6221 if (!(dbc & 0xc0000000))
6222 phase = (dbc >> 24) & 7;
6224 msg = MSG_PARITY_ERROR;
6226 msg = INITIATOR_ERROR;
6230 * If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
6231 * script that will ignore all data in bytes until phase
6232 * change, since we are not sure the chip will wait the phase
6233 * change prior to delivering the interrupt.
6236 jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
6238 jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
6240 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
6241 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
6243 np->msgout[0] = msg;
6248 ncr_start_reset(np);
6252 /*==========================================================
6255 ** ncr chip exception handler for phase errors.
6258 **==========================================================
6260 ** We have to construct a new transfer descriptor,
6261 ** to transfer the rest of the current block.
6263 **----------------------------------------------------------
6266 static void ncr_int_ma (struct ncb *np)
6283 sbcl = INB (nc_sbcl);
6286 rest = dbc & 0xffffff;
6289 ** Take into account dma fifo and various buffers and latches,
6290 ** only if the interrupted phase is an OUTPUT phase.
6293 if ((cmd & 1) == 0) {
6294 u_char ctest5, ss0, ss2;
6297 ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
6299 delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
6301 delta=(INB (nc_dfifo) - rest) & 0x7f;
6304 ** The data in the dma fifo has not been transferred to
6305 ** the target -> add the amount to the rest
6306 ** and clear the data.
6307 ** Check the sstat2 register in case of wide transfer.
6311 ss0 = INB (nc_sstat0);
6312 if (ss0 & OLF) rest++;
6313 if (ss0 & ORF) rest++;
6314 if (INB(nc_scntl3) & EWS) {
6315 ss2 = INB (nc_sstat2);
6316 if (ss2 & OLF1) rest++;
6317 if (ss2 & ORF1) rest++;
6320 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6321 printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
6322 (unsigned) rest, (unsigned) delta, ss0);
6325 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6326 printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
6332 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
6333 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
6336 ** locate matching cp.
6337 ** if the interrupted phase is DATA IN or DATA OUT,
6338 ** trust the global header.
6343 if (CCB_PHYS(cp, phys) != dsa)
6347 while (cp && (CCB_PHYS (cp, phys) != dsa))
6352 ** try to find the interrupted script command,
6353 ** and the address at which to continue.
6357 if (dsp > np->p_script &&
6358 dsp <= np->p_script + sizeof(struct script)) {
6359 vdsp = (u32 *)((char*)np->script0 + (dsp-np->p_script-8));
6362 else if (dsp > np->p_scripth &&
6363 dsp <= np->p_scripth + sizeof(struct scripth)) {
6364 vdsp = (u32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
6368 if (dsp == CCB_PHYS (cp, patch[2])) {
6369 vdsp = &cp->patch[0];
6370 nxtdsp = scr_to_cpu(vdsp[3]);
6372 else if (dsp == CCB_PHYS (cp, patch[6])) {
6373 vdsp = &cp->patch[4];
6374 nxtdsp = scr_to_cpu(vdsp[3]);
6379 ** log the information
6382 if (DEBUG_FLAGS & DEBUG_PHASE) {
6383 printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
6386 (unsigned)nxtdsp, vdsp, cmd);
6390 ** cp=0 means that the DSA does not point to a valid control
6391 ** block. This should not happen since we donnot use multi-byte
6392 ** move while we are being reselected ot after command complete.
6393 ** We are not able to recover from such a phase error.
6396 printk ("%s: SCSI phase error fixup: "
6397 "CCB already dequeued (0x%08lx)\n",
6398 ncr_name (np), (u_long) np->header.cp);
6403 ** get old startaddress and old length.
6406 oadr = scr_to_cpu(vdsp[1]);
6408 if (cmd & 0x10) { /* Table indirect */
6409 tblp = (u32 *) ((char*) &cp->phys + oadr);
6410 olen = scr_to_cpu(tblp[0]);
6411 oadr = scr_to_cpu(tblp[1]);
6414 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
6417 if (DEBUG_FLAGS & DEBUG_PHASE) {
6418 printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
6419 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
6426 ** check cmd against assumed interrupted script command.
6429 if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
6430 PRINT_ADDR(cp->cmd, "internal error: cmd=%02x != %02x=(vdsp[0] "
6431 ">> 24)\n", cmd, scr_to_cpu(vdsp[0]) >> 24);
6437 ** cp != np->header.cp means that the header of the CCB
6438 ** currently being processed has not yet been copied to
6439 ** the global header area. That may happen if the device did
6440 ** not accept all our messages after having been selected.
6442 if (cp != np->header.cp) {
6443 printk ("%s: SCSI phase error fixup: "
6444 "CCB address mismatch (0x%08lx != 0x%08lx)\n",
6445 ncr_name (np), (u_long) cp, (u_long) np->header.cp);
6449 ** if old phase not dataphase, leave here.
6453 PRINT_ADDR(cp->cmd, "phase change %x-%x %d@%08x resid=%d.\n",
6454 cmd&7, sbcl&7, (unsigned)olen,
6455 (unsigned)oadr, (unsigned)rest);
6456 goto unexpected_phase;
6460 ** choose the correct patch area.
6461 ** if savep points to one, choose the other.
6465 newtmp = CCB_PHYS (cp, patch);
6466 if (newtmp == scr_to_cpu(cp->phys.header.savep)) {
6467 newcmd = &cp->patch[4];
6468 newtmp = CCB_PHYS (cp, patch[4]);
6472 ** fillin the commands
6475 newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
6476 newcmd[1] = cpu_to_scr(oadr + olen - rest);
6477 newcmd[2] = cpu_to_scr(SCR_JUMP);
6478 newcmd[3] = cpu_to_scr(nxtdsp);
6480 if (DEBUG_FLAGS & DEBUG_PHASE) {
6481 PRINT_ADDR(cp->cmd, "newcmd[%d] %x %x %x %x.\n",
6482 (int) (newcmd - cp->patch),
6483 (unsigned)scr_to_cpu(newcmd[0]),
6484 (unsigned)scr_to_cpu(newcmd[1]),
6485 (unsigned)scr_to_cpu(newcmd[2]),
6486 (unsigned)scr_to_cpu(newcmd[3]));
6489 ** fake the return address (to the patch).
6490 ** and restart script processor at dispatcher.
6492 OUTL (nc_temp, newtmp);
6493 OUTL_DSP (NCB_SCRIPT_PHYS (np, dispatch));
6497 ** Unexpected phase changes that occurs when the current phase
6498 ** is not a DATA IN or DATA OUT phase are due to error conditions.
6499 ** Such event may only happen when the SCRIPTS is using a
6500 ** multibyte SCSI MOVE.
6502 ** Phase change Some possible cause
6504 ** COMMAND --> MSG IN SCSI parity error detected by target.
6505 ** COMMAND --> STATUS Bad command or refused by target.
6506 ** MSG OUT --> MSG IN Message rejected by target.
6507 ** MSG OUT --> COMMAND Bogus target that discards extended
6508 ** negotiation messages.
6510 ** The code below does not care of the new phase and so
6511 ** trusts the target. Why to annoy it ?
6512 ** If the interrupted phase is COMMAND phase, we restart at
6514 ** If a target does not get all the messages after selection,
6515 ** the code assumes blindly that the target discards extended
6516 ** messages and clears the negotiation status.
6517 ** If the target does not want all our response to negotiation,
6518 ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
6519 ** bloat for such a should_not_happen situation).
6520 ** In all other situation, we reset the BUS.
6521 ** Are these assumptions reasonnable ? (Wait and see ...)
6528 case 2: /* COMMAND phase */
6529 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
6532 case 3: /* STATUS phase */
6533 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
6536 case 6: /* MSG OUT phase */
6537 np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
6538 if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
6539 cp->host_status = HS_BUSY;
6540 nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
6542 else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
6543 dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
6544 nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
6548 case 7: /* MSG IN phase */
6549 nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
6560 ncr_start_reset(np);
6564 static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
6566 struct scsi_cmnd *cmd = cp->cmd;
6567 struct tcb *tp = &np->target[cmd->device->id];
6568 struct lcb *lp = tp->lp[cmd->device->lun];
6569 struct list_head *qp;
6574 u_char s_status = INB (SS_PRT);
6577 ** Let the SCRIPTS processor skip all not yet started CCBs,
6578 ** and count disconnected CCBs. Since the busy queue is in
6579 ** the same order as the chip start queue, disconnected CCBs
6580 ** are before cp and busy ones after.
6583 qp = lp->busy_ccbq.prev;
6584 while (qp != &lp->busy_ccbq) {
6585 cp2 = list_entry(qp, struct ccb, link_ccbq);
6590 cp2->start.schedule.l_paddr =
6591 cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
6593 lp->held_ccb = cp; /* Requeue when this one completes */
6594 disc_cnt = lp->queuedccbs - busy_cnt;
6598 default: /* Just for safety, should never happen */
6601 ** Decrease number of tags to the number of
6602 ** disconnected commands.
6606 if (bootverbose >= 1) {
6607 PRINT_ADDR(cmd, "QUEUE FULL! %d busy, %d disconnected "
6608 "CCBs\n", busy_cnt, disc_cnt);
6610 if (disc_cnt < lp->numtags) {
6611 lp->numtags = disc_cnt > 2 ? disc_cnt : 2;
6613 ncr_setup_tags (np, cmd->device);
6616 ** Requeue the command to the start queue.
6617 ** If any disconnected commands,
6619 ** Jump to reselect.
6621 cp->phys.header.savep = cp->startp;
6622 cp->host_status = HS_BUSY;
6623 cp->scsi_status = S_ILLEGAL;
6625 ncr_put_start_queue(np, cp);
6627 INB (nc_ctest2); /* Clear SIGP */
6628 OUTL_DSP (NCB_SCRIPT_PHYS (np, reselect));
6633 ** If we were requesting sense, give up.
6639 ** Device returned CHECK CONDITION status.
6640 ** Prepare all needed data strutures for getting
6645 cp->scsi_smsg2[0] = IDENTIFY(0, cmd->device->lun);
6646 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
6647 cp->phys.smsg.size = cpu_to_scr(1);
6652 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
6653 cp->phys.cmd.size = cpu_to_scr(6);
6656 ** patch requested size into sense command
6658 cp->sensecmd[0] = 0x03;
6659 cp->sensecmd[1] = cmd->device->lun << 5;
6660 cp->sensecmd[4] = sizeof(cp->sense_buf);
6665 memset(cp->sense_buf, 0, sizeof(cp->sense_buf));
6666 cp->phys.sense.addr = cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
6667 cp->phys.sense.size = cpu_to_scr(sizeof(cp->sense_buf));
6670 ** requeue the command.
6672 startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
6674 cp->phys.header.savep = startp;
6675 cp->phys.header.goalp = startp + 24;
6676 cp->phys.header.lastp = startp;
6677 cp->phys.header.wgoalp = startp + 24;
6678 cp->phys.header.wlastp = startp;
6680 cp->host_status = HS_BUSY;
6681 cp->scsi_status = S_ILLEGAL;
6682 cp->auto_sense = s_status;
6684 cp->start.schedule.l_paddr =
6685 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
6688 ** Select without ATN for quirky devices.
6690 if (cmd->device->select_no_atn)
6691 cp->start.schedule.l_paddr =
6692 cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
6694 ncr_put_start_queue(np, cp);
6696 OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
6706 /*==========================================================
6709 ** ncr chip exception handler for programmed interrupts.
6712 **==========================================================
6715 void ncr_int_sir (struct ncb *np)
6718 u_char chg, ofs, per, fak, wide;
6719 u_char num = INB (nc_dsps);
6720 struct ccb *cp=NULL;
6721 u_long dsa = INL (nc_dsa);
6722 u_char target = INB (nc_sdid) & 0x0f;
6723 struct tcb *tp = &np->target[target];
6724 struct scsi_target *starget = tp->starget;
6726 if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
6731 ** This is used for HP Zalon/53c720 where INTFLY
6732 ** operation is currently broken.
6734 ncr_wakeup_done(np);
6735 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
6736 OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, done_end) + 8);
6738 OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, start));
6741 case SIR_RESEL_NO_MSG_IN:
6742 case SIR_RESEL_NO_IDENTIFY:
6744 ** If devices reselecting without sending an IDENTIFY
6745 ** message still exist, this should help.
6746 ** We just assume lun=0, 1 CCB, no tag.
6749 OUTL_DSP (scr_to_cpu(tp->lp[0]->jump_ccb[0]));
6752 case SIR_RESEL_BAD_TARGET: /* Will send a TARGET RESET message */
6753 case SIR_RESEL_BAD_LUN: /* Will send a TARGET RESET message */
6754 case SIR_RESEL_BAD_I_T_L_Q: /* Will send an ABORT TAG message */
6755 case SIR_RESEL_BAD_I_T_L: /* Will send an ABORT message */
6756 printk ("%s:%d: SIR %d, "
6757 "incorrect nexus identification on reselection\n",
6758 ncr_name (np), target, num);
6760 case SIR_DONE_OVERFLOW:
6761 printk ("%s:%d: SIR %d, "
6762 "CCB done queue overflow\n",
6763 ncr_name (np), target, num);
6765 case SIR_BAD_STATUS:
6767 if (!cp || CCB_PHYS (cp, phys) != dsa)
6769 ncr_sir_to_redo(np, num, cp);
6776 while (cp && (CCB_PHYS (cp, phys) != dsa))
6780 BUG_ON(cp != np->header.cp);
6782 if (!cp || cp != np->header.cp)
6787 /*-----------------------------------------------------------------------------
6789 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
6790 ** ("Everything you've always wanted to know about transfer mode
6793 ** We try to negotiate sync and wide transfer only after
6794 ** a successful inquire command. We look at byte 7 of the
6795 ** inquire data to determine the capabilities of the target.
6797 ** When we try to negotiate, we append the negotiation message
6798 ** to the identify and (maybe) simple tag message.
6799 ** The host status field is set to HS_NEGOTIATE to mark this
6802 ** If the target doesn't answer this message immediately
6803 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
6804 ** will be raised eventually.
6805 ** The handler removes the HS_NEGOTIATE status, and sets the
6806 ** negotiated value to the default (async / nowide).
6808 ** If we receive a matching answer immediately, we check it
6809 ** for validity, and set the values.
6811 ** If we receive a Reject message immediately, we assume the
6812 ** negotiation has failed, and fall back to standard values.
6814 ** If we receive a negotiation message while not in HS_NEGOTIATE
6815 ** state, it's a target initiated negotiation. We prepare a
6816 ** (hopefully) valid answer, set our parameters, and send back
6817 ** this answer to the target.
6819 ** If the target doesn't fetch the answer (no message out phase),
6820 ** we assume the negotiation has failed, and fall back to default
6823 ** When we set the values, we adjust them in all ccbs belonging
6824 ** to this target, in the controller's register, and in the "phys"
6825 ** field of the controller's struct ncb.
6827 ** Possible cases: hs sir msg_in value send goto
6828 ** We try to negotiate:
6829 ** -> target doesn't msgin NEG FAIL noop defa. - dispatch
6830 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
6831 ** -> target answered (ok) NEG SYNC sdtr set - clrack
6832 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
6833 ** -> target answered (ok) NEG WIDE wdtr set - clrack
6834 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
6835 ** -> any other msgin NEG FAIL noop defa. - dispatch
6837 ** Target tries to negotiate:
6838 ** -> incoming message --- SYNC sdtr set SDTR -
6839 ** -> incoming message --- WIDE wdtr set WDTR -
6840 ** We sent our answer:
6841 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
6843 **-----------------------------------------------------------------------------
6846 case SIR_NEGO_FAILED:
6847 /*-------------------------------------------------------
6849 ** Negotiation failed.
6850 ** Target doesn't send an answer message,
6851 ** or target rejected our message.
6853 ** Remove negotiation request.
6855 **-------------------------------------------------------
6857 OUTB (HS_PRT, HS_BUSY);
6861 case SIR_NEGO_PROTO:
6862 /*-------------------------------------------------------
6864 ** Negotiation failed.
6865 ** Target doesn't fetch the answer message.
6867 **-------------------------------------------------------
6870 if (DEBUG_FLAGS & DEBUG_NEGO) {
6871 PRINT_ADDR(cp->cmd, "negotiation failed sir=%x "
6872 "status=%x.\n", num, cp->nego_status);
6876 ** any error in negotiation:
6877 ** fall back to default mode.
6879 switch (cp->nego_status) {
6882 spi_period(starget) = 0;
6883 spi_offset(starget) = 0;
6884 ncr_setsync (np, cp, 0, 0xe0);
6888 spi_width(starget) = 0;
6889 ncr_setwide (np, cp, 0, 0);
6893 np->msgin [0] = NOP;
6894 np->msgout[0] = NOP;
6895 cp->nego_status = 0;
6899 if (DEBUG_FLAGS & DEBUG_NEGO) {
6900 ncr_print_msg(cp, "sync msgin", np->msgin);
6906 if (ofs==0) per=255;
6909 ** if target sends SDTR message,
6910 ** it CAN transfer synch.
6914 spi_support_sync(starget) = 1;
6917 ** check values against driver limits.
6920 if (per < np->minsync)
6921 {chg = 1; per = np->minsync;}
6922 if (per < tp->minsync)
6923 {chg = 1; per = tp->minsync;}
6924 if (ofs > tp->maxoffs)
6925 {chg = 1; ofs = tp->maxoffs;}
6928 ** Check against controller limits.
6933 ncr_getsync(np, per, &fak, &scntl3);
6946 if (DEBUG_FLAGS & DEBUG_NEGO) {
6947 PRINT_ADDR(cp->cmd, "sync: per=%d scntl3=0x%x ofs=%d "
6948 "fak=%d chg=%d.\n", per, scntl3, ofs, fak, chg);
6951 if (INB (HS_PRT) == HS_NEGOTIATE) {
6952 OUTB (HS_PRT, HS_BUSY);
6953 switch (cp->nego_status) {
6956 /* This was an answer message */
6958 /* Answer wasn't acceptable. */
6959 spi_period(starget) = 0;
6960 spi_offset(starget) = 0;
6961 ncr_setsync(np, cp, 0, 0xe0);
6962 OUTL_DSP(NCB_SCRIPT_PHYS (np, msg_bad));
6965 spi_period(starget) = per;
6966 spi_offset(starget) = ofs;
6967 ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
6968 OUTL_DSP(NCB_SCRIPT_PHYS (np, clrack));
6973 spi_width(starget) = 0;
6974 ncr_setwide(np, cp, 0, 0);
6980 ** It was a request. Set value and
6981 ** prepare an answer message
6984 spi_period(starget) = per;
6985 spi_offset(starget) = ofs;
6986 ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
6988 spi_populate_sync_msg(np->msgout, per, ofs);
6989 cp->nego_status = NS_SYNC;
6991 if (DEBUG_FLAGS & DEBUG_NEGO) {
6992 ncr_print_msg(cp, "sync msgout", np->msgout);
6996 OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
6999 np->msgin [0] = NOP;
7005 ** Wide request message received.
7007 if (DEBUG_FLAGS & DEBUG_NEGO) {
7008 ncr_print_msg(cp, "wide msgin", np->msgin);
7012 ** get requested values.
7016 wide = np->msgin[3];
7019 ** if target sends WDTR message,
7020 ** it CAN transfer wide.
7023 if (wide && starget)
7024 spi_support_wide(starget) = 1;
7027 ** check values against driver limits.
7030 if (wide > tp->usrwide)
7031 {chg = 1; wide = tp->usrwide;}
7033 if (DEBUG_FLAGS & DEBUG_NEGO) {
7034 PRINT_ADDR(cp->cmd, "wide: wide=%d chg=%d.\n", wide,
7038 if (INB (HS_PRT) == HS_NEGOTIATE) {
7039 OUTB (HS_PRT, HS_BUSY);
7040 switch (cp->nego_status) {
7044 ** This was an answer message
7047 /* Answer wasn't acceptable. */
7048 spi_width(starget) = 0;
7049 ncr_setwide(np, cp, 0, 1);
7050 OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
7053 spi_width(starget) = wide;
7054 ncr_setwide(np, cp, wide, 1);
7055 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
7060 spi_period(starget) = 0;
7061 spi_offset(starget) = 0;
7062 ncr_setsync(np, cp, 0, 0xe0);
7068 ** It was a request, set value and
7069 ** prepare an answer message
7072 spi_width(starget) = wide;
7073 ncr_setwide(np, cp, wide, 1);
7074 spi_populate_width_msg(np->msgout, wide);
7076 np->msgin [0] = NOP;
7078 cp->nego_status = NS_WIDE;
7080 if (DEBUG_FLAGS & DEBUG_NEGO) {
7081 ncr_print_msg(cp, "wide msgout", np->msgin);
7085 /*--------------------------------------------------------------------
7087 ** Processing of special messages
7089 **--------------------------------------------------------------------
7092 case SIR_REJECT_RECEIVED:
7093 /*-----------------------------------------------
7095 ** We received a MESSAGE_REJECT.
7097 **-----------------------------------------------
7100 PRINT_ADDR(cp->cmd, "MESSAGE_REJECT received (%x:%x).\n",
7101 (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
7104 case SIR_REJECT_SENT:
7105 /*-----------------------------------------------
7107 ** We received an unknown message
7109 **-----------------------------------------------
7112 ncr_print_msg(cp, "MESSAGE_REJECT sent for", np->msgin);
7115 /*--------------------------------------------------------------------
7117 ** Processing of special messages
7119 **--------------------------------------------------------------------
7122 case SIR_IGN_RESIDUE:
7123 /*-----------------------------------------------
7125 ** We received an IGNORE RESIDUE message,
7126 ** which couldn't be handled by the script.
7128 **-----------------------------------------------
7131 PRINT_ADDR(cp->cmd, "IGNORE_WIDE_RESIDUE received, but not yet "
7135 case SIR_MISSING_SAVE:
7136 /*-----------------------------------------------
7138 ** We received an DISCONNECT message,
7139 ** but the datapointer wasn't saved before.
7141 **-----------------------------------------------
7144 PRINT_ADDR(cp->cmd, "DISCONNECT received, but datapointer "
7145 "not saved: data=%x save=%x goal=%x.\n",
7146 (unsigned) INL (nc_temp),
7147 (unsigned) scr_to_cpu(np->header.savep),
7148 (unsigned) scr_to_cpu(np->header.goalp));
7157 /*==========================================================
7160 ** Acquire a control block
7163 **==========================================================
7166 static struct ccb *ncr_get_ccb(struct ncb *np, struct scsi_cmnd *cmd)
7168 u_char tn = cmd->device->id;
7169 u_char ln = cmd->device->lun;
7170 struct tcb *tp = &np->target[tn];
7171 struct lcb *lp = tp->lp[ln];
7172 u_char tag = NO_TAG;
7173 struct ccb *cp = NULL;
7176 ** Lun structure available ?
7179 struct list_head *qp;
7181 ** Keep from using more tags than we can handle.
7183 if (lp->usetags && lp->busyccbs >= lp->maxnxs)
7187 ** Allocate a new CCB if needed.
7189 if (list_empty(&lp->free_ccbq))
7190 ncr_alloc_ccb(np, tn, ln);
7193 ** Look for free CCB
7195 qp = ncr_list_pop(&lp->free_ccbq);
7197 cp = list_entry(qp, struct ccb, link_ccbq);
7199 PRINT_ADDR(cmd, "ccb free list corrupted "
7203 list_add_tail(qp, &lp->wait_ccbq);
7209 ** If a CCB is available,
7210 ** Get a tag for this nexus if required.
7214 tag = lp->cb_tags[lp->ia_tag];
7216 else if (lp->actccbs > 0)
7221 ** if nothing available, take the default.
7227 ** Wait until available.
7231 if (flags & SCSI_NOSLEEP) break;
7232 if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
7243 ** Move to next available tag if tag used.
7246 if (tag != NO_TAG) {
7248 if (lp->ia_tag == MAX_TAGS)
7250 lp->tags_umap |= (((tagmap_t) 1) << tag);
7255 ** Remember all informations needed to free this CCB.
7261 if (DEBUG_FLAGS & DEBUG_TAGS) {
7262 PRINT_ADDR(cmd, "ccb @%p using tag %d.\n", cp, tag);
7268 /*==========================================================
7271 ** Release one control block
7274 **==========================================================
7277 static void ncr_free_ccb (struct ncb *np, struct ccb *cp)
7279 struct tcb *tp = &np->target[cp->target];
7280 struct lcb *lp = tp->lp[cp->lun];
7282 if (DEBUG_FLAGS & DEBUG_TAGS) {
7283 PRINT_ADDR(cp->cmd, "ccb @%p freeing tag %d.\n", cp, cp->tag);
7287 ** If lun control block available,
7288 ** decrement active commands and increment credit,
7289 ** free the tag if any and remove the JUMP for reselect.
7292 if (cp->tag != NO_TAG) {
7293 lp->cb_tags[lp->if_tag++] = cp->tag;
7294 if (lp->if_tag == MAX_TAGS)
7296 lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
7297 lp->tags_smap &= lp->tags_umap;
7298 lp->jump_ccb[cp->tag] =
7299 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
7302 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
7307 ** Make this CCB available.
7312 list_move(&cp->link_ccbq, &lp->free_ccbq);
7318 cp -> host_status = HS_IDLE;
7327 wakeup ((caddr_t) cp);
7332 #define ncr_reg_bus_addr(r) (np->paddr + offsetof (struct ncr_reg, r))
7334 /*------------------------------------------------------------------------
7335 ** Initialize the fixed part of a CCB structure.
7336 **------------------------------------------------------------------------
7337 **------------------------------------------------------------------------
7339 static void ncr_init_ccb(struct ncb *np, struct ccb *cp)
7341 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7344 ** Remember virtual and bus address of this ccb.
7346 cp->p_ccb = vtobus(cp);
7347 cp->phys.header.cp = cp;
7350 ** This allows list_del to work for the default ccb.
7352 INIT_LIST_HEAD(&cp->link_ccbq);
7355 ** Initialyze the start and restart launch script.
7357 ** COPY(4) @(...p_phys), @(dsa)
7358 ** JUMP @(sched_point)
7360 cp->start.setup_dsa[0] = cpu_to_scr(copy_4);
7361 cp->start.setup_dsa[1] = cpu_to_scr(CCB_PHYS(cp, start.p_phys));
7362 cp->start.setup_dsa[2] = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
7363 cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
7364 cp->start.p_phys = cpu_to_scr(CCB_PHYS(cp, phys));
7366 memcpy(&cp->restart, &cp->start, sizeof(cp->restart));
7368 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
7369 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
7373 /*------------------------------------------------------------------------
7374 ** Allocate a CCB and initialize its fixed part.
7375 **------------------------------------------------------------------------
7376 **------------------------------------------------------------------------
7378 static void ncr_alloc_ccb(struct ncb *np, u_char tn, u_char ln)
7380 struct tcb *tp = &np->target[tn];
7381 struct lcb *lp = tp->lp[ln];
7382 struct ccb *cp = NULL;
7385 ** Allocate memory for this CCB.
7387 cp = m_calloc_dma(sizeof(struct ccb), "CCB");
7392 ** Count it and initialyze it.
7396 memset(cp, 0, sizeof (*cp));
7397 ncr_init_ccb(np, cp);
7400 ** Chain into wakeup list and free ccb queue and take it
7401 ** into account for tagged commands.
7403 cp->link_ccb = np->ccb->link_ccb;
7404 np->ccb->link_ccb = cp;
7406 list_add(&cp->link_ccbq, &lp->free_ccbq);
7409 /*==========================================================
7412 ** Allocation of resources for Targets/Luns/Tags.
7415 **==========================================================
7419 /*------------------------------------------------------------------------
7420 ** Target control block initialisation.
7421 **------------------------------------------------------------------------
7422 ** This data structure is fully initialized after a SCSI command
7423 ** has been successfully completed for this target.
7424 ** It contains a SCRIPT that is called on target reselection.
7425 **------------------------------------------------------------------------
7427 static void ncr_init_tcb (struct ncb *np, u_char tn)
7429 struct tcb *tp = &np->target[tn];
7430 ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
7435 ** Jump to next tcb if SFBR does not match this target.
7436 ** JUMP IF (SFBR != #target#), @(next tcb)
7438 tp->jump_tcb.l_cmd =
7439 cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
7440 tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
7443 ** Load the synchronous transfer register.
7444 ** COPY @(tp->sval), @(sxfer)
7446 tp->getscr[0] = cpu_to_scr(copy_1);
7447 tp->getscr[1] = cpu_to_scr(vtobus (&tp->sval));
7448 #ifdef SCSI_NCR_BIG_ENDIAN
7449 tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer) ^ 3);
7451 tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
7455 ** Load the timing register.
7456 ** COPY @(tp->wval), @(scntl3)
7458 tp->getscr[3] = cpu_to_scr(copy_1);
7459 tp->getscr[4] = cpu_to_scr(vtobus (&tp->wval));
7460 #ifdef SCSI_NCR_BIG_ENDIAN
7461 tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3) ^ 3);
7463 tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
7467 ** Get the IDENTIFY message and the lun.
7468 ** CALL @script(resel_lun)
7470 tp->call_lun.l_cmd = cpu_to_scr(SCR_CALL);
7471 tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
7474 ** Look for the lun control block of this nexus.
7476 ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
7478 for (i = 0 ; i < 4 ; i++) {
7479 tp->jump_lcb[i].l_cmd =
7480 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
7481 tp->jump_lcb[i].l_paddr =
7482 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
7486 ** Link this target control block to the JUMP chain.
7488 np->jump_tcb[th].l_paddr = cpu_to_scr(vtobus (&tp->jump_tcb));
7491 ** These assert's should be moved at driver initialisations.
7493 #ifdef SCSI_NCR_BIG_ENDIAN
7494 BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
7495 offsetof(struct tcb , sval )) &3) != 3);
7496 BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
7497 offsetof(struct tcb , wval )) &3) != 3);
7499 BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
7500 offsetof(struct tcb , sval )) &3) != 0);
7501 BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
7502 offsetof(struct tcb , wval )) &3) != 0);
7507 /*------------------------------------------------------------------------
7508 ** Lun control block allocation and initialization.
7509 **------------------------------------------------------------------------
7510 ** This data structure is allocated and initialized after a SCSI
7511 ** command has been successfully completed for this target/lun.
7512 **------------------------------------------------------------------------
7514 static struct lcb *ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln)
7516 struct tcb *tp = &np->target[tn];
7517 struct lcb *lp = tp->lp[ln];
7518 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7522 ** Already done, return.
7528 ** Allocate the lcb.
7530 lp = m_calloc_dma(sizeof(struct lcb), "LCB");
7533 memset(lp, 0, sizeof(*lp));
7537 ** Initialize the target control block if not yet.
7539 if (!tp->jump_tcb.l_cmd)
7540 ncr_init_tcb(np, tn);
7543 ** Initialize the CCB queue headers.
7545 INIT_LIST_HEAD(&lp->free_ccbq);
7546 INIT_LIST_HEAD(&lp->busy_ccbq);
7547 INIT_LIST_HEAD(&lp->wait_ccbq);
7548 INIT_LIST_HEAD(&lp->skip_ccbq);
7551 ** Set max CCBs to 1 and use the default 1 entry
7552 ** jump table by default.
7555 lp->jump_ccb = &lp->jump_ccb_0;
7556 lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
7559 ** Initilialyze the reselect script:
7561 ** Jump to next lcb if SFBR does not match this lun.
7562 ** Load TEMP with the CCB direct jump table bus address.
7563 ** Get the SIMPLE TAG message and the tag.
7565 ** JUMP IF (SFBR != #lun#), @(next lcb)
7566 ** COPY @(lp->p_jump_ccb), @(temp)
7567 ** JUMP @script(resel_notag)
7569 lp->jump_lcb.l_cmd =
7570 cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
7571 lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
7573 lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
7574 lp->load_jump_ccb[1] = cpu_to_scr(vtobus (&lp->p_jump_ccb));
7575 lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
7577 lp->jump_tag.l_cmd = cpu_to_scr(SCR_JUMP);
7578 lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
7581 ** Link this lun control block to the JUMP chain.
7583 tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtobus (&lp->jump_lcb));
7586 ** Initialize command queuing control.
7596 /*------------------------------------------------------------------------
7597 ** Lun control block setup on INQUIRY data received.
7598 **------------------------------------------------------------------------
7599 ** We only support WIDE, SYNC for targets and CMDQ for logical units.
7600 ** This setup is done on each INQUIRY since we are expecting user
7601 ** will play with CHANGE DEFINITION commands. :-)
7602 **------------------------------------------------------------------------
7604 static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
7606 unsigned char tn = sdev->id, ln = sdev->lun;
7607 struct tcb *tp = &np->target[tn];
7608 struct lcb *lp = tp->lp[ln];
7610 /* If no lcb, try to allocate it. */
7611 if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
7615 ** If unit supports tagged commands, allocate the
7616 ** CCB JUMP table if not yet.
7618 if (sdev->tagged_supported && lp->jump_ccb == &lp->jump_ccb_0) {
7620 lp->jump_ccb = m_calloc_dma(256, "JUMP_CCB");
7621 if (!lp->jump_ccb) {
7622 lp->jump_ccb = &lp->jump_ccb_0;
7625 lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
7626 for (i = 0 ; i < 64 ; i++)
7628 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
7629 for (i = 0 ; i < MAX_TAGS ; i++)
7631 lp->maxnxs = MAX_TAGS;
7632 lp->tags_stime = jiffies + 3*HZ;
7633 ncr_setup_tags (np, sdev);
7641 /*==========================================================
7644 ** Build Scatter Gather Block
7647 **==========================================================
7649 ** The transfer area may be scattered among
7650 ** several non adjacent physical pages.
7652 ** We may use MAX_SCATTER blocks.
7654 **----------------------------------------------------------
7658 ** We try to reduce the number of interrupts caused
7659 ** by unexpected phase changes due to disconnects.
7660 ** A typical harddisk may disconnect before ANY block.
7661 ** If we wanted to avoid unexpected phase changes at all
7662 ** we had to use a break point every 512 bytes.
7663 ** Of course the number of scatter/gather blocks is
7665 ** Under Linux, the scatter/gatter blocks are provided by
7666 ** the generic driver. We just have to copy addresses and
7667 ** sizes to the data segment array.
7670 static int ncr_scatter_no_sglist(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
7672 struct scr_tblmove *data = &cp->phys.data[MAX_SCATTER - 1];
7675 cp->data_len = cmd->request_bufflen;
7677 if (cmd->request_bufflen) {
7678 dma_addr_t baddr = map_scsi_single_data(np, cmd);
7680 ncr_build_sge(np, data, baddr, cmd->request_bufflen);
7692 static int ncr_scatter(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
7695 int use_sg = (int) cmd->use_sg;
7700 segment = ncr_scatter_no_sglist(np, cp, cmd);
7701 else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
7702 struct scatterlist *scatter = (struct scatterlist *)cmd->request_buffer;
7703 struct scr_tblmove *data;
7705 if (use_sg > MAX_SCATTER) {
7706 unmap_scsi_data(np, cmd);
7710 data = &cp->phys.data[MAX_SCATTER - use_sg];
7712 for (segment = 0; segment < use_sg; segment++) {
7713 dma_addr_t baddr = sg_dma_address(&scatter[segment]);
7714 unsigned int len = sg_dma_len(&scatter[segment]);
7716 ncr_build_sge(np, &data[segment], baddr, len);
7717 cp->data_len += len;
7726 /*==========================================================
7729 ** Test the bus snoop logic :-(
7731 ** Has to be called with interrupts disabled.
7734 **==========================================================
7737 static int __init ncr_regtest (struct ncb* np)
7739 register volatile u32 data;
7741 ** ncr registers may NOT be cached.
7742 ** write 0xffffffff to a read only register area,
7743 ** and try to read it back.
7746 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
7747 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
7749 if (data == 0xffffffff) {
7751 if ((data & 0xe2f0fffd) != 0x02000080) {
7753 printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
7760 static int __init ncr_snooptest (struct ncb* np)
7762 u32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
7765 err |= ncr_regtest (np);
7771 pc = NCB_SCRIPTH_PHYS (np, snooptest);
7775 ** Set memory and register.
7777 np->ncr_cache = cpu_to_scr(host_wr);
7778 OUTL (nc_temp, ncr_wr);
7780 ** Start script (exchange values)
7784 ** Wait 'til done (with timeout)
7786 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
7787 if (INB(nc_istat) & (INTF|SIP|DIP))
7790 ** Save termination position.
7794 ** Read memory and register.
7796 host_rd = scr_to_cpu(np->ncr_cache);
7797 ncr_rd = INL (nc_scratcha);
7798 ncr_bk = INL (nc_temp);
7802 ncr_chip_reset(np, 100);
7804 ** check for timeout
7806 if (i>=NCR_SNOOP_TIMEOUT) {
7807 printk ("CACHE TEST FAILED: timeout.\n");
7811 ** Check termination position.
7813 if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
7814 printk ("CACHE TEST FAILED: script execution failed.\n");
7815 printk ("start=%08lx, pc=%08lx, end=%08lx\n",
7816 (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
7817 (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
7823 if (host_wr != ncr_rd) {
7824 printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
7825 (int) host_wr, (int) ncr_rd);
7828 if (host_rd != ncr_wr) {
7829 printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
7830 (int) ncr_wr, (int) host_rd);
7833 if (ncr_bk != ncr_wr) {
7834 printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
7835 (int) ncr_wr, (int) ncr_bk);
7841 /*==========================================================
7843 ** Determine the ncr's clock frequency.
7844 ** This is essential for the negotiation
7845 ** of the synchronous transfer rate.
7847 **==========================================================
7849 ** Note: we have to return the correct value.
7850 ** THERE IS NO SAFE DEFAULT VALUE.
7852 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
7853 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
7854 ** do not have a clock doubler and so are provided with a
7855 ** 80 MHz clock. All other fast20 boards incorporate a doubler
7856 ** and so should be delivered with a 40 MHz clock.
7857 ** The future fast40 chips (895/895) use a 40 Mhz base clock
7858 ** and provide a clock quadrupler (160 Mhz). The code below
7859 ** tries to deal as cleverly as possible with all this stuff.
7861 **----------------------------------------------------------
7865 * Select NCR SCSI clock frequency
7867 static void ncr_selectclock(struct ncb *np, u_char scntl3)
7869 if (np->multiplier < 2) {
7870 OUTB(nc_scntl3, scntl3);
7874 if (bootverbose >= 2)
7875 printk ("%s: enabling clock multiplier\n", ncr_name(np));
7877 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
7878 if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
7880 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
7883 printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
7884 } else /* Wait 20 micro-seconds for doubler */
7886 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
7887 OUTB(nc_scntl3, scntl3);
7888 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
7889 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
7894 * calculate NCR SCSI clock frequency (in KHz)
7896 static unsigned __init ncrgetfreq (struct ncb *np, int gen)
7902 * Measure GEN timer delay in order
7903 * to calculate SCSI clock frequency
7905 * This code will never execute too
7906 * many loop iterations (if DELAY is
7907 * reasonably correct). It could get
7908 * too low a delay (too high a freq.)
7909 * if the CPU is slow executing the
7910 * loop for some reason (an NMI, for
7911 * example). For this reason we will
7912 * if multiple measurements are to be
7913 * performed trust the higher delay
7914 * (lower frequency returned).
7916 OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
7917 OUTW (nc_sien , 0); /* mask all scsi interrupts */
7918 (void) INW (nc_sist); /* clear pending scsi interrupt */
7919 OUTB (nc_dien , 0); /* mask all dma interrupts */
7920 (void) INW (nc_sist); /* another one, just to be sure :) */
7921 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
7922 OUTB (nc_stime1, 0); /* disable general purpose timer */
7923 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
7924 while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
7925 for (count = 0; count < 10; count ++)
7926 udelay(100); /* count ms */
7928 OUTB (nc_stime1, 0); /* disable general purpose timer */
7930 * set prescaler to divide by whatever 0 means
7931 * 0 ought to choose divide by 2, but appears
7932 * to set divide by 3.5 mode in my 53c810 ...
7934 OUTB (nc_scntl3, 0);
7936 if (bootverbose >= 2)
7937 printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
7939 * adjust for prescaler, and convert into KHz
7941 return ms ? ((1 << gen) * 4340) / ms : 0;
7945 * Get/probe NCR SCSI clock frequency
7947 static void __init ncr_getclock (struct ncb *np, int mult)
7949 unsigned char scntl3 = INB(nc_scntl3);
7950 unsigned char stest1 = INB(nc_stest1);
7957 ** True with 875 or 895 with clock multiplier selected
7959 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
7960 if (bootverbose >= 2)
7961 printk ("%s: clock multiplier found\n", ncr_name(np));
7962 np->multiplier = mult;
7966 ** If multiplier not found or scntl3 not 7,5,3,
7967 ** reset chip and get frequency from general purpose timer.
7968 ** Otherwise trust scntl3 BIOS setting.
7970 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
7973 ncr_chip_reset(np, 5);
7975 (void) ncrgetfreq (np, 11); /* throw away first result */
7976 f1 = ncrgetfreq (np, 11);
7977 f2 = ncrgetfreq (np, 11);
7980 printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
7982 if (f1 > f2) f1 = f2; /* trust lower result */
7984 if (f1 < 45000) f1 = 40000;
7985 else if (f1 < 55000) f1 = 50000;
7988 if (f1 < 80000 && mult > 1) {
7989 if (bootverbose >= 2)
7990 printk ("%s: clock multiplier assumed\n", ncr_name(np));
7991 np->multiplier = mult;
7994 if ((scntl3 & 7) == 3) f1 = 40000;
7995 else if ((scntl3 & 7) == 5) f1 = 80000;
7998 f1 /= np->multiplier;
8002 ** Compute controller synchronous parameters.
8004 f1 *= np->multiplier;
8008 /*===================== LINUX ENTRY POINTS SECTION ==========================*/
8010 static int ncr53c8xx_slave_alloc(struct scsi_device *device)
8012 struct Scsi_Host *host = device->host;
8013 struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
8014 struct tcb *tp = &np->target[device->id];
8015 tp->starget = device->sdev_target;
8020 static int ncr53c8xx_slave_configure(struct scsi_device *device)
8022 struct Scsi_Host *host = device->host;
8023 struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
8024 struct tcb *tp = &np->target[device->id];
8025 struct lcb *lp = tp->lp[device->lun];
8026 int numtags, depth_to_use;
8028 ncr_setup_lcb(np, device);
8031 ** Select queue depth from driver setup.
8032 ** Donnot use more than configured by user.
8034 ** Donnot use more than our maximum.
8036 numtags = device_queue_depth(np->unit, device->id, device->lun);
8037 if (numtags > tp->usrtags)
8038 numtags = tp->usrtags;
8039 if (!device->tagged_supported)
8041 depth_to_use = numtags;
8042 if (depth_to_use < 2)
8044 if (depth_to_use > MAX_TAGS)
8045 depth_to_use = MAX_TAGS;
8047 scsi_adjust_queue_depth(device,
8048 (device->tagged_supported ?
8049 MSG_SIMPLE_TAG : 0),
8053 ** Since the queue depth is not tunable under Linux,
8054 ** we need to know this value in order not to
8055 ** announce stupid things to user.
8057 ** XXX(hch): As of Linux 2.6 it certainly _is_ tunable..
8058 ** In fact we just tuned it, or did I miss
8059 ** something important? :)
8062 lp->numtags = lp->maxtags = numtags;
8063 lp->scdev_depth = depth_to_use;
8065 ncr_setup_tags (np, device);
8067 #ifdef DEBUG_NCR53C8XX
8068 printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
8069 np->unit, device->id, device->lun, depth_to_use);
8072 if (spi_support_sync(device->sdev_target) &&
8073 !spi_initial_dv(device->sdev_target))
8074 spi_dv_device(device);
8078 static int ncr53c8xx_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
8080 struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
8081 unsigned long flags;
8084 #ifdef DEBUG_NCR53C8XX
8085 printk("ncr53c8xx_queue_command\n");
8088 cmd->scsi_done = done;
8089 cmd->host_scribble = NULL;
8090 cmd->__data_mapped = 0;
8091 cmd->__data_mapping = 0;
8093 spin_lock_irqsave(&np->smp_lock, flags);
8095 if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
8096 cmd->result = ScsiResult(sts, 0);
8097 #ifdef DEBUG_NCR53C8XX
8098 printk("ncr53c8xx : command not queued - result=%d\n", sts);
8101 #ifdef DEBUG_NCR53C8XX
8103 printk("ncr53c8xx : command successfully queued\n");
8106 spin_unlock_irqrestore(&np->smp_lock, flags);
8108 if (sts != DID_OK) {
8109 unmap_scsi_data(np, cmd);
8117 irqreturn_t ncr53c8xx_intr(int irq, void *dev_id)
8119 unsigned long flags;
8120 struct Scsi_Host *shost = (struct Scsi_Host *)dev_id;
8121 struct host_data *host_data = (struct host_data *)shost->hostdata;
8122 struct ncb *np = host_data->ncb;
8123 struct scsi_cmnd *done_list;
8125 #ifdef DEBUG_NCR53C8XX
8126 printk("ncr53c8xx : interrupt received\n");
8129 if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
8131 spin_lock_irqsave(&np->smp_lock, flags);
8133 done_list = np->done_list;
8134 np->done_list = NULL;
8135 spin_unlock_irqrestore(&np->smp_lock, flags);
8137 if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
8140 ncr_flush_done_cmds(done_list);
8144 static void ncr53c8xx_timeout(unsigned long npref)
8146 struct ncb *np = (struct ncb *) npref;
8147 unsigned long flags;
8148 struct scsi_cmnd *done_list;
8150 spin_lock_irqsave(&np->smp_lock, flags);
8152 done_list = np->done_list;
8153 np->done_list = NULL;
8154 spin_unlock_irqrestore(&np->smp_lock, flags);
8157 ncr_flush_done_cmds(done_list);
8160 static int ncr53c8xx_bus_reset(struct scsi_cmnd *cmd)
8162 struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
8164 unsigned long flags;
8165 struct scsi_cmnd *done_list;
8168 * If the mid-level driver told us reset is synchronous, it seems
8169 * that we must call the done() callback for the involved command,
8170 * even if this command was not queued to the low-level driver,
8171 * before returning SUCCESS.
8174 spin_lock_irqsave(&np->smp_lock, flags);
8175 sts = ncr_reset_bus(np, cmd, 1);
8177 done_list = np->done_list;
8178 np->done_list = NULL;
8179 spin_unlock_irqrestore(&np->smp_lock, flags);
8181 ncr_flush_done_cmds(done_list);
8186 #if 0 /* unused and broken */
8187 static int ncr53c8xx_abort(struct scsi_cmnd *cmd)
8189 struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
8191 unsigned long flags;
8192 struct scsi_cmnd *done_list;
8194 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8195 printk("ncr53c8xx_abort: pid=%lu serial_number=%ld\n",
8196 cmd->pid, cmd->serial_number);
8198 printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
8201 NCR_LOCK_NCB(np, flags);
8203 sts = ncr_abort_command(np, cmd);
8205 done_list = np->done_list;
8206 np->done_list = NULL;
8207 NCR_UNLOCK_NCB(np, flags);
8209 ncr_flush_done_cmds(done_list);
8217 ** Scsi command waiting list management.
8219 ** It may happen that we cannot insert a scsi command into the start queue,
8220 ** in the following circumstances.
8221 ** Too few preallocated ccb(s),
8222 ** maxtags < cmd_per_lun of the Linux host control block,
8224 ** Such scsi commands are inserted into a waiting list.
8225 ** When a scsi command complete, we try to requeue the commands of the
8229 #define next_wcmd host_scribble
8231 static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd)
8233 struct scsi_cmnd *wcmd;
8235 #ifdef DEBUG_WAITING_LIST
8236 printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
8238 cmd->next_wcmd = NULL;
8239 if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
8241 while ((wcmd->next_wcmd) != 0)
8242 wcmd = (struct scsi_cmnd *) wcmd->next_wcmd;
8243 wcmd->next_wcmd = (char *) cmd;
8247 static struct scsi_cmnd *retrieve_from_waiting_list(int to_remove, struct ncb *np, struct scsi_cmnd *cmd)
8249 struct scsi_cmnd **pcmd = &np->waiting_list;
8254 *pcmd = (struct scsi_cmnd *) cmd->next_wcmd;
8255 cmd->next_wcmd = NULL;
8257 #ifdef DEBUG_WAITING_LIST
8258 printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
8262 pcmd = (struct scsi_cmnd **) &(*pcmd)->next_wcmd;
8267 static void process_waiting_list(struct ncb *np, int sts)
8269 struct scsi_cmnd *waiting_list, *wcmd;
8271 waiting_list = np->waiting_list;
8272 np->waiting_list = NULL;
8274 #ifdef DEBUG_WAITING_LIST
8275 if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
8277 while ((wcmd = waiting_list) != 0) {
8278 waiting_list = (struct scsi_cmnd *) wcmd->next_wcmd;
8279 wcmd->next_wcmd = NULL;
8280 if (sts == DID_OK) {
8281 #ifdef DEBUG_WAITING_LIST
8282 printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
8284 sts = ncr_queue_command(np, wcmd);
8286 if (sts != DID_OK) {
8287 #ifdef DEBUG_WAITING_LIST
8288 printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
8290 wcmd->result = ScsiResult(sts, 0);
8291 ncr_queue_done_cmd(np, wcmd);
8298 static ssize_t show_ncr53c8xx_revision(struct class_device *dev, char *buf)
8300 struct Scsi_Host *host = class_to_shost(dev);
8301 struct host_data *host_data = (struct host_data *)host->hostdata;
8303 return snprintf(buf, 20, "0x%x\n", host_data->ncb->revision_id);
8306 static struct class_device_attribute ncr53c8xx_revision_attr = {
8307 .attr = { .name = "revision", .mode = S_IRUGO, },
8308 .show = show_ncr53c8xx_revision,
8311 static struct class_device_attribute *ncr53c8xx_host_attrs[] = {
8312 &ncr53c8xx_revision_attr,
8316 /*==========================================================
8318 ** Boot command line.
8320 **==========================================================
8323 char *ncr53c8xx; /* command line passed by insmod */
8324 module_param(ncr53c8xx, charp, 0);
8328 static int __init ncr53c8xx_setup(char *str)
8330 return sym53c8xx__setup(str);
8333 __setup("ncr53c8xx=", ncr53c8xx_setup);
8338 * Host attach and initialisations.
8340 * Allocate host data and ncb structure.
8341 * Request IO region and remap MMIO region.
8342 * Do chip initialization.
8343 * If all is OK, install interrupt handling and
8344 * start the timer daemon.
8346 struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
8347 int unit, struct ncr_device *device)
8349 struct host_data *host_data;
8350 struct ncb *np = NULL;
8351 struct Scsi_Host *instance = NULL;
8356 tpnt->name = SCSI_NCR_DRIVER_NAME;
8357 if (!tpnt->shost_attrs)
8358 tpnt->shost_attrs = ncr53c8xx_host_attrs;
8360 tpnt->queuecommand = ncr53c8xx_queue_command;
8361 tpnt->slave_configure = ncr53c8xx_slave_configure;
8362 tpnt->slave_alloc = ncr53c8xx_slave_alloc;
8363 tpnt->eh_bus_reset_handler = ncr53c8xx_bus_reset;
8364 tpnt->can_queue = SCSI_NCR_CAN_QUEUE;
8366 tpnt->sg_tablesize = SCSI_NCR_SG_TABLESIZE;
8367 tpnt->cmd_per_lun = SCSI_NCR_CMD_PER_LUN;
8368 tpnt->use_clustering = ENABLE_CLUSTERING;
8370 if (device->differential)
8371 driver_setup.diff_support = device->differential;
8373 printk(KERN_INFO "ncr53c720-%d: rev 0x%x irq %d\n",
8374 unit, device->chip.revision_id, device->slot.irq);
8376 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
8379 host_data = (struct host_data *) instance->hostdata;
8381 np = __m_calloc_dma(device->dev, sizeof(struct ncb), "NCB");
8384 spin_lock_init(&np->smp_lock);
8385 np->dev = device->dev;
8386 np->p_ncb = vtobus(np);
8387 host_data->ncb = np;
8389 np->ccb = m_calloc_dma(sizeof(struct ccb), "CCB");
8393 /* Store input information in the host data structure. */
8395 np->verbose = driver_setup.verbose;
8396 sprintf(np->inst_name, "ncr53c720-%d", np->unit);
8397 np->revision_id = device->chip.revision_id;
8398 np->features = device->chip.features;
8399 np->clock_divn = device->chip.nr_divisor;
8400 np->maxoffs = device->chip.offset_max;
8401 np->maxburst = device->chip.burst_max;
8402 np->myaddr = device->host_id;
8404 /* Allocate SCRIPTS areas. */
8405 np->script0 = m_calloc_dma(sizeof(struct script), "SCRIPT");
8408 np->scripth0 = m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
8412 init_timer(&np->timer);
8413 np->timer.data = (unsigned long) np;
8414 np->timer.function = ncr53c8xx_timeout;
8416 /* Try to map the controller chip to virtual and physical memory. */
8418 np->paddr = device->slot.base;
8419 np->paddr2 = (np->features & FE_RAM) ? device->slot.base_2 : 0;
8421 if (device->slot.base_v)
8422 np->vaddr = device->slot.base_v;
8424 np->vaddr = ioremap(device->slot.base_c, 128);
8428 "%s: can't map memory mapped IO region\n",ncr_name(np));
8431 if (bootverbose > 1)
8433 "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
8436 /* Make the controller's registers available. Now the INB INW INL
8437 * OUTB OUTW OUTL macros can be used safely.
8440 np->reg = (struct ncr_reg __iomem *)np->vaddr;
8442 /* Do chip dependent initialization. */
8443 ncr_prepare_setting(np);
8445 if (np->paddr2 && sizeof(struct script) > 4096) {
8447 printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
8451 instance->max_channel = 0;
8452 instance->this_id = np->myaddr;
8453 instance->max_id = np->maxwide ? 16 : 8;
8454 instance->max_lun = SCSI_NCR_MAX_LUN;
8455 instance->base = (unsigned long) np->reg;
8456 instance->irq = device->slot.irq;
8457 instance->unique_id = device->slot.base;
8458 instance->dma_channel = 0;
8459 instance->cmd_per_lun = MAX_TAGS;
8460 instance->can_queue = (MAX_START-4);
8461 /* This can happen if you forget to call ncr53c8xx_init from
8462 * your module_init */
8463 BUG_ON(!ncr53c8xx_transport_template);
8464 instance->transportt = ncr53c8xx_transport_template;
8466 /* Patch script to physical addresses */
8467 ncr_script_fill(&script0, &scripth0);
8469 np->scripth = np->scripth0;
8470 np->p_scripth = vtobus(np->scripth);
8471 np->p_script = (np->paddr2) ? np->paddr2 : vtobus(np->script0);
8473 ncr_script_copy_and_bind(np, (ncrcmd *) &script0,
8474 (ncrcmd *) np->script0, sizeof(struct script));
8475 ncr_script_copy_and_bind(np, (ncrcmd *) &scripth0,
8476 (ncrcmd *) np->scripth0, sizeof(struct scripth));
8477 np->ccb->p_ccb = vtobus (np->ccb);
8479 /* Patch the script for LED support. */
8481 if (np->features & FE_LED0) {
8482 np->script0->idle[0] =
8483 cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
8484 np->script0->reselected[0] =
8485 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
8486 np->script0->start[0] =
8487 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
8491 * Look for the target control block of this nexus.
8493 * JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
8495 for (i = 0 ; i < 4 ; i++) {
8496 np->jump_tcb[i].l_cmd =
8497 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
8498 np->jump_tcb[i].l_paddr =
8499 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
8502 ncr_chip_reset(np, 100);
8504 /* Now check the cache handling of the chipset. */
8506 if (ncr_snooptest(np)) {
8507 printk(KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
8511 /* Install the interrupt handler. */
8512 np->irq = device->slot.irq;
8514 /* Initialize the fixed part of the default ccb. */
8515 ncr_init_ccb(np, np->ccb);
8518 * After SCSI devices have been opened, we cannot reset the bus
8519 * safely, so we do it here. Interrupt handler does the real work.
8520 * Process the reset exception if interrupts are not enabled yet.
8521 * Then enable disconnects.
8523 spin_lock_irqsave(&np->smp_lock, flags);
8524 if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
8525 printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
8527 spin_unlock_irqrestore(&np->smp_lock, flags);
8535 * The middle-level SCSI driver does not wait for devices to settle.
8536 * Wait synchronously if more than 2 seconds.
8538 if (driver_setup.settle_delay > 2) {
8539 printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
8540 ncr_name(np), driver_setup.settle_delay);
8541 mdelay(1000 * driver_setup.settle_delay);
8544 /* start the timeout daemon */
8548 /* use SIMPLE TAG messages by default */
8549 #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
8550 np->order = SIMPLE_QUEUE_TAG;
8553 spin_unlock_irqrestore(&np->smp_lock, flags);
8560 printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
8564 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
8566 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
8568 m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
8569 m_free_dma(np, sizeof(struct ncb), "NCB");
8570 host_data->ncb = NULL;
8573 scsi_host_put(instance);
8579 int ncr53c8xx_release(struct Scsi_Host *host)
8581 struct host_data *host_data;
8582 #ifdef DEBUG_NCR53C8XX
8583 printk("ncr53c8xx: release\n");
8587 host_data = (struct host_data *)host->hostdata;
8588 if (host_data && host_data->ncb)
8589 ncr_detach(host_data->ncb);
8593 static void ncr53c8xx_set_period(struct scsi_target *starget, int period)
8595 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8596 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8597 struct tcb *tp = &np->target[starget->id];
8599 if (period > np->maxsync)
8600 period = np->maxsync;
8601 else if (period < np->minsync)
8602 period = np->minsync;
8604 tp->usrsync = period;
8606 ncr_negotiate(np, tp);
8609 static void ncr53c8xx_set_offset(struct scsi_target *starget, int offset)
8611 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8612 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8613 struct tcb *tp = &np->target[starget->id];
8615 if (offset > np->maxoffs)
8616 offset = np->maxoffs;
8617 else if (offset < 0)
8620 tp->maxoffs = offset;
8622 ncr_negotiate(np, tp);
8625 static void ncr53c8xx_set_width(struct scsi_target *starget, int width)
8627 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8628 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8629 struct tcb *tp = &np->target[starget->id];
8631 if (width > np->maxwide)
8632 width = np->maxwide;
8636 tp->usrwide = width;
8638 ncr_negotiate(np, tp);
8641 static void ncr53c8xx_get_signalling(struct Scsi_Host *shost)
8643 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8644 enum spi_signal_type type;
8646 switch (np->scsi_mode) {
8648 type = SPI_SIGNAL_SE;
8651 type = SPI_SIGNAL_HVD;
8654 type = SPI_SIGNAL_UNKNOWN;
8657 spi_signalling(shost) = type;
8660 static struct spi_function_template ncr53c8xx_transport_functions = {
8661 .set_period = ncr53c8xx_set_period,
8663 .set_offset = ncr53c8xx_set_offset,
8665 .set_width = ncr53c8xx_set_width,
8667 .get_signalling = ncr53c8xx_get_signalling,
8670 int __init ncr53c8xx_init(void)
8672 ncr53c8xx_transport_template = spi_attach_transport(&ncr53c8xx_transport_functions);
8673 if (!ncr53c8xx_transport_template)
8678 void ncr53c8xx_exit(void)
8680 spi_release_transport(ncr53c8xx_transport_template);