wlan: Maximum allowed number of stations fix for IBSS

The maximum number of allowed stations should be same as hardware
supported stations when VSTA support is disabled. And when VSTA is
enabled, the maximum number of stations should increase.

The handling of error code from coalesce request was not handled
properly causing the kernel panic.

Change-Id: I30f5fd429ed5cb2b6282bd1c9df7b70df336ea14
CRs-Fixed: 536968
diff --git a/CORE/MAC/inc/aniGlobal.h b/CORE/MAC/inc/aniGlobal.h
index 7028559..1880321 100644
--- a/CORE/MAC/inc/aniGlobal.h
+++ b/CORE/MAC/inc/aniGlobal.h
@@ -920,6 +920,7 @@
     tANI_U8 deferredMsgCnt;
     tSirDFSChannelList    dfschannelList;
     tANI_U8 deauthMsgCnt;
+    tANI_U8 gLimIbssStaLimit;
 } tAniSirLim, *tpAniSirLim;
 
 typedef struct sLimMgmtFrameRegistration
diff --git a/CORE/MAC/src/pe/lim/limApi.c b/CORE/MAC/src/pe/lim/limApi.c
index 2e2e985..209a546 100644
--- a/CORE/MAC/src/pe/lim/limApi.c
+++ b/CORE/MAC/src/pe/lim/limApi.c
@@ -371,6 +371,7 @@
         limLog( pMac, LOGP, FL( "cfg get assoc sta limit failed" ));
     }
     pMac->lim.gLimAssocStaLimit = val;
+    pMac->lim.gLimIbssStaLimit = val;
 
     // Place holder for current authentication request
     // being handled
