Fix the max tx power issue with 11h/d enable.

Fixes:
1) Do not update the power values during scan in case of 11h.
2) Use default power values from NV
3) Use power value either from NV or Country IE based in join req.
4) Reset the power values for 5G after disconnect.

Change-Id: I9dfa5efe5e0e937940ac903dd852ea596bbad2bc
CRs-fixed: 435807
diff --git a/CORE/HDD/src/wlan_hdd_assoc.c b/CORE/HDD/src/wlan_hdd_assoc.c
old mode 100644
new mode 100755
index 2334939..8a30124
--- a/CORE/HDD/src/wlan_hdd_assoc.c
+++ b/CORE/HDD/src/wlan_hdd_assoc.c
@@ -3313,5 +3313,12 @@
             }
         }
     }
+    else if (smeConfig.csrConfig.Is11hSupportEnabled)
+    {
+        VOS_TRACE( VOS_MODULE_ID_VOSS, VOS_TRACE_LEVEL_INFO,
+                            "%s: Disconnected from the AP/Assoc failed and "
+                            "resetting the 5G power values to default", __func__);
+        sme_ResetPowerValuesFor5G (pHddCtx->hHal);
+    }
 }
 
diff --git a/CORE/SME/inc/sme_Api.h b/CORE/SME/inc/sme_Api.h
old mode 100644
new mode 100755
index 49f91ae..39546ef
--- a/CORE/SME/inc/sme_Api.h
+++ b/CORE/SME/inc/sme_Api.h
@@ -2228,6 +2228,14 @@
    v_BOOL_t  toggleStallDetect
 );
 
