Import 2.3.18pre1
[davej-history.git] / drivers / scsi / ncr53c8xx.c
blob383c0c1f1314d835c6275a66cb683d871f33302c
1 /******************************************************************************
2 ** Device driver for the PCI-SCSI NCR538XX controller family.
3 **
4 ** Copyright (C) 1994 Wolfgang Stanglmeier
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **-----------------------------------------------------------------------------
22 ** This driver has been ported to Linux from the FreeBSD NCR53C8XX driver
23 ** and is currently maintained by
25 ** Gerard Roudier <groudier@club-internet.fr>
27 ** Being given that this driver originates from the FreeBSD version, and
28 ** in order to keep synergy on both, any suggested enhancements and corrections
29 ** received on Linux are automatically a potential candidate for the FreeBSD
30 ** version.
32 ** The original driver has been written for 386bsd and FreeBSD by
33 ** Wolfgang Stanglmeier <wolf@cologne.de>
34 ** Stefan Esser <se@mi.Uni-Koeln.de>
36 ** And has been ported to NetBSD by
37 ** Charles M. Hannum <mycroft@gnu.ai.mit.edu>
39 **-----------------------------------------------------------------------------
41 ** Brief history
43 ** December 10 1995 by Gerard Roudier:
44 ** Initial port to Linux.
46 ** June 23 1996 by Gerard Roudier:
47 ** Support for 64 bits architectures (Alpha).
49 ** November 30 1996 by Gerard Roudier:
50 ** Support for Fast-20 scsi.
51 ** Support for large DMA fifo and 128 dwords bursting.
53 ** February 27 1997 by Gerard Roudier:
54 ** Support for Fast-40 scsi.
55 ** Support for on-Board RAM.
57 ** May 3 1997 by Gerard Roudier:
58 ** Full support for scsi scripts instructions pre-fetching.
60 ** May 19 1997 by Richard Waltham <dormouse@farsrobt.demon.co.uk>:
61 ** Support for NvRAM detection and reading.
63 ** August 18 1997 by Cort <cort@cs.nmt.edu>:
64 ** Support for Power/PC (Big Endian).
66 ** June 20 1998 by Gerard Roudier <groudier@club-internet.fr>:
67 ** Support for up to 64 tags per lun.
68 ** O(1) everywhere (C and SCRIPTS) for normal cases.
69 ** Low PCI traffic for command handling when on-chip RAM is present.
70 ** Aggressive SCSI SCRIPTS optimizations.
72 *******************************************************************************
76 ** March 7 1999, version 3.2
78 ** Supported SCSI-II features:
79 ** Synchronous negotiation
80 ** Wide negotiation (depends on the NCR Chip)
81 ** Enable disconnection
82 ** Tagged command queuing
83 ** Parity checking
84 ** Etc...
86 ** Supported NCR chips:
87 ** 53C810 (8 bits, Fast SCSI-2, no rom BIOS)
88 ** 53C815 (8 bits, Fast SCSI-2, on board rom BIOS)
89 ** 53C820 (Wide, Fast SCSI-2, no rom BIOS)
90 ** 53C825 (Wide, Fast SCSI-2, on board rom BIOS)
91 ** 53C860 (8 bits, Fast 20, no rom BIOS)
92 ** 53C875 (Wide, Fast 20, on board rom BIOS)
93 ** 53C895 (Wide, Fast 40, on board rom BIOS)
95 ** Other features:
96 ** Memory mapped IO (linux-1.3.X and above only)
97 ** Module
98 ** Shared IRQ (since linux-1.3.72)
102 ** Name and version of the driver
104 #define SCSI_NCR_DRIVER_NAME "ncr53c8xx - version 3.2"
106 #define SCSI_NCR_DEBUG_FLAGS (0)
108 /*==========================================================
110 ** Include files
112 **==========================================================
115 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
117 #ifdef MODULE
118 #include <linux/module.h>
119 #endif
121 #include <asm/dma.h>
122 #include <asm/io.h>
123 #include <asm/system.h>
124 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
125 #include <linux/spinlock.h>
126 #endif
127 #include <linux/delay.h>
128 #include <linux/signal.h>
129 #include <linux/sched.h>
130 #include <linux/errno.h>
131 #include <linux/pci.h>
132 #include <linux/string.h>
133 #include <linux/malloc.h>
134 #include <linux/mm.h>
135 #include <linux/ioport.h>
136 #include <linux/time.h>
137 #include <linux/timer.h>
138 #include <linux/stat.h>
140 #include <linux/version.h>
141 #include <linux/blk.h>
143 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,35)
144 #include <linux/init.h>
145 #else
146 #ifndef __initdata
147 #define __initdata
148 #endif
149 #endif
151 #if LINUX_VERSION_CODE <= LinuxVersionCode(2,1,92)
152 #include <linux/bios32.h>
153 #endif
155 #include "scsi.h"
156 #include "hosts.h"
157 #include "constants.h"
158 #include "sd.h"
160 #include <linux/types.h>
163 ** Define BITS_PER_LONG for earlier linux versions.
165 #ifndef BITS_PER_LONG
166 #if (~0UL) == 0xffffffffUL
167 #define BITS_PER_LONG 32
168 #else
169 #define BITS_PER_LONG 64
170 #endif
171 #endif
174 ** Define the BSD style u_int32 and u_int64 type.
175 ** Are in fact u_int32_t and u_int64_t :-)
177 typedef u32 u_int32;
178 typedef u64 u_int64;
180 #include "ncr53c8xx.h"
182 /*==========================================================
184 ** A la VMS/CAM-3 queue management.
185 ** Implemented from linux list management.
187 **==========================================================
190 typedef struct xpt_quehead {
191 struct xpt_quehead *flink; /* Forward pointer */
192 struct xpt_quehead *blink; /* Backward pointer */
193 } XPT_QUEHEAD;
195 #define xpt_que_init(ptr) do { \
196 (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
197 } while (0)
199 static inline void __xpt_que_add(struct xpt_quehead * new,
200 struct xpt_quehead * blink,
201 struct xpt_quehead * flink)
203 flink->blink = new;
204 new->flink = flink;
205 new->blink = blink;
206 blink->flink = new;
209 static inline void __xpt_que_del(struct xpt_quehead * blink,
210 struct xpt_quehead * flink)
212 flink->blink = blink;
213 blink->flink = flink;
216 static inline int xpt_que_empty(struct xpt_quehead *head)
218 return head->flink == head;
221 static inline void xpt_que_splice(struct xpt_quehead *list,
222 struct xpt_quehead *head)
224 struct xpt_quehead *first = list->flink;
226 if (first != list) {
227 struct xpt_quehead *last = list->blink;
228 struct xpt_quehead *at = head->flink;
230 first->blink = head;
231 head->flink = first;
233 last->flink = at;
234 at->blink = last;
238 #define xpt_que_entry(ptr, type, member) \
239 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
242 #define xpt_insque(new, pos) __xpt_que_add(new, pos, (pos)->flink)
244 #define xpt_remque(el) __xpt_que_del((el)->blink, (el)->flink)
246 #define xpt_insque_head(new, head) __xpt_que_add(new, head, (head)->flink)
248 static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
250 struct xpt_quehead *elem = head->flink;
252 if (elem != head)
253 __xpt_que_del(head, elem->flink);
254 else
255 elem = 0;
256 return elem;
259 #define xpt_insque_tail(new, head) __xpt_que_add(new, (head)->blink, head)
261 static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
263 struct xpt_quehead *elem = head->blink;
265 if (elem != head)
266 __xpt_que_del(elem->blink, head);
267 else
268 elem = 0;
269 return elem;
272 /*==========================================================
274 ** The CCB done queue uses an array of CCB virtual
275 ** addresses. Empty entries are flagged using the bogus
276 ** virtual address 0xffffffff.
278 ** Since PCI ensures that only aligned DWORDs are accessed
279 ** atomically, 64 bit little-endian architecture requires
280 ** to test the high order DWORD of the entry to determine
281 ** if it is empty or valid.
283 ** BTW, I will make things differently as soon as I will
284 ** have a better idea, but this is simple and should work.
286 **==========================================================
289 #define SCSI_NCR_CCB_DONE_SUPPORT
290 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
292 #define MAX_DONE 24
293 #define CCB_DONE_EMPTY 0xffffffffUL
295 /* All 32 bit architectures */
296 #if BITS_PER_LONG == 32
297 #define CCB_DONE_VALID(cp) (((u_long) cp) != CCB_DONE_EMPTY)
299 /* All > 32 bit (64 bit) architectures regardless endian-ness */
300 #else
301 #define CCB_DONE_VALID(cp) \
302 ((((u_long) cp) & 0xffffffff00000000ul) && \
303 (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
304 #endif
306 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
308 /*==========================================================
310 ** On x86 architecture, write buffers management does
311 ** not reorder writes to memory. So, using compiler
312 ** optimization barriers is enough to guarantee some
313 ** ordering when the CPU is writing data accessed by
314 ** the NCR.
315 ** On Alpha architecture, explicit memory barriers have
316 ** to be used.
317 ** Other architectures are defaulted to mb() macro if
318 ** defined, otherwise use compiler barrier.
320 **==========================================================
323 #if defined(__i386__)
324 #define MEMORY_BARRIER() barrier()
325 #elif defined(__alpha__)
326 #define MEMORY_BARRIER() mb()
327 #else
328 # ifdef mb
329 # define MEMORY_BARRIER() mb()
330 # else
331 # define MEMORY_BARRIER() barrier()
332 # endif
333 #endif
335 /*==========================================================
337 ** Configuration and Debugging
339 **==========================================================
343 ** SCSI address of this device.
344 ** The boot routines should have set it.
345 ** If not, use this.
348 #ifndef SCSI_NCR_MYADDR
349 #define SCSI_NCR_MYADDR (7)
350 #endif
353 ** The maximum number of tags per logic unit.
354 ** Used only for disk devices that support tags.
357 #ifndef SCSI_NCR_MAX_TAGS
358 #define SCSI_NCR_MAX_TAGS (8)
359 #endif
362 ** TAGS are actually limited to 64 tags/lun.
363 ** We need to deal with power of 2, for alignment constraints.
365 #if SCSI_NCR_MAX_TAGS > 64
366 #undef SCSI_NCR_MAX_TAGS
367 #define SCSI_NCR_MAX_TAGS (64)
368 #endif
370 #define NO_TAG (255)
373 ** Choose appropriate type for tag bitmap.
375 #if SCSI_NCR_MAX_TAGS > 32
376 typedef u_int64 tagmap_t;
377 #else
378 typedef u_int32 tagmap_t;
379 #endif
382 ** Number of targets supported by the driver.
383 ** n permits target numbers 0..n-1.
384 ** Default is 16, meaning targets #0..#15.
385 ** #7 .. is myself.
388 #ifdef SCSI_NCR_MAX_TARGET
389 #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
390 #else
391 #define MAX_TARGET (16)
392 #endif
395 ** Number of logic units supported by the driver.
396 ** n enables logic unit numbers 0..n-1.
397 ** The common SCSI devices require only
398 ** one lun, so take 1 as the default.
401 #ifdef SCSI_NCR_MAX_LUN
402 #define MAX_LUN SCSI_NCR_MAX_LUN
403 #else
404 #define MAX_LUN (1)
405 #endif
408 ** Asynchronous pre-scaler (ns). Shall be 40
411 #ifndef SCSI_NCR_MIN_ASYNC
412 #define SCSI_NCR_MIN_ASYNC (40)
413 #endif
416 ** The maximum number of jobs scheduled for starting.
417 ** There should be one slot per target, and one slot
418 ** for each tag of each target in use.
419 ** The calculation below is actually quite silly ...
422 #ifdef SCSI_NCR_CAN_QUEUE
423 #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
424 #else
425 #define MAX_START (MAX_TARGET + 7 * SCSI_NCR_MAX_TAGS)
426 #endif
429 ** The maximum number of segments a transfer is split into.
430 ** We support up to 127 segments for both read and write.
431 ** The data scripts are broken into 2 sub-scripts.
432 ** 80 (MAX_SCATTERL) segments are moved from a sub-script
433 ** in on-chip RAM. This makes data transfers shorter than
434 ** 80k (assuming 1k fs) as fast as possible.
437 #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
439 #if (MAX_SCATTER > 80)
440 #define MAX_SCATTERL 80
441 #define MAX_SCATTERH (MAX_SCATTER - MAX_SCATTERL)
442 #else
443 #define MAX_SCATTERL (MAX_SCATTER-1)
444 #define MAX_SCATTERH 1
445 #endif
448 ** Io mapped or memory mapped.
451 #if defined(SCSI_NCR_IOMAPPED)
452 #define NCR_IOMAPPED
453 #endif
456 ** other
459 #define NCR_SNOOP_TIMEOUT (1000000)
461 /*==========================================================
463 ** Defines for Linux.
465 ** Linux and Bsd kernel functions are quite different.
466 ** These defines allow a minimum change of the original
467 ** code.
469 **==========================================================
473 ** Obvious definitions
476 #define u_char unsigned char
477 #define u_short unsigned short
478 #define u_int unsigned int
479 #define u_long unsigned long
481 typedef u_long vm_offset_t;
482 typedef int vm_size_t;
484 #ifndef bcopy
485 #define bcopy(s, d, n) memcpy((d), (s), (n))
486 #endif
487 #ifndef bzero
488 #define bzero(d, n) memset((d), 0, (n))
489 #endif
491 #ifndef offsetof
492 #define offsetof(t, m) ((size_t) (&((t *)0)->m))
493 #endif
496 ** SMP threading.
498 ** Assuming that SMP systems are generally high end systems and may
499 ** use several SCSI adapters, we are using one lock per controller
500 ** instead of some global one. For the moment (linux-2.1.95), driver's
501 ** entry points are called with the 'io_request_lock' lock held, so:
502 ** - We are uselessly loosing a couple of micro-seconds to lock the
503 ** controller data structure.
504 ** - But the driver is not broken by design for SMP and so can be
505 ** more resistant to bugs or bad changes in the IO sub-system code.
506 ** - A small advantage could be that the interrupt code is grained as
507 ** wished (e.g.: threaded by controller).
510 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
512 #if 0 /* not yet needed */
513 static spinlock_t driver_lock;
514 #define NCR_LOCK_DRIVER(flags) spin_lock_irqsave(&driver_lock, flags)
515 #define NCR_UNLOCK_DRIVER(flags) spin_unlock_irqrestore(&driver_lock, flags)
516 #endif
518 #define NCR_INIT_LOCK_NCB(np) spin_lock_init(&np->smp_lock);
519 #define NCR_LOCK_NCB(np, flags) spin_lock_irqsave(&np->smp_lock, flags)
520 #define NCR_UNLOCK_NCB(np, flags) spin_unlock_irqrestore(&np->smp_lock, flags)
522 # if LINUX_VERSION_CODE < LinuxVersionCode(2,3,99)
524 # define NCR_LOCK_SCSI_DONE(np, flags) \
525 spin_lock_irqsave(&io_request_lock, flags)
526 # define NCR_UNLOCK_SCSI_DONE(np, flags) \
527 spin_unlock_irqrestore(&io_request_lock, flags)
529 # else
531 # define NCR_LOCK_SCSI_DONE(np, flags) do {;} while (0)
532 # define NCR_UNLOCK_SCSI_DONE(np, flags) do {;} while (0)
534 # endif
536 #else
538 #if 0 /* not yet needed */
539 #define NCR_LOCK_DRIVER(flags) do {;} while (0)
540 #define NCR_UNLOCK_DRIVER(flags) do {;} while (0)
541 #endif
543 #define NCR_INIT_LOCK_NCB(np) do { } while (0)
544 #define NCR_LOCK_NCB(np, flags) do { save_flags(flags); cli(); } while (0)
545 #define NCR_UNLOCK_NCB(np, flags) do { restore_flags(flags); } while (0)
547 #define NCR_LOCK_SCSI_DONE(np, flags) do {;} while (0)
548 #define NCR_UNLOCK_SCSI_DONE(np, flags) do {;} while (0)
550 #endif
553 ** Address translation
555 ** The driver has to provide physical memory addresses to
556 ** the script processor. Because some architectures use
557 ** different physical addresses from the PCI BUS, we must
558 ** use virt_to_bus instead of virt_to_phys.
561 #define vtophys(p) virt_to_bus(p)
564 ** Memory mapped IO
566 ** Since linux-2.1, we must use ioremap() to map the io memory space.
567 ** iounmap() to unmap it. That allows portability.
568 ** Linux 1.3.X and 2.0.X allow to remap physical pages addresses greater
569 ** than the highest physical memory address to kernel virtual pages with
570 ** vremap() / vfree(). That was not portable but worked with i386
571 ** architecture.
574 #if LINUX_VERSION_CODE < LinuxVersionCode(2,1,0)
575 #define ioremap vremap
576 #define iounmap vfree
577 #endif
579 #if defined (__sparc__)
580 #include <asm/irq.h>
581 #elif defined (__alpha__)
582 #define bus_dvma_to_mem(p) ((p) & 0xfffffffful)
583 #else
584 #define bus_dvma_to_mem(p) (p)
585 #endif
587 #if defined(__i386__) || !defined(NCR_IOMAPPED)
588 static vm_offset_t __init remap_pci_mem(u_long base, u_long size)
590 u_long page_base = ((u_long) base) & PAGE_MASK;
591 u_long page_offs = ((u_long) base) - page_base;
592 u_long page_remapped = (u_long) ioremap(page_base, page_offs+size);
594 return (vm_offset_t) (page_remapped? (page_remapped + page_offs) : 0UL);
597 static void __init unmap_pci_mem(vm_offset_t vaddr, u_long size)
599 if (vaddr)
600 iounmap((void *) (vaddr & PAGE_MASK));
602 #endif /* __i386__ || !NCR_IOMAPPED */
605 ** Insert a delay in micro-seconds and milli-seconds.
606 ** -------------------------------------------------
607 ** Under Linux, udelay() is restricted to delay < 1 milli-second.
608 ** In fact, it generally works for up to 1 second delay.
609 ** Since 2.1.105, the mdelay() function is provided for delays
610 ** in milli-seconds.
611 ** Under 2.0 kernels, udelay() is an inline function that is very
612 ** inaccurate on Pentium processors.
615 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,105)
616 #define UDELAY udelay
617 #define MDELAY mdelay
618 #else
619 static void UDELAY(long us) { udelay(us); }
620 static void MDELAY(long ms) { while (ms--) UDELAY(1000); }
621 #endif
624 ** Internal data structure allocation.
626 ** Linux scsi memory poor pool is adjusted for the need of
627 ** middle-level scsi driver.
628 ** We allocate our control blocks in the kernel memory pool
629 ** to avoid scsi pool shortage.
631 ** kmalloc() only ensures 8 bytes boundary alignment.
632 ** The NCR need better alignment for cache line bursting.
633 ** The global header is moved between the NCB and CCBs and needs
634 ** origin and destination addresses to have same lower four bits.
636 ** We use 32 boundary alignment for NCB and CCBs and offset multiple
637 ** of 32 for global header fields. That's too much but at least enough.
640 #define ALIGN_SIZE(shift) (1UL << shift)
641 #define ALIGN_MASK(shift) (~(ALIGN_SIZE(shift)-1))
643 #define CACHE_LINE_SHIFT 5
644 #define CACHE_LINE_SIZE ALIGN_SIZE(CACHE_LINE_SHIFT)
645 #define CACHE_LINE_MASK ALIGN_MASK(CACHE_LINE_SHIFT)
647 static void *m_alloc(int size, int a_shift)
649 u_long addr;
650 void *ptr;
651 u_long a_size, a_mask;
653 if (a_shift < 3)
654 a_shift = 3;
656 a_size = ALIGN_SIZE(a_shift);
657 a_mask = ALIGN_MASK(a_shift);
659 ptr = (void *) kmalloc(size + a_size, GFP_ATOMIC);
660 if (ptr) {
661 addr = (((u_long) ptr) + a_size) & a_mask;
662 *((void **) (addr - sizeof(void *))) = ptr;
663 ptr = (void *) addr;
666 return ptr;
669 #ifdef MODULE
670 static void m_free(void *ptr, int size)
672 u_long addr;
674 if (ptr) {
675 addr = (u_long) ptr;
676 ptr = *((void **) (addr - sizeof(void *)));
678 kfree(ptr);
681 #endif
684 ** Transfer direction
686 ** Low-level scsi drivers under Linux do not receive the expected
687 ** data transfer direction from upper scsi drivers.
688 ** The driver will only check actual data direction for common
689 ** scsi opcodes. Other ones may cause problem, since they may
690 ** depend on device type or be vendor specific.
691 ** I would prefer to never trust the device for data direction,
692 ** but that is not possible.
694 ** The original driver requires the expected direction to be known.
695 ** The Linux version of the driver has been enhanced in order to
696 ** be able to transfer data in the direction choosen by the target.
699 #define XFER_IN (1)
700 #define XFER_OUT (2)
703 ** Head of list of NCR boards
705 ** For kernel version < 1.3.70, host is retrieved by its irq level.
706 ** For later kernels, the internal host control block address
707 ** (struct ncb) is used as device id parameter of the irq stuff.
710 static struct Scsi_Host *first_host = NULL;
711 static Scsi_Host_Template *the_template = NULL;
715 ** /proc directory entry and proc_info function
718 static struct proc_dir_entry proc_scsi_ncr53c8xx = {
719 PROC_SCSI_NCR53C8XX, 9, "ncr53c8xx",
720 S_IFDIR | S_IRUGO | S_IXUGO, 2
722 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
723 static int ncr53c8xx_proc_info(char *buffer, char **start, off_t offset,
724 int length, int hostno, int func);
725 #endif
728 ** Driver setup.
730 ** This structure is initialized from linux config options.
731 ** It can be overridden at boot-up by the boot command line.
733 #define SCSI_NCR_MAX_EXCLUDES 8
734 struct ncr_driver_setup {
735 u_char master_parity;
736 u_char scsi_parity;
737 u_char disconnection;
738 u_char special_features;
739 u_char ultra_scsi;
740 u_char force_sync_nego;
741 u_char reverse_probe;
742 u_char pci_fix_up;
743 u_char use_nvram;
744 u_char verbose;
745 u_char default_tags;
746 u_short default_sync;
747 u_short debug;
748 u_char burst_max;
749 u_char led_pin;
750 u_char max_wide;
751 u_char settle_delay;
752 u_char diff_support;
753 u_char irqm;
754 u_char bus_check;
755 u_char optimize;
756 u_char recovery;
757 u_int excludes[SCSI_NCR_MAX_EXCLUDES];
758 char tag_ctrl[100];
761 static struct ncr_driver_setup
762 driver_setup = SCSI_NCR_DRIVER_SETUP;
764 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
765 static struct ncr_driver_setup
766 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
767 # ifdef MODULE
768 char *ncr53c8xx = 0; /* command line passed by insmod */
769 # if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,30)
770 MODULE_PARM(ncr53c8xx, "s");
771 # endif
772 # endif
773 #endif
776 ** Other Linux definitions
779 #define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
781 static void ncr53c8xx_select_queue_depths(
782 struct Scsi_Host *host, struct scsi_device *devlist);
783 static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs);
784 static void ncr53c8xx_timeout(unsigned long np);
786 #define initverbose (driver_setup.verbose)
787 #define bootverbose (np->verbose)
789 #ifdef SCSI_NCR_NVRAM_SUPPORT
791 ** Symbios NvRAM data format
793 #define SYMBIOS_NVRAM_SIZE 368
794 #define SYMBIOS_NVRAM_ADDRESS 0x100
796 struct Symbios_nvram {
797 /* Header 6 bytes */
798 u_short start_marker; /* 0x0000 */
799 u_short byte_count; /* excluding header/trailer */
800 u_short checksum;
802 /* Controller set up 20 bytes */
803 u_short word0; /* 0x3000 */
804 u_short word2; /* 0x0000 */
805 u_short word4; /* 0x0000 */
806 u_short flags;
807 #define SYMBIOS_SCAM_ENABLE (1)
808 #define SYMBIOS_PARITY_ENABLE (1<<1)
809 #define SYMBIOS_VERBOSE_MSGS (1<<2)
810 #define SYMBIOS_CHS_MAPPING (1<<3)
811 u_short flags1;
812 #define SYMBIOS_SCAN_HI_LO (1)
813 u_short word10; /* 0x00 */
814 u_short flags3; /* 0x00 */
815 #define SYMBIOS_REMOVABLE_FLAGS (3) /* 0=none, 1=bootable, 2=all */
816 u_char host_id;
817 u_char byte15; /* 0x04 */
818 u_short word16; /* 0x0410 */
819 u_short word18; /* 0x0000 */
821 /* Boot order 14 bytes * 4 */
822 struct Symbios_host{
823 u_char word0; /* 0x0004:ok / 0x0000:nok */
824 u_short device_id; /* PCI device id */
825 u_short vendor_id; /* PCI vendor id */
826 u_char byte6; /* 0x00 */
827 u_char device_fn; /* PCI device/function number << 3*/
828 u_short word8;
829 u_short flags;
830 #define SYMBIOS_INIT_SCAN_AT_BOOT (1)
831 u_short io_port; /* PCI io_port address */
832 } host[4];
834 /* Targets 8 bytes * 16 */
835 struct Symbios_target {
836 u_short flags;
837 #define SYMBIOS_DISCONNECT_ENABLE (1)
838 #define SYMBIOS_SCAN_AT_BOOT_TIME (1<<1)
839 #define SYMBIOS_SCAN_LUNS (1<<2)
840 #define SYMBIOS_QUEUE_TAGS_ENABLED (1<<3)
841 u_char bus_width; /* 0x08/0x10 */
842 u_char sync_offset;
843 u_char sync_period; /* 4*period factor */
844 u_char byte6; /* 0x00 */
845 u_short timeout;
846 } target[16];
847 u_char spare_devices[19*8];
848 u_char trailer[6]; /* 0xfe 0xfe 0x00 0x00 0x00 0x00 */
850 typedef struct Symbios_nvram Symbios_nvram;
851 typedef struct Symbios_host Symbios_host;
852 typedef struct Symbios_target Symbios_target;
855 ** Tekram NvRAM data format.
857 #define TEKRAM_NVRAM_SIZE 64
858 #define TEKRAM_NVRAM_ADDRESS 0
860 struct Tekram_nvram {
861 struct Tekram_target {
862 u_char flags;
863 #define TEKRAM_PARITY_CHECK (1)
864 #define TEKRAM_SYNC_NEGO (1<<1)
865 #define TEKRAM_DISCONNECT_ENABLE (1<<2)
866 #define TEKRAM_START_CMD (1<<3)
867 #define TEKRAM_TAGGED_COMMANDS (1<<4)
868 #define TEKRAM_WIDE_NEGO (1<<5)
869 u_char sync_index;
870 u_short word2;
871 } target[16];
872 u_char host_id;
873 u_char flags;
874 #define TEKRAM_MORE_THAN_2_DRIVES (1)
875 #define TEKRAM_DRIVES_SUP_1GB (1<<1)
876 #define TEKRAM_RESET_ON_POWER_ON (1<<2)
877 #define TEKRAM_ACTIVE_NEGATION (1<<3)
878 #define TEKRAM_IMMEDIATE_SEEK (1<<4)
879 #define TEKRAM_SCAN_LUNS (1<<5)
880 #define TEKRAM_REMOVABLE_FLAGS (3<<6) /* 0: disable; 1: boot device; 2:all */
881 u_char boot_delay_index;
882 u_char max_tags_index;
883 u_short flags1;
884 #define TEKRAM_F2_F6_ENABLED (1)
885 u_short spare[29];
887 typedef struct Tekram_nvram Tekram_nvram;
888 typedef struct Tekram_target Tekram_target;
890 static u_char Tekram_sync[12] __initdata = {25,31,37,43,50,62,75,125,12,15,18,21};
892 #endif /* SCSI_NCR_NVRAM_SUPPORT */
895 ** Structures used by ncr53c8xx_detect/ncr53c8xx_pci_init to
896 ** transmit device configuration to the ncr_attach() function.
898 typedef struct {
899 int bus;
900 u_char device_fn;
901 u_long base;
902 u_long base_2;
903 u_long io_port;
904 int irq;
905 /* port and reg fields to use INB, OUTB macros */
906 u_long port;
907 volatile struct ncr_reg *reg;
908 } ncr_slot;
910 typedef struct {
911 int type;
912 #define SCSI_NCR_SYMBIOS_NVRAM (1)
913 #define SCSI_NCR_TEKRAM_NVRAM (2)
914 #ifdef SCSI_NCR_NVRAM_SUPPORT
915 union {
916 Symbios_nvram Symbios;
917 Tekram_nvram Tekram;
918 } data;
919 #endif
920 } ncr_nvram;
923 ** Structure used by ncr53c8xx_detect/ncr53c8xx_pci_init
924 ** to save data on each detected board for ncr_attach().
926 typedef struct {
927 ncr_slot slot;
928 ncr_chip chip;
929 ncr_nvram *nvram;
930 int attach_done;
931 } ncr_device;
933 /*==========================================================
935 ** Debugging tags
937 **==========================================================
940 #define DEBUG_ALLOC (0x0001)
941 #define DEBUG_PHASE (0x0002)
942 #define DEBUG_POLL (0x0004)
943 #define DEBUG_QUEUE (0x0008)
944 #define DEBUG_RESULT (0x0010)
945 #define DEBUG_SCATTER (0x0020)
946 #define DEBUG_SCRIPT (0x0040)
947 #define DEBUG_TINY (0x0080)
948 #define DEBUG_TIMING (0x0100)
949 #define DEBUG_NEGO (0x0200)
950 #define DEBUG_TAGS (0x0400)
951 #define DEBUG_FREEZE (0x0800)
952 #define DEBUG_RESTART (0x1000)
955 ** Enable/Disable debug messages.
956 ** Can be changed at runtime too.
959 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
960 #define DEBUG_FLAGS ncr_debug
961 #else
962 #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
963 #endif
967 /*==========================================================
969 ** assert ()
971 **==========================================================
973 ** modified copy from 386bsd:/usr/include/sys/assert.h
975 **----------------------------------------------------------
978 #define assert(expression) { \
979 if (!(expression)) { \
980 (void)printk(KERN_ERR \
981 "assertion \"%s\" failed: file \"%s\", line %d\n", \
982 #expression, \
983 __FILE__, __LINE__); \
987 /*==========================================================
989 ** Big/Little endian support.
991 **==========================================================
995 ** If the NCR uses big endian addressing mode over the
996 ** PCI, actual io register addresses for byte and word
997 ** accesses must be changed according to lane routing.
998 ** Btw, ncr_offb() and ncr_offw() macros only apply to
999 ** constants and so donnot generate bloated code.
1002 #if defined(SCSI_NCR_BIG_ENDIAN)
1004 #define ncr_offb(o) (((o)&~3)+((~((o)&3))&3))
1005 #define ncr_offw(o) (((o)&~3)+((~((o)&3))&2))
1007 #else
1009 #define ncr_offb(o) (o)
1010 #define ncr_offw(o) (o)
1012 #endif
1015 ** If the CPU and the NCR use same endian-ness addressing,
1016 ** no byte reordering is needed for script patching.
1017 ** Macro cpu_to_scr() is to be used for script patching.
1018 ** Macro scr_to_cpu() is to be used for getting a DWORD
1019 ** from the script.
1022 #if defined(__BIG_ENDIAN) && !defined(SCSI_NCR_BIG_ENDIAN)
1024 #define cpu_to_scr(dw) cpu_to_le32(dw)
1025 #define scr_to_cpu(dw) le32_to_cpu(dw)
1027 #elif defined(__LITTLE_ENDIAN) && defined(SCSI_NCR_BIG_ENDIAN)
1029 #define cpu_to_scr(dw) cpu_to_be32(dw)
1030 #define scr_to_cpu(dw) be32_to_cpu(dw)
1032 #else
1034 #define cpu_to_scr(dw) (dw)
1035 #define scr_to_cpu(dw) (dw)
1037 #endif
1039 /*==========================================================
1041 ** Access to the controller chip.
1043 ** If NCR_IOMAPPED is defined, the driver will use
1044 ** normal IOs instead of the MEMORY MAPPED IO method
1045 ** recommended by PCI specifications.
1046 ** If all PCI bridges, host brigdes and architectures
1047 ** would have been correctly designed for PCI, this
1048 ** option would be useless.
1050 **==========================================================
1054 ** If the CPU and the NCR use same endian-ness addressing,
1055 ** no byte reordering is needed for accessing chip io
1056 ** registers. Functions suffixed by '_raw' are assumed
1057 ** to access the chip over the PCI without doing byte
1058 ** reordering. Functions suffixed by '_l2b' are
1059 ** assumed to perform little-endian to big-endian byte
1060 ** reordering, those suffixed by '_b2l' blah, blah,
1061 ** blah, ...
1064 #if defined(NCR_IOMAPPED)
1067 ** IO mapped only input / ouput
1070 #define INB_OFF(o) inb (np->port + ncr_offb(o))
1071 #define OUTB_OFF(o, val) outb ((val), np->port + ncr_offb(o))
1073 #if defined(__BIG_ENDIAN) && !defined(SCSI_NCR_BIG_ENDIAN)
1075 #define INW_OFF(o) inw_l2b (np->port + ncr_offw(o))
1076 #define INL_OFF(o) inl_l2b (np->port + (o))
1078 #define OUTW_OFF(o, val) outw_b2l ((val), np->port + ncr_offw(o))
1079 #define OUTL_OFF(o, val) outl_b2l ((val), np->port + (o))
1081 #elif defined(__LITTLE_ENDIAN) && defined(SCSI_NCR_BIG_ENDIAN)
1083 #define INW_OFF(o) inw_b2l (np->port + ncr_offw(o))
1084 #define INL_OFF(o) inl_b2l (np->port + (o))
1086 #define OUTW_OFF(o, val) outw_l2b ((val), np->port + ncr_offw(o))
1087 #define OUTL_OFF(o, val) outl_l2b ((val), np->port + (o))
1089 #else
1091 #define INW_OFF(o) inw_raw (np->port + ncr_offw(o))
1092 #define INL_OFF(o) inl_raw (np->port + (o))
1094 #define OUTW_OFF(o, val) outw_raw ((val), np->port + ncr_offw(o))
1095 #define OUTL_OFF(o, val) outl_raw ((val), np->port + (o))
1097 #endif /* ENDIANs */
1099 #else /* defined NCR_IOMAPPED */
1102 ** MEMORY mapped IO input / output
1105 #define INB_OFF(o) readb((char *)np->reg + ncr_offb(o))
1106 #define OUTB_OFF(o, val) writeb((val), (char *)np->reg + ncr_offb(o))
1108 #if defined(__BIG_ENDIAN) && !defined(SCSI_NCR_BIG_ENDIAN)
1110 #define INW_OFF(o) readw_l2b((char *)np->reg + ncr_offw(o))
1111 #define INL_OFF(o) readl_l2b((char *)np->reg + (o))
1113 #define OUTW_OFF(o, val) writew_b2l((val), (char *)np->reg + ncr_offw(o))
1114 #define OUTL_OFF(o, val) writel_b2l((val), (char *)np->reg + (o))
1116 #elif defined(__LITTLE_ENDIAN) && defined(SCSI_NCR_BIG_ENDIAN)
1118 #define INW_OFF(o) readw_b2l((char *)np->reg + ncr_offw(o))
1119 #define INL_OFF(o) readl_b2l((char *)np->reg + (o))
1121 #define OUTW_OFF(o, val) writew_l2b((val), (char *)np->reg + ncr_offw(o))
1122 #define OUTL_OFF(o, val) writel_l2b((val), (char *)np->reg + (o))
1124 #else
1126 #define INW_OFF(o) readw_raw((char *)np->reg + ncr_offw(o))
1127 #define INL_OFF(o) readl_raw((char *)np->reg + (o))
1129 #define OUTW_OFF(o, val) writew_raw((val), (char *)np->reg + ncr_offw(o))
1130 #define OUTL_OFF(o, val) writel_raw((val), (char *)np->reg + (o))
1132 #endif
1134 #endif /* defined NCR_IOMAPPED */
1136 #define INB(r) INB_OFF (offsetof(struct ncr_reg,r))
1137 #define INW(r) INW_OFF (offsetof(struct ncr_reg,r))
1138 #define INL(r) INL_OFF (offsetof(struct ncr_reg,r))
1140 #define OUTB(r, val) OUTB_OFF (offsetof(struct ncr_reg,r), (val))
1141 #define OUTW(r, val) OUTW_OFF (offsetof(struct ncr_reg,r), (val))
1142 #define OUTL(r, val) OUTL_OFF (offsetof(struct ncr_reg,r), (val))
1145 ** Set bit field ON, OFF
1148 #define OUTONB(r, m) OUTB(r, INB(r) | (m))
1149 #define OUTOFFB(r, m) OUTB(r, INB(r) & ~(m))
1150 #define OUTONW(r, m) OUTW(r, INW(r) | (m))
1151 #define OUTOFFW(r, m) OUTW(r, INW(r) & ~(m))
1152 #define OUTONL(r, m) OUTL(r, INL(r) | (m))
1153 #define OUTOFFL(r, m) OUTL(r, INL(r) & ~(m))
1156 /*==========================================================
1158 ** Command control block states.
1160 **==========================================================
1163 #define HS_IDLE (0)
1164 #define HS_BUSY (1)
1165 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
1166 #define HS_DISCONNECT (3) /* Disconnected by target */
1168 #define HS_DONEMASK (0x80)
1169 #define HS_COMPLETE (4|HS_DONEMASK)
1170 #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
1171 #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
1172 #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
1173 #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
1174 #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
1175 #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
1178 ** Invalid host status values used by the SCRIPTS processor
1179 ** when the nexus is not fully identified.
1180 ** Shall never appear in a CCB.
1183 #define HS_INVALMASK (0x40)
1184 #define HS_SELECTING (0|HS_INVALMASK)
1185 #define HS_IN_RESELECT (1|HS_INVALMASK)
1186 #define HS_STARTING (2|HS_INVALMASK)
1189 ** Flags set by the SCRIPT processor for commands
1190 ** that have been skipped.
1192 #define HS_SKIPMASK (0x20)
1194 /*==========================================================
1196 ** Software Interrupt Codes
1198 **==========================================================
1201 #define SIR_BAD_STATUS (1)
1202 #define SIR_XXXXXXXXXX (2)
1203 #define SIR_NEGO_SYNC (3)
1204 #define SIR_NEGO_WIDE (4)
1205 #define SIR_NEGO_FAILED (5)
1206 #define SIR_NEGO_PROTO (6)
1207 #define SIR_REJECT_RECEIVED (7)
1208 #define SIR_REJECT_SENT (8)
1209 #define SIR_IGN_RESIDUE (9)
1210 #define SIR_MISSING_SAVE (10)
1211 #define SIR_RESEL_NO_MSG_IN (11)
1212 #define SIR_RESEL_NO_IDENTIFY (12)
1213 #define SIR_RESEL_BAD_LUN (13)
1214 #define SIR_RESEL_BAD_TARGET (14)
1215 #define SIR_RESEL_BAD_I_T_L (15)
1216 #define SIR_RESEL_BAD_I_T_L_Q (16)
1217 #define SIR_DONE_OVERFLOW (17)
1218 #define SIR_MAX (17)
1220 /*==========================================================
1222 ** Extended error codes.
1223 ** xerr_status field of struct ccb.
1225 **==========================================================
1228 #define XE_OK (0)
1229 #define XE_EXTRA_DATA (1) /* unexpected data phase */
1230 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
1232 /*==========================================================
1234 ** Negotiation status.
1235 ** nego_status field of struct ccb.
1237 **==========================================================
1240 #define NS_SYNC (1)
1241 #define NS_WIDE (2)
1243 /*==========================================================
1245 ** "Special features" of targets.
1246 ** quirks field of struct tcb.
1247 ** actualquirks field of struct ccb.
1249 **==========================================================
1252 #define QUIRK_AUTOSAVE (0x01)
1253 #define QUIRK_NOMSG (0x02)
1254 #define QUIRK_NOSYNC (0x10)
1255 #define QUIRK_NOWIDE16 (0x20)
1257 /*==========================================================
1259 ** Capability bits in Inquire response byte 7.
1261 **==========================================================
1264 #define INQ7_QUEUE (0x02)
1265 #define INQ7_SYNC (0x10)
1266 #define INQ7_WIDE16 (0x20)
1268 /*==========================================================
1270 ** Misc.
1272 **==========================================================
1275 #define CCB_MAGIC (0xf2691ad2)
1277 /*==========================================================
1279 ** Declaration of structs.
1281 **==========================================================
1284 struct tcb;
1285 struct lcb;
1286 struct ccb;
1287 struct ncb;
1288 struct script;
1290 typedef struct ncb * ncb_p;
1291 typedef struct tcb * tcb_p;
1292 typedef struct lcb * lcb_p;
1293 typedef struct ccb * ccb_p;
1295 struct link {
1296 ncrcmd l_cmd;
1297 ncrcmd l_paddr;
1300 struct usrcmd {
1301 u_long target;
1302 u_long lun;
1303 u_long data;
1304 u_long cmd;
1307 #define UC_SETSYNC 10
1308 #define UC_SETTAGS 11
1309 #define UC_SETDEBUG 12
1310 #define UC_SETORDER 13
1311 #define UC_SETWIDE 14
1312 #define UC_SETFLAG 15
1313 #define UC_CLEARPROF 16
1314 #define UC_SETVERBOSE 17
1316 #define UF_TRACE (0x01)
1317 #define UF_NODISC (0x02)
1318 #define UF_NOSCAN (0x04)
1320 /*---------------------------------------
1322 ** Timestamps for profiling
1324 **---------------------------------------
1327 #ifdef SCSI_NCR_PROFILE_SUPPORT
1329 struct tstamp {
1330 u_long start;
1331 u_long end;
1332 u_long command;
1333 u_long status;
1334 u_long disconnect;
1335 u_long reselect;
1339 ** profiling data (per device)
1342 struct profile {
1343 u_long num_trans;
1344 u_long num_kbytes;
1345 u_long rest_bytes;
1346 u_long num_disc;
1347 u_long num_break;
1348 u_long num_int;
1349 u_long num_fly;
1350 u_long ms_setup;
1351 u_long ms_data;
1352 u_long ms_disc;
1353 u_long ms_post;
1355 #endif
1357 /*========================================================================
1359 ** Declaration of structs: target control block
1361 **========================================================================
1363 struct tcb {
1364 /*----------------------------------------------------------------
1365 ** During reselection the ncr jumps to this point with SFBR
1366 ** set to the encoded target number with bit 7 set.
1367 ** if it's not this target, jump to the next.
1369 ** JUMP IF (SFBR != #target#), @(next tcb)
1370 **----------------------------------------------------------------
1372 struct link jump_tcb;
1374 /*----------------------------------------------------------------
1375 ** Load the actual values for the sxfer and the scntl3
1376 ** register (sync/wide mode).
1378 ** SCR_COPY (1), @(sval field of this tcb), @(sxfer register)
1379 ** SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
1380 **----------------------------------------------------------------
1382 ncrcmd getscr[6];
1384 /*----------------------------------------------------------------
1385 ** Get the IDENTIFY message and load the LUN to SFBR.
1387 ** CALL, <RESEL_LUN>
1388 **----------------------------------------------------------------
1390 struct link call_lun;
1392 /*----------------------------------------------------------------
1393 ** Now look for the right lun.
1395 ** For i = 0 to 3
1396 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
1398 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1399 ** It is kind of hashcoding.
1400 **----------------------------------------------------------------
1402 struct link jump_lcb[4]; /* JUMPs for reselection */
1403 lcb_p lp[MAX_LUN]; /* The lcb's of this tcb */
1404 u_char inq_done; /* Target capabilities received */
1405 u_char inq_byte7; /* Contains these capabilities */
1407 /*----------------------------------------------------------------
1408 ** Pointer to the ccb used for negotiation.
1409 ** Prevent from starting a negotiation for all queued commands
1410 ** when tagged command queuing is enabled.
1411 **----------------------------------------------------------------
1413 ccb_p nego_cp;
1415 /*----------------------------------------------------------------
1416 ** statistical data
1417 **----------------------------------------------------------------
1419 u_long transfers;
1420 u_long bytes;
1422 /*----------------------------------------------------------------
1423 ** negotiation of wide and synch transfer and device quirks.
1424 **----------------------------------------------------------------
1426 /*0*/ u_char minsync;
1427 /*1*/ u_char sval;
1428 /*2*/ u_short period;
1429 /*0*/ u_char maxoffs;
1430 /*1*/ u_char quirks;
1431 /*2*/ u_char widedone;
1432 /*3*/ u_char wval;
1434 /*----------------------------------------------------------------
1435 ** User settable limits and options.
1436 ** These limits are read from the NVRAM if present.
1437 **----------------------------------------------------------------
1439 u_char usrsync;
1440 u_char usrwide;
1441 u_char usrtags;
1442 u_char usrflag;
1445 /*========================================================================
1447 ** Declaration of structs: lun control block
1449 **========================================================================
1451 struct lcb {
1452 /*----------------------------------------------------------------
1453 ** During reselection the ncr jumps to this point
1454 ** with SFBR set to the "Identify" message.
1455 ** if it's not this lun, jump to the next.
1457 ** JUMP IF (SFBR != #lun#), @(next lcb of this target)
1459 ** It is this lun. Load TEMP with the nexus jumps table
1460 ** address and jump to RESEL_TAG (or RESEL_NOTAG).
1462 ** SCR_COPY (4), p_jump_ccb, TEMP,
1463 ** SCR_JUMP, <RESEL_TAG>
1464 **----------------------------------------------------------------
1466 struct link jump_lcb;
1467 ncrcmd load_jump_ccb[3];
1468 struct link jump_tag;
1469 ncrcmd p_jump_ccb; /* Jump table bus address */
1471 /*----------------------------------------------------------------
1472 ** Jump table used by the script processor to directly jump
1473 ** to the CCB corresponding to the reselected nexus.
1474 ** Address is allocated on 256 bytes boundary in order to
1475 ** allow 8 bit calculation of the tag jump entry for up to
1476 ** 64 possible tags.
1477 **----------------------------------------------------------------
1479 u_int32 jump_ccb_0; /* Default table if no tags */
1480 u_int32 *jump_ccb; /* Virtual address */
1482 /*----------------------------------------------------------------
1483 ** CCB queue management.
1484 **----------------------------------------------------------------
1486 XPT_QUEHEAD free_ccbq; /* Queue of available CCBs */
1487 XPT_QUEHEAD busy_ccbq; /* Queue of busy CCBs */
1488 XPT_QUEHEAD wait_ccbq; /* Queue of waiting for IO CCBs */
1489 XPT_QUEHEAD skip_ccbq; /* Queue of skipped CCBs */
1490 u_char actccbs; /* Number of allocated CCBs */
1491 u_char busyccbs; /* CCBs busy for this lun */
1492 u_char queuedccbs; /* CCBs queued to the controller*/
1493 u_char queuedepth; /* Queue depth for this lun */
1494 u_char scdev_depth; /* SCSI device queue depth */
1495 u_char maxnxs; /* Max possible nexuses */
1497 /*----------------------------------------------------------------
1498 ** Control of tagged command queuing.
1499 ** Tags allocation is performed using a circular buffer.
1500 ** This avoids using a loop for tag allocation.
1501 **----------------------------------------------------------------
1503 u_char ia_tag; /* Allocation index */
1504 u_char if_tag; /* Freeing index */
1505 u_char cb_tags[SCSI_NCR_MAX_TAGS]; /* Circular tags buffer */
1506 u_char usetags; /* Command queuing is active */
1507 u_char maxtags; /* Max nr of tags asked by user */
1508 u_char numtags; /* Current number of tags */
1509 u_char inq_byte7; /* Store unit CmdQ capabitility */
1511 /*----------------------------------------------------------------
1512 ** QUEUE FULL control and ORDERED tag control.
1513 **----------------------------------------------------------------
1515 /*----------------------------------------------------------------
1516 ** QUEUE FULL and ORDERED tag control.
1517 **----------------------------------------------------------------
1519 u_short num_good; /* Nr of GOOD since QUEUE FULL */
1520 tagmap_t tags_umap; /* Used tags bitmap */
1521 tagmap_t tags_smap; /* Tags in use at 'tag_stime' */
1522 u_long tags_stime; /* Last time we set smap=umap */
1523 ccb_p held_ccb; /* CCB held for QUEUE FULL */
1526 /*========================================================================
1528 ** Declaration of structs: the launch script.
1530 **========================================================================
1532 ** It is part of the CCB and is called by the scripts processor to
1533 ** start or restart the data structure (nexus).
1534 ** This 6 DWORDs mini script makes use of prefetching.
1536 **------------------------------------------------------------------------
1538 struct launch {
1539 /*----------------------------------------------------------------
1540 ** SCR_COPY(4), @(p_phys), @(dsa register)
1541 ** SCR_JUMP, @(scheduler_point)
1542 **----------------------------------------------------------------
1544 ncrcmd setup_dsa[3]; /* Copy 'phys' address to dsa */
1545 struct link schedule; /* Jump to scheduler point */
1546 ncrcmd p_phys; /* 'phys' header bus address */
1549 /*========================================================================
1551 ** Declaration of structs: global HEADER.
1553 **========================================================================
1555 ** This substructure is copied from the ccb to a global address after
1556 ** selection (or reselection) and copied back before disconnect.
1558 ** These fields are accessible to the script processor.
1560 **------------------------------------------------------------------------
1563 struct head {
1564 /*----------------------------------------------------------------
1565 ** Saved data pointer.
1566 ** Points to the position in the script responsible for the
1567 ** actual transfer transfer of data.
1568 ** It's written after reception of a SAVE_DATA_POINTER message.
1569 ** The goalpointer points after the last transfer command.
1570 **----------------------------------------------------------------
1572 u_int32 savep;
1573 u_int32 lastp;
1574 u_int32 goalp;
1576 /*----------------------------------------------------------------
1577 ** Alternate data pointer.
1578 ** They are copied back to savep/lastp/goalp by the SCRIPTS
1579 ** when the direction is unknown and the device claims data out.
1580 **----------------------------------------------------------------
1582 u_int32 wlastp;
1583 u_int32 wgoalp;
1585 /*----------------------------------------------------------------
1586 ** The virtual address of the ccb containing this header.
1587 **----------------------------------------------------------------
1589 ccb_p cp;
1591 #ifdef SCSI_NCR_PROFILE_SUPPORT
1592 /*----------------------------------------------------------------
1593 ** Space for some timestamps to gather profiling data.
1594 **----------------------------------------------------------------
1596 struct tstamp stamp;
1597 #endif
1599 /*----------------------------------------------------------------
1600 ** Status fields.
1601 **----------------------------------------------------------------
1603 u_char scr_st[4]; /* script status */
1604 u_char status[4]; /* host status. must be the */
1605 /* last DWORD of the header. */
1609 ** The status bytes are used by the host and the script processor.
1611 ** The byte corresponding to the host_status must be stored in the
1612 ** last DWORD of the CCB header since it is used for command
1613 ** completion (ncr_wakeup()). Doing so, we are sure that the header
1614 ** has been entirely copied back to the CCB when the host_status is
1615 ** seen complete by the CPU.
1617 ** The last four bytes (status[4]) are copied to the scratchb register
1618 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
1619 ** and copied back just after disconnecting.
1620 ** Inside the script the XX_REG are used.
1622 ** The first four bytes (scr_st[4]) are used inside the script by
1623 ** "COPY" commands.
1624 ** Because source and destination must have the same alignment
1625 ** in a DWORD, the fields HAVE to be at the choosen offsets.
1626 ** xerr_st 0 (0x34) scratcha
1627 ** sync_st 1 (0x05) sxfer
1628 ** wide_st 3 (0x03) scntl3
1632 ** Last four bytes (script)
1634 #define QU_REG scr0
1635 #define HS_REG scr1
1636 #define HS_PRT nc_scr1
1637 #define SS_REG scr2
1638 #define SS_PRT nc_scr2
1639 #define PS_REG scr3
1642 ** Last four bytes (host)
1644 #define actualquirks phys.header.status[0]
1645 #define host_status phys.header.status[1]
1646 #define scsi_status phys.header.status[2]
1647 #define parity_status phys.header.status[3]
1650 ** First four bytes (script)
1652 #define xerr_st header.scr_st[0]
1653 #define sync_st header.scr_st[1]
1654 #define nego_st header.scr_st[2]
1655 #define wide_st header.scr_st[3]
1658 ** First four bytes (host)
1660 #define xerr_status phys.xerr_st
1661 #define nego_status phys.nego_st
1663 #if 0
1664 #define sync_status phys.sync_st
1665 #define wide_status phys.wide_st
1666 #endif
1668 /*==========================================================
1670 ** Declaration of structs: Data structure block
1672 **==========================================================
1674 ** During execution of a ccb by the script processor,
1675 ** the DSA (data structure address) register points
1676 ** to this substructure of the ccb.
1677 ** This substructure contains the header with
1678 ** the script-processor-changable data and
1679 ** data blocks for the indirect move commands.
1681 **----------------------------------------------------------
1684 struct dsb {
1687 ** Header.
1690 struct head header;
1693 ** Table data for Script
1696 struct scr_tblsel select;
1697 struct scr_tblmove smsg ;
1698 struct scr_tblmove cmd ;
1699 struct scr_tblmove sense ;
1700 struct scr_tblmove data [MAX_SCATTER];
1704 /*========================================================================
1706 ** Declaration of structs: Command control block.
1708 **========================================================================
1710 struct ccb {
1711 /*----------------------------------------------------------------
1712 ** This is the data structure which is pointed by the DSA
1713 ** register when it is executed by the script processor.
1714 ** It must be the first entry because it contains the header
1715 ** as first entry that must be cache line aligned.
1716 **----------------------------------------------------------------
1718 struct dsb phys;
1720 /*----------------------------------------------------------------
1721 ** Mini-script used at CCB execution start-up.
1722 ** Load the DSA with the data structure address (phys) and
1723 ** jump to SELECT. Jump to CANCEL if CCB is to be canceled.
1724 **----------------------------------------------------------------
1726 struct launch start;
1728 /*----------------------------------------------------------------
1729 ** Mini-script used at CCB relection to restart the nexus.
1730 ** Load the DSA with the data structure address (phys) and
1731 ** jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
1732 **----------------------------------------------------------------
1734 struct launch restart;
1736 /*----------------------------------------------------------------
1737 ** If a data transfer phase is terminated too early
1738 ** (after reception of a message (i.e. DISCONNECT)),
1739 ** we have to prepare a mini script to transfer
1740 ** the rest of the data.
1741 **----------------------------------------------------------------
1743 ncrcmd patch[8];
1745 /*----------------------------------------------------------------
1746 ** The general SCSI driver provides a
1747 ** pointer to a control block.
1748 **----------------------------------------------------------------
1750 Scsi_Cmnd *cmd; /* SCSI command */
1751 u_long tlimit; /* Deadline for this job */
1752 int data_len; /* Total data length */
1754 /*----------------------------------------------------------------
1755 ** Message areas.
1756 ** We prepare a message to be sent after selection.
1757 ** We may use a second one if the command is rescheduled
1758 ** due to GETCC or QFULL.
1759 ** Contents are IDENTIFY and SIMPLE_TAG.
1760 ** While negotiating sync or wide transfer,
1761 ** a SDTR or WDTR message is appended.
1762 **----------------------------------------------------------------
1764 u_char scsi_smsg [8];
1765 u_char scsi_smsg2[8];
1767 /*----------------------------------------------------------------
1768 ** Other fields.
1769 **----------------------------------------------------------------
1771 u_long p_ccb; /* BUS address of this CCB */
1772 u_char sensecmd[6]; /* Sense command */
1773 u_char tag; /* Tag for this transfer */
1774 /* 255 means no tag */
1775 u_char target;
1776 u_char lun;
1777 u_char queued;
1778 u_char auto_sense;
1779 ccb_p link_ccb; /* Host adapter CCB chain */
1780 XPT_QUEHEAD link_ccbq; /* Link to unit CCB queue */
1781 u_int32 startp; /* Initial data pointer */
1782 u_long magic; /* Free / busy CCB flag */
1785 #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
1788 /*========================================================================
1790 ** Declaration of structs: NCR device descriptor
1792 **========================================================================
1794 struct ncb {
1795 /*----------------------------------------------------------------
1796 ** The global header.
1797 ** It is accessible to both the host and the script processor.
1798 ** Must be cache line size aligned (32 for x86) in order to
1799 ** allow cache line bursting when it is copied to/from CCB.
1800 **----------------------------------------------------------------
1802 struct head header;
1804 /*----------------------------------------------------------------
1805 ** CCBs management queues.
1806 **----------------------------------------------------------------
1808 Scsi_Cmnd *waiting_list; /* Commands waiting for a CCB */
1809 /* when lcb is not allocated. */
1810 Scsi_Cmnd *done_list; /* Commands waiting for done() */
1811 /* callback to be invoked. */
1812 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
1813 spinlock_t smp_lock; /* Lock for SMP threading */
1814 #endif
1816 /*----------------------------------------------------------------
1817 ** Chip and controller indentification.
1818 **----------------------------------------------------------------
1820 int unit; /* Unit number */
1821 char chip_name[8]; /* Chip name */
1822 char inst_name[16]; /* ncb instance name */
1824 /*----------------------------------------------------------------
1825 ** Initial value of some IO register bits.
1826 ** These values are assumed to have been set by BIOS, and may
1827 ** be used for probing adapter implementation differences.
1828 **----------------------------------------------------------------
1830 u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
1831 sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
1833 /*----------------------------------------------------------------
1834 ** Actual initial value of IO register bits used by the
1835 ** driver. They are loaded at initialisation according to
1836 ** features that are to be enabled.
1837 **----------------------------------------------------------------
1839 u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4,
1840 rv_ctest5, rv_stest2;
1842 /*----------------------------------------------------------------
1843 ** Targets management.
1844 ** During reselection the ncr jumps to jump_tcb.
1845 ** The SFBR register is loaded with the encoded target id.
1846 ** For i = 0 to 3
1847 ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
1849 ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
1850 ** It is kind of hashcoding.
1851 **----------------------------------------------------------------
1853 struct link jump_tcb[4]; /* JUMPs for reselection */
1854 struct tcb target[MAX_TARGET]; /* Target data */
1856 /*----------------------------------------------------------------
1857 ** Virtual and physical bus addresses of the chip.
1858 **----------------------------------------------------------------
1860 vm_offset_t vaddr; /* Virtual and bus address of */
1861 vm_offset_t paddr; /* chip's IO registers. */
1862 vm_offset_t paddr2; /* On-chip RAM bus address. */
1863 volatile /* Pointer to volatile for */
1864 struct ncr_reg *reg; /* memory mapped IO. */
1866 /*----------------------------------------------------------------
1867 ** SCRIPTS virtual and physical bus addresses.
1868 ** 'script' is loaded in the on-chip RAM if present.
1869 ** 'scripth' stays in main memory.
1870 **----------------------------------------------------------------
1872 struct script *script0; /* Copies of script and scripth */
1873 struct scripth *scripth0; /* relocated for this ncb. */
1874 struct scripth *scripth; /* Actual scripth virt. address */
1875 u_long p_script; /* Actual script and scripth */
1876 u_long p_scripth; /* bus addresses. */
1878 /*----------------------------------------------------------------
1879 ** General controller parameters and configuration.
1880 **----------------------------------------------------------------
1882 u_short device_id; /* PCI device id */
1883 u_char revision_id; /* PCI device revision id */
1884 u_long port; /* IO space base address */
1885 u_int irq; /* IRQ level */
1886 u_int features; /* Chip features map */
1887 u_char myaddr; /* SCSI id of the adapter */
1888 u_char maxburst; /* log base 2 of dwords burst */
1889 u_char maxwide; /* Maximum transfer width */
1890 u_char minsync; /* Minimum sync period factor */
1891 u_char maxsync; /* Maximum sync period factor */
1892 u_char maxoffs; /* Max scsi offset */
1893 u_char multiplier; /* Clock multiplier (1,2,4) */
1894 u_char clock_divn; /* Number of clock divisors */
1895 u_long clock_khz; /* SCSI clock frequency in KHz */
1897 /*----------------------------------------------------------------
1898 ** Start queue management.
1899 ** It is filled up by the host processor and accessed by the
1900 ** SCRIPTS processor in order to start SCSI commands.
1901 **----------------------------------------------------------------
1903 u_short squeueput; /* Next free slot of the queue */
1904 u_short actccbs; /* Number of allocated CCBs */
1905 u_short queuedccbs; /* Number of CCBs in start queue*/
1906 u_short queuedepth; /* Start queue depth */
1908 /*----------------------------------------------------------------
1909 ** Timeout handler.
1910 **----------------------------------------------------------------
1912 struct timer_list timer; /* Timer handler link header */
1913 u_long lasttime;
1914 u_long settle_time; /* Resetting the SCSI BUS */
1916 /*----------------------------------------------------------------
1917 ** Debugging and profiling.
1918 **----------------------------------------------------------------
1920 struct ncr_reg regdump; /* Register dump */
1921 u_long regtime; /* Time it has been done */
1922 #ifdef SCSI_NCR_PROFILE_SUPPORT
1923 struct profile profile; /* Profiling data */
1924 u_int disc_phys; /* Disconnection counters */
1925 u_int disc_ref;
1926 #endif
1928 /*----------------------------------------------------------------
1929 ** Miscellaneous buffers accessed by the scripts-processor.
1930 ** They shall be DWORD aligned, because they may be read or
1931 ** written with a SCR_COPY script command.
1932 **----------------------------------------------------------------
1934 u_char msgout[8]; /* Buffer for MESSAGE OUT */
1935 u_char msgin [8]; /* Buffer for MESSAGE IN */
1936 u_int32 lastmsg; /* Last SCSI message sent */
1937 u_char scratch; /* Scratch for SCSI receive */
1939 /*----------------------------------------------------------------
1940 ** Miscellaneous configuration and status parameters.
1941 **----------------------------------------------------------------
1943 u_char disc; /* Diconnection allowed */
1944 u_char scsi_mode; /* Current SCSI BUS mode */
1945 u_char order; /* Tag order to use */
1946 u_char verbose; /* Verbosity for this controller*/
1947 int ncr_cache; /* Used for cache test at init. */
1949 /*----------------------------------------------------------------
1950 ** Command completion handling.
1951 **----------------------------------------------------------------
1953 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1954 struct ccb *(ccb_done[MAX_DONE]);
1955 int ccb_done_ic;
1956 #endif
1957 /*----------------------------------------------------------------
1958 ** Fields that should be removed or changed.
1959 **----------------------------------------------------------------
1961 struct ccb *ccb; /* Global CCB */
1962 struct usrcmd user; /* Command from user */
1963 u_char release_stage; /* Synchronisation stage on release */
1966 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
1967 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1969 /*==========================================================
1972 ** Script for NCR-Processor.
1974 ** Use ncr_script_fill() to create the variable parts.
1975 ** Use ncr_script_copy_and_bind() to make a copy and
1976 ** bind to physical addresses.
1979 **==========================================================
1981 ** We have to know the offsets of all labels before
1982 ** we reach them (for forward jumps).
1983 ** Therefore we declare a struct here.
1984 ** If you make changes inside the script,
1985 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
1987 **----------------------------------------------------------
1991 ** Script fragments which are loaded into the on-chip RAM
1992 ** of 825A, 875 and 895 chips.
1994 struct script {
1995 ncrcmd start [ 5];
1996 ncrcmd startpos [ 1];
1997 ncrcmd select [ 6];
1998 ncrcmd select2 [ 9];
1999 ncrcmd loadpos [ 4];
2000 ncrcmd send_ident [ 9];
2001 ncrcmd prepare [ 6];
2002 ncrcmd prepare2 [ 7];
2003 #ifdef SCSI_NCR_PROFILE_SUPPORT
2004 ncrcmd command [ 9];
2005 #else
2006 ncrcmd command [ 6];
2007 #endif
2008 ncrcmd dispatch [ 32];
2009 ncrcmd clrack [ 4];
2010 ncrcmd no_data [ 17];
2011 #ifdef SCSI_NCR_PROFILE_SUPPORT
2012 ncrcmd status [ 11];
2013 #else
2014 ncrcmd status [ 8];
2015 #endif
2016 ncrcmd msg_in [ 2];
2017 ncrcmd msg_in2 [ 16];
2018 ncrcmd msg_bad [ 4];
2019 ncrcmd setmsg [ 7];
2020 ncrcmd cleanup [ 6];
2021 ncrcmd complete [ 9];
2022 ncrcmd cleanup_ok [ 8];
2023 ncrcmd cleanup0 [ 1];
2024 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
2025 ncrcmd signal [ 12];
2026 #else
2027 ncrcmd signal [ 9];
2028 ncrcmd done_pos [ 1];
2029 ncrcmd done_plug [ 2];
2030 ncrcmd done_end [ 7];
2031 #endif
2032 ncrcmd save_dp [ 7];
2033 ncrcmd restore_dp [ 5];
2034 #ifdef SCSI_NCR_PROFILE_SUPPORT
2035 ncrcmd disconnect [ 28];
2036 #else
2037 ncrcmd disconnect [ 17];
2038 #endif
2039 ncrcmd msg_out [ 9];
2040 ncrcmd msg_out_done [ 7];
2041 ncrcmd idle [ 2];
2042 ncrcmd reselect [ 8];
2043 ncrcmd reselected [ 8];
2044 ncrcmd resel_dsa [ 6];
2045 #ifdef SCSI_NCR_PROFILE_SUPPORT
2046 ncrcmd loadpos1 [ 7];
2047 #else
2048 ncrcmd loadpos1 [ 4];
2049 #endif
2050 ncrcmd resel_lun [ 6];
2051 ncrcmd resel_tag [ 6];
2052 ncrcmd jump_to_nexus [ 4];
2053 ncrcmd nexus_indirect [ 4];
2054 ncrcmd resel_notag [ 4];
2055 ncrcmd data_in [MAX_SCATTERL * 4];
2056 ncrcmd data_in2 [ 4];
2057 ncrcmd data_out [MAX_SCATTERL * 4];
2058 ncrcmd data_out2 [ 4];
2062 ** Script fragments which stay in main memory for all chips.
2064 struct scripth {
2065 ncrcmd tryloop [MAX_START*2];
2066 ncrcmd tryloop2 [ 2];
2067 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2068 ncrcmd done_queue [MAX_DONE*5];
2069 ncrcmd done_queue2 [ 2];
2070 #endif
2071 ncrcmd select_no_atn [ 8];
2072 ncrcmd cancel [ 4];
2073 ncrcmd skip [ 9];
2074 ncrcmd skip2 [ 19];
2075 ncrcmd par_err_data_in [ 6];
2076 ncrcmd par_err_other [ 4];
2077 ncrcmd msg_reject [ 8];
2078 ncrcmd msg_ign_residue [ 24];
2079 ncrcmd msg_extended [ 10];
2080 ncrcmd msg_ext_2 [ 10];
2081 ncrcmd msg_wdtr [ 14];
2082 ncrcmd send_wdtr [ 7];
2083 ncrcmd msg_ext_3 [ 10];
2084 ncrcmd msg_sdtr [ 14];
2085 ncrcmd send_sdtr [ 7];
2086 ncrcmd nego_bad_phase [ 4];
2087 ncrcmd msg_out_abort [ 10];
2088 ncrcmd hdata_in [MAX_SCATTERH * 4];
2089 ncrcmd hdata_in2 [ 2];
2090 ncrcmd hdata_out [MAX_SCATTERH * 4];
2091 ncrcmd hdata_out2 [ 2];
2092 ncrcmd reset [ 4];
2093 ncrcmd aborttag [ 4];
2094 ncrcmd abort [ 2];
2095 ncrcmd abort_resel [ 20];
2096 ncrcmd resend_ident [ 4];
2097 ncrcmd clratn_go_on [ 3];
2098 ncrcmd nxtdsp_go_on [ 1];
2099 ncrcmd sdata_in [ 8];
2100 ncrcmd data_io [ 18];
2101 ncrcmd bad_identify [ 12];
2102 ncrcmd bad_i_t_l [ 4];
2103 ncrcmd bad_i_t_l_q [ 4];
2104 ncrcmd bad_target [ 8];
2105 ncrcmd bad_status [ 8];
2106 ncrcmd start_ram [ 4];
2107 ncrcmd start_ram0 [ 4];
2108 ncrcmd sto_restart [ 5];
2109 ncrcmd snooptest [ 9];
2110 ncrcmd snoopend [ 2];
2113 /*==========================================================
2116 ** Function headers.
2119 **==========================================================
2122 static void ncr_alloc_ccb (ncb_p np, u_char tn, u_char ln);
2123 static void ncr_complete (ncb_p np, ccb_p cp);
2124 static void ncr_exception (ncb_p np);
2125 static void ncr_free_ccb (ncb_p np, ccb_p cp);
2126 static void ncr_init_ccb (ncb_p np, ccb_p cp);
2127 static void ncr_init_tcb (ncb_p np, u_char tn);
2128 static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln);
2129 static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln,
2130 u_char *inq_data);
2131 static void ncr_getclock (ncb_p np, int mult);
2132 static void ncr_selectclock (ncb_p np, u_char scntl3);
2133 static ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln);
2134 static void ncr_init (ncb_p np, int reset, char * msg, u_long code);
2135 static int ncr_int_sbmc (ncb_p np);
2136 static int ncr_int_par (ncb_p np);
2137 static void ncr_int_ma (ncb_p np);
2138 static void ncr_int_sir (ncb_p np);
2139 static void ncr_int_sto (ncb_p np);
2140 static u_long ncr_lookup (char* id);
2141 static void ncr_negotiate (struct ncb* np, struct tcb* tp);
2143 #ifdef SCSI_NCR_PROFILE_SUPPORT
2144 static void ncb_profile (ncb_p np, ccb_p cp);
2145 #endif
2147 static void ncr_script_copy_and_bind
2148 (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
2149 static void ncr_script_fill (struct script * scr, struct scripth * scripth);
2150 static int ncr_scatter (ccb_p cp, Scsi_Cmnd *cmd);
2151 static void ncr_getsync (ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p);
2152 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer);
2153 static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln);
2154 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack);
2155 static int ncr_show_msg (u_char * msg);
2156 static int ncr_snooptest (ncb_p np);
2157 static void ncr_timeout (ncb_p np);
2158 static void ncr_wakeup (ncb_p np, u_long code);
2159 static void ncr_wakeup_done (ncb_p np);
2160 static void ncr_start_next_ccb (ncb_p np, lcb_p lp, int maxn);
2161 static void ncr_put_start_queue(ncb_p np, ccb_p cp);
2162 static void ncr_start_reset (ncb_p np);
2163 static int ncr_reset_scsi_bus (ncb_p np, int enab_int, int settle_delay);
2165 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
2166 static void ncr_usercmd (ncb_p np);
2167 #endif
2169 static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device);
2171 static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd);
2172 static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd);
2173 static void process_waiting_list(ncb_p np, int sts);
2175 #define remove_from_waiting_list(np, cmd) \
2176 retrieve_from_waiting_list(1, (np), (cmd))
2177 #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
2178 #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
2180 #ifdef SCSI_NCR_NVRAM_SUPPORT
2181 static int ncr_get_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram);
2182 static int ncr_get_Tekram_nvram (ncr_slot *np, Tekram_nvram *nvram);
2183 #endif
2185 /*==========================================================
2188 ** Global static data.
2191 **==========================================================
2194 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
2195 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
2196 #endif
2198 static inline char *ncr_name (ncb_p np)
2200 return np->inst_name;
2204 /*==========================================================
2207 ** Scripts for NCR-Processor.
2209 ** Use ncr_script_bind for binding to physical addresses.
2212 **==========================================================
2214 ** NADDR generates a reference to a field of the controller data.
2215 ** PADDR generates a reference to another part of the script.
2216 ** RADDR generates a reference to a script processor register.
2217 ** FADDR generates a reference to a script processor register
2218 ** with offset.
2220 **----------------------------------------------------------
2223 #define RELOC_SOFTC 0x40000000
2224 #define RELOC_LABEL 0x50000000
2225 #define RELOC_REGISTER 0x60000000
2226 #define RELOC_KVAR 0x70000000
2227 #define RELOC_LABELH 0x80000000
2228 #define RELOC_MASK 0xf0000000
2230 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
2231 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
2232 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
2233 #define RADDR(label) (RELOC_REGISTER | REG(label))
2234 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
2235 #define KVAR(which) (RELOC_KVAR | (which))
2237 #define SCRIPT_KVAR_JIFFIES (0)
2239 #define SCRIPT_KVAR_FIRST SCRIPT_KVAR_JIFFIES
2240 #define SCRIPT_KVAR_LAST SCRIPT_KVAR_JIFFIES
2243 * Kernel variables referenced in the scripts.
2244 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
2246 static void *script_kvars[] __initdata =
2247 { (void *)&jiffies };
2249 static struct script script0 __initdata = {
2250 /*--------------------------< START >-----------------------*/ {
2252 ** This NOP will be patched with LED ON
2253 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2255 SCR_NO_OP,
2258 ** Clear SIGP.
2260 SCR_FROM_REG (ctest2),
2263 ** Then jump to a certain point in tryloop.
2264 ** Due to the lack of indirect addressing the code
2265 ** is self modifying here.
2267 SCR_JUMP,
2268 }/*-------------------------< STARTPOS >--------------------*/,{
2269 PADDRH(tryloop),
2271 }/*-------------------------< SELECT >----------------------*/,{
2273 ** DSA contains the address of a scheduled
2274 ** data structure.
2276 ** SCRATCHA contains the address of the script,
2277 ** which starts the next entry.
2279 ** Set Initiator mode.
2281 ** (Target mode is left as an exercise for the reader)
2284 SCR_CLR (SCR_TRG),
2286 SCR_LOAD_REG (HS_REG, HS_SELECTING),
2290 ** And try to select this target.
2292 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2293 PADDR (reselect),
2295 }/*-------------------------< SELECT2 >----------------------*/,{
2297 ** Now there are 4 possibilities:
2299 ** (1) The ncr looses arbitration.
2300 ** This is ok, because it will try again,
2301 ** when the bus becomes idle.
2302 ** (But beware of the timeout function!)
2304 ** (2) The ncr is reselected.
2305 ** Then the script processor takes the jump
2306 ** to the RESELECT label.
2308 ** (3) The ncr wins arbitration.
2309 ** Then it will execute SCRIPTS instruction until
2310 ** the next instruction that checks SCSI phase.
2311 ** Then will stop and wait for selection to be
2312 ** complete or selection time-out to occur.
2313 ** As a result the SCRIPTS instructions until
2314 ** LOADPOS + 2 should be executed in parallel with
2315 ** the SCSI core performing selection.
2319 ** The M_REJECT problem seems to be due to a selection
2320 ** timing problem.
2321 ** Wait immediately for the selection to complete.
2322 ** (2.5x behaves so)
2324 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2328 ** Next time use the next slot.
2330 SCR_COPY (4),
2331 RADDR (temp),
2332 PADDR (startpos),
2334 ** The ncr doesn't have an indirect load
2335 ** or store command. So we have to
2336 ** copy part of the control block to a
2337 ** fixed place, where we can access it.
2339 ** We patch the address part of a
2340 ** COPY command with the DSA-register.
2342 SCR_COPY_F (4),
2343 RADDR (dsa),
2344 PADDR (loadpos),
2346 ** then we do the actual copy.
2348 SCR_COPY (sizeof (struct head)),
2350 ** continued after the next label ...
2352 }/*-------------------------< LOADPOS >---------------------*/,{
2354 NADDR (header),
2356 ** Wait for the next phase or the selection
2357 ** to complete or time-out.
2359 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2360 PADDR (prepare),
2362 }/*-------------------------< SEND_IDENT >----------------------*/,{
2364 ** Selection complete.
2365 ** Send the IDENTIFY and SIMPLE_TAG messages
2366 ** (and the M_X_SYNC_REQ message)
2368 SCR_MOVE_TBL ^ SCR_MSG_OUT,
2369 offsetof (struct dsb, smsg),
2370 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2371 PADDRH (resend_ident),
2372 SCR_LOAD_REG (scratcha, 0x80),
2374 SCR_COPY (1),
2375 RADDR (scratcha),
2376 NADDR (lastmsg),
2377 }/*-------------------------< PREPARE >----------------------*/,{
2379 ** load the savep (saved pointer) into
2380 ** the TEMP register (actual pointer)
2382 SCR_COPY (4),
2383 NADDR (header.savep),
2384 RADDR (temp),
2386 ** Initialize the status registers
2388 SCR_COPY (4),
2389 NADDR (header.status),
2390 RADDR (scr0),
2391 }/*-------------------------< PREPARE2 >---------------------*/,{
2393 ** Initialize the msgout buffer with a NOOP message.
2395 SCR_LOAD_REG (scratcha, M_NOOP),
2397 SCR_COPY (1),
2398 RADDR (scratcha),
2399 NADDR (msgout),
2400 #if 0
2401 SCR_COPY (1),
2402 RADDR (scratcha),
2403 NADDR (msgin),
2404 #endif
2406 ** Anticipate the COMMAND phase.
2407 ** This is the normal case for initial selection.
2409 SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
2410 PADDR (dispatch),
2412 }/*-------------------------< COMMAND >--------------------*/,{
2413 #ifdef SCSI_NCR_PROFILE_SUPPORT
2415 ** ... set a timestamp ...
2417 SCR_COPY (sizeof (u_long)),
2418 KVAR(SCRIPT_KVAR_JIFFIES),
2419 NADDR (header.stamp.command),
2420 #endif
2422 ** ... and send the command
2424 SCR_MOVE_TBL ^ SCR_COMMAND,
2425 offsetof (struct dsb, cmd),
2427 ** If status is still HS_NEGOTIATE, negotiation failed.
2428 ** We check this here, since we want to do that
2429 ** only once.
2431 SCR_FROM_REG (HS_REG),
2433 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2434 SIR_NEGO_FAILED,
2436 }/*-----------------------< DISPATCH >----------------------*/,{
2438 ** MSG_IN is the only phase that shall be
2439 ** entered at least once for each (re)selection.
2440 ** So we test it first.
2442 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
2443 PADDR (msg_in),
2445 SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
2448 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
2449 ** Possible data corruption during Memory Write and Invalidate.
2450 ** This work-around resets the addressing logic prior to the
2451 ** start of the first MOVE of a DATA IN phase.
2452 ** (See README.ncr53c8xx for more information)
2454 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2456 SCR_COPY (4),
2457 RADDR (scratcha),
2458 RADDR (scratcha),
2459 SCR_RETURN,
2461 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
2462 PADDR (status),
2463 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
2464 PADDR (command),
2465 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
2466 PADDR (msg_out),
2468 ** Discard one illegal phase byte, if required.
2470 SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
2472 SCR_COPY (1),
2473 RADDR (scratcha),
2474 NADDR (xerr_st),
2475 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
2477 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
2478 NADDR (scratch),
2479 SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
2481 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
2482 NADDR (scratch),
2483 SCR_JUMP,
2484 PADDR (dispatch),
2486 }/*-------------------------< CLRACK >----------------------*/,{
2488 ** Terminate possible pending message phase.
2490 SCR_CLR (SCR_ACK),
2492 SCR_JUMP,
2493 PADDR (dispatch),
2495 }/*-------------------------< NO_DATA >--------------------*/,{
2497 ** The target wants to tranfer too much data
2498 ** or in the wrong direction.
2499 ** Remember that in extended error.
2501 SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
2503 SCR_COPY (1),
2504 RADDR (scratcha),
2505 NADDR (xerr_st),
2507 ** Discard one data byte, if required.
2509 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2511 SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
2512 NADDR (scratch),
2513 SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2515 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2516 NADDR (scratch),
2518 ** .. and repeat as required.
2520 SCR_CALL,
2521 PADDR (dispatch),
2522 SCR_JUMP,
2523 PADDR (no_data),
2525 }/*-------------------------< STATUS >--------------------*/,{
2526 #ifdef SCSI_NCR_PROFILE_SUPPORT
2528 ** set the timestamp.
2530 SCR_COPY (sizeof (u_long)),
2531 KVAR(SCRIPT_KVAR_JIFFIES),
2532 NADDR (header.stamp.status),
2533 #endif
2535 ** get the status
2537 SCR_MOVE_ABS (1) ^ SCR_STATUS,
2538 NADDR (scratch),
2540 ** save status to scsi_status.
2541 ** mark as complete.
2543 SCR_TO_REG (SS_REG),
2545 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
2547 SCR_JUMP,
2548 PADDR (dispatch),
2549 }/*-------------------------< MSG_IN >--------------------*/,{
2551 ** Get the first byte of the message
2552 ** and save it to SCRATCHA.
2554 ** The script processor doesn't negate the
2555 ** ACK signal after this transfer.
2557 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2558 NADDR (msgin[0]),
2559 }/*-------------------------< MSG_IN2 >--------------------*/,{
2561 ** Handle this message.
2563 SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
2564 PADDR (complete),
2565 SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
2566 PADDR (disconnect),
2567 SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
2568 PADDR (save_dp),
2569 SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
2570 PADDR (restore_dp),
2571 SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
2572 PADDRH (msg_extended),
2573 SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
2574 PADDR (clrack),
2575 SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
2576 PADDRH (msg_reject),
2577 SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
2578 PADDRH (msg_ign_residue),
2580 ** Rest of the messages left as
2581 ** an exercise ...
2583 ** Unimplemented messages:
2584 ** fall through to MSG_BAD.
2586 }/*-------------------------< MSG_BAD >------------------*/,{
2588 ** unimplemented message - reject it.
2590 SCR_INT,
2591 SIR_REJECT_SENT,
2592 SCR_LOAD_REG (scratcha, M_REJECT),
2594 }/*-------------------------< SETMSG >----------------------*/,{
2595 SCR_COPY (1),
2596 RADDR (scratcha),
2597 NADDR (msgout),
2598 SCR_SET (SCR_ATN),
2600 SCR_JUMP,
2601 PADDR (clrack),
2602 }/*-------------------------< CLEANUP >-------------------*/,{
2604 ** dsa: Pointer to ccb
2605 ** or xxxxxxFF (no ccb)
2607 ** HS_REG: Host-Status (<>0!)
2609 SCR_FROM_REG (dsa),
2611 SCR_JUMP ^ IFTRUE (DATA (0xff)),
2612 PADDR (start),
2614 ** dsa is valid.
2615 ** complete the cleanup.
2617 SCR_JUMP,
2618 PADDR (cleanup_ok),
2620 }/*-------------------------< COMPLETE >-----------------*/,{
2622 ** Complete message.
2624 ** Copy TEMP register to LASTP in header.
2626 SCR_COPY (4),
2627 RADDR (temp),
2628 NADDR (header.lastp),
2630 ** When we terminate the cycle by clearing ACK,
2631 ** the target may disconnect immediately.
2633 ** We don't want to be told of an
2634 ** "unexpected disconnect",
2635 ** so we disable this feature.
2637 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2640 ** Terminate cycle ...
2642 SCR_CLR (SCR_ACK|SCR_ATN),
2645 ** ... and wait for the disconnect.
2647 SCR_WAIT_DISC,
2649 }/*-------------------------< CLEANUP_OK >----------------*/,{
2651 ** Save host status to header.
2653 SCR_COPY (4),
2654 RADDR (scr0),
2655 NADDR (header.status),
2657 ** and copy back the header to the ccb.
2659 SCR_COPY_F (4),
2660 RADDR (dsa),
2661 PADDR (cleanup0),
2662 SCR_COPY (sizeof (struct head)),
2663 NADDR (header),
2664 }/*-------------------------< CLEANUP0 >--------------------*/,{
2666 }/*-------------------------< SIGNAL >----------------------*/,{
2668 ** if job not completed ...
2670 SCR_FROM_REG (HS_REG),
2673 ** ... start the next command.
2675 SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
2676 PADDR(start),
2678 ** If command resulted in not GOOD status,
2679 ** call the C code if needed.
2681 SCR_FROM_REG (SS_REG),
2683 SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
2684 PADDRH (bad_status),
2686 #ifndef SCSI_NCR_CCB_DONE_SUPPORT
2689 ** ... signal completion to the host
2691 SCR_INT_FLY,
2694 ** Auf zu neuen Schandtaten!
2696 SCR_JUMP,
2697 PADDR(start),
2699 #else /* defined SCSI_NCR_CCB_DONE_SUPPORT */
2702 ** ... signal completion to the host
2704 SCR_JUMP,
2705 }/*------------------------< DONE_POS >---------------------*/,{
2706 PADDRH (done_queue),
2707 }/*------------------------< DONE_PLUG >--------------------*/,{
2708 SCR_INT,
2709 SIR_DONE_OVERFLOW,
2710 }/*------------------------< DONE_END >---------------------*/,{
2711 SCR_INT_FLY,
2713 SCR_COPY (4),
2714 RADDR (temp),
2715 PADDR (done_pos),
2716 SCR_JUMP,
2717 PADDR (start),
2719 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2721 }/*-------------------------< SAVE_DP >------------------*/,{
2723 ** SAVE_DP message:
2724 ** Copy TEMP register to SAVEP in header.
2726 SCR_COPY (4),
2727 RADDR (temp),
2728 NADDR (header.savep),
2729 SCR_CLR (SCR_ACK),
2731 SCR_JUMP,
2732 PADDR (dispatch),
2733 }/*-------------------------< RESTORE_DP >---------------*/,{
2735 ** RESTORE_DP message:
2736 ** Copy SAVEP in header to TEMP register.
2738 SCR_COPY (4),
2739 NADDR (header.savep),
2740 RADDR (temp),
2741 SCR_JUMP,
2742 PADDR (clrack),
2744 }/*-------------------------< DISCONNECT >---------------*/,{
2746 ** DISCONNECTing ...
2748 ** disable the "unexpected disconnect" feature,
2749 ** and remove the ACK signal.
2751 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2753 SCR_CLR (SCR_ACK|SCR_ATN),
2756 ** Wait for the disconnect.
2758 SCR_WAIT_DISC,
2760 #ifdef SCSI_NCR_PROFILE_SUPPORT
2762 ** Profiling:
2763 ** Set a time stamp,
2764 ** and count the disconnects.
2766 SCR_COPY (sizeof (u_long)),
2767 KVAR(SCRIPT_KVAR_JIFFIES),
2768 NADDR (header.stamp.disconnect),
2769 SCR_COPY (4),
2770 NADDR (disc_phys),
2771 RADDR (scratcha),
2772 SCR_REG_REG (scratcha, SCR_ADD, 0x01),
2774 SCR_COPY (4),
2775 RADDR (scratcha),
2776 NADDR (disc_phys),
2777 #endif
2779 ** Status is: DISCONNECTED.
2781 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2784 ** If QUIRK_AUTOSAVE is set,
2785 ** do an "save pointer" operation.
2787 SCR_FROM_REG (QU_REG),
2789 SCR_JUMP ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
2790 PADDR (cleanup_ok),
2792 ** like SAVE_DP message:
2793 ** Copy TEMP register to SAVEP in header.
2795 SCR_COPY (4),
2796 RADDR (temp),
2797 NADDR (header.savep),
2798 SCR_JUMP,
2799 PADDR (cleanup_ok),
2801 }/*-------------------------< MSG_OUT >-------------------*/,{
2803 ** The target requests a message.
2805 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2806 NADDR (msgout),
2807 SCR_COPY (1),
2808 NADDR (msgout),
2809 NADDR (lastmsg),
2811 ** If it was no ABORT message ...
2813 SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
2814 PADDRH (msg_out_abort),
2816 ** ... wait for the next phase
2817 ** if it's a message out, send it again, ...
2819 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2820 PADDR (msg_out),
2821 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
2823 ** ... else clear the message ...
2825 SCR_LOAD_REG (scratcha, M_NOOP),
2827 SCR_COPY (4),
2828 RADDR (scratcha),
2829 NADDR (msgout),
2831 ** ... and process the next phase
2833 SCR_JUMP,
2834 PADDR (dispatch),
2835 }/*-------------------------< IDLE >------------------------*/,{
2837 ** Nothing to do?
2838 ** Wait for reselect.
2839 ** This NOP will be patched with LED OFF
2840 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
2842 SCR_NO_OP,
2844 }/*-------------------------< RESELECT >--------------------*/,{
2846 ** make the DSA invalid.
2848 SCR_LOAD_REG (dsa, 0xff),
2850 SCR_CLR (SCR_TRG),
2852 SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
2855 ** Sleep waiting for a reselection.
2856 ** If SIGP is set, special treatment.
2858 ** Zu allem bereit ..
2860 SCR_WAIT_RESEL,
2861 PADDR(start),
2862 }/*-------------------------< RESELECTED >------------------*/,{
2864 ** This NOP will be patched with LED ON
2865 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2867 SCR_NO_OP,
2870 ** ... zu nichts zu gebrauchen ?
2872 ** load the target id into the SFBR
2873 ** and jump to the control block.
2875 ** Look at the declarations of
2876 ** - struct ncb
2877 ** - struct tcb
2878 ** - struct lcb
2879 ** - struct ccb
2880 ** to understand what's going on.
2882 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2884 SCR_TO_REG (sdid),
2886 SCR_JUMP,
2887 NADDR (jump_tcb),
2889 }/*-------------------------< RESEL_DSA >-------------------*/,{
2891 ** Ack the IDENTIFY or TAG previously received.
2893 SCR_CLR (SCR_ACK),
2896 ** The ncr doesn't have an indirect load
2897 ** or store command. So we have to
2898 ** copy part of the control block to a
2899 ** fixed place, where we can access it.
2901 ** We patch the address part of a
2902 ** COPY command with the DSA-register.
2904 SCR_COPY_F (4),
2905 RADDR (dsa),
2906 PADDR (loadpos1),
2908 ** then we do the actual copy.
2910 SCR_COPY (sizeof (struct head)),
2912 ** continued after the next label ...
2915 }/*-------------------------< LOADPOS1 >-------------------*/,{
2917 NADDR (header),
2918 #ifdef SCSI_NCR_PROFILE_SUPPORT
2920 ** Set a time stamp for this reselection
2922 SCR_COPY (sizeof (u_long)),
2923 KVAR(SCRIPT_KVAR_JIFFIES),
2924 NADDR (header.stamp.reselect),
2925 #endif
2927 ** The DSA contains the data structure address.
2929 SCR_JUMP,
2930 PADDR (prepare),
2932 }/*-------------------------< RESEL_LUN >-------------------*/,{
2934 ** come back to this point
2935 ** to get an IDENTIFY message
2936 ** Wait for a msg_in phase.
2938 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2939 SIR_RESEL_NO_MSG_IN,
2941 ** message phase.
2942 ** Read the data directly from the BUS DATA lines.
2943 ** This helps to support very old SCSI devices that
2944 ** may reselect without sending an IDENTIFY.
2946 SCR_FROM_REG (sbdl),
2949 ** It should be an Identify message.
2951 SCR_RETURN,
2953 }/*-------------------------< RESEL_TAG >-------------------*/,{
2955 ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
2956 ** Agressive optimization, is'nt it?
2957 ** No need to test the SIMPLE TAG message, since the
2958 ** driver only supports conformant devices for tags. ;-)
2960 SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
2961 NADDR (msgin),
2963 ** Read the TAG from the SIDL.
2964 ** Still an aggressive optimization. ;-)
2965 ** Compute the CCB indirect jump address which
2966 ** is (#TAG*2 & 0xfc) due to tag numbering using
2967 ** 1,3,5..MAXTAGS*2+1 actual values.
2969 SCR_REG_SFBR (sidl, SCR_SHL, 0),
2971 SCR_SFBR_REG (temp, SCR_AND, 0xfc),
2973 }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
2974 SCR_COPY_F (4),
2975 RADDR (temp),
2976 PADDR (nexus_indirect),
2977 SCR_COPY (4),
2978 }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
2980 RADDR (temp),
2981 SCR_RETURN,
2983 }/*-------------------------< RESEL_NOTAG >-------------------*/,{
2985 ** No tag expected.
2986 ** Read an throw away the IDENTIFY.
2988 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2989 NADDR (msgin),
2990 SCR_JUMP,
2991 PADDR (jump_to_nexus),
2992 }/*-------------------------< DATA_IN >--------------------*/,{
2994 ** Because the size depends on the
2995 ** #define MAX_SCATTERL parameter,
2996 ** it is filled in at runtime.
2998 ** ##===========< i=0; i<MAX_SCATTERL >=========
2999 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3000 ** || PADDR (dispatch),
3001 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
3002 ** || offsetof (struct dsb, data[ i]),
3003 ** ##==========================================
3005 **---------------------------------------------------------
3008 }/*-------------------------< DATA_IN2 >-------------------*/,{
3009 SCR_CALL,
3010 PADDR (dispatch),
3011 SCR_JUMP,
3012 PADDR (no_data),
3013 }/*-------------------------< DATA_OUT >--------------------*/,{
3015 ** Because the size depends on the
3016 ** #define MAX_SCATTERL parameter,
3017 ** it is filled in at runtime.
3019 ** ##===========< i=0; i<MAX_SCATTERL >=========
3020 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3021 ** || PADDR (dispatch),
3022 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
3023 ** || offsetof (struct dsb, data[ i]),
3024 ** ##==========================================
3026 **---------------------------------------------------------
3029 }/*-------------------------< DATA_OUT2 >-------------------*/,{
3030 SCR_CALL,
3031 PADDR (dispatch),
3032 SCR_JUMP,
3033 PADDR (no_data),
3034 }/*--------------------------------------------------------*/
3037 static struct scripth scripth0 __initdata = {
3038 /*-------------------------< TRYLOOP >---------------------*/{
3040 ** Start the next entry.
3041 ** Called addresses point to the launch script in the CCB.
3042 ** They are patched by the main processor.
3044 ** Because the size depends on the
3045 ** #define MAX_START parameter, it is filled
3046 ** in at runtime.
3048 **-----------------------------------------------------------
3050 ** ##===========< I=0; i<MAX_START >===========
3051 ** || SCR_CALL,
3052 ** || PADDR (idle),
3053 ** ##==========================================
3055 **-----------------------------------------------------------
3058 }/*------------------------< TRYLOOP2 >---------------------*/,{
3059 SCR_JUMP,
3060 PADDRH(tryloop),
3062 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
3064 }/*------------------------< DONE_QUEUE >-------------------*/,{
3066 ** Copy the CCB address to the next done entry.
3067 ** Because the size depends on the
3068 ** #define MAX_DONE parameter, it is filled
3069 ** in at runtime.
3071 **-----------------------------------------------------------
3073 ** ##===========< I=0; i<MAX_DONE >===========
3074 ** || SCR_COPY (sizeof(ccb_p)),
3075 ** || NADDR (header.cp),
3076 ** || NADDR (ccb_done[i]),
3077 ** || SCR_CALL,
3078 ** || PADDR (done_end),
3079 ** ##==========================================
3081 **-----------------------------------------------------------
3084 }/*------------------------< DONE_QUEUE2 >------------------*/,{
3085 SCR_JUMP,
3086 PADDRH (done_queue),
3088 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
3089 }/*------------------------< SELECT_NO_ATN >-----------------*/,{
3091 ** Set Initiator mode.
3092 ** And try to select this target without ATN.
3095 SCR_CLR (SCR_TRG),
3097 SCR_LOAD_REG (HS_REG, HS_SELECTING),
3099 SCR_SEL_TBL ^ offsetof (struct dsb, select),
3100 PADDR (reselect),
3101 SCR_JUMP,
3102 PADDR (select2),
3104 }/*-------------------------< CANCEL >------------------------*/,{
3106 SCR_LOAD_REG (scratcha, HS_ABORTED),
3108 SCR_JUMPR,
3110 }/*-------------------------< SKIP >------------------------*/,{
3111 SCR_LOAD_REG (scratcha, 0),
3114 ** This entry has been canceled.
3115 ** Next time use the next slot.
3117 SCR_COPY (4),
3118 RADDR (temp),
3119 PADDR (startpos),
3121 ** The ncr doesn't have an indirect load
3122 ** or store command. So we have to
3123 ** copy part of the control block to a
3124 ** fixed place, where we can access it.
3126 ** We patch the address part of a
3127 ** COPY command with the DSA-register.
3129 SCR_COPY_F (4),
3130 RADDR (dsa),
3131 PADDRH (skip2),
3133 ** then we do the actual copy.
3135 SCR_COPY (sizeof (struct head)),
3137 ** continued after the next label ...
3139 }/*-------------------------< SKIP2 >---------------------*/,{
3141 NADDR (header),
3143 ** Initialize the status registers
3145 SCR_COPY (4),
3146 NADDR (header.status),
3147 RADDR (scr0),
3149 ** Force host status.
3151 SCR_FROM_REG (scratcha),
3153 SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
3155 SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
3157 SCR_JUMPR,
3159 SCR_TO_REG (HS_REG),
3161 SCR_LOAD_REG (SS_REG, S_GOOD),
3163 SCR_JUMP,
3164 PADDR (cleanup_ok),
3166 },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
3168 ** Ignore all data in byte, until next phase
3170 SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
3171 PADDRH (par_err_other),
3172 SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
3173 NADDR (scratch),
3174 SCR_JUMPR,
3175 -24,
3176 },/*-------------------------< PAR_ERR_OTHER >------------------*/{
3178 ** count it.
3180 SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
3183 ** jump to dispatcher.
3185 SCR_JUMP,
3186 PADDR (dispatch),
3187 }/*-------------------------< MSG_REJECT >---------------*/,{
3189 ** If a negotiation was in progress,
3190 ** negotiation failed.
3191 ** Otherwise, let the C code print
3192 ** some message.
3194 SCR_FROM_REG (HS_REG),
3196 SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
3197 SIR_REJECT_RECEIVED,
3198 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
3199 SIR_NEGO_FAILED,
3200 SCR_JUMP,
3201 PADDR (clrack),
3203 }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
3205 ** Terminate cycle
3207 SCR_CLR (SCR_ACK),
3209 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3210 PADDR (dispatch),
3212 ** get residue size.
3214 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3215 NADDR (msgin[1]),
3217 ** Size is 0 .. ignore message.
3219 SCR_JUMP ^ IFTRUE (DATA (0)),
3220 PADDR (clrack),
3222 ** Size is not 1 .. have to interrupt.
3224 SCR_JUMPR ^ IFFALSE (DATA (1)),
3227 ** Check for residue byte in swide register
3229 SCR_FROM_REG (scntl2),
3231 SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
3234 ** There IS data in the swide register.
3235 ** Discard it.
3237 SCR_REG_REG (scntl2, SCR_OR, WSR),
3239 SCR_JUMP,
3240 PADDR (clrack),
3242 ** Load again the size to the sfbr register.
3244 SCR_FROM_REG (scratcha),
3246 SCR_INT,
3247 SIR_IGN_RESIDUE,
3248 SCR_JUMP,
3249 PADDR (clrack),
3251 }/*-------------------------< MSG_EXTENDED >-------------*/,{
3253 ** Terminate cycle
3255 SCR_CLR (SCR_ACK),
3257 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3258 PADDR (dispatch),
3260 ** get length.
3262 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3263 NADDR (msgin[1]),
3266 SCR_JUMP ^ IFTRUE (DATA (3)),
3267 PADDRH (msg_ext_3),
3268 SCR_JUMP ^ IFFALSE (DATA (2)),
3269 PADDR (msg_bad),
3270 }/*-------------------------< MSG_EXT_2 >----------------*/,{
3271 SCR_CLR (SCR_ACK),
3273 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3274 PADDR (dispatch),
3276 ** get extended message code.
3278 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3279 NADDR (msgin[2]),
3280 SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
3281 PADDRH (msg_wdtr),
3283 ** unknown extended message
3285 SCR_JUMP,
3286 PADDR (msg_bad)
3287 }/*-------------------------< MSG_WDTR >-----------------*/,{
3288 SCR_CLR (SCR_ACK),
3290 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3291 PADDR (dispatch),
3293 ** get data bus width
3295 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3296 NADDR (msgin[3]),
3298 ** let the host do the real work.
3300 SCR_INT,
3301 SIR_NEGO_WIDE,
3303 ** let the target fetch our answer.
3305 SCR_SET (SCR_ATN),
3307 SCR_CLR (SCR_ACK),
3309 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3310 PADDRH (nego_bad_phase),
3312 }/*-------------------------< SEND_WDTR >----------------*/,{
3314 ** Send the M_X_WIDE_REQ
3316 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
3317 NADDR (msgout),
3318 SCR_COPY (1),
3319 NADDR (msgout),
3320 NADDR (lastmsg),
3321 SCR_JUMP,
3322 PADDR (msg_out_done),
3324 }/*-------------------------< MSG_EXT_3 >----------------*/,{
3325 SCR_CLR (SCR_ACK),
3327 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3328 PADDR (dispatch),
3330 ** get extended message code.
3332 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3333 NADDR (msgin[2]),
3334 SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
3335 PADDRH (msg_sdtr),
3337 ** unknown extended message
3339 SCR_JUMP,
3340 PADDR (msg_bad)
3342 }/*-------------------------< MSG_SDTR >-----------------*/,{
3343 SCR_CLR (SCR_ACK),
3345 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3346 PADDR (dispatch),
3348 ** get period and offset
3350 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
3351 NADDR (msgin[3]),
3353 ** let the host do the real work.
3355 SCR_INT,
3356 SIR_NEGO_SYNC,
3358 ** let the target fetch our answer.
3360 SCR_SET (SCR_ATN),
3362 SCR_CLR (SCR_ACK),
3364 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3365 PADDRH (nego_bad_phase),
3367 }/*-------------------------< SEND_SDTR >-------------*/,{
3369 ** Send the M_X_SYNC_REQ
3371 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
3372 NADDR (msgout),
3373 SCR_COPY (1),
3374 NADDR (msgout),
3375 NADDR (lastmsg),
3376 SCR_JUMP,
3377 PADDR (msg_out_done),
3379 }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
3380 SCR_INT,
3381 SIR_NEGO_PROTO,
3382 SCR_JUMP,
3383 PADDR (dispatch),
3385 }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
3387 ** After ABORT message,
3389 ** expect an immediate disconnect, ...
3391 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3393 SCR_CLR (SCR_ACK|SCR_ATN),
3395 SCR_WAIT_DISC,
3398 ** ... and set the status to "ABORTED"
3400 SCR_LOAD_REG (HS_REG, HS_ABORTED),
3402 SCR_JUMP,
3403 PADDR (cleanup),
3405 }/*-------------------------< HDATA_IN >-------------------*/,{
3407 ** Because the size depends on the
3408 ** #define MAX_SCATTERH parameter,
3409 ** it is filled in at runtime.
3411 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3412 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3413 ** || PADDR (dispatch),
3414 ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
3415 ** || offsetof (struct dsb, data[ i]),
3416 ** ##===================================================
3418 **---------------------------------------------------------
3421 }/*-------------------------< HDATA_IN2 >------------------*/,{
3422 SCR_JUMP,
3423 PADDR (data_in),
3425 }/*-------------------------< HDATA_OUT >-------------------*/,{
3427 ** Because the size depends on the
3428 ** #define MAX_SCATTERH parameter,
3429 ** it is filled in at runtime.
3431 ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3432 ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3433 ** || PADDR (dispatch),
3434 ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
3435 ** || offsetof (struct dsb, data[ i]),
3436 ** ##===================================================
3438 **---------------------------------------------------------
3441 }/*-------------------------< HDATA_OUT2 >------------------*/,{
3442 SCR_JUMP,
3443 PADDR (data_out),
3445 }/*-------------------------< RESET >----------------------*/,{
3447 ** Send a M_RESET message if bad IDENTIFY
3448 ** received on reselection.
3450 SCR_LOAD_REG (scratcha, M_ABORT_TAG),
3452 SCR_JUMP,
3453 PADDRH (abort_resel),
3454 }/*-------------------------< ABORTTAG >-------------------*/,{
3456 ** Abort a wrong tag received on reselection.
3458 SCR_LOAD_REG (scratcha, M_ABORT_TAG),
3460 SCR_JUMP,
3461 PADDRH (abort_resel),
3462 }/*-------------------------< ABORT >----------------------*/,{
3464 ** Abort a reselection when no active CCB.
3466 SCR_LOAD_REG (scratcha, M_ABORT),
3468 }/*-------------------------< ABORT_RESEL >----------------*/,{
3469 SCR_COPY (1),
3470 RADDR (scratcha),
3471 NADDR (msgout),
3472 SCR_SET (SCR_ATN),
3474 SCR_CLR (SCR_ACK),
3477 ** and send it.
3478 ** we expect an immediate disconnect
3480 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3482 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
3483 NADDR (msgout),
3484 SCR_COPY (1),
3485 NADDR (msgout),
3486 NADDR (lastmsg),
3487 SCR_CLR (SCR_ACK|SCR_ATN),
3489 SCR_WAIT_DISC,
3491 SCR_JUMP,
3492 PADDR (start),
3493 }/*-------------------------< RESEND_IDENT >-------------------*/,{
3495 ** The target stays in MSG OUT phase after having acked
3496 ** Identify [+ Tag [+ Extended message ]]. Targets shall
3497 ** behave this way on parity error.
3498 ** We must send it again all the messages.
3500 SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
3501 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
3502 SCR_JUMP,
3503 PADDR (send_ident),
3504 }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
3505 SCR_CLR (SCR_ATN),
3507 SCR_JUMP,
3508 }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
3510 }/*-------------------------< SDATA_IN >-------------------*/,{
3511 SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3512 PADDR (dispatch),
3513 SCR_MOVE_TBL ^ SCR_DATA_IN,
3514 offsetof (struct dsb, sense),
3515 SCR_CALL,
3516 PADDR (dispatch),
3517 SCR_JUMP,
3518 PADDR (no_data),
3519 }/*-------------------------< DATA_IO >--------------------*/,{
3521 ** We jump here if the data direction was unknown at the
3522 ** time we had to queue the command to the scripts processor.
3523 ** Pointers had been set as follow in this situation:
3524 ** savep --> DATA_IO
3525 ** lastp --> start pointer when DATA_IN
3526 ** goalp --> goal pointer when DATA_IN
3527 ** wlastp --> start pointer when DATA_OUT
3528 ** wgoalp --> goal pointer when DATA_OUT
3529 ** This script sets savep/lastp/goalp according to the
3530 ** direction chosen by the target.
3532 SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
3535 ** Direction is DATA IN.
3536 ** Warning: we jump here, even when phase is DATA OUT.
3538 SCR_COPY (4),
3539 NADDR (header.lastp),
3540 NADDR (header.savep),
3543 ** Jump to the SCRIPTS according to actual direction.
3545 SCR_COPY (4),
3546 NADDR (header.savep),
3547 RADDR (temp),
3548 SCR_RETURN,
3551 ** Direction is DATA OUT.
3553 SCR_COPY (4),
3554 NADDR (header.wlastp),
3555 NADDR (header.lastp),
3556 SCR_COPY (4),
3557 NADDR (header.wgoalp),
3558 NADDR (header.goalp),
3559 SCR_JUMPR,
3560 -64,
3561 }/*-------------------------< BAD_IDENTIFY >---------------*/,{
3563 ** If message phase but not an IDENTIFY,
3564 ** get some help from the C code.
3565 ** Old SCSI device may behave so.
3567 SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
3569 SCR_INT,
3570 SIR_RESEL_NO_IDENTIFY,
3571 SCR_JUMP,
3572 PADDRH (reset),
3574 ** Message is an IDENTIFY, but lun is unknown.
3575 ** Read the message, since we got it directly
3576 ** from the SCSI BUS data lines.
3577 ** Signal problem to C code for logging the event.
3578 ** Send a M_ABORT to clear all pending tasks.
3580 SCR_INT,
3581 SIR_RESEL_BAD_LUN,
3582 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3583 NADDR (msgin),
3584 SCR_JUMP,
3585 PADDRH (abort),
3586 }/*-------------------------< BAD_I_T_L >------------------*/,{
3588 ** We donnot have a task for that I_T_L.
3589 ** Signal problem to C code for logging the event.
3590 ** Send a M_ABORT message.
3592 SCR_INT,
3593 SIR_RESEL_BAD_I_T_L,
3594 SCR_JUMP,
3595 PADDRH (abort),
3596 }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
3598 ** We donnot have a task that matches the tag.
3599 ** Signal problem to C code for logging the event.
3600 ** Send a M_ABORTTAG message.
3602 SCR_INT,
3603 SIR_RESEL_BAD_I_T_L_Q,
3604 SCR_JUMP,
3605 PADDRH (aborttag),
3606 }/*-------------------------< BAD_TARGET >-----------------*/,{
3608 ** We donnot know the target that reselected us.
3609 ** Grab the first message if any (IDENTIFY).
3610 ** Signal problem to C code for logging the event.
3611 ** M_RESET message.
3613 SCR_INT,
3614 SIR_RESEL_BAD_TARGET,
3615 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
3617 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3618 NADDR (msgin),
3619 SCR_JUMP,
3620 PADDRH (reset),
3621 }/*-------------------------< BAD_STATUS >-----------------*/,{
3623 ** If command resulted in either QUEUE FULL,
3624 ** CHECK CONDITION or COMMAND TERMINATED,
3625 ** call the C code.
3627 SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
3628 SIR_BAD_STATUS,
3629 SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
3630 SIR_BAD_STATUS,
3631 SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
3632 SIR_BAD_STATUS,
3633 SCR_RETURN,
3635 }/*-------------------------< START_RAM >-------------------*/,{
3637 ** Load the script into on-chip RAM,
3638 ** and jump to start point.
3640 SCR_COPY_F (4),
3641 RADDR (scratcha),
3642 PADDRH (start_ram0),
3643 SCR_COPY (sizeof (struct script)),
3644 }/*-------------------------< START_RAM0 >--------------------*/,{
3646 PADDR (start),
3647 SCR_JUMP,
3648 PADDR (start),
3649 }/*-------------------------< STO_RESTART >-------------------*/,{
3652 ** Repair start queue (e.g. next time use the next slot)
3653 ** and jump to start point.
3655 SCR_COPY (4),
3656 RADDR (temp),
3657 PADDR (startpos),
3658 SCR_JUMP,
3659 PADDR (start),
3660 }/*-------------------------< SNOOPTEST >-------------------*/,{
3662 ** Read the variable.
3664 SCR_COPY (4),
3665 NADDR(ncr_cache),
3666 RADDR (scratcha),
3668 ** Write the variable.
3670 SCR_COPY (4),
3671 RADDR (temp),
3672 NADDR(ncr_cache),
3674 ** Read back the variable.
3676 SCR_COPY (4),
3677 NADDR(ncr_cache),
3678 RADDR (temp),
3679 }/*-------------------------< SNOOPEND >-------------------*/,{
3681 ** And stop.
3683 SCR_INT,
3685 }/*--------------------------------------------------------*/
3688 /*==========================================================
3691 ** Fill in #define dependent parts of the script
3694 **==========================================================
3697 void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
3699 int i;
3700 ncrcmd *p;
3702 p = scrh->tryloop;
3703 for (i=0; i<MAX_START; i++) {
3704 *p++ =SCR_CALL;
3705 *p++ =PADDR (idle);
3708 assert ((u_long)p == (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
3710 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
3712 p = scrh->done_queue;
3713 for (i = 0; i<MAX_DONE; i++) {
3714 *p++ =SCR_COPY (sizeof(ccb_p));
3715 *p++ =NADDR (header.cp);
3716 *p++ =NADDR (ccb_done[i]);
3717 *p++ =SCR_CALL;
3718 *p++ =PADDR (done_end);
3721 assert ((u_long)p ==(u_long)&scrh->done_queue+sizeof(scrh->done_queue));
3723 #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
3725 p = scrh->hdata_in;
3726 for (i=0; i<MAX_SCATTERH; i++) {
3727 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3728 *p++ =PADDR (dispatch);
3729 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3730 *p++ =offsetof (struct dsb, data[i]);
3732 assert ((u_long)p == (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
3734 p = scr->data_in;
3735 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3736 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3737 *p++ =PADDR (dispatch);
3738 *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3739 *p++ =offsetof (struct dsb, data[i]);
3741 assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
3743 p = scrh->hdata_out;
3744 for (i=0; i<MAX_SCATTERH; i++) {
3745 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3746 *p++ =PADDR (dispatch);
3747 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3748 *p++ =offsetof (struct dsb, data[i]);
3750 assert ((u_long)p==(u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
3752 p = scr->data_out;
3753 for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3754 *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3755 *p++ =PADDR (dispatch);
3756 *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3757 *p++ =offsetof (struct dsb, data[i]);
3760 assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
3763 /*==========================================================
3766 ** Copy and rebind a script.
3769 **==========================================================
3772 static void __init ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
3774 ncrcmd opcode, new, old, tmp1, tmp2;
3775 ncrcmd *start, *end;
3776 int relocs;
3777 int opchanged = 0;
3779 start = src;
3780 end = src + len/4;
3782 while (src < end) {
3784 opcode = *src++;
3785 *dst++ = cpu_to_scr(opcode);
3788 ** If we forget to change the length
3789 ** in struct script, a field will be
3790 ** padded with 0. This is an illegal
3791 ** command.
3794 if (opcode == 0) {
3795 printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
3796 ncr_name(np), (int) (src-start-1));
3797 MDELAY (1000);
3800 if (DEBUG_FLAGS & DEBUG_SCRIPT)
3801 printk (KERN_DEBUG "%p: <%x>\n",
3802 (src-1), (unsigned)opcode);
3805 ** We don't have to decode ALL commands
3807 switch (opcode >> 28) {
3809 case 0xc:
3811 ** COPY has TWO arguments.
3813 relocs = 2;
3814 tmp1 = src[0];
3815 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3816 tmp1 = 0;
3817 tmp2 = src[1];
3818 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3819 tmp2 = 0;
3820 if ((tmp1 ^ tmp2) & 3) {
3821 printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
3822 ncr_name(np), (int) (src-start-1));
3823 MDELAY (1000);
3826 ** If PREFETCH feature not enabled, remove
3827 ** the NO FLUSH bit if present.
3829 if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
3830 dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
3831 ++opchanged;
3833 break;
3835 case 0x0:
3837 ** MOVE (absolute address)
3839 relocs = 1;
3840 break;
3842 case 0x8:
3844 ** JUMP / CALL
3845 ** dont't relocate if relative :-)
3847 if (opcode & 0x00800000)
3848 relocs = 0;
3849 else
3850 relocs = 1;
3851 break;
3853 case 0x4:
3854 case 0x5:
3855 case 0x6:
3856 case 0x7:
3857 relocs = 1;
3858 break;
3860 default:
3861 relocs = 0;
3862 break;
3865 if (relocs) {
3866 while (relocs--) {
3867 old = *src++;
3869 switch (old & RELOC_MASK) {
3870 case RELOC_REGISTER:
3871 new = (old & ~RELOC_MASK)
3872 + bus_dvma_to_mem(np->paddr);
3873 break;
3874 case RELOC_LABEL:
3875 new = (old & ~RELOC_MASK) + np->p_script;
3876 break;
3877 case RELOC_LABELH:
3878 new = (old & ~RELOC_MASK) + np->p_scripth;
3879 break;
3880 case RELOC_SOFTC:
3881 new = (old & ~RELOC_MASK) + vtophys(np);
3882 break;
3883 case RELOC_KVAR:
3884 if (((old & ~RELOC_MASK) <
3885 SCRIPT_KVAR_FIRST) ||
3886 ((old & ~RELOC_MASK) >
3887 SCRIPT_KVAR_LAST))
3888 panic("ncr KVAR out of range");
3889 new = vtophys(script_kvars[old &
3890 ~RELOC_MASK]);
3891 break;
3892 case 0:
3893 /* Don't relocate a 0 address. */
3894 if (old == 0) {
3895 new = old;
3896 break;
3898 /* fall through */
3899 default:
3900 panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
3901 break;
3904 *dst++ = cpu_to_scr(new);
3906 } else
3907 *dst++ = cpu_to_scr(*src++);
3912 /*==========================================================
3915 ** Auto configuration: attach and init a host adapter.
3918 **==========================================================
3922 ** Linux host data structure
3924 ** The script area is allocated in the host data structure
3925 ** because kmalloc() returns NULL during scsi initialisations
3926 ** with Linux 1.2.X
3929 struct host_data {
3930 struct ncb *ncb;
3932 char ncb_align[CACHE_LINE_SIZE-1]; /* Filler for alignment */
3933 struct ncb _ncb_data;
3935 char ccb_align[CACHE_LINE_SIZE-1]; /* Filler for alignment */
3936 struct ccb _ccb_data;
3938 char scr_align[CACHE_LINE_SIZE-1]; /* Filler for alignment */
3939 struct script script_data;
3941 struct scripth scripth_data;
3945 ** Print something which allows to retrieve the controler type, unit,
3946 ** target, lun concerned by a kernel message.
3949 static void PRINT_TARGET(ncb_p np, int target)
3951 printk(KERN_INFO "%s-<%d,*>: ", ncr_name(np), target);
3954 static void PRINT_LUN(ncb_p np, int target, int lun)
3956 printk(KERN_INFO "%s-<%d,%d>: ", ncr_name(np), target, lun);
3959 static void PRINT_ADDR(Scsi_Cmnd *cmd)
3961 struct host_data *host_data = (struct host_data *) cmd->host->hostdata;
3962 PRINT_LUN(host_data->ncb, cmd->target, cmd->lun);
3965 /*==========================================================
3967 ** NCR chip clock divisor table.
3968 ** Divisors are multiplied by 10,000,000 in order to make
3969 ** calculations more simple.
3971 **==========================================================
3974 #define _5M 5000000
3975 static u_long div_10M[] =
3976 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3979 /*===============================================================
3981 ** Prepare io register values used by ncr_init() according
3982 ** to selected and supported features.
3984 ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3985 ** transfers. 32,64,128 are only supported by 875 and 895 chips.
3986 ** We use log base 2 (burst length) as internal code, with
3987 ** value 0 meaning "burst disabled".
3989 **===============================================================
3993 * Burst length from burst code.
3995 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3998 * Burst code from io register bits.
4000 #define burst_code(dmode, ctest4, ctest5) \
4001 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
4004 * Set initial io register bits from burst code.
4006 static inline void ncr_init_burst(ncb_p np, u_char bc)
4008 np->rv_ctest4 &= ~0x80;
4009 np->rv_dmode &= ~(0x3 << 6);
4010 np->rv_ctest5 &= ~0x4;
4012 if (!bc) {
4013 np->rv_ctest4 |= 0x80;
4015 else {
4016 --bc;
4017 np->rv_dmode |= ((bc & 0x3) << 6);
4018 np->rv_ctest5 |= (bc & 0x4);
4022 #ifdef SCSI_NCR_NVRAM_SUPPORT
4025 ** Get target set-up from Symbios format NVRAM.
4028 static void __init
4029 ncr_Symbios_setup_target(ncb_p np, int target, Symbios_nvram *nvram)
4031 tcb_p tp = &np->target[target];
4032 Symbios_target *tn = &nvram->target[target];
4034 tp->usrsync = tn->sync_period ? (tn->sync_period + 3) / 4 : 255;
4035 tp->usrwide = tn->bus_width == 0x10 ? 1 : 0;
4036 tp->usrtags =
4037 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? SCSI_NCR_MAX_TAGS : 0;
4039 if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
4040 tp->usrflag |= UF_NODISC;
4041 if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
4042 tp->usrflag |= UF_NOSCAN;
4046 ** Get target set-up from Tekram format NVRAM.
4049 static void __init
4050 ncr_Tekram_setup_target(ncb_p np, int target, Tekram_nvram *nvram)
4052 tcb_p tp = &np->target[target];
4053 struct Tekram_target *tn = &nvram->target[target];
4054 int i;
4056 if (tn->flags & TEKRAM_SYNC_NEGO) {
4057 i = tn->sync_index & 0xf;
4058 tp->usrsync = i < 12 ? Tekram_sync[i] : 255;
4061 tp->usrwide = (tn->flags & TEKRAM_WIDE_NEGO) ? 1 : 0;
4063 if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
4064 tp->usrtags = 2 << nvram->max_tags_index;
4067 if (!(tn->flags & TEKRAM_DISCONNECT_ENABLE))
4068 tp->usrflag = UF_NODISC;
4070 /* If any device does not support parity, we will not use this option */
4071 if (!(tn->flags & TEKRAM_PARITY_CHECK))
4072 np->rv_scntl0 &= ~0x0a; /* SCSI parity checking disabled */
4074 #endif /* SCSI_NCR_NVRAM_SUPPORT */
4076 static int __init ncr_prepare_setting(ncb_p np, ncr_nvram *nvram)
4078 u_char burst_max;
4079 u_long period;
4080 int i;
4083 ** Save assumed BIOS setting
4086 np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
4087 np->sv_scntl3 = INB(nc_scntl3) & 0x07;
4088 np->sv_dmode = INB(nc_dmode) & 0xce;
4089 np->sv_dcntl = INB(nc_dcntl) & 0xa8;
4090 np->sv_ctest3 = INB(nc_ctest3) & 0x01;
4091 np->sv_ctest4 = INB(nc_ctest4) & 0x80;
4092 np->sv_ctest5 = INB(nc_ctest5) & 0x24;
4093 np->sv_gpcntl = INB(nc_gpcntl);
4094 np->sv_stest2 = INB(nc_stest2) & 0x20;
4095 np->sv_stest4 = INB(nc_stest4);
4098 ** Wide ?
4101 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
4104 ** Get the frequency of the chip's clock.
4105 ** Find the right value for scntl3.
4108 if (np->features & FE_QUAD)
4109 np->multiplier = 4;
4110 else if (np->features & FE_DBLR)
4111 np->multiplier = 2;
4112 else
4113 np->multiplier = 1;
4115 np->clock_khz = (np->features & FE_CLK80)? 80000 : 40000;
4116 np->clock_khz *= np->multiplier;
4118 if (np->clock_khz != 40000)
4119 ncr_getclock(np, np->multiplier);
4122 * Divisor to be used for async (timer pre-scaler).
4124 i = np->clock_divn - 1;
4125 while (--i >= 0) {
4126 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
4127 ++i;
4128 break;
4131 np->rv_scntl3 = i+1;
4134 * Minimum synchronous period factor supported by the chip.
4135 * Btw, 'period' is in tenths of nanoseconds.
4138 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
4139 if (period <= 250) np->minsync = 10;
4140 else if (period <= 303) np->minsync = 11;
4141 else if (period <= 500) np->minsync = 12;
4142 else np->minsync = (period + 40 - 1) / 40;
4145 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
4148 if (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
4149 np->minsync = 25;
4150 else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
4151 np->minsync = 12;
4154 * Maximum synchronous period factor supported by the chip.
4157 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
4158 np->maxsync = period > 2540 ? 254 : period / 10;
4161 ** Prepare initial value of other IO registers
4163 #if defined SCSI_NCR_TRUST_BIOS_SETTING
4164 np->rv_scntl0 = np->sv_scntl0;
4165 np->rv_dmode = np->sv_dmode;
4166 np->rv_dcntl = np->sv_dcntl;
4167 np->rv_ctest3 = np->sv_ctest3;
4168 np->rv_ctest4 = np->sv_ctest4;
4169 np->rv_ctest5 = np->sv_ctest5;
4170 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
4171 #else
4174 ** Select burst length (dwords)
4176 burst_max = driver_setup.burst_max;
4177 if (burst_max == 255)
4178 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
4179 if (burst_max > 7)
4180 burst_max = 7;
4181 if (burst_max > np->maxburst)
4182 burst_max = np->maxburst;
4185 ** Select all supported special features
4187 if (np->features & FE_ERL)
4188 np->rv_dmode |= ERL; /* Enable Read Line */
4189 if (np->features & FE_BOF)
4190 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
4191 if (np->features & FE_ERMP)
4192 np->rv_dmode |= ERMP; /* Enable Read Multiple */
4193 if (np->features & FE_PFEN)
4194 np->rv_dcntl |= PFEN; /* Prefetch Enable */
4195 if (np->features & FE_CLSE)
4196 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
4197 if (np->features & FE_WRIE)
4198 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
4199 if (np->features & FE_DFS)
4200 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
4203 ** Select some other
4205 if (driver_setup.master_parity)
4206 np->rv_ctest4 |= MPEE; /* Master parity checking */
4207 if (driver_setup.scsi_parity)
4208 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
4210 #ifdef SCSI_NCR_NVRAM_SUPPORT
4212 ** Get parity checking, host ID and verbose mode from NVRAM
4214 if (nvram) {
4215 switch(nvram->type) {
4216 case SCSI_NCR_TEKRAM_NVRAM:
4217 np->myaddr = nvram->data.Tekram.host_id & 0x0f;
4218 break;
4219 case SCSI_NCR_SYMBIOS_NVRAM:
4220 if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
4221 np->rv_scntl0 &= ~0x0a;
4222 np->myaddr = nvram->data.Symbios.host_id & 0x0f;
4223 if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
4224 np->verbose += 1;
4225 break;
4228 #endif
4230 ** Get SCSI addr of host adapter (set by bios?).
4232 if (!np->myaddr) np->myaddr = INB(nc_scid) & 0x07;
4233 if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
4236 #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
4239 * Prepare initial io register bits for burst length
4241 ncr_init_burst(np, burst_max);
4244 ** Set differential mode and LED support.
4245 ** Ignore these features for boards known to use a
4246 ** specific GPIO wiring (Tekram only for now).
4247 ** Probe initial setting of GPREG and GPCNTL for
4248 ** other ones.
4250 if (!nvram || nvram->type != SCSI_NCR_TEKRAM_NVRAM) {
4251 switch(driver_setup.diff_support) {
4252 case 3:
4253 if (INB(nc_gpreg) & 0x08)
4254 break;
4255 case 2:
4256 np->rv_stest2 |= 0x20;
4257 break;
4258 case 1:
4259 np->rv_stest2 |= (np->sv_stest2 & 0x20);
4260 break;
4261 default:
4262 break;
4265 if ((driver_setup.led_pin ||
4266 (nvram && nvram->type == SCSI_NCR_SYMBIOS_NVRAM)) &&
4267 !(np->sv_gpcntl & 0x01))
4268 np->features |= FE_LED0;
4271 ** Set irq mode.
4273 switch(driver_setup.irqm & 3) {
4274 case 2:
4275 np->rv_dcntl |= IRQM;
4276 break;
4277 case 1:
4278 np->rv_dcntl |= (np->sv_dcntl & IRQM);
4279 break;
4280 default:
4281 break;
4285 ** Configure targets according to driver setup.
4286 ** If NVRAM present get targets setup from NVRAM.
4287 ** Allow to override sync, wide and NOSCAN from
4288 ** boot command line.
4290 for (i = 0 ; i < MAX_TARGET ; i++) {
4291 tcb_p tp = &np->target[i];
4293 tp->usrsync = 255;
4294 #ifdef SCSI_NCR_NVRAM_SUPPORT
4295 if (nvram) {
4296 switch(nvram->type) {
4297 case SCSI_NCR_TEKRAM_NVRAM:
4298 ncr_Tekram_setup_target(np, i, &nvram->data.Tekram);
4299 break;
4300 case SCSI_NCR_SYMBIOS_NVRAM:
4301 ncr_Symbios_setup_target(np, i, &nvram->data.Symbios);
4302 break;
4304 if (driver_setup.use_nvram & 0x2)
4305 tp->usrsync = driver_setup.default_sync;
4306 if (driver_setup.use_nvram & 0x4)
4307 tp->usrwide = driver_setup.max_wide;
4308 if (driver_setup.use_nvram & 0x8)
4309 tp->usrflag &= ~UF_NOSCAN;
4311 else {
4312 #else
4313 if (1) {
4314 #endif
4315 tp->usrsync = driver_setup.default_sync;
4316 tp->usrwide = driver_setup.max_wide;
4317 tp->usrtags = SCSI_NCR_MAX_TAGS;
4318 if (!driver_setup.disconnection)
4319 np->target[i].usrflag = UF_NODISC;
4324 ** Announce all that stuff to user.
4327 i = nvram ? nvram->type : 0;
4328 printk(KERN_INFO "%s: %sID %d, Fast-%d%s%s\n", ncr_name(np),
4329 i == SCSI_NCR_SYMBIOS_NVRAM ? "Symbios format NVRAM, " :
4330 (i == SCSI_NCR_TEKRAM_NVRAM ? "Tekram format NVRAM, " : ""),
4331 np->myaddr,
4332 np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
4333 (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
4334 (np->rv_stest2 & 0x20) ? ", Differential" : "");
4336 if (bootverbose > 1) {
4337 printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4338 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4339 ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
4340 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
4342 printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4343 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4344 ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
4345 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
4348 if (bootverbose && np->paddr2)
4349 printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
4350 ncr_name(np), np->paddr2);
4352 return 0;
4356 #ifdef SCSI_NCR_DEBUG_NVRAM
4358 void __init ncr_display_Symbios_nvram(ncb_p np, Symbios_nvram *nvram)
4360 int i;
4362 /* display Symbios nvram host data */
4363 printk(KERN_DEBUG "%s: HOST ID=%d%s%s%s%s%s\n",
4364 ncr_name(np), nvram->host_id & 0x0f,
4365 (nvram->flags & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"",
4366 (nvram->flags & SYMBIOS_PARITY_ENABLE) ? " PARITY" :"",
4367 (nvram->flags & SYMBIOS_VERBOSE_MSGS) ? " VERBOSE" :"",
4368 (nvram->flags & SYMBIOS_CHS_MAPPING) ? " CHS_ALT" :"",
4369 (nvram->flags1 & SYMBIOS_SCAN_HI_LO) ? " HI_LO" :"");
4371 /* display Symbios nvram drive data */
4372 for (i = 0 ; i < 15 ; i++) {
4373 struct Symbios_target *tn = &nvram->target[i];
4374 printk(KERN_DEBUG "%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
4375 ncr_name(np), i,
4376 (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC" : "",
4377 (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT" : "",
4378 (tn->flags & SYMBIOS_SCAN_LUNS) ? " SCAN_LUNS" : "",
4379 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ" : "",
4380 tn->bus_width,
4381 tn->sync_period / 4,
4382 tn->timeout);
4386 static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
4388 void __init ncr_display_Tekram_nvram(ncb_p np, Tekram_nvram *nvram)
4390 int i, tags, boot_delay;
4391 char *rem;
4393 /* display Tekram nvram host data */
4394 tags = 2 << nvram->max_tags_index;
4395 boot_delay = 0;
4396 if (nvram->boot_delay_index < 6)
4397 boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
4398 switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
4399 default:
4400 case 0: rem = ""; break;
4401 case 1: rem = " REMOVABLE=boot device"; break;
4402 case 2: rem = " REMOVABLE=all"; break;
4405 printk(KERN_DEBUG
4406 "%s: HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
4407 ncr_name(np), nvram->host_id & 0x0f,
4408 (nvram->flags1 & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"",
4409 (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES" :"",
4410 (nvram->flags & TEKRAM_DRIVES_SUP_1GB) ? " >1GB" :"",
4411 (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET" :"",
4412 (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG" :"",
4413 (nvram->flags & TEKRAM_IMMEDIATE_SEEK) ? " IMM_SEEK" :"",
4414 (nvram->flags & TEKRAM_SCAN_LUNS) ? " SCAN_LUNS" :"",
4415 (nvram->flags1 & TEKRAM_F2_F6_ENABLED) ? " F2_F6" :"",
4416 rem, boot_delay, tags);
4418 /* display Tekram nvram drive data */
4419 for (i = 0; i <= 15; i++) {
4420 int sync, j;
4421 struct Tekram_target *tn = &nvram->target[i];
4422 j = tn->sync_index & 0xf;
4423 sync = j < 12 ? Tekram_sync[j] : 255;
4424 printk(KERN_DEBUG "%s-%d:%s%s%s%s%s%s PERIOD=%d\n",
4425 ncr_name(np), i,
4426 (tn->flags & TEKRAM_PARITY_CHECK) ? " PARITY" : "",
4427 (tn->flags & TEKRAM_SYNC_NEGO) ? " SYNC" : "",
4428 (tn->flags & TEKRAM_DISCONNECT_ENABLE) ? " DISC" : "",
4429 (tn->flags & TEKRAM_START_CMD) ? " START" : "",
4430 (tn->flags & TEKRAM_TAGGED_COMMANDS) ? " TCQ" : "",
4431 (tn->flags & TEKRAM_WIDE_NEGO) ? " WIDE" : "",
4432 sync);
4435 #endif /* SCSI_NCR_DEBUG_NVRAM */
4438 ** Host attach and initialisations.
4440 ** Allocate host data and ncb structure.
4441 ** Request IO region and remap MMIO region.
4442 ** Do chip initialization.
4443 ** If all is OK, install interrupt handling and
4444 ** start the timer daemon.
4447 static int __init
4448 ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device)
4450 struct host_data *host_data;
4451 ncb_p np;
4452 struct Scsi_Host *instance = 0;
4453 u_long flags = 0;
4454 ncr_nvram *nvram = device->nvram;
4455 int i;
4457 #ifdef __sparc__
4458 printk(KERN_INFO "ncr53c%s-%d: rev=0x%02x, base=0x%lx, io_port=0x%lx, irq=%s\n",
4459 device->chip.name, unit, device->chip.revision_id, device->slot.base,
4460 device->slot.io_port, __irq_itoa(device->slot.irq));
4461 #else
4462 printk(KERN_INFO "ncr53c%s-%d: rev=0x%02x, base=0x%lx, io_port=0x%lx, irq=%d\n",
4463 device->chip.name, unit, device->chip.revision_id, device->slot.base,
4464 device->slot.io_port, device->slot.irq);
4465 #endif
4468 ** Allocate host_data structure
4470 if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
4471 goto attach_error;
4474 ** Initialize structure.
4476 host_data = (struct host_data *) instance->hostdata;
4477 bzero (host_data, sizeof(*host_data));
4480 ** Align np and first ccb to 32 boundary for cache line
4481 ** bursting when copying the global header.
4483 np = (ncb_p) (((u_long) &host_data->_ncb_data) & CACHE_LINE_MASK);
4484 NCR_INIT_LOCK_NCB(np);
4485 host_data->ncb = np;
4486 np->ccb = (ccb_p) (((u_long) &host_data->_ccb_data) & CACHE_LINE_MASK);
4489 ** Store input informations in the host data structure.
4491 strncpy(np->chip_name, device->chip.name, sizeof(np->chip_name) - 1);
4492 np->unit = unit;
4493 np->verbose = driver_setup.verbose;
4494 sprintf(np->inst_name, "ncr53c%s-%d", np->chip_name, np->unit);
4495 np->device_id = device->chip.device_id;
4496 np->revision_id = device->chip.revision_id;
4497 np->features = device->chip.features;
4498 np->clock_divn = device->chip.nr_divisor;
4499 np->maxoffs = device->chip.offset_max;
4500 np->maxburst = device->chip.burst_max;
4502 np->script0 = (struct script *)
4503 (((u_long) &host_data->script_data) & CACHE_LINE_MASK);
4504 np->scripth0 = &host_data->scripth_data;
4507 ** Initialize timer structure
4510 init_timer(&np->timer);
4511 np->timer.data = (unsigned long) np;
4512 np->timer.function = ncr53c8xx_timeout;
4515 ** Try to map the controller chip to
4516 ** virtual and physical memory.
4519 np->paddr = device->slot.base;
4520 np->paddr2 = (np->features & FE_RAM)? device->slot.base_2 : 0;
4522 #ifndef NCR_IOMAPPED
4523 np->vaddr = remap_pci_mem((u_long) np->paddr, (u_long) 128);
4524 if (!np->vaddr) {
4525 printk(KERN_ERR
4526 "%s: can't map memory mapped IO region\n",ncr_name(np));
4527 goto attach_error;
4529 else
4530 if (bootverbose > 1)
4531 printk(KERN_INFO
4532 "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
4535 ** Make the controller's registers available.
4536 ** Now the INB INW INL OUTB OUTW OUTL macros
4537 ** can be used safely.
4540 np->reg = (struct ncr_reg*) np->vaddr;
4542 #endif /* !defined NCR_IOMAPPED */
4545 ** Try to map the controller chip into iospace.
4548 request_region(device->slot.io_port, 128, "ncr53c8xx");
4549 np->port = device->slot.io_port;
4551 #ifdef SCSI_NCR_NVRAM_SUPPORT
4552 if (nvram) {
4553 switch(nvram->type) {
4554 case SCSI_NCR_SYMBIOS_NVRAM:
4555 #ifdef SCSI_NCR_DEBUG_NVRAM
4556 ncr_display_Symbios_nvram(np, &nvram->data.Symbios);
4557 #endif
4558 break;
4559 case SCSI_NCR_TEKRAM_NVRAM:
4560 #ifdef SCSI_NCR_DEBUG_NVRAM
4561 ncr_display_Tekram_nvram(np, &nvram->data.Tekram);
4562 #endif
4563 break;
4564 default:
4565 nvram = 0;
4566 #ifdef SCSI_NCR_DEBUG_NVRAM
4567 printk(KERN_DEBUG "%s: NVRAM: None or invalid data.\n", ncr_name(np));
4568 #endif
4571 #endif
4574 ** Do chip dependent initialization.
4576 (void)ncr_prepare_setting(np, nvram);
4578 if (np->paddr2 && sizeof(struct script) > 4096) {
4579 np->paddr2 = 0;
4580 printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
4581 ncr_name(np));
4585 ** Fill Linux host instance structure
4587 instance->max_channel = 0;
4588 instance->max_id = np->maxwide ? 16 : 8;
4589 instance->max_lun = SCSI_NCR_MAX_LUN;
4590 #ifndef NCR_IOMAPPED
4591 instance->base = (char *) np->reg;
4592 #endif
4593 instance->irq = device->slot.irq;
4594 instance->unique_id = device->slot.io_port;
4595 instance->io_port = device->slot.io_port;
4596 instance->n_io_port = 128;
4597 instance->dma_channel = 0;
4598 instance->select_queue_depths = ncr53c8xx_select_queue_depths;
4601 ** Patch script to physical addresses
4603 ncr_script_fill (&script0, &scripth0);
4605 np->scripth = np->scripth0;
4606 np->p_scripth = vtophys(np->scripth);
4608 np->p_script = (np->paddr2) ? bus_dvma_to_mem(np->paddr2) : vtophys(np->script0);
4610 ncr_script_copy_and_bind (np, (ncrcmd *) &script0, (ncrcmd *) np->script0, sizeof(struct script));
4611 ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0, (ncrcmd *) np->scripth0, sizeof(struct scripth));
4612 np->ccb->p_ccb = vtophys (np->ccb);
4615 ** Patch the script for LED support.
4618 if (np->features & FE_LED0) {
4619 np->script0->idle[0] =
4620 cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
4621 np->script0->reselected[0] =
4622 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
4623 np->script0->start[0] =
4624 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
4628 ** Look for the target control block of this nexus.
4629 ** For i = 0 to 3
4630 ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
4632 for (i = 0 ; i < 4 ; i++) {
4633 np->jump_tcb[i].l_cmd =
4634 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
4635 np->jump_tcb[i].l_paddr =
4636 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
4640 ** Reset chip.
4643 OUTB (nc_istat, SRST);
4644 UDELAY (100);
4645 OUTB (nc_istat, 0 );
4648 ** Now check the cache handling of the pci chipset.
4651 if (ncr_snooptest (np)) {
4652 printk (KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
4653 goto attach_error;
4657 ** Install the interrupt handler.
4660 if (request_irq(device->slot.irq, ncr53c8xx_intr,
4661 ((driver_setup.irqm & 0x10) ? 0 : SA_SHIRQ) |
4662 #if LINUX_VERSION_CODE < LinuxVersionCode(2,2,0)
4663 ((driver_setup.irqm & 0x20) ? 0 : SA_INTERRUPT),
4664 #else
4666 #endif
4667 "ncr53c8xx", np)) {
4668 #ifdef __sparc__
4669 printk(KERN_ERR "%s: request irq %s failure\n",
4670 ncr_name(np), __irq_itoa(device->slot.irq));
4671 #else
4672 printk(KERN_ERR "%s: request irq %d failure\n",
4673 ncr_name(np), device->slot.irq);
4674 #endif
4675 goto attach_error;
4678 np->irq = device->slot.irq;
4681 ** Initialize the fixed part of the default ccb.
4683 ncr_init_ccb(np, np->ccb);
4686 ** After SCSI devices have been opened, we cannot
4687 ** reset the bus safely, so we do it here.
4688 ** Interrupt handler does the real work.
4689 ** Process the reset exception,
4690 ** if interrupts are not enabled yet.
4691 ** Then enable disconnects.
4693 NCR_LOCK_NCB(np, flags);
4694 if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
4695 printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
4697 NCR_UNLOCK_NCB(np, flags);
4698 goto attach_error;
4700 ncr_exception (np);
4702 np->disc = 1;
4705 ** The middle-level SCSI driver does not
4706 ** wait for devices to settle.
4707 ** Wait synchronously if more than 2 seconds.
4709 if (driver_setup.settle_delay > 2) {
4710 printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
4711 ncr_name(np), driver_setup.settle_delay);
4712 MDELAY (1000 * driver_setup.settle_delay);
4716 ** Now let the generic SCSI driver
4717 ** look for the SCSI devices on the bus ..
4721 ** start the timeout daemon
4723 np->lasttime=0;
4724 ncr_timeout (np);
4727 ** use SIMPLE TAG messages by default
4729 #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
4730 np->order = M_SIMPLE_TAG;
4731 #endif
4734 ** Done.
4736 if (!the_template) {
4737 the_template = instance->hostt;
4738 first_host = instance;
4741 NCR_UNLOCK_NCB(np, flags);
4743 return 0;
4745 attach_error:
4746 if (!instance) return -1;
4747 printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
4748 #ifndef NCR_IOMAPPED
4749 if (np->vaddr) {
4750 #ifdef DEBUG_NCR53C8XX
4751 printk(KERN_DEBUG "%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
4752 #endif
4753 unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
4755 #endif /* !NCR_IOMAPPED */
4756 if (np->port) {
4757 #ifdef DEBUG_NCR53C8XX
4758 printk(KERN_DEBUG "%s: releasing IO region %x[%d]\n", ncr_name(np), np->port, 128);
4759 #endif
4760 release_region(np->port, 128);
4762 if (np->irq) {
4763 #ifdef DEBUG_NCR53C8XX
4764 #ifdef __sparc__
4765 printk(KERN_INFO "%s: freeing irq %s\n", ncr_name(np),
4766 __irq_itoa(np->irq));
4767 #else
4768 printk(KERN_INFO "%s: freeing irq %d\n", ncr_name(np), np->irq);
4769 #endif
4770 #endif
4771 free_irq(np->irq, np);
4773 scsi_unregister(instance);
4775 return -1;
4779 /*==========================================================
4782 ** Done SCSI commands list management.
4784 ** We donnot enter the scsi_done() callback immediately
4785 ** after a command has been seen as completed but we
4786 ** insert it into a list which is flushed outside any kind
4787 ** of driver critical section.
4788 ** This allows to do minimal stuff under interrupt and
4789 ** inside critical sections and to also avoid locking up
4790 ** on recursive calls to driver entry points under SMP.
4791 ** In fact, the only kernel point which is entered by the
4792 ** driver with a driver lock set is kmalloc(GFP_ATOMIC)
4793 ** that shall not reenter the driver under any circumstances,
4794 ** AFAIK.
4796 **==========================================================
4798 static inline void ncr_queue_done_cmd(ncb_p np, Scsi_Cmnd *cmd)
4800 cmd->host_scribble = (char *) np->done_list;
4801 np->done_list = cmd;
4804 static inline void ncr_flush_done_cmds(Scsi_Cmnd *lcmd)
4806 Scsi_Cmnd *cmd;
4808 while (lcmd) {
4809 cmd = lcmd;
4810 lcmd = (Scsi_Cmnd *) cmd->host_scribble;
4811 cmd->scsi_done(cmd);
4816 /*==========================================================
4819 ** Start execution of a SCSI command.
4820 ** This is called from the generic SCSI driver.
4823 **==========================================================
4825 static int ncr_queue_command (ncb_p np, Scsi_Cmnd *cmd)
4827 /* Scsi_Device *device = cmd->device; */
4828 tcb_p tp = &np->target[cmd->target];
4829 lcb_p lp = tp->lp[cmd->lun];
4830 ccb_p cp;
4832 int segments;
4833 u_char nego, idmsg, *msgptr;
4834 u_int msglen;
4835 int direction;
4836 u_int32 lastp, goalp;
4838 /*---------------------------------------------
4840 ** Some shortcuts ...
4842 **---------------------------------------------
4844 if ((cmd->target == np->myaddr ) ||
4845 (cmd->target >= MAX_TARGET) ||
4846 (cmd->lun >= MAX_LUN )) {
4847 return(DID_BAD_TARGET);
4850 /*---------------------------------------------
4852 ** Complete the 1st TEST UNIT READY command
4853 ** with error condition if the device is
4854 ** flagged NOSCAN, in order to speed up
4855 ** the boot.
4857 **---------------------------------------------
4859 if (cmd->cmnd[0] == 0 && (tp->usrflag & UF_NOSCAN)) {
4860 tp->usrflag &= ~UF_NOSCAN;
4861 return DID_BAD_TARGET;
4864 if (DEBUG_FLAGS & DEBUG_TINY) {
4865 PRINT_ADDR(cmd);
4866 printk ("CMD=%x ", cmd->cmnd[0]);
4869 /*---------------------------------------------------
4871 ** Assign a ccb / bind cmd.
4872 ** If resetting, shorten settle_time if necessary
4873 ** in order to avoid spurious timeouts.
4874 ** If resetting or no free ccb,
4875 ** insert cmd into the waiting list.
4877 **----------------------------------------------------
4879 if (np->settle_time && cmd->timeout_per_command >= HZ &&
4880 np->settle_time > jiffies + cmd->timeout_per_command - HZ) {
4881 np->settle_time = jiffies + cmd->timeout_per_command - HZ;
4884 if (np->settle_time || !(cp=ncr_get_ccb (np, cmd->target, cmd->lun))) {
4885 insert_into_waiting_list(np, cmd);
4886 return(DID_OK);
4888 cp->cmd = cmd;
4890 /*---------------------------------------------------
4892 ** Enable tagged queue if asked by scsi ioctl
4894 **----------------------------------------------------
4896 #if 0 /* This stuff was only usefull for linux-1.2.13 */
4897 if (lp && !lp->numtags && cmd->device && cmd->device->tagged_queue) {
4898 lp->numtags = tp->usrtags;
4899 ncr_setup_tags (np, cmd->target, cmd->lun);
4901 #endif
4903 /*---------------------------------------------------
4905 ** timestamp
4907 **----------------------------------------------------
4909 #ifdef SCSI_NCR_PROFILE_SUPPORT
4910 bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
4911 cp->phys.header.stamp.start = jiffies;
4912 #endif
4914 /*---------------------------------------------------
4916 ** negotiation required?
4918 **---------------------------------------------------
4921 nego = 0;
4923 if ((!tp->widedone || !tp->period) && !tp->nego_cp && tp->inq_done && lp) {
4926 ** negotiate wide transfers ?
4929 if (!tp->widedone) {
4930 if (tp->inq_byte7 & INQ7_WIDE16) {
4931 nego = NS_WIDE;
4932 } else
4933 tp->widedone=1;
4937 ** negotiate synchronous transfers?
4940 if (!nego && !tp->period) {
4941 if (tp->inq_byte7 & INQ7_SYNC) {
4942 nego = NS_SYNC;
4943 } else {
4944 tp->period =0xffff;
4945 PRINT_TARGET(np, cmd->target);
4946 printk ("device did not report SYNC.\n");
4951 ** remember nego is pending for the target.
4952 ** Avoid to start a nego for all queued commands
4953 ** when tagged command queuing is enabled.
4956 if (nego)
4957 tp->nego_cp = cp;
4960 /*----------------------------------------------------
4962 ** Build the identify / tag / sdtr message
4964 **----------------------------------------------------
4967 idmsg = M_IDENTIFY | cmd->lun;
4969 if (cp ->tag != NO_TAG ||
4970 (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
4971 idmsg |= 0x40;
4973 msgptr = cp->scsi_smsg;
4974 msglen = 0;
4975 msgptr[msglen++] = idmsg;
4977 if (cp->tag != NO_TAG) {
4978 char order = np->order;
4981 ** Force ordered tag if necessary to avoid timeouts
4982 ** and to preserve interactivity.
4984 if (lp && lp->tags_stime + (3*HZ) <= jiffies) {
4985 if (lp->tags_smap) {
4986 order = M_ORDERED_TAG;
4987 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
4988 PRINT_ADDR(cmd);
4989 printk("ordered tag forced.\n");
4992 lp->tags_stime = jiffies;
4993 lp->tags_smap = lp->tags_umap;
4996 if (order == 0) {
4998 ** Ordered write ops, unordered read ops.
5000 switch (cmd->cmnd[0]) {
5001 case 0x08: /* READ_SMALL (6) */
5002 case 0x28: /* READ_BIG (10) */
5003 case 0xa8: /* READ_HUGE (12) */
5004 order = M_SIMPLE_TAG;
5005 break;
5006 default:
5007 order = M_ORDERED_TAG;
5010 msgptr[msglen++] = order;
5012 ** Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
5013 ** since we may have to deal with devices that have
5014 ** problems with #TAG 0 or too great #TAG numbers.
5016 msgptr[msglen++] = (cp->tag << 1) + 1;
5019 switch (nego) {
5020 case NS_SYNC:
5021 msgptr[msglen++] = M_EXTENDED;
5022 msgptr[msglen++] = 3;
5023 msgptr[msglen++] = M_X_SYNC_REQ;
5024 msgptr[msglen++] = tp->maxoffs ? tp->minsync : 0;
5025 msgptr[msglen++] = tp->maxoffs;
5026 if (DEBUG_FLAGS & DEBUG_NEGO) {
5027 PRINT_ADDR(cp->cmd);
5028 printk ("sync msgout: ");
5029 ncr_show_msg (&cp->scsi_smsg [msglen-5]);
5030 printk (".\n");
5032 break;
5033 case NS_WIDE:
5034 msgptr[msglen++] = M_EXTENDED;
5035 msgptr[msglen++] = 2;
5036 msgptr[msglen++] = M_X_WIDE_REQ;
5037 msgptr[msglen++] = tp->usrwide;
5038 if (DEBUG_FLAGS & DEBUG_NEGO) {
5039 PRINT_ADDR(cp->cmd);
5040 printk ("wide msgout: ");
5041 ncr_show_msg (&cp->scsi_smsg [msglen-4]);
5042 printk (".\n");
5044 break;
5047 /*----------------------------------------------------
5049 ** Build the data descriptors
5051 **----------------------------------------------------
5054 segments = ncr_scatter (cp, cp->cmd);
5056 if (segments < 0) {
5057 ncr_free_ccb(np, cp);
5058 return(DID_ERROR);
5061 /*----------------------------------------------------
5063 ** Guess xfer direction.
5064 ** Spare some CPU by testing here frequently opcode.
5066 **----------------------------------------------------
5068 if (!cp->data_len)
5069 direction = 0;
5070 else {
5071 switch((int) cmd->cmnd[0]) {
5072 case 0x08: /* READ(6) 08 */
5073 case 0x28: /* READ(10) 28 */
5074 case 0xA8: /* READ(12) A8 */
5075 direction = XFER_IN;
5076 break;
5077 case 0x0A: /* WRITE(6) 0A */
5078 case 0x2A: /* WRITE(10) 2A */
5079 case 0xAA: /* WRITE(12) AA */
5080 direction = XFER_OUT;
5081 break;
5082 default:
5083 direction = (XFER_IN|XFER_OUT);
5084 break;
5088 /*----------------------------------------------------
5090 ** Set the SAVED_POINTER.
5092 **----------------------------------------------------
5096 ** Default to no data transfer.
5098 lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
5101 ** Compute data out pointers, if needed.
5103 if (direction & XFER_OUT) {
5104 goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
5105 if (segments <= MAX_SCATTERL)
5106 lastp = goalp - 8 - (segments * 16);
5107 else {
5108 lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
5109 lastp -= (segments - MAX_SCATTERL) * 16;
5112 ** If actual data direction is unknown, save pointers
5113 ** in header. The SCRIPTS will swap them to current
5114 ** if target decision will be data out.
5116 if (direction & XFER_IN) {
5117 cp->phys.header.wgoalp = cpu_to_scr(goalp);
5118 cp->phys.header.wlastp = cpu_to_scr(lastp);
5123 ** Compute data in pointers, if needed.
5125 if (direction & XFER_IN) {
5126 goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
5127 if (segments <= MAX_SCATTERL)
5128 lastp = goalp - 8 - (segments * 16);
5129 else {
5130 lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
5131 lastp -= (segments - MAX_SCATTERL) * 16;
5136 ** Set all pointers values needed by SCRIPTS.
5137 ** If direction is unknown, start at data_io.
5139 cp->phys.header.lastp = cpu_to_scr(lastp);
5140 cp->phys.header.goalp = cpu_to_scr(goalp);
5142 if ((direction & (XFER_IN|XFER_OUT)) == (XFER_IN|XFER_OUT))
5143 cp->phys.header.savep =
5144 cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
5145 else
5146 cp->phys.header.savep= cpu_to_scr(lastp);
5149 ** Save the initial data pointer in order to be able
5150 ** to redo the command.
5152 cp->startp = cp->phys.header.savep;
5154 /*----------------------------------------------------
5156 ** fill in ccb
5158 **----------------------------------------------------
5161 ** physical -> virtual backlink
5162 ** Generic SCSI command
5166 ** Startqueue
5168 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5169 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
5171 ** select
5173 cp->phys.select.sel_id = cmd->target;
5174 cp->phys.select.sel_scntl3 = tp->wval;
5175 cp->phys.select.sel_sxfer = tp->sval;
5177 ** message
5179 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
5180 cp->phys.smsg.size = cpu_to_scr(msglen);
5183 ** command
5185 cp->phys.cmd.addr = cpu_to_scr(vtophys (&cmd->cmnd[0]));
5186 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
5189 ** status
5191 cp->actualquirks = tp->quirks;
5192 cp->host_status = nego ? HS_NEGOTIATE : HS_BUSY;
5193 cp->scsi_status = S_ILLEGAL;
5194 cp->parity_status = 0;
5196 cp->xerr_status = XE_OK;
5197 cp->nego_status = nego;
5198 #if 0
5199 cp->sync_status = tp->sval;
5200 cp->wide_status = tp->wval;
5201 #endif
5203 /*----------------------------------------------------
5205 ** Critical region: start this job.
5207 **----------------------------------------------------
5211 ** activate this job.
5214 /* Compute a time limit greater than the middle-level driver one */
5215 if (cmd->timeout_per_command > 0)
5216 cp->tlimit = jiffies + cmd->timeout_per_command + HZ;
5217 else
5218 cp->tlimit = jiffies + 86400 * HZ;/* No timeout=24 hours */
5219 cp->magic = CCB_MAGIC;
5222 ** insert next CCBs into start queue.
5223 ** 2 max at a time is enough to flush the CCB wait queue.
5225 cp->auto_sense = 0;
5226 if (lp)
5227 ncr_start_next_ccb(np, lp, 2);
5228 else
5229 ncr_put_start_queue(np, cp);
5232 ** Command is successfully queued.
5235 return(DID_OK);
5239 /*==========================================================
5242 ** Insert a CCB into the start queue and wake up the
5243 ** SCRIPTS processor.
5246 **==========================================================
5249 static void ncr_start_next_ccb(ncb_p np, lcb_p lp, int maxn)
5251 XPT_QUEHEAD *qp;
5252 ccb_p cp;
5254 if (lp->held_ccb)
5255 return;
5257 while (maxn-- && lp->queuedccbs < lp->queuedepth) {
5258 qp = xpt_remque_head(&lp->wait_ccbq);
5259 if (!qp)
5260 break;
5261 ++lp->queuedccbs;
5262 cp = xpt_que_entry(qp, struct ccb, link_ccbq);
5263 xpt_insque_tail(qp, &lp->busy_ccbq);
5264 lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
5265 cpu_to_scr(CCB_PHYS (cp, restart));
5266 ncr_put_start_queue(np, cp);
5270 static void ncr_put_start_queue(ncb_p np, ccb_p cp)
5272 u_short qidx;
5275 ** insert into start queue.
5277 if (!np->squeueput) np->squeueput = 1;
5278 qidx = np->squeueput + 2;
5279 if (qidx >= MAX_START + MAX_START) qidx = 1;
5281 np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5282 MEMORY_BARRIER();
5283 np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
5285 np->squeueput = qidx;
5286 ++np->queuedccbs;
5287 cp->queued = 1;
5289 if (DEBUG_FLAGS & DEBUG_QUEUE)
5290 printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
5293 ** Script processor may be waiting for reselect.
5294 ** Wake it up.
5296 MEMORY_BARRIER();
5297 OUTB (nc_istat, SIGP);
5301 /*==========================================================
5304 ** Start reset process.
5305 ** If reset in progress do nothing.
5306 ** The interrupt handler will reinitialize the chip.
5307 ** The timeout handler will wait for settle_time before
5308 ** clearing it and so resuming command processing.
5311 **==========================================================
5313 static void ncr_start_reset(ncb_p np)
5315 if (!np->settle_time) {
5316 (void) ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
5320 static int ncr_reset_scsi_bus(ncb_p np, int enab_int, int settle_delay)
5322 u_int32 term;
5323 int retv = 0;
5325 np->settle_time = jiffies + settle_delay * HZ;
5327 if (bootverbose > 1)
5328 printk("%s: resetting, "
5329 "command processing suspended for %d seconds\n",
5330 ncr_name(np), settle_delay);
5332 OUTB (nc_istat, SRST);
5333 UDELAY (100);
5334 OUTB (nc_istat, 0);
5335 UDELAY (2000); /* The 895 needs time for the bus mode to settle */
5336 if (enab_int)
5337 OUTW (nc_sien, RST);
5339 ** Enable Tolerant, reset IRQD if present and
5340 ** properly set IRQ mode, prior to resetting the bus.
5342 OUTB (nc_stest3, TE);
5343 OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
5344 OUTB (nc_scntl1, CRST);
5345 UDELAY (200);
5347 if (!driver_setup.bus_check)
5348 goto out;
5350 ** Check for no terminators or SCSI bus shorts to ground.
5351 ** Read SCSI data bus, data parity bits and control signals.
5352 ** We are expecting RESET to be TRUE and other signals to be
5353 ** FALSE.
5355 term = INB(nc_sstat0); /* rst, sdp0 */
5356 term = ((term & 2) << 7) + ((term & 1) << 16);
5357 term |= ((INB(nc_sstat2) & 0x01) << 25) | /* sdp1 */
5358 (INW(nc_sbdl) << 9) | /* d15-0 */
5359 INB(nc_sbcl); /* req, ack, bsy, sel, atn, msg, cd, io */
5361 if (!(np->features & FE_WIDE))
5362 term &= 0x3ffff;
5364 if (term != (2<<7)) {
5365 printk("%s: suspicious SCSI data while resetting the BUS.\n",
5366 ncr_name(np));
5367 printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
5368 "0x%lx, expecting 0x%lx\n",
5369 ncr_name(np),
5370 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
5371 (u_long)term, (u_long)(2<<7));
5372 if (driver_setup.bus_check == 1)
5373 retv = 1;
5375 out:
5376 OUTB (nc_scntl1, 0);
5377 return retv;
5380 /*==========================================================
5383 ** Reset the SCSI BUS.
5384 ** This is called from the generic SCSI driver.
5387 **==========================================================
5389 static int ncr_reset_bus (ncb_p np, Scsi_Cmnd *cmd, int sync_reset)
5391 /* Scsi_Device *device = cmd->device; */
5392 ccb_p cp;
5393 int found;
5396 * Return immediately if reset is in progress.
5398 if (np->settle_time) {
5399 return SCSI_RESET_PUNT;
5402 * Start the reset process.
5403 * The script processor is then assumed to be stopped.
5404 * Commands will now be queued in the waiting list until a settle
5405 * delay of 2 seconds will be completed.
5407 ncr_start_reset(np);
5409 * First, look in the wakeup list
5411 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
5413 ** look for the ccb of this command.
5415 if (cp->host_status == HS_IDLE) continue;
5416 if (cp->cmd == cmd) {
5417 found = 1;
5418 break;
5422 * Then, look in the waiting list
5424 if (!found && retrieve_from_waiting_list(0, np, cmd))
5425 found = 1;
5427 * Wake-up all awaiting commands with DID_RESET.
5429 reset_waiting_list(np);
5431 * Wake-up all pending commands with HS_RESET -> DID_RESET.
5433 ncr_wakeup(np, HS_RESET);
5435 * If the involved command was not in a driver queue, and the
5436 * scsi driver told us reset is synchronous, and the command is not
5437 * currently in the waiting list, complete it with DID_RESET status,
5438 * in order to keep it alive.
5440 if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
5441 cmd->result = ScsiResult(DID_RESET, 0);
5442 ncr_queue_done_cmd(np, cmd);
5445 return SCSI_RESET_SUCCESS;
5448 /*==========================================================
5451 ** Abort an SCSI command.
5452 ** This is called from the generic SCSI driver.
5455 **==========================================================
5457 static int ncr_abort_command (ncb_p np, Scsi_Cmnd *cmd)
5459 /* Scsi_Device *device = cmd->device; */
5460 ccb_p cp;
5461 int found;
5462 int retv;
5465 * First, look for the scsi command in the waiting list
5467 if (remove_from_waiting_list(np, cmd)) {
5468 cmd->result = ScsiResult(DID_ABORT, 0);
5469 ncr_queue_done_cmd(np, cmd);
5470 return SCSI_ABORT_SUCCESS;
5474 * Then, look in the wakeup list
5476 for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
5478 ** look for the ccb of this command.
5480 if (cp->host_status == HS_IDLE) continue;
5481 if (cp->cmd == cmd) {
5482 found = 1;
5483 break;
5487 if (!found) {
5488 return SCSI_ABORT_NOT_RUNNING;
5491 if (np->settle_time) {
5492 return SCSI_ABORT_SNOOZE;
5496 ** If the CCB is active, patch schedule jumps for the
5497 ** script to abort the command.
5500 cp->tlimit = 0;
5501 switch(cp->host_status) {
5502 case HS_BUSY:
5503 case HS_NEGOTIATE:
5504 printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
5505 cp->start.schedule.l_paddr =
5506 cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
5507 retv = SCSI_ABORT_PENDING;
5508 break;
5509 case HS_DISCONNECT:
5510 cp->restart.schedule.l_paddr =
5511 cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
5512 retv = SCSI_ABORT_PENDING;
5513 break;
5514 default:
5515 retv = SCSI_ABORT_NOT_RUNNING;
5516 break;
5521 ** If there are no requests, the script
5522 ** processor will sleep on SEL_WAIT_RESEL.
5523 ** Let's wake it up, since it may have to work.
5525 OUTB (nc_istat, SIGP);
5527 return retv;
5530 /*==========================================================
5532 ** Linux release module stuff.
5534 ** Called before unloading the module
5535 ** Detach the host.
5536 ** We have to free resources and halt the NCR chip
5538 **==========================================================
5541 #ifdef MODULE
5542 static int ncr_detach(ncb_p np)
5544 ccb_p cp;
5545 tcb_p tp;
5546 lcb_p lp;
5547 int target, lun;
5548 int i;
5550 printk("%s: releasing host resources\n", ncr_name(np));
5553 ** Stop the ncr_timeout process
5554 ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
5557 #ifdef DEBUG_NCR53C8XX
5558 printk("%s: stopping the timer\n", ncr_name(np));
5559 #endif
5560 np->release_stage = 1;
5561 for (i = 50 ; i && np->release_stage != 2 ; i--) MDELAY (100);
5562 if (np->release_stage != 2)
5563 printk("%s: the timer seems to be already stopped\n", ncr_name(np));
5564 else np->release_stage = 2;
5567 ** Disable chip interrupts
5570 #ifdef DEBUG_NCR53C8XX
5571 printk("%s: disabling chip interrupts\n", ncr_name(np));
5572 #endif
5573 OUTW (nc_sien , 0);
5574 OUTB (nc_dien , 0);
5577 ** Free irq
5580 #ifdef DEBUG_NCR53C8XX
5581 #ifdef __sparc__
5582 printk("%s: freeing irq %s\n", ncr_name(np), __irq_itoa(np->irq));
5583 #else
5584 printk("%s: freeing irq %d\n", ncr_name(np), np->irq);
5585 #endif
5586 #endif
5587 free_irq(np->irq, np);
5590 ** Reset NCR chip
5591 ** Restore bios setting for automatic clock detection.
5594 printk("%s: resetting chip\n", ncr_name(np));
5595 OUTB (nc_istat, SRST);
5596 UDELAY (100);
5597 OUTB (nc_istat, 0 );
5599 OUTB(nc_dmode, np->sv_dmode);
5600 OUTB(nc_dcntl, np->sv_dcntl);
5601 OUTB(nc_ctest3, np->sv_ctest3);
5602 OUTB(nc_ctest4, np->sv_ctest4);
5603 OUTB(nc_ctest5, np->sv_ctest5);
5604 OUTB(nc_gpcntl, np->sv_gpcntl);
5605 OUTB(nc_stest2, np->sv_stest2);
5607 ncr_selectclock(np, np->sv_scntl3);
5610 ** Release Memory mapped IO region and IO mapped region
5613 #ifndef NCR_IOMAPPED
5614 #ifdef DEBUG_NCR53C8XX
5615 printk("%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
5616 #endif
5617 unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
5618 #endif /* !NCR_IOMAPPED */
5620 #ifdef DEBUG_NCR53C8XX
5621 printk("%s: releasing IO region %x[%d]\n", ncr_name(np), np->port, 128);
5622 #endif
5623 release_region(np->port, 128);
5626 ** Free allocated ccb(s)
5629 while ((cp=np->ccb->link_ccb) != NULL) {
5630 np->ccb->link_ccb = cp->link_ccb;
5631 if (cp->host_status) {
5632 printk("%s: shall free an active ccb (host_status=%d)\n",
5633 ncr_name(np), cp->host_status);
5635 #ifdef DEBUG_NCR53C8XX
5636 printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
5637 #endif
5638 m_free(cp, sizeof(*cp));
5642 ** Free allocated tp(s)
5645 for (target = 0; target < MAX_TARGET ; target++) {
5646 tp=&np->target[target];
5647 for (lun = 0 ; lun < MAX_LUN ; lun++) {
5648 lp = tp->lp[lun];
5649 if (lp) {
5650 #ifdef DEBUG_NCR53C8XX
5651 printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
5652 #endif
5653 if (lp->jump_ccb != &lp->jump_ccb_0)
5654 m_free(lp->jump_ccb, 256);
5655 m_free(lp, sizeof(*lp));
5660 printk("%s: host resources successfully released\n", ncr_name(np));
5662 return 1;
5664 #endif
5666 /*==========================================================
5669 ** Complete execution of a SCSI command.
5670 ** Signal completion to the generic SCSI driver.
5673 **==========================================================
5676 void ncr_complete (ncb_p np, ccb_p cp)
5678 Scsi_Cmnd *cmd;
5679 tcb_p tp;
5680 lcb_p lp;
5683 ** Sanity check
5686 if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
5687 return;
5690 ** timestamp
5691 ** Optional, spare some CPU time
5693 #ifdef SCSI_NCR_PROFILE_SUPPORT
5694 ncb_profile (np, cp);
5695 #endif
5697 if (DEBUG_FLAGS & DEBUG_TINY)
5698 printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp & 0xfff,
5699 cp->host_status,cp->scsi_status);
5702 ** Get command, target and lun pointers.
5705 cmd = cp->cmd;
5706 cp->cmd = NULL;
5707 tp = &np->target[cmd->target];
5708 lp = tp->lp[cmd->lun];
5711 ** We donnot queue more than 1 ccb per target
5712 ** with negotiation at any time. If this ccb was
5713 ** used for negotiation, clear this info in the tcb.
5716 if (cp == tp->nego_cp)
5717 tp->nego_cp = 0;
5720 ** If auto-sense performed, change scsi status.
5722 if (cp->auto_sense) {
5723 cp->scsi_status = cp->auto_sense;
5727 ** If we were recovering from queue full or performing
5728 ** auto-sense, requeue skipped CCBs to the wait queue.
5731 if (lp && lp->held_ccb) {
5732 if (cp == lp->held_ccb) {
5733 xpt_que_splice(&lp->skip_ccbq, &lp->wait_ccbq);
5734 xpt_que_init(&lp->skip_ccbq);
5735 lp->held_ccb = 0;
5740 ** Check for parity errors.
5743 if (cp->parity_status > 1) {
5744 PRINT_ADDR(cmd);
5745 printk ("%d parity error(s).\n",cp->parity_status);
5749 ** Check for extended errors.
5752 if (cp->xerr_status != XE_OK) {
5753 PRINT_ADDR(cmd);
5754 switch (cp->xerr_status) {
5755 case XE_EXTRA_DATA:
5756 printk ("extraneous data discarded.\n");
5757 break;
5758 case XE_BAD_PHASE:
5759 printk ("illegal scsi phase (4/5).\n");
5760 break;
5761 default:
5762 printk ("extended error %d.\n", cp->xerr_status);
5763 break;
5765 if (cp->host_status==HS_COMPLETE)
5766 cp->host_status = HS_FAIL;
5770 ** Print out any error for debugging purpose.
5772 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5773 if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
5774 PRINT_ADDR(cmd);
5775 printk ("ERROR: cmd=%x host_status=%x scsi_status=%x\n",
5776 cmd->cmnd[0], cp->host_status, cp->scsi_status);
5781 ** Check the status.
5783 if ( (cp->host_status == HS_COMPLETE)
5784 && (cp->scsi_status == S_GOOD ||
5785 cp->scsi_status == S_COND_MET)) {
5787 ** All went well (GOOD status).
5788 ** CONDITION MET status is returned on
5789 ** `Pre-Fetch' or `Search data' success.
5791 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5794 ** @RESID@
5795 ** Could dig out the correct value for resid,
5796 ** but it would be quite complicated.
5798 /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
5801 ** Allocate the lcb if not yet.
5803 if (!lp)
5804 ncr_alloc_lcb (np, cmd->target, cmd->lun);
5807 ** On standard INQUIRY response (EVPD and CmDt
5808 ** not set), setup logical unit according to
5809 ** announced capabilities (we need the 1rst 7 bytes).
5811 if (cmd->cmnd[0] == 0x12 && !(cmd->cmnd[1] & 0x3) &&
5812 cmd->cmnd[4] >= 7 && !cmd->use_sg) {
5813 ncr_setup_lcb (np, cmd->target, cmd->lun,
5814 (char *) cmd->request_buffer);
5817 tp->bytes += cp->data_len;
5818 tp->transfers ++;
5821 ** If tags was reduced due to queue full,
5822 ** increase tags if 1000 good status received.
5824 if (lp && lp->usetags && lp->numtags < lp->maxtags) {
5825 ++lp->num_good;
5826 if (lp->num_good >= 1000) {
5827 lp->num_good = 0;
5828 ++lp->numtags;
5829 ncr_setup_tags (np, cmd->target, cmd->lun);
5832 } else if ((cp->host_status == HS_COMPLETE)
5833 && (cp->scsi_status == S_CHECK_COND)) {
5835 ** Check condition code
5837 cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
5839 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5840 u_char * p = (u_char*) & cmd->sense_buffer;
5841 int i;
5842 PRINT_ADDR(cmd);
5843 printk ("sense data:");
5844 for (i=0; i<14; i++) printk (" %x", *p++);
5845 printk (".\n");
5848 } else if ((cp->host_status == HS_COMPLETE)
5849 && (cp->scsi_status == S_BUSY ||
5850 cp->scsi_status == S_QUEUE_FULL)) {
5853 ** Target is busy.
5855 cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5857 } else if ((cp->host_status == HS_SEL_TIMEOUT)
5858 || (cp->host_status == HS_TIMEOUT)) {
5861 ** No response
5863 cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
5865 } else if (cp->host_status == HS_RESET) {
5868 ** SCSI bus reset
5870 cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
5872 } else if (cp->host_status == HS_ABORTED) {
5875 ** Transfer aborted
5877 cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
5879 } else {
5882 ** Other protocol messes
5884 PRINT_ADDR(cmd);
5885 printk ("COMMAND FAILED (%x %x) @%p.\n",
5886 cp->host_status, cp->scsi_status, cp);
5888 cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
5892 ** trace output
5895 if (tp->usrflag & UF_TRACE) {
5896 u_char * p;
5897 int i;
5898 PRINT_ADDR(cmd);
5899 printk (" CMD:");
5900 p = (u_char*) &cmd->cmnd[0];
5901 for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
5903 if (cp->host_status==HS_COMPLETE) {
5904 switch (cp->scsi_status) {
5905 case S_GOOD:
5906 printk (" GOOD");
5907 break;
5908 case S_CHECK_COND:
5909 printk (" SENSE:");
5910 p = (u_char*) &cmd->sense_buffer;
5911 for (i=0; i<14; i++)
5912 printk (" %x", *p++);
5913 break;
5914 default:
5915 printk (" STAT: %x\n", cp->scsi_status);
5916 break;
5918 } else printk (" HOSTERROR: %x", cp->host_status);
5919 printk ("\n");
5923 ** Free this ccb
5925 ncr_free_ccb (np, cp);
5928 ** requeue awaiting scsi commands for this lun.
5930 if (lp && lp->queuedccbs < lp->queuedepth &&
5931 !xpt_que_empty(&lp->wait_ccbq))
5932 ncr_start_next_ccb(np, lp, 2);
5935 ** requeue awaiting scsi commands for this controller.
5937 if (np->waiting_list)
5938 requeue_waiting_list(np);
5941 ** signal completion to generic driver.
5943 ncr_queue_done_cmd(np, cmd);
5946 /*==========================================================
5949 ** Signal all (or one) control block done.
5952 **==========================================================
5956 ** This CCB has been skipped by the NCR.
5957 ** Queue it in the correponding unit queue.
5959 static void ncr_ccb_skipped(ncb_p np, ccb_p cp)
5961 tcb_p tp = &np->target[cp->target];
5962 lcb_p lp = tp->lp[cp->lun];
5964 if (lp && cp != np->ccb) {
5965 cp->host_status &= ~HS_SKIPMASK;
5966 cp->start.schedule.l_paddr =
5967 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5968 xpt_remque(&cp->link_ccbq);
5969 xpt_insque_tail(&cp->link_ccbq, &lp->skip_ccbq);
5970 if (cp->queued) {
5971 --lp->queuedccbs;
5974 if (cp->queued) {
5975 --np->queuedccbs;
5976 cp->queued = 0;
5981 ** The NCR has completed CCBs.
5982 ** Look at the DONE QUEUE if enabled, otherwise scan all CCBs
5984 void ncr_wakeup_done (ncb_p np)
5986 ccb_p cp;
5987 #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5988 int i, j;
5990 i = np->ccb_done_ic;
5991 while (1) {
5992 j = i+1;
5993 if (j >= MAX_DONE)
5994 j = 0;
5996 cp = np->ccb_done[j];
5997 if (!CCB_DONE_VALID(cp))
5998 break;
6000 np->ccb_done[j] = (ccb_p) CCB_DONE_EMPTY;
6001 np->scripth->done_queue[5*j + 4] =
6002 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
6003 MEMORY_BARRIER();
6004 np->scripth->done_queue[5*i + 4] =
6005 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
6007 if (cp->host_status & HS_DONEMASK)
6008 ncr_complete (np, cp);
6009 else if (cp->host_status & HS_SKIPMASK)
6010 ncr_ccb_skipped (np, cp);
6012 i = j;
6014 np->ccb_done_ic = i;
6015 #else
6016 cp = np->ccb;
6017 while (cp) {
6018 if (cp->host_status & HS_DONEMASK)
6019 ncr_complete (np, cp);
6020 else if (cp->host_status & HS_SKIPMASK)
6021 ncr_ccb_skipped (np, cp);
6022 cp = cp->link_ccb;
6024 #endif
6028 ** Complete all active CCBs.
6030 void ncr_wakeup (ncb_p np, u_long code)
6032 ccb_p cp = np->ccb;
6034 while (cp) {
6035 if (cp->host_status != HS_IDLE) {
6036 cp->host_status = code;
6037 ncr_complete (np, cp);
6039 cp = cp->link_ccb;
6043 /*==========================================================
6046 ** Start NCR chip.
6049 **==========================================================
6052 void ncr_init (ncb_p np, int reset, char * msg, u_long code)
6054 int i;
6057 ** Reset chip if asked, otherwise just clear fifos.
6060 if (reset) {
6061 OUTB (nc_istat, SRST);
6062 UDELAY (100);
6064 else {
6065 OUTB (nc_stest3, TE|CSF);
6066 OUTONB (nc_ctest3, CLF);
6070 ** Message.
6073 if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
6076 ** Clear Start Queue
6078 np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
6079 for (i = 1; i < MAX_START + MAX_START; i += 2)
6080 np->scripth0->tryloop[i] =
6081 cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
6084 ** Start at first entry.
6086 np->squeueput = 0;
6087 np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
6090 ** Clear Done Queue
6092 for (i = 0; i < MAX_DONE; i++) {
6093 np->ccb_done[i] = (ccb_p) CCB_DONE_EMPTY;
6094 np->scripth0->done_queue[5*i + 4] =
6095 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
6099 ** Start at first entry.
6101 np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
6102 np->ccb_done_ic = MAX_DONE-1;
6103 np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
6104 cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
6107 ** Wakeup all pending jobs.
6109 ncr_wakeup (np, code);
6112 ** Init chip.
6115 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort */
6116 UDELAY (2000); /* The 895 needs time for the bus mode to settle */
6118 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
6119 /* full arb., ena parity, par->ATN */
6120 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
6122 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
6124 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
6125 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
6126 OUTB (nc_istat , SIGP ); /* Signal Process */
6127 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
6128 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
6130 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
6131 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
6132 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
6134 OUTB (nc_stest2, EXT|np->rv_stest2); /* Extended Sreq/Sack filtering */
6135 OUTB (nc_stest3, TE); /* TolerANT enable */
6136 OUTB (nc_stime0, 0x0c ); /* HTH disabled STO 0.25 sec */
6139 ** Disable disconnects.
6142 np->disc = 0;
6145 ** Enable GPIO0 pin for writing if LED support.
6148 if (np->features & FE_LED0) {
6149 OUTOFFB (nc_gpcntl, 0x01);
6153 ** enable ints
6156 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
6157 OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
6160 ** For 895/6 enable SBMC interrupt and save current SCSI bus mode.
6162 if (np->features & FE_ULTRA2) {
6163 OUTONW (nc_sien, SBMC);
6164 np->scsi_mode = INB (nc_stest4) & SMODE;
6168 ** DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
6169 ** Disable overlapped arbitration.
6170 ** The 896 Rev 1 is also affected by this errata.
6172 if (np->device_id == PCI_DEVICE_ID_NCR_53C875 &&
6173 np->revision_id >= 0x10 && np->revision_id <= 0x15)
6174 OUTB (nc_ctest0, (1<<5));
6175 else if (np->device_id == PCI_DEVICE_ID_NCR_53C896 &&
6176 np->revision_id <= 0x1)
6177 OUTB (nc_ccntl0, DPR);
6180 ** Fill in target structure.
6181 ** Reinitialize usrsync.
6182 ** Reinitialize usrwide.
6183 ** Prepare sync negotiation according to actual SCSI bus mode.
6186 for (i=0;i<MAX_TARGET;i++) {
6187 tcb_p tp = &np->target[i];
6189 tp->sval = 0;
6190 tp->wval = np->rv_scntl3;
6192 if (tp->usrsync != 255) {
6193 if (tp->usrsync <= np->maxsync) {
6194 if (tp->usrsync < np->minsync) {
6195 tp->usrsync = np->minsync;
6198 else
6199 tp->usrsync = 255;
6202 if (tp->usrwide > np->maxwide)
6203 tp->usrwide = np->maxwide;
6205 ncr_negotiate (np, tp);
6209 ** Start script processor.
6211 MEMORY_BARRIER();
6212 if (np->paddr2) {
6213 if (bootverbose)
6214 printk ("%s: Downloading SCSI SCRIPTS.\n",
6215 ncr_name(np));
6216 OUTL (nc_scratcha, vtophys(np->script0));
6217 OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, start_ram));
6219 else
6220 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
6223 /*==========================================================
6225 ** Prepare the negotiation values for wide and
6226 ** synchronous transfers.
6228 **==========================================================
6231 static void ncr_negotiate (struct ncb* np, struct tcb* tp)
6234 ** minsync unit is 4ns !
6237 u_long minsync = tp->usrsync;
6240 ** SCSI bus mode limit
6243 if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
6244 if (minsync < 12) minsync = 12;
6248 ** our limit ..
6251 if (minsync < np->minsync)
6252 minsync = np->minsync;
6255 ** divider limit
6258 if (minsync > np->maxsync)
6259 minsync = 255;
6261 tp->minsync = minsync;
6262 tp->maxoffs = (minsync<255 ? np->maxoffs : 0);
6265 ** period=0: has to negotiate sync transfer
6268 tp->period=0;
6271 ** widedone=0: has to negotiate wide transfer
6273 tp->widedone=0;
6276 /*==========================================================
6278 ** Get clock factor and sync divisor for a given
6279 ** synchronous factor period.
6280 ** Returns the clock factor (in sxfer) and scntl3
6281 ** synchronous divisor field.
6283 **==========================================================
6286 static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
6288 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
6289 int div = np->clock_divn; /* Number of divisors supported */
6290 u_long fak; /* Sync factor in sxfer */
6291 u_long per; /* Period in tenths of ns */
6292 u_long kpc; /* (per * clk) */
6295 ** Compute the synchronous period in tenths of nano-seconds
6297 if (sfac <= 10) per = 250;
6298 else if (sfac == 11) per = 303;
6299 else if (sfac == 12) per = 500;
6300 else per = 40 * sfac;
6303 ** Look for the greatest clock divisor that allows an
6304 ** input speed faster than the period.
6306 kpc = per * clk;
6307 while (--div >= 0)
6308 if (kpc >= (div_10M[div] << 2)) break;
6311 ** Calculate the lowest clock factor that allows an output
6312 ** speed not faster than the period.
6314 fak = (kpc - 1) / div_10M[div] + 1;
6316 #if 0 /* This optimization does not seem very usefull */
6318 per = (fak * div_10M[div]) / clk;
6321 ** Why not to try the immediate lower divisor and to choose
6322 ** the one that allows the fastest output speed ?
6323 ** We dont want input speed too much greater than output speed.
6325 if (div >= 1 && fak < 8) {
6326 u_long fak2, per2;
6327 fak2 = (kpc - 1) / div_10M[div-1] + 1;
6328 per2 = (fak2 * div_10M[div-1]) / clk;
6329 if (per2 < per && fak2 <= 8) {
6330 fak = fak2;
6331 per = per2;
6332 --div;
6335 #endif
6337 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
6340 ** Compute and return sync parameters for the ncr
6342 *fakp = fak - 4;
6343 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
6347 /*==========================================================
6349 ** Set actual values, sync status and patch all ccbs of
6350 ** a target according to new sync/wide agreement.
6352 **==========================================================
6355 static void ncr_set_sync_wide_status (ncb_p np, u_char target)
6357 ccb_p cp;
6358 tcb_p tp = &np->target[target];
6361 ** set actual value and sync_status
6363 OUTB (nc_sxfer, tp->sval);
6364 np->sync_st = tp->sval;
6365 OUTB (nc_scntl3, tp->wval);
6366 np->wide_st = tp->wval;
6369 ** patch ALL ccbs of this target.
6371 for (cp = np->ccb; cp; cp = cp->link_ccb) {
6372 if (!cp->cmd) continue;
6373 if (cp->cmd->target != target) continue;
6374 #if 0
6375 cp->sync_status = tp->sval;
6376 cp->wide_status = tp->wval;
6377 #endif
6378 cp->phys.select.sel_scntl3 = tp->wval;
6379 cp->phys.select.sel_sxfer = tp->sval;
6383 /*==========================================================
6385 ** Switch sync mode for current job and it's target
6387 **==========================================================
6390 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer)
6392 Scsi_Cmnd *cmd;
6393 tcb_p tp;
6394 u_char target = INB (nc_sdid) & 0x0f;
6395 u_char idiv;
6397 assert (cp && cp->cmd);
6398 if (!cp) return;
6400 cmd = cp->cmd;
6401 if (!cmd) return;
6403 assert (target == (cmd->target & 0xf));
6405 tp = &np->target[target];
6407 if (!scntl3 || !(sxfer & 0x1f))
6408 scntl3 = np->rv_scntl3;
6409 scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
6412 ** Deduce the value of controller sync period from scntl3.
6413 ** period is in tenths of nano-seconds.
6416 idiv = ((scntl3 >> 4) & 0x7);
6417 if ((sxfer & 0x1f) && idiv)
6418 tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
6419 else
6420 tp->period = 0xffff;
6423 ** Stop there if sync parameters are unchanged
6425 if (tp->sval == sxfer && tp->wval == scntl3) return;
6426 tp->sval = sxfer;
6427 tp->wval = scntl3;
6430 ** Bells and whistles ;-)
6432 PRINT_TARGET(np, target);
6433 if (sxfer & 0x01f) {
6434 unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
6435 unsigned mb10 = (f10 + tp->period/2) / tp->period;
6436 char *scsi;
6439 ** Disable extended Sreq/Sack filtering
6441 if (tp->period <= 2000) OUTOFFB (nc_stest2, EXT);
6444 ** Bells and whistles ;-)
6446 if (tp->period < 500) scsi = "FAST-40";
6447 else if (tp->period < 1000) scsi = "FAST-20";
6448 else if (tp->period < 2000) scsi = "FAST-10";
6449 else scsi = "FAST-5";
6451 printk ("%s %sSCSI %d.%d MB/s (%d ns, offset %d)\n", scsi,
6452 tp->widedone > 1 ? "WIDE " : "",
6453 mb10 / 10, mb10 % 10, tp->period / 10, sxfer & 0x1f);
6454 } else
6455 printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
6458 ** set actual value and sync_status
6459 ** patch ALL ccbs of this target.
6461 ncr_set_sync_wide_status(np, target);
6464 /*==========================================================
6466 ** Switch wide mode for current job and it's target
6467 ** SCSI specs say: a SCSI device that accepts a WDTR
6468 ** message shall reset the synchronous agreement to
6469 ** asynchronous mode.
6471 **==========================================================
6474 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack)
6476 Scsi_Cmnd *cmd;
6477 u_short target = INB (nc_sdid) & 0x0f;
6478 tcb_p tp;
6479 u_char scntl3;
6480 u_char sxfer;
6482 assert (cp && cp->cmd);
6483 if (!cp) return;
6485 cmd = cp->cmd;
6486 if (!cmd) return;
6488 assert (target == (cmd->target & 0xf));
6490 tp = &np->target[target];
6491 tp->widedone = wide+1;
6492 scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
6494 sxfer = ack ? 0 : tp->sval;
6497 ** Stop there if sync/wide parameters are unchanged
6499 if (tp->sval == sxfer && tp->wval == scntl3) return;
6500 tp->sval = sxfer;
6501 tp->wval = scntl3;
6504 ** Bells and whistles ;-)
6506 if (bootverbose >= 2) {
6507 PRINT_TARGET(np, target);
6508 if (scntl3 & EWS)
6509 printk ("WIDE SCSI (16 bit) enabled.\n");
6510 else
6511 printk ("WIDE SCSI disabled.\n");
6515 ** set actual value and sync_status
6516 ** patch ALL ccbs of this target.
6518 ncr_set_sync_wide_status(np, target);
6521 /*==========================================================
6523 ** Switch tagged mode for a target.
6525 **==========================================================
6528 static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln)
6530 tcb_p tp = &np->target[tn];
6531 lcb_p lp = tp->lp[ln];
6532 u_char reqtags, maxdepth;
6535 ** Just in case ...
6537 if ((!tp) || (!lp))
6538 return;
6541 ** If SCSI device queue depth is not yet set, leave here.
6543 if (!lp->scdev_depth)
6544 return;
6547 ** Donnot allow more tags than the SCSI driver can queue
6548 ** for this device.
6549 ** Donnot allow more tags than we can handle.
6551 maxdepth = lp->scdev_depth;
6552 if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
6553 if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
6554 if (lp->numtags > maxdepth) lp->numtags = maxdepth;
6557 ** only devices conformant to ANSI Version >= 2
6558 ** only devices capable of tagged commands
6559 ** only if enabled by user ..
6561 if ((lp->inq_byte7 & INQ7_QUEUE) && lp->numtags > 1) {
6562 reqtags = lp->numtags;
6563 } else {
6564 reqtags = 1;
6568 ** Update max number of tags
6570 lp->numtags = reqtags;
6571 if (lp->numtags > lp->maxtags)
6572 lp->maxtags = lp->numtags;
6575 ** If we want to switch tag mode, we must wait
6576 ** for no CCB to be active.
6578 if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
6579 if (lp->queuedepth == reqtags) /* Already announced */
6580 return;
6581 lp->queuedepth = reqtags;
6583 else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
6584 lp->queuedepth = reqtags;
6585 return;
6587 else { /* Want to switch tag mode */
6588 if (lp->busyccbs) /* If not yet safe, return */
6589 return;
6590 lp->queuedepth = reqtags;
6591 lp->usetags = reqtags > 1 ? 1 : 0;
6595 ** Patch the lun mini-script, according to tag mode.
6597 lp->jump_tag.l_paddr = lp->usetags?
6598 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
6599 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
6602 ** Announce change to user.
6604 if (bootverbose) {
6605 PRINT_LUN(np, tn, ln);
6606 if (lp->usetags) {
6607 printk("tagged command queue depth set to %d\n", reqtags);
6609 else {
6610 printk("tagged command queueing disabled\n");
6615 /*----------------------------------------------------
6617 ** handle user commands
6619 **----------------------------------------------------
6622 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
6624 static void ncr_usercmd (ncb_p np)
6626 u_char t;
6627 tcb_p tp;
6629 switch (np->user.cmd) {
6631 case 0: return;
6633 case UC_SETSYNC:
6634 for (t=0; t<MAX_TARGET; t++) {
6635 if (!((np->user.target>>t)&1)) continue;
6636 tp = &np->target[t];
6637 tp->usrsync = np->user.data;
6638 ncr_negotiate (np, tp);
6640 break;
6642 case UC_SETTAGS:
6643 for (t=0; t<MAX_TARGET; t++) {
6644 int ln;
6645 if (!((np->user.target>>t)&1)) continue;
6646 np->target[t].usrtags = np->user.data;
6647 for (ln = 0; ln < MAX_LUN; ln++) {
6648 lcb_p lp = np->target[t].lp[ln];
6649 if (!lp)
6650 continue;
6651 lp->maxtags = lp->numtags = np->user.data;
6652 ncr_setup_tags (np, t, ln);
6655 break;
6657 case UC_SETDEBUG:
6658 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
6659 ncr_debug = np->user.data;
6660 #endif
6661 break;
6663 case UC_SETORDER:
6664 np->order = np->user.data;
6665 break;
6667 case UC_SETVERBOSE:
6668 np->verbose = np->user.data;
6669 break;
6671 case UC_SETWIDE:
6672 for (t=0; t<MAX_TARGET; t++) {
6673 u_long size;
6674 if (!((np->user.target>>t)&1)) continue;
6675 tp = &np->target[t];
6676 size = np->user.data;
6677 if (size > np->maxwide) size=np->maxwide;
6678 tp->usrwide = size;
6679 ncr_negotiate (np, tp);
6681 break;
6683 case UC_SETFLAG:
6684 for (t=0; t<MAX_TARGET; t++) {
6685 if (!((np->user.target>>t)&1)) continue;
6686 tp = &np->target[t];
6687 tp->usrflag = np->user.data;
6689 break;
6691 #ifdef SCSI_NCR_PROFILE_SUPPORT
6692 case UC_CLEARPROF:
6693 bzero(&np->profile, sizeof(np->profile));
6694 break;
6695 #endif
6697 np->user.cmd=0;
6699 #endif
6701 /*==========================================================
6704 ** ncr timeout handler.
6707 **==========================================================
6709 ** Misused to keep the driver running when
6710 ** interrupts are not configured correctly.
6712 **----------------------------------------------------------
6715 static void ncr_timeout (ncb_p np)
6717 u_long thistime = jiffies;
6720 ** If release process in progress, let's go
6721 ** Set the release stage from 1 to 2 to synchronize
6722 ** with the release process.
6725 if (np->release_stage) {
6726 if (np->release_stage == 1) np->release_stage = 2;
6727 return;
6730 np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
6731 add_timer(&np->timer);
6734 ** If we are resetting the ncr, wait for settle_time before
6735 ** clearing it. Then command processing will be resumed.
6737 if (np->settle_time) {
6738 if (np->settle_time <= thistime) {
6739 if (bootverbose > 1)
6740 printk("%s: command processing resumed\n", ncr_name(np));
6741 np->settle_time = 0;
6742 np->disc = 1;
6743 requeue_waiting_list(np);
6745 return;
6749 ** Since the generic scsi driver only allows us 0.5 second
6750 ** to perform abort of a command, we must look at ccbs about
6751 ** every 0.25 second.
6753 if (np->lasttime + 4*HZ < thistime) {
6755 ** block ncr interrupts
6757 np->lasttime = thistime;
6759 #ifdef SCSI_NCR_PROFILE_SUPPORT
6761 ** Reset profile data to avoid ugly overflow
6762 ** (Limited to 1024 GB for 32 bit architecture)
6764 if (np->profile.num_kbytes > (~0UL >> 2))
6765 bzero(&np->profile, sizeof(np->profile));
6766 #endif
6769 #ifdef SCSI_NCR_BROKEN_INTR
6770 if (INB(nc_istat) & (INTF|SIP|DIP)) {
6773 ** Process pending interrupts.
6775 if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
6776 ncr_exception (np);
6777 if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
6779 #endif /* SCSI_NCR_BROKEN_INTR */
6782 /*==========================================================
6784 ** log message for real hard errors
6786 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
6787 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
6789 ** exception register:
6790 ** ds: dstat
6791 ** si: sist
6793 ** SCSI bus lines:
6794 ** so: control lines as driver by NCR.
6795 ** si: control lines as seen by NCR.
6796 ** sd: scsi data lines as seen by NCR.
6798 ** wide/fastmode:
6799 ** sxfer: (see the manual)
6800 ** scntl3: (see the manual)
6802 ** current script command:
6803 ** dsp: script address (relative to start of script).
6804 ** dbc: first word of script command.
6806 ** First 16 register of the chip:
6807 ** r0..rf
6809 **==========================================================
6812 static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
6814 u_int32 dsp;
6815 int script_ofs;
6816 int script_size;
6817 char *script_name;
6818 u_char *script_base;
6819 int i;
6821 dsp = INL (nc_dsp);
6823 if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
6824 script_ofs = dsp - np->p_script;
6825 script_size = sizeof(struct script);
6826 script_base = (u_char *) np->script0;
6827 script_name = "script";
6829 else if (np->p_scripth < dsp &&
6830 dsp <= np->p_scripth + sizeof(struct scripth)) {
6831 script_ofs = dsp - np->p_scripth;
6832 script_size = sizeof(struct scripth);
6833 script_base = (u_char *) np->scripth0;
6834 script_name = "scripth";
6835 } else {
6836 script_ofs = dsp;
6837 script_size = 0;
6838 script_base = 0;
6839 script_name = "mem";
6842 printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
6843 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
6844 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
6845 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
6846 (unsigned)INL (nc_dbc));
6848 if (((script_ofs & 3) == 0) &&
6849 (unsigned)script_ofs < script_size) {
6850 printk ("%s: script cmd = %08x\n", ncr_name(np),
6851 scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
6854 printk ("%s: regdump:", ncr_name(np));
6855 for (i=0; i<16;i++)
6856 printk (" %02x", (unsigned)INB_OFF(i));
6857 printk (".\n");
6860 /*============================================================
6862 ** ncr chip exception handler.
6864 **============================================================
6866 ** In normal cases, interrupt conditions occur one at a
6867 ** time. The ncr is able to stack in some extra registers
6868 ** other interrupts that will occurs after the first one.
6869 ** But severall interrupts may occur at the same time.
6871 ** We probably should only try to deal with the normal
6872 ** case, but it seems that multiple interrupts occur in
6873 ** some cases that are not abnormal at all.
6875 ** The most frequent interrupt condition is Phase Mismatch.
6876 ** We should want to service this interrupt quickly.
6877 ** A SCSI parity error may be delivered at the same time.
6878 ** The SIR interrupt is not very frequent in this driver,
6879 ** since the INTFLY is likely used for command completion
6880 ** signaling.
6881 ** The Selection Timeout interrupt may be triggered with
6882 ** IID and/or UDC.
6883 ** The SBMC interrupt (SCSI Bus Mode Change) may probably
6884 ** occur at any time.
6886 ** This handler try to deal as cleverly as possible with all
6887 ** the above.
6889 **============================================================
6892 void ncr_exception (ncb_p np)
6894 u_char istat, dstat;
6895 u_short sist;
6896 int i;
6899 ** interrupt on the fly ?
6900 ** Since the global header may be copied back to a CCB
6901 ** using a posted PCI memory write, the last operation on
6902 ** the istat register is a READ in order to flush posted
6903 ** PCI write commands.
6905 istat = INB (nc_istat);
6906 if (istat & INTF) {
6907 OUTB (nc_istat, (istat & SIGP) | INTF);
6908 istat = INB (nc_istat);
6909 if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
6910 #ifdef SCSI_NCR_PROFILE_SUPPORT
6911 np->profile.num_fly++;
6912 #endif
6913 ncr_wakeup_done (np);
6916 if (!(istat & (SIP|DIP)))
6917 return;
6919 #ifdef SCSI_NCR_PROFILE_SUPPORT
6920 np->profile.num_int++;
6921 #endif
6923 if (istat & CABRT)
6924 OUTB (nc_istat, CABRT);
6927 ** Steinbach's Guideline for Systems Programming:
6928 ** Never test for an error condition you don't know how to handle.
6931 sist = (istat & SIP) ? INW (nc_sist) : 0;
6932 dstat = (istat & DIP) ? INB (nc_dstat) : 0;
6934 if (DEBUG_FLAGS & DEBUG_TINY)
6935 printk ("<%d|%x:%x|%x:%x>",
6936 (int)INB(nc_scr0),
6937 dstat,sist,
6938 (unsigned)INL(nc_dsp),
6939 (unsigned)INL(nc_dbc));
6941 /*========================================================
6942 ** First, interrupts we want to service cleanly.
6944 ** Phase mismatch is the most frequent interrupt, and
6945 ** so we have to service it as quickly and as cleanly
6946 ** as possible.
6947 ** Programmed interrupts are rarely used in this driver,
6948 ** but we must handle them cleanly anyway.
6949 ** We try to deal with PAR and SBMC combined with
6950 ** some other interrupt(s).
6951 **=========================================================
6954 if (!(sist & (STO|GEN|HTH|SGE|UDC|RST)) &&
6955 !(dstat & (MDPE|BF|ABRT|IID))) {
6956 if ((sist & SBMC) && ncr_int_sbmc (np))
6957 return;
6958 if ((sist & PAR) && ncr_int_par (np))
6959 return;
6960 if (sist & MA) {
6961 ncr_int_ma (np);
6962 return;
6964 if (dstat & SIR) {
6965 ncr_int_sir (np);
6966 return;
6969 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
6971 if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
6972 printk( "%s: unknown interrupt(s) ignored, "
6973 "ISTAT=%x DSTAT=%x SIST=%x\n",
6974 ncr_name(np), istat, dstat, sist);
6975 return;
6977 OUTONB (nc_dcntl, (STD|NOCOM));
6978 return;
6981 /*========================================================
6982 ** Now, interrupts that need some fixing up.
6983 ** Order and multiple interrupts is so less important.
6985 ** If SRST has been asserted, we just reset the chip.
6987 ** Selection is intirely handled by the chip. If the
6988 ** chip says STO, we trust it. Seems some other
6989 ** interrupts may occur at the same time (UDC, IID), so
6990 ** we ignore them. In any case we do enough fix-up
6991 ** in the service routine.
6992 ** We just exclude some fatal dma errors.
6993 **=========================================================
6996 if (sist & RST) {
6997 ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
6998 return;
7001 if ((sist & STO) &&
7002 !(dstat & (MDPE|BF|ABRT))) {
7004 ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
7006 OUTONB (nc_ctest3, CLF);
7008 ncr_int_sto (np);
7009 return;
7012 /*=========================================================
7013 ** Now, interrupts we are not able to recover cleanly.
7014 ** (At least for the moment).
7016 ** Do the register dump.
7017 ** Log message for real hard errors.
7018 ** Clear all fifos.
7019 ** For MDPE, BF, ABORT, IID, SGE and HTH we reset the
7020 ** BUS and the chip.
7021 ** We are more soft for UDC.
7022 **=========================================================
7024 if (jiffies - np->regtime > 10*HZ) {
7025 np->regtime = jiffies;
7026 for (i = 0; i<sizeof(np->regdump); i++)
7027 ((char*)&np->regdump)[i] = INB_OFF(i);
7028 np->regdump.nc_dstat = dstat;
7029 np->regdump.nc_sist = sist;
7032 ncr_log_hard_error(np, sist, dstat);
7034 printk ("%s: have to clear fifos.\n", ncr_name (np));
7035 OUTB (nc_stest3, TE|CSF);
7036 OUTONB (nc_ctest3, CLF);
7038 if ((sist & (SGE)) ||
7039 (dstat & (MDPE|BF|ABRT|IID))) {
7040 ncr_start_reset(np);
7041 return;
7044 if (sist & HTH) {
7045 printk ("%s: handshake timeout\n", ncr_name(np));
7046 ncr_start_reset(np);
7047 return;
7050 if (sist & UDC) {
7051 printk ("%s: unexpected disconnect\n", ncr_name(np));
7052 OUTB (HS_PRT, HS_UNEXPECTED);
7053 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
7054 return;
7057 /*=========================================================
7058 ** We just miss the cause of the interrupt. :(
7059 ** Print a message. The timeout will do the real work.
7060 **=========================================================
7062 printk ("%s: unknown interrupt\n", ncr_name(np));
7065 /*==========================================================
7067 ** ncr chip exception handler for selection timeout
7069 **==========================================================
7071 ** There seems to be a bug in the 53c810.
7072 ** Although a STO-Interrupt is pending,
7073 ** it continues executing script commands.
7074 ** But it will fail and interrupt (IID) on
7075 ** the next instruction where it's looking
7076 ** for a valid phase.
7078 **----------------------------------------------------------
7081 void ncr_int_sto (ncb_p np)
7083 u_long dsa;
7084 ccb_p cp;
7085 if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
7088 ** look for ccb and set the status.
7091 dsa = INL (nc_dsa);
7092 cp = np->ccb;
7093 while (cp && (CCB_PHYS (cp, phys) != dsa))
7094 cp = cp->link_ccb;
7096 if (cp) {
7097 cp-> host_status = HS_SEL_TIMEOUT;
7098 ncr_complete (np, cp);
7102 ** repair start queue and jump to start point.
7105 OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, sto_restart));
7106 return;
7109 /*==========================================================
7111 ** ncr chip exception handler for SCSI bus mode change
7113 **==========================================================
7115 ** spi2-r12 11.2.3 says a transceiver mode change must
7116 ** generate a reset event and a device that detects a reset
7117 ** event shall initiate a hard reset. It says also that a
7118 ** device that detects a mode change shall set data transfer
7119 ** mode to eight bit asynchronous, etc...
7120 ** So, just resetting should be enough.
7123 **----------------------------------------------------------
7126 static int ncr_int_sbmc (ncb_p np)
7128 u_char scsi_mode = INB (nc_stest4) & SMODE;
7130 if (scsi_mode != np->scsi_mode) {
7131 printk("%s: SCSI bus mode change from %x to %x.\n",
7132 ncr_name(np), np->scsi_mode, scsi_mode);
7134 np->scsi_mode = scsi_mode;
7138 ** Suspend command processing for 1 second and
7139 ** reinitialize all except the chip.
7141 np->settle_time = jiffies + HZ;
7142 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
7143 return 1;
7145 return 0;
7148 /*==========================================================
7150 ** ncr chip exception handler for SCSI parity error.
7152 **==========================================================
7155 **----------------------------------------------------------
7158 static int ncr_int_par (ncb_p np)
7160 u_char hsts = INB (HS_PRT);
7161 u_int32 dbc = INL (nc_dbc);
7162 u_char sstat1 = INB (nc_sstat1);
7163 int phase = -1;
7164 int msg = -1;
7165 u_int32 jmp;
7167 printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
7168 ncr_name(np), hsts, dbc, sstat1);
7171 * Ignore the interrupt if the NCR is not connected
7172 * to the SCSI bus, since the right work should have
7173 * been done on unexpected disconnection handling.
7175 if (!(INB (nc_scntl1) & ISCON))
7176 return 0;
7179 * If the nexus is not clearly identified, reset the bus.
7180 * We will try to do better later.
7182 if (hsts & HS_INVALMASK)
7183 goto reset_all;
7186 * If the SCSI parity error occurs in MSG IN phase, prepare a
7187 * MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
7188 * ERROR message and let the device decide to retry the command
7189 * or to terminate with check condition. If we were in MSG IN
7190 * phase waiting for the response of a negotiation, we will
7191 * get SIR_NEGO_FAILED at dispatch.
7193 if (!(dbc & 0xc0000000))
7194 phase = (dbc >> 24) & 7;
7195 if (phase == 7)
7196 msg = M_PARITY;
7197 else
7198 msg = M_ID_ERROR;
7201 * If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
7202 * script that will ignore all data in bytes until phase
7203 * change, since we are not sure the chip will wait the phase
7204 * change prior to delivering the interrupt.
7206 if (phase == 1)
7207 jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
7208 else
7209 jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
7211 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
7212 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
7214 np->msgout[0] = msg;
7215 OUTL (nc_dsp, jmp);
7216 return 1;
7218 reset_all:
7219 ncr_start_reset(np);
7220 return 1;
7223 /*==========================================================
7226 ** ncr chip exception handler for phase errors.
7229 **==========================================================
7231 ** We have to construct a new transfer descriptor,
7232 ** to transfer the rest of the current block.
7234 **----------------------------------------------------------
7237 static void ncr_int_ma (ncb_p np)
7239 u_int32 dbc;
7240 u_int32 rest;
7241 u_int32 dsp;
7242 u_int32 dsa;
7243 u_int32 nxtdsp;
7244 u_int32 *vdsp;
7245 u_int32 oadr, olen;
7246 u_int32 *tblp;
7247 ncrcmd *newcmd;
7248 u_char cmd, sbcl;
7249 ccb_p cp;
7251 dsp = INL (nc_dsp);
7252 dbc = INL (nc_dbc);
7253 sbcl = INB (nc_sbcl);
7255 cmd = dbc >> 24;
7256 rest = dbc & 0xffffff;
7259 ** Take into account dma fifo and various buffers and latches,
7260 ** only if the interrupted phase is an OUTPUT phase.
7263 if ((cmd & 1) == 0) {
7264 u_char ctest5, ss0, ss2;
7265 u_short delta;
7267 ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
7268 if (ctest5 & DFS)
7269 delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
7270 else
7271 delta=(INB (nc_dfifo) - rest) & 0x7f;
7274 ** The data in the dma fifo has not been transfered to
7275 ** the target -> add the amount to the rest
7276 ** and clear the data.
7277 ** Check the sstat2 register in case of wide transfer.
7280 rest += delta;
7281 ss0 = INB (nc_sstat0);
7282 if (ss0 & OLF) rest++;
7283 if (ss0 & ORF) rest++;
7284 if (INB(nc_scntl3) & EWS) {
7285 ss2 = INB (nc_sstat2);
7286 if (ss2 & OLF1) rest++;
7287 if (ss2 & ORF1) rest++;
7290 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
7291 printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
7292 (unsigned) rest, (unsigned) delta, ss0);
7294 } else {
7295 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
7296 printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
7300 ** Clear fifos.
7302 OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
7303 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
7306 ** locate matching cp.
7307 ** if the interrupted phase is DATA IN or DATA OUT,
7308 ** trust the global header.
7310 dsa = INL (nc_dsa);
7311 if (!(cmd & 6)) {
7312 cp = np->header.cp;
7313 if (CCB_PHYS(cp, phys) != dsa)
7314 cp = 0;
7315 } else {
7316 cp = np->ccb;
7317 while (cp && (CCB_PHYS (cp, phys) != dsa))
7318 cp = cp->link_ccb;
7322 ** try to find the interrupted script command,
7323 ** and the address at which to continue.
7325 vdsp = 0;
7326 nxtdsp = 0;
7327 if (dsp > np->p_script &&
7328 dsp <= np->p_script + sizeof(struct script)) {
7329 vdsp = (u_int32 *)((char*)np->script0 + (dsp-np->p_script-8));
7330 nxtdsp = dsp;
7332 else if (dsp > np->p_scripth &&
7333 dsp <= np->p_scripth + sizeof(struct scripth)) {
7334 vdsp = (u_int32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
7335 nxtdsp = dsp;
7337 else if (cp) {
7338 if (dsp == vtophys (&cp->patch[2])) {
7339 vdsp = &cp->patch[0];
7340 nxtdsp = scr_to_cpu(vdsp[3]);
7342 else if (dsp == vtophys (&cp->patch[6])) {
7343 vdsp = &cp->patch[4];
7344 nxtdsp = scr_to_cpu(vdsp[3]);
7349 ** log the information
7352 if (DEBUG_FLAGS & DEBUG_PHASE) {
7353 printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
7354 cp, np->header.cp,
7355 (unsigned)dsp,
7356 (unsigned)nxtdsp, vdsp, cmd);
7360 ** cp=0 means that the DSA does not point to a valid control
7361 ** block. This should not happen since we donnot use multi-byte
7362 ** move while we are being reselected ot after command complete.
7363 ** We are not able to recover from such a phase error.
7365 if (!cp) {
7366 printk ("%s: SCSI phase error fixup: "
7367 "CCB already dequeued (0x%08lx)\n",
7368 ncr_name (np), (u_long) np->header.cp);
7369 goto reset_all;
7373 ** get old startaddress and old length.
7376 oadr = scr_to_cpu(vdsp[1]);
7378 if (cmd & 0x10) { /* Table indirect */
7379 tblp = (u_int32 *) ((char*) &cp->phys + oadr);
7380 olen = scr_to_cpu(tblp[0]);
7381 oadr = scr_to_cpu(tblp[1]);
7382 } else {
7383 tblp = (u_int32 *) 0;
7384 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
7387 if (DEBUG_FLAGS & DEBUG_PHASE) {
7388 printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
7389 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
7390 tblp,
7391 (unsigned) olen,
7392 (unsigned) oadr);
7396 ** check cmd against assumed interrupted script command.
7399 if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
7400 PRINT_ADDR(cp->cmd);
7401 printk ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
7402 (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
7404 goto reset_all;
7408 ** cp != np->header.cp means that the header of the CCB
7409 ** currently being processed has not yet been copied to
7410 ** the global header area. That may happen if the device did
7411 ** not accept all our messages after having been selected.
7413 if (cp != np->header.cp) {
7414 printk ("%s: SCSI phase error fixup: "
7415 "CCB address mismatch (0x%08lx != 0x%08lx)\n",
7416 ncr_name (np), (u_long) cp, (u_long) np->header.cp);
7420 ** if old phase not dataphase, leave here.
7423 if (cmd & 0x06) {
7424 PRINT_ADDR(cp->cmd);
7425 printk ("phase change %x-%x %d@%08x resid=%d.\n",
7426 cmd&7, sbcl&7, (unsigned)olen,
7427 (unsigned)oadr, (unsigned)rest);
7428 goto unexpected_phase;
7432 ** choose the correct patch area.
7433 ** if savep points to one, choose the other.
7436 newcmd = cp->patch;
7437 if (cp->phys.header.savep == cpu_to_scr(vtophys (newcmd))) newcmd+=4;
7440 ** fillin the commands
7443 newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
7444 newcmd[1] = cpu_to_scr(oadr + olen - rest);
7445 newcmd[2] = cpu_to_scr(SCR_JUMP);
7446 newcmd[3] = cpu_to_scr(nxtdsp);
7448 if (DEBUG_FLAGS & DEBUG_PHASE) {
7449 PRINT_ADDR(cp->cmd);
7450 printk ("newcmd[%d] %x %x %x %x.\n",
7451 (int) (newcmd - cp->patch),
7452 (unsigned)scr_to_cpu(newcmd[0]),
7453 (unsigned)scr_to_cpu(newcmd[1]),
7454 (unsigned)scr_to_cpu(newcmd[2]),
7455 (unsigned)scr_to_cpu(newcmd[3]));
7458 ** fake the return address (to the patch).
7459 ** and restart script processor at dispatcher.
7461 #ifdef SCSI_NCR_PROFILE_SUPPORT
7462 np->profile.num_break++;
7463 #endif
7464 OUTL (nc_temp, vtophys (newcmd));
7465 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
7466 return;
7469 ** Unexpected phase changes that occurs when the current phase
7470 ** is not a DATA IN or DATA OUT phase are due to error conditions.
7471 ** Such event may only happen when the SCRIPTS is using a
7472 ** multibyte SCSI MOVE.
7474 ** Phase change Some possible cause
7476 ** COMMAND --> MSG IN SCSI parity error detected by target.
7477 ** COMMAND --> STATUS Bad command or refused by target.
7478 ** MSG OUT --> MSG IN Message rejected by target.
7479 ** MSG OUT --> COMMAND Bogus target that discards extended
7480 ** negotiation messages.
7482 ** The code below does not care of the new phase and so
7483 ** trusts the target. Why to annoy it ?
7484 ** If the interrupted phase is COMMAND phase, we restart at
7485 ** dispatcher.
7486 ** If a target does not get all the messages after selection,
7487 ** the code assumes blindly that the target discards extended
7488 ** messages and clears the negotiation status.
7489 ** If the target does not want all our response to negotiation,
7490 ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
7491 ** bloat for such a should_not_happen situation).
7492 ** In all other situation, we reset the BUS.
7493 ** Are these assumptions reasonnable ? (Wait and see ...)
7495 unexpected_phase:
7496 dsp -= 8;
7497 nxtdsp = 0;
7499 switch (cmd & 7) {
7500 case 2: /* COMMAND phase */
7501 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
7502 break;
7503 #if 0
7504 case 3: /* STATUS phase */
7505 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
7506 break;
7507 #endif
7508 case 6: /* MSG OUT phase */
7509 np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
7510 if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
7511 cp->host_status = HS_BUSY;
7512 nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
7514 else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
7515 dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
7516 nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
7518 break;
7519 #if 0
7520 case 7: /* MSG IN phase */
7521 nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
7522 break;
7523 #endif
7526 if (nxtdsp) {
7527 OUTL (nc_dsp, nxtdsp);
7528 return;
7531 reset_all:
7532 ncr_start_reset(np);
7536 static void ncr_sir_to_redo(ncb_p np, int num, ccb_p cp)
7538 Scsi_Cmnd *cmd = cp->cmd;
7539 tcb_p tp = &np->target[cmd->target];
7540 lcb_p lp = tp->lp[cmd->lun];
7541 XPT_QUEHEAD *qp;
7542 ccb_p cp2;
7543 int disc_cnt = 0;
7544 int busy_cnt = 0;
7545 u_int32 startp;
7546 u_char s_status = INB (SS_PRT);
7549 ** Let the SCRIPTS processor skip all not yet started CCBs,
7550 ** and count disconnected CCBs. Since the busy queue is in
7551 ** the same order as the chip start queue, disconnected CCBs
7552 ** are before cp and busy ones after.
7554 if (lp) {
7555 qp = lp->busy_ccbq.blink;
7556 while (qp != &lp->busy_ccbq) {
7557 cp2 = xpt_que_entry(qp, struct ccb, link_ccbq);
7558 qp = qp->blink;
7559 ++busy_cnt;
7560 if (cp2 == cp)
7561 break;
7562 cp2->start.schedule.l_paddr =
7563 cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
7565 lp->held_ccb = cp; /* Requeue when this one completes */
7566 disc_cnt = lp->queuedccbs - busy_cnt;
7569 switch(s_status) {
7570 default: /* Just for safety, should never happen */
7571 case S_QUEUE_FULL:
7573 ** Decrease number of tags to the number of
7574 ** disconnected commands.
7576 if (!lp)
7577 goto out;
7578 if (bootverbose >= 1) {
7579 PRINT_ADDR(cmd);
7580 printk ("QUEUE FULL! %d busy, %d disconnected CCBs\n",
7581 busy_cnt, disc_cnt);
7583 if (disc_cnt < lp->numtags) {
7584 lp->numtags = disc_cnt > 2 ? disc_cnt : 2;
7585 lp->num_good = 0;
7586 ncr_setup_tags (np, cmd->target, cmd->lun);
7589 ** Requeue the command to the start queue.
7590 ** If any disconnected commands,
7591 ** Clear SIGP.
7592 ** Jump to reselect.
7594 cp->phys.header.savep = cp->startp;
7595 cp->host_status = HS_BUSY;
7596 cp->scsi_status = S_ILLEGAL;
7598 ncr_put_start_queue(np, cp);
7599 if (disc_cnt)
7600 INB (nc_ctest2); /* Clear SIGP */
7601 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
7602 return;
7603 case S_TERMINATED:
7604 case S_CHECK_COND:
7606 ** If we were requesting sense, give up.
7608 if (cp->auto_sense)
7609 goto out;
7612 ** Device returned CHECK CONDITION status.
7613 ** Prepare all needed data strutures for getting
7614 ** sense data.
7616 ** identify message
7618 cp->scsi_smsg2[0] = M_IDENTIFY | cmd->lun;
7619 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
7620 cp->phys.smsg.size = cpu_to_scr(1);
7623 ** sense command
7625 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
7626 cp->phys.cmd.size = cpu_to_scr(6);
7629 ** patch requested size into sense command
7631 cp->sensecmd[0] = 0x03;
7632 cp->sensecmd[1] = cmd->lun << 5;
7633 cp->sensecmd[4] = sizeof(cmd->sense_buffer);
7636 ** sense data
7638 cp->phys.sense.addr =
7639 cpu_to_scr(vtophys (&cmd->sense_buffer[0]));
7640 cp->phys.sense.size =
7641 cpu_to_scr(sizeof(cmd->sense_buffer));
7644 ** requeue the command.
7646 startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
7648 cp->phys.header.savep = startp;
7649 cp->phys.header.goalp = startp + 24;
7650 cp->phys.header.lastp = startp;
7651 cp->phys.header.wgoalp = startp + 24;
7652 cp->phys.header.wlastp = startp;
7654 cp->host_status = HS_BUSY;
7655 cp->scsi_status = S_ILLEGAL;
7656 cp->auto_sense = s_status;
7658 cp->start.schedule.l_paddr =
7659 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
7662 ** Select without ATN for quirky devices.
7664 if (tp->quirks & QUIRK_NOMSG)
7665 cp->start.schedule.l_paddr =
7666 cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
7668 ncr_put_start_queue(np, cp);
7670 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
7671 return;
7674 out:
7675 OUTONB (nc_dcntl, (STD|NOCOM));
7676 return;
7680 /*==========================================================
7683 ** ncr chip exception handler for programmed interrupts.
7686 **==========================================================
7689 static int ncr_show_msg (u_char * msg)
7691 u_char i;
7692 printk ("%x",*msg);
7693 if (*msg==M_EXTENDED) {
7694 for (i=1;i<8;i++) {
7695 if (i-1>msg[1]) break;
7696 printk ("-%x",msg[i]);
7698 return (i+1);
7699 } else if ((*msg & 0xf0) == 0x20) {
7700 printk ("-%x",msg[1]);
7701 return (2);
7703 return (1);
7707 void ncr_int_sir (ncb_p np)
7709 u_char scntl3;
7710 u_char chg, ofs, per, fak, wide;
7711 u_char num = INB (nc_dsps);
7712 ccb_p cp=0;
7713 u_long dsa = INL (nc_dsa);
7714 u_char target = INB (nc_sdid) & 0x0f;
7715 tcb_p tp = &np->target[target];
7717 if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
7719 switch (num) {
7720 case SIR_RESEL_NO_MSG_IN:
7721 case SIR_RESEL_NO_IDENTIFY:
7723 ** If devices reselecting without sending an IDENTIFY
7724 ** message still exist, this should help.
7725 ** We just assume lun=0, 1 CCB, no tag.
7727 if (tp->lp[0]) {
7728 OUTL (nc_dsp, scr_to_cpu(tp->lp[0]->jump_ccb[0]));
7729 return;
7731 case SIR_RESEL_BAD_TARGET: /* Will send a TARGET RESET message */
7732 case SIR_RESEL_BAD_LUN: /* Will send a TARGET RESET message */
7733 case SIR_RESEL_BAD_I_T_L_Q: /* Will send an ABORT TAG message */
7734 case SIR_RESEL_BAD_I_T_L: /* Will send an ABORT message */
7735 printk ("%s:%d: SIR %d, "
7736 "incorrect nexus identification on reselection\n",
7737 ncr_name (np), target, num);
7738 goto out;
7739 case SIR_DONE_OVERFLOW:
7740 printk ("%s:%d: SIR %d, "
7741 "CCB done queue overflow\n",
7742 ncr_name (np), target, num);
7743 goto out;
7744 case SIR_BAD_STATUS:
7745 cp = np->header.cp;
7746 if (!cp || CCB_PHYS (cp, phys) != dsa)
7747 goto out;
7748 ncr_sir_to_redo(np, num, cp);
7749 return;
7750 default:
7752 ** lookup the ccb
7754 cp = np->ccb;
7755 while (cp && (CCB_PHYS (cp, phys) != dsa))
7756 cp = cp->link_ccb;
7758 assert (cp && cp == np->header.cp);
7760 if (!cp || cp != np->header.cp)
7761 goto out;
7764 switch (num) {
7765 /*-----------------------------------------------------------------------------
7767 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
7769 ** We try to negotiate sync and wide transfer only after
7770 ** a successfull inquire command. We look at byte 7 of the
7771 ** inquire data to determine the capabilities of the target.
7773 ** When we try to negotiate, we append the negotiation message
7774 ** to the identify and (maybe) simple tag message.
7775 ** The host status field is set to HS_NEGOTIATE to mark this
7776 ** situation.
7778 ** If the target doesn't answer this message immidiately
7779 ** (as required by the standard), the SIR_NEGO_FAIL interrupt
7780 ** will be raised eventually.
7781 ** The handler removes the HS_NEGOTIATE status, and sets the
7782 ** negotiated value to the default (async / nowide).
7784 ** If we receive a matching answer immediately, we check it
7785 ** for validity, and set the values.
7787 ** If we receive a Reject message immediately, we assume the
7788 ** negotiation has failed, and fall back to standard values.
7790 ** If we receive a negotiation message while not in HS_NEGOTIATE
7791 ** state, it's a target initiated negotiation. We prepare a
7792 ** (hopefully) valid answer, set our parameters, and send back
7793 ** this answer to the target.
7795 ** If the target doesn't fetch the answer (no message out phase),
7796 ** we assume the negotiation has failed, and fall back to default
7797 ** settings.
7799 ** When we set the values, we adjust them in all ccbs belonging
7800 ** to this target, in the controller's register, and in the "phys"
7801 ** field of the controller's struct ncb.
7803 ** Possible cases: hs sir msg_in value send goto
7804 ** We try to negotiate:
7805 ** -> target doesnt't msgin NEG FAIL noop defa. - dispatch
7806 ** -> target rejected our msg NEG FAIL reject defa. - dispatch
7807 ** -> target answered (ok) NEG SYNC sdtr set - clrack
7808 ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
7809 ** -> target answered (ok) NEG WIDE wdtr set - clrack
7810 ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
7811 ** -> any other msgin NEG FAIL noop defa. - dispatch
7813 ** Target tries to negotiate:
7814 ** -> incoming message --- SYNC sdtr set SDTR -
7815 ** -> incoming message --- WIDE wdtr set WDTR -
7816 ** We sent our answer:
7817 ** -> target doesn't msgout --- PROTO ? defa. - dispatch
7819 **-----------------------------------------------------------------------------
7822 case SIR_NEGO_FAILED:
7823 /*-------------------------------------------------------
7825 ** Negotiation failed.
7826 ** Target doesn't send an answer message,
7827 ** or target rejected our message.
7829 ** Remove negotiation request.
7831 **-------------------------------------------------------
7833 OUTB (HS_PRT, HS_BUSY);
7835 /* fall through */
7837 case SIR_NEGO_PROTO:
7838 /*-------------------------------------------------------
7840 ** Negotiation failed.
7841 ** Target doesn't fetch the answer message.
7843 **-------------------------------------------------------
7846 if (DEBUG_FLAGS & DEBUG_NEGO) {
7847 PRINT_ADDR(cp->cmd);
7848 printk ("negotiation failed sir=%x status=%x.\n",
7849 num, cp->nego_status);
7853 ** any error in negotiation:
7854 ** fall back to default mode.
7856 switch (cp->nego_status) {
7858 case NS_SYNC:
7859 ncr_setsync (np, cp, 0, 0xe0);
7860 break;
7862 case NS_WIDE:
7863 ncr_setwide (np, cp, 0, 0);
7864 break;
7867 np->msgin [0] = M_NOOP;
7868 np->msgout[0] = M_NOOP;
7869 cp->nego_status = 0;
7870 break;
7872 case SIR_NEGO_SYNC:
7874 ** Synchronous request message received.
7877 if (DEBUG_FLAGS & DEBUG_NEGO) {
7878 PRINT_ADDR(cp->cmd);
7879 printk ("sync msgin: ");
7880 (void) ncr_show_msg (np->msgin);
7881 printk (".\n");
7885 ** get requested values.
7888 chg = 0;
7889 per = np->msgin[3];
7890 ofs = np->msgin[4];
7891 if (ofs==0) per=255;
7894 ** if target sends SDTR message,
7895 ** it CAN transfer synch.
7898 if (ofs)
7899 tp->inq_byte7 |= INQ7_SYNC;
7902 ** check values against driver limits.
7905 if (per < np->minsync)
7906 {chg = 1; per = np->minsync;}
7907 if (per < tp->minsync)
7908 {chg = 1; per = tp->minsync;}
7909 if (ofs > tp->maxoffs)
7910 {chg = 1; ofs = tp->maxoffs;}
7913 ** Check against controller limits.
7915 fak = 7;
7916 scntl3 = 0;
7917 if (ofs != 0) {
7918 ncr_getsync(np, per, &fak, &scntl3);
7919 if (fak > 7) {
7920 chg = 1;
7921 ofs = 0;
7924 if (ofs == 0) {
7925 fak = 7;
7926 per = 0;
7927 scntl3 = 0;
7928 tp->minsync = 0;
7931 if (DEBUG_FLAGS & DEBUG_NEGO) {
7932 PRINT_ADDR(cp->cmd);
7933 printk ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
7934 per, scntl3, ofs, fak, chg);
7937 if (INB (HS_PRT) == HS_NEGOTIATE) {
7938 OUTB (HS_PRT, HS_BUSY);
7939 switch (cp->nego_status) {
7941 case NS_SYNC:
7943 ** This was an answer message
7945 if (chg) {
7947 ** Answer wasn't acceptable.
7949 ncr_setsync (np, cp, 0, 0xe0);
7950 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
7951 } else {
7953 ** Answer is ok.
7955 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7956 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
7958 return;
7960 case NS_WIDE:
7961 ncr_setwide (np, cp, 0, 0);
7962 break;
7967 ** It was a request. Set value and
7968 ** prepare an answer message
7971 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7973 np->msgout[0] = M_EXTENDED;
7974 np->msgout[1] = 3;
7975 np->msgout[2] = M_X_SYNC_REQ;
7976 np->msgout[3] = per;
7977 np->msgout[4] = ofs;
7979 cp->nego_status = NS_SYNC;
7981 if (DEBUG_FLAGS & DEBUG_NEGO) {
7982 PRINT_ADDR(cp->cmd);
7983 printk ("sync msgout: ");
7984 (void) ncr_show_msg (np->msgout);
7985 printk (".\n");
7988 if (!ofs) {
7989 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
7990 return;
7992 np->msgin [0] = M_NOOP;
7994 break;
7996 case SIR_NEGO_WIDE:
7998 ** Wide request message received.
8000 if (DEBUG_FLAGS & DEBUG_NEGO) {
8001 PRINT_ADDR(cp->cmd);
8002 printk ("wide msgin: ");
8003 (void) ncr_show_msg (np->msgin);
8004 printk (".\n");
8008 ** get requested values.
8011 chg = 0;
8012 wide = np->msgin[3];
8015 ** if target sends WDTR message,
8016 ** it CAN transfer wide.
8019 if (wide)
8020 tp->inq_byte7 |= INQ7_WIDE16;
8023 ** check values against driver limits.
8026 if (wide > tp->usrwide)
8027 {chg = 1; wide = tp->usrwide;}
8029 if (DEBUG_FLAGS & DEBUG_NEGO) {
8030 PRINT_ADDR(cp->cmd);
8031 printk ("wide: wide=%d chg=%d.\n", wide, chg);
8034 if (INB (HS_PRT) == HS_NEGOTIATE) {
8035 OUTB (HS_PRT, HS_BUSY);
8036 switch (cp->nego_status) {
8038 case NS_WIDE:
8040 ** This was an answer message
8042 if (chg) {
8044 ** Answer wasn't acceptable.
8046 ncr_setwide (np, cp, 0, 1);
8047 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
8048 } else {
8050 ** Answer is ok.
8052 ncr_setwide (np, cp, wide, 1);
8053 OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
8055 return;
8057 case NS_SYNC:
8058 ncr_setsync (np, cp, 0, 0xe0);
8059 break;
8064 ** It was a request, set value and
8065 ** prepare an answer message
8068 ncr_setwide (np, cp, wide, 1);
8070 np->msgout[0] = M_EXTENDED;
8071 np->msgout[1] = 2;
8072 np->msgout[2] = M_X_WIDE_REQ;
8073 np->msgout[3] = wide;
8075 np->msgin [0] = M_NOOP;
8077 cp->nego_status = NS_WIDE;
8079 if (DEBUG_FLAGS & DEBUG_NEGO) {
8080 PRINT_ADDR(cp->cmd);
8081 printk ("wide msgout: ");
8082 (void) ncr_show_msg (np->msgin);
8083 printk (".\n");
8085 break;
8087 /*--------------------------------------------------------------------
8089 ** Processing of special messages
8091 **--------------------------------------------------------------------
8094 case SIR_REJECT_RECEIVED:
8095 /*-----------------------------------------------
8097 ** We received a M_REJECT message.
8099 **-----------------------------------------------
8102 PRINT_ADDR(cp->cmd);
8103 printk ("M_REJECT received (%x:%x).\n",
8104 (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
8105 break;
8107 case SIR_REJECT_SENT:
8108 /*-----------------------------------------------
8110 ** We received an unknown message
8112 **-----------------------------------------------
8115 PRINT_ADDR(cp->cmd);
8116 printk ("M_REJECT sent for ");
8117 (void) ncr_show_msg (np->msgin);
8118 printk (".\n");
8119 break;
8121 /*--------------------------------------------------------------------
8123 ** Processing of special messages
8125 **--------------------------------------------------------------------
8128 case SIR_IGN_RESIDUE:
8129 /*-----------------------------------------------
8131 ** We received an IGNORE RESIDUE message,
8132 ** which couldn't be handled by the script.
8134 **-----------------------------------------------
8137 PRINT_ADDR(cp->cmd);
8138 printk ("M_IGN_RESIDUE received, but not yet implemented.\n");
8139 break;
8140 #if 0
8141 case SIR_MISSING_SAVE:
8142 /*-----------------------------------------------
8144 ** We received an DISCONNECT message,
8145 ** but the datapointer wasn't saved before.
8147 **-----------------------------------------------
8150 PRINT_ADDR(cp->cmd);
8151 printk ("M_DISCONNECT received, but datapointer not saved: "
8152 "data=%x save=%x goal=%x.\n",
8153 (unsigned) INL (nc_temp),
8154 (unsigned) scr_to_cpu(np->header.savep),
8155 (unsigned) scr_to_cpu(np->header.goalp));
8156 break;
8157 #endif
8160 out:
8161 OUTONB (nc_dcntl, (STD|NOCOM));
8164 /*==========================================================
8167 ** Aquire a control block
8170 **==========================================================
8173 static ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln)
8175 tcb_p tp = &np->target[tn];
8176 lcb_p lp = tp->lp[ln];
8177 u_char tag = NO_TAG;
8178 ccb_p cp = (ccb_p) 0;
8181 ** Lun structure available ?
8183 if (lp) {
8184 XPT_QUEHEAD *qp;
8186 ** Keep from using more tags than we can handle.
8188 if (lp->usetags && lp->busyccbs >= lp->maxnxs)
8189 return (ccb_p) 0;
8192 ** Allocate a new CCB if needed.
8194 if (xpt_que_empty(&lp->free_ccbq))
8195 ncr_alloc_ccb(np, tn, ln);
8198 ** Tune tag mode if asked by user.
8200 if (lp->queuedepth != lp->numtags) {
8201 ncr_setup_tags(np, tn, ln);
8205 ** Look for free CCB
8207 qp = xpt_remque_head(&lp->free_ccbq);
8208 if (qp) {
8209 cp = xpt_que_entry(qp, struct ccb, link_ccbq);
8210 if (cp->magic) {
8211 PRINT_LUN(np, tn, ln);
8212 printk ("ccb free list corrupted (@%p)\n", cp);
8213 cp = 0;
8215 else {
8216 xpt_insque_tail(qp, &lp->wait_ccbq);
8217 ++lp->busyccbs;
8222 ** If a CCB is available,
8223 ** Get a tag for this nexus if required.
8225 if (cp) {
8226 if (lp->usetags)
8227 tag = lp->cb_tags[lp->ia_tag];
8229 else if (lp->actccbs > 0)
8230 return (ccb_p) 0;
8234 ** if nothing available, take the default.
8236 if (!cp)
8237 cp = np->ccb;
8240 ** Wait until available.
8242 #if 0
8243 while (cp->magic) {
8244 if (flags & SCSI_NOSLEEP) break;
8245 if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
8246 break;
8248 #endif
8250 if (cp->magic)
8251 return ((ccb_p) 0);
8253 cp->magic = 1;
8256 ** Move to next available tag if tag used.
8258 if (lp) {
8259 if (tag != NO_TAG) {
8260 ++lp->ia_tag;
8261 if (lp->ia_tag == SCSI_NCR_MAX_TAGS)
8262 lp->ia_tag = 0;
8263 lp->tags_umap |= (((tagmap_t) 1) << tag);
8268 ** Remember all informations needed to free this CCB.
8270 cp->tag = tag;
8271 cp->target = tn;
8272 cp->lun = ln;
8274 if (DEBUG_FLAGS & DEBUG_TAGS) {
8275 PRINT_LUN(np, tn, ln);
8276 printk ("ccb @%p using tag %d.\n", cp, tag);
8279 return cp;
8282 /*==========================================================
8285 ** Release one control block
8288 **==========================================================
8291 static void ncr_free_ccb (ncb_p np, ccb_p cp)
8293 tcb_p tp = &np->target[cp->target];
8294 lcb_p lp = tp->lp[cp->lun];
8296 if (DEBUG_FLAGS & DEBUG_TAGS) {
8297 PRINT_LUN(np, cp->target, cp->lun);
8298 printk ("ccb @%p freeing tag %d.\n", cp, cp->tag);
8302 ** If lun control block available,
8303 ** decrement active commands and increment credit,
8304 ** free the tag if any and remove the JUMP for reselect.
8306 if (lp) {
8307 if (cp->tag != NO_TAG) {
8308 lp->cb_tags[lp->if_tag++] = cp->tag;
8309 if (lp->if_tag == SCSI_NCR_MAX_TAGS)
8310 lp->if_tag = 0;
8311 lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
8312 lp->tags_smap &= lp->tags_umap;
8313 lp->jump_ccb[cp->tag] =
8314 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
8315 } else {
8316 lp->jump_ccb[0] =
8317 cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
8322 ** Make this CCB available.
8325 if (lp) {
8326 if (cp != np->ccb) {
8327 xpt_remque(&cp->link_ccbq);
8328 xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
8330 --lp->busyccbs;
8331 if (cp->queued) {
8332 --lp->queuedccbs;
8335 cp -> host_status = HS_IDLE;
8336 cp -> magic = 0;
8337 if (cp->queued) {
8338 --np->queuedccbs;
8339 cp->queued = 0;
8342 #if 0
8343 if (cp == np->ccb)
8344 wakeup ((caddr_t) cp);
8345 #endif
8349 #define ncr_reg_bus_addr(r) \
8350 (bus_dvma_to_mem(np->paddr) + offsetof (struct ncr_reg, r))
8352 /*------------------------------------------------------------------------
8353 ** Initialize the fixed part of a CCB structure.
8354 **------------------------------------------------------------------------
8355 **------------------------------------------------------------------------
8357 static void ncr_init_ccb(ncb_p np, ccb_p cp)
8359 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
8362 ** Remember virtual and bus address of this ccb.
8364 cp->p_ccb = vtophys(cp);
8365 cp->phys.header.cp = cp;
8368 ** This allows xpt_remque to work for the default ccb.
8370 xpt_que_init(&cp->link_ccbq);
8373 ** Initialyze the start and restart launch script.
8375 ** COPY(4) @(...p_phys), @(dsa)
8376 ** JUMP @(sched_point)
8378 cp->start.setup_dsa[0] = cpu_to_scr(copy_4);
8379 cp->start.setup_dsa[1] = cpu_to_scr(vtophys(&cp->start.p_phys));
8380 cp->start.setup_dsa[2] = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
8381 cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
8382 cp->start.p_phys = cpu_to_scr(vtophys(&cp->phys));
8384 bcopy(&cp->start, &cp->restart, sizeof(cp->restart));
8386 cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
8387 cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
8391 /*------------------------------------------------------------------------
8392 ** Allocate a CCB and initialize its fixed part.
8393 **------------------------------------------------------------------------
8394 **------------------------------------------------------------------------
8396 static void ncr_alloc_ccb(ncb_p np, u_char tn, u_char ln)
8398 tcb_p tp = &np->target[tn];
8399 lcb_p lp = tp->lp[ln];
8400 ccb_p cp = 0;
8403 ** Allocate memory for this CCB.
8405 cp = m_alloc(sizeof(struct ccb), 5);
8406 if (!cp)
8407 return;
8409 if (DEBUG_FLAGS & DEBUG_ALLOC) {
8410 PRINT_LUN(np, tn, ln);
8411 printk ("new ccb @%p.\n", cp);
8415 ** Count it and initialyze it.
8417 lp->actccbs++;
8418 np->actccbs++;
8419 bzero (cp, sizeof (*cp));
8420 ncr_init_ccb(np, cp);
8423 ** Chain into wakeup list and free ccb queue and take it
8424 ** into account for tagged commands.
8426 cp->link_ccb = np->ccb->link_ccb;
8427 np->ccb->link_ccb = cp;
8429 xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
8430 ncr_setup_tags (np, tn, ln);
8433 /*==========================================================
8436 ** Allocation of resources for Targets/Luns/Tags.
8439 **==========================================================
8443 /*------------------------------------------------------------------------
8444 ** Target control block initialisation.
8445 **------------------------------------------------------------------------
8446 ** This data structure is fully initialized after a SCSI command
8447 ** has been successfully completed for this target.
8448 ** It contains a SCRIPT that is called on target reselection.
8449 **------------------------------------------------------------------------
8451 static void ncr_init_tcb (ncb_p np, u_char tn)
8453 tcb_p tp = &np->target[tn];
8454 ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
8455 int th = tn & 3;
8456 int i;
8459 ** Jump to next tcb if SFBR does not match this target.
8460 ** JUMP IF (SFBR != #target#), @(next tcb)
8462 tp->jump_tcb.l_cmd =
8463 cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
8464 tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
8467 ** Load the synchronous transfer register.
8468 ** COPY @(tp->sval), @(sxfer)
8470 tp->getscr[0] = cpu_to_scr(copy_1);
8471 tp->getscr[1] = cpu_to_scr(vtophys (&tp->sval));
8472 tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
8475 ** Load the timing register.
8476 ** COPY @(tp->wval), @(scntl3)
8478 tp->getscr[3] = cpu_to_scr(copy_1);
8479 tp->getscr[4] = cpu_to_scr(vtophys (&tp->wval));
8480 tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
8483 ** Get the IDENTIFY message and the lun.
8484 ** CALL @script(resel_lun)
8486 tp->call_lun.l_cmd = cpu_to_scr(SCR_CALL);
8487 tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
8490 ** Look for the lun control block of this nexus.
8491 ** For i = 0 to 3
8492 ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
8494 for (i = 0 ; i < 4 ; i++) {
8495 tp->jump_lcb[i].l_cmd =
8496 cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
8497 tp->jump_lcb[i].l_paddr =
8498 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
8502 ** Link this target control block to the JUMP chain.
8504 np->jump_tcb[th].l_paddr = cpu_to_scr(vtophys (&tp->jump_tcb));
8507 ** These assert's should be moved at driver initialisations.
8509 assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
8510 offsetof(struct tcb , sval )) &3) == 0);
8511 assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
8512 offsetof(struct tcb , wval )) &3) == 0);
8516 /*------------------------------------------------------------------------
8517 ** Lun control block allocation and initialization.
8518 **------------------------------------------------------------------------
8519 ** This data structure is allocated and initialized after a SCSI
8520 ** command has been successfully completed for this target/lun.
8521 **------------------------------------------------------------------------
8523 static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln)
8525 tcb_p tp = &np->target[tn];
8526 lcb_p lp = tp->lp[ln];
8527 ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
8528 int lh = ln & 3;
8531 ** Already done, return.
8533 if (lp)
8534 return lp;
8537 ** Allocate the lcb.
8539 lp = m_alloc(sizeof(struct lcb), 3);
8540 if (!lp)
8541 goto fail;
8542 bzero(lp, sizeof(*lp));
8543 tp->lp[ln] = lp;
8545 if (DEBUG_FLAGS & DEBUG_ALLOC) {
8546 PRINT_LUN(np, tn, ln);
8547 printk ("new lcb @%p.\n", lp);
8551 ** Initialize the target control block if not yet.
8553 if (!tp->jump_tcb.l_cmd)
8554 ncr_init_tcb(np, tn);
8557 ** Initialize the CCB queue headers.
8559 xpt_que_init(&lp->free_ccbq);
8560 xpt_que_init(&lp->busy_ccbq);
8561 xpt_que_init(&lp->wait_ccbq);
8562 xpt_que_init(&lp->skip_ccbq);
8565 ** Set max CCBs to 1 and use the default 1 entry
8566 ** jump table by default.
8568 lp->maxnxs = 1;
8569 lp->jump_ccb = &lp->jump_ccb_0;
8570 lp->p_jump_ccb = cpu_to_scr(vtophys(lp->jump_ccb));
8573 ** Initilialyze the reselect script:
8575 ** Jump to next lcb if SFBR does not match this lun.
8576 ** Load TEMP with the CCB direct jump table bus address.
8577 ** Get the SIMPLE TAG message and the tag.
8579 ** JUMP IF (SFBR != #lun#), @(next lcb)
8580 ** COPY @(lp->p_jump_ccb), @(temp)
8581 ** JUMP @script(resel_notag)
8583 lp->jump_lcb.l_cmd =
8584 cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
8585 lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
8587 lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
8588 lp->load_jump_ccb[1] = cpu_to_scr(vtophys (&lp->p_jump_ccb));
8589 lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
8591 lp->jump_tag.l_cmd = cpu_to_scr(SCR_JUMP);
8592 lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
8595 ** Link this lun control block to the JUMP chain.
8597 tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtophys (&lp->jump_lcb));
8600 ** Initialize command queuing control.
8602 lp->busyccbs = 1;
8603 lp->queuedccbs = 1;
8604 lp->queuedepth = 1;
8605 fail:
8606 return lp;
8610 /*------------------------------------------------------------------------
8611 ** Lun control block setup on INQUIRY data received.
8612 **------------------------------------------------------------------------
8613 ** We only support WIDE, SYNC for targets and CMDQ for logical units.
8614 ** This setup is done on each INQUIRY since we are expecting user
8615 ** will play with CHANGE DEFINITION commands. :-)
8616 **------------------------------------------------------------------------
8618 static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln, u_char *inq_data)
8620 tcb_p tp = &np->target[tn];
8621 lcb_p lp = tp->lp[ln];
8622 u_char inq_byte7;
8625 ** If no lcb, try to allocate it.
8627 if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
8628 goto fail;
8631 ** Get device quirks from a speciality table.
8633 tp->quirks = ncr_lookup (inq_data);
8634 if (tp->quirks && bootverbose) {
8635 PRINT_LUN(np, tn, ln);
8636 printk ("quirks=%x.\n", tp->quirks);
8640 ** Evaluate trustable target/unit capabilities.
8641 ** We only believe device version >= SCSI-2 that
8642 ** use appropriate response data format (2).
8643 ** But it seems that some CCS devices also
8644 ** support SYNC and I donnot want to frustrate
8645 ** anybody. ;-)
8647 inq_byte7 = 0;
8648 if ((inq_data[2] & 0x7) >= 2 && (inq_data[3] & 0xf) == 2)
8649 inq_byte7 = inq_data[7];
8650 else if ((inq_data[2] & 0x7) == 1 && (inq_data[3] & 0xf) == 1)
8651 inq_byte7 = INQ7_SYNC;
8654 ** Throw away announced LUN capabilities if we are told
8655 ** that there is no real device supported by the logical unit.
8657 if ((inq_data[0] & 0xe0) > 0x20 || (inq_data[0] & 0x1f) == 0x1f)
8658 inq_byte7 &= (INQ7_SYNC | INQ7_WIDE16);
8661 ** If user is wanting SYNC, force this feature.
8663 if (driver_setup.force_sync_nego)
8664 inq_byte7 |= INQ7_SYNC;
8667 ** Prepare negotiation if SIP capabilities have changed.
8669 tp->inq_done = 1;
8670 if ((inq_byte7 ^ tp->inq_byte7) & (INQ7_SYNC | INQ7_WIDE16)) {
8671 tp->inq_byte7 = inq_byte7;
8672 ncr_negotiate(np, tp);
8676 ** If unit supports tagged commands, allocate the
8677 ** CCB JUMP table if not yet.
8679 if ((inq_byte7 & INQ7_QUEUE) && lp->jump_ccb == &lp->jump_ccb_0) {
8680 int i;
8681 lp->jump_ccb = m_alloc(256, 8);
8682 if (!lp->jump_ccb) {
8683 lp->jump_ccb = &lp->jump_ccb_0;
8684 goto fail;
8686 lp->p_jump_ccb = cpu_to_scr(vtophys(lp->jump_ccb));
8687 for (i = 0 ; i < 64 ; i++)
8688 lp->jump_ccb[i] =
8689 cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
8690 for (i = 0 ; i < SCSI_NCR_MAX_TAGS ; i++)
8691 lp->cb_tags[i] = i;
8692 lp->maxnxs = SCSI_NCR_MAX_TAGS;
8693 lp->tags_stime = jiffies;
8697 ** Adjust tagged queueing status if needed.
8699 if ((inq_byte7 ^ lp->inq_byte7) & INQ7_QUEUE) {
8700 lp->inq_byte7 = inq_byte7;
8701 lp->numtags = lp->maxtags;
8702 ncr_setup_tags (np, tn, ln);
8705 fail:
8706 return lp;
8709 /*==========================================================
8712 ** Build Scatter Gather Block
8715 **==========================================================
8717 ** The transfer area may be scattered among
8718 ** several non adjacent physical pages.
8720 ** We may use MAX_SCATTER blocks.
8722 **----------------------------------------------------------
8726 ** We try to reduce the number of interrupts caused
8727 ** by unexpected phase changes due to disconnects.
8728 ** A typical harddisk may disconnect before ANY block.
8729 ** If we wanted to avoid unexpected phase changes at all
8730 ** we had to use a break point every 512 bytes.
8731 ** Of course the number of scatter/gather blocks is
8732 ** limited.
8733 ** Under Linux, the scatter/gatter blocks are provided by
8734 ** the generic driver. We just have to copy addresses and
8735 ** sizes to the data segment array.
8738 static int ncr_scatter(ccb_p cp, Scsi_Cmnd *cmd)
8740 struct scr_tblmove *data;
8741 int segment = 0;
8742 int use_sg = (int) cmd->use_sg;
8744 data = cp->phys.data;
8745 cp->data_len = 0;
8747 if (!use_sg) {
8748 if (cmd->request_bufflen) {
8749 data = &data[MAX_SCATTER - 1];
8750 data[0].addr = cpu_to_scr(vtophys(cmd->request_buffer));
8751 data[0].size = cpu_to_scr(cmd->request_bufflen);
8752 cp->data_len = cmd->request_bufflen;
8753 segment = 1;
8756 else if (use_sg <= MAX_SCATTER) {
8757 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
8759 data = &data[MAX_SCATTER - use_sg];
8760 while (segment < use_sg) {
8761 data[segment].addr =
8762 cpu_to_scr(vtophys(scatter[segment].address));
8763 data[segment].size =
8764 cpu_to_scr(scatter[segment].length);
8765 cp->data_len += scatter[segment].length;
8766 ++segment;
8769 else {
8770 return -1;
8773 return segment;
8776 /*==========================================================
8779 ** Test the pci bus snoop logic :-(
8781 ** Has to be called with interrupts disabled.
8784 **==========================================================
8787 #ifndef NCR_IOMAPPED
8788 static int __init ncr_regtest (struct ncb* np)
8790 register volatile u_int32 data;
8792 ** ncr registers may NOT be cached.
8793 ** write 0xffffffff to a read only register area,
8794 ** and try to read it back.
8796 data = 0xffffffff;
8797 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
8798 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
8799 #if 1
8800 if (data == 0xffffffff) {
8801 #else
8802 if ((data & 0xe2f0fffd) != 0x02000080) {
8803 #endif
8804 printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
8805 (unsigned) data);
8806 return (0x10);
8808 return (0);
8810 #endif
8812 static int __init ncr_snooptest (struct ncb* np)
8814 u_int32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
8815 int i, err=0;
8816 #ifndef NCR_IOMAPPED
8817 if (np->reg) {
8818 err |= ncr_regtest (np);
8819 if (err) return (err);
8821 #endif
8823 ** init
8825 pc = NCB_SCRIPTH_PHYS (np, snooptest);
8826 host_wr = 1;
8827 ncr_wr = 2;
8829 ** Set memory and register.
8831 np->ncr_cache = cpu_to_scr(host_wr);
8832 OUTL (nc_temp, ncr_wr);
8834 ** Start script (exchange values)
8836 OUTL (nc_dsp, pc);
8838 ** Wait 'til done (with timeout)
8840 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
8841 if (INB(nc_istat) & (INTF|SIP|DIP))
8842 break;
8844 ** Save termination position.
8846 pc = INL (nc_dsp);
8848 ** Read memory and register.
8850 host_rd = scr_to_cpu(np->ncr_cache);
8851 ncr_rd = INL (nc_scratcha);
8852 ncr_bk = INL (nc_temp);
8854 ** Reset ncr chip
8856 OUTB (nc_istat, SRST);
8857 UDELAY (100);
8858 OUTB (nc_istat, 0 );
8860 ** check for timeout
8862 if (i>=NCR_SNOOP_TIMEOUT) {
8863 printk ("CACHE TEST FAILED: timeout.\n");
8864 return (0x20);
8867 ** Check termination position.
8869 if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
8870 printk ("CACHE TEST FAILED: script execution failed.\n");
8871 printk ("start=%08lx, pc=%08lx, end=%08lx\n",
8872 (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
8873 (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
8874 return (0x40);
8877 ** Show results.
8879 if (host_wr != ncr_rd) {
8880 printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
8881 (int) host_wr, (int) ncr_rd);
8882 err |= 1;
8884 if (host_rd != ncr_wr) {
8885 printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
8886 (int) ncr_wr, (int) host_rd);
8887 err |= 2;
8889 if (ncr_bk != ncr_wr) {
8890 printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
8891 (int) ncr_wr, (int) ncr_bk);
8892 err |= 4;
8894 return (err);
8897 /*==========================================================
8900 ** Profiling the drivers and targets performance.
8903 **==========================================================
8906 #ifdef SCSI_NCR_PROFILE_SUPPORT
8909 ** Compute the difference in jiffies ticks.
8912 #define ncr_delta(from, to) \
8913 ( ((to) && (from))? (to) - (from) : -1 )
8915 #define PROFILE cp->phys.header.stamp
8916 static void ncb_profile (ncb_p np, ccb_p cp)
8918 long co, st, en, di, re, post, work, disc;
8919 u_int diff;
8921 PROFILE.end = jiffies;
8923 st = ncr_delta (PROFILE.start,PROFILE.status);
8924 if (st<0) return; /* status not reached */
8926 co = ncr_delta (PROFILE.start,PROFILE.command);
8927 if (co<0) return; /* command not executed */
8929 en = ncr_delta (PROFILE.start,PROFILE.end),
8930 di = ncr_delta (PROFILE.start,PROFILE.disconnect),
8931 re = ncr_delta (PROFILE.start,PROFILE.reselect);
8932 post = en - st;
8935 ** @PROFILE@ Disconnect time invalid if multiple disconnects
8938 if (di>=0) disc = re - di; else disc = 0;
8940 work = (st - co) - disc;
8942 diff = (scr_to_cpu(np->disc_phys) - np->disc_ref) & 0xff;
8943 np->disc_ref += diff;
8945 np->profile.num_trans += 1;
8946 if (cp->cmd) {
8947 np->profile.num_kbytes += (cp->cmd->request_bufflen >> 10);
8948 np->profile.rest_bytes += (cp->cmd->request_bufflen & (0x400-1));
8949 if (np->profile.rest_bytes >= 0x400) {
8950 ++np->profile.num_kbytes;
8951 np->profile.rest_bytes -= 0x400;
8954 np->profile.num_disc += diff;
8955 np->profile.ms_setup += co;
8956 np->profile.ms_data += work;
8957 np->profile.ms_disc += disc;
8958 np->profile.ms_post += post;
8960 #undef PROFILE
8962 #endif /* SCSI_NCR_PROFILE_SUPPORT */
8964 /*==========================================================
8967 ** Device lookup.
8969 ** @GENSCSI@ should be integrated to scsiconf.c
8972 **==========================================================
8975 struct table_entry {
8976 char * manufacturer;
8977 char * model;
8978 char * version;
8979 u_long info;
8982 static struct table_entry device_tab[] =
8984 #if 0
8985 {"", "", "", QUIRK_NOMSG},
8986 #endif
8987 {"SONY", "SDT-5000", "3.17", QUIRK_NOMSG},
8988 {"WangDAT", "Model 2600", "01.7", QUIRK_NOMSG},
8989 {"WangDAT", "Model 3200", "02.2", QUIRK_NOMSG},
8990 {"WangDAT", "Model 1300", "02.4", QUIRK_NOMSG},
8991 {"", "", "", 0} /* catch all: must be last entry. */
8994 static u_long ncr_lookup(char * id)
8996 struct table_entry * p = device_tab;
8997 char *d, *r, c;
8999 for (;;p++) {
9001 d = id+8;
9002 r = p->manufacturer;
9003 while ((c=*r++)) if (c!=*d++) break;
9004 if (c) continue;
9006 d = id+16;
9007 r = p->model;
9008 while ((c=*r++)) if (c!=*d++) break;
9009 if (c) continue;
9011 d = id+32;
9012 r = p->version;
9013 while ((c=*r++)) if (c!=*d++) break;
9014 if (c) continue;
9016 return (p->info);
9020 /*==========================================================
9022 ** Determine the ncr's clock frequency.
9023 ** This is essential for the negotiation
9024 ** of the synchronous transfer rate.
9026 **==========================================================
9028 ** Note: we have to return the correct value.
9029 ** THERE IS NO SAVE DEFAULT VALUE.
9031 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
9032 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
9033 ** do not have a clock doubler and so are provided with a
9034 ** 80 MHz clock. All other fast20 boards incorporate a doubler
9035 ** and so should be delivered with a 40 MHz clock.
9036 ** The future fast40 chips (895/895) use a 40 Mhz base clock
9037 ** and provide a clock quadrupler (160 Mhz). The code below
9038 ** tries to deal as cleverly as possible with all this stuff.
9040 **----------------------------------------------------------
9044 * Select NCR SCSI clock frequency
9046 static void ncr_selectclock(ncb_p np, u_char scntl3)
9048 if (np->multiplier < 2) {
9049 OUTB(nc_scntl3, scntl3);
9050 return;
9053 if (bootverbose >= 2)
9054 printk ("%s: enabling clock multiplier\n", ncr_name(np));
9056 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
9057 if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
9058 int i = 20;
9059 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
9060 UDELAY (20);
9061 if (!i)
9062 printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
9063 } else /* Wait 20 micro-seconds for doubler */
9064 UDELAY (20);
9065 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
9066 OUTB(nc_scntl3, scntl3);
9067 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
9068 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
9073 * calculate NCR SCSI clock frequency (in KHz)
9075 static unsigned __init ncrgetfreq (ncb_p np, int gen)
9077 unsigned ms = 0;
9080 * Measure GEN timer delay in order
9081 * to calculate SCSI clock frequency
9083 * This code will never execute too
9084 * many loop iterations (if DELAY is
9085 * reasonably correct). It could get
9086 * too low a delay (too high a freq.)
9087 * if the CPU is slow executing the
9088 * loop for some reason (an NMI, for
9089 * example). For this reason we will
9090 * if multiple measurements are to be
9091 * performed trust the higher delay
9092 * (lower frequency returned).
9094 OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
9095 OUTW (nc_sien , 0); /* mask all scsi interrupts */
9096 (void) INW (nc_sist); /* clear pending scsi interrupt */
9097 OUTB (nc_dien , 0); /* mask all dma interrupts */
9098 (void) INW (nc_sist); /* another one, just to be sure :) */
9099 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
9100 OUTB (nc_stime1, 0); /* disable general purpose timer */
9101 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
9102 while (!(INW(nc_sist) & GEN) && ms++ < 100000)
9103 UDELAY (1000); /* count ms */
9104 OUTB (nc_stime1, 0); /* disable general purpose timer */
9106 * set prescaler to divide by whatever 0 means
9107 * 0 ought to choose divide by 2, but appears
9108 * to set divide by 3.5 mode in my 53c810 ...
9110 OUTB (nc_scntl3, 0);
9112 if (bootverbose >= 2)
9113 printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
9115 * adjust for prescaler, and convert into KHz
9117 return ms ? ((1 << gen) * 4340) / ms : 0;
9121 * Get/probe NCR SCSI clock frequency
9123 static void __init ncr_getclock (ncb_p np, int mult)
9125 unsigned char scntl3 = INB(nc_scntl3);
9126 unsigned char stest1 = INB(nc_stest1);
9127 unsigned f1;
9129 np->multiplier = 1;
9130 f1 = 40000;
9133 ** True with 875 or 895 with clock multiplier selected
9135 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
9136 if (bootverbose >= 2)
9137 printk ("%s: clock multiplier found\n", ncr_name(np));
9138 np->multiplier = mult;
9142 ** If multiplier not found or scntl3 not 7,5,3,
9143 ** reset chip and get frequency from general purpose timer.
9144 ** Otherwise trust scntl3 BIOS setting.
9146 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
9147 unsigned f2;
9149 OUTB(nc_istat, SRST); UDELAY (5); OUTB(nc_istat, 0);
9151 (void) ncrgetfreq (np, 11); /* throw away first result */
9152 f1 = ncrgetfreq (np, 11);
9153 f2 = ncrgetfreq (np, 11);
9155 if (bootverbose)
9156 printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
9158 if (f1 > f2) f1 = f2; /* trust lower result */
9160 if (f1 < 45000) f1 = 40000;
9161 else if (f1 < 55000) f1 = 50000;
9162 else f1 = 80000;
9164 if (f1 < 80000 && mult > 1) {
9165 if (bootverbose >= 2)
9166 printk ("%s: clock multiplier assumed\n", ncr_name(np));
9167 np->multiplier = mult;
9169 } else {
9170 if ((scntl3 & 7) == 3) f1 = 40000;
9171 else if ((scntl3 & 7) == 5) f1 = 80000;
9172 else f1 = 160000;
9174 f1 /= np->multiplier;
9178 ** Compute controller synchronous parameters.
9180 f1 *= np->multiplier;
9181 np->clock_khz = f1;
9184 /*===================== LINUX ENTRY POINTS SECTION ==========================*/
9186 #ifndef uchar
9187 #define uchar unsigned char
9188 #endif
9190 #ifndef ushort
9191 #define ushort unsigned short
9192 #endif
9194 #ifndef ulong
9195 #define ulong unsigned long
9196 #endif
9198 /* ---------------------------------------------------------------------
9200 ** Driver setup from the boot command line
9202 ** ---------------------------------------------------------------------
9205 #ifdef MODULE
9206 #define ARG_SEP ' '
9207 #else
9208 #define ARG_SEP ','
9209 #endif
9212 void __init ncr53c8xx_setup(char *str, int *ints)
9214 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
9215 char *cur = str;
9216 char *pc, *pv;
9217 int val;
9218 int base;
9219 int c;
9220 int xi = 0;
9222 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
9223 char *pe;
9225 val = 0;
9226 pv = pc;
9227 c = *++pv;
9229 if (c == 'n')
9230 val = 0;
9231 else if (c == 'y')
9232 val = 1;
9233 else {
9234 base = 0;
9235 val = (int) simple_strtoul(pv, &pe, base);
9237 if (!strncmp(cur, "tags:", 5)) {
9238 int i;
9239 driver_setup.default_tags = val;
9240 if (pe && *pe == '/') {
9241 i = 0;
9242 while (*pe && *pe != ARG_SEP &&
9243 i < sizeof(driver_setup.tag_ctrl)-1) {
9244 driver_setup.tag_ctrl[i++] = *pe++;
9246 driver_setup.tag_ctrl[i] = '\0';
9249 else if (!strncmp(cur, "mpar:", 5))
9250 driver_setup.master_parity = val;
9251 else if (!strncmp(cur, "spar:", 5))
9252 driver_setup.scsi_parity = val;
9253 else if (!strncmp(cur, "disc:", 5))
9254 driver_setup.disconnection = val;
9255 else if (!strncmp(cur, "specf:", 6))
9256 driver_setup.special_features = val;
9257 else if (!strncmp(cur, "ultra:", 6))
9258 driver_setup.ultra_scsi = val;
9259 else if (!strncmp(cur, "fsn:", 4))
9260 driver_setup.force_sync_nego = val;
9261 else if (!strncmp(cur, "revprob:", 8))
9262 driver_setup.reverse_probe = val;
9263 else if (!strncmp(cur, "sync:", 5))
9264 driver_setup.default_sync = val;
9265 else if (!strncmp(cur, "verb:", 5))
9266 driver_setup.verbose = val;
9267 else if (!strncmp(cur, "debug:", 6))
9268 driver_setup.debug = val;
9269 else if (!strncmp(cur, "burst:", 6))
9270 driver_setup.burst_max = val;
9271 else if (!strncmp(cur, "led:", 4))
9272 driver_setup.led_pin = val;
9273 else if (!strncmp(cur, "wide:", 5))
9274 driver_setup.max_wide = val? 1:0;
9275 else if (!strncmp(cur, "settle:", 7))
9276 driver_setup.settle_delay= val;
9277 else if (!strncmp(cur, "diff:", 5))
9278 driver_setup.diff_support= val;
9279 else if (!strncmp(cur, "irqm:", 5))
9280 driver_setup.irqm = val;
9281 else if (!strncmp(cur, "pcifix:", 7))
9282 driver_setup.pci_fix_up = val;
9283 else if (!strncmp(cur, "buschk:", 7))
9284 driver_setup.bus_check = val;
9285 #ifdef SCSI_NCR_NVRAM_SUPPORT
9286 else if (!strncmp(cur, "nvram:", 6))
9287 driver_setup.use_nvram = val;
9288 #endif
9290 else if (!strncmp(cur, "safe:", 5) && val)
9291 memcpy(&driver_setup, &driver_safe_setup, sizeof(driver_setup));
9292 else if (!strncmp(cur, "excl:", 5)) {
9293 if (xi < SCSI_NCR_MAX_EXCLUDES)
9294 driver_setup.excludes[xi++] = val;
9296 else
9297 printk("ncr53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
9299 if ((cur = strchr(cur, ARG_SEP)) != NULL)
9300 ++cur;
9302 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
9305 static int ncr53c8xx_pci_init(Scsi_Host_Template *tpnt,
9306 uchar bus, uchar device_fn, ncr_device *device);
9309 ** Linux entry point for NCR53C8XX devices detection routine.
9311 ** Called by the middle-level scsi drivers at initialization time,
9312 ** or at module installation.
9314 ** Read the PCI configuration and try to attach each
9315 ** detected NCR board.
9317 ** If NVRAM is present, try to attach boards according to
9318 ** the used defined boot order.
9320 ** Returns the number of boards successfully attached.
9323 static void __init ncr_print_driver_setup(void)
9325 #define YesNo(y) y ? 'y' : 'n'
9326 printk ("ncr53c8xx: setup=disc:%c,specf:%d,ultra:%d,tags:%d,sync:%d,"
9327 "burst:%d,wide:%c,diff:%d,revprob:%c,buschk:0x%x\n",
9328 YesNo(driver_setup.disconnection),
9329 driver_setup.special_features,
9330 driver_setup.ultra_scsi,
9331 driver_setup.default_tags,
9332 driver_setup.default_sync,
9333 driver_setup.burst_max,
9334 YesNo(driver_setup.max_wide),
9335 driver_setup.diff_support,
9336 YesNo(driver_setup.reverse_probe),
9337 driver_setup.bus_check);
9339 printk ("ncr53c8xx: setup=mpar:%c,spar:%c,fsn=%c,verb:%d,debug:0x%x,"
9340 "led:%c,settle:%d,irqm:%d,nvram:0x%x,pcifix:0x%x\n",
9341 YesNo(driver_setup.master_parity),
9342 YesNo(driver_setup.scsi_parity),
9343 YesNo(driver_setup.force_sync_nego),
9344 driver_setup.verbose,
9345 driver_setup.debug,
9346 YesNo(driver_setup.led_pin),
9347 driver_setup.settle_delay,
9348 driver_setup.irqm,
9349 driver_setup.use_nvram,
9350 driver_setup.pci_fix_up);
9351 #undef YesNo
9355 ** NCR53C8XX devices description table and chip ids list.
9358 static ncr_chip ncr_chip_table[] __initdata = SCSI_NCR_CHIP_TABLE;
9359 static ushort ncr_chip_ids[] __initdata = SCSI_NCR_CHIP_IDS;
9361 #ifdef SCSI_NCR_NVRAM_SUPPORT
9362 static int __init
9363 ncr_attach_using_nvram(Scsi_Host_Template *tpnt, int nvram_index, int count, ncr_device device[])
9365 int i, j;
9366 int attach_count = 0;
9367 ncr_nvram *nvram;
9368 ncr_device *devp = 0; /* to shut up gcc */
9370 if (!nvram_index)
9371 return 0;
9373 /* find first Symbios NVRAM if there is one as we need to check it for host boot order */
9374 for (i = 0, nvram_index = -1; i < count; i++) {
9375 devp = &device[i];
9376 nvram = devp->nvram;
9377 if (!nvram)
9378 continue;
9379 if (nvram->type == SCSI_NCR_SYMBIOS_NVRAM) {
9380 if (nvram_index == -1)
9381 nvram_index = i;
9382 #ifdef SCSI_NCR_DEBUG_NVRAM
9383 printk("ncr53c8xx: NVRAM: Symbios format Boot Block, 53c%s, PCI bus %d, device %d, function %d\n",
9384 devp->chip.name, devp->slot.bus,
9385 (int) (devp->slot.device_fn & 0xf8) >> 3,
9386 (int) devp->slot.device_fn & 7);
9387 for (j = 0 ; j < 4 ; j++) {
9388 Symbios_host *h = &nvram->data.Symbios.host[j];
9389 printk("ncr53c8xx: BOOT[%d] device_id=%04x vendor_id=%04x device_fn=%02x io_port=%04x %s\n",
9390 j, h->device_id, h->vendor_id,
9391 h->device_fn, h->io_port,
9392 (h->flags & SYMBIOS_INIT_SCAN_AT_BOOT) ? "SCAN AT BOOT" : "");
9395 else if (nvram->type == SCSI_NCR_TEKRAM_NVRAM) {
9396 /* display Tekram nvram data */
9397 printk("ncr53c8xx: NVRAM: Tekram format data, 53c%s, PCI bus %d, device %d, function %d\n",
9398 devp->chip.name, devp->slot.bus,
9399 (int) (devp->slot.device_fn & 0xf8) >> 3,
9400 (int) devp->slot.device_fn & 7);
9401 #endif
9405 if (nvram_index >= 0 && nvram_index < count)
9406 nvram = device[nvram_index].nvram;
9407 else
9408 nvram = 0;
9410 if (!nvram)
9411 goto out;
9414 ** check devices in the boot record against devices detected.
9415 ** attach devices if we find a match. boot table records that
9416 ** do not match any detected devices will be ignored.
9417 ** devices that do not match any boot table will not be attached
9418 ** here but will attempt to be attached during the device table
9419 ** rescan.
9421 for (i = 0; i < 4; i++) {
9422 Symbios_host *h = &nvram->data.Symbios.host[i];
9423 for (j = 0 ; j < count ; j++) {
9424 devp = &device[j];
9425 if (h->device_fn == devp->slot.device_fn &&
9426 #if 0 /* bus number location in nvram ? */
9427 h->bus == devp->slot.bus &&
9428 #endif
9429 h->device_id == devp->chip.device_id)
9430 break;
9432 if (j < count && !devp->attach_done) {
9433 if (!ncr_attach (tpnt, attach_count, devp))
9434 attach_count++;
9435 devp->attach_done = 1;
9439 out:
9440 return attach_count;
9442 #endif /* SCSI_NCR_NVRAM_SUPPORT */
9444 int __init ncr53c8xx_detect(Scsi_Host_Template *tpnt)
9446 int i, j;
9447 int chips;
9448 int count = 0;
9449 uchar bus, device_fn;
9450 short index;
9451 int attach_count = 0;
9452 ncr_device device[8];
9453 #ifdef SCSI_NCR_NVRAM_SUPPORT
9454 ncr_nvram nvram[4];
9455 int k, nvrams;
9456 #endif
9457 int hosts;
9459 #ifdef SCSI_NCR_NVRAM_SUPPORT
9460 int nvram_index = 0;
9461 #endif
9463 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
9464 ncr_debug = driver_setup.debug;
9465 #endif
9467 tpnt->proc_dir = &proc_scsi_ncr53c8xx;
9468 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
9469 tpnt->proc_info = ncr53c8xx_proc_info;
9470 #endif
9472 #if defined(SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT) && defined(MODULE)
9473 if (ncr53c8xx)
9474 ncr53c8xx_setup(ncr53c8xx, (int *) 0);
9475 #endif
9477 if (initverbose >= 2)
9478 ncr_print_driver_setup();
9481 ** Detect all 53c8xx hosts and then attach them.
9483 ** If we are using NVRAM, once all hosts are detected, we need to check
9484 ** any NVRAM for boot order in case detect and boot order differ and
9485 ** attach them using the order in the NVRAM.
9487 ** If no NVRAM is found or data appears invalid attach boards in the
9488 ** the order they are detected.
9491 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,92)
9492 if (!pci_present())
9493 #else
9494 if (!pcibios_present())
9495 #endif
9496 return 0;
9498 chips = sizeof(ncr_chip_ids) / sizeof(ncr_chip_ids[0]);
9499 hosts = sizeof(device) / sizeof(device[0]);
9500 #ifdef SCSI_NCR_NVRAM_SUPPORT
9501 k = 0;
9502 if (driver_setup.use_nvram & 0x1)
9503 nvrams = sizeof(nvram) / sizeof(nvram[0]);
9504 else
9505 nvrams = 0;
9506 #endif
9508 for (j = 0; j < chips ; ++j) {
9509 i = driver_setup.reverse_probe ? chips-1 - j : j;
9510 for (index = 0; ; index++) {
9511 char *msg = "";
9512 if ((pcibios_find_device(PCI_VENDOR_ID_NCR, ncr_chip_ids[i],
9513 index, &bus, &device_fn)) ||
9514 (count == hosts))
9515 break;
9516 #ifdef SCSI_NCR_NVRAM_SUPPORT
9517 device[count].nvram = k < nvrams ? &nvram[k] : 0;
9518 #else
9519 device[count].nvram = 0;
9520 #endif
9521 if (ncr53c8xx_pci_init(tpnt, bus, device_fn, &device[count])) {
9522 device[count].nvram = 0;
9523 continue;
9525 #ifdef SCSI_NCR_NVRAM_SUPPORT
9526 if (device[count].nvram) {
9527 ++k;
9528 nvram_index |= device[count].nvram->type;
9529 switch (device[count].nvram->type) {
9530 case SCSI_NCR_TEKRAM_NVRAM:
9531 msg = "with Tekram NVRAM";
9532 break;
9533 case SCSI_NCR_SYMBIOS_NVRAM:
9534 msg = "with Symbios NVRAM";
9535 break;
9536 default:
9537 msg = "";
9538 device[count].nvram = 0;
9539 --k;
9542 #endif
9543 printk(KERN_INFO "ncr53c8xx: 53c%s detected %s\n",
9544 device[count].chip.name, msg);
9545 ++count;
9548 #ifdef SCSI_NCR_NVRAM_SUPPORT
9549 attach_count = ncr_attach_using_nvram(tpnt, nvram_index, count, device);
9550 #endif
9552 ** rescan device list to make sure all boards attached.
9553 ** devices without boot records will not be attached yet
9554 ** so try to attach them here.
9556 for (i= 0; i < count; i++) {
9557 if (!device[i].attach_done &&
9558 !ncr_attach (tpnt, attach_count, &device[i])) {
9559 attach_count++;
9563 return attach_count;
9567 ** Generically read a base address from the PCI configuration space.
9568 ** Return the offset immediately after the base address that has
9569 ** been read. Btw, we blindly assume that the high 32 bits of 64 bit
9570 ** base addresses are set to zero on 32 bit architectures.
9571 ** (the pci generic code now does this for us)
9575 static int __init
9576 pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
9578 *base = pdev->resource[index].start;
9579 if ((pdev->resource[index].flags & 0x7) == 0x4)
9580 ++index;
9581 return ++index;
9585 ** Read and check the PCI configuration for any detected NCR
9586 ** boards and save data for attaching after all boards have
9587 ** been detected.
9591 static int __init ncr53c8xx_pci_init(Scsi_Host_Template *tpnt,
9592 uchar bus, uchar device_fn, ncr_device *device)
9594 ushort vendor_id, device_id, command;
9595 uchar cache_line_size, latency_timer;
9596 uchar revision;
9597 #if LINUX_VERSION_CODE > LinuxVersionCode(2,1,92)
9598 struct pci_dev *pdev;
9599 uint irq;
9600 #else
9601 uchar irq;
9602 #endif
9603 ulong base, base_2, io_port;
9604 int i;
9605 #ifdef SCSI_NCR_NVRAM_SUPPORT
9606 ncr_nvram *nvram = device->nvram;
9607 #endif
9608 ncr_chip *chip;
9611 ** Read info from the PCI config space.
9612 ** pcibios_read_config_xxx() functions are assumed to be used for
9613 ** successfully detected PCI devices.
9615 #if LINUX_VERSION_CODE > LinuxVersionCode(2,1,92)
9616 pdev = pci_find_slot(bus, device_fn);
9617 vendor_id = pdev->vendor;
9618 device_id = pdev->device;
9619 irq = pdev->irq;
9620 i = 0;
9621 i = pci_get_base_address(pdev, i, &io_port);
9622 i = pci_get_base_address(pdev, i, &base);
9623 (void) pci_get_base_address(pdev, i, &base_2);
9624 #else
9625 pcibios_read_config_word(bus, device_fn, PCI_VENDOR_ID, &vendor_id);
9626 pcibios_read_config_word(bus, device_fn, PCI_DEVICE_ID, &device_id);
9627 pcibios_read_config_byte(bus, device_fn, PCI_INTERRUPT_LINE, &irq);
9628 i = PCI_BASE_ADDRESS_0;
9629 i = pci_read_base_address(bus, device_fn, i, &io_port);
9630 i = pci_read_base_address(bus, device_fn, i, &base);
9631 (void) pci_read_base_address(bus, device_fn, i, &base_2);
9632 #endif
9633 pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9634 pcibios_read_config_byte(bus, device_fn, PCI_CLASS_REVISION, &revision);
9635 pcibios_read_config_byte(bus, device_fn, PCI_CACHE_LINE_SIZE,
9636 &cache_line_size);
9637 pcibios_read_config_byte(bus, device_fn, PCI_LATENCY_TIMER,
9638 &latency_timer);
9641 ** If user excludes this chip, donnot initialize it.
9643 for (i = 0 ; i < SCSI_NCR_MAX_EXCLUDES ; i++) {
9644 if (driver_setup.excludes[i] ==
9645 (io_port & PCI_BASE_ADDRESS_IO_MASK))
9646 return -1;
9649 * Check if the chip is supported
9651 chip = 0;
9652 for (i = 0; i < sizeof(ncr_chip_table)/sizeof(ncr_chip_table[0]); i++) {
9653 if (device_id != ncr_chip_table[i].device_id)
9654 continue;
9655 if (revision > ncr_chip_table[i].revision_id)
9656 continue;
9657 chip = &device->chip;
9658 memcpy(chip, &ncr_chip_table[i], sizeof(*chip));
9659 chip->revision_id = revision;
9660 break;
9663 #if defined(__i386__)
9665 * Ignore Symbios chips controlled by SISL RAID controller.
9667 if (chip && (base_2 & PCI_BASE_ADDRESS_MEM_MASK)) {
9668 unsigned int ScriptsSize, MagicValue;
9669 vm_offset_t ScriptsRAM;
9671 if (chip->features & FE_RAM8K)
9672 ScriptsSize = 8192;
9673 else
9674 ScriptsSize = 4096;
9676 ScriptsRAM = remap_pci_mem(base_2 & PCI_BASE_ADDRESS_MEM_MASK,
9677 ScriptsSize);
9678 if (ScriptsRAM) {
9679 MagicValue = readl(ScriptsRAM + ScriptsSize - 16);
9680 unmap_pci_mem(ScriptsRAM, ScriptsSize);
9681 if (MagicValue == 0x52414944)
9682 return -1;
9685 #endif
9687 printk(KERN_INFO "ncr53c8xx: at PCI bus %d, device %d, function %d\n",
9688 bus, (int) (device_fn & 0xf8) >> 3, (int) device_fn & 7);
9690 if (!chip) {
9691 printk("ncr53c8xx: not initializing, device not supported\n");
9692 return -1;
9695 #ifdef __powerpc__
9697 * Several fix-up for power/pc.
9698 * Should not be performed by the driver.
9700 if (!(command & PCI_COMMAND_MASTER)) {
9701 printk("ncr53c8xx: attempting to force PCI_COMMAND_MASTER...");
9702 command |= PCI_COMMAND_MASTER;
9703 pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9704 pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9705 if (!(command & PCI_COMMAND_MASTER)) {
9706 printk("failed!\n");
9707 } else {
9708 printk("succeeded.\n");
9712 if (!(command & PCI_COMMAND_IO)) {
9713 printk("ncr53c8xx: attempting to force PCI_COMMAND_IO...");
9714 command |= PCI_COMMAND_IO;
9715 pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9716 pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9717 if (!(command & PCI_COMMAND_IO)) {
9718 printk("failed!\n");
9719 } else {
9720 printk("succeeded.\n");
9724 if (!(command & PCI_COMMAND_MEMORY)) {
9725 printk("ncr53c8xx: attempting to force PCI_COMMAND_MEMORY...");
9726 command |= PCI_COMMAND_MEMORY;
9727 pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9728 pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9729 if (!(command & PCI_COMMAND_MEMORY)) {
9730 printk("failed!\n");
9731 } else {
9732 printk("succeeded.\n");
9737 #if LINUX_VERSION_CODE < LinuxVersionCode(2,1,140)
9738 if ( is_prep ) {
9739 if (io_port >= 0x10000000) {
9740 printk("ncr53c8xx: reallocating io_port (Wacky IBM)");
9741 io_port = (io_port & 0x00FFFFFF) | 0x01000000;
9742 pcibios_write_config_dword(bus, device_fn, PCI_BASE_ADDRESS_0, io_port);
9744 if (base >= 0x10000000) {
9745 printk("ncr53c8xx: reallocating base (Wacky IBM)");
9746 base = (base & 0x00FFFFFF) | 0x01000000;
9747 pcibios_write_config_dword(bus, device_fn, PCI_BASE_ADDRESS_1, base);
9749 if (base_2 >= 0x10000000) {
9750 printk("ncr53c8xx: reallocating base2 (Wacky IBM)");
9751 base_2 = (base_2 & 0x00FFFFFF) | 0x01000000;
9752 pcibios_write_config_dword(bus, device_fn, PCI_BASE_ADDRESS_2, base_2);
9755 #endif
9756 #endif /* __powerpc__ */
9758 #ifdef __sparc__
9760 * Severall fix-ups for sparc.
9762 * Should not be performed by the driver, but how can OBP know
9763 * each and every PCI card, if they don't use Fcode?
9766 if (!(command & PCI_COMMAND_MASTER)) {
9767 if (initverbose >= 2)
9768 printk("ncr53c8xx: setting PCI_COMMAND_MASTER bit (fixup)\n");
9769 command |= PCI_COMMAND_MASTER;
9770 pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9771 pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9774 if ((chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
9775 if (initverbose >= 2)
9776 printk("ncr53c8xx: setting PCI_COMMAND_INVALIDATE bit (fixup)\n");
9777 command |= PCI_COMMAND_INVALIDATE;
9778 pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9779 pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9782 if ((chip->features & FE_CLSE) && !cache_line_size) {
9783 /* PCI_CACHE_LINE_SIZE value is in 32-bit words. */
9784 cache_line_size = 64 / sizeof(u_int32);
9785 if (initverbose >= 2)
9786 printk("ncr53c8xx: setting PCI_CACHE_LINE_SIZE to %d (fixup)\n", cache_line_size);
9787 pcibios_write_config_byte(bus, device_fn,
9788 PCI_CACHE_LINE_SIZE, cache_line_size);
9789 pcibios_read_config_byte(bus, device_fn,
9790 PCI_CACHE_LINE_SIZE, &cache_line_size);
9793 if (!latency_timer) {
9794 latency_timer = 64;
9795 if (initverbose >= 2)
9796 printk("ncr53c8xx: setting PCI_LATENCY_TIMER to %d bus clocks (fixup)\n", latency_timer);
9797 pcibios_write_config_byte(bus, device_fn,
9798 PCI_LATENCY_TIMER, latency_timer);
9799 pcibios_read_config_byte(bus, device_fn,
9800 PCI_LATENCY_TIMER, &latency_timer);
9802 #endif /* __sparc__ */
9805 * Check availability of IO space, memory space and master capability.
9806 * No need to test BARs flags since they are hardwired to the
9807 * expected value.
9809 if (command & PCI_COMMAND_IO)
9810 io_port &= PCI_BASE_ADDRESS_IO_MASK;
9811 else
9812 io_port = 0;
9814 if (command & PCI_COMMAND_MEMORY)
9815 base &= PCI_BASE_ADDRESS_MEM_MASK;
9816 else
9817 base = 0;
9819 if (!io_port && !base) {
9820 printk("ncr53c8xx: not initializing, both I/O and memory mappings disabled\n");
9821 return -1;
9824 base_2 &= PCI_BASE_ADDRESS_MEM_MASK;
9826 if (io_port && check_region (io_port, 128)) {
9827 #ifdef __sparc__
9828 printk("ncr53c8xx: IO region 0x%lx to 0x%lx is in use\n",
9829 io_port, (io_port + 127));
9830 #else
9831 printk("ncr53c8xx: IO region 0x%x to 0x%x is in use\n",
9832 (int) io_port, (int) (io_port + 127));
9833 #endif
9834 return -1;
9837 if (!(command & PCI_COMMAND_MASTER)) {
9838 printk("ncr53c8xx: not initializing, BUS MASTERING was disabled\n");
9839 return -1;
9843 * Fix some features according to driver setup.
9845 if (!(driver_setup.special_features & 1))
9846 chip->features &= ~FE_SPECIAL_SET;
9847 else {
9848 if (driver_setup.special_features & 2)
9849 chip->features &= ~FE_WRIE;
9851 if (driver_setup.ultra_scsi < 2 && (chip->features & FE_ULTRA2)) {
9852 chip->features |= FE_ULTRA;
9853 chip->features &= ~FE_ULTRA2;
9855 if (driver_setup.ultra_scsi < 1)
9856 chip->features &= ~FE_ULTRA;
9857 if (!driver_setup.max_wide)
9858 chip->features &= ~FE_WIDE;
9861 #ifdef SCSI_NCR_PCI_FIX_UP_SUPPORT
9864 * Try to fix up PCI config according to wished features.
9866 #if defined(__i386__) && !defined(MODULE)
9867 if ((driver_setup.pci_fix_up & 1) &&
9868 (chip->features & FE_CLSE) && cache_line_size == 0) {
9869 #if LINUX_VERSION_CODE < LinuxVersionCode(2,1,75)
9870 extern char x86;
9871 switch(x86) {
9872 #else
9873 switch(boot_cpu_data.x86) {
9874 #endif
9875 case 4: cache_line_size = 4; break;
9876 case 6:
9877 case 5: cache_line_size = 8; break;
9879 if (cache_line_size)
9880 (void) pcibios_write_config_byte(bus, device_fn,
9881 PCI_CACHE_LINE_SIZE, cache_line_size);
9882 if (initverbose)
9883 printk("ncr53c8xx: setting PCI_CACHE_LINE_SIZE to %d (fix-up).\n", cache_line_size);
9886 if ((driver_setup.pci_fix_up & 2) && cache_line_size &&
9887 (chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
9888 command |= PCI_COMMAND_INVALIDATE;
9889 (void) pcibios_write_config_word(bus, device_fn,
9890 PCI_COMMAND, command);
9891 if (initverbose)
9892 printk("ncr53c8xx: setting PCI_COMMAND_INVALIDATE bit (fix-up).\n");
9894 #endif
9896 * Fix up for old chips that support READ LINE but not CACHE LINE SIZE.
9897 * - If CACHE LINE SIZE is unknown, set burst max to 32 bytes = 8 dwords
9898 * and donnot enable READ LINE.
9899 * - Otherwise set it to the CACHE LINE SIZE (power of 2 assumed).
9902 if (!(chip->features & FE_CLSE)) {
9903 int burst_max = chip->burst_max;
9904 if (cache_line_size == 0) {
9905 chip->features &= ~FE_ERL;
9906 if (burst_max > 3)
9907 burst_max = 3;
9909 else {
9910 while (cache_line_size < (1 << burst_max))
9911 --burst_max;
9913 chip->burst_max = burst_max;
9917 * Tune PCI LATENCY TIMER according to burst max length transfer.
9918 * (latency timer >= burst length + 6, we add 10 to be quite sure)
9919 * If current value is zero, the device has probably been configured
9920 * for no bursting due to some broken hardware.
9923 if (latency_timer == 0 && chip->burst_max)
9924 printk("ncr53c8xx: PCI_LATENCY_TIMER=0, bursting should'nt be allowed.\n");
9926 if ((driver_setup.pci_fix_up & 4) && chip->burst_max) {
9927 uchar lt = (1 << chip->burst_max) + 6 + 10;
9928 if (latency_timer < lt) {
9929 latency_timer = lt;
9930 if (initverbose)
9931 printk("ncr53c8xx: setting PCI_LATENCY_TIMER to %d bus clocks (fix-up).\n", latency_timer);
9932 (void) pcibios_write_config_byte(bus, device_fn,
9933 PCI_LATENCY_TIMER, latency_timer);
9938 * Fix up for recent chips that support CACHE LINE SIZE.
9939 * If PCI config space is not OK, remove features that shall not be
9940 * used by the chip. No need to trigger possible chip bugs.
9943 if ((chip->features & FE_CLSE) && cache_line_size == 0) {
9944 chip->features &= ~FE_CACHE_SET;
9945 printk("ncr53c8xx: PCI_CACHE_LINE_SIZE not set, features based on CACHE LINE SIZE not used.\n");
9948 if ((chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
9949 chip->features &= ~FE_WRIE;
9950 printk("ncr53c8xx: PCI_COMMAND_INVALIDATE not set, WRITE AND INVALIDATE not used\n");
9953 #endif /* SCSI_NCR_PCI_FIX_UP_SUPPORT */
9955 /* initialise ncr_device structure with items required by ncr_attach */
9956 device->slot.bus = bus;
9957 device->slot.device_fn = device_fn;
9958 device->slot.base = base;
9959 device->slot.base_2 = base_2;
9960 device->slot.io_port = io_port;
9961 device->slot.irq = irq;
9962 device->attach_done = 0;
9963 #ifdef SCSI_NCR_NVRAM_SUPPORT
9964 if (!nvram)
9965 goto out;
9968 ** Get access to chip IO registers
9970 #ifdef NCR_IOMAPPED
9971 request_region(io_port, 128, "ncr53c8xx");
9972 device->slot.port = io_port;
9973 #else
9974 device->slot.reg = (struct ncr_reg *) remap_pci_mem((ulong) base, 128);
9975 if (!device->slot.reg)
9976 goto out;
9977 #endif
9980 ** Try to read SYMBIOS nvram.
9981 ** Data can be used to order booting of boards.
9983 ** Data is saved in ncr_device structure if NVRAM found. This
9984 ** is then used to find drive boot order for ncr_attach().
9986 ** NVRAM data is passed to Scsi_Host_Template later during ncr_attach()
9987 ** for any device set up.
9989 ** Try to read TEKRAM nvram if Symbios nvram not found.
9992 if (!ncr_get_Symbios_nvram(&device->slot, &nvram->data.Symbios))
9993 nvram->type = SCSI_NCR_SYMBIOS_NVRAM;
9994 else if (!ncr_get_Tekram_nvram(&device->slot, &nvram->data.Tekram))
9995 nvram->type = SCSI_NCR_TEKRAM_NVRAM;
9996 else
9997 nvram->type = 0;
9998 out:
10000 ** Release access to chip IO registers
10002 #ifdef NCR_IOMAPPED
10003 release_region(device->slot.port, 128);
10004 #else
10005 unmap_pci_mem((vm_offset_t) device->slot.reg, (u_long) 128);
10006 #endif
10008 #endif /* SCSI_NCR_NVRAM_SUPPORT */
10009 return 0;
10013 ** Linux select queue depths function
10016 #define DEF_DEPTH (driver_setup.default_tags)
10017 #define ALL_TARGETS -2
10018 #define NO_TARGET -1
10019 #define ALL_LUNS -2
10020 #define NO_LUN -1
10022 static int device_queue_depth(ncb_p np, int target, int lun)
10024 int c, h, t, u, v;
10025 char *p = driver_setup.tag_ctrl;
10026 char *ep;
10028 h = -1;
10029 t = NO_TARGET;
10030 u = NO_LUN;
10031 while ((c = *p++) != 0) {
10032 v = simple_strtoul(p, &ep, 0);
10033 switch(c) {
10034 case '/':
10035 ++h;
10036 t = ALL_TARGETS;
10037 u = ALL_LUNS;
10038 break;
10039 case 't':
10040 if (t != target)
10041 t = (target == v) ? v : NO_TARGET;
10042 u = ALL_LUNS;
10043 break;
10044 case 'u':
10045 if (u != lun)
10046 u = (lun == v) ? v : NO_LUN;
10047 break;
10048 case 'q':
10049 if (h == np->unit &&
10050 (t == ALL_TARGETS || t == target) &&
10051 (u == ALL_LUNS || u == lun))
10052 return v;
10053 break;
10054 case '-':
10055 t = ALL_TARGETS;
10056 u = ALL_LUNS;
10057 break;
10058 default:
10059 break;
10061 p = ep;
10063 return DEF_DEPTH;
10066 static void ncr53c8xx_select_queue_depths(struct Scsi_Host *host, struct scsi_device *devlist)
10068 struct scsi_device *device;
10070 for (device = devlist; device; device = device->next) {
10071 ncb_p np;
10072 tcb_p tp;
10073 lcb_p lp;
10074 int numtags;
10076 if (device->host != host)
10077 continue;
10079 np = ((struct host_data *) host->hostdata)->ncb;
10080 tp = &np->target[device->id];
10081 lp = tp->lp[device->lun];
10084 ** Select queue depth from driver setup.
10085 ** Donnot use more than configured by user.
10086 ** Use at least 2.
10087 ** Donnot use more than our maximum.
10089 numtags = device_queue_depth(np, device->id, device->lun);
10090 if (numtags > tp->usrtags)
10091 numtags = tp->usrtags;
10092 if (!device->tagged_supported)
10093 numtags = 1;
10094 device->queue_depth = numtags;
10095 if (device->queue_depth < 2)
10096 device->queue_depth = 2;
10097 if (device->queue_depth > SCSI_NCR_MAX_TAGS)
10098 device->queue_depth = SCSI_NCR_MAX_TAGS;
10101 ** Since the queue depth is not tunable under Linux,
10102 ** we need to know this value in order not to
10103 ** announce stupid things to user.
10105 if (lp) {
10106 lp->numtags = lp->maxtags = numtags;
10107 lp->scdev_depth = device->queue_depth;
10109 ncr_setup_tags (np, device->id, device->lun);
10111 #ifdef DEBUG_NCR53C8XX
10112 printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
10113 np->unit, device->id, device->lun, device->queue_depth);
10114 #endif
10119 ** Linux entry point for info() function
10121 const char *ncr53c8xx_info (struct Scsi_Host *host)
10123 return SCSI_NCR_DRIVER_NAME;
10127 ** Linux entry point of queuecommand() function
10130 int ncr53c8xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
10132 ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
10133 unsigned long flags;
10134 int sts;
10136 #ifdef DEBUG_NCR53C8XX
10137 printk("ncr53c8xx_queue_command\n");
10138 #endif
10140 cmd->scsi_done = done;
10141 cmd->host_scribble = NULL;
10143 NCR_LOCK_NCB(np, flags);
10145 if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
10146 cmd->result = ScsiResult(sts, 0);
10147 #ifdef DEBUG_NCR53C8XX
10148 printk("ncr53c8xx : command not queued - result=%d\n", sts);
10149 #endif
10151 #ifdef DEBUG_NCR53C8XX
10152 else
10153 printk("ncr53c8xx : command successfully queued\n");
10154 #endif
10156 NCR_UNLOCK_NCB(np, flags);
10158 if (sts != DID_OK)
10159 done(cmd);
10161 return sts;
10165 ** Linux entry point of the interrupt handler.
10166 ** Since linux versions > 1.3.70, we trust the kernel for
10167 ** passing the internal host descriptor as 'dev_id'.
10168 ** Otherwise, we scan the host list and call the interrupt
10169 ** routine for each host that uses this IRQ.
10172 static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
10174 unsigned long flags;
10175 ncb_p np = (ncb_p) dev_id;
10176 Scsi_Cmnd *done_list;
10178 #ifdef DEBUG_NCR53C8XX
10179 printk("ncr53c8xx : interrupt received\n");
10180 #endif
10182 if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
10184 NCR_LOCK_NCB(np, flags);
10185 ncr_exception(np);
10186 done_list = np->done_list;
10187 np->done_list = 0;
10188 NCR_UNLOCK_NCB(np, flags);
10190 if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
10192 if (done_list) {
10193 NCR_LOCK_SCSI_DONE(np, flags);
10194 ncr_flush_done_cmds(done_list);
10195 NCR_UNLOCK_SCSI_DONE(np, flags);
10200 ** Linux entry point of the timer handler
10203 static void ncr53c8xx_timeout(unsigned long npref)
10205 ncb_p np = (ncb_p) npref;
10206 unsigned long flags;
10207 Scsi_Cmnd *done_list;
10209 NCR_LOCK_NCB(np, flags);
10210 ncr_timeout((ncb_p) np);
10211 done_list = np->done_list;
10212 np->done_list = 0;
10213 NCR_UNLOCK_NCB(np, flags);
10215 if (done_list) {
10216 NCR_LOCK_SCSI_DONE(np, flags);
10217 ncr_flush_done_cmds(done_list);
10218 NCR_UNLOCK_SCSI_DONE(np, flags);
10223 ** Linux entry point of reset() function
10226 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10227 int ncr53c8xx_reset(Scsi_Cmnd *cmd, unsigned int reset_flags)
10228 #else
10229 int ncr53c8xx_reset(Scsi_Cmnd *cmd)
10230 #endif
10232 ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
10233 int sts;
10234 unsigned long flags;
10235 Scsi_Cmnd *done_list;
10237 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10238 printk("ncr53c8xx_reset: pid=%lu reset_flags=%x serial_number=%ld serial_number_at_timeout=%ld\n",
10239 cmd->pid, reset_flags, cmd->serial_number, cmd->serial_number_at_timeout);
10240 #else
10241 printk("ncr53c8xx_reset: command pid %lu\n", cmd->pid);
10242 #endif
10244 NCR_LOCK_NCB(np, flags);
10247 * We have to just ignore reset requests in some situations.
10249 #if defined SCSI_RESET_NOT_RUNNING
10250 if (cmd->serial_number != cmd->serial_number_at_timeout) {
10251 sts = SCSI_RESET_NOT_RUNNING;
10252 goto out;
10254 #endif
10256 * If the mid-level driver told us reset is synchronous, it seems
10257 * that we must call the done() callback for the involved command,
10258 * even if this command was not queued to the low-level driver,
10259 * before returning SCSI_RESET_SUCCESS.
10262 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10263 sts = ncr_reset_bus(np, cmd,
10264 (reset_flags & (SCSI_RESET_SYNCHRONOUS | SCSI_RESET_ASYNCHRONOUS)) == SCSI_RESET_SYNCHRONOUS);
10265 #else
10266 sts = ncr_reset_bus(np, cmd, 0);
10267 #endif
10270 * Since we always reset the controller, when we return success,
10271 * we add this information to the return code.
10273 #if defined SCSI_RESET_HOST_RESET
10274 if (sts == SCSI_RESET_SUCCESS)
10275 sts |= SCSI_RESET_HOST_RESET;
10276 #endif
10278 out:
10279 done_list = np->done_list;
10280 np->done_list = 0;
10281 NCR_UNLOCK_NCB(np, flags);
10283 ncr_flush_done_cmds(done_list);
10285 return sts;
10289 ** Linux entry point of abort() function
10292 int ncr53c8xx_abort(Scsi_Cmnd *cmd)
10294 ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
10295 int sts;
10296 unsigned long flags;
10297 Scsi_Cmnd *done_list;
10299 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10300 printk("ncr53c8xx_abort: pid=%lu serial_number=%ld serial_number_at_timeout=%ld\n",
10301 cmd->pid, cmd->serial_number, cmd->serial_number_at_timeout);
10302 #else
10303 printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
10304 #endif
10306 NCR_LOCK_NCB(np, flags);
10308 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10310 * We have to just ignore abort requests in some situations.
10312 if (cmd->serial_number != cmd->serial_number_at_timeout) {
10313 sts = SCSI_ABORT_NOT_RUNNING;
10314 goto out;
10316 #endif
10318 sts = ncr_abort_command(np, cmd);
10319 out:
10320 done_list = np->done_list;
10321 np->done_list = 0;
10322 NCR_UNLOCK_NCB(np, flags);
10324 ncr_flush_done_cmds(done_list);
10326 return sts;
10330 #ifdef MODULE
10331 int ncr53c8xx_release(struct Scsi_Host *host)
10333 #ifdef DEBUG_NCR53C8XX
10334 printk("ncr53c8xx : release\n");
10335 #endif
10336 ncr_detach(((struct host_data *) host->hostdata)->ncb);
10338 return 1;
10340 #endif
10344 ** Scsi command waiting list management.
10346 ** It may happen that we cannot insert a scsi command into the start queue,
10347 ** in the following circumstances.
10348 ** Too few preallocated ccb(s),
10349 ** maxtags < cmd_per_lun of the Linux host control block,
10350 ** etc...
10351 ** Such scsi commands are inserted into a waiting list.
10352 ** When a scsi command complete, we try to requeue the commands of the
10353 ** waiting list.
10356 #define next_wcmd host_scribble
10358 static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd)
10360 Scsi_Cmnd *wcmd;
10362 #ifdef DEBUG_WAITING_LIST
10363 printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
10364 #endif
10365 cmd->next_wcmd = 0;
10366 if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
10367 else {
10368 while ((wcmd->next_wcmd) != 0)
10369 wcmd = (Scsi_Cmnd *) wcmd->next_wcmd;
10370 wcmd->next_wcmd = (char *) cmd;
10374 static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd)
10376 Scsi_Cmnd **pcmd = &np->waiting_list;
10378 while (*pcmd) {
10379 if (cmd == *pcmd) {
10380 if (to_remove) {
10381 *pcmd = (Scsi_Cmnd *) cmd->next_wcmd;
10382 cmd->next_wcmd = 0;
10384 #ifdef DEBUG_WAITING_LIST
10385 printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
10386 #endif
10387 return cmd;
10389 pcmd = (Scsi_Cmnd **) &(*pcmd)->next_wcmd;
10391 return 0;
10394 static void process_waiting_list(ncb_p np, int sts)
10396 Scsi_Cmnd *waiting_list, *wcmd;
10398 waiting_list = np->waiting_list;
10399 np->waiting_list = 0;
10401 #ifdef DEBUG_WAITING_LIST
10402 if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
10403 #endif
10404 while ((wcmd = waiting_list) != 0) {
10405 waiting_list = (Scsi_Cmnd *) wcmd->next_wcmd;
10406 wcmd->next_wcmd = 0;
10407 if (sts == DID_OK) {
10408 #ifdef DEBUG_WAITING_LIST
10409 printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
10410 #endif
10411 sts = ncr_queue_command(np, wcmd);
10413 if (sts != DID_OK) {
10414 #ifdef DEBUG_WAITING_LIST
10415 printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
10416 #endif
10417 wcmd->result = ScsiResult(sts, 0);
10418 ncr_queue_done_cmd(np, wcmd);
10423 #undef next_wcmd
10425 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
10427 /*=========================================================================
10428 ** Proc file system stuff
10430 ** A read operation returns profile information.
10431 ** A write operation is a control command.
10432 ** The string is parsed in the driver code and the command is passed
10433 ** to the ncr_usercmd() function.
10434 **=========================================================================
10437 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
10439 #define is_digit(c) ((c) >= '0' && (c) <= '9')
10440 #define digit_to_bin(c) ((c) - '0')
10441 #define is_space(c) ((c) == ' ' || (c) == '\t')
10443 static int skip_spaces(char *ptr, int len)
10445 int cnt, c;
10447 for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
10449 return (len - cnt);
10452 static int get_int_arg(char *ptr, int len, u_long *pv)
10454 int cnt, c;
10455 u_long v;
10457 for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
10458 v = (v * 10) + digit_to_bin(c);
10461 if (pv)
10462 *pv = v;
10464 return (len - cnt);
10467 static int is_keyword(char *ptr, int len, char *verb)
10469 int verb_len = strlen(verb);
10471 if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
10472 return verb_len;
10473 else
10474 return 0;
10478 #define SKIP_SPACES(min_spaces) \
10479 if ((arg_len = skip_spaces(ptr, len)) < (min_spaces)) \
10480 return -EINVAL; \
10481 ptr += arg_len; len -= arg_len;
10483 #define GET_INT_ARG(v) \
10484 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
10485 return -EINVAL; \
10486 ptr += arg_len; len -= arg_len;
10490 ** Parse a control command
10493 static int ncr_user_command(ncb_p np, char *buffer, int length)
10495 char *ptr = buffer;
10496 int len = length;
10497 struct usrcmd *uc = &np->user;
10498 int arg_len;
10499 u_long target;
10501 bzero(uc, sizeof(*uc));
10503 if (len > 0 && ptr[len-1] == '\n')
10504 --len;
10506 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
10507 uc->cmd = UC_SETSYNC;
10508 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
10509 uc->cmd = UC_SETTAGS;
10510 else if ((arg_len = is_keyword(ptr, len, "setorder")) != 0)
10511 uc->cmd = UC_SETORDER;
10512 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
10513 uc->cmd = UC_SETVERBOSE;
10514 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
10515 uc->cmd = UC_SETWIDE;
10516 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
10517 uc->cmd = UC_SETDEBUG;
10518 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
10519 uc->cmd = UC_SETFLAG;
10520 else if ((arg_len = is_keyword(ptr, len, "clearprof")) != 0)
10521 uc->cmd = UC_CLEARPROF;
10522 else
10523 arg_len = 0;
10525 #ifdef DEBUG_PROC_INFO
10526 printk("ncr_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
10527 #endif
10529 if (!arg_len)
10530 return -EINVAL;
10531 ptr += arg_len; len -= arg_len;
10533 switch(uc->cmd) {
10534 case UC_SETSYNC:
10535 case UC_SETTAGS:
10536 case UC_SETWIDE:
10537 case UC_SETFLAG:
10538 SKIP_SPACES(1);
10539 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
10540 ptr += arg_len; len -= arg_len;
10541 uc->target = ~0;
10542 } else {
10543 GET_INT_ARG(target);
10544 uc->target = (1<<target);
10545 #ifdef DEBUG_PROC_INFO
10546 printk("ncr_user_command: target=%ld\n", target);
10547 #endif
10549 break;
10552 switch(uc->cmd) {
10553 case UC_SETVERBOSE:
10554 case UC_SETSYNC:
10555 case UC_SETTAGS:
10556 case UC_SETWIDE:
10557 SKIP_SPACES(1);
10558 GET_INT_ARG(uc->data);
10559 #ifdef DEBUG_PROC_INFO
10560 printk("ncr_user_command: data=%ld\n", uc->data);
10561 #endif
10562 break;
10563 case UC_SETORDER:
10564 SKIP_SPACES(1);
10565 if ((arg_len = is_keyword(ptr, len, "simple")))
10566 uc->data = M_SIMPLE_TAG;
10567 else if ((arg_len = is_keyword(ptr, len, "ordered")))
10568 uc->data = M_ORDERED_TAG;
10569 else if ((arg_len = is_keyword(ptr, len, "default")))
10570 uc->data = 0;
10571 else
10572 return -EINVAL;
10573 break;
10574 case UC_SETDEBUG:
10575 while (len > 0) {
10576 SKIP_SPACES(1);
10577 if ((arg_len = is_keyword(ptr, len, "alloc")))
10578 uc->data |= DEBUG_ALLOC;
10579 else if ((arg_len = is_keyword(ptr, len, "phase")))
10580 uc->data |= DEBUG_PHASE;
10581 else if ((arg_len = is_keyword(ptr, len, "poll")))
10582 uc->data |= DEBUG_POLL;
10583 else if ((arg_len = is_keyword(ptr, len, "queue")))
10584 uc->data |= DEBUG_QUEUE;
10585 else if ((arg_len = is_keyword(ptr, len, "result")))
10586 uc->data |= DEBUG_RESULT;
10587 else if ((arg_len = is_keyword(ptr, len, "scatter")))
10588 uc->data |= DEBUG_SCATTER;
10589 else if ((arg_len = is_keyword(ptr, len, "script")))
10590 uc->data |= DEBUG_SCRIPT;
10591 else if ((arg_len = is_keyword(ptr, len, "tiny")))
10592 uc->data |= DEBUG_TINY;
10593 else if ((arg_len = is_keyword(ptr, len, "timing")))
10594 uc->data |= DEBUG_TIMING;
10595 else if ((arg_len = is_keyword(ptr, len, "nego")))
10596 uc->data |= DEBUG_NEGO;
10597 else if ((arg_len = is_keyword(ptr, len, "tags")))
10598 uc->data |= DEBUG_TAGS;
10599 else if ((arg_len = is_keyword(ptr, len, "freeze")))
10600 uc->data |= DEBUG_FREEZE;
10601 else if ((arg_len = is_keyword(ptr, len, "restart")))
10602 uc->data |= DEBUG_RESTART;
10603 else
10604 return -EINVAL;
10605 ptr += arg_len; len -= arg_len;
10607 #ifdef DEBUG_PROC_INFO
10608 printk("ncr_user_command: data=%ld\n", uc->data);
10609 #endif
10610 break;
10611 case UC_SETFLAG:
10612 while (len > 0) {
10613 SKIP_SPACES(1);
10614 if ((arg_len = is_keyword(ptr, len, "trace")))
10615 uc->data |= UF_TRACE;
10616 else if ((arg_len = is_keyword(ptr, len, "no_disc")))
10617 uc->data |= UF_NODISC;
10618 else
10619 return -EINVAL;
10620 ptr += arg_len; len -= arg_len;
10622 break;
10623 default:
10624 break;
10627 if (len)
10628 return -EINVAL;
10629 else {
10630 long flags;
10632 NCR_LOCK_NCB(np, flags);
10633 ncr_usercmd (np);
10634 NCR_UNLOCK_NCB(np, flags);
10636 return length;
10639 #endif /* SCSI_NCR_USER_COMMAND_SUPPORT */
10641 #ifdef SCSI_NCR_USER_INFO_SUPPORT
10643 struct info_str
10645 char *buffer;
10646 int length;
10647 int offset;
10648 int pos;
10651 static void copy_mem_info(struct info_str *info, char *data, int len)
10653 if (info->pos + len > info->length)
10654 len = info->length - info->pos;
10656 if (info->pos + len < info->offset) {
10657 info->pos += len;
10658 return;
10660 if (info->pos < info->offset) {
10661 data += (info->offset - info->pos);
10662 len -= (info->offset - info->pos);
10665 if (len > 0) {
10666 memcpy(info->buffer + info->pos, data, len);
10667 info->pos += len;
10671 static int copy_info(struct info_str *info, char *fmt, ...)
10673 va_list args;
10674 char buf[81];
10675 int len;
10677 va_start(args, fmt);
10678 len = vsprintf(buf, fmt, args);
10679 va_end(args);
10681 copy_mem_info(info, buf, len);
10682 return len;
10686 ** Copy formatted profile information into the input buffer.
10689 #define to_ms(t) ((t) * 1000 / HZ)
10691 static int ncr_host_info(ncb_p np, char *ptr, off_t offset, int len)
10693 struct info_str info;
10695 info.buffer = ptr;
10696 info.length = len;
10697 info.offset = offset;
10698 info.pos = 0;
10700 copy_info(&info, "General information:\n");
10701 copy_info(&info, " Chip NCR53C%s, ", np->chip_name);
10702 copy_info(&info, "device id 0x%x, ", np->device_id);
10703 copy_info(&info, "revision id 0x%x\n", np->revision_id);
10705 copy_info(&info, " IO port address 0x%lx, ", (u_long) np->port);
10706 #ifdef __sparc__
10707 copy_info(&info, "IRQ number %s\n", __irq_itoa(np->irq));
10708 #else
10709 copy_info(&info, "IRQ number %d\n", (int) np->irq);
10710 #endif
10712 #ifndef NCR_IOMAPPED
10713 if (np->reg)
10714 copy_info(&info, " Using memory mapped IO at virtual address 0x%lx\n",
10715 (u_long) np->reg);
10716 #endif
10717 copy_info(&info, " Synchronous period factor %d, ", (int) np->minsync);
10718 copy_info(&info, "max commands per lun %d\n", SCSI_NCR_MAX_TAGS);
10720 if (driver_setup.debug || driver_setup.verbose > 1) {
10721 copy_info(&info, " Debug flags 0x%x, ", driver_setup.debug);
10722 copy_info(&info, "verbosity level %d\n", driver_setup.verbose);
10725 #ifdef SCSI_NCR_PROFILE_SUPPORT
10726 copy_info(&info, "Profiling information:\n");
10727 copy_info(&info, " %-12s = %lu\n", "num_trans",np->profile.num_trans);
10728 copy_info(&info, " %-12s = %lu\n", "num_kbytes",np->profile.num_kbytes);
10729 copy_info(&info, " %-12s = %lu\n", "num_disc", np->profile.num_disc);
10730 copy_info(&info, " %-12s = %lu\n", "num_break",np->profile.num_break);
10731 copy_info(&info, " %-12s = %lu\n", "num_int", np->profile.num_int);
10732 copy_info(&info, " %-12s = %lu\n", "num_fly", np->profile.num_fly);
10733 copy_info(&info, " %-12s = %lu\n", "ms_setup", to_ms(np->profile.ms_setup));
10734 copy_info(&info, " %-12s = %lu\n", "ms_data", to_ms(np->profile.ms_data));
10735 copy_info(&info, " %-12s = %lu\n", "ms_disc", to_ms(np->profile.ms_disc));
10736 copy_info(&info, " %-12s = %lu\n", "ms_post", to_ms(np->profile.ms_post));
10737 #endif
10739 return info.pos > info.offset? info.pos - info.offset : 0;
10742 #endif /* SCSI_NCR_USER_INFO_SUPPORT */
10745 ** Entry point of the scsi proc fs of the driver.
10746 ** - func = 0 means read (returns profile data)
10747 ** - func = 1 means write (parse user control command)
10750 static int ncr53c8xx_proc_info(char *buffer, char **start, off_t offset,
10751 int length, int hostno, int func)
10753 struct Scsi_Host *host;
10754 struct host_data *host_data;
10755 ncb_p ncb = 0;
10756 int retv;
10758 #ifdef DEBUG_PROC_INFO
10759 printk("ncr53c8xx_proc_info: hostno=%d, func=%d\n", hostno, func);
10760 #endif
10762 for (host = first_host; host; host = host->next) {
10763 if (host->hostt == the_template && host->host_no == hostno) {
10764 host_data = (struct host_data *) host->hostdata;
10765 ncb = host_data->ncb;
10766 break;
10770 if (!ncb)
10771 return -EINVAL;
10773 if (func) {
10774 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
10775 retv = ncr_user_command(ncb, buffer, length);
10776 #else
10777 retv = -EINVAL;
10778 #endif
10780 else {
10781 if (start)
10782 *start = buffer;
10783 #ifdef SCSI_NCR_USER_INFO_SUPPORT
10784 retv = ncr_host_info(ncb, buffer, offset, length);
10785 #else
10786 retv = -EINVAL;
10787 #endif
10790 return retv;
10794 /*=========================================================================
10795 ** End of proc file system stuff
10796 **=========================================================================
10798 #endif
10801 #ifdef SCSI_NCR_NVRAM_SUPPORT
10803 /* ---------------------------------------------------------------------
10805 ** Try reading Symbios format nvram
10807 ** ---------------------------------------------------------------------
10809 ** GPOI0 - data in/data out
10810 ** GPIO1 - clock
10812 ** return 0 if NVRAM data OK, 1 if NVRAM data not OK
10813 ** ---------------------------------------------------------------------
10816 #define SET_BIT 0
10817 #define CLR_BIT 1
10818 #define SET_CLK 2
10819 #define CLR_CLK 3
10821 static u_short nvram_read_data(ncr_slot *np, u_char *data, int len, u_char *gpreg, u_char *gpcntl);
10822 static void nvram_start(ncr_slot *np, u_char *gpreg);
10823 static void nvram_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data, u_char *gpreg, u_char *gpcntl);
10824 static void nvram_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data, u_char *gpreg, u_char *gpcntl);
10825 static void nvram_readAck(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl);
10826 static void nvram_writeAck(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl);
10827 static void nvram_doBit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg);
10828 static void nvram_stop(ncr_slot *np, u_char *gpreg);
10829 static void nvram_setBit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode);
10831 static int __init ncr_get_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
10833 static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
10834 u_char gpcntl, gpreg;
10835 u_char old_gpcntl, old_gpreg;
10836 u_short csum;
10837 u_char ack_data;
10838 int retv = 1;
10840 /* save current state of GPCNTL and GPREG */
10841 old_gpreg = INB (nc_gpreg);
10842 old_gpcntl = INB (nc_gpcntl);
10843 gpcntl = old_gpcntl & 0xfc;
10845 /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
10846 OUTB (nc_gpreg, old_gpreg);
10847 OUTB (nc_gpcntl, gpcntl);
10849 /* this is to set NVRAM into a known state with GPIO0/1 both low */
10850 gpreg = old_gpreg;
10851 nvram_setBit(np, 0, &gpreg, CLR_CLK);
10852 nvram_setBit(np, 0, &gpreg, CLR_BIT);
10854 /* now set NVRAM inactive with GPIO0/1 both high */
10855 nvram_stop(np, &gpreg);
10857 /* activate NVRAM */
10858 nvram_start(np, &gpreg);
10860 /* write device code and random address MSB */
10861 nvram_write_byte(np, &ack_data,
10862 0xa0 | ((SYMBIOS_NVRAM_ADDRESS >> 7) & 0x0e), &gpreg, &gpcntl);
10863 if (ack_data & 0x01)
10864 goto out;
10866 /* write random address LSB */
10867 nvram_write_byte(np, &ack_data,
10868 (SYMBIOS_NVRAM_ADDRESS & 0x7f) << 1, &gpreg, &gpcntl);
10869 if (ack_data & 0x01)
10870 goto out;
10872 /* regenerate START state to set up for reading */
10873 nvram_start(np, &gpreg);
10875 /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
10876 nvram_write_byte(np, &ack_data,
10877 0xa1 | ((SYMBIOS_NVRAM_ADDRESS >> 7) & 0x0e), &gpreg, &gpcntl);
10878 if (ack_data & 0x01)
10879 goto out;
10881 /* now set up GPIO0 for inputting data */
10882 gpcntl |= 0x01;
10883 OUTB (nc_gpcntl, gpcntl);
10885 /* input all active data - only part of total NVRAM */
10886 csum = nvram_read_data(np,
10887 (u_char *) nvram, sizeof(*nvram), &gpreg, &gpcntl);
10889 /* finally put NVRAM back in inactive mode */
10890 gpcntl &= 0xfe;
10891 OUTB (nc_gpcntl, gpcntl);
10892 nvram_stop(np, &gpreg);
10894 #ifdef SCSI_NCR_DEBUG_NVRAM
10895 printk("ncr53c8xx: NvRAM marker=%x trailer=%x %x %x %x %x %x byte_count=%d/%d checksum=%x/%x\n",
10896 nvram->start_marker,
10897 nvram->trailer[0], nvram->trailer[1], nvram->trailer[2],
10898 nvram->trailer[3], nvram->trailer[4], nvram->trailer[5],
10899 nvram->byte_count, sizeof(*nvram) - 12,
10900 nvram->checksum, csum);
10901 #endif
10903 /* check valid NVRAM signature, verify byte count and checksum */
10904 if (nvram->start_marker == 0 &&
10905 !memcmp(nvram->trailer, Symbios_trailer, 6) &&
10906 nvram->byte_count == sizeof(*nvram) - 12 &&
10907 csum == nvram->checksum)
10908 retv = 0;
10909 out:
10910 /* return GPIO0/1 to original states after having accessed NVRAM */
10911 OUTB (nc_gpcntl, old_gpcntl);
10912 OUTB (nc_gpreg, old_gpreg);
10914 return retv;
10918 * Read Symbios NvRAM data and compute checksum.
10920 static u_short __init nvram_read_data(ncr_slot *np, u_char *data, int len,
10921 u_char *gpreg, u_char *gpcntl)
10923 int x;
10924 u_short csum;
10926 for (x = 0; x < len; x++)
10927 nvram_read_byte(np, &data[x], (x == (len - 1)), gpreg, gpcntl);
10929 for (x = 6, csum = 0; x < len - 6; x++)
10930 csum += data[x];
10932 return csum;
10936 * Send START condition to NVRAM to wake it up.
10938 static void __init nvram_start(ncr_slot *np, u_char *gpreg)
10940 nvram_setBit(np, 1, gpreg, SET_BIT);
10941 nvram_setBit(np, 0, gpreg, SET_CLK);
10942 nvram_setBit(np, 0, gpreg, CLR_BIT);
10943 nvram_setBit(np, 0, gpreg, CLR_CLK);
10947 * WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
10948 * GPIO0 must already be set as an output
10950 static void __init nvram_write_byte(ncr_slot *np, u_char *ack_data,
10951 u_char write_data, u_char *gpreg,
10952 u_char *gpcntl)
10954 int x;
10956 for (x = 0; x < 8; x++)
10957 nvram_doBit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
10959 nvram_readAck(np, ack_data, gpreg, gpcntl);
10963 * READ a byte from the NVRAM and then send an ACK to say we have got it,
10964 * GPIO0 must already be set as an input
10966 static void __init nvram_read_byte(ncr_slot *np, u_char *read_data,
10967 u_char ack_data, u_char *gpreg,
10968 u_char *gpcntl)
10970 int x;
10971 u_char read_bit;
10973 *read_data = 0;
10974 for (x = 0; x < 8; x++) {
10975 nvram_doBit(np, &read_bit, 1, gpreg);
10976 *read_data |= ((read_bit & 0x01) << (7 - x));
10979 nvram_writeAck(np, ack_data, gpreg, gpcntl);
10983 * Output an ACK to the NVRAM after reading,
10984 * change GPIO0 to output and when done back to an input
10986 static void __init nvram_writeAck(ncr_slot *np, u_char write_bit,
10987 u_char *gpreg, u_char *gpcntl)
10989 OUTB (nc_gpcntl, *gpcntl & 0xfe);
10990 nvram_doBit(np, 0, write_bit, gpreg);
10991 OUTB (nc_gpcntl, *gpcntl);
10995 * Input an ACK from NVRAM after writing,
10996 * change GPIO0 to input and when done back to an output
10998 static void __init nvram_readAck(ncr_slot *np, u_char *read_bit,
10999 u_char *gpreg, u_char *gpcntl)
11001 OUTB (nc_gpcntl, *gpcntl | 0x01);
11002 nvram_doBit(np, read_bit, 1, gpreg);
11003 OUTB (nc_gpcntl, *gpcntl);
11007 * Read or write a bit to the NVRAM,
11008 * read if GPIO0 input else write if GPIO0 output
11010 static void __init nvram_doBit(ncr_slot *np, u_char *read_bit,
11011 u_char write_bit, u_char *gpreg)
11013 nvram_setBit(np, write_bit, gpreg, SET_BIT);
11014 nvram_setBit(np, 0, gpreg, SET_CLK);
11015 if (read_bit)
11016 *read_bit = INB (nc_gpreg);
11017 nvram_setBit(np, 0, gpreg, CLR_CLK);
11018 nvram_setBit(np, 0, gpreg, CLR_BIT);
11022 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
11024 static void __init nvram_stop(ncr_slot *np, u_char *gpreg)
11026 nvram_setBit(np, 0, gpreg, SET_CLK);
11027 nvram_setBit(np, 1, gpreg, SET_BIT);
11031 * Set/clear data/clock bit in GPIO0
11033 static void __init nvram_setBit(ncr_slot *np, u_char write_bit,
11034 u_char *gpreg, int bit_mode)
11036 UDELAY (5);
11037 switch (bit_mode){
11038 case SET_BIT:
11039 *gpreg |= write_bit;
11040 break;
11041 case CLR_BIT:
11042 *gpreg &= 0xfe;
11043 break;
11044 case SET_CLK:
11045 *gpreg |= 0x02;
11046 break;
11047 case CLR_CLK:
11048 *gpreg &= 0xfd;
11049 break;
11052 OUTB (nc_gpreg, *gpreg);
11053 UDELAY (5);
11056 #undef SET_BIT 0
11057 #undef CLR_BIT 1
11058 #undef SET_CLK 2
11059 #undef CLR_CLK 3
11062 /* ---------------------------------------------------------------------
11064 ** Try reading Tekram format nvram
11066 ** ---------------------------------------------------------------------
11068 ** GPOI0 - data in
11069 ** GPIO1 - data out
11070 ** GPIO2 - clock
11071 ** GPIO4 - chip select
11073 ** return 0 if NVRAM data OK, 1 if NVRAM data not OK
11074 ** ---------------------------------------------------------------------
11077 static u_short Tnvram_read_data(ncr_slot *np, u_short *data, int len, u_char *gpreg);
11078 static void Tnvram_Send_Command(ncr_slot *np, u_short write_data, u_char *read_bit, u_char *gpreg);
11079 static void Tnvram_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg);
11080 static void Tnvram_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg);
11081 static void Tnvram_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg);
11082 static void Tnvram_Stop(ncr_slot *np, u_char *gpreg);
11083 static void Tnvram_Clk(ncr_slot *np, u_char *gpreg);
11085 static int __init ncr_get_Tekram_nvram (ncr_slot *np, Tekram_nvram *nvram)
11087 u_char gpcntl, gpreg;
11088 u_char old_gpcntl, old_gpreg;
11089 u_short csum;
11091 /* save current state of GPCNTL and GPREG */
11092 old_gpreg = INB (nc_gpreg);
11093 old_gpcntl = INB (nc_gpcntl);
11095 /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
11096 1/2/4 out */
11097 gpreg = old_gpreg & 0xe9;
11098 OUTB (nc_gpreg, gpreg);
11099 gpcntl = (old_gpcntl & 0xe9) | 0x09;
11100 OUTB (nc_gpcntl, gpcntl);
11102 /* input all of NVRAM, 64 words */
11103 csum = Tnvram_read_data(np, (u_short *) nvram,
11104 sizeof(*nvram) / sizeof(short), &gpreg);
11106 /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
11107 OUTB (nc_gpcntl, old_gpcntl);
11108 OUTB (nc_gpreg, old_gpreg);
11110 /* check data valid */
11111 if (csum != 0x1234)
11112 return 1;
11114 return 0;
11118 * Read Tekram NvRAM data and compute checksum.
11120 static u_short __init Tnvram_read_data(ncr_slot *np, u_short *data, int len,
11121 u_char *gpreg)
11123 u_char read_bit;
11124 u_short csum;
11125 int x;
11127 for (x = 0, csum = 0; x < len; x++) {
11129 /* output read command and address */
11130 Tnvram_Send_Command(np, 0x180 | x, &read_bit, gpreg);
11131 if (read_bit & 0x01)
11132 return 0; /* Force bad checksum */
11134 Tnvram_Read_Word(np, &data[x], gpreg);
11135 csum += data[x];
11137 Tnvram_Stop(np, gpreg);
11140 return csum;
11144 * Send read command and address to NVRAM
11146 static void __init Tnvram_Send_Command(ncr_slot *np, u_short write_data,
11147 u_char *read_bit, u_char *gpreg)
11149 int x;
11151 /* send 9 bits, start bit (1), command (2), address (6) */
11152 for (x = 0; x < 9; x++)
11153 Tnvram_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
11155 *read_bit = INB (nc_gpreg);
11159 * READ a byte from the NVRAM
11161 static void __init Tnvram_Read_Word(ncr_slot *np, u_short *nvram_data,
11162 u_char *gpreg)
11164 int x;
11165 u_char read_bit;
11167 *nvram_data = 0;
11168 for (x = 0; x < 16; x++) {
11169 Tnvram_Read_Bit(np, &read_bit, gpreg);
11171 if (read_bit & 0x01)
11172 *nvram_data |= (0x01 << (15 - x));
11173 else
11174 *nvram_data &= ~(0x01 << (15 - x));
11179 * Read bit from NVRAM
11181 static void __init Tnvram_Read_Bit(ncr_slot *np, u_char *read_bit,
11182 u_char *gpreg)
11184 UDELAY (2);
11185 Tnvram_Clk(np, gpreg);
11186 *read_bit = INB (nc_gpreg);
11190 * Write bit to GPIO0
11192 static void __init Tnvram_Write_Bit(ncr_slot *np, u_char write_bit,
11193 u_char *gpreg)
11195 if (write_bit & 0x01)
11196 *gpreg |= 0x02;
11197 else
11198 *gpreg &= 0xfd;
11200 *gpreg |= 0x10;
11202 OUTB (nc_gpreg, *gpreg);
11203 UDELAY (2);
11205 Tnvram_Clk(np, gpreg);
11209 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
11211 static void __init Tnvram_Stop(ncr_slot *np, u_char *gpreg)
11213 *gpreg &= 0xef;
11214 OUTB (nc_gpreg, *gpreg);
11215 UDELAY (2);
11217 Tnvram_Clk(np, gpreg);
11221 * Pulse clock bit in GPIO0
11223 static void __init Tnvram_Clk(ncr_slot *np, u_char *gpreg)
11225 OUTB (nc_gpreg, *gpreg | 0x04);
11226 UDELAY (2);
11227 OUTB (nc_gpreg, *gpreg);
11230 #endif /* SCSI_NCR_NVRAM_SUPPORT */
11233 ** Module stuff
11236 #ifdef MODULE
11237 Scsi_Host_Template driver_template = NCR53C8XX;
11238 #include "scsi_module.c"
11239 #endif