Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / scsi / ncr53c8xx.c
index 9b91b2e..b28712d 100644 (file)
 **     Low PCI traffic for command handling when on-chip RAM is present.
 **     Aggressive SCSI SCRIPTS optimizations.
 **
+**  2005 by Matthew Wilcox and James Bottomley
+**     PCI-ectomy.  This driver now supports only the 720 chip (see the
+**     NCR_Q720 and zalon drivers for the bus probe logic).
+**
 *******************************************************************************
 */
 
 */
 
 /* Name and version of the driver */
-#define SCSI_NCR_DRIVER_NAME   "ncr53c8xx-3.4.3f"
+#define SCSI_NCR_DRIVER_NAME   "ncr53c8xx-3.4.3g"
 
 #define SCSI_NCR_DEBUG_FLAGS   (0)
 
-/*==========================================================
-**
-**      Include files
-**
-**==========================================================
-*/
-
 #include <linux/blkdev.h>
 #include <linux/delay.h>
 #include <linux/dma-mapping.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
+#include <scsi/scsi_dbg.h>
 #include <scsi/scsi_device.h>
 #include <scsi/scsi_tcq.h>
 #include <scsi/scsi_transport.h>
 
 #include "ncr53c8xx.h"
 
-#define NAME53C                        "ncr53c"
 #define NAME53C8XX             "ncr53c8xx"
 
