Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / scsi / sym53c8xx_2 / sym_glue.c
index 3ed36af..9c83b4d 100644 (file)
@@ -3,7 +3,7 @@
  * of PCI-SCSI IO processors.
  *
  * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
- * Copyright (c) 2003-2004  Matthew Wilcox <matthew@wil.cx>
+ * Copyright (c) 2003-2005  Matthew Wilcox <matthew@wil.cx>
  *
  * This driver is derived from the Linux sym53c8xx driver.
  * Copyright (C) 1998-2000  Gerard Roudier
@@ -47,7 +47,6 @@
 #include <scsi/scsi_tcq.h>
 #include <scsi/scsi_device.h>
 #include <scsi/scsi_transport.h>
-#include <scsi/scsi_transport_spi.h>
 
 #include "sym_glue.h"
 #include "sym_nvram.h"
@@ -135,104 +134,21 @@ static void sym2_setup_params(void)
        }
 }
 
-static int __devinit
-pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
-{
-       u32 tmp;
-#define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
-
-       pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
-       *base = tmp;
-       ++index;
-       if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
-#if BITS_PER_LONG > 32
-               pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
-               *base |= (((u_long)tmp) << 32);
-#endif
-               ++index;
-       }
-       return index;
-#undef PCI_BAR_OFFSET
-}
-
-/* This lock protects only the memory allocation/free.  */
-static DEFINE_SPINLOCK(sym53c8xx_lock);
-
 static struct scsi_transport_template *sym2_transport_template = NULL;
 
-/*
- *  Wrappers to the generic memory allocator.
- */
-void *sym_calloc(int size, char *name)
-{
-       unsigned long flags;
-       void *m;
-       spin_lock_irqsave(&sym53c8xx_lock, flags);
-       m = sym_calloc_unlocked(size, name);
-       spin_unlock_irqrestore(&sym53c8xx_lock, flags);
-       return m;
-}
-
-void sym_mfree(void *m, int size, char *name)
-{
-       unsigned long flags;
-       spin_lock_irqsave(&sym53c8xx_lock, flags);
-       sym_mfree_unlocked(m, size, name);
-       spin_unlock_irqrestore(&sym53c8xx_lock, flags);
-}
-
-void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
-{
-       unsigned long flags;
-       void *m;
-       spin_lock_irqsave(&sym53c8xx_lock, flags);
-       m = __sym_calloc_dma_unlocked(dev_dmat, size, name);
-       spin_unlock_irqrestore(&sym53c8xx_lock, flags);
-       return m;
-}
-
-void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
-{
-       unsigned long flags;
-       spin_lock_irqsave(&sym53c8xx_lock, flags);
-       __sym_mfree_dma_unlocked(dev_dmat, m, size, name);
-       spin_unlock_irqrestore(&sym53c8xx_lock, flags);
-}
-
-m_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
-{
-       unsigned long flags;
-       m_addr_t b;
-       spin_lock_irqsave(&sym53c8xx_lock, flags);
-       b = __vtobus_unlocked(dev_dmat, m);
-       spin_unlock_irqrestore(&sym53c8xx_lock, flags);
-       return b;
-}
-
-/*
- *  Used by the eh thread to wait for command completion.
- *  It is allocated on the eh thread stack.
- */
-struct sym_eh_wait {
-       struct completion done;
-       struct timer_list timer;
-       void (*old_done)(struct scsi_cmnd *);
-       int to_do;
-       int timed_out;
-};
-
 /*
  *  Driver private area in the SCSI command structure.
  */
 struct sym_ucmd {              /* Override the SCSI pointer structure */
-       dma_addr_t data_mapping;
-       u_char  data_mapped;
-       struct sym_eh_wait *eh_wait;
+       dma_addr_t      data_mapping;
+       unsigned char   data_mapped;
+       unsigned char   to_do;                  /* For error handling */
+       void (*old_done)(struct scsi_cmnd *);   /* For error handling */
+       struct completion *eh_done;             /* For error handling */
 };
 
 #define SYM_UCMD_PTR(cmd)  ((struct sym_ucmd *)(&(cmd)->SCp))
-#define SYM_SCMD_PTR(ucmd) sym_que_entry(ucmd, struct scsi_cmnd, SCp)
-#define SYM_SOFTC_PTR(cmd) (((struct host_data *)cmd->device->host->hostdata)->ncb)
+#define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
 
 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
 {
@@ -288,30 +204,19 @@ static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
 /*
  *  Complete a pending CAM CCB.
  */
-void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *ccb)
+void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
 {
-       unmap_scsi_data(np, ccb);
-       ccb->scsi_done(ccb);
+       unmap_scsi_data(np, cmd);
+       cmd->scsi_done(cmd);
 }
 
-static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *ccb, int cam_status)
+static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *cmd, int cam_status)
 {
-       sym_set_cam_status(ccb, cam_status);
-       sym_xpt_done(np, ccb);
+       sym_set_cam_status(cmd, cam_status);
+       sym_xpt_done(np, cmd);
 }
 
 
-/*
- *  Print something that identifies the IO.
- */
-void sym_print_addr(struct sym_ccb *cp)
-{
-       struct scsi_cmnd *cmd = cp->cam_ccb;
-       if (cmd)
-               printf("%s:%d:%d:", sym_name(SYM_SOFTC_PTR(cmd)),
-                               cmd->device->id, cmd->device->lun);
-}
-
 /*
  *  Tell the SCSI layer about a BUS RESET.
  */
@@ -333,16 +238,6 @@ void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
        printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
 }
 
