TDLS: Optimize TDLS External Control operation handling.

Commit "Initiate the TDLS setup only when externally configured"
defined an additional data structure to represent a force peer.
Optimize this implementation by defining a new field isForcedPeer,
signifying a forced peer, in TDLS peer data structure (hddTdlsPeer_t).

CRs-fixed: 537537

Change-Id: Ibf55499fc648868e04a2ec9f18f5dce94fe61108
diff --git a/CORE/HDD/src/wlan_hdd_cfg80211.c b/CORE/HDD/src/wlan_hdd_cfg80211.c
index fd64ef6..76a770c 100644
--- a/CORE/HDD/src/wlan_hdd_cfg80211.c
+++ b/CORE/HDD/src/wlan_hdd_cfg80211.c
@@ -8572,8 +8572,6 @@
                     return -ENOTSUPP;
                 }
 
-                if ( 0 != wlan_hdd_tdls_remove_force_peer(pAdapter, peer) )
-                    return -EINVAL;
 
                 pTdlsPeer = wlan_hdd_tdls_find_peer(pAdapter, peer, TRUE);
 
@@ -8581,11 +8579,15 @@
                     hddLog(VOS_TRACE_LEVEL_INFO, "%s: " MAC_ADDRESS_STR
                            " peer not exsting",
                            __func__, MAC_ADDR_ARRAY(peer));
+                    return -EINVAL;
                 }
                 else {
                     wlan_hdd_tdls_indicate_teardown(pAdapter, pTdlsPeer,
                                        eSIR_MAC_TDLS_TEARDOWN_UNSPEC_REASON);
                 }
+
+                if ( 0 != wlan_hdd_tdls_set_force_peer(pAdapter, peer, FALSE) )
+                    return -EINVAL;
                 break;
             }
         case NL80211_TDLS_SETUP:
@@ -8604,13 +8606,6 @@
                     return -ENOTSUPP;
                 }
 
-                if ( 0 != wlan_hdd_tdls_add_force_peer(pAdapter, peer) ) {
-
-                    VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_ERROR,
-                          " %s TDLS Add Force Peer Failed",
-                          __func__);
-                    return -EINVAL;
-                }
                 /* To cater the requirement of establishing the TDLS link
                  * irrespective of the data traffic , get an entry of TDLS peer.
                  */
@@ -8621,6 +8616,14 @@
                               __func__, MAC_ADDR_ARRAY(peer));
                     return -EINVAL;
                 }
+
+                if ( 0 != wlan_hdd_tdls_set_force_peer(pAdapter, peer, TRUE) ) {
+
+                    VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_ERROR,
+                          " %s TDLS Add Force Peer Failed",
+                          __func__);
+                    return -EINVAL;
+                }
                 break;
             }
         case NL80211_TDLS_DISCOVERY_REQ:
diff --git a/CORE/HDD/src/wlan_hdd_tdls.c b/CORE/HDD/src/wlan_hdd_tdls.c
index bc108c0..160313a 100644
--- a/CORE/HDD/src/wlan_hdd_tdls.c
+++ b/CORE/HDD/src/wlan_hdd_tdls.c
@@ -286,7 +286,7 @@
                        " tdls_support %d", __func__, MAC_ADDR_ARRAY(curr_peer->peerMac),
                        curr_peer->link_status, curr_peer->tdls_support);
             if (pHddCtx->cfg_ini->fTDLSExternalControl &&
-                (0 == wpa_tdls_is_allowed_force_peer (pHddTdlsCtx, curr_peer->peerMac))) {
+                (FALSE == curr_peer->isForcedPeer)) {
                 continue;
             }
 
@@ -1275,29 +1275,10 @@
     return 0;
 }
 
-int wpa_tdls_is_allowed_force_peer(tdlsCtx_t *pHddTdlsCtx, u8 *mac)
+int wlan_hdd_tdls_set_force_peer(hdd_adapter_t *pAdapter, u8 *mac,
+                                 tANI_BOOLEAN forcePeer)
 {
-    int i;
-    for (i=0; i<pHddTdlsCtx->forcePeerCnt; i++)
-    {
-        if (!memcmp(mac, pHddTdlsCtx->forcePeer[i].macAddr, 6))
-        {
-            VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-                      "%s: " MAC_ADDRESS_STR "is a forced peer",
-                      __func__,MAC_ADDR_ARRAY(mac));
-            return 1;
-        }
-    }
-    VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-              "%s: " MAC_ADDRESS_STR "is not a forced peer",
-              __func__,MAC_ADDR_ARRAY(mac));
-    return 0;
-}
-
-int wlan_hdd_tdls_remove_force_peer(hdd_adapter_t *pAdapter, u8 *mac)
-{
-    int i;
-    tdlsCtx_t *pHddTdlsCtx;
+    hddTdlsPeer_t *curr_peer;
     hdd_context_t *pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
 
     if ((NULL == pHddCtx)) return -1;
@@ -1308,99 +1289,10 @@
             "%s: unable to lock list", __func__);
         return -1;
     }