-#include "sym53c8xx_comm.h"
+/*==========================================================
+**
+**     Debugging tags
+**
+**==========================================================
+*/
+
+#define DEBUG_ALLOC    (0x0001)
+#define DEBUG_PHASE    (0x0002)
+#define DEBUG_QUEUE    (0x0008)
+#define DEBUG_RESULT   (0x0010)
+#define DEBUG_POINTER  (0x0020)
+#define DEBUG_SCRIPT   (0x0040)
+#define DEBUG_TINY     (0x0080)
+#define DEBUG_TIMING   (0x0100)
+#define DEBUG_NEGO     (0x0200)
+#define DEBUG_TAGS     (0x0400)
+#define DEBUG_SCATTER  (0x0800)
+#define DEBUG_IC        (0x1000)
+
+/*
+**    Enable/Disable debug messages.
+**    Can be changed at runtime too.
+*/
+
+#ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
+static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
+       #define DEBUG_FLAGS ncr_debug
+#else
+       #define DEBUG_FLAGS     SCSI_NCR_DEBUG_FLAGS
+#endif
+
+static inline struct list_head *ncr_list_pop(struct list_head *head)
+{
+       if (!list_empty(head)) {
+               struct list_head *elem = head->next;
+
+               list_del(elem);
+               return elem;
+       }
+
+       return NULL;
+}
+
+/*==========================================================
+**
+**     Simple power of two buddy-like allocator.
+**
+**     This simple code is not intended to be fast, but to 
+**     provide power of 2 aligned memory allocations.
+**     Since the SCRIPTS processor only supplies 8 bit 
+**     arithmetic, this allocator allows simple and fast 
+**     address calculations  from the SCRIPTS code.
+**     In addition, cache line alignment is guaranteed for 
+**     power of 2 cache line size.
+**     Enhanced in linux-2.3.44 to provide a memory pool 
+**     per pcidev to support dynamic dma mapping. (I would 
+**     have preferred a real bus astraction, btw).
+**
+**==========================================================
+*/
+
+#define MEMO_SHIFT     4       /* 16 bytes minimum memory chunk */
+#if PAGE_SIZE >= 8192
+#define MEMO_PAGE_ORDER        0       /* 1 PAGE  maximum */
+#else
+#define MEMO_PAGE_ORDER        1       /* 2 PAGES maximum */
+#endif
+#define MEMO_FREE_UNUSED       /* Free unused pages immediately */
+#define MEMO_WARN      1
+#define MEMO_GFP_FLAGS GFP_ATOMIC
+#define MEMO_CLUSTER_SHIFT     (PAGE_SHIFT+MEMO_PAGE_ORDER)
+#define MEMO_CLUSTER_SIZE      (1UL << MEMO_CLUSTER_SHIFT)
+#define MEMO_CLUSTER_MASK      (MEMO_CLUSTER_SIZE-1)
+
+typedef u_long m_addr_t;       /* Enough bits to bit-hack addresses */
+typedef struct device *m_bush_t;       /* Something that addresses DMAable */
+
+typedef struct m_link {                /* Link between free memory chunks */
+       struct m_link *next;
+} m_link_s;
+
+typedef struct m_vtob {                /* Virtual to Bus address translation */
+       struct m_vtob *next;
+       m_addr_t vaddr;
+       m_addr_t baddr;
+} m_vtob_s;
+#define VTOB_HASH_SHIFT                5
+#define VTOB_HASH_SIZE         (1UL << VTOB_HASH_SHIFT)
+#define VTOB_HASH_MASK         (VTOB_HASH_SIZE-1)
+#define VTOB_HASH_CODE(m)      \
+       ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
+
+typedef struct m_pool {                /* Memory pool of a given kind */
+       m_bush_t bush;
+       m_addr_t (*getp)(struct m_pool *);
+       void (*freep)(struct m_pool *, m_addr_t);
+       int nump;
+       m_vtob_s *(vtob[VTOB_HASH_SIZE]);
+       struct m_pool *next;
+       struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
+} m_pool_s;
+
+static void *___m_alloc(m_pool_s *mp, int size)
+{
+       int i = 0;
+       int s = (1 << MEMO_SHIFT);
+       int j;
+       m_addr_t a;
+       m_link_s *h = mp->h;
+
+       if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
+               return NULL;
+
+       while (size > s) {
+               s <<= 1;
+               ++i;
+       }
+
+       j = i;
+       while (!h[j].next) {
+               if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
+                       h[j].next = (m_link_s *)mp->getp(mp);
+                       if (h[j].next)
+                               h[j].next->next = NULL;
+                       break;
+               }
+               ++j;
+               s <<= 1;
+       }
+       a = (m_addr_t) h[j].next;
+       if (a) {
+               h[j].next = h[j].next->next;
+               while (j > i) {
+                       j -= 1;
+                       s >>= 1;
+                       h[j].next = (m_link_s *) (a+s);
+                       h[j].next->next = NULL;
+               }
+       }
+#ifdef DEBUG
+       printk("___m_alloc(%d) = %p\n", size, (void *) a);
+#endif
+       return (void *) a;
+}
+
+static void ___m_free(m_pool_s *mp, void *ptr, int size)
+{
+       int i = 0;
+       int s = (1 << MEMO_SHIFT);
+       m_link_s *q;
+       m_addr_t a, b;
+       m_link_s *h = mp->h;
+
+#ifdef DEBUG
+       printk("___m_free(%p, %d)\n", ptr, size);
+#endif
+
+       if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
+               return;
+
+       while (size > s) {
+               s <<= 1;
+               ++i;
+       }
+
+       a = (m_addr_t) ptr;
+
+       while (1) {
+#ifdef MEMO_FREE_UNUSED
+               if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
+                       mp->freep(mp, a);
+                       break;
+               }
+#endif
+               b = a ^ s;
+               q = &h[i];
+               while (q->next && q->next != (m_link_s *) b) {
+                       q = q->next;
+               }
+               if (!q->next) {
+                       ((m_link_s *) a)->next = h[i].next;
+                       h[i].next = (m_link_s *) a;
+                       break;
+               }
+               q->next = q->next->next;
+               a = a & b;
+               s <<= 1;
+               ++i;
+       }
+}
+
+static DEFINE_SPINLOCK(ncr53c8xx_lock);
+
+static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
+{
+       void *p;
+
+       p = ___m_alloc(mp, size);
+
+       if (DEBUG_FLAGS & DEBUG_ALLOC)
+               printk ("new %-10s[%4d] @%p.\n", name, size, p);
+
+       if (p)
+               memset(p, 0, size);
+       else if (uflags & MEMO_WARN)
+               printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
+
+       return p;
+}
+
+#define __m_calloc(mp, s, n)   __m_calloc2(mp, s, n, MEMO_WARN)
+
+static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
+{
+       if (DEBUG_FLAGS & DEBUG_ALLOC)
+               printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
+
+       ___m_free(mp, ptr, size);
+
+}
+
+/*
+ * With pci bus iommu support, we use a default pool of unmapped memory 
+ * for memory we donnot need to DMA from/to and one pool per pcidev for 
+ * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
+ */
+
+static m_addr_t ___mp0_getp(m_pool_s *mp)
+{
+       m_addr_t m = __get_free_pages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER);
+       if (m)
+               ++mp->nump;
+       return m;
+}
+
+static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
+{
+       free_pages(m, MEMO_PAGE_ORDER);
+       --mp->nump;
+}
+
+static m_pool_s mp0 = {NULL, ___mp0_getp, ___mp0_freep};
+
+/*
+ * DMAable pools.
+ */
+
+/*
+ * With pci bus iommu support, we maintain one pool per pcidev and a 
+ * hashed reverse table for virtual to bus physical address translations.
+ */
+static m_addr_t ___dma_getp(m_pool_s *mp)
+{
+       m_addr_t vp;
+       m_vtob_s *vbp;
+
+       vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
+       if (vbp) {
+               dma_addr_t daddr;
+               vp = (m_addr_t) dma_alloc_coherent(mp->bush,
+                                               PAGE_SIZE<<MEMO_PAGE_ORDER,
+                                               &daddr, GFP_ATOMIC);
+               if (vp) {
+                       int hc = VTOB_HASH_CODE(vp);
+                       vbp->vaddr = vp;
+                       vbp->baddr = daddr;
+                       vbp->next = mp->vtob[hc];
+                       mp->vtob[hc] = vbp;
+                       ++mp->nump;
+                       return vp;
+               }
+       }
+       if (vbp)
+               __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
+       return 0;
+}
+
+static void ___dma_freep(m_pool_s *mp, m_addr_t m)
+{
+       m_vtob_s **vbpp, *vbp;
+       int hc = VTOB_HASH_CODE(m);
+
+       vbpp = &mp->vtob[hc];
+       while (*vbpp && (*vbpp)->vaddr != m)
+               vbpp = &(*vbpp)->next;
+       if (*vbpp) {
+               vbp = *vbpp;
+               *vbpp = (*vbpp)->next;
+               dma_free_coherent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
+                                 (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
+               __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
+               --mp->nump;
+       }
+}
+
+static inline m_pool_s *___get_dma_pool(m_bush_t bush)
+{
+       m_pool_s *mp;
+       for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
+       return mp;
+}
+
+static m_pool_s *___cre_dma_pool(m_bush_t bush)
+{
+       m_pool_s *mp;
+       mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
+       if (mp) {
+               memset(mp, 0, sizeof(*mp));
+               mp->bush = bush;
+               mp->getp = ___dma_getp;
+               mp->freep = ___dma_freep;
+               mp->next = mp0.next;
+               mp0.next = mp;
+       }
+       return mp;
+}
+
+static void ___del_dma_pool(m_pool_s *p)
+{
+       struct m_pool **pp = &mp0.next;
+
+       while (*pp && *pp != p)
+               pp = &(*pp)->next;
+       if (*pp) {
+               *pp = (*pp)->next;
+               __m_free(&mp0, p, sizeof(*p), "MPOOL");
+       }
+}
+
+static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
+{
+       u_long flags;
+       struct m_pool *mp;
+       void *m = NULL;
+
+       spin_lock_irqsave(&ncr53c8xx_lock, flags);
+       mp = ___get_dma_pool(bush);
+       if (!mp)
+               mp = ___cre_dma_pool(bush);
+       if (mp)
+               m = __m_calloc(mp, size, name);
+       if (mp && !mp->nump)
+               ___del_dma_pool(mp);
+       spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
+
+       return m;
+}
+
+static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
+{
+       u_long flags;
+       struct m_pool *mp;
+
+       spin_lock_irqsave(&ncr53c8xx_lock, flags);
+       mp = ___get_dma_pool(bush);
+       if (mp)
+               __m_free(mp, m, size, name);
+       if (mp && !mp->nump)
+               ___del_dma_pool(mp);
+       spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
+}
+
+static m_addr_t __vtobus(m_bush_t bush, void *m)
+{
+       u_long flags;
+       m_pool_s *mp;
+       int hc = VTOB_HASH_CODE(m);
+       m_vtob_s *vp = NULL;
+       m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
+
+       spin_lock_irqsave(&ncr53c8xx_lock, flags);
+       mp = ___get_dma_pool(bush);
+       if (mp) {
+               vp = mp->vtob[hc];
+               while (vp && (m_addr_t) vp->vaddr != a)
+                       vp = vp->next;
+       }
+       spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
+       return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
+}
+
+#define _m_calloc_dma(np, s, n)                __m_calloc_dma(np->dev, s, n)
+#define _m_free_dma(np, p, s, n)       __m_free_dma(np->dev, p, s, n)
+#define m_calloc_dma(s, n)             _m_calloc_dma(np, s, n)
+#define m_free_dma(p, s, n)            _m_free_dma(np, p, s, n)
+#define _vtobus(np, p)                 __vtobus(np->dev, p)
+#define vtobus(p)                      _vtobus(np, p)
+
+/*
+ *  Deal with DMA mapping/unmapping.
+ */
+
+/* To keep track of the dma mapping (sg/single) that has been set */
+#define __data_mapped  SCp.phase
+#define __data_mapping SCp.have_data_in
+
+static void __unmap_scsi_data(struct device *dev, struct scsi_cmnd *cmd)
+{
+       switch(cmd->__data_mapped) {
+       case 2:
+               dma_unmap_sg(dev, cmd->request_buffer, cmd->use_sg,
+                               cmd->sc_data_direction);
+               break;
+       case 1:
+               dma_unmap_single(dev, cmd->__data_mapping,
+                                cmd->request_bufflen,
+                                cmd->sc_data_direction);
+               break;
+       }
+       cmd->__data_mapped = 0;
+}
+
+static u_long __map_scsi_single_data(struct device *dev, struct scsi_cmnd *cmd)
+{
+       dma_addr_t mapping;
+
+       if (cmd->request_bufflen == 0)
+               return 0;
+
+       mapping = dma_map_single(dev, cmd->request_buffer,
+                                cmd->request_bufflen,
+                                cmd->sc_data_direction);
+       cmd->__data_mapped = 1;
+       cmd->__data_mapping = mapping;
+
+       return mapping;
+}
+
+static int __map_scsi_sg_data(struct device *dev, struct scsi_cmnd *cmd)
+{
+       int use_sg;
+
+       if (cmd->use_sg == 0)
+               return 0;
+
+       use_sg = dma_map_sg(dev, cmd->request_buffer, cmd->use_sg,
+                       cmd->sc_data_direction);
+       cmd->__data_mapped = 2;
+       cmd->__data_mapping = use_sg;
+
+       return use_sg;
+}
+
+#define unmap_scsi_data(np, cmd)       __unmap_scsi_data(np->dev, cmd)
+#define map_scsi_single_data(np, cmd)  __map_scsi_single_data(np->dev, cmd)
+#define map_scsi_sg_data(np, cmd)      __map_scsi_sg_data(np->dev, cmd)
+
+/*==========================================================
+**
+**     Driver setup.
+**
+**     This structure is initialized from linux config 
+**     options. It can be overridden at boot-up by the boot 
+**     command line.
+**
+**==========================================================
+*/
+static struct ncr_driver_setup
+       driver_setup                    = SCSI_NCR_DRIVER_SETUP;
+
+#ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
+static struct ncr_driver_setup
+       driver_safe_setup __initdata    = SCSI_NCR_DRIVER_SAFE_SETUP;
+#endif
+
+#define initverbose (driver_setup.verbose)
+#define bootverbose (np->verbose)
+
+
+/*===================================================================
+**
+**     Driver setup from the boot command line
+**
+**===================================================================
+*/
+
+#ifdef MODULE
+#define        ARG_SEP ' '
+#else
+#define        ARG_SEP ','
+#endif
+
+#define OPT_TAGS               1
+#define OPT_MASTER_PARITY      2
+#define OPT_SCSI_PARITY                3
+#define OPT_DISCONNECTION      4
+#define OPT_SPECIAL_FEATURES   5
+#define OPT_UNUSED_1           6
+#define OPT_FORCE_SYNC_NEGO    7
+#define OPT_REVERSE_PROBE      8
+#define OPT_DEFAULT_SYNC       9
+#define OPT_VERBOSE            10
+#define OPT_DEBUG              11
+#define OPT_BURST_MAX          12
+#define OPT_LED_PIN            13
+#define OPT_MAX_WIDE           14
+#define OPT_SETTLE_DELAY       15
+#define OPT_DIFF_SUPPORT       16
+#define OPT_IRQM               17
+#define OPT_PCI_FIX_UP         18
+#define OPT_BUS_CHECK          19
+#define OPT_OPTIMIZE           20
+#define OPT_RECOVERY           21
+#define OPT_SAFE_SETUP         22
+#define OPT_USE_NVRAM          23
+#define OPT_EXCLUDE            24
+#define OPT_HOST_ID            25
+
+#ifdef SCSI_NCR_IARB_SUPPORT
+#define OPT_IARB               26
+#endif
+
+static char setup_token[] __initdata = 
+       "tags:"   "mpar:"
+       "spar:"   "disc:"
+       "specf:"  "ultra:"
+       "fsn:"    "revprob:"
+       "sync:"   "verb:"
+       "debug:"  "burst:"
+       "led:"    "wide:"
+       "settle:" "diff:"
+       "irqm:"   "pcifix:"
+       "buschk:" "optim:"
+       "recovery:"
+       "safe:"   "nvram:"
+       "excl:"   "hostid:"
+#ifdef SCSI_NCR_IARB_SUPPORT
+       "iarb:"
+#endif
+       ;       /* DONNOT REMOVE THIS ';' */
+
+#ifdef MODULE
+#define        ARG_SEP ' '
+#else
+#define        ARG_SEP ','
+#endif
+
+static int __init get_setup_token(char *p)
+{
+       char *cur = setup_token;
+       char *pc;
+       int i = 0;
+
+       while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
+               ++pc;
+               ++i;
+               if (!strncmp(p, cur, pc - cur))
+                       return i;
+               cur = pc;
+       }
+       return 0;
+}
+
+
+static int __init sym53c8xx__setup(char *str)
+{
+#ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
+       char *cur = str;
+       char *pc, *pv;
+       int i, val, c;
+       int xi = 0;
+
+       while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
+               char *pe;
+
+               val = 0;
+               pv = pc;
+               c = *++pv;
+
+               if      (c == 'n')
+                       val = 0;
+               else if (c == 'y')
+                       val = 1;
+               else
+                       val = (int) simple_strtoul(pv, &pe, 0);
+
+               switch (get_setup_token(cur)) {
+               case OPT_TAGS:
+                       driver_setup.default_tags = val;
+                       if (pe && *pe == '/') {
+                               i = 0;
+                               while (*pe && *pe != ARG_SEP && 
+                                       i < sizeof(driver_setup.tag_ctrl)-1) {
+                                       driver_setup.tag_ctrl[i++] = *pe++;
+                               }
+                               driver_setup.tag_ctrl[i] = '\0';
+                       }
+                       break;
+               case OPT_MASTER_PARITY:
+                       driver_setup.master_parity = val;
+                       break;
+               case OPT_SCSI_PARITY:
+                       driver_setup.scsi_parity = val;
+                       break;
+               case OPT_DISCONNECTION:
+                       driver_setup.disconnection = val;
+                       break;
+               case OPT_SPECIAL_FEATURES:
+                       driver_setup.special_features = val;
+                       break;
+               case OPT_FORCE_SYNC_NEGO:
+                       driver_setup.force_sync_nego = val;
+                       break;
+               case OPT_REVERSE_PROBE:
+                       driver_setup.reverse_probe = val;
+                       break;
+               case OPT_DEFAULT_SYNC:
+                       driver_setup.default_sync = val;
+                       break;
+               case OPT_VERBOSE:
+                       driver_setup.verbose = val;
+                       break;
+               case OPT_DEBUG:
+                       driver_setup.debug = val;
+                       break;
+               case OPT_BURST_MAX:
+                       driver_setup.burst_max = val;
+                       break;
+               case OPT_LED_PIN:
+                       driver_setup.led_pin = val;
+                       break;
+               case OPT_MAX_WIDE:
+                       driver_setup.max_wide = val? 1:0;
+                       break;
+               case OPT_SETTLE_DELAY:
+                       driver_setup.settle_delay = val;
+                       break;
+               case OPT_DIFF_SUPPORT:
+                       driver_setup.diff_support = val;
+                       break;
+               case OPT_IRQM:
+                       driver_setup.irqm = val;
+                       break;
+               case OPT_PCI_FIX_UP:
+                       driver_setup.pci_fix_up = val;
+                       break;
+               case OPT_BUS_CHECK:
+                       driver_setup.bus_check = val;
+                       break;
+               case OPT_OPTIMIZE:
+                       driver_setup.optimize = val;
+                       break;
+               case OPT_RECOVERY:
+                       driver_setup.recovery = val;
+                       break;
+               case OPT_USE_NVRAM:
+                       driver_setup.use_nvram = val;
+                       break;
+               case OPT_SAFE_SETUP:
+                       memcpy(&driver_setup, &driver_safe_setup,
+                               sizeof(driver_setup));
+                       break;
+               case OPT_EXCLUDE:
+                       if (xi < SCSI_NCR_MAX_EXCLUDES)
+                               driver_setup.excludes[xi++] = val;
+                       break;
+               case OPT_HOST_ID:
+                       driver_setup.host_id = val;
+                       break;
+#ifdef SCSI_NCR_IARB_SUPPORT
+               case OPT_IARB:
+                       driver_setup.iarb = val;
+                       break;
+#endif
+               default:
+                       printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
+                       break;
+               }
+
+               if ((cur = strchr(cur, ARG_SEP)) != NULL)
+                       ++cur;
+       }
+#endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
+       return 1;
+}
+
+/*===================================================================
+**
+**     Get device queue depth from boot command line.
+**
+**===================================================================
+*/
+#define DEF_DEPTH      (driver_setup.default_tags)
+#define ALL_TARGETS    -2
+#define NO_TARGET      -1
+#define ALL_LUNS       -2
+#define NO_LUN         -1
+
+static int device_queue_depth(int unit, int target, int lun)
+{
+       int c, h, t, u, v;
+       char *p = driver_setup.tag_ctrl;
+       char *ep;
+
+       h = -1;
+       t = NO_TARGET;
+       u = NO_LUN;
+       while ((c = *p++) != 0) {
+               v = simple_strtoul(p, &ep, 0);
+               switch(c) {
+               case '/':
+                       ++h;
+                       t = ALL_TARGETS;
+                       u = ALL_LUNS;
+                       break;
+               case 't':
+                       if (t != target)
+                               t = (target == v) ? v : NO_TARGET;
+                       u = ALL_LUNS;
+                       break;
+               case 'u':
+                       if (u != lun)
+                               u = (lun == v) ? v : NO_LUN;
+                       break;
+               case 'q':
+                       if (h == unit &&
+                               (t == ALL_TARGETS || t == target) &&
+                               (u == ALL_LUNS    || u == lun))
+                               return v;
+                       break;
+               case '-':
+                       t = ALL_TARGETS;
+                       u = ALL_LUNS;
+                       break;
+               default:
+                       break;
+               }
+               p = ep;
+       }
+       return DEF_DEPTH;
+}
 
 
 /*==========================================================
@@ -1219,7 +1947,7 @@ static    struct lcb *    ncr_alloc_lcb   (struct ncb *np, u_char tn, u_char ln);
 static struct lcb *    ncr_setup_lcb   (struct ncb *np, struct scsi_device *sdev);
 static void    ncr_getclock    (struct ncb *np, int mult);
 static void    ncr_selectclock (struct ncb *np, u_char scntl3);
-static struct ccb *ncr_get_ccb (struct ncb *np, u_char tn, u_char ln);
+static struct ccb *ncr_get_ccb (struct ncb *np, struct scsi_cmnd *cmd);
 static void    ncr_chip_reset  (struct ncb *np, int delay);
 static void    ncr_init        (struct ncb *np, int reset, char * msg, u_long code);
 static int     ncr_int_sbmc    (struct ncb *np);
@@ -1238,8 +1966,6 @@ static    void    ncr_getsync     (struct ncb *np, u_char sfac, u_char *fakp, u_char *scnt
 static void    ncr_setsync     (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer);
 static void    ncr_setup_tags  (struct ncb *np, struct scsi_device *sdev);
 static void    ncr_setwide     (struct ncb *np, struct ccb *cp, u_char wide, u_char ack);
-static int     ncr_show_msg    (u_char * msg);
-static  void    ncr_print_msg   (struct ccb *cp, char *label, u_char *msg);
 static int     ncr_snooptest   (struct ncb *np);
 static void    ncr_timeout     (struct ncb *np);
 static  void    ncr_wakeup      (struct ncb *np, u_long code);
@@ -1381,7 +2107,7 @@ static    struct script script0 __initdata = {
        */
 
        /*
-       **      The M_REJECT problem seems to be due to a selection 
+       **      The MESSAGE_REJECT problem seems to be due to a selection 
        **      timing problem.
        **      Wait immediately for the selection to complete. 
        **      (2.5x behaves so)
@@ -1432,7 +2158,7 @@ static    struct script script0 __initdata = {
        /*
        **      Selection complete.
        **      Send the IDENTIFY and SIMPLE_TAG messages
-       **      (and the M_X_SYNC_REQ message)
+       **      (and the EXTENDED_SDTR message)
        */
        SCR_MOVE_TBL ^ SCR_MSG_OUT,
                offsetof (struct dsb, smsg),