-/*
- *  Tell the SCSI layer about the new transfer parameters.
- */
-void sym_xpt_async_nego_wide(struct sym_hcb *np, int target)
-{
-       if (sym_verbose < 3)
-               return;
-       sym_announce_transfer_rate(np, target);
-}
-
 /*
  *  Choose the more appropriate CAM status if 
  *  the IO encountered an extended error.
@@ -367,7 +262,7 @@ static int sym_xerr_cam_status(int cam_status, int x_status)
  */
 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
 {
-       struct scsi_cmnd *csio = cp->cam_ccb;
+       struct scsi_cmnd *cmd = cp->cmd;
        u_int cam_status, scsi_status, drv_status;
 
        drv_status  = 0;
@@ -378,7 +273,7 @@ void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
                scsi_status = cp->sv_scsi_status;
                resid = cp->sv_resid;
                if (sym_verbose && cp->sv_xerr_status)
-                       sym_print_xerr(cp, cp->sv_xerr_status);
+                       sym_print_xerr(cmd, cp->sv_xerr_status);
                if (cp->host_status == HS_COMPLETE &&
                    cp->ssss_status == S_GOOD &&
                    cp->xerr_status == 0) {
@@ -388,9 +283,9 @@ void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
                        /*
                         *  Bounce back the sense data to user.
                         */
-                       memset(&csio->sense_buffer, 0, sizeof(csio->sense_buffer));
-                       memcpy(csio->sense_buffer, cp->sns_bbuf,
-                             min(sizeof(csio->sense_buffer),
+                       memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
+                       memcpy(cmd->sense_buffer, cp->sns_bbuf,
+                             min(sizeof(cmd->sense_buffer),
                                  (size_t)SYM_SNS_BBUF_LEN));
 #if 0
                        /*
@@ -400,7 +295,7 @@ void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
                         */
                        if (1) {
                                u_char *p;
-                               p  = (u_char *) csio->sense_data;
+                               p  = (u_char *) cmd->sense_data;
                                if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
                                        sym_clear_tasks(np, DID_ABORT,
                                                        cp->target,cp->lun, -1);
@@ -413,7 +308,7 @@ void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
                         * condition otherwise the device will always return
                         * BUSY.  Use a big stick.
                         */
-                       sym_reset_scsi_target(np, csio->device->id);
+                       sym_reset_scsi_target(np, cmd->device->id);
                        cam_status = DID_ERROR;
                }
        } else if (cp->host_status == HS_COMPLETE)      /* Bad SCSI status */
@@ -424,8 +319,7 @@ void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
                cam_status = DID_ERROR;
        else {                                          /* Extended error */
                if (sym_verbose) {
-                       PRINT_ADDR(cp);
-                       printf ("COMMAND FAILED (%x %x %x).\n",
+                       sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
                                cp->host_status, cp->ssss_status,
                                cp->xerr_status);
                }
@@ -434,8 +328,8 @@ void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
                 */
                cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
        }
-       csio->resid = resid;
-       csio->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
+       cmd->resid = resid;
+       cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
 }
 
 
@@ -447,13 +341,20 @@ static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct
 {
        struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
        int segment;
+       unsigned int len = cmd->request_bufflen;
 
-       cp->data_len = cmd->request_bufflen;
-
-       if (cmd->request_bufflen) {
+       if (len) {
                dma_addr_t baddr = map_scsi_single_data(np, cmd);
                if (baddr) {
-                       sym_build_sge(np, data, baddr, cmd->request_bufflen);
+                       if (len & 1) {
+                               struct sym_tcb *tp = &np->target[cp->target];
+                               if (tp->head.wval & EWS) {
+                                       len++;
+                                       cp->odd_byte_adjustment++;
+                               }
+                       }
+                       cp->data_len = len;
+                       sym_build_sge(np, data, baddr, len);
                        segment = 1;
                } else {
                        segment = -2;
@@ -476,6 +377,7 @@ static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd
                segment = sym_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 sym_tcb *tp = &np->target[cp->target];
                struct sym_tblmove *data;
 
                if (use_sg > SYM_CONF_MAX_SG) {
@@ -489,6 +391,11 @@ static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd
                        dma_addr_t baddr = sg_dma_address(&scatter[segment]);
                        unsigned int len = sg_dma_len(&scatter[segment]);
 
+                       if ((len & 1) && (tp->head.wval & EWS)) {
+                               len++;
+                               cp->odd_byte_adjustment++;
+                       }
+
                        sym_build_sge(np, &data[segment], baddr, len);
                        cp->data_len += len;
                }
@@ -502,9 +409,9 @@ static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd
 /*
  *  Queue a SCSI command.
  */
-static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *ccb)
+static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
 {
-/*     struct scsi_device        *device    = ccb->device; */
+       struct scsi_device *sdev = cmd->device;
        struct sym_tcb *tp;
        struct sym_lcb *lp;
        struct sym_ccb *cp;
@@ -514,78 +421,41 @@ static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *ccb)
         *  Minimal checkings, so that we will not 
         *  go outside our tables.
         */
-       if (ccb->device->id == np->myaddr ||
-           ccb->device->id >= SYM_CONF_MAX_TARGET ||
-           ccb->device->lun >= SYM_CONF_MAX_LUN) {
-               sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
+       if (sdev->id == np->myaddr) {
+               sym_xpt_done2(np, cmd, DID_NO_CONNECT);
                return 0;
        }
 
        /*
         *  Retrieve the target descriptor.
         */
-       tp = &np->target[ccb->device->id];
-
-       /*
-        *  Complete the 1st INQUIRY command with error 
-        *  condition if the device is flagged NOSCAN 
-        *  at BOOT in the NVRAM. This may speed up 
-        *  the boot and maintain coherency with BIOS 
-        *  device numbering. Clearing the flag allows 
-        *  user to rescan skipped devices later.
-        *  We also return error for devices not flagged 
-        *  for SCAN LUNS in the NVRAM since some mono-lun 
-        *  devices behave badly when asked for some non 
-        *  zero LUN. Btw, this is an absolute hack.:-)
-        */
-       if (ccb->cmnd[0] == 0x12 || ccb->cmnd[0] == 0x0) {
-               if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
-                   ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && 
-                    ccb->device->lun != 0)) {
-                       tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
-                       sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
-                       return 0;
-               }
-       }
+       tp = &np->target[sdev->id];
 
        /*
         *  Select tagged/untagged.
         */
-       lp = sym_lp(np, tp, ccb->device->lun);
+       lp = sym_lp(tp, sdev->lun);
        order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
 
        /*
         *  Queue the SCSI IO.
         */
-       cp = sym_get_ccb(np, ccb->device->id, ccb->device->lun, order);
+       cp = sym_get_ccb(np, cmd, order);
        if (!cp)
                return 1;       /* Means resource shortage */
-       sym_queue_scsiio(np, ccb, cp);
+       sym_queue_scsiio(np, cmd, cp);
        return 0;
 }
 
 /*
  *  Setup buffers and pointers that address the CDB.
  */
-static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *ccb, struct sym_ccb *cp)
+static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
 {
-       u32     cmd_ba;
-       int     cmd_len;
-
-       /*
-        *  CDB is 16 bytes max.
-        */
-       if (ccb->cmd_len > sizeof(cp->cdb_buf)) {
-               sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
-               return -1;
-       }
-
-       memcpy(cp->cdb_buf, ccb->cmnd, ccb->cmd_len);
-       cmd_ba  = CCB_BA (cp, cdb_buf[0]);
-       cmd_len = ccb->cmd_len;
+       memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
 
-       cp->phys.cmd.addr       = cpu_to_scr(cmd_ba);
-       cp->phys.cmd.size       = cpu_to_scr(cmd_len);
+       cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
+       cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
 
        return 0;
 }
@@ -593,40 +463,68 @@ static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *ccb, struc
 /*
  *  Setup pointers that address the data and start the I/O.
  */
-int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *csio, struct sym_ccb *cp)
+int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
 {
+       u32 lastp, goalp;
        int dir;
-       struct sym_tcb *tp = &np->target[cp->target];
-       struct sym_lcb *lp = sym_lp(np, tp, cp->lun);
 
        /*
         *  Build the CDB.
         */
-       if (sym_setup_cdb(np, csio, cp))
+       if (sym_setup_cdb(np, cmd, cp))
                goto out_abort;
 
        /*
         *  No direction means no data.
         */
-       dir = csio->sc_data_direction;
+       dir = cmd->sc_data_direction;
        if (dir != DMA_NONE) {
-               cp->segments = sym_scatter(np, cp, csio);
+               cp->segments = sym_scatter(np, cp, cmd);
                if (cp->segments < 0) {
-                       if (cp->segments == -2)
-                               sym_set_cam_status(csio, CAM_RESRC_UNAVAIL);
-                       else
-                               sym_set_cam_status(csio, CAM_REQ_TOO_BIG);
+                       sym_set_cam_status(cmd, DID_ERROR);
                        goto out_abort;
                }
+
+               /*
+                *  No segments means no data.
+                */
+               if (!cp->segments)
+                       dir = DMA_NONE;
        } else {
                cp->data_len = 0;
                cp->segments = 0;
        }
 
        /*
-        *  Set data pointers.
+        *  Set the data pointer.
+        */
+       switch (dir) {
+       case DMA_BIDIRECTIONAL:
+               printk("%s: got DMA_BIDIRECTIONAL command", sym_name(np));
+               sym_set_cam_status(cmd, DID_ERROR);
+               goto out_abort;
+       case DMA_TO_DEVICE:
+               goalp = SCRIPTA_BA(np, data_out2) + 8;
+               lastp = goalp - 8 - (cp->segments * (2*4));
+               break;
+       case DMA_FROM_DEVICE:
+               cp->host_flags |= HF_DATA_IN;
+               goalp = SCRIPTA_BA(np, data_in2) + 8;
+               lastp = goalp - 8 - (cp->segments * (2*4));
+               break;
+       case DMA_NONE:
+       default:
+               lastp = goalp = SCRIPTB_BA(np, no_data);
+               break;
+       }
+
+       /*
+        *  Set all pointers values needed by SCRIPTS.
         */
-       sym_setup_data_pointers(np, cp, dir);
+       cp->phys.head.lastp = cpu_to_scr(lastp);
+       cp->phys.head.savep = cpu_to_scr(lastp);
+       cp->startp          = cp->phys.head.savep;
+       cp->goalp           = cpu_to_scr(goalp);
 
        /*
         *  When `#ifed 1', the code below makes the driver 
@@ -647,15 +545,12 @@ int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *csio, struct
        /*
         *      activate this job.
         */
-       if (lp)
-               sym_start_next_ccbs(np, lp, 2);
-       else
-               sym_put_start_queue(np, cp);
+       sym_put_start_queue(np, cp);
        return 0;
 
 out_abort:
        sym_free_ccb(np, cp);
-       sym_xpt_done(np, csio);
+       sym_xpt_done(np, cmd);
        return 0;
 }
 
@@ -737,7 +632,6 @@ static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
        int sts = 0;
 
        cmd->scsi_done     = done;
-       cmd->host_scribble = NULL;
        memset(ucp, 0, sizeof(*ucp));
 
        /*
@@ -806,44 +700,22 @@ static void sym53c8xx_timer(unsigned long npref)
  *  What we will do regarding the involved SCSI command.
  */
 #define SYM_EH_DO_IGNORE       0
-#define SYM_EH_DO_COMPLETE     1
 #define SYM_EH_DO_WAIT         2
 
 /*
- *  Our general completion handler.
+ *  scsi_done() alias when error recovery is in progress.
  */
-static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
+static void sym_eh_done(struct scsi_cmnd *cmd)
 {
-       struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
-       if (!ep)
-               return;
-
-       /* Try to avoid a race here (not 100% safe) */
-       if (!timed_out) {
-               ep->timed_out = 0;
-               if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
-                       return;
-       }
+       struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
+       BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
 
-       /* Revert everything */
-       SYM_UCMD_PTR(cmd)->eh_wait = NULL;
-       cmd->scsi_done = ep->old_done;
+       cmd->scsi_done = ucmd->old_done;
 
-       /* Wake up the eh thread if it wants to sleep */
-       if (ep->to_do == SYM_EH_DO_WAIT)
-               complete(&ep->done);
+       if (ucmd->to_do == SYM_EH_DO_WAIT)
+               complete(ucmd->eh_done);
 }
 
-/*
- *  scsi_done() alias when error recovery is in progress. 
- */
-static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
-
-/*
- *  Some timeout handler to avoid waiting too long.
- */
-static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
-
 /*
  *  Generic method for our eh processing.
  *  The 'op' argument tells what we have to do.
@@ -851,44 +723,31 @@ static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1);
 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
 {
        struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
+       struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
+       struct Scsi_Host *host = cmd->device->host;
        SYM_QUEHEAD *qp;
        int to_do = SYM_EH_DO_IGNORE;
        int sts = -1;
-       struct sym_eh_wait eh, *ep = &eh;
-       char devname[20];
+       struct completion eh_done;
 
-       sprintf(devname, "%s:%d:%d", sym_name(np), cmd->device->id, cmd->device->lun);
-
-       printf_warning("%s: %s operation started.\n", devname, opname);
-
-#if 0
-       /* This one should be the result of some race, thus to ignore */
-       if (cmd->serial_number != cmd->serial_number_at_timeout)
-               goto prepare;
-#endif
+       dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
 
+       spin_lock_irq(host->host_lock);
        /* This one is queued in some place -> to wait for completion */
        FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
                struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
-               if (cp->cam_ccb == cmd) {
+               if (cp->cmd == cmd) {
                        to_do = SYM_EH_DO_WAIT;
-                       goto prepare;
+                       break;
                }
        }
 
-prepare:
-       /* Prepare stuff to either ignore, complete or wait for completion */
-       switch(to_do) {
-       default:
-       case SYM_EH_DO_IGNORE:
-               break;
-       case SYM_EH_DO_WAIT:
-               init_completion(&ep->done);
-               /* fall through */
-       case SYM_EH_DO_COMPLETE:
-               ep->old_done = cmd->scsi_done;
+       if (to_do == SYM_EH_DO_WAIT) {
+               init_completion(&eh_done);
+               ucmd->old_done = cmd->scsi_done;
+               ucmd->eh_done = &eh_done;
+               wmb();
                cmd->scsi_done = sym_eh_done;
-               SYM_UCMD_PTR(cmd)->eh_wait = ep;
        }
 
        /* Try to proceed the operation we have been asked for */
@@ -915,33 +774,23 @@ prepare:
 
        /* On error, restore everything and cross fingers :) */
        if (sts) {
-               SYM_UCMD_PTR(cmd)->eh_wait = NULL;
-               cmd->scsi_done = ep->old_done;
+               cmd->scsi_done = ucmd->old_done;
                to_do = SYM_EH_DO_IGNORE;
        }
 
-       ep->to_do = to_do;
-       /* Complete the command with locks held as required by the driver */
-       if (to_do == SYM_EH_DO_COMPLETE)
-               sym_xpt_done2(np, cmd, CAM_REQ_ABORTED);
+       ucmd->to_do = to_do;
+       spin_unlock_irq(host->host_lock);
 
-       /* Wait for completion with locks released, as required by kernel */
        if (to_do == SYM_EH_DO_WAIT) {
-               init_timer(&ep->timer);
-               ep->timer.expires = jiffies + (5*HZ);
-               ep->timer.function = sym_eh_timeout;
-               ep->timer.data = (u_long)cmd;
-               ep->timed_out = 1;      /* Be pessimistic for once :) */
-               add_timer(&ep->timer);
-               spin_unlock_irq(np->s.host->host_lock);
-               wait_for_completion(&ep->done);
-               spin_lock_irq(np->s.host->host_lock);
-               if (ep->timed_out)
+               if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
+                       ucmd->to_do = SYM_EH_DO_IGNORE;
+                       wmb();
                        sts = -2;
+               }
        }
-       printf_warning("%s: %s operation %s.\n", devname, opname,
-                       sts==0?"complete":sts==-2?"timed-out":"failed");
-       return sts? SCSI_FAILED : SCSI_SUCCESS;
+       dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
+                       sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
+       return sts ? SCSI_FAILED : SCSI_SUCCESS;
 }
 
 
