OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [scsi/] [ncr53c8xx.c] - Blame information for rev 1772

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/******************************************************************************
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.
10
**
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.
15
**
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.
19
**
20
**-----------------------------------------------------------------------------
21
**
22
**  This driver has been ported to Linux from the FreeBSD NCR53C8XX driver
23
**  and is currently maintained by
24
**
25
**          Gerard Roudier              <groudier@club-internet.fr>
26
**
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.
31
**
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>
35
**
36
**  And has been ported to NetBSD by
37
**          Charles M. Hannum           <mycroft@gnu.ai.mit.edu>
38
**
39
**-----------------------------------------------------------------------------
40
**
41
**                     Brief history
42
**
43
**  December 10 1995 by Gerard Roudier:
44
**     Initial port to Linux.
45
**
46
**  June 23 1996 by Gerard Roudier:
47
**     Support for 64 bits architectures (Alpha).
48
**
49
**  November 30 1996 by Gerard Roudier:
50
**     Support for Fast-20 scsi.
51
**     Support for large DMA fifo and 128 dwords bursting.
52
**
53
**  February 27 1997 by Gerard Roudier:
54
**     Support for Fast-40 scsi.
55
**     Support for on-Board RAM.
56
**
57
**  May 3 1997 by Gerard Roudier:
58
**     Full support for scsi scripts instructions pre-fetching.
59
**
60
**  May 19 1997 by Richard Waltham <dormouse@farsrobt.demon.co.uk>:
61
**     Support for NvRAM detection and reading.
62
**
63
**  August 18 1997 by Cort <cort@cs.nmt.edu>:
64
**     Support for Power/PC (Big Endian).
65
**
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.
71
**
72
*******************************************************************************
73
*/
74
 
75
/*
76
**      December 14 1998, version 3.1e
77
**
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...
85
**
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)
94
**
95
**      Other features:
96
**              Memory mapped IO (linux-1.3.X and above only)
97
**              Module
98
**              Shared IRQ (since linux-1.3.72)
99
*/
100
 
101
#define SCSI_NCR_DEBUG_FLAGS    (0)
102
 
103
/*==========================================================
104
**
105
**      Include files
106
**
107
**==========================================================
108
*/
109
 
110
#define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
111
 
112
#ifdef MODULE
113
#include <linux/module.h>
114
#endif
115
 
116
#include <asm/dma.h>
117
#include <asm/io.h>
118
#include <asm/system.h>
119
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
120
#include <asm/spinlock.h>
121
#endif
122
#include <linux/delay.h>
123
#include <linux/signal.h>
124
#include <linux/sched.h>
125
#include <linux/errno.h>
126
#include <linux/pci.h>
127
#include <linux/string.h>
128
#include <linux/malloc.h>
129
#include <linux/mm.h>
130
#include <linux/ioport.h>
131
#include <linux/time.h>
132
#include <linux/timer.h>
133
#include <linux/stat.h>
134
 
135
#include <linux/version.h>
136
#include <linux/blk.h>
137
 
138
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,35)
139
#include <linux/init.h>
140
#else
141
#ifndef __initdata
142
#define __initdata
143
#endif
144
#ifndef __initfunc
145
#define __initfunc(__arginit) __arginit
146
#endif
147
#endif
148
 
149
#if LINUX_VERSION_CODE <= LinuxVersionCode(2,1,92)
150
#include <linux/bios32.h>
151
#endif
152
 
153
#include "scsi.h"
154
#include "hosts.h"
155
#include "constants.h"
156
#include "sd.h"
157
 
158
#include <linux/types.h>
159
 
160
/*
161
**      Define BITS_PER_LONG for earlier linux versions.
162
*/
163
#ifndef BITS_PER_LONG
164
#if (~0UL) == 0xffffffffUL
165
#define BITS_PER_LONG   32
166
#else
167
#define BITS_PER_LONG   64
168
#endif
169
#endif
170
 
171
/*
172
**      Define the BSD style u_int32 and u_int64 type.
173
**      Are in fact u_int32_t and u_int64_t :-)
174
*/
175
typedef u32 u_int32;
176
typedef u64 u_int64;
177
 
178
#include "ncr53c8xx.h"
179
 
180
/*==========================================================
181
**
182
**      A la VMS/CAM-3 queue management.
183
**      Implemented from linux list management.
184
**
185
**==========================================================
186
*/
187
 
188
typedef struct xpt_quehead {
189
        struct xpt_quehead *flink;      /* Forward  pointer */
190
        struct xpt_quehead *blink;      /* Backward pointer */
191
} XPT_QUEHEAD;
192
 
193
#define xpt_que_init(ptr) do { \
194
        (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
195
} while (0)
196
 
197
static inline void __xpt_que_add(struct xpt_quehead * new,
198
        struct xpt_quehead * blink,
199
        struct xpt_quehead * flink)
200
{
201
        flink->blink    = new;
202
        new->flink      = flink;
203
        new->blink      = blink;
204
        blink->flink    = new;
205
}
206
 
207
static inline void __xpt_que_del(struct xpt_quehead * blink,
208
        struct xpt_quehead * flink)
209
{
210
        flink->blink = blink;
211
        blink->flink = flink;
212
}
213
 
214
static inline int xpt_que_empty(struct xpt_quehead *head)
215
{
216
        return head->flink == head;
217
}
218
 
219
static inline void xpt_que_splice(struct xpt_quehead *list,
220
        struct xpt_quehead *head)
221
{
222
        struct xpt_quehead *first = list->flink;
223
 
224
        if (first != list) {
225
                struct xpt_quehead *last = list->blink;
226
                struct xpt_quehead *at   = head->flink;
227
 
228
                first->blink = head;
229
                head->flink  = first;
230
 
231
                last->flink = at;
232
                at->blink   = last;
233
        }
234
}
235
 
236
#define xpt_que_entry(ptr, type, member) \
237
        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
238
 
239
 
240
#define xpt_insque(new, pos)            __xpt_que_add(new, pos, (pos)->flink)
241
 
242
#define xpt_remque(el)                  __xpt_que_del((el)->blink, (el)->flink)
243
 
244
#define xpt_insque_head(new, head)      __xpt_que_add(new, head, (head)->flink)
245
 
246
static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
247
{
248
        struct xpt_quehead *elem = head->flink;
249
 
250
        if (elem != head)
251
                __xpt_que_del(head, elem->flink);
252
        else
253
                elem = 0;
254
        return elem;
255
}
256
 
257
#define xpt_insque_tail(new, head)      __xpt_que_add(new, (head)->blink, head)
258
 
259
static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
260
{
261
        struct xpt_quehead *elem = head->blink;
262
 
263
        if (elem != head)
264
                __xpt_que_del(elem->blink, head);
265
        else
266
                elem = 0;
267
        return elem;
268
}
269
 
270
/*==========================================================
271
**
272
**      The CCB done queue uses an array of CCB virtual
273
**      addresses. Empty entries are flagged using the bogus
274
**      virtual address 0xffffffff.
275
**
276
**      Since PCI ensures that only aligned DWORDs are accessed
277
**      atomically, 64 bit little-endian architecture requires
278
**      to test the high order DWORD of the entry to determine
279
**      if it is empty or valid.
280
**
281
**      BTW, I will make things differently as soon as I will
282
**      have a better idea, but this is simple and should work.
283
**
284
**==========================================================
285
*/
286
 
287
#define SCSI_NCR_CCB_DONE_SUPPORT
288
#ifdef  SCSI_NCR_CCB_DONE_SUPPORT
289
 
290
#define MAX_DONE 24
291
#define CCB_DONE_EMPTY 0xffffffffUL
292
 
293
/* All 32 bit architectures */
294
#if BITS_PER_LONG == 32
295
#define CCB_DONE_VALID(cp)  (((u_long) cp) != CCB_DONE_EMPTY)
296
 
297
/* All > 32 bit (64 bit) architectures regardless endian-ness */
298
#else
299
#define CCB_DONE_VALID(cp)  \
300
        ((((u_long) cp) & 0xffffffff00000000ul) &&      \
301
         (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
302
#endif
303
 
304
#endif /* SCSI_NCR_CCB_DONE_SUPPORT */
305
 
306
/*==========================================================
307
**
308
**      On x86 architecture, write buffers management does
309
**      not reorder writes to memory. So, using compiler
310
**      optimization barriers is enough to guarantee some
311
**      ordering when the CPU is writing data accessed by
312
**      the NCR.
313
**      On Alpha architecture, explicit memory barriers have
314
**      to be used.
315
**      Other architectures are defaulted to mb() macro if
316
**      defined, otherwise use compiler barrier.
317
**
318
**==========================================================
319
*/
320
 
321
#if defined(__i386__)
322
#define MEMORY_BARRIER()        barrier()
323
#elif defined(__alpha__)
324
#define MEMORY_BARRIER()        mb()
325
#else
326
#  ifdef mb
327
#  define MEMORY_BARRIER()      mb()
328
#  else
329
#  define MEMORY_BARRIER()      barrier()
330
#  endif
331
#endif
332
 
333
/*==========================================================
334
**
335
**      Configuration and Debugging
336
**
337
**==========================================================
338
*/
339
 
340
/*
341
**    SCSI address of this device.
342
**    The boot routines should have set it.
343
**    If not, use this.
344
*/
345
 
346
#ifndef SCSI_NCR_MYADDR
347
#define SCSI_NCR_MYADDR      (7)
348
#endif
349
 
350
/*
351
**    The maximum number of tags per logic unit.
352
**    Used only for disk devices that support tags.
353
*/
354
 
355
#ifndef SCSI_NCR_MAX_TAGS
356
#define SCSI_NCR_MAX_TAGS    (8)
357
#endif
358
 
359
/*
360
**    TAGS are actually limited to 64 tags/lun.
361
**    We need to deal with power of 2, for alignment constraints.
362
*/
363
#if     SCSI_NCR_MAX_TAGS > 64
364
#undef  SCSI_NCR_MAX_TAGS
365
#define SCSI_NCR_MAX_TAGS (64)
366
#endif
367
 
368
#define NO_TAG  (255)
369
 
370
/*
371
**      Choose appropriate type for tag bitmap.
372
*/
373
#if     SCSI_NCR_MAX_TAGS > 32
374
typedef u_int64 tagmap_t;
375
#else
376
typedef u_int32 tagmap_t;
377
#endif
378
 
379
/*
380
**    Number of targets supported by the driver.
381
**    n permits target numbers 0..n-1.
382
**    Default is 16, meaning targets #0..#15.
383
**    #7 .. is myself.
384
*/
385
 
386
#ifdef SCSI_NCR_MAX_TARGET
387
#define MAX_TARGET  (SCSI_NCR_MAX_TARGET)
388
#else
389
#define MAX_TARGET  (16)
390
#endif
391
 
392
/*
393
**    Number of logic units supported by the driver.
394
**    n enables logic unit numbers 0..n-1.
395
**    The common SCSI devices require only
396
**    one lun, so take 1 as the default.
397
*/
398
 
399
#ifdef SCSI_NCR_MAX_LUN
400
#define MAX_LUN    SCSI_NCR_MAX_LUN
401
#else
402
#define MAX_LUN    (1)
403
#endif
404
 
405
/*
406
**    Asynchronous pre-scaler (ns). Shall be 40
407
*/
408
 
409
#ifndef SCSI_NCR_MIN_ASYNC
410
#define SCSI_NCR_MIN_ASYNC (40)
411
#endif
412
 
413
/*
414
**    The maximum number of jobs scheduled for starting.
415
**    There should be one slot per target, and one slot
416
**    for each tag of each target in use.
417
**    The calculation below is actually quite silly ...
418
*/
419
 
420
#ifdef SCSI_NCR_CAN_QUEUE
421
#define MAX_START   (SCSI_NCR_CAN_QUEUE + 4)
422
#else
423
#define MAX_START   (MAX_TARGET + 7 * SCSI_NCR_MAX_TAGS)
424
#endif
425
 
426
/*
427
**    The maximum number of segments a transfer is split into.
428
**    We support up to 127 segments for both read and write.
429
**    The data scripts are broken into 2 sub-scripts.
430
**    80 (MAX_SCATTERL) segments are moved from a sub-script
431
**    in on-chip RAM. This makes data transfers shorter than
432
**    80k (assuming 1k fs) as fast as possible.
433
*/
434
 
435
#define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
436
 
437
#if (MAX_SCATTER > 80)
438
#define MAX_SCATTERL    80
439
#define MAX_SCATTERH    (MAX_SCATTER - MAX_SCATTERL)
440
#else
441
#define MAX_SCATTERL    (MAX_SCATTER-1)
442
#define MAX_SCATTERH    1
443
#endif
444
 
445
/*
446
**    Io mapped or memory mapped.
447
*/
448
 
449
#if defined(SCSI_NCR_IOMAPPED)
450
#define NCR_IOMAPPED
451
#endif
452
 
453
/*
454
**      other
455
*/
456
 
457
#define NCR_SNOOP_TIMEOUT (1000000)
458
 
459
/*==========================================================
460
**
461
**      Defines for Linux.
462
**
463
**      Linux and Bsd kernel functions are quite different.
464
**      These defines allow a minimum change of the original
465
**      code.
466
**
467
**==========================================================
468
*/
469
 
470
 /*
471
 **     Obvious definitions
472
 */
473
 
474
#define u_char          unsigned char
475
#define u_short         unsigned short
476
#define u_int           unsigned int
477
#define u_long          unsigned long
478
 
479
typedef u_long          vm_offset_t;
480
typedef int             vm_size_t;
481
 
482
#ifndef bcopy
483
#define bcopy(s, d, n)  memcpy((d), (s), (n))
484
#endif
485
#ifndef bzero
486
#define bzero(d, n)     memset((d), 0, (n))
487
#endif
488
 
489
#ifndef offsetof
490
#define offsetof(t, m)  ((size_t) (&((t *)0)->m))
491
#endif
492
 
493
/*
494
**      SMP threading.
495
**
496
**      Assuming that SMP systems are generally high end systems and may
497
**      use several SCSI adapters, we are using one lock per controller
498
**      instead of some global one. For the moment (linux-2.1.95), driver's
499
**      entry points are called with the 'io_request_lock' lock held, so:
500
**      - We are uselessly loosing a couple of micro-seconds to lock the
501
**        controller data structure.
502
**      - But the driver is not broken by design for SMP and so can be
503
**        more resistant to bugs or bad changes in the IO sub-system code.
504
**      - A small advantage could be that the interrupt code is grained as
505
**        wished (e.g.: threaded by controller).
506
*/
507
 
508
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
509
 
510
#if 0   /* not yet needed */
511
static spinlock_t driver_lock;
512
#define NCR_LOCK_DRIVER(flags)     spin_lock_irqsave(&driver_lock, flags)
513
#define NCR_UNLOCK_DRIVER(flags)   spin_unlock_irqrestore(&driver_lock, flags)
514
#endif
515
 
516
#define NCR_INIT_LOCK_NCB(np)      spin_lock_init(&np->smp_lock);
517
#define NCR_LOCK_NCB(np, flags)    spin_lock_irqsave(&np->smp_lock, flags)
518
#define NCR_UNLOCK_NCB(np, flags)  spin_unlock_irqrestore(&np->smp_lock, flags)
519
 
520
#       if LINUX_VERSION_CODE < LinuxVersionCode(2,3,99)
521
 
522
#       define  NCR_LOCK_SCSI_DONE(np, flags) \
523
                spin_lock_irqsave(&io_request_lock, flags)
524
#       define  NCR_UNLOCK_SCSI_DONE(np, flags) \
525
                spin_unlock_irqrestore(&io_request_lock, flags)
526
 
527
#       else
528
 
529
#       define  NCR_LOCK_SCSI_DONE(np, flags)    do {;} while (0)
530
#       define  NCR_UNLOCK_SCSI_DONE(np, flags)  do {;} while (0)
531
 
532
#       endif
533
 
534
#else
535
 
536
#if 0   /* not yet needed */
537
#define NCR_LOCK_DRIVER(flags)     do {;} while (0)
538
#define NCR_UNLOCK_DRIVER(flags)   do {;} while (0)
539
#endif
540
 
541
#define NCR_INIT_LOCK_NCB(np)      do { } while (0)
542
#define NCR_LOCK_NCB(np, flags)    do { save_flags(flags); cli(); } while (0)
543
#define NCR_UNLOCK_NCB(np, flags)  do { restore_flags(flags); } while (0)
544
 
545
#define NCR_LOCK_SCSI_DONE(np, flags)    do {;} while (0)
546
#define NCR_UNLOCK_SCSI_DONE(np, flags)  do {;} while (0)
547
 
548
#endif
549
 
550
/*
551
**      Address translation
552
**
553
**      The driver has to provide physical memory addresses to
554
**      the script processor. Because some architectures use
555
**      different physical addresses from the PCI BUS, we must
556
**      use virt_to_bus instead of virt_to_phys.
557
*/
558
 
559
#define vtophys(p)      virt_to_bus(p)
560
 
561
/*
562
**      Memory mapped IO
563
**
564
**      Since linux-2.1, we must use ioremap() to map the io memory space.
565
**      iounmap() to unmap it. That allows portability.
566
**      Linux 1.3.X and 2.0.X allow to remap physical pages addresses greater
567
**      than the highest physical memory address to kernel virtual pages with
568
**      vremap() / vfree(). That was not portable but worked with i386
569
**      architecture.
570
*/
571
 
572
#if LINUX_VERSION_CODE < LinuxVersionCode(2,1,0)
573
#define ioremap vremap
574
#define iounmap vfree
575
#endif
576
 
577
#if defined (__sparc__)
578
#include <asm/irq.h>
579
#elif defined (__alpha__)
580
#define bus_dvma_to_mem(p)              ((p) & 0xfffffffful)
581
#else
582
#define bus_dvma_to_mem(p)              (p)
583
#endif
584
 
585
#if defined(__i386__) || !defined(NCR_IOMAPPED)
586
__initfunc(
587
static vm_offset_t remap_pci_mem(u_long base, u_long size)
588
)
589
{
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);
593
 
594
        return (vm_offset_t) (page_remapped? (page_remapped + page_offs) : 0UL);
595
}
596
 
597
__initfunc(
598
static void unmap_pci_mem(vm_offset_t vaddr, u_long size)
599
)
600
{
601
        if (vaddr)
602
                iounmap((void *) (vaddr & PAGE_MASK));
603
}
604
#endif  /* __i386__ || !NCR_IOMAPPED */
605
 
606
/*
607
**      Insert a delay in micro-seconds and milli-seconds.
608
**      -------------------------------------------------
609
**      Under Linux, udelay() is restricted to delay < 1 milli-second.
610
**      In fact, it generally works for up to 1 second delay.
611
**      Since 2.1.105, the mdelay() function is provided for delays
612
**      in milli-seconds.
613
**      Under 2.0 kernels, udelay() is an inline function that is very
614
**      inaccurate on Pentium processors.
615
*/
616
 
617
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,105)
618
#define UDELAY udelay
619
#define MDELAY mdelay
620
#else
621
static void UDELAY(long us) { udelay(us); }
622
static void MDELAY(long ms) { while (ms--) UDELAY(1000); }
623
#endif
624
 
625
/*
626
**      Internal data structure allocation.
627
**
628
**      Linux scsi memory poor pool is adjusted for the need of
629
**      middle-level scsi driver.
630
**      We allocate our control blocks in the kernel memory pool
631
**      to avoid scsi pool shortage.
632
**
633
**      kmalloc() only ensures 8 bytes boundary alignment.
634
**      The NCR need better alignment for cache line bursting.
635
**      The global header is moved between the NCB and CCBs and needs
636
**      origin and destination addresses to have same lower four bits.
637
**
638
**      We use 32 boundary alignment for NCB and CCBs and offset multiple
639
**      of 32 for global header fields. That's too much but at least enough.
640
*/
641
 
642
#define ALIGN_SIZE(shift)       (1UL << shift)
643
#define ALIGN_MASK(shift)       (~(ALIGN_SIZE(shift)-1))
644
 
645
#define CACHE_LINE_SHIFT        5
646
#define CACHE_LINE_SIZE         ALIGN_SIZE(CACHE_LINE_SHIFT)
647
#define CACHE_LINE_MASK         ALIGN_MASK(CACHE_LINE_SHIFT)
648
 
649
static void *m_alloc(int size, int a_shift)
650
{
651
        u_long addr;
652
        void *ptr;
653
        u_long a_size, a_mask;
654
 
655
        if (a_shift < 3)
656
                a_shift = 3;
657
 
658
        a_size  = ALIGN_SIZE(a_shift);
659
        a_mask  = ALIGN_MASK(a_shift);
660
 
661
        ptr = (void *) kmalloc(size + a_size, GFP_ATOMIC);
662
        if (ptr) {
663
                addr    = (((u_long) ptr) + a_size) & a_mask;
664
                *((void **) (addr - sizeof(void *))) = ptr;
665
                ptr     = (void *) addr;
666
        }
667
 
668
        return ptr;
669
}
670
 
671
#ifdef MODULE
672
static void m_free(void *ptr, int size)
673
{
674
        u_long addr;
675
 
676
        if (ptr) {
677
                addr    = (u_long) ptr;
678
                ptr     = *((void **) (addr - sizeof(void *)));
679
 
680
                kfree(ptr);
681
        }
682
}
683
#endif
684
 
685
/*
686
**      Transfer direction
687
**
688
**      Low-level scsi drivers under Linux do not receive the expected
689
**      data transfer direction from upper scsi drivers.
690
**      The driver will only check actual data direction for common
691
**      scsi opcodes. Other ones may cause problem, since they may
692
**      depend on device type or be vendor specific.
693
**      I would prefer to never trust the device for data direction,
694
**      but that is not possible.
695
**
696
**      The original driver requires the expected direction to be known.
697
**      The Linux version of the driver has been enhanced in order to
698
**      be able to transfer data in the direction choosen by the target.
699
*/
700
 
701
#define XFER_IN         (1)
702
#define XFER_OUT        (2)
703
 
704
/*
705
**      Head of list of NCR boards
706
**
707
**      For kernel version < 1.3.70, host is retrieved by its irq level.
708
**      For later kernels, the internal host control block address
709
**      (struct ncb) is used as device id parameter of the irq stuff.
710
*/
711
 
712
static struct Scsi_Host         *first_host     = NULL;
713
static Scsi_Host_Template       *the_template   = NULL;
714
 
715
 
716
/*
717
**      /proc directory entry and proc_info function
718
*/
719
 
720
struct proc_dir_entry proc_scsi_ncr53c8xx = {
721
    PROC_SCSI_NCR53C8XX, 9, "ncr53c8xx",
722
    S_IFDIR | S_IRUGO | S_IXUGO, 2
723
};
724
#ifdef SCSI_NCR_PROC_INFO_SUPPORT
725
int ncr53c8xx_proc_info(char *buffer, char **start, off_t offset,
726
                        int length, int hostno, int func);
727
#endif
728
 
729
/*
730
**      Driver setup.
731
**
732
**      This structure is initialized from linux config options.
733
**      It can be overridden at boot-up by the boot command line.
734
*/
735
struct ncr_driver_setup {
736
        u_char  master_parity;
737
        u_char  scsi_parity;
738
        u_char  disconnection;
739
        u_char  special_features;
740
        u_char  ultra_scsi;
741
        u_char  force_sync_nego;
742
        u_char  reverse_probe;
743
        u_char  pci_fix_up;
744
        u_char  use_nvram;
745
        u_char  verbose;
746
        u_char  default_tags;
747
        u_short default_sync;
748
        u_short debug;
749
        u_char  burst_max;
750
        u_char  led_pin;
751
        u_char  max_wide;
752
        u_char  settle_delay;
753
        u_char  diff_support;
754
        u_char  irqm;
755
        u_char  bus_check;
756
        char    tag_ctrl[100];
757
};
758
 
759
static struct ncr_driver_setup
760
        driver_setup                    = SCSI_NCR_DRIVER_SETUP;
761
 
762
#ifdef  SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
763
static struct ncr_driver_setup
764
        driver_safe_setup __initdata    = SCSI_NCR_DRIVER_SAFE_SETUP;
765
# ifdef MODULE
766
char *ncr53c8xx = 0;     /* command line passed by insmod */
767
#  if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,30)
768
MODULE_PARM(ncr53c8xx, "s");
769
#  endif
770
# endif
771
#endif
772
 
773
/*
774
**      Other Linux definitions
775
*/
776
 
777
#define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
778
 
779
static void ncr53c8xx_select_queue_depths(
780
        struct Scsi_Host *host, struct scsi_device *devlist);
781
static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs);
782
static void ncr53c8xx_timeout(unsigned long np);
783
 
784
#define initverbose (driver_setup.verbose)
785
#define bootverbose (np->verbose)
786
 
787
#ifdef SCSI_NCR_NVRAM_SUPPORT
788
/*
789
**      Symbios NvRAM data format
790
*/
791
#define SYMBIOS_NVRAM_SIZE 368
792
#define SYMBIOS_NVRAM_ADDRESS 0x100
793
 
794
struct Symbios_nvram {
795
/* Header 6 bytes */
796
        u_short start_marker;   /* 0x0000 */
797
        u_short byte_count;     /* excluding header/trailer */
798
        u_short checksum;
799
 
800
/* Controller set up 20 bytes */
801
        u_short word0;          /* 0x3000 */
802
        u_short word2;          /* 0x0000 */
803
        u_short word4;          /* 0x0000 */
804
        u_short flags;
805
#define SYMBIOS_SCAM_ENABLE     (1)
806
#define SYMBIOS_PARITY_ENABLE   (1<<1)
807
#define SYMBIOS_VERBOSE_MSGS    (1<<2)
808
#define SYMBIOS_CHS_MAPPING     (1<<3)
809
        u_short flags1;
810
#define SYMBIOS_SCAN_HI_LO      (1)
811
        u_short word10;         /* 0x00 */
812
        u_short flags3;         /* 0x00 */
813
#define SYMBIOS_REMOVABLE_FLAGS (3)             /* 0=none, 1=bootable, 2=all */
814
        u_char  host_id;
815
        u_char  byte15;         /* 0x04 */
816
        u_short word16;         /* 0x0410 */
817
        u_short word18;         /* 0x0000 */
818
 
819
/* Boot order 14 bytes * 4 */
820
        struct Symbios_host{
821
                u_char  word0;          /* 0x0004:ok / 0x0000:nok */
822
                u_short device_id;      /* PCI device id */
823
                u_short vendor_id;      /* PCI vendor id */
824
                u_char  byte6;          /* 0x00 */
825
                u_char  device_fn;      /* PCI device/function number << 3*/
826
                u_short word8;
827
                u_short flags;
828
#define SYMBIOS_INIT_SCAN_AT_BOOT       (1)
829
                u_short io_port;        /* PCI io_port address */
830
        } host[4];
831
 
832
/* Targets 8 bytes * 16 */
833
        struct Symbios_target {
834
                u_short flags;
835
#define SYMBIOS_DISCONNECT_ENABLE       (1)
836
#define SYMBIOS_SCAN_AT_BOOT_TIME       (1<<1)
837
#define SYMBIOS_SCAN_LUNS               (1<<2)
838
#define SYMBIOS_QUEUE_TAGS_ENABLED      (1<<3)
839
                u_char  bus_width;      /* 0x08/0x10 */
840
                u_char  sync_offset;
841
                u_char  sync_period;    /* 4*period factor */
842
                u_char  byte6;          /* 0x00 */
843
                u_short timeout;
844
        } target[16];
845
        u_char  spare_devices[19*8];
846
        u_char  trailer[6];             /* 0xfe 0xfe 0x00 0x00 0x00 0x00 */
847
};
848
typedef struct Symbios_nvram    Symbios_nvram;
849
typedef struct Symbios_host     Symbios_host;
850
typedef struct Symbios_target   Symbios_target;
851
 
852
/*
853
**      Tekram NvRAM data format.
854
*/
855
#define TEKRAM_NVRAM_SIZE 64
856
#define TEKRAM_NVRAM_ADDRESS 0
857
 
858
struct Tekram_nvram {
859
        struct Tekram_target {
860
                u_char  flags;
861
#define TEKRAM_PARITY_CHECK             (1)
862
#define TEKRAM_SYNC_NEGO                (1<<1)
863
#define TEKRAM_DISCONNECT_ENABLE        (1<<2)
864
#define TEKRAM_START_CMD                (1<<3)
865
#define TEKRAM_TAGGED_COMMANDS          (1<<4)
866
#define TEKRAM_WIDE_NEGO                (1<<5)
867
                u_char  sync_index;
868
                u_short word2;
869
        } target[16];
870
        u_char  host_id;
871
        u_char  flags;
872
#define TEKRAM_MORE_THAN_2_DRIVES       (1)
873
#define TEKRAM_DRIVES_SUP_1GB           (1<<1)
874
#define TEKRAM_RESET_ON_POWER_ON        (1<<2)
875
#define TEKRAM_ACTIVE_NEGATION          (1<<3)
876
#define TEKRAM_IMMEDIATE_SEEK           (1<<4)
877
#define TEKRAM_SCAN_LUNS                (1<<5)
878
#define TEKRAM_REMOVABLE_FLAGS          (3<<6)  /* 0: disable; 1: boot device; 2:all */
879
        u_char  boot_delay_index;
880
        u_char  max_tags_index;
881
        u_short flags1;
882
#define TEKRAM_F2_F6_ENABLED            (1)
883
        u_short spare[29];
884
};
885
typedef struct Tekram_nvram     Tekram_nvram;
886
typedef struct Tekram_target    Tekram_target;
887
 
888
static u_char Tekram_sync[12] __initdata = {25,31,37,43,50,62,75,125,12,15,18,21};
889
 
890
#endif /* SCSI_NCR_NVRAM_SUPPORT */
891
 
892
/*
893
**      Structures used by ncr53c8xx_detect/ncr53c8xx_pci_init to
894
**      transmit device configuration to the ncr_attach() function.
895
*/
896
typedef struct {
897
        int     bus;
898
        u_char  device_fn;
899
        u_long  base;
900
        u_long  base_2;
901
        u_long  io_port;
902
        int     irq;
903
/* port and reg fields to use INB, OUTB macros */
904
        u_long  port;
905
        volatile struct ncr_reg *reg;
906
} ncr_slot;
907
 
908
typedef struct {
909
        int type;
910
#define SCSI_NCR_SYMBIOS_NVRAM  (1)
911
#define SCSI_NCR_TEKRAM_NVRAM   (2)
912
#ifdef  SCSI_NCR_NVRAM_SUPPORT
913
        union {
914
                Symbios_nvram Symbios;
915
                Tekram_nvram Tekram;
916
        } data;
917
#endif
918
} ncr_nvram;
919
 
920
/*
921
**      Structure used by ncr53c8xx_detect/ncr53c8xx_pci_init
922
**      to save data on each detected board for ncr_attach().
923
*/
924
typedef struct {
925
        ncr_slot  slot;
926
        ncr_chip  chip;
927
        ncr_nvram *nvram;
928
        int attach_done;
929
} ncr_device;
930
 
931
/*==========================================================
932
**
933
**      Debugging tags
934
**
935
**==========================================================
936
*/
937
 
938
#define DEBUG_ALLOC    (0x0001)
939
#define DEBUG_PHASE    (0x0002)
940
#define DEBUG_POLL     (0x0004)
941
#define DEBUG_QUEUE    (0x0008)
942
#define DEBUG_RESULT   (0x0010)
943
#define DEBUG_SCATTER  (0x0020)
944
#define DEBUG_SCRIPT   (0x0040)
945
#define DEBUG_TINY     (0x0080)
946
#define DEBUG_TIMING   (0x0100)
947
#define DEBUG_NEGO     (0x0200)
948
#define DEBUG_TAGS     (0x0400)
949
#define DEBUG_FREEZE   (0x0800)
950
#define DEBUG_RESTART  (0x1000)
951
 
952
/*
953
**    Enable/Disable debug messages.
954
**    Can be changed at runtime too.
955
*/
956
 
957
#ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
958
        #define DEBUG_FLAGS ncr_debug
959
#else
960
        #define DEBUG_FLAGS     SCSI_NCR_DEBUG_FLAGS
961
#endif
962
 
963
 
964
 
965
/*==========================================================
966
**
967
**      assert ()
968
**
969
**==========================================================
970
**
971
**      modified copy from 386bsd:/usr/include/sys/assert.h
972
**
973
**----------------------------------------------------------
974
*/
975
 
976
#define assert(expression) { \
977
        if (!(expression)) { \
978
                (void)printk(KERN_ERR \
979
                        "assertion \"%s\" failed: file \"%s\", line %d\n", \
980
                        #expression, \
981
                        __FILE__, __LINE__); \
982
        } \
983
}
984
 
985
/*==========================================================
986
**
987
**      Big/Little endian support.
988
**
989
**==========================================================
990
*/
991
 
992
/*
993
**      If the NCR uses big endian addressing mode over the
994
**      PCI, actual io register addresses for byte and word
995
**      accesses must be changed according to lane routing.
996
**      Btw, ncr_offb() and ncr_offw() macros only apply to
997
**      constants and so donnot generate bloated code.
998
*/
999
 
1000
#if     defined(SCSI_NCR_BIG_ENDIAN)
1001
 
1002
#define ncr_offb(o)     (((o)&~3)+((~((o)&3))&3))
1003
#define ncr_offw(o)     (((o)&~3)+((~((o)&3))&2))
1004
 
1005
#else
1006
 
1007
#define ncr_offb(o)     (o)
1008
#define ncr_offw(o)     (o)
1009
 
1010
#endif
1011
 
1012
/*
1013
**      If the CPU and the NCR use same endian-ness adressing,
1014
**      no byte reordering is needed for script patching.
1015
**      Macro cpu_to_scr() is to be used for script patching.
1016
**      Macro scr_to_cpu() is to be used for getting a DWORD
1017
**      from the script.
1018
*/
1019
 
1020
#if     defined(__BIG_ENDIAN) && !defined(SCSI_NCR_BIG_ENDIAN)
1021
 
1022
#define cpu_to_scr(dw)  cpu_to_le32(dw)
1023
#define scr_to_cpu(dw)  le32_to_cpu(dw)
1024
 
1025
#elif   defined(__LITTLE_ENDIAN) && defined(SCSI_NCR_BIG_ENDIAN)
1026
 
1027
#define cpu_to_scr(dw)  cpu_to_be32(dw)
1028
#define scr_to_cpu(dw)  be32_to_cpu(dw)
1029
 
1030
#else
1031
 
1032
#define cpu_to_scr(dw)  (dw)
1033
#define scr_to_cpu(dw)  (dw)
1034
 
1035
#endif
1036
 
1037
/*==========================================================
1038
**
1039
**      Access to the controller chip.
1040
**
1041
**      If NCR_IOMAPPED is defined, the driver will use
1042
**      normal IOs instead of the MEMORY MAPPED IO method
1043
**      recommended by PCI specifications.
1044
**      If all PCI bridges, host brigdes and architectures
1045
**      would have been correctly designed for PCI, this
1046
**      option would be useless.
1047
**
1048
**==========================================================
1049
*/
1050
 
1051
/*
1052
**      If the CPU and the NCR use same endian-ness adressing,
1053
**      no byte reordering is needed for accessing chip io
1054
**      registers. Functions suffixed by '_raw' are assumed
1055
**      to access the chip over the PCI without doing byte
1056
**      reordering. Functions suffixed by '_l2b' are
1057
**      assumed to perform little-endian to big-endian byte
1058
**      reordering, those suffixed by '_b2l' blah, blah,
1059
**      blah, ...
1060
*/
1061
 
1062
#if defined(NCR_IOMAPPED)
1063
 
1064
/*
1065
**      IO mapped only input / ouput
1066
*/
1067
 
1068
#define INB_OFF(o)              inb (np->port + ncr_offb(o))
1069
#define OUTB_OFF(o, val)        outb ((val), np->port + ncr_offb(o))
1070
 
1071
#if     defined(__BIG_ENDIAN) && !defined(SCSI_NCR_BIG_ENDIAN)
1072
 
1073
#define INW_OFF(o)              inw_l2b (np->port + ncr_offw(o))
1074
#define INL_OFF(o)              inl_l2b (np->port + (o))
1075
 
1076
#define OUTW_OFF(o, val)        outw_b2l ((val), np->port + ncr_offw(o))
1077
#define OUTL_OFF(o, val)        outl_b2l ((val), np->port + (o))
1078
 
1079
#elif   defined(__LITTLE_ENDIAN) && defined(SCSI_NCR_BIG_ENDIAN)
1080
 
1081
#define INW_OFF(o)              inw_b2l (np->port + ncr_offw(o))
1082
#define INL_OFF(o)              inl_b2l (np->port + (o))
1083
 
1084
#define OUTW_OFF(o, val)        outw_l2b ((val), np->port + ncr_offw(o))
1085
#define OUTL_OFF(o, val)        outl_l2b ((val), np->port + (o))
1086
 
1087
#else
1088
 
1089
#define INW_OFF(o)              inw_raw (np->port + ncr_offw(o))
1090
#define INL_OFF(o)              inl_raw (np->port + (o))
1091
 
1092
#define OUTW_OFF(o, val)        outw_raw ((val), np->port + ncr_offw(o))
1093
#define OUTL_OFF(o, val)        outl_raw ((val), np->port + (o))
1094
 
1095
#endif  /* ENDIANs */
1096
 
1097
#else   /* defined NCR_IOMAPPED */
1098
 
1099
/*
1100
**      MEMORY mapped IO input / output
1101
*/
1102
 
1103
#define INB_OFF(o)              readb((char *)np->reg + ncr_offb(o))
1104
#define OUTB_OFF(o, val)        writeb((val), (char *)np->reg + ncr_offb(o))
1105
 
1106
#if     defined(__BIG_ENDIAN) && !defined(SCSI_NCR_BIG_ENDIAN)
1107
 
1108
#define INW_OFF(o)              readw_l2b((char *)np->reg + ncr_offw(o))
1109
#define INL_OFF(o)              readl_l2b((char *)np->reg + (o))
1110
 
1111
#define OUTW_OFF(o, val)        writew_b2l((val), (char *)np->reg + ncr_offw(o))
1112
#define OUTL_OFF(o, val)        writel_b2l((val), (char *)np->reg + (o))
1113
 
1114
#elif   defined(__LITTLE_ENDIAN) && defined(SCSI_NCR_BIG_ENDIAN)
1115
 
1116
#define INW_OFF(o)              readw_b2l((char *)np->reg + ncr_offw(o))
1117
#define INL_OFF(o)              readl_b2l((char *)np->reg + (o))
1118
 
1119
#define OUTW_OFF(o, val)        writew_l2b((val), (char *)np->reg + ncr_offw(o))
1120
#define OUTL_OFF(o, val)        writel_l2b((val), (char *)np->reg + (o))
1121
 
1122
#else
1123
 
1124
#define INW_OFF(o)              readw_raw((char *)np->reg + ncr_offw(o))
1125
#define INL_OFF(o)              readl_raw((char *)np->reg + (o))
1126
 
1127
#define OUTW_OFF(o, val)        writew_raw((val), (char *)np->reg + ncr_offw(o))
1128
#define OUTL_OFF(o, val)        writel_raw((val), (char *)np->reg + (o))
1129
 
1130
#endif
1131
 
1132
#endif  /* defined NCR_IOMAPPED */
1133
 
1134
#define INB(r)          INB_OFF (offsetof(struct ncr_reg,r))
1135
#define INW(r)          INW_OFF (offsetof(struct ncr_reg,r))
1136
#define INL(r)          INL_OFF (offsetof(struct ncr_reg,r))
1137
 
1138
#define OUTB(r, val)    OUTB_OFF (offsetof(struct ncr_reg,r), (val))
1139
#define OUTW(r, val)    OUTW_OFF (offsetof(struct ncr_reg,r), (val))
1140
#define OUTL(r, val)    OUTL_OFF (offsetof(struct ncr_reg,r), (val))
1141
 
1142
/*
1143
**      Set bit field ON, OFF
1144
*/
1145
 
1146
#define OUTONB(r, m)    OUTB(r, INB(r) | (m))
1147
#define OUTOFFB(r, m)   OUTB(r, INB(r) & ~(m))
1148
#define OUTONW(r, m)    OUTW(r, INW(r) | (m))
1149
#define OUTOFFW(r, m)   OUTW(r, INW(r) & ~(m))
1150
#define OUTONL(r, m)    OUTL(r, INL(r) | (m))
1151
#define OUTOFFL(r, m)   OUTL(r, INL(r) & ~(m))
1152
 
1153
 
1154
/*==========================================================
1155
**
1156
**      Command control block states.
1157
**
1158
**==========================================================
1159
*/
1160
 
1161
#define HS_IDLE         (0)
1162
#define HS_BUSY         (1)
1163
#define HS_NEGOTIATE    (2)     /* sync/wide data transfer*/
1164
#define HS_DISCONNECT   (3)     /* Disconnected by target */
1165
 
1166
#define HS_DONEMASK     (0x80)
1167
#define HS_COMPLETE     (4|HS_DONEMASK)
1168
#define HS_SEL_TIMEOUT  (5|HS_DONEMASK) /* Selection timeout      */
1169
#define HS_RESET        (6|HS_DONEMASK) /* SCSI reset             */
1170
#define HS_ABORTED      (7|HS_DONEMASK) /* Transfer aborted       */
1171
#define HS_TIMEOUT      (8|HS_DONEMASK) /* Software timeout       */
1172
#define HS_FAIL         (9|HS_DONEMASK) /* SCSI or PCI bus errors */
1173
#define HS_UNEXPECTED   (10|HS_DONEMASK)/* Unexpected disconnect  */
1174
 
1175
/*
1176
**      Invalid host status values used by the SCRIPTS processor
1177
**      when the nexus is not fully identified.
1178
**      Shall never appear in a CCB.
1179
*/
1180
 
1181
#define HS_INVALMASK    (0x40)
1182
#define HS_SELECTING    (0|HS_INVALMASK)
1183
#define HS_IN_RESELECT  (1|HS_INVALMASK)
1184
#define HS_STARTING     (2|HS_INVALMASK)
1185
 
1186
/*
1187
**      Flags set by the SCRIPT processor for commands
1188
**      that have been skipped.
1189
*/
1190
#define HS_SKIPMASK     (0x20)
1191
 
1192
/*==========================================================
1193
**
1194
**      Software Interrupt Codes
1195
**
1196
**==========================================================
1197
*/
1198
 
1199
#define SIR_BAD_STATUS          (1)
1200
#define SIR_XXXXXXXXXX          (2)
1201
#define SIR_NEGO_SYNC           (3)
1202
#define SIR_NEGO_WIDE           (4)
1203
#define SIR_NEGO_FAILED         (5)
1204
#define SIR_NEGO_PROTO          (6)
1205
#define SIR_REJECT_RECEIVED     (7)
1206
#define SIR_REJECT_SENT         (8)
1207
#define SIR_IGN_RESIDUE         (9)
1208
#define SIR_MISSING_SAVE        (10)
1209
#define SIR_RESEL_NO_MSG_IN     (11)
1210
#define SIR_RESEL_NO_IDENTIFY   (12)
1211
#define SIR_RESEL_BAD_LUN       (13)
1212
#define SIR_RESEL_BAD_TARGET    (14)
1213
#define SIR_RESEL_BAD_I_T_L     (15)
1214
#define SIR_RESEL_BAD_I_T_L_Q   (16)
1215
#define SIR_DONE_OVERFLOW       (17)
1216
#define SIR_MAX                 (17)
1217
 
1218
/*==========================================================
1219
**
1220
**      Extended error codes.
1221
**      xerr_status field of struct ccb.
1222
**
1223
**==========================================================
1224
*/
1225
 
1226
#define XE_OK           (0)
1227
#define XE_EXTRA_DATA   (1)     /* unexpected data phase */
1228
#define XE_BAD_PHASE    (2)     /* illegal phase (4/5)   */
1229
 
1230
/*==========================================================
1231
**
1232
**      Negotiation status.
1233
**      nego_status field       of struct ccb.
1234
**
1235
**==========================================================
1236
*/
1237
 
1238
#define NS_SYNC         (1)
1239
#define NS_WIDE         (2)
1240
 
1241
/*==========================================================
1242
**
1243
**      "Special features" of targets.
1244
**      quirks field            of struct tcb.
1245
**      actualquirks field      of struct ccb.
1246
**
1247
**==========================================================
1248
*/
1249
 
1250
#define QUIRK_AUTOSAVE  (0x01)
1251
#define QUIRK_NOMSG     (0x02)
1252
#define QUIRK_NOSYNC    (0x10)
1253
#define QUIRK_NOWIDE16  (0x20)
1254
 
1255
/*==========================================================
1256
**
1257
**      Capability bits in Inquire response byte 7.
1258
**
1259
**==========================================================
1260
*/
1261
 
1262
#define INQ7_QUEUE      (0x02)
1263
#define INQ7_SYNC       (0x10)
1264
#define INQ7_WIDE16     (0x20)
1265
 
1266
/*==========================================================
1267
**
1268
**      Misc.
1269
**
1270
**==========================================================
1271
*/
1272
 
1273
#define CCB_MAGIC       (0xf2691ad2)
1274
 
1275
/*==========================================================
1276
**
1277
**      Declaration of structs.
1278
**
1279
**==========================================================
1280
*/
1281
 
1282
struct tcb;
1283
struct lcb;
1284
struct ccb;
1285
struct ncb;
1286
struct script;
1287
 
1288
typedef struct ncb * ncb_p;
1289
typedef struct tcb * tcb_p;
1290
typedef struct lcb * lcb_p;
1291
typedef struct ccb * ccb_p;
1292
 
1293
struct link {
1294
        ncrcmd  l_cmd;
1295
        ncrcmd  l_paddr;
1296
};
1297
 
1298
struct  usrcmd {
1299
        u_long  target;
1300
        u_long  lun;
1301
        u_long  data;
1302
        u_long  cmd;
1303
};
1304
 
1305
#define UC_SETSYNC      10
1306
#define UC_SETTAGS      11
1307
#define UC_SETDEBUG     12
1308
#define UC_SETORDER     13
1309
#define UC_SETWIDE      14
1310
#define UC_SETFLAG      15
1311
#define UC_CLEARPROF    16
1312
#define UC_SETVERBOSE   17
1313
 
1314
#define UF_TRACE        (0x01)
1315
#define UF_NODISC       (0x02)
1316
#define UF_NOSCAN       (0x04)
1317
 
1318
/*---------------------------------------
1319
**
1320
**      Timestamps for profiling
1321
**
1322
**---------------------------------------
1323
*/
1324
 
1325
#ifdef SCSI_NCR_PROFILE_SUPPORT
1326
 
1327
struct tstamp {
1328
        u_long start;
1329
        u_long end;
1330
        u_long command;
1331
        u_long status;
1332
        u_long disconnect;
1333
        u_long reselect;
1334
};
1335
 
1336
/*
1337
**      profiling data (per device)
1338
*/
1339
 
1340
struct profile {
1341
        u_long  num_trans;
1342
        u_long  num_kbytes;
1343
        u_long  rest_bytes;
1344
        u_long  num_disc;
1345
        u_long  num_break;
1346
        u_long  num_int;
1347
        u_long  num_fly;
1348
        u_long  ms_setup;
1349
        u_long  ms_data;
1350
        u_long  ms_disc;
1351
        u_long  ms_post;
1352
};
1353
#endif
1354
 
1355
/*========================================================================
1356
**
1357
**      Declaration of structs:         target control block
1358
**
1359
**========================================================================
1360
*/
1361
struct tcb {
1362
        /*----------------------------------------------------------------
1363
        **      During reselection the ncr jumps to this point with SFBR
1364
        **      set to the encoded target number with bit 7 set.
1365
        **      if it's not this target, jump to the next.
1366
        **
1367
        **      JUMP  IF (SFBR != #target#), @(next tcb)
1368
        **----------------------------------------------------------------
1369
        */
1370
        struct link   jump_tcb;
1371
 
1372
        /*----------------------------------------------------------------
1373
        **      Load the actual values for the sxfer and the scntl3
1374
        **      register (sync/wide mode).
1375
        **
1376
        **      SCR_COPY (1), @(sval field of this tcb), @(sxfer  register)
1377
        **      SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
1378
        **----------------------------------------------------------------
1379
        */
1380
        ncrcmd  getscr[6];
1381
 
1382
        /*----------------------------------------------------------------
1383
        **      Get the IDENTIFY message and load the LUN to SFBR.
1384
        **
1385
        **      CALL, <RESEL_LUN>
1386
        **----------------------------------------------------------------
1387
        */
1388
        struct link   call_lun;
1389
 
1390
        /*----------------------------------------------------------------
1391
        **      Now look for the right lun.
1392
        **
1393
        **      For i = 0 to 3
1394
        **              SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
1395
        **
1396
        **      Recent chips will prefetch the 4 JUMPS using only 1 burst.
1397
        **      It is kind of hashcoding.
1398
        **----------------------------------------------------------------
1399
        */
1400
        struct link     jump_lcb[4];    /* JUMPs for reselection        */
1401
        lcb_p           lp[MAX_LUN];    /* The lcb's of this tcb        */
1402
        u_char          inq_done;       /* Target capabilities received */
1403
        u_char          inq_byte7;      /* Contains these capabilities  */
1404
 
1405
        /*----------------------------------------------------------------
1406
        **      Pointer to the ccb used for negotiation.
1407
        **      Prevent from starting a negotiation for all queued commands
1408
        **      when tagged command queuing is enabled.
1409
        **----------------------------------------------------------------
1410
        */
1411
        ccb_p   nego_cp;
1412
 
1413
        /*----------------------------------------------------------------
1414
        **      statistical data
1415
        **----------------------------------------------------------------
1416
        */
1417
        u_long  transfers;
1418
        u_long  bytes;
1419
 
1420
        /*----------------------------------------------------------------
1421
        **      negotiation of wide and synch transfer and device quirks.
1422
        **----------------------------------------------------------------
1423
        */
1424
/*0*/   u_char  minsync;
1425
/*1*/   u_char  sval;
1426
/*2*/   u_short period;
1427
/*0*/   u_char  maxoffs;
1428
/*1*/   u_char  quirks;
1429
/*2*/   u_char  widedone;
1430
/*3*/   u_char  wval;
1431
 
1432
        /*----------------------------------------------------------------
1433
        **      User settable limits and options.
1434
        **      These limits are read from the NVRAM if present.
1435
        **----------------------------------------------------------------
1436
        */
1437
        u_char  usrsync;
1438
        u_char  usrwide;
1439
        u_char  usrtags;
1440
        u_char  usrflag;
1441
};
1442
 
1443
/*========================================================================
1444
**
1445
**      Declaration of structs:         lun control block
1446
**
1447
**========================================================================
1448
*/
1449
struct lcb {
1450
        /*----------------------------------------------------------------
1451
        **      During reselection the ncr jumps to this point
1452
        **      with SFBR set to the "Identify" message.
1453
        **      if it's not this lun, jump to the next.
1454
        **
1455
        **      JUMP  IF (SFBR != #lun#), @(next lcb of this target)
1456
        **
1457
        **      It is this lun. Load TEMP with the nexus jumps table
1458
        **      address and jump to RESEL_TAG (or RESEL_NOTAG).
1459
        **
1460
        **              SCR_COPY (4), p_jump_ccb, TEMP,
1461
        **              SCR_JUMP, <RESEL_TAG>
1462
        **----------------------------------------------------------------
1463
        */
1464
        struct link     jump_lcb;
1465
        ncrcmd          load_jump_ccb[3];
1466
        struct link     jump_tag;
1467
        ncrcmd          p_jump_ccb;     /* Jump table bus address       */
1468
 
1469
        /*----------------------------------------------------------------
1470
        **      Jump table used by the script processor to directly jump
1471
        **      to the CCB corresponding to the reselected nexus.
1472
        **      Address is allocated on 256 bytes boundary in order to
1473
        **      allow 8 bit calculation of the tag jump entry for up to
1474
        **      64 possible tags.
1475
        **----------------------------------------------------------------
1476
        */
1477
        u_int32         jump_ccb_0;     /* Default table if no tags     */
1478
        u_int32         *jump_ccb;      /* Virtual address              */
1479
 
1480
        /*----------------------------------------------------------------
1481
        **      CCB queue management.
1482
        **----------------------------------------------------------------
1483
        */
1484
        XPT_QUEHEAD     free_ccbq;      /* Queue of available CCBs      */
1485
        XPT_QUEHEAD     busy_ccbq;      /* Queue of busy CCBs           */
1486
        XPT_QUEHEAD     wait_ccbq;      /* Queue of waiting for IO CCBs */
1487
        XPT_QUEHEAD     skip_ccbq;      /* Queue of skipped CCBs        */
1488
        u_char          actccbs;        /* Number of allocated CCBs     */
1489
        u_char          busyccbs;       /* CCBs busy for this lun       */
1490
        u_char          queuedccbs;     /* CCBs queued to the controller*/
1491
        u_char          queuedepth;     /* Queue depth for this lun     */
1492
        u_char          scdev_depth;    /* SCSI device queue depth      */
1493
        u_char          maxnxs;         /* Max possible nexuses         */
1494
 
1495
        /*----------------------------------------------------------------
1496
        **      Control of tagged command queuing.
1497
        **      Tags allocation is performed using a circular buffer.
1498
        **      This avoids using a loop for tag allocation.
1499
        **----------------------------------------------------------------
1500
        */
1501
        u_char          ia_tag;         /* Allocation index             */
1502
        u_char          if_tag;         /* Freeing index                */
1503
        u_char cb_tags[SCSI_NCR_MAX_TAGS];      /* Circular tags buffer */
1504
        u_char          usetags;        /* Command queuing is active    */
1505
        u_char          maxtags;        /* Max nr of tags asked by user */
1506
        u_char          numtags;        /* Current number of tags       */
1507
        u_char          inq_byte7;      /* Store unit CmdQ capabitility */
1508
 
1509
        /*----------------------------------------------------------------
1510
        **      QUEUE FULL control and ORDERED tag control.
1511
        **----------------------------------------------------------------
1512
        */
1513
        /*----------------------------------------------------------------
1514
        **      QUEUE FULL and ORDERED tag control.
1515
        **----------------------------------------------------------------
1516
        */
1517
        u_short         num_good;       /* Nr of GOOD since QUEUE FULL  */
1518
        tagmap_t        tags_umap;      /* Used tags bitmap             */
1519
        tagmap_t        tags_smap;      /* Tags in use at 'tag_stime'   */
1520
        u_long          tags_stime;     /* Last time we set smap=umap   */
1521
        ccb_p           held_ccb;       /* CCB held for QUEUE FULL      */
1522
};
1523
 
1524
/*========================================================================
1525
**
1526
**      Declaration of structs:     the launch script.
1527
**
1528
**========================================================================
1529
**
1530
**      It is part of the CCB and is called by the scripts processor to
1531
**      start or restart the data structure (nexus).
1532
**      This 6 DWORDs mini script makes use of prefetching.
1533
**
1534
**------------------------------------------------------------------------
1535
*/
1536
struct launch {
1537
        /*----------------------------------------------------------------
1538
        **      SCR_COPY(4),    @(p_phys), @(dsa register)
1539
        **      SCR_JUMP,       @(scheduler_point)
1540
        **----------------------------------------------------------------
1541
        */
1542
        ncrcmd          setup_dsa[3];   /* Copy 'phys' address to dsa   */
1543
        struct link     schedule;       /* Jump to scheduler point      */
1544
        ncrcmd          p_phys;         /* 'phys' header bus address    */
1545
};
1546
 
1547
/*========================================================================
1548
**
1549
**      Declaration of structs:     global HEADER.
1550
**
1551
**========================================================================
1552
**
1553
**      This substructure is copied from the ccb to a global address after
1554
**      selection (or reselection) and copied back before disconnect.
1555
**
1556
**      These fields are accessible to the script processor.
1557
**
1558
**------------------------------------------------------------------------
1559
*/
1560
 
1561
struct head {
1562
        /*----------------------------------------------------------------
1563
        **      Saved data pointer.
1564
        **      Points to the position in the script responsible for the
1565
        **      actual transfer transfer of data.
1566
        **      It's written after reception of a SAVE_DATA_POINTER message.
1567
        **      The goalpointer points after the last transfer command.
1568
        **----------------------------------------------------------------
1569
        */
1570
        u_int32         savep;
1571
        u_int32         lastp;
1572
        u_int32         goalp;
1573
 
1574
        /*----------------------------------------------------------------
1575
        **      Alternate data pointer.
1576
        **      They are copied back to savep/lastp/goalp by the SCRIPTS
1577
        **      when the direction is unknown and the device claims data out.
1578
        **----------------------------------------------------------------
1579
        */
1580
        u_int32         wlastp;
1581
        u_int32         wgoalp;
1582
 
1583
        /*----------------------------------------------------------------
1584
        **      The virtual address of the ccb containing this header.
1585
        **----------------------------------------------------------------
1586
        */
1587
        ccb_p   cp;
1588
 
1589
#ifdef SCSI_NCR_PROFILE_SUPPORT
1590
        /*----------------------------------------------------------------
1591
        **      Space for some timestamps to gather profiling data.
1592
        **----------------------------------------------------------------
1593
        */
1594
        struct tstamp   stamp;
1595
#endif
1596
 
1597
        /*----------------------------------------------------------------
1598
        **      Status fields.
1599
        **----------------------------------------------------------------
1600
        */
1601
        u_char          scr_st[4];      /* script status                */
1602
        u_char          status[4];      /* host status. must be the     */
1603
                                        /*  last DWORD of the header.   */
1604
};
1605
 
1606
/*
1607
**      The status bytes are used by the host and the script processor.
1608
**
1609
**      The byte corresponding to the host_status must be stored in the
1610
**      last DWORD of the CCB header since it is used for command
1611
**      completion (ncr_wakeup()). Doing so, we are sure that the header
1612
**      has been entirely copied back to the CCB when the host_status is
1613
**      seen complete by the CPU.
1614
**
1615
**      The last four bytes (status[4]) are copied to the scratchb register
1616
**      (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
1617
**      and copied back just after disconnecting.
1618
**      Inside the script the XX_REG are used.
1619
**
1620
**      The first four bytes (scr_st[4]) are used inside the script by
1621
**      "COPY" commands.
1622
**      Because source and destination must have the same alignment
1623
**      in a DWORD, the fields HAVE to be at the choosen offsets.
1624
**              xerr_st         0        (0x34)  scratcha
1625
**              sync_st         1       (0x05)  sxfer
1626
**              wide_st         3       (0x03)  scntl3
1627
*/
1628
 
1629
/*
1630
**      Last four bytes (script)
1631
*/
1632
#define  QU_REG scr0
1633
#define  HS_REG scr1
1634
#define  HS_PRT nc_scr1
1635
#define  SS_REG scr2
1636
#define  SS_PRT nc_scr2
1637
#define  PS_REG scr3
1638
 
1639
/*
1640
**      Last four bytes (host)
1641
*/
1642
#define  actualquirks  phys.header.status[0]
1643
#define  host_status   phys.header.status[1]
1644
#define  scsi_status   phys.header.status[2]
1645
#define  parity_status phys.header.status[3]
1646
 
1647
/*
1648
**      First four bytes (script)
1649
*/
1650
#define  xerr_st       header.scr_st[0]
1651
#define  sync_st       header.scr_st[1]
1652
#define  nego_st       header.scr_st[2]
1653
#define  wide_st       header.scr_st[3]
1654
 
1655
/*
1656
**      First four bytes (host)
1657
*/
1658
#define  xerr_status   phys.xerr_st
1659
#define  nego_status   phys.nego_st
1660
 
1661
#if 0
1662
#define  sync_status   phys.sync_st
1663
#define  wide_status   phys.wide_st
1664
#endif
1665
 
1666
/*==========================================================
1667
**
1668
**      Declaration of structs:     Data structure block
1669
**
1670
**==========================================================
1671
**
1672
**      During execution of a ccb by the script processor,
1673
**      the DSA (data structure address) register points
1674
**      to this substructure of the ccb.
1675
**      This substructure contains the header with
1676
**      the script-processor-changable data and
1677
**      data blocks for the indirect move commands.
1678
**
1679
**----------------------------------------------------------
1680
*/
1681
 
1682
struct dsb {
1683
 
1684
        /*
1685
        **      Header.
1686
        */
1687
 
1688
        struct head     header;
1689
 
1690
        /*
1691
        **      Table data for Script
1692
        */
1693
 
1694
        struct scr_tblsel  select;
1695
        struct scr_tblmove smsg  ;
1696
        struct scr_tblmove cmd   ;
1697
        struct scr_tblmove sense ;
1698
        struct scr_tblmove data [MAX_SCATTER];
1699
};
1700
 
1701
 
1702
/*========================================================================
1703
**
1704
**      Declaration of structs:     Command control block.
1705
**
1706
**========================================================================
1707
*/
1708
struct ccb {
1709
        /*----------------------------------------------------------------
1710
        **      This is the data structure which is pointed by the DSA
1711
        **      register when it is executed by the script processor.
1712
        **      It must be the first entry because it contains the header
1713
        **      as first entry that must be cache line aligned.
1714
        **----------------------------------------------------------------
1715
        */
1716
        struct dsb      phys;
1717
 
1718
        /*----------------------------------------------------------------
1719
        **      Mini-script used at CCB execution start-up.
1720
        **      Load the DSA with the data structure address (phys) and
1721
        **      jump to SELECT. Jump to CANCEL if CCB is to be canceled.
1722
        **----------------------------------------------------------------
1723
        */
1724
        struct launch   start;
1725
 
1726
        /*----------------------------------------------------------------
1727
        **      Mini-script used at CCB relection to restart the nexus.
1728
        **      Load the DSA with the data structure address (phys) and
1729
        **      jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
1730
        **----------------------------------------------------------------
1731
        */
1732
        struct launch   restart;
1733
 
1734
        /*----------------------------------------------------------------
1735
        **      If a data transfer phase is terminated too early
1736
        **      (after reception of a message (i.e. DISCONNECT)),
1737
        **      we have to prepare a mini script to transfer
1738
        **      the rest of the data.
1739
        **----------------------------------------------------------------
1740
        */
1741
        ncrcmd          patch[8];
1742
 
1743
        /*----------------------------------------------------------------
1744
        **      The general SCSI driver provides a
1745
        **      pointer to a control block.
1746
        **----------------------------------------------------------------
1747
        */
1748
        Scsi_Cmnd       *cmd;           /* SCSI command                 */
1749
        u_long          tlimit;         /* Deadline for this job        */
1750
        int             data_len;       /* Total data length            */
1751
 
1752
        /*----------------------------------------------------------------
1753
        **      Message areas.
1754
        **      We prepare a message to be sent after selection.
1755
        **      We may use a second one if the command is rescheduled
1756
        **      due to GETCC or QFULL.
1757
        **      Contents are IDENTIFY and SIMPLE_TAG.
1758
        **      While negotiating sync or wide transfer,
1759
        **      a SDTR or WDTR message is appended.
1760
        **----------------------------------------------------------------
1761
        */
1762
        u_char          scsi_smsg [8];
1763
        u_char          scsi_smsg2[8];
1764
 
1765
        /*----------------------------------------------------------------
1766
        **      Other fields.
1767
        **----------------------------------------------------------------
1768
        */
1769
        u_long          p_ccb;          /* BUS address of this CCB      */
1770
        u_char          sensecmd[6];    /* Sense command                */
1771
        u_char          tag;            /* Tag for this transfer        */
1772
                                        /*  255 means no tag            */
1773
        u_char          target;
1774
        u_char          lun;
1775
        u_char          queued;
1776
        u_char          auto_sense;
1777
        ccb_p           link_ccb;       /* Host adapter CCB chain       */
1778
        XPT_QUEHEAD     link_ccbq;      /* Link to unit CCB queue       */
1779
        u_int32         startp;         /* Initial data pointer         */
1780
        u_long          magic;          /* Free / busy  CCB flag        */
1781
};
1782
 
1783
#define CCB_PHYS(cp,lbl)        (cp->p_ccb + offsetof(struct ccb, lbl))
1784
 
1785
 
1786
/*========================================================================
1787
**
1788
**      Declaration of structs:     NCR device descriptor
1789
**
1790
**========================================================================
1791
*/
1792
struct ncb {
1793
        /*----------------------------------------------------------------
1794
        **      The global header.
1795
        **      It is accessible to both the host and the script processor.
1796
        **      Must be cache line size aligned (32 for x86) in order to
1797
        **      allow cache line bursting when it is copied to/from CCB.
1798
        **----------------------------------------------------------------
1799
        */
1800
        struct head     header;
1801
 
1802
        /*----------------------------------------------------------------
1803
        **      CCBs management queues.
1804
        **----------------------------------------------------------------
1805
        */
1806
        Scsi_Cmnd       *waiting_list;  /* Commands waiting for a CCB   */
1807
                                        /*  when lcb is not allocated.  */
1808
        Scsi_Cmnd       *done_list;     /* Commands waiting for done()  */
1809
                                        /* callback to be invoked.      */
1810
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
1811
        spinlock_t      smp_lock;       /* Lock for SMP threading       */
1812
#endif
1813
 
1814
        /*----------------------------------------------------------------
1815
        **      Chip and controller indentification.
1816
        **----------------------------------------------------------------
1817
        */
1818
        int             unit;           /* Unit number                  */
1819
        char            chip_name[8];   /* Chip name                    */
1820
        char            inst_name[16];  /* ncb instance name            */
1821
 
1822
        /*----------------------------------------------------------------
1823
        **      Initial value of some IO register bits.
1824
        **      These values are assumed to have been set by BIOS, and may
1825
        **      be used for probing adapter implementation differences.
1826
        **----------------------------------------------------------------
1827
        */
1828
        u_char  sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
1829
                sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
1830
 
1831
        /*----------------------------------------------------------------
1832
        **      Actual initial value of IO register bits used by the
1833
        **      driver. They are loaded at initialisation according to
1834
        **      features that are to be enabled.
1835
        **----------------------------------------------------------------
1836
        */
1837
        u_char  rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4,
1838
                rv_ctest5, rv_stest2;
1839
 
1840
        /*----------------------------------------------------------------
1841
        **      Targets management.
1842
        **      During reselection the ncr jumps to jump_tcb.
1843
        **      The SFBR register is loaded with the encoded target id.
1844
        **      For i = 0 to 3
1845
        **              SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
1846
        **
1847
        **      Recent chips will prefetch the 4 JUMPS using only 1 burst.
1848
        **      It is kind of hashcoding.
1849
        **----------------------------------------------------------------
1850
        */
1851
        struct link     jump_tcb[4];    /* JUMPs for reselection        */
1852
        struct tcb  target[MAX_TARGET]; /* Target data                  */
1853
 
1854
        /*----------------------------------------------------------------
1855
        **      Virtual and physical bus addresses of the chip.
1856
        **----------------------------------------------------------------
1857
        */
1858
        vm_offset_t     vaddr;          /* Virtual and bus address of   */
1859
        vm_offset_t     paddr;          /*  chip's IO registers.        */
1860
        vm_offset_t     paddr2;         /* On-chip RAM bus address.     */
1861
        volatile                        /* Pointer to volatile for      */
1862
        struct ncr_reg  *reg;           /*  memory mapped IO.           */
1863
 
1864
        /*----------------------------------------------------------------
1865
        **      SCRIPTS virtual and physical bus addresses.
1866
        **      'script'  is loaded in the on-chip RAM if present.
1867
        **      'scripth' stays in main memory.
1868
        **----------------------------------------------------------------
1869
        */
1870
        struct script   *script0;       /* Copies of script and scripth */
1871
        struct scripth  *scripth0;      /*  relocated for this ncb.     */
1872
        struct scripth  *scripth;       /* Actual scripth virt. address */
1873
        u_long          p_script;       /* Actual script and scripth    */
1874
        u_long          p_scripth;      /*  bus addresses.              */
1875
 
1876
        /*----------------------------------------------------------------
1877
        **      General controller parameters and configuration.
1878
        **----------------------------------------------------------------
1879
        */
1880
        u_short         device_id;      /* PCI device id                */
1881
        u_char          revision_id;    /* PCI device revision id       */
1882
        u_long          port;           /* IO space base address        */
1883
        u_int           irq;            /* IRQ level                    */
1884
        u_int           features;       /* Chip features map            */
1885
        u_char          myaddr;         /* SCSI id of the adapter       */
1886
        u_char          maxburst;       /* log base 2 of dwords burst   */
1887
        u_char          maxwide;        /* Maximum transfer width       */
1888
        u_char          minsync;        /* Minimum sync period factor   */
1889
        u_char          maxsync;        /* Maximum sync period factor   */
1890
        u_char          maxoffs;        /* Max scsi offset              */
1891
        u_char          multiplier;     /* Clock multiplier (1,2,4)     */
1892
        u_char          clock_divn;     /* Number of clock divisors     */
1893
        u_long          clock_khz;      /* SCSI clock frequency in KHz  */
1894
 
1895
        /*----------------------------------------------------------------
1896
        **      Start queue management.
1897
        **      It is filled up by the host processor and accessed by the
1898
        **      SCRIPTS processor in order to start SCSI commands.
1899
        **----------------------------------------------------------------
1900
        */
1901
        u_short         squeueput;      /* Next free slot of the queue  */
1902
        u_short         actccbs;        /* Number of allocated CCBs     */
1903
        u_short         queuedccbs;     /* Number of CCBs in start queue*/
1904
        u_short         queuedepth;     /* Start queue depth            */
1905
 
1906
        /*----------------------------------------------------------------
1907
        **      Timeout handler.
1908
        **----------------------------------------------------------------
1909
        */
1910
        struct timer_list timer;        /* Timer handler link header    */
1911
        u_long          lasttime;
1912
        u_long          settle_time;    /* Resetting the SCSI BUS       */
1913
 
1914
        /*----------------------------------------------------------------
1915
        **      Debugging and profiling.
1916
        **----------------------------------------------------------------
1917
        */
1918
        struct ncr_reg  regdump;        /* Register dump                */
1919
        u_long          regtime;        /* Time it has been done        */
1920
#ifdef SCSI_NCR_PROFILE_SUPPORT
1921
        struct profile  profile;        /* Profiling data               */
1922
        u_int           disc_phys;      /* Disconnection counters       */
1923
        u_int           disc_ref;
1924
#endif
1925
 
1926
        /*----------------------------------------------------------------
1927
        **      Miscellaneous buffers accessed by the scripts-processor.
1928
        **      They shall be DWORD aligned, because they may be read or
1929
        **      written with a SCR_COPY script command.
1930
        **----------------------------------------------------------------
1931
        */
1932
        u_char          msgout[8];      /* Buffer for MESSAGE OUT       */
1933
        u_char          msgin [8];      /* Buffer for MESSAGE IN        */
1934
        u_int32         lastmsg;        /* Last SCSI message sent       */
1935
        u_char          scratch;        /* Scratch for SCSI receive     */
1936
 
1937
        /*----------------------------------------------------------------
1938
        **      Miscellaneous configuration and status parameters.
1939
        **----------------------------------------------------------------
1940
        */
1941
        u_char          disc;           /* Diconnection allowed         */
1942
        u_char          scsi_mode;      /* Current SCSI BUS mode        */
1943
        u_char          order;          /* Tag order to use             */
1944
        u_char          verbose;        /* Verbosity for this controller*/
1945
        int             ncr_cache;      /* Used for cache test at init. */
1946
 
1947
        /*----------------------------------------------------------------
1948
        **      Command completion handling.
1949
        **----------------------------------------------------------------
1950
        */
1951
#ifdef SCSI_NCR_CCB_DONE_SUPPORT
1952
        struct ccb      *(ccb_done[MAX_DONE]);
1953
        int             ccb_done_ic;
1954
#endif
1955
        /*----------------------------------------------------------------
1956
        **      Fields that should be removed or changed.
1957
        **----------------------------------------------------------------
1958
        */
1959
        struct ccb      *ccb;           /* Global CCB                   */
1960
        struct usrcmd   user;           /* Command from user            */
1961
        u_char          release_stage;  /* Synchronisation stage on release  */
1962
};
1963
 
1964
#define NCB_SCRIPT_PHYS(np,lbl)  (np->p_script  + offsetof (struct script, lbl))
1965
#define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1966
 
1967
/*==========================================================
1968
**
1969
**
1970
**      Script for NCR-Processor.
1971
**
1972
**      Use ncr_script_fill() to create the variable parts.
1973
**      Use ncr_script_copy_and_bind() to make a copy and
1974
**      bind to physical addresses.
1975
**
1976
**
1977
**==========================================================
1978
**
1979
**      We have to know the offsets of all labels before
1980
**      we reach them (for forward jumps).
1981
**      Therefore we declare a struct here.
1982
**      If you make changes inside the script,
1983
**      DONT FORGET TO CHANGE THE LENGTHS HERE!
1984
**
1985
**----------------------------------------------------------
1986
*/
1987
 
1988
/*
1989
**      Script fragments which are loaded into the on-chip RAM
1990
**      of 825A, 875 and 895 chips.
1991
*/
1992
struct script {
1993
        ncrcmd  start           [  5];
1994
        ncrcmd  startpos        [  1];
1995
        ncrcmd  select          [  6];
1996
        ncrcmd  select2         [  9];
1997
        ncrcmd  loadpos         [  4];
1998
        ncrcmd  send_ident      [  9];
1999
        ncrcmd  prepare         [  6];
2000
        ncrcmd  prepare2        [  7];
2001
#ifdef SCSI_NCR_PROFILE_SUPPORT
2002
        ncrcmd  command         [  9];
2003
#else
2004
        ncrcmd  command         [  6];
2005
#endif
2006
        ncrcmd  dispatch        [ 32];
2007
        ncrcmd  clrack          [  4];
2008
        ncrcmd  no_data         [ 17];
2009
#ifdef SCSI_NCR_PROFILE_SUPPORT
2010
        ncrcmd  status          [ 11];
2011
#else
2012
        ncrcmd  status          [  8];
2013
#endif
2014
        ncrcmd  msg_in          [  2];
2015
        ncrcmd  msg_in2         [ 16];
2016
        ncrcmd  msg_bad         [  4];
2017
        ncrcmd  setmsg          [  7];
2018
        ncrcmd  cleanup         [  6];
2019
        ncrcmd  complete        [  9];
2020
        ncrcmd  cleanup_ok      [  8];
2021
        ncrcmd  cleanup0        [  1];
2022
#ifndef SCSI_NCR_CCB_DONE_SUPPORT
2023
        ncrcmd  signal          [ 12];
2024
#else
2025
        ncrcmd  signal          [  9];
2026
        ncrcmd  done_pos        [  1];
2027
        ncrcmd  done_plug       [  2];
2028
        ncrcmd  done_end        [  7];
2029
#endif
2030
        ncrcmd  save_dp         [  7];
2031
        ncrcmd  restore_dp      [  5];
2032
#ifdef SCSI_NCR_PROFILE_SUPPORT
2033
        ncrcmd  disconnect      [ 28];
2034
#else
2035
        ncrcmd  disconnect      [ 17];
2036
#endif
2037
        ncrcmd  msg_out         [  9];
2038
        ncrcmd  msg_out_done    [  7];
2039
        ncrcmd  idle            [  2];
2040
        ncrcmd  reselect        [  8];
2041
        ncrcmd  reselected      [  8];
2042
        ncrcmd  resel_dsa       [  6];
2043
#ifdef SCSI_NCR_PROFILE_SUPPORT
2044
        ncrcmd  loadpos1        [  7];
2045
#else
2046
        ncrcmd  loadpos1        [  4];
2047
#endif
2048
        ncrcmd  resel_lun       [  6];
2049
        ncrcmd  resel_tag       [  6];
2050
        ncrcmd  jump_to_nexus   [  4];
2051
        ncrcmd  nexus_indirect  [  4];
2052
        ncrcmd  resel_notag     [  4];
2053
        ncrcmd  data_in         [MAX_SCATTERL * 4];
2054
        ncrcmd  data_in2        [  4];
2055
        ncrcmd  data_out        [MAX_SCATTERL * 4];
2056
        ncrcmd  data_out2       [  4];
2057
};
2058
 
2059
/*
2060
**      Script fragments which stay in main memory for all chips.
2061
*/
2062
struct scripth {
2063
        ncrcmd  tryloop         [MAX_START*2];
2064
        ncrcmd  tryloop2        [  2];
2065
#ifdef SCSI_NCR_CCB_DONE_SUPPORT
2066
        ncrcmd  done_queue      [MAX_DONE*5];
2067
        ncrcmd  done_queue2     [  2];
2068
#endif
2069
        ncrcmd  select_no_atn   [  8];
2070
        ncrcmd  cancel          [  4];
2071
        ncrcmd  skip            [  9];
2072
        ncrcmd  skip2           [ 19];
2073
        ncrcmd  par_err_data_in [  6];
2074
        ncrcmd  par_err_other   [  4];
2075
        ncrcmd  msg_reject      [  8];
2076
        ncrcmd  msg_ign_residue [ 24];
2077
        ncrcmd  msg_extended    [ 10];
2078
        ncrcmd  msg_ext_2       [ 10];
2079
        ncrcmd  msg_wdtr        [ 14];
2080
        ncrcmd  send_wdtr       [  7];
2081
        ncrcmd  msg_ext_3       [ 10];
2082
        ncrcmd  msg_sdtr        [ 14];
2083
        ncrcmd  send_sdtr       [  7];
2084
        ncrcmd  msg_out_abort   [ 10];
2085
        ncrcmd  hdata_in        [MAX_SCATTERH * 4];
2086
        ncrcmd  hdata_in2       [  2];
2087
        ncrcmd  hdata_out       [MAX_SCATTERH * 4];
2088
        ncrcmd  hdata_out2      [  2];
2089
        ncrcmd  reset           [  4];
2090
        ncrcmd  aborttag        [  4];
2091
        ncrcmd  abort           [  2];
2092
        ncrcmd  abort_resel     [ 20];
2093
        ncrcmd  resend_ident    [  4];
2094
        ncrcmd  clratn_go_on    [  3];
2095
        ncrcmd  nxtdsp_go_on    [  1];
2096
        ncrcmd  sdata_in        [  8];
2097
        ncrcmd  data_io         [ 18];
2098
        ncrcmd  bad_identify    [ 12];
2099
        ncrcmd  bad_i_t_l       [  4];
2100
        ncrcmd  bad_i_t_l_q     [  4];
2101
        ncrcmd  bad_target      [  8];
2102
        ncrcmd  bad_status      [  8];
2103
        ncrcmd  start_ram       [  4];
2104
        ncrcmd  start_ram0      [  4];
2105
        ncrcmd  sto_restart     [  5];
2106
        ncrcmd  snooptest       [  9];
2107
        ncrcmd  snoopend        [  2];
2108
};
2109
 
2110
/*==========================================================
2111
**
2112
**
2113
**      Function headers.
2114
**
2115
**
2116
**==========================================================
2117
*/
2118
 
2119
static  void    ncr_alloc_ccb   (ncb_p np, u_char tn, u_char ln);
2120
static  void    ncr_complete    (ncb_p np, ccb_p cp);
2121
static  void    ncr_exception   (ncb_p np);
2122
static  void    ncr_free_ccb    (ncb_p np, ccb_p cp);
2123
static  void    ncr_init_ccb    (ncb_p np, ccb_p cp);
2124
static  void    ncr_init_tcb    (ncb_p np, u_char tn);
2125
static  lcb_p   ncr_alloc_lcb   (ncb_p np, u_char tn, u_char ln);
2126
static  lcb_p   ncr_setup_lcb   (ncb_p np, u_char tn, u_char ln,
2127
                                 u_char *inq_data);
2128
static  void    ncr_getclock    (ncb_p np, int mult);
2129
static  void    ncr_selectclock (ncb_p np, u_char scntl3);
2130
static  ccb_p   ncr_get_ccb     (ncb_p np, u_char tn, u_char ln);
2131
static  void    ncr_init        (ncb_p np, int reset, char * msg, u_long code);
2132
static  int     ncr_int_sbmc    (ncb_p np);
2133
static  int     ncr_int_par     (ncb_p np);
2134
static  void    ncr_int_ma      (ncb_p np);
2135
static  void    ncr_int_sir     (ncb_p np);
2136
static  void    ncr_int_sto     (ncb_p np);
2137
static  u_long  ncr_lookup      (char* id);
2138
static  void    ncr_negotiate   (struct ncb* np, struct tcb* tp);
2139
 
2140
#ifdef SCSI_NCR_PROFILE_SUPPORT
2141
static  void    ncb_profile     (ncb_p np, ccb_p cp);
2142
#endif
2143
 
2144
static  void    ncr_script_copy_and_bind
2145
                                (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
2146
static  void    ncr_script_fill (struct script * scr, struct scripth * scripth);
2147
static  int     ncr_scatter     (ccb_p cp, Scsi_Cmnd *cmd);
2148
static  void    ncr_getsync     (ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p);
2149
static  void    ncr_setsync     (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer);
2150
static  void    ncr_setup_tags  (ncb_p np, u_char tn, u_char ln);
2151
static  void    ncr_setwide     (ncb_p np, ccb_p cp, u_char wide, u_char ack);
2152
static  int     ncr_show_msg    (u_char * msg);
2153
static  int     ncr_snooptest   (ncb_p np);
2154
static  void    ncr_timeout     (ncb_p np);
2155
static  void    ncr_wakeup      (ncb_p np, u_long code);
2156
static  void    ncr_wakeup_done (ncb_p np);
2157
static  void    ncr_start_next_ccb (ncb_p np, lcb_p lp, int maxn);
2158
static  void    ncr_put_start_queue(ncb_p np, ccb_p cp);
2159
static  void    ncr_start_reset (ncb_p np);
2160
static  int     ncr_reset_scsi_bus (ncb_p np, int enab_int, int settle_delay);
2161
 
2162
#ifdef SCSI_NCR_USER_COMMAND_SUPPORT
2163
static  void    ncr_usercmd     (ncb_p np);
2164
#endif
2165
 
2166
static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device);
2167
 
2168
static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd);
2169
static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd);
2170
static void process_waiting_list(ncb_p np, int sts);
2171
 
2172
#define remove_from_waiting_list(np, cmd) \
2173
                retrieve_from_waiting_list(1, (np), (cmd))
2174
#define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
2175
#define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
2176
 
2177
#ifdef SCSI_NCR_NVRAM_SUPPORT
2178
static  int     ncr_get_Symbios_nvram   (ncr_slot *np, Symbios_nvram *nvram);
2179
static  int     ncr_get_Tekram_nvram    (ncr_slot *np, Tekram_nvram *nvram);
2180
#endif
2181
 
2182
/*==========================================================
2183
**
2184
**
2185
**      Global static data.
2186
**
2187
**
2188
**==========================================================
2189
*/
2190
 
2191
#ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
2192
static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
2193
#endif
2194
 
2195
static inline char *ncr_name (ncb_p np)
2196
{
2197
        return np->inst_name;
2198
}
2199
 
2200
 
2201
/*==========================================================
2202
**
2203
**
2204
**      Scripts for NCR-Processor.
2205
**
2206
**      Use ncr_script_bind for binding to physical addresses.
2207
**
2208
**
2209
**==========================================================
2210
**
2211
**      NADDR generates a reference to a field of the controller data.
2212
**      PADDR generates a reference to another part of the script.
2213
**      RADDR generates a reference to a script processor register.
2214
**      FADDR generates a reference to a script processor register
2215
**              with offset.
2216
**
2217
**----------------------------------------------------------
2218
*/
2219
 
2220
#define RELOC_SOFTC     0x40000000
2221
#define RELOC_LABEL     0x50000000
2222
#define RELOC_REGISTER  0x60000000
2223
#define RELOC_KVAR      0x70000000
2224
#define RELOC_LABELH    0x80000000
2225
#define RELOC_MASK      0xf0000000
2226
 
2227
#define NADDR(label)    (RELOC_SOFTC | offsetof(struct ncb, label))
2228
#define PADDR(label)    (RELOC_LABEL | offsetof(struct script, label))
2229
#define PADDRH(label)   (RELOC_LABELH | offsetof(struct scripth, label))
2230
#define RADDR(label)    (RELOC_REGISTER | REG(label))
2231
#define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
2232
#define KVAR(which)     (RELOC_KVAR | (which))
2233
 
2234
#define SCRIPT_KVAR_JIFFIES     (0)
2235
 
2236
#define SCRIPT_KVAR_FIRST               SCRIPT_KVAR_JIFFIES
2237
#define SCRIPT_KVAR_LAST                SCRIPT_KVAR_JIFFIES
2238
 
2239
/*
2240
 * Kernel variables referenced in the scripts.
2241
 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
2242
 */
2243
static void *script_kvars[] __initdata =
2244
        { (void *)&jiffies };
2245
 
2246
static  struct script script0 __initdata = {
2247
/*--------------------------< START >-----------------------*/ {
2248
        /*
2249
        **      This NOP will be patched with LED ON
2250
        **      SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2251
        */
2252
        SCR_NO_OP,
2253
                0,
2254
        /*
2255
        **      Clear SIGP.
2256
        */
2257
        SCR_FROM_REG (ctest2),
2258
                0,
2259
        /*
2260
        **      Then jump to a certain point in tryloop.
2261
        **      Due to the lack of indirect addressing the code
2262
        **      is self modifying here.
2263
        */
2264
        SCR_JUMP,
2265
}/*-------------------------< STARTPOS >--------------------*/,{
2266
                PADDRH(tryloop),
2267
 
2268
}/*-------------------------< SELECT >----------------------*/,{
2269
        /*
2270
        **      DSA     contains the address of a scheduled
2271
        **              data structure.
2272
        **
2273
        **      SCRATCHA contains the address of the script,
2274
        **              which starts the next entry.
2275
        **
2276
        **      Set Initiator mode.
2277
        **
2278
        **      (Target mode is left as an exercise for the reader)
2279
        */
2280
 
2281
        SCR_CLR (SCR_TRG),
2282
                0,
2283
        SCR_LOAD_REG (HS_REG, HS_SELECTING),
2284
                0,
2285
 
2286
        /*
2287
        **      And try to select this target.
2288
        */
2289
        SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2290
                PADDR (reselect),
2291
 
2292
}/*-------------------------< SELECT2 >----------------------*/,{
2293
        /*
2294
        **      Now there are 4 possibilities:
2295
        **
2296
        **      (1) The ncr looses arbitration.
2297
        **      This is ok, because it will try again,
2298
        **      when the bus becomes idle.
2299
        **      (But beware of the timeout function!)
2300
        **
2301
        **      (2) The ncr is reselected.
2302
        **      Then the script processor takes the jump
2303
        **      to the RESELECT label.
2304
        **
2305
        **      (3) The ncr wins arbitration.
2306
        **      Then it will execute SCRIPTS instruction until
2307
        **      the next instruction that checks SCSI phase.
2308
        **      Then will stop and wait for selection to be
2309
        **      complete or selection time-out to occur.
2310
        **      As a result the SCRIPTS instructions until
2311
        **      LOADPOS + 2 should be executed in parallel with
2312
        **      the SCSI core performing selection.
2313
        */
2314
 
2315
        /*
2316
        **      The M_REJECT problem seems to be due to a selection
2317
        **      timing problem.
2318
        **      Wait immediately for the selection to complete.
2319
        **      (2.5x behaves so)
2320
        */
2321
        SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2322
                0,
2323
 
2324
        /*
2325
        **      Next time use the next slot.
2326
        */
2327
        SCR_COPY (4),
2328
                RADDR (temp),
2329
                PADDR (startpos),
2330
        /*
2331
        **      The ncr doesn't have an indirect load
2332
        **      or store command. So we have to
2333
        **      copy part of the control block to a
2334
        **      fixed place, where we can access it.
2335
        **
2336
        **      We patch the address part of a
2337
        **      COPY command with the DSA-register.
2338
        */
2339
        SCR_COPY_F (4),
2340
                RADDR (dsa),
2341
                PADDR (loadpos),
2342
        /*
2343
        **      then we do the actual copy.
2344
        */
2345
        SCR_COPY (sizeof (struct head)),
2346
        /*
2347
        **      continued after the next label ...
2348
        */
2349
}/*-------------------------< LOADPOS >---------------------*/,{
2350
                0,
2351
                NADDR (header),
2352
        /*
2353
        **      Wait for the next phase or the selection
2354
        **      to complete or time-out.
2355
        */
2356
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2357
                PADDR (prepare),
2358
 
2359
}/*-------------------------< SEND_IDENT >----------------------*/,{
2360
        /*
2361
        **      Selection complete.
2362
        **      Send the IDENTIFY and SIMPLE_TAG messages
2363
        **      (and the M_X_SYNC_REQ message)
2364
        */
2365
        SCR_MOVE_TBL ^ SCR_MSG_OUT,
2366
                offsetof (struct dsb, smsg),
2367
        SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2368
                PADDRH (resend_ident),
2369
        SCR_LOAD_REG (scratcha, 0x80),
2370
                0,
2371
        SCR_COPY (1),
2372
                RADDR (scratcha),
2373
                NADDR (lastmsg),
2374
}/*-------------------------< PREPARE >----------------------*/,{
2375
        /*
2376
        **      load the savep (saved pointer) into
2377
        **      the TEMP register (actual pointer)
2378
        */
2379
        SCR_COPY (4),
2380
                NADDR (header.savep),
2381
                RADDR (temp),
2382
        /*
2383
        **      Initialize the status registers
2384
        */
2385
        SCR_COPY (4),
2386
                NADDR (header.status),
2387
                RADDR (scr0),
2388
}/*-------------------------< PREPARE2 >---------------------*/,{
2389
        /*
2390
        **      Initialize the msgout buffer with a NOOP message.
2391
        */
2392
        SCR_LOAD_REG (scratcha, M_NOOP),
2393
                0,
2394
        SCR_COPY (1),
2395
                RADDR (scratcha),
2396
                NADDR (msgout),
2397
#if 0
2398
        SCR_COPY (1),
2399
                RADDR (scratcha),
2400
                NADDR (msgin),
2401
#endif
2402
        /*
2403
        **      Anticipate the COMMAND phase.
2404
        **      This is the normal case for initial selection.
2405
        */
2406
        SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
2407
                PADDR (dispatch),
2408
 
2409
}/*-------------------------< COMMAND >--------------------*/,{
2410
#ifdef SCSI_NCR_PROFILE_SUPPORT
2411
        /*
2412
        **      ... set a timestamp ...
2413
        */
2414
        SCR_COPY (sizeof (u_long)),
2415
                KVAR(SCRIPT_KVAR_JIFFIES),
2416
                NADDR (header.stamp.command),
2417
#endif
2418
        /*
2419
        **      ... and send the command
2420
        */
2421
        SCR_MOVE_TBL ^ SCR_COMMAND,
2422
                offsetof (struct dsb, cmd),
2423
        /*
2424
        **      If status is still HS_NEGOTIATE, negotiation failed.
2425
        **      We check this here, since we want to do that
2426
        **      only once.
2427
        */
2428
        SCR_FROM_REG (HS_REG),
2429
                0,
2430
        SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2431
                SIR_NEGO_FAILED,
2432
 
2433
}/*-----------------------< DISPATCH >----------------------*/,{
2434
        /*
2435
        **      MSG_IN is the only phase that shall be
2436
        **      entered at least once for each (re)selection.
2437
        **      So we test it first.
2438
        */
2439
        SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
2440
                PADDR (msg_in),
2441
 
2442
        SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
2443
                0,
2444
        /*
2445
        **      DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
2446
        **      Possible data corruption during Memory Write and Invalidate.
2447
        **      This work-around resets the addressing logic prior to the
2448
        **      start of the first MOVE of a DATA IN phase.
2449
        **      (See README.ncr53c8xx for more information)
2450
        */
2451
        SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2452
                20,
2453
        SCR_COPY (4),
2454
                RADDR (scratcha),
2455
                RADDR (scratcha),
2456
        SCR_RETURN,
2457
                0,
2458
        SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
2459
                PADDR (status),
2460
        SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
2461
                PADDR (command),
2462
        SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
2463
                PADDR (msg_out),
2464
        /*
2465
        **      Discard one illegal phase byte, if required.
2466
        */
2467
        SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
2468
                0,
2469
        SCR_COPY (1),
2470
                RADDR (scratcha),
2471
                NADDR (xerr_st),
2472
        SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
2473
                8,
2474
        SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
2475
                NADDR (scratch),
2476
        SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
2477
                8,
2478
        SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
2479
                NADDR (scratch),
2480
        SCR_JUMP,
2481
                PADDR (dispatch),
2482
 
2483
}/*-------------------------< CLRACK >----------------------*/,{
2484
        /*
2485
        **      Terminate possible pending message phase.
2486
        */
2487
        SCR_CLR (SCR_ACK),
2488
                0,
2489
        SCR_JUMP,
2490
                PADDR (dispatch),
2491
 
2492
}/*-------------------------< NO_DATA >--------------------*/,{
2493
        /*
2494
        **      The target wants to tranfer too much data
2495
        **      or in the wrong direction.
2496
        **      Remember that in extended error.
2497
        */
2498
        SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
2499
                0,
2500
        SCR_COPY (1),
2501
                RADDR (scratcha),
2502
                NADDR (xerr_st),
2503
        /*
2504
        **      Discard one data byte, if required.
2505
        */
2506
        SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2507
                8,
2508
        SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
2509
                NADDR (scratch),
2510
        SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
2511
                8,
2512
        SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2513
                NADDR (scratch),
2514
        /*
2515
        **      .. and repeat as required.
2516
        */
2517
        SCR_CALL,
2518
                PADDR (dispatch),
2519
        SCR_JUMP,
2520
                PADDR (no_data),
2521
 
2522
}/*-------------------------< STATUS >--------------------*/,{
2523
#ifdef SCSI_NCR_PROFILE_SUPPORT
2524
        /*
2525
        **      set the timestamp.
2526
        */
2527
        SCR_COPY (sizeof (u_long)),
2528
                KVAR(SCRIPT_KVAR_JIFFIES),
2529
                NADDR (header.stamp.status),
2530
#endif
2531
        /*
2532
        **      get the status
2533
        */
2534
        SCR_MOVE_ABS (1) ^ SCR_STATUS,
2535
                NADDR (scratch),
2536
        /*
2537
        **      save status to scsi_status.
2538
        **      mark as complete.
2539
        */
2540
        SCR_TO_REG (SS_REG),
2541
                0,
2542
        SCR_LOAD_REG (HS_REG, HS_COMPLETE),
2543
                0,
2544
        SCR_JUMP,
2545
                PADDR (dispatch),
2546
}/*-------------------------< MSG_IN >--------------------*/,{
2547
        /*
2548
        **      Get the first byte of the message
2549
        **      and save it to SCRATCHA.
2550
        **
2551
        **      The script processor doesn't negate the
2552
        **      ACK signal after this transfer.
2553
        */
2554
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2555
                NADDR (msgin[0]),
2556
}/*-------------------------< MSG_IN2 >--------------------*/,{
2557
        /*
2558
        **      Handle this message.
2559
        */
2560
        SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
2561
                PADDR (complete),
2562
        SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
2563
                PADDR (disconnect),
2564
        SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
2565
                PADDR (save_dp),
2566
        SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
2567
                PADDR (restore_dp),
2568
        SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
2569
                PADDRH (msg_extended),
2570
        SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
2571
                PADDR (clrack),
2572
        SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
2573
                PADDRH (msg_reject),
2574
        SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
2575
                PADDRH (msg_ign_residue),
2576
        /*
2577
        **      Rest of the messages left as
2578
        **      an exercise ...
2579
        **
2580
        **      Unimplemented messages:
2581
        **      fall through to MSG_BAD.
2582
        */
2583
}/*-------------------------< MSG_BAD >------------------*/,{
2584
        /*
2585
        **      unimplemented message - reject it.
2586
        */
2587
        SCR_INT,
2588
                SIR_REJECT_SENT,
2589
        SCR_LOAD_REG (scratcha, M_REJECT),
2590
                0,
2591
}/*-------------------------< SETMSG >----------------------*/,{
2592
        SCR_COPY (1),
2593
                RADDR (scratcha),
2594
                NADDR (msgout),
2595
        SCR_SET (SCR_ATN),
2596
                0,
2597
        SCR_JUMP,
2598
                PADDR (clrack),
2599
}/*-------------------------< CLEANUP >-------------------*/,{
2600
        /*
2601
        **      dsa:    Pointer to ccb
2602
        **            or xxxxxxFF (no ccb)
2603
        **
2604
        **      HS_REG:   Host-Status (<>0!)
2605
        */
2606
        SCR_FROM_REG (dsa),
2607
                0,
2608
        SCR_JUMP ^ IFTRUE (DATA (0xff)),
2609
                PADDR (start),
2610
        /*
2611
        **      dsa is valid.
2612
        **      complete the cleanup.
2613
        */
2614
        SCR_JUMP,
2615
                PADDR (cleanup_ok),
2616
 
2617
}/*-------------------------< COMPLETE >-----------------*/,{
2618
        /*
2619
        **      Complete message.
2620
        **
2621
        **      Copy TEMP register to LASTP in header.
2622
        */
2623
        SCR_COPY (4),
2624
                RADDR (temp),
2625
                NADDR (header.lastp),
2626
        /*
2627
        **      When we terminate the cycle by clearing ACK,
2628
        **      the target may disconnect immediately.
2629
        **
2630
        **      We don't want to be told of an
2631
        **      "unexpected disconnect",
2632
        **      so we disable this feature.
2633
        */
2634
        SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2635
                0,
2636
        /*
2637
        **      Terminate cycle ...
2638
        */
2639
        SCR_CLR (SCR_ACK|SCR_ATN),
2640
                0,
2641
        /*
2642
        **      ... and wait for the disconnect.
2643
        */
2644
        SCR_WAIT_DISC,
2645
                0,
2646
}/*-------------------------< CLEANUP_OK >----------------*/,{
2647
        /*
2648
        **      Save host status to header.
2649
        */
2650
        SCR_COPY (4),
2651
                RADDR (scr0),
2652
                NADDR (header.status),
2653
        /*
2654
        **      and copy back the header to the ccb.
2655
        */
2656
        SCR_COPY_F (4),
2657
                RADDR (dsa),
2658
                PADDR (cleanup0),
2659
        SCR_COPY (sizeof (struct head)),
2660
                NADDR (header),
2661
}/*-------------------------< CLEANUP0 >--------------------*/,{
2662
                0,
2663
}/*-------------------------< SIGNAL >----------------------*/,{
2664
        /*
2665
        **      if job not completed ...
2666
        */
2667
        SCR_FROM_REG (HS_REG),
2668
                0,
2669
        /*
2670
        **      ... start the next command.
2671
        */
2672
        SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
2673
                PADDR(start),
2674
        /*
2675
        **      If command resulted in not GOOD status,
2676
        **      call the C code if needed.
2677
        */
2678
        SCR_FROM_REG (SS_REG),
2679
                0,
2680
        SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
2681
                PADDRH (bad_status),
2682
 
2683
#ifndef SCSI_NCR_CCB_DONE_SUPPORT
2684
 
2685
        /*
2686
        **      ... signal completion to the host
2687
        */
2688
        SCR_INT_FLY,
2689
                0,
2690
        /*
2691
        **      Auf zu neuen Schandtaten!
2692
        */
2693
        SCR_JUMP,
2694
                PADDR(start),
2695
 
2696
#else   /* defined SCSI_NCR_CCB_DONE_SUPPORT */
2697
 
2698
        /*
2699
        **      ... signal completion to the host
2700
        */
2701
        SCR_JUMP,
2702
}/*------------------------< DONE_POS >---------------------*/,{
2703
                PADDRH (done_queue),
2704
}/*------------------------< DONE_PLUG >--------------------*/,{
2705
        SCR_INT,
2706
                SIR_DONE_OVERFLOW,
2707
}/*------------------------< DONE_END >---------------------*/,{
2708
        SCR_INT_FLY,
2709
                0,
2710
        SCR_COPY (4),
2711
                RADDR (temp),
2712
                PADDR (done_pos),
2713
        SCR_JUMP,
2714
                PADDR (start),
2715
 
2716
#endif  /* SCSI_NCR_CCB_DONE_SUPPORT */
2717
 
2718
}/*-------------------------< SAVE_DP >------------------*/,{
2719
        /*
2720
        **      SAVE_DP message:
2721
        **      Copy TEMP register to SAVEP in header.
2722
        */
2723
        SCR_COPY (4),
2724
                RADDR (temp),
2725
                NADDR (header.savep),
2726
        SCR_CLR (SCR_ACK),
2727
                0,
2728
        SCR_JUMP,
2729
                PADDR (dispatch),
2730
}/*-------------------------< RESTORE_DP >---------------*/,{
2731
        /*
2732
        **      RESTORE_DP message:
2733
        **      Copy SAVEP in header to TEMP register.
2734
        */
2735
        SCR_COPY (4),
2736
                NADDR (header.savep),
2737
                RADDR (temp),
2738
        SCR_JUMP,
2739
                PADDR (clrack),
2740
 
2741
}/*-------------------------< DISCONNECT >---------------*/,{
2742
        /*
2743
        **      DISCONNECTing  ...
2744
        **
2745
        **      disable the "unexpected disconnect" feature,
2746
        **      and remove the ACK signal.
2747
        */
2748
        SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2749
                0,
2750
        SCR_CLR (SCR_ACK|SCR_ATN),
2751
                0,
2752
        /*
2753
        **      Wait for the disconnect.
2754
        */
2755
        SCR_WAIT_DISC,
2756
                0,
2757
#ifdef SCSI_NCR_PROFILE_SUPPORT
2758
        /*
2759
        **      Profiling:
2760
        **      Set a time stamp,
2761
        **      and count the disconnects.
2762
        */
2763
        SCR_COPY (sizeof (u_long)),
2764
                KVAR(SCRIPT_KVAR_JIFFIES),
2765
                NADDR (header.stamp.disconnect),
2766
        SCR_COPY (4),
2767
                NADDR (disc_phys),
2768
                RADDR (scratcha),
2769
        SCR_REG_REG (scratcha, SCR_ADD, 0x01),
2770
                0,
2771
        SCR_COPY (4),
2772
                RADDR (scratcha),
2773
                NADDR (disc_phys),
2774
#endif
2775
        /*
2776
        **      Status is: DISCONNECTED.
2777
        */
2778
        SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2779
                0,
2780
        /*
2781
        **      If QUIRK_AUTOSAVE is set,
2782
        **      do an "save pointer" operation.
2783
        */
2784
        SCR_FROM_REG (QU_REG),
2785
                0,
2786
        SCR_JUMP ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
2787
                PADDR (cleanup_ok),
2788
        /*
2789
        **      like SAVE_DP message:
2790
        **      Copy TEMP register to SAVEP in header.
2791
        */
2792
        SCR_COPY (4),
2793
                RADDR (temp),
2794
                NADDR (header.savep),
2795
        SCR_JUMP,
2796
                PADDR (cleanup_ok),
2797
 
2798
}/*-------------------------< MSG_OUT >-------------------*/,{
2799
        /*
2800
        **      The target requests a message.
2801
        */
2802
        SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2803
                NADDR (msgout),
2804
        SCR_COPY (1),
2805
                NADDR (msgout),
2806
                NADDR (lastmsg),
2807
        /*
2808
        **      If it was no ABORT message ...
2809
        */
2810
        SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
2811
                PADDRH (msg_out_abort),
2812
        /*
2813
        **      ... wait for the next phase
2814
        **      if it's a message out, send it again, ...
2815
        */
2816
        SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2817
                PADDR (msg_out),
2818
}/*-------------------------< MSG_OUT_DONE >--------------*/,{
2819
        /*
2820
        **      ... else clear the message ...
2821
        */
2822
        SCR_LOAD_REG (scratcha, M_NOOP),
2823
                0,
2824
        SCR_COPY (4),
2825
                RADDR (scratcha),
2826
                NADDR (msgout),
2827
        /*
2828
        **      ... and process the next phase
2829
        */
2830
        SCR_JUMP,
2831
                PADDR (dispatch),
2832
}/*-------------------------< IDLE >------------------------*/,{
2833
        /*
2834
        **      Nothing to do?
2835
        **      Wait for reselect.
2836
        **      This NOP will be patched with LED OFF
2837
        **      SCR_REG_REG (gpreg, SCR_OR, 0x01)
2838
        */
2839
        SCR_NO_OP,
2840
                0,
2841
}/*-------------------------< RESELECT >--------------------*/,{
2842
        /*
2843
        **      make the DSA invalid.
2844
        */
2845
        SCR_LOAD_REG (dsa, 0xff),
2846
                0,
2847
        SCR_CLR (SCR_TRG),
2848
                0,
2849
        SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
2850
                0,
2851
        /*
2852
        **      Sleep waiting for a reselection.
2853
        **      If SIGP is set, special treatment.
2854
        **
2855
        **      Zu allem bereit ..
2856
        */
2857
        SCR_WAIT_RESEL,
2858
                PADDR(start),
2859
}/*-------------------------< RESELECTED >------------------*/,{
2860
        /*
2861
        **      This NOP will be patched with LED ON
2862
        **      SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2863
        */
2864
        SCR_NO_OP,
2865
                0,
2866
        /*
2867
        **      ... zu nichts zu gebrauchen ?
2868
        **
2869
        **      load the target id into the SFBR
2870
        **      and jump to the control block.
2871
        **
2872
        **      Look at the declarations of
2873
        **      - struct ncb
2874
        **      - struct tcb
2875
        **      - struct lcb
2876
        **      - struct ccb
2877
        **      to understand what's going on.
2878
        */
2879
        SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2880
                0,
2881
        SCR_TO_REG (sdid),
2882
                0,
2883
        SCR_JUMP,
2884
                NADDR (jump_tcb),
2885
 
2886
}/*-------------------------< RESEL_DSA >-------------------*/,{
2887
        /*
2888
        **      Ack the IDENTIFY or TAG previously received.
2889
        */
2890
        SCR_CLR (SCR_ACK),
2891
                0,
2892
        /*
2893
        **      The ncr doesn't have an indirect load
2894
        **      or store command. So we have to
2895
        **      copy part of the control block to a
2896
        **      fixed place, where we can access it.
2897
        **
2898
        **      We patch the address part of a
2899
        **      COPY command with the DSA-register.
2900
        */
2901
        SCR_COPY_F (4),
2902
                RADDR (dsa),
2903
                PADDR (loadpos1),
2904
        /*
2905
        **      then we do the actual copy.
2906
        */
2907
        SCR_COPY (sizeof (struct head)),
2908
        /*
2909
        **      continued after the next label ...
2910
        */
2911
 
2912
}/*-------------------------< LOADPOS1 >-------------------*/,{
2913
                0,
2914
                NADDR (header),
2915
#ifdef SCSI_NCR_PROFILE_SUPPORT
2916
        /*
2917
        **      Set a time stamp for this reselection
2918
        */
2919
        SCR_COPY (sizeof (u_long)),
2920
                KVAR(SCRIPT_KVAR_JIFFIES),
2921
                NADDR (header.stamp.reselect),
2922
#endif
2923
        /*
2924
        **      The DSA contains the data structure address.
2925
        */
2926
        SCR_JUMP,
2927
                PADDR (prepare),
2928
 
2929
}/*-------------------------< RESEL_LUN >-------------------*/,{
2930
        /*
2931
        **      come back to this point
2932
        **      to get an IDENTIFY message
2933
        **      Wait for a msg_in phase.
2934
        */
2935
        SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2936
                SIR_RESEL_NO_MSG_IN,
2937
        /*
2938
        **      message phase.
2939
        **      Read the data directly from the BUS DATA lines.
2940
        **      This helps to support very old SCSI devices that
2941
        **      may reselect without sending an IDENTIFY.
2942
        */
2943
        SCR_FROM_REG (sbdl),
2944
                0,
2945
        /*
2946
        **      It should be an Identify message.
2947
        */
2948
        SCR_RETURN,
2949
                0,
2950
}/*-------------------------< RESEL_TAG >-------------------*/,{
2951
        /*
2952
        **      Read IDENTIFY + SIMPLE + TAG using a single MOVE.
2953
        **      Agressive optimization, is'nt it?
2954
        **      No need to test the SIMPLE TAG message, since the
2955
        **      driver only supports conformant devices for tags. ;-)
2956
        */
2957
        SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
2958
                NADDR (msgin),
2959
        /*
2960
        **      Read the TAG from the SIDL.
2961
        **      Still an aggressive optimization. ;-)
2962
        **      Compute the CCB indirect jump address which
2963
        **      is (#TAG*2 & 0xfc) due to tag numbering using
2964
        **      1,3,5..MAXTAGS*2+1 actual values.
2965
        */
2966
        SCR_REG_SFBR (sidl, SCR_SHL, 0),
2967
                0,
2968
        SCR_SFBR_REG (temp, SCR_AND, 0xfc),
2969
                0,
2970
}/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
2971
        SCR_COPY_F (4),
2972
                RADDR (temp),
2973
                PADDR (nexus_indirect),
2974
        SCR_COPY (4),
2975
}/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
2976
                0,
2977
                RADDR (temp),
2978
        SCR_RETURN,
2979
                0,
2980
}/*-------------------------< RESEL_NOTAG >-------------------*/,{
2981
        /*
2982
        **      No tag expected.
2983
        **      Read an throw away the IDENTIFY.
2984
        */
2985
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2986
                NADDR (msgin),
2987
        SCR_JUMP,
2988
                PADDR (jump_to_nexus),
2989
}/*-------------------------< DATA_IN >--------------------*/,{
2990
/*
2991
**      Because the size depends on the
2992
**      #define MAX_SCATTERL parameter,
2993
**      it is filled in at runtime.
2994
**
2995
**  ##===========< i=0; i<MAX_SCATTERL >=========
2996
**  ||  SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2997
**  ||          PADDR (dispatch),
2998
**  ||  SCR_MOVE_TBL ^ SCR_DATA_IN,
2999
**  ||          offsetof (struct dsb, data[ i]),
3000
**  ##==========================================
3001
**
3002
**---------------------------------------------------------
3003
*/
3004
 
3005
}/*-------------------------< DATA_IN2 >-------------------*/,{
3006
        SCR_CALL,
3007
                PADDR (dispatch),
3008
        SCR_JUMP,
3009
                PADDR (no_data),
3010
}/*-------------------------< DATA_OUT >--------------------*/,{
3011
/*
3012
**      Because the size depends on the
3013
**      #define MAX_SCATTERL parameter,
3014
**      it is filled in at runtime.
3015
**
3016
**  ##===========< i=0; i<MAX_SCATTERL >=========
3017
**  ||  SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3018
**  ||          PADDR (dispatch),
3019
**  ||  SCR_MOVE_TBL ^ SCR_DATA_OUT,
3020
**  ||          offsetof (struct dsb, data[ i]),
3021
**  ##==========================================
3022
**
3023
**---------------------------------------------------------
3024
*/
3025
 
3026
}/*-------------------------< DATA_OUT2 >-------------------*/,{
3027
        SCR_CALL,
3028
                PADDR (dispatch),
3029
        SCR_JUMP,
3030
                PADDR (no_data),
3031
}/*--------------------------------------------------------*/
3032
};
3033
 
3034
static  struct scripth scripth0 __initdata = {
3035
/*-------------------------< TRYLOOP >---------------------*/{
3036
/*
3037
**      Start the next entry.
3038
**      Called addresses point to the launch script in the CCB.
3039
**      They are patched by the main processor.
3040
**
3041
**      Because the size depends on the
3042
**      #define MAX_START parameter, it is filled
3043
**      in at runtime.
3044
**
3045
**-----------------------------------------------------------
3046
**
3047
**  ##===========< I=0; i<MAX_START >===========
3048
**  ||  SCR_CALL,
3049
**  ||          PADDR (idle),
3050
**  ##==========================================
3051
**
3052
**-----------------------------------------------------------
3053
*/
3054
 
3055
}/*------------------------< TRYLOOP2 >---------------------*/,{
3056
        SCR_JUMP,
3057
                PADDRH(tryloop),
3058
 
3059
#ifdef SCSI_NCR_CCB_DONE_SUPPORT
3060
 
3061
}/*------------------------< DONE_QUEUE >-------------------*/,{
3062
/*
3063
**      Copy the CCB address to the next done entry.
3064
**      Because the size depends on the
3065
**      #define MAX_DONE parameter, it is filled
3066
**      in at runtime.
3067
**
3068
**-----------------------------------------------------------
3069
**
3070
**  ##===========< I=0; i<MAX_DONE >===========
3071
**  ||  SCR_COPY (sizeof(ccb_p)),
3072
**  ||          NADDR (header.cp),
3073
**  ||          NADDR (ccb_done[i]),
3074
**  ||  SCR_CALL,
3075
**  ||          PADDR (done_end),
3076
**  ##==========================================
3077
**
3078
**-----------------------------------------------------------
3079
*/
3080
 
3081
}/*------------------------< DONE_QUEUE2 >------------------*/,{
3082
        SCR_JUMP,
3083
                PADDRH (done_queue),
3084
 
3085
#endif /* SCSI_NCR_CCB_DONE_SUPPORT */
3086
}/*------------------------< SELECT_NO_ATN >-----------------*/,{
3087
        /*
3088
        **      Set Initiator mode.
3089
        **      And try to select this target without ATN.
3090
        */
3091
 
3092
        SCR_CLR (SCR_TRG),
3093
                0,
3094
        SCR_LOAD_REG (HS_REG, HS_SELECTING),
3095
                0,
3096
        SCR_SEL_TBL ^ offsetof (struct dsb, select),
3097
                PADDR (reselect),
3098
        SCR_JUMP,
3099
                PADDR (select2),
3100
 
3101
}/*-------------------------< CANCEL >------------------------*/,{
3102
 
3103
        SCR_LOAD_REG (scratcha, HS_ABORTED),
3104
                0,
3105
        SCR_JUMPR,
3106
                8,
3107
}/*-------------------------< SKIP >------------------------*/,{
3108
        SCR_LOAD_REG (scratcha, 0),
3109
                0,
3110
        /*
3111
        **      This entry has been canceled.
3112
        **      Next time use the next slot.
3113
        */
3114
        SCR_COPY (4),
3115
                RADDR (temp),
3116
                PADDR (startpos),
3117
        /*
3118
        **      The ncr doesn't have an indirect load
3119
        **      or store command. So we have to
3120
        **      copy part of the control block to a
3121
        **      fixed place, where we can access it.
3122
        **
3123
        **      We patch the address part of a
3124
        **      COPY command with the DSA-register.
3125
        */
3126
        SCR_COPY_F (4),
3127
                RADDR (dsa),
3128
                PADDRH (skip2),
3129
        /*
3130
        **      then we do the actual copy.
3131
        */
3132
        SCR_COPY (sizeof (struct head)),
3133
        /*
3134
        **      continued after the next label ...
3135
        */
3136
}/*-------------------------< SKIP2 >---------------------*/,{
3137
                0,
3138
                NADDR (header),
3139
        /*
3140
        **      Initialize the status registers
3141
        */
3142
        SCR_COPY (4),
3143
                NADDR (header.status),
3144
                RADDR (scr0),
3145
        /*
3146
        **      Force host status.
3147
        */
3148
        SCR_FROM_REG (scratcha),
3149
                0,
3150
        SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
3151
                16,
3152
        SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
3153
                0,
3154
        SCR_JUMPR,
3155
                8,
3156
        SCR_TO_REG (HS_REG),
3157
                0,
3158
        SCR_LOAD_REG (SS_REG, S_GOOD),
3159
                0,
3160
        SCR_JUMP,
3161
                PADDR (cleanup_ok),
3162
 
3163
},/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
3164
        /*
3165
        **      Ignore all data in byte, until next phase
3166
        */
3167
        SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
3168
                PADDRH (par_err_other),
3169
        SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
3170
                NADDR (scratch),
3171
        SCR_JUMPR,
3172
                -24,
3173
},/*-------------------------< PAR_ERR_OTHER >------------------*/{
3174
        /*
3175
        **      count it.
3176
        */
3177
        SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
3178
                0,
3179
        /*
3180
        **      jump to dispatcher.
3181
        */
3182
        SCR_JUMP,
3183
                PADDR (dispatch),
3184
}/*-------------------------< MSG_REJECT >---------------*/,{
3185
        /*
3186
        **      If a negotiation was in progress,
3187
        **      negotiation failed.
3188
        */
3189
        SCR_FROM_REG (HS_REG),
3190
                0,
3191
        SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
3192
                SIR_NEGO_FAILED,
3193
        /*
3194
        **      else make host log this message
3195
        */
3196
        SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
3197
                SIR_REJECT_RECEIVED,
3198
        SCR_JUMP,
3199
                PADDR (clrack),
3200
 
3201
}/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
3202
        /*
3203
        **      Terminate cycle
3204
        */
3205
        SCR_CLR (SCR_ACK),
3206
                0,
3207
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3208
                PADDR (dispatch),
3209
        /*
3210
        **      get residue size.
3211
        */
3212
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3213
                NADDR (msgin[1]),
3214
        /*
3215
        **      Size is 0 .. ignore message.
3216
        */
3217
        SCR_JUMP ^ IFTRUE (DATA (0)),
3218
                PADDR (clrack),
3219
        /*
3220
        **      Size is not 1 .. have to interrupt.
3221
        */
3222
        SCR_JUMPR ^ IFFALSE (DATA (1)),
3223
                40,
3224
        /*
3225
        **      Check for residue byte in swide register
3226
        */
3227
        SCR_FROM_REG (scntl2),
3228
                0,
3229
        SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
3230
                16,
3231
        /*
3232
        **      There IS data in the swide register.
3233
        **      Discard it.
3234
        */
3235
        SCR_REG_REG (scntl2, SCR_OR, WSR),
3236
                0,
3237
        SCR_JUMP,
3238
                PADDR (clrack),
3239
        /*
3240
        **      Load again the size to the sfbr register.
3241
        */
3242
        SCR_FROM_REG (scratcha),
3243
                0,
3244
        SCR_INT,
3245
                SIR_IGN_RESIDUE,
3246
        SCR_JUMP,
3247
                PADDR (clrack),
3248
 
3249
}/*-------------------------< MSG_EXTENDED >-------------*/,{
3250
        /*
3251
        **      Terminate cycle
3252
        */
3253
        SCR_CLR (SCR_ACK),
3254
                0,
3255
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3256
                PADDR (dispatch),
3257
        /*
3258
        **      get length.
3259
        */
3260
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3261
                NADDR (msgin[1]),
3262
        /*
3263
        */
3264
        SCR_JUMP ^ IFTRUE (DATA (3)),
3265
                PADDRH (msg_ext_3),
3266
        SCR_JUMP ^ IFFALSE (DATA (2)),
3267
                PADDR (msg_bad),
3268
}/*-------------------------< MSG_EXT_2 >----------------*/,{
3269
        SCR_CLR (SCR_ACK),
3270
                0,
3271
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3272
                PADDR (dispatch),
3273
        /*
3274
        **      get extended message code.
3275
        */
3276
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3277
                NADDR (msgin[2]),
3278
        SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
3279
                PADDRH (msg_wdtr),
3280
        /*
3281
        **      unknown extended message
3282
        */
3283
        SCR_JUMP,
3284
                PADDR (msg_bad)
3285
}/*-------------------------< MSG_WDTR >-----------------*/,{
3286
        SCR_CLR (SCR_ACK),
3287
                0,
3288
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3289
                PADDR (dispatch),
3290
        /*
3291
        **      get data bus width
3292
        */
3293
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3294
                NADDR (msgin[3]),
3295
        /*
3296
        **      let the host do the real work.
3297
        */
3298
        SCR_INT,
3299
                SIR_NEGO_WIDE,
3300
        /*
3301
        **      let the target fetch our answer.
3302
        */
3303
        SCR_SET (SCR_ATN),
3304
                0,
3305
        SCR_CLR (SCR_ACK),
3306
                0,
3307
 
3308
/* CHECK THE SOURCE FOR 'send_wdtr' IF YOU INTEND TO CHANGE SOMETHING HERE */
3309
        SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3310
                SIR_NEGO_PROTO,
3311
}/*-------------------------< SEND_WDTR >----------------*/,{
3312
        /*
3313
        **      Send the M_X_WIDE_REQ
3314
        */
3315
        SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
3316
                NADDR (msgout),
3317
        SCR_COPY (1),
3318
                NADDR (msgout),
3319
                NADDR (lastmsg),
3320
        SCR_JUMP,
3321
                PADDR (msg_out_done),
3322
 
3323
}/*-------------------------< MSG_EXT_3 >----------------*/,{
3324
        SCR_CLR (SCR_ACK),
3325
                0,
3326
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3327
                PADDR (dispatch),
3328
        /*
3329
        **      get extended message code.
3330
        */
3331
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3332
                NADDR (msgin[2]),
3333
        SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
3334
                PADDRH (msg_sdtr),
3335
        /*
3336
        **      unknown extended message
3337
        */
3338
        SCR_JUMP,
3339
                PADDR (msg_bad)
3340
 
3341
}/*-------------------------< MSG_SDTR >-----------------*/,{
3342
        SCR_CLR (SCR_ACK),
3343
                0,
3344
        SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3345
                PADDR (dispatch),
3346
        /*
3347
        **      get period and offset
3348
        */
3349
        SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
3350
                NADDR (msgin[3]),
3351
        /*
3352
        **      let the host do the real work.
3353
        */
3354
        SCR_INT,
3355
                SIR_NEGO_SYNC,
3356
        /*
3357
        **      let the target fetch our answer.
3358
        */
3359
        SCR_SET (SCR_ATN),
3360
                0,
3361
        SCR_CLR (SCR_ACK),
3362
                0,
3363
 
3364
/* CHECK THE SOURCE FOR 'send_sdtr' IF YOU INTEND TO CHANGE SOMETHING HERE */
3365
        SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3366
                SIR_NEGO_PROTO,
3367
}/*-------------------------< SEND_SDTR >-------------*/,{
3368
        /*
3369
        **      Send the M_X_SYNC_REQ
3370
        */
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),
3378
 
3379
}/*-------------------------< MSG_OUT_ABORT >-------------*/,{
3380
        /*
3381
        **      After ABORT message,
3382
        **
3383
        **      expect an immediate disconnect, ...
3384
        */
3385
        SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3386
                0,
3387
        SCR_CLR (SCR_ACK|SCR_ATN),
3388
                0,
3389
        SCR_WAIT_DISC,
3390
                0,
3391
        /*
3392
        **      ... and set the status to "ABORTED"
3393
        */
3394
        SCR_LOAD_REG (HS_REG, HS_ABORTED),
3395
                0,
3396
        SCR_JUMP,
3397
                PADDR (cleanup),
3398
 
3399
}/*-------------------------< HDATA_IN >-------------------*/,{
3400
/*
3401
**      Because the size depends on the
3402
**      #define MAX_SCATTERH parameter,
3403
**      it is filled in at runtime.
3404
**
3405
**  ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3406
**  ||  SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3407
**  ||          PADDR (dispatch),
3408
**  ||  SCR_MOVE_TBL ^ SCR_DATA_IN,
3409
**  ||          offsetof (struct dsb, data[ i]),
3410
**  ##===================================================
3411
**
3412
**---------------------------------------------------------
3413
*/
3414
 
3415
}/*-------------------------< HDATA_IN2 >------------------*/,{
3416
        SCR_JUMP,
3417
                PADDR (data_in),
3418
 
3419
}/*-------------------------< HDATA_OUT >-------------------*/,{
3420
/*
3421
**      Because the size depends on the
3422
**      #define MAX_SCATTERH parameter,
3423
**      it is filled in at runtime.
3424
**
3425
**  ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
3426
**  ||  SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3427
**  ||          PADDR (dispatch),
3428
**  ||  SCR_MOVE_TBL ^ SCR_DATA_OUT,
3429
**  ||          offsetof (struct dsb, data[ i]),
3430
**  ##===================================================
3431
**
3432
**---------------------------------------------------------
3433
*/
3434
 
3435
}/*-------------------------< HDATA_OUT2 >------------------*/,{
3436
        SCR_JUMP,
3437
                PADDR (data_out),
3438
 
3439
}/*-------------------------< RESET >----------------------*/,{
3440
        /*
3441
        **      Send a M_RESET message if bad IDENTIFY
3442
        **      received on reselection.
3443
        */
3444
        SCR_LOAD_REG (scratcha, M_ABORT_TAG),
3445
                0,
3446
        SCR_JUMP,
3447
                PADDRH (abort_resel),
3448
}/*-------------------------< ABORTTAG >-------------------*/,{
3449
        /*
3450
        **      Abort a wrong tag received on reselection.
3451
        */
3452
        SCR_LOAD_REG (scratcha, M_ABORT_TAG),
3453
                0,
3454
        SCR_JUMP,
3455
                PADDRH (abort_resel),
3456
}/*-------------------------< ABORT >----------------------*/,{
3457
        /*
3458
        **      Abort a reselection when no active CCB.
3459
        */
3460
        SCR_LOAD_REG (scratcha, M_ABORT),
3461
                0,
3462
}/*-------------------------< ABORT_RESEL >----------------*/,{
3463
        SCR_COPY (1),
3464
                RADDR (scratcha),
3465
                NADDR (msgout),
3466
        SCR_SET (SCR_ATN),
3467
                0,
3468
        SCR_CLR (SCR_ACK),
3469
                0,
3470
        /*
3471
        **      and send it.
3472
        **      we expect an immediate disconnect
3473
        */
3474
        SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3475
                0,
3476
        SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
3477
                NADDR (msgout),
3478
        SCR_COPY (1),
3479
                NADDR (msgout),
3480
                NADDR (lastmsg),
3481
        SCR_CLR (SCR_ACK|SCR_ATN),
3482
                0,
3483
        SCR_WAIT_DISC,
3484
                0,
3485
        SCR_JUMP,
3486
                PADDR (start),
3487
}/*-------------------------< RESEND_IDENT >-------------------*/,{
3488
        /*
3489
        **      The target stays in MSG OUT phase after having acked
3490
        **      Identify [+ Tag [+ Extended message ]]. Targets shall
3491
        **      behave this way on parity error.
3492
        **      We must send it again all the messages.
3493
        */
3494
        SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the  */
3495
                0,         /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
3496
        SCR_JUMP,
3497
                PADDR (send_ident),
3498
}/*-------------------------< CLRATN_GO_ON >-------------------*/,{
3499
        SCR_CLR (SCR_ATN),
3500
                0,
3501
        SCR_JUMP,
3502
}/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
3503
                0,
3504
}/*-------------------------< SDATA_IN >-------------------*/,{
3505
        SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
3506
                PADDR (dispatch),
3507
        SCR_MOVE_TBL ^ SCR_DATA_IN,
3508
                offsetof (struct dsb, sense),
3509
        SCR_CALL,
3510
                PADDR (dispatch),
3511
        SCR_JUMP,
3512
                PADDR (no_data),
3513
}/*-------------------------< DATA_IO >--------------------*/,{
3514
        /*
3515
        **      We jump here if the data direction was unknown at the
3516
        **      time we had to queue the command to the scripts processor.
3517
        **      Pointers had been set as follow in this situation:
3518
        **        savep   -->   DATA_IO
3519
        **        lastp   -->   start pointer when DATA_IN
3520
        **        goalp   -->   goal  pointer when DATA_IN
3521
        **        wlastp  -->   start pointer when DATA_OUT
3522
        **        wgoalp  -->   goal  pointer when DATA_OUT
3523
        **      This script sets savep/lastp/goalp according to the
3524
        **      direction chosen by the target.
3525
        */
3526
        SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
3527
                32,
3528
        /*
3529
        **      Direction is DATA IN.
3530
        **      Warning: we jump here, even when phase is DATA OUT.
3531
        */
3532
        SCR_COPY (4),
3533
                NADDR (header.lastp),
3534
                NADDR (header.savep),
3535
 
3536
        /*
3537
        **      Jump to the SCRIPTS according to actual direction.
3538
        */
3539
        SCR_COPY (4),
3540
                NADDR (header.savep),
3541
                RADDR (temp),
3542
        SCR_RETURN,
3543
                0,
3544
        /*
3545
        **      Direction is DATA OUT.
3546
        */
3547
        SCR_COPY (4),
3548
                NADDR (header.wlastp),
3549
                NADDR (header.lastp),
3550
        SCR_COPY (4),
3551
                NADDR (header.wgoalp),
3552
                NADDR (header.goalp),
3553
        SCR_JUMPR,
3554
                -64,
3555
}/*-------------------------< BAD_IDENTIFY >---------------*/,{
3556
        /*
3557
        **      If message phase but not an IDENTIFY,
3558
        **      get some help from the C code.
3559
        **      Old SCSI device may behave so.
3560
        */
3561
        SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
3562
                16,
3563
        SCR_INT,
3564
                SIR_RESEL_NO_IDENTIFY,
3565
        SCR_JUMP,
3566
                PADDRH (reset),
3567
        /*
3568
        **      Message is an IDENTIFY, but lun is unknown.
3569
        **      Read the message, since we got it directly
3570
        **      from the SCSI BUS data lines.
3571
        **      Signal problem to C code for logging the event.
3572
        **      Send a M_ABORT to clear all pending tasks.
3573
        */
3574
        SCR_INT,
3575
                SIR_RESEL_BAD_LUN,
3576
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3577
                NADDR (msgin),
3578
        SCR_JUMP,
3579
                PADDRH (abort),
3580
}/*-------------------------< BAD_I_T_L >------------------*/,{
3581
        /*
3582
        **      We donnot have a task for that I_T_L.
3583
        **      Signal problem to C code for logging the event.
3584
        **      Send a M_ABORT message.
3585
        */
3586
        SCR_INT,
3587
                SIR_RESEL_BAD_I_T_L,
3588
        SCR_JUMP,
3589
                PADDRH (abort),
3590
}/*-------------------------< BAD_I_T_L_Q >----------------*/,{
3591
        /*
3592
        **      We donnot have a task that matches the tag.
3593
        **      Signal problem to C code for logging the event.
3594
        **      Send a M_ABORTTAG message.
3595
        */
3596
        SCR_INT,
3597
                SIR_RESEL_BAD_I_T_L_Q,
3598
        SCR_JUMP,
3599
                PADDRH (aborttag),
3600
}/*-------------------------< BAD_TARGET >-----------------*/,{
3601
        /*
3602
        **      We donnot know the target that reselected us.
3603
        **      Grab the first message if any (IDENTIFY).
3604
        **      Signal problem to C code for logging the event.
3605
        **      M_RESET message.
3606
        */
3607
        SCR_INT,
3608
                SIR_RESEL_BAD_TARGET,
3609
        SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
3610
                8,
3611
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3612
                NADDR (msgin),
3613
        SCR_JUMP,
3614
                PADDRH (reset),
3615
}/*-------------------------< BAD_STATUS >-----------------*/,{
3616
        /*
3617
        **      If command resulted in either QUEUE FULL,
3618
        **      CHECK CONDITION or COMMAND TERMINATED,
3619
        **      call the C code.
3620
        */
3621
        SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
3622
                SIR_BAD_STATUS,
3623
        SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
3624
                SIR_BAD_STATUS,
3625
        SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
3626
                SIR_BAD_STATUS,
3627
        SCR_RETURN,
3628
                0,
3629
}/*-------------------------< START_RAM >-------------------*/,{
3630
        /*
3631
        **      Load the script into on-chip RAM,
3632
        **      and jump to start point.
3633
        */
3634
        SCR_COPY_F (4),
3635
                RADDR (scratcha),
3636
                PADDRH (start_ram0),
3637
        SCR_COPY (sizeof (struct script)),
3638
}/*-------------------------< START_RAM0 >--------------------*/,{
3639
                0,
3640
                PADDR (start),
3641
        SCR_JUMP,
3642
                PADDR (start),
3643
}/*-------------------------< STO_RESTART >-------------------*/,{
3644
        /*
3645
        **
3646
        **      Repair start queue (e.g. next time use the next slot)
3647
        **      and jump to start point.
3648
        */
3649
        SCR_COPY (4),
3650
                RADDR (temp),
3651
                PADDR (startpos),
3652
        SCR_JUMP,
3653
                PADDR (start),
3654
}/*-------------------------< SNOOPTEST >-------------------*/,{
3655
        /*
3656
        **      Read the variable.
3657
        */
3658
        SCR_COPY (4),
3659
                NADDR(ncr_cache),
3660
                RADDR (scratcha),
3661
        /*
3662
        **      Write the variable.
3663
        */
3664
        SCR_COPY (4),
3665
                RADDR (temp),
3666
                NADDR(ncr_cache),
3667
        /*
3668
        **      Read back the variable.
3669
        */
3670
        SCR_COPY (4),
3671
                NADDR(ncr_cache),
3672
                RADDR (temp),
3673
}/*-------------------------< SNOOPEND >-------------------*/,{
3674
        /*
3675
        **      And stop.
3676
        */
3677
        SCR_INT,
3678
                99,
3679
}/*--------------------------------------------------------*/
3680
};
3681
 
3682
/*==========================================================
3683
**
3684
**
3685
**      Fill in #define dependent parts of the script
3686
**
3687
**
3688
**==========================================================
3689
*/
3690
 
3691
__initfunc(
3692
void ncr_script_fill (struct script * scr, struct scripth * scrh)
3693
)
3694
{
3695
        int     i;
3696
        ncrcmd  *p;
3697
 
3698
        p = scrh->tryloop;
3699
        for (i=0; i<MAX_START; i++) {
3700
                *p++ =SCR_CALL;
3701
                *p++ =PADDR (idle);
3702
        };
3703
 
3704
        assert ((u_long)p == (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
3705
 
3706
#ifdef SCSI_NCR_CCB_DONE_SUPPORT
3707
 
3708
        p = scrh->done_queue;
3709
        for (i = 0; i<MAX_DONE; i++) {
3710
                *p++ =SCR_COPY (sizeof(ccb_p));
3711
                *p++ =NADDR (header.cp);
3712
                *p++ =NADDR (ccb_done[i]);
3713
                *p++ =SCR_CALL;
3714
                *p++ =PADDR (done_end);
3715
        }
3716
 
3717
        assert ((u_long)p ==(u_long)&scrh->done_queue+sizeof(scrh->done_queue));
3718
 
3719
#endif /* SCSI_NCR_CCB_DONE_SUPPORT */
3720
 
3721
        p = scrh->hdata_in;
3722
        for (i=0; i<MAX_SCATTERH; i++) {
3723
                *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3724
                *p++ =PADDR (dispatch);
3725
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3726
                *p++ =offsetof (struct dsb, data[i]);
3727
        };
3728
        assert ((u_long)p == (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
3729
 
3730
        p = scr->data_in;
3731
        for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3732
                *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
3733
                *p++ =PADDR (dispatch);
3734
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
3735
                *p++ =offsetof (struct dsb, data[i]);
3736
        };
3737
        assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
3738
 
3739
        p = scrh->hdata_out;
3740
        for (i=0; i<MAX_SCATTERH; i++) {
3741
                *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3742
                *p++ =PADDR (dispatch);
3743
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3744
                *p++ =offsetof (struct dsb, data[i]);
3745
        };
3746
        assert ((u_long)p==(u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
3747
 
3748
        p = scr->data_out;
3749
        for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
3750
                *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
3751
                *p++ =PADDR (dispatch);
3752
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
3753
                *p++ =offsetof (struct dsb, data[i]);
3754
        };
3755
 
3756
        assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
3757
}
3758
 
3759
/*==========================================================
3760
**
3761
**
3762
**      Copy and rebind a script.
3763
**
3764
**
3765
**==========================================================
3766
*/
3767
 
3768
__initfunc(
3769
static void ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
3770
)
3771
{
3772
        ncrcmd  opcode, new, old, tmp1, tmp2;
3773
        ncrcmd  *start, *end;
3774
        int relocs;
3775
        int opchanged = 0;
3776
 
3777
        start = src;
3778
        end = src + len/4;
3779
 
3780
        while (src < end) {
3781
 
3782
                opcode = *src++;
3783
                *dst++ = cpu_to_scr(opcode);
3784
 
3785
                /*
3786
                **      If we forget to change the length
3787
                **      in struct script, a field will be
3788
                **      padded with 0. This is an illegal
3789
                **      command.
3790
                */
3791
 
3792
                if (opcode == 0) {
3793
                        printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
3794
                                ncr_name(np), (int) (src-start-1));
3795
                        MDELAY (1000);
3796
                };
3797
 
3798
                if (DEBUG_FLAGS & DEBUG_SCRIPT)
3799
                        printk (KERN_DEBUG "%p:  <%x>\n",
3800
                                (src-1), (unsigned)opcode);
3801
 
3802
                /*
3803
                **      We don't have to decode ALL commands
3804
                */
3805
                switch (opcode >> 28) {
3806
 
3807
                case 0xc:
3808
                        /*
3809
                        **      COPY has TWO arguments.
3810
                        */
3811
                        relocs = 2;
3812
                        tmp1 = src[0];
3813
                        if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
3814
                                tmp1 = 0;
3815
                        tmp2 = src[1];
3816
                        if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
3817
                                tmp2 = 0;
3818
                        if ((tmp1 ^ tmp2) & 3) {
3819
                                printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
3820
                                        ncr_name(np), (int) (src-start-1));
3821
                                MDELAY (1000);
3822
                        }
3823
                        /*
3824
                        **      If PREFETCH feature not enabled, remove
3825
                        **      the NO FLUSH bit if present.
3826
                        */
3827
                        if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
3828
                                dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
3829
                                ++opchanged;
3830
                        }
3831
                        break;
3832
 
3833
                case 0x0:
3834
                        /*
3835
                        **      MOVE (absolute address)
3836
                        */
3837
                        relocs = 1;
3838
                        break;
3839
 
3840
                case 0x8:
3841
                        /*
3842
                        **      JUMP / CALL
3843
                        **      dont't relocate if relative :-)
3844
                        */
3845
                        if (opcode & 0x00800000)
3846
                                relocs = 0;
3847
                        else
3848
                                relocs = 1;
3849
                        break;
3850
 
3851
                case 0x4:
3852
                case 0x5:
3853
                case 0x6:
3854
                case 0x7:
3855
                        relocs = 1;
3856
                        break;
3857
 
3858
                default:
3859
                        relocs = 0;
3860
                        break;
3861
                };
3862
 
3863
                if (relocs) {
3864
                        while (relocs--) {
3865
                                old = *src++;
3866
 
3867
                                switch (old & RELOC_MASK) {
3868
                                case RELOC_REGISTER:
3869
                                        new = (old & ~RELOC_MASK)
3870
                                                + bus_dvma_to_mem(np->paddr);
3871
                                        break;
3872
                                case RELOC_LABEL:
3873
                                        new = (old & ~RELOC_MASK) + np->p_script;
3874
                                        break;
3875
                                case RELOC_LABELH:
3876
                                        new = (old & ~RELOC_MASK) + np->p_scripth;
3877
                                        break;
3878
                                case RELOC_SOFTC:
3879
                                        new = (old & ~RELOC_MASK) + vtophys(np);
3880
                                        break;
3881
                                case RELOC_KVAR:
3882
                                        if (((old & ~RELOC_MASK) <
3883
                                             SCRIPT_KVAR_FIRST) ||
3884
                                            ((old & ~RELOC_MASK) >
3885
                                             SCRIPT_KVAR_LAST))
3886
                                                panic("ncr KVAR out of range");
3887
                                        new = vtophys(script_kvars[old &
3888
                                            ~RELOC_MASK]);
3889
                                        break;
3890
                                case 0:
3891
                                        /* Don't relocate a 0 address. */
3892
                                        if (old == 0) {
3893
                                                new = old;
3894
                                                break;
3895
                                        }
3896
                                        /* fall through */
3897
                                default:
3898
                                        panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
3899
                                        break;
3900
                                }
3901
 
3902
                                *dst++ = cpu_to_scr(new);
3903
                        }
3904
                } else
3905
                        *dst++ = cpu_to_scr(*src++);
3906
 
3907
        };
3908
}
3909
 
3910
/*==========================================================
3911
**
3912
**
3913
**      Auto configuration:  attach and init a host adapter.
3914
**
3915
**
3916
**==========================================================
3917
*/
3918
 
3919
/*
3920
**      Linux host data structure
3921
**
3922
**      The script area is allocated in the host data structure
3923
**      because kmalloc() returns NULL during scsi initialisations
3924
**      with Linux 1.2.X
3925
*/
3926
 
3927
struct host_data {
3928
     struct ncb *ncb;
3929
 
3930
     char ncb_align[CACHE_LINE_SIZE-1]; /* Filler for alignment */
3931
     struct ncb _ncb_data;
3932
 
3933
     char ccb_align[CACHE_LINE_SIZE-1]; /* Filler for alignment */
3934
     struct ccb _ccb_data;
3935
 
3936
     char scr_align[CACHE_LINE_SIZE-1]; /* Filler for alignment */
3937
     struct script script_data;
3938
 
3939
     struct scripth scripth_data;
3940
};
3941
 
3942
/*
3943
**      Print something which allows to retrieve the controler type, unit,
3944
**      target, lun concerned by a kernel message.
3945
*/
3946
 
3947
static void PRINT_TARGET(ncb_p np, int target)
3948
{
3949
        printk(KERN_INFO "%s-<%d,*>: ", ncr_name(np), target);
3950
}
3951
 
3952
static void PRINT_LUN(ncb_p np, int target, int lun)
3953
{
3954
        printk(KERN_INFO "%s-<%d,%d>: ", ncr_name(np), target, lun);
3955
}
3956
 
3957
static void PRINT_ADDR(Scsi_Cmnd *cmd)
3958
{
3959
        struct host_data *host_data = (struct host_data *) cmd->host->hostdata;
3960
        PRINT_LUN(host_data->ncb, cmd->target, cmd->lun);
3961
}
3962
 
3963
/*==========================================================
3964
**
3965
**      NCR chip clock divisor table.
3966
**      Divisors are multiplied by 10,000,000 in order to make
3967
**      calculations more simple.
3968
**
3969
**==========================================================
3970
*/
3971
 
3972
#define _5M 5000000
3973
static u_long div_10M[] =
3974
        {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3975
 
3976
 
3977
/*===============================================================
3978
**
3979
**      Prepare io register values used by ncr_init() according
3980
**      to selected and supported features.
3981
**
3982
**      NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
3983
**      transfers. 32,64,128 are only supported by 875 and 895 chips.
3984
**      We use log base 2 (burst length) as internal code, with
3985
**      value 0 meaning "burst disabled".
3986
**
3987
**===============================================================
3988
*/
3989
 
3990
/*
3991
 *      Burst length from burst code.
3992
 */
3993
#define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3994
 
3995
/*
3996
 *      Burst code from io register bits.
3997
 */
3998
#define burst_code(dmode, ctest4, ctest5) \
3999
        (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
4000
 
4001
/*
4002
 *      Set initial io register bits from burst code.
4003
 */
4004
static inline void ncr_init_burst(ncb_p np, u_char bc)
4005
{
4006
        np->rv_ctest4   &= ~0x80;
4007
        np->rv_dmode    &= ~(0x3 << 6);
4008
        np->rv_ctest5   &= ~0x4;
4009
 
4010
        if (!bc) {
4011
                np->rv_ctest4   |= 0x80;
4012
        }
4013
        else {
4014
                --bc;
4015
                np->rv_dmode    |= ((bc & 0x3) << 6);
4016
                np->rv_ctest5   |= (bc & 0x4);
4017
        }
4018
}
4019
 
4020
#ifdef SCSI_NCR_NVRAM_SUPPORT
4021
 
4022
/*
4023
**      Get target set-up from Symbios format NVRAM.
4024
*/
4025
 
4026
__initfunc(
4027
static void
4028
        ncr_Symbios_setup_target(ncb_p np, int target, Symbios_nvram *nvram)
4029
)
4030
{
4031
        tcb_p tp = &np->target[target];
4032
        Symbios_target *tn = &nvram->target[target];
4033
 
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;
4038
 
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;
4043
}
4044
 
4045
/*
4046
**      Get target set-up from Tekram format NVRAM.
4047
*/
4048
 
4049
__initfunc(
4050
static void
4051
        ncr_Tekram_setup_target(ncb_p np, int target, Tekram_nvram *nvram)
4052
)
4053
{
4054
        tcb_p tp = &np->target[target];
4055
        struct Tekram_target *tn = &nvram->target[target];
4056
        int i;
4057
 
4058
        if (tn->flags & TEKRAM_SYNC_NEGO) {
4059
                i = tn->sync_index & 0xf;
4060
                tp->usrsync = i < 12 ? Tekram_sync[i] : 255;
4061
        }
4062
 
4063
        tp->usrwide = (tn->flags & TEKRAM_WIDE_NEGO) ? 1 : 0;
4064
 
4065
        if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
4066
                tp->usrtags = 2 << nvram->max_tags_index;
4067
        }
4068
 
4069
        if (!(tn->flags & TEKRAM_DISCONNECT_ENABLE))
4070
                tp->usrflag = UF_NODISC;
4071
 
4072
        /* If any device does not support parity, we will not use this option */
4073
        if (!(tn->flags & TEKRAM_PARITY_CHECK))
4074
                np->rv_scntl0  &= ~0x0a; /* SCSI parity checking disabled */
4075
}
4076
#endif /* SCSI_NCR_NVRAM_SUPPORT */
4077
 
4078
__initfunc(
4079
static int ncr_prepare_setting(ncb_p np, ncr_nvram *nvram)
4080
)
4081
{
4082
        u_char  burst_max;
4083
        u_long  period;
4084
        int i;
4085
 
4086
        /*
4087
        **      Save assumed BIOS setting
4088
        */
4089
 
4090
        np->sv_scntl0   = INB(nc_scntl0) & 0x0a;
4091
        np->sv_scntl3   = INB(nc_scntl3) & 0x07;
4092
        np->sv_dmode    = INB(nc_dmode)  & 0xce;
4093
        np->sv_dcntl    = INB(nc_dcntl)  & 0xa8;
4094
        np->sv_ctest3   = INB(nc_ctest3) & 0x01;
4095
        np->sv_ctest4   = INB(nc_ctest4) & 0x80;
4096
        np->sv_ctest5   = INB(nc_ctest5) & 0x24;
4097
        np->sv_gpcntl   = INB(nc_gpcntl);
4098
        np->sv_stest2   = INB(nc_stest2) & 0x20;
4099
        np->sv_stest4   = INB(nc_stest4);
4100
 
4101
        /*
4102
        **      Wide ?
4103
        */
4104
 
4105
        np->maxwide     = (np->features & FE_WIDE)? 1 : 0;
4106
 
4107
        /*
4108
        **      Get the frequency of the chip's clock.
4109
        **      Find the right value for scntl3.
4110
        */
4111
 
4112
        if      (np->features & FE_QUAD)
4113
                np->multiplier  = 4;
4114
        else if (np->features & FE_DBLR)
4115
                np->multiplier  = 2;
4116
        else
4117
                np->multiplier  = 1;
4118
 
4119
        np->clock_khz   = (np->features & FE_CLK80)? 80000 : 40000;
4120
        np->clock_khz   *= np->multiplier;
4121
 
4122
        if (np->clock_khz != 40000)
4123
                ncr_getclock(np, np->multiplier);
4124
 
4125
        /*
4126
         * Divisor to be used for async (timer pre-scaler).
4127
         */
4128
        i = np->clock_divn - 1;
4129
        while (--i >= 0) {
4130
                if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
4131
                        ++i;
4132
                        break;
4133
                }
4134
        }
4135
        np->rv_scntl3 = i+1;
4136
 
4137
        /*
4138
         * Minimum synchronous period factor supported by the chip.
4139
         * Btw, 'period' is in tenths of nanoseconds.
4140
         */
4141
 
4142
        period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
4143
        if      (period <= 250)         np->minsync = 10;
4144
        else if (period <= 303)         np->minsync = 11;
4145
        else if (period <= 500)         np->minsync = 12;
4146
        else                            np->minsync = (period + 40 - 1) / 40;
4147
 
4148
        /*
4149
         * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
4150
         */
4151
 
4152
        if      (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
4153
                np->minsync = 25;
4154
        else if (np->minsync < 12 && !(np->features & FE_ULTRA2))
4155
                np->minsync = 12;
4156
 
4157
        /*
4158
         * Maximum synchronous period factor supported by the chip.
4159
         */
4160
 
4161
        period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
4162
        np->maxsync = period > 2540 ? 254 : period / 10;
4163
 
4164
        /*
4165
        **      Prepare initial value of other IO registers
4166
        */
4167
#if defined SCSI_NCR_TRUST_BIOS_SETTING
4168
        np->rv_scntl0   = np->sv_scntl0;
4169
        np->rv_dmode    = np->sv_dmode;
4170
        np->rv_dcntl    = np->sv_dcntl;
4171
        np->rv_ctest3   = np->sv_ctest3;
4172
        np->rv_ctest4   = np->sv_ctest4;
4173
        np->rv_ctest5   = np->sv_ctest5;
4174
        burst_max       = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
4175
#else
4176
 
4177
        /*
4178
        **      Select burst length (dwords)
4179
        */
4180
        burst_max       = driver_setup.burst_max;
4181
        if (burst_max == 255)
4182
                burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
4183
        if (burst_max > 7)
4184
                burst_max = 7;
4185
        if (burst_max > np->maxburst)
4186
                burst_max = np->maxburst;
4187
 
4188
        /*
4189
        **      Select all supported special features
4190
        */
4191
        if (np->features & FE_ERL)
4192
                np->rv_dmode    |= ERL;         /* Enable Read Line */
4193
        if (np->features & FE_BOF)
4194
                np->rv_dmode    |= BOF;         /* Burst Opcode Fetch */
4195
        if (np->features & FE_ERMP)
4196
                np->rv_dmode    |= ERMP;        /* Enable Read Multiple */
4197
        if (np->features & FE_PFEN)
4198
                np->rv_dcntl    |= PFEN;        /* Prefetch Enable */
4199
        if (np->features & FE_CLSE)
4200
                np->rv_dcntl    |= CLSE;        /* Cache Line Size Enable */
4201
        if (np->features & FE_WRIE)
4202
                np->rv_ctest3   |= WRIE;        /* Write and Invalidate */
4203
        if (np->features & FE_DFS)
4204
                np->rv_ctest5   |= DFS;         /* Dma Fifo Size */
4205
 
4206
        /*
4207
        **      Select some other
4208
        */
4209
        if (driver_setup.master_parity)
4210
                np->rv_ctest4   |= MPEE;        /* Master parity checking */
4211
        if (driver_setup.scsi_parity)
4212
                np->rv_scntl0   |= 0x0a;        /*  full arb., ena parity, par->ATN  */
4213
 
4214
#ifdef SCSI_NCR_NVRAM_SUPPORT
4215
        /*
4216
        **      Get parity checking, host ID and verbose mode from NVRAM
4217
        **/
4218
        if (nvram) {
4219
                switch(nvram->type) {
4220
                case SCSI_NCR_TEKRAM_NVRAM:
4221
                        np->myaddr = nvram->data.Tekram.host_id & 0x0f;
4222
                        break;
4223
                case SCSI_NCR_SYMBIOS_NVRAM:
4224
                        if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
4225
                                np->rv_scntl0  &= ~0x0a;
4226
                        np->myaddr = nvram->data.Symbios.host_id & 0x0f;
4227
                        if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
4228
                                np->verbose += 1;
4229
                        break;
4230
                }
4231
        }
4232
#endif
4233
        /*
4234
        **  Get SCSI addr of host adapter (set by bios?).
4235
        */
4236
        if (!np->myaddr) np->myaddr = INB(nc_scid) & 0x07;
4237
        if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
4238
 
4239
 
4240
#endif /* SCSI_NCR_TRUST_BIOS_SETTING */
4241
 
4242
        /*
4243
         *      Prepare initial io register bits for burst length
4244
         */
4245
        ncr_init_burst(np, burst_max);
4246
 
4247
        /*
4248
        **      Set differential mode and LED support.
4249
        **      Ignore these features for boards known to use a
4250
        **      specific GPIO wiring (Tekram only for now).
4251
        **      Probe initial setting of GPREG and GPCNTL for
4252
        **      other ones.
4253
        */
4254
        if (!nvram || nvram->type != SCSI_NCR_TEKRAM_NVRAM) {
4255
                switch(driver_setup.diff_support) {
4256
                case 3:
4257
                        if (INB(nc_gpreg) & 0x08)
4258
                        break;
4259
                case 2:
4260
                        np->rv_stest2   |= 0x20;
4261
                        break;
4262
                case 1:
4263
                        np->rv_stest2   |= (np->sv_stest2 & 0x20);
4264
                        break;
4265
                default:
4266
                        break;
4267
                }
4268
        }
4269
        if ((driver_setup.led_pin ||
4270
             (nvram && nvram->type == SCSI_NCR_SYMBIOS_NVRAM)) &&
4271
            !(np->sv_gpcntl & 0x01))
4272
                np->features |= FE_LED0;
4273
 
4274
        /*
4275
        **      Set irq mode.
4276
        */
4277
        switch(driver_setup.irqm) {
4278
        case 2:
4279
                np->rv_dcntl    |= IRQM;
4280
                break;
4281
        case 1:
4282
                np->rv_dcntl    |= (np->sv_dcntl & IRQM);
4283
                break;
4284
        default:
4285
                break;
4286
        }
4287
 
4288
        /*
4289
        **      Configure targets according to driver setup.
4290
        **      If NVRAM present get targets setup from NVRAM.
4291
        **      Allow to override sync, wide and NOSCAN from
4292
        **      boot command line.
4293
        */
4294
        for (i = 0 ; i < MAX_TARGET ; i++) {
4295
                tcb_p tp = &np->target[i];
4296
 
4297
                tp->usrsync = 255;
4298
#ifdef SCSI_NCR_NVRAM_SUPPORT
4299
                if (nvram) {
4300
                        switch(nvram->type) {
4301
                        case SCSI_NCR_TEKRAM_NVRAM:
4302
                                ncr_Tekram_setup_target(np, i, &nvram->data.Tekram);
4303
                                break;
4304
                        case SCSI_NCR_SYMBIOS_NVRAM:
4305
                                ncr_Symbios_setup_target(np, i, &nvram->data.Symbios);
4306
                                break;
4307
                        }
4308
                        if (driver_setup.use_nvram & 0x2)
4309
                                tp->usrsync = driver_setup.default_sync;
4310
                        if (driver_setup.use_nvram & 0x4)
4311
                                tp->usrwide = driver_setup.max_wide;
4312
                        if (driver_setup.use_nvram & 0x8)
4313
                                tp->usrflag &= ~UF_NOSCAN;
4314
                }
4315
                else {
4316
#else
4317
                if (1) {
4318
#endif
4319
                        tp->usrsync = driver_setup.default_sync;
4320
                        tp->usrwide = driver_setup.max_wide;
4321
                        tp->usrtags = SCSI_NCR_MAX_TAGS;
4322
                        if (!driver_setup.disconnection)
4323
                                np->target[i].usrflag = UF_NODISC;
4324
                }
4325
        }
4326
 
4327
        /*
4328
        **      Announce all that stuff to user.
4329
        */
4330
 
4331
        i = nvram ? nvram->type : 0;
4332
        printk(KERN_INFO "%s: %sID %d, Fast-%d%s%s\n", ncr_name(np),
4333
                i  == SCSI_NCR_SYMBIOS_NVRAM ? "Symbios format NVRAM, " :
4334
                (i == SCSI_NCR_TEKRAM_NVRAM  ? "Tekram format NVRAM, " : ""),
4335
                np->myaddr,
4336
                np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
4337
                (np->rv_scntl0 & 0xa)   ? ", Parity Checking"   : ", NO Parity",
4338
                (np->rv_stest2 & 0x20)  ? ", Differential"      : "");
4339
 
4340
        if (bootverbose > 1) {
4341
                printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4342
                        "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4343
                        ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
4344
                        np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
4345
 
4346
                printk (KERN_INFO "%s: final   SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
4347
                        "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
4348
                        ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
4349
                        np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
4350
        }
4351
 
4352
        if (bootverbose && np->paddr2)
4353
                printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
4354
                        ncr_name(np), np->paddr2);
4355
 
4356
        return 0;
4357
}
4358
 
4359
 
4360
#ifdef SCSI_NCR_DEBUG_NVRAM
4361
 
4362
__initfunc(
4363
void ncr_display_Symbios_nvram(ncb_p np, Symbios_nvram *nvram)
4364
)
4365
{
4366
        int i;
4367
 
4368
        /* display Symbios nvram host data */
4369
        printk(KERN_DEBUG "%s: HOST ID=%d%s%s%s%s%s\n",
4370
                ncr_name(np), nvram->host_id & 0x0f,
4371
                (nvram->flags  & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
4372
                (nvram->flags  & SYMBIOS_PARITY_ENABLE) ? " PARITY"     :"",
4373
                (nvram->flags  & SYMBIOS_VERBOSE_MSGS)  ? " VERBOSE"    :"",
4374
                (nvram->flags  & SYMBIOS_CHS_MAPPING)   ? " CHS_ALT"    :"",
4375
                (nvram->flags1 & SYMBIOS_SCAN_HI_LO)    ? " HI_LO"      :"");
4376
 
4377
        /* display Symbios nvram drive data */
4378
        for (i = 0 ; i < 15 ; i++) {
4379
                struct Symbios_target *tn = &nvram->target[i];
4380
                printk(KERN_DEBUG "%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
4381
                ncr_name(np), i,
4382
                (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC"       : "",
4383
                (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT"  : "",
4384
                (tn->flags & SYMBIOS_SCAN_LUNS)         ? " SCAN_LUNS"  : "",
4385
                (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ"        : "",
4386
                tn->bus_width,
4387
                tn->sync_period / 4,
4388
                tn->timeout);
4389
        }
4390
}
4391
 
4392
static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
4393
 
4394
__initfunc(
4395
void ncr_display_Tekram_nvram(ncb_p np, Tekram_nvram *nvram)
4396
)
4397
{
4398
        int i, tags, boot_delay;
4399
        char *rem;
4400
 
4401
        /* display Tekram nvram host data */
4402
        tags = 2 << nvram->max_tags_index;
4403
        boot_delay = 0;
4404
        if (nvram->boot_delay_index < 6)
4405
                boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
4406
        switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
4407
        default:
4408
        case 0:  rem = "";                       break;
4409
        case 1: rem = " REMOVABLE=boot device"; break;
4410
        case 2: rem = " REMOVABLE=all";         break;
4411
        }
4412
 
4413
        printk(KERN_DEBUG
4414
                "%s: HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
4415
                ncr_name(np), nvram->host_id & 0x0f,
4416
                (nvram->flags1 & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
4417
                (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES"        :"",
4418
                (nvram->flags & TEKRAM_DRIVES_SUP_1GB)  ? " >1GB"       :"",
4419
                (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET"    :"",
4420
                (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG"    :"",
4421
                (nvram->flags & TEKRAM_IMMEDIATE_SEEK)  ? " IMM_SEEK"   :"",
4422
                (nvram->flags & TEKRAM_SCAN_LUNS)       ? " SCAN_LUNS"  :"",
4423
                (nvram->flags1 & TEKRAM_F2_F6_ENABLED)  ? " F2_F6"      :"",
4424
                rem, boot_delay, tags);
4425
 
4426
        /* display Tekram nvram drive data */
4427
        for (i = 0; i <= 15; i++) {
4428
                int sync, j;
4429
                struct Tekram_target *tn = &nvram->target[i];
4430
                j = tn->sync_index & 0xf;
4431
                sync = j < 12 ? Tekram_sync[j] : 255;
4432
                printk(KERN_DEBUG "%s-%d:%s%s%s%s%s%s PERIOD=%d\n",
4433
                ncr_name(np), i,
4434
                (tn->flags & TEKRAM_PARITY_CHECK)       ? " PARITY"     : "",
4435
                (tn->flags & TEKRAM_SYNC_NEGO)          ? " SYNC"       : "",
4436
                (tn->flags & TEKRAM_DISCONNECT_ENABLE)  ? " DISC"       : "",
4437
                (tn->flags & TEKRAM_START_CMD)          ? " START"      : "",
4438
                (tn->flags & TEKRAM_TAGGED_COMMANDS)    ? " TCQ"        : "",
4439
                (tn->flags & TEKRAM_WIDE_NEGO)          ? " WIDE"       : "",
4440
                sync);
4441
        }
4442
}
4443
#endif /* SCSI_NCR_DEBUG_NVRAM */
4444
 
4445
/*
4446
**      Host attach and initialisations.
4447
**
4448
**      Allocate host data and ncb structure.
4449
**      Request IO region and remap MMIO region.
4450
**      Do chip initialization.
4451
**      If all is OK, install interrupt handling and
4452
**      start the timer daemon.
4453
*/
4454
 
4455
__initfunc(
4456
static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device)
4457
)
4458
{
4459
        struct host_data *host_data;
4460
        ncb_p np;
4461
        struct Scsi_Host *instance = 0;
4462
        u_long flags = 0;
4463
        ncr_nvram *nvram = device->nvram;
4464
        int i;
4465
 
4466
#ifdef __sparc__
4467
printk(KERN_INFO "ncr53c%s-%d: rev=0x%02x, base=0x%lx, io_port=0x%lx, irq=%s\n",
4468
        device->chip.name, unit, device->chip.revision_id, device->slot.base,
4469
        device->slot.io_port, __irq_itoa(device->slot.irq));
4470
#else
4471
printk(KERN_INFO "ncr53c%s-%d: rev=0x%02x, base=0x%lx, io_port=0x%lx, irq=%d\n",
4472
        device->chip.name, unit, device->chip.revision_id, device->slot.base,
4473
        device->slot.io_port, device->slot.irq);
4474
#endif
4475
 
4476
        /*
4477
        **      Allocate host_data structure
4478
        */
4479
        if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
4480
                goto attach_error;
4481
 
4482
        /*
4483
        **      Initialize structure.
4484
        */
4485
        host_data = (struct host_data *) instance->hostdata;
4486
        bzero (host_data, sizeof(*host_data));
4487
 
4488
        /*
4489
        **      Align np and first ccb to 32 boundary for cache line
4490
        **      bursting when copying the global header.
4491
        */
4492
        np = (ncb_p) (((u_long) &host_data->_ncb_data) & CACHE_LINE_MASK);
4493
        NCR_INIT_LOCK_NCB(np);
4494
        host_data->ncb = np;
4495
        np->ccb = (ccb_p) (((u_long) &host_data->_ccb_data) & CACHE_LINE_MASK);
4496
 
4497
        /*
4498
        **      Store input informations in the host data structure.
4499
        */
4500
        strncpy(np->chip_name, device->chip.name, sizeof(np->chip_name) - 1);
4501
        np->unit        = unit;
4502
        np->verbose     = driver_setup.verbose;
4503
        sprintf(np->inst_name, "ncr53c%s-%d", np->chip_name, np->unit);
4504
        np->device_id   = device->chip.device_id;
4505
        np->revision_id = device->chip.revision_id;
4506
        np->features    = device->chip.features;
4507
        np->clock_divn  = device->chip.nr_divisor;
4508
        np->maxoffs     = device->chip.offset_max;
4509
        np->maxburst    = device->chip.burst_max;
4510
 
4511
        np->script0  = (struct script *)
4512
                (((u_long) &host_data->script_data) & CACHE_LINE_MASK);
4513
        np->scripth0 = &host_data->scripth_data;
4514
 
4515
        /*
4516
        **    Initialize timer structure
4517
        **
4518
        */
4519
        init_timer(&np->timer);
4520
        np->timer.data     = (unsigned long) np;
4521
        np->timer.function = ncr53c8xx_timeout;
4522
 
4523
        /*
4524
        **      Try to map the controller chip to
4525
        **      virtual and physical memory.
4526
        */
4527
 
4528
        np->paddr       = device->slot.base;
4529
        np->paddr2      = (np->features & FE_RAM)? device->slot.base_2 : 0;
4530
 
4531
#ifndef NCR_IOMAPPED
4532
        np->vaddr = remap_pci_mem((u_long) np->paddr, (u_long) 128);
4533
        if (!np->vaddr) {
4534
                printk(KERN_ERR
4535
                        "%s: can't map memory mapped IO region\n",ncr_name(np));
4536
                goto attach_error;
4537
        }
4538
        else
4539
                if (bootverbose > 1)
4540
                        printk(KERN_INFO
4541
                                "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
4542
 
4543
        /*
4544
        **      Make the controller's registers available.
4545
        **      Now the INB INW INL OUTB OUTW OUTL macros
4546
        **      can be used safely.
4547
        */
4548
 
4549
        np->reg = (struct ncr_reg*) np->vaddr;
4550
 
4551
#endif /* !defined NCR_IOMAPPED */
4552
 
4553
        /*
4554
        **      Try to map the controller chip into iospace.
4555
        */
4556
 
4557
        request_region(device->slot.io_port, 128, "ncr53c8xx");
4558
        np->port = device->slot.io_port;
4559
 
4560
#ifdef SCSI_NCR_NVRAM_SUPPORT
4561
        if (nvram) {
4562
                switch(nvram->type) {
4563
                case SCSI_NCR_SYMBIOS_NVRAM:
4564
#ifdef SCSI_NCR_DEBUG_NVRAM
4565
                        ncr_display_Symbios_nvram(np, &nvram->data.Symbios);
4566
#endif
4567
                        break;
4568
                case SCSI_NCR_TEKRAM_NVRAM:
4569
#ifdef SCSI_NCR_DEBUG_NVRAM
4570
                        ncr_display_Tekram_nvram(np, &nvram->data.Tekram);
4571
#endif
4572
                        break;
4573
                default:
4574
                        nvram = 0;
4575
#ifdef SCSI_NCR_DEBUG_NVRAM
4576
                        printk(KERN_DEBUG "%s: NVRAM: None or invalid data.\n", ncr_name(np));
4577
#endif
4578
                }
4579
        }
4580
#endif
4581
 
4582
        /*
4583
        **      Do chip dependent initialization.
4584
        */
4585
        (void)ncr_prepare_setting(np, nvram);
4586
 
4587
        if (np->paddr2 && sizeof(struct script) > 4096) {
4588
                np->paddr2 = 0;
4589
                printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
4590
                        ncr_name(np));
4591
        }
4592
 
4593
        /*
4594
        **      Fill Linux host instance structure
4595
        */
4596
        instance->max_channel   = 0;
4597
        instance->max_id        = np->maxwide ? 16 : 8;
4598
        instance->max_lun       = SCSI_NCR_MAX_LUN;
4599
#ifndef NCR_IOMAPPED
4600
        instance->base          = (char *) np->reg;
4601
#endif
4602
        instance->irq           = device->slot.irq;
4603
        instance->unique_id     = device->slot.io_port;
4604
        instance->io_port       = device->slot.io_port;
4605
        instance->n_io_port     = 128;
4606
        instance->dma_channel   = 0;
4607
        instance->select_queue_depths = ncr53c8xx_select_queue_depths;
4608
 
4609
        /*
4610
        **      Patch script to physical addresses
4611
        */
4612
        ncr_script_fill (&script0, &scripth0);
4613
 
4614
        np->scripth     = np->scripth0;
4615
        np->p_scripth   = vtophys(np->scripth);
4616
 
4617
        np->p_script    = (np->paddr2) ? bus_dvma_to_mem(np->paddr2) : vtophys(np->script0);
4618
 
4619
        ncr_script_copy_and_bind (np, (ncrcmd *) &script0, (ncrcmd *) np->script0, sizeof(struct script));
4620
        ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0, (ncrcmd *) np->scripth0, sizeof(struct scripth));
4621
        np->ccb->p_ccb          = vtophys (np->ccb);
4622
 
4623
        /*
4624
        **    Patch the script for LED support.
4625
        */
4626
 
4627
        if (np->features & FE_LED0) {
4628
                np->script0->idle[0]  =
4629
                                cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR,  0x01));
4630
                np->script0->reselected[0] =
4631
                                cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
4632
                np->script0->start[0] =
4633
                                cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
4634
        }
4635
 
4636
        /*
4637
        **      Look for the target control block of this nexus.
4638
        **      For i = 0 to 3
4639
        **              JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
4640
        */
4641
        for (i = 0 ; i < 4 ; i++) {
4642
                np->jump_tcb[i].l_cmd   =
4643
                                cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
4644
                np->jump_tcb[i].l_paddr =
4645
                                cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
4646
        }
4647
 
4648
        /*
4649
        **      Reset chip.
4650
        */
4651
 
4652
        OUTB (nc_istat,  SRST);
4653
        UDELAY (100);
4654
        OUTB (nc_istat,  0   );
4655
 
4656
        /*
4657
        **      Now check the cache handling of the pci chipset.
4658
        */
4659
 
4660
        if (ncr_snooptest (np)) {
4661
                printk (KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
4662
                goto attach_error;
4663
        };
4664
 
4665
        /*
4666
        **      Install the interrupt handler.
4667
        */
4668
#ifdef  SCSI_NCR_SHARE_IRQ
4669
#define NCR_SA_INTERRUPT_FLAGS (SA_INTERRUPT | SA_SHIRQ)
4670
        if (bootverbose > 1)
4671
#ifdef __sparc__
4672
                printk(KERN_INFO "%s: requesting shared irq %s (dev_id=0x%lx)\n",
4673
                        ncr_name(np), __irq_itoa(device->slot.irq), (u_long) np);
4674
#else
4675
                printk(KERN_INFO "%s: requesting shared irq %d (dev_id=0x%lx)\n",
4676
                        ncr_name(np), device->slot.irq, (u_long) np);
4677
#endif
4678
#else
4679
#define NCR_SA_INTERRUPT_FLAGS SA_INTERRUPT
4680
#endif
4681
        if (request_irq(device->slot.irq, ncr53c8xx_intr,
4682
                        NCR_SA_INTERRUPT_FLAGS, "ncr53c8xx", np)) {
4683
#ifdef __sparc__
4684
                printk(KERN_ERR "%s: request irq %s failure\n",
4685
                        ncr_name(np), __irq_itoa(device->slot.irq));
4686
#else
4687
                printk(KERN_ERR "%s: request irq %d failure\n",
4688
                        ncr_name(np), device->slot.irq);
4689
#endif
4690
                goto attach_error;
4691
        }
4692
        np->irq = device->slot.irq;
4693
 
4694
        /*
4695
        **      Initialize the fixed part of the default ccb.
4696
        */
4697
        ncr_init_ccb(np, np->ccb);
4698
 
4699
        /*
4700
        **      After SCSI devices have been opened, we cannot
4701
        **      reset the bus safely, so we do it here.
4702
        **      Interrupt handler does the real work.
4703
        **      Process the reset exception,
4704
        **      if interrupts are not enabled yet.
4705
        **      Then enable disconnects.
4706
        */
4707
        NCR_LOCK_NCB(np, flags);
4708
        if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
4709
                printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
4710
 
4711
                NCR_UNLOCK_NCB(np, flags);
4712
                goto attach_error;
4713
        }
4714
        ncr_exception (np);
4715
 
4716
        np->disc = 1;
4717
 
4718
        /*
4719
        **      The middle-level SCSI driver does not
4720
        **      wait for devices to settle.
4721
        **      Wait synchronously if more than 2 seconds.
4722
        */
4723
        if (driver_setup.settle_delay > 2) {
4724
                printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
4725
                        ncr_name(np), driver_setup.settle_delay);
4726
                MDELAY (1000 * driver_setup.settle_delay);
4727
        }
4728
 
4729
        /*
4730
        **      Now let the generic SCSI driver
4731
        **      look for the SCSI devices on the bus ..
4732
        */
4733
 
4734
        /*
4735
        **      start the timeout daemon
4736
        */
4737
        np->lasttime=0;
4738
        ncr_timeout (np);
4739
 
4740
        /*
4741
        **  use SIMPLE TAG messages by default
4742
        */
4743
#ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
4744
        np->order = M_SIMPLE_TAG;
4745
#endif
4746
 
4747
        /*
4748
        **  Done.
4749
        */
4750
        if (!the_template) {
4751
                the_template = instance->hostt;
4752
                first_host = instance;
4753
        }
4754
 
4755
        NCR_UNLOCK_NCB(np, flags);
4756
 
4757
        return 0;
4758
 
4759
attach_error:
4760
        if (!instance) return -1;
4761
        printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
4762
#ifndef NCR_IOMAPPED
4763
        if (np->vaddr) {
4764
#ifdef DEBUG_NCR53C8XX
4765
                printk(KERN_DEBUG "%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
4766
#endif
4767
                unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
4768
        }
4769
#endif /* !NCR_IOMAPPED */
4770
        if (np->port) {
4771
#ifdef DEBUG_NCR53C8XX
4772
                printk(KERN_DEBUG "%s: releasing IO region %x[%d]\n", ncr_name(np), np->port, 128);
4773
#endif
4774
                release_region(np->port, 128);
4775
        }
4776
        if (np->irq) {
4777
#ifdef DEBUG_NCR53C8XX
4778
#ifdef __sparc__
4779
        printk(KERN_INFO "%s: freeing irq %s\n", ncr_name(np),
4780
               __irq_itoa(np->irq));
4781
#else
4782
        printk(KERN_INFO "%s: freeing irq %d\n", ncr_name(np), np->irq);
4783
#endif
4784
#endif
4785
                free_irq(np->irq, np);
4786
        }
4787
        scsi_unregister(instance);
4788
 
4789
        return -1;
4790
 }
4791
 
4792
 
4793
/*==========================================================
4794
**
4795
**
4796
**      Done SCSI commands list management.
4797
**
4798
**      We donnot enter the scsi_done() callback immediately
4799
**      after a command has been seen as completed but we
4800
**      insert it into a list which is flushed outside any kind
4801
**      of driver critical section.
4802
**      This allows to do minimal stuff under interrupt and
4803
**      inside critical sections and to also avoid locking up
4804
**      on recursive calls to driver entry points under SMP.
4805
**      In fact, the only kernel point which is entered by the
4806
**      driver with a driver lock set is kmalloc(GFP_ATOMIC)
4807
**      that shall not reenter the driver under any circumstances,
4808
**      AFAIK.
4809
**
4810
**==========================================================
4811
*/
4812
static inline void ncr_queue_done_cmd(ncb_p np, Scsi_Cmnd *cmd)
4813
{
4814
        cmd->host_scribble = (char *) np->done_list;
4815
        np->done_list = cmd;
4816
}
4817
 
4818
static inline void ncr_flush_done_cmds(Scsi_Cmnd *lcmd)
4819
{
4820
        Scsi_Cmnd *cmd;
4821
 
4822
        while (lcmd) {
4823
                cmd = lcmd;
4824
                lcmd = (Scsi_Cmnd *) cmd->host_scribble;
4825
                cmd->scsi_done(cmd);
4826
        }
4827
}
4828
 
4829
 
4830
/*==========================================================
4831
**
4832
**
4833
**      Start execution of a SCSI command.
4834
**      This is called from the generic SCSI driver.
4835
**
4836
**
4837
**==========================================================
4838
*/
4839
int ncr_queue_command (ncb_p np, Scsi_Cmnd *cmd)
4840
{
4841
/*      Scsi_Device        *device    = cmd->device; */
4842
        tcb_p tp                      = &np->target[cmd->target];
4843
        lcb_p lp                      = tp->lp[cmd->lun];
4844
        ccb_p cp;
4845
 
4846
        int     segments;
4847
        u_char  nego, idmsg, *msgptr;
4848
        u_int  msglen;
4849
        int     direction;
4850
        u_int32 lastp, goalp;
4851
 
4852
        /*---------------------------------------------
4853
        **
4854
        **      Some shortcuts ...
4855
        **
4856
        **---------------------------------------------
4857
        */
4858
        if ((cmd->target == np->myaddr    ) ||
4859
                (cmd->target >= MAX_TARGET) ||
4860
                (cmd->lun    >= MAX_LUN   )) {
4861
                return(DID_BAD_TARGET);
4862
        }
4863
 
4864
        /*---------------------------------------------
4865
        **
4866
        **      Complete the 1st TEST UNIT READY command
4867
        **      with error condition if the device is
4868
        **      flagged NOSCAN, in order to speed up
4869
        **      the boot.
4870
        **
4871
        **---------------------------------------------
4872
        */
4873
        if (cmd->cmnd[0] == 0 && (tp->usrflag & UF_NOSCAN)) {
4874
                tp->usrflag &= ~UF_NOSCAN;
4875
                return DID_BAD_TARGET;
4876
        }
4877
 
4878
        if (DEBUG_FLAGS & DEBUG_TINY) {
4879
                PRINT_ADDR(cmd);
4880
                printk ("CMD=%x ", cmd->cmnd[0]);
4881
        }
4882
 
4883
        /*---------------------------------------------------
4884
        **
4885
        **      Assign a ccb / bind cmd.
4886
        **      If resetting, shorten settle_time if necessary
4887
        **      in order to avoid spurious timeouts.
4888
        **      If resetting or no free ccb,
4889
        **      insert cmd into the waiting list.
4890
        **
4891
        **----------------------------------------------------
4892
        */
4893
        if (np->settle_time && cmd->timeout_per_command >= HZ &&
4894
                np->settle_time > jiffies + cmd->timeout_per_command - HZ) {
4895
                np->settle_time = jiffies + cmd->timeout_per_command - HZ;
4896
        }
4897
 
4898
        if (np->settle_time || !(cp=ncr_get_ccb (np, cmd->target, cmd->lun))) {
4899
                insert_into_waiting_list(np, cmd);
4900
                return(DID_OK);
4901
        }
4902
        cp->cmd = cmd;
4903
 
4904
        /*---------------------------------------------------
4905
        **
4906
        **      Enable tagged queue if asked by scsi ioctl
4907
        **
4908
        **----------------------------------------------------
4909
        */
4910
#if 0   /* This stuff was only usefull for linux-1.2.13 */
4911
        if (lp && !lp->numtags && cmd->device && cmd->device->tagged_queue) {
4912
                lp->numtags = tp->usrtags;
4913
                ncr_setup_tags (np, cmd->target, cmd->lun);
4914
        }
4915
#endif
4916
 
4917
        /*---------------------------------------------------
4918
        **
4919
        **      timestamp
4920
        **
4921
        **----------------------------------------------------
4922
        */
4923
#ifdef SCSI_NCR_PROFILE_SUPPORT
4924
        bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
4925
        cp->phys.header.stamp.start = jiffies;
4926
#endif
4927
 
4928
        /*---------------------------------------------------
4929
        **
4930
        **      negotiation required?
4931
        **
4932
        **---------------------------------------------------
4933
        */
4934
 
4935
        nego = 0;
4936
 
4937
        if ((!tp->widedone || !tp->period) && !tp->nego_cp && tp->inq_done && lp) {
4938
 
4939
                /*
4940
                **      negotiate wide transfers ?
4941
                */
4942
 
4943
                if (!tp->widedone) {
4944
                        if (tp->inq_byte7 & INQ7_WIDE16) {
4945
                                nego = NS_WIDE;
4946
                        } else
4947
                                tp->widedone=1;
4948
                };
4949
 
4950
                /*
4951
                **      negotiate synchronous transfers?
4952
                */
4953
 
4954
                if (!nego && !tp->period) {
4955
                        if (tp->inq_byte7 & INQ7_SYNC) {
4956
                                nego = NS_SYNC;
4957
                        } else {
4958
                                tp->period  =0xffff;
4959
                                tp->sval = 0xe0;
4960
                                PRINT_TARGET(np, cmd->target);
4961
                                printk ("SYNC transfers not supported.\n");
4962
                        };
4963
                };
4964
 
4965
                /*
4966
                **      remember nego is pending for the target.
4967
                **      Avoid to start a nego for all queued commands
4968
                **      when tagged command queuing is enabled.
4969
                */
4970
 
4971
                if (nego)
4972
                        tp->nego_cp = cp;
4973
        };
4974
 
4975
        /*----------------------------------------------------
4976
        **
4977
        **      Build the identify / tag / sdtr message
4978
        **
4979
        **----------------------------------------------------
4980
        */
4981
 
4982
        idmsg = M_IDENTIFY | cmd->lun;
4983
 
4984
        if (cp ->tag != NO_TAG ||
4985
                (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
4986
                idmsg |= 0x40;
4987
 
4988
        msgptr = cp->scsi_smsg;
4989
        msglen = 0;
4990
        msgptr[msglen++] = idmsg;
4991
 
4992
        if (cp->tag != NO_TAG) {
4993
                char order = np->order;
4994
 
4995
                /*
4996
                **      Force ordered tag if necessary to avoid timeouts
4997
                **      and to preserve interactivity.
4998
                */
4999
                if (lp && lp->tags_stime + (3*HZ) <= jiffies) {
5000
                        if (lp->tags_smap) {
5001
                                order = M_ORDERED_TAG;
5002
                                if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
5003
                                        PRINT_ADDR(cmd);
5004
                                        printk("ordered tag forced.\n");
5005
                                }
5006
                        }
5007
                        lp->tags_stime = jiffies;
5008
                        lp->tags_smap = lp->tags_umap;
5009
                }
5010
 
5011
                if (order == 0) {
5012
                        /*
5013
                        **      Ordered write ops, unordered read ops.
5014
                        */
5015
                        switch (cmd->cmnd[0]) {
5016
                        case 0x08:  /* READ_SMALL (6) */
5017
                        case 0x28:  /* READ_BIG  (10) */
5018
                        case 0xa8:  /* READ_HUGE (12) */
5019
                                order = M_SIMPLE_TAG;
5020
                                break;
5021
                        default:
5022
                                order = M_ORDERED_TAG;
5023
                        }
5024
                }
5025
                msgptr[msglen++] = order;
5026
                /*
5027
                **      Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
5028
                **      since we may have to deal with devices that have
5029
                **      problems with #TAG 0 or too great #TAG numbers.
5030
                */
5031
                msgptr[msglen++] = (cp->tag << 1) + 1;
5032
        }
5033
 
5034
        switch (nego) {
5035
        case NS_SYNC:
5036
                msgptr[msglen++] = M_EXTENDED;
5037
                msgptr[msglen++] = 3;
5038
                msgptr[msglen++] = M_X_SYNC_REQ;
5039
                msgptr[msglen++] = tp->maxoffs ? tp->minsync : 0;
5040
                msgptr[msglen++] = tp->maxoffs;
5041
                if (DEBUG_FLAGS & DEBUG_NEGO) {
5042
                        PRINT_ADDR(cp->cmd);
5043
                        printk ("sync msgout: ");
5044
                        ncr_show_msg (&cp->scsi_smsg [msglen-5]);
5045
                        printk (".\n");
5046
                };
5047
                break;
5048
        case NS_WIDE:
5049
                msgptr[msglen++] = M_EXTENDED;
5050
                msgptr[msglen++] = 2;
5051
                msgptr[msglen++] = M_X_WIDE_REQ;
5052
                msgptr[msglen++] = tp->usrwide;
5053
                if (DEBUG_FLAGS & DEBUG_NEGO) {
5054
                        PRINT_ADDR(cp->cmd);
5055
                        printk ("wide msgout: ");
5056
                        ncr_show_msg (&cp->scsi_smsg [msglen-4]);
5057
                        printk (".\n");
5058
                };
5059
                break;
5060
        };
5061
 
5062
        /*----------------------------------------------------
5063
        **
5064
        **      Build the data descriptors
5065
        **
5066
        **----------------------------------------------------
5067
        */
5068
 
5069
        segments = ncr_scatter (cp, cp->cmd);
5070
 
5071
        if (segments < 0) {
5072
                ncr_free_ccb(np, cp);
5073
                return(DID_ERROR);
5074
        }
5075
 
5076
        /*----------------------------------------------------
5077
        **
5078
        **      Guess xfer direction.
5079
        **      Spare some CPU by testing here frequently opcode.
5080
        **
5081
        **----------------------------------------------------
5082
        */
5083
        if (!cp->data_len)
5084
                direction = 0;
5085
        else {
5086
                switch((int) cmd->cmnd[0]) {
5087
                case 0x08:  /*  READ(6)                         08 */
5088
                case 0x28:  /*  READ(10)                        28 */
5089
                case 0xA8:  /*  READ(12)                        A8 */
5090
                        direction = XFER_IN;
5091
                        break;
5092
                case 0x0A:  /*  WRITE(6)                        0A */
5093
                case 0x2A:  /*  WRITE(10)                       2A */
5094
                case 0xAA:  /*  WRITE(12)                       AA */
5095
                        direction = XFER_OUT;
5096
                        break;
5097
                default:
5098
                        direction = (XFER_IN|XFER_OUT);
5099
                        break;
5100
                }
5101
        }
5102
 
5103
        /*----------------------------------------------------
5104
        **
5105
        **      Set the SAVED_POINTER.
5106
        **
5107
        **----------------------------------------------------
5108
        */
5109
 
5110
        /*
5111
        **      Default to no data transfer.
5112
        */
5113
        lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
5114
 
5115
        /*
5116
        **      Compute data out pointers, if needed.
5117
        */
5118
        if (direction & XFER_OUT) {
5119
                goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
5120
                if (segments <= MAX_SCATTERL)
5121
                        lastp = goalp - 8 - (segments * 16);
5122
                else {
5123
                        lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
5124
                        lastp -= (segments - MAX_SCATTERL) * 16;
5125
                }
5126
                /*
5127
                **      If actual data direction is unknown, save pointers
5128
                **      in header. The SCRIPTS will swap them to current
5129
                **      if target decision will be data out.
5130
                */
5131
                if (direction & XFER_IN) {
5132
                        cp->phys.header.wgoalp  = cpu_to_scr(goalp);
5133
                        cp->phys.header.wlastp  = cpu_to_scr(lastp);
5134
                }
5135
        }
5136
 
5137
        /*
5138
        **      Compute data in pointers, if needed.
5139
        */
5140
        if (direction & XFER_IN) {
5141
                goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
5142
                if (segments <= MAX_SCATTERL)
5143
                        lastp = goalp - 8 - (segments * 16);
5144
                else {
5145
                        lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
5146
                        lastp -= (segments - MAX_SCATTERL) * 16;
5147
                }
5148
        }
5149
 
5150
        /*
5151
        **      Set all pointers values needed by SCRIPTS.
5152
        **      If direction is unknown, start at data_io.
5153
        */
5154
        cp->phys.header.lastp = cpu_to_scr(lastp);
5155
        cp->phys.header.goalp = cpu_to_scr(goalp);
5156
 
5157
        if ((direction & (XFER_IN|XFER_OUT)) == (XFER_IN|XFER_OUT))
5158
                cp->phys.header.savep =
5159
                        cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
5160
        else
5161
                cp->phys.header.savep= cpu_to_scr(lastp);
5162
 
5163
        /*
5164
        **      Save the initial data pointer in order to be able
5165
        **      to redo the command.
5166
        */
5167
        cp->startp = cp->phys.header.savep;
5168
 
5169
        /*----------------------------------------------------
5170
        **
5171
        **      fill in ccb
5172
        **
5173
        **----------------------------------------------------
5174
        **
5175
        **
5176
        **      physical -> virtual backlink
5177
        **      Generic SCSI command
5178
        */
5179
 
5180
        /*
5181
        **      Startqueue
5182
        */
5183
        cp->start.schedule.l_paddr   = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5184
        cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
5185
        /*
5186
        **      select
5187
        */
5188
        cp->phys.select.sel_id          = cmd->target;
5189
        cp->phys.select.sel_scntl3      = tp->wval;
5190
        cp->phys.select.sel_sxfer       = tp->sval;
5191
        /*
5192
        **      message
5193
        */
5194
        cp->phys.smsg.addr              = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
5195
        cp->phys.smsg.size              = cpu_to_scr(msglen);
5196
 
5197
        /*
5198
        **      command
5199
        */
5200
        cp->phys.cmd.addr               = cpu_to_scr(vtophys (&cmd->cmnd[0]));
5201
        cp->phys.cmd.size               = cpu_to_scr(cmd->cmd_len);
5202
 
5203
        /*
5204
        **      status
5205
        */
5206
        cp->actualquirks                = tp->quirks;
5207
        cp->host_status                 = nego ? HS_NEGOTIATE : HS_BUSY;
5208
        cp->scsi_status                 = S_ILLEGAL;
5209
        cp->parity_status               = 0;
5210
 
5211
        cp->xerr_status                 = XE_OK;
5212
        cp->nego_status                 = nego;
5213
#if 0
5214
        cp->sync_status                 = tp->sval;
5215
        cp->wide_status                 = tp->wval;
5216
#endif
5217
 
5218
        /*----------------------------------------------------
5219
        **
5220
        **      Critical region: start this job.
5221
        **
5222
        **----------------------------------------------------
5223
        */
5224
 
5225
        /*
5226
        **      activate this job.
5227
        */
5228
 
5229
        /* Compute a time limit greater than the middle-level driver one */
5230
        if (cmd->timeout_per_command > 0)
5231
                cp->tlimit      = jiffies + cmd->timeout_per_command + HZ;
5232
        else
5233
                cp->tlimit      = jiffies + 86400 * HZ;/* No timeout=24 hours */
5234
        cp->magic               = CCB_MAGIC;
5235
 
5236
        /*
5237
        **      insert next CCBs into start queue.
5238
        **      2 max at a time is enough to flush the CCB wait queue.
5239
        */
5240
        cp->auto_sense = 0;
5241
        if (lp)
5242
                ncr_start_next_ccb(np, lp, 2);
5243
        else
5244
                ncr_put_start_queue(np, cp);
5245
 
5246
        /*
5247
        **      Command is successfully queued.
5248
        */
5249
 
5250
        return(DID_OK);
5251
}
5252
 
5253
 
5254
/*==========================================================
5255
**
5256
**
5257
**      Insert a CCB into the start queue and wake up the
5258
**      SCRIPTS processor.
5259
**
5260
**
5261
**==========================================================
5262
*/
5263
 
5264
static void ncr_start_next_ccb(ncb_p np, lcb_p lp, int maxn)
5265
{
5266
        XPT_QUEHEAD *qp;
5267
        ccb_p cp;
5268
 
5269
        if (lp->held_ccb)
5270
                return;
5271
 
5272
        while (maxn-- && lp->queuedccbs < lp->queuedepth) {
5273
                qp = xpt_remque_head(&lp->wait_ccbq);
5274
                if (!qp)
5275
                        break;
5276
                ++lp->queuedccbs;
5277
                cp = xpt_que_entry(qp, struct ccb, link_ccbq);
5278
                xpt_insque_tail(qp, &lp->busy_ccbq);
5279
                lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
5280
                        cpu_to_scr(CCB_PHYS (cp, restart));
5281
                ncr_put_start_queue(np, cp);
5282
        }
5283
}
5284
 
5285
static void ncr_put_start_queue(ncb_p np, ccb_p cp)
5286
{
5287
        u_short qidx;
5288
 
5289
        /*
5290
        **      insert into start queue.
5291
        */
5292
        if (!np->squeueput) np->squeueput = 1;
5293
        qidx = np->squeueput + 2;
5294
        if (qidx >= MAX_START + MAX_START) qidx = 1;
5295
 
5296
        np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5297
        MEMORY_BARRIER();
5298
        np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
5299
 
5300
        np->squeueput = qidx;
5301
        ++np->queuedccbs;
5302
        cp->queued = 1;
5303
 
5304
        if (DEBUG_FLAGS & DEBUG_QUEUE)
5305
                printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
5306
 
5307
        /*
5308
        **      Script processor may be waiting for reselect.
5309
        **      Wake it up.
5310
        */
5311
        MEMORY_BARRIER();
5312
        OUTB (nc_istat, SIGP);
5313
}
5314
 
5315
 
5316
/*==========================================================
5317
**
5318
**
5319
**      Start reset process.
5320
**      If reset in progress do nothing.
5321
**      The interrupt handler will reinitialize the chip.
5322
**      The timeout handler will wait for settle_time before
5323
**      clearing it and so resuming command processing.
5324
**
5325
**
5326
**==========================================================
5327
*/
5328
static void ncr_start_reset(ncb_p np)
5329
{
5330
        if (!np->settle_time) {
5331
                (void) ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
5332
        }
5333
 }
5334
 
5335
static int ncr_reset_scsi_bus(ncb_p np, int enab_int, int settle_delay)
5336
{
5337
        u_int32 term;
5338
        int retv = 0;
5339
 
5340
        np->settle_time = jiffies + settle_delay * HZ;
5341
 
5342
        if (bootverbose > 1)
5343
                printk("%s: resetting, "
5344
                        "command processing suspended for %d seconds\n",
5345
                        ncr_name(np), settle_delay);
5346
 
5347
        OUTB (nc_istat, SRST);
5348
        UDELAY (100);
5349
        OUTB (nc_istat, 0);
5350
        UDELAY (2000);  /* The 895 needs time for the bus mode to settle */
5351
        if (enab_int)
5352
                OUTW (nc_sien, RST);
5353
        /*
5354
        **      Enable Tolerant, reset IRQD if present and
5355
        **      properly set IRQ mode, prior to resetting the bus.
5356
        */
5357
        OUTB (nc_stest3, TE);
5358
        OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
5359
        OUTB (nc_scntl1, CRST);
5360
        UDELAY (200);
5361
 
5362
        if (!driver_setup.bus_check)
5363
                goto out;
5364
        /*
5365
        **      Check for no terminators or SCSI bus shorts to ground.
5366
        **      Read SCSI data bus, data parity bits and control signals.
5367
        **      We are expecting RESET to be TRUE and other signals to be
5368
        **      FALSE.
5369
        */
5370
        term =  INB(nc_sstat0);                         /* rst, sdp0 */
5371
        term =  ((term & 2) << 7) + ((term & 1) << 16);
5372
        term |= ((INB(nc_sstat2) & 0x01) << 25) |       /* sdp1 */
5373
                (INW(nc_sbdl) << 9) |                   /* d15-0 */
5374
                INB(nc_sbcl);   /* req, ack, bsy, sel, atn, msg, cd, io */
5375
 
5376
        if (!(np->features & FE_WIDE))
5377
                term &= 0x3ffff;
5378
 
5379
        if (term != (2<<7)) {
5380
                printk("%s: suspicious SCSI data while resetting the BUS.\n",
5381
                        ncr_name(np));
5382
                printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
5383
                        "0x%lx, expecting 0x%lx\n",
5384
                        ncr_name(np),
5385
                        (np->features & FE_WIDE) ? "dp1,d15-8," : "",
5386
                        (u_long)term, (u_long)(2<<7));
5387
                if (driver_setup.bus_check == 1)
5388
                        retv = 1;
5389
        }
5390
out:
5391
        OUTB (nc_scntl1, 0);
5392
        return retv;
5393
}
5394
 
5395
/*==========================================================
5396
**
5397
**
5398
**      Reset the SCSI BUS.
5399
**      This is called from the generic SCSI driver.
5400
**
5401
**
5402
**==========================================================
5403
*/
5404
int ncr_reset_bus (ncb_p np, Scsi_Cmnd *cmd, int sync_reset)
5405
{
5406
/*      Scsi_Device        *device    = cmd->device; */
5407
        ccb_p cp;
5408
        int found;
5409
 
5410
/*
5411
 * Return immediately if reset is in progress.
5412
 */
5413
        if (np->settle_time) {
5414
                return SCSI_RESET_PUNT;
5415
        }
5416
/*
5417
 * Start the reset process.
5418
 * The script processor is then assumed to be stopped.
5419
 * Commands will now be queued in the waiting list until a settle
5420
 * delay of 2 seconds will be completed.
5421
 */
5422
        ncr_start_reset(np);
5423
/*
5424
 * First, look in the wakeup list
5425
 */
5426
        for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
5427
                /*
5428
                **      look for the ccb of this command.
5429
                */
5430
                if (cp->host_status == HS_IDLE) continue;
5431
                if (cp->cmd == cmd) {
5432
                        found = 1;
5433
                        break;
5434
                }
5435
        }
5436
/*
5437
 * Then, look in the waiting list
5438
 */
5439
        if (!found && retrieve_from_waiting_list(0, np, cmd))
5440
                found = 1;
5441
/*
5442
 * Wake-up all awaiting commands with DID_RESET.
5443
 */
5444
        reset_waiting_list(np);
5445
/*
5446
 * Wake-up all pending commands with HS_RESET -> DID_RESET.
5447
 */
5448
        ncr_wakeup(np, HS_RESET);
5449
/*
5450
 * If the involved command was not in a driver queue, and the
5451
 * scsi driver told us reset is synchronous, and the command is not
5452
 * currently in the waiting list, complete it with DID_RESET status,
5453
 * in order to keep it alive.
5454
 */
5455
        if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
5456
                cmd->result = ScsiResult(DID_RESET, 0);
5457
                ncr_queue_done_cmd(np, cmd);
5458
        }
5459
 
5460
        return SCSI_RESET_SUCCESS;
5461
}
5462
 
5463
/*==========================================================
5464
**
5465
**
5466
**      Abort an SCSI command.
5467
**      This is called from the generic SCSI driver.
5468
**
5469
**
5470
**==========================================================
5471
*/
5472
static int ncr_abort_command (ncb_p np, Scsi_Cmnd *cmd)
5473
{
5474
/*      Scsi_Device        *device    = cmd->device; */
5475
        ccb_p cp;
5476
        int found;
5477
        int retv;
5478
 
5479
/*
5480
 * First, look for the scsi command in the waiting list
5481
 */
5482
        if (remove_from_waiting_list(np, cmd)) {
5483
                cmd->result = ScsiResult(DID_ABORT, 0);
5484
                ncr_queue_done_cmd(np, cmd);
5485
                return SCSI_ABORT_SUCCESS;
5486
        }
5487
 
5488
/*
5489
 * Then, look in the wakeup list
5490
 */
5491
        for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
5492
                /*
5493
                **      look for the ccb of this command.
5494
                */
5495
                if (cp->host_status == HS_IDLE) continue;
5496
                if (cp->cmd == cmd) {
5497
                        found = 1;
5498
                        break;
5499
                }
5500
        }
5501
 
5502
        if (!found) {
5503
                return SCSI_ABORT_NOT_RUNNING;
5504
        }
5505
 
5506
        if (np->settle_time) {
5507
                return SCSI_ABORT_SNOOZE;
5508
        }
5509
 
5510
        /*
5511
        **      If the CCB is active, patch schedule jumps for the
5512
        **      script to abort the command.
5513
        */
5514
 
5515
        cp->tlimit = 0;
5516
        switch(cp->host_status) {
5517
        case HS_BUSY:
5518
        case HS_NEGOTIATE:
5519
                printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
5520
                        cp->start.schedule.l_paddr =
5521
                                cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
5522
                retv = SCSI_ABORT_PENDING;
5523
                break;
5524
        case HS_DISCONNECT:
5525
                cp->restart.schedule.l_paddr =
5526
                                cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
5527
                retv = SCSI_ABORT_PENDING;
5528
                break;
5529
        default:
5530
                retv = SCSI_ABORT_NOT_RUNNING;
5531
                break;
5532
 
5533
        }
5534
 
5535
        /*
5536
        **      If there are no requests, the script
5537
        **      processor will sleep on SEL_WAIT_RESEL.
5538
        **      Let's wake it up, since it may have to work.
5539
        */
5540
        OUTB (nc_istat, SIGP);
5541
 
5542
        return retv;
5543
}
5544
 
5545
/*==========================================================
5546
**
5547
**      Linux release module stuff.
5548
**
5549
**      Called before unloading the module
5550
**      Detach the host.
5551
**      We have to free resources and halt the NCR chip
5552
**
5553
**==========================================================
5554
*/
5555
 
5556
#ifdef MODULE
5557
static int ncr_detach(ncb_p np)
5558
{
5559
        ccb_p cp;
5560
        tcb_p tp;
5561
        lcb_p lp;
5562
        int target, lun;
5563
        int i;
5564
 
5565
        printk("%s: releasing host resources\n", ncr_name(np));
5566
 
5567
/*
5568
**      Stop the ncr_timeout process
5569
**      Set release_stage to 1 and wait that ncr_timeout() set it to 2.
5570
*/
5571
 
5572
#ifdef DEBUG_NCR53C8XX
5573
        printk("%s: stopping the timer\n", ncr_name(np));
5574
#endif
5575
        np->release_stage = 1;
5576
        for (i = 50 ; i && np->release_stage != 2 ; i--) MDELAY (100);
5577
        if (np->release_stage != 2)
5578
                printk("%s: the timer seems to be already stopped\n", ncr_name(np));
5579
        else np->release_stage = 2;
5580
 
5581
/*
5582
**      Disable chip interrupts
5583
*/
5584
 
5585
#ifdef DEBUG_NCR53C8XX
5586
        printk("%s: disabling chip interrupts\n", ncr_name(np));
5587
#endif
5588
        OUTW (nc_sien , 0);
5589
        OUTB (nc_dien , 0);
5590
 
5591
/*
5592
**      Free irq
5593
*/
5594
 
5595
#ifdef DEBUG_NCR53C8XX
5596
#ifdef __sparc__
5597
        printk("%s: freeing irq %s\n", ncr_name(np), __irq_itoa(np->irq));
5598
#else
5599
        printk("%s: freeing irq %d\n", ncr_name(np), np->irq);
5600
#endif
5601
#endif
5602
        free_irq(np->irq, np);
5603
 
5604
        /*
5605
        **      Reset NCR chip
5606
        **      Restore bios setting for automatic clock detection.
5607
        */
5608
 
5609
        printk("%s: resetting chip\n", ncr_name(np));
5610
        OUTB (nc_istat,  SRST);
5611
        UDELAY (100);
5612
        OUTB (nc_istat,  0   );
5613
 
5614
        OUTB(nc_dmode,  np->sv_dmode);
5615
        OUTB(nc_dcntl,  np->sv_dcntl);
5616
        OUTB(nc_ctest3, np->sv_ctest3);
5617
        OUTB(nc_ctest4, np->sv_ctest4);
5618
        OUTB(nc_ctest5, np->sv_ctest5);
5619
        OUTB(nc_gpcntl, np->sv_gpcntl);
5620
        OUTB(nc_stest2, np->sv_stest2);
5621
 
5622
        ncr_selectclock(np, np->sv_scntl3);
5623
 
5624
        /*
5625
        **      Release Memory mapped IO region and IO mapped region
5626
        */
5627
 
5628
#ifndef NCR_IOMAPPED
5629
#ifdef DEBUG_NCR53C8XX
5630
        printk("%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
5631
#endif
5632
        unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
5633
#endif /* !NCR_IOMAPPED */
5634
 
5635
#ifdef DEBUG_NCR53C8XX
5636
        printk("%s: releasing IO region %x[%d]\n", ncr_name(np), np->port, 128);
5637
#endif
5638
        release_region(np->port, 128);
5639
 
5640
        /*
5641
        **      Free allocated ccb(s)
5642
        */
5643
 
5644
        while ((cp=np->ccb->link_ccb) != NULL) {
5645
                np->ccb->link_ccb = cp->link_ccb;
5646
                if (cp->host_status) {
5647
                printk("%s: shall free an active ccb (host_status=%d)\n",
5648
                        ncr_name(np), cp->host_status);
5649
                }
5650
#ifdef DEBUG_NCR53C8XX
5651
        printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
5652
#endif
5653
                m_free(cp, sizeof(*cp));
5654
        }
5655
 
5656
        /*
5657
        **      Free allocated tp(s)
5658
        */
5659
 
5660
        for (target = 0; target < MAX_TARGET ; target++) {
5661
                tp=&np->target[target];
5662
                for (lun = 0 ; lun < MAX_LUN ; lun++) {
5663
                        lp = tp->lp[lun];
5664
                        if (lp) {
5665
#ifdef DEBUG_NCR53C8XX
5666
        printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
5667
#endif
5668
                                if (lp->jump_ccb != &lp->jump_ccb_0)
5669
                                        m_free(lp->jump_ccb, 256);
5670
                                m_free(lp, sizeof(*lp));
5671
                        }
5672
                }
5673
        }
5674
 
5675
        printk("%s: host resources successfully released\n", ncr_name(np));
5676
 
5677
        return 1;
5678
}
5679
#endif
5680
 
5681
/*==========================================================
5682
**
5683
**
5684
**      Complete execution of a SCSI command.
5685
**      Signal completion to the generic SCSI driver.
5686
**
5687
**
5688
**==========================================================
5689
*/
5690
 
5691
void ncr_complete (ncb_p np, ccb_p cp)
5692
{
5693
        Scsi_Cmnd *cmd;
5694
        tcb_p tp;
5695
        lcb_p lp;
5696
 
5697
        /*
5698
        **      Sanity check
5699
        */
5700
 
5701
        if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
5702
                return;
5703
 
5704
        /*
5705
        **      timestamp
5706
        **      Optional, spare some CPU time
5707
        */
5708
#ifdef SCSI_NCR_PROFILE_SUPPORT
5709
        ncb_profile (np, cp);
5710
#endif
5711
 
5712
        if (DEBUG_FLAGS & DEBUG_TINY)
5713
                printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp & 0xfff,
5714
                        cp->host_status,cp->scsi_status);
5715
 
5716
        /*
5717
        **      Get command, target and lun pointers.
5718
        */
5719
 
5720
        cmd = cp->cmd;
5721
        cp->cmd = NULL;
5722
        tp = &np->target[cmd->target];
5723
        lp = tp->lp[cmd->lun];
5724
 
5725
        /*
5726
        **      We donnot queue more than 1 ccb per target
5727
        **      with negotiation at any time. If this ccb was
5728
        **      used for negotiation, clear this info in the tcb.
5729
        */
5730
 
5731
        if (cp == tp->nego_cp)
5732
                tp->nego_cp = 0;
5733
 
5734
        /*
5735
        **      If auto-sense performed, change scsi status.
5736
        */
5737
        if (cp->auto_sense) {
5738
                cp->scsi_status = cp->auto_sense;
5739
        }
5740
 
5741
        /*
5742
        **      If we were recovering from queue full or performing
5743
        **      auto-sense, requeue skipped CCBs to the wait queue.
5744
        */
5745
 
5746
        if (lp && lp->held_ccb) {
5747
                if (cp == lp->held_ccb) {
5748
                        xpt_que_splice(&lp->skip_ccbq, &lp->wait_ccbq);
5749
                        xpt_que_init(&lp->skip_ccbq);
5750
                        lp->held_ccb = 0;
5751
                }
5752
        }
5753
 
5754
        /*
5755
        **      Check for parity errors.
5756
        */
5757
 
5758
        if (cp->parity_status > 1) {
5759
                PRINT_ADDR(cmd);
5760
                printk ("%d parity error(s).\n",cp->parity_status);
5761
        }
5762
 
5763
        /*
5764
        **      Check for extended errors.
5765
        */
5766
 
5767
        if (cp->xerr_status != XE_OK) {
5768
                PRINT_ADDR(cmd);
5769
                switch (cp->xerr_status) {
5770
                case XE_EXTRA_DATA:
5771
                        printk ("extraneous data discarded.\n");
5772
                        break;
5773
                case XE_BAD_PHASE:
5774
                        printk ("illegal scsi phase (4/5).\n");
5775
                        break;
5776
                default:
5777
                        printk ("extended error %d.\n", cp->xerr_status);
5778
                        break;
5779
                }
5780
                if (cp->host_status==HS_COMPLETE)
5781
                        cp->host_status = HS_FAIL;
5782
        }
5783
 
5784
        /*
5785
        **      Print out any error for debugging purpose.
5786
        */
5787
        if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5788
                if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
5789
                        PRINT_ADDR(cmd);
5790
                        printk ("ERROR: cmd=%x host_status=%x scsi_status=%x\n",
5791
                                cmd->cmnd[0], cp->host_status, cp->scsi_status);
5792
                }
5793
        }
5794
 
5795
        /*
5796
        **      Check the status.
5797
        */
5798
        if (   (cp->host_status == HS_COMPLETE)
5799
                && (cp->scsi_status == S_GOOD ||
5800
                    cp->scsi_status == S_COND_MET)) {
5801
                /*
5802
                **      All went well (GOOD status).
5803
                **      CONDITION MET status is returned on
5804
                **      `Pre-Fetch' or `Search data' success.
5805
                */
5806
                cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5807
 
5808
                /*
5809
                **      @RESID@
5810
                **      Could dig out the correct value for resid,
5811
                **      but it would be quite complicated.
5812
                */
5813
                /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
5814
 
5815
                /*
5816
                **      Allocate the lcb if not yet.
5817
                */
5818
                if (!lp)
5819
                        ncr_alloc_lcb (np, cmd->target, cmd->lun);
5820
 
5821
                /*
5822
                **      On standard INQUIRY response (EVPD and CmDt
5823
                **      not set), setup logical unit according to
5824
                **      announced capabilities (we need the 1rst 7 bytes).
5825
                */
5826
                if (cmd->cmnd[0] == 0x12 && !(cmd->cmnd[1] & 0x3) &&
5827
                    cmd->cmnd[4] >= 7) {
5828
                        ncr_setup_lcb (np, cmd->target, cmd->lun,
5829
                                       (char *) cmd->request_buffer);
5830
                }
5831
 
5832
                tp->bytes     += cp->data_len;
5833
                tp->transfers ++;
5834
 
5835
                /*
5836
                **      If tags was reduced due to queue full,
5837
                **      increase tags if 1000 good status received.
5838
                */
5839
                if (lp && lp->usetags && lp->numtags < lp->maxtags) {
5840
                        ++lp->num_good;
5841
                        if (lp->num_good >= 1000) {
5842
                                lp->num_good = 0;
5843
                                ++lp->numtags;
5844
                                ncr_setup_tags (np, cmd->target, cmd->lun);
5845
                        }
5846
                }
5847
        } else if ((cp->host_status == HS_COMPLETE)
5848
                && (cp->scsi_status == S_CHECK_COND)) {
5849
                /*
5850
                **   Check condition code
5851
                */
5852
                cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
5853
 
5854
                if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5855
                        u_char * p = (u_char*) & cmd->sense_buffer;
5856
                        int i;
5857
                        PRINT_ADDR(cmd);
5858
                        printk ("sense data:");
5859
                        for (i=0; i<14; i++) printk (" %x", *p++);
5860
                        printk (".\n");
5861
                }
5862
 
5863
        } else if ((cp->host_status == HS_COMPLETE)
5864
                && (cp->scsi_status == S_BUSY ||
5865
                    cp->scsi_status == S_QUEUE_FULL)) {
5866
 
5867
                /*
5868
                **   Target is busy.
5869
                */
5870
                cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5871
 
5872
        } else if ((cp->host_status == HS_SEL_TIMEOUT)
5873
                || (cp->host_status == HS_TIMEOUT)) {
5874
 
5875
                /*
5876
                **   No response
5877
                */
5878
                cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
5879
 
5880
        } else if (cp->host_status == HS_RESET) {
5881
 
5882
                /*
5883
                **   SCSI bus reset
5884
                */
5885
                cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
5886
 
5887
        } else if (cp->host_status == HS_ABORTED) {
5888
 
5889
                /*
5890
                **   Transfer aborted
5891
                */
5892
                cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
5893
 
5894
        } else {
5895
 
5896
                /*
5897
                **  Other protocol messes
5898
                */
5899
                PRINT_ADDR(cmd);
5900
                printk ("COMMAND FAILED (%x %x) @%p.\n",
5901
                        cp->host_status, cp->scsi_status, cp);
5902
 
5903
                cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
5904
        }
5905
 
5906
        /*
5907
        **      trace output
5908
        */
5909
 
5910
        if (tp->usrflag & UF_TRACE) {
5911
                u_char * p;
5912
                int i;
5913
                PRINT_ADDR(cmd);
5914
                printk (" CMD:");
5915
                p = (u_char*) &cmd->cmnd[0];
5916
                for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
5917
 
5918
                if (cp->host_status==HS_COMPLETE) {
5919
                        switch (cp->scsi_status) {
5920
                        case S_GOOD:
5921
                                printk ("  GOOD");
5922
                                break;
5923
                        case S_CHECK_COND:
5924
                                printk ("  SENSE:");
5925
                                p = (u_char*) &cmd->sense_buffer;
5926
                                for (i=0; i<14; i++)
5927
                                        printk (" %x", *p++);
5928
                                break;
5929
                        default:
5930
                                printk ("  STAT: %x\n", cp->scsi_status);
5931
                                break;
5932
                        }
5933
                } else printk ("  HOSTERROR: %x", cp->host_status);
5934
                printk ("\n");
5935
        }
5936
 
5937
        /*
5938
        **      Free this ccb
5939
        */
5940
        ncr_free_ccb (np, cp);
5941
 
5942
        /*
5943
        **      requeue awaiting scsi commands for this lun.
5944
        */
5945
        if (lp && lp->queuedccbs < lp->queuedepth &&
5946
            !xpt_que_empty(&lp->wait_ccbq))
5947
                ncr_start_next_ccb(np, lp, 2);
5948
 
5949
        /*
5950
        **      requeue awaiting scsi commands for this controller.
5951
        */
5952
        if (np->waiting_list)
5953
                requeue_waiting_list(np);
5954
 
5955
        /*
5956
        **      signal completion to generic driver.
5957
        */
5958
        ncr_queue_done_cmd(np, cmd);
5959
}
5960
 
5961
/*==========================================================
5962
**
5963
**
5964
**      Signal all (or one) control block done.
5965
**
5966
**
5967
**==========================================================
5968
*/
5969
 
5970
/*
5971
**      This CCB has been skipped by the NCR.
5972
**      Queue it in the correponding unit queue.
5973
*/
5974
void ncr_ccb_skipped(ncb_p np, ccb_p cp)
5975
{
5976
        tcb_p tp = &np->target[cp->target];
5977
        lcb_p lp = tp->lp[cp->lun];
5978
 
5979
        if (lp && cp != np->ccb) {
5980
                cp->host_status &= ~HS_SKIPMASK;
5981
                cp->start.schedule.l_paddr =
5982
                        cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5983
                xpt_remque(&cp->link_ccbq);
5984
                xpt_insque_tail(&cp->link_ccbq, &lp->skip_ccbq);
5985
                if (cp->queued) {
5986
                        --lp->queuedccbs;
5987
                }
5988
        }
5989
        if (cp->queued) {
5990
                --np->queuedccbs;
5991
                cp->queued = 0;
5992
        }
5993
}
5994
 
5995
/*
5996
**      The NCR has completed CCBs.
5997
**      Look at the DONE QUEUE if enabled, otherwise scan all CCBs
5998
*/
5999
void ncr_wakeup_done (ncb_p np)
6000
{
6001
        ccb_p cp;
6002
#ifdef SCSI_NCR_CCB_DONE_SUPPORT
6003
        int i, j;
6004
 
6005
        i = np->ccb_done_ic;
6006
        while (1) {
6007
                j = i+1;
6008
                if (j >= MAX_DONE)
6009
                        j = 0;
6010
 
6011
                cp = np->ccb_done[j];
6012
                if (!CCB_DONE_VALID(cp))
6013
                        break;
6014
 
6015
                np->ccb_done[j] = (ccb_p) CCB_DONE_EMPTY;
6016
                np->scripth->done_queue[5*j + 4] =
6017
                                cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
6018
                MEMORY_BARRIER();
6019
                np->scripth->done_queue[5*i + 4] =
6020
                                cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
6021
 
6022
                if (cp->host_status & HS_DONEMASK)
6023
                        ncr_complete (np, cp);
6024
                else if (cp->host_status & HS_SKIPMASK)
6025
                        ncr_ccb_skipped (np, cp);
6026
 
6027
                i = j;
6028
        }
6029
        np->ccb_done_ic = i;
6030
#else
6031
        cp = np->ccb;
6032
        while (cp) {
6033
                if (cp->host_status & HS_DONEMASK)
6034
                        ncr_complete (np, cp);
6035
                else if (cp->host_status & HS_SKIPMASK)
6036
                        ncr_ccb_skipped (np, cp);
6037
                cp = cp->link_ccb;
6038
        }
6039
#endif
6040
}
6041
 
6042
/*
6043
**      Complete all active CCBs.
6044
*/
6045
void ncr_wakeup (ncb_p np, u_long code)
6046
{
6047
        ccb_p cp = np->ccb;
6048
 
6049
        while (cp) {
6050
                if (cp->host_status != HS_IDLE) {
6051
                        cp->host_status = code;
6052
                        ncr_complete (np, cp);
6053
                }
6054
                cp = cp->link_ccb;
6055
        }
6056
}
6057
 
6058
/*==========================================================
6059
**
6060
**
6061
**      Start NCR chip.
6062
**
6063
**
6064
**==========================================================
6065
*/
6066
 
6067
void ncr_init (ncb_p np, int reset, char * msg, u_long code)
6068
{
6069
        int     i;
6070
 
6071
        /*
6072
        **      Reset chip if asked, otherwise just clear fifos.
6073
        */
6074
 
6075
        if (reset) {
6076
                OUTB (nc_istat,  SRST);
6077
                UDELAY (100);
6078
        }
6079
        else {
6080
                OUTB (nc_stest3, TE|CSF);
6081
                OUTONB (nc_ctest3, CLF);
6082
        }
6083
 
6084
        /*
6085
        **      Message.
6086
        */
6087
 
6088
        if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
6089
 
6090
        /*
6091
        **      Clear Start Queue
6092
        */
6093
        np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
6094
        for (i = 1; i < MAX_START + MAX_START; i += 2)
6095
                np->scripth0->tryloop[i] =
6096
                                cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
6097
 
6098
        /*
6099
        **      Start at first entry.
6100
        */
6101
        np->squeueput = 0;
6102
        np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
6103
 
6104
        /*
6105
        **      Clear Done Queue
6106
        */
6107
        for (i = 0; i < MAX_DONE; i++) {
6108
                np->ccb_done[i] = (ccb_p) CCB_DONE_EMPTY;
6109
                np->scripth0->done_queue[5*i + 4] =
6110
                        cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
6111
        }
6112
 
6113
        /*
6114
        **      Start at first entry.
6115
        */
6116
        np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
6117
        np->ccb_done_ic = MAX_DONE-1;
6118
        np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
6119
                        cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
6120
 
6121
        /*
6122
        **      Wakeup all pending jobs.
6123
        */
6124
        ncr_wakeup (np, code);
6125
 
6126
        /*
6127
        **      Init chip.
6128
        */
6129
 
6130
        OUTB (nc_istat,  0x00   );      /*  Remove Reset, abort */
6131
        UDELAY (2000);  /* The 895 needs time for the bus mode to settle */
6132
 
6133
        OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
6134
                                        /*  full arb., ena parity, par->ATN  */
6135
        OUTB (nc_scntl1, 0x00);         /*  odd parity, and remove CRST!! */
6136
 
6137
        ncr_selectclock(np, np->rv_scntl3);     /* Select SCSI clock */
6138
 
6139
        OUTB (nc_scid  , RRE|np->myaddr);       /* Adapter SCSI address */
6140
        OUTW (nc_respid, 1ul<<np->myaddr);      /* Id to respond to */
6141
        OUTB (nc_istat , SIGP   );              /*  Signal Process */
6142
        OUTB (nc_dmode , np->rv_dmode);         /* Burst length, dma mode */
6143
        OUTB (nc_ctest5, np->rv_ctest5);        /* Large fifo + large burst */
6144
 
6145
        OUTB (nc_dcntl , NOCOM|np->rv_dcntl);   /* Protect SFBR */
6146
        OUTB (nc_ctest3, np->rv_ctest3);        /* Write and invalidate */
6147
        OUTB (nc_ctest4, np->rv_ctest4);        /* Master parity checking */
6148
 
6149
        OUTB (nc_stest2, EXT|np->rv_stest2);    /* Extended Sreq/Sack filtering */
6150
        OUTB (nc_stest3, TE);                   /* TolerANT enable */
6151
        OUTB (nc_stime0, 0x0c   );              /* HTH disabled  STO 0.25 sec */
6152
 
6153
        /*
6154
        **      Disable disconnects.
6155
        */
6156
 
6157
        np->disc = 0;
6158
 
6159
        /*
6160
        **    Enable GPIO0 pin for writing if LED support.
6161
        */
6162
 
6163
        if (np->features & FE_LED0) {
6164
                OUTOFFB (nc_gpcntl, 0x01);
6165
        }
6166
 
6167
        /*
6168
        **      enable ints
6169
        */
6170
 
6171
        OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
6172
        OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
6173
 
6174
        /*
6175
        **      For 895/6 enable SBMC interrupt and save current SCSI bus mode.
6176
        */
6177
        if (np->features & FE_ULTRA2) {
6178
                OUTONW (nc_sien, SBMC);
6179
                np->scsi_mode = INB (nc_stest4) & SMODE;
6180
        }
6181
 
6182
        /*
6183
        **      DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
6184
        **      Disable overlapped arbitration.
6185
        **      The 896 Rev 1 is also affected by this errata.
6186
        */
6187
        if (np->device_id == PCI_DEVICE_ID_NCR_53C875 &&
6188
            np->revision_id >= 0x10 && np->revision_id <= 0x15)
6189
                OUTB (nc_ctest0, (1<<5));
6190
        else if (np->device_id == PCI_DEVICE_ID_NCR_53C896 &&
6191
                 np->revision_id <= 0x1)
6192
                OUTB (nc_ccntl0, DPR);
6193
 
6194
        /*
6195
        **      Fill in target structure.
6196
        **      Reinitialize usrsync.
6197
        **      Reinitialize usrwide.
6198
        **      Prepare sync negotiation according to actual SCSI bus mode.
6199
        */
6200
 
6201
        for (i=0;i<MAX_TARGET;i++) {
6202
                tcb_p tp = &np->target[i];
6203
 
6204
                tp->sval    = 0;
6205
                tp->wval    = np->rv_scntl3;
6206
 
6207
                if (tp->usrsync != 255) {
6208
                        if (tp->usrsync <= np->maxsync) {
6209
                                if (tp->usrsync < np->minsync) {
6210
                                        tp->usrsync = np->minsync;
6211
                                }
6212
                        }
6213
                        else
6214
                                tp->usrsync = 255;
6215
                };
6216
 
6217
                if (tp->usrwide > np->maxwide)
6218
                        tp->usrwide = np->maxwide;
6219
 
6220
                ncr_negotiate (np, tp);
6221
        }
6222
 
6223
        /*
6224
        **    Start script processor.
6225
        */
6226
        MEMORY_BARRIER();
6227
        if (np->paddr2) {
6228
                if (bootverbose)
6229
                        printk ("%s: Downloading SCSI SCRIPTS.\n",
6230
                                ncr_name(np));
6231
                OUTL (nc_scratcha, vtophys(np->script0));
6232
                OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, start_ram));
6233
        }
6234
        else
6235
                OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
6236
}
6237
 
6238
/*==========================================================
6239
**
6240
**      Prepare the negotiation values for wide and
6241
**      synchronous transfers.
6242
**
6243
**==========================================================
6244
*/
6245
 
6246
static void ncr_negotiate (struct ncb* np, struct tcb* tp)
6247
{
6248
        /*
6249
        **      minsync unit is 4ns !
6250
        */
6251
 
6252
        u_long minsync = tp->usrsync;
6253
 
6254
        /*
6255
        **      SCSI bus mode limit
6256
        */
6257
 
6258
        if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
6259
                if (minsync < 12) minsync = 12;
6260
        }
6261
 
6262
        /*
6263
        **      our limit ..
6264
        */
6265
 
6266
        if (minsync < np->minsync)
6267
                minsync = np->minsync;
6268
 
6269
        /*
6270
        **      divider limit
6271
        */
6272
 
6273
        if (minsync > np->maxsync)
6274
                minsync = 255;
6275
 
6276
        tp->minsync = minsync;
6277
        tp->maxoffs = (minsync<255 ? np->maxoffs : 0);
6278
 
6279
        /*
6280
        **      period=0: has to negotiate sync transfer
6281
        */
6282
 
6283
        tp->period=0;
6284
 
6285
        /*
6286
        **      widedone=0: has to negotiate wide transfer
6287
        */
6288
        tp->widedone=0;
6289
}
6290
 
6291
/*==========================================================
6292
**
6293
**      Get clock factor and sync divisor for a given
6294
**      synchronous factor period.
6295
**      Returns the clock factor (in sxfer) and scntl3
6296
**      synchronous divisor field.
6297
**
6298
**==========================================================
6299
*/
6300
 
6301
static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
6302
{
6303
        u_long  clk = np->clock_khz;    /* SCSI clock frequency in kHz  */
6304
        int     div = np->clock_divn;   /* Number of divisors supported */
6305
        u_long  fak;                    /* Sync factor in sxfer         */
6306
        u_long  per;                    /* Period in tenths of ns       */
6307
        u_long  kpc;                    /* (per * clk)                  */
6308
 
6309
        /*
6310
        **      Compute the synchronous period in tenths of nano-seconds
6311
        */
6312
        if      (sfac <= 10)    per = 250;
6313
        else if (sfac == 11)    per = 303;
6314
        else if (sfac == 12)    per = 500;
6315
        else                    per = 40 * sfac;
6316
 
6317
        /*
6318
        **      Look for the greatest clock divisor that allows an
6319
        **      input speed faster than the period.
6320
        */
6321
        kpc = per * clk;
6322
        while (--div >= 0)
6323
                if (kpc >= (div_10M[div] << 2)) break;
6324
 
6325
        /*
6326
        **      Calculate the lowest clock factor that allows an output
6327
        **      speed not faster than the period.
6328
        */
6329
        fak = (kpc - 1) / div_10M[div] + 1;
6330
 
6331
#if 0   /* This optimization does not seem very usefull */
6332
 
6333
        per = (fak * div_10M[div]) / clk;
6334
 
6335
        /*
6336
        **      Why not to try the immediate lower divisor and to choose
6337
        **      the one that allows the fastest output speed ?
6338
        **      We dont want input speed too much greater than output speed.
6339
        */
6340
        if (div >= 1 && fak < 8) {
6341
                u_long fak2, per2;
6342
                fak2 = (kpc - 1) / div_10M[div-1] + 1;
6343
                per2 = (fak2 * div_10M[div-1]) / clk;
6344
                if (per2 < per && fak2 <= 8) {
6345
                        fak = fak2;
6346
                        per = per2;
6347
                        --div;
6348
                }
6349
        }
6350
#endif
6351
 
6352
        if (fak < 4) fak = 4;   /* Should never happen, too bad ... */
6353
 
6354
        /*
6355
        **      Compute and return sync parameters for the ncr
6356
        */
6357
        *fakp           = fak - 4;
6358
        *scntl3p        = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
6359
}
6360
 
6361
 
6362
/*==========================================================
6363
**
6364
**      Set actual values, sync status and patch all ccbs of
6365
**      a target according to new sync/wide agreement.
6366
**
6367
**==========================================================
6368
*/
6369
 
6370
static void ncr_set_sync_wide_status (ncb_p np, u_char target)
6371
{
6372
        ccb_p cp;
6373
        tcb_p tp = &np->target[target];
6374
 
6375
        /*
6376
        **      set actual value and sync_status
6377
        */
6378
        OUTB (nc_sxfer, tp->sval);
6379
        np->sync_st = tp->sval;
6380
        OUTB (nc_scntl3, tp->wval);
6381
        np->wide_st = tp->wval;
6382
 
6383
        /*
6384
        **      patch ALL ccbs of this target.
6385
        */
6386
        for (cp = np->ccb; cp; cp = cp->link_ccb) {
6387
                if (!cp->cmd) continue;
6388
                if (cp->cmd->target != target) continue;
6389
#if 0
6390
                cp->sync_status = tp->sval;
6391
                cp->wide_status = tp->wval;
6392
#endif
6393
                cp->phys.select.sel_scntl3 = tp->wval;
6394
                cp->phys.select.sel_sxfer  = tp->sval;
6395
        };
6396
}
6397
 
6398
/*==========================================================
6399
**
6400
**      Switch sync mode for current job and it's target
6401
**
6402
**==========================================================
6403
*/
6404
 
6405
static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer)
6406
{
6407
        Scsi_Cmnd *cmd;
6408
        tcb_p tp;
6409
        u_char target = INB (nc_sdid) & 0x0f;
6410
        u_char idiv;
6411
 
6412
        assert (cp && cp->cmd);
6413
        if (!cp) return;
6414
 
6415
        cmd = cp->cmd;
6416
        if (!cmd) return;
6417
 
6418
        assert (target == (cmd->target & 0xf));
6419
 
6420
        tp = &np->target[target];
6421
 
6422
        if (!scntl3 || !(sxfer & 0x1f))
6423
                scntl3 = np->rv_scntl3;
6424
        scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
6425
 
6426
        /*
6427
        **      Deduce the value of controller sync period from scntl3.
6428
        **      period is in tenths of nano-seconds.
6429
        */
6430
 
6431
        idiv = ((scntl3 >> 4) & 0x7);
6432
        if ((sxfer & 0x1f) && idiv)
6433
                tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
6434
        else
6435
                tp->period = 0xffff;
6436
 
6437
        /*
6438
        **       Stop there if sync parameters are unchanged
6439
        */
6440
        if (tp->sval == sxfer && tp->wval == scntl3) return;
6441
        tp->sval = sxfer;
6442
        tp->wval = scntl3;
6443
 
6444
        /*
6445
        **      Bells and whistles   ;-)
6446
        */
6447
        PRINT_TARGET(np, target);
6448
        if (sxfer & 0x01f) {
6449
                unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
6450
                unsigned mb10 = (f10 + tp->period/2) / tp->period;
6451
                char *scsi;
6452
 
6453
                /*
6454
                **  Disable extended Sreq/Sack filtering
6455
                */
6456
                if (tp->period <= 2000) OUTOFFB (nc_stest2, EXT);
6457
 
6458
                /*
6459
                **      Bells and whistles   ;-)
6460
                */
6461
                if      (tp->period < 500)      scsi = "FAST-40";
6462
                else if (tp->period < 1000)     scsi = "FAST-20";
6463
                else if (tp->period < 2000)     scsi = "FAST-10";
6464
                else                            scsi = "FAST-5";
6465
 
6466
                printk ("%s %sSCSI %d.%d MB/s (%d ns, offset %d)\n", scsi,
6467
                        tp->widedone > 1 ? "WIDE " : "",
6468
                        mb10 / 10, mb10 % 10, tp->period / 10, sxfer & 0x1f);
6469
        } else
6470
                printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
6471
 
6472
        /*
6473
        **      set actual value and sync_status
6474
        **      patch ALL ccbs of this target.
6475
        */
6476
        ncr_set_sync_wide_status(np, target);
6477
}
6478
 
6479
/*==========================================================
6480
**
6481
**      Switch wide mode for current job and it's target
6482
**      SCSI specs say: a SCSI device that accepts a WDTR
6483
**      message shall reset the synchronous agreement to
6484
**      asynchronous mode.
6485
**
6486
**==========================================================
6487
*/
6488
 
6489
static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack)
6490
{
6491
        Scsi_Cmnd *cmd;
6492
        u_short target = INB (nc_sdid) & 0x0f;
6493
        tcb_p tp;
6494
        u_char  scntl3;
6495
        u_char  sxfer;
6496
 
6497
        assert (cp && cp->cmd);
6498
        if (!cp) return;
6499
 
6500
        cmd = cp->cmd;
6501
        if (!cmd) return;
6502
 
6503
        assert (target == (cmd->target & 0xf));
6504
 
6505
        tp = &np->target[target];
6506
        tp->widedone  =  wide+1;
6507
        scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
6508
 
6509
        sxfer = ack ? 0 : tp->sval;
6510
 
6511
        /*
6512
        **       Stop there if sync/wide parameters are unchanged
6513
        */
6514
        if (tp->sval == sxfer && tp->wval == scntl3) return;
6515
        tp->sval = sxfer;
6516
        tp->wval = scntl3;
6517
 
6518
        /*
6519
        **      Bells and whistles   ;-)
6520
        */
6521
        if (bootverbose >= 2) {
6522
                PRINT_TARGET(np, target);
6523
                if (scntl3 & EWS)
6524
                        printk ("WIDE SCSI (16 bit) enabled.\n");
6525
                else
6526
                        printk ("WIDE SCSI disabled.\n");
6527
        }
6528
 
6529
        /*
6530
        **      set actual value and sync_status
6531
        **      patch ALL ccbs of this target.
6532
        */
6533
        ncr_set_sync_wide_status(np, target);
6534
}
6535
 
6536
/*==========================================================
6537
**
6538
**      Switch tagged mode for a target.
6539
**
6540
**==========================================================
6541
*/
6542
 
6543
static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln)
6544
{
6545
        tcb_p tp = &np->target[tn];
6546
        lcb_p lp = tp->lp[ln];
6547
        u_char   reqtags, maxdepth;
6548
 
6549
        /*
6550
        **      Just in case ...
6551
        */
6552
        if ((!tp) || (!lp))
6553
                return;
6554
 
6555
        /*
6556
        **      If SCSI device queue depth is not yet set, leave here.
6557
        */
6558
        if (!lp->scdev_depth)
6559
                return;
6560
 
6561
        /*
6562
        **      Donnot allow more tags than the SCSI driver can queue
6563
        **      for this device.
6564
        **      Donnot allow more tags than we can handle.
6565
        */
6566
        maxdepth = lp->scdev_depth;
6567
        if (maxdepth > lp->maxnxs)      maxdepth    = lp->maxnxs;
6568
        if (lp->maxtags > maxdepth)     lp->maxtags = maxdepth;
6569
        if (lp->numtags > maxdepth)     lp->numtags = maxdepth;
6570
 
6571
        /*
6572
        **      only devices conformant to ANSI Version >= 2
6573
        **      only devices capable of tagged commands
6574
        **      only if enabled by user ..
6575
        */
6576
        if ((lp->inq_byte7 & INQ7_QUEUE) && lp->numtags > 1) {
6577
                reqtags = lp->numtags;
6578
        } else {
6579
                reqtags = 1;
6580
        };
6581
 
6582
        /*
6583
        **      Update max number of tags
6584
        */
6585
        lp->numtags = reqtags;
6586
        if (lp->numtags > lp->maxtags)
6587
                lp->maxtags = lp->numtags;
6588
 
6589
        /*
6590
        **      If we want to switch tag mode, we must wait
6591
        **      for no CCB to be active.
6592
        */
6593
        if      (reqtags > 1 && lp->usetags) {   /* Stay in tagged mode    */
6594
                if (lp->queuedepth == reqtags)   /* Already announced      */
6595
                        return;
6596
                lp->queuedepth  = reqtags;
6597
        }
6598
        else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode  */
6599
                lp->queuedepth  = reqtags;
6600
                return;
6601
        }
6602
        else {                                   /* Want to switch tag mode */
6603
                if (lp->busyccbs)                /* If not yet safe, return */
6604
                        return;
6605
                lp->queuedepth  = reqtags;
6606
                lp->usetags     = reqtags > 1 ? 1 : 0;
6607
        }
6608
 
6609
        /*
6610
        **      Patch the lun mini-script, according to tag mode.
6611
        */
6612
        lp->jump_tag.l_paddr = lp->usetags?
6613
                        cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
6614
                        cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
6615
 
6616
        /*
6617
        **      Announce change to user.
6618
        */
6619
        if (bootverbose) {
6620
                PRINT_LUN(np, tn, ln);
6621
                if (lp->usetags) {
6622
                        printk("tagged command queue depth set to %d\n", reqtags);
6623
                }
6624
                else {
6625
                        printk("tagged command queueing disabled\n");
6626
                }
6627
        }
6628
}
6629
 
6630
/*----------------------------------------------------
6631
**
6632
**      handle user commands
6633
**
6634
**----------------------------------------------------
6635
*/
6636
 
6637
#ifdef SCSI_NCR_USER_COMMAND_SUPPORT
6638
 
6639
static void ncr_usercmd (ncb_p np)
6640
{
6641
        u_char t;
6642
        tcb_p tp;
6643
 
6644
        switch (np->user.cmd) {
6645
 
6646
        case 0: return;
6647
 
6648
        case UC_SETSYNC:
6649
                for (t=0; t<MAX_TARGET; t++) {
6650
                        if (!((np->user.target>>t)&1)) continue;
6651
                        tp = &np->target[t];
6652
                        tp->usrsync = np->user.data;
6653
                        ncr_negotiate (np, tp);
6654
                };
6655
                break;
6656
 
6657
        case UC_SETTAGS:
6658
                for (t=0; t<MAX_TARGET; t++) {
6659
                        int ln;
6660
                        if (!((np->user.target>>t)&1)) continue;
6661
                        np->target[t].usrtags = np->user.data;
6662
                        for (ln = 0; ln < MAX_LUN; ln++) {
6663
                                lcb_p lp = np->target[t].lp[ln];
6664
                                if (!lp)
6665
                                        continue;
6666
                                lp->maxtags = lp->numtags = np->user.data;
6667
                                ncr_setup_tags (np, t, ln);
6668
                        }
6669
                };
6670
                break;
6671
 
6672
        case UC_SETDEBUG:
6673
#ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
6674
                ncr_debug = np->user.data;
6675
#endif
6676
                break;
6677
 
6678
        case UC_SETORDER:
6679
                np->order = np->user.data;
6680
                break;
6681
 
6682
        case UC_SETVERBOSE:
6683
                np->verbose = np->user.data;
6684
                break;
6685
 
6686
        case UC_SETWIDE:
6687
                for (t=0; t<MAX_TARGET; t++) {
6688
                        u_long size;
6689
                        if (!((np->user.target>>t)&1)) continue;
6690
                        tp = &np->target[t];
6691
                        size = np->user.data;
6692
                        if (size > np->maxwide) size=np->maxwide;
6693
                        tp->usrwide = size;
6694
                        ncr_negotiate (np, tp);
6695
                };
6696
                break;
6697
 
6698
        case UC_SETFLAG:
6699
                for (t=0; t<MAX_TARGET; t++) {
6700
                        if (!((np->user.target>>t)&1)) continue;
6701
                        tp = &np->target[t];
6702
                        tp->usrflag = np->user.data;
6703
                };
6704
                break;
6705
 
6706
#ifdef SCSI_NCR_PROFILE_SUPPORT
6707
        case UC_CLEARPROF:
6708
                bzero(&np->profile, sizeof(np->profile));
6709
                break;
6710
#endif
6711
        }
6712
        np->user.cmd=0;
6713
}
6714
#endif
6715
 
6716
/*==========================================================
6717
**
6718
**
6719
**      ncr timeout handler.
6720
**
6721
**
6722
**==========================================================
6723
**
6724
**      Misused to keep the driver running when
6725
**      interrupts are not configured correctly.
6726
**
6727
**----------------------------------------------------------
6728
*/
6729
 
6730
static void ncr_timeout (ncb_p np)
6731
{
6732
        u_long  thistime = jiffies;
6733
 
6734
        /*
6735
        **      If release process in progress, let's go
6736
        **      Set the release stage from 1 to 2 to synchronize
6737
        **      with the release process.
6738
        */
6739
 
6740
        if (np->release_stage) {
6741
                if (np->release_stage == 1) np->release_stage = 2;
6742
                return;
6743
        }
6744
 
6745
        np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
6746
        add_timer(&np->timer);
6747
 
6748
        /*
6749
        **      If we are resetting the ncr, wait for settle_time before
6750
        **      clearing it. Then command processing will be resumed.
6751
        */
6752
        if (np->settle_time) {
6753
                if (np->settle_time <= thistime) {
6754
                        if (bootverbose > 1)
6755
                                printk("%s: command processing resumed\n", ncr_name(np));
6756
                        np->settle_time = 0;
6757
                        np->disc        = 1;
6758
                        requeue_waiting_list(np);
6759
                }
6760
                return;
6761
        }
6762
 
6763
        /*
6764
        **      Since the generic scsi driver only allows us 0.5 second
6765
        **      to perform abort of a command, we must look at ccbs about
6766
        **      every 0.25 second.
6767
        */
6768
        if (np->lasttime + 4*HZ < thistime) {
6769
                /*
6770
                **      block ncr interrupts
6771
                */
6772
                np->lasttime = thistime;
6773
 
6774
#ifdef SCSI_NCR_PROFILE_SUPPORT
6775
                /*
6776
                **      Reset profile data to avoid ugly overflow
6777
                **      (Limited to 1024 GB for 32 bit architecture)
6778
                */
6779
                if (np->profile.num_kbytes > (~0UL >> 2))
6780
                        bzero(&np->profile, sizeof(np->profile));
6781
#endif
6782
        }
6783
 
6784
#ifdef SCSI_NCR_BROKEN_INTR
6785
        if (INB(nc_istat) & (INTF|SIP|DIP)) {
6786
 
6787
                /*
6788
                **      Process pending interrupts.
6789
                */
6790
                if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
6791
                ncr_exception (np);
6792
                if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
6793
        }
6794
#endif /* SCSI_NCR_BROKEN_INTR */
6795
}
6796
 
6797
/*==========================================================
6798
**
6799
**      log message for real hard errors
6800
**
6801
**      "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
6802
**      "             reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
6803
**
6804
**      exception register:
6805
**              ds:     dstat
6806
**              si:     sist
6807
**
6808
**      SCSI bus lines:
6809
**              so:     control lines as driver by NCR.
6810
**              si:     control lines as seen by NCR.
6811
**              sd:     scsi data lines as seen by NCR.
6812
**
6813
**      wide/fastmode:
6814
**              sxfer:  (see the manual)
6815
**              scntl3: (see the manual)
6816
**
6817
**      current script command:
6818
**              dsp:    script adress (relative to start of script).
6819
**              dbc:    first word of script command.
6820
**
6821
**      First 16 register of the chip:
6822
**              r0..rf
6823
**
6824
**==========================================================
6825
*/
6826
 
6827
static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
6828
{
6829
        u_int32 dsp;
6830
        int     script_ofs;
6831
        int     script_size;
6832
        char    *script_name;
6833
        u_char  *script_base;
6834
        int     i;
6835
 
6836
        dsp     = INL (nc_dsp);
6837
 
6838
        if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
6839
                script_ofs      = dsp - np->p_script;
6840
                script_size     = sizeof(struct script);
6841
                script_base     = (u_char *) np->script0;
6842
                script_name     = "script";
6843
        }
6844
        else if (np->p_scripth < dsp &&
6845
                 dsp <= np->p_scripth + sizeof(struct scripth)) {
6846
                script_ofs      = dsp - np->p_scripth;
6847
                script_size     = sizeof(struct scripth);
6848
                script_base     = (u_char *) np->scripth0;
6849
                script_name     = "scripth";
6850
        } else {
6851
                script_ofs      = dsp;
6852
                script_size     = 0;
6853
                script_base     = 0;
6854
                script_name     = "mem";
6855
        }
6856
 
6857
        printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
6858
                ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
6859
                (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
6860
                (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
6861
                (unsigned)INL (nc_dbc));
6862
 
6863
        if (((script_ofs & 3) == 0) &&
6864
            (unsigned)script_ofs < script_size) {
6865
                printk ("%s: script cmd = %08x\n", ncr_name(np),
6866
                        scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
6867
        }
6868
 
6869
        printk ("%s: regdump:", ncr_name(np));
6870
        for (i=0; i<16;i++)
6871
            printk (" %02x", (unsigned)INB_OFF(i));
6872
        printk (".\n");
6873
}
6874
 
6875
/*============================================================
6876
**
6877
**      ncr chip exception handler.
6878
**
6879
**============================================================
6880
**
6881
**      In normal cases, interrupt conditions occur one at a
6882
**      time. The ncr is able to stack in some extra registers
6883
**      other interrupts that will occurs after the first one.
6884
**      But severall interrupts may occur at the same time.
6885
**
6886
**      We probably should only try to deal with the normal
6887
**      case, but it seems that multiple interrupts occur in
6888
**      some cases that are not abnormal at all.
6889
**
6890
**      The most frequent interrupt condition is Phase Mismatch.
6891
**      We should want to service this interrupt quickly.
6892
**      A SCSI parity error may be delivered at the same time.
6893
**      The SIR interrupt is not very frequent in this driver,
6894
**      since the INTFLY is likely used for command completion
6895
**      signaling.
6896
**      The Selection Timeout interrupt may be triggered with
6897
**      IID and/or UDC.
6898
**      The SBMC interrupt (SCSI Bus Mode Change) may probably
6899
**      occur at any time.
6900
**
6901
**      This handler try to deal as cleverly as possible with all
6902
**      the above.
6903
**
6904
**============================================================
6905
*/
6906
 
6907
void ncr_exception (ncb_p np)
6908
{
6909
        u_char  istat, dstat;
6910
        u_short sist;
6911
        int     i;
6912
 
6913
        /*
6914
        **      interrupt on the fly ?
6915
        **      Since the global header may be copied back to a CCB
6916
        **      using a posted PCI memory write, the last operation on
6917
        **      the istat register is a READ in order to flush posted
6918
        **      PCI write commands.
6919
        */
6920
        istat = INB (nc_istat);
6921
        if (istat & INTF) {
6922
                OUTB (nc_istat, (istat & SIGP) | INTF);
6923
                istat = INB (nc_istat);
6924
                if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
6925
#ifdef SCSI_NCR_PROFILE_SUPPORT
6926
                np->profile.num_fly++;
6927
#endif
6928
                ncr_wakeup_done (np);
6929
        };
6930
 
6931
        if (!(istat & (SIP|DIP)))
6932
                return;
6933
 
6934
#ifdef SCSI_NCR_PROFILE_SUPPORT
6935
        np->profile.num_int++;
6936
#endif
6937
 
6938
        if (istat & CABRT)
6939
                OUTB (nc_istat, CABRT);
6940
 
6941
        /*
6942
        **      Steinbach's Guideline for Systems Programming:
6943
        **      Never test for an error condition you don't know how to handle.
6944
        */
6945
 
6946
        sist  = (istat & SIP) ? INW (nc_sist)  : 0;
6947
        dstat = (istat & DIP) ? INB (nc_dstat) : 0;
6948
 
6949
        if (DEBUG_FLAGS & DEBUG_TINY)
6950
                printk ("<%d|%x:%x|%x:%x>",
6951
                        (int)INB(nc_scr0),
6952
                        dstat,sist,
6953
                        (unsigned)INL(nc_dsp),
6954
                        (unsigned)INL(nc_dbc));
6955
 
6956
        /*========================================================
6957
        **      First, interrupts we want to service cleanly.
6958
        **
6959
        **      Phase mismatch is the most frequent interrupt, and
6960
        **      so we have to service it as quickly and as cleanly
6961
        **      as possible.
6962
        **      Programmed interrupts are rarely used in this driver,
6963
        **      but we must handle them cleanly anyway.
6964
        **      We try to deal with PAR and SBMC combined with
6965
        **      some other interrupt(s).
6966
        **=========================================================
6967
        */
6968
 
6969
        if (!(sist  & (STO|GEN|HTH|SGE|UDC|RST)) &&
6970
            !(dstat & (MDPE|BF|ABRT|IID))) {
6971
                if ((sist & SBMC) && ncr_int_sbmc (np))
6972
                        return;
6973
                if ((sist & PAR)  && ncr_int_par  (np))
6974
                        return;
6975
                if (sist & MA) {
6976
                        ncr_int_ma (np);
6977
                        return;
6978
                }
6979
                if (dstat & SIR) {
6980
                        ncr_int_sir (np);
6981
                        return;
6982
                }
6983
                /*
6984
                **  DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
6985
                */
6986
                if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
6987
                        printk( "%s: unknown interrupt(s) ignored, "
6988
                                "ISTAT=%x DSTAT=%x SIST=%x\n",
6989
                                ncr_name(np), istat, dstat, sist);
6990
                        return;
6991
                }
6992
                OUTONB (nc_dcntl, (STD|NOCOM));
6993
                return;
6994
        };
6995
 
6996
        /*========================================================
6997
        **      Now, interrupts that need some fixing up.
6998
        **      Order and multiple interrupts is so less important.
6999
        **
7000
        **      If SRST has been asserted, we just reset the chip.
7001
        **
7002
        **      Selection is intirely handled by the chip. If the
7003
        **      chip says STO, we trust it. Seems some other
7004
        **      interrupts may occur at the same time (UDC, IID), so
7005
        **      we ignore them. In any case we do enough fix-up
7006
        **      in the service routine.
7007
        **      We just exclude some fatal dma errors.
7008
        **=========================================================
7009
        */
7010
 
7011
        if (sist & RST) {
7012
                ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
7013
                return;
7014
        };
7015
 
7016
        if ((sist & STO) &&
7017
                !(dstat & (MDPE|BF|ABRT))) {
7018
        /*
7019
        **      DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
7020
        */
7021
                OUTONB (nc_ctest3, CLF);
7022
 
7023
                ncr_int_sto (np);
7024
                return;
7025
        };
7026
 
7027
        /*=========================================================
7028
        **      Now, interrupts we are not able to recover cleanly.
7029
        **      (At least for the moment).
7030
        **
7031
        **      Do the register dump.
7032
        **      Log message for real hard errors.
7033
        **      Clear all fifos.
7034
        **      For MDPE, BF, ABORT, IID, SGE and HTH we reset the
7035
        **      BUS and the chip.
7036
        **      We are more soft for UDC.
7037
        **=========================================================
7038
        */
7039
        if (jiffies - np->regtime > 10*HZ) {
7040
                np->regtime = jiffies;
7041
                for (i = 0; i<sizeof(np->regdump); i++)
7042
                        ((char*)&np->regdump)[i] = INB_OFF(i);
7043
                np->regdump.nc_dstat = dstat;
7044
                np->regdump.nc_sist  = sist;
7045
        };
7046
 
7047
        ncr_log_hard_error(np, sist, dstat);
7048
 
7049
        printk ("%s: have to clear fifos.\n", ncr_name (np));
7050
        OUTB (nc_stest3, TE|CSF);
7051
        OUTONB (nc_ctest3, CLF);
7052
 
7053
        if ((sist & (SGE)) ||
7054
                (dstat & (MDPE|BF|ABORT|IID))) {
7055
                ncr_start_reset(np);
7056
                return;
7057
        };
7058
 
7059
        if (sist & HTH) {
7060
                printk ("%s: handshake timeout\n", ncr_name(np));
7061
                ncr_start_reset(np);
7062
                return;
7063
        };
7064
 
7065
        if (sist & UDC) {
7066
                printk ("%s: unexpected disconnect\n", ncr_name(np));
7067
                OUTB (HS_PRT, HS_UNEXPECTED);
7068
                OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
7069
                return;
7070
        };
7071
 
7072
        /*=========================================================
7073
        **      We just miss the cause of the interrupt. :(
7074
        **      Print a message. The timeout will do the real work.
7075
        **=========================================================
7076
        */
7077
        printk ("%s: unknown interrupt\n", ncr_name(np));
7078
}
7079
 
7080
/*==========================================================
7081
**
7082
**      ncr chip exception handler for selection timeout
7083
**
7084
**==========================================================
7085
**
7086
**      There seems to be a bug in the 53c810.
7087
**      Although a STO-Interrupt is pending,
7088
**      it continues executing script commands.
7089
**      But it will fail and interrupt (IID) on
7090
**      the next instruction where it's looking
7091
**      for a valid phase.
7092
**
7093
**----------------------------------------------------------
7094
*/
7095
 
7096
void ncr_int_sto (ncb_p np)
7097
{
7098
        u_long dsa;
7099
        ccb_p cp;
7100
        if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
7101
 
7102
        /*
7103
        **      look for ccb and set the status.
7104
        */
7105
 
7106
        dsa = INL (nc_dsa);
7107
        cp = np->ccb;
7108
        while (cp && (CCB_PHYS (cp, phys) != dsa))
7109
                cp = cp->link_ccb;
7110
 
7111
        if (cp) {
7112
                cp-> host_status = HS_SEL_TIMEOUT;
7113
                ncr_complete (np, cp);
7114
        };
7115
 
7116
        /*
7117
        **      repair start queue and jump to start point.
7118
        */
7119
 
7120
        OUTL (nc_dsp, NCB_SCRIPTH_PHYS (np, sto_restart));
7121
        return;
7122
}
7123
 
7124
/*==========================================================
7125
**
7126
**      ncr chip exception handler for SCSI bus mode change
7127
**
7128
**==========================================================
7129
**
7130
**      spi2-r12 11.2.3 says a transceiver mode change must
7131
**      generate a reset event and a device that detects a reset
7132
**      event shall initiate a hard reset. It says also that a
7133
**      device that detects a mode change shall set data transfer
7134
**      mode to eight bit asynchronous, etc...
7135
**      So, just resetting should be enough.
7136
**
7137
**
7138
**----------------------------------------------------------
7139
*/
7140
 
7141
static int ncr_int_sbmc (ncb_p np)
7142
{
7143
        u_char scsi_mode = INB (nc_stest4) & SMODE;
7144
 
7145
        if (scsi_mode != np->scsi_mode) {
7146
                printk("%s: SCSI bus mode change from %x to %x.\n",
7147
                        ncr_name(np), np->scsi_mode, scsi_mode);
7148
 
7149
                np->scsi_mode = scsi_mode;
7150
 
7151
 
7152
                /*
7153
                **      Suspend command processing for 1 second and
7154
                **      reinitialize all except the chip.
7155
                */
7156
                np->settle_time = jiffies + HZ;
7157
                ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
7158
                return 1;
7159
        }
7160
        return 0;
7161
}
7162
 
7163
/*==========================================================
7164
**
7165
**      ncr chip exception handler for SCSI parity error.
7166
**
7167
**==========================================================
7168
**
7169
**
7170
**----------------------------------------------------------
7171
*/
7172
 
7173
static int ncr_int_par (ncb_p np)
7174
{
7175
        u_char  hsts    = INB (HS_PRT);
7176
        u_int32 dbc     = INL (nc_dbc);
7177
        u_char  sstat1  = INB (nc_sstat1);
7178
        int phase       = -1;
7179
        int msg         = -1;
7180
        u_int32 jmp;
7181
 
7182
        printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
7183
                ncr_name(np), hsts, dbc, sstat1);
7184
 
7185
        /*
7186
         *      Ignore the interrupt if the NCR is not connected
7187
         *      to the SCSI bus, since the right work should have
7188
         *      been done on unexpected disconnection handling.
7189
         */
7190
        if (!(INB (nc_scntl1) & ISCON))
7191
                return 0;
7192
 
7193
        /*
7194
         *      If the nexus is not clearly identified, reset the bus.
7195
         *      We will try to do better later.
7196
         */
7197
        if (hsts & HS_INVALMASK)
7198
                goto reset_all;
7199
 
7200
        /*
7201
         *      If the SCSI parity error occurs in MSG IN phase, prepare a
7202
         *      MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
7203
         *      ERROR message and let the device decide to retry the command
7204
         *      or to terminate with check condition. If we were in MSG IN
7205
         *      phase waiting for the response of a negotiation, we will
7206
         *      get SIR_NEGO_FAILED at dispatch.
7207
         */
7208
        if (!(dbc & 0xc0000000))
7209
                phase = (dbc >> 24) & 7;
7210
        if (phase == 7)
7211
                msg = M_PARITY;
7212
        else
7213
                msg = M_ID_ERROR;
7214
 
7215
        /*
7216
         *      If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
7217
         *      script that will ignore all data in bytes until phase
7218
         *      change, since we are not sure the chip will wait the phase
7219
         *      change prior to delivering the interrupt.
7220
         */
7221
        if (phase == 1)
7222
                jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
7223
        else
7224
                jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
7225
 
7226
        OUTONB (nc_ctest3, CLF );       /* clear dma fifo  */
7227
        OUTB (nc_stest3, TE|CSF);       /* clear scsi fifo */
7228
 
7229
        np->msgout[0] = msg;
7230
        OUTL (nc_dsp, jmp);
7231
        return 1;
7232
 
7233
reset_all:
7234
        ncr_start_reset(np);
7235
        return 1;
7236
}
7237
 
7238
/*==========================================================
7239
**
7240
**
7241
**      ncr chip exception handler for phase errors.
7242
**
7243
**
7244
**==========================================================
7245
**
7246
**      We have to construct a new transfer descriptor,
7247
**      to transfer the rest of the current block.
7248
**
7249
**----------------------------------------------------------
7250
*/
7251
 
7252
static void ncr_int_ma (ncb_p np)
7253
{
7254
        u_int32 dbc;
7255
        u_int32 rest;
7256
        u_int32 dsp;
7257
        u_int32 dsa;
7258
        u_int32 nxtdsp;
7259
        u_int32 *vdsp;
7260
        u_int32 oadr, olen;
7261
        u_int32 *tblp;
7262
        ncrcmd *newcmd;
7263
        u_char  cmd, sbcl;
7264
        ccb_p   cp;
7265
 
7266
        dsp     = INL (nc_dsp);
7267
        dbc     = INL (nc_dbc);
7268
        sbcl    = INB (nc_sbcl);
7269
 
7270
        cmd     = dbc >> 24;
7271
        rest    = dbc & 0xffffff;
7272
 
7273
        /*
7274
        **      Take into account dma fifo and various buffers and latches,
7275
        **      only if the interrupted phase is an OUTPUT phase.
7276
        */
7277
 
7278
        if ((cmd & 1) == 0) {
7279
                u_char  ctest5, ss0, ss2;
7280
                u_short delta;
7281
 
7282
                ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
7283
                if (ctest5 & DFS)
7284
                        delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
7285
                else
7286
                        delta=(INB (nc_dfifo) - rest) & 0x7f;
7287
 
7288
                /*
7289
                **      The data in the dma fifo has not been transfered to
7290
                **      the target -> add the amount to the rest
7291
                **      and clear the data.
7292
                **      Check the sstat2 register in case of wide transfer.
7293
                */
7294
 
7295
                rest += delta;
7296
                ss0  = INB (nc_sstat0);
7297
                if (ss0 & OLF) rest++;
7298
                if (ss0 & ORF) rest++;
7299
                if (INB(nc_scntl3) & EWS) {
7300
                        ss2 = INB (nc_sstat2);
7301
                        if (ss2 & OLF1) rest++;
7302
                        if (ss2 & ORF1) rest++;
7303
                };
7304
 
7305
                if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
7306
                        printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
7307
                                (unsigned) rest, (unsigned) delta, ss0);
7308
 
7309
        } else  {
7310
                if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
7311
                        printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
7312
        }
7313
 
7314
        /*
7315
        **      Clear fifos.
7316
        */
7317
        OUTONB (nc_ctest3, CLF );       /* clear dma fifo  */
7318
        OUTB (nc_stest3, TE|CSF);       /* clear scsi fifo */
7319
 
7320
        /*
7321
        **      locate matching cp.
7322
        **      if the interrupted phase is DATA IN or DATA OUT,
7323
        **      trust the global header.
7324
        */
7325
        dsa = INL (nc_dsa);
7326
        if (!(cmd & 6)) {
7327
                cp = np->header.cp;
7328
                if (CCB_PHYS(cp, phys) != dsa)
7329
                        cp = 0;
7330
        } else {
7331
                cp  = np->ccb;
7332
                while (cp && (CCB_PHYS (cp, phys) != dsa))
7333
                        cp = cp->link_ccb;
7334
        }
7335
 
7336
        /*
7337
        **      try to find the interrupted script command,
7338
        **      and the address at which to continue.
7339
        */
7340
        vdsp    = 0;
7341
        nxtdsp  = 0;
7342
        if      (dsp >  np->p_script &&
7343
                 dsp <= np->p_script + sizeof(struct script)) {
7344
                vdsp = (u_int32 *)((char*)np->script0 + (dsp-np->p_script-8));
7345
                nxtdsp = dsp;
7346
        }
7347
        else if (dsp >  np->p_scripth &&
7348
                 dsp <= np->p_scripth + sizeof(struct scripth)) {
7349
                vdsp = (u_int32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
7350
                nxtdsp = dsp;
7351
        }
7352
        else if (cp) {
7353
                if      (dsp == vtophys (&cp->patch[2])) {
7354
                        vdsp = &cp->patch[0];
7355
                        nxtdsp = scr_to_cpu(vdsp[3]);
7356
                }
7357
                else if (dsp == vtophys (&cp->patch[6])) {
7358
                        vdsp = &cp->patch[4];
7359
                        nxtdsp = scr_to_cpu(vdsp[3]);
7360
                }
7361
        }
7362
 
7363
        /*
7364
        **      log the information
7365
        */
7366
 
7367
        if (DEBUG_FLAGS & DEBUG_PHASE) {
7368
                printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
7369
                        cp, np->header.cp,
7370
                        (unsigned)dsp,
7371
                        (unsigned)nxtdsp, vdsp, cmd);
7372
        };
7373
 
7374
        /*
7375
        **      cp=0 means that the DSA does not point to a valid control
7376
        **      block. This should not happen since we donnot use multi-byte
7377
        **      move while we are being reselected ot after command complete.
7378
        **      We are not able to recover from such a phase error.
7379
        */
7380
        if (!cp) {
7381
                printk ("%s: SCSI phase error fixup: "
7382
                        "CCB already dequeued (0x%08lx)\n",
7383
                        ncr_name (np), (u_long) np->header.cp);
7384
                goto reset_all;
7385
        }
7386
 
7387
        /*
7388
        **      get old startaddress and old length.
7389
        */
7390
 
7391
        oadr = scr_to_cpu(vdsp[1]);
7392
 
7393
        if (cmd & 0x10) {       /* Table indirect */
7394
                tblp = (u_int32 *) ((char*) &cp->phys + oadr);
7395
                olen = scr_to_cpu(tblp[0]);
7396
                oadr = scr_to_cpu(tblp[1]);
7397
        } else {
7398
                tblp = (u_int32 *) 0;
7399
                olen = scr_to_cpu(vdsp[0]) & 0xffffff;
7400
        };
7401
 
7402
        if (DEBUG_FLAGS & DEBUG_PHASE) {
7403
                printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
7404
                        (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
7405
                        tblp,
7406
                        (unsigned) olen,
7407
                        (unsigned) oadr);
7408
        };
7409
 
7410
        /*
7411
        **      check cmd against assumed interrupted script command.
7412
        */
7413
 
7414
        if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
7415
                PRINT_ADDR(cp->cmd);
7416
                printk ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
7417
                        (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
7418
 
7419
                goto reset_all;
7420
        }
7421
 
7422
        /*
7423
        **      cp != np->header.cp means that the header of the CCB
7424
        **      currently being processed has not yet been copied to
7425
        **      the global header area. That may happen if the device did
7426
        **      not accept all our messages after having been selected.
7427
        */
7428
        if (cp != np->header.cp) {
7429
                printk ("%s: SCSI phase error fixup: "
7430
                        "CCB address mismatch (0x%08lx != 0x%08lx)\n",
7431
                        ncr_name (np), (u_long) cp, (u_long) np->header.cp);
7432
        }
7433
 
7434
        /*
7435
        **      if old phase not dataphase, leave here.
7436
        */
7437
 
7438
        if (cmd & 0x06) {
7439
                PRINT_ADDR(cp->cmd);
7440
                printk ("phase change %x-%x %d@%08x resid=%d.\n",
7441
                        cmd&7, sbcl&7, (unsigned)olen,
7442
                        (unsigned)oadr, (unsigned)rest);
7443
                goto unexpected_phase;
7444
        };
7445
 
7446
        /*
7447
        **      choose the correct patch area.
7448
        **      if savep points to one, choose the other.
7449
        */
7450
 
7451
        newcmd = cp->patch;
7452
        if (cp->phys.header.savep == cpu_to_scr(vtophys (newcmd))) newcmd+=4;
7453
 
7454
        /*
7455
        **      fillin the commands
7456
        */
7457
 
7458
        newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
7459
        newcmd[1] = cpu_to_scr(oadr + olen - rest);
7460
        newcmd[2] = cpu_to_scr(SCR_JUMP);
7461
        newcmd[3] = cpu_to_scr(nxtdsp);
7462
 
7463
        if (DEBUG_FLAGS & DEBUG_PHASE) {
7464
                PRINT_ADDR(cp->cmd);
7465
                printk ("newcmd[%d] %x %x %x %x.\n",
7466
                        (int) (newcmd - cp->patch),
7467
                        (unsigned)scr_to_cpu(newcmd[0]),
7468
                        (unsigned)scr_to_cpu(newcmd[1]),
7469
                        (unsigned)scr_to_cpu(newcmd[2]),
7470
                        (unsigned)scr_to_cpu(newcmd[3]));
7471
        }
7472
        /*
7473
        **      fake the return address (to the patch).
7474
        **      and restart script processor at dispatcher.
7475
        */
7476
#ifdef SCSI_NCR_PROFILE_SUPPORT
7477
        np->profile.num_break++;
7478
#endif
7479
        OUTL (nc_temp, vtophys (newcmd));
7480
        OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
7481
        return;
7482
 
7483
        /*
7484
        **      Unexpected phase changes that occurs when the current phase
7485
        **      is not a DATA IN or DATA OUT phase are due to error conditions.
7486
        **      Such event may only happen when the SCRIPTS is using a
7487
        **      multibyte SCSI MOVE.
7488
        **
7489
        **      Phase change            Some possible cause
7490
        **
7491
        **      COMMAND  --> MSG IN     SCSI parity error detected by target.
7492
        **      COMMAND  --> STATUS     Bad command or refused by target.
7493
        **      MSG OUT  --> MSG IN     Message rejected by target.
7494
        **      MSG OUT  --> COMMAND    Bogus target that discards extended
7495
        **                              negotiation messages.
7496
        **
7497
        **      The code below does not care of the new phase and so
7498
        **      trusts the target. Why to annoy it ?
7499
        **      If the interrupted phase is COMMAND phase, we restart at
7500
        **      dispatcher.
7501
        **      If a target does not get all the messages after selection,
7502
        **      the code assumes blindly that the target discards extended
7503
        **      messages and clears the negotiation status.
7504
        **      If the target does not want all our response to negotiation,
7505
        **      we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
7506
        **      bloat for such a should_not_happen situation).
7507
        **      In all other situation, we reset the BUS.
7508
        **      Are these assumptions reasonnable ? (Wait and see ...)
7509
        */
7510
unexpected_phase:
7511
        dsp -= 8;
7512
        nxtdsp = 0;
7513
 
7514
        switch (cmd & 7) {
7515
        case 2: /* COMMAND phase */
7516
                nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
7517
                break;
7518
#if 0
7519
        case 3: /* STATUS  phase */
7520
                nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
7521
                break;
7522
#endif
7523
        case 6: /* MSG OUT phase */
7524
                np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
7525
                if      (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
7526
                        cp->host_status = HS_BUSY;
7527
                        nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
7528
                }
7529
                else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
7530
                         dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
7531
                        nxtdsp = dsp - 8; /* Should raise SIR_NEGO_PROTO */
7532
                }
7533
                break;
7534
#if 0
7535
        case 7: /* MSG IN  phase */
7536
                nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
7537
                break;
7538
#endif
7539
        }
7540
 
7541
        if (nxtdsp) {
7542
                OUTL (nc_dsp, nxtdsp);
7543
                return;
7544
        }
7545
 
7546
reset_all:
7547
        ncr_start_reset(np);
7548
}
7549
 
7550
 
7551
static void ncr_sir_to_redo(ncb_p np, int num, ccb_p cp)
7552
{
7553
        Scsi_Cmnd *cmd  = cp->cmd;
7554
        tcb_p tp        = &np->target[cmd->target];
7555
        lcb_p lp        = tp->lp[cmd->lun];
7556
        XPT_QUEHEAD     *qp;
7557
        ccb_p           cp2;
7558
        int             disc_cnt = 0;
7559
        int             busy_cnt = 0;
7560
        u_int32         startp;
7561
        u_char          s_status = INB (SS_PRT);
7562
 
7563
        /*
7564
        **      Let the SCRIPTS processor skip all not yet started CCBs,
7565
        **      and count disconnected CCBs. Since the busy queue is in
7566
        **      the same order as the chip start queue, disconnected CCBs
7567
        **      are before cp and busy ones after.
7568
        */
7569
        if (lp) {
7570
                qp = lp->busy_ccbq.blink;
7571
                while (qp != &lp->busy_ccbq) {
7572
                        cp2 = xpt_que_entry(qp, struct ccb, link_ccbq);
7573
                        qp  = qp->blink;
7574
                        ++busy_cnt;
7575
                        if (cp2 == cp)
7576
                                break;
7577
                        cp2->start.schedule.l_paddr =
7578
                        cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
7579
                }
7580
                lp->held_ccb = cp;      /* Requeue when this one completes */
7581
                disc_cnt = lp->queuedccbs - busy_cnt;
7582
        }
7583
 
7584
        switch(s_status) {
7585
        default:        /* Just for safety, should never happen */
7586
        case S_QUEUE_FULL:
7587
                /*
7588
                **      Decrease number of tags to the number of
7589
                **      disconnected commands.
7590
                */
7591
                if (!lp)
7592
                        goto out;
7593
                if (bootverbose >= 1) {
7594
                        PRINT_ADDR(cmd);
7595
                        printk ("QUEUE FULL! %d busy, %d disconnected CCBs\n",
7596
                                busy_cnt, disc_cnt);
7597
                }
7598
                if (disc_cnt < lp->numtags) {
7599
                        lp->numtags     = disc_cnt > 2 ? disc_cnt : 2;
7600
                        lp->num_good    = 0;
7601
                        ncr_setup_tags (np, cmd->target, cmd->lun);
7602
                }
7603
                /*
7604
                **      Requeue the command to the start queue.
7605
                **      If any disconnected commands,
7606
                **              Clear SIGP.
7607
                **              Jump to reselect.
7608
                */
7609
                cp->phys.header.savep = cp->startp;
7610
                cp->host_status = HS_BUSY;
7611
                cp->scsi_status = S_ILLEGAL;
7612
 
7613
                ncr_put_start_queue(np, cp);
7614
                if (disc_cnt)
7615
                        INB (nc_ctest2);                /* Clear SIGP */
7616
                OUTL (nc_dsp,   NCB_SCRIPT_PHYS (np, reselect));
7617
                return;
7618
        case S_TERMINATED:
7619
        case S_CHECK_COND:
7620
                /*
7621
                **      If we were requesting sense, give up.
7622
                */
7623
                if (cp->auto_sense)
7624
                        goto out;
7625
 
7626
                /*
7627
                **      Device returned CHECK CONDITION status.
7628
                **      Prepare all needed data strutures for getting
7629
                **      sense data.
7630
                **
7631
                **      identify message
7632
                */
7633
                cp->scsi_smsg2[0]        = M_IDENTIFY | cmd->lun;
7634
                cp->phys.smsg.addr      = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
7635
                cp->phys.smsg.size      = cpu_to_scr(1);
7636
 
7637
                /*
7638
                **      sense command
7639
                */
7640
                cp->phys.cmd.addr       = cpu_to_scr(CCB_PHYS (cp, sensecmd));
7641
                cp->phys.cmd.size       = cpu_to_scr(6);
7642
 
7643
                /*
7644
                **      patch requested size into sense command
7645
                */
7646
                cp->sensecmd[0]          = 0x03;
7647
                cp->sensecmd[1]         = cmd->lun << 5;
7648
                cp->sensecmd[4]         = sizeof(cmd->sense_buffer);
7649
 
7650
                /*
7651
                **      sense data
7652
                */
7653
                cp->phys.sense.addr     =
7654
                                cpu_to_scr(vtophys (&cmd->sense_buffer[0]));
7655
                cp->phys.sense.size     =
7656
                                cpu_to_scr(sizeof(cmd->sense_buffer));
7657
 
7658
                /*
7659
                **      requeue the command.
7660
                */
7661
                startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
7662
 
7663
                cp->phys.header.savep   = startp;
7664
                cp->phys.header.goalp   = startp + 24;
7665
                cp->phys.header.lastp   = startp;
7666
                cp->phys.header.wgoalp  = startp + 24;
7667
                cp->phys.header.wlastp  = startp;
7668
 
7669
                cp->host_status = HS_BUSY;
7670
                cp->scsi_status = S_ILLEGAL;
7671
                cp->auto_sense  = s_status;
7672
 
7673
                cp->start.schedule.l_paddr =
7674
                        cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
7675
 
7676
                /*
7677
                **      Select without ATN for quirky devices.
7678
                */
7679
                if (tp->quirks & QUIRK_NOMSG)
7680
                        cp->start.schedule.l_paddr =
7681
                        cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
7682
 
7683
                ncr_put_start_queue(np, cp);
7684
 
7685
                OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
7686
                return;
7687
        }
7688
 
7689
out:
7690
        OUTONB (nc_dcntl, (STD|NOCOM));
7691
        return;
7692
}
7693
 
7694
 
7695
/*==========================================================
7696
**
7697
**
7698
**      ncr chip exception handler for programmed interrupts.
7699
**
7700
**
7701
**==========================================================
7702
*/
7703
 
7704
static int ncr_show_msg (u_char * msg)
7705
{
7706
        u_char i;
7707
        printk ("%x",*msg);
7708
        if (*msg==M_EXTENDED) {
7709
                for (i=1;i<8;i++) {
7710
                        if (i-1>msg[1]) break;
7711
                        printk ("-%x",msg[i]);
7712
                };
7713
                return (i+1);
7714
        } else if ((*msg & 0xf0) == 0x20) {
7715
                printk ("-%x",msg[1]);
7716
                return (2);
7717
        };
7718
        return (1);
7719
}
7720
 
7721
 
7722
void ncr_int_sir (ncb_p np)
7723
{
7724
        u_char scntl3;
7725
        u_char chg, ofs, per, fak, wide;
7726
        u_char num = INB (nc_dsps);
7727
        ccb_p   cp=0;
7728
        u_long  dsa    = INL (nc_dsa);
7729
        u_char  target = INB (nc_sdid) & 0x0f;
7730
        tcb_p   tp     = &np->target[target];
7731
 
7732
        if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
7733
 
7734
        switch (num) {
7735
        case SIR_RESEL_NO_MSG_IN:
7736
        case SIR_RESEL_NO_IDENTIFY:
7737
                /*
7738
                **      If devices reselecting without sending an IDENTIFY
7739
                **      message still exist, this should help.
7740
                **      We just assume lun=0, 1 CCB, no tag.
7741
                */
7742
                if (tp->lp[0]) {
7743
                        OUTL (nc_dsp, scr_to_cpu(tp->lp[0]->jump_ccb[0]));
7744
                        return;
7745
                }
7746
        case SIR_RESEL_BAD_TARGET:      /* Will send a TARGET RESET message */
7747
        case SIR_RESEL_BAD_LUN:         /* Will send a TARGET RESET message */
7748
        case SIR_RESEL_BAD_I_T_L_Q:     /* Will send an ABORT TAG message   */
7749
        case SIR_RESEL_BAD_I_T_L:       /* Will send an ABORT message       */
7750
                printk ("%s:%d: SIR %d, "
7751
                        "incorrect nexus identification on reselection\n",
7752
                        ncr_name (np), target, num);
7753
                goto out;
7754
        case SIR_DONE_OVERFLOW:
7755
                printk ("%s:%d: SIR %d, "
7756
                        "CCB done queue overflow\n",
7757
                        ncr_name (np), target, num);
7758
                goto out;
7759
        case SIR_BAD_STATUS:
7760
                cp = np->header.cp;
7761
                if (!cp || CCB_PHYS (cp, phys) != dsa)
7762
                        goto out;
7763
                ncr_sir_to_redo(np, num, cp);
7764
                return;
7765
        default:
7766
                /*
7767
                **      lookup the ccb
7768
                */
7769
                cp = np->ccb;
7770
                while (cp && (CCB_PHYS (cp, phys) != dsa))
7771
                        cp = cp->link_ccb;
7772
 
7773
                assert (cp && cp == np->header.cp);
7774
 
7775
                if (!cp || cp != np->header.cp)
7776
                        goto out;
7777
        }
7778
 
7779
        switch (num) {
7780
/*-----------------------------------------------------------------------------
7781
**
7782
**      Was Sie schon immer ueber transfermode negotiation wissen wollten ...
7783
**
7784
**      We try to negotiate sync and wide transfer only after
7785
**      a successfull inquire command. We look at byte 7 of the
7786
**      inquire data to determine the capabilities of the target.
7787
**
7788
**      When we try to negotiate, we append the negotiation message
7789
**      to the identify and (maybe) simple tag message.
7790
**      The host status field is set to HS_NEGOTIATE to mark this
7791
**      situation.
7792
**
7793
**      If the target doesn't answer this message immidiately
7794
**      (as required by the standard), the SIR_NEGO_FAIL interrupt
7795
**      will be raised eventually.
7796
**      The handler removes the HS_NEGOTIATE status, and sets the
7797
**      negotiated value to the default (async / nowide).
7798
**
7799
**      If we receive a matching answer immediately, we check it
7800
**      for validity, and set the values.
7801
**
7802
**      If we receive a Reject message immediately, we assume the
7803
**      negotiation has failed, and fall back to standard values.
7804
**
7805
**      If we receive a negotiation message while not in HS_NEGOTIATE
7806
**      state, it's a target initiated negotiation. We prepare a
7807
**      (hopefully) valid answer, set our parameters, and send back
7808
**      this answer to the target.
7809
**
7810
**      If the target doesn't fetch the answer (no message out phase),
7811
**      we assume the negotiation has failed, and fall back to default
7812
**      settings.
7813
**
7814
**      When we set the values, we adjust them in all ccbs belonging
7815
**      to this target, in the controller's register, and in the "phys"
7816
**      field of the controller's struct ncb.
7817
**
7818
**      Possible cases:            hs  sir   msg_in value  send   goto
7819
**      We try to negotiate:
7820
**      -> target doesnt't msgin   NEG FAIL  noop   defa.  -      dispatch
7821
**      -> target rejected our msg NEG FAIL  reject defa.  -      dispatch
7822
**      -> target answered  (ok)   NEG SYNC  sdtr   set    -      clrack
7823
**      -> target answered (!ok)   NEG SYNC  sdtr   defa.  REJ--->msg_bad
7824
**      -> target answered  (ok)   NEG WIDE  wdtr   set    -      clrack
7825
**      -> target answered (!ok)   NEG WIDE  wdtr   defa.  REJ--->msg_bad
7826
**      -> any other msgin         NEG FAIL  noop   defa.  -      dispatch
7827
**
7828
**      Target tries to negotiate:
7829
**      -> incoming message        --- SYNC  sdtr   set    SDTR   -
7830
**      -> incoming message        --- WIDE  wdtr   set    WDTR   -
7831
**      We sent our answer:
7832
**      -> target doesn't msgout   --- PROTO ?      defa.  -      dispatch
7833
**
7834
**-----------------------------------------------------------------------------
7835
*/
7836
 
7837
        case SIR_NEGO_FAILED:
7838
                /*-------------------------------------------------------
7839
                **
7840
                **      Negotiation failed.
7841
                **      Target doesn't send an answer message,
7842
                **      or target rejected our message.
7843
                **
7844
                **      Remove negotiation request.
7845
                **
7846
                **-------------------------------------------------------
7847
                */
7848
                OUTB (HS_PRT, HS_BUSY);
7849
 
7850
                /* fall through */
7851
 
7852
        case SIR_NEGO_PROTO:
7853
                /*-------------------------------------------------------
7854
                **
7855
                **      Negotiation failed.
7856
                **      Target doesn't fetch the answer message.
7857
                **
7858
                **-------------------------------------------------------
7859
                */
7860
 
7861
                if (DEBUG_FLAGS & DEBUG_NEGO) {
7862
                        PRINT_ADDR(cp->cmd);
7863
                        printk ("negotiation failed sir=%x status=%x.\n",
7864
                                num, cp->nego_status);
7865
                };
7866
 
7867
                /*
7868
                **      any error in negotiation:
7869
                **      fall back to default mode.
7870
                */
7871
                switch (cp->nego_status) {
7872
 
7873
                case NS_SYNC:
7874
                        ncr_setsync (np, cp, 0, 0xe0);
7875
                        break;
7876
 
7877
                case NS_WIDE:
7878
                        ncr_setwide (np, cp, 0, 0);
7879
                        break;
7880
 
7881
                };
7882
                np->msgin [0] = M_NOOP;
7883
                np->msgout[0] = M_NOOP;
7884
                cp->nego_status = 0;
7885
                OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
7886
                return;
7887
/*              break;  */
7888
 
7889
        case SIR_NEGO_SYNC:
7890
                /*
7891
                **      Synchronous request message received.
7892
                */
7893
 
7894
                if (DEBUG_FLAGS & DEBUG_NEGO) {
7895
                        PRINT_ADDR(cp->cmd);
7896
                        printk ("sync msgin: ");
7897
                        (void) ncr_show_msg (np->msgin);
7898
                        printk (".\n");
7899
                };
7900
 
7901
                /*
7902
                **      get requested values.
7903
                */
7904
 
7905
                chg = 0;
7906
                per = np->msgin[3];
7907
                ofs = np->msgin[4];
7908
                if (ofs==0) per=255;
7909
 
7910
                /*
7911
                **      if target sends SDTR message,
7912
                **            it CAN transfer synch.
7913
                */
7914
 
7915
                if (ofs)
7916
                        tp->inq_byte7 |= INQ7_SYNC;
7917
 
7918
                /*
7919
                **      check values against driver limits.
7920
                */
7921
 
7922
                if (per < np->minsync)
7923
                        {chg = 1; per = np->minsync;}
7924
                if (per < tp->minsync)
7925
                        {chg = 1; per = tp->minsync;}
7926
                if (ofs > tp->maxoffs)
7927
                        {chg = 1; ofs = tp->maxoffs;}
7928
 
7929
                /*
7930
                **      Check against controller limits.
7931
                */
7932
                fak     = 7;
7933
                scntl3  = 0;
7934
                if (ofs != 0) {
7935
                        ncr_getsync(np, per, &fak, &scntl3);
7936
                        if (fak > 7) {
7937
                                chg = 1;
7938
                                ofs = 0;
7939
                        }
7940
                }
7941
                if (ofs == 0) {
7942
                        fak     = 7;
7943
                        per     = 0;
7944
                        scntl3  = 0;
7945
                        tp->minsync = 0;
7946
                }
7947
 
7948
                if (DEBUG_FLAGS & DEBUG_NEGO) {
7949
                        PRINT_ADDR(cp->cmd);
7950
                        printk ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
7951
                                per, scntl3, ofs, fak, chg);
7952
                }
7953
 
7954
                if (INB (HS_PRT) == HS_NEGOTIATE) {
7955
                        OUTB (HS_PRT, HS_BUSY);
7956
                        switch (cp->nego_status) {
7957
 
7958
                        case NS_SYNC:
7959
                                /*
7960
                                **      This was an answer message
7961
                                */
7962
                                if (chg) {
7963
                                        /*
7964
                                        **      Answer wasn't acceptable.
7965
                                        */
7966
                                        ncr_setsync (np, cp, 0, 0xe0);
7967
                                        OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
7968
                                } else {
7969
                                        /*
7970
                                        **      Answer is ok.
7971
                                        */
7972
                                        ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7973
                                        OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
7974
                                };
7975
                                return;
7976
 
7977
                        case NS_WIDE:
7978
                                ncr_setwide (np, cp, 0, 0);
7979
                                break;
7980
                        };
7981
                };
7982
 
7983
                /*
7984
                **      It was a request. Set value and
7985
                **      prepare an answer message
7986
                */
7987
 
7988
                ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7989
 
7990
                np->msgout[0] = M_EXTENDED;
7991
                np->msgout[1] = 3;
7992
                np->msgout[2] = M_X_SYNC_REQ;
7993
                np->msgout[3] = per;
7994
                np->msgout[4] = ofs;
7995
 
7996
                cp->nego_status = NS_SYNC;
7997
 
7998
                if (DEBUG_FLAGS & DEBUG_NEGO) {
7999
                        PRINT_ADDR(cp->cmd);
8000
                        printk ("sync msgout: ");
8001
                        (void) ncr_show_msg (np->msgout);
8002
                        printk (".\n");
8003
                }
8004
 
8005
                if (!ofs) {
8006
                        OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
8007
                        return;
8008
                }
8009
                np->msgin [0] = M_NOOP;
8010
 
8011
                break;
8012
 
8013
        case SIR_NEGO_WIDE:
8014
                /*
8015
                **      Wide request message received.
8016
                */
8017
                if (DEBUG_FLAGS & DEBUG_NEGO) {
8018
                        PRINT_ADDR(cp->cmd);
8019
                        printk ("wide msgin: ");
8020
                        (void) ncr_show_msg (np->msgin);
8021
                        printk (".\n");
8022
                };
8023
 
8024
                /*
8025
                **      get requested values.
8026
                */
8027
 
8028
                chg  = 0;
8029
                wide = np->msgin[3];
8030
 
8031
                /*
8032
                **      if target sends WDTR message,
8033
                **            it CAN transfer wide.
8034
                */
8035
 
8036
                if (wide)
8037
                        tp->inq_byte7 |= INQ7_WIDE16;
8038
 
8039
                /*
8040
                **      check values against driver limits.
8041
                */
8042
 
8043
                if (wide > tp->usrwide)
8044
                        {chg = 1; wide = tp->usrwide;}
8045
 
8046
                if (DEBUG_FLAGS & DEBUG_NEGO) {
8047
                        PRINT_ADDR(cp->cmd);
8048
                        printk ("wide: wide=%d chg=%d.\n", wide, chg);
8049
                }
8050
 
8051
                if (INB (HS_PRT) == HS_NEGOTIATE) {
8052
                        OUTB (HS_PRT, HS_BUSY);
8053
                        switch (cp->nego_status) {
8054
 
8055
                        case NS_WIDE:
8056
                                /*
8057
                                **      This was an answer message
8058
                                */
8059
                                if (chg) {
8060
                                        /*
8061
                                        **      Answer wasn't acceptable.
8062
                                        */
8063
                                        ncr_setwide (np, cp, 0, 1);
8064
                                        OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
8065
                                } else {
8066
                                        /*
8067
                                        **      Answer is ok.
8068
                                        */
8069
                                        ncr_setwide (np, cp, wide, 1);
8070
                                        OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
8071
                                };
8072
                                return;
8073
 
8074
                        case NS_SYNC:
8075
                                ncr_setsync (np, cp, 0, 0xe0);
8076
                                break;
8077
                        };
8078
                };
8079
 
8080
                /*
8081
                **      It was a request, set value and
8082
                **      prepare an answer message
8083
                */
8084
 
8085
                ncr_setwide (np, cp, wide, 1);
8086
 
8087
                np->msgout[0] = M_EXTENDED;
8088
                np->msgout[1] = 2;
8089
                np->msgout[2] = M_X_WIDE_REQ;
8090
                np->msgout[3] = wide;
8091
 
8092
                np->msgin [0] = M_NOOP;
8093
 
8094
                cp->nego_status = NS_WIDE;
8095
 
8096
                if (DEBUG_FLAGS & DEBUG_NEGO) {
8097
                        PRINT_ADDR(cp->cmd);
8098
                        printk ("wide msgout: ");
8099
                        (void) ncr_show_msg (np->msgin);
8100
                        printk (".\n");
8101
                }
8102
                break;
8103
 
8104
/*--------------------------------------------------------------------
8105
**
8106
**      Processing of special messages
8107
**
8108
**--------------------------------------------------------------------
8109
*/
8110
 
8111
        case SIR_REJECT_RECEIVED:
8112
                /*-----------------------------------------------
8113
                **
8114
                **      We received a M_REJECT message.
8115
                **
8116
                **-----------------------------------------------
8117
                */
8118
 
8119
                PRINT_ADDR(cp->cmd);
8120
                printk ("M_REJECT received (%x:%x).\n",
8121
                        (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
8122
                break;
8123
 
8124
        case SIR_REJECT_SENT:
8125
                /*-----------------------------------------------
8126
                **
8127
                **      We received an unknown message
8128
                **
8129
                **-----------------------------------------------
8130
                */
8131
 
8132
                PRINT_ADDR(cp->cmd);
8133
                printk ("M_REJECT sent for ");
8134
                (void) ncr_show_msg (np->msgin);
8135
                printk (".\n");
8136
                break;
8137
 
8138
/*--------------------------------------------------------------------
8139
**
8140
**      Processing of special messages
8141
**
8142
**--------------------------------------------------------------------
8143
*/
8144
 
8145
        case SIR_IGN_RESIDUE:
8146
                /*-----------------------------------------------
8147
                **
8148
                **      We received an IGNORE RESIDUE message,
8149
                **      which couldn't be handled by the script.
8150
                **
8151
                **-----------------------------------------------
8152
                */
8153
 
8154
                PRINT_ADDR(cp->cmd);
8155
                printk ("M_IGN_RESIDUE received, but not yet implemented.\n");
8156
                break;
8157
#if 0
8158
        case SIR_MISSING_SAVE:
8159
                /*-----------------------------------------------
8160
                **
8161
                **      We received an DISCONNECT message,
8162
                **      but the datapointer wasn't saved before.
8163
                **
8164
                **-----------------------------------------------
8165
                */
8166
 
8167
                PRINT_ADDR(cp->cmd);
8168
                printk ("M_DISCONNECT received, but datapointer not saved: "
8169
                        "data=%x save=%x goal=%x.\n",
8170
                        (unsigned) INL (nc_temp),
8171
                        (unsigned) scr_to_cpu(np->header.savep),
8172
                        (unsigned) scr_to_cpu(np->header.goalp));
8173
                break;
8174
#endif
8175
        };
8176
 
8177
out:
8178
        OUTONB (nc_dcntl, (STD|NOCOM));
8179
}
8180
 
8181
/*==========================================================
8182
**
8183
**
8184
**      Aquire a control block
8185
**
8186
**
8187
**==========================================================
8188
*/
8189
 
8190
static  ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln)
8191
{
8192
        tcb_p tp = &np->target[tn];
8193
        lcb_p lp = tp->lp[ln];
8194
        u_char tag = NO_TAG;
8195
        ccb_p cp = (ccb_p) 0;
8196
 
8197
        /*
8198
        **      Lun structure available ?
8199
        */
8200
        if (lp) {
8201
                XPT_QUEHEAD *qp;
8202
                /*
8203
                **      Keep from using more tags than we can handle.
8204
                */
8205
                if (lp->usetags && lp->busyccbs >= lp->maxnxs)
8206
                        return (ccb_p) 0;
8207
 
8208
                /*
8209
                **      Allocate a new CCB if needed.
8210
                */
8211
                if (xpt_que_empty(&lp->free_ccbq))
8212
                        ncr_alloc_ccb(np, tn, ln);
8213
 
8214
                /*
8215
                **      Tune tag mode if asked by user.
8216
                */
8217
                if (lp->queuedepth != lp->numtags) {
8218
                        ncr_setup_tags(np, tn, ln);
8219
                }
8220
 
8221
                /*
8222
                **      Look for free CCB
8223
                */
8224
                qp = xpt_remque_head(&lp->free_ccbq);
8225
                if (qp) {
8226
                        cp = xpt_que_entry(qp, struct ccb, link_ccbq);
8227
                        if (cp->magic) {
8228
                                PRINT_LUN(np, tn, ln);
8229
                                printk ("ccb free list corrupted (@%p)\n", cp);
8230
                                cp = 0;
8231
                        }
8232
                        else {
8233
                                xpt_insque_tail(qp, &lp->wait_ccbq);
8234
                                ++lp->busyccbs;
8235
                        }
8236
                }
8237
 
8238
                /*
8239
                **      If a CCB is available,
8240
                **      Get a tag for this nexus if required.
8241
                */
8242
                if (cp) {
8243
                        if (lp->usetags)
8244
                                tag = lp->cb_tags[lp->ia_tag];
8245
                }
8246
                else if (lp->actccbs > 0)
8247
                        return (ccb_p) 0;
8248
        }
8249
 
8250
        /*
8251
        **      if nothing available, take the default.
8252
        */
8253
        if (!cp)
8254
                cp = np->ccb;
8255
 
8256
        /*
8257
        **      Wait until available.
8258
        */
8259
#if 0
8260
        while (cp->magic) {
8261
                if (flags & SCSI_NOSLEEP) break;
8262
                if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
8263
                        break;
8264
        };
8265
#endif
8266
 
8267
        if (cp->magic)
8268
                return ((ccb_p) 0);
8269
 
8270
        cp->magic = 1;
8271
 
8272
        /*
8273
        **      Move to next available tag if tag used.
8274
        */
8275
        if (lp) {
8276
                if (tag != NO_TAG) {
8277
                        ++lp->ia_tag;
8278
                        if (lp->ia_tag == SCSI_NCR_MAX_TAGS)
8279
                                lp->ia_tag = 0;
8280
                        lp->tags_umap |= (((tagmap_t) 1) << tag);
8281
                }
8282
        }
8283
 
8284
        /*
8285
        **      Remember all informations needed to free this CCB.
8286
        */
8287
        cp->tag    = tag;
8288
        cp->target = tn;
8289
        cp->lun    = ln;
8290
 
8291
        if (DEBUG_FLAGS & DEBUG_TAGS) {
8292
                PRINT_LUN(np, tn, ln);
8293
                printk ("ccb @%p using tag %d.\n", cp, tag);
8294
        }
8295
 
8296
        return cp;
8297
}
8298
 
8299
/*==========================================================
8300
**
8301
**
8302
**      Release one control block
8303
**
8304
**
8305
**==========================================================
8306
*/
8307
 
8308
static void ncr_free_ccb (ncb_p np, ccb_p cp)
8309
{
8310
        tcb_p tp = &np->target[cp->target];
8311
        lcb_p lp = tp->lp[cp->lun];
8312
 
8313
        if (DEBUG_FLAGS & DEBUG_TAGS) {
8314
                PRINT_LUN(np, cp->target, cp->lun);
8315
                printk ("ccb @%p freeing tag %d.\n", cp, cp->tag);
8316
        }
8317
 
8318
        /*
8319
        **      If lun control block available,
8320
        **      decrement active commands and increment credit,
8321
        **      free the tag if any and remove the JUMP for reselect.
8322
        */
8323
        if (lp) {
8324
                if (cp->tag != NO_TAG) {
8325
                        lp->cb_tags[lp->if_tag++] = cp->tag;
8326
                        if (lp->if_tag == SCSI_NCR_MAX_TAGS)
8327
                                lp->if_tag = 0;
8328
                        lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
8329
                        lp->tags_smap &= lp->tags_umap;
8330
                        lp->jump_ccb[cp->tag] =
8331
                                cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
8332
                } else {
8333
                        lp->jump_ccb[0] =
8334
                                cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
8335
                }
8336
        }
8337
 
8338
        /*
8339
        **      Make this CCB available.
8340
        */
8341
 
8342
        if (lp) {
8343
                if (cp != np->ccb) {
8344
                        xpt_remque(&cp->link_ccbq);
8345
                        xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
8346
                }
8347
                --lp->busyccbs;
8348
                if (cp->queued) {
8349
                        --lp->queuedccbs;
8350
                }
8351
        }
8352
        cp -> host_status = HS_IDLE;
8353
        cp -> magic = 0;
8354
        if (cp->queued) {
8355
                --np->queuedccbs;
8356
                cp->queued = 0;
8357
        }
8358
 
8359
#if 0
8360
        if (cp == np->ccb)
8361
                wakeup ((caddr_t) cp);
8362
#endif
8363
}
8364
 
8365
 
8366
#define ncr_reg_bus_addr(r) \
8367
        (bus_dvma_to_mem(np->paddr) + offsetof (struct ncr_reg, r))
8368
 
8369
/*------------------------------------------------------------------------
8370
**      Initialize the fixed part of a CCB structure.
8371
**------------------------------------------------------------------------
8372
**------------------------------------------------------------------------
8373
*/
8374
static void ncr_init_ccb(ncb_p np, ccb_p cp)
8375
{
8376
        ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
8377
 
8378
        /*
8379
        **      Remember virtual and bus address of this ccb.
8380
        */
8381
        cp->p_ccb          = vtophys(cp);
8382
        cp->phys.header.cp = cp;
8383
 
8384
        /*
8385
        **      This allows xpt_remque to work for the default ccb.
8386
        */
8387
        xpt_que_init(&cp->link_ccbq);
8388
 
8389
        /*
8390
        **      Initialyze the start and restart launch script.
8391
        **
8392
        **      COPY(4) @(...p_phys), @(dsa)
8393
        **      JUMP @(sched_point)
8394
        */
8395
        cp->start.setup_dsa[0]    = cpu_to_scr(copy_4);
8396
        cp->start.setup_dsa[1]   = cpu_to_scr(vtophys(&cp->start.p_phys));
8397
        cp->start.setup_dsa[2]   = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
8398
        cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
8399
        cp->start.p_phys         = cpu_to_scr(vtophys(&cp->phys));
8400
 
8401
        bcopy(&cp->start, &cp->restart, sizeof(cp->restart));
8402
 
8403
        cp->start.schedule.l_paddr   = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
8404
        cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
8405
}
8406
 
8407
 
8408
/*------------------------------------------------------------------------
8409
**      Allocate a CCB and initialize its fixed part.
8410
**------------------------------------------------------------------------
8411
**------------------------------------------------------------------------
8412
*/
8413
static void ncr_alloc_ccb(ncb_p np, u_char tn, u_char ln)
8414
{
8415
        tcb_p tp = &np->target[tn];
8416
        lcb_p lp = tp->lp[ln];
8417
        ccb_p cp = 0;
8418
 
8419
        /*
8420
        **      Allocate memory for this CCB.
8421
        */
8422
        cp = m_alloc(sizeof(struct ccb), 5);
8423
        if (!cp)
8424
                return;
8425
 
8426
        if (DEBUG_FLAGS & DEBUG_ALLOC) {
8427
                PRINT_LUN(np, tn, ln);
8428
                printk ("new ccb @%p.\n", cp);
8429
        }
8430
 
8431
        /*
8432
        **      Count it and initialyze it.
8433
        */
8434
        lp->actccbs++;
8435
        np->actccbs++;
8436
        bzero (cp, sizeof (*cp));
8437
        ncr_init_ccb(np, cp);
8438
 
8439
        /*
8440
        **      Chain into wakeup list and free ccb queue and take it
8441
        **      into account for tagged commands.
8442
        */
8443
        cp->link_ccb      = np->ccb->link_ccb;
8444
        np->ccb->link_ccb = cp;
8445
 
8446
        xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
8447
        ncr_setup_tags (np, tn, ln);
8448
}
8449
 
8450
/*==========================================================
8451
**
8452
**
8453
**      Allocation of resources for Targets/Luns/Tags.
8454
**
8455
**
8456
**==========================================================
8457
*/
8458
 
8459
 
8460
/*------------------------------------------------------------------------
8461
**      Target control block initialisation.
8462
**------------------------------------------------------------------------
8463
**      This data structure is fully initialized after a SCSI command
8464
**      has been successfully completed for this target.
8465
**      It contains a SCRIPT that is called on target reselection.
8466
**------------------------------------------------------------------------
8467
*/
8468
static void ncr_init_tcb (ncb_p np, u_char tn)
8469
{
8470
        tcb_p tp = &np->target[tn];
8471
        ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
8472
        int th = tn & 3;
8473
        int i;
8474
 
8475
        /*
8476
        **      Jump to next tcb if SFBR does not match this target.
8477
        **      JUMP  IF (SFBR != #target#), @(next tcb)
8478
        */
8479
        tp->jump_tcb.l_cmd   =
8480
                cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
8481
        tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
8482
 
8483
        /*
8484
        **      Load the synchronous transfer register.
8485
        **      COPY @(tp->sval), @(sxfer)
8486
        */
8487
        tp->getscr[0] =  cpu_to_scr(copy_1);
8488
        tp->getscr[1] = cpu_to_scr(vtophys (&tp->sval));
8489
        tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
8490
 
8491
        /*
8492
        **      Load the timing register.
8493
        **      COPY @(tp->wval), @(scntl3)
8494
        */
8495
        tp->getscr[3] = cpu_to_scr(copy_1);
8496
        tp->getscr[4] = cpu_to_scr(vtophys (&tp->wval));
8497
        tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
8498
 
8499
        /*
8500
        **      Get the IDENTIFY message and the lun.
8501
        **      CALL @script(resel_lun)
8502
        */
8503
        tp->call_lun.l_cmd   = cpu_to_scr(SCR_CALL);
8504
        tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
8505
 
8506
        /*
8507
        **      Look for the lun control block of this nexus.
8508
        **      For i = 0 to 3
8509
        **              JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
8510
        */
8511
        for (i = 0 ; i < 4 ; i++) {
8512
                tp->jump_lcb[i].l_cmd   =
8513
                                cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
8514
                tp->jump_lcb[i].l_paddr =
8515
                                cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
8516
        }
8517
 
8518
        /*
8519
        **      Link this target control block to the JUMP chain.
8520
        */
8521
        np->jump_tcb[th].l_paddr = cpu_to_scr(vtophys (&tp->jump_tcb));
8522
 
8523
        /*
8524
        **      These assert's should be moved at driver initialisations.
8525
        */
8526
        assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
8527
                offsetof(struct tcb    , sval    )) &3) == 0);
8528
        assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
8529
                offsetof(struct tcb    , wval    )) &3) == 0);
8530
}
8531
 
8532
 
8533
/*------------------------------------------------------------------------
8534
**      Lun control block allocation and initialization.
8535
**------------------------------------------------------------------------
8536
**      This data structure is allocated and initialized after a SCSI
8537
**      command has been successfully completed for this target/lun.
8538
**------------------------------------------------------------------------
8539
*/
8540
static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln)
8541
{
8542
        tcb_p tp = &np->target[tn];
8543
        lcb_p lp = tp->lp[ln];
8544
        ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
8545
        int lh = ln & 3;
8546
 
8547
        /*
8548
        **      Already done, return.
8549
        */
8550
        if (lp)
8551
                return lp;
8552
 
8553
        /*
8554
        **      Allocate the lcb.
8555
        */
8556
        lp = m_alloc(sizeof(struct lcb), 3);
8557
        if (!lp)
8558
                goto fail;
8559
        bzero(lp, sizeof(*lp));
8560
        tp->lp[ln] = lp;
8561
 
8562
        if (DEBUG_FLAGS & DEBUG_ALLOC) {
8563
                PRINT_LUN(np, tn, ln);
8564
                printk ("new lcb @%p.\n", lp);
8565
        }
8566
 
8567
        /*
8568
        **      Initialize the target control block if not yet.
8569
        */
8570
        if (!tp->jump_tcb.l_cmd)
8571
                ncr_init_tcb(np, tn);
8572
 
8573
        /*
8574
        **      Initialize the CCB queue headers.
8575
        */
8576
        xpt_que_init(&lp->free_ccbq);
8577
        xpt_que_init(&lp->busy_ccbq);
8578
        xpt_que_init(&lp->wait_ccbq);
8579
        xpt_que_init(&lp->skip_ccbq);
8580
 
8581
        /*
8582
        **      Set max CCBs to 1 and use the default 1 entry
8583
        **      jump table by default.
8584
        */
8585
        lp->maxnxs      = 1;
8586
        lp->jump_ccb    = &lp->jump_ccb_0;
8587
        lp->p_jump_ccb  = cpu_to_scr(vtophys(lp->jump_ccb));
8588
 
8589
        /*
8590
        **      Initilialyze the reselect script:
8591
        **
8592
        **      Jump to next lcb if SFBR does not match this lun.
8593
        **      Load TEMP with the CCB direct jump table bus address.
8594
        **      Get the SIMPLE TAG message and the tag.
8595
        **
8596
        **      JUMP  IF (SFBR != #lun#), @(next lcb)
8597
        **      COPY @(lp->p_jump_ccb),   @(temp)
8598
        **      JUMP @script(resel_notag)
8599
        */
8600
        lp->jump_lcb.l_cmd   =
8601
                cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
8602
        lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
8603
 
8604
        lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
8605
        lp->load_jump_ccb[1] = cpu_to_scr(vtophys (&lp->p_jump_ccb));
8606
        lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
8607
 
8608
        lp->jump_tag.l_cmd   = cpu_to_scr(SCR_JUMP);
8609
        lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
8610
 
8611
        /*
8612
        **      Link this lun control block to the JUMP chain.
8613
        */
8614
        tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtophys (&lp->jump_lcb));
8615
 
8616
        /*
8617
        **      Initialize command queuing control.
8618
        */
8619
        lp->busyccbs    = 1;
8620
        lp->queuedccbs  = 1;
8621
        lp->queuedepth  = 1;
8622
fail:
8623
        return lp;
8624
}
8625
 
8626
 
8627
/*------------------------------------------------------------------------
8628
**      Lun control block setup on INQUIRY data received.
8629
**------------------------------------------------------------------------
8630
**      We only support WIDE, SYNC for targets and CMDQ for logical units.
8631
**      This setup is done on each INQUIRY since we are expecting user
8632
**      will play with CHANGE DEFINITION commands. :-)
8633
**------------------------------------------------------------------------
8634
*/
8635
static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln, u_char *inq_data)
8636
{
8637
        tcb_p tp = &np->target[tn];
8638
        lcb_p lp = tp->lp[ln];
8639
        u_char inq_byte7;
8640
 
8641
        /*
8642
        **      If no lcb, try to allocate it.
8643
        */
8644
        if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
8645
                goto fail;
8646
 
8647
        /*
8648
        **      Get device quirks from a speciality table.
8649
        */
8650
        tp->quirks = ncr_lookup (inq_data);
8651
        if (tp->quirks && bootverbose) {
8652
                PRINT_LUN(np, tn, ln);
8653
                printk ("quirks=%x.\n", tp->quirks);
8654
        }
8655
 
8656
        /*
8657
        **      Evaluate trustable target/unit capabilities.
8658
        **      We only believe device version >= SCSI-2 that
8659
        **      use appropriate response data format (2).
8660
        */
8661
        inq_byte7 = 0;
8662
        if ((inq_data[2] & 0x7) >= 2 && (inq_data[3] & 0xf) == 2)
8663
                inq_byte7 = inq_data[7];
8664
 
8665
        /*
8666
        **      Throw away announced LUN capabilities if we are told
8667
        **      that there is no real device supported by the logical unit.
8668
        */
8669
        if ((inq_data[0] & 0xe0) > 0x20 || (inq_data[0] & 0x1f) == 0x1f)
8670
                inq_byte7 &= (INQ7_SYNC | INQ7_WIDE16);
8671
 
8672
        /*
8673
        **      If user is wanting SYNC, force this feature.
8674
        */
8675
        if (driver_setup.force_sync_nego)
8676
                inq_byte7 |= INQ7_SYNC;
8677
 
8678
        /*
8679
        **      Prepare negotiation if SIP capabilities have changed.
8680
        */
8681
        tp->inq_done = 1;
8682
        if ((inq_byte7 ^ tp->inq_byte7) & (INQ7_SYNC | INQ7_WIDE16)) {
8683
                tp->inq_byte7 = inq_byte7;
8684
                ncr_negotiate(np, tp);
8685
        }
8686
 
8687
        /*
8688
        **      If unit supports tagged commands, allocate the
8689
        **      CCB JUMP table if not yet.
8690
        */
8691
        if ((inq_byte7 & INQ7_QUEUE) && lp->jump_ccb == &lp->jump_ccb_0) {
8692
                int i;
8693
                lp->jump_ccb = m_alloc(256, 8);
8694
                if (!lp->jump_ccb) {
8695
                        lp->jump_ccb = &lp->jump_ccb_0;
8696
                        goto fail;
8697
                }
8698
                lp->p_jump_ccb = cpu_to_scr(vtophys(lp->jump_ccb));
8699
                for (i = 0 ; i < 64 ; i++)
8700
                        lp->jump_ccb[i] =
8701
                                cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
8702
                for (i = 0 ; i < SCSI_NCR_MAX_TAGS ; i++)
8703
                        lp->cb_tags[i] = i;
8704
                lp->maxnxs = SCSI_NCR_MAX_TAGS;
8705
                lp->tags_stime = jiffies;
8706
        }
8707
 
8708
        /*
8709
        **      Adjust tagged queueing status if needed.
8710
        */
8711
        if ((inq_byte7 ^ lp->inq_byte7) & INQ7_QUEUE) {
8712
                lp->inq_byte7 = inq_byte7;
8713
                lp->numtags   = lp->maxtags;
8714
                ncr_setup_tags (np, tn, ln);
8715
        }
8716
 
8717
fail:
8718
        return lp;
8719
}
8720
 
8721
/*==========================================================
8722
**
8723
**
8724
**      Build Scatter Gather Block
8725
**
8726
**
8727
**==========================================================
8728
**
8729
**      The transfer area may be scattered among
8730
**      several non adjacent physical pages.
8731
**
8732
**      We may use MAX_SCATTER blocks.
8733
**
8734
**----------------------------------------------------------
8735
*/
8736
 
8737
/*
8738
**      We try to reduce the number of interrupts caused
8739
**      by unexpected phase changes due to disconnects.
8740
**      A typical harddisk may disconnect before ANY block.
8741
**      If we wanted to avoid unexpected phase changes at all
8742
**      we had to use a break point every 512 bytes.
8743
**      Of course the number of scatter/gather blocks is
8744
**      limited.
8745
**      Under Linux, the scatter/gatter blocks are provided by
8746
**      the generic driver. We just have to copy addresses and
8747
**      sizes to the data segment array.
8748
*/
8749
 
8750
static  int     ncr_scatter(ccb_p cp, Scsi_Cmnd *cmd)
8751
{
8752
        struct scr_tblmove *data;
8753
        int segment     = 0;
8754
        int use_sg      = (int) cmd->use_sg;
8755
 
8756
        data            = cp->phys.data;
8757
        cp->data_len    = 0;
8758
 
8759
        if (!use_sg) {
8760
                if (cmd->request_bufflen) {
8761
                        data = &data[MAX_SCATTER - 1];
8762
                        data[0].addr = cpu_to_scr(vtophys(cmd->request_buffer));
8763
                        data[0].size = cpu_to_scr(cmd->request_bufflen);
8764
                        cp->data_len = cmd->request_bufflen;
8765
                        segment = 1;
8766
                }
8767
        }
8768
        else if (use_sg <= MAX_SCATTER) {
8769
                struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
8770
 
8771
                data = &data[MAX_SCATTER - use_sg];
8772
                while (segment < use_sg) {
8773
                        data[segment].addr =
8774
                                cpu_to_scr(vtophys(scatter[segment].address));
8775
                        data[segment].size =
8776
                                cpu_to_scr(scatter[segment].length);
8777
                        cp->data_len       += scatter[segment].length;
8778
                        ++segment;
8779
                }
8780
        }
8781
        else {
8782
                return -1;
8783
        }
8784
 
8785
        return segment;
8786
}
8787
 
8788
/*==========================================================
8789
**
8790
**
8791
**      Test the pci bus snoop logic :-(
8792
**
8793
**      Has to be called with interrupts disabled.
8794
**
8795
**
8796
**==========================================================
8797
*/
8798
 
8799
#ifndef NCR_IOMAPPED
8800
__initfunc(
8801
static int ncr_regtest (struct ncb* np)
8802
)
8803
{
8804
        register volatile u_int32 data;
8805
        /*
8806
        **      ncr registers may NOT be cached.
8807
        **      write 0xffffffff to a read only register area,
8808
        **      and try to read it back.
8809
        */
8810
        data = 0xffffffff;
8811
        OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
8812
        data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
8813
#if 1
8814
        if (data == 0xffffffff) {
8815
#else
8816
        if ((data & 0xe2f0fffd) != 0x02000080) {
8817
#endif
8818
                printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
8819
                        (unsigned) data);
8820
                return (0x10);
8821
        };
8822
        return (0);
8823
}
8824
#endif
8825
 
8826
__initfunc(
8827
static int ncr_snooptest (struct ncb* np)
8828
)
8829
{
8830
        u_int32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
8831
        int     i, err=0;
8832
#ifndef NCR_IOMAPPED
8833
        if (np->reg) {
8834
            err |= ncr_regtest (np);
8835
            if (err) return (err);
8836
        }
8837
#endif
8838
        /*
8839
        **      init
8840
        */
8841
        pc  = NCB_SCRIPTH_PHYS (np, snooptest);
8842
        host_wr = 1;
8843
        ncr_wr  = 2;
8844
        /*
8845
        **      Set memory and register.
8846
        */
8847
        np->ncr_cache = cpu_to_scr(host_wr);
8848
        OUTL (nc_temp, ncr_wr);
8849
        /*
8850
        **      Start script (exchange values)
8851
        */
8852
        OUTL (nc_dsp, pc);
8853
        /*
8854
        **      Wait 'til done (with timeout)
8855
        */
8856
        for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
8857
                if (INB(nc_istat) & (INTF|SIP|DIP))
8858
                        break;
8859
        /*
8860
        **      Save termination position.
8861
        */
8862
        pc = INL (nc_dsp);
8863
        /*
8864
        **      Read memory and register.
8865
        */
8866
        host_rd = scr_to_cpu(np->ncr_cache);
8867
        ncr_rd  = INL (nc_scratcha);
8868
        ncr_bk  = INL (nc_temp);
8869
        /*
8870
        **      Reset ncr chip
8871
        */
8872
        OUTB (nc_istat,  SRST);
8873
        UDELAY (100);
8874
        OUTB (nc_istat,  0   );
8875
        /*
8876
        **      check for timeout
8877
        */
8878
        if (i>=NCR_SNOOP_TIMEOUT) {
8879
                printk ("CACHE TEST FAILED: timeout.\n");
8880
                return (0x20);
8881
        };
8882
        /*
8883
        **      Check termination position.
8884
        */
8885
        if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
8886
                printk ("CACHE TEST FAILED: script execution failed.\n");
8887
                printk ("start=%08lx, pc=%08lx, end=%08lx\n",
8888
                        (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
8889
                        (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
8890
                return (0x40);
8891
        };
8892
        /*
8893
        **      Show results.
8894
        */
8895
        if (host_wr != ncr_rd) {
8896
                printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
8897
                        (int) host_wr, (int) ncr_rd);
8898
                err |= 1;
8899
        };
8900
        if (host_rd != ncr_wr) {
8901
                printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
8902
                        (int) ncr_wr, (int) host_rd);
8903
                err |= 2;
8904
        };
8905
        if (ncr_bk != ncr_wr) {
8906
                printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
8907
                        (int) ncr_wr, (int) ncr_bk);
8908
                err |= 4;
8909
        };
8910
        return (err);
8911
}
8912
 
8913
/*==========================================================
8914
**
8915
**
8916
**      Profiling the drivers and targets performance.
8917
**
8918
**
8919
**==========================================================
8920
*/
8921
 
8922
#ifdef SCSI_NCR_PROFILE_SUPPORT
8923
 
8924
/*
8925
**      Compute the difference in jiffies ticks.
8926
*/
8927
 
8928
#define ncr_delta(from, to) \
8929
        ( ((to) && (from))? (to) - (from) : -1 )
8930
 
8931
#define PROFILE  cp->phys.header.stamp
8932
static  void ncb_profile (ncb_p np, ccb_p cp)
8933
{
8934
        long co, st, en, di, re, post, work, disc;
8935
        u_int diff;
8936
 
8937
        PROFILE.end = jiffies;
8938
 
8939
        st = ncr_delta (PROFILE.start,PROFILE.status);
8940
        if (st<0) return;        /* status  not reached  */
8941
 
8942
        co = ncr_delta (PROFILE.start,PROFILE.command);
8943
        if (co<0) return;        /* command not executed */
8944
 
8945
        en = ncr_delta (PROFILE.start,PROFILE.end),
8946
        di = ncr_delta (PROFILE.start,PROFILE.disconnect),
8947
        re = ncr_delta (PROFILE.start,PROFILE.reselect);
8948
        post = en - st;
8949
 
8950
        /*
8951
        **      @PROFILE@  Disconnect time invalid if multiple disconnects
8952
        */
8953
 
8954
        if (di>=0) disc = re - di; else  disc = 0;
8955
 
8956
        work = (st - co) - disc;
8957
 
8958
        diff = (scr_to_cpu(np->disc_phys) - np->disc_ref) & 0xff;
8959
        np->disc_ref += diff;
8960
 
8961
        np->profile.num_trans   += 1;
8962
        if (cp->cmd) {
8963
                np->profile.num_kbytes  += (cp->cmd->request_bufflen >> 10);
8964
                np->profile.rest_bytes  += (cp->cmd->request_bufflen & (0x400-1));
8965
                if (np->profile.rest_bytes >= 0x400) {
8966
                        ++np->profile.num_kbytes;
8967
                        np->profile.rest_bytes  -= 0x400;
8968
                }
8969
        }
8970
        np->profile.num_disc    += diff;
8971
        np->profile.ms_setup    += co;
8972
        np->profile.ms_data     += work;
8973
        np->profile.ms_disc     += disc;
8974
        np->profile.ms_post     += post;
8975
}
8976
#undef PROFILE
8977
 
8978
#endif /* SCSI_NCR_PROFILE_SUPPORT */
8979
 
8980
/*==========================================================
8981
**
8982
**
8983
**      Device lookup.
8984
**
8985
**      @GENSCSI@ should be integrated to scsiconf.c
8986
**
8987
**
8988
**==========================================================
8989
*/
8990
 
8991
struct table_entry {
8992
        char *  manufacturer;
8993
        char *  model;
8994
        char *  version;
8995
        u_long  info;
8996
};
8997
 
8998
static struct table_entry device_tab[] =
8999
{
9000
#if 0
9001
        {"", "", "", QUIRK_NOMSG},
9002
#endif
9003
        {"SONY", "SDT-5000", "3.17", QUIRK_NOMSG},
9004
        {"WangDAT", "Model 2600", "01.7", QUIRK_NOMSG},
9005
        {"WangDAT", "Model 3200", "02.2", QUIRK_NOMSG},
9006
        {"WangDAT", "Model 1300", "02.4", QUIRK_NOMSG},
9007
        {"", "", "", 0} /* catch all: must be last entry. */
9008
};
9009
 
9010
static u_long ncr_lookup(char * id)
9011
{
9012
        struct table_entry * p = device_tab;
9013
        char *d, *r, c;
9014
 
9015
        for (;;p++) {
9016
 
9017
                d = id+8;
9018
                r = p->manufacturer;
9019
                while ((c=*r++)) if (c!=*d++) break;
9020
                if (c) continue;
9021
 
9022
                d = id+16;
9023
                r = p->model;
9024
                while ((c=*r++)) if (c!=*d++) break;
9025
                if (c) continue;
9026
 
9027
                d = id+32;
9028
                r = p->version;
9029
                while ((c=*r++)) if (c!=*d++) break;
9030
                if (c) continue;
9031
 
9032
                return (p->info);
9033
        }
9034
}
9035
 
9036
/*==========================================================
9037
**
9038
**      Determine the ncr's clock frequency.
9039
**      This is essential for the negotiation
9040
**      of the synchronous transfer rate.
9041
**
9042
**==========================================================
9043
**
9044
**      Note: we have to return the correct value.
9045
**      THERE IS NO SAVE DEFAULT VALUE.
9046
**
9047
**      Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
9048
**      53C860 and 53C875 rev. 1 support fast20 transfers but
9049
**      do not have a clock doubler and so are provided with a
9050
**      80 MHz clock. All other fast20 boards incorporate a doubler
9051
**      and so should be delivered with a 40 MHz clock.
9052
**      The future fast40 chips (895/895) use a 40 Mhz base clock
9053
**      and provide a clock quadrupler (160 Mhz). The code below
9054
**      tries to deal as cleverly as possible with all this stuff.
9055
**
9056
**----------------------------------------------------------
9057
*/
9058
 
9059
/*
9060
 *      Select NCR SCSI clock frequency
9061
 */
9062
static void ncr_selectclock(ncb_p np, u_char scntl3)
9063
{
9064
        if (np->multiplier < 2) {
9065
                OUTB(nc_scntl3, scntl3);
9066
                return;
9067
        }
9068
 
9069
        if (bootverbose >= 2)
9070
                printk ("%s: enabling clock multiplier\n", ncr_name(np));
9071
 
9072
        OUTB(nc_stest1, DBLEN);    /* Enable clock multiplier             */
9073
        if (np->multiplier > 2) {  /* Poll bit 5 of stest4 for quadrupler */
9074
                int i = 20;
9075
                while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
9076
                        UDELAY (20);
9077
                if (!i)
9078
                        printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
9079
        } else                  /* Wait 20 micro-seconds for doubler    */
9080
                UDELAY (20);
9081
        OUTB(nc_stest3, HSC);           /* Halt the scsi clock          */
9082
        OUTB(nc_scntl3, scntl3);
9083
        OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier      */
9084
        OUTB(nc_stest3, 0x00);          /* Restart scsi clock           */
9085
}
9086
 
9087
 
9088
/*
9089
 *      calculate NCR SCSI clock frequency (in KHz)
9090
 */
9091
__initfunc(
9092
static unsigned ncrgetfreq (ncb_p np, int gen)
9093
)
9094
{
9095
        unsigned ms = 0;
9096
 
9097
        /*
9098
         * Measure GEN timer delay in order
9099
         * to calculate SCSI clock frequency
9100
         *
9101
         * This code will never execute too
9102
         * many loop iterations (if DELAY is
9103
         * reasonably correct). It could get
9104
         * too low a delay (too high a freq.)
9105
         * if the CPU is slow executing the
9106
         * loop for some reason (an NMI, for
9107
         * example). For this reason we will
9108
         * if multiple measurements are to be
9109
         * performed trust the higher delay
9110
         * (lower frequency returned).
9111
         */
9112
        OUTB (nc_stest1, 0);     /* make sure clock doubler is OFF */
9113
        OUTW (nc_sien , 0);      /* mask all scsi interrupts */
9114
        (void) INW (nc_sist);   /* clear pending scsi interrupt */
9115
        OUTB (nc_dien , 0);      /* mask all dma interrupts */
9116
        (void) INW (nc_sist);   /* another one, just to be sure :) */
9117
        OUTB (nc_scntl3, 4);    /* set pre-scaler to divide by 3 */
9118
        OUTB (nc_stime1, 0);     /* disable general purpose timer */
9119
        OUTB (nc_stime1, gen);  /* set to nominal delay of 1<<gen * 125us */
9120
        while (!(INW(nc_sist) & GEN) && ms++ < 100000)
9121
                UDELAY (1000);  /* count ms */
9122
        OUTB (nc_stime1, 0);     /* disable general purpose timer */
9123
        /*
9124
         * set prescaler to divide by whatever 0 means
9125
         * 0 ought to choose divide by 2, but appears
9126
         * to set divide by 3.5 mode in my 53c810 ...
9127
         */
9128
        OUTB (nc_scntl3, 0);
9129
 
9130
        if (bootverbose >= 2)
9131
                printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
9132
        /*
9133
         * adjust for prescaler, and convert into KHz
9134
         */
9135
        return ms ? ((1 << gen) * 4340) / ms : 0;
9136
}
9137
 
9138
/*
9139
 *      Get/probe NCR SCSI clock frequency
9140
 */
9141
__initfunc(
9142
static void ncr_getclock (ncb_p np, int mult)
9143
)
9144
{
9145
        unsigned char scntl3 = INB(nc_scntl3);
9146
        unsigned char stest1 = INB(nc_stest1);
9147
        unsigned f1;
9148
 
9149
        np->multiplier = 1;
9150
        f1 = 40000;
9151
 
9152
        /*
9153
        **      True with 875 or 895 with clock multiplier selected
9154
        */
9155
        if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
9156
                if (bootverbose >= 2)
9157
                        printk ("%s: clock multiplier found\n", ncr_name(np));
9158
                np->multiplier = mult;
9159
        }
9160
 
9161
        /*
9162
        **      If multiplier not found or scntl3 not 7,5,3,
9163
        **      reset chip and get frequency from general purpose timer.
9164
        **      Otherwise trust scntl3 BIOS setting.
9165
        */
9166
        if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
9167
                unsigned f2;
9168
 
9169
                OUTB(nc_istat, SRST); UDELAY (5); OUTB(nc_istat, 0);
9170
 
9171
                (void) ncrgetfreq (np, 11);     /* throw away first result */
9172
                f1 = ncrgetfreq (np, 11);
9173
                f2 = ncrgetfreq (np, 11);
9174
 
9175
                if (bootverbose)
9176
                        printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
9177
 
9178
                if (f1 > f2) f1 = f2;           /* trust lower result   */
9179
 
9180
                if      (f1 <   45000)          f1 =  40000;
9181
                else if (f1 <   55000)          f1 =  50000;
9182
                else                            f1 =  80000;
9183
 
9184
                if (f1 < 80000 && mult > 1) {
9185
                        if (bootverbose >= 2)
9186
                                printk ("%s: clock multiplier assumed\n", ncr_name(np));
9187
                        np->multiplier  = mult;
9188
                }
9189
        } else {
9190
                if      ((scntl3 & 7) == 3)     f1 =  40000;
9191
                else if ((scntl3 & 7) == 5)     f1 =  80000;
9192
                else                            f1 = 160000;
9193
 
9194
                f1 /= np->multiplier;
9195
        }
9196
 
9197
        /*
9198
        **      Compute controller synchronous parameters.
9199
        */
9200
        f1              *= np->multiplier;
9201
        np->clock_khz   = f1;
9202
}
9203
 
9204
/*===================== LINUX ENTRY POINTS SECTION ==========================*/
9205
 
9206
#ifndef uchar
9207
#define uchar unsigned char
9208
#endif
9209
 
9210
#ifndef ushort
9211
#define ushort unsigned short
9212
#endif
9213
 
9214
#ifndef ulong
9215
#define ulong unsigned long
9216
#endif
9217
 
9218
/* ---------------------------------------------------------------------
9219
**
9220
**      Driver setup from the boot command line
9221
**
9222
** ---------------------------------------------------------------------
9223
*/
9224
 
9225
#ifdef MODULE
9226
#define ARG_SEP ' '
9227
#else
9228
#define ARG_SEP ','
9229
#endif
9230
 
9231
__initfunc(
9232
void ncr53c8xx_setup(char *str, int *ints)
9233
)
9234
{
9235
#ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
9236
        char *cur = str;
9237
        char *pc, *pv;
9238
        int val;
9239
        int base;
9240
        int c;
9241
 
9242
        while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
9243
                char *pe;
9244
 
9245
                val = 0;
9246
                pv = pc;
9247
                c = *++pv;
9248
 
9249
                if      (c == 'n')
9250
                        val = 0;
9251
                else if (c == 'y')
9252
                        val = 1;
9253
                else {
9254
                        base = 0;
9255
                        val = (int) simple_strtoul(pv, &pe, base);
9256
                }
9257
                if      (!strncmp(cur, "tags:", 5)) {
9258
                        int i;
9259
                        driver_setup.default_tags = val;
9260
                        if (pe && *pe == '/') {
9261
                                i = 0;
9262
                                while (*pe && *pe != ARG_SEP &&
9263
                                        i < sizeof(driver_setup.tag_ctrl)-1) {
9264
                                        driver_setup.tag_ctrl[i++] = *pe++;
9265
                                }
9266
                                driver_setup.tag_ctrl[i] = '\0';
9267
                        }
9268
                }
9269
                else if (!strncmp(cur, "mpar:", 5))
9270
                        driver_setup.master_parity      = val;
9271
                else if (!strncmp(cur, "spar:", 5))
9272
                        driver_setup.scsi_parity        = val;
9273
                else if (!strncmp(cur, "disc:", 5))
9274
                        driver_setup.disconnection      = val;
9275
                else if (!strncmp(cur, "specf:", 6))
9276
                        driver_setup.special_features = val;
9277
                else if (!strncmp(cur, "ultra:", 6))
9278
                        driver_setup.ultra_scsi = val;
9279
                else if (!strncmp(cur, "fsn:", 4))
9280
                        driver_setup.force_sync_nego    = val;
9281
                else if (!strncmp(cur, "revprob:", 8))
9282
                        driver_setup.reverse_probe      = val;
9283
                else if (!strncmp(cur, "sync:", 5))
9284
                        driver_setup.default_sync       = val;
9285
                else if (!strncmp(cur, "verb:", 5))
9286
                        driver_setup.verbose    = val;
9287
                else if (!strncmp(cur, "debug:", 6))
9288
                        driver_setup.debug      = val;
9289
                else if (!strncmp(cur, "burst:", 6))
9290
                        driver_setup.burst_max  = val;
9291
                else if (!strncmp(cur, "led:", 4))
9292
                        driver_setup.led_pin    = val;
9293
                else if (!strncmp(cur, "wide:", 5))
9294
                        driver_setup.max_wide   = val? 1:0;
9295
                else if (!strncmp(cur, "settle:", 7))
9296
                        driver_setup.settle_delay= val;
9297
                else if (!strncmp(cur, "diff:", 5))
9298
                        driver_setup.diff_support= val;
9299
                else if (!strncmp(cur, "irqm:", 5))
9300
                        driver_setup.irqm       = val;
9301
                else if (!strncmp(cur, "pcifix:", 7))
9302
                        driver_setup.pci_fix_up = val;
9303
                else if (!strncmp(cur, "buschk:", 7))
9304
                        driver_setup.bus_check  = val;
9305
#ifdef SCSI_NCR_NVRAM_SUPPORT
9306
                else if (!strncmp(cur, "nvram:", 6))
9307
                        driver_setup.use_nvram  = val;
9308
#endif
9309
 
9310
                else if (!strncmp(cur, "safe:", 5) && val)
9311
                        memcpy(&driver_setup, &driver_safe_setup, sizeof(driver_setup));
9312
                else
9313
                        printk("ncr53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
9314
 
9315
                if ((cur = strchr(cur, ARG_SEP)) != NULL)
9316
                        ++cur;
9317
        }
9318
#endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
9319
}
9320
 
9321
static int ncr53c8xx_pci_init(Scsi_Host_Template *tpnt,
9322
             uchar bus, uchar device_fn, ncr_device *device);
9323
 
9324
/*
9325
**   Linux entry point for NCR53C8XX devices detection routine.
9326
**
9327
**   Called by the middle-level scsi drivers at initialization time,
9328
**   or at module installation.
9329
**
9330
**   Read the PCI configuration and try to attach each
9331
**   detected NCR board.
9332
**
9333
**   If NVRAM is present, try to attach boards according to
9334
**   the used defined boot order.
9335
**
9336
**   Returns the number of boards successfully attached.
9337
*/
9338
 
9339
__initfunc(
9340
static void ncr_print_driver_setup(void)
9341
)
9342
{
9343
#define YesNo(y)        y ? 'y' : 'n'
9344
        printk ("ncr53c8xx: setup=disc:%c,specf:%d,ultra:%d,tags:%d,sync:%d,"
9345
                "burst:%d,wide:%c,diff:%d,revprob:%c,buschk:0x%x\n",
9346
                YesNo(driver_setup.disconnection),
9347
                driver_setup.special_features,
9348
                driver_setup.ultra_scsi,
9349
                driver_setup.default_tags,
9350
                driver_setup.default_sync,
9351
                driver_setup.burst_max,
9352
                YesNo(driver_setup.max_wide),
9353
                driver_setup.diff_support,
9354
                YesNo(driver_setup.reverse_probe),
9355
                driver_setup.bus_check);
9356
 
9357
        printk ("ncr53c8xx: setup=mpar:%c,spar:%c,fsn=%c,verb:%d,debug:0x%x,"
9358
                "led:%c,settle:%d,irqm:%d,nvram:0x%x,pcifix:0x%x\n",
9359
                YesNo(driver_setup.master_parity),
9360
                YesNo(driver_setup.scsi_parity),
9361
                YesNo(driver_setup.force_sync_nego),
9362
                driver_setup.verbose,
9363
                driver_setup.debug,
9364
                YesNo(driver_setup.led_pin),
9365
                driver_setup.settle_delay,
9366
                driver_setup.irqm,
9367
                driver_setup.use_nvram,
9368
                driver_setup.pci_fix_up);
9369
#undef YesNo
9370
}
9371
 
9372
/*
9373
**   NCR53C8XX devices description table and chip ids list.
9374
*/
9375
 
9376
static ncr_chip ncr_chip_table[] __initdata     = SCSI_NCR_CHIP_TABLE;
9377
static ushort   ncr_chip_ids[]   __initdata     = SCSI_NCR_CHIP_IDS;
9378
 
9379
#ifdef SCSI_NCR_NVRAM_SUPPORT
9380
__initfunc(
9381
static int
9382
ncr_attach_using_nvram(Scsi_Host_Template *tpnt, int nvram_index, int count, ncr_device device[])
9383
)
9384
{
9385
        int i, j;
9386
        int attach_count = 0;
9387
        ncr_nvram  *nvram;
9388
        ncr_device *devp = 0;    /* to shut up gcc */
9389
 
9390
        if (!nvram_index)
9391
                return 0;
9392
 
9393
        /* find first Symbios NVRAM if there is one as we need to check it for host boot order */
9394
        for (i = 0, nvram_index = -1; i < count; i++) {
9395
                devp  = &device[i];
9396
                nvram = devp->nvram;
9397
                if (!nvram)
9398
                        continue;
9399
                if (nvram->type == SCSI_NCR_SYMBIOS_NVRAM) {
9400
                        if (nvram_index == -1)
9401
                                nvram_index = i;
9402
#ifdef SCSI_NCR_DEBUG_NVRAM
9403
                        printk("ncr53c8xx: NVRAM: Symbios format Boot Block, 53c%s, PCI bus %d, device %d, function %d\n",
9404
                                devp->chip.name, devp->slot.bus,
9405
                                (int) (devp->slot.device_fn & 0xf8) >> 3,
9406
                                (int) devp->slot.device_fn & 7);
9407
                        for (j = 0 ; j < 4 ; j++) {
9408
                                Symbios_host *h = &nvram->data.Symbios.host[j];
9409
                        printk("ncr53c8xx: BOOT[%d] device_id=%04x vendor_id=%04x device_fn=%02x io_port=%04x %s\n",
9410
                                j,              h->device_id,   h->vendor_id,
9411
                                h->device_fn,   h->io_port,
9412
                                (h->flags & SYMBIOS_INIT_SCAN_AT_BOOT) ? "SCAN AT BOOT" : "");
9413
                        }
9414
                }
9415
                else if (nvram->type == SCSI_NCR_TEKRAM_NVRAM) {
9416
                        /* display Tekram nvram data */
9417
                        printk("ncr53c8xx: NVRAM: Tekram format data, 53c%s, PCI bus %d, device %d, function %d\n",
9418
                                devp->chip.name, devp->slot.bus,
9419
                                (int) (devp->slot.device_fn & 0xf8) >> 3,
9420
                                (int) devp->slot.device_fn & 7);
9421
#endif
9422
                }
9423
        }
9424
 
9425
        if (nvram_index >= 0 && nvram_index < count)
9426
                nvram = device[nvram_index].nvram;
9427
        else
9428
                nvram = 0;
9429
 
9430
        if (!nvram)
9431
                goto out;
9432
 
9433
        /*
9434
        ** check devices in the boot record against devices detected.
9435
        ** attach devices if we find a match. boot table records that
9436
        ** do not match any detected devices will be ignored.
9437
        ** devices that do not match any boot table will not be attached
9438
        ** here but will attempt to be attached during the device table
9439
        ** rescan.
9440
        */
9441
        for (i = 0; i < 4; i++) {
9442
                Symbios_host *h = &nvram->data.Symbios.host[i];
9443
                for (j = 0 ; j < count ; j++) {
9444
                        devp = &device[j];
9445
                        if (h->device_fn == devp->slot.device_fn &&
9446
#if 0   /* bus number location in nvram ? */
9447
                            h->bus       == devp->slot.bus       &&
9448
#endif
9449
                            h->device_id == devp->chip.device_id)
9450
                                break;
9451
                }
9452
                if (j < count && !devp->attach_done) {
9453
                        if (!ncr_attach (tpnt, attach_count, devp))
9454
                                attach_count++;
9455
                        devp->attach_done = 1;
9456
                }
9457
        }
9458
 
9459
out:
9460
        return attach_count;
9461
}
9462
#endif /* SCSI_NCR_NVRAM_SUPPORT */
9463
 
9464
__initfunc(
9465
int ncr53c8xx_detect(Scsi_Host_Template *tpnt)
9466
)
9467
{
9468
        int i, j;
9469
        int chips;
9470
        int count = 0;
9471
        uchar bus, device_fn;
9472
        short index;
9473
        int attach_count = 0;
9474
        ncr_device device[8];
9475
#ifdef SCSI_NCR_NVRAM_SUPPORT
9476
        ncr_nvram  nvram[4];
9477
        int k, nvrams;
9478
#endif
9479
        int hosts;
9480
 
9481
#ifdef SCSI_NCR_NVRAM_SUPPORT
9482
        int nvram_index = 0;
9483
#endif
9484
 
9485
#ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
9486
        ncr_debug = driver_setup.debug;
9487
#endif
9488
 
9489
     tpnt->proc_dir = &proc_scsi_ncr53c8xx;
9490
#ifdef SCSI_NCR_PROC_INFO_SUPPORT
9491
     tpnt->proc_info = ncr53c8xx_proc_info;
9492
#endif
9493
 
9494
#if     defined(SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT) && defined(MODULE)
9495
if (ncr53c8xx)
9496
        ncr53c8xx_setup(ncr53c8xx, (int *) 0);
9497
#endif
9498
 
9499
        if (initverbose >= 2)
9500
                ncr_print_driver_setup();
9501
 
9502
        /*
9503
        ** Detect all 53c8xx hosts and then attach them.
9504
        **
9505
        ** If we are using NVRAM, once all hosts are detected, we need to check
9506
        ** any NVRAM for boot order in case detect and boot order differ and
9507
        ** attach them using the order in the NVRAM.
9508
        **
9509
        ** If no NVRAM is found or data appears invalid attach boards in the
9510
        ** the order they are detected.
9511
        */
9512
 
9513
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,92)
9514
        if (!pci_present())
9515
#else
9516
        if (!pcibios_present())
9517
#endif
9518
                return 0;
9519
 
9520
        chips   = sizeof(ncr_chip_ids)  / sizeof(ncr_chip_ids[0]);
9521
        hosts   = sizeof(device)        / sizeof(device[0]);
9522
#ifdef SCSI_NCR_NVRAM_SUPPORT
9523
        k = 0;
9524
        if (driver_setup.use_nvram & 0x1)
9525
                nvrams  = sizeof(nvram) / sizeof(nvram[0]);
9526
        else
9527
                nvrams  = 0;
9528
#endif
9529
 
9530
        for (j = 0; j < chips ; ++j) {
9531
                i = driver_setup.reverse_probe ? chips-1 - j : j;
9532
                for (index = 0; ; index++) {
9533
                        char *msg = "";
9534
                        if ((pcibios_find_device(PCI_VENDOR_ID_NCR, ncr_chip_ids[i],
9535
                                                index, &bus, &device_fn)) ||
9536
                            (count == hosts))
9537
                                break;
9538
#ifdef SCSI_NCR_NVRAM_SUPPORT
9539
                        device[count].nvram = k < nvrams ? &nvram[k] : 0;
9540
#else
9541
                        device[count].nvram = 0;
9542
#endif
9543
                        if (ncr53c8xx_pci_init(tpnt, bus, device_fn, &device[count])) {
9544
                                device[count].nvram = 0;
9545
                                continue;
9546
                        }
9547
#ifdef SCSI_NCR_NVRAM_SUPPORT
9548
                        if (device[count].nvram) {
9549
                                ++k;
9550
                                nvram_index |= device[count].nvram->type;
9551
                                switch (device[count].nvram->type) {
9552
                                case SCSI_NCR_TEKRAM_NVRAM:
9553
                                        msg = "with Tekram NVRAM";
9554
                                        break;
9555
                                case SCSI_NCR_SYMBIOS_NVRAM:
9556
                                        msg = "with Symbios NVRAM";
9557
                                        break;
9558
                                default:
9559
                                        msg = "";
9560
                                        device[count].nvram = 0;
9561
                                        --k;
9562
                                }
9563
                        }
9564
#endif
9565
                        printk(KERN_INFO "ncr53c8xx: 53c%s detected %s\n",
9566
                                device[count].chip.name, msg);
9567
                        ++count;
9568
                }
9569
        }
9570
#ifdef SCSI_NCR_NVRAM_SUPPORT
9571
        attach_count = ncr_attach_using_nvram(tpnt, nvram_index, count, device);
9572
#endif
9573
        /*
9574
        ** rescan device list to make sure all boards attached.
9575
        ** devices without boot records will not be attached yet
9576
        ** so try to attach them here.
9577
        */
9578
        for (i= 0; i < count; i++) {
9579
                if (!device[i].attach_done &&
9580
                    !ncr_attach (tpnt, attach_count, &device[i])) {
9581
                        attach_count++;
9582
                }
9583
        }
9584
 
9585
        return attach_count;
9586
}
9587
 
9588
/*
9589
**   Read and check the PCI configuration for any detected NCR
9590
**   boards and save data for attaching after all boards have
9591
**   been detected.
9592
*/
9593
 
9594
__initfunc(
9595
static int ncr53c8xx_pci_init(Scsi_Host_Template *tpnt,
9596
                              uchar bus, uchar device_fn, ncr_device *device)
9597
)
9598
{
9599
        ushort vendor_id, device_id, command;
9600
        uchar cache_line_size, latency_timer;
9601
        uchar revision;
9602
#if LINUX_VERSION_CODE > LinuxVersionCode(2,1,92)
9603
        struct pci_dev *pdev;
9604
        ulong base, base_2, io_port;
9605
        uint irq;
9606
#else
9607
        uchar irq;
9608
        uint base, base_2, io_port;
9609
#endif
9610
        int i;
9611
 
9612
#ifdef SCSI_NCR_NVRAM_SUPPORT
9613
        ncr_nvram *nvram = device->nvram;
9614
#endif
9615
        ncr_chip *chip;
9616
 
9617
        /*
9618
         * Read info from the PCI config space.
9619
         * pcibios_read_config_xxx() functions are assumed to be used for
9620
         * successfully detected PCI devices.
9621
         * Expecting error conditions from them is just paranoia,
9622
         * thus void cast.
9623
         */
9624
        (void) pcibios_read_config_word(bus, device_fn,
9625
                                        PCI_VENDOR_ID, &vendor_id);
9626
        (void) pcibios_read_config_word(bus, device_fn,
9627
                                        PCI_DEVICE_ID, &device_id);
9628
        (void) pcibios_read_config_word(bus, device_fn,
9629
                                        PCI_COMMAND, &command);
9630
#if LINUX_VERSION_CODE > LinuxVersionCode(2,1,92)
9631
        pdev = pci_find_slot(bus, device_fn);
9632
        io_port = pdev->base_address[0];
9633
        base = pdev->base_address[1];
9634
        base_2 = pdev->base_address[2];
9635
        irq = pdev->irq;
9636
#else
9637
        (void) pcibios_read_config_dword(bus, device_fn,
9638
                                        PCI_BASE_ADDRESS_0, &io_port);
9639
        (void) pcibios_read_config_dword(bus, device_fn,
9640
                                        PCI_BASE_ADDRESS_1, &base);
9641
        (void) pcibios_read_config_dword(bus, device_fn,
9642
                                        PCI_BASE_ADDRESS_2, &base_2);
9643
 
9644
        /* Handle 64bit base adresses for 53C896. */
9645
        if ((base & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64)
9646
                (void) pcibios_read_config_dword(bus, device_fn,
9647
                                                 PCI_BASE_ADDRESS_3, &base_2);
9648
        (void) pcibios_read_config_byte(bus, device_fn,
9649
                                        PCI_INTERRUPT_LINE, &irq);
9650
#endif
9651
        (void) pcibios_read_config_byte(bus, device_fn,
9652
                                        PCI_CLASS_REVISION,&revision);
9653
        (void) pcibios_read_config_byte(bus, device_fn,
9654
                                        PCI_CACHE_LINE_SIZE, &cache_line_size);
9655
        (void) pcibios_read_config_byte(bus, device_fn,
9656
                                        PCI_LATENCY_TIMER, &latency_timer);
9657
 
9658
        /*
9659
         *      Check if the chip is supported
9660
         */
9661
        chip = 0;
9662
        for (i = 0; i < sizeof(ncr_chip_table)/sizeof(ncr_chip_table[0]); i++) {
9663
                if (device_id != ncr_chip_table[i].device_id)
9664
                        continue;
9665
                if (revision > ncr_chip_table[i].revision_id)
9666
                        continue;
9667
                chip = &device->chip;
9668
                memcpy(chip, &ncr_chip_table[i], sizeof(*chip));
9669
                chip->revision_id = revision;
9670
                break;
9671
        }
9672
 
9673
#if defined(__i386__)
9674
        /*
9675
         *      Ignore Symbios chips controlled by SISL RAID controller.
9676
         */
9677
        if (chip && (base_2 & PCI_BASE_ADDRESS_MEM_MASK)) {
9678
                unsigned int ScriptsSize, MagicValue;
9679
                vm_offset_t ScriptsRAM;
9680
 
9681
                if (chip->features & FE_RAM8K)
9682
                        ScriptsSize = 8192;
9683
                else
9684
                        ScriptsSize = 4096;
9685
 
9686
                ScriptsRAM = remap_pci_mem(base_2 & PCI_BASE_ADDRESS_MEM_MASK,
9687
                                           ScriptsSize);
9688
                if (ScriptsRAM) {
9689
                        MagicValue = readl(ScriptsRAM + ScriptsSize - 16);
9690
                        unmap_pci_mem(ScriptsRAM, ScriptsSize);
9691
                        if (MagicValue == 0x52414944)
9692
                                return -1;
9693
                }
9694
        }
9695
#endif
9696
 
9697
        printk(KERN_INFO "ncr53c8xx: at PCI bus %d, device %d, function %d\n",
9698
                bus, (int) (device_fn & 0xf8) >> 3, (int) device_fn & 7);
9699
 
9700
        if (!chip) {
9701
                printk("ncr53c8xx: not initializing, device not supported\n");
9702
                return -1;
9703
        }
9704
 
9705
#ifdef __powerpc__
9706
        /*
9707
         *      Several fix-up for power/pc.
9708
         *      Should not be performed by the driver.
9709
         */
9710
        if (!(command & PCI_COMMAND_MASTER)) {
9711
                printk("ncr53c8xx: attempting to force PCI_COMMAND_MASTER...");
9712
                command |= PCI_COMMAND_MASTER;
9713
                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9714
                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9715
                if (!(command & PCI_COMMAND_MASTER)) {
9716
                        printk("failed!\n");
9717
                } else {
9718
                        printk("succeeded.\n");
9719
                }
9720
        }
9721
 
9722
        if (!(command & PCI_COMMAND_IO)) {
9723
                printk("ncr53c8xx: attempting to force PCI_COMMAND_IO...");
9724
                command |= PCI_COMMAND_IO;
9725
                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9726
                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9727
                if (!(command & PCI_COMMAND_IO)) {
9728
                        printk("failed!\n");
9729
                } else {
9730
                        printk("succeeded.\n");
9731
                }
9732
        }
9733
 
9734
        if (!(command & PCI_COMMAND_MEMORY)) {
9735
                printk("ncr53c8xx: attempting to force PCI_COMMAND_MEMORY...");
9736
                command |= PCI_COMMAND_MEMORY;
9737
                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9738
                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9739
                if (!(command & PCI_COMMAND_MEMORY)) {
9740
                        printk("failed!\n");
9741
                } else {
9742
                        printk("succeeded.\n");
9743
                }
9744
        }
9745
 
9746
        if ( is_prep ) {
9747
                if (io_port >= 0x10000000) {
9748
                        printk("ncr53c8xx: reallocating io_port (Wacky IBM)");
9749
                        io_port = (io_port & 0x00FFFFFF) | 0x01000000;
9750
                        pcibios_write_config_dword(bus, device_fn, PCI_BASE_ADDRESS_0, io_port);
9751
                }
9752
                if (base >= 0x10000000) {
9753
                        printk("ncr53c8xx: reallocating base (Wacky IBM)");
9754
                        base = (base & 0x00FFFFFF) | 0x01000000;
9755
                        pcibios_write_config_dword(bus, device_fn, PCI_BASE_ADDRESS_1, base);
9756
                }
9757
                if (base_2 >= 0x10000000) {
9758
                        printk("ncr53c8xx: reallocating base2 (Wacky IBM)");
9759
                        base_2 = (base_2 & 0x00FFFFFF) | 0x01000000;
9760
                        pcibios_write_config_dword(bus, device_fn, PCI_BASE_ADDRESS_2, base_2);
9761
                }
9762
        }
9763
#endif  /* __powerpc__ */
9764
 
9765
#ifdef __sparc__
9766
        /*
9767
         *      Severall fix-ups for sparc.
9768
         *
9769
         *      Should not be performed by the driver, but how can OBP know
9770
         *      each and every PCI card, if they don't use Fcode?
9771
         */
9772
 
9773
        base = __pa(base);
9774
        base_2 = __pa(base_2);
9775
 
9776
        if (!(command & PCI_COMMAND_MASTER)) {
9777
                if (initverbose >= 2)
9778
                        printk("ncr53c8xx: setting PCI_COMMAND_MASTER bit (fixup)\n");
9779
                command |= PCI_COMMAND_MASTER;
9780
                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9781
                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9782
        }
9783
 
9784
        if ((chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
9785
                if (initverbose >= 2)
9786
                        printk("ncr53c8xx: setting PCI_COMMAND_INVALIDATE bit (fixup)\n");
9787
                command |= PCI_COMMAND_INVALIDATE;
9788
                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
9789
                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
9790
        }
9791
 
9792
        if ((chip->features & FE_CLSE) && !cache_line_size) {
9793
                cache_line_size = CACHE_LINE_SIZE;
9794
                if (initverbose >= 2)
9795
                        printk("ncr53c8xx: setting PCI_CACHE_LINE_SIZE to %d (fixup)\n", cache_line_size);
9796
                pcibios_write_config_byte(bus, device_fn,
9797
                                          PCI_CACHE_LINE_SIZE, cache_line_size);
9798
                pcibios_read_config_byte(bus, device_fn,
9799
                                         PCI_CACHE_LINE_SIZE, &cache_line_size);
9800
        }
9801
 
9802
        if (!latency_timer) {
9803
                latency_timer = 248;
9804
                if (initverbose >= 2)
9805
                        printk("ncr53c8xx: setting PCI_LATENCY_TIMER to %d bus clocks (fixup)\n", latency_timer);
9806
                pcibios_write_config_byte(bus, device_fn,
9807
                                          PCI_LATENCY_TIMER, latency_timer);
9808
                pcibios_read_config_byte(bus, device_fn,
9809
                                         PCI_LATENCY_TIMER, &latency_timer);
9810
        }
9811
#endif  /* __sparc__ */
9812
 
9813
        /*
9814
         * Check availability of IO space, memory space and master capability.
9815
         */
9816
        if (command & PCI_COMMAND_IO) {
9817
                if ((io_port & 3) != 1) {
9818
                        printk("ncr53c8xx: disabling I/O mapping since base address 0 (0x%x)\n"
9819
                                "           bits 0..1 indicate a non-IO mapping\n", (int) io_port);
9820
                        io_port = 0;
9821
                }
9822
                else
9823
                        io_port &= PCI_BASE_ADDRESS_IO_MASK;
9824
        }
9825
        else
9826
                io_port = 0;
9827
 
9828
        if (command & PCI_COMMAND_MEMORY) {
9829
                if ((base & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
9830
                        printk("ncr53c8xx: disabling memory mapping since base address 1\n"
9831
                                "            contains a non-memory mapping\n");
9832
                        base = 0;
9833
                }
9834
                else
9835
                        base &= PCI_BASE_ADDRESS_MEM_MASK;
9836
        }
9837
        else
9838
                base = 0;
9839
 
9840
        if (!io_port && !base) {
9841
                printk("ncr53c8xx: not initializing, both I/O and memory mappings disabled\n");
9842
                return -1;
9843
        }
9844
 
9845
        base_2 &= PCI_BASE_ADDRESS_MEM_MASK;
9846
 
9847
        if (io_port && check_region (io_port, 128)) {
9848
#ifdef __sparc__
9849
                printk("ncr53c8xx: IO region 0x%lx to 0x%lx is in use\n",
9850
                        io_port, (io_port + 127));
9851
#else
9852
                printk("ncr53c8xx: IO region 0x%x to 0x%x is in use\n",
9853
                        (int) io_port, (int) (io_port + 127));
9854
#endif
9855
                return -1;
9856
        }
9857
 
9858
        if (!(command & PCI_COMMAND_MASTER)) {
9859
                printk("ncr53c8xx: not initializing, BUS MASTERING was disabled\n");
9860
                return -1;
9861
        }
9862
 
9863
        /*
9864
         * Fix some features according to driver setup.
9865
         */
9866
        if (!(driver_setup.special_features & 1))
9867
                chip->features &= ~FE_SPECIAL_SET;
9868
        else {
9869
                if (driver_setup.special_features & 2)
9870
                        chip->features &= ~FE_WRIE;
9871
        }
9872
        if (driver_setup.ultra_scsi < 2 && (chip->features & FE_ULTRA2)) {
9873
                chip->features |=  FE_ULTRA;
9874
                chip->features &= ~FE_ULTRA2;
9875
        }
9876
        if (driver_setup.ultra_scsi < 1)
9877
                chip->features &= ~FE_ULTRA;
9878
        if (!driver_setup.max_wide)
9879
                chip->features &= ~FE_WIDE;
9880
 
9881
 
9882
#ifdef  SCSI_NCR_PCI_FIX_UP_SUPPORT
9883
 
9884
        /*
9885
         * Try to fix up PCI config according to wished features.
9886
         */
9887
#if defined(__i386__) && !defined(MODULE)
9888
        if ((driver_setup.pci_fix_up & 1) &&
9889
            (chip->features & FE_CLSE) && cache_line_size == 0) {
9890
#if LINUX_VERSION_CODE < LinuxVersionCode(2,1,75)
9891
                extern char x86;
9892
                switch(x86) {
9893
#else
9894
                switch(boot_cpu_data.x86) {
9895
#endif
9896
                case 4: cache_line_size = 4; break;
9897
                case 6:
9898
                case 5: cache_line_size = 8; break;
9899
                }
9900
                if (cache_line_size)
9901
                        (void) pcibios_write_config_byte(bus, device_fn,
9902
                                        PCI_CACHE_LINE_SIZE, cache_line_size);
9903
                if (initverbose)
9904
                        printk("ncr53c8xx: setting PCI_CACHE_LINE_SIZE to %d (fix-up).\n", cache_line_size);
9905
        }
9906
 
9907
        if ((driver_setup.pci_fix_up & 2) && cache_line_size &&
9908
            (chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
9909
                command |= PCI_COMMAND_INVALIDATE;
9910
                (void) pcibios_write_config_word(bus, device_fn,
9911
                                                PCI_COMMAND, command);
9912
                if (initverbose)
9913
                        printk("ncr53c8xx: setting PCI_COMMAND_INVALIDATE bit (fix-up).\n");
9914
        }
9915
#endif
9916
        /*
9917
         * Fix up for old chips that support READ LINE but not CACHE LINE SIZE.
9918
         * - If CACHE LINE SIZE is unknown, set burst max to 32 bytes = 8 dwords
9919
         *   and donnot enable READ LINE.
9920
         * - Otherwise set it to the CACHE LINE SIZE (power of 2 assumed).
9921
         */
9922
 
9923
        if (!(chip->features & FE_CLSE)) {
9924
                int burst_max = chip->burst_max;
9925
                if (cache_line_size == 0) {
9926
                        chip->features  &= ~FE_ERL;
9927
                        if (burst_max > 3)
9928
                                burst_max = 3;
9929
                }
9930
                else {
9931
                        while (cache_line_size < (1 << burst_max))
9932
                                --burst_max;
9933
                }
9934
                chip->burst_max = burst_max;
9935
        }
9936
 
9937
        /*
9938
         * Tune PCI LATENCY TIMER according to burst max length transfer.
9939
         * (latency timer >= burst length + 6, we add 10 to be quite sure)
9940
         * If current value is zero, the device has probably been configured
9941
         * for no bursting due to some broken hardware.
9942
         */
9943
 
9944
        if (latency_timer == 0 && chip->burst_max)
9945
                printk("ncr53c8xx: PCI_LATENCY_TIMER=0, bursting should'nt be allowed.\n");
9946
 
9947
        if ((driver_setup.pci_fix_up & 4) && chip->burst_max) {
9948
                uchar lt = (1 << chip->burst_max) + 6 + 10;
9949
                if (latency_timer < lt) {
9950
                        latency_timer = lt;
9951
                        if (initverbose)
9952
                                printk("ncr53c8xx: setting PCI_LATENCY_TIMER to %d bus clocks (fix-up).\n", latency_timer);
9953
                         (void) pcibios_write_config_byte(bus, device_fn,
9954
                                        PCI_LATENCY_TIMER, latency_timer);
9955
                }
9956
        }
9957
 
9958
        /*
9959
         * Fix up for recent chips that support CACHE LINE SIZE.
9960
         * If PCI config space is not OK, remove features that shall not be
9961
         * used by the chip. No need to trigger possible chip bugs.
9962
         */
9963
 
9964
        if ((chip->features & FE_CLSE) && cache_line_size == 0) {
9965
                chip->features &= ~FE_CACHE_SET;
9966
                printk("ncr53c8xx: PCI_CACHE_LINE_SIZE not set, features based on CACHE LINE SIZE not used.\n");
9967
        }
9968
 
9969
        if ((chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
9970
                chip->features &= ~FE_WRIE;
9971
                printk("ncr53c8xx: PCI_COMMAND_INVALIDATE not set, WRITE AND INVALIDATE not used\n");
9972
        }
9973
 
9974
#endif  /* SCSI_NCR_PCI_FIX_UP_SUPPORT */
9975
 
9976
        /* initialise ncr_device structure with items required by ncr_attach */
9977
        device->slot.bus        = bus;
9978
        device->slot.device_fn  = device_fn;
9979
        device->slot.base       = base;
9980
        device->slot.base_2     = base_2;
9981
        device->slot.io_port    = io_port;
9982
        device->slot.irq        = irq;
9983
        device->attach_done     = 0;
9984
#ifdef SCSI_NCR_NVRAM_SUPPORT
9985
        if (!nvram)
9986
                goto out;
9987
 
9988
        /*
9989
        ** Get access to chip IO registers
9990
        */
9991
#ifdef NCR_IOMAPPED
9992
        request_region(io_port, 128, "ncr53c8xx");
9993
        device->slot.port = io_port;
9994
#else
9995
        device->slot.reg = (struct ncr_reg *) remap_pci_mem((ulong) base, 128);
9996
        if (!device->slot.reg)
9997
                goto out;
9998
#endif
9999
 
10000
        /*
10001
        ** Try to read SYMBIOS nvram.
10002
        ** Data can be used to order booting of boards.
10003
        **
10004
        ** Data is saved in ncr_device structure if NVRAM found. This
10005
        ** is then used to find drive boot order for ncr_attach().
10006
        **
10007
        ** NVRAM data is passed to Scsi_Host_Template later during ncr_attach()
10008
        ** for any device set up.
10009
        **
10010
        ** Try to read TEKRAM nvram if Symbios nvram not found.
10011
        */
10012
 
10013
        if      (!ncr_get_Symbios_nvram(&device->slot, &nvram->data.Symbios))
10014
                nvram->type = SCSI_NCR_SYMBIOS_NVRAM;
10015
        else if (!ncr_get_Tekram_nvram(&device->slot, &nvram->data.Tekram))
10016
                nvram->type = SCSI_NCR_TEKRAM_NVRAM;
10017
        else
10018
                nvram->type = 0;
10019
out:
10020
        /*
10021
        ** Release access to chip IO registers
10022
        */
10023
#ifdef NCR_IOMAPPED
10024
        release_region(device->slot.port, 128);
10025
#else
10026
        unmap_pci_mem((vm_offset_t) device->slot.reg, (u_long) 128);
10027
#endif
10028
 
10029
#endif  /* SCSI_NCR_NVRAM_SUPPORT */
10030
        return 0;
10031
}
10032
 
10033
/*
10034
**   Linux select queue depths function
10035
*/
10036
 
10037
#define DEF_DEPTH       (driver_setup.default_tags)
10038
#define ALL_TARGETS     -2
10039
#define NO_TARGET       -1
10040
#define ALL_LUNS        -2
10041
#define NO_LUN          -1
10042
 
10043
static int device_queue_depth(ncb_p np, int target, int lun)
10044
{
10045
        int c, h, t, u, v;
10046
        char *p = driver_setup.tag_ctrl;
10047
        char *ep;
10048
 
10049
        h = -1;
10050
        t = NO_TARGET;
10051
        u = NO_LUN;
10052
        while ((c = *p++) != 0) {
10053
                v = simple_strtoul(p, &ep, 0);
10054
                switch(c) {
10055
                case '/':
10056
                        ++h;
10057
                        t = ALL_TARGETS;
10058
                        u = ALL_LUNS;
10059
                        break;
10060
                case 't':
10061
                        if (t != target)
10062
                                t = (target == v) ? v : NO_TARGET;
10063
                        u = ALL_LUNS;
10064
                        break;
10065
                case 'u':
10066
                        if (u != lun)
10067
                                u = (lun == v) ? v : NO_LUN;
10068
                        break;
10069
                case 'q':
10070
                        if (h == np->unit &&
10071
                                (t == ALL_TARGETS || t == target) &&
10072
                                (u == ALL_LUNS    || u == lun))
10073
                                return v;
10074
                        break;
10075
                case '-':
10076
                        t = ALL_TARGETS;
10077
                        u = ALL_LUNS;
10078
                        break;
10079
                default:
10080
                        break;
10081
                }
10082
                p = ep;
10083
        }
10084
        return DEF_DEPTH;
10085
}
10086
 
10087
static void ncr53c8xx_select_queue_depths(struct Scsi_Host *host, struct scsi_device *devlist)
10088
{
10089
        struct scsi_device *device;
10090
 
10091
        for (device = devlist; device; device = device->next) {
10092
                ncb_p np;
10093
                tcb_p tp;
10094
                lcb_p lp;
10095
                int numtags;
10096
 
10097
                if (device->host != host)
10098
                        continue;
10099
 
10100
                np = ((struct host_data *) host->hostdata)->ncb;
10101
                tp = &np->target[device->id];
10102
                lp = tp->lp[device->lun];
10103
 
10104
                /*
10105
                **      Select queue depth from driver setup.
10106
                **      Donnot use more than configured by user.
10107
                **      Use at least 2.
10108
                **      Donnot use more than our maximum.
10109
                */
10110
                numtags = device_queue_depth(np, device->id, device->lun);
10111
                if (numtags > tp->usrtags)
10112
                        numtags = tp->usrtags;
10113
                if (!device->tagged_supported)
10114
                        numtags = 1;
10115
                device->queue_depth = numtags;
10116
                if (device->queue_depth < 2)
10117
                        device->queue_depth = 2;
10118
                if (device->queue_depth > SCSI_NCR_MAX_TAGS)
10119
                        device->queue_depth = SCSI_NCR_MAX_TAGS;
10120
 
10121
                /*
10122
                **      Since the queue depth is not tunable under Linux,
10123
                **      we need to know this value in order not to
10124
                **      announce stupid things to user.
10125
                */
10126
                if (lp) {
10127
                        lp->numtags = lp->maxtags = numtags;
10128
                        lp->scdev_depth = device->queue_depth;
10129
                }
10130
                ncr_setup_tags (np, device->id, device->lun);
10131
 
10132
#ifdef DEBUG_NCR53C8XX
10133
printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
10134
        np->unit, device->id, device->lun, device->queue_depth);
10135
#endif
10136
        }
10137
}
10138
 
10139
/*
10140
**   Linux entry point of queuecommand() function
10141
*/
10142
 
10143
int ncr53c8xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
10144
{
10145
     ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
10146
     unsigned long flags;
10147
     int sts;
10148
 
10149
#ifdef DEBUG_NCR53C8XX
10150
printk("ncr53c8xx_queue_command\n");
10151
#endif
10152
 
10153
     cmd->scsi_done     = done;
10154
     cmd->host_scribble = NULL;
10155
     cmd->SCp.ptr       = NULL;
10156
     cmd->SCp.buffer    = NULL;
10157
 
10158
     NCR_LOCK_NCB(np, flags);
10159
 
10160
     if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
10161
          cmd->result = ScsiResult(sts, 0);
10162
#ifdef DEBUG_NCR53C8XX
10163
printk("ncr53c8xx : command not queued - result=%d\n", sts);
10164
#endif
10165
     }
10166
#ifdef DEBUG_NCR53C8XX
10167
     else
10168
printk("ncr53c8xx : command successfully queued\n");
10169
#endif
10170
 
10171
     NCR_UNLOCK_NCB(np, flags);
10172
 
10173
     if (sts != DID_OK)
10174
          done(cmd);
10175
 
10176
     return sts;
10177
}
10178
 
10179
/*
10180
**   Linux entry point of the interrupt handler.
10181
**   Since linux versions > 1.3.70, we trust the kernel for
10182
**   passing the internal host descriptor as 'dev_id'.
10183
**   Otherwise, we scan the host list and call the interrupt
10184
**   routine for each host that uses this IRQ.
10185
*/
10186
 
10187
static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
10188
{
10189
     unsigned long flags;
10190
     ncb_p np = (ncb_p) dev_id;
10191
     Scsi_Cmnd *done_list;
10192
 
10193
#ifdef DEBUG_NCR53C8XX
10194
     printk("ncr53c8xx : interrupt received\n");
10195
#endif
10196
 
10197
     if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
10198
 
10199
     NCR_LOCK_NCB(np, flags);
10200
     ncr_exception(np);
10201
     done_list     = np->done_list;
10202
     np->done_list = 0;
10203
     NCR_UNLOCK_NCB(np, flags);
10204
 
10205
     if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
10206
 
10207
     if (done_list) {
10208
          NCR_LOCK_SCSI_DONE(np, flags);
10209
          ncr_flush_done_cmds(done_list);
10210
          NCR_UNLOCK_SCSI_DONE(np, flags);
10211
     }
10212
}
10213
 
10214
/*
10215
**   Linux entry point of the timer handler
10216
*/
10217
 
10218
static void ncr53c8xx_timeout(unsigned long npref)
10219
{
10220
     ncb_p np = (ncb_p) npref;
10221
     unsigned long flags;
10222
     Scsi_Cmnd *done_list;
10223
 
10224
     NCR_LOCK_NCB(np, flags);
10225
     ncr_timeout((ncb_p) np);
10226
     done_list     = np->done_list;
10227
     np->done_list = 0;
10228
     NCR_UNLOCK_NCB(np, flags);
10229
 
10230
     if (done_list) {
10231
          NCR_LOCK_SCSI_DONE(np, flags);
10232
          ncr_flush_done_cmds(done_list);
10233
          NCR_UNLOCK_SCSI_DONE(np, flags);
10234
     }
10235
}
10236
 
10237
/*
10238
**   Linux entry point of reset() function
10239
*/
10240
 
10241
#if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10242
int ncr53c8xx_reset(Scsi_Cmnd *cmd, unsigned int reset_flags)
10243
#else
10244
int ncr53c8xx_reset(Scsi_Cmnd *cmd)
10245
#endif
10246
{
10247
        ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
10248
        int sts;
10249
        unsigned long flags;
10250
        Scsi_Cmnd *done_list;
10251
 
10252
#if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10253
        printk("ncr53c8xx_reset: pid=%lu reset_flags=%x serial_number=%ld serial_number_at_timeout=%ld\n",
10254
                cmd->pid, reset_flags, cmd->serial_number, cmd->serial_number_at_timeout);
10255
#else
10256
        printk("ncr53c8xx_reset: command pid %lu\n", cmd->pid);
10257
#endif
10258
 
10259
        NCR_LOCK_NCB(np, flags);
10260
 
10261
        /*
10262
         * We have to just ignore reset requests in some situations.
10263
         */
10264
#if defined SCSI_RESET_NOT_RUNNING
10265
        if (cmd->serial_number != cmd->serial_number_at_timeout) {
10266
                sts = SCSI_RESET_NOT_RUNNING;
10267
                goto out;
10268
        }
10269
#endif
10270
        /*
10271
         * If the mid-level driver told us reset is synchronous, it seems
10272
         * that we must call the done() callback for the involved command,
10273
         * even if this command was not queued to the low-level driver,
10274
         * before returning SCSI_RESET_SUCCESS.
10275
         */
10276
 
10277
#if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10278
        sts = ncr_reset_bus(np, cmd,
10279
        (reset_flags & (SCSI_RESET_SYNCHRONOUS | SCSI_RESET_ASYNCHRONOUS)) == SCSI_RESET_SYNCHRONOUS);
10280
#else
10281
        sts = ncr_reset_bus(np, cmd, 0);
10282
#endif
10283
 
10284
        /*
10285
         * Since we always reset the controller, when we return success,
10286
         * we add this information to the return code.
10287
         */
10288
#if defined SCSI_RESET_HOST_RESET
10289
        if (sts == SCSI_RESET_SUCCESS)
10290
                sts |= SCSI_RESET_HOST_RESET;
10291
#endif
10292
 
10293
out:
10294
        done_list     = np->done_list;
10295
        np->done_list = 0;
10296
        NCR_UNLOCK_NCB(np, flags);
10297
 
10298
        ncr_flush_done_cmds(done_list);
10299
 
10300
        return sts;
10301
}
10302
 
10303
/*
10304
**   Linux entry point of abort() function
10305
*/
10306
 
10307
int ncr53c8xx_abort(Scsi_Cmnd *cmd)
10308
{
10309
        ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
10310
        int sts;
10311
        unsigned long flags;
10312
        Scsi_Cmnd *done_list;
10313
 
10314
#if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10315
        printk("ncr53c8xx_abort: pid=%lu serial_number=%ld serial_number_at_timeout=%ld\n",
10316
                cmd->pid, cmd->serial_number, cmd->serial_number_at_timeout);
10317
#else
10318
        printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
10319
#endif
10320
 
10321
        NCR_LOCK_NCB(np, flags);
10322
 
10323
#if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
10324
        /*
10325
         * We have to just ignore abort requests in some situations.
10326
         */
10327
        if (cmd->serial_number != cmd->serial_number_at_timeout) {
10328
                sts = SCSI_ABORT_NOT_RUNNING;
10329
                goto out;
10330
        }
10331
#endif
10332
 
10333
        sts = ncr_abort_command(np, cmd);
10334
out:
10335
        done_list     = np->done_list;
10336
        np->done_list = 0;
10337
        NCR_UNLOCK_NCB(np, flags);
10338
 
10339
        ncr_flush_done_cmds(done_list);
10340
 
10341
        return sts;
10342
}
10343
 
10344
 
10345
#ifdef MODULE
10346
int ncr53c8xx_release(struct Scsi_Host *host)
10347
{
10348
#ifdef DEBUG_NCR53C8XX
10349
printk("ncr53c8xx : release\n");
10350
#endif
10351
     ncr_detach(((struct host_data *) host->hostdata)->ncb);
10352
 
10353
     return 1;
10354
}
10355
#endif
10356
 
10357
 
10358
/*
10359
**      Scsi command waiting list management.
10360
**
10361
**      It may happen that we cannot insert a scsi command into the start queue,
10362
**      in the following circumstances.
10363
**              Too few preallocated ccb(s),
10364
**              maxtags < cmd_per_lun of the Linux host control block,
10365
**              etc...
10366
**      Such scsi commands are inserted into a waiting list.
10367
**      When a scsi command complete, we try to requeue the commands of the
10368
**      waiting list.
10369
*/
10370
 
10371
#define next_wcmd host_scribble
10372
 
10373
static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd)
10374
{
10375
        Scsi_Cmnd *wcmd;
10376
 
10377
#ifdef DEBUG_WAITING_LIST
10378
        printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
10379
#endif
10380
        cmd->next_wcmd = 0;
10381
        if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
10382
        else {
10383
                while ((wcmd->next_wcmd) != 0)
10384
                        wcmd = (Scsi_Cmnd *) wcmd->next_wcmd;
10385
                wcmd->next_wcmd = (char *) cmd;
10386
        }
10387
}
10388
 
10389
static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd)
10390
{
10391
        Scsi_Cmnd *wcmd;
10392
 
10393
        if (!(wcmd = np->waiting_list)) return 0;
10394
        while (wcmd->next_wcmd) {
10395
                if (cmd == (Scsi_Cmnd *) wcmd->next_wcmd) {
10396
                        if (to_remove) {
10397
                                wcmd->next_wcmd = cmd->next_wcmd;
10398
                                cmd->next_wcmd = 0;
10399
                        }
10400
#ifdef DEBUG_WAITING_LIST
10401
        printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
10402
#endif
10403
                        return cmd;
10404
                }
10405
        }
10406
        return 0;
10407
}
10408
 
10409
static void process_waiting_list(ncb_p np, int sts)
10410
{
10411
        Scsi_Cmnd *waiting_list, *wcmd;
10412
 
10413
        waiting_list = np->waiting_list;
10414
        np->waiting_list = 0;
10415
 
10416
#ifdef DEBUG_WAITING_LIST
10417
        if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
10418
#endif
10419
        while ((wcmd = waiting_list) != 0) {
10420
                waiting_list = (Scsi_Cmnd *) wcmd->next_wcmd;
10421
                wcmd->next_wcmd = 0;
10422
                if (sts == DID_OK) {
10423
#ifdef DEBUG_WAITING_LIST
10424
        printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
10425
#endif
10426
                        sts = ncr_queue_command(np, wcmd);
10427
                }
10428
                if (sts != DID_OK) {
10429
#ifdef DEBUG_WAITING_LIST
10430
        printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
10431
#endif
10432
                        wcmd->result = ScsiResult(sts, 0);
10433
                        ncr_queue_done_cmd(np, wcmd);
10434
                }
10435
        }
10436
}
10437
 
10438
#undef next_wcmd
10439
 
10440
#ifdef SCSI_NCR_PROC_INFO_SUPPORT
10441
 
10442
/*=========================================================================
10443
**      Proc file system stuff
10444
**
10445
**      A read operation returns profile information.
10446
**      A write operation is a control command.
10447
**      The string is parsed in the driver code and the command is passed
10448
**      to the ncr_usercmd() function.
10449
**=========================================================================
10450
*/
10451
 
10452
#ifdef SCSI_NCR_USER_COMMAND_SUPPORT
10453
 
10454
#define is_digit(c)     ((c) >= '0' && (c) <= '9')
10455
#define digit_to_bin(c) ((c) - '0')
10456
#define is_space(c)     ((c) == ' ' || (c) == '\t')
10457
 
10458
static int skip_spaces(char *ptr, int len)
10459
{
10460
        int cnt, c;
10461
 
10462
        for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
10463
 
10464
        return (len - cnt);
10465
}
10466
 
10467
static int get_int_arg(char *ptr, int len, u_long *pv)
10468
{
10469
        int     cnt, c;
10470
        u_long  v;
10471
 
10472
        for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
10473
                v = (v * 10) + digit_to_bin(c);
10474
        }
10475
 
10476
        if (pv)
10477
                *pv = v;
10478
 
10479
        return (len - cnt);
10480
}
10481
 
10482
static int is_keyword(char *ptr, int len, char *verb)
10483
{
10484
        int verb_len = strlen(verb);
10485
 
10486
        if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
10487
                return verb_len;
10488
        else
10489
                return 0;
10490
 
10491
}
10492
 
10493
#define SKIP_SPACES(min_spaces)                                         \
10494
        if ((arg_len = skip_spaces(ptr, len)) < (min_spaces))           \
10495
                return -EINVAL;                                         \
10496
        ptr += arg_len; len -= arg_len;
10497
 
10498
#define GET_INT_ARG(v)                                                  \
10499
        if (!(arg_len = get_int_arg(ptr, len, &(v))))                   \
10500
                return -EINVAL;                                         \
10501
        ptr += arg_len; len -= arg_len;
10502
 
10503
 
10504
/*
10505
**      Parse a control command
10506
*/
10507
 
10508
static int ncr_user_command(ncb_p np, char *buffer, int length)
10509
{
10510
        char *ptr       = buffer;
10511
        int len         = length;
10512
        struct usrcmd    *uc = &np->user;
10513
        int             arg_len;
10514
        u_long          target;
10515
 
10516
        bzero(uc, sizeof(*uc));
10517
 
10518
        if (len > 0 && ptr[len-1] == '\n')
10519
                --len;
10520
 
10521
        if      ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
10522
                uc->cmd = UC_SETSYNC;
10523
        else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
10524
                uc->cmd = UC_SETTAGS;
10525
        else if ((arg_len = is_keyword(ptr, len, "setorder")) != 0)
10526
                uc->cmd = UC_SETORDER;
10527
        else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
10528
                uc->cmd = UC_SETVERBOSE;
10529
        else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
10530
                uc->cmd = UC_SETWIDE;
10531
        else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
10532
                uc->cmd = UC_SETDEBUG;
10533
        else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
10534
                uc->cmd = UC_SETFLAG;
10535
        else if ((arg_len = is_keyword(ptr, len, "clearprof")) != 0)
10536
                uc->cmd = UC_CLEARPROF;
10537
        else
10538
                arg_len = 0;
10539
 
10540
#ifdef DEBUG_PROC_INFO
10541
printk("ncr_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
10542
#endif
10543
 
10544
        if (!arg_len)
10545
                return -EINVAL;
10546
        ptr += arg_len; len -= arg_len;
10547
 
10548
        switch(uc->cmd) {
10549
        case UC_SETSYNC:
10550
        case UC_SETTAGS:
10551
        case UC_SETWIDE:
10552
        case UC_SETFLAG:
10553
                SKIP_SPACES(1);
10554
                if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
10555
                        ptr += arg_len; len -= arg_len;
10556
                        uc->target = ~0;
10557
                } else {
10558
                        GET_INT_ARG(target);
10559
                        uc->target = (1<<target);
10560
#ifdef DEBUG_PROC_INFO
10561
printk("ncr_user_command: target=%ld\n", target);
10562
#endif
10563
                }
10564
                break;
10565
        }
10566
 
10567
        switch(uc->cmd) {
10568
        case UC_SETVERBOSE:
10569
        case UC_SETSYNC:
10570
        case UC_SETTAGS:
10571
        case UC_SETWIDE:
10572
                SKIP_SPACES(1);
10573
                GET_INT_ARG(uc->data);
10574
#ifdef DEBUG_PROC_INFO
10575
printk("ncr_user_command: data=%ld\n", uc->data);
10576
#endif
10577
                break;
10578
        case UC_SETORDER:
10579
                SKIP_SPACES(1);
10580
                if      ((arg_len = is_keyword(ptr, len, "simple")))
10581
                        uc->data = M_SIMPLE_TAG;
10582
                else if ((arg_len = is_keyword(ptr, len, "ordered")))
10583
                        uc->data = M_ORDERED_TAG;
10584
                else if ((arg_len = is_keyword(ptr, len, "default")))
10585
                        uc->data = 0;
10586
                else
10587
                        return -EINVAL;
10588
                break;
10589
        case UC_SETDEBUG:
10590
                while (len > 0) {
10591
                        SKIP_SPACES(1);
10592
                        if      ((arg_len = is_keyword(ptr, len, "alloc")))
10593
                                uc->data |= DEBUG_ALLOC;
10594
                        else if ((arg_len = is_keyword(ptr, len, "phase")))
10595
                                uc->data |= DEBUG_PHASE;
10596
                        else if ((arg_len = is_keyword(ptr, len, "poll")))
10597
                                uc->data |= DEBUG_POLL;
10598
                        else if ((arg_len = is_keyword(ptr, len, "queue")))
10599
                                uc->data |= DEBUG_QUEUE;
10600
                        else if ((arg_len = is_keyword(ptr, len, "result")))
10601
                                uc->data |= DEBUG_RESULT;
10602
                        else if ((arg_len = is_keyword(ptr, len, "scatter")))
10603
                                uc->data |= DEBUG_SCATTER;
10604
                        else if ((arg_len = is_keyword(ptr, len, "script")))
10605
                                uc->data |= DEBUG_SCRIPT;
10606
                        else if ((arg_len = is_keyword(ptr, len, "tiny")))
10607
                                uc->data |= DEBUG_TINY;
10608
                        else if ((arg_len = is_keyword(ptr, len, "timing")))
10609
                                uc->data |= DEBUG_TIMING;
10610
                        else if ((arg_len = is_keyword(ptr, len, "nego")))
10611
                                uc->data |= DEBUG_NEGO;
10612
                        else if ((arg_len = is_keyword(ptr, len, "tags")))
10613
                                uc->data |= DEBUG_TAGS;
10614
                        else if ((arg_len = is_keyword(ptr, len, "freeze")))
10615
                                uc->data |= DEBUG_FREEZE;
10616
                        else if ((arg_len = is_keyword(ptr, len, "restart")))
10617
                                uc->data |= DEBUG_RESTART;
10618
                        else
10619
                                return -EINVAL;
10620
                        ptr += arg_len; len -= arg_len;
10621
                }
10622
#ifdef DEBUG_PROC_INFO
10623
printk("ncr_user_command: data=%ld\n", uc->data);
10624
#endif
10625
                break;
10626
        case UC_SETFLAG:
10627
                while (len > 0) {
10628
                        SKIP_SPACES(1);
10629
                        if      ((arg_len = is_keyword(ptr, len, "trace")))
10630
                                uc->data |= UF_TRACE;
10631
                        else if ((arg_len = is_keyword(ptr, len, "no_disc")))
10632
                                uc->data |= UF_NODISC;
10633
                        else
10634
                                return -EINVAL;
10635
                        ptr += arg_len; len -= arg_len;
10636
                }
10637
                break;
10638
        default:
10639
                break;
10640
        }
10641
 
10642
        if (len)
10643
                return -EINVAL;
10644
        else {
10645
                long flags;
10646
 
10647
                NCR_LOCK_NCB(np, flags);
10648
                ncr_usercmd (np);
10649
                NCR_UNLOCK_NCB(np, flags);
10650
        }
10651
        return length;
10652
}
10653
 
10654
#endif  /* SCSI_NCR_USER_COMMAND_SUPPORT */
10655
 
10656
#ifdef SCSI_NCR_USER_INFO_SUPPORT
10657
 
10658
struct info_str
10659
{
10660
        char *buffer;
10661
        int length;
10662
        int offset;
10663
        int pos;
10664
};
10665
 
10666
static void copy_mem_info(struct info_str *info, char *data, int len)
10667
{
10668
        if (info->pos + len > info->length)
10669
                len = info->length - info->pos;
10670
 
10671
        if (info->pos + len < info->offset) {
10672
                info->pos += len;
10673
                return;
10674
        }
10675
        if (info->pos < info->offset) {
10676
                data += (info->offset - info->pos);
10677
                len  -= (info->offset - info->pos);
10678
        }
10679
 
10680
        if (len > 0) {
10681
                memcpy(info->buffer + info->pos, data, len);
10682
                info->pos += len;
10683
        }
10684
}
10685
 
10686
static int copy_info(struct info_str *info, char *fmt, ...)
10687
{
10688
        va_list args;
10689
        char buf[81];
10690
        int len;
10691
 
10692
        va_start(args, fmt);
10693
        len = vsprintf(buf, fmt, args);
10694
        va_end(args);
10695
 
10696
        copy_mem_info(info, buf, len);
10697
        return len;
10698
}
10699
 
10700
/*
10701
**      Copy formatted profile information into the input buffer.
10702
*/
10703
 
10704
#define to_ms(t) ((t) * 1000 / HZ)
10705
 
10706
static int ncr_host_info(ncb_p np, char *ptr, off_t offset, int len)
10707
{
10708
        struct info_str info;
10709
 
10710
        info.buffer     = ptr;
10711
        info.length     = len;
10712
        info.offset     = offset;
10713
        info.pos        = 0;
10714
 
10715
        copy_info(&info, "General information:\n");
10716
        copy_info(&info, "  Chip NCR53C%s, ",   np->chip_name);
10717
        copy_info(&info, "device id 0x%x, ",    np->device_id);
10718
        copy_info(&info, "revision id 0x%x\n",  np->revision_id);
10719
 
10720
        copy_info(&info, "  IO port address 0x%lx, ", (u_long) np->port);
10721
#ifdef __sparc__
10722
        copy_info(&info, "IRQ number %s\n", __irq_itoa(np->irq));
10723
#else
10724
        copy_info(&info, "IRQ number %d\n", (int) np->irq);
10725
#endif
10726
 
10727
#ifndef NCR_IOMAPPED
10728
        if (np->reg)
10729
                copy_info(&info, "  Using memory mapped IO at virtual address 0x%lx\n",
10730
                                  (u_long) np->reg);
10731
#endif
10732
        copy_info(&info, "  Synchronous period factor %d, ", (int) np->minsync);
10733
        copy_info(&info, "max commands per lun %d\n", SCSI_NCR_MAX_TAGS);
10734
 
10735
        if (driver_setup.debug || driver_setup.verbose > 1) {
10736
                copy_info(&info, "  Debug flags 0x%x, ", driver_setup.debug);
10737
                copy_info(&info, "verbosity level %d\n", driver_setup.verbose);
10738
        }
10739
 
10740
#ifdef SCSI_NCR_PROFILE_SUPPORT
10741
        copy_info(&info, "Profiling information:\n");
10742
        copy_info(&info, "  %-12s = %lu\n", "num_trans",np->profile.num_trans);
10743
        copy_info(&info, "  %-12s = %lu\n", "num_kbytes",np->profile.num_kbytes);
10744
        copy_info(&info, "  %-12s = %lu\n", "num_disc", np->profile.num_disc);
10745
        copy_info(&info, "  %-12s = %lu\n", "num_break",np->profile.num_break);
10746
        copy_info(&info, "  %-12s = %lu\n", "num_int",  np->profile.num_int);
10747
        copy_info(&info, "  %-12s = %lu\n", "num_fly",  np->profile.num_fly);
10748
        copy_info(&info, "  %-12s = %lu\n", "ms_setup", to_ms(np->profile.ms_setup));
10749
        copy_info(&info, "  %-12s = %lu\n", "ms_data",  to_ms(np->profile.ms_data));
10750
        copy_info(&info, "  %-12s = %lu\n", "ms_disc",  to_ms(np->profile.ms_disc));
10751
        copy_info(&info, "  %-12s = %lu\n", "ms_post",  to_ms(np->profile.ms_post));
10752
#endif
10753
 
10754
        return info.pos > info.offset? info.pos - info.offset : 0;
10755
}
10756
 
10757
#endif /* SCSI_NCR_USER_INFO_SUPPORT */
10758
 
10759
/*
10760
**      Entry point of the scsi proc fs of the driver.
10761
**      - func = 0 means read  (returns profile data)
10762
**      - func = 1 means write (parse user control command)
10763
*/
10764
 
10765
int ncr53c8xx_proc_info(char *buffer, char **start, off_t offset,
10766
                        int length, int hostno, int func)
10767
{
10768
        struct Scsi_Host *host;
10769
        struct host_data *host_data;
10770
        ncb_p ncb = 0;
10771
        int retv;
10772
 
10773
#ifdef DEBUG_PROC_INFO
10774
printk("ncr53c8xx_proc_info: hostno=%d, func=%d\n", hostno, func);
10775
#endif
10776
 
10777
        for (host = first_host; host; host = host->next) {
10778
                if (host->hostt == the_template && host->host_no == hostno) {
10779
                        host_data = (struct host_data *) host->hostdata;
10780
                        ncb = host_data->ncb;
10781
                        break;
10782
                }
10783
        }
10784
 
10785
        if (!ncb)
10786
                return -EINVAL;
10787
 
10788
        if (func) {
10789
#ifdef  SCSI_NCR_USER_COMMAND_SUPPORT
10790
                retv = ncr_user_command(ncb, buffer, length);
10791
#else
10792
                retv = -EINVAL;
10793
#endif
10794
        }
10795
        else {
10796
                if (start)
10797
                        *start = buffer;
10798
#ifdef SCSI_NCR_USER_INFO_SUPPORT
10799
                retv = ncr_host_info(ncb, buffer, offset, length);
10800
#else
10801
                retv = -EINVAL;
10802
#endif
10803
        }
10804
 
10805
        return retv;
10806
}
10807
 
10808
 
10809
/*=========================================================================
10810
**      End of proc file system stuff
10811
**=========================================================================
10812
*/
10813
#endif
10814
 
10815
 
10816
#ifdef SCSI_NCR_NVRAM_SUPPORT
10817
 
10818
/* ---------------------------------------------------------------------
10819
**
10820
**      Try reading Symbios format nvram
10821
**
10822
** ---------------------------------------------------------------------
10823
**
10824
** GPOI0 - data in/data out
10825
** GPIO1 - clock
10826
**
10827
**      return 0 if NVRAM data OK, 1 if NVRAM data not OK
10828
** ---------------------------------------------------------------------
10829
*/
10830
 
10831
#define SET_BIT 0
10832
#define CLR_BIT 1
10833
#define SET_CLK 2
10834
#define CLR_CLK 3
10835
 
10836
static u_short nvram_read_data(ncr_slot *np, u_char *data, int len, u_char *gpreg, u_char *gpcntl);
10837
static void nvram_start(ncr_slot *np, u_char *gpreg);
10838
static void nvram_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data, u_char *gpreg, u_char *gpcntl);
10839
static void nvram_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data, u_char *gpreg, u_char *gpcntl);
10840
static void nvram_readAck(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl);
10841
static void nvram_writeAck(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl);
10842
static void nvram_doBit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg);
10843
static void nvram_stop(ncr_slot *np, u_char *gpreg);
10844
static void nvram_setBit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode);
10845
 
10846
__initfunc(
10847
static int ncr_get_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
10848
)
10849
{
10850
        static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
10851
        u_char  gpcntl, gpreg;
10852
        u_char  old_gpcntl, old_gpreg;
10853
        u_short csum;
10854
        u_char  ack_data;
10855
        int     retv = 1;
10856
 
10857
        /* save current state of GPCNTL and GPREG */
10858
        old_gpreg       = INB (nc_gpreg);
10859
        old_gpcntl      = INB (nc_gpcntl);
10860
        gpcntl          = old_gpcntl & 0xfc;
10861
 
10862
        /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
10863
        OUTB (nc_gpreg,  old_gpreg);
10864
        OUTB (nc_gpcntl, gpcntl);
10865
 
10866
        /* this is to set NVRAM into a known state with GPIO0/1 both low */
10867
        gpreg = old_gpreg;
10868
        nvram_setBit(np, 0, &gpreg, CLR_CLK);
10869
        nvram_setBit(np, 0, &gpreg, CLR_BIT);
10870
 
10871
        /* now set NVRAM inactive with GPIO0/1 both high */
10872
        nvram_stop(np, &gpreg);
10873
 
10874
        /* activate NVRAM */
10875
        nvram_start(np, &gpreg);
10876
 
10877
        /* write device code and random address MSB */
10878
        nvram_write_byte(np, &ack_data,
10879
                0xa0 | ((SYMBIOS_NVRAM_ADDRESS >> 7) & 0x0e), &gpreg, &gpcntl);
10880
        if (ack_data & 0x01)
10881
                goto out;
10882
 
10883
        /* write random address LSB */
10884
        nvram_write_byte(np, &ack_data,
10885
                (SYMBIOS_NVRAM_ADDRESS & 0x7f) << 1, &gpreg, &gpcntl);
10886
        if (ack_data & 0x01)
10887
                goto out;
10888
 
10889
        /* regenerate START state to set up for reading */
10890
        nvram_start(np, &gpreg);
10891
 
10892
        /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
10893
        nvram_write_byte(np, &ack_data,
10894
                0xa1 | ((SYMBIOS_NVRAM_ADDRESS >> 7) & 0x0e), &gpreg, &gpcntl);
10895
        if (ack_data & 0x01)
10896
                goto out;
10897
 
10898
        /* now set up GPIO0 for inputting data */
10899
        gpcntl |= 0x01;
10900
        OUTB (nc_gpcntl, gpcntl);
10901
 
10902
        /* input all active data - only part of total NVRAM */
10903
        csum = nvram_read_data(np,
10904
                        (u_char *) nvram, sizeof(*nvram), &gpreg, &gpcntl);
10905
 
10906
        /* finally put NVRAM back in inactive mode */
10907
        gpcntl &= 0xfe;
10908
        OUTB (nc_gpcntl, gpcntl);
10909
        nvram_stop(np, &gpreg);
10910
 
10911
#ifdef SCSI_NCR_DEBUG_NVRAM
10912
printk("ncr53c8xx: NvRAM marker=%x trailer=%x %x %x %x %x %x byte_count=%d/%d checksum=%x/%x\n",
10913
        nvram->start_marker,
10914
        nvram->trailer[0], nvram->trailer[1], nvram->trailer[2],
10915
        nvram->trailer[3], nvram->trailer[4], nvram->trailer[5],
10916
        nvram->byte_count, sizeof(*nvram) - 12,
10917
        nvram->checksum, csum);
10918
#endif
10919
 
10920
        /* check valid NVRAM signature, verify byte count and checksum */
10921
        if (nvram->start_marker == 0 &&
10922
            !memcmp(nvram->trailer, Symbios_trailer, 6) &&
10923
            nvram->byte_count == sizeof(*nvram) - 12 &&
10924
            csum == nvram->checksum)
10925
                retv = 0;
10926
out:
10927
        /* return GPIO0/1 to original states after having accessed NVRAM */
10928
        OUTB (nc_gpcntl, old_gpcntl);
10929
        OUTB (nc_gpreg,  old_gpreg);
10930
 
10931
        return retv;
10932
}
10933
 
10934
/*
10935
 * Read Symbios NvRAM data and compute checksum.
10936
 */
10937
__initfunc(
10938
static u_short nvram_read_data(ncr_slot *np, u_char *data, int len, u_char *gpreg, u_char *gpcntl)
10939
)
10940
{
10941
        int     x;
10942
        u_short csum;
10943
 
10944
        for (x = 0; x < len; x++)
10945
                nvram_read_byte(np, &data[x], (x == (len - 1)), gpreg, gpcntl);
10946
 
10947
        for (x = 6, csum = 0; x < len - 6; x++)
10948
                csum += data[x];
10949
 
10950
        return csum;
10951
}
10952
 
10953
/*
10954
 * Send START condition to NVRAM to wake it up.
10955
 */
10956
__initfunc(
10957
static void nvram_start(ncr_slot *np, u_char *gpreg)
10958
)
10959
{
10960
        nvram_setBit(np, 1, gpreg, SET_BIT);
10961
        nvram_setBit(np, 0, gpreg, SET_CLK);
10962
        nvram_setBit(np, 0, gpreg, CLR_BIT);
10963
        nvram_setBit(np, 0, gpreg, CLR_CLK);
10964
}
10965
 
10966
/*
10967
 * WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
10968
 * GPIO0 must already be set as an output
10969
 */
10970
__initfunc(
10971
static void nvram_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data, u_char *gpreg, u_char *gpcntl)
10972
)
10973
{
10974
        int x;
10975
 
10976
        for (x = 0; x < 8; x++)
10977
                nvram_doBit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
10978
 
10979
        nvram_readAck(np, ack_data, gpreg, gpcntl);
10980
}
10981
 
10982
/*
10983
 * READ a byte from the NVRAM and then send an ACK to say we have got it,
10984
 * GPIO0 must already be set as an input
10985
 */
10986
__initfunc(
10987
static void nvram_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data, u_char *gpreg, u_char *gpcntl)
10988
)
10989
{
10990
        int x;
10991
        u_char read_bit;
10992
 
10993
        *read_data = 0;
10994
        for (x = 0; x < 8; x++) {
10995
                nvram_doBit(np, &read_bit, 1, gpreg);
10996
                *read_data |= ((read_bit & 0x01) << (7 - x));
10997
        }
10998
 
10999
        nvram_writeAck(np, ack_data, gpreg, gpcntl);
11000
}
11001
 
11002
/*
11003
 * Output an ACK to the NVRAM after reading,
11004
 * change GPIO0 to output and when done back to an input
11005
 */
11006
__initfunc(
11007
static void nvram_writeAck(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl)
11008
)
11009
{
11010
        OUTB (nc_gpcntl, *gpcntl & 0xfe);
11011
        nvram_doBit(np, 0, write_bit, gpreg);
11012
        OUTB (nc_gpcntl, *gpcntl);
11013
}
11014
 
11015
/*
11016
 * Input an ACK from NVRAM after writing,
11017
 * change GPIO0 to input and when done back to an output
11018
 */
11019
__initfunc(
11020
static void nvram_readAck(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl)
11021
)
11022
{
11023
        OUTB (nc_gpcntl, *gpcntl | 0x01);
11024
        nvram_doBit(np, read_bit, 1, gpreg);
11025
        OUTB (nc_gpcntl, *gpcntl);
11026
}
11027
 
11028
/*
11029
 * Read or write a bit to the NVRAM,
11030
 * read if GPIO0 input else write if GPIO0 output
11031
 */
11032
__initfunc(
11033
static void nvram_doBit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg)
11034
)
11035
{
11036
        nvram_setBit(np, write_bit, gpreg, SET_BIT);
11037
        nvram_setBit(np, 0, gpreg, SET_CLK);
11038
        if (read_bit)
11039
                *read_bit = INB (nc_gpreg);
11040
        nvram_setBit(np, 0, gpreg, CLR_CLK);
11041
        nvram_setBit(np, 0, gpreg, CLR_BIT);
11042
}
11043
 
11044
/*
11045
 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
11046
 */
11047
__initfunc(
11048
static void nvram_stop(ncr_slot *np, u_char *gpreg)
11049
)
11050
{
11051
        nvram_setBit(np, 0, gpreg, SET_CLK);
11052
        nvram_setBit(np, 1, gpreg, SET_BIT);
11053
}
11054
 
11055
/*
11056
 * Set/clear data/clock bit in GPIO0
11057
 */
11058
__initfunc(
11059
static void nvram_setBit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode)
11060
)
11061
{
11062
        UDELAY (5);
11063
        switch (bit_mode){
11064
        case SET_BIT:
11065
                *gpreg |= write_bit;
11066
                break;
11067
        case CLR_BIT:
11068
                *gpreg &= 0xfe;
11069
                break;
11070
        case SET_CLK:
11071
                *gpreg |= 0x02;
11072
                break;
11073
        case CLR_CLK:
11074
                *gpreg &= 0xfd;
11075
                break;
11076
 
11077
        }
11078
        OUTB (nc_gpreg, *gpreg);
11079
        UDELAY (5);
11080
}
11081
 
11082
#undef SET_BIT 0
11083
#undef CLR_BIT 1
11084
#undef SET_CLK 2
11085
#undef CLR_CLK 3
11086
 
11087
 
11088
/* ---------------------------------------------------------------------
11089
**
11090
**      Try reading Tekram format nvram
11091
**
11092
** ---------------------------------------------------------------------
11093
**
11094
** GPOI0 - data in
11095
** GPIO1 - data out
11096
** GPIO2 - clock
11097
** GPIO4 - chip select
11098
**
11099
**      return 0 if NVRAM data OK, 1 if NVRAM data not OK
11100
** ---------------------------------------------------------------------
11101
*/
11102
 
11103
static u_short Tnvram_read_data(ncr_slot *np, u_short *data, int len, u_char *gpreg);
11104
static void Tnvram_Send_Command(ncr_slot *np, u_short write_data, u_char *read_bit, u_char *gpreg);
11105
static void Tnvram_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg);
11106
static void Tnvram_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg);
11107
static void Tnvram_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg);
11108
static void Tnvram_Stop(ncr_slot *np, u_char *gpreg);
11109
static void Tnvram_Clk(ncr_slot *np, u_char *gpreg);
11110
 
11111
__initfunc(
11112
static int ncr_get_Tekram_nvram (ncr_slot *np, Tekram_nvram *nvram)
11113
)
11114
{
11115
        u_char gpcntl, gpreg;
11116
        u_char old_gpcntl, old_gpreg;
11117
        u_short csum;
11118
 
11119
        /* save current state of GPCNTL and GPREG */
11120
        old_gpreg       = INB (nc_gpreg);
11121
        old_gpcntl      = INB (nc_gpcntl);
11122
 
11123
        /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
11124
           1/2/4 out */
11125
        gpreg = old_gpreg & 0xe9;
11126
        OUTB (nc_gpreg, gpreg);
11127
        gpcntl = (old_gpcntl & 0xe9) | 0x09;
11128
        OUTB (nc_gpcntl, gpcntl);
11129
 
11130
        /* input all of NVRAM, 64 words */
11131
        csum = Tnvram_read_data(np, (u_short *) nvram,
11132
                        sizeof(*nvram) / sizeof(short), &gpreg);
11133
 
11134
        /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
11135
        OUTB (nc_gpcntl, old_gpcntl);
11136
        OUTB (nc_gpreg,  old_gpreg);
11137
 
11138
        /* check data valid */
11139
        if (csum != 0x1234)
11140
                return 1;
11141
 
11142
        return 0;
11143
}
11144
 
11145
/*
11146
 * Read Tekram NvRAM data and compute checksum.
11147
 */
11148
__initfunc(
11149
static u_short Tnvram_read_data(ncr_slot *np, u_short *data, int len, u_char *gpreg)
11150
)
11151
{
11152
        u_char  read_bit;
11153
        u_short csum;
11154
        int     x;
11155
 
11156
        for (x = 0, csum = 0; x < len; x++)  {
11157
 
11158
                /* output read command and address */
11159
                Tnvram_Send_Command(np, 0x180 | x, &read_bit, gpreg);
11160
                if (read_bit & 0x01)
11161
                        return 0; /* Force bad checksum */
11162
 
11163
                Tnvram_Read_Word(np, &data[x], gpreg);
11164
                csum += data[x];
11165
 
11166
                Tnvram_Stop(np, gpreg);
11167
        }
11168
 
11169
        return csum;
11170
}
11171
 
11172
/*
11173
 * Send read command and address to NVRAM
11174
 */
11175
__initfunc(
11176
static void Tnvram_Send_Command(ncr_slot *np, u_short write_data, u_char *read_bit, u_char *gpreg)
11177
)
11178
{
11179
        int x;
11180
 
11181
        /* send 9 bits, start bit (1), command (2), address (6)  */
11182
        for (x = 0; x < 9; x++)
11183
                Tnvram_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
11184
 
11185
        *read_bit = INB (nc_gpreg);
11186
}
11187
 
11188
/*
11189
 * READ a byte from the NVRAM
11190
 */
11191
__initfunc(
11192
static void Tnvram_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg)
11193
)
11194
{
11195
        int x;
11196
        u_char read_bit;
11197
 
11198
        *nvram_data = 0;
11199
        for (x = 0; x < 16; x++) {
11200
                Tnvram_Read_Bit(np, &read_bit, gpreg);
11201
 
11202
                if (read_bit & 0x01)
11203
                        *nvram_data |=  (0x01 << (15 - x));
11204
                else
11205
                        *nvram_data &= ~(0x01 << (15 - x));
11206
        }
11207
}
11208
 
11209
/*
11210
 * Read bit from NVRAM
11211
 */
11212
__initfunc(
11213
static void Tnvram_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg)
11214
)
11215
{
11216
        UDELAY (2);
11217
        Tnvram_Clk(np, gpreg);
11218
        *read_bit = INB (nc_gpreg);
11219
}
11220
 
11221
/*
11222
 * Write bit to GPIO0
11223
 */
11224
__initfunc(
11225
static void Tnvram_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg)
11226
)
11227
{
11228
        if (write_bit & 0x01)
11229
                *gpreg |= 0x02;
11230
        else
11231
                *gpreg &= 0xfd;
11232
 
11233
        *gpreg |= 0x10;
11234
 
11235
        OUTB (nc_gpreg, *gpreg);
11236
        UDELAY (2);
11237
 
11238
        Tnvram_Clk(np, gpreg);
11239
}
11240
 
11241
/*
11242
 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
11243
 */
11244
__initfunc(
11245
static void Tnvram_Stop(ncr_slot *np, u_char *gpreg)
11246
)
11247
{
11248
        *gpreg &= 0xef;
11249
        OUTB (nc_gpreg, *gpreg);
11250
        UDELAY (2);
11251
 
11252
        Tnvram_Clk(np, gpreg);
11253
}
11254
 
11255
/*
11256
 * Pulse clock bit in GPIO0
11257
 */
11258
__initfunc(
11259
static void Tnvram_Clk(ncr_slot *np, u_char *gpreg)
11260
)
11261
{
11262
        OUTB (nc_gpreg, *gpreg | 0x04);
11263
        UDELAY (2);
11264
        OUTB (nc_gpreg, *gpreg);
11265
}
11266
 
11267
#endif  /* SCSI_NCR_NVRAM_SUPPORT */
11268
 
11269
/*
11270
**      Module stuff
11271
*/
11272
 
11273
#ifdef MODULE
11274
Scsi_Host_Template driver_template = NCR53C8XX;
11275
#include "scsi_module.c"
11276
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.