@@ -1461,7 +2187,7 @@ static    struct script script0 __initdata = {
        /*
        **      Initialize the msgout buffer with a NOOP message.
        */
-       SCR_LOAD_REG (scratcha, M_NOOP),
+       SCR_LOAD_REG (scratcha, NOP),
                0,
        SCR_COPY (1),
                RADDR (scratcha),
@@ -1613,21 +2339,21 @@ static  struct script script0 __initdata = {
        /*
        **      Handle this message.
        */
-       SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
+       SCR_JUMP ^ IFTRUE (DATA (COMMAND_COMPLETE)),
                PADDR (complete),
-       SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
+       SCR_JUMP ^ IFTRUE (DATA (DISCONNECT)),
                PADDR (disconnect),
-       SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
+       SCR_JUMP ^ IFTRUE (DATA (SAVE_POINTERS)),
                PADDR (save_dp),
-       SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
+       SCR_JUMP ^ IFTRUE (DATA (RESTORE_POINTERS)),
                PADDR (restore_dp),
-       SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
+       SCR_JUMP ^ IFTRUE (DATA (EXTENDED_MESSAGE)),
                PADDRH (msg_extended),
-       SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
+       SCR_JUMP ^ IFTRUE (DATA (NOP)),
                PADDR (clrack),
-       SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
+       SCR_JUMP ^ IFTRUE (DATA (MESSAGE_REJECT)),
                PADDRH (msg_reject),
-       SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
+       SCR_JUMP ^ IFTRUE (DATA (IGNORE_WIDE_RESIDUE)),
                PADDRH (msg_ign_residue),
        /*
        **      Rest of the messages left as
@@ -1642,7 +2368,7 @@ static    struct script script0 __initdata = {
        */
        SCR_INT,
                SIR_REJECT_SENT,
-       SCR_LOAD_REG (scratcha, M_REJECT),
+       SCR_LOAD_REG (scratcha, MESSAGE_REJECT),
                0,
 }/*-------------------------< SETMSG >----------------------*/,{
        SCR_COPY (1),
@@ -1834,7 +2560,7 @@ static    struct script script0 __initdata = {
        /*
        **      If it was no ABORT message ...
        */
-       SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
+       SCR_JUMP ^ IFTRUE (DATA (ABORT_TASK_SET)),
                PADDRH (msg_out_abort),
        /*
        **      ... wait for the next phase
@@ -1846,7 +2572,7 @@ static    struct script script0 __initdata = {
        /*
        **      ... else clear the message ...
        */
-       SCR_LOAD_REG (scratcha, M_NOOP),
+       SCR_LOAD_REG (scratcha, NOP),
                0,
        SCR_COPY (4),
                RADDR (scratcha),
@@ -2305,7 +3031,7 @@ static    struct scripth scripth0 __initdata = {
        */
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
                NADDR (msgin[2]),
-       SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
+       SCR_JUMP ^ IFTRUE (DATA (EXTENDED_WDTR)),
                PADDRH (msg_wdtr),
        /*
        **      unknown extended message
@@ -2339,7 +3065,7 @@ static    struct scripth scripth0 __initdata = {
 
 }/*-------------------------< SEND_WDTR >----------------*/,{
        /*
-       **      Send the M_X_WIDE_REQ
+       **      Send the EXTENDED_WDTR
        */
        SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
                NADDR (msgout),
@@ -2359,7 +3085,7 @@ static    struct scripth scripth0 __initdata = {
        */
        SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
                NADDR (msgin[2]),
-       SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
+       SCR_JUMP ^ IFTRUE (DATA (EXTENDED_SDTR)),
                PADDRH (msg_sdtr),
        /*
        **      unknown extended message
@@ -2394,7 +3120,7 @@ static    struct scripth scripth0 __initdata = {
 
 }/*-------------------------< SEND_SDTR >-------------*/,{
        /*
-       **      Send the M_X_SYNC_REQ
+       **      Send the EXTENDED_SDTR
        */
        SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
                NADDR (msgout),
@@ -2472,10 +3198,10 @@ static  struct scripth scripth0 __initdata = {
 
 }/*-------------------------< RESET >----------------------*/,{
        /*
-       **      Send a M_RESET message if bad IDENTIFY 
+       **      Send a TARGET_RESET message if bad IDENTIFY 
        **      received on reselection.
        */
-       SCR_LOAD_REG (scratcha, M_ABORT_TAG),
+       SCR_LOAD_REG (scratcha, ABORT_TASK),
                0,
        SCR_JUMP,
                PADDRH (abort_resel),
@@ -2483,7 +3209,7 @@ static    struct scripth scripth0 __initdata = {
        /*
        **      Abort a wrong tag received on reselection.
        */
-       SCR_LOAD_REG (scratcha, M_ABORT_TAG),
+       SCR_LOAD_REG (scratcha, ABORT_TASK),
                0,
        SCR_JUMP,
                PADDRH (abort_resel),
@@ -2491,7 +3217,7 @@ static    struct scripth scripth0 __initdata = {
        /*
        **      Abort a reselection when no active CCB.
        */
-       SCR_LOAD_REG (scratcha, M_ABORT),
+       SCR_LOAD_REG (scratcha, ABORT_TASK_SET),
                0,
 }/*-------------------------< ABORT_RESEL >----------------*/,{
        SCR_COPY (1),
@@ -2603,7 +3329,7 @@ static    struct scripth scripth0 __initdata = {
        **      Read the message, since we got it directly 
        **      from the SCSI BUS data lines.
        **      Signal problem to C code for logging the event.
-       **      Send a M_ABORT to clear all pending tasks.
+       **      Send an ABORT_TASK_SET to clear all pending tasks.
        */
        SCR_INT,
                SIR_RESEL_BAD_LUN,
@@ -2615,7 +3341,7 @@ static    struct scripth scripth0 __initdata = {
        /*
        **      We donnot have a task for that I_T_L.
        **      Signal problem to C code for logging the event.
-       **      Send a M_ABORT message.
+       **      Send an ABORT_TASK_SET message.
        */
        SCR_INT,
                SIR_RESEL_BAD_I_T_L,
@@ -2625,7 +3351,7 @@ static    struct scripth scripth0 __initdata = {
        /*
        **      We donnot have a task that matches the tag.
        **      Signal problem to C code for logging the event.
-       **      Send a M_ABORTTAG message.
+       **      Send an ABORT_TASK message.
        */
        SCR_INT,
                SIR_RESEL_BAD_I_T_L_Q,
@@ -2636,7 +3362,7 @@ static    struct scripth scripth0 __initdata = {
        **      We donnot know the target that reselected us.
        **      Grab the first message if any (IDENTIFY).
        **      Signal problem to C code for logging the event.
-       **      M_RESET message.
+       **      TARGET_RESET message.
        */
        SCR_INT,
                SIR_RESEL_BAD_TARGET,
@@ -2746,7 +3472,7 @@ void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
        for (i=0; i<MAX_START; i++) {
                *p++ =SCR_CALL;
                *p++ =PADDR (idle);
-       };
+       }
 
        BUG_ON((u_long)p != (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
 
@@ -2771,7 +3497,7 @@ void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
                *p++ =PADDR (dispatch);
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
                *p++ =offsetof (struct dsb, data[i]);
-       };
+       }
 
        BUG_ON((u_long)p != (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
 
@@ -2781,7 +3507,7 @@ void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
                *p++ =PADDR (dispatch);
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
                *p++ =offsetof (struct dsb, data[i]);
-       };
+       }
 
        BUG_ON((u_long)p != (u_long)&scr->data_in + sizeof (scr->data_in));
 
@@ -2791,7 +3517,7 @@ void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
                *p++ =PADDR (dispatch);
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
                *p++ =offsetof (struct dsb, data[i]);
-       };
+       }
 
        BUG_ON((u_long)p != (u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
 
@@ -2801,7 +3527,7 @@ void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
                *p++ =PADDR (dispatch);
                *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
                *p++ =offsetof (struct dsb, data[i]);
-       };
+       }
 
        BUG_ON((u_long) p != (u_long)&scr->data_out + sizeof (scr->data_out));
 }
@@ -2842,7 +3568,7 @@ ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
                        printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
                                ncr_name(np), (int) (src-start-1));
                        mdelay(1000);
-               };
+               }
 
                if (DEBUG_FLAGS & DEBUG_SCRIPT)
                        printk (KERN_DEBUG "%p:  <%x>\n",
@@ -2911,7 +3637,7 @@ ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
                default:
                        relocs = 0;
                        break;
-               };
+               }
 
                if (relocs) {
                        while (relocs--) {
@@ -2958,7 +3684,7 @@ ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
                } else
                        *dst++ = cpu_to_scr(*src++);
 
-       };
+       }
 }
 
 /*
@@ -2969,25 +3695,14 @@ struct host_data {
      struct ncb *ncb;
 };
 
-/*
-**     Print something which allows to retrieve the controller type, unit,
-**     target, lun concerned by a kernel message.
-*/
+#define PRINT_ADDR(cmd, arg...) dev_info(&cmd->device->sdev_gendev , ## arg)
 
-static void PRINT_TARGET(struct ncb *np, int target)
+static void ncr_print_msg(struct ccb *cp, char *label, u_char *msg)
 {
-       printk(KERN_INFO "%s-<%d,*>: ", ncr_name(np), target);
-}
+       PRINT_ADDR(cp->cmd, "%s: ", label);
 
-static void PRINT_LUN(struct ncb *np, int target, int lun)
-{
-       printk(KERN_INFO "%s-<%d,%d>: ", ncr_name(np), target, lun);
-}
-
-static void PRINT_ADDR(struct scsi_cmnd *cmd)
-{
-       struct host_data *host_data = (struct host_data *) cmd->device->host->hostdata;
-       PRINT_LUN(host_data->ncb, cmd->device->id, cmd->device->lun);
+       spi_print_msg(msg);
+       printk("\n");
 }
 
 /*==========================================================
@@ -3280,6 +3995,7 @@ static void __init ncr_prepare_setting(struct ncb *np)
                tp->usrsync = driver_setup.default_sync;
                tp->usrwide = driver_setup.max_wide;
                tp->usrtags = MAX_TAGS;
+               tp->period = 0xffff;
                if (!driver_setup.disconnection)
                        np->target[i].usrflag = UF_NODISC;
        }
@@ -3369,50 +4085,33 @@ static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
        int nego = 0;
        struct scsi_target *starget = tp->starget;
 
-       if (likely(starget)) {
-
-               /*
-               **      negotiate wide transfers ?
-               */
-
-               if (!tp->widedone) {
-                       if (spi_support_wide(starget)) {
-                               nego = NS_WIDE;
-                       } else
-                               tp->widedone=1;
-
-               };
-
-               /*
-               **      negotiate synchronous transfers?
-               */
+       /* negotiate wide transfers ?  */
+       if (!tp->widedone) {
+               if (spi_support_wide(starget)) {
+                       nego = NS_WIDE;
+               } else
+                       tp->widedone=1;
+       }
 
-               if (!nego && !tp->period) {
-                       if (spi_support_sync(starget)) {
-                               nego = NS_SYNC;
-                       } else {
-                               tp->period  =0xffff;
-                               PRINT_TARGET(np, cp->target);
-                               printk ("target did not report SYNC.\n");
-                       };
-               };
-       };
+       /* negotiate synchronous transfers?  */
+       if (!nego && !tp->period) {
+               if (spi_support_sync(starget)) {
+                       nego = NS_SYNC;
+               } else {
+                       tp->period  =0xffff;
+                       dev_info(&starget->dev, "target did not report SYNC.\n");
+               }
+       }
 
        switch (nego) {
        case NS_SYNC:
-               msgptr[msglen++] = M_EXTENDED;
-               msgptr[msglen++] = 3;
-               msgptr[msglen++] = M_X_SYNC_REQ;
-               msgptr[msglen++] = tp->maxoffs ? tp->minsync : 0;
-               msgptr[msglen++] = tp->maxoffs;
+               msglen += spi_populate_sync_msg(msgptr + msglen,
+                               tp->maxoffs ? tp->minsync : 0, tp->maxoffs);
                break;
        case NS_WIDE:
-               msgptr[msglen++] = M_EXTENDED;
-               msgptr[msglen++] = 2;
-               msgptr[msglen++] = M_X_WIDE_REQ;
-               msgptr[msglen++] = tp->usrwide;
+               msglen += spi_populate_width_msg(msgptr + msglen, tp->usrwide);
                break;
-       };
+       }
 
        cp->nego_status = nego;
 
@@ -3421,8 +4120,8 @@ static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
                if (DEBUG_FLAGS & DEBUG_NEGO) {
                        ncr_print_msg(cp, nego == NS_WIDE ?
                                          "wide msgout":"sync_msgout", msgptr);
-               };
-       };
+               }
+       }
 
        return msglen;
 }
