MAC: synchronize PE and HDD initiated disconnects

PE generated disassoc request is not serialized in SME queue. This
results in corrupting 'lim.limDisassocDeauthCnfReq.pMlmDisassocReq'
which is a global context to save disassoc request.
Address the above by indicating all the PE generated disassoc requests
to SME and process disconnection request only after receiving
eWNI_SME_DISASSOC_CNF from SME(similar to that of processing disassoc
request from peer).

Change-Id: I2b93925ba64c4d9ff22b071dd2e5c7681be59e15
CRs-Fixed: 934183
diff --git a/CORE/MAC/src/pe/lim/limAssocUtils.c b/CORE/MAC/src/pe/lim/limAssocUtils.c
index 9f12869..59b7515 100644
--- a/CORE/MAC/src/pe/lim/limAssocUtils.c
+++ b/CORE/MAC/src/pe/lim/limAssocUtils.c
@@ -823,15 +823,19 @@
 
         psessionEntry->limAID = 0;
 
-
+    } else if (
+       (mlmStaContext.cleanupTrigger == eLIM_LINK_MONITORING_DISASSOC) ||
+       (mlmStaContext.cleanupTrigger == eLIM_LINK_MONITORING_DEAUTH)) {
+       /* only for non-STA cases PE/SME is serialized */
+       return;
     }
 
     if ((mlmStaContext.cleanupTrigger ==
                                       eLIM_HOST_DISASSOC) ||
         (mlmStaContext.cleanupTrigger ==
-                                      eLIM_LINK_MONITORING_DISASSOC) ||
+                                      eLIM_PROMISCUOUS_MODE_DISASSOC) ||
         (mlmStaContext.cleanupTrigger ==
-                                      eLIM_PROMISCUOUS_MODE_DISASSOC))
+                                      eLIM_LINK_MONITORING_DISASSOC))
     {
         /**
          * Host or LMM driven Disassociation.
@@ -855,10 +859,8 @@
                           LIM_MLM_DISASSOC_CNF,
                           (tANI_U32 *) &mlmDisassocCnf);
     }
-    else if ((mlmStaContext.cleanupTrigger ==
-                                           eLIM_HOST_DEAUTH) ||
-             (mlmStaContext.cleanupTrigger ==
-                                           eLIM_LINK_MONITORING_DEAUTH))
+    else if ((mlmStaContext.cleanupTrigger == eLIM_HOST_DEAUTH) ||
+             (mlmStaContext.cleanupTrigger == eLIM_LINK_MONITORING_DEAUTH))
     {
         /**
          * Host or LMM driven Deauthentication.
diff --git a/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c b/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c
index 1c92204..94aa90f 100644
--- a/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c
+++ b/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c
@@ -166,7 +166,12 @@
                      return;
                  }
                  else
+                 {
+                     limSendDisassocMgmtFrame(pMac,
+                                     eSIR_MAC_DISASSOC_DUE_TO_INACTIVITY_REASON,
+                                     pStaDs->staAddr, psessionEntry, FALSE);
                      limTriggerSTAdeletion(pMac, pStaDs, psessionEntry);
+                 }
              }
              else
              {
@@ -290,82 +295,37 @@
 void
 limTriggerSTAdeletion(tpAniSirGlobal pMac, tpDphHashNode pStaDs, tpPESession psessionEntry)
 {
-    tSirSmeDeauthReq    *pSmeDeauthReq;
-    tANI_U8             *pBuf;
-    tANI_U8             *pLen;
-    tANI_U16            msgLength = 0;
-
-    if (! pStaDs)
-    {
-        PELOGW(limLog(pMac, LOGW, FL("Skip STA deletion (invalid STA)"));)
-        return;
-    }
-    /**
-     * MAC based Authentication was used. Trigger
-     * Deauthentication frame to peer since it will
-     * take care of disassociation as well.
-     */
-
-    pSmeDeauthReq = vos_mem_malloc(sizeof(tSirSmeDeauthReq));
-    if (NULL == pSmeDeauthReq)
-    {
-        limLog(pMac, LOGP, FL("AllocateMemory failed for eWNI_SME_DEAUTH_REQ "));
-        return;
-    }
-
-    pBuf = (tANI_U8 *) &pSmeDeauthReq->messageType;
-
-    //messageType
-    limCopyU16((tANI_U8*)pBuf, eWNI_SME_DISASSOC_REQ);
-    pBuf += sizeof(tANI_U16);
-    msgLength += sizeof(tANI_U16);
-
-    //length
-    pLen = pBuf;
-    pBuf += sizeof(tANI_U16);
-    msgLength += sizeof(tANI_U16);
-
-    //sessionId
-    *pBuf = psessionEntry->smeSessionId;
-    pBuf++;
-    msgLength++;
-
-    //transactionId
-    limCopyU16((tANI_U8*)pBuf, psessionEntry->transactionId);
-    pBuf += sizeof(tANI_U16);
-    msgLength += sizeof(tANI_U16);
-
-    //bssId
-    vos_mem_copy(pBuf, psessionEntry->bssId, sizeof(tSirMacAddr));
-    pBuf += sizeof(tSirMacAddr);
-    msgLength += sizeof(tSirMacAddr);
-
-    //peerMacAddr
-    vos_mem_copy(pBuf, pStaDs->staAddr, sizeof(tSirMacAddr));
-    pBuf += sizeof(tSirMacAddr);
-    msgLength += sizeof(tSirMacAddr);
-
-    //reasonCode 
-    limCopyU16((tANI_U8*)pBuf,
-            (tANI_U16)eSIR_MAC_DISASSOC_DUE_TO_INACTIVITY_REASON);
-    pBuf += sizeof(tANI_U16);
-    msgLength += sizeof(tANI_U16);
-
-    //Do not send disassoc OTA
-    //pBuf[0] = 1 means do not send the disassoc frame over the air
-    //pBuf[0] = 0 means send the disassoc frame over the air
-    pBuf[0]= 0;
-    pBuf += sizeof(tANI_U8);
-    msgLength += sizeof(tANI_U8);
+    tLimMlmDisassocInd mlmDisassocInd;
 
 
-  
-    //Fill in length
-    limCopyU16((tANI_U8*)pLen , msgLength);
+     if (!pStaDs)
+     {
+         PELOGW(limLog(pMac, LOGW, FL("Skip STA deletion (invalid STA)"));)
+         return;
+     }
 
-    limPostSmeMessage(pMac, eWNI_SME_DISASSOC_REQ, (tANI_U32 *) pSmeDeauthReq);
-    vos_mem_free(pSmeDeauthReq);
+     if ((pStaDs->mlmStaContext.mlmState == eLIM_MLM_WT_DEL_STA_RSP_STATE) ||
+         (pStaDs->mlmStaContext.mlmState == eLIM_MLM_WT_DEL_BSS_RSP_STATE)) {
+         /* Already in the process of deleting context for the peer */
+         PELOGE(limLog(pMac, LOGE,
+                 FL("Deletion is in progress for peer:%pM"), pStaDs->staAddr);)
+         return;
+     }
 
+     pStaDs->mlmStaContext.disassocReason =
+              eSIR_MAC_DISASSOC_DUE_TO_INACTIVITY_REASON;
+     pStaDs->mlmStaContext.cleanupTrigger = eLIM_LINK_MONITORING_DISASSOC;
+     vos_mem_copy(&mlmDisassocInd.peerMacAddr, pStaDs->staAddr,
+                  sizeof(tSirMacAddr));
+     mlmDisassocInd.reasonCode = eSIR_MAC_DISASSOC_DUE_TO_INACTIVITY_REASON;
+     mlmDisassocInd.disassocTrigger = eLIM_LINK_MONITORING_DISASSOC;
+
+     /* Update PE session Id  */
+     mlmDisassocInd.sessionId = psessionEntry->peSessionId;
+     limPostSmeMessage(pMac, LIM_MLM_DISASSOC_IND,
+                       (tANI_U32 *) &mlmDisassocInd);
+     // Issue Disassoc Indication to SME.
+     limSendSmeDisassocInd(pMac, pStaDs, psessionEntry);
 } /*** end limTriggerSTAdeletion() ***/
 
 
