wlan: Change AID pool to peerIndex pool and sessionize it.

Assign proper index for TDLS station

Change-Id: I24bcc47db9dbe6bc5e7a22fd245363c68180a500
CR-Fixed: 431092
diff --git a/CORE/MAC/src/pe/lim/limAIDmgmt.c b/CORE/MAC/src/pe/lim/limAIDmgmt.c
index 519e849..85fea83 100644
--- a/CORE/MAC/src/pe/lim/limAIDmgmt.c
+++ b/CORE/MAC/src/pe/lim/limAIDmgmt.c
@@ -59,17 +59,12 @@
 #include "limUtils.h"
 #include "limTimerUtils.h"
 #include "limSession.h"
+#include "limSessionUtils.h"
 
-#define LIM_START_AID   1
-#ifdef FEATURE_WLAN_TDLS_INTERNAL 
-#define LIM_START_AID_STA   2
-#else
-#define LIM_START_AID_STA   1
-#endif
-
+#define LIM_START_PEER_IDX   1
 
 /**
- * limInitAIDpool()
+ * limInitPeerIdxpool()
  *
  *FUNCTION:
  * This function is called while starting a BSS at AP
@@ -90,31 +85,46 @@
  */
 
 void
-limInitAIDpool(tpAniSirGlobal pMac,tpPESession sessionEntry)
+limInitPeerIdxpool(tpAniSirGlobal pMac,tpPESession pSessionEntry)
 {
     tANI_U8 i;
     tANI_U8 maxAssocSta = pMac->lim.gLimAssocStaLimit;
 
-    pMac->lim.gpLimAIDpool[0]=0;
-    pMac->lim.freeAidHead=LIM_START_AID;
+    pSessionEntry->gpLimPeerIdxpool[0]=0;
 
-    for (i=pMac->lim.freeAidHead;i<maxAssocSta; i++)
+#ifdef FEATURE_WLAN_TDLS
+    //In station role, DPH_STA_HASH_INDEX_PEER (index 1) is reserved for peer
+    //station index corresponding to AP. Avoid choosing that index and get index
+    //starting from (DPH_STA_HASH_INDEX_PEER + 1) (index 2) for TDLS stations;
+    if (pSessionEntry->limSystemRole == eLIM_STA_ROLE )
     {
-        pMac->lim.gpLimAIDpool[i]         = i+1;
+        pSessionEntry->freePeerIdxHead = DPH_STA_HASH_INDEX_PEER + 1;
     }
-    pMac->lim.gpLimAIDpool[i]         =  0;
+    else
+#endif
+    {
+        pSessionEntry->freePeerIdxHead=LIM_START_PEER_IDX;
+    }
 
-    pMac->lim.freeAidTail=i;
+    for (i=pSessionEntry->freePeerIdxHead;i<maxAssocSta; i++)
+    {
+        pSessionEntry->gpLimPeerIdxpool[i]         = i+1;
+    }
+    pSessionEntry->gpLimPeerIdxpool[i]         =  0;
+
+    pSessionEntry->freePeerIdxTail=i;
 
 }
 
 
 /**
- * limAssignAID()
+ * limAssignPeerIdx()
  *
  *FUNCTION:
- * This function is called during Association/Reassociation
+ * This function is called to get a peer station index. This index is
+ * used during Association/Reassociation
  * frame handling to assign association ID (aid) to a STA.
+ * In case of TDLS, this is used to assign a index into the Dph hash entry.
  *
  *LOGIC:
  *
@@ -124,16 +134,17 @@
  *NOTE:
  *
  * @param  pMac - Pointer to Global MAC structure
- * @return aid  - assigned Association ID for STA
+ * @return peerIdx  - assigned peer Station IDx for STA
  */
 
 tANI_U16