@@ -3440,9 +4139,9 @@ static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
 */
 static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
 {
-/*     struct scsi_device        *device    = cmd->device; */
-       struct tcb *tp                      = &np->target[cmd->device->id];
-       struct lcb *lp                = tp->lp[cmd->device->lun];
+       struct scsi_device *sdev = cmd->device;
+       struct tcb *tp = &np->target[sdev->id];
+       struct lcb *lp = tp->lp[sdev->lun];
        struct ccb *cp;
 
        int     segments;
@@ -3457,9 +4156,9 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        **
        **---------------------------------------------
        */
-       if ((cmd->device->id == np->myaddr        ) ||
-               (cmd->device->id >= MAX_TARGET) ||
-               (cmd->device->lun    >= MAX_LUN   )) {
+       if ((sdev->id == np->myaddr       ) ||
+               (sdev->id >= MAX_TARGET) ||
+               (sdev->lun    >= MAX_LUN   )) {
                return(DID_BAD_TARGET);
        }
 
@@ -3479,8 +4178,7 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        }
 
        if (DEBUG_FLAGS & DEBUG_TINY) {
-               PRINT_ADDR(cmd);
-               printk ("CMD=%x ", cmd->cmnd[0]);
+               PRINT_ADDR(cmd, "CMD=%x ", cmd->cmnd[0]);
        }
 
        /*---------------------------------------------------
@@ -3494,12 +4192,12 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        **----------------------------------------------------
        */
        if (np->settle_time && cmd->timeout_per_command >= HZ) {
-               u_long tlimit = ktime_get(cmd->timeout_per_command - HZ);
-               if (ktime_dif(np->settle_time, tlimit) > 0)
+               u_long tlimit = jiffies + cmd->timeout_per_command - HZ;
+               if (time_after(np->settle_time, tlimit))
                        np->settle_time = tlimit;
        }
 
-       if (np->settle_time || !(cp=ncr_get_ccb (np, cmd->device->id, cmd->device->lun))) {
+       if (np->settle_time || !(cp=ncr_get_ccb (np, cmd))) {
                insert_into_waiting_list(np, cmd);
                return(DID_OK);
        }
@@ -3512,7 +4210,7 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        **----------------------------------------------------
        */
 
-       idmsg = M_IDENTIFY | cmd->device->lun;
+       idmsg = IDENTIFY(0, sdev->lun);
 
        if (cp ->tag != NO_TAG ||
                (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
@@ -3529,15 +4227,15 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
                **      Force ordered tag if necessary to avoid timeouts 
                **      and to preserve interactivity.
                */
-               if (lp && ktime_exp(lp->tags_stime)) {
+               if (lp && time_after(jiffies, lp->tags_stime)) {
                        if (lp->tags_smap) {
-                               order = M_ORDERED_TAG;
+                               order = ORDERED_QUEUE_TAG;
                                if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){ 
-                                       PRINT_ADDR(cmd);
-                                       printk("ordered tag forced.\n");
+                                       PRINT_ADDR(cmd,
+                                               "ordered tag forced.\n");
                                }
                        }
-                       lp->tags_stime = ktime_get(3*HZ);
+                       lp->tags_stime = jiffies + 3*HZ;
                        lp->tags_smap = lp->tags_umap;
                }
 
@@ -3549,10 +4247,10 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
                        case 0x08:  /* READ_SMALL (6) */
                        case 0x28:  /* READ_BIG  (10) */
                        case 0xa8:  /* READ_HUGE (12) */
-                               order = M_SIMPLE_TAG;
+                               order = SIMPLE_QUEUE_TAG;
                                break;
                        default:
-                               order = M_ORDERED_TAG;
+                               order = ORDERED_QUEUE_TAG;
                        }
                }
                msgptr[msglen++] = order;
@@ -3682,7 +4380,7 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        /*
        **      select
        */
-       cp->phys.select.sel_id          = cmd->device->id;
+       cp->phys.select.sel_id          = sdev_id(sdev);
        cp->phys.select.sel_scntl3      = tp->wval;
        cp->phys.select.sel_sxfer       = tp->sval;
        /*
@@ -3719,9 +4417,7 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        **----------------------------------------------------
        */
 
-       /*
-       **      activate this job.
-       */
+       /* activate this job.  */
        cp->magic               = CCB_MAGIC;
 
        /*
@@ -3734,11 +4430,9 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
        else
                ncr_put_start_queue(np, cp);
 
-       /*
-       **      Command is successfully queued.
-       */
+       /* Command is successfully queued.  */
 
-       return(DID_OK);
+       return DID_OK;
 }
 
 
@@ -3809,7 +4503,7 @@ static int ncr_reset_scsi_bus(struct ncb *np, int enab_int, int settle_delay)
        u32 term;
        int retv = 0;
 
-       np->settle_time = ktime_get(settle_delay * HZ);
+       np->settle_time = jiffies + settle_delay * HZ;
 
        if (bootverbose > 1)
                printk("%s: resetting, "
@@ -4203,8 +4897,7 @@ void ncr_complete (struct ncb *np, struct ccb *cp)
        */
 
        if (cp->parity_status > 1) {
-               PRINT_ADDR(cmd);
-               printk ("%d parity error(s).\n",cp->parity_status);
+               PRINT_ADDR(cmd, "%d parity error(s).\n",cp->parity_status);
        }
 
        /*
@@ -4212,16 +4905,16 @@ void ncr_complete (struct ncb *np, struct ccb *cp)
        */
 
        if (cp->xerr_status != XE_OK) {
-               PRINT_ADDR(cmd);
                switch (cp->xerr_status) {
                case XE_EXTRA_DATA:
-                       printk ("extraneous data discarded.\n");
+                       PRINT_ADDR(cmd, "extraneous data discarded.\n");
                        break;
                case XE_BAD_PHASE:
-                       printk ("invalid scsi phase (4/5).\n");
+                       PRINT_ADDR(cmd, "invalid scsi phase (4/5).\n");
                        break;
                default:
-                       printk ("extended error %d.\n", cp->xerr_status);
+                       PRINT_ADDR(cmd, "extended error %d.\n",
+                                       cp->xerr_status);
                        break;
                }
                if (cp->host_status==HS_COMPLETE)
@@ -4233,9 +4926,9 @@ void ncr_complete (struct ncb *np, struct ccb *cp)
        */
        if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
                if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
-                       PRINT_ADDR(cmd);
-                       printk ("ERROR: cmd=%x host_status=%x scsi_status=%x\n",
-                               cmd->cmnd[0], cp->host_status, cp->scsi_status);
+                       PRINT_ADDR(cmd, "ERROR: cmd=%x host_status=%x "
+                                       "scsi_status=%x\n", cmd->cmnd[0],
+                                       cp->host_status, cp->scsi_status);
                }
        }
 
@@ -4296,8 +4989,7 @@ void ncr_complete (struct ncb *np, struct ccb *cp)
                if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
                        u_char * p = (u_char*) & cmd->sense_buffer;
                        int i;
-                       PRINT_ADDR(cmd);
-                       printk ("sense data:");
+                       PRINT_ADDR(cmd, "sense data:");
                        for (i=0; i<14; i++) printk (" %x", *p++);
                        printk (".\n");
                }
