staging: dst: fix coding style
[safe/jmp/linux-2.6] / drivers / staging / sep / sep_driver.c
index fe46c1b..67c7d2c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *
- *  sep_main_mod.c - Security Processor Driver main group of functions
+ *  sep_driver.c - Security Processor Driver main group of functions
  *
  *  Copyright(c) 2009 Intel Corporation. All rights reserved.
  *  Copyright(c) 2009 Discretix. All rights reserved.
@@ -38,6 +38,7 @@
 #include <linux/mm.h>
 #include <linux/poll.h>
 #include <linux/wait.h>
+#include <linux/sched.h>
 #include <linux/pci.h>
 #include <linux/firmware.h>
 #include <asm/ioctl.h>
@@ -49,7 +50,6 @@
 #include "sep_driver_hw_defs.h"
 #include "sep_driver_config.h"
 #include "sep_driver_api.h"
-#include "sep_driver_ext_api.h"
 #include "sep_dev.h"
 
 #if SEP_DRIVER_ARM_DEBUG_MODE
 
 /* 2M size */
 
-static void sep_load_rom_code(void)
+static void sep_load_rom_code(struct sep_device *sep)
 {
        /* Index variables */
        unsigned long i, k, j;
-       unsigned long regVal;
-       unsigned long Error;
-       unsigned long warning;
+       u32 reg;
+       u32 error;
+       u32 warning;
 
        /* Loading ROM from SEP_ROM_image.h file */
        k = sizeof(CRYS_SEP_ROM);
@@ -83,15 +83,15 @@ static void sep_load_rom_code(void)
        edbg("SEP Driver: DX_CC_TST_SepRomLoader start\n");
 
        edbg("SEP Driver: k is %lu\n", k);
-       edbg("SEP Driver: sep_dev->reg_base_address is %p\n", sep_dev->reg_base_address);
+       edbg("SEP Driver: sep->reg_addr is %p\n", sep->reg_addr);
        edbg("SEP Driver: CRYS_SEP_ROM_start_address_offset is %p\n", CRYS_SEP_ROM_start_address_offset);
 
        for (i = 0; i < 4; i++) {
                /* write bank */
-               sep_write_reg(sep_dev, SEP_ROM_BANK_register_offset, i);
+               sep_write_reg(sep, SEP_ROM_BANK_register_offset, i);
 
                for (j = 0; j < CRYS_SEP_ROM_length / 4; j++) {
-                       sep_write_reg(sep_dev, CRYS_SEP_ROM_start_address_offset + 4 * j, CRYS_SEP_ROM[i * 0x1000 + j]);
+                       sep_write_reg(sep, CRYS_SEP_ROM_start_address_offset + 4 * j, CRYS_SEP_ROM[i * 0x1000 + j]);
 
                        k = k - 4;
 
@@ -103,53 +103,40 @@ static void sep_load_rom_code(void)
        }
 
        /* reset the SEP */
-       sep_write_reg(sep_dev, HW_HOST_SEP_SW_RST_REG_ADDR, 0x1);
+       sep_write_reg(sep, HW_HOST_SEP_SW_RST_REG_ADDR, 0x1);
 
        /* poll for SEP ROM boot finish */
-       do {
-               retVal = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
-       } while (!regVal);
+       do
+               reg = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
+       while (!reg);
 
        edbg("SEP Driver: ROM polling ended\n");
 
-       switch (regVal) {
+       switch (reg) {
        case 0x1:
                /* fatal error - read erro status from GPRO */
-               Error = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
+               error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
                edbg("SEP Driver: ROM polling case 1\n");
                break;
-       case 0x2:
-               /* Boot First Phase ended  */
-               warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
-               edbg("SEP Driver: ROM polling case 2\n");
-               break;
        case 0x4:
                /* Cold boot ended successfully  */
-               warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
-               edbg("SEP Driver: ROM polling case 4\n");
-               Error = 0;
-               break;
        case 0x8:
                /* Warmboot ended successfully */
-               warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
-               edbg("SEP Driver: ROM polling case 8\n");
-               Error = 0;
-               break;
        case 0x10:
                /* ColdWarm boot ended successfully */
-               warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
-               edbg("SEP Driver: ROM polling case 16\n");
-               Error = 0;
-               break;
+               error = 0;
+       case 0x2:
+               /* Boot First Phase ended  */
+               warning = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
        case 0x20:
-               edbg("SEP Driver: ROM polling case 32\n");
+               edbg("SEP Driver: ROM polling case %d\n", reg);
                break;
        }
 
 }
 
 #else
-static void sep_load_rom_code(void) { }
+static void sep_load_rom_code(struct sep_device *sep) { }
 #endif                         /* SEP_DRIVER_ARM_DEBUG_MODE */
 
 
@@ -158,7 +145,6 @@ static void sep_load_rom_code(void) { }
        DEFINES
 -----------------------------------------*/
 
-#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
 #define BASE_ADDRESS_FOR_SYSTEM 0xfffc0000
 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
 
@@ -167,8 +153,9 @@ static void sep_load_rom_code(void) { }
 --------------------------------------------*/
 
 /* debug messages level */
-INT_MODULE_PARM(sepDebug, 0x0);
-MODULE_PARM_DESC(sepDebug, "Flag to enable SEP debug messages");
+static int debug;
+module_param(debug, int , 0);
+MODULE_PARM_DESC(debug, "Flag to enable SEP debug messages");
 
 /* Keep this a single static object for now to keep the conversion easy */
 
@@ -182,235 +169,210 @@ static DEFINE_MUTEX(sep_mutex);
 
 
 /* wait queue head (event) of the driver */
-static DECLARE_WAIT_QUEUE_HEAD(g_sep_event);
+static DECLARE_WAIT_QUEUE_HEAD(sep_event);
 
-/*
-  This functions copies the cache and resident from their source location into
-  destination memory, which is external to Linux VM and is given as
-   physical address
-*/
-static int sep_copy_cache_resident_to_area(unsigned long src_cache_addr, unsigned long cache_size_in_bytes, unsigned long src_resident_addr, unsigned long resident_size_in_bytes, unsigned long *dst_new_cache_addr_ptr, unsigned long *dst_new_resident_addr_ptr)
+/**
+ *     sep_load_firmware       -       copy firmware cache/resident
+ *     @sep: device we are loading
+ *
+ *     This functions copies the cache and resident from their source
+ *     location into destination shared memory.
+ */
+
+static int sep_load_firmware(struct sep_device *sep)
 {
-       unsigned long resident_addr;
-       unsigned long cache_addr;
        const struct firmware *fw;
-
        char *cache_name = "cache.image.bin";
        char *res_name = "resident.image.bin";
-
-       /* error */
        int error;
 
-       /*--------------------------------
-           CODE
-       -------------------------------------*/
-       error = 0;
-
-       edbg("SEP Driver:rar_virtual is %p\n", sep_dev->rar_virtual_address);
-       edbg("SEP Driver:rar_physical is %08lx\n", sep_dev->rar_physical_address);
-
-       sep_dev->rar_region_addr = (unsigned long) sep_dev->rar_virtual_address;
-
-       sep_dev->cache_physical_address = sep_dev->rar_physical_address;
-       sep_dev->cache_virtual_address = sep_dev->rar_virtual_address;
+       edbg("SEP Driver:rar_virtual is %p\n", sep->rar_addr);
+       edbg("SEP Driver:rar_bus is %08llx\n", (unsigned long long)sep->rar_bus);
 
        /* load cache */
-       error = request_firmware(&fw, cache_name, &sep_dev->sep_pci_dev_ptr->dev);
+       error = request_firmware(&fw, cache_name, &sep->pdev->dev);
        if (error) {
                edbg("SEP Driver:cant request cache fw\n");
-               goto end_function;
+               return error;
        }
+       edbg("SEP Driver:cache %08Zx@%p\n", fw->size, (void *) fw->data);
 
-       edbg("SEP Driver:cache data loc is %p\n", (void *) fw->data);
-       edbg("SEP Driver:cache data size is %08Zx\n", fw->size);
-
-       memcpy((void *) sep_dev->cache_virtual_address, (void *) fw->data, fw->size);
-
-       sep_dev->cache_size = fw->size;
-
-       cache_addr = (unsigned long) sep_dev->cache_virtual_address;
-
+       memcpy(sep->rar_addr, (void *)fw->data, fw->size);
+       sep->cache_size = fw->size;
        release_firmware(fw);
 
-       sep_dev->resident_physical_address = sep_dev->cache_physical_address + sep_dev->cache_size;
-       sep_dev->resident_virtual_address = sep_dev->cache_virtual_address + sep_dev->cache_size;
+       sep->resident_bus = sep->rar_bus + sep->cache_size;
+       sep->resident_addr = sep->rar_addr + sep->cache_size;
 
        /* load resident */
-       error = request_firmware(&fw, res_name, &sep_dev->sep_pci_dev_ptr->dev);
+       error = request_firmware(&fw, res_name, &sep->pdev->dev);
        if (error) {
                edbg("SEP Driver:cant request res fw\n");
-               goto end_function;
+               return error;
        }
+       edbg("sep: res %08Zx@%p\n", fw->size, (void *)fw->data);
 
-       edbg("SEP Driver:res data loc is %p\n", (void *) fw->data);
-       edbg("SEP Driver:res data size is %08Zx\n", fw->size);
-
-       memcpy((void *) sep_dev->resident_virtual_address, (void *) fw->data, fw->size);
-
-       sep_dev->resident_size = fw->size;
-
+       memcpy(sep->resident_addr, (void *) fw->data, fw->size);
+       sep->resident_size = fw->size;
        release_firmware(fw);
 
-       resident_addr = (unsigned long) sep_dev->resident_virtual_address;
-
-       edbg("SEP Driver:resident_addr (physical )is %08lx\n", sep_dev->resident_physical_address);
-       edbg("SEP Driver:cache_addr (physical) is %08lx\n", sep_dev->cache_physical_address);
-
-       edbg("SEP Driver:resident_addr (logical )is %08lx\n", resident_addr);
-       edbg("SEP Driver:cache_addr (logical) is %08lx\n", cache_addr);
-
-       edbg("SEP Driver:resident_size is %08lx\n", sep_dev->resident_size);
-       edbg("SEP Driver:cache_size is %08lx\n", sep_dev->cache_size);
-
-
-
-       /* physical addresses */
-       *dst_new_cache_addr_ptr = sep_dev->cache_physical_address;
-       *dst_new_resident_addr_ptr = sep_dev->resident_physical_address;
-end_function:
-       return error;
+       edbg("sep: resident v %p b %08llx cache v %p b %08llx\n",
+               sep->resident_addr, (unsigned long long)sep->resident_bus,
+               sep->rar_addr, (unsigned long long)sep->rar_bus);
+       return 0;
 }
 
-/*
-  This functions maps and allocates the
-  shared area on the  external RAM (device)
-  The input is shared_area_size - the size of the memory to
-  allocate. The outputs
-  are kernel_shared_area_addr_ptr - the kerenl
-  address of the mapped and allocated
-  shared area, and phys_shared_area_addr_ptr
-  - the physical address of the shared area
-*/
-static int sep_map_and_alloc_shared_area(unsigned long shared_area_size, unsigned long *kernel_shared_area_addr_ptr, unsigned long *phys_shared_area_addr_ptr)
+/**
+ *     sep_map_and_alloc_shared_area   -       allocate shared block
+ *     @sep: security processor
+ *     @size: size of shared area
+ *
+ *     Allocate a shared buffer in host memory that can be used by both the
+ *     kernel and also the hardware interface via DMA.
+ */
+
+static int sep_map_and_alloc_shared_area(struct sep_device *sep,
+                                                       unsigned long size)
 {
-       // shared_virtual_address = ioremap_nocache(0xda00000,shared_area_size);
-       sep_dev->shared_virtual_address = kmalloc(shared_area_size, GFP_KERNEL);
-       if (!sep_dev->shared_virtual_address) {
-               edbg("sep_driver:shared memory kmalloc failed\n");
-               return -1;
-       }
-       /* FIXME */
-       sep_dev->shared_physical_address = __pa(sep_dev->shared_virtual_address);
-       /* shared_physical_address = 0xda00000; */
-       *kernel_shared_area_addr_ptr = (unsigned long) sep_dev->shared_virtual_address;
-       /* set the physical address of the shared area */
-       *phys_shared_area_addr_ptr = sep_dev->shared_physical_address;
-       edbg("SEP Driver:shared_virtual_address is %p\n", sep_dev->shared_virtual_address);
-       edbg("SEP Driver:shared_region_size is %08lx\n", shared_area_size);
-       edbg("SEP Driver:shared_physical_addr is %08lx\n", *phys_shared_area_addr_ptr);
+       /* shared_addr = ioremap_nocache(0xda00000,shared_area_size); */
+       sep->shared_addr = dma_alloc_coherent(&sep->pdev->dev, size,
+                                       &sep->shared_bus, GFP_KERNEL);
 
+       if (!sep->shared_addr) {
+               edbg("sep_driver :shared memory dma_alloc_coherent failed\n");
+               return -ENOMEM;
+       }
+       /* set the bus address of the shared area */
+       edbg("sep: shared_addr %ld bytes @%p (bus %08llx)\n",
+               size, sep->shared_addr, (unsigned long long)sep->shared_bus);
        return 0;
 }
 
-/*
-  This functions unmaps and deallocates the shared area
-  on the  external RAM (device)
-  The input is shared_area_size - the size of the memory to deallocate,kernel_
-  shared_area_addr_ptr - the kernel address of the mapped and allocated
-  shared area,phys_shared_area_addr_ptr - the physical address of
-  the shared area
-*/
-static void sep_unmap_and_free_shared_area(unsigned long shared_area_size, unsigned long kernel_shared_area_addr, unsigned long phys_shared_area_addr)
+/**
+ *     sep_unmap_and_free_shared_area  -       free shared block
+ *     @sep: security processor
+ *
+ *     Free the shared area allocated to the security processor. The
+ *     processor must have finished with this and any final posted
+ *     writes cleared before we do so.
+ */
+static void sep_unmap_and_free_shared_area(struct sep_device *sep, int size)
 {
-       kfree((void *) kernel_shared_area_addr);
+       dma_free_coherent(&sep->pdev->dev, size,
+                               sep->shared_addr, sep->shared_bus);
 }
 
-/*
-  This functions returns the physical address inside shared area according
-  to the virtual address. It can be either on the externa RAM device
-  (ioremapped), or on the system RAM
-  This implementation is for the external RAM
-*/
-static unsigned long sep_shared_area_virt_to_phys(unsigned long virt_address)
-{
-       edbg("SEP Driver:sh virt to phys v %08lx\n", virt_address);
-       edbg("SEP Driver:sh virt to phys p %08lx\n", sep_dev->shared_physical_address + (virt_address - (unsigned long) sep_dev->shared_virtual_address));
-
-       return (unsigned long) sep_dev->shared_physical_address + (virt_address - (unsigned long) sep_dev->shared_virtual_address);
-}
+/**
+ *     sep_shared_virt_to_bus  -       convert bus/virt addresses
+ *
+ *     Returns the bus address inside the shared area according
+ *     to the virtual address.
+ */
 
-/*
-  This functions returns the virtual address inside shared area
-  according to the physical address. It can be either on the
-  externa RAM device (ioremapped), or on the system RAM This implementation
-  is for the external RAM
-*/
-static unsigned long sep_shared_area_phys_to_virt(unsigned long phys_address)
+static dma_addr_t sep_shared_virt_to_bus(struct sep_device *sep,
+                                               void *virt_address)
 {
-       return (unsigned long) sep_dev->shared_virtual_address + (phys_address - sep_dev->shared_physical_address);
+       dma_addr_t pa = sep->shared_bus + (virt_address - sep->shared_addr);
+       edbg("sep: virt to bus b %08llx v %p\n",
+               (unsigned long long)pa, virt_address);
+       return pa;
 }
 
+/**
+ *     sep_shared_bus_to_virt  -       convert bus/virt addresses
+ *
+ *     Returns virtual address inside the shared area according
+ *     to the bus address.
+ */
 
-/*----------------------------------------------------------------------
-  open function of the character driver - must only lock the mutex
-       must also release the memory data pool allocations
-------------------------------------------------------------------------*/
-static int sep_open(struct inode *inode_ptr, struct file *file_ptr)
+static void *sep_shared_bus_to_virt(struct sep_device *sep,
+                                               dma_addr_t bus_address)
 {
-       int error;
+       return sep->shared_addr + (bus_address - sep->shared_bus);
+}
 
-       dbg("SEP Driver:--------> open start\n");
 
-       error = 0;
+/**
+ *     sep_try_open            -       attempt to open a SEP device
+ *     @sep: device to attempt to open
+ *
+ *     Atomically attempt to get ownership of a SEP device.
+ *     Returns 1 if the device was opened, 0 on failure.
+ */
 
-       /* check the blocking mode */
-       if (sep_dev->block_mode_flag)
-               /* lock mutex */
-               mutex_lock(&sep_mutex);
-       else
-               error = mutex_trylock(&sep_mutex);
+static int sep_try_open(struct sep_device *sep)
+{
+       if (!test_and_set_bit(0, &sep->in_use))
+               return 1;
+       return 0;
+}
 
-       /* check the error */
-       if (error) {
-               edbg("SEP Driver: down_interruptible failed\n");
+/**
+ *     sep_open                -       device open method
+ *     @inode: inode of sep device
+ *     @filp: file handle to sep device
+ *
+ *     Open method for the SEP device. Called when userspace opens
+ *     the SEP device node. Must also release the memory data pool
+ *     allocations.
+ *
+ *     Returns zero on success otherwise an error code.
+ */
 
-               goto end_function;
-       }
+static int sep_open(struct inode *inode, struct file *filp)
+{
+       if (sep_dev == NULL)
+               return -ENODEV;
 
+       /* check the blocking mode */
+       if (filp->f_flags & O_NDELAY) {
+               if (sep_try_open(sep_dev) == 0)
+                       return -EAGAIN;
+       } else
+               if (wait_event_interruptible(sep_event, sep_try_open(sep_dev)) < 0)
+                       return -EINTR;
+
+       /* Bind to the device, we only have one which makes it easy */
+       filp->private_data = sep_dev;
        /* release data pool allocations */
        sep_dev->data_pool_bytes_allocated = 0;
-
-end_function:
-       dbg("SEP Driver:<-------- open end\n");
-       return error;
+       return 0;
 }
 
 
+/**
+ *     sep_release             -       close a SEP device
+ *     @inode: inode of SEP device
+ *     @filp: file handle being closed
+ *
+ *     Called on the final close of a SEP device. As the open protects against
+ *     multiple simultaenous opens that means this method is called when the
+ *     final reference to the open handle is dropped.
+ */
 
-
-/*------------------------------------------------------------
-       release function
--------------------------------------------------------------*/
-static int sep_release(struct inode *inode_ptr, struct file *file_ptr)
+static int sep_release(struct inode *inode, struct file *filp)
 {
-       dbg("----------->SEP Driver: sep_release start\n");
-
+       struct sep_device *sep =  filp->private_data;
 #if 0                          /*!SEP_DRIVER_POLLING_MODE */
        /* close IMR */
-       sep_write_reg(sep_dev, HW_HOST_IMR_REG_ADDR, 0x7FFF);
-
+       sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, 0x7FFF);
        /* release IRQ line */
-       free_irq(SEP_DIRVER_IRQ_NUM, &sep_dev->reg_base_address);
+       free_irq(SEP_DIRVER_IRQ_NUM, sep);
 
 #endif
-
-       /* unlock the sep mutex */
-       mutex_unlock(&sep_mutex);
-
-       dbg("SEP Driver:<-------- sep_release end\n");
-
+       /* Ensure any blocked open progresses */
+       clear_bit(0, &sep->in_use);
+       wake_up(&sep_event);
        return 0;
 }
 
-
-
-
 /*---------------------------------------------------------------
   map function - this functions maps the message shared area
 -----------------------------------------------------------------*/
 static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
 {
-       unsigned long phys_addr;
+       dma_addr_t bus_addr;
+       struct sep_device *sep = filp->private_data;
 
        dbg("-------->SEP Driver: mmap start\n");
 
@@ -425,14 +387,14 @@ static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
                return -EAGAIN;
        }
 
-       edbg("SEP Driver:g_message_shared_area_addr is %08lx\n", sep_dev->message_shared_area_addr);
+       edbg("SEP Driver:sep->shared_addr is %p\n", sep->shared_addr);
 
-       /* get physical address */
-       phys_addr = sep_dev->phys_shared_area_addr;
+       /* get bus address */
+       bus_addr = sep->shared_bus;
 
-       edbg("SEP Driver: phys_addr is %08lx\n", phys_addr);
+       edbg("SEP Driver: phys_addr is %08llx\n", (unsigned long long)bus_addr);
 
-       if (remap_pfn_range(vma, vma->vm_start, phys_addr >> PAGE_SHIFT, vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
+       if (remap_pfn_range(vma, vma->vm_start, bus_addr >> PAGE_SHIFT, vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
                edbg("SEP Driver remap_page_range failed\n");
                printk(KERN_WARNING "SEP Driver remap_page_range failed\n");
                return -EAGAIN;
@@ -451,42 +413,43 @@ static unsigned int sep_poll(struct file *filp, poll_table * wait)
 {
        unsigned long count;
        unsigned int mask = 0;
-       unsigned long retVal = 0;       /* flow id */
+       unsigned long retval = 0;       /* flow id */
+       struct sep_device *sep = filp->private_data;
 
        dbg("---------->SEP Driver poll: start\n");
 
 
 #if SEP_DRIVER_POLLING_MODE
 
-       while (sep_dev->host_to_sep_send_counter != (retVal & 0x7FFFFFFF)) {
-               retVal = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
+       while (sep->send_ct != (retval & 0x7FFFFFFF)) {
+               retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
 
                for (count = 0; count < 10 * 4; count += 4)
-                       edbg("Poll Debug Word %lu of the message is %lu\n", count, *((unsigned long *) (sep_dev->shared_area_addr + SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES + count)));
+                       edbg("Poll Debug Word %lu of the message is %lu\n", count, *((unsigned long *) (sep->shared_addr + SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES + count)));
        }
 
-       sep_dev->sep_to_host_reply_counter++;
+       sep->reply_ct++;
 #else
        /* add the event to the polling wait table */
-       poll_wait(filp, &g_sep_event, wait);
+       poll_wait(filp, &sep_event, wait);
 
 #endif
 
-       edbg("sep_dev->host_to_sep_send_counter is %lu\n", sep_dev->host_to_sep_send_counter);
-       edbg("sep_dev->sep_to_host_reply_counter is %lu\n", sep_dev->sep_to_host_reply_counter);
+       edbg("sep->send_ct is %lu\n", sep->send_ct);
+       edbg("sep->reply_ct is %lu\n", sep->reply_ct);
 
        /* check if the data is ready */
-       if (sep_dev->host_to_sep_send_counter == sep_dev->sep_to_host_reply_counter) {
+       if (sep->send_ct == sep->reply_ct) {
                for (count = 0; count < 12 * 4; count += 4)
-                       edbg("Sep Mesg Word %lu of the message is %lu\n", count, *((unsigned long *) (sep_dev->shared_area_addr + count)));
+                       edbg("Sep Mesg Word %lu of the message is %lu\n", count, *((unsigned long *) (sep->shared_addr + count)));
 
                for (count = 0; count < 10 * 4; count += 4)
-                       edbg("Debug Data Word %lu of the message is %lu\n", count, *((unsigned long *) (sep_dev->shared_area_addr + 0x1800 + count)));
+                       edbg("Debug Data Word %lu of the message is %lu\n", count, *((unsigned long *) (sep->shared_addr + 0x1800 + count)));
 
-               retVal = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
-               edbg("retVal is %lu\n", retVal);
+               retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
+               edbg("retval is %lu\n", retval);
                /* check if the this is sep reply or request */
-               if (retVal >> 31) {
+               if (retval >> 31) {
                        edbg("SEP Driver: sep request in\n");
                        /* request */
                        mask |= POLLOUT | POLLWRNORM;
@@ -499,99 +462,128 @@ static unsigned int sep_poll(struct file *filp, poll_table * wait)
        return mask;
 }
 
-/*
-  calculates time and sets it at the predefined address
-*/
-static int sep_set_time(unsigned long *address_ptr, unsigned long *time_in_sec_ptr)
+/**
+ *     sep_time_address        -       address in SEP memory of time
+ *     @sep: SEP device we want the address from
+ *
+ *     Return the address of the two dwords in memory used for time
+ *     setting.
+ */
+
+static u32 *sep_time_address(struct sep_device *sep)
+{
+       return sep->shared_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
+}
+
+/**
+ *     sep_set_time            -       set the SEP time
+ *     @sep: the SEP we are setting the time for
+ *
+ *     Calculates time and sets it at the predefined address.
+ *     Called with the sep mutex held.
+ */
+static unsigned long sep_set_time(struct sep_device *sep)
 {
        struct timeval time;
-       /* address of time in the kernel */
-       unsigned long time_addr;
+       u32 *time_addr; /* address of time as seen by the kernel */
 
 
-       dbg("SEP Driver:--------> sep_set_time start\n");
+       dbg("sep:sep_set_time start\n");
 
        do_gettimeofday(&time);
 
        /* set value in the SYSTEM MEMORY offset */
-       time_addr = sep_dev->message_shared_area_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
+       time_addr = sep_time_address(sep);
 
-       *(unsigned long *) time_addr = SEP_TIME_VAL_TOKEN;
-       *(unsigned long *) (time_addr + 4) = time.tv_sec;
+       time_addr[0] = SEP_TIME_VAL_TOKEN;
+       time_addr[1] = time.tv_sec;
 
        edbg("SEP Driver:time.tv_sec is %lu\n", time.tv_sec);
-       edbg("SEP Driver:time_addr is %lu\n", time_addr);
-       edbg("SEP Driver:g_message_shared_area_addr is %lu\n", sep_dev->message_shared_area_addr);
-
-       /* set the output parameters if needed */
-       if (address_ptr)
-               *address_ptr = sep_shared_area_virt_to_phys(time_addr);
+       edbg("SEP Driver:time_addr is %p\n", time_addr);
+       edbg("SEP Driver:sep->shared_addr is %p\n", sep->shared_addr);
 
-       if (time_in_sec_ptr)
-               *time_in_sec_ptr = time.tv_sec;
+       return time.tv_sec;
+}
 
-       dbg("SEP Driver:<-------- sep_set_time end\n");
+/**
+ *     sep_dump_message        - dump the message that is pending
+ *     @sep: sep device
+ *
+ *     Dump out the message pending in the shared message area
+ */
 
-       return 0;
+static void sep_dump_message(struct sep_device *sep)
+{
+       int count;
+       for (count = 0; count < 12 * 4; count += 4)
+               edbg("Word %d of the message is %u\n", count, *((u32 *) (sep->shared_addr + count)));
 }
 
-/*
-  This function raises interrupt to SEP that signals that is has a new
-       command from HOST
-*/
-static void sep_send_command_handler(void)
+/**
+ *     sep_send_command_handler        -       kick off a command
+ *     @sep: sep being signalled
+ *
+ *     This function raises interrupt to SEP that signals that is has a new
+ *     command from the host
+ */
+
+static void sep_send_command_handler(struct sep_device *sep)
 {
-       unsigned long count;
+       dbg("sep:sep_send_command_handler start\n");
 
-       dbg("SEP Driver:--------> sep_send_command_handler start\n");
-       sep_set_time(0, 0);
+       mutex_lock(&sep_mutex);
+       sep_set_time(sep);
 
-       /* flash cache */
+       /* FIXME: flush cache */
        flush_cache_all();
 
-       for (count = 0; count < 12 * 4; count += 4)
-               edbg("Word %lu of the message is %lu\n", count, *((unsigned long *) (sep_dev->shared_area_addr + count)));
-
+       sep_dump_message(sep);
        /* update counter */
-       sep_dev->host_to_sep_send_counter++;
+       sep->send_ct++;
        /* send interrupt to SEP */
-       sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
+       sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
        dbg("SEP Driver:<-------- sep_send_command_handler end\n");
+       mutex_unlock(&sep_mutex);
        return;
 }
 
-/*
-  This function raises interrupt to SEPm that signals that is has a
-  new command from HOST
-*/
-static void sep_send_reply_command_handler(void)
-{
-       unsigned long count;
+/**
+ *     sep_send_reply_command_handler  -       kick off a command reply
+ *     @sep: sep being signalled
+ *
+ *     This function raises interrupt to SEP that signals that is has a new
+ *     command from the host
+ */
 
-       dbg("SEP Driver:--------> sep_send_reply_command_handler start\n");
+static void sep_send_reply_command_handler(struct sep_device *sep)
+{
+       dbg("sep:sep_send_reply_command_handler start\n");
 
        /* flash cache */
        flush_cache_all();
-       for (count = 0; count < 12 * 4; count += 4)
-               edbg("Word %lu of the message is %lu\n", count, *((unsigned long *) (sep_dev->shared_area_addr + count)));
-       /* update counter */
-       sep_dev->host_to_sep_send_counter++;
+
+       sep_dump_message(sep);
+
+       mutex_lock(&sep_mutex);
+       sep->send_ct++;         /* update counter */
        /* send the interrupt to SEP */
-       sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR2_REG_ADDR, sep_dev->host_to_sep_send_counter);
+       sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR, sep->send_ct);
        /* update both counters */
-       sep_dev->host_to_sep_send_counter++;
-       sep_dev->sep_to_host_reply_counter++;
-       dbg("SEP Driver:<-------- sep_send_reply_command_handler end\n");
+       sep->send_ct++;
+       sep->reply_ct++;
+       mutex_unlock(&sep_mutex);
+       dbg("sep: sep_send_reply_command_handler end\n");
 }
 
 /*
   This function handles the allocate data pool memory request
-  This function returns calculates the physical address of the
+  This function returns calculates the bus address of the
   allocated memory, and the offset of this area from the mapped address.
   Therefore, the FVOs in user space can calculate the exact virtual
   address of this allocated memory
 */
-static int sep_allocate_data_pool_memory_handler(unsigned long arg)
+static int sep_allocate_data_pool_memory_handler(struct sep_device *sep,
+                                                       unsigned long arg)
 {
        int error;
        struct sep_driver_alloc_t command_args;
@@ -603,14 +595,14 @@ static int sep_allocate_data_pool_memory_handler(unsigned long arg)
                goto end_function;
 
        /* allocate memory */
-       if ((sep_dev->data_pool_bytes_allocated + command_args.num_bytes) > SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
-               error = -ENOTTY;
+       if ((sep->data_pool_bytes_allocated + command_args.num_bytes) > SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
+               error = -ENOMEM;
                goto end_function;
        }
 
-       /* set the virtual and physical address */
-       command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES + sep_dev->data_pool_bytes_allocated;
-       command_args.phys_address = sep_dev->phys_shared_area_addr + SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES + sep_dev->data_pool_bytes_allocated;
+       /* set the virtual and bus address */
+       command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES + sep->data_pool_bytes_allocated;
+       command_args.phys_address = sep->shared_bus + SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES + sep->data_pool_bytes_allocated;
 
        /* write the memory back to the user space */
        error = copy_to_user((void *) arg, (void *) &command_args, sizeof(struct sep_driver_alloc_t));
@@ -618,7 +610,7 @@ static int sep_allocate_data_pool_memory_handler(unsigned long arg)
                goto end_function;
 
        /* set the allocation */
-       sep_dev->data_pool_bytes_allocated += command_args.num_bytes;
+       sep->data_pool_bytes_allocated += command_args.num_bytes;
 
 end_function:
        dbg("SEP Driver:<-------- sep_allocate_data_pool_memory_handler end\n");
@@ -628,13 +620,14 @@ end_function:
 /*
   This function  handles write into allocated data pool command
 */
-static int sep_write_into_data_pool_handler(unsigned long arg)
+static int sep_write_into_data_pool_handler(struct sep_device *sep, unsigned long arg)
 {
        int error;
-       unsigned long virt_address;
+       void *virt_address;
+       unsigned long va;
        unsigned long app_in_address;
        unsigned long num_bytes;
-       unsigned long data_pool_area_addr;
+       void *data_pool_area_addr;
 
        dbg("SEP Driver:--------> sep_write_into_data_pool_handler start\n");
 
@@ -644,9 +637,10 @@ static int sep_write_into_data_pool_handler(unsigned long arg)
                goto end_function;
 
        /* get the virtual kernel address address */
-       error = get_user(virt_address, &(((struct sep_driver_write_t *) arg)->datapool_address));
+       error = get_user(va, &(((struct sep_driver_write_t *) arg)->datapool_address));
        if (error)
                goto end_function;
+       virt_address = (void *)va;
 
        /* get the number of bytes */
        error = get_user(num_bytes, &(((struct sep_driver_write_t *) arg)->num_bytes));
@@ -654,16 +648,16 @@ static int sep_write_into_data_pool_handler(unsigned long arg)
                goto end_function;
 
        /* calculate the start of the data pool */
-       data_pool_area_addr = sep_dev->shared_area_addr + SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES;
+       data_pool_area_addr = sep->shared_addr + SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES;
 
 
        /* check that the range of the virtual kernel address is correct */
-       if ((virt_address < data_pool_area_addr) || (virt_address > (data_pool_area_addr + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES))) {
-               error = -ENOTTY;
+       if (virt_address < data_pool_area_addr || virt_address > (data_pool_area_addr + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES)) {
+               error = -EINVAL;
                goto end_function;
        }
        /* copy the application data */
-       error = copy_from_user((void *) virt_address, (void *) app_in_address, num_bytes);
+       error = copy_from_user(virt_address, (void *) app_in_address, num_bytes);
 end_function:
        dbg("SEP Driver:<-------- sep_write_into_data_pool_handler end\n");
        return error;
@@ -672,15 +666,16 @@ end_function:
 /*
   this function handles the read from data pool command
 */
-static int sep_read_from_data_pool_handler(unsigned long arg)
+static int sep_read_from_data_pool_handler(struct sep_device *sep, unsigned long arg)
 {
        int error;
        /* virtual address of dest application buffer */
        unsigned long app_out_address;
        /* virtual address of the data pool */
-       unsigned long virt_address;
+       unsigned long va;
+       void *virt_address;
        unsigned long num_bytes;
-       unsigned long data_pool_area_addr;
+       void *data_pool_area_addr;
 
        dbg("SEP Driver:--------> sep_read_from_data_pool_handler start\n");
 
@@ -690,9 +685,10 @@ static int sep_read_from_data_pool_handler(unsigned long arg)
                goto end_function;
 
        /* get the virtual kernel address address */
-       error = get_user(virt_address, &(((struct sep_driver_write_t *) arg)->datapool_address));
+       error = get_user(va, &(((struct sep_driver_write_t *) arg)->datapool_address));
        if (error)
                goto end_function;
+       virt_address = (void *)va;
 
        /* get the number of bytes */
        error = get_user(num_bytes, &(((struct sep_driver_write_t *) arg)->num_bytes));
@@ -700,16 +696,18 @@ static int sep_read_from_data_pool_handler(unsigned long arg)
                goto end_function;
 
        /* calculate the start of the data pool */
-       data_pool_area_addr = sep_dev->shared_area_addr + SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES;
+       data_pool_area_addr = sep->shared_addr + SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES;
 
+       /* FIXME: These are incomplete all over the driver: what about + len
+          and when doing that also overflows */
        /* check that the range of the virtual kernel address is correct */
-       if ((virt_address < data_pool_area_addr) || (virt_address > (data_pool_area_addr + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES))) {
-               error = -ENOTTY;
+       if (virt_address < data_pool_area_addr || virt_address > data_pool_area_addr + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
+               error = -EINVAL;
                goto end_function;
        }
 
        /* copy the application data */
-       error = copy_to_user((void *) app_out_address, (void *) virt_address, num_bytes);
+       error = copy_to_user((void *) app_out_address, virt_address, num_bytes);
 end_function:
        dbg("SEP Driver:<-------- sep_read_from_data_pool_handler end\n");
        return error;
@@ -749,7 +747,12 @@ static int sep_free_dma_pages(struct page **page_array_ptr, unsigned long num_pa
   and construct a basic lli  array, where each entry holds the physical
   page address and the size that application data holds in this physical pages
 */
-static int sep_lock_kernel_pages(unsigned long kernel_virt_addr, unsigned long data_size, unsigned long *num_pages_ptr, struct sep_lli_entry_t **lli_array_ptr, struct page ***page_array_ptr)
+static int sep_lock_kernel_pages(struct sep_device *sep,
+                                unsigned long kernel_virt_addr,
+                                unsigned long data_size,
+                                unsigned long *num_pages_ptr,
+                                struct sep_lli_entry_t **lli_array_ptr,
+                                struct page ***page_array_ptr)
 {
        int error = 0;
        /* the the page of the end address of the user space buffer */
@@ -838,7 +841,12 @@ end_function:
   and construct a basic lli  array, where each entry holds the physical page
   address and the size that application data holds in this physical pages
 */
-static int sep_lock_user_pages(unsigned long app_virt_addr, unsigned long data_size, unsigned long *num_pages_ptr, struct sep_lli_entry_t **lli_array_ptr, struct page ***page_array_ptr)
+static int sep_lock_user_pages(struct sep_device *sep,
+                       unsigned long app_virt_addr,
+                       unsigned long data_size,
+                       unsigned long *num_pages_ptr,
+                       struct sep_lli_entry_t **lli_array_ptr,
+                       struct page ***page_array_ptr)
 {
        int error = 0;
        /* the the page of the end address of the user space buffer */
@@ -1048,7 +1056,7 @@ static void sep_build_lli_table(struct sep_lli_entry_t *lli_array_ptr, struct se
   this function goes over the list of the print created tables and
   prints all the data
 */
-static void sep_debug_print_lli_tables(struct sep_lli_entry_t *lli_table_ptr, unsigned long num_table_entries, unsigned long table_data_size)
+static void sep_debug_print_lli_tables(struct sep_device *sep, struct sep_lli_entry_t *lli_table_ptr, unsigned long num_table_entries, unsigned long table_data_size)
 {
        unsigned long table_count;
        unsigned long entries_count;
@@ -1081,7 +1089,7 @@ static void sep_debug_print_lli_tables(struct sep_lli_entry_t *lli_table_ptr, un
                edbg("SEP Driver:phys table_data_size is %lu num_table_entries is %lu lli_table_ptr is%lu\n", table_data_size, num_table_entries, (unsigned long) lli_table_ptr);
 
                if ((unsigned long) lli_table_ptr != 0xffffffff)
-                       lli_table_ptr = (struct sep_lli_entry_t *) sep_shared_area_phys_to_virt((unsigned long) lli_table_ptr);
+                       lli_table_ptr = (struct sep_lli_entry_t *) sep_shared_bus_to_virt(sep, (unsigned long) lli_table_ptr);
 
                table_count++;
        }
@@ -1093,7 +1101,14 @@ static void sep_debug_print_lli_tables(struct sep_lli_entry_t *lli_table_ptr, un
   This function prepares only input DMA table for synhronic symmetric
   operations (HASH)
 */
-static int sep_prepare_input_dma_table(unsigned long app_virt_addr, unsigned long data_size, unsigned long block_size, unsigned long *lli_table_ptr, unsigned long *num_entries_ptr, unsigned long *table_data_size_ptr, bool isKernelVirtualAddress)
+static int sep_prepare_input_dma_table(struct sep_device *sep,
+                               unsigned long app_virt_addr,
+                               unsigned long data_size,
+                               unsigned long block_size,
+                               unsigned long *lli_table_ptr,
+                               unsigned long *num_entries_ptr,
+                               unsigned long *table_data_size_ptr,
+                               bool isKernelVirtualAddress)
 {
        /* pointer to the info entry of the table - the last entry */
        struct sep_lli_entry_t *info_entry_ptr;
@@ -1110,7 +1125,7 @@ static int sep_prepare_input_dma_table(unsigned long app_virt_addr, unsigned lon
        /* number of entries in lli table */
        unsigned long num_entries_in_table;
        /* next table address */
-       unsigned long lli_table_alloc_addr;
+       void *lli_table_alloc_addr;
        unsigned long result;
 
        dbg("SEP Driver:--------> sep_prepare_input_dma_table start\n");
@@ -1119,20 +1134,21 @@ static int sep_prepare_input_dma_table(unsigned long app_virt_addr, unsigned lon
        edbg("SEP Driver:block_size is %lu\n", block_size);
 
        /* initialize the pages pointers */
-       sep_dev->in_page_array = 0;
-       sep_dev->in_num_pages = 0;
+       sep->in_page_array = 0;
+       sep->in_num_pages = 0;
 
        if (data_size == 0) {
                /* special case  - created 2 entries table with zero data */
-               in_lli_table_ptr = (struct sep_lli_entry_t *) (sep_dev->shared_area_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES);
-               in_lli_table_ptr->physical_address = sep_dev->shared_area_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
+               in_lli_table_ptr = (struct sep_lli_entry_t *) (sep->shared_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES);
+               /* FIXME: Should the entry below not be for _bus */
+               in_lli_table_ptr->physical_address = (unsigned long)sep->shared_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
                in_lli_table_ptr->block_size = 0;
 
                in_lli_table_ptr++;
                in_lli_table_ptr->physical_address = 0xFFFFFFFF;
                in_lli_table_ptr->block_size = 0;
 
-               *lli_table_ptr = sep_dev->phys_shared_area_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
+               *lli_table_ptr = sep->shared_bus + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
                *num_entries_ptr = 2;
                *table_data_size_ptr = 0;
 
@@ -1142,22 +1158,22 @@ static int sep_prepare_input_dma_table(unsigned long app_virt_addr, unsigned lon
        /* check if the pages are in Kernel Virtual Address layout */
        if (isKernelVirtualAddress == true)
                /* lock the pages of the kernel buffer and translate them to pages */
-               result = sep_lock_kernel_pages(app_virt_addr, data_size, &sep_dev->in_num_pages, &lli_array_ptr, &sep_dev->in_page_array);
+               result = sep_lock_kernel_pages(sep, app_virt_addr, data_size, &sep->in_num_pages, &lli_array_ptr, &sep->in_page_array);
        else
                /* lock the pages of the user buffer and translate them to pages */
-               result = sep_lock_user_pages(app_virt_addr, data_size, &sep_dev->in_num_pages, &lli_array_ptr, &sep_dev->in_page_array);
+               result = sep_lock_user_pages(sep, app_virt_addr, data_size, &sep->in_num_pages, &lli_array_ptr, &sep->in_page_array);
 
        if (result)
                return result;
 
-       edbg("SEP Driver:output sep_dev->in_num_pages is %lu\n", sep_dev->in_num_pages);
+       edbg("SEP Driver:output sep->in_num_pages is %lu\n", sep->in_num_pages);
 
        current_entry = 0;
        info_entry_ptr = 0;
-       sep_lli_entries = sep_dev->in_num_pages;
+       sep_lli_entries = sep->in_num_pages;
 
        /* initiate to point after the message area */
-       lli_table_alloc_addr = sep_dev->shared_area_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
+       lli_table_alloc_addr = sep->shared_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
 
        /* loop till all the entries in in array are not processed */
        while (current_entry < sep_lli_entries) {
@@ -1179,14 +1195,14 @@ static int sep_prepare_input_dma_table(unsigned long app_virt_addr, unsigned lon
 
                if (info_entry_ptr == 0) {
                        /* set the output parameters to physical addresses */
-                       *lli_table_ptr = sep_shared_area_virt_to_phys((unsigned long) in_lli_table_ptr);
+                       *lli_table_ptr = sep_shared_virt_to_bus(sep, in_lli_table_ptr);
                        *num_entries_ptr = num_entries_in_table;
                        *table_data_size_ptr = table_data_size;
 
                        edbg("SEP Driver:output lli_table_in_ptr is %08lx\n", *lli_table_ptr);
                } else {
                        /* update the info entry of the previous in table */
-                       info_entry_ptr->physical_address = sep_shared_area_virt_to_phys((unsigned long) in_lli_table_ptr);
+                       info_entry_ptr->physical_address = sep_shared_virt_to_bus(sep, in_lli_table_ptr);
                        info_entry_ptr->block_size = ((num_entries_in_table) << 24) | (table_data_size);
                }
 
@@ -1195,8 +1211,8 @@ static int sep_prepare_input_dma_table(unsigned long app_virt_addr, unsigned lon
        }
 
        /* print input tables */
-       sep_debug_print_lli_tables((struct sep_lli_entry_t *)
-                                  sep_shared_area_phys_to_virt(*lli_table_ptr), *num_entries_ptr, *table_data_size_ptr);
+       sep_debug_print_lli_tables(sep, (struct sep_lli_entry_t *)
+                                  sep_shared_bus_to_virt(sep, *lli_table_ptr), *num_entries_ptr, *table_data_size_ptr);
 
        /* the array of the pages */
        kfree(lli_array_ptr);
@@ -1210,14 +1226,16 @@ end_function:
  This function creates the input and output dma tables for
  symmetric operations (AES/DES) according to the block size from LLI arays
 */
-static int sep_construct_dma_tables_from_lli(struct sep_lli_entry_t *lli_in_array,
+static int sep_construct_dma_tables_from_lli(struct sep_device *sep,
+                                     struct sep_lli_entry_t *lli_in_array,
                                      unsigned long sep_in_lli_entries,
                                      struct sep_lli_entry_t *lli_out_array,
                                      unsigned long sep_out_lli_entries,
                                      unsigned long block_size, unsigned long *lli_table_in_ptr, unsigned long *lli_table_out_ptr, unsigned long *in_num_entries_ptr, unsigned long *out_num_entries_ptr, unsigned long *table_data_size_ptr)
 {
-       /* points to the area where next lli table can be allocated */
-       unsigned long lli_table_alloc_addr;
+       /* points to the area where next lli table can be allocated: keep void *
+          as there is pointer scaling to fix otherwise */
+       void *lli_table_alloc_addr;
        /* input lli table */
        struct sep_lli_entry_t *in_lli_table_ptr;
        /* output lli table */
@@ -1246,7 +1264,7 @@ static int sep_construct_dma_tables_from_lli(struct sep_lli_entry_t *lli_in_arra
        dbg("SEP Driver:--------> sep_construct_dma_tables_from_lli start\n");
 
        /* initiate to pint after the message area */
-       lli_table_alloc_addr = sep_dev->shared_area_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
+       lli_table_alloc_addr = sep->shared_addr + SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_OFFSET_IN_BYTES;
 
        current_in_entry = 0;
        current_out_entry = 0;
@@ -1294,9 +1312,9 @@ static int sep_construct_dma_tables_from_lli(struct sep_lli_entry_t *lli_in_arra
                /* if info entry is null - this is the first table built */
                if (info_in_entry_ptr == 0) {
                        /* set the output parameters to physical addresses */
-                       *lli_table_in_ptr = sep_shared_area_virt_to_phys((unsigned long) in_lli_table_ptr);
+                       *lli_table_in_ptr = sep_shared_virt_to_bus(sep, in_lli_table_ptr);
                        *in_num_entries_ptr = num_entries_in_table;
-                       *lli_table_out_ptr = sep_shared_area_virt_to_phys((unsigned long) out_lli_table_ptr);
+                       *lli_table_out_ptr = sep_shared_virt_to_bus(sep, out_lli_table_ptr);
                        *out_num_entries_ptr = num_entries_out_table;
                        *table_data_size_ptr = table_data_size;
 
@@ -1304,11 +1322,11 @@ static int sep_construct_dma_tables_from_lli(struct sep_lli_entry_t *lli_in_arra
                        edbg("SEP Driver:output lli_table_out_ptr is %08lx\n", *lli_table_out_ptr);
                } else {
                        /* update the info entry of the previous in table */
-                       info_in_entry_ptr->physical_address = sep_shared_area_virt_to_phys((unsigned long) in_lli_table_ptr);
+                       info_in_entry_ptr->physical_address = sep_shared_virt_to_bus(sep, in_lli_table_ptr);
                        info_in_entry_ptr->block_size = ((num_entries_in_table) << 24) | (table_data_size);
 
                        /* update the info entry of the previous in table */
-                       info_out_entry_ptr->physical_address = sep_shared_area_virt_to_phys((unsigned long) out_lli_table_ptr);
+                       info_out_entry_ptr->physical_address = sep_shared_virt_to_bus(sep, out_lli_table_ptr);
                        info_out_entry_ptr->block_size = ((num_entries_out_table) << 24) | (table_data_size);
                }
 
@@ -1322,11 +1340,11 @@ static int sep_construct_dma_tables_from_lli(struct sep_lli_entry_t *lli_in_arra
        }
 
        /* print input tables */
-       sep_debug_print_lli_tables((struct sep_lli_entry_t *)
-                                  sep_shared_area_phys_to_virt(*lli_table_in_ptr), *in_num_entries_ptr, *table_data_size_ptr);
+       sep_debug_print_lli_tables(sep, (struct sep_lli_entry_t *)
+                                  sep_shared_bus_to_virt(sep, *lli_table_in_ptr), *in_num_entries_ptr, *table_data_size_ptr);
        /* print output tables */
-       sep_debug_print_lli_tables((struct sep_lli_entry_t *)
-                                  sep_shared_area_phys_to_virt(*lli_table_out_ptr), *out_num_entries_ptr, *table_data_size_ptr);
+       sep_debug_print_lli_tables(sep, (struct sep_lli_entry_t *)
+                                  sep_shared_bus_to_virt(sep, *lli_table_out_ptr), *out_num_entries_ptr, *table_data_size_ptr);
        dbg("SEP Driver:<-------- sep_construct_dma_tables_from_lli end\n");
        return 0;
 }
@@ -1337,7 +1355,8 @@ static int sep_construct_dma_tables_from_lli(struct sep_lli_entry_t *lli_in_arra
   symmetric operations (AES, DES). It also checks that each table
   is of the modular block size
 */
-static int sep_prepare_input_output_dma_table(unsigned long app_virt_in_addr,
+static int sep_prepare_input_output_dma_table(struct sep_device *sep,
+                                      unsigned long app_virt_in_addr,
                                       unsigned long app_virt_out_addr,
                                       unsigned long data_size,
                                       unsigned long block_size,
@@ -1352,20 +1371,20 @@ static int sep_prepare_input_output_dma_table(unsigned long app_virt_in_addr,
        dbg("SEP Driver:--------> sep_prepare_input_output_dma_table start\n");
 
        /* initialize the pages pointers */
-       sep_dev->in_page_array = 0;
-       sep_dev->out_page_array = 0;
+       sep->in_page_array = 0;
+       sep->out_page_array = 0;
 
        /* check if the pages are in Kernel Virtual Address layout */
        if (isKernelVirtualAddress == true) {
                /* lock the pages of the kernel buffer and translate them to pages */
-               result = sep_lock_kernel_pages(app_virt_in_addr, data_size, &sep_dev->in_num_pages, &lli_in_array, &sep_dev->in_page_array);
+               result = sep_lock_kernel_pages(sep, app_virt_in_addr, data_size, &sep->in_num_pages, &lli_in_array, &sep->in_page_array);
                if (result) {
                        edbg("SEP Driver: sep_lock_kernel_pages for input virtual buffer failed\n");
                        goto end_function;
                }
        } else {
                /* lock the pages of the user buffer and translate them to pages */
-               result = sep_lock_user_pages(app_virt_in_addr, data_size, &sep_dev->in_num_pages, &lli_in_array, &sep_dev->in_page_array);
+               result = sep_lock_user_pages(sep, app_virt_in_addr, data_size, &sep->in_num_pages, &lli_in_array, &sep->in_page_array);
                if (result) {
                        edbg("SEP Driver: sep_lock_user_pages for input virtual buffer failed\n");
                        goto end_function;
@@ -1373,25 +1392,25 @@ static int sep_prepare_input_output_dma_table(unsigned long app_virt_in_addr,
        }
 
        if (isKernelVirtualAddress == true) {
-               result = sep_lock_kernel_pages(app_virt_out_addr, data_size, &sep_dev->out_num_pages, &lli_out_array, &sep_dev->out_page_array);
+               result = sep_lock_kernel_pages(sep, app_virt_out_addr, data_size, &sep->out_num_pages, &lli_out_array, &sep->out_page_array);
                if (result) {
                        edbg("SEP Driver: sep_lock_kernel_pages for output virtual buffer failed\n");
                        goto end_function_with_error1;
                }
        } else {
-               result = sep_lock_user_pages(app_virt_out_addr, data_size, &sep_dev->out_num_pages, &lli_out_array, &sep_dev->out_page_array);
+               result = sep_lock_user_pages(sep, app_virt_out_addr, data_size, &sep->out_num_pages, &lli_out_array, &sep->out_page_array);
                if (result) {
                        edbg("SEP Driver: sep_lock_user_pages for output virtual buffer failed\n");
                        goto end_function_with_error1;
                }
        }
-       edbg("sep_dev->in_num_pages is %lu\n", sep_dev->in_num_pages);
-       edbg("sep_dev->out_num_pages is %lu\n", sep_dev->out_num_pages);
+       edbg("sep->in_num_pages is %lu\n", sep->in_num_pages);
+       edbg("sep->out_num_pages is %lu\n", sep->out_num_pages);
        edbg("SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP is %x\n", SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
 
 
        /* call the fucntion that creates table from the lli arrays */
-       result = sep_construct_dma_tables_from_lli(lli_in_array, sep_dev->in_num_pages, lli_out_array, sep_dev->out_num_pages, block_size, lli_table_in_ptr, lli_table_out_ptr, in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);
+       result = sep_construct_dma_tables_from_lli(sep, lli_in_array, sep->in_num_pages, lli_out_array, sep->out_num_pages, block_size, lli_table_in_ptr, lli_table_out_ptr, in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);
        if (result) {
                edbg("SEP Driver: sep_construct_dma_tables_from_lli failed\n");
                goto end_function_with_error2;
@@ -1415,7 +1434,8 @@ end_function:
   this function handles tha request for creation of the DMA table
   for the synchronic symmetric operations (AES,DES)
 */
-static int sep_create_sync_dma_tables_handler(unsigned long arg)
+static int sep_create_sync_dma_tables_handler(struct sep_device *sep,
+                                               unsigned long arg)
 {
        int error;
        /* command arguments */
@@ -1435,7 +1455,8 @@ static int sep_create_sync_dma_tables_handler(unsigned long arg)
        /* check if we need to build only input table or input/output */
        if (command_args.app_out_address)
                /* prepare input and output tables */
-               error = sep_prepare_input_output_dma_table(command_args.app_in_address,
+               error = sep_prepare_input_output_dma_table(sep,
+                                                          command_args.app_in_address,
                                                           command_args.app_out_address,
                                                           command_args.data_in_size,
                                                           command_args.block_size,
@@ -1443,13 +1464,15 @@ static int sep_create_sync_dma_tables_handler(unsigned long arg)
                                                           &command_args.out_table_address, &command_args.in_table_num_entries, &command_args.out_table_num_entries, &command_args.table_data_size, command_args.isKernelVirtualAddress);
        else
                /* prepare input tables */
-               error = sep_prepare_input_dma_table(command_args.app_in_address,
+               error = sep_prepare_input_dma_table(sep,
+                                                   command_args.app_in_address,
                                                    command_args.data_in_size, command_args.block_size, &command_args.in_table_address, &command_args.in_table_num_entries, &command_args.table_data_size, command_args.isKernelVirtualAddress);
 
        if (error)
                goto end_function;
        /* copy to user */
-       error = copy_to_user((void *) arg, (void *) &command_args, sizeof(struct sep_driver_build_sync_table_t));
+       if (copy_to_user((void *) arg, (void *) &command_args, sizeof(struct sep_driver_build_sync_table_t)))
+               error = -EFAULT;
 end_function:
        dbg("SEP Driver:<-------- sep_create_sync_dma_tables_handler end\n");
        return error;
@@ -1458,22 +1481,22 @@ end_function:
 /*
   this function handles the request for freeing dma table for synhronic actions
 */
-static int sep_free_dma_table_data_handler(void)
+static int sep_free_dma_table_data_handler(struct sep_device *sep)
 {
        dbg("SEP Driver:--------> sep_free_dma_table_data_handler start\n");
 
        /* free input pages array */
-       sep_free_dma_pages(sep_dev->in_page_array, sep_dev->in_num_pages, 0);
+       sep_free_dma_pages(sep->in_page_array, sep->in_num_pages, 0);
 
        /* free output pages array if needed */
-       if (sep_dev->out_page_array)
-               sep_free_dma_pages(sep_dev->out_page_array, sep_dev->out_num_pages, 1);
+       if (sep->out_page_array)
+               sep_free_dma_pages(sep->out_page_array, sep->out_num_pages, 1);
 
        /* reset all the values */
-       sep_dev->in_page_array = 0;
-       sep_dev->out_page_array = 0;
-       sep_dev->in_num_pages = 0;
-       sep_dev->out_num_pages = 0;
+       sep->in_page_array = 0;
+       sep->out_page_array = 0;
+       sep->in_num_pages = 0;
+       sep->out_num_pages = 0;
        dbg("SEP Driver:<-------- sep_free_dma_table_data_handler end\n");
        return 0;
 }
@@ -1481,18 +1504,21 @@ static int sep_free_dma_table_data_handler(void)
 /*
   this function find a space for the new flow dma table
 */
-static int sep_find_free_flow_dma_table_space(unsigned long **table_address_ptr)
+static int sep_find_free_flow_dma_table_space(struct sep_device *sep,
+                                       unsigned long **table_address_ptr)
 {
        int error = 0;
        /* pointer to the id field of the flow dma table */
        unsigned long *start_table_ptr;
-       unsigned long flow_dma_area_start_addr;
-       unsigned long flow_dma_area_end_addr;
+       /* Do not make start_addr unsigned long * unless fixing the offset
+          computations ! */
+       void *flow_dma_area_start_addr;
+       unsigned long *flow_dma_area_end_addr;
        /* maximum table size in words */
        unsigned long table_size_in_words;
 
        /* find the start address of the flow DMA table area */
-       flow_dma_area_start_addr = sep_dev->shared_area_addr + SEP_DRIVER_FLOW_DMA_TABLES_AREA_OFFSET_IN_BYTES;
+       flow_dma_area_start_addr = sep->shared_addr + SEP_DRIVER_FLOW_DMA_TABLES_AREA_OFFSET_IN_BYTES;
 
        /* set end address of the flow table area */
        flow_dma_area_end_addr = flow_dma_area_start_addr + SEP_DRIVER_FLOW_DMA_TABLES_AREA_SIZE_IN_BYTES;
@@ -1501,14 +1527,14 @@ static int sep_find_free_flow_dma_table_space(unsigned long **table_address_ptr)
        table_size_in_words = SEP_DRIVER_MAX_FLOW_NUM_ENTRIES_IN_TABLE * (sizeof(struct sep_lli_entry_t) / sizeof(long)) + 2;
 
        /* set the pointer to the start address of DMA area */
-       start_table_ptr = (unsigned long *) flow_dma_area_start_addr;
+       start_table_ptr = flow_dma_area_start_addr;
 
        /* find the space for the next table */
-       while (((*start_table_ptr & 0x7FFFFFFF) != 0) && ((unsigned long) start_table_ptr < flow_dma_area_end_addr))
+       while (((*start_table_ptr & 0x7FFFFFFF) != 0) && start_table_ptr < flow_dma_area_end_addr)
                start_table_ptr += table_size_in_words;
 
        /* check if we reached the end of floa tables area */
-       if ((unsigned long) start_table_ptr >= flow_dma_area_end_addr)
+       if (start_table_ptr >= flow_dma_area_end_addr)
                error = -1;
        else
                *table_address_ptr = start_table_ptr;
@@ -1516,13 +1542,17 @@ static int sep_find_free_flow_dma_table_space(unsigned long **table_address_ptr)
        return error;
 }
 
-
-
 /*
   This function creates one DMA table for flow and returns its data,
   and pointer to its info entry
 */
-static int sep_prepare_one_flow_dma_table(unsigned long virt_buff_addr, unsigned long virt_buff_size, struct sep_lli_entry_t *table_data, struct sep_lli_entry_t **info_entry_ptr, struct sep_flow_context_t *flow_data_ptr, bool isKernelVirtualAddress)
+static int sep_prepare_one_flow_dma_table(struct sep_device *sep,
+                                       unsigned long virt_buff_addr,
+                                       unsigned long virt_buff_size,
+                                       struct sep_lli_entry_t *table_data,
+                                       struct sep_lli_entry_t **info_entry_ptr,
+                                       struct sep_flow_context_t *flow_data_ptr,
+                                       bool isKernelVirtualAddress)
 {
        int error;
        /* the range in pages */
@@ -1537,17 +1567,17 @@ static int sep_prepare_one_flow_dma_table(unsigned long virt_buff_addr, unsigned
        unsigned long entry_count;
 
        /* find the space for the new table */
-       error = sep_find_free_flow_dma_table_space(&start_dma_table_ptr);
+       error = sep_find_free_flow_dma_table_space(sep, &start_dma_table_ptr);
        if (error)
                goto end_function;
 
        /* check if the pages are in Kernel Virtual Address layout */
        if (isKernelVirtualAddress == true)
                /* lock kernel buffer in the memory */
-               error = sep_lock_kernel_pages(virt_buff_addr, virt_buff_size, &lli_array_size, &lli_array, &page_array_ptr);
+               error = sep_lock_kernel_pages(sep, virt_buff_addr, virt_buff_size, &lli_array_size, &lli_array, &page_array_ptr);
        else
                /* lock user buffer in the memory */
-               error = sep_lock_user_pages(virt_buff_addr, virt_buff_size, &lli_array_size, &lli_array, &page_array_ptr);
+               error = sep_lock_user_pages(sep, virt_buff_addr, virt_buff_size, &lli_array_size, &lli_array, &page_array_ptr);
 
        if (error)
                goto end_function;
@@ -1602,8 +1632,9 @@ end_function:
   This function creates a list of tables for flow and returns the data for
        the first and last tables of the list
 */
-static int sep_prepare_flow_dma_tables(unsigned long num_virtual_buffers,
-                                      unsigned long first_buff_addr, struct sep_flow_context_t *flow_data_ptr, struct sep_lli_entry_t *first_table_data_ptr, struct sep_lli_entry_t *last_table_data_ptr, bool isKernelVirtualAddress)
+static int sep_prepare_flow_dma_tables(struct sep_device *sep,
+                                       unsigned long num_virtual_buffers,
+                                       unsigned long first_buff_addr, struct sep_flow_context_t *flow_data_ptr, struct sep_lli_entry_t *first_table_data_ptr, struct sep_lli_entry_t *last_table_data_ptr, bool isKernelVirtualAddress)
 {
        int error;
        unsigned long virt_buff_addr;
@@ -1638,7 +1669,7 @@ static int sep_prepare_flow_dma_tables(unsigned long num_virtual_buffers,
                first_buff_addr++;
 
                /* now prepare the one flow LLI table from the data */
-               error = sep_prepare_one_flow_dma_table(virt_buff_addr, virt_buff_size, &table_data, &info_entry_ptr, flow_data_ptr, isKernelVirtualAddress);
+               error = sep_prepare_one_flow_dma_table(sep, virt_buff_addr, virt_buff_size, &table_data, &info_entry_ptr, flow_data_ptr, isKernelVirtualAddress);
                if (error)
                        goto end_function;
 
@@ -1708,39 +1739,37 @@ static void sep_deallocated_flow_tables(struct sep_lli_entry_t *first_table_ptr)
        return;
 }
 
-/*
-  This function returns pointer to the  flow data structure
-  that contains the given id
-*/
-static int sep_find_flow_context(unsigned long flow_id, struct sep_flow_context_t **flow_data_ptr)
-{
-       unsigned long count;
-       int error = 0;
+/**
+ *     sep_find_flow_context   -       find a flow
+ *     @sep: the SEP we are working with
+ *     @flow_id: flow identifier
+ *
+ *     Returns a pointer the matching flow, or NULL if the flow does not
+ *     exist.
+ */
 
+static struct sep_flow_context_t *sep_find_flow_context(struct sep_device *sep,
+                               unsigned long flow_id)
+{
+       int count;
        /*
-          always search for flow with id default first - in case we
-          already started working on the flow there can be no situation
-          when 2 flows are with default flag
+        *  always search for flow with id default first - in case we
+        *  already started working on the flow there can be no situation
+        *  when 2 flows are with default flag
         */
        for (count = 0; count < SEP_DRIVER_NUM_FLOWS; count++) {
-               if (sep_dev->flows_data_array[count].flow_id == flow_id) {
-                       *flow_data_ptr = &sep_dev->flows_data_array[count];
-                       break;
-               }
+               if (sep->flows[count].flow_id == flow_id)
+                       return &sep->flows[count];
        }
-
-       if (count == SEP_DRIVER_NUM_FLOWS)
-               /* no flow found  */
-               error = -ENOMEM;
-
-       return error;
+       return NULL;
 }
 
 
 /*
   this function handles the request to create the DMA tables for flow
 */
-static int sep_create_flow_dma_tables_handler(unsigned long arg)
+static int sep_create_flow_dma_tables_handler(struct sep_device *sep,
+                                                       unsigned long arg)
 {
        int error;
        struct sep_driver_build_flow_table_t command_args;
@@ -1760,8 +1789,9 @@ static int sep_create_flow_dma_tables_handler(unsigned long arg)
        first_table_data.physical_address = 0xffffffff;
 
        /* find the free structure for flow data */
-       error = sep_find_flow_context(SEP_FREE_FLOW_ID, &flow_context_ptr);
-       if (error)
+       error = -EINVAL;
+       flow_context_ptr = sep_find_flow_context(sep, SEP_FREE_FLOW_ID);
+       if (flow_context_ptr == NULL)
                goto end_function;
 
        error = copy_from_user(&command_args, (void *) arg, sizeof(struct sep_driver_build_flow_table_t));
@@ -1769,7 +1799,7 @@ static int sep_create_flow_dma_tables_handler(unsigned long arg)
                goto end_function;
 
        /* create flow tables */
-       error = sep_prepare_flow_dma_tables(command_args.num_virtual_buffers, command_args.virt_buff_data_addr, flow_context_ptr, &first_table_data, &last_table_data, command_args.isKernelVirtualAddress);
+       error = sep_prepare_flow_dma_tables(sep, command_args.num_virtual_buffers, command_args.virt_buff_data_addr, flow_context_ptr, &first_table_data, &last_table_data, command_args.isKernelVirtualAddress);
        if (error)
                goto end_function_with_error;
 
@@ -1808,9 +1838,9 @@ end_function:
 }
 
 /*
-  this functio nhandles add tables to flow
+  this functiohandles add tables to flow
 */
-static int sep_add_flow_tables_handler(unsigned long arg)
+static int sep_add_flow_tables_handler(struct sep_device *sep, unsigned long arg)
 {
        int error;
        unsigned long num_entries;
@@ -1831,12 +1861,12 @@ static int sep_add_flow_tables_handler(unsigned long arg)
                goto end_function;
 
        /* find the flow structure for the flow id */
-       error = sep_find_flow_context(command_args.flow_id, &flow_context_ptr);
-       if (error)
+       flow_context_ptr = sep_find_flow_context(sep, command_args.flow_id);
+       if (flow_context_ptr == NULL)
                goto end_function;
 
        /* prepare the flow dma tables */
-       error = sep_prepare_flow_dma_tables(command_args.num_virtual_buffers, command_args.virt_buff_data_addr, flow_context_ptr, &first_table_data, &last_table_data, command_args.isKernelVirtualAddress);
+       error = sep_prepare_flow_dma_tables(sep, command_args.num_virtual_buffers, command_args.virt_buff_data_addr, flow_context_ptr, &first_table_data, &last_table_data, command_args.isKernelVirtualAddress);
        if (error)
                goto end_function_with_error;
 
@@ -1910,7 +1940,7 @@ end_function:
 /*
   this function add the flow add message to the specific flow
 */
-static int sep_add_flow_tables_message_handler(unsigned long arg)
+static int sep_add_flow_tables_message_handler(struct sep_device *sep, unsigned long arg)
 {
        int error;
        struct sep_driver_add_message_t command_args;
@@ -1929,8 +1959,8 @@ static int sep_add_flow_tables_message_handler(unsigned long arg)
        }
 
        /* find the flow context */
-       error = sep_find_flow_context(command_args.flow_id, &flow_context_ptr);
-       if (error)
+       flow_context_ptr = sep_find_flow_context(sep, command_args.flow_id);
+       if (flow_context_ptr == NULL)
                goto end_function;
 
        /* copy the message into context */
@@ -1943,9 +1973,9 @@ end_function:
 
 
 /*
-  this function returns the physical and virtual addresses of the static pool
+  this function returns the bus and virtual addresses of the static pool
 */
-static int sep_get_static_pool_addr_handler(unsigned long arg)
+static int sep_get_static_pool_addr_handler(struct sep_device *sep, unsigned long arg)
 {
        int error;
        struct sep_driver_static_pool_addr_t command_args;
@@ -1953,10 +1983,10 @@ static int sep_get_static_pool_addr_handler(unsigned long arg)
        dbg("SEP Driver:--------> sep_get_static_pool_addr_handler start\n");
 
        /*prepare the output parameters in the struct */
-       command_args.physical_static_address = sep_dev->phys_shared_area_addr + SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
-       command_args.virtual_static_address = sep_dev->shared_area_addr + SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
+       command_args.physical_static_address = sep->shared_bus + SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
+       command_args.virtual_static_address = (unsigned long)sep->shared_addr + SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
 
-       edbg("SEP Driver:physical_static_address is %08lx, virtual_static_address %08lx\n", command_args.physical_static_address, command_args.virtual_static_address);
+       edbg("SEP Driver:bus_static_address is %08lx, virtual_static_address %08lx\n", command_args.physical_static_address, command_args.virtual_static_address);
 
        /* send the parameters to user application */
        error = copy_to_user((void *) arg, &command_args, sizeof(struct sep_driver_static_pool_addr_t));
@@ -1968,7 +1998,7 @@ static int sep_get_static_pool_addr_handler(unsigned long arg)
   this address gets the offset of the physical address from the start
   of the mapped area
 */
-static int sep_get_physical_mapped_offset_handler(unsigned long arg)
+static int sep_get_physical_mapped_offset_handler(struct sep_device *sep, unsigned long arg)
 {
        int error;
        struct sep_driver_get_mapped_offset_t command_args;
@@ -1979,15 +2009,15 @@ static int sep_get_physical_mapped_offset_handler(unsigned long arg)
        if (error)
                goto end_function;
 
-       if (command_args.physical_address < sep_dev->phys_shared_area_addr) {
-               error = -ENOTTY;
+       if (command_args.physical_address < sep->shared_bus) {
+               error = -EINVAL;
                goto end_function;
        }
 
        /*prepare the output parameters in the struct */
-       command_args.offset = command_args.physical_address - sep_dev->phys_shared_area_addr;
+       command_args.offset = command_args.physical_address - sep->shared_bus;
 
-       edbg("SEP Driver:physical_address is %08lx, offset is %lu\n", command_args.physical_address, command_args.offset);
+       edbg("SEP Driver:bus_address is %08lx, offset is %lu\n", command_args.physical_address, command_args.offset);
 
        /* send the parameters to user application */
        error = copy_to_user((void *) arg, &command_args, sizeof(struct sep_driver_get_mapped_offset_t));
@@ -2000,7 +2030,7 @@ end_function:
 /*
   ?
 */
-static int sep_start_handler(void)
+static int sep_start_handler(struct sep_device *sep)
 {
        unsigned long reg_val;
        unsigned long error = 0;
@@ -2009,13 +2039,13 @@ static int sep_start_handler(void)
 
        /* wait in polling for message from SEP */
        do
-               reg_val = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
+               reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
        while (!reg_val);
 
        /* check the value */
        if (reg_val == 0x1)
-               /* fatal error - read erro status from GPRO */
-               error = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
+               /* fatal error - read error status from GPRO */
+               error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
        dbg("SEP Driver:<-------- sep_start_handler end\n");
        return error;
 }
@@ -2023,7 +2053,7 @@ static int sep_start_handler(void)
 /*
   this function handles the request for SEP initialization
 */
-static int sep_init_handler(unsigned long arg)
+static int sep_init_handler(struct sep_device *sep, unsigned long arg)
 {
        unsigned long message_word;
        unsigned long *message_ptr;
@@ -2050,22 +2080,22 @@ static int sep_init_handler(unsigned long arg)
        message_ptr = (unsigned long *) command_args.message_addr;
 
        /* set the base address of the SRAM  */
-       sep_write_reg(sep_dev, HW_SRAM_ADDR_REG_ADDR, HW_CC_SRAM_BASE_ADDRESS);
+       sep_write_reg(sep, HW_SRAM_ADDR_REG_ADDR, HW_CC_SRAM_BASE_ADDRESS);
 
        for (counter = 0; counter < command_args.message_size_in_words; counter++, message_ptr++) {
                get_user(message_word, message_ptr);
                /* write data to SRAM */
-               sep_write_reg(sep_dev, HW_SRAM_DATA_REG_ADDR, message_word);
+               sep_write_reg(sep, HW_SRAM_DATA_REG_ADDR, message_word);
                edbg("SEP Driver:message_word is %lu\n", message_word);
                /* wait for write complete */
-               sep_wait_sram_write(sep_dev);
+               sep_wait_sram_write(sep);
        }
        dbg("SEP Driver:--------> sep_init_handler - finished getting messages from user space\n");
        /* signal SEP */
-       sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);
+       sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);
 
        do
-               reg_val = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
+               reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
        while (!(reg_val & 0xFFFFFFFD));
 
        dbg("SEP Driver:--------> sep_init_handler - finished waiting for reg_val & 0xFFFFFFFD \n");
@@ -2074,11 +2104,11 @@ static int sep_init_handler(unsigned long arg)
        if (reg_val == 0x1) {
                edbg("SEP Driver:init failed\n");
 
-               error = sep_read_reg(sep_dev, 0x8060);
+               error = sep_read_reg(sep, 0x8060);
                edbg("SEP Driver:sw monitor is %lu\n", error);
 
                /* fatal error - read erro status from GPRO */
-               error = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
+               error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
                edbg("SEP Driver:error is %lu\n", error);
        }
 end_function:
@@ -2090,98 +2120,81 @@ end_function:
 /*
   this function handles the request cache and resident reallocation
 */
-static int sep_realloc_cache_resident_handler(unsigned long arg)
+static int sep_realloc_cache_resident_handler(struct sep_device *sep,
+                                               unsigned long arg)
 {
-       int error;
-       unsigned long phys_cache_address;
-       unsigned long phys_resident_address;
        struct sep_driver_realloc_cache_resident_t command_args;
-
-       /* copy the data */
-       error = copy_from_user(&command_args, (void *) arg, sizeof(struct sep_driver_realloc_cache_resident_t));
-       if (error)
-               goto end_function;
+       int error;
 
        /* copy cache and resident to the their intended locations */
-       error = sep_copy_cache_resident_to_area(command_args.cache_addr, command_args.cache_size_in_bytes, command_args.resident_addr, command_args.resident_size_in_bytes, &phys_cache_address, &phys_resident_address);
+       error = sep_load_firmware(sep);
        if (error)
-               goto end_function;
+               return error;
 
-       command_args.new_base_addr = sep_dev->phys_shared_area_addr;
+       command_args.new_base_addr = sep->shared_bus;
 
        /* find the new base address according to the lowest address between
           cache, resident and shared area */
-       if (phys_resident_address < command_args.new_base_addr)
-               command_args.new_base_addr = phys_resident_address;
-       if (phys_cache_address < command_args.new_base_addr)
-               command_args.new_base_addr = phys_cache_address;
+       if (sep->resident_bus < command_args.new_base_addr)
+               command_args.new_base_addr = sep->resident_bus;
+       if (sep->rar_bus < command_args.new_base_addr)
+               command_args.new_base_addr = sep->rar_bus;
 
        /* set the return parameters */
-       command_args.new_cache_addr = phys_cache_address;
-       command_args.new_resident_addr = phys_resident_address;
+       command_args.new_cache_addr = sep->rar_bus;
+       command_args.new_resident_addr = sep->resident_bus;
 
        /* set the new shared area */
-       command_args.new_shared_area_addr = sep_dev->phys_shared_area_addr;
+       command_args.new_shared_area_addr = sep->shared_bus;
 
-       edbg("SEP Driver:command_args.new_shared_area_addr is %08lx\n", command_args.new_shared_area_addr);
-       edbg("SEP Driver:command_args.new_base_addr is %08lx\n", command_args.new_base_addr);
-       edbg("SEP Driver:command_args.new_resident_addr is %08lx\n", command_args.new_resident_addr);
-       edbg("SEP Driver:command_args.new_cache_addr is %08lx\n", command_args.new_cache_addr);
+       edbg("SEP Driver:command_args.new_shared_addr is %08llx\n", command_args.new_shared_area_addr);
+       edbg("SEP Driver:command_args.new_base_addr is %08llx\n", command_args.new_base_addr);
+       edbg("SEP Driver:command_args.new_resident_addr is %08llx\n", command_args.new_resident_addr);
+       edbg("SEP Driver:command_args.new_rar_addr is %08llx\n", command_args.new_cache_addr);
 
        /* return to user */
-       error = copy_to_user((void *) arg, (void *) &command_args, sizeof(struct sep_driver_realloc_cache_resident_t));
-end_function:
-       return error;
+       if (copy_to_user((void *) arg, &command_args, sizeof(struct sep_driver_realloc_cache_resident_t)))
+               return -EFAULT;
+       return 0;
 }
 
-/*
-  this function handles the request for get time
-*/
-static int sep_get_time_handler(unsigned long arg)
-{
-       int error;
-       struct sep_driver_get_time_t command_args;
-
-       error = sep_set_time(&command_args.time_physical_address, &command_args.time_value);
-       error = copy_to_user((void *) arg, (void *) &command_args, sizeof(struct sep_driver_get_time_t));
-       return error;
-
-}
+/**
+ *     sep_get_time_handler    -       time request from user space
+ *     @sep: sep we are to set the time for
+ *     @arg: pointer to user space arg buffer
+ *
+ *     This function reports back the time and the address in the SEP
+ *     shared buffer at which it has been placed. (Do we really need this!!!)
+ */
 
-/*
-  This api handles the setting of API mode to blocking or non-blocking
-*/
-static int sep_set_api_mode_handler(unsigned long arg)
+static int sep_get_time_handler(struct sep_device *sep, unsigned long arg)
 {
-       int error;
-       unsigned long mode_flag;
-
-       dbg("SEP Driver:--------> sep_set_api_mode_handler start\n");
+       struct sep_driver_get_time_t command_args;
 
-       error = get_user(mode_flag, &(((struct sep_driver_set_api_mode_t *) arg)->mode));
-       if (error)
-               goto end_function;
+       mutex_lock(&sep_mutex);
+       command_args.time_value = sep_set_time(sep);
+       command_args.time_physical_address = (unsigned long)sep_time_address(sep);
+       mutex_unlock(&sep_mutex);
+       if (copy_to_user((void __user *)arg,
+                       &command_args, sizeof(struct sep_driver_get_time_t)))
+                       return -EFAULT;
+       return 0;
 
-       /* set the global flag */
-       sep_dev->block_mode_flag = mode_flag;
-end_function:
-       dbg("SEP Driver:<-------- sep_set_api_mode_handler end\n");
-       return error;
 }
 
 /*
   This API handles the end transaction request
 */
-static int sep_end_transaction_handler(unsigned long arg)
+static int sep_end_transaction_handler(struct sep_device *sep, unsigned long arg)
 {
        dbg("SEP Driver:--------> sep_end_transaction_handler start\n");
 
 #if 0                          /*!SEP_DRIVER_POLLING_MODE */
        /* close IMR */
-       sep_write_reg(sep_dev, HW_HOST_IMR_REG_ADDR, 0x7FFF);
+       sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, 0x7FFF);
 
        /* release IRQ line */
-       free_irq(SEP_DIRVER_IRQ_NUM, &sep_dev->reg_base_address);
+       free_irq(SEP_DIRVER_IRQ_NUM, sep);
 
        /* lock the sep mutex */
        mutex_unlock(&sep_mutex);
@@ -2193,124 +2206,110 @@ static int sep_end_transaction_handler(unsigned long arg)
 }
 
 
-/*
-  This function handler the set flow id command
-*/
-static int sep_set_flow_id_handler(unsigned long arg)
+/**
+ *     sep_set_flow_id_handler -       handle flow setting
+ *     @sep: the SEP we are configuring
+ *     @flow_id: the flow we are setting
+ *
+ * This function handler the set flow id command
+ */
+static int sep_set_flow_id_handler(struct sep_device *sep,
+                                               unsigned long flow_id)
 {
-       int error;
-       unsigned long flow_id;
+       int error = 0;
        struct sep_flow_context_t *flow_data_ptr;
 
-       dbg("------------>SEP Driver: sep_set_flow_id_handler start\n");
-
-       error = get_user(flow_id, &(((struct sep_driver_set_flow_id_t *) arg)->flow_id));
-       if (error)
-               goto end_function;
-
        /* find the flow data structure that was just used for creating new flow
           - its id should be default */
-       error = sep_find_flow_context(SEP_TEMP_FLOW_ID, &flow_data_ptr);
-       if (error)
-               goto end_function;
-
-       /* set flow id */
-       flow_data_ptr->flow_id = flow_id;
 
-end_function:
-       dbg("SEP Driver:<-------- sep_set_flow_id_handler end\n");
+       mutex_lock(&sep_mutex);
+       flow_data_ptr = sep_find_flow_context(sep, SEP_TEMP_FLOW_ID);
+       if (flow_data_ptr)
+               flow_data_ptr->flow_id = flow_id;       /* set flow id */
+       else
+               error = -EINVAL;
+       mutex_unlock(&sep_mutex);
        return error;
 }
 
-
-
-
-
 static int sep_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
 {
        int error = 0;
+       struct sep_device *sep = filp->private_data;
 
        dbg("------------>SEP Driver: ioctl start\n");
 
        edbg("SEP Driver: cmd is %x\n", cmd);
 
-       /* check that the command is for sep device */
-       if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER)
-               error = -ENOTTY;
-
        switch (cmd) {
        case SEP_IOCSENDSEPCOMMAND:
                /* send command to SEP */
-               sep_send_command_handler();
+               sep_send_command_handler(sep);
                edbg("SEP Driver: after sep_send_command_handler\n");
                break;
        case SEP_IOCSENDSEPRPLYCOMMAND:
                /* send reply command to SEP */
-               sep_send_reply_command_handler();
+               sep_send_reply_command_handler(sep);
                break;
        case SEP_IOCALLOCDATAPOLL:
                /* allocate data pool */
-               error = sep_allocate_data_pool_memory_handler(arg);
+               error = sep_allocate_data_pool_memory_handler(sep, arg);
                break;
        case SEP_IOCWRITEDATAPOLL:
                /* write data into memory pool */
-               error = sep_write_into_data_pool_handler(arg);
+               error = sep_write_into_data_pool_handler(sep, arg);
                break;
        case SEP_IOCREADDATAPOLL:
                /* read data from data pool into application memory */
-               error = sep_read_from_data_pool_handler(arg);
+               error = sep_read_from_data_pool_handler(sep, arg);
                break;
        case SEP_IOCCREATESYMDMATABLE:
                /* create dma table for synhronic operation */
-               error = sep_create_sync_dma_tables_handler(arg);
+               error = sep_create_sync_dma_tables_handler(sep, arg);
                break;
        case SEP_IOCCREATEFLOWDMATABLE:
                /* create flow dma tables */
-               error = sep_create_flow_dma_tables_handler(arg);
+               error = sep_create_flow_dma_tables_handler(sep, arg);
                break;
        case SEP_IOCFREEDMATABLEDATA:
                /* free the pages */
-               error = sep_free_dma_table_data_handler();
+               error = sep_free_dma_table_data_handler(sep);
                break;
        case SEP_IOCSETFLOWID:
                /* set flow id */
-               error = sep_set_flow_id_handler(arg);
+               error = sep_set_flow_id_handler(sep, (unsigned long)arg);
                break;
        case SEP_IOCADDFLOWTABLE:
                /* add tables to the dynamic flow */
-               error = sep_add_flow_tables_handler(arg);
+               error = sep_add_flow_tables_handler(sep, arg);
                break;
        case SEP_IOCADDFLOWMESSAGE:
                /* add message of add tables to flow */
-               error = sep_add_flow_tables_message_handler(arg);
+               error = sep_add_flow_tables_message_handler(sep, arg);
                break;
        case SEP_IOCSEPSTART:
                /* start command to sep */
-               error = sep_start_handler();
+               error = sep_start_handler(sep);
                break;
        case SEP_IOCSEPINIT:
                /* init command to sep */
-               error = sep_init_handler(arg);
-               break;
-       case SEP_IOCSETAPIMODE:
-               /* set non- blocking mode */
-               error = sep_set_api_mode_handler(arg);
+               error = sep_init_handler(sep, arg);
                break;
        case SEP_IOCGETSTATICPOOLADDR:
                /* get the physical and virtual addresses of the static pool */
-               error = sep_get_static_pool_addr_handler(arg);
+               error = sep_get_static_pool_addr_handler(sep, arg);
                break;
        case SEP_IOCENDTRANSACTION:
-               error = sep_end_transaction_handler(arg);
+               error = sep_end_transaction_handler(sep, arg);
                break;
        case SEP_IOCREALLOCCACHERES:
-               error = sep_realloc_cache_resident_handler(arg);
+               error = sep_realloc_cache_resident_handler(sep, arg);
                break;
        case SEP_IOCGETMAPPEDADDROFFSET:
-               error = sep_get_physical_mapped_offset_handler(arg);
+               error = sep_get_physical_mapped_offset_handler(sep, arg);
                break;
        case SEP_IOCGETIME:
-               error = sep_get_time_handler(arg);
+               error = sep_get_time_handler(sep, arg);
                break;
        default:
                error = -ENOTTY;
@@ -2347,9 +2346,9 @@ static void sep_flow_done_handler(struct work_struct *work)
           flag may be checked */
        if (flow_data_ptr->input_tables_flag) {
                /* copy the message to the shared RAM and signal SEP */
-               memcpy((void *) flow_data_ptr->message, (void *) sep_dev->shared_area_addr, flow_data_ptr->message_size_in_bytes);
+               memcpy((void *) flow_data_ptr->message, (void *) sep->shared_addr, flow_data_ptr->message_size_in_bytes);
 
-               sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR2_REG_ADDR, 0x2);
+               sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR, 0x2);
        }
        mutex_unlock(&sep_mutex);
 }
@@ -2359,40 +2358,38 @@ static void sep_flow_done_handler(struct work_struct *work)
 static irqreturn_t sep_inthandler(int irq, void *dev_id)
 {
        irqreturn_t int_error;
-       unsigned long error;
        unsigned long reg_val;
        unsigned long flow_id;
        struct sep_flow_context_t *flow_context_ptr;
+       struct sep_device *sep = dev_id;
 
        int_error = IRQ_HANDLED;
 
        /* read the IRR register to check if this is SEP interrupt */
-       reg_val = sep_read_reg(sep_dev, HW_HOST_IRR_REG_ADDR);
+       reg_val = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
        edbg("SEP Interrupt - reg is %08lx\n", reg_val);
 
        /* check if this is the flow interrupt */
        if (0 /*reg_val & (0x1 << 11) */ ) {
                /* read GPRO to find out the which flow is done */
-               flow_id = sep_read_reg(sep_dev, HW_HOST_IRR_REG_ADDR);
+               flow_id = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
 
                /* find the contex of the flow */
-               error = sep_find_flow_context(flow_id >> 28, &flow_context_ptr);
-               if (error)
+               flow_context_ptr = sep_find_flow_context(sep, flow_id >> 28);
+               if (flow_context_ptr == NULL)
                        goto end_function_with_error;
 
-               INIT_WORK(&flow_context_ptr->flow_wq, sep_flow_done_handler);
-
                /* queue the work */
-               queue_work(sep_dev->flow_wq_ptr, &flow_context_ptr->flow_wq);
+               INIT_WORK(&flow_context_ptr->flow_wq, sep_flow_done_handler);
+               queue_work(sep->flow_wq, &flow_context_ptr->flow_wq);
 
        } else {
                /* check if this is reply interrupt from SEP */
                if (reg_val & (0x1 << 13)) {
                        /* update the counter of reply messages */
-                       sep_dev->sep_to_host_reply_counter++;
-
+                       sep->reply_ct++;
                        /* wake up the waiting process */
-                       wake_up(&g_sep_event);
+                       wake_up(&sep_event);
                } else {
                        int_error = IRQ_NONE;
                        goto end_function;
@@ -2400,7 +2397,7 @@ static irqreturn_t sep_inthandler(int irq, void *dev_id)
        }
 end_function_with_error:
        /* clear the interrupt */
-       sep_write_reg(sep_dev, HW_HOST_ICR_REG_ADDR, reg_val);
+       sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, reg_val);
 end_function:
        return int_error;
 }
@@ -2409,54 +2406,63 @@ end_function:
 
 
 
+#if 0
 
-
-static void sep_wait_busy(struct sep_device *dev)
+static void sep_wait_busy(struct sep_device *sep)
 {
        u32 reg;
 
        do {
-               reg = sep_read_reg(sep_dev, HW_HOST_SEP_BUSY_REG_ADDR);
+               reg = sep_read_reg(sep, HW_HOST_SEP_BUSY_REG_ADDR);
        } while (reg);
 }
 
 /*
   PATCH for configuring the DMA to single burst instead of multi-burst
 */
-static void sep_configure_dma_burst(void)
+static void sep_configure_dma_burst(struct sep_device *sep)
 {
 #define         HW_AHB_RD_WR_BURSTS_REG_ADDR            0x0E10UL
 
        dbg("SEP Driver:<-------- sep_configure_dma_burst start \n");
 
        /* request access to registers from SEP */
-       sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
+       sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
 
        dbg("SEP Driver:<-------- sep_configure_dma_burst finished request access to registers from SEP (write reg)  \n");
 
-       sep_wait_busy(sep_dev);
+       sep_wait_busy(sep);
 
        dbg("SEP Driver:<-------- sep_configure_dma_burst finished request access to registers from SEP (while(revVal) wait loop)  \n");
 
        /* set the DMA burst register to single burst */
-       sep_write_reg(sep_dev, HW_AHB_RD_WR_BURSTS_REG_ADDR, 0x0UL);
+       sep_write_reg(sep, HW_AHB_RD_WR_BURSTS_REG_ADDR, 0x0UL);
 
        /* release the sep busy */
-       sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x0UL);
-       sep_wait_busy(sep_dev);
+       sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x0UL);
+       sep_wait_busy(sep);
 
        dbg("SEP Driver:<-------- sep_configure_dma_burst done  \n");
 
 }
 
+#endif
+
 /*
-  function that is activaed on the succesfull probe of the SEP device
+  Function that is activaed on the succesful probe of the SEP device
 */
 static int __devinit sep_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
        int error = 0;
+       struct sep_device *sep;
+       int counter;
+       int size;               /* size of memory for allocation */
 
        edbg("Sep pci probe starting\n");
+       if (sep_dev != NULL) {
+               dev_warn(&pdev->dev, "only one SEP supported.\n");
+               return -EBUSY;
+       }
 
        /* enable the device */
        error = pci_enable_device(pdev);
@@ -2466,84 +2472,116 @@ static int __devinit sep_probe(struct pci_dev *pdev, const struct pci_device_id
        }
 
        /* set the pci dev pointer */
-       sep_dev->sep_pci_dev_ptr = pdev;
-
-       /* get the io memory start address */
-       sep_dev->io_memory_start_physical_address = pci_resource_start(pdev, 0);
-       if (!sep_dev->io_memory_start_physical_address) {
-               edbg("SEP Driver error pci resource start\n");
-               goto end_function;
-       }
-
-       /* get the io memory end address */
-       sep_dev->io_memory_end_physical_address = pci_resource_end(pdev, 0);
-       if (!sep_dev->io_memory_end_physical_address) {
-               edbg("SEP Driver error pci resource end\n");
-               goto end_function;
-       }
+       sep_dev = &sep_instance;
+       sep = &sep_instance;
 
-       sep_dev->io_memory_size = sep_dev->io_memory_end_physical_address - sep_dev->io_memory_start_physical_address + 1;
+       edbg("sep->shared_addr = %p\n", sep->shared_addr);
+       /* transaction counter that coordinates the transactions between SEP
+       and HOST */
+       sep->send_ct = 0;
+       /* counter for the messages from sep */
+       sep->reply_ct = 0;
+       /* counter for the number of bytes allocated in the pool
+       for the current transaction */
+       sep->data_pool_bytes_allocated = 0;
 
-       edbg("SEP Driver:io_memory_start_physical_address is %08lx\n", sep_dev->io_memory_start_physical_address);
+       /* calculate the total size for allocation */
+       size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
+           SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_SIZE_IN_BYTES + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES + SEP_DRIVER_FLOW_DMA_TABLES_AREA_SIZE_IN_BYTES + SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES + SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
 
-       edbg("SEP Driver:io_memory_end_phyaical_address is %08lx\n", sep_dev->io_memory_end_physical_address);
+       /* allocate the shared area */
+       if (sep_map_and_alloc_shared_area(sep, size)) {
+               error = -ENOMEM;
+               /* allocation failed */
+               goto end_function_error;
+       }
+       /* now set the memory regions */
+#if (SEP_DRIVER_RECONFIG_MESSAGE_AREA == 1)
+       /* Note: this test section will need moving before it could ever
+          work as the registers are not yet mapped ! */
+       /* send the new SHARED MESSAGE AREA to the SEP */
+       sep_write_reg(sep, HW_HOST_HOST_SEP_GPR1_REG_ADDR, sep->shared_bus);
 
-       edbg("SEP Driver:io_memory_size is %08lx\n", sep_dev->io_memory_size);
+       /* poll for SEP response */
+       retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
+       while (retval != 0xffffffff && retval != sep->shared_bus)
+               retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
 
-       sep_dev->io_memory_start_virtual_address = ioremap_nocache(sep_dev->io_memory_start_physical_address, sep_dev->io_memory_size);
-       if (!sep_dev->io_memory_start_virtual_address) {
-               edbg("SEP Driver error ioremap of io memory\n");
-               goto end_function;
+       /* check the return value (register) */
+       if (retval != sep->shared_bus) {
+               error = -ENOMEM;
+               goto end_function_deallocate_sep_shared_area;
        }
+#endif
+       /* init the flow contextes */
+       for (counter = 0; counter < SEP_DRIVER_NUM_FLOWS; counter++)
+               sep->flows[counter].flow_id = SEP_FREE_FLOW_ID;
 
-       edbg("SEP Driver:io_memory_start_virtual_address is %p\n", sep_dev->io_memory_start_virtual_address);
+       sep->flow_wq = create_singlethread_workqueue("sepflowwq");
+       if (sep->flow_wq == NULL) {
+               error = -ENOMEM;
+               edbg("sep_driver:flow queue creation failed\n");
+               goto end_function_deallocate_sep_shared_area;
+       }
+       edbg("SEP Driver: create flow workqueue \n");
+       sep->pdev = pci_dev_get(pdev);
 
-       sep_dev->reg_base_address = (void __iomem *) sep_dev->io_memory_start_virtual_address;
+       sep->reg_addr = pci_ioremap_bar(pdev, 0);
+       if (!sep->reg_addr) {
+               edbg("sep: ioremap of registers failed.\n");
+               goto end_function_deallocate_sep_shared_area;
+       }
+       edbg("SEP Driver:reg_addr is %p\n", sep->reg_addr);
 
+       /* load the rom code */
+       sep_load_rom_code(sep);
 
        /* set up system base address and shared memory location */
+       sep->rar_addr = dma_alloc_coherent(&sep->pdev->dev,
+                       2 * SEP_RAR_IO_MEM_REGION_SIZE,
+                       &sep->rar_bus, GFP_KERNEL);
 
-       sep_dev->rar_virtual_address = kmalloc(2 * SEP_RAR_IO_MEM_REGION_SIZE, GFP_KERNEL);
-
-       if (!sep_dev->rar_virtual_address) {
-               edbg("SEP Driver:cant kmalloc rar\n");
-               goto end_function;
+       if (!sep->rar_addr) {
+               edbg("SEP Driver:can't allocate rar\n");
+               goto end_function_uniomap;
        }
-       /* FIXME */
-       sep_dev->rar_physical_address = __pa(sep_dev->rar_virtual_address);
 
-       edbg("SEP Driver:rar_physical is %08lx\n", sep_dev->rar_physical_address);
-       edbg("SEP Driver:rar_virtual is %p\n", sep_dev->rar_virtual_address);
+
+       edbg("SEP Driver:rar_bus is %08llx\n", (unsigned long long)sep->rar_bus);
+       edbg("SEP Driver:rar_virtual is %p\n", sep->rar_addr);
 
 #if !SEP_DRIVER_POLLING_MODE
 
        edbg("SEP Driver: about to write IMR and ICR REG_ADDR\n");
 
        /* clear ICR register */
-       sep_write_reg(sep_dev, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);
+       sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);
 
        /* set the IMR register - open only GPR 2 */
-       sep_write_reg(sep_dev, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
-
-       /* figure out our irq */
-       /* FIXME: */
-       error = pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, (u8 *) & sep_dev->sep_irq);
-
-       edbg("SEP Driver: my irq is %d\n", sep_irq);
+       sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
 
        edbg("SEP Driver: about to call request_irq\n");
        /* get the interrupt line */
-       error = request_irq(sep_irq, sep_inthandler, IRQF_SHARED, "sep_driver", &sep_dev->reg_base_address);
+       error = request_irq(pdev->irq, sep_inthandler, IRQF_SHARED, "sep_driver", sep);
        if (error)
-               goto end_function;
-
-       goto end_function;
+               goto end_function_free_res;
+       return 0;
        edbg("SEP Driver: about to write IMR REG_ADDR");
 
        /* set the IMR register - open only GPR 2 */
-       sep_write_reg(sep_dev, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
+       sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
 
+end_function_free_res:
+       dma_free_coherent(&sep->pdev->dev, 2 * SEP_RAR_IO_MEM_REGION_SIZE,
+                       sep->rar_addr, sep->rar_bus);
 #endif                         /* SEP_DRIVER_POLLING_MODE */
+end_function_uniomap:
+       iounmap(sep->reg_addr);
+end_function_deallocate_sep_shared_area:
+       /* de-allocate shared area */
+       sep_unmap_and_free_shared_area(sep, size);
+end_function_error:
+       sep_dev = NULL;
 end_function:
        return error;
 }
@@ -2560,6 +2598,7 @@ static struct pci_driver sep_pci_driver = {
        .name = "sep_sec_driver",
        .id_table = sep_pci_id_tbl,
        .probe = sep_probe
+       /* FIXME: remove handler */
 };
 
 /* major and minor device numbers */
@@ -2586,31 +2625,22 @@ static int sep_register_driver_to_fs(void)
 {
        int ret_val = alloc_chrdev_region(&sep_devno, 0, 1, "sep_sec_driver");
        if (ret_val) {
-               edbg("sep_driver:major number allocation failed, retval is %d\n", ret_val);
-               goto end_function;
+               edbg("sep: major number allocation failed, retval is %d\n",
+                                                               ret_val);
+               return ret_val;
        }
-
        /* init cdev */
        cdev_init(&sep_cdev, &sep_file_operations);
        sep_cdev.owner = THIS_MODULE;
 
        /* register the driver with the kernel */
        ret_val = cdev_add(&sep_cdev, sep_devno, 1);
-
        if (ret_val) {
                edbg("sep_driver:cdev_add failed, retval is %d\n", ret_val);
-               goto end_function_unregister_devnum;
+               /* unregister dev numbers */
+               unregister_chrdev_region(sep_devno, 1);
        }
-
-       goto end_function;
-
-end_function_unregister_devnum:
-
-       /* unregister dev numbers */
-       unregister_chrdev_region(sep_devno, 1);
-
-end_function:
-      return ret_val;
+       return ret_val;
 }
 
 
@@ -2620,93 +2650,25 @@ end_function:
 static int __init sep_init(void)
 {
        int ret_val = 0;
-       int counter;
-       int size;               /* size of memory for allocation */
-
        dbg("SEP Driver:-------->Init start\n");
-       edbg("sep->shared_area_addr = %lx\n", (unsigned long) &sep_dev->shared_area_addr);
-
-       /* transaction counter that coordinates the transactions between SEP
-       and HOST */
-       sep_dev->host_to_sep_send_counter = 0;
-
-       /* counter for the messages from sep */
-       sep_dev->sep_to_host_reply_counter = 0;
-
-       /* counter for the number of bytes allocated in the pool
-       for the current transaction */
-       sep_dev->data_pool_bytes_allocated = 0;
-
-       /* set the starting mode to blocking */
-       sep_dev->block_mode_flag = 1;
-
        /* FIXME: Probe can occur before we are ready to survive a probe */
        ret_val = pci_register_driver(&sep_pci_driver);
        if (ret_val) {
                edbg("sep_driver:sep_driver_to_device failed, ret_val is %d\n", ret_val);
                goto end_function_unregister_from_fs;
        }
-       /* calculate the total size for allocation */
-       size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
-           SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_SIZE_IN_BYTES + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES + SEP_DRIVER_FLOW_DMA_TABLES_AREA_SIZE_IN_BYTES + SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES + SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
-
-       /* allocate the shared area */
-       if (sep_map_and_alloc_shared_area(size, &sep_dev->shared_area_addr, &sep_dev->phys_shared_area_addr)) {
-               ret_val = -ENOMEM;
-               /* allocation failed */
-               goto end_function_unmap_io_memory;
-       }
-       /* now set the memory regions */
-       sep_dev->message_shared_area_addr = sep_dev->shared_area_addr;
-
-       edbg("SEP Driver: g_message_shared_area_addr is %08lx\n", sep_dev->message_shared_area_addr);
-
-#if (SEP_DRIVER_RECONFIG_MESSAGE_AREA == 1)
-       /* send the new SHARED MESSAGE AREA to the SEP */
-       sep_write_reg(sep_dev, HW_HOST_HOST_SEP_GPR1_REG_ADDR, sep_dev->phys_shared_area_addr);
-
-       /* poll for SEP response */
-       retVal = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
-       while (retVal != 0xffffffff && retVal != sep_dev->phys_shared_area_addr)
-               retVal = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
-
-       /* check the return value (register) */
-       if (retVal != sep_dev->phys_shared_area_addr) {
-               ret_val = -ENOMEM;
-               goto end_function_deallocate_message_area;
-       }
-#endif
-       /* init the flow contextes */
-       for (counter = 0; counter < SEP_DRIVER_NUM_FLOWS; counter++)
-               sep_dev->flows_data_array[counter].flow_id = SEP_FREE_FLOW_ID;
-
-       sep_dev->flow_wq_ptr = create_singlethread_workqueue("sepflowwq");
-       if (sep_dev->flow_wq_ptr == 0) {
-               ret_val = -ENOMEM;
-               edbg("sep_driver:flow queue creation failed\n");
-               goto end_function_deallocate_sep_shared_area;
-       }
-       edbg("SEP Driver: create flow workqueue \n");
-
        /* register driver to fs */
        ret_val = sep_register_driver_to_fs();
        if (ret_val)
-               goto end_function_deallocate_sep_shared_area;
-       /* load the rom code */
-       sep_load_rom_code();
+               goto end_function_unregister_pci;
        goto end_function;
+end_function_unregister_pci:
+       pci_unregister_driver(&sep_pci_driver);
 end_function_unregister_from_fs:
        /* unregister from fs */
        cdev_del(&sep_cdev);
        /* unregister dev numbers */
        unregister_chrdev_region(sep_devno, 1);
-end_function_deallocate_sep_shared_area:
-       /* de-allocate shared area */
-       sep_unmap_and_free_shared_area(size, sep_dev->shared_area_addr, sep_dev->phys_shared_area_addr);
-end_function_unmap_io_memory:
-       iounmap((void *) sep_dev->reg_base_address);
-       /* release io memory region */
-       release_mem_region(SEP_IO_MEM_REGION_START_ADDRESS, SEP_IO_MEM_REGION_SIZE);
 end_function:
        dbg("SEP Driver:<-------- Init end\n");
        return ret_val;
@@ -2729,13 +2691,14 @@ static void __exit sep_exit(void)
        /* calculate the total size for de-allocation */
        size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
            SEP_DRIVER_SYNCHRONIC_DMA_TABLES_AREA_SIZE_IN_BYTES + SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES + SEP_DRIVER_FLOW_DMA_TABLES_AREA_SIZE_IN_BYTES + SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES + SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
+       /* FIXME: We need to do this in the unload for the device */
        /* free shared area  */
-       sep_unmap_and_free_shared_area(size, sep_dev->shared_area_addr, sep_dev->phys_shared_area_addr);
-       edbg("SEP Driver: free pages SEP SHARED AREA \n");
-       iounmap((void *) sep_dev->reg_base_address);
-       edbg("SEP Driver: iounmap \n");
-       /* release io memory region */
-       release_mem_region(SEP_IO_MEM_REGION_START_ADDRESS, SEP_IO_MEM_REGION_SIZE);
+       if (sep_dev) {
+               sep_unmap_and_free_shared_area(sep_dev, size);
+               edbg("SEP Driver: free pages SEP SHARED AREA \n");
+               iounmap((void *) sep_dev->reg_addr);
+               edbg("SEP Driver: iounmap \n");
+       }
        edbg("SEP Driver: release_mem_region \n");
        dbg("SEP Driver:<-------- Exit end\n");
 }