Wlan: Sessionize SME scan cache.

Separate beacon & porbe rsp entry for PE scan cache.
Enable scan break down logic in SME for multi-session. The change fixes
issue with host initiated scans interferring with WFD connection and
causing jitter in the video stream on the WFD connection.

CR-Fixed: NA

Change-Id: I26e467a399524c9241494df85db293b33e8eda78
diff --git a/CORE/MAC/inc/aniGlobal.h b/CORE/MAC/inc/aniGlobal.h
index 716f9e2..bd51bcd 100644
--- a/CORE/MAC/inc/aniGlobal.h
+++ b/CORE/MAC/inc/aniGlobal.h
@@ -282,6 +282,7 @@
     tANI_U8               gLim24Band11dScanDone;
     tANI_U8               gLim50Band11dScanDone;
     tANI_U8               gLimReturnUniqueResults;
+    tANI_U8               gSeparateProbeBeacon;
 
     // Background Scan related globals on STA
     tANI_U32               gLimNumOfBackgroundScanSuccess;
diff --git a/CORE/MAC/inc/sirApi.h b/CORE/MAC/inc/sirApi.h
index 78eaa9e..b5a617e 100644
--- a/CORE/MAC/inc/sirApi.h
+++ b/CORE/MAC/inc/sirApi.h
@@ -1035,6 +1035,8 @@
     tANI_BOOLEAN         p2pSearch;
     tANI_BOOLEAN         skipDfsChnlInP2pSearch;
 #endif
+    tANI_U8              separateProbeBeacon; //If set to 1, PE scan cache to have separate entry for probe rsp & beacon for same BSSID
+
     tANI_U16             uIEFieldLen;
     tANI_U16             uIEFieldOffset;
 
diff --git a/CORE/MAC/src/pe/lim/limApi.c b/CORE/MAC/src/pe/lim/limApi.c
index 9a0fd9d..4f46e97 100644
--- a/CORE/MAC/src/pe/lim/limApi.c
+++ b/CORE/MAC/src/pe/lim/limApi.c
@@ -87,6 +87,7 @@
     pMac->lim.gLim24Band11dScanDone = 0;
     pMac->lim.gLim50Band11dScanDone = 0;
     pMac->lim.gLimReturnUniqueResults = 0;
+    pMac->lim.gSeparateProbeBeacon = false;
 
     // Background Scan related globals on STA
     pMac->lim.gLimNumOfBackgroundScanSuccess = 0;
@@ -640,6 +641,7 @@
       // By default return unique scan results
       pMac->lim.gLimReturnUniqueResults = true;
       pMac->lim.gLimSmeScanResultLength = 0;
+      pMac->lim.gSeparateProbeBeacon = false;
    }
    else
    {
diff --git a/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c b/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
index 7ade82a..3392ad5 100644
--- a/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
+++ b/CORE/MAC/src/pe/lim/limProcessSmeReqMessages.c
@@ -377,6 +377,7 @@
 
         /// By default return unique scan results
         pMac->lim.gLimReturnUniqueResults = true;
+        pMac->lim.gSeparateProbeBeacon = false;
         pMac->lim.gLimSmeScanResultLength = 0;
 
 #if defined(ANI_PRODUCT_TYPE_CLIENT) || defined(ANI_AP_CLIENT_SDK)
@@ -1179,6 +1180,9 @@
 
         pMac->lim.gLimReturnUniqueResults   =
               ((pScanReq->returnUniqueResults) > 0 ? true : false);
+
+        pMac->lim.gSeparateProbeBeacon = ((pScanReq->separateProbeBeacon) > 0 ? true : false);
+
         /* De-activate Heartbeat timers for connected sessions while
          * scan is in progress if the system is in Active mode */
         if((ePMM_STATE_BMPS_WAKEUP == pMac->pmm.gPmmState) ||
diff --git a/CORE/MAC/src/pe/lim/limScanResultUtils.c b/CORE/MAC/src/pe/lim/limScanResultUtils.c
index 511c1f1..06d1a88 100644
--- a/CORE/MAC/src/pe/lim/limScanResultUtils.c
+++ b/CORE/MAC/src/pe/lim/limScanResultUtils.c
@@ -622,8 +622,9 @@
 limLookupNaddHashEntry(tpAniSirGlobal pMac,
                        tLimScanResultNode *pBssDescr, tANI_U8 action)
 {
-    tANI_U8                  index, ssidLen = 0;
-    tANI_U8                found = false;
+    tANI_U8 index, ssidLen = 0;
+    tANI_U8 found = false;
+    tANI_U8 continueSearch = false;
     tLimScanResultNode *ptemp, *pprev;
     tSirMacCapabilityInfo *pSirCap, *pSirCapTemp;
     int idx, len;
@@ -698,18 +699,35 @@
                 }
 
                 // Delete this entry
-                if (ptemp == pMac->lim.gLimCachedScanHashTable[index])
-                    pprev = pMac->lim.gLimCachedScanHashTable[index] = ptemp->next;
+                // Delete this entry, if we don't want to keep separate entries for
+                // beacon & probe rsp
+                if(pMac->lim.gSeparateProbeBeacon && 
+                   (pBssDescr->bssDescription.fProbeRsp != ptemp->bssDescription.fProbeRsp))
+                {
+                    continueSearch = true;
+                }
                 else
-                    pprev->next = ptemp->next;
+                {
+                    if (ptemp == pMac->lim.gLimCachedScanHashTable[index]) {
+                        pprev = pMac->lim.gLimCachedScanHashTable[index] = ptemp->next;
+                    } else {
+                        pprev->next = ptemp->next;
+                    }
 
-                pMac->lim.gLimMlmScanResultLength -=
-                    ptemp->bssDescription.length + sizeof(tANI_U16);
+                    pMac->lim.gLimMlmScanResultLength -=
+                        ptemp->bssDescription.length + sizeof(tANI_U16);
 
-                palFreeMemory( pMac->hHdd, (tANI_U8 *) ptemp);
+                    palFreeMemory( pMac->hHdd, (tANI_U8 *) ptemp);
+                    continueSearch = false;
+                }
             }
-            found = true;
-            break;
+
+            // lets see if there are other entries in the list which can match
+            if(false == continueSearch)
+            {
+                found = true;
+                break;
+            }
         }
     }
 
diff --git a/CORE/MAC/src/pe/lim/limSendSmeRspMessages.c b/CORE/MAC/src/pe/lim/limSendSmeRspMessages.c
index 3aa403b..e432fa5 100644
--- a/CORE/MAC/src/pe/lim/limSendSmeRspMessages.c
+++ b/CORE/MAC/src/pe/lim/limSendSmeRspMessages.c
@@ -797,6 +797,8 @@
         PELOG2(limLog(pMac, LOG2, FL("statusCode : eSIR_SME_SUCCESS\n"));)
     }
 
+    //reset the flag, to be set again by new scan request from SME
+    pMac->lim.gSeparateProbeBeacon = false;
     return;
 
 } /*** end limSendSmeScanRsp() ***/
diff --git a/CORE/SME/inc/csrInternal.h b/CORE/SME/inc/csrInternal.h
index 504f9dc..65ba29d 100644
--- a/CORE/SME/inc/csrInternal.h
+++ b/CORE/SME/inc/csrInternal.h
@@ -607,7 +607,7 @@
 {
     tScanProfile scanProfile;
     tANI_U32 nextScanID;
-    tDblLinkList scanResultList;
+    tDblLinkList scanResultList[CSR_ROAM_SESSION_MAX];
     tDblLinkList tempScanResults;
     tANI_BOOLEAN fScanEnable;
     tANI_BOOLEAN fFullScanIssued;
@@ -1033,6 +1033,7 @@
 tANI_BOOLEAN csrIsConnStateDisconnectedWds( tpAniSirGlobal pMac, tANI_U32 sessionId );
 tANI_BOOLEAN csrIsAnySessionInConnectState( tpAniSirGlobal pMac );
 tANI_BOOLEAN csrIsAllSessionDisconnected( tpAniSirGlobal pMac );
+tANI_BOOLEAN csrIsAnySessionConnected( tpAniSirGlobal pMac );
 tANI_BOOLEAN csrIsInfraConnected( tpAniSirGlobal pMac );
 tANI_BOOLEAN csrIsConcurrentInfraConnected( tpAniSirGlobal pMac );
 tANI_BOOLEAN csrIsConcurrentSessionRunning( tpAniSirGlobal pMac );
diff --git a/CORE/SME/src/csr/csrInsideApi.h b/CORE/SME/src/csr/csrInsideApi.h
index fdd7f12..426026a 100644
--- a/CORE/SME/src/csr/csrInsideApi.h
+++ b/CORE/SME/src/csr/csrInsideApi.h
@@ -138,6 +138,7 @@
     tANI_U8 fUniqueResult;
     tANI_U8 freshScan;
     tANI_U8 hiddenSsid;
+    tANI_U8 separateProbeBeacon; //If set to 1, PE scan cache to have separate entry for probe rsp & beacon for same BSSID
     tANI_U8 reserved;
 }tScanReqParam;
 
@@ -219,13 +220,13 @@
 eHalStatus csrScanHandleFailedLostlink1(tpAniSirGlobal pMac, tANI_U32 sessionId);
 eHalStatus csrScanHandleFailedLostlink2(tpAniSirGlobal pMac, tANI_U32 sessionId);
 eHalStatus csrScanHandleFailedLostlink3(tpAniSirGlobal pMac, tANI_U32 sessionId);
-tCsrScanResult *csrScanAppendBssDescription( tpAniSirGlobal pMac, 
+tCsrScanResult *csrScanAppendBssDescription( tpAniSirGlobal pMac, tANI_U32 sessionId, 
                                              tSirBssDescription *pSirBssDescription,
                                              tDot11fBeaconIEs *pIes);
 void csrScanCallCallback(tpAniSirGlobal pMac, tSmeCmd *pCommand, eCsrScanStatus scanStatus);
 eHalStatus csrScanCopyRequest(tpAniSirGlobal pMac, tCsrScanRequest *pDstReq, tCsrScanRequest *pSrcReq);
 eHalStatus csrScanFreeRequest(tpAniSirGlobal pMac, tCsrScanRequest *pReq);
-eHalStatus csrScanCopyResultList(tpAniSirGlobal pMac, tScanResultHandle hIn, tScanResultHandle *phResult);
+eHalStatus csrScanCopyResultList(tpAniSirGlobal pMac, tANI_U32 sessionId, tScanResultHandle hIn, tScanResultHandle *phResult);
 void csrInitBGScanChannelList(tpAniSirGlobal pMac);
 eHalStatus csrScanForSSID(tpAniSirGlobal pMac, tANI_U32 sessionId, tCsrRoamProfile *pProfile, tANI_U32 roamId);
 eHalStatus csrScanForCapabilityChange(tpAniSirGlobal pMac, tSirSmeApNewCaps *pNewCaps);
@@ -257,13 +258,13 @@
 //The logic is that whenever CSR add a BSS to scan result, it set the age count to
 //a value. This function deduct the age count if channelId matches the BSS' channelId
 //The BSS is remove if the count reaches 0.
-eHalStatus csrScanAgeResults(tpAniSirGlobal pMac, tSmeGetScanChnRsp *pScanChnInfo);
+eHalStatus csrScanAgeResults(tpAniSirGlobal pMac, tANI_U32 sessionId, tSmeGetScanChnRsp *pScanChnInfo);
 
 //If fForce is TRUE we will save the new String that is learn't.
 //Typically it will be true in case of Join or user initiated ioctl
 tANI_BOOLEAN csrLearnCountryInformation( tpAniSirGlobal pMac, tSirBssDescription *pSirBssDesc,
                                          tDot11fBeaconIEs *pIes, tANI_BOOLEAN fForce );
-void csrApplyCountryInformation( tpAniSirGlobal pMac, tANI_BOOLEAN fForce );
+void csrApplyCountryInformation( tpAniSirGlobal pMac, tANI_U32 sessionId, tANI_BOOLEAN fForce );
 void csrSetCfgScanControlList( tpAniSirGlobal pMac, tANI_U8 *countryCode, tCsrChannel *pChannelList  );
 void csrReinitScanCmd(tpAniSirGlobal pMac, tSmeCmd *pCommand);
 void csrFreeScanResultEntry( tpAniSirGlobal pMac, tCsrScanResult *pResult );
@@ -358,7 +359,7 @@
 void csrAssignRssiForCategory(tpAniSirGlobal pMac, tANI_U8 catOffset);
 tANI_BOOLEAN csrIsMacAddressZero( tpAniSirGlobal pMac, tCsrBssid *pMacAddr );
 tANI_BOOLEAN csrIsMacAddressBroadcast( tpAniSirGlobal pMac, tCsrBssid *pMacAddr );
-eHalStatus csrRoamRemoveConnectedBssFromScanCache(tpAniSirGlobal pMac, tCsrRoamConnectedProfile *pConnProfile);
+eHalStatus csrRoamRemoveConnectedBssFromScanCache(tpAniSirGlobal pMac, tANI_U32 sessionId, tCsrRoamConnectedProfile *pConnProfile);
 eHalStatus csrRoamStartRoaming(tpAniSirGlobal pMac, tANI_U32 sessionId, eCsrRoamingReason roamingReason);
 //return a boolean to indicate whether roaming completed or continue.
 tANI_BOOLEAN csrRoamCompleteRoaming(tpAniSirGlobal pMac, tANI_U32 sessionId,
@@ -439,14 +440,14 @@
     \param phResult - an object for the result.
     \return eHalStatus     
   -------------------------------------------------------------------------------*/
-eHalStatus csrScanGetResult(tpAniSirGlobal, tCsrScanResultFilter *pFilter, tScanResultHandle *phResult);
+eHalStatus csrScanGetResult(tpAniSirGlobal, tANI_U32 sessionId, tCsrScanResultFilter *pFilter, tScanResultHandle *phResult);
 
 /* ---------------------------------------------------------------------------
     \fn csrScanFlushResult
     \brief Clear scan results.
     \return eHalStatus     
   -------------------------------------------------------------------------------*/
-eHalStatus csrScanFlushResult(tpAniSirGlobal);
+eHalStatus csrScanFlushResult(tpAniSirGlobal, tANI_U32 sessionId);
 
 /* ---------------------------------------------------------------------------
     \fn csrScanBGScanGetParam
diff --git a/CORE/SME/src/p2p/p2p_Api.c b/CORE/SME/src/p2p/p2p_Api.c
index 8b45b1d..3776ffd 100644
--- a/CORE/SME/src/p2p/p2p_Api.c
+++ b/CORE/SME/src/p2p/p2p_Api.c
@@ -1256,7 +1256,7 @@
       filter.bWPSAssociation = TRUE;
       filter.BSSType = eCSR_BSS_TYPE_ANY;
 
-      status = csrScanGetResult(pMac, &filter, &hScanResult);
+      status = csrScanGetResult(pMac, pP2pContext->SMEsessionId, &filter, &hScanResult);
 
       if (hScanResult)
       {
@@ -1298,7 +1298,7 @@
             filter.SSIDs.SSIDList->SSID.length = ssIdLen;
             vos_mem_copy(&filter.SSIDs.SSIDList[0].SSID.ssId, &ssId, ssIdLen);
             filter.SSIDs.numOfSSIDs = 1;
-            status = csrScanGetResult(pMac, &filter, &hScanResult);
+            status = csrScanGetResult(pMac, pP2pContext->SMEsessionId, &filter, &hScanResult);
             if (hScanResult)
             {
                pScanResult = csrScanResultGetFirst(pMac, hScanResult );
@@ -1852,7 +1852,7 @@
             }
          }//if(NULL != pDeviceFilters)
 
-         status = csrScanGetResult(pMac, &filter, &hScanResult);
+         status = csrScanGetResult(pMac, SessionID, &filter, &hScanResult);
          if (hScanResult)
          {
             VOS_TRACE(VOS_MODULE_ID_SME, VOS_TRACE_LEVEL_ERROR, 
@@ -2162,12 +2162,13 @@
 {
    eHalStatus status = eHAL_STATUS_FAILURE;
    tpAniSirGlobal pMac = PMAC_STRUCT( hHal );
+   tp2pContext *pP2pContext = &pMac->p2pContext[HDDSessionId];
 
    smsLog(pMac, LOG2, FL("enter"));
    status = sme_AcquireGlobalLock( &pMac->sme );
    if ( HAL_STATUS_SUCCESS( status ) )
    {
-      status = p2pPurgeDeviceList(pMac, &pMac->scan.scanResultList);
+      status = p2pPurgeDeviceList(pMac, &pMac->scan.scanResultList[pP2pContext->SMEsessionId]);
       sme_ReleaseGlobalLock( &pMac->sme );
    }
 
diff --git a/CORE/SME/src/sme_common/sme_Api.c b/CORE/SME/src/sme_common/sme_Api.c
index 1538df4..2950024 100644
--- a/CORE/SME/src/sme_common/sme_Api.c
+++ b/CORE/SME/src/sme_common/sme_Api.c
@@ -1932,7 +1932,7 @@
    status = sme_AcquireGlobalLock( &pMac->sme );
    if ( HAL_STATUS_SUCCESS( status ) )
    {
-       status = csrScanGetResult( hHal, pFilter, phResult );
+       status = csrScanGetResult( pMac, sessionId, pFilter, phResult );
        sme_ReleaseGlobalLock( &pMac->sme );
    }
    smsLog(pMac, LOG2, FL("exit status %d"), status);
@@ -1955,7 +1955,7 @@
    status = sme_AcquireGlobalLock( &pMac->sme );
    if ( HAL_STATUS_SUCCESS( status ) )
    {
-       status = csrScanFlushResult( hHal );
+       status = csrScanFlushResult( hHal, sessionId );
        sme_ReleaseGlobalLock( &pMac->sme );
    }
 
@@ -5976,7 +5976,8 @@
     if i don't do this than I still get old ap's (of different country code) as available (even if they are powered off). 
     Looks like a bug in current scan sequence. 
    */
-   csrScanFlushResult(pMac);
+   //Is it ok to assume infra session here - TBD
+   csrScanFlushResult(pMac, 0);
 
    /* overwrite the defualt country code */
    palCopyMemory(pMac->hHdd, pMac->scan.countryCodeDefault, pMac->scan.countryCodeCurrent, WNI_CFG_COUNTRY_CODE_LEN);