@@ -4344,8 +5036,7 @@ void ncr_complete (struct ncb *np, struct ccb *cp)
                /*
                **  Other protocol messes
                */
-               PRINT_ADDR(cmd);
-               printk ("COMMAND FAILED (%x %x) @%p.\n",
+               PRINT_ADDR(cmd, "COMMAND FAILED (%x %x) @%p.\n",
                        cp->host_status, cp->scsi_status, cp);
 
                cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
@@ -4358,8 +5049,7 @@ void ncr_complete (struct ncb *np, struct ccb *cp)
        if (tp->usrflag & UF_TRACE) {
                u_char * p;
                int i;
-               PRINT_ADDR(cmd);
-               printk (" CMD:");
+               PRINT_ADDR(cmd, " CMD:");
                p = (u_char*) &cmd->cmnd[0];
                for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
 
@@ -4428,8 +5118,7 @@ static void ncr_ccb_skipped(struct ncb *np, struct ccb *cp)
                cp->host_status &= ~HS_SKIPMASK;
                cp->start.schedule.l_paddr = 
                        cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
-               list_del(&cp->link_ccbq);
-               list_add_tail(&cp->link_ccbq, &lp->skip_ccbq);
+               list_move_tail(&cp->link_ccbq, &lp->skip_ccbq);
                if (cp->queued) {
                        --lp->queuedccbs;
                }
@@ -4667,12 +5356,11 @@ void ncr_init (struct ncb *np, int reset, char * msg, u_long code)
                        }
                        else
                                tp->usrsync = 255;
-               };
+               }
 
                if (tp->usrwide > np->maxwide)
                        tp->usrwide = np->maxwide;
 
-               ncr_negotiate (np, tp);
        }
 
        /*
@@ -4842,14 +5530,14 @@ static void ncr_set_sync_wide_status (struct ncb *np, u_char target)
        */
        for (cp = np->ccb; cp; cp = cp->link_ccb) {
                if (!cp->cmd) continue;
-               if (cp->cmd->device->id != target) continue;
+               if (scmd_id(cp->cmd) != target) continue;
 #if 0
                cp->sync_status = tp->sval;
                cp->wide_status = tp->wval;
 #endif
                cp->phys.select.sel_scntl3 = tp->wval;
                cp->phys.select.sel_sxfer  = tp->sval;
-       };
+       }
 }
 
 /*==========================================================
@@ -4866,7 +5554,7 @@ static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char s
        u_char target = INB (nc_sdid) & 0x0f;
        u_char idiv;
 
-       BUG_ON(target != (cmd->device->id & 0xf));
+       BUG_ON(target != (scmd_id(cmd) & 0xf));
 
        tp = &np->target[target];
 
@@ -4885,40 +5573,19 @@ static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char s
        else
                tp->period = 0xffff;
 
-       /*
-       **       Stop there if sync parameters are unchanged
-       */
-       if (tp->sval == sxfer && tp->wval == scntl3) return;
+       /* Stop there if sync parameters are unchanged */
+       if (tp->sval == sxfer && tp->wval == scntl3)
+               return;
        tp->sval = sxfer;
        tp->wval = scntl3;
 