-limAssignAID(tpAniSirGlobal pMac)
+limAssignPeerIdx(tpAniSirGlobal pMac, tpPESession pSessionEntry)
 {
-    tANI_U16 aid;
+    tANI_U16 peerId;
 
     // make sure we haven't exceeded the configurable limit on associations
-    if (pMac->lim.gLimNumOfCurrentSTAs >= pMac->lim.gLimAssocStaLimit)
+    // This count is global to ensure that it doesnt exceed the hardware limits.
+    if (peGetCurrentSTAsCount(pMac) >= pMac->lim.gLimAssocStaLimit)
     {
         // too many associations already active
         return 0;
@@ -141,27 +152,27 @@
 
     /* return head of free list */
 
-    if (pMac->lim.freeAidHead)
+    if (pSessionEntry->freePeerIdxHead)
     {
-        aid=pMac->lim.freeAidHead;
-        pMac->lim.freeAidHead=pMac->lim.gpLimAIDpool[pMac->lim.freeAidHead];
-        if (pMac->lim.freeAidHead==0)
-            pMac->lim.freeAidTail=0;
-        pMac->lim.gLimNumOfCurrentSTAs++;
-        //PELOG2(limLog(pMac, LOG2,FL("Assign aid %d, numSta %d, head %d tail %d \n"),aid,pMac->lim.gLimNumOfCurrentSTAs,pMac->lim.freeAidHead,pMac->lim.freeAidTail);)
-        return aid;
+        peerId=pSessionEntry->freePeerIdxHead;
+        pSessionEntry->freePeerIdxHead = pSessionEntry->gpLimPeerIdxpool[pSessionEntry->freePeerIdxHead];
+        if (pSessionEntry->freePeerIdxHead==0)
+            pSessionEntry->freePeerIdxTail=0;
+        pSessionEntry->gLimNumOfCurrentSTAs++;
+        //PELOG2(limLog(pMac, LOG2,FL("Assign aid %d, numSta %d, head %d tail %d \n"),aid,pSessionEntry->gLimNumOfCurrentSTAs,pSessionEntry->freeAidHead,pSessionEntry->freeAidTail);)
+        return peerId;
     }
 
-    return 0; /* no more free aids */
+    return 0; /* no more free peer index */
 }
 
 
 /**
- * limReleaseAID()
+ * limReleasePeerIdx()
  *
  *FUNCTION:
  * This function is called when a STA context is removed
- * at AP (or at a STA in IBSS mode) to return association ID (aid)
+ * at AP (or at a STA in IBSS mode or TDLS) to return peer Index
  * to free pool.
  *
  *LOGIC:
@@ -172,27 +183,27 @@
  *NOTE:
  *
  * @param  pMac - Pointer to Global MAC structure
- * @param  aid - Association ID that need to return to free pool
+ * @param  peerIdx - peer station index that need to return to free pool
  *
  * @return None
  */
 
 void
-limReleaseAID(tpAniSirGlobal pMac, tANI_U16 aid)
+limReleasePeerIdx(tpAniSirGlobal pMac, tANI_U16 peerIdx, tpPESession pSessionEntry)
 {
-    pMac->lim.gLimNumOfCurrentSTAs--;
+    pSessionEntry->gLimNumOfCurrentSTAs--;
 
     /* insert at tail of free list */
-    if (pMac->lim.freeAidTail)
+    if (pSessionEntry->freePeerIdxTail)
     {
-        pMac->lim.gpLimAIDpool[pMac->lim.freeAidTail]=(tANI_U8)aid;
-        pMac->lim.freeAidTail=(tANI_U8)aid;
+        pSessionEntry->gpLimPeerIdxpool[pSessionEntry->freePeerIdxTail]=(tANI_U8)peerIdx;
+        pSessionEntry->freePeerIdxTail=(tANI_U8)peerIdx;
     }
     else
     {
-        pMac->lim.freeAidTail=pMac->lim.freeAidHead=(tANI_U8)aid;
+        pSessionEntry->freePeerIdxTail=pSessionEntry->freePeerIdxHead=(tANI_U8)peerIdx;
     }
-    pMac->lim.gpLimAIDpool[(tANI_U8)aid]=0;
+    pSessionEntry->gpLimPeerIdxpool[(tANI_U8)peerIdx]=0;
     //PELOG2(limLog(pMac, LOG2,FL("Release aid %d, numSta %d, head %d tail %d \n"),aid,pMac->lim.gLimNumOfCurrentSTAs,pMac->lim.freeAidHead,pMac->lim.freeAidTail);)
 
 }
diff --git a/CORE/MAC/src/pe/lim/limApi.c b/CORE/MAC/src/pe/lim/limApi.c
index 44ff896..b7f52f7 100644
--- a/CORE/MAC/src/pe/lim/limApi.c
+++ b/CORE/MAC/src/pe/lim/limApi.c
@@ -212,7 +212,7 @@
     pMac->lim.gLimNumDeferredMsgs = 0;
 
     /// Variable to keep track of number of currently associated STAs
-    pMac->lim.gLimNumOfCurrentSTAs = 0;
+    //pMac->lim.gLimNumOfCurrentSTAs = 0;
     pMac->lim.gLimNumOfAniSTAs = 0;      // count of ANI peers
 
     /// This indicates number of RXed Beacons during HB period
@@ -380,10 +380,12 @@
 
 static void __limInitAssocVars(tpAniSirGlobal pMac)
 {
+#if 0
     palZeroMemory(pMac->hHdd, pMac->lim.gpLimAIDpool,
                   sizeof(*pMac->lim.gpLimAIDpool) * (WNI_CFG_ASSOC_STA_LIMIT_STAMAX+1));
     pMac->lim.freeAidHead = 0;
     pMac->lim.freeAidTail = 0;
+#endif
     pMac->lim.gLimAssocStaLimit = WNI_CFG_ASSOC_STA_LIMIT_STADEF;
 
     // Place holder for current authentication request
@@ -1002,6 +1004,7 @@
         return eSIR_FAILURE;
     }
 
+#if 0
     if (eHAL_STATUS_SUCCESS != palAllocateMemory(pMac->hHdd,
               (void **) &pMac->lim.gpLimAIDpool, 
               sizeof(*pMac->lim.gpLimAIDpool) * (WNI_CFG_ASSOC_STA_LIMIT_STAMAX+1)))
@@ -1009,7 +1012,7 @@
         PELOGE(limLog(pMac, LOGE, FL("memory allocate failed!\n"));)
         return eSIR_FAILURE;
     }
-
+#endif
     if (eHAL_STATUS_SUCCESS != palAllocateMemory(pMac->hHdd,
         (void **) &pMac->lim.gpSession, sizeof(tPESession)* pMac->lim.maxBssId))
     {
@@ -1100,8 +1103,10 @@
     }
     palFreeMemory(pMac->hHdd, pMac->lim.limTimers.gpLimCnfWaitTimer);
     pMac->lim.limTimers.gpLimCnfWaitTimer = NULL;
+#if 0
     palFreeMemory(pMac->hHdd, pMac->lim.gpLimAIDpool);
     pMac->lim.gpLimAIDpool = NULL;
+#endif
     
     palFreeMemory(pMac->hHdd, pMac->lim.gpSession);
     pMac->lim.gpSession = NULL;
diff --git a/CORE/MAC/src/pe/lim/limAssocUtils.c b/CORE/MAC/src/pe/lim/limAssocUtils.c
index 1e5add6..4e8aa7b 100644
--- a/CORE/MAC/src/pe/lim/limAssocUtils.c
+++ b/CORE/MAC/src/pe/lim/limAssocUtils.c
@@ -647,7 +647,7 @@
             if ((psessionEntry->limSystemRole == eLIM_AP_ROLE) || 
                 (psessionEntry->limSystemRole == eLIM_BT_AMP_AP_ROLE))
             {    
-                limReleaseAID(pMac, pStaDs->assocId);
+                limReleasePeerIdx(pMac, pStaDs->assocId, psessionEntry);
             }
             limDeleteDphHashEntry(pMac, pStaDs->staAddr, pStaDs->assocId,psessionEntry);
 
