Annotation of Gnu-Mach/linux/src/drivers/scsi/sym53c8xx_comm.h, revision 1.1.1.1

1.1       root        1: /******************************************************************************
                      2: **  High Performance device driver for the Symbios 53C896 controller.
                      3: **
                      4: **  Copyright (C) 1998-2000  Gerard Roudier <[email protected]>
                      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              <[email protected]>
                     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        <[email protected]>
                     44: **          Stefan Esser                <[email protected]>
                     45: **
                     46: **-----------------------------------------------------------------------------
                     47: **
                     48: **  Major contributions:
                     49: **  --------------------
                     50: **
                     51: **  NVRAM detection and reading.
                     52: **    Copyright (C) 1997 Richard Waltham <[email protected]>
                     53: **
                     54: *******************************************************************************
                     55: */
                     56: 
                     57: /*
                     58: **     This file contains definitions and code that the 
                     59: **     sym53c8xx and ncr53c8xx drivers should share.
                     60: **     The sharing will be achieved in a further version  
                     61: **     of the driver bundle. For now, only the ncr53c8xx 
                     62: **     driver includes this file.
                     63: */
                     64: 
                     65: #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
                     66: #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
                     67: 
                     68: /*==========================================================
                     69: **
                     70: **     Hmmm... What complex some PCI-HOST bridges actually 
                     71: **     are, despite the fact that the PCI specifications 
                     72: **     are looking so smart and simple! ;-)
                     73: **
                     74: **==========================================================
                     75: */
                     76: 
                     77: #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,47)
                     78: #define SCSI_NCR_DYNAMIC_DMA_MAPPING
                     79: #endif
                     80: 
                     81: /*==========================================================
                     82: **
                     83: **     Miscallaneous defines.
                     84: **
                     85: **==========================================================
                     86: */
                     87: 
                     88: #define u_char         unsigned char
                     89: #define u_short                unsigned short
                     90: #define u_int          unsigned int
                     91: #define u_long         unsigned long
                     92: 
                     93: #ifndef bcopy
                     94: #define bcopy(s, d, n) memcpy((d), (s), (n))
                     95: #endif
                     96: 
                     97: #ifndef bcmp
                     98: #define bcmp(s, d, n)  memcmp((d), (s), (n))
                     99: #endif
                    100: 
                    101: #ifndef bzero
                    102: #define bzero(d, n)    memset((d), 0, (n))
                    103: #endif
                    104:  
                    105: #ifndef offsetof
                    106: #define offsetof(t, m) ((size_t) (&((t *)0)->m))
                    107: #endif
                    108: 
                    109: /*==========================================================
                    110: **
                    111: **     assert ()
                    112: **
                    113: **==========================================================
                    114: **
                    115: **     modified copy from 386bsd:/usr/include/sys/assert.h
                    116: **
                    117: **----------------------------------------------------------
                    118: */
                    119: 
                    120: #define        assert(expression) { \
                    121:        if (!(expression)) { \
                    122:                (void)panic( \
                    123:                        "assertion \"%s\" failed: file \"%s\", line %d\n", \
                    124:                        #expression, \
                    125:                        __FILE__, __LINE__); \
                    126:        } \
                    127: }
                    128: 
                    129: /*==========================================================
                    130: **
                    131: **     Debugging tags
                    132: **
                    133: **==========================================================
                    134: */
                    135: 
                    136: #define DEBUG_ALLOC    (0x0001)
                    137: #define DEBUG_PHASE    (0x0002)
                    138: #define DEBUG_QUEUE    (0x0008)
                    139: #define DEBUG_RESULT   (0x0010)
                    140: #define DEBUG_POINTER  (0x0020)
                    141: #define DEBUG_SCRIPT   (0x0040)
                    142: #define DEBUG_TINY     (0x0080)
                    143: #define DEBUG_TIMING   (0x0100)
                    144: #define DEBUG_NEGO     (0x0200)
                    145: #define DEBUG_TAGS     (0x0400)
                    146: #define DEBUG_SCATTER  (0x0800)
                    147: #define DEBUG_IC        (0x1000)
                    148: 
                    149: /*
                    150: **    Enable/Disable debug messages.
                    151: **    Can be changed at runtime too.
                    152: */
                    153: 
                    154: #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
                    155: static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
                    156:        #define DEBUG_FLAGS ncr_debug
                    157: #else
                    158:        #define DEBUG_FLAGS     SCSI_NCR_DEBUG_FLAGS
                    159: #endif
                    160: 
                    161: /*==========================================================
                    162: **
                    163: **     A la VMS/CAM-3 queue management.
                    164: **     Implemented from linux list management.
                    165: **
                    166: **==========================================================
                    167: */
                    168: 
                    169: typedef struct xpt_quehead {
                    170:        struct xpt_quehead *flink;      /* Forward  pointer */
                    171:        struct xpt_quehead *blink;      /* Backward pointer */
                    172: } XPT_QUEHEAD;
                    173: 
                    174: #define xpt_que_init(ptr) do { \
                    175:        (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
                    176: } while (0)
                    177: 
                    178: static inline void __xpt_que_add(struct xpt_quehead * new,
                    179:        struct xpt_quehead * blink,
                    180:        struct xpt_quehead * flink)
                    181: {
                    182:        flink->blink    = new;
                    183:        new->flink      = flink;
                    184:        new->blink      = blink;
                    185:        blink->flink    = new;
                    186: }
                    187: 
                    188: static inline void __xpt_que_del(struct xpt_quehead * blink,
                    189:        struct xpt_quehead * flink)
                    190: {
                    191:        flink->blink = blink;
                    192:        blink->flink = flink;
                    193: }
                    194: 
                    195: static inline int xpt_que_empty(struct xpt_quehead *head)
                    196: {
                    197:        return head->flink == head;
                    198: }
                    199: 
                    200: static inline void xpt_que_splice(struct xpt_quehead *list,
                    201:        struct xpt_quehead *head)
                    202: {
                    203:        struct xpt_quehead *first = list->flink;
                    204: 
                    205:        if (first != list) {
                    206:                struct xpt_quehead *last = list->blink;
                    207:                struct xpt_quehead *at   = head->flink;
                    208: 
                    209:                first->blink = head;
                    210:                head->flink  = first;
                    211: 
                    212:                last->flink = at;
                    213:                at->blink   = last;
                    214:        }
                    215: }
                    216: 
                    217: #define xpt_que_entry(ptr, type, member) \
                    218:        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
                    219: 
                    220: 
                    221: #define xpt_insque(new, pos)           __xpt_que_add(new, pos, (pos)->flink)
                    222: 
                    223: #define xpt_remque(el)                 __xpt_que_del((el)->blink, (el)->flink)
                    224: 
                    225: #define xpt_insque_head(new, head)     __xpt_que_add(new, head, (head)->flink)
                    226: 
                    227: static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
                    228: {
                    229:        struct xpt_quehead *elem = head->flink;
                    230: 
                    231:        if (elem != head)
                    232:                __xpt_que_del(head, elem->flink);
                    233:        else
                    234:                elem = 0;
                    235:        return elem;
                    236: }
                    237: 
                    238: #define xpt_insque_tail(new, head)     __xpt_que_add(new, (head)->blink, head)
                    239: 
                    240: static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
                    241: {
                    242:        struct xpt_quehead *elem = head->blink;
                    243: 
                    244:        if (elem != head)
                    245:                __xpt_que_del(elem->blink, head);
                    246:        else
                    247:                elem = 0;
                    248:        return elem;
                    249: }
                    250: 
                    251: /*==========================================================
                    252: **
                    253: **     Simple Wrapper to kernel PCI bus interface.
                    254: **
                    255: **     This wrapper allows to get rid of old kernel PCI 
                    256: **     interface and still allows to preserve linux-2.0 
                    257: **     compatibilty. In fact, it is mostly an incomplete 
                    258: **     emulation of the new PCI code for pre-2.2 kernels.
                    259: **     When kernel-2.0 support will be dropped, we will 
                    260: **     just have to remove most of this code.
                    261: **
                    262: **==========================================================
                    263: */
                    264: 
                    265: #if LINUX_VERSION_CODE >= LinuxVersionCode(2,2,0)
                    266: 
                    267: typedef struct pci_dev *pcidev_t;
                    268: #define PCIDEV_NULL            (0)
                    269: #define PciBusNumber(d)                (d)->bus->number
                    270: #define PciDeviceFn(d)         (d)->devfn
                    271: #define PciVendorId(d)         (d)->vendor
                    272: #define PciDeviceId(d)         (d)->device
                    273: #define PciIrqLine(d)          (d)->irq
                    274: 
                    275: #if LINUX_VERSION_CODE > LinuxVersionCode(2,3,12)
                    276: 
                    277: static int __init 
                    278: pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
                    279: {
                    280:        *base = pdev->resource[index].start;
                    281:        if ((pdev->resource[index].flags & 0x7) == 0x4)
                    282:                ++index;
                    283:        return ++index;
                    284: }
                    285: #else
                    286: static int __init 
                    287: pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
                    288: {
                    289:        *base = pdev->base_address[index++];
                    290:        if ((*base & 0x7) == 0x4) {
                    291: #if BITS_PER_LONG > 32
                    292:                *base |= (((u_long)pdev->base_address[index]) << 32);
                    293: #endif
                    294:                ++index;
                    295:        }
                    296:        return index;
                    297: }
                    298: #endif
                    299: 
                    300: #else  /* Incomplete emulation of current PCI code for pre-2.2 kernels */
                    301: 
                    302: typedef unsigned int pcidev_t;
                    303: #define PCIDEV_NULL            (~0u)
                    304: #define PciBusNumber(d)                ((d)>>8)
                    305: #define PciDeviceFn(d)         ((d)&0xff)
                    306: #define __PciDev(busn, devfn)  (((busn)<<8)+(devfn))
                    307: 
                    308: #define pci_present pcibios_present
                    309: 
                    310: #define pci_read_config_byte(d, w, v) \
                    311:        pcibios_read_config_byte(PciBusNumber(d), PciDeviceFn(d), w, v)
                    312: #define pci_read_config_word(d, w, v) \
                    313:        pcibios_read_config_word(PciBusNumber(d), PciDeviceFn(d), w, v)
                    314: #define pci_read_config_dword(d, w, v) \
                    315:        pcibios_read_config_dword(PciBusNumber(d), PciDeviceFn(d), w, v)
                    316: 
                    317: #define pci_write_config_byte(d, w, v) \
                    318:        pcibios_write_config_byte(PciBusNumber(d), PciDeviceFn(d), w, v)
                    319: #define pci_write_config_word(d, w, v) \
                    320:        pcibios_write_config_word(PciBusNumber(d), PciDeviceFn(d), w, v)
                    321: #define pci_write_config_dword(d, w, v) \
                    322:        pcibios_write_config_dword(PciBusNumber(d), PciDeviceFn(d), w, v)
                    323: 
                    324: static pcidev_t __init
                    325: pci_find_device(unsigned int vendor, unsigned int device, pcidev_t prev)
                    326: {
                    327:        static unsigned short pci_index;
                    328:        int retv;
                    329:        unsigned char bus_number, device_fn;
                    330: 
                    331:        if (prev == PCIDEV_NULL)
                    332:                pci_index = 0;
                    333:        else
                    334:                ++pci_index;
                    335:        retv = pcibios_find_device (vendor, device, pci_index,
                    336:                                    &bus_number, &device_fn);
                    337:        return retv ? PCIDEV_NULL : __PciDev(bus_number, device_fn);
                    338: }
                    339: 
                    340: static u_short __init PciVendorId(pcidev_t dev)
                    341: {
                    342:        u_short vendor_id;
                    343:        pci_read_config_word(dev, PCI_VENDOR_ID, &vendor_id);
                    344:        return vendor_id;
                    345: }
                    346: 
                    347: static u_short __init PciDeviceId(pcidev_t dev)
                    348: {
                    349:        u_short device_id;
                    350:        pci_read_config_word(dev, PCI_DEVICE_ID, &device_id);
                    351:        return device_id;
                    352: }
                    353: 
                    354: static u_int __init PciIrqLine(pcidev_t dev)
                    355: {
                    356:        u_char irq;
                    357:        pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
                    358:        return irq;
                    359: }
                    360: 
                    361: static int __init 
                    362: pci_get_base_address(pcidev_t dev, int offset, u_long *base)
                    363: {
                    364:        u_int32 tmp;
                    365:        
                    366:        pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + offset, &tmp);
                    367:        *base = tmp;
                    368:        offset += sizeof(u_int32);
                    369:        if ((tmp & 0x7) == 0x4) {
                    370: #if BITS_PER_LONG > 32
                    371:                pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + offset, &tmp);
                    372:                *base |= (((u_long)tmp) << 32);
                    373: #endif
                    374:                offset += sizeof(u_int32);
                    375:        }
                    376:        return offset;
                    377: }
                    378: 
                    379: #endif /* LINUX_VERSION_CODE >= LinuxVersionCode(2,2,0) */
                    380: 
                    381: /*==========================================================
                    382: **
                    383: **     SMP threading.
                    384: **
                    385: **     Assuming that SMP systems are generally high end 
                    386: **     systems and may use several SCSI adapters, we are 
                    387: **     using one lock per controller instead of some global 
                    388: **     one. For the moment (linux-2.1.95), driver's entry 
                    389: **     points are called with the 'io_request_lock' lock 
                    390: **     held, so:
                    391: **     - We are uselessly loosing a couple of micro-seconds 
                    392: **       to lock the controller data structure.
                    393: **     - But the driver is not broken by design for SMP and 
                    394: **       so can be more resistant to bugs or bad changes in 
                    395: **       the IO sub-system code.
                    396: **     - A small advantage could be that the interrupt code 
                    397: **       is grained as wished (e.g.: by controller).
                    398: **
                    399: **==========================================================
                    400: */
                    401: 
                    402: #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
                    403: spinlock_t DRIVER_SMP_LOCK = SPIN_LOCK_UNLOCKED;
                    404: #define        NCR_LOCK_DRIVER(flags)     spin_lock_irqsave(&DRIVER_SMP_LOCK, flags)
                    405: #define        NCR_UNLOCK_DRIVER(flags)   \
                    406:                spin_unlock_irqrestore(&DRIVER_SMP_LOCK, flags)
                    407: 
                    408: #define NCR_INIT_LOCK_NCB(np)      spin_lock_init(&np->smp_lock)
                    409: #define        NCR_LOCK_NCB(np, flags)    spin_lock_irqsave(&np->smp_lock, flags)
                    410: #define        NCR_UNLOCK_NCB(np, flags)  spin_unlock_irqrestore(&np->smp_lock, flags)
                    411: 
                    412: #define        NCR_LOCK_SCSI_DONE(np, flags) \
                    413:                spin_lock_irqsave(&io_request_lock, flags)
                    414: #define        NCR_UNLOCK_SCSI_DONE(np, flags) \
                    415:                spin_unlock_irqrestore(&io_request_lock, flags)
                    416: 
                    417: #else
                    418: 
                    419: #define        NCR_LOCK_DRIVER(flags)     do { save_flags(flags); cli(); } while (0)
                    420: #define        NCR_UNLOCK_DRIVER(flags)   do { restore_flags(flags); } while (0)
                    421: 
                    422: #define        NCR_INIT_LOCK_NCB(np)      do { } while (0)
                    423: #define        NCR_LOCK_NCB(np, flags)    do { save_flags(flags); cli(); } while (0)
                    424: #define        NCR_UNLOCK_NCB(np, flags)  do { restore_flags(flags); } while (0)
                    425: 
                    426: #define        NCR_LOCK_SCSI_DONE(np, flags)    do {;} while (0)
                    427: #define        NCR_UNLOCK_SCSI_DONE(np, flags)  do {;} while (0)
                    428: 
                    429: #endif
                    430: 
                    431: /*==========================================================
                    432: **
                    433: **     Memory mapped IO
                    434: **
                    435: **     Since linux-2.1, we must use ioremap() to map the io 
                    436: **     memory space and iounmap() to unmap it. This allows 
                    437: **     portability. Linux 1.3.X and 2.0.X allow to remap 
                    438: **     physical pages addresses greater than the highest 
                    439: **     physical memory address to kernel virtual pages with 
                    440: **     vremap() / vfree(). That was not portable but worked 
                    441: **     with i386 architecture.
                    442: **
                    443: **==========================================================
                    444: */
                    445: 
                    446: #if LINUX_VERSION_CODE < LinuxVersionCode(2,1,0)
                    447: #define ioremap vremap
                    448: #define iounmap vfree
                    449: #endif
                    450: 
                    451: #ifdef __sparc__
                    452: #  include <asm/irq.h>
                    453: #  define pcivtobus(p)                 bus_dvma_to_mem(p)
                    454: #  define memcpy_to_pci(a, b, c)       memcpy_toio((a), (b), (c))
                    455: #elif defined(__alpha__)
                    456: #  define pcivtobus(p)                 ((p) & 0xfffffffful)
                    457: #  define memcpy_to_pci(a, b, c)       memcpy_toio((a), (b), (c))
                    458: #else  /* others */
                    459: #  define pcivtobus(p)                 (p)
                    460: #  define memcpy_to_pci(a, b, c)       memcpy_toio((a), (b), (c))
                    461: #endif
                    462: 
                    463: #ifndef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
                    464: static u_long __init remap_pci_mem(u_long base, u_long size)
                    465: {
                    466:        u_long page_base        = ((u_long) base) & PAGE_MASK;
                    467:        u_long page_offs        = ((u_long) base) - page_base;
                    468:        u_long page_remapped    = (u_long) ioremap(page_base, page_offs+size);
                    469: 
                    470:        return page_remapped? (page_remapped + page_offs) : 0UL;
                    471: }
                    472: 
                    473: static void __init unmap_pci_mem(u_long vaddr, u_long size)
                    474: {
                    475:        if (vaddr)
                    476:                iounmap((void *) (vaddr & PAGE_MASK));
                    477: }
                    478: 
                    479: #endif /* not def SCSI_NCR_PCI_MEM_NOT_SUPPORTED */
                    480: 
                    481: /*==========================================================
                    482: **
                    483: **     Insert a delay in micro-seconds and milli-seconds.
                    484: **
                    485: **     Under Linux, udelay() is restricted to delay < 
                    486: **     1 milli-second. In fact, it generally works for up 
                    487: **     to 1 second delay. Since 2.1.105, the mdelay() function 
                    488: **     is provided for delays in milli-seconds.
                    489: **     Under 2.0 kernels, udelay() is an inline function 
                    490: **     that is very inaccurate on Pentium processors.
                    491: **
                    492: **==========================================================
                    493: */
                    494: 
                    495: #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,105)
                    496: #define UDELAY udelay
                    497: #define MDELAY mdelay
                    498: #else
                    499: static void UDELAY(long us) { udelay(us); }
                    500: static void MDELAY(long ms) { while (ms--) UDELAY(1000); }
                    501: #endif
                    502: 
                    503: /*==========================================================
                    504: **
                    505: **     Simple power of two buddy-like allocator.
                    506: **
                    507: **     This simple code is not intended to be fast, but to 
                    508: **     provide power of 2 aligned memory allocations.
                    509: **     Since the SCRIPTS processor only supplies 8 bit 
                    510: **     arithmetic, this allocator allows simple and fast 
                    511: **     address calculations  from the SCRIPTS code.
                    512: **     In addition, cache line alignment is guaranteed for 
                    513: **     power of 2 cache line size.
                    514: **     Enhanced in linux-2.3.44 to provide a memory pool 
                    515: **     per pcidev to support dynamic dma mapping. (I would 
                    516: **     have preferred a real bus astraction, btw).
                    517: **
                    518: **==========================================================
                    519: */
                    520: 
                    521: #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,0)
                    522: #define __GetFreePages(flags, order) __get_free_pages(flags, order)
                    523: #else
                    524: #define __GetFreePages(flags, order) __get_free_pages(flags, order, 0)
                    525: #endif
                    526: 
                    527: #define MEMO_SHIFT     4       /* 16 bytes minimum memory chunk */
                    528: #if PAGE_SIZE >= 8192
                    529: #define MEMO_PAGE_ORDER        0       /* 1 PAGE  maximum */
                    530: #else
                    531: #define MEMO_PAGE_ORDER        1       /* 2 PAGES maximum */
                    532: #endif
                    533: #define MEMO_FREE_UNUSED       /* Free unused pages immediately */
                    534: #define MEMO_WARN      1
                    535: #define MEMO_GFP_FLAGS GFP_ATOMIC
                    536: #define MEMO_CLUSTER_SHIFT     (PAGE_SHIFT+MEMO_PAGE_ORDER)
                    537: #define MEMO_CLUSTER_SIZE      (1UL << MEMO_CLUSTER_SHIFT)
                    538: #define MEMO_CLUSTER_MASK      (MEMO_CLUSTER_SIZE-1)
                    539: 
                    540: typedef u_long m_addr_t;       /* Enough bits to bit-hack addresses */
                    541: typedef pcidev_t m_bush_t;     /* Something that addresses DMAable */
                    542: 
                    543: typedef struct m_link {                /* Link between free memory chunks */
                    544:        struct m_link *next;
                    545: } m_link_s;
                    546: 
                    547: #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
                    548: typedef struct m_vtob {                /* Virtual to Bus address translation */
                    549:        struct m_vtob *next;
                    550:        m_addr_t vaddr;
                    551:        m_addr_t baddr;
                    552: } m_vtob_s;
                    553: #define VTOB_HASH_SHIFT                5
                    554: #define VTOB_HASH_SIZE         (1UL << VTOB_HASH_SHIFT)
                    555: #define VTOB_HASH_MASK         (VTOB_HASH_SIZE-1)
                    556: #define VTOB_HASH_CODE(m)      \
                    557:        ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
                    558: #endif
                    559: 
                    560: typedef struct m_pool {                /* Memory pool of a given kind */
                    561: #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
                    562:        m_bush_t bush;
                    563:        m_addr_t (*getp)(struct m_pool *);
                    564:        void (*freep)(struct m_pool *, m_addr_t);
                    565: #define M_GETP()               mp->getp(mp)
                    566: #define M_FREEP(p)             mp->freep(mp, p)
                    567: #define GetPages()             __GetFreePages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER)
                    568: #define FreePages(p)           free_pages(p, MEMO_PAGE_ORDER)
                    569:        int nump;
                    570:        m_vtob_s *(vtob[VTOB_HASH_SIZE]);
                    571:        struct m_pool *next;
                    572: #else
                    573: #define M_GETP()               __GetFreePages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER)
                    574: #define M_FREEP(p)             free_pages(p, MEMO_PAGE_ORDER)
                    575: #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
                    576:        struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
                    577: } m_pool_s;
                    578: 
                    579: static void *___m_alloc(m_pool_s *mp, int size)
                    580: {
                    581:        int i = 0;
                    582:        int s = (1 << MEMO_SHIFT);
                    583:        int j;
                    584:        m_addr_t a;
                    585:        m_link_s *h = mp->h;
                    586: 
                    587:        if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
                    588:                return 0;
                    589: 
                    590:        while (size > s) {
                    591:                s <<= 1;
                    592:                ++i;
                    593:        }
                    594: 
                    595:        j = i;
                    596:        while (!h[j].next) {
                    597:                if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
                    598:                        h[j].next = (m_link_s *) M_GETP();
                    599:                        if (h[j].next)
                    600:                                h[j].next->next = 0;
                    601:                        break;
                    602:                }
                    603:                ++j;
                    604:                s <<= 1;
                    605:        }
                    606:        a = (m_addr_t) h[j].next;
                    607:        if (a) {
                    608:                h[j].next = h[j].next->next;
                    609:                while (j > i) {
                    610:                        j -= 1;
                    611:                        s >>= 1;
                    612:                        h[j].next = (m_link_s *) (a+s);
                    613:                        h[j].next->next = 0;
                    614:                }
                    615:        }
                    616: #ifdef DEBUG
                    617:        printk("___m_alloc(%d) = %p\n", size, (void *) a);
                    618: #endif
                    619:        return (void *) a;
                    620: }
                    621: 
                    622: static void ___m_free(m_pool_s *mp, void *ptr, int size)
                    623: {
                    624:        int i = 0;
                    625:        int s = (1 << MEMO_SHIFT);
                    626:        m_link_s *q;
                    627:        m_addr_t a, b;
                    628:        m_link_s *h = mp->h;
                    629: 
                    630: #ifdef DEBUG
                    631:        printk("___m_free(%p, %d)\n", ptr, size);
                    632: #endif
                    633: 
                    634:        if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
                    635:                return;
                    636: 
                    637:        while (size > s) {
                    638:                s <<= 1;
                    639:                ++i;
                    640:        }
                    641: 
                    642:        a = (m_addr_t) ptr;
                    643: 
                    644:        while (1) {
                    645: #ifdef MEMO_FREE_UNUSED
                    646:                if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
                    647:                        M_FREEP(a);
                    648:                        break;
                    649:                }
                    650: #endif
                    651:                b = a ^ s;
                    652:                q = &h[i];
                    653:                while (q->next && q->next != (m_link_s *) b) {
                    654:                        q = q->next;
                    655:                }
                    656:                if (!q->next) {
                    657:                        ((m_link_s *) a)->next = h[i].next;
                    658:                        h[i].next = (m_link_s *) a;
                    659:                        break;
                    660:                }
                    661:                q->next = q->next->next;
                    662:                a = a & b;
                    663:                s <<= 1;
                    664:                ++i;
                    665:        }
                    666: }
                    667: 
                    668: static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
                    669: {
                    670:        void *p;
                    671: 
                    672:        p = ___m_alloc(mp, size);
                    673: 
                    674:        if (DEBUG_FLAGS & DEBUG_ALLOC)
                    675:                printk ("new %-10s[%4d] @%p.\n", name, size, p);
                    676: 
                    677:        if (p)
                    678:                bzero(p, size);
                    679:        else if (uflags & MEMO_WARN)
                    680:                printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
                    681: 
                    682:        return p;
                    683: }
                    684: 
                    685: #define __m_calloc(mp, s, n)   __m_calloc2(mp, s, n, MEMO_WARN)
                    686: 
                    687: static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
                    688: {
                    689:        if (DEBUG_FLAGS & DEBUG_ALLOC)
                    690:                printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
                    691: 
                    692:        ___m_free(mp, ptr, size);
                    693: 
                    694: }
                    695: 
                    696: /*
                    697:  * With pci bus iommu support, we use a default pool of unmapped memory 
                    698:  * for memory we donnot need to DMA from/to and one pool per pcidev for 
                    699:  * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
                    700:  */
                    701: 
                    702: #ifndef        SCSI_NCR_DYNAMIC_DMA_MAPPING
                    703: 
                    704: static m_pool_s mp0;
                    705: 
                    706: #else
                    707: 
                    708: static m_addr_t ___mp0_getp(m_pool_s *mp)
                    709: {
                    710:        m_addr_t m = GetPages();
                    711:        if (m)
                    712:                ++mp->nump;
                    713:        return m;
                    714: }
                    715: 
                    716: static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
                    717: {
                    718:        FreePages(m);
                    719:        --mp->nump;
                    720: }
                    721: 
                    722: static m_pool_s mp0 = {0, ___mp0_getp, ___mp0_freep};
                    723: 
                    724: #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
                    725: 
                    726: static void *m_calloc(int size, char *name)
                    727: {
                    728:        u_long flags;
                    729:        void *m;
                    730:        NCR_LOCK_DRIVER(flags);
                    731:        m = __m_calloc(&mp0, size, name);
                    732:        NCR_UNLOCK_DRIVER(flags);
                    733:        return m;
                    734: }
                    735: 
                    736: static void m_free(void *ptr, int size, char *name)
                    737: {
                    738:        u_long flags;
                    739:        NCR_LOCK_DRIVER(flags);
                    740:        __m_free(&mp0, ptr, size, name);
                    741:        NCR_UNLOCK_DRIVER(flags);
                    742: }
                    743: 
                    744: /*
                    745:  * DMAable pools.
                    746:  */
                    747: 
                    748: #ifndef        SCSI_NCR_DYNAMIC_DMA_MAPPING
                    749: 
                    750: /* Without pci bus iommu support, all the memory is assumed DMAable */
                    751: 
                    752: #define __m_calloc_dma(b, s, n)                m_calloc(s, n)
                    753: #define __m_free_dma(b, p, s, n)       m_free(p, s, n)
                    754: #define __vtobus(b, p)                 virt_to_bus(p)
                    755: 
                    756: #else
                    757: 
                    758: /*
                    759:  * With pci bus iommu support, we maintain one pool per pcidev and a 
                    760:  * hashed reverse table for virtual to bus physical address translations.
                    761:  */
                    762: static m_addr_t ___dma_getp(m_pool_s *mp)
                    763: {
                    764:        m_addr_t vp;
                    765:        m_vtob_s *vbp;
                    766: 
                    767:        vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
                    768:        if (vbp) {
                    769:                dma_addr_t daddr;
                    770:                vp = (m_addr_t) pci_alloc_consistent(mp->bush,
                    771:                                                PAGE_SIZE<<MEMO_PAGE_ORDER,
                    772:                                                &daddr);
                    773:                if (vp) {
                    774:                        int hc = VTOB_HASH_CODE(vp);
                    775:                        vbp->vaddr = vp;
                    776:                        vbp->baddr = daddr;
                    777:                        vbp->next = mp->vtob[hc];
                    778:                        mp->vtob[hc] = vbp;
                    779:                        ++mp->nump;
                    780:                        return vp;
                    781:                }
                    782:        }
                    783:        if (vbp)
                    784:                __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
                    785:        return 0;
                    786: }
                    787: 
                    788: static void ___dma_freep(m_pool_s *mp, m_addr_t m)
                    789: {
                    790:        m_vtob_s **vbpp, *vbp;
                    791:        int hc = VTOB_HASH_CODE(m);
                    792: 
                    793:        vbpp = &mp->vtob[hc];
                    794:        while (*vbpp && (*vbpp)->vaddr != m)
                    795:                vbpp = &(*vbpp)->next;
                    796:        if (*vbpp) {
                    797:                vbp = *vbpp;
                    798:                *vbpp = (*vbpp)->next;
                    799:                pci_free_consistent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
                    800:                                    (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
                    801:                __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
                    802:                --mp->nump;
                    803:        }
                    804: }
                    805: 
                    806: static inline m_pool_s *___get_dma_pool(m_bush_t bush)
                    807: {
                    808:        m_pool_s *mp;
                    809:        for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
                    810:        return mp;
                    811: }
                    812: 
                    813: static m_pool_s *___cre_dma_pool(m_bush_t bush)
                    814: {
                    815:        m_pool_s *mp;
                    816:        mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
                    817:        if (mp) {
                    818:                bzero(mp, sizeof(*mp));
                    819:                mp->bush = bush;
                    820:                mp->getp = ___dma_getp;
                    821:                mp->freep = ___dma_freep;
                    822:                mp->next = mp0.next;
                    823:                mp0.next = mp;
                    824:        }
                    825:        return mp;
                    826: }
                    827: 
                    828: static void ___del_dma_pool(m_pool_s *p)
                    829: {
                    830:        struct m_pool **pp = &mp0.next;
                    831: 
                    832:        while (*pp && *pp != p)
                    833:                pp = &(*pp)->next;
                    834:        if (*pp) {
                    835:                *pp = (*pp)->next;
                    836:                __m_free(&mp0, p, sizeof(*p), "MPOOL");
                    837:        }
                    838: }
                    839: 
                    840: static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
                    841: {
                    842:        u_long flags;
                    843:        struct m_pool *mp;
                    844:        void *m = 0;
                    845: 
                    846:        NCR_LOCK_DRIVER(flags);
                    847:        mp = ___get_dma_pool(bush);
                    848:        if (!mp)
                    849:                mp = ___cre_dma_pool(bush);
                    850:        if (mp)
                    851:                m = __m_calloc(mp, size, name);
                    852:        if (mp && !mp->nump)
                    853:                ___del_dma_pool(mp);
                    854:        NCR_UNLOCK_DRIVER(flags);
                    855: 
                    856:        return m;
                    857: }
                    858: 
                    859: static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
                    860: {
                    861:        u_long flags;
                    862:        struct m_pool *mp;
                    863: 
                    864:        NCR_LOCK_DRIVER(flags);
                    865:        mp = ___get_dma_pool(bush);
                    866:        if (mp)
                    867:                __m_free(mp, m, size, name);
                    868:        if (mp && !mp->nump)
                    869:                ___del_dma_pool(mp);
                    870:        NCR_UNLOCK_DRIVER(flags);
                    871: }
                    872: 
                    873: static m_addr_t __vtobus(m_bush_t bush, void *m)
                    874: {
                    875:        u_long flags;
                    876:        m_pool_s *mp;
                    877:        int hc = VTOB_HASH_CODE(m);
                    878:        m_vtob_s *vp = 0;
                    879:        m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
                    880: 
                    881:        NCR_LOCK_DRIVER(flags);
                    882:        mp = ___get_dma_pool(bush);
                    883:        if (mp) {
                    884:                vp = mp->vtob[hc];
                    885:                while (vp && (m_addr_t) vp->vaddr != a)
                    886:                        vp = vp->next;
                    887:        }
                    888:        NCR_UNLOCK_DRIVER(flags);
                    889:        return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
                    890: }
                    891: 
                    892: #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
                    893: 
                    894: #define _m_calloc_dma(np, s, n)                __m_calloc_dma(np->pdev, s, n)
                    895: #define _m_free_dma(np, p, s, n)       __m_free_dma(np->pdev, p, s, n)
                    896: #define m_calloc_dma(s, n)             _m_calloc_dma(np, s, n)
                    897: #define m_free_dma(p, s, n)            _m_free_dma(np, p, s, n)
                    898: #define _vtobus(np, p)                 __vtobus(np->pdev, p)
                    899: #define vtobus(p)                      _vtobus(np, p)
                    900: 
                    901: /*
                    902:  *  Deal with DMA mapping/unmapping.
                    903:  */
                    904: 
                    905: #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
                    906: 
                    907: /* Linux versions prior to pci bus iommu kernel interface */
                    908: 
                    909: #define __unmap_scsi_data(pdev, cmd)   do {; } while (0)
                    910: #define __map_scsi_single_data(pdev, cmd) (__vtobus(pdev,(cmd)->request_buffer))
                    911: #define __map_scsi_sg_data(pdev, cmd)  ((cmd)->use_sg)
                    912: #define __sync_scsi_data(pdev, cmd)    do {; } while (0)
                    913: 
                    914: #define scsi_sg_dma_address(sc)                vtobus((sc)->address)
                    915: #define scsi_sg_dma_len(sc)            ((sc)->length)
                    916: 
                    917: #else
                    918: 
                    919: /* Linux version with pci bus iommu kernel interface */
                    920: 
                    921: /* To keep track of the dma mapping (sg/single) that has been set */
                    922: #define __data_mapped  SCp.phase
                    923: #define __data_mapping SCp.have_data_in
                    924: 
                    925: static void __unmap_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
                    926: {
                    927:        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
                    928: 
                    929:        switch(cmd->__data_mapped) {
                    930:        case 2:
                    931:                pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
                    932:                break;
                    933:        case 1:
                    934:                pci_unmap_single(pdev, cmd->__data_mapping,
                    935:                                 cmd->request_bufflen, dma_dir);
                    936:                break;
                    937:        }
                    938:        cmd->__data_mapped = 0;
                    939: }
                    940: 
                    941: static u_long __map_scsi_single_data(pcidev_t pdev, Scsi_Cmnd *cmd)
                    942: {
                    943:        dma_addr_t mapping;
                    944:        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
                    945: 
                    946:        if (cmd->request_bufflen == 0)
                    947:                return 0;
                    948: 
                    949:        mapping = pci_map_single(pdev, cmd->request_buffer,
                    950:                                 cmd->request_bufflen, dma_dir);
                    951:        cmd->__data_mapped = 1;
                    952:        cmd->__data_mapping = mapping;
                    953: 
                    954:        return mapping;
                    955: }
                    956: 
                    957: static int __map_scsi_sg_data(pcidev_t pdev, Scsi_Cmnd *cmd)
                    958: {
                    959:        int use_sg;
                    960:        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
                    961: 
                    962:        if (cmd->use_sg == 0)
                    963:                return 0;
                    964: 
                    965:        use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
                    966:        cmd->__data_mapped = 2;
                    967:        cmd->__data_mapping = use_sg;
                    968: 
                    969:        return use_sg;
                    970: }
                    971: 
                    972: static void __sync_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
                    973: {
                    974:        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
                    975: 
                    976:        switch(cmd->__data_mapped) {
                    977:        case 2:
                    978:                pci_dma_sync_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
                    979:                break;
                    980:        case 1:
                    981:                pci_dma_sync_single(pdev, cmd->__data_mapping,
                    982:                                    cmd->request_bufflen, dma_dir);
                    983:                break;
                    984:        }
                    985: }
                    986: 
                    987: #define scsi_sg_dma_address(sc)                sg_dma_address(sc)
                    988: #define scsi_sg_dma_len(sc)            sg_dma_len(sc)
                    989: 
                    990: #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
                    991: 
                    992: #define unmap_scsi_data(np, cmd)       __unmap_scsi_data(np->pdev, cmd)
                    993: #define map_scsi_single_data(np, cmd)  __map_scsi_single_data(np->pdev, cmd)
                    994: #define map_scsi_sg_data(np, cmd)      __map_scsi_sg_data(np->pdev, cmd)
                    995: #define sync_scsi_data(np, cmd)                __sync_scsi_data(np->pdev, cmd)
                    996: 
                    997: /*==========================================================
                    998: **
                    999: **     SCSI data transfer direction
                   1000: **
                   1001: **     Until some linux kernel version near 2.3.40, 
                   1002: **     low-level scsi drivers were not told about data 
                   1003: **     transfer direction. We check the existence of this 
                   1004: **     feature that has been expected for a _long_ time by 
                   1005: **     all SCSI driver developers by just testing against 
                   1006: **     the definition of SCSI_DATA_UNKNOWN. Indeed this is 
                   1007: **     a hack, but testing against a kernel version would 
                   1008: **     have been a shame. ;-)
                   1009: **
                   1010: **==========================================================
                   1011: */
                   1012: #ifdef SCSI_DATA_UNKNOWN
                   1013: 
                   1014: #define scsi_data_direction(cmd)       (cmd->sc_data_direction)
                   1015: 
                   1016: #else
                   1017: 
                   1018: #define        SCSI_DATA_UNKNOWN       0
                   1019: #define        SCSI_DATA_WRITE         1
                   1020: #define        SCSI_DATA_READ          2
                   1021: #define        SCSI_DATA_NONE          3
                   1022: 
                   1023: static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
                   1024: {
                   1025:        int direction;
                   1026: 
                   1027:        switch((int) cmd->cmnd[0]) {
                   1028:        case 0x08:  /*  READ(6)                         08 */
                   1029:        case 0x28:  /*  READ(10)                        28 */
                   1030:        case 0xA8:  /*  READ(12)                        A8 */
                   1031:                direction = SCSI_DATA_READ;
                   1032:                break;
                   1033:        case 0x0A:  /*  WRITE(6)                        0A */
                   1034:        case 0x2A:  /*  WRITE(10)                       2A */
                   1035:        case 0xAA:  /*  WRITE(12)                       AA */
                   1036:                direction = SCSI_DATA_WRITE;
                   1037:                break;
                   1038:        default:
                   1039:                direction = SCSI_DATA_UNKNOWN;
                   1040:                break;
                   1041:        }
                   1042: 
                   1043:        return direction;
                   1044: }
                   1045: 
                   1046: #endif /* SCSI_DATA_UNKNOWN */
                   1047: 
                   1048: /*==========================================================
                   1049: **
                   1050: **     Driver setup.
                   1051: **
                   1052: **     This structure is initialized from linux config 
                   1053: **     options. It can be overridden at boot-up by the boot 
                   1054: **     command line.
                   1055: **
                   1056: **==========================================================
                   1057: */
                   1058: static struct ncr_driver_setup
                   1059:        driver_setup                    = SCSI_NCR_DRIVER_SETUP;
                   1060: 
                   1061: #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
                   1062: static struct ncr_driver_setup
                   1063:        driver_safe_setup __initdata    = SCSI_NCR_DRIVER_SAFE_SETUP;
                   1064: #endif
                   1065: 
                   1066: #define initverbose (driver_setup.verbose)
                   1067: #define bootverbose (np->verbose)
                   1068: 
                   1069: 
                   1070: /*==========================================================
                   1071: **
                   1072: **     Structures used by the detection routine to transmit 
                   1073: **     device configuration to the attach function.
                   1074: **
                   1075: **==========================================================
                   1076: */
                   1077: typedef struct {
                   1078:        int     bus;
                   1079:        u_char  device_fn;
                   1080:        u_long  base;
                   1081:        u_long  base_2;
                   1082:        u_long  io_port;
                   1083:        int     irq;
                   1084: /* port and reg fields to use INB, OUTB macros */
                   1085:        u_long  base_io;
                   1086:        volatile struct ncr_reg *reg;
                   1087: } ncr_slot;
                   1088: 
                   1089: /*==========================================================
                   1090: **
                   1091: **     Structure used to store the NVRAM content.
                   1092: **
                   1093: **==========================================================
                   1094: */
                   1095: typedef struct {
                   1096:        int type;
                   1097: #define        SCSI_NCR_SYMBIOS_NVRAM  (1)
                   1098: #define        SCSI_NCR_TEKRAM_NVRAM   (2)
                   1099: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   1100:        union {
                   1101:                Symbios_nvram Symbios;
                   1102:                Tekram_nvram Tekram;
                   1103:        } data;
                   1104: #endif
                   1105: } ncr_nvram;
                   1106: 
                   1107: /*==========================================================
                   1108: **
                   1109: **     Structure used by detection routine to save data on 
                   1110: **     each detected board for attach.
                   1111: **
                   1112: **==========================================================
                   1113: */
                   1114: typedef struct {
                   1115:        pcidev_t  pdev;
                   1116:        ncr_slot  slot;
                   1117:        ncr_chip  chip;
                   1118:        ncr_nvram *nvram;
                   1119:        u_char host_id;
                   1120: #ifdef SCSI_NCR_PQS_PDS_SUPPORT
                   1121:        u_char pqs_pds;
                   1122: #endif
                   1123:        int attach_done;
                   1124: } ncr_device;
                   1125: 
                   1126: static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device);
                   1127: 
                   1128: /*==========================================================
                   1129: **
                   1130: **     NVRAM detection and reading.
                   1131: **      
                   1132: **     Currently supported:
                   1133: **     - 24C16 EEPROM with both Symbios and Tekram layout.
                   1134: **     - 93C46 EEPROM with Tekram layout.
                   1135: **
                   1136: **==========================================================
                   1137: */
                   1138: 
                   1139: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   1140: /*
                   1141:  *  24C16 EEPROM reading.
                   1142:  *
                   1143:  *  GPOI0 - data in/data out
                   1144:  *  GPIO1 - clock
                   1145:  *  Symbios NVRAM wiring now also used by Tekram.
                   1146:  */
                   1147: 
                   1148: #define SET_BIT 0
                   1149: #define CLR_BIT 1
                   1150: #define SET_CLK 2
                   1151: #define CLR_CLK 3
                   1152: 
                   1153: /*
                   1154:  *  Set/clear data/clock bit in GPIO0
                   1155:  */
                   1156: static void __init
                   1157: S24C16_set_bit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode)
                   1158: {
                   1159:        UDELAY (5);
                   1160:        switch (bit_mode){
                   1161:        case SET_BIT:
                   1162:                *gpreg |= write_bit;
                   1163:                break;
                   1164:        case CLR_BIT:
                   1165:                *gpreg &= 0xfe;
                   1166:                break;
                   1167:        case SET_CLK:
                   1168:                *gpreg |= 0x02;
                   1169:                break;
                   1170:        case CLR_CLK:
                   1171:                *gpreg &= 0xfd;
                   1172:                break;
                   1173: 
                   1174:        }
                   1175:        OUTB (nc_gpreg, *gpreg);
                   1176:        UDELAY (5);
                   1177: }
                   1178: 
                   1179: /*
                   1180:  *  Send START condition to NVRAM to wake it up.
                   1181:  */
                   1182: static void __init S24C16_start(ncr_slot *np, u_char *gpreg)
                   1183: {
                   1184:        S24C16_set_bit(np, 1, gpreg, SET_BIT);
                   1185:        S24C16_set_bit(np, 0, gpreg, SET_CLK);
                   1186:        S24C16_set_bit(np, 0, gpreg, CLR_BIT);
                   1187:        S24C16_set_bit(np, 0, gpreg, CLR_CLK);
                   1188: }
                   1189: 
                   1190: /*
                   1191:  *  Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
                   1192:  */
                   1193: static void __init S24C16_stop(ncr_slot *np, u_char *gpreg)
                   1194: {
                   1195:        S24C16_set_bit(np, 0, gpreg, SET_CLK);
                   1196:        S24C16_set_bit(np, 1, gpreg, SET_BIT);
                   1197: }
                   1198: 
                   1199: /*
                   1200:  *  Read or write a bit to the NVRAM,
                   1201:  *  read if GPIO0 input else write if GPIO0 output
                   1202:  */
                   1203: static void __init 
                   1204: S24C16_do_bit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg)
                   1205: {
                   1206:        S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
                   1207:        S24C16_set_bit(np, 0, gpreg, SET_CLK);
                   1208:        if (read_bit)
                   1209:                *read_bit = INB (nc_gpreg);
                   1210:        S24C16_set_bit(np, 0, gpreg, CLR_CLK);
                   1211:        S24C16_set_bit(np, 0, gpreg, CLR_BIT);
                   1212: }
                   1213: 
                   1214: /*
                   1215:  *  Output an ACK to the NVRAM after reading,
                   1216:  *  change GPIO0 to output and when done back to an input
                   1217:  */
                   1218: static void __init
                   1219: S24C16_write_ack(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl)
                   1220: {
                   1221:        OUTB (nc_gpcntl, *gpcntl & 0xfe);
                   1222:        S24C16_do_bit(np, 0, write_bit, gpreg);
                   1223:        OUTB (nc_gpcntl, *gpcntl);
                   1224: }
                   1225: 
                   1226: /*
                   1227:  *  Input an ACK from NVRAM after writing,
                   1228:  *  change GPIO0 to input and when done back to an output
                   1229:  */
                   1230: static void __init 
                   1231: S24C16_read_ack(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl)
                   1232: {
                   1233:        OUTB (nc_gpcntl, *gpcntl | 0x01);
                   1234:        S24C16_do_bit(np, read_bit, 1, gpreg);
                   1235:        OUTB (nc_gpcntl, *gpcntl);
                   1236: }
                   1237: 
                   1238: /*
                   1239:  *  WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
                   1240:  *  GPIO0 must already be set as an output
                   1241:  */
                   1242: static void __init 
                   1243: S24C16_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data, 
                   1244:                  u_char *gpreg, u_char *gpcntl)
                   1245: {
                   1246:        int x;
                   1247:        
                   1248:        for (x = 0; x < 8; x++)
                   1249:                S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
                   1250:                
                   1251:        S24C16_read_ack(np, ack_data, gpreg, gpcntl);
                   1252: }
                   1253: 
                   1254: /*
                   1255:  *  READ a byte from the NVRAM and then send an ACK to say we have got it,
                   1256:  *  GPIO0 must already be set as an input
                   1257:  */
                   1258: static void __init 
                   1259: S24C16_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data, 
                   1260:                 u_char *gpreg, u_char *gpcntl)
                   1261: {
                   1262:        int x;
                   1263:        u_char read_bit;
                   1264: 
                   1265:        *read_data = 0;
                   1266:        for (x = 0; x < 8; x++) {
                   1267:                S24C16_do_bit(np, &read_bit, 1, gpreg);
                   1268:                *read_data |= ((read_bit & 0x01) << (7 - x));
                   1269:        }
                   1270: 
                   1271:        S24C16_write_ack(np, ack_data, gpreg, gpcntl);
                   1272: }
                   1273: 
                   1274: /*
                   1275:  *  Read 'len' bytes starting at 'offset'.
                   1276:  */
                   1277: static int __init 
                   1278: sym_read_S24C16_nvram (ncr_slot *np, int offset, u_char *data, int len)
                   1279: {
                   1280:        u_char  gpcntl, gpreg;
                   1281:        u_char  old_gpcntl, old_gpreg;
                   1282:        u_char  ack_data;
                   1283:        int     retv = 1;
                   1284:        int     x;
                   1285: 
                   1286:        /* save current state of GPCNTL and GPREG */
                   1287:        old_gpreg       = INB (nc_gpreg);
                   1288:        old_gpcntl      = INB (nc_gpcntl);
                   1289:        gpcntl          = old_gpcntl & 0xfc;
                   1290: 
                   1291:        /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
                   1292:        OUTB (nc_gpreg,  old_gpreg);
                   1293:        OUTB (nc_gpcntl, gpcntl);
                   1294: 
                   1295:        /* this is to set NVRAM into a known state with GPIO0/1 both low */
                   1296:        gpreg = old_gpreg;
                   1297:        S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
                   1298:        S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
                   1299:                
                   1300:        /* now set NVRAM inactive with GPIO0/1 both high */
                   1301:        S24C16_stop(np, &gpreg);
                   1302:        
                   1303:        /* activate NVRAM */
                   1304:        S24C16_start(np, &gpreg);
                   1305: 
                   1306:        /* write device code and random address MSB */
                   1307:        S24C16_write_byte(np, &ack_data,
                   1308:                0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
                   1309:        if (ack_data & 0x01)
                   1310:                goto out;
                   1311: 
                   1312:        /* write random address LSB */
                   1313:        S24C16_write_byte(np, &ack_data,
                   1314:                offset & 0xff, &gpreg, &gpcntl);
                   1315:        if (ack_data & 0x01)
                   1316:                goto out;
                   1317: 
                   1318:        /* regenerate START state to set up for reading */
                   1319:        S24C16_start(np, &gpreg);
                   1320:        
                   1321:        /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
                   1322:        S24C16_write_byte(np, &ack_data,
                   1323:                0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
                   1324:        if (ack_data & 0x01)
                   1325:                goto out;
                   1326: 
                   1327:        /* now set up GPIO0 for inputting data */
                   1328:        gpcntl |= 0x01;
                   1329:        OUTB (nc_gpcntl, gpcntl);
                   1330:                
                   1331:        /* input all requested data - only part of total NVRAM */
                   1332:        for (x = 0; x < len; x++) 
                   1333:                S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl);
                   1334: 
                   1335:        /* finally put NVRAM back in inactive mode */
                   1336:        gpcntl &= 0xfe;
                   1337:        OUTB (nc_gpcntl, gpcntl);
                   1338:        S24C16_stop(np, &gpreg);
                   1339:        retv = 0;
                   1340: out:
                   1341:        /* return GPIO0/1 to original states after having accessed NVRAM */
                   1342:        OUTB (nc_gpcntl, old_gpcntl);
                   1343:        OUTB (nc_gpreg,  old_gpreg);
                   1344: 
                   1345:        return retv;
                   1346: }
                   1347: 
                   1348: #undef SET_BIT 0
                   1349: #undef CLR_BIT 1
                   1350: #undef SET_CLK 2
                   1351: #undef CLR_CLK 3
                   1352: 
                   1353: /*
                   1354:  *  Try reading Symbios NVRAM.
                   1355:  *  Return 0 if OK.
                   1356:  */
                   1357: static int __init sym_read_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
                   1358: {
                   1359:        static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
                   1360:        u_char *data = (u_char *) nvram;
                   1361:        int len  = sizeof(*nvram);
                   1362:        u_short csum;
                   1363:        int x;
                   1364: 
                   1365:        /* probe the 24c16 and read the SYMBIOS 24c16 area */
                   1366:        if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
                   1367:                return 1;
                   1368: 
                   1369:        /* check valid NVRAM signature, verify byte count and checksum */
                   1370:        if (nvram->type != 0 ||
                   1371:            memcmp(nvram->trailer, Symbios_trailer, 6) ||
                   1372:            nvram->byte_count != len - 12)
                   1373:                return 1;
                   1374: 
                   1375:        /* verify checksum */
                   1376:        for (x = 6, csum = 0; x < len - 6; x++)
                   1377:                csum += data[x];
                   1378:        if (csum != nvram->checksum)
                   1379:                return 1;
                   1380: 
                   1381:        return 0;
                   1382: }
                   1383: 
                   1384: /*
                   1385:  *  93C46 EEPROM reading.
                   1386:  *
                   1387:  *  GPOI0 - data in
                   1388:  *  GPIO1 - data out
                   1389:  *  GPIO2 - clock
                   1390:  *  GPIO4 - chip select
                   1391:  *
                   1392:  *  Used by Tekram.
                   1393:  */
                   1394: 
                   1395: /*
                   1396:  *  Pulse clock bit in GPIO0
                   1397:  */
                   1398: static void __init T93C46_Clk(ncr_slot *np, u_char *gpreg)
                   1399: {
                   1400:        OUTB (nc_gpreg, *gpreg | 0x04);
                   1401:        UDELAY (2);
                   1402:        OUTB (nc_gpreg, *gpreg);
                   1403: }
                   1404: 
                   1405: /* 
                   1406:  *  Read bit from NVRAM
                   1407:  */
                   1408: static void __init T93C46_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg)
                   1409: {
                   1410:        UDELAY (2);
                   1411:        T93C46_Clk(np, gpreg);
                   1412:        *read_bit = INB (nc_gpreg);
                   1413: }
                   1414: 
                   1415: /*
                   1416:  *  Write bit to GPIO0
                   1417:  */
                   1418: static void __init T93C46_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg)
                   1419: {
                   1420:        if (write_bit & 0x01)
                   1421:                *gpreg |= 0x02;
                   1422:        else
                   1423:                *gpreg &= 0xfd;
                   1424:                
                   1425:        *gpreg |= 0x10;
                   1426:                
                   1427:        OUTB (nc_gpreg, *gpreg);
                   1428:        UDELAY (2);
                   1429: 
                   1430:        T93C46_Clk(np, gpreg);
                   1431: }
                   1432: 
                   1433: /*
                   1434:  *  Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
                   1435:  */
                   1436: static void __init T93C46_Stop(ncr_slot *np, u_char *gpreg)
                   1437: {
                   1438:        *gpreg &= 0xef;
                   1439:        OUTB (nc_gpreg, *gpreg);
                   1440:        UDELAY (2);
                   1441: 
                   1442:        T93C46_Clk(np, gpreg);
                   1443: }
                   1444: 
                   1445: /*
                   1446:  *  Send read command and address to NVRAM
                   1447:  */
                   1448: static void __init 
                   1449: T93C46_Send_Command(ncr_slot *np, u_short write_data, 
                   1450:                    u_char *read_bit, u_char *gpreg)
                   1451: {
                   1452:        int x;
                   1453: 
                   1454:        /* send 9 bits, start bit (1), command (2), address (6)  */
                   1455:        for (x = 0; x < 9; x++)
                   1456:                T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
                   1457: 
                   1458:        *read_bit = INB (nc_gpreg);
                   1459: }
                   1460: 
                   1461: /*
                   1462:  *  READ 2 bytes from the NVRAM
                   1463:  */
                   1464: static void __init 
                   1465: T93C46_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg)
                   1466: {
                   1467:        int x;
                   1468:        u_char read_bit;
                   1469: 
                   1470:        *nvram_data = 0;
                   1471:        for (x = 0; x < 16; x++) {
                   1472:                T93C46_Read_Bit(np, &read_bit, gpreg);
                   1473: 
                   1474:                if (read_bit & 0x01)
                   1475:                        *nvram_data |=  (0x01 << (15 - x));
                   1476:                else
                   1477:                        *nvram_data &= ~(0x01 << (15 - x));
                   1478:        }
                   1479: }
                   1480: 
                   1481: /*
                   1482:  *  Read Tekram NvRAM data.
                   1483:  */
                   1484: static int __init 
                   1485: T93C46_Read_Data(ncr_slot *np, u_short *data,int len,u_char *gpreg)
                   1486: {
                   1487:        u_char  read_bit;
                   1488:        int     x;
                   1489: 
                   1490:        for (x = 0; x < len; x++)  {
                   1491: 
                   1492:                /* output read command and address */
                   1493:                T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
                   1494:                if (read_bit & 0x01)
                   1495:                        return 1; /* Bad */
                   1496:                T93C46_Read_Word(np, &data[x], gpreg);
                   1497:                T93C46_Stop(np, gpreg);
                   1498:        }
                   1499: 
                   1500:        return 0;
                   1501: }
                   1502: 
                   1503: /*
                   1504:  *  Try reading 93C46 Tekram NVRAM.
                   1505:  */
                   1506: static int __init 
                   1507: sym_read_T93C46_nvram (ncr_slot *np, Tekram_nvram *nvram)
                   1508: {
                   1509:        u_char gpcntl, gpreg;
                   1510:        u_char old_gpcntl, old_gpreg;
                   1511:        int retv = 1;
                   1512: 
                   1513:        /* save current state of GPCNTL and GPREG */
                   1514:        old_gpreg       = INB (nc_gpreg);
                   1515:        old_gpcntl      = INB (nc_gpcntl);
                   1516: 
                   1517:        /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
                   1518:           1/2/4 out */
                   1519:        gpreg = old_gpreg & 0xe9;
                   1520:        OUTB (nc_gpreg, gpreg);
                   1521:        gpcntl = (old_gpcntl & 0xe9) | 0x09;
                   1522:        OUTB (nc_gpcntl, gpcntl);
                   1523: 
                   1524:        /* input all of NVRAM, 64 words */
                   1525:        retv = T93C46_Read_Data(np, (u_short *) nvram,
                   1526:                                sizeof(*nvram) / sizeof(short), &gpreg);
                   1527:        
                   1528:        /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
                   1529:        OUTB (nc_gpcntl, old_gpcntl);
                   1530:        OUTB (nc_gpreg,  old_gpreg);
                   1531: 
                   1532:        return retv;
                   1533: }
                   1534: 
                   1535: /*
                   1536:  *  Try reading Tekram NVRAM.
                   1537:  *  Return 0 if OK.
                   1538:  */
                   1539: static int __init 
                   1540: sym_read_Tekram_nvram (ncr_slot *np, u_short device_id, Tekram_nvram *nvram)
                   1541: {
                   1542:        u_char *data = (u_char *) nvram;
                   1543:        int len = sizeof(*nvram);
                   1544:        u_short csum;
                   1545:        int x;
                   1546: 
                   1547:        switch (device_id) {
                   1548:        case PCI_DEVICE_ID_NCR_53C885:
                   1549:        case PCI_DEVICE_ID_NCR_53C895:
                   1550:        case PCI_DEVICE_ID_NCR_53C896:
                   1551:                x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
                   1552:                                          data, len);
                   1553:                break;
                   1554:        case PCI_DEVICE_ID_NCR_53C875:
                   1555:                x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
                   1556:                                          data, len);
                   1557:                if (!x)
                   1558:                        break;
                   1559:        default:
                   1560:                x = sym_read_T93C46_nvram(np, nvram);
                   1561:                break;
                   1562:        }
                   1563:        if (x)
                   1564:                return 1;
                   1565: 
                   1566:        /* verify checksum */
                   1567:        for (x = 0, csum = 0; x < len - 1; x += 2)
                   1568:                csum += data[x] + (data[x+1] << 8);
                   1569:        if (csum != 0x1234)
                   1570:                return 1;
                   1571: 
                   1572:        return 0;
                   1573: }
                   1574: 
                   1575: #endif /* SCSI_NCR_NVRAM_SUPPORT */
                   1576: 
                   1577: /*===================================================================
                   1578: **
                   1579: **    Detect and try to read SYMBIOS and TEKRAM NVRAM.
                   1580: **
                   1581: **    Data can be used to order booting of boards.
                   1582: **
                   1583: **    Data is saved in ncr_device structure if NVRAM found. This
                   1584: **    is then used to find drive boot order for ncr_attach().
                   1585: **
                   1586: **    NVRAM data is passed to Scsi_Host_Template later during 
                   1587: **    ncr_attach() for any device set up.
                   1588: **
                   1589: **===================================================================
                   1590: */
                   1591: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   1592: static void __init ncr_get_nvram(ncr_device *devp, ncr_nvram *nvp)
                   1593: {
                   1594:        devp->nvram = nvp;
                   1595:        if (!nvp)
                   1596:                return;
                   1597:        /*
                   1598:        **    Get access to chip IO registers
                   1599:        */
                   1600: #ifdef SCSI_NCR_IOMAPPED
                   1601:        request_region(devp->slot.io_port, 128, NAME53C8XX);
                   1602:        devp->slot.base_io = devp->slot.io_port;
                   1603: #else
                   1604:        devp->slot.reg = (struct ncr_reg *) remap_pci_mem(devp->slot.base, 128);
                   1605:        if (!devp->slot.reg)
                   1606:                return;
                   1607: #endif
                   1608: 
                   1609:        /*
                   1610:        **    Try to read SYMBIOS nvram.
                   1611:        **    Try to read TEKRAM nvram if Symbios nvram not found.
                   1612:        */
                   1613:        if      (!sym_read_Symbios_nvram(&devp->slot, &nvp->data.Symbios))
                   1614:                nvp->type = SCSI_NCR_SYMBIOS_NVRAM;
                   1615:        else if (!sym_read_Tekram_nvram(&devp->slot, devp->chip.device_id,
                   1616:                                        &nvp->data.Tekram))
                   1617:                nvp->type = SCSI_NCR_TEKRAM_NVRAM;
                   1618:        else {
                   1619:                nvp->type = 0;
                   1620:                devp->nvram = 0;
                   1621:        }
                   1622: 
                   1623:        /*
                   1624:        ** Release access to chip IO registers
                   1625:        */
                   1626: #ifdef SCSI_NCR_IOMAPPED
                   1627:        release_region(devp->slot.base_io, 128);
                   1628: #else
                   1629:        unmap_pci_mem((u_long) devp->slot.reg, 128ul);
                   1630: #endif
                   1631: 
                   1632: }
                   1633: 
                   1634: /*===================================================================
                   1635: **
                   1636: **     Display the content of NVRAM for debugging purpose.
                   1637: **
                   1638: **===================================================================
                   1639: */
                   1640: #ifdef SCSI_NCR_DEBUG_NVRAM
                   1641: static void __init ncr_display_Symbios_nvram(Symbios_nvram *nvram)
                   1642: {
                   1643:        int i;
                   1644: 
                   1645:        /* display Symbios nvram host data */
                   1646:        printk(KERN_DEBUG NAME53C8XX ": HOST ID=%d%s%s%s%s%s\n",
                   1647:                nvram->host_id & 0x0f,
                   1648:                (nvram->flags  & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
                   1649:                (nvram->flags  & SYMBIOS_PARITY_ENABLE) ? " PARITY"     :"",
                   1650:                (nvram->flags  & SYMBIOS_VERBOSE_MSGS)  ? " VERBOSE"    :"", 
                   1651:                (nvram->flags  & SYMBIOS_CHS_MAPPING)   ? " CHS_ALT"    :"", 
                   1652:                (nvram->flags1 & SYMBIOS_SCAN_HI_LO)    ? " HI_LO"      :"");
                   1653: 
                   1654:        /* display Symbios nvram drive data */
                   1655:        for (i = 0 ; i < 15 ; i++) {
                   1656:                struct Symbios_target *tn = &nvram->target[i];
                   1657:                printk(KERN_DEBUG NAME53C8XX 
                   1658:                "-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
                   1659:                i,
                   1660:                (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC"       : "",
                   1661:                (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT"  : "",
                   1662:                (tn->flags & SYMBIOS_SCAN_LUNS)         ? " SCAN_LUNS"  : "",
                   1663:                (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ"        : "",
                   1664:                tn->bus_width,
                   1665:                tn->sync_period / 4,
                   1666:                tn->timeout);
                   1667:        }
                   1668: }
                   1669: 
                   1670: static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
                   1671: 
                   1672: static void __init ncr_display_Tekram_nvram(Tekram_nvram *nvram)
                   1673: {
                   1674:        int i, tags, boot_delay;
                   1675:        char *rem;
                   1676: 
                   1677:        /* display Tekram nvram host data */
                   1678:        tags = 2 << nvram->max_tags_index;
                   1679:        boot_delay = 0;
                   1680:        if (nvram->boot_delay_index < 6)
                   1681:                boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
                   1682:        switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
                   1683:        default:
                   1684:        case 0: rem = "";                       break;
                   1685:        case 1: rem = " REMOVABLE=boot device"; break;
                   1686:        case 2: rem = " REMOVABLE=all";         break;
                   1687:        }
                   1688: 
                   1689:        printk(KERN_DEBUG NAME53C8XX
                   1690:                ": HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
                   1691:                nvram->host_id & 0x0f,
                   1692:                (nvram->flags1 & SYMBIOS_SCAM_ENABLE)   ? " SCAM"       :"",
                   1693:                (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES":"",
                   1694:                (nvram->flags & TEKRAM_DRIVES_SUP_1GB)  ? " >1GB"       :"",
                   1695:                (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET"    :"",
                   1696:                (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG"    :"",
                   1697:                (nvram->flags & TEKRAM_IMMEDIATE_SEEK)  ? " IMM_SEEK"   :"",
                   1698:                (nvram->flags & TEKRAM_SCAN_LUNS)       ? " SCAN_LUNS"  :"",
                   1699:                (nvram->flags1 & TEKRAM_F2_F6_ENABLED)  ? " F2_F6"      :"",
                   1700:                rem, boot_delay, tags);
                   1701: 
                   1702:        /* display Tekram nvram drive data */
                   1703:        for (i = 0; i <= 15; i++) {
                   1704:                int sync, j;
                   1705:                struct Tekram_target *tn = &nvram->target[i];
                   1706:                j = tn->sync_index & 0xf;
                   1707:                sync = Tekram_sync[j];
                   1708:                printk(KERN_DEBUG NAME53C8XX "-%d:%s%s%s%s%s%s PERIOD=%d\n",
                   1709:                i,
                   1710:                (tn->flags & TEKRAM_PARITY_CHECK)       ? " PARITY"     : "",
                   1711:                (tn->flags & TEKRAM_SYNC_NEGO)          ? " SYNC"       : "",
                   1712:                (tn->flags & TEKRAM_DISCONNECT_ENABLE)  ? " DISC"       : "",
                   1713:                (tn->flags & TEKRAM_START_CMD)          ? " START"      : "",
                   1714:                (tn->flags & TEKRAM_TAGGED_COMMANDS)    ? " TCQ"        : "",
                   1715:                (tn->flags & TEKRAM_WIDE_NEGO)          ? " WIDE"       : "",
                   1716:                sync);
                   1717:        }
                   1718: }
                   1719: #endif /* SCSI_NCR_DEBUG_NVRAM */
                   1720: #endif /* SCSI_NCR_NVRAM_SUPPORT */
                   1721: 
                   1722: 
                   1723: /*===================================================================
                   1724: **
                   1725: **     Utility routines that protperly return data through /proc FS.
                   1726: **
                   1727: **===================================================================
                   1728: */
                   1729: #ifdef SCSI_NCR_USER_INFO_SUPPORT
                   1730: 
                   1731: struct info_str
                   1732: {
                   1733:        char *buffer;
                   1734:        int length;
                   1735:        int offset;
                   1736:        int pos;
                   1737: };
                   1738: 
                   1739: static void copy_mem_info(struct info_str *info, char *data, int len)
                   1740: {
                   1741:        if (info->pos + len > info->length)
                   1742:                len = info->length - info->pos;
                   1743: 
                   1744:        if (info->pos + len < info->offset) {
                   1745:                info->pos += len;
                   1746:                return;
                   1747:        }
                   1748:        if (info->pos < info->offset) {
                   1749:                data += (info->offset - info->pos);
                   1750:                len  -= (info->offset - info->pos);
                   1751:        }
                   1752: 
                   1753:        if (len > 0) {
                   1754:                memcpy(info->buffer + info->pos, data, len);
                   1755:                info->pos += len;
                   1756:        }
                   1757: }
                   1758: 
                   1759: static int copy_info(struct info_str *info, char *fmt, ...)
                   1760: {
                   1761:        va_list args;
                   1762:        char buf[81];
                   1763:        int len;
                   1764: 
                   1765:        va_start(args, fmt);
                   1766:        len = vsprintf(buf, fmt, args);
                   1767:        va_end(args);
                   1768: 
                   1769:        copy_mem_info(info, buf, len);
                   1770:        return len;
                   1771: }
                   1772: 
                   1773: #endif
                   1774: 
                   1775: /*===================================================================
                   1776: **
                   1777: **     Driver setup from the boot command line
                   1778: **
                   1779: **===================================================================
                   1780: */
                   1781: 
                   1782: #ifdef MODULE
                   1783: #define        ARG_SEP ' '
                   1784: #else
                   1785: #define        ARG_SEP ','
                   1786: #endif
                   1787: 
                   1788: #define OPT_TAGS               1
                   1789: #define OPT_MASTER_PARITY      2
                   1790: #define OPT_SCSI_PARITY                3
                   1791: #define OPT_DISCONNECTION      4
                   1792: #define OPT_SPECIAL_FEATURES   5
                   1793: #define OPT_ULTRA_SCSI         6
                   1794: #define OPT_FORCE_SYNC_NEGO    7
                   1795: #define OPT_REVERSE_PROBE      8
                   1796: #define OPT_DEFAULT_SYNC       9
                   1797: #define OPT_VERBOSE            10
                   1798: #define OPT_DEBUG              11
                   1799: #define OPT_BURST_MAX          12
                   1800: #define OPT_LED_PIN            13
                   1801: #define OPT_MAX_WIDE           14
                   1802: #define OPT_SETTLE_DELAY       15
                   1803: #define OPT_DIFF_SUPPORT       16
                   1804: #define OPT_IRQM               17
                   1805: #define OPT_PCI_FIX_UP         18
                   1806: #define OPT_BUS_CHECK          19
                   1807: #define OPT_OPTIMIZE           20
                   1808: #define OPT_RECOVERY           21
                   1809: #define OPT_SAFE_SETUP         22
                   1810: #define OPT_USE_NVRAM          23
                   1811: #define OPT_EXCLUDE            24
                   1812: #define OPT_HOST_ID            25
                   1813: 
                   1814: #ifdef SCSI_NCR_IARB_SUPPORT
                   1815: #define OPT_IARB               26
                   1816: #endif
                   1817: 
                   1818: static char setup_token[] __initdata = 
                   1819:        "tags:"   "mpar:"
                   1820:        "spar:"   "disc:"
                   1821:        "specf:"  "ultra:"
                   1822:        "fsn:"    "revprob:"
                   1823:        "sync:"   "verb:"
                   1824:        "debug:"  "burst:"
                   1825:        "led:"    "wide:"
                   1826:        "settle:" "diff:"
                   1827:        "irqm:"   "pcifix:"
                   1828:        "buschk:" "optim:"
                   1829:        "recovery:"
                   1830:        "safe:"   "nvram:"
                   1831:        "excl:"   "hostid:"
                   1832: #ifdef SCSI_NCR_IARB_SUPPORT
                   1833:        "iarb:"
                   1834: #endif
                   1835:        ;       /* DONNOT REMOVE THIS ';' */
                   1836: 
                   1837: #ifdef MODULE
                   1838: #define        ARG_SEP ' '
                   1839: #else
                   1840: #define        ARG_SEP ','
                   1841: #endif
                   1842: 
                   1843: static int __init get_setup_token(char *p)
                   1844: {
                   1845:        char *cur = setup_token;
                   1846:        char *pc;
                   1847:        int i = 0;
                   1848: 
                   1849:        while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
                   1850:                ++pc;
                   1851:                ++i;
                   1852:                if (!strncmp(p, cur, pc - cur))
                   1853:                        return i;
                   1854:                cur = pc;
                   1855:        }
                   1856:        return 0;
                   1857: }
                   1858: 
                   1859: 
                   1860: static int __init sym53c8xx__setup(char *str)
                   1861: {
                   1862: #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
                   1863:        char *cur = str;
                   1864:        char *pc, *pv;
                   1865:        int i, val, c;
                   1866:        int xi = 0;
                   1867: 
                   1868:        while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
                   1869:                char *pe;
                   1870: 
                   1871:                val = 0;
                   1872:                pv = pc;
                   1873:                c = *++pv;
                   1874: 
                   1875:                if      (c == 'n')
                   1876:                        val = 0;
                   1877:                else if (c == 'y')
                   1878:                        val = 1;
                   1879:                else
                   1880:                        val = (int) simple_strtoul(pv, &pe, 0);
                   1881: 
                   1882:                switch (get_setup_token(cur)) {
                   1883:                case OPT_TAGS:
                   1884:                        driver_setup.default_tags = val;
                   1885:                        if (pe && *pe == '/') {
                   1886:                                i = 0;
                   1887:                                while (*pe && *pe != ARG_SEP && 
                   1888:                                        i < sizeof(driver_setup.tag_ctrl)-1) {
                   1889:                                        driver_setup.tag_ctrl[i++] = *pe++;
                   1890:                                }
                   1891:                                driver_setup.tag_ctrl[i] = '\0';
                   1892:                        }
                   1893:                        break;
                   1894:                case OPT_MASTER_PARITY:
                   1895:                        driver_setup.master_parity = val;
                   1896:                        break;
                   1897:                case OPT_SCSI_PARITY:
                   1898:                        driver_setup.scsi_parity = val;
                   1899:                        break;
                   1900:                case OPT_DISCONNECTION:
                   1901:                        driver_setup.disconnection = val;
                   1902:                        break;
                   1903:                case OPT_SPECIAL_FEATURES:
                   1904:                        driver_setup.special_features = val;
                   1905:                        break;
                   1906:                case OPT_ULTRA_SCSI:
                   1907:                        driver_setup.ultra_scsi = val;
                   1908:                        break;
                   1909:                case OPT_FORCE_SYNC_NEGO:
                   1910:                        driver_setup.force_sync_nego = val;
                   1911:                        break;
                   1912:                case OPT_REVERSE_PROBE:
                   1913:                        driver_setup.reverse_probe = val;
                   1914:                        break;
                   1915:                case OPT_DEFAULT_SYNC:
                   1916:                        driver_setup.default_sync = val;
                   1917:                        break;
                   1918:                case OPT_VERBOSE:
                   1919:                        driver_setup.verbose = val;
                   1920:                        break;
                   1921:                case OPT_DEBUG:
                   1922:                        driver_setup.debug = val;
                   1923:                        break;
                   1924:                case OPT_BURST_MAX:
                   1925:                        driver_setup.burst_max = val;
                   1926:                        break;
                   1927:                case OPT_LED_PIN:
                   1928:                        driver_setup.led_pin = val;
                   1929:                        break;
                   1930:                case OPT_MAX_WIDE:
                   1931:                        driver_setup.max_wide = val? 1:0;
                   1932:                        break;
                   1933:                case OPT_SETTLE_DELAY:
                   1934:                        driver_setup.settle_delay = val;
                   1935:                        break;
                   1936:                case OPT_DIFF_SUPPORT:
                   1937:                        driver_setup.diff_support = val;
                   1938:                        break;
                   1939:                case OPT_IRQM:
                   1940:                        driver_setup.irqm = val;
                   1941:                        break;
                   1942:                case OPT_PCI_FIX_UP:
                   1943:                        driver_setup.pci_fix_up = val;
                   1944:                        break;
                   1945:                case OPT_BUS_CHECK:
                   1946:                        driver_setup.bus_check = val;
                   1947:                        break;
                   1948:                case OPT_OPTIMIZE:
                   1949:                        driver_setup.optimize = val;
                   1950:                        break;
                   1951:                case OPT_RECOVERY:
                   1952:                        driver_setup.recovery = val;
                   1953:                        break;
                   1954:                case OPT_USE_NVRAM:
                   1955:                        driver_setup.use_nvram = val;
                   1956:                        break;
                   1957:                case OPT_SAFE_SETUP:
                   1958:                        memcpy(&driver_setup, &driver_safe_setup,
                   1959:                                sizeof(driver_setup));
                   1960:                        break;
                   1961:                case OPT_EXCLUDE:
                   1962:                        if (xi < SCSI_NCR_MAX_EXCLUDES)
                   1963:                                driver_setup.excludes[xi++] = val;
                   1964:                        break;
                   1965:                case OPT_HOST_ID:
                   1966:                        driver_setup.host_id = val;
                   1967:                        break;
                   1968: #ifdef SCSI_NCR_IARB_SUPPORT
                   1969:                case OPT_IARB:
                   1970:                        driver_setup.iarb = val;
                   1971:                        break;
                   1972: #endif
                   1973:                default:
                   1974:                        printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
                   1975:                        break;
                   1976:                }
                   1977: 
                   1978:                if ((cur = strchr(cur, ARG_SEP)) != NULL)
                   1979:                        ++cur;
                   1980:        }
                   1981: #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
                   1982:        return 1;
                   1983: }
                   1984: 
                   1985: /*===================================================================
                   1986: **
                   1987: **     Get device queue depth from boot command line.
                   1988: **
                   1989: **===================================================================
                   1990: */
                   1991: #define DEF_DEPTH      (driver_setup.default_tags)
                   1992: #define ALL_TARGETS    -2
                   1993: #define NO_TARGET      -1
                   1994: #define ALL_LUNS       -2
                   1995: #define NO_LUN         -1
                   1996: 
                   1997: static int device_queue_depth(int unit, int target, int lun)
                   1998: {
                   1999:        int c, h, t, u, v;
                   2000:        char *p = driver_setup.tag_ctrl;
                   2001:        char *ep;
                   2002: 
                   2003:        h = -1;
                   2004:        t = NO_TARGET;
                   2005:        u = NO_LUN;
                   2006:        while ((c = *p++) != 0) {
                   2007:                v = simple_strtoul(p, &ep, 0);
                   2008:                switch(c) {
                   2009:                case '/':
                   2010:                        ++h;
                   2011:                        t = ALL_TARGETS;
                   2012:                        u = ALL_LUNS;
                   2013:                        break;
                   2014:                case 't':
                   2015:                        if (t != target)
                   2016:                                t = (target == v) ? v : NO_TARGET;
                   2017:                        u = ALL_LUNS;
                   2018:                        break;
                   2019:                case 'u':
                   2020:                        if (u != lun)
                   2021:                                u = (lun == v) ? v : NO_LUN;
                   2022:                        break;
                   2023:                case 'q':
                   2024:                        if (h == unit &&
                   2025:                                (t == ALL_TARGETS || t == target) &&
                   2026:                                (u == ALL_LUNS    || u == lun))
                   2027:                                return v;
                   2028:                        break;
                   2029:                case '-':
                   2030:                        t = ALL_TARGETS;
                   2031:                        u = ALL_LUNS;
                   2032:                        break;
                   2033:                default:
                   2034:                        break;
                   2035:                }
                   2036:                p = ep;
                   2037:        }
                   2038:        return DEF_DEPTH;
                   2039: }
                   2040: 
                   2041: /*===================================================================
                   2042: **
                   2043: **     Print out information about driver configuration.
                   2044: **
                   2045: **===================================================================
                   2046: */
                   2047: static void __init ncr_print_driver_setup(void)
                   2048: {
                   2049: #define YesNo(y)       y ? 'y' : 'n'
                   2050:        printk (NAME53C8XX ": setup=disc:%c,specf:%d,ultra:%d,tags:%d,sync:%d,"
                   2051:                "burst:%d,wide:%c,diff:%d,revprob:%c,buschk:0x%x\n",
                   2052:                YesNo(driver_setup.disconnection),
                   2053:                driver_setup.special_features,
                   2054:                driver_setup.ultra_scsi,
                   2055:                driver_setup.default_tags,
                   2056:                driver_setup.default_sync,
                   2057:                driver_setup.burst_max,
                   2058:                YesNo(driver_setup.max_wide),
                   2059:                driver_setup.diff_support,
                   2060:                YesNo(driver_setup.reverse_probe),
                   2061:                driver_setup.bus_check);
                   2062: 
                   2063:        printk (NAME53C8XX ": setup=mpar:%c,spar:%c,fsn=%c,verb:%d,debug:0x%x,"
                   2064:                "led:%c,settle:%d,irqm:0x%x,nvram:0x%x,pcifix:0x%x\n",
                   2065:                YesNo(driver_setup.master_parity),
                   2066:                YesNo(driver_setup.scsi_parity),
                   2067:                YesNo(driver_setup.force_sync_nego),
                   2068:                driver_setup.verbose,
                   2069:                driver_setup.debug,
                   2070:                YesNo(driver_setup.led_pin),
                   2071:                driver_setup.settle_delay,
                   2072:                driver_setup.irqm,
                   2073:                driver_setup.use_nvram,
                   2074:                driver_setup.pci_fix_up);
                   2075: #undef YesNo
                   2076: }
                   2077: 
                   2078: /*===================================================================
                   2079: **
                   2080: **   SYM53C8XX devices description table.
                   2081: **
                   2082: **===================================================================
                   2083: */
                   2084: 
                   2085: static ncr_chip        ncr_chip_table[] __initdata     = SCSI_NCR_CHIP_TABLE;
                   2086: 
                   2087: #ifdef SCSI_NCR_PQS_PDS_SUPPORT
                   2088: /*===================================================================
                   2089: **
                   2090: **    Detect all NCR PQS/PDS boards and keep track of their bus nr.
                   2091: **
                   2092: **    The NCR PQS or PDS card is constructed as a DEC bridge
                   2093: **    behind which sit a proprietary NCR memory controller and
                   2094: **    four or two 53c875s as separate devices.  In its usual mode
                   2095: **    of operation, the 875s are slaved to the memory controller
                   2096: **    for all transfers.  We can tell if an 875 is part of a
                   2097: **    PQS/PDS or not since if it is, it will be on the same bus
                   2098: **    as the memory controller.  To operate with the Linux
                   2099: **    driver, the memory controller is disabled and the 875s
                   2100: **    freed to function independently.  The only wrinkle is that
                   2101: **    the preset SCSI ID (which may be zero) must be read in from
                   2102: **    a special configuration space register of the 875.
                   2103: **
                   2104: **===================================================================
                   2105: */
                   2106: #define        SCSI_NCR_MAX_PQS_BUS    16
                   2107: static int pqs_bus[SCSI_NCR_MAX_PQS_BUS] __initdata = { 0 };
                   2108: 
                   2109: static void __init ncr_detect_pqs_pds(void)
                   2110: {
                   2111:        short index;
                   2112:        pcidev_t dev = PCIDEV_NULL;
                   2113: 
                   2114:        for(index=0; index < SCSI_NCR_MAX_PQS_BUS; index++) {
                   2115:                u_char tmp;
                   2116: 
                   2117:                dev = pci_find_device(0x101a, 0x0009, dev);
                   2118:                if (dev == PCIDEV_NULL) {
                   2119:                        pqs_bus[index] = -1;
                   2120:                        break;
                   2121:                }
                   2122:                printk(KERN_INFO NAME53C8XX ": NCR PQS/PDS memory controller detected on bus %d\n", PciBusNumber(dev));
                   2123:                pci_read_config_byte(dev, 0x44, &tmp);
                   2124:                /* bit 1: allow individual 875 configuration */
                   2125:                tmp |= 0x2;
                   2126:                pci_write_config_byte(dev, 0x44, tmp);
                   2127:                pci_read_config_byte(dev, 0x45, &tmp);
                   2128:                /* bit 2: drive individual 875 interrupts to the bus */
                   2129:                tmp |= 0x4;
                   2130:                pci_write_config_byte(dev, 0x45, tmp);
                   2131: 
                   2132:                pqs_bus[index] = PciBusNumber(dev);
                   2133:        }
                   2134: }
                   2135: #endif /* SCSI_NCR_PQS_PDS_SUPPORT */
                   2136: 
                   2137: /*===================================================================
                   2138: **
                   2139: **   Read and check the PCI configuration for any detected NCR 
                   2140: **   boards and save data for attaching after all boards have 
                   2141: **   been detected.
                   2142: **
                   2143: **===================================================================
                   2144: */
                   2145: static int __init
                   2146: sym53c8xx_pci_init(Scsi_Host_Template *tpnt, pcidev_t pdev, ncr_device *device)
                   2147: {
                   2148:        u_short vendor_id, device_id, command;
                   2149:        u_char cache_line_size, latency_timer;
                   2150:        u_char suggested_cache_line_size = 0;
                   2151:        u_char pci_fix_up = driver_setup.pci_fix_up;
                   2152:        u_char revision;
                   2153:        u_int irq;
                   2154:        u_long base, base_2, io_port; 
                   2155:        int i;
                   2156:        ncr_chip *chip;
                   2157: 
                   2158:        printk(KERN_INFO NAME53C8XX ": at PCI bus %d, device %d, function %d\n",
                   2159:                PciBusNumber(pdev),
                   2160:                (int) (PciDeviceFn(pdev) & 0xf8) >> 3,
                   2161:                (int) (PciDeviceFn(pdev) & 7));
                   2162: 
                   2163: #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
                   2164:        if (!pci_dma_supported(pdev, (dma_addr_t) (0xffffffffUL))) {
                   2165:                printk(KERN_WARNING NAME53C8XX
                   2166:                       "32 BIT PCI BUS DMA ADDRESSING NOT SUPPORTED\n");
                   2167:                return -1;
                   2168:        }
                   2169: #endif
                   2170: 
                   2171:        /*
                   2172:        **    Read info from the PCI config space.
                   2173:        **    pci_read_config_xxx() functions are assumed to be used for 
                   2174:        **    successfully detected PCI devices.
                   2175:        */
                   2176:        vendor_id = PciVendorId(pdev);
                   2177:        device_id = PciDeviceId(pdev);
                   2178:        irq       = PciIrqLine(pdev);
                   2179:        i =     0;
                   2180:        i =     pci_get_base_address(pdev, i, &io_port);
                   2181:        i =     pci_get_base_address(pdev, i, &base);
                   2182:        (void)  pci_get_base_address(pdev, i, &base_2);
                   2183: 
                   2184:        pci_read_config_word(pdev, PCI_COMMAND,         &command);
                   2185:        pci_read_config_byte(pdev, PCI_CLASS_REVISION,  &revision);
                   2186:        pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size);
                   2187:        pci_read_config_byte(pdev, PCI_LATENCY_TIMER,   &latency_timer);
                   2188: 
                   2189: #ifdef SCSI_NCR_PQS_PDS_SUPPORT
                   2190:        /*
                   2191:        **    Match the BUS number for PQS/PDS devices.
                   2192:        **    Read the SCSI ID from a special register mapped
                   2193:        **    into the configuration space of the individual
                   2194:        **    875s.  This register is set up by the PQS bios
                   2195:        */
                   2196:        for(i = 0; i < SCSI_NCR_MAX_PQS_BUS && pqs_bus[i] != -1; i++) {
                   2197:                u_char tmp;
                   2198:                if (pqs_bus[i] == PciBusNumber(pdev)) {
                   2199:                        pci_read_config_byte(pdev, 0x84, &tmp);
                   2200:                        device->pqs_pds = 1;
                   2201:                        device->host_id = tmp;
                   2202:                        break;
                   2203:                }
                   2204:        }
                   2205: #endif /* SCSI_NCR_PQS_PDS_SUPPORT */
                   2206: 
                   2207:        /*
                   2208:        **      If user excludes this chip, donnot initialize it.
                   2209:        */
                   2210:        for (i = 0 ; i < SCSI_NCR_MAX_EXCLUDES ; i++) {
                   2211:                if (driver_setup.excludes[i] ==
                   2212:                                (io_port & PCI_BASE_ADDRESS_IO_MASK))
                   2213:                        return -1;
                   2214:        }
                   2215:        /*
                   2216:        **    Check if the chip is supported
                   2217:        */
                   2218:        if ((device_id == PCI_DEVICE_ID_LSI_53C1010) ||
                   2219:                        (device_id == PCI_DEVICE_ID_LSI_53C1010_66)){
                   2220:                printk(NAME53C8XX ": not initializing, device not supported\n");
                   2221:                return -1;
                   2222:        }
                   2223:        chip = 0;
                   2224:        for (i = 0; i < sizeof(ncr_chip_table)/sizeof(ncr_chip_table[0]); i++) {
                   2225:                if (device_id != ncr_chip_table[i].device_id)
                   2226:                        continue;
                   2227:                if (revision > ncr_chip_table[i].revision_id)
                   2228:                        continue;
                   2229:                chip = &device->chip;
                   2230:                memcpy(chip, &ncr_chip_table[i], sizeof(*chip));
                   2231:                chip->revision_id = revision;
                   2232:                break;
                   2233:        }
                   2234: 
                   2235:        /*
                   2236:        **      Ignore Symbios chips controlled by SISL RAID controller.
                   2237:        **      This controller sets value 0x52414944 at RAM end - 16.
                   2238:        */
                   2239: #if defined(__i386__) && !defined(SCSI_NCR_PCI_MEM_NOT_SUPPORTED)
                   2240:        if (chip && (base_2 & PCI_BASE_ADDRESS_MEM_MASK)) {
                   2241:                unsigned int ram_size, ram_val;
                   2242:                u_long ram_ptr;
                   2243: 
                   2244:                if (chip->features & FE_RAM8K)
                   2245:                        ram_size = 8192;
                   2246:                else
                   2247:                        ram_size = 4096;
                   2248: 
                   2249:                ram_ptr = remap_pci_mem(base_2 & PCI_BASE_ADDRESS_MEM_MASK,
                   2250:                                        ram_size);
                   2251:                if (ram_ptr) {
                   2252:                        ram_val = readl_raw(ram_ptr + ram_size - 16);
                   2253:                        unmap_pci_mem(ram_ptr, ram_size);
                   2254:                        if (ram_val == 0x52414944) {
                   2255:                                printk(NAME53C8XX": not initializing, "
                   2256:                                       "driven by SISL RAID controller.\n");
                   2257:                                return -1;
                   2258:                        }
                   2259:                }
                   2260:        }
                   2261: #endif /* i386 and PCI MEMORY accessible */
                   2262: 
                   2263:        if (!chip) {
                   2264:                printk(NAME53C8XX ": not initializing, device not supported\n");
                   2265:                return -1;
                   2266:        }
                   2267: 
                   2268: #ifdef __powerpc__
                   2269:        /*
                   2270:        **      Fix-up for power/pc.
                   2271:        **      Should not be performed by the driver.
                   2272:        */
                   2273:        if ((command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
                   2274:                    != (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
                   2275:                printk(NAME53C8XX ": setting%s%s...\n",
                   2276:                (command & PCI_COMMAND_IO)     ? "" : " PCI_COMMAND_IO",
                   2277:                (command & PCI_COMMAND_MEMORY) ? "" : " PCI_COMMAND_MEMORY");
                   2278:                command |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
                   2279:                pci_write_config_word(pdev, PCI_COMMAND, command);
                   2280:        }
                   2281: 
                   2282: #if LINUX_VERSION_CODE < LinuxVersionCode(2,2,0)
                   2283:        if ( is_prep ) {
                   2284:                if (io_port >= 0x10000000) {
                   2285:                        printk(NAME53C8XX ": reallocating io_port (Wacky IBM)");
                   2286:                        io_port = (io_port & 0x00FFFFFF) | 0x01000000;
                   2287:                        pci_write_config_dword(pdev,
                   2288:                                               PCI_BASE_ADDRESS_0, io_port);
                   2289:                }
                   2290:                if (base >= 0x10000000) {
                   2291:                        printk(NAME53C8XX ": reallocating base (Wacky IBM)");
                   2292:                        base = (base & 0x00FFFFFF) | 0x01000000;
                   2293:                        pci_write_config_dword(pdev,
                   2294:                                               PCI_BASE_ADDRESS_1, base);
                   2295:                }
                   2296:                if (base_2 >= 0x10000000) {
                   2297:                        printk(NAME53C8XX ": reallocating base2 (Wacky IBM)");
                   2298:                        base_2 = (base_2 & 0x00FFFFFF) | 0x01000000;
                   2299:                        pci_write_config_dword(pdev,
                   2300:                                               PCI_BASE_ADDRESS_2, base_2);
                   2301:                }
                   2302:        }
                   2303: #endif
                   2304: #endif /* __powerpc__ */
                   2305: 
                   2306: #if defined(__sparc__) && (LINUX_VERSION_CODE < LinuxVersionCode(2,3,0))
                   2307:        /*
                   2308:         *      Severall fix-ups for sparc.
                   2309:         *
                   2310:         *      Should not be performed by the driver, which is why all
                   2311:         *      this crap is cleaned up in 2.4.x
                   2312:         */
                   2313: 
                   2314:        base = __pa(base);
                   2315:        base_2 = __pa(base_2);
                   2316: 
                   2317:        if (!(command & PCI_COMMAND_MASTER)) {
                   2318:                if (initverbose >= 2)
                   2319:                        printk("ncr53c8xx: setting PCI_COMMAND_MASTER bit (fixup)\n");
                   2320:                command |= PCI_COMMAND_MASTER;
                   2321:                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
                   2322:                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
                   2323:        }
                   2324: 
                   2325:        if ((chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
                   2326:                if (initverbose >= 2)
                   2327:                        printk("ncr53c8xx: setting PCI_COMMAND_INVALIDATE bit (fixup)\n");
                   2328:                command |= PCI_COMMAND_INVALIDATE;
                   2329:                pcibios_write_config_word(bus, device_fn, PCI_COMMAND, command);
                   2330:                pcibios_read_config_word(bus, device_fn, PCI_COMMAND, &command);
                   2331:        }
                   2332: 
                   2333:        if ((chip->features & FE_CLSE) && !cache_line_size) {
                   2334:                /* PCI_CACHE_LINE_SIZE value is in 32-bit words. */
                   2335:                cache_line_size = 64 / sizeof(u_int32);
                   2336:                if (initverbose >= 2)
                   2337:                        printk("ncr53c8xx: setting PCI_CACHE_LINE_SIZE to %d (fixup)\n",
                   2338:                               cache_line_size);
                   2339:                pcibios_write_config_byte(bus, device_fn,
                   2340:                                          PCI_CACHE_LINE_SIZE, cache_line_size);
                   2341:                pcibios_read_config_byte(bus, device_fn,
                   2342:                                         PCI_CACHE_LINE_SIZE, &cache_line_size);
                   2343:        }
                   2344: 
                   2345:        if (!latency_timer) {
                   2346:                unsigned char min_gnt;
                   2347: 
                   2348:                pcibios_read_config_byte(bus, device_fn,
                   2349:                                         PCI_MIN_GNT, &min_gnt);
                   2350:                if (min_gnt == 0)
                   2351:                        latency_timer = 128;
                   2352:                else
                   2353:                        latency_timer = ((min_gnt << 3) & 0xff);
                   2354:                printk("ncr53c8xx: setting PCI_LATENCY_TIMER to %d bus clocks (fixup)\n", latency_timer);
                   2355:                pcibios_write_config_byte(bus, device_fn,
                   2356:                                          PCI_LATENCY_TIMER, latency_timer);
                   2357:                pcibios_read_config_byte(bus, device_fn,
                   2358:                                         PCI_LATENCY_TIMER, &latency_timer);
                   2359:        }
                   2360: #endif /* __sparc__ && (LINUX_VERSION_CODE < LinuxVersionCode(2,3,0)) */
                   2361: 
                   2362: #if defined(__i386__) && !defined(MODULE)
                   2363:        if (!cache_line_size) {
                   2364: #if LINUX_VERSION_CODE < LinuxVersionCode(2,1,75)
                   2365:                extern char x86;
                   2366:                switch(x86) {
                   2367: #else
                   2368:                switch(boot_cpu_data.x86) {
                   2369: #endif
                   2370:                case 4: suggested_cache_line_size = 4; break;
                   2371:                case 6:
                   2372:                case 5: suggested_cache_line_size = 8; break;
                   2373:                }
                   2374:        }
                   2375: #endif /* __i386__ */
                   2376: 
                   2377:        /*
                   2378:        **    Check availability of IO space, memory space.
                   2379:        **    Enable master capability if not yet.
                   2380:        **
                   2381:        **    We shouldn't have to care about the IO region when 
                   2382:        **    we are using MMIO. But calling check_region() from 
                   2383:        **    both the ncr53c8xx and the sym53c8xx drivers prevents 
                   2384:        **    from attaching devices from the both drivers.
                   2385:        **    If you have a better idea, let me know.
                   2386:        */
                   2387: /* #ifdef SCSI_NCR_IOMAPPED */
                   2388: #if 1
                   2389:        if (!(command & PCI_COMMAND_IO)) { 
                   2390:                printk(NAME53C8XX ": I/O base address (0x%lx) disabled.\n",
                   2391:                        (long) io_port);
                   2392:                io_port = 0;
                   2393:        }
                   2394: #endif
                   2395:        if (!(command & PCI_COMMAND_MEMORY)) {
                   2396:                printk(NAME53C8XX ": PCI_COMMAND_MEMORY not set.\n");
                   2397:                base    = 0;
                   2398:                base_2  = 0;
                   2399:        }
                   2400:        io_port &= PCI_BASE_ADDRESS_IO_MASK;
                   2401:        base    &= PCI_BASE_ADDRESS_MEM_MASK;
                   2402:        base_2  &= PCI_BASE_ADDRESS_MEM_MASK;
                   2403: 
                   2404: /* #ifdef SCSI_NCR_IOMAPPED */
                   2405: #if 1
                   2406:        if (io_port && check_region (io_port, 128)) {
                   2407:                printk(NAME53C8XX ": IO region 0x%lx[0..127] is in use\n",
                   2408:                        (long) io_port);
                   2409:                io_port = 0;
                   2410:        }
                   2411:        if (!io_port)
                   2412:                return -1;
                   2413: #endif
                   2414: #ifndef SCSI_NCR_IOMAPPED
                   2415:        if (!base) {
                   2416:                printk(NAME53C8XX ": MMIO base address disabled.\n");
                   2417:                return -1;
                   2418:        }
                   2419: #endif
                   2420: 
                   2421: /* The ncr53c8xx driver never did set the PCI parity bit.      */
                   2422: /* Since setting this bit is known to trigger spurious MDPE    */
                   2423: /* errors on some 895 controllers when noise on power lines is */
                   2424: /* too high, I donnot want to change previous ncr53c8xx driver */
                   2425: /* behaviour on that point (the sym53c8xx driver set this bit).        */
                   2426: #if 0
                   2427:        /*
                   2428:        **    Set MASTER capable and PARITY bit, if not yet.
                   2429:        */
                   2430:        if ((command & (PCI_COMMAND_MASTER | PCI_COMMAND_PARITY))
                   2431:                     != (PCI_COMMAND_MASTER | PCI_COMMAND_PARITY)) {
                   2432:                printk(NAME53C8XX ": setting%s%s...(fix-up)\n",
                   2433:                (command & PCI_COMMAND_MASTER) ? "" : " PCI_COMMAND_MASTER",
                   2434:                (command & PCI_COMMAND_PARITY) ? "" : " PCI_COMMAND_PARITY");
                   2435:                command |= (PCI_COMMAND_MASTER | PCI_COMMAND_PARITY);
                   2436:                pci_write_config_word(pdev, PCI_COMMAND, command);
                   2437:        }
                   2438: #else
                   2439:        /*
                   2440:        **    Set MASTER capable if not yet.
                   2441:        */
                   2442:        if ((command & PCI_COMMAND_MASTER) != PCI_COMMAND_MASTER) {
                   2443:                printk(NAME53C8XX ": setting PCI_COMMAND_MASTER...(fix-up)\n");
                   2444:                command |= PCI_COMMAND_MASTER;
                   2445:                pci_write_config_word(pdev, PCI_COMMAND, command);
                   2446:        }
                   2447: #endif
                   2448: 
                   2449:        /*
                   2450:        **    Fix some features according to driver setup.
                   2451:        */
                   2452:        if (!(driver_setup.special_features & 1))
                   2453:                chip->features &= ~FE_SPECIAL_SET;
                   2454:        else {
                   2455:                if (driver_setup.special_features & 2)
                   2456:                        chip->features &= ~FE_WRIE;
                   2457:                if (driver_setup.special_features & 4)
                   2458:                        chip->features &= ~FE_NOPM;
                   2459:        }
                   2460:        if (driver_setup.ultra_scsi < 2 && (chip->features & FE_ULTRA2)) {
                   2461:                chip->features |=  FE_ULTRA;
                   2462:                chip->features &= ~FE_ULTRA2;
                   2463:        }
                   2464:        if (driver_setup.ultra_scsi < 1)
                   2465:                chip->features &= ~FE_ULTRA;
                   2466:        if (!driver_setup.max_wide)
                   2467:                chip->features &= ~FE_WIDE;
                   2468: 
                   2469:        /*
                   2470:        **      Some features are required to be enabled in order to 
                   2471:        **      work around some chip problems. :) ;)
                   2472:        **      (ITEM 12 of a DEL about the 896 I haven't yet).
                   2473:        **      We must ensure the chip will use WRITE AND INVALIDATE.
                   2474:        **      The revision number limit is for now arbitrary.
                   2475:        */
                   2476:        if (device_id == PCI_DEVICE_ID_NCR_53C896 && revision <= 0x10) {
                   2477:                chip->features  |= (FE_WRIE | FE_CLSE);
                   2478:                pci_fix_up      |=  3;  /* Force appropriate PCI fix-up */
                   2479:        }
                   2480: 
                   2481: #ifdef SCSI_NCR_PCI_FIX_UP_SUPPORT
                   2482:        /*
                   2483:        **    Try to fix up PCI config according to wished features.
                   2484:        */
                   2485:        if ((pci_fix_up & 1) && (chip->features & FE_CLSE) && 
                   2486:            !cache_line_size && suggested_cache_line_size) {
                   2487:                cache_line_size = suggested_cache_line_size;
                   2488:                pci_write_config_byte(pdev,
                   2489:                                      PCI_CACHE_LINE_SIZE, cache_line_size);
                   2490:                printk(NAME53C8XX ": PCI_CACHE_LINE_SIZE set to %d (fix-up).\n",
                   2491:                        cache_line_size);
                   2492:        }
                   2493: 
                   2494:        if ((pci_fix_up & 2) && cache_line_size &&
                   2495:            (chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
                   2496:                printk(NAME53C8XX": setting PCI_COMMAND_INVALIDATE (fix-up)\n");
                   2497:                command |= PCI_COMMAND_INVALIDATE;
                   2498:                pci_write_config_word(pdev, PCI_COMMAND, command);
                   2499:        }
                   2500: 
                   2501:        /*
                   2502:        **    Tune PCI LATENCY TIMER according to burst max length transfer.
                   2503:        **    (latency timer >= burst length + 6, we add 10 to be quite sure)
                   2504:        */
                   2505: 
                   2506:        if (chip->burst_max && (latency_timer == 0 || (pci_fix_up & 4))) {
                   2507:                u_char lt = (1 << chip->burst_max) + 6 + 10;
                   2508:                if (latency_timer < lt) {
                   2509:                        printk(NAME53C8XX 
                   2510:                               ": changing PCI_LATENCY_TIMER from %d to %d.\n",
                   2511:                               (int) latency_timer, (int) lt);
                   2512:                        latency_timer = lt;
                   2513:                        pci_write_config_byte(pdev,
                   2514:                                              PCI_LATENCY_TIMER, latency_timer);
                   2515:                }
                   2516:        }
                   2517: 
                   2518: #endif /* SCSI_NCR_PCI_FIX_UP_SUPPORT */
                   2519: 
                   2520:        /*
                   2521:        **    Initialise ncr_device structure with items required by ncr_attach.
                   2522:        */
                   2523:        device->pdev            = pdev;
                   2524:        device->slot.bus        = PciBusNumber(pdev);
                   2525:        device->slot.device_fn  = PciDeviceFn(pdev);
                   2526:        device->slot.base       = base;
                   2527:        device->slot.base_2     = base_2;
                   2528:        device->slot.io_port    = io_port;
                   2529:        device->slot.irq        = irq;
                   2530:        device->attach_done     = 0;
                   2531: 
                   2532:        return 0;
                   2533: }
                   2534: 
                   2535: /*===================================================================
                   2536: **
                   2537: **    Detect all 53c8xx hosts and then attach them.
                   2538: **
                   2539: **    If we are using NVRAM, once all hosts are detected, we need to 
                   2540: **    check any NVRAM for boot order in case detect and boot order 
                   2541: **    differ and attach them using the order in the NVRAM.
                   2542: **
                   2543: **    If no NVRAM is found or data appears invalid attach boards in 
                   2544: **    the the order they are detected.
                   2545: **
                   2546: **===================================================================
                   2547: */
                   2548: static int __init 
                   2549: sym53c8xx__detect(Scsi_Host_Template *tpnt, u_short ncr_chip_ids[], int chips)
                   2550: {
                   2551:        pcidev_t pcidev;
                   2552:        int i, j, hosts, count;
                   2553:        int attach_count = 0;
                   2554:        ncr_device *devtbl, *devp;
                   2555: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   2556:        ncr_nvram  nvram0, nvram, *nvp;
                   2557: #endif
                   2558: 
                   2559:        /*
                   2560:        **    PCI is required.
                   2561:        */
                   2562:        if (!pci_present())
                   2563:                return 0;
                   2564: 
                   2565: #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
                   2566:        ncr_debug = driver_setup.debug;
                   2567: #endif
                   2568:        if (initverbose >= 2)
                   2569:                ncr_print_driver_setup();
                   2570: 
                   2571:        /*
                   2572:        **      Allocate the device table since we donnot want to 
                   2573:        **      overflow the kernel stack.
                   2574:        **      1 x 4K PAGE is enough for more than 40 devices for i386.
                   2575:        */
                   2576:        devtbl = m_calloc(PAGE_SIZE, "devtbl");
                   2577:        if (!devtbl)
                   2578:                return 0;
                   2579: 
                   2580:        /*
                   2581:        **    Detect all NCR PQS/PDS memory controllers.
                   2582:        */
                   2583: #ifdef SCSI_NCR_PQS_PDS_SUPPORT
                   2584:        ncr_detect_pqs_pds();
                   2585: #endif
                   2586: 
                   2587:        /* 
                   2588:        **    Detect all 53c8xx hosts.
                   2589:        **    Save the first Symbios NVRAM content if any 
                   2590:        **    for the boot order.
                   2591:        */
                   2592:        hosts   = PAGE_SIZE             / sizeof(*devtbl);
                   2593: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   2594:        nvp = (driver_setup.use_nvram & 0x1) ? &nvram0 : 0;
                   2595: #endif
                   2596:        j = 0;
                   2597:        count = 0;
                   2598:        pcidev = PCIDEV_NULL;
                   2599:        while (1) {
                   2600:                char *msg = "";
                   2601:                if (count >= hosts)
                   2602:                        break;
                   2603:                if (j >= chips)
                   2604:                        break;
                   2605:                i = driver_setup.reverse_probe ? chips - 1 - j : j;
                   2606:                pcidev = pci_find_device(PCI_VENDOR_ID_NCR, ncr_chip_ids[i],
                   2607:                                         pcidev);
                   2608:                if (pcidev == PCIDEV_NULL) {
                   2609:                        ++j;
                   2610:                        continue;
                   2611:                }
                   2612:                /* Some HW as the HP LH4 may report twice PCI devices */
                   2613:                for (i = 0; i < count ; i++) {
                   2614:                        if (devtbl[i].slot.bus       == PciBusNumber(pcidev) && 
                   2615:                            devtbl[i].slot.device_fn == PciDeviceFn(pcidev))
                   2616:                                break;
                   2617:                }
                   2618:                if (i != count) /* Ignore this device if we already have it */
                   2619:                        continue;
                   2620:                devp = &devtbl[count];
                   2621:                devp->host_id = driver_setup.host_id;
                   2622:                devp->attach_done = 0;
                   2623:                if (sym53c8xx_pci_init(tpnt, pcidev, devp)) {
                   2624:                        continue;
                   2625:                }
                   2626:                ++count;
                   2627: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   2628:                if (nvp) {
                   2629:                        ncr_get_nvram(devp, nvp);
                   2630:                        switch(nvp->type) {
                   2631:                        case SCSI_NCR_SYMBIOS_NVRAM:
                   2632:                                /*
                   2633:                                 *   Switch to the other nvram buffer, so that 
                   2634:                                 *   nvram0 will contain the first Symbios 
                   2635:                                 *   format NVRAM content with boot order.
                   2636:                                 */
                   2637:                                nvp = &nvram;
                   2638:                                msg = "with Symbios NVRAM";
                   2639:                                break;
                   2640:                        case SCSI_NCR_TEKRAM_NVRAM:
                   2641:                                msg = "with Tekram NVRAM";
                   2642:                                break;
                   2643:                        }
                   2644:                }
                   2645: #endif
                   2646: #ifdef SCSI_NCR_PQS_PDS_SUPPORT
                   2647:                if (devp->pqs_pds)
                   2648:                        msg = "(NCR PQS/PDS)";
                   2649: #endif
                   2650:                printk(KERN_INFO NAME53C8XX ": 53c%s detected %s\n",
                   2651:                       devp->chip.name, msg);
                   2652:        }
                   2653: 
                   2654:        /*
                   2655:        **    If we have found a SYMBIOS NVRAM, use first the NVRAM boot 
                   2656:        **    sequence as device boot order.
                   2657:        **    check devices in the boot record against devices detected. 
                   2658:        **    attach devices if we find a match. boot table records that 
                   2659:        **    do not match any detected devices will be ignored. 
                   2660:        **    devices that do not match any boot table will not be attached
                   2661:        **    here but will attempt to be attached during the device table 
                   2662:        **    rescan.
                   2663:        */
                   2664: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   2665:        if (!nvp || nvram0.type != SCSI_NCR_SYMBIOS_NVRAM)
                   2666:                goto next;
                   2667:        for (i = 0; i < 4; i++) {
                   2668:                Symbios_host *h = &nvram0.data.Symbios.host[i];
                   2669:                for (j = 0 ; j < count ; j++) {
                   2670:                        devp = &devtbl[j];
                   2671:                        if (h->device_fn != devp->slot.device_fn ||
                   2672:                            h->bus_nr    != devp->slot.bus       ||
                   2673:                            h->device_id != devp->chip.device_id)
                   2674:                                continue;
                   2675:                        if (devp->attach_done)
                   2676:                                continue;
                   2677:                        if (h->flags & SYMBIOS_INIT_SCAN_AT_BOOT) {
                   2678:                                ncr_get_nvram(devp, nvp);
                   2679:                                if (!ncr_attach (tpnt, attach_count, devp))
                   2680:                                        attach_count++;
                   2681:                        }
                   2682: #if 0  /* Restore previous behaviour of ncr53c8xx driver */
                   2683:                        else if (!(driver_setup.use_nvram & 0x80))
                   2684:                                printk(KERN_INFO NAME53C8XX
                   2685:                                       ": 53c%s state OFF thus not attached\n",
                   2686:                                       devp->chip.name);
                   2687: #endif
                   2688:                        else
                   2689:                                continue;
                   2690: 
                   2691:                        devp->attach_done = 1;
                   2692:                        break;
                   2693:                }
                   2694:        }
                   2695: next:
                   2696: #endif
                   2697: 
                   2698:        /* 
                   2699:        **    Rescan device list to make sure all boards attached.
                   2700:        **    Devices without boot records will not be attached yet
                   2701:        **    so try to attach them here.
                   2702:        */
                   2703:        for (i= 0; i < count; i++) {
                   2704:                devp = &devtbl[i];
                   2705:                if (!devp->attach_done) {
                   2706: #ifdef SCSI_NCR_NVRAM_SUPPORT
                   2707:                        ncr_get_nvram(devp, nvp);
                   2708: #endif
                   2709:                        if (!ncr_attach (tpnt, attach_count, devp))
                   2710:                                attach_count++;
                   2711:                }
                   2712:        }
                   2713: 
                   2714:        m_free(devtbl, PAGE_SIZE, "devtbl");
                   2715: 
                   2716:        return attach_count;
                   2717: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.