-       /*
-       **      Bells and whistles   ;-)
-       */
-       PRINT_TARGET(np, target);
        if (sxfer & 0x01f) {
-               unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
-               unsigned mb10 = (f10 + tp->period/2) / tp->period;
-               char *scsi;
-
-               /*
-               **  Disable extended Sreq/Sack filtering
-               */
-               if (tp->period <= 2000) OUTOFFB (nc_stest2, EXT);
-
-               /*
-               **      Bells and whistles   ;-)
-               */
-               if      (tp->period < 500)      scsi = "FAST-40";
-               else if (tp->period < 1000)     scsi = "FAST-20";
-               else if (tp->period < 2000)     scsi = "FAST-10";
-               else                            scsi = "FAST-5";
-
-               printk ("%s %sSCSI %d.%d MB/s (%d ns, offset %d)\n", scsi,
-                       tp->widedone > 1 ? "WIDE " : "",
-                       mb10 / 10, mb10 % 10, tp->period / 10, sxfer & 0x1f);
-       } else
-               printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
+               /* Disable extended Sreq/Sack filtering */
+               if (tp->period <= 2000)
+                       OUTOFFB(nc_stest2, EXT);
+       }
+       spi_display_xfer_agreement(tp->starget);
 
        /*
        **      set actual value and sync_status
@@ -4945,7 +5612,7 @@ static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack
        u_char  scntl3;
        u_char  sxfer;
 
-       BUG_ON(target != (cmd->device->id & 0xf));
+       BUG_ON(target != (scmd_id(cmd) & 0xf));
 
        tp = &np->target[target];
        tp->widedone  =  wide+1;
@@ -4964,11 +5631,8 @@ static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack
        **      Bells and whistles   ;-)
        */
        if (bootverbose >= 2) {
-               PRINT_TARGET(np, target);
-               if (scntl3 & EWS)
-                       printk ("WIDE SCSI (16 bit) enabled.\n");
-               else
-                       printk ("WIDE SCSI disabled.\n");
+               dev_info(&cmd->device->sdev_target->dev, "WIDE SCSI %sabled.\n",
+                               (scntl3 & EWS) ? "en" : "dis");
        }
 
        /*
@@ -5023,7 +5687,7 @@ static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
                reqtags = lp->numtags;
        } else {
                reqtags = 1;
-       };
+       }
 
        /*
        **      Update max number of tags
@@ -5063,12 +5727,13 @@ static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
        **      Announce change to user.
        */
        if (bootverbose) {
-               PRINT_LUN(np, tn, ln);
                if (lp->usetags) {
-                       printk("tagged command queue depth set to %d\n", reqtags);
-               }
-               else {
-                       printk("tagged command queueing disabled\n");
+                       dev_info(&sdev->sdev_gendev,
+                               "tagged command queue depth set to %d\n",
+                               reqtags);
+               } else {
+                       dev_info(&sdev->sdev_gendev,
+                                       "tagged command queueing disabled\n");
                }
        }
 }
@@ -5089,7 +5754,7 @@ static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
 
 static void ncr_timeout (struct ncb *np)
 {
-       u_long  thistime = ktime_get(0);
+       u_long  thistime = jiffies;
 
        /*
        **      If release process in progress, let's go
@@ -5102,7 +5767,7 @@ static void ncr_timeout (struct ncb *np)
                return;
        }
 
-       np->timer.expires = ktime_get(SCSI_NCR_TIMER_INTERVAL);
+       np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
        add_timer(&np->timer);
 
        /*
@@ -5274,7 +5939,7 @@ void ncr_exception (struct ncb *np)
                istat = INB (nc_istat);
                if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
                ncr_wakeup_done (np);
-       };
+       }
 
        if (!(istat & (SIP|DIP)))
                return;
@@ -5335,7 +6000,7 @@ void ncr_exception (struct ncb *np)
                }
                OUTONB_STD ();
                return;
-       };
+       }
 
        /*========================================================
        **      Now, interrupts that need some fixing up.
@@ -5355,7 +6020,7 @@ void ncr_exception (struct ncb *np)
        if (sist & RST) {
                ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
                return;
-       };
+       }
 
        if ((sist & STO) &&
                !(dstat & (MDPE|BF|ABRT))) {
@@ -5366,7 +6031,7 @@ void ncr_exception (struct ncb *np)
 
                ncr_int_sto (np);
                return;
-       };
+       }
 
        /*=========================================================
        **      Now, interrupts we are not able to recover cleanly.
@@ -5381,13 +6046,13 @@ void ncr_exception (struct ncb *np)
        **=========================================================
        */
 
-       if (ktime_exp(np->regtime)) {
-               np->regtime = ktime_get(10*HZ);
+       if (time_after(jiffies, np->regtime)) {
+               np->regtime = jiffies + 10*HZ;
                for (i = 0; i<sizeof(np->regdump); i++)
                        ((char*)&np->regdump)[i] = INB_OFF(i);
                np->regdump.nc_dstat = dstat;
                np->regdump.nc_sist  = sist;
-       };
+       }
 
        ncr_log_hard_error(np, sist, dstat);
 
@@ -5399,20 +6064,20 @@ void ncr_exception (struct ncb *np)
                (dstat & (MDPE|BF|ABRT|IID))) {
                ncr_start_reset(np);
                return;
-       };
+       }
 
        if (sist & HTH) {
                printk ("%s: handshake timeout\n", ncr_name(np));
                ncr_start_reset(np);
                return;
-       };
+       }
 
        if (sist & UDC) {
                printk ("%s: unexpected disconnect\n", ncr_name(np));
                OUTB (HS_PRT, HS_UNEXPECTED);
                OUTL_DSP (NCB_SCRIPT_PHYS (np, cleanup));
                return;
-       };
+       }
 
        /*=========================================================
        **      We just miss the cause of the interrupt. :(
@@ -5456,7 +6121,7 @@ void ncr_int_sto (struct ncb *np)
        if (cp) {
                cp-> host_status = HS_SEL_TIMEOUT;
                ncr_complete (np, cp);
-       };
+       }
 
        /*
        **      repair start queue and jump to start point.
@@ -5498,7 +6163,7 @@ static int ncr_int_sbmc (struct ncb *np)
                **      Suspend command processing for 1 second and 
                **      reinitialize all except the chip.
                */
-               np->settle_time = ktime_get(1*HZ);
+               np->settle_time = jiffies + HZ;
                ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
                return 1;
        }
@@ -5553,9 +6218,9 @@ static int ncr_int_par (struct ncb *np)
        if (!(dbc & 0xc0000000))
                phase = (dbc >> 24) & 7;
        if (phase == 7)
-               msg = M_PARITY;
+               msg = MSG_PARITY_ERROR;
        else
-               msg = M_ID_ERROR;
+               msg = INITIATOR_ERROR;
 
 
        /*
@@ -5647,7 +6312,7 @@ static void ncr_int_ma (struct ncb *np)
                        ss2 = INB (nc_sstat2);
                        if (ss2 & OLF1) rest++;
                        if (ss2 & ORF1) rest++;
-               };
+               }
 
                if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
                        printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
@@ -5716,7 +6381,7 @@ static void ncr_int_ma (struct ncb *np)
                        cp, np->header.cp,
                        (unsigned)dsp,
                        (unsigned)nxtdsp, vdsp, cmd);
-       };
+       }
 
        /*
        **      cp=0 means that the DSA does not point to a valid control 
@@ -5744,7 +6409,7 @@ static void ncr_int_ma (struct ncb *np)
        } else {
                tblp = (u32 *) 0;
                olen = scr_to_cpu(vdsp[0]) & 0xffffff;
-       };
+       }
 
        if (DEBUG_FLAGS & DEBUG_PHASE) {
                printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
@@ -5752,16 +6417,15 @@ static void ncr_int_ma (struct ncb *np)
                        tblp,
                        (unsigned) olen,
                        (unsigned) oadr);
-       };
+       }
 
        /*
        **      check cmd against assumed interrupted script command.
        */
 
        if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
-               PRINT_ADDR(cp->cmd);
-               printk ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
-                       (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
+               PRINT_ADDR(cp->cmd, "internal error: cmd=%02x != %02x=(vdsp[0] "
+                               ">> 24)\n", cmd, scr_to_cpu(vdsp[0]) >> 24);
 
                goto reset_all;
        }
@@ -5783,12 +6447,11 @@ static void ncr_int_ma (struct ncb *np)
        */
 
        if (cmd & 0x06) {
-               PRINT_ADDR(cp->cmd);
-               printk ("phase change %x-%x %d@%08x resid=%d.\n",
+               PRINT_ADDR(cp->cmd, "phase change %x-%x %d@%08x resid=%d.\n",
                        cmd&7, sbcl&7, (unsigned)olen,
                        (unsigned)oadr, (unsigned)rest);
                goto unexpected_phase;
-       };
+       }
 
        /*
        **      choose the correct patch area.
@@ -5812,8 +6475,7 @@ static void ncr_int_ma (struct ncb *np)
        newcmd[3] = cpu_to_scr(nxtdsp);
 
        if (DEBUG_FLAGS & DEBUG_PHASE) {
-               PRINT_ADDR(cp->cmd);
-               printk ("newcmd[%d] %x %x %x %x.\n",
+               PRINT_ADDR(cp->cmd, "newcmd[%d] %x %x %x %x.\n",
                        (int) (newcmd - cp->patch),
                        (unsigned)scr_to_cpu(newcmd[0]),
                        (unsigned)scr_to_cpu(newcmd[1]),
@@ -5939,9 +6601,8 @@ static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
                if (!lp)
                        goto out;
                if (bootverbose >= 1) {
-                       PRINT_ADDR(cmd);
-                       printk ("QUEUE FULL! %d busy, %d disconnected CCBs\n",
-                               busy_cnt, disc_cnt);
+                       PRINT_ADDR(cmd, "QUEUE FULL! %d busy, %d disconnected "
+                                       "CCBs\n", busy_cnt, disc_cnt);
                }
                if (disc_cnt < lp->numtags) {
                        lp->numtags     = disc_cnt > 2 ? disc_cnt : 2;
@@ -5978,7 +6639,7 @@ static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
                **
                **      identify message
                */
-               cp->scsi_smsg2[0]       = M_IDENTIFY | cmd->device->lun;
+               cp->scsi_smsg2[0]       = IDENTIFY(0, cmd->device->lun);
                cp->phys.smsg.addr      = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
                cp->phys.smsg.size      = cpu_to_scr(1);
 
@@ -6048,34 +6709,6 @@ out:
 **==========================================================
 */
 
