Wlan: Unrecoverable Scan failure when on/off stress is running

Host initiated Disconnect was in progress before this get completed,
Firmware send limDeleteStaContext (WDA_DELETE_STA_CONTEXT_IND) due to
AP is not giving ack of the last certain no of TX packets. So there
is race happening between the HOST initiated Disassoc versus TX monitored
deauth.This is leading the modifying the trigger reason,hence different
callback is invoked by the LIM to SME.So state machine at SME goes in
bad state and then active cmd is not getting out from queue.

Change-Id: I0626b5d648dcb60114fee39265e2f13a75963edc
CRs-Fixed: 671871
diff --git a/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c b/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c
index 1ccfd34..17c1107 100644
--- a/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c
+++ b/CORE/MAC/src/pe/lim/limLinkMonitoringAlgo.c
@@ -117,7 +117,10 @@
         case HAL_DEL_STA_REASON_CODE_TIM_BASED:
              PELOGE(limLog(pMac, LOGE, FL(" Deleting station: staId = %d, reasonCode = %d"), pMsg->staId, pMsg->reasonCode);)
              if (eLIM_STA_IN_IBSS_ROLE == psessionEntry->limSystemRole)
+             {
+                 vos_mem_free(pMsg);
                  return;
+             }
 
              pStaDs = dphLookupAssocId(pMac, pMsg->staId, &pMsg->assocId, &psessionEntry->dph.dphHashTable);
 
@@ -164,6 +167,25 @@
                     tLimMlmDeauthInd  mlmDeauthInd;
                     PELOGW(limLog(pMac, LOGW, FL("lim Delete Station Context (staId: %d, assocId: %d) "),
                                 pMsg->staId, pMsg->assocId);)
+                    if ((pStaDs &&
+                            ((pStaDs->mlmStaContext.mlmState !=
+                             eLIM_MLM_LINK_ESTABLISHED_STATE) &&
+                             (pStaDs->mlmStaContext.mlmState !=
+                             eLIM_MLM_WT_ASSOC_CNF_STATE) &&
+                             (pStaDs->mlmStaContext.mlmState !=
+                             eLIM_MLM_ASSOCIATED_STATE))))
+                    {
+                        /**
+                         * Received WDA_DELETE_STA_CONTEXT_IND for STA that does not
+                         * have context or in some transit state.
+                         * Log error
+                         */
+                         PELOGE(limLog(pMac, LOGE,
+                                FL("received WDA_DELETE_STA_CONTEXT_IND for STA that either has no context or in some transit state, Addr= "
+                                    MAC_ADDRESS_STR), MAC_ADDR_ARRAY(pMsg->bssId));)
+                         vos_mem_free(pMsg);
+                         return;
+                    }
 
                     if( pMac->roam.configParam.sendDeauthBeforeCon )
                     {
@@ -183,6 +205,15 @@
                     pStaDs->mlmStaContext.disassocReason = eSIR_MAC_UNSPEC_FAILURE_REASON;
                     pStaDs->mlmStaContext.cleanupTrigger = eLIM_LINK_MONITORING_DEAUTH;
 
+                   /** Set state to mlm State to eLIM_MLM_WT_DEL_STA_RSP_STATE
+                    * This is to address the issue of race condition between
+                    * disconnect request from the HDD and deauth from
+                    * Tx inactivity timer by FWR. This will make sure that we will not
+                    * process disassoc if deauth is in progress for the station
+                    * and thus mlmStaContext.cleanupTrigger will not be overwritten.
+                    */
+                    pStaDs->mlmStaContext.mlmState   = eLIM_MLM_WT_DEL_STA_RSP_STATE;
+
                     // Issue Deauth Indication to SME.
                     vos_mem_copy((tANI_U8 *) &mlmDeauthInd.peerMacAddr,
                                   pStaDs->staAddr, sizeof(tSirMacAddr));