1 /******************************************************************************
2 ** High Performance device driver for the Symbios 53C896 controller.
4 ** Copyright (C) 1998-2001 Gerard Roudier <groudier@free.fr>
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.
10 ** This driver is based on the Linux port of the FreeBSD ncr driver.
12 ** Copyright (C) 1994 Wolfgang Stanglmeier
14 **-----------------------------------------------------------------------------
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.
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.
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.
30 **-----------------------------------------------------------------------------
32 ** The Linux port of the FreeBSD ncr driver has been achieved in
35 ** Gerard Roudier <groudier@free.fr>
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
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>
46 **-----------------------------------------------------------------------------
48 ** Major contributions:
49 ** --------------------
51 ** NVRAM detection and reading.
52 ** Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
54 *******************************************************************************
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.
65 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
66 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
68 /*==========================================================
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! ;-)
74 **==========================================================
77 #define SCSI_NCR_DYNAMIC_DMA_MAPPING
79 /*==========================================================
81 ** Miscallaneous defines.
83 **==========================================================
86 #define u_char unsigned char
87 #define u_short unsigned short
88 #define u_int unsigned int
89 #define u_long unsigned long
92 #define bcmp(s, d, n) memcmp((d), (s), (n))
96 #define bzero(d, n) memset((d), 0, (n))
100 #define offsetof(t, m) ((size_t) (&((t *)0)->m))
103 /*==========================================================
107 **==========================================================
109 ** modified copy from 386bsd:/usr/include/sys/assert.h
111 **----------------------------------------------------------
114 #define assert(expression) { \
115 if (!(expression)) { \
117 "assertion \"%s\" failed: file \"%s\", line %d\n", \
119 __FILE__, __LINE__); \
123 /*==========================================================
127 **==========================================================
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)
144 ** Enable/Disable debug messages.
145 ** Can be changed at runtime too.
148 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
149 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
150 #define DEBUG_FLAGS ncr_debug
152 #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
155 /*==========================================================
157 ** A la VMS/CAM-3 queue management.
158 ** Implemented from linux list management.
160 **==========================================================
163 typedef struct xpt_quehead {
164 struct xpt_quehead *flink; /* Forward pointer */
165 struct xpt_quehead *blink; /* Backward pointer */
168 #define xpt_que_init(ptr) do { \
169 (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
172 static inline void __xpt_que_add(struct xpt_quehead * new,
173 struct xpt_quehead * blink,
174 struct xpt_quehead * flink)
182 static inline void __xpt_que_del(struct xpt_quehead * blink,
183 struct xpt_quehead * flink)
185 flink->blink = blink;
186 blink->flink = flink;
189 static inline int xpt_que_empty(struct xpt_quehead *head)
191 return head->flink == head;
194 static inline void xpt_que_splice(struct xpt_quehead *list,
195 struct xpt_quehead *head)
197 struct xpt_quehead *first = list->flink;
200 struct xpt_quehead *last = list->blink;
201 struct xpt_quehead *at = head->flink;
211 #define xpt_que_entry(ptr, type, member) \
212 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
215 #define xpt_insque(new, pos) __xpt_que_add(new, pos, (pos)->flink)
217 #define xpt_remque(el) __xpt_que_del((el)->blink, (el)->flink)
219 #define xpt_insque_head(new, head) __xpt_que_add(new, head, (head)->flink)
221 static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
223 struct xpt_quehead *elem = head->flink;
226 __xpt_que_del(head, elem->flink);
232 #define xpt_insque_tail(new, head) __xpt_que_add(new, (head)->blink, head)
234 static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
236 struct xpt_quehead *elem = head->blink;
239 __xpt_que_del(elem->blink, head);
246 /*==========================================================
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
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).
264 **==========================================================
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)
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)
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)
281 /*==========================================================
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.
293 **==========================================================
300 #define memcpy_to_pci(a, b, c) memcpy_toio((a), (b), (c))
302 /*==========================================================
304 ** Insert a delay in micro-seconds and milli-seconds.
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.
313 **==========================================================
316 #define UDELAY udelay
317 #define MDELAY mdelay
319 /*==========================================================
321 ** Simple power of two buddy-like allocator.
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).
334 **==========================================================
337 #define __GetFreePages(flags, order) __get_free_pages(flags, order)
339 #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
340 #if PAGE_SIZE >= 8192
341 #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
343 #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
345 #define MEMO_FREE_UNUSED /* Free unused pages immediately */
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)
352 typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
353 typedef struct device *m_bush_t; /* Something that addresses DMAable */
355 typedef struct m_link { /* Link between free memory chunks */
359 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
360 typedef struct m_vtob { /* Virtual to Bus address translation */
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)
372 typedef struct m_pool { /* Memory pool of a given kind */
373 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
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)
382 m_vtob_s *(vtob[VTOB_HASH_SIZE]);
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];
391 static void *___m_alloc(m_pool_s *mp, int size)
394 int s = (1 << MEMO_SHIFT);
399 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
409 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
410 h[j].next = (m_link_s *) M_GETP();
418 a = (m_addr_t) h[j].next;
420 h[j].next = h[j].next->next;
424 h[j].next = (m_link_s *) (a+s);
429 printk("___m_alloc(%d) = %p\n", size, (void *) a);
434 static void ___m_free(m_pool_s *mp, void *ptr, int size)
437 int s = (1 << MEMO_SHIFT);
443 printk("___m_free(%p, %d)\n", ptr, size);
446 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
457 #ifdef MEMO_FREE_UNUSED
458 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
465 while (q->next && q->next != (m_link_s *) b) {
469 ((m_link_s *) a)->next = h[i].next;
470 h[i].next = (m_link_s *) a;
473 q->next = q->next->next;
480 static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
484 p = ___m_alloc(mp, size);
486 if (DEBUG_FLAGS & DEBUG_ALLOC)
487 printk ("new %-10s[%4d] @%p.\n", name, size, p);
491 else if (uflags & MEMO_WARN)
492 printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
497 #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
499 static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
501 if (DEBUG_FLAGS & DEBUG_ALLOC)
502 printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
504 ___m_free(mp, ptr, size);
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.
514 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
520 static m_addr_t ___mp0_getp(m_pool_s *mp)
522 m_addr_t m = GetPages();
528 static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
534 static m_pool_s mp0 = {0, ___mp0_getp, ___mp0_freep};
536 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
542 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
544 /* Without pci bus iommu support, all the memory is assumed DMAable */
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)
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.
556 static m_addr_t ___dma_getp(m_pool_s *mp)
561 vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
564 vp = (m_addr_t) dma_alloc_coherent(mp->bush,
565 PAGE_SIZE<<MEMO_PAGE_ORDER,
568 int hc = VTOB_HASH_CODE(vp);
571 vbp->next = mp->vtob[hc];
578 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
582 static void ___dma_freep(m_pool_s *mp, m_addr_t m)
584 m_vtob_s **vbpp, *vbp;
585 int hc = VTOB_HASH_CODE(m);
587 vbpp = &mp->vtob[hc];
588 while (*vbpp && (*vbpp)->vaddr != m)
589 vbpp = &(*vbpp)->next;
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");
600 static inline m_pool_s *___get_dma_pool(m_bush_t bush)
603 for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
607 static m_pool_s *___cre_dma_pool(m_bush_t bush)
610 mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
612 bzero(mp, sizeof(*mp));
614 mp->getp = ___dma_getp;
615 mp->freep = ___dma_freep;
622 static void ___del_dma_pool(m_pool_s *p)
624 struct m_pool **pp = &mp0.next;
626 while (*pp && *pp != p)
630 __m_free(&mp0, p, sizeof(*p), "MPOOL");
634 static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
640 NCR_LOCK_DRIVER(flags);
641 mp = ___get_dma_pool(bush);
643 mp = ___cre_dma_pool(bush);
645 m = __m_calloc(mp, size, name);
648 NCR_UNLOCK_DRIVER(flags);
653 static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
658 NCR_LOCK_DRIVER(flags);
659 mp = ___get_dma_pool(bush);
661 __m_free(mp, m, size, name);
664 NCR_UNLOCK_DRIVER(flags);
667 static m_addr_t __vtobus(m_bush_t bush, void *m)
671 int hc = VTOB_HASH_CODE(m);
673 m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
675 NCR_LOCK_DRIVER(flags);
676 mp = ___get_dma_pool(bush);
679 while (vp && (m_addr_t) vp->vaddr != a)
682 NCR_UNLOCK_DRIVER(flags);
683 return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
686 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
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)
696 * Deal with DMA mapping/unmapping.
699 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
701 /* Linux versions prior to pci bus iommu kernel interface */
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)
709 #define scsi_sg_dma_address(sc) vtobus((sc)->address)
710 #define scsi_sg_dma_len(sc) ((sc)->length)
714 /* Linux version with pci bus iommu kernel interface */
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
720 static void __unmap_scsi_data(struct device *dev, Scsi_Cmnd *cmd)
722 enum dma_data_direction dma_dir =
723 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
725 switch(cmd->__data_mapped) {
727 dma_unmap_sg(dev, cmd->buffer, cmd->use_sg, dma_dir);
730 dma_unmap_single(dev, cmd->__data_mapping,
731 cmd->request_bufflen, dma_dir);
734 cmd->__data_mapped = 0;
737 static u_long __map_scsi_single_data(struct device *dev, Scsi_Cmnd *cmd)
740 enum dma_data_direction dma_dir =
741 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
744 if (cmd->request_bufflen == 0)
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;
755 static int __map_scsi_sg_data(struct device *dev, Scsi_Cmnd *cmd)
758 enum dma_data_direction dma_dir =
759 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
761 if (cmd->use_sg == 0)
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;
771 static void __sync_scsi_data_for_cpu(struct device *dev, Scsi_Cmnd *cmd)
773 enum dma_data_direction dma_dir =
774 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
776 switch(cmd->__data_mapped) {
778 dma_sync_sg_for_cpu(dev, cmd->buffer, cmd->use_sg, dma_dir);
781 dma_sync_single_for_cpu(dev, cmd->__data_mapping,
782 cmd->request_bufflen, dma_dir);
787 static void __sync_scsi_data_for_device(struct device *dev, Scsi_Cmnd *cmd)
789 enum dma_data_direction dma_dir =
790 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
792 switch(cmd->__data_mapped) {
794 dma_sync_sg_for_device(dev, cmd->buffer, cmd->use_sg, dma_dir);
797 dma_sync_single_for_device(dev, cmd->__data_mapping,
798 cmd->request_bufflen, dma_dir);
803 #define scsi_sg_dma_address(sc) sg_dma_address(sc)
804 #define scsi_sg_dma_len(sc) sg_dma_len(sc)
806 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
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)
814 /*==========================================================
816 ** SCSI data transfer direction
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. ;-)
827 **==========================================================
829 #ifdef SCSI_DATA_UNKNOWN
831 #define scsi_data_direction(cmd) (cmd->sc_data_direction)
835 #define SCSI_DATA_UNKNOWN 0
836 #define SCSI_DATA_WRITE 1
837 #define SCSI_DATA_READ 2
838 #define SCSI_DATA_NONE 3
840 static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
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;
850 case 0x0A: /* WRITE(6) 0A */
851 case 0x2A: /* WRITE(10) 2A */
852 case 0xAA: /* WRITE(12) AA */
853 direction = SCSI_DATA_WRITE;
856 direction = SCSI_DATA_UNKNOWN;
863 #endif /* SCSI_DATA_UNKNOWN */
865 /*==========================================================
869 ** This structure is initialized from linux config
870 ** options. It can be overridden at boot-up by the boot
873 **==========================================================
875 static struct ncr_driver_setup
876 driver_setup = SCSI_NCR_DRIVER_SETUP;
878 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
879 static struct ncr_driver_setup
880 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
883 #define initverbose (driver_setup.verbose)
884 #define bootverbose (np->verbose)
887 /*==========================================================
889 ** NVRAM detection and reading.
891 ** Currently supported:
892 ** - 24C16 EEPROM with both Symbios and Tekram layout.
893 ** - 93C46 EEPROM with Tekram layout.
895 **==========================================================
898 #ifdef SCSI_NCR_NVRAM_SUPPORT
900 * 24C16 EEPROM reading.
902 * GPOI0 - data in/data out
904 * Symbios NVRAM wiring now also used by Tekram.
913 * Set/clear data/clock bit in GPIO0
916 S24C16_set_bit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode)
934 OUTB (nc_gpreg, *gpreg);
939 * Send START condition to NVRAM to wake it up.
941 static void __init S24C16_start(ncr_slot *np, u_char *gpreg)
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);
950 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
952 static void __init S24C16_stop(ncr_slot *np, u_char *gpreg)
954 S24C16_set_bit(np, 0, gpreg, SET_CLK);
955 S24C16_set_bit(np, 1, gpreg, SET_BIT);
959 * Read or write a bit to the NVRAM,
960 * read if GPIO0 input else write if GPIO0 output
963 S24C16_do_bit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg)
965 S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
966 S24C16_set_bit(np, 0, gpreg, SET_CLK);
968 *read_bit = INB (nc_gpreg);
969 S24C16_set_bit(np, 0, gpreg, CLR_CLK);
970 S24C16_set_bit(np, 0, gpreg, CLR_BIT);
974 * Output an ACK to the NVRAM after reading,
975 * change GPIO0 to output and when done back to an input
978 S24C16_write_ack(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl)
980 OUTB (nc_gpcntl, *gpcntl & 0xfe);
981 S24C16_do_bit(np, 0, write_bit, gpreg);
982 OUTB (nc_gpcntl, *gpcntl);
986 * Input an ACK from NVRAM after writing,
987 * change GPIO0 to input and when done back to an output
990 S24C16_read_ack(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl)
992 OUTB (nc_gpcntl, *gpcntl | 0x01);
993 S24C16_do_bit(np, read_bit, 1, gpreg);
994 OUTB (nc_gpcntl, *gpcntl);
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
1002 S24C16_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data,
1003 u_char *gpreg, u_char *gpcntl)
1007 for (x = 0; x < 8; x++)
1008 S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
1010 S24C16_read_ack(np, ack_data, gpreg, gpcntl);
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
1018 S24C16_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data,
1019 u_char *gpreg, u_char *gpcntl)
1025 for (x = 0; x < 8; x++) {
1026 S24C16_do_bit(np, &read_bit, 1, gpreg);
1027 *read_data |= ((read_bit & 0x01) << (7 - x));
1030 S24C16_write_ack(np, ack_data, gpreg, gpcntl);
1034 * Read 'len' bytes starting at 'offset'.
1037 sym_read_S24C16_nvram (ncr_slot *np, int offset, u_char *data, int len)
1039 u_char gpcntl, gpreg;
1040 u_char old_gpcntl, old_gpreg;
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;
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);
1054 /* this is to set NVRAM into a known state with GPIO0/1 both low */
1056 S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
1057 S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
1059 /* now set NVRAM inactive with GPIO0/1 both high */
1060 S24C16_stop(np, &gpreg);
1062 /* activate NVRAM */
1063 S24C16_start(np, &gpreg);
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)
1071 /* write random address LSB */
1072 S24C16_write_byte(np, &ack_data,
1073 offset & 0xff, &gpreg, &gpcntl);
1074 if (ack_data & 0x01)
1077 /* regenerate START state to set up for reading */
1078 S24C16_start(np, &gpreg);
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)
1086 /* now set up GPIO0 for inputting data */
1088 OUTB (nc_gpcntl, gpcntl);
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);
1094 /* finally put NVRAM back in inactive mode */
1096 OUTB (nc_gpcntl, gpcntl);
1097 S24C16_stop(np, &gpreg);
1100 /* return GPIO0/1 to original states after having accessed NVRAM */
1101 OUTB (nc_gpcntl, old_gpcntl);
1102 OUTB (nc_gpreg, old_gpreg);
1113 * Try reading Symbios NVRAM.
1116 static int __init sym_read_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
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);
1124 /* probe the 24c16 and read the SYMBIOS 24c16 area */
1125 if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
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)
1134 /* verify checksum */
1135 for (x = 6, csum = 0; x < len - 6; x++)
1137 if (csum != nvram->checksum)
1144 * 93C46 EEPROM reading.
1149 * GPIO4 - chip select
1155 * Pulse clock bit in GPIO0
1157 static void __init T93C46_Clk(ncr_slot *np, u_char *gpreg)
1159 OUTB (nc_gpreg, *gpreg | 0x04);
1161 OUTB (nc_gpreg, *gpreg);
1165 * Read bit from NVRAM
1167 static void __init T93C46_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg)
1170 T93C46_Clk(np, gpreg);
1171 *read_bit = INB (nc_gpreg);
1175 * Write bit to GPIO0
1177 static void __init T93C46_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg)
1179 if (write_bit & 0x01)
1186 OUTB (nc_gpreg, *gpreg);
1189 T93C46_Clk(np, gpreg);
1193 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
1195 static void __init T93C46_Stop(ncr_slot *np, u_char *gpreg)
1198 OUTB (nc_gpreg, *gpreg);
1201 T93C46_Clk(np, gpreg);
1205 * Send read command and address to NVRAM
1208 T93C46_Send_Command(ncr_slot *np, u_short write_data,
1209 u_char *read_bit, u_char *gpreg)
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);
1217 *read_bit = INB (nc_gpreg);
1221 * READ 2 bytes from the NVRAM
1224 T93C46_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg)
1230 for (x = 0; x < 16; x++) {
1231 T93C46_Read_Bit(np, &read_bit, gpreg);
1233 if (read_bit & 0x01)
1234 *nvram_data |= (0x01 << (15 - x));
1236 *nvram_data &= ~(0x01 << (15 - x));
1241 * Read Tekram NvRAM data.
1244 T93C46_Read_Data(ncr_slot *np, u_short *data,int len,u_char *gpreg)
1249 for (x = 0; x < len; x++) {
1251 /* output read command and address */
1252 T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
1253 if (read_bit & 0x01)
1255 T93C46_Read_Word(np, &data[x], gpreg);
1256 T93C46_Stop(np, gpreg);
1263 * Try reading 93C46 Tekram NVRAM.
1266 sym_read_T93C46_nvram (ncr_slot *np, Tekram_nvram *nvram)
1268 u_char gpcntl, gpreg;
1269 u_char old_gpcntl, old_gpreg;
1272 /* save current state of GPCNTL and GPREG */
1273 old_gpreg = INB (nc_gpreg);
1274 old_gpcntl = INB (nc_gpcntl);
1276 /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
1278 gpreg = old_gpreg & 0xe9;
1279 OUTB (nc_gpreg, gpreg);
1280 gpcntl = (old_gpcntl & 0xe9) | 0x09;
1281 OUTB (nc_gpcntl, gpcntl);
1283 /* input all of NVRAM, 64 words */
1284 retv = T93C46_Read_Data(np, (u_short *) nvram,
1285 sizeof(*nvram) / sizeof(short), &gpreg);
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);
1295 * Try reading Tekram NVRAM.
1299 sym_read_Tekram_nvram (ncr_slot *np, u_short device_id, Tekram_nvram *nvram)
1301 u_char *data = (u_char *) nvram;
1302 int len = sizeof(*nvram);
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,
1313 case PCI_DEVICE_ID_NCR_53C875:
1314 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
1319 x = sym_read_T93C46_nvram(np, nvram);
1325 /* verify checksum */
1326 for (x = 0, csum = 0; x < len - 1; x += 2)
1327 csum += data[x] + (data[x+1] << 8);
1334 #endif /* SCSI_NCR_NVRAM_SUPPORT */
1336 /*===================================================================
1338 ** Detect and try to read SYMBIOS and TEKRAM NVRAM.
1340 ** Data can be used to order booting of boards.
1342 ** Data is saved in ncr_device structure if NVRAM found. This
1343 ** is then used to find drive boot order for ncr_attach().
1345 ** NVRAM data is passed to Scsi_Host_Template later during
1346 ** ncr_attach() for any device set up.
1348 **===================================================================
1350 #ifdef SCSI_NCR_NVRAM_SUPPORT
1351 static void __init ncr_get_nvram(struct ncr_device *devp, ncr_nvram *nvp)
1357 ** Get access to chip IO registers
1359 #ifdef SCSI_NCR_IOMAPPED
1360 request_region(devp->slot.io_port, 128, NAME53C8XX);
1361 devp->slot.base_io = devp->slot.io_port;
1364 (struct ncr_reg *) remap_pci_mem(devp->slot.base_c, 128);
1365 if (!devp->slot.reg)
1370 ** Try to read SYMBIOS nvram.
1371 ** Try to read TEKRAM nvram if Symbios nvram not found.
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,
1377 nvp->type = SCSI_NCR_TEKRAM_NVRAM;
1384 ** Release access to chip IO registers
1386 #ifdef SCSI_NCR_IOMAPPED
1387 release_region(devp->slot.base_io, 128);
1389 unmap_pci_mem((u_long) devp->slot.reg, 128ul);
1394 /*===================================================================
1396 ** Display the content of NVRAM for debugging purpose.
1398 **===================================================================
1400 #ifdef SCSI_NCR_DEBUG_NVRAM
1401 static void __init ncr_display_Symbios_nvram(Symbios_nvram *nvram)
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" :"");
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",
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" : "",
1425 tn->sync_period / 4,
1430 static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
1432 static void __init ncr_display_Tekram_nvram(Tekram_nvram *nvram)
1434 int i, tags, boot_delay;
1437 /* display Tekram nvram host data */
1438 tags = 2 << nvram->max_tags_index;
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) {
1444 case 0: rem = ""; break;
1445 case 1: rem = " REMOVABLE=boot device"; break;
1446 case 2: rem = " REMOVABLE=all"; break;
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);
1462 /* display Tekram nvram drive data */
1463 for (i = 0; i <= 15; i++) {
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",
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" : "",
1479 #endif /* SCSI_NCR_DEBUG_NVRAM */
1480 #endif /* SCSI_NCR_NVRAM_SUPPORT */
1483 /*===================================================================
1485 ** Utility routines that protperly return data through /proc FS.
1487 **===================================================================
1489 #ifdef SCSI_NCR_USER_INFO_SUPPORT
1499 static void copy_mem_info(struct info_str *info, char *data, int len)
1501 if (info->pos + len > info->length)
1502 len = info->length - info->pos;
1504 if (info->pos + len < info->offset) {
1508 if (info->pos < info->offset) {
1509 data += (info->offset - info->pos);
1510 len -= (info->offset - info->pos);
1514 memcpy(info->buffer + info->pos, data, len);
1519 static int copy_info(struct info_str *info, char *fmt, ...)
1525 va_start(args, fmt);
1526 len = vsprintf(buf, fmt, args);
1529 copy_mem_info(info, buf, len);
1535 /*===================================================================
1537 ** Driver setup from the boot command line
1539 **===================================================================
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
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
1574 #ifdef SCSI_NCR_IARB_SUPPORT
1578 static char setup_token[] __initdata =
1592 #ifdef SCSI_NCR_IARB_SUPPORT
1595 ; /* DONNOT REMOVE THIS ';' */
1603 static int __init get_setup_token(char *p)
1605 char *cur = setup_token;
1609 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1612 if (!strncmp(p, cur, pc - cur))
1620 static int __init sym53c8xx__setup(char *str)
1622 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
1628 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1640 val = (int) simple_strtoul(pv, &pe, 0);
1642 switch (get_setup_token(cur)) {
1644 driver_setup.default_tags = val;
1645 if (pe && *pe == '/') {
1647 while (*pe && *pe != ARG_SEP &&
1648 i < sizeof(driver_setup.tag_ctrl)-1) {
1649 driver_setup.tag_ctrl[i++] = *pe++;
1651 driver_setup.tag_ctrl[i] = '\0';
1654 case OPT_MASTER_PARITY:
1655 driver_setup.master_parity = val;
1657 case OPT_SCSI_PARITY:
1658 driver_setup.scsi_parity = val;
1660 case OPT_DISCONNECTION:
1661 driver_setup.disconnection = val;
1663 case OPT_SPECIAL_FEATURES:
1664 driver_setup.special_features = val;
1666 case OPT_FORCE_SYNC_NEGO:
1667 driver_setup.force_sync_nego = val;
1669 case OPT_REVERSE_PROBE:
1670 driver_setup.reverse_probe = val;
1672 case OPT_DEFAULT_SYNC:
1673 driver_setup.default_sync = val;
1676 driver_setup.verbose = val;
1679 driver_setup.debug = val;
1682 driver_setup.burst_max = val;
1685 driver_setup.led_pin = val;
1688 driver_setup.max_wide = val? 1:0;
1690 case OPT_SETTLE_DELAY:
1691 driver_setup.settle_delay = val;
1693 case OPT_DIFF_SUPPORT:
1694 driver_setup.diff_support = val;
1697 driver_setup.irqm = val;
1699 case OPT_PCI_FIX_UP:
1700 driver_setup.pci_fix_up = val;
1703 driver_setup.bus_check = val;
1706 driver_setup.optimize = val;
1709 driver_setup.recovery = val;
1712 driver_setup.use_nvram = val;
1714 case OPT_SAFE_SETUP:
1715 memcpy(&driver_setup, &driver_safe_setup,
1716 sizeof(driver_setup));
1719 if (xi < SCSI_NCR_MAX_EXCLUDES)
1720 driver_setup.excludes[xi++] = val;
1723 driver_setup.host_id = val;
1725 #ifdef SCSI_NCR_IARB_SUPPORT
1727 driver_setup.iarb = val;
1731 printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
1735 if ((cur = strchr(cur, ARG_SEP)) != NULL)
1738 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
1742 /*===================================================================
1744 ** Get device queue depth from boot command line.
1746 **===================================================================
1748 #define DEF_DEPTH (driver_setup.default_tags)
1749 #define ALL_TARGETS -2
1750 #define NO_TARGET -1
1754 static int device_queue_depth(int unit, int target, int lun)
1757 char *p = driver_setup.tag_ctrl;
1763 while ((c = *p++) != 0) {
1764 v = simple_strtoul(p, &ep, 0);
1773 t = (target == v) ? v : NO_TARGET;
1778 u = (lun == v) ? v : NO_LUN;
1782 (t == ALL_TARGETS || t == target) &&
1783 (u == ALL_LUNS || u == lun))