@@ -3918,7 +3918,7 @@
         if ((psessionEntry->limSystemRole == eLIM_AP_ROLE) ||
             (psessionEntry->limSystemRole == eLIM_BT_AMP_AP_ROLE))
         {
-            limReleaseAID(pMac, pStaDs->assocId);
+            limReleasePeerIdx(pMac, pStaDs->assocId, psessionEntry);
         }
 
         limDeleteDphHashEntry(pMac, pStaDs->staAddr, pStaDs->assocId,psessionEntry);
diff --git a/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c b/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c
index 8e65145..8fda56e 100644
--- a/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c
+++ b/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c
@@ -243,13 +243,13 @@
     tLimIbssPeerNode *pPeerNode,
     tpPESession       psessionEntry)
 {
-    tANI_U16      aid;
+    tANI_U16      peerIdx;
     tpDphHashNode pStaDs;
 
     pPeerNode->beaconHBCount++; //Update beacon count.
 
     // if the peer node exists, update its qos capabilities
-    if ((pStaDs = dphLookupHashEntry(pMac, pPeerNode->peerMacAddr, &aid, &psessionEntry->dph.dphHashTable)) == NULL)
+    if ((pStaDs = dphLookupHashEntry(pMac, pPeerNode->peerMacAddr, &peerIdx, &psessionEntry->dph.dphHashTable)) == NULL)
         return;
 
 
@@ -449,12 +449,12 @@
     tpDphHashNode   *ppSta,
     tpPESession     psessionEntry)
 {
-    tANI_U16      aid;
+    tANI_U16      peerIdx;
     tpDphHashNode pStaDs;
 
     *ppSta = NULL;
 
-    pStaDs = dphLookupHashEntry(pMac, peerAddr, &aid, &psessionEntry->dph.dphHashTable);
+    pStaDs = dphLookupHashEntry(pMac, peerAddr, &peerIdx, &psessionEntry->dph.dphHashTable);
     if (pStaDs != NULL)
     {
         /* Trying to add context for already existing STA in IBSS */
@@ -468,20 +468,20 @@
      * AID and then add an entry to hash table maintained
      * by DPH module.
      */
-    aid = limAssignAID(pMac);
+    peerIdx = limAssignPeerIdx(pMac, psessionEntry);
 
-    pStaDs = dphGetHashEntry(pMac, aid, &psessionEntry->dph.dphHashTable);
+    pStaDs = dphGetHashEntry(pMac, peerIdx, &psessionEntry->dph.dphHashTable);
     if (pStaDs)
     {
         (void) limDelSta(pMac, pStaDs, false /*asynchronous*/,psessionEntry);
-        limDeleteDphHashEntry(pMac, pStaDs->staAddr, aid,psessionEntry);
+        limDeleteDphHashEntry(pMac, pStaDs->staAddr, peerIdx,psessionEntry);
     }
 
-    pStaDs = dphAddHashEntry(pMac, peerAddr, aid, &psessionEntry->dph.dphHashTable);
+    pStaDs = dphAddHashEntry(pMac, peerAddr, peerIdx, &psessionEntry->dph.dphHashTable);
     if (pStaDs == NULL)
     {
         // Could not add hash table entry
-        PELOGE(limLog(pMac, LOGE, FL("could not add hash entry at DPH for aid=%d MACaddr:\n"), aid);)
+        PELOGE(limLog(pMac, LOGE, FL("could not add hash entry at DPH for peerIdx/aid=%d MACaddr:\n"), peerIdx);)
         limPrintMacAddr(pMac, peerAddr, LOGE);
         return eSIR_FAILURE;
     }
@@ -740,7 +740,7 @@
 {
     tLimIbssPeerNode    *pCurrNode, *pTempNode;
     tpDphHashNode pStaDs;
-    tANI_U16 aid;
+    tANI_U16 peerIdx;
 
     pCurrNode = pTempNode = pMac->lim.gLimIbssPeerList;
 
@@ -756,14 +756,14 @@
               * Since it is called to remove all peers, just delete from dph,
               * no need to do any beacon related params i.e., dont call limDeleteDphHashEntry
               */
-        pStaDs = dphLookupHashEntry(pMac, pCurrNode->peerMacAddr, &aid, &psessionEntry->dph.dphHashTable);
+        pStaDs = dphLookupHashEntry(pMac, pCurrNode->peerMacAddr, &peerIdx, &psessionEntry->dph.dphHashTable);
         if( pStaDs )
         {
 
             ibss_status_chg_notify( pMac, pCurrNode->peerMacAddr, pStaDs->staIndex, 
                                     pStaDs->ucUcastSig, pStaDs->ucBcastSig,
                                     eWNI_SME_IBSS_PEER_DEPARTED_IND, psessionEntry->smeSessionId );
-            dphDeleteHashEntry(pMac, pStaDs->staAddr, aid, &psessionEntry->dph.dphHashTable);
+            dphDeleteHashEntry(pMac, pStaDs->staAddr, peerIdx, &psessionEntry->dph.dphHashTable);
         }
 
         pTempNode = pCurrNode->next;
@@ -1144,7 +1144,7 @@
     void *msg,tpPESession psessionEntry)
 {
     tpDphHashNode   pStaDs;
-    tANI_U16        aid;
+    tANI_U16        peerIdx;
     tpAddStaParams  pAddStaParams = (tpAddStaParams) msg;
 
     SET_LIM_PROCESS_DEFD_MESGS(pMac, true);
@@ -1154,7 +1154,7 @@
         return eSIR_FAILURE;
     }
 
-    pStaDs = dphLookupHashEntry(pMac, pAddStaParams->staMac, &aid, &psessionEntry->dph.dphHashTable);
+    pStaDs = dphLookupHashEntry(pMac, pAddStaParams->staMac, &peerIdx, &psessionEntry->dph.dphHashTable);
     if (pStaDs == NULL)
     {
         PELOGE(limLog(pMac, LOGE, FL("IBSS: ADD_STA_RSP for unknown MAC addr "));)
@@ -1381,7 +1381,7 @@
     tANI_U16            fTsfLater,
     tpPESession         psessionEntry)
 {
-    tANI_U16            aid;
+    tANI_U16            peerIdx;
     tSirMacAddr         currentBssId;
     tLimIbssPeerNode    *pPeerNode;
     tpDphHashNode       pStaDs;
@@ -1449,7 +1449,7 @@
         }
         ibss_peer_add(pMac, pPeerNode);
 
-        pStaDs = dphLookupHashEntry(pMac, pPeerNode->peerMacAddr, &aid, &psessionEntry->dph.dphHashTable);
+        pStaDs = dphLookupHashEntry(pMac, pPeerNode->peerMacAddr, &peerIdx, &psessionEntry->dph.dphHashTable);
         if (pStaDs != NULL)
         {
             /// DPH node already exists for the peer
@@ -1466,7 +1466,7 @@
         }
 
         // Decide protection mode
-        pStaDs = dphLookupHashEntry(pMac, pPeerNode->peerMacAddr, &aid, &psessionEntry->dph.dphHashTable);
+        pStaDs = dphLookupHashEntry(pMac, pPeerNode->peerMacAddr, &peerIdx, &psessionEntry->dph.dphHashTable);
         if(pMac->lim.gLimProtectionControl != WNI_CFG_FORCE_POLICY_PROTECTION_DISABLE)
             limIbssDecideProtection(pMac, pStaDs, &beaconParams, psessionEntry);
 
@@ -1506,7 +1506,7 @@
 {
     tLimIbssPeerNode *pTempNode, *pPrevNode;
     tLimIbssPeerNode *pTempNextNode = NULL;
-    tANI_U16      aid;
+    tANI_U16      peerIdx;
     tpDphHashNode pStaDs;
     tANI_U32 threshold;
     tANI_U16 staIndex;
@@ -1550,7 +1550,7 @@
             if(pTempNode->heartbeatFailure >= threshold )
             {
                 //Remove this entry from the list.
-                pStaDs = dphLookupHashEntry(pMac, pTempNode->peerMacAddr, &aid, &psessionEntry->dph.dphHashTable);
+                pStaDs = dphLookupHashEntry(pMac, pTempNode->peerMacAddr, &peerIdx, &psessionEntry->dph.dphHashTable);
                 if (pStaDs)
                 {
                     staIndex = pStaDs->staIndex;
@@ -1558,7 +1558,7 @@
                     ucBcastSig = pStaDs->ucBcastSig;
 
                     (void) limDelSta(pMac, pStaDs, false /*asynchronous*/,psessionEntry);
-                    limDeleteDphHashEntry(pMac, pStaDs->staAddr, aid,psessionEntry);
+                    limDeleteDphHashEntry(pMac, pStaDs->staAddr, peerIdx,psessionEntry);
 
                     //Send indication.
                     ibss_status_chg_notify( pMac, pTempNode->peerMacAddr, staIndex, 
diff --git a/CORE/MAC/src/pe/lim/limLogDump.c b/CORE/MAC/src/pe/lim/limLogDump.c
index be36cd5..b1caa1f 100644
--- a/CORE/MAC/src/pe/lim/limLogDump.c
+++ b/CORE/MAC/src/pe/lim/limLogDump.c
@@ -142,7 +142,7 @@
   else if (pMac->lim.gLimSystemRole == eLIM_AP_ROLE)
   {
       p += log_sprintf( pMac,p, "Num of STAs associated                     = %d\n",
-                      pMac->lim.gLimNumOfCurrentSTAs);
+                      peGetCurrentSTAsCount(pMac));
 
       p += log_sprintf( pMac,p, "Num of Pre-auth contexts                   = %d\n",
                       pMac->lim.gLimNumPreAuthContexts);
@@ -1072,15 +1072,15 @@
     tpDphHashNode pStaDs;
     tpPESession psessionEntry = &pMac->lim.gpSession[0];  //TBD-RAJESH HOW TO GET sessionEntry?????
     tSirMacAddr staMac = {0};
-    tANI_U16 aid;
+    tANI_U16 peerIdx;
     if(arg2 > 5)
       goto addStaFail;
-    aid = limAssignAID(pMac);
-    pStaDs = dphGetHashEntry(pMac, aid);
+    peerIdx = limAssignPeerIdx(pMac, psessionEntry);
+    pStaDs = dphGetHashEntry(pMac, peerIdx);
     if(NULL == pStaDs)
     {
         staMac[5] = (tANI_U8) arg1;
-        pStaDs = dphAddHashEntry(pMac, staMac, aid, &psessionEntry->dph.dphHashTable);
+        pStaDs = dphAddHashEntry(pMac, staMac, peerIdx, &psessionEntry->dph.dphHashTable);
         if(NULL == pStaDs)
           goto addStaFail;
 
diff --git a/CORE/MAC/src/pe/lim/limProcessAssocReqFrame.c b/CORE/MAC/src/pe/lim/limProcessAssocReqFrame.c
index 0c6eb62..643d702 100644
--- a/CORE/MAC/src/pe/lim/limProcessAssocReqFrame.c
+++ b/CORE/MAC/src/pe/lim/limProcessAssocReqFrame.c
@@ -68,6 +68,7 @@
 #include "limStaHashApi.h"
 #include "limAdmitControl.h"
 #include "palApi.h"
+#include "limSessionUtils.h"
 
 
 #include "vos_types.h"
@@ -184,7 +185,7 @@
 {
     tANI_U8                 updateContext;
     tANI_U8                 *pBody;
-    tANI_U16                aid, temp;
+    tANI_U16                peerIdx, temp;
     tANI_U32                val;
     tANI_S32                framelen;
     tSirRetStatus           status;
@@ -817,7 +818,7 @@
      * Extract 'associated' context for STA, if any.
      * This is maintained by DPH and created by LIM.
      */
-    pStaDs = dphLookupHashEntry(pMac, pHdr->sa, &aid, &psessionEntry->dph.dphHashTable);
+    pStaDs = dphLookupHashEntry(pMac, pHdr->sa, &peerIdx, &psessionEntry->dph.dphHashTable);
 
     /// Extract pre-auth context for the STA, if any.
     pStaPreAuthContext = limSearchPreAuthList(pMac, pHdr->sa);
@@ -825,7 +826,7 @@
     if (pStaDs == NULL)
     {
         /// Requesting STA is not currently associated
-        if (pMac->lim.gLimNumOfCurrentSTAs == pMac->lim.maxStation)
+        if (peGetCurrentSTAsCount(pMac) == pMac->lim.maxStation)
         {
             /**
              * Maximum number of STAs that AP can handle reached.
@@ -931,9 +932,9 @@
 
         updateContext = true;
 
-        if (dphInitStaState(pMac, pHdr->sa, aid, true, &psessionEntry->dph.dphHashTable) == NULL)   
+        if (dphInitStaState(pMac, pHdr->sa, peerIdx, true, &psessionEntry->dph.dphHashTable) == NULL)   
         {
-            limLog(pMac, LOGE, FL("could not Init STAid=%d\n"), aid);
+            limLog(pMac, LOGE, FL("could not Init STAid=%d\n"), peerIdx);
             goto  error;
         }
 
@@ -1001,20 +1002,21 @@
                MAC_ADDR_ARRAY(pHdr->sa));
 
     /**
-     * Assign unused/least recently used AID from perStaDs.
-     * This will 12-bit STAid used by MAC HW.
-     * NOTE: limAssignAID() assigns AID values ranging between 1 - 255
+     * AID for this association will be same as the peer Index used in DPH table.
+     * Assign unused/least recently used peer Index from perStaDs.
+     * NOTE: limAssignPeerIdx() assigns AID values ranging 
+     * between 1 - cfg_item(WNI_CFG_ASSOC_STA_LIMIT)
      */
 
-    aid = limAssignAID(pMac);
+    peerIdx = limAssignPeerIdx(pMac, psessionEntry);
 
-    if (!aid)
+    if (!peerIdx)
     {
         // Could not assign AID
         // Reject association
         limRejectAssociation(pMac, pHdr->sa,
                              subType, true, authType,
-                             aid, false,
+                             peerIdx, false,
                              (tSirResultCodes) eSIR_MAC_UNSPEC_FAILURE_STATUS, psessionEntry);
 
         goto error;
@@ -1024,21 +1026,21 @@
      * Add an entry to hash table maintained by DPH module
      */
 
-    pStaDs = dphAddHashEntry(pMac, pHdr->sa, aid, &psessionEntry->dph.dphHashTable);
+    pStaDs = dphAddHashEntry(pMac, pHdr->sa, peerIdx, &psessionEntry->dph.dphHashTable);
 
     if (pStaDs == NULL)
     {
         // Could not add hash table entry at DPH
         limLog(pMac, LOGE,
            FL("could not add hash entry at DPH for aid=%d, MacAddr:\n"),
-           aid);
+           peerIdx);
         limPrintMacAddr(pMac, pHdr->sa, LOGE);
 
         // Release AID
-        limReleaseAID(pMac, aid);
+        limReleasePeerIdx(pMac, peerIdx, psessionEntry);
 
         limRejectAssociation(pMac, pHdr->sa,
-                             subType, true, authType, aid, false,
+                             subType, true, authType, peerIdx, false,
                              (tSirResultCodes) eSIR_MAC_UNSPEC_FAILURE_STATUS, psessionEntry);
 
         goto error;
@@ -1160,15 +1162,15 @@
         // Could not update hash table entry at DPH with rateset
         limLog(pMac, LOGE,
            FL("could not update hash entry at DPH for aid=%d, MacAddr:\n"),
-           aid);
+           peerIdx);
         limPrintMacAddr(pMac, pHdr->sa, LOGE);
 
                 // Release AID
-        limReleaseAID(pMac, aid);
+        limReleasePeerIdx(pMac, peerIdx, psessionEntry);
 
 
         limRejectAssociation(pMac, pHdr->sa,
-                             subType, true, authType, aid, true,
+                             subType, true, authType, peerIdx, true,
                              (tSirResultCodes) eSIR_MAC_UNSPEC_FAILURE_STATUS, psessionEntry);
 
         /*return it from here rather than goto error statement.This is done as the memory is getting free twice*/
@@ -1218,7 +1220,7 @@
                  */
                 limLog( pMac, LOGE, FL( "AP do not support UPASD REASSOC Failed\n" ));
                 limRejectAssociation(pMac, pHdr->sa,
-                                     subType, true, authType, aid, true,
+                                     subType, true, authType, peerIdx, true,
                                      (tSirResultCodes) eSIR_MAC_WME_REFUSED_STATUS, psessionEntry);
 
 
@@ -1410,7 +1412,7 @@
 
         if( eHAL_STATUS_SUCCESS != palAllocateMemory( pMac->hHdd, (void **)&pMlmAssocInd, temp))
         {
-            limReleaseAID(pMac, pStaDs->assocId);
+            limReleasePeerIdx(pMac, pStaDs->assocId, psessionEntry);
             limLog(pMac, LOGP, FL("palAllocateMemory failed for pMlmAssocInd\n"));
             return;
         }
@@ -1444,7 +1446,7 @@
             }
         }
         pMlmAssocInd->assocType = (tSirAssocType)pAssocReq->propIEinfo.assocType;
-        pMlmAssocInd->load.numStas = pMac->lim.gLimNumOfCurrentSTAs;
+        pMlmAssocInd->load.numStas = psessionEntry->gLimNumOfCurrentSTAs;
         pMlmAssocInd->load.channelUtilization =(pMac->lim.gpLimMeasData) ? pMac->lim.gpLimMeasData->avgChannelUtilization : 0;
         pMlmAssocInd->numBss = (tANI_U32) pAssocReq->propIEinfo.numBss;
         if (pAssocReq->propIEinfo.numBss)
@@ -1564,7 +1566,7 @@
         if( eHAL_STATUS_SUCCESS != palAllocateMemory( pMac->hHdd, (void **)&pMlmReassocInd, temp))
         {
             limLog(pMac, LOGP, FL("call to palAllocateMemory failed for pMlmReassocInd\n"));
-            limReleaseAID(pMac, pStaDs->assocId);
+            limReleasePeerIdx(pMac, pStaDs->assocId, psessionEntry);
             return;
         }
         palZeroMemory( pMac->hHdd, pMlmReassocInd, temp);
@@ -1597,7 +1599,7 @@
         }
 
         pMlmReassocInd->reassocType  = (tSirAssocType)pAssocReq->propIEinfo.assocType;
-        pMlmReassocInd->load.numStas = pMac->lim.gLimNumOfCurrentSTAs;
+        pMlmReassocInd->load.numStas = psessionEntry->gLimNumOfCurrentSTAs;
         pMlmReassocInd->load.channelUtilization = (pMac->lim.gpLimMeasData) ?
                                                   pMac->lim.gpLimMeasData->avgChannelUtilization : 0;
         pMlmReassocInd->numBss = (tANI_U32) pAssocReq->propIEinfo.numBss;
diff --git a/CORE/MAC/src/pe/lim/limProcessMlmRspMessages.c b/CORE/MAC/src/pe/lim/limProcessMlmRspMessages.c
index c038348..d196390 100644
--- a/CORE/MAC/src/pe/lim/limProcessMlmRspMessages.c
+++ b/CORE/MAC/src/pe/lim/limProcessMlmRspMessages.c
@@ -2214,7 +2214,7 @@
     limDeletePreAuthList(pMac);
 #ifdef WLAN_SOFTAP_FEATURE
     //Initialize number of associated stations during cleanup
-    pMac->lim.gLimNumOfCurrentSTAs = 0;
+    psessionEntry->gLimNumOfCurrentSTAs = 0;
 #endif
     end:
     limSendSmeRsp(pMac, eWNI_SME_STOP_BSS_RSP, rc,  psessionEntry->smeSessionId,  psessionEntry->transactionId);
@@ -2559,7 +2559,7 @@
 #endif
         schEdcaProfileUpdate(pMac, psessionEntry);
         limInitPreAuthList(pMac);
-        limInitAIDpool(pMac,psessionEntry);
+        limInitPeerIdxpool(pMac,psessionEntry);
         // Create timers used by LIM
         if (!pMac->lim.gLimTimersCreated)
             limCreateTimers(pMac);
@@ -2662,7 +2662,7 @@
         schEdcaProfileUpdate(pMac, psessionEntry);
         //TBD-RAJESH limInitPreauthList should re removed for IBSS also ?????
        //limInitPreAuthList(pMac);
-        limInitAIDpool(pMac,psessionEntry);
+        limInitPeerIdxpool(pMac,psessionEntry);
         // Create timers used by LIM
 #ifdef FIXME_GEN6  //following code may not be required, as limCreateTimers is now invoked from limInitialize (peStart)
         if (!pMac->lim.gLimTimersCreated)
@@ -3217,7 +3217,7 @@
                 PELOGE(limLog(pMac, LOGE, FL("could not Add Self Entry for the station\n"));)
                 mlmAssocCnf.resultCode = (tSirResultCodes) eSIR_SME_REFUSED;
             }
-#ifdef FEATURE_WLAN_TDLS_INTERNAL
+#ifdef FEATURE_WLAN_TDLS
             else {
                /* initialize TDLS peer related data */
                limInitTdlsData(pMac,psessionEntry);
@@ -4870,7 +4870,7 @@
         psessionEntry->statypeForBss = STA_ENTRY_SELF; // to know session started for peer or for self
         psessionEntry->bssIdx = (tANI_U8) pAddBssParams->bssIdx;
         schEdcaProfileUpdate(pMac, psessionEntry);
-        limInitAIDpool(pMac,psessionEntry);
+        limInitPeerIdxpool(pMac,psessionEntry);
         // Create timers used by LIM
         if (!pMac->lim.gLimTimersCreated)
         limCreateTimers(pMac);
diff --git a/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c b/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
index 595aad1..9fd1a34 100644
--- a/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
+++ b/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
@@ -2847,19 +2847,6 @@
 #endif
 
         limPostMlmMessage(pMac, LIM_MLM_SETKEYS_REQ, (tANI_U32 *) pMlmSetKeysReq);
-
-#ifdef ANI_AP_SDK
-        /* For SDK acting as STA under Linux, need to consider the AP as *
-         * as authenticatated.                                           */
-        if ( (psessionEntry->limSystemRole == eLIM_STA_ROLE) &&
-             (psessionEntry->limSmeState == eLIM_SME_LINK_EST_STATE))
-        {
-            tpDphHashNode pSta;
-            pSta = dphGetHashEntry(pMac, 0, &psessionEntry->dph.dphHashTable);
-            if (pSta)
-                pSta->staAuthenticated = 1;
-        }
-#endif
     }
     else
     {
diff --git a/CORE/MAC/src/pe/lim/limProcessTdls.c b/CORE/MAC/src/pe/lim/limProcessTdls.c
index e0cdf93..4a79516 100644
--- a/CORE/MAC/src/pe/lim/limProcessTdls.c
+++ b/CORE/MAC/src/pe/lim/limProcessTdls.c
@@ -229,12 +229,12 @@
     return ;
 }
 #endif
-#ifdef FEATURE_WLAN_TDLS_INTERNAL
 /*
  * initialize TDLS setup list and related data structures.
  */
-void limInitTdlsData(tpAniSirGlobal pMac, tpPESession sessionEntry)
+void limInitTdlsData(tpAniSirGlobal pMac, tpPESession pSessionEntry)
 {
+#ifdef FEATURE_WLAN_TDLS_INTERNAL
     pMac->lim.gLimTdlsDisResultList = NULL ;
     pMac->lim.gLimTdlsDisStaCount = 0 ;
     palZeroMemory(pMac->hHdd, &pMac->lim.gLimTdlsDisReq, 
@@ -248,11 +248,11 @@
     /* you have to explicitly enable negative behavior per (re)association */
     pMac->lim.gLimTdlsNegativeBehavior = 0;
 #endif
-    limInitAIDpool(pMac, sessionEntry) ;
+#endif
+    limInitPeerIdxpool(pMac, pSessionEntry) ;
 
     return ;
 }
-#endif
 #ifdef FEATURE_WLAN_TDLS_NEGATIVE
 void limTdlsSetNegativeBehavior(tpAniSirGlobal pMac, tANI_U8 value, tANI_BOOLEAN on)
 {
@@ -3321,7 +3321,7 @@
         return status ;
     }
 
-    aid = limAssignAID(pMac) ;
+    aid = limAssignPeerIdx(pMac, psessionEntry) ;
 
     pStaDs = dphGetHashEntry(pMac, aid, &psessionEntry->dph.dphHashTable);
 
@@ -3435,8 +3435,13 @@
         VOS_ASSERT(0) ;
         return status ;
     }
-    aid = limAssignAID(pMac) ;
+    aid = limAssignPeerIdx(pMac, psessionEntry) ;
 
+    if( !aid )
+    {
+       //Reject request.
+       return eSIR_FAILURE;
+    }
     VOS_TRACE(VOS_MODULE_ID_PE, VOS_TRACE_LEVEL_INFO, 
           ("Aid = %d, for peer = %02x,%02x,%02x,%02x,%02x,%02x\n"),
                      aid, peerMac[0],peerMac[1],peerMac[2],
@@ -3481,10 +3486,10 @@
                                                     tpPESession psessionEntry)
 {
     tSirRetStatus status = eSIR_SUCCESS ;
-    tANI_U16 aid = 0 ;
+    tANI_U16 peerIdx = 0 ;
     tpDphHashNode pStaDs = NULL ;
  
-    pStaDs = dphLookupHashEntry(pMac, peerMac, &aid, 
+    pStaDs = dphLookupHashEntry(pMac, peerMac, &peerIdx, 
                                          &psessionEntry->dph.dphHashTable) ;
 
     if(pStaDs)
@@ -3507,8 +3512,8 @@
         status = limDelSta(pMac, pStaDs, false, psessionEntry) ;
         if(eSIR_SUCCESS == status)
         {
-            limDeleteDphHashEntry(pMac, pStaDs->staAddr, aid, psessionEntry) ;
-            limReleaseAID(pMac, aid) ;
+            limDeleteDphHashEntry(pMac, pStaDs->staAddr, peerIdx, psessionEntry) ;
+            limReleasePeerIdx(pMac, peerIdx, psessionEntry) ;
         }
         else
         {
diff --git a/CORE/MAC/src/pe/lim/limSerDesUtils.c b/CORE/MAC/src/pe/lim/limSerDesUtils.c
index cad1ff9..dae0ba6 100644
--- a/CORE/MAC/src/pe/lim/limSerDesUtils.c
+++ b/CORE/MAC/src/pe/lim/limSerDesUtils.c
@@ -2644,7 +2644,7 @@
     pBuf += sizeof(tANI_U32);
     len += sizeof(tANI_U32);
 
-    load.numStas            = pMac->lim.gLimNumOfCurrentSTAs;
+    load.numStas            = peGetCurrentSTAsCount(pMac);
     load.channelUtilization =
                         pMac->lim.gpLimMeasData->avgChannelUtilization;
     limCopyLoad(pBuf, load);
diff --git a/CORE/MAC/src/pe/lim/limSession.c b/CORE/MAC/src/pe/lim/limSession.c
index e2d5d22..7d68bb5 100644
--- a/CORE/MAC/src/pe/lim/limSession.c
+++ b/CORE/MAC/src/pe/lim/limSession.c
@@ -147,6 +147,21 @@
             dphHashTableClassInit(pMac, 
                            &pMac->lim.gpSession[i].dph.dphHashTable);
 
+            if (eHAL_STATUS_SUCCESS != palAllocateMemory(pMac->hHdd,
+                    (void **) &pMac->lim.gpSession[i].gpLimPeerIdxpool, 
+                    sizeof(*pMac->lim.gpSession[i].gpLimPeerIdxpool) * (numSta+1)))
+            {
+                PELOGE(limLog(pMac, LOGE, FL("memory allocate failed!\n"));)
+                palFreeMemory(pMac->hHdd,pMac->lim.gpSession[i].dph.dphHashTable.pHashTable);
+                palFreeMemory(pMac->hHdd,pMac->lim.gpSession[i].dph.dphHashTable.pDphNodeArray);
+                return NULL;
+            }
+            palZeroMemory(pMac->hHdd, pMac->lim.gpSession[i].gpLimPeerIdxpool,
+                  sizeof(*pMac->lim.gpSession[i].gpLimPeerIdxpool) * (numSta+1));
+            pMac->lim.gpSession[i].freePeerIdxHead = 0;
+            pMac->lim.gpSession[i].freePeerIdxTail = 0;
+            pMac->lim.gpSession[i].gLimNumOfCurrentSTAs = 0;
+
             /* Copy the BSSID to the session table */
             sirCopyMacAddr(pMac->lim.gpSession[i].bssId, bssid);
             pMac->lim.gpSession[i].valid = TRUE;
@@ -358,6 +373,12 @@
         psessionEntry->dph.dphHashTable.pDphNodeArray = NULL;
     }
 
+    if(psessionEntry->gpLimPeerIdxpool != NULL)
+    {
+        palFreeMemory(pMac->hHdd, psessionEntry->gpLimPeerIdxpool);
+        psessionEntry->gpLimPeerIdxpool = NULL;
+    }
+
     if(psessionEntry->beacon != NULL)
     {
         palFreeMemory( pMac->hHdd, psessionEntry->beacon);
diff --git a/CORE/MAC/src/pe/lim/limSessionUtils.c b/CORE/MAC/src/pe/lim/limSessionUtils.c
index 59328dc..9192318 100644
--- a/CORE/MAC/src/pe/lim/limSessionUtils.c
+++ b/CORE/MAC/src/pe/lim/limSessionUtils.c
@@ -424,3 +424,28 @@
     }
     return FALSE;
 }
+
+/*--------------------------------------------------------------------------
+  \brief peGetCurrentSTAsCount() - Returns total stations associated on 
+                                      all session.
+
+  \param pMac                   - pointer to global adapter context
+  \return                       - Number of station active on all sessions.
+  
+  \sa
+  --------------------------------------------------------------------------*/
+
+tANI_U8 peGetCurrentSTAsCount(tpAniSirGlobal pMac)
+{
+    tANI_U8 i;
+    tANI_U8 staCount = 0;
+    for(i =0; i < pMac->lim.maxBssId; i++)
+    {
+        if(pMac->lim.gpSession[i].valid == TRUE) 
+        {
+           staCount += pMac->lim.gpSession[i].gLimNumOfCurrentSTAs;
+        }
+    }
+    return staCount;
+}
+
diff --git a/CORE/MAC/src/pe/lim/limSessionUtils.h b/CORE/MAC/src/pe/lim/limSessionUtils.h
index 0d2d3b0..61011ee 100644
--- a/CORE/MAC/src/pe/lim/limSessionUtils.h
+++ b/CORE/MAC/src/pe/lim/limSessionUtils.h
@@ -227,5 +227,16 @@
   --------------------------------------------------------------------------*/
 tANI_U8
 limIsInMCC (tpAniSirGlobal pMac);
+/*--------------------------------------------------------------------------
+  \brief peGetCurrentSTAsCount() - Returns total stations associated on 
+                                      all session.
+
+  \param pMac                   - pointer to global adapter context
+  \return                       - Number of station active on all sessions.
+  
+  \sa
+  --------------------------------------------------------------------------*/
+tANI_U8
+peGetCurrentSTAsCount(tpAniSirGlobal pMac);
 #endif //#if !defined( __LIM_SESSION_UTILS_H )
 
diff --git a/CORE/MAC/src/pe/lim/limTypes.h b/CORE/MAC/src/pe/lim/limTypes.h
index 920f8b9..5bc76ab 100644
--- a/CORE/MAC/src/pe/lim/limTypes.h
+++ b/CORE/MAC/src/pe/lim/limTypes.h
@@ -782,6 +782,7 @@
                                                  tpPESession psessionEntry);
 #endif
 #ifdef FEATURE_WLAN_TDLS
+void limInitTdlsData(tpAniSirGlobal, tpPESession);
 tSirRetStatus limProcessSmeTdlsMgmtSendReq(tpAniSirGlobal pMac, 
                                                            tANI_U32 *pMsgBuf);
 tSirRetStatus limProcessSmeTdlsAddStaReq(tpAniSirGlobal pMac, 
@@ -1130,7 +1131,6 @@
 #endif
 #ifdef FEATURE_WLAN_TDLS_INTERNAL
 void limProcessTdlsFrame(tpAniSirGlobal, tANI_U32 *);
-void limInitTdlsData(tpAniSirGlobal, tpPESession);
 void limProcessTdlsPublicActionFrame(tpAniSirGlobal pMac, tANI_U32 *pBd, 
                                                                tpPESession) ;
 #ifdef FEATURE_WLAN_TDLS_NEGATIVE
diff --git a/CORE/MAC/src/pe/lim/limUtils.h b/CORE/MAC/src/pe/lim/limUtils.h
index 65be943..eec548e 100644
--- a/CORE/MAC/src/pe/lim/limUtils.h
+++ b/CORE/MAC/src/pe/lim/limUtils.h
@@ -130,8 +130,8 @@
 tANI_U8 limActiveScanAllowed(tpAniSirGlobal, tANI_U8);
 
 // AID pool management functions
-void    limInitAIDpool(tpAniSirGlobal,tpPESession);
-tANI_U16     limAssignAID(tpAniSirGlobal);
+void    limInitPeerIdxpool(tpAniSirGlobal,tpPESession);
+tANI_U16     limAssignPeerIdx(tpAniSirGlobal,tpPESession);
 
 void limEnableOverlap11gProtection(tpAniSirGlobal pMac, tpUpdateBeaconParams pBeaconParams, tpSirMacMgmtHdr pMh,tpPESession psessionEntry);
 void limUpdateOverlapStaParam(tpAniSirGlobal pMac, tSirMacAddr bssId, tpLimProtStaParams pStaParams);
@@ -142,7 +142,7 @@
  * The below 'product' check tobe removed if 'Association' is
  * allowed in IBSS.
  */
-void    limReleaseAID(tpAniSirGlobal, tANI_U16);
+void    limReleasePeerIdx(tpAniSirGlobal, tANI_U16, tpPESession);
 
 #if (WNI_POLARIS_FW_PRODUCT == AP)
 // LIM informs WSM that radar is detected