diff --git a/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c b/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c
index 9ec4525..9a9faa6 100644
--- a/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c
+++ b/CORE/MAC/src/pe/lim/limIbssPeerMgmt.c
@@ -1370,9 +1370,9 @@
 {
    tLimIbssPeerNode *pTempNode, *pPrevNode;
    tLimIbssPeerNode *pTempNextNode = NULL;
-   tpDphHashNode     pStaDs;
-   tANI_U16          peerIdx;
-   tANI_U16          staIndex;
+   tpDphHashNode     pStaDs=NULL;
+   tANI_U16          peerIdx=0;
+   tANI_U16          staIndex=0;
    tANI_U8           ucUcastSig;
    tANI_U8           ucBcastSig;
 
@@ -1518,14 +1518,18 @@
         /* Peer not in the list - Collect BSS description & add to the list */
         tANI_U32      frameLen;
         tSirRetStatus retCode;
-        PELOGW(limLog(pMac, LOGW, FL("IBSS Peer node does not exist, adding it***"));)
 
-#ifndef ANI_SIR_IBSS_PEER_CACHING
         /** Limit the Max number of IBSS Peers allowed as the max number of STA's allowed
          */
-        if (pMac->lim.gLimNumIbssPeers >= pMac->lim.maxStation)
+#ifndef ANI_SIR_IBSS_PEER_CACHINGT
+        if (pMac->lim.gLimNumIbssPeers >
+              (pMac->lim.gLimIbssStaLimit - IBSS_STATIONS_USED_DURING_INIT))
+        {
+            PELOGE(limLog(pMac, LOGE, FL("**** MAX STA LIMIT HAS REACHED ****"));)
             return eSIR_LIM_MAX_STA_REACHED_ERROR;
+        }
 #endif
+        PELOGW(limLog(pMac, LOGW, FL("IBSS Peer node does not exist, adding it***"));)
         frameLen = sizeof(tLimIbssPeerNode) + ieLen - sizeof(tANI_U32);
 
         pPeerNode = vos_mem_malloc((tANI_U16)frameLen);
@@ -1611,12 +1615,12 @@
 {
     tLimIbssPeerNode *pTempNode, *pPrevNode;
     tLimIbssPeerNode *pTempNextNode = NULL;
-    tANI_U16      peerIdx;
-    tpDphHashNode pStaDs;
-    tANI_U32 threshold;
-    tANI_U16 staIndex;
-    tANI_U8 ucUcastSig;
-    tANI_U8 ucBcastSig;
+    tANI_U16      peerIdx=0;
+    tpDphHashNode pStaDs=0;
+    tANI_U32 threshold=0;
+    tANI_U16 staIndex=0;
+    tANI_U8 ucUcastSig=0;
+    tANI_U8 ucBcastSig=0;
 
     /** MLM BSS is started and if PE in scanmode then MLM state will be waiting for probe resp.
      *  If Heart beat timeout triggers during this corner case then we need to reactivate HeartBeat timer 
diff --git a/CORE/MAC/src/pe/lim/limIbssPeerMgmt.h b/CORE/MAC/src/pe/lim/limIbssPeerMgmt.h
index bed0be1..13aafb8 100644
--- a/CORE/MAC/src/pe/lim/limIbssPeerMgmt.h
+++ b/CORE/MAC/src/pe/lim/limIbssPeerMgmt.h
@@ -54,6 +54,8 @@
 #include "sirCommon.h"
 #include "limUtils.h"
 
+#define IBSS_STATIONS_USED_DURING_INIT 4  //(broadcast + self + p2p + softap)
+
 void limIbssInit(tpAniSirGlobal);
 void limIbssDelete(tpAniSirGlobal,tpPESession psessionEntry);
 tSirRetStatus limIbssCoalesce(tpAniSirGlobal, tpSirMacMgmtHdr, tpSchBeaconStruct, tANI_U8*,tANI_U32, tANI_U16,tpPESession);
diff --git a/CORE/MAC/src/pe/sch/schBeaconProcess.c b/CORE/MAC/src/pe/sch/schBeaconProcess.c
index 1b6a178..3b61b62 100644
--- a/CORE/MAC/src/pe/sch/schBeaconProcess.c
+++ b/CORE/MAC/src/pe/sch/schBeaconProcess.c
@@ -477,112 +477,115 @@
         (psessionEntry->limSystemRole == eLIM_BT_AMP_STA_ROLE) ||
         (psessionEntry->limSystemRole == eLIM_STA_IN_IBSS_ROLE))
     {
-        // check for VHT capability
-        pStaDs = dphLookupHashEntry(pMac, pMh->sa, &aid,
-                                    &psessionEntry->dph.dphHashTable);
-        if (psessionEntry->vhtCapability && pBeacon->OperatingMode.present)
-        {
-            operMode = pStaDs->vhtSupportedChannelWidthSet ?
-                       eHT_CHANNEL_WIDTH_80MHZ :
-                       pStaDs->htSupportedChannelWidthSet ?
-                       eHT_CHANNEL_WIDTH_40MHZ: eHT_CHANNEL_WIDTH_20MHZ;
-            if (operMode != pBeacon->OperatingMode.chanWidth)
-            {
+       // check for VHT capability
+       pStaDs = dphLookupHashEntry(pMac, pMh->sa, &aid,
+             &psessionEntry->dph.dphHashTable);
+       if (NULL != pStaDs)
+       {
+          if (psessionEntry->vhtCapability && pBeacon->OperatingMode.present )
+          {
+             operMode = pStaDs->vhtSupportedChannelWidthSet ?
+                eHT_CHANNEL_WIDTH_80MHZ :
+                pStaDs->htSupportedChannelWidthSet ?
+                eHT_CHANNEL_WIDTH_40MHZ: eHT_CHANNEL_WIDTH_20MHZ;
+             if (operMode != pBeacon->OperatingMode.chanWidth)
+             {
                 PELOG1(limLog(pMac, LOG1,
-                              FL(" received OpMode Chanwidth %d, staIdx = %d"),
-                                          pBeacon->OperatingMode.chanWidth,
-                                          pStaDs->staIndex);)
-                PELOG1(limLog(pMac, LOG1, FL(" MAC - %0x:%0x:%0x:%0x:%0x:%0x"),
-                                          pMh->sa[0],
-                                          pMh->sa[1],
-                                          pMh->sa[2],
-                                          pMh->sa[3],
-                                          pMh->sa[4],
-                                          pMh->sa[5]);)
+                         FL(" received OpMode Chanwidth %d, staIdx = %d"),
+                         pBeacon->OperatingMode.chanWidth,
+                         pStaDs->staIndex);)
+                   PELOG1(limLog(pMac, LOG1, FL(" MAC - %0x:%0x:%0x:%0x:%0x:%0x"),
+                            pMh->sa[0],
+                            pMh->sa[1],
+                            pMh->sa[2],
+                            pMh->sa[3],
+                            pMh->sa[4],
+                            pMh->sa[5]);)
 
-                if (pBeacon->OperatingMode.chanWidth == eHT_CHANNEL_WIDTH_80MHZ)
-                {
-                    PELOG1(limLog(pMac, LOG1,
-                                 FL("Updating the CH Width to 80MHz"));)
-                    pStaDs->vhtSupportedChannelWidthSet =
-                                         WNI_CFG_VHT_CHANNEL_WIDTH_80MHZ;
-                    pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_40MHZ;
-                }
-                else if (pBeacon->OperatingMode.chanWidth == eHT_CHANNEL_WIDTH_40MHZ)
-                {
-                    PELOG1(limLog(pMac, LOG1,
+                   if (pBeacon->OperatingMode.chanWidth == eHT_CHANNEL_WIDTH_80MHZ)
+                   {
+                      PELOG1(limLog(pMac, LOG1,
+                               FL("Updating the CH Width to 80MHz"));)
+                         pStaDs->vhtSupportedChannelWidthSet =
+                         WNI_CFG_VHT_CHANNEL_WIDTH_80MHZ;
+                      pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_40MHZ;
+                   }
+                   else if (pBeacon->OperatingMode.chanWidth == eHT_CHANNEL_WIDTH_40MHZ)
+                   {
+                      PELOG1(limLog(pMac, LOG1,
+                               FL("Updating the CH Width to 40MHz"));)
+                         pStaDs->vhtSupportedChannelWidthSet =
+                         WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ;
+                      pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_40MHZ;
+                   }
+                   else if (pBeacon->OperatingMode.chanWidth ==
+                         eHT_CHANNEL_WIDTH_20MHZ)
+                   {
+                      PELOG1(limLog(pMac, LOG1,
+                               FL("Updating the CH Width to 20MHz"));)
+                         pStaDs->vhtSupportedChannelWidthSet =
+                         WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ;
+                      pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_20MHZ;
+                   }
+                limCheckVHTOpModeChange(pMac, psessionEntry,
+                      pBeacon->OperatingMode.chanWidth,
+                      pStaDs->staIndex);
+             }
+          }
+          else if (psessionEntry->vhtCapability && pBeacon->VHTOperation.present)
+          {
+             operMode = pStaDs->vhtSupportedChannelWidthSet;
+             if (operMode != pBeacon->VHTOperation.chanWidth)
+             {
+                PELOG1(limLog(pMac, LOG1,
+                         FL(" received VHTOP CHWidth %d staIdx = %d"),
+                         pBeacon->VHTOperation.chanWidth,
+                         pStaDs->staIndex);)
+                   PELOG1(limLog(pMac, LOG1, FL(" MAC - %0x:%0x:%0x:%0x:%0x:%0x"),
+                            pMh->sa[0],
+                            pMh->sa[1],
+                            pMh->sa[2],
+                            pMh->sa[3],
+                            pMh->sa[4],
+                            pMh->sa[5]);)
+
+                   if (pBeacon->VHTOperation.chanWidth ==
+                         WNI_CFG_VHT_CHANNEL_WIDTH_80MHZ)
+                   {
+                      PELOG1(limLog(pMac, LOG1,
+                               FL("Updating the CH Width to 80MHz"));)
+                         pStaDs->vhtSupportedChannelWidthSet =
+                         WNI_CFG_VHT_CHANNEL_WIDTH_80MHZ;
+                      pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_40MHZ;
+                      chWidth = eHT_CHANNEL_WIDTH_80MHZ;
+                   }
+                   else if (pBeacon->VHTOperation.chanWidth ==
+                         WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ)
+                   {
+                      pStaDs->vhtSupportedChannelWidthSet =
+                         WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ;
+                      if (pBeacon->HTCaps.supportedChannelWidthSet)
+                      {
+                         PELOG1(limLog(pMac, LOG1,
                                   FL("Updating the CH Width to 40MHz"));)
-                    pStaDs->vhtSupportedChannelWidthSet =
-                                             WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ;
-                    pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_40MHZ;
-                }
-                else if (pBeacon->OperatingMode.chanWidth ==
-                                eHT_CHANNEL_WIDTH_20MHZ)
-                {
-                    PELOG1(limLog(pMac, LOG1,
+                            pStaDs->htSupportedChannelWidthSet =
+                            eHT_CHANNEL_WIDTH_40MHZ;
+                         chWidth = eHT_CHANNEL_WIDTH_40MHZ;
+                      }
+                      else
+                      {
+                         PELOG1(limLog(pMac, LOG1,
                                   FL("Updating the CH Width to 20MHz"));)
-                    pStaDs->vhtSupportedChannelWidthSet =
-                                            WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ;
-                    pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_20MHZ;
-                }
+                            pStaDs->htSupportedChannelWidthSet =
+                            eHT_CHANNEL_WIDTH_20MHZ;
+                         chWidth = eHT_CHANNEL_WIDTH_20MHZ;
+                      }
+                   }
                 limCheckVHTOpModeChange(pMac, psessionEntry,
-                                        pBeacon->OperatingMode.chanWidth,
-                                        pStaDs->staIndex);
-            }
-        }
-        else if (psessionEntry->vhtCapability && pBeacon->VHTOperation.present)
-        {
-            operMode = pStaDs->vhtSupportedChannelWidthSet;
-            if (operMode != pBeacon->VHTOperation.chanWidth)
-            {
-                PELOG1(limLog(pMac, LOG1,
-                       FL(" received VHTOP CHWidth %d staIdx = %d"),
-                                          pBeacon->VHTOperation.chanWidth,
-                                          pStaDs->staIndex);)
-                PELOG1(limLog(pMac, LOG1, FL(" MAC - %0x:%0x:%0x:%0x:%0x:%0x"),
-                                          pMh->sa[0],
-                                          pMh->sa[1],
-                                          pMh->sa[2],
-                                          pMh->sa[3],
-                                          pMh->sa[4],
-                                          pMh->sa[5]);)
-
-                if (pBeacon->VHTOperation.chanWidth ==
-                                WNI_CFG_VHT_CHANNEL_WIDTH_80MHZ)
-                {
-                    PELOG1(limLog(pMac, LOG1,
-                                  FL("Updating the CH Width to 80MHz"));)
-                    pStaDs->vhtSupportedChannelWidthSet =
-                                                 WNI_CFG_VHT_CHANNEL_WIDTH_80MHZ;
-                    pStaDs->htSupportedChannelWidthSet = eHT_CHANNEL_WIDTH_40MHZ;
-                    chWidth = eHT_CHANNEL_WIDTH_80MHZ;
-                }
-                else if (pBeacon->VHTOperation.chanWidth ==
-                                WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ)
-                {
-                    pStaDs->vhtSupportedChannelWidthSet =
-                            WNI_CFG_VHT_CHANNEL_WIDTH_20_40MHZ;
-                    if (pBeacon->HTCaps.supportedChannelWidthSet)
-                    {
-                        PELOG1(limLog(pMac, LOG1,
-                                      FL("Updating the CH Width to 40MHz"));)
-                        pStaDs->htSupportedChannelWidthSet =
-                                                     eHT_CHANNEL_WIDTH_40MHZ;
-                        chWidth = eHT_CHANNEL_WIDTH_40MHZ;
-                    }
-                    else
-                    {
-                        PELOG1(limLog(pMac, LOG1,
-                                      FL("Updating the CH Width to 20MHz"));)
-                        pStaDs->htSupportedChannelWidthSet =
-                                                     eHT_CHANNEL_WIDTH_20MHZ;
-                        chWidth = eHT_CHANNEL_WIDTH_20MHZ;
-                    }
-                }
-                limCheckVHTOpModeChange(pMac, psessionEntry,
-                                chWidth, pStaDs->staIndex);
-            }
-        }
+                      chWidth, pStaDs->staIndex);
+             }
+          }
+       }
     }
 #endif