@@ -971,10 +820,9 @@ static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
 /*
  *  Tune device queuing depth, according to various limits.
  */
-static void sym_tune_dev_queuing(struct sym_hcb *np, int target, int lun, u_short reqtags)
+static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
 {
-       struct sym_tcb *tp = &np->target[target];
-       struct sym_lcb *lp = sym_lp(np, tp, lun);
+       struct sym_lcb *lp = sym_lp(tp, lun);
        u_short oldtags;
 
        if (!lp)
@@ -985,16 +833,12 @@ static void sym_tune_dev_queuing(struct sym_hcb *np, int target, int lun, u_shor
        if (reqtags > lp->s.scdev_depth)
                reqtags = lp->s.scdev_depth;
 
-       lp->started_limit = reqtags ? reqtags : 2;
-       lp->started_max   = 1;
        lp->s.reqtags     = reqtags;
 
        if (reqtags != oldtags) {
-               printf_info("%s:%d:%d: "
+               dev_info(&tp->starget->dev,
                         "tagged command queuing %s, command queue depth %d.\n",
-                         sym_name(np), target, lun,
-                         lp->s.reqtags ? "enabled" : "disabled",
-                         lp->started_limit);
+                         lp->s.reqtags ? "enabled" : "disabled", reqtags);
        }
 }
 
@@ -1051,29 +895,59 @@ static int device_queue_depth(struct sym_hcb *np, int target, int lun)
        return DEF_DEPTH;
 }
 