diff --git a/CORE/MAC/src/pe/lim/limProcessMlmReqMessages.c b/CORE/MAC/src/pe/lim/limProcessMlmReqMessages.c
index 7f3cc19..8035a5f 100644
--- a/CORE/MAC/src/pe/lim/limProcessMlmReqMessages.c
+++ b/CORE/MAC/src/pe/lim/limProcessMlmReqMessages.c
@@ -3012,28 +3012,17 @@
     if (sendDisassocFrame && (pMlmDisassocReq->reasonCode != eSIR_MAC_DISASSOC_DUE_TO_FTHANDOFF_REASON))
     {
         pMac->lim.limDisassocDeauthCnfReq.pMlmDisassocReq = pMlmDisassocReq;
-
-
-        /* If the reason for disassociation is inactivity of STA, then
-           dont wait for acknowledgement. Also, if FW_IN_TX_PATH feature
-           is enabled do not wait for ACK */
-        if (((pMlmDisassocReq->reasonCode == eSIR_MAC_DISASSOC_DUE_TO_INACTIVITY_REASON) &&
-            (psessionEntry->limSystemRole == eLIM_AP_ROLE)) ||
-            IS_FW_IN_TX_PATH_FEATURE_ENABLE )
+        if (IS_FW_IN_TX_PATH_FEATURE_ENABLE)
         {
-
-             limSendDisassocMgmtFrame(pMac,
-                                 pMlmDisassocReq->reasonCode,
+            limSendDisassocMgmtFrame(pMac, pMlmDisassocReq->reasonCode,
                                  pMlmDisassocReq->peerMacAddr,
                                  psessionEntry, FALSE);
-
-             /* Send Disassoc CNF and receive path cleanup */
-             limSendDisassocCnf(pMac);
+            /* Send Disassoc CNF and receive path cleanup */
+            limSendDisassocCnf(pMac);
         }
         else
         {
-             limSendDisassocMgmtFrame(pMac,
-                                 pMlmDisassocReq->reasonCode,
+            limSendDisassocMgmtFrame(pMac, pMlmDisassocReq->reasonCode,
                                  pMlmDisassocReq->peerMacAddr,
                                  psessionEntry, TRUE);
         }
diff --git a/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c b/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
index d298602..426e4e0 100644
--- a/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
+++ b/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
@@ -2710,15 +2710,8 @@
             goto sendDisassoc;
     } // end switch (pMac->lim.gLimSystemRole)
 
-    if (smeDisassocReq.reasonCode == eSIR_MAC_DISASSOC_DUE_TO_INACTIVITY_REASON)
-    {
-        /// Disassociation is triggered by Link Monitoring
-        limLog(pMac, LOG1, FL("Sending Disasscoc with reason Link Monitoring"));
-        disassocTrigger = eLIM_LINK_MONITORING_DISASSOC;
-    }
-    else
-        disassocTrigger = eLIM_HOST_DISASSOC;
-        reasonCode      = smeDisassocReq.reasonCode;
+    disassocTrigger = eLIM_HOST_DISASSOC;
+    reasonCode      = smeDisassocReq.reasonCode;
 
     if (smeDisassocReq.doNotSendOverTheAir)
     {