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