-/*
- * Linux entry point for device queue sizing.
- */
-static int sym53c8xx_slave_configure(struct scsi_device *device)
+static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
 {
-       struct Scsi_Host *host = device->host;
-       struct sym_hcb *np;
-       struct sym_tcb *tp;
+       struct sym_hcb *np = sym_get_hcb(sdev->host);
+       struct sym_tcb *tp = &np->target[sdev->id];
        struct sym_lcb *lp;
-       int reqtags, depth_to_use;
 
-       np = ((struct host_data *) host->hostdata)->ncb;
-       tp = &np->target[device->id];
-       tp->sdev = device;
+       if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
+               return -ENXIO;
 
+       tp->starget = sdev->sdev_target;
        /*
-        *  Allocate the LCB if not yet.
-        *  If it fail, we may well be in the sh*t. :)
+        * Fail the device init if the device is flagged NOSCAN at BOOT in
+        * the NVRAM.  This may speed up boot and maintain coherency with
+        * BIOS device numbering.  Clearing the flag allows the user to
+        * rescan skipped devices later.  We also return an error for
+        * devices not flagged for SCAN LUNS in the NVRAM since some single
+        * lun devices behave badly when asked for a non zero LUN.
         */
-       lp = sym_alloc_lcb(np, device->id, device->lun);
+
+       if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
+               tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
+               starget_printk(KERN_INFO, tp->starget,
+                               "Scan at boot disabled in NVRAM\n");
+               return -ENXIO;
+       }
+
+       if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
+               if (sdev->lun != 0)
+                       return -ENXIO;
+               starget_printk(KERN_INFO, tp->starget,
+                               "Multiple LUNs disabled in NVRAM\n");
+       }
+
+       lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
        if (!lp)
                return -ENOMEM;
 
+       spi_min_period(tp->starget) = tp->usr_period;
+       spi_max_width(tp->starget) = tp->usr_width;
+
+       return 0;
+}
+
+/*
+ * Linux entry point for device queue sizing.
+ */
+static int sym53c8xx_slave_configure(struct scsi_device *sdev)
+{
+       struct sym_hcb *np = sym_get_hcb(sdev->host);
+       struct sym_tcb *tp = &np->target[sdev->id];
+       struct sym_lcb *lp = sym_lp(tp, sdev->lun);
+       int reqtags, depth_to_use;
+
        /*
         *  Get user flags.
         */
@@ -1085,10 +959,10 @@ static int sym53c8xx_slave_configure(struct scsi_device *device)
         *  Use at least 2.
         *  Donnot use more than our maximum.
         */
-       reqtags = device_queue_depth(np, device->id, device->lun);
+       reqtags = device_queue_depth(np, sdev->id, sdev->lun);
        if (reqtags > tp->usrtags)
                reqtags = tp->usrtags;
-       if (!device->tagged_supported)
+       if (!sdev->tagged_supported)
                reqtags = 0;
 #if 1 /* Avoid to locally queue commands for no good reasons */
        if (reqtags > SYM_CONF_MAX_TAG)
@@ -1097,25 +971,36 @@ static int sym53c8xx_slave_configure(struct scsi_device *device)
 #else
        depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
 #endif
-       scsi_adjust_queue_depth(device,
-                               (device->tagged_supported ?
+       scsi_adjust_queue_depth(sdev,
+                               (sdev->tagged_supported ?
                                 MSG_SIMPLE_TAG : 0),
                                depth_to_use);
        lp->s.scdev_depth = depth_to_use;
-       sym_tune_dev_queuing(np, device->id, device->lun, reqtags);
+       sym_tune_dev_queuing(tp, sdev->lun, reqtags);
 
-       if (!spi_initial_dv(device->sdev_target))
-               spi_dv_device(device);
+       if (!spi_initial_dv(sdev->sdev_target))
+               spi_dv_device(sdev);
 
        return 0;
 }
 
+static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
+{
+       struct sym_hcb *np = sym_get_hcb(sdev->host);
+       struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
+
+       if (lp->itlq_tbl)
+               sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
+       kfree(lp->cb_tags);
+       sym_mfree_dma(lp, sizeof(*lp), "LCB");
+}
+
 /*
  *  Linux entry point for info() function
  */
 static const char *sym53c8xx_info (struct Scsi_Host *host)
 {
-       return sym_driver_name();
+       return SYM_DRIVER_NAME;
 }
 
 
@@ -1178,44 +1063,47 @@ static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
 
                        case UC_SETSYNC:
                                if (!uc->data || uc->data >= 255) {
-                                       tp->tinfo.goal.options = 0;
-                                       tp->tinfo.goal.offset  = 0;
-                                       break;
-                               }
-                               if (uc->data <= 9 && np->minsync_dt) {
+                                       tp->tgoal.iu = tp->tgoal.dt =
+                                               tp->tgoal.qas = 0;
+                                       tp->tgoal.offset = 0;
+                               } else if (uc->data <= 9 && np->minsync_dt) {
                                        if (uc->data < np->minsync_dt)
                                                uc->data = np->minsync_dt;
-                                       tp->tinfo.goal.options = PPR_OPT_MASK;
-                                       tp->tinfo.goal.width   = 1;
-                                       tp->tinfo.goal.period = uc->data;
-                                       tp->tinfo.goal.offset = np->maxoffs_dt;
+                                       tp->tgoal.iu = tp->tgoal.dt =
+                                               tp->tgoal.qas = 1;
+                                       tp->tgoal.width = 1;
+                                       tp->tgoal.period = uc->data;
+                                       tp->tgoal.offset = np->maxoffs_dt;
                                } else {
                                        if (uc->data < np->minsync)
                                                uc->data = np->minsync;
-                                       tp->tinfo.goal.options = 0;
-                                       tp->tinfo.goal.period = uc->data;
-                                       tp->tinfo.goal.offset = np->maxoffs;
+                                       tp->tgoal.iu = tp->tgoal.dt =
+                                               tp->tgoal.qas = 0;
+                                       tp->tgoal.period = uc->data;
+                                       tp->tgoal.offset = np->maxoffs;
                                }
+                               tp->tgoal.check_nego = 1;
                                break;
                        case UC_SETWIDE:
-                               tp->tinfo.goal.width = uc->data ? 1 : 0;
+                               tp->tgoal.width = uc->data ? 1 : 0;
+                               tp->tgoal.check_nego = 1;
                                break;
                        case UC_SETTAGS:
                                for (l = 0; l < SYM_CONF_MAX_LUN; l++)
-                                       sym_tune_dev_queuing(np, t,l, uc->data);
+                                       sym_tune_dev_queuing(tp, l, uc->data);
                                break;
                        case UC_RESETDEV:
                                tp->to_reset = 1;
                                np->istat_sem = SEM;
-                               OUTB (nc_istat, SIGP|SEM);
+                               OUTB(np, nc_istat, SIGP|SEM);
                                break;
                        case UC_CLEARDEV:
                                for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
-                                       struct sym_lcb *lp = sym_lp(np, tp, l);
+                                       struct sym_lcb *lp = sym_lp(tp, l);
                                        if (lp) lp->to_clear = 1;
                                }
                                np->istat_sem = SEM;
-                               OUTB (nc_istat, SIGP|SEM);
+                               OUTB(np, nc_istat, SIGP|SEM);
                                break;
                        case UC_SETFLAG:
                                tp->usrflags = uc->data;
@@ -1226,8 +1114,6 @@ static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
        }
 }
 