-static int ncr_show_msg (u_char * msg)
-{
-       u_char i;
-       printk ("%x",*msg);
-       if (*msg==M_EXTENDED) {
-               for (i=1;i<8;i++) {
-                       if (i-1>msg[1]) break;
-                       printk ("-%x",msg[i]);
-               };
-               return (i+1);
-       } else if ((*msg & 0xf0) == 0x20) {
-               printk ("-%x",msg[1]);
-               return (2);
-       };
-       return (1);
-}
-
-static void ncr_print_msg ( struct ccb *cp, char *label, u_char *msg)
-{
-       if (cp)
-               PRINT_ADDR(cp->cmd);
-       if (label)
-               printk("%s: ", label);
-       
-       (void) ncr_show_msg (msg);
-       printk(".\n");
-}
-
 void ncr_int_sir (struct ncb *np)
 {
        u_char scntl3;
@@ -6151,6 +6784,8 @@ void ncr_int_sir (struct ncb *np)
 /*-----------------------------------------------------------------------------
 **
 **     Was Sie schon immer ueber transfermode negotiation wissen wollten ...
+**     ("Everything you've always wanted to know about transfer mode
+**       negotiation")
 **
 **     We try to negotiate sync and wide transfer only after
 **     a successful inquire command. We look at byte 7 of the
@@ -6230,10 +6865,9 @@ void ncr_int_sir (struct ncb *np)
                */
 
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("negotiation failed sir=%x status=%x.\n",
-                               num, cp->nego_status);
-               };
+                       PRINT_ADDR(cp->cmd, "negotiation failed sir=%x "
+                                       "status=%x.\n", num, cp->nego_status);
+               }
 
                /*
                **      any error in negotiation:
@@ -6242,37 +6876,26 @@ void ncr_int_sir (struct ncb *np)
                switch (cp->nego_status) {
 
                case NS_SYNC:
-                       ncr_setsync (np, cp, 0, 0xe0);
                        spi_period(starget) = 0;
                        spi_offset(starget) = 0;
+                       ncr_setsync (np, cp, 0, 0xe0);
                        break;
 
                case NS_WIDE:
-                       ncr_setwide (np, cp, 0, 0);
                        spi_width(starget) = 0;
+                       ncr_setwide (np, cp, 0, 0);
                        break;
 
-               };
-               np->msgin [0] = M_NOOP;
-               np->msgout[0] = M_NOOP;
+               }
+               np->msgin [0] = NOP;
+               np->msgout[0] = NOP;
                cp->nego_status = 0;
                break;
 
        case SIR_NEGO_SYNC:
-               /*
-               **      Synchronous request message received.
-               */
-
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("sync msgin: ");
-                       (void) ncr_show_msg (np->msgin);
-                       printk (".\n");
-               };
-
-               /*
-               **      get requested values.
-               */
+                       ncr_print_msg(cp, "sync msgin", np->msgin);
+               }
 
                chg = 0;
                per = np->msgin[3];
@@ -6284,8 +6907,8 @@ void ncr_int_sir (struct ncb *np)
                **            it CAN transfer synch.
                */
 
-               if (ofs && tp->starget)
-                       spi_support_sync(tp->starget) = 1;
+               if (ofs && starget)
+                       spi_support_sync(starget) = 1;
 
                /*
                **      check values against driver limits.
@@ -6318,9 +6941,8 @@ void ncr_int_sir (struct ncb *np)
                }
 
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
-                               per, scntl3, ofs, fak, chg);
+                       PRINT_ADDR(cp->cmd, "sync: per=%d scntl3=0x%x ofs=%d "
+                               "fak=%d chg=%d.\n", per, scntl3, ofs, fak, chg);
                }
 
                if (INB (HS_PRT) == HS_NEGOTIATE) {
@@ -6328,64 +6950,50 @@ void ncr_int_sir (struct ncb *np)
                        switch (cp->nego_status) {
 
                        case NS_SYNC:
-                               /*
-                               **      This was an answer message
-                               */
+                               /* This was an answer message */
                                if (chg) {
-                                       /*
-                                       **      Answer wasn't acceptable.
-                                       */
-                                       ncr_setsync (np, cp, 0, 0xe0);
+                                       /* Answer wasn't acceptable.  */
                                        spi_period(starget) = 0;
                                        spi_offset(starget) = 0;
-                                       OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
+                                       ncr_setsync(np, cp, 0, 0xe0);
+                                       OUTL_DSP(NCB_SCRIPT_PHYS (np, msg_bad));
                                } else {
-                                       /*
-                                       **      Answer is ok.
-                                       */
-                                       ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
+                                       /* Answer is ok.  */
                                        spi_period(starget) = per;
                                        spi_offset(starget) = ofs;
-                                       OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
-                               };
+                                       ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
+                                       OUTL_DSP(NCB_SCRIPT_PHYS (np, clrack));
+                               }
                                return;
 
                        case NS_WIDE:
-                               ncr_setwide (np, cp, 0, 0);
                                spi_width(starget) = 0;
+                               ncr_setwide(np, cp, 0, 0);
                                break;
-                       };
-               };
+                       }
+               }
 
                /*
                **      It was a request. Set value and
                **      prepare an answer message
                */
 
-               ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
                spi_period(starget) = per;
                spi_offset(starget) = ofs;
+               ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
 
-               np->msgout[0] = M_EXTENDED;
-               np->msgout[1] = 3;
-               np->msgout[2] = M_X_SYNC_REQ;
-               np->msgout[3] = per;
-               np->msgout[4] = ofs;
-
+               spi_populate_sync_msg(np->msgout, per, ofs);
                cp->nego_status = NS_SYNC;
 
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("sync msgout: ");
-                       (void) ncr_show_msg (np->msgout);
-                       printk (".\n");
+                       ncr_print_msg(cp, "sync msgout", np->msgout);
                }
 
                if (!ofs) {
                        OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
                        return;
                }
-               np->msgin [0] = M_NOOP;
+               np->msgin [0] = NOP;
 
                break;
 
@@ -6394,11 +7002,8 @@ void ncr_int_sir (struct ncb *np)
                **      Wide request message received.
                */
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("wide msgin: ");
-                       (void) ncr_show_msg (np->msgin);
-                       printk (".\n");
-               };
+                       ncr_print_msg(cp, "wide msgin", np->msgin);
+               }
 
                /*
                **      get requested values.
@@ -6412,8 +7017,8 @@ void ncr_int_sir (struct ncb *np)
                **            it CAN transfer wide.
                */
 
-               if (wide && tp->starget)
-                       spi_support_wide(tp->starget) = 1;
+               if (wide && starget)
+                       spi_support_wide(starget) = 1;
 
                /*
                **      check values against driver limits.
@@ -6423,8 +7028,8 @@ void ncr_int_sir (struct ncb *np)
                        {chg = 1; wide = tp->usrwide;}
 
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("wide: wide=%d chg=%d.\n", wide, chg);
+                       PRINT_ADDR(cp->cmd, "wide: wide=%d chg=%d.\n", wide,
+                                       chg);
                }
 
                if (INB (HS_PRT) == HS_NEGOTIATE) {
@@ -6436,52 +7041,41 @@ void ncr_int_sir (struct ncb *np)
                                **      This was an answer message
                                */
                                if (chg) {
-                                       /*
-                                       **      Answer wasn't acceptable.
-                                       */
-                                       ncr_setwide (np, cp, 0, 1);
+                                       /* Answer wasn't acceptable.  */
                                        spi_width(starget) = 0;
+                                       ncr_setwide(np, cp, 0, 1);
                                        OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
                                } else {
-                                       /*
-                                       **      Answer is ok.
-                                       */
-                                       ncr_setwide (np, cp, wide, 1);
+                                       /* Answer is ok.  */
                                        spi_width(starget) = wide;
+                                       ncr_setwide(np, cp, wide, 1);
                                        OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
-                               };
+                               }
                                return;
 
                        case NS_SYNC:
-                               ncr_setsync (np, cp, 0, 0xe0);
                                spi_period(starget) = 0;
                                spi_offset(starget) = 0;
+                               ncr_setsync(np, cp, 0, 0xe0);
                                break;
-                       };
-               };
+                       }
+               }
 
                /*
                **      It was a request, set value and
                **      prepare an answer message
                */
 
-               ncr_setwide (np, cp, wide, 1);
                spi_width(starget) = wide;
+               ncr_setwide(np, cp, wide, 1);
+               spi_populate_width_msg(np->msgout, wide);
 
-               np->msgout[0] = M_EXTENDED;
-               np->msgout[1] = 2;
-               np->msgout[2] = M_X_WIDE_REQ;
-               np->msgout[3] = wide;
-
-               np->msgin [0] = M_NOOP;
+               np->msgin [0] = NOP;
 
                cp->nego_status = NS_WIDE;
 
                if (DEBUG_FLAGS & DEBUG_NEGO) {
-                       PRINT_ADDR(cp->cmd);
-                       printk ("wide msgout: ");
-                       (void) ncr_show_msg (np->msgin);
-                       printk (".\n");
+                       ncr_print_msg(cp, "wide msgout", np->msgin);
                }
                break;
 
@@ -6495,13 +7089,12 @@ void ncr_int_sir (struct ncb *np)
        case SIR_REJECT_RECEIVED:
                /*-----------------------------------------------
                **
-               **      We received a M_REJECT message.
+               **      We received a MESSAGE_REJECT.
                **
                **-----------------------------------------------
                */
 
