Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

vipregistermem.c

Go to the documentation of this file.
00001 /********************************************************
00002  * Myricom VI-GM  networking software and documentation *
00003  * Copyright (c) 2001 by Myricom, Inc.                  *
00004  * All rights reserved.                                 *
00005  * See the file `COPYING' for copyright notice.         *
00006  ********************************************************/
00007 
00013 #include "vipl_priv.h"
00014 
00015 
00067 VIP_ENTRY_POINT VIP_RETURN
00068 VipRegisterMem (VIP_NIC_HANDLE NicHandle,
00069                 VIP_PVOID VirtualAddress,
00070                 VIP_ULONG Length,
00071                 VIP_MEM_ATTRIBUTES * MemAttribs,
00072                 VIP_MEM_HANDLE * MemoryHandle)
00073 {
00074   VIP_NIC *vip_nic_ptr;
00075   VIP_MEM *vip_mem_ptr;
00076   VIP_GM *vip_gm_ptr;
00077   VIP_PTAG *vip_ptag_ptr;
00078   VIP_UINTPTR start;
00079   VIP_UINTPTR end;
00080   VIP_UINT32 registered;
00081   VIP_MEM_HANDLE vip_mem_handle;
00082   VIP_DEBUG_LABEL (("VipRegisterMem"));
00083 
00084 
00085   if (MemoryHandle == NULL)
00086     {
00087       VIP_DEBUG (("MemoryHandle is null"));
00088       return VIP_INVALID_PARAMETER;
00089     }
00090   *MemoryHandle = (VIP_MEM_HANDLE) NULL;
00091 
00092   if (VirtualAddress == NULL)
00093     {
00094       VIP_DEBUG (("VirtualAddress is null"));
00095       return VIP_INVALID_PARAMETER;
00096     }
00097 
00098   if (Length == 0)
00099     {
00100       VIP_DEBUG (("Length is 0"));
00101       return VIP_INVALID_PARAMETER;
00102     }
00103 
00104   if (Length > VI_GM_MAX_REGISTER_BLOCK_BYTES)
00105     {
00106       VIP_DEBUG (("Length > VI_GM_MAX_REGISTER_BLOCK_BYTES"));
00107       return VIP_ERROR_RESOURCE;
00108     }
00109 
00110   if (MemAttribs == NULL)
00111     {
00112       VIP_DEBUG (("MemAttribs is null"));
00113       return VIP_INVALID_PARAMETER;
00114     }
00115 
00116   if (MemAttribs->EnableRdmaRead != VI_GM_ENABLE_RDMAREAD)
00117     {
00118       VIP_DEBUG (("Invalid EnableRdmaRead in MemAttribs"));
00119       return VIP_INVALID_RDMAREAD;
00120     }
00121 
00122   if ((MemAttribs->EnableRdmaWrite != VIP_TRUE)
00123       && (MemAttribs->EnableRdmaWrite != VIP_FALSE))
00124     {
00125       VIP_DEBUG (("Invalid EnableRdmaWrite in MemAttribs"));
00126       return VIP_INVALID_PARAMETER;
00127     }
00128 
00129   /* check nic handle */
00130   vip_nic_ptr = (VIP_NIC *) NicHandle;
00131   if (VIP_INVALID_NIC_HANDLE (vip_nic_ptr))
00132     {
00133       VIP_DEBUG (("Invalid NIC handle"));
00134       return VIP_INVALID_PARAMETER;
00135     }
00136   vip_gm_ptr = vip_nic_ptr->vip_gm_ptr;
00137   VIP_MUTEX_LOCK (&(vip_gm_ptr->lock));
00138   VIP_PROGRESSION (vip_gm_ptr);
00139   VIP_MUTEX_LOCK (&(vip_nic_ptr->lock));
00140 
00141   /* check limit */
00142   if ((vip_gm_ptr->memory_registered + Length) > VI_GM_MAX_REGISTER_BYTES)
00143     {
00144       VIP_DEBUG (("VI_GM_MAX_REGISTER_BYTES reached"));
00145       VIP_MUTEX_UNLOCK (&(vip_nic_ptr->lock));
00146       VIP_MUTEX_UNLOCK (&(vip_gm_ptr->lock));
00147       return VIP_ERROR_RESOURCE;
00148     }
00149 
00150   /* check ptag */
00151   vip_ptag_ptr = (VIP_PTAG *) MemAttribs->Ptag;
00152   if (VIP_INVALID_PTAG_HANDLE (vip_ptag_ptr))
00153     {
00154       VIP_DEBUG (("Invalid PTAG handle"));
00155       VIP_MUTEX_UNLOCK (&(vip_nic_ptr->lock));
00156       VIP_MUTEX_UNLOCK (&(vip_gm_ptr->lock));
00157       return VIP_INVALID_PTAG;
00158     }
00159 
00160   /* check ptag's nic */
00161   if (vip_ptag_ptr->handle.vip_nic_ptr != vip_nic_ptr)
00162     {
00163       VIP_DEBUG (("Invalid NIC in PTAG"));
00164       VIP_MUTEX_UNLOCK (&(vip_nic_ptr->lock));
00165       VIP_MUTEX_UNLOCK (&(vip_gm_ptr->lock));
00166       return VIP_INVALID_PTAG;
00167     }
00168 
00169   /* allocate regmem */
00170   vip_mem_ptr = (VIP_MEM *) vip_allocate_handle (&(vip_nic_ptr->vip_mem_set));
00171   if (vip_mem_ptr == NULL)
00172     {
00173       VIP_DEBUG (("VIP_MEM alloc failed"));
00174       VIP_MUTEX_UNLOCK (&(vip_nic_ptr->lock));
00175       VIP_MUTEX_UNLOCK (&(vip_gm_ptr->lock));
00176       return VIP_ERROR_RESOURCE;
00177     }
00178 
00179   /* try to register memory, using the AVL tree as a cache */
00180   start = ((VIP_UINTPTR) VirtualAddress) & ~((VIP_UINTPTR) GM_PAGE_LEN - 1);
00181   end = ((((VIP_UINTPTR) VirtualAddress) 
00182           + Length + GM_PAGE_LEN - 1) & ~((VIP_UINTPTR) GM_PAGE_LEN - 1));
00183   registered = vip_avl_register_memory (vip_gm_ptr, start, end);
00184   VIP_ASSERT (registered <= (end - start));
00185   if (registered < (end - start))
00186     {
00187       vip_avl_deregister_memory (vip_gm_ptr, start, start + registered);
00188       VIP_DEBUG (("Memory registration failed"));
00189       vip_free_handle (&(vip_nic_ptr->vip_mem_set),
00190                        (VIP_HANDLE *) vip_mem_ptr);
00191       VIP_MUTEX_UNLOCK (&(vip_nic_ptr->lock));
00192       VIP_MUTEX_UNLOCK (&(vip_gm_ptr->lock));
00193       return VIP_ERROR_RESOURCE;
00194     }
00195 
00196   /* increment ptag reference counter */
00197   vip_ptag_ptr->handle.ref_count++;
00198 
00199   /* update the amount of registered memory */
00200   vip_gm_ptr->memory_registered += Length;
00201 
00202   vip_mem_ptr->address = VirtualAddress;
00203   vip_mem_ptr->vip_ptag_ptr = MemAttribs->Ptag;
00204   vip_mem_ptr->rdma_write = MemAttribs->EnableRdmaWrite;
00205   vip_mem_ptr->length = Length;
00206 
00207   /* register the mem handle pointer into the mem handle tab */
00208   vip_mem_handle = vip_nic_ptr->vip_mem_handle_free;
00209   vip_nic_ptr->vip_mem_handle_free = (VIP_MEM_HANDLE) 
00210     (((VIP_UINTPTR) (vip_nic_ptr->vip_mem_handle_ptrs[vip_mem_handle])) 
00211      & 0xFFFFFFFF);
00212   vip_nic_ptr->vip_mem_handle_ptrs[vip_mem_handle] = vip_mem_ptr;
00213 
00214   *MemoryHandle = vip_mem_handle;
00215   VIP_DEBUG (("Success"));
00216   VIP_MUTEX_UNLOCK (&(vip_nic_ptr->lock));
00217   VIP_MUTEX_UNLOCK (&(vip_gm_ptr->lock));
00218   return VIP_SUCCESS;
00219 }
VI-GM-1.3 by Myricom © 1997-2006. Documentation generated on 20 May 2006 by doxygen 1.4.4.