+/* ---------------------------------------------------------------------------
+    \fn     sme_ResetPowerValuesFor5G
+    \brief  Reset the power values for 5G band with NV power values.
+    \param  hHal - HAL handle for device
+    \- return NONE
+    -------------------------------------------------------------------------*/
+void sme_ResetPowerValuesFor5G (tHalHandle hHal);
+
 #if  defined (WLAN_FEATURE_VOWIFI_11R) || defined (FEATURE_WLAN_CCX) || defined(FEATURE_WLAN_LFR)
 /* ---------------------------------------------------------------------------
     \fn sme_UpdateRoamPrefer5GHz
diff --git a/CORE/SME/src/csr/csrApiRoam.c b/CORE/SME/src/csr/csrApiRoam.c
old mode 100644
new mode 100755
index bb769f0..37ea852
--- a/CORE/SME/src/csr/csrApiRoam.c
+++ b/CORE/SME/src/csr/csrApiRoam.c
@@ -1715,6 +1715,8 @@
     eHalStatus status = eHAL_STATUS_SUCCESS;
     csrPruneChannelListForMode(pMac, &pMac->scan.baseChannels);
     csrPruneChannelListForMode(pMac, &pMac->scan.base20MHzChannels);
+    csrSaveChannelPowerForBand(pMac, eANI_BOOLEAN_FALSE);
+    csrSaveChannelPowerForBand(pMac, eANI_BOOLEAN_TRUE);
     // Apply the base channel list, power info, and set the Country code...
     csrApplyChannelPowerCountryInfo( pMac, &pMac->scan.base20MHzChannels, pMac->scan.countryCodeCurrent, eANI_BOOLEAN_TRUE );
  
@@ -3268,8 +3270,12 @@
             pMac->scan.fAmbiguous11dInfoFound = eANI_BOOLEAN_FALSE;
             csrApplyCountryInformation( pMac, TRUE );
         }
+        if ((csrIs11dSupported (pMac)) && pIes)
+        {
+            if (!pIes->Country.present)
+                csrResetCountryInformation(pMac, eANI_BOOLEAN_FALSE, eANI_BOOLEAN_FALSE );
+        }
     }
-        
     //Qos
     csrSetQosToCfg( pMac, sessionId, pBssConfig->qosType );
     //SSID
@@ -9930,6 +9936,65 @@
                   pNumChan));
 }
 
+tPowerdBm csrGetCfgMaxTxPower (tpAniSirGlobal pMac, tANI_U8 channel)
+{
+    tANI_U32    cfgLength = 0;
+    tANI_U16    cfgId = 0;
+    tPowerdBm   maxTxPwr = 0;
+    tANI_U8     *pCountryInfo = NULL;
+    eHalStatus  status;
+    tANI_U8     count = 0;
+    tANI_U8     firstChannel;
+    tANI_U8     maxChannels;
+
+    if (CSR_IS_CHANNEL_5GHZ(channel))
+    {
+        cfgId = WNI_CFG_MAX_TX_POWER_5;
+        cfgLength = WNI_CFG_MAX_TX_POWER_5_LEN;
+    }
+    else if (CSR_IS_CHANNEL_24GHZ(channel))
+    {
+        cfgId = WNI_CFG_MAX_TX_POWER_2_4;
+        cfgLength = WNI_CFG_MAX_TX_POWER_2_4_LEN;
+    }
+    else
+        return  maxTxPwr;
+
+    status = palAllocateMemory(pMac->hHdd, (void **)&pCountryInfo, cfgLength);
+    if (status != eHAL_STATUS_SUCCESS)
+    {
+        VOS_TRACE(VOS_MODULE_ID_SME, VOS_TRACE_LEVEL_ERROR,
+                 FL("%s: palAllocateMemory() failed, status = %d"),
+              __FUNCTION__, status);
+        goto error;
+    }
+    status = ccmCfgGetStr(pMac, cfgId, (tANI_U8 *)pCountryInfo, &cfgLength);
+    if (status != eHAL_STATUS_SUCCESS)
+    {
+        goto error;
+    }
+    /* Identify the channel and maxtxpower */
+    while (count <= (cfgLength - (sizeof(tSirMacChanInfo))))
+    {
+        firstChannel = pCountryInfo[count++];
+        maxChannels = pCountryInfo[count++];
+        maxTxPwr = pCountryInfo[count++];
+
+        if ((channel >= firstChannel) &&
+            (channel < (firstChannel + maxChannels)))
+        {
+            break;
+        }
+    }
+
+error:
+    if (NULL != pCountryInfo)
+        palFreeMemory(pMac->hHdd, pCountryInfo);
+
+    return maxTxPwr;
+}
+
+
 tANI_BOOLEAN csrRoamIsChannelValid( tpAniSirGlobal pMac, tANI_U8 channel )
 {
     tANI_BOOLEAN fValid = FALSE;
@@ -11248,7 +11313,8 @@
     tAniBool fTmp;
     tANI_BOOLEAN found = FALSE;
     tANI_U32 size = 0;
-        tANI_U16 i;
+    tANI_S8 pwrLimit = 0;
+    tANI_U16 i;
     // plug in neighborhood occupancy info (i.e. BSSes on primary or secondary channels)
     *pBuf++ = (tANI_U8)FALSE;  //tAniTitanCBNeighborInfo->cbBssFoundPri
     *pBuf++ = (tANI_U8)FALSE;  //tAniTitanCBNeighborInfo->cbBssFoundSecDown
@@ -11256,9 +11322,9 @@
     // 802.11h
     //We can do this because it is in HOST CPU order for now.
     pAP_capabilityInfo = (tSirMacCapabilityInfo *)&pBssDescription->capabilityInfo;
-        //tell the target AP my 11H capability only if both AP and STA support 11H and the channel being used is 11a
-        if ( csrIs11hSupported( pMac ) && pAP_capabilityInfo->spectrumMgt && eSIR_11A_NW_TYPE == pBssDescription->nwType )   
-        {  
+    //tell the target AP my 11H capability only if both AP and STA support 11H and the channel being used is 11a
+    if ( csrIs11hSupported( pMac ) && pAP_capabilityInfo->spectrumMgt && eSIR_11A_NW_TYPE == pBssDescription->nwType )
+    {
         fTmp = (tAniBool)pal_cpu_to_be32(1);
     }
     else
@@ -11268,12 +11334,20 @@
     palCopyMemory( pMac->hHdd, pBuf, (tANI_U8 *)&fTmp, sizeof(tAniBool) );
     pBuf += sizeof(tAniBool);
     *pBuf++ = MIN_STA_PWR_CAP_DBM; // it is for pMsg->powerCap.minTxPower = 0;
-        found = csrSearchChannelListForTxPower(pMac, pBssDescription, &channelGroup);
+    found = csrSearchChannelListForTxPower(pMac, pBssDescription, &channelGroup);
     // This is required for 11k test VoWiFi Ent: Test 2.
     // We need the power capabilities for Assoc Req. 
     // This macro is provided by the halPhyCfg.h. We pick our
     // max and min capability by the halPhy provided macros
-    *pBuf++ = MAX_STA_PWR_CAP_DBM;
+    pwrLimit = csrGetCfgMaxTxPower (pMac, pBssDescription->channelId);
+    if (0 != pwrLimit)
+    {
+        *pBuf++ = pwrLimit;
+    }
+    else
+    {
+        *pBuf++ = MAX_STA_PWR_CAP_DBM;
+    }
     size = sizeof(pMac->roam.validChannelList);
     if(HAL_STATUS_SUCCESS(csrGetCfgValidChannels(pMac, (tANI_U8 *)pMac->roam.validChannelList, &size)))
     { 
@@ -11808,10 +11882,18 @@
         *pBuf = (tANI_U8)pMac->roam.configParam.txLdpcEnable;
         pBuf++;
 
+        if ((csrIs11hSupported (pMac)) && (CSR_IS_CHANNEL_5GHZ(pBssDescription->channelId)) &&
+            (pIes->Country.present) && (!pMac->roam.configParam.fSupplicantCountryCodeHasPriority))
+        {
+            csrSaveToChannelPower2G_5G( pMac, pIes->Country.num_triplets * sizeof(tSirMacChanInfo),
+                                        (tSirMacChanInfo *)(&pIes->Country.triplets[0]) );
+            csrApplyPower2Current(pMac);
+        }
+
 #ifdef WLAN_FEATURE_11AC
-	// txBFIniFeatureEnabled
-	*pBuf = (tANI_U8)pMac->roam.configParam.txBFEnable;
-	 pBuf++;
+        // txBFIniFeatureEnabled
+        *pBuf = (tANI_U8)pMac->roam.configParam.txBFEnable;
+        pBuf++;
 #endif
         //BssDesc
         csrPrepareJoinReassocReqBuffer( pMac, pBssDescription, pBuf,
diff --git a/CORE/SME/src/csr/csrApiScan.c b/CORE/SME/src/csr/csrApiScan.c
old mode 100644
new mode 100755
index 4d384bf..7657411
--- a/CORE/SME/src/csr/csrApiScan.c
+++ b/CORE/SME/src/csr/csrApiScan.c
@@ -2814,13 +2814,6 @@
             }
         }
 
-        else if( csrIs11hSupported( pMac ) && pIesLocal->Country.present && 
-                 !pMac->roam.configParam.fSupplicantCountryCodeHasPriority )
-        {
-             /* If 11h is supported, store the power information received in the Country IE */
-            csrSaveToChannelPower2G_5G( pMac, pIesLocal->Country.num_triplets * sizeof(tSirMacChanInfo), 
-                                        (tSirMacChanInfo *)(&pIesLocal->Country.triplets[0]) );
-        }
         
         // append to main list
         csrScanAddResult(pMac, pBssDescription, pIesLocal);
@@ -3766,11 +3759,6 @@
     {
         csrApplyCountryInformation( pMac, FALSE );
     }
-    else if( csrIs11hSupported(pMac) && !pMac->roam.configParam.fSupplicantCountryCodeHasPriority) 
-    {
-        // If llh is enabled, store the channel + power information gathered  in the cfg
-        csrApplyPower2Current( pMac );
-    }     
 }
 
 
diff --git a/CORE/SME/src/csr/csrInsideApi.h b/CORE/SME/src/csr/csrInsideApi.h
old mode 100644
new mode 100755
index fab0330..6ff32e9
--- a/CORE/SME/src/csr/csrInsideApi.h
+++ b/CORE/SME/src/csr/csrInsideApi.h
@@ -364,6 +364,8 @@
 void csrRoamCcmCfgSetCallback(tHalHandle hHal, tANI_S32 result);
 void csrScanCcmCfgSetCallback(tHalHandle hHal, tANI_S32 result);
 
+tPowerdBm csrGetCfgMaxTxPower (tpAniSirGlobal pMac, tANI_U8 channel);
+
 //To free the last roaming profile
 void csrFreeRoamProfile(tpAniSirGlobal pMac, tANI_U32 sessionId);
 void csrFreeConnectBssDesc(tpAniSirGlobal pMac, tANI_U32 sessionId);
diff --git a/CORE/SME/src/sme_common/sme_Api.c b/CORE/SME/src/sme_common/sme_Api.c
old mode 100644
new mode 100755
index 1e777d9..176f68c
--- a/CORE/SME/src/sme_common/sme_Api.c
+++ b/CORE/SME/src/sme_common/sme_Api.c
@@ -6905,6 +6905,19 @@
    WDA_TransportChannelDebug(displaySnapshot, toggleStallDetect);
 }
 