-               PRINT_ADDR(cp->cmd);
-               printk ("M_REJECT received (%x:%x).\n",
+               PRINT_ADDR(cp->cmd, "MESSAGE_REJECT received (%x:%x).\n",
                        (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
                break;
 
@@ -6513,10 +7106,7 @@ void ncr_int_sir (struct ncb *np)
                **-----------------------------------------------
                */
 
-               PRINT_ADDR(cp->cmd);
-               printk ("M_REJECT sent for ");
-               (void) ncr_show_msg (np->msgin);
-               printk (".\n");
+               ncr_print_msg(cp, "MESSAGE_REJECT sent for", np->msgin);
                break;
 
 /*--------------------------------------------------------------------
@@ -6535,8 +7125,8 @@ void ncr_int_sir (struct ncb *np)
                **-----------------------------------------------
                */
 
-               PRINT_ADDR(cp->cmd);
-               printk ("M_IGN_RESIDUE received, but not yet implemented.\n");
+               PRINT_ADDR(cp->cmd, "IGNORE_WIDE_RESIDUE received, but not yet "
+                               "implemented.\n");
                break;
 #if 0
        case SIR_MISSING_SAVE:
@@ -6548,15 +7138,14 @@ void ncr_int_sir (struct ncb *np)
                **-----------------------------------------------
                */
 
-               PRINT_ADDR(cp->cmd);
-               printk ("M_DISCONNECT received, but datapointer not saved: "
-                       "data=%x save=%x goal=%x.\n",
+               PRINT_ADDR(cp->cmd, "DISCONNECT received, but datapointer "
+                               "not saved: data=%x save=%x goal=%x.\n",
                        (unsigned) INL (nc_temp),
                        (unsigned) scr_to_cpu(np->header.savep),
                        (unsigned) scr_to_cpu(np->header.goalp));
                break;
 #endif
-       };
+       }
 
 out:
        OUTONB_STD ();
@@ -6571,8 +7160,10 @@ out:
 **==========================================================
 */
 
-static struct ccb *ncr_get_ccb (struct ncb *np, u_char tn, u_char ln)
+static struct ccb *ncr_get_ccb(struct ncb *np, struct scsi_cmnd *cmd)
 {
+       u_char tn = cmd->device->id;
+       u_char ln = cmd->device->lun;
        struct tcb *tp = &np->target[tn];
        struct lcb *lp = tp->lp[ln];
        u_char tag = NO_TAG;
@@ -6602,8 +7193,8 @@ static    struct ccb *ncr_get_ccb (struct ncb *np, u_char tn, u_char ln)
                if (qp) {
                        cp = list_entry(qp, struct ccb, link_ccbq);
                        if (cp->magic) {
-                               PRINT_LUN(np, tn, ln);
-                               printk ("ccb free list corrupted (@%p)\n", cp);
+                               PRINT_ADDR(cmd, "ccb free list corrupted "
+                                               "(@%p)\n", cp);
                                cp = NULL;
                        } else {
                                list_add_tail(qp, &lp->wait_ccbq);
@@ -6637,7 +7228,7 @@ static    struct ccb *ncr_get_ccb (struct ncb *np, u_char tn, u_char ln)
                if (flags & SCSI_NOSLEEP) break;
                if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
                        break;
-       };
+       }
 #endif
 
        if (cp->magic)
@@ -6665,8 +7256,7 @@ static    struct ccb *ncr_get_ccb (struct ncb *np, u_char tn, u_char ln)
        cp->lun    = ln;
 
        if (DEBUG_FLAGS & DEBUG_TAGS) {
-               PRINT_LUN(np, tn, ln);
-               printk ("ccb @%p using tag %d.\n", cp, tag);
+               PRINT_ADDR(cmd, "ccb @%p using tag %d.\n", cp, tag);
        }
 
        return cp;
@@ -6687,8 +7277,7 @@ static void ncr_free_ccb (struct ncb *np, struct ccb *cp)
        struct lcb *lp = tp->lp[cp->lun];
 
        if (DEBUG_FLAGS & DEBUG_TAGS) {
-               PRINT_LUN(np, cp->target, cp->lun);
-               printk ("ccb @%p freeing tag %d.\n", cp, cp->tag);
+               PRINT_ADDR(cp->cmd, "ccb @%p freeing tag %d.\n", cp, cp->tag);
        }
 
        /*
@@ -7014,20 +7603,11 @@ static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
        unsigned char tn = sdev->id, ln = sdev->lun;
        struct tcb *tp = &np->target[tn];
        struct lcb *lp = tp->lp[ln];
-       struct scsi_target *starget = tp->starget;
 
-       /*
-       **      If no lcb, try to allocate it.
-       */
+       /* If no lcb, try to allocate it.  */
        if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
                goto fail;
 
-       /*
-       **      Prepare negotiation
-       */
-       if (spi_support_wide(starget) || spi_support_sync(starget))
-               ncr_negotiate(np, tp);
-
        /*
        **      If unit supports tagged commands, allocate the 
        **      CCB JUMP table if not yet.
@@ -7046,7 +7626,7 @@ static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
                for (i = 0 ; i < MAX_TAGS ; i++)
                        lp->cb_tags[i] = i;
                lp->maxnxs = MAX_TAGS;
-               lp->tags_stime = ktime_get(3*HZ);
+               lp->tags_stime = jiffies + 3*HZ;
                ncr_setup_tags (np, sdev);
        }
 
@@ -7116,7 +7696,7 @@ static int ncr_scatter(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
        if (!use_sg)
                segment = ncr_scatter_no_sglist(np, cp, cmd);
        else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
-               struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
+               struct scatterlist *scatter = (struct scatterlist *)cmd->request_buffer;
                struct scr_tblmove *data;
 
                if (use_sg > MAX_SCATTER) {
@@ -7170,7 +7750,7 @@ static int __init ncr_regtest (struct ncb* np)
                printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
                        (unsigned) data);
                return (0x10);
-       };
+       }
        return (0);
 }
 
@@ -7223,7 +7803,7 @@ static int __init ncr_snooptest (struct ncb* np)
        if (i>=NCR_SNOOP_TIMEOUT) {
                printk ("CACHE TEST FAILED: timeout.\n");
                return (0x20);
-       };
+       }
        /*
        **      Check termination position.
        */
@@ -7233,7 +7813,7 @@ static int __init ncr_snooptest (struct ncb* np)
                        (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
                        (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
                return (0x40);
-       };
+       }
        /*
        **      Show results.
        */
@@ -7241,17 +7821,17 @@ static int __init ncr_snooptest (struct ncb* np)
                printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
                        (int) host_wr, (int) ncr_rd);
                err |= 1;
-       };
+       }
        if (host_rd != ncr_wr) {
                printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
                        (int) ncr_wr, (int) host_rd);
                err |= 2;
-       };
+       }
        if (ncr_bk != ncr_wr) {
                printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
                        (int) ncr_wr, (int) ncr_bk);
                err |= 4;
-       };
+       }
        return (err);
 }
 
@@ -7264,7 +7844,7 @@ static int __init ncr_snooptest (struct ncb* np)
 **==========================================================
 **
 **     Note: we have to return the correct value.
-**     THERE IS NO SAVE DEFAULT VALUE.
+**     THERE IS NO SAFE DEFAULT VALUE.
 **
 **     Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
 **     53C860 and 53C875 rev. 1 support fast20 transfers but 
@@ -7424,6 +8004,16 @@ static void __init ncr_getclock (struct ncb *np, int mult)
 
 /*===================== LINUX ENTRY POINTS SECTION ==========================*/
 
+static int ncr53c8xx_slave_alloc(struct scsi_device *device)
+{
+       struct Scsi_Host *host = device->host;
+       struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
+       struct tcb *tp = &np->target[device->id];
+       tp->starget = device->sdev_target;
+
+       return 0;
+}
+
 static int ncr53c8xx_slave_configure(struct scsi_device *device)
 {
        struct Scsi_Host *host = device->host;
@@ -7432,8 +8022,6 @@ static int ncr53c8xx_slave_configure(struct scsi_device *device)
        struct lcb *lp = tp->lp[device->lun];
        int numtags, depth_to_use;
 
-       tp->starget = device->sdev_target;
-
        ncr_setup_lcb(np, device);
 
        /*
@@ -7601,24 +8189,14 @@ static int ncr53c8xx_abort(struct scsi_cmnd *cmd)
        struct scsi_cmnd *done_list;
 
 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
-       printk("ncr53c8xx_abort: pid=%lu serial_number=%ld serial_number_at_timeout=%ld\n",
-               cmd->pid, cmd->serial_number, cmd->serial_number_at_timeout);
+       printk("ncr53c8xx_abort: pid=%lu serial_number=%ld\n",
+               cmd->pid, cmd->serial_number);
 #else
        printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
 #endif
 
        NCR_LOCK_NCB(np, flags);
 
-#if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
-       /*
-        * We have to just ignore abort requests in some situations.
-        */
-       if (cmd->serial_number != cmd->serial_number_at_timeout) {
-               sts = SCSI_ABORT_NOT_RUNNING;
-               goto out;
-       }
-#endif
-
        sts = ncr_abort_command(np, cmd);
 out:
        done_list     = np->done_list;
@@ -7778,6 +8356,7 @@ struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
 
        tpnt->queuecommand      = ncr53c8xx_queue_command;
        tpnt->slave_configure   = ncr53c8xx_slave_configure;
+       tpnt->slave_alloc       = ncr53c8xx_slave_alloc;
        tpnt->eh_bus_reset_handler = ncr53c8xx_bus_reset;
        tpnt->can_queue         = SCSI_NCR_CAN_QUEUE;
        tpnt->this_id           = 7;
@@ -7880,7 +8459,6 @@ struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
         * your module_init */
        BUG_ON(!ncr53c8xx_transport_template);
        instance->transportt    = ncr53c8xx_transport_template;
-       scsi_set_device(instance, device->dev);
 
        /* Patch script to physical addresses */
        ncr_script_fill(&script0, &scripth0);
@@ -7925,7 +8503,7 @@ struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
        if (ncr_snooptest(np)) {
                printk(KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
                goto attach_error;
-       };
+       }
 
        /* Install the interrupt handler.  */
        np->irq = device->slot.irq;
@@ -7966,7 +8544,7 @@ struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
 
        /* use SIMPLE TAG messages by default */
 #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
-       np->order = M_SIMPLE_TAG;
+       np->order = SIMPLE_QUEUE_TAG;
 #endif
 
        spin_unlock_irqrestore(&np->smp_lock, flags);
@@ -8057,6 +8635,25 @@ static void ncr53c8xx_set_width(struct scsi_target *starget, int width)
        ncr_negotiate(np, tp);
 }
 
+static void ncr53c8xx_get_signalling(struct Scsi_Host *shost)
+{
+       struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
+       enum spi_signal_type type;
+
+       switch (np->scsi_mode) {
+       case SMODE_SE:
+               type = SPI_SIGNAL_SE;
+               break;
+       case SMODE_HVD:
+               type = SPI_SIGNAL_HVD;
+               break;
+       default:
+               type = SPI_SIGNAL_UNKNOWN;
+               break;
+       }
+       spi_signalling(shost) = type;
+}
+
 static struct spi_function_template ncr53c8xx_transport_functions =  {
        .set_period     = ncr53c8xx_set_period,
        .show_period    = 1,
@@ -8064,6 +8661,7 @@ static struct spi_function_template ncr53c8xx_transport_functions =  {
        .show_offset    = 1,
        .set_width      = ncr53c8xx_set_width,
        .show_width     = 1,
+       .get_signalling = ncr53c8xx_get_signalling,
 };
 
 int __init ncr53c8xx_init(void)