-
-    pHddTdlsCtx = WLAN_HDD_GET_TDLS_CTX_PTR(pAdapter);
-    if (NULL == pHddTdlsCtx)
-    {
-        mutex_unlock(&pHddCtx->tdls_lock);
-        return -1;
-    }
-
-    for (i=0; i<HDD_MAX_NUM_TDLS_STA; i++)
-    {
-        if (!memcmp(mac, pHddTdlsCtx->forcePeer[i].macAddr, 6))
-        {
-            /*
-             * Entry Already Exists,Clear the entry and return.
-             */
-            memset(pHddTdlsCtx->forcePeer[i].macAddr, 0, 6);
-            pHddTdlsCtx->forcePeerCnt--;
-            mutex_unlock(&pHddCtx->tdls_lock);
-            VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-                      "%s: Removed the requested foce peer", __func__);
-            return 0;
-        }
-    }
-
-    /* Should not have come here if the existing entry has to be removed */
-    mutex_unlock(&pHddCtx->tdls_lock);
-    VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_ERROR,
-              "%s: Failed to remove the requested force peer: No Entry Found ", __func__);
-    return -1;
-}
-
-int wlan_hdd_tdls_add_force_peer(hdd_adapter_t *pAdapter, u8 *mac)
-{
-    int i ;
-    tdlsCtx_t *pHddTdlsCtx;
-    tANI_U8 zeroMac[6] = {0};
-    hdd_context_t *pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
-
-    if ((NULL == pHddCtx)) return -1;
-
-    if (mutex_lock_interruptible(&pHddCtx->tdls_lock))
-    {
-        VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_ERROR,
-            "%s: unable to lock list", __func__);
-        return -1;
-    }
-
-    pHddTdlsCtx = WLAN_HDD_GET_TDLS_CTX_PTR(pAdapter);
-    if (NULL == pHddTdlsCtx)
-    {
-        mutex_unlock(&pHddCtx->tdls_lock);
-        return -1;
-    }
-
-    if (pHddTdlsCtx->forcePeerCnt >= HDD_MAX_NUM_TDLS_STA )
-    {
-       VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-                 "%s: Fail to add the requested foce peer : Quota Full"
-                 " forcePeerCnt is %d ", __func__,pHddTdlsCtx->forcePeerCnt);
-       /* Quota Full, hence return failure from here*/
+    curr_peer = wlan_hdd_tdls_find_peer(pAdapter, mac, FALSE);
+    if (curr_peer == NULL)
         goto error;
-    }
-
-    for (i=0; i<HDD_MAX_NUM_TDLS_STA; i++)
-    {
-        if (!memcmp(mac, pHddTdlsCtx->forcePeer[i].macAddr, 6))
-        {
-            /*
-             * Entry Already Exists,No need to create a new one.
-             * Hence return from here.
-             */
-            mutex_unlock(&pHddCtx->tdls_lock);
-            VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-              "%s: Add the requested foce peer success : Entry present"
-              " forcePeerCnt is %d ", __func__,
-              pHddTdlsCtx->forcePeerCnt);
-            return 0;
-        }
-    }
-    for (i=0; i<HDD_MAX_NUM_TDLS_STA; i++)
-    {
-        if ( !memcmp(pHddTdlsCtx->forcePeer[i].macAddr, zeroMac, 6) )
-        {
-            memcpy(pHddTdlsCtx->forcePeer[i].macAddr, mac, 6);
-            pHddTdlsCtx->forcePeerCnt++;
-            mutex_unlock(&pHddCtx->tdls_lock);
-            VOS_TRACE(VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-              "%s: Add the requested foce peer success at index i"
-              " forcePeerCnt is %d ", __func__, i,
-              pHddTdlsCtx->forcePeerCnt);
-            return 0;
-        }
-    }
+    curr_peer->isForcedPeer = forcePeer;
 error:
     mutex_unlock(&pHddCtx->tdls_lock);
     return -1;