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 astraction, 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->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->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;
592 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
593 static struct ncr_driver_setup
594 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
597 #define initverbose (driver_setup.verbose)
598 #define bootverbose (np->verbose)
601 /*===================================================================
603 ** Driver setup from the boot command line
605 **===================================================================
615 #define OPT_MASTER_PARITY 2
616 #define OPT_SCSI_PARITY 3
617 #define OPT_DISCONNECTION 4
618 #define OPT_SPECIAL_FEATURES 5
619 #define OPT_UNUSED_1 6
620 #define OPT_FORCE_SYNC_NEGO 7
621 #define OPT_REVERSE_PROBE 8
622 #define OPT_DEFAULT_SYNC 9
623 #define OPT_VERBOSE 10
625 #define OPT_BURST_MAX 12
626 #define OPT_LED_PIN 13
627 #define OPT_MAX_WIDE 14
628 #define OPT_SETTLE_DELAY 15
629 #define OPT_DIFF_SUPPORT 16
631 #define OPT_PCI_FIX_UP 18
632 #define OPT_BUS_CHECK 19
633 #define OPT_OPTIMIZE 20
634 #define OPT_RECOVERY 21
635 #define OPT_SAFE_SETUP 22
636 #define OPT_USE_NVRAM 23
637 #define OPT_EXCLUDE 24
638 #define OPT_HOST_ID 25
640 #ifdef SCSI_NCR_IARB_SUPPORT
644 static char setup_token[] __initdata =
658 #ifdef SCSI_NCR_IARB_SUPPORT
661 ; /* DONNOT REMOVE THIS ';' */
669 static int __init get_setup_token(char *p)
671 char *cur = setup_token;
675 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
678 if (!strncmp(p, cur, pc - cur))
686 static int __init sym53c8xx__setup(char *str)
688 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
694 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
706 val = (int) simple_strtoul(pv, &pe, 0);
708 switch (get_setup_token(cur)) {
710 driver_setup.default_tags = val;
711 if (pe && *pe == '/') {
713 while (*pe && *pe != ARG_SEP &&
714 i < sizeof(driver_setup.tag_ctrl)-1) {
715 driver_setup.tag_ctrl[i++] = *pe++;
717 driver_setup.tag_ctrl[i] = '\0';
720 case OPT_MASTER_PARITY:
721 driver_setup.master_parity = val;
723 case OPT_SCSI_PARITY:
724 driver_setup.scsi_parity = val;
726 case OPT_DISCONNECTION:
727 driver_setup.disconnection = val;
729 case OPT_SPECIAL_FEATURES:
730 driver_setup.special_features = val;
732 case OPT_FORCE_SYNC_NEGO:
733 driver_setup.force_sync_nego = val;
735 case OPT_REVERSE_PROBE:
736 driver_setup.reverse_probe = val;
738 case OPT_DEFAULT_SYNC:
739 driver_setup.default_sync = val;
742 driver_setup.verbose = val;
745 driver_setup.debug = val;
748 driver_setup.burst_max = val;
751 driver_setup.led_pin = val;
754 driver_setup.max_wide = val? 1:0;
756 case OPT_SETTLE_DELAY:
757 driver_setup.settle_delay = val;
759 case OPT_DIFF_SUPPORT:
760 driver_setup.diff_support = val;
763 driver_setup.irqm = val;
766 driver_setup.pci_fix_up = val;
769 driver_setup.bus_check = val;
772 driver_setup.optimize = val;
775 driver_setup.recovery = val;
778 driver_setup.use_nvram = val;
781 memcpy(&driver_setup, &driver_safe_setup,
782 sizeof(driver_setup));
785 if (xi < SCSI_NCR_MAX_EXCLUDES)
786 driver_setup.excludes[xi++] = val;
789 driver_setup.host_id = val;
791 #ifdef SCSI_NCR_IARB_SUPPORT
793 driver_setup.iarb = val;
797 printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
801 if ((cur = strchr(cur, ARG_SEP)) != NULL)
804 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
808 /*===================================================================
810 ** Get device queue depth from boot command line.
812 **===================================================================
814 #define DEF_DEPTH (driver_setup.default_tags)
815 #define ALL_TARGETS -2
820 static int device_queue_depth(int unit, int target, int lun)
823 char *p = driver_setup.tag_ctrl;
829 while ((c = *p++) != 0) {
830 v = simple_strtoul(p, &ep, 0);
839 t = (target == v) ? v : NO_TARGET;
844 u = (lun == v) ? v : NO_LUN;
848 (t == ALL_TARGETS || t == target) &&
849 (u == ALL_LUNS || u == lun))
865 /*==========================================================
867 ** The CCB done queue uses an array of CCB virtual
868 ** addresses. Empty entries are flagged using the bogus
869 ** virtual address 0xffffffff.
871 ** Since PCI ensures that only aligned DWORDs are accessed
872 ** atomically, 64 bit little-endian architecture requires
873 ** to test the high order DWORD of the entry to determine
874 ** if it is empty or valid.
876 ** BTW, I will make things differently as soon as I will
877 ** have a better idea, but this is simple and should work.
879 **==========================================================
882 #define SCSI_NCR_CCB_DONE_SUPPORT
883 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
886 #define CCB_DONE_EMPTY 0xffffffffUL
888 /* All 32 bit architectures */
889 #if BITS_PER_LONG == 32
890 #define CCB_DONE_VALID(cp) (((u_long) cp) != CCB_DONE_EMPTY)
892 /* All > 32 bit (64 bit) architectures regardless endian-ness */
894 #define CCB_DONE_VALID(cp) \
895 ((((u_long) cp) & 0xffffffff00000000ul) && \
896 (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
899 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
901 /*==========================================================
903 ** Configuration and Debugging
905 **==========================================================
909 ** SCSI address of this device.
910 ** The boot routines should have set it.
914 #ifndef SCSI_NCR_MYADDR
915 #define SCSI_NCR_MYADDR (7)
919 ** The maximum number of tags per logic unit.
920 ** Used only for disk devices that support tags.
923 #ifndef SCSI_NCR_MAX_TAGS
924 #define SCSI_NCR_MAX_TAGS (8)
928 ** TAGS are actually limited to 64 tags/lun.
929 ** We need to deal with power of 2, for alignment constraints.
931 #if SCSI_NCR_MAX_TAGS > 64
932 #define MAX_TAGS (64)
934 #define MAX_TAGS SCSI_NCR_MAX_TAGS
940 ** Choose appropriate type for tag bitmap.
943 typedef u64 tagmap_t;
945 typedef u32 tagmap_t;
949 ** Number of targets supported by the driver.
950 ** n permits target numbers 0..n-1.
951 ** Default is 16, meaning targets #0..#15.
955 #ifdef SCSI_NCR_MAX_TARGET
956 #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
958 #define MAX_TARGET (16)
962 ** Number of logic units supported by the driver.
963 ** n enables logic unit numbers 0..n-1.
964 ** The common SCSI devices require only
965 ** one lun, so take 1 as the default.
968 #ifdef SCSI_NCR_MAX_LUN
969 #define MAX_LUN SCSI_NCR_MAX_LUN
975 ** Asynchronous pre-scaler (ns). Shall be 40
978 #ifndef SCSI_NCR_MIN_ASYNC
979 #define SCSI_NCR_MIN_ASYNC (40)
983 ** The maximum number of jobs scheduled for starting.
984 ** There should be one slot per target, and one slot
985 ** for each tag of each target in use.
986 ** The calculation below is actually quite silly ...
989 #ifdef SCSI_NCR_CAN_QUEUE
990 #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
992 #define MAX_START (MAX_TARGET + 7 * MAX_TAGS)
996 ** We limit the max number of pending IO to 250.
997 ** since we donnot want to allocate more than 1
998 ** PAGE for 'scripth'.
1002 #define MAX_START 250
1006 ** The maximum number of segments a transfer is split into.
1007 ** We support up to 127 segments for both read and write.
1008 ** The data scripts are broken into 2 sub-scripts.
1009 ** 80 (MAX_SCATTERL) segments are moved from a sub-script
1010 ** in on-chip RAM. This makes data transfers shorter than
1011 ** 80k (assuming 1k fs) as fast as possible.
1014 #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
1016 #if (MAX_SCATTER > 80)
1017 #define MAX_SCATTERL 80
1018 #define MAX_SCATTERH (MAX_SCATTER - MAX_SCATTERL)
1020 #define MAX_SCATTERL (MAX_SCATTER-1)
1021 #define MAX_SCATTERH 1
1028 #define NCR_SNOOP_TIMEOUT (1000000)
1031 ** Other definitions
1034 #define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
1036 #define initverbose (driver_setup.verbose)
1037 #define bootverbose (np->verbose)
1039 /*==========================================================
1041 ** Command control block states.
1043 **==========================================================
1048 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
1049 #define HS_DISCONNECT (3) /* Disconnected by target */
1051 #define HS_DONEMASK (0x80)
1052 #define HS_COMPLETE (4|HS_DONEMASK)
1053 #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
1054 #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
1055 #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
1056 #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
1057 #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
1058 #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
1061 ** Invalid host status values used by the SCRIPTS processor
1062 ** when the nexus is not fully identified.
1063 ** Shall never appear in a CCB.
1066 #define HS_INVALMASK (0x40)
1067 #define HS_SELECTING (0|HS_INVALMASK)
1068 #define HS_IN_RESELECT (1|HS_INVALMASK)
1069 #define HS_STARTING (2|HS_INVALMASK)
1072 ** Flags set by the SCRIPT processor for commands
1073 ** that have been skipped.
1075 #define HS_SKIPMASK (0x20)
1077 /*==========================================================
1079 ** Software Interrupt Codes
1081 **==========================================================
1084 #define SIR_BAD_STATUS (1)
1085 #define SIR_XXXXXXXXXX (2)
1086 #define SIR_NEGO_SYNC (3)
1087 #define SIR_NEGO_WIDE (4)
1088 #define SIR_NEGO_FAILED (5)
1089 #define SIR_NEGO_PROTO (6)
1090 #define SIR_REJECT_RECEIVED (7)
1091 #define SIR_REJECT_SENT (8)
1092 #define SIR_IGN_RESIDUE (9)
1093 #define SIR_MISSING_SAVE (10)
1094 #define SIR_RESEL_NO_MSG_IN (11)
1095 #define SIR_RESEL_NO_IDENTIFY (12)
1096 #define SIR_RESEL_BAD_LUN (13)
1097 #define SIR_RESEL_BAD_TARGET (14)
1098 #define SIR_RESEL_BAD_I_T_L (15)
1099 #define SIR_RESEL_BAD_I_T_L_Q (16)
1100 #define SIR_DONE_OVERFLOW (17)
1101 #define SIR_INTFLY (18)
1102 #define SIR_MAX (18)
1104 /*==========================================================
1106 ** Extended error codes.
1107 ** xerr_status field of struct ccb.
1109 **==========================================================
1113 #define XE_EXTRA_DATA (1) /* unexpected data phase */
1114 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
1116 /*==========================================================
1118 ** Negotiation status.
1119 ** nego_status field of struct ccb.
1121 **==========================================================
1124 #define NS_NOCHANGE (0)
1129 /*==========================================================
1133 **==========================================================
1136 #define CCB_MAGIC (0xf2691ad2)
1138 /*==========================================================
1140 ** Declaration of structs.
1142 **==========================================================
1145 static struct scsi_transport_template *ncr53c8xx_transport_template = NULL;
1165 #define UC_SETSYNC 10
1166 #define UC_SETTAGS 11
1167 #define UC_SETDEBUG 12
1168 #define UC_SETORDER 13
1169 #define UC_SETWIDE 14
1170 #define UC_SETFLAG 15
1171 #define UC_SETVERBOSE 17
1173 #define UF_TRACE (0x01)
1174 #define UF_NODISC (0x02)
1175 #define UF_NOSCAN (0x04)
1177 /*========================================================================
1179 ** Declaration of structs: target control block
1181 **========================================================================
1184 /*----------------------------------------------------------------
1185 ** During reselection the ncr jumps to this point with SFBR
1186 ** set to the encoded target number with bit 7 set.
1187 ** if it's not this target, jump to the next.
1189 ** JUMP IF (SFBR != #target#), @(next tcb)
1190 **----------------------------------------------------------------
1192 struct link jump_tcb;
1194 /*----------------------------------------------------------------
1195 ** Load the actual values for the sxfer and the scntl3
1196 ** register (sync/wide mode).
1198 ** SCR_COPY (1), @(sval field of this tcb), @(sxfer register)
1199 ** SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
1200 **----------------------------------------------------------------
1204 /*----------------------------------------------------------------
1205 ** Get the IDENTIFY message and load the LUN to SFBR.
1207 ** CALL, <RESEL_LUN>
1208 **----------------------------------------------------------------
1210 struct link call_lun;
1212 /*----------------------------------------------------------------
1213 ** Now look for the right lun.
1216 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
1218 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1219 ** It is kind of hashcoding.
1220 **----------------------------------------------------------------
1222 struct link jump_lcb[4]; /* JUMPs for reselection */
1223 struct lcb * lp[MAX_LUN]; /* The lcb's of this tcb */
1225 /*----------------------------------------------------------------
1226 ** Pointer to the ccb used for negotiation.
1227 ** Prevent from starting a negotiation for all queued commands
1228 ** when tagged command queuing is enabled.
1229 **----------------------------------------------------------------
1231 struct ccb * nego_cp;
1233 /*----------------------------------------------------------------
1235 **----------------------------------------------------------------
1240 /*----------------------------------------------------------------
1241 ** negotiation of wide and synch transfer and device quirks.
1242 **----------------------------------------------------------------
1244 #ifdef SCSI_NCR_BIG_ENDIAN
1247 /*3*/ u_char minsync;
1249 /*1*/ u_char widedone;
1250 /*2*/ u_char quirks;
1251 /*3*/ u_char maxoffs;
1253 /*0*/ u_char minsync;
1256 /*0*/ u_char maxoffs;
1257 /*1*/ u_char quirks;
1258 /*2*/ u_char widedone;
1262 /* User settable limits and options. */
1267 struct scsi_target *starget;
1270 /*========================================================================
1272 ** Declaration of structs: lun control block
1274 **========================================================================
1277 /*----------------------------------------------------------------
1278 ** During reselection the ncr jumps to this point
1279 ** with SFBR set to the "Identify" message.
1280 ** if it's not this lun, jump to the next.
1282 ** JUMP IF (SFBR != #lun#), @(next lcb of this target)
1284 ** It is this lun. Load TEMP with the nexus jumps table
1285 ** address and jump to RESEL_TAG (or RESEL_NOTAG).
1287 ** SCR_COPY (4), p_jump_ccb, TEMP,
1288 ** SCR_JUMP, <RESEL_TAG>
1289 **----------------------------------------------------------------
1291 struct link jump_lcb;
1292 ncrcmd load_jump_ccb[3];
1293 struct link jump_tag;
1294 ncrcmd p_jump_ccb; /* Jump table bus address */
1296 /*----------------------------------------------------------------
1297 ** Jump table used by the script processor to directly jump
1298 ** to the CCB corresponding to the reselected nexus.
1299 ** Address is allocated on 256 bytes boundary in order to
1300 ** allow 8 bit calculation of the tag jump entry for up to
1301 ** 64 possible tags.
1302 **----------------------------------------------------------------
1304 u32 jump_ccb_0; /* Default table if no tags */
1305 u32 *jump_ccb; /* Virtual address */
1307 /*----------------------------------------------------------------
1308 ** CCB queue management.
1309 **----------------------------------------------------------------
1311 struct list_head free_ccbq; /* Queue of available CCBs */
1312 struct list_head busy_ccbq; /* Queue of busy CCBs */
1313 struct list_head wait_ccbq; /* Queue of waiting for IO CCBs */
1314 struct list_head skip_ccbq; /* Queue of skipped CCBs */
1315 u_char actccbs; /* Number of allocated CCBs */
1316 u_char busyccbs; /* CCBs busy for this lun */
1317 u_char queuedccbs; /* CCBs queued to the controller*/
1318 u_char queuedepth; /* Queue depth for this lun */
1319 u_char scdev_depth; /* SCSI device queue depth */
1320 u_char maxnxs; /* Max possible nexuses */
1322 /*----------------------------------------------------------------
1323 ** Control of tagged command queuing.
1324 ** Tags allocation is performed using a circular buffer.
1325 ** This avoids using a loop for tag allocation.
1326 **----------------------------------------------------------------
1328 u_char ia_tag; /* Allocation index */
1329 u_char if_tag; /* Freeing index */
1330 u_char cb_tags[MAX_TAGS]; /* Circular tags buffer */
1331 u_char usetags; /* Command queuing is active */
1332 u_char maxtags; /* Max nr of tags asked by user */
1333 u_char numtags; /* Current number of tags */
1335 /*----------------------------------------------------------------
1336 ** QUEUE FULL control and ORDERED tag control.
1337 **----------------------------------------------------------------
1339 /*----------------------------------------------------------------
1340 ** QUEUE FULL and ORDERED tag control.
1341 **----------------------------------------------------------------
1343 u16 num_good; /* Nr of GOOD since QUEUE FULL */
1344 tagmap_t tags_umap; /* Used tags bitmap */
1345 tagmap_t tags_smap; /* Tags in use at 'tag_stime' */
1346 u_long tags_stime; /* Last time we set smap=umap */
1347 struct ccb * held_ccb; /* CCB held for QUEUE FULL */
1350 /*========================================================================
1352 ** Declaration of structs: the launch script.
1354 **========================================================================
1356 ** It is part of the CCB and is called by the scripts processor to
1357 ** start or restart the data structure (nexus).
1358 ** This 6 DWORDs mini script makes use of prefetching.
1360 **------------------------------------------------------------------------
1363 /*----------------------------------------------------------------
1364 ** SCR_COPY(4), @(p_phys), @(dsa register)
1365 ** SCR_JUMP, @(scheduler_point)
1366 **----------------------------------------------------------------
1368 ncrcmd setup_dsa[3]; /* Copy 'phys' address to dsa */
1369 struct link schedule; /* Jump to scheduler point */
1370 ncrcmd p_phys; /* 'phys' header bus address */
1373 /*========================================================================
1375 ** Declaration of structs: global HEADER.
1377 **========================================================================
1379 ** This substructure is copied from the ccb to a global address after
1380 ** selection (or reselection) and copied back before disconnect.
1382 ** These fields are accessible to the script processor.
1384 **------------------------------------------------------------------------
1388 /*----------------------------------------------------------------
1389 ** Saved data pointer.
1390 ** Points to the position in the script responsible for the
1391 ** actual transfer transfer of data.
1392 ** It's written after reception of a SAVE_DATA_POINTER message.
1393 ** The goalpointer points after the last transfer command.
1394 **----------------------------------------------------------------
1400 /*----------------------------------------------------------------
1401 ** Alternate data pointer.
1402 ** They are copied back to savep/lastp/goalp by the SCRIPTS
1403 ** when the direction is unknown and the device claims data out.
1404 **----------------------------------------------------------------
1409 /*----------------------------------------------------------------
1410 ** The virtual address of the ccb containing this header.
1411 **----------------------------------------------------------------
1415 /*----------------------------------------------------------------
1417 **----------------------------------------------------------------
1419 u_char scr_st[4]; /* script status */
1420 u_char status[4]; /* host status. must be the */
1421 /* last DWORD of the header. */
1425 ** The status bytes are used by the host and the script processor.
1427 ** The byte corresponding to the host_status must be stored in the
1428 ** last DWORD of the CCB header since it is used for command
1429 ** completion (ncr_wakeup()). Doing so, we are sure that the header
1430 ** has been entirely copied back to the CCB when the host_status is
1431 ** seen complete by the CPU.
1433 ** The last four bytes (status[4]) are copied to the scratchb register
1434 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
1435 ** and copied back just after disconnecting.
1436 ** Inside the script the XX_REG are used.
1438 ** The first four bytes (scr_st[4]) are used inside the script by
1440 ** Because source and destination must have the same alignment
1441 ** in a DWORD, the fields HAVE to be at the choosen offsets.
1442 ** xerr_st 0 (0x34) scratcha
1443 ** sync_st 1 (0x05) sxfer
1444 ** wide_st 3 (0x03) scntl3
1448 ** Last four bytes (script)
1452 #define HS_PRT nc_scr1
1454 #define SS_PRT nc_scr2
1458 ** Last four bytes (host)
1460 #ifdef SCSI_NCR_BIG_ENDIAN
1461 #define actualquirks phys.header.status[3]
1462 #define host_status phys.header.status[2]
1463 #define scsi_status phys.header.status[1]
1464 #define parity_status phys.header.status[0]
1466 #define actualquirks phys.header.status[0]
1467 #define host_status phys.header.status[1]
1468 #define scsi_status phys.header.status[2]
1469 #define parity_status phys.header.status[3]
1473 ** First four bytes (script)
1475 #define xerr_st header.scr_st[0]
1476 #define sync_st header.scr_st[1]
1477 #define nego_st header.scr_st[2]
1478 #define wide_st header.scr_st[3]
1481 ** First four bytes (host)
1483 #define xerr_status phys.xerr_st
1484 #define nego_status phys.nego_st
1487 #define sync_status phys.sync_st
1488 #define wide_status phys.wide_st
1491 /*==========================================================
1493 ** Declaration of structs: Data structure block
1495 **==========================================================
1497 ** During execution of a ccb by the script processor,
1498 ** the DSA (data structure address) register points
1499 ** to this substructure of the ccb.
1500 ** This substructure contains the header with
1501 ** the script-processor-changable data and
1502 ** data blocks for the indirect move commands.
1504 **----------------------------------------------------------
1516 ** Table data for Script
1519 struct scr_tblsel select;
1520 struct scr_tblmove smsg ;
1521 struct scr_tblmove cmd ;
1522 struct scr_tblmove sense ;
1523 struct scr_tblmove data[MAX_SCATTER];
1527 /*========================================================================
1529 ** Declaration of structs: Command control block.
1531 **========================================================================
1534 /*----------------------------------------------------------------
1535 ** This is the data structure which is pointed by the DSA
1536 ** register when it is executed by the script processor.
1537 ** It must be the first entry because it contains the header
1538 ** as first entry that must be cache line aligned.
1539 **----------------------------------------------------------------
1543 /*----------------------------------------------------------------
1544 ** Mini-script used at CCB execution start-up.
1545 ** Load the DSA with the data structure address (phys) and
1546 ** jump to SELECT. Jump to CANCEL if CCB is to be canceled.
1547 **----------------------------------------------------------------
1549 struct launch start;
1551 /*----------------------------------------------------------------
1552 ** Mini-script used at CCB relection to restart the nexus.
1553 ** Load the DSA with the data structure address (phys) and
1554 ** jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
1555 **----------------------------------------------------------------
1557 struct launch restart;
1559 /*----------------------------------------------------------------
1560 ** If a data transfer phase is terminated too early
1561 ** (after reception of a message (i.e. DISCONNECT)),
1562 ** we have to prepare a mini script to transfer
1563 ** the rest of the data.
1564 **----------------------------------------------------------------
1568 /*----------------------------------------------------------------
1569 ** The general SCSI driver provides a
1570 ** pointer to a control block.
1571 **----------------------------------------------------------------
1573 struct scsi_cmnd *cmd; /* SCSI command */
1574 u_char cdb_buf[16]; /* Copy of CDB */
1575 u_char sense_buf[64];
1576 int data_len; /* Total data length */
1578 /*----------------------------------------------------------------
1580 ** We prepare a message to be sent after selection.
1581 ** We may use a second one if the command is rescheduled
1582 ** due to GETCC or QFULL.
1583 ** Contents are IDENTIFY and SIMPLE_TAG.
1584 ** While negotiating sync or wide transfer,
1585 ** a SDTR or WDTR message is appended.
1586 **----------------------------------------------------------------
1588 u_char scsi_smsg [8];
1589 u_char scsi_smsg2[8];
1591 /*----------------------------------------------------------------
1593 **----------------------------------------------------------------
1595 u_long p_ccb; /* BUS address of this CCB */
1596 u_char sensecmd[6]; /* Sense command */
1597 u_char tag; /* Tag for this transfer */
1598 /* 255 means no tag */
1603 struct ccb * link_ccb; /* Host adapter CCB chain */
1604 struct list_head link_ccbq; /* Link to unit CCB queue */
1605 u32 startp; /* Initial data pointer */
1606 u_long magic; /* Free / busy CCB flag */
1609 #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
1612 /*========================================================================
1614 ** Declaration of structs: NCR device descriptor
1616 **========================================================================
1619 /*----------------------------------------------------------------
1620 ** The global header.
1621 ** It is accessible to both the host and the script processor.
1622 ** Must be cache line size aligned (32 for x86) in order to
1623 ** allow cache line bursting when it is copied to/from CCB.
1624 **----------------------------------------------------------------
1628 /*----------------------------------------------------------------
1629 ** CCBs management queues.
1630 **----------------------------------------------------------------
1632 struct scsi_cmnd *waiting_list; /* Commands waiting for a CCB */
1633 /* when lcb is not allocated. */
1634 struct scsi_cmnd *done_list; /* Commands waiting for done() */
1635 /* callback to be invoked. */
1636 spinlock_t smp_lock; /* Lock for SMP threading */
1638 /*----------------------------------------------------------------
1639 ** Chip and controller indentification.
1640 **----------------------------------------------------------------
1642 int unit; /* Unit number */
1643 char inst_name[16]; /* ncb instance name */
1645 /*----------------------------------------------------------------
1646 ** Initial value of some IO register bits.
1647 ** These values are assumed to have been set by BIOS, and may
1648 ** be used for probing adapter implementation differences.
1649 **----------------------------------------------------------------
1651 u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest0, sv_ctest3,
1652 sv_ctest4, sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
1654 /*----------------------------------------------------------------
1655 ** Actual initial value of IO register bits used by the
1656 ** driver. They are loaded at initialisation according to
1657 ** features that are to be enabled.
1658 **----------------------------------------------------------------
1660 u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest0, rv_ctest3,
1661 rv_ctest4, rv_ctest5, rv_stest2;
1663 /*----------------------------------------------------------------
1664 ** Targets management.
1665 ** During reselection the ncr jumps to jump_tcb.
1666 ** The SFBR register is loaded with the encoded target id.
1668 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
1670 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1671 ** It is kind of hashcoding.
1672 **----------------------------------------------------------------
1674 struct link jump_tcb[4]; /* JUMPs for reselection */
1675 struct tcb target[MAX_TARGET]; /* Target data */
1677 /*----------------------------------------------------------------
1678 ** Virtual and physical bus addresses of the chip.
1679 **----------------------------------------------------------------
1681 void __iomem *vaddr; /* Virtual and bus address of */
1682 unsigned long paddr; /* chip's IO registers. */
1683 unsigned long paddr2; /* On-chip RAM bus address. */
1684 volatile /* Pointer to volatile for */
1685 struct ncr_reg __iomem *reg; /* memory mapped IO. */
1687 /*----------------------------------------------------------------
1688 ** SCRIPTS virtual and physical bus addresses.
1689 ** 'script' is loaded in the on-chip RAM if present.
1690 ** 'scripth' stays in main memory.
1691 **----------------------------------------------------------------
1693 struct script *script0; /* Copies of script and scripth */
1694 struct scripth *scripth0; /* relocated for this ncb. */
1695 struct scripth *scripth; /* Actual scripth virt. address */
1696 u_long p_script; /* Actual script and scripth */
1697 u_long p_scripth; /* bus addresses. */
1699 /*----------------------------------------------------------------
1700 ** General controller parameters and configuration.
1701 **----------------------------------------------------------------
1704 u_char revision_id; /* PCI device revision id */
1705 u32 irq; /* IRQ level */
1706 u32 features; /* Chip features map */
1707 u_char myaddr; /* SCSI id of the adapter */
1708 u_char maxburst; /* log base 2 of dwords burst */
1709 u_char maxwide; /* Maximum transfer width */
1710 u_char minsync; /* Minimum sync period factor */
1711 u_char maxsync; /* Maximum sync period factor */
1712 u_char maxoffs; /* Max scsi offset */
1713 u_char multiplier; /* Clock multiplier (1,2,4) */
1714 u_char clock_divn; /* Number of clock divisors */
1715 u_long clock_khz; /* SCSI clock frequency in KHz */
1717 /*----------------------------------------------------------------
1718 ** Start queue management.
1719 ** It is filled up by the host processor and accessed by the
1720 ** SCRIPTS processor in order to start SCSI commands.
1721 **----------------------------------------------------------------
1723 u16 squeueput; /* Next free slot of the queue */
1724 u16 actccbs; /* Number of allocated CCBs */
1725 u16 queuedccbs; /* Number of CCBs in start queue*/
1726 u16 queuedepth; /* Start queue depth */
1728 /*----------------------------------------------------------------
1730 **----------------------------------------------------------------
1732 struct timer_list timer; /* Timer handler link header */
1734 u_long settle_time; /* Resetting the SCSI BUS */
1736 /*----------------------------------------------------------------
1737 ** Debugging and profiling.
1738 **----------------------------------------------------------------
1740 struct ncr_reg regdump; /* Register dump */
1741 u_long regtime; /* Time it has been done */
1743 /*----------------------------------------------------------------
1744 ** Miscellaneous buffers accessed by the scripts-processor.
1745 ** They shall be DWORD aligned, because they may be read or
1746 ** written with a SCR_COPY script command.
1747 **----------------------------------------------------------------
1749 u_char msgout[8]; /* Buffer for MESSAGE OUT */
1750 u_char msgin [8]; /* Buffer for MESSAGE IN */
1751 u32 lastmsg; /* Last SCSI message sent */
1752 u_char scratch; /* Scratch for SCSI receive */
1754 /*----------------------------------------------------------------
1755 ** Miscellaneous configuration and status parameters.
1756 **----------------------------------------------------------------
1758 u_char disc; /* Diconnection allowed */
1759 u_char scsi_mode; /* Current SCSI BUS mode */
1760 u_char order; /* Tag order to use */
1761 u_char verbose; /* Verbosity for this controller*/
1762 int ncr_cache; /* Used for cache test at init. */
1763 u_long p_ncb; /* BUS address of this NCB */
1765 /*----------------------------------------------------------------
1766 ** Command completion handling.
1767 **----------------------------------------------------------------
1769 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1770 struct ccb *(ccb_done[MAX_DONE]);
1773 /*----------------------------------------------------------------
1774 ** Fields that should be removed or changed.
1775 **----------------------------------------------------------------
1777 struct ccb *ccb; /* Global CCB */
1778 struct usrcmd user; /* Command from user */
1779 volatile u_char release_stage; /* Synchronisation stage on release */
1782 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1783 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1785 /*==========================================================
1788 ** Script for NCR-Processor.
1790 ** Use ncr_script_fill() to create the variable parts.
1791 ** Use ncr_script_copy_and_bind() to make a copy and
1792 ** bind to physical addresses.
1795 **==========================================================
1797 ** We have to know the offsets of all labels before
1798 ** we reach them (for forward jumps).
1799 ** Therefore we declare a struct here.
1800 ** If you make changes inside the script,
1801 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1803 **----------------------------------------------------------
1807 ** For HP Zalon/53c720 systems, the Zalon interface
1808 ** between CPU and 53c720 does prefetches, which causes
1809 ** problems with self modifying scripts. The problem
1810 ** is overcome by calling a dummy subroutine after each
1811 ** modification, to force a refetch of the script on
1812 ** return from the subroutine.
1815 #ifdef CONFIG_NCR53C8XX_PREFETCH
1816 #define PREFETCH_FLUSH_CNT 2
1817 #define PREFETCH_FLUSH SCR_CALL, PADDRH (wait_dma),
1819 #define PREFETCH_FLUSH_CNT 0
1820 #define PREFETCH_FLUSH
1824 ** Script fragments which are loaded into the on-chip RAM
1825 ** of 825A, 875 and 895 chips.
1829 ncrcmd startpos [ 1];
1831 ncrcmd select2 [ 9 + PREFETCH_FLUSH_CNT];
1832 ncrcmd loadpos [ 4];
1833 ncrcmd send_ident [ 9];
1834 ncrcmd prepare [ 6];
1835 ncrcmd prepare2 [ 7];
1836 ncrcmd command [ 6];
1837 ncrcmd dispatch [ 32];
1839 ncrcmd no_data [ 17];
1842 ncrcmd msg_in2 [ 16];
1843 ncrcmd msg_bad [ 4];
1845 ncrcmd cleanup [ 6];
1846 ncrcmd complete [ 9];
1847 ncrcmd cleanup_ok [ 8 + PREFETCH_FLUSH_CNT];
1848 ncrcmd cleanup0 [ 1];
1849 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
1850 ncrcmd signal [ 12];
1853 ncrcmd done_pos [ 1];
1854 ncrcmd done_plug [ 2];
1855 ncrcmd done_end [ 7];
1857 ncrcmd save_dp [ 7];
1858 ncrcmd restore_dp [ 5];
1859 ncrcmd disconnect [ 10];
1860 ncrcmd msg_out [ 9];
1861 ncrcmd msg_out_done [ 7];
1863 ncrcmd reselect [ 8];
1864 ncrcmd reselected [ 8];
1865 ncrcmd resel_dsa [ 6 + PREFETCH_FLUSH_CNT];
1866 ncrcmd loadpos1 [ 4];
1867 ncrcmd resel_lun [ 6];
1868 ncrcmd resel_tag [ 6];
1869 ncrcmd jump_to_nexus [ 4 + PREFETCH_FLUSH_CNT];
1870 ncrcmd nexus_indirect [ 4];
1871 ncrcmd resel_notag [ 4];
1872 ncrcmd data_in [MAX_SCATTERL * 4];
1873 ncrcmd data_in2 [ 4];
1874 ncrcmd data_out [MAX_SCATTERL * 4];
1875 ncrcmd data_out2 [ 4];
1879 ** Script fragments which stay in main memory for all chips.
1882 ncrcmd tryloop [MAX_START*2];
1883 ncrcmd tryloop2 [ 2];
1884 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1885 ncrcmd done_queue [MAX_DONE*5];
1886 ncrcmd done_queue2 [ 2];
1888 ncrcmd select_no_atn [ 8];
1890 ncrcmd skip [ 9 + PREFETCH_FLUSH_CNT];
1892 ncrcmd par_err_data_in [ 6];
1893 ncrcmd par_err_other [ 4];
1894 ncrcmd msg_reject [ 8];
1895 ncrcmd msg_ign_residue [ 24];
1896 ncrcmd msg_extended [ 10];
1897 ncrcmd msg_ext_2 [ 10];
1898 ncrcmd msg_wdtr [ 14];
1899 ncrcmd send_wdtr [ 7];
1900 ncrcmd msg_ext_3 [ 10];
1901 ncrcmd msg_sdtr [ 14];
1902 ncrcmd send_sdtr [ 7];
1903 ncrcmd nego_bad_phase [ 4];
1904 ncrcmd msg_out_abort [ 10];
1905 ncrcmd hdata_in [MAX_SCATTERH * 4];
1906 ncrcmd hdata_in2 [ 2];
1907 ncrcmd hdata_out [MAX_SCATTERH * 4];
1908 ncrcmd hdata_out2 [ 2];
1910 ncrcmd aborttag [ 4];
1912 ncrcmd abort_resel [ 20];
1913 ncrcmd resend_ident [ 4];
1914 ncrcmd clratn_go_on [ 3];
1915 ncrcmd nxtdsp_go_on [ 1];
1916 ncrcmd sdata_in [ 8];
1917 ncrcmd data_io [ 18];
1918 ncrcmd bad_identify [ 12];
1919 ncrcmd bad_i_t_l [ 4];
1920 ncrcmd bad_i_t_l_q [ 4];
1921 ncrcmd bad_target [ 8];
1922 ncrcmd bad_status [ 8];
1923 ncrcmd start_ram [ 4 + PREFETCH_FLUSH_CNT];
1924 ncrcmd start_ram0 [ 4];
1925 ncrcmd sto_restart [ 5];
1926 ncrcmd wait_dma [ 2];
1927 ncrcmd snooptest [ 9];
1928 ncrcmd snoopend [ 2];
1931 /*==========================================================
1934 ** Function headers.
1937 **==========================================================
1940 static void ncr_alloc_ccb (struct ncb *np, u_char tn, u_char ln);
1941 static void ncr_complete (struct ncb *np, struct ccb *cp);
1942 static void ncr_exception (struct ncb *np);
1943 static void ncr_free_ccb (struct ncb *np, struct ccb *cp);
1944 static void ncr_init_ccb (struct ncb *np, struct ccb *cp);
1945 static void ncr_init_tcb (struct ncb *np, u_char tn);
1946 static struct lcb * ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln);
1947 static struct lcb * ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev);
1948 static void ncr_getclock (struct ncb *np, int mult);
1949 static void ncr_selectclock (struct ncb *np, u_char scntl3);
1950 static struct ccb *ncr_get_ccb (struct ncb *np, struct scsi_cmnd *cmd);
1951 static void ncr_chip_reset (struct ncb *np, int delay);
1952 static void ncr_init (struct ncb *np, int reset, char * msg, u_long code);
1953 static int ncr_int_sbmc (struct ncb *np);
1954 static int ncr_int_par (struct ncb *np);
1955 static void ncr_int_ma (struct ncb *np);
1956 static void ncr_int_sir (struct ncb *np);
1957 static void ncr_int_sto (struct ncb *np);
1958 static void ncr_negotiate (struct ncb* np, struct tcb* tp);
1959 static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr);
1961 static void ncr_script_copy_and_bind
1962 (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len);
1963 static void ncr_script_fill (struct script * scr, struct scripth * scripth);
1964 static int ncr_scatter (struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd);
1965 static void ncr_getsync (struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p);
1966 static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer);
1967 static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev);
1968 static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack);
1969 static int ncr_snooptest (struct ncb *np);
1970 static void ncr_timeout (struct ncb *np);
1971 static void ncr_wakeup (struct ncb *np, u_long code);
1972 static void ncr_wakeup_done (struct ncb *np);
1973 static void ncr_start_next_ccb (struct ncb *np, struct lcb * lp, int maxn);
1974 static void ncr_put_start_queue(struct ncb *np, struct ccb *cp);
1976 static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd);
1977 static struct scsi_cmnd *retrieve_from_waiting_list(int to_remove, struct ncb *np, struct scsi_cmnd *cmd);
1978 static void process_waiting_list(struct ncb *np, int sts);
1980 #define remove_from_waiting_list(np, cmd) \
1981 retrieve_from_waiting_list(1, (np), (cmd))
1982 #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
1983 #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
1985 static inline char *ncr_name (struct ncb *np)
1987 return np->inst_name;
1991 /*==========================================================
1994 ** Scripts for NCR-Processor.
1996 ** Use ncr_script_bind for binding to physical addresses.
1999 **==========================================================
2001 ** NADDR generates a reference to a field of the controller data.
2002 ** PADDR generates a reference to another part of the script.
2003 ** RADDR generates a reference to a script processor register.
2004 ** FADDR generates a reference to a script processor register
2007 **----------------------------------------------------------
2010 #define RELOC_SOFTC 0x40000000
2011 #define RELOC_LABEL 0x50000000
2012 #define RELOC_REGISTER 0x60000000
2014 #define RELOC_KVAR 0x70000000
2016 #define RELOC_LABELH 0x80000000
2017 #define RELOC_MASK 0xf0000000
2019 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
2020 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
2021 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
2022 #define RADDR(label) (RELOC_REGISTER | REG(label))
2023 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
2025 #define KVAR(which) (RELOC_KVAR | (which))
2029 #define SCRIPT_KVAR_JIFFIES (0)
2030 #define SCRIPT_KVAR_FIRST SCRIPT_KVAR_JIFFIES
2031 #define SCRIPT_KVAR_LAST SCRIPT_KVAR_JIFFIES
2033 * Kernel variables referenced in the scripts.
2034 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
2036 static void *script_kvars[] __initdata =
2037 { (void *)&jiffies };
2040 static struct script script0 __initdata = {
2041 /*--------------------------< START >-----------------------*/ {
2043 ** This NOP will be patched with LED ON
2044 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2051 SCR_FROM_REG (ctest2),
2054 ** Then jump to a certain point in tryloop.
2055 ** Due to the lack of indirect addressing the code
2056 ** is self modifying here.
2059 }/*-------------------------< STARTPOS >--------------------*/,{
2062 }/*-------------------------< SELECT >----------------------*/,{
2064 ** DSA contains the address of a scheduled
2067 ** SCRATCHA contains the address of the script,
2068 ** which starts the next entry.
2070 ** Set Initiator mode.
2072 ** (Target mode is left as an exercise for the reader)
2077 SCR_LOAD_REG (HS_REG, HS_SELECTING),
2081 ** And try to select this target.
2083 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2086 }/*-------------------------< SELECT2 >----------------------*/,{
2088 ** Now there are 4 possibilities:
2090 ** (1) The ncr loses arbitration.
2091 ** This is ok, because it will try again,
2092 ** when the bus becomes idle.
2093 ** (But beware of the timeout function!)
2095 ** (2) The ncr is reselected.
2096 ** Then the script processor takes the jump
2097 ** to the RESELECT label.
2099 ** (3) The ncr wins arbitration.
2100 ** Then it will execute SCRIPTS instruction until
2101 ** the next instruction that checks SCSI phase.
2102 ** Then will stop and wait for selection to be
2103 ** complete or selection time-out to occur.
2104 ** As a result the SCRIPTS instructions until
2105 ** LOADPOS + 2 should be executed in parallel with
2106 ** the SCSI core performing selection.
2110 ** The MESSAGE_REJECT problem seems to be due to a selection
2112 ** Wait immediately for the selection to complete.
2113 ** (2.5x behaves so)
2115 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2119 ** Next time use the next slot.
2125 ** The ncr doesn't have an indirect load
2126 ** or store command. So we have to
2127 ** copy part of the control block to a
2128 ** fixed place, where we can access it.
2130 ** We patch the address part of a
2131 ** COPY command with the DSA-register.
2137 ** Flush script prefetch if required
2141 ** then we do the actual copy.
2143 SCR_COPY (sizeof (struct head)),
2145 ** continued after the next label ...
2147 }/*-------------------------< LOADPOS >---------------------*/,{
2151 ** Wait for the next phase or the selection
2152 ** to complete or time-out.
2154 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2157 }/*-------------------------< SEND_IDENT >----------------------*/,{
2159 ** Selection complete.
2160 ** Send the IDENTIFY and SIMPLE_TAG messages
2161 ** (and the EXTENDED_SDTR message)
2163 SCR_MOVE_TBL ^ SCR_MSG_OUT,
2164 offsetof (struct dsb, smsg),
2165 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2166 PADDRH (resend_ident),
2167 SCR_LOAD_REG (scratcha, 0x80),
2172 }/*-------------------------< PREPARE >----------------------*/,{
2174 ** load the savep (saved pointer) into
2175 ** the TEMP register (actual pointer)
2178 NADDR (header.savep),
2181 ** Initialize the status registers
2184 NADDR (header.status),
2186 }/*-------------------------< PREPARE2 >---------------------*/,{
2188 ** Initialize the msgout buffer with a NOOP message.
2190 SCR_LOAD_REG (scratcha, NOP),
2201 ** Anticipate the COMMAND phase.
2202 ** This is the normal case for initial selection.
2204 SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
2207 }/*-------------------------< COMMAND >--------------------*/,{
2209 ** ... and send the command
2211 SCR_MOVE_TBL ^ SCR_COMMAND,
2212 offsetof (struct dsb, cmd),
2214 ** If status is still HS_NEGOTIATE, negotiation failed.
2215 ** We check this here, since we want to do that
2218 SCR_FROM_REG (HS_REG),
2220 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2223 }/*-----------------------< DISPATCH >----------------------*/,{
2225 ** MSG_IN is the only phase that shall be
2226 ** entered at least once for each (re)selection.
2227 ** So we test it first.
2229 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
2232 SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
2235 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
2236 ** Possible data corruption during Memory Write and Invalidate.
2237 ** This work-around resets the addressing logic prior to the
2238 ** start of the first MOVE of a DATA IN phase.
2239 ** (See Documentation/scsi/ncr53c8xx.txt for more information)
2241 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2248 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
2250 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
2252 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
2255 ** Discard one illegal phase byte, if required.
2257 SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
2262 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
2264 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
2266 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
2268 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
2273 }/*-------------------------< CLRACK >----------------------*/,{
2275 ** Terminate possible pending message phase.
2282 }/*-------------------------< NO_DATA >--------------------*/,{
2284 ** The target wants to tranfer too much data
2285 ** or in the wrong direction.
2286 ** Remember that in extended error.
2288 SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
2294 ** Discard one data byte, if required.
2296 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2298 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
2300 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2302 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2305 ** .. and repeat as required.
2312 }/*-------------------------< STATUS >--------------------*/,{
2316 SCR_MOVE_ABS (1) ^ SCR_STATUS,
2319 ** save status to scsi_status.
2320 ** mark as complete.
2322 SCR_TO_REG (SS_REG),
2324 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
2328 }/*-------------------------< MSG_IN >--------------------*/,{
2330 ** Get the first byte of the message
2331 ** and save it to SCRATCHA.
2333 ** The script processor doesn't negate the
2334 ** ACK signal after this transfer.
2336 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2338 }/*-------------------------< MSG_IN2 >--------------------*/,{
2340 ** Handle this message.
2342 SCR_JUMP ^ IFTRUE (DATA (COMMAND_COMPLETE)),
2344 SCR_JUMP ^ IFTRUE (DATA (DISCONNECT)),
2346 SCR_JUMP ^ IFTRUE (DATA (SAVE_POINTERS)),
2348 SCR_JUMP ^ IFTRUE (DATA (RESTORE_POINTERS)),
2350 SCR_JUMP ^ IFTRUE (DATA (EXTENDED_MESSAGE)),
2351 PADDRH (msg_extended),
2352 SCR_JUMP ^ IFTRUE (DATA (NOP)),
2354 SCR_JUMP ^ IFTRUE (DATA (MESSAGE_REJECT)),
2355 PADDRH (msg_reject),
2356 SCR_JUMP ^ IFTRUE (DATA (IGNORE_WIDE_RESIDUE)),
2357 PADDRH (msg_ign_residue),
2359 ** Rest of the messages left as
2362 ** Unimplemented messages:
2363 ** fall through to MSG_BAD.
2365 }/*-------------------------< MSG_BAD >------------------*/,{
2367 ** unimplemented message - reject it.
2371 SCR_LOAD_REG (scratcha, MESSAGE_REJECT),
2373 }/*-------------------------< SETMSG >----------------------*/,{
2381 }/*-------------------------< CLEANUP >-------------------*/,{
2383 ** dsa: Pointer to ccb
2384 ** or xxxxxxFF (no ccb)
2386 ** HS_REG: Host-Status (<>0!)
2390 SCR_JUMP ^ IFTRUE (DATA (0xff)),
2394 ** complete the cleanup.
2399 }/*-------------------------< COMPLETE >-----------------*/,{
2401 ** Complete message.
2403 ** Copy TEMP register to LASTP in header.
2407 NADDR (header.lastp),
2409 ** When we terminate the cycle by clearing ACK,
2410 ** the target may disconnect immediately.
2412 ** We don't want to be told of an
2413 ** "unexpected disconnect",
2414 ** so we disable this feature.
2416 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2419 ** Terminate cycle ...
2421 SCR_CLR (SCR_ACK|SCR_ATN),
2424 ** ... and wait for the disconnect.
2428 }/*-------------------------< CLEANUP_OK >----------------*/,{
2430 ** Save host status to header.
2434 NADDR (header.status),
2436 ** and copy back the header to the ccb.
2442 ** Flush script prefetch if required
2445 SCR_COPY (sizeof (struct head)),
2447 }/*-------------------------< CLEANUP0 >--------------------*/,{
2449 }/*-------------------------< SIGNAL >----------------------*/,{
2451 ** if job not completed ...
2453 SCR_FROM_REG (HS_REG),
2456 ** ... start the next command.
2458 SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
2461 ** If command resulted in not GOOD status,
2462 ** call the C code if needed.
2464 SCR_FROM_REG (SS_REG),
2466 SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
2467 PADDRH (bad_status),
2469 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
2472 ** ... signal completion to the host
2477 ** Auf zu neuen Schandtaten!
2482 #else /* defined SCSI_NCR_CCB_DONE_SUPPORT */
2485 ** ... signal completion to the host
2488 }/*------------------------< DONE_POS >---------------------*/,{
2489 PADDRH (done_queue),
2490 }/*------------------------< DONE_PLUG >--------------------*/,{
2493 }/*------------------------< DONE_END >---------------------*/,{
2502 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2504 }/*-------------------------< SAVE_DP >------------------*/,{
2507 ** Copy TEMP register to SAVEP in header.
2511 NADDR (header.savep),
2516 }/*-------------------------< RESTORE_DP >---------------*/,{
2518 ** RESTORE_DP message:
2519 ** Copy SAVEP in header to TEMP register.
2522 NADDR (header.savep),
2527 }/*-------------------------< DISCONNECT >---------------*/,{
2529 ** DISCONNECTing ...
2531 ** disable the "unexpected disconnect" feature,
2532 ** and remove the ACK signal.
2534 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2536 SCR_CLR (SCR_ACK|SCR_ATN),
2539 ** Wait for the disconnect.
2544 ** Status is: DISCONNECTED.
2546 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2551 }/*-------------------------< MSG_OUT >-------------------*/,{
2553 ** The target requests a message.
2555 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2561 ** If it was no ABORT message ...
2563 SCR_JUMP ^ IFTRUE (DATA (ABORT_TASK_SET)),
2564 PADDRH (msg_out_abort),
2566 ** ... wait for the next phase
2567 ** if it's a message out, send it again, ...
2569 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2571 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2573 ** ... else clear the message ...
2575 SCR_LOAD_REG (scratcha, NOP),
2581 ** ... and process the next phase
2585 }/*-------------------------< IDLE >------------------------*/,{
2588 ** Wait for reselect.
2589 ** This NOP will be patched with LED OFF
2590 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2594 }/*-------------------------< RESELECT >--------------------*/,{
2596 ** make the DSA invalid.
2598 SCR_LOAD_REG (dsa, 0xff),
2602 SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
2605 ** Sleep waiting for a reselection.
2606 ** If SIGP is set, special treatment.
2608 ** Zu allem bereit ..
2612 }/*-------------------------< RESELECTED >------------------*/,{
2614 ** This NOP will be patched with LED ON
2615 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2620 ** ... zu nichts zu gebrauchen ?
2622 ** load the target id into the SFBR
2623 ** and jump to the control block.
2625 ** Look at the declarations of
2630 ** to understand what's going on.
2632 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2639 }/*-------------------------< RESEL_DSA >-------------------*/,{
2641 ** Ack the IDENTIFY or TAG previously received.
2646 ** The ncr doesn't have an indirect load
2647 ** or store command. So we have to
2648 ** copy part of the control block to a
2649 ** fixed place, where we can access it.
2651 ** We patch the address part of a
2652 ** COPY command with the DSA-register.
2658 ** Flush script prefetch if required
2662 ** then we do the actual copy.
2664 SCR_COPY (sizeof (struct head)),
2666 ** continued after the next label ...
2669 }/*-------------------------< LOADPOS1 >-------------------*/,{
2673 ** The DSA contains the data structure address.
2678 }/*-------------------------< RESEL_LUN >-------------------*/,{
2680 ** come back to this point
2681 ** to get an IDENTIFY message
2682 ** Wait for a msg_in phase.
2684 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2685 SIR_RESEL_NO_MSG_IN,
2688 ** Read the data directly from the BUS DATA lines.
2689 ** This helps to support very old SCSI devices that
2690 ** may reselect without sending an IDENTIFY.
2692 SCR_FROM_REG (sbdl),
2695 ** It should be an Identify message.
2699 }/*-------------------------< RESEL_TAG >-------------------*/,{
2701 ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
2702 ** Agressive optimization, is'nt it?
2703 ** No need to test the SIMPLE TAG message, since the
2704 ** driver only supports conformant devices for tags. ;-)
2706 SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
2709 ** Read the TAG from the SIDL.
2710 ** Still an aggressive optimization. ;-)
2711 ** Compute the CCB indirect jump address which
2712 ** is (#TAG*2 & 0xfc) due to tag numbering using
2713 ** 1,3,5..MAXTAGS*2+1 actual values.
2715 SCR_REG_SFBR (sidl, SCR_SHL, 0),
2717 SCR_SFBR_REG (temp, SCR_AND, 0xfc),
2719 }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
2722 PADDR (nexus_indirect),
2724 ** Flush script prefetch if required
2728 }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
2733 }/*-------------------------< RESEL_NOTAG >-------------------*/,{
2736 ** Read an throw away the IDENTIFY.
2738 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2741 PADDR (jump_to_nexus),
2742 }/*-------------------------< DATA_IN >--------------------*/,{
2744 ** Because the size depends on the
2745 ** #define MAX_SCATTERL parameter,
2746 ** it is filled in at runtime.
2748 ** ##===========< i=0; i<MAX_SCATTERL >=========
2749 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2750 ** || PADDR (dispatch),
2751 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
2752 ** || offsetof (struct dsb, data[ i]),
2753 ** ##==========================================
2755 **---------------------------------------------------------
2758 }/*-------------------------< DATA_IN2 >-------------------*/,{
2763 }/*-------------------------< DATA_OUT >--------------------*/,{
2765 ** Because the size depends on the
2766 ** #define MAX_SCATTERL parameter,
2767 ** it is filled in at runtime.
2769 ** ##===========< i=0; i<MAX_SCATTERL >=========
2770 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2771 ** || PADDR (dispatch),
2772 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
2773 ** || offsetof (struct dsb, data[ i]),
2774 ** ##==========================================
2776 **---------------------------------------------------------
2779 }/*-------------------------< DATA_OUT2 >-------------------*/,{
2784 }/*--------------------------------------------------------*/
2787 static struct scripth scripth0 __initdata = {
2788 /*-------------------------< TRYLOOP >---------------------*/{
2790 ** Start the next entry.
2791 ** Called addresses point to the launch script in the CCB.
2792 ** They are patched by the main processor.
2794 ** Because the size depends on the
2795 ** #define MAX_START parameter, it is filled
2798 **-----------------------------------------------------------
2800 ** ##===========< I=0; i<MAX_START >===========
2803 ** ##==========================================
2805 **-----------------------------------------------------------
2808 }/*------------------------< TRYLOOP2 >---------------------*/,{
2812 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2814 }/*------------------------< DONE_QUEUE >-------------------*/,{
2816 ** Copy the CCB address to the next done entry.
2817 ** Because the size depends on the
2818 ** #define MAX_DONE parameter, it is filled
2821 **-----------------------------------------------------------
2823 ** ##===========< I=0; i<MAX_DONE >===========
2824 ** || SCR_COPY (sizeof(struct ccb *),
2825 ** || NADDR (header.cp),
2826 ** || NADDR (ccb_done[i]),
2828 ** || PADDR (done_end),
2829 ** ##==========================================
2831 **-----------------------------------------------------------
2834 }/*------------------------< DONE_QUEUE2 >------------------*/,{
2836 PADDRH (done_queue),
2838 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2839 }/*------------------------< SELECT_NO_ATN >-----------------*/,{
2841 ** Set Initiator mode.
2842 ** And try to select this target without ATN.
2847 SCR_LOAD_REG (HS_REG, HS_SELECTING),
2849 SCR_SEL_TBL ^ offsetof (struct dsb, select),
2854 }/*-------------------------< CANCEL >------------------------*/,{
2856 SCR_LOAD_REG (scratcha, HS_ABORTED),
2860 }/*-------------------------< SKIP >------------------------*/,{
2861 SCR_LOAD_REG (scratcha, 0),
2864 ** This entry has been canceled.
2865 ** Next time use the next slot.
2871 ** The ncr doesn't have an indirect load
2872 ** or store command. So we have to
2873 ** copy part of the control block to a
2874 ** fixed place, where we can access it.
2876 ** We patch the address part of a
2877 ** COPY command with the DSA-register.
2883 ** Flush script prefetch if required
2887 ** then we do the actual copy.
2889 SCR_COPY (sizeof (struct head)),
2891 ** continued after the next label ...
2893 }/*-------------------------< SKIP2 >---------------------*/,{
2897 ** Initialize the status registers
2900 NADDR (header.status),
2903 ** Force host status.
2905 SCR_FROM_REG (scratcha),
2907 SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
2909 SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
2913 SCR_TO_REG (HS_REG),
2915 SCR_LOAD_REG (SS_REG, S_GOOD),
2920 },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
2922 ** Ignore all data in byte, until next phase
2924 SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2925 PADDRH (par_err_other),
2926 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2930 },/*-------------------------< PAR_ERR_OTHER >------------------*/{
2934 SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
2937 ** jump to dispatcher.
2941 }/*-------------------------< MSG_REJECT >---------------*/,{
2943 ** If a negotiation was in progress,
2944 ** negotiation failed.
2945 ** Otherwise, let the C code print
2948 SCR_FROM_REG (HS_REG),
2950 SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
2951 SIR_REJECT_RECEIVED,
2952 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2957 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2963 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2966 ** get residue size.
2968 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2971 ** Size is 0 .. ignore message.
2973 SCR_JUMP ^ IFTRUE (DATA (0)),
2976 ** Size is not 1 .. have to interrupt.
2978 SCR_JUMPR ^ IFFALSE (DATA (1)),
2981 ** Check for residue byte in swide register
2983 SCR_FROM_REG (scntl2),
2985 SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2988 ** There IS data in the swide register.
2991 SCR_REG_REG (scntl2, SCR_OR, WSR),
2996 ** Load again the size to the sfbr register.
2998 SCR_FROM_REG (scratcha),
3005 }/*-------------------------< MSG_EXTENDED >-------------*/,{
3011 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3016 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3020 SCR_JUMP ^ IFTRUE (DATA (3)),
3022 SCR_JUMP ^ IFFALSE (DATA (2)),
3024 }/*-------------------------< MSG_EXT_2 >----------------*/,{
3027 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3030 ** get extended message code.
3032 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3034 SCR_JUMP ^ IFTRUE (DATA (EXTENDED_WDTR)),
3037 ** unknown extended message
3041 }/*-------------------------< MSG_WDTR >-----------------*/,{
3044 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3047 ** get data bus width
3049 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3052 ** let the host do the real work.
3057 ** let the target fetch our answer.
3063 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3064 PADDRH (nego_bad_phase),
3066 }/*-------------------------< SEND_WDTR >----------------*/,{
3068 ** Send the EXTENDED_WDTR
3070 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
3076 PADDR (msg_out_done),
3078 }/*-------------------------< MSG_EXT_3 >----------------*/,{
3081 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3084 ** get extended message code.
3086 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3088 SCR_JUMP ^ IFTRUE (DATA (EXTENDED_SDTR)),
3091 ** unknown extended message
3096 }/*-------------------------< MSG_SDTR >-----------------*/,{
3099 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3102 ** get period and offset
3104 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
3107 ** let the host do the real work.
3112 ** let the target fetch our answer.
3118 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3119 PADDRH (nego_bad_phase),
3121 }/*-------------------------< SEND_SDTR >-------------*/,{
3123 ** Send the EXTENDED_SDTR
3125 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
3131 PADDR (msg_out_done),
3133 }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
3139 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
3141 ** After ABORT message,
3143 ** expect an immediate disconnect, ...
3145 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3147 SCR_CLR (SCR_ACK|SCR_ATN),
3152 ** ... and set the status to "ABORTED"
3154 SCR_LOAD_REG (HS_REG, HS_ABORTED),
3159 }/*-------------------------< HDATA_IN >-------------------*/,{
3161 ** Because the size depends on the
3162 ** #define MAX_SCATTERH parameter,
3163 ** it is filled in at runtime.
3165 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3166 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3167 ** || PADDR (dispatch),
3168 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
3169 ** || offsetof (struct dsb, data[ i]),
3170 ** ##===================================================
3172 **---------------------------------------------------------
3175 }/*-------------------------< HDATA_IN2 >------------------*/,{
3179 }/*-------------------------< HDATA_OUT >-------------------*/,{
3181 ** Because the size depends on the
3182 ** #define MAX_SCATTERH parameter,
3183 ** it is filled in at runtime.
3185 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3186 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3187 ** || PADDR (dispatch),
3188 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
3189 ** || offsetof (struct dsb, data[ i]),
3190 ** ##===================================================
3192 **---------------------------------------------------------
3195 }/*-------------------------< HDATA_OUT2 >------------------*/,{
3199 }/*-------------------------< RESET >----------------------*/,{
3201 ** Send a TARGET_RESET message if bad IDENTIFY
3202 ** received on reselection.
3204 SCR_LOAD_REG (scratcha, ABORT_TASK),
3207 PADDRH (abort_resel),
3208 }/*-------------------------< ABORTTAG >-------------------*/,{
3210 ** Abort a wrong tag received on reselection.
3212 SCR_LOAD_REG (scratcha, ABORT_TASK),
3215 PADDRH (abort_resel),
3216 }/*-------------------------< ABORT >----------------------*/,{
3218 ** Abort a reselection when no active CCB.
3220 SCR_LOAD_REG (scratcha, ABORT_TASK_SET),
3222 }/*-------------------------< ABORT_RESEL >----------------*/,{
3232 ** we expect an immediate disconnect
3234 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3236 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
3241 SCR_CLR (SCR_ACK|SCR_ATN),
3247 }/*-------------------------< RESEND_IDENT >-------------------*/,{
3249 ** The target stays in MSG OUT phase after having acked
3250 ** Identify [+ Tag [+ Extended message ]]. Targets shall
3251 ** behave this way on parity error.
3252 ** We must send it again all the messages.
3254 SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
3255 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
3258 }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
3262 }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
3264 }/*-------------------------< SDATA_IN >-------------------*/,{
3265 SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3267 SCR_MOVE_TBL ^ SCR_DATA_IN,
3268 offsetof (struct dsb, sense),
3273 }/*-------------------------< DATA_IO >--------------------*/,{
3275 ** We jump here if the data direction was unknown at the
3276 ** time we had to queue the command to the scripts processor.
3277 ** Pointers had been set as follow in this situation:
3278 ** savep --> DATA_IO
3279 ** lastp --> start pointer when DATA_IN
3280 ** goalp --> goal pointer when DATA_IN
3281 ** wlastp --> start pointer when DATA_OUT
3282 ** wgoalp --> goal pointer when DATA_OUT
3283 ** This script sets savep/lastp/goalp according to the
3284 ** direction chosen by the target.
3286 SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
3289 ** Direction is DATA IN.
3290 ** Warning: we jump here, even when phase is DATA OUT.
3293 NADDR (header.lastp),
3294 NADDR (header.savep),
3297 ** Jump to the SCRIPTS according to actual direction.
3300 NADDR (header.savep),
3305 ** Direction is DATA OUT.
3308 NADDR (header.wlastp),
3309 NADDR (header.lastp),
3311 NADDR (header.wgoalp),
3312 NADDR (header.goalp),
3315 }/*-------------------------< BAD_IDENTIFY >---------------*/,{
3317 ** If message phase but not an IDENTIFY,
3318 ** get some help from the C code.
3319 ** Old SCSI device may behave so.
3321 SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
3324 SIR_RESEL_NO_IDENTIFY,
3328 ** Message is an IDENTIFY, but lun is unknown.
3329 ** Read the message, since we got it directly
3330 ** from the SCSI BUS data lines.
3331 ** Signal problem to C code for logging the event.
3332 ** Send an ABORT_TASK_SET to clear all pending tasks.
3336 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3340 }/*-------------------------< BAD_I_T_L >------------------*/,{
3342 ** We donnot have a task for that I_T_L.
3343 ** Signal problem to C code for logging the event.
3344 ** Send an ABORT_TASK_SET message.
3347 SIR_RESEL_BAD_I_T_L,
3350 }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
3352 ** We donnot have a task that matches the tag.
3353 ** Signal problem to C code for logging the event.
3354 ** Send an ABORT_TASK message.
3357 SIR_RESEL_BAD_I_T_L_Q,
3360 }/*-------------------------< BAD_TARGET >-----------------*/,{
3362 ** We donnot know the target that reselected us.
3363 ** Grab the first message if any (IDENTIFY).
3364 ** Signal problem to C code for logging the event.
3365 ** TARGET_RESET message.
3368 SIR_RESEL_BAD_TARGET,
3369 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
3371 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3375 }/*-------------------------< BAD_STATUS >-----------------*/,{
3377 ** If command resulted in either QUEUE FULL,
3378 ** CHECK CONDITION or COMMAND TERMINATED,
3381 SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
3383 SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
3385 SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
3389 }/*-------------------------< START_RAM >-------------------*/,{
3391 ** Load the script into on-chip RAM,
3392 ** and jump to start point.
3396 PADDRH (start_ram0),
3398 ** Flush script prefetch if required
3401 SCR_COPY (sizeof (struct script)),
3402 }/*-------------------------< START_RAM0 >--------------------*/,{
3407 }/*-------------------------< STO_RESTART >-------------------*/,{
3410 ** Repair start queue (e.g. next time use the next slot)
3411 ** and jump to start point.
3418 }/*-------------------------< WAIT_DMA >-------------------*/,{
3420 ** For HP Zalon/53c720 systems, the Zalon interface
3421 ** between CPU and 53c720 does prefetches, which causes
3422 ** problems with self modifying scripts. The problem
3423 ** is overcome by calling a dummy subroutine after each
3424 ** modification, to force a refetch of the script on
3425 ** return from the subroutine.
3429 }/*-------------------------< SNOOPTEST >-------------------*/,{
3431 ** Read the variable.
3437 ** Write the variable.
3443 ** Read back the variable.
3448 }/*-------------------------< SNOOPEND >-------------------*/,{
3454 }/*--------------------------------------------------------*/
3457 /*==========================================================
3460 ** Fill in #define dependent parts of the script
3463 **==========================================================
3466 void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
3472 for (i=0; i<MAX_START; i++) {
3477 BUG_ON((u_long)p != (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
3479 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
3481 p = scrh->done_queue;
3482 for (i = 0; i<MAX_DONE; i++) {
3483 *p++ =SCR_COPY (sizeof(struct ccb *));
3484 *p++ =NADDR (header.cp);
3485 *p++ =NADDR (ccb_done[i]);
3487 *p++ =PADDR (done_end);
3490 BUG_ON((u_long)p != (u_long)&scrh->done_queue+sizeof(scrh->done_queue));
3492 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
3495 for (i=0; i<MAX_SCATTERH; i++) {
3496 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3497 *p++ =PADDR (dispatch);
3498 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3499 *p++ =offsetof (struct dsb, data[i]);
3502 BUG_ON((u_long)p != (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
3505 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3506 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3507 *p++ =PADDR (dispatch);
3508 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3509 *p++ =offsetof (struct dsb, data[i]);
3512 BUG_ON((u_long)p != (u_long)&scr->data_in + sizeof (scr->data_in));
3514 p = scrh->hdata_out;
3515 for (i=0; i<MAX_SCATTERH; i++) {
3516 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3517 *p++ =PADDR (dispatch);
3518 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3519 *p++ =offsetof (struct dsb, data[i]);
3522 BUG_ON((u_long)p != (u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
3525 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3526 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3527 *p++ =PADDR (dispatch);
3528 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3529 *p++ =offsetof (struct dsb, data[i]);
3532 BUG_ON((u_long) p != (u_long)&scr->data_out + sizeof (scr->data_out));
3535 /*==========================================================
3538 ** Copy and rebind a script.
3541 **==========================================================
3545 ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
3547 ncrcmd opcode, new, old, tmp1, tmp2;
3548 ncrcmd *start, *end;
3558 *dst++ = cpu_to_scr(opcode);
3561 ** If we forget to change the length
3562 ** in struct script, a field will be
3563 ** padded with 0. This is an illegal
3568 printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
3569 ncr_name(np), (int) (src-start-1));
3573 if (DEBUG_FLAGS & DEBUG_SCRIPT)
3574 printk (KERN_DEBUG "%p: <%x>\n",
3575 (src-1), (unsigned)opcode);
3578 ** We don't have to decode ALL commands
3580 switch (opcode >> 28) {
3584 ** COPY has TWO arguments.
3589 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3594 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3597 if ((tmp1 ^ tmp2) & 3) {
3598 printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
3599 ncr_name(np), (int) (src-start-1));
3603 ** If PREFETCH feature not enabled, remove
3604 ** the NO FLUSH bit if present.
3606 if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
3607 dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
3614 ** MOVE (absolute address)
3622 ** don't relocate if relative :-)
3624 if (opcode & 0x00800000)
3646 switch (old & RELOC_MASK) {
3647 case RELOC_REGISTER:
3648 new = (old & ~RELOC_MASK) + np->paddr;
3651 new = (old & ~RELOC_MASK) + np->p_script;
3654 new = (old & ~RELOC_MASK) + np->p_scripth;
3657 new = (old & ~RELOC_MASK) + np->p_ncb;
3661 if (((old & ~RELOC_MASK) <
3662 SCRIPT_KVAR_FIRST) ||
3663 ((old & ~RELOC_MASK) >
3665 panic("ncr KVAR out of range");
3666 new = vtophys(script_kvars[old &
3671 /* Don't relocate a 0 address. */
3678 panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
3682 *dst++ = cpu_to_scr(new);
3685 *dst++ = cpu_to_scr(*src++);
3691 ** Linux host data structure
3698 #define PRINT_ADDR(cmd, arg...) dev_info(&cmd->device->sdev_gendev , ## arg)
3700 static void ncr_print_msg(struct ccb *cp, char *label, u_char *msg)
3702 PRINT_ADDR(cp->cmd, "%s: ", label);
3708 /*==========================================================
3710 ** NCR chip clock divisor table.
3711 ** Divisors are multiplied by 10,000,000 in order to make
3712 ** calculations more simple.
3714 **==========================================================
3718 static u_long div_10M[] =
3719 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3722 /*===============================================================
3724 ** Prepare io register values used by ncr_init() according
3725 ** to selected and supported features.
3727 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3728 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3729 ** We use log base 2 (burst length) as internal code, with
3730 ** value 0 meaning "burst disabled".
3732 **===============================================================
3736 * Burst length from burst code.
3738 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3741 * Burst code from io register bits. Burst enable is ctest0 for c720
3743 #define burst_code(dmode, ctest0) \
3744 (ctest0) & 0x80 ? 0 : (((dmode) & 0xc0) >> 6) + 1
3747 * Set initial io register bits from burst code.
3749 static inline void ncr_init_burst(struct ncb *np, u_char bc)
3751 u_char *be = &np->rv_ctest0;
3753 np->rv_dmode &= ~(0x3 << 6);
3754 np->rv_ctest5 &= ~0x4;
3760 np->rv_dmode |= ((bc & 0x3) << 6);
3761 np->rv_ctest5 |= (bc & 0x4);
3765 static void __init ncr_prepare_setting(struct ncb *np)
3772 ** Save assumed BIOS setting
3775 np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
3776 np->sv_scntl3 = INB(nc_scntl3) & 0x07;
3777 np->sv_dmode = INB(nc_dmode) & 0xce;
3778 np->sv_dcntl = INB(nc_dcntl) & 0xa8;
3779 np->sv_ctest0 = INB(nc_ctest0) & 0x84;
3780 np->sv_ctest3 = INB(nc_ctest3) & 0x01;
3781 np->sv_ctest4 = INB(nc_ctest4) & 0x80;
3782 np->sv_ctest5 = INB(nc_ctest5) & 0x24;
3783 np->sv_gpcntl = INB(nc_gpcntl);
3784 np->sv_stest2 = INB(nc_stest2) & 0x20;
3785 np->sv_stest4 = INB(nc_stest4);
3791 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
3794 * Guess the frequency of the chip's clock.
3796 if (np->features & FE_ULTRA)
3797 np->clock_khz = 80000;
3799 np->clock_khz = 40000;
3802 * Get the clock multiplier factor.
3804 if (np->features & FE_QUAD)
3806 else if (np->features & FE_DBLR)
3812 * Measure SCSI clock frequency for chips
3813 * it may vary from assumed one.
3815 if (np->features & FE_VARCLK)
3816 ncr_getclock(np, np->multiplier);
3819 * Divisor to be used for async (timer pre-scaler).
3821 i = np->clock_divn - 1;
3823 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
3828 np->rv_scntl3 = i+1;
3831 * Minimum synchronous period factor supported by the chip.
3832 * Btw, 'period' is in tenths of nanoseconds.
3835 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
3836 if (period <= 250) np->minsync = 10;
3837 else if (period <= 303) np->minsync = 11;
3838 else if (period <= 500) np->minsync = 12;
3839 else np->minsync = (period + 40 - 1) / 40;
3842 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3845 if (np->minsync < 25 && !(np->features & FE_ULTRA))
3849 * Maximum synchronous period factor supported by the chip.
3852 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
3853 np->maxsync = period > 2540 ? 254 : period / 10;
3856 ** Prepare initial value of other IO registers
3858 #if defined SCSI_NCR_TRUST_BIOS_SETTING
3859 np->rv_scntl0 = np->sv_scntl0;
3860 np->rv_dmode = np->sv_dmode;
3861 np->rv_dcntl = np->sv_dcntl;
3862 np->rv_ctest0 = np->sv_ctest0;
3863 np->rv_ctest3 = np->sv_ctest3;
3864 np->rv_ctest4 = np->sv_ctest4;
3865 np->rv_ctest5 = np->sv_ctest5;
3866 burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
3870 ** Select burst length (dwords)
3872 burst_max = driver_setup.burst_max;
3873 if (burst_max == 255)
3874 burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
3877 if (burst_max > np->maxburst)
3878 burst_max = np->maxburst;
3881 ** Select all supported special features
3883 if (np->features & FE_ERL)
3884 np->rv_dmode |= ERL; /* Enable Read Line */
3885 if (np->features & FE_BOF)
3886 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
3887 if (np->features & FE_ERMP)
3888 np->rv_dmode |= ERMP; /* Enable Read Multiple */
3889 if (np->features & FE_PFEN)
3890 np->rv_dcntl |= PFEN; /* Prefetch Enable */
3891 if (np->features & FE_CLSE)
3892 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
3893 if (np->features & FE_WRIE)
3894 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
3895 if (np->features & FE_DFS)
3896 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
3897 if (np->features & FE_MUX)
3898 np->rv_ctest4 |= MUX; /* Host bus multiplex mode */
3899 if (np->features & FE_EA)
3900 np->rv_dcntl |= EA; /* Enable ACK */
3901 if (np->features & FE_EHP)
3902 np->rv_ctest0 |= EHP; /* Even host parity */
3905 ** Select some other
3907 if (driver_setup.master_parity)
3908 np->rv_ctest4 |= MPEE; /* Master parity checking */
3909 if (driver_setup.scsi_parity)
3910 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
3913 ** Get SCSI addr of host adapter (set by bios?).
3915 if (np->myaddr == 255) {
3916 np->myaddr = INB(nc_scid) & 0x07;
3918 np->myaddr = SCSI_NCR_MYADDR;
3921 #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
3924 * Prepare initial io register bits for burst length
3926 ncr_init_burst(np, burst_max);
3929 ** Set SCSI BUS mode.
3931 ** - ULTRA2 chips (895/895A/896) report the current
3932 ** BUS mode through the STEST4 IO register.
3933 ** - For previous generation chips (825/825A/875),
3934 ** user has to tell us how to check against HVD,
3935 ** since a 100% safe algorithm is not possible.
3937 np->scsi_mode = SMODE_SE;
3938 if (np->features & FE_DIFF) {
3939 switch(driver_setup.diff_support) {
3940 case 4: /* Trust previous settings if present, then GPIO3 */
3941 if (np->sv_scntl3) {
3942 if (np->sv_stest2 & 0x20)
3943 np->scsi_mode = SMODE_HVD;
3946 case 3: /* SYMBIOS controllers report HVD through GPIO3 */
3947 if (INB(nc_gpreg) & 0x08)
3949 case 2: /* Set HVD unconditionally */
3950 np->scsi_mode = SMODE_HVD;
3951 case 1: /* Trust previous settings for HVD */
3952 if (np->sv_stest2 & 0x20)
3953 np->scsi_mode = SMODE_HVD;
3955 default:/* Don't care about HVD */
3959 if (np->scsi_mode == SMODE_HVD)
3960 np->rv_stest2 |= 0x20;
3963 ** Set LED support from SCRIPTS.
3964 ** Ignore this feature for boards known to use a
3965 ** specific GPIO wiring and for the 895A or 896
3966 ** that drive the LED directly.
3967 ** Also probe initial setting of GPIO0 as output.
3969 if ((driver_setup.led_pin) &&
3970 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
3971 np->features |= FE_LED0;
3976 switch(driver_setup.irqm & 3) {
3978 np->rv_dcntl |= IRQM;
3981 np->rv_dcntl |= (np->sv_dcntl & IRQM);
3988 ** Configure targets according to driver setup.
3989 ** Allow to override sync, wide and NOSCAN from
3990 ** boot command line.
3992 for (i = 0 ; i < MAX_TARGET ; i++) {
3993 struct tcb *tp = &np->target[i];
3995 tp->usrsync = driver_setup.default_sync;
3996 tp->usrwide = driver_setup.max_wide;
3997 tp->usrtags = MAX_TAGS;
3998 tp->period = 0xffff;
3999 if (!driver_setup.disconnection)
4000 np->target[i].usrflag = UF_NODISC;
4004 ** Announce all that stuff to user.
4007 printk(KERN_INFO "%s: ID %d, Fast-%d%s%s\n", ncr_name(np),
4009 np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
4010 (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
4011 (np->rv_stest2 & 0x20) ? ", Differential" : "");
4013 if (bootverbose > 1) {
4014 printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4015 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4016 ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
4017 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
4019 printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4020 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4021 ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
4022 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
4025 if (bootverbose && np->paddr2)
4026 printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
4027 ncr_name(np), np->paddr2);
4030 /*==========================================================
4033 ** Done SCSI commands list management.
4035 ** We donnot enter the scsi_done() callback immediately
4036 ** after a command has been seen as completed but we
4037 ** insert it into a list which is flushed outside any kind
4038 ** of driver critical section.
4039 ** This allows to do minimal stuff under interrupt and
4040 ** inside critical sections and to also avoid locking up
4041 ** on recursive calls to driver entry points under SMP.
4042 ** In fact, the only kernel point which is entered by the
4043 ** driver with a driver lock set is kmalloc(GFP_ATOMIC)
4044 ** that shall not reenter the driver under any circumstances,
4047 **==========================================================
4049 static inline void ncr_queue_done_cmd(struct ncb *np, struct scsi_cmnd *cmd)
4051 unmap_scsi_data(np, cmd);
4052 cmd->host_scribble = (char *) np->done_list;
4053 np->done_list = cmd;
4056 static inline void ncr_flush_done_cmds(struct scsi_cmnd *lcmd)
4058 struct scsi_cmnd *cmd;
4062 lcmd = (struct scsi_cmnd *) cmd->host_scribble;
4063 cmd->scsi_done(cmd);
4067 /*==========================================================
4070 ** Prepare the next negotiation message if needed.
4072 ** Fill in the part of message buffer that contains the
4073 ** negotiation and the nego_status field of the CCB.
4074 ** Returns the size of the message in bytes.
4077 **==========================================================
4081 static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
4083 struct tcb *tp = &np->target[cp->target];
4086 struct scsi_target *starget = tp->starget;
4088 /* negotiate wide transfers ? */
4089 if (!tp->widedone) {
4090 if (spi_support_wide(starget)) {
4096 /* negotiate synchronous transfers? */
4097 if (!nego && !tp->period) {
4098 if (spi_support_sync(starget)) {
4102 dev_info(&starget->dev, "target did not report SYNC.\n");
4108 msgptr[msglen++] = EXTENDED_MESSAGE;
4109 msgptr[msglen++] = 3;
4110 msgptr[msglen++] = EXTENDED_SDTR;
4111 msgptr[msglen++] = tp->maxoffs ? tp->minsync : 0;
4112 msgptr[msglen++] = tp->maxoffs;
4115 msgptr[msglen++] = EXTENDED_MESSAGE;
4116 msgptr[msglen++] = 2;
4117 msgptr[msglen++] = EXTENDED_WDTR;
4118 msgptr[msglen++] = tp->usrwide;
4122 cp->nego_status = nego;
4126 if (DEBUG_FLAGS & DEBUG_NEGO) {
4127 ncr_print_msg(cp, nego == NS_WIDE ?
4128 "wide msgout":"sync_msgout", msgptr);
4137 /*==========================================================
4140 ** Start execution of a SCSI command.
4141 ** This is called from the generic SCSI driver.
4144 **==========================================================
4146 static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
4148 struct scsi_device *sdev = cmd->device;
4149 struct tcb *tp = &np->target[sdev->id];
4150 struct lcb *lp = tp->lp[sdev->lun];
4154 u_char idmsg, *msgptr;
4159 /*---------------------------------------------
4161 ** Some shortcuts ...
4163 **---------------------------------------------
4165 if ((sdev->id == np->myaddr ) ||
4166 (sdev->id >= MAX_TARGET) ||
4167 (sdev->lun >= MAX_LUN )) {
4168 return(DID_BAD_TARGET);
4171 /*---------------------------------------------
4173 ** Complete the 1st TEST UNIT READY command
4174 ** with error condition if the device is
4175 ** flagged NOSCAN, in order to speed up
4178 **---------------------------------------------
4180 if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12) &&
4181 (tp->usrflag & UF_NOSCAN)) {
4182 tp->usrflag &= ~UF_NOSCAN;
4183 return DID_BAD_TARGET;
4186 if (DEBUG_FLAGS & DEBUG_TINY) {
4187 PRINT_ADDR(cmd, "CMD=%x ", cmd->cmnd[0]);
4190 /*---------------------------------------------------
4192 ** Assign a ccb / bind cmd.
4193 ** If resetting, shorten settle_time if necessary
4194 ** in order to avoid spurious timeouts.
4195 ** If resetting or no free ccb,
4196 ** insert cmd into the waiting list.
4198 **----------------------------------------------------
4200 if (np->settle_time && cmd->timeout_per_command >= HZ) {
4201 u_long tlimit = jiffies + cmd->timeout_per_command - HZ;
4202 if (time_after(np->settle_time, tlimit))
4203 np->settle_time = tlimit;
4206 if (np->settle_time || !(cp=ncr_get_ccb (np, cmd))) {
4207 insert_into_waiting_list(np, cmd);
4212 /*----------------------------------------------------
4214 ** Build the identify / tag / sdtr message
4216 **----------------------------------------------------
4219 idmsg = IDENTIFY(0, sdev->lun);
4221 if (cp ->tag != NO_TAG ||
4222 (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
4225 msgptr = cp->scsi_smsg;
4227 msgptr[msglen++] = idmsg;
4229 if (cp->tag != NO_TAG) {
4230 char order = np->order;
4233 ** Force ordered tag if necessary to avoid timeouts
4234 ** and to preserve interactivity.
4236 if (lp && time_after(jiffies, lp->tags_stime)) {
4237 if (lp->tags_smap) {
4238 order = ORDERED_QUEUE_TAG;
4239 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
4241 "ordered tag forced.\n");
4244 lp->tags_stime = jiffies + 3*HZ;
4245 lp->tags_smap = lp->tags_umap;
4250 ** Ordered write ops, unordered read ops.
4252 switch (cmd->cmnd[0]) {
4253 case 0x08: /* READ_SMALL (6) */
4254 case 0x28: /* READ_BIG (10) */
4255 case 0xa8: /* READ_HUGE (12) */
4256 order = SIMPLE_QUEUE_TAG;
4259 order = ORDERED_QUEUE_TAG;
4262 msgptr[msglen++] = order;
4264 ** Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
4265 ** since we may have to deal with devices that have
4266 ** problems with #TAG 0 or too great #TAG numbers.
4268 msgptr[msglen++] = (cp->tag << 1) + 1;
4271 /*----------------------------------------------------
4273 ** Build the data descriptors
4275 **----------------------------------------------------
4278 direction = cmd->sc_data_direction;
4279 if (direction != DMA_NONE) {
4280 segments = ncr_scatter(np, cp, cp->cmd);
4282 ncr_free_ccb(np, cp);
4291 /*---------------------------------------------------
4293 ** negotiation required?
4295 ** (nego_status is filled by ncr_prepare_nego())
4297 **---------------------------------------------------
4300 cp->nego_status = 0;
4302 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
4303 msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
4306 /*----------------------------------------------------
4308 ** Determine xfer direction.
4310 **----------------------------------------------------
4313 direction = DMA_NONE;
4316 ** If data direction is BIDIRECTIONAL, speculate FROM_DEVICE
4317 ** but prepare alternate pointers for TO_DEVICE in case
4318 ** of our speculation will be just wrong.
4319 ** SCRIPTS will swap values if needed.
4322 case DMA_BIDIRECTIONAL:
4324 goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
4325 if (segments <= MAX_SCATTERL)
4326 lastp = goalp - 8 - (segments * 16);
4328 lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
4329 lastp -= (segments - MAX_SCATTERL) * 16;
4331 if (direction != DMA_BIDIRECTIONAL)
4333 cp->phys.header.wgoalp = cpu_to_scr(goalp);
4334 cp->phys.header.wlastp = cpu_to_scr(lastp);
4336 case DMA_FROM_DEVICE:
4337 goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
4338 if (segments <= MAX_SCATTERL)
4339 lastp = goalp - 8 - (segments * 16);
4341 lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
4342 lastp -= (segments - MAX_SCATTERL) * 16;
4347 lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
4352 ** Set all pointers values needed by SCRIPTS.
4353 ** If direction is unknown, start at data_io.
4355 cp->phys.header.lastp = cpu_to_scr(lastp);
4356 cp->phys.header.goalp = cpu_to_scr(goalp);
4358 if (direction == DMA_BIDIRECTIONAL)
4359 cp->phys.header.savep =
4360 cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
4362 cp->phys.header.savep= cpu_to_scr(lastp);
4365 ** Save the initial data pointer in order to be able
4366 ** to redo the command.
4368 cp->startp = cp->phys.header.savep;
4370 /*----------------------------------------------------
4374 **----------------------------------------------------
4377 ** physical -> virtual backlink
4378 ** Generic SCSI command
4384 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
4385 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
4389 cp->phys.select.sel_id = sdev_id(sdev);
4390 cp->phys.select.sel_scntl3 = tp->wval;
4391 cp->phys.select.sel_sxfer = tp->sval;
4395 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
4396 cp->phys.smsg.size = cpu_to_scr(msglen);
4401 memcpy(cp->cdb_buf, cmd->cmnd, min_t(int, cmd->cmd_len, sizeof(cp->cdb_buf)));
4402 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
4403 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
4408 cp->actualquirks = 0;
4409 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
4410 cp->scsi_status = S_ILLEGAL;
4411 cp->parity_status = 0;
4413 cp->xerr_status = XE_OK;
4415 cp->sync_status = tp->sval;
4416 cp->wide_status = tp->wval;
4419 /*----------------------------------------------------
4421 ** Critical region: start this job.
4423 **----------------------------------------------------
4426 /* activate this job. */
4427 cp->magic = CCB_MAGIC;
4430 ** insert next CCBs into start queue.
4431 ** 2 max at a time is enough to flush the CCB wait queue.
4435 ncr_start_next_ccb(np, lp, 2);
4437 ncr_put_start_queue(np, cp);
4439 /* Command is successfully queued. */
4445 /*==========================================================
4448 ** Insert a CCB into the start queue and wake up the
4449 ** SCRIPTS processor.
4452 **==========================================================
4455 static void ncr_start_next_ccb(struct ncb *np, struct lcb *lp, int maxn)
4457 struct list_head *qp;
4463 while (maxn-- && lp->queuedccbs < lp->queuedepth) {
4464 qp = ncr_list_pop(&lp->wait_ccbq);
4468 cp = list_entry(qp, struct ccb, link_ccbq);
4469 list_add_tail(qp, &lp->busy_ccbq);
4470 lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
4471 cpu_to_scr(CCB_PHYS (cp, restart));
4472 ncr_put_start_queue(np, cp);
4476 static void ncr_put_start_queue(struct ncb *np, struct ccb *cp)
4481 ** insert into start queue.
4483 if (!np->squeueput) np->squeueput = 1;
4484 qidx = np->squeueput + 2;
4485 if (qidx >= MAX_START + MAX_START) qidx = 1;
4487 np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
4489 np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
4491 np->squeueput = qidx;
4495 if (DEBUG_FLAGS & DEBUG_QUEUE)
4496 printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
4499 ** Script processor may be waiting for reselect.
4503 OUTB (nc_istat, SIGP);
4507 static int ncr_reset_scsi_bus(struct ncb *np, int enab_int, int settle_delay)
4512 np->settle_time = jiffies + settle_delay * HZ;
4514 if (bootverbose > 1)
4515 printk("%s: resetting, "
4516 "command processing suspended for %d seconds\n",
4517 ncr_name(np), settle_delay);
4519 ncr_chip_reset(np, 100);
4520 udelay(2000); /* The 895 needs time for the bus mode to settle */
4522 OUTW (nc_sien, RST);
4524 ** Enable Tolerant, reset IRQD if present and
4525 ** properly set IRQ mode, prior to resetting the bus.
4527 OUTB (nc_stest3, TE);
4528 OUTB (nc_scntl1, CRST);
4531 if (!driver_setup.bus_check)
4534 ** Check for no terminators or SCSI bus shorts to ground.
4535 ** Read SCSI data bus, data parity bits and control signals.
4536 ** We are expecting RESET to be TRUE and other signals to be
4540 term = INB(nc_sstat0);
4541 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
4542 term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
4543 ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
4544 ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
4545 INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
4547 if (!(np->features & FE_WIDE))
4550 if (term != (2<<7)) {
4551 printk("%s: suspicious SCSI data while resetting the BUS.\n",
4553 printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
4554 "0x%lx, expecting 0x%lx\n",
4556 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
4557 (u_long)term, (u_long)(2<<7));
4558 if (driver_setup.bus_check == 1)
4562 OUTB (nc_scntl1, 0);
4567 * Start reset process.
4568 * If reset in progress do nothing.
4569 * The interrupt handler will reinitialize the chip.
4570 * The timeout handler will wait for settle_time before
4571 * clearing it and so resuming command processing.
4573 static void ncr_start_reset(struct ncb *np)
4575 if (!np->settle_time) {
4576 ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
4580 /*==========================================================
4583 ** Reset the SCSI BUS.
4584 ** This is called from the generic SCSI driver.
4587 **==========================================================
4589 static int ncr_reset_bus (struct ncb *np, struct scsi_cmnd *cmd, int sync_reset)
4591 /* struct scsi_device *device = cmd->device; */
4596 * Return immediately if reset is in progress.
4598 if (np->settle_time) {
4602 * Start the reset process.
4603 * The script processor is then assumed to be stopped.
4604 * Commands will now be queued in the waiting list until a settle
4605 * delay of 2 seconds will be completed.
4607 ncr_start_reset(np);
4609 * First, look in the wakeup list
4611 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4613 ** look for the ccb of this command.
4615 if (cp->host_status == HS_IDLE) continue;
4616 if (cp->cmd == cmd) {
4622 * Then, look in the waiting list
4624 if (!found && retrieve_from_waiting_list(0, np, cmd))
4627 * Wake-up all awaiting commands with DID_RESET.
4629 reset_waiting_list(np);
4631 * Wake-up all pending commands with HS_RESET -> DID_RESET.
4633 ncr_wakeup(np, HS_RESET);
4635 * If the involved command was not in a driver queue, and the
4636 * scsi driver told us reset is synchronous, and the command is not
4637 * currently in the waiting list, complete it with DID_RESET status,
4638 * in order to keep it alive.
4640 if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
4641 cmd->result = ScsiResult(DID_RESET, 0);
4642 ncr_queue_done_cmd(np, cmd);
4648 #if 0 /* unused and broken.. */
4649 /*==========================================================
4652 ** Abort an SCSI command.
4653 ** This is called from the generic SCSI driver.
4656 **==========================================================
4658 static int ncr_abort_command (struct ncb *np, struct scsi_cmnd *cmd)
4660 /* struct scsi_device *device = cmd->device; */
4666 * First, look for the scsi command in the waiting list
4668 if (remove_from_waiting_list(np, cmd)) {
4669 cmd->result = ScsiResult(DID_ABORT, 0);
4670 ncr_queue_done_cmd(np, cmd);
4671 return SCSI_ABORT_SUCCESS;
4675 * Then, look in the wakeup list
4677 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4679 ** look for the ccb of this command.
4681 if (cp->host_status == HS_IDLE) continue;
4682 if (cp->cmd == cmd) {
4689 return SCSI_ABORT_NOT_RUNNING;
4692 if (np->settle_time) {
4693 return SCSI_ABORT_SNOOZE;
4697 ** If the CCB is active, patch schedule jumps for the
4698 ** script to abort the command.
4701 switch(cp->host_status) {
4704 printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
4705 cp->start.schedule.l_paddr =
4706 cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
4707 retv = SCSI_ABORT_PENDING;
4710 cp->restart.schedule.l_paddr =
4711 cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
4712 retv = SCSI_ABORT_PENDING;
4715 retv = SCSI_ABORT_NOT_RUNNING;
4721 ** If there are no requests, the script
4722 ** processor will sleep on SEL_WAIT_RESEL.
4723 ** Let's wake it up, since it may have to work.
4725 OUTB (nc_istat, SIGP);
4731 static void ncr_detach(struct ncb *np)
4740 /* Local copy so we don't access np after freeing it! */
4741 strlcpy(inst_name, ncr_name(np), sizeof(inst_name));
4743 printk("%s: releasing host resources\n", ncr_name(np));
4746 ** Stop the ncr_timeout process
4747 ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
4750 #ifdef DEBUG_NCR53C8XX
4751 printk("%s: stopping the timer\n", ncr_name(np));
4753 np->release_stage = 1;
4754 for (i = 50 ; i && np->release_stage != 2 ; i--)
4756 if (np->release_stage != 2)
4757 printk("%s: the timer seems to be already stopped\n", ncr_name(np));
4758 else np->release_stage = 2;
4761 ** Disable chip interrupts
4764 #ifdef DEBUG_NCR53C8XX
4765 printk("%s: disabling chip interrupts\n", ncr_name(np));
4772 ** Restore bios setting for automatic clock detection.
4775 printk("%s: resetting chip\n", ncr_name(np));
4776 ncr_chip_reset(np, 100);
4778 OUTB(nc_dmode, np->sv_dmode);
4779 OUTB(nc_dcntl, np->sv_dcntl);
4780 OUTB(nc_ctest0, np->sv_ctest0);
4781 OUTB(nc_ctest3, np->sv_ctest3);
4782 OUTB(nc_ctest4, np->sv_ctest4);
4783 OUTB(nc_ctest5, np->sv_ctest5);
4784 OUTB(nc_gpcntl, np->sv_gpcntl);
4785 OUTB(nc_stest2, np->sv_stest2);
4787 ncr_selectclock(np, np->sv_scntl3);
4790 ** Free allocated ccb(s)
4793 while ((cp=np->ccb->link_ccb) != NULL) {
4794 np->ccb->link_ccb = cp->link_ccb;
4795 if (cp->host_status) {
4796 printk("%s: shall free an active ccb (host_status=%d)\n",
4797 ncr_name(np), cp->host_status);
4799 #ifdef DEBUG_NCR53C8XX
4800 printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
4802 m_free_dma(cp, sizeof(*cp), "CCB");
4805 /* Free allocated tp(s) */
4807 for (target = 0; target < MAX_TARGET ; target++) {
4808 tp=&np->target[target];
4809 for (lun = 0 ; lun < MAX_LUN ; lun++) {
4812 #ifdef DEBUG_NCR53C8XX
4813 printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
4815 if (lp->jump_ccb != &lp->jump_ccb_0)
4816 m_free_dma(lp->jump_ccb,256,"JUMP_CCB");
4817 m_free_dma(lp, sizeof(*lp), "LCB");
4823 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
4825 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
4827 m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
4828 m_free_dma(np, sizeof(struct ncb), "NCB");
4830 printk("%s: host resources successfully released\n", inst_name);
4833 /*==========================================================
4836 ** Complete execution of a SCSI command.
4837 ** Signal completion to the generic SCSI driver.
4840 **==========================================================
4843 void ncr_complete (struct ncb *np, struct ccb *cp)
4845 struct scsi_cmnd *cmd;
4853 if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
4857 ** Print minimal debug information.
4860 if (DEBUG_FLAGS & DEBUG_TINY)
4861 printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
4862 cp->host_status,cp->scsi_status);
4865 ** Get command, target and lun pointers.
4870 tp = &np->target[cmd->device->id];
4871 lp = tp->lp[cmd->device->lun];
4874 ** We donnot queue more than 1 ccb per target
4875 ** with negotiation at any time. If this ccb was
4876 ** used for negotiation, clear this info in the tcb.
4879 if (cp == tp->nego_cp)
4883 ** If auto-sense performed, change scsi status.
4885 if (cp->auto_sense) {
4886 cp->scsi_status = cp->auto_sense;
4890 ** If we were recovering from queue full or performing
4891 ** auto-sense, requeue skipped CCBs to the wait queue.
4894 if (lp && lp->held_ccb) {
4895 if (cp == lp->held_ccb) {
4896 list_splice_init(&lp->skip_ccbq, &lp->wait_ccbq);
4897 lp->held_ccb = NULL;
4902 ** Check for parity errors.
4905 if (cp->parity_status > 1) {
4906 PRINT_ADDR(cmd, "%d parity error(s).\n",cp->parity_status);
4910 ** Check for extended errors.
4913 if (cp->xerr_status != XE_OK) {
4914 switch (cp->xerr_status) {
4916 PRINT_ADDR(cmd, "extraneous data discarded.\n");
4919 PRINT_ADDR(cmd, "invalid scsi phase (4/5).\n");
4922 PRINT_ADDR(cmd, "extended error %d.\n",
4926 if (cp->host_status==HS_COMPLETE)
4927 cp->host_status = HS_FAIL;
4931 ** Print out any error for debugging purpose.
4933 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
4934 if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
4935 PRINT_ADDR(cmd, "ERROR: cmd=%x host_status=%x "
4936 "scsi_status=%x\n", cmd->cmnd[0],
4937 cp->host_status, cp->scsi_status);
4942 ** Check the status.
4944 if ( (cp->host_status == HS_COMPLETE)
4945 && (cp->scsi_status == S_GOOD ||
4946 cp->scsi_status == S_COND_MET)) {
4948 * All went well (GOOD status).
4949 * CONDITION MET status is returned on
4950 * `Pre-Fetch' or `Search data' success.
4952 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
4956 ** Could dig out the correct value for resid,
4957 ** but it would be quite complicated.
4959 /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
4962 ** Allocate the lcb if not yet.
4965 ncr_alloc_lcb (np, cmd->device->id, cmd->device->lun);
4967 tp->bytes += cp->data_len;
4971 ** If tags was reduced due to queue full,
4972 ** increase tags if 1000 good status received.
4974 if (lp && lp->usetags && lp->numtags < lp->maxtags) {
4976 if (lp->num_good >= 1000) {
4979 ncr_setup_tags (np, cmd->device);
4982 } else if ((cp->host_status == HS_COMPLETE)
4983 && (cp->scsi_status == S_CHECK_COND)) {
4985 ** Check condition code
4987 cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
4990 ** Copy back sense data to caller's buffer.
4992 memcpy(cmd->sense_buffer, cp->sense_buf,
4993 min(sizeof(cmd->sense_buffer), sizeof(cp->sense_buf)));
4995 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
4996 u_char * p = (u_char*) & cmd->sense_buffer;
4998 PRINT_ADDR(cmd, "sense data:");
4999 for (i=0; i<14; i++) printk (" %x", *p++);
5002 } else if ((cp->host_status == HS_COMPLETE)
5003 && (cp->scsi_status == S_CONFLICT)) {
5005 ** Reservation Conflict condition code
5007 cmd->result = ScsiResult(DID_OK, S_CONFLICT);
5009 } else if ((cp->host_status == HS_COMPLETE)
5010 && (cp->scsi_status == S_BUSY ||
5011 cp->scsi_status == S_QUEUE_FULL)) {
5016 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5018 } else if ((cp->host_status == HS_SEL_TIMEOUT)
5019 || (cp->host_status == HS_TIMEOUT)) {
5024 cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
5026 } else if (cp->host_status == HS_RESET) {
5031 cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
5033 } else if (cp->host_status == HS_ABORTED) {
5038 cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
5043 ** Other protocol messes
5045 PRINT_ADDR(cmd, "COMMAND FAILED (%x %x) @%p.\n",
5046 cp->host_status, cp->scsi_status, cp);
5048 cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
5055 if (tp->usrflag & UF_TRACE) {
5058 PRINT_ADDR(cmd, " CMD:");
5059 p = (u_char*) &cmd->cmnd[0];
5060 for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
5062 if (cp->host_status==HS_COMPLETE) {
5063 switch (cp->scsi_status) {
5069 p = (u_char*) &cmd->sense_buffer;
5070 for (i=0; i<14; i++)
5071 printk (" %x", *p++);
5074 printk (" STAT: %x\n", cp->scsi_status);
5077 } else printk (" HOSTERROR: %x", cp->host_status);
5084 ncr_free_ccb (np, cp);
5087 ** requeue awaiting scsi commands for this lun.
5089 if (lp && lp->queuedccbs < lp->queuedepth &&
5090 !list_empty(&lp->wait_ccbq))
5091 ncr_start_next_ccb(np, lp, 2);
5094 ** requeue awaiting scsi commands for this controller.
5096 if (np->waiting_list)
5097 requeue_waiting_list(np);
5100 ** signal completion to generic driver.
5102 ncr_queue_done_cmd(np, cmd);
5105 /*==========================================================
5108 ** Signal all (or one) control block done.
5111 **==========================================================
5115 ** This CCB has been skipped by the NCR.
5116 ** Queue it in the correponding unit queue.
5118 static void ncr_ccb_skipped(struct ncb *np, struct ccb *cp)
5120 struct tcb *tp = &np->target[cp->target];
5121 struct lcb *lp = tp->lp[cp->lun];
5123 if (lp && cp != np->ccb) {
5124 cp->host_status &= ~HS_SKIPMASK;
5125 cp->start.schedule.l_paddr =
5126 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5127 list_del(&cp->link_ccbq);
5128 list_add_tail(&cp->link_ccbq, &lp->skip_ccbq);
5140 ** The NCR has completed CCBs.
5141 ** Look at the DONE QUEUE if enabled, otherwise scan all CCBs
5143 void ncr_wakeup_done (struct ncb *np)
5146 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5149 i = np->ccb_done_ic;
5155 cp = np->ccb_done[j];
5156 if (!CCB_DONE_VALID(cp))
5159 np->ccb_done[j] = (struct ccb *)CCB_DONE_EMPTY;
5160 np->scripth->done_queue[5*j + 4] =
5161 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5163 np->scripth->done_queue[5*i + 4] =
5164 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5166 if (cp->host_status & HS_DONEMASK)
5167 ncr_complete (np, cp);
5168 else if (cp->host_status & HS_SKIPMASK)
5169 ncr_ccb_skipped (np, cp);
5173 np->ccb_done_ic = i;
5177 if (cp->host_status & HS_DONEMASK)
5178 ncr_complete (np, cp);
5179 else if (cp->host_status & HS_SKIPMASK)
5180 ncr_ccb_skipped (np, cp);
5187 ** Complete all active CCBs.
5189 void ncr_wakeup (struct ncb *np, u_long code)
5191 struct ccb *cp = np->ccb;
5194 if (cp->host_status != HS_IDLE) {
5195 cp->host_status = code;
5196 ncr_complete (np, cp);
5206 /* Some initialisation must be done immediately following reset, for 53c720,
5207 * at least. EA (dcntl bit 5) isn't set here as it is set once only in
5208 * the _detect function.
5210 static void ncr_chip_reset(struct ncb *np, int delay)
5212 OUTB (nc_istat, SRST);
5214 OUTB (nc_istat, 0 );
5216 if (np->features & FE_EHP)
5217 OUTB (nc_ctest0, EHP);
5218 if (np->features & FE_MUX)
5219 OUTB (nc_ctest4, MUX);
5223 /*==========================================================
5229 **==========================================================
5232 void ncr_init (struct ncb *np, int reset, char * msg, u_long code)
5237 ** Reset chip if asked, otherwise just clear fifos.
5241 OUTB (nc_istat, SRST);
5245 OUTB (nc_stest3, TE|CSF);
5246 OUTONB (nc_ctest3, CLF);
5253 if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
5256 ** Clear Start Queue
5258 np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
5259 for (i = 1; i < MAX_START + MAX_START; i += 2)
5260 np->scripth0->tryloop[i] =
5261 cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5264 ** Start at first entry.
5267 np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
5269 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5273 for (i = 0; i < MAX_DONE; i++) {
5274 np->ccb_done[i] = (struct ccb *)CCB_DONE_EMPTY;
5275 np->scripth0->done_queue[5*i + 4] =
5276 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5281 ** Start at first entry.
5283 np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
5284 np->ccb_done_ic = MAX_DONE-1;
5285 np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
5286 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5289 ** Wakeup all pending jobs.
5291 ncr_wakeup (np, code);
5298 ** Remove reset; big delay because the 895 needs time for the
5299 ** bus mode to settle
5301 ncr_chip_reset(np, 2000);
5303 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
5304 /* full arb., ena parity, par->ATN */
5305 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
5307 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
5309 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
5310 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
5311 OUTB (nc_istat , SIGP ); /* Signal Process */
5312 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
5313 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
5315 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
5316 OUTB (nc_ctest0, np->rv_ctest0); /* 720: CDIS and EHP */
5317 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
5318 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
5320 OUTB (nc_stest2, EXT|np->rv_stest2); /* Extended Sreq/Sack filtering */
5321 OUTB (nc_stest3, TE); /* TolerANT enable */
5322 OUTB (nc_stime0, 0x0c ); /* HTH disabled STO 0.25 sec */
5325 ** Disable disconnects.
5331 ** Enable GPIO0 pin for writing if LED support.
5334 if (np->features & FE_LED0) {
5335 OUTOFFB (nc_gpcntl, 0x01);
5342 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
5343 OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
5346 ** Fill in target structure.
5347 ** Reinitialize usrsync.
5348 ** Reinitialize usrwide.
5349 ** Prepare sync negotiation according to actual SCSI bus mode.
5352 for (i=0;i<MAX_TARGET;i++) {
5353 struct tcb *tp = &np->target[i];
5356 tp->wval = np->rv_scntl3;
5358 if (tp->usrsync != 255) {
5359 if (tp->usrsync <= np->maxsync) {
5360 if (tp->usrsync < np->minsync) {
5361 tp->usrsync = np->minsync;
5368 if (tp->usrwide > np->maxwide)
5369 tp->usrwide = np->maxwide;
5374 ** Start script processor.
5378 printk ("%s: Downloading SCSI SCRIPTS.\n",
5380 OUTL (nc_scratcha, vtobus(np->script0));
5381 OUTL_DSP (NCB_SCRIPTH_PHYS (np, start_ram));
5384 OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
5387 /*==========================================================
5389 ** Prepare the negotiation values for wide and
5390 ** synchronous transfers.
5392 **==========================================================
5395 static void ncr_negotiate (struct ncb* np, struct tcb* tp)
5398 ** minsync unit is 4ns !
5401 u_long minsync = tp->usrsync;
5404 ** SCSI bus mode limit
5407 if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
5408 if (minsync < 12) minsync = 12;
5415 if (minsync < np->minsync)
5416 minsync = np->minsync;
5422 if (minsync > np->maxsync)
5425 if (tp->maxoffs > np->maxoffs)
5426 tp->maxoffs = np->maxoffs;
5428 tp->minsync = minsync;
5429 tp->maxoffs = (minsync<255 ? tp->maxoffs : 0);
5432 ** period=0: has to negotiate sync transfer
5438 ** widedone=0: has to negotiate wide transfer
5443 /*==========================================================
5445 ** Get clock factor and sync divisor for a given
5446 ** synchronous factor period.
5447 ** Returns the clock factor (in sxfer) and scntl3
5448 ** synchronous divisor field.
5450 **==========================================================
5453 static void ncr_getsync(struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p)
5455 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
5456 int div = np->clock_divn; /* Number of divisors supported */
5457 u_long fak; /* Sync factor in sxfer */
5458 u_long per; /* Period in tenths of ns */
5459 u_long kpc; /* (per * clk) */
5462 ** Compute the synchronous period in tenths of nano-seconds
5464 if (sfac <= 10) per = 250;
5465 else if (sfac == 11) per = 303;
5466 else if (sfac == 12) per = 500;
5467 else per = 40 * sfac;
5470 ** Look for the greatest clock divisor that allows an
5471 ** input speed faster than the period.
5475 if (kpc >= (div_10M[div] << 2)) break;
5478 ** Calculate the lowest clock factor that allows an output
5479 ** speed not faster than the period.
5481 fak = (kpc - 1) / div_10M[div] + 1;
5483 #if 0 /* This optimization does not seem very useful */
5485 per = (fak * div_10M[div]) / clk;
5488 ** Why not to try the immediate lower divisor and to choose
5489 ** the one that allows the fastest output speed ?
5490 ** We don't want input speed too much greater than output speed.
5492 if (div >= 1 && fak < 8) {
5494 fak2 = (kpc - 1) / div_10M[div-1] + 1;
5495 per2 = (fak2 * div_10M[div-1]) / clk;
5496 if (per2 < per && fak2 <= 8) {
5504 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
5507 ** Compute and return sync parameters for the ncr
5510 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
5514 /*==========================================================
5516 ** Set actual values, sync status and patch all ccbs of
5517 ** a target according to new sync/wide agreement.
5519 **==========================================================
5522 static void ncr_set_sync_wide_status (struct ncb *np, u_char target)
5525 struct tcb *tp = &np->target[target];
5528 ** set actual value and sync_status
5530 OUTB (nc_sxfer, tp->sval);
5531 np->sync_st = tp->sval;
5532 OUTB (nc_scntl3, tp->wval);
5533 np->wide_st = tp->wval;
5536 ** patch ALL ccbs of this target.
5538 for (cp = np->ccb; cp; cp = cp->link_ccb) {
5539 if (!cp->cmd) continue;
5540 if (scmd_id(cp->cmd) != target) continue;
5542 cp->sync_status = tp->sval;
5543 cp->wide_status = tp->wval;
5545 cp->phys.select.sel_scntl3 = tp->wval;
5546 cp->phys.select.sel_sxfer = tp->sval;
5550 /*==========================================================
5552 ** Switch sync mode for current job and it's target
5554 **==========================================================
5557 static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer)
5559 struct scsi_cmnd *cmd = cp->cmd;
5561 u_char target = INB (nc_sdid) & 0x0f;
5564 BUG_ON(target != (scmd_id(cmd) & 0xf));
5566 tp = &np->target[target];
5568 if (!scntl3 || !(sxfer & 0x1f))
5569 scntl3 = np->rv_scntl3;
5570 scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
5573 ** Deduce the value of controller sync period from scntl3.
5574 ** period is in tenths of nano-seconds.
5577 idiv = ((scntl3 >> 4) & 0x7);
5578 if ((sxfer & 0x1f) && idiv)
5579 tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
5581 tp->period = 0xffff;
5583 /* Stop there if sync parameters are unchanged */
5584 if (tp->sval == sxfer && tp->wval == scntl3)
5589 if (sxfer & 0x01f) {
5590 /* Disable extended Sreq/Sack filtering */
5591 if (tp->period <= 2000)
5592 OUTOFFB(nc_stest2, EXT);
5595 spi_display_xfer_agreement(tp->starget);
5598 ** set actual value and sync_status
5599 ** patch ALL ccbs of this target.
5601 ncr_set_sync_wide_status(np, target);
5604 /*==========================================================
5606 ** Switch wide mode for current job and it's target
5607 ** SCSI specs say: a SCSI device that accepts a WDTR
5608 ** message shall reset the synchronous agreement to
5609 ** asynchronous mode.
5611 **==========================================================
5614 static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack)
5616 struct scsi_cmnd *cmd = cp->cmd;
5617 u16 target = INB (nc_sdid) & 0x0f;
5622 BUG_ON(target != (scmd_id(cmd) & 0xf));
5624 tp = &np->target[target];
5625 tp->widedone = wide+1;
5626 scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
5628 sxfer = ack ? 0 : tp->sval;
5631 ** Stop there if sync/wide parameters are unchanged
5633 if (tp->sval == sxfer && tp->wval == scntl3) return;
5638 ** Bells and whistles ;-)
5640 if (bootverbose >= 2) {
5641 dev_info(&cmd->device->sdev_target->dev, "WIDE SCSI %sabled.\n",
5642 (scntl3 & EWS) ? "en" : "dis");
5646 ** set actual value and sync_status
5647 ** patch ALL ccbs of this target.
5649 ncr_set_sync_wide_status(np, target);
5652 /*==========================================================
5654 ** Switch tagged mode for a target.
5656 **==========================================================
5659 static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
5661 unsigned char tn = sdev->id, ln = sdev->lun;
5662 struct tcb *tp = &np->target[tn];
5663 struct lcb *lp = tp->lp[ln];
5664 u_char reqtags, maxdepth;
5669 if ((!tp) || (!lp) || !sdev)
5673 ** If SCSI device queue depth is not yet set, leave here.
5675 if (!lp->scdev_depth)
5679 ** Donnot allow more tags than the SCSI driver can queue
5681 ** Donnot allow more tags than we can handle.
5683 maxdepth = lp->scdev_depth;
5684 if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
5685 if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
5686 if (lp->numtags > maxdepth) lp->numtags = maxdepth;
5689 ** only devices conformant to ANSI Version >= 2
5690 ** only devices capable of tagged commands
5691 ** only if enabled by user ..
5693 if (sdev->tagged_supported && lp->numtags > 1) {
5694 reqtags = lp->numtags;
5700 ** Update max number of tags
5702 lp->numtags = reqtags;
5703 if (lp->numtags > lp->maxtags)
5704 lp->maxtags = lp->numtags;
5707 ** If we want to switch tag mode, we must wait
5708 ** for no CCB to be active.
5710 if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
5711 if (lp->queuedepth == reqtags) /* Already announced */
5713 lp->queuedepth = reqtags;
5715 else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
5716 lp->queuedepth = reqtags;
5719 else { /* Want to switch tag mode */
5720 if (lp->busyccbs) /* If not yet safe, return */
5722 lp->queuedepth = reqtags;
5723 lp->usetags = reqtags > 1 ? 1 : 0;
5727 ** Patch the lun mini-script, according to tag mode.
5729 lp->jump_tag.l_paddr = lp->usetags?
5730 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
5731 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
5734 ** Announce change to user.
5738 dev_info(&sdev->sdev_gendev,
5739 "tagged command queue depth set to %d\n",
5742 dev_info(&sdev->sdev_gendev,
5743 "tagged command queueing disabled\n");
5748 /*==========================================================
5751 ** ncr timeout handler.
5754 **==========================================================
5756 ** Misused to keep the driver running when
5757 ** interrupts are not configured correctly.
5759 **----------------------------------------------------------
5762 static void ncr_timeout (struct ncb *np)
5764 u_long thistime = jiffies;
5767 ** If release process in progress, let's go
5768 ** Set the release stage from 1 to 2 to synchronize
5769 ** with the release process.
5772 if (np->release_stage) {
5773 if (np->release_stage == 1) np->release_stage = 2;
5777 np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
5778 add_timer(&np->timer);
5781 ** If we are resetting the ncr, wait for settle_time before
5782 ** clearing it. Then command processing will be resumed.
5784 if (np->settle_time) {
5785 if (np->settle_time <= thistime) {
5786 if (bootverbose > 1)
5787 printk("%s: command processing resumed\n", ncr_name(np));
5788 np->settle_time = 0;
5790 requeue_waiting_list(np);
5796 ** Since the generic scsi driver only allows us 0.5 second
5797 ** to perform abort of a command, we must look at ccbs about
5798 ** every 0.25 second.
5800 if (np->lasttime + 4*HZ < thistime) {
5802 ** block ncr interrupts
5804 np->lasttime = thistime;
5807 #ifdef SCSI_NCR_BROKEN_INTR
5808 if (INB(nc_istat) & (INTF|SIP|DIP)) {
5811 ** Process pending interrupts.
5813 if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
5815 if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
5817 #endif /* SCSI_NCR_BROKEN_INTR */
5820 /*==========================================================
5822 ** log message for real hard errors
5824 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
5825 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
5827 ** exception register:
5832 ** so: control lines as driver by NCR.
5833 ** si: control lines as seen by NCR.
5834 ** sd: scsi data lines as seen by NCR.
5837 ** sxfer: (see the manual)
5838 ** scntl3: (see the manual)
5840 ** current script command:
5841 ** dsp: script address (relative to start of script).
5842 ** dbc: first word of script command.
5844 ** First 16 register of the chip:
5847 **==========================================================
5850 static void ncr_log_hard_error(struct ncb *np, u16 sist, u_char dstat)
5856 u_char *script_base;
5861 if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
5862 script_ofs = dsp - np->p_script;
5863 script_size = sizeof(struct script);
5864 script_base = (u_char *) np->script0;
5865 script_name = "script";
5867 else if (np->p_scripth < dsp &&
5868 dsp <= np->p_scripth + sizeof(struct scripth)) {
5869 script_ofs = dsp - np->p_scripth;
5870 script_size = sizeof(struct scripth);
5871 script_base = (u_char *) np->scripth0;
5872 script_name = "scripth";
5877 script_name = "mem";
5880 printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
5881 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
5882 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
5883 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
5884 (unsigned)INL (nc_dbc));
5886 if (((script_ofs & 3) == 0) &&
5887 (unsigned)script_ofs < script_size) {
5888 printk ("%s: script cmd = %08x\n", ncr_name(np),
5889 scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
5892 printk ("%s: regdump:", ncr_name(np));
5894 printk (" %02x", (unsigned)INB_OFF(i));
5898 /*============================================================
5900 ** ncr chip exception handler.
5902 **============================================================
5904 ** In normal cases, interrupt conditions occur one at a
5905 ** time. The ncr is able to stack in some extra registers
5906 ** other interrupts that will occurs after the first one.
5907 ** But severall interrupts may occur at the same time.
5909 ** We probably should only try to deal with the normal
5910 ** case, but it seems that multiple interrupts occur in
5911 ** some cases that are not abnormal at all.
5913 ** The most frequent interrupt condition is Phase Mismatch.
5914 ** We should want to service this interrupt quickly.
5915 ** A SCSI parity error may be delivered at the same time.
5916 ** The SIR interrupt is not very frequent in this driver,
5917 ** since the INTFLY is likely used for command completion
5919 ** The Selection Timeout interrupt may be triggered with
5921 ** The SBMC interrupt (SCSI Bus Mode Change) may probably
5922 ** occur at any time.
5924 ** This handler try to deal as cleverly as possible with all
5927 **============================================================
5930 void ncr_exception (struct ncb *np)
5932 u_char istat, dstat;
5937 ** interrupt on the fly ?
5938 ** Since the global header may be copied back to a CCB
5939 ** using a posted PCI memory write, the last operation on
5940 ** the istat register is a READ in order to flush posted
5941 ** PCI write commands.
5943 istat = INB (nc_istat);
5945 OUTB (nc_istat, (istat & SIGP) | INTF);
5946 istat = INB (nc_istat);
5947 if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
5948 ncr_wakeup_done (np);
5951 if (!(istat & (SIP|DIP)))
5955 OUTB (nc_istat, CABRT);
5958 ** Steinbach's Guideline for Systems Programming:
5959 ** Never test for an error condition you don't know how to handle.
5962 sist = (istat & SIP) ? INW (nc_sist) : 0;
5963 dstat = (istat & DIP) ? INB (nc_dstat) : 0;
5965 if (DEBUG_FLAGS & DEBUG_TINY)
5966 printk ("<%d|%x:%x|%x:%x>",
5969 (unsigned)INL(nc_dsp),
5970 (unsigned)INL(nc_dbc));
5972 /*========================================================
5973 ** First, interrupts we want to service cleanly.
5975 ** Phase mismatch is the most frequent interrupt, and
5976 ** so we have to service it as quickly and as cleanly
5978 ** Programmed interrupts are rarely used in this driver,
5979 ** but we must handle them cleanly anyway.
5980 ** We try to deal with PAR and SBMC combined with
5981 ** some other interrupt(s).
5982 **=========================================================
5985 if (!(sist & (STO|GEN|HTH|SGE|UDC|RST)) &&
5986 !(dstat & (MDPE|BF|ABRT|IID))) {
5987 if ((sist & SBMC) && ncr_int_sbmc (np))
5989 if ((sist & PAR) && ncr_int_par (np))
6000 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
6002 if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
6003 printk( "%s: unknown interrupt(s) ignored, "
6004 "ISTAT=%x DSTAT=%x SIST=%x\n",
6005 ncr_name(np), istat, dstat, sist);
6012 /*========================================================
6013 ** Now, interrupts that need some fixing up.
6014 ** Order and multiple interrupts is so less important.
6016 ** If SRST has been asserted, we just reset the chip.
6018 ** Selection is intirely handled by the chip. If the
6019 ** chip says STO, we trust it. Seems some other
6020 ** interrupts may occur at the same time (UDC, IID), so
6021 ** we ignore them. In any case we do enough fix-up
6022 ** in the service routine.
6023 ** We just exclude some fatal dma errors.
6024 **=========================================================
6028 ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
6033 !(dstat & (MDPE|BF|ABRT))) {
6035 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
6037 OUTONB (nc_ctest3, CLF);
6043 /*=========================================================
6044 ** Now, interrupts we are not able to recover cleanly.
6045 ** (At least for the moment).
6047 ** Do the register dump.
6048 ** Log message for real hard errors.
6050 ** For MDPE, BF, ABORT, IID, SGE and HTH we reset the
6051 ** BUS and the chip.
6052 ** We are more soft for UDC.
6053 **=========================================================
6056 if (time_after(jiffies, np->regtime)) {
6057 np->regtime = jiffies + 10*HZ;
6058 for (i = 0; i<sizeof(np->regdump); i++)
6059 ((char*)&np->regdump)[i] = INB_OFF(i);
6060 np->regdump.nc_dstat = dstat;
6061 np->regdump.nc_sist = sist;
6064 ncr_log_hard_error(np, sist, dstat);
6066 printk ("%s: have to clear fifos.\n", ncr_name (np));
6067 OUTB (nc_stest3, TE|CSF);
6068 OUTONB (nc_ctest3, CLF);
6070 if ((sist & (SGE)) ||
6071 (dstat & (MDPE|BF|ABRT|IID))) {
6072 ncr_start_reset(np);
6077 printk ("%s: handshake timeout\n", ncr_name(np));
6078 ncr_start_reset(np);
6083 printk ("%s: unexpected disconnect\n", ncr_name(np));
6084 OUTB (HS_PRT, HS_UNEXPECTED);
6085 OUTL_DSP (NCB_SCRIPT_PHYS (np, cleanup));
6089 /*=========================================================
6090 ** We just miss the cause of the interrupt. :(
6091 ** Print a message. The timeout will do the real work.
6092 **=========================================================
6094 printk ("%s: unknown interrupt\n", ncr_name(np));
6097 /*==========================================================
6099 ** ncr chip exception handler for selection timeout
6101 **==========================================================
6103 ** There seems to be a bug in the 53c810.
6104 ** Although a STO-Interrupt is pending,
6105 ** it continues executing script commands.
6106 ** But it will fail and interrupt (IID) on
6107 ** the next instruction where it's looking
6108 ** for a valid phase.
6110 **----------------------------------------------------------
6113 void ncr_int_sto (struct ncb *np)
6117 if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
6120 ** look for ccb and set the status.
6125 while (cp && (CCB_PHYS (cp, phys) != dsa))
6129 cp-> host_status = HS_SEL_TIMEOUT;
6130 ncr_complete (np, cp);
6134 ** repair start queue and jump to start point.
6137 OUTL_DSP (NCB_SCRIPTH_PHYS (np, sto_restart));
6141 /*==========================================================
6143 ** ncr chip exception handler for SCSI bus mode change
6145 **==========================================================
6147 ** spi2-r12 11.2.3 says a transceiver mode change must
6148 ** generate a reset event and a device that detects a reset
6149 ** event shall initiate a hard reset. It says also that a
6150 ** device that detects a mode change shall set data transfer
6151 ** mode to eight bit asynchronous, etc...
6152 ** So, just resetting should be enough.
6155 **----------------------------------------------------------
6158 static int ncr_int_sbmc (struct ncb *np)
6160 u_char scsi_mode = INB (nc_stest4) & SMODE;
6162 if (scsi_mode != np->scsi_mode) {
6163 printk("%s: SCSI bus mode change from %x to %x.\n",
6164 ncr_name(np), np->scsi_mode, scsi_mode);
6166 np->scsi_mode = scsi_mode;
6170 ** Suspend command processing for 1 second and
6171 ** reinitialize all except the chip.
6173 np->settle_time = jiffies + HZ;
6174 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
6180 /*==========================================================
6182 ** ncr chip exception handler for SCSI parity error.
6184 **==========================================================
6187 **----------------------------------------------------------
6190 static int ncr_int_par (struct ncb *np)
6192 u_char hsts = INB (HS_PRT);
6193 u32 dbc = INL (nc_dbc);
6194 u_char sstat1 = INB (nc_sstat1);
6199 printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
6200 ncr_name(np), hsts, dbc, sstat1);
6203 * Ignore the interrupt if the NCR is not connected
6204 * to the SCSI bus, since the right work should have
6205 * been done on unexpected disconnection handling.
6207 if (!(INB (nc_scntl1) & ISCON))
6211 * If the nexus is not clearly identified, reset the bus.
6212 * We will try to do better later.
6214 if (hsts & HS_INVALMASK)
6218 * If the SCSI parity error occurs in MSG IN phase, prepare a
6219 * MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
6220 * ERROR message and let the device decide to retry the command
6221 * or to terminate with check condition. If we were in MSG IN
6222 * phase waiting for the response of a negotiation, we will
6223 * get SIR_NEGO_FAILED at dispatch.
6225 if (!(dbc & 0xc0000000))
6226 phase = (dbc >> 24) & 7;
6228 msg = MSG_PARITY_ERROR;
6230 msg = INITIATOR_ERROR;
6234 * If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
6235 * script that will ignore all data in bytes until phase
6236 * change, since we are not sure the chip will wait the phase
6237 * change prior to delivering the interrupt.
6240 jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
6242 jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
6244 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
6245 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
6247 np->msgout[0] = msg;
6252 ncr_start_reset(np);
6256 /*==========================================================
6259 ** ncr chip exception handler for phase errors.
6262 **==========================================================
6264 ** We have to construct a new transfer descriptor,
6265 ** to transfer the rest of the current block.
6267 **----------------------------------------------------------
6270 static void ncr_int_ma (struct ncb *np)
6287 sbcl = INB (nc_sbcl);
6290 rest = dbc & 0xffffff;
6293 ** Take into account dma fifo and various buffers and latches,
6294 ** only if the interrupted phase is an OUTPUT phase.
6297 if ((cmd & 1) == 0) {
6298 u_char ctest5, ss0, ss2;
6301 ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
6303 delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
6305 delta=(INB (nc_dfifo) - rest) & 0x7f;
6308 ** The data in the dma fifo has not been transferred to
6309 ** the target -> add the amount to the rest
6310 ** and clear the data.
6311 ** Check the sstat2 register in case of wide transfer.
6315 ss0 = INB (nc_sstat0);
6316 if (ss0 & OLF) rest++;
6317 if (ss0 & ORF) rest++;
6318 if (INB(nc_scntl3) & EWS) {
6319 ss2 = INB (nc_sstat2);
6320 if (ss2 & OLF1) rest++;
6321 if (ss2 & ORF1) rest++;
6324 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6325 printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
6326 (unsigned) rest, (unsigned) delta, ss0);
6329 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6330 printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
6336 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
6337 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
6340 ** locate matching cp.
6341 ** if the interrupted phase is DATA IN or DATA OUT,
6342 ** trust the global header.
6347 if (CCB_PHYS(cp, phys) != dsa)
6351 while (cp && (CCB_PHYS (cp, phys) != dsa))
6356 ** try to find the interrupted script command,
6357 ** and the address at which to continue.
6361 if (dsp > np->p_script &&
6362 dsp <= np->p_script + sizeof(struct script)) {
6363 vdsp = (u32 *)((char*)np->script0 + (dsp-np->p_script-8));
6366 else if (dsp > np->p_scripth &&
6367 dsp <= np->p_scripth + sizeof(struct scripth)) {
6368 vdsp = (u32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
6372 if (dsp == CCB_PHYS (cp, patch[2])) {
6373 vdsp = &cp->patch[0];
6374 nxtdsp = scr_to_cpu(vdsp[3]);
6376 else if (dsp == CCB_PHYS (cp, patch[6])) {
6377 vdsp = &cp->patch[4];
6378 nxtdsp = scr_to_cpu(vdsp[3]);
6383 ** log the information
6386 if (DEBUG_FLAGS & DEBUG_PHASE) {
6387 printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
6390 (unsigned)nxtdsp, vdsp, cmd);
6394 ** cp=0 means that the DSA does not point to a valid control
6395 ** block. This should not happen since we donnot use multi-byte
6396 ** move while we are being reselected ot after command complete.
6397 ** We are not able to recover from such a phase error.
6400 printk ("%s: SCSI phase error fixup: "
6401 "CCB already dequeued (0x%08lx)\n",
6402 ncr_name (np), (u_long) np->header.cp);
6407 ** get old startaddress and old length.
6410 oadr = scr_to_cpu(vdsp[1]);
6412 if (cmd & 0x10) { /* Table indirect */
6413 tblp = (u32 *) ((char*) &cp->phys + oadr);
6414 olen = scr_to_cpu(tblp[0]);
6415 oadr = scr_to_cpu(tblp[1]);
6418 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
6421 if (DEBUG_FLAGS & DEBUG_PHASE) {
6422 printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
6423 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
6430 ** check cmd against assumed interrupted script command.
6433 if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
6434 PRINT_ADDR(cp->cmd, "internal error: cmd=%02x != %02x=(vdsp[0] "
6435 ">> 24)\n", cmd, scr_to_cpu(vdsp[0]) >> 24);
6441 ** cp != np->header.cp means that the header of the CCB
6442 ** currently being processed has not yet been copied to
6443 ** the global header area. That may happen if the device did
6444 ** not accept all our messages after having been selected.
6446 if (cp != np->header.cp) {
6447 printk ("%s: SCSI phase error fixup: "
6448 "CCB address mismatch (0x%08lx != 0x%08lx)\n",
6449 ncr_name (np), (u_long) cp, (u_long) np->header.cp);
6453 ** if old phase not dataphase, leave here.
6457 PRINT_ADDR(cp->cmd, "phase change %x-%x %d@%08x resid=%d.\n",
6458 cmd&7, sbcl&7, (unsigned)olen,
6459 (unsigned)oadr, (unsigned)rest);
6460 goto unexpected_phase;
6464 ** choose the correct patch area.
6465 ** if savep points to one, choose the other.
6469 newtmp = CCB_PHYS (cp, patch);
6470 if (newtmp == scr_to_cpu(cp->phys.header.savep)) {
6471 newcmd = &cp->patch[4];
6472 newtmp = CCB_PHYS (cp, patch[4]);
6476 ** fillin the commands
6479 newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
6480 newcmd[1] = cpu_to_scr(oadr + olen - rest);
6481 newcmd[2] = cpu_to_scr(SCR_JUMP);
6482 newcmd[3] = cpu_to_scr(nxtdsp);
6484 if (DEBUG_FLAGS & DEBUG_PHASE) {
6485 PRINT_ADDR(cp->cmd, "newcmd[%d] %x %x %x %x.\n",
6486 (int) (newcmd - cp->patch),
6487 (unsigned)scr_to_cpu(newcmd[0]),
6488 (unsigned)scr_to_cpu(newcmd[1]),
6489 (unsigned)scr_to_cpu(newcmd[2]),
6490 (unsigned)scr_to_cpu(newcmd[3]));
6493 ** fake the return address (to the patch).
6494 ** and restart script processor at dispatcher.
6496 OUTL (nc_temp, newtmp);
6497 OUTL_DSP (NCB_SCRIPT_PHYS (np, dispatch));
6501 ** Unexpected phase changes that occurs when the current phase
6502 ** is not a DATA IN or DATA OUT phase are due to error conditions.
6503 ** Such event may only happen when the SCRIPTS is using a
6504 ** multibyte SCSI MOVE.
6506 ** Phase change Some possible cause
6508 ** COMMAND --> MSG IN SCSI parity error detected by target.
6509 ** COMMAND --> STATUS Bad command or refused by target.
6510 ** MSG OUT --> MSG IN Message rejected by target.
6511 ** MSG OUT --> COMMAND Bogus target that discards extended
6512 ** negotiation messages.
6514 ** The code below does not care of the new phase and so
6515 ** trusts the target. Why to annoy it ?
6516 ** If the interrupted phase is COMMAND phase, we restart at
6518 ** If a target does not get all the messages after selection,
6519 ** the code assumes blindly that the target discards extended
6520 ** messages and clears the negotiation status.
6521 ** If the target does not want all our response to negotiation,
6522 ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
6523 ** bloat for such a should_not_happen situation).
6524 ** In all other situation, we reset the BUS.
6525 ** Are these assumptions reasonnable ? (Wait and see ...)
6532 case 2: /* COMMAND phase */
6533 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
6536 case 3: /* STATUS phase */
6537 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
6540 case 6: /* MSG OUT phase */
6541 np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
6542 if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
6543 cp->host_status = HS_BUSY;
6544 nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
6546 else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
6547 dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
6548 nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
6552 case 7: /* MSG IN phase */
6553 nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
6564 ncr_start_reset(np);
6568 static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
6570 struct scsi_cmnd *cmd = cp->cmd;
6571 struct tcb *tp = &np->target[cmd->device->id];
6572 struct lcb *lp = tp->lp[cmd->device->lun];
6573 struct list_head *qp;
6578 u_char s_status = INB (SS_PRT);
6581 ** Let the SCRIPTS processor skip all not yet started CCBs,
6582 ** and count disconnected CCBs. Since the busy queue is in
6583 ** the same order as the chip start queue, disconnected CCBs
6584 ** are before cp and busy ones after.
6587 qp = lp->busy_ccbq.prev;
6588 while (qp != &lp->busy_ccbq) {
6589 cp2 = list_entry(qp, struct ccb, link_ccbq);
6594 cp2->start.schedule.l_paddr =
6595 cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
6597 lp->held_ccb = cp; /* Requeue when this one completes */
6598 disc_cnt = lp->queuedccbs - busy_cnt;
6602 default: /* Just for safety, should never happen */
6605 ** Decrease number of tags to the number of
6606 ** disconnected commands.
6610 if (bootverbose >= 1) {
6611 PRINT_ADDR(cmd, "QUEUE FULL! %d busy, %d disconnected "
6612 "CCBs\n", busy_cnt, disc_cnt);
6614 if (disc_cnt < lp->numtags) {
6615 lp->numtags = disc_cnt > 2 ? disc_cnt : 2;
6617 ncr_setup_tags (np, cmd->device);
6620 ** Requeue the command to the start queue.
6621 ** If any disconnected commands,
6623 ** Jump to reselect.
6625 cp->phys.header.savep = cp->startp;
6626 cp->host_status = HS_BUSY;
6627 cp->scsi_status = S_ILLEGAL;
6629 ncr_put_start_queue(np, cp);
6631 INB (nc_ctest2); /* Clear SIGP */
6632 OUTL_DSP (NCB_SCRIPT_PHYS (np, reselect));
6637 ** If we were requesting sense, give up.
6643 ** Device returned CHECK CONDITION status.
6644 ** Prepare all needed data strutures for getting
6649 cp->scsi_smsg2[0] = IDENTIFY(0, cmd->device->lun);
6650 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
6651 cp->phys.smsg.size = cpu_to_scr(1);
6656 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
6657 cp->phys.cmd.size = cpu_to_scr(6);
6660 ** patch requested size into sense command
6662 cp->sensecmd[0] = 0x03;
6663 cp->sensecmd[1] = cmd->device->lun << 5;
6664 cp->sensecmd[4] = sizeof(cp->sense_buf);
6669 memset(cp->sense_buf, 0, sizeof(cp->sense_buf));
6670 cp->phys.sense.addr = cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
6671 cp->phys.sense.size = cpu_to_scr(sizeof(cp->sense_buf));
6674 ** requeue the command.
6676 startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
6678 cp->phys.header.savep = startp;
6679 cp->phys.header.goalp = startp + 24;
6680 cp->phys.header.lastp = startp;
6681 cp->phys.header.wgoalp = startp + 24;
6682 cp->phys.header.wlastp = startp;
6684 cp->host_status = HS_BUSY;
6685 cp->scsi_status = S_ILLEGAL;
6686 cp->auto_sense = s_status;
6688 cp->start.schedule.l_paddr =
6689 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
6692 ** Select without ATN for quirky devices.
6694 if (cmd->device->select_no_atn)
6695 cp->start.schedule.l_paddr =
6696 cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
6698 ncr_put_start_queue(np, cp);
6700 OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
6710 /*==========================================================
6713 ** ncr chip exception handler for programmed interrupts.
6716 **==========================================================
6719 void ncr_int_sir (struct ncb *np)
6722 u_char chg, ofs, per, fak, wide;
6723 u_char num = INB (nc_dsps);
6724 struct ccb *cp=NULL;
6725 u_long dsa = INL (nc_dsa);
6726 u_char target = INB (nc_sdid) & 0x0f;
6727 struct tcb *tp = &np->target[target];
6728 struct scsi_target *starget = tp->starget;
6730 if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
6735 ** This is used for HP Zalon/53c720 where INTFLY
6736 ** operation is currently broken.
6738 ncr_wakeup_done(np);
6739 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
6740 OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, done_end) + 8);
6742 OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, start));
6745 case SIR_RESEL_NO_MSG_IN:
6746 case SIR_RESEL_NO_IDENTIFY:
6748 ** If devices reselecting without sending an IDENTIFY
6749 ** message still exist, this should help.
6750 ** We just assume lun=0, 1 CCB, no tag.
6753 OUTL_DSP (scr_to_cpu(tp->lp[0]->jump_ccb[0]));
6756 case SIR_RESEL_BAD_TARGET: /* Will send a TARGET RESET message */
6757 case SIR_RESEL_BAD_LUN: /* Will send a TARGET RESET message */
6758 case SIR_RESEL_BAD_I_T_L_Q: /* Will send an ABORT TAG message */
6759 case SIR_RESEL_BAD_I_T_L: /* Will send an ABORT message */
6760 printk ("%s:%d: SIR %d, "
6761 "incorrect nexus identification on reselection\n",
6762 ncr_name (np), target, num);
6764 case SIR_DONE_OVERFLOW:
6765 printk ("%s:%d: SIR %d, "
6766 "CCB done queue overflow\n",
6767 ncr_name (np), target, num);
6769 case SIR_BAD_STATUS:
6771 if (!cp || CCB_PHYS (cp, phys) != dsa)
6773 ncr_sir_to_redo(np, num, cp);
6780 while (cp && (CCB_PHYS (cp, phys) != dsa))
6784 BUG_ON(cp != np->header.cp);
6786 if (!cp || cp != np->header.cp)
6791 /*-----------------------------------------------------------------------------
6793 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
6794 ** ("Everything you've always wanted to know about transfer mode
6797 ** We try to negotiate sync and wide transfer only after
6798 ** a successful inquire command. We look at byte 7 of the
6799 ** inquire data to determine the capabilities of the target.
6801 ** When we try to negotiate, we append the negotiation message
6802 ** to the identify and (maybe) simple tag message.
6803 ** The host status field is set to HS_NEGOTIATE to mark this
6806 ** If the target doesn't answer this message immidiately
6807 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
6808 ** will be raised eventually.
6809 ** The handler removes the HS_NEGOTIATE status, and sets the
6810 ** negotiated value to the default (async / nowide).
6812 ** If we receive a matching answer immediately, we check it
6813 ** for validity, and set the values.
6815 ** If we receive a Reject message immediately, we assume the
6816 ** negotiation has failed, and fall back to standard values.
6818 ** If we receive a negotiation message while not in HS_NEGOTIATE
6819 ** state, it's a target initiated negotiation. We prepare a
6820 ** (hopefully) valid answer, set our parameters, and send back
6821 ** this answer to the target.
6823 ** If the target doesn't fetch the answer (no message out phase),
6824 ** we assume the negotiation has failed, and fall back to default
6827 ** When we set the values, we adjust them in all ccbs belonging
6828 ** to this target, in the controller's register, and in the "phys"
6829 ** field of the controller's struct ncb.
6831 ** Possible cases: hs sir msg_in value send goto
6832 ** We try to negotiate:
6833 ** -> target doesn't msgin NEG FAIL noop defa. - dispatch
6834 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
6835 ** -> target answered (ok) NEG SYNC sdtr set - clrack
6836 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
6837 ** -> target answered (ok) NEG WIDE wdtr set - clrack
6838 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
6839 ** -> any other msgin NEG FAIL noop defa. - dispatch
6841 ** Target tries to negotiate:
6842 ** -> incoming message --- SYNC sdtr set SDTR -
6843 ** -> incoming message --- WIDE wdtr set WDTR -
6844 ** We sent our answer:
6845 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
6847 **-----------------------------------------------------------------------------
6850 case SIR_NEGO_FAILED:
6851 /*-------------------------------------------------------
6853 ** Negotiation failed.
6854 ** Target doesn't send an answer message,
6855 ** or target rejected our message.
6857 ** Remove negotiation request.
6859 **-------------------------------------------------------
6861 OUTB (HS_PRT, HS_BUSY);
6865 case SIR_NEGO_PROTO:
6866 /*-------------------------------------------------------
6868 ** Negotiation failed.
6869 ** Target doesn't fetch the answer message.
6871 **-------------------------------------------------------
6874 if (DEBUG_FLAGS & DEBUG_NEGO) {
6875 PRINT_ADDR(cp->cmd, "negotiation failed sir=%x "
6876 "status=%x.\n", num, cp->nego_status);
6880 ** any error in negotiation:
6881 ** fall back to default mode.
6883 switch (cp->nego_status) {
6886 spi_period(starget) = 0;
6887 spi_offset(starget) = 0;
6888 ncr_setsync (np, cp, 0, 0xe0);
6892 spi_width(starget) = 0;
6893 ncr_setwide (np, cp, 0, 0);
6897 np->msgin [0] = NOP;
6898 np->msgout[0] = NOP;
6899 cp->nego_status = 0;
6903 if (DEBUG_FLAGS & DEBUG_NEGO) {
6904 ncr_print_msg(cp, "sync msgin", np->msgin);
6910 if (ofs==0) per=255;
6913 ** if target sends SDTR message,
6914 ** it CAN transfer synch.
6918 spi_support_sync(starget) = 1;
6921 ** check values against driver limits.
6924 if (per < np->minsync)
6925 {chg = 1; per = np->minsync;}
6926 if (per < tp->minsync)
6927 {chg = 1; per = tp->minsync;}
6928 if (ofs > tp->maxoffs)
6929 {chg = 1; ofs = tp->maxoffs;}
6932 ** Check against controller limits.
6937 ncr_getsync(np, per, &fak, &scntl3);
6950 if (DEBUG_FLAGS & DEBUG_NEGO) {
6951 PRINT_ADDR(cp->cmd, "sync: per=%d scntl3=0x%x ofs=%d "
6952 "fak=%d chg=%d.\n", per, scntl3, ofs, fak, chg);
6955 if (INB (HS_PRT) == HS_NEGOTIATE) {
6956 OUTB (HS_PRT, HS_BUSY);
6957 switch (cp->nego_status) {
6960 /* This was an answer message */
6962 /* Answer wasn't acceptable. */
6963 spi_period(starget) = 0;
6964 spi_offset(starget) = 0;
6965 ncr_setsync(np, cp, 0, 0xe0);
6966 OUTL_DSP(NCB_SCRIPT_PHYS (np, msg_bad));
6969 spi_period(starget) = per;
6970 spi_offset(starget) = ofs;
6971 ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
6972 OUTL_DSP(NCB_SCRIPT_PHYS (np, clrack));
6977 spi_width(starget) = 0;
6978 ncr_setwide(np, cp, 0, 0);
6984 ** It was a request. Set value and
6985 ** prepare an answer message
6988 spi_period(starget) = per;
6989 spi_offset(starget) = ofs;
6990 ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
6992 np->msgout[0] = EXTENDED_MESSAGE;
6994 np->msgout[2] = EXTENDED_SDTR;
6995 np->msgout[3] = per;
6996 np->msgout[4] = ofs;
6998 cp->nego_status = NS_SYNC;
7000 if (DEBUG_FLAGS & DEBUG_NEGO) {
7001 ncr_print_msg(cp, "sync msgout", np->msgout);
7005 OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
7008 np->msgin [0] = NOP;
7014 ** Wide request message received.
7016 if (DEBUG_FLAGS & DEBUG_NEGO) {
7017 ncr_print_msg(cp, "wide msgin", np->msgin);
7021 ** get requested values.
7025 wide = np->msgin[3];
7028 ** if target sends WDTR message,
7029 ** it CAN transfer wide.
7032 if (wide && starget)
7033 spi_support_wide(starget) = 1;
7036 ** check values against driver limits.
7039 if (wide > tp->usrwide)
7040 {chg = 1; wide = tp->usrwide;}
7042 if (DEBUG_FLAGS & DEBUG_NEGO) {
7043 PRINT_ADDR(cp->cmd, "wide: wide=%d chg=%d.\n", wide,
7047 if (INB (HS_PRT) == HS_NEGOTIATE) {
7048 OUTB (HS_PRT, HS_BUSY);
7049 switch (cp->nego_status) {
7053 ** This was an answer message
7056 /* Answer wasn't acceptable. */
7057 spi_width(starget) = 0;
7058 ncr_setwide(np, cp, 0, 1);
7059 OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
7062 spi_width(starget) = wide;
7063 ncr_setwide(np, cp, wide, 1);
7064 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
7069 spi_period(starget) = 0;
7070 spi_offset(starget) = 0;
7071 ncr_setsync(np, cp, 0, 0xe0);
7077 ** It was a request, set value and
7078 ** prepare an answer message
7081 spi_width(starget) = wide;
7082 ncr_setwide(np, cp, wide, 1);
7084 np->msgout[0] = EXTENDED_MESSAGE;
7086 np->msgout[2] = EXTENDED_WDTR;
7087 np->msgout[3] = wide;
7089 np->msgin [0] = NOP;
7091 cp->nego_status = NS_WIDE;
7093 if (DEBUG_FLAGS & DEBUG_NEGO) {
7094 ncr_print_msg(cp, "wide msgout", np->msgin);
7098 /*--------------------------------------------------------------------
7100 ** Processing of special messages
7102 **--------------------------------------------------------------------
7105 case SIR_REJECT_RECEIVED:
7106 /*-----------------------------------------------
7108 ** We received a MESSAGE_REJECT.
7110 **-----------------------------------------------
7113 PRINT_ADDR(cp->cmd, "MESSAGE_REJECT received (%x:%x).\n",
7114 (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
7117 case SIR_REJECT_SENT:
7118 /*-----------------------------------------------
7120 ** We received an unknown message
7122 **-----------------------------------------------
7125 ncr_print_msg(cp, "MESSAGE_REJECT sent for", np->msgin);
7128 /*--------------------------------------------------------------------
7130 ** Processing of special messages
7132 **--------------------------------------------------------------------
7135 case SIR_IGN_RESIDUE:
7136 /*-----------------------------------------------
7138 ** We received an IGNORE RESIDUE message,
7139 ** which couldn't be handled by the script.
7141 **-----------------------------------------------
7144 PRINT_ADDR(cp->cmd, "IGNORE_WIDE_RESIDUE received, but not yet "
7148 case SIR_MISSING_SAVE:
7149 /*-----------------------------------------------
7151 ** We received an DISCONNECT message,
7152 ** but the datapointer wasn't saved before.
7154 **-----------------------------------------------
7157 PRINT_ADDR(cp->cmd, "DISCONNECT received, but datapointer "
7158 "not saved: data=%x save=%x goal=%x.\n",
7159 (unsigned) INL (nc_temp),
7160 (unsigned) scr_to_cpu(np->header.savep),
7161 (unsigned) scr_to_cpu(np->header.goalp));
7170 /*==========================================================
7173 ** Acquire a control block
7176 **==========================================================
7179 static struct ccb *ncr_get_ccb(struct ncb *np, struct scsi_cmnd *cmd)
7181 u_char tn = cmd->device->id;
7182 u_char ln = cmd->device->lun;
7183 struct tcb *tp = &np->target[tn];
7184 struct lcb *lp = tp->lp[ln];
7185 u_char tag = NO_TAG;
7186 struct ccb *cp = NULL;
7189 ** Lun structure available ?
7192 struct list_head *qp;
7194 ** Keep from using more tags than we can handle.
7196 if (lp->usetags && lp->busyccbs >= lp->maxnxs)
7200 ** Allocate a new CCB if needed.
7202 if (list_empty(&lp->free_ccbq))
7203 ncr_alloc_ccb(np, tn, ln);
7206 ** Look for free CCB
7208 qp = ncr_list_pop(&lp->free_ccbq);
7210 cp = list_entry(qp, struct ccb, link_ccbq);
7212 PRINT_ADDR(cmd, "ccb free list corrupted "
7216 list_add_tail(qp, &lp->wait_ccbq);
7222 ** If a CCB is available,
7223 ** Get a tag for this nexus if required.
7227 tag = lp->cb_tags[lp->ia_tag];
7229 else if (lp->actccbs > 0)
7234 ** if nothing available, take the default.
7240 ** Wait until available.
7244 if (flags & SCSI_NOSLEEP) break;
7245 if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
7256 ** Move to next available tag if tag used.
7259 if (tag != NO_TAG) {
7261 if (lp->ia_tag == MAX_TAGS)
7263 lp->tags_umap |= (((tagmap_t) 1) << tag);
7268 ** Remember all informations needed to free this CCB.
7274 if (DEBUG_FLAGS & DEBUG_TAGS) {
7275 PRINT_ADDR(cmd, "ccb @%p using tag %d.\n", cp, tag);
7281 /*==========================================================
7284 ** Release one control block
7287 **==========================================================
7290 static void ncr_free_ccb (struct ncb *np, struct ccb *cp)
7292 struct tcb *tp = &np->target[cp->target];
7293 struct lcb *lp = tp->lp[cp->lun];
7295 if (DEBUG_FLAGS & DEBUG_TAGS) {
7296 PRINT_ADDR(cp->cmd, "ccb @%p freeing tag %d.\n", cp, cp->tag);
7300 ** If lun control block available,
7301 ** decrement active commands and increment credit,
7302 ** free the tag if any and remove the JUMP for reselect.
7305 if (cp->tag != NO_TAG) {
7306 lp->cb_tags[lp->if_tag++] = cp->tag;
7307 if (lp->if_tag == MAX_TAGS)
7309 lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
7310 lp->tags_smap &= lp->tags_umap;
7311 lp->jump_ccb[cp->tag] =
7312 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
7315 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
7320 ** Make this CCB available.
7325 list_move(&cp->link_ccbq, &lp->free_ccbq);
7331 cp -> host_status = HS_IDLE;
7340 wakeup ((caddr_t) cp);
7345 #define ncr_reg_bus_addr(r) (np->paddr + offsetof (struct ncr_reg, r))
7347 /*------------------------------------------------------------------------
7348 ** Initialize the fixed part of a CCB structure.
7349 **------------------------------------------------------------------------
7350 **------------------------------------------------------------------------
7352 static void ncr_init_ccb(struct ncb *np, struct ccb *cp)
7354 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7357 ** Remember virtual and bus address of this ccb.
7359 cp->p_ccb = vtobus(cp);
7360 cp->phys.header.cp = cp;
7363 ** This allows list_del to work for the default ccb.
7365 INIT_LIST_HEAD(&cp->link_ccbq);
7368 ** Initialyze the start and restart launch script.
7370 ** COPY(4) @(...p_phys), @(dsa)
7371 ** JUMP @(sched_point)
7373 cp->start.setup_dsa[0] = cpu_to_scr(copy_4);
7374 cp->start.setup_dsa[1] = cpu_to_scr(CCB_PHYS(cp, start.p_phys));
7375 cp->start.setup_dsa[2] = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
7376 cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
7377 cp->start.p_phys = cpu_to_scr(CCB_PHYS(cp, phys));
7379 memcpy(&cp->restart, &cp->start, sizeof(cp->restart));
7381 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
7382 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
7386 /*------------------------------------------------------------------------
7387 ** Allocate a CCB and initialize its fixed part.
7388 **------------------------------------------------------------------------
7389 **------------------------------------------------------------------------
7391 static void ncr_alloc_ccb(struct ncb *np, u_char tn, u_char ln)
7393 struct tcb *tp = &np->target[tn];
7394 struct lcb *lp = tp->lp[ln];
7395 struct ccb *cp = NULL;
7398 ** Allocate memory for this CCB.
7400 cp = m_calloc_dma(sizeof(struct ccb), "CCB");
7405 ** Count it and initialyze it.
7409 memset(cp, 0, sizeof (*cp));
7410 ncr_init_ccb(np, cp);
7413 ** Chain into wakeup list and free ccb queue and take it
7414 ** into account for tagged commands.
7416 cp->link_ccb = np->ccb->link_ccb;
7417 np->ccb->link_ccb = cp;
7419 list_add(&cp->link_ccbq, &lp->free_ccbq);
7422 /*==========================================================
7425 ** Allocation of resources for Targets/Luns/Tags.
7428 **==========================================================
7432 /*------------------------------------------------------------------------
7433 ** Target control block initialisation.
7434 **------------------------------------------------------------------------
7435 ** This data structure is fully initialized after a SCSI command
7436 ** has been successfully completed for this target.
7437 ** It contains a SCRIPT that is called on target reselection.
7438 **------------------------------------------------------------------------
7440 static void ncr_init_tcb (struct ncb *np, u_char tn)
7442 struct tcb *tp = &np->target[tn];
7443 ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
7448 ** Jump to next tcb if SFBR does not match this target.
7449 ** JUMP IF (SFBR != #target#), @(next tcb)
7451 tp->jump_tcb.l_cmd =
7452 cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
7453 tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
7456 ** Load the synchronous transfer register.
7457 ** COPY @(tp->sval), @(sxfer)
7459 tp->getscr[0] = cpu_to_scr(copy_1);
7460 tp->getscr[1] = cpu_to_scr(vtobus (&tp->sval));
7461 #ifdef SCSI_NCR_BIG_ENDIAN
7462 tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer) ^ 3);
7464 tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
7468 ** Load the timing register.
7469 ** COPY @(tp->wval), @(scntl3)
7471 tp->getscr[3] = cpu_to_scr(copy_1);
7472 tp->getscr[4] = cpu_to_scr(vtobus (&tp->wval));
7473 #ifdef SCSI_NCR_BIG_ENDIAN
7474 tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3) ^ 3);
7476 tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
7480 ** Get the IDENTIFY message and the lun.
7481 ** CALL @script(resel_lun)
7483 tp->call_lun.l_cmd = cpu_to_scr(SCR_CALL);
7484 tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
7487 ** Look for the lun control block of this nexus.
7489 ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
7491 for (i = 0 ; i < 4 ; i++) {
7492 tp->jump_lcb[i].l_cmd =
7493 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
7494 tp->jump_lcb[i].l_paddr =
7495 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
7499 ** Link this target control block to the JUMP chain.
7501 np->jump_tcb[th].l_paddr = cpu_to_scr(vtobus (&tp->jump_tcb));
7504 ** These assert's should be moved at driver initialisations.
7506 #ifdef SCSI_NCR_BIG_ENDIAN
7507 BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
7508 offsetof(struct tcb , sval )) &3) != 3);
7509 BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
7510 offsetof(struct tcb , wval )) &3) != 3);
7512 BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
7513 offsetof(struct tcb , sval )) &3) != 0);
7514 BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
7515 offsetof(struct tcb , wval )) &3) != 0);
7520 /*------------------------------------------------------------------------
7521 ** Lun control block allocation and initialization.
7522 **------------------------------------------------------------------------
7523 ** This data structure is allocated and initialized after a SCSI
7524 ** command has been successfully completed for this target/lun.
7525 **------------------------------------------------------------------------
7527 static struct lcb *ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln)
7529 struct tcb *tp = &np->target[tn];
7530 struct lcb *lp = tp->lp[ln];
7531 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7535 ** Already done, return.
7541 ** Allocate the lcb.
7543 lp = m_calloc_dma(sizeof(struct lcb), "LCB");
7546 memset(lp, 0, sizeof(*lp));
7550 ** Initialize the target control block if not yet.
7552 if (!tp->jump_tcb.l_cmd)
7553 ncr_init_tcb(np, tn);
7556 ** Initialize the CCB queue headers.
7558 INIT_LIST_HEAD(&lp->free_ccbq);
7559 INIT_LIST_HEAD(&lp->busy_ccbq);
7560 INIT_LIST_HEAD(&lp->wait_ccbq);
7561 INIT_LIST_HEAD(&lp->skip_ccbq);
7564 ** Set max CCBs to 1 and use the default 1 entry
7565 ** jump table by default.
7568 lp->jump_ccb = &lp->jump_ccb_0;
7569 lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
7572 ** Initilialyze the reselect script:
7574 ** Jump to next lcb if SFBR does not match this lun.
7575 ** Load TEMP with the CCB direct jump table bus address.
7576 ** Get the SIMPLE TAG message and the tag.
7578 ** JUMP IF (SFBR != #lun#), @(next lcb)
7579 ** COPY @(lp->p_jump_ccb), @(temp)
7580 ** JUMP @script(resel_notag)
7582 lp->jump_lcb.l_cmd =
7583 cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
7584 lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
7586 lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
7587 lp->load_jump_ccb[1] = cpu_to_scr(vtobus (&lp->p_jump_ccb));
7588 lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
7590 lp->jump_tag.l_cmd = cpu_to_scr(SCR_JUMP);
7591 lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
7594 ** Link this lun control block to the JUMP chain.
7596 tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtobus (&lp->jump_lcb));
7599 ** Initialize command queuing control.
7609 /*------------------------------------------------------------------------
7610 ** Lun control block setup on INQUIRY data received.
7611 **------------------------------------------------------------------------
7612 ** We only support WIDE, SYNC for targets and CMDQ for logical units.
7613 ** This setup is done on each INQUIRY since we are expecting user
7614 ** will play with CHANGE DEFINITION commands. :-)
7615 **------------------------------------------------------------------------
7617 static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
7619 unsigned char tn = sdev->id, ln = sdev->lun;
7620 struct tcb *tp = &np->target[tn];
7621 struct lcb *lp = tp->lp[ln];
7623 /* If no lcb, try to allocate it. */
7624 if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
7628 ** If unit supports tagged commands, allocate the
7629 ** CCB JUMP table if not yet.
7631 if (sdev->tagged_supported && lp->jump_ccb == &lp->jump_ccb_0) {
7633 lp->jump_ccb = m_calloc_dma(256, "JUMP_CCB");
7634 if (!lp->jump_ccb) {
7635 lp->jump_ccb = &lp->jump_ccb_0;
7638 lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
7639 for (i = 0 ; i < 64 ; i++)
7641 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
7642 for (i = 0 ; i < MAX_TAGS ; i++)
7644 lp->maxnxs = MAX_TAGS;
7645 lp->tags_stime = jiffies + 3*HZ;
7646 ncr_setup_tags (np, sdev);
7654 /*==========================================================
7657 ** Build Scatter Gather Block
7660 **==========================================================
7662 ** The transfer area may be scattered among
7663 ** several non adjacent physical pages.
7665 ** We may use MAX_SCATTER blocks.
7667 **----------------------------------------------------------
7671 ** We try to reduce the number of interrupts caused
7672 ** by unexpected phase changes due to disconnects.
7673 ** A typical harddisk may disconnect before ANY block.
7674 ** If we wanted to avoid unexpected phase changes at all
7675 ** we had to use a break point every 512 bytes.
7676 ** Of course the number of scatter/gather blocks is
7678 ** Under Linux, the scatter/gatter blocks are provided by
7679 ** the generic driver. We just have to copy addresses and
7680 ** sizes to the data segment array.
7683 static int ncr_scatter_no_sglist(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
7685 struct scr_tblmove *data = &cp->phys.data[MAX_SCATTER - 1];
7688 cp->data_len = cmd->request_bufflen;
7690 if (cmd->request_bufflen) {
7691 dma_addr_t baddr = map_scsi_single_data(np, cmd);
7693 ncr_build_sge(np, data, baddr, cmd->request_bufflen);
7705 static int ncr_scatter(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
7708 int use_sg = (int) cmd->use_sg;
7713 segment = ncr_scatter_no_sglist(np, cp, cmd);
7714 else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
7715 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
7716 struct scr_tblmove *data;
7718 if (use_sg > MAX_SCATTER) {
7719 unmap_scsi_data(np, cmd);
7723 data = &cp->phys.data[MAX_SCATTER - use_sg];
7725 for (segment = 0; segment < use_sg; segment++) {
7726 dma_addr_t baddr = sg_dma_address(&scatter[segment]);
7727 unsigned int len = sg_dma_len(&scatter[segment]);
7729 ncr_build_sge(np, &data[segment], baddr, len);
7730 cp->data_len += len;
7739 /*==========================================================
7742 ** Test the bus snoop logic :-(
7744 ** Has to be called with interrupts disabled.
7747 **==========================================================
7750 static int __init ncr_regtest (struct ncb* np)
7752 register volatile u32 data;
7754 ** ncr registers may NOT be cached.
7755 ** write 0xffffffff to a read only register area,
7756 ** and try to read it back.
7759 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
7760 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
7762 if (data == 0xffffffff) {
7764 if ((data & 0xe2f0fffd) != 0x02000080) {
7766 printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
7773 static int __init ncr_snooptest (struct ncb* np)
7775 u32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
7778 err |= ncr_regtest (np);
7784 pc = NCB_SCRIPTH_PHYS (np, snooptest);
7788 ** Set memory and register.
7790 np->ncr_cache = cpu_to_scr(host_wr);
7791 OUTL (nc_temp, ncr_wr);
7793 ** Start script (exchange values)
7797 ** Wait 'til done (with timeout)
7799 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
7800 if (INB(nc_istat) & (INTF|SIP|DIP))
7803 ** Save termination position.
7807 ** Read memory and register.
7809 host_rd = scr_to_cpu(np->ncr_cache);
7810 ncr_rd = INL (nc_scratcha);
7811 ncr_bk = INL (nc_temp);
7815 ncr_chip_reset(np, 100);
7817 ** check for timeout
7819 if (i>=NCR_SNOOP_TIMEOUT) {
7820 printk ("CACHE TEST FAILED: timeout.\n");
7824 ** Check termination position.
7826 if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
7827 printk ("CACHE TEST FAILED: script execution failed.\n");
7828 printk ("start=%08lx, pc=%08lx, end=%08lx\n",
7829 (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
7830 (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
7836 if (host_wr != ncr_rd) {
7837 printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
7838 (int) host_wr, (int) ncr_rd);
7841 if (host_rd != ncr_wr) {
7842 printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
7843 (int) ncr_wr, (int) host_rd);
7846 if (ncr_bk != ncr_wr) {
7847 printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
7848 (int) ncr_wr, (int) ncr_bk);
7854 /*==========================================================
7856 ** Determine the ncr's clock frequency.
7857 ** This is essential for the negotiation
7858 ** of the synchronous transfer rate.
7860 **==========================================================
7862 ** Note: we have to return the correct value.
7863 ** THERE IS NO SAFE DEFAULT VALUE.
7865 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
7866 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
7867 ** do not have a clock doubler and so are provided with a
7868 ** 80 MHz clock. All other fast20 boards incorporate a doubler
7869 ** and so should be delivered with a 40 MHz clock.
7870 ** The future fast40 chips (895/895) use a 40 Mhz base clock
7871 ** and provide a clock quadrupler (160 Mhz). The code below
7872 ** tries to deal as cleverly as possible with all this stuff.
7874 **----------------------------------------------------------
7878 * Select NCR SCSI clock frequency
7880 static void ncr_selectclock(struct ncb *np, u_char scntl3)
7882 if (np->multiplier < 2) {
7883 OUTB(nc_scntl3, scntl3);
7887 if (bootverbose >= 2)
7888 printk ("%s: enabling clock multiplier\n", ncr_name(np));
7890 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
7891 if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
7893 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
7896 printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
7897 } else /* Wait 20 micro-seconds for doubler */
7899 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
7900 OUTB(nc_scntl3, scntl3);
7901 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
7902 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
7907 * calculate NCR SCSI clock frequency (in KHz)
7909 static unsigned __init ncrgetfreq (struct ncb *np, int gen)
7915 * Measure GEN timer delay in order
7916 * to calculate SCSI clock frequency
7918 * This code will never execute too
7919 * many loop iterations (if DELAY is
7920 * reasonably correct). It could get
7921 * too low a delay (too high a freq.)
7922 * if the CPU is slow executing the
7923 * loop for some reason (an NMI, for
7924 * example). For this reason we will
7925 * if multiple measurements are to be
7926 * performed trust the higher delay
7927 * (lower frequency returned).
7929 OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
7930 OUTW (nc_sien , 0); /* mask all scsi interrupts */
7931 (void) INW (nc_sist); /* clear pending scsi interrupt */
7932 OUTB (nc_dien , 0); /* mask all dma interrupts */
7933 (void) INW (nc_sist); /* another one, just to be sure :) */
7934 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
7935 OUTB (nc_stime1, 0); /* disable general purpose timer */
7936 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
7937 while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
7938 for (count = 0; count < 10; count ++)
7939 udelay(100); /* count ms */
7941 OUTB (nc_stime1, 0); /* disable general purpose timer */
7943 * set prescaler to divide by whatever 0 means
7944 * 0 ought to choose divide by 2, but appears
7945 * to set divide by 3.5 mode in my 53c810 ...
7947 OUTB (nc_scntl3, 0);
7949 if (bootverbose >= 2)
7950 printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
7952 * adjust for prescaler, and convert into KHz
7954 return ms ? ((1 << gen) * 4340) / ms : 0;
7958 * Get/probe NCR SCSI clock frequency
7960 static void __init ncr_getclock (struct ncb *np, int mult)
7962 unsigned char scntl3 = INB(nc_scntl3);
7963 unsigned char stest1 = INB(nc_stest1);
7970 ** True with 875 or 895 with clock multiplier selected
7972 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
7973 if (bootverbose >= 2)
7974 printk ("%s: clock multiplier found\n", ncr_name(np));
7975 np->multiplier = mult;
7979 ** If multiplier not found or scntl3 not 7,5,3,
7980 ** reset chip and get frequency from general purpose timer.
7981 ** Otherwise trust scntl3 BIOS setting.
7983 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
7986 ncr_chip_reset(np, 5);
7988 (void) ncrgetfreq (np, 11); /* throw away first result */
7989 f1 = ncrgetfreq (np, 11);
7990 f2 = ncrgetfreq (np, 11);
7993 printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
7995 if (f1 > f2) f1 = f2; /* trust lower result */
7997 if (f1 < 45000) f1 = 40000;
7998 else if (f1 < 55000) f1 = 50000;
8001 if (f1 < 80000 && mult > 1) {
8002 if (bootverbose >= 2)
8003 printk ("%s: clock multiplier assumed\n", ncr_name(np));
8004 np->multiplier = mult;
8007 if ((scntl3 & 7) == 3) f1 = 40000;
8008 else if ((scntl3 & 7) == 5) f1 = 80000;
8011 f1 /= np->multiplier;
8015 ** Compute controller synchronous parameters.
8017 f1 *= np->multiplier;
8021 /*===================== LINUX ENTRY POINTS SECTION ==========================*/
8023 static int ncr53c8xx_slave_alloc(struct scsi_device *device)
8025 struct Scsi_Host *host = device->host;
8026 struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
8027 struct tcb *tp = &np->target[device->id];
8028 tp->starget = device->sdev_target;
8033 static int ncr53c8xx_slave_configure(struct scsi_device *device)
8035 struct Scsi_Host *host = device->host;
8036 struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
8037 struct tcb *tp = &np->target[device->id];
8038 struct lcb *lp = tp->lp[device->lun];
8039 int numtags, depth_to_use;
8041 ncr_setup_lcb(np, device);
8044 ** Select queue depth from driver setup.
8045 ** Donnot use more than configured by user.
8047 ** Donnot use more than our maximum.
8049 numtags = device_queue_depth(np->unit, device->id, device->lun);
8050 if (numtags > tp->usrtags)
8051 numtags = tp->usrtags;
8052 if (!device->tagged_supported)
8054 depth_to_use = numtags;
8055 if (depth_to_use < 2)
8057 if (depth_to_use > MAX_TAGS)
8058 depth_to_use = MAX_TAGS;
8060 scsi_adjust_queue_depth(device,
8061 (device->tagged_supported ?
8062 MSG_SIMPLE_TAG : 0),
8066 ** Since the queue depth is not tunable under Linux,
8067 ** we need to know this value in order not to
8068 ** announce stupid things to user.
8070 ** XXX(hch): As of Linux 2.6 it certainly _is_ tunable..
8071 ** In fact we just tuned it, or did I miss
8072 ** something important? :)
8075 lp->numtags = lp->maxtags = numtags;
8076 lp->scdev_depth = depth_to_use;
8078 ncr_setup_tags (np, device);
8080 #ifdef DEBUG_NCR53C8XX
8081 printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
8082 np->unit, device->id, device->lun, depth_to_use);
8085 if (spi_support_sync(device->sdev_target) &&
8086 !spi_initial_dv(device->sdev_target))
8087 spi_dv_device(device);
8091 static int ncr53c8xx_queue_command (struct scsi_cmnd *cmd, void (* done)(struct scsi_cmnd *))
8093 struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
8094 unsigned long flags;
8097 #ifdef DEBUG_NCR53C8XX
8098 printk("ncr53c8xx_queue_command\n");
8101 cmd->scsi_done = done;
8102 cmd->host_scribble = NULL;
8103 cmd->__data_mapped = 0;
8104 cmd->__data_mapping = 0;
8106 spin_lock_irqsave(&np->smp_lock, flags);
8108 if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
8109 cmd->result = ScsiResult(sts, 0);
8110 #ifdef DEBUG_NCR53C8XX
8111 printk("ncr53c8xx : command not queued - result=%d\n", sts);
8114 #ifdef DEBUG_NCR53C8XX
8116 printk("ncr53c8xx : command successfully queued\n");
8119 spin_unlock_irqrestore(&np->smp_lock, flags);
8121 if (sts != DID_OK) {
8122 unmap_scsi_data(np, cmd);
8130 irqreturn_t ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
8132 unsigned long flags;
8133 struct Scsi_Host *shost = (struct Scsi_Host *)dev_id;
8134 struct host_data *host_data = (struct host_data *)shost->hostdata;
8135 struct ncb *np = host_data->ncb;
8136 struct scsi_cmnd *done_list;
8138 #ifdef DEBUG_NCR53C8XX
8139 printk("ncr53c8xx : interrupt received\n");
8142 if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
8144 spin_lock_irqsave(&np->smp_lock, flags);
8146 done_list = np->done_list;
8147 np->done_list = NULL;
8148 spin_unlock_irqrestore(&np->smp_lock, flags);
8150 if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
8153 ncr_flush_done_cmds(done_list);
8157 static void ncr53c8xx_timeout(unsigned long npref)
8159 struct ncb *np = (struct ncb *) npref;
8160 unsigned long flags;
8161 struct scsi_cmnd *done_list;
8163 spin_lock_irqsave(&np->smp_lock, flags);
8165 done_list = np->done_list;
8166 np->done_list = NULL;
8167 spin_unlock_irqrestore(&np->smp_lock, flags);
8170 ncr_flush_done_cmds(done_list);
8173 static int ncr53c8xx_bus_reset(struct scsi_cmnd *cmd)
8175 struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
8177 unsigned long flags;
8178 struct scsi_cmnd *done_list;
8181 * If the mid-level driver told us reset is synchronous, it seems
8182 * that we must call the done() callback for the involved command,
8183 * even if this command was not queued to the low-level driver,
8184 * before returning SUCCESS.
8187 spin_lock_irqsave(&np->smp_lock, flags);
8188 sts = ncr_reset_bus(np, cmd, 1);
8190 done_list = np->done_list;
8191 np->done_list = NULL;
8192 spin_unlock_irqrestore(&np->smp_lock, flags);
8194 ncr_flush_done_cmds(done_list);
8199 #if 0 /* unused and broken */
8200 static int ncr53c8xx_abort(struct scsi_cmnd *cmd)
8202 struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
8204 unsigned long flags;
8205 struct scsi_cmnd *done_list;
8207 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8208 printk("ncr53c8xx_abort: pid=%lu serial_number=%ld\n",
8209 cmd->pid, cmd->serial_number);
8211 printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
8214 NCR_LOCK_NCB(np, flags);
8216 sts = ncr_abort_command(np, cmd);
8218 done_list = np->done_list;
8219 np->done_list = NULL;
8220 NCR_UNLOCK_NCB(np, flags);
8222 ncr_flush_done_cmds(done_list);
8230 ** Scsi command waiting list management.
8232 ** It may happen that we cannot insert a scsi command into the start queue,
8233 ** in the following circumstances.
8234 ** Too few preallocated ccb(s),
8235 ** maxtags < cmd_per_lun of the Linux host control block,
8237 ** Such scsi commands are inserted into a waiting list.
8238 ** When a scsi command complete, we try to requeue the commands of the
8242 #define next_wcmd host_scribble
8244 static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd)
8246 struct scsi_cmnd *wcmd;
8248 #ifdef DEBUG_WAITING_LIST
8249 printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
8251 cmd->next_wcmd = NULL;
8252 if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
8254 while ((wcmd->next_wcmd) != 0)
8255 wcmd = (struct scsi_cmnd *) wcmd->next_wcmd;
8256 wcmd->next_wcmd = (char *) cmd;
8260 static struct scsi_cmnd *retrieve_from_waiting_list(int to_remove, struct ncb *np, struct scsi_cmnd *cmd)
8262 struct scsi_cmnd **pcmd = &np->waiting_list;
8267 *pcmd = (struct scsi_cmnd *) cmd->next_wcmd;
8268 cmd->next_wcmd = NULL;
8270 #ifdef DEBUG_WAITING_LIST
8271 printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
8275 pcmd = (struct scsi_cmnd **) &(*pcmd)->next_wcmd;
8280 static void process_waiting_list(struct ncb *np, int sts)
8282 struct scsi_cmnd *waiting_list, *wcmd;
8284 waiting_list = np->waiting_list;
8285 np->waiting_list = NULL;
8287 #ifdef DEBUG_WAITING_LIST
8288 if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
8290 while ((wcmd = waiting_list) != 0) {
8291 waiting_list = (struct scsi_cmnd *) wcmd->next_wcmd;
8292 wcmd->next_wcmd = NULL;
8293 if (sts == DID_OK) {
8294 #ifdef DEBUG_WAITING_LIST
8295 printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
8297 sts = ncr_queue_command(np, wcmd);
8299 if (sts != DID_OK) {
8300 #ifdef DEBUG_WAITING_LIST
8301 printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
8303 wcmd->result = ScsiResult(sts, 0);
8304 ncr_queue_done_cmd(np, wcmd);
8311 static ssize_t show_ncr53c8xx_revision(struct class_device *dev, char *buf)
8313 struct Scsi_Host *host = class_to_shost(dev);
8314 struct host_data *host_data = (struct host_data *)host->hostdata;
8316 return snprintf(buf, 20, "0x%x\n", host_data->ncb->revision_id);
8319 static struct class_device_attribute ncr53c8xx_revision_attr = {
8320 .attr = { .name = "revision", .mode = S_IRUGO, },
8321 .show = show_ncr53c8xx_revision,
8324 static struct class_device_attribute *ncr53c8xx_host_attrs[] = {
8325 &ncr53c8xx_revision_attr,
8329 /*==========================================================
8331 ** Boot command line.
8333 **==========================================================
8336 char *ncr53c8xx; /* command line passed by insmod */
8337 module_param(ncr53c8xx, charp, 0);
8340 static int __init ncr53c8xx_setup(char *str)
8342 return sym53c8xx__setup(str);
8346 __setup("ncr53c8xx=", ncr53c8xx_setup);
8351 * Host attach and initialisations.
8353 * Allocate host data and ncb structure.
8354 * Request IO region and remap MMIO region.
8355 * Do chip initialization.
8356 * If all is OK, install interrupt handling and
8357 * start the timer daemon.
8359 struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
8360 int unit, struct ncr_device *device)
8362 struct host_data *host_data;
8363 struct ncb *np = NULL;
8364 struct Scsi_Host *instance = NULL;
8369 tpnt->name = SCSI_NCR_DRIVER_NAME;
8370 if (!tpnt->shost_attrs)
8371 tpnt->shost_attrs = ncr53c8xx_host_attrs;
8373 tpnt->queuecommand = ncr53c8xx_queue_command;
8374 tpnt->slave_configure = ncr53c8xx_slave_configure;
8375 tpnt->slave_alloc = ncr53c8xx_slave_alloc;
8376 tpnt->eh_bus_reset_handler = ncr53c8xx_bus_reset;
8377 tpnt->can_queue = SCSI_NCR_CAN_QUEUE;
8379 tpnt->sg_tablesize = SCSI_NCR_SG_TABLESIZE;
8380 tpnt->cmd_per_lun = SCSI_NCR_CMD_PER_LUN;
8381 tpnt->use_clustering = ENABLE_CLUSTERING;
8383 if (device->differential)
8384 driver_setup.diff_support = device->differential;
8386 printk(KERN_INFO "ncr53c720-%d: rev 0x%x irq %d\n",
8387 unit, device->chip.revision_id, device->slot.irq);
8389 instance = scsi_host_alloc(tpnt, sizeof(*host_data));
8392 host_data = (struct host_data *) instance->hostdata;
8394 np = __m_calloc_dma(device->dev, sizeof(struct ncb), "NCB");
8397 spin_lock_init(&np->smp_lock);
8398 np->dev = device->dev;
8399 np->p_ncb = vtobus(np);
8400 host_data->ncb = np;
8402 np->ccb = m_calloc_dma(sizeof(struct ccb), "CCB");
8406 /* Store input information in the host data structure. */
8408 np->verbose = driver_setup.verbose;
8409 sprintf(np->inst_name, "ncr53c720-%d", np->unit);
8410 np->revision_id = device->chip.revision_id;
8411 np->features = device->chip.features;
8412 np->clock_divn = device->chip.nr_divisor;
8413 np->maxoffs = device->chip.offset_max;
8414 np->maxburst = device->chip.burst_max;
8415 np->myaddr = device->host_id;
8417 /* Allocate SCRIPTS areas. */
8418 np->script0 = m_calloc_dma(sizeof(struct script), "SCRIPT");
8421 np->scripth0 = m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
8425 init_timer(&np->timer);
8426 np->timer.data = (unsigned long) np;
8427 np->timer.function = ncr53c8xx_timeout;
8429 /* Try to map the controller chip to virtual and physical memory. */
8431 np->paddr = device->slot.base;
8432 np->paddr2 = (np->features & FE_RAM) ? device->slot.base_2 : 0;
8434 if (device->slot.base_v)
8435 np->vaddr = device->slot.base_v;
8437 np->vaddr = ioremap(device->slot.base_c, 128);
8441 "%s: can't map memory mapped IO region\n",ncr_name(np));
8444 if (bootverbose > 1)
8446 "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
8449 /* Make the controller's registers available. Now the INB INW INL
8450 * OUTB OUTW OUTL macros can be used safely.
8453 np->reg = (struct ncr_reg __iomem *)np->vaddr;
8455 /* Do chip dependent initialization. */
8456 ncr_prepare_setting(np);
8458 if (np->paddr2 && sizeof(struct script) > 4096) {
8460 printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
8464 instance->max_channel = 0;
8465 instance->this_id = np->myaddr;
8466 instance->max_id = np->maxwide ? 16 : 8;
8467 instance->max_lun = SCSI_NCR_MAX_LUN;
8468 instance->base = (unsigned long) np->reg;
8469 instance->irq = device->slot.irq;
8470 instance->unique_id = device->slot.base;
8471 instance->dma_channel = 0;
8472 instance->cmd_per_lun = MAX_TAGS;
8473 instance->can_queue = (MAX_START-4);
8474 /* This can happen if you forget to call ncr53c8xx_init from
8475 * your module_init */
8476 BUG_ON(!ncr53c8xx_transport_template);
8477 instance->transportt = ncr53c8xx_transport_template;
8479 /* Patch script to physical addresses */
8480 ncr_script_fill(&script0, &scripth0);
8482 np->scripth = np->scripth0;
8483 np->p_scripth = vtobus(np->scripth);
8484 np->p_script = (np->paddr2) ? np->paddr2 : vtobus(np->script0);
8486 ncr_script_copy_and_bind(np, (ncrcmd *) &script0,
8487 (ncrcmd *) np->script0, sizeof(struct script));
8488 ncr_script_copy_and_bind(np, (ncrcmd *) &scripth0,
8489 (ncrcmd *) np->scripth0, sizeof(struct scripth));
8490 np->ccb->p_ccb = vtobus (np->ccb);
8492 /* Patch the script for LED support. */
8494 if (np->features & FE_LED0) {
8495 np->script0->idle[0] =
8496 cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
8497 np->script0->reselected[0] =
8498 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
8499 np->script0->start[0] =
8500 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
8504 * Look for the target control block of this nexus.
8506 * JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
8508 for (i = 0 ; i < 4 ; i++) {
8509 np->jump_tcb[i].l_cmd =
8510 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
8511 np->jump_tcb[i].l_paddr =
8512 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
8515 ncr_chip_reset(np, 100);
8517 /* Now check the cache handling of the chipset. */
8519 if (ncr_snooptest(np)) {
8520 printk(KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
8524 /* Install the interrupt handler. */
8525 np->irq = device->slot.irq;
8527 /* Initialize the fixed part of the default ccb. */
8528 ncr_init_ccb(np, np->ccb);
8531 * After SCSI devices have been opened, we cannot reset the bus
8532 * safely, so we do it here. Interrupt handler does the real work.
8533 * Process the reset exception if interrupts are not enabled yet.
8534 * Then enable disconnects.
8536 spin_lock_irqsave(&np->smp_lock, flags);
8537 if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
8538 printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
8540 spin_unlock_irqrestore(&np->smp_lock, flags);
8548 * The middle-level SCSI driver does not wait for devices to settle.
8549 * Wait synchronously if more than 2 seconds.
8551 if (driver_setup.settle_delay > 2) {
8552 printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
8553 ncr_name(np), driver_setup.settle_delay);
8554 mdelay(1000 * driver_setup.settle_delay);
8557 /* start the timeout daemon */
8561 /* use SIMPLE TAG messages by default */
8562 #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
8563 np->order = SIMPLE_QUEUE_TAG;
8566 spin_unlock_irqrestore(&np->smp_lock, flags);
8573 printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
8577 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
8579 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
8581 m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
8582 m_free_dma(np, sizeof(struct ncb), "NCB");
8583 host_data->ncb = NULL;
8586 scsi_host_put(instance);
8592 int ncr53c8xx_release(struct Scsi_Host *host)
8594 struct host_data *host_data;
8595 #ifdef DEBUG_NCR53C8XX
8596 printk("ncr53c8xx: release\n");
8600 host_data = (struct host_data *)host->hostdata;
8601 if (host_data && host_data->ncb)
8602 ncr_detach(host_data->ncb);
8606 static void ncr53c8xx_set_period(struct scsi_target *starget, int period)
8608 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8609 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8610 struct tcb *tp = &np->target[starget->id];
8612 if (period > np->maxsync)
8613 period = np->maxsync;
8614 else if (period < np->minsync)
8615 period = np->minsync;
8617 tp->usrsync = period;
8619 ncr_negotiate(np, tp);
8622 static void ncr53c8xx_set_offset(struct scsi_target *starget, int offset)
8624 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8625 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8626 struct tcb *tp = &np->target[starget->id];
8628 if (offset > np->maxoffs)
8629 offset = np->maxoffs;
8630 else if (offset < 0)
8633 tp->maxoffs = offset;
8635 ncr_negotiate(np, tp);
8638 static void ncr53c8xx_set_width(struct scsi_target *starget, int width)
8640 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8641 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8642 struct tcb *tp = &np->target[starget->id];
8644 if (width > np->maxwide)
8645 width = np->maxwide;
8649 tp->usrwide = width;
8651 ncr_negotiate(np, tp);
8654 static void ncr53c8xx_get_signalling(struct Scsi_Host *shost)
8656 struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
8657 enum spi_signal_type type;
8659 switch (np->scsi_mode) {
8661 type = SPI_SIGNAL_SE;
8664 type = SPI_SIGNAL_HVD;
8667 type = SPI_SIGNAL_UNKNOWN;
8670 spi_signalling(shost) = type;
8673 static struct spi_function_template ncr53c8xx_transport_functions = {
8674 .set_period = ncr53c8xx_set_period,
8676 .set_offset = ncr53c8xx_set_offset,
8678 .set_width = ncr53c8xx_set_width,
8680 .get_signalling = ncr53c8xx_get_signalling,
8683 int __init ncr53c8xx_init(void)
8685 ncr53c8xx_transport_template = spi_attach_transport(&ncr53c8xx_transport_functions);
8686 if (!ncr53c8xx_transport_template)
8691 void ncr53c8xx_exit(void)
8693 spi_release_transport(ncr53c8xx_transport_template);