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 /*==========================================================
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! ;-)
71 **==========================================================
74 #define SCSI_NCR_DYNAMIC_DMA_MAPPING
76 /*==========================================================
78 ** Miscallaneous defines.
80 **==========================================================
83 #define u_char unsigned char
84 #define u_short unsigned short
85 #define u_int unsigned int
86 #define u_long unsigned long
89 #define bcmp(s, d, n) memcmp((d), (s), (n))
93 #define bzero(d, n) memset((d), 0, (n))
97 #define offsetof(t, m) ((size_t) (&((t *)0)->m))
100 /*==========================================================
104 **==========================================================
106 ** modified copy from 386bsd:/usr/include/sys/assert.h
108 **----------------------------------------------------------
111 #define assert(expression) { \
112 if (!(expression)) { \
114 "assertion \"%s\" failed: file \"%s\", line %d\n", \
116 __FILE__, __LINE__); \
120 /*==========================================================
124 **==========================================================
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)
141 ** Enable/Disable debug messages.
142 ** Can be changed at runtime too.
145 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
146 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
147 #define DEBUG_FLAGS ncr_debug
149 #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
152 /*==========================================================
154 ** A la VMS/CAM-3 queue management.
155 ** Implemented from linux list management.
157 **==========================================================
160 typedef struct xpt_quehead {
161 struct xpt_quehead *flink; /* Forward pointer */
162 struct xpt_quehead *blink; /* Backward pointer */
165 #define xpt_que_init(ptr) do { \
166 (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
169 static inline void __xpt_que_add(struct xpt_quehead * new,
170 struct xpt_quehead * blink,
171 struct xpt_quehead * flink)
179 static inline void __xpt_que_del(struct xpt_quehead * blink,
180 struct xpt_quehead * flink)
182 flink->blink = blink;
183 blink->flink = flink;
186 static inline int xpt_que_empty(struct xpt_quehead *head)
188 return head->flink == head;
191 static inline void xpt_que_splice(struct xpt_quehead *list,
192 struct xpt_quehead *head)
194 struct xpt_quehead *first = list->flink;
197 struct xpt_quehead *last = list->blink;
198 struct xpt_quehead *at = head->flink;
208 #define xpt_que_entry(ptr, type, member) \
209 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
212 #define xpt_insque(new, pos) __xpt_que_add(new, pos, (pos)->flink)
214 #define xpt_remque(el) __xpt_que_del((el)->blink, (el)->flink)
216 #define xpt_insque_head(new, head) __xpt_que_add(new, head, (head)->flink)
218 static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
220 struct xpt_quehead *elem = head->flink;
223 __xpt_que_del(head, elem->flink);
229 #define xpt_insque_tail(new, head) __xpt_que_add(new, (head)->blink, head)
231 static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
233 struct xpt_quehead *elem = head->blink;
236 __xpt_que_del(elem->blink, head);
243 /*==========================================================
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
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).
261 **==========================================================
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)
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)
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)
278 /*==========================================================
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.
290 **==========================================================
297 #define memcpy_to_pci(a, b, c) memcpy_toio((a), (b), (c))
299 /*==========================================================
301 ** Insert a delay in micro-seconds and milli-seconds.
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.
310 **==========================================================
313 #define UDELAY udelay
314 #define MDELAY mdelay
316 /*==========================================================
318 ** Simple power of two buddy-like allocator.
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).
331 **==========================================================
334 #define __GetFreePages(flags, order) __get_free_pages(flags, order)
336 #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
337 #if PAGE_SIZE >= 8192
338 #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
340 #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
342 #define MEMO_FREE_UNUSED /* Free unused pages immediately */
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)
349 typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
350 typedef struct device *m_bush_t; /* Something that addresses DMAable */
352 typedef struct m_link { /* Link between free memory chunks */
356 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
357 typedef struct m_vtob { /* Virtual to Bus address translation */
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)
369 typedef struct m_pool { /* Memory pool of a given kind */
370 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
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)
379 m_vtob_s *(vtob[VTOB_HASH_SIZE]);
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];
388 static void *___m_alloc(m_pool_s *mp, int size)
391 int s = (1 << MEMO_SHIFT);
396 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
406 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
407 h[j].next = (m_link_s *) M_GETP();
415 a = (m_addr_t) h[j].next;
417 h[j].next = h[j].next->next;
421 h[j].next = (m_link_s *) (a+s);
426 printk("___m_alloc(%d) = %p\n", size, (void *) a);
431 static void ___m_free(m_pool_s *mp, void *ptr, int size)
434 int s = (1 << MEMO_SHIFT);
440 printk("___m_free(%p, %d)\n", ptr, size);
443 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
454 #ifdef MEMO_FREE_UNUSED
455 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
462 while (q->next && q->next != (m_link_s *) b) {
466 ((m_link_s *) a)->next = h[i].next;
467 h[i].next = (m_link_s *) a;
470 q->next = q->next->next;
477 static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
481 p = ___m_alloc(mp, size);
483 if (DEBUG_FLAGS & DEBUG_ALLOC)
484 printk ("new %-10s[%4d] @%p.\n", name, size, p);
488 else if (uflags & MEMO_WARN)
489 printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
494 #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
496 static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
498 if (DEBUG_FLAGS & DEBUG_ALLOC)
499 printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
501 ___m_free(mp, ptr, size);
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.
511 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
517 static m_addr_t ___mp0_getp(m_pool_s *mp)
519 m_addr_t m = GetPages();
525 static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
531 static m_pool_s mp0 = {0, ___mp0_getp, ___mp0_freep};
533 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
539 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
541 /* Without pci bus iommu support, all the memory is assumed DMAable */
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)
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.
553 static m_addr_t ___dma_getp(m_pool_s *mp)
558 vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
561 vp = (m_addr_t) dma_alloc_coherent(mp->bush,
562 PAGE_SIZE<<MEMO_PAGE_ORDER,
565 int hc = VTOB_HASH_CODE(vp);
568 vbp->next = mp->vtob[hc];
575 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
579 static void ___dma_freep(m_pool_s *mp, m_addr_t m)
581 m_vtob_s **vbpp, *vbp;
582 int hc = VTOB_HASH_CODE(m);
584 vbpp = &mp->vtob[hc];
585 while (*vbpp && (*vbpp)->vaddr != m)
586 vbpp = &(*vbpp)->next;
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");
597 static inline m_pool_s *___get_dma_pool(m_bush_t bush)
600 for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
604 static m_pool_s *___cre_dma_pool(m_bush_t bush)
607 mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
609 bzero(mp, sizeof(*mp));
611 mp->getp = ___dma_getp;
612 mp->freep = ___dma_freep;
619 static void ___del_dma_pool(m_pool_s *p)
621 struct m_pool **pp = &mp0.next;
623 while (*pp && *pp != p)
627 __m_free(&mp0, p, sizeof(*p), "MPOOL");
631 static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
637 NCR_LOCK_DRIVER(flags);
638 mp = ___get_dma_pool(bush);
640 mp = ___cre_dma_pool(bush);
642 m = __m_calloc(mp, size, name);
645 NCR_UNLOCK_DRIVER(flags);
650 static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
655 NCR_LOCK_DRIVER(flags);
656 mp = ___get_dma_pool(bush);
658 __m_free(mp, m, size, name);
661 NCR_UNLOCK_DRIVER(flags);
664 static m_addr_t __vtobus(m_bush_t bush, void *m)
668 int hc = VTOB_HASH_CODE(m);
670 m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
672 NCR_LOCK_DRIVER(flags);
673 mp = ___get_dma_pool(bush);
676 while (vp && (m_addr_t) vp->vaddr != a)
679 NCR_UNLOCK_DRIVER(flags);
680 return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
683 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
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)
693 * Deal with DMA mapping/unmapping.
696 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
698 /* Linux versions prior to pci bus iommu kernel interface */
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)
706 #define scsi_sg_dma_address(sc) vtobus((sc)->address)
707 #define scsi_sg_dma_len(sc) ((sc)->length)
711 /* Linux version with pci bus iommu kernel interface */
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
717 static void __unmap_scsi_data(struct device *dev, Scsi_Cmnd *cmd)
719 enum dma_data_direction dma_dir =
720 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
722 switch(cmd->__data_mapped) {
724 dma_unmap_sg(dev, cmd->buffer, cmd->use_sg, dma_dir);
727 dma_unmap_single(dev, cmd->__data_mapping,
728 cmd->request_bufflen, dma_dir);
731 cmd->__data_mapped = 0;
734 static u_long __map_scsi_single_data(struct device *dev, Scsi_Cmnd *cmd)
737 enum dma_data_direction dma_dir =
738 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
741 if (cmd->request_bufflen == 0)
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;
752 static int __map_scsi_sg_data(struct device *dev, Scsi_Cmnd *cmd)
755 enum dma_data_direction dma_dir =
756 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
758 if (cmd->use_sg == 0)
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;
768 static void __sync_scsi_data_for_cpu(struct device *dev, Scsi_Cmnd *cmd)
770 enum dma_data_direction dma_dir =
771 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
773 switch(cmd->__data_mapped) {
775 dma_sync_sg_for_cpu(dev, cmd->buffer, cmd->use_sg, dma_dir);
778 dma_sync_single_for_cpu(dev, cmd->__data_mapping,
779 cmd->request_bufflen, dma_dir);
784 static void __sync_scsi_data_for_device(struct device *dev, Scsi_Cmnd *cmd)
786 enum dma_data_direction dma_dir =
787 (enum dma_data_direction)scsi_to_pci_dma_dir(cmd->sc_data_direction);
789 switch(cmd->__data_mapped) {
791 dma_sync_sg_for_device(dev, cmd->buffer, cmd->use_sg, dma_dir);
794 dma_sync_single_for_device(dev, cmd->__data_mapping,
795 cmd->request_bufflen, dma_dir);
800 #define scsi_sg_dma_address(sc) sg_dma_address(sc)
801 #define scsi_sg_dma_len(sc) sg_dma_len(sc)
803 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
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)
811 /*==========================================================
813 ** SCSI data transfer direction
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. ;-)
824 **==========================================================
826 #ifdef SCSI_DATA_UNKNOWN
828 #define scsi_data_direction(cmd) (cmd->sc_data_direction)
832 #define SCSI_DATA_UNKNOWN 0
833 #define SCSI_DATA_WRITE 1
834 #define SCSI_DATA_READ 2
835 #define SCSI_DATA_NONE 3
837 static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
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;
847 case 0x0A: /* WRITE(6) 0A */
848 case 0x2A: /* WRITE(10) 2A */
849 case 0xAA: /* WRITE(12) AA */
850 direction = SCSI_DATA_WRITE;
853 direction = SCSI_DATA_UNKNOWN;
860 #endif /* SCSI_DATA_UNKNOWN */
862 /*==========================================================
866 ** This structure is initialized from linux config
867 ** options. It can be overridden at boot-up by the boot
870 **==========================================================
872 static struct ncr_driver_setup
873 driver_setup = SCSI_NCR_DRIVER_SETUP;
875 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
876 static struct ncr_driver_setup
877 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
880 #define initverbose (driver_setup.verbose)
881 #define bootverbose (np->verbose)
884 /*==========================================================
886 ** NVRAM detection and reading.
888 ** Currently supported:
889 ** - 24C16 EEPROM with both Symbios and Tekram layout.
890 ** - 93C46 EEPROM with Tekram layout.
892 **==========================================================
895 #ifdef SCSI_NCR_NVRAM_SUPPORT
897 * 24C16 EEPROM reading.
899 * GPOI0 - data in/data out
901 * Symbios NVRAM wiring now also used by Tekram.
910 * Set/clear data/clock bit in GPIO0
913 S24C16_set_bit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode)
931 OUTB (nc_gpreg, *gpreg);
936 * Send START condition to NVRAM to wake it up.
938 static void __init S24C16_start(ncr_slot *np, u_char *gpreg)
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);
947 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
949 static void __init S24C16_stop(ncr_slot *np, u_char *gpreg)
951 S24C16_set_bit(np, 0, gpreg, SET_CLK);
952 S24C16_set_bit(np, 1, gpreg, SET_BIT);
956 * Read or write a bit to the NVRAM,
957 * read if GPIO0 input else write if GPIO0 output
960 S24C16_do_bit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg)
962 S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
963 S24C16_set_bit(np, 0, gpreg, SET_CLK);
965 *read_bit = INB (nc_gpreg);
966 S24C16_set_bit(np, 0, gpreg, CLR_CLK);
967 S24C16_set_bit(np, 0, gpreg, CLR_BIT);
971 * Output an ACK to the NVRAM after reading,
972 * change GPIO0 to output and when done back to an input
975 S24C16_write_ack(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl)
977 OUTB (nc_gpcntl, *gpcntl & 0xfe);
978 S24C16_do_bit(np, 0, write_bit, gpreg);
979 OUTB (nc_gpcntl, *gpcntl);
983 * Input an ACK from NVRAM after writing,
984 * change GPIO0 to input and when done back to an output
987 S24C16_read_ack(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl)
989 OUTB (nc_gpcntl, *gpcntl | 0x01);
990 S24C16_do_bit(np, read_bit, 1, gpreg);
991 OUTB (nc_gpcntl, *gpcntl);
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
999 S24C16_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data,
1000 u_char *gpreg, u_char *gpcntl)
1004 for (x = 0; x < 8; x++)
1005 S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
1007 S24C16_read_ack(np, ack_data, gpreg, gpcntl);
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
1015 S24C16_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data,
1016 u_char *gpreg, u_char *gpcntl)
1022 for (x = 0; x < 8; x++) {
1023 S24C16_do_bit(np, &read_bit, 1, gpreg);
1024 *read_data |= ((read_bit & 0x01) << (7 - x));
1027 S24C16_write_ack(np, ack_data, gpreg, gpcntl);
1031 * Read 'len' bytes starting at 'offset'.
1034 sym_read_S24C16_nvram (ncr_slot *np, int offset, u_char *data, int len)
1036 u_char gpcntl, gpreg;
1037 u_char old_gpcntl, old_gpreg;
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;
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);
1051 /* this is to set NVRAM into a known state with GPIO0/1 both low */
1053 S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
1054 S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
1056 /* now set NVRAM inactive with GPIO0/1 both high */
1057 S24C16_stop(np, &gpreg);
1059 /* activate NVRAM */
1060 S24C16_start(np, &gpreg);
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)
1068 /* write random address LSB */
1069 S24C16_write_byte(np, &ack_data,
1070 offset & 0xff, &gpreg, &gpcntl);
1071 if (ack_data & 0x01)
1074 /* regenerate START state to set up for reading */
1075 S24C16_start(np, &gpreg);
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)
1083 /* now set up GPIO0 for inputting data */
1085 OUTB (nc_gpcntl, gpcntl);
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);
1091 /* finally put NVRAM back in inactive mode */
1093 OUTB (nc_gpcntl, gpcntl);
1094 S24C16_stop(np, &gpreg);
1097 /* return GPIO0/1 to original states after having accessed NVRAM */
1098 OUTB (nc_gpcntl, old_gpcntl);
1099 OUTB (nc_gpreg, old_gpreg);
1110 * Try reading Symbios NVRAM.
1113 static int __init sym_read_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
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);
1121 /* probe the 24c16 and read the SYMBIOS 24c16 area */
1122 if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
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)
1131 /* verify checksum */
1132 for (x = 6, csum = 0; x < len - 6; x++)
1134 if (csum != nvram->checksum)
1141 * 93C46 EEPROM reading.
1146 * GPIO4 - chip select
1152 * Pulse clock bit in GPIO0
1154 static void __init T93C46_Clk(ncr_slot *np, u_char *gpreg)
1156 OUTB (nc_gpreg, *gpreg | 0x04);
1158 OUTB (nc_gpreg, *gpreg);
1162 * Read bit from NVRAM
1164 static void __init T93C46_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg)
1167 T93C46_Clk(np, gpreg);
1168 *read_bit = INB (nc_gpreg);
1172 * Write bit to GPIO0
1174 static void __init T93C46_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg)
1176 if (write_bit & 0x01)
1183 OUTB (nc_gpreg, *gpreg);
1186 T93C46_Clk(np, gpreg);
1190 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
1192 static void __init T93C46_Stop(ncr_slot *np, u_char *gpreg)
1195 OUTB (nc_gpreg, *gpreg);
1198 T93C46_Clk(np, gpreg);
1202 * Send read command and address to NVRAM
1205 T93C46_Send_Command(ncr_slot *np, u_short write_data,
1206 u_char *read_bit, u_char *gpreg)
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);
1214 *read_bit = INB (nc_gpreg);
1218 * READ 2 bytes from the NVRAM
1221 T93C46_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg)
1227 for (x = 0; x < 16; x++) {
1228 T93C46_Read_Bit(np, &read_bit, gpreg);
1230 if (read_bit & 0x01)
1231 *nvram_data |= (0x01 << (15 - x));
1233 *nvram_data &= ~(0x01 << (15 - x));
1238 * Read Tekram NvRAM data.
1241 T93C46_Read_Data(ncr_slot *np, u_short *data,int len,u_char *gpreg)
1246 for (x = 0; x < len; x++) {
1248 /* output read command and address */
1249 T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
1250 if (read_bit & 0x01)
1252 T93C46_Read_Word(np, &data[x], gpreg);
1253 T93C46_Stop(np, gpreg);
1260 * Try reading 93C46 Tekram NVRAM.
1263 sym_read_T93C46_nvram (ncr_slot *np, Tekram_nvram *nvram)
1265 u_char gpcntl, gpreg;
1266 u_char old_gpcntl, old_gpreg;
1269 /* save current state of GPCNTL and GPREG */
1270 old_gpreg = INB (nc_gpreg);
1271 old_gpcntl = INB (nc_gpcntl);
1273 /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
1275 gpreg = old_gpreg & 0xe9;
1276 OUTB (nc_gpreg, gpreg);
1277 gpcntl = (old_gpcntl & 0xe9) | 0x09;
1278 OUTB (nc_gpcntl, gpcntl);
1280 /* input all of NVRAM, 64 words */
1281 retv = T93C46_Read_Data(np, (u_short *) nvram,
1282 sizeof(*nvram) / sizeof(short), &gpreg);
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);
1292 * Try reading Tekram NVRAM.
1296 sym_read_Tekram_nvram (ncr_slot *np, u_short device_id, Tekram_nvram *nvram)
1298 u_char *data = (u_char *) nvram;
1299 int len = sizeof(*nvram);
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,
1310 case PCI_DEVICE_ID_NCR_53C875:
1311 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
1316 x = sym_read_T93C46_nvram(np, nvram);
1322 /* verify checksum */
1323 for (x = 0, csum = 0; x < len - 1; x += 2)
1324 csum += data[x] + (data[x+1] << 8);
1331 #endif /* SCSI_NCR_NVRAM_SUPPORT */
1333 /*===================================================================
1335 ** Detect and try to read SYMBIOS and TEKRAM NVRAM.
1337 ** Data can be used to order booting of boards.
1339 ** Data is saved in ncr_device structure if NVRAM found. This
1340 ** is then used to find drive boot order for ncr_attach().
1342 ** NVRAM data is passed to Scsi_Host_Template later during
1343 ** ncr_attach() for any device set up.
1345 **===================================================================
1347 #ifdef SCSI_NCR_NVRAM_SUPPORT
1348 static void __init ncr_get_nvram(struct ncr_device *devp, ncr_nvram *nvp)
1354 ** Get access to chip IO registers
1356 #ifdef SCSI_NCR_IOMAPPED
1357 request_region(devp->slot.io_port, 128, NAME53C8XX);
1358 devp->slot.base_io = devp->slot.io_port;
1361 (struct ncr_reg *) remap_pci_mem(devp->slot.base_c, 128);
1362 if (!devp->slot.reg)
1367 ** Try to read SYMBIOS nvram.
1368 ** Try to read TEKRAM nvram if Symbios nvram not found.
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,
1374 nvp->type = SCSI_NCR_TEKRAM_NVRAM;
1381 ** Release access to chip IO registers
1383 #ifdef SCSI_NCR_IOMAPPED
1384 release_region(devp->slot.base_io, 128);
1386 unmap_pci_mem((u_long) devp->slot.reg, 128ul);
1391 /*===================================================================
1393 ** Display the content of NVRAM for debugging purpose.
1395 **===================================================================
1397 #ifdef SCSI_NCR_DEBUG_NVRAM
1398 static void __init ncr_display_Symbios_nvram(Symbios_nvram *nvram)
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" :"");
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",
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" : "",
1422 tn->sync_period / 4,
1427 static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
1429 static void __init ncr_display_Tekram_nvram(Tekram_nvram *nvram)
1431 int i, tags, boot_delay;
1434 /* display Tekram nvram host data */
1435 tags = 2 << nvram->max_tags_index;
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) {
1441 case 0: rem = ""; break;
1442 case 1: rem = " REMOVABLE=boot device"; break;
1443 case 2: rem = " REMOVABLE=all"; break;
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);
1459 /* display Tekram nvram drive data */
1460 for (i = 0; i <= 15; i++) {
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",
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" : "",
1476 #endif /* SCSI_NCR_DEBUG_NVRAM */
1477 #endif /* SCSI_NCR_NVRAM_SUPPORT */
1480 /*===================================================================
1482 ** Utility routines that protperly return data through /proc FS.
1484 **===================================================================
1486 #ifdef SCSI_NCR_USER_INFO_SUPPORT
1496 static void copy_mem_info(struct info_str *info, char *data, int len)
1498 if (info->pos + len > info->length)
1499 len = info->length - info->pos;
1501 if (info->pos + len < info->offset) {
1505 if (info->pos < info->offset) {
1506 data += (info->offset - info->pos);
1507 len -= (info->offset - info->pos);
1511 memcpy(info->buffer + info->pos, data, len);
1516 static int copy_info(struct info_str *info, char *fmt, ...)
1522 va_start(args, fmt);
1523 len = vsprintf(buf, fmt, args);
1526 copy_mem_info(info, buf, len);
1532 /*===================================================================
1534 ** Driver setup from the boot command line
1536 **===================================================================
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
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
1571 #ifdef SCSI_NCR_IARB_SUPPORT
1575 static char setup_token[] __initdata =
1589 #ifdef SCSI_NCR_IARB_SUPPORT
1592 ; /* DONNOT REMOVE THIS ';' */
1600 static int __init get_setup_token(char *p)
1602 char *cur = setup_token;
1606 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1609 if (!strncmp(p, cur, pc - cur))
1617 static int __init sym53c8xx__setup(char *str)
1619 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
1625 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1637 val = (int) simple_strtoul(pv, &pe, 0);
1639 switch (get_setup_token(cur)) {
1641 driver_setup.default_tags = val;
1642 if (pe && *pe == '/') {
1644 while (*pe && *pe != ARG_SEP &&
1645 i < sizeof(driver_setup.tag_ctrl)-1) {
1646 driver_setup.tag_ctrl[i++] = *pe++;
1648 driver_setup.tag_ctrl[i] = '\0';
1651 case OPT_MASTER_PARITY:
1652 driver_setup.master_parity = val;
1654 case OPT_SCSI_PARITY:
1655 driver_setup.scsi_parity = val;
1657 case OPT_DISCONNECTION:
1658 driver_setup.disconnection = val;
1660 case OPT_SPECIAL_FEATURES:
1661 driver_setup.special_features = val;
1663 case OPT_FORCE_SYNC_NEGO:
1664 driver_setup.force_sync_nego = val;
1666 case OPT_REVERSE_PROBE:
1667 driver_setup.reverse_probe = val;
1669 case OPT_DEFAULT_SYNC:
1670 driver_setup.default_sync = val;
1673 driver_setup.verbose = val;
1676 driver_setup.debug = val;
1679 driver_setup.burst_max = val;
1682 driver_setup.led_pin = val;
1685 driver_setup.max_wide = val? 1:0;
1687 case OPT_SETTLE_DELAY:
1688 driver_setup.settle_delay = val;
1690 case OPT_DIFF_SUPPORT:
1691 driver_setup.diff_support = val;
1694 driver_setup.irqm = val;
1696 case OPT_PCI_FIX_UP:
1697 driver_setup.pci_fix_up = val;
1700 driver_setup.bus_check = val;
1703 driver_setup.optimize = val;
1706 driver_setup.recovery = val;
1709 driver_setup.use_nvram = val;
1711 case OPT_SAFE_SETUP:
1712 memcpy(&driver_setup, &driver_safe_setup,
1713 sizeof(driver_setup));
1716 if (xi < SCSI_NCR_MAX_EXCLUDES)
1717 driver_setup.excludes[xi++] = val;
1720 driver_setup.host_id = val;
1722 #ifdef SCSI_NCR_IARB_SUPPORT
1724 driver_setup.iarb = val;
1728 printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
1732 if ((cur = strchr(cur, ARG_SEP)) != NULL)
1735 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
1739 /*===================================================================
1741 ** Get device queue depth from boot command line.
1743 **===================================================================
1745 #define DEF_DEPTH (driver_setup.default_tags)
1746 #define ALL_TARGETS -2
1747 #define NO_TARGET -1
1751 static int device_queue_depth(int unit, int target, int lun)
1754 char *p = driver_setup.tag_ctrl;
1760 while ((c = *p++) != 0) {
1761 v = simple_strtoul(p, &ep, 0);
1770 t = (target == v) ? v : NO_TARGET;
1775 u = (lun == v) ? v : NO_LUN;
1779 (t == ALL_TARGETS || t == target) &&
1780 (u == ALL_LUNS || u == lun))