ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / sym53c8xx_comm.h
1 /******************************************************************************
2 **  High Performance device driver for the Symbios 53C896 controller.
3 **
4 **  Copyright (C) 1998-2001  Gerard Roudier <groudier@free.fr>
5 **
6 **  This driver also supports all the Symbios 53C8XX controller family, 
7 **  except 53C810 revisions < 16, 53C825 revisions < 16 and all 
8 **  revisions of 53C815 controllers.
9 **
10 **  This driver is based on the Linux port of the FreeBSD ncr driver.
11 ** 
12 **  Copyright (C) 1994  Wolfgang Stanglmeier
13 **  
14 **-----------------------------------------------------------------------------
15 **  
16 **  This program is free software; you can redistribute it and/or modify
17 **  it under the terms of the GNU General Public License as published by
18 **  the Free Software Foundation; either version 2 of the License, or
19 **  (at your option) any later version.
20 **
21 **  This program is distributed in the hope that it will be useful,
22 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
23 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 **  GNU General Public License for more details.
25 **
26 **  You should have received a copy of the GNU General Public License
27 **  along with this program; if not, write to the Free Software
28 **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 **
30 **-----------------------------------------------------------------------------
31 **
32 **  The Linux port of the FreeBSD ncr driver has been achieved in 
33 **  november 1995 by:
34 **
35 **          Gerard Roudier              <groudier@free.fr>
36 **
37 **  Being given that this driver originates from the FreeBSD version, and
38 **  in order to keep synergy on both, any suggested enhancements and corrections
39 **  received on Linux are automatically a potential candidate for the FreeBSD 
40 **  version.
41 **
42 **  The original driver has been written for 386bsd and FreeBSD by
43 **          Wolfgang Stanglmeier        <wolf@cologne.de>
44 **          Stefan Esser                <se@mi.Uni-Koeln.de>
45 **
46 **-----------------------------------------------------------------------------
47 **
48 **  Major contributions:
49 **  --------------------
50 **
51 **  NVRAM detection and reading.
52 **    Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
53 **
54 *******************************************************************************
55 */
56
57 /*
58 **      This file contains definitions and code that the 
59 **      sym53c8xx and ncr53c8xx drivers should share.
60 **      The sharing will be achieved in a further version  
61 **      of the driver bundle. For now, only the ncr53c8xx 
62 **      driver includes this file.
63 */
64
65 #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
66 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
67
68 /*==========================================================
69 **
70 **      Hmmm... What complex some PCI-HOST bridges actually 
71 **      are, despite the fact that the PCI specifications 
72 **      are looking so smart and simple! ;-)
73 **
74 **==========================================================
75 */
76
77 #define SCSI_NCR_DYNAMIC_DMA_MAPPING
78
79 /*==========================================================
80 **
81 **      Miscallaneous defines.
82 **
83 **==========================================================
84 */
85
86 #define u_char          unsigned char
87 #define u_short         unsigned short
88 #define u_int           unsigned int
89 #define u_long          unsigned long
90
91 #ifndef bcmp
92 #define bcmp(s, d, n)   memcmp((d), (s), (n))
93 #endif
94
95 #ifndef bzero
96 #define bzero(d, n)     memset((d), 0, (n))
97 #endif
98  
99 #ifndef offsetof
100 #define offsetof(t, m)  ((size_t) (&((t *)0)->m))
101 #endif
102
103 /*==========================================================
104 **
105 **      assert ()
106 **
107 **==========================================================
108 **
109 **      modified copy from 386bsd:/usr/include/sys/assert.h
110 **
111 **----------------------------------------------------------
112 */
113
114 #define assert(expression) { \
115         if (!(expression)) { \
116                 (void)panic( \
117                         "assertion \"%s\" failed: file \"%s\", line %d\n", \
118                         #expression, \
119                         __FILE__, __LINE__); \
120         } \
121 }
122
123 /*==========================================================
124 **
125 **      Debugging tags
126 **
127 **==========================================================
128 */
129
130 #define DEBUG_ALLOC    (0x0001)
131 #define DEBUG_PHASE    (0x0002)
132 #define DEBUG_QUEUE    (0x0008)
133 #define DEBUG_RESULT   (0x0010)
134 #define DEBUG_POINTER  (0x0020)
135 #define DEBUG_SCRIPT   (0x0040)
136 #define DEBUG_TINY     (0x0080)
137 #define DEBUG_TIMING   (0x0100)
138 #define DEBUG_NEGO     (0x0200)
139 #define DEBUG_TAGS     (0x0400)
140 #define DEBUG_SCATTER  (0x0800)
141 #define DEBUG_IC        (0x1000)
142
143 /*
144 **    Enable/Disable debug messages.
145 **    Can be changed at runtime too.
146 */
147
148 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
149 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
150         #define DEBUG_FLAGS ncr_debug
151 #else
152         #define DEBUG_FLAGS     SCSI_NCR_DEBUG_FLAGS
153 #endif
154
155 /*==========================================================
156 **
157 **      A la VMS/CAM-3 queue management.
158 **      Implemented from linux list management.
159 **
160 **==========================================================
161 */
162
163 typedef struct xpt_quehead {
164         struct xpt_quehead *flink;      /* Forward  pointer */
165         struct xpt_quehead *blink;      /* Backward pointer */
166 } XPT_QUEHEAD;
167
168 #define xpt_que_init(ptr) do { \
169         (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
170 } while (0)
171
172 static inline void __xpt_que_add(struct xpt_quehead * new,
173         struct xpt_quehead * blink,
174         struct xpt_quehead * flink)
175 {
176         flink->blink    = new;
177         new->flink      = flink;
178         new->blink      = blink;
179         blink->flink    = new;
180 }
181
182 static inline void __xpt_que_del(struct xpt_quehead * blink,
183         struct xpt_quehead * flink)
184 {
185         flink->blink = blink;
186         blink->flink = flink;
187 }
188
189 static inline int xpt_que_empty(struct xpt_quehead *head)
190 {
191         return head->flink == head;
192 }
193
194 static inline void xpt_que_splice(struct xpt_quehead *list,
195         struct xpt_quehead *head)
196 {
197         struct xpt_quehead *first = list->flink;
198
199         if (first != list) {
200                 struct xpt_quehead *last = list->blink;
201                 struct xpt_quehead *at   = head->flink;
202
203                 first->blink = head;
204                 head->flink  = first;
205
206                 last->flink = at;
207                 at->blink   = last;
208         }
209 }
210
211 #define xpt_que_entry(ptr, type, member) \
212         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
213
214
215 #define xpt_insque(new, pos)            __xpt_que_add(new, pos, (pos)->flink)
216
217 #define xpt_remque(el)                  __xpt_que_del((el)->blink, (el)->flink)
218
219 #define xpt_insque_head(new, head)      __xpt_que_add(new, head, (head)->flink)
220
221 static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
222 {
223         struct xpt_quehead *elem = head->flink;
224
225         if (elem != head)
226                 __xpt_que_del(head, elem->flink);
227         else
228                 elem = 0;
229         return elem;
230 }
231
232 #define xpt_insque_tail(new, head)      __xpt_que_add(new, (head)->blink, head)
233
234 static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
235 {
236         struct xpt_quehead *elem = head->blink;
237
238         if (elem != head)
239                 __xpt_que_del(elem->blink, head);
240         else
241                 elem = 0;
242         return elem;
243 }
244
245
246 /*==========================================================
247 **
248 **      SMP threading.
249 **
250 **      Assuming that SMP systems are generally high end 
251 **      systems and may use several SCSI adapters, we are 
252 **      using one lock per controller instead of some global 
253 **      one. For the moment (linux-2.1.95), driver's entry 
254 **      points are called with the 'io_request_lock' lock 
255 **      held, so:
256 **      - We are uselessly loosing a couple of micro-seconds 
257 **        to lock the controller data structure.
258 **      - But the driver is not broken by design for SMP and 
259 **        so can be more resistant to bugs or bad changes in 
260 **        the IO sub-system code.
261 **      - A small advantage could be that the interrupt code 
262 **        is grained as wished (e.g.: by controller).
263 **
264 **==========================================================
265 */
266
267 spinlock_t DRIVER_SMP_LOCK = SPIN_LOCK_UNLOCKED;
268 #define NCR_LOCK_DRIVER(flags)     spin_lock_irqsave(&DRIVER_SMP_LOCK, flags)
269 #define NCR_UNLOCK_DRIVER(flags)   \
270                 spin_unlock_irqrestore(&DRIVER_SMP_LOCK, flags)
271
272 #define NCR_INIT_LOCK_NCB(np)      spin_lock_init(&np->smp_lock)
273 #define NCR_LOCK_NCB(np, flags)    spin_lock_irqsave(&np->smp_lock, flags)
274 #define NCR_UNLOCK_NCB(np, flags)  spin_unlock_irqrestore(&np->smp_lock, flags)
275
276 #define NCR_LOCK_SCSI_DONE(host, flags) \
277                 spin_lock_irqsave((host)->host_lock, flags)
278 #define NCR_UNLOCK_SCSI_DONE(host, flags) \
279                 spin_unlock_irqrestore(((host)->host_lock), flags)
280
281 /*==========================================================
282 **
283 **      Memory mapped IO
284 **
285 **      Since linux-2.1, we must use ioremap() to map the io 
286 **      memory space and iounmap() to unmap it. This allows 
287 **      portability. Linux 1.3.X and 2.0.X allow to remap 
288 **      physical pages addresses greater than the highest 
289 **      physical memory address to kernel virtual pages with 
290 **      vremap() / vfree(). That was not portable but worked 
291 **      with i386 architecture.
292 **
293 **==========================================================
294 */
295
296 #ifdef __sparc__
297 #include <asm/irq.h>
298 #endif
299
300 #define memcpy_to_pci(a, b, c)  memcpy_toio((a), (b), (c))
301
302 /*==========================================================
303 **
304 **      Insert a delay in micro-seconds and milli-seconds.
305 **
306 **      Under Linux, udelay() is restricted to delay < 
307 **      1 milli-second. In fact, it generally works for up 
308 **      to 1 second delay. Since 2.1.105, the mdelay() function 
309 **      is provided for delays in milli-seconds.
310 **      Under 2.0 kernels, udelay() is an inline function 
311 **      that is very inaccurate on Pentium processors.
312 **
313 **==========================================================
314 */
315
316 #define UDELAY udelay
317 #define MDELAY mdelay
318
319 /*==========================================================
320 **
321 **      Simple power of two buddy-like allocator.
322 **
323 **      This simple code is not intended to be fast, but to 
324 **      provide power of 2 aligned memory allocations.
325 **      Since the SCRIPTS processor only supplies 8 bit 
326 **      arithmetic, this allocator allows simple and fast 
327 **      address calculations  from the SCRIPTS code.
328 **      In addition, cache line alignment is guaranteed for 
329 **      power of 2 cache line size.
330 **      Enhanced in linux-2.3.44 to provide a memory pool 
331 **      per pcidev to support dynamic dma mapping. (I would 
332 **      have preferred a real bus astraction, btw).
333 **
334 **==========================================================
335 */
336
337 #define __GetFreePages(flags, order) __get_free_pages(flags, order)
338
339 #define MEMO_SHIFT      4       /* 16 bytes minimum memory chunk */
340 #if PAGE_SIZE >= 8192
341 #define MEMO_PAGE_ORDER 0       /* 1 PAGE  maximum */
342 #else
343 #define MEMO_PAGE_ORDER 1       /* 2 PAGES maximum */
344 #endif
345 #define MEMO_FREE_UNUSED        /* Free unused pages immediately */
346 #define MEMO_WARN       1
347 #define MEMO_GFP_FLAGS  GFP_ATOMIC
348 #define MEMO_CLUSTER_SHIFT      (PAGE_SHIFT+MEMO_PAGE_ORDER)
349 #define MEMO_CLUSTER_SIZE       (1UL << MEMO_CLUSTER_SHIFT)
350 #define MEMO_CLUSTER_MASK       (MEMO_CLUSTER_SIZE-1)
351
352 typedef u_long m_addr_t;        /* Enough bits to bit-hack addresses */
353 typedef struct device *m_bush_t;        /* Something that addresses DMAable */
354
355 typedef struct m_link {         /* Link between free memory chunks */
356         struct m_link *next;
357 } m_link_s;
358
359 #ifdef  SCSI_NCR_DYNAMIC_DMA_MAPPING
360 typedef struct m_vtob {         /* Virtual to Bus address translation */
361         struct m_vtob *next;
362         m_addr_t vaddr;
363         m_addr_t baddr;
364 } m_vtob_s;
365 #define VTOB_HASH_SHIFT         5
366 #define VTOB_HASH_SIZE          (1UL << VTOB_HASH_SHIFT)
367 #define VTOB_HASH_MASK          (VTOB_HASH_SIZE-1)
368 #define VTOB_HASH_CODE(m)       \
369         ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
370 #endif
371
372 typedef struct m_pool {         /* Memory pool of a given kind */
373 #ifdef  SCSI_NCR_DYNAMIC_DMA_MAPPING
374         m_bush_t bush;
375         m_addr_t (*getp)(struct m_pool *);
376         void (*freep)(struct m_pool *, m_addr_t);
377 #define M_GETP()                mp->getp(mp)
378 #define M_FREEP(p)              mp->freep(mp, p)
379 #define GetPages()              __GetFreePages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER)
380 #define FreePages(p)            free_pages(p, MEMO_PAGE_ORDER)
381         int nump;
382         m_vtob_s *(vtob[VTOB_HASH_SIZE]);
383         struct m_pool *next;
384 #else
385 #define M_GETP()                __GetFreePages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER)
386 #define M_FREEP(p)              free_pages(p, MEMO_PAGE_ORDER)
387 #endif  /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
388         struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
389 } m_pool_s;
390
391 static void *___m_alloc(m_pool_s *mp, int size)
392 {
393         int i = 0;
394         int s = (1 << MEMO_SHIFT);
395         int j;
396         m_addr_t a;
397         m_link_s *h = mp->h;
398
399         if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
400                 return 0;
401
402         while (size > s) {
403                 s <<= 1;
404                 ++i;
405         }
406
407         j = i;
408         while (!h[j].next) {
409                 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
410                         h[j].next = (m_link_s *) M_GETP();
411                         if (h[j].next)
412                                 h[j].next->next = 0;
413                         break;
414                 }
415                 ++j;
416                 s <<= 1;
417         }
418         a = (m_addr_t) h[j].next;
419         if (a) {
420                 h[j].next = h[j].next->next;
421                 while (j > i) {
422                         j -= 1;
423                         s >>= 1;
424                         h[j].next = (m_link_s *) (a+s);
425                         h[j].next->next = 0;
426                 }
427         }
428 #ifdef DEBUG
429         printk("___m_alloc(%d) = %p\n", size, (void *) a);
430 #endif
431         return (void *) a;
432 }
433
434 static void ___m_free(m_pool_s *mp, void *ptr, int size)
435 {
436         int i = 0;
437         int s = (1 << MEMO_SHIFT);
438         m_link_s *q;
439         m_addr_t a, b;
440         m_link_s *h = mp->h;
441
442 #ifdef DEBUG
443         printk("___m_free(%p, %d)\n", ptr, size);
444 #endif
445
446         if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
447                 return;
448
449         while (size > s) {
450                 s <<= 1;
451                 ++i;
452         }
453
454         a = (m_addr_t) ptr;
455
456         while (1) {
457 #ifdef MEMO_FREE_UNUSED
458                 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
459                         M_FREEP(a);
460                         break;
461                 }
462 #endif
463                 b = a ^ s;
464                 q = &h[i];
465                 while (q->next && q->next != (m_link_s *) b) {
466                         q = q->next;
467                 }
468                 if (!q->next) {
469                         ((m_link_s *) a)->next = h[i].next;
470                         h[i].next = (m_link_s *) a;
471                         break;
472                 }
473                 q->next = q->next->next;
474                 a = a & b;
475                 s <<= 1;
476                 ++i;
477         }
478 }
479
480 static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
481 {
482         void *p;
483
484         p = ___m_alloc(mp, size);
485
486         if (DEBUG_FLAGS & DEBUG_ALLOC)
487                 printk ("new %-10s[%4d] @%p.\n", name, size, p);
488
489         if (p)
490                 bzero(p, size);
491         else if (uflags & MEMO_WARN)
492                 printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
493
494         return p;
495 }
496
497 #define __m_calloc(mp, s, n)    __m_calloc2(mp, s, n, MEMO_WARN)
498
499 static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
500 {
501         if (DEBUG_FLAGS & DEBUG_ALLOC)
502                 printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
503
504         ___m_free(mp, ptr, size);
505
506 }
507
508 /*
509  * With pci bus iommu support, we use a default pool of unmapped memory 
510  * for memory we donnot need to DMA from/to and one pool per pcidev for 
511  * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
512  */
513
514 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
515
516 static m_pool_s mp0;
517
518 #else
519
520 static m_addr_t ___mp0_getp(m_pool_s *mp)
521 {
522         m_addr_t m = GetPages();
523         if (m)
524                 ++mp->nump;
525         return m;
526 }
527
528 static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
529 {
530         FreePages(m);
531         --mp->nump;
532 }
533
534 static m_pool_s mp0 = {0, ___mp0_getp, ___mp0_freep};
535
536 #endif  /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
537
538 /*
539  * DMAable pools.
540  */
541
542 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
543
544 /* Without pci bus iommu support, all the memory is assumed DMAable */
545
546 #define __m_calloc_dma(b, s, n)         m_calloc(s, n)
547 #define __m_free_dma(b, p, s, n)        m_free(p, s, n)
548 #define __vtobus(b, p)                  virt_to_bus(p)
549
550 #else
551
552 /*
553  * With pci bus iommu support, we maintain one pool per pcidev and a 
554  * hashed reverse table for virtual to bus physical address translations.
555  */
556 static m_addr_t ___dma_getp(m_pool_s *mp)
557 {
558         m_addr_t vp;
559         m_vtob_s *vbp;
560
561         vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
562         if (vbp) {
563                 dma_addr_t daddr;
564                 vp = (m_addr_t) dma_alloc_coherent(mp->bush,
565                                                 PAGE_SIZE<<MEMO_PAGE_ORDER,
566                                                 &daddr, GFP_ATOMIC);
567                 if (vp) {
568                         int hc = VTOB_HASH_CODE(vp);
569                         vbp->vaddr = vp;
570                         vbp->baddr = daddr;
571                         vbp->next = mp->vtob[hc];
572                         mp->vtob[hc] = vbp;
573                         ++mp->nump;
574                         return vp;
575                 }
576         }
577         if (vbp)
578                 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
579         return 0;
580 }
581
582 static void ___dma_freep(m_pool_s *mp, m_addr_t m)
583 {
584         m_vtob_s **vbpp, *vbp;
585         int hc = VTOB_HASH_CODE(m);
586
587         vbpp = &mp->vtob[hc];
588         while (*vbpp && (*vbpp)->vaddr != m)
589                 vbpp = &(*vbpp)->next;
590         if (*vbpp) {
591                 vbp = *vbpp;
592                 *vbpp = (*vbpp)->next;
593                 dma_free_coherent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
594                                   (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
595                 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
596                 --mp->nump;
597         }
598 }
599
600 static inline m_pool_s *___get_dma_pool(m_bush_t bush)
601 {
602         m_pool_s *mp;
603         for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
604         return mp;
605 }
606
607 static m_pool_s *___cre_dma_pool(m_bush_t bush)
608 {
609         m_pool_s *mp;
610         mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
611         if (mp) {
612                 bzero(mp, sizeof(*mp));
613                 mp->bush = bush;
614                 mp->getp = ___dma_getp;
615                 mp->freep = ___dma_freep;
616                 mp->next = mp0.next;
617                 mp0.next = mp;
618         }
619         return mp;
620 }
621
622 static void ___del_dma_pool(m_pool_s *p)
623 {
624         struct m_pool **pp = &mp0.next;
625
626         while (*pp && *pp != p)
627                 pp = &(*pp)->next;
628         if (*pp) {
629                 *pp = (*pp)->next;
630                 __m_free(&mp0, p, sizeof(*p), "MPOOL");
631         }
632 }
633
634 static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
635 {
636         u_long flags;
637         struct m_pool *mp;
638         void *m = 0;
639
640         NCR_LOCK_DRIVER(flags);
641         mp = ___get_dma_pool(bush);
642         if (!mp)
643                 mp = ___cre_dma_pool(bush);
644         if (mp)
645                 m = __m_calloc(mp, size, name);
646         if (mp && !mp->nump)
647                 ___del_dma_pool(mp);
648         NCR_UNLOCK_DRIVER(flags);
649
650         return m;
651 }
652
653 static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
654 {
655         u_long flags;
656         struct m_pool *mp;
657
658         NCR_LOCK_DRIVER(flags);
659         mp = ___get_dma_pool(bush);
660         if (mp)
661                 __m_free(mp, m, size, name);
662         if (mp && !mp->nump)
663                 ___del_dma_pool(mp);
664         NCR_UNLOCK_DRIVER(flags);
665 }
666
667 static m_addr_t __vtobus(m_bush_t bush, void *m)
668 {
669         u_long flags;
670         m_pool_s *mp;
671         int hc = VTOB_HASH_CODE(m);
672         m_vtob_s *vp = 0;
673         m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
674
675         NCR_LOCK_DRIVER(flags);
676         mp = ___get_dma_pool(bush);
677         if (mp) {
678                 vp = mp->vtob[hc];
679                 while (vp && (m_addr_t) vp->vaddr != a)
680                         vp = vp->next;
681         }
682         NCR_UNLOCK_DRIVER(flags);
683         return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
684 }
685
686 #endif  /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
687
688 #define _m_calloc_dma(np, s, n)         __m_calloc_dma(np->dev, s, n)
689 #define _m_free_dma(np, p, s, n)        __m_free_dma(np->dev, p, s, n)
690 #define m_calloc_dma(s, n)              _m_calloc_dma(np, s, n)
691 #define m_free_dma(p, s, n)             _m_free_dma(np, p, s, n)
692 #define _vtobus(np, p)                  __vtobus(np->dev, p)
693 #define vtobus(p)                       _vtobus(np, p)
694
695 /*
696  *  Deal with DMA mapping/unmapping.
697  */
698
699 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
700
701 /* Linux versions prior to pci bus iommu kernel interface */
702
703 #define __unmap_scsi_data(dev, cmd)     do {; } while (0)
704 #define __map_scsi_single_data(dev, cmd) (__vtobus(dev,(cmd)->request_buffer))
705 #define __map_scsi_sg_data(dev, cmd)    ((cmd)->use_sg)
706 #define __sync_scsi_data_for_cpu(dev, cmd)      do {; } while (0)
707 #define __sync_scsi_data_for_device(dev, cmd)   do {; } while (0)
708
709 #define scsi_sg_dma_address(sc)         vtobus((sc)->address)
710 #define scsi_sg_dma_len(sc)             ((sc)->length)
711
712 #else
713
714 /* Linux version with pci bus iommu kernel interface */
715
716 /* To keep track of the dma mapping (sg/single) that has been set */
717 #define __data_mapped   SCp.phase
718 #define __data_mapping  SCp.have_data_in
719
720 static void __unmap_scsi_data(struct device *dev, Scsi_Cmnd *cmd)
721 {
722         enum dma_data_direction dma_dir = 
723                 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
724
725         switch(cmd->__data_mapped) {
726         case 2:
727                 dma_unmap_sg(dev, cmd->buffer, cmd->use_sg, dma_dir);
728                 break;
729         case 1:
730                 dma_unmap_single(dev, cmd->__data_mapping,
731                                  cmd->request_bufflen, dma_dir);
732                 break;
733         }
734         cmd->__data_mapped = 0;
735 }
736
737 static u_long __map_scsi_single_data(struct device *dev, Scsi_Cmnd *cmd)
738 {
739         dma_addr_t mapping;
740         enum dma_data_direction dma_dir = 
741                 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
742
743
744         if (cmd->request_bufflen == 0)
745                 return 0;
746
747         mapping = dma_map_single(dev, cmd->request_buffer,
748                                  cmd->request_bufflen, dma_dir);
749         cmd->__data_mapped = 1;
750         cmd->__data_mapping = mapping;
751
752         return mapping;
753 }
754
755 static int __map_scsi_sg_data(struct device *dev, Scsi_Cmnd *cmd)
756 {
757         int use_sg;
758         enum dma_data_direction dma_dir = 
759                 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
760
761         if (cmd->use_sg == 0)
762                 return 0;
763
764         use_sg = dma_map_sg(dev, cmd->buffer, cmd->use_sg, dma_dir);
765         cmd->__data_mapped = 2;
766         cmd->__data_mapping = use_sg;
767
768         return use_sg;
769 }
770
771 static void __sync_scsi_data_for_cpu(struct device *dev, Scsi_Cmnd *cmd)
772 {
773         enum dma_data_direction dma_dir = 
774                 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
775
776         switch(cmd->__data_mapped) {
777         case 2:
778                 dma_sync_sg_for_cpu(dev, cmd->buffer, cmd->use_sg, dma_dir);
779                 break;
780         case 1:
781                 dma_sync_single_for_cpu(dev, cmd->__data_mapping,
782                                         cmd->request_bufflen, dma_dir);
783                 break;
784         }
785 }
786
787 static void __sync_scsi_data_for_device(struct device *dev, Scsi_Cmnd *cmd)
788 {
789         enum dma_data_direction dma_dir =
790                 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
791
792         switch(cmd->__data_mapped) {
793         case 2:
794                 dma_sync_sg_for_device(dev, cmd->buffer, cmd->use_sg, dma_dir);
795                 break;
796         case 1:
797                 dma_sync_single_for_device(dev, cmd->__data_mapping,
798                                            cmd->request_bufflen, dma_dir);
799                 break;
800         }
801 }
802
803 #define scsi_sg_dma_address(sc)         sg_dma_address(sc)
804 #define scsi_sg_dma_len(sc)             sg_dma_len(sc)
805
806 #endif  /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
807
808 #define unmap_scsi_data(np, cmd)        __unmap_scsi_data(np->dev, cmd)
809 #define map_scsi_single_data(np, cmd)   __map_scsi_single_data(np->dev, cmd)
810 #define map_scsi_sg_data(np, cmd)       __map_scsi_sg_data(np->dev, cmd)
811 #define sync_scsi_data_for_cpu(np, cmd) __sync_scsi_data_for_cpu(np->dev, cmd)
812 #define sync_scsi_data_for_device(np, cmd) __sync_scsi_data_for_device(np->dev, cmd)
813
814 /*==========================================================
815 **
816 **      SCSI data transfer direction
817 **
818 **      Until some linux kernel version near 2.3.40, 
819 **      low-level scsi drivers were not told about data 
820 **      transfer direction. We check the existence of this 
821 **      feature that has been expected for a _long_ time by 
822 **      all SCSI driver developers by just testing against 
823 **      the definition of SCSI_DATA_UNKNOWN. Indeed this is 
824 **      a hack, but testing against a kernel version would 
825 **      have been a shame. ;-)
826 **
827 **==========================================================
828 */
829 #ifdef  SCSI_DATA_UNKNOWN
830
831 #define scsi_data_direction(cmd)        (cmd->sc_data_direction)
832
833 #else
834
835 #define SCSI_DATA_UNKNOWN       0
836 #define SCSI_DATA_WRITE         1
837 #define SCSI_DATA_READ          2
838 #define SCSI_DATA_NONE          3
839
840 static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
841 {
842         int direction;
843
844         switch((int) cmd->cmnd[0]) {
845         case 0x08:  /*  READ(6)                         08 */
846         case 0x28:  /*  READ(10)                        28 */
847         case 0xA8:  /*  READ(12)                        A8 */
848                 direction = SCSI_DATA_READ;
849                 break;
850         case 0x0A:  /*  WRITE(6)                        0A */
851         case 0x2A:  /*  WRITE(10)                       2A */
852         case 0xAA:  /*  WRITE(12)                       AA */
853                 direction = SCSI_DATA_WRITE;
854                 break;
855         default:
856                 direction = SCSI_DATA_UNKNOWN;
857                 break;
858         }
859
860         return direction;
861 }
862
863 #endif  /* SCSI_DATA_UNKNOWN */
864
865 /*==========================================================
866 **
867 **      Driver setup.
868 **
869 **      This structure is initialized from linux config 
870 **      options. It can be overridden at boot-up by the boot 
871 **      command line.
872 **
873 **==========================================================
874 */
875 static struct ncr_driver_setup
876         driver_setup                    = SCSI_NCR_DRIVER_SETUP;
877
878 #ifdef  SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
879 static struct ncr_driver_setup
880         driver_safe_setup __initdata    = SCSI_NCR_DRIVER_SAFE_SETUP;
881 #endif
882
883 #define initverbose (driver_setup.verbose)
884 #define bootverbose (np->verbose)
885
886
887 /*==========================================================
888 **
889 **      NVRAM detection and reading.
890 **       
891 **      Currently supported:
892 **      - 24C16 EEPROM with both Symbios and Tekram layout.
893 **      - 93C46 EEPROM with Tekram layout.
894 **
895 **==========================================================
896 */
897
898 #ifdef SCSI_NCR_NVRAM_SUPPORT
899 /*
900  *  24C16 EEPROM reading.
901  *
902  *  GPOI0 - data in/data out
903  *  GPIO1 - clock
904  *  Symbios NVRAM wiring now also used by Tekram.
905  */
906
907 #define SET_BIT 0
908 #define CLR_BIT 1
909 #define SET_CLK 2
910 #define CLR_CLK 3
911
912 /*
913  *  Set/clear data/clock bit in GPIO0
914  */
915 static void __init
916 S24C16_set_bit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode)
917 {
918         UDELAY (5);
919         switch (bit_mode){
920         case SET_BIT:
921                 *gpreg |= write_bit;
922                 break;
923         case CLR_BIT:
924                 *gpreg &= 0xfe;
925                 break;
926         case SET_CLK:
927                 *gpreg |= 0x02;
928                 break;
929         case CLR_CLK:
930                 *gpreg &= 0xfd;
931                 break;
932
933         }
934         OUTB (nc_gpreg, *gpreg);
935         UDELAY (5);
936 }
937
938 /*
939  *  Send START condition to NVRAM to wake it up.
940  */
941 static void __init S24C16_start(ncr_slot *np, u_char *gpreg)
942 {
943         S24C16_set_bit(np, 1, gpreg, SET_BIT);
944         S24C16_set_bit(np, 0, gpreg, SET_CLK);
945         S24C16_set_bit(np, 0, gpreg, CLR_BIT);
946         S24C16_set_bit(np, 0, gpreg, CLR_CLK);
947 }
948
949 /*
950  *  Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
951  */
952 static void __init S24C16_stop(ncr_slot *np, u_char *gpreg)
953 {
954         S24C16_set_bit(np, 0, gpreg, SET_CLK);
955         S24C16_set_bit(np, 1, gpreg, SET_BIT);
956 }
957
958 /*
959  *  Read or write a bit to the NVRAM,
960  *  read if GPIO0 input else write if GPIO0 output
961  */
962 static void __init 
963 S24C16_do_bit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg)
964 {
965         S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
966         S24C16_set_bit(np, 0, gpreg, SET_CLK);
967         if (read_bit)
968                 *read_bit = INB (nc_gpreg);
969         S24C16_set_bit(np, 0, gpreg, CLR_CLK);
970         S24C16_set_bit(np, 0, gpreg, CLR_BIT);
971 }
972
973 /*
974  *  Output an ACK to the NVRAM after reading,
975  *  change GPIO0 to output and when done back to an input
976  */
977 static void __init
978 S24C16_write_ack(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl)
979 {
980         OUTB (nc_gpcntl, *gpcntl & 0xfe);
981         S24C16_do_bit(np, 0, write_bit, gpreg);
982         OUTB (nc_gpcntl, *gpcntl);
983 }
984
985 /*
986  *  Input an ACK from NVRAM after writing,
987  *  change GPIO0 to input and when done back to an output
988  */
989 static void __init 
990 S24C16_read_ack(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl)
991 {
992         OUTB (nc_gpcntl, *gpcntl | 0x01);
993         S24C16_do_bit(np, read_bit, 1, gpreg);
994         OUTB (nc_gpcntl, *gpcntl);
995 }
996
997 /*
998  *  WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
999  *  GPIO0 must already be set as an output
1000  */
1001 static void __init 
1002 S24C16_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data, 
1003                   u_char *gpreg, u_char *gpcntl)
1004 {
1005         int x;
1006         
1007         for (x = 0; x < 8; x++)
1008                 S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
1009                 
1010         S24C16_read_ack(np, ack_data, gpreg, gpcntl);
1011 }
1012
1013 /*
1014  *  READ a byte from the NVRAM and then send an ACK to say we have got it,
1015  *  GPIO0 must already be set as an input
1016  */
1017 static void __init 
1018 S24C16_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data, 
1019                  u_char *gpreg, u_char *gpcntl)
1020 {
1021         int x;
1022         u_char read_bit;
1023
1024         *read_data = 0;
1025         for (x = 0; x < 8; x++) {
1026                 S24C16_do_bit(np, &read_bit, 1, gpreg);
1027                 *read_data |= ((read_bit & 0x01) << (7 - x));
1028         }
1029
1030         S24C16_write_ack(np, ack_data, gpreg, gpcntl);
1031 }
1032
1033 /*
1034  *  Read 'len' bytes starting at 'offset'.
1035  */
1036 static int __init 
1037 sym_read_S24C16_nvram (ncr_slot *np, int offset, u_char *data, int len)
1038 {
1039         u_char  gpcntl, gpreg;
1040         u_char  old_gpcntl, old_gpreg;
1041         u_char  ack_data;
1042         int     retv = 1;
1043         int     x;
1044
1045         /* save current state of GPCNTL and GPREG */
1046         old_gpreg       = INB (nc_gpreg);
1047         old_gpcntl      = INB (nc_gpcntl);
1048         gpcntl          = old_gpcntl & 0x1c;
1049
1050         /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
1051         OUTB (nc_gpreg,  old_gpreg);
1052         OUTB (nc_gpcntl, gpcntl);
1053
1054         /* this is to set NVRAM into a known state with GPIO0/1 both low */
1055         gpreg = old_gpreg;
1056         S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
1057         S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
1058                 
1059         /* now set NVRAM inactive with GPIO0/1 both high */
1060         S24C16_stop(np, &gpreg);
1061         
1062         /* activate NVRAM */
1063         S24C16_start(np, &gpreg);
1064
1065         /* write device code and random address MSB */
1066         S24C16_write_byte(np, &ack_data,
1067                 0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
1068         if (ack_data & 0x01)
1069                 goto out;
1070
1071         /* write random address LSB */
1072         S24C16_write_byte(np, &ack_data,
1073                 offset & 0xff, &gpreg, &gpcntl);
1074         if (ack_data & 0x01)
1075                 goto out;
1076
1077         /* regenerate START state to set up for reading */
1078         S24C16_start(np, &gpreg);
1079         
1080         /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
1081         S24C16_write_byte(np, &ack_data,
1082                 0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
1083         if (ack_data & 0x01)
1084                 goto out;
1085
1086         /* now set up GPIO0 for inputting data */
1087         gpcntl |= 0x01;
1088         OUTB (nc_gpcntl, gpcntl);
1089                 
1090         /* input all requested data - only part of total NVRAM */
1091         for (x = 0; x < len; x++) 
1092                 S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl);
1093
1094         /* finally put NVRAM back in inactive mode */
1095         gpcntl &= 0xfe;
1096         OUTB (nc_gpcntl, gpcntl);
1097         S24C16_stop(np, &gpreg);
1098         retv = 0;
1099 out:
1100         /* return GPIO0/1 to original states after having accessed NVRAM */
1101         OUTB (nc_gpcntl, old_gpcntl);
1102         OUTB (nc_gpreg,  old_gpreg);
1103
1104         return retv;
1105 }
1106
1107 #undef SET_BIT
1108 #undef CLR_BIT
1109 #undef SET_CLK
1110 #undef CLR_CLK
1111
1112 /*
1113  *  Try reading Symbios NVRAM.
1114  *  Return 0 if OK.
1115  */
1116 static int __init sym_read_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
1117 {
1118         static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
1119         u_char *data = (u_char *) nvram;
1120         int len  = sizeof(*nvram);
1121         u_short csum;
1122         int x;
1123
1124         /* probe the 24c16 and read the SYMBIOS 24c16 area */
1125         if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
1126                 return 1;
1127
1128         /* check valid NVRAM signature, verify byte count and checksum */
1129         if (nvram->type != 0 ||
1130             memcmp(nvram->trailer, Symbios_trailer, 6) ||
1131             nvram->byte_count != len - 12)
1132                 return 1;
1133
1134         /* verify checksum */
1135         for (x = 6, csum = 0; x < len - 6; x++)
1136                 csum += data[x];
1137         if (csum != nvram->checksum)
1138                 return 1;
1139
1140         return 0;
1141 }
1142
1143 /*
1144  *  93C46 EEPROM reading.
1145  *
1146  *  GPOI0 - data in
1147  *  GPIO1 - data out
1148  *  GPIO2 - clock
1149  *  GPIO4 - chip select
1150  *
1151  *  Used by Tekram.
1152  */
1153
1154 /*
1155  *  Pulse clock bit in GPIO0
1156  */
1157 static void __init T93C46_Clk(ncr_slot *np, u_char *gpreg)
1158 {
1159         OUTB (nc_gpreg, *gpreg | 0x04);
1160         UDELAY (2);
1161         OUTB (nc_gpreg, *gpreg);
1162 }
1163
1164 /* 
1165  *  Read bit from NVRAM
1166  */
1167 static void __init T93C46_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg)
1168 {
1169         UDELAY (2);
1170         T93C46_Clk(np, gpreg);
1171         *read_bit = INB (nc_gpreg);
1172 }
1173
1174 /*
1175  *  Write bit to GPIO0
1176  */
1177 static void __init T93C46_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg)
1178 {
1179         if (write_bit & 0x01)
1180                 *gpreg |= 0x02;
1181         else
1182                 *gpreg &= 0xfd;
1183                 
1184         *gpreg |= 0x10;
1185                 
1186         OUTB (nc_gpreg, *gpreg);
1187         UDELAY (2);
1188
1189         T93C46_Clk(np, gpreg);
1190 }
1191
1192 /*
1193  *  Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
1194  */
1195 static void __init T93C46_Stop(ncr_slot *np, u_char *gpreg)
1196 {
1197         *gpreg &= 0xef;
1198         OUTB (nc_gpreg, *gpreg);
1199         UDELAY (2);
1200
1201         T93C46_Clk(np, gpreg);
1202 }
1203
1204 /*
1205  *  Send read command and address to NVRAM
1206  */
1207 static void __init 
1208 T93C46_Send_Command(ncr_slot *np, u_short write_data, 
1209                     u_char *read_bit, u_char *gpreg)
1210 {
1211         int x;
1212
1213         /* send 9 bits, start bit (1), command (2), address (6)  */
1214         for (x = 0; x < 9; x++)
1215                 T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
1216
1217         *read_bit = INB (nc_gpreg);
1218 }
1219
1220 /*
1221  *  READ 2 bytes from the NVRAM
1222  */
1223 static void __init 
1224 T93C46_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg)
1225 {
1226         int x;
1227         u_char read_bit;
1228
1229         *nvram_data = 0;
1230         for (x = 0; x < 16; x++) {
1231                 T93C46_Read_Bit(np, &read_bit, gpreg);
1232
1233                 if (read_bit & 0x01)
1234                         *nvram_data |=  (0x01 << (15 - x));
1235                 else
1236                         *nvram_data &= ~(0x01 << (15 - x));
1237         }
1238 }
1239
1240 /*
1241  *  Read Tekram NvRAM data.
1242  */
1243 static int __init 
1244 T93C46_Read_Data(ncr_slot *np, u_short *data,int len,u_char *gpreg)
1245 {
1246         u_char  read_bit;
1247         int     x;
1248
1249         for (x = 0; x < len; x++)  {
1250
1251                 /* output read command and address */
1252                 T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
1253                 if (read_bit & 0x01)
1254                         return 1; /* Bad */
1255                 T93C46_Read_Word(np, &data[x], gpreg);
1256                 T93C46_Stop(np, gpreg);
1257         }
1258
1259         return 0;
1260 }
1261
1262 /*
1263  *  Try reading 93C46 Tekram NVRAM.
1264  */
1265 static int __init 
1266 sym_read_T93C46_nvram (ncr_slot *np, Tekram_nvram *nvram)
1267 {
1268         u_char gpcntl, gpreg;
1269         u_char old_gpcntl, old_gpreg;
1270         int retv = 1;
1271
1272         /* save current state of GPCNTL and GPREG */
1273         old_gpreg       = INB (nc_gpreg);
1274         old_gpcntl      = INB (nc_gpcntl);
1275
1276         /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
1277            1/2/4 out */
1278         gpreg = old_gpreg & 0xe9;
1279         OUTB (nc_gpreg, gpreg);
1280         gpcntl = (old_gpcntl & 0xe9) | 0x09;
1281         OUTB (nc_gpcntl, gpcntl);
1282
1283         /* input all of NVRAM, 64 words */
1284         retv = T93C46_Read_Data(np, (u_short *) nvram,
1285                                 sizeof(*nvram) / sizeof(short), &gpreg);
1286         
1287         /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
1288         OUTB (nc_gpcntl, old_gpcntl);
1289         OUTB (nc_gpreg,  old_gpreg);
1290
1291         return retv;
1292 }
1293
1294 /*
1295  *  Try reading Tekram NVRAM.
1296  *  Return 0 if OK.
1297  */
1298 static int __init 
1299 sym_read_Tekram_nvram (ncr_slot *np, u_short device_id, Tekram_nvram *nvram)
1300 {
1301         u_char *data = (u_char *) nvram;
1302         int len = sizeof(*nvram);
1303         u_short csum;
1304         int x;
1305
1306         switch (device_id) {
1307         case PCI_DEVICE_ID_NCR_53C885:
1308         case PCI_DEVICE_ID_NCR_53C895:
1309         case PCI_DEVICE_ID_NCR_53C896:
1310                 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
1311                                           data, len);
1312                 break;
1313         case PCI_DEVICE_ID_NCR_53C875:
1314                 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
1315                                           data, len);
1316                 if (!x)
1317                         break;
1318         default:
1319                 x = sym_read_T93C46_nvram(np, nvram);
1320                 break;
1321         }
1322         if (x)
1323                 return 1;
1324
1325         /* verify checksum */
1326         for (x = 0, csum = 0; x < len - 1; x += 2)
1327                 csum += data[x] + (data[x+1] << 8);
1328         if (csum != 0x1234)
1329                 return 1;
1330
1331         return 0;
1332 }
1333
1334 #endif  /* SCSI_NCR_NVRAM_SUPPORT */
1335
1336 /*===================================================================
1337 **
1338 **    Detect and try to read SYMBIOS and TEKRAM NVRAM.
1339 **
1340 **    Data can be used to order booting of boards.
1341 **
1342 **    Data is saved in ncr_device structure if NVRAM found. This
1343 **    is then used to find drive boot order for ncr_attach().
1344 **
1345 **    NVRAM data is passed to Scsi_Host_Template later during 
1346 **    ncr_attach() for any device set up.
1347 **
1348 **===================================================================
1349 */
1350 #ifdef SCSI_NCR_NVRAM_SUPPORT
1351 static void __init ncr_get_nvram(struct ncr_device *devp, ncr_nvram *nvp)
1352 {
1353         devp->nvram = nvp;
1354         if (!nvp)
1355                 return;
1356         /*
1357         **    Get access to chip IO registers
1358         */
1359 #ifdef SCSI_NCR_IOMAPPED
1360         request_region(devp->slot.io_port, 128, NAME53C8XX);
1361         devp->slot.base_io = devp->slot.io_port;
1362 #else
1363         devp->slot.reg = 
1364                 (struct ncr_reg *) remap_pci_mem(devp->slot.base_c, 128);
1365         if (!devp->slot.reg)
1366                 return;
1367 #endif
1368
1369         /*
1370         **    Try to read SYMBIOS nvram.
1371         **    Try to read TEKRAM nvram if Symbios nvram not found.
1372         */
1373         if      (!sym_read_Symbios_nvram(&devp->slot, &nvp->data.Symbios))
1374                 nvp->type = SCSI_NCR_SYMBIOS_NVRAM;
1375         else if (!sym_read_Tekram_nvram(&devp->slot, devp->chip.device_id,
1376                                         &nvp->data.Tekram))
1377                 nvp->type = SCSI_NCR_TEKRAM_NVRAM;
1378         else {
1379                 nvp->type = 0;
1380                 devp->nvram = 0;
1381         }
1382
1383         /*
1384         ** Release access to chip IO registers
1385         */
1386 #ifdef SCSI_NCR_IOMAPPED
1387         release_region(devp->slot.base_io, 128);
1388 #else
1389         unmap_pci_mem((u_long) devp->slot.reg, 128ul);
1390 #endif
1391
1392 }
1393
1394 /*===================================================================
1395 **
1396 **      Display the content of NVRAM for debugging purpose.
1397 **
1398 **===================================================================
1399 */
1400 #ifdef  SCSI_NCR_DEBUG_NVRAM
1401 static void __init ncr_display_Symbios_nvram(Symbios_nvram *nvram)
1402 {
1403         int i;
1404
1405         /* display Symbios nvram host data */
1406         printk(KERN_DEBUG NAME53C8XX ": HOST ID=%d%s%s%s%s%s\n",
1407                 nvram->host_id & 0x0f,
1408                 (nvram->flags  & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
1409                 (nvram->flags  & SYMBIOS_PARITY_ENABLE) ? " PARITY"     :"",
1410                 (nvram->flags  & SYMBIOS_VERBOSE_MSGS)  ? " VERBOSE"    :"", 
1411                 (nvram->flags  & SYMBIOS_CHS_MAPPING)   ? " CHS_ALT"    :"", 
1412                 (nvram->flags1 & SYMBIOS_SCAN_HI_LO)    ? " HI_LO"      :"");
1413
1414         /* display Symbios nvram drive data */
1415         for (i = 0 ; i < 15 ; i++) {
1416                 struct Symbios_target *tn = &nvram->target[i];
1417                 printk(KERN_DEBUG NAME53C8XX 
1418                 "-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
1419                 i,
1420                 (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC"       : "",
1421                 (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT"  : "",
1422                 (tn->flags & SYMBIOS_SCAN_LUNS)         ? " SCAN_LUNS"  : "",
1423                 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ"        : "",
1424                 tn->bus_width,
1425                 tn->sync_period / 4,
1426                 tn->timeout);
1427         }
1428 }
1429
1430 static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
1431
1432 static void __init ncr_display_Tekram_nvram(Tekram_nvram *nvram)
1433 {
1434         int i, tags, boot_delay;
1435         char *rem;
1436
1437         /* display Tekram nvram host data */
1438         tags = 2 << nvram->max_tags_index;
1439         boot_delay = 0;
1440         if (nvram->boot_delay_index < 6)
1441                 boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
1442         switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
1443         default:
1444         case 0: rem = "";                       break;
1445         case 1: rem = " REMOVABLE=boot device"; break;
1446         case 2: rem = " REMOVABLE=all";         break;
1447         }
1448
1449         printk(KERN_DEBUG NAME53C8XX
1450                 ": HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
1451                 nvram->host_id & 0x0f,
1452                 (nvram->flags1 & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
1453                 (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES":"",
1454                 (nvram->flags & TEKRAM_DRIVES_SUP_1GB)  ? " >1GB"       :"",
1455                 (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET"    :"",
1456                 (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG"    :"",
1457                 (nvram->flags & TEKRAM_IMMEDIATE_SEEK)  ? " IMM_SEEK"   :"",
1458                 (nvram->flags & TEKRAM_SCAN_LUNS)       ? " SCAN_LUNS"  :"",
1459                 (nvram->flags1 & TEKRAM_F2_F6_ENABLED)  ? " F2_F6"      :"",
1460                 rem, boot_delay, tags);
1461
1462         /* display Tekram nvram drive data */
1463         for (i = 0; i <= 15; i++) {
1464                 int sync, j;
1465                 struct Tekram_target *tn = &nvram->target[i];
1466                 j = tn->sync_index & 0xf;
1467                 sync = Tekram_sync[j];
1468                 printk(KERN_DEBUG NAME53C8XX "-%d:%s%s%s%s%s%s PERIOD=%d\n",
1469                 i,
1470                 (tn->flags & TEKRAM_PARITY_CHECK)       ? " PARITY"     : "",
1471                 (tn->flags & TEKRAM_SYNC_NEGO)          ? " SYNC"       : "",
1472                 (tn->flags & TEKRAM_DISCONNECT_ENABLE)  ? " DISC"       : "",
1473                 (tn->flags & TEKRAM_START_CMD)          ? " START"      : "",
1474                 (tn->flags & TEKRAM_TAGGED_COMMANDS)    ? " TCQ"        : "",
1475                 (tn->flags & TEKRAM_WIDE_NEGO)          ? " WIDE"       : "",
1476                 sync);
1477         }
1478 }
1479 #endif /* SCSI_NCR_DEBUG_NVRAM */
1480 #endif  /* SCSI_NCR_NVRAM_SUPPORT */
1481
1482
1483 /*===================================================================
1484 **
1485 **      Utility routines that protperly return data through /proc FS.
1486 **
1487 **===================================================================
1488 */
1489 #ifdef SCSI_NCR_USER_INFO_SUPPORT
1490
1491 struct info_str
1492 {
1493         char *buffer;
1494         int length;
1495         int offset;
1496         int pos;
1497 };
1498
1499 static void copy_mem_info(struct info_str *info, char *data, int len)
1500 {
1501         if (info->pos + len > info->length)
1502                 len = info->length - info->pos;
1503
1504         if (info->pos + len < info->offset) {
1505                 info->pos += len;
1506                 return;
1507         }
1508         if (info->pos < info->offset) {
1509                 data += (info->offset - info->pos);
1510                 len  -= (info->offset - info->pos);
1511         }
1512
1513         if (len > 0) {
1514                 memcpy(info->buffer + info->pos, data, len);
1515                 info->pos += len;
1516         }
1517 }
1518
1519 static int copy_info(struct info_str *info, char *fmt, ...)
1520 {
1521         va_list args;
1522         char buf[81];
1523         int len;
1524
1525         va_start(args, fmt);
1526         len = vsprintf(buf, fmt, args);
1527         va_end(args);
1528
1529         copy_mem_info(info, buf, len);
1530         return len;
1531 }
1532
1533 #endif
1534
1535 /*===================================================================
1536 **
1537 **      Driver setup from the boot command line
1538 **
1539 **===================================================================
1540 */
1541
1542 #ifdef MODULE
1543 #define ARG_SEP ' '
1544 #else
1545 #define ARG_SEP ','
1546 #endif
1547
1548 #define OPT_TAGS                1
1549 #define OPT_MASTER_PARITY       2
1550 #define OPT_SCSI_PARITY         3
1551 #define OPT_DISCONNECTION       4
1552 #define OPT_SPECIAL_FEATURES    5
1553 #define OPT_UNUSED_1            6
1554 #define OPT_FORCE_SYNC_NEGO     7
1555 #define OPT_REVERSE_PROBE       8
1556 #define OPT_DEFAULT_SYNC        9
1557 #define OPT_VERBOSE             10
1558 #define OPT_DEBUG               11
1559 #define OPT_BURST_MAX           12
1560 #define OPT_LED_PIN             13
1561 #define OPT_MAX_WIDE            14
1562 #define OPT_SETTLE_DELAY        15
1563 #define OPT_DIFF_SUPPORT        16
1564 #define OPT_IRQM                17
1565 #define OPT_PCI_FIX_UP          18
1566 #define OPT_BUS_CHECK           19
1567 #define OPT_OPTIMIZE            20
1568 #define OPT_RECOVERY            21
1569 #define OPT_SAFE_SETUP          22
1570 #define OPT_USE_NVRAM           23
1571 #define OPT_EXCLUDE             24
1572 #define OPT_HOST_ID             25
1573
1574 #ifdef SCSI_NCR_IARB_SUPPORT
1575 #define OPT_IARB                26
1576 #endif
1577
1578 static char setup_token[] __initdata = 
1579         "tags:"   "mpar:"
1580         "spar:"   "disc:"
1581         "specf:"  "ultra:"
1582         "fsn:"    "revprob:"
1583         "sync:"   "verb:"
1584         "debug:"  "burst:"
1585         "led:"    "wide:"
1586         "settle:" "diff:"
1587         "irqm:"   "pcifix:"
1588         "buschk:" "optim:"
1589         "recovery:"
1590         "safe:"   "nvram:"
1591         "excl:"   "hostid:"
1592 #ifdef SCSI_NCR_IARB_SUPPORT
1593         "iarb:"
1594 #endif
1595         ;       /* DONNOT REMOVE THIS ';' */
1596
1597 #ifdef MODULE
1598 #define ARG_SEP ' '
1599 #else
1600 #define ARG_SEP ','
1601 #endif
1602
1603 static int __init get_setup_token(char *p)
1604 {
1605         char *cur = setup_token;
1606         char *pc;
1607         int i = 0;
1608
1609         while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1610                 ++pc;
1611                 ++i;
1612                 if (!strncmp(p, cur, pc - cur))
1613                         return i;
1614                 cur = pc;
1615         }
1616         return 0;
1617 }
1618
1619
1620 static int __init sym53c8xx__setup(char *str)
1621 {
1622 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
1623         char *cur = str;
1624         char *pc, *pv;
1625         int i, val, c;
1626         int xi = 0;
1627
1628         while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1629                 char *pe;
1630
1631                 val = 0;
1632                 pv = pc;
1633                 c = *++pv;
1634
1635                 if      (c == 'n')
1636                         val = 0;
1637                 else if (c == 'y')
1638                         val = 1;
1639                 else
1640                         val = (int) simple_strtoul(pv, &pe, 0);
1641
1642                 switch (get_setup_token(cur)) {
1643                 case OPT_TAGS:
1644                         driver_setup.default_tags = val;
1645                         if (pe && *pe == '/') {
1646                                 i = 0;
1647                                 while (*pe && *pe != ARG_SEP && 
1648                                         i < sizeof(driver_setup.tag_ctrl)-1) {
1649                                         driver_setup.tag_ctrl[i++] = *pe++;
1650                                 }
1651                                 driver_setup.tag_ctrl[i] = '\0';
1652                         }
1653                         break;
1654                 case OPT_MASTER_PARITY:
1655                         driver_setup.master_parity = val;
1656                         break;
1657                 case OPT_SCSI_PARITY:
1658                         driver_setup.scsi_parity = val;
1659                         break;
1660                 case OPT_DISCONNECTION:
1661                         driver_setup.disconnection = val;
1662                         break;
1663                 case OPT_SPECIAL_FEATURES:
1664                         driver_setup.special_features = val;
1665                         break;
1666                 case OPT_FORCE_SYNC_NEGO:
1667                         driver_setup.force_sync_nego = val;
1668                         break;
1669                 case OPT_REVERSE_PROBE:
1670                         driver_setup.reverse_probe = val;
1671                         break;
1672                 case OPT_DEFAULT_SYNC:
1673                         driver_setup.default_sync = val;
1674                         break;
1675                 case OPT_VERBOSE:
1676                         driver_setup.verbose = val;
1677                         break;
1678                 case OPT_DEBUG:
1679                         driver_setup.debug = val;
1680                         break;
1681                 case OPT_BURST_MAX:
1682                         driver_setup.burst_max = val;
1683                         break;
1684                 case OPT_LED_PIN:
1685                         driver_setup.led_pin = val;
1686                         break;
1687                 case OPT_MAX_WIDE:
1688                         driver_setup.max_wide = val? 1:0;
1689                         break;
1690                 case OPT_SETTLE_DELAY:
1691                         driver_setup.settle_delay = val;
1692                         break;
1693                 case OPT_DIFF_SUPPORT:
1694                         driver_setup.diff_support = val;
1695                         break;
1696                 case OPT_IRQM:
1697                         driver_setup.irqm = val;
1698                         break;
1699                 case OPT_PCI_FIX_UP:
1700                         driver_setup.pci_fix_up = val;
1701                         break;
1702                 case OPT_BUS_CHECK:
1703                         driver_setup.bus_check = val;
1704                         break;
1705                 case OPT_OPTIMIZE:
1706                         driver_setup.optimize = val;
1707                         break;
1708                 case OPT_RECOVERY:
1709                         driver_setup.recovery = val;
1710                         break;
1711                 case OPT_USE_NVRAM:
1712                         driver_setup.use_nvram = val;
1713                         break;
1714                 case OPT_SAFE_SETUP:
1715                         memcpy(&driver_setup, &driver_safe_setup,
1716                                 sizeof(driver_setup));
1717                         break;
1718                 case OPT_EXCLUDE:
1719                         if (xi < SCSI_NCR_MAX_EXCLUDES)
1720                                 driver_setup.excludes[xi++] = val;
1721                         break;
1722                 case OPT_HOST_ID:
1723                         driver_setup.host_id = val;
1724                         break;
1725 #ifdef SCSI_NCR_IARB_SUPPORT
1726                 case OPT_IARB:
1727                         driver_setup.iarb = val;
1728                         break;
1729 #endif
1730                 default:
1731                         printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
1732                         break;
1733                 }
1734
1735                 if ((cur = strchr(cur, ARG_SEP)) != NULL)
1736                         ++cur;
1737         }
1738 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
1739         return 1;
1740 }
1741
1742 /*===================================================================
1743 **
1744 **      Get device queue depth from boot command line.
1745 **
1746 **===================================================================
1747 */
1748 #define DEF_DEPTH       (driver_setup.default_tags)
1749 #define ALL_TARGETS     -2
1750 #define NO_TARGET       -1
1751 #define ALL_LUNS        -2
1752 #define NO_LUN          -1
1753
1754 static int device_queue_depth(int unit, int target, int lun)
1755 {
1756         int c, h, t, u, v;
1757         char *p = driver_setup.tag_ctrl;
1758         char *ep;
1759
1760         h = -1;
1761         t = NO_TARGET;
1762         u = NO_LUN;
1763         while ((c = *p++) != 0) {
1764                 v = simple_strtoul(p, &ep, 0);
1765                 switch(c) {
1766                 case '/':
1767                         ++h;
1768                         t = ALL_TARGETS;
1769                         u = ALL_LUNS;
1770                         break;
1771                 case 't':
1772                         if (t != target)
1773                                 t = (target == v) ? v : NO_TARGET;
1774                         u = ALL_LUNS;
1775                         break;
1776                 case 'u':
1777                         if (u != lun)
1778                                 u = (lun == v) ? v : NO_LUN;
1779                         break;
1780                 case 'q':
1781                         if (h == unit &&
1782                                 (t == ALL_TARGETS || t == target) &&
1783                                 (u == ALL_LUNS    || u == lun))
1784                                 return v;
1785                         break;
1786                 case '-':
1787                         t = ALL_TARGETS;
1788                         u = ALL_LUNS;
1789                         break;
1790                 default:
1791                         break;
1792                 }
1793                 p = ep;
1794         }
1795         return DEF_DEPTH;
1796 }