-#define digit_to_bin(c)        ((c) - '0')
-
 static int skip_spaces(char *ptr, int len)
 {
        int cnt, c;
@@ -1239,17 +1125,10 @@ static int skip_spaces(char *ptr, int len)
 
 static int get_int_arg(char *ptr, int len, u_long *pv)
 {
-       int     cnt, c;
-       u_long  v;
-
-       for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && isdigit(c); cnt--) {
-               v = (v * 10) + digit_to_bin(c);
-       }
-
-       if (pv)
-               *pv = v;
+       char *end;
 
-       return (len - cnt);
+       *pv = simple_strtoul(ptr, &end, 10);
+       return (end - ptr);
 }
 
 static int is_keyword(char *ptr, int len, char *verb)
@@ -1260,15 +1139,14 @@ static int is_keyword(char *ptr, int len, char *verb)
                return verb_len;
        else
                return 0;
-
 }
 
-#define SKIP_SPACES(min_spaces)                                                \
-       if ((arg_len = skip_spaces(ptr, len)) < (min_spaces))           \
+#define SKIP_SPACES(ptr, len)                                          \
+       if ((arg_len = skip_spaces(ptr, len)) < 1)                      \
                return -EINVAL;                                         \
        ptr += arg_len; len -= arg_len;
 
-#define GET_INT_ARG(v)                                                 \
+#define GET_INT_ARG(ptr, len, v)                                       \
        if (!(arg_len = get_int_arg(ptr, len, &(v))))                   \
                return -EINVAL;                                         \
        ptr += arg_len; len -= arg_len;
@@ -1327,12 +1205,12 @@ printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
        case UC_SETFLAG:
        case UC_RESETDEV:
        case UC_CLEARDEV:
-               SKIP_SPACES(1);
+               SKIP_SPACES(ptr, len);
                if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
                        ptr += arg_len; len -= arg_len;
                        uc->target = ~0;
                } else {
-                       GET_INT_ARG(target);
+                       GET_INT_ARG(ptr, len, target);
                        uc->target = (1<<target);
 #ifdef DEBUG_PROC_INFO
 printk("sym_user_command: target=%ld\n", target);
@@ -1346,8 +1224,8 @@ printk("sym_user_command: target=%ld\n", target);
        case UC_SETSYNC:
        case UC_SETTAGS:
        case UC_SETWIDE:
-               SKIP_SPACES(1);
-               GET_INT_ARG(uc->data);
+               SKIP_SPACES(ptr, len);
+               GET_INT_ARG(ptr, len, uc->data);
 #ifdef DEBUG_PROC_INFO
 printk("sym_user_command: data=%ld\n", uc->data);
 #endif
@@ -1355,7 +1233,7 @@ printk("sym_user_command: data=%ld\n", uc->data);
 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
        case UC_SETDEBUG:
                while (len > 0) {
-                       SKIP_SPACES(1);
+                       SKIP_SPACES(ptr, len);
                        if      ((arg_len = is_keyword(ptr, len, "alloc")))
                                uc->data |= DEBUG_ALLOC;
                        else if ((arg_len = is_keyword(ptr, len, "phase")))
@@ -1389,7 +1267,7 @@ printk("sym_user_command: data=%ld\n", uc->data);
 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
        case UC_SETFLAG:
                while (len > 0) {
-                       SKIP_SPACES(1);
+                       SKIP_SPACES(ptr, len);
                        if      ((arg_len = is_keyword(ptr, len, "no_disc")))
                                uc->data &= ~SYM_DISC_ENABLED;
                        else
@@ -1499,15 +1377,9 @@ static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
                        char **start, off_t offset, int length, int func)
 {
-       struct host_data *host_data;
-       struct sym_hcb *np = NULL;
+       struct sym_hcb *np = sym_get_hcb(host);
        int retv;
 
-       host_data = (struct host_data *) host->hostdata;
-       np = host_data->ncb;
-       if (!np)
-               return -EINVAL;
-
        if (func) {
 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
                retv = sym_user_command(np, buffer, length);
@@ -1531,19 +1403,17 @@ static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
 /*
  *     Free controller resources.
  */
-static void sym_free_resources(struct sym_hcb *np)
+static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
 {
        /*
         *  Free O/S specific resources.
         */
        if (np->s.irq)
                free_irq(np->s.irq, np);
-#ifndef SYM_CONF_IOMAPPED
-       if (np->s.mmio_va)
-               iounmap(np->s.mmio_va);
-#endif
-       if (np->s.ram_va)
-               iounmap(np->s.ram_va);
+       if (np->s.ioaddr)
+               pci_iounmap(pdev, np->s.ioaddr);
+       if (np->s.ramaddr)
+               pci_iounmap(pdev, np->s.ramaddr);
        /*
         *  Free O/S independent resources.
         */
@@ -1559,7 +1429,7 @@ static int sym_setup_bus_dma_mask(struct sym_hcb *np)
 {
 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
 #if   SYM_CONF_DMA_ADDRESSING_MODE == 1
-#define        DMA_DAC_MASK    0x000000ffffffffffULL /* 40-bit */
+#define        DMA_DAC_MASK    DMA_40BIT_MASK
 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
 #define        DMA_DAC_MASK    DMA_64BIT_MASK
 #endif
@@ -1592,13 +1462,14 @@ static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
        struct host_data *host_data;
        struct sym_hcb *np = NULL;
        struct Scsi_Host *instance = NULL;
+       struct pci_dev *pdev = dev->pdev;
        unsigned long flags;
        struct sym_fw *fw;
 
        printk(KERN_INFO
                "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
                unit, dev->chip.name, dev->chip.revision_id,
-               pci_name(dev->pdev), IRQ_PRM(dev->s.irq));
+               pci_name(pdev), IRQ_PRM(pdev->irq));
 
        /*
         *  Get the firmware for this chip.
@@ -1621,22 +1492,22 @@ static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
         *  We keep track in the HCB of all the resources that 
         *  are to be released on error.
         */
-       np = __sym_calloc_dma(dev->pdev, sizeof(*np), "HCB");
+       np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
        if (!np)
                goto attach_failed;
-       np->s.device = dev->pdev;
-       np->bus_dmat = dev->pdev; /* Result in 1 DMA pool per HBA */
+       np->s.device = pdev;
+       np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
        host_data->ncb = np;
        np->s.host = instance;
 
-       pci_set_drvdata(dev->pdev, np);
+       pci_set_drvdata(pdev, np);
 
        /*
         *  Copy some useful infos to the HCB.
         */
        np->hcb_ba      = vtobus(np);
        np->verbose     = sym_driver_setup.verbose;
-       np->s.device    = dev->pdev;
+       np->s.device    = pdev;
        np->s.unit      = unit;
        np->device_id   = dev->chip.device_id;
        np->revision_id = dev->chip.revision_id;
@@ -1659,58 +1530,35 @@ static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
         *  Try to map the controller chip to
         *  virtual and physical memory.
         */
-       np->mmio_ba     = (u32)dev->s.base;
-       np->s.io_ws     = (np->features & FE_IO256)? 256 : 128;
-
-#ifndef SYM_CONF_IOMAPPED
-       np->s.mmio_va = ioremap(dev->s.base_c, np->s.io_ws);
-       if (!np->s.mmio_va) {
-               printf_err("%s: can't map PCI MMIO region\n", sym_name(np));
-               goto attach_failed;
-       } else if (sym_verbose > 1)
-               printf_info("%s: using memory mapped IO\n", sym_name(np));
-#endif /* !defined SYM_CONF_IOMAPPED */
-
-       np->s.io_port = dev->s.io_port;
+       np->mmio_ba = (u32)dev->mmio_base;
+       np->s.ioaddr    = dev->s.ioaddr;
+       np->s.ramaddr   = dev->s.ramaddr;
+       np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
 
        /*
         *  Map on-chip RAM if present and supported.
         */
        if (!(np->features & FE_RAM))
-               dev->s.base_2 = 0;
-       if (dev->s.base_2) {
-               np->ram_ba = (u32)dev->s.base_2;
-               if (np->features & FE_RAM8K)
-                       np->ram_ws = 8192;
-               else
-                       np->ram_ws = 4096;
-               np->s.ram_va = ioremap(dev->s.base_2_c, np->ram_ws);
-               if (!np->s.ram_va) {
-                       printf_err("%s: can't map PCI MEMORY region\n",
-                               sym_name(np));
-                       goto attach_failed;
-               }
+               dev->ram_base = 0;
+       if (dev->ram_base) {
+               np->ram_ba = (u32)dev->ram_base;
+               np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
        }
 
-       /*
-        *  Perform O/S independent stuff.
-        */
-       if (sym_hcb_attach(np, fw, dev->nvram))
+       if (sym_hcb_attach(instance, fw, dev->nvram))
                goto attach_failed;
 
-
        /*
         *  Install the interrupt handler.
         *  If we synchonize the C code with SCRIPTS on interrupt, 
-        *  we donnot want to share the INTR line at all.
+        *  we do not want to share the INTR line at all.
         */
-       if (request_irq(dev->s.irq, sym53c8xx_intr, SA_SHIRQ,
-                       NAME53C8XX, np)) {
+       if (request_irq(pdev->irq, sym53c8xx_intr, SA_SHIRQ, NAME53C8XX, np)) {
                printf_err("%s: request irq %d failure\n",
-                       sym_name(np), dev->s.irq);
+                       sym_name(np), pdev->irq);
                goto attach_failed;
        }
-       np->s.irq = dev->s.irq;
+       np->s.irq = pdev->irq;
 
        /*
         *  After SCSI devices have been opened, we cannot
@@ -1742,14 +1590,7 @@ static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
        instance->this_id       = np->myaddr;
        instance->max_id        = np->maxwide ? 16 : 8;
        instance->max_lun       = SYM_CONF_MAX_LUN;
-#ifndef SYM_CONF_IOMAPPED
-       instance->base          = (unsigned long) np->s.mmio_va;
-#endif
-       instance->irq           = np->s.irq;
-       instance->unique_id     = np->s.io_port;
-       instance->io_port       = np->s.io_port;
-       instance->n_io_port     = np->s.io_ws;
-       instance->dma_channel   = 0;
+       instance->unique_id     = pci_resource_start(pdev, 0);
        instance->cmd_per_lun   = SYM_CONF_MAX_TAG;
        instance->can_queue     = (SYM_CONF_MAX_START-2);
        instance->sg_tablesize  = SYM_CONF_MAX_SG;
@@ -1770,7 +1611,7 @@ static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
                return NULL;
        printf_info("%s: giving up ...\n", sym_name(np));
        if (np)
-               sym_free_resources(np);
+               sym_free_resources(np, pdev);
        scsi_host_put(instance);
 
        return NULL;
@@ -1787,23 +1628,7 @@ static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *n
        devp->device_id = devp->chip.device_id;
        nvp->type = 0;
 
-       /*
-        *  Get access to chip IO registers
-        */
-#ifndef SYM_CONF_IOMAPPED
-       devp->s.mmio_va = ioremap(devp->s.base_c, 128);
-       if (!devp->s.mmio_va)
-               return;
-#endif
-
        sym_read_nvram(devp, nvp);
-
-       /*
-        *  Release access to chip IO registers
-        */
-#ifndef SYM_CONF_IOMAPPED
-       iounmap(devp->s.mmio_va);
-#endif
 }
 #else
 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
@@ -1813,15 +1638,15 @@ static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
 
 static int __devinit sym_check_supported(struct sym_device *device)
 {
-       struct sym_pci_chip *chip;
+       struct sym_chip *chip;
        struct pci_dev *pdev = device->pdev;
        u_char revision;
-       unsigned long io_port = device->s.io_port;
-       unsigned long base = device->s.base;
+       unsigned long io_port = pci_resource_start(pdev, 0);
        int i;
 
        /*
         *  If user excluded this chip, do not initialize it.
+        *  I hate this code so much.  Must kill it.
         */
        if (io_port) {
                for (i = 0 ; i < 8 ; i++) {
@@ -1830,33 +1655,15 @@ static int __devinit sym_check_supported(struct sym_device *device)
                }
        }
 
-       /*
-        *  Check if the chip has been assigned resources we need.
-        *  XXX: can this still happen with Linux 2.6's PCI layer?
-        */
-#ifdef SYM_CONF_IOMAPPED
-       if (!io_port) {
-               printf_info("%s: IO base address disabled.\n",
-                           sym_name(device));
-               return -ENODEV;
-       }
-#else
-       if (!base) {
-               printf_info("%s: MMIO base address disabled.\n",
-                           sym_name(device));
-               return -ENODEV;
-       }
-#endif
-
        /*
         * Check if the chip is supported.  Then copy the chip description
         * to our device structure so we can make it match the actual device
         * and options.
         */
        pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
-       chip = sym_lookup_pci_chip_table(pdev->device, revision);
+       chip = sym_lookup_chip_table(pdev->device, revision);
        if (!chip) {
-               printf_info("%s: device not supported\n", sym_name(device));
+               dev_info(&pdev->dev, "device not supported\n");
                return -ENODEV;
        }
        memcpy(&device->chip, chip, sizeof(device->chip));
@@ -1871,11 +1678,9 @@ static int __devinit sym_check_supported(struct sym_device *device)
  */
 static int __devinit sym_check_raid(struct sym_device *device)
 {
-       unsigned long base_2_c = device->s.base_2_c;
        unsigned int ram_size, ram_val;
-       void __iomem *ram_ptr;
 
-       if (!base_2_c)
+       if (!device->s.ramaddr)
                return 0;
 
        if (device->chip.features & FE_RAM8K)
@@ -1883,23 +1688,18 @@ static int __devinit sym_check_raid(struct sym_device *device)
        else
                ram_size = 4096;
 
-       ram_ptr = ioremap(base_2_c, ram_size);
-       if (!ram_ptr)
-               return 0;
-
-       ram_val = readl(ram_ptr + ram_size - 16);
-       iounmap(ram_ptr);
+       ram_val = readl(device->s.ramaddr + ram_size - 16);
        if (ram_val != 0x52414944)
                return 0;
 
-       printf_info("%s: not initializing, driven by RAID controller.\n",
-                   sym_name(device));
+       dev_info(&device->pdev->dev,
+                       "not initializing, driven by RAID controller.\n");
        return -ENODEV;
 }
 
 static int __devinit sym_set_workarounds(struct sym_device *device)
 {
-       struct sym_pci_chip *chip = &device->chip;
+       struct sym_chip *chip = &device->chip;
        struct pci_dev *pdev = device->pdev;
        u_short status_reg;
 
@@ -1952,26 +1752,35 @@ static int __devinit sym_set_workarounds(struct sym_device *device)
 static void __devinit
 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
 {
-       unsigned long base, base_2; 
-       int i;
+       int i = 2;
+       struct pci_bus_region bus_addr;
 
        device->host_id = SYM_SETUP_HOST_ID;
        device->pdev = pdev;
-       device->s.irq = pdev->irq;
 
-       /* Choose some short name for this device */
-       sprintf(device->s.inst_name, "sym.%d.%d.%d", pdev->bus->number,
-                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+       pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
+       device->mmio_base = bus_addr.start;
 
-       device->s.io_port = pdev->resource[0].start;
-
-       device->s.base_c = pdev->resource[1].start;
-       i = pci_get_base_address(pdev, 1, &base);
-       device->s.base = base & PCI_BASE_ADDRESS_MEM_MASK;
-
-       device->s.base_2_c = pdev->resource[i].start;
-       pci_get_base_address(pdev, i, &base_2);
-       device->s.base_2 = base_2 & PCI_BASE_ADDRESS_MEM_MASK;
+       /*
+        * If the BAR is 64-bit, resource 2 will be occupied by the
+        * upper 32 bits
+        */
+       if (!pdev->resource[i].flags)
+               i++;
+       pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
+       device->ram_base = bus_addr.start;
+
+#ifdef CONFIG_SCSI_SYM53C8XX_MMIO
+       if (device->mmio_base)
+               device->s.ioaddr = pci_iomap(pdev, 1,
+                                               pci_resource_len(pdev, 1));
+#endif
+       if (!device->s.ioaddr)
+               device->s.ioaddr = pci_iomap(pdev, 0,
+                                               pci_resource_len(pdev, 0));
+       if (device->ram_base)
+               device->s.ramaddr = pci_iomap(pdev, i,
+                                               pci_resource_len(pdev, i));
 }
 
 /*
@@ -2027,7 +1836,7 @@ static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  *  Detach the host.
  *  We have to free resources and halt the NCR chip.
  */
-static int sym_detach(struct sym_hcb *np)
+static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
 {
        printk("%s: detaching ...\n", sym_name(np));
 
@@ -2039,11 +1848,12 @@ static int sym_detach(struct sym_hcb *np)
         * so, since we may not be safe if interrupts occur.
         */
        printk("%s: resetting chip\n", sym_name(np));
-       OUTB (nc_istat, SRST);
-       UDELAY (10);
-       OUTB (nc_istat, 0);
+       OUTB(np, nc_istat, SRST);
+       INB(np, nc_mbox1);
+       udelay(10);
+       OUTB(np, nc_istat, 0);
 
-       sym_free_resources(np);
+       sym_free_resources(np, pdev);
 
        return 1;
 }
@@ -2056,13 +1866,16 @@ static struct scsi_host_template sym2_template = {
        .name                   = "sym53c8xx",
        .info                   = sym53c8xx_info, 
        .queuecommand           = sym53c8xx_queue_command,
+       .slave_alloc            = sym53c8xx_slave_alloc,
        .slave_configure        = sym53c8xx_slave_configure,
+       .slave_destroy          = sym53c8xx_slave_destroy,
        .eh_abort_handler       = sym53c8xx_eh_abort_handler,
        .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
        .eh_bus_reset_handler   = sym53c8xx_eh_bus_reset_handler,
        .eh_host_reset_handler  = sym53c8xx_eh_host_reset_handler,
        .this_id                = 7,
-       .use_clustering         = DISABLE_CLUSTERING,
+       .use_clustering         = ENABLE_CLUSTERING,
+       .max_sectors            = 0xFFFF,
 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
        .proc_info              = sym53c8xx_proc_info,
        .proc_name              = NAME53C8XX,
@@ -2116,7 +1929,7 @@ static int __devinit sym2_probe(struct pci_dev *pdev,
        return 0;
 
  detach:
-       sym_detach(pci_get_drvdata(pdev));
+       sym_detach(pci_get_drvdata(pdev), pdev);
  free:
        pci_release_regions(pdev);
  disable:
@@ -2133,7 +1946,7 @@ static void __devexit sym2_remove(struct pci_dev *pdev)
        scsi_remove_host(host);
        scsi_host_put(host);
 
-       sym_detach(np);
+       sym_detach(np, pdev);
 
        pci_release_regions(pdev);
        pci_disable_device(pdev);
@@ -2143,12 +1956,12 @@ static void __devexit sym2_remove(struct pci_dev *pdev)
 
 static void sym2_get_signalling(struct Scsi_Host *shost)
 {
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        enum spi_signal_type type;
 
        switch (np->scsi_mode) {
        case SMODE_SE:
-               type =  SPI_SIGNAL_SE;
+               type = SPI_SIGNAL_SE;
                break;
        case SMODE_LVD:
                type = SPI_SIGNAL_LVD;
@@ -2163,154 +1976,103 @@ static void sym2_get_signalling(struct Scsi_Host *shost)
        spi_signalling(shost) = type;
 }
 
-static void sym2_get_offset(struct scsi_target *starget)
-{
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
-       struct sym_tcb *tp = &np->target[starget->id];
-
-       spi_offset(starget) = tp->tinfo.curr.offset;
-}
-
 static void sym2_set_offset(struct scsi_target *starget, int offset)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        struct sym_tcb *tp = &np->target[starget->id];
 
-       tp->tinfo.goal.offset = offset;
-}
-
-
-static void sym2_get_period(struct scsi_target *starget)
-{
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
-       struct sym_tcb *tp = &np->target[starget->id];
-
-       spi_period(starget) = tp->tinfo.curr.period;
+       tp->tgoal.offset = offset;
+       tp->tgoal.check_nego = 1;
 }
 
 static void sym2_set_period(struct scsi_target *starget, int period)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        struct sym_tcb *tp = &np->target[starget->id];
 
-       /* have to have DT for these transfers */
-       if (period <= np->minsync)
-               tp->tinfo.goal.options |= PPR_OPT_DT;
+       /* have to have DT for these transfers, but DT will also
+        * set width, so check that this is allowed */
+       if (period <= np->minsync && spi_width(starget))
+               tp->tgoal.dt = 1;
 
-       tp->tinfo.goal.period = period;
-}
-
-static void sym2_get_width(struct scsi_target *starget)
-{
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
-       struct sym_tcb *tp = &np->target[starget->id];
-
-       spi_width(starget) = tp->tinfo.curr.width ? 1 : 0;
+       tp->tgoal.period = period;
+       tp->tgoal.check_nego = 1;
 }
 
 static void sym2_set_width(struct scsi_target *starget, int width)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        struct sym_tcb *tp = &np->target[starget->id];
 
        /* It is illegal to have DT set on narrow transfers.  If DT is
         * clear, we must also clear IU and QAS.  */
        if (width == 0)
-               tp->tinfo.goal.options &= ~PPR_OPT_MASK;
+               tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
 
-       tp->tinfo.goal.width = width;
-}
-
-static void sym2_get_dt(struct scsi_target *starget)
-{
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
-       struct sym_tcb *tp = &np->target[starget->id];
-
-       spi_dt(starget) = (tp->tinfo.curr.options & PPR_OPT_DT) ? 1 : 0;
+       tp->tgoal.width = width;
+       tp->tgoal.check_nego = 1;
 }
 
 static void sym2_set_dt(struct scsi_target *starget, int dt)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        struct sym_tcb *tp = &np->target[starget->id];
 
        /* We must clear QAS and IU if DT is clear */
        if (dt)
-               tp->tinfo.goal.options |= PPR_OPT_DT;
+               tp->tgoal.dt = 1;
        else
-               tp->tinfo.goal.options &= ~PPR_OPT_MASK;
-}
-
-static void sym2_get_iu(struct scsi_target *starget)
-{
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
-       struct sym_tcb *tp = &np->target[starget->id];
-
-       spi_iu(starget) = (tp->tinfo.curr.options & PPR_OPT_IU) ? 1 : 0;
+               tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
+       tp->tgoal.check_nego = 1;
 }
 
+#if 0
 static void sym2_set_iu(struct scsi_target *starget, int iu)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        struct sym_tcb *tp = &np->target[starget->id];
 
        if (iu)
-               tp->tinfo.goal.options |= PPR_OPT_IU | PPR_OPT_DT;
+               tp->tgoal.iu = tp->tgoal.dt = 1;
        else
-               tp->tinfo.goal.options &= ~PPR_OPT_IU;
-}
-
-static void sym2_get_qas(struct scsi_target *starget)
-{
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
-       struct sym_tcb *tp = &np->target[starget->id];
-
-       spi_qas(starget) = (tp->tinfo.curr.options & PPR_OPT_QAS) ? 1 : 0;
+               tp->tgoal.iu = 0;
+       tp->tgoal.check_nego = 1;
 }
 
 static void sym2_set_qas(struct scsi_target *starget, int qas)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       struct sym_hcb *np = ((struct host_data *)shost->hostdata)->ncb;
+       struct sym_hcb *np = sym_get_hcb(shost);
        struct sym_tcb *tp = &np->target[starget->id];
 
        if (qas)
-               tp->tinfo.goal.options |= PPR_OPT_QAS | PPR_OPT_DT;
+               tp->tgoal.dt = tp->tgoal.qas = 1;
        else
-               tp->tinfo.goal.options &= ~PPR_OPT_QAS;
+               tp->tgoal.qas = 0;
+       tp->tgoal.check_nego = 1;
 }
-
+#endif
 
 static struct spi_function_template sym2_transport_functions = {
        .set_offset     = sym2_set_offset,
-       .get_offset     = sym2_get_offset,
        .show_offset    = 1,
        .set_period     = sym2_set_period,
-       .get_period     = sym2_get_period,
        .show_period    = 1,
        .set_width      = sym2_set_width,
-       .get_width      = sym2_get_width,
        .show_width     = 1,
-       .get_dt         = sym2_get_dt,
        .set_dt         = sym2_set_dt,
        .show_dt        = 1,
-       .get_iu         = sym2_get_iu,
+#if 0
        .set_iu         = sym2_set_iu,
        .show_iu        = 1,
-       .get_qas        = sym2_get_qas,
        .set_qas        = sym2_set_qas,
        .show_qas       = 1,
+#endif
        .get_signalling = sym2_get_signalling,
 };
 
@@ -2370,7 +2132,7 @@ static int __init sym2_init(void)
        if (!sym2_transport_template)
                return -ENODEV;
 
-       error = pci_module_init(&sym2_driver);
+       error = pci_register_driver(&sym2_driver);
        if (error)
                spi_release_transport(sym2_transport_template);
        return error;