+/* ---------------------------------------------------------------------------
+    \fn     sme_ResetPowerValuesFor5G
+    \brief  Reset the power values for 5G band with NV power values.
+    \param  hHal - HAL handle for device
+    \- return NONE
+    -------------------------------------------------------------------------*/
+void sme_ResetPowerValuesFor5G (tHalHandle hHal)
+{
+    tpAniSirGlobal pMac = PMAC_STRUCT (hHal);
+    csrSaveChannelPowerForBand(pMac, eANI_BOOLEAN_TRUE);
+    csrApplyPower2Current(pMac);     // Store the channel+power info in the global place: Cfg
+}
+
 #if  defined (WLAN_FEATURE_VOWIFI_11R) || defined (FEATURE_WLAN_CCX) || defined(FEATURE_WLAN_LFR)
 /* ---------------------------------------------------------------------------
     \fn sme_UpdateRoamPrefer5GHz
diff --git a/CORE/WDA/src/wlan_qct_wda.c b/CORE/WDA/src/wlan_qct_wda.c
old mode 100644
new mode 100755
index 2c34468..8bb48ff
--- a/CORE/WDA/src/wlan_qct_wda.c
+++ b/CORE/WDA/src/wlan_qct_wda.c
@@ -2911,6 +2911,8 @@
       vos_mem_free(wdiConfigBssReqParam);
       return VOS_STATUS_E_NOMEM;
    }
+   VOS_TRACE( VOS_MODULE_ID_WDA, VOS_TRACE_LEVEL_INFO,
+                           "%s: maxTxPower %d", __func__, configBssReqParam->maxTxPower);
    vos_mem_set(wdiConfigBssReqParam, sizeof(WDI_ConfigBSSReqParamsType), 0);
    WDA_UpdateBSSParams(pWDA, &wdiConfigBssReqParam->wdiReqInfo, 
                